Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

turnDiffTree.ts

174 lines4.7 KB

1import type { TurnDiffFileChange } from "../types";
2​
3export interface TurnDiffStat {
4 additions: number;
5 deletions: number;
6}
7​
8export interface TurnDiffTreeDirectoryNode {
9 kind: "directory";
10 name: string;
11 path: string;
12 stat: TurnDiffStat;
13 children: TurnDiffTreeNode[];
15​
16export interface TurnDiffTreeFileNode {
17 kind: "file";
18 name: string;
19 path: string;
20 stat: TurnDiffStat | null;
22​
23export type TurnDiffTreeNode = TurnDiffTreeDirectoryNode | TurnDiffTreeFileNode;
24​
25interface MutableDirectoryNode {
26 name: string;
27 path: string;
28 stat: TurnDiffStat;
29 directories: Map<string, MutableDirectoryNode>;
30 files: TurnDiffTreeFileNode[];
32​
33let sortCollator: Intl.Collator | undefined;
34​
35function normalizePathSegments(pathValue: string): string[] {
36 return pathValue
37 .replaceAll("\\", "/")
38 .split("/")
39 .filter((segment) => segment.length > 0);
41​
42function compareByName(a: { name: string }, b: { name: string }): number {
43 // Defer ICU initialization until the first name comparison.
44 sortCollator ??= new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
45 return sortCollator.compare(a.name, b.name);
47​
48function readStat(file: TurnDiffFileChange): TurnDiffStat | null {
49 if (typeof file.additions !== "number" || typeof file.deletions !== "number") {
50 return null;
51 }
52 return {
53 additions: file.additions,
54 deletions: file.deletions,
55 };
57​
58function compactDirectoryNode(node: TurnDiffTreeDirectoryNode): TurnDiffTreeDirectoryNode {
59 const compactedChildren = node.children.map((child) =>
60 child.kind === "directory" ? compactDirectoryNode(child) : child,
61 );
62​
63 let compactedNode: TurnDiffTreeDirectoryNode = {
64 ...node,
65 children: compactedChildren,
66 };
67​
68 while (compactedNode.children.length === 1 && compactedNode.children[0]?.kind === "directory") {
69 const onlyChild = compactedNode.children[0];
70 compactedNode = {
71 kind: "directory",
72 name: `${compactedNode.name}/${onlyChild.name}`,
73 path: onlyChild.path,
74 stat: onlyChild.stat,
75 children: onlyChild.children,
76 };
77 }
78​
79 return compactedNode;
81​
82function toTreeNodes(directory: MutableDirectoryNode): TurnDiffTreeNode[] {
83 const subdirectories: TurnDiffTreeDirectoryNode[] = Array.from(directory.directories.values())
84 .toSorted(compareByName)
85 .map<TurnDiffTreeDirectoryNode>((subdirectory) => ({
86 kind: "directory",
87 name: subdirectory.name,
88 path: subdirectory.path,
89 stat: {
90 additions: subdirectory.stat.additions,
91 deletions: subdirectory.stat.deletions,
92 },
93 children: toTreeNodes(subdirectory),
94 }))
95 .map((subdirectory) => compactDirectoryNode(subdirectory));
96​
97 const files = directory.files.toSorted(compareByName);
98 return [...subdirectories, ...files];
100​
101export function summarizeTurnDiffStats(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffStat {
102 return files.reduce(
103 (acc, file) => {
104 const stat = readStat(file);
105 if (!stat) return acc;
106 return {
107 additions: acc.additions + stat.additions,
108 deletions: acc.deletions + stat.deletions,
109 };
110 },
111 { additions: 0, deletions: 0 },
112 );
114​
115export function buildTurnDiffTree(files: ReadonlyArray<TurnDiffFileChange>): TurnDiffTreeNode[] {
116 const root: MutableDirectoryNode = {
117 name: "",
118 path: "",
119 stat: { additions: 0, deletions: 0 },
120 directories: new Map(),
121 files: [],
122 };
123​
124 for (const file of files) {
125 const segments = normalizePathSegments(file.path);
126 if (segments.length === 0) {
127 continue;
128 }
129​
130 const filePath = segments.join("/");
131 const fileName = segments.at(-1);
132 if (!fileName) {
133 continue;
134 }
135 const stat = readStat(file);
136 const ancestors: MutableDirectoryNode[] = [root];
137 let currentDirectory = root;
138​
139 for (const segment of segments.slice(0, -1)) {
140 const nextPath = currentDirectory.path ? `${currentDirectory.path}/${segment}` : segment;
141 const existing = currentDirectory.directories.get(segment);
142 if (existing) {
143 currentDirectory = existing;
144 } else {
145 const created: MutableDirectoryNode = {
146 name: segment,
147 path: nextPath,
148 stat: { additions: 0, deletions: 0 },
149 directories: new Map(),
150 files: [],
151 };
152 currentDirectory.directories.set(segment, created);
153 currentDirectory = created;
154 }
155 ancestors.push(currentDirectory);
156 }
157​
158 currentDirectory.files.push({
159 kind: "file",
160 name: fileName,
161 path: filePath,
162 stat,
163 });
164​
165 if (stat) {
166 for (const ancestor of ancestors) {
167 ancestor.stat.additions += stat.additions;
168 ancestor.stat.deletions += stat.deletions;
169 }
170 }
171 }
172​
173 return toTreeNodes(root);