]> source.dussan.org Git - jgit.git/commitdiff
Fix PlotCommit for commits with duplicate parents 92/6592/3
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Sun, 5 Aug 2012 10:50:06 +0000 (12:50 +0200)
committerRobin Rosenberg <robin.rosenberg@dewire.com>
Sun, 5 Aug 2012 10:50:06 +0000 (12:50 +0200)
JGit allows to create commits which have duplicate parents: e.g. a
commit X has first parent Y and second parent Y. Such commits are not
handled correctly by PlotCommit leading to wrong display of the history
in EGit. In such cases there is a never ending passing line drawn beside
all commits younger than the commit with duplicate parents. This commit
fixes this by explicitly checking for duplicate parents.

In a different commit we should fix JGit not to create commits with
duplicate parents. I think native git also doesn't allow such commits,
although history display in native git (gitk, git log --graph) is not
damaged by such commits.

Change-Id: Ie3019ef613a507023958bea27b1badc3b8950279

org.eclipse.jgit.test/tst/org/eclipse/jgit/revplot/PlotCommitListTest.java
org.eclipse.jgit/src/org/eclipse/jgit/revplot/PlotCommit.java

index 57d4c39e3721e2045b221a56f8c31b59026669f2..5db6b86f81114c4925f2bde22f900bd1708c981d 100644 (file)
@@ -75,6 +75,13 @@ public class PlotCommitListTest extends RevWalkTestCase {
                        return this;
                }
 
+               public CommitListAssert nrOfPassingLanes(int lanes) {
+                       assertEquals("Number of passing lanes of commit #"
+                                       + (nextIndex - 1)
+                                       + " not as expected.", lanes, current.passingLanes.length);
+                       return this;
+               }
+
                public CommitListAssert parents(RevCommit... parents) {
                        assertEquals("Number of parents of commit #" + (nextIndex - 1)
                                        + " not as expected.", parents.length,
@@ -308,4 +315,31 @@ public class PlotCommitListTest extends RevWalkTestCase {
                test.commit(merge_fix).parents().lanePos(3);
                test.noMoreCommits();
        }
+
+       // test a history where a merge commit has two time the same parent
+       @Test
+       public void testDuplicateParents() throws Exception {
+               final RevCommit m1 = commit();
+               final RevCommit m2 = commit(m1);
+               final RevCommit m3 = commit(m2, m2);
+
+               final RevCommit s1 = commit(m2);
+               final RevCommit s2 = commit(s1);
+
+               PlotWalk pw = new PlotWalk(db);
+               pw.markStart(pw.lookupCommit(m3));
+               pw.markStart(pw.lookupCommit(s2));
+               PlotCommitList<PlotLane> pcl = new PlotCommitList<PlotLane>();
+               pcl.source(pw);
+               pcl.fillTo(Integer.MAX_VALUE);
+
+               CommitListAssert test = new CommitListAssert(pcl);
+               test.commit(s2).nrOfPassingLanes(0);
+               test.commit(s1).nrOfPassingLanes(0);
+               test.commit(m3).nrOfPassingLanes(1);
+               test.commit(m2).nrOfPassingLanes(0);
+               test.commit(m1).nrOfPassingLanes(0);
+               test.noMoreCommits();
+       }
+
 }
index 40e6aba11e5b8db317ab93404f17e4c8147f38f7..4a413c306a4194704b996739ef16a2930cedc84a 100644 (file)
@@ -100,9 +100,13 @@ public class PlotCommit<L extends PlotLane> extends RevCommit {
                final int cnt = children.length;
                if (cnt == 0)
                        children = new PlotCommit[] { c };
-               else if (cnt == 1)
-                       children = new PlotCommit[] { children[0], c };
-               else {
+               else if (cnt == 1) {
+                       if (!c.getId().equals(children[0].getId()))
+                               children = new PlotCommit[] { children[0], c };
+               } else {
+                       for (PlotCommit pc : children)
+                               if (c.getId().equals(pc.getId()))
+                                       return;
                        final PlotCommit[] n = new PlotCommit[cnt + 1];
                        System.arraycopy(children, 0, n, 0, cnt);
                        n[cnt] = c;