Skip to content

Commit 7101c1a

Browse files
dschoGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
Merge branch 'msys2'
2 parents e7c7075 + b3cc540 commit 7101c1a

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

compat/terminal.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,55 @@ 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+
child.silent_exec_failure = 1;
438+
439+
if (start_command(&child))
440+
return NULL;
441+
442+
if (write_in_full(child.in, prompt, prompt_len) != prompt_len) {
443+
error("could not write to prompt script");
444+
close(child.in);
445+
goto ret;
446+
}
447+
close(child.in);
448+
449+
strbuf_reset(&buffer);
450+
len = strbuf_read(&buffer, child.out, 1024);
451+
if (len < 0) {
452+
error("could not read from prompt script");
453+
goto ret;
454+
}
455+
456+
strbuf_strip_suffix(&buffer, "\n");
457+
strbuf_strip_suffix(&buffer, "\r");
458+
459+
ret:
460+
close(child.out);
461+
code = finish_command(&child);
462+
if (code) {
463+
error("failed to execute prompt script (exit code %d)", code);
464+
return NULL;
465+
}
466+
467+
return len < 0 ? NULL : buffer.buf;
468+
}
469+
421470
#endif
422471

423472
#ifndef FORCE_TEXT
@@ -430,6 +479,15 @@ char *git_terminal_prompt(const char *prompt, int echo)
430479
int r;
431480
FILE *input_fh, *output_fh;
432481

482+
#ifdef GIT_WINDOWS_NATIVE
483+
484+
/* try shell_prompt first, fall back to CONIN/OUT if bash is missing */
485+
char *result = shell_prompt(prompt, echo);
486+
if (result)
487+
return result;
488+
489+
#endif
490+
433491
input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
434492
if (!input_fh)
435493
return NULL;

0 commit comments

Comments
 (0)