aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMinh Thai <mthai@google.com>2018-09-17 14:48:06 -0700
committerMinh Thai <mthai@google.com>2018-09-24 15:17:48 -0700
commita51e686e47f4347d7a14ca734f94efa9f16018c7 (patch)
tree826f70715e0f1015bf3535ce01eeb884a515940a
parent751abf4a5047903c44f433ff9d59136898b9cfdb (diff)
downloadjgit-a51e686e47f4347d7a14ca734f94efa9f16018c7.tar.gz
jgit-a51e686e47f4347d7a14ca734f94efa9f16018c7.zip
Query references by multiple prefixes
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>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java13
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java25
2 files changed, 38 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
index a42027b584..ac142ed322 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
@@ -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"));
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java
index 3170787dd9..ac26b51364 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java
@@ -415,6 +415,31 @@ public abstract class RefDatabase {
}
/**
+ * 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>
* This uses the same definition of refs as {@link #getRefs()}. In