@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/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
|
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
initManagedRepository,
|
|
7
|
+
nativeGit,
|
|
8
|
+
nativeGitEnvironment,
|
|
9
|
+
} from "./native-git.js";
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* A workspace's base as a Git remote (ADR 0353). A sandbox fetches the
|
|
13
|
+
* commit it stands on with Git's own protocol instead of being handed a
|
|
14
|
+
* pack: the transfer streams, a receiver that already holds some of it
|
|
15
|
+
* (an image, a cache, an earlier base) is sent the rest, and how much
|
|
16
|
+
* arrives (shallow, without blobs, a sparse cone) is the fetching
|
|
17
|
+
* command's to say.
|
|
18
|
+
*
|
|
19
|
+
* The remote is a repository of its own, made for one request and thrown
|
|
20
|
+
* away: it borrows the objects of the copy that holds the commit, ends
|
|
21
|
+
* history at that commit, and advertises one branch at it. Over smart HTTP
|
|
22
|
+
* `git upload-pack` serves a want only when it is reachable from what is
|
|
23
|
+
* advertised, so the commit, its tree and its files can be fetched and
|
|
24
|
+
* nothing else the copy holds, whatever object id a caller names.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
const COMMIT = /^[0-9a-f]{40}$|^[0-9a-f]{64}$/;
|
|
28
|
+
|
|
29
|
+
/** The one branch a checkout source advertises, at its commit. */
|
|
30
|
+
export const CHECKOUT_BRANCH = "base";
|
|
31
|
+
|
|
32
|
+
/** How long one `git upload-pack` may run. */
|
|
33
|
+
const UPLOAD_PACK_TIMEOUT_MS = 30 * 60_000;
|
|
34
|
+
|
|
35
|
+
/** A repository that serves one commit of another, and nothing else. */
|
|
36
|
+
export interface CheckoutSource {
|
|
37
|
+
/** The bare repository `git upload-pack` serves. */
|
|
38
|
+
directory: string;
|
|
39
|
+
/** Remove it. Safe to call again. */
|
|
40
|
+
dispose(): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Make a repository that serves `commit` of the repository at `repoPath`:
|
|
45
|
+
* the commit with its tree, shallow there, on the branch
|
|
46
|
+
* {@link CHECKOUT_BRANCH}. Nothing is written into `repoPath`.
|
|
47
|
+
*/
|
|
48
|
+
export async function openCheckoutSource(opts: {
|
|
49
|
+
repoPath: string;
|
|
50
|
+
commit: string;
|
|
51
|
+
}): Promise<CheckoutSource> {
|
|
52
|
+
if (!COMMIT.test(opts.commit))
|
|
53
|
+
throw new Error("A checkout source names a full commit id");
|
|
54
|
+
const objects = path.resolve(
|
|
55
|
+
opts.repoPath,
|
|
56
|
+
(
|
|
57
|
+
await nativeGit(opts.repoPath, ["rev-parse", "--git-path", "objects"])
|
|
58
|
+
).trim(),
|
|
59
|
+
);
|
|
60
|
+
if (/[\n"]/.test(objects))
|
|
61
|
+
throw new Error("The repository's path cannot be served from");
|
|
62
|
+
// The commit must be there whole before anything is advertised.
|
|
63
|
+
await nativeGit(opts.repoPath, ["cat-file", "-e", `${opts.commit}^{commit}`]);
|
|
64
|
+
const directory = await fs.mkdtemp(path.join(tmpdir(), "work-checkout-"));
|
|
65
|
+
const dispose = () => fs.rm(directory, { recursive: true, force: true });
|
|
66
|
+
try {
|
|
67
|
+
await initManagedRepository({
|
|
68
|
+
path: directory,
|
|
69
|
+
bare: true,
|
|
70
|
+
objectFormat: opts.commit.length === 64 ? "sha256" : "sha1",
|
|
71
|
+
});
|
|
72
|
+
await fs.writeFile(
|
|
73
|
+
path.join(directory, "objects", "info", "alternates"),
|
|
74
|
+
`${objects}\n`,
|
|
75
|
+
);
|
|
76
|
+
// History ends here: nothing before the commit is reachable, so none
|
|
77
|
+
// of it is served.
|
|
78
|
+
await fs.writeFile(path.join(directory, "shallow"), `${opts.commit}\n`);
|
|
79
|
+
await nativeGit(directory, [
|
|
80
|
+
"update-ref",
|
|
81
|
+
`refs/heads/${CHECKOUT_BRANCH}`,
|
|
82
|
+
opts.commit,
|
|
83
|
+
]);
|
|
84
|
+
await nativeGit(directory, [
|
|
85
|
+
"symbolic-ref",
|
|
86
|
+
"HEAD",
|
|
87
|
+
`refs/heads/${CHECKOUT_BRANCH}`,
|
|
88
|
+
]);
|
|
89
|
+
// A fetching command may leave blobs or trees out (`--filter`), and
|
|
90
|
+
// ask for one later by its id: served when it is reachable from the
|
|
91
|
+
// branch, which is all that "reachable" can mean in a repository
|
|
92
|
+
// whose history ends at it.
|
|
93
|
+
await nativeGit(directory, ["config", "uploadpack.allowFilter", "true"]);
|
|
94
|
+
await nativeGit(directory, [
|
|
95
|
+
"config",
|
|
96
|
+
"uploadpack.allowReachableSHA1InWant",
|
|
97
|
+
"true",
|
|
98
|
+
]);
|
|
99
|
+
return { directory, dispose };
|
|
100
|
+
} catch (error) {
|
|
101
|
+
await dispose();
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Run `git upload-pack` on a checkout source and stream what it writes.
|
|
108
|
+
* Protocol version 0 always: there the server checks that every want is
|
|
109
|
+
* reachable from what it advertised, which version 2 does not. A request
|
|
110
|
+
* the server refuses ends with Git's own error line for the client.
|
|
111
|
+
*/
|
|
112
|
+
async function* uploadPack(opts: {
|
|
113
|
+
directory: string;
|
|
114
|
+
args: readonly string[];
|
|
115
|
+
request?: AsyncIterable<Uint8Array>;
|
|
116
|
+
signal?: AbortSignal;
|
|
117
|
+
}): AsyncGenerator<Uint8Array> {
|
|
118
|
+
// Work's own environment for the source it made: nothing inherited, so
|
|
119
|
+
// no `GIT_PROTOCOL` either.
|
|
120
|
+
const child = spawn(
|
|
121
|
+
"git",
|
|
122
|
+
["upload-pack", "--stateless-rpc", ...opts.args, opts.directory],
|
|
123
|
+
{
|
|
124
|
+
env: await nativeGitEnvironment({
|
|
125
|
+
cwd: opts.directory,
|
|
126
|
+
managed: true,
|
|
127
|
+
}),
|
|
128
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
const stop = () => {
|
|
132
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
133
|
+
child.kill("SIGKILL");
|
|
134
|
+
};
|
|
135
|
+
const timer = setTimeout(stop, UPLOAD_PACK_TIMEOUT_MS);
|
|
136
|
+
timer.unref?.();
|
|
137
|
+
opts.signal?.addEventListener("abort", stop, { once: true });
|
|
138
|
+
const spawned = new Promise<void>((resolve, reject) => {
|
|
139
|
+
child.once("spawn", () => resolve());
|
|
140
|
+
child.once("error", reject);
|
|
141
|
+
});
|
|
142
|
+
// The request is written beside the read: a large negotiation fills
|
|
143
|
+
// both pipes at once.
|
|
144
|
+
const written = (async () => {
|
|
145
|
+
try {
|
|
146
|
+
if (opts.request)
|
|
147
|
+
for await (const chunk of opts.request) {
|
|
148
|
+
if (!child.stdin.write(chunk))
|
|
149
|
+
await new Promise<void>((resolve) =>
|
|
150
|
+
child.stdin.once("drain", () => resolve()),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
} catch {
|
|
154
|
+
// A request that ends early ends the answer: Git says why.
|
|
155
|
+
} finally {
|
|
156
|
+
child.stdin.end();
|
|
157
|
+
}
|
|
158
|
+
})();
|
|
159
|
+
child.stdin.on("error", () => {});
|
|
160
|
+
try {
|
|
161
|
+
await spawned;
|
|
162
|
+
for await (const chunk of child.stdout)
|
|
163
|
+
if (chunk instanceof Uint8Array) yield chunk;
|
|
164
|
+
await written;
|
|
165
|
+
} finally {
|
|
166
|
+
clearTimeout(timer);
|
|
167
|
+
opts.signal?.removeEventListener("abort", stop);
|
|
168
|
+
stop();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** What a smart HTTP `info/refs?service=git-upload-pack` answers with. */
|
|
173
|
+
export async function* advertiseCheckout(opts: {
|
|
174
|
+
directory: string;
|
|
175
|
+
signal?: AbortSignal;
|
|
176
|
+
}): AsyncGenerator<Uint8Array> {
|
|
177
|
+
const service = "# service=git-upload-pack\n";
|
|
178
|
+
yield new TextEncoder().encode(
|
|
179
|
+
`${(service.length + 4).toString(16).padStart(4, "0")}${service}0000`,
|
|
180
|
+
);
|
|
181
|
+
yield* uploadPack({ ...opts, args: ["--advertise-refs"] });
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** What a smart HTTP `git-upload-pack` request answers with. */
|
|
185
|
+
export function serveCheckout(opts: {
|
|
186
|
+
directory: string;
|
|
187
|
+
/** The request's body, already decoded when it was compressed. */
|
|
188
|
+
request: AsyncIterable<Uint8Array>;
|
|
189
|
+
signal?: AbortSignal;
|
|
190
|
+
}): AsyncGenerator<Uint8Array> {
|
|
191
|
+
return uploadPack({ ...opts, args: [] });
|
|
192
|
+
}
|