mingw: fix isatty() after dup2()

Since a9b8a09c3c30 (mingw: replace isatty() hack, 2016-12-22), we handle isatty() by special-casing the stdin/stdout/stderr file descriptors, caching the return value. However, we missed the case where dup2() overrides the respective file descriptor. That poses a problem e.g. where the `show` builtin asks for a pager very early, the `setup_pager()` function sets the pager depending on the return value of `isatty()` and then redirects stdout. Subsequently, `cmd_log_init_finish()` calls `setup_pager()` *again*. What should happen now is that `isatty()` reports that stdout is *not* a TTY and consequently stdout should be left alone. Let's override dup2() to handle this appropriately. This fixes https://github.com/git-for-windows/git/issues/1077 Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 30, 2018 at 11:44 UTC ff8978d533207f472b183223d88786b1053f1989
2 files changed +15
compat/mingw.h
+3
@@ -390,6 +390,9 @@ int mingw_raise(int sig);
390 int winansi_isatty(int fd);
391 #define isatty winansi_isatty
392
393 +int winansi_dup2(int oldfd, int newfd);
394 +#define dup2 winansi_dup2
395 +
396 void winansi_init(void);
397 HANDLE winansi_get_osfhandle(int fd);
398
compat/winansi.c
+12
@@ -474,6 +474,18 @@ static void die_lasterr(const char *fmt, ...)
474 va_end(params);
475 }
476
477 +#undef dup2
478 +int winansi_dup2(int oldfd, int newfd)
479 +{
480 + int ret = dup2(oldfd, newfd);
481 +
482 + if (!ret && newfd >= 0 && newfd <= 2)
483 + fd_is_interactive[newfd] = oldfd < 0 || oldfd > 2 ?
484 + 0 : fd_is_interactive[oldfd];
485 +
486 + return ret;
487 +}
488 +
489 static HANDLE duplicate_handle(HANDLE hnd)
490 {
491 HANDLE hresult, hproc = GetCurrentProcess();