Class CommitCommand
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 Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private Boolean
private boolean
private PersonIdent
private boolean
private CommitConfig.CleanupMode
private Character
private PersonIdent
private CredentialsProvider
private GpgConfig
private GpgSigner
private HashMap<String,
PrintStream> private HashMap<String,
PrintStream> private boolean
private static final org.slf4j.Logger
private String
private boolean
Setting this option bypasses the pre-commit and commit-msg hooks.private boolean[]
parents this commit should have.private String
private Boolean
private String
private boolean
Fields inherited from class org.eclipse.jgit.api.GitCommand
repo
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
CommitCommand
(Repository repo) Constructor for CommitCommand -
Method Summary
Modifier and TypeMethodDescriptioncall()
private void
checkIfEmpty
(RevWalk rw, ObjectId headId, ObjectId indexTreeId) private DirCache
createTemporaryIndex
(ObjectId headId, DirCache index, RevWalk rw) Get the authorGet the committerGet the commit messageprivate void
insertChangeId
(ObjectId treeId) private boolean
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.private void
processOptions
(RepositoryState state, RevWalk rw) Sets default values for not explicitly specified options.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.setAllowEmpty
(boolean allowEmpty) Set whether to allow to create an empty commitsetAmend
(boolean amend) Used to amend the tip of the current branch.Sets the author for thiscommit
.setAuthor
(PersonIdent author) Sets the author for thiscommit
.Sets theCommitConfig.CleanupMode
to apply to the commit message.setCommentCharacter
(Character commentChar) Sets the comment character to apply when cleaning a commit message.setCommitter
(String name, String email) Sets the committer for thiscommit
.setCommitter
(PersonIdent committer) Sets the committer for thiscommit
.setCredentialsProvider
(CredentialsProvider credentialsProvider) Sets aCredentialsProvider
setDefaultClean
(boolean strip) Sets the default clean mode ifsetCleanupMode(CleanupMode.DEFAULT)
is set and git configcommit.cleanup = default
or is not set.setGpgConfig
(GpgConfig config) Sets an externalGpgConfig
to use.setGpgSigner
(GpgSigner signer) Sets theGpgSigner
to use if the commit is to be signed.setHookErrorStream
(PrintStream hookStdErr) Set the error stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).setHookErrorStream
(String hookName, PrintStream hookStdErr) Set the error stream for a selected hook script executed by this command (pre-commit, commit-msg, post-commit).setHookOutputStream
(PrintStream hookStdOut) Set the output stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).setHookOutputStream
(String hookName, PrintStream hookStdOut) Set the output stream for a selected hook script executed by this command (pre-commit, commit-msg, post-commit).setInsertChangeId
(boolean insertChangeId) If set to true a change id will be inserted into the commit message An existing change id is not replaced.setMessage
(String message) Set the commit messagesetNoVerify
(boolean noVerify) Sets thenoVerify
option on this commit command.Commit dedicated path only.setReflogComment
(String reflogComment) Override the message written to the reflogSets whether the commit should be signed.setSigningKey
(String signingKey) Sets the signing keyprivate void
sign
(CommitBuilder commit) private void
updateRef
(RepositoryState state, ObjectId headId, RevCommit revCommit, ObjectId commitId) Methods inherited from class org.eclipse.jgit.api.GitCommand
checkCallable, getRepository, setCallable
-
Field Details
-
log
private static final org.slf4j.Logger log -
author
-
committer
-
message
-
all
private boolean all -
only
-
onlyProcessed
private boolean[] onlyProcessed -
amend
private boolean amend -
insertChangeId
private boolean insertChangeId -
parents
parents this commit should have. The current HEAD will be in this list and also all commits mentioned in .git/MERGE_HEAD -
reflogComment
-
useDefaultReflogMessage
private boolean useDefaultReflogMessage -
noVerify
private boolean noVerifySetting this option bypasses the pre-commit and commit-msg hooks. -
hookOutRedirect
-
hookErrRedirect
-
allowEmpty
-
signCommit
-
signingKey
-
gpgSigner
-
gpgConfig
-
credentialsProvider
-
cleanupMode
-
cleanDefaultIsStrip
private boolean cleanDefaultIsStrip -
commentChar
-
-
Constructor Details
-
CommitCommand
Constructor for CommitCommand- Parameters:
repo
- theRepository
-
-
Method Details
-
call
public RevCommit call() throws GitAPIException, AbortedByHookException, ConcurrentRefUpdateException, NoHeadException, NoMessageException, ServiceUnavailableException, UnmergedPathsException, WrongRepositoryStateExceptionExecute 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
-
checkIfEmpty
private void checkIfEmpty(RevWalk rw, ObjectId headId, ObjectId indexTreeId) throws EmptyCommitException, MissingObjectException, IncorrectObjectTypeException, IOException -
sign
private void sign(CommitBuilder commit) throws ServiceUnavailableException, CanceledException, UnsupportedSigningFormatException -
updateRef
private void updateRef(RepositoryState state, ObjectId headId, RevCommit revCommit, ObjectId commitId) throws ConcurrentRefUpdateException, IOException -
insertChangeId
-
createTemporaryIndex
private DirCache createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw) throws IOException - Throws:
IOException
-
lookupOnly
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
-
setMessage
Set the commit message- Parameters:
message
- the commit message used for thecommit
- Returns:
this
-
setCleanupMode
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
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
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
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
Get the commit message- Returns:
- the commit message used for the
commit
-
setCommitter
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Sets aCredentialsProvider
- Parameters:
credentialsProvider
- the provider to use when querying for credentials (eg., during signing)- Returns:
this
- Since:
- 6.0
-