Replicate HEAD reference when replicating a project

Replication plugin replicates HEAD changes to target instances giving
such changes are done using the web UI. If for any reason the target
instance was down when doing such change, or if the change was manually
done (either by editing the HEAD file directly or using a git command),
this HEAD change was not propagated and the target instance kept the
outdated head. Pushing manual replication did not replicate HEAD to
targets, and thus, the HEAD reference had to be manually updated on the
target instance.

Replicate HEAD reference when replicating a project so that HEAD changes
are always replicated to target instances, either when replication is
triggered automatically or when it is done using the 'replication start'
ssh command.

Change-Id: Ibd9be085dd56bf80a27e2798513fd06592209704
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 255c51b..36e3ecb 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/PushOne.java
@@ -372,6 +372,7 @@
     PushResult res;
     try {
       res = pushVia(tn);
+      updateHead();
     } finally {
       try {
         tn.close();
@@ -545,6 +546,11 @@
     cmds.add(new RemoteRefUpdate(git, (Ref) null, dst, force, null, null));
   }
 
+  private void updateHead() throws IOException {
+    replicationQueue.updateHead(projectName, git.getRef(Constants.HEAD)
+        .getTarget().getName());
+  }
+
   private void updateStates(Collection<RemoteRefUpdate> refUpdates)
       throws LockFailureException {
     Set<String> doneRefs = new HashSet<>();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java
index 95693d9..c3b4675 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java
@@ -163,9 +163,13 @@
 
   @Override
   public void onHeadUpdated(HeadUpdatedListener.Event event) {
-    for (URIish uri : getURIs(new Project.NameKey(event.getProjectName()),
-        FilterType.ALL)) {
-      updateHead(uri, event.getNewHeadName());
+    updateHead(new Project.NameKey(event.getProjectName()),
+        event.getNewHeadName());
+  }
+
+  void updateHead(Project.NameKey project, String newHeadName) {
+    for (URIish uri : getURIs(project, FilterType.ALL)) {
+      updateHead(uri, newHeadName);
     }
   }