Class Git

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class Git
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Offers a "GitPorcelain"-like API to interact with a git repository.

    The GitPorcelain commands are described in the Git Documentation.

    This class only offers methods to construct so-called command classes. Each GitPorcelain command is represented by one command class.
    Example: this class offers a commit() method returning an instance of the CommitCommand class. The CommitCommand class has setters for all the arguments and options. The CommitCommand class also has a call method to actually execute the commit. The following code show's how to do a simple commit:

     Git git = new Git(myRepo);
     git.commit().setMessage("Fix393").setAuthor(developerIdent).call();
     
    All mandatory parameters for commands have to be specified in the methods of this class, the optional parameters have to be specified by the setter-methods of the Command class.

    This class is intended to be used internally (e.g. by JGit tests) or by external components (EGit, third-party tools) when they need exactly the functionality of a GitPorcelain command. There are use-cases where this class is not optimal and where you should use the more low-level JGit classes. The methods in this class may for example offer too much functionality or they offer the functionality with the wrong arguments.

    • Field Detail

      • repo

        private final Repository repo
        The git repository this class is interacting with
      • closeRepo

        private final boolean closeRepo
    • Constructor Detail

      • Git

        public Git​(Repository repo)
        Construct a new Git object which can interact with the specified git repository.

        All command classes returned by methods of this class will always interact with this git repository.

        The caller is responsible for closing the repository; close() on this instance does not close the repo.

        Parameters:
        repo - the git repository this class is interacting with; null is not allowed.
      • Git

        Git​(Repository repo,
            boolean closeRepo)
    • Method Detail

      • open

        public static Git open​(java.io.File dir)
                        throws java.io.IOException
        Open repository
        Parameters:
        dir - the repository to open. May be either the GIT_DIR, or the working tree directory that contains .git.
        Returns:
        a Git object for the existing git repository
        Throws:
        java.io.IOException
      • open

        public static Git open​(java.io.File dir,
                               FS fs)
                        throws java.io.IOException
        Open repository
        Parameters:
        dir - the repository to open. May be either the GIT_DIR, or the working tree directory that contains .git.
        fs - filesystem abstraction to use when accessing the repository.
        Returns:
        a Git object for the existing git repository. Closing this instance will close the repo.
        Throws:
        java.io.IOException
      • wrap

        public static Git wrap​(Repository repo)
        Wrap repository
        Parameters:
        repo - the git repository this class is interacting with; null is not allowed.
        Returns:
        a Git object for the existing git repository. The caller is responsible for closing the repository; close() on this instance does not close the repo.
      • close

        public void close()

        Free resources associated with this instance.

        If the repository was opened by a static factory method in this class, then this method calls Repository.close() on the underlying repository instance. (Whether this actually releases underlying resources, such as file handles, may vary; see Repository for more details.)

        If the repository was created by a caller and passed into Git(Repository) or a static factory method in this class, then this method does not call close on the underlying repository.

        In all cases, after calling this method you should not use this Git instance anymore.

        Specified by:
        close in interface java.lang.AutoCloseable
        Since:
        3.2
      • lsRemoteRepository

        public static LsRemoteCommand lsRemoteRepository()
        Return a command to list remote branches/tags without a local repository.
        Returns:
        a LsRemoteCommand
        Since:
        3.1
      • shutdown

        public static void shutdown()
        Shutdown JGit and release resources it holds like NLS and thread pools
        Since:
        5.8
      • submoduleAdd

        public SubmoduleAddCommand submoduleAdd()
        Return a command object to execute a submodule add command
        Returns:
        a SubmoduleAddCommand used to add a new submodule to a parent repository
      • submoduleInit

        public SubmoduleInitCommand submoduleInit()
        Return a command object to execute a submodule init command
        Returns:
        a SubmoduleInitCommand used to initialize the repository's config with settings from the .gitmodules file in the working tree
      • submoduleDeinit

        public SubmoduleDeinitCommand submoduleDeinit()
        Returns a command object to execute a submodule deinit command
        Returns:
        a SubmoduleDeinitCommand used to remove a submodule's working tree manifestation
        Since:
        4.10
      • submoduleStatus

        public SubmoduleStatusCommand submoduleStatus()
        Returns a command object to execute a submodule status command
        Returns:
        a SubmoduleStatusCommand used to report the status of a repository's configured submodules
      • submoduleSync

        public SubmoduleSyncCommand submoduleSync()
        Return a command object to execute a submodule sync command
        Returns:
        a SubmoduleSyncCommand used to update the URL of a submodule from the parent repository's .gitmodules file
      • submoduleUpdate

        public SubmoduleUpdateCommand submoduleUpdate()
        Return a command object to execute a submodule update command
        Returns:
        a SubmoduleUpdateCommand used to update the submodules in a repository to the configured revision
      • nameRev

        public NameRevCommand nameRev()
        Return a command object to find human-readable names of revisions.
        Returns:
        a NameRevCommand.
        Since:
        3.0
      • describe

        public DescribeCommand describe()
        Return a command object to come up with a short name that describes a commit in terms of the nearest git tag.
        Returns:
        a DescribeCommand.
        Since:
        3.2
      • getRepository

        public Repository getRepository()
        Get repository
        Returns:
        the git repository this class is interacting with; see close() for notes on closing this repository.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object