fix: compute first changed diff line from patch hunks

Uses hunk contents to find the first modified line instead of the hunk start
Handles added, removed, and binary-only patches more accurately
Adds tests for patch parsing edge cases
This commit is contained in:
Bohdan Triapitsyn
2026-07-06 01:26:56 +03:00
parent 0f3b7f96c2
commit d8f0ef074b
3 changed files with 67 additions and 20 deletions
@@ -0,0 +1,26 @@
import { describe, expect, test } from 'bun:test';
import { getFirstChangedModifiedLineFromPatch } from './diffPatchUtils';
describe('getFirstChangedModifiedLineFromPatch', () => {
test('returns the first added line instead of the hunk context start', () => {
expect(getFirstChangedModifiedLineFromPatch(`diff --git a/src/file.ts b/src/file.ts
@@ -56,10 +56,11 @@
unchanged 58
unchanged 59
unchanged 60
+changed 61
unchanged 62`)).toBe(59);
});
test('returns the following modified line for deletion-only hunks', () => {
expect(getFirstChangedModifiedLineFromPatch(`@@ -10,4 +10,3 @@
context
-removed
after`)).toBe(11);
});
test('returns null when the patch has no hunk change lines', () => {
expect(getFirstChangedModifiedLineFromPatch('Binary files a/image.png and b/image.png differ')).toBeNull();
});
});