git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] connect: handle putty/plink also in GIT_SSH_COMMAND
@ 2017-01-02 12:09 Johannes Schindelin
  2017-01-08  2:33 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 38+ messages in thread
From: Johannes Schindelin @ 2017-01-02 12:09 UTC (permalink / raw)
  To: git; +Cc: Segev Finer, Junio C Hamano

From: Segev Finer <segev208@gmail.com>

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>
---
Published-As: https://github.com/dscho/git/releases/tag/putty-w-args-v1
Fetch-It-Via: git fetch https://github.com/dscho/git putty-w-args-v1


	Original Pull Request:
	https://github.com/git-for-windows/git/pull/1006

 connect.c        | 23 ++++++++++++++++-------
 t/t5601-clone.sh | 15 +++++++++++++++
 2 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/connect.c b/connect.c
index 8cb93b0720..c81f77001b 100644
--- a/connect.c
+++ b/connect.c
@@ -772,6 +772,7 @@ struct child_process *git_connect(int fd[2], const char *url,
 			int putty = 0, tortoiseplink = 0;
 			char *ssh_host = hostandport;
 			const char *port = NULL;
+			char *ssh_argv0 = NULL;
 			transport_check_allowed("ssh");
 			get_host_and_port(&ssh_host, &port);
 
@@ -792,10 +793,15 @@ struct child_process *git_connect(int fd[2], const char *url,
 			}
 
 			ssh = get_ssh_command();
-			if (!ssh) {
-				const char *base;
-				char *ssh_dup;
-
+			if (ssh) {
+				char *split_ssh = xstrdup(ssh);
+				const char **ssh_argv;
+
+				if (split_cmdline(split_ssh, &ssh_argv))
+					ssh_argv0 = xstrdup(ssh_argv[0]);
+				free(split_ssh);
+				free((void *)ssh_argv);
+			} else {
 				/*
 				 * GIT_SSH is the no-shell version of
 				 * GIT_SSH_COMMAND (and must remain so for
@@ -807,8 +813,11 @@ struct child_process *git_connect(int fd[2], const char *url,
 				if (!ssh)
 					ssh = "ssh";
 
-				ssh_dup = xstrdup(ssh);
-				base = basename(ssh_dup);
+				ssh_argv0 = xstrdup(ssh);
+			}
+
+			if (ssh_argv0) {
+				const char *base = basename(ssh_argv0);
 
 				tortoiseplink = !strcasecmp(base, "tortoiseplink") ||
 					!strcasecmp(base, "tortoiseplink.exe");
@@ -816,7 +825,7 @@ struct child_process *git_connect(int fd[2], const char *url,
 					!strcasecmp(base, "plink") ||
 					!strcasecmp(base, "plink.exe");
 
-				free(ssh_dup);
+				free(ssh_argv0);
 			}
 
 			argv_array_push(&conn->args, ssh);
diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh
index a433394200..5b228e2675 100755
--- a/t/t5601-clone.sh
+++ b/t/t5601-clone.sh
@@ -386,6 +386,21 @@ test_expect_success 'tortoiseplink is like putty, with extra arguments' '
 	expect_ssh "-batch -P 123" myhost src
 '
 
+test_expect_success 'double quoted plink.exe in GIT_SSH_COMMAND' '
+	copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
+	GIT_SSH_COMMAND="\"$TRASH_DIRECTORY/plink.exe\" -v" \
+		git clone "[myhost:123]:src" ssh-bracket-clone-plink-3 &&
+	expect_ssh "-v -P 123" myhost src
+'
+
+SQ="'"
+test_expect_success 'single quoted plink.exe in GIT_SSH_COMMAND' '
+	copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
+	GIT_SSH_COMMAND="$SQ$TRASH_DIRECTORY/plink.exe$SQ -v" \
+		git clone "[myhost:123]:src" ssh-bracket-clone-plink-4 &&
+	expect_ssh "-v -P 123" myhost src
+'
+
 # Reset the GIT_SSH environment variable for clone tests.
 setup_ssh_wrapper
 

base-commit: e05806da9ec4aff8adfed142ab2a2b3b02e33c8c
-- 
2.11.0.rc3.windows.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2017-02-01 23:07 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-02 12:09 [PATCH] connect: handle putty/plink also in GIT_SSH_COMMAND Johannes Schindelin
2017-01-08  2:33 ` Junio C Hamano
2017-01-09  1:08   ` Junio C Hamano
2017-01-09  7:46     ` Johannes Schindelin
2017-01-09  9:28       ` Junio C Hamano
2017-01-09 11:13         ` Johannes Schindelin
2017-01-09 14:19           ` Junio C Hamano
2017-01-25 12:34             ` Johannes Schindelin
2017-01-25 22:35               ` Junio C Hamano
2017-01-25 22:37                 ` Junio C Hamano
2017-01-26 14:45                   ` Johannes Schindelin
2017-01-25 22:40                 ` Jeff King
2017-01-25 23:25                   ` Junio C Hamano
2017-01-26 12:01                 ` Johannes Schindelin
2017-01-26 14:51 ` [PATCH v2 0/3] Handle PuTTY (plink/tortoiseplink) even " Johannes Schindelin
     [not found] ` <cover.1485442231.git.johannes.schindelin@gmx.de>
2017-01-26 14:51   ` [PATCH v2 1/3] connect: handle putty/plink also " Johannes Schindelin
2017-01-26 14:51   ` [PATCH v2 2/3] connect: rename tortoiseplink and putty variables Johannes Schindelin
2017-01-26 14:52   ` [PATCH v2 3/3] connect: Add the envvar GIT_SSH_VARIANT and ssh.variant config Johannes Schindelin
2017-01-26 19:27     ` Junio C Hamano
2017-01-27 10:35       ` Johannes Schindelin
2017-01-27 18:17       ` Junio C Hamano
2017-02-01 12:01         ` Johannes Schindelin
2017-02-01 16:53           ` Junio C Hamano
2017-02-01 11:57   ` [PATCH v3 0/4] Handle PuTTY (plink/tortoiseplink) even in GIT_SSH_COMMAND Johannes Schindelin
2017-02-01 12:01     ` [PATCH v3 1/4] connect: handle putty/plink also " Johannes Schindelin
2017-02-01 12:01     ` [PATCH v3 2/4] connect: rename tortoiseplink and putty variables Johannes Schindelin
2017-02-01 12:01     ` [PATCH v3 3/4] git_connect(): factor out SSH variant handling Johannes Schindelin
2017-02-01 12:01     ` [PATCH v3 4/4] connect: Add the envvar GIT_SSH_VARIANT and ssh.variant config Johannes Schindelin
2017-02-01 19:19       ` Junio C Hamano
2017-02-01 19:46         ` Junio C Hamano
2017-02-01 22:24           ` Johannes Schindelin
2017-02-01 22:33             ` Junio C Hamano
2017-02-01 22:42               ` Johannes Schindelin
2017-02-01 22:43               ` Junio C Hamano
2017-02-01 23:07                 ` Johannes Schindelin
2017-02-01 20:07     ` [PATCH v3 0/4] Handle PuTTY (plink/tortoiseplink) even in GIT_SSH_COMMAND Junio C Hamano
2017-02-01 22:17       ` Johannes Schindelin
2017-02-01 22:55         ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).