Skip to content

Commit 53e34b4

Browse files
committed
fix(review): align lazy previews with file metadata
1 parent 571d5a3 commit 53e34b4

6 files changed

Lines changed: 62 additions & 23 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ layer("BitbucketPullRequestApi.layer", (it) => {
376376
cursor: first.nextCursor!,
377377
});
378378
expect(last.patch).toBe("last file\n");
379+
expect(last.omittedFileStats).toBeUndefined();
379380
expect(last.nextCursor).toBeNull();
380381
expect(callAt(3).url).toBe("/repositories/acme/web/pullrequests/7/diffstat?pagelen=4&page=2");
381382
}),

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,16 +654,17 @@ export const make = Effect.gen(function* () {
654654
},
655655
{ concurrency: 4 },
656656
);
657+
const omittedFileStats = files.values.flatMap((file, index) => {
658+
const path = file.new?.path ?? file.old?.path;
659+
return path === undefined || !patches[index]?.truncated
660+
? []
661+
: [{ path, additions: file.lines_added, deletions: file.lines_removed }];
662+
});
657663
return {
658664
patch: patches.map((patch) => patch.body.replace(/\n?$/, "\n")).join(""),
659665
truncated: patches.some((patch) => patch.truncated),
660666
nextCursor: files.next ? String(page + 1) : null,
661-
omittedFileStats: files.values.flatMap((file) => {
662-
const path = file.new?.path ?? file.old?.path;
663-
return path === undefined
664-
? []
665-
: [{ path, additions: file.lines_added, deletions: file.lines_removed }];
666-
}),
667+
...(omittedFileStats.length > 0 ? { omittedFileStats } : {}),
667668
};
668669
}),
669670
);

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,42 @@ const runtime = Layer.mergeAll(Layer.mock(ForgejoCli)({ api }), VcsProcess.layer
4242

4343
afterEach(() => api.mockReset());
4444

45+
it.effect("emits ordinary rename paths without literal quotes", () =>
46+
Effect.gen(function* () {
47+
api.mockImplementation((request) => {
48+
if (request.path.endsWith("/pulls/1")) return json(pull);
49+
if (request.path.includes("/files?"))
50+
return json([
51+
{
52+
filename: "new.ts",
53+
previous_filename: "old.ts",
54+
status: "renamed",
55+
additions: 0,
56+
deletions: 0,
57+
},
58+
]);
59+
if (request.path.includes("/git/trees/"))
60+
return json({
61+
truncated: false,
62+
tree: [
63+
{
64+
path: request.path.includes("/head?") ? "new.ts" : "old.ts",
65+
mode: "100644",
66+
sha: "blob",
67+
},
68+
],
69+
});
70+
return json({ encoding: "base64", content: Buffer.from("unchanged\n").toString("base64") });
71+
});
72+
const provider = yield* Provider.make;
73+
const result = yield* provider.getDiff({ ...input, cursor: "1" });
74+
expect(result.patch).toBe(
75+
"diff --git a/old.ts b/new.ts\nrename from old.ts\nrename to new.ts\n--- a/old.ts\n+++ b/new.ts\n",
76+
);
77+
expect(result.omittedFileStats).toBeUndefined();
78+
}).pipe(Effect.provide(runtime)),
79+
);
80+
4581
it.effect("marks non-UTF-8 blobs as omitted instead of losing byte-only changes", () =>
4682
Effect.gen(function* () {
4783
api.mockImplementation((request) => {
@@ -69,7 +105,7 @@ it.effect("marks non-UTF-8 blobs as omitted instead of losing byte-only changes"
69105
const provider = yield* Provider.make;
70106
const result = yield* provider.getDiff({ ...input, cursor: "1" });
71107
expect(result.truncated).toBe(true);
72-
expect(result.patch).toContain('diff --git "a/legacy.txt" "b/legacy.txt"\nBinary files differ');
108+
expect(result.patch).toContain("diff --git a/legacy.txt b/legacy.txt\nBinary files differ");
73109
expect(result.omittedFileStats).toEqual([{ path: "legacy.txt", additions: 1, deletions: 1 }]);
74110
}).pipe(Effect.provide(runtime)),
75111
);
@@ -144,13 +180,9 @@ it.effect(
144180
expect(first.patch).toContain('rename to " new.ts"');
145181
expect(first.patch).toContain("-before\n+after\n");
146182
expect(first.patch).toContain("new file mode 100644\n--- /dev/null");
147-
expect(first.patch).toContain('deleted file mode 100644\n--- "a/deleted.ts"\n+++ /dev/null');
183+
expect(first.patch).toContain("deleted file mode 100644\n--- a/deleted.ts\n+++ /dev/null");
148184
expect(first.patch).toContain("Binary files differ");
149-
expect(first.omittedFileStats).toContainEqual({
150-
path: " new.ts",
151-
additions: 1,
152-
deletions: 1,
153-
});
185+
expect(first.omittedFileStats).toEqual([{ path: "binary.bin", additions: 0, deletions: 0 }]);
154186
const last = yield* provider.getDiff({ ...input, cursor: "2" });
155187
expect(last.nextCursor).toBeNull();
156188
expect(last.patch).toBe("");
@@ -180,7 +212,7 @@ it.effect("keeps later files reachable when tea truncates a file-content respons
180212
const provider = yield* Provider.make;
181213
const first = yield* provider.getDiff(input);
182214
expect(first.truncated).toBe(true);
183-
expect(first.patch).toContain('"b/large.ts"');
215+
expect(first.patch).toContain("b/large.ts");
184216
expect(first.omittedFileStats).toEqual([
185217
{ path: "large.ts", additions: 100000, deletions: 10 },
186218
]);
@@ -252,7 +284,7 @@ it.effect("preserves executable, symlink, and submodule changes in rebuilt previ
252284
expect(result.patch).toContain("old mode 100644\nnew mode 100755");
253285
expect(result.patch).toContain("old mode 100644\nnew mode 120000");
254286
expect(result.patch).toContain("-Subproject commit old-commit\n+Subproject commit new-commit");
255-
expect(result.omittedFileStats).toHaveLength(3);
287+
expect(result.omittedFileStats).toBeUndefined();
256288
}).pipe(Effect.provide(runtime)),
257289
);
258290

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ import {
3535
forgejoReactions,
3636
} from "./forgejoPullRequestJson.ts";
3737

38-
const quotePath = Schema.encodeEffect(Schema.fromJsonString(Schema.String));
38+
const encodeQuotedPath = Schema.encodeEffect(Schema.fromJsonString(Schema.String));
39+
const quotePath = (path: string) =>
40+
/[\s"\\]/.test(path) ? encodeQuotedPath(path) : Effect.succeed(path);
3941
const isOversizedResponse = (error: PullRequestProviderError) =>
4042
error.detail.includes("oversized") || error.detail.includes("exceeded the output limit");
4143

@@ -580,15 +582,16 @@ export const make = Effect.gen(function* () {
580582
),
581583
{ concurrency: 4 },
582584
);
585+
const omittedFileStats = files.flatMap((file, index) =>
586+
patches[index]?.truncated
587+
? [{ path: file.filename, additions: file.additions, deletions: file.deletions }]
588+
: [],
589+
);
583590
return {
584591
patch: patches.map((patch) => patch.patch).join(""),
585592
truncated: patches.some((patch) => patch.truncated),
586593
nextCursor: filePage.more ? String(page + 1) : null,
587-
omittedFileStats: files.map((file) => ({
588-
path: file.filename,
589-
additions: file.additions,
590-
deletions: file.deletions,
591-
})),
594+
...(omittedFileStats.length > 0 ? { omittedFileStats } : {}),
592595
};
593596
}),
594597
getDiffFileContents,

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2530,7 +2530,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
25302530
const tracked = yield* readTrackedDiff("HEAD");
25312531
return { ...tracked, stdoutTruncated: true };
25322532
}
2533-
const paths = splitNullSeparatedGitStdoutPaths(untracked.value);
2533+
const paths = splitNullSeparatedGitStdoutPaths(untracked.value).filter(
2534+
(candidate) => !input.file || candidate === input.file.path,
2535+
);
25342536
if (paths.length === 0) return yield* readTrackedDiff("HEAD");
25352537
const index = yield* prepareReviewIndex(cwd, paths).pipe(Effect.option);
25362538
if (index._tag === "Some") return yield* readTrackedDiff("HEAD", index.value);

‎apps/web/src/components/DiffPanel.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ export default function DiffPanel({
400400
environmentId: activeThread?.environmentId,
401401
cwd: branchDiffPreview.data?.cwd,
402402
source: lazySource,
403-
baseRef: selectedBaseRef,
403+
baseRef: lazySource?.baseRef ?? selectedBaseRef,
404404
ignoreWhitespace: diffIgnoreWhitespace,
405405
theme: resolvedTheme,
406406
revision: filePatchRevision,

0 commit comments

Comments
 (0)