{"draft":null,"from":175,"to":318,"files":[{"path":"server/src/git-intake-trees.ts","added":77,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/git-intake-trees.ts\n@@ -0,0 +1,77 @@\n+import { StoredText } from \"../../src/engine/stored-text.ts\";\n+import type { SqliteStorage } from \"./engine-project.ts\";\n+import type { PushedCommits } from \"./ingest.ts\";\n+import { SqliteWriterState } from \"./writer-host.ts\";\n+\n+type Tree = Pick<PushedCommits[\"commits\"][number], \"files\" | \"modes\" | \"blobs\">;\n+\n+/** Keeps earlier Git trees on disk while later commits are decoded. */\n+export class GitIntakeTrees {\n+\t/** Only at a cold Durable Object start, before any intake can be active. */\n+\tstatic recover(storage: SqliteStorage): void {\n+\t\tSqliteWriterState.initialize(storage);\n+\t\tstorage.sql.exec(\"DELETE FROM writer_state WHERE namespace GLOB 'git-intake:*'\");\n+\t}\n+\treadonly namespace = `git-intake:${crypto.randomUUID()}`;\n+\treadonly storage: SqliteStorage;\n+\treadonly text: StoredText;\n+\tprivate next = 0;\n+\tprivate active = true;\n+\tconstructor(storage: SqliteStorage) {\n+\t\tthis.storage = storage;\n+\t\tSqliteWriterState.initialize(storage);\n+\t\tthis.text = new StoredText(new SqliteWriterState(storage, this.namespace), \"files\");\n+\t}\n+\t/** Created outside any scope containing decoded file text or a whole tree. */\n+\tprivate getter(key: string): () => string {\n+\t\treturn () => {\n+\t\t\tif (!this.active) throw Error(\"Git intake trees are closed\");\n+\t\t\treturn this.text.read(key);\n+\t\t};\n+\t}\n+\tretainFile(files: Record<string, string>, path: string, body: string, oid?: string): void {\n+\t\tthis.retainChunks(\n+\t\t\tfiles,\n+\t\t\tpath,\n+\t\t\t(function* () {\n+\t\t\t\tfor (let at = 0; at < body.length; at += StoredText.chunkCharacters)\n+\t\t\t\t\tyield body.slice(at, at + StoredText.chunkCharacters);\n+\t\t\t})(),\n+\t\t\toid,\n+\t\t);\n+\t}\n+\tretainChunks(files: Record<string, string>, path: string, chunks: Iterable<string>, oid?: string): void {\n+\t\tif (!this.active) throw Error(\"Git intake trees are closed\");\n+\t\tif (oid !== undefined && !/^[0-9a-f]{40}$/.test(oid)) throw Error(\"Invalid Git blob id\");\n+\t\tconst key = oid === undefined ? String(this.next++) : `blob:${oid}`;\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tif (this.text.storage.read(this.text.collection, key) === undefined) this.text.writeChunks(key, chunks);\n+\t\t});\n+\t\tObject.defineProperty(files, path, {\n+\t\t\tenumerable: true,\n+\t\t\tget: this.getter(key),\n+\t\t});\n+\t}\n+\tretain(tree: Tree): Tree {\n+\t\tif (!this.active) throw Error(\"Git intake trees are closed\");\n+\t\tconst files: Record<string, string> = Object.create(null);\n+\t\tthis.storage.transactionSync(() => {\n+\t\t\tfor (const path of Object.keys(tree.files)) {\n+\t\t\t\tconst key = String(this.next++);\n+\t\t\t\tthis.text.write(key, tree.files[path]!);\n+\t\t\t\tObject.defineProperty(files, path, {\n+\t\t\t\t\tenumerable: true,\n+\t\t\t\t\tget: this.getter(key),\n+\t\t\t\t});\n+\t\t\t}\n+\t\t});\n+\t\treturn { files, modes: tree.modes, blobs: tree.blobs };\n+\t}\n+\t[Symbol.dispose](): void {\n+\t\tthis.dispose();\n+\t}\n+\tdispose(): void {\n+\t\tthis.active = false;\n+\t\tthis.storage.sql.exec(\"DELETE FROM writer_state WHERE namespace = ?\", this.namespace);\n+\t}\n+}"}],"unmatched":[]}