@catamorphic/git 0.0.0-stage → 0.1.0-alpha.31
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/LICENSE.txt +202 -0
- package/NOTICE.txt +32 -0
- package/dist/checkout-remote-backend.d.ts +12 -0
- package/dist/checkout-remote-backend.d.ts.map +1 -0
- package/dist/file-reads.d.ts +15 -0
- package/dist/file-reads.d.ts.map +1 -0
- package/dist/fs-backend.d.ts +30 -0
- package/dist/fs-backend.d.ts.map +1 -0
- package/dist/fs-remote-backend.d.ts +47 -0
- package/dist/fs-remote-backend.d.ts.map +1 -0
- package/dist/git-object-codec.d.ts +31 -0
- package/dist/git-object-codec.d.ts.map +1 -0
- package/dist/git-sync.d.ts +61 -0
- package/dist/git-sync.d.ts.map +1 -0
- package/dist/in-memory-object-store.d.ts +23 -0
- package/dist/in-memory-object-store.d.ts.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4746 -0
- package/dist/index.js.map +32 -0
- package/dist/migrate-workflow.d.ts +12 -0
- package/dist/migrate-workflow.d.ts.map +1 -0
- package/dist/native-git.d.ts +25 -0
- package/dist/native-git.d.ts.map +1 -0
- package/dist/native-project-repo.d.ts +57 -0
- package/dist/native-project-repo.d.ts.map +1 -0
- package/dist/network-sync.d.ts +58 -0
- package/dist/network-sync.d.ts.map +1 -0
- package/dist/network.d.ts +81 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/object-remote-backend.d.ts +95 -0
- package/dist/object-remote-backend.d.ts.map +1 -0
- package/dist/object-store.d.ts +42 -0
- package/dist/object-store.d.ts.map +1 -0
- package/dist/origin-draft.d.ts +264 -0
- package/dist/origin-draft.d.ts.map +1 -0
- package/dist/origin-objects.d.ts +201 -0
- package/dist/origin-objects.d.ts.map +1 -0
- package/dist/personal-files.d.ts +6 -0
- package/dist/personal-files.d.ts.map +1 -0
- package/dist/project-manager.d.ts +214 -0
- package/dist/project-manager.d.ts.map +1 -0
- package/dist/project-repo.d.ts +79 -0
- package/dist/project-repo.d.ts.map +1 -0
- package/dist/ref-moved-error.d.ts +13 -0
- package/dist/ref-moved-error.d.ts.map +1 -0
- package/dist/ref-names.d.ts +18 -0
- package/dist/ref-names.d.ts.map +1 -0
- package/dist/types.d.ts +276 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/workspace-mirror.d.ts +110 -0
- package/dist/workspace-mirror.d.ts.map +1 -0
- package/package.json +53 -4
- package/src/checkout-remote-backend.ts +154 -0
- package/src/diff3.d.ts +16 -0
- package/src/file-reads.ts +53 -0
- package/src/fs-backend.ts +206 -0
- package/src/fs-remote-backend.ts +252 -0
- package/src/git-object-codec.ts +87 -0
- package/src/git-sync.ts +539 -0
- package/src/in-memory-object-store.ts +60 -0
- package/src/index.ts +110 -0
- package/src/migrate-workflow.ts +30 -0
- package/src/native-git.ts +177 -0
- package/src/native-project-repo.ts +515 -0
- package/src/network-sync.ts +239 -0
- package/src/network.ts +269 -0
- package/src/object-remote-backend.ts +366 -0
- package/src/object-store.ts +41 -0
- package/src/origin-draft.ts +1033 -0
- package/src/origin-objects.ts +906 -0
- package/src/personal-files.ts +57 -0
- package/src/project-manager.ts +743 -0
- package/src/project-repo.ts +800 -0
- package/src/ref-moved-error.ts +18 -0
- package/src/ref-names.ts +47 -0
- package/src/types.ts +289 -0
- package/src/workspace-mirror.ts +400 -0
- package/README.md +0 -3
package/src/git-sync.ts
ADDED
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
import nodeFs from "node:fs";
|
|
2
|
+
import { publishedRef } from "@catamorphic/workflow/project-layout";
|
|
3
|
+
import git from "isomorphic-git";
|
|
4
|
+
import { nativeGit } from "./native-git.js";
|
|
5
|
+
import { NativeProjectRepo } from "./native-project-repo.js";
|
|
6
|
+
import { mapLimit } from "./origin-objects.js";
|
|
7
|
+
import type {
|
|
8
|
+
ConflictEntry,
|
|
9
|
+
MergeResult,
|
|
10
|
+
OriginRepo,
|
|
11
|
+
ProjectRepo,
|
|
12
|
+
RemoteBackend,
|
|
13
|
+
} from "./types.js";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Server-orchestrated git transport between a dev `ProjectRepo` (working tree)
|
|
17
|
+
* and an `OriginRepo` (bare repo). This module implements **object-level
|
|
18
|
+
* transfer** — walking reachable commits/trees/blobs and copying them between
|
|
19
|
+
* the two stores — so we never need a network git protocol between sandboxes.
|
|
20
|
+
*
|
|
21
|
+
* All operations assume the dev repo is an FS-backed `ProjectRepoImpl`. When
|
|
22
|
+
* we later wire in Cloudflare dev sandboxes, this module will grow a thin
|
|
23
|
+
* adapter, but the transfer algorithm stays the same.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const SYSTEM_AUTHOR = {
|
|
27
|
+
name: "Work",
|
|
28
|
+
email: "system@work.software",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export interface PushOpts {
|
|
32
|
+
dev: ProjectRepo;
|
|
33
|
+
remote: Pick<RemoteBackend, "withOrigin">;
|
|
34
|
+
tenantId: string;
|
|
35
|
+
projectId: string;
|
|
36
|
+
/** Branch on the remote to push to. Defaults to `main`. */
|
|
37
|
+
remoteBranch?: string;
|
|
38
|
+
/** SHA on the dev side to push. Defaults to the tip of the current branch. */
|
|
39
|
+
localSha?: string;
|
|
40
|
+
/**
|
|
41
|
+
* Move the branch even when it is not a fast-forward. Only for branches
|
|
42
|
+
* Work owns outright, such as a session's `sessions/<id>` whose base moved.
|
|
43
|
+
*/
|
|
44
|
+
force?: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface FetchOpts {
|
|
48
|
+
dev: ProjectRepo;
|
|
49
|
+
remote: Pick<RemoteBackend, "withOrigin">;
|
|
50
|
+
tenantId: string;
|
|
51
|
+
projectId: string;
|
|
52
|
+
remoteBranch?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface PullOpts extends FetchOpts {
|
|
56
|
+
/** Author for the merge commit (if one is needed). */
|
|
57
|
+
author?: { name: string; email: string };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Copy reachable commits from dev → origin and fast-forward the remote branch.
|
|
62
|
+
* Throws if the push would not be a fast-forward (caller should pull first).
|
|
63
|
+
*/
|
|
64
|
+
export async function push(opts: PushOpts): Promise<{ sha: string }> {
|
|
65
|
+
const branch = opts.remoteBranch ?? "main";
|
|
66
|
+
const localSha = opts.localSha ?? (await opts.dev.resolveRef("HEAD"));
|
|
67
|
+
|
|
68
|
+
return opts.remote.withOrigin(
|
|
69
|
+
opts.tenantId,
|
|
70
|
+
opts.projectId,
|
|
71
|
+
async (origin) => {
|
|
72
|
+
const ref = `refs/heads/${branch}`;
|
|
73
|
+
const currentRemote = await origin.resolveRef(ref);
|
|
74
|
+
|
|
75
|
+
if (currentRemote === localSha) {
|
|
76
|
+
return { sha: localSha };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (currentRemote && !opts.force) {
|
|
80
|
+
const fastForward = await isAncestor({
|
|
81
|
+
dev: opts.dev,
|
|
82
|
+
ancestor: currentRemote,
|
|
83
|
+
descendant: localSha,
|
|
84
|
+
});
|
|
85
|
+
if (!fastForward) {
|
|
86
|
+
throw new PushNotFastForwardError({
|
|
87
|
+
localSha,
|
|
88
|
+
remoteSha: currentRemote,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await transferCommits({
|
|
94
|
+
source: devSource(opts.dev),
|
|
95
|
+
sink: originSink(origin),
|
|
96
|
+
sha: localSha,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
await origin.updateRef({
|
|
100
|
+
ref,
|
|
101
|
+
sha: localSha,
|
|
102
|
+
expected: currentRemote,
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
await syncRemoteTrackingRef({
|
|
106
|
+
dev: opts.dev,
|
|
107
|
+
branch,
|
|
108
|
+
sha: localSha,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return { sha: localSha };
|
|
112
|
+
},
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Copy the tip of the remote branch into the dev repo and update the
|
|
118
|
+
* `refs/work/published/<branch>` tracking ref. Does not touch the working tree.
|
|
119
|
+
*/
|
|
120
|
+
export async function fetchRemote(opts: FetchOpts): Promise<{
|
|
121
|
+
sha: string | null;
|
|
122
|
+
alreadyUpToDate: boolean;
|
|
123
|
+
}> {
|
|
124
|
+
const branch = opts.remoteBranch ?? "main";
|
|
125
|
+
|
|
126
|
+
return opts.remote.withOrigin(
|
|
127
|
+
opts.tenantId,
|
|
128
|
+
opts.projectId,
|
|
129
|
+
async (origin) => {
|
|
130
|
+
const ref = `refs/heads/${branch}`;
|
|
131
|
+
const remoteSha = await origin.resolveRef(ref);
|
|
132
|
+
if (!remoteSha) return { sha: null, alreadyUpToDate: true };
|
|
133
|
+
|
|
134
|
+
const alreadyUpToDate = await devHasCommit({
|
|
135
|
+
dev: opts.dev,
|
|
136
|
+
sha: remoteSha,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
if (!alreadyUpToDate) {
|
|
140
|
+
await transferCommits({
|
|
141
|
+
source: originSource(origin),
|
|
142
|
+
sink: devSink(opts.dev),
|
|
143
|
+
sha: remoteSha,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
await syncRemoteTrackingRef({
|
|
148
|
+
dev: opts.dev,
|
|
149
|
+
branch,
|
|
150
|
+
sha: remoteSha,
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
return { sha: remoteSha, alreadyUpToDate };
|
|
154
|
+
},
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Fetch the remote branch, then merge it into the current branch. Uses a
|
|
160
|
+
* 3-way merge via isomorphic-git. Returns conflicts when the merge cannot be
|
|
161
|
+
* resolved automatically; working tree is left with conflict markers in that
|
|
162
|
+
* case so the caller can surface them or invoke the AI resolver.
|
|
163
|
+
*/
|
|
164
|
+
export async function pull(opts: PullOpts): Promise<MergeResult> {
|
|
165
|
+
const branch = opts.remoteBranch ?? "main";
|
|
166
|
+
const author = opts.author ?? SYSTEM_AUTHOR;
|
|
167
|
+
|
|
168
|
+
const fetched = await fetchRemote(opts);
|
|
169
|
+
if (!fetched.sha) {
|
|
170
|
+
return {
|
|
171
|
+
status: "up-to-date",
|
|
172
|
+
mergeCommit: null,
|
|
173
|
+
conflicts: [],
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const localSha = await opts.dev.resolveRef("HEAD");
|
|
178
|
+
if (localSha === fetched.sha) {
|
|
179
|
+
return {
|
|
180
|
+
status: "up-to-date",
|
|
181
|
+
mergeCommit: null,
|
|
182
|
+
conflicts: [],
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const localIsAhead = await isAncestor({
|
|
187
|
+
dev: opts.dev,
|
|
188
|
+
ancestor: fetched.sha,
|
|
189
|
+
descendant: localSha,
|
|
190
|
+
});
|
|
191
|
+
if (localIsAhead) {
|
|
192
|
+
return {
|
|
193
|
+
status: "up-to-date",
|
|
194
|
+
mergeCommit: null,
|
|
195
|
+
conflicts: [],
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const currentBranch = await opts.dev.currentBranch();
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
const result = await git.merge({
|
|
203
|
+
fs: nodeFs,
|
|
204
|
+
dir: opts.dev.repoPath,
|
|
205
|
+
ours: currentBranch,
|
|
206
|
+
theirs: fetched.sha,
|
|
207
|
+
author,
|
|
208
|
+
committer: author,
|
|
209
|
+
fastForward: true,
|
|
210
|
+
abortOnConflict: false,
|
|
211
|
+
message: `Merge origin/${branch} into ${currentBranch}`,
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
await git.checkout({
|
|
215
|
+
fs: nodeFs,
|
|
216
|
+
dir: opts.dev.repoPath,
|
|
217
|
+
ref: currentBranch,
|
|
218
|
+
force: true,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
return {
|
|
222
|
+
status: "clean",
|
|
223
|
+
mergeCommit: result.oid ?? null,
|
|
224
|
+
conflicts: [],
|
|
225
|
+
};
|
|
226
|
+
} catch (err: unknown) {
|
|
227
|
+
const maybeConflict = err as { code?: string; data?: unknown };
|
|
228
|
+
if (maybeConflict?.code === "MergeConflictError") {
|
|
229
|
+
const conflicts = await collectConflicts({
|
|
230
|
+
dev: opts.dev,
|
|
231
|
+
oursSha: localSha,
|
|
232
|
+
theirsSha: fetched.sha,
|
|
233
|
+
raw: maybeConflict.data,
|
|
234
|
+
});
|
|
235
|
+
return {
|
|
236
|
+
status: "conflict",
|
|
237
|
+
mergeCommit: null,
|
|
238
|
+
conflicts,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
throw err;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export class PushNotFastForwardError extends Error {
|
|
246
|
+
readonly localSha: string;
|
|
247
|
+
readonly remoteSha: string;
|
|
248
|
+
constructor(opts: { localSha: string; remoteSha: string }) {
|
|
249
|
+
super(
|
|
250
|
+
`Push is not a fast-forward (local ${opts.localSha.slice(0, 8)} vs remote ${opts.remoteSha.slice(0, 8)})`,
|
|
251
|
+
);
|
|
252
|
+
this.name = "PushNotFastForwardError";
|
|
253
|
+
this.localSha = opts.localSha;
|
|
254
|
+
this.remoteSha = opts.remoteSha;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// --- Object transfer primitives ---
|
|
259
|
+
|
|
260
|
+
interface ObjectSource {
|
|
261
|
+
hasObject(sha: string): Promise<boolean>;
|
|
262
|
+
readObject(sha: string): Promise<{
|
|
263
|
+
type: "blob" | "tree" | "commit" | "tag";
|
|
264
|
+
data: Uint8Array;
|
|
265
|
+
}>;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
interface ObjectSink {
|
|
269
|
+
hasObject(sha: string): Promise<boolean>;
|
|
270
|
+
writeObject(opts: {
|
|
271
|
+
type: "blob" | "tree" | "commit" | "tag";
|
|
272
|
+
data: Uint8Array;
|
|
273
|
+
}): Promise<string>;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function devSource(dev: ProjectRepo): ObjectSource {
|
|
277
|
+
const cache = {};
|
|
278
|
+
return {
|
|
279
|
+
hasObject: (sha) =>
|
|
280
|
+
git
|
|
281
|
+
.readObject({ fs: nodeFs, dir: dev.repoPath, oid: sha, cache })
|
|
282
|
+
.then(() => true)
|
|
283
|
+
.catch(() => false),
|
|
284
|
+
readObject: async (sha) => {
|
|
285
|
+
const obj = await git.readObject({
|
|
286
|
+
fs: nodeFs,
|
|
287
|
+
dir: dev.repoPath,
|
|
288
|
+
oid: sha,
|
|
289
|
+
cache,
|
|
290
|
+
format: "content",
|
|
291
|
+
});
|
|
292
|
+
return {
|
|
293
|
+
type: obj.type as "blob" | "tree" | "commit" | "tag",
|
|
294
|
+
data: obj.object as Uint8Array,
|
|
295
|
+
};
|
|
296
|
+
},
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function devSink(dev: ProjectRepo): ObjectSink {
|
|
301
|
+
const cache = {};
|
|
302
|
+
return {
|
|
303
|
+
hasObject: (sha) =>
|
|
304
|
+
git
|
|
305
|
+
.readObject({ fs: nodeFs, dir: dev.repoPath, oid: sha, cache })
|
|
306
|
+
.then(() => true)
|
|
307
|
+
.catch(() => false),
|
|
308
|
+
writeObject: (opts) =>
|
|
309
|
+
git.writeObject({
|
|
310
|
+
fs: nodeFs,
|
|
311
|
+
dir: dev.repoPath,
|
|
312
|
+
type: opts.type,
|
|
313
|
+
object: opts.data,
|
|
314
|
+
format: "content",
|
|
315
|
+
}),
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function originSource(origin: OriginRepo): ObjectSource {
|
|
320
|
+
return {
|
|
321
|
+
hasObject: (sha) => origin.hasObject(sha),
|
|
322
|
+
readObject: (sha) => origin.readObject(sha),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function originSink(origin: OriginRepo): ObjectSink {
|
|
327
|
+
return {
|
|
328
|
+
hasObject: (sha) => origin.hasObject(sha),
|
|
329
|
+
writeObject: (opts) => origin.writeObject(opts),
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async function transferCommits(opts: {
|
|
334
|
+
source: ObjectSource;
|
|
335
|
+
sink: ObjectSink;
|
|
336
|
+
sha: string;
|
|
337
|
+
}): Promise<void> {
|
|
338
|
+
// Breadth first, a batch of objects at a time: seeding a checkout from a
|
|
339
|
+
// network origin is bound by round trips, not by the objects' size.
|
|
340
|
+
let frontier: string[] = [opts.sha];
|
|
341
|
+
const seen = new Set<string>();
|
|
342
|
+
while (frontier.length > 0) {
|
|
343
|
+
const batch = [...new Set(frontier)].filter((sha) => !seen.has(sha));
|
|
344
|
+
for (const sha of batch) seen.add(sha);
|
|
345
|
+
const next = await mapLimit(batch, TRANSFER_CONCURRENCY, async (sha) => {
|
|
346
|
+
if (await opts.sink.hasObject(sha)) return [];
|
|
347
|
+
const obj = await opts.source.readObject(sha);
|
|
348
|
+
await opts.sink.writeObject(obj);
|
|
349
|
+
if (obj.type === "commit") {
|
|
350
|
+
const commit = parseCommit(obj.data);
|
|
351
|
+
return [commit.tree, ...commit.parents];
|
|
352
|
+
}
|
|
353
|
+
if (obj.type === "tree")
|
|
354
|
+
return parseTree(obj.data)
|
|
355
|
+
.filter((entry) => entry.mode !== "160000")
|
|
356
|
+
.map((entry) => entry.oid);
|
|
357
|
+
return [];
|
|
358
|
+
});
|
|
359
|
+
frontier = next.flat();
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const TRANSFER_CONCURRENCY = 16;
|
|
364
|
+
|
|
365
|
+
interface ParsedCommit {
|
|
366
|
+
tree: string;
|
|
367
|
+
parents: string[];
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function parseCommit(data: Uint8Array): ParsedCommit {
|
|
371
|
+
const text = new TextDecoder("utf-8").decode(data);
|
|
372
|
+
const headerEnd = text.indexOf("\n\n");
|
|
373
|
+
const header = headerEnd === -1 ? text : text.slice(0, headerEnd);
|
|
374
|
+
const parents: string[] = [];
|
|
375
|
+
let tree = "";
|
|
376
|
+
for (const line of header.split("\n")) {
|
|
377
|
+
if (line.startsWith("tree ")) tree = line.slice(5).trim();
|
|
378
|
+
else if (line.startsWith("parent ")) parents.push(line.slice(7).trim());
|
|
379
|
+
}
|
|
380
|
+
return { tree, parents };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
interface TreeEntry {
|
|
384
|
+
mode: string;
|
|
385
|
+
name: string;
|
|
386
|
+
oid: string;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function parseTree(data: Uint8Array): TreeEntry[] {
|
|
390
|
+
const entries: TreeEntry[] = [];
|
|
391
|
+
let i = 0;
|
|
392
|
+
while (i < data.length) {
|
|
393
|
+
let spaceIdx = i;
|
|
394
|
+
while (spaceIdx < data.length && data[spaceIdx] !== 0x20) spaceIdx++;
|
|
395
|
+
const mode = new TextDecoder("utf-8").decode(data.slice(i, spaceIdx));
|
|
396
|
+
|
|
397
|
+
let nullIdx = spaceIdx + 1;
|
|
398
|
+
while (nullIdx < data.length && data[nullIdx] !== 0x00) nullIdx++;
|
|
399
|
+
const name = new TextDecoder("utf-8").decode(
|
|
400
|
+
data.slice(spaceIdx + 1, nullIdx),
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
const oidBytes = data.slice(nullIdx + 1, nullIdx + 21);
|
|
404
|
+
const oid = Array.from(oidBytes)
|
|
405
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
406
|
+
.join("");
|
|
407
|
+
|
|
408
|
+
entries.push({ mode, name, oid });
|
|
409
|
+
i = nullIdx + 21;
|
|
410
|
+
}
|
|
411
|
+
return entries;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// --- Ancestor check ---
|
|
415
|
+
|
|
416
|
+
async function isAncestor(opts: {
|
|
417
|
+
dev: ProjectRepo;
|
|
418
|
+
ancestor: string;
|
|
419
|
+
descendant: string;
|
|
420
|
+
}): Promise<boolean> {
|
|
421
|
+
if (opts.ancestor === opts.descendant) return true;
|
|
422
|
+
if (opts.dev instanceof NativeProjectRepo)
|
|
423
|
+
return nativeGit(opts.dev.repoPath, [
|
|
424
|
+
"merge-base",
|
|
425
|
+
"--is-ancestor",
|
|
426
|
+
opts.ancestor,
|
|
427
|
+
opts.descendant,
|
|
428
|
+
]).then(
|
|
429
|
+
() => true,
|
|
430
|
+
() => false,
|
|
431
|
+
);
|
|
432
|
+
try {
|
|
433
|
+
const commits = await git.log({
|
|
434
|
+
fs: nodeFs,
|
|
435
|
+
dir: opts.dev.repoPath,
|
|
436
|
+
ref: opts.descendant,
|
|
437
|
+
depth: -1,
|
|
438
|
+
});
|
|
439
|
+
return commits.some((c) => c.oid === opts.ancestor);
|
|
440
|
+
} catch {
|
|
441
|
+
return false;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
async function devHasCommit(opts: {
|
|
446
|
+
dev: ProjectRepo;
|
|
447
|
+
sha: string;
|
|
448
|
+
}): Promise<boolean> {
|
|
449
|
+
if (opts.dev instanceof NativeProjectRepo)
|
|
450
|
+
return nativeGit(opts.dev.repoPath, ["cat-file", "-e", opts.sha]).then(
|
|
451
|
+
() => true,
|
|
452
|
+
() => false,
|
|
453
|
+
);
|
|
454
|
+
try {
|
|
455
|
+
await git.readObject({
|
|
456
|
+
fs: nodeFs,
|
|
457
|
+
dir: opts.dev.repoPath,
|
|
458
|
+
oid: opts.sha,
|
|
459
|
+
});
|
|
460
|
+
return true;
|
|
461
|
+
} catch {
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
async function syncRemoteTrackingRef(opts: {
|
|
467
|
+
dev: ProjectRepo;
|
|
468
|
+
branch: string;
|
|
469
|
+
sha: string;
|
|
470
|
+
}): Promise<void> {
|
|
471
|
+
if (opts.dev instanceof NativeProjectRepo) {
|
|
472
|
+
await nativeGit(opts.dev.repoPath, [
|
|
473
|
+
"update-ref",
|
|
474
|
+
publishedRef(opts.branch),
|
|
475
|
+
opts.sha,
|
|
476
|
+
]);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
await git.writeRef({
|
|
480
|
+
fs: nodeFs,
|
|
481
|
+
dir: opts.dev.repoPath,
|
|
482
|
+
ref: publishedRef(opts.branch),
|
|
483
|
+
value: opts.sha,
|
|
484
|
+
force: true,
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async function collectConflicts(opts: {
|
|
489
|
+
dev: ProjectRepo;
|
|
490
|
+
oursSha: string;
|
|
491
|
+
theirsSha: string;
|
|
492
|
+
raw: unknown;
|
|
493
|
+
}): Promise<ConflictEntry[]> {
|
|
494
|
+
const raw = opts.raw as { filepaths?: string[] } | undefined;
|
|
495
|
+
const paths = raw?.filepaths ?? [];
|
|
496
|
+
const result: ConflictEntry[] = [];
|
|
497
|
+
|
|
498
|
+
for (const filepath of paths) {
|
|
499
|
+
const ours = await readAtRef(opts.dev.repoPath, opts.oursSha, filepath);
|
|
500
|
+
const theirs = await readAtRef(opts.dev.repoPath, opts.theirsSha, filepath);
|
|
501
|
+
const baseSha = await git
|
|
502
|
+
.findMergeBase({
|
|
503
|
+
fs: nodeFs,
|
|
504
|
+
dir: opts.dev.repoPath,
|
|
505
|
+
oids: [opts.oursSha, opts.theirsSha],
|
|
506
|
+
})
|
|
507
|
+
.then((bases) => bases[0] ?? null)
|
|
508
|
+
.catch(() => null);
|
|
509
|
+
const base = baseSha
|
|
510
|
+
? await readAtRef(opts.dev.repoPath, baseSha, filepath)
|
|
511
|
+
: null;
|
|
512
|
+
|
|
513
|
+
result.push({
|
|
514
|
+
path: filepath,
|
|
515
|
+
base,
|
|
516
|
+
ours,
|
|
517
|
+
theirs,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
return result;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
async function readAtRef(
|
|
524
|
+
dir: string,
|
|
525
|
+
oid: string,
|
|
526
|
+
filepath: string,
|
|
527
|
+
): Promise<string | null> {
|
|
528
|
+
try {
|
|
529
|
+
const { blob } = await git.readBlob({
|
|
530
|
+
fs: nodeFs,
|
|
531
|
+
dir,
|
|
532
|
+
oid,
|
|
533
|
+
filepath,
|
|
534
|
+
});
|
|
535
|
+
return new TextDecoder("utf-8").decode(blob);
|
|
536
|
+
} catch {
|
|
537
|
+
return null;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { type ObjectStore, PreconditionFailedError } from "./object-store.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* In-memory `ObjectStore` mirroring S3 conditional-write semantics. Intended
|
|
6
|
+
* for unit tests (both catamorphic's and host apps').
|
|
7
|
+
*/
|
|
8
|
+
export class InMemoryObjectStore implements ObjectStore {
|
|
9
|
+
private readonly objects = new Map<
|
|
10
|
+
string,
|
|
11
|
+
{ data: Uint8Array; etag: string }
|
|
12
|
+
>();
|
|
13
|
+
|
|
14
|
+
async get(key: string): Promise<{ data: Uint8Array; etag: string } | null> {
|
|
15
|
+
const entry = this.objects.get(key);
|
|
16
|
+
if (!entry) return null;
|
|
17
|
+
return { data: entry.data.slice(), etag: entry.etag };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async has(key: string): Promise<boolean> {
|
|
21
|
+
return this.objects.has(key);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async put(
|
|
25
|
+
key: string,
|
|
26
|
+
data: Uint8Array,
|
|
27
|
+
opts?: { ifMatch?: string; ifNoneMatch?: "*" },
|
|
28
|
+
): Promise<void> {
|
|
29
|
+
const current = this.objects.get(key);
|
|
30
|
+
if (opts?.ifNoneMatch === "*" && current) {
|
|
31
|
+
throw new PreconditionFailedError(key);
|
|
32
|
+
}
|
|
33
|
+
if (opts?.ifMatch !== undefined) {
|
|
34
|
+
if (!current || current.etag !== opts.ifMatch) {
|
|
35
|
+
throw new PreconditionFailedError(key);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const etag = `"${createHash("md5").update(data).digest("hex")}"`;
|
|
39
|
+
this.objects.set(key, { data: data.slice(), etag });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async list(prefix: string): Promise<string[]> {
|
|
43
|
+
return [...this.objects.keys()].filter((key) => key.startsWith(prefix));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async delete(key: string, opts?: { ifMatch?: string }): Promise<void> {
|
|
47
|
+
if (
|
|
48
|
+
opts?.ifMatch !== undefined &&
|
|
49
|
+
this.objects.get(key)?.etag !== opts.ifMatch
|
|
50
|
+
)
|
|
51
|
+
throw new PreconditionFailedError(key);
|
|
52
|
+
this.objects.delete(key);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async deletePrefix(prefix: string): Promise<void> {
|
|
56
|
+
for (const key of await this.list(prefix)) {
|
|
57
|
+
this.objects.delete(key);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export { CheckoutRemoteBackend } from "./checkout-remote-backend.js";
|
|
2
|
+
export { type FileReadOptions, readFileSnapshot } from "./file-reads.js";
|
|
3
|
+
export { FsBackend } from "./fs-backend.js";
|
|
4
|
+
export { FsOriginRepo, FsRemoteBackend } from "./fs-remote-backend.js";
|
|
5
|
+
export {
|
|
6
|
+
fetchRemote,
|
|
7
|
+
PushNotFastForwardError,
|
|
8
|
+
pull,
|
|
9
|
+
push,
|
|
10
|
+
} from "./git-sync.js";
|
|
11
|
+
export { InMemoryObjectStore } from "./in-memory-object-store.js";
|
|
12
|
+
export { migrateWorkflowToProject } from "./migrate-workflow.js";
|
|
13
|
+
export {
|
|
14
|
+
discoverLocalFolder,
|
|
15
|
+
hasLocalGit,
|
|
16
|
+
type LocalFolder,
|
|
17
|
+
nativeGit,
|
|
18
|
+
} from "./native-git.js";
|
|
19
|
+
export { NativeProjectRepo } from "./native-project-repo.js";
|
|
20
|
+
export {
|
|
21
|
+
assertPushAllowed,
|
|
22
|
+
type CloneFromRemoteOptions,
|
|
23
|
+
cloneFromRemote,
|
|
24
|
+
fetchFromRemote,
|
|
25
|
+
isManagedBranch,
|
|
26
|
+
pushToRemote,
|
|
27
|
+
type RemoteOwnership,
|
|
28
|
+
RemotePushRefusedError,
|
|
29
|
+
} from "./network.js";
|
|
30
|
+
export {
|
|
31
|
+
type NetworkSyncResult,
|
|
32
|
+
type NetworkSyncStatus,
|
|
33
|
+
syncWithNetworkRemote,
|
|
34
|
+
} from "./network-sync.js";
|
|
35
|
+
export {
|
|
36
|
+
ObjectOriginRepo,
|
|
37
|
+
ObjectRemoteBackend,
|
|
38
|
+
type ObjectRemoteBackendOpts,
|
|
39
|
+
probeConditionalWrites,
|
|
40
|
+
} from "./object-remote-backend.js";
|
|
41
|
+
export type { ObjectStore } from "./object-store.js";
|
|
42
|
+
export { PreconditionFailedError } from "./object-store.js";
|
|
43
|
+
export {
|
|
44
|
+
DraftBusyError,
|
|
45
|
+
type DraftChange,
|
|
46
|
+
DraftContentChangedError,
|
|
47
|
+
type DraftPublishResult,
|
|
48
|
+
DraftRefNotAllowedError,
|
|
49
|
+
DraftsUnsupportedError,
|
|
50
|
+
DraftUnresolvedError,
|
|
51
|
+
InvalidBaseError,
|
|
52
|
+
NoDraftError,
|
|
53
|
+
OriginDraftRepo,
|
|
54
|
+
type ProjectDraft,
|
|
55
|
+
refreshPublished,
|
|
56
|
+
} from "./origin-draft.js";
|
|
57
|
+
export {
|
|
58
|
+
DraftBinaryResolutionError,
|
|
59
|
+
DraftIgnoredPathError,
|
|
60
|
+
DraftPathError,
|
|
61
|
+
GitObjectCache,
|
|
62
|
+
} from "./origin-objects.js";
|
|
63
|
+
export {
|
|
64
|
+
ensurePersonalFilesExcluded,
|
|
65
|
+
isPersonalFile,
|
|
66
|
+
} from "./personal-files.js";
|
|
67
|
+
export {
|
|
68
|
+
PROGRAM_READER_ID,
|
|
69
|
+
PROJECT_GITIGNORE,
|
|
70
|
+
ProjectManager,
|
|
71
|
+
} from "./project-manager.js";
|
|
72
|
+
export { ProjectRepoImpl } from "./project-repo.js";
|
|
73
|
+
export { RefMovedError } from "./ref-moved-error.js";
|
|
74
|
+
export {
|
|
75
|
+
assertValidRefName,
|
|
76
|
+
InvalidRefNameError,
|
|
77
|
+
isValidRefName,
|
|
78
|
+
} from "./ref-names.js";
|
|
79
|
+
export type {
|
|
80
|
+
BranchInfo,
|
|
81
|
+
CloneSource,
|
|
82
|
+
CommitInfo,
|
|
83
|
+
ConflictEntry,
|
|
84
|
+
DiffEntry,
|
|
85
|
+
DraftSupport,
|
|
86
|
+
FileChange,
|
|
87
|
+
GitCredentials,
|
|
88
|
+
InitProjectOptions,
|
|
89
|
+
MergeResult,
|
|
90
|
+
OriginRepo,
|
|
91
|
+
ProjectPathResolver,
|
|
92
|
+
ProjectRepo,
|
|
93
|
+
RemoteBackend,
|
|
94
|
+
RepoStatus,
|
|
95
|
+
StorageBackend,
|
|
96
|
+
} from "./types.js";
|
|
97
|
+
export {
|
|
98
|
+
type BaseMoveOutcome,
|
|
99
|
+
buildSeedPack,
|
|
100
|
+
copyFromMirror,
|
|
101
|
+
fetchIntoMirror,
|
|
102
|
+
isCommitId,
|
|
103
|
+
mirrorChangedFiles,
|
|
104
|
+
moveCheckoutBase,
|
|
105
|
+
parseWorkspaceRef,
|
|
106
|
+
seedPackInstallScript,
|
|
107
|
+
shellQuote,
|
|
108
|
+
unpinMirrorRef,
|
|
109
|
+
type WorkspaceBase,
|
|
110
|
+
} from "./workspace-mirror.js";
|