Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import type { FileTreeBatchOperation, GitStatus } from "@pierre/trees";
3
4import { resolveFileDiffPath } from "~/lib/diffRendering";
5
6/** One changed file as the tree shows it: its current path and how it changed. */
7export interface DiffFileTreeEntry {
8 readonly path: string;
9 readonly status: GitStatus;
10}
11
12function toGitStatus(file: FileDiffMetadata): GitStatus {
13 switch (file.type) {
14 case "new":
15 return "added";
16 case "deleted":
17 return "deleted";
18 case "rename-pure":
19 case "rename-changed":
20 return "renamed";
21 case "change":
22 return "modified";
23 }
24}
25
26/** Maps parsed diff files to tree entries, keeping the diff's own order. */
27export function diffFileTreeEntries(
28 files: ReadonlyArray<FileDiffMetadata>,
29): ReadonlyArray<DiffFileTreeEntry> {
30 return files.map((file) => ({ path: resolveFileDiffPath(file), status: toGitStatus(file) }));
31}
32
33/**
34 * Every directory on the way to each file, registered with the trailing slash Pierre uses for
35 * directory ids. Parents come before children so the tree can add them in order.
36 */
37export function collectDirectoryPaths(paths: ReadonlyArray<string>): ReadonlyArray<string> {
38 const directories = new Set<string>();
39 for (const path of paths) {
40 const segments = path.split("/");
41 let directory = "";
42 for (const segment of segments.slice(0, -1)) {
43 directory += `${segment}/`;
44 directories.add(directory);
45 }
46 }
47 return [...directories];
48}
49
50function pathDepth(path: string): number {
51 return path.split("/").filter(Boolean).length;
52}
53
54/**
55 * The adds and removes that turn one set of file paths into another, so a diff that changes
56 * under the reader (a new slice, a refresh after an agent edit) keeps the directories they
57 * have already opened or closed instead of rebuilding the tree from scratch.
58 *
59 * Directories are removed only once no file needs them; a directory that gains its first file
60 * is added before that file.
61 */
62export function buildDiffFileTreeUpdates(
63 previousPaths: ReadonlyArray<string>,
64 nextPaths: ReadonlyArray<string>,
65): FileTreeBatchOperation[] {
66 const previousDirectories = new Set(collectDirectoryPaths(previousPaths));
67 const nextDirectories = new Set(collectDirectoryPaths(nextPaths));
68 const previous = new Set(previousPaths);
69 const next = new Set(nextPaths);
70 const updates: FileTreeBatchOperation[] = [];
71
72 for (const path of previousPaths) {
73 if (!next.has(path)) updates.push({ type: "remove", path });
74 }
75 // Deepest first: a directory can only go once everything under it has.
76 const removedDirectories = [...previousDirectories]
77 .filter((directory) => !nextDirectories.has(directory))
78 .toSorted((left, right) => pathDepth(right) - pathDepth(left));
79 for (const directory of removedDirectories) {
80 updates.push({ type: "remove", path: directory, recursive: true });
81 }
82
83 // Shallowest first: a file's directory has to exist before the file does.
84 const addedDirectories = [...nextDirectories]
85 .filter((directory) => !previousDirectories.has(directory))
86 .toSorted((left, right) => pathDepth(left) - pathDepth(right));
87 for (const directory of addedDirectories) updates.push({ type: "add", path: directory });
88 for (const path of nextPaths) {
89 if (!previous.has(path)) updates.push({ type: "add", path });
90 }
91
92 return updates;
93}