use of com.google.gerrit.reviewdb.client.Change 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);
}
use of com.google.gerrit.reviewdb.client.Change 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);
}
}
use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.
the class SubmittedTogether method applyInfo.
public SubmittedTogetherInfo applyInfo(ChangeResource resource) throws AuthException, IOException, OrmException {
Change c = resource.getChange();
try {
List<ChangeData> cds;
int hidden;
if (c.getStatus().isOpen()) {
ChangeSet cs = mergeSuperSet.get().completeChangeSet(dbProvider.get(), c, resource.getControl().getUser());
cds = cs.changes().asList();
hidden = cs.nonVisibleChanges().size();
} else if (c.getStatus().asChangeStatus() == ChangeStatus.MERGED) {
cds = queryProvider.get().bySubmissionId(c.getSubmissionId());
hidden = 0;
} else {
cds = Collections.emptyList();
hidden = 0;
}
if (hidden != 0 && !options.contains(NON_VISIBLE_CHANGES)) {
throw new AuthException("change would be submitted with a change that you cannot see");
}
if (cds.size() <= 1 && hidden == 0) {
cds = Collections.emptyList();
} else {
// Skip sorting for singleton lists, to avoid WalkSorter opening the
// repo just to fill out the commit field in PatchSetData.
cds = sort(cds);
}
SubmittedTogetherInfo info = new SubmittedTogetherInfo();
info.changes = json.create(jsonOpt).formatChangeDatas(cds);
info.nonVisibleChanges = hidden;
return info;
} catch (OrmException | IOException e) {
log.error("Error on getting a ChangeSet", e);
throw e;
}
}
use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.
the class WorkInProgressOp method addMessage.
private void addMessage(ChangeContext ctx, ChangeUpdate update) throws OrmException {
Change c = ctx.getChange();
StringBuilder buf = new StringBuilder(c.isWorkInProgress() ? "Set Work In Progress" : "Set Ready For Review");
String m = Strings.nullToEmpty(in == null ? null : in.message).trim();
if (!m.isEmpty()) {
buf.append("\n\n");
buf.append(m);
}
ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, buf.toString(), c.isWorkInProgress() ? ChangeMessagesUtil.TAG_SET_WIP : ChangeMessagesUtil.TAG_SET_READY);
cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
}
use of com.google.gerrit.reviewdb.client.Change in project gerrit by GerritCodeReview.
the class SetPrivateOp method addMessage.
private void addMessage(ChangeContext ctx, ChangeUpdate update) throws OrmException {
Change c = ctx.getChange();
StringBuilder buf = new StringBuilder(c.isPrivate() ? "Set private" : "Unset private");
String m = Strings.nullToEmpty(input == null ? null : input.message).trim();
if (!m.isEmpty()) {
buf.append("\n\n");
buf.append(m);
}
ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, buf.toString(), c.isPrivate() ? ChangeMessagesUtil.TAG_SET_PRIVATE : ChangeMessagesUtil.TAG_UNSET_PRIVATE);
cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
}
Aggregations