diff options
author | Jonathan Nieder <jrn@google.com> | 2015-06-05 16:08:55 -0700 |
---|---|---|
committer | Jonathan Nieder <jrn@google.com> | 2015-06-05 16:08:55 -0700 |
commit | 48b67012d610f9151b425a27a4287eeedfbff0a4 (patch) | |
tree | d4b680a3f0462046975a5506597c4db0a662f2d9 /org.eclipse.jgit | |
parent | a04d1ad2f1f0f0bbb852dbddf47dd8057efa3457 (diff) | |
download | jgit-48b67012d610f9151b425a27a4287eeedfbff0a4.tar.gz jgit-48b67012d610f9151b425a27a4287eeedfbff0a4.zip |
Allow lookup of multiple exact refs in one shot
exactRef(ref1, ref2, ref3) requests multiple specific refs in a single
lookup, which may be faster in some backends than looking them up one by
one.
firstExactRef generalizes getRef by finding the first existing ref from
the list of refs named. Its main purpose is for the default
implementation of getRef (finding the first existing ref in a search
path). Hopefully it can be useful for other operations that look for
refs in a search path (e.g., git log --notes=<name>), too.
Change-Id: I5c6fcf1d3920f6968b8b97f3d4c3a267258c4b86
Signed-off-by: Jonathan Nieder <jrn@google.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java | 50 |
1 files changed, 50 insertions, 0 deletions
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 1a58c51382..fd99e4d038 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java @@ -47,6 +47,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -255,6 +256,55 @@ public abstract class RefDatabase { } /** + * Read the specified references. + * <p> + * This method expects a list of unshortened reference names and returns + * a map from reference names to refs. Any named references that do not + * exist will not be included in the returned map. + * + * @param refs + * the unabbreviated names of references to look up. + * @return modifiable map describing any refs that exist among the ref + * ref names supplied. The map can be an unsorted map. + * @throws IOException + * the reference space cannot be accessed. + * @since 4.1 + */ + public Map<String, Ref> exactRef(String... refs) throws IOException { + Map<String, Ref> result = new HashMap<>(refs.length); + for (String name : refs) { + Ref ref = exactRef(name); + if (ref != null) { + result.put(name, ref); + } + } + return result; + } + + /** + * Find the first named reference. + * <p> + * This method expects a list of unshortened reference names and returns + * the first that exists. + * + * @param refs + * the unabbreviated names of references to look up. + * @return the first named reference that exists (if any); else {@code null}. + * @throws IOException + * the reference space cannot be accessed. + * @since 4.1 + */ + public Ref firstExactRef(String... refs) throws IOException { + for (String name : refs) { + Ref ref = exactRef(name); + if (ref != null) { + return ref; + } + } + return null; + } + + /** * Get a section of the reference namespace. * * @param prefix |