Skip to content

Commit 615ac2c

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 f5bf87d + 5caced3 commit 615ac2c

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
@@ -2834,6 +2834,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
28342834
return -1;
28352835
}
28362836

2837+
#ifdef ENSURE_MSYSTEM_IS_SET
2838+
static size_t append_system_bin_dirs(char *path, size_t size)
2839+
{
2840+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
2841+
return 0;
2842+
#else
2843+
char prefix[32768];
2844+
const char *slash;
2845+
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
2846+
2847+
if (len == 0 || len >= sizeof(prefix) ||
2848+
!(slash = find_last_dir_sep(prefix)))
2849+
return 0;
2850+
/* strip trailing `git.exe` */
2851+
len = slash - prefix;
2852+
2853+
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2854+
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
2855+
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
2856+
off += xsnprintf(path + off, size - off,
2857+
"%.*s\\mingw64\\bin;", (int)len, prefix);
2858+
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
2859+
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
2860+
off += xsnprintf(path + off, size - off,
2861+
"%.*s\\mingw32\\bin;", (int)len, prefix);
2862+
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
2863+
strip_suffix_mem(prefix, &len, "\\bin") ||
2864+
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
2865+
off += xsnprintf(path + off, size - off,
2866+
"%.*s\\mingw%d\\bin;", (int)len, prefix,
2867+
(int)(sizeof(void *) * 8));
2868+
else
2869+
return 0;
2870+
2871+
off += xsnprintf(path + off, size - off,
2872+
"%.*s\\usr\\bin;", (int)len, prefix);
2873+
return off;
2874+
#endif
2875+
}
2876+
#endif
2877+
2878+
static int is_system32_path(const char *path)
2879+
{
2880+
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
2881+
2882+
if (xutftowcs_path(wpath, path) < 0 ||
2883+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
2884+
_wcsicmp(system32, wpath))
2885+
return 0;
2886+
2887+
return 1;
2888+
}
2889+
28372890
static void setup_windows_environment(void)
28382891
{
28392892
char *tmp = getenv("TMPDIR");
@@ -2874,7 +2927,8 @@ static void setup_windows_environment(void)
28742927
strbuf_addstr(&buf, tmp);
28752928
if ((tmp = getenv("HOMEPATH"))) {
28762929
strbuf_addstr(&buf, tmp);
2877-
if (is_directory(buf.buf))
2930+
if (!is_system32_path(buf.buf) &&
2931+
is_directory(buf.buf))
28782932
setenv("HOME", buf.buf, 1);
28792933
else
28802934
tmp = NULL; /* use $USERPROFILE */
@@ -2885,6 +2939,37 @@ static void setup_windows_environment(void)
28852939
if (!tmp && (tmp = getenv("USERPROFILE")))
28862940
setenv("HOME", tmp, 1);
28872941
}
2942+
2943+
if (!getenv("PLINK_PROTOCOL"))
2944+
setenv("PLINK_PROTOCOL", "ssh", 0);
2945+
2946+
#ifdef ENSURE_MSYSTEM_IS_SET
2947+
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
2948+
const char *home = getenv("HOME"), *path = getenv("PATH");
2949+
char buf[32768];
2950+
size_t off = 0;
2951+
2952+
xsnprintf(buf, sizeof(buf),
2953+
"MINGW%d", (int)(sizeof(void *) * 8));
2954+
setenv("MSYSTEM", buf, 1);
2955+
2956+
if (home)
2957+
off += xsnprintf(buf + off, sizeof(buf) - off,
2958+
"%s\\bin;", home);
2959+
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
2960+
if (path)
2961+
off += xsnprintf(buf + off, sizeof(buf) - off,
2962+
"%s", path);
2963+
else if (off > 0)
2964+
buf[off - 1] = '\0';
2965+
else
2966+
buf[0] = '\0';
2967+
setenv("PATH", buf, 1);
2968+
}
2969+
#endif
2970+
2971+
if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
2972+
setenv("LC_CTYPE", "C.UTF-8", 1);
28882973
}
28892974

28902975
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
@@ -555,7 +555,8 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX wor
555555
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
556556
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
557557
echo HERE >expect &&
558-
test_cmp expect actual'
558+
test_cmp expect actual
559+
'
559560

560561
test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works' '
561562
mkdir -p pretend/bin &&
@@ -566,4 +567,32 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works'
566567
test_cmp expect actual
567568
'
568569

570+
test_expect_success MINGW 'MSYSTEM/PATH is adjusted if necessary' '
571+
mkdir -p "$HOME"/bin pretend/mingw64/bin \
572+
pretend/mingw64/libexec/git-core pretend/usr/bin &&
573+
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/bin/ &&
574+
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/libexec/git-core/ &&
575+
# copy the .dll files, if any (happens when building via CMake)
576+
case "$GIT_EXEC_PATH"/*.dll in
577+
*/"*.dll") ;; # no `.dll` files to be copied
578+
*)
579+
cp "$GIT_EXEC_PATH"/*.dll pretend/mingw64/bin/ &&
580+
cp "$GIT_EXEC_PATH"/*.dll pretend/mingw64/libexec/git-core/
581+
;;
582+
esac &&
583+
echo "env | grep MSYSTEM=" | write_script "$HOME"/bin/git-test-home &&
584+
echo "echo mingw64" | write_script pretend/mingw64/bin/git-test-bin &&
585+
echo "echo usr" | write_script pretend/usr/bin/git-test-bin2 &&
586+
587+
(
588+
MSYSTEM= &&
589+
GIT_EXEC_PATH= &&
590+
pretend/mingw64/libexec/git-core/git.exe test-home >actual &&
591+
pretend/mingw64/libexec/git-core/git.exe test-bin >>actual &&
592+
pretend/mingw64/bin/git.exe test-bin2 >>actual
593+
) &&
594+
test_write_lines MSYSTEM=$MSYSTEM mingw64 usr >expect &&
595+
test_cmp expect actual
596+
'
597+
569598
test_done

0 commit comments

Comments
 (0)