Merge branch 'stable-3.2' into stable-3.3

* stable-3.2:
  Add newline at EOF as required by HAProxy
  Use GerritForge's archive-ci for downloading artifacts
  Fix issue with disabling ref-database
  Use archive-ci.gerritforge.com for downloading plugins
  Add handling of multi-base local disk repositories

Change-Id: I7120e9b9c861ffda7567e45f34bd26df5a2f0bfa
diff --git a/e2e-tests/test.sh b/e2e-tests/test.sh
index 49615e7..19c8b04 100755
--- a/e2e-tests/test.sh
+++ b/e2e-tests/test.sh
@@ -17,7 +17,7 @@
 LOCATION="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
 LOCAL_ENV="$( cd "${LOCATION}/../setup_local_env" >/dev/null 2>&1 && pwd )"
 GERRIT_BRANCH=stable-3.3
-GERRIT_CI=https://gerrit-ci.gerritforge.com/view/Plugins-$GERRIT_BRANCH/job
+GERRIT_CI=https://archive-ci.gerritforge.com/view/Plugins-$GERRIT_BRANCH/job
 LAST_BUILD=lastSuccessfulBuild/artifact/bazel-bin/plugins
 DEF_MULTISITE_LOCATION=${LOCATION}/../../../bazel-bin/plugins/multi-site/multi-site.jar
 DEF_GERRIT_IMAGE=3.3.6-centos8
diff --git a/setup_local_env/setup.sh b/setup_local_env/setup.sh
index 5342fbf..11ae04f 100755
--- a/setup_local_env/setup.sh
+++ b/setup_local_env/setup.sh
@@ -17,7 +17,7 @@
 
 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
 GERRIT_BRANCH=stable-3.3
-GERRIT_CI=https://gerrit-ci.gerritforge.com/view/Plugins-$GERRIT_BRANCH/job
+GERRIT_CI=https://archive-ci.gerritforge.com/view/Plugins-$GERRIT_BRANCH/job
 LAST_BUILD=lastSuccessfulBuild/artifact/bazel-bin/plugins
 EVENTS_BROKER_VER=`grep 'com.gerritforge:events-broker' $(dirname $0)/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
 GLOBAL_REFDB_VER=`grep 'com.gerritforge:global-refdb' $(dirname $0)/../external_plugin_deps.bzl | cut -d '"' -f 2 | cut -d ':' -f 3`
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/Configuration.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/Configuration.java
index 81e1c12..6b77713 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/Configuration.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/Configuration.java
@@ -135,6 +135,13 @@
           null,
           SharedRefDbConfiguration.SharedRefDatabase.ENABLE_KEY,
           true);
+      if (cfg instanceof FileBasedConfig) {
+        try {
+          ((FileBasedConfig) cfg).save();
+        } catch (IOException e) {
+          throw new IllegalStateException("Error while enabling global-refdb by default", e);
+        }
+      }
     }
     return cfg;
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/GitModule.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/GitModule.java
index 38cddd5..96bb4e8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/GitModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/GitModule.java
@@ -15,24 +15,33 @@
 package com.googlesource.gerrit.plugins.multisite;
 
 import com.gerritforge.gerrit.globalrefdb.validation.SharedRefDbConfiguration;
+import com.google.gerrit.server.ModuleImpl;
+import com.google.gerrit.server.config.RepositoryConfig;
+import com.google.gerrit.server.git.GitRepositoryManagerModule;
 import com.google.inject.AbstractModule;
 import com.google.inject.Inject;
 import com.googlesource.gerrit.plugins.multisite.validation.ValidationModule;
 
+@ModuleImpl(name = GitRepositoryManagerModule.MANAGER_MODULE)
 public class GitModule extends AbstractModule {
   private final Configuration config;
+  private final RepositoryConfig repoConfig;
 
   @Inject
-  public GitModule(Configuration config) {
+  public GitModule(Configuration config, RepositoryConfig repoConfig) {
     this.config = config;
+    this.repoConfig = repoConfig;
   }
 
   @Override
   protected void configure() {
-    bind(SharedRefDbConfiguration.class).toInstance(config.getSharedRefDbConfiguration());
+    SharedRefDbConfiguration sharedRefDbConfiguration = config.getSharedRefDbConfiguration();
+    bind(SharedRefDbConfiguration.class).toInstance(sharedRefDbConfiguration);
     bind(ProjectVersionLogger.class).to(Log4jProjectVersionLogger.class);
-    if (config.getSharedRefDbConfiguration().getSharedRefDb().isEnabled()) {
-      install(new ValidationModule(config));
+    if (sharedRefDbConfiguration.getSharedRefDb().isEnabled()) {
+      install(new ValidationModule(config, repoConfig));
+    } else {
+      install(new GitRepositoryManagerModule(repoConfig));
     }
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/RepositoryManagerModule.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/RepositoryManagerModule.java
new file mode 100644
index 0000000..b71ca67
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/RepositoryManagerModule.java
@@ -0,0 +1,41 @@
+// Copyright (C) 2022 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.googlesource.gerrit.plugins.multisite.validation;
+
+import com.gerritforge.gerrit.globalrefdb.validation.SharedRefDbGitRepositoryManager;
+import com.google.gerrit.lifecycle.LifecycleModule;
+import com.google.gerrit.server.config.RepositoryConfig;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.gerrit.server.git.LocalDiskRepositoryManager;
+import com.google.gerrit.server.git.MultiBaseLocalDiskRepositoryManager;
+
+class RepositoryManagerModule extends LifecycleModule {
+  private final RepositoryConfig cfg;
+
+  RepositoryManagerModule(RepositoryConfig cfg) {
+    this.cfg = cfg;
+  }
+
+  @Override
+  protected void configure() {
+    bind(GitRepositoryManager.class).to(SharedRefDbGitRepositoryManager.class);
+
+    // part responsible for physical repositories handling
+    listener().to(LocalDiskRepositoryManager.Lifecycle.class);
+    if (!cfg.getAllBasePaths().isEmpty()) {
+      bind(LocalDiskRepositoryManager.class).to(MultiBaseLocalDiskRepositoryManager.class);
+    }
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/ValidationModule.java b/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/ValidationModule.java
index f66bf37..8601cf2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/ValidationModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/multisite/validation/ValidationModule.java
@@ -31,7 +31,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.gerrit.extensions.config.FactoryModule;
 import com.google.gerrit.extensions.registration.DynamicItem;
-import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.gerrit.server.config.RepositoryConfig;
 import com.google.inject.Scopes;
 import com.google.inject.TypeLiteral;
 import com.google.inject.name.Names;
@@ -41,9 +41,11 @@
 
 public class ValidationModule extends FactoryModule {
   private final Configuration cfg;
+  private final RepositoryConfig repoConfig;
 
-  public ValidationModule(Configuration cfg) {
+  public ValidationModule(Configuration cfg, RepositoryConfig repoConfig) {
     this.cfg = cfg;
+    this.repoConfig = repoConfig;
   }
 
   @Override
@@ -67,7 +69,7 @@
             ImmutableSet.of(
                 ProjectVersionRefUpdate.MULTI_SITE_VERSIONING_REF,
                 ProjectVersionRefUpdate.MULTI_SITE_VERSIONING_VALUE_REF));
-    bind(GitRepositoryManager.class).to(SharedRefDbGitRepositoryManager.class);
+    install(new RepositoryManagerModule(repoConfig));
     DynamicItem.bind(binder(), ReplicationPushFilter.class)
         .to(MultisiteReplicationPushFilter.class);
 
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/GitModuleTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/GitModuleTest.java
new file mode 100644
index 0000000..7faccae
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/GitModuleTest.java
@@ -0,0 +1,67 @@
+// Copyright (C) 2022 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.googlesource.gerrit.plugins.multisite;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.gerritforge.gerrit.globalrefdb.validation.SharedRefDbGitRepositoryManager;
+import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.acceptance.NoHttpd;
+import com.google.gerrit.acceptance.UseLocalDisk;
+import com.google.gerrit.acceptance.config.GerritConfig;
+import com.google.gerrit.acceptance.config.GlobalPluginConfig;
+import com.google.gerrit.server.git.GitRepositoryManager;
+import com.google.gerrit.server.git.LocalDiskRepositoryManager;
+import com.google.gerrit.server.git.MultiBaseLocalDiskRepositoryManager;
+import com.google.inject.Inject;
+import org.junit.Test;
+
+@UseLocalDisk
+@NoHttpd
+public class GitModuleTest extends AbstractDaemonTest {
+  @Inject private GitRepositoryManager gitRepoManager;
+  @Inject private LocalDiskRepositoryManager wrapped;
+
+  @Test
+  @GerritConfig(
+      name = "gerrit.installDbModule",
+      value = "com.googlesource.gerrit.plugins.multisite.GitModule")
+  public void shouldUseLocalDiskRepositoryManagerByDefault() {
+    assertThat(gitRepoManager).isInstanceOf(SharedRefDbGitRepositoryManager.class);
+    assertThat(wrapped).isNotInstanceOf(MultiBaseLocalDiskRepositoryManager.class);
+  }
+
+  @Test
+  @GerritConfig(
+      name = "gerrit.installDbModule",
+      value = "com.googlesource.gerrit.plugins.multisite.GitModule")
+  @GerritConfig(name = "repository.r1.basePath", value = "/tmp/git1")
+  public void shouldUseMultiBaseLocalDiskRepositoryManagerWhenItIsConfigured() {
+    assertThat(gitRepoManager).isInstanceOf(SharedRefDbGitRepositoryManager.class);
+    assertThat(wrapped).isInstanceOf(MultiBaseLocalDiskRepositoryManager.class);
+  }
+
+  @Test
+  @GerritConfig(
+      name = "gerrit.installDbModule",
+      value = "com.googlesource.gerrit.plugins.multisite.GitModule")
+  @GlobalPluginConfig(
+      pluginName = Configuration.PLUGIN_NAME,
+      name = "ref-database.enabled",
+      value = "false")
+  public void shouldInstallDefaultGerritGitManagerWhenRefDbIsDisabled() {
+    assertThat(gitRepoManager).isInstanceOf(LocalDiskRepositoryManager.class);
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/ProjectVersionRefUpdateTest.java b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/ProjectVersionRefUpdateTest.java
index 0d6fb67..88d2270 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/ProjectVersionRefUpdateTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/multisite/validation/ProjectVersionRefUpdateTest.java
@@ -265,28 +265,6 @@
   }
 
   @Test
-  public void shouldNotUpdateProjectVersionWhenProjectFilteredOut() throws Exception {
-    when(projectsFilter.matches(any(Event.class))).thenReturn(false);
-
-    Context.setForwardedEvent(false);
-
-    Thread.sleep(1000L);
-    repo.branch("master").commit().create();
-
-    Thread.sleep(1000L);
-    repo.branch("master").update(masterCommit);
-
-    new ProjectVersionRefUpdateImpl(
-            repoManager, sharedRefDb, gitReferenceUpdated, verLogger, projectsFilter)
-        .onEvent(refUpdatedEvent);
-
-    Ref ref = repo.getRepository().findRef(MULTI_SITE_VERSIONING_REF);
-    assertThat(ref).isNull();
-
-    verifyZeroInteractions(verLogger);
-  }
-
-  @Test
   public void getRemoteProjectVersionShouldReturnCorrectValue() {
     when(sharedRefDb.get(A_TEST_PROJECT_NAME_KEY, MULTI_SITE_VERSIONING_VALUE_REF, String.class))
         .thenReturn(Optional.of("123"));