Skip to content

Commit 5892455

Browse files
carnunokonet
authored andcommittedJun 13, 2019
fix: Override env GIT_DIR variable to resolve to the correct git dir path (#629)
Fixes #627
1 parent bffef73 commit 5892455

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed
 

‎src/resolveGitDir.js

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const path = require('path')
55

66
module.exports = async function resolveGitDir(options) {
77
try {
8+
// git cli uses GIT_DIR to fast track its response however it might be set to a different path
9+
// depending on where the caller initiated this from, hence clear GIT_DIR
10+
delete process.env.GIT_DIR
811
const gitDir = await execGit(['rev-parse', '--show-toplevel'], options)
912
return path.normalize(gitDir)
1013
} catch (error) {

‎test/resolveGitDir.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ describe('resolveGitDir', () => {
2121
process.cwd = processCwdBkp
2222
})
2323

24+
it('should resolve to the parent dir when .git is in the parent dir even when the GIT_DIR environment variable is set', async () => {
25+
const expected = path.dirname(__dirname)
26+
const processCwdBkp = process.cwd
27+
process.cwd = () => __dirname
28+
process.env.GIT_DIR = 'wrong/path/.git' // refer to https://github.com/DonJayamanne/gitHistoryVSCode/issues/233#issuecomment-375769718
29+
expect(path.resolve(await resolveGitDir())).toEqual(expected)
30+
process.cwd = processCwdBkp
31+
})
32+
2433
it('should return null when not in a git directory', async () => {
2534
const gitDir = await resolveGitDir({ cwd: '/' }) // assume root is not a git directory
2635
expect(gitDir).toEqual(null)

0 commit comments

Comments
 (0)
Please sign in to comment.