Always return the output from ref-update hook

Change I5905c44c7 moved the invocation of the ref-update hook out of
the ReceiveCommits implementation and into CommitValidators. Since
then, the output of the ref-update hook is not sent back to the git
client except in the case of an error or rejection.

Update the handling of the ref-update hook in the plugin so that its
output is returned back to the caller as a commit validation message
which is then sent to the client.

Change-Id: I88af8a3c690cc226214208c3c93f552f054ccae0
Reported-By: Robert Niemi <robert.den.klurige@googlemail.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java
index 8f8ea57..b95c2a3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/CommitReceived.java
@@ -17,6 +17,7 @@
 import static com.google.gerrit.reviewdb.client.RefNames.REFS_CHANGES;
 import static org.eclipse.jgit.lib.Constants.R_HEADS;
 
+import com.google.common.collect.ImmutableList;
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.events.CommitReceivedEvent;
 import com.google.gerrit.server.git.validators.CommitValidationException;
@@ -70,8 +71,14 @@
     args.add("--newrev", receiveEvent.commit.name());
 
     HookResult result = hook.run(args);
-    if (result != null && result.getExitValue() != 0) {
-      throw new CommitValidationException(result.toString().trim());
+    if (result != null) {
+      String output = result.toString();
+      if (result.getExitValue() != 0) {
+        throw new CommitValidationException(output);
+      }
+      if (!output.isEmpty()) {
+        return ImmutableList.of(new CommitValidationMessage(output, false));
+      }
     }
 
     return Collections.emptyList();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookResult.java b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookResult.java
index c9711db..36dab41 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/hooks/HookResult.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/hooks/HookResult.java
@@ -57,6 +57,6 @@
       sb.append(executionError);
     }
 
-    return sb.toString();
+    return sb.toString().trim();
   }
 }