{"draft":null,"from":175,"to":318,"files":[{"path":"server/src/ingest.ts","added":115,"removed":14,"diff":"--- a/server/src/ingest.ts\n+++ b/server/src/ingest.ts\n@@ -2,12 +2,17 @@\n // project's Artifacts repo, read back for project.ingest. Every commit the\n // Worker pushes ends \"abso-version: N\" (\"avcs-version: N\" before the rename); the commits above the newest such\n // commit that haven't been taken in yet are the person's, and N is their base.\n-import git from \"isomorphic-git\";\n-import http from \"isomorphic-git/http/web\";\n+import git, { type HttpClient } from \"isomorphic-git\";\n import type { FileModes } from \"../../worker/project/project-api.ts\";\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+import { gitBlobText, InvalidGitUtf8 } from \"./git-blob-text.ts\";\n+import { GitDiskIntake } from \"./git-disk-intake.ts\";\n+import { gitHttp } from \"./git-http.ts\";\n import { MemoryFS } from \"./memory-fs.ts\";\n import { repoName } from \"./push.ts\";\n \n+const http = gitHttp();\n+\n const EXEC = 0o100755;\n const LINK = 0o120000;\n \n@@ -40,7 +45,10 @@\n async function readAccess(\n \tartifacts: Artifacts,\n \tproject: string,\n-): Promise<{ url: string; onAuth: () => { username: string; password: string } } | null> {\n+): Promise<{\n+\turl: string;\n+\tonAuth: () => { username: string; password: string };\n+} | null> {\n \tlet repo: Awaited<ReturnType<Artifacts[\"get\"]>>;\n \ttry {\n \t\trepo = await artifacts.get(repoName(project));\n@@ -57,7 +65,12 @@\n export async function remoteHead(artifacts: Artifacts, project: string): Promise<string | null> {\n \tconst access = await readAccess(artifacts, project);\n \tif (!access) return null;\n-\tconst refs = await git.listServerRefs({ http, url: access.url, onAuth: access.onAuth, prefix: \"refs/heads/main\" });\n+\tconst refs = await git.listServerRefs({\n+\t\thttp,\n+\t\turl: access.url,\n+\t\tonAuth: access.onAuth,\n+\t\tprefix: \"refs/heads/main\",\n+\t});\n \treturn refs.find((r) => r.ref === \"refs/heads/main\")?.oid ?? null;\n }\n \n@@ -67,6 +80,12 @@\n  */\n /** Stores a binary file's bytes and answers their SHA-256, or null when they're over the limit. */\n export type StoreBlob = (bytes: Uint8Array) => Promise<string | null>;\n+export type RetainFileChunks = (\n+\tfiles: Record<string, string>,\n+\tpath: string,\n+\tchunks: Iterable<string>,\n+\toid: string,\n+) => void;\n \n /**\n  * `oldBlob` gives shared's blob for a path: a binary file over the limit\n@@ -78,14 +97,36 @@\n \tseen: string | undefined,\n \tstoreBlob?: StoreBlob,\n \toldBlob: (path: string) => string | undefined = () => undefined,\n+\tretainFile: (files: Record<string, string>, path: string, body: string, oid?: string) => void = (\n+\t\tfiles,\n+\t\tpath,\n+\t\tbody,\n+\t) => {\n+\t\tfiles[path] = body;\n+\t},\n+\tdisk?: {\n+\t\tstorage: SqliteStorage;\n+\t\tmaxPackBytes: number;\n+\t\tmaxObjectBytes: number;\n+\t\tmaxDecodedBytes?: number;\n+\t\tmaxBinaryBytes?: number;\n+\t\thttp?: HttpClient;\n+\t\tretainFileChunks?: RetainFileChunks;\n+\t},\n ): Promise<PushedCommits | null> {\n \tconst access = await readAccess(artifacts, project);\n \tif (!access) return null;\n \tconst fs = new MemoryFS();\n \tconst dir = \"/repo\";\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 \tawait git.clone({\n \t\tfs,\n-\t\thttp,\n+\t\tcache,\n+\t\thttp: disk?.http ?? http,\n \t\tdir,\n \t\turl: access.url,\n \t\tonAuth: access.onAuth,\n@@ -94,7 +135,8 @@\n \t\tdepth: DEPTH,\n \t\tnoCheckout: true,\n \t});\n-\tconst log = await git.log({ fs, dir, ref: \"main\", depth: DEPTH });\n+\tintake?.assertReady();\n+\tconst log = await git.log({ fs, dir, cache, ref: \"main\", depth: DEPTH });\n \tif (log.length === 0) return null;\n \tconst head = log[0]!.oid;\n \tconst fresh: typeof log = [];\n@@ -113,9 +155,27 @@\n \t\tcommits.push({\n \t\t\tsha: c.oid,\n \t\t\tmessage: c.commit.message.trim(),\n-\t\t\t...(await treeText(fs, dir, c.oid, storeBlob, tooBig, oldBlob)),\n+\t\t\t...(await treeText(\n+\t\t\t\tfs,\n+\t\t\t\tdir,\n+\t\t\t\tc.oid,\n+\t\t\t\tstoreBlob,\n+\t\t\t\ttooBig,\n+\t\t\t\toldBlob,\n+\t\t\t\tretainFile,\n+\t\t\t\tcache,\n+\t\t\t\tintake,\n+\t\t\t\tdisk?.retainFileChunks,\n+\t\t\t\tdisk?.maxBinaryBytes,\n+\t\t\t)),\n \t\t});\n-\treturn { head, author: fresh.at(-1)?.commit.author.name ?? \"git\", base, commits, tooBig: [...tooBig] };\n+\treturn {\n+\t\thead,\n+\t\tauthor: fresh.at(-1)?.commit.author.name ?? \"git\",\n+\t\tbase,\n+\t\tcommits,\n+\t\ttooBig: [...tooBig],\n+\t};\n }\n \n /**\n@@ -123,24 +183,66 @@\n  * a link (120000) is \"\" with its target in modes. Files that aren't UTF-8\n  * are left out.\n  */\n-async function treeText(\n+export async function treeText(\n \tfs: MemoryFS,\n \tdir: string,\n \toid: string,\n \tstoreBlob: StoreBlob | undefined,\n \ttooBig: Set<string>,\n \toldBlob: (path: string) => string | undefined,\n-): Promise<{ files: Record<string, string>; modes: FileModes; blobs: Record<string, string> }> {\n+\tretainFile: (files: Record<string, string>, path: string, body: string, oid?: string) => void,\n+\tcache: object = {},\n+\tintake?: GitDiskIntake,\n+\tretainFileChunks?: RetainFileChunks,\n+\tmaxBinaryBytes?: number,\n+): Promise<{\n+\tfiles: Record<string, string>;\n+\tmodes: FileModes;\n+\tblobs: Record<string, string>;\n+}> {\n \tconst files: Record<string, string> = {};\n \tconst modes: FileModes = {};\n \tconst blobs: Record<string, string> = {};\n \tawait git.walk({\n \t\tfs,\n+\t\tcache,\n \t\tdir,\n \t\ttrees: [git.TREE({ ref: oid })],\n+\t\titerate: async (walk, children) => {\n+\t\t\tfor (const child of children) await walk(child);\n+\t\t\treturn [];\n+\t\t},\n+\t\treduce: async () => undefined,\n \t\tmap: async (path, [entry]) => {\n \t\t\tif (!entry || path === \".\") return true;\n \t\t\tif ((await entry.type()) !== \"blob\") return true;\n+\t\t\tconst mode = await entry.mode();\n+\t\t\tconst blobOid = await entry.oid();\n+\t\t\tif (intake && retainFileChunks && mode !== LINK) {\n+\t\t\t\tconst stored = intake.bytes(blobOid);\n+\t\t\t\tif (stored?.type !== \"blob\") throw Error(\"Git intake blob is missing\");\n+\t\t\t\tlet valid = true;\n+\t\t\t\ttry {\n+\t\t\t\t\t// Validate before retaining: a late invalid byte must take the binary path.\n+\t\t\t\t\tfor (const _ of gitBlobText(stored)) {\n+\t\t\t\t\t\t/* Retain no decoded text. */\n+\t\t\t\t\t}\n+\t\t\t\t} catch (error) {\n+\t\t\t\t\tif (!(error instanceof InvalidGitUtf8)) throw error;\n+\t\t\t\t\tvalid = false;\n+\t\t\t\t}\n+\t\t\t\tif (valid) {\n+\t\t\t\t\tretainFileChunks(files, path, gitBlobText(stored), blobOid);\n+\t\t\t\t\tmodes[path] = { exec: mode === EXEC, link: null };\n+\t\t\t\t\treturn true;\n+\t\t\t\t}\n+\t\t\t\tif (storeBlob && maxBinaryBytes !== undefined && stored.byteLength > maxBinaryBytes) {\n+\t\t\t\t\ttooBig.add(path);\n+\t\t\t\t\tconst kept = oldBlob(path);\n+\t\t\t\t\tif (kept) blobs[path] = kept;\n+\t\t\t\t\treturn true;\n+\t\t\t\t}\n+\t\t\t}\n \t\t\tconst bytes = await entry.content();\n \t\t\tif (!bytes) return true;\n \t\t\tlet body: string;\n@@ -151,7 +253,7 @@\n \t\t\t\tconst sha = storeBlob ? await storeBlob(bytes) : null;\n \t\t\t\tif (sha) {\n \t\t\t\t\tblobs[path] = sha;\n-\t\t\t\t\tmodes[path] = { exec: (await entry.mode()) === EXEC, link: null };\n+\t\t\t\t\tmodes[path] = { exec: mode === EXEC, link: null };\n \t\t\t\t} else if (storeBlob) {\n \t\t\t\t\ttooBig.add(path);\n \t\t\t\t\tconst kept = oldBlob(path);\n@@ -159,12 +261,11 @@\n \t\t\t\t}\n \t\t\t\treturn true;\n \t\t\t}\n-\t\t\tconst mode = await entry.mode();\n \t\t\tif (mode === LINK) {\n-\t\t\t\tfiles[path] = \"\";\n+\t\t\t\tretainFile(files, path, \"\");\n \t\t\t\tmodes[path] = { exec: false, link: body };\n \t\t\t} else {\n-\t\t\t\tfiles[path] = body;\n+\t\t\t\tretainFile(files, path, body, blobOid);\n \t\t\t\tmodes[path] = { exec: mode === EXEC, link: null };\n \t\t\t}\n \t\t\treturn true;"}],"unmatched":[]}