reftable: resolve symbolic references

resolve(Ref) helps callers recursively chase symbolic references and
is a useful function when wrapping a Reftable inside a RefDatabase, as
RefCursor does not resolve symbolic references during iteration.

Change-Id: I1ba143f403773497972e225dc92c35ecb989e154
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java
index 9650fa6..61ed7d5 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/reftable/ReftableTest.java
@@ -280,6 +280,38 @@
 	}
 
 	@Test
+	public void resolveSymbolicRef() throws IOException {
+		Reftable t = read(write(
+				sym(HEAD, "refs/heads/tmp"),
+				sym("refs/heads/tmp", MASTER),
+				ref(MASTER, 1)));
+
+		Ref head = t.exactRef(HEAD);
+		assertNull(head.getObjectId());
+		assertEquals("refs/heads/tmp", head.getTarget().getName());
+
+		head = t.resolve(head);
+		assertNotNull(head);
+		assertEquals(id(1), head.getObjectId());
+	}
+
+	@Test
+	public void failChainOfSymbolicRef() throws IOException {
+		Reftable t = read(write(
+				sym(HEAD, "refs/heads/1"),
+				sym("refs/heads/1", "refs/heads/2"),
+				sym("refs/heads/2", "refs/heads/3"),
+				sym("refs/heads/3", "refs/heads/4"),
+				sym("refs/heads/4", "refs/heads/5"),
+				sym("refs/heads/5", MASTER),
+				ref(MASTER, 1)));
+
+		Ref head = t.exactRef(HEAD);
+		assertNull(head.getObjectId());
+		assertNull(t.resolve(head));
+	}
+
+	@Test
 	public void oneDeletedRef() throws IOException {
 		String name = "refs/heads/gone";
 		Ref exp = newRef(name);
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java
index c5d41d1..f6f7fd4 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/Reftable.java
@@ -43,12 +43,14 @@
 
 package org.eclipse.jgit.internal.storage.reftable;
 
+import static org.eclipse.jgit.lib.RefDatabase.MAX_SYMBOLIC_REF_DEPTH;
 import java.io.IOException;
 import java.util.Collection;
 
 import org.eclipse.jgit.annotations.Nullable;
 import org.eclipse.jgit.lib.AnyObjectId;
 import org.eclipse.jgit.lib.Ref;
+import org.eclipse.jgit.lib.SymbolicRef;
 
 /** Abstract table of references. */
 public abstract class Reftable implements AutoCloseable {
@@ -201,6 +203,42 @@
 		}
 	}
 
+	/**
+	 * Resolve a symbolic reference to populate its value.
+	 *
+	 * @param symref
+	 *            reference to resolve.
+	 * @return resolved {@code symref}, or {@code null}.
+	 * @throws IOException
+	 *             references cannot be read.
+	 */
+	@Nullable
+	public Ref resolve(Ref symref) throws IOException {
+		return resolve(symref, 0);
+	}
+
+	private Ref resolve(Ref ref, int depth) throws IOException {
+		if (!ref.isSymbolic()) {
+			return ref;
+		}
+
+		Ref dst = ref.getTarget();
+		if (MAX_SYMBOLIC_REF_DEPTH <= depth) {
+			return null; // claim it doesn't exist
+		}
+
+		dst = exactRef(dst.getName());
+		if (dst == null) {
+			return ref;
+		}
+
+		dst = resolve(dst, depth + 1);
+		if (dst == null) {
+			return null; // claim it doesn't exist
+		}
+		return new SymbolicRef(ref.getName(), dst);
+	}
+
 	@Override
 	public abstract void close() throws IOException;
 }