Repository navigation
Conversation
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 <2015900818@qq.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
UnifiedDiffUtils.generateUnifiedDifftreats any single-delta patch with anulloriginal file name as a newly created file. The new-file flag reachesprocessDeltas, which then hardcodes the hunk start of the original side to0regardless 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):
Actual output:
diff -U0andgit diff -U0write@@ -2,1 +2,1 @@for the same change:(
difflib.unified_diff(..., n=0)produces@@ -2 +2 @@as well.)The damage is not only cosmetic.
parseUnifiedDiffapplies the library's own "an empty range names the line before it" rule to the0and produces aChangeDeltaat position 0 instead of position 1, so the generated diff no longer applies to the file it was generated from:PatchFailedException: could not apply patch due to CONTENT_DOES_NOT_MATCH_TARGET[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:196The 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":
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.testSingleDeltaWithNullOriginalNameKeepsOriginalLineNumbersasserts the header, the body and — importantly — that the generated diff still parses back and applies to the original.Run with the unmodified baseline (
git stashof the one-line source change, test kept):With the fix (the whole reactor,
mvn -B packageon JDK 17):The 3 skipped tests are skipped on
masteras well. Spotless (palantirJavaFormat, bound tovalidate) is applied to the changed files.Note
UnifiedDiffWriter.processDeltascarries the samepatchDeltas.size() == 1 && file.getFromFile() == nullcondition, so aUnifiedDiffFilewhosefromFileis 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.