Skip to content

Commit f3f3c4d

Browse files
committed
Merge branch 'jk/prompt-fallback-to-tty' into maint
* jk/prompt-fallback-to-tty: prompt: fall back to terminal if askpass fails prompt: clean up strbuf usage
2 parents 35c60a0 + 84d7273 commit f3f3c4d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

prompt.c

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ static char *do_askpass(const char *cmd, const char *prompt)
99
struct child_process pass;
1010
const char *args[3];
1111
static struct strbuf buffer = STRBUF_INIT;
12+
int err = 0;
1213

1314
args[0] = cmd;
1415
args[1] = prompt;
@@ -19,25 +20,30 @@ static char *do_askpass(const char *cmd, const char *prompt)
1920
pass.out = -1;
2021

2122
if (start_command(&pass))
22-
exit(1);
23+
return NULL;
2324

24-
strbuf_reset(&buffer);
2525
if (strbuf_read(&buffer, pass.out, 20) < 0)
26-
die("failed to get '%s' from %s\n", prompt, cmd);
26+
err = 1;
2727

2828
close(pass.out);
2929

3030
if (finish_command(&pass))
31-
exit(1);
31+
err = 1;
32+
33+
if (err) {
34+
error("unable to read askpass response from '%s'", cmd);
35+
strbuf_release(&buffer);
36+
return NULL;
37+
}
3238

3339
strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
3440

35-
return buffer.buf;
41+
return strbuf_detach(&buffer, NULL);
3642
}
3743

3844
char *git_prompt(const char *prompt, int flags)
3945
{
40-
char *r;
46+
char *r = NULL;
4147

4248
if (flags & PROMPT_ASKPASS) {
4349
const char *askpass;
@@ -48,12 +54,15 @@ char *git_prompt(const char *prompt, int flags)
4854
if (!askpass)
4955
askpass = getenv("SSH_ASKPASS");
5056
if (askpass && *askpass)
51-
return do_askpass(askpass, prompt);
57+
r = do_askpass(askpass, prompt);
5258
}
5359

54-
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
5560
if (!r)
56-
die_errno("could not read '%s'", prompt);
61+
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
62+
if (!r) {
63+
/* prompts already contain ": " at the end */
64+
die("could not read %s%s", prompt, strerror(errno));
65+
}
5766
return r;
5867
}
5968

0 commit comments

Comments
 (0)