fix(review): resolve repo root for untracked file discovery (#450)

git ls-files scopes to the CWD subtree and returns CWD-relative paths,
unlike git diff HEAD which covers the full repo with root-relative paths.
When the hook process inherits a subdirectory CWD, untracked files outside
that subtree are silently dropped and paths for files inside it are wrong.

Resolve the repo root via rev-parse --show-toplevel and use it as the CWD
for both git ls-files and git diff --no-index in getUntrackedFileDiffs.

Fixes #449
This commit is contained in:
Ben Limmer
2026-03-31 12:08:44 -06:00
committed by GitHub
parent d8a67beb1c
commit cce877d3c4
2 changed files with 39 additions and 3 deletions
+13 -2
View File
@@ -174,9 +174,20 @@ async function getUntrackedFileDiffs(
dstPrefix = "b/",
cwd?: string,
): Promise<string> {
// git ls-files scopes to the CWD subtree and returns CWD-relative paths,
// unlike git diff HEAD which always covers the full repo with root-relative
// paths. Resolve the repo root so untracked files from the entire repo are
// included and their paths match the tracked-diff output.
const toplevelResult = await runtime.runGit(
["rev-parse", "--show-toplevel"],
{ cwd },
);
const rootCwd =
toplevelResult.exitCode === 0 ? toplevelResult.stdout.trim() : cwd;
const lsResult = await runtime.runGit(
["ls-files", "--others", "--exclude-standard"],
{ cwd },
{ cwd: rootCwd },
);
if (lsResult.exitCode !== 0) return "";
@@ -199,7 +210,7 @@ async function getUntrackedFileDiffs(
"/dev/null",
file,
],
{ cwd },
{ cwd: rootCwd },
);
return diffResult.stdout;
}),