From 0a35e5f25bb6b865b90678f0589f97407f461f9c Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 29 Apr 2018 17:15:18 -0700 Subject: [PATCH] Rename RefDatabase#getAllRefs to getRefs This is easier to type and makes it clearer that it only returns refs and not the pseudo-refs returned by getAdditionalRefs. It also puts us in a better position to add a method to the Repository class later that delegates to this one without colliding with the existing Repository#getAllRefs method that returns a Map. While at it, clarify the javadoc of getRefs and hasRefs to make the same point. Suggested-by: David Pursehouse Change-Id: I23497c66ac7b5e0c987b91efbc9e9cc29924ca66 Signed-off-by: Jonathan Nieder --- .../src/org/eclipse/jgit/pgm/RevParse.java | 3 +- .../eclipse/jgit/pgm/RevWalkTextBuiltin.java | 3 +- .../src/org/eclipse/jgit/pgm/ShowRef.java | 2 +- .../jgit/pgm/debug/RebuildCommitGraph.java | 2 +- .../jgit/pgm/debug/RebuildRefTree.java | 2 +- .../src/org/eclipse/jgit/api/LogCommand.java | 2 +- .../storage/dfs/DfsGarbageCollector.java | 2 +- .../jgit/internal/storage/file/GC.java | 4 +- .../src/org/eclipse/jgit/lib/RefDatabase.java | 51 ++++++++++++------- .../transport/BasePackFetchConnection.java | 2 +- .../jgit/transport/BundleFetchConnection.java | 2 +- .../org/eclipse/jgit/transport/Transport.java | 2 +- .../eclipse/jgit/transport/UploadPack.java | 5 +- .../jgit/transport/WalkFetchConnection.java | 2 +- 14 files changed, 47 insertions(+), 37 deletions(-) diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevParse.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevParse.java index a52954307..21e37cd8c 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevParse.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevParse.java @@ -71,8 +71,7 @@ class RevParse extends TextBuiltin { @Override protected void run() throws Exception { if (all) { - List allRefs = db.getRefDatabase().getAllRefs(); - for (final Ref r : allRefs) { + for (Ref r : db.getRefDatabase().getRefs()) { ObjectId objectId = r.getObjectId(); // getRefs skips dangling symrefs, so objectId should never be // null. diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java index 423887cc1..80161b482 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java @@ -169,8 +169,7 @@ abstract class RevWalkTextBuiltin extends TextBuiltin { walk.setRevFilter(AndRevFilter.create(revLimiter)); if (all) { - List refs = db.getRefDatabase().getAllRefs(); - for (Ref a : refs) { + for (Ref a : db.getRefDatabase().getRefs()) { ObjectId oid = a.getPeeledObjectId(); if (oid == null) oid = a.getObjectId(); diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/ShowRef.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/ShowRef.java index f6384a2ce..9ce8347d9 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/ShowRef.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/ShowRef.java @@ -65,7 +65,7 @@ class ShowRef extends TextBuiltin { } private Iterable getSortedRefs() throws Exception { - List all = db.getRefDatabase().getAllRefs(); + List all = db.getRefDatabase().getRefs(); // TODO(jrn) check if we can reintroduce fast-path by e.g. implementing // SortedList return RefComparator.sort(all); diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildCommitGraph.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildCommitGraph.java index 19d94eade..42dea219f 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildCommitGraph.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildCommitGraph.java @@ -245,7 +245,7 @@ class RebuildCommitGraph extends TextBuiltin { private void deleteAllRefs() throws Exception { final RevWalk rw = new RevWalk(db); - for (Ref r : db.getRefDatabase().getAllRefs()) { + for (Ref r : db.getRefDatabase().getRefs()) { if (Constants.HEAD.equals(r.getName())) continue; final RefUpdate u = db.updateRef(r.getName()); diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildRefTree.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildRefTree.java index 6665ce7c1..86092b27a 100644 --- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildRefTree.java +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/RebuildRefTree.java @@ -154,7 +154,7 @@ class RebuildRefTree extends TextBuiltin { head)); } - for (Ref r : refdb.getAllRefs()) { + for (Ref r : refdb.getRefs()) { if (r.getName().equals(txnCommitted) || r.getName().equals(HEAD) || r.getName().startsWith(txnNamespace)) { continue; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/LogCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/LogCommand.java index 73433c18b..cdf8cdd8d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/LogCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/LogCommand.java @@ -272,7 +272,7 @@ public class LogCommand extends GitCommand> { * the references could not be accessed */ public LogCommand all() throws IOException { - for (Ref ref : getRepository().getRefDatabase().getAllRefs()) { + for (Ref ref : getRepository().getRefDatabase().getRefs()) { if(!ref.isPeeled()) ref = getRepository().peel(ref); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java index 30bcbf068..ca11fb926 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java @@ -403,7 +403,7 @@ public class DfsGarbageCollector { } private Collection getAllRefs() throws IOException { - Collection refs = refdb.getAllRefs(); + Collection refs = refdb.getRefs(); List addl = refdb.getAdditionalRefs(); if (!addl.isEmpty()) { List all = new ArrayList<>(refs.size() + addl.size()); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java index 1ea686e7c..137a75731 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java @@ -1068,7 +1068,7 @@ public class GC { */ private Collection getAllRefs() throws IOException { RefDatabase refdb = repo.getRefDatabase(); - Collection refs = refdb.getAllRefs(); + Collection refs = refdb.getRefs(); List addl = refdb.getAdditionalRefs(); if (!addl.isEmpty()) { List all = new ArrayList<>(refs.size() + addl.size()); @@ -1376,7 +1376,7 @@ public class GC { } RefDatabase refDb = repo.getRefDatabase(); - for (Ref r : refDb.getAllRefs()) { + for (Ref r : refDb.getRefs()) { Storage storage = r.getStorage(); if (storage == Storage.LOOSE || storage == Storage.LOOSE_PACKED) ret.numberOfLooseRefs++; 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 0ae14d130..3170787dd 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java @@ -336,6 +336,29 @@ public abstract class RefDatabase { return null; } + /** + * Returns all refs. + *

+ * This includes {@code HEAD}, branches under {@code ref/heads/}, tags + * under {@code refs/tags/}, etc. It does not include pseudo-refs like + * {@code FETCH_HEAD}; for those, see {@link #getAdditionalRefs}. + *

+ * Symbolic references to a non-existent ref (for example, + * {@code HEAD} pointing to a branch yet to be born) are not included. + *

+ * Callers interested in only a portion of the ref hierarchy can call + * {@link #getRefsByPrefix} instead. + * + * @return immutable list of all refs. + * @throws java.io.IOException + * the reference space cannot be accessed. + * @since 5.0 + */ + @NonNull + public List getRefs() throws IOException { + return getRefsByPrefix(ALL); + } + /** * Get a section of the reference namespace. * @@ -357,7 +380,7 @@ public abstract class RefDatabase { /** * Returns refs whose names start with a given prefix. *

- * The default implementation uses {@link #getRefs}. Implementors of + * The default implementation uses {@link #getRefs(String)}. Implementors of * {@link RefDatabase} should override this method directly if a better * implementation is possible. * @@ -391,24 +414,14 @@ public abstract class RefDatabase { return Collections.unmodifiableList(result); } - /** - * Returns all refs. - *

- * Callers interested in only a portion of the ref hierarchy can call - * {@link #getRefsByPrefix} instead. - * - * @return immutable list of all refs. - * @throws java.io.IOException - * the reference space cannot be accessed. - * @since 5.0 - */ - @NonNull - public List getAllRefs() throws IOException { - return getRefsByPrefix(ALL); - } - /** * Check if any refs exist in the ref database. + *

+ * This uses the same definition of refs as {@link #getRefs()}. In + * particular, returns {@code false} in a new repository with no refs + * under {@code refs/} and {@code HEAD} pointing to a branch yet to be + * born, and returns {@code true} in a repository with no refs under + * {@code refs/} and a detached {@code HEAD} pointing to history. * * @return true if the database has refs. * @throws java.io.IOException @@ -416,7 +429,7 @@ public abstract class RefDatabase { * @since 5.0 */ public boolean hasRefs() throws IOException { - return !getAllRefs().isEmpty(); + return !getRefs().isEmpty(); } /** @@ -424,7 +437,7 @@ public abstract class RefDatabase { *

* The result list includes non-ref items such as MERGE_HEAD and * FETCH_RESULT cast to be refs. The names of these refs are not returned by - * getRefs(ALL) but are accepted by {@link #getRef(String)} + * getRefs() but are accepted by {@link #getRef(String)} * and {@link #exactRef(String)}. * * @return a list of additional refs diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackFetchConnection.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackFetchConnection.java index a88918e7a..96329d1f1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackFetchConnection.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/BasePackFetchConnection.java @@ -437,7 +437,7 @@ public abstract class BasePackFetchConnection extends BasePackConnection private void markReachable(final Set have, final int maxTime) throws IOException { - for (Ref r : local.getRefDatabase().getAllRefs()) { + for (Ref r : local.getRefDatabase().getRefs()) { ObjectId id = r.getPeeledObjectId(); if (id == null) id = r.getObjectId(); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/BundleFetchConnection.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/BundleFetchConnection.java index 24f44cb46..7cf10e95b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/BundleFetchConnection.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/BundleFetchConnection.java @@ -254,7 +254,7 @@ class BundleFetchConnection extends BaseFetchConnection { List localRefs; try { - localRefs = transport.local.getRefDatabase().getAllRefs(); + localRefs = transport.local.getRefDatabase().getRefs(); } catch (IOException e) { throw new TransportException(transport.uri, e.getMessage(), e); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/Transport.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/Transport.java index 9ff5f7b91..77a4d92ab 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/Transport.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/Transport.java @@ -688,7 +688,7 @@ public abstract class Transport implements AutoCloseable { private static Collection expandPushWildcardsFor( final Repository db, final Collection specs) throws IOException { - final List localRefs = db.getRefDatabase().getAllRefs(); + final List localRefs = db.getRefDatabase().getRefs(); final Collection procRefs = new LinkedHashSet<>(); for (final RefSpec spec : specs) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java index 2a1b2492e..b1832fd02 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -1581,8 +1581,7 @@ public class UploadPack { new ReachableCommitTipRequestValidator().checkWants(up, wants); else if (!wants.isEmpty()) { Set refIds = - refIdSet(up.getRepository().getRefDatabase() - .getAllRefs()); + refIdSet(up.getRepository().getRefDatabase().getRefs()); for (ObjectId obj : wants) { if (!refIds.contains(obj)) throw new WantNotValidException(obj); @@ -1602,7 +1601,7 @@ public class UploadPack { public void checkWants(UploadPack up, List wants) throws PackProtocolException, IOException { checkNotAdvertisedWants(up, wants, - refIdSet(up.getRepository().getRefDatabase().getAllRefs())); + refIdSet(up.getRepository().getRefDatabase().getRefs())); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java index c0ee28a2c..b8f8d97be 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/WalkFetchConnection.java @@ -689,7 +689,7 @@ class WalkFetchConnection extends BaseFetchConnection { private void markLocalRefsComplete(final Set have) throws TransportException { List refs; try { - refs = local.getRefDatabase().getAllRefs(); + refs = local.getRefDatabase().getRefs(); } catch (IOException e) { throw new TransportException(e.getMessage(), e); }