|
| 1 | +import { exec } from 'node:child_process' |
| 2 | +import { promisify } from 'node:util' |
| 3 | +import type { ChangedLinesMap } from './model.js' |
| 4 | + |
| 5 | +const execAsync = promisify(exec) |
| 6 | + |
| 7 | +/** |
| 8 | + * Get changed lines by comparing against a base ref using local git. |
| 9 | + * Requires the repository to be checked out. |
| 10 | + * |
| 11 | + * @param baseRef - The base ref to compare against (e.g., 'origin/main', commit SHA) |
| 12 | + * @returns Map of filename to set of changed line numbers |
| 13 | + */ |
| 14 | +export async function getChangedLinesFromGit(baseRef: string): Promise<ChangedLinesMap> { |
| 15 | + // Get the unified diff between base ref and HEAD |
| 16 | + const { stdout: diffOutput } = await execAsync(`git diff ${baseRef}...HEAD`, { |
| 17 | + maxBuffer: 10 * 1024 * 1024, // 10MB buffer for large diffs |
| 18 | + }) |
| 19 | + |
| 20 | + return parseDiffOutput(diffOutput) |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Parse full git diff output to extract changed lines per file. |
| 25 | + * |
| 26 | + * @param diffOutput - Full git diff output |
| 27 | + * @returns Map of filename to set of changed line numbers |
| 28 | + */ |
| 29 | +export function parseDiffOutput(diffOutput: string): ChangedLinesMap { |
| 30 | + const changedLines: ChangedLinesMap = new Map() |
| 31 | + const lines = diffOutput.split('\n') |
| 32 | + |
| 33 | + let currentFile: string | null = null |
| 34 | + |
| 35 | + for (let i = 0; i < lines.length; i++) { |
| 36 | + const line = lines[i]! |
| 37 | + |
| 38 | + // Parse diff header: diff --git a/path/to/file b/path/to/file |
| 39 | + const diffMatch = line.match(/^diff --git a\/.+ b\/(.+)$/) |
| 40 | + if (diffMatch) { |
| 41 | + currentFile = diffMatch[1]! |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + // Skip deleted files (indicated by /dev/null in new file) |
| 46 | + if (line.startsWith('+++ /dev/null')) { |
| 47 | + currentFile = null |
| 48 | + continue |
| 49 | + } |
| 50 | + |
| 51 | + // Skip if we don't have a current file |
| 52 | + if (!currentFile) { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + // Parse hunk and extract changed lines |
| 57 | + const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/) |
| 58 | + if (hunkMatch) { |
| 59 | + const startLine = Number.parseInt(hunkMatch[1]!, 10) |
| 60 | + const hunkChangedLines = parseHunkForChangedLines(lines, i, startLine) |
| 61 | + |
| 62 | + // Merge with existing changed lines for this file |
| 63 | + const existing = changedLines.get(currentFile) ?? new Set<number>() |
| 64 | + for (const lineNum of hunkChangedLines) { |
| 65 | + existing.add(lineNum) |
| 66 | + } |
| 67 | + changedLines.set(currentFile, existing) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return changedLines |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Parse a single hunk starting from a given index to extract changed line numbers. |
| 76 | + * |
| 77 | + * @param lines - All lines of the diff |
| 78 | + * @param hunkStartIndex - Index of the hunk header line |
| 79 | + * @param startLine - Starting line number from the hunk header |
| 80 | + * @returns Set of changed line numbers in this hunk |
| 81 | + */ |
| 82 | +function parseHunkForChangedLines(lines: string[], hunkStartIndex: number, startLine: number): Set<number> { |
| 83 | + const changedLines = new Set<number>() |
| 84 | + let currentLine = startLine |
| 85 | + |
| 86 | + // Start after the hunk header |
| 87 | + for (let i = hunkStartIndex + 1; i < lines.length; i++) { |
| 88 | + const line = lines[i]! |
| 89 | + |
| 90 | + // Stop at next hunk or diff header |
| 91 | + if (line.startsWith('@@') || line.startsWith('diff --git')) { |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | + // Lines starting with '-' are removed lines (don't exist in new file) |
| 96 | + if (line.startsWith('-')) { |
| 97 | + // Don't increment currentLine for removed lines |
| 98 | + continue |
| 99 | + } |
| 100 | + |
| 101 | + // Lines starting with '+' are added lines |
| 102 | + if (line.startsWith('+')) { |
| 103 | + changedLines.add(currentLine) |
| 104 | + currentLine++ |
| 105 | + continue |
| 106 | + } |
| 107 | + |
| 108 | + // Context lines (starting with space or no prefix) exist in both versions |
| 109 | + currentLine++ |
| 110 | + } |
| 111 | + |
| 112 | + return changedLines |
| 113 | +} |
| 114 | + |
| 115 | +/** |
| 116 | + * Parse a unified diff patch to extract added/modified line numbers. |
| 117 | + * |
| 118 | + * The patch format uses @@ -old_start,old_count +new_start,new_count @@ headers |
| 119 | + * followed by context lines (starting with space), removed lines (starting with -) |
| 120 | + * and added lines (starting with +). |
| 121 | + * |
| 122 | + * We only care about added lines (lines starting with +) as those are the |
| 123 | + * "new" lines that exist in the PR's version of the file. |
| 124 | + * |
| 125 | + * @param patch - Unified diff patch string |
| 126 | + * @returns Set of line numbers that were added/modified |
| 127 | + */ |
| 128 | +export function parsePatchForChangedLines(patch: string): Set<number> { |
| 129 | + const changedLines = new Set<number>() |
| 130 | + const lines = patch.split('\n') |
| 131 | + |
| 132 | + let currentLine = 0 |
| 133 | + |
| 134 | + for (const line of lines) { |
| 135 | + // Parse hunk header: @@ -old_start,old_count +new_start,new_count @@ |
| 136 | + const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/) |
| 137 | + if (hunkMatch) { |
| 138 | + currentLine = Number.parseInt(hunkMatch[1]!, 10) |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + // Skip if we haven't seen a hunk header yet |
| 143 | + if (currentLine === 0) { |
| 144 | + continue |
| 145 | + } |
| 146 | + |
| 147 | + // Lines starting with '-' are removed lines (don't exist in new file) |
| 148 | + if (line.startsWith('-')) { |
| 149 | + // Don't increment currentLine for removed lines |
| 150 | + continue |
| 151 | + } |
| 152 | + |
| 153 | + // Lines starting with '+' are added lines |
| 154 | + if (line.startsWith('+')) { |
| 155 | + changedLines.add(currentLine) |
| 156 | + currentLine++ |
| 157 | + continue |
| 158 | + } |
| 159 | + |
| 160 | + // Context lines (starting with space or no prefix) exist in both versions |
| 161 | + // We don't add them to changed lines, but we do increment the line counter |
| 162 | + currentLine++ |
| 163 | + } |
| 164 | + |
| 165 | + return changedLines |
| 166 | +} |
0 commit comments