Clone with commit-msg hook: Use curl if ssh scheme is disabled

If ssh scheme download was disabled, no command was offered to clone a
project with the commit-msg hook.

Create a curl command to download commit-msg hook when cloning using
http and use the existing scp command only when cloning through ssh.
Using scp requires uploading an ssh key to Gerrit. When using http clone
only, regular users would not need the ssh key anymore.

Change-Id: Ie7b6ce6f9769042caa541f80cf2cc758d184c044
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
index d76615f..16cd7d2 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
@@ -19,9 +19,14 @@
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 
+import com.googlesource.gerrit.plugins.download.scheme.AnonymousHttpScheme;
+import com.googlesource.gerrit.plugins.download.scheme.HttpScheme;
 import com.googlesource.gerrit.plugins.download.scheme.SshScheme;
 
 public class CloneWithCommitMsgHook extends CloneCommand {
+  private static final String HOOK = "hooks/commit-msg";
+  private static final String TARGET = " `git rev-parse --git-dir`/";
+
   private final SshScheme sshScheme;
   private final Provider<CurrentUser> userProvider;
 
@@ -35,22 +40,60 @@
   @Override
   public String getCommand(DownloadScheme scheme, String project) {
     String username = userProvider.get().getUserName();
-    if (!sshScheme.isEnabled() || username == null) {
+    if (username == null) {
       return null;
     }
+    String projectName = getBaseName(project);
 
-    return new StringBuilder()
-        .append(super.getCommand(scheme, project))
-        .append(" && scp -p -P ")
-        .append(sshScheme.getSshdPort())
-        .append(" ")
-        .append(username)
-        .append("@")
-        .append(sshScheme.getSshdHost())
-        .append(":hooks/commit-msg ")
-        .append(getBaseName(project))
-        .append("/.git/hooks/")
-        .toString();
+    if (scheme instanceof SshScheme) {
+      return new StringBuilder()
+          .append(super.getCommand(scheme, project))
+          .append(" && scp -p -P ")
+          .append(sshScheme.getSshdPort())
+          .append(" ")
+          .append(username)
+          .append("@")
+          .append(sshScheme.getSshdHost())
+          .append(":")
+          .append(HOOK)
+          .append(" ")
+          .append(projectName)
+          .append("/.git/hooks/")
+          .toString();
+    }
+
+    if (scheme instanceof HttpScheme || scheme instanceof AnonymousHttpScheme) {
+      String host = getHttpHost(scheme, project);
+      return new StringBuilder()
+          .append("git clone ")
+          .append(host)
+          .append(project)
+          .append(" && (cd ")
+          .append(projectName)
+          .append(" && curl -kLo")
+          .append(TARGET)
+          .append(HOOK)
+          .append(" ")
+          .append(host)
+          .append("tools/")
+          .append(HOOK)
+          .append("; chmod +x")
+          .append(TARGET)
+          .append(HOOK)
+          .append(")")
+          .toString();
+    }
+    return null;
+  }
+
+  private String getHttpHost(DownloadScheme scheme, String project) {
+    String host = scheme.getUrl(project);
+    host = host.substring(0, host.lastIndexOf(project));
+    int auth = host.lastIndexOf("/a/");
+    if (auth > -1) {
+      host = host.substring(0, auth + 1);
+    }
+    return host;
   }
 
   private static String getBaseName(String project) {
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 60634cd..68b4650 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -64,6 +64,6 @@
 Standard git clone command.
 
 * `Clone with commit-msg hook`:
-Standard git clone command with scp command to copy the commit-msg hook
+Standard git clone command with a command to copy the commit-msg hook
 into the newly cloned repository.