Add support for special symref FETCH_HEAD and MERGE_HEAD

The RefDirectory class was not returning FETCH_HEAD and
MERGE_HEAD when trying to get all refs via getRefs(RefDatabase.ALL).
This fix adds constants for FETCH_HEAD and ORIG_HEAD and adds a
new getter getAdditionalRefs() to get these additional refs.
To be compatible with c git the getRefs(ALL) method will not return
FETCH_HEAD, MERGE_HEAD and ORIG_HEAD.

Change-Id: Ie114ca92e9d5e7d61d892f4413ade65acdc08c32
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
This commit is contained in:
Christian Halstrick 2010-10-20 17:39:19 +02:00
parent 8067197049
commit a4f7992dfb
3 changed files with 40 additions and 0 deletions

View File

@ -79,6 +79,9 @@ public final class Constants {
/** Special name for the "HEAD" symbolic-ref. */
public static final String HEAD = "HEAD";
/** Special name for the "FETCH_HEAD" symbolic-ref. */
public static final String FETCH_HEAD = "FETCH_HEAD";
/**
* Text string that identifies an object as a commit.
* <p>
@ -527,6 +530,12 @@ public static byte[] encode(final String str) {
/** name of the file containing the IDs of the parents of a merge commit */
public static final String MERGE_HEAD = "MERGE_HEAD";
/**
* name of the ref ORIG_HEAD used by certain commands to store the original
* value of HEAD
*/
public static final String ORIG_HEAD = "ORIG_HEAD";
/** objectid for the empty blob */
public static final ObjectId EMPTY_BLOB_ID = ObjectId
.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");

View File

@ -44,6 +44,7 @@
package org.eclipse.jgit.lib;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
@ -176,6 +177,19 @@ public abstract RefRename newRename(String fromName, String toName)
*/
public abstract Map<String, Ref> getRefs(String prefix) throws IOException;
/**
* Get the additional reference-like entities from the repository.
* <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)}
*
* @return a list of additional refs
* @throws IOException
* the reference space cannot be accessed.
*/
public abstract List<Ref> getAdditionalRefs() throws IOException;
/**
* Peel a possibly unpeeled reference by traversing the annotated tags.
* <p>

View File

@ -69,6 +69,8 @@
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@ -123,6 +125,10 @@ public class RefDirectory extends RefDatabase {
/** If in the header, denotes the file has peeled data. */
public static final String PACKED_REFS_PEELED = " peeled"; //$NON-NLS-1$
/** The names of the additional refs supported by this class */
private static final String[] additionalRefsNames = new String[] {
Constants.MERGE_HEAD, Constants.FETCH_HEAD, Constants.ORIG_HEAD };
private final FileRepository parent;
private final File gitDir;
@ -297,6 +303,17 @@ public Map<String, Ref> getRefs(String prefix) throws IOException {
return new RefMap(prefix, packed, upcast(loose), symbolic.toRefList());
}
@Override
public List<Ref> getAdditionalRefs() throws IOException {
List<Ref> ret = new LinkedList<Ref>();
for (String name : additionalRefsNames) {
Ref r = getRef(name);
if (r != null)
ret.add(r);
}
return ret;
}
@SuppressWarnings("unchecked")
private RefList<Ref> upcast(RefList<? extends Ref> loose) {
return (RefList<Ref>) loose;