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

overriding env variable to find git dir #629

Merged
Merged
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
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