Top Description Fields Constructors Methods
org.eclipse.jgit.lib

public abstract Class RefDatabase

extends Object
Class Inheritance
Known Direct Subclasses
org.eclipse.jgit.internal.storage.dfs.DfsRefDatabase, org.eclipse.jgit.internal.storage.file.FileReftableDatabase, org.eclipse.jgit.internal.storage.file.RefDirectory
Static Imports
java.util.stream.Collectors.toList, .Collectors.toSet

Abstraction of name to org.eclipse.jgit.lib.ObjectId mapping.

A reference database stores a mapping of reference names to org.eclipse.jgit.lib.ObjectId. Every org.eclipse.jgit.lib.Repository has a single reference database, mapping names to the tips of the object graph contained by the org.eclipse.jgit.lib.ObjectDatabase.

Field Summary

Modifier and TypeField and Description
protected static final String[]
additionalRefsNames

The names of additional refs

public static final String
ALL

Magic value for getRefsByPrefix(String) to return all references.

public static final int
MAX_SYMBOLIC_REF_DEPTH

Maximum number of times a SymbolicRef can be traversed.

protected static final String[]
SEARCH_PATH

Order of prefixes to search when using non-absolute references.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public abstract void
close()

Close any resources held by this database.

public abstract void
create()

Initialize a new reference database at this location.

public abstract Ref

Returns:

the reference (if it exists); else null.
exactRef
(String
the unabbreviated name of the reference.
name
)

Read a single reference.

public Map<String, Ref>

Returns:

modifiable map describing any refs that exist among the ref ref names supplied. The map can be an unsorted map.
exactRef
(String...
the unabbreviated names of references to look up.
refs
)

Read the specified references.

public final Ref

Returns:

the reference (if it exists); else null.
findRef
(String
the name of the reference. May be a short name which must be searched for using the standard SEARCH_PATH.
name
)

Read a single reference.

public static Ref

Returns:

The first ref matching the name, or null if not found.
findRef
(Map<String, Ref>
map of refs to search within. Names should be fully qualified, e.g. "refs/heads/master".
map
,
String
short name of ref to find, e.g. "master" to find "refs/heads/master" in map.
name
)

Try to find the specified name in the ref map using SEARCH_PATH.

public Ref

Returns:

the first named reference that exists (if any); else null.
firstExactRef
(String...
the unabbreviated names of references to look up.
refs
)

Find the first named reference.

public abstract List<Ref>

Returns:

a list of additional refs
getAdditionalRefs
()

Get the additional reference-like entities from the repository.

public Collection<String>

Returns:

a collection of full names of existing refs which would conflict with the passed ref name; empty collection when there are no conflicts
getConflictingNames
(String
proposed name to check for conflicts against
name
)

Determine if a proposed reference cannot coexist with existing ones.

public ReflogReader

Returns:

a org.eclipse.jgit.lib.ReflogReader for the supplied refname, or null if the named ref does not exist.
getReflogReader
(String refName)

Get the reflog reader

public abstract ReflogReader

Returns:

a org.eclipse.jgit.lib.ReflogReader for the supplied ref.
getReflogReader
(Ref
a Ref
ref
)

Get the reflog reader.

public List<Ref>

Returns:

immutable list of all refs.
getRefs
()

Returns all refs.

public abstract Map<String, Ref>

Returns:

modifiable map that is a complete snapshot of the current reference namespace, with prefix removed from the start of each key. The map can be an unsorted map.
getRefs
(String
prefix to search the namespace with; must end with /. If the empty string (ALL), obtain a complete snapshot of all references.
prefix
)
Deprecated use getRefsByPrefix instead

Get a section of the reference namespace.

public List<Ref>

Returns:

immutable list of refs whose names start with prefix.
getRefsByPrefix
(String
string that names of refs should start with; may be empty (to return all refs).
prefix
)

Returns refs whose names start with a given prefix.

public List<Ref>

Returns:

immutable list of refs whose names start with one of prefixes. Refs can be unsorted and may contain duplicates if the prefixes overlap.
getRefsByPrefix
(String...
strings that names of refs should start with.
prefixes
)

Returns refs whose names start with one of the given prefixes.

public List<Ref>

Returns:

immutable list of refs whose names start with prefix and none of the strings in excludes.
getRefsByPrefixWithExclusions
(String
string that names of refs should start with; may be empty.
include
,
Set<String>
strings that names of refs can't start with; may be empty.
excludes
)

Returns refs whose names start with a given prefix excluding all refs that start with one of the given prefixes.

public Set<Ref>

Returns:

a Set of Refs whose tips point to the provided id.
getTipsWithSha1
(ObjectId
ObjectId to resolve
id
)

Returns all refs that resolve directly to the given ObjectId.

public boolean

Returns:

whether this RefDatabase supports fast inverse ref queries.
hasFastTipsWithSha1
()

If the ref database does not support fast inverse queries, it may be advantageous to build a complete SHA1 to ref map in advance for multiple uses.

public boolean

Returns:

true if the database has refs.
hasRefs
()

Check if any refs exist in the ref database.

public boolean

Returns:

true if the implementation assigns update indices to references.
hasVersioning
()

With versioning, each reference has a version number that increases on update.

public abstract boolean

Returns:

true if the name overlaps with an existing reference; false if using this name right now would be safe.
isNameConflicting
(String
proposed name.
name
)

Determine if a proposed reference name overlaps with an existing one.

public BatchRefUpdate

Returns:

a new batch update object.
newBatchUpdate
()

Create a new batch update to attempt on this database.

public abstract RefRename

Returns:

an update command that knows how to rename a branch to another.
newRename
(String
name of reference to rename from
fromName
,
String
name of reference to rename to
toName
)

Create a new update command to rename a reference.

public abstract RefUpdate

Returns:

a new update for the requested name; never null.
newUpdate
(String
the name of the reference.
name
,
boolean
if true and name is currently a org.eclipse.jgit.lib.SymbolicRef, the update will replace it with an org.eclipse.jgit.lib.ObjectIdRef. Otherwise, the update will recursively traverse org.eclipse.jgit.lib.SymbolicRefs and operate on the leaf org.eclipse.jgit.lib.ObjectIdRef.
detach
)

Create a new update command to create, modify or delete a reference.

public void
packRefs(ProgressMonitor
a progress monitor
pm
,
PackRefsCommand
PackRefsCommand to control ref packing behavior
packRefs
)

Optimize pack ref storage.

public abstract Ref

Returns:

ref if ref.isPeeled() is true; otherwise a new Ref object representing the same data as Ref, but isPeeled() will be true and getPeeledObjectId() will contain the peeled object (or null).
peel
(Ref
The reference to peel
ref
)

Peel a possibly unpeeled reference by traversing the annotated tags.

public boolean

Returns:

whether transactions are atomic by default.
performsAtomicTransactions
()

Whether the database is capable of performing batch updates as atomic transactions.

public void
refresh()

Triggers a refresh of all internal data structures.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

additionalRefsNamesback to summary
protected static final String[] additionalRefsNames

The names of additional refs

Since
6.5
ALLback to summary
public static final String ALL

Magic value for getRefsByPrefix(String) to return all references.

MAX_SYMBOLIC_REF_DEPTHback to summary
public static final int MAX_SYMBOLIC_REF_DEPTH

Maximum number of times a SymbolicRef can be traversed.

If the reference is nested deeper than this depth, the implementation should either fail, or at least claim the reference does not exist.

Since
4.2
SEARCH_PATHback to summary
protected static final String[] SEARCH_PATH

Order of prefixes to search when using non-absolute references.

findRef(String) takes this search space into consideration when locating a reference by name. The first entry in the path is always "", ensuring that absolute references are resolved without further mangling.

Constructor Detail

RefDatabaseback to summary
public RefDatabase()

Method Detail

closeback to summary
public abstract void close()

Close any resources held by this database.

createback to summary
public abstract void create() throws IOException

Initialize a new reference database at this location.

Exceptions
IOException:
the database could not be created.
exactRefback to summary
public abstract Ref exactRef(String name) throws IOException

Read a single reference.

Unlike findRef, this method expects an unshortened reference name and does not search using the standard SEARCH_PATH.

Parameters
name:String

the unabbreviated name of the reference.

Returns:Ref

the reference (if it exists); else null.

Annotations
@Nullable
Exceptions
IOException:
the reference space cannot be accessed.
Since
4.1
exactRefback to summary
public Map<String, Ref> exactRef(String... refs) throws IOException

Read the specified references.

This method expects a list of unshortened reference names and returns a map from reference names to refs. Any named references that do not exist will not be included in the returned map.

Parameters
refs:String[]

the unabbreviated names of references to look up.

Returns:Map<String, Ref>

modifiable map describing any refs that exist among the ref ref names supplied. The map can be an unsorted map.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
Since
4.1
findRefback to summary
public final Ref findRef(String name) throws IOException

Read a single reference.

Aside from taking advantage of SEARCH_PATH, this method may be able to more quickly resolve a single reference name than obtaining the complete namespace by getRefs(ALL).get(name).

To read a specific reference without using @{link #SEARCH_PATH}, see exactRef(String).

Parameters
name:String

the name of the reference. May be a short name which must be searched for using the standard SEARCH_PATH.

Returns:Ref

the reference (if it exists); else null.

Annotations
@Nullable
Exceptions
IOException:
the reference space cannot be accessed.
Since
5.3
findRefback to summary
public static Ref findRef(Map<String, Ref> map, String name)

Try to find the specified name in the ref map using SEARCH_PATH.

Parameters
map:Map<String, Ref>

map of refs to search within. Names should be fully qualified, e.g. "refs/heads/master".

name:String

short name of ref to find, e.g. "master" to find "refs/heads/master" in map.

Returns:Ref

The first ref matching the name, or null if not found.

Annotations
@Nullable
Since
3.4
firstExactRefback to summary
public Ref firstExactRef(String... refs) throws IOException

Find the first named reference.

This method expects a list of unshortened reference names and returns the first that exists.

Parameters
refs:String[]

the unabbreviated names of references to look up.

Returns:Ref

the first named reference that exists (if any); else null.

Annotations
@Nullable
Exceptions
IOException:
the reference space cannot be accessed.
Since
4.1
getAdditionalRefsback to summary
public abstract List<Ref> getAdditionalRefs() throws IOException

Get the additional reference-like entities from the repository.

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 getRefs() but are accepted by findRef(String) and exactRef(String).

Returns:List<Ref>

a list of additional refs

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
getConflictingNamesback to summary
public Collection<String> getConflictingNames(String name) throws IOException

Determine if a proposed reference cannot coexist with existing ones. If the passed name already exists, it's not considered a conflict.

Parameters
name:String

proposed name to check for conflicts against

Returns:Collection<String>

a collection of full names of existing refs which would conflict with the passed ref name; empty collection when there are no conflicts

Annotations
@NonNull
Exceptions
IOException:
if an error occurred
Since
2.3
See Also
isNameConflicting(String)
getReflogReaderback to summary
public ReflogReader getReflogReader(String refName) throws IOException

Get the reflog reader

Parameters
refName:String

a java.lang.String object.

Returns:ReflogReader

a org.eclipse.jgit.lib.ReflogReader for the supplied refname, or null if the named ref does not exist.

Annotations
@Nullable
Exceptions
IOException:
the ref could not be accessed.
Since
7.2
getReflogReaderback to summary
public abstract ReflogReader getReflogReader(Ref ref) throws IOException

Get the reflog reader.

Parameters
ref:Ref

a Ref

Returns:ReflogReader

a org.eclipse.jgit.lib.ReflogReader for the supplied ref.

Annotations
@NonNull
Exceptions
IOException:
if an IO error occurred
Since
7.2
getRefsback to summary
public List<Ref> getRefs() throws IOException

Returns all refs.

This includes HEAD, branches under ref/heads/, tags under refs/tags/, etc. It does not include pseudo-refs like FETCH_HEAD; for those, see getAdditionalRefs.

Symbolic references to a non-existent ref (for example, HEAD pointing to a branch yet to be born) are not included.

Callers interested in only a portion of the ref hierarchy can call getRefsByPrefix instead.

Returns:List<Ref>

immutable list of all refs.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
Since
5.0
getRefsback to summary
public abstract Map<String, Ref> getRefs(String prefix) throws IOException

Deprecated

use getRefsByPrefix instead

Get a section of the reference namespace.

Parameters
prefix:String

prefix to search the namespace with; must end with /. If the empty string (ALL), obtain a complete snapshot of all references.

Returns:Map<String, Ref>

modifiable map that is a complete snapshot of the current reference namespace, with prefix removed from the start of each key. The map can be an unsorted map.

Annotations
@NonNull
@Deprecated
Exceptions
IOException:
the reference space cannot be accessed.
getRefsByPrefixback to summary
public List<Ref> getRefsByPrefix(String prefix) throws IOException

Returns refs whose names start with a given prefix.

The default implementation uses getRefs(String). Implementors of RefDatabase should override this method directly if a better implementation is possible.

Parameters
prefix:String

string that names of refs should start with; may be empty (to return all refs).

Returns:List<Ref>

immutable list of refs whose names start with prefix.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
Since
5.0
getRefsByPrefixback to summary
public List<Ref> getRefsByPrefix(String... prefixes) throws IOException

Returns refs whose names start with one of the given prefixes.

The default implementation uses getRefsByPrefix(String). Implementors of RefDatabase should override this method directly if a better implementation is possible.

Parameters
prefixes:String[]

strings that names of refs should start with.

Returns:List<Ref>

immutable list of refs whose names start with one of prefixes. Refs can be unsorted and may contain duplicates if the prefixes overlap.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
Since
5.2
getRefsByPrefixWithExclusionsback to summary
public List<Ref> getRefsByPrefixWithExclusions(String include, Set<String> excludes) throws IOException

Returns refs whose names start with a given prefix excluding all refs that start with one of the given prefixes.

The default implementation is not efficient. Implementors of RefDatabase should override this method directly if a better implementation is possible.

Parameters
include:String

string that names of refs should start with; may be empty.

excludes:Set<String>

strings that names of refs can't start with; may be empty.

Returns:List<Ref>

immutable list of refs whose names start with prefix and none of the strings in excludes.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
Since
5.11
getTipsWithSha1back to summary
public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException

Returns all refs that resolve directly to the given ObjectId. Includes peeled ObjectIds. This is the inverse lookup of exactRef(String...).

The default implementation uses a linear scan. Implementors of RefDatabase should override this method directly if a better implementation is possible.

Parameters
id:ObjectId

ObjectId to resolve

Returns:Set<Ref>

a Set of Refs whose tips point to the provided id.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
Since
5.4
hasFastTipsWithSha1back to summary
public boolean hasFastTipsWithSha1() throws IOException

If the ref database does not support fast inverse queries, it may be advantageous to build a complete SHA1 to ref map in advance for multiple uses. To let applications decide on this decision, this function indicates whether the inverse map is available.

Returns:boolean

whether this RefDatabase supports fast inverse ref queries.

Exceptions
IOException:
on I/O problems.
Since
5.6
hasRefsback to summary
public boolean hasRefs() throws IOException

Check if any refs exist in the ref database.

This uses the same definition of refs as getRefs(). In particular, returns false in a new repository with no refs under refs/ and HEAD pointing to a branch yet to be born, and returns true in a repository with no refs under refs/ and a detached HEAD pointing to history.

Returns:boolean

true if the database has refs.

Exceptions
IOException:
the reference space cannot be accessed.
Since
5.0
hasVersioningback to summary
public boolean hasVersioning()

With versioning, each reference has a version number that increases on update. See Ref#getUpdateIndex().

Implementation Specification

This method returns false by default. Implementations supporting versioning must override it to return true.

Returns:boolean

true if the implementation assigns update indices to references.

Since
5.3
isNameConflictingback to summary
public abstract boolean isNameConflicting(String name) throws IOException

Determine if a proposed reference name overlaps with an existing one.

Reference names use '/' as a component separator, and may be stored in a hierarchical storage such as a directory on the local filesystem.

If the reference "refs/heads/foo" exists then "refs/heads/foo/bar" must not exist, as a reference cannot have a value and also be a container for other references at the same time.

If the reference "refs/heads/foo/bar" exists than the reference "refs/heads/foo" cannot exist, for the same reason.

Parameters
name:String

proposed name.

Returns:boolean

true if the name overlaps with an existing reference; false if using this name right now would be safe.

Exceptions
IOException:
the database could not be read to check for conflicts.
See Also
getConflictingNames(String)
newBatchUpdateback to summary
public BatchRefUpdate newBatchUpdate()

Create a new batch update to attempt on this database.

The default implementation performs a sequential update of each command.

Returns:BatchRefUpdate

a new batch update object.

Annotations
@NonNull
newRenameback to summary
public abstract RefRename newRename(String fromName, String toName) throws IOException

Create a new update command to rename a reference.

Parameters
fromName:String

name of reference to rename from

toName:String

name of reference to rename to

Returns:RefRename

an update command that knows how to rename a branch to another.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
newUpdateback to summary
public abstract RefUpdate newUpdate(String name, boolean detach) throws IOException

Create a new update command to create, modify or delete a reference.

Parameters
name:String

the name of the reference.

detach:boolean

if true and name is currently a org.eclipse.jgit.lib.SymbolicRef, the update will replace it with an org.eclipse.jgit.lib.ObjectIdRef. Otherwise, the update will recursively traverse org.eclipse.jgit.lib.SymbolicRefs and operate on the leaf org.eclipse.jgit.lib.ObjectIdRef.

Returns:RefUpdate

a new update for the requested name; never null.

Annotations
@NonNull
Exceptions
IOException:
the reference space cannot be accessed.
packRefsback to summary
public void packRefs(ProgressMonitor pm, PackRefsCommand packRefs) throws IOException

Optimize pack ref storage.

Parameters
pm:ProgressMonitor

a progress monitor

packRefs:PackRefsCommand

PackRefsCommand to control ref packing behavior

Exceptions
IOException:
if an IO error occurred
Since
7.1
peelback to summary
public abstract Ref peel(Ref ref) throws IOException

Peel a possibly unpeeled reference by traversing the annotated tags.

If the reference cannot be peeled (as it does not refer to an annotated tag) the peeled id stays null, but org.eclipse.jgit.lib.Ref#isPeeled() will be true.

Implementors should check org.eclipse.jgit.lib.Ref#isPeeled() before performing any additional work effort.

Parameters
ref:Ref

The reference to peel

Returns:Ref

ref if ref.isPeeled() is true; otherwise a new Ref object representing the same data as Ref, but isPeeled() will be true and getPeeledObjectId() will contain the peeled object (or null).

Annotations
@NonNull
Exceptions
IOException:
the reference space or object space cannot be accessed.
performsAtomicTransactionsback to summary
public boolean performsAtomicTransactions()

Whether the database is capable of performing batch updates as atomic transactions.

If true, by default org.eclipse.jgit.lib.BatchRefUpdate instances will perform updates atomically, meaning either all updates will succeed, or all updates will fail. It is still possible to turn off this behavior on a per-batch basis by calling update.setAtomic(false).

If false, org.eclipse.jgit.lib.BatchRefUpdate instances will never perform updates atomically, and calling update.setAtomic(true) will cause the entire batch to fail with REJECTED_OTHER_REASON.

This definition of atomicity is stronger than what is provided by org.eclipse.jgit.transport.ReceivePack. ReceivePack will attempt to reject all commands if it knows in advance some commands may fail, even if the storage layer does not support atomic transactions. Here, atomicity applies even in the case of unforeseeable errors.

Returns:boolean

whether transactions are atomic by default.

Since
3.6
refreshback to summary
public void refresh()

Triggers a refresh of all internal data structures.

In case the RefDatabase implementation has internal caches this method will trigger that all these caches are cleared.

Implementors should overwrite this method if they use any kind of caches.