Browse Source

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 <spearce@spearce.org>
stable-0.10
Shawn O. Pearce 14 years ago
parent
commit
8ea558bd82
  1. 19
      org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java
  2. 8
      org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java

19
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); 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) { private static RawText t(String text) {
StringBuilder r = new StringBuilder(); StringBuilder r = new StringBuilder();
for (int i = 0; i < text.length(); i++) { for (int i = 0; i < text.length(); i++) {

8
org.eclipse.jgit/src/org/eclipse/jgit/diff/RawTextComparator.java

@ -303,8 +303,16 @@ public abstract class RawTextComparator extends SequenceComparator<RawText> {
e.beginB = findForwardLine(b.lines, e.beginB, bPtr); e.beginB = findForwardLine(b.lines, e.beginB, bPtr);
e.endA = findReverseLine(a.lines, e.endA, aEnd); 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); 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); return super.reduceCommonStartEnd(a, b, e);
} }

Loading…
Cancel
Save