Skip to content

Commit a2d2649

Browse files
mohit-daymaclaude
andcommitted
fix(web): commit tree keeps backslashes in Git paths
Git separates folders with `/` only, so a POSIX file name can contain a literal backslash. The commit tree split on it, so excluding such a file recorded a different path and the file was still committed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3bdeb0a commit a2d2649

4 files changed

Lines changed: 49 additions & 26 deletions

File tree

‎apps/web/src/components/CommitFileTree.logic.test.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { buildTurnDiffTree } from "~/lib/turnDiffTree";
43
import {
4+
buildCommitFileTree,
55
flattenCommitFileTree,
66
resolveCommitFileTreeSelection,
77
toggleCommitFileTreeNode,
88
} from "./CommitFileTree.logic";
99

10-
const tree = buildTurnDiffTree([
11-
{ path: "src/app/page.tsx", additions: 1, deletions: 0 },
12-
{ path: "src/app/layout.tsx", additions: 2, deletions: 1 },
13-
{ path: "src/lib/util.ts", additions: 3, deletions: 0 },
14-
{ path: "README.md", additions: 1, deletions: 1 },
10+
const tree = buildCommitFileTree([
11+
{ path: "src/app/page.tsx", insertions: 1, deletions: 0 },
12+
{ path: "src/app/layout.tsx", insertions: 2, deletions: 1 },
13+
{ path: "src/lib/util.ts", insertions: 3, deletions: 0 },
14+
{ path: "README.md", insertions: 1, deletions: 1 },
1515
]);
1616
const src = tree[0]!;
1717

@@ -50,4 +50,18 @@ describe("commit file tree", () => {
5050
const partial = new Set(["src/lib/util.ts", "README.md"]);
5151
expect([...toggleCommitFileTreeNode(src, partial)]).toEqual(["README.md"]);
5252
});
53+
54+
it("keeps a backslash in a Git path as part of the file name", () => {
55+
const [docs] = buildCommitFileTree([
56+
{ path: "docs/a\\b.md", insertions: 1, deletions: 0 },
57+
{ path: "docs/c.md", insertions: 1, deletions: 0 },
58+
]);
59+
60+
expect(flattenCommitFileTree([docs!], new Set()).map((row) => row.node.path)).toEqual([
61+
"docs",
62+
"docs/a\\b.md",
63+
"docs/c.md",
64+
]);
65+
expect([...toggleCommitFileTreeNode(docs!, new Set())]).toEqual(["docs/a\\b.md", "docs/c.md"]);
66+
});
5367
});

‎apps/web/src/components/CommitFileTree.logic.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { type TurnDiffTreeNode } from "~/lib/turnDiffTree";
1+
import { buildTurnDiffTree, type TurnDiffTreeNode } from "~/lib/turnDiffTree";
2+
3+
export interface CommitFile {
4+
path: string;
5+
insertions: number;
6+
deletions: number;
7+
}
28

39
export interface CommitFileTreeRow {
410
node: TurnDiffTreeNode;
@@ -7,6 +13,17 @@ export interface CommitFileTreeRow {
713

814
export type CommitFileTreeSelection = "all" | "some" | "none";
915

16+
export function buildCommitFileTree(files: ReadonlyArray<CommitFile>): TurnDiffTreeNode[] {
17+
return buildTurnDiffTree(
18+
files.map((file) => ({
19+
path: file.path,
20+
additions: file.insertions,
21+
deletions: file.deletions,
22+
})),
23+
{ splitBackslashes: false },
24+
);
25+
}
26+
1027
export function flattenCommitFileTree(
1128
nodes: ReadonlyArray<TurnDiffTreeNode>,
1229
collapsedDirectories: ReadonlySet<string>,

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
import { ChevronRightIcon, FolderIcon, FolderOpenIcon } from "lucide-react";
22
import { useState } from "react";
33
import { Checkbox } from "~/components/ui/checkbox";
4-
import { buildTurnDiffTree } from "~/lib/turnDiffTree";
54
import { cn } from "~/lib/utils";
65
import { formatDiffCount } from "@t3tools/client-runtime/diff-count";
76
import {
7+
buildCommitFileTree,
8+
type CommitFile,
89
flattenCommitFileTree,
910
resolveCommitFileTreeSelection,
1011
toggleCommitFileTreeNode,
1112
} from "./CommitFileTree.logic";
1213

13-
interface CommitFile {
14-
path: string;
15-
insertions: number;
16-
deletions: number;
17-
}
18-
1914
export function CommitFileTree(props: {
2015
files: ReadonlyArray<CommitFile>;
2116
excludedFiles: ReadonlySet<string>;
@@ -28,13 +23,7 @@ export function CommitFileTree(props: {
2823
const [collapsedDirectories, setCollapsedDirectories] = useState<ReadonlySet<string>>(
2924
() => new Set(),
3025
);
31-
const tree = buildTurnDiffTree(
32-
files.map((file) => ({
33-
path: file.path,
34-
additions: file.insertions,
35-
deletions: file.deletions,
36-
})),
37-
);
26+
const tree = buildCommitFileTree(files);
3827
const rows = flattenCommitFileTree(tree, collapsedDirectories);
3928

4029
const toggleDirectory = (path: string) => {

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ interface MutableDirectoryNode {
3232

3333
let sortCollator: Intl.Collator | undefined;
3434

35-
function normalizePathSegments(pathValue: string): string[] {
36-
return pathValue
37-
.replaceAll("\\", "/")
35+
function normalizePathSegments(pathValue: string, splitBackslashes: boolean): string[] {
36+
return (splitBackslashes ? pathValue.replaceAll("\\", "/") : pathValue)
3837
.split("/")
3938
.filter((segment) => segment.length > 0);
4039
}
@@ -93,7 +92,11 @@ function toTreeNodes(directory: MutableDirectoryNode): TurnDiffTreeNode[] {
9392

9493
export function buildTurnDiffTree<
9594
File extends Pick<TurnDiffFileChange, "path" | "additions" | "deletions">,
96-
>(files: ReadonlyArray<File>): TurnDiffTreeNode[] {
95+
>(
96+
files: ReadonlyArray<File>,
97+
// Git paths only use `/`; a backslash there is part of a POSIX file name.
98+
{ splitBackslashes = true }: { splitBackslashes?: boolean } = {},
99+
): TurnDiffTreeNode[] {
97100
const root: MutableDirectoryNode = {
98101
name: "",
99102
path: "",
@@ -103,7 +106,7 @@ export function buildTurnDiffTree<
103106
};
104107

105108
for (const file of files) {
106-
const segments = normalizePathSegments(file.path);
109+
const segments = normalizePathSegments(file.path, splitBackslashes);
107110
if (segments.length === 0) {
108111
continue;
109112
}

0 commit comments

Comments
 (0)