Skip to content

Commit f291824

Browse files
tkalmarThomas Kalmar
andauthored
fix: detect git repo root correctly on cygwin (#1026)
* Fix #1025 using git rev-parse to get the relative path inside git repo Co-authored-by: Thomas Kalmar <[email protected]>
1 parent 32c08d3 commit f291824

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/resolveGitRepo.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,20 @@ const resolveGitConfigDir = async (gitDir) => {
2525
return path.resolve(gitDir, file.replace(/^gitdir: /, '')).trim()
2626
}
2727

28+
const determineGitDir = (cwd, relativeDir) => {
29+
if (relativeDir) {
30+
// the current working dir is inside the git top-level directory
31+
return normalize(cwd.substring(0, cwd.lastIndexOf(relativeDir)))
32+
} else {
33+
// the current working dir is the top-level git directory
34+
return normalize(cwd)
35+
}
36+
}
37+
2838
/**
2939
* Resolve git directory and possible submodule paths
3040
*/
31-
const resolveGitRepo = async (cwd) => {
41+
const resolveGitRepo = async (cwd = process.cwd()) => {
3242
try {
3343
debugLog('Resolving git repo from `%s`', cwd)
3444

@@ -38,7 +48,10 @@ const resolveGitRepo = async (cwd) => {
3848
debugLog('Unset GIT_WORK_TREE (was `%s`)', process.env.GIT_WORK_TREE)
3949
delete process.env.GIT_WORK_TREE
4050

41-
const gitDir = normalize(await execGit(['rev-parse', '--show-toplevel'], { cwd }))
51+
// read the path of the current directory relative to the top-level directory
52+
// don't read the toplevel directly, it will lead to an posix conform path on non posix systems (cygwin)
53+
const gitRel = normalize(await execGit(['rev-parse', '--show-prefix']))
54+
const gitDir = determineGitDir(cwd, gitRel)
4255
const gitConfigDir = normalize(await resolveGitConfigDir(gitDir))
4356

4457
debugLog('Resolved git directory to be `%s`', gitDir)

0 commit comments

Comments
 (0)