Skip to content

Commit d2d3d82

Browse files
committed
fix: more robust computation of git directory
1 parent 56e166d commit d2d3d82

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/scms/git.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,30 @@ export const detect = (directory) => {
1414
cwd: directory,
1515
type: 'directory',
1616
});
17-
if (gitDirectory) {
18-
return dirname(gitDirectory);
19-
}
2017

2118
const gitWorktreeFile = findUp.sync('.git', {
2219
cwd: directory,
2320
type: 'file',
2421
});
22+
// if both of these are null then return null
23+
if (!gitDirectory && !gitWorktreeFile) {
24+
return null;
25+
}
2526

26-
if (gitWorktreeFile) {
27+
// if only one of these exists then return it
28+
if (gitDirectory && !gitWorktreeFile) {
29+
return dirname(gitDirectory);
30+
}
31+
if (gitWorktreeFile && !gitDirectory) {
2732
return dirname(gitWorktreeFile);
2833
}
34+
35+
const gitRepoDirectory = dirname(gitDirectory);
36+
const gitWorktreeDirectory = dirname(gitWorktreeFile);
37+
// return the deeper of these two
38+
return gitRepoDirectory.length > gitWorktreeDirectory.length
39+
? gitRepoDirectory
40+
: gitWorktreeDirectory;
2941
};
3042

3143
const runGit = (directory, args) =>

0 commit comments

Comments
 (0)