Skip to content

Commit ffc425e

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. 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: 마누엘 <[email protected]> Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent d9714e1 commit ffc425e

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

compat/terminal.c

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "git-compat-util.h"
1+
#include "cache.h"
22
#include "compat/terminal.h"
33
#include "sigchain.h"
44
#include "strbuf.h"
@@ -194,6 +194,55 @@ static int mingw_getchar(void)
194194
}
195195
#define getchar mingw_getchar
196196

197+
static char *shell_prompt(const char *prompt, int echo)
198+
{
199+
const char *read_input[] = {
200+
/* Note: call 'bash' explicitly, as 'read -s' is bash-specific */
201+
"bash", "-c", echo ?
202+
"cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" :
203+
"cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty",
204+
NULL
205+
};
206+
struct child_process child = CHILD_PROCESS_INIT;
207+
static struct strbuf buffer = STRBUF_INIT;
208+
int prompt_len = strlen(prompt), len = -1, code;
209+
210+
child.argv = read_input;
211+
child.in = -1;
212+
child.out = -1;
213+
child.silent_exec_failure = 1;
214+
215+
if (start_command(&child))
216+
return NULL;
217+
218+
if (write_in_full(child.in, prompt, prompt_len) != prompt_len) {
219+
error("could not write to prompt script");
220+
close(child.in);
221+
goto ret;
222+
}
223+
close(child.in);
224+
225+
strbuf_reset(&buffer);
226+
len = strbuf_read(&buffer, child.out, 1024);
227+
if (len < 0) {
228+
error("could not read from prompt script");
229+
goto ret;
230+
}
231+
232+
strbuf_strip_suffix(&buffer, "\n");
233+
strbuf_strip_suffix(&buffer, "\r");
234+
235+
ret:
236+
close(child.out);
237+
code = finish_command(&child);
238+
if (code) {
239+
error("failed to execute prompt script (exit code %d)", code);
240+
return NULL;
241+
}
242+
243+
return len < 0 ? NULL : buffer.buf;
244+
}
245+
197246
#endif
198247

199248
#ifndef FORCE_TEXT
@@ -206,6 +255,15 @@ char *git_terminal_prompt(const char *prompt, int echo)
206255
int r;
207256
FILE *input_fh, *output_fh;
208257

258+
#ifdef GIT_WINDOWS_NATIVE
259+
260+
/* try shell_prompt first, fall back to CONIN/OUT if bash is missing */
261+
char *result = shell_prompt(prompt, echo);
262+
if (result)
263+
return result;
264+
265+
#endif
266+
209267
input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
210268
if (!input_fh)
211269
return NULL;

0 commit comments

Comments
 (0)