Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated resolver to ignore currently set GIT_DIR #628

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/resolveGitDir.js
Expand Up @@ -5,7 +5,12 @@ const path = require('path')

module.exports = async function resolveGitDir(options) {
try {
const gitDir = await execGit(['rev-parse', '--show-toplevel'], options)
// 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
const gitDir = await execGit(['rev-parse', '--show-toplevel'], {
...options,
env: { GIT_DIR: undefined }
})
return path.normalize(gitDir)
carnun marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
return null
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