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<String, Ref>.

While at it, clarify the javadoc of getRefs and hasRefs to make the
same point.

Suggested-by: David Pursehouse <david.pursehouse@gmail.com>
Change-Id: I23497c66ac7b5e0c987b91efbc9e9cc29924ca66
Signed-off-by: Jonathan Nieder <jrn@google.com>
This commit is contained in:
Jonathan Nieder 2018-04-29 17:15:18 -07:00
parent 9fb724f1b9
commit 0a35e5f25b
14 changed files with 47 additions and 37 deletions

View File

@ -71,8 +71,7 @@ class RevParse extends TextBuiltin {
@Override
protected void run() throws Exception {
if (all) {
List<Ref> 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.

View File

@ -169,8 +169,7 @@ else if (revLimiter.size() > 1)
walk.setRevFilter(AndRevFilter.create(revLimiter));
if (all) {
List<Ref> refs = db.getRefDatabase().getAllRefs();
for (Ref a : refs) {
for (Ref a : db.getRefDatabase().getRefs()) {
ObjectId oid = a.getPeeledObjectId();
if (oid == null)
oid = a.getObjectId();

View File

@ -65,7 +65,7 @@ protected void run() throws Exception {
}
private Iterable<Ref> getSortedRefs() throws Exception {
List<Ref> all = db.getRefDatabase().getAllRefs();
List<Ref> all = db.getRefDatabase().getRefs();
// TODO(jrn) check if we can reintroduce fast-path by e.g. implementing
// SortedList
return RefComparator.sort(all);

View File

@ -245,7 +245,7 @@ private void detachHead() throws IOException {
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());

View File

@ -154,7 +154,7 @@ private RefTree rebuild(RefDatabase refdb) throws IOException {
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;

View File

@ -272,7 +272,7 @@ public LogCommand addRange(AnyObjectId since, AnyObjectId until)
* 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);

View File

@ -403,7 +403,7 @@ public boolean pack(ProgressMonitor pm) throws IOException {
}
private Collection<Ref> getAllRefs() throws IOException {
Collection<Ref> refs = refdb.getAllRefs();
Collection<Ref> refs = refdb.getRefs();
List<Ref> addl = refdb.getAdditionalRefs();
if (!addl.isEmpty()) {
List<Ref> all = new ArrayList<>(refs.size() + addl.size());

View File

@ -1068,7 +1068,7 @@ private Set<ObjectId> listRefLogObjects(Ref ref, long minTime) throws IOExceptio
*/
private Collection<Ref> getAllRefs() throws IOException {
RefDatabase refdb = repo.getRefDatabase();
Collection<Ref> refs = refdb.getAllRefs();
Collection<Ref> refs = refdb.getRefs();
List<Ref> addl = refdb.getAdditionalRefs();
if (!addl.isEmpty()) {
List<Ref> all = new ArrayList<>(refs.size() + addl.size());
@ -1376,7 +1376,7 @@ public RepoStatistics getStatistics() throws IOException {
}
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++;

View File

@ -336,6 +336,29 @@ public Ref firstExactRef(String... refs) throws IOException {
return null;
}
/**
* Returns all refs.
* <p>
* 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}.
* <p>
* Symbolic references to a non-existent ref (for example,
* {@code HEAD} pointing to a branch yet to be born) are not included.
* <p>
* 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<Ref> getRefs() throws IOException {
return getRefsByPrefix(ALL);
}
/**
* Get a section of the reference namespace.
*
@ -357,7 +380,7 @@ public Ref firstExactRef(String... refs) throws IOException {
/**
* Returns refs whose names start with a given prefix.
* <p>
* 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 List<Ref> getRefsByPrefix(String prefix) throws IOException {
return Collections.unmodifiableList(result);
}
/**
* Returns all refs.
* <p>
* 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<Ref> getAllRefs() throws IOException {
return getRefsByPrefix(ALL);
}
/**
* Check if any refs exist in the ref database.
* <p>
* 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 List<Ref> getAllRefs() throws IOException {
* @since 5.0
*/
public boolean hasRefs() throws IOException {
return !getAllRefs().isEmpty();
return !getRefs().isEmpty();
}
/**
@ -424,7 +437,7 @@ public boolean hasRefs() throws IOException {
* <p>
* 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
* <code>getRefs(ALL)</code> but are accepted by {@link #getRef(String)}
* <code>getRefs()</code> but are accepted by {@link #getRef(String)}
* and {@link #exactRef(String)}.
*
* @return a list of additional refs

View File

@ -437,7 +437,7 @@ private int maxTimeWanted(final Collection<Ref> wants) {
private void markReachable(final Set<ObjectId> 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();

View File

@ -254,7 +254,7 @@ private void verifyPrerequisites() throws TransportException {
List<Ref> localRefs;
try {
localRefs = transport.local.getRefDatabase().getAllRefs();
localRefs = transport.local.getRefDatabase().getRefs();
} catch (IOException e) {
throw new TransportException(transport.uri, e.getMessage(), e);
}

View File

@ -688,7 +688,7 @@ public static Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
private static Collection<RefSpec> expandPushWildcardsFor(
final Repository db, final Collection<RefSpec> specs)
throws IOException {
final List<Ref> localRefs = db.getRefDatabase().getAllRefs();
final List<Ref> localRefs = db.getRefDatabase().getRefs();
final Collection<RefSpec> procRefs = new LinkedHashSet<>();
for (final RefSpec spec : specs) {

View File

@ -1581,8 +1581,7 @@ public void checkWants(UploadPack up, List<ObjectId> wants)
new ReachableCommitTipRequestValidator().checkWants(up, wants);
else if (!wants.isEmpty()) {
Set<ObjectId> 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 static final class ReachableCommitTipRequestValidator
public void checkWants(UploadPack up, List<ObjectId> wants)
throws PackProtocolException, IOException {
checkNotAdvertisedWants(up, wants,
refIdSet(up.getRepository().getRefDatabase().getAllRefs()));
refIdSet(up.getRepository().getRefDatabase().getRefs()));
}
}

View File

@ -689,7 +689,7 @@ private Collection<WalkRemoteObjectDatabase> expandOneAlternate(
private void markLocalRefsComplete(final Set<ObjectId> have) throws TransportException {
List<Ref> refs;
try {
refs = local.getRefDatabase().getAllRefs();
refs = local.getRefDatabase().getRefs();
} catch (IOException e) {
throw new TransportException(e.getMessage(), e);
}