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

public abstract Class Repository

extends Object
implements AutoCloseable
Class Inheritance
All Implemented Interfaces
java.lang.AutoCloseable
Known Direct Subclasses
org.eclipse.jgit.internal.storage.dfs.DfsRepository, org.eclipse.jgit.internal.storage.file.FileRepository
Static Imports
java.nio.charset.StandardCharsets.UTF_8, org.eclipse.jgit.lib.Constants.LOCK_SUFFIX

Represents a Git repository.

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 org.eclipse.jgit.lib.Repository very much depends on the concrete implementation. Applications working with a generic Repository type must not assume the instance is thread-safe.

Field Summary

Modifier and TypeField and Description
private final AtomicReference<Boolean>
pack-priv final AtomicLong
private static final Pattern
FORBIDDEN_BRANCH_NAME_COMPONENTS

Branch names containing slashes should not have a name component that is one of the reserved device names on Windows.

private final FS
fs

File abstraction used to resolve paths.

private final File
gitCommonDir

$GIT_COMMON_DIR: metadata directory holding the common repository's critical files.

private final File
gitDir

$GIT_DIR: metadata directory holding the repository's critical files.

private static final ListenerList
private final File
indexFile

If not bare, the index file caching the working file states.

private final String
private static final Logger
private final ListenerList
pack-priv final AtomicInteger
useCnt

Use counter

private final File
workTree

If not bare, the top level directory of the working files.

Constructor Summary

AccessConstructor and Description
protected
Repository(BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Repository>
options to configure the repository.
options
)

Initialize a new repository instance.

Method Summary

Modifier and TypeMethod and Description
public void
autoGC(ProgressMonitor
to report progress
monitor
)

Check whether any housekeeping is required; if yes, run garbage collection; if not, exit without performing any work.

public void
close()

Implements java.lang.AutoCloseable.close.

Closes this resource, relinquishing any underlying resources.

public void
create()

Create a new Git repository.

public abstract void
create(boolean
if true, a bare repository (a repository without a working directory) is created.
bare
)

Create a new Git repository initializing the necessary files and directories.

public abstract AttributesNodeProvider

Returns:

a new org.eclipse.jgit.attributes.AttributesNodeProvider. This org.eclipse.jgit.attributes.AttributesNodeProvider is lazy loaded only once. It means that it will not be updated after loading. Prefer creating new instance for each use.
createAttributesNodeProvider
()

Create a new org.eclipse.jgit.attributes.AttributesNodeProvider.

protected void
doClose()

Invoked when the use count drops to zero during close().

public final Ref

Returns:

the Ref with the given name, or null if it does not exist
exactRef
(String
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".
name
)

Get a ref by name.

public final Ref

Returns:

the Ref with the given name, or null if it does not exist
findRef
(String
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.
name
)

Search for a ref by (possibly abbreviated) name.

public void
fireEvent(RepositoryEvent<?>
the event to deliver.
event
)

Fire an event to all registered listeners.

public Set<ObjectId>

Returns:

unmodifiable collection of other known objects.
getAdditionalHaves
()

Objects known to exist but not expressed by getAllRefs().

public Map<String, Ref>

Returns:

mutable map of all known refs (heads, tags, remotes).
getAllRefs
()
Deprecated use getRefDatabase().getRefs() instead.

Get mutable map of all known refs, including symrefs like HEAD that may not point to any object yet.

public Map<AnyObjectId, Set<Ref>>

Returns:

a map with all objects referenced by a peeled ref.
getAllRefsByPeeledObjectId
()

Get a map with all objects referenced by a peeled ref.

public String

Returns:

name of current branch (for example master), an ObjectId in hex format if the current branch is detached, or null if the repository is corrupt and has no HEAD reference.
getBranch
()

Get the short name of the current branch that HEAD points to.

public File

Returns:

$GIT_COMMON_DIR: local common metadata directory;
getCommonDirectory
()

Get common dir.

public abstract StoredConfig

Returns:

the configuration of this repository.
getConfig
()

Get the configuration of this repository.

public File

Returns:

local metadata directory; null if repository isn't local.
getDirectory
()

Get local metadata directory

public FS

Returns:

the used file system abstraction, or null if repository isn't local.
getFS
()

Get the used file system abstraction.

public String

Returns:

name of current branch (for example 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.
getFullBranch
()

Get the name of the reference that HEAD points to.

public String

Returns:

description text; null if no description has been configured.
getGitwebDescription
()

Read the GIT_DIR/description file for gitweb.

public static ListenerList

Returns:

the global listener list observing all events in this JVM.
getGlobalListenerList
()

Get the global listener list observing all events in this JVM.

public abstract String

Returns:

repository identifier. The returned identifier has to be unique within a given Git server.
getIdentifier
()

Get repository identifier.

public File

Returns:

the index file location or null if repository isn't local.
getIndexFile
()

Get the index file location or null if repository isn't local.

protected String

Returns:

the initial branch name of a new repository
getInitialBranch
()

Get the initial branch name of a new repository

public ListenerList

Returns:

listeners observing only events on this repository.
getListenerList
()

Get listeners observing only events on this repository.

public abstract ObjectDatabase

Returns:

the object database which stores this repository's data.
getObjectDatabase
()

Get the object database which stores this repository's data.

public abstract RefDatabase

Returns:

the reference database which stores the reference namespace.
getRefDatabase
()

Get the reference database which stores the reference namespace.

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)
Deprecated since 7.2. use #getRefDatabase().getReflogReader(String) instead

Get the reflog reader

public ReflogReader

Returns:

a org.eclipse.jgit.lib.ReflogReader for the supplied ref.
getReflogReader
(Ref
a Ref
ref
)
Deprecated since 7.2. use #getRefDatabase().getReflogReader(Ref) instead

Get the reflog reader.

public String

Returns:

the remote name part of refName, i.e. without the refs/remotes/<remote> prefix, if refName represents a remote tracking branch; otherwise null.
getRemoteName
(String refName)

Get remote name

public Set<String>

Returns:

the names of all known remotes
getRemoteNames
()

Get the names of all known remotes

public RepositoryState

Returns:

the repository state
getRepositoryState
()

Get the repository state

public Map<String, Ref>

Returns:

mutable map of all tags; key is short tag name ("v1.0") and value of the entry contains the ref with the full tag name ("refs/tags/v1.0").
getTags
()
Deprecated use getRefDatabase().getRefsByPrefix(R_TAGS) instead

Get mutable map of all tags

public File

Returns:

the root directory of the working tree, where files are checked out for viewing and editing.
getWorkTree
()

Get the root directory of the working tree, where files are checked out for viewing and editing.

public void
incrementOpen()

Increment the use counter by one, requiring a matched close().

private static boolean
isAllHex(String str, int ptr)

public boolean

Returns:

true if this is bare, which implies it has no working directory.
isBare
()

Whether this repository is bare

private static boolean
isHex(char c)

public static boolean

Returns:

true if refName is a valid ref name
isValidRefName
(String refName)

Check validity of a ref name.

public boolean

Returns:

true if the work tree is case-insensitive; false otherwise
isWorkTreeCaseInsensitive
()

Tells whether the work tree is on a case-insensitive file system.

public DirCache

Returns:

a cache representing the contents of the specified index file (if it exists) or an empty cache if the file does not exist.
lockDirCache
()

Create a new in-core index representation, lock it, and read from disk.

public ObjectInserter

Returns:

a new inserter to create objects in getObjectDatabase().
newObjectInserter
()

Create a new inserter to create objects in getObjectDatabase().

public ObjectReader

Returns:

a new reader to read objects from getObjectDatabase().
newObjectReader
()

Create a new reader to read objects from getObjectDatabase().

public static String

Returns:

The normalized name or an empty String if it is null or empty.
normalizeBranchName
(String
to normalize
name
)

Normalizes the passed branch name into a possible valid branch name.

public abstract void
notifyIndexChanged(boolean
true if the index was changed by the same JGit process
internal
)

Notify that the index changed by firing an IndexChangedEvent.

public ObjectLoader

Returns:

a org.eclipse.jgit.lib.ObjectLoader for accessing the object.
open
(AnyObjectId
identity of the object to open.
objectId
)

Open an object from this repository.

public ObjectLoader

Returns:

a org.eclipse.jgit.lib.ObjectLoader for accessing the object.
open
(AnyObjectId
identity of the object to open.
objectId
,
int
hint about the type of object being requested, e.g. org.eclipse.jgit.lib.Constants#OBJ_BLOB; org.eclipse.jgit.lib.ObjectReader#OBJ_ANY if the object type is not known, or does not matter to the caller.
typeHint
)

Open an object from this repository.

public RevCommit

Returns:

reference to the commit object. Never null.
parseCommit
(AnyObjectId
name of the commit object.
id
)

Locate a reference to a commit and immediately parse its content.

private RevObject
private Ref

Returns:

ref 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).
peel
(Ref
The ref to peel
ref
)

Peel a possibly unpeeled reference to an annotated tag.

public ObjectId

Returns:

object id from CHERRY_PICK_HEAD file or null if this file doesn't exist. Also if the file exists but is empty null will be returned
readCherryPickHead
()

Return the information stored in the file $GIT_DIR/CHERRY_PICK_HEAD.

public String

Returns:

a String containing the content of the COMMIT_EDITMSG file or null if this file doesn't exist
readCommitEditMsg
()

Return the information stored in the file $GIT_DIR/COMMIT_EDITMSG.

private String
public DirCache

Returns:

a cache representing the contents of the specified index file (if it exists) or an empty cache if the file does not exist.
readDirCache
()

Create a new in-core index representation and read an index from disk.

private byte[]

Returns:

the raw contents or null if the file doesn't exist or is empty
readGitDirectoryFile
(String
the file to read
filename
)

Read a file from the git directory.

public String

Returns:

a String containing the content of the MERGE_MSG file or null if this file doesn't exist
readMergeCommitMsg
()

Return the information stored in the file $GIT_DIR/MERGE_MSG.

public List<ObjectId>

Returns:

a list of commits which IDs are listed in the MERGE_HEAD file or null if this file doesn't exist. Also if the file exists but is empty null will be returned
readMergeHeads
()

Return the information stored in the file $GIT_DIR/MERGE_HEAD.

public ObjectId

Returns:

object id from ORIG_HEAD file or null if this file doesn't exist. Also if the file exists but is empty null will be returned
readOrigHead
()

Return the information stored in the file $GIT_DIR/ORIG_HEAD.

public List<RebaseTodoLine>

Returns:

the list of steps
readRebaseTodo
(String
path to the file relative to the repository's git-dir. E.g. "rebase-merge/git-rebase-todo" or "rebase-append/done"
path
,
boolean
true if also comments should be reported
includeComments
)

Read a file formatted like the git-rebase-todo file.

public ObjectId

Returns:

object id from REVERT_HEAD file or null if this file doesn't exist. Also if the file exists but is empty null will be returned
readRevertHead
()

Return the information stored in the file $GIT_DIR/REVERT_HEAD.

public String

Returns:

a String containing the content of the SQUASH_MSG file or null if this file doesn't exist
readSquashCommitMsg
()

Return the information stored in the file $GIT_DIR/SQUASH_MSG.

public RefRename

Returns:

an update command that knows how to rename a branch to another.
renameRef
(String
name of ref to rename from
fromRef
,
String
name of ref to rename to
toRef
)

Create a command to rename a ref in this repository

public ObjectId

Returns:

an ObjectId or null if revstr can't be resolved to any ObjectId
resolve
(String
A git object references expression
revstr
)

Parse a git revision string and return an object id. Combinations of these operators are supported:

  • HEAD, MERGE_HEAD, FETCH_HEAD
  • SHA-1: a complete or abbreviated SHA-1
  • refs/...: a complete reference name
  • short-name: a short reference name under refs/heads, refs/tags, or refs/remotes namespace
  • tag-NN-gABBREV: output from describe, parsed by treating ABBREV as an abbreviated SHA-1.
  • id^: first parent of commit id, this is the same as id^1
  • id^0: ensure id is a commit
  • id^n: n-th parent of commit id
  • id~n: n-th historical ancestor of id, by first parent. id~3 is equivalent to id^1^1^1 or id^^^.
  • id:path: Lookup path under tree named by id
  • id^{commit}: ensure id is a commit
  • id^{tree}: ensure id is a tree
  • id^{tag}: ensure id is a tag
  • id^{blob}: ensure id is a blob
private Object
resolve(RevWalk rw, String revstr)

private ObjectId
private RevCommit
private String
resolveReflogCheckout(int checkoutNo)

private ObjectId
public abstract void
scanForRepoChanges()

Force a scan for changed refs.

public void
setGitwebDescription(String
new description; null to clear the description.
description
)

Set the GIT_DIR/description file for gitweb.

public static String

Returns:

a more user friendly ref name
shortenRefName
(String refName)

Get a shortened more user friendly ref name

public String

Returns:

the remote branch name part of refName, i.e. without the refs/remotes/<remote> prefix, if refName represents a remote tracking branch; otherwise null.
shortenRemoteBranchName
(String refName)

Get a shortened more user friendly remote tracking branch name

public String

Returns:

object id or ref name from resolved expression or null if given expression cannot be resolved
simplify
(String revstr)

Simplify an expression, but unlike resolve(String) it will not resolve a branch passed or resulting from the expression, such as @{-}.

public static String

Returns:

normalized repository relative path or the empty string if the file is not relative to the work directory.
stripWorkDir
(File
Work dir
workDir
,
File
File whose path shall be stripped of its workdir
file
)

Strip work dir and return normalized repository path.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public RefUpdate

Returns:

an update command. The caller must finish populating this command and then invoke one of the update methods to actually make a change.
updateRef
(String
name of the ref the caller wants to modify.
ref
)

Create a command to update, create or delete a ref in this repository.

public RefUpdate

Returns:

an update command. The caller must finish populating this command and then invoke one of the update methods to actually make a change.
updateRef
(String
name of the ref the caller wants to modify.
ref
,
boolean
true to create a detached head
detach
)

Create a command to update, create or delete a ref in this repository.

public void
writeCherryPickHead(ObjectId
an object id of the cherry commit or null to delete the file
head
)

Write cherry pick commit into $GIT_DIR/CHERRY_PICK_HEAD.

public void
writeCommitEditMsg(String
the message which should be written or null to delete the file
msg
)

Write new content to the file $GIT_DIR/COMMIT_EDITMSG.

private void
writeCommitMsg(File msgFile, String msg)

private void
writeHeadsFile(List<? extends ObjectId>
a list of object ids to write or null if the file should be deleted.
heads
,
String
name of the file to write heads to
filename
)

Write the given heads to a file in the git directory.

public void
writeMergeCommitMsg(String
the message which should be written or null to delete the file
msg
)

Write new content to the file $GIT_DIR/MERGE_MSG.

public void
writeMergeHeads(List<? extends ObjectId>
a list of commits which IDs should be written to $GIT_DIR/MERGE_HEAD or null to delete the file
heads
)

Write new merge-heads into $GIT_DIR/MERGE_HEAD.

public void
writeOrigHead(ObjectId
an object id of the original HEAD commit or null to delete the file
head
)

Write original HEAD commit into $GIT_DIR/ORIG_HEAD.

public void
writeRebaseTodoFile(String
path to the file relative to the repository's git-dir. E.g. "rebase-merge/git-rebase-todo" or "rebase-append/done"
path
,
List<RebaseTodoLine>
the steps to be written
steps
,
boolean
whether to append to an existing file or to write a new file
append
)

Write a file formatted like a git-rebase-todo file.

public void
writeRevertHead(ObjectId
an object id of the revert commit or null to delete the file
head
)

Write revert commit into $GIT_DIR/REVERT_HEAD.

public void
writeSquashCommitMsg(String
the message which should be written or null to delete the file
msg
)

Write new content to the file $GIT_DIR/SQUASH_MSG.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait