Skip to content

Commit ec9dafc

Browse files
kbleesdscho
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. The most prominent user of `git_terminal_prompt()` is certainly `git-remote-https.exe`. It is an interesting use case because both `stdin` and `stdout` are redirected when Git calls said executable, yet it still wants to access the terminal. When running inside a `mintty`, the terminal is not accessible to the `git-remote-https.exe` program, though, because it is a MinGW program and the `mintty` terminal is not backed by a Win32 console. To solve that problem, we simply call out to the shell -- which is an *MSys2* program and can therefore access `/dev/tty`. Helped-by: nalla <nalla@hamal.uberspace.de> Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent b91c2a1 commit ec9dafc

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

compat/terminal.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
#ifndef NO_INTTYPES_H
2+
#include <inttypes.h>
3+
#endif
14
#include "git-compat-util.h"
5+
#include "run-command.h"
26
#include "compat/terminal.h"
37
#include "sigchain.h"
48
#include "strbuf.h"
9+
#include "cache.h"
510

611
#if defined(HAVE_DEV_TTY) || defined(GIT_WINDOWS_NATIVE)
712

@@ -91,6 +96,54 @@ static int disable_echo(void)
9196
return 0;
9297
}
9398

99+
static char *shell_prompt(const char *prompt, int echo)
100+
{
101+
const char *read_input[] = {
102+
/* Note: call 'bash' explicitly, as 'read -s' is bash-specific */
103+
"bash", "-c", echo ?
104+
"cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" :
105+
"cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty",
106+
NULL
107+
};
108+
struct child_process child = CHILD_PROCESS_INIT;
109+
static struct strbuf buffer = STRBUF_INIT;
110+
int prompt_len = strlen(prompt), len = -1, code;
111+
112+
child.argv = read_input;
113+
child.in = -1;
114+
child.out = -1;
115+
116+
if (start_command(&child))
117+
return NULL;
118+
119+
if (write_in_full(child.in, prompt, prompt_len) != prompt_len) {
120+
error("could not write to prompt script");
121+
close(child.in);
122+
goto ret;
123+
}
124+
close(child.in);
125+
126+
strbuf_reset(&buffer);
127+
len = strbuf_read(&buffer, child.out, 1024);
128+
if (len < 0) {
129+
error("could not read from prompt script");
130+
goto ret;
131+
}
132+
133+
strbuf_strip_suffix(&buffer, "\n");
134+
strbuf_strip_suffix(&buffer, "\r");
135+
136+
ret:
137+
close(child.out);
138+
code = finish_command(&child);
139+
if (code) {
140+
error("failed to execute prompt script (exit code %d)", code);
141+
return NULL;
142+
}
143+
144+
return len < 0 ? NULL : buffer.buf;
145+
}
146+
94147
#endif
95148

96149
#ifndef FORCE_TEXT
@@ -102,6 +155,12 @@ char *git_terminal_prompt(const char *prompt, int echo)
102155
static struct strbuf buf = STRBUF_INIT;
103156
int r;
104157
FILE *input_fh, *output_fh;
158+
#ifdef GIT_WINDOWS_NATIVE
159+
const char *term = getenv("TERM");
160+
161+
if (term && starts_with(term, "xterm"))
162+
return shell_prompt(prompt, echo);
163+
#endif
105164

106165
input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
107166
if (!input_fh)

0 commit comments

Comments
 (0)