diff --git a/compat/mingw.c b/compat/mingw.c index b8675ae314..964ff3e4bc 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -6,6 +6,8 @@ #include "../run-command.h" #include "../cache.h" +#undef isatty + static const int delay[] = { 0, 1, 10, 20, 40 }; int err_win_to_posix(DWORD winerr) @@ -2161,3 +2163,38 @@ void mingw_startup() /* initialize Unicode console */ winansi_init(); } + +int mingw_isatty(int fd) { + static DWORD id[] = { + STD_INPUT_HANDLE, + STD_OUTPUT_HANDLE, + STD_ERROR_HANDLE + }; + static unsigned initialized; + static int is_tty[ARRAY_SIZE(id)]; + + if (fd < 0 || fd >= ARRAY_SIZE(is_tty)) + return isatty(fd); + + if (isatty(fd)) + return 1; + + if (!initialized) { + const char *env = getenv("MSYS_TTY_HANDLES"); + + if (env) { + int i; + char buffer[64]; + + for (i = 0; i < ARRAY_SIZE(is_tty); i++) { + sprintf(buffer, " %ld ", (unsigned long) + GetStdHandle(id[i])); + is_tty[i] = !!strstr(env, buffer); + } + } + + initialized = 1; + } + + return is_tty[fd]; +} diff --git a/compat/mingw.h b/compat/mingw.h index efc27c4fff..60c6600ec7 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -351,6 +351,9 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler); int mingw_raise(int sig); #define raise mingw_raise +int mingw_isatty(int fd); +#define isatty mingw_isatty + /* * ANSI emulation wrappers */ diff --git a/compat/winansi.c b/compat/winansi.c index aac430a952..71fb0a4f25 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -7,6 +7,8 @@ #include #include +#undef isatty + /* ANSI codes used by git: m, K