context hunk merging in diff

Open
#268 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
rust
Domain
cli

Research direction

Reproduce the issue with the shopping-old.txt and shopping-new.txt examples using -U1 and -U2, then compare the unified hunks with the official diff utility. No source file or test is named; trace the unified-diff hunk generation and merging entry point, and consider the issue done when overlapping or abutting context produces one merged hunk.

Written by the indexing model from the issue text.

Description

In playing with diffutils in the uutils playground web page, I noticed that -U1 on the shopping files was outputting a second hunk that has no prior context line. The official diff utility joins hunks together when the context overlaps or even just abuts.

My test:

uutils $ diff shopping-old.txt shopping-new.txt -U1
--- shopping-old.txt    2026-08-09 03:43:13.212000000 +0000
+++ shopping-new.txt    2026-08-09 03:43:13.212000000 +0000
@@ -2,2 +2,3 @@
 eggs
+yogurt
 bread
@@ -4,2 +5,2 @@
-butter
+honey
 apples

As another test, I added another line of context between the chunks:

uutils $ sed shopping-old.txt >s1 -e 's/bread/bread\nrolls/'
uutils $ sed shopping-new.txt >s2 -e 's/bread/bread\nrolls/'

Now the 1-line context is present on hunk 2, but the normal diff util would join them together since they abut:

uutils $ diff s1 s2 -u1
--- s1  1970-01-01 00:00:00.000000000 +0000
+++ s2  1970-01-01 00:00:00.000000000 +0000
@@ -2,2 +2,3 @@
 eggs
+yogurt
 bread
@@ -4,3 +5,3 @@
 rolls
-butter
+honey
 apples

At this point -U2 has the missing context in the same manner as in the first example.

Dominant language
Rust
Stars
276
Forks
39
Avg merge
3h 27m
Merged PRs (30d)
3

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from uutils/diffutils

All issues in uutils/diffutils

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.