Skip to content

context hunk merging in diff #268

Description

@WayneD

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions