Skip to content

Commit 20af844

Browse files
committed
Merge pull request #2506 from dscho/issue-2283
Allow running Git directly from `C:\Program Files\Git\mingw64\bin\git.exe`
2 parents 6a89524 + 5cf32c7 commit 20af844

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

compat/mingw.c

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,62 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
28802880
return -1;
28812881
}
28822882

2883+
#ifdef ENSURE_MSYSTEM_IS_SET
2884+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
2885+
static size_t append_system_bin_dirs(char *path UNUSED, size_t size UNUSED)
2886+
{
2887+
return 0;
2888+
}
2889+
#else
2890+
static size_t append_system_bin_dirs(char *path, size_t size)
2891+
{
2892+
char prefix[32768];
2893+
const char *slash;
2894+
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
2895+
2896+
if (len == 0 || len >= sizeof(prefix) ||
2897+
!(slash = find_last_dir_sep(prefix)))
2898+
return 0;
2899+
/* strip trailing `git.exe` */
2900+
len = slash - prefix;
2901+
2902+
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2903+
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
2904+
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
2905+
off += xsnprintf(path + off, size - off,
2906+
"%.*s\\mingw64\\bin;", (int)len, prefix);
2907+
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
2908+
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
2909+
off += xsnprintf(path + off, size - off,
2910+
"%.*s\\mingw32\\bin;", (int)len, prefix);
2911+
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
2912+
strip_suffix_mem(prefix, &len, "\\bin") ||
2913+
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
2914+
off += xsnprintf(path + off, size - off,
2915+
"%.*s\\mingw%d\\bin;", (int)len, prefix,
2916+
(int)(sizeof(void *) * 8));
2917+
else
2918+
return 0;
2919+
2920+
off += xsnprintf(path + off, size - off,
2921+
"%.*s\\usr\\bin;", (int)len, prefix);
2922+
return off;
2923+
}
2924+
#endif
2925+
#endif
2926+
2927+
static int is_system32_path(const char *path)
2928+
{
2929+
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
2930+
2931+
if (xutftowcs_path(wpath, path) < 0 ||
2932+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
2933+
_wcsicmp(system32, wpath))
2934+
return 0;
2935+
2936+
return 1;
2937+
}
2938+
28832939
static void setup_windows_environment(void)
28842940
{
28852941
char *tmp = getenv("TMPDIR");
@@ -2920,7 +2976,8 @@ static void setup_windows_environment(void)
29202976
strbuf_addstr(&buf, tmp);
29212977
if ((tmp = getenv("HOMEPATH"))) {
29222978
strbuf_addstr(&buf, tmp);
2923-
if (is_directory(buf.buf))
2979+
if (!is_system32_path(buf.buf) &&
2980+
is_directory(buf.buf))
29242981
setenv("HOME", buf.buf, 1);
29252982
else
29262983
tmp = NULL; /* use $USERPROFILE */
@@ -2931,6 +2988,37 @@ static void setup_windows_environment(void)
29312988
if (!tmp && (tmp = getenv("USERPROFILE")))
29322989
setenv("HOME", tmp, 1);
29332990
}
2991+
2992+
if (!getenv("PLINK_PROTOCOL"))
2993+
setenv("PLINK_PROTOCOL", "ssh", 0);
2994+
2995+
#ifdef ENSURE_MSYSTEM_IS_SET
2996+
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
2997+
const char *home = getenv("HOME"), *path = getenv("PATH");
2998+
char buf[32768];
2999+
size_t off = 0;
3000+
3001+
xsnprintf(buf, sizeof(buf),
3002+
"MINGW%d", (int)(sizeof(void *) * 8));
3003+
setenv("MSYSTEM", buf, 1);
3004+
3005+
if (home)
3006+
off += xsnprintf(buf + off, sizeof(buf) - off,
3007+
"%s\\bin;", home);
3008+
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
3009+
if (path)
3010+
off += xsnprintf(buf + off, sizeof(buf) - off,
3011+
"%s", path);
3012+
else if (off > 0)
3013+
buf[off - 1] = '\0';
3014+
else
3015+
buf[0] = '\0';
3016+
setenv("PATH", buf, 1);
3017+
}
3018+
#endif
3019+
3020+
if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
3021+
setenv("LC_CTYPE", "C.UTF-8", 1);
29343022
}
29353023

29363024
static PSID get_current_user_sid(void)

config.mak.uname

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ endif
501501
compat/win32/pthread.o compat/win32/syslog.o \
502502
compat/win32/trace2_win32_process_info.o \
503503
compat/win32/dirent.o
504-
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
504+
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\"
505505
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
506506
# invalidcontinue.obj allows Git's source code to close the same file
507507
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -730,7 +730,7 @@ ifeq ($(uname_S),MINGW)
730730
endif
731731
CC = gcc
732732
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
733-
-fstack-protector-strong
733+
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
734734
EXTLIBS += -lntdll
735735
EXTRA_PROGRAMS += headless-git$X
736736
INSTALL = /bin/install

t/t0060-path-utils.sh

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX wor
610610
echo "echo HERE" | write_script pretend/libexec/git-core/git-here &&
611611
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
612612
echo HERE >expect &&
613-
test_cmp expect actual'
613+
test_cmp expect actual
614+
'
614615

615616
test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works' '
616617
git config yes.path "%(prefix)/yes" &&
@@ -619,4 +620,32 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works'
619620
test_cmp expect actual
620621
'
621622

623+
test_expect_success MINGW,RUNTIME_PREFIX 'MSYSTEM/PATH is adjusted if necessary' '
624+
mkdir -p "$HOME"/bin pretend/mingw64/bin \
625+
pretend/mingw64/libexec/git-core pretend/usr/bin &&
626+
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/bin/ &&
627+
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/libexec/git-core/ &&
628+
# copy the .dll files, if any (happens when building via CMake)
629+
case "$GIT_EXEC_PATH"/*.dll in
630+
*/"*.dll") ;; # no `.dll` files to be copied
631+
*)
632+
cp "$GIT_EXEC_PATH"/*.dll pretend/mingw64/bin/ &&
633+
cp "$GIT_EXEC_PATH"/*.dll pretend/mingw64/libexec/git-core/
634+
;;
635+
esac &&
636+
echo "env | grep MSYSTEM=" | write_script "$HOME"/bin/git-test-home &&
637+
echo "echo mingw64" | write_script pretend/mingw64/bin/git-test-bin &&
638+
echo "echo usr" | write_script pretend/usr/bin/git-test-bin2 &&
639+
640+
(
641+
MSYSTEM= &&
642+
GIT_EXEC_PATH= &&
643+
pretend/mingw64/libexec/git-core/git.exe test-home >actual &&
644+
pretend/mingw64/libexec/git-core/git.exe test-bin >>actual &&
645+
pretend/mingw64/bin/git.exe test-bin2 >>actual
646+
) &&
647+
test_write_lines MSYSTEM=$MSYSTEM mingw64 usr >expect &&
648+
test_cmp expect actual
649+
'
650+
622651
test_done

0 commit comments

Comments
 (0)