connect: handle putty/plink also in GIT_SSH_COMMAND

Git for Windows has special support for the popular SSH client PuTTY: when using PuTTY's non-interactive version ("plink.exe"), we use the -P option to specify the port rather than OpenSSH's -p option. TortoiseGit ships with its own, forked version of plink.exe, that adds support for the -batch option, and for good measure we special-case that, too. However, this special-casing of PuTTY only covers the case where the user overrides the SSH command via the environment variable GIT_SSH (which allows specifying the name of the executable), not GIT_SSH_COMMAND (which allows specifying a full command, including additional command-line options). When users want to pass any additional arguments to (Tortoise-)Plink, such as setting a private key, they are required to either use a shell script named plink or tortoiseplink or duplicate the logic that is already in Git for passing the correct style of command line arguments, which can be difficult, error prone and annoying to get right. This patch simply reuses the existing logic and expands it to cover GIT_SSH_COMMAND, too. Note: it may look a little heavy-handed to duplicate the entire command-line and then split it, only to extract the name of the executable. However, this is not a performance-critical code path, and the code is much more readable this way. Signed-off-by: Segev Finer <segev208@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Segev Finer committed Jan 2, 2017 at 13:09 UTC e9d9a8a4d281d1f5945c710c65b6f6ee26478923
2 files changed +31 -7
connect.c
+16 -7
@@ -772,6 +772,7 @@ struct child_process *git_connect(int fd[2], const char *url,
772 int putty = 0, tortoiseplink = 0;
773 char *ssh_host = hostandport;
774 const char *port = NULL;
775 + char *ssh_argv0 = NULL;
776 transport_check_allowed("ssh");
777 get_host_and_port(&ssh_host, &port);
778
@@ -792,10 +793,15 @@ struct child_process *git_connect(int fd[2], const char *url,
793 }
794
795 ssh = get_ssh_command();
795 - if (!ssh) {
796 - const char *base;
797 - char *ssh_dup;
798 -
796 + if (ssh) {
797 + char *split_ssh = xstrdup(ssh);
798 + const char **ssh_argv;
799 +
800 + if (split_cmdline(split_ssh, &ssh_argv))
801 + ssh_argv0 = xstrdup(ssh_argv[0]);
802 + free(split_ssh);
803 + free((void *)ssh_argv);
804 + } else {
805 /*
806 * GIT_SSH is the no-shell version of
807 * GIT_SSH_COMMAND (and must remain so for
@@ -807,8 +813,11 @@ struct child_process *git_connect(int fd[2], const char *url,
813 if (!ssh)
814 ssh = "ssh";
815
810 - ssh_dup = xstrdup(ssh);
811 - base = basename(ssh_dup);
816 + ssh_argv0 = xstrdup(ssh);
817 + }
818 +
819 + if (ssh_argv0) {
820 + const char *base = basename(ssh_argv0);
821
822 tortoiseplink = !strcasecmp(base, "tortoiseplink") ||
823 !strcasecmp(base, "tortoiseplink.exe");
@@ -816,7 +825,7 @@ struct child_process *git_connect(int fd[2], const char *url,
825 !strcasecmp(base, "plink") ||
826 !strcasecmp(base, "plink.exe");
827
819 - free(ssh_dup);
828 + free(ssh_argv0);
829 }
830
831 argv_array_push(&conn->args, ssh);
t/t5601-clone.sh
+15
@@ -386,6 +386,21 @@ test_expect_success 'tortoiseplink is like putty, with extra arguments' '
386 expect_ssh "-batch -P 123" myhost src
387 '
388
389 +test_expect_success 'double quoted plink.exe in GIT_SSH_COMMAND' '
390 + copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
391 + GIT_SSH_COMMAND="\"$TRASH_DIRECTORY/plink.exe\" -v" \
392 + git clone "[myhost:123]:src" ssh-bracket-clone-plink-3 &&
393 + expect_ssh "-v -P 123" myhost src
394 +'
395 +
396 +SQ="'"
397 +test_expect_success 'single quoted plink.exe in GIT_SSH_COMMAND' '
398 + copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
399 + GIT_SSH_COMMAND="$SQ$TRASH_DIRECTORY/plink.exe$SQ -v" \
400 + git clone "[myhost:123]:src" ssh-bracket-clone-plink-4 &&
401 + expect_ssh "-v -P 123" myhost src
402 +'
403 +
404 # Reset the GIT_SSH environment variable for clone tests.
405 setup_ssh_wrapper
406