Skip to content

Commit 71aab56

Browse files
JounQincramakri
andauthored
fix: more robust computation of git directory (#183)
Co-authored-by: Chandrasekhar Ramakrishnan <cramakri@ethz.ch>
1 parent 93924ab commit 71aab56

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

.changeset/sour-kangaroos-jam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"pretty-quick": patch
3+
---
4+
5+
fix: more robust computation of git directory

src/scms/git.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,31 @@ export const detect = (directory: string) => {
1616
type: 'directory',
1717
})
1818

19-
if (gitDirectory) {
20-
return path.dirname(gitDirectory)
21-
}
22-
2319
const gitWorkTreeFile = findUp.sync('.git', {
2420
cwd: directory,
2521
type: 'file',
2622
})
2723

28-
if (gitWorkTreeFile) {
24+
// if both of these are null then return null
25+
if (!gitDirectory && !gitWorkTreeFile) {
26+
return null
27+
}
28+
29+
// if only one of these exists then return it
30+
if (gitDirectory && !gitWorkTreeFile) {
31+
return path.dirname(gitDirectory)
32+
}
33+
34+
if (gitWorkTreeFile && !gitDirectory) {
2935
return path.dirname(gitWorkTreeFile)
3036
}
37+
38+
const gitRepoDirectory = path.dirname(gitDirectory!)
39+
const gitWorkTreeDirectory = path.dirname(gitWorkTreeFile!)
40+
// return the deeper of these two
41+
return gitRepoDirectory.length > gitWorkTreeDirectory.length
42+
? gitRepoDirectory
43+
: gitWorkTreeDirectory
3144
}
3245

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

0 commit comments

Comments
 (0)