Skip to content

Commit 83aefa7

Browse files
committed
POSIX-ify the SHELL variable
When calling a non-MSys2 binary, all of the environment is converted from POSIX to Win32, including the SHELL environment variable. In Git for Windows, for example, `SHELL=/usr/bin/bash` is converted to `SHELL=C:\Program Files\Git\usr\bin\bash.exe` when calling the `git.exe` binary. This is appropriate because non-MSys2 binaries would not handle POSIX paths correctly. Under certain circumstances, however, `git.exe` calls an *MSys2* binary in turn, such as `git config --edit` calling `vim.exe` unless Git is configured to use another editor specifically. Now, when this "improved vi" calls shell commands, it uses that $SHELL variable *without quoting*, resulting in a nasty error: C:\Program: No such file or directory Many other programs behave in the same manner, assuming that $SHELL does not contain spaces and hence needs no quoting, unfortunately including some of Git's own scripts. Therefore let's make sure that $SHELL gets "posified" again when entering MSys2 programs. Earlier attempts by Git for Windows contributors claimed that adding `SHELL` to the `conv_envvars` array does not have the intended effect. These reports just missed that the `conv_start_chars` array (which makes the code more performant) needs to be adjusted, too. Note that we set the `immediate` flag to `true` so that the environment variable is set immediately by the MSys2 runtime, i.e. not only spawned processes will see the POSIX-ified `SHELL` variable, but the MSys2 runtime *itself*, too. This fixes git-for-windows/git#542, git-for-windows/git#498, and git-for-windows/git#468. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 501d56d commit 83aefa7

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

winsup/cygwin/environ.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ static win_env conv_envvars[] =
309309
{NL ("HOME="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
310310
{NL ("LD_LIBRARY_PATH="), NULL, NULL,
311311
env_plist_to_posix, env_plist_to_win32, true},
312+
{NL ("SHELL="), NULL, NULL, env_path_to_posix, env_path_to_win32, true, true},
312313
{NL ("TMPDIR="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
313314
{NL ("TMP="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
314315
{NL ("TEMP="), NULL, NULL, env_path_to_posix, env_path_to_win32, false},
@@ -337,7 +338,7 @@ static const unsigned char conv_start_chars[256] =
337338
WC, 0, 0, 0, WC, 0, 0, 0,
338339
/* 80 */
339340
/* P Q R S T U V W */
340-
WC, 0, 0, 0, WC, 0, 0, 0,
341+
WC, 0, 0, WC, WC, 0, 0, 0,
341342
/* 88 */
342343
/* x Y Z */
343344
0, 0, 0, 0, 0, 0, 0, 0,
@@ -387,6 +388,8 @@ win_env::add_cache (const char *in_posix, const char *in_native)
387388
native = (char *) realloc (native, namelen + 1 + strlen (in_native));
388389
stpcpy (stpcpy (native, name), in_native);
389390
}
391+
else if (skip_if_empty && !*in_posix)
392+
native = (char *) calloc(1, 1);
390393
else
391394
{
392395
tmp_pathbuf tp;
@@ -452,6 +455,8 @@ posify_maybe (char **here, const char *value, char *outenv)
452455
return;
453456

454457
int len = strcspn (src, "=") + 1;
458+
if (conv->skip_if_empty && !src[len])
459+
return;
455460

456461
/* Turn all the items from c:<foo>;<bar> into their
457462
mounted equivalents - if there is one. */

winsup/cygwin/environ.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct win_env
2121
char *native;
2222
ssize_t (*toposix) (const void *, void *, size_t);
2323
ssize_t (*towin32) (const void *, void *, size_t);
24-
bool immediate;
24+
bool immediate, skip_if_empty;
2525
void __reg3 add_cache (const char *in_posix, const char *in_native = NULL);
2626
const char * get_native () const {return native ? native + namelen : NULL;}
2727
const char * get_posix () const {return posix ? posix : NULL;}

0 commit comments

Comments
 (0)