use of com.google.gerrit.extensions.common.MergeInput in project gerrit by GerritCodeReview.
the class ChangeIT method createMergePatchSetInheritParent.
@Test
public void createMergePatchSetInheritParent() throws Exception {
PushOneCommit.Result start = pushTo("refs/heads/master");
start.assertOkStatus();
// create a change for master
PushOneCommit.Result r = createChange();
r.assertOkStatus();
String changeId = r.getChangeId();
String parent = r.getCommit().getParent(0).getName();
// advance master branch
testRepo.reset(start.getCommit());
PushOneCommit.Result currentMaster = pushTo("refs/heads/master");
currentMaster.assertOkStatus();
// push a commit into dev branch
createBranch(new Branch.NameKey(project, "dev"));
PushOneCommit.Result changeA = pushFactory.create(db, user.getIdent(), testRepo, "change A", "A.txt", "A content").to("refs/heads/dev");
changeA.assertOkStatus();
MergeInput mergeInput = new MergeInput();
mergeInput.source = "dev";
MergePatchSetInput in = new MergePatchSetInput();
in.merge = mergeInput;
in.subject = "update change by merge ps2 inherit parent of ps1";
in.inheritParent = true;
gApi.changes().id(changeId).createMergePatchSet(in);
ChangeInfo changeInfo = gApi.changes().id(changeId).get(EnumSet.of(ListChangesOption.ALL_REVISIONS, ListChangesOption.CURRENT_COMMIT, ListChangesOption.CURRENT_REVISION));
assertThat(changeInfo.revisions.size()).isEqualTo(2);
assertThat(changeInfo.subject).isEqualTo(in.subject);
assertThat(changeInfo.revisions.get(changeInfo.currentRevision).commit.parents.get(0).commit).isEqualTo(parent);
assertThat(changeInfo.revisions.get(changeInfo.currentRevision).commit.parents.get(0).commit).isNotEqualTo(currentMaster.getCommit().getName());
}
use of com.google.gerrit.extensions.common.MergeInput in project gerrit by GerritCodeReview.
the class CreateChangeIT method newMergeChangeInput.
private ChangeInput newMergeChangeInput(String targetBranch, String sourceRef, String strategy) {
// create a merge change from branchA to master in gerrit
ChangeInput in = new ChangeInput();
in.project = project.get();
in.branch = targetBranch;
in.subject = "merge " + sourceRef + " to " + targetBranch;
in.status = ChangeStatus.NEW;
MergeInput mergeInput = new MergeInput();
mergeInput.source = sourceRef;
in.merge = mergeInput;
if (!Strings.isNullOrEmpty(strategy)) {
in.merge.strategy = strategy;
}
return in;
}
use of com.google.gerrit.extensions.common.MergeInput in project gerrit by GerritCodeReview.
the class ChangeIT method createMergePatchSet.
@Test
public void createMergePatchSet() throws Exception {
PushOneCommit.Result start = pushTo("refs/heads/master");
start.assertOkStatus();
// create a change for master
PushOneCommit.Result r = createChange();
r.assertOkStatus();
String changeId = r.getChangeId();
testRepo.reset(start.getCommit());
PushOneCommit.Result currentMaster = pushTo("refs/heads/master");
currentMaster.assertOkStatus();
String parent = currentMaster.getCommit().getName();
// push a commit into dev branch
createBranch(new Branch.NameKey(project, "dev"));
PushOneCommit.Result changeA = pushFactory.create(db, user.getIdent(), testRepo, "change A", "A.txt", "A content").to("refs/heads/dev");
changeA.assertOkStatus();
MergeInput mergeInput = new MergeInput();
mergeInput.source = "dev";
MergePatchSetInput in = new MergePatchSetInput();
in.merge = mergeInput;
in.subject = "update change by merge ps2";
gApi.changes().id(changeId).createMergePatchSet(in);
ChangeInfo changeInfo = gApi.changes().id(changeId).get(EnumSet.of(ListChangesOption.ALL_REVISIONS, ListChangesOption.CURRENT_COMMIT, ListChangesOption.CURRENT_REVISION));
assertThat(changeInfo.revisions.size()).isEqualTo(2);
assertThat(changeInfo.subject).isEqualTo(in.subject);
assertThat(changeInfo.revisions.get(changeInfo.currentRevision).commit.parents.get(0).commit).isEqualTo(parent);
}
use of com.google.gerrit.extensions.common.MergeInput in project gerrit by GerritCodeReview.
the class CreateMergePatchSet method applyImpl.
@Override
protected Response<ChangeInfo> applyImpl(BatchUpdate.Factory updateFactory, ChangeResource rsrc, MergePatchSetInput in) throws OrmException, IOException, InvalidChangeOperationException, RestApiException, UpdateException, PermissionBackendException {
rsrc.permissions().database(db).check(ChangePermission.ADD_PATCH_SET);
MergeInput merge = in.merge;
if (merge == null || Strings.isNullOrEmpty(merge.source)) {
throw new BadRequestException("merge.source must be non-empty");
}
ChangeControl ctl = rsrc.getControl();
PatchSet ps = psUtil.current(db.get(), ctl.getNotes());
ProjectControl projectControl = ctl.getProjectControl();
Change change = ctl.getChange();
Project.NameKey project = change.getProject();
Branch.NameKey dest = change.getDest();
try (Repository git = gitManager.openRepository(project);
ObjectInserter oi = git.newObjectInserter();
ObjectReader reader = oi.newReader();
RevWalk rw = new RevWalk(reader)) {
RevCommit sourceCommit = MergeUtil.resolveCommit(git, rw, merge.source);
if (!projectControl.canReadCommit(db.get(), git, sourceCommit)) {
throw new ResourceNotFoundException("cannot find source commit: " + merge.source + " to merge.");
}
RevCommit currentPsCommit = rw.parseCommit(ObjectId.fromString(ps.getRevision().get()));
Timestamp now = TimeUtil.nowTs();
IdentifiedUser me = user.get().asIdentifiedUser();
PersonIdent author = me.newCommitterIdent(now, serverTimeZone);
RevCommit newCommit = createMergeCommit(in, projectControl, dest, git, oi, rw, currentPsCommit, sourceCommit, author, ObjectId.fromString(change.getKey().get().substring(1)));
PatchSet.Id nextPsId = ChangeUtil.nextPatchSetId(ps.getId());
PatchSetInserter psInserter = patchSetInserterFactory.create(ctl, nextPsId, newCommit);
try (BatchUpdate bu = updateFactory.create(db.get(), project, me, now)) {
bu.setRepository(git, rw, oi);
bu.addOp(ctl.getId(), psInserter.setMessage("Uploaded patch set " + nextPsId.get() + ".").setDraft(ps.isDraft()).setNotify(NotifyHandling.NONE).setCheckAddPatchSetPermission(false));
bu.execute();
}
ChangeJson json = jsonFactory.create(ListChangesOption.CURRENT_REVISION);
return Response.ok(json.format(psInserter.getChange()));
}
}
Aggregations