public class RefDirectory extends RefDatabase
RefDatabase
.
This is the classical reference database representation for a Git repository. References are stored in two formats: loose, and packed.
Loose references are stored as individual files within the refs/
directory. The file name matches the reference name and the file contents is
the current ObjectId
in string form.
Packed references are stored in a single text file named packed-refs
.
In the packed format, each reference is stored on its own line. This file
reduces the number of files needed for large reference spaces, reducing the
overall size of a Git repository on disk.
Modifier and Type | Class and Description |
---|---|
private static class |
RefDirectory.LooseNonTag |
private static class |
RefDirectory.LoosePeeledTag |
private static interface |
RefDirectory.LooseRef |
private class |
RefDirectory.LooseScanner |
private static class |
RefDirectory.LooseSymbolicRef |
private static class |
RefDirectory.LooseUnpeeled |
(package private) static class |
RefDirectory.PackedRefList |
Modifier and Type | Field and Description |
---|---|
private static java.lang.String[] |
additionalRefsNames
The names of the additional refs supported by this class
|
private java.io.File |
gitDir |
(package private) java.util.concurrent.locks.ReentrantLock |
inProcessPackedRefsLock
Lock for coordinating operations within a single process that may contend
on the
packed-refs file. |
private java.util.concurrent.atomic.AtomicInteger |
lastNotifiedModCnt
Last
modCnt that we sent to listeners. |
private static org.slf4j.Logger |
LOG |
(package private) java.io.File |
logsDir |
(package private) java.io.File |
logsRefsDir |
private java.util.concurrent.atomic.AtomicReference<RefList<RefDirectory.LooseRef>> |
looseRefs
Immutable sorted list of loose references.
|
private java.util.concurrent.atomic.AtomicInteger |
modCnt
Number of modifications made to this database.
|
private static RefDirectory.PackedRefList |
NO_PACKED_REFS |
static java.lang.String |
PACKED_REFS_HEADER
Magic string denoting the header of a packed-refs file.
|
static java.lang.String |
PACKED_REFS_PEELED
If in the header, denotes the file has peeled data.
|
(package private) java.util.concurrent.atomic.AtomicReference<RefDirectory.PackedRefList> |
packedRefs
Immutable sorted list of packed references.
|
(package private) java.io.File |
packedRefsFile |
private FileRepository |
parent |
(package private) java.io.File |
refsDir |
private static java.util.List<java.lang.Integer> |
RETRY_SLEEP_MS |
private java.util.List<java.lang.Integer> |
retrySleepMs |
static java.lang.String |
SYMREF
Magic string denoting the start of a symbolic reference file.
|
ALL, MAX_SYMBOLIC_REF_DEPTH, SEARCH_PATH
Constructor and Description |
---|
RefDirectory(FileRepository db) |
Modifier and Type | Method and Description |
---|---|
private void |
clearReferences() |
void |
close()
Close any resources held by this database.
|
(package private) RefDirectory.PackedRefList |
commitPackedRefs(LockFile lck,
RefList<Ref> refs,
RefDirectory.PackedRefList oldPackedList,
boolean changed) |
private static java.lang.String |
copy(java.lang.String src,
int off,
int end) |
void |
create()
Initialize a new reference database at this location.
|
(package private) static void |
delete(java.io.File file,
int depth) |
private static void |
delete(java.io.File file,
int depth,
LockFile rLck) |
(package private) void |
delete(RefDirectoryUpdate update) |
private ObjectIdRef |
doPeel(Ref leaf) |
java.util.Map<java.lang.String,Ref> |
exactRef(java.lang.String... refs)
Read the specified references.
|
Ref |
exactRef(java.lang.String name)
Read a single reference.
|
(package private) java.io.File |
fileFor(java.lang.String name)
Locate the file on disk for a single reference name.
|
(package private) void |
fireRefsChanged()
If the parent should fire listeners, fires them.
|
Ref |
firstExactRef(java.lang.String... refs)
Find the first named reference.
|
java.util.List<Ref> |
getAdditionalRefs()
Get the additional reference-like entities from the repository.
|
private RefList<RefDirectory.LooseRef> |
getLooseRefs() |
(package private) RefDirectory.PackedRefList |
getPackedRefs() |
java.util.Map<java.lang.String,Ref> |
getRefs(java.lang.String prefix)
Get a section of the reference namespace.
|
(package private) Repository |
getRepository() |
(package private) java.lang.Iterable<java.lang.Integer> |
getRetrySleepMs()
Get times to sleep while retrying a possibly contentious operation.
|
private boolean |
hasDanglingHead() |
private boolean |
hasLooseRef() |
(package private) boolean |
isInClone()
Detect if we are in a clone command execution
|
boolean |
isNameConflicting(java.lang.String name)
Determine if a proposed reference name overlaps with an existing one.
|
private static boolean |
isSymRef(byte[] buf,
int n) |
(package private) static int |
levelsIn(java.lang.String name) |
(package private) LockFile |
lockPackedRefs() |
private LockFile |
lockPackedRefsOrThrow() |
(package private) void |
log(boolean force,
RefUpdate update,
java.lang.String msg,
boolean deref) |
java.io.File |
logFor(java.lang.String name)
Locate the log file on disk for a single reference name.
|
PackedBatchRefUpdate |
newBatchUpdate()
Create a new batch update to attempt on this database.
|
(package private) ReflogWriter |
newLogWriter(boolean force) |
RefDirectoryRename |
newRename(java.lang.String fromName,
java.lang.String toName)
Create a new update command to rename a reference.
|
private static RefDirectory.LooseSymbolicRef |
newSymbolicRef(FileSnapshot snapshot,
java.lang.String name,
java.lang.String target) |
(package private) RefDirectoryUpdate |
newTemporaryUpdate()
Create a reference update to write a temporary reference.
|
RefDirectoryUpdate |
newUpdate(java.lang.String name,
boolean detach)
Create a new update command to create, modify or delete a reference.
|
private RefDirectory.PackedRefList |
pack(java.util.Collection<java.lang.String> refs,
java.util.Map<java.lang.String,LockFile> heldLocks) |
void |
pack(java.util.List<java.lang.String> refs)
Adds a set of refs to the set of packed-refs.
|
(package private) RefDirectory.PackedRefList |
pack(java.util.Map<java.lang.String,LockFile> heldLocks) |
private RefList<Ref> |
parsePackedRefs(java.io.BufferedReader br) |
Ref |
peel(Ref ref)
Peel a possibly unpeeled reference by traversing the annotated tags.
|
private Ref |
peeledPackedRef(Ref f)
Make sure a ref is peeled and has the Storage PACKED.
|
boolean |
performsAtomicTransactions()
Whether the database is capable of performing batch updates as atomic
transactions.
|
private void |
putLooseRef(RefDirectory.LooseRef ref) |
private Ref |
readAndResolve(java.lang.String name,
RefList<Ref> packed) |
private RefDirectory.PackedRefList |
readPackedRefs() |
private Ref |
readRef(java.lang.String name,
RefList<Ref> packed) |
private static Ref |
recreate(Ref old,
ObjectIdRef leaf) |
void |
refresh()
Triggers a refresh of all internal data structures.
|
private Ref |
resolve(Ref ref,
int depth,
java.lang.String prefix,
RefList<RefDirectory.LooseRef> loose,
RefList<Ref> packed) |
(package private) RefDirectory.LooseRef |
scanRef(RefDirectory.LooseRef ref,
java.lang.String name) |
(package private) void |
setRetrySleepMs(java.util.List<java.lang.Integer> retrySleepMs) |
(package private) static void |
sleep(long ms)
Sleep with
Thread.sleep(long) , converting InterruptedException to InterruptedIOException . |
(package private) void |
stored(RefDirectoryUpdate update,
FileSnapshot snapshot) |
(package private) void |
storedSymbolicRef(RefDirectoryUpdate u,
FileSnapshot snapshot,
java.lang.String target) |
private RefList<Ref> |
upcast(RefList<? extends Ref> loose) |
findRef, findRef, getConflictingNames, getRef, getRefs, getRefsByPrefix, getRefsByPrefix, getTipsWithSha1, hasRefs, hasVersioning
private static final org.slf4j.Logger LOG
public static final java.lang.String SYMREF
public static final java.lang.String PACKED_REFS_HEADER
public static final java.lang.String PACKED_REFS_PEELED
private static final java.lang.String[] additionalRefsNames
private static final java.util.List<java.lang.Integer> RETRY_SLEEP_MS
private final FileRepository parent
private final java.io.File gitDir
final java.io.File refsDir
final java.io.File packedRefsFile
final java.io.File logsDir
final java.io.File logsRefsDir
private final java.util.concurrent.atomic.AtomicReference<RefList<RefDirectory.LooseRef>> looseRefs
Symbolic references in this collection are stored unresolved, that is their target appears to be a new reference with no ObjectId. These are converted into resolved references during a get operation, ensuring the live value is always returned.
final java.util.concurrent.atomic.AtomicReference<RefDirectory.PackedRefList> packedRefs
final java.util.concurrent.locks.ReentrantLock inProcessPackedRefsLock
packed-refs
file.
All operations that write packed-refs
must still acquire a
LockFile
on packedRefsFile
, even after they have acquired
this lock, since there may be multiple RefDirectory
instances or
other processes operating on the same repo on disk.
This lock exists so multiple threads in the same process can wait in a fair
queue without trying, failing, and retrying to acquire the on-disk lock. If
RepositoryCache
is used, this lock instance will be used by all
threads.
private final java.util.concurrent.atomic.AtomicInteger modCnt
This counter is incremented when a change is made, or detected from the filesystem during a read operation.
private final java.util.concurrent.atomic.AtomicInteger lastNotifiedModCnt
private java.util.List<java.lang.Integer> retrySleepMs
private static final RefDirectory.PackedRefList NO_PACKED_REFS
RefDirectory(FileRepository db)
Repository getRepository()
ReflogWriter newLogWriter(boolean force)
public java.io.File logFor(java.lang.String name)
name
- name of the ref, relative to the Git repository top level
directory (so typically starts with refs/).public void create() throws java.io.IOException
create
in class RefDatabase
java.io.IOException
- the database could not be created.public void close()
close
in class RefDatabase
private void clearReferences()
public void refresh()
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.
refresh
in class RefDatabase
public boolean isNameConflicting(java.lang.String name) throws java.io.IOException
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.
isNameConflicting
in class RefDatabase
name
- proposed name.java.io.IOException
- the database could not be read to check for conflicts.RefDatabase.getConflictingNames(String)
private RefList<RefDirectory.LooseRef> getLooseRefs()
@Nullable private Ref readAndResolve(java.lang.String name, RefList<Ref> packed) throws java.io.IOException
java.io.IOException
public Ref exactRef(java.lang.String name) throws java.io.IOException
Unlike RefDatabase.findRef(java.lang.String)
, this method expects an unshortened reference
name and does not search using the standard RefDatabase.SEARCH_PATH
.
exactRef
in class RefDatabase
name
- the unabbreviated name of the reference.null
.java.io.IOException
- the reference space cannot be accessed.@NonNull public java.util.Map<java.lang.String,Ref> exactRef(java.lang.String... refs) throws java.io.IOException
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.
exactRef
in class RefDatabase
refs
- the unabbreviated names of references to look up.java.io.IOException
- the reference space cannot be accessed.@Nullable public Ref firstExactRef(java.lang.String... refs) throws java.io.IOException
This method expects a list of unshortened reference names and returns the first that exists.
firstExactRef
in class RefDatabase
refs
- the unabbreviated names of references to look up.null
.java.io.IOException
- the reference space cannot be accessed.public java.util.Map<java.lang.String,Ref> getRefs(java.lang.String prefix) throws java.io.IOException
getRefs
in class RefDatabase
prefix
- prefix to search the namespace with; must end with /
.
If the empty string (RefDatabase.ALL
), obtain a complete snapshot
of all references.prefix
removed from the start
of each key. The map can be an unsorted map.java.io.IOException
- the reference space cannot be accessed.public java.util.List<Ref> getAdditionalRefs() throws java.io.IOException
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 RefDatabase.findRef(String)
and RefDatabase.exactRef(String)
.
getAdditionalRefs
in class RefDatabase
java.io.IOException
- the reference space cannot be accessed.public Ref peel(Ref ref) throws java.io.IOException
If the reference cannot be peeled (as it does not refer to an annotated
tag) the peeled id stays null, but
Ref.isPeeled()
will be true.
Implementors should check Ref.isPeeled()
before performing any additional work effort.
peel
in class RefDatabase
ref
- The reference to peelref
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
).java.io.IOException
- the reference space or object space cannot be accessed.private ObjectIdRef doPeel(Ref leaf) throws MissingObjectException, java.io.IOException
MissingObjectException
java.io.IOException
private static Ref recreate(Ref old, ObjectIdRef leaf)
void storedSymbolicRef(RefDirectoryUpdate u, FileSnapshot snapshot, java.lang.String target)
public RefDirectoryUpdate newUpdate(java.lang.String name, boolean detach) throws java.io.IOException
newUpdate
in class RefDatabase
name
- the name of the reference.detach
- if true
and name
is currently a
SymbolicRef
, the update will
replace it with an ObjectIdRef
.
Otherwise, the update will recursively traverse
SymbolicRef
s and operate on the
leaf ObjectIdRef
.java.io.IOException
- the reference space cannot be accessed.public RefDirectoryRename newRename(java.lang.String fromName, java.lang.String toName) throws java.io.IOException
newRename
in class RefDatabase
fromName
- name of reference to rename fromtoName
- name of reference to rename tojava.io.IOException
- the reference space cannot be accessed.public PackedBatchRefUpdate newBatchUpdate()
The default implementation performs a sequential update of each command.
newBatchUpdate
in class RefDatabase
public boolean performsAtomicTransactions()
If true, by default 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, 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
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.
performsAtomicTransactions
in class RefDatabase
void stored(RefDirectoryUpdate update, FileSnapshot snapshot)
private void putLooseRef(RefDirectory.LooseRef ref)
void delete(RefDirectoryUpdate update) throws java.io.IOException
java.io.IOException
public void pack(java.util.List<java.lang.String> refs) throws java.io.IOException
refs
- the refs to be added. Must be fully qualified.java.io.IOException
RefDirectory.PackedRefList pack(java.util.Map<java.lang.String,LockFile> heldLocks) throws java.io.IOException
java.io.IOException
private RefDirectory.PackedRefList pack(java.util.Collection<java.lang.String> refs, java.util.Map<java.lang.String,LockFile> heldLocks) throws java.io.IOException
java.io.IOException
@Nullable LockFile lockPackedRefs() throws java.io.IOException
java.io.IOException
private LockFile lockPackedRefsOrThrow() throws java.io.IOException
java.io.IOException
private Ref peeledPackedRef(Ref f) throws MissingObjectException, java.io.IOException
ObjectIdRef
where Storage is set to PACKED.f
- MissingObjectException
java.io.IOException
void log(boolean force, RefUpdate update, java.lang.String msg, boolean deref) throws java.io.IOException
java.io.IOException
private Ref resolve(Ref ref, int depth, java.lang.String prefix, RefList<RefDirectory.LooseRef> loose, RefList<Ref> packed) throws java.io.IOException
java.io.IOException
RefDirectory.PackedRefList getPackedRefs() throws java.io.IOException
java.io.IOException
private RefDirectory.PackedRefList readPackedRefs() throws java.io.IOException
java.io.IOException
private RefList<Ref> parsePackedRefs(java.io.BufferedReader br) throws java.io.IOException
java.io.IOException
private static java.lang.String copy(java.lang.String src, int off, int end)
RefDirectory.PackedRefList commitPackedRefs(LockFile lck, RefList<Ref> refs, RefDirectory.PackedRefList oldPackedList, boolean changed) throws java.io.IOException
java.io.IOException
private Ref readRef(java.lang.String name, RefList<Ref> packed) throws java.io.IOException
java.io.IOException
RefDirectory.LooseRef scanRef(RefDirectory.LooseRef ref, java.lang.String name) throws java.io.IOException
java.io.IOException
private static boolean isSymRef(byte[] buf, int n)
boolean isInClone() throws java.io.IOException
true
if we are currently cloning a repositoryjava.io.IOException
private boolean hasDanglingHead() throws java.io.IOException
java.io.IOException
private boolean hasLooseRef() throws java.io.IOException
java.io.IOException
void fireRefsChanged()
RefDirectoryUpdate newTemporaryUpdate() throws java.io.IOException
java.io.IOException
- a temporary name cannot be allocated.java.io.File fileFor(java.lang.String name)
name
- name of the ref, relative to the Git repository top level
directory (so typically starts with refs/).static int levelsIn(java.lang.String name)
static void delete(java.io.File file, int depth) throws java.io.IOException
java.io.IOException
private static void delete(java.io.File file, int depth, LockFile rLck) throws java.io.IOException
java.io.IOException
java.lang.Iterable<java.lang.Integer> getRetrySleepMs()
For retrying an operation that might have high contention, such as locking
the packed-refs
file, the caller may implement a retry loop using
the returned values:
for (int toSleepMs : getRetrySleepMs()) { sleep(toSleepMs); if (isSuccessful(doSomething())) { return success; } } return failure;The first value in the returned iterable is 0, and the caller should treat a fully-consumed iterator as a timeout.
void setRetrySleepMs(java.util.List<java.lang.Integer> retrySleepMs)
static void sleep(long ms) throws java.io.InterruptedIOException
Thread.sleep(long)
, converting InterruptedException
to InterruptedIOException
.ms
- time to sleep, in milliseconds; zero or negative is a no-op.java.io.InterruptedIOException
- if sleeping was interrupted.private static RefDirectory.LooseSymbolicRef newSymbolicRef(FileSnapshot snapshot, java.lang.String name, java.lang.String target)