@@ -25,10 +25,20 @@ const resolveGitConfigDir = async (gitDir) => {
2525 return path . resolve ( gitDir , file . replace ( / ^ g i t d i r : / , '' ) ) . 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