]> source.dussan.org Git - jgit.git/commitdiff
Fix NameRevCommand when repo has no annotated tags 04/23904/2
authorDave Borowitz <dborowitz@google.com>
Wed, 26 Mar 2014 01:34:54 +0000 (18:34 -0700)
committerDave Borowitz <dborowitz@google.com>
Wed, 26 Mar 2014 01:39:48 +0000 (18:39 -0700)
Previously, calling addAnnotatedTags() did not modify any state when
there were no annotated tags in the repository. This caused the code
to assume no addFoo() methods had been called, and fell back to the
default of adding refs/*. Instead, use null to indicate neither
addRefs() nor addAnnotatedTags() was called.

Add a test for this behavior.

Change-Id: I9926e5ac17e1a983cd399798993031c72bd79c2c

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

index b92a636f5818181f0bf9f6f974ad14cc1e3064e9..491595498e4a4d3f77a283fa0559adc3db8777dc 100644 (file)
@@ -44,6 +44,7 @@
 package org.eclipse.jgit.api;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.util.Map;
 
@@ -108,6 +109,19 @@ public class NameRevCommandTest extends RepositoryTestCase {
                assertOneResult("tag2", git.nameRev().addAnnotatedTags(), c);
        }
 
+       @Test
+       public void annotatedTagsNoResult() throws Exception {
+               RevCommit c = tr.commit().create();
+               tr.update("refs/heads/master", c);
+               tr.update("refs/tags/tag1", c);
+               tr.update("refs/tags/tag2", c);
+               Map<ObjectId, String> result = git.nameRev()
+                               .add(c)
+                               .addAnnotatedTags()
+                               .call();
+               assertTrue(result.toString(), result.isEmpty());
+       }
+
        @Test
        public void simpleAncestor() throws Exception {
                // 0--1--2
index 95a1f352b1cf6c4fc0a2a2cd46ac2f7880be2784..cce42fc51aa4f52feb237f71c848df4a57ec8a0f 100644 (file)
@@ -112,8 +112,8 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
 
        private final RevWalk walk;
        private final List<String> prefixes;
-       private final List<Ref> refs;
        private final List<ObjectId> revs;
+       private List<Ref> refs;
        private int mergeCost;
 
        /**
@@ -125,7 +125,6 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
                super(repo);
                mergeCost = MERGE_COST;
                prefixes = new ArrayList<String>(2);
-               refs = new ArrayList<Ref>();
                revs = new ArrayList<ObjectId>(2);
                walk = new RevWalk(repo) {
                        @Override
@@ -140,8 +139,10 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
                try {
                        Map<ObjectId, String> nonCommits = new HashMap<ObjectId, String>();
                        FIFORevQueue pending = new FIFORevQueue();
-                       for (Ref ref : refs)
-                               addRef(ref, nonCommits, pending);
+                       if (refs != null) {
+                               for (Ref ref : refs)
+                                       addRef(ref, nonCommits, pending);
+                       }
                        addPrefixes(nonCommits, pending);
                        int cutoff = minCommitTime() - COMMIT_TIME_SLOP;
 
@@ -273,6 +274,8 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
         */
        public NameRevCommand addAnnotatedTags() {
                checkCallable();
+               if (refs == null)
+                       refs = new ArrayList<Ref>();
                try {
                        for (Ref ref : repo.getRefDatabase().getRefs(Constants.R_TAGS).values()) {
                                ObjectId id = ref.getObjectId();
@@ -298,6 +301,8 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
         */
        public NameRevCommand addRef(Ref ref) {
                checkCallable();
+               if (refs == null)
+                       refs = new ArrayList<Ref>();
                refs.add(ref);
                return this;
        }
@@ -312,7 +317,7 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
                if (!prefixes.isEmpty()) {
                        for (String prefix : prefixes)
                                addPrefix(prefix, nonCommits, pending);
-               } else if (refs.isEmpty())
+               } else if (refs == null)
                        addPrefix(Constants.R_REFS, nonCommits, pending);
        }