|
| 1 | +import type { DiffFile, DiffHunk } from "./types"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Parse a unified diff (with optional [L<n>] annotations) into structured DiffFile objects. |
| 5 | + * Returns a Map of file path -> DiffFile. |
| 6 | + */ |
| 7 | +export function parseDiff(diffContent: string): Map<string, DiffFile> { |
| 8 | + const files = new Map<string, DiffFile>(); |
| 9 | + |
| 10 | + // Split on "diff --git" boundaries |
| 11 | + const fileDiffs = diffContent.split(/^diff --git /m).filter(Boolean); |
| 12 | + |
| 13 | + for (const fileDiff of fileDiffs) { |
| 14 | + const lines = fileDiff.split("\n"); |
| 15 | + |
| 16 | + // Extract file path from "a/path b/path" |
| 17 | + const headerMatch = lines[0]?.match(/a\/(.+?)\s+b\/(.+)/); |
| 18 | + if (!headerMatch) continue; |
| 19 | + const filePath = headerMatch[2]; |
| 20 | + |
| 21 | + const addedLines = new Map<number, string>(); |
| 22 | + const removedLines = new Map<number, string>(); |
| 23 | + const hunks: DiffHunk[] = []; |
| 24 | + |
| 25 | + let currentHunk: DiffHunk | null = null; |
| 26 | + let newLineNum = 0; |
| 27 | + let oldLineNum = 0; |
| 28 | + |
| 29 | + for (const line of lines) { |
| 30 | + // Hunk header: @@ -oldStart,oldCount +newStart,newCount @@ |
| 31 | + const hunkMatch = line.match(/^@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@/); |
| 32 | + if (hunkMatch) { |
| 33 | + currentHunk = { |
| 34 | + oldStart: parseInt(hunkMatch[1]), |
| 35 | + oldCount: parseInt(hunkMatch[2] ?? "1"), |
| 36 | + newStart: parseInt(hunkMatch[3]), |
| 37 | + newCount: parseInt(hunkMatch[4] ?? "1"), |
| 38 | + lines: [], |
| 39 | + }; |
| 40 | + hunks.push(currentHunk); |
| 41 | + oldLineNum = currentHunk.oldStart; |
| 42 | + newLineNum = currentHunk.newStart; |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + if (!currentHunk) continue; |
| 47 | + |
| 48 | + // Strip [L<n>] annotation if present |
| 49 | + const stripped = line.replace(/^\[L\d+\]\s*/, ""); |
| 50 | + |
| 51 | + if (stripped.startsWith("+") && !stripped.startsWith("+++")) { |
| 52 | + addedLines.set(newLineNum, stripped.slice(1)); |
| 53 | + currentHunk.lines.push(stripped); |
| 54 | + newLineNum++; |
| 55 | + } else if (stripped.startsWith("-") && !stripped.startsWith("---")) { |
| 56 | + removedLines.set(oldLineNum, stripped.slice(1)); |
| 57 | + currentHunk.lines.push(stripped); |
| 58 | + oldLineNum++; |
| 59 | + } else if (!stripped.startsWith("\\")) { |
| 60 | + // Context line |
| 61 | + currentHunk.lines.push(stripped); |
| 62 | + newLineNum++; |
| 63 | + oldLineNum++; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Normalize path: strip leading ./ |
| 68 | + const normalizedPath = filePath.replace(/^\.\//, ""); |
| 69 | + files.set(normalizedPath, { path: normalizedPath, addedLines, removedLines, hunks }); |
| 70 | + } |
| 71 | + |
| 72 | + return files; |
| 73 | +} |
| 74 | + |
| 75 | +/** Check if a given file path + line number is in the diff (on a + line) */ |
| 76 | +export function isInDiff(diffFiles: Map<string, DiffFile>, filePath: string, lineNum: number): boolean { |
| 77 | + // Try exact match first, then suffix match |
| 78 | + const df = diffFiles.get(filePath) ?? [...diffFiles.values()].find(f => filePath.endsWith(f.path) || f.path.endsWith(filePath)); |
| 79 | + if (!df) return false; |
| 80 | + return df.addedLines.has(lineNum); |
| 81 | +} |
0 commit comments