diff --git a/src/resolveGitDir.js b/src/resolveGitDir.js index 6a0698110..786daf6d9 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) { diff --git a/test/resolveGitDir.spec.js b/test/resolveGitDir.spec.js index 4411a313a..53e4e8d7d 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)