diff options
author | David Pursehouse <dpursehouse@collab.net> | 2018-10-09 09:25:44 +0900 |
---|---|---|
committer | David Pursehouse <dpursehouse@collab.net> | 2018-10-09 09:47:10 +0900 |
commit | 914a17d662ee13c9e881bec45777ba989b0039c6 (patch) | |
tree | d49d8c41512560f75fc5670cda4fabe650d124e4 | |
parent | 3e05704f5bbfd9e56f7e0fc9e8dfd996bc4b4f34 (diff) | |
parent | f6e7913d143b4e35c6d17ec9b592d9820a7132d8 (diff) |
Merge branch 'stable-2.14' into stable-2.15v2.15.5
* stable-2.14:
Fix access path propagation on git/ssh protocol
Change-Id: Ia38a54d210fb53dcb36a364f885cb1f2e17abf23
-rw-r--r-- | gerrit-sshd/src/main/java/com/google/gerrit/sshd/AbstractGitCommand.java | 43 | ||||
-rw-r--r-- | gerrit-sshd/src/main/java/com/google/gerrit/sshd/BaseCommand.java | 40 |
2 files changed, 52 insertions, 31 deletions
diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/AbstractGitCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/AbstractGitCommand.java index b9a98b9ba5..c747c3ea5d 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/AbstractGitCommand.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/AbstractGitCommand.java @@ -33,8 +33,6 @@ public abstract class AbstractGitCommand extends BaseCommand { @Argument(index = 0, metaVar = "PROJECT.git", required = true, usage = "project name") protected ProjectControl projectControl; - @Inject private SshScope sshScope; - @Inject private GitRepositoryManager repoManager; @Inject private SshSession session; @@ -53,28 +51,25 @@ public abstract class AbstractGitCommand extends BaseCommand { @Override public void start(Environment env) { Context ctx = context.subContext(newSession(), context.getCommandLine()); - final Context old = sshScope.set(ctx); - try { - startThread( - new ProjectCommandRunnable() { - @Override - public void executeParseCommand() throws Exception { - parseCommandLine(); - } - - @Override - public void run() throws Exception { - AbstractGitCommand.this.service(); - } - - @Override - public Project.NameKey getProjectName() { - return projectControl.getProjectState().getNameKey(); - } - }); - } finally { - sshScope.set(old); - } + startThreadWithContext( + ctx, + new ProjectCommandRunnable() { + @Override + public void executeParseCommand() throws Exception { + parseCommandLine(); + } + + @Override + public void run() throws Exception { + AbstractGitCommand.this.service(); + } + + @Override + public Project.NameKey getProjectName() { + Project project = projectControl.getProjectState().getProject(); + return project.getNameKey(); + } + }); } private SshSession newSession() { diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/BaseCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/BaseCommand.java index 788b33fbe9..c881b878db 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/BaseCommand.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/BaseCommand.java @@ -49,6 +49,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.charset.Charset; +import java.util.Optional; import java.util.concurrent.Future; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.atomic.AtomicReference; @@ -260,12 +261,12 @@ public abstract class BaseCommand implements Command { } /** - * Spawn a function into its own thread. + * Spawn a function into its own thread with the provided context. * * <p>Typically this should be invoked within {@link Command#start(Environment)}, such as: * * <pre> - * startThread(new CommandRunnable() { + * startThreadWithContext(SshScope.Context context, new CommandRunnable() { * public void run() throws Exception { * runImp(); * } @@ -277,8 +278,8 @@ public abstract class BaseCommand implements Command { * * @param thunk the runnable to execute on the thread, performing the command's logic. */ - protected void startThread(CommandRunnable thunk) { - final TaskThunk tt = new TaskThunk(thunk); + protected void startThreadWithContext(SshScope.Context context, CommandRunnable thunk) { + final TaskThunk tt = new TaskThunk(thunk, Optional.ofNullable(context)); if (isAdminHighPriorityCommand()) { // Admin commands should not block the main work threads (there @@ -291,6 +292,28 @@ public abstract class BaseCommand implements Command { } } + /** + * Spawn a function into its own thread. + * + * <p>Typically this should be invoked within {@link Command#start(Environment)}, such as: + * + * <pre> + * startThread(new CommandRunnable() { + * public void run() throws Exception { + * runImp(); + * } + * }); + * </pre> + * + * <p>If the function throws an exception, it is translated to a simple message for the client, a + * non-zero exit code, and the stack trace is logged. + * + * @param thunk the runnable to execute on the thread, performing the command's logic. + */ + protected void startThread(final CommandRunnable thunk) { + startThreadWithContext(null, thunk); + } + private boolean isAdminHighPriorityCommand() { if (getClass().getAnnotation(AdminHighPriorityCommand.class) != null) { try { @@ -413,18 +436,21 @@ public abstract class BaseCommand implements Command { private final class TaskThunk implements CancelableRunnable, ProjectRunnable { private final CommandRunnable thunk; + private final Context taskContext; private final String taskName; + private Project.NameKey projectName; - private TaskThunk(CommandRunnable thunk) { + private TaskThunk(CommandRunnable thunk, Optional<Context> oneOffContext) { this.thunk = thunk; this.taskName = getTaskName(); + this.taskContext = oneOffContext.orElse(context); } @Override public void cancel() { synchronized (this) { - final Context old = sshScope.set(context); + final Context old = sshScope.set(taskContext); try { onExit(STATUS_CANCEL); } finally { @@ -439,7 +465,7 @@ public abstract class BaseCommand implements Command { final Thread thisThread = Thread.currentThread(); final String thisName = thisThread.getName(); int rc = 0; - final Context old = sshScope.set(context); + final Context old = sshScope.set(taskContext); try { context.started = TimeUtil.nowMs(); thisThread.setName("SSH " + taskName); |