Open History to see the last commit
1import type { FileDiffMetadata } from "@pierre/diffs";
2import type { FileTreeBatchOperation, FileTreeSortComparator, GitStatus } from "@pierre/trees";
3
4import { resolveFileDiffPath } from "~/lib/diffRendering";
5
6export interface DiffFileTreeEntry {
7 readonly path: string;
8 readonly status: GitStatus;
9}
10
11function toGitStatus(file: FileDiffMetadata): GitStatus {
12 switch (file.type) {
13 case "new":
14 return "added";
15 case "deleted":
16 return "deleted";
17 case "rename-pure":
18 case "rename-changed":
19 return "renamed";
20 case "change":
21 return "modified";
22 }
23}
24
25export function diffFileTreeEntries(
26 files: ReadonlyArray<FileDiffMetadata>,
27): ReadonlyArray<DiffFileTreeEntry> {
28 const statusByPath = new Map<string, GitStatus>();
29 for (const file of files) {
30 const path = resolveFileDiffPath(file);
31 const status = toGitStatus(file);
32 const previous = statusByPath.get(path);
33 statusByPath.set(path, previous === undefined || previous === status ? status : "modified");
34 }
35 return [...statusByPath].map(([path, status]) => ({ path, status }));
36}
37
38export function collectDirectoryPaths(paths: ReadonlyArray<string>): ReadonlyArray<string> {
39 const directories = new Set<string>();
40 for (const path of paths) {
41 const segments = path.split("/");
42 let directory = "";
43 for (const segment of segments.slice(0, -1)) {
44 directory += `${segment}/`;
45 directories.add(directory);
46 }
47 }
48 return [...directories];
49}
50
51export function diffFileTreePositions(paths: ReadonlyArray<string>): ReadonlyMap<string, number> {
52 const positions = new Map<string, number>();
53 paths.forEach((path, index) => {
54 positions.set(path, index);
55 let directory = "";
56 for (const segment of path.split("/").slice(0, -1)) {
57 directory += `${segment}/`;
58 if (!positions.has(directory)) positions.set(directory, index);
59 }
60 });
61 return positions;
62}
63
64export function compareDiffFileTreeEntries(
65 getPositions: () => ReadonlyMap<string, number>,
66): FileTreeSortComparator {
67 return (left, right) => {
68 const positions = getPositions();
69 return (
70 (positions.get(left.path) ?? Number.MAX_SAFE_INTEGER) -
71 (positions.get(right.path) ?? Number.MAX_SAFE_INTEGER) ||
72 left.depth - right.depth ||
73 left.path.localeCompare(right.path)
74 );
75 };
76}
77
78function pathDepth(path: string): number {
79 return path.split("/").filter(Boolean).length;
80}
81
82export function buildDiffFileTreeUpdates(
83 previousPaths: ReadonlyArray<string>,
84 nextPaths: ReadonlyArray<string>,
85): FileTreeBatchOperation[] {
86 const previousDirectories = new Set(collectDirectoryPaths(previousPaths));
87 const nextDirectories = new Set(collectDirectoryPaths(nextPaths));
88 const previous = new Set(previousPaths);
89 const next = new Set(nextPaths);
90 const updates: FileTreeBatchOperation[] = [];
91
92 for (const path of previousPaths) {
93 if (!next.has(path)) updates.push({ type: "remove", path });
94 }
95 // Deepest first: a directory can only go once everything under it has.
96 const removedDirectories = [...previousDirectories]
97 .filter((directory) => !nextDirectories.has(directory))
98 .toSorted((left, right) => pathDepth(right) - pathDepth(left));
99 for (const directory of removedDirectories) {
100 updates.push({ type: "remove", path: directory, recursive: true });
101 }
102
103 // Shallowest first: a file's directory has to exist before the file does.
104 const addedDirectories = [...nextDirectories]
105 .filter((directory) => !previousDirectories.has(directory))
106 .toSorted((left, right) => pathDepth(left) - pathDepth(right));
107 for (const directory of addedDirectories) updates.push({ type: "add", path: directory });
108 for (const path of nextPaths) {
109 if (!previous.has(path)) updates.push({ type: "add", path });
110 }
111
112 return updates;
113}