RefDatabase: Introduce findRef synonym for getRef
Using findRef instead of getRef makes it clearer that the caller wants to search for the ref in the search path, instead of looking for a ref that exactly matches the input. This change introduces the new findRef method and deprecates getRef. It updates Repository#findRef to use the new method, ensuring some test coverage. Other callers will be updated in followup changes. A nice side effect of introducing the new findRef method is that it is final and based on firstExactRef, so implementers can focus on implementing the latter efficiently and do not have to carefully write custom path search code respecting SEARCH_PATH. Change-Id: Id3bb944344a9743705fd1f20193ab679298fa51c Signed-off-by: Jonathan Nieder <jrn@google.com>
This commit is contained in:
parent
d2bab65470
commit
c1954f6c36
|
@ -99,11 +99,6 @@ public Ref exactRef(String name) throws IOException {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Ref getRef(String name) throws IOException {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Ref> getAdditionalRefs() throws IOException {
|
public List<Ref> getAdditionalRefs() throws IOException {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -105,20 +105,6 @@ public Ref exactRef(String name) throws IOException {
|
||||||
return ref != null ? resolve(ref, 0, curr.ids) : null;
|
return ref != null ? resolve(ref, 0, curr.ids) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public Ref getRef(String needle) throws IOException {
|
|
||||||
RefCache curr = read();
|
|
||||||
for (String prefix : SEARCH_PATH) {
|
|
||||||
Ref ref = curr.ids.get(prefix + needle);
|
|
||||||
if (ref != null) {
|
|
||||||
ref = resolve(ref, 0, curr.ids);
|
|
||||||
return ref;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public List<Ref> getAdditionalRefs() {
|
public List<Ref> getAdditionalRefs() {
|
||||||
|
|
|
@ -227,18 +227,6 @@ public Ref exactRef(String name) throws IOException {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public Ref getRef(String needle) throws IOException {
|
|
||||||
for (String prefix : SEARCH_PATH) {
|
|
||||||
Ref ref = exactRef(prefix + needle);
|
|
||||||
if (ref != null) {
|
|
||||||
return ref;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Ref> getRefs(String prefix) throws IOException {
|
public Map<String, Ref> getRefs(String prefix) throws IOException {
|
||||||
|
|
|
@ -388,23 +388,6 @@ public Ref firstExactRef(String... refs) throws IOException {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public Ref getRef(String needle) throws IOException {
|
|
||||||
try {
|
|
||||||
RefList<Ref> packed = getPackedRefs();
|
|
||||||
for (String prefix : SEARCH_PATH) {
|
|
||||||
Ref ref = readAndResolve(prefix + needle, packed);
|
|
||||||
if (ref != null) {
|
|
||||||
return ref;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
fireRefsChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Ref> getRefs(String prefix) throws IOException {
|
public Map<String, Ref> getRefs(String prefix) throws IOException {
|
||||||
|
|
|
@ -204,16 +204,6 @@ public void close() {
|
||||||
bootstrap.close();
|
bootstrap.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
|
||||||
@Override
|
|
||||||
public Ref getRef(String name) throws IOException {
|
|
||||||
String[] needle = new String[SEARCH_PATH.length];
|
|
||||||
for (int i = 0; i < SEARCH_PATH.length; i++) {
|
|
||||||
needle[i] = SEARCH_PATH[i] + name;
|
|
||||||
}
|
|
||||||
return firstExactRef(needle);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public Ref exactRef(String name) throws IOException {
|
public Ref exactRef(String name) throws IOException {
|
||||||
|
|
|
@ -69,10 +69,10 @@ public abstract class RefDatabase {
|
||||||
/**
|
/**
|
||||||
* Order of prefixes to search when using non-absolute references.
|
* Order of prefixes to search when using non-absolute references.
|
||||||
* <p>
|
* <p>
|
||||||
* The implementation's {@link #getRef(String)} method must take this search
|
* {@link #findRef(String)} takes this search space into consideration
|
||||||
* space into consideration when locating a reference by name. The first
|
* when locating a reference by name. The first entry in the path is
|
||||||
* entry in the path is always {@code ""}, ensuring that absolute references
|
* always {@code ""}, ensuring that absolute references are resolved
|
||||||
* are resolved without further mangling.
|
* without further mangling.
|
||||||
*/
|
*/
|
||||||
protected static final String[] SEARCH_PATH = { "", //$NON-NLS-1$
|
protected static final String[] SEARCH_PATH = { "", //$NON-NLS-1$
|
||||||
Constants.R_REFS, //
|
Constants.R_REFS, //
|
||||||
|
@ -256,6 +256,23 @@ public boolean performsAtomicTransactions() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compatibility synonym for {@link #findRef(String)}.
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* the name of the reference. May be a short name which must be
|
||||||
|
* searched for using the standard {@link #SEARCH_PATH}.
|
||||||
|
* @return the reference (if it exists); else {@code null}.
|
||||||
|
* @throws IOException
|
||||||
|
* the reference space cannot be accessed.
|
||||||
|
* @deprecated Use {@link #findRef(String)} instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@Nullable
|
||||||
|
public final Ref getRef(String name) throws IOException {
|
||||||
|
return findRef(name);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a single reference.
|
* Read a single reference.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -272,14 +289,21 @@ public boolean performsAtomicTransactions() {
|
||||||
* @return the reference (if it exists); else {@code null}.
|
* @return the reference (if it exists); else {@code null}.
|
||||||
* @throws java.io.IOException
|
* @throws java.io.IOException
|
||||||
* the reference space cannot be accessed.
|
* the reference space cannot be accessed.
|
||||||
|
* @since 5.3
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public abstract Ref getRef(String name) throws IOException;
|
public final Ref findRef(String name) throws IOException {
|
||||||
|
String[] names = new String[SEARCH_PATH.length];
|
||||||
|
for (int i = 0; i < SEARCH_PATH.length; i++) {
|
||||||
|
names[i] = SEARCH_PATH[i] + name;
|
||||||
|
}
|
||||||
|
return firstExactRef(names);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a single reference.
|
* Read a single reference.
|
||||||
* <p>
|
* <p>
|
||||||
* Unlike {@link #getRef}, this method expects an unshortened reference
|
* Unlike {@link #findRef}, this method expects an unshortened reference
|
||||||
* name and does not search using the standard {@link #SEARCH_PATH}.
|
* name and does not search using the standard {@link #SEARCH_PATH}.
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
|
@ -469,7 +493,7 @@ public boolean hasRefs() throws IOException {
|
||||||
* <p>
|
* <p>
|
||||||
* The result list includes non-ref items such as MERGE_HEAD and
|
* 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
|
* FETCH_RESULT cast to be refs. The names of these refs are not returned by
|
||||||
* <code>getRefs()</code> but are accepted by {@link #getRef(String)}
|
* <code>getRefs()</code> but are accepted by {@link #findRef(String)}
|
||||||
* and {@link #exactRef(String)}.
|
* and {@link #exactRef(String)}.
|
||||||
*
|
*
|
||||||
* @return a list of additional refs
|
* @return a list of additional refs
|
||||||
|
|
|
@ -849,7 +849,7 @@ private ObjectId resolveSimple(String revstr) throws IOException {
|
||||||
return ObjectId.fromString(revstr);
|
return ObjectId.fromString(revstr);
|
||||||
|
|
||||||
if (Repository.isValidRefName("x/" + revstr)) { //$NON-NLS-1$
|
if (Repository.isValidRefName("x/" + revstr)) { //$NON-NLS-1$
|
||||||
Ref r = getRefDatabase().getRef(revstr);
|
Ref r = getRefDatabase().findRef(revstr);
|
||||||
if (r != null)
|
if (r != null)
|
||||||
return r.getObjectId();
|
return r.getObjectId();
|
||||||
}
|
}
|
||||||
|
@ -1087,7 +1087,7 @@ public final Ref exactRef(String name) throws IOException {
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public final Ref findRef(String name) throws IOException {
|
public final Ref findRef(String name) throws IOException {
|
||||||
return getRefDatabase().getRef(name);
|
return getRefDatabase().findRef(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue