Skip to content

Commit 7ef487d

Browse files
dschomjcheetham
authored andcommitted
Merge pull request #2506 from dscho/issue-2283
Allow running Git directly from `C:\Program Files\Git\mingw64\bin\git.exe`
2 parents dbaf0e4 + 324ab16 commit 7ef487d

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
@@ -2841,6 +2841,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
28412841
return -1;
28422842
}
28432843

2844+
#ifdef ENSURE_MSYSTEM_IS_SET
2845+
static size_t append_system_bin_dirs(char *path, size_t size)
2846+
{
2847+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
2848+
return 0;
2849+
#else
2850+
char prefix[32768];
2851+
const char *slash;
2852+
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
2853+
2854+
if (len == 0 || len >= sizeof(prefix) ||
2855+
!(slash = find_last_dir_sep(prefix)))
2856+
return 0;
2857+
/* strip trailing `git.exe` */
2858+
len = slash - prefix;
2859+
2860+
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2861+
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
2862+
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
2863+
off += xsnprintf(path + off, size - off,
2864+
"%.*s\\mingw64\\bin;", (int)len, prefix);
2865+
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
2866+
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
2867+
off += xsnprintf(path + off, size - off,
2868+
"%.*s\\mingw32\\bin;", (int)len, prefix);
2869+
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
2870+
strip_suffix_mem(prefix, &len, "\\bin") ||
2871+
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
2872+
off += xsnprintf(path + off, size - off,
2873+
"%.*s\\mingw%d\\bin;", (int)len, prefix,
2874+
(int)(sizeof(void *) * 8));
2875+
else
2876+
return 0;
2877+
2878+
off += xsnprintf(path + off, size - off,
2879+
"%.*s\\usr\\bin;", (int)len, prefix);
2880+
return off;
2881+
#endif
2882+
}
2883+
#endif
2884+
2885+
static int is_system32_path(const char *path)
2886+
{
2887+
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
2888+
2889+
if (xutftowcs_path(wpath, path) < 0 ||
2890+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
2891+
_wcsicmp(system32, wpath))
2892+
return 0;
2893+
2894+
return 1;
2895+
}
2896+
28442897
static void setup_windows_environment(void)
28452898
{
28462899
char *tmp = getenv("TMPDIR");
@@ -2881,7 +2934,8 @@ static void setup_windows_environment(void)
28812934
strbuf_addstr(&buf, tmp);
28822935
if ((tmp = getenv("HOMEPATH"))) {
28832936
strbuf_addstr(&buf, tmp);
2884-
if (is_directory(buf.buf))
2937+
if (!is_system32_path(buf.buf) &&
2938+
is_directory(buf.buf))
28852939
setenv("HOME", buf.buf, 1);
28862940
else
28872941
tmp = NULL; /* use $USERPROFILE */
@@ -2892,6 +2946,37 @@ static void setup_windows_environment(void)
28922946
if (!tmp && (tmp = getenv("USERPROFILE")))
28932947
setenv("HOME", tmp, 1);
28942948
}
2949+
2950+
if (!getenv("PLINK_PROTOCOL"))
2951+
setenv("PLINK_PROTOCOL", "ssh", 0);
2952+
2953+
#ifdef ENSURE_MSYSTEM_IS_SET
2954+
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
2955+
const char *home = getenv("HOME"), *path = getenv("PATH");
2956+
char buf[32768];
2957+
size_t off = 0;
2958+
2959+
xsnprintf(buf, sizeof(buf),
2960+
"MINGW%d", (int)(sizeof(void *) * 8));
2961+
setenv("MSYSTEM", buf, 1);
2962+
2963+
if (home)
2964+
off += xsnprintf(buf + off, sizeof(buf) - off,
2965+
"%s\\bin;", home);
2966+
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
2967+
if (path)
2968+
off += xsnprintf(buf + off, sizeof(buf) - off,
2969+
"%s", path);
2970+
else if (off > 0)
2971+
buf[off - 1] = '\0';
2972+
else
2973+
buf[0] = '\0';
2974+
setenv("PATH", buf, 1);
2975+
}
2976+
#endif
2977+
2978+
if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
2979+
setenv("LC_CTYPE", "C.UTF-8", 1);
28952980
}
28962981

28972982
static PSID get_current_user_sid(void)

config.mak.uname

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ endif
488488
compat/win32/pthread.o compat/win32/syslog.o \
489489
compat/win32/trace2_win32_process_info.o \
490490
compat/win32/dirent.o
491-
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
491+
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\"
492492
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
493493
# invalidcontinue.obj allows Git's source code to close the same file
494494
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -703,7 +703,7 @@ ifeq ($(uname_S),MINGW)
703703
endif
704704
CC = gcc
705705
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
706-
-fstack-protector-strong
706+
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
707707
EXTLIBS += -lntdll
708708
INSTALL = /bin/install
709709
INTERNAL_QSORT = YesPlease

t/t0060-path-utils.sh

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,8 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX wor
607607
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
608608
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
609609
echo HERE >expect &&
610-
test_cmp expect actual'
610+
test_cmp expect actual
611+
'
611612

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

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

0 commit comments

Comments
 (0)