{"draft":null,"from":116,"to":318,"files":[{"path":"server/src/ingest.ts","added":275,"removed":0,"diff":"--- /dev/null\n+++ b/server/src/ingest.ts\n@@ -0,0 +1,275 @@\n+// In from plain git (#8, demo moment 5): commits a person pushed to the\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, { 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+/** How far back to look for the Worker's last commit. */\n+const DEPTH = 50;\n+const text = new TextDecoder(\"utf-8\", { fatal: true, ignoreBOM: false });\n+\n+export interface PushedCommits {\n+\t/** main's head when read. */\n+\thead: string;\n+\tauthor: string;\n+\tbase: number;\n+\t/** Oldest first, each with its whole tree as text and every file's mode. Binary files are left out. */\n+\tcommits: {\n+\t\tsha: string;\n+\t\tmessage: string;\n+\t\tfiles: Record<string, string>;\n+\t\tmodes: FileModes;\n+\t\tblobs: Record<string, string>;\n+\t}[];\n+\t/** Binary files over the blob limit, left out of the commits. */\n+\ttooBig: string[];\n+}\n+\n+export function absoVersion(message: string): number | undefined {\n+\tconst m = /^(?:abso|avcs)-version: (\\d+)\\s*$/m.exec(message);\n+\treturn m ? Number(m[1]) : undefined;\n+}\n+\n+async function readAccess(\n+\tartifacts: Artifacts,\n+\tproject: string,\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+\t} catch {\n+\t\treturn null;\n+\t}\n+\tusing r = repo;\n+\tconst [info, token] = await Promise.all([r.info(), r.createToken(\"read\", 600)]);\n+\tconst password = token.plaintext.split(\"?expires=\")[0]!;\n+\treturn { url: info!.remote, onAuth: () => ({ username: \"x\", password }) };\n+}\n+\n+/** main's head on the project's repo, or null when there's no repo or no main yet. Cheap: one ref listing. */\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({\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+/**\n+ * The commits pushed since `seen` (the last head this project handled), or\n+ * none when every commit above the Worker's last one has been seen.\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+ * keeps it, since a path missing from a commit would mean the file was deleted.\n+ */\n+export async function pushedSince(\n+\tartifacts: Artifacts,\n+\tproject: string,\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\tcache,\n+\t\thttp: disk?.http ?? http,\n+\t\tdir,\n+\t\turl: access.url,\n+\t\tonAuth: access.onAuth,\n+\t\tref: \"main\",\n+\t\tsingleBranch: true,\n+\t\tdepth: DEPTH,\n+\t\tnoCheckout: true,\n+\t});\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+\tlet base: number | undefined;\n+\tlet past = false;\n+\tfor (const c of log) {\n+\t\tif (c.oid === seen) past = true;\n+\t\tbase = absoVersion(c.commit.message);\n+\t\tif (base !== undefined) break;\n+\t\tif (!past) fresh.unshift(c);\n+\t}\n+\tif (base === undefined) throw new Error(`no abso-version commit in main's last ${DEPTH} commits`);\n+\tconst commits = [];\n+\tconst tooBig = new Set<string>();\n+\tfor (const c of fresh)\n+\t\tcommits.push({\n+\t\t\tsha: c.oid,\n+\t\t\tmessage: c.commit.message.trim(),\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 {\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+ * A commit's whole tree as text, and every file's mode: 100755 is exec, and\n+ * a link (120000) is \"\" with its target in modes. Files that aren't UTF-8\n+ * are left out.\n+ */\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+\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+\t\t\ttry {\n+\t\t\t\tbody = text.decode(bytes);\n+\t\t\t} catch {\n+\t\t\t\t// Binary (#27): the bytes go to the blob store, and the file is a pointer to them.\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: 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+\t\t\t\t\tif (kept) blobs[path] = kept;\n+\t\t\t\t}\n+\t\t\t\treturn true;\n+\t\t\t}\n+\t\t\tif (mode === LINK) {\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\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;\n+\t\t},\n+\t});\n+\treturn { files, modes, blobs };\n+}"}],"unmatched":[]}