@catamorphic/git 0.1.0-alpha.43 → 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 +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3824 -3099
- package/dist/index.js.map +23 -22
- 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.map +1 -1
- package/dist/workspace-mirror.d.ts +13 -1
- 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 +9 -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 +16 -9
- package/src/workspace-mirror.ts +199 -58
- 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/ref-names.ts
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
export function isValidRefName(ref: string): boolean {
|
|
8
8
|
if (!ref || ref === "@" || ref.endsWith("/") || ref.endsWith("."))
|
|
9
9
|
return false;
|
|
10
|
+
// A name that starts with a dash reads as an option to every command.
|
|
11
|
+
if (ref.startsWith("-")) return false;
|
|
10
12
|
if (ref.includes("..") || ref.includes("//") || ref.includes("@{"))
|
|
11
13
|
return false;
|
|
12
14
|
// Control characters, space, and git's reserved punctuation.
|
|
@@ -45,3 +47,57 @@ export function assertResolvableRef(ref: string): void {
|
|
|
45
47
|
if (ref === "HEAD" || /^[0-9a-f]{40}$/.test(ref)) return;
|
|
46
48
|
assertValidRefName(ref);
|
|
47
49
|
}
|
|
50
|
+
|
|
51
|
+
const OBJECT_ID = /^[0-9a-f]{40}$|^[0-9a-f]{64}$/;
|
|
52
|
+
|
|
53
|
+
/** Whether a value is a full object id: 40 or 64 lower-case hex digits. */
|
|
54
|
+
export function isObjectId(value: string): boolean {
|
|
55
|
+
return OBJECT_ID.test(value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Refuse anything but a full object id where one is passed to Git: an id is
|
|
60
|
+
* never a ref name, a path, a revision expression, or an option.
|
|
61
|
+
*/
|
|
62
|
+
export function assertObjectId(value: string): void {
|
|
63
|
+
if (!OBJECT_ID.test(value))
|
|
64
|
+
throw new Error(`Invalid object id: ${JSON.stringify(value.slice(0, 80))}`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Refuse a value Git would read as an option where a path, an address or a
|
|
69
|
+
* revision is meant. Commands also mark where their options end (see
|
|
70
|
+
* {@link revisionThenPaths}); this makes the refusal plain where it is not
|
|
71
|
+
* a Git error.
|
|
72
|
+
*/
|
|
73
|
+
export function assertNotOption(value: string): void {
|
|
74
|
+
if (value.startsWith("-") || /[\n\0]/.test(value))
|
|
75
|
+
throw new Error(
|
|
76
|
+
`${JSON.stringify(value.slice(0, 80))} cannot be passed to Git as a value`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A revision as `git checkout` and `git reset` take it: the revision, then
|
|
82
|
+
* `--`, which says that what came before is a revision and never a path.
|
|
83
|
+
*
|
|
84
|
+
* How a value reaches Git, so that none is read as an option. There are two
|
|
85
|
+
* forms, and which one a command takes is a fact about Git, checked by
|
|
86
|
+
* running each command on Git 2.32 (the oldest Work supports) and later:
|
|
87
|
+
*
|
|
88
|
+
* - `--end-of-options` before the values, for every command that honours
|
|
89
|
+
* it on Git 2.32: `log`, `rev-parse`, `for-each-ref`, `update-ref`,
|
|
90
|
+
* `branch`, `diff`, `fetch`, `push`, `ls-remote`, `remote`.
|
|
91
|
+
* - The revision followed by `--`, for `checkout` and `reset`. Before Git
|
|
92
|
+
* 2.43.1 these two read the marker as the revision itself and fail
|
|
93
|
+
* (`'--end-of-options' is not a commit`), and that is the Git of Debian
|
|
94
|
+
* 12, Ubuntu 22.04 and 24.04, and older Xcode. Nothing precedes the
|
|
95
|
+
* revision to stop it reading as an option, so it is refused here when
|
|
96
|
+
* it starts with a dash.
|
|
97
|
+
*
|
|
98
|
+
* A new command with the marker is run on Git 2.32 before it is trusted.
|
|
99
|
+
*/
|
|
100
|
+
export function revisionThenPaths(revision: string): [string, "--"] {
|
|
101
|
+
assertNotOption(revision);
|
|
102
|
+
return [revision, "--"];
|
|
103
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -248,8 +248,9 @@ export type DraftSupport =
|
|
|
248
248
|
| { supported: false; reason: string };
|
|
249
249
|
|
|
250
250
|
/**
|
|
251
|
-
*
|
|
252
|
-
*
|
|
251
|
+
* A project's origin: its refs, single objects for work that has no copy on
|
|
252
|
+
* disk (drafts, reads of the published program), and whole histories moved
|
|
253
|
+
* to and from a repository on this machine as packs (ADR 0356).
|
|
253
254
|
*/
|
|
254
255
|
export interface OriginRepo {
|
|
255
256
|
readonly gitdir: string;
|
|
@@ -275,8 +276,16 @@ export interface OriginRepo {
|
|
|
275
276
|
deleteRef(input: { ref: string; expected?: string }): Promise<void>;
|
|
276
277
|
/** Whether the object is present locally. */
|
|
277
278
|
hasObject(sha: string): Promise<boolean>;
|
|
278
|
-
/**
|
|
279
|
-
|
|
279
|
+
/**
|
|
280
|
+
* Read the raw git object (returns { type, data }) into memory. One
|
|
281
|
+
* larger than `maxBytes` (64 MiB when left out) is refused with
|
|
282
|
+
* `ObjectTooLargeError`, before it is read wherever the origin can know
|
|
283
|
+
* its size first.
|
|
284
|
+
*/
|
|
285
|
+
readObject(
|
|
286
|
+
sha: string,
|
|
287
|
+
options?: { maxBytes?: number },
|
|
288
|
+
): Promise<{
|
|
280
289
|
type: "blob" | "tree" | "commit" | "tag";
|
|
281
290
|
data: Uint8Array;
|
|
282
291
|
}>;
|
|
@@ -286,4 +295,29 @@ export interface OriginRepo {
|
|
|
286
295
|
data: Uint8Array;
|
|
287
296
|
}): Promise<string>;
|
|
288
297
|
log(ref: string, maxCount?: number): Promise<CommitInfo[]>;
|
|
298
|
+
/**
|
|
299
|
+
* Take `commit` and everything it reaches from the repository at
|
|
300
|
+
* `repoPath` on this machine. Only what the origin lacks travels, in
|
|
301
|
+
* packs, with memory that does not grow with the history. `ref` names
|
|
302
|
+
* the ref about to move to `commit`, which helps an origin tell what it
|
|
303
|
+
* already holds. The ref itself is moved by {@link updateRef}, after.
|
|
304
|
+
*/
|
|
305
|
+
receive(input: {
|
|
306
|
+
repoPath: string;
|
|
307
|
+
commit: string;
|
|
308
|
+
ref?: string;
|
|
309
|
+
}): Promise<void>;
|
|
310
|
+
/**
|
|
311
|
+
* Make `commit` and everything it reaches readable in the repository at
|
|
312
|
+
* `repoPath` on this machine. With `borrow`, for a copy Work manages, an
|
|
313
|
+
* origin that keeps its objects on this machine lends them instead of
|
|
314
|
+
* copying them (Git alternates), so a copy of a large history costs only
|
|
315
|
+
* what the copy itself adds; otherwise what the repository lacks is
|
|
316
|
+
* copied, as a pack.
|
|
317
|
+
*/
|
|
318
|
+
send(input: {
|
|
319
|
+
repoPath: string;
|
|
320
|
+
commit: string;
|
|
321
|
+
borrow?: boolean;
|
|
322
|
+
}): Promise<void>;
|
|
289
323
|
}
|
|
@@ -2,7 +2,11 @@ import { spawn } 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
|
+
initManagedRepository,
|
|
7
|
+
nativeGit,
|
|
8
|
+
nativeGitEnvironment,
|
|
9
|
+
} from "./native-git.js";
|
|
6
10
|
|
|
7
11
|
/*
|
|
8
12
|
* A workspace's base as a Git remote (ADR 0353). A sandbox fetches the
|
|
@@ -60,12 +64,11 @@ export async function openCheckoutSource(opts: {
|
|
|
60
64
|
const directory = await fs.mkdtemp(path.join(tmpdir(), "work-checkout-"));
|
|
61
65
|
const dispose = () => fs.rm(directory, { recursive: true, force: true });
|
|
62
66
|
try {
|
|
63
|
-
await
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
]);
|
|
67
|
+
await initManagedRepository({
|
|
68
|
+
path: directory,
|
|
69
|
+
bare: true,
|
|
70
|
+
objectFormat: opts.commit.length === 64 ? "sha256" : "sha1",
|
|
71
|
+
});
|
|
69
72
|
await fs.writeFile(
|
|
70
73
|
path.join(directory, "objects", "info", "alternates"),
|
|
71
74
|
`${objects}\n`,
|
|
@@ -112,12 +115,16 @@ async function* uploadPack(opts: {
|
|
|
112
115
|
request?: AsyncIterable<Uint8Array>;
|
|
113
116
|
signal?: AbortSignal;
|
|
114
117
|
}): AsyncGenerator<Uint8Array> {
|
|
115
|
-
|
|
118
|
+
// Work's own environment for the source it made: nothing inherited, so
|
|
119
|
+
// no `GIT_PROTOCOL` either.
|
|
116
120
|
const child = spawn(
|
|
117
121
|
"git",
|
|
118
122
|
["upload-pack", "--stateless-rpc", ...opts.args, opts.directory],
|
|
119
123
|
{
|
|
120
|
-
env: {
|
|
124
|
+
env: await nativeGitEnvironment({
|
|
125
|
+
cwd: opts.directory,
|
|
126
|
+
managed: true,
|
|
127
|
+
}),
|
|
121
128
|
stdio: ["pipe", "pipe", "ignore"],
|
|
122
129
|
},
|
|
123
130
|
);
|
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,39 +342,92 @@ 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
|
|
@@ -307,12 +462,11 @@ export async function buildSeedPack(opts: {
|
|
|
307
462
|
// A repository of its own that borrows the copy's objects and ends
|
|
308
463
|
// history at the base, so the walk below stops there without a ref or
|
|
309
464
|
// a shallow file ever being written into the copy itself.
|
|
310
|
-
await
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
"
|
|
314
|
-
|
|
315
|
-
]);
|
|
465
|
+
await initManagedRepository({
|
|
466
|
+
path: directory,
|
|
467
|
+
bare: true,
|
|
468
|
+
objectFormat: opts.base.length === 64 ? "sha256" : "sha1",
|
|
469
|
+
});
|
|
316
470
|
await fs.writeFile(
|
|
317
471
|
path.join(directory, "objects", "info", "alternates"),
|
|
318
472
|
`${objects}\n`,
|
|
@@ -428,26 +582,13 @@ function assertPinRef(ref: string): void {
|
|
|
428
582
|
}
|
|
429
583
|
}
|
|
430
584
|
|
|
431
|
-
function gitErrorText(error: unknown): string {
|
|
432
|
-
const stderr =
|
|
433
|
-
error && typeof error === "object" && "stderr" in error
|
|
434
|
-
? String(error.stderr)
|
|
435
|
-
: "";
|
|
436
|
-
const text = (stderr || (error instanceof Error ? error.message : ""))
|
|
437
|
-
.split("\n")
|
|
438
|
-
.map((line) => line.trim())
|
|
439
|
-
.filter((line) => line.length > 0 && !/authorization/i.test(line))
|
|
440
|
-
.slice(-2)
|
|
441
|
-
.join(" ");
|
|
442
|
-
return text || "the remote refused the request";
|
|
443
|
-
}
|
|
444
|
-
|
|
445
585
|
/** Run Git and collect its binary standard output (a pack may be large). */
|
|
446
|
-
function gitOutput(opts: {
|
|
586
|
+
async function gitOutput(opts: {
|
|
447
587
|
cwd: string;
|
|
448
588
|
args: readonly string[];
|
|
449
589
|
input?: string;
|
|
450
590
|
}): Promise<Uint8Array> {
|
|
591
|
+
const env = await nativeGitEnvironment({ cwd: opts.cwd });
|
|
451
592
|
return new Promise((resolve, reject) => {
|
|
452
593
|
const child = execFile(
|
|
453
594
|
"git",
|
|
@@ -456,7 +597,7 @@ function gitOutput(opts: {
|
|
|
456
597
|
encoding: "buffer",
|
|
457
598
|
maxBuffer: 2 * 1024 * 1024 * 1024,
|
|
458
599
|
timeout: 15 * 60_000,
|
|
459
|
-
env
|
|
600
|
+
env,
|
|
460
601
|
},
|
|
461
602
|
(error, stdout) => {
|
|
462
603
|
if (error) reject(error);
|