Class CommitCommand
- java.lang.Object
-
- org.eclipse.jgit.api.GitCommand<RevCommit>
-
- org.eclipse.jgit.api.CommitCommand
-
public class CommitCommand extends GitCommand<RevCommit>
A class used to execute aCommit
command. It has setters for all supported options and arguments of this command and acall()
method to finally execute the command.- See Also:
- Git documentation about Commit
-
-
Field Summary
-
Fields inherited from class org.eclipse.jgit.api.GitCommand
repo
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
CommitCommand(Repository repo)
Constructor for CommitCommand
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RevCommit
call()
PersonIdent
getAuthor()
Get the authorPersonIdent
getCommitter()
Get the committerString
getMessage()
Get the commit messageCommitCommand
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.CommitCommand
setAllowEmpty(boolean allowEmpty)
Set whether to allow to create an empty commitCommitCommand
setAmend(boolean amend)
Used to amend the tip of the current branch.CommitCommand
setAuthor(String name, String email)
Sets the author for thiscommit
.CommitCommand
setAuthor(PersonIdent author)
Sets the author for thiscommit
.CommitCommand
setCleanupMode(CommitConfig.CleanupMode mode)
Sets theCommitConfig.CleanupMode
to apply to the commit message.CommitCommand
setCommentCharacter(Character commentChar)
Sets the comment character to apply when cleaning a commit message.CommitCommand
setCommitter(String name, String email)
Sets the committer for thiscommit
.CommitCommand
setCommitter(PersonIdent committer)
Sets the committer for thiscommit
.CommitCommand
setCredentialsProvider(CredentialsProvider credentialsProvider)
Sets aCredentialsProvider
CommitCommand
setDefaultClean(boolean strip)
Sets the default clean mode ifsetCleanupMode(CleanupMode.DEFAULT)
is set and git configcommit.cleanup = default
or is not set.CommitCommand
setGpgConfig(GpgConfig config)
Sets an externalGpgConfig
to use.CommitCommand
setGpgSigner(GpgSigner signer)
Sets theGpgSigner
to use if the commit is to be signed.CommitCommand
setHookErrorStream(PrintStream hookStdErr)
Set the error stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).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).CommitCommand
setHookOutputStream(PrintStream hookStdOut)
Set the output stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).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).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.CommitCommand
setMessage(String message)
Set the commit messageCommitCommand
setNoVerify(boolean noVerify)
Sets thenoVerify
option on this commit command.CommitCommand
setOnly(String only)
Commit dedicated path only.CommitCommand
setReflogComment(String reflogComment)
Override the message written to the reflogCommitCommand
setSign(Boolean sign)
Sets whether the commit should be signed.CommitCommand
setSigningKey(String signingKey)
Sets the signing key-
Methods inherited from class org.eclipse.jgit.api.GitCommand
checkCallable, getRepository, setCallable
-
-
-
-
Constructor Detail
-
CommitCommand
protected CommitCommand(Repository repo)
Constructor for CommitCommand- Parameters:
repo
- theRepository
-
-
Method Detail
-
call
public RevCommit call() throws GitAPIException, AbortedByHookException, ConcurrentRefUpdateException, NoHeadException, NoMessageException, ServiceUnavailableException, UnmergedPathsException, WrongRepositoryStateException
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 tocall()
)- Specified by:
call
in interfaceCallable<RevCommit>
- Specified by:
call
in classGitCommand<RevCommit>
- Throws:
ServiceUnavailableException
- if signing service is not available e.g. since it isn't installedGitAPIException
AbortedByHookException
ConcurrentRefUpdateException
NoHeadException
NoMessageException
UnmergedPathsException
WrongRepositoryStateException
-
setMessage
public CommitCommand setMessage(String message)
Set the commit message- Parameters:
message
- the commit message used for thecommit
- Returns:
this
-
setCleanupMode
public CommitCommand setCleanupMode(@NonNull CommitConfig.CleanupMode mode)
Sets theCommitConfig.CleanupMode
to apply to the commit message. If not called,CommitCommand
appliesCommitConfig.CleanupMode.VERBATIM
.- Parameters:
mode
-CommitConfig.CleanupMode
to set- Returns:
this
- Since:
- 6.1
-
setDefaultClean
public CommitCommand setDefaultClean(boolean strip)
Sets the default clean mode ifsetCleanupMode(CleanupMode.DEFAULT)
is set and git configcommit.cleanup = default
or is not set.- Parameters:
strip
- iftrue
, default toCommitConfig.CleanupMode.STRIP
; otherwise default toCommitConfig.CleanupMode.WHITESPACE
- Returns:
this
- Since:
- 6.1
-
setCommentCharacter
public CommitCommand setCommentCharacter(Character commentChar)
Sets the comment character to apply when cleaning a commit message. Ifnull
(the default) and theclean-up mode
isCommitConfig.CleanupMode.STRIP
orCommitConfig.CleanupMode.SCISSORS
, the value of git configcore.commentChar
will be used.- Parameters:
commentChar
- the comment character, ornull
to use the value from the git config- Returns:
this
- Since:
- 6.1
-
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 totrue
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 thiscommit
. If no committer is explicitly specified because this method is never called or called withnull
value then the committer will be deduced from config info in repository, with current time.- Parameters:
committer
- the committer used for thecommit
- Returns:
this
-
setCommitter
public CommitCommand setCommitter(String name, String email)
Sets the committer for thiscommit
. 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 thecommit
email
- the email of the committer used for thecommit
- Returns:
this
-
getCommitter
public PersonIdent getCommitter()
Get the committer- Returns:
- the committer used for the
commit
. If no committer was specifiednull
is returned and the defaultPersonIdent
of this repo is used during execution of the command
-
setAuthor
public CommitCommand setAuthor(PersonIdent author)
Sets the author for thiscommit
. If no author is explicitly specified because this method is never called or called withnull
value then the author will be set to the committer or to the original author when amending.- Parameters:
author
- the author used for thecommit
- Returns:
this
-
setAuthor
public CommitCommand setAuthor(String name, String email)
Sets the author for thiscommit
. 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 thecommit
email
- the email of the author used for thecommit
- Returns:
this
-
getAuthor
public PersonIdent getAuthor()
Get the author- Returns:
- the author used for the
commit
. If no author was specifiednull
is returned and the defaultPersonIdent
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 totrue
, the previous commit will be amended. This is equivalent to --amend on the command line.- Parameters:
amend
- whether to amend 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 ornull
to specify that no reflog should be written- Returns:
this
-
setNoVerify
public CommitCommand setNoVerify(boolean noVerify)
Sets thenoVerify
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 toSystem.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 toSystem.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 toSystem.out
.- Parameters:
hookName
- name of the hook to set the output stream forhookStdOut
- 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 toSystem.err
.- Parameters:
hookName
- name of the hook to set the output stream forhookStdErr
- the output stream to use for the selected hook- Returns:
this
- Since:
- 5.6
-
setSigningKey
public CommitCommand setSigningKey(String signingKey)
Sets the signing keyPer 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 (maybenull
)- 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 andnull
for default behavior (read from configuration)- Returns:
this
- Since:
- 5.3
-
setGpgSigner
public CommitCommand setGpgSigner(GpgSigner signer)
Sets theGpgSigner
to use if the commit is to be signed.- Parameters:
signer
- to use; ifnull
, the default signer will be used- Returns:
this
- Since:
- 5.11
-
setGpgConfig
public CommitCommand setGpgConfig(GpgConfig config)
Sets an externalGpgConfig
to use. Whether it will be used is at the discretion of thesetGpgSigner(GpgSigner)
.- Parameters:
config
- to set; ifnull
, the config will be loaded from the git config of the repository- Returns:
this
- Since:
- 5.11
-
setCredentialsProvider
public CommitCommand setCredentialsProvider(CredentialsProvider credentialsProvider)
Sets aCredentialsProvider
- Parameters:
credentialsProvider
- the provider to use when querying for credentials (eg., during signing)- Returns:
this
- Since:
- 6.0
-
-