diff options
| author | Junio C Hamano <gitster@pobox.com> | 2016-12-13 14:09:27 -0800 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2016-12-13 14:09:27 -0800 |
| commit | c04790a93a437458638604af90351557c98e5a5d (patch) | |
| tree | 4c41d32f66fa7b3fa7cced82f8e46d39f3a4ffcf | |
| parent | 8d7a455ed52e2a96debc080dfc011b6bb00db5d2 (diff) | |
| parent | cbb3f3c9b1975c9bdd07f24fc4ef4e504507adaa (diff) | |
| download | git-c04790a93a437458638604af90351557c98e5a5d.tar.xz | |
Merge branch 'js/mingw-isatty'
We often decide if a session is interactive by checking if the
standard I/O streams are connected to a TTY, but isatty() emulation
on Windows incorrectly returned true if it is used on NUL (i.e. an
equivalent to /dev/null). This has been fixed.
* js/mingw-isatty:
mingw: intercept isatty() to handle /dev/null as Git expects it
| -rw-r--r-- | compat/mingw.h | 3 | ||||
| -rw-r--r-- | compat/winansi.c | 33 |
2 files changed, 36 insertions, 0 deletions
diff --git a/compat/mingw.h b/compat/mingw.h index 034fff9479..3350169555 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -384,6 +384,9 @@ int mingw_raise(int sig); * ANSI emulation wrappers */ +int winansi_isatty(int fd); +#define isatty winansi_isatty + void winansi_init(void); HANDLE winansi_get_osfhandle(int fd); diff --git a/compat/winansi.c b/compat/winansi.c index db4a5b0a37..cb725fb02f 100644 --- a/compat/winansi.c +++ b/compat/winansi.c @@ -7,6 +7,9 @@ #include <wingdi.h> #include <winreg.h> +/* In this file, we actually want to use Windows' own isatty(). */ +#undef isatty + /* ANSI codes used by git: m, K @@ -570,6 +573,36 @@ static void detect_msys_tty(int fd) #endif +int winansi_isatty(int fd) +{ + int res = isatty(fd); + + if (res) { + /* + * Make sure that /dev/null is not fooling Git into believing + * that we are connected to a terminal, as "_isatty() returns a + * nonzero value if the descriptor is associated with a + * character device."; for more information, see + * + * https://msdn.microsoft.com/en-us/library/f4s0ddew.aspx + */ + HANDLE handle = (HANDLE)_get_osfhandle(fd); + if (fd == STDIN_FILENO) { + DWORD dummy; + + if (!GetConsoleMode(handle, &dummy)) + res = 0; + } else if (fd == STDOUT_FILENO || fd == STDERR_FILENO) { + CONSOLE_SCREEN_BUFFER_INFO dummy; + + if (!GetConsoleScreenBufferInfo(handle, &dummy)) + res = 0; + } + } + + return res; +} + void winansi_init(void) { int con1, con2; |
