{"draft":null,"from":179,"to":180,"files":[{"path":"server/src/push.ts","added":61,"removed":19,"diff":"--- a/server/src/push.ts\n+++ b/server/src/push.ts\n@@ -3,9 +3,14 @@\n // is created on first use. isomorphic-git works in an in-memory filesystem:\n // a depth-1 clone of main with nothing checked out, then a new tree from\n // shared's files that compresses and writes only what changed (abso issue 293).\n+import { createHash } from \"node:crypto\";\n import git from \"isomorphic-git\";\n import http from \"isomorphic-git/http/web\";\n import type { FileModes } from \"../../worker/project/project-api.ts\";\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+import { GitDiskIntake } from \"./git-disk-intake.ts\";\n+import { GitFileContents } from \"./git-file-contents.ts\";\n+import { gitHttp } from \"./git-http.ts\";\n import { MemoryFS } from \"./memory-fs.ts\";\n \n export interface PushResult {\n@@ -33,28 +38,23 @@\n \tprefix = \"\",\n \tfiles = new Map<string, TreeFile>(),\n \ttrees = new Map<string, string>(),\n+\tcache?: object,\n ): Promise<{ files: Map<string, TreeFile>; trees: Map<string, string> }> {\n \ttrees.set(prefix, oid);\n-\tconst { tree } = await git.readTree({ fs, dir, oid });\n+\tconst { tree } = await git.readTree({ fs, dir, oid, cache });\n \tfor (const e of tree) {\n \t\tconst path = prefix === \"\" ? e.path : `${prefix}/${e.path}`;\n-\t\tif (e.type === \"tree\") await readTreeFiles(fs, dir, e.oid, path, files, trees);\n+\t\tif (e.type === \"tree\") await readTreeFiles(fs, dir, e.oid, path, files, trees, cache);\n \t\telse if (e.type === \"blob\") files.set(path, { oid: e.oid, mode: e.mode });\n \t}\n \treturn { files, trees };\n }\n \n /**\n- * A blob's git oid, with the platform's SHA-1 (abso issue 293): native and awaited, so hashing every file of a big\n- * project neither burns the object's CPU in JavaScript nor holds its other requests back for long.\n+ * Hash the Git header and content separately so export does not allocate a second whole-file buffer.\n  */\n export async function blobOid(bytes: Uint8Array): Promise<string> {\n-\tconst head = new TextEncoder().encode(`blob ${bytes.byteLength}\\0`);\n-\tconst all = new Uint8Array(head.byteLength + bytes.byteLength);\n-\tall.set(head);\n-\tall.set(bytes, head.byteLength);\n-\tconst d = new Uint8Array(await crypto.subtle.digest(\"SHA-1\", all));\n-\treturn [...d].map((b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n+\treturn createHash(\"sha1\").update(`blob ${bytes.byteLength}\\0`).update(bytes).digest(\"hex\");\n }\n \n const parentOf = (path: string) => {\n@@ -152,9 +152,18 @@\n \tshared: SharedFiles,\n \treadBlob: (sha256: string) => Uint8Array | null,\n \tpart: (commit: string) => Promise<void> = async () => {},\n+\tcache?: object,\n ): Promise<{ commit: string; changed: number }> {\n \tconst old = head\n-\t\t? await readTreeFiles(fs, dir, (await git.readCommit({ fs, dir, oid: head })).commit.tree)\n+\t\t? await readTreeFiles(\n+\t\t\t\tfs,\n+\t\t\t\tdir,\n+\t\t\t\t(await git.readCommit({ fs, dir, oid: head, cache })).commit.tree,\n+\t\t\t\t\"\",\n+\t\t\t\tnew Map(),\n+\t\t\t\tnew Map(),\n+\t\t\t\tcache,\n+\t\t\t)\n \t\t: { files: new Map<string, TreeFile>(), trees: new Map<string, string>() };\n \tconst blobs = shared.blobs ?? {};\n \tconst now = new Map<string, TreeFile>();\n@@ -187,6 +196,7 @@\n \t\tfor (const [path, f] of old.files) if (!files.has(path) && path in blobs) files.set(path, f);\n \t\tconst tree = await writeTrees(fs, dir, files, old.trees, changed);\n \t\tparent = await git.commit({\n+\t\t\tcache,\n \t\t\tfs,\n \t\t\tdir,\n \t\t\tmessage,\n@@ -226,27 +236,59 @@\n \tshared: SharedFiles,\n \t/** A binary file's bytes by SHA-256 (#27), from the project's blob store. */\n \treadBlob: (sha256: string) => Uint8Array | null = () => null,\n+\tdisk?: { storage: SqliteStorage; maxPackBytes: number; maxObjectBytes: number; maxDecodedBytes: number },\n ): Promise<PushResult> {\n \tconst t0 = Date.now();\n \tconst name = repoName(project);\n \tconst { remote, secret } = await writeAccess(artifacts, name);\n \tconst onAuth = () => ({ username: \"x\", password: secret });\n-\tlet fs = new MemoryFS();\n+\tusing contents = disk ? new GitFileContents(disk.storage, disk.maxObjectBytes, disk.maxDecodedBytes) : undefined;\n+\tusing intake = disk\n+\t\t? new GitDiskIntake(disk.storage, disk.maxPackBytes, disk.maxObjectBytes, disk.maxDecodedBytes)\n+\t\t: undefined;\n+\tconst cache = disk ? {} : undefined;\n+\tif (intake && cache) intake.attach(cache);\n+\tconst transport = disk ? gitHttp() : http;\n+\tlet fs = new MemoryFS(contents);\n \tconst dir = \"/repo\";\n \t// A new repo has no main yet. isomorphic-git \"clones\" an empty repo without\n \t// an error but leaves HEAD on master, so check for main and start clean.\n \t// Nothing is checked out: the new tree is built from tree objects (293).\n-\tconst head = await git\n-\t\t.clone({ fs, http, dir, url: remote, ref: \"main\", singleBranch: true, depth: 1, noCheckout: true, onAuth })\n-\t\t.then(() => git.resolveRef({ fs, dir, ref: \"main\" }))\n-\t\t.catch(() => null);\n+\t// Only an explicitly absent main is an empty repository. Transport, decode,\n+\t// and size failures must never become permission to replace remote history.\n+\tconst refs = await git.listServerRefs({ http: transport, url: remote, onAuth, prefix: \"refs/heads/main\" });\n+\tlet head: string | null = null;\n+\tif (refs.some((ref) => ref.ref === \"refs/heads/main\")) {\n+\t\tawait git.clone({\n+\t\t\tfs,\n+\t\t\thttp: transport,\n+\t\t\tcache,\n+\t\t\tdir,\n+\t\t\turl: remote,\n+\t\t\tref: \"main\",\n+\t\t\tsingleBranch: true,\n+\t\t\tdepth: 1,\n+\t\t\tnoCheckout: true,\n+\t\t\tonAuth,\n+\t\t});\n+\t\tintake?.assertReady();\n+\t\thead = await git.resolveRef({ fs, dir, ref: \"main\" });\n+\t}\n \tif (head === null) {\n-\t\tfs = new MemoryFS();\n+\t\tfs = new MemoryFS(contents);\n \t\tawait git.init({ fs, dir, defaultBranch: \"main\" });\n \t}\n \tconst t1 = Date.now();\n-\tconst push = () => git.push({ fs, http, dir, url: remote, ref: \"main\", remoteRef: \"main\", onAuth });\n-\tconst { commit, changed } = await commitShared(fs, dir, head, shared, readBlob, async () => void (await push()));\n+\tconst push = () => git.push({ fs, http: transport, cache, dir, url: remote, ref: \"main\", remoteRef: \"main\", onAuth });\n+\tconst { commit, changed } = await commitShared(\n+\t\tfs,\n+\t\tdir,\n+\t\thead,\n+\t\tshared,\n+\t\treadBlob,\n+\t\tasync () => void (await push()),\n+\t\tcache,\n+\t);\n \tconst t2 = Date.now();\n \tawait push();\n \tconsole.log("}],"unmatched":[]}