]> source.dussan.org Git - jgit.git/commitdiff
NameRevCommand: Don't use merge cost for first parent 02/11202/3
authorDave Borowitz <dborowitz@google.com>
Fri, 15 Mar 2013 15:10:59 +0000 (08:10 -0700)
committerDave Borowitz <dborowitz@google.com>
Fri, 15 Mar 2013 15:58:14 +0000 (08:58 -0700)
Treat first parent traversals as 1 and higher parents as MERGE_COST,
to match git name-rev. Allow overriding the merge cost during tests to
avoid creating 2^16 commits on the fly.

Change-Id: I0175e0c3ab1abe6722e4241abe2f106d1fe92a69

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/NameRevCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/NameRevCommand.java

index 26dc2d05a62a02a70f73edf6656f12e802772870..b92a636f5818181f0bf9f6f974ad14cc1e3064e9 100644 (file)
@@ -162,7 +162,7 @@ public class NameRevCommandTest extends RepositoryTestCase {
        }
 
        @Test
-       public void oneMergeDifferentLengths() throws Exception {
+       public void onePathMergeLongerFirstParentPath() throws Exception {
                // 0--1--2--4
                //  \--3---/
                RevCommit c0 = tr.commit().create();
@@ -171,27 +171,24 @@ public class NameRevCommandTest extends RepositoryTestCase {
                RevCommit c3 = tr.commit().parent(c0).create();
                RevCommit c4 = tr.commit().parent(c2).parent(c3).create();
                tr.update("master", c4);
-               assertOneResult("master^2~1", c0);
+               assertOneResult("master^2", c3);
+               assertOneResult("master~3", c0);
        }
 
        @Test
-       public void longerPathWithoutMerge() throws Exception {
-               // 0--1--2--4         <- master
-               //  \  \-3-/
-               //   \--5--6--7--8--9 <- branch
+       public void multiplePathsSecondParent() throws Exception {
+               // 0--...--2
+               //  \--1--/
                RevCommit c0 = tr.commit().create();
                RevCommit c1 = tr.commit().parent(c0).create();
-               RevCommit c2 = tr.commit().parent(c1).create();
-               RevCommit c3 = tr.commit().parent(c1).create();
-               RevCommit c4 = tr.commit().parent(c2).parent(c3).create();
-               RevCommit c5 = tr.commit().parent(c0).create();
-               RevCommit c6 = tr.commit().parent(c5).create();
-               RevCommit c7 = tr.commit().parent(c6).create();
-               RevCommit c8 = tr.commit().parent(c7).create();
-               RevCommit c9 = tr.commit().parent(c8).create();
-               tr.update("master", c4);
-               tr.update("branch", c9);
-               assertOneResult("branch~5", c0);
+               RevCommit c = c0;
+               int mergeCost = 5;
+               for (int i = 0; i < mergeCost; i++) {
+                       c = tr.commit().parent(c).create();
+               }
+               RevCommit c2 = tr.commit().parent(c).parent(c1).create();
+               tr.update("master", c2);
+               assertOneResult("master^2~1", git.nameRev().setMergeCost(mergeCost), c0);
        }
 
        private static void assertOneResult(String expected, NameRevCommand nameRev,
index 2397636f3b1345f121612a9d7679a9dc6e7cfe89..fcc02c7fef198b910763cb29740c0267cbd319f2 100644 (file)
@@ -112,6 +112,7 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
        private final List<String> prefixes;
        private final List<Ref> refs;
        private final List<ObjectId> revs;
+       private int mergeCost;
 
        /**
         * Create a new name-rev command.
@@ -120,6 +121,7 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
         */
        protected NameRevCommand(Repository repo) {
                super(repo);
+               mergeCost = MERGE_COST;
                prefixes = new ArrayList<String>(2);
                refs = new ArrayList<Ref>();
                revs = new ArrayList<ObjectId>(2);
@@ -147,9 +149,9 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
                                        break;
                                if (c.getCommitTime() < cutoff)
                                        continue;
-                               long cost = c.cost + (c.getParentCount() > 1 ? MERGE_COST : 1);
                                for (int i = 0; i < c.getParentCount(); i++) {
                                        NameRevCommit p = (NameRevCommit) walk.parseCommit(c.getParent(i));
+                                       long cost = c.cost + (i > 0 ? mergeCost : 1);
                                        if (p.tip == null || compare(c.tip, cost, p.tip, p.cost) < 0) {
                                                if (i > 0) {
                                                        p.tip = c.format().append('^').append(i + 1).toString();
@@ -298,6 +300,11 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
                return this;
        }
 
+       NameRevCommand setMergeCost(int cost) {
+               mergeCost = cost;
+               return this;
+       }
+
        private void addPrefixes(Map<ObjectId, String> nonCommits,
                        FIFORevQueue pending) throws IOException {
                if (!prefixes.isEmpty()) {