Skip to content

Commit cbb04ea

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 36281b0 + 93c95a1 commit cbb04ea

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,6 +2820,11 @@ ifdef GIT_TEST_INDEX_VERSION
28202820
endif
28212821
ifdef GIT_TEST_PERL_FATAL_WARNINGS
28222822
@echo GIT_TEST_PERL_FATAL_WARNINGS=\''$(subst ','\'',$(subst ','\'',$(GIT_TEST_PERL_FATAL_WARNINGS)))'\' >>$@+
2823+
endif
2824+
ifdef RUNTIME_PREFIX
2825+
@echo RUNTIME_PREFIX=\'true\' >>$@+
2826+
else
2827+
@echo RUNTIME_PREFIX=\'false\' >>$@+
28232828
endif
28242829
@if cmp $@+ $@ >/dev/null 2>&1; then $(RM) $@+; else mv $@+ $@; fi
28252830

compat/mingw.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,47 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
27752775
return -1;
27762776
}
27772777

2778+
#ifdef ENSURE_MSYSTEM_IS_SET
2779+
static size_t append_system_bin_dirs(char *path, size_t size)
2780+
{
2781+
#if !defined(RUNTIME_PREFIX) || !defined(HAVE_WPGMPTR)
2782+
return 0;
2783+
#else
2784+
char prefix[32768];
2785+
const char *slash;
2786+
size_t len = xwcstoutf(prefix, _wpgmptr, sizeof(prefix)), off = 0;
2787+
2788+
if (len == 0 || len >= sizeof(prefix) ||
2789+
!(slash = find_last_dir_sep(prefix)))
2790+
return 0;
2791+
/* strip trailing `git.exe` */
2792+
len = slash - prefix;
2793+
2794+
/* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2795+
if (strip_suffix_mem(prefix, &len, "\\mingw64\\libexec\\git-core") ||
2796+
strip_suffix_mem(prefix, &len, "\\mingw64\\bin"))
2797+
off += xsnprintf(path + off, size - off,
2798+
"%.*s\\mingw64\\bin;", (int)len, prefix);
2799+
else if (strip_suffix_mem(prefix, &len, "\\mingw32\\libexec\\git-core") ||
2800+
strip_suffix_mem(prefix, &len, "\\mingw32\\bin"))
2801+
off += xsnprintf(path + off, size - off,
2802+
"%.*s\\mingw32\\bin;", (int)len, prefix);
2803+
else if (strip_suffix_mem(prefix, &len, "\\cmd") ||
2804+
strip_suffix_mem(prefix, &len, "\\bin") ||
2805+
strip_suffix_mem(prefix, &len, "\\libexec\\git-core"))
2806+
off += xsnprintf(path + off, size - off,
2807+
"%.*s\\mingw%d\\bin;", (int)len, prefix,
2808+
(int)(sizeof(void *) * 8));
2809+
else
2810+
return 0;
2811+
2812+
off += xsnprintf(path + off, size - off,
2813+
"%.*s\\usr\\bin;", (int)len, prefix);
2814+
return off;
2815+
#endif
2816+
}
2817+
#endif
2818+
27782819
static void setup_windows_environment(void)
27792820
{
27802821
char *tmp = getenv("TMPDIR");
@@ -2826,6 +2867,37 @@ static void setup_windows_environment(void)
28262867
if (!tmp && (tmp = getenv("USERPROFILE")))
28272868
setenv("HOME", tmp, 1);
28282869
}
2870+
2871+
if (!getenv("PLINK_PROTOCOL"))
2872+
setenv("PLINK_PROTOCOL", "ssh", 0);
2873+
2874+
#ifdef ENSURE_MSYSTEM_IS_SET
2875+
if (!(tmp = getenv("MSYSTEM")) || !tmp[0]) {
2876+
const char *home = getenv("HOME"), *path = getenv("PATH");
2877+
char buf[32768];
2878+
size_t off = 0;
2879+
2880+
xsnprintf(buf, sizeof(buf),
2881+
"MINGW%d", (int)(sizeof(void *) * 8));
2882+
setenv("MSYSTEM", buf, 1);
2883+
2884+
if (home)
2885+
off += xsnprintf(buf + off, sizeof(buf) - off,
2886+
"%s\\bin;", home);
2887+
off += append_system_bin_dirs(buf + off, sizeof(buf) - off);
2888+
if (path)
2889+
off += xsnprintf(buf + off, sizeof(buf) - off,
2890+
"%s", path);
2891+
else if (off > 0)
2892+
buf[off - 1] = '\0';
2893+
else
2894+
buf[0] = '\0';
2895+
setenv("PATH", buf, 1);
2896+
}
2897+
#endif
2898+
2899+
if (!getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"))
2900+
setenv("LC_CTYPE", "C.UTF-8", 1);
28292901
}
28302902

28312903
int is_valid_win32_path(const char *path, int allow_literal_nul)

config.mak.uname

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ endif
449449
compat/win32/pthread.o compat/win32/syslog.o \
450450
compat/win32/trace2_win32_process_info.o \
451451
compat/win32/dirent.o
452-
COMPAT_CFLAGS = -D__USE_MINGW_ACCESS -DDETECT_MSYS_TTY -DNOGDI -DHAVE_STRING_H -Icompat -Icompat/regex -Icompat/win32 -DSTRIP_EXTENSION=\".exe\"
452+
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\"
453453
BASIC_LDFLAGS = -IGNORE:4217 -IGNORE:4049 -NOLOGO -ENTRY:wmainCRTStartup -SUBSYSTEM:CONSOLE
454454
# invalidcontinue.obj allows Git's source code to close the same file
455455
# handle twice, or to access the osfhandle of an already-closed stdout
@@ -663,7 +663,7 @@ else
663663
endif
664664
CC = gcc
665665
COMPAT_CFLAGS += -D__USE_MINGW_ANSI_STDIO=0 -DDETECT_MSYS_TTY \
666-
-fstack-protector-strong
666+
-DENSURE_MSYSTEM_IS_SET -fstack-protector-strong
667667
EXTLIBS += -lntdll
668668
INSTALL = /bin/install
669669
INTERNAL_QSORT = YesPlease

t/t0060-path-utils.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,42 @@ test_expect_success MINGW 'is_valid_path() on Windows' '
515515
"PRN./abc"
516516
'
517517

518+
test_expect_success MINGW 'MSYSTEM/PATH is adjusted if necessary' '
519+
mkdir -p "$HOME"/bin pretend/mingw64/bin \
520+
pretend/mingw64/libexec/git-core pretend/usr/bin &&
521+
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/bin/ &&
522+
cp "$GIT_EXEC_PATH"/git.exe pretend/mingw64/libexec/git-core/ &&
523+
echo "env | grep MSYSTEM=" | write_script "$HOME"/bin/git-test-home &&
524+
echo "echo mingw64" | write_script pretend/mingw64/bin/git-test-bin &&
525+
echo "echo usr" | write_script pretend/usr/bin/git-test-bin2 &&
526+
527+
(
528+
MSYSTEM= &&
529+
GIT_EXEC_PATH= &&
530+
pretend/mingw64/libexec/git-core/git.exe test-home >actual &&
531+
pretend/mingw64/libexec/git-core/git.exe test-bin >>actual &&
532+
pretend/mingw64/bin/git.exe test-bin2 >>actual
533+
) &&
534+
test_write_lines MSYSTEM=$MSYSTEM mingw64 usr >expect &&
535+
test_cmp expect actual
536+
'
537+
538+
test_lazy_prereq RUNTIME_PREFIX '
539+
test true = "$RUNTIME_PREFIX"
540+
'
541+
542+
test_lazy_prereq CAN_EXEC_IN_PWD '
543+
cp "$GIT_EXEC_PATH"/git$X ./ &&
544+
./git rev-parse
545+
'
546+
547+
test_expect_success RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX works' '
548+
mkdir -p pretend/git pretend/libexec/git-core &&
549+
echo "echo HERE" | write_script pretend/libexec/git-core/git-here &&
550+
cp "$GIT_EXEC_PATH"/git$X pretend/git/ &&
551+
GIT_EXEC_PATH= ./pretend/git/git here >actual &&
552+
echo HERE >expect &&
553+
test_cmp expect actual
554+
'
555+
518556
test_done

0 commit comments

Comments
 (0)