@catamorphic/git 0.1.0-alpha.38 → 0.1.0-alpha.41
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/NOTICE.txt +7 -0
- package/dist/git-sync.d.ts +2 -0
- package/dist/git-sync.d.ts.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1324 -104
- package/dist/index.js.map +14 -12
- package/dist/native-project-repo.d.ts +1 -0
- package/dist/native-project-repo.d.ts.map +1 -1
- package/dist/network.d.ts +43 -4
- package/dist/network.d.ts.map +1 -1
- package/dist/origin-draft.d.ts +10 -2
- package/dist/origin-draft.d.ts.map +1 -1
- package/dist/origin-objects.d.ts +5 -0
- package/dist/origin-objects.d.ts.map +1 -1
- package/dist/project-manager.d.ts +97 -9
- package/dist/project-manager.d.ts.map +1 -1
- package/dist/project-repo.d.ts +2 -0
- package/dist/project-repo.d.ts.map +1 -1
- package/dist/session-checkpoint.d.ts +75 -0
- package/dist/session-checkpoint.d.ts.map +1 -0
- package/dist/workspace-mirror.d.ts +4 -1
- package/dist/workspace-mirror.d.ts.map +1 -1
- package/dist/workspace-save.d.ts +170 -0
- package/dist/workspace-save.d.ts.map +1 -0
- package/package.json +6 -6
- package/src/git-sync.ts +7 -0
- package/src/index.ts +20 -0
- package/src/native-project-repo.ts +148 -16
- package/src/network.ts +210 -7
- package/src/origin-draft.ts +42 -13
- package/src/origin-objects.ts +38 -12
- package/src/project-manager.ts +401 -47
- package/src/project-repo.ts +26 -5
- package/src/session-checkpoint.ts +210 -0
- package/src/workspace-mirror.ts +31 -30
- package/src/workspace-save.ts +999 -0
package/src/network.ts
CHANGED
|
@@ -2,7 +2,8 @@ import nodeFs from "node:fs";
|
|
|
2
2
|
import { MANAGED_BRANCH_PREFIX } from "@catamorphic/workflow/project-layout";
|
|
3
3
|
import git from "isomorphic-git";
|
|
4
4
|
import http from "isomorphic-git/http/node";
|
|
5
|
-
import { nativeGit } from "./native-git.js";
|
|
5
|
+
import { type NativeGitAuth, nativeGit } from "./native-git.js";
|
|
6
|
+
import { isValidRefName } from "./ref-names.js";
|
|
6
7
|
import type { GitCredentials } from "./types.js";
|
|
7
8
|
|
|
8
9
|
export interface CloneFromRemoteOptions {
|
|
@@ -144,15 +145,20 @@ export function isManagedBranch(branch: string): boolean {
|
|
|
144
145
|
}
|
|
145
146
|
|
|
146
147
|
/**
|
|
147
|
-
* The one push rule (
|
|
148
|
-
* fast-forward branches under `work/`; the default branch and
|
|
149
|
-
* branch belong to the people who share the repository
|
|
150
|
-
*
|
|
148
|
+
* The one push rule (ADRs 0170, 0345). On an attached remote Work may only
|
|
149
|
+
* create or fast-forward branches under `work/`; the default branch and
|
|
150
|
+
* every other branch belong to the people who share the repository.
|
|
151
|
+
* History there is rewritten in one case only: a chat force-updates its
|
|
152
|
+
* own `work/` branch with a lease on the commit it last pushed, so
|
|
153
|
+
* anything anyone else pushed there refuses the push instead of being
|
|
154
|
+
* overwritten. Owned remotes are unrestricted.
|
|
151
155
|
*/
|
|
152
156
|
export function assertPushAllowed(input: {
|
|
153
157
|
ownership: RemoteOwnership;
|
|
154
158
|
remoteBranch: string;
|
|
155
159
|
force?: boolean;
|
|
160
|
+
/** The push names the commit it expects to replace. */
|
|
161
|
+
lease?: boolean;
|
|
156
162
|
}): void {
|
|
157
163
|
if (input.ownership === "owned") return;
|
|
158
164
|
if (!isManagedBranch(input.remoteBranch)) {
|
|
@@ -161,7 +167,7 @@ export function assertPushAllowed(input: {
|
|
|
161
167
|
`this repository is attached, so Work only pushes branches it creates under '${MANAGED_BRANCH_PREFIX}'. Open a pull request instead.`,
|
|
162
168
|
);
|
|
163
169
|
}
|
|
164
|
-
if (input.force) {
|
|
170
|
+
if (input.force && !input.lease) {
|
|
165
171
|
throw new RemotePushRefusedError(
|
|
166
172
|
input.remoteBranch,
|
|
167
173
|
"Work never rewrites history in an attached repository.",
|
|
@@ -187,6 +193,11 @@ export async function pushToRemote(opts: {
|
|
|
187
193
|
/** Branch name on the remote (a plain name, not `refs/heads/...`). */
|
|
188
194
|
remoteBranch: string;
|
|
189
195
|
force?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Replace the branch only while it still holds `expected` (null: only
|
|
198
|
+
* while it does not exist). Native Git only.
|
|
199
|
+
*/
|
|
200
|
+
lease?: { expected: string | null };
|
|
190
201
|
}): Promise<void> {
|
|
191
202
|
const { ref, remoteBranch } = opts;
|
|
192
203
|
// Plain names only: a `+`, `:` or leading `-` would smuggle a forced or
|
|
@@ -196,7 +207,31 @@ export async function pushToRemote(opts: {
|
|
|
196
207
|
throw new RemotePushRefusedError(name, "not a plain branch or ref name.");
|
|
197
208
|
}
|
|
198
209
|
}
|
|
199
|
-
assertPushAllowed(
|
|
210
|
+
assertPushAllowed({
|
|
211
|
+
ownership: opts.ownership,
|
|
212
|
+
remoteBranch,
|
|
213
|
+
force: opts.force ?? opts.lease !== undefined,
|
|
214
|
+
lease: opts.lease !== undefined,
|
|
215
|
+
});
|
|
216
|
+
if (opts.lease) {
|
|
217
|
+
const expected = opts.lease.expected ?? "";
|
|
218
|
+
if (!opts.native || (expected && !/^[0-9a-f]{40,64}$/.test(expected)))
|
|
219
|
+
throw new RemotePushRefusedError(
|
|
220
|
+
remoteBranch,
|
|
221
|
+
"a push with a lease names the commit it replaces.",
|
|
222
|
+
);
|
|
223
|
+
await pushWithLease({
|
|
224
|
+
repoPath: opts.repoPath,
|
|
225
|
+
url: opts.url,
|
|
226
|
+
ref,
|
|
227
|
+
target: `refs/heads/${remoteBranch}`,
|
|
228
|
+
expected,
|
|
229
|
+
auth: opts.credentials
|
|
230
|
+
? { ...opts.credentials, url: opts.url }
|
|
231
|
+
: undefined,
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
200
235
|
if (opts.native) {
|
|
201
236
|
await nativeGit(
|
|
202
237
|
opts.repoPath,
|
|
@@ -222,6 +257,174 @@ export async function pushToRemote(opts: {
|
|
|
222
257
|
});
|
|
223
258
|
}
|
|
224
259
|
|
|
260
|
+
function pushWithLease(input: {
|
|
261
|
+
repoPath: string;
|
|
262
|
+
url: string;
|
|
263
|
+
ref: string;
|
|
264
|
+
target: string;
|
|
265
|
+
expected: string;
|
|
266
|
+
auth: NativeGitAuth | undefined;
|
|
267
|
+
}): Promise<string> {
|
|
268
|
+
return nativeGit(
|
|
269
|
+
input.repoPath,
|
|
270
|
+
[
|
|
271
|
+
"push",
|
|
272
|
+
"--porcelain",
|
|
273
|
+
"--no-follow-tags",
|
|
274
|
+
`--force-with-lease=${input.target}:${input.expected}`,
|
|
275
|
+
input.url,
|
|
276
|
+
`${input.ref}:${input.target}`,
|
|
277
|
+
],
|
|
278
|
+
input.auth,
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The branch a chat last pushed to the project's repository (ADR 0345). */
|
|
283
|
+
export interface PushedBranch {
|
|
284
|
+
branch: string;
|
|
285
|
+
commit: string;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Push a chat's branch (ADR 0345): a `work/` branch it creates, moves
|
|
290
|
+
* forward, or, after a rebase, replaces. It replaces only a branch it
|
|
291
|
+
* pushed before, and only while that branch still holds the commit it last
|
|
292
|
+
* pushed there; anything a person or another chat pushed since refuses
|
|
293
|
+
* the push. Never a branch outside `work/`, and never a deletion.
|
|
294
|
+
*/
|
|
295
|
+
export async function pushOwnBranch(opts: {
|
|
296
|
+
repoPath: string;
|
|
297
|
+
url: string;
|
|
298
|
+
credentials?: GitCredentials;
|
|
299
|
+
ownership: RemoteOwnership;
|
|
300
|
+
/** The local commit to push. */
|
|
301
|
+
commit: string;
|
|
302
|
+
remoteBranch: string;
|
|
303
|
+
/** What this chat last pushed, when it pushed before. */
|
|
304
|
+
pushed?: PushedBranch;
|
|
305
|
+
}): Promise<{
|
|
306
|
+
outcome: "created" | "updated" | "replaced" | "unchanged";
|
|
307
|
+
/** This chat created the branch or updated the exact tip it owned. */
|
|
308
|
+
recordOwnership: boolean;
|
|
309
|
+
}> {
|
|
310
|
+
const { remoteBranch, commit } = opts;
|
|
311
|
+
if (!isManagedBranch(remoteBranch) || !isValidRefName(remoteBranch))
|
|
312
|
+
throw new RemotePushRefusedError(
|
|
313
|
+
remoteBranch,
|
|
314
|
+
`a chat pushes only its own branch under '${MANAGED_BRANCH_PREFIX}'.`,
|
|
315
|
+
);
|
|
316
|
+
if (!/^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(commit))
|
|
317
|
+
throw new RemotePushRefusedError(remoteBranch, "not a commit to push.");
|
|
318
|
+
assertPushAllowed({
|
|
319
|
+
ownership: opts.ownership,
|
|
320
|
+
remoteBranch,
|
|
321
|
+
force: true,
|
|
322
|
+
lease: true,
|
|
323
|
+
});
|
|
324
|
+
const auth = opts.credentials
|
|
325
|
+
? { ...opts.credentials, url: opts.url }
|
|
326
|
+
: undefined;
|
|
327
|
+
const target = `refs/heads/${remoteBranch}`;
|
|
328
|
+
const listed = await nativeGit(
|
|
329
|
+
opts.repoPath,
|
|
330
|
+
["ls-remote", "--symref", opts.url, "HEAD", target],
|
|
331
|
+
auth,
|
|
332
|
+
);
|
|
333
|
+
const refs = new Map<string, string>();
|
|
334
|
+
for (const line of listed.split("\n")) {
|
|
335
|
+
const [value, name] = line.split("\t");
|
|
336
|
+
if (name && value && /^[0-9a-f]{40}$|^[0-9a-f]{64}$/.test(value))
|
|
337
|
+
refs.set(name, value);
|
|
338
|
+
}
|
|
339
|
+
const defaultRef = listed.match(/^ref: (refs\/heads\/[^\t\n]+)\tHEAD$/m)?.[1];
|
|
340
|
+
if (!defaultRef || !refs.get("HEAD"))
|
|
341
|
+
throw new RemotePushRefusedError(
|
|
342
|
+
remoteBranch,
|
|
343
|
+
"the remote names no default branch, so Work cannot check this push.",
|
|
344
|
+
);
|
|
345
|
+
if (target === defaultRef)
|
|
346
|
+
throw new RemotePushRefusedError(
|
|
347
|
+
remoteBranch,
|
|
348
|
+
"Work cannot push a chat to the repository's default branch.",
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
const remote = refs.get(target);
|
|
352
|
+
const push = async (expected: string) => {
|
|
353
|
+
const output = await pushWithLease({
|
|
354
|
+
repoPath: opts.repoPath,
|
|
355
|
+
url: opts.url,
|
|
356
|
+
ref: commit,
|
|
357
|
+
target,
|
|
358
|
+
expected,
|
|
359
|
+
auth,
|
|
360
|
+
}).catch((error: unknown) => {
|
|
361
|
+
throw isStaleLease(error)
|
|
362
|
+
? new RemotePushRefusedError(
|
|
363
|
+
remoteBranch,
|
|
364
|
+
"the branch changed in the repository while it was being pushed. Try again.",
|
|
365
|
+
)
|
|
366
|
+
: error;
|
|
367
|
+
});
|
|
368
|
+
// If another caller pushed this exact commit in the meantime, Git can
|
|
369
|
+
// report success without updating the ref, even with a lease.
|
|
370
|
+
const status = output
|
|
371
|
+
.split("\n")
|
|
372
|
+
.find((line) => line.split("\t")[1]?.endsWith(`:${target}`))?.[0];
|
|
373
|
+
if (status === "=") return false;
|
|
374
|
+
if (status === "*" || status === "+" || status === " ") return true;
|
|
375
|
+
throw new Error("Git did not confirm what happened to the pushed branch.");
|
|
376
|
+
};
|
|
377
|
+
const unchanged: Awaited<ReturnType<typeof pushOwnBranch>> = {
|
|
378
|
+
outcome: "unchanged",
|
|
379
|
+
recordOwnership: false,
|
|
380
|
+
};
|
|
381
|
+
if (!remote)
|
|
382
|
+
return (await push(""))
|
|
383
|
+
? { outcome: "created", recordOwnership: true }
|
|
384
|
+
: unchanged;
|
|
385
|
+
if (remote === commit) return unchanged;
|
|
386
|
+
|
|
387
|
+
const ownsTip =
|
|
388
|
+
opts.pushed?.branch === remoteBranch && opts.pushed.commit === remote;
|
|
389
|
+
const forward = await nativeGit(opts.repoPath, [
|
|
390
|
+
"merge-base",
|
|
391
|
+
"--is-ancestor",
|
|
392
|
+
remote,
|
|
393
|
+
commit,
|
|
394
|
+
]).then(
|
|
395
|
+
() => true,
|
|
396
|
+
() => false,
|
|
397
|
+
);
|
|
398
|
+
if (forward)
|
|
399
|
+
return (await push(remote))
|
|
400
|
+
? { outcome: "updated", recordOwnership: ownsTip }
|
|
401
|
+
: unchanged;
|
|
402
|
+
|
|
403
|
+
if (opts.pushed?.branch !== remoteBranch)
|
|
404
|
+
throw new RemotePushRefusedError(
|
|
405
|
+
remoteBranch,
|
|
406
|
+
"a branch of that name is already in the repository with other commits, and this chat did not create it. Switch to another branch name and try again.",
|
|
407
|
+
);
|
|
408
|
+
if (!ownsTip)
|
|
409
|
+
throw new RemotePushRefusedError(
|
|
410
|
+
remoteBranch,
|
|
411
|
+
"someone else pushed to this branch since this chat last did, so it was not overwritten. Fetch the branch, rebase or merge onto it, and try again.",
|
|
412
|
+
);
|
|
413
|
+
return (await push(remote))
|
|
414
|
+
? { outcome: "replaced", recordOwnership: true }
|
|
415
|
+
: unchanged;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function isStaleLease(error: unknown): boolean {
|
|
419
|
+
const text =
|
|
420
|
+
error && typeof error === "object" && "stderr" in error
|
|
421
|
+
? String(error.stderr)
|
|
422
|
+
: error instanceof Error
|
|
423
|
+
? error.message
|
|
424
|
+
: "";
|
|
425
|
+
return /stale info|\[rejected\]|already exists/i.test(text);
|
|
426
|
+
}
|
|
427
|
+
|
|
225
428
|
/**
|
|
226
429
|
* Fetch a branch from a network remote into a tracking ref without touching
|
|
227
430
|
* the working tree. Returns the fetched sha (null when the remote branch does
|
package/src/origin-draft.ts
CHANGED
|
@@ -46,9 +46,17 @@ const MAX_ATTEMPTS = 8;
|
|
|
46
46
|
*/
|
|
47
47
|
export type ProjectDraft = ProjectRepo | OriginDraftRepo;
|
|
48
48
|
|
|
49
|
-
/**
|
|
49
|
+
/**
|
|
50
|
+
* One change to a draft: new content, or `delete: true`. `mode` is the
|
|
51
|
+
* file's Git mode, kept from the file it replaces when left out: `100755`
|
|
52
|
+
* for an executable, `120000` for a link whose target is the content.
|
|
53
|
+
*/
|
|
50
54
|
export type DraftChange =
|
|
51
|
-
| {
|
|
55
|
+
| {
|
|
56
|
+
path: string;
|
|
57
|
+
content: string | Uint8Array;
|
|
58
|
+
mode?: "100644" | "100755" | "120000";
|
|
59
|
+
}
|
|
52
60
|
| { path: string; delete: true };
|
|
53
61
|
|
|
54
62
|
export type DraftPublishResult =
|
|
@@ -466,7 +474,9 @@ export class OriginDraftRepo {
|
|
|
466
474
|
* Paths the project's ignore rules keep out of history are refused, or
|
|
467
475
|
* dropped with `skipIgnored`; with `skipMissingDeletes`, deleting a file
|
|
468
476
|
* the draft lacks is not an error (a sandbox sync reporting a file it
|
|
469
|
-
* made and removed again).
|
|
477
|
+
* made and removed again). With `replaceLinks`, a change to a path that
|
|
478
|
+
* is a link replaces or deletes the link itself, as Git would (a saved
|
|
479
|
+
* workspace, ADR 0343); otherwise it is refused. No write follows one.
|
|
470
480
|
*/
|
|
471
481
|
async write(input: {
|
|
472
482
|
changes: readonly DraftChange[];
|
|
@@ -478,16 +488,24 @@ export class OriginDraftRepo {
|
|
|
478
488
|
expected?: Record<string, string>;
|
|
479
489
|
skipIgnored?: boolean;
|
|
480
490
|
skipMissingDeletes?: boolean;
|
|
491
|
+
replaceLinks?: boolean;
|
|
481
492
|
}): Promise<string | null> {
|
|
482
|
-
const pending = new Map<
|
|
493
|
+
const pending = new Map<
|
|
494
|
+
string,
|
|
495
|
+
{ content: Uint8Array; mode?: string } | null
|
|
496
|
+
>();
|
|
483
497
|
for (const change of input.changes)
|
|
484
498
|
pending.set(
|
|
485
499
|
normalizePath(change.path),
|
|
486
500
|
"delete" in change
|
|
487
501
|
? null
|
|
488
|
-
:
|
|
489
|
-
|
|
490
|
-
|
|
502
|
+
: {
|
|
503
|
+
content:
|
|
504
|
+
typeof change.content === "string"
|
|
505
|
+
? new TextEncoder().encode(change.content)
|
|
506
|
+
: change.content,
|
|
507
|
+
...(change.mode ? { mode: change.mode } : {}),
|
|
508
|
+
},
|
|
491
509
|
);
|
|
492
510
|
const draftRefName = this.draftRefOrThrow();
|
|
493
511
|
if (pending.size === 0) return null;
|
|
@@ -495,13 +513,18 @@ export class OriginDraftRepo {
|
|
|
495
513
|
input.message ??
|
|
496
514
|
`Draft: ${[...pending.keys()].slice(0, 3).join(", ")}${pending.size > 3 ? ", ..." : ""}`;
|
|
497
515
|
// Contents are immutable objects: write them once, before any race.
|
|
498
|
-
const written = [...pending].
|
|
499
|
-
|
|
516
|
+
const written = [...pending].flatMap(([file, change]) =>
|
|
517
|
+
change ? [{ file, ...change }] : [],
|
|
500
518
|
);
|
|
501
519
|
const oids = await this.withObjects((objects) =>
|
|
502
|
-
objects.writeBlobs(written.map((
|
|
520
|
+
objects.writeBlobs(written.map((change) => change.content)),
|
|
521
|
+
);
|
|
522
|
+
const blobs = new Map(
|
|
523
|
+
written.map((change, index) => [
|
|
524
|
+
change.file,
|
|
525
|
+
{ oid: oids[index], mode: change.mode },
|
|
526
|
+
]),
|
|
503
527
|
);
|
|
504
|
-
const blobs = new Map(written.map(([file], index) => [file, oids[index]]));
|
|
505
528
|
for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {
|
|
506
529
|
const outcome = await this.attempt(async (objects) => {
|
|
507
530
|
const { draft, main } = await this.tips(objects);
|
|
@@ -525,14 +548,20 @@ export class OriginDraftRepo {
|
|
|
525
548
|
ignored.push(file);
|
|
526
549
|
continue;
|
|
527
550
|
}
|
|
528
|
-
const
|
|
529
|
-
changes.set(
|
|
551
|
+
const blob = blobs.get(file);
|
|
552
|
+
changes.set(
|
|
553
|
+
file,
|
|
554
|
+
blob?.oid
|
|
555
|
+
? { oid: blob.oid, ...(blob.mode ? { mode: blob.mode } : {}) }
|
|
556
|
+
: null,
|
|
557
|
+
);
|
|
530
558
|
}
|
|
531
559
|
if (ignored.length > 0 && !input.skipIgnored)
|
|
532
560
|
throw new DraftIgnoredPathError(ignored);
|
|
533
561
|
if (changes.size === 0) return { sha: parent };
|
|
534
562
|
const tree = await objects.writeTree(baseTree, changes, {
|
|
535
563
|
skipMissingDeletes: input.skipMissingDeletes,
|
|
564
|
+
replaceLinks: input.replaceLinks,
|
|
536
565
|
});
|
|
537
566
|
if (tree === baseTree) return { sha: parent };
|
|
538
567
|
const sha = await objects.writeCommit({
|
package/src/origin-objects.ts
CHANGED
|
@@ -373,7 +373,8 @@ export class OriginObjects {
|
|
|
373
373
|
if (segments.slice(0, -1).some((segment) => ALWAYS_IGNORED.has(segment)))
|
|
374
374
|
return true;
|
|
375
375
|
if (!treeOid) return false;
|
|
376
|
-
|
|
376
|
+
// A path through a link is tracked by nothing; the write itself says so.
|
|
377
|
+
if (await this.entry(treeOid, filePath).catch(() => null)) return false;
|
|
377
378
|
let ignored = false;
|
|
378
379
|
for (let depth = 0; depth < segments.length; depth++) {
|
|
379
380
|
const directory = segments.slice(0, depth).join("/");
|
|
@@ -401,14 +402,18 @@ export class OriginObjects {
|
|
|
401
402
|
* `treeOid` with `changes` applied (path to an existing blob, or `null`
|
|
402
403
|
* to delete), written to the origin. Returns the new tree id. With
|
|
403
404
|
* `skipMissingDeletes`, deleting a path the tree lacks is not an error.
|
|
405
|
+
* With `replaceLinks`, a change to a path that is a link replaces or
|
|
406
|
+
* deletes the link's own entry; a path through a link is always refused.
|
|
407
|
+
* A file may take the place of a folder whose every file the same
|
|
408
|
+
* changes delete.
|
|
404
409
|
*/
|
|
405
410
|
async writeTree(
|
|
406
411
|
treeOid: string | null,
|
|
407
412
|
changes: ReadonlyMap<string, TreeChange>,
|
|
408
|
-
options?: { skipMissingDeletes?: boolean },
|
|
413
|
+
options?: { skipMissingDeletes?: boolean; replaceLinks?: boolean },
|
|
409
414
|
): Promise<string> {
|
|
410
415
|
return (
|
|
411
|
-
(await this.rewrite(treeOid, changes, "", options
|
|
416
|
+
(await this.rewrite(treeOid, changes, "", options ?? {})) ??
|
|
412
417
|
this.write({ type: "tree", data: new Uint8Array() })
|
|
413
418
|
);
|
|
414
419
|
}
|
|
@@ -417,8 +422,9 @@ export class OriginObjects {
|
|
|
417
422
|
treeOid: string | null,
|
|
418
423
|
changes: ReadonlyMap<string, TreeChange>,
|
|
419
424
|
base: string,
|
|
420
|
-
skipMissingDeletes
|
|
425
|
+
options: { skipMissingDeletes?: boolean; replaceLinks?: boolean },
|
|
421
426
|
): Promise<string | null> {
|
|
427
|
+
const skipMissingDeletes = options.skipMissingDeletes ?? false;
|
|
422
428
|
// Keyed by the stored bytes, so names that are not UTF-8 (and would
|
|
423
429
|
// decode alike) stay distinct entries.
|
|
424
430
|
const entries = new Map(
|
|
@@ -428,21 +434,41 @@ export class OriginObjects {
|
|
|
428
434
|
]),
|
|
429
435
|
);
|
|
430
436
|
const keyed = (name: string) => nameKey(new TextEncoder().encode(name));
|
|
437
|
+
const leaves = new Map<string, TreeChange>();
|
|
431
438
|
const nested = new Map<string, Map<string, TreeChange>>();
|
|
432
439
|
for (const [filePath, change] of changes) {
|
|
433
440
|
const slash = filePath.indexOf("/");
|
|
434
|
-
if (slash
|
|
435
|
-
|
|
436
|
-
const inner = nested.get(directory) ?? new Map<string, TreeChange>();
|
|
437
|
-
inner.set(filePath.slice(slash + 1), change);
|
|
438
|
-
nested.set(directory, inner);
|
|
441
|
+
if (slash < 0) {
|
|
442
|
+
leaves.set(filePath, change);
|
|
439
443
|
continue;
|
|
440
444
|
}
|
|
441
|
-
const
|
|
445
|
+
const directory = filePath.slice(0, slash);
|
|
446
|
+
const inner = nested.get(directory) ?? new Map<string, TreeChange>();
|
|
447
|
+
inner.set(filePath.slice(slash + 1), change);
|
|
448
|
+
nested.set(directory, inner);
|
|
449
|
+
}
|
|
450
|
+
for (const [filePath, change] of leaves) {
|
|
442
451
|
const fullPath = `${base}${filePath}`;
|
|
452
|
+
const inside = nested.get(filePath);
|
|
453
|
+
const found = entries.get(keyed(filePath));
|
|
454
|
+
// A file where a folder was: the folder goes when the same changes
|
|
455
|
+
// delete everything in it.
|
|
456
|
+
if (
|
|
457
|
+
found &&
|
|
458
|
+
isTreeMode(found.mode) &&
|
|
459
|
+
change !== null &&
|
|
460
|
+
inside &&
|
|
461
|
+
[...inside.values()].every((entry) => entry === null) &&
|
|
462
|
+
(await this.rewrite(found.oid, inside, `${fullPath}/`, options)) ===
|
|
463
|
+
null
|
|
464
|
+
) {
|
|
465
|
+
entries.delete(keyed(filePath));
|
|
466
|
+
nested.delete(filePath);
|
|
467
|
+
}
|
|
468
|
+
const existing = entries.get(keyed(filePath));
|
|
443
469
|
if (existing && isTreeMode(existing.mode))
|
|
444
470
|
throw new DraftPathError({ code: "EISDIR", path: fullPath });
|
|
445
|
-
if (existing?.mode === SYMLINK_MODE)
|
|
471
|
+
if (existing?.mode === SYMLINK_MODE && !options.replaceLinks)
|
|
446
472
|
throw new DraftPathError({ code: "ELOOP", path: fullPath });
|
|
447
473
|
if (change === null) {
|
|
448
474
|
if (!existing) {
|
|
@@ -480,7 +506,7 @@ export class OriginObjects {
|
|
|
480
506
|
existing?.oid ?? null,
|
|
481
507
|
inner,
|
|
482
508
|
`${fullPath}/`,
|
|
483
|
-
|
|
509
|
+
options,
|
|
484
510
|
);
|
|
485
511
|
return { directory, oid, keep: false };
|
|
486
512
|
}),
|