{"thread":{"id":"49718","subject":"[PATCH 0/1] mingw: fix isatty() after dup2()","startedAt":"2018-10-30T18:44:42Z","lastAt":"2018-10-30T18:44:44Z","messageCount":2,"participants":["Johannes Schindelin via GitGitGadget"],"isPatch":true,"patchVersion":1,"patchTotal":1},"messages":[{"id":"362009","messageId":"pull.61.git.gitgitgadget@gmail.com","threadId":"49718","inReplyTo":null,"subject":"[PATCH 0/1] mingw: fix isatty() after dup2()","fromName":"Johannes Schindelin via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2018-10-30T18:44:39Z","receivedAt":"2018-10-30T18:44:42Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"This patch has been looong in the waiting (well over a year) for being sent\nto the Git mailing list; it is a fixup for a change of isatty() on Windows\nthat had long made it into git.git's master branch.\n\nJohannes Schindelin (1):\n  mingw: fix isatty() after dup2()\n\n compat/mingw.h   |  3 +++\n compat/winansi.c | 12 ++++++++++++\n 2 files changed, 15 insertions(+)\n\n\nbase-commit: 4ede3d42dfb57f9a41ac96a1f216c62eb7566cc2\nPublished-As: https://github.com/gitgitgadget/git/releases/tags/pr-61%2Fdscho%2Fmingw-isatty-and-dup2-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-61/dscho/mingw-isatty-and-dup2-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/61\n-- \ngitgitgadget\n"},{"id":"362010","messageId":"dbea6293c72d7f94fd6114a8de3b0b9a7fa09bba.1540925078.git.gitgitgadget@gmail.com","threadId":"49718","inReplyTo":"pull.61.git.gitgitgadget@gmail.com","subject":"[PATCH 1/1] mingw: fix isatty() after dup2()","fromName":"Johannes Schindelin via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2018-10-30T18:44:40Z","receivedAt":"2018-10-30T18:44:44Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"From: Johannes Schindelin <johannes.schindelin@gmx.de>\n\nSince a9b8a09c3c30 (mingw: replace isatty() hack, 2016-12-22), we handle\nisatty() by special-casing the stdin/stdout/stderr file descriptors,\ncaching the return value. However, we missed the case where dup2()\noverrides the respective file descriptor.\n\nThat poses a problem e.g. where the `show` builtin asks for a pager very\nearly, the `setup_pager()` function sets the pager depending on the\nreturn value of `isatty()` and then redirects stdout. Subsequently,\n`cmd_log_init_finish()` calls `setup_pager()` *again*. What should\nhappen now is that `isatty()` reports that stdout is *not* a TTY and\nconsequently stdout should be left alone.\n\nLet's override dup2() to handle this appropriately.\n\nThis fixes https://github.com/git-for-windows/git/issues/1077\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n compat/mingw.h   |  3 +++\n compat/winansi.c | 12 ++++++++++++\n 2 files changed, 15 insertions(+)\n\ndiff --git a/compat/mingw.h b/compat/mingw.h\nindex f31dcff2b..b04556ce0 100644\n--- a/compat/mingw.h\n+++ b/compat/mingw.h\n@@ -390,6 +390,9 @@ int mingw_raise(int sig);\n int winansi_isatty(int fd);\n #define isatty winansi_isatty\n \n+int winansi_dup2(int oldfd, int newfd);\n+#define dup2 winansi_dup2\n+\n void winansi_init(void);\n HANDLE winansi_get_osfhandle(int fd);\n \ndiff --git a/compat/winansi.c b/compat/winansi.c\nindex a11a0f16d..f4f08237f 100644\n--- a/compat/winansi.c\n+++ b/compat/winansi.c\n@@ -474,6 +474,18 @@ static void die_lasterr(const char *fmt, ...)\n \tva_end(params);\n }\n \n+#undef dup2\n+int winansi_dup2(int oldfd, int newfd)\n+{\n+\tint ret = dup2(oldfd, newfd);\n+\n+\tif (!ret && newfd >= 0 && newfd <= 2)\n+\t\tfd_is_interactive[newfd] = oldfd < 0 || oldfd > 2 ?\n+\t\t\t0 : fd_is_interactive[oldfd];\n+\n+\treturn ret;\n+}\n+\n static HANDLE duplicate_handle(HANDLE hnd)\n {\n \tHANDLE hresult, hproc = GetCurrentProcess();\n-- \ngitgitgadget\n"}]}