@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/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
|
|
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
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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(
|
package/src/fs-remote-backend.ts
CHANGED
|
@@ -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
|
|
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 {
|
|
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
|
|
20
|
-
*
|
|
21
|
-
*
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
|
88
|
-
*
|
|
89
|
-
*
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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(
|
|
199
|
-
|
|
244
|
+
async readObject(
|
|
245
|
+
sha: string,
|
|
246
|
+
options?: { maxBytes?: number },
|
|
247
|
+
): Promise<{
|
|
248
|
+
type: GitObjectType;
|
|
200
249
|
data: Uint8Array;
|
|
201
250
|
}> {
|
|
202
|
-
|
|
203
|
-
|
|
251
|
+
assertObjectId(sha);
|
|
252
|
+
const object = await objectContent({
|
|
204
253
|
gitdir: this.gitdir,
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
254
|
+
name: sha,
|
|
255
|
+
...(options?.maxBytes !== undefined
|
|
256
|
+
? { maxBytes: options.maxBytes }
|
|
257
|
+
: {}),
|
|
208
258
|
});
|
|
209
|
-
if (
|
|
210
|
-
|
|
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:
|
|
264
|
+
type: GitObjectType;
|
|
220
265
|
data: Uint8Array;
|
|
221
266
|
}): Promise<string> {
|
|
222
|
-
return
|
|
223
|
-
|
|
224
|
-
gitdir: this.gitdir,
|
|
267
|
+
return writeLooseObject({
|
|
268
|
+
objects: path.join(this.gitdir, "objects"),
|
|
225
269
|
type: opts.type,
|
|
226
|
-
|
|
227
|
-
format: "content",
|
|
270
|
+
data: opts.data,
|
|
228
271
|
});
|
|
229
272
|
}
|
|
230
273
|
|
|
231
274
|
async log(ref: string, maxCount = 50): Promise<CommitInfo[]> {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
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
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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
|
}
|