Package org.eclipse.jgit.api
Class RebaseCommand
- java.lang.Object
-
- org.eclipse.jgit.api.GitCommand<RebaseResult>
-
- org.eclipse.jgit.api.RebaseCommand
-
- All Implemented Interfaces:
java.util.concurrent.Callable<RebaseResult>
public class RebaseCommand extends GitCommand<RebaseResult>
A class used to execute aRebasecommand. It has setters for all supported options and arguments of this command and acall()method to finally execute the command. Each instance of this class should only be used for one invocation of the command (means: one call tocall())- See Also:
- Git documentation about Rebase
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceRebaseCommand.InteractiveHandlerAllows configure rebase interactive process and modify commit messagestatic classRebaseCommand.OperationThe available operationsprivate static classRebaseCommand.RebaseState
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringAMENDprivate static java.lang.StringAUTHOR_SCRIPTprivate static java.lang.StringAUTOSTASHprivate static java.lang.StringAUTOSTASH_MSGprivate static java.lang.StringCURRENT_COMMITFile containing the current commit(s) to cherry pick when --preserve-merges is used.private static java.lang.StringDONEprivate static java.lang.StringGIT_AUTHOR_DATEprivate static java.lang.StringGIT_AUTHOR_EMAILprivate static java.lang.StringGIT_AUTHOR_NAMEprivate static java.lang.StringGIT_REBASE_TODOprivate static java.lang.StringHEAD_NAMEprivate static java.lang.StringINTERACTIVEprivate RebaseCommand.InteractiveHandlerinteractiveHandlerprivate booleanlastStepWasForwardprivate static java.lang.StringMESSAGEprivate static java.lang.StringMESSAGE_FIXUPprivate static java.lang.StringMESSAGE_SQUASHprivate ProgressMonitormonitorprivate RevCommitnewHeadprivate static java.lang.StringONTOprivate static java.lang.StringONTO_NAMEprivate RebaseCommand.Operationoperationprivate static java.lang.StringPATCHprivate booleanpreserveMergesprivate static java.lang.StringQUIETprivate static java.lang.StringREBASE_APPLYThe name of the "rebase-apply" folder for non-interactive rebases.private static java.lang.StringREBASE_HEADprivate static java.lang.StringREBASE_HEAD_LEGACYPre git 1.7.6 file name forREBASE_HEAD.static java.lang.StringREBASE_MERGEThe name of the "rebase-merge" folder for interactive rebases.private RebaseCommand.RebaseStaterebaseStateprivate static java.lang.StringREFLOG_PREFIXprivate static java.lang.StringREWRITTENThe folder containing the hashes of (potentially) rewritten commits when --preserve-merges is used.private booleanstopAfterInitializationstatic java.lang.StringSTOPPED_SHAThe name of the "stopped-sha" fileprivate MergeStrategystrategyprivate RevCommitupstreamCommitprivate java.lang.StringupstreamCommitNameprivate RevWalkwalk-
Fields inherited from class org.eclipse.jgit.api.GitCommand
repo
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedRebaseCommand(Repository repo)Constructor for RebaseCommand.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private RebaseResultabort(RebaseResult result)private voidautoStash()private booleanautoStashApply()private java.util.List<RevCommit>calculatePickList(RevCommit headCommit)RebaseResultcall()private booleancheckoutCommit(java.lang.String headName, RevCommit commit)private RevCommitcheckoutCurrentHead()private voidcheckParameters()private voidcheckSteps(java.util.List<RebaseTodoLine> steps)private RebaseResultcherryPickCommit(RevCommit commitToPick)private RebaseResultcherryPickCommitFlattening(RevCommit commitToPick)private RebaseResultcherryPickCommitPreservingMerges(RevCommit commitToPick)private static java.lang.StringcomposeSquashMessage(boolean isSquash, RevCommit commitToPick, java.lang.String currSquashMessage, int count)private RevCommitcontinueRebase()private RevCommitdoSquashFixup(boolean isSquash, RevCommit commitToPick, RebaseTodoLine nextStep, java.io.File messageFixup, java.io.File messageSquash)private RebaseResultfinishRebase(RevCommit finalHead, boolean lastStepIsForward)private RefgetHead()private static java.lang.StringgetHeadName(Ref head)private java.util.List<RevCommit>getNewParents(RevCommit commitToPick)private static java.lang.StringgetOrdinal(int count)private ObjectIdgetOriginalHead()private java.lang.StringgetOurCommitName()private RebaseResultinitFilesAndRewind()private voidinitializeSquashFixupFile(java.lang.String messageFile, java.lang.String fullMessage)private booleanisInteractive()private PersonIdentparseAuthor()(package private) PersonIdentparseAuthor(byte[] raw)(package private) static intparseSquashFixupSequenceCount(java.lang.String currSquashMessage)Parse the count from squashed commit messagesprivate voidpopSteps(int numSteps)Removes the number of lines given in the parameter from thegit-rebase-todofile but preserves comments and other lines that can not be parsed as stepsprivate RebaseResultprocessStep(RebaseTodoLine step, boolean shouldPick)private voidresetSoftToParent()RebaseCommandrunInteractively(RebaseCommand.InteractiveHandler handler)Enable interactive rebaseRebaseCommandrunInteractively(RebaseCommand.InteractiveHandler handler, boolean stopAfterRebaseInteractiveInitialization)Enable interactive rebaseRebaseCommandsetOperation(RebaseCommand.Operation operation)Set the operation to execute during rebaseRebaseCommandsetPreserveMerges(boolean preserve)Whether to preserve merges during rebaseRebaseCommandsetProgressMonitor(ProgressMonitor monitor)Set progress monitorRebaseCommandsetStrategy(MergeStrategy strategy)Set theMergeStrategy.RebaseCommandsetUpstream(java.lang.String upstream)Set the upstream branchRebaseCommandsetUpstream(AnyObjectId upstream)Set the upstream commitRebaseCommandsetUpstream(RevCommit upstream)Set upstreamRevCommitRebaseCommandsetUpstreamName(java.lang.String upstreamName)Optionally override the name of the upstream.private RevCommitsquashIntoPrevious(boolean sequenceContainsSquash, RebaseTodoLine nextStep)private RebaseResultstop(RevCommit commitToPick, RebaseResult.Status status)private static java.lang.StringstripCommentLines(java.lang.String commitMessage)(package private) java.lang.StringtoAuthorScript(PersonIdent author)private RevCommittryFastForward(java.lang.String headName, RevCommit oldCommit, RevCommit newCommit)RevCommittryFastForward(RevCommit newCommit)Check if we can fast-forward and returns the new head if it is possibleprivate voidupdateHead(java.lang.String headName, RevCommit aNewHead, RevCommit onto)private voidupdateStashRef(ObjectId commitId, PersonIdent refLogIdent, java.lang.String refLogMessage)private voidwriteCurrentCommit(RevCommit commit)private voidwriteMergeInfo(RevCommit commitToPick, java.util.List<RevCommit> newParents)private voidwriteRewrittenHashes()-
Methods inherited from class org.eclipse.jgit.api.GitCommand
checkCallable, getRepository, setCallable
-
-
-
-
Field Detail
-
REBASE_MERGE
public static final java.lang.String REBASE_MERGE
The name of the "rebase-merge" folder for interactive rebases.- See Also:
- Constant Field Values
-
REBASE_APPLY
private static final java.lang.String REBASE_APPLY
The name of the "rebase-apply" folder for non-interactive rebases.- See Also:
- Constant Field Values
-
STOPPED_SHA
public static final java.lang.String STOPPED_SHA
The name of the "stopped-sha" file- See Also:
- Constant Field Values
-
AUTHOR_SCRIPT
private static final java.lang.String AUTHOR_SCRIPT
- See Also:
- Constant Field Values
-
DONE
private static final java.lang.String DONE
- See Also:
- Constant Field Values
-
GIT_AUTHOR_DATE
private static final java.lang.String GIT_AUTHOR_DATE
- See Also:
- Constant Field Values
-
GIT_AUTHOR_EMAIL
private static final java.lang.String GIT_AUTHOR_EMAIL
- See Also:
- Constant Field Values
-
GIT_AUTHOR_NAME
private static final java.lang.String GIT_AUTHOR_NAME
- See Also:
- Constant Field Values
-
GIT_REBASE_TODO
private static final java.lang.String GIT_REBASE_TODO
- See Also:
- Constant Field Values
-
HEAD_NAME
private static final java.lang.String HEAD_NAME
- See Also:
- Constant Field Values
-
INTERACTIVE
private static final java.lang.String INTERACTIVE
- See Also:
- Constant Field Values
-
QUIET
private static final java.lang.String QUIET
- See Also:
- Constant Field Values
-
MESSAGE
private static final java.lang.String MESSAGE
- See Also:
- Constant Field Values
-
ONTO
private static final java.lang.String ONTO
- See Also:
- Constant Field Values
-
ONTO_NAME
private static final java.lang.String ONTO_NAME
- See Also:
- Constant Field Values
-
PATCH
private static final java.lang.String PATCH
- See Also:
- Constant Field Values
-
REBASE_HEAD
private static final java.lang.String REBASE_HEAD
- See Also:
- Constant Field Values
-
REBASE_HEAD_LEGACY
private static final java.lang.String REBASE_HEAD_LEGACY
Pre git 1.7.6 file name forREBASE_HEAD.- See Also:
- Constant Field Values
-
AMEND
private static final java.lang.String AMEND
- See Also:
- Constant Field Values
-
MESSAGE_FIXUP
private static final java.lang.String MESSAGE_FIXUP
- See Also:
- Constant Field Values
-
MESSAGE_SQUASH
private static final java.lang.String MESSAGE_SQUASH
- See Also:
- Constant Field Values
-
AUTOSTASH
private static final java.lang.String AUTOSTASH
- See Also:
- Constant Field Values
-
AUTOSTASH_MSG
private static final java.lang.String AUTOSTASH_MSG
- See Also:
- Constant Field Values
-
REWRITTEN
private static final java.lang.String REWRITTEN
The folder containing the hashes of (potentially) rewritten commits when --preserve-merges is used.Native git rebase --merge uses a file of that name to record commits to copy notes at the end of the whole rebase.
- See Also:
- Constant Field Values
-
CURRENT_COMMIT
private static final java.lang.String CURRENT_COMMIT
File containing the current commit(s) to cherry pick when --preserve-merges is used.- See Also:
- Constant Field Values
-
REFLOG_PREFIX
private static final java.lang.String REFLOG_PREFIX
- See Also:
- Constant Field Values
-
operation
private RebaseCommand.Operation operation
-
upstreamCommit
private RevCommit upstreamCommit
-
upstreamCommitName
private java.lang.String upstreamCommitName
-
monitor
private ProgressMonitor monitor
-
walk
private final RevWalk walk
-
rebaseState
private final RebaseCommand.RebaseState rebaseState
-
interactiveHandler
private RebaseCommand.InteractiveHandler interactiveHandler
-
stopAfterInitialization
private boolean stopAfterInitialization
-
newHead
private RevCommit newHead
-
lastStepWasForward
private boolean lastStepWasForward
-
strategy
private MergeStrategy strategy
-
preserveMerges
private boolean preserveMerges
-
-
Constructor Detail
-
RebaseCommand
protected RebaseCommand(Repository repo)
Constructor for RebaseCommand.
- Parameters:
repo- theRepository
-
-
Method Detail
-
call
public RebaseResult call() throws GitAPIException, NoHeadException, RefNotFoundException, WrongRepositoryStateException
Execute the command
Executes the
Rebasecommand 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. Don't call this method twice on an instance.- Specified by:
callin interfacejava.util.concurrent.Callable<RebaseResult>- Specified by:
callin classGitCommand<RebaseResult>- Throws:
GitAPIExceptionNoHeadExceptionRefNotFoundExceptionWrongRepositoryStateException
-
autoStash
private void autoStash() throws GitAPIException, java.io.IOException- Throws:
GitAPIExceptionjava.io.IOException
-
autoStashApply
private boolean autoStashApply() throws java.io.IOException, GitAPIException- Throws:
java.io.IOExceptionGitAPIException
-
updateStashRef
private void updateStashRef(ObjectId commitId, PersonIdent refLogIdent, java.lang.String refLogMessage) throws java.io.IOException
- Throws:
java.io.IOException
-
processStep
private RebaseResult processStep(RebaseTodoLine step, boolean shouldPick) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
cherryPickCommit
private RebaseResult cherryPickCommit(RevCommit commitToPick) throws java.io.IOException, GitAPIException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, NoHeadException
- Throws:
java.io.IOExceptionGitAPIExceptionNoMessageExceptionUnmergedPathsExceptionConcurrentRefUpdateExceptionWrongRepositoryStateExceptionNoHeadException
-
cherryPickCommitFlattening
private RebaseResult cherryPickCommitFlattening(RevCommit commitToPick) throws java.io.IOException, GitAPIException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, NoHeadException
- Throws:
java.io.IOExceptionGitAPIExceptionNoMessageExceptionUnmergedPathsExceptionConcurrentRefUpdateExceptionWrongRepositoryStateExceptionNoHeadException
-
cherryPickCommitPreservingMerges
private RebaseResult cherryPickCommitPreservingMerges(RevCommit commitToPick) throws java.io.IOException, GitAPIException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, NoHeadException
- Throws:
java.io.IOExceptionGitAPIExceptionNoMessageExceptionUnmergedPathsExceptionConcurrentRefUpdateExceptionWrongRepositoryStateExceptionNoHeadException
-
writeMergeInfo
private void writeMergeInfo(RevCommit commitToPick, java.util.List<RevCommit> newParents) throws java.io.IOException
- Throws:
java.io.IOException
-
getNewParents
private java.util.List<RevCommit> getNewParents(RevCommit commitToPick) throws java.io.IOException
- Throws:
java.io.IOException
-
writeCurrentCommit
private void writeCurrentCommit(RevCommit commit) throws java.io.IOException
- Throws:
java.io.IOException
-
writeRewrittenHashes
private void writeRewrittenHashes() throws RevisionSyntaxException, java.io.IOException, RefNotFoundException- Throws:
RevisionSyntaxExceptionjava.io.IOExceptionRefNotFoundException
-
finishRebase
private RebaseResult finishRebase(RevCommit finalHead, boolean lastStepIsForward) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
checkSteps
private void checkSteps(java.util.List<RebaseTodoLine> steps) throws InvalidRebaseStepException, java.io.IOException
- Throws:
InvalidRebaseStepExceptionjava.io.IOException
-
doSquashFixup
private RevCommit doSquashFixup(boolean isSquash, RevCommit commitToPick, RebaseTodoLine nextStep, java.io.File messageFixup, java.io.File messageSquash) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
resetSoftToParent
private void resetSoftToParent() throws java.io.IOException, GitAPIException, CheckoutConflictException- Throws:
java.io.IOExceptionGitAPIExceptionCheckoutConflictException
-
squashIntoPrevious
private RevCommit squashIntoPrevious(boolean sequenceContainsSquash, RebaseTodoLine nextStep) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
stripCommentLines
private static java.lang.String stripCommentLines(java.lang.String commitMessage)
-
composeSquashMessage
private static java.lang.String composeSquashMessage(boolean isSquash, RevCommit commitToPick, java.lang.String currSquashMessage, int count)
-
getOrdinal
private static java.lang.String getOrdinal(int count)
-
parseSquashFixupSequenceCount
static int parseSquashFixupSequenceCount(java.lang.String currSquashMessage)
Parse the count from squashed commit messages- Parameters:
currSquashMessage- the squashed commit message to be parsed- Returns:
- the count of squashed messages in the given string
-
initializeSquashFixupFile
private void initializeSquashFixupFile(java.lang.String messageFile, java.lang.String fullMessage) throws java.io.IOException- Throws:
java.io.IOException
-
getOurCommitName
private java.lang.String getOurCommitName()
-
updateHead
private void updateHead(java.lang.String headName, RevCommit aNewHead, RevCommit onto) throws java.io.IOException- Throws:
java.io.IOException
-
checkoutCurrentHead
private RevCommit checkoutCurrentHead() throws java.io.IOException, NoHeadException
- Throws:
java.io.IOExceptionNoHeadException
-
continueRebase
private RevCommit continueRebase() throws GitAPIException, java.io.IOException
- Returns:
- the commit if we had to do a commit, otherwise null
- Throws:
GitAPIExceptionjava.io.IOException
-
parseAuthor
private PersonIdent parseAuthor() throws java.io.IOException
- Throws:
java.io.IOException
-
stop
private RebaseResult stop(RevCommit commitToPick, RebaseResult.Status status) throws java.io.IOException
- Throws:
java.io.IOException
-
toAuthorScript
java.lang.String toAuthorScript(PersonIdent author)
-
popSteps
private void popSteps(int numSteps) throws java.io.IOExceptionRemoves the number of lines given in the parameter from thegit-rebase-todofile but preserves comments and other lines that can not be parsed as steps- Parameters:
numSteps-- Throws:
java.io.IOException
-
initFilesAndRewind
private RebaseResult initFilesAndRewind() throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
calculatePickList
private java.util.List<RevCommit> calculatePickList(RevCommit headCommit) throws GitAPIException, NoHeadException, java.io.IOException
- Throws:
GitAPIExceptionNoHeadExceptionjava.io.IOException
-
getHeadName
private static java.lang.String getHeadName(Ref head)
-
getHead
private Ref getHead() throws java.io.IOException, RefNotFoundException
- Throws:
java.io.IOExceptionRefNotFoundException
-
isInteractive
private boolean isInteractive()
-
tryFastForward
public RevCommit tryFastForward(RevCommit newCommit) throws java.io.IOException, GitAPIException
Check if we can fast-forward and returns the new head if it is possible- Parameters:
newCommit- aRevCommitobject to check if we can fast-forward to.- Returns:
- the new head, or null
- Throws:
java.io.IOExceptionGitAPIException
-
tryFastForward
private RevCommit tryFastForward(java.lang.String headName, RevCommit oldCommit, RevCommit newCommit) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
checkParameters
private void checkParameters() throws WrongRepositoryStateException- Throws:
WrongRepositoryStateException
-
abort
private RebaseResult abort(RebaseResult result) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOExceptionGitAPIException
-
getOriginalHead
private ObjectId getOriginalHead() throws java.io.IOException
- Throws:
java.io.IOException
-
checkoutCommit
private boolean checkoutCommit(java.lang.String headName, RevCommit commit) throws java.io.IOException, CheckoutConflictException- Throws:
java.io.IOExceptionCheckoutConflictException
-
setUpstream
public RebaseCommand setUpstream(RevCommit upstream)
Set upstreamRevCommit- Parameters:
upstream- the upstream commit- Returns:
this
-
setUpstream
public RebaseCommand setUpstream(AnyObjectId upstream)
Set the upstream commit- Parameters:
upstream- id of the upstream commit- Returns:
this
-
setUpstream
public RebaseCommand setUpstream(java.lang.String upstream) throws RefNotFoundException
Set the upstream branch- Parameters:
upstream- the name of the upstream branch- Returns:
this- Throws:
RefNotFoundException
-
setUpstreamName
public RebaseCommand setUpstreamName(java.lang.String upstreamName)
Optionally override the name of the upstream. If this is used, it has to come after anysetUpstream(org.eclipse.jgit.revwalk.RevCommit)call.- Parameters:
upstreamName- the name which will be used to refer to upstream in conflicts- Returns:
this
-
setOperation
public RebaseCommand setOperation(RebaseCommand.Operation operation)
Set the operation to execute during rebase- Parameters:
operation- the operation to perform- Returns:
this
-
setProgressMonitor
public RebaseCommand setProgressMonitor(ProgressMonitor monitor)
Set progress monitor- Parameters:
monitor- a progress monitor- Returns:
- this instance
-
runInteractively
public RebaseCommand runInteractively(RebaseCommand.InteractiveHandler handler)
Enable interactive rebaseDoes not stop after initialization of interactive rebase. This is equivalent to
runInteractively(handler, false);- Parameters:
handler- theRebaseCommand.InteractiveHandlerto use- Returns:
- this
-
runInteractively
public RebaseCommand runInteractively(RebaseCommand.InteractiveHandler handler, boolean stopAfterRebaseInteractiveInitialization)
Enable interactive rebaseIf stopAfterRebaseInteractiveInitialization is
truethe rebase stops after initialization of interactive rebase returningRebaseResult.INTERACTIVE_PREPARED_RESULT- Parameters:
handler- theRebaseCommand.InteractiveHandlerto usestopAfterRebaseInteractiveInitialization- iftruethe rebase stops after initialization- Returns:
- this instance
- Since:
- 3.2
-
setStrategy
public RebaseCommand setStrategy(MergeStrategy strategy)
Set theMergeStrategy.- Parameters:
strategy- The merge strategy to use during this rebase operation.- Returns:
this- Since:
- 3.4
-
setPreserveMerges
public RebaseCommand setPreserveMerges(boolean preserve)
Whether to preserve merges during rebase- Parameters:
preserve-trueto re-create merges during rebase. Defaults tofalse, a flattening rebase.- Returns:
this- Since:
- 3.5
-
parseAuthor
PersonIdent parseAuthor(byte[] raw)
-
-