Skip to content

Commit 62ab45e

Browse files
fix(source-control): GitCafe diff headers C-escape names; detail lists requested reviewers
From review on #17681: - Patch headers use the shared `quoteGitPatchPath`, so a name with a newline or tab no longer breaks `diff --git`/`---`/`+++`. A plain space is no longer quoted, which matches git and what the shared reader expects. - The detail's `reviewers` lists the reviewers GitCafe has asked, so they show before they review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c82eff7 commit 62ab45e

4 files changed

Lines changed: 40 additions & 13 deletions

File tree

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,13 +589,13 @@ describe("GitCafePullRequestProvider", () => {
589589
},
590590
);
591591

592-
it.effect("counts lines from hunk reads and offers only strategies no blocker names", () => {
592+
it.effect("reads line counts, open merge strategies and requested reviewers", () => {
593593
const snapshot = { version: 4, headOid, comparisonBaseOid: baseOid };
594594
const files = (from: number, count: number) =>
595595
Array.from({ length: count }, (_, index) => ({ path: `f${from + index}.ts` }));
596596
let changePage = 0;
597597
const server = fakeGitCafe({
598-
"GET /repos/owner/repo/pulls/7": pull,
598+
"GET /repos/owner/repo/pulls/7": { ...pull, reviewers: [{ actor }] },
599599
"GET /repos/owner/repo/pulls/7/status": {
600600
merge: {
601601
conflicts: "unknown",
@@ -633,6 +633,11 @@ describe("GitCafePullRequestProvider", () => {
633633
squash: false,
634634
rebase: true,
635635
});
636+
// A requested reviewer shows before they have reviewed.
637+
assert.deepStrictEqual(
638+
detail.reviewers.map((reviewer) => reviewer.login),
639+
["alice"],
640+
);
636641
}).pipe(Effect.provide(server.layer));
637642
});
638643

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ export const make = Effect.gen(function* () {
573573
changedFiles: changes?.changedFiles ?? 0,
574574
closedAt: pull.closedAt,
575575
mergedAt: pull.mergedAt,
576-
reviewers: [],
576+
reviewers: Json.toReviewers(pull, input.host),
577577
checks: checks === null ? [] : Json.toChecks(checks),
578578
...(status.checks.total === 0
579579
? {}

‎packages/source-control-gitcafe/src/server/gitCafePullRequestJson.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,8 +501,20 @@ describe("toDiff", () => {
501501
truncated: false,
502502
}),
503503
);
504-
assert.include(patch, 'rename from "old image.png"');
505-
assert.include(patch, 'Binary files "a/old image.png" and "b/new image.png" differ');
504+
// Git quotes a name for the bytes a header can't carry, not for a space.
505+
assert.include(patch, "rename from old image.png");
506+
assert.include(patch, "Binary files a/old image.png and b/new image.png differ");
507+
});
508+
509+
it("C-escapes a name whose newline or tab would break the patch headers", () => {
510+
const { patch } = GitCafePullRequestJson.toDiff(
511+
decodeDiff({
512+
items: [{ path: "two\nlines\tname.ts", status: "added", hunks: [] }],
513+
truncated: false,
514+
}),
515+
);
516+
assert.include(patch, 'diff --git "a/two\\nlines\\tname.ts" "b/two\\nlines\\tname.ts"');
517+
assert.notInclude(patch, "two\nlines");
506518
});
507519
});
508520

‎packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* @module source-control-gitcafe/server/gitCafePullRequestJson
66
*/
7+
import { quoteGitPatchPath } from "@t3tools/shared/gitPatchPath";
78
import * as DateTime from "effect/DateTime";
89
import * as Schema from "effect/Schema";
910
import {
@@ -261,6 +262,17 @@ function toActor(actor: Actor, host: string): PullRequestActor | null {
261262
};
262263
}
263264

265+
/** The reviewers GitCafe has asked, as the detail's reviewer list shows them. */
266+
export function toReviewers(
267+
pull: Pick<GitCafePull, "reviewers">,
268+
host: string,
269+
): ReadonlyArray<PullRequestActor> {
270+
return (pull.reviewers ?? []).flatMap(({ actor }) => {
271+
const reviewer = toActor(actor, host);
272+
return reviewer === null ? [] : [reviewer];
273+
});
274+
}
275+
264276
/** A linked GitHub reviewer answers to both logins, so either one finds the review request. */
265277
function reviewerLogins(actor: Actor, host: string): ReadonlyArray<string> {
266278
const reviewer = toActor(actor, host);
@@ -480,19 +492,14 @@ export function toStack(stack: GitCafeStack, target: RepositoryOnHost): Provider
480492
};
481493
}
482494

483-
/** Git's own quoting for a path a patch header would otherwise split. */
484-
function quotePath(path: string): string {
485-
return /[\s"\\]/u.test(path) ? `"${path.replace(/["\\]/gu, "\\$&")}"` : path;
486-
}
487-
488495
/** Converts GitCafe's structured hunks into a unified patch, keeping omitted files' counts. */
489496
export function toDiff(diff: typeof GitCafeDiff.Type): ProviderDiffSlice {
490497
const chunks: Array<string> = [];
491498
const omittedFileStats: Array<{ path: string; additions: number; deletions: number }> = [];
492499
for (const file of diff.items) {
493500
const oldPath = file.oldPath ?? file.path;
494-
const a = quotePath(`a/${oldPath}`);
495-
const b = quotePath(`b/${file.path}`);
501+
const a = quoteGitPatchPath(`a/${oldPath}`);
502+
const b = quoteGitPatchPath(`b/${file.path}`);
496503
const before = file.status === "added" ? "/dev/null" : a;
497504
const after = file.status === "deleted" ? "/dev/null" : b;
498505
const mode = file.isSubmodule ? "160000" : "100644";
@@ -501,7 +508,10 @@ export function toDiff(diff: typeof GitCafeDiff.Type): ProviderDiffSlice {
501508
if (file.status === "deleted") lines.push(`deleted file mode ${mode}`);
502509
if (file.status === "renamed" || file.status === "copied") {
503510
const action = file.status === "renamed" ? "rename" : "copy";
504-
lines.push(`${action} from ${quotePath(oldPath)}`, `${action} to ${quotePath(file.path)}`);
511+
lines.push(
512+
`${action} from ${quoteGitPatchPath(oldPath)}`,
513+
`${action} to ${quoteGitPatchPath(file.path)}`,
514+
);
505515
}
506516
if (file.binary) {
507517
lines.push(`Binary files ${before} and ${after} differ`);

0 commit comments

Comments
 (0)