@matheusbsbs / matheus-bagliano / commits / 9ea416cb51
Fork 0

terminal: accommodate Git for Windows' default terminal

Git for Windows' Git Bash runs in MinTTY by default, which does not have a Win32 Console instance, but uses MSYS2 pseudo terminals instead. This is a problem, as Git for Windows does not want to use the MSYS2 emulation layer for Git itself, and therefore has no direct way to interact with that pseudo terminal. As a workaround, use the `stty` utility (which is included in Git for Windows, and which *is* an MSYS2 program, so it knows how to deal with the pseudo terminal). Note: If Git runs in a regular CMD or PowerShell window, there *is* a regular Win32 Console to work with. This is not a problem for the MSYS2 `stty`: it copes with this scenario just fine. Also note that we introduce support for more bits than would be necessary for a mere `disable_echo()` here, in preparation for the upcoming `enable_non_canonical()` function. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jan 14, 2020 at 18:43 UTC 9ea416cb511e87f830462b08ae74b8a78fcca223
1 file changed +50
compat/terminal.c
+50
@@ -2,6 +2,8 @@
2 #include "compat/terminal.h"
3 #include "sigchain.h"
4 #include "strbuf.h"
5 +#include "run-command.h"
6 +#include "string-list.h"
7
8 #if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
9
@@ -64,11 +66,28 @@ static int disable_echo(void)
66 #define OUTPUT_PATH "CONOUT$"
67 #define FORCE_TEXT "t"
68
69 +static int use_stty = 1;
70 +static struct string_list stty_restore = STRING_LIST_INIT_DUP;
71 static HANDLE hconin = INVALID_HANDLE_VALUE;
72 static DWORD cmode;
73
74 static void restore_term(void)
75 {
76 + if (use_stty) {
77 + int i;
78 + struct child_process cp = CHILD_PROCESS_INIT;
79 +
80 + if (stty_restore.nr == 0)
81 + return;
82 +
83 + argv_array_push(&cp.args, "stty");
84 + for (i = 0; i < stty_restore.nr; i++)
85 + argv_array_push(&cp.args, stty_restore.items[i].string);
86 + run_command(&cp);
87 + string_list_clear(&stty_restore, 0);
88 + return;
89 + }
90 +
91 if (hconin == INVALID_HANDLE_VALUE)
92 return;
93
@@ -79,6 +98,37 @@ static void restore_term(void)
98
99 static int disable_bits(DWORD bits)
100 {
101 + if (use_stty) {
102 + struct child_process cp = CHILD_PROCESS_INIT;
103 +
104 + argv_array_push(&cp.args, "stty");
105 +
106 + if (bits & ENABLE_LINE_INPUT) {
107 + string_list_append(&stty_restore, "icanon");
108 + argv_array_push(&cp.args, "-icanon");
109 + }
110 +
111 + if (bits & ENABLE_ECHO_INPUT) {
112 + string_list_append(&stty_restore, "echo");
113 + argv_array_push(&cp.args, "-echo");
114 + }
115 +
116 + if (bits & ENABLE_PROCESSED_INPUT) {
117 + string_list_append(&stty_restore, "-ignbrk");
118 + string_list_append(&stty_restore, "intr");
119 + string_list_append(&stty_restore, "^c");
120 + argv_array_push(&cp.args, "ignbrk");
121 + argv_array_push(&cp.args, "intr");
122 + argv_array_push(&cp.args, "");
123 + }
124 +
125 + if (run_command(&cp) == 0)
126 + return 0;
127 +
128 + /* `stty` could not be executed; access the Console directly */
129 + use_stty = 0;
130 + }
131 +
132 hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
133 FILE_SHARE_READ, NULL, OPEN_EXISTING,
134 FILE_ATTRIBUTE_NORMAL, NULL);