Fix index out of bounds exception when nothing able to merge

The IndexOutOfBoundsException could happen when all merge commits are
filtered out during the MergeSorter.sort() process due to missing
dependency.

Change-Id: Ibfaaccf70cd31f84ac5df5336eedc6de5cbd11fd
This commit is contained in:
Zhen Chen
2016-04-26 13:44:37 -07:00
parent a675549908
commit 42134d1a63
2 changed files with 23 additions and 0 deletions

View File

@@ -4,11 +4,14 @@ import static com.google.common.truth.Truth.assertThat;
import com.google.gerrit.acceptance.GitUtil;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.TestProjectInput;
import com.google.gerrit.extensions.api.changes.ChangeApi;
import com.google.gerrit.extensions.api.changes.CherryPickInput;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.api.changes.SubmitInput;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.client.SubmitType;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.reviewdb.client.Project;
import org.eclipse.jgit.junit.TestRepository;
@@ -414,4 +417,21 @@ public class SubmitByMergeIfNecessaryIT extends AbstractSubmitByMerge {
"Change " + change3.getPatchSetId().getParentKey().get() +
": depends on change that was not submitted");
}
@Test
@TestProjectInput(createEmptyCommit = false)
public void mergeWithMissingChange() throws Exception {
// create a draft change
PushOneCommit.Result draftResult = createDraftChange();
// create a new change based on the draft change
PushOneCommit.Result changeResult = createChange();
// delete the draft change
gApi.changes().id(draftResult.getChangeId()).delete();
// approve and submit the change
submit(changeResult.getChangeId(), new SubmitInput(),
ResourceConflictException.class, "nothing to merge", false);
}
}

View File

@@ -34,6 +34,9 @@ public class MergeIfNecessary extends SubmitStrategy {
List<SubmitStrategyOp> ops = new ArrayList<>(sorted.size());
CodeReviewCommit firstFastForward;
if (args.mergeTip.getInitialTip() == null) {
if (sorted.isEmpty()) {
throw new IntegrationException("nothing to merge");
}
firstFastForward = sorted.remove(0);
} else {
firstFastForward = args.mergeUtil.getFirstFastForward(