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

fix: Git directory is not resolved if GIT_WORK_TREE is set to relative path #887

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
2 changes: 2 additions & 0 deletions lib/resolveGitRepo.js
Expand Up @@ -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))
Expand Down
10 changes: 10 additions & 0 deletions test/resolveGitRepo.spec.js
Expand Up @@ -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)
Expand Down