aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
diff options
context:
space:
mode:
authorGal Paikin <paiking@google.com>2020-11-30 15:57:06 +0100
committerGal Paikin <paiking@google.com>2021-01-27 02:22:45 -0500
commita6b90b7ec5c238692dc323e25ef927e4433edb1d (patch)
tree0ec4bcfdcf40f64137b351149777a4ad4b4a5f92 /org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
parent68b95afc706bdac78443f1b7c17c48bf57735f2d (diff)
downloadjgit-a6b90b7ec5c238692dc323e25ef927e4433edb1d.tar.gz
jgit-a6b90b7ec5c238692dc323e25ef927e4433edb1d.zip
Add getsRefsByPrefixWithSkips (excluding prefixes) to ReftableDatabase
We sometimes want to get all the refs except specific prefixes, similarly to getRefsByPrefix that gets all the refs of a specific prefix. We now create a new method that gets all refs matching a prefix except a set of specific prefixes. One use-case is for Gerrit to be able to get all the refs except refs/changes; in Gerrit we often have lots of refs/changes, but very little other refs. Currently, to get all the refs except refs/changes we need to get all the refs and then filter the refs/changes, which is very inefficient. With this method, we can simply skip the unneeded prefix so that we don't have to go over all the elements. RefDirectory still uses the inefficient implementation, since there isn't a simple way to use Refcursor to achieve the efficient implementation (as done in ReftableDatabase). Signed-off-by: Gal Paikin <paiking@google.com> Change-Id: I8c5db581acdeb6698e3d3a2abde8da32f70c854c
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java59
1 files changed, 59 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 88d17ec153..7590048a71 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
@@ -26,6 +26,7 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@@ -318,6 +319,64 @@ public class RefTest extends SampleDataRepositoryTestCase {
}
@Test
+ public void testGetRefsExcludingPrefix() throws IOException {
+ Set<String> exclude = new HashSet<>();
+ exclude.add("refs/tags");
+ // HEAD + 12 refs/heads are present here.
+ List<Ref> refs =
+ db.getRefDatabase().getRefsByPrefixWithExclusions(RefDatabase.ALL, exclude);
+ assertEquals(13, refs.size());
+ checkContainsRef(refs, db.exactRef("HEAD"));
+ checkContainsRef(refs, db.exactRef("refs/heads/a"));
+ for (Ref notInResult : db.getRefDatabase().getRefsByPrefix("refs/tags")) {
+ assertFalse(refs.contains(notInResult));
+ }
+ }
+
+ @Test
+ public void testGetRefsExcludingPrefixes() throws IOException {
+ Set<String> exclude = new HashSet<>();
+ exclude.add("refs/tags/");
+ exclude.add("refs/heads/");
+ List<Ref> refs = db.getRefDatabase().getRefsByPrefixWithExclusions(RefDatabase.ALL, exclude);
+ assertEquals(1, refs.size());
+ checkContainsRef(refs, db.exactRef("HEAD"));
+ }
+
+ @Test
+ public void testGetRefsExcludingNonExistingPrefixes() throws IOException {
+ Set<String> prefixes = new HashSet<>();
+ prefixes.add("refs/tags/");
+ prefixes.add("refs/heads/");
+ prefixes.add("refs/nonexistent/");
+ List<Ref> refs = db.getRefDatabase().getRefsByPrefixWithExclusions(RefDatabase.ALL, prefixes);
+ assertEquals(1, refs.size());
+ checkContainsRef(refs, db.exactRef("HEAD"));
+ }
+
+ @Test
+ public void testGetRefsWithPrefixExcludingPrefixes() throws IOException {
+ Set<String> exclude = new HashSet<>();
+ exclude.add("refs/heads/pa");
+ String include = "refs/heads/p";
+ List<Ref> refs = db.getRefDatabase().getRefsByPrefixWithExclusions(include, exclude);
+ assertEquals(1, refs.size());
+ checkContainsRef(refs, db.exactRef("refs/heads/prefix/a"));
+ }
+
+ @Test
+ public void testGetRefsWithPrefixExcludingOverlappingPrefixes() throws IOException {
+ Set<String> exclude = new HashSet<>();
+ exclude.add("refs/heads/pa");
+ exclude.add("refs/heads/");
+ exclude.add("refs/heads/p");
+ exclude.add("refs/tags/");
+ List<Ref> refs = db.getRefDatabase().getRefsByPrefixWithExclusions(RefDatabase.ALL, exclude);
+ assertEquals(1, refs.size());
+ checkContainsRef(refs, db.exactRef("HEAD"));
+ }
+
+ @Test
public void testResolveTipSha1() throws IOException {
ObjectId masterId = db.resolve("refs/heads/master");
Set<Ref> resolved = db.getRefDatabase().getTipsWithSha1(masterId);