Skip to content
Permalink
Browse files

Merge branch 'fix-terminal-prompt'

This fixes the issue identified in

	#1498

where Git would not fall back to reading credentials from a Win32
Console when the credentials could not be read from the terminal via the
Bash hack (that is necessary to support running in a MinTTY).

Tested in a Powershell window.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information...
dscho committed Feb 27, 2018
2 parents 58c6fc3 + 80a18d4 commit 9d58696f8c5634201238749abb0ba476057f68da
Showing with 30 additions and 9 deletions.
  1. +30 −9 compat/terminal.c
@@ -77,17 +77,26 @@ static void restore_term(void)
hconin = INVALID_HANDLE_VALUE;
}

static int disable_echo(void)
static int set_echo(int echo)
{
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD new_cmode;

if (hconin == INVALID_HANDLE_VALUE)
hconin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hconin == INVALID_HANDLE_VALUE)
return -1;

GetConsoleMode(hconin, &cmode);
new_cmode = cmode | ENABLE_LINE_INPUT;
if (echo)
new_cmode |= ENABLE_ECHO_INPUT;
else
new_cmode &= ~ENABLE_ECHO_INPUT;

sigchain_push_common(restore_term_on_signal);
if (!SetConsoleMode(hconin, cmode & (~ENABLE_ECHO_INPUT))) {
if (!SetConsoleMode(hconin, new_cmode)) {
CloseHandle(hconin);
hconin = INVALID_HANDLE_VALUE;
return -1;
@@ -96,13 +105,20 @@ static int disable_echo(void)
return 0;
}

static int disable_echo(void)
{
return set_echo(0);
}

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",
"test \"a$SHELL\" != \"a${SHELL%.exe}\" || exit 127; cat >/dev/tty &&"
" read -r line </dev/tty && echo \"$line\"" :
"test \"a$SHELL\" != \"a${SHELL%.exe}\" || exit 127; cat >/dev/tty &&"
" read -r -s line </dev/tty && echo \"$line\" && echo >/dev/tty",
NULL
};
struct child_process child = CHILD_PROCESS_INIT;
@@ -138,7 +154,10 @@ static char *shell_prompt(const char *prompt, int echo)
close(child.out);
code = finish_command(&child);
if (code) {
error("failed to execute prompt script (exit code %d)", code);
if (code != 127)
error("failed to execute prompt script (exit code %d)",
code);

return NULL;
}

@@ -161,9 +180,11 @@ char *git_terminal_prompt(const char *prompt, int echo)

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

if (echo && set_echo(1))
return NULL;
#endif

input_fh = fopen(INPUT_PATH, "r" FORCE_TEXT);

0 comments on commit 9d58696

Please sign in to comment.
You can’t perform that action at this time.