Open History to see the last commit
1import type { ProjectEntry } from "@workbench/contracts";
2
3export interface WorkspaceTreeFileNode {
4 kind: "file";
5 name: string;
6 path: string;
7 changed: boolean;
8}
9
10export interface WorkspaceTreeDirectoryNode {
11 kind: "directory";
12 name: string;
13 path: string;
14 changed: boolean;
15 children: WorkspaceTreeNode[];
16}
17
18export type WorkspaceTreeNode = WorkspaceTreeDirectoryNode | WorkspaceTreeFileNode;
19
20interface MutableDirectoryNode {
21 kind: "directory";
22 name: string;
23 path: string;
24 changed: boolean;
25 directories: Map<string, MutableDirectoryNode>;
26 files: WorkspaceTreeFileNode[];
27}
28
29function basenameOf(path: string): string {
30 const separatorIndex = path.lastIndexOf("/");
31 return separatorIndex >= 0 ? path.slice(separatorIndex + 1) : path;
32}
33
34function compareNodeNames(
35 left: { name: string; kind: string },
36 right: { name: string; kind: string },
37) {
38 if (left.kind !== right.kind) {
39 return left.kind === "directory" ? -1 : 1;
40 }
41 return left.name.localeCompare(right.name, undefined, {
42 numeric: true,
43 sensitivity: "base",
44 });
45}
46
47function toTreeNodes(directory: MutableDirectoryNode): WorkspaceTreeNode[] {
48 const directories = [...directory.directories.values()]
49 .map<WorkspaceTreeDirectoryNode>((child) => ({
50 kind: "directory",
51 name: child.name,
52 path: child.path,
53 changed: child.changed,
54 children: toTreeNodes(child),
55 }))
56 .sort(compareNodeNames);
57 const files = [...directory.files].sort(compareNodeNames);
58 return [...directories, ...files];
59}
60
61export function buildWorkspaceFileTree(input: {
62 entries: ReadonlyArray<ProjectEntry>;
63 changedPaths?: ReadonlySet<string>;
64}): WorkspaceTreeNode[] {
65 const changedPaths = input.changedPaths ?? new Set<string>();
66 const root: MutableDirectoryNode = {
67 kind: "directory",
68 name: "",
69 path: "",
70 changed: false,
71 directories: new Map(),
72 files: [],
73 };
74
75 const sortedEntries = [...input.entries].sort((left, right) =>
76 left.path.localeCompare(right.path, undefined, {
77 numeric: true,
78 sensitivity: "base",
79 }),
80 );
81
82 for (const entry of sortedEntries) {
83 // Submodules + nested git repos can come back from `git ls-files --others`
84 // as paths with a trailing slash (e.g. `workbench/`). The server tags them
85 // as `kind: "file"`, but they're really opaque directories — promote them
86 // here so the tree renders them with a folder name + icon instead of an
87 // empty-name file row.
88 const hasTrailingSlash = entry.path.endsWith("/");
89 const normalizedPath = hasTrailingSlash ? entry.path.slice(0, -1) : entry.path;
90 const segments = normalizedPath.split("/").filter((segment) => segment.length > 0);
91 if (segments.length === 0) {
92 continue;
93 }
94
95 let current = root;
96 for (let index = 0; index < segments.length - 1; index += 1) {
97 const segment = segments[index]!;
98 const path = segments.slice(0, index + 1).join("/");
99 let next = current.directories.get(segment);
100 if (!next) {
101 next = {
102 kind: "directory",
103 name: segment,
104 path,
105 changed: false,
106 directories: new Map(),
107 files: [],
108 };
109 current.directories.set(segment, next);
110 }
111 current = next;
112 }
113
114 const changed = changedPaths.has(entry.path);
115 if (entry.kind === "directory" || hasTrailingSlash) {
116 const name = segments.at(-1)!;
117 let next = current.directories.get(name);
118 if (!next) {
119 next = {
120 kind: "directory",
121 name,
122 path: normalizedPath,
123 changed,
124 directories: new Map(),
125 files: [],
126 };
127 current.directories.set(name, next);
128 } else if (changed) {
129 next.changed = true;
130 }
131 continue;
132 }
133
134 current.files.push({
135 kind: "file",
136 name: basenameOf(normalizedPath),
137 path: normalizedPath,
138 changed,
139 });
140 }
141
142 const markChangedDirectories = (directory: MutableDirectoryNode): boolean => {
143 let changed = directory.changed;
144 for (const child of directory.directories.values()) {
145 changed = markChangedDirectories(child) || changed;
146 }
147 if (directory.files.some((file) => file.changed)) {
148 changed = true;
149 }
150 directory.changed = changed;
151 return changed;
152 };
153
154 markChangedDirectories(root);
155 return toTreeNodes(root);
156}