Skip to content

Commit 240882b

Browse files
dschoGit for Windows Build Agent
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 e162b75 + e1190bd commit 240882b

File tree

3 files changed

+131
-4
lines changed

3 files changed

+131
-4
lines changed

compat/mingw.c

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2866,6 +2866,66 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
28662866
return -1;
28672867
}
28682868

2869+
#ifdef ENSURE_MSYSTEM_IS_SET
2870+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
2871+
static size_t append_system_bin_dirs(char *path UNUSED, size_t size UNUSED)
2872+
{
2873+
return 0;
2874+
}
2875+
#else
2876+
static size_t append_system_bin_dirs(char *path, size_t size)
2877+
{
2878+
char prefix[32768];
2879+
const char *slash;
2880+
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
2881+
2882+
if (len == 0 || len >= sizeof(prefix) ||
2883+
!(slash = find_last_dir_sep(prefix)))
2884+
return 0;
2885+
/* strip trailing `git.exe` */
2886+
len = slash - prefix;
2887+
2888+
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2889+
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
2890+
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
2891+
off += xsnprintf(path + off, size - off,
2892+
"%.*s\\mingw64\\bin;", (int)len, prefix);
2893+
else if (strip_suffix_mem(prefix, &len, "\\clangarm64\\libexec\\git-core") ||
2894+
strip_suffix_mem(prefix, &len, "\\clangarm64\\bin"))
2895+
off += xsnprintf(path + off, size - off,
2896+
"%.*s\\clangarm64\\bin;", (int)len, prefix);
2897+
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
2898+
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
2899+
off += xsnprintf(path + off, size - off,
2900+
"%.*s\\mingw32\\bin;", (int)len, prefix);
2901+
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
2902+
strip_suffix_mem(prefix, &len, "\\bin") ||
2903+
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
2904+
off += xsnprintf(path + off, size - off,
2905+
"%.*s\\mingw%d\\bin;", (int)len, prefix,
2906+
(int)(sizeof(void *) * 8));
2907+
else
2908+
return 0;
2909+
2910+
off += xsnprintf(path + off, size - off,
2911+
"%.*s\\usr\\bin;", (int)len, prefix);
2912+
return off;
2913+
}
2914+
#endif
2915+
#endif
2916+
2917+
static int is_system32_path(const char *path)
2918+
{
2919+
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
2920+
2921+
if (xutftowcs_path(wpath, path) < 0 ||
2922+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
2923+
_wcsicmp(system32, wpath))
2924+
return 0;
2925+
2926+
return 1;
2927+
}
2928+
28692929
static void setup_windows_environment(void)
28702930
{
28712931
char *tmp = getenv("TMPDIR");
@@ -2906,7 +2966,8 @@ static void setup_windows_environment(void)
29062966
strbuf_addstr(&buf, tmp);
29072967
if ((tmp = getenv("HOMEPATH"))) {
29082968
strbuf_addstr(&buf, tmp);
2909-
if (is_directory(buf.buf))
2969+
if (!is_system32_path(buf.buf) &&
2970+
is_directory(buf.buf))
29102971
setenv("HOME", buf.buf, 1);
29112972
else
29122973
tmp = NULL; /* use $USERPROFILE */
@@ -2917,6 +2978,41 @@ static void setup_windows_environment(void)
29172978
if (!tmp && (tmp = getenv("USERPROFILE")))
29182979
setenv("HOME", tmp, 1);
29192980
}
2981+
2982+
if (!getenv("PLINK_PROTOCOL"))
2983+
setenv("PLINK_PROTOCOL", "ssh", 0);
2984+
2985+
#ifdef ENSURE_MSYSTEM_IS_SET
2986+
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
2987+
const char *home = getenv("HOME"), *path = getenv("PATH");
2988+
char buf[32768];
2989+
size_t off = 0;
2990+
2991+
#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
2992+
setenv("MSYSTEM", "CLANGARM64", 1);
2993+
#elif defined(__MINGW64__) || defined(_M_AMD64)
2994+
setenv("MSYSTEM", "MINGW64", 1);
2995+
#else
2996+
setenv("MSYSTEM", "MINGW32", 1);
2997+
#endif
2998+
2999+
if (home)
3000+
off += xsnprintf(buf + off, sizeof(buf) - off,
3001+
"%s\\bin;", home);
3002+
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
3003+
if (path)
3004+
off += xsnprintf(buf + off, sizeof(buf) - off,
3005+
"%s", path);
3006+
else if (off > 0)
3007+
buf[off - 1] = '\0';
3008+
else
3009+
buf[0] = '\0';
3010+
setenv("PATH", buf, 1);
3011+
}
3012+
#endif
3013+
3014+
if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
3015+
setenv("LC_CTYPE", "C.UTF-8", 1);
29203016
}
29213017

29223018
static void get_current_user_sid(PSID *sid, HANDLE *linked_token)

config.mak.uname

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ endif
528528
compat/win32/pthread.o compat/win32/syslog.o \
529529
compat/win32/trace2_win32_process_info.o \
530530
compat/win32/dirent.o
531-
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
531+
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\"
532532
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
533533
# invalidcontinue.obj allows Git's source code to close the same file
534534
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -761,7 +761,7 @@ ifeq ($(uname_S),MINGW)
761761
endif
762762
CC = gcc
763763
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
764-
-fstack-protector-strong
764+
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
765765
EXTLIBS += -lntdll
766766
EXTRA_PROGRAMS += headless-git$X
767767
INSTALL = /bin/install

t/t0060-path-utils.sh

Lines changed: 32 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,34 @@ 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+
if test -z "$MINGW_PREFIX"
625+
then
626+
MINGW_PREFIX="/$(echo "${MSYSTEM:-MINGW64}" | tr A-Z a-z)"
627+
fi &&
628+
mkdir -p "$HOME"/bin pretend"$MINGW_PREFIX"/bin \
629+
pretend"$MINGW_PREFIX"/libexec/git-core pretend/usr/bin &&
630+
cp "$GIT_EXEC_PATH"/git.exe pretend"$MINGW_PREFIX"/bin/ &&
631+
cp "$GIT_EXEC_PATH"/git.exe pretend"$MINGW_PREFIX"/libexec/git-core/ &&
632+
# copy the .dll files, if any (happens when building via CMake)
633+
if test -n "$(ls "$GIT_EXEC_PATH"/*.dll 2>/dev/null)"
634+
then
635+
cp "$GIT_EXEC_PATH"/*.dll pretend"$MINGW_PREFIX"/bin/ &&
636+
cp "$GIT_EXEC_PATH"/*.dll pretend"$MINGW_PREFIX"/libexec/git-core/
637+
fi &&
638+
echo "env | grep MSYSTEM=" | write_script "$HOME"/bin/git-test-home &&
639+
echo "echo ${MINGW_PREFIX#/}" | write_script pretend"$MINGW_PREFIX"/bin/git-test-bin &&
640+
echo "echo usr" | write_script pretend/usr/bin/git-test-bin2 &&
641+
642+
(
643+
MSYSTEM= &&
644+
GIT_EXEC_PATH= &&
645+
pretend"$MINGW_PREFIX"/libexec/git-core/git.exe test-home >actual &&
646+
pretend"$MINGW_PREFIX"/libexec/git-core/git.exe test-bin >>actual &&
647+
pretend"$MINGW_PREFIX"/bin/git.exe test-bin2 >>actual
648+
) &&
649+
test_write_lines MSYSTEM=$MSYSTEM "${MINGW_PREFIX#/}" usr >expect &&
650+
test_cmp expect actual
651+
'
652+
622653
test_done

0 commit comments

Comments
 (0)