From 7c3402f44d57444ca1b803274381da927a5a7ee9 Mon Sep 17 00:00:00 2001 From: David Pursehouse Date: Tue, 3 Mar 2015 15:53:07 +0900 Subject: [PATCH 1/2] Update revision of replication plugin - Make createMissingRepositories = false take effect on project-created event Change-Id: Iaa3fcd9ced449870eab64e127c9be5854d2dc61f --- plugins/replication | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/replication b/plugins/replication index 3973b31f3c..eded6451be 160000 --- a/plugins/replication +++ b/plugins/replication @@ -1 +1 @@ -Subproject commit 3973b31f3c86cc5401ef2dfac5ac82f25d79e432 +Subproject commit eded6451be8dc10945c110955d2bfcfe629eac32 From 99f9f99efd333dbbd2336e548c08684090184fce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Ar=C3=A8s?= Date: Mon, 2 Mar 2015 12:07:21 -0500 Subject: [PATCH 2/2] Fix force push Since change I3dbf66f17, the force push was no longer working. The intent was to set allowNonFastForwards to true for changeEdit but it was always setting it to false when not a changeEdit. Change-Id: Ic4ead13ef5f2fabf2ea2e487c0de0f9ecb1ef52f --- .../gerrit/acceptance/AbstractDaemonTest.java | 10 ++- .../com/google/gerrit/acceptance/GitUtil.java | 6 ++ .../gerrit/acceptance/PushOneCommit.java | 7 +- .../gerrit/acceptance/git/ForcePushIT.java | 70 +++++++++++++++++++ .../gerrit/server/git/ReceiveCommits.java | 5 +- 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/ForcePushIT.java diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java index c59b3d9669..ff61899205 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/AbstractDaemonTest.java @@ -327,13 +327,21 @@ public abstract class AbstractDaemonTest { protected void grant(String permission, Project.NameKey project, String ref) throws RepositoryNotFoundException, IOException, ConfigInvalidException { + grant(permission, project, ref, false); + } + + protected void grant(String permission, Project.NameKey project, String ref, + boolean force) throws RepositoryNotFoundException, IOException, + ConfigInvalidException { MetaDataUpdate md = metaDataUpdateFactory.create(project); md.setMessage(String.format("Grant %s on %s", permission, ref)); ProjectConfig config = ProjectConfig.read(md); AccessSection s = config.getAccessSection(ref, true); Permission p = s.getPermission(permission, true); AccountGroup adminGroup = groupCache.get(new AccountGroup.NameKey("Administrators")); - p.add(new PermissionRule(config.resolve(adminGroup))); + PermissionRule rule = new PermissionRule(config.resolve(adminGroup)); + rule.setForce(force); + p.add(rule); config.commit(md); projectCache.evict(config.getProject()); } diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/GitUtil.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/GitUtil.java index e3164648e8..494f094bc7 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/GitUtil.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/GitUtil.java @@ -193,7 +193,13 @@ public class GitUtil { public static PushResult pushHead(Git git, String ref, boolean pushTags) throws GitAPIException { + return pushHead(git, ref, pushTags, false); + } + + public static PushResult pushHead(Git git, String ref, boolean pushTags, + boolean force) throws GitAPIException { PushCommand pushCmd = git.push(); + pushCmd.setForce(force); pushCmd.setRefSpecs(new RefSpec("HEAD:" + ref)); if (pushTags) { pushCmd.setPushTags(); diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java index 74f9f805ca..6cd8031b92 100644 --- a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/PushOneCommit.java @@ -113,6 +113,7 @@ public class PushOneCommit { private final String content; private String changeId; private Tag tag; + private boolean force; @AssistedInject PushOneCommit(ChangeNotes.Factory notesFactory, @@ -189,13 +190,17 @@ public class PushOneCommit { } tagCommand.call(); } - return new Result(ref, pushHead(git, ref, tag != null), c, subject); + return new Result(ref, pushHead(git, ref, tag != null, force), c, subject); } public void setTag(final Tag tag) { this.tag = tag; } + public void setForce(boolean force) { + this.force = force; + } + public class Result { private final String ref; private final PushResult result; diff --git a/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/ForcePushIT.java b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/ForcePushIT.java new file mode 100644 index 0000000000..5da524e160 --- /dev/null +++ b/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/git/ForcePushIT.java @@ -0,0 +1,70 @@ +// Copyright (C) 2015 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.acceptance.git; + +import static com.google.common.truth.Truth.assertThat; +import static org.eclipse.jgit.lib.Constants.HEAD; + +import com.google.gerrit.acceptance.AbstractDaemonTest; +import com.google.gerrit.acceptance.PushOneCommit; +import com.google.gerrit.common.data.Permission; + +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.RefUpdate; +import org.junit.Test; + +public class ForcePushIT extends AbstractDaemonTest { + + @Test + public void forcePushNotAllowed() throws Exception { + ObjectId initial = git.getRepository().getRef(HEAD).getLeaf().getObjectId(); + PushOneCommit push1 = + pushFactory.create(db, admin.getIdent(), "change1", "a.txt", "content"); + PushOneCommit.Result r1 = push1.to(git, "refs/heads/master"); + r1.assertOkStatus(); + + // Reset HEAD to initial so the new change is a non-fast forward + RefUpdate ru = git.getRepository().updateRef(HEAD); + ru.setNewObjectId(initial); + assertThat(ru.forceUpdate()).isEqualTo(RefUpdate.Result.FORCED); + + PushOneCommit push2 = + pushFactory.create(db, admin.getIdent(), "change2", "b.txt", "content"); + push2.setForce(true); + PushOneCommit.Result r2 = push2.to(git, "refs/heads/master"); + r2.assertErrorStatus("non-fast forward"); + } + + @Test + public void forcePushAllowed() throws Exception { + ObjectId initial = git.getRepository().getRef(HEAD).getLeaf().getObjectId(); + grant(Permission.PUSH, project, "refs/*", true); + PushOneCommit push1 = + pushFactory.create(db, admin.getIdent(), "change1", "a.txt", "content"); + PushOneCommit.Result r1 = push1.to(git, "refs/heads/master"); + r1.assertOkStatus(); + + // Reset HEAD to initial so the new change is a non-fast forward + RefUpdate ru = git.getRepository().updateRef(HEAD); + ru.setNewObjectId(initial); + assertThat(ru.forceUpdate()).isEqualTo(RefUpdate.Result.FORCED); + + PushOneCommit push2 = + pushFactory.create(db, admin.getIdent(), "change2", "b.txt", "content"); + push2.setForce(true); + PushOneCommit.Result r2 = push2.to(git, "refs/heads/master"); + r2.assertOkStatus(); + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java index 81833fe715..cbf57010af 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java @@ -567,7 +567,10 @@ public class ReceiveCommits { if (!batch.getCommands().isEmpty()) { try { - batch.setAllowNonFastForwards(magicBranch != null && magicBranch.edit); + if (!batch.isAllowNonFastForwards() && magicBranch != null + && magicBranch.edit) { + batch.setAllowNonFastForwards(true); + } batch.execute(rp.getRevWalk(), commandProgress); } catch (IOException err) { int cnt = 0;