aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org
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
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')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileReftableTest.java72
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java20
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java59
3 files changed, 151 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileReftableTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileReftableTest.java
index 33bacbe3e2..15c9109ca0 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileReftableTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/FileReftableTest.java
@@ -28,14 +28,18 @@ import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.RefRename;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.RefUpdate.Result;
@@ -579,6 +583,64 @@ public class FileReftableTest extends SampleDataRepositoryTestCase {
assertEquals(Ref.Storage.PACKED, b.getStorage());
}
+ @Test
+ public void testGetRefsExcludingPrefix() throws IOException {
+ Set<String> prefixes = new HashSet<>();
+ prefixes.add("refs/tags");
+ // HEAD + 12 refs/heads are present here.
+ List<Ref> refs =
+ db.getRefDatabase().getRefsByPrefixWithExclusions(RefDatabase.ALL, prefixes);
+ 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> exclude = new HashSet<>();
+ exclude.add("refs/tags/");
+ exclude.add("refs/heads/");
+ exclude.add("refs/nonexistent/");
+ List<Ref> refs = db.getRefDatabase().getRefsByPrefixWithExclusions(RefDatabase.ALL, exclude);
+ 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"));
+ }
+
private RefUpdate updateRef(String name) throws IOException {
final RefUpdate ref = db.updateRef(name);
ref.setNewObjectId(db.resolve(Constants.HEAD));
@@ -596,4 +658,14 @@ public class FileReftableTest extends SampleDataRepositoryTestCase {
fail("link " + src + " to " + dst);
}
}
+
+ private static void checkContainsRef(Collection<Ref> haystack, Ref needle) {
+ for (Ref ref : haystack) {
+ if (ref.getName().equals(needle.getName()) &&
+ ref.getObjectId().equals(needle.getObjectId())) {
+ return;
+ }
+ }
+ fail("list " + haystack + " does not contain ref " + needle);
+ }
}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java
index 97ef5993b9..38c545ef57 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/RefDirectoryTest.java
@@ -30,8 +30,10 @@ import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -353,6 +355,24 @@ public class RefDirectoryTest extends LocalDiskRepositoryTestCase {
}
@Test
+ public void testGetRefs_ExcludingPrefixes() throws IOException {
+ writeLooseRef("refs/heads/A", A);
+ writeLooseRef("refs/heads/B", B);
+ writeLooseRef("refs/tags/tag", A);
+ writeLooseRef("refs/something/something", B);
+ writeLooseRef("refs/aaa/aaa", A);
+
+ Set<String> toExclude = new HashSet<>();
+ toExclude.add("refs/aaa/");
+ toExclude.add("refs/heads/");
+ List<Ref> refs = refdir.getRefsByPrefixWithExclusions(RefDatabase.ALL, toExclude);
+
+ assertEquals(2, refs.size());
+ assertTrue(refs.contains(refdir.exactRef("refs/tags/tag")));
+ assertTrue(refs.contains(refdir.exactRef("refs/something/something")));
+ }
+
+ @Test
public void testFirstExactRef_IgnoresGarbageRef() throws IOException {
writeLooseRef("refs/heads/A", A);
write(new File(diskRepo.getDirectory(), "refs/heads/bad"), "FAIL\n");
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);