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 — Read and write empty hunk ranges the way diff and git do by youdie006 · Pull Request #236 · java-diff-utils/java-diff-utils

Conversation

@youdie006

@youdie006 youdie006 commented Sep 28, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

In a unified diff, a range with a count of 0 names the line before it: diff -U0 and git diff -U0 write an insertion after line 2 as @@ -2,0 +3 @@. java-diff-utils reads and writes that start as the first affected line instead. Its own round trip agrees with itself, but patches exchanged with other tools land one line off:

a,b,c -> a,b,X,c:

master this PR
GNU diff -U0 output parsed by UnifiedDiffUtils.parseUnifiedDiff + applyTo [a, X, b, c] [a, b, X, c]
same with UnifiedDiffReader.parseUnifiedDiff [a, X, b, c] [a, b, X, c]
generateUnifiedDiff(..., 0) header @@ -3,0 +3,1 @@ @@ -2,0 +3,1 @@
that patch applied by GNU patch a,b,c,X a,b,X,c

The parsers already treat -0,0 this way (if (old_ln == 0) old_ln = 1), and the writers already emit @@ -0,0 +1,n @@ for a new file; this extends the same rule to -N,0 for N > 0. Diffs with context of 1 or more only have an empty range when a file is empty, so they are unaffected otherwise.

Compatibility: zero-context patches generated by earlier versions of this library with a -N,0 or +N,0 range (N > 0) will now be read one line later.

Tests are added next to testNewFileCreation in GenerateUnifiedDiffTest, next to testWriteWithNewFile in UnifiedDiffWriterTest, and in UnifiedDiffReaderTest; they fail on master. mvn -B package passes on JDK 8 (154 tests); an earlier run also passed on JDK 11 and for the core module on JDK 17. I also checked the table above against GNU diff and patch.

Written with AI assistance (Claude); I have reviewed the change.

Sorry, something went wrong.

Uh oh!

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

All reactions
youdie006 and others added 2 commits September 28, 2026 20:28
@youdie006
Read and write empty hunk ranges the way diff and git do
ba1556e
In a unified diff a range with a count of 0 names the line before it,
so "@@ -2,0 +3 @@" inserts after line 2. Both parsers took the start
as the first affected line and both writers wrote it that way, so a
zero-context diff from GNU diff or git was applied one line early, and
GNU patch applied ours one line late.
@wumpz
Merge branch 'master' into empty-range-hunk-start
488ab58
@wumpz
wumpz merged commit 1083d88 into java-diff-utils:master Oct 9, 2026
4 checks passed

Uh oh!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

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.

Uh oh!

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

2 participants

@youdie006 @wumpz
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.