From 32da5ac3c3591ddcaf8d3169248b03c163dc3097 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Tue, 10 Nov 2015 16:56:41 -0800 Subject: [PATCH] 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 --- .../internal/storage/file/RefDirectory.java | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java index de7e4b3f2..36adcd6ae 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/RefDirectory.java +++ b/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 packed = getPackedRefs(); - Ref ref; + @Nullable + private Ref readAndResolve(String name, RefList 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 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();