repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

pi/agent/extensions/diff-review/git.ts

Raw
File outline
import { execFile, spawn } from "node:child_process";
import { join, relative } from "node:path";
import { promisify } from "node:util";
import { MAX_DIFF_BYTES, MAX_FILE_DIFF_BYTES } from "./constants.js";
import { sourceFromArgs, type DiffReviewArgs } from "./args.js";
import type { CollectedDiff, DiffFileSummary, DiffFileStatus, 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\/(.*)$/;

export async function collectDiff(cwd: string, args: DiffReviewArgs): Promise<CollectedDiff> {
	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];
	if (!pieces.diffText.trim() && 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 files = await changedFiles(cwd, args);
	const chunks: string[] = [];
	const skippedFiles: string[] = [];
	let totalBytes = 0;

	for (const file of files) {
		try {
			const chunk = await gitDiff(cwd, diffArgs(args, [file]), MAX_FILE_DIFF_BYTES);
			const bytes = Buffer.byteLength(chunk, "utf8");
			if (totalBytes + bytes > MAX_DIFF_BYTES) {
				skippedFiles.push(`${file} (review diff limit)`);
				continue;
			}
			totalBytes += bytes;
			chunks.push(chunk);
		} catch (err) {
			if (err instanceof DiffTooLargeError) skippedFiles.push(`${file} (${Math.round(MAX_FILE_DIFF_BYTES / 1024 / 1024)} MiB per-file limit)`);
			else throw err;
		}
	}

	return { diffText: chunks.join(""), skippedFiles };
}

async function changedFiles(cwd: string, args: DiffReviewArgs) {
	const output = await git(cwd, nameOnlyArgs(args), { maxBuffer: DEFAULT_GIT_BUFFER_BYTES });
	return output.split("\0").filter(Boolean);
}

function diffArgs(args: DiffReviewArgs, files = args.files): string[] {
	const out = baseDiffArgs(args);
	out.push("--");
	if (files) out.push(...files);
	return out;
}

function nameOnlyArgs(args: DiffReviewArgs): string[] {
	const out = [...baseDiffArgs(args), "--name-only", "-z", "--"];
	if (args.files) out.push(...args.files);
	return out;
}

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) out.push(`${args.base}...HEAD`);
	return out;
}

async function git(cwd: string, args: string[], options: { maxBuffer?: number } = {}): Promise<string> {
	try {
		const { stdout } = await execFileAsync("git", args, { cwd, 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<string> {
	return new Promise((resolve, reject) => {
		const child = spawn("git", args, { cwd, 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 gitError(err: unknown) {
	const e = err as { stderr?: string | Buffer; message?: string };
	return String(e.stderr ?? "").trim() || e.message || "git failed";
}

function parseUnifiedDiff(diffText: string): Omit<CollectedDiff, "repoRoot" | "source" | "diffText"> {
	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.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`);
	}
}