Skip to content

Commit

Permalink
Merge branch 'msys2'
Browse files Browse the repository at this point in the history
  • Loading branch information
dscho authored and Git for Windows Build Agent committed Oct 30, 2024
2 parents 629a541 + 603e2fc commit e91eb5e
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions compat/terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,55 @@ static int getchar_with_timeout(int timeout)
return getchar();
}

static char *shell_prompt(const char *prompt, int echo)
{
const char *read_input[] = {
/* Note: call 'bash' explicitly, as 'read -s' is bash-specific */
"bash", "-c", echo ?
"cat >/dev/tty && read -r line </dev/tty && echo \"$line\"" :
"cat >/dev/tty && read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty",
NULL
};
struct child_process child = CHILD_PROCESS_INIT;
static struct strbuf buffer = STRBUF_INIT;
int prompt_len = strlen(prompt), len = -1, code;

strvec_pushv(&child.args, read_input);
child.in = -1;
child.out = -1;
child.silent_exec_failure = 1;

if (start_command(&child))
return NULL;

if (write_in_full(child.in, prompt, prompt_len) != prompt_len) {
error("could not write to prompt script");
close(child.in);
goto ret;
}
close(child.in);

strbuf_reset(&buffer);
len = strbuf_read(&buffer, child.out, 1024);
if (len < 0) {
error("could not read from prompt script");
goto ret;
}

strbuf_strip_suffix(&buffer, "\n");
strbuf_strip_suffix(&buffer, "\r");

ret:
close(child.out);
code = finish_command(&child);
if (code) {
error("failed to execute prompt script (exit code %d)", code);
return NULL;
}

return len < 0 ? NULL : buffer.buf;
}

#endif

#ifndef FORCE_TEXT
Expand All @@ -431,6 +480,15 @@ char *git_terminal_prompt(const char *prompt, int echo)
int r;
FILE *input_fh, *output_fh;

#ifdef GIT_WINDOWS_NATIVE

/* try shell_prompt first, fall back to CONIN/OUT if bash is missing */
char *result = shell_prompt(prompt, echo);
if (result)
return result;

#endif

input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);
if (!input_fh)
return NULL;
Expand Down

0 comments on commit e91eb5e

Please sign in to comment.