From 398ae9082f4fd339d54e87e63bab3c2b80db9365 Mon Sep 17 00:00:00 2001 From: TaduJR Date: Tue, 3 Mar 2026 20:59:05 +0300 Subject: [PATCH 1/3] fix: use contextLineCount counter instead of storing context lines in hunk.lines --- scripts/utils/Git.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/scripts/utils/Git.ts b/scripts/utils/Git.ts index c340c0fe182e..523ef31cf8c0 100644 --- a/scripts/utils/Git.ts +++ b/scripts/utils/Git.ts @@ -39,7 +39,7 @@ const GITHUB_BASE_REF = process.env.GITHUB_BASE_REF as string | undefined; /** * Represents a single changed line in a git diff. - * Only added and removed lines are tracked (context lines are skipped during parsing). + * Only added and removed lines are tracked (context lines are counted separately). */ type DiffLine = { number: number; @@ -56,6 +56,7 @@ type DiffHunk = { newStart: number; newCount: number; lines: DiffLine[]; + contextLineCount: number; }; /** @@ -239,6 +240,7 @@ class Git { newStart, newCount, lines: [], + contextLineCount: 0, }; } continue; @@ -268,7 +270,8 @@ class Git { content, }); } else if (firstChar === ' ') { - // Context line - skip it (we only care about added/removed lines) + // Context line - count it so calculateLineNumber accounts for position advancement + currentHunk.contextLineCount++; continue; } else if (firstChar === '\\') { // "No newline at end of file" marker - skip it (metadata, not content) @@ -339,9 +342,9 @@ class Git { switch (lineType) { case 'added': - return hunk.newStart + addedCount; + return hunk.newStart + hunk.contextLineCount + addedCount; case 'removed': - return hunk.oldStart + removedCount; + return hunk.oldStart + hunk.contextLineCount + removedCount; default: throw new Error(`Unknown line type: ${String(lineType)}`); } @@ -560,6 +563,7 @@ class Git { newStart: 1, newCount: lines.length, lines: diffLines, + contextLineCount: 0, }; const fileDiff: FileDiff = { From 07ce78ccec48756c47e85d133fe11b30ccfc33b9 Mon Sep 17 00:00:00 2001 From: TaduJR Date: Tue, 3 Mar 2026 21:26:29 +0300 Subject: [PATCH 2/3] fix: rebuild getPullRequestIncrementalChanges bundle --- .../javascript/getPullRequestIncrementalChanges/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/actions/javascript/getPullRequestIncrementalChanges/index.js b/.github/actions/javascript/getPullRequestIncrementalChanges/index.js index 16fcffa08e5d..a4e00405a379 100644 --- a/.github/actions/javascript/getPullRequestIncrementalChanges/index.js +++ b/.github/actions/javascript/getPullRequestIncrementalChanges/index.js @@ -12506,6 +12506,7 @@ class Git { newStart, newCount, lines: [], + contextLineCount: 0, }; } continue; @@ -12533,7 +12534,8 @@ class Git { }); } else if (firstChar === ' ') { - // Context line - skip it (we only care about added/removed lines) + // Context line - count it so calculateLineNumber accounts for position advancement + currentHunk.contextLineCount++; continue; } else if (firstChar === '\\') { @@ -12597,9 +12599,9 @@ class Git { const removedCount = hunk.lines.filter((l) => l.type === 'removed').length; switch (lineType) { case 'added': - return hunk.newStart + addedCount; + return hunk.newStart + hunk.contextLineCount + addedCount; case 'removed': - return hunk.oldStart + removedCount; + return hunk.oldStart + hunk.contextLineCount + removedCount; default: throw new Error(`Unknown line type: ${String(lineType)}`); } @@ -12795,6 +12797,7 @@ class Git { newStart: 1, newCount: lines.length, lines: diffLines, + contextLineCount: 0, }; const fileDiff = { filePath, From e767c4d9db98e579c8d8e743a8bcc1cc2f4821b1 Mon Sep 17 00:00:00 2001 From: TaduJR Date: Tue, 3 Mar 2026 21:37:30 +0300 Subject: [PATCH 3/3] test: add unit tests for parseDiff context line handling --- tests/unit/GitTest.ts | 203 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) diff --git a/tests/unit/GitTest.ts b/tests/unit/GitTest.ts index 1a7c3515ad5f..bcb705ae3260 100644 --- a/tests/unit/GitTest.ts +++ b/tests/unit/GitTest.ts @@ -982,6 +982,209 @@ describe('Git', () => { }); }); + describe('parseDiff with context lines', () => { + it('calculates correct line numbers when context lines are present', () => { + // GitHub API diffs include 3 context lines by default + const diffWithContext = dedent(` + diff --git a/src/languages/en.ts b/src/languages/en.ts + --- a/src/languages/en.ts + +++ b/src/languages/en.ts + @@ -100,6 +100,7 @@ + formatPolicyRules + exportToCSV + duplicateExpense + +cannotDuplicateDistanceExpense + reportField + reportFieldList + reportFieldRule + `); + + const result = Git.parseDiff(diffWithContext); + + expect(result.hasChanges).toBe(true); + expect(result.files).toHaveLength(1); + + const file = result.files.at(0); + expect(file).toBeDefined(); + if (!file) { + return; + } + + const hunk = file.hunks.at(0); + expect(hunk).toBeDefined(); + if (!hunk) { + return; + } + + // Context lines should NOT appear in hunk.lines + expect(hunk.lines).toHaveLength(1); + expect(hunk.lines.at(0)).toEqual({ + number: 103, + type: 'added', + content: 'cannotDuplicateDistanceExpense', + }); + + // Context lines should be counted + expect(hunk.contextLineCount).toBe(6); + + // addedLines should reflect the correct line number + expect(file.addedLines.has(103)).toBe(true); + }); + + it('calculates correct line numbers with zero context lines', () => { + // Local git diff uses -U0 + const diffWithoutContext = dedent(` + diff --git a/src/languages/en.ts b/src/languages/en.ts + --- a/src/languages/en.ts + +++ b/src/languages/en.ts + @@ -102,0 +103 @@ + +cannotDuplicateDistanceExpense + `); + + const result = Git.parseDiff(diffWithoutContext); + + const hunk = result.files.at(0)?.hunks.at(0); + expect(hunk).toBeDefined(); + if (!hunk) { + return; + } + + expect(hunk.lines).toHaveLength(1); + expect(hunk.lines.at(0)?.number).toBe(103); + expect(hunk.contextLineCount).toBe(0); + }); + + it('handles context lines interleaved with additions and removals', () => { + const diff = dedent(` + diff --git a/file.ts b/file.ts + --- a/file.ts + +++ b/file.ts + @@ -10,5 +10,6 @@ + unchanged1 + -removed1 + +replaced1 + +added1 + unchanged2 + unchanged3 + `); + + const result = Git.parseDiff(diff); + const hunk = result.files.at(0)?.hunks.at(0); + expect(hunk).toBeDefined(); + if (!hunk) { + return; + } + + // Only changed lines in hunk.lines + expect(hunk.lines).toHaveLength(3); + expect(hunk.lines.at(0)).toEqual({number: 11, type: 'removed', content: 'removed1'}); + expect(hunk.lines.at(1)).toEqual({number: 11, type: 'added', content: 'replaced1'}); + expect(hunk.lines.at(2)).toEqual({number: 12, type: 'added', content: 'added1'}); + + expect(hunk.contextLineCount).toBe(3); + }); + + it('produces same line numbers for -U0 and context diffs', () => { + // Same logical change, different context levels + const withContext = dedent(` + diff --git a/file.ts b/file.ts + --- a/file.ts + +++ b/file.ts + @@ -8,4 +8,5 @@ + line8 + line9 + line10 + +newLine11 + line11 + `); + + const withoutContext = dedent(` + diff --git a/file.ts b/file.ts + --- a/file.ts + +++ b/file.ts + @@ -10,0 +11 @@ + +newLine11 + `); + + const resultWithContext = Git.parseDiff(withContext); + const resultWithoutContext = Git.parseDiff(withoutContext); + + const lineWithContext = resultWithContext.files.at(0)?.hunks.at(0)?.lines.at(0)?.number; + const lineWithoutContext = resultWithoutContext.files.at(0)?.hunks.at(0)?.lines.at(0)?.number; + + expect(lineWithContext).toBe(11); + expect(lineWithoutContext).toBe(11); + expect(lineWithContext).toBe(lineWithoutContext); + }); + + it('calculates correct removed line numbers with context lines', () => { + const diff = dedent(` + diff --git a/file.ts b/file.ts + --- a/file.ts + +++ b/file.ts + @@ -5,4 +5,3 @@ + context1 + context2 + -removedLine + context3 + `); + + const result = Git.parseDiff(diff); + const file = result.files.at(0); + expect(file).toBeDefined(); + if (!file) { + return; + } + + const hunk = file.hunks.at(0); + expect(hunk).toBeDefined(); + if (!hunk) { + return; + } + + expect(hunk.lines).toHaveLength(1); + expect(hunk.lines.at(0)).toEqual({number: 7, type: 'removed', content: 'removedLine'}); + expect(hunk.contextLineCount).toBe(3); + expect(file.removedLines.has(7)).toBe(true); + }); + + it('tracks contextLineCount independently per hunk', () => { + const diff = dedent(` + diff --git a/file.ts b/file.ts + --- a/file.ts + +++ b/file.ts + @@ -1,3 +1,4 @@ + ctx1 + ctx2 + +addedInFirstHunk + ctx3 + @@ -20,2 +21,3 @@ + ctx4 + +addedInSecondHunk + ctx5 + `); + + const result = Git.parseDiff(diff); + const file = result.files.at(0); + expect(file).toBeDefined(); + if (!file) { + return; + } + + expect(file.hunks).toHaveLength(2); + + const firstHunk = file.hunks.at(0); + expect(firstHunk?.contextLineCount).toBe(3); + expect(firstHunk?.lines).toHaveLength(1); + expect(firstHunk?.lines.at(0)).toEqual({number: 3, type: 'added', content: 'addedInFirstHunk'}); + + const secondHunk = file.hunks.at(1); + expect(secondHunk?.contextLineCount).toBe(2); + expect(secondHunk?.lines).toHaveLength(1); + expect(secondHunk?.lines.at(0)).toEqual({number: 22, type: 'added', content: 'addedInSecondHunk'}); + }); + }); + describe('getUntrackedFiles', () => { it('returns array of untracked file paths', () => { mockExecSync.mockReturnValue('src/new-file.ts\nsrc/another-file.tsx\n');