@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
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A compare-and-swap ref update or delete lost its race: the ref no longer
|
|
3
|
+
* holds the value the caller read. Callers re-read and retry.
|
|
4
|
+
*/
|
|
5
|
+
export class RefMovedError extends Error {
|
|
6
|
+
readonly ref: string;
|
|
7
|
+
constructor(input: {
|
|
8
|
+
ref: string;
|
|
9
|
+
expected: string | null;
|
|
10
|
+
actual: string | null;
|
|
11
|
+
}) {
|
|
12
|
+
super(
|
|
13
|
+
`Ref ${input.ref} moved (expected ${input.expected ?? "none"}, got ${input.actual ?? "none"})`,
|
|
14
|
+
);
|
|
15
|
+
this.name = "RefMovedError";
|
|
16
|
+
this.ref = input.ref;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/ref-names.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Git's rules for ref names (`git check-ref-format`), applied to every ref a
|
|
3
|
+
* caller names before any backend reads it. A backend that stores refs as
|
|
4
|
+
* files or keys must never see `..`, empty segments, or other forms that
|
|
5
|
+
* could reach a ref outside the one named.
|
|
6
|
+
*/
|
|
7
|
+
export function isValidRefName(ref: string): boolean {
|
|
8
|
+
if (!ref || ref === "@" || ref.endsWith("/") || ref.endsWith("."))
|
|
9
|
+
return false;
|
|
10
|
+
if (ref.includes("..") || ref.includes("//") || ref.includes("@{"))
|
|
11
|
+
return false;
|
|
12
|
+
// Control characters, space, and git's reserved punctuation.
|
|
13
|
+
for (const char of ref) {
|
|
14
|
+
const code = char.charCodeAt(0);
|
|
15
|
+
if (code <= 0x20 || code === 0x7f || "~^:?*[\\".includes(char))
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return ref
|
|
19
|
+
.split("/")
|
|
20
|
+
.every(
|
|
21
|
+
(segment) =>
|
|
22
|
+
segment.length > 0 &&
|
|
23
|
+
!segment.startsWith(".") &&
|
|
24
|
+
!segment.endsWith(".lock"),
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A ref name that fails git's rules. */
|
|
29
|
+
export class InvalidRefNameError extends Error {
|
|
30
|
+
constructor(ref: string) {
|
|
31
|
+
super(`Invalid ref name: ${JSON.stringify(ref)}`);
|
|
32
|
+
this.name = "InvalidRefNameError";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function assertValidRefName(ref: string): void {
|
|
37
|
+
if (!isValidRefName(ref)) throw new InvalidRefNameError(ref);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A name `resolveRef` may be given: `HEAD`, a full commit id, or a valid
|
|
42
|
+
* branch or full ref name.
|
|
43
|
+
*/
|
|
44
|
+
export function assertResolvableRef(ref: string): void {
|
|
45
|
+
if (ref === "HEAD" || /^[0-9a-f]{40}$/.test(ref)) return;
|
|
46
|
+
assertValidRefName(ref);
|
|
47
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import type { FileReadOptions } from "./file-reads.js";
|
|
2
|
+
export interface CommitInfo {
|
|
3
|
+
sha: string;
|
|
4
|
+
message: string;
|
|
5
|
+
author: { name: string; email: string };
|
|
6
|
+
timestamp: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface GitCredentials {
|
|
10
|
+
username: string;
|
|
11
|
+
password: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Host-provided lookup for projects whose working copy lives at an explicit
|
|
16
|
+
* filesystem location (e.g. a user-visible folder chosen in a desktop app).
|
|
17
|
+
* Returning `null` falls back to the backend's own internal layout.
|
|
18
|
+
*/
|
|
19
|
+
export type ProjectPathResolver = (
|
|
20
|
+
tenantId: string,
|
|
21
|
+
projectId: string,
|
|
22
|
+
) => Promise<string | null>;
|
|
23
|
+
|
|
24
|
+
export interface InitProjectOptions {
|
|
25
|
+
externalUserId?: string;
|
|
26
|
+
/** Explicit absolute directory to initialize the project in. */
|
|
27
|
+
rootPath?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface StorageBackend {
|
|
31
|
+
acquireProject(
|
|
32
|
+
tenantId: string,
|
|
33
|
+
projectId: string,
|
|
34
|
+
externalUserId?: string,
|
|
35
|
+
): Promise<{ repoPath: string; release: () => Promise<void> }>;
|
|
36
|
+
initProject(
|
|
37
|
+
tenantId: string,
|
|
38
|
+
projectId: string,
|
|
39
|
+
opts?: InitProjectOptions,
|
|
40
|
+
): Promise<string>;
|
|
41
|
+
deleteProject(tenantId: string, projectId: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Remove one user's internal working copy (a session's `session-<id>`
|
|
44
|
+
* copy). A copy that is a user-visible project folder is never removed.
|
|
45
|
+
* Missing is a no-op.
|
|
46
|
+
*/
|
|
47
|
+
deleteCopy(
|
|
48
|
+
tenantId: string,
|
|
49
|
+
projectId: string,
|
|
50
|
+
externalUserId: string,
|
|
51
|
+
): Promise<void>;
|
|
52
|
+
exists(
|
|
53
|
+
tenantId: string,
|
|
54
|
+
projectId: string,
|
|
55
|
+
externalUserId?: string,
|
|
56
|
+
): Promise<boolean>;
|
|
57
|
+
/**
|
|
58
|
+
* Where this host keeps its bare mirror of the project's linked remote
|
|
59
|
+
* (ADR 0178): a cache sessions are seeded from. Backends without local
|
|
60
|
+
* disk omit it, and workspaces at a ref are then unavailable.
|
|
61
|
+
*/
|
|
62
|
+
mirrorPath?(tenantId: string, projectId: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* A folder this host may use as a disposable cache for one project (a
|
|
65
|
+
* member's store mirror around agent turns). Losing it loses nothing
|
|
66
|
+
* that is not also elsewhere.
|
|
67
|
+
*/
|
|
68
|
+
cachePath?(tenantId: string, projectId: string, name: string): string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type FileChange =
|
|
72
|
+
| { kind: "added"; path: string; content: string }
|
|
73
|
+
| { kind: "modified"; path: string; before: string; after: string }
|
|
74
|
+
| { kind: "deleted"; path: string; before: string };
|
|
75
|
+
|
|
76
|
+
export interface RepoStatus {
|
|
77
|
+
branch: string;
|
|
78
|
+
dirty: boolean;
|
|
79
|
+
modifiedFiles: string[];
|
|
80
|
+
ahead: number;
|
|
81
|
+
behind: number;
|
|
82
|
+
baseCommit: string | null;
|
|
83
|
+
remoteHead: string | null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface BranchInfo {
|
|
87
|
+
name: string;
|
|
88
|
+
commit: string;
|
|
89
|
+
isCurrent: boolean;
|
|
90
|
+
createdAt: number | null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface DiffEntry {
|
|
94
|
+
path: string;
|
|
95
|
+
kind: "added" | "modified" | "deleted";
|
|
96
|
+
before: string | null;
|
|
97
|
+
after: string | null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface MergeResult {
|
|
101
|
+
status: "clean" | "conflict" | "up-to-date";
|
|
102
|
+
mergeCommit: string | null;
|
|
103
|
+
conflicts: ConflictEntry[];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface ConflictEntry {
|
|
107
|
+
path: string;
|
|
108
|
+
/** Text of each side; null when absent there, or when the file is binary. */
|
|
109
|
+
base: string | null;
|
|
110
|
+
ours: string | null;
|
|
111
|
+
theirs: string | null;
|
|
112
|
+
/** A file that is not text: resolved by keeping a side, not by text. */
|
|
113
|
+
binary?: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ProjectRepo {
|
|
117
|
+
readonly projectId: string;
|
|
118
|
+
readonly repoPath: string;
|
|
119
|
+
|
|
120
|
+
readFile(filePath: string): Promise<string>;
|
|
121
|
+
writeFile(filePath: string, content: string): Promise<void>;
|
|
122
|
+
deleteFile(filePath: string): Promise<void>;
|
|
123
|
+
listFiles(opts?: { prefix?: string }): Promise<string[]>;
|
|
124
|
+
/** Optional accelerated literal search returning text file paths, without loading their bodies into the host. */
|
|
125
|
+
findFilesContaining?(input: {
|
|
126
|
+
text: string;
|
|
127
|
+
ref?: string;
|
|
128
|
+
globs: readonly string[];
|
|
129
|
+
}): Promise<string[]>;
|
|
130
|
+
readAllFiles(options?: FileReadOptions): Promise<Record<string, string>>;
|
|
131
|
+
readAllFilesAtRef(
|
|
132
|
+
ref: string,
|
|
133
|
+
options?: FileReadOptions,
|
|
134
|
+
): Promise<Record<string, string>>;
|
|
135
|
+
/**
|
|
136
|
+
* The files under one directory prefix at a ref (e.g. `.work/roles/`), without
|
|
137
|
+
* materializing the whole tree. `prefix` is a directory path with its
|
|
138
|
+
* trailing slash; results are keyed by full path.
|
|
139
|
+
*/
|
|
140
|
+
readFilesAtRef(
|
|
141
|
+
ref: string,
|
|
142
|
+
opts: { prefix: string },
|
|
143
|
+
): Promise<Record<string, string>>;
|
|
144
|
+
/** File paths at a ref, optionally under one directory prefix; no content. */
|
|
145
|
+
listFilesAtRef(ref: string, opts?: { prefix?: string }): Promise<string[]>;
|
|
146
|
+
/** One file's raw bytes at a ref, or null when absent (binaries intact). */
|
|
147
|
+
readBlobAtRef(
|
|
148
|
+
ref: string,
|
|
149
|
+
filePath: string,
|
|
150
|
+
options?: { maxBytes?: number },
|
|
151
|
+
): Promise<Uint8Array | null>;
|
|
152
|
+
/** One working-tree file's raw bytes, or null when absent. */
|
|
153
|
+
readFileBytes(filePath: string): Promise<Uint8Array | null>;
|
|
154
|
+
/** File paths + blob ids at a ref (content-addressed digests, no content). */
|
|
155
|
+
listBlobsAtRef(
|
|
156
|
+
ref: string,
|
|
157
|
+
opts?: { prefix?: string },
|
|
158
|
+
): Promise<Array<{ path: string; oid: string }>>;
|
|
159
|
+
|
|
160
|
+
/** Commit the working tree — or, with `paths`, only those files (added,
|
|
161
|
+
* modified or deleted), leaving everything else uncommitted. */
|
|
162
|
+
commit(
|
|
163
|
+
message: string,
|
|
164
|
+
author: { name: string; email: string },
|
|
165
|
+
opts?: { paths?: readonly string[]; allowEmpty?: boolean },
|
|
166
|
+
): Promise<string>;
|
|
167
|
+
log(options?: { maxCount?: number; ref?: string }): Promise<CommitInfo[]>;
|
|
168
|
+
resolveRef(ref?: string): Promise<string>;
|
|
169
|
+
|
|
170
|
+
checkout(ref?: string): Promise<void>;
|
|
171
|
+
|
|
172
|
+
status(): Promise<RepoStatus>;
|
|
173
|
+
currentBranch(): Promise<string>;
|
|
174
|
+
listBranches(): Promise<BranchInfo[]>;
|
|
175
|
+
createBranch(name: string, fromRef?: string): Promise<void>;
|
|
176
|
+
deleteBranch(name: string): Promise<void>;
|
|
177
|
+
hasBranch(name: string): Promise<boolean>;
|
|
178
|
+
|
|
179
|
+
workdirDiff(): Promise<DiffEntry[]>;
|
|
180
|
+
diff(opts: { base: string; head: string }): Promise<DiffEntry[]>;
|
|
181
|
+
resetWorkingTree(): Promise<void>;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Point a branch ref at `sha`, creating it if missing. Unlike
|
|
185
|
+
* {@link createBranch} this does not switch HEAD; use {@link checkout} after
|
|
186
|
+
* if needed.
|
|
187
|
+
*/
|
|
188
|
+
moveBranch(name: string, sha: string): Promise<void>;
|
|
189
|
+
|
|
190
|
+
dispose(): Promise<void>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Everything a git client (sandbox, CI job, local checkout) needs to clone a
|
|
195
|
+
* project directly from the remote backend. Credentials are typically
|
|
196
|
+
* short-lived scoped tokens — treat the value as ephemeral and request a
|
|
197
|
+
* fresh one per operation.
|
|
198
|
+
*/
|
|
199
|
+
export interface CloneSource {
|
|
200
|
+
url: string;
|
|
201
|
+
username?: string;
|
|
202
|
+
password?: string;
|
|
203
|
+
branch?: string;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* A backend that stores the canonical (shared) bare repository for a project.
|
|
208
|
+
* The "origin" in git terminology — contents are never checked out; only git
|
|
209
|
+
* objects + refs live here. Push/pull is orchestrated by the server using
|
|
210
|
+
* {@link RemoteBackend.withOrigin}.
|
|
211
|
+
*/
|
|
212
|
+
export interface RemoteBackend {
|
|
213
|
+
initRemote(tenantId: string, projectId: string): Promise<void>;
|
|
214
|
+
deleteRemote(tenantId: string, projectId: string): Promise<void>;
|
|
215
|
+
exists(tenantId: string, projectId: string): Promise<boolean>;
|
|
216
|
+
/**
|
|
217
|
+
* Invoke `fn` with an {@link OriginRepo} scoped to the project's bare repo.
|
|
218
|
+
* Implementations decide whether this is a local FS path or a synced mirror
|
|
219
|
+
* of a network remote (e.g. Cloudflare Artifacts); the interface is always
|
|
220
|
+
* the same.
|
|
221
|
+
*/
|
|
222
|
+
withOrigin<T>(
|
|
223
|
+
tenantId: string,
|
|
224
|
+
projectId: string,
|
|
225
|
+
fn: (origin: OriginRepo) => Promise<T>,
|
|
226
|
+
): Promise<T>;
|
|
227
|
+
/**
|
|
228
|
+
* Optional capability: backends whose origin is a real network git remote
|
|
229
|
+
* (Cloudflare Artifacts) can hand out a URL + short-lived credentials so
|
|
230
|
+
* sandboxes `git clone` the project directly instead of receiving file
|
|
231
|
+
* uploads from the host. FS-backed origins do not implement this.
|
|
232
|
+
*/
|
|
233
|
+
getCloneSource?(
|
|
234
|
+
tenantId: string,
|
|
235
|
+
projectId: string,
|
|
236
|
+
opts?: { scope?: "read" | "write" },
|
|
237
|
+
): Promise<CloneSource>;
|
|
238
|
+
/**
|
|
239
|
+
* Whether this origin can keep members' drafts (ADR 0191): ref updates
|
|
240
|
+
* and deletes that are atomic across every process using it, and draft
|
|
241
|
+
* refs no sandbox credential can read. Absent means it can.
|
|
242
|
+
*/
|
|
243
|
+
draftSupport?(): Promise<DraftSupport>;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export type DraftSupport =
|
|
247
|
+
| { supported: true }
|
|
248
|
+
| { supported: false; reason: string };
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Thin git-object-level interface over a bare repo used by {@link git-sync}.
|
|
252
|
+
* Only the primitives needed for server-orchestrated push/fetch are exposed.
|
|
253
|
+
*/
|
|
254
|
+
export interface OriginRepo {
|
|
255
|
+
readonly gitdir: string;
|
|
256
|
+
/** Resolve a ref (e.g. `refs/heads/main`) to a SHA, or null if missing. */
|
|
257
|
+
resolveRef(ref: string): Promise<string | null>;
|
|
258
|
+
/** List refs under a prefix (e.g. `refs/heads/`) with their SHAs. */
|
|
259
|
+
listRefs(prefix: string): Promise<{ ref: string; sha: string }[]>;
|
|
260
|
+
/**
|
|
261
|
+
* Update a ref to a new SHA. With `expected` (`null`: the ref must not
|
|
262
|
+
* exist) the update is a compare-and-swap that throws
|
|
263
|
+
* {@link RefMovedError} when the ref holds something else.
|
|
264
|
+
*/
|
|
265
|
+
updateRef(opts: {
|
|
266
|
+
ref: string;
|
|
267
|
+
sha: string;
|
|
268
|
+
expected?: string | null;
|
|
269
|
+
}): Promise<void>;
|
|
270
|
+
/**
|
|
271
|
+
* Remove one ref without deleting its immutable objects. Missing is a
|
|
272
|
+
* no-op; with `expected`, throws {@link RefMovedError} unless the ref
|
|
273
|
+
* still holds that SHA.
|
|
274
|
+
*/
|
|
275
|
+
deleteRef(input: { ref: string; expected?: string }): Promise<void>;
|
|
276
|
+
/** Whether the object is present locally. */
|
|
277
|
+
hasObject(sha: string): Promise<boolean>;
|
|
278
|
+
/** Read the raw git object (returns { type, data }). */
|
|
279
|
+
readObject(sha: string): Promise<{
|
|
280
|
+
type: "blob" | "tree" | "commit" | "tag";
|
|
281
|
+
data: Uint8Array;
|
|
282
|
+
}>;
|
|
283
|
+
/** Write a git object with a known type; returns the resolved SHA. */
|
|
284
|
+
writeObject(opts: {
|
|
285
|
+
type: "blob" | "tree" | "commit" | "tag";
|
|
286
|
+
data: Uint8Array;
|
|
287
|
+
}): Promise<string>;
|
|
288
|
+
log(ref: string, maxCount?: number): Promise<CommitInfo[]>;
|
|
289
|
+
}
|