Fail with proper error when trying to revert initial commit

Trying the revert the very first change in a repository, when the
project was created without initial commit, failed with an Internal
Server Error. Instead return '409 Conflict' and inform the user with a
proper error message that this is not supported.

Change-Id: I3638ef9d9383d5cc583e1245c6c9d27e83820839
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2015-10-30 10:26:12 +01:00
parent d98efb657e
commit d218e4078c
2 changed files with 26 additions and 0 deletions

View File

@@ -29,6 +29,7 @@ import com.google.common.collect.Sets;
import com.google.gerrit.acceptance.AbstractDaemonTest; import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd; import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.PushOneCommit; import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.TestProjectInput;
import com.google.gerrit.common.data.LabelType; import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.Permission; import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.changes.AddReviewerInput; import com.google.gerrit.extensions.api.changes.AddReviewerInput;
@@ -130,6 +131,26 @@ public class ChangeIT extends AbstractDaemonTest {
.revert(); .revert();
} }
@Test
@TestProjectInput(createEmptyCommit = false)
public void revertInitialCommit() throws Exception {
PushOneCommit.Result r = createChange();
gApi.changes()
.id(r.getChangeId())
.revision(r.getCommit().name())
.review(ReviewInput.approve());
gApi.changes()
.id(r.getChangeId())
.revision(r.getCommit().name())
.submit();
exception.expect(ResourceConflictException.class);
exception.expectMessage("Cannot revert initial commit");
gApi.changes()
.id(r.getChangeId())
.revert();
}
// Change is already up to date // Change is already up to date
@Test(expected = ResourceConflictException.class) @Test(expected = ResourceConflictException.class)
public void rebase() throws Exception { public void rebase() throws Exception {

View File

@@ -21,6 +21,7 @@ import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import com.google.gerrit.common.TimeUtil; import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@@ -226,6 +227,10 @@ public class ChangeUtil {
PersonIdent authorIdent = user.get() PersonIdent authorIdent = user.get()
.newCommitterIdent(myIdent.getWhen(), myIdent.getTimeZone()); .newCommitterIdent(myIdent.getWhen(), myIdent.getTimeZone());
if (commitToRevert.getParentCount() == 0) {
throw new ResourceConflictException("Cannot revert initial commit");
}
RevCommit parentToCommitToRevert = commitToRevert.getParent(0); RevCommit parentToCommitToRevert = commitToRevert.getParent(0);
revWalk.parseHeaders(parentToCommitToRevert); revWalk.parseHeaders(parentToCommitToRevert);