Merge "Reject submitting change if its destination branch does not exist"
This commit is contained in:
@@ -152,7 +152,8 @@ public class ChangeDetailFactory extends Handler<ChangeDetail> {
|
||||
}
|
||||
if (detail.getChange().getStatus().isOpen()
|
||||
&& rec.status == SubmitRecord.Status.OK
|
||||
&& control.getRefControl().canSubmit()) {
|
||||
&& control.getRefControl().canSubmit()
|
||||
&& ProjectUtil.branchExists(repoManager, change.getDest())) {
|
||||
detail.setCanSubmit(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ import com.google.gerrit.reviewdb.client.PatchSetApproval;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.ChangeUtil;
|
||||
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.MergeQueue;
|
||||
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.assistedinject.Assisted;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@@ -49,6 +52,7 @@ public class Submit implements Callable<ReviewResult> {
|
||||
private final MergeOp.Factory opFactory;
|
||||
private final MergeQueue merger;
|
||||
private final ReviewDb db;
|
||||
private final GitRepositoryManager repoManager;
|
||||
private final IdentifiedUser currentUser;
|
||||
|
||||
private final PatchSet.Id patchSetId;
|
||||
@@ -56,12 +60,13 @@ public class Submit implements Callable<ReviewResult> {
|
||||
@Inject
|
||||
Submit(final ChangeControl.Factory changeControlFactory,
|
||||
final MergeOp.Factory opFactory, final MergeQueue merger,
|
||||
final ReviewDb db, final IdentifiedUser currentUser,
|
||||
@Assisted final PatchSet.Id patchSetId) {
|
||||
final ReviewDb db, final GitRepositoryManager repoManager,
|
||||
final IdentifiedUser currentUser, @Assisted final PatchSet.Id patchSetId) {
|
||||
this.changeControlFactory = changeControlFactory;
|
||||
this.opFactory = opFactory;
|
||||
this.merger = merger;
|
||||
this.db = db;
|
||||
this.repoManager = repoManager;
|
||||
this.currentUser = currentUser;
|
||||
|
||||
this.patchSetId = patchSetId;
|
||||
@@ -69,7 +74,8 @@ public class Submit implements Callable<ReviewResult> {
|
||||
|
||||
@Override
|
||||
public ReviewResult call() throws IllegalStateException,
|
||||
InvalidChangeOperationException, NoSuchChangeException, OrmException {
|
||||
InvalidChangeOperationException, NoSuchChangeException, OrmException,
|
||||
IOException {
|
||||
final ReviewResult result = new ReviewResult();
|
||||
|
||||
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
|
||||
if (result.getErrors().isEmpty()) {
|
||||
final List<PatchSetApproval> allApprovals =
|
||||
|
||||
@@ -355,6 +355,21 @@ public class MergeOp {
|
||||
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()) {
|
||||
if (r.getName().startsWith(Constants.R_HEADS)
|
||||
|| r.getName().startsWith(Constants.R_TAGS)) {
|
||||
|
||||
Reference in New Issue
Block a user