From 8ea558bd8245097a05dfa8600f765c670b0bad5e Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 13 Oct 2010 20:43:11 -0700 Subject: [PATCH] Fix RawTextComparator reduceCommonStartEnd at empty lines When an empty line was inserted at the beginning of the common end part of a RawText the comparator incorrectly considered it to be common, which meant the DiffAlgorithm would later not even have it be part of the region it examines. This would cause JGit to skip a line of insertion, which later confused Gerrit Code Review when it tried to match up the pre and post RawText files for a difference that had this type of insertion. Define two new unit tests to check for this insertion of a blank line condition and correct for it by removing the LF from the common region when the condition is detected. Change-Id: I2108570eb2929803b9a56f9fb9c400c758e7156b Signed-off-by: Shawn O. Pearce --- .../org/eclipse/jgit/diff/RawTextTest.java | 19 +++++++++++++++++++ .../eclipse/jgit/diff/RawTextComparator.java | 8 ++++++++ 2 files changed, 27 insertions(+) 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 void testComparatorReduceCommonStartEnd() 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 Edit reduceCommonStartEnd(RawText a, RawText b, Edit e) { 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); }