Implement clone commands At the moment the clone commands are built on Gerrit client-side. Now this plugin provides these clone commands via the new CloneCommand extension point and in a next step the Gerrit client can retrieve the clone commands via the /config/server/info REST endpoint so that the client code to build the clone commands can be removed. Change-Id: I2f284b3995ebe3ab17df331713be3d76c9e08dd6 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
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..50a11c6 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneWithCommitMsgHook.java
@@ -0,0 +1,55 @@ +// 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(project) + .append("/.git/hooks/") + .toString(); + } +}
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 6e7cb15..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
@@ -33,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; @@ -50,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); @@ -95,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 f5b697e..60634cd 100644 --- a/src/main/resources/Documentation/about.md +++ b/src/main/resources/Documentation/about.md
@@ -55,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. +