@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.
Files changed (66) hide show
  1. package/dist/checkout-remote-backend.d.ts.map +1 -1
  2. package/dist/fs-backend.d.ts +8 -0
  3. package/dist/fs-backend.d.ts.map +1 -1
  4. package/dist/fs-remote-backend.d.ts +43 -9
  5. package/dist/fs-remote-backend.d.ts.map +1 -1
  6. package/dist/git-objects.d.ts +77 -0
  7. package/dist/git-objects.d.ts.map +1 -0
  8. package/dist/git-sync.d.ts +16 -17
  9. package/dist/git-sync.d.ts.map +1 -1
  10. package/dist/index.d.ts +6 -5
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +3912 -3059
  13. package/dist/index.js.map +23 -21
  14. package/dist/native-git.d.ts +114 -2
  15. package/dist/native-git.d.ts.map +1 -1
  16. package/dist/native-project-repo.d.ts +40 -5
  17. package/dist/native-project-repo.d.ts.map +1 -1
  18. package/dist/network-sync.d.ts.map +1 -1
  19. package/dist/network.d.ts +14 -9
  20. package/dist/network.d.ts.map +1 -1
  21. package/dist/object-origin-cache.d.ts +86 -0
  22. package/dist/object-origin-cache.d.ts.map +1 -0
  23. package/dist/object-remote-backend.d.ts +64 -10
  24. package/dist/object-remote-backend.d.ts.map +1 -1
  25. package/dist/origin-draft.d.ts.map +1 -1
  26. package/dist/origin-objects.d.ts +7 -2
  27. package/dist/origin-objects.d.ts.map +1 -1
  28. package/dist/project-manager.d.ts +22 -0
  29. package/dist/project-manager.d.ts.map +1 -1
  30. package/dist/project-paths.d.ts +20 -0
  31. package/dist/project-paths.d.ts.map +1 -0
  32. package/dist/ref-names.d.ts +35 -0
  33. package/dist/ref-names.d.ts.map +1 -1
  34. package/dist/types.d.ts +37 -4
  35. package/dist/types.d.ts.map +1 -1
  36. package/dist/workspace-checkout.d.ts +31 -0
  37. package/dist/workspace-checkout.d.ts.map +1 -0
  38. package/dist/workspace-mirror.d.ts +38 -5
  39. package/dist/workspace-mirror.d.ts.map +1 -1
  40. package/dist/workspace-save.d.ts +27 -2
  41. package/dist/workspace-save.d.ts.map +1 -1
  42. package/package.json +4 -4
  43. package/src/checkout-remote-backend.ts +77 -29
  44. package/src/fs-backend.ts +69 -7
  45. package/src/fs-remote-backend.ts +255 -133
  46. package/src/git-objects.ts +363 -0
  47. package/src/git-sync.ts +124 -453
  48. package/src/index.ts +17 -7
  49. package/src/native-git.ts +566 -25
  50. package/src/native-project-repo.ts +318 -99
  51. package/src/network-sync.ts +43 -35
  52. package/src/network.ts +177 -153
  53. package/src/object-origin-cache.ts +689 -0
  54. package/src/object-remote-backend.ts +277 -20
  55. package/src/origin-draft.ts +22 -15
  56. package/src/origin-objects.ts +21 -5
  57. package/src/project-manager.ts +261 -73
  58. package/src/project-paths.ts +142 -0
  59. package/src/ref-names.ts +56 -0
  60. package/src/types.ts +38 -4
  61. package/src/workspace-checkout.ts +192 -0
  62. package/src/workspace-mirror.ts +276 -68
  63. package/src/workspace-save.ts +99 -30
  64. package/dist/project-repo.d.ts +0 -81
  65. package/dist/project-repo.d.ts.map +0 -1
  66. package/src/project-repo.ts +0 -821
package/src/fs-backend.ts CHANGED
@@ -1,7 +1,11 @@
1
- import nodeFs from "node:fs";
2
1
  import fs from "node:fs/promises";
3
2
  import path from "node:path";
4
- import git from "isomorphic-git";
3
+ import { closeObjectReaders } from "./git-objects.js";
4
+ import {
5
+ initManagedRepository,
6
+ markManagedRepository,
7
+ nativeGit,
8
+ } from "./native-git.js";
5
9
  import { ensurePersonalFilesExcluded } from "./personal-files.js";
6
10
  import type {
7
11
  InitProjectOptions,
@@ -85,6 +89,14 @@ export class FsBackend implements StorageBackend {
85
89
  throw new Error(`Project not found: ${projectId}`);
86
90
  }
87
91
  await ensurePersonalFilesExcluded({ repoPath: projectPath });
92
+ await this.excludeOwnStorage({ tenantId, projectId, projectPath });
93
+ // A copy in Work's own storage runs Git with Work's environment
94
+ // (ADR 0356); one made before copies were marked is marked now.
95
+ if (
96
+ projectPath ===
97
+ this.resolveInternalPath(tenantId, projectId, externalUserId)
98
+ )
99
+ await markManagedRepository(projectPath);
88
100
  return {
89
101
  repoPath: projectPath,
90
102
  release: async () => {},
@@ -112,12 +124,62 @@ export class FsBackend implements StorageBackend {
112
124
  () => false,
113
125
  );
114
126
  if (!hasRepo) {
115
- await git.init({ fs: nodeFs, dir: projectPath, defaultBranch: "main" });
127
+ // A folder a person chose keeps Git's own defaults; a copy Work
128
+ // manages is never repacked behind a borrower's back.
129
+ if (
130
+ opts?.rootPath ||
131
+ projectPath !==
132
+ this.resolveInternalPath(tenantId, projectId, opts?.externalUserId)
133
+ )
134
+ await nativeGit(projectPath, [
135
+ "init",
136
+ "--quiet",
137
+ "--initial-branch=main",
138
+ ]);
139
+ else await initManagedRepository({ path: projectPath });
116
140
  }
117
141
  await ensurePersonalFilesExcluded({ repoPath: projectPath });
142
+ await this.excludeOwnStorage({ tenantId, projectId, projectPath });
143
+ if (
144
+ projectPath ===
145
+ this.resolveInternalPath(tenantId, projectId, opts?.externalUserId)
146
+ )
147
+ await markManagedRepository(projectPath);
118
148
  return projectPath;
119
149
  }
120
150
 
151
+ /**
152
+ * In the internal layout the project's own copy shares its folder with
153
+ * Work's storage for it: members' and chats' copies under `dev/`, caches
154
+ * under `cache/`. Git must never read those as the project's files (a
155
+ * copy being made is a repository with no commit yet, which stops every
156
+ * `git add`), so the project's copy excludes them.
157
+ */
158
+ private async excludeOwnStorage(input: {
159
+ tenantId: string;
160
+ projectId: string;
161
+ projectPath: string;
162
+ }): Promise<void> {
163
+ if (
164
+ input.projectPath !==
165
+ this.resolveInternalPath(input.tenantId, input.projectId)
166
+ )
167
+ return;
168
+ const excludePath = path.join(input.projectPath, ".git", "info", "exclude");
169
+ const current = await fs.readFile(excludePath, "utf8").catch(() => null);
170
+ // No Git here yet: nothing reads the folder as a repository.
171
+ if (current === null) return;
172
+ const lines = current.split(/\r?\n/);
173
+ const missing = ["/dev/", "/cache/"].filter(
174
+ (rule) => !lines.includes(rule),
175
+ );
176
+ if (missing.length === 0) return;
177
+ await fs.appendFile(
178
+ excludePath,
179
+ `${current && !current.endsWith("\n") ? "\n" : ""}${missing.join("\n")}\n`,
180
+ );
181
+ }
182
+
121
183
  mirrorPath(tenantId: string, projectId: string): string {
122
184
  assertUuid(tenantId);
123
185
  assertUuid(projectId);
@@ -140,6 +202,7 @@ export class FsBackend implements StorageBackend {
140
202
  // Only internal storage is removed. Explicitly-rooted folders belong to
141
203
  // the user; the host decides separately whether to trash them.
142
204
  const projectRoot = path.join(this.basePath, tenantId, projectId);
205
+ closeObjectReaders(projectRoot);
143
206
  await fs.rm(projectRoot, { recursive: true, force: true });
144
207
  await fs.rm(this.mirrorPath(tenantId, projectId), {
145
208
  recursive: true,
@@ -159,10 +222,9 @@ export class FsBackend implements StorageBackend {
159
222
  this.initializedRoots.has(`${tenantId}:${projectId}`)
160
223
  )
161
224
  return;
162
- await fs.rm(this.resolveInternalPath(tenantId, projectId, externalUserId), {
163
- recursive: true,
164
- force: true,
165
- });
225
+ const copy = this.resolveInternalPath(tenantId, projectId, externalUserId);
226
+ closeObjectReaders(copy);
227
+ await fs.rm(copy, { recursive: true, force: true });
166
228
  }
167
229
 
168
230
  async exists(
@@ -1,11 +1,34 @@
1
- import nodeFs from "node:fs";
2
1
  import fs from "node:fs/promises";
3
2
  import path from "node:path";
4
- import git from "isomorphic-git";
3
+ import { getTracer, withSpan } from "@catamorphic/otel";
4
+ import type { GitObjectType } from "./git-object-codec.js";
5
+ import {
6
+ closeObjectReaders,
7
+ objectContent,
8
+ objectHeader,
9
+ writeLooseObject,
10
+ } from "./git-objects.js";
11
+ import {
12
+ borrowObjects,
13
+ gitErrorText,
14
+ initManagedRepository,
15
+ markManagedRepository,
16
+ nativeGit,
17
+ TRANSFER_TIMEOUT_MS,
18
+ } from "./native-git.js";
19
+ import { nativeLog } from "./native-project-repo.js";
5
20
  import { RefMovedError } from "./ref-moved-error.js";
6
- import { assertResolvableRef, assertValidRefName } from "./ref-names.js";
21
+ import {
22
+ assertNotOption,
23
+ assertObjectId,
24
+ assertResolvableRef,
25
+ assertValidRefName,
26
+ isObjectId,
27
+ } from "./ref-names.js";
7
28
  import type { CommitInfo, OriginRepo, RemoteBackend } from "./types.js";
8
29
 
30
+ const tracer = getTracer("@catamorphic/git");
31
+
9
32
  const UUID_RE =
10
33
  /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
11
34
 
@@ -15,10 +38,12 @@ function assertUuid(value: string): void {
15
38
  }
16
39
  }
17
40
 
41
+ const NO_COMMIT = "0000000000000000000000000000000000000000";
42
+
18
43
  /**
19
- * Ref updates in flight per ref. A bare repository on local disk serves one
20
- * process (a single-machine host), so serializing its compare-and-swap in
21
- * that process makes it atomic; several processes need an object origin.
44
+ * Ref updates in flight per ref, in this process. Git's own lock makes a
45
+ * compare-and-swap atomic across processes; this only keeps one process
46
+ * from contending with itself for that lock.
22
47
  */
23
48
  const refLocks = new Map<string, Promise<unknown>>();
24
49
 
@@ -48,17 +73,20 @@ export class FsRemoteBackend implements RemoteBackend {
48
73
 
49
74
  async initRemote(tenantId: string, projectId: string): Promise<void> {
50
75
  const barePath = this.resolveBarePath(tenantId, projectId);
51
- await fs.mkdir(barePath, { recursive: true });
52
- await git.init({
53
- fs: nodeFs,
54
- dir: barePath,
55
- bare: true,
56
- defaultBranch: "main",
57
- });
76
+ if (
77
+ await fs.access(path.join(barePath, "HEAD")).then(
78
+ () => true,
79
+ () => false,
80
+ )
81
+ )
82
+ return;
83
+ closeObjectReaders(barePath);
84
+ await initManagedRepository({ path: barePath, bare: true });
58
85
  }
59
86
 
60
87
  async deleteRemote(tenantId: string, projectId: string): Promise<void> {
61
88
  const barePath = this.resolveBarePath(tenantId, projectId);
89
+ closeObjectReaders(barePath);
62
90
  await fs.rm(barePath, { recursive: true, force: true });
63
91
  }
64
92
 
@@ -78,175 +106,269 @@ export class FsRemoteBackend implements RemoteBackend {
78
106
  fn: (origin: OriginRepo) => Promise<T>,
79
107
  ): Promise<T> {
80
108
  const barePath = this.resolveBarePath(tenantId, projectId);
109
+ // An origin made before Work marked its repositories is marked now.
110
+ await markManagedRepository(barePath);
81
111
  const origin = new FsOriginRepo(barePath);
82
112
  return fn(origin);
83
113
  }
84
114
  }
85
115
 
86
116
  /**
87
- * OriginRepo over a local bare repo directory. Used directly by
88
- * `FsRemoteBackend` and reused by network-remote backends (e.g. Cloudflare
89
- * Artifacts) that maintain a local bare mirror.
117
+ * OriginRepo over a bare repository on this machine's disk, read and
118
+ * written with the Git program. Used directly by `FsRemoteBackend` and
119
+ * reused by network-remote backends (e.g. Cloudflare Artifacts) that
120
+ * maintain a local bare mirror.
90
121
  */
91
122
  export class FsOriginRepo implements OriginRepo {
92
- private readonly cache = {};
93
123
  constructor(readonly gitdir: string) {}
94
124
 
125
+ /**
126
+ * The id a ref names now, or null when there is no such ref. Asked of the
127
+ * repository's long-lived reader, so a lookup starts no Git: every
128
+ * workflow invocation makes several (which draft, which `main`), and a
129
+ * process for each cost a batch four starts an item. The reader resolves
130
+ * the name when it is asked, as `git rev-parse` would, so it sees a ref
131
+ * that moved, was packed, was deleted or was made after it started.
132
+ *
133
+ * Git answers by itself, as before, where the reader cannot say the
134
+ * same: a full id (named back whether or not its object is here), and a
135
+ * lookup the reader failed (it was closed, or its Git stopped). What the
136
+ * reader calls missing is a ref that is not there: Git writes no ref to
137
+ * an object the repository lacks.
138
+ */
95
139
  async resolveRef(ref: string): Promise<string | null> {
96
140
  assertResolvableRef(ref);
97
- try {
98
- return await git.resolveRef({ fs: nodeFs, gitdir: this.gitdir, ref });
99
- } catch {
100
- return null;
141
+ if (!isObjectId(ref)) {
142
+ const asked = await objectHeader({ gitdir: this.gitdir, name: ref }).then(
143
+ (answer) => ({ oid: answer?.oid ?? null }),
144
+ () => null,
145
+ );
146
+ if (asked) return asked.oid;
101
147
  }
148
+ return nativeGit(this.gitdir, [
149
+ "rev-parse",
150
+ "--verify",
151
+ "--quiet",
152
+ "--end-of-options",
153
+ ref,
154
+ ]).then(
155
+ (output) => output.trim() || null,
156
+ () => null,
157
+ );
102
158
  }
103
159
 
104
160
  async listRefs(prefix: string): Promise<{ ref: string; sha: string }[]> {
105
161
  const normalized = prefix.endsWith("/") ? prefix : `${prefix}/`;
106
- const stripped = normalized.replace(/\/$/, "");
107
- const refs = await git.listBranches({
108
- fs: nodeFs,
109
- gitdir: this.gitdir,
110
- });
111
- const prefixMatch = stripped === "refs/heads";
112
- if (!prefixMatch) return [];
113
- const entries = await Promise.all(
114
- refs.map(async (name) => ({
115
- ref: `refs/heads/${name}`,
116
- sha: await git.resolveRef({
117
- fs: nodeFs,
118
- gitdir: this.gitdir,
119
- ref: `refs/heads/${name}`,
120
- }),
121
- })),
122
- );
123
- return entries;
162
+ assertValidRefName(normalized.slice(0, -1));
163
+ const output = await nativeGit(this.gitdir, [
164
+ "for-each-ref",
165
+ "--format=%(refname)%09%(objectname)",
166
+ "--end-of-options",
167
+ normalized,
168
+ ]);
169
+ return output
170
+ .split("\n")
171
+ .filter(Boolean)
172
+ .flatMap((line) => {
173
+ const [ref, sha] = line.split("\t");
174
+ return ref && sha ? [{ ref, sha }] : [];
175
+ });
124
176
  }
125
177
 
126
178
  async updateRef(opts: {
127
179
  ref: string;
128
180
  sha: string;
129
181
  expected?: string | null;
130
- }): Promise<void> {
131
- await withRefLock(`${this.gitdir}\0${opts.ref}`, () =>
132
- this.writeRefChecked(opts),
133
- );
134
- }
135
-
136
- private async writeRefChecked(opts: {
137
- ref: string;
138
- sha: string;
139
- expected?: string | null;
140
182
  }): Promise<void> {
141
183
  assertValidRefName(opts.ref);
142
- if (opts.expected !== undefined) {
143
- const current = await this.resolveRef(opts.ref);
144
- if (current !== opts.expected) {
145
- throw new RefMovedError({
146
- ref: opts.ref,
147
- expected: opts.expected,
148
- actual: current,
149
- });
150
- }
151
- }
152
- await git.writeRef({
153
- fs: nodeFs,
154
- gitdir: this.gitdir,
155
- ref: opts.ref,
156
- value: opts.sha,
157
- force: true,
184
+ assertObjectId(opts.sha);
185
+ if (opts.expected) assertObjectId(opts.expected);
186
+ await withRefLock(`${this.gitdir}\0${opts.ref}`, async () => {
187
+ await nativeGit(this.gitdir, [
188
+ "update-ref",
189
+ "--end-of-options",
190
+ opts.ref,
191
+ opts.sha,
192
+ ...(opts.expected !== undefined ? [opts.expected ?? NO_COMMIT] : []),
193
+ ]).catch(async (error: unknown) => {
194
+ // Git refuses a swap whose expected value no longer holds.
195
+ if (opts.expected !== undefined) {
196
+ const actual = await this.resolveRef(opts.ref);
197
+ if (actual !== opts.expected)
198
+ throw new RefMovedError({
199
+ ref: opts.ref,
200
+ expected: opts.expected,
201
+ actual,
202
+ });
203
+ }
204
+ throw new Error(
205
+ `Could not update ${opts.ref} in the project's origin: ${gitErrorText(error)}`,
206
+ );
207
+ });
158
208
  });
159
209
  }
160
210
 
161
211
  async deleteRef(input: { ref: string; expected?: string }): Promise<void> {
162
- await withRefLock(`${this.gitdir}\0${input.ref}`, () =>
163
- this.deleteRefChecked(input),
164
- );
165
- }
166
-
167
- private async deleteRefChecked(input: {
168
- ref: string;
169
- expected?: string;
170
- }): Promise<void> {
171
212
  assertValidRefName(input.ref);
172
- if (input.expected !== undefined) {
173
- const current = await this.resolveRef(input.ref);
174
- if (current !== input.expected)
175
- throw new RefMovedError({
176
- ref: input.ref,
177
- expected: input.expected,
178
- actual: current,
179
- });
180
- }
181
- await git.deleteRef({ fs: nodeFs, gitdir: this.gitdir, ref: input.ref });
213
+ if (input.expected !== undefined) assertObjectId(input.expected);
214
+ await withRefLock(`${this.gitdir}\0${input.ref}`, async () => {
215
+ await nativeGit(this.gitdir, [
216
+ "update-ref",
217
+ "-d",
218
+ "--end-of-options",
219
+ input.ref,
220
+ ...(input.expected !== undefined ? [input.expected] : []),
221
+ ]).catch(async (error: unknown) => {
222
+ if (input.expected !== undefined) {
223
+ const actual = await this.resolveRef(input.ref);
224
+ if (actual !== input.expected)
225
+ throw new RefMovedError({
226
+ ref: input.ref,
227
+ expected: input.expected,
228
+ actual,
229
+ });
230
+ }
231
+ throw new Error(
232
+ `Could not delete ${input.ref} in the project's origin: ${gitErrorText(error)}`,
233
+ );
234
+ });
235
+ });
182
236
  }
183
237
 
184
238
  async hasObject(sha: string): Promise<boolean> {
185
- try {
186
- await git.readObject({
187
- fs: nodeFs,
188
- gitdir: this.gitdir,
189
- oid: sha,
190
- cache: this.cache,
191
- });
192
- return true;
193
- } catch {
194
- return false;
195
- }
239
+ // An id only: the reader would resolve a ref name or `rev:path` too.
240
+ if (!isObjectId(sha)) return false;
241
+ return (await objectHeader({ gitdir: this.gitdir, name: sha })) !== null;
196
242
  }
197
243
 
198
- async readObject(sha: string): Promise<{
199
- type: "blob" | "tree" | "commit" | "tag";
244
+ async readObject(
245
+ sha: string,
246
+ options?: { maxBytes?: number },
247
+ ): Promise<{
248
+ type: GitObjectType;
200
249
  data: Uint8Array;
201
250
  }> {
202
- const obj = await git.readObject({
203
- fs: nodeFs,
251
+ assertObjectId(sha);
252
+ const object = await objectContent({
204
253
  gitdir: this.gitdir,
205
- oid: sha,
206
- format: "content",
207
- cache: this.cache,
254
+ name: sha,
255
+ ...(options?.maxBytes !== undefined
256
+ ? { maxBytes: options.maxBytes }
257
+ : {}),
208
258
  });
209
- if (obj.type === "deflated" || obj.type === "wrapped") {
210
- throw new Error(`Unexpected object format for ${sha}: ${obj.type}`);
211
- }
212
- return {
213
- type: obj.type as "blob" | "tree" | "commit" | "tag",
214
- data: obj.object as Uint8Array,
215
- };
259
+ if (!object) throw new Error(`Object not found: ${sha}`);
260
+ return { type: object.type, data: object.data };
216
261
  }
217
262
 
218
263
  async writeObject(opts: {
219
- type: "blob" | "tree" | "commit" | "tag";
264
+ type: GitObjectType;
220
265
  data: Uint8Array;
221
266
  }): Promise<string> {
222
- return git.writeObject({
223
- fs: nodeFs,
224
- gitdir: this.gitdir,
267
+ return writeLooseObject({
268
+ objects: path.join(this.gitdir, "objects"),
225
269
  type: opts.type,
226
- object: opts.data,
227
- format: "content",
270
+ data: opts.data,
228
271
  });
229
272
  }
230
273
 
231
274
  async log(ref: string, maxCount = 50): Promise<CommitInfo[]> {
232
- try {
233
- const commits = await git.log({
234
- fs: nodeFs,
235
- gitdir: this.gitdir,
236
- ref,
237
- depth: maxCount,
238
- });
239
- return commits.map((c) => ({
240
- sha: c.oid,
241
- message: c.commit.message,
242
- author: {
243
- name: c.commit.author.name,
244
- email: c.commit.author.email,
275
+ assertResolvableRef(ref);
276
+ return nativeLog({ repoPath: this.gitdir, ref, maxCount });
277
+ }
278
+
279
+ /**
280
+ * Take `commit` and what it reaches from the repository at `repoPath`.
281
+ * Git negotiates against every ref this origin holds, so only what it
282
+ * lacks travels, as one pack.
283
+ */
284
+ async receive(input: { repoPath: string; commit: string }): Promise<void> {
285
+ assertObjectId(input.commit);
286
+ assertNotOption(input.repoPath);
287
+ await withSpan(
288
+ {
289
+ tracer,
290
+ name: "git.origin.receive",
291
+ attributes: { "catamorphic.git.origin": "filesystem" },
292
+ },
293
+ async () => {
294
+ await nativeGit(
295
+ this.gitdir,
296
+ [
297
+ // Version 2 serves a commit by its id; nothing is repacked
298
+ // behind the copies that borrow from this origin.
299
+ "-c",
300
+ "protocol.version=2",
301
+ "-c",
302
+ "gc.auto=0",
303
+ "fetch",
304
+ "--quiet",
305
+ "--no-tags",
306
+ "--no-write-fetch-head",
307
+ "--end-of-options",
308
+ input.repoPath,
309
+ input.commit,
310
+ ],
311
+ undefined,
312
+ { timeoutMs: TRANSFER_TIMEOUT_MS },
313
+ ).catch((error: unknown) => {
314
+ throw new Error(
315
+ `Could not copy ${input.commit.slice(0, 12)} into the project's origin: ${gitErrorText(error)}`,
316
+ );
317
+ });
318
+ },
319
+ );
320
+ }
321
+
322
+ /**
323
+ * Make `commit` and what it reaches readable in the repository at
324
+ * `repoPath`: by lending it this origin's objects (`borrow`, for a copy
325
+ * Work manages on this machine), or by copying what it lacks as a pack.
326
+ */
327
+ async send(input: {
328
+ repoPath: string;
329
+ commit: string;
330
+ borrow?: boolean;
331
+ }): Promise<void> {
332
+ assertObjectId(input.commit);
333
+ assertNotOption(input.repoPath);
334
+ await withSpan(
335
+ {
336
+ tracer,
337
+ name: "git.origin.send",
338
+ attributes: {
339
+ "catamorphic.git.origin": "filesystem",
340
+ "catamorphic.git.borrow": input.borrow ?? false,
245
341
  },
246
- timestamp: c.commit.author.timestamp,
247
- }));
248
- } catch {
249
- return [];
250
- }
342
+ },
343
+ async () => {
344
+ if (input.borrow) {
345
+ await borrowObjects({
346
+ repoPath: input.repoPath,
347
+ objects: path.join(await fs.realpath(this.gitdir), "objects"),
348
+ });
349
+ return;
350
+ }
351
+ await nativeGit(
352
+ input.repoPath,
353
+ [
354
+ "-c",
355
+ "protocol.version=2",
356
+ "fetch",
357
+ "--quiet",
358
+ "--no-tags",
359
+ "--no-write-fetch-head",
360
+ "--end-of-options",
361
+ this.gitdir,
362
+ input.commit,
363
+ ],
364
+ undefined,
365
+ { timeoutMs: TRANSFER_TIMEOUT_MS },
366
+ ).catch((error: unknown) => {
367
+ throw new Error(
368
+ `Could not copy ${input.commit.slice(0, 12)} from the project's origin: ${gitErrorText(error)}`,
369
+ );
370
+ });
371
+ },
372
+ );
251
373
  }
252
374
  }