import { execFile } from "node:child_process"; import { promisify } from "node:util"; const execFileAsync = promisify(execFile); export async function worktreeDiff(worktree: string): Promise { await execFileAsync("git", ["add", "-A"], { cwd: worktree }); const { stdout } = await execFileAsync("git", ["diff", "--cached", "--binary"], { cwd: worktree, encoding: "utf8", maxBuffer: 4 * 1024 * 1024 }); return stdout; } export async function worktreeChangedPaths(worktree: string): Promise { await execFileAsync("git", ["add", "-A"], { cwd: worktree }); const { stdout } = await execFileAsync("git", ["diff", "--cached", "--name-only", "-z"], { cwd: worktree, encoding: "utf8", maxBuffer: 1024 * 1024 }); return stdout.split("\0").filter(Boolean); } export async function integrateReviewedDiff(repo: string, patch: string, approved: boolean): Promise { if (!approved) throw new Error("Integration requires explicit review approval"); if (!patch.trim()) return; await new Promise((resolvePromise, reject) => { const child = execFile("git", ["apply", "--index", "-"], { cwd: repo }, (error) => error ? reject(error) : resolvePromise()); child.stdin?.end(patch); }); }