import { execFile, spawn } from "node:child_process"; import { join, relative } from "node:path"; import { promisify } from "node:util"; import { type DiffReviewArgs, sourceFromArgs } from "./args.js"; import { MAX_DIFF_BYTES, MAX_FILE_DIFF_BYTES } from "./constants.js"; import type { CollectedDiff, DiffFileStatus, DiffFileSummary, DiffIndex, IndexedFileDiff, } from "./types.js"; const execFileAsync = promisify(execFile); const DEFAULT_GIT_BUFFER_BYTES = 10 * 1024 * 1024; const STDERR_BUFFER_BYTES = 64 * 1024; const HUNK_HEADER = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/; const DIFF_GIT_HEADER = /^diff --git a\/(.*) b\/(.*)$/; const GIT_LOCAL_ENV = [ "GIT_ALTERNATE_OBJECT_DIRECTORIES", "GIT_CONFIG", "GIT_CONFIG_PARAMETERS", "GIT_CONFIG_COUNT", "GIT_OBJECT_DIRECTORY", "GIT_DIR", "GIT_WORK_TREE", "GIT_IMPLICIT_WORK_TREE", "GIT_GRAFT_FILE", "GIT_INDEX_FILE", "GIT_NO_REPLACE_OBJECTS", "GIT_REPLACE_REF_BASE", "GIT_PREFIX", "GIT_SHALLOW_FILE", "GIT_COMMON_DIR", ] as const; type ChangedFileSpec = { paths: string[]; displayPath: string }; export async function collectDiff( cwd: string, args: DiffReviewArgs, ): Promise { const repoRoot = (await git(cwd, ["rev-parse", "--show-toplevel"])).trim(); const repoArgs = rebasePathArgs(args, repoRoot, cwd); const source = sourceFromArgs(repoArgs); const pieces = await collectRenderableDiff(repoRoot, repoArgs); const parsed = parseUnifiedDiff(pieces.diffText); const skippedFiles = [...parsed.skippedFiles, ...pieces.skippedFiles]; const hasRenderableFile = parsed.files.some( (file) => file.status !== "binary", ); if (!hasRenderableFile && skippedFiles.length > 0) throw new Error( `No renderable diff; skipped oversized/binary files: ${skippedFiles.join(", ")}`, ); return { repoRoot, source, diffText: pieces.diffText, ...parsed, skippedFiles, }; } function rebasePathArgs( args: DiffReviewArgs, repoRoot: string, cwd: string, ): DiffReviewArgs { if (!args.files) return args; const prefix = relative(repoRoot, cwd).replace(/\\/g, "/"); return { ...args, files: args.files.map((file) => prefix ? join(prefix, file).replace(/\\/g, "/") : file, ), }; } async function collectRenderableDiff(cwd: string, args: DiffReviewArgs) { const specs = await changedFileSpecs(cwd, args); const chunks: string[] = []; const skippedFiles: string[] = []; let totalBytes = 0; for (const spec of specs) { try { const chunk = normalizeDiffHeaders( await gitDiff(cwd, diffArgs(args, spec.paths), MAX_FILE_DIFF_BYTES), ); const bytes = Buffer.byteLength(chunk, "utf8"); if (totalBytes + bytes > MAX_DIFF_BYTES) { skippedFiles.push(`${spec.displayPath} (review diff limit)`); continue; } totalBytes += bytes; chunks.push(chunk); } catch (err) { if (err instanceof DiffTooLargeError) skippedFiles.push( `${spec.displayPath} (${Math.round(MAX_FILE_DIFF_BYTES / 1024 / 1024)} MiB per-file limit)`, ); else throw err; } } return { diffText: chunks.join(""), skippedFiles }; } async function changedFileSpecs( cwd: string, args: DiffReviewArgs, ): Promise { const output = await git(cwd, nameStatusArgs(args), { maxBuffer: DEFAULT_GIT_BUFFER_BYTES, }); return parseNameStatus(output); } function diffArgs(args: DiffReviewArgs, files = args.files): string[] { const out = baseDiffArgs(args); out.push("--"); if (files) out.push(...files); return out; } function nameStatusArgs(args: DiffReviewArgs): string[] { const out = [...baseDiffArgs(args), "--name-status", "-z", "--"]; if (args.files) out.push(...args.files); return out; } function parseNameStatus(output: string): ChangedFileSpec[] { const parts = output.split("\0"); const specs: ChangedFileSpec[] = []; for (let i = 0; i < parts.length; ) { const status = parts[i++]; if (!status) continue; if (status.startsWith("R") || status.startsWith("C")) { const oldPath = parts[i++]; const newPath = parts[i++]; if (oldPath && newPath) specs.push({ paths: [oldPath, newPath], displayPath: newPath }); continue; } const file = parts[i++]; if (file) specs.push({ paths: [file], displayPath: file }); } return specs; } function baseDiffArgs(args: DiffReviewArgs): string[] { const out = [ "diff", "--no-ext-diff", "--find-renames", "--src-prefix=a/", "--dst-prefix=b/", "--unified=3", ]; if (args.staged) out.push("--cached"); if (args.base) { if (args.base.startsWith("-")) throw new Error("--base must be a git revision, not an option"); out.push(`${args.base}...HEAD`); } return out; } async function git( cwd: string, args: string[], options: { maxBuffer?: number } = {}, ): Promise { try { const { stdout } = await execFileAsync("git", args, { cwd, env: cleanGitEnv(), maxBuffer: options.maxBuffer ?? DEFAULT_GIT_BUFFER_BYTES, }); return String(stdout); } catch (err) { throw new Error(gitError(err)); } } function gitDiff( cwd: string, args: string[], maxBytes: number, ): Promise { return new Promise((resolve, reject) => { const child = spawn("git", args, { cwd, env: cleanGitEnv(), stdio: ["ignore", "pipe", "pipe"], }); const stdout: Buffer[] = []; const stderr: Buffer[] = []; let stdoutBytes = 0; let stderrBytes = 0; let settled = false; const resolveOnce = (text: string) => { if (settled) return; settled = true; resolve(text); }; const rejectOnce = (error: Error) => { if (settled) return; settled = true; reject(error); }; child.stdout.on("data", (chunk: Buffer) => { stdoutBytes += chunk.length; if (stdoutBytes > maxBytes) { child.kill("SIGTERM"); rejectOnce(new DiffTooLargeError(maxBytes)); return; } stdout.push(chunk); }); child.stderr.on("data", (chunk: Buffer) => { if (stderrBytes >= STDERR_BUFFER_BYTES) return; const remaining = STDERR_BUFFER_BYTES - stderrBytes; stderr.push(chunk.subarray(0, remaining)); stderrBytes += Math.min(chunk.length, remaining); }); child.once("error", rejectOnce); child.once("close", (code) => { if (settled) return; if (code === 0) resolveOnce(Buffer.concat(stdout).toString("utf8")); else rejectOnce( new Error( Buffer.concat(stderr).toString("utf8").trim() || `git diff exited with code ${code ?? "unknown"}`, ), ); }); }); } function cleanGitEnv() { const env = { ...process.env }; for (const key of GIT_LOCAL_ENV) delete env[key]; return env; } function gitError(err: unknown) { const e = err as { stderr?: string | Buffer; message?: string }; return String(e.stderr ?? "").trim() || e.message || "git failed"; } export function normalizeDiffHeaders(diffText: string): string { return diffText .split(/\r?\n/) .map((line) => { const quotedDiff = /^diff --git ("(?:[^"\\]|\\.)+") ("(?:[^"\\]|\\.)+")$/.exec(line); const [, oldPath, newPath] = quotedDiff ?? []; if (oldPath && newPath) return `diff --git ${gitPathToken(oldPath)} ${gitPathToken(newPath)}`; const quotedHeader = /^(---|\+\+\+) ("(?:[^"\\]|\\.)+")$/.exec(line); const [, marker, path] = quotedHeader ?? []; if (marker && path) return `${marker} ${gitPathToken(path)}`; return line; }) .join("\n"); } function gitPathToken(token: string): string { return token.startsWith('"') && token.endsWith('"') ? token.slice(1, -1) : token; } function parseUnifiedDiff( diffText: string, ): Omit { const state: DiffParserState = { diffIndex: new Map(), files: [], skippedFiles: [], oldLine: 0, newLine: 0, inHunk: false, }; for (const line of diffText.split(/\r?\n/)) parseDiffLine(state, line); finishFile(state); return { files: state.files, diffIndex: state.diffIndex, skippedFiles: state.skippedFiles, additions: sumChanges(state.files, "additions"), deletions: sumChanges(state.files, "deletions"), }; } type DiffParserState = { current?: IndexedFileDiff; diffIndex: DiffIndex; files: DiffFileSummary[]; skippedFiles: string[]; oldLine: number; newLine: number; inHunk: boolean; }; function parseDiffLine(state: DiffParserState, line: string) { if (line.startsWith("diff --git ")) return startFile(state, line); if (!state.current) return; if (updateFileMetadata(state.current, line)) return; if (updateHeaderPath(state.current, line)) return; if (startHunk(state, line)) return; if (!state.inHunk) return; applyHunkLine(state, line); } function startFile(state: DiffParserState, line: string) { finishFile(state); const { oldPath, newPath } = parseDiffGitLine(line); state.current = { file: newPath, prevFile: oldPath, status: "modified", additions: new Map(), deletions: new Map(), oldLines: new Map(), newLines: new Map(), }; state.oldLine = 0; state.newLine = 0; state.inHunk = false; } function finishFile(state: DiffParserState) { const file = state.current; if (!file) return; if (file.status === "binary") { state.skippedFiles.push(file.file); state.current = undefined; return; } state.diffIndex.set(file.file, file); state.files.push(summary(file)); state.current = undefined; } function summary(file: IndexedFileDiff): DiffFileSummary { return { path: file.file, ...(file.prevFile && file.prevFile !== file.file && { prevPath: file.prevFile }), status: file.status, additions: file.additions.size, deletions: file.deletions.size, }; } function updateFileMetadata(file: IndexedFileDiff, line: string) { const status = statusFromMetadata(line, file.status); if (!status) return false; file.status = status; return true; } function statusFromMetadata( line: string, current: DiffFileStatus, ): DiffFileStatus | undefined { if (line.startsWith("new file mode ")) return "added"; if (line.startsWith("deleted file mode ")) return "deleted"; if (line.startsWith("Binary files ")) return "binary"; if ( line.startsWith("similarity index ") || line.startsWith("rename from ") || line.startsWith("rename to ") ) return current === "added" || current === "deleted" ? current : "renamed"; return undefined; } function updateHeaderPath(file: IndexedFileDiff, line: string) { if (line.startsWith("--- ")) { const path = parseHeaderPath(line.slice(4)); if (path && path !== "/dev/null") file.prevFile = path; return true; } if (line.startsWith("+++ ")) { const path = parseHeaderPath(line.slice(4)); if (path && path !== "/dev/null") file.file = path; return true; } return false; } function startHunk(state: DiffParserState, line: string) { const match = HUNK_HEADER.exec(line); if (!match) return false; state.oldLine = Number(match[1]); state.newLine = Number(match[2]); state.inHunk = true; return true; } function applyHunkLine(state: DiffParserState, line: string) { const file = state.current; if (!file || line.startsWith("\\")) return; if (line.startsWith("+")) return addLine(file, state, line); if (line.startsWith("-")) return deleteLine(file, state, line); if (line.startsWith(" ")) return contextLine(state, line); } function addLine(file: IndexedFileDiff, state: DiffParserState, line: string) { const text = line.slice(1); file.additions.set(state.newLine, text); file.newLines.set(state.newLine, text); state.newLine++; } function deleteLine( file: IndexedFileDiff, state: DiffParserState, line: string, ) { const text = line.slice(1); file.deletions.set(state.oldLine, text); file.oldLines.set(state.oldLine, text); state.oldLine++; } function contextLine(state: DiffParserState, line: string) { if (state.current) { const text = line.slice(1); state.current.oldLines.set(state.oldLine, text); state.current.newLines.set(state.newLine, text); } state.oldLine++; state.newLine++; } function parseDiffGitLine(line: string) { const match = DIFF_GIT_HEADER.exec(line); return { oldPath: match?.[1] ?? "", newPath: match?.[2] ?? match?.[1] ?? "" }; } function parseHeaderPath(header: string): string | undefined { const trimmed = header.trim(); if (trimmed === "/dev/null") return trimmed; if (trimmed.startsWith("a/") || trimmed.startsWith("b/")) return trimmed.slice(2); return trimmed.split(/\s+/)[0]; } function sumChanges(files: DiffFileSummary[], key: "additions" | "deletions") { return files.reduce((sum, file) => sum + file[key], 0); } class DiffTooLargeError extends Error { constructor(readonly maxBytes: number) { super(`diff exceeds ${Math.round(maxBytes / 1024 / 1024)} MiB limit`); } }