Repository: okonet/lint-staged
Pull request #629 (2019-06-12 13:16:36)

overriding env variable to find git dir
A potential, but slightly more hacky, fix for https://github.com/okonet/lint-staged/issues/627

codecov[bot]: # [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/629?src=pr&el=h1) Report
> Merging [#629](https://codecov.io/gh/okonet/lint-staged/pull/629?src=pr&el=desc) into [master](https://codecov.io/gh/okonet/lint-staged/commit/bffef73e0d542275e387280c3eeb7377307730d8?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/okonet/lint-staged/pull/629/graphs/tree.svg?width=650&token=LSoEgsjk4O&height=150&src=pr)](https://codecov.io/gh/okonet/lint-staged/pull/629?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #629      +/-   ##
==========================================
+ Coverage   98.15%   98.15%   +<.01%     
==========================================
  Files          14       14              
  Lines         379      380       +1     
  Branches       53       53              
==========================================
+ Hits          372      373       +1     
  Misses          7        7
```


| [Impacted Files](https://codecov.io/gh/okonet/lint-staged/pull/629?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/resolveGitDir.js](https://codecov.io/gh/okonet/lint-staged/pull/629/diff?src=pr&el=tree#diff-c3JjL3Jlc29sdmVHaXREaXIuanM=) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/okonet/lint-staged/pull/629?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/okonet/lint-staged/pull/629?src=pr&el=footer). Last update [bffef73...c179a31](https://codecov.io/gh/okonet/lint-staged/pull/629?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

carnun: Please refer to comments made here - https://github.com/okonet/lint-staged/issues/627#issuecomment-501276271
vadimka123: Updating git to latest version (2.21.0) resolve issue
Not need this changes

Patch:
diff --git a/src/resolveGitDir.js b/src/resolveGitDir.js
index 6a06981..786daf6 100644
--- a/src/resolveGitDir.js
+++ b/src/resolveGitDir.js
@@ -5,6 +5,9 @@ const path = require('path')
 
 module.exports = async function resolveGitDir(options) {
   try {
+    // git cli uses GIT_DIR to fast track its response however it might be set to a different path
+    // depending on where the caller initiated this from, hence clear GIT_DIR
+    delete process.env.GIT_DIR
     const gitDir = await execGit(['rev-parse', '--show-toplevel'], options)
     return path.normalize(gitDir)
   } catch (error) {


Test patch:
diff --git a/test/resolveGitDir.spec.js b/test/resolveGitDir.spec.js
index 4411a31..53e4e8d 100644
--- a/test/resolveGitDir.spec.js
+++ b/test/resolveGitDir.spec.js
@@ -21,6 +21,15 @@ describe('resolveGitDir', () => {
     process.cwd = processCwdBkp
   })
 
+  it('should resolve to the parent dir when .git is in the parent dir even when the GIT_DIR environment variable is set', async () => {
+    const expected = path.dirname(__dirname)
+    const processCwdBkp = process.cwd
+    process.cwd = () => __dirname
+    process.env.GIT_DIR = 'wrong/path/.git' // refer to https://github.com/DonJayamanne/gitHistoryVSCode/issues/233#issuecomment-375769718
+    expect(path.resolve(await resolveGitDir())).toEqual(expected)
+    process.cwd = processCwdBkp
+  })
+
   it('should return null when not in a git directory', async () => {
     const gitDir = await resolveGitDir({ cwd: '/' }) // assume root is not a git directory
     expect(gitDir).toEqual(null)