]> source.dussan.org Git - jgit.git/commitdiff
Allow adding single refs or all tags to NameRevCommand 27/11127/2
authorDave Borowitz <dborowitz@google.com>
Wed, 13 Mar 2013 18:05:19 +0000 (11:05 -0700)
committerDave Borowitz <dborowitz@google.com>
Wed, 13 Mar 2013 19:28:58 +0000 (12:28 -0700)
Change-Id: I90e85bc835d11278631afd0e801425a292578bba

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

index 82839e3bc17734a294d21b9ef12aadca48a2296c..f67d669316ccdfdc07953e7e252b5852046fa64e 100644 (file)
@@ -88,6 +88,26 @@ public class NameRevCommandTest extends RepositoryTestCase {
                                c);
        }
 
+       @Test
+       public void ref() throws Exception {
+               RevCommit c = tr.commit().create();
+               tr.update("refs/heads/master", c);
+               tr.update("refs/tags/tag", c);
+               assertOneResult("master",
+                               git.nameRev().addRef(db.getRef("refs/heads/master")), c);
+               assertOneResult("tag",
+                               git.nameRev().addRef(db.getRef("refs/tags/tag")), c);
+       }
+
+       @Test
+       public void annotatedTags() throws Exception {
+               RevCommit c = tr.commit().create();
+               tr.update("refs/heads/master", c);
+               tr.update("refs/tags/tag1", c);
+               tr.update("refs/tags/tag2", tr.tag("tag2", c));
+               assertOneResult("tag2", git.nameRev().addAnnotatedTags(), c);
+       }
+
        @Test
        public void simpleAncestor() throws Exception {
                // 0--1--2
index dfae50a725057f5618899d5a44bcb95161a21812..073802db48098317661264fc39bfa5a74ea285bb 100644 (file)
@@ -110,6 +110,7 @@ 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;
 
        /**
@@ -120,6 +121,7 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
        protected NameRevCommand(Repository repo) {
                super(repo);
                prefixes = new ArrayList<String>(2);
+               refs = new ArrayList<Ref>();
                revs = new ArrayList<ObjectId>(2);
                walk = new RevWalk(repo) {
                        @Override
@@ -134,6 +136,8 @@ 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);
                        addPrefixes(nonCommits, pending);
                        int cutoff = minCommitTime() - COMMIT_TIME_SLOP;
 
@@ -235,10 +239,11 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
        }
 
        /**
-        * Add a ref prefix that all results must match.
+        * Add a ref prefix to the set that results must match.
         * <p>
-        * If an object matches refs under multiple prefixes equally well, the first
-        * prefix added to this command is preferred.
+        * If an object matches multiple refs equally well, the first matching ref
+        * added with {@link #addRef(Ref)} is preferred, or else the first matching
+        * prefix added by {@link #addPrefix(String)}.
         *
         * @param prefix
         *            prefix to add; see {@link RefDatabase#getRefs(String)}
@@ -250,35 +255,83 @@ public class NameRevCommand extends GitCommand<Map<ObjectId, String>> {
                return this;
        }
 
+       /**
+        * Add all annotated tags under {@code refs/tags/} to the set that all results
+        * must match.
+        * <p>
+        * Calls {@link #addRef(Ref)}; see that method for a note on matching
+        * priority.
+        *
+        * @return {@code this}
+        * @throws JGitInternalException
+        *             a low-level exception of JGit has occurred. The original
+        *             exception can be retrieved by calling
+        *             {@link Exception#getCause()}.
+        */
+       public NameRevCommand addAnnotatedTags() {
+               checkCallable();
+               try {
+                       for (Ref ref : repo.getRefDatabase().getRefs(Constants.R_TAGS).values()) {
+                               ObjectId id = ref.getObjectId();
+                               if (id != null && (walk.parseAny(id) instanceof RevTag))
+                                       addRef(ref);
+                       }
+               } catch (IOException e) {
+                       throw new JGitInternalException(e.getMessage(), e);
+               }
+               return this;
+       }
+
+       /**
+        * Add a ref to the set that all results must match.
+        * <p>
+        * If an object matches multiple refs equally well, the first matching ref
+        * added with {@link #addRef(Ref)} is preferred, or else the first matching
+        * prefix added by {@link #addPrefix(String)}.
+        *
+        * @param ref
+        *            ref to add.
+        * @return {@code this}
+        */
+       public NameRevCommand addRef(Ref ref) {
+               checkCallable();
+               refs.add(ref);
+               return this;
+       }
+
        private void addPrefixes(Map<ObjectId, String> nonCommits,
                        FIFORevQueue pending) throws IOException {
                if (!prefixes.isEmpty()) {
                        for (String prefix : prefixes)
                                addPrefix(prefix, nonCommits, pending);
-               } else
+               } else if (refs.isEmpty())
                        addPrefix(Constants.R_REFS, nonCommits, pending);
        }
 
        private void addPrefix(String prefix, Map<ObjectId, String> nonCommits,
                        FIFORevQueue pending) throws IOException {
-               for (Ref ref : repo.getRefDatabase().getRefs(prefix).values()) {
-                       if (ref.getObjectId() == null)
-                               continue;
-                       RevObject o = walk.parseAny(ref.getObjectId());
-                       while (o instanceof RevTag) {
-                               RevTag t = (RevTag) o;
-                               nonCommits.put(o, ref.getName());
-                               o = t.getObject();
-                               walk.parseHeaders(o);
-                       }
-                       if (o instanceof NameRevCommit) {
-                               NameRevCommit c = (NameRevCommit) o;
-                               if (c.tip == null)
-                                       c.tip = ref.getName();
-                               pending.add(c);
-                       } else if (!nonCommits.containsKey(o))
-                               nonCommits.put(o, ref.getName());
+               for (Ref ref : repo.getRefDatabase().getRefs(prefix).values())
+                       addRef(ref, nonCommits, pending);
+       }
+
+       private void addRef(Ref ref, Map<ObjectId, String> nonCommits,
+                       FIFORevQueue pending) throws IOException {
+               if (ref.getObjectId() == null)
+                       return;
+               RevObject o = walk.parseAny(ref.getObjectId());
+               while (o instanceof RevTag) {
+                       RevTag t = (RevTag) o;
+                       nonCommits.put(o, ref.getName());
+                       o = t.getObject();
+                       walk.parseHeaders(o);
                }
+               if (o instanceof NameRevCommit) {
+                       NameRevCommit c = (NameRevCommit) o;
+                       if (c.tip == null)
+                               c.tip = ref.getName();
+                       pending.add(c);
+               } else if (!nonCommits.containsKey(o))
+                       nonCommits.put(o, ref.getName());
        }
 
        private int minCommitTime() throws IOException {