diff --git a/lib/resolveGitRepo.js b/lib/resolveGitRepo.js index d194da039..a3e550998 100644 --- a/lib/resolveGitRepo.js +++ b/lib/resolveGitRepo.js @@ -35,6 +35,8 @@ const resolveGitRepo = async (cwd) => { // Unset GIT_DIR before running any git operations in case it's pointing to an incorrect location debugLog('Unset GIT_DIR (was `%s`)', process.env.GIT_DIR) delete process.env.GIT_DIR + debugLog('Unset GIT_WORK_TREE (was `%s`)', process.env.GIT_WORK_TREE) + delete process.env.GIT_WORK_TREE const gitDir = normalize(await execGit(['rev-parse', '--show-toplevel'], { cwd })) const gitConfigDir = normalize(await resolveGitConfigDir(gitDir)) diff --git a/test/resolveGitRepo.spec.js b/test/resolveGitRepo.spec.js index 11666bd95..46086ab41 100644 --- a/test/resolveGitRepo.spec.js +++ b/test/resolveGitRepo.spec.js @@ -34,6 +34,16 @@ describe('resolveGitRepo', () => { process.cwd = processCwdBkp }) + it('should resolve to the parent dir when .git is in the parent dir even when the GIT_WORK_TREE environment variable is set', async () => { + const expected = normalize(path.dirname(__dirname)) + const processCwdBkp = process.cwd + process.cwd = () => __dirname + process.env.GIT_WORK_TREE = './wrong/path/' + const { gitDir } = await resolveGitRepo() + expect(gitDir).toEqual(expected) + process.cwd = processCwdBkp + }) + it('should return null when not in a git directory', async () => { const { gitDir } = await resolveGitRepo({ cwd: '/' }) // assume root is not a git directory expect(gitDir).toEqual(null)