diff --git a/.changeset/sour-kangaroos-jam.md b/.changeset/sour-kangaroos-jam.md new file mode 100644 index 0000000..c23a243 --- /dev/null +++ b/.changeset/sour-kangaroos-jam.md @@ -0,0 +1,5 @@ +--- +"pretty-quick": patch +--- + +fix: more robust computation of git directory diff --git a/src/scms/git.ts b/src/scms/git.ts index e51e3b7..5f1fea7 100644 --- a/src/scms/git.ts +++ b/src/scms/git.ts @@ -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[]) =>