diff options
author | Jonathan Tan <jonathantanmy@google.com> | 2018-04-06 15:50:01 -0700 |
---|---|---|
committer | Jonathan Nieder <jrn@google.com> | 2018-04-20 11:10:48 -0700 |
commit | 4faec31c0a9ff92efdac10d1bd5db1a929ce74a2 (patch) | |
tree | c78b793028240be22185ec7f4fed33053b9159ea /org.eclipse.jgit.test/tst/org/eclipse | |
parent | 42e69409d796421bb1353279cfae463b19a43fe9 (diff) | |
download | jgit-4faec31c0a9ff92efdac10d1bd5db1a929ce74a2.tar.gz jgit-4faec31c0a9ff92efdac10d1bd5db1a929ce74a2.zip |
Add RefDatabase#getRefsByPrefix method
The existing RefDatabase#getRefs abstract method (to be implemented by
ref database backends) has the following issues:
- It returns a map with a key (the name of the ref with the prefix
removed) which is potentially superfluous (it can be derived by the
caller if need be) and confusing (in that the prefix is removed).
- The prefix is required to end with a '/', but some backends (e.g.
reftable) have fast search by prefix regardless of what the last
character of the prefix is.
Add a new method #getRefsByPrefix that does not have these issues. This
is non-abstract with a default implementation that uses #getRefs (for
backwards compatibility), but ref database backends can reimplement it.
This also prepares for supporting "ref-prefix" in the "ls-refs" command
in the fetch-pack/upload-pack protocol v2, which does not require that
the prefix end with a '/'.
Change-Id: I4c92f852e8c1558095dd460b5fd7b602c1d82df1
Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
Signed-off-by: Jonathan Nieder <jrn@google.com>
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java | 23 |
1 files changed, 23 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 e93867829a..2481e64997 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 @@ -57,6 +57,7 @@ import static org.junit.Assert.fail; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.List; import java.util.Map; import java.util.TreeSet; @@ -305,4 +306,26 @@ public class RefTest extends SampleDataRepositoryTestCase { assertSame(dst.getPeeledObjectId(), ref.getPeeledObjectId()); assertEquals(dst.isPeeled(), ref.isPeeled()); } + + private static void checkContainsRef(List<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); + } + + @Test + public void testGetRefsByPrefix() throws IOException { + List<Ref> refs = db.getRefDatabase().getRefsByPrefix("refs/heads/g"); + assertEquals(2, refs.size()); + checkContainsRef(refs, db.exactRef("refs/heads/g")); + checkContainsRef(refs, db.exactRef("refs/heads/gitlink")); + + refs = db.getRefDatabase().getRefsByPrefix("refs/heads/prefix/"); + assertEquals(1, refs.size()); + checkContainsRef(refs, db.exactRef("refs/heads/prefix/a")); + } } |