summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/transport/ssh/git
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/gitblit/transport/ssh/git')
-rw-r--r--src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java104
-rw-r--r--src/main/java/com/gitblit/transport/ssh/git/GitDispatchCommand.java61
-rw-r--r--src/main/java/com/gitblit/transport/ssh/git/Receive.java33
-rw-r--r--src/main/java/com/gitblit/transport/ssh/git/Upload.java33
4 files changed, 231 insertions, 0 deletions
diff --git a/src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java b/src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java
new file mode 100644
index 00000000..b203d476
--- /dev/null
+++ b/src/main/java/com/gitblit/transport/ssh/git/BaseGitCommand.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * 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.gitblit.transport.ssh.git;
+
+import java.io.IOException;
+
+import org.apache.sshd.server.Environment;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
+import org.eclipse.jgit.transport.resolver.UploadPackFactory;
+import org.kohsuke.args4j.Argument;
+
+import com.gitblit.git.GitblitReceivePackFactory;
+import com.gitblit.git.GitblitUploadPackFactory;
+import com.gitblit.git.RepositoryResolver;
+import com.gitblit.transport.ssh.SshDaemonClient;
+import com.gitblit.transport.ssh.commands.BaseCommand;
+
+/**
+ * @author Eric Myhre
+ *
+ */
+abstract class BaseGitCommand extends BaseCommand {
+ @Argument(index = 0, metaVar = "REPOSITORY", required = true, usage = "repository name")
+ protected String repository;
+
+ protected RepositoryResolver<SshDaemonClient> repositoryResolver;
+ protected ReceivePackFactory<SshDaemonClient> receivePackFactory;
+ protected UploadPackFactory<SshDaemonClient> uploadPackFactory;
+
+ protected Repository repo;
+
+ @Override
+ public void start(final Environment env) {
+ startThread(new RepositoryCommandRunnable() {
+ @Override
+ public void run() throws Exception {
+ parseCommandLine();
+ BaseGitCommand.this.service();
+ }
+
+ @Override
+ public String getRepository() {
+ return repository;
+ }
+ });
+ }
+
+ private void service() throws IOException, Failure {
+ try {
+ repo = openRepository();
+ runImpl();
+ } finally {
+ if (repo != null) {
+ repo.close();
+ }
+ }
+ }
+
+ protected abstract void runImpl() throws IOException, Failure;
+
+ protected Repository openRepository() throws Failure {
+ // Assume any attempt to use \ was by a Windows client
+ // and correct to the more typical / used in Git URIs.
+ //
+ repository = repository.replace('\\', '/');
+ // ssh://git@thishost/path should always be name="/path" here
+ //
+ if (!repository.startsWith("/")) {
+ throw new Failure(1, "fatal: '" + repository + "': not starts with / character");
+ }
+ repository = repository.substring(1);
+ try {
+ return repositoryResolver.open(ctx.getClient(), repository);
+ } catch (Exception e) {
+ throw new Failure(1, "fatal: '" + repository + "': not a git archive", e);
+ }
+ }
+
+ public void setRepositoryResolver(RepositoryResolver<SshDaemonClient> repositoryResolver) {
+ this.repositoryResolver = repositoryResolver;
+ }
+
+ public void setReceivePackFactory(GitblitReceivePackFactory<SshDaemonClient> receivePackFactory) {
+ this.receivePackFactory = receivePackFactory;
+ }
+
+ public void setUploadPackFactory(GitblitUploadPackFactory<SshDaemonClient> uploadPackFactory) {
+ this.uploadPackFactory = uploadPackFactory;
+ }
+} \ No newline at end of file
diff --git a/src/main/java/com/gitblit/transport/ssh/git/GitDispatchCommand.java b/src/main/java/com/gitblit/transport/ssh/git/GitDispatchCommand.java
new file mode 100644
index 00000000..adeace52
--- /dev/null
+++ b/src/main/java/com/gitblit/transport/ssh/git/GitDispatchCommand.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * 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.gitblit.transport.ssh.git;
+
+import com.gitblit.git.GitblitReceivePackFactory;
+import com.gitblit.git.GitblitUploadPackFactory;
+import com.gitblit.git.RepositoryResolver;
+import com.gitblit.manager.IGitblit;
+import com.gitblit.models.UserModel;
+import com.gitblit.transport.ssh.CommandMetaData;
+import com.gitblit.transport.ssh.SshCommandContext;
+import com.gitblit.transport.ssh.SshDaemonClient;
+import com.gitblit.transport.ssh.commands.BaseCommand;
+import com.gitblit.transport.ssh.commands.DispatchCommand;
+
+@CommandMetaData(name = "git", description="Dispatcher for git receive and upload commands", hidden = true)
+public class GitDispatchCommand extends DispatchCommand {
+
+ protected RepositoryResolver<SshDaemonClient> repositoryResolver;
+ protected GitblitUploadPackFactory<SshDaemonClient> uploadPackFactory;
+ protected GitblitReceivePackFactory<SshDaemonClient> receivePackFactory;
+
+ @Override
+ public void setContext(SshCommandContext context) {
+ super.setContext(context);
+
+ IGitblit gitblit = context.getGitblit();
+ repositoryResolver = new RepositoryResolver<SshDaemonClient>(gitblit);
+ uploadPackFactory = new GitblitUploadPackFactory<SshDaemonClient>(gitblit);
+ receivePackFactory = new GitblitReceivePackFactory<SshDaemonClient>(gitblit);
+ }
+
+ @Override
+ protected void registerCommands(UserModel user) {
+ registerCommand(user, Upload.class);
+ registerCommand(user, Receive.class);
+ }
+
+ @Override
+ protected void provideStateTo(final BaseCommand cmd) {
+ super.provideStateTo(cmd);
+
+ BaseGitCommand a = (BaseGitCommand) cmd;
+ a.setRepositoryResolver(repositoryResolver);
+ a.setUploadPackFactory(uploadPackFactory);
+ a.setReceivePackFactory(receivePackFactory);
+ }
+}
diff --git a/src/main/java/com/gitblit/transport/ssh/git/Receive.java b/src/main/java/com/gitblit/transport/ssh/git/Receive.java
new file mode 100644
index 00000000..4089f1df
--- /dev/null
+++ b/src/main/java/com/gitblit/transport/ssh/git/Receive.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * 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.gitblit.transport.ssh.git;
+
+import org.eclipse.jgit.transport.ReceivePack;
+
+import com.gitblit.transport.ssh.CommandMetaData;
+
+@CommandMetaData(name = "git-receive-pack", description = "Receives pushes from a client")
+public class Receive extends BaseGitCommand {
+ @Override
+ protected void runImpl() throws Failure {
+ try {
+ ReceivePack rp = receivePackFactory.create(ctx.getClient(), repo);
+ rp.receive(in, out, null);
+ } catch (Exception e) {
+ throw new Failure(1, "fatal: Cannot receive pack: ", e);
+ }
+ }
+}
diff --git a/src/main/java/com/gitblit/transport/ssh/git/Upload.java b/src/main/java/com/gitblit/transport/ssh/git/Upload.java
new file mode 100644
index 00000000..5793c3e6
--- /dev/null
+++ b/src/main/java/com/gitblit/transport/ssh/git/Upload.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2014 gitblit.com.
+ *
+ * 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.gitblit.transport.ssh.git;
+
+import org.eclipse.jgit.transport.UploadPack;
+
+import com.gitblit.transport.ssh.CommandMetaData;
+
+@CommandMetaData(name = "git-upload-pack", description = "Sends packs to a client for clone and fetch")
+public class Upload extends BaseGitCommand {
+ @Override
+ protected void runImpl() throws Failure {
+ try {
+ UploadPack up = uploadPackFactory.create(ctx.getClient(), repo);
+ up.upload(in, out, null);
+ } catch (Exception e) {
+ throw new Failure(1, "fatal: Cannot upload pack: ", e);
+ }
+ }
+} \ No newline at end of file