diff --git a/lib/resolveGitRepo.js b/lib/resolveGitRepo.js index a3e550998..03d04ed55 100644 --- a/lib/resolveGitRepo.js +++ b/lib/resolveGitRepo.js @@ -25,10 +25,20 @@ const resolveGitConfigDir = async (gitDir) => { return path.resolve(gitDir, file.replace(/^gitdir: /, '')).trim() } +const determineGitDir = (cwd, relativeDir) => { + if (relativeDir) { + // the current working dir is inside the git top-level directory + return normalize(cwd.substring(0, cwd.lastIndexOf(relativeDir))) + } else { + // the current working dir is the top-level git directory + return normalize(cwd) + } +} + /** * Resolve git directory and possible submodule paths */ -const resolveGitRepo = async (cwd) => { +const resolveGitRepo = async (cwd = process.cwd()) => { try { debugLog('Resolving git repo from `%s`', cwd) @@ -38,7 +48,10 @@ const resolveGitRepo = async (cwd) => { 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 })) + // read the path of the current directory relative to the top-level directory + // don't read the toplevel directly, it will lead to an posix conform path on non posix systems (cygwin) + const gitRel = normalize(await execGit(['rev-parse', '--show-prefix'])) + const gitDir = determineGitDir(cwd, gitRel) const gitConfigDir = normalize(await resolveGitConfigDir(gitDir)) debugLog('Resolved git directory to be `%s`', gitDir)