Merge branch 'stable-2.11'

* stable-2.11:
  Include '/a' into HttpScheme URLs to trigger authentication

Change-Id: I588d47262d67d6b86f430d4a1afcef41bc3ceb7a
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/Module.java b/src/main/java/com/googlesource/gerrit/plugins/download/Module.java
index 14ff781..fa874d6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/Module.java
@@ -16,12 +16,14 @@
 
 import com.google.inject.AbstractModule;
 
+import com.googlesource.gerrit.plugins.download.command.CloneCommandsModule;
 import com.googlesource.gerrit.plugins.download.command.DownloadCommandsModule;
 import com.googlesource.gerrit.plugins.download.scheme.SchemeModule;
 
 class Module extends AbstractModule {
   @Override
   protected void configure() {
+    install(new CloneCommandsModule());
     install(new DownloadCommandsModule());
     install(new SchemeModule());
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommand.java
new file mode 100644
index 0000000..dac10f5
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommand.java
@@ -0,0 +1,25 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.download.command;
+
+import com.google.gerrit.extensions.config.DownloadScheme;
+
+public class CloneCommand extends com.google.gerrit.extensions.config.CloneCommand {
+
+  @Override
+  public String getCommand(DownloadScheme scheme, String project) {
+    return "git clone " + scheme.getUrl(project);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommandsModule.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommandsModule.java
new file mode 100644
index 0000000..acd867c
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommandsModule.java
@@ -0,0 +1,32 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.download.command;
+
+import com.google.gerrit.extensions.annotations.Exports;
+import com.google.gerrit.extensions.config.CloneCommand;
+import com.google.inject.AbstractModule;
+
+public class CloneCommandsModule extends AbstractModule {
+  @Override
+  protected void configure() {
+    bind(CloneCommand.class)
+      .annotatedWith(Exports.named("Clone"))
+      .to(com.googlesource.gerrit.plugins.download.command.CloneCommand.class);
+
+    bind(CloneCommand.class)
+        .annotatedWith(Exports.named("Clone with commit-msg hook"))
+        .to(CloneWithCommitMsgHook.class);
+  }
+}
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
new file mode 100644
index 0000000..d76615f
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
@@ -0,0 +1,59 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.download.command;
+
+import com.google.gerrit.extensions.config.DownloadScheme;
+import com.google.gerrit.server.CurrentUser;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
+import com.googlesource.gerrit.plugins.download.scheme.SshScheme;
+
+public class CloneWithCommitMsgHook extends CloneCommand {
+  private final SshScheme sshScheme;
+  private final Provider<CurrentUser> userProvider;
+
+  @Inject
+  CloneWithCommitMsgHook(SshScheme sshScheme,
+      Provider<CurrentUser> userProvider) {
+    this.sshScheme = sshScheme;
+    this.userProvider = userProvider;
+  }
+
+  @Override
+  public String getCommand(DownloadScheme scheme, String project) {
+    String username = userProvider.get().getUserName();
+    if (!sshScheme.isEnabled() || username == null) {
+      return null;
+    }
+
+    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();
+  }
+
+  private static String getBaseName(String project) {
+    return project.substring(project.lastIndexOf('/') + 1);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java
index 8253b66..e190569 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java
@@ -17,6 +17,7 @@
 import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme.DEFAULT_DOWNLOADS;
 import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme.SSH;
 
+import com.google.common.base.Strings;
 import com.google.gerrit.common.Nullable;
 import com.google.gerrit.extensions.config.DownloadScheme;
 import com.google.gerrit.server.CurrentUser;
@@ -32,6 +33,8 @@
 
 public class SshScheme extends DownloadScheme {
   private final String sshdAddress;
+  private final String sshdHost;
+  private final int sshdPort;
   private final Provider<CurrentUser> userProvider;
   private final boolean schemeAllowed;
 
@@ -49,6 +52,21 @@
       }
     }
     this.sshdAddress = sshAddr;
+
+    int port = 29418;
+    int p = sshdAddress.indexOf(":");
+    if (p > 0) {
+      this.sshdHost = sshdAddress.substring(0, p);
+      try {
+        port = Integer.parseInt(sshdAddress.substring(p + 1));
+      } catch (NumberFormatException e) {
+        // use default port
+      }
+    } else {
+      this.sshdHost = sshdAddress;
+    }
+    this.sshdPort = port;
+
     this.userProvider = userProvider;
     this.schemeAllowed = downloadConfig.getDownloadSchemes().contains(SSH)
         || downloadConfig.getDownloadSchemes().contains(DEFAULT_DOWNLOADS);
@@ -59,10 +77,14 @@
     if (!isEnabled() || !userProvider.get().isIdentifiedUser()) {
       return null;
     }
+    String username = userProvider.get().getUserName();
+    if (Strings.isNullOrEmpty(username)) {
+      return null;
+    }
 
     StringBuilder r = new StringBuilder();
     r.append("ssh://");
-    r.append(userProvider.get().getUserName());
+    r.append(username);
     r.append("@");
     r.append(ensureSlash(sshdAddress));
     r.append(project);
@@ -90,4 +112,12 @@
     }
     return in;
   }
+
+  public String getSshdHost() {
+    return sshdHost;
+  }
+
+  public int getSshdPort() {
+    return sshdPort;
+  }
 }
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index e4eae75..60634cd 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -16,6 +16,8 @@
 * `HTTP`: Scheme for authenticated downloads via the HTTP protocol.
 
 * `SSH`: Scheme for authenticated downloads via the SSH protocol.
+<br />
+Requires that users have a username.
 
 * `REPO`: Scheme for downloading with the Repo tool.
 
@@ -53,3 +55,15 @@
 * `Repo`:
 Command to download a patch set of a change with the Repo tool.
 
+Clone Commands
+--------------
+
+The following clone commands are defined by this plugin.
+
+* `Clone`:
+Standard git clone command.
+
+* `Clone with commit-msg hook`:
+Standard git clone command with scp command to copy the commit-msg hook
+into the newly cloned repository.
+