Skip to content

Commit 9ea9c3d

Browse files
Mnigosshivamhwp
andauthored
fix(web): keep a file-to-symlink type change from crashing the diff view (#11075)
Co-authored-by: shivam <[email protected]>
1 parent 52e4b44 commit 9ea9c3d

5 files changed

Lines changed: 87 additions & 8 deletions

File tree

‎apps/web/src/components/diffs/DiffFileTree.test.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { act, type MouseEvent, type ReactNode } from "react";
55
import { create, type ReactTestRenderer } from "react-test-renderer";
66
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
77

8+
import type { FileDiffMetadata } from "@pierre/diffs";
9+
810
import { DiffFileTree, type DiffFileTreeEntry } from "./DiffFileTree";
11+
import { diffFileTreeEntries } from "./diffFileTree.logic";
912
import { useCodeViewFileReveal } from "./useCodeViewFileReveal";
1013

1114
vi.mock("../../hooks/useTheme", () => ({ useTheme: () => ({ resolvedTheme: "dark" }) }));
@@ -112,6 +115,22 @@ describe("diff tree file activation", () => {
112115
vi.unstubAllGlobals();
113116
});
114117

118+
it("mounts a file-to-symlink type change as one tree row", async () => {
119+
// Git carries a type change as a deletion and an addition of the same path.
120+
const typeChange = diffFileTreeEntries([
121+
{ type: "deleted", name: "AGENTS.md", prevName: "AGENTS.md" } as FileDiffMetadata,
122+
{ type: "new", name: "AGENTS.md", prevName: "AGENTS.md" } as FileDiffMetadata,
123+
{ type: "change", name: "CLAUDE.md", prevName: "CLAUDE.md" } as FileDiffMetadata,
124+
]);
125+
await mount({ files: [...typeChange] });
126+
127+
const tree = model();
128+
expect(tree.getItem("AGENTS.md")?.isDirectory()).toBe(false);
129+
expect(tree.getItem("CLAUDE.md")?.isDirectory()).toBe(false);
130+
await activate("AGENTS.md");
131+
expect(targets).toEqual([{ type: "item", id: "AGENTS.md\u0000AGENTS.md", align: "start" }]);
132+
});
133+
115134
it("reissues the reveal when the sole selected file is activated again", async () => {
116135
await mount();
117136
await activate("02-short.ts");

‎apps/web/src/components/diffs/diffFileTree.logic.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ describe("diffFileTreeEntries", () => {
3434
});
3535
});
3636

37+
describe("diffFileTreeEntries", () => {
38+
it("folds a file-to-symlink type change into one modified entry", () => {
39+
expect(
40+
diffFileTreeEntries([
41+
file("change", "CLAUDE.md"),
42+
file("deleted", "AGENTS.md"),
43+
file("new", "AGENTS.md"),
44+
file("new", "docs/new.md"),
45+
]),
46+
).toEqual([
47+
{ path: "CLAUDE.md", status: "modified" },
48+
{ path: "AGENTS.md", status: "modified" },
49+
{ path: "docs/new.md", status: "added" },
50+
]);
51+
});
52+
});
53+
3754
describe("collectDirectoryPaths", () => {
3855
it("lists every ancestor once, parents first, with Pierre's trailing slash", () => {
3956
expect(collectDirectoryPaths(["apps/web/src/a.ts", "apps/web/b.ts", "README.md"])).toEqual([

‎apps/web/src/components/diffs/diffFileTree.logic.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,22 @@ function toGitStatus(file: FileDiffMetadata): GitStatus {
2323
}
2424
}
2525

26-
/** Maps parsed diff files to tree entries, keeping the diff's own order. */
26+
/**
27+
* Maps parsed diff files to tree entries, keeping the diff's own order. A path
28+
* appears once: a type change (regular file to symlink) is a deletion plus an
29+
* addition of the same path, and the tree shows the surviving file as modified.
30+
*/
2731
export function diffFileTreeEntries(
2832
files: ReadonlyArray<FileDiffMetadata>,
2933
): ReadonlyArray<DiffFileTreeEntry> {
30-
return files.map((file) => ({ path: resolveFileDiffPath(file), status: toGitStatus(file) }));
34+
const statusByPath = new Map<string, GitStatus>();
35+
for (const file of files) {
36+
const path = resolveFileDiffPath(file);
37+
const status = toGitStatus(file);
38+
const previous = statusByPath.get(path);
39+
statusByPath.set(path, previous === undefined || previous === status ? status : "modified");
40+
}
41+
return [...statusByPath].map(([path, status]) => ({ path, status }));
3142
}
3243

3344
/**

‎apps/web/src/lib/diffRendering.test.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ describe("buildPatchCacheKey", () => {
3535

3636
describe("getRenderablePatch", () => {
3737
it.each([
38-
["a/example.ts", "a/example.ts"],
39-
["b/example.ts", "b/example.ts"],
40-
["a/before.ts", "b/after.ts"],
41-
])("preserves repository paths from %s to %s", (previousPath, path) => {
38+
["a/example.ts", "a/example.ts", "change"],
39+
["b/example.ts", "b/example.ts", "change"],
40+
["a/before.ts", "b/after.ts", "rename-changed"],
41+
])("preserves repository paths from %s to %s", (previousPath, path, type) => {
4242
const parsed = getRenderablePatch(
4343
[
4444
`diff --git a/${previousPath} b/${path}`,
@@ -59,7 +59,7 @@ describe("getRenderablePatch", () => {
5959
if (!file) return;
6060
expect(resolveFileDiffPath(file)).toBe(path);
6161
expect(resolveFileDiffPreviousPath(file)).toBe(previousPath);
62-
expect(buildFileDiffIdentityKey(file)).toBe(`${previousPath}\0${path}`);
62+
expect(buildFileDiffIdentityKey(file)).toBe(`${previousPath}\0${path}\0${type}`);
6363
});
6464

6565
it("compacts partial hunk render offsets for virtualized review diffs", () => {
@@ -137,6 +137,33 @@ describe("diff file reconciliation", () => {
137137
expect(buildFileDiffRenderKey(file)).toBe(key);
138138
});
139139

140+
it("gives a type change its own identity per block", () => {
141+
const patch = [
142+
"diff --git a/AGENTS.md b/AGENTS.md",
143+
"deleted file mode 100644",
144+
"--- a/AGENTS.md",
145+
"+++ /dev/null",
146+
"@@ -1 +0,0 @@",
147+
"-duplicated instructions",
148+
"diff --git a/AGENTS.md b/AGENTS.md",
149+
"new file mode 120000",
150+
"--- /dev/null",
151+
"+++ b/AGENTS.md",
152+
"@@ -0,0 +1 @@",
153+
"+CLAUDE.md",
154+
].join("\n");
155+
const parsed = getRenderablePatch(patch, "type-change");
156+
expect(parsed?.kind).toBe("files");
157+
if (parsed?.kind !== "files") return;
158+
const [deleted, added] = parsed.files;
159+
expect(deleted?.type).toBe("deleted");
160+
expect(added?.type).toBe("new");
161+
if (!deleted || !added) return;
162+
163+
expect(buildFileDiffIdentityKey(deleted)).not.toBe(buildFileDiffIdentityKey(added));
164+
expect(new Set(parsed.files.map(buildFileDiffIdentityKey)).size).toBe(parsed.files.length);
165+
});
166+
140167
it("keeps identities stable and versions local to the changed file", () => {
141168
const patch = (secondLine: string) =>
142169
[

‎apps/web/src/lib/diffRendering.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,13 @@ export function resolveFileDiffPreviousPath(fileDiff: FileDiffMetadata): string
166166
return fileDiffPath(fileDiff.prevName ?? fileDiff.name ?? "");
167167
}
168168

169+
/**
170+
* Stable across re-renders of the same file, distinct for every block in a
171+
* patch. A type change (regular file to symlink) arrives as a deletion and an
172+
* addition of the same path, so the change type is part of the identity.
173+
*/
169174
export function buildFileDiffIdentityKey(fileDiff: FileDiffMetadata): string {
170-
return `${resolveFileDiffPreviousPath(fileDiff)}\u0000${resolveFileDiffPath(fileDiff)}`;
175+
return `${resolveFileDiffPreviousPath(fileDiff)}\u0000${resolveFileDiffPath(fileDiff)}\u0000${fileDiff.type}`;
171176
}
172177

173178
export function buildFileDiffRenderKey(fileDiff: FileDiffMetadata): string {

0 commit comments

Comments
 (0)