Skip to content

Commit

Permalink
fix: detect git repo root correctly on cygwin (#1026)
Browse files Browse the repository at this point in the history
* Fix  #1025 using git rev-parse to get the relative path inside git repo

Co-authored-by: Thomas Kalmar <KalmarT@t-systems.com>
  • Loading branch information
tkalmar and Thomas Kalmar committed Oct 9, 2021
1 parent 32c08d3 commit f291824
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/resolveGitRepo.js
Expand Up @@ -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)

Expand All @@ -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)
Expand Down

0 comments on commit f291824

Please sign in to comment.