@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
package/src/workspace-mirror.ts
CHANGED
|
@@ -2,7 +2,16 @@ import { execFile } from "node:child_process";
|
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
gitErrorText,
|
|
7
|
+
initManagedRepository,
|
|
8
|
+
markManagedRepository,
|
|
9
|
+
nativeGit,
|
|
10
|
+
nativeGitEnvironment,
|
|
11
|
+
TRANSFER_TIMEOUT_MS,
|
|
12
|
+
wasStopped,
|
|
13
|
+
} from "./native-git.js";
|
|
14
|
+
import { revisionThenPaths } from "./ref-names.js";
|
|
6
15
|
import type { GitCredentials } from "./types.js";
|
|
7
16
|
|
|
8
17
|
/**
|
|
@@ -75,16 +84,24 @@ async function withMirrorLock<T>(
|
|
|
75
84
|
}
|
|
76
85
|
}
|
|
77
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Make the mirror when it is missing. Callers that then write into it do so
|
|
89
|
+
* under {@link withMirrorLock}.
|
|
90
|
+
*/
|
|
91
|
+
export function prepareMirror(mirrorPath: string): Promise<void> {
|
|
92
|
+
return withMirrorLock(mirrorPath, () => ensureMirror(mirrorPath));
|
|
93
|
+
}
|
|
94
|
+
|
|
78
95
|
async function ensureMirror(mirrorPath: string): Promise<void> {
|
|
79
96
|
const exists = await fs.access(path.join(mirrorPath, "HEAD")).then(
|
|
80
97
|
() => true,
|
|
81
98
|
() => false,
|
|
82
99
|
);
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
//
|
|
87
|
-
await
|
|
100
|
+
// A mirror made before Work marked its repositories is marked now.
|
|
101
|
+
if (exists) return markManagedRepository(mirrorPath);
|
|
102
|
+
// Pinned refs keep what sessions stand on; nothing else is kept, and
|
|
103
|
+
// nothing is ever pruned.
|
|
104
|
+
await initManagedRepository({ path: mirrorPath, bare: true });
|
|
88
105
|
}
|
|
89
106
|
|
|
90
107
|
/**
|
|
@@ -105,7 +122,36 @@ export async function fetchIntoMirror(opts: {
|
|
|
105
122
|
assertPinRef(opts.pin);
|
|
106
123
|
return withMirrorLock(opts.mirrorPath, async () => {
|
|
107
124
|
await ensureMirror(opts.mirrorPath);
|
|
125
|
+
// A commit id is known only when one of the mirror's own refs holds
|
|
126
|
+
// it. That the object can be read is not enough: the mirror borrows
|
|
127
|
+
// the project's origin (ADR 0356), where other chats' checkpoints and
|
|
128
|
+
// members' drafts are too, and none of those is the repository's.
|
|
108
129
|
const known =
|
|
130
|
+
isCommitId(ref) &&
|
|
131
|
+
(await nativeGit(opts.mirrorPath, [
|
|
132
|
+
"rev-list",
|
|
133
|
+
"--max-count=1",
|
|
134
|
+
ref,
|
|
135
|
+
"--not",
|
|
136
|
+
"--all",
|
|
137
|
+
]).then(
|
|
138
|
+
(beyond) => beyond.trim() === "",
|
|
139
|
+
() => false,
|
|
140
|
+
));
|
|
141
|
+
const auth = opts.credentials
|
|
142
|
+
? { ...opts.credentials, url: opts.url }
|
|
143
|
+
: undefined;
|
|
144
|
+
const refused = (error: unknown) =>
|
|
145
|
+
new Error(
|
|
146
|
+
`Could not fetch '${ref}' from the project's remote: ${gitErrorText(error)}`,
|
|
147
|
+
);
|
|
148
|
+
// A commit id the mirror can read but none of its refs holds may have
|
|
149
|
+
// come from what it borrows. Fetching it here would ask the remote
|
|
150
|
+
// nothing: Git sends no request for an object it can already read. So
|
|
151
|
+
// the remote is asked from a repository that holds nothing, and only
|
|
152
|
+
// a commit it serves is pinned.
|
|
153
|
+
const readable =
|
|
154
|
+
!known &&
|
|
109
155
|
isCommitId(ref) &&
|
|
110
156
|
(await nativeGit(opts.mirrorPath, [
|
|
111
157
|
"cat-file",
|
|
@@ -115,7 +161,45 @@ export async function fetchIntoMirror(opts: {
|
|
|
115
161
|
() => true,
|
|
116
162
|
() => false,
|
|
117
163
|
));
|
|
118
|
-
if (
|
|
164
|
+
if (readable) {
|
|
165
|
+
const probe = await fs.mkdtemp(path.join(tmpdir(), "work-probe-"));
|
|
166
|
+
try {
|
|
167
|
+
await initManagedRepository({
|
|
168
|
+
path: probe,
|
|
169
|
+
bare: true,
|
|
170
|
+
objectFormat: ref.length === 64 ? "sha256" : "sha1",
|
|
171
|
+
});
|
|
172
|
+
await nativeGit(
|
|
173
|
+
probe,
|
|
174
|
+
[
|
|
175
|
+
"fetch",
|
|
176
|
+
"--quiet",
|
|
177
|
+
"--no-tags",
|
|
178
|
+
"--no-write-fetch-head",
|
|
179
|
+
// The commit alone where the remote can leave the rest out.
|
|
180
|
+
"--depth=1",
|
|
181
|
+
"--filter=tree:0",
|
|
182
|
+
"--end-of-options",
|
|
183
|
+
opts.url,
|
|
184
|
+
ref,
|
|
185
|
+
],
|
|
186
|
+
auth,
|
|
187
|
+
{ timeoutMs: TRANSFER_TIMEOUT_MS },
|
|
188
|
+
).catch((error: unknown) => {
|
|
189
|
+
throw refused(error);
|
|
190
|
+
});
|
|
191
|
+
} finally {
|
|
192
|
+
await fs.rm(probe, { recursive: true, force: true });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (known || readable) {
|
|
196
|
+
await nativeGit(opts.mirrorPath, [
|
|
197
|
+
"update-ref",
|
|
198
|
+
"--end-of-options",
|
|
199
|
+
opts.pin,
|
|
200
|
+
ref,
|
|
201
|
+
]);
|
|
202
|
+
} else {
|
|
119
203
|
await nativeGit(
|
|
120
204
|
opts.mirrorPath,
|
|
121
205
|
[
|
|
@@ -123,17 +207,15 @@ export async function fetchIntoMirror(opts: {
|
|
|
123
207
|
"--quiet",
|
|
124
208
|
"--no-tags",
|
|
125
209
|
"--no-write-fetch-head",
|
|
210
|
+
"--end-of-options",
|
|
126
211
|
opts.url,
|
|
127
212
|
`+${ref}:${opts.pin}`,
|
|
128
213
|
],
|
|
129
|
-
|
|
214
|
+
auth,
|
|
215
|
+
{ timeoutMs: TRANSFER_TIMEOUT_MS },
|
|
130
216
|
).catch((error: unknown) => {
|
|
131
|
-
throw
|
|
132
|
-
`Could not fetch '${ref}' from the project's remote: ${gitErrorText(error)}`,
|
|
133
|
-
);
|
|
217
|
+
throw refused(error);
|
|
134
218
|
});
|
|
135
|
-
} else {
|
|
136
|
-
await nativeGit(opts.mirrorPath, ["update-ref", opts.pin, ref]);
|
|
137
219
|
}
|
|
138
220
|
const commit = (
|
|
139
221
|
await nativeGit(opts.mirrorPath, [
|
|
@@ -158,7 +240,12 @@ export async function unpinMirrorRef(opts: {
|
|
|
158
240
|
);
|
|
159
241
|
if (!exists) return;
|
|
160
242
|
await withMirrorLock(opts.mirrorPath, () =>
|
|
161
|
-
nativeGit(opts.mirrorPath, [
|
|
243
|
+
nativeGit(opts.mirrorPath, [
|
|
244
|
+
"update-ref",
|
|
245
|
+
"-d",
|
|
246
|
+
"--end-of-options",
|
|
247
|
+
opts.pin,
|
|
248
|
+
]).catch(() => ""),
|
|
162
249
|
);
|
|
163
250
|
}
|
|
164
251
|
|
|
@@ -169,11 +256,15 @@ export async function mirrorChangedFiles(opts: {
|
|
|
169
256
|
to: string;
|
|
170
257
|
limit?: number;
|
|
171
258
|
}): Promise<{ files: string[]; total: number }> {
|
|
259
|
+
// Commit ids only: never a path, a revision expression, or an option.
|
|
260
|
+
if (!COMMIT.test(opts.from) || !COMMIT.test(opts.to))
|
|
261
|
+
return { files: [], total: 0 };
|
|
172
262
|
const output = await nativeGit(opts.mirrorPath, [
|
|
173
263
|
"diff",
|
|
174
264
|
"--name-only",
|
|
175
265
|
"--no-renames",
|
|
176
266
|
"-z",
|
|
267
|
+
"--end-of-options",
|
|
177
268
|
opts.from,
|
|
178
269
|
opts.to,
|
|
179
270
|
]).catch(() => "");
|
|
@@ -195,14 +286,20 @@ export async function copyFromMirror(opts: {
|
|
|
195
286
|
assertPinRef(opts.pin);
|
|
196
287
|
assertPinRef(opts.into);
|
|
197
288
|
await withMirrorLock(opts.mirrorPath, () =>
|
|
198
|
-
nativeGit(
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
289
|
+
nativeGit(
|
|
290
|
+
opts.repoPath,
|
|
291
|
+
[
|
|
292
|
+
"fetch",
|
|
293
|
+
"--quiet",
|
|
294
|
+
"--no-tags",
|
|
295
|
+
"--no-write-fetch-head",
|
|
296
|
+
"--end-of-options",
|
|
297
|
+
opts.mirrorPath,
|
|
298
|
+
`+${opts.pin}:${opts.into}`,
|
|
299
|
+
],
|
|
300
|
+
undefined,
|
|
301
|
+
{ timeoutMs: TRANSFER_TIMEOUT_MS },
|
|
302
|
+
),
|
|
206
303
|
);
|
|
207
304
|
}
|
|
208
305
|
|
|
@@ -224,7 +321,10 @@ const MOVE_AUTHOR = [
|
|
|
224
321
|
* mirror. `reset` discards the checkout's own commits and changes; `rebase`
|
|
225
322
|
* replays its commits since `from` onto `to` (keeping uncommitted changes)
|
|
226
323
|
* and, when that conflicts, puts everything back as it was so the caller
|
|
227
|
-
* can tell the agent instead of losing work.
|
|
324
|
+
* can tell the agent instead of losing work. A move that was stopped for
|
|
325
|
+
* running too long, or that could not be undone, is an error and never a
|
|
326
|
+
* conflict: the checkout may then stand part way, and its caller puts it
|
|
327
|
+
* back before anything reads its files as work.
|
|
228
328
|
*/
|
|
229
329
|
export async function moveCheckoutBase(opts: {
|
|
230
330
|
repoPath: string;
|
|
@@ -233,6 +333,8 @@ export async function moveCheckoutBase(opts: {
|
|
|
233
333
|
from: string;
|
|
234
334
|
to: string;
|
|
235
335
|
update: "reset" | "rebase";
|
|
336
|
+
/** How long the move's own Git command may run; minutes when left out. */
|
|
337
|
+
timeoutMs?: number;
|
|
236
338
|
}): Promise<BaseMoveOutcome> {
|
|
237
339
|
await copyFromMirror({
|
|
238
340
|
mirrorPath: opts.mirrorPath,
|
|
@@ -240,53 +342,111 @@ export async function moveCheckoutBase(opts: {
|
|
|
240
342
|
repoPath: opts.repoPath,
|
|
241
343
|
into: "refs/work/base",
|
|
242
344
|
});
|
|
345
|
+
for (const id of [opts.from, opts.to])
|
|
346
|
+
if (!COMMIT.test(id))
|
|
347
|
+
throw new Error("A workspace's base is named by a full commit id");
|
|
348
|
+
// The new base is what was just fetched, not any commit the copy can read.
|
|
349
|
+
await nativeGit(opts.repoPath, [
|
|
350
|
+
"merge-base",
|
|
351
|
+
"--is-ancestor",
|
|
352
|
+
opts.to,
|
|
353
|
+
"refs/work/base",
|
|
354
|
+
]).catch(() => {
|
|
355
|
+
throw new Error(
|
|
356
|
+
`The workspace's new base ${opts.to.slice(0, 12)} is not in what was fetched from the project's remote`,
|
|
357
|
+
);
|
|
358
|
+
});
|
|
243
359
|
const head = async () =>
|
|
244
360
|
(await nativeGit(opts.repoPath, ["rev-parse", "HEAD"])).trim();
|
|
361
|
+
const budget = opts.timeoutMs ? { timeoutMs: opts.timeoutMs } : undefined;
|
|
245
362
|
if (opts.update === "reset") {
|
|
246
|
-
await nativeGit(
|
|
363
|
+
await nativeGit(
|
|
364
|
+
opts.repoPath,
|
|
365
|
+
["reset", "--quiet", "--hard", ...revisionThenPaths(opts.to)],
|
|
366
|
+
undefined,
|
|
367
|
+
budget,
|
|
368
|
+
);
|
|
247
369
|
await nativeGit(opts.repoPath, ["clean", "-fdq"]);
|
|
248
370
|
return { status: "moved", head: await head() };
|
|
249
371
|
}
|
|
250
372
|
try {
|
|
251
|
-
await nativeGit(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
373
|
+
await nativeGit(
|
|
374
|
+
opts.repoPath,
|
|
375
|
+
[
|
|
376
|
+
...MOVE_AUTHOR,
|
|
377
|
+
"rebase",
|
|
378
|
+
"--quiet",
|
|
379
|
+
"--autostash",
|
|
380
|
+
"--onto",
|
|
381
|
+
opts.to,
|
|
382
|
+
opts.from,
|
|
383
|
+
],
|
|
384
|
+
undefined,
|
|
385
|
+
budget,
|
|
386
|
+
);
|
|
260
387
|
return { status: "moved", head: await head() };
|
|
261
|
-
} catch {
|
|
388
|
+
} catch (error) {
|
|
389
|
+
// NUL-separated: Git prints a name as it is, never quoted or escaped.
|
|
262
390
|
const files = (
|
|
263
391
|
await nativeGit(opts.repoPath, [
|
|
264
392
|
"diff",
|
|
265
393
|
"--name-only",
|
|
394
|
+
"-z",
|
|
266
395
|
"--diff-filter=U",
|
|
267
396
|
]).catch(() => "")
|
|
268
397
|
)
|
|
269
|
-
.split("\
|
|
398
|
+
.split("\0")
|
|
270
399
|
.filter(Boolean);
|
|
271
|
-
await nativeGit(opts.repoPath, ["rebase", "--abort"]).
|
|
400
|
+
const undone = await nativeGit(opts.repoPath, ["rebase", "--abort"]).then(
|
|
401
|
+
() => true,
|
|
402
|
+
() => false,
|
|
403
|
+
);
|
|
404
|
+
// Stopped for taking too long is not a conflict: nothing says the
|
|
405
|
+
// commits clash, and telling the agent so would be false.
|
|
406
|
+
if (wasStopped(error))
|
|
407
|
+
throw new Error(
|
|
408
|
+
"Moving the workspace onto its new base took too long and was stopped",
|
|
409
|
+
);
|
|
410
|
+
if (!undone && (await rebaseInProgress(opts.repoPath)))
|
|
411
|
+
throw new Error(
|
|
412
|
+
`The workspace could not be put back after its move onto a new base failed: ${gitErrorText(error)}`,
|
|
413
|
+
);
|
|
272
414
|
return { status: "conflict", head: await head(), files };
|
|
273
415
|
}
|
|
274
416
|
}
|
|
275
417
|
|
|
418
|
+
/** Whether a rebase stands part way in the checkout at `repoPath`. */
|
|
419
|
+
export async function rebaseInProgress(repoPath: string): Promise<boolean> {
|
|
420
|
+
for (const state of ["rebase-merge", "rebase-apply"])
|
|
421
|
+
if (
|
|
422
|
+
await fs.access(path.join(repoPath, ".git", state)).then(
|
|
423
|
+
() => true,
|
|
424
|
+
() => false,
|
|
425
|
+
)
|
|
426
|
+
)
|
|
427
|
+
return true;
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
|
|
276
431
|
/**
|
|
277
432
|
* A packfile holding `head` and its history back to `base`, and the
|
|
278
433
|
* commits a receiver must record as shallow. A sandbox seeded from it has
|
|
279
434
|
* the real commits (so `git log`, `diff`, and pushes of new branches work)
|
|
280
435
|
* without the repository's history before the base. With `tree`, the pack
|
|
281
436
|
* also holds that tree whole: a session's working tree as last saved (ADR
|
|
282
|
-
* 0344), which its sandbox puts back on top of `head`.
|
|
437
|
+
* 0344), which its sandbox puts back on top of `head`. With `holdsBase`
|
|
438
|
+
* the receiver already has the base commit and its tree (it fetched them,
|
|
439
|
+
* ADR 0353), so the pack holds only what the session added: its commits,
|
|
440
|
+
* and the files of the saved tree that differ from the base's. `objects`
|
|
441
|
+
* counts what the pack holds; an empty one need not travel.
|
|
283
442
|
*/
|
|
284
443
|
export async function buildSeedPack(opts: {
|
|
285
444
|
repoPath: string;
|
|
286
445
|
head: string;
|
|
287
446
|
base: string;
|
|
288
447
|
tree?: string;
|
|
289
|
-
|
|
448
|
+
holdsBase?: boolean;
|
|
449
|
+
}): Promise<{ pack: Uint8Array; shallow: string[]; objects: number }> {
|
|
290
450
|
for (const id of [opts.head, opts.base, ...(opts.tree ? [opts.tree] : [])])
|
|
291
451
|
if (!COMMIT.test(id)) throw new Error("A seed pack names full object ids");
|
|
292
452
|
const objects = path.resolve(
|
|
@@ -302,33 +462,91 @@ export async function buildSeedPack(opts: {
|
|
|
302
462
|
// A repository of its own that borrows the copy's objects and ends
|
|
303
463
|
// history at the base, so the walk below stops there without a ref or
|
|
304
464
|
// a shallow file ever being written into the copy itself.
|
|
305
|
-
await
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
"
|
|
309
|
-
|
|
310
|
-
]);
|
|
465
|
+
await initManagedRepository({
|
|
466
|
+
path: directory,
|
|
467
|
+
bare: true,
|
|
468
|
+
objectFormat: opts.base.length === 64 ? "sha256" : "sha1",
|
|
469
|
+
});
|
|
311
470
|
await fs.writeFile(
|
|
312
471
|
path.join(directory, "objects", "info", "alternates"),
|
|
313
472
|
`${objects}\n`,
|
|
314
473
|
);
|
|
315
474
|
await fs.writeFile(path.join(directory, "shallow"), `${opts.base}\n`);
|
|
475
|
+
// The base's tree is left out by name as well as its commit: a walk
|
|
476
|
+
// that finds no commit to send (a head still on its base) never
|
|
477
|
+
// reaches the base's tree to leave it out, and would send the saved
|
|
478
|
+
// tree whole.
|
|
479
|
+
const held = opts.holdsBase
|
|
480
|
+
? [
|
|
481
|
+
`^${opts.base}`,
|
|
482
|
+
`^${(await nativeGit(directory, ["rev-parse", "--verify", `${opts.base}^{tree}`])).trim()}`,
|
|
483
|
+
]
|
|
484
|
+
: [opts.base];
|
|
316
485
|
const pack = await gitOutput({
|
|
317
486
|
cwd: directory,
|
|
318
487
|
args: ["pack-objects", "--revs", "--stdout", "--quiet"],
|
|
319
|
-
// The base itself too: a head that left it still needs it as its
|
|
320
|
-
|
|
488
|
+
// The base itself too: a head that left it still needs it as its
|
|
489
|
+
// ref. Left out, with everything it reaches, when the receiver
|
|
490
|
+
// holds it.
|
|
491
|
+
input: `${[...held, opts.head, ...(opts.tree ? [opts.tree] : [])].join("\n")}\n`,
|
|
321
492
|
});
|
|
322
|
-
return { pack, shallow: [opts.base] };
|
|
493
|
+
return { pack, shallow: [opts.base], objects: packObjectCount(pack) };
|
|
323
494
|
} finally {
|
|
324
495
|
await fs.rm(directory, { recursive: true, force: true });
|
|
325
496
|
}
|
|
326
497
|
}
|
|
327
498
|
|
|
499
|
+
/** How many objects a packfile says it holds (its header's count). */
|
|
500
|
+
function packObjectCount(pack: Uint8Array): number {
|
|
501
|
+
if (pack.byteLength < 12) return 0;
|
|
502
|
+
return new DataView(pack.buffer, pack.byteOffset, pack.byteLength).getUint32(
|
|
503
|
+
8,
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* The bytes of a seed pack one uploaded part holds: a multiple of three,
|
|
509
|
+
* so every part is whole base64 groups and the parts decode joined, and
|
|
510
|
+
* small enough that a part's operation stays far under what a queue row
|
|
511
|
+
* can carry (4 MiB of text).
|
|
512
|
+
*/
|
|
513
|
+
const SEED_PART_BYTES = 3 * 1024 * 1024;
|
|
514
|
+
|
|
515
|
+
/** Digits in a part's number: parts sort by name whatever their count. */
|
|
516
|
+
const SEED_PART_DIGITS = 6;
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* A seed pack as the files to upload beside each other: base64, in parts
|
|
520
|
+
* named `<name>.000000`, `<name>.000001`, and so on, which
|
|
521
|
+
* {@link seedPackInstallScript} reads back in order. One file per upload
|
|
522
|
+
* keeps every operation small whatever the pack's size, and each part is
|
|
523
|
+
* encoded on its own, so no string grows with the pack either.
|
|
524
|
+
*/
|
|
525
|
+
export function seedPackParts(opts: {
|
|
526
|
+
pack: Uint8Array;
|
|
527
|
+
name: string;
|
|
528
|
+
}): { name: string; content: string }[] {
|
|
529
|
+
const bytes = Buffer.from(
|
|
530
|
+
opts.pack.buffer,
|
|
531
|
+
opts.pack.byteOffset,
|
|
532
|
+
opts.pack.byteLength,
|
|
533
|
+
);
|
|
534
|
+
const parts: { name: string; content: string }[] = [];
|
|
535
|
+
for (let start = 0; start < bytes.byteLength; start += SEED_PART_BYTES)
|
|
536
|
+
parts.push({
|
|
537
|
+
name: `${opts.name}.${String(parts.length).padStart(SEED_PART_DIGITS, "0")}`,
|
|
538
|
+
content: bytes
|
|
539
|
+
.subarray(start, start + SEED_PART_BYTES)
|
|
540
|
+
.toString("base64"),
|
|
541
|
+
});
|
|
542
|
+
return parts;
|
|
543
|
+
}
|
|
544
|
+
|
|
328
545
|
/**
|
|
329
|
-
* Shell that installs a seed pack uploaded as base64
|
|
330
|
-
*
|
|
331
|
-
*
|
|
546
|
+
* Shell that installs a seed pack uploaded as base64 parts
|
|
547
|
+
* (`<packFile>.000000`, ...; see {@link seedPackParts}) into the repository
|
|
548
|
+
* in the current directory and records `shallow` commits. It leaves refs
|
|
549
|
+
* and the working tree to the caller.
|
|
332
550
|
*/
|
|
333
551
|
export function seedPackInstallScript(opts: {
|
|
334
552
|
packFile: string;
|
|
@@ -338,9 +556,12 @@ export function seedPackInstallScript(opts: {
|
|
|
338
556
|
if (!COMMIT.test(commit)) throw new Error("Invalid shallow commit");
|
|
339
557
|
}
|
|
340
558
|
const gitDir = `"$(git rev-parse --git-dir)"`;
|
|
559
|
+
const parts = `${shellQuote(opts.packFile)}.${"[0-9]".repeat(SEED_PART_DIGITS)}`;
|
|
341
560
|
return [
|
|
342
|
-
|
|
343
|
-
|
|
561
|
+
// Parts sort by name, and each is whole base64 groups, so they decode
|
|
562
|
+
// as one stream.
|
|
563
|
+
`cat ${parts} | base64 -d | git index-pack --stdin --fix-thin >/dev/null`,
|
|
564
|
+
`rm -f ${parts}`,
|
|
344
565
|
...(opts.shallow.length > 0
|
|
345
566
|
? [
|
|
346
567
|
`touch ${gitDir}/shallow`,
|
|
@@ -361,26 +582,13 @@ function assertPinRef(ref: string): void {
|
|
|
361
582
|
}
|
|
362
583
|
}
|
|
363
584
|
|
|
364
|
-
function gitErrorText(error: unknown): string {
|
|
365
|
-
const stderr =
|
|
366
|
-
error && typeof error === "object" && "stderr" in error
|
|
367
|
-
? String(error.stderr)
|
|
368
|
-
: "";
|
|
369
|
-
const text = (stderr || (error instanceof Error ? error.message : ""))
|
|
370
|
-
.split("\n")
|
|
371
|
-
.map((line) => line.trim())
|
|
372
|
-
.filter((line) => line.length > 0 && !/authorization/i.test(line))
|
|
373
|
-
.slice(-2)
|
|
374
|
-
.join(" ");
|
|
375
|
-
return text || "the remote refused the request";
|
|
376
|
-
}
|
|
377
|
-
|
|
378
585
|
/** Run Git and collect its binary standard output (a pack may be large). */
|
|
379
|
-
function gitOutput(opts: {
|
|
586
|
+
async function gitOutput(opts: {
|
|
380
587
|
cwd: string;
|
|
381
588
|
args: readonly string[];
|
|
382
589
|
input?: string;
|
|
383
590
|
}): Promise<Uint8Array> {
|
|
591
|
+
const env = await nativeGitEnvironment({ cwd: opts.cwd });
|
|
384
592
|
return new Promise((resolve, reject) => {
|
|
385
593
|
const child = execFile(
|
|
386
594
|
"git",
|
|
@@ -389,7 +597,7 @@ function gitOutput(opts: {
|
|
|
389
597
|
encoding: "buffer",
|
|
390
598
|
maxBuffer: 2 * 1024 * 1024 * 1024,
|
|
391
599
|
timeout: 15 * 60_000,
|
|
392
|
-
env
|
|
600
|
+
env,
|
|
393
601
|
},
|
|
394
602
|
(error, stdout) => {
|
|
395
603
|
if (error) reject(error);
|