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>
This commit is contained in:
parent
abac15e45e
commit
8ea558bd82
|
@ -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++) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue