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 — Number single-delta hunks correctly when the original file name is null by simpleqt · Pull Request #241 · java-diff-utils/java-diff-utils

Conversation

@simpleqt

@simpleqt simpleqt commented Nov 4, 2026

Copy link
Copy Markdown

Problem

UnifiedDiffUtils.generateUnifiedDiff treats any single-delta patch with a null original file name as a newly created file. The new-file flag reaches processDeltas, which then hardcodes the hunk start of the original side to 0 regardless of where the delta is. For a patch that replaces or deletes lines this writes a header naming a line that does not exist.

Reproduction (public API only, no context lines):

List<String> original = Arrays.asList("a", "b", "c");
List<String> revised  = Arrays.asList("a", "x", "c");
Patch<String> patch = DiffUtils.diff(original, revised);

UnifiedDiffUtils.generateUnifiedDiff(null, "revised", original, patch, 0)
        .forEach(System.out::println);

Actual output:

--- /dev/null
+++ revised
@@ -0,1 +2,1 @@
-b
+x

diff -U0 and git diff -U0 write @@ -2,1 +2,1 @@ for the same change:

$ git diff --no-index --no-color -U0 -- a.txt b.txt
--- a/a.txt
+++ b/b.txt
@@ -2 +2 @@
-b
+x

(difflib.unified_diff(..., n=0) produces @@ -2 +2 @@ as well.)

The damage is not only cosmetic. parseUnifiedDiff applies the library's own "an empty range names the line before it" rule to the 0 and produces a ChangeDelta at position 0 instead of position 1, so the generated diff no longer applies to the file it was generated from:

  • replacing or deleting a line → PatchFailedException: could not apply patch due to CONTENT_DOES_NOT_MATCH_TARGET
  • pure insertion → applies silently at the wrong place, e.g. [a, b, c] -> [a, b, c, x] generated with a null name applies as [x, a, b, c]

Appending with one line of context, or using two or more deltas, numbers correctly — the bug is specific to a lone delta whose source side is not empty.

Fix

java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java:196

The new-file handling is now restricted to patches whose source chunk is empty, which is what actually distinguishes "created this file" from "renamed/no original name given":

patchDeltas.size() == 1 && originalFileName == null && delta.getSource().size() == 0

A genuine new file (empty original, null name) still emits @@ -0,0 +1,N @@, unchanged. Patches with two or more deltas are untouched.

Tests

GenerateUnifiedDiffTest.testSingleDeltaWithNullOriginalNameKeepsOriginalLineNumbers asserts the header, the body and — importantly — that the generated diff still parses back and applies to the original.

Run with the unmodified baseline (git stash of the one-line source change, test kept):

[ERROR] GenerateUnifiedDiffTest.testSingleDeltaWithNullOriginalNameKeepsOriginalLineNumbers:168
        expected: <@@ -2,1 +2,1 @@> but was: <@@ -0,1 +2,1 @@>
[ERROR] Tests run: 14, Failures: 1, Errors: 0

With the fix (the whole reactor, mvn -B package on JDK 17):

[INFO] Tests run: 169, Failures: 0, Errors: 0, Skipped: 3      (core module)
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0        (java-diff-utils-jgit)
[INFO] BUILD SUCCESS

The 3 skipped tests are skipped on master as well. Spotless (palantirJavaFormat, bound to validate) is applied to the changed files.

Note

UnifiedDiffWriter.processDeltas carries the same patchDeltas.size() == 1 && file.getFromFile() == null condition, so a UnifiedDiffFile whose fromFile is null has the same numbering problem. I left that out of this change to keep it to one subject — happy to follow up separately if you want both sides fixed.

Sorry, something went wrong.

Uh oh!

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

All reactions
@simpleqt
Number single-delta hunks correctly when the original file name is null
dad228e
generateUnifiedDiff builds the header of a single-delta patch as a new file
whenever the original file name is null, so that patch's hunk start is
hardcoded to 0 in processDeltas. That is only correct when the delta really
inserts into an empty original; for a patch that replaces or deletes lines it
produces a header that names a line that does not exist:

  UnifiedDiffUtils.generateUnifiedDiff(null, "revised", [a, b, c], patch, 0)
  @@ -0,1 +2,1 @@      (should be "@@ -2,1 +2,1 @@", as diff -U0 and git diff -U0 write)

parseUnifiedDiff applies the library's own "0 means 1" rule to such a header
and then places the chunk at position 0, so the patch no longer applies to the
file it was generated from (PatchFailedException, or a silent one-line
misplacement for a pure insertion).

Restrict the new-file handling to patches whose source side is empty. Patches
with two or more deltas are unaffected, and a genuine new file - an empty
original with a null name - still gets "@@ -0,0 +1,N @@".

Fixes the expected result of
UnifiedDiffUtils.generateUnifiedDiff(null, "revised", [a, b, c],
DiffUtils.diff([a, b, c], [a, x, c]), 0).

Signed-off-by: simpleqt <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 4, 2026 16:42
Copilot AI reviewed Nov 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sorry, something went wrong.

Uh oh!

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

All reactions

This branch has not been deployed

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

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot 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.

Uh oh!

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

2 participants

@simpleqt
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.