From aa09599a3d15e94d1fb317f65155ce22e6d9fc58 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Fri, 29 Oct 2010 18:35:43 -0700 Subject: [PATCH] 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 --- .../jgit/diff/AbstractDiffTestCase.java | 7 +++++++ .../org/eclipse/jgit/diff/DiffAlgorithm.java | 19 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/AbstractDiffTestCase.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/AbstractDiffTestCase.java index a46a4bd70..bee333b16 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/AbstractDiffTestCase.java +++ b/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); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffAlgorithm.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffAlgorithm.java index 2fa89cc58..f6859a29c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffAlgorithm.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/DiffAlgorithm.java @@ -85,7 +85,24 @@ public abstract class DiffAlgorithm { SubsequenceComparator cs = new SubsequenceComparator(cmp); Subsequence as = Subsequence.a(a, region); Subsequence 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: