MCPcopy Index your code
hub / github.com/promptfoo/promptfoo / annotateDiffWithLineRanges

Function annotateDiffWithLineRanges

src/codeScan/git/diffAnnotator.ts:23–106  ·  view source on GitHub ↗
(patch: string)

Source from the content-addressed store, hash-verified

21 * Annotate a unified diff patch with absolute line numbers and extract valid line ranges.
22 */
23export function annotateDiffWithLineRanges(patch: string): AnnotationResult {
24 if (!patch || patch.trim() === '') {
25 return { annotatedDiff: patch, lineRanges: [] };
26 }
27
28 const lines = patch.split('\n');
29 const result: string[] = [];
30 const lineRanges: LineRange[] = [];
31 let currentNewLine = 0;
32 let inHunk = false;
33 let hunkStartLine = 0;
34
35 for (let i = 0; i < lines.length; i++) {
36 const line = lines[i];
37 const isLastLine = i === lines.length - 1;
38 // Check if this is a hunk header: @@ -start,count +start,count @@
39 const hunkHeader = parseHunkHeader(line);
40
41 if (hunkHeader) {
42 // Save the previous hunk's range if we have one
43 if (hunkStartLine > 0 && currentNewLine > hunkStartLine) {
44 lineRanges.push({
45 start: hunkStartLine,
46 end: currentNewLine - 1,
47 });
48 }
49
50 // Extract the starting line number for the new file (right side)
51 currentNewLine = hunkHeader.newStart;
52 inHunk = true;
53
54 // Start tracking new hunk (unless it's a pure deletion with 0 new lines)
55 hunkStartLine = hunkHeader.newCount === 0 ? 0 : hunkHeader.newStart;
56
57 // Hunk headers are not annotated
58 result.push(line);
59 continue;
60 }
61
62 // If we haven't encountered a hunk yet, preserve the line as-is
63 // (file headers like "diff --git", "---", "+++" etc.)
64 if (!inHunk) {
65 result.push(line);
66 continue;
67 }
68
69 // Process lines within a hunk
70 if (line.startsWith('-')) {
71 // Removed line - doesn't exist in new file, no line number
72 result.push(line);
73 } else if (line.startsWith('+')) {
74 // Added line - exists in new file at currentNewLine
75 result.push(`L${currentNewLine}: ${line}`);
76 currentNewLine++;
77 } else if (line.startsWith(' ')) {
78 // Context line - exists in new file at currentNewLine
79 result.push(`L${currentNewLine}: ${line}`);
80 currentNewLine++;

Callers 2

generatePatchForFileFunction · 0.90

Calls 3

parseHunkHeaderFunction · 0.90
splitMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…