Class CommitCommand

java.lang.Object
org.eclipse.jgit.api.GitCommand<RevCommit>
org.eclipse.jgit.api.CommitCommand
All Implemented Interfaces:
Callable<RevCommit>

public class CommitCommand extends GitCommand<RevCommit>
A class used to execute a Commit command. It has setters for all supported options and arguments of this command and a call() method to finally execute the command.
See Also:
  • Field Details

    • author

      private PersonIdent author
    • committer

      private PersonIdent committer
    • message

      private String message
    • all

      private boolean all
    • only

      private List<String> only
    • onlyProcessed

      private boolean[] onlyProcessed
    • amend

      private boolean amend
    • insertChangeId

      private boolean insertChangeId
    • parents

      private List<ObjectId> parents
      parents this commit should have. The current HEAD will be in this list and also all commits mentioned in .git/MERGE_HEAD
    • reflogComment

      private String reflogComment
    • useDefaultReflogMessage

      private boolean useDefaultReflogMessage
    • noVerify

      private boolean noVerify
      Setting this option bypasses the pre-commit and commit-msg hooks.
    • hookOutRedirect

      private HashMap<String,PrintStream> hookOutRedirect
    • hookErrRedirect

      private HashMap<String,PrintStream> hookErrRedirect
    • allowEmpty

      private Boolean allowEmpty
    • signCommit

      private Boolean signCommit
    • signingKey

      private String signingKey
    • gpgSigner

      private GpgSigner gpgSigner
    • gpgConfig

      private GpgConfig gpgConfig
    • credentialsProvider

      private CredentialsProvider credentialsProvider
  • Constructor Details

    • CommitCommand

      protected CommitCommand(Repository repo)
      Constructor for CommitCommand
      Parameters:
      repo - the Repository
  • Method Details

    • call

      Execute the command

      Executes the commit command with all the options and parameters collected by the setter methods of this class. Each instance of this class should only be used for one invocation of the command (means: one call to call())

      Specified by:
      call in interface Callable<RevCommit>
      Specified by:
      call in class GitCommand<RevCommit>
      Throws:
      ServiceUnavailableException - if signing service is not available e.g. since it isn't installed
      GitAPIException
      AbortedByHookException
      ConcurrentRefUpdateException
      NoHeadException
      NoMessageException
      UnmergedPathsException
      WrongRepositoryStateException
    • insertChangeId

      private void insertChangeId(ObjectId treeId)
    • createTemporaryIndex

      private DirCache createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw) throws IOException
      Throws:
      IOException
    • lookupOnly

      private int lookupOnly(String pathString)
      Look an entry's path up in the list of paths specified by the --only/ -o option In case the complete (file) path (e.g. "d1/d2/f1") cannot be found in only, lookup is also tried with (parent) directory paths (e.g. "d1/d2" and "d1").
      Parameters:
      pathString - entry's path
      Returns:
      the item's index in only; -1 if no item matches
    • processOptions

      private void processOptions(RepositoryState state, RevWalk rw) throws NoMessageException, UnsupportedSigningFormatException
      Sets default values for not explicitly specified options. Then validates that all required data has been provided.
      Parameters:
      state - the state of the repository we are working on
      rw - the RevWalk to use
      Throws:
      NoMessageException - if the commit message has not been specified
      UnsupportedSigningFormatException - if the configured gpg.format is not supported
    • isMergeDuringRebase

      private boolean isMergeDuringRebase(RepositoryState state)
    • setMessage

      public CommitCommand setMessage(String message)
      Set the commit message
      Parameters:
      message - the commit message used for the commit
      Returns:
      this
    • setAllowEmpty

      public CommitCommand setAllowEmpty(boolean allowEmpty)
      Set whether to allow to create an empty commit
      Parameters:
      allowEmpty - whether it should be allowed to create a commit which has the same tree as it's sole predecessor (a commit which doesn't change anything). By default when creating standard commits (without specifying paths) JGit allows to create such commits. When this flag is set to false an attempt to create an "empty" standard commit will lead to an EmptyCommitException.

      By default when creating a commit containing only specified paths an attempt to create an empty commit leads to a JGitInternalException. By setting this flag to true this exception will not be thrown.

      Returns:
      this
      Since:
      4.2
    • getMessage

      public String getMessage()
      Get the commit message
      Returns:
      the commit message used for the commit
    • setCommitter

      public CommitCommand setCommitter(PersonIdent committer)
      Sets the committer for this commit. If no committer is explicitly specified because this method is never called or called with null value then the committer will be deduced from config info in repository, with current time.
      Parameters:
      committer - the committer used for the commit
      Returns:
      this
    • setCommitter

      public CommitCommand setCommitter(String name, String email)
      Sets the committer for this commit. If no committer is explicitly specified because this method is never called then the committer will be deduced from config info in repository, with current time.
      Parameters:
      name - the name of the committer used for the commit
      email - the email of the committer used for the commit
      Returns:
      this
    • getCommitter

      public PersonIdent getCommitter()
      Get the committer
      Returns:
      the committer used for the commit. If no committer was specified null is returned and the default PersonIdent of this repo is used during execution of the command
    • setAuthor

      public CommitCommand setAuthor(PersonIdent author)
      Sets the author for this commit. If no author is explicitly specified because this method is never called or called with null value then the author will be set to the committer or to the original author when amending.
      Parameters:
      author - the author used for the commit
      Returns:
      this
    • setAuthor

      public CommitCommand setAuthor(String name, String email)
      Sets the author for this commit. If no author is explicitly specified because this method is never called then the author will be set to the committer or to the original author when amending.
      Parameters:
      name - the name of the author used for the commit
      email - the email of the author used for the commit
      Returns:
      this
    • getAuthor

      public PersonIdent getAuthor()
      Get the author
      Returns:
      the author used for the commit. If no author was specified null is returned and the default PersonIdent of this repo is used during execution of the command
    • setAll

      public CommitCommand setAll(boolean all)
      If set to true the Commit command automatically stages files that have been modified and deleted, but new files not known by the repository are not affected. This corresponds to the parameter -a on the command line.
      Parameters:
      all - whether to auto-stage all files that have been modified and deleted
      Returns:
      this
      Throws:
      JGitInternalException - in case of an illegal combination of arguments/ options
    • setAmend

      public CommitCommand setAmend(boolean amend)
      Used to amend the tip of the current branch. If set to true, the previous commit will be amended. This is equivalent to --amend on the command line.
      Parameters:
      amend - whether to ammend the tip of the current branch
      Returns:
      this
    • setOnly

      public CommitCommand setOnly(String only)
      Commit dedicated path only.

      This method can be called several times to add multiple paths. Full file paths are supported as well as directory paths; in the latter case this commits all files/directories below the specified path.

      Parameters:
      only - path to commit (with / as separator)
      Returns:
      this
    • setInsertChangeId

      public CommitCommand setInsertChangeId(boolean insertChangeId)
      If set to true a change id will be inserted into the commit message An existing change id is not replaced. An initial change id (I000...) will be replaced by the change id.
      Parameters:
      insertChangeId - whether to insert a change id
      Returns:
      this
    • setReflogComment

      public CommitCommand setReflogComment(String reflogComment)
      Override the message written to the reflog
      Parameters:
      reflogComment - the comment to be written into the reflog or null to specify that no reflog should be written
      Returns:
      this
    • setNoVerify

      public CommitCommand setNoVerify(boolean noVerify)
      Sets the noVerify option on this commit command.

      Both the pre-commit and commit-msg hooks can block a commit by their return value; setting this option to true will bypass these two hooks.

      Parameters:
      noVerify - Whether this commit should be verified by the pre-commit and commit-msg hooks.
      Returns:
      this
      Since:
      3.7
    • setHookOutputStream

      public CommitCommand setHookOutputStream(PrintStream hookStdOut)
      Set the output stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit). If not set it defaults to System.out.
      Parameters:
      hookStdOut - the output stream for hook scripts executed by this command
      Returns:
      this
      Since:
      3.7
    • setHookErrorStream

      public CommitCommand setHookErrorStream(PrintStream hookStdErr)
      Set the error stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit). If not set it defaults to System.err.
      Parameters:
      hookStdErr - the error stream for hook scripts executed by this command
      Returns:
      this
      Since:
      5.6
    • setHookOutputStream

      public CommitCommand setHookOutputStream(String hookName, PrintStream hookStdOut)
      Set the output stream for a selected hook script executed by this command (pre-commit, commit-msg, post-commit). If not set it defaults to System.out.
      Parameters:
      hookName - name of the hook to set the output stream for
      hookStdOut - the output stream to use for the selected hook
      Returns:
      this
      Since:
      4.5
    • setHookErrorStream

      public CommitCommand setHookErrorStream(String hookName, PrintStream hookStdErr)
      Set the error stream for a selected hook script executed by this command (pre-commit, commit-msg, post-commit). If not set it defaults to System.err.
      Parameters:
      hookName - name of the hook to set the output stream for
      hookStdErr - the output stream to use for the selected hook
      Returns:
      this
      Since:
      5.6
    • setSigningKey

      public CommitCommand setSigningKey(String signingKey)
      Sets the signing key

      Per spec of user.signingKey: this will be sent to the GPG program as is, i.e. can be anything supported by the GPG program.

      Note, if none was set or null is specified a default will be obtained from the configuration.

      Parameters:
      signingKey - signing key (maybe null)
      Returns:
      this
      Since:
      5.3
    • setSign

      public CommitCommand setSign(Boolean sign)
      Sets whether the commit should be signed.
      Parameters:
      sign - true to sign, false to not sign and null for default behavior (read from configuration)
      Returns:
      this
      Since:
      5.3
    • setGpgSigner

      public CommitCommand setGpgSigner(GpgSigner signer)
      Sets the GpgSigner to use if the commit is to be signed.
      Parameters:
      signer - to use; if null, the default signer will be used
      Returns:
      this
      Since:
      5.11
    • setGpgConfig

      public CommitCommand setGpgConfig(GpgConfig config)
      Sets an external GpgConfig to use. Whether it will be used is at the discretion of the setGpgSigner(GpgSigner).
      Parameters:
      config - to set; if null, the config will be loaded from the git config of the repository
      Returns:
      this
      Since:
      5.11
    • setCredentialsProvider

      public void setCredentialsProvider(CredentialsProvider credentialsProvider)
      Parameters:
      credentialsProvider - the provider to use when querying for credentials (eg., during signing)
      Since:
      5.3