Versions 33 to 318
new file, 275 lines
1+// In from plain git (#8, demo moment 5): commits a person pushed to the 2+// project's Artifacts repo, read back for project.ingest. Every commit the 3+// Worker pushes ends "abso-version: N" ("avcs-version: N" before the rename); the commits above the newest such 4+// commit that haven't been taken in yet are the person's, and N is their base. 5+import git, { type HttpClient } from "isomorphic-git"; 6+import type { FileModes } from "../../worker/project/project-api.ts"; 7+import type { SqliteStorage } from "./engine-project.ts"; 8+import { gitBlobText, InvalidGitUtf8 } from "./git-blob-text.ts"; 9+import { GitDiskIntake } from "./git-disk-intake.ts"; 10+import { gitHttp } from "./git-http.ts"; 11+import { MemoryFS } from "./memory-fs.ts"; 12+import { repoName } from "./push.ts"; 14+const http = gitHttp(); 16+const EXEC = 0o100755; 17+const LINK = 0o120000; 19+/** How far back to look for the Worker's last commit. */ 21+const text = new TextDecoder("utf-8", { fatal: true, ignoreBOM: false }); 23+export interface PushedCommits { 24+ /** main's head when read. */ 28+ /** Oldest first, each with its whole tree as text and every file's mode. Binary files are left out. */ 32+ files: Record<string, string>; 34+ blobs: Record<string, string>; 36+ /** Binary files over the blob limit, left out of the commits. */ 40+export function absoVersion(message: string): number | undefined { 41+ const m = /^(?:abso|avcs)-version: (\d+)\s*$/m.exec(message); 42+ return m ? Number(m[1]) : undefined; 45+async function readAccess( 46+ artifacts: Artifacts, 50+ onAuth: () => { username: string; password: string }; 52+ let repo: Awaited<ReturnType<Artifacts["get"]>>; 54+ repo = await artifacts.get(repoName(project)); 59+ const [info, token] = await Promise.all([r.info(), r.createToken("read", 600)]); 60+ const password = token.plaintext.split("?expires=")[0]!; 61+ return { url: info!.remote, onAuth: () => ({ username: "x", password }) }; 64+/** main's head on the project's repo, or null when there's no repo or no main yet. Cheap: one ref listing. */ 65+export async function remoteHead(artifacts: Artifacts, project: string): Promise<string | null> { 66+ const access = await readAccess(artifacts, project); 67+ if (!access) return null; 68+ const refs = await git.listServerRefs({ 71+ onAuth: access.onAuth, 72+ prefix: "refs/heads/main", 74+ return refs.find((r) => r.ref === "refs/heads/main")?.oid ?? null; 78+ * The commits pushed since `seen` (the last head this project handled), or 79+ * none when every commit above the Worker's last one has been seen. 81+/** Stores a binary file's bytes and answers their SHA-256, or null when they're over the limit. */ 82+export type StoreBlob = (bytes: Uint8Array) => Promise<string | null>; 83+export type RetainFileChunks = ( 84+ files: Record<string, string>, 86+ chunks: Iterable<string>, 91+ * `oldBlob` gives shared's blob for a path: a binary file over the limit 92+ * keeps it, since a path missing from a commit would mean the file was deleted. 94+export async function pushedSince( 95+ artifacts: Artifacts, 97+ seen: string | undefined, 98+ storeBlob?: StoreBlob, 99+ oldBlob: (path: string) => string | undefined = () => undefined, 100+ retainFile: (files: Record<string, string>, path: string, body: string, oid?: string) => void = ( 108+ storage: SqliteStorage; 109+ maxPackBytes: number; 110+ maxObjectBytes: number; 111+ maxDecodedBytes?: number; 112+ maxBinaryBytes?: number; 114+ retainFileChunks?: RetainFileChunks; 116+): Promise<PushedCommits | null> { 117+ const access = await readAccess(artifacts, project); 118+ if (!access) return null; 119+ const fs = new MemoryFS(); 120+ const dir = "/repo"; 122+ ? new GitDiskIntake(disk.storage, disk.maxPackBytes, disk.maxObjectBytes, disk.maxDecodedBytes) 124+ const cache = disk ? {} : undefined; 125+ if (intake && cache) intake.attach(cache); 129+ http: disk?.http ?? http, 132+ onAuth: access.onAuth, 138+ intake?.assertReady(); 139+ const log = await git.log({ fs, dir, cache, ref: "main", depth: DEPTH }); 140+ if (log.length === 0) return null; 141+ const head = log[0]!.oid; 142+ const fresh: typeof log = []; 143+ let base: number | undefined; 145+ for (const c of log) { 146+ if (c.oid === seen) past = true; 147+ base = absoVersion(c.commit.message); 148+ if (base !== undefined) break; 149+ if (!past) fresh.unshift(c); 151+ if (base === undefined) throw new Error(`no abso-version commit in main's last ${DEPTH} commits`); 153+ const tooBig = new Set<string>(); 154+ for (const c of fresh) 157+ message: c.commit.message.trim(), 168+ disk?.retainFileChunks, 169+ disk?.maxBinaryBytes, 174+ author: fresh.at(-1)?.commit.author.name ?? "git", 177+ tooBig: [...tooBig], 182+ * A commit's whole tree as text, and every file's mode: 100755 is exec, and 183+ * a link (120000) is "" with its target in modes. Files that aren't UTF-8 186+export async function treeText( 190+ storeBlob: StoreBlob | undefined, 191+ tooBig: Set<string>, 192+ oldBlob: (path: string) => string | undefined, 193+ retainFile: (files: Record<string, string>, path: string, body: string, oid?: string) => void, 195+ intake?: GitDiskIntake, 196+ retainFileChunks?: RetainFileChunks, 197+ maxBinaryBytes?: number, 199+ files: Record<string, string>; 201+ blobs: Record<string, string>; 203+ const files: Record<string, string> = {}; 204+ const modes: FileModes = {}; 205+ const blobs: Record<string, string> = {}; 210+ trees: [git.TREE({ ref: oid })], 211+ iterate: async (walk, children) => { 212+ for (const child of children) await walk(child); 215+ reduce: async () => undefined, 216+ map: async (path, [entry]) => { 217+ if (!entry || path === ".") return true; 218+ if ((await entry.type()) !== "blob") return true; 219+ const mode = await entry.mode(); 220+ const blobOid = await entry.oid(); 221+ if (intake && retainFileChunks && mode !== LINK) { 222+ const stored = intake.bytes(blobOid); 223+ if (stored?.type !== "blob") throw Error("Git intake blob is missing"); 226+ // Validate before retaining: a late invalid byte must take the binary path. 227+ for (const _ of gitBlobText(stored)) { 228+ /* Retain no decoded text. */ 231+ if (!(error instanceof InvalidGitUtf8)) throw error; 235+ retainFileChunks(files, path, gitBlobText(stored), blobOid); 236+ modes[path] = { exec: mode === EXEC, link: null }; 239+ if (storeBlob && maxBinaryBytes !== undefined && stored.byteLength > maxBinaryBytes) { 241+ const kept = oldBlob(path); 242+ if (kept) blobs[path] = kept; 246+ const bytes = await entry.content(); 247+ if (!bytes) return true; 250+ body = text.decode(bytes); 252+ // Binary (#27): the bytes go to the blob store, and the file is a pointer to them. 253+ const sha = storeBlob ? await storeBlob(bytes) : null; 256+ modes[path] = { exec: mode === EXEC, link: null }; 257+ } else if (storeBlob) { 259+ const kept = oldBlob(path); 260+ if (kept) blobs[path] = kept; 264+ if (mode === LINK) { 265+ retainFile(files, path, ""); 266+ modes[path] = { exec: false, link: body }; 268+ retainFile(files, path, body, blobOid); 269+ modes[path] = { exec: mode === EXEC, link: null }; 274+ return { files, modes, blobs };