Skip to content

Commit 28f4250

Browse files
committed
mingw: respect core.shell when executing scripts
On Windows, we have to emulate that Linux/Unix/macOS feature where a file starting with a `#!` line and being marked as executable is run as a script through the interpreter specified by said `#!` line. Traditionally, we ignore the actual path specified in that line because it will be a Unix-style path anyway, something that `git.exe` is not even supposed to understand. We then go on to look up the actual path of the interpreter by iterating over the components in the environment variable `PATH`. Let's special-case `sh` in that scenario when the config setting `core.shell` exists: in this case, we want to use it instead. This allows us to configure BusyBox' `ash` to be used for all of the shell scripting needs of the BusyBox flavor of MinGit. While at it, assume that any shell configured via `core.shell` is _not_ an MSYS2 shell, i.e. that we should use regular Win32 command-line quoting, not MSYS2/Cygwin one. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 5907f97 commit 28f4250

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

compat/mingw.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,10 @@ static char *path_lookup(const char *cmd, int exe_only)
20002000
if (strpbrk(cmd, "/\\"))
20012001
return xstrdup(cmd);
20022002

2003+
if (!strcmp(cmd, "sh") &&
2004+
(prog = xstrdup_or_null(get_shell_path(NULL))))
2005+
return prog;
2006+
20032007
path = mingw_getenv("PATH");
20042008
if (!path)
20052009
return NULL;
@@ -2193,6 +2197,12 @@ static int is_msys2_sh(const char *cmd)
21932197
if (ret >= 0)
21942198
return ret;
21952199

2200+
if (get_shell_path(NULL)) {
2201+
/* Assume an overridden shell is not MSYS2 */
2202+
ret = 0;
2203+
return ret;
2204+
}
2205+
21962206
p = path_lookup(cmd, 0);
21972207
if (!p)
21982208
ret = 0;

t/t0061-run-command.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,11 @@ test_expect_success 'core.shell' '
298298
grep "${SQ}a.b=c${SQ} is not a git command" actual
299299
'
300300

301+
test_expect_success MINGW 'core.shell executes scripts' '
302+
test_config_global core.shell "$GIT_EXEC_PATH/git$X" &&
303+
echo "#!/bin/sh" >script &&
304+
test_must_fail git -c alias.s="!./script" s 2>actual &&
305+
grep "${SQ}./script${SQ} is not a git command" actual
306+
'
307+
301308
test_done

0 commit comments

Comments
 (0)