Skip to content

Commit 2266935

Browse files
committed
fix(web): keep a file-to-symlink type change from crashing the diff view
1 parent d29c56a commit 2266935

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,33 @@ describe("diff file reconciliation", () => {
107107
expect(buildFileDiffRenderKey(file)).toBe(key);
108108
});
109109

110+
it("gives a type change its own identity per block", () => {
111+
const patch = [
112+
"diff --git a/AGENTS.md b/AGENTS.md",
113+
"deleted file mode 100644",
114+
"--- a/AGENTS.md",
115+
"+++ /dev/null",
116+
"@@ -1 +0,0 @@",
117+
"-duplicated instructions",
118+
"diff --git a/AGENTS.md b/AGENTS.md",
119+
"new file mode 120000",
120+
"--- /dev/null",
121+
"+++ b/AGENTS.md",
122+
"@@ -0,0 +1 @@",
123+
"+CLAUDE.md",
124+
].join("\n");
125+
const parsed = getRenderablePatch(patch, "type-change");
126+
expect(parsed?.kind).toBe("files");
127+
if (parsed?.kind !== "files") return;
128+
const [deleted, added] = parsed.files;
129+
expect(deleted?.type).toBe("deleted");
130+
expect(added?.type).toBe("new");
131+
if (!deleted || !added) return;
132+
133+
expect(buildFileDiffIdentityKey(deleted)).not.toBe(buildFileDiffIdentityKey(added));
134+
expect(new Set(parsed.files.map(buildFileDiffIdentityKey)).size).toBe(parsed.files.length);
135+
});
136+
110137
it("keeps identities stable and versions local to the changed file", () => {
111138
const patch = (secondLine: string) =>
112139
[

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,13 @@ export function resolveFileDiffPreviousPath(fileDiff: FileDiffMetadata): string
163163
return raw;
164164
}
165165

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

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

0 commit comments

Comments
 (0)