Skip to content

Commit 5a2c44e

Browse files
committed
fix(web): show a type change as one file in the diff tree
1 parent 2266935 commit 5a2c44e

3 files changed

Lines changed: 49 additions & 2 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: "b/AGENTS.md", prevName: "a/AGENTS.md" } as FileDiffMetadata,
122+
{ type: "new", name: "b/AGENTS.md", prevName: "a/AGENTS.md" } as FileDiffMetadata,
123+
{ type: "change", name: "b/CLAUDE.md", prevName: "a/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
@@ -31,6 +31,23 @@ describe("diffFileTreeEntries", () => {
3131
});
3232
});
3333

34+
describe("diffFileTreeEntries", () => {
35+
it("folds a file-to-symlink type change into one modified entry", () => {
36+
expect(
37+
diffFileTreeEntries([
38+
file("change", "CLAUDE.md"),
39+
file("deleted", "AGENTS.md"),
40+
file("new", "AGENTS.md"),
41+
file("new", "docs/new.md"),
42+
]),
43+
).toEqual([
44+
{ path: "CLAUDE.md", status: "modified" },
45+
{ path: "AGENTS.md", status: "modified" },
46+
{ path: "docs/new.md", status: "added" },
47+
]);
48+
});
49+
});
50+
3451
describe("collectDirectoryPaths", () => {
3552
it("lists every ancestor once, parents first, with Pierre's trailing slash", () => {
3653
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
/**

0 commit comments

Comments
 (0)