Skip to content

Commit

Permalink
fix: Override env GIT_DIR variable to resolve to the correct git dir …
Browse files Browse the repository at this point in the history
…path (#629)

Fixes #627
  • Loading branch information
carnun authored and okonet committed Jun 13, 2019
1 parent bffef73 commit 5892455
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/resolveGitDir.js
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions test/resolveGitDir.spec.js
Expand Up @@ -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)
Expand Down

0 comments on commit 5892455

Please sign in to comment.