Skip to content

Commit

Permalink
mingw: allow git.exe to be used instead of the "Git wrapper"
Browse files Browse the repository at this point in the history
Git for Windows wants to add `git.exe` to the users' `PATH`, without
cluttering the latter with unnecessary executables such as `wish.exe`.
To that end, it invented the concept of its "Git wrapper", i.e. a tiny
executable located in `C:\Program Files\Git\cmd\git.exe` (originally a
CMD script) whose sole purpose is to set up a couple of environment
variables and then spawn the _actual_ `git.exe` (which nowadays lives in
`C:\Program Files\Git\mingw64\bin\git.exe` for 64-bit, and the obvious
equivalent for 32-bit installations).

Currently, the following environment variables are set unless already
initialized:

- `MSYSTEM`, to make sure that the MSYS2 Bash and the MSYS2 Perl
  interpreter behave as expected, and

- `PLINK_PROTOCOL`, to force PuTTY's `plink.exe` to use the SSH
  protocol instead of Telnet,

- `PATH`, to make sure that the `bin` folder in the user's home
  directory, as well as the `/mingw64/bin` and the `/usr/bin`
  directories are included. The trick here is that the `/mingw64/bin/`
  and `/usr/bin/` directories are relative to the top-level installation
  directory of Git for Windows (which the included Bash interprets as
  `/`, i.e. as the MSYS pseudo root directory).

Using the absence of `MSYSTEM` as a tell-tale, we can detect in
`git.exe` whether these environment variables have been initialized
properly. Therefore we can call `C:\Program Files\Git\mingw64\bin\git`
in-place after this change, without having to call Git through the Git
wrapper.

Obviously, above-mentioned directories must be _prepended_ to the `PATH`
variable, otherwise we risk picking up executables from unrelated Git
installations. We do that by constructing the new `PATH` value from
scratch, appending `$HOME/bin` (if `HOME` is set), then the MSYS2 system
directories, and then appending the original `PATH`.

Side note: this modification of the `PATH` variable is independent of
the modification necessary to reach the executables and scripts in
`/mingw64/libexec/git-core/`, i.e. the `GIT_EXEC_PATH`. That
modification is still performed by Git, elsewhere, long after making the
changes described above.

While we _still_ cannot simply hard-link `mingw64\bin\git.exe` to `cmd`
(because the former depends on a couple of `.dll` files that are only in
`mingw64\bin`, i.e. calling `...\cmd\git.exe` would fail to load due to
missing dependencies), at least we can now avoid that extra process of
running the Git wrapper (which then has to wait for the spawned
`git.exe` to finish) by calling `...\mingw64\bin\git.exe` directly, via
its absolute path.

Testing this is in Git's test suite tricky: we set up a "new" MSYS
pseudo-root and copy the `git.exe` file into the appropriate location,
then verify that `MSYSTEM` is set properly, and also that the `PATH` is
modified so that scripts can be found in `$HOME/bin`, `/mingw64/bin/`
and `/usr/bin/`.

This addresses #2283

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Oct 20, 2024
1 parent 9541183 commit 7912ea2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
69 changes: 69 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2625,6 +2625,47 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
return -1;
}

#ifdef ENSURE_MSYSTEM_IS_SET
static size_t append_system_bin_dirs(char *path, size_t size)
{
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
return 0;
#else
char prefix[32768];
const char *slash;
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;

if (len == 0 || len >= sizeof(prefix) ||
!(slash = find_last_dir_sep(prefix)))
return 0;
/* strip trailing `git.exe` */
len = slash - prefix;

/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
off += xsnprintf(path + off, size - off,
"%.*s\\mingw64\\bin;", (int)len, prefix);
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
off += xsnprintf(path + off, size - off,
"%.*s\\mingw32\\bin;", (int)len, prefix);
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
strip_suffix_mem(prefix, &len, "\\bin") ||
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
off += xsnprintf(path + off, size - off,
"%.*s\\mingw%d\\bin;", (int)len, prefix,
(int)(sizeof(void *) * 8));
else
return 0;

off += xsnprintf(path + off, size - off,
"%.*s\\usr\\bin;", (int)len, prefix);
return off;
#endif
}
#endif

static void setup_windows_environment(void)
{
char *tmp = getenv("TMPDIR");
Expand Down Expand Up @@ -2677,6 +2718,34 @@ static void setup_windows_environment(void)
setenv("HOME", tmp, 1);
}

if (!getenv("PLINK_PROTOCOL"))
setenv("PLINK_PROTOCOL", "ssh", 0);

#ifdef ENSURE_MSYSTEM_IS_SET
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
const char *home = getenv("HOME"), *path = getenv("PATH");
char buf[32768];
size_t off = 0;

xsnprintf(buf, sizeof(buf),
"MINGW%d", (int)(sizeof(void *) * 8));
setenv("MSYSTEM", buf, 1);

if (home)
off += xsnprintf(buf + off, sizeof(buf) - off,
"%s\\bin;", home);
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
if (path)
off += xsnprintf(buf + off, sizeof(buf) - off,
"%s", path);
else if (off > 0)
buf[off - 1] = '\0';
else
buf[0] = '\0';
setenv("PATH", buf, 1);
}
#endif

if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
setenv("LC_CTYPE", "C.UTF-8", 1);
}
Expand Down
4 changes: 2 additions & 2 deletions config.mak.uname
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ endif
compat/win32/pthread.o compat/win32/syslog.o \
compat/win32/trace2_win32_process_info.o \
compat/win32/dirent.o
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DENSURE_MSYSTEM_IS_SET -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
# invalidcontinue.obj allows Git's source code to close the same file
# handle twice, or to access the osfhandle of an already-closed stdout
Expand Down Expand Up @@ -735,7 +735,7 @@ ifeq ($(uname_S),MINGW)
endif
CC = gcc
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
-fstack-protector-strong
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
EXTLIBS += -lntdll
EXTRA_PROGRAMS += headless-git$X
INSTALL = /bin/install
Expand Down
31 changes: 30 additions & 1 deletion t/t0060-path-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,8 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX wor
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
echo HERE >expect &&
test_cmp expect actual'
test_cmp expect actual
'

test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works' '
mkdir -p pretend/bin &&
Expand All @@ -610,4 +611,32 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works'
test_cmp expect actual
'

test_expect_success MINGW 'MSYSTEM/PATH is adjusted if necessary' '
mkdir -p "$HOME"/bin pretend/mingw64/bin \
pretend/mingw64/libexec/git-core pretend/usr/bin &&
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/bin/ &&
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/libexec/git-core/ &&
# copy the .dll files, if any (happens when building via CMake)
case "$GIT_EXEC_PATH"/*.dll in
*/"*.dll") ;; # no `.dll` files to be copied
*)
cp "$GIT_EXEC_PATH"/*.dll pretend/mingw64/bin/ &&
cp "$GIT_EXEC_PATH"/*.dll pretend/mingw64/libexec/git-core/
;;
esac &&
echo "env | grep MSYSTEM=" | write_script "$HOME"/bin/git-test-home &&
echo "echo mingw64" | write_script pretend/mingw64/bin/git-test-bin &&
echo "echo usr" | write_script pretend/usr/bin/git-test-bin2 &&
(
MSYSTEM= &&
GIT_EXEC_PATH= &&
pretend/mingw64/libexec/git-core/git.exe test-home >actual &&
pretend/mingw64/libexec/git-core/git.exe test-bin >>actual &&
pretend/mingw64/bin/git.exe test-bin2 >>actual
) &&
test_write_lines MSYSTEM=$MSYSTEM mingw64 usr >expect &&
test_cmp expect actual
'

test_done

0 comments on commit 7912ea2

Please sign in to comment.