Merge "Fix change stuck in SUBMITTED state but actually merged"

This commit is contained in:
Dave Borowitz
2013-08-15 17:16:46 +00:00
committed by Gerrit Code Review
4 changed files with 51 additions and 12 deletions

View File

@@ -171,9 +171,13 @@ public class GitUtil {
} }
} }
public static PushResult pushHead(Git git, String ref) throws GitAPIException { public static PushResult pushHead(Git git, String ref, boolean pushTags)
throws GitAPIException {
PushCommand pushCmd = git.push(); PushCommand pushCmd = git.push();
pushCmd.setRefSpecs(new RefSpec("HEAD:" + ref)); pushCmd.setRefSpecs(new RefSpec("HEAD:" + ref));
if (pushTags) {
pushCmd.setPushTags();
}
Iterable<PushResult> r = pushCmd.call(); Iterable<PushResult> r = pushCmd.call();
return Iterables.getOnlyElement(r); return Iterables.getOnlyElement(r);
} }

View File

@@ -58,6 +58,7 @@ public class PushOneCommit {
private final String fileName; private final String fileName;
private final String content; private final String content;
private String changeId; private String changeId;
private String tagName;
public PushOneCommit(ReviewDb db, PersonIdent i) { public PushOneCommit(ReviewDb db, PersonIdent i) {
this(db, i, SUBJECT, FILE_NAME, FILE_CONTENT); this(db, i, SUBJECT, FILE_NAME, FILE_CONTENT);
@@ -86,7 +87,14 @@ public class PushOneCommit {
} else { } else {
changeId = createCommit(git, i, subject); changeId = createCommit(git, i, subject);
} }
return new Result(db, ref, pushHead(git, ref), changeId, subject); if (tagName != null) {
git.tag().setName(tagName).setAnnotated(false).call();
}
return new Result(db, ref, pushHead(git, ref, tagName != null), changeId, subject);
}
public void setTag(final String tagName) {
this.tagName = tagName;
} }
public static class Result { public static class Result {

View File

@@ -113,7 +113,7 @@ public class SubmitOnPushIT extends AbstractDaemonTest {
@Test @Test
public void submitOnPush() throws GitAPIException, OrmException, public void submitOnPush() throws GitAPIException, OrmException,
IOException, ConfigInvalidException { IOException, ConfigInvalidException {
grantSubmit(project, "refs/for/refs/heads/master"); grant(Permission.SUBMIT, project, "refs/for/refs/heads/master");
PushOneCommit.Result r = pushTo("refs/for/master%submit"); PushOneCommit.Result r = pushTo("refs/for/master%submit");
r.assertOkStatus(); r.assertOkStatus();
r.assertChange(Change.Status.MERGED, null, admin); r.assertChange(Change.Status.MERGED, null, admin);
@@ -121,10 +121,26 @@ public class SubmitOnPushIT extends AbstractDaemonTest {
assertCommit(project, "refs/heads/master"); assertCommit(project, "refs/heads/master");
} }
@Test
public void submitOnPushWithTag() throws GitAPIException, OrmException,
IOException, ConfigInvalidException {
grant(Permission.SUBMIT, project, "refs/for/refs/heads/master");
grant(Permission.CREATE, project, "refs/tags/*");
final String tag = "v1.0";
PushOneCommit push = new PushOneCommit(db, admin.getIdent());
push.setTag(tag);
PushOneCommit.Result r = push.to(git, "refs/for/master%submit");
r.assertOkStatus();
r.assertChange(Change.Status.MERGED, null, admin);
assertSubmitApproval(r.getPatchSetId());
assertCommit(project, "refs/heads/master");
assertTag(project, "refs/heads/master", tag);
}
@Test @Test
public void submitOnPushToRefsMetaConfig() throws GitAPIException, public void submitOnPushToRefsMetaConfig() throws GitAPIException,
OrmException, IOException, ConfigInvalidException { OrmException, IOException, ConfigInvalidException {
grantSubmit(project, "refs/for/refs/meta/config"); grant(Permission.SUBMIT, project, "refs/for/refs/meta/config");
git.fetch().setRefSpecs(new RefSpec("refs/meta/config:refs/meta/config")).call(); git.fetch().setRefSpecs(new RefSpec("refs/meta/config:refs/meta/config")).call();
ObjectId objectId = git.getRepository().getRef("refs/meta/config").getObjectId(); ObjectId objectId = git.getRepository().getRef("refs/meta/config").getObjectId();
@@ -145,7 +161,7 @@ public class SubmitOnPushIT extends AbstractDaemonTest {
push(master, "one change", "a.txt", "some content"); push(master, "one change", "a.txt", "some content");
git.checkout().setName(objectId.getName()).call(); git.checkout().setName(objectId.getName()).call();
grantSubmit(project, "refs/for/refs/heads/master"); grant(Permission.SUBMIT, project, "refs/for/refs/heads/master");
PushOneCommit.Result r = PushOneCommit.Result r =
push("refs/for/master%submit", "other change", "a.txt", "other content"); push("refs/for/master%submit", "other change", "a.txt", "other content");
r.assertOkStatus(); r.assertOkStatus();
@@ -161,7 +177,7 @@ public class SubmitOnPushIT extends AbstractDaemonTest {
push(master, "one change", "a.txt", "some content"); push(master, "one change", "a.txt", "some content");
git.checkout().setName(objectId.getName()).call(); git.checkout().setName(objectId.getName()).call();
grantSubmit(project, "refs/for/refs/heads/master"); grant(Permission.SUBMIT, project, "refs/for/refs/heads/master");
PushOneCommit.Result r = PushOneCommit.Result r =
push("refs/for/master%submit", "other change", "b.txt", "other content"); push("refs/for/master%submit", "other change", "b.txt", "other content");
r.assertOkStatus(); r.assertOkStatus();
@@ -175,7 +191,7 @@ public class SubmitOnPushIT extends AbstractDaemonTest {
PushOneCommit.Result r = PushOneCommit.Result r =
push("refs/for/master", PushOneCommit.SUBJECT, "a.txt", "some content"); push("refs/for/master", PushOneCommit.SUBJECT, "a.txt", "some content");
grantSubmit(project, "refs/for/refs/heads/master"); grant(Permission.SUBMIT, project, "refs/for/refs/heads/master");
r = push("refs/for/master%submit", PushOneCommit.SUBJECT, "a.txt", r = push("refs/for/master%submit", PushOneCommit.SUBJECT, "a.txt",
"other content", r.getChangeId()); "other content", r.getChangeId());
r.assertOkStatus(); r.assertOkStatus();
@@ -220,13 +236,13 @@ public class SubmitOnPushIT extends AbstractDaemonTest {
r.assertErrorStatus("branch " + branchName + " not found"); r.assertErrorStatus("branch " + branchName + " not found");
} }
private void grantSubmit(Project.NameKey project, String ref) private void grant(String permission, Project.NameKey project, String ref)
throws RepositoryNotFoundException, IOException, ConfigInvalidException { throws RepositoryNotFoundException, IOException, ConfigInvalidException {
MetaDataUpdate md = metaDataUpdateFactory.create(project); MetaDataUpdate md = metaDataUpdateFactory.create(project);
md.setMessage("Grant submit on " + ref); md.setMessage(String.format("Grant %s on %s", permission, ref));
ProjectConfig config = ProjectConfig.read(md); ProjectConfig config = ProjectConfig.read(md);
AccessSection s = config.getAccessSection(ref, true); AccessSection s = config.getAccessSection(ref, true);
Permission p = s.getPermission(Permission.SUBMIT, true); Permission p = s.getPermission(permission, true);
AccountGroup adminGroup = groupCache.get(new AccountGroup.NameKey("Administrators")); AccountGroup adminGroup = groupCache.get(new AccountGroup.NameKey("Administrators"));
p.add(new PermissionRule(config.resolve(adminGroup))); p.add(new PermissionRule(config.resolve(adminGroup)));
config.commit(md); config.commit(md);
@@ -277,6 +293,18 @@ public class SubmitOnPushIT extends AbstractDaemonTest {
} }
} }
private void assertTag(Project.NameKey project, String branch, String tagName)
throws IOException {
Repository r = repoManager.openRepository(project);
try {
ObjectId headCommit = r.getRef(branch).getObjectId();
ObjectId taggedCommit = r.getRef(tagName).getObjectId();
assertEquals(headCommit, taggedCommit);
} finally {
r.close();
}
}
private PushOneCommit.Result pushTo(String ref) throws GitAPIException, private PushOneCommit.Result pushTo(String ref) throws GitAPIException,
IOException { IOException {
PushOneCommit push = new PushOneCommit(db, admin.getIdent()); PushOneCommit push = new PushOneCommit(db, admin.getIdent());

View File

@@ -405,8 +405,7 @@ public class MergeOp {
try { try {
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)) {
try { try {
alreadyAccepted.add(rw.parseCommit(r.getObjectId())); alreadyAccepted.add(rw.parseCommit(r.getObjectId()));
} catch (IncorrectObjectTypeException iote) { } catch (IncorrectObjectTypeException iote) {