type DiffOp = | { kind: 'add'; line: string } | { kind: 'del'; line: string } | { kind: 'eq'; line: string }; function splitLines(text: string): string[] { if (!text) return []; const lines = text.split('\n'); if (lines.at(-1) === '') lines.pop(); return lines; } function diffLines( before: readonly string[], after: readonly string[] ): DiffOp[] { let prefix = 0; while ( prefix < before.length && prefix < after.length && before[prefix] === after[prefix] ) { prefix += 1; } let suffix = 0; while ( suffix < before.length - prefix && suffix < after.length - prefix && before[before.length - 1 - suffix] === after[after.length - 1 - suffix] ) { suffix += 1; } const aMid = before.slice(prefix, before.length - suffix); const bMid = after.slice(prefix, after.length - suffix); const ops: DiffOp[] = []; for (let i = 0; i < prefix; i += 1) { ops.push({ kind: 'eq', line: before[i] }); } const n = aMid.length; const m = bMid.length; // Bound DP table size so multi-thousand-line files fall back cleanly. if (n * m <= 2_000_000) { const dp = new Int32Array((n + 1) * (m + 1)); const stride = m + 1; for (let i = n - 1; i >= 0; i -= 1) { for (let j = m - 1; j >= 0; j -= 1) { dp[i * stride + j] = aMid[i] === bMid[j] ? dp[(i + 1) * stride + (j + 1)] + 1 : Math.max(dp[(i + 1) * stride + j], dp[i * stride + (j + 1)]); } } let i = 0; let j = 0; while (i < n && j < m) { if (aMid[i] === bMid[j]) { ops.push({ kind: 'eq', line: aMid[i] }); i += 1; j += 1; } else if (dp[(i + 1) * stride + j] >= dp[i * stride + (j + 1)]) { ops.push({ kind: 'del', line: aMid[i] }); i += 1; } else { ops.push({ kind: 'add', line: bMid[j] }); j += 1; } } while (i < n) { ops.push({ kind: 'del', line: aMid[i] }); i += 1; } while (j < m) { ops.push({ kind: 'add', line: bMid[j] }); j += 1; } } else { for (const line of aMid) ops.push({ kind: 'del', line }); for (const line of bMid) ops.push({ kind: 'add', line }); } for (let i = before.length - suffix; i < before.length; i += 1) { ops.push({ kind: 'eq', line: before[i] }); } return ops; } export function createUnifiedDiff( relPath: string, beforeText: string, afterText: string, contextLines = 3 ): string { if (beforeText === afterText) return ''; const before = splitLines(beforeText); const after = splitLines(afterText); const ops = diffLines(before, after); const changedIndices: number[] = []; for (let i = 0; i < ops.length; i += 1) { if (ops[i].kind !== 'eq') changedIndices.push(i); } if (changedIndices.length === 0) return ''; const hunks: Array<{ end: number; start: number }> = []; for (const idx of changedIndices) { const start = Math.max(0, idx - contextLines); const end = Math.min(ops.length - 1, idx + contextLines); const prev = hunks.at(-1); if (prev && start <= prev.end + 1) { prev.end = end; } else { hunks.push({ end, start }); } } const out: string[] = [`--- a/${relPath}`, `+++ b/${relPath}`]; for (const hunk of hunks) { let oldLine = 1; let newLine = 1; for (let i = 0; i < hunk.start; i += 1) { const { kind } = ops[i]; if (kind !== 'add') oldLine += 1; if (kind !== 'del') newLine += 1; } let oldCount = 0; let newCount = 0; const body: string[] = []; for (let i = hunk.start; i <= hunk.end; i += 1) { const op = ops[i]; if (op.kind === 'eq') { oldCount += 1; newCount += 1; body.push(` ${op.line}`); } else if (op.kind === 'del') { oldCount += 1; body.push(`-${op.line}`); } else { newCount += 1; body.push(`+${op.line}`); } } const oldStart = oldCount === 0 ? oldLine - 1 : oldLine; const newStart = newCount === 0 ? newLine - 1 : newLine; out.push(`@@ -${oldStart},${oldCount} +${newStart},${newCount} @@`); out.push(...body); } return `${out.join('\n')}\n`; }