import { ObjectId, RepoHost } from "@repo/artifacts/repo-host"; import type { RepoNotFound, TreeEntry } from "@repo/artifacts/repo-host"; import { BlobSha, RepoPath } from "@repo/protocol/ids"; import type { RepoName, TreeSha } from "@repo/protocol/ids"; import { Array, Effect, Option, Order, Semaphore } from "effect"; import { Edit, sharedEnds, shortestDiff } from "./line-diff.ts"; /** * One file an answer changed, with its blob ids before and after. The same * shape as the gate's `Change` (`@repo/gate/facts`), which git cannot import. */ export interface Change { readonly path: RepoPath; readonly status: "added" | "modified" | "deleted"; readonly before?: BlobSha; readonly after?: BlobSha; } /** A line a change added, numbered from 1 in the new file. */ export interface AddedLine { readonly line: number; readonly text: string; } // Store calls under way at once, however deep the walk or long the change set. const STORE_CALLS = 8; // The walk forks freely; the semaphore alone holds the store to its calls. const ALL = { concurrency: "unbounded" } as const; const entriesOf = ( calls: Semaphore.Semaphore, repo: RepoName, tree: Option.Option ) => Option.match(tree, { onNone: () => Effect.succeed([]), onSome: (id) => RepoHost.use((host) => host.readTree(repo, ObjectId.make(id))).pipe( Semaphore.withPermit(calls), Effect.flatMap( Option.match({ onNone: () => Effect.die(`${repo} has no tree ${id}`), onSome: Effect.succeed, }) ) ), }); const treeIdOf = (entry: TreeEntry | undefined) => entry?.type === "tree" ? Option.some(entry.sha) : Option.none(); // A submodule's pointer counts as a file at its path, so a change to it is seen. const fileIdOf = (entry: TreeEntry | undefined) => entry === undefined || entry.type === "tree" ? undefined : BlobSha.make(entry.sha); const fileChange = ( path: string, before: BlobSha | undefined, after: BlobSha | undefined ): readonly Change[] => { const at = RepoPath.make(path); if (before !== undefined && after !== undefined) { return [{ after, before, path: at, status: "modified" }]; } if (before !== undefined) { return [{ before, path: at, status: "deleted" }]; } return after === undefined ? [] : [{ after, path: at, status: "added" }]; }; const walk = ( calls: Semaphore.Semaphore, repo: RepoName, prefix: string, base: Option.Option, head: Option.Option ): Effect.Effect => Effect.gen(function* () { const [before, after] = yield* Effect.all( [entriesOf(calls, repo, base), entriesOf(calls, repo, head)], ALL ); const beforeByName = new Map(before.map((entry) => [entry.name, entry])); const afterByName = new Map(after.map((entry) => [entry.name, entry])); // The same id and mode: nothing under it changed, so it is never read. const names = Array.sort( new Set([...beforeByName.keys(), ...afterByName.keys()]), Order.String ).filter((name) => { const old = beforeByName.get(name); const next = afterByName.get(name); return old?.sha !== next?.sha || old?.mode !== next?.mode; }); const changed = yield* Effect.forEach( names, (name) => { const old = beforeByName.get(name); const next = afterByName.get(name); const path = `${prefix}${name}`; const files = fileChange(path, fileIdOf(old), fileIdOf(next)); const inside = [treeIdOf(old), treeIdOf(next)] as const; return Option.isNone(inside[0]) && Option.isNone(inside[1]) ? Effect.succeed(files) : walk(calls, repo, `${path}/`, inside[0], inside[1]).pipe( Effect.map((nested) => [...files, ...nested]) ); }, ALL ); return changed.flat(); }); /** * The files that differ between two trees of `repo`, read through the * `RepoHost` port alone: it walks both roots and descends only into subtrees * whose ids differ, since the store has no diff of its own. At most eight * trees are read at once. */ export const treeDiff = ( repo: RepoName, base: TreeSha, head: TreeSha ): Effect.Effect => Effect.flatMap(Semaphore.make(STORE_CALLS), (calls) => walk(calls, repo, "", Option.some(base), Option.some(head)) ); // git's own test: a NUL among the first bytes means the blob is not text. const SNIFF = 8192; const isBinary = (bytes: Uint8Array) => bytes.subarray(0, SNIFF).includes(0); const linesOf = (bytes: Uint8Array) => { const text = new TextDecoder().decode(bytes); const lines = text.split("\n"); if (text.endsWith("\n")) { lines.pop(); } return lines; }; /** * Lines of `next` with no partner in `previous`, matched by count: the * fallback for a rewrite too large for the shortest script. */ const unmatched = (previous: readonly string[], next: readonly string[]) => { const left = new Map(); for (const line of previous) { left.set(line, (left.get(line) ?? 0) + 1); } return next.flatMap((line, index) => { const count = left.get(line) ?? 0; left.set(line, count - 1); return count > 0 ? [] : [index]; }); }; /** * The lines of `next` that are not in `previous`, numbered from 1: the * insertions of the shortest edit script (`line-diff.ts`), or, for a * rewrite too large for it, the lines with no partner by count. */ const added = ( previous: readonly string[], next: readonly string[] ): readonly AddedLine[] => Option.match(shortestDiff(previous, next), { onNone: () => { const { head, tail } = sharedEnds(previous, next); return unmatched( previous.slice(head, previous.length - tail), next.slice(head, next.length - tail) ).map((index) => head + index); }, onSome: (edits) => edits.flatMap((edit) => Edit.$is("Insert")(edit) ? Array.makeBy(edit.count, (at) => edit.newStart + at) : [] ), }).map((index) => ({ line: index + 1, text: next[index] ?? "" })); /** * A file's lines; none for a binary blob, or for a submodule's pointer, whose * id names a commit in another repo, so the store has no blob by it. */ const textOf = (repo: RepoName, blob: BlobSha | undefined) => blob === undefined ? Effect.succeedSome([]) : RepoHost.use((host) => host.readBlob(repo, ObjectId.make(blob))).pipe( Effect.map( Option.flatMap((bytes) => isBinary(bytes) ? Option.none() : Option.some(linesOf(bytes)) ) ) ); // Changes diffed at once, each reading its two blobs together: eight store // calls, and no more than four files' text held at a time. const CHANGES = { concurrency: STORE_CALLS / 2 } as const; /** * The lines each added or modified file gained, by a line diff of its blobs. * A binary blob (a NUL in its first 8 KiB) and a submodule's pointer report * no lines. At most eight blobs are read at once. */ export const addedLines = ( repo: RepoName, changes: readonly Change[] ): Effect.Effect< ReadonlyMap, RepoNotFound, RepoHost > => Effect.forEach( changes.filter((change) => change.after !== undefined), (change) => Effect.gen(function* () { const [previous, next] = yield* Effect.all( [textOf(repo, change.before), textOf(repo, change.after)], ALL ); const lines = Option.match(next, { onNone: () => [], onSome: (text) => added( Option.getOrElse(previous, () => []), text ), }); return [change.path, lines] as const; }), CHANGES ).pipe(Effect.map((entries) => new Map(entries)));