fix: split dense hunks when staging non-contiguous replacements - #2
Open
andresespinosapc wants to merge 1 commit into
Open
Conversation
When the user selects multiple non-contiguous delete+add pairs from a single dense replacement hunk (e.g. `file:-2,2,-4,4` against a hunk of `-b,-c,-d,+B,+C,+D`), the previous implementation collapsed the kept deletions and additions into a single output hunk. git apply rejected the resulting patch with `patch does not apply` because the deleted lines are not consecutive in HEAD. `FilteredContent.additions` now preserves the new-line position of each addition, letting `into_hunks` split the mixed (deletions + additions) case into one hunk per contiguous deletion group, paired by index with the corresponding contiguous addition group. The split applies only when group counts match and no no-newline state is involved; asymmetric selections fall back to the previous single-hunk behavior. Adds e2e test 3.10 (`non_contiguous_in_dense_hunk`) covering the bug and updates the unit tests that asserted on the `additions` field. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
When the user selects multiple non-contiguous delete+add pairs from a single dense replacement hunk (e.g.
file:-2,2,-4,4against a hunk of-b,-c,-d,+B,+C,+D), the previous implementation collapsed the kept deletions and additions into a single output hunk.git applyrejected the resulting patch withpatch does not applybecause the deleted lines are not consecutive in HEAD.Repro
This is the case the existing
// Case 3: Mixed ... For now, keep as single hunk - more complex splitting could be added latercomment ininto_hunksflagged.Fix
FilteredContent.additionsnow preserves the new-line position of each addition (Vec<(u32, String)>instead ofVec<String>), lettinginto_hunkssplit the mixed (deletions + additions) case into one hunk per contiguous deletion group, paired by index with the corresponding contiguous addition group. The split applies only when group counts match and no no-newline state is involved; asymmetric selections fall back to the previous single-hunk behavior.After the fix:
Test plan
replacement::non_contiguous_in_dense_hunk(3.10) covering the bugFilteredContent.additionsfor the new tuple type🤖 Generated with Claude Code