Allow default force update to be configurable

In I4509d3500c730b8b5921dc155ea0476a70b3de9e the default refspec was
changed to not used forced update.

Administrators of sites that don't specify a refspec, because they
actually want replication to default to 'force push all refs', will
now need to edit their replication config to explicitly add the force
push refspec.

To make the configuration update a bit simpler, add a new setting
`gerrit.defaultForceUpdate` which, when set to true, will cause the
replication to behave in the same way as before for targets where no
refspec is given.

Change-Id: Ia60a916135e87f9b7f36e5038c7595ed708045aa
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/AutoReloadConfigDecorator.java b/src/main/java/com/googlesource/gerrit/plugins/replication/AutoReloadConfigDecorator.java
index 8bf2da4..a2b66d1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/AutoReloadConfigDecorator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/AutoReloadConfigDecorator.java
@@ -113,6 +113,11 @@
   }
 
   @Override
+  public synchronized boolean isDefaultForceUpdate() {
+    return currentConfig.isDefaultForceUpdate();
+  }
+
+  @Override
   public synchronized boolean isEmpty() {
     return currentConfig.isEmpty();
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfig.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfig.java
index 1b971c3..5c18f75 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfig.java
@@ -23,6 +23,8 @@
 
   boolean isReplicateAllOnPluginStart();
 
+  boolean isDefaultForceUpdate();
+
   boolean isEmpty();
 
   int shutdown();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationFileBasedConfig.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationFileBasedConfig.java
index 0e3894b..dd8f4f5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationFileBasedConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationFileBasedConfig.java
@@ -48,6 +48,7 @@
   private List<Destination> destinations;
   private File cfgPath;
   private boolean replicateAllOnPluginStart;
+  private boolean defaultForceUpdate;
   private Injector injector;
   private final SchemaFactory<ReviewDb> database;
   private final RemoteSiteUser.Factory replicationUserFactory;
@@ -104,6 +105,9 @@
     replicateAllOnPluginStart =
         config.getBoolean("gerrit", "replicateOnStartup", true);
 
+    defaultForceUpdate =
+        config.getBoolean("gerrit", "defaultForceUpdate", false);
+
     ImmutableList.Builder<Destination> dest = ImmutableList.builder();
     for (RemoteConfig c : allRemotes(config)) {
       if (c.getURIs().isEmpty()) {
@@ -118,7 +122,8 @@
       }
 
       if (c.getPushRefSpecs().isEmpty()) {
-        c.addPushRefSpec(new RefSpec().setSourceDestination("refs/*", "refs/*"));
+        c.addPushRefSpec(new RefSpec().setSourceDestination("refs/*", "refs/*")
+            .setForceUpdate(defaultForceUpdate));
       }
 
       Destination destination =
@@ -148,6 +153,14 @@
     return replicateAllOnPluginStart;
   }
 
+  /* (non-Javadoc)
+   * @see com.googlesource.gerrit.plugins.replication.ReplicationConfig#isDefaultForceUpdate()
+   */
+  @Override
+  public boolean isDefaultForceUpdate() {
+    return defaultForceUpdate;
+  }
+
   private static List<RemoteConfig> allRemotes(FileBasedConfig cfg)
       throws ConfigInvalidException {
     Set<String> names = cfg.getSubsections("remote");
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 9521375..de0b907 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -73,6 +73,10 @@
 	the replication plugin. When the reload takes place, pending replication
 	events based on old settings are discarded. By default, false.
 
+gerrit.defaultForceUpdate
+:	If true, the default push refspec will be set to use forced
+	update to the remote when no refspec is given.  By default, false.
+
 remote.NAME.url
 :	Address of the remote server to push to.  Multiple URLs may be
 	specified within a single remote block, listing different
@@ -141,7 +145,9 @@
 	and `refs/tags/*`, but excludes all others, including
 	`refs/changes/*`.
 
-	Defaults to `refs/*:refs/*` (push all refs) if not specified.
+	Defaults to `refs/*:refs/*` (push all refs) if not specified,
+	or `+refs/*:refs/*` (force push all refs) if not specified and
+	`gerrit.defaultForceUpdate` is true.
 
 [2]: #example_file