@Override public boolean rebase(String upStreamBranchName) { RebaseCommand command = _git.rebase(); RebaseResult result = null; try { command.setUpstream(upStreamBranchName); result = command.call(); // if there are merge conflicts (rebase interactive) - reset the repository if (!result.getStatus().isSuccessful()) { _git.rebase().setOperation(Operation.ABORT).call(); } } catch (Throwable e) { throw new RuntimeException(String.format( "Failed to rebase with upstream [%s]", upStreamBranchName), e); } return result.getStatus().isSuccessful(); }
static Operation getOperation (GitClient.RebaseOperationType operation) { return Operation.valueOf(operation.name()); }
@SneakyThrows public boolean rebase( final Repository repo, final PullRequest pullRequest ) { log.info( "Rebasing {}.", pullRequest ); try { final Git repository = repo.getGit(); final CredentialsProvider credentials = repo.getCredentials(); repository.fetch().setCredentialsProvider( credentials ).setRemoveDeletedRefs( true ).call(); repository.checkout().setCreateBranch( true ).setName( pullRequest.getSource() ) .setStartPoint( "origin/" + pullRequest.getSource() ).call(); final RebaseResult rebaseResult = repository.rebase().setUpstream( "origin/" + pullRequest.getDestination() ).call(); switch ( rebaseResult.getStatus() ) { case UP_TO_DATE: log.warn( "Why rebasing up to date {}?", pullRequest ); return true; case FAST_FORWARD: log.warn( "Why creating {} without changes?", pullRequest ); // fall-through case OK: repository.push().setCredentialsProvider( credentials ).setForce( true ).call(); return true; case STOPPED: log.info( "Merge conflict in {}", pullRequest ); repository.rebase().setOperation( Operation.ABORT ).call(); return false; default: repository.rebase().setOperation( Operation.ABORT ).call(); throw new RuntimeException( "For " + pullRequest + " rebase causes an unexpected result: " + rebaseResult.getStatus() ); } } finally { cleanUp( repo ); } }
private void abortRebase(Git git) throws Exception { git.rebase().setOperation(Operation.ABORT) .setProgressMonitor(new NotificationProgressMonitor(request, notification)) .call(); }