GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — diff: merge hunks separated by up to twice the context size by emirmrkaljevic · Pull Request #306 · uutils/diffutils

Conversation

@emirmrkaljevic

@emirmrkaljevic emirmrkaljevic commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #268.

GNU diff keeps two changes in one hunk as long as at most 2*N unchanged lines separate them (N = context size). We were splitting earlier: after N lines in unified mode and N+1 in context mode. With -U1 on the shopping files from the issue, that gave two hunks, the second one without its leading context line.

Both make_diff functions now start a new hunk only when the gap is larger than 2*N. Context lines in between already go through context_queue, so they now get added to the current hunk.

Added test_hunk_merging to unified_diff.rs and context_diff.rs (gap of 2*N merges, 2*N+1 splits; the expected output comes from GNU diff 3.10). I also ran a few thousand random small file pairs with -U0..4 and -C0..4. The output still applies with patch, and no two hunks touch or overlap anymore. Before this change, about 840 of 3000 runs had adjacent hunks. cargo test, cargo clippy -- -D warnings and cargo fmt --check pass.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@emirmrkaljevic
diff: merge hunks separated by up to twice the context size
771db10
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
sylvestre reviewed Oct 18, 2026
View reviewed changes
Comment thread src/unified_diff.rs
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",

@sylvestre sylvestre Oct 18, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also add the case from the issue (an insertion, so different line counts on both sides)?
in a different PR

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sylvestre
sylvestre merged commit 141e2d6 into uutils:main Oct 18, 2026
31 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@sylvestre

sylvestre commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks for your PR

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@emirmrkaljevic emirmrkaljevic mentioned this pull request Oct 18, 2026
Add hunk merging test for the insertion case from #268 #307
Merged
@emirmrkaljevic

emirmrkaljevic commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Hey, of course. Here it is: #307

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@BrewTestBot BrewTestBot mentioned this pull request Oct 23, 2026
uutils-diffutils 0.6.0 Homebrew/homebrew-core#320660
Merged
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sylvestre sylvestre sylvestre left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

context hunk merging in diff

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@emirmrkaljevic @sylvestre
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.