{"draft":null,"from":175,"to":318,"files":[{"path":"server/src/push.ts","added":78,"removed":29,"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@@ -170,23 +179,19 @@\n \t// Each file goes into the tree as a blob with its mode (#27): 100755 for exec, 120000 for a link, whose blob is\n \t// its target.\n \tconst enc = new TextEncoder();\n-\tfor (const path of Object.keys(shared.files)) {\n-\t\tconst mode = shared.modes?.[path];\n-\t\tconst link = typeof mode?.link === \"string\" ? mode.link : null;\n-\t\tconst bytes = enc.encode(link ?? shared.files[path]!);\n-\t\tawait put(path, bytes, link !== null ? \"120000\" : mode?.exec ? \"100755\" : \"100644\", await blobOid(bytes));\n-\t}\n \tconst agent = shared.last?.agent ?? \"abso\";\n \tconst author = { name: agent, email: `${agent}@abso.invalid` };\n \tconst committer = { name: \"abso\", email: \"abso@abso.invalid\" };\n \tconst say = (p: string) => `${shared.last?.message ?? \"Start the project\"}${p}\\n\\nabso-version: ${shared.version}\\n`;\n \tlet parent = head;\n-\t// A part holds what's written so far, and the last push's entries for the binary files not reached yet.\n+\t// A part holds what's written so far and preserves old entries for files not reached yet.\n \tconst commitNow = async (message: string) => {\n \t\tconst files = new Map(now);\n-\t\tfor (const [path, f] of old.files) if (!files.has(path) && path in blobs) files.set(path, f);\n+\t\tfor (const [path, f] of old.files)\n+\t\t\tif (!files.has(path) && (path in blobs || path in shared.files)) 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@@ -198,12 +203,24 @@\n \t\t});\n \t\treturn parent;\n \t};\n-\t// Binary files (#27) aren't in files: their bytes come from the blob store.\n-\t// Artifacts refuses a push much over 20 MB, so when the new blobs add up\n+\t// Artifacts refuses a push much over 20 MB, so when new text or binary blobs add up\n \t// to more than PUSH_BATCH, the earlier ones go in commits of their own\n \t// first. Every part carries abso-version, so intake never takes them in.\n \tlet pending = 0;\n \tlet parts = 0;\n+\tfor (const path of Object.keys(shared.files)) {\n+\t\tconst mode = shared.modes?.[path];\n+\t\tconst link = typeof mode?.link === \"string\" ? mode.link : null;\n+\t\tconst bytes = enc.encode(link ?? shared.files[path]!);\n+\t\tconst oid = await blobOid(bytes);\n+\t\tconst fresh = old.files.get(path)?.oid !== oid;\n+\t\tif (fresh && pending > 0 && pending + bytes.byteLength > PUSH_BATCH) {\n+\t\t\tawait part(await commitNow(say(` (part ${++parts})`)));\n+\t\t\tpending = 0;\n+\t\t}\n+\t\tif (fresh) pending += bytes.byteLength;\n+\t\tawait put(path, bytes, link !== null ? \"120000\" : mode?.exec ? \"100755\" : \"100644\", oid);\n+\t}\n \tfor (const [path, sha] of Object.entries(blobs)) {\n \t\tconst bytes = readBlob(sha);\n \t\tif (!bytes) throw new Error(`${path}: blob ${sha} is missing from the project's store`);\n@@ -226,27 +243,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":[]}