From 9b8825976f5a8209e01a7fcae1b1e81561f30254 Mon Sep 17 00:00:00 2001 From: Thomas Kalmar Date: Thu, 7 Oct 2021 14:20:26 +0200 Subject: [PATCH 1/2] Fix #1025 using git rev-parse to get the relative path inside git repo --- lib/resolveGitRepo.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/resolveGitRepo.js b/lib/resolveGitRepo.js index a3e550998..b775b819f 100644 --- a/lib/resolveGitRepo.js +++ b/lib/resolveGitRepo.js @@ -38,7 +38,11 @@ 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 })) + const gitRel = normalize(await execGit(['rev-parse', '--show-prefix'])) + const currentWorkDir = cwd ? cwd : process.cwd() + const gitDir = gitRel + ? normalize(currentWorkDir.substring(0, currentWorkDir.lastIndexOf(gitRel))) + : currentWorkDir const gitConfigDir = normalize(await resolveGitConfigDir(gitDir)) debugLog('Resolved git directory to be `%s`', gitDir) From e43880dcf6d84afe99affec85ebf9efc2948edcc Mon Sep 17 00:00:00 2001 From: Thomas Kalmar Date: Thu, 7 Oct 2021 14:20:26 +0200 Subject: [PATCH 2/2] Fix #1025 using git rev-parse to get the relative path inside git repo --- lib/resolveGitRepo.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/resolveGitRepo.js b/lib/resolveGitRepo.js index b775b819f..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,11 +48,10 @@ const resolveGitRepo = async (cwd) => { debugLog('Unset GIT_WORK_TREE (was `%s`)', process.env.GIT_WORK_TREE) delete process.env.GIT_WORK_TREE + // 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 currentWorkDir = cwd ? cwd : process.cwd() - const gitDir = gitRel - ? normalize(currentWorkDir.substring(0, currentWorkDir.lastIndexOf(gitRel))) - : currentWorkDir + const gitDir = determineGitDir(cwd, gitRel) const gitConfigDir = normalize(await resolveGitConfigDir(gitDir)) debugLog('Resolved git directory to be `%s`', gitDir)