public abstract class Repository extends Object implements AutoCloseable
A repository holds all objects and refs used for managing source code (could be any type of file, but source code is what SCM's are typically used for).
The thread-safety of a Repository
very much
depends on the concrete implementation. Applications working with a generic
Repository
type must not assume the instance is thread-safe.
FileRepository
is thread-safe.
DfsRepository
thread-safety is determined by its subclass.
Modifier | Constructor and Description |
---|---|
protected |
Repository(BaseRepositoryBuilder options)
Initialize a new repository instance.
|
Modifier and Type | Method and Description |
---|---|
void |
autoGC(ProgressMonitor monitor)
Check whether any housekeeping is required; if yes, run garbage
collection; if not, exit without performing any work.
|
void |
close() |
void |
create()
Create a new Git repository.
|
abstract void |
create(boolean bare)
Create a new Git repository initializing the necessary files and
directories.
|
abstract AttributesNodeProvider |
createAttributesNodeProvider()
Create a new
AttributesNodeProvider . |
protected void |
doClose()
Invoked when the use count drops to zero during
close() . |
Ref |
exactRef(String name)
Get a ref by name.
|
Ref |
findRef(String name)
Search for a ref by (possibly abbreviated) name.
|
void |
fireEvent(RepositoryEvent<?> event)
Fire an event to all registered listeners.
|
Set<ObjectId> |
getAdditionalHaves()
Objects known to exist but not expressed by
getAllRefs() . |
Map<String,Ref> |
getAllRefs()
Deprecated.
use
getRefDatabase().getRefs() instead. |
Map<AnyObjectId,Set<Ref>> |
getAllRefsByPeeledObjectId()
Get a map with all objects referenced by a peeled ref.
|
String |
getBranch()
Get the short name of the current branch that
HEAD points to. |
abstract StoredConfig |
getConfig()
Get the configuration of this repository.
|
File |
getDirectory()
Get local metadata directory
|
FS |
getFS()
Get the used file system abstraction.
|
String |
getFullBranch()
Get the name of the reference that
HEAD points to. |
String |
getGitwebDescription()
Read the
GIT_DIR/description file for gitweb. |
static ListenerList |
getGlobalListenerList()
Get the global listener list observing all events in this JVM.
|
abstract String |
getIdentifier()
Get repository identifier.
|
File |
getIndexFile()
Get the index file location or
null if repository isn't local. |
ListenerList |
getListenerList()
Get listeners observing only events on this repository.
|
abstract ObjectDatabase |
getObjectDatabase()
Get the object database which stores this repository's data.
|
abstract RefDatabase |
getRefDatabase()
Get the reference database which stores the reference namespace.
|
abstract ReflogReader |
getReflogReader(String refName)
Get the reflog reader
|
String |
getRemoteName(String refName)
Get remote name
|
Set<String> |
getRemoteNames()
Get the names of all known remotes
|
RepositoryState |
getRepositoryState()
Get the repository state
|
Map<String,Ref> |
getTags()
Deprecated.
use
getRefDatabase().getRefsByPrefix(R_TAGS) instead |
File |
getWorkTree()
Get the root directory of the working tree, where files are checked out
for viewing and editing.
|
boolean |
hasObject(AnyObjectId objectId)
Deprecated.
use
getObjectDatabase().has(objectId) |
void |
incrementOpen()
Increment the use counter by one, requiring a matched
close() . |
boolean |
isBare()
Whether this repository is bare
|
static boolean |
isValidRefName(String refName)
Check validity of a ref name.
|
DirCache |
lockDirCache()
Create a new in-core index representation, lock it, and read from disk.
|
ObjectInserter |
newObjectInserter()
Create a new inserter to create objects in
getObjectDatabase() . |
ObjectReader |
newObjectReader()
Create a new reader to read objects from
getObjectDatabase() . |
static String |
normalizeBranchName(String name)
Normalizes the passed branch name into a possible valid branch name.
|
abstract void |
notifyIndexChanged(boolean internal)
Notify that the index changed by firing an IndexChangedEvent.
|
ObjectLoader |
open(AnyObjectId objectId)
Open an object from this repository.
|
ObjectLoader |
open(AnyObjectId objectId,
int typeHint)
Open an object from this repository.
|
RevCommit |
parseCommit(AnyObjectId id)
Locate a reference to a commit and immediately parse its content.
|
Ref |
peel(Ref ref)
Deprecated.
use
getRefDatabase().peel(ref) instead. |
ObjectId |
readCherryPickHead()
Return the information stored in the file $GIT_DIR/CHERRY_PICK_HEAD.
|
String |
readCommitEditMsg()
Return the information stored in the file $GIT_DIR/COMMIT_EDITMSG.
|
DirCache |
readDirCache()
Create a new in-core index representation and read an index from disk.
|
String |
readMergeCommitMsg()
Return the information stored in the file $GIT_DIR/MERGE_MSG.
|
List<ObjectId> |
readMergeHeads()
Return the information stored in the file $GIT_DIR/MERGE_HEAD.
|
ObjectId |
readOrigHead()
Return the information stored in the file $GIT_DIR/ORIG_HEAD.
|
List<RebaseTodoLine> |
readRebaseTodo(String path,
boolean includeComments)
Read a file formatted like the git-rebase-todo file.
|
ObjectId |
readRevertHead()
Return the information stored in the file $GIT_DIR/REVERT_HEAD.
|
String |
readSquashCommitMsg()
Return the information stored in the file $GIT_DIR/SQUASH_MSG.
|
RefRename |
renameRef(String fromRef,
String toRef)
Create a command to rename a ref in this repository
|
ObjectId |
resolve(String revstr)
Parse a git revision string and return an object id.
|
abstract void |
scanForRepoChanges()
Force a scan for changed refs.
|
void |
setGitwebDescription(String description)
Set the
GIT_DIR/description file for gitweb. |
static String |
shortenRefName(String refName)
Get a shortened more user friendly ref name
|
String |
shortenRemoteBranchName(String refName)
Get a shortened more user friendly remote tracking branch name
|
String |
simplify(String revstr)
Simplify an expression, but unlike
resolve(String) it will not
resolve a branch passed or resulting from the expression, such as @{-}. |
static String |
stripWorkDir(File workDir,
File file)
Strip work dir and return normalized repository path.
|
String |
toString() |
RefUpdate |
updateRef(String ref)
Create a command to update, create or delete a ref in this repository.
|
RefUpdate |
updateRef(String ref,
boolean detach)
Create a command to update, create or delete a ref in this repository.
|
void |
writeCherryPickHead(ObjectId head)
Write cherry pick commit into $GIT_DIR/CHERRY_PICK_HEAD.
|
void |
writeCommitEditMsg(String msg)
Write new content to the file $GIT_DIR/COMMIT_EDITMSG.
|
void |
writeMergeCommitMsg(String msg)
Write new content to the file $GIT_DIR/MERGE_MSG.
|
void |
writeMergeHeads(List<? extends ObjectId> heads)
Write new merge-heads into $GIT_DIR/MERGE_HEAD.
|
void |
writeOrigHead(ObjectId head)
Write original HEAD commit into $GIT_DIR/ORIG_HEAD.
|
void |
writeRebaseTodoFile(String path,
List<RebaseTodoLine> steps,
boolean append)
Write a file formatted like a git-rebase-todo file.
|
void |
writeRevertHead(ObjectId head)
Write revert commit into $GIT_DIR/REVERT_HEAD.
|
void |
writeSquashCommitMsg(String msg)
Write new content to the file $GIT_DIR/SQUASH_MSG.
|
protected Repository(BaseRepositoryBuilder options)
options
- options to configure the repository.public static ListenerList getGlobalListenerList()
@NonNull public ListenerList getListenerList()
public void fireEvent(RepositoryEvent<?> event)
The source repository of the event is automatically set to this repository, before the event is delivered to any listeners.
event
- the event to deliver.public void create() throws IOException
Repository with working tree is created using this method. This method is
the same as create(false)
.
IOException
create(boolean)
public abstract void create(boolean bare) throws IOException
bare
- if true, a bare repository (a repository without a working
directory) is created.IOException
- in case of IO problempublic File getDirectory()
null
if repository isn't local.public abstract String getIdentifier()
@NonNull public abstract ObjectDatabase getObjectDatabase()
@NonNull public ObjectInserter newObjectInserter()
getObjectDatabase()
.getObjectDatabase()
.@NonNull public ObjectReader newObjectReader()
getObjectDatabase()
.getObjectDatabase()
.@NonNull public abstract RefDatabase getRefDatabase()
@NonNull public abstract StoredConfig getConfig()
@NonNull public abstract AttributesNodeProvider createAttributesNodeProvider()
AttributesNodeProvider
.AttributesNodeProvider
.
This AttributesNodeProvider
is lazy loaded only once. It means that it will not be updated
after loading. Prefer creating new instance for each use.public FS getFS()
null
if
repository isn't local.@Deprecated public boolean hasObject(AnyObjectId objectId)
getObjectDatabase().has(objectId)
objectId
- a AnyObjectId
object.@NonNull public ObjectLoader open(AnyObjectId objectId) throws MissingObjectException, IOException
This is a one-shot call interface which may be faster than allocating a
newObjectReader()
to perform the lookup.
objectId
- identity of the object to open.ObjectLoader
for accessing the
object.MissingObjectException
- the object does not exist.IOException
- the object store cannot be accessed.@NonNull public ObjectLoader open(AnyObjectId objectId, int typeHint) throws MissingObjectException, IncorrectObjectTypeException, IOException
This is a one-shot call interface which may be faster than allocating a
newObjectReader()
to perform the lookup.
objectId
- identity of the object to open.typeHint
- hint about the type of object being requested, e.g.
Constants.OBJ_BLOB
;
ObjectReader.OBJ_ANY
if the
object type is not known, or does not matter to the caller.ObjectLoader
for accessing the
object.MissingObjectException
- the object does not exist.IncorrectObjectTypeException
- typeHint was not OBJ_ANY, and the object's actual type does
not match typeHint.IOException
- the object store cannot be accessed.@NonNull public RefUpdate updateRef(String ref) throws IOException
ref
- name of the ref the caller wants to modify.IOException
- a symbolic ref was passed in and could not be resolved back
to the base ref, as the symbolic ref could not be read.@NonNull public RefUpdate updateRef(String ref, boolean detach) throws IOException
ref
- name of the ref the caller wants to modify.detach
- true to create a detached headIOException
- a symbolic ref was passed in and could not be resolved back
to the base ref, as the symbolic ref could not be read.@NonNull public RefRename renameRef(String fromRef, String toRef) throws IOException
fromRef
- name of ref to rename fromtoRef
- name of ref to rename toIOException
- the rename could not be performed.@Nullable public ObjectId resolve(String revstr) throws AmbiguousObjectException, IncorrectObjectTypeException, RevisionSyntaxException, IOException
refs/heads
,
refs/tags
, or refs/remotes
namespaceABBREV
as an abbreviated SHA-1.id^1
id~3
is equivalent to id^1^1^1
or id^^^
.The following operators are specified by Git conventions, but are not supported by this method:
revstr
- A git object references expressionnull
if revstr can't be resolved to any
ObjectIdAmbiguousObjectException
- revstr
contains an abbreviated ObjectId and this
repository contains more than one object which match to the
input abbreviation.IncorrectObjectTypeException
- the id parsed does not meet the type required to finish
applying the operators in the expression.RevisionSyntaxException
- the expression is not supported by this implementation, or
does not meet the standard syntax.IOException
- on serious errors@Nullable public String simplify(String revstr) throws AmbiguousObjectException, IOException
resolve(String)
it will not
resolve a branch passed or resulting from the expression, such as @{-}.
Thus this method can be used to process an expression to a method that
expects a branch or revision id.revstr
- a String
object.null
if
given expression cannot be resolvedAmbiguousObjectException
IOException
public void incrementOpen()
close()
.public void close()
Decrement the use count, and maybe close resources.
close
in interface AutoCloseable
protected void doClose()
close()
.
The default implementation closes the object and ref databases.
@Nullable public String getFullBranch() throws IOException
HEAD
points to.
This is essentially the same as doing:
return exactRef(Constants.HEAD).getTarget().getName()Except when HEAD is detached, in which case this method returns the current ObjectId in hexadecimal string format.
refs/heads/master
),
an ObjectId in hex format if the current branch is detached, or
null
if the repository is corrupt and has no HEAD
reference.IOException
@Nullable public String getBranch() throws IOException
HEAD
points to.
This is essentially the same as getFullBranch()
, except the
leading prefix refs/heads/
is removed from the reference before
it is returned to the caller.
master
), an ObjectId
in hex format if the current branch is detached, or null
if the repository is corrupt and has no HEAD reference.IOException
@NonNull public Set<ObjectId> getAdditionalHaves()
getAllRefs()
.
When a repository borrows objects from another repository, it can advertise that it safely has that other repository's references, without exposing any other details about the other repository. This may help a client trying to push changes avoid pushing more than it needs to.
@Nullable public final Ref exactRef(String name) throws IOException
name
- the name of the ref to lookup. Must not be a short-hand
form; e.g., "master" is not automatically expanded to
"refs/heads/master".null
if it does not existIOException
@Nullable public final Ref findRef(String name) throws IOException
name
- the name of the ref to lookup. May be a short-hand form, e.g.
"master" which is automatically expanded to
"refs/heads/master" if "refs/heads/master" already exists.null
if it does not existIOException
@Deprecated @NonNull public Map<String,Ref> getAllRefs()
getRefDatabase().getRefs()
instead.@Deprecated @NonNull public Map<String,Ref> getTags()
getRefDatabase().getRefsByPrefix(R_TAGS)
instead@Deprecated @NonNull public Ref peel(Ref ref)
getRefDatabase().peel(ref)
instead.
If the ref cannot be peeled (as it does not refer to an annotated tag)
the peeled id stays null, but Ref.isPeeled()
will be true.
ref
- The ref to peelref
if ref.isPeeled()
is true; else a
new Ref object representing the same data as Ref, but isPeeled()
will be true and getPeeledObjectId will contain the peeled object
(or null).@NonNull public Map<AnyObjectId,Set<Ref>> getAllRefsByPeeledObjectId()
@NonNull public File getIndexFile() throws NoWorkTreeException
null
if repository isn't local.null
if repository isn't
local.NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.public RevCommit parseCommit(AnyObjectId id) throws IncorrectObjectTypeException, IOException, MissingObjectException
This method only returns successfully if the commit object exists, is verified to be a commit, and was parsed without error.
id
- name of the commit object.MissingObjectException
- the supplied commit does not exist.IncorrectObjectTypeException
- the supplied id is not a commit or an annotated tag.IOException
- a pack file or loose object could not be read.@NonNull public DirCache readDirCache() throws NoWorkTreeException, CorruptObjectException, IOException
The new index will be read before it is returned to the caller. Read failures are reported as exceptions and therefore prevent the method from returning a partially populated index.
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.IOException
- the index file is present but could not be read.CorruptObjectException
- the index file is using a format or extension that this
library does not support.@NonNull public DirCache lockDirCache() throws NoWorkTreeException, CorruptObjectException, IOException
The new index will be locked and then read before it is returned to the caller. Read failures are reported as exceptions and therefore prevent the method from returning a partially populated index.
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.IOException
- the index file is present but could not be read, or the lock
could not be obtained.CorruptObjectException
- the index file is using a format or extension that this
library does not support.@NonNull public RepositoryState getRepositoryState()
public static boolean isValidRefName(String refName)
refName
- a String
object.public static String normalizeBranchName(String name)
isValidRefName(String)
.
Future implementations of this method could be more restrictive or more lenient about the validity of specific characters in the returned name.
The current implementation returns the trimmed input string if this is
already a valid branch name. Otherwise it returns a trimmed string with
special characters not allowed by isValidRefName(String)
replaced by hyphens ('-') and blanks replaced by underscores ('_').
Leading and trailing slashes, dots, hyphens, and underscores are removed.
name
- to normalizenull
or
empty.isValidRefName(String)
@NonNull public static String stripWorkDir(File workDir, File file)
workDir
- Work dirfile
- File whose path shall be stripped of its workdirpublic boolean isBare()
@NonNull public File getWorkTree() throws NoWorkTreeException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.public abstract void scanForRepoChanges() throws IOException
IOException
public abstract void notifyIndexChanged(boolean internal)
internal
- true
if the index was changed by the same
JGit process@NonNull public static String shortenRefName(String refName)
refName
- a String
object.@Nullable public String shortenRemoteBranchName(String refName)
refName
- a String
object.refName
, i.e. without
the refs/remotes/<remote>
prefix, if
refName
represents a remote tracking branch;
otherwise null
.@Nullable public String getRemoteName(String refName)
refName
- a String
object.refName
, i.e. without the
refs/remotes/<remote>
prefix, if
refName
represents a remote tracking branch;
otherwise null
.@Nullable public String getGitwebDescription() throws IOException
GIT_DIR/description
file for gitweb.IOException
- description cannot be accessed.public void setGitwebDescription(@Nullable String description) throws IOException
GIT_DIR/description
file for gitweb.description
- new description; null to clear the description.IOException
- description cannot be persisted.@Nullable public abstract ReflogReader getReflogReader(String refName) throws IOException
refName
- a String
object.ReflogReader
for the supplied
refname, or null
if the named ref does not exist.IOException
- the ref could not be accessed.@Nullable public String readMergeCommitMsg() throws IOException, NoWorkTreeException
null
if this file doesn't existIOException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.public void writeMergeCommitMsg(String msg) throws IOException
null
is specified as message the file will
be deleted.msg
- the message which should be written or null
to
delete the fileIOException
@Nullable public String readCommitEditMsg() throws IOException, NoWorkTreeException
null
if this file doesn't existIOException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.public void writeCommitEditMsg(String msg) throws IOException
null
is specified as message the file will be deleted.msg
- the message which should be written or null
to delete
the fileIOException
@Nullable public List<ObjectId> readMergeHeads() throws IOException, NoWorkTreeException
null
if this file doesn't exist. Also if the file exists
but is empty null
will be returnedIOException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.public void writeMergeHeads(List<? extends ObjectId> heads) throws IOException
null
is specified as list of commits
the file will be deletedheads
- a list of commits which IDs should be written to
$GIT_DIR/MERGE_HEAD or null
to delete the fileIOException
@Nullable public ObjectId readCherryPickHead() throws IOException, NoWorkTreeException
null
if this file
doesn't exist. Also if the file exists but is empty null
will be returnedIOException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.@Nullable public ObjectId readRevertHead() throws IOException, NoWorkTreeException
null
if this file
doesn't exist. Also if the file exists but is empty null
will be returnedIOException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.public void writeCherryPickHead(ObjectId head) throws IOException
head
- an object id of the cherry commit or null
to
delete the fileIOException
public void writeRevertHead(ObjectId head) throws IOException
head
- an object id of the revert commit or null
to
delete the fileIOException
public void writeOrigHead(ObjectId head) throws IOException
head
- an object id of the original HEAD commit or null
to delete the fileIOException
@Nullable public ObjectId readOrigHead() throws IOException, NoWorkTreeException
null
if this file
doesn't exist. Also if the file exists but is empty null
will be returnedIOException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.@Nullable public String readSquashCommitMsg() throws IOException
null
if this file doesn't existIOException
NoWorkTreeException
- if this is bare, which implies it has no working directory.
See isBare()
.public void writeSquashCommitMsg(String msg) throws IOException
null
is specified as
message the file will be deleted.msg
- the message which should be written or null
to
delete the fileIOException
@NonNull public List<RebaseTodoLine> readRebaseTodo(String path, boolean includeComments) throws IOException
path
- path to the file relative to the repository's git-dir. E.g.
"rebase-merge/git-rebase-todo" or "rebase-append/done"includeComments
- true
if also comments should be reportedIOException
public void writeRebaseTodoFile(String path, List<RebaseTodoLine> steps, boolean append) throws IOException
path
- path to the file relative to the repository's git-dir. E.g.
"rebase-merge/git-rebase-todo" or "rebase-append/done"steps
- the steps to be writtenappend
- whether to append to an existing file or to write a new fileIOException
@NonNull public Set<String> getRemoteNames()
public void autoGC(ProgressMonitor monitor)
Currently this option is supported for repositories of type
FileRepository
only. See
GC.setAuto(boolean)
for
configuration details.
monitor
- to report progressCopyright © 2020 Eclipse JGit Project. All rights reserved.