Skip to content

fix: correct "to" line number in unified diff hunk headers#10

Open
Yanhu007 wants to merge 1 commit intohexops:mainfrom
Yanhu007:fix/unified-hunk-toline
Open

fix: correct "to" line number in unified diff hunk headers#10
Yanhu007 wants to merge 1 commit intohexops:mainfrom
Yanhu007:fix/unified-hunk-toline

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #6

Problem

When a unified diff has multiple hunks and the first hunk contains equal (context) lines between its edits, subsequent hunk headers report incorrect "to" line numbers.

Before (incorrect):

@@ -12,5 +10,5 @@

After (correct, matches git diff):

@@ -12,5 +12,5 @@

Root Cause

In ToUnified, when edits within the same hunk are separated by equal lines (the "within range" case at line 100), addEqualLines adds context lines to the hunk but its return value was discarded. This meant toLine did not account for these intra-hunk equal lines, causing the "to" line offset to drift for subsequent hunks.

Fix

One-line change: accumulate the return value of addEqualLines into toLine for within-range equal lines:

// Before
addEqualLines(h, lines, last, start)

// After
toLine += addEqualLines(h, lines, last, start)

All existing tests pass.

When multiple edits within the same hunk were separated by equal lines,
the equal lines added via addEqualLines in the "within range" case were
not being counted in toLine. This caused subsequent hunk headers to
report incorrect "to" line numbers.

For example, a diff with two hunks where the first hunk had equal lines
between its edits would produce:
  @@ -12,5 +10,5 @@
instead of the correct:
  @@ -12,5 +12,5 @@

Fix by accumulating the return value of addEqualLines into toLine
for within-range equal lines, matching git diff behavior.

Fixes hexops#6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect line numbers in unified hunk headers

1 participant