Skip to content

Commit 99a2bb3

Browse files
committed
compat/terminal: support echoing on windows
Without /dev/tty support, git_terminal_prompt simply ignores the 'echo'-parameter. On Windows we can do better by clevering up our getpass-implementation a bit so it can conditionally echo. While we're at it, plug a small memory-leak by returning a pointer to a static strbuf instead of detaching it. This is the same thing the /dev/tty-version of git_terminal_prompt does, and the callee doesn't expect to have to free it's memory. Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
1 parent f71be5c commit 99a2bb3

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

compat/mingw.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,21 +1699,6 @@ int link(const char *oldpath, const char *newpath)
16991699
return 0;
17001700
}
17011701

1702-
char *getpass(const char *prompt)
1703-
{
1704-
struct strbuf buf = STRBUF_INIT;
1705-
1706-
fputs(prompt, stderr);
1707-
for (;;) {
1708-
char c = _getch();
1709-
if (c == '\r' || c == '\n')
1710-
break;
1711-
strbuf_addch(&buf, c);
1712-
}
1713-
fputs("\n", stderr);
1714-
return strbuf_detach(&buf, NULL);
1715-
}
1716-
17171702
pid_t waitpid(pid_t pid, int *status, int options)
17181703
{
17191704
HANDLE h = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION,

compat/mingw.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ struct passwd {
5555
char *pw_dir;
5656
};
5757

58-
extern char *getpass(const char *prompt);
59-
6058
typedef void (__cdecl *sig_handler_t)(int);
6159
struct sigaction {
6260
sig_handler_t sa_handler;

compat/terminal.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,26 @@ char *git_terminal_prompt(const char *prompt, int echo)
7171
return buf.buf;
7272
}
7373

74+
#elif defined(WIN32)
75+
76+
char *git_terminal_prompt(const char *prompt, int echo)
77+
{
78+
static struct strbuf buf = STRBUF_INIT;
79+
80+
fputs(prompt, stderr);
81+
strbuf_reset(&buf);
82+
for (;;) {
83+
int c = _getch();
84+
if (c == '\n' || c == '\r')
85+
break;
86+
if (echo)
87+
putc(c, stderr);
88+
strbuf_addch(&buf, c);
89+
}
90+
putc('\n', stderr);
91+
return buf.buf;
92+
}
93+
7494
#else
7595

7696
char *git_terminal_prompt(const char *prompt, int echo)

0 commit comments

Comments
 (0)