Top Description Fields Constructors Methods
org.eclipse.jgit.api

public Class CommitCommand

extends GitCommand<RevCommit>
Class Inheritance
Imports
java.io.IOException, .InputStream, .PrintStream, java.text.MessageFormat, java.util.ArrayList, .Collections, .HashMap, .List, org.eclipse.jgit.annotations.NonNull, org.eclipse.jgit.api.errors.AbortedByHookException, .CanceledException, .ConcurrentRefUpdateException, .EmptyCommitException, .GitAPIException, .JGitInternalException, .NoFilepatternException, .NoHeadException, .NoMessageException, .ServiceUnavailableException, .UnmergedPathsException, .UnsupportedSigningFormatException, .WrongRepositoryStateException, org.eclipse.jgit.dircache.DirCache, .DirCacheBuildIterator, .DirCacheBuilder, .DirCacheEntry, .DirCacheIterator, org.eclipse.jgit.errors.IncorrectObjectTypeException, .MissingObjectException, .UnmergedPathException, org.eclipse.jgit.hooks.CommitMsgHook, .Hooks, .PostCommitHook, .PreCommitHook, org.eclipse.jgit.internal.JGitText, org.eclipse.jgit.lib.CommitBuilder, .CommitConfig, .CommitConfig.CleanupMode, .Constants, .FileMode, .GpgConfig, .ObjectId, .ObjectInserter, .PersonIdent, .Ref, .RefUpdate, .RefUpdate.Result, .Repository, .RepositoryState, .Signer, .Signers, org.eclipse.jgit.revwalk.RevCommit, .RevObject, .RevTag, .RevWalk, org.eclipse.jgit.transport.CredentialsProvider, org.eclipse.jgit.treewalk.CanonicalTreeParser, .FileTreeIterator, .TreeWalk, .TreeWalk.OperationType, org.eclipse.jgit.util.ChangeIdUtil, org.slf4j.Logger, .LoggerFactory

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
Git documentation about Commit

Field Summary

Modifier and TypeField and Description
private boolean
private Boolean
private boolean
private PersonIdent
private boolean
private CommitConfig.CleanupMode
private Character
private PersonIdent
private CredentialsProvider
private GpgConfig
private HashMap<String, PrintStream>
private HashMap<String, PrintStream>
private boolean
private static final Logger
private String
private boolean
noVerify

Setting this option bypasses the pre-commit and commit-msg hooks.

private List<String>
private boolean[]
private List<ObjectId>
parents

parents this commit should have.

private String
private Boolean
private Signer
private String
private boolean
Inherited from org.eclipse.jgit.api.GitCommand:
repo

Constructor Summary

AccessConstructor and Description
protected
CommitCommand(Repository repo)

Constructor for CommitCommand

Method Summary

Modifier and TypeMethod and Description
public RevCommit
call()

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Computes a result, or throws an exception if unable to do so.

private void
checkIfEmpty(RevWalk rw, ObjectId headId, ObjectId indexTreeId)

private DirCache
public PersonIdent

Returns:

the author used for the commit. If no author was specified null is returned and the default org.eclipse.jgit.lib.PersonIdent of this repo is used during execution of the command
getAuthor
()

Get the author

public PersonIdent

Returns:

the committer used for the commit. If no committer was specified null is returned and the default org.eclipse.jgit.lib.PersonIdent of this repo is used during execution of the command
getCommitter
()

Get the committer

public String

Returns:

the commit message used for the commit
getMessage
()

Get the commit message

private void
private boolean
private int

Returns:

the item's index in only; -1 if no item matches
lookupOnly
(String
entry's path
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").

private void
processOptions(RepositoryState
the state of the repository we are working on
state
,
RevWalk
the RevWalk to use
rw
)

Sets default values for not explicitly specified options.

public CommitCommand

Returns:

this
setAll
(boolean
whether to auto-stage all files that have been modified and deleted
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.

public CommitCommand

Returns:

this
setAllowEmpty
(boolean
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 org.eclipse.jgit.api.errors.JGitInternalException. By setting this flag to true this exception will not be thrown.

allowEmpty
)

Set whether to allow to create an empty commit

public CommitCommand

Returns:

this
setAmend
(boolean
whether to amend the tip of the current branch
amend
)

Used to amend the tip of the current branch.

public CommitCommand

Returns:

this
setAuthor
(PersonIdent
the author used for the commit
author
)

Sets the author for this commit.

public CommitCommand

Returns:

this
setAuthor
(String
the name of the author used for the commit
name
,
String
the email of the author used for the commit
email
)

Sets the author for this commit.

public CommitCommand

Returns:

this
setCleanupMode
(CommitConfig.CleanupMode mode)

Sets the CleanupMode to apply to the commit message.

public CommitCommand

Returns:

this
setCommentCharacter
(Character
the comment character, or null to use the value from the git config
commentChar
)

Sets the comment character to apply when cleaning a commit message.

public CommitCommand

Returns:

this
setCommitter
(PersonIdent
the committer used for the commit
committer
)

Sets the committer for this commit.

public CommitCommand

Returns:

this
setCommitter
(String
the name of the committer used for the commit
name
,
String
the email of the committer used for the commit
email
)

Sets the committer for this commit.

public CommitCommand

Returns:

this
setCredentialsProvider
(CredentialsProvider
the provider to use when querying for credentials (eg., during signing)
credentialsProvider
)

Sets a CredentialsProvider

public CommitCommand

Returns:

this
setDefaultClean
(boolean
if true, default to CleanupMode#STRIP; otherwise default to CleanupMode#WHITESPACE
strip
)

Sets the default clean mode if setCleanupMode(CleanupMode.DEFAULT) is set and git config commit.cleanup = default or is not set.

public CommitCommand

Returns:

this
setGpgConfig
(GpgConfig
to set; if null, the config will be loaded from the git config of the repository
config
)

Sets an external GpgConfig to use.

public CommitCommand

Returns:

this
setHookErrorStream
(PrintStream
the error stream for hook scripts executed by this command
hookStdErr
)

Set the error stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).

public CommitCommand

Returns:

this
setHookErrorStream
(String
name of the hook to set the output stream for
hookName
,
PrintStream
the output stream to use for the selected hook
hookStdErr
)

Set the error stream for a selected hook script executed by this command (pre-commit, commit-msg, post-commit).

public CommitCommand

Returns:

this
setHookOutputStream
(PrintStream
the output stream for hook scripts executed by this command
hookStdOut
)

Set the output stream for all hook scripts executed by this command (pre-commit, commit-msg, post-commit).

public CommitCommand

Returns:

this
setHookOutputStream
(String
name of the hook to set the output stream for
hookName
,
PrintStream
the output stream to use for the selected hook
hookStdOut
)

Set the output stream for a selected hook script executed by this command (pre-commit, commit-msg, post-commit).

public CommitCommand

Returns:

this
setInsertChangeId
(boolean
whether to insert a change id
insertChangeId
)

If set to true a change id will be inserted into the commit message An existing change id is not replaced.

public CommitCommand

Returns:

this
setMessage
(String
the commit message used for the commit
message
)

Set the commit message

public CommitCommand

Returns:

this
setNoVerify
(boolean
Whether this commit should be verified by the pre-commit and commit-msg hooks.
noVerify
)

Sets the noVerify option on this commit command.

public CommitCommand

Returns:

this
setOnly
(String
path to commit (with / as separator)
only
)

Commit dedicated path only.

public CommitCommand

Returns:

this
setReflogComment
(String
the comment to be written into the reflog or null to specify that no reflog should be written
reflogComment
)

Override the message written to the reflog

public CommitCommand

Returns:

this
setSign
(Boolean
true to sign, false to not sign and null for default behavior (read from configuration)
sign
)

Sets whether the commit should be signed.

public CommitCommand

Returns:

this
setSigner
(Signer
to use; if null, the default signer will be used
signer
)

Sets the Signer to use if the commit is to be signed.

public CommitCommand

Returns:

this
setSigningKey
(String
signing key (maybe null)
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.

private void
private void
updateRef(RepositoryState state, ObjectId headId, RevCommit revCommit, ObjectId commitId)

Inherited from org.eclipse.jgit.api.GitCommand:
checkCallablegetRepositorysetCallable

Field Detail

allback to summary
private boolean all
allowEmptyback to summary
private Boolean allowEmpty
amendback to summary
private boolean amend
authorback to summary
private PersonIdent author
cleanDefaultIsStripback to summary
private boolean cleanDefaultIsStrip
cleanupModeback to summary
private CommitConfig.CleanupMode cleanupMode
Annotations
@NonNull
commentCharback to summary
private Character commentChar
committerback to summary
private PersonIdent committer
credentialsProviderback to summary
private CredentialsProvider credentialsProvider
gpgConfigback to summary
private GpgConfig gpgConfig
hookErrRedirectback to summary
private HashMap<String, PrintStream> hookErrRedirect
hookOutRedirectback to summary
private HashMap<String, PrintStream> hookOutRedirect
insertChangeIdback to summary
private boolean insertChangeId
logback to summary
private static final Logger log
messageback to summary
private String message
noVerifyback to summary
private boolean noVerify

Setting this option bypasses the pre-commit and commit-msg hooks.

onlyback to summary
private List<String> only
onlyProcessedback to summary
private boolean[] onlyProcessed
parentsback to summary
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

reflogCommentback to summary
private String reflogComment
signCommitback to summary
private Boolean signCommit
signerback to summary
private Signer signer
signingKeyback to summary
private String signingKey
useDefaultReflogMessageback to summary
private boolean useDefaultReflogMessage

Constructor Detail

CommitCommandback to summary
protected CommitCommand(Repository repo)

Constructor for CommitCommand

Parameters
repo:Repository

the org.eclipse.jgit.lib.Repository

Method Detail

callback to summary
public RevCommit call() throws GitAPIException, AbortedByHookException, ConcurrentRefUpdateException, NoHeadException, NoMessageException, ServiceUnavailableException, UnmergedPathsException, WrongRepositoryStateException

Implements abstract org.eclipse.jgit.api.GitCommand.call.

Implements java.util.concurrent.Callable.call.

Doc from org.eclipse.jgit.api.GitCommand.call.

Computes a result, or throws an exception if unable to do so.

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())

Returns:RevCommit

Doc from java.util.concurrent.Callable.call.

computed result

Annotations
@Override
Exceptions
ServiceUnavailableException:
if signing service is not available e.g. since it isn't installed
checkIfEmptyback to summary
private void checkIfEmpty(RevWalk rw, ObjectId headId, ObjectId indexTreeId) throws EmptyCommitException, MissingObjectException, IncorrectObjectTypeException, IOException
createTemporaryIndexback to summary
private DirCache createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw) throws IOException
getAuthorback to summary
public PersonIdent getAuthor()

Get the author

Returns:PersonIdent

the author used for the commit. If no author was specified null is returned and the default org.eclipse.jgit.lib.PersonIdent of this repo is used during execution of the command

getCommitterback to summary
public PersonIdent getCommitter()

Get the committer

Returns:PersonIdent

the committer used for the commit. If no committer was specified null is returned and the default org.eclipse.jgit.lib.PersonIdent of this repo is used during execution of the command

getMessageback to summary
public String getMessage()

Get the commit message

Returns:String

the commit message used for the commit

insertChangeIdback to summary
private void insertChangeId(ObjectId treeId)
isMergeDuringRebaseback to summary
private boolean isMergeDuringRebase(RepositoryState state)
lookupOnlyback to summary
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:String

entry's path

Returns:int

the item's index in only; -1 if no item matches

processOptionsback to summary
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:RepositoryState

the state of the repository we are working on

rw:RevWalk

the RevWalk to use

Exceptions
NoMessageException:
if the commit message has not been specified
UnsupportedSigningFormatException:
if the configured gpg.format is not supported
setAllback to summary
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:boolean

whether to auto-stage all files that have been modified and deleted

Returns:CommitCommand

this

Exceptions
JGitInternalException:
in case of an illegal combination of arguments/ options
setAllowEmptyback to summary
public CommitCommand setAllowEmpty(boolean allowEmpty)

Set whether to allow to create an empty commit

Parameters
allowEmpty:boolean

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 org.eclipse.jgit.api.errors.JGitInternalException. By setting this flag to true this exception will not be thrown.

Returns:CommitCommand

this

Since
4.2
setAmendback to summary
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:boolean

whether to amend the tip of the current branch

Returns:CommitCommand

this

setAuthorback to summary
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:PersonIdent

the author used for the commit

Returns:CommitCommand

this

setAuthorback to summary
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:String

the name of the author used for the commit

email:String

the email of the author used for the commit

Returns:CommitCommand

this

setCleanupModeback to summary
public CommitCommand setCleanupMode(CommitConfig.CleanupMode mode)

Sets the CleanupMode to apply to the commit message. If not called, CommitCommand applies CleanupMode#VERBATIM.

Parameters
mode:CommitConfig.CleanupMode

CleanupMode to set

Returns:CommitCommand

this

Since
6.1
setCommentCharacterback to summary
public CommitCommand setCommentCharacter(Character commentChar)

Sets the comment character to apply when cleaning a commit message. If null (the default) and the clean-up mode is CleanupMode#STRIP or CleanupMode#SCISSORS, the value of git config core.commentChar will be used.

Parameters
commentChar:Character

the comment character, or null to use the value from the git config

Returns:CommitCommand

this

Since
6.1
setCommitterback to summary
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:PersonIdent

the committer used for the commit

Returns:CommitCommand

this

setCommitterback to summary
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:String

the name of the committer used for the commit

email:String

the email of the committer used for the commit

Returns:CommitCommand

this

setCredentialsProviderback to summary
public CommitCommand setCredentialsProvider(CredentialsProvider credentialsProvider)

Sets a CredentialsProvider

Parameters
credentialsProvider:CredentialsProvider

the provider to use when querying for credentials (eg., during signing)

Returns:CommitCommand

this

Since
6.0
setDefaultCleanback to summary
public CommitCommand setDefaultClean(boolean strip)

Sets the default clean mode if setCleanupMode(CleanupMode.DEFAULT) is set and git config commit.cleanup = default or is not set.

Parameters
strip:boolean

if true, default to CleanupMode#STRIP; otherwise default to CleanupMode#WHITESPACE

Returns:CommitCommand

this

Since
6.1
setGpgConfigback to summary
public CommitCommand setGpgConfig(GpgConfig config)

Sets an external GpgConfig to use. Whether it will be used is at the discretion of the setSigner(Signer).

Parameters
config:GpgConfig

to set; if null, the config will be loaded from the git config of the repository

Returns:CommitCommand

this

Since
5.11
setHookErrorStreamback to summary
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:PrintStream

the error stream for hook scripts executed by this command

Returns:CommitCommand

this

Since
5.6
setHookErrorStreamback to summary
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:String

name of the hook to set the output stream for

hookStdErr:PrintStream

the output stream to use for the selected hook

Returns:CommitCommand

this

Since
5.6
setHookOutputStreamback to summary
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:PrintStream

the output stream for hook scripts executed by this command

Returns:CommitCommand

this

Since
3.7
setHookOutputStreamback to summary
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:String

name of the hook to set the output stream for

hookStdOut:PrintStream

the output stream to use for the selected hook

Returns:CommitCommand

this

Since
4.5
setInsertChangeIdback to summary
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:boolean

whether to insert a change id

Returns:CommitCommand

this

setMessageback to summary
public CommitCommand setMessage(String message)

Set the commit message

Parameters
message:String

the commit message used for the commit

Returns:CommitCommand

this

setNoVerifyback to summary
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:boolean

Whether this commit should be verified by the pre-commit and commit-msg hooks.

Returns:CommitCommand

this

Since
3.7
setOnlyback to summary
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:String

path to commit (with / as separator)

Returns:CommitCommand

this

setReflogCommentback to summary
public CommitCommand setReflogComment(String reflogComment)

Override the message written to the reflog

Parameters
reflogComment:String

the comment to be written into the reflog or null to specify that no reflog should be written

Returns:CommitCommand

this

setSignback to summary
public CommitCommand setSign(Boolean sign)

Sets whether the commit should be signed.

Parameters
sign:Boolean

true to sign, false to not sign and null for default behavior (read from configuration)

Returns:CommitCommand

this

Since
5.3
setSignerback to summary
public CommitCommand setSigner(Signer signer)

Sets the Signer to use if the commit is to be signed.

Parameters
signer:Signer

to use; if null, the default signer will be used

Returns:CommitCommand

this

Since
7.0
setSigningKeyback to summary
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:String

signing key (maybe null)

Returns:CommitCommand

this

Since
5.3
signback to summary
private void sign(CommitBuilder commit) throws CanceledException, IOException, UnsupportedSigningFormatException
updateRefback to summary
private void updateRef(RepositoryState state, ObjectId headId, RevCommit revCommit, ObjectId commitId) throws ConcurrentRefUpdateException, IOException