Merge branch 'cp/merge-continue'
[gitweb.git] / compat / winansi.c
index 3be60ce1c6fc0b5a6d96fdbe1101ed99e28b3585..cb725fb02f6e4f48c4f314c3c6c5e8535aa6eda8 100644 (file)
@@ -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
 
@@ -492,7 +495,7 @@ static inline ioinfo* _pioinfo(int fd)
                        (fd & (IOINFO_ARRAY_ELTS - 1)) * sizeof_ioinfo);
 }
 
-static int init_sizeof_ioinfo()
+static int init_sizeof_ioinfo(void)
 {
        int istty, wastty;
        /* don't init twice */
@@ -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;