Browse Source

Fix ugly diff showing insertion of new method

When adding a new method near the end of the sequence we want to
show the full method inserted, and not tear the prior method due
to the common trailing curly brace being consumed as part of the
common end region of the sequences.

Bug: 328895
Change-Id: I233bc40445fb5452863f5fb082bc3097433a8da6
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
stable-0.10
Shawn O. Pearce 14 years ago
parent
commit
aa09599a3d
  1. 7
      org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/AbstractDiffTestCase.java
  2. 19
      org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffAlgorithm.java

7
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/AbstractDiffTestCase.java

@ -166,6 +166,13 @@ public abstract class AbstractDiffTestCase extends TestCase {
assertEquals(new Edit(4, 5, 3, 4), r.get(1));
}
public void testEdit_InsertNearCommonTail() {
EditList r = diff(t("aq}nb"), t("aCq}nD}nb"));
assertEquals(new Edit(1, 1, 1, 2), r.get(0));
assertEquals(new Edit(3, 3, 4, 7), r.get(1));
assertEquals(2, r.size());
}
public EditList diff(RawText a, RawText b) {
return algorithm().diff(RawTextComparator.DEFAULT, a, b);
}

19
org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffAlgorithm.java

@ -85,7 +85,24 @@ public abstract class DiffAlgorithm {
SubsequenceComparator<S> cs = new SubsequenceComparator<S>(cmp);
Subsequence<S> as = Subsequence.a(a, region);
Subsequence<S> bs = Subsequence.b(b, region);
return Subsequence.toBase(diffNonCommon(cs, as, bs), as, bs);
EditList e = Subsequence.toBase(diffNonCommon(cs, as, bs), as, bs);
// The last insertion may need to be shifted later if it
// inserts elements that were previously reduced out as
// common at the end.
//
Edit last = e.get(e.size() - 1);
if (last.getType() == Edit.Type.INSERT) {
while (last.endB < b.size()
&& cmp.equals(b, last.beginB, b, region.endB)) {
last.beginA++;
last.endA++;
last.beginB++;
last.endB++;
}
}
return e;
}
case EMPTY:

Loading…
Cancel
Save