Package org.eclipse.jgit.lib
Class CommitBuilder
- java.lang.Object
-
- org.eclipse.jgit.lib.CommitBuilder
-
public class CommitBuilder extends java.lang.Object
Mutable builder to construct a commit recording the state of a project. Applications should use this object when they need to manually construct a commit and want precise control over its fields. For a higher level interface seeCommitCommand
. To read a commit object, construct aRevWalk
and obtain aRevCommit
instance by callingRevWalk.parseCommit(AnyObjectId)
.
-
-
Field Summary
Fields Modifier and Type Field Description private PersonIdent
author
private PersonIdent
committer
private static ObjectId[]
EMPTY_OBJECTID_LIST
private java.nio.charset.Charset
encoding
private GpgSignature
gpgSignature
private static byte[]
hauthor
private static byte[]
hcommitter
private static byte[]
hencoding
private static byte[]
hgpgsig
private static byte[]
hparent
private static byte[]
htree
private java.lang.String
message
private ObjectId[]
parentIds
private ObjectId
treeId
-
Constructor Summary
Constructors Constructor Description CommitBuilder()
Initialize an empty commit.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addParentId(AnyObjectId additionalParent)
Add a parent onto the end of the parent list.byte[]
build()
Format this builder's state as a commit object.PersonIdent
getAuthor()
Get the author of this commit (who wrote it).PersonIdent
getCommitter()
Get the committer and commit time for this object.java.nio.charset.Charset
getEncoding()
Get the encoding that should be used for the commit message text.GpgSignature
getGpgSignature()
Get the GPG signature of this commit.java.lang.String
getMessage()
Get the complete commit message.ObjectId[]
getParentIds()
Get the ancestors of this commit.ObjectId
getTreeId()
Get id of the root tree listing this commit's snapshot.void
setAuthor(PersonIdent newAuthor)
Set the author (name, email address, and date) of who wrote the commit.void
setCommitter(PersonIdent newCommitter)
Set the committer and commit time for this object.void
setEncoding(java.lang.String encodingName)
Deprecated.usesetEncoding(Charset)
instead.void
setEncoding(java.nio.charset.Charset enc)
Set the encoding for the commit information.void
setGpgSignature(GpgSignature newSignature)
Set the GPG signature of this commit.void
setMessage(java.lang.String newMessage)
Set the commit message.void
setParentId(AnyObjectId newParent)
Set the parent of this commit.void
setParentIds(java.util.List<? extends AnyObjectId> newParents)
Set the parents of this commit.void
setParentIds(AnyObjectId parent1, AnyObjectId parent2)
Set the parents of this commit.void
setParentIds(ObjectId... newParents)
Set the parents of this commit.void
setTreeId(AnyObjectId id)
Set the tree id for this commit object.byte[]
toByteArray()
Format this builder's state as a commit object.java.lang.String
toString()
(package private) static void
writeGpgSignatureString(java.lang.String in, java.io.OutputStream out)
Writes signature to output as per gpgsig header.
-
-
-
Field Detail
-
EMPTY_OBJECTID_LIST
private static final ObjectId[] EMPTY_OBJECTID_LIST
-
htree
private static final byte[] htree
-
hparent
private static final byte[] hparent
-
hauthor
private static final byte[] hauthor
-
hcommitter
private static final byte[] hcommitter
-
hgpgsig
private static final byte[] hgpgsig
-
hencoding
private static final byte[] hencoding
-
treeId
private ObjectId treeId
-
parentIds
private ObjectId[] parentIds
-
author
private PersonIdent author
-
committer
private PersonIdent committer
-
gpgSignature
private GpgSignature gpgSignature
-
message
private java.lang.String message
-
encoding
private java.nio.charset.Charset encoding
-
-
Method Detail
-
getTreeId
public ObjectId getTreeId()
Get id of the root tree listing this commit's snapshot.- Returns:
- id of the root tree listing this commit's snapshot.
-
setTreeId
public void setTreeId(AnyObjectId id)
Set the tree id for this commit object.- Parameters:
id
- the tree identity.
-
getAuthor
public PersonIdent getAuthor()
Get the author of this commit (who wrote it).- Returns:
- the author of this commit (who wrote it).
-
setAuthor
public void setAuthor(PersonIdent newAuthor)
Set the author (name, email address, and date) of who wrote the commit.- Parameters:
newAuthor
- the new author. Should not be null.
-
getCommitter
public PersonIdent getCommitter()
Get the committer and commit time for this object.- Returns:
- the committer and commit time for this object.
-
setCommitter
public void setCommitter(PersonIdent newCommitter)
Set the committer and commit time for this object.- Parameters:
newCommitter
- the committer information. Should not be null.
-
setGpgSignature
public void setGpgSignature(GpgSignature newSignature)
Set the GPG signature of this commit.Note, the signature set here will change the payload of the commit, i.e. the output of
build()
will include the signature. Thus, the typical flow will be:- call
build()
without a signature set to obtain payload - create
GpgSignature
from payload - set
GpgSignature
- Parameters:
newSignature
- the signature to set ornull
to unset- Since:
- 5.3
- call
-
getGpgSignature
public GpgSignature getGpgSignature()
Get the GPG signature of this commit.- Returns:
- the GPG signature of this commit, maybe
null
if the commit is not to be signed - Since:
- 5.3
-
getParentIds
public ObjectId[] getParentIds()
Get the ancestors of this commit.- Returns:
- the ancestors of this commit. Never null.
-
setParentId
public void setParentId(AnyObjectId newParent)
Set the parent of this commit.- Parameters:
newParent
- the single parent for the commit.
-
setParentIds
public void setParentIds(AnyObjectId parent1, AnyObjectId parent2)
Set the parents of this commit.- Parameters:
parent1
- the first parent of this commit. Typically this is the current value of theHEAD
reference and is thus the current branch's position in history.parent2
- the second parent of this merge commit. Usually this is the branch being merged into the current branch.
-
setParentIds
public void setParentIds(ObjectId... newParents)
Set the parents of this commit.- Parameters:
newParents
- the entire list of parents for this commit.
-
setParentIds
public void setParentIds(java.util.List<? extends AnyObjectId> newParents)
Set the parents of this commit.- Parameters:
newParents
- the entire list of parents for this commit.
-
addParentId
public void addParentId(AnyObjectId additionalParent)
Add a parent onto the end of the parent list.- Parameters:
additionalParent
- new parent to add onto the end of the current parent list.
-
getMessage
public java.lang.String getMessage()
Get the complete commit message.- Returns:
- the complete commit message.
-
setMessage
public void setMessage(java.lang.String newMessage)
Set the commit message.- Parameters:
newMessage
- the commit message. Should not be null.
-
setEncoding
@Deprecated public void setEncoding(java.lang.String encodingName)
Deprecated.usesetEncoding(Charset)
instead.Set the encoding for the commit information.- Parameters:
encodingName
- the encoding name. SeeCharset.forName(String)
.
-
setEncoding
public void setEncoding(java.nio.charset.Charset enc)
Set the encoding for the commit information.- Parameters:
enc
- the encoding to use.
-
getEncoding
public java.nio.charset.Charset getEncoding()
Get the encoding that should be used for the commit message text.- Returns:
- the encoding that should be used for the commit message text.
-
build
public byte[] build() throws java.io.UnsupportedEncodingException
Format this builder's state as a commit object.- Returns:
- this object in the canonical commit format, suitable for storage in a repository.
- Throws:
java.io.UnsupportedEncodingException
- the encoding specified bygetEncoding()
is not supported by this Java runtime.
-
writeGpgSignatureString
static void writeGpgSignatureString(java.lang.String in, java.io.OutputStream out) throws java.io.IOException, java.lang.IllegalArgumentException
Writes signature to output as per gpgsig header.CRLF and CR will be sanitized to LF and signature will have a hanging indent of one space starting with line two. A trailing line break is not written; the caller is supposed to terminate the GPG signature header by writing a single newline.
- Parameters:
in
- signature string with line breaksout
- output stream- Throws:
java.io.IOException
- thrown by the output streamjava.lang.IllegalArgumentException
- if the signature string contains non 7-bit ASCII chars
-
toByteArray
public byte[] toByteArray() throws java.io.UnsupportedEncodingException
Format this builder's state as a commit object.- Returns:
- this object in the canonical commit format, suitable for storage in a repository.
- Throws:
java.io.UnsupportedEncodingException
- the encoding specified bygetEncoding()
is not supported by this Java runtime.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-