Skip to content

Commit

Permalink
fix: more robust computation of git directory
Browse files Browse the repository at this point in the history
  • Loading branch information
cramakri authored and JounQin committed Jan 17, 2024
1 parent 93924ab commit b32b9dc
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/scms/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,31 @@ export const detect = (directory: string) => {
type: 'directory',
})

if (gitDirectory) {
return path.dirname(gitDirectory)
}

const gitWorkTreeFile = findUp.sync('.git', {
cwd: directory,
type: 'file',
})

if (gitWorkTreeFile) {
// if both of these are null then return null
if (!gitDirectory && !gitWorkTreeFile) {
return null
}

// if only one of these exists then return it
if (gitDirectory && !gitWorkTreeFile) {
return path.dirname(gitDirectory)
}

if (gitWorkTreeFile && !gitDirectory) {
return path.dirname(gitWorkTreeFile)
}

const gitRepoDirectory = path.dirname(gitDirectory!)
const gitWorkTreeDirectory = path.dirname(gitWorkTreeFile!)
// return the deeper of these two
return gitRepoDirectory.length > gitWorkTreeDirectory.length
? gitRepoDirectory
: gitWorkTreeDirectory
}

const runGit = (directory: string, args: string[]) =>
Expand Down

0 comments on commit b32b9dc

Please sign in to comment.