Skip to content

Commit

Permalink
fix: more robust computation of git directory (#183)
Browse files Browse the repository at this point in the history
Co-authored-by: Chandrasekhar Ramakrishnan <cramakri@ethz.ch>
  • Loading branch information
JounQin and cramakri authored Jan 17, 2024
1 parent 93924ab commit 71aab56
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-kangaroos-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"pretty-quick": patch
---

fix: more robust computation of git directory
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 71aab56

Please # to comment.