Repository: isomorphic-git/isomorphic-git (js)

Issue:
isogit init in existing repo overwrites .git/config unconditionally
git init documentation and behavior indicate that git init in an existing repository do not remove information from .git/config.  isomorphic-git init unconditionally overwrites .git/config.

I'd suggest that checking for the existence of .git/config and not changing it if present would be more appropriate behavior.

Pull request:
resolves #899 do not overwrite existing .git/config on init
resolves #899 
## I'm fixing a bug or typo

- [ ] if this is your first time contributing, run `npm run add-contributor` and follow the prompts to add yourself to the README

running this made no changes, I got:

$ npm run add-contributor

> isomorphic-git@0.0.0-development add-contributor /Users/david/projects/isomorphic-git/isomorphic-git
> nps contributors.add

nps is executing `contributors.add` : all-contributors add
 is/are invalid contribution type(s)
The script called "contributors.add" which runs "all-contributors add" failed with exit code 1 https://github.com/sezna/nps/blob/v5.9.8/other/ERRORS_AND_WARNINGS.md#failed-with-exit-code
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! isomorphic-git@0.0.0-development add-contributor: `nps contributors.add`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the isomorphic-git@0.0.0-development add-contributor script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/david/.npm/_logs/2019-10-19T02_31_37_634Z-debug.log



Patch:
diff --git a/.all-contributorsrc b/.all-contributorsrc
index 8fbe5122..d8b2f6f3 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -356,6 +356,17 @@
         "bug",
         "code"
       ]
+    },
+    {
+      "login": "djencks",
+      "name": "djencks",
+      "avatar_url": "https://avatars2.githubusercontent.com/u/569822?v=4",
+      "profile": "https://github.com/djencks",
+      "contributions": [
+        "code",
+        "test"
+      ]
     }
-  ]
+  ],
+  "commitConvention": "angular"
 }
diff --git a/README.md b/README.md
index c3e73dda..e023867f 100644
--- a/README.md
+++ b/README.md
@@ -332,6 +332,9 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
     <td align="center"><a href="https://twitter.com/addaleax"><img src="https://avatars2.githubusercontent.com/u/899444?v=4&s=60" width="60px;" alt="Anna Henningsen"/><br /><sub><b>Anna Henningsen</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=addaleax" title="Code">💻</a></td>
     <td align="center"><a href="https://hen.ne.ke"><img src="https://avatars0.githubusercontent.com/u/4312191?v=4&s=60" width="60px;" alt="Fabian Henneke"/><br /><sub><b>Fabian Henneke</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/issues?q=author%3AFabianHenneke" title="Bug reports">🐛</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=FabianHenneke" title="Code">💻</a></td>
   </tr>
+  <tr>
+    <td align="center"><a href="https://github.com/djencks"><img src="https://avatars2.githubusercontent.com/u/569822?v=4" width="60px;" alt="djencks"/><br /><sub><b>djencks</b></sub></a><br /><a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=djencks" title="Code">💻</a> <a href="https://github.com/isomorphic-git/isomorphic-git/commits?author=djencks" title="Tests">⚠️</a></td>
+  </tr>
 </table>
 
 <!-- markdownlint-enable -->
diff --git a/src/commands/init.js b/src/commands/init.js
index 82ada8bc..a099427e 100644
--- a/src/commands/init.js
+++ b/src/commands/init.js
@@ -12,6 +12,7 @@ import { cores } from '../utils/plugins.js'
  * @param {string} [args.dir] - The [working tree](dir-vs-gitdir.md) directory path
  * @param {string} [args.gitdir=join(dir,'.git')] - [required] The [git directory](dir-vs-gitdir.md) path
  * @param {boolean} [args.bare = false] - Initialize a bare repository
+ * @param {boolean} [args.noOverwrite = false] - Detect if this is already a git repo and do not re-write `.git/config`
  * @returns {Promise<void>}  Resolves successfully when filesystem operations are complete
  *
  * @example
@@ -24,10 +25,12 @@ export async function init ({
   bare = false,
   dir,
   gitdir = bare ? dir : join(dir, '.git'),
-  fs: _fs = cores.get(core).get('fs')
+  fs: _fs = cores.get(core).get('fs'),
+  noOverwrite = false
 }) {
   try {
     const fs = new FileSystem(_fs)
+    if (noOverwrite && await fs.exists(gitdir + '/config')) return
     let folders = [
       'hooks',
       'info',
@@ -43,12 +46,12 @@ export async function init ({
     await fs.write(
       gitdir + '/config',
       '[core]\n' +
-        '\trepositoryformatversion = 0\n' +
-        '\tfilemode = false\n' +
-        `\tbare = ${bare}\n` +
-        (bare ? '' : '\tlogallrefupdates = true\n') +
-        '\tsymlinks = false\n' +
-        '\tignorecase = true\n'
+      '\trepositoryformatversion = 0\n' +
+      '\tfilemode = false\n' +
+      `\tbare = ${bare}\n` +
+      (bare ? '' : '\tlogallrefupdates = true\n') +
+      '\tsymlinks = false\n' +
+      '\tignorecase = true\n'
     )
     await fs.write(gitdir + '/HEAD', 'ref: refs/heads/master\n')
   } catch (err) {
diff --git a/src/index.d.ts b/src/index.d.ts
index 93cc669c..b2c44220 100644
--- a/src/index.d.ts
+++ b/src/index.d.ts
@@ -449,6 +449,7 @@ export function init(args: GitDir & {
   core?: string;
   fs?: any;
   bare?: boolean;
+  noOverwrite?: boolean;
 }): Promise<void>;
 
 export function isDescendent(args: GitDir & {


Test patch:
diff --git a/__tests__/test-init.js b/__tests__/test-init.js
index c695bc6a..00360f1a 100644
--- a/__tests__/test-init.js
+++ b/__tests__/test-init.js
@@ -2,6 +2,7 @@
 const { makeFixture } = require('./__helpers__/FixtureFS.js')
 
 const { init } = require('isomorphic-git')
+const { config } = require('isomorphic-git')
 
 describe('init', () => {
   it('init', async () => {
@@ -20,4 +21,40 @@ describe('init', () => {
     expect(await fs.exists(`${dir}/refs/heads`)).toBe(true)
     expect(await fs.exists(`${dir}/HEAD`)).toBe(true)
   })
+  it('init config init overwrite', async () => {
+    // Setup
+    const { fs, dir } = await makeFixture('test-init')
+    const name = 'me'
+    const email = 'meme'
+    await init({ dir })
+    expect(await fs.exists(dir)).toBe(true)
+    expect(await fs.exists(`${dir}/.git/config`)).toBe(true)
+    await config({ dir, path: 'user.name', value: name })
+    await config({ dir, path: 'user.email', value: email })
+    // Test
+    await init({ dir })
+    expect(await fs.exists(dir)).toBe(true)
+    expect(await fs.exists(`${dir}/.git/config`)).toBe(true)
+    // check that the properties we added got removed
+    expect(await config({ dir, path: 'user.name' })).toBeUndefined()
+    expect(await config({ dir, path: 'user.email' })).toBeUndefined()
+  })
+  it('init config init no overwrite', async () => {
+    // Setup
+    const { fs, dir } = await makeFixture('test-init')
+    const name = 'me'
+    const email = 'meme'
+    await init({ dir })
+    expect(await fs.exists(dir)).toBe(true)
+    expect(await fs.exists(`${dir}/.git/config`)).toBe(true)
+    await config({ dir, path: 'user.name', value: name })
+    await config({ dir, path: 'user.email', value: email })
+    // Test
+    await init({ dir, noOverwrite: true })
+    expect(await fs.exists(dir)).toBe(true)
+    expect(await fs.exists(`${dir}/.git/config`)).toBe(true)
+    // check that the properties we added are still there.
+    expect(await config({ dir, path: 'user.name' })).toEqual(name)
+    expect(await config({ dir, path: 'user.email' })).toEqual(email)
+  })
 })