Merge "Reject submitting change if its destination branch does not exist"

This commit is contained in:
Shawn Pearce
2012-06-19 18:03:21 -07:00
committed by gerrit code review
3 changed files with 34 additions and 4 deletions

View File

@@ -152,7 +152,8 @@ public class ChangeDetailFactory extends Handler<ChangeDetail> {
} }
if (detail.getChange().getStatus().isOpen() if (detail.getChange().getStatus().isOpen()
&& rec.status == SubmitRecord.Status.OK && rec.status == SubmitRecord.Status.OK
&& control.getRefControl().canSubmit()) { && control.getRefControl().canSubmit()
&& ProjectUtil.branchExists(repoManager, change.getDest())) {
detail.setCanSubmit(true); detail.setCanSubmit(true);
} }
} }

View File

@@ -24,6 +24,8 @@ import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeUtil; import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.ProjectUtil;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.MergeOp; import com.google.gerrit.server.git.MergeOp;
import com.google.gerrit.server.git.MergeQueue; import com.google.gerrit.server.git.MergeQueue;
import com.google.gerrit.server.project.ChangeControl; import com.google.gerrit.server.project.ChangeControl;
@@ -34,6 +36,7 @@ import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted; import com.google.inject.assistedinject.Assisted;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -49,6 +52,7 @@ public class Submit implements Callable<ReviewResult> {
private final MergeOp.Factory opFactory; private final MergeOp.Factory opFactory;
private final MergeQueue merger; private final MergeQueue merger;
private final ReviewDb db; private final ReviewDb db;
private final GitRepositoryManager repoManager;
private final IdentifiedUser currentUser; private final IdentifiedUser currentUser;
private final PatchSet.Id patchSetId; private final PatchSet.Id patchSetId;
@@ -56,12 +60,13 @@ public class Submit implements Callable<ReviewResult> {
@Inject @Inject
Submit(final ChangeControl.Factory changeControlFactory, Submit(final ChangeControl.Factory changeControlFactory,
final MergeOp.Factory opFactory, final MergeQueue merger, final MergeOp.Factory opFactory, final MergeQueue merger,
final ReviewDb db, final IdentifiedUser currentUser, final ReviewDb db, final GitRepositoryManager repoManager,
@Assisted final PatchSet.Id patchSetId) { final IdentifiedUser currentUser, @Assisted final PatchSet.Id patchSetId) {
this.changeControlFactory = changeControlFactory; this.changeControlFactory = changeControlFactory;
this.opFactory = opFactory; this.opFactory = opFactory;
this.merger = merger; this.merger = merger;
this.db = db; this.db = db;
this.repoManager = repoManager;
this.currentUser = currentUser; this.currentUser = currentUser;
this.patchSetId = patchSetId; this.patchSetId = patchSetId;
@@ -69,7 +74,8 @@ public class Submit implements Callable<ReviewResult> {
@Override @Override
public ReviewResult call() throws IllegalStateException, public ReviewResult call() throws IllegalStateException,
InvalidChangeOperationException, NoSuchChangeException, OrmException { InvalidChangeOperationException, NoSuchChangeException, OrmException,
IOException {
final ReviewResult result = new ReviewResult(); final ReviewResult result = new ReviewResult();
final PatchSet patch = db.patchSets().get(patchSetId); final PatchSet patch = db.patchSets().get(patchSetId);
@@ -151,6 +157,14 @@ public class Submit implements Callable<ReviewResult> {
} }
} }
if (!ProjectUtil.branchExists(repoManager, control.getChange().getDest())) {
result.addError(new ReviewResult.Error(
ReviewResult.Error.Type.DEST_BRANCH_NOT_FOUND,
"Destination branch \"" + control.getChange().getDest().get()
+ "\" not found."));
return result;
}
// Submit the change if we can // Submit the change if we can
if (result.getErrors().isEmpty()) { if (result.getErrors().isEmpty()) {
final List<PatchSetApproval> allApprovals = final List<PatchSetApproval> allApprovals =

View File

@@ -355,6 +355,21 @@ public class MergeOp {
branchTip = null; branchTip = null;
} }
try {
final Ref destRef = repo.getRef(destBranch.get());
if (destRef != null) {
branchUpdate.setExpectedOldObjectId(destRef.getObjectId());
} else if (repo.getFullBranch().equals(destBranch.get())) {
branchUpdate.setExpectedOldObjectId(ObjectId.zeroId());
} else {
throw new MergeException("Destination branch \""
+ branchUpdate.getRef().getName() + "\" does not exist");
}
} catch (IOException e) {
throw new MergeException(
"Failed to check existence of destination branch", e);
}
for (final Ref r : repo.getAllRefs().values()) { for (final Ref r : repo.getAllRefs().values()) {
if (r.getName().startsWith(Constants.R_HEADS) if (r.getName().startsWith(Constants.R_HEADS)
|| r.getName().startsWith(Constants.R_TAGS)) { || r.getName().startsWith(Constants.R_TAGS)) {