@catamorphic/git 0.0.0-stage → 0.1.0-alpha.30
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,252 @@
|
|
|
1
|
+
import nodeFs from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import git from "isomorphic-git";
|
|
5
|
+
import { RefMovedError } from "./ref-moved-error.js";
|
|
6
|
+
import { assertResolvableRef, assertValidRefName } from "./ref-names.js";
|
|
7
|
+
import type { CommitInfo, OriginRepo, RemoteBackend } from "./types.js";
|
|
8
|
+
|
|
9
|
+
const UUID_RE =
|
|
10
|
+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
11
|
+
|
|
12
|
+
function assertUuid(value: string): void {
|
|
13
|
+
if (!UUID_RE.test(value)) {
|
|
14
|
+
throw new Error(`Invalid UUID: ${value}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
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.
|
|
22
|
+
*/
|
|
23
|
+
const refLocks = new Map<string, Promise<unknown>>();
|
|
24
|
+
|
|
25
|
+
async function withRefLock<T>(key: string, fn: () => Promise<T>): Promise<T> {
|
|
26
|
+
const previous = refLocks.get(key) ?? Promise.resolve();
|
|
27
|
+
const run = previous.catch(() => {}).then(fn);
|
|
28
|
+
refLocks.set(key, run);
|
|
29
|
+
try {
|
|
30
|
+
return await run;
|
|
31
|
+
} finally {
|
|
32
|
+
if (refLocks.get(key) === run) refLocks.delete(key);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class FsRemoteBackend implements RemoteBackend {
|
|
37
|
+
constructor(private readonly basePath: string) {}
|
|
38
|
+
|
|
39
|
+
private resolveBarePath(tenantId: string, projectId: string): string {
|
|
40
|
+
assertUuid(tenantId);
|
|
41
|
+
assertUuid(projectId);
|
|
42
|
+
const barePath = path.join(this.basePath, tenantId, `${projectId}.git`);
|
|
43
|
+
if (!path.resolve(barePath).startsWith(path.resolve(this.basePath))) {
|
|
44
|
+
throw new Error("Path traversal detected");
|
|
45
|
+
}
|
|
46
|
+
return barePath;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async initRemote(tenantId: string, projectId: string): Promise<void> {
|
|
50
|
+
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
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async deleteRemote(tenantId: string, projectId: string): Promise<void> {
|
|
61
|
+
const barePath = this.resolveBarePath(tenantId, projectId);
|
|
62
|
+
await fs.rm(barePath, { recursive: true, force: true });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async exists(tenantId: string, projectId: string): Promise<boolean> {
|
|
66
|
+
const barePath = this.resolveBarePath(tenantId, projectId);
|
|
67
|
+
try {
|
|
68
|
+
await fs.access(barePath);
|
|
69
|
+
return true;
|
|
70
|
+
} catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async withOrigin<T>(
|
|
76
|
+
tenantId: string,
|
|
77
|
+
projectId: string,
|
|
78
|
+
fn: (origin: OriginRepo) => Promise<T>,
|
|
79
|
+
): Promise<T> {
|
|
80
|
+
const barePath = this.resolveBarePath(tenantId, projectId);
|
|
81
|
+
const origin = new FsOriginRepo(barePath);
|
|
82
|
+
return fn(origin);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
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.
|
|
90
|
+
*/
|
|
91
|
+
export class FsOriginRepo implements OriginRepo {
|
|
92
|
+
private readonly cache = {};
|
|
93
|
+
constructor(readonly gitdir: string) {}
|
|
94
|
+
|
|
95
|
+
async resolveRef(ref: string): Promise<string | null> {
|
|
96
|
+
assertResolvableRef(ref);
|
|
97
|
+
try {
|
|
98
|
+
return await git.resolveRef({ fs: nodeFs, gitdir: this.gitdir, ref });
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async listRefs(prefix: string): Promise<{ ref: string; sha: string }[]> {
|
|
105
|
+
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;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async updateRef(opts: {
|
|
127
|
+
ref: string;
|
|
128
|
+
sha: string;
|
|
129
|
+
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
|
+
}): Promise<void> {
|
|
141
|
+
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,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
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
|
+
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 });
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
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
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async readObject(sha: string): Promise<{
|
|
199
|
+
type: "blob" | "tree" | "commit" | "tag";
|
|
200
|
+
data: Uint8Array;
|
|
201
|
+
}> {
|
|
202
|
+
const obj = await git.readObject({
|
|
203
|
+
fs: nodeFs,
|
|
204
|
+
gitdir: this.gitdir,
|
|
205
|
+
oid: sha,
|
|
206
|
+
format: "content",
|
|
207
|
+
cache: this.cache,
|
|
208
|
+
});
|
|
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
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async writeObject(opts: {
|
|
219
|
+
type: "blob" | "tree" | "commit" | "tag";
|
|
220
|
+
data: Uint8Array;
|
|
221
|
+
}): Promise<string> {
|
|
222
|
+
return git.writeObject({
|
|
223
|
+
fs: nodeFs,
|
|
224
|
+
gitdir: this.gitdir,
|
|
225
|
+
type: opts.type,
|
|
226
|
+
object: opts.data,
|
|
227
|
+
format: "content",
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
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,
|
|
245
|
+
},
|
|
246
|
+
timestamp: c.commit.author.timestamp,
|
|
247
|
+
}));
|
|
248
|
+
} catch {
|
|
249
|
+
return [];
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
export type GitObjectType = "blob" | "tree" | "commit" | "tag";
|
|
4
|
+
|
|
5
|
+
const OBJECT_TYPES: readonly GitObjectType[] = [
|
|
6
|
+
"blob",
|
|
7
|
+
"tree",
|
|
8
|
+
"commit",
|
|
9
|
+
"tag",
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Encode a git object in "wrapped" form (`<type> <length>\0<content>`) — the
|
|
14
|
+
* uncompressed loose-object layout. Stored bodies are therefore
|
|
15
|
+
* self-describing, and `sha1(wrapped)` equals the git oid, so stored objects
|
|
16
|
+
* are verifiable by key alone.
|
|
17
|
+
*/
|
|
18
|
+
export function wrapObject(opts: { type: GitObjectType; data: Uint8Array }): {
|
|
19
|
+
wrapped: Uint8Array;
|
|
20
|
+
sha: string;
|
|
21
|
+
} {
|
|
22
|
+
const header = new TextEncoder().encode(
|
|
23
|
+
`${opts.type} ${opts.data.byteLength}\0`,
|
|
24
|
+
);
|
|
25
|
+
const wrapped = new Uint8Array(header.byteLength + opts.data.byteLength);
|
|
26
|
+
wrapped.set(header, 0);
|
|
27
|
+
wrapped.set(opts.data, header.byteLength);
|
|
28
|
+
const sha = createHash("sha1").update(wrapped).digest("hex");
|
|
29
|
+
return { wrapped, sha };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function unwrapObject(wrapped: Uint8Array): {
|
|
33
|
+
type: GitObjectType;
|
|
34
|
+
data: Uint8Array;
|
|
35
|
+
} {
|
|
36
|
+
const nullIdx = wrapped.indexOf(0);
|
|
37
|
+
if (nullIdx === -1) {
|
|
38
|
+
throw new Error("Malformed git object: missing header terminator");
|
|
39
|
+
}
|
|
40
|
+
const header = new TextDecoder().decode(wrapped.slice(0, nullIdx));
|
|
41
|
+
const spaceIdx = header.indexOf(" ");
|
|
42
|
+
const type = header.slice(0, spaceIdx);
|
|
43
|
+
if (!OBJECT_TYPES.includes(type as GitObjectType)) {
|
|
44
|
+
throw new Error(`Malformed git object: unknown type '${type}'`);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
type: type as GitObjectType,
|
|
48
|
+
data: wrapped.slice(nullIdx + 1),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ParsedCommit {
|
|
53
|
+
tree: string;
|
|
54
|
+
parents: string[];
|
|
55
|
+
author: { name: string; email: string; timestamp: number };
|
|
56
|
+
message: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Parse a commit object's content (author line, parents, message). */
|
|
60
|
+
export function parseCommit(data: Uint8Array): ParsedCommit {
|
|
61
|
+
const text = new TextDecoder().decode(data);
|
|
62
|
+
const headerEnd = text.indexOf("\n\n");
|
|
63
|
+
const header = headerEnd === -1 ? text : text.slice(0, headerEnd);
|
|
64
|
+
const message = headerEnd === -1 ? "" : text.slice(headerEnd + 2);
|
|
65
|
+
|
|
66
|
+
const parents: string[] = [];
|
|
67
|
+
const treeLine = header.split("\n").find((line) => line.startsWith("tree "));
|
|
68
|
+
const authorLine = header
|
|
69
|
+
.split("\n")
|
|
70
|
+
.find((line) => line.startsWith("author "));
|
|
71
|
+
for (const line of header.split("\n")) {
|
|
72
|
+
if (line.startsWith("parent ")) parents.push(line.slice(7).trim());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// `author Name <email> <unix-ts> <tz>`
|
|
76
|
+
const authorMatch = authorLine?.match(/^author (.*) <(.*)> (\d+) [+-]\d{4}$/);
|
|
77
|
+
return {
|
|
78
|
+
tree: treeLine?.slice(5).trim() ?? "",
|
|
79
|
+
parents,
|
|
80
|
+
author: {
|
|
81
|
+
name: authorMatch?.[1] ?? "",
|
|
82
|
+
email: authorMatch?.[2] ?? "",
|
|
83
|
+
timestamp: authorMatch ? Number(authorMatch[3]) : 0,
|
|
84
|
+
},
|
|
85
|
+
message,
|
|
86
|
+
};
|
|
87
|
+
}
|