Skip to content

Commit 20bfaf1

Browse files
dschoGit for Windows Build Agent
authored andcommitted
Merge pull request git-for-windows#2506 from dscho/issue-2283
Allow running Git directly from `C:\Program Files\Git\mingw64\bin\git.exe`
2 parents 99f5436 + fea32af commit 20bfaf1

3 files changed

Lines changed: 120 additions & 4 deletions

File tree

config.mak.uname

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,9 @@ endif
535535
lib/compat/win32/pthread.o lib/compat/win32/syslog.o \
536536
lib/compat/win32/trace2_win32_process_info.o \
537537
lib/compat/win32/dirent.o
538-
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Ilib/compat -Ilib/compat/regex -Ilib/compat/win32 -DSTRIP_EXTENSION=\".exe\"
538+
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY \
539+
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" -DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\"" \
540+
-DNOGDI -DHAVE_STRING_H -Ilib/compat -Ilib/compat/regex -Ilib/compat/win32 -DSTRIP_EXTENSION=\".exe\"
539541
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
540542
# invalidcontinue.obj allows Git's source code to close the same file
541543
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -758,7 +760,9 @@ ifeq ($(uname_S),MINGW)
758760
prefix = $(MINGW_PREFIX)
759761
HOST_CPU = $(patsubst %-w64-mingw32,%,$(MINGW_CHOST))
760762
BASIC_LDFLAGS += -Wl,--pic-executable
761-
COMPAT_CFLAGS += -DDETECT_MSYS_TTY
763+
COMPAT_CFLAGS += -DDETECT_MSYS_TTY \
764+
-DENSURE_MSYSTEM_IS_SET="\"$(MSYSTEM)\"" \
765+
-DMINGW_PREFIX="\"$(patsubst /%,%,$(MINGW_PREFIX))\""
762766
ifeq (MINGW32,$(MSYSTEM))
763767
BASIC_LDFLAGS += -Wl,--large-address-aware
764768
endif

lib/compat/mingw.c

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3268,6 +3268,57 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
32683268
return -1;
32693269
}
32703270

3271+
#ifdef ENSURE_MSYSTEM_IS_SET
3272+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR) || !defined(MINGW_PREFIX)
3273+
static size_t append_system_bin_dirs(char *path UNUSED, size_t size UNUSED)
3274+
{
3275+
return 0;
3276+
}
3277+
#else
3278+
static size_t append_system_bin_dirs(char *path, size_t size)
3279+
{
3280+
char prefix[32768];
3281+
const char *slash;
3282+
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
3283+
3284+
if (len == 0 || len >= sizeof(prefix) ||
3285+
!(slash = find_last_dir_sep(prefix)))
3286+
return 0;
3287+
/* strip trailing `git.exe` */
3288+
len = slash - prefix;
3289+
3290+
/* strip trailing `cmd` or `<mingw-prefix>\bin` or `bin` or `libexec\git-core` */
3291+
if (strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\libexec\\git-core") ||
3292+
strip_suffix_mem(prefix, &len, "\\" MINGW_PREFIX "\\bin"))
3293+
off += xsnprintf(path + off, size - off,
3294+
"%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
3295+
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
3296+
strip_suffix_mem(prefix, &len, "\\bin") ||
3297+
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
3298+
off += xsnprintf(path + off, size - off,
3299+
"%.*s\\" MINGW_PREFIX "\\bin;", (int)len, prefix);
3300+
else
3301+
return 0;
3302+
3303+
off += xsnprintf(path + off, size - off,
3304+
"%.*s\\usr\\bin;", (int)len, prefix);
3305+
return off;
3306+
}
3307+
#endif
3308+
#endif
3309+
3310+
static int is_system32_path(const char *path)
3311+
{
3312+
WCHAR system32[MAX_PATH], wpath[MAX_PATH];
3313+
3314+
if (xutftowcs_path(wpath, path) < 0 ||
3315+
!GetSystemDirectoryW(system32, ARRAY_SIZE(system32)) ||
3316+
_wcsicmp(system32, wpath))
3317+
return 0;
3318+
3319+
return 1;
3320+
}
3321+
32713322
static void setup_windows_environment(void)
32723323
{
32733324
char *tmp = getenv("TMPDIR");
@@ -3319,7 +3370,8 @@ static void setup_windows_environment(void)
33193370
strbuf_addstr(&buf, tmp);
33203371
if ((tmp = getenv("HOMEPATH"))) {
33213372
strbuf_addstr(&buf, tmp);
3322-
if (is_directory(buf.buf))
3373+
if (!is_system32_path(buf.buf) &&
3374+
is_directory(buf.buf))
33233375
setenv("HOME", buf.buf, 1);
33243376
else
33253377
tmp = NULL; /* use $USERPROFILE */
@@ -3331,6 +3383,35 @@ static void setup_windows_environment(void)
33313383
setenv("HOME", tmp, 1);
33323384
}
33333385

3386+
if (!getenv("PLINK_PROTOCOL"))
3387+
setenv("PLINK_PROTOCOL", "ssh", 0);
3388+
3389+
#ifdef ENSURE_MSYSTEM_IS_SET
3390+
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
3391+
const char *home = getenv("HOME"), *path = getenv("PATH");
3392+
char buf[32768];
3393+
size_t off = 0;
3394+
3395+
setenv("MSYSTEM", ENSURE_MSYSTEM_IS_SET, 1);
3396+
3397+
if (home)
3398+
off += xsnprintf(buf + off, sizeof(buf) - off,
3399+
"%s\\bin;", home);
3400+
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
3401+
if (path)
3402+
off += xsnprintf(buf + off, sizeof(buf) - off,
3403+
"%s", path);
3404+
else if (off > 0)
3405+
buf[off - 1] = '\0';
3406+
else
3407+
buf[0] = '\0';
3408+
setenv("PATH", buf, 1);
3409+
}
3410+
#endif
3411+
3412+
if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
3413+
setenv("LC_CTYPE", "C.UTF-8", 1);
3414+
33343415
/*
33353416
* Change 'core.symlinks' default to false, unless native symlinks are
33363417
* enabled in MSys2 (via 'MSYS=winsymlinks:nativestrict'). Thus we can

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)