Add hook trigger on branching a manifest

Change-Id: I5d8173fab1645b4e35c6066ac433116735f58264
diff --git a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/Utilities.java b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/Utilities.java
index 73dcddc..db8eaf9 100644
--- a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/Utilities.java
+++ b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/Utilities.java
@@ -14,7 +14,6 @@
 
 package com.amd.gerrit.plugins.manifestsubscription;
 
-import com.google.common.base.Strings;
 import com.google.common.base.Throwables;
 import com.google.common.collect.Maps;
 import com.google.gerrit.common.ChangeHooks;
@@ -29,13 +28,11 @@
 import com.amd.gerrit.plugins.manifestsubscription.manifest.Default;
 import com.amd.gerrit.plugins.manifestsubscription.manifest.Manifest;
 
-import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.errors.ConfigInvalidException;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
-import org.eclipse.jgit.revwalk.RevWalk;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -128,9 +125,14 @@
     // May be related:
     // https://code.google.com/p/gerrit/issues/detail?id=2564
     // https://gerrit-review.googlesource.com/55540
-    if (commit != null && commit.getParents().length > 0) {
+    if (commit != null) {
+      ObjectId parent = ObjectId.zeroId();
+
+      if (commit.getParents().length > 0) {
+        parent = commit.getParent(0).getId();
+      }
       changeHooks.doRefUpdatedHook(new Branch.NameKey(p, refName),
-                                    commit.getParent(0).getId(),
+                                    parent,
                                     commit.getId(), null);
       return commit.getId();
     } else {
@@ -323,7 +325,8 @@
     try {
       manifest = getManifest(gitRepoManager, manifestRepo,
                               manifestCommitish, manifestPath);
-      VersionedManifests.branchManifest(gitRepoManager, manifest, newBranch);
+      VersionedManifests.branchManifest(gitRepoManager, manifest,
+                                        newBranch, changeHooks);
 
       if (newManifestBranch != null &&
           newManifestPath != null &&
diff --git a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/VersionedManifests.java b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/VersionedManifests.java
index bdffd8c..44b5889 100644
--- a/src/main/java/com/amd/gerrit/plugins/manifestsubscription/VersionedManifests.java
+++ b/src/main/java/com/amd/gerrit/plugins/manifestsubscription/VersionedManifests.java
@@ -17,6 +17,8 @@
 import com.google.common.collect.HashBasedTable;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Table;
+import com.google.gerrit.common.ChangeHooks;
+import com.google.gerrit.reviewdb.client.Branch;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gerrit.server.git.VersionedMetaData;
@@ -29,6 +31,7 @@
 import org.eclipse.jgit.lib.CommitBuilder;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.PersonIdent;
+import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
@@ -249,7 +252,8 @@
 
   static void branchManifest(GitRepositoryManager gitRepoManager,
                              Manifest manifest,
-                             final String branch)
+                             final String branch,
+                             final ChangeHooks changeHooks)
                                           throws GitAPIException, IOException {
     Table<String, String, String> lookup = HashBasedTable.create();
     String defaultRef = null;
@@ -267,7 +271,14 @@
         Project.NameKey p = new Project.NameKey(project.getName());
         try (Repository db = gitRepoManager.openRepository(p);
              Git git = new Git(db)) {
-          git.branchCreate().setName(branch).setStartPoint(hash).call();
+          try {
+            Ref r = git.branchCreate().setName(branch).setStartPoint(hash).call();
+            changeHooks.doRefUpdatedHook(new Branch.NameKey(p, branch),
+                ObjectId.zeroId(),
+                r.getObjectId(), null);
+          } catch (Exception e) {
+
+          }
         }
         return true;
       }