@catamorphic/git 0.0.0-stage → 0.1.0-alpha.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +202 -0
- package/NOTICE.txt +32 -0
- package/dist/checkout-remote-backend.d.ts +12 -0
- package/dist/checkout-remote-backend.d.ts.map +1 -0
- package/dist/file-reads.d.ts +15 -0
- package/dist/file-reads.d.ts.map +1 -0
- package/dist/fs-backend.d.ts +30 -0
- package/dist/fs-backend.d.ts.map +1 -0
- package/dist/fs-remote-backend.d.ts +47 -0
- package/dist/fs-remote-backend.d.ts.map +1 -0
- package/dist/git-object-codec.d.ts +31 -0
- package/dist/git-object-codec.d.ts.map +1 -0
- package/dist/git-sync.d.ts +61 -0
- package/dist/git-sync.d.ts.map +1 -0
- package/dist/in-memory-object-store.d.ts +23 -0
- package/dist/in-memory-object-store.d.ts.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4746 -0
- package/dist/index.js.map +32 -0
- package/dist/migrate-workflow.d.ts +12 -0
- package/dist/migrate-workflow.d.ts.map +1 -0
- package/dist/native-git.d.ts +25 -0
- package/dist/native-git.d.ts.map +1 -0
- package/dist/native-project-repo.d.ts +57 -0
- package/dist/native-project-repo.d.ts.map +1 -0
- package/dist/network-sync.d.ts +58 -0
- package/dist/network-sync.d.ts.map +1 -0
- package/dist/network.d.ts +81 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/object-remote-backend.d.ts +95 -0
- package/dist/object-remote-backend.d.ts.map +1 -0
- package/dist/object-store.d.ts +42 -0
- package/dist/object-store.d.ts.map +1 -0
- package/dist/origin-draft.d.ts +264 -0
- package/dist/origin-draft.d.ts.map +1 -0
- package/dist/origin-objects.d.ts +201 -0
- package/dist/origin-objects.d.ts.map +1 -0
- package/dist/personal-files.d.ts +6 -0
- package/dist/personal-files.d.ts.map +1 -0
- package/dist/project-manager.d.ts +214 -0
- package/dist/project-manager.d.ts.map +1 -0
- package/dist/project-repo.d.ts +79 -0
- package/dist/project-repo.d.ts.map +1 -0
- package/dist/ref-moved-error.d.ts +13 -0
- package/dist/ref-moved-error.d.ts.map +1 -0
- package/dist/ref-names.d.ts +18 -0
- package/dist/ref-names.d.ts.map +1 -0
- package/dist/types.d.ts +276 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/workspace-mirror.d.ts +110 -0
- package/dist/workspace-mirror.d.ts.map +1 -0
- package/package.json +53 -4
- package/src/checkout-remote-backend.ts +154 -0
- package/src/diff3.d.ts +16 -0
- package/src/file-reads.ts +53 -0
- package/src/fs-backend.ts +206 -0
- package/src/fs-remote-backend.ts +252 -0
- package/src/git-object-codec.ts +87 -0
- package/src/git-sync.ts +539 -0
- package/src/in-memory-object-store.ts +60 -0
- package/src/index.ts +110 -0
- package/src/migrate-workflow.ts +30 -0
- package/src/native-git.ts +177 -0
- package/src/native-project-repo.ts +515 -0
- package/src/network-sync.ts +239 -0
- package/src/network.ts +269 -0
- package/src/object-remote-backend.ts +366 -0
- package/src/object-store.ts +41 -0
- package/src/origin-draft.ts +1033 -0
- package/src/origin-objects.ts +906 -0
- package/src/personal-files.ts +57 -0
- package/src/project-manager.ts +743 -0
- package/src/project-repo.ts +800 -0
- package/src/ref-moved-error.ts +18 -0
- package/src/ref-names.ts +47 -0
- package/src/types.ts +289 -0
- package/src/workspace-mirror.ts +400 -0
- package/README.md +0 -3
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
PROJECT_APP_DATA_DIR,
|
|
5
|
+
PROJECT_PERSONAL_DIR,
|
|
6
|
+
publishedRef,
|
|
7
|
+
} from "@catamorphic/workflow/project-layout";
|
|
8
|
+
import { type FileReadOptions, readFileSnapshot } from "./file-reads.js";
|
|
9
|
+
import { hasLocalGit, nativeGit, nativeGitBytes } from "./native-git.js";
|
|
10
|
+
import {
|
|
11
|
+
ensurePersonalFilesExcluded,
|
|
12
|
+
isPersonalFile,
|
|
13
|
+
} from "./personal-files.js";
|
|
14
|
+
import {
|
|
15
|
+
assertSafePath,
|
|
16
|
+
ProjectRepoImpl,
|
|
17
|
+
walkDirectory,
|
|
18
|
+
} from "./project-repo.js";
|
|
19
|
+
import type { BranchInfo, CommitInfo, DiffEntry, RepoStatus } from "./types.js";
|
|
20
|
+
|
|
21
|
+
/** Local-checkout adapter. File IO stays shared; Git honors the user's repository format and tools. */
|
|
22
|
+
export class NativeProjectRepo extends ProjectRepoImpl {
|
|
23
|
+
protected override readonly followsSymlinks = true;
|
|
24
|
+
|
|
25
|
+
override async readFileBytes(filePath: string): Promise<Uint8Array | null> {
|
|
26
|
+
assertSafePath(filePath);
|
|
27
|
+
const target = path.join(this.repoPath, filePath);
|
|
28
|
+
const stat = await fs.lstat(target).catch((error: unknown) => {
|
|
29
|
+
if (
|
|
30
|
+
error instanceof Error &&
|
|
31
|
+
"code" in error &&
|
|
32
|
+
(error.code === "ENOENT" || error.code === "ENOTDIR")
|
|
33
|
+
)
|
|
34
|
+
return null;
|
|
35
|
+
throw error;
|
|
36
|
+
});
|
|
37
|
+
if (!stat) return null;
|
|
38
|
+
if (stat.isSymbolicLink())
|
|
39
|
+
return new TextEncoder().encode(await fs.readlink(target));
|
|
40
|
+
if (!stat.isFile()) return null;
|
|
41
|
+
if (stat.size > 64 * 1024 * 1024)
|
|
42
|
+
throw new Error(
|
|
43
|
+
"This file exceeds the 64 MB document limit. Open it from its folder.",
|
|
44
|
+
);
|
|
45
|
+
return this.readSnapshotBytes(filePath, 64 * 1024 * 1024);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
override async readAllFiles(
|
|
49
|
+
options: FileReadOptions = {},
|
|
50
|
+
): Promise<Record<string, string>> {
|
|
51
|
+
return readFileSnapshot({
|
|
52
|
+
paths: await this.listFiles(),
|
|
53
|
+
options: { maxFileBytes: 2 * 1024 * 1024, ...options },
|
|
54
|
+
read: async (file, maxBytes) => {
|
|
55
|
+
const stat = await fs.lstat(path.join(this.repoPath, file));
|
|
56
|
+
if (!stat.isFile() || stat.size > maxBytes) return null;
|
|
57
|
+
const bytes = await this.readSnapshotBytes(file, maxBytes);
|
|
58
|
+
if (bytes.includes(0)) return null;
|
|
59
|
+
try {
|
|
60
|
+
return new TextDecoder("utf8", { fatal: true }).decode(bytes);
|
|
61
|
+
} catch {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
override async listFiles(opts?: { prefix?: string }): Promise<string[]> {
|
|
69
|
+
if (!(await hasLocalGit({ path: this.repoPath }))) {
|
|
70
|
+
if (opts?.prefix) assertSafePath(opts.prefix);
|
|
71
|
+
const files = await walkDirectory(this.repoPath, this.repoPath, {
|
|
72
|
+
includeHidden: true,
|
|
73
|
+
excludeNestedRepositories: true,
|
|
74
|
+
tracked: new Set(),
|
|
75
|
+
trackedDirectories: new Set(),
|
|
76
|
+
rules: [],
|
|
77
|
+
});
|
|
78
|
+
return files
|
|
79
|
+
.filter((file) => !opts?.prefix || file.startsWith(opts.prefix))
|
|
80
|
+
.sort();
|
|
81
|
+
}
|
|
82
|
+
const pathspec = opts?.prefix ? ["--", opts.prefix] : [];
|
|
83
|
+
const [output, removed] = await Promise.all([
|
|
84
|
+
nativeGit(this.repoPath, [
|
|
85
|
+
"ls-files",
|
|
86
|
+
"--stage",
|
|
87
|
+
"--others",
|
|
88
|
+
"--exclude-standard",
|
|
89
|
+
"-z",
|
|
90
|
+
...pathspec,
|
|
91
|
+
]),
|
|
92
|
+
nativeGit(this.repoPath, ["ls-files", "--deleted", "-z", ...pathspec]),
|
|
93
|
+
]);
|
|
94
|
+
const deleted = new Set(removed.split("\0"));
|
|
95
|
+
const files = new Set<string>();
|
|
96
|
+
for (const record of output.split("\0").filter(Boolean)) {
|
|
97
|
+
const tracked = /^(\d{6}) [a-f0-9]+ [0-3]\t(.*)$/s.exec(record);
|
|
98
|
+
if (tracked?.[1] === "160000") continue;
|
|
99
|
+
const file = tracked?.[2] ?? record;
|
|
100
|
+
if (!isPersonalFile(file) && !deleted.has(file)) files.add(file);
|
|
101
|
+
}
|
|
102
|
+
return [...files].sort();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async findFilesContaining(input: {
|
|
106
|
+
text: string;
|
|
107
|
+
ref?: string;
|
|
108
|
+
globs: readonly string[];
|
|
109
|
+
}): Promise<string[]> {
|
|
110
|
+
if (!input.ref && !(await hasLocalGit({ path: this.repoPath }))) {
|
|
111
|
+
const matches = (file: string, glob: string) =>
|
|
112
|
+
path.matchesGlob(file, glob) ||
|
|
113
|
+
(!glob.includes("/") && path.matchesGlob(path.basename(file), glob));
|
|
114
|
+
const includes = input.globs.filter((glob) => !glob.startsWith(":!"));
|
|
115
|
+
const excludes = input.globs
|
|
116
|
+
.filter((glob) => glob.startsWith(":!"))
|
|
117
|
+
.map((glob) => glob.slice(2));
|
|
118
|
+
const files = await this.readAllFiles({
|
|
119
|
+
filter: (file) =>
|
|
120
|
+
includes.some((glob) => matches(file, glob)) &&
|
|
121
|
+
!excludes.some((glob) => matches(file, glob)),
|
|
122
|
+
});
|
|
123
|
+
return Object.entries(files)
|
|
124
|
+
.filter(([, content]) => content.includes(input.text))
|
|
125
|
+
.map(([file]) => file);
|
|
126
|
+
}
|
|
127
|
+
const ref = input.ref ? await this.resolveRef(input.ref) : undefined;
|
|
128
|
+
try {
|
|
129
|
+
const output = await nativeGit(this.repoPath, [
|
|
130
|
+
"grep",
|
|
131
|
+
"-l",
|
|
132
|
+
"-I",
|
|
133
|
+
"-z",
|
|
134
|
+
"-F",
|
|
135
|
+
...(ref ? [] : ["--untracked", "--exclude-standard"]),
|
|
136
|
+
"-e",
|
|
137
|
+
input.text,
|
|
138
|
+
...(ref ? [ref] : []),
|
|
139
|
+
"--",
|
|
140
|
+
...input.globs,
|
|
141
|
+
]);
|
|
142
|
+
return output
|
|
143
|
+
.split("\0")
|
|
144
|
+
.filter(Boolean)
|
|
145
|
+
.map((file) => (ref ? file.slice(ref.length + 1) : file))
|
|
146
|
+
.filter((file) => !isPersonalFile(file));
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (error instanceof Error && "code" in error && error.code === 1)
|
|
149
|
+
return [];
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
override async resolveRef(ref = "HEAD"): Promise<string> {
|
|
155
|
+
return (
|
|
156
|
+
await nativeGit(this.repoPath, [
|
|
157
|
+
"rev-parse",
|
|
158
|
+
"--verify",
|
|
159
|
+
"--end-of-options",
|
|
160
|
+
ref,
|
|
161
|
+
])
|
|
162
|
+
).trim();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
override async currentBranch(): Promise<string> {
|
|
166
|
+
if (!(await hasLocalGit({ path: this.repoPath }))) return "main";
|
|
167
|
+
return (
|
|
168
|
+
(await nativeGit(this.repoPath, ["branch", "--show-current"])).trim() ||
|
|
169
|
+
"HEAD"
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
override async status(): Promise<RepoStatus> {
|
|
174
|
+
if (!(await hasLocalGit({ path: this.repoPath }))) {
|
|
175
|
+
const modifiedFiles = await this.listFiles();
|
|
176
|
+
return {
|
|
177
|
+
branch: "main",
|
|
178
|
+
dirty: modifiedFiles.length > 0,
|
|
179
|
+
modifiedFiles,
|
|
180
|
+
baseCommit: null,
|
|
181
|
+
remoteHead: null,
|
|
182
|
+
ahead: 0,
|
|
183
|
+
behind: 0,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const output = await nativeGit(this.repoPath, [
|
|
187
|
+
"status",
|
|
188
|
+
"--porcelain=v1",
|
|
189
|
+
"-z",
|
|
190
|
+
"--untracked-files=all",
|
|
191
|
+
]);
|
|
192
|
+
const records = output.split("\0");
|
|
193
|
+
const modifiedFiles: string[] = [];
|
|
194
|
+
for (let i = 0; i < records.length; i++) {
|
|
195
|
+
const record = records[i];
|
|
196
|
+
if (!record) continue;
|
|
197
|
+
modifiedFiles.push(record.slice(3));
|
|
198
|
+
if (/[RC]/.test(record.slice(0, 2))) {
|
|
199
|
+
const previous = records[++i];
|
|
200
|
+
if (record.slice(0, 2).includes("R") && previous)
|
|
201
|
+
modifiedFiles.push(previous);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const baseCommit = await this.resolveRef().catch(() => null);
|
|
205
|
+
const remoteHead = await this.resolveRef(publishedRef()).catch(() => null);
|
|
206
|
+
const counts =
|
|
207
|
+
baseCommit && remoteHead
|
|
208
|
+
? (
|
|
209
|
+
await nativeGit(this.repoPath, [
|
|
210
|
+
"rev-list",
|
|
211
|
+
"--left-right",
|
|
212
|
+
"--count",
|
|
213
|
+
`${baseCommit}...${remoteHead}`,
|
|
214
|
+
])
|
|
215
|
+
)
|
|
216
|
+
.trim()
|
|
217
|
+
.split(/\s+/)
|
|
218
|
+
.map(Number)
|
|
219
|
+
: [0, 0];
|
|
220
|
+
return {
|
|
221
|
+
branch: await this.currentBranch(),
|
|
222
|
+
dirty: modifiedFiles.length > 0,
|
|
223
|
+
modifiedFiles,
|
|
224
|
+
baseCommit,
|
|
225
|
+
remoteHead,
|
|
226
|
+
ahead: counts[0] ?? 0,
|
|
227
|
+
behind: counts[1] ?? 0,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
override async commit(
|
|
232
|
+
message: string,
|
|
233
|
+
author: { name: string; email: string },
|
|
234
|
+
opts?: { paths?: readonly string[]; allowEmpty?: boolean },
|
|
235
|
+
): Promise<string> {
|
|
236
|
+
const paths = opts?.paths;
|
|
237
|
+
for (const file of paths ?? []) assertSafePath(file);
|
|
238
|
+
if (paths?.length === 0) return this.resolveRef();
|
|
239
|
+
if (!(await hasLocalGit({ path: this.repoPath })))
|
|
240
|
+
await nativeGit(this.repoPath, ["init", "--initial-branch=main"]);
|
|
241
|
+
const allIndexed = (
|
|
242
|
+
await nativeGit(this.repoPath, ["ls-files", "-z"])
|
|
243
|
+
).split("\0");
|
|
244
|
+
if (allIndexed.some(isPersonalFile))
|
|
245
|
+
throw new Error(
|
|
246
|
+
"Personal files are tracked. Remove them from the git index before checkpointing or sharing this project.",
|
|
247
|
+
);
|
|
248
|
+
await ensurePersonalFilesExcluded({ repoPath: this.repoPath });
|
|
249
|
+
// --only commits the requested working-tree paths without sweeping the user's existing index.
|
|
250
|
+
if (!paths) await nativeGit(this.repoPath, ["add", "-A"]);
|
|
251
|
+
else {
|
|
252
|
+
const indexed = new Set(
|
|
253
|
+
(
|
|
254
|
+
await nativeGit(this.repoPath, [
|
|
255
|
+
"--literal-pathspecs",
|
|
256
|
+
"ls-files",
|
|
257
|
+
"-z",
|
|
258
|
+
"--",
|
|
259
|
+
...paths,
|
|
260
|
+
])
|
|
261
|
+
).split("\0"),
|
|
262
|
+
);
|
|
263
|
+
const untracked: string[] = [];
|
|
264
|
+
for (const file of paths) {
|
|
265
|
+
if (
|
|
266
|
+
!indexed.has(file) &&
|
|
267
|
+
(await fs.lstat(path.join(this.repoPath, file)).then(
|
|
268
|
+
() => true,
|
|
269
|
+
() => false,
|
|
270
|
+
))
|
|
271
|
+
)
|
|
272
|
+
untracked.push(file);
|
|
273
|
+
}
|
|
274
|
+
if (untracked.length)
|
|
275
|
+
await nativeGit(this.repoPath, [
|
|
276
|
+
"--literal-pathspecs",
|
|
277
|
+
"add",
|
|
278
|
+
"--intent-to-add",
|
|
279
|
+
"--",
|
|
280
|
+
...untracked,
|
|
281
|
+
]);
|
|
282
|
+
}
|
|
283
|
+
await nativeGit(this.repoPath, [
|
|
284
|
+
"--literal-pathspecs",
|
|
285
|
+
"-c",
|
|
286
|
+
`user.name=${author.name}`,
|
|
287
|
+
"-c",
|
|
288
|
+
`user.email=${author.email}`,
|
|
289
|
+
"commit",
|
|
290
|
+
"-m",
|
|
291
|
+
message,
|
|
292
|
+
...(opts?.allowEmpty ? ["--allow-empty"] : []),
|
|
293
|
+
...(paths ? ["--only", "--", ...paths] : []),
|
|
294
|
+
]);
|
|
295
|
+
return this.resolveRef();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
override async workdirDiff(): Promise<DiffEntry[]> {
|
|
299
|
+
const status = await this.status();
|
|
300
|
+
const entries: DiffEntry[] = [];
|
|
301
|
+
let retainedBytes = 0;
|
|
302
|
+
for (const file of status.modifiedFiles) {
|
|
303
|
+
const beforeBytes = status.baseCommit
|
|
304
|
+
? await this.readBlobAtRef(status.baseCommit, file)
|
|
305
|
+
: null;
|
|
306
|
+
const afterBytes = await this.readFileBytes(file);
|
|
307
|
+
const decode = (bytes: Uint8Array | null): string | null => {
|
|
308
|
+
if (!bytes) return null;
|
|
309
|
+
if (bytes.length > 1024 * 1024 || bytes.includes(0))
|
|
310
|
+
return "Binary or large file. Open from its folder to inspect.";
|
|
311
|
+
return new TextDecoder().decode(bytes);
|
|
312
|
+
};
|
|
313
|
+
const entry: DiffEntry = {
|
|
314
|
+
path: file,
|
|
315
|
+
kind: !beforeBytes ? "added" : !afterBytes ? "deleted" : "modified",
|
|
316
|
+
before: decode(beforeBytes),
|
|
317
|
+
after: decode(afterBytes),
|
|
318
|
+
};
|
|
319
|
+
retainedBytes += Buffer.byteLength(JSON.stringify(entry));
|
|
320
|
+
if (retainedBytes > 64 * 1024 * 1024)
|
|
321
|
+
throw new Error("Working diff exceeds the 64 MiB snapshot limit");
|
|
322
|
+
entries.push(entry);
|
|
323
|
+
}
|
|
324
|
+
return entries;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
override async resetWorkingTree(): Promise<void> {
|
|
328
|
+
await nativeGit(this.repoPath, [
|
|
329
|
+
"restore",
|
|
330
|
+
"--source=HEAD",
|
|
331
|
+
"--staged",
|
|
332
|
+
"--worktree",
|
|
333
|
+
"--",
|
|
334
|
+
".",
|
|
335
|
+
`:!${PROJECT_PERSONAL_DIR}/`,
|
|
336
|
+
]);
|
|
337
|
+
await nativeGit(this.repoPath, [
|
|
338
|
+
"clean",
|
|
339
|
+
"-fd",
|
|
340
|
+
"-e",
|
|
341
|
+
`${PROJECT_APP_DATA_DIR}/`,
|
|
342
|
+
"-e",
|
|
343
|
+
`${PROJECT_PERSONAL_DIR}/`,
|
|
344
|
+
"--",
|
|
345
|
+
".",
|
|
346
|
+
]);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
override async checkout(ref?: string): Promise<void> {
|
|
350
|
+
await nativeGit(this.repoPath, ["checkout", ref ?? "main"]);
|
|
351
|
+
}
|
|
352
|
+
override async createBranch(name: string, fromRef?: string): Promise<void> {
|
|
353
|
+
await nativeGit(this.repoPath, [
|
|
354
|
+
"checkout",
|
|
355
|
+
"-b",
|
|
356
|
+
name,
|
|
357
|
+
...(fromRef ? [fromRef] : []),
|
|
358
|
+
]);
|
|
359
|
+
}
|
|
360
|
+
override async deleteBranch(name: string): Promise<void> {
|
|
361
|
+
await nativeGit(this.repoPath, ["branch", "-D", name]);
|
|
362
|
+
}
|
|
363
|
+
override async hasBranch(name: string): Promise<boolean> {
|
|
364
|
+
return this.resolveRef(`refs/heads/${name}`).then(
|
|
365
|
+
() => true,
|
|
366
|
+
() => false,
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
override async moveBranch(name: string, sha: string): Promise<void> {
|
|
370
|
+
await nativeGit(this.repoPath, ["update-ref", `refs/heads/${name}`, sha]);
|
|
371
|
+
}
|
|
372
|
+
override async listBranches(): Promise<BranchInfo[]> {
|
|
373
|
+
if (!(await hasLocalGit({ path: this.repoPath }))) return [];
|
|
374
|
+
const current = await this.currentBranch();
|
|
375
|
+
return (
|
|
376
|
+
await nativeGit(this.repoPath, [
|
|
377
|
+
"for-each-ref",
|
|
378
|
+
"--format=%(refname:short)%09%(objectname)%09%(committerdate:unix)",
|
|
379
|
+
"refs/heads/",
|
|
380
|
+
])
|
|
381
|
+
)
|
|
382
|
+
.trim()
|
|
383
|
+
.split("\n")
|
|
384
|
+
.filter(Boolean)
|
|
385
|
+
.map((line) => {
|
|
386
|
+
const [name = "", commit = "", timestamp] = line.split("\t");
|
|
387
|
+
return {
|
|
388
|
+
name,
|
|
389
|
+
commit,
|
|
390
|
+
isCurrent: current === name,
|
|
391
|
+
createdAt: Number(timestamp) || null,
|
|
392
|
+
};
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
override async log(options?: {
|
|
396
|
+
maxCount?: number;
|
|
397
|
+
ref?: string;
|
|
398
|
+
}): Promise<CommitInfo[]> {
|
|
399
|
+
const output = await nativeGit(this.repoPath, [
|
|
400
|
+
"log",
|
|
401
|
+
`--max-count=${options?.maxCount ?? 50}`,
|
|
402
|
+
"--format=%H%x00%an%x00%ae%x00%at%x00%B%x00",
|
|
403
|
+
options?.ref ?? "HEAD",
|
|
404
|
+
"--",
|
|
405
|
+
]).catch(() => "");
|
|
406
|
+
const parts = output.split("\0");
|
|
407
|
+
const commits: CommitInfo[] = [];
|
|
408
|
+
for (let i = 0; i + 4 < parts.length; i += 5) {
|
|
409
|
+
const [sha = "", name = "", email = "", timestamp = "0", message = ""] =
|
|
410
|
+
parts.slice(i, i + 5);
|
|
411
|
+
commits.push({
|
|
412
|
+
sha: sha.trim(),
|
|
413
|
+
author: { name, email },
|
|
414
|
+
timestamp: Number(timestamp),
|
|
415
|
+
message,
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
return commits;
|
|
419
|
+
}
|
|
420
|
+
override async listFilesAtRef(
|
|
421
|
+
ref: string,
|
|
422
|
+
opts?: { prefix?: string },
|
|
423
|
+
): Promise<string[]> {
|
|
424
|
+
return (await this.listBlobsAtRef(ref, opts)).map((entry) => entry.path);
|
|
425
|
+
}
|
|
426
|
+
override async listBlobsAtRef(
|
|
427
|
+
ref: string,
|
|
428
|
+
opts?: { prefix?: string },
|
|
429
|
+
): Promise<Array<{ path: string; oid: string }>> {
|
|
430
|
+
const sha = await this.resolveRef(ref);
|
|
431
|
+
const output = await nativeGit(this.repoPath, [
|
|
432
|
+
"ls-tree",
|
|
433
|
+
"-r",
|
|
434
|
+
"-z",
|
|
435
|
+
sha,
|
|
436
|
+
...(opts?.prefix ? ["--", opts.prefix] : []),
|
|
437
|
+
]);
|
|
438
|
+
return output
|
|
439
|
+
.split("\0")
|
|
440
|
+
.filter(Boolean)
|
|
441
|
+
.flatMap((entry) => {
|
|
442
|
+
const tab = entry.indexOf("\t");
|
|
443
|
+
const [, type, oid] = entry.slice(0, tab).split(" ");
|
|
444
|
+
return type === "blob" && oid && !isPersonalFile(entry.slice(tab + 1))
|
|
445
|
+
? [{ path: entry.slice(tab + 1), oid }]
|
|
446
|
+
: [];
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
override async readBlobAtRef(
|
|
450
|
+
ref: string,
|
|
451
|
+
filePath: string,
|
|
452
|
+
options?: { maxBytes?: number },
|
|
453
|
+
): Promise<Uint8Array | null> {
|
|
454
|
+
assertSafePath(filePath);
|
|
455
|
+
const sha = await this.resolveRef(ref);
|
|
456
|
+
const size = await nativeGit(this.repoPath, [
|
|
457
|
+
"cat-file",
|
|
458
|
+
"-s",
|
|
459
|
+
`${sha}:${filePath}`,
|
|
460
|
+
]).then(
|
|
461
|
+
(value) => Number(value.trim()),
|
|
462
|
+
() => null,
|
|
463
|
+
);
|
|
464
|
+
if (size === null) return null;
|
|
465
|
+
if (options?.maxBytes !== undefined && size > options.maxBytes)
|
|
466
|
+
throw new Error(
|
|
467
|
+
`Project file '${filePath}' exceeds the ${options.maxBytes}-byte snapshot limit`,
|
|
468
|
+
);
|
|
469
|
+
if (size > 64 * 1024 * 1024)
|
|
470
|
+
throw new Error(
|
|
471
|
+
"This file exceeds the 64 MB document limit. Open it from its folder.",
|
|
472
|
+
);
|
|
473
|
+
return nativeGitBytes(this.repoPath, ["show", `${sha}:${filePath}`]);
|
|
474
|
+
}
|
|
475
|
+
override async readAllFilesAtRef(
|
|
476
|
+
ref: string,
|
|
477
|
+
options?: FileReadOptions,
|
|
478
|
+
): Promise<Record<string, string>> {
|
|
479
|
+
return this.readTextSnapshotAtRef(ref, { prefix: "" }, options);
|
|
480
|
+
}
|
|
481
|
+
override async readFilesAtRef(
|
|
482
|
+
ref: string,
|
|
483
|
+
opts: { prefix: string },
|
|
484
|
+
): Promise<Record<string, string>> {
|
|
485
|
+
return this.readTextSnapshotAtRef(ref, opts);
|
|
486
|
+
}
|
|
487
|
+
private async readTextSnapshotAtRef(
|
|
488
|
+
ref: string,
|
|
489
|
+
opts: { prefix: string },
|
|
490
|
+
options?: FileReadOptions,
|
|
491
|
+
): Promise<Record<string, string>> {
|
|
492
|
+
const sha = await this.resolveRef(ref);
|
|
493
|
+
const blobs = await this.listBlobsAtRef(sha, opts);
|
|
494
|
+
const byPath = new Map(blobs.map((entry) => [entry.path, entry.oid]));
|
|
495
|
+
return readFileSnapshot({
|
|
496
|
+
paths: blobs.map((entry) => entry.path),
|
|
497
|
+
options: { maxFileBytes: 2 * 1024 * 1024, ...options },
|
|
498
|
+
read: async (file, maxBytes) => {
|
|
499
|
+
const oid = byPath.get(file);
|
|
500
|
+
if (!oid) return null;
|
|
501
|
+
const size = Number(
|
|
502
|
+
(await nativeGit(this.repoPath, ["cat-file", "-s", oid])).trim(),
|
|
503
|
+
);
|
|
504
|
+
if (size > maxBytes) return null;
|
|
505
|
+
const bytes = await this.readBlobAtRef(sha, file, { maxBytes });
|
|
506
|
+
if (!bytes || bytes.includes(0)) return null;
|
|
507
|
+
try {
|
|
508
|
+
return new TextDecoder("utf8", { fatal: true }).decode(bytes);
|
|
509
|
+
} catch {
|
|
510
|
+
return null;
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
}
|