Skip to content

Commit 33c6401

Browse files
only21milclaude
andauthored
fix(server): review guides keep excerpts in files with spaces or escaped names (#22053)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 79d9820 commit 33c6401

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎apps/server/src/pullRequest/pullRequestReviewGuideNormalize.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,37 @@ describe("parseReviewGuideChange", () => {
9191
expect(change.hunks.get("src/gone.ts")).toEqual({ new: [], old: [{ start: 1, end: 20 }] });
9292
expect(change.hunks.get("docs/readme.md")).toEqual({ new: [{ start: 3, end: 3 }], old: [] });
9393
});
94+
95+
it("reads file names git quotes or ends with a tab", () => {
96+
const unusual = [
97+
"diff --git a/with space.txt b/with space.txt",
98+
"--- a/with space.txt\t",
99+
"+++ b/with space.txt\t",
100+
"@@ -1,0 +2 @@",
101+
'diff --git "a/tab\\tname.txt" "b/tab\\tname.txt"',
102+
'--- "a/tab\\tname.txt"',
103+
'+++ "b/tab\\tname.txt"',
104+
"@@ -1,0 +2 @@",
105+
'diff --git "a/back\\\\slash.txt" "b/back\\\\slash.txt"',
106+
'--- "a/back\\\\slash.txt"',
107+
'+++ "b/back\\\\slash.txt"',
108+
"@@ -1,0 +2 @@",
109+
'diff --git "a/gone\\tfile.txt" "b/gone\\tfile.txt"',
110+
'--- "a/gone\\tfile.txt"',
111+
"+++ /dev/null",
112+
"@@ -1 +0,0 @@",
113+
"",
114+
].join("\n");
115+
const names = ["with space.txt", "tab\tname.txt", "back\\slash.txt", "gone\tfile.txt"];
116+
const parsed = parseReviewGuideChange(names.join("\0"), unusual);
117+
expect([...parsed.hunks.keys()]).toEqual(names);
118+
expect(parsed.hunks.get("gone\tfile.txt")).toEqual({ new: [], old: [{ start: 1, end: 1 }] });
119+
const result = normalizeReviewGuide(
120+
guide({ stops: [stop({ excerpts: names.slice(0, 3).map((path) => excerpt(path, 1, 5)) })] }),
121+
parsed,
122+
);
123+
expect(result.stops[0]?.excerpts).toEqual(names.slice(0, 3).map((path) => excerpt(path, 2, 2)));
124+
});
94125
});
95126

96127
describe("normalizeReviewGuide", () => {

‎apps/server/src/pullRequest/pullRequestReviewGuideNormalize.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PullRequestReviewGuideExcerpt, PullRequestReviewGuideStop } from "@t3tools/contracts";
22
import type { ReviewGuideGenerationResult } from "@t3tools/provider-core/server/textGeneration";
3+
import { unquoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
34

45
const MAX_EXCERPT_LINES = 25;
56

@@ -25,6 +26,18 @@ const hunkRange = (start: string, count: string | undefined): LineRange | null =
2526
return lines === 0 ? null : { start: Number(start), end: Number(start) + lines - 1 };
2627
};
2728

29+
/**
30+
* Git quotes a name holding a tab, quote or backslash, and ends an unquoted name holding a space
31+
* with a tab, so the first literal tab is never part of what the file is called.
32+
*/
33+
function headerPath(line: string, prefix: string): string | null {
34+
const rest = line.slice(4);
35+
const tab = rest.indexOf("\t");
36+
const token = tab === -1 ? rest : rest.slice(0, tab);
37+
const path = token.startsWith('"') ? unquoteGitPatchPath(token) : token;
38+
return path.startsWith(prefix) ? path.slice(prefix.length) : null;
39+
}
40+
2841
export function parseReviewGuideChange(nameOnly: string, patch: string): ReviewGuideChange {
2942
const hunks = new Map<string, FileHunks>();
3043
let oldPath: string | null = null;
@@ -37,9 +50,9 @@ export function parseReviewGuideChange(nameOnly: string, patch: string): ReviewG
3750
current = null;
3851
inHunks = false;
3952
} else if (!inHunks && line.startsWith("--- ")) {
40-
oldPath = line.startsWith("--- a/") ? line.slice(6) : null;
53+
oldPath = headerPath(line, "a/");
4154
} else if (!inHunks && line.startsWith("+++ ")) {
42-
const path = line.startsWith("+++ b/") ? line.slice(6) : oldPath;
55+
const path = headerPath(line, "b/") ?? oldPath;
4356
if (path === null) continue;
4457
current = hunks.get(path) ?? { new: [], old: [] };
4558
hunks.set(path, current);

0 commit comments

Comments
 (0)