Trim multi-line arguments for task name and ssh_log

Previously, the task name of review command could be multi line string
because of --message argument.

Keep only the first line of the multi-line arguments to improve
readability for the task name in output of show-queue command and logs.
This applies to any multi line argument of any command, not only to
review command.

Change-Id: Ic8f3c167a73fae9f5f883e8cc3b43f84647e1b8d
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 e38c080..59972e2 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
@@ -16,6 +16,7 @@
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 
+import com.google.common.base.Joiner;
 import com.google.common.util.concurrent.Atomics;
 import com.google.gerrit.common.Nullable;
 import com.google.gerrit.common.TimeUtil;
@@ -98,6 +99,9 @@
   /** Unparsed command line options. */
   private String[] argv;
 
+  /** trimmed command line arguments. */
+  private String[] trimmedArgv;
+
   public BaseCommand() {
     task = Atomics.newReference();
   }
@@ -143,6 +147,26 @@
     this.argv = argv;
   }
 
+  /**
+   * Trim the argument if it is spanning multiple lines.
+   *
+   * @return the arguments where all the multiple-line fields are trimmed.
+   */
+  protected String[] getTrimmedArguments() {
+    if (trimmedArgv == null && argv != null) {
+      trimmedArgv = new String[argv.length];
+      for (int i = 0; i < argv.length; i++) {
+        String arg = argv[i];
+        int indexOfMultiLine = arg.indexOf("\n");
+        if (indexOfMultiLine > -1) {
+          arg = arg.substring(0, indexOfMultiLine).concat(" [trimmed]");
+        }
+        trimmedArgv[i] = arg;
+      }
+    }
+    return trimmedArgv;
+  }
+
   @Override
   public void destroy() {
     Future<?> future = task.getAndSet(null);
@@ -370,8 +394,11 @@
   }
 
   protected String getTaskDescription() {
-    StringBuilder m = new StringBuilder();
-    m.append(context.getCommandLine());
+    StringBuilder m = new StringBuilder(commandName);
+    String[] ta = getTrimmedArguments();
+    if (ta != null) {
+      m.append(Joiner.on(" ").join(ta));
+    }
     return m.toString();
   }
 
diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshLog.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshLog.java
index dfd56f1..5d840f9 100644
--- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshLog.java
+++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshLog.java
@@ -14,6 +14,7 @@
 
 package com.google.gerrit.sshd;
 
+import com.google.common.base.Joiner;
 import com.google.common.collect.ListMultimap;
 import com.google.common.collect.MultimapBuilder;
 import com.google.gerrit.audit.AuditService;
@@ -282,9 +283,9 @@
       return "Command was already destroyed";
     }
     StringBuilder commandName = new StringBuilder(dcmd.getCommandName());
-    String[] args = dcmd.getArguments();
-    for (int i = 1; i < args.length; i++) {
-      commandName.append(".").append(args[i]);
+    String[] trimmedArgs = dcmd.getTrimmedArguments();
+    if (trimmedArgs != null) {
+      commandName.append(Joiner.on(".").join(trimmedArgs));
     }
     return commandName.toString();
   }