ConfigEntry: consolidate calculating the actual destination branch

Jiri and Repo updaters have the logic to handle the '*' in the
destination to calculate the actual destination branch to write.

Consolidate that in the ConfigEntry. This simplifies introducing the
support for ref patterns in later changes.

Change-Id: Iec6f72225e2e560335c0c30f23e39c86a2dde232
diff --git a/java/com/googlesource/gerrit/plugins/supermanifest/ConfigEntry.java b/java/com/googlesource/gerrit/plugins/supermanifest/ConfigEntry.java
index d482917..066cfd4 100644
--- a/java/com/googlesource/gerrit/plugins/supermanifest/ConfigEntry.java
+++ b/java/com/googlesource/gerrit/plugins/supermanifest/ConfigEntry.java
@@ -209,11 +209,27 @@
     return repoGroups;
   }
 
-  /** @return the destBranch */
+  /** @return the destBranch as defined in the conf */
   public String getDestBranch() {
     return destBranch;
   }
 
+  /**
+   * Get the real destination branch for the srcRef in this conf.
+   *
+   * <p>This takes into account patterns in destination
+   *
+   * @param updatedRef the source ref, expected to start with refs/heads
+   * @return the short-named destination branch (assumed under refs/heads)
+   */
+  public String getActualDestBranch(String updatedRef) {
+    if (destBranch.equals("*")) {
+      // We can take the ref, because we know it matched this conf before
+      return updatedRef.substring(REFS_HEADS.length());
+    }
+    return destBranch;
+  }
+
   public boolean matchesSource(String project, String refName) {
     if (!srcRepoKey.get().equals(project)) {
       return false;
diff --git a/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java b/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java
index 129ae35..66d6262 100644
--- a/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java
+++ b/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java
@@ -294,7 +294,7 @@
       JiriProjects projects =
           JiriManifestParser.getProjects(
               reader, c.getSrcRepoKey().toString(), srcRef, c.getXmlPath());
-      String targetRef = c.getDestBranch().equals("*") ? srcRef : REFS_HEADS + c.getDestBranch();
+      String targetRef = REFS_HEADS + c.getActualDestBranch(srcRef);
       updateSubmodules(
           destRepo, targetRef, URI.create(c.getDestRepoKey().toString() + "/"), projects, reader);
     }
diff --git a/java/com/googlesource/gerrit/plugins/supermanifest/RepoUpdater.java b/java/com/googlesource/gerrit/plugins/supermanifest/RepoUpdater.java
index 8f899bd..950f252 100644
--- a/java/com/googlesource/gerrit/plugins/supermanifest/RepoUpdater.java
+++ b/java/com/googlesource/gerrit/plugins/supermanifest/RepoUpdater.java
@@ -14,8 +14,6 @@
 
 package com.googlesource.gerrit.plugins.supermanifest;
 
-import static com.google.gerrit.entities.RefNames.REFS_HEADS;
-
 import com.googlesource.gerrit.plugins.supermanifest.SuperManifestRefUpdatedListener.GerritRemoteReader;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -43,12 +41,7 @@
     Repository srcRepo = reader.openRepository(c.getSrcRepoKey().toString());
 
     RepoCommand cmd = new RepoCommand(destRepo);
-
-    if (c.getDestBranch().equals("*")) {
-      cmd.setTargetBranch(srcRef.substring(REFS_HEADS.length()));
-    } else {
-      cmd.setTargetBranch(c.getDestBranch());
-    }
+    cmd.setTargetBranch(c.getActualDestBranch(srcRef));
 
     InputStream manifestStream =
         new ByteArrayInputStream(Utils.readBlob(srcRepo, srcRef + ":" + c.getXmlPath()));
diff --git a/javatests/com/googlesource/gerrit/plugins/supermanifest/ConfigEntryTest.java b/javatests/com/googlesource/gerrit/plugins/supermanifest/ConfigEntryTest.java
index 6293f79..c3caec0 100644
--- a/javatests/com/googlesource/gerrit/plugins/supermanifest/ConfigEntryTest.java
+++ b/javatests/com/googlesource/gerrit/plugins/supermanifest/ConfigEntryTest.java
@@ -48,6 +48,7 @@
     assertThat(entry.matchesSource("manifest", "refs/heads/nyc-src")).isTrue();
     assertThat(entry.matchesSource("manifest", "refs/heads/other")).isFalse();
     assertThat(entry.matchesSource("otherproject", "refs/heads/nyc-src")).isFalse();
+    assertThat(entry.getActualDestBranch("refs/heads/nyc-src")).isEqualTo("nyc");
   }
 
   @Test
@@ -68,6 +69,8 @@
     assertThat(entry.matchesSource("manifest", "refs/heads/a")).isTrue();
     assertThat(entry.matchesSource("manifest", "refs/heads/b")).isTrue();
     assertThat(entry.matchesSource("otherproject", "refs/heads/c")).isFalse();
+    assertThat(entry.getActualDestBranch("refs/heads/a")).isEqualTo("a");
+    assertThat(entry.getActualDestBranch("refs/heads/b")).isEqualTo("b");
   }
 
   @Test