diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java index 395fb7fed..e18d1c4eb 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java @@ -134,6 +134,25 @@ public class RawTextTest extends TestCase { assertEquals(new Edit(2, 3, 2, 3), e); } + public void testComparatorReduceCommonStartEnd_EmptyLine() + throws UnsupportedEncodingException { + RawText a; + RawText b; + Edit e; + + a = new RawText("R\n y\n".getBytes("UTF-8")); + b = new RawText("S\n\n y\n".getBytes("UTF-8")); + e = new Edit(0, 2, 0, 3); + e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e); + assertEquals(new Edit(0, 1, 0, 2), e); + + a = new RawText("S\n\n y\n".getBytes("UTF-8")); + b = new RawText("R\n y\n".getBytes("UTF-8")); + e = new Edit(0, 3, 0, 2); + e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e); + assertEquals(new Edit(0, 2, 0, 1), e); + } + private static RawText t(String text) { StringBuilder r = new StringBuilder(); for (int i = 0; i < text.length(); i++) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java index f9cf376ab..3576c5004 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java @@ -303,8 +303,16 @@ public abstract class RawTextComparator extends SequenceComparator { e.beginB = findForwardLine(b.lines, e.beginB, bPtr); e.endA = findReverseLine(a.lines, e.endA, aEnd); + + final boolean partialA = aEnd < a.lines.get(e.endA + 1); + if (partialA) + bEnd += a.lines.get(e.endA + 1) - aEnd; + e.endB = findReverseLine(b.lines, e.endB, bEnd); + if (!partialA && bEnd < b.lines.get(e.endB + 1)) + e.endA++; + return super.reduceCommonStartEnd(a, b, e); }