Repository navigation
diff: merge hunks separated by up to twice the context size - #306
Merged
Merged
Conversation
GNU diff keeps two changes in the same unified or context hunk when at most 2*N unchanged lines separate them. We split them earlier (after N lines for -u, N+1 for -c), so we printed separate hunks where GNU prints one, and -u could start a hunk with no leading context. Fixes uutils#268
sylvestre
reviewed
Oct 18, 2026
| let from = ["a", "b", "c", "d", "e", "f", "g", ""].join("\n"); | ||
| let to = ["a", "B", "c", "d", "e", "F", "g", ""].join("\n"); | ||
| let expected = [ | ||
| "--- foo\tTIMESTAMP", |
Collaborator
There was a problem hiding this comment.
could you please also add the case from the issue (an insertion, so different line counts on both sides)?
in a different PR
Collaborator
|
Thanks for your PR |
Contributor
Author
|
Hey, of course. Here it is: #307 |
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.
Fixes #268.
GNU diff keeps two changes in one hunk as long as at most
2*Nunchanged lines separate them (N= context size). We were splitting earlier: afterNlines in unified mode andN+1in context mode. With-U1on the shopping files from the issue, that gave two hunks, the second one without its leading context line.Both
make_difffunctions now start a new hunk only when the gap is larger than2*N. Context lines in between already go throughcontext_queue, so they now get added to the current hunk.Added
test_hunk_mergingtounified_diff.rsandcontext_diff.rs(gap of2*Nmerges,2*N+1splits; the expected output comes from GNU diff 3.10). I also ran a few thousand random small file pairs with-U0..4and-C0..4. The output still applies withpatch, and no two hunks touch or overlap anymore. Before this change, about 840 of 3000 runs had adjacent hunks.cargo test,cargo clippy -- -D warningsandcargo fmt --checkpass.