Skip to content

Commit 24f9b07

Browse files
nalladscho
authored andcommitted
mingw: Support git_terminal_prompt with more terminals
The `git_terminal_prompt()` function expects the terminal window to be attached to a Win32 Console. However, this is not the case with terminal windows other than `cmd.exe`'s, e.g. with MSys2's own `mintty`. Non-cmd terminals such as `mintty` still have to have a Win32 Console to be proper console programs, but have to hide the Win32 Console to be able to provide more flexibility (such as being resizeable not only vertically but also horizontally). By writing to that Win32 Console, `git_terminal_prompt()` manages only to send the prompt to nowhere and to wait for input from a Console to which the user has no access. This commit introduces a function specifically to support `mintty` -- or other terminals that are compatible with MSys2's `/dev/tty` emulation. We use the `TERM` environment variable as an indicator for that: if the value starts with "xterm" (such as `mintty`'s "xterm_256color"), we prefer to let `xterm_prompt()` handle the user interaction. To handle the case when standard input/output are redirected – as is the case when pushing via HTTPS: `git-remote-https`' standard input and output are pipes from/to the main Git executable – we make use of the `MSYS_TTY_HANDLES` environment variable that was introduced to fix another bug in MSys2-based Git: this environment variable contains the Win32 `HANDLE`s of the standard input, output and error as originally passed from MSys2 to the Git executable, enclosed within space characters, skipping handles that do not refer to the terminal window (e.g. when they were redirected). We will only use those handles when that environment variable lists all three handles because then we can be 100% certain that we are running inside a terminal window, and that we know exactly which Win32 handles to use to communicate with it. Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: nalla <[email protected]>
1 parent 5727569 commit 24f9b07

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

compat/terminal.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#include <inttypes.h>
12
#include "git-compat-util.h"
3+
#include "run-command.h"
24
#include "compat/terminal.h"
35
#include "sigchain.h"
46
#include "strbuf.h"
@@ -91,6 +93,53 @@ static int disable_echo(void)
9193
return 0;
9294
}
9395

96+
static char *xterm_prompt(const char *prompt, int echo)
97+
{
98+
const char *env = getenv("MSYS_TTY_HANDLES");
99+
const char *echo_off[] = { "sh", "-c", "stty -echo </dev/tty", NULL };
100+
const char *echo_on[] = { "sh", "-c", "stty echo </dev/tty", NULL };
101+
static char buffer[1024];
102+
DWORD len, dummy;
103+
size_t tty0, tty1, tty2;
104+
HANDLE in_handle, out_handle;
105+
106+
if (!env || 3 != sscanf(env,
107+
" %" SCNuPTR " %" SCNuPTR " %" SCNuPTR " ",
108+
&tty0, &tty1, &tty2)) {
109+
warning("Cannot read from xterm");
110+
return NULL;
111+
}
112+
113+
in_handle = (HANDLE)tty0;
114+
out_handle = (HANDLE)tty1;
115+
116+
if (!echo && run_command_v_opt(echo_off, 0))
117+
warning("Could not disable echo on xterm");
118+
119+
if (!WriteFile(out_handle, prompt, strlen(prompt), &dummy, NULL)) {
120+
warning("Could not write to xterm");
121+
return NULL;
122+
}
123+
124+
if (!ReadFile(in_handle, buffer, 1024, &len, NULL)) {
125+
warning("Could not read from xterm");
126+
return NULL;
127+
}
128+
129+
if (len && buffer[len - 1] == '\n')
130+
buffer[--len] = '\0';
131+
if (len && buffer[len - 1] == '\r')
132+
buffer[--len] = '\0';
133+
134+
if (!echo) {
135+
if(run_command_v_opt(echo_on, 0))
136+
warning("Could not re-enable echo on xterm");
137+
WriteFile(out_handle, "\n", 1, &dummy, NULL);
138+
}
139+
140+
return len == 0 ? NULL : buffer;
141+
}
142+
94143
#endif
95144

96145
#ifndef FORCE_TEXT
@@ -102,6 +151,12 @@ char *git_terminal_prompt(const char *prompt, int echo)
102151
static struct strbuf buf = STRBUF_INIT;
103152
int r;
104153
FILE *input_fh, *output_fh;
154+
#ifdef GIT_WINDOWS_NATIVE
155+
const char *term = getenv("TERM");
156+
157+
if (term && starts_with(term, "xterm"))
158+
return xterm_prompt(prompt, echo);
159+
#endif
105160

106161
input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
107162
if (!input_fh)

0 commit comments

Comments
 (0)