Repair destinations end-to-end in repair command

Previously, the SSH 'repair' command copied packs to all destinations
first and then issued a single replication start for the project. A
failure during the copy phase still left a replication attempt queued
for the affected destination, which is not desired. Rework the command
to drive one destination at a time so that the follow-up replication
is only triggered when its preceding pack copy succeeds.

Change-Id: Ibcc8cd5ee8a005b0c29d4fe9f4f8e1af96524d32
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/RepairCommand.java b/src/main/java/com/googlesource/gerrit/plugins/replication/RepairCommand.java
index 70e8622..92af46a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/RepairCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/RepairCommand.java
@@ -87,26 +87,13 @@
       full = true;
     }
 
-    Set<URIish> failedUris = new HashSet<>();
-    if (full || copyPacks) {
-      failedUris.addAll(copyPacks(project));
-    }
-
+    Set<URIish> failedUris = repair(project);
     if (!failedUris.isEmpty()) {
       throw new UnloggedFailure(1, "Repair failed for " + failedUris.size() + " destination(s)");
     }
-
-    writeStdOutSync("\nRunning replication start for " + project.get() + " ...");
-    replicationStarter.start(
-        urlMatch,
-        Set.of(),
-        new ReplicationFilter(List.of(project.get()), Collections.emptyList()),
-        /* now= */ true,
-        /* wait= */ true,
-        this);
   }
 
-  private Set<URIish> copyPacks(Project.NameKey project) throws Failure {
+  private Set<URIish> repair(Project.NameKey project) throws Failure {
     Path packDir;
     try (Repository repo = gitManager.openRepository(project)) {
       packDir = repo.getDirectory().toPath().resolve("objects").resolve("pack");
@@ -138,10 +125,20 @@
 
     Set<URIish> failedUris = new HashSet<>();
     for (URIish uri : copyTargets) {
-      writeStdOutSync("Copying pack files to " + uri + " ...");
-      if (!copyInOrder(packDir, uri)) {
+      writeStdOutSync("\nRepairing " + uri + " ...");
+      if ((full || copyPacks) && !copyInOrder(packDir, uri)) {
         failedUris.add(uri);
+        continue;
       }
+      writeStdOutSync(
+          "\nRunning replication start for " + project.get() + " to " + uri.toString() + " ...");
+      replicationStarter.start(
+          uri.toString(),
+          Set.of(),
+          new ReplicationFilter(List.of(project.get()), Collections.emptyList()),
+          /* now= */ true,
+          /* wait= */ true,
+          this);
     }
     return failedUris;
   }