Lines 3–16 at 318 (were 3–11 at 156)
33// is created on first use. isomorphic-git works in an in-memory filesystem: 44// a depth-1 clone of main with nothing checked out, then a new tree from 55// shared's files that compresses and writes only what changed (abso issue 293). 6+import { createHash } from "node:crypto"; 67import git from "isomorphic-git"; 78import http from "isomorphic-git/http/web"; 89import type { FileModes } from "../../worker/project/project-api.ts"; 10+import type { SqliteStorage } from "./engine-project.ts"; 11+import { GitDiskIntake } from "./git-disk-intake.ts"; 12+import { GitFileContents } from "./git-file-contents.ts"; 13+import { gitHttp } from "./git-http.ts"; 914import { MemoryFS } from "./memory-fs.ts"; 1116export interface PushResult { Lines 38–60 at 318 (were 33–60 at 156)
3439 files = new Map<string, TreeFile>(), 3540 trees = new Map<string, string>(), 3642): Promise<{ files: Map<string, TreeFile>; trees: Map<string, string> }> { 3743 trees.set(prefix, oid); 38− const { tree } = await git.readTree({ fs, dir, oid });
44+ const { tree } = await git.readTree({ fs, dir, oid, cache }); 3945 for (const e of tree) { 4046 const path = prefix === "" ? e.path : `${prefix}/${e.path}`; 41− if (e.type === "tree") await readTreeFiles(fs, dir, e.oid, path, files, trees);
47+ if (e.type === "tree") await readTreeFiles(fs, dir, e.oid, path, files, trees, cache); 4248 else if (e.type === "blob") files.set(path, { oid: e.oid, mode: e.mode }); 4450 return { files, trees }; 48− * A blob's git oid, with the platform's SHA-1 (abso issue 293): native and awaited, so hashing every file of a big
49− * project neither burns the object's CPU in JavaScript nor holds its other requests back for long.
54+ * Hash the Git header and content separately so export does not allocate a second whole-file buffer. 5156export async function blobOid(bytes: Uint8Array): Promise<string> { 52− const head = new TextEncoder().encode(`blob ${bytes.byteLength}\0`);
53− const all = new Uint8Array(head.byteLength + bytes.byteLength);
54− all.set(head);
55− all.set(bytes, head.byteLength);
56− const d = new Uint8Array(await crypto.subtle.digest("SHA-1", all));
57− return [...d].map((b) => b.toString(16).padStart(2, "0")).join("");
57+ return createHash("sha1").update(`blob ${bytes.byteLength}\0`).update(bytes).digest("hex"); 6060const parentOf = (path: string) => { Lines 152–169 at 318 (were 152–160 at 156)
152152 shared: SharedFiles, 153153 readBlob: (sha256: string) => Uint8Array | null, 154154 part: (commit: string) => Promise<void> = async () => {}, 155156): Promise<{ commit: string; changed: number }> { 157− ? await readTreeFiles(fs, dir, (await git.readCommit({ fs, dir, oid: head })).commit.tree)
158+ ? await readTreeFiles( 161+ (await git.readCommit({ fs, dir, oid: head, cache })).commit.tree, 158167 : { files: new Map<string, TreeFile>(), trees: new Map<string, string>() }; 159168 const blobs = shared.blobs ?? {}; 160169 const now = new Map<string, TreeFile>(); Lines 179–197 at 318 (were 170–192 at 156)
170179 // Each file goes into the tree as a blob with its mode (#27): 100755 for exec, 120000 for a link, whose blob is 172181 const enc = new TextEncoder(); 173− for (const [path, text] of Object.entries(shared.files)) {
174− const mode = shared.modes?.[path];
175− const link = typeof mode?.link === "string" ? mode.link : null;
176− const bytes = enc.encode(link ?? text);
177− await put(path, bytes, link !== null ? "120000" : mode?.exec ? "100755" : "100644", await blobOid(bytes));
178− }
179182 const agent = shared.last?.agent ?? "abso"; 180183 const author = { name: agent, email: `${agent}@abso.invalid` }; 181184 const committer = { name: "abso", email: "abso@abso.invalid" }; 182185 const say = (p: string) => `${shared.last?.message ?? "Start the project"}${p}\n\nabso-version: ${shared.version}\n`; 183186 let parent = head; 184− // A part holds what's written so far, and the last push's entries for the binary files not reached yet.
187+ // A part holds what's written so far and preserves old entries for files not reached yet. 185188 const commitNow = async (message: string) => { 186189 const files = new Map(now); 187− for (const [path, f] of old.files) if (!files.has(path) && path in blobs) files.set(path, f);
190+ for (const [path, f] of old.files) 191+ if (!files.has(path) && (path in blobs || path in shared.files)) files.set(path, f); 188192 const tree = await writeTrees(fs, dir, files, old.trees, changed); 189193 parent = await git.commit({ Lines 203–226 at 318 (were 198–209 at 156)
201− // Binary files (#27) aren't in files: their bytes come from the blob store.
202− // Artifacts refuses a push much over 20 MB, so when the new blobs add up
206+ // Artifacts refuses a push much over 20 MB, so when new text or binary blobs add up 203207 // to more than PUSH_BATCH, the earlier ones go in commits of their own 204208 // first. Every part carries abso-version, so intake never takes them in. 211+ for (const path of Object.keys(shared.files)) { 212+ const mode = shared.modes?.[path]; 213+ const link = typeof mode?.link === "string" ? mode.link : null; 214+ const bytes = enc.encode(link ?? shared.files[path]!); 215+ const oid = await blobOid(bytes); 216+ const fresh = old.files.get(path)?.oid !== oid; 217+ if (fresh && pending > 0 && pending + bytes.byteLength > PUSH_BATCH) { 218+ await part(await commitNow(say(` (part ${++parts})`))); 221+ if (fresh) pending += bytes.byteLength; 222+ await put(path, bytes, link !== null ? "120000" : mode?.exec ? "100755" : "100644", oid); 207224 for (const [path, sha] of Object.entries(blobs)) { 208225 const bytes = readBlob(sha); 209226 if (!bytes) throw new Error(`${path}: blob ${sha} is missing from the project's store`); Lines 243–301 at 318 (were 226–252 at 156)
226243 shared: SharedFiles, 227244 /** A binary file's bytes by SHA-256 (#27), from the project's blob store. */ 228245 readBlob: (sha256: string) => Uint8Array | null = () => null, 246+ disk?: { storage: SqliteStorage; maxPackBytes: number; maxObjectBytes: number; maxDecodedBytes: number }, 229247): Promise<PushResult> { 230248 const t0 = Date.now(); 231249 const name = repoName(project); 232250 const { remote, secret } = await writeAccess(artifacts, name); 233251 const onAuth = () => ({ username: "x", password: secret }); 234− let fs = new MemoryFS();
252+ using contents = disk ? new GitFileContents(disk.storage, disk.maxObjectBytes, disk.maxDecodedBytes) : undefined; 254+ ? new GitDiskIntake(disk.storage, disk.maxPackBytes, disk.maxObjectBytes, disk.maxDecodedBytes) 256+ const cache = disk ? {} : undefined; 257+ if (intake && cache) intake.attach(cache); 258+ const transport = disk ? gitHttp() : http; 259+ let fs = new MemoryFS(contents); 235260 const dir = "/repo"; 236261 // A new repo has no main yet. isomorphic-git "clones" an empty repo without 237262 // an error but leaves HEAD on master, so check for main and start clean. 238263 // Nothing is checked out: the new tree is built from tree objects (293). 239− const head = await git
240− .clone({ fs, http, dir, url: remote, ref: "main", singleBranch: true, depth: 1, noCheckout: true, onAuth })
241− .then(() => git.resolveRef({ fs, dir, ref: "main" }))
242− .catch(() => null);
264+ // Only an explicitly absent main is an empty repository. Transport, decode, 265+ // and size failures must never become permission to replace remote history. 266+ const refs = await git.listServerRefs({ http: transport, url: remote, onAuth, prefix: "refs/heads/main" }); 267+ let head: string | null = null; 268+ if (refs.some((ref) => ref.ref === "refs/heads/main")) { 281+ intake?.assertReady(); 282+ head = await git.resolveRef({ fs, dir, ref: "main" }); 243284 if (head === null) { 244− fs = new MemoryFS();
285+ fs = new MemoryFS(contents); 245286 await git.init({ fs, dir, defaultBranch: "main" }); 247288 const t1 = Date.now(); 248− const push = () => git.push({ fs, http, dir, url: remote, ref: "main", remoteRef: "main", onAuth });
249− const { commit, changed } = await commitShared(fs, dir, head, shared, readBlob, async () => void (await push()));
289+ const push = () => git.push({ fs, http: transport, cache, dir, url: remote, ref: "main", remoteRef: "main", onAuth }); 290+ const { commit, changed } = await commitShared( 296+ async () => void (await push()), 250299 const t2 = Date.now();