Commit

caa5d9eab2b7bf62bfe6196c4c0354f82ab808d7

mingw: allow `git.exe` to be used instead of the "Git wrapper"

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 https://github.com/git-for-windows/git/issues/2283

Note: This keeps the same, hard-coded MSYSTEM platform support for CMake
as before, and introduces an `msystem' and `mingw-prefix` knob for Meson
(read: neither CMake nor Meson will automatically inherit the setting
from the current build environment).

Helped-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin <johannes.schindelin@gmx.de> · 2026-09-10 06:05 UTC · committed by Junio C Hamano

Browse files
6 files changed, +127 −4
modified compat/mingw.c
@@ -3134,6 +3134,45 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
3134 3134 return -1;
3135 3135 }
3136 3136
3137 + #ifdef ENSURE_MSYSTEM_IS_SET
3138 + #if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR) || !defined(MINGW_PREFIX)
3139 + static size_t append_system_bin_dirs(char *path UNUSED, size_t size UNUSED)
3140 + {
3141 + return 0;
3142 + }
3143 + #else
3144 + static size_t append_system_bin_dirs(char *path, size_t size)
3145 + {
3146 + char prefix[32768];
3147 + const char *slash;
3148 + size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
3149 +
3150 + if (len == 0 || len >= sizeof(prefix) ||
3151 + !(slash = find_last_dir_sep(prefix)))
3152 + return 0;
3153 + /* strip trailing `git.exe` */
3154 + len = slash - prefix;
3155 +
3156 + /* strip trailing `cmd` or `<mingw-prefix>\bin` or `bin` or `libexec\git-core` */
3157 + if (strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\libexec\\git-core") ||
3158 + strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\bin"))
3159 + off += xsnprintf(path + off, size - off,
3160 + "%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
3161 + else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
3162 + strip_suffix_mem(prefix, &len, "\\bin") ||
3163 + strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
3164 + off += xsnprintf(path + off, size - off,
3165 + "%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
3166 + else
3167 + return 0;
3168 +
3169 + off += xsnprintf(path + off, size - off,
3170 + "%.*s\\usr\\bin;", (int)len, prefix);
3171 + return off;
3172 + }
3173 + #endif
3174 + #endif
3175 +
3137 3176 static void setup_windows_environment(void)
3138 3177 {
3139 3178 char *tmp = getenv("TMPDIR");
@@ -3186,6 +3225,32 @@ static void setup_windows_environment(void)
3186 3225 setenv("HOME", tmp, 1);
3187 3226 }
3188 3227
3228 + if (!getenv("PLINK_PROTOCOL"))
3229 + setenv("PLINK_PROTOCOL", "ssh", 0);
3230 +
3231 + #ifdef ENSURE_MSYSTEM_IS_SET
3232 + if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
3233 + const char *home = getenv("HOME"), *path = getenv("PATH");
3234 + char buf[32768];
3235 + size_t off = 0;
3236 +
3237 + setenv("MSYSTEM", ENSURE_MSYSTEM_IS_SET, 1);
3238 +
3239 + if (home)
3240 + off += xsnprintf(buf + off, sizeof(buf) - off,
3241 + "%s\\bin;", home);
3242 + off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
3243 + if (path)
3244 + off += xsnprintf(buf + off, sizeof(buf) - off,
3245 + "%s", path);
3246 + else if (off > 0)
3247 + buf[off - 1] = '\0';
3248 + else
3249 + buf[0] = '\0';
3250 + setenv("PATH", buf, 1);
3251 + }
3252 + #endif
3253 +
3189 3254 if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
3190 3255 setenv("LC_CTYPE", "C.UTF-8", 1);
3191 3256 }
modified config.mak.uname
@@ -535,7 +535,9 @@ endif
535 535 compat/win32/pthread.o compat/win32/syslog.o \
536 536 compat/win32/trace2_win32_process_info.o \
537 537 compat/win32/dirent.o
538 - COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
538 + COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY \
539 + -DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\"" \
540 + -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
539 541 BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
540 542 # invalidcontinue.obj allows Git's source code to close the same file
541 543 # handle twice, or to access the osfhandle of an already-closed stdout
@@ -758,7 +760,9 @@ ifeq ($(uname_S),MINGW)
758 760 prefix = $(MINGW_PREFIX)
759 761 HOST_CPU = $(patsubst %-w64-mingw32,%,$(MINGW_CHOST))
760 762 BASIC_LDFLAGS += -Wl,--pic-executable
761 - COMPAT_CFLAGS += -DDETECT_MSYS_TTY
763 + COMPAT_CFLAGS += -DDETECT_MSYS_TTY \
764 + -DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" \
765 + -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\""
762 766 ifeq (MINGW32,$(MSYSTEM))
763 767 BASIC_LDFLAGS += -Wl,--large-address-aware
764 768 endif
modified contrib/buildsystems/CMakeLists.txt
@@ -256,7 +256,14 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
256 256 _CONSOLE DETECT_MSYS_TTY STRIP_EXTENSION=".exe" NO_SYMLINK_HEAD UNRELIABLE_FSTAT
257 257 NOGDI OBJECT_CREATION_MODE=1 __USE_MINGW_ANSI_STDIO=0
258 258 OVERRIDE_STRDUP MMAP_PREVENTS_DELETE USE_WIN32_MMAP
259 - HAVE_WPGMPTR ENSURE_MSYSTEM_IS_SET HAVE_RTLGENRANDOM)
259 + HAVE_WPGMPTR HAVE_RTLGENRANDOM)
260 + if(CMAKE_GENERATOR_PLATFORM STREQUAL "x64")
261 + add_compile_definitions(ENSURE_MSYSTEM_IS_SET="MINGW64" MINGW_PREFIX="mingw64")
262 + elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "arm64")
263 + add_compile_definitions(ENSURE_MSYSTEM_IS_SET="CLANGARM64" MINGW_PREFIX="clangarm64")
264 + elseif(CMAKE_GENERATOR_PLATFORM STREQUAL "x86")
265 + add_compile_definitions(ENSURE_MSYSTEM_IS_SET="MINGW32" MINGW_PREFIX="mingw32")
266 + endif()
260 267 list(APPEND compat_SOURCES
261 268 compat/mingw.c
262 269 compat/winansi.c
modified meson.build
@@ -1318,7 +1318,6 @@ elif host_machine.system() == 'windows'
1318 1318
1319 1319 libgit_c_args += [
1320 1320 '-DDETECT_MSYS_TTY',
1321 - '-DENSURE_MSYSTEM_IS_SET',
1322 1321 '-DNATIVE_CRLF',
1323 1322 '-DNOGDI',
1324 1323 '-DNO_POSIX_GOODIES',
@@ -1328,6 +1327,20 @@ elif host_machine.system() == 'windows'
1328 1327 '-D__USE_MINGW_ANSI_STDIO=0',
1329 1328 ]
1330 1329
1330 + msystem = get_option('msystem')
1331 + if msystem != ''
1332 + mingw_prefix = get_option('mingw_prefix')
1333 + if mingw_prefix == ''
1334 + mingw_prefix = msystem.to_lower()
1335 + elif mingw_prefix.startswith('/')
1336 + mingw_prefix = mingw_prefix.substring(1)
1337 + endif
1338 + libgit_c_args += [
1339 + '-DENSURE_MSYSTEM_IS_SET="' + msystem + '"',
1340 + '-DMINGW_PREFIX="' + mingw_prefix + '"'
1341 + ]
1342 + endif
1343 +
1331 1344 libgit_dependencies += compiler.find_library('ntdll')
1332 1345 libgit_include_directories += 'compat/win32'
1333 1346 if compiler.get_id() == 'msvc'
modified meson_options.txt
@@ -21,6 +21,10 @@ option('runtime_prefix', type: 'boolean', value: false,
21 21 description: 'Resolve ancillary tooling and support files relative to the location of the runtime binary instead of hard-coding them into the binary.')
22 22 option('sane_tool_path', type: 'array', value: [],
23 23 description: 'An array of paths to pick up tools from in case the normal tools are broken or lacking.')
24 + option('msystem', type: 'string', value: '',
25 + description: 'Fall-back on Windows when MSYSTEM is not set.')
26 + option('mingw_prefix', type: 'string', value: '',
27 + description: 'Fall-back on Windows when MINGW_PREFIX is not set.')
24 28
25 29 # Build information compiled into Git and other parts like documentation.
26 30 option('build_date', type: 'string', value: '',
modified t/t0060-path-utils.sh
@@ -611,4 +611,34 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works'
611 611 test_cmp expect actual
612 612 '
613 613
614 + test_expect_success MINGW,RUNTIME_PREFIX 'MSYSTEM/PATH is adjusted if necessary' '
615 + if test -z "$MINGW_PREFIX"
616 + then
617 + MINGW_PREFIX="/$(echo "${MSYSTEM:-MINGW64}" | tr A-Z a-z)"
618 + fi &&
619 + mkdir -p "$HOME"/bin pretend"$MINGW_PREFIX"/bin \
620 + pretend"$MINGW_PREFIX"/libexec/git-core pretend/usr/bin &&
621 + cp "$GIT_EXEC_PATH"/git.exe pretend"$MINGW_PREFIX"/bin/ &&
622 + cp "$GIT_EXEC_PATH"/git.exe pretend"$MINGW_PREFIX"/libexec/git-core/ &&
623 + # copy the .dll files, if any (happens when building via CMake)
624 + if test -n "$(ls "$GIT_EXEC_PATH"/*.dll 2>/dev/null)"
625 + then
626 + cp "$GIT_EXEC_PATH"/*.dll pretend"$MINGW_PREFIX"/bin/ &&
627 + cp "$GIT_EXEC_PATH"/*.dll pretend"$MINGW_PREFIX"/libexec/git-core/
628 + fi &&
629 + echo "env | grep MSYSTEM=" | write_script "$HOME"/bin/git-test-home &&
630 + echo "echo ${MINGW_PREFIX#/}" | write_script pretend"$MINGW_PREFIX"/bin/git-test-bin &&
631 + echo "echo usr" | write_script pretend/usr/bin/git-test-bin2 &&
632 +
633 + (
634 + MSYSTEM= &&
635 + GIT_EXEC_PATH= &&
636 + pretend"$MINGW_PREFIX"/libexec/git-core/git.exe test-home >actual &&
637 + pretend"$MINGW_PREFIX"/libexec/git-core/git.exe test-bin >>actual &&
638 + pretend"$MINGW_PREFIX"/bin/git.exe test-bin2 >>actual
639 + ) &&
640 + test_write_lines MSYSTEM=$MSYSTEM "${MINGW_PREFIX#/}" usr >expect &&
641 + test_cmp expect actual
642 + '
643 +
614 644 test_done