Class RefDirectory
- java.lang.Object
-
- org.eclipse.jgit.lib.RefDatabase
-
- org.eclipse.jgit.internal.storage.file.RefDirectory
-
public class RefDirectory extends RefDatabase
Traditional file system basedRefDatabase.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 currentObjectIdin 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classRefDirectory.LooseNonTagprivate static classRefDirectory.LoosePeeledTagprivate static interfaceRefDirectory.LooseRefprivate classRefDirectory.LooseScannerprivate static classRefDirectory.LooseSymbolicRefprivate static classRefDirectory.LooseUnpeeled(package private) static classRefDirectory.PackedRefList
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String[]additionalRefsNamesThe names of the additional refs supported by this classprivate java.io.FilegitDir(package private) java.util.concurrent.locks.ReentrantLockinProcessPackedRefsLockLock for coordinating operations within a single process that may contend on thepacked-refsfile.private java.util.concurrent.atomic.AtomicIntegerlastNotifiedModCntLastmodCntthat we sent to listeners.private static org.slf4j.LoggerLOG(package private) java.io.FilelogsDir(package private) java.io.FilelogsRefsDirprivate java.util.concurrent.atomic.AtomicReference<RefList<RefDirectory.LooseRef>>looseRefsImmutable sorted list of loose references.private java.util.concurrent.atomic.AtomicIntegermodCntNumber of modifications made to this database.private static RefDirectory.PackedRefListNO_PACKED_REFSstatic java.lang.StringPACKED_REFS_HEADERMagic string denoting the header of a packed-refs file.static java.lang.StringPACKED_REFS_PEELEDIf in the header, denotes the file has peeled data.(package private) java.util.concurrent.atomic.AtomicReference<RefDirectory.PackedRefList>packedRefsImmutable sorted list of packed references.(package private) java.io.FilepackedRefsFileprivate FileRepositoryparent(package private) java.io.FilerefsDirprivate static java.util.List<java.lang.Integer>RETRY_SLEEP_MSprivate java.util.List<java.lang.Integer>retrySleepMsstatic java.lang.StringSYMREFMagic string denoting the start of a symbolic reference file.-
Fields inherited from class org.eclipse.jgit.lib.RefDatabase
ALL, MAX_SYMBOLIC_REF_DEPTH, SEARCH_PATH
-
-
Constructor Summary
Constructors Constructor Description RefDirectory(FileRepository db)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidclearReferences()voidclose()Close any resources held by this database.(package private) RefDirectory.PackedRefListcommitPackedRefs(LockFile lck, RefList<Ref> refs, RefDirectory.PackedRefList oldPackedList, boolean changed)private static java.lang.Stringcopy(java.lang.String src, int off, int end)voidcreate()Initialize a new reference database at this location.(package private) static voiddelete(java.io.File file, int depth)private static voiddelete(java.io.File file, int depth, LockFile rLck)(package private) voiddelete(RefDirectoryUpdate update)private ObjectIdRefdoPeel(Ref leaf)RefexactRef(java.lang.String name)Read a single reference.java.util.Map<java.lang.String,Ref>exactRef(java.lang.String... refs)Read the specified references.(package private) java.io.FilefileFor(java.lang.String name)Locate the file on disk for a single reference name.(package private) voidfireRefsChanged()If the parent should fire listeners, fires them.ReffirstExactRef(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.PackedRefListgetPackedRefs()java.util.Map<java.lang.String,Ref>getRefs(java.lang.String prefix)Get a section of the reference namespace.(package private) RepositorygetRepository()(package private) java.lang.Iterable<java.lang.Integer>getRetrySleepMs()Get times to sleep while retrying a possibly contentious operation.private booleanhasDanglingHead()private booleanhasLooseRef()(package private) booleanisInClone()Detect if we are in a clone command executionbooleanisNameConflicting(java.lang.String name)Determine if a proposed reference name overlaps with an existing one.private static booleanisSymRef(byte[] buf, int n)(package private) static intlevelsIn(java.lang.String name)(package private) LockFilelockPackedRefs()private LockFilelockPackedRefsOrThrow()(package private) voidlog(boolean force, RefUpdate update, java.lang.String msg, boolean deref)java.io.FilelogFor(java.lang.String name)Locate the log file on disk for a single reference name.PackedBatchRefUpdatenewBatchUpdate()Create a new batch update to attempt on this database.(package private) ReflogWriternewLogWriter(boolean force)RefDirectoryRenamenewRename(java.lang.String fromName, java.lang.String toName)Create a new update command to rename a reference.private static RefDirectory.LooseSymbolicRefnewSymbolicRef(FileSnapshot snapshot, java.lang.String name, java.lang.String target)(package private) RefDirectoryUpdatenewTemporaryUpdate()Create a reference update to write a temporary reference.RefDirectoryUpdatenewUpdate(java.lang.String name, boolean detach)Create a new update command to create, modify or delete a reference.private RefDirectory.PackedRefListpack(java.util.Collection<java.lang.String> refs, java.util.Map<java.lang.String,LockFile> heldLocks)voidpack(java.util.List<java.lang.String> refs)Adds a set of refs to the set of packed-refs.(package private) RefDirectory.PackedRefListpack(java.util.Map<java.lang.String,LockFile> heldLocks)private RefList<Ref>parsePackedRefs(java.io.BufferedReader br)Refpeel(Ref ref)Peel a possibly unpeeled reference by traversing the annotated tags.private RefpeeledPackedRef(Ref f)Make sure a ref is peeled and has the Storage PACKED.booleanperformsAtomicTransactions()Whether the database is capable of performing batch updates as atomic transactions.private voidputLooseRef(RefDirectory.LooseRef ref)private RefreadAndResolve(java.lang.String name, RefList<Ref> packed)private RefDirectory.PackedRefListreadPackedRefs()private RefreadRef(java.lang.String name, RefList<Ref> packed)private static Refrecreate(Ref old, ObjectIdRef leaf)voidrefresh()Triggers a refresh of all internal data structures.private Refresolve(Ref ref, int depth, java.lang.String prefix, RefList<RefDirectory.LooseRef> loose, RefList<Ref> packed)(package private) RefDirectory.LooseRefscanRef(RefDirectory.LooseRef ref, java.lang.String name)(package private) voidsetRetrySleepMs(java.util.List<java.lang.Integer> retrySleepMs)(package private) static voidsleep(long ms)Sleep withThread.sleep(long), convertingInterruptedExceptiontoInterruptedIOException.(package private) voidstored(RefDirectoryUpdate update, FileSnapshot snapshot)(package private) voidstoredSymbolicRef(RefDirectoryUpdate u, FileSnapshot snapshot, java.lang.String target)private RefList<Ref>upcast(RefList<? extends Ref> loose)-
Methods inherited from class org.eclipse.jgit.lib.RefDatabase
findRef, findRef, getConflictingNames, getRef, getRefs, getRefsByPrefix, getRefsByPrefix, getRefsByPrefixWithExclusions, getTipsWithSha1, hasFastTipsWithSha1, hasRefs, hasVersioning
-
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
SYMREF
public static final java.lang.String SYMREF
Magic string denoting the start of a symbolic reference file.- See Also:
- Constant Field Values
-
PACKED_REFS_HEADER
public static final java.lang.String PACKED_REFS_HEADER
Magic string denoting the header of a packed-refs file.- See Also:
- Constant Field Values
-
PACKED_REFS_PEELED
public static final java.lang.String PACKED_REFS_PEELED
If in the header, denotes the file has peeled data.- See Also:
- Constant Field Values
-
additionalRefsNames
private static final java.lang.String[] additionalRefsNames
The names of the additional refs supported by this class
-
RETRY_SLEEP_MS
private static final java.util.List<java.lang.Integer> RETRY_SLEEP_MS
-
parent
private final FileRepository parent
-
gitDir
private final java.io.File gitDir
-
refsDir
final java.io.File refsDir
-
packedRefsFile
final java.io.File packedRefsFile
-
logsDir
final java.io.File logsDir
-
logsRefsDir
final java.io.File logsRefsDir
-
looseRefs
private final java.util.concurrent.atomic.AtomicReference<RefList<RefDirectory.LooseRef>> looseRefs
Immutable sorted list of loose references.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.
-
packedRefs
final java.util.concurrent.atomic.AtomicReference<RefDirectory.PackedRefList> packedRefs
Immutable sorted list of packed references.
-
inProcessPackedRefsLock
final java.util.concurrent.locks.ReentrantLock inProcessPackedRefsLock
Lock for coordinating operations within a single process that may contend on thepacked-refsfile.All operations that write
packed-refsmust still acquire aLockFileonpackedRefsFile, even after they have acquired this lock, since there may be multipleRefDirectoryinstances 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
RepositoryCacheis used, this lock instance will be used by all threads.
-
modCnt
private final java.util.concurrent.atomic.AtomicInteger modCnt
Number of modifications made to this database.This counter is incremented when a change is made, or detected from the filesystem during a read operation.
-
lastNotifiedModCnt
private final java.util.concurrent.atomic.AtomicInteger lastNotifiedModCnt
-
retrySleepMs
private java.util.List<java.lang.Integer> retrySleepMs
-
NO_PACKED_REFS
private static final RefDirectory.PackedRefList NO_PACKED_REFS
-
-
Constructor Detail
-
RefDirectory
RefDirectory(FileRepository db)
-
-
Method Detail
-
getRepository
Repository getRepository()
-
newLogWriter
ReflogWriter newLogWriter(boolean force)
-
logFor
public java.io.File logFor(java.lang.String name)
Locate the log file on disk for a single reference name.- Parameters:
name- name of the ref, relative to the Git repository top level directory (so typically starts with refs/).- Returns:
- the log file location.
-
create
public void create() throws java.io.IOExceptionInitialize a new reference database at this location.- Specified by:
createin classRefDatabase- Throws:
java.io.IOException- the database could not be created.
-
close
public void close()
Close any resources held by this database.- Specified by:
closein classRefDatabase
-
clearReferences
private void clearReferences()
-
refresh
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.
- Overrides:
refreshin classRefDatabase
-
isNameConflicting
public boolean isNameConflicting(java.lang.String name) throws java.io.IOExceptionDetermine 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.
- Specified by:
isNameConflictingin classRefDatabase- Parameters:
name- proposed name.- Returns:
- true if the name overlaps with an existing reference; false if using this name right now would be safe.
- Throws:
java.io.IOException- the database could not be read to check for conflicts.- See Also:
RefDatabase.getConflictingNames(String)
-
getLooseRefs
private RefList<RefDirectory.LooseRef> getLooseRefs()
-
readAndResolve
@Nullable private Ref readAndResolve(java.lang.String name, RefList<Ref> packed) throws java.io.IOException
- Throws:
java.io.IOException
-
exactRef
public Ref exactRef(java.lang.String name) throws java.io.IOException
Read a single reference.Unlike
RefDatabase.findRef(java.lang.String), this method expects an unshortened reference name and does not search using the standardRefDatabase.SEARCH_PATH.- Specified by:
exactRefin classRefDatabase- Parameters:
name- the unabbreviated name of the reference.- Returns:
- the reference (if it exists); else
null. - Throws:
java.io.IOException- the reference space cannot be accessed.
-
exactRef
@NonNull public java.util.Map<java.lang.String,Ref> exactRef(java.lang.String... refs) throws java.io.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.
- Overrides:
exactRefin classRefDatabase- Parameters:
refs- the unabbreviated names of references to look up.- Returns:
- modifiable map describing any refs that exist among the ref ref names supplied. The map can be an unsorted map.
- Throws:
java.io.IOException- the reference space cannot be accessed.
-
firstExactRef
@Nullable public Ref firstExactRef(java.lang.String... refs) throws java.io.IOException
Find the first named reference.This method expects a list of unshortened reference names and returns the first that exists.
- Overrides:
firstExactRefin classRefDatabase- Parameters:
refs- the unabbreviated names of references to look up.- Returns:
- the first named reference that exists (if any); else
null. - Throws:
java.io.IOException- the reference space cannot be accessed.
-
getRefs
public java.util.Map<java.lang.String,Ref> getRefs(java.lang.String prefix) throws java.io.IOException
Get a section of the reference namespace.- Specified by:
getRefsin classRefDatabase- Parameters:
prefix- prefix to search the namespace with; must end with/. If the empty string (RefDatabase.ALL), obtain a complete snapshot of all references.- Returns:
- modifiable map that is a complete snapshot of the current
reference namespace, with
prefixremoved from the start of each key. The map can be an unsorted map. - Throws:
java.io.IOException- the reference space cannot be accessed.
-
getAdditionalRefs
public java.util.List<Ref> getAdditionalRefs() throws java.io.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 byRefDatabase.findRef(String)andRefDatabase.exactRef(String).- Specified by:
getAdditionalRefsin classRefDatabase- Returns:
- a list of additional refs
- Throws:
java.io.IOException- the reference space cannot be accessed.
-
peel
public Ref peel(Ref ref) throws java.io.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
Ref.isPeeled()will be true.Implementors should check
Ref.isPeeled()before performing any additional work effort.- Specified by:
peelin classRefDatabase- Parameters:
ref- The reference to peel- Returns:
refifref.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 (ornull).- Throws:
java.io.IOException- the reference space or object space cannot be accessed.
-
doPeel
private ObjectIdRef doPeel(Ref leaf) throws MissingObjectException, java.io.IOException
- Throws:
MissingObjectExceptionjava.io.IOException
-
recreate
private static Ref recreate(Ref old, ObjectIdRef leaf)
-
storedSymbolicRef
void storedSymbolicRef(RefDirectoryUpdate u, FileSnapshot snapshot, java.lang.String target)
-
newUpdate
public RefDirectoryUpdate newUpdate(java.lang.String name, boolean detach) throws java.io.IOException
Create a new update command to create, modify or delete a reference.- Specified by:
newUpdatein classRefDatabase- Parameters:
name- the name of the reference.detach- iftrueandnameis currently aSymbolicRef, the update will replace it with anObjectIdRef. Otherwise, the update will recursively traverseSymbolicRefs and operate on the leafObjectIdRef.- Returns:
- a new update for the requested name; never null.
- Throws:
java.io.IOException- the reference space cannot be accessed.
-
newRename
public RefDirectoryRename newRename(java.lang.String fromName, java.lang.String toName) throws java.io.IOException
Create a new update command to rename a reference.- Specified by:
newRenamein classRefDatabase- Parameters:
fromName- name of reference to rename fromtoName- name of reference to rename to- Returns:
- an update command that knows how to rename a branch to another.
- Throws:
java.io.IOException- the reference space cannot be accessed.
-
newBatchUpdate
public PackedBatchRefUpdate newBatchUpdate()
Create a new batch update to attempt on this database.The default implementation performs a sequential update of each command.
- Overrides:
newBatchUpdatein classRefDatabase- Returns:
- a new batch update object.
-
performsAtomicTransactions
public boolean performsAtomicTransactions()
Whether the database is capable of performing batch updates as atomic transactions.If true, by default
BatchRefUpdateinstances 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 callingupdate.setAtomic(false).If false,
BatchRefUpdateinstances will never perform updates atomically, and callingupdate.setAtomic(true)will cause the entire batch to fail withREJECTED_OTHER_REASON.This definition of atomicity is stronger than what is provided by
ReceivePack.ReceivePackwill 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.- Overrides:
performsAtomicTransactionsin classRefDatabase- Returns:
- whether transactions are atomic by default.
-
stored
void stored(RefDirectoryUpdate update, FileSnapshot snapshot)
-
putLooseRef
private void putLooseRef(RefDirectory.LooseRef ref)
-
delete
void delete(RefDirectoryUpdate update) throws java.io.IOException
- Throws:
java.io.IOException
-
pack
public void pack(java.util.List<java.lang.String> refs) throws java.io.IOExceptionAdds a set of refs to the set of packed-refs. Only non-symbolic refs are added. If a ref with the given name already existed in packed-refs it is updated with the new value. Each loose ref which was added to the packed-ref file is deleted. If a given ref can't be locked it will not be added to the pack file.- Parameters:
refs- the refs to be added. Must be fully qualified.- Throws:
java.io.IOException
-
pack
RefDirectory.PackedRefList pack(java.util.Map<java.lang.String,LockFile> heldLocks) throws java.io.IOException
- Throws:
java.io.IOException
-
pack
private RefDirectory.PackedRefList pack(java.util.Collection<java.lang.String> refs, java.util.Map<java.lang.String,LockFile> heldLocks) throws java.io.IOException
- Throws:
java.io.IOException
-
lockPackedRefs
@Nullable LockFile lockPackedRefs() throws java.io.IOException
- Throws:
java.io.IOException
-
lockPackedRefsOrThrow
private LockFile lockPackedRefsOrThrow() throws java.io.IOException
- Throws:
java.io.IOException
-
peeledPackedRef
private Ref peeledPackedRef(Ref f) throws MissingObjectException, java.io.IOException
Make sure a ref is peeled and has the Storage PACKED. If the given ref has this attributes simply return it. Otherwise create a new peeledObjectIdRefwhere Storage is set to PACKED.- Parameters:
f-- Returns:
- a ref for Storage PACKED having the same name, id, peeledId as f
- Throws:
MissingObjectExceptionjava.io.IOException
-
log
void log(boolean force, RefUpdate update, java.lang.String msg, boolean deref) throws java.io.IOException- Throws:
java.io.IOException
-
resolve
private Ref resolve(Ref ref, int depth, java.lang.String prefix, RefList<RefDirectory.LooseRef> loose, RefList<Ref> packed) throws java.io.IOException
- Throws:
java.io.IOException
-
getPackedRefs
RefDirectory.PackedRefList getPackedRefs() throws java.io.IOException
- Throws:
java.io.IOException
-
readPackedRefs
private RefDirectory.PackedRefList readPackedRefs() throws java.io.IOException
- Throws:
java.io.IOException
-
parsePackedRefs
private RefList<Ref> parsePackedRefs(java.io.BufferedReader br) throws java.io.IOException
- Throws:
java.io.IOException
-
copy
private static java.lang.String copy(java.lang.String src, int off, int end)
-
commitPackedRefs
RefDirectory.PackedRefList commitPackedRefs(LockFile lck, RefList<Ref> refs, RefDirectory.PackedRefList oldPackedList, boolean changed) throws java.io.IOException
- Throws:
java.io.IOException
-
readRef
private Ref readRef(java.lang.String name, RefList<Ref> packed) throws java.io.IOException
- Throws:
java.io.IOException
-
scanRef
RefDirectory.LooseRef scanRef(RefDirectory.LooseRef ref, java.lang.String name) throws java.io.IOException
- Throws:
java.io.IOException
-
isSymRef
private static boolean isSymRef(byte[] buf, int n)
-
isInClone
boolean isInClone() throws java.io.IOExceptionDetect if we are in a clone command execution- Returns:
trueif we are currently cloning a repository- Throws:
java.io.IOException
-
hasDanglingHead
private boolean hasDanglingHead() throws java.io.IOException- Throws:
java.io.IOException
-
hasLooseRef
private boolean hasLooseRef() throws java.io.IOException- Throws:
java.io.IOException
-
fireRefsChanged
void fireRefsChanged()
If the parent should fire listeners, fires them.
-
newTemporaryUpdate
RefDirectoryUpdate newTemporaryUpdate() throws java.io.IOException
Create a reference update to write a temporary reference.- Returns:
- an update for a new temporary reference.
- Throws:
java.io.IOException- a temporary name cannot be allocated.
-
fileFor
java.io.File fileFor(java.lang.String name)
Locate the file on disk for a single reference name.- Parameters:
name- name of the ref, relative to the Git repository top level directory (so typically starts with refs/).- Returns:
- the loose file location.
-
levelsIn
static int levelsIn(java.lang.String name)
-
delete
static void delete(java.io.File file, int depth) throws java.io.IOException- Throws:
java.io.IOException
-
delete
private static void delete(java.io.File file, int depth, LockFile rLck) throws java.io.IOException- Throws:
java.io.IOException
-
getRetrySleepMs
java.lang.Iterable<java.lang.Integer> getRetrySleepMs()
Get times to sleep while retrying a possibly contentious operation.For retrying an operation that might have high contention, such as locking the
packed-refsfile, 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.- Returns:
- iterable of times, in milliseconds, that the caller should sleep before attempting an operation.
-
setRetrySleepMs
void setRetrySleepMs(java.util.List<java.lang.Integer> retrySleepMs)
-
sleep
static void sleep(long ms) throws java.io.InterruptedIOExceptionSleep withThread.sleep(long), convertingInterruptedExceptiontoInterruptedIOException.- Parameters:
ms- time to sleep, in milliseconds; zero or negative is a no-op.- Throws:
java.io.InterruptedIOException- if sleeping was interrupted.
-
newSymbolicRef
private static RefDirectory.LooseSymbolicRef newSymbolicRef(FileSnapshot snapshot, java.lang.String name, java.lang.String target)
-
-