Repository navigation
fix(diff): emit true hunk offsets and context lines for --change - #121
Merged
Merged
Conversation
`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
marked this pull request as ready for review
July 21, 2026 18:58
vinceblock99
approved these changes
Jul 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
atomic diff --change <hash>renders modifications to existing files as a single all-add hunk anchored at line 1 with zero context: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:DiffHunk::new(1, 0, 1, 0)and stuffed every changed line into it, so the header always read@@ -1,X +1,Y @@Fix
Reworked the FileOps path into two passes:
NumberedLines, tracking the running insert/delete offset (net) that maps each line between old-file and new-file coordinates (an added linenwas inserted after old linen - 1 - net; a removed linelsat at new boundaryl - 1 + net).hunks_from_changed_lines: changes more than2 * contextapart (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):Emitted lines verified line-for-line against the actual file content.
--context 0yields exact zero-context headers (@@ -10,0 +11,1 @@,@@ -52,1 +75,1 @@— the latter matchings.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-clisuite passes (1640 tests), fmt + clippy clean. No frontend change needed — renderers that handle arbitrary hunk offsets, multiple hunks, and context lines will just work.