Search in sources :

Example 1 with RebaseChangeOp

use of com.google.gerrit.server.change.RebaseChangeOp in project gerrit by GerritCodeReview.

the class Rebase method apply.

@Override
public Response<ChangeInfo> apply(RevisionResource rsrc, RebaseInput input) throws UpdateException, RestApiException, IOException, PermissionBackendException {
    // Not allowed to rebase if the current patch set is locked.
    patchSetUtil.checkPatchSetNotLocked(rsrc.getNotes());
    rsrc.permissions().check(ChangePermission.REBASE);
    projectCache.get(rsrc.getProject()).orElseThrow(illegalState(rsrc.getProject())).checkStatePermitsWrite();
    Change change = rsrc.getChange();
    try (Repository repo = repoManager.openRepository(change.getProject());
        ObjectInserter oi = repo.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk rw = CodeReviewCommit.newRevWalk(reader);
        BatchUpdate bu = updateFactory.create(change.getProject(), rsrc.getUser(), TimeUtil.now())) {
        if (!change.isNew()) {
            throw new ResourceConflictException("change is " + ChangeUtil.status(change));
        } else if (!hasOneParent(rw, rsrc.getPatchSet())) {
            throw new ResourceConflictException("cannot rebase merge commits or commit with no ancestor");
        }
        RebaseChangeOp rebaseOp = rebaseFactory.create(rsrc.getNotes(), rsrc.getPatchSet(), findBaseRev(repo, rw, rsrc, input)).setForceContentMerge(true).setAllowConflicts(input.allowConflicts).setValidationOptions(getValidateOptionsAsMultimap(input.validationOptions)).setFireRevisionCreated(true);
        // TODO(dborowitz): Why no notification? This seems wrong; dig up blame.
        bu.setNotify(NotifyResolver.Result.none());
        bu.setRepository(repo, rw, oi);
        bu.addOp(change.getId(), rebaseOp);
        bu.execute();
        ChangeInfo changeInfo = json.create(OPTIONS).format(change.getProject(), change.getId());
        changeInfo.containsGitConflicts = !rebaseOp.getRebasedCommit().getFilesWithGitConflicts().isEmpty() ? true : null;
        return Response.ok(changeInfo);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Change(com.google.gerrit.entities.Change) RebaseChangeOp(com.google.gerrit.server.change.RebaseChangeOp) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchUpdate(com.google.gerrit.server.update.BatchUpdate)

Aggregations

Change (com.google.gerrit.entities.Change)1 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 RebaseChangeOp (com.google.gerrit.server.change.RebaseChangeOp)1 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)1 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 Repository (org.eclipse.jgit.lib.Repository)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1