@catamorphic/git 0.1.0-alpha.42 → 0.1.0-alpha.44
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/dist/checkout-remote-backend.d.ts.map +1 -1
- package/dist/fs-backend.d.ts +8 -0
- package/dist/fs-backend.d.ts.map +1 -1
- package/dist/fs-remote-backend.d.ts +43 -9
- package/dist/fs-remote-backend.d.ts.map +1 -1
- package/dist/git-objects.d.ts +77 -0
- package/dist/git-objects.d.ts.map +1 -0
- package/dist/git-sync.d.ts +16 -17
- package/dist/git-sync.d.ts.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3912 -3059
- package/dist/index.js.map +23 -21
- package/dist/native-git.d.ts +114 -2
- package/dist/native-git.d.ts.map +1 -1
- package/dist/native-project-repo.d.ts +40 -5
- package/dist/native-project-repo.d.ts.map +1 -1
- package/dist/network-sync.d.ts.map +1 -1
- package/dist/network.d.ts +14 -9
- package/dist/network.d.ts.map +1 -1
- package/dist/object-origin-cache.d.ts +86 -0
- package/dist/object-origin-cache.d.ts.map +1 -0
- package/dist/object-remote-backend.d.ts +64 -10
- package/dist/object-remote-backend.d.ts.map +1 -1
- package/dist/origin-draft.d.ts.map +1 -1
- package/dist/origin-objects.d.ts +7 -2
- package/dist/origin-objects.d.ts.map +1 -1
- package/dist/project-manager.d.ts +22 -0
- package/dist/project-manager.d.ts.map +1 -1
- package/dist/project-paths.d.ts +20 -0
- package/dist/project-paths.d.ts.map +1 -0
- package/dist/ref-names.d.ts +35 -0
- package/dist/ref-names.d.ts.map +1 -1
- package/dist/types.d.ts +37 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/workspace-checkout.d.ts +31 -0
- package/dist/workspace-checkout.d.ts.map +1 -0
- package/dist/workspace-mirror.d.ts +38 -5
- package/dist/workspace-mirror.d.ts.map +1 -1
- package/dist/workspace-save.d.ts +27 -2
- package/dist/workspace-save.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/checkout-remote-backend.ts +77 -29
- package/src/fs-backend.ts +69 -7
- package/src/fs-remote-backend.ts +255 -133
- package/src/git-objects.ts +363 -0
- package/src/git-sync.ts +124 -453
- package/src/index.ts +17 -7
- package/src/native-git.ts +566 -25
- package/src/native-project-repo.ts +318 -99
- package/src/network-sync.ts +43 -35
- package/src/network.ts +177 -153
- package/src/object-origin-cache.ts +689 -0
- package/src/object-remote-backend.ts +277 -20
- package/src/origin-draft.ts +22 -15
- package/src/origin-objects.ts +21 -5
- package/src/project-manager.ts +261 -73
- package/src/project-paths.ts +142 -0
- package/src/ref-names.ts +56 -0
- package/src/types.ts +38 -4
- package/src/workspace-checkout.ts +192 -0
- package/src/workspace-mirror.ts +276 -68
- package/src/workspace-save.ts +99 -30
- package/dist/project-repo.d.ts +0 -81
- package/dist/project-repo.d.ts.map +0 -1
- package/src/project-repo.ts +0 -821
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import nodeFs from "node:fs";
|
|
1
2
|
import fs from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import {
|
|
@@ -7,18 +8,32 @@ import {
|
|
|
7
8
|
} from "@catamorphic/workflow/project-layout";
|
|
8
9
|
import ignore, { type Ignore } from "ignore";
|
|
9
10
|
import { type FileReadOptions, readFileSnapshot } from "./file-reads.js";
|
|
10
|
-
import {
|
|
11
|
+
import { objectContent, objectHeader } from "./git-objects.js";
|
|
12
|
+
import { hasLocalGit, nativeGit, nativeGitRecords } from "./native-git.js";
|
|
11
13
|
import {
|
|
12
14
|
ensurePersonalFilesExcluded,
|
|
13
15
|
isPersonalFile,
|
|
14
16
|
} from "./personal-files.js";
|
|
15
17
|
import {
|
|
18
|
+
assertNoSymlinks,
|
|
16
19
|
assertSafePath,
|
|
17
|
-
ProjectRepoImpl,
|
|
18
20
|
WALK_SKIPPED_DIRS,
|
|
19
21
|
walkDirectory,
|
|
20
|
-
} from "./project-
|
|
21
|
-
import
|
|
22
|
+
} from "./project-paths.js";
|
|
23
|
+
import {
|
|
24
|
+
assertObjectId,
|
|
25
|
+
assertResolvableRef,
|
|
26
|
+
assertValidRefName,
|
|
27
|
+
isObjectId,
|
|
28
|
+
revisionThenPaths,
|
|
29
|
+
} from "./ref-names.js";
|
|
30
|
+
import type {
|
|
31
|
+
BranchInfo,
|
|
32
|
+
CommitInfo,
|
|
33
|
+
DiffEntry,
|
|
34
|
+
ProjectRepo,
|
|
35
|
+
RepoStatus,
|
|
36
|
+
} from "./types.js";
|
|
22
37
|
|
|
23
38
|
/**
|
|
24
39
|
* Walks of folders without Git, by root and folder: the one running and the
|
|
@@ -147,12 +162,127 @@ function literalFolder(globs: readonly string[]): string {
|
|
|
147
162
|
});
|
|
148
163
|
}
|
|
149
164
|
|
|
150
|
-
/**
|
|
151
|
-
export
|
|
152
|
-
|
|
165
|
+
/** Commits a repository's log lists, newest first. */
|
|
166
|
+
export async function nativeLog(input: {
|
|
167
|
+
repoPath: string;
|
|
168
|
+
ref: string;
|
|
169
|
+
maxCount: number;
|
|
170
|
+
}): Promise<CommitInfo[]> {
|
|
171
|
+
const output = await nativeGit(input.repoPath, [
|
|
172
|
+
"log",
|
|
173
|
+
`--max-count=${input.maxCount}`,
|
|
174
|
+
"--format=%H%x00%an%x00%ae%x00%at%x00%B%x00",
|
|
175
|
+
"--end-of-options",
|
|
176
|
+
input.ref,
|
|
177
|
+
"--",
|
|
178
|
+
]).catch(() => "");
|
|
179
|
+
const parts = output.split("\0");
|
|
180
|
+
const commits: CommitInfo[] = [];
|
|
181
|
+
for (let i = 0; i + 4 < parts.length; i += 5) {
|
|
182
|
+
const [sha = "", name = "", email = "", timestamp = "0", message = ""] =
|
|
183
|
+
parts.slice(i, i + 5);
|
|
184
|
+
commits.push({
|
|
185
|
+
sha: sha.trim(),
|
|
186
|
+
author: { name, email },
|
|
187
|
+
timestamp: Number(timestamp),
|
|
188
|
+
message,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
return commits;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A repository on this machine's disk, read and written with the Git
|
|
196
|
+
* program: a person's own folder, or a copy Work manages (a session's, a
|
|
197
|
+
* member's on a host without an origin, the project's own). Git honors the
|
|
198
|
+
* repository's format, ignore rules and tools, and its memory does not grow
|
|
199
|
+
* with the repository.
|
|
200
|
+
*
|
|
201
|
+
* A managed copy never follows a symbolic link, resolves only names that
|
|
202
|
+
* are plainly a ref or a commit, and always records a commit when asked; a
|
|
203
|
+
* person's own folder may hold links of theirs and may have no Git yet.
|
|
204
|
+
*/
|
|
205
|
+
export class NativeProjectRepo implements ProjectRepo {
|
|
206
|
+
/** A copy Work manages, as opposed to a person's own folder. */
|
|
207
|
+
readonly managed: boolean;
|
|
208
|
+
|
|
209
|
+
constructor(
|
|
210
|
+
readonly projectId: string,
|
|
211
|
+
readonly repoPath: string,
|
|
212
|
+
private readonly releaseFn: () => Promise<void>,
|
|
213
|
+
options?: { managed?: boolean },
|
|
214
|
+
) {
|
|
215
|
+
this.managed = options?.managed ?? false;
|
|
216
|
+
}
|
|
153
217
|
|
|
154
|
-
|
|
218
|
+
private get followsSymlinks(): boolean {
|
|
219
|
+
return !this.managed;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async readFile(filePath: string): Promise<string> {
|
|
223
|
+
assertSafePath(filePath);
|
|
224
|
+
if (!this.followsSymlinks) await assertNoSymlinks(this.repoPath, filePath);
|
|
225
|
+
return fs.readFile(path.join(this.repoPath, filePath), "utf-8");
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async writeFile(filePath: string, content: string): Promise<void> {
|
|
155
229
|
assertSafePath(filePath);
|
|
230
|
+
if (!this.followsSymlinks) await assertNoSymlinks(this.repoPath, filePath);
|
|
231
|
+
const fullPath = path.join(this.repoPath, filePath);
|
|
232
|
+
await fs.mkdir(path.dirname(fullPath), { recursive: true });
|
|
233
|
+
await fs.writeFile(fullPath, content);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async deleteFile(filePath: string): Promise<void> {
|
|
237
|
+
assertSafePath(filePath);
|
|
238
|
+
if (!this.followsSymlinks) await assertNoSymlinks(this.repoPath, filePath);
|
|
239
|
+
await fs.unlink(path.join(this.repoPath, filePath));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private async readSnapshotBytes(
|
|
243
|
+
file: string,
|
|
244
|
+
maxBytes: number,
|
|
245
|
+
): Promise<Uint8Array> {
|
|
246
|
+
assertSafePath(file);
|
|
247
|
+
const handle = await fs.open(
|
|
248
|
+
path.join(this.repoPath, file),
|
|
249
|
+
this.followsSymlinks
|
|
250
|
+
? nodeFs.constants.O_RDONLY
|
|
251
|
+
: nodeFs.constants.O_RDONLY | (nodeFs.constants.O_NOFOLLOW ?? 0),
|
|
252
|
+
);
|
|
253
|
+
try {
|
|
254
|
+
const size = (await handle.stat()).size;
|
|
255
|
+
if (size > maxBytes)
|
|
256
|
+
throw new Error(
|
|
257
|
+
`Project file '${file}' exceeds the ${maxBytes}-byte snapshot limit`,
|
|
258
|
+
);
|
|
259
|
+
// A concurrent writer cannot make this read grow beyond its budget.
|
|
260
|
+
const buffer = Buffer.alloc(size + 1);
|
|
261
|
+
let bytesRead = 0;
|
|
262
|
+
while (bytesRead < buffer.length) {
|
|
263
|
+
const chunk = await handle.read(
|
|
264
|
+
buffer,
|
|
265
|
+
bytesRead,
|
|
266
|
+
buffer.length - bytesRead,
|
|
267
|
+
bytesRead,
|
|
268
|
+
);
|
|
269
|
+
if (chunk.bytesRead === 0) break;
|
|
270
|
+
bytesRead += chunk.bytesRead;
|
|
271
|
+
}
|
|
272
|
+
if (bytesRead > size)
|
|
273
|
+
throw new Error(
|
|
274
|
+
`Project file '${file}' changed during snapshot; retry the read`,
|
|
275
|
+
);
|
|
276
|
+
return buffer.subarray(0, bytesRead);
|
|
277
|
+
} finally {
|
|
278
|
+
await handle.close();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async readFileBytes(filePath: string): Promise<Uint8Array | null> {
|
|
283
|
+
assertSafePath(filePath);
|
|
284
|
+
// A managed copy never reads a link, nor through one.
|
|
285
|
+
if (!this.followsSymlinks) await assertNoSymlinks(this.repoPath, filePath);
|
|
156
286
|
const target = path.join(this.repoPath, filePath);
|
|
157
287
|
const stat = await fs.lstat(target).catch((error: unknown) => {
|
|
158
288
|
if (
|
|
@@ -174,7 +304,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
174
304
|
return this.readSnapshotBytes(filePath, 64 * 1024 * 1024);
|
|
175
305
|
}
|
|
176
306
|
|
|
177
|
-
|
|
307
|
+
async readAllFiles(
|
|
178
308
|
options: FileReadOptions = {},
|
|
179
309
|
): Promise<Record<string, string>> {
|
|
180
310
|
return this.readSnapshot(await this.listFiles(), options);
|
|
@@ -201,7 +331,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
201
331
|
});
|
|
202
332
|
}
|
|
203
333
|
|
|
204
|
-
|
|
334
|
+
async listFiles(opts?: { prefix?: string }): Promise<string[]> {
|
|
205
335
|
if (!(await hasLocalGit({ path: this.repoPath }))) {
|
|
206
336
|
if (opts?.prefix) assertSafePath(opts.prefix);
|
|
207
337
|
const prefix = opts?.prefix ?? "";
|
|
@@ -209,8 +339,9 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
209
339
|
return files.filter((file) => file.startsWith(prefix)).sort();
|
|
210
340
|
}
|
|
211
341
|
const pathspec = opts?.prefix ? ["--", opts.prefix] : [];
|
|
212
|
-
|
|
213
|
-
|
|
342
|
+
// Streamed: a large tree's listing has no cap on its size.
|
|
343
|
+
const [listed, removed] = await Promise.all([
|
|
344
|
+
nativeGitRecords(this.repoPath, [
|
|
214
345
|
"ls-files",
|
|
215
346
|
"--stage",
|
|
216
347
|
"--others",
|
|
@@ -218,11 +349,16 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
218
349
|
"-z",
|
|
219
350
|
...pathspec,
|
|
220
351
|
]),
|
|
221
|
-
|
|
352
|
+
nativeGitRecords(this.repoPath, [
|
|
353
|
+
"ls-files",
|
|
354
|
+
"--deleted",
|
|
355
|
+
"-z",
|
|
356
|
+
...pathspec,
|
|
357
|
+
]),
|
|
222
358
|
]);
|
|
223
|
-
const deleted = new Set(removed
|
|
359
|
+
const deleted = new Set(removed);
|
|
224
360
|
const files = new Set<string>();
|
|
225
|
-
for (const record of
|
|
361
|
+
for (const record of listed.filter(Boolean)) {
|
|
226
362
|
const tracked = /^(\d{6}) [a-f0-9]+ [0-3]\t(.*)$/s.exec(record);
|
|
227
363
|
if (tracked?.[1] === "160000") continue;
|
|
228
364
|
const file = tracked?.[2] ?? record;
|
|
@@ -258,7 +394,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
258
394
|
}
|
|
259
395
|
const ref = input.ref ? await this.resolveRef(input.ref) : undefined;
|
|
260
396
|
try {
|
|
261
|
-
const
|
|
397
|
+
const found = await nativeGitRecords(this.repoPath, [
|
|
262
398
|
"grep",
|
|
263
399
|
"-l",
|
|
264
400
|
"-I",
|
|
@@ -271,8 +407,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
271
407
|
"--",
|
|
272
408
|
...input.globs,
|
|
273
409
|
]);
|
|
274
|
-
return
|
|
275
|
-
.split("\0")
|
|
410
|
+
return found
|
|
276
411
|
.filter(Boolean)
|
|
277
412
|
.map((file) => (ref ? file.slice(ref.length + 1) : file))
|
|
278
413
|
.filter((file) => !isPersonalFile(file));
|
|
@@ -283,7 +418,10 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
283
418
|
}
|
|
284
419
|
}
|
|
285
420
|
|
|
286
|
-
|
|
421
|
+
async resolveRef(ref = "HEAD"): Promise<string> {
|
|
422
|
+
// A managed copy is asked by callers Work does not write: only a name
|
|
423
|
+
// that is plainly a ref or a commit, never a revision expression.
|
|
424
|
+
if (this.managed) assertResolvableRef(ref);
|
|
287
425
|
return (
|
|
288
426
|
await nativeGit(this.repoPath, [
|
|
289
427
|
"rev-parse",
|
|
@@ -294,7 +432,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
294
432
|
).trim();
|
|
295
433
|
}
|
|
296
434
|
|
|
297
|
-
|
|
435
|
+
async currentBranch(): Promise<string> {
|
|
298
436
|
if (!(await hasLocalGit({ path: this.repoPath }))) return "main";
|
|
299
437
|
return (
|
|
300
438
|
(await nativeGit(this.repoPath, ["branch", "--show-current"])).trim() ||
|
|
@@ -302,7 +440,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
302
440
|
);
|
|
303
441
|
}
|
|
304
442
|
|
|
305
|
-
|
|
443
|
+
async status(): Promise<RepoStatus> {
|
|
306
444
|
if (!(await hasLocalGit({ path: this.repoPath }))) {
|
|
307
445
|
const modifiedFiles = await this.listFiles();
|
|
308
446
|
return {
|
|
@@ -315,13 +453,12 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
315
453
|
behind: 0,
|
|
316
454
|
};
|
|
317
455
|
}
|
|
318
|
-
const
|
|
456
|
+
const records = await nativeGitRecords(this.repoPath, [
|
|
319
457
|
"status",
|
|
320
458
|
"--porcelain=v1",
|
|
321
459
|
"-z",
|
|
322
460
|
"--untracked-files=all",
|
|
323
461
|
]);
|
|
324
|
-
const records = output.split("\0");
|
|
325
462
|
const modifiedFiles: string[] = [];
|
|
326
463
|
for (let i = 0; i < records.length; i++) {
|
|
327
464
|
const record = records[i];
|
|
@@ -360,7 +497,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
360
497
|
};
|
|
361
498
|
}
|
|
362
499
|
|
|
363
|
-
|
|
500
|
+
async commit(
|
|
364
501
|
message: string,
|
|
365
502
|
author: { name: string; email: string },
|
|
366
503
|
opts?: { paths?: readonly string[]; allowEmpty?: boolean },
|
|
@@ -370,9 +507,10 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
370
507
|
if (paths?.length === 0) return this.resolveRef();
|
|
371
508
|
if (!(await hasLocalGit({ path: this.repoPath })))
|
|
372
509
|
await nativeGit(this.repoPath, ["init", "--initial-branch=main"]);
|
|
373
|
-
const allIndexed = (
|
|
374
|
-
|
|
375
|
-
|
|
510
|
+
const allIndexed = await nativeGitRecords(this.repoPath, [
|
|
511
|
+
"ls-files",
|
|
512
|
+
"-z",
|
|
513
|
+
]);
|
|
376
514
|
if (allIndexed.some(isPersonalFile))
|
|
377
515
|
throw new Error(
|
|
378
516
|
"Personal files are tracked. Remove them from the git index before checkpointing or sharing this project.",
|
|
@@ -421,13 +559,13 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
421
559
|
"commit",
|
|
422
560
|
"-m",
|
|
423
561
|
message,
|
|
424
|
-
...(opts?.allowEmpty ? ["--allow-empty"] : []),
|
|
562
|
+
...(opts?.allowEmpty || this.managed ? ["--allow-empty"] : []),
|
|
425
563
|
...(paths ? ["--only", "--", ...paths] : []),
|
|
426
564
|
]);
|
|
427
565
|
return this.resolveRef();
|
|
428
566
|
}
|
|
429
567
|
|
|
430
|
-
|
|
568
|
+
async workdirDiff(): Promise<DiffEntry[]> {
|
|
431
569
|
const status = await this.status();
|
|
432
570
|
const entries: DiffEntry[] = [];
|
|
433
571
|
let retainedBytes = 0;
|
|
@@ -456,7 +594,7 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
456
594
|
return entries;
|
|
457
595
|
}
|
|
458
596
|
|
|
459
|
-
|
|
597
|
+
async resetWorkingTree(): Promise<void> {
|
|
460
598
|
await nativeGit(this.repoPath, [
|
|
461
599
|
"restore",
|
|
462
600
|
"--source=HEAD",
|
|
@@ -478,30 +616,43 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
478
616
|
]);
|
|
479
617
|
}
|
|
480
618
|
|
|
481
|
-
|
|
482
|
-
await nativeGit(this.repoPath, [
|
|
619
|
+
async checkout(ref?: string): Promise<void> {
|
|
620
|
+
await nativeGit(this.repoPath, [
|
|
621
|
+
"checkout",
|
|
622
|
+
...(this.managed ? ["--force"] : []),
|
|
623
|
+
...revisionThenPaths(ref ?? "main"),
|
|
624
|
+
]);
|
|
483
625
|
}
|
|
484
|
-
|
|
626
|
+
async createBranch(name: string, fromRef?: string): Promise<void> {
|
|
627
|
+
assertValidRefName(name);
|
|
485
628
|
await nativeGit(this.repoPath, [
|
|
486
629
|
"checkout",
|
|
487
630
|
"-b",
|
|
488
631
|
name,
|
|
489
|
-
...(fromRef ?
|
|
632
|
+
...(fromRef ? revisionThenPaths(fromRef) : []),
|
|
490
633
|
]);
|
|
491
634
|
}
|
|
492
|
-
|
|
493
|
-
|
|
635
|
+
async deleteBranch(name: string): Promise<void> {
|
|
636
|
+
assertValidRefName(name);
|
|
637
|
+
await nativeGit(this.repoPath, ["branch", "-D", "--end-of-options", name]);
|
|
494
638
|
}
|
|
495
|
-
|
|
639
|
+
async hasBranch(name: string): Promise<boolean> {
|
|
496
640
|
return this.resolveRef(`refs/heads/${name}`).then(
|
|
497
641
|
() => true,
|
|
498
642
|
() => false,
|
|
499
643
|
);
|
|
500
644
|
}
|
|
501
|
-
|
|
502
|
-
|
|
645
|
+
async moveBranch(name: string, sha: string): Promise<void> {
|
|
646
|
+
assertValidRefName(name);
|
|
647
|
+
assertObjectId(sha);
|
|
648
|
+
await nativeGit(this.repoPath, [
|
|
649
|
+
"update-ref",
|
|
650
|
+
"--end-of-options",
|
|
651
|
+
`refs/heads/${name}`,
|
|
652
|
+
sha,
|
|
653
|
+
]);
|
|
503
654
|
}
|
|
504
|
-
|
|
655
|
+
async listBranches(): Promise<BranchInfo[]> {
|
|
505
656
|
if (!(await hasLocalGit({ path: this.repoPath }))) return [];
|
|
506
657
|
const current = await this.currentBranch();
|
|
507
658
|
return (
|
|
@@ -524,93 +675,107 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
524
675
|
};
|
|
525
676
|
});
|
|
526
677
|
}
|
|
527
|
-
|
|
678
|
+
async log(options?: {
|
|
528
679
|
maxCount?: number;
|
|
529
680
|
ref?: string;
|
|
530
681
|
}): Promise<CommitInfo[]> {
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
"
|
|
535
|
-
options?.
|
|
536
|
-
|
|
537
|
-
]).catch(() => "");
|
|
538
|
-
const parts = output.split("\0");
|
|
539
|
-
const commits: CommitInfo[] = [];
|
|
540
|
-
for (let i = 0; i + 4 < parts.length; i += 5) {
|
|
541
|
-
const [sha = "", name = "", email = "", timestamp = "0", message = ""] =
|
|
542
|
-
parts.slice(i, i + 5);
|
|
543
|
-
commits.push({
|
|
544
|
-
sha: sha.trim(),
|
|
545
|
-
author: { name, email },
|
|
546
|
-
timestamp: Number(timestamp),
|
|
547
|
-
message,
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
return commits;
|
|
682
|
+
if (options?.ref && this.managed) assertResolvableRef(options.ref);
|
|
683
|
+
return nativeLog({
|
|
684
|
+
repoPath: this.repoPath,
|
|
685
|
+
ref: options?.ref ?? "HEAD",
|
|
686
|
+
maxCount: options?.maxCount ?? 50,
|
|
687
|
+
});
|
|
551
688
|
}
|
|
552
|
-
|
|
689
|
+
async listFilesAtRef(
|
|
553
690
|
ref: string,
|
|
554
691
|
opts?: { prefix?: string },
|
|
555
692
|
): Promise<string[]> {
|
|
556
693
|
return (await this.listBlobsAtRef(ref, opts)).map((entry) => entry.path);
|
|
557
694
|
}
|
|
558
|
-
|
|
695
|
+
async listBlobsAtRef(
|
|
559
696
|
ref: string,
|
|
560
697
|
opts?: { prefix?: string },
|
|
561
|
-
): Promise<Array<{ path: string; oid: string }>> {
|
|
698
|
+
): Promise<Array<{ path: string; oid: string; size: number }>> {
|
|
562
699
|
const sha = await this.resolveRef(ref);
|
|
563
|
-
|
|
700
|
+
// One listing names every file, its object and its size: nothing
|
|
701
|
+
// below starts a Git of its own for a file.
|
|
702
|
+
const listed = await nativeGitRecords(this.repoPath, [
|
|
564
703
|
"ls-tree",
|
|
565
704
|
"-r",
|
|
566
705
|
"-z",
|
|
706
|
+
"-l",
|
|
567
707
|
sha,
|
|
568
708
|
...(opts?.prefix ? ["--", opts.prefix] : []),
|
|
569
709
|
]);
|
|
570
|
-
return
|
|
571
|
-
.
|
|
572
|
-
.
|
|
573
|
-
.
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
710
|
+
return listed.filter(Boolean).flatMap((entry) => {
|
|
711
|
+
// `<mode> <type> <id> <size, padded>`, a tab, then the path as it is.
|
|
712
|
+
const tab = entry.indexOf("\t");
|
|
713
|
+
const [, type, oid, size] = entry.slice(0, tab).split(/ +/);
|
|
714
|
+
return type === "blob" &&
|
|
715
|
+
oid &&
|
|
716
|
+
/^\d+$/.test(size ?? "") &&
|
|
717
|
+
!isPersonalFile(entry.slice(tab + 1))
|
|
718
|
+
? [{ path: entry.slice(tab + 1), oid, size: Number(size) }]
|
|
719
|
+
: [];
|
|
720
|
+
});
|
|
580
721
|
}
|
|
581
|
-
|
|
722
|
+
async readBlobAtRef(
|
|
582
723
|
ref: string,
|
|
583
724
|
filePath: string,
|
|
584
725
|
options?: { maxBytes?: number },
|
|
585
726
|
): Promise<Uint8Array | null> {
|
|
586
727
|
assertSafePath(filePath);
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
728
|
+
// A full id names its commit already; anything else is resolved once.
|
|
729
|
+
// No file's name holds a NUL: Git could not be asked for one.
|
|
730
|
+
if (filePath.includes("\0")) return null;
|
|
731
|
+
const sha = isObjectId(ref) ? ref : await this.resolveRef(ref);
|
|
732
|
+
// Asked of the repository's long-lived reader: a caller that reads
|
|
733
|
+
// many files starts no Git for each. The reader takes one name a
|
|
734
|
+
// line, so a path with a line break in it is turned into its id first.
|
|
735
|
+
const name = filePath.includes("\n")
|
|
736
|
+
? await nativeGit(this.repoPath, [
|
|
737
|
+
"rev-parse",
|
|
738
|
+
"--verify",
|
|
739
|
+
"--quiet",
|
|
740
|
+
"--end-of-options",
|
|
741
|
+
`${sha}:${filePath}`,
|
|
742
|
+
]).then(
|
|
743
|
+
(id) => id.trim(),
|
|
744
|
+
() => null,
|
|
745
|
+
)
|
|
746
|
+
: `${sha}:${filePath}`;
|
|
747
|
+
if (!name) return null;
|
|
748
|
+
const found = await objectHeader({ gitdir: this.repoPath, name });
|
|
749
|
+
// A folder or a nested repository at that path is not a file.
|
|
750
|
+
if (found?.type !== "blob") return null;
|
|
751
|
+
if (options?.maxBytes !== undefined && found.size > options.maxBytes)
|
|
598
752
|
throw new Error(
|
|
599
753
|
`Project file '${filePath}' exceeds the ${options.maxBytes}-byte snapshot limit`,
|
|
600
754
|
);
|
|
601
|
-
if (size > 64 * 1024 * 1024)
|
|
755
|
+
if (found.size > 64 * 1024 * 1024)
|
|
602
756
|
throw new Error(
|
|
603
757
|
"This file exceeds the 64 MB document limit. Open it from its folder.",
|
|
604
758
|
);
|
|
605
|
-
return
|
|
759
|
+
return this.blob(found.oid, found.size);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/** A blob's bytes by id, from the repository's long-lived reader. */
|
|
763
|
+
private async blob(oid: string, size: number): Promise<Uint8Array | null> {
|
|
764
|
+
const object = await objectContent({
|
|
765
|
+
gitdir: this.repoPath,
|
|
766
|
+
name: oid,
|
|
767
|
+
size,
|
|
768
|
+
maxBytes: Math.max(size, 1),
|
|
769
|
+
});
|
|
770
|
+
return object?.type === "blob" ? object.data : null;
|
|
606
771
|
}
|
|
607
|
-
|
|
772
|
+
async readAllFilesAtRef(
|
|
608
773
|
ref: string,
|
|
609
774
|
options?: FileReadOptions,
|
|
610
775
|
): Promise<Record<string, string>> {
|
|
611
776
|
return this.readTextSnapshotAtRef(ref, { prefix: "" }, options);
|
|
612
777
|
}
|
|
613
|
-
|
|
778
|
+
async readFilesAtRef(
|
|
614
779
|
ref: string,
|
|
615
780
|
opts: { prefix: string },
|
|
616
781
|
): Promise<Record<string, string>> {
|
|
@@ -623,18 +788,15 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
623
788
|
): Promise<Record<string, string>> {
|
|
624
789
|
const sha = await this.resolveRef(ref);
|
|
625
790
|
const blobs = await this.listBlobsAtRef(sha, opts);
|
|
626
|
-
const byPath = new Map(blobs.map((entry) => [entry.path, entry
|
|
791
|
+
const byPath = new Map(blobs.map((entry) => [entry.path, entry]));
|
|
627
792
|
return readFileSnapshot({
|
|
628
793
|
paths: blobs.map((entry) => entry.path),
|
|
629
794
|
options: { maxFileBytes: 2 * 1024 * 1024, ...options },
|
|
630
795
|
read: async (file, maxBytes) => {
|
|
631
|
-
const
|
|
632
|
-
if (!
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
);
|
|
636
|
-
if (size > maxBytes) return null;
|
|
637
|
-
const bytes = await this.readBlobAtRef(sha, file, { maxBytes });
|
|
796
|
+
const entry = byPath.get(file);
|
|
797
|
+
if (!entry || entry.size > maxBytes) return null;
|
|
798
|
+
// By the id the listing gave, whatever the file is called.
|
|
799
|
+
const bytes = await this.blob(entry.oid, entry.size);
|
|
638
800
|
if (!bytes || bytes.includes(0)) return null;
|
|
639
801
|
try {
|
|
640
802
|
return new TextDecoder("utf8", { fatal: true }).decode(bytes);
|
|
@@ -644,4 +806,61 @@ export class NativeProjectRepo extends ProjectRepoImpl {
|
|
|
644
806
|
},
|
|
645
807
|
});
|
|
646
808
|
}
|
|
809
|
+
|
|
810
|
+
async diff(opts: { base: string; head: string }): Promise<DiffEntry[]> {
|
|
811
|
+
const baseFiles = new Map(
|
|
812
|
+
(await this.listBlobsAtRef(opts.base)).map((entry) => [
|
|
813
|
+
entry.path,
|
|
814
|
+
entry.oid,
|
|
815
|
+
]),
|
|
816
|
+
);
|
|
817
|
+
const headFiles = new Map(
|
|
818
|
+
(await this.listBlobsAtRef(opts.head)).map((entry) => [
|
|
819
|
+
entry.path,
|
|
820
|
+
entry.oid,
|
|
821
|
+
]),
|
|
822
|
+
);
|
|
823
|
+
const changed = [
|
|
824
|
+
...new Set([...baseFiles.keys(), ...headFiles.keys()]),
|
|
825
|
+
].filter((file) => baseFiles.get(file) !== headFiles.get(file));
|
|
826
|
+
const snapshots = await readFileSnapshot({
|
|
827
|
+
paths: changed.flatMap((file) => [`before/${file}`, `after/${file}`]),
|
|
828
|
+
read: async (key, maxBytes) => {
|
|
829
|
+
const before = key.startsWith("before/");
|
|
830
|
+
const file = key.slice(before ? 7 : 6);
|
|
831
|
+
if (!(before ? baseFiles : headFiles).has(file)) return "";
|
|
832
|
+
const blob = await this.readBlobAtRef(
|
|
833
|
+
before ? opts.base : opts.head,
|
|
834
|
+
file,
|
|
835
|
+
{ maxBytes },
|
|
836
|
+
);
|
|
837
|
+
return blob ? new TextDecoder().decode(blob) : "";
|
|
838
|
+
},
|
|
839
|
+
});
|
|
840
|
+
const entries: DiffEntry[] = [];
|
|
841
|
+
for (const filepath of changed) {
|
|
842
|
+
const before = baseFiles.has(filepath)
|
|
843
|
+
? (snapshots[`before/${filepath}`] ?? "")
|
|
844
|
+
: null;
|
|
845
|
+
const after = headFiles.has(filepath)
|
|
846
|
+
? (snapshots[`after/${filepath}`] ?? "")
|
|
847
|
+
: null;
|
|
848
|
+
if (before === after) continue;
|
|
849
|
+
if (before == null && after != null) {
|
|
850
|
+
entries.push({ path: filepath, kind: "added", before: null, after });
|
|
851
|
+
} else if (before != null && after == null) {
|
|
852
|
+
entries.push({ path: filepath, kind: "deleted", before, after: null });
|
|
853
|
+
} else {
|
|
854
|
+
entries.push({ path: filepath, kind: "modified", before, after });
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return entries;
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
async dispose(): Promise<void> {
|
|
861
|
+
// The folder's reader is not this holder's to close: every holder of
|
|
862
|
+
// the folder reads through it, and another may be waiting on it now.
|
|
863
|
+
// Whoever removes the folder closes it.
|
|
864
|
+
await this.releaseFn();
|
|
865
|
+
}
|
|
647
866
|
}
|