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 #1025 using git rev-parse to get the relative path inside git repo #1026

Merged
merged 2 commits into from Oct 9, 2021
Merged
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
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