Class FileRepository

java.lang.Object
org.eclipse.jgit.lib.Repository
org.eclipse.jgit.internal.storage.file.FileRepository
All Implemented Interfaces:
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.

  • Field Details

  • Constructor Details

    • FileRepository

      public FileRepository(File gitDir) throws IOException
      Construct 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:
      IOException - the repository appears to already exist but cannot be accessed.
      See Also:
    • FileRepository

      public FileRepository(String gitDir) throws IOException
      A convenience API for FileRepository(File).
      Parameters:
      gitDir - GIT_DIR (the location of the repository metadata).
      Throws:
      IOException - the repository appears to already exist but cannot be accessed.
      See Also:
    • FileRepository

      public FileRepository(BaseRepositoryBuilder options) throws IOException
      Create a repository using the local file system.
      Parameters:
      options - description of the repository's important paths.
      Throws:
      IOException - the user configuration file or repository configuration file cannot be accessed.
  • Method Details

    • loadRepoConfig

      private void loadRepoConfig() throws IOException
      Throws:
      IOException
    • create

      public void create(boolean bare) throws IOException
      Create a new Git repository initializing the necessary files and directories.

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

      Specified by:
      create in class Repository
      Parameters:
      bare - if true, a bare repository (a repository without a working directory) is created.
      Throws:
      IOException - in case of IO problem
    • getObjectsDirectory

      public 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:
      getObjectDatabase in class Repository
      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:
      getRefDatabase in class Repository
      Returns:
      the reference database which stores the reference namespace.
    • getIdentifier

      public String getIdentifier()
      Get repository identifier.
      Specified by:
      getIdentifier in class Repository
      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:
      getConfig in class Repository
      Returns:
      the configuration of this repository.
    • getGitwebDescription

      @Nullable public String getGitwebDescription() throws IOException
      Read the GIT_DIR/description file for gitweb.
      Overrides:
      getGitwebDescription in class Repository
      Returns:
      description text; null if no description has been configured.
      Throws:
      IOException - description cannot be accessed.
    • setGitwebDescription

      public void setGitwebDescription(@Nullable String description) throws IOException
      Set the GIT_DIR/description file for gitweb.
      Overrides:
      setGitwebDescription in class Repository
      Parameters:
      description - new description; null to clear the description.
      Throws:
      IOException - description cannot be persisted.
    • descriptionFile

      private File descriptionFile()
    • getAdditionalHaves

      public Set<ObjectId> getAdditionalHaves()
      Objects known to exist but not expressed by Repository.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:
      getAdditionalHaves in class Repository
      Returns:
      unmodifiable collection of other known objects.
    • getAdditionalHaves

      private Set<ObjectId> getAdditionalHaves(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(File pack) throws IOException
      Add a single existing pack to the list of available pack files.
      Parameters:
      pack - path of the pack file to open.
      Throws:
      IOException - index file could not be opened, read, or is not recognized as a Git pack file index.
    • scanForRepoChanges

      public void scanForRepoChanges() throws IOException
      Force a scan for changed refs. Fires an IndexChangedEvent(false) if changes are detected.
      Specified by:
      scanForRepoChanges in class Repository
      Throws:
      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:
      notifyIndexChanged in class Repository
      Parameters:
      internal - true if the index was changed by the same JGit process
    • getReflogReader

      public ReflogReader getReflogReader(String refName) throws IOException
      Get the reflog reader
      Specified by:
      getReflogReader in class Repository
      Parameters:
      refName - a String object.
      Returns:
      a ReflogReader for the supplied refname, or null if the named ref does not exist.
      Throws:
      IOException - the ref could not be accessed.
    • createAttributesNodeProvider

      public AttributesNodeProvider createAttributesNodeProvider()
      Specified by:
      createAttributesNodeProvider in class Repository
      Returns:
      a new 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.
    • 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 FileRepository only. See GC.setAuto(boolean) for configuration details.

      Overrides:
      autoGC in class Repository
      Parameters:
      monitor - to report progress
    • convertToPackedRefs

      void convertToPackedRefs(boolean writeLogs, boolean backup) throws IOException
      Converts the RefDatabase from reftable to RefDirectory. This operation is not atomic.
      Parameters:
      writeLogs - whether to write reflogs
      backup - whether to rename or delete the old storage files. If set to true, the reftable list is left in refs.old, and the reftable/ dir is left alone. If set to false, the reftable/ dir is removed, and refs file is removed.
      Throws:
      IOException - on IO problem
    • convertToReftable

      void convertToReftable(boolean writeLogs, boolean backup) throws IOException
      Converts the RefDatabase from RefDirectory to reftable. This operation is not atomic.
      Parameters:
      writeLogs - whether to write reflogs
      backup - whether to rename or delete the old storage files. If set to true, the loose refs are left in refs.old, the packed-refs in packed-refs.old and reflogs in refs.old/. HEAD is left in HEAD.old and also .log is appended to additional refs. If set to false, the refs/ and logs/ directories and HEAD and additional symbolic refs are removed.
      Throws:
      IOException - on IO problem
    • convertRefStorage

      public void convertRefStorage(String format, boolean writeLogs, boolean backup) throws IOException
      Converts between ref storage formats.
      Parameters:
      format - the format to convert to, either "reftable" or "refdir"
      writeLogs - whether to write reflogs
      backup - whether to make a backup of the old data
      Throws:
      IOException - on I/O problems.