Skip to content

Commit 9ea416c

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94ac3c3 commit 9ea416c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

compat/terminal.c

+50
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "compat/terminal.h"
33
#include "sigchain.h"
44
#include "strbuf.h"
5+
#include "run-command.h"
6+
#include "string-list.h"
57

68
#if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
79

@@ -64,11 +66,28 @@ static int disable_echo(void)
6466
#define OUTPUT_PATH "CONOUT$"
6567
#define FORCE_TEXT "t"
6668

69+
static int use_stty = 1;
70+
static struct string_list stty_restore = STRING_LIST_INIT_DUP;
6771
static HANDLE hconin = INVALID_HANDLE_VALUE;
6872
static DWORD cmode;
6973

7074
static void restore_term(void)
7175
{
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+
7291
if (hconin == INVALID_HANDLE_VALUE)
7392
return;
7493

@@ -79,6 +98,37 @@ static void restore_term(void)
7998

8099
static int disable_bits(DWORD bits)
81100
{
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+
82132
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
83133
FILE_SHARE_READ, NULL, OPEN_EXISTING,
84134
FILE_ATTRIBUTE_NORMAL, NULL);

0 commit comments

Comments
 (0)