Skip to content

Commit b16c686

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 2a1472b + 2eeab94 commit b16c686

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

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

28912976
static PSID get_current_user_sid(void)

config.mak.uname

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ endif
496496
compat/win32/pthread.o compat/win32/syslog.o \
497497
compat/win32/trace2_win32_process_info.o \
498498
compat/win32/dirent.o
499-
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
499+
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\"
500500
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
501501
# invalidcontinue.obj allows Git's source code to close the same file
502502
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -711,7 +711,7 @@ ifeq ($(uname_S),MINGW)
711711
endif
712712
CC = gcc
713713
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
714-
-fstack-protector-strong
714+
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
715715
EXTLIBS += -lntdll
716716
INSTALL = /bin/install
717717
INTERNAL_QSORT = YesPlease

t/t0060-path-utils.sh

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX wor
605605
cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
606606
GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
607607
echo HERE >expect &&
608-
test_cmp expect actual'
608+
test_cmp expect actual
609+
'
609610

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

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

0 commit comments

Comments
 (0)