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 aRebase
command. 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 interface
RebaseCommand.InteractiveHandler
Allows to configure the interactive rebase process steps and to modify commit messages.static interface
RebaseCommand.InteractiveHandler2
ExtendsRebaseCommand.InteractiveHandler
with an enhanced callback for editing commit messages.static class
RebaseCommand.Operation
The available operationsprivate static class
RebaseCommand.RebaseState
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
AMEND
private static java.lang.String
AUTHOR_SCRIPT
private static java.lang.String
AUTOSTASH
private static java.lang.String
AUTOSTASH_MSG
private CommitConfig
commitConfig
private ContentMergeStrategy
contentStrategy
private static java.lang.String
CURRENT_COMMIT
File containing the current commit(s) to cherry pick when --preserve-merges is used.private static java.lang.String
DONE
private static java.lang.String
GIT_AUTHOR_DATE
private static java.lang.String
GIT_AUTHOR_EMAIL
private static java.lang.String
GIT_AUTHOR_NAME
private static java.lang.String
GIT_REBASE_TODO
private static java.lang.String
HEAD_NAME
private static java.lang.String
INTERACTIVE
private RebaseCommand.InteractiveHandler
interactiveHandler
private boolean
lastStepWasForward
private static java.lang.String
MESSAGE
private static java.lang.String
MESSAGE_FIXUP
private static java.lang.String
MESSAGE_SQUASH
private ProgressMonitor
monitor
private RevCommit
newHead
private static java.lang.String
ONTO
private static java.lang.String
ONTO_NAME
private RebaseCommand.Operation
operation
private static java.lang.String
PATCH
private boolean
preserveMerges
private static java.lang.String
QUIET
private static java.lang.String
REBASE_APPLY
The name of the "rebase-apply" folder for non-interactive rebases.private static java.lang.String
REBASE_HEAD
private static java.lang.String
REBASE_HEAD_LEGACY
Pre git 1.7.6 file name forREBASE_HEAD
.static java.lang.String
REBASE_MERGE
The name of the "rebase-merge" folder for interactive rebases.private RebaseCommand.RebaseState
rebaseState
private static java.lang.String
REFLOG_PREFIX
private static java.lang.String
REWRITTEN
The folder containing the hashes of (potentially) rewritten commits when --preserve-merges is used.private boolean
stopAfterInitialization
static java.lang.String
STOPPED_SHA
The name of the "stopped-sha" fileprivate MergeStrategy
strategy
private RevCommit
upstreamCommit
private java.lang.String
upstreamCommitName
private RevWalk
walk
-
Fields inherited from class org.eclipse.jgit.api.GitCommand
repo
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
RebaseCommand(Repository repo)
Constructor for RebaseCommand.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private RebaseResult
abort(RebaseResult result)
private void
autoStash()
private boolean
autoStashApply()
private java.util.List<RevCommit>
calculatePickList(RevCommit headCommit)
RebaseResult
call()
private boolean
checkoutCommit(java.lang.String headName, RevCommit commit)
private RevCommit
checkoutCurrentHead()
private void
checkParameters()
private void
checkSteps(java.util.List<RebaseTodoLine> steps)
private RebaseResult
cherryPickCommit(RevCommit commitToPick)
private RebaseResult
cherryPickCommitFlattening(RevCommit commitToPick)
private RebaseResult
cherryPickCommitPreservingMerges(RevCommit commitToPick)
private static java.lang.String
composeSquashMessage(boolean isSquash, RevCommit commitToPick, java.lang.String currSquashMessage, int count)
private RevCommit
continueRebase()
private RevCommit
doSquashFixup(boolean isSquash, RevCommit commitToPick, boolean isLast, java.io.File messageFixup, java.io.File messageSquash)
private java.lang.String
editCommitMessage(boolean[] doChangeId, java.lang.String message, CommitConfig.CleanupMode mode)
private RebaseResult
finishRebase(RevCommit finalHead, boolean lastStepIsForward)
private Ref
getHead()
private static java.lang.String
getHeadName(Ref head)
private java.util.List<RevCommit>
getNewParents(RevCommit commitToPick)
private static java.lang.String
getOrdinal(int count)
private ObjectId
getOriginalHead()
private java.lang.String
getOurCommitName()
private RebaseResult
initFilesAndRewind()
private void
initializeSquashFixupFile(java.lang.String messageFile, java.lang.String fullMessage)
private boolean
isInteractive()
private PersonIdent
parseAuthor()
(package private) PersonIdent
parseAuthor(byte[] raw)
(package private) static int
parseSquashFixupSequenceCount(java.lang.String currSquashMessage)
Parse the count from squashed commit messagesprivate void
popSteps(int numSteps)
Removes the number of lines given in the parameter from thegit-rebase-todo
file but preserves comments and other lines that can not be parsed as stepsprivate RebaseResult
processStep(RebaseTodoLine step, boolean shouldPick)
private void
resetSoftToParent()
RebaseCommand
runInteractively(RebaseCommand.InteractiveHandler handler)
Enable interactive rebaseRebaseCommand
runInteractively(RebaseCommand.InteractiveHandler handler, boolean stopAfterRebaseInteractiveInitialization)
Enable interactive rebaseRebaseCommand
setContentMergeStrategy(ContentMergeStrategy strategy)
Sets the content merge strategy to use if themerge strategy
is "resolve" or "recursive".RebaseCommand
setOperation(RebaseCommand.Operation operation)
Set the operation to execute during rebaseRebaseCommand
setPreserveMerges(boolean preserve)
Whether to preserve merges during rebaseRebaseCommand
setProgressMonitor(ProgressMonitor monitor)
Set progress monitorRebaseCommand
setStrategy(MergeStrategy strategy)
Set theMergeStrategy
.RebaseCommand
setUpstream(java.lang.String upstream)
Set the upstream branchRebaseCommand
setUpstream(AnyObjectId upstream)
Set the upstream commitRebaseCommand
setUpstream(RevCommit upstream)
Set upstreamRevCommit
RebaseCommand
setUpstreamName(java.lang.String upstreamName)
Optionally override the name of the upstream.private RevCommit
squashIntoPrevious(boolean sequenceContainsSquash, boolean isLast)
private RebaseResult
stop(RevCommit commitToPick, RebaseResult.Status status)
(package private) java.lang.String
toAuthorScript(PersonIdent author)
private RevCommit
tryFastForward(java.lang.String headName, RevCommit oldCommit, RevCommit newCommit)
RevCommit
tryFastForward(RevCommit newCommit)
Check if we can fast-forward and returns the new head if it is possibleprivate void
updateHead(java.lang.String headName, RevCommit aNewHead, RevCommit onto)
private void
updateStashRef(ObjectId commitId, PersonIdent refLogIdent, java.lang.String refLogMessage)
private void
writeCurrentCommit(RevCommit commit)
private void
writeMergeInfo(RevCommit commitToPick, java.util.List<RevCommit> newParents)
private void
writeRewrittenHashes()
-
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
-
commitConfig
private CommitConfig commitConfig
-
stopAfterInitialization
private boolean stopAfterInitialization
-
newHead
private RevCommit newHead
-
lastStepWasForward
private boolean lastStepWasForward
-
strategy
private MergeStrategy strategy
-
contentStrategy
private ContentMergeStrategy contentStrategy
-
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
Rebase
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. Don't call this method twice on an instance.- Specified by:
call
in interfacejava.util.concurrent.Callable<RebaseResult>
- Specified by:
call
in classGitCommand<RebaseResult>
- Throws:
GitAPIException
NoHeadException
RefNotFoundException
WrongRepositoryStateException
-
autoStash
private void autoStash() throws GitAPIException, java.io.IOException
- Throws:
GitAPIException
java.io.IOException
-
autoStashApply
private boolean autoStashApply() throws java.io.IOException, GitAPIException
- Throws:
java.io.IOException
GitAPIException
-
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.IOException
GitAPIException
-
editCommitMessage
private java.lang.String editCommitMessage(boolean[] doChangeId, java.lang.String message, @NonNull CommitConfig.CleanupMode mode)
-
cherryPickCommit
private RebaseResult cherryPickCommit(RevCommit commitToPick) throws java.io.IOException, GitAPIException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, NoHeadException
- Throws:
java.io.IOException
GitAPIException
NoMessageException
UnmergedPathsException
ConcurrentRefUpdateException
WrongRepositoryStateException
NoHeadException
-
cherryPickCommitFlattening
private RebaseResult cherryPickCommitFlattening(RevCommit commitToPick) throws java.io.IOException, GitAPIException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, NoHeadException
- Throws:
java.io.IOException
GitAPIException
NoMessageException
UnmergedPathsException
ConcurrentRefUpdateException
WrongRepositoryStateException
NoHeadException
-
cherryPickCommitPreservingMerges
private RebaseResult cherryPickCommitPreservingMerges(RevCommit commitToPick) throws java.io.IOException, GitAPIException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException, WrongRepositoryStateException, NoHeadException
- Throws:
java.io.IOException
GitAPIException
NoMessageException
UnmergedPathsException
ConcurrentRefUpdateException
WrongRepositoryStateException
NoHeadException
-
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:
RevisionSyntaxException
java.io.IOException
RefNotFoundException
-
finishRebase
private RebaseResult finishRebase(RevCommit finalHead, boolean lastStepIsForward) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOException
GitAPIException
-
checkSteps
private void checkSteps(java.util.List<RebaseTodoLine> steps) throws InvalidRebaseStepException, java.io.IOException
- Throws:
InvalidRebaseStepException
java.io.IOException
-
doSquashFixup
private RevCommit doSquashFixup(boolean isSquash, RevCommit commitToPick, boolean isLast, java.io.File messageFixup, java.io.File messageSquash) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOException
GitAPIException
-
resetSoftToParent
private void resetSoftToParent() throws java.io.IOException, GitAPIException, CheckoutConflictException
- Throws:
java.io.IOException
GitAPIException
CheckoutConflictException
-
squashIntoPrevious
private RevCommit squashIntoPrevious(boolean sequenceContainsSquash, boolean isLast) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOException
GitAPIException
-
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.IOException
NoHeadException
-
continueRebase
private RevCommit continueRebase() throws GitAPIException, java.io.IOException
- Returns:
- the commit if we had to do a commit, otherwise null
- Throws:
GitAPIException
java.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.IOException
Removes the number of lines given in the parameter from thegit-rebase-todo
file 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.IOException
GitAPIException
-
calculatePickList
private java.util.List<RevCommit> calculatePickList(RevCommit headCommit) throws java.io.IOException
- Throws:
java.io.IOException
-
getHeadName
private static java.lang.String getHeadName(Ref head)
-
getHead
private Ref getHead() throws java.io.IOException, RefNotFoundException
- Throws:
java.io.IOException
RefNotFoundException
-
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
- aRevCommit
object to check if we can fast-forward to.- Returns:
- the new head, or null
- Throws:
java.io.IOException
GitAPIException
-
tryFastForward
private RevCommit tryFastForward(java.lang.String headName, RevCommit oldCommit, RevCommit newCommit) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOException
GitAPIException
-
checkParameters
private void checkParameters() throws WrongRepositoryStateException
- Throws:
WrongRepositoryStateException
-
abort
private RebaseResult abort(RebaseResult result) throws java.io.IOException, GitAPIException
- Throws:
java.io.IOException
GitAPIException
-
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.IOException
CheckoutConflictException
-
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.InteractiveHandler
to use- Returns:
- this
-
runInteractively
public RebaseCommand runInteractively(RebaseCommand.InteractiveHandler handler, boolean stopAfterRebaseInteractiveInitialization)
Enable interactive rebaseIf stopAfterRebaseInteractiveInitialization is
true
the rebase stops after initialization of interactive rebase returningRebaseResult.INTERACTIVE_PREPARED_RESULT
- Parameters:
handler
- theRebaseCommand.InteractiveHandler
to usestopAfterRebaseInteractiveInitialization
- iftrue
the 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
-
setContentMergeStrategy
public RebaseCommand setContentMergeStrategy(ContentMergeStrategy strategy)
Sets the content merge strategy to use if themerge strategy
is "resolve" or "recursive".- Parameters:
strategy
- theContentMergeStrategy
to be used- Returns:
this
- Since:
- 5.12
-
setPreserveMerges
public RebaseCommand setPreserveMerges(boolean preserve)
Whether to preserve merges during rebase- Parameters:
preserve
-true
to re-create merges during rebase. Defaults tofalse
, a flattening rebase.- Returns:
this
- Since:
- 3.5
-
parseAuthor
PersonIdent parseAuthor(byte[] raw)
-
-