Skip to content

Commit 3efc959

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 201e4df + 98bdeb0 commit 3efc959

File tree

3 files changed

+118
-4
lines changed

3 files changed

+118
-4
lines changed

compat/mingw.c

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3014,6 +3014,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
30143014
return -1;
30153015
}
30163016

3017+
#ifdef ENSURE_MSYSTEM_IS_SET
3018+
static size_t append_system_bin_dirs(char *path, size_t size)
3019+
{
3020+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
3021+
return 0;
3022+
#else
3023+
char prefix[32768];
3024+
const char *slash;
3025+
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
3026+
3027+
if (len == 0 || len >= sizeof(prefix) ||
3028+
!(slash = find_last_dir_sep(prefix)))
3029+
return 0;
3030+
/* strip trailing `git.exe` */
3031+
len = slash - prefix;
3032+
3033+
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
3034+
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
3035+
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
3036+
off += xsnprintf(path + off, size - off,
3037+
"%.*s\\mingw64\\bin;", (int)len, prefix);
3038+
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
3039+
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
3040+
off += xsnprintf(path + off, size - off,
3041+
"%.*s\\mingw32\\bin;", (int)len, prefix);
3042+
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
3043+
strip_suffix_mem(prefix, &len, "\\bin") ||
3044+
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
3045+
off += xsnprintf(path + off, size - off,
3046+
"%.*s\\mingw%d\\bin;", (int)len, prefix,
3047+
(int)(sizeof(void *) * 8));
3048+
else
3049+
return 0;
3050+
3051+
off += xsnprintf(path + off, size - off,
3052+
"%.*s\\usr\\bin;", (int)len, prefix);
3053+
return off;
3054+
#endif
3055+
}
3056+
#endif
3057+
3058+
static int is_system32_path(const char *path)
3059+
{
3060+
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
3061+
3062+
if (xutftowcs_path(wpath, path) < 0 ||
3063+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
3064+
_wcsicmp(system32, wpath))
3065+
return 0;
3066+
3067+
return 1;
3068+
}
3069+
30173070
static void setup_windows_environment(void)
30183071
{
30193072
char *tmp = getenv("TMPDIR");
@@ -3054,7 +3107,8 @@ static void setup_windows_environment(void)
30543107
strbuf_addstr(&buf, tmp);
30553108
if ((tmp = getenv("HOMEPATH"))) {
30563109
strbuf_addstr(&buf, tmp);
3057-
if (is_directory(buf.buf))
3110+
if (!is_system32_path(buf.buf) &&
3111+
is_directory(buf.buf))
30583112
setenv("HOME", buf.buf, 1);
30593113
else
30603114
tmp = NULL; /* use $USERPROFILE */
@@ -3065,6 +3119,37 @@ static void setup_windows_environment(void)
30653119
if (!tmp && (tmp = getenv("USERPROFILE")))
30663120
setenv("HOME", tmp, 1);
30673121
}
3122+
3123+
if (!getenv("PLINK_PROTOCOL"))
3124+
setenv("PLINK_PROTOCOL", "ssh", 0);
3125+
3126+
#ifdef ENSURE_MSYSTEM_IS_SET
3127+
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
3128+
const char *home = getenv("HOME"), *path = getenv("PATH");
3129+
char buf[32768];
3130+
size_t off = 0;
3131+
3132+
xsnprintf(buf, sizeof(buf),
3133+
"MINGW%d", (int)(sizeof(void *) * 8));
3134+
setenv("MSYSTEM", buf, 1);
3135+
3136+
if (home)
3137+
off += xsnprintf(buf + off, sizeof(buf) - off,
3138+
"%s\\bin;", home);
3139+
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
3140+
if (path)
3141+
off += xsnprintf(buf + off, sizeof(buf) - off,
3142+
"%s", path);
3143+
else if (off > 0)
3144+
buf[off - 1] = '\0';
3145+
else
3146+
buf[0] = '\0';
3147+
setenv("PATH", buf, 1);
3148+
}
3149+
#endif
3150+
3151+
if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
3152+
setenv("LC_CTYPE", "C.UTF-8", 1);
30683153
}
30693154

30703155
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
@@ -606,7 +606,8 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX wor
606606
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
607607
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
608608
echo HERE >expect &&
609-
test_cmp expect actual'
609+
test_cmp expect actual
610+
'
610611

611612
test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works' '
612613
mkdir -p pretend/bin &&
@@ -617,4 +618,32 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works'
617618
test_cmp expect actual
618619
'
619620

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

0 commit comments

Comments
 (0)