Skip to content

fix(diff): emit true hunk offsets and context lines for --change - #121

Merged
graywolf336 merged 1 commit into
devfrom
fix/diff-change-hunk-offsets
Jul 21, 2026
Merged

graywolf336 merged 1 commit into
devfrom
fix/diff-change-hunk-offsets

Conversation

@graywolf336

Copy link
Copy Markdown
Contributor

Problem

atomic diff --change <hash> renders modifications to existing files as a single all-add hunk anchored at line 1 with zero context:

diff --atomic a/internal/store/store.go b/internal/store/store.go (+67)
@@ -1,0 +1,67 @@      ← 0 old lines, 67 new lines starting at line 1

Downstream renderers faithfully number the added lines 1…N per that header — but the numbers don't match where the changed code actually lives in the file. (Reported by a renderer author who verified the bug is in atomic's emitted diff, not their numbering logic.)

Follow-up to #120 (same command, related file-filter fix).

Root cause

The record side already stores accurate old/new line numbers on every branch operation — I dumped the FileOps for a real change and found correct values (first insert at new line 11, exact old↔new mappings via Reparent ops). The display layer (show_change_diff_from_file_ops) simply:

  1. Created one DiffHunk::new(1, 0, 1, 0) and stuffed every changed line into it, so the header always read @@ -1,X +1,Y @@
  2. Never split hunks on gaps
  3. Never emitted context lines (FileOps alone don't carry unchanged content)

Fix

Reworked the FileOps path into two passes:

  1. Flatten line ops into NumberedLines, tracking the running insert/delete offset (net) that maps each line between old-file and new-file coordinates (an added line n was inserted after old line n - 1 - net; a removed line l sat at new boundary l - 1 + net).
  2. Group via hunks_from_changed_lines: changes more than 2 * context apart (old-file coordinates) split into separate hunks, closer ones merge. Headers follow git conventions (@@ -p,0 +n,k @@ pure insertion, @@ -l,k +p,0 @@ pure deletion, context counted on both sides). Context content comes from the file's recorded before-content, fetched lazily only for unified output with --context > 0; failures degrade gracefully to zero context with correct offsets.

Verification

Real change (internal/store/store.go, +60 -4, edits mid-file):

# Before
@@ -1,4 +1,60 @@          ← one hunk at line 1, no context

# After
@@ -8,11 +8,14 @@           ← import block, 3 context lines each side
@@ -27,15 +30,29 @@         ← New(), with context
@@ -44,13 +61,52 @@        ← backupCorrupt/maxSeq, with context

Emitted lines verified line-for-line against the actual file content. --context 0 yields exact zero-context headers (@@ -10,0 +11,1 @@, @@ -52,1 +75,1 @@ — the latter matching s.seq = maxSeq(links) at real line 75). New files now get git's @@ -0,0 +1,N @@; full-file deletions get @@ -1,N +0,0 @@.

9 new unit tests (insertion/deletion/modify offsets, hunk split/merge, new-file and full-deletion conventions). Full atomic-cli suite passes (1640 tests), fmt + clippy clean. No frontend change needed — renderers that handle arbitrary hunk offsets, multiple hunks, and context lines will just work.

`atomic diff --change <hash>` rendered every file as a single hunk
anchored at line 1 with zero context lines (e.g. `@@ -1,0 +1,67 @@`
for a change whose edits live mid-file), so downstream renderers
numbered added lines 1..N regardless of where the code actually sits.

The record side already stores accurate old/new line numbers on every
branch operation; the display layer simply ignored them when building
the hunk header, and never had access to file content for context.

Rework show_change_diff_from_file_ops into two passes:

1. Flatten line ops into NumberedLines, tracking the running
   insert/delete offset (`net`) that maps each line between old-file
   and new-file coordinates.
2. Group them into hunks via hunks_from_changed_lines: changes more
   than 2*context apart (in old-file coordinates) split into separate
   hunks, closer ones merge; headers follow git conventions
   (`@@ -p,0 +n,k @@` for pure insertions, `@@ -l,k +p,0 @@` for
   pure deletions, context counted on both sides). Context line
   content comes from the file's recorded before-content, fetched
   lazily only for unified output with --context > 0; failures
   degrade to zero context with correct offsets.

Before: @@ -1,4 +1,60 @@            (one hunk, anchored at 1, no context)
After:  @@ -8,11 +8,14 @@          (import block, with context)
        @@ -27,15 +30,29 @@        (New(), with context)
        @@ -44,13 +61,52 @@       (backupCorrupt/maxSeq, with context)

Adds 9 unit tests covering insertion/deletion/modify offsets, hunk
splitting and merging, new-file and full-deletion conventions.
@graywolf336
graywolf336 marked this pull request as ready for review July 21, 2026 18:58
@graywolf336
graywolf336 merged commit a76353a into dev Jul 21, 2026
8 checks passed
@graywolf336
graywolf336 deleted the fix/diff-change-hunk-offsets branch July 21, 2026 21:39
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.

2 participants