Skip to content

Commit

Permalink
mingw: respect core.shell when executing scripts
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dscho committed Jul 2, 2021
1 parent 149f67a commit acbd1f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,10 @@ static char *path_lookup(const char *cmd, int exe_only)
if (strpbrk(cmd, "/\\"))
return xstrdup(cmd);

if (!strcmp(cmd, "sh") &&
(prog = xstrdup_or_null(get_shell_path(NULL))))
return prog;

path = mingw_getenv("PATH");
if (!path)
return NULL;
Expand Down Expand Up @@ -1463,6 +1467,12 @@ static int is_msys2_sh(const char *cmd)
if (ret >= 0)
return ret;

if (get_shell_path(NULL)) {
/* Assume an overridden shell is not MSYS2 */
ret = 0;
return ret;
}

p = path_lookup(cmd, 0);
if (!p)
ret = 0;
Expand Down
7 changes: 7 additions & 0 deletions t/t0061-run-command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,11 @@ test_expect_success 'core.shell' '
grep "${SQ}a.b=c${SQ} is not a git command" actual
'

test_expect_success MINGW 'core.shell executes scripts' '
test_config_global core.shell "$GIT_EXEC_PATH/git$X" &&
echo "#!/bin/sh" >script &&
test_must_fail git -c alias.s="!./script" s 2>actual &&
grep "${SQ}./script${SQ} is not a git command" actual
'

test_done

0 comments on commit acbd1f3

Please sign in to comment.