Class FileRepository
- java.lang.Object
-
- org.eclipse.jgit.lib.Repository
-
- org.eclipse.jgit.internal.storage.file.FileRepository
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class FileRepository extends Repository
Represents a Git repository. A repository holds all objects and refs used for managing source code (could by any type of file, but source code is what SCM's are typically used for). In Git terms all data is stored in GIT_DIR, typically a directory called .git. A work tree is maintained unless the repository is a bare repository. Typically the .git directory is located at the root of the work dir.- GIT_DIR
- objects/ - objects
- refs/ - tags and heads
- config - configuration
- info/ - more configurations
This class is thread-safe.
This implementation only handles a subtly undocumented subset of git features.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classFileRepository.AttributesNodeProviderImplImplementation aAttributesNodeProviderfor aFileRepository.
-
Field Summary
Fields Modifier and Type Field Description private static org.slf4j.LoggerLOGprivate ObjectDirectoryobjectDatabaseprivate RefDatabaserefsprivate FileBasedConfigrepoConfigprivate FileSnapshotsnapshotprivate java.lang.ObjectsnapshotLockprivate static java.lang.StringUNNAMED
-
Constructor Summary
Constructors Constructor Description FileRepository(java.io.File gitDir)Construct a representation of a Git repository.FileRepository(java.lang.String gitDir)A convenience API forFileRepository(File).FileRepository(BaseRepositoryBuilder options)Create a repository using the local file system.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidautoGC(ProgressMonitor monitor)Check whether any housekeeping is required; if yes, run garbage collection; if not, exit without performing any work.voidconvertRefStorage(java.lang.String format, boolean writeLogs, boolean backup)Converts between ref storage formats.(package private) voidconvertToPackedRefs(boolean writeLogs, boolean backup)Converts the RefDatabase from reftable to RefDirectory.(package private) voidconvertToReftable(boolean writeLogs, boolean backup)Converts the RefDatabase from RefDirectory to reftable.voidcreate(boolean bare)Create a new Git repository initializing the necessary files and directories.AttributesNodeProvidercreateAttributesNodeProvider()Create a newAttributesNodeProvider.private java.io.FiledescriptionFile()private voiddetectIndexChanges()Detect index changes.java.util.Set<ObjectId>getAdditionalHaves()Objects known to exist but not expressed byRepository.getAllRefs().private java.util.Set<ObjectId>getAdditionalHaves(java.util.Set<ObjectDirectory.AlternateHandle.Id> skips)Objects known to exist but not expressed by#getAllRefs().FileBasedConfiggetConfig()Get the configuration of this repository.java.lang.StringgetGitwebDescription()Read theGIT_DIR/descriptionfile for gitweb.java.lang.StringgetIdentifier()Get repository identifier.ObjectDirectorygetObjectDatabase()Get the object database which stores this repository's data.java.io.FilegetObjectsDirectory()Get the directory containing the objects owned by this repositoryRefDatabasegetRefDatabase()Get the reference database which stores the reference namespace.ReflogReadergetReflogReader(java.lang.String refName)Get the reflog readerprivate voidloadRepoConfig()voidnotifyIndexChanged(boolean internal)Notify that the index changed by firing an IndexChangedEvent.voidopenPack(java.io.File pack)Add a single existing pack to the list of available pack files.voidscanForRepoChanges()Force a scan for changed refs.voidsetGitwebDescription(java.lang.String description)Set theGIT_DIR/descriptionfile for gitweb.private booleanshouldAutoDetach()-
Methods inherited from class org.eclipse.jgit.lib.Repository
close, create, doClose, exactRef, findRef, fireEvent, getAllRefs, getAllRefsByPeeledObjectId, getBranch, getDirectory, getFS, getFullBranch, getGlobalListenerList, getIndexFile, getInitialBranch, getListenerList, getRemoteName, getRemoteNames, getRepositoryState, getTags, getWorkTree, hasObject, incrementOpen, isBare, isValidRefName, lockDirCache, newObjectInserter, newObjectReader, normalizeBranchName, open, open, parseCommit, peel, readCherryPickHead, readCommitEditMsg, readDirCache, readMergeCommitMsg, readMergeHeads, readOrigHead, readRebaseTodo, readRevertHead, readSquashCommitMsg, renameRef, resolve, shortenRefName, shortenRemoteBranchName, simplify, stripWorkDir, toString, updateRef, updateRef, writeCherryPickHead, writeCommitEditMsg, writeMergeCommitMsg, writeMergeHeads, writeOrigHead, writeRebaseTodoFile, writeRevertHead, writeSquashCommitMsg
-
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
UNNAMED
private static final java.lang.String UNNAMED
- See Also:
- Constant Field Values
-
repoConfig
private final FileBasedConfig repoConfig
-
refs
private RefDatabase refs
-
objectDatabase
private final ObjectDirectory objectDatabase
-
snapshotLock
private final java.lang.Object snapshotLock
-
snapshot
private FileSnapshot snapshot
-
-
Constructor Detail
-
FileRepository
public FileRepository(java.io.File gitDir) throws java.io.IOExceptionConstruct a representation of a Git repository.The work tree, object directory, alternate object directories and index file locations are deduced from the given git directory and the default rules by running
FileRepositoryBuilder. This constructor is the same as saying:new FileRepositoryBuilder().setGitDir(gitDir).build()
- Parameters:
gitDir- GIT_DIR (the location of the repository metadata).- Throws:
java.io.IOException- the repository appears to already exist but cannot be accessed.- See Also:
FileRepositoryBuilder
-
FileRepository
public FileRepository(java.lang.String gitDir) throws java.io.IOExceptionA convenience API forFileRepository(File).- Parameters:
gitDir- GIT_DIR (the location of the repository metadata).- Throws:
java.io.IOException- the repository appears to already exist but cannot be accessed.- See Also:
FileRepositoryBuilder
-
FileRepository
public FileRepository(BaseRepositoryBuilder options) throws java.io.IOException
Create a repository using the local file system.- Parameters:
options- description of the repository's important paths.- Throws:
java.io.IOException- the user configuration file or repository configuration file cannot be accessed.
-
-
Method Detail
-
loadRepoConfig
private void loadRepoConfig() throws java.io.IOException- Throws:
java.io.IOException
-
create
public void create(boolean bare) throws java.io.IOExceptionCreate a new Git repository initializing the necessary files and directories.Create a new Git repository initializing the necessary files and directories.
- Specified by:
createin classRepository- Parameters:
bare- if true, a bare repository (a repository without a working directory) is created.- Throws:
java.io.IOException- in case of IO problem
-
getObjectsDirectory
public java.io.File getObjectsDirectory()
Get the directory containing the objects owned by this repository- Returns:
- the directory containing the objects owned by this repository.
-
getObjectDatabase
public ObjectDirectory getObjectDatabase()
Get the object database which stores this repository's data.- Specified by:
getObjectDatabasein classRepository- Returns:
- the object database which stores this repository's data.
-
getRefDatabase
public RefDatabase getRefDatabase()
Get the reference database which stores the reference namespace.- Specified by:
getRefDatabasein classRepository- Returns:
- the reference database which stores the reference namespace.
-
getIdentifier
public java.lang.String getIdentifier()
Get repository identifier.- Specified by:
getIdentifierin classRepository- Returns:
- repository identifier. The returned identifier has to be unique within a given Git server.
-
getConfig
public FileBasedConfig getConfig()
Get the configuration of this repository.- Specified by:
getConfigin classRepository- Returns:
- the configuration of this repository.
-
getGitwebDescription
@Nullable public java.lang.String getGitwebDescription() throws java.io.IOException
Read theGIT_DIR/descriptionfile for gitweb.- Overrides:
getGitwebDescriptionin classRepository- Returns:
- description text; null if no description has been configured.
- Throws:
java.io.IOException- description cannot be accessed.
-
setGitwebDescription
public void setGitwebDescription(@Nullable java.lang.String description) throws java.io.IOException
Set theGIT_DIR/descriptionfile for gitweb.- Overrides:
setGitwebDescriptionin classRepository- Parameters:
description- new description; null to clear the description.- Throws:
java.io.IOException- description cannot be persisted.
-
descriptionFile
private java.io.File descriptionFile()
-
getAdditionalHaves
public java.util.Set<ObjectId> getAdditionalHaves()
Objects known to exist but not expressed byRepository.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.
Objects known to exist but not expressed by
#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.
- Overrides:
getAdditionalHavesin classRepository- Returns:
- unmodifiable collection of other known objects.
-
getAdditionalHaves
private java.util.Set<ObjectId> getAdditionalHaves(java.util.Set<ObjectDirectory.AlternateHandle.Id> skips)
Objects known to exist but not expressed by#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.
- Parameters:
skips- Set of AlternateHandle Ids already seen- Returns:
- unmodifiable collection of other known objects.
-
openPack
public void openPack(java.io.File pack) throws java.io.IOExceptionAdd a single existing pack to the list of available pack files.- Parameters:
pack- path of the pack file to open.- Throws:
java.io.IOException- index file could not be opened, read, or is not recognized as a Git pack file index.
-
scanForRepoChanges
public void scanForRepoChanges() throws java.io.IOExceptionForce a scan for changed refs. Fires an IndexChangedEvent(false) if changes are detected.- Specified by:
scanForRepoChangesin classRepository- Throws:
java.io.IOException
-
detectIndexChanges
private void detectIndexChanges()
Detect index changes.
-
notifyIndexChanged
public void notifyIndexChanged(boolean internal)
Notify that the index changed by firing an IndexChangedEvent.- Specified by:
notifyIndexChangedin classRepository- Parameters:
internal-trueif the index was changed by the same JGit process
-
getReflogReader
public ReflogReader getReflogReader(java.lang.String refName) throws java.io.IOException
Get the reflog reader- Specified by:
getReflogReaderin classRepository- Parameters:
refName- aStringobject.- Returns:
- a
ReflogReaderfor the supplied refname, ornullif the named ref does not exist. - Throws:
java.io.IOException- the ref could not be accessed.
-
createAttributesNodeProvider
public AttributesNodeProvider createAttributesNodeProvider()
Create a newAttributesNodeProvider.- Specified by:
createAttributesNodeProviderin classRepository- Returns:
- a new
AttributesNodeProvider. ThisAttributesNodeProvideris lazy loaded only once. It means that it will not be updated after loading. Prefer creating new instance for each use.
-
shouldAutoDetach
private boolean shouldAutoDetach()
-
autoGC
public void autoGC(ProgressMonitor monitor)
Check whether any housekeeping is required; if yes, run garbage collection; if not, exit without performing any work. Some JGit commands run autoGC after performing operations that could create many loose objects.Currently this option is supported for repositories of type
FileRepositoryonly. SeeGC.setAuto(boolean)for configuration details.- Overrides:
autoGCin classRepository- Parameters:
monitor- to report progress
-
convertToPackedRefs
void convertToPackedRefs(boolean writeLogs, boolean backup) throws java.io.IOExceptionConverts the RefDatabase from reftable to RefDirectory. This operation is not atomic.- Parameters:
writeLogs- whether to write reflogsbackup- whether to rename or delete the old storage files. If set totrue, the reftable list is left inrefs.old, and thereftable/dir is left alone. If set tofalse, thereftable/dir is removed, andrefsfile is removed.- Throws:
java.io.IOException- on IO problem
-
convertToReftable
void convertToReftable(boolean writeLogs, boolean backup) throws java.io.IOExceptionConverts the RefDatabase from RefDirectory to reftable. This operation is not atomic.- Parameters:
writeLogs- whether to write reflogsbackup- whether to rename or delete the old storage files. If set totrue, the loose refs are left inrefs.old, the packed-refs inpacked-refs.oldand reflogs inrefs.old/. HEAD is left inHEAD.oldand also.logis appended to additional refs. If set tofalse, therefs/andlogs/directories andHEADand additional symbolic refs are removed.- Throws:
java.io.IOException- on IO problem
-
convertRefStorage
public void convertRefStorage(java.lang.String format, boolean writeLogs, boolean backup) throws java.io.IOExceptionConverts between ref storage formats.- Parameters:
format- the format to convert to, either "reftable" or "refdir"writeLogs- whether to write reflogsbackup- whether to make a backup of the old data- Throws:
java.io.IOException- on I/O problems.
-
-