Class CommitCommand
- java.lang.Object
-
- org.eclipse.jgit.api.GitCommand<RevCommit>
-
- org.eclipse.jgit.api.CommitCommand
-
- All Implemented Interfaces:
java.util.concurrent.Callable<RevCommit>
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 Modifier and Type Field Description private boolean
all
private java.lang.Boolean
allowEmpty
private boolean
amend
private PersonIdent
author
private PersonIdent
committer
private CredentialsProvider
credentialsProvider
private GpgSigner
gpgSigner
private java.util.HashMap<java.lang.String,java.io.PrintStream>
hookErrRedirect
private java.util.HashMap<java.lang.String,java.io.PrintStream>
hookOutRedirect
private boolean
insertChangeId
private java.lang.String
message
private boolean
noVerify
Setting this option bypasses the pre-commit and commit-msg hooks.private java.util.List<java.lang.String>
only
private boolean[]
onlyProcessed
private java.util.List<ObjectId>
parents
parents this commit should have.private java.lang.String
reflogComment
private java.lang.Boolean
signCommit
private java.lang.String
signingKey
private boolean
useDefaultReflogMessage
-
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()
private DirCache
createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw)
PersonIdent
getAuthor()
Get the authorPersonIdent
getCommitter()
Get the committerjava.lang.String
getMessage()
Get the commit messageprivate void
insertChangeId(ObjectId treeId)
private boolean
isMergeDuringRebase(RepositoryState state)
private int
lookupOnly(java.lang.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.private void
processOptions(RepositoryState state, RevWalk rw)
Sets default values for not explicitly specified options.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.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(java.lang.String name, java.lang.String email)
Sets the author for thiscommit
.CommitCommand
setAuthor(PersonIdent author)
Sets the author for thiscommit
.CommitCommand
setCommitter(java.lang.String name, java.lang.String email)
Sets the committer for thiscommit
.CommitCommand
setCommitter(PersonIdent committer)
Sets the committer for thiscommit
.void
setCredentialsProvider(CredentialsProvider credentialsProvider)
Sets aCredentialsProvider
CommitCommand
setHookErrorStream(java.io.PrintStream hookStdErr)
Set the error stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).CommitCommand
setHookErrorStream(java.lang.String hookName, java.io.PrintStream hookStdErr)
Set the error stream for a selected hook script executed by this command (pre-commit, commit-msg, post-commit).CommitCommand
setHookOutputStream(java.io.PrintStream hookStdOut)
Set the output stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).CommitCommand
setHookOutputStream(java.lang.String hookName, java.io.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(java.lang.String message)
Set the commit messageCommitCommand
setNoVerify(boolean noVerify)
Sets thenoVerify
option on this commit command.CommitCommand
setOnly(java.lang.String only)
Commit dedicated path only.CommitCommand
setReflogComment(java.lang.String reflogComment)
Override the message written to the reflogCommitCommand
setSign(java.lang.Boolean sign)
Sets whether the commit should be signed.CommitCommand
setSigningKey(java.lang.String signingKey)
Sets the signing key-
Methods inherited from class org.eclipse.jgit.api.GitCommand
checkCallable, getRepository, setCallable
-
-
-
-
Field Detail
-
author
private PersonIdent author
-
committer
private PersonIdent committer
-
message
private java.lang.String message
-
all
private boolean all
-
only
private java.util.List<java.lang.String> only
-
onlyProcessed
private boolean[] onlyProcessed
-
amend
private boolean amend
-
insertChangeId
private boolean insertChangeId
-
parents
private java.util.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 java.lang.String reflogComment
-
useDefaultReflogMessage
private boolean useDefaultReflogMessage
-
noVerify
private boolean noVerify
Setting this option bypasses the pre-commit and commit-msg hooks.
-
hookOutRedirect
private java.util.HashMap<java.lang.String,java.io.PrintStream> hookOutRedirect
-
hookErrRedirect
private java.util.HashMap<java.lang.String,java.io.PrintStream> hookErrRedirect
-
allowEmpty
private java.lang.Boolean allowEmpty
-
signCommit
private java.lang.Boolean signCommit
-
signingKey
private java.lang.String signingKey
-
gpgSigner
private GpgSigner gpgSigner
-
credentialsProvider
private CredentialsProvider credentialsProvider
-
-
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 interfacejava.util.concurrent.Callable<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
-
insertChangeId
private void insertChangeId(ObjectId treeId)
-
createTemporaryIndex
private DirCache createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw) throws java.io.IOException
- Throws:
java.io.IOException
-
lookupOnly
private int lookupOnly(java.lang.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 inonly
, 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 onrw
- the RevWalk to use- Throws:
NoMessageException
- if the commit message has not been specifiedUnsupportedSigningFormatException
- if the configured gpg.format is not supported
-
isMergeDuringRebase
private boolean isMergeDuringRebase(RepositoryState state)
-
setMessage
public CommitCommand setMessage(java.lang.String message)
Set the commit message- Parameters:
message
- the commit message used for thecommit
- 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 totrue
this exception will not be thrown.- Returns:
this
- Since:
- 4.2
-
getMessage
public java.lang.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(java.lang.String name, java.lang.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(java.lang.String name, java.lang.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 ammend the tip of the current branch- Returns:
this
-
setOnly
public CommitCommand setOnly(java.lang.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(java.lang.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(java.io.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(java.io.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(java.lang.String hookName, java.io.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(java.lang.String hookName, java.io.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(java.lang.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(java.lang.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
-
setCredentialsProvider
public void setCredentialsProvider(CredentialsProvider credentialsProvider)
Sets aCredentialsProvider
- Parameters:
credentialsProvider
- the provider to use when querying for credentials (eg., during signing)- Since:
- 5.3
-
-