From e3b636e7fa2a823cfe90ea75e88034a60f7e59e6 Mon Sep 17 00:00:00 2001
From: David Ostrovsky <david@ostrovsky.org>
Date: Thu, 10 Apr 2014 18:58:07 -0400
Subject: [PATCH] SSHD: Add support for git pack commands

---
 src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java |   38 +++++++++++++++++++++++---------------
 1 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java
index fd73ccf..a04c505 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/BaseCommand.java
@@ -33,8 +33,10 @@
 import org.slf4j.LoggerFactory;
 
 import com.gitblit.transport.ssh.AbstractSshCommand;
+import com.gitblit.transport.ssh.SshContext;
 import com.gitblit.utils.IdGenerator;
 import com.gitblit.utils.WorkQueue;
+import com.gitblit.utils.WorkQueue.CancelableRunnable;
 import com.gitblit.utils.cli.CmdLineParser;
 import com.google.common.base.Charsets;
 import com.google.common.util.concurrent.Atomics;
@@ -49,6 +51,9 @@
   /** Unparsed command line options. */
   private String[] argv;
 
+  /** Ssh context */
+  protected SshContext ctx;
+
   /** The task, as scheduled on a worker thread. */
   private final AtomicReference<Future<?>> task;
 
@@ -59,6 +64,10 @@
     IdGenerator gen = new IdGenerator();
     WorkQueue w = new WorkQueue(gen);
     this.executor = w.getDefaultQueue();
+  }
+
+  public void setContext(SshContext ctx) {
+	this.ctx = ctx;
   }
 
   public void setInputStream(final InputStream in) {
@@ -77,7 +86,10 @@
     this.exit = callback;
   }
 
-  protected void provideStateTo(final Command cmd) {
+  protected void provideBaseStateTo(final Command cmd) {
+    if (cmd instanceof BaseCommand) {
+	  ((BaseCommand)cmd).setContext(ctx);
+    }
     cmd.setInputStream(in);
     cmd.setOutputStream(out);
     cmd.setErrorStream(err);
@@ -155,31 +167,25 @@
     return "";
   }
 
-  private final class TaskThunk implements com.gitblit.utils.WorkQueue.CancelableRunnable {
+  private final class TaskThunk implements CancelableRunnable {
     private final CommandRunnable thunk;
     private final String taskName;
 
     private TaskThunk(final CommandRunnable thunk) {
       this.thunk = thunk;
 
-      // TODO
-//      StringBuilder m = new StringBuilder("foo");
-//      m.append(context.getCommandLine());
-//      if (userProvider.get().isIdentifiedUser()) {
-//        IdentifiedUser u = (IdentifiedUser) userProvider.get();
-//        m.append(" (").append(u.getAccount().getUserName()).append(")");
-//      }
-      this.taskName = "foo";//m.toString();
+      StringBuilder m = new StringBuilder();
+      m.append(ctx.getCommandLine());
+      this.taskName = m.toString();
     }
 
     @Override
     public void cancel() {
       synchronized (this) {
-        //final Context old = sshScope.set(context);
         try {
           //onExit(/*STATUS_CANCEL*/);
         } finally {
-          //sshScope.set(old);
+          ctx = null;
         }
       }
     }
@@ -190,11 +196,8 @@
         final Thread thisThread = Thread.currentThread();
         final String thisName = thisThread.getName();
         int rc = 0;
-        //final Context old = sshScope.set(context);
         try {
-          //context.started = TimeUtil.nowMs();
           thisThread.setName("SSH " + taskName);
-
           thunk.run();
 
           out.flush();
@@ -231,6 +234,11 @@
   }
 
 
+  /** Runnable function which can retrieve a project name related to the task */
+  public static interface RepositoryCommandRunnable extends CommandRunnable {
+	public String getRepository();
+  }
+
   /**
    * Spawn a function into its own thread.
    * <p>

--
Gitblit v1.9.1