git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/1] mingw: support spawning programs containing spaces in their names

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 16, 2019, 14:03 UTC
Message-ID
<bc9e45488c27452155e57336354d3e935fbc3c5d.1563285791.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.220.git.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

On some older Windows versions (e.g. Windows 7), the CreateProcessW() function does not really support spaces in its first argument, lpApplicationName. But it supports passing NULL as lpApplicationName, which makes it figure out the application from the (possibly quoted) first argument of lpCommandLine.

Let's use that trick (if we are certain that the first argument matches the executable's path) to support launching programs whose path contains spaces.

We will abuse the test-fake-ssh.exe helper to verify that this works and does not regress.

This fixes https://github.com/git-for-windows/git/issues/692
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/mingw.c         | 8 +++++---
 t/t0061-run-command.sh | 6 ++++++
 2 files changed, 11 insertions(+), 3 deletions(-)
diff --git a/compat/mingw.c b/compat/mingw.c
index 9b6d2400e1..6d7fc07a48 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1437,7 +1437,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
 	si.hStdOutput = winansi_get_osfhandle(fhout);
 	si.hStdError = winansi_get_osfhandle(fherr);
 
-	if (xutftowcs_path(wcmd, cmd) < 0)
+	if (*argv && !strcmp(cmd, *argv))
+		wcmd[0] = L'\0';
+	else if (xutftowcs_path(wcmd, cmd) < 0)
 		return -1;
 	if (dir && xutftowcs_path(wdir, dir) < 0)
 		return -1;
@@ -1466,8 +1468,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
 	wenvblk = make_environment_block(deltaenv);
 
 	memset(&pi, 0, sizeof(pi));
-	ret = CreateProcessW(wcmd, wargs, NULL, NULL, TRUE, flags,
-		wenvblk, dir ? wdir : NULL, &si, &pi);
+	ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
+		flags, wenvblk, dir ? wdir : NULL, &si, &pi);
 
 	free(wenvblk);
 	free(wargs);
diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh
index ebc49561ac..015fac8b5d 100755
--- a/t/t0061-run-command.sh
+++ b/t/t0061-run-command.sh
@@ -210,4 +210,10 @@ test_expect_success MINGW 'verify curlies are quoted properly' '
 	test_cmp expect actual
 '
 
+test_expect_success MINGW 'can spawn with argv[0] containing spaces' '
+	cp "$GIT_BUILD_DIR/t/helper/test-fake-ssh$X" ./ &&
+	test_must_fail "$PWD/test-fake-ssh$X" 2>err &&
+	grep TRASH_DIRECTORY err
+'
+
 test_done
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadget
Message 2 of 2 in “mingw: support spawning programs with paths containing spacesnames”
  1. 0/1 mingw: support spawning programs with paths containing spacesnamesJohannes Schindelin via GitGitGadget, Jul 16, 2019
  2. 1/1 mingw: support spawning programs containing spaces in their namesJohannes Schindelin via GitGitGadget, Jul 16, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.