Skip to content

Commit 2dc4f30

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 <[email protected]> Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 7bd3018 commit 2dc4f30

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

compat/terminal.c

+54
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,54 @@ static int getchar_with_timeout(int timeout)
418418
return getchar();
419419
}
420420

421+
static char *shell_prompt(const char *prompt, int echo)
422+
{
423+
const char *read_input[] = {
424+
/* Note: call 'bash' explicitly, as 'read -s' is bash-specific */
425+
"bash", "-c", echo ?
426+
"cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" :
427+
"cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty",
428+
NULL
429+
};
430+
struct child_process child = CHILD_PROCESS_INIT;
431+
static struct strbuf buffer = STRBUF_INIT;
432+
int prompt_len = strlen(prompt), len = -1, code;
433+
434+
strvec_pushv(&child.args, read_input);
435+
child.in = -1;
436+
child.out = -1;
437+
438+
if (start_command(&child))
439+
return NULL;
440+
441+
if (write_in_full(child.in, prompt, prompt_len) != prompt_len) {
442+
error("could not write to prompt script");
443+
close(child.in);
444+
goto ret;
445+
}
446+
close(child.in);
447+
448+
strbuf_reset(&buffer);
449+
len = strbuf_read(&buffer, child.out, 1024);
450+
if (len < 0) {
451+
error("could not read from prompt script");
452+
goto ret;
453+
}
454+
455+
strbuf_strip_suffix(&buffer, "\n");
456+
strbuf_strip_suffix(&buffer, "\r");
457+
458+
ret:
459+
close(child.out);
460+
code = finish_command(&child);
461+
if (code) {
462+
error("failed to execute prompt script (exit code %d)", code);
463+
return NULL;
464+
}
465+
466+
return len < 0 ? NULL : buffer.buf;
467+
}
468+
421469
#endif
422470

423471
#ifndef FORCE_TEXT
@@ -429,6 +477,12 @@ char *git_terminal_prompt(const char *prompt, int echo)
429477
static struct strbuf buf = STRBUF_INIT;
430478
int r;
431479
FILE *input_fh, *output_fh;
480+
#ifdef GIT_WINDOWS_NATIVE
481+
const char *term = getenv("TERM");
482+
483+
if (term && starts_with(term, "xterm"))
484+
return shell_prompt(prompt, echo);
485+
#endif
432486

433487
input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
434488
if (!input_fh)

0 commit comments

Comments
 (0)