Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,16 @@ public static List<String> generateUnifiedDiff(
}
}
// don't forget to process the last set of Deltas
// A missing original file name only means "new file" when there is no
// original side to number. Otherwise the hunk has to be numbered like any
// other one, as "diff -U0" and "git diff -U0" do.
List<String> curBlock = processDeltas(
originalLines, deltas, contextSize, patchDeltas.size() == 1 && originalFileName == null);
originalLines,
deltas,
contextSize,
patchDeltas.size() == 1
&& originalFileName == null
&& delta.getSource().size() == 0);
ret.addAll(curBlock);
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ public void testZeroContextEmptyRangeHeader() throws PatchFailedException {
assertEquals(revised, UnifiedDiffUtils.parseUnifiedDiff(udiff).applyTo(original));
}

/**
* A patch with a single delta and a missing original file name is not a new
* file: the hunk still has to be numbered from the original side, as
* {@code diff -U0} and {@code git diff -U0} do.
*/
@Test
public void testSingleDeltaWithNullOriginalNameKeepsOriginalLineNumbers() throws PatchFailedException {
List<String> original = Arrays.asList("a", "b", "c");
List<String> revised = Arrays.asList("a", "x", "c");
Patch<String> patch = DiffUtils.diff(original, revised);

List<String> udiff = UnifiedDiffUtils.generateUnifiedDiff(null, "revised", original, patch, 0);

assertEquals("--- /dev/null", udiff.get(0));
assertEquals("+++ revised", udiff.get(1));
assertEquals("@@ -2,1 +2,1 @@", udiff.get(2));
assertEquals(Arrays.asList("-b", "+x"), udiff.subList(3, 5));
assertEquals(revised, UnifiedDiffUtils.parseUnifiedDiff(udiff).applyTo(original));
}

@Test
public void testParseZeroContextEmptyRanges() throws PatchFailedException {
// as written by diff -U0 and git diff -U0
Expand Down