Search in sources :

Example 56 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class TestChanges method newPatchSet.

public static PatchSet newPatchSet(PatchSet.Id id, String revision, Account.Id userId) {
    PatchSet ps = new PatchSet(id);
    ps.setRevision(new RevId(revision));
    ps.setUploader(userId);
    ps.setCreatedOn(TimeUtil.nowTs());
    return ps;
}
Also used : PatchSet(com.google.gerrit.reviewdb.client.PatchSet) RevId(com.google.gerrit.reviewdb.client.RevId)

Example 57 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class ReviewCommand method run.

@Override
protected void run() throws UnloggedFailure {
    if (abandonChange) {
        if (restoreChange) {
            throw die("abandon and restore actions are mutually exclusive");
        }
        if (submitChange) {
            throw die("abandon and submit actions are mutually exclusive");
        }
        if (publishPatchSet) {
            throw die("abandon and publish actions are mutually exclusive");
        }
        if (deleteDraftPatchSet) {
            throw die("abandon and delete actions are mutually exclusive");
        }
        if (rebaseChange) {
            throw die("abandon and rebase actions are mutually exclusive");
        }
        if (moveToBranch != null) {
            throw die("abandon and move actions are mutually exclusive");
        }
    }
    if (publishPatchSet) {
        if (restoreChange) {
            throw die("publish and restore actions are mutually exclusive");
        }
        if (submitChange) {
            throw die("publish and submit actions are mutually exclusive");
        }
        if (deleteDraftPatchSet) {
            throw die("publish and delete actions are mutually exclusive");
        }
    }
    if (json) {
        if (restoreChange) {
            throw die("json and restore actions are mutually exclusive");
        }
        if (submitChange) {
            throw die("json and submit actions are mutually exclusive");
        }
        if (deleteDraftPatchSet) {
            throw die("json and delete actions are mutually exclusive");
        }
        if (publishPatchSet) {
            throw die("json and publish actions are mutually exclusive");
        }
        if (abandonChange) {
            throw die("json and abandon actions are mutually exclusive");
        }
        if (changeComment != null) {
            throw die("json and message are mutually exclusive");
        }
        if (rebaseChange) {
            throw die("json and rebase actions are mutually exclusive");
        }
        if (moveToBranch != null) {
            throw die("json and move actions are mutually exclusive");
        }
        if (changeTag != null) {
            throw die("json and tag actions are mutually exclusive");
        }
    }
    if (rebaseChange) {
        if (deleteDraftPatchSet) {
            throw die("rebase and delete actions are mutually exclusive");
        }
        if (submitChange) {
            throw die("rebase and submit actions are mutually exclusive");
        }
    }
    if (deleteDraftPatchSet && submitChange) {
        throw die("delete and submit actions are mutually exclusive");
    }
    boolean ok = true;
    ReviewInput input = null;
    if (json) {
        input = reviewFromJson();
    }
    for (final PatchSet patchSet : patchSets) {
        try {
            if (input != null) {
                applyReview(patchSet, input);
            } else {
                reviewPatchSet(patchSet);
            }
        } catch (RestApiException | UnloggedFailure e) {
            ok = false;
            writeError("error", e.getMessage() + "\n");
        } catch (NoSuchChangeException e) {
            ok = false;
            writeError("error", "no such change " + patchSet.getId().getParentKey().get());
        } catch (Exception e) {
            ok = false;
            writeError("fatal", "internal server error while reviewing " + patchSet.getId() + "\n");
            log.error("internal error while reviewing " + patchSet.getId(), e);
        }
    }
    if (!ok) {
        throw die("one or more reviews failed; review output above");
    }
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) OrmException(com.google.gwtorm.server.OrmException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) JsonSyntaxException(com.google.gson.JsonSyntaxException) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) IOException(java.io.IOException)

Example 58 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class RebaseUtil method findBaseRevision.

/**
   * Find the commit onto which a patch set should be rebased.
   *
   * <p>This is defined as the latest patch set of the change corresponding to this commit's parent,
   * or the destination branch tip in the case where the parent's change is merged.
   *
   * @param patchSet patch set for which the new base commit should be found.
   * @param destBranch the destination branch.
   * @param git the repository.
   * @param rw the RevWalk.
   * @return the commit onto which the patch set should be rebased.
   * @throws RestApiException if rebase is not possible.
   * @throws IOException if accessing the repository fails.
   * @throws OrmException if accessing the database fails.
   */
ObjectId findBaseRevision(PatchSet patchSet, Branch.NameKey destBranch, Repository git, RevWalk rw) throws RestApiException, IOException, OrmException {
    String baseRev = null;
    RevCommit commit = rw.parseCommit(ObjectId.fromString(patchSet.getRevision().get()));
    if (commit.getParentCount() > 1) {
        throw new UnprocessableEntityException("Cannot rebase a change with multiple parents.");
    } else if (commit.getParentCount() == 0) {
        throw new UnprocessableEntityException("Cannot rebase a change without any parents (is this the initial commit?).");
    }
    RevId parentRev = new RevId(commit.getParent(0).name());
    CHANGES: for (ChangeData cd : queryProvider.get().byBranchCommit(destBranch, parentRev.get())) {
        for (PatchSet depPatchSet : cd.patchSets()) {
            if (!depPatchSet.getRevision().equals(parentRev)) {
                continue;
            }
            Change depChange = cd.change();
            if (depChange.getStatus() == Status.ABANDONED) {
                throw new ResourceConflictException("Cannot rebase a change with an abandoned parent: " + depChange.getKey());
            }
            if (depChange.getStatus().isOpen()) {
                if (depPatchSet.getId().equals(depChange.currentPatchSetId())) {
                    throw new ResourceConflictException("Change is already based on the latest patch set of the dependent change.");
                }
                baseRev = cd.currentPatchSet().getRevision().get();
            }
            break CHANGES;
        }
    }
    if (baseRev == null) {
        // We are dependent on a merged PatchSet or have no PatchSet
        // dependencies at all.
        Ref destRef = git.getRefDatabase().exactRef(destBranch.get());
        if (destRef == null) {
            throw new UnprocessableEntityException("The destination branch does not exist: " + destBranch.get());
        }
        baseRev = destRef.getObjectId().getName();
        if (baseRev.equals(parentRev.get())) {
            throw new ResourceConflictException("Change is already up to date.");
        }
    }
    return ObjectId.fromString(baseRev);
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Ref(org.eclipse.jgit.lib.Ref) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RevId(com.google.gerrit.reviewdb.client.RevId) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 59 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class RelatedChangesSorter method collectById.

private Map<String, PatchSetData> collectById(List<ChangeData> in) throws OrmException, IOException {
    Project.NameKey project = in.get(0).change().getProject();
    Map<String, PatchSetData> result = Maps.newHashMapWithExpectedSize(in.size() * 3);
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        rw.setRetainBody(true);
        for (ChangeData cd : in) {
            checkArgument(cd.change().getProject().equals(project), "Expected change %s in project %s, found %s", cd.getId(), project, cd.change().getProject());
            for (PatchSet ps : cd.patchSets()) {
                String id = ps.getRevision().get();
                RevCommit c = rw.parseCommit(ObjectId.fromString(id));
                PatchSetData psd = PatchSetData.create(cd, ps, c);
                result.put(id, psd);
            }
        }
    }
    return result;
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 60 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class Revert method revert.

private Change.Id revert(BatchUpdate.Factory updateFactory, ChangeControl ctl, String message) throws OrmException, IOException, RestApiException, UpdateException {
    Change.Id changeIdToRevert = ctl.getChange().getId();
    PatchSet.Id patchSetId = ctl.getChange().currentPatchSetId();
    PatchSet patch = psUtil.get(db.get(), ctl.getNotes(), patchSetId);
    if (patch == null) {
        throw new ResourceNotFoundException(changeIdToRevert.toString());
    }
    Project.NameKey project = ctl.getProject().getNameKey();
    CurrentUser user = ctl.getUser();
    try (Repository git = repoManager.openRepository(project);
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk revWalk = new RevWalk(reader)) {
        RevCommit commitToRevert = revWalk.parseCommit(ObjectId.fromString(patch.getRevision().get()));
        if (commitToRevert.getParentCount() == 0) {
            throw new ResourceConflictException("Cannot revert initial commit");
        }
        Timestamp now = TimeUtil.nowTs();
        PersonIdent committerIdent = new PersonIdent(serverIdent, now);
        PersonIdent authorIdent = user.asIdentifiedUser().newCommitterIdent(now, committerIdent.getTimeZone());
        RevCommit parentToCommitToRevert = commitToRevert.getParent(0);
        revWalk.parseHeaders(parentToCommitToRevert);
        CommitBuilder revertCommitBuilder = new CommitBuilder();
        revertCommitBuilder.addParentId(commitToRevert);
        revertCommitBuilder.setTreeId(parentToCommitToRevert.getTree());
        revertCommitBuilder.setAuthor(authorIdent);
        revertCommitBuilder.setCommitter(authorIdent);
        Change changeToRevert = ctl.getChange();
        if (message == null) {
            message = MessageFormat.format(ChangeMessages.get().revertChangeDefaultMessage, changeToRevert.getSubject(), patch.getRevision().get());
        }
        ObjectId computedChangeId = ChangeIdUtil.computeChangeId(parentToCommitToRevert.getTree(), commitToRevert, authorIdent, committerIdent, message);
        revertCommitBuilder.setMessage(ChangeIdUtil.insertId(message, computedChangeId, true));
        Change.Id changeId = new Change.Id(seq.nextChangeId());
        ObjectId id = oi.insert(revertCommitBuilder);
        RevCommit revertCommit = revWalk.parseCommit(id);
        ChangeInserter ins = changeInserterFactory.create(changeId, revertCommit, ctl.getChange().getDest().get()).setTopic(changeToRevert.getTopic());
        ins.setMessage("Uploaded patch set 1.");
        Set<Account.Id> reviewers = new HashSet<>();
        reviewers.add(changeToRevert.getOwner());
        reviewers.addAll(approvalsUtil.getReviewers(db.get(), ctl.getNotes()).all());
        reviewers.remove(user.getAccountId());
        ins.setReviewers(reviewers);
        try (BatchUpdate bu = updateFactory.create(db.get(), project, user, now)) {
            bu.setRepository(git, revWalk, oi);
            bu.insertChange(ins);
            bu.addOp(changeId, new NotifyOp(ctl.getChange(), ins));
            bu.addOp(changeToRevert.getId(), new PostRevertedMessageOp(computedChangeId));
            bu.execute();
        }
        return changeId;
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(changeIdToRevert.toString(), e);
    }
}
Also used : CurrentUser(com.google.gerrit.server.CurrentUser) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Timestamp(java.sql.Timestamp) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) HashSet(java.util.HashSet) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ObjectId(org.eclipse.jgit.lib.ObjectId)

Aggregations

PatchSet (com.google.gerrit.reviewdb.client.PatchSet)124 Change (com.google.gerrit.reviewdb.client.Change)51 Test (org.junit.Test)44 ObjectId (org.eclipse.jgit.lib.ObjectId)35 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)27 RevCommit (org.eclipse.jgit.revwalk.RevCommit)26 Repository (org.eclipse.jgit.lib.Repository)21 RevId (com.google.gerrit.reviewdb.client.RevId)20 ChangeControl (com.google.gerrit.server.project.ChangeControl)20 ChangeData (com.google.gerrit.server.query.change.ChangeData)19 OrmException (com.google.gwtorm.server.OrmException)19 Timestamp (java.sql.Timestamp)18 RevWalk (org.eclipse.jgit.revwalk.RevWalk)15 Project (com.google.gerrit.reviewdb.client.Project)14 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)11 TestChanges.newPatchSet (com.google.gerrit.testutil.TestChanges.newPatchSet)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)10 Account (com.google.gerrit.reviewdb.client.Account)10