Browse Source

RefDirectory: Refactor getRef and exactRef to share code

Both getRef and exactRef look for a ref or pseudoref in the $GIT_DIR
directory, with careful error handling to handle non-refs like
.git/config.

Avoid the duplication by factoring out a helper that takes care of
this.  This should make the code easier to understand and manipulate.

Change-Id: I2ea67816d2385e84e2d3394b897e23df5826ba50
Signed-off-by: Jonathan Nieder <jrn@google.com>
stable-5.3
Jonathan Nieder 9 years ago
parent
commit
32da5ac3c3
  1. 34
      org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java

34
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java

@ -319,16 +319,14 @@ public class RefDirectory extends RefDatabase {
return loose;
}
/** {@inheritDoc} */
@Override
public Ref exactRef(String name) throws IOException {
RefList<Ref> packed = getPackedRefs();
Ref ref;
@Nullable
private Ref readAndResolve(String name, RefList<Ref> packed) throws IOException {
try {
ref = readRef(name, packed);
Ref ref = readRef(name, packed);
if (ref != null) {
ref = resolve(ref, 0, null, null, packed);
}
return ref;
} catch (IOException e) {
if (name.contains("/") //$NON-NLS-1$
|| !(e.getCause() instanceof InvalidObjectIdException)) {
@ -338,8 +336,14 @@ public class RefDirectory extends RefDatabase {
// While looking for a ref outside of refs/ (e.g., 'config'), we
// found a non-ref file (e.g., a config file) instead. Treat this
// as a ref-not-found condition.
ref = null;
return null;
}
}
/** {@inheritDoc} */
@Override
public Ref exactRef(String name) throws IOException {
Ref ref = readAndResolve(name, getPackedRefs());
fireRefsChanged();
return ref;
}
@ -350,19 +354,9 @@ public class RefDirectory extends RefDatabase {
final RefList<Ref> packed = getPackedRefs();
Ref ref = null;
for (String prefix : SEARCH_PATH) {
try {
ref = readRef(prefix + needle, packed);
if (ref != null) {
ref = resolve(ref, 0, null, null, packed);
}
if (ref != null) {
break;
}
} catch (IOException e) {
if (!(!needle.contains("/") && "".equals(prefix) && e //$NON-NLS-1$ //$NON-NLS-2$
.getCause() instanceof InvalidObjectIdException)) {
throw e;
}
ref = readAndResolve(prefix + needle, packed);
if (ref != null) {
break;
}
}
fireRefsChanged();

Loading…
Cancel
Save