Skip to content

Commit eb7c786

Browse files
dschogitster
authored andcommitted
mingw: support spawning programs containing spaces in their names
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 git-for-windows#692 Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b697d92 commit eb7c786

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

compat/mingw.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14371437
si.hStdOutput = winansi_get_osfhandle(fhout);
14381438
si.hStdError = winansi_get_osfhandle(fherr);
14391439

1440-
if (xutftowcs_path(wcmd, cmd) < 0)
1440+
if (*argv && !strcmp(cmd, *argv))
1441+
wcmd[0] = L'\0';
1442+
else if (xutftowcs_path(wcmd, cmd) < 0)
14411443
return -1;
14421444
if (dir && xutftowcs_path(wdir, dir) < 0)
14431445
return -1;
@@ -1466,8 +1468,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14661468
wenvblk = make_environment_block(deltaenv);
14671469

14681470
memset(&pi, 0, sizeof(pi));
1469-
ret = CreateProcessW(wcmd, wargs, NULL, NULL, TRUE, flags,
1470-
wenvblk, dir ? wdir : NULL, &si, &pi);
1471+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
1472+
flags, wenvblk, dir ? wdir : NULL, &si, &pi);
14711473

14721474
free(wenvblk);
14731475
free(wargs);

t/t0061-run-command.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,10 @@ test_expect_success MINGW 'verify curlies are quoted properly' '
210210
test_cmp expect actual
211211
'
212212

213+
test_expect_success MINGW 'can spawn with argv[0] containing spaces' '
214+
cp "$GIT_BUILD_DIR/t/helper/test-fake-ssh$X" ./ &&
215+
test_must_fail "$PWD/test-fake-ssh$X" 2>err &&
216+
grep TRASH_DIRECTORY err
217+
'
218+
213219
test_done

0 commit comments

Comments
 (0)