]> source.dussan.org Git - jgit.git/commitdiff
Query references by multiple prefixes 67/129567/8
authorMinh Thai <mthai@google.com>
Mon, 17 Sep 2018 21:48:06 +0000 (14:48 -0700)
committerMinh Thai <mthai@google.com>
Mon, 24 Sep 2018 22:17:48 +0000 (15:17 -0700)
Support multiple prefixes when querying references to allow
implementor to minimize number of RPC calls.

Change-Id: I5f822fd7eaf9756b44750080d3056de138b64f4a
Signed-off-by: Minh Thai <mthai@google.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java

index a42027b584aa39a3f1e097d4c62038627a5e0886..ac142ed3227b27c1ce3d3caf63b6e3fd81752280 100644 (file)
@@ -333,4 +333,17 @@ public class RefTest extends SampleDataRepositoryTestCase {
                assertEquals(1, refs.size());
                checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
        }
+
+       @Test
+       public void testGetRefsByPrefixes() throws IOException {
+               List<Ref> refs = db.getRefDatabase().getRefsByPrefix();
+               assertEquals(0, refs.size());
+
+               refs = db.getRefDatabase().getRefsByPrefix("refs/heads/p",
+                               "refs/tags/A");
+               assertEquals(3, refs.size());
+               checkContainsRef(refs, db.exactRef("refs/heads/pa"));
+               checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
+               checkContainsRef(refs, db.exactRef("refs/tags/A"));
+       }
 }
index 3170787dd949273bfa286fded7acb435760b2f60..ac26b513644c63da730b5aff40c22f67a0f34e1f 100644 (file)
@@ -414,6 +414,31 @@ public abstract class RefDatabase {
                return Collections.unmodifiableList(result);
        }
 
+       /**
+        * Returns refs whose names start with one of the given prefixes.
+        * <p>
+        * The default implementation uses {@link #getRefsByPrefix(String)}.
+        * Implementors of {@link RefDatabase} should override this method directly
+        * if a better implementation is possible.
+        *
+        * @param prefixes
+        *            strings that names of refs should start with.
+        * @return immutable list of refs whose names start with one of
+        *         {@code prefixes}.  Refs can be unsorted and may contain
+        *         duplicates if the prefixes overlap.
+        * @throws java.io.IOException
+        *             the reference space cannot be accessed.
+        * @since 5.1
+        */
+       @NonNull
+       public List<Ref> getRefsByPrefix(String... prefixes) throws IOException {
+               List<Ref> result = new ArrayList<>();
+               for (String prefix : prefixes) {
+                       result.addAll(getRefsByPrefix(prefix));
+               }
+               return Collections.unmodifiableList(result);
+       }
+
        /**
         * Check if any refs exist in the ref database.
         * <p>