Add refs being replicated to show-queue output of replication tasks

This change adds refs to show-queue output making it convenient to see
which refs are being replicated by each task.

By default show-queue output will be limited to show 2 refs.

The value 2 is chosen because whenever a new patchset is created
there are two refs to be replicated (change ref and meta ref), refs
needs to be limited since it will become inconvenient if there are too
many refs being replicated.

Gerrit admin can override this behavior by providing "maxRefsToShow"
config in replication config file, to show all refs gerrit admin can set
"maxRefsToShow" config to zero.

Sample show-queue output:
(retry 1) push aaa.com:/git/All-Projects.git [..all..]
(retry 1) push aaa.com:/git/test.git [refs/meta/config refs/heads/b1]
(retry 1) push aaa.com:/git/test.git [refs/heads/b1 refs/heads/b2 (+1)]
(retry 1) push aaa.com:/git/test.git [refs/heads/b1 refs/heads/b2 (+2)]

Change-Id: Iaf7b32a0ac5f029671757658174cfde4e07f365c
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 782ff4f..a0d9624 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/AutoReloadConfigDecorator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/AutoReloadConfigDecorator.java
@@ -77,6 +77,11 @@
   }
 
   @Override
+  public int getMaxRefsToShow() {
+    return currentConfig.getMaxRefsToShow();
+  }
+
+  @Override
   public Path getEventsDirectory() {
     return currentConfig.getEventsDirectory();
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/FanoutReplicationConfig.java b/src/main/java/com/googlesource/gerrit/plugins/replication/FanoutReplicationConfig.java
index 5b24bf5..b915d0d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/FanoutReplicationConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/FanoutReplicationConfig.java
@@ -137,6 +137,11 @@
   }
 
   @Override
+  public int getMaxRefsToShow() {
+    return replicationConfig.getMaxRefsToShow();
+  }
+
+  @Override
   public Path getEventsDirectory() {
     return replicationConfig.getEventsDirectory();
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java b/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
index 565790c..2243a3c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
@@ -61,6 +61,7 @@
 import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
 import org.eclipse.jgit.errors.NoRemoteRepositoryException;
 import org.eclipse.jgit.errors.NotSupportedException;
 import org.eclipse.jgit.errors.RemoteRepositoryException;
@@ -212,7 +213,8 @@
 
   @Override
   public String toString() {
-    String print = "[" + HexFormat.fromInt(id) + "] push " + uri;
+    String print =
+        "[" + HexFormat.fromInt(id) + "] push " + uri + " " + getLimitedRefs();
 
     if (retryCount > 0) {
       print = "(retry " + retryCount + ") " + print;
@@ -220,6 +222,36 @@
     return print;
   }
 
+ /**
+   * Returns a string of refs limited to the maxRefsToShow config with count of total refs hidden when
+   * there are more refs than maxRefsToShow config.
+   *
+   * <ul>
+   *   <li>Refs will not be limited when maxRefsToShow config is set to zero.
+   *   <li>By default output will be limited to two refs.
+   * </ul>
+   *
+   * The default value of two is chosen because whenever a new patchset is created there are two
+   * refs to be replicated(change ref and meta ref).
+   *
+   * @return Space separated string of refs (in square bracket) limited to the maxRefsToShow with
+   *     count of total refs hidden(in parentheses) when there are more refs than maxRefsToShow
+   *     config.
+   */
+  protected String getLimitedRefs() {
+    Set<String> refs = getRefs();
+    int maxRefsToShow = replConfig.getMaxRefsToShow();
+    if (maxRefsToShow == 0) {
+      maxRefsToShow = refs.size();
+    }
+    String refsString = refs.stream().limit(maxRefsToShow).collect(Collectors.joining(" "));
+    int hiddenRefs = refs.size() - maxRefsToShow;
+    if (hiddenRefs > 0) {
+      refsString += " (+" + hiddenRefs + ")";
+    }
+    return "[" + refsString + "]";
+  }
+
   boolean isRetrying() {
     return retrying;
   }
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 8bbb180..410cf5b 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationConfig.java
@@ -60,6 +60,13 @@
   int getMaxRefsToLog();
 
   /**
+  * Returns the maximum number of replicating refs to show in the show-queue output
+  *
+  * @return maximum number of refs to show, 2 by default.
+  */
+  int getMaxRefsToShow();
+
+  /**
    * Configured location where the replication events are stored on the filesystem for being resumed
    * and kept across restarts.
    *
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 2631cbe..555a5c7 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationFileBasedConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationFileBasedConfig.java
@@ -34,6 +34,7 @@
   private boolean replicateAllOnPluginStart;
   private boolean defaultForceUpdate;
   private int maxRefsToLog;
+  private final int maxRefsToShow;
   private int sshCommandTimeout;
   private int sshConnectionTimeout = DEFAULT_SSH_CONNECTION_TIMEOUT_MS;
   private final FileBasedConfig config;
@@ -54,6 +55,7 @@
     this.replicateAllOnPluginStart = config.getBoolean("gerrit", "replicateOnStartup", false);
     this.defaultForceUpdate = config.getBoolean("gerrit", "defaultForceUpdate", false);
     this.maxRefsToLog = config.getInt("gerrit", "maxRefsToLog", 0);
+    this.maxRefsToShow= config.getInt("gerrit", "maxRefsToShow", 2);
     this.pluginDataDir = pluginDataDir;
   }
 
@@ -96,6 +98,11 @@
   }
 
   @Override
+  public int getMaxRefsToShow() {
+    return maxRefsToShow;
+  }
+
+  @Override
   public Path getEventsDirectory() {
     String eventsDirectory = config.getString("replication", null, "eventsDirectory");
     if (!Strings.isNullOrEmpty(eventsDirectory)) {
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 6a2cf26..3d409ae 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -86,6 +86,15 @@
 :	Number of refs, that are pushed during replication, to be logged.
 	For printing all refs to the logs, use a value of 0. By default, 0.
 
+gerrit.maxRefsToShow
+:	Number of refs, that are pushed during replication, to be shown
+	in the show-queue output. To show all refs, use a value of 0.
+	By default, 2, because whenever a new patchset is created there
+	are two refs (change ref and meta ref) eg.
+
+	`(retry 1) push aaa.com:/git/test.git [refs/heads/b1 refs/heads/b2 (+2)]`
+
+
 gerrit.sshCommandTimeout
 :	Timeout for SSH command execution. If 0, there is no timeout and
 	the client waits indefinitely. By default, 0.