JiriUpdater: Do not set branch for SHA1

In pinned manifest, the ref is a SHA1. Current code is writing "branch =
<SHA1>" that is not a valid value. Furthermore, tags are not valid
values for the branch field.

Write the branch field only for non-tags. Tags go to the "ref" field
(RepoCommand does this).

Change-Id: Id89d8a4c1dd6d9dc3327fc3f60a6b7c7a324b0a5
diff --git a/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java b/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java
index 85f4429..83a2eb7 100644
--- a/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java
+++ b/java/com/googlesource/gerrit/plugins/supermanifest/JiriUpdater.java
@@ -1,6 +1,7 @@
 package com.googlesource.gerrit.plugins.supermanifest;
 
 import static com.google.gerrit.entities.RefNames.REFS_HEADS;
+import static com.google.gerrit.entities.RefNames.REFS_TAGS;
 import static java.nio.charset.StandardCharsets.UTF_8;
 
 import com.google.common.collect.Lists;
@@ -99,8 +100,13 @@
           }
         }
 
-        // can be branch or tag
-        cfg.setString("submodule", path, "branch", ref);
+        // can be branch, tag or SHA1 (objectId)
+        if (!ObjectId.isId(ref)) {
+          // "branch" field is only for non-tag references.
+          // Keep tags in "ref" field as hint for other tools.
+          String field = ref.startsWith(REFS_TAGS) ? "ref" : "branch";
+          cfg.setString("submodule", path, field, ref);
+        }
 
         if (proj.getHistorydepth() > 0) {
           cfg.setBoolean("submodule", path, "shallow", true);
diff --git a/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java b/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java
index f1d71c7..a83b5f7 100644
--- a/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java
+++ b/javatests/com/googlesource/gerrit/plugins/supermanifest/JiriSuperManifestIT.java
@@ -48,6 +48,7 @@
     name = "supermanifest",
     sysModule = "com.googlesource.gerrit.plugins.supermanifest.SuperManifestModule")
 public class JiriSuperManifestIT extends LightweightPluginDaemonTest {
+
   Project.NameKey[] testRepoKeys;
 
   @Inject private ProjectOperations projectOperations;
@@ -129,6 +130,10 @@
 
     BranchApi branch = gApi.projects().name(superKey.get()).branch("refs/heads/destbranch");
     assertThat(branch.file("project1").getContentType()).isEqualTo("x-git/gitlink; charset=UTF-8");
+    Config base = new Config();
+    BlobBasedConfig cfg =
+        new BlobBasedConfig(base, branch.file(".gitmodules").asString().getBytes(UTF_8));
+    assertThat(cfg.getString("submodule", "project1", "branch")).isEqualTo("master");
     assertThrows(ResourceNotFoundException.class, () -> branch.file("project2"));
 
     xml =
@@ -622,6 +627,10 @@
     // Though the this project has the same name as a local repo, the subUrl must be absolute
     // as this is an external repo.
     assertThat(subUrl).isEqualTo("https://external/" + testRepoKeys[1].get());
+
+    assertThat(cfg.getString("submodule", "project1", "branch")).isEqualTo("master");
+    assertThat(cfg.getString("submodule", "project2", "branch")).isNull();
+    assertThat(cfg.getString("submodule", "project3", "branch")).isNull();
   }
 
   @Test