Lines 3–16 at 180 (were 3–11 at 179)
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 180 (were 33–60 at 179)
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 180 (were 152–160 at 179)
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 196–202 at 180 (were 187–192 at 179)
187196 for (const [path, f] of old.files) if (!files.has(path) && path in blobs) files.set(path, f); 188197 const tree = await writeTrees(fs, dir, files, old.trees, changed); 189198 parent = await git.commit({ Lines 236–294 at 180 (were 226–252 at 179)
226236 shared: SharedFiles, 227237 /** A binary file's bytes by SHA-256 (#27), from the project's blob store. */ 228238 readBlob: (sha256: string) => Uint8Array | null = () => null, 239+ disk?: { storage: SqliteStorage; maxPackBytes: number; maxObjectBytes: number; maxDecodedBytes: number }, 229240): Promise<PushResult> { 230241 const t0 = Date.now(); 231242 const name = repoName(project); 232243 const { remote, secret } = await writeAccess(artifacts, name); 233244 const onAuth = () => ({ username: "x", password: secret }); 234− let fs = new MemoryFS();
245+ using contents = disk ? new GitFileContents(disk.storage, disk.maxObjectBytes, disk.maxDecodedBytes) : undefined; 247+ ? new GitDiskIntake(disk.storage, disk.maxPackBytes, disk.maxObjectBytes, disk.maxDecodedBytes) 249+ const cache = disk ? {} : undefined; 250+ if (intake && cache) intake.attach(cache); 251+ const transport = disk ? gitHttp() : http; 252+ let fs = new MemoryFS(contents); 235253 const dir = "/repo"; 236254 // A new repo has no main yet. isomorphic-git "clones" an empty repo without 237255 // an error but leaves HEAD on master, so check for main and start clean. 238256 // 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);
257+ // Only an explicitly absent main is an empty repository. Transport, decode, 258+ // and size failures must never become permission to replace remote history. 259+ const refs = await git.listServerRefs({ http: transport, url: remote, onAuth, prefix: "refs/heads/main" }); 260+ let head: string | null = null; 261+ if (refs.some((ref) => ref.ref === "refs/heads/main")) { 274+ intake?.assertReady(); 275+ head = await git.resolveRef({ fs, dir, ref: "main" }); 243277 if (head === null) { 244− fs = new MemoryFS();
278+ fs = new MemoryFS(contents); 245279 await git.init({ fs, dir, defaultBranch: "main" }); 247281 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()));
282+ const push = () => git.push({ fs, http: transport, cache, dir, url: remote, ref: "main", remoteRef: "main", onAuth }); 283+ const { commit, changed } = await commitShared( 289+ async () => void (await push()), 250292 const t2 = Date.now();