Skip to content

Commit 1cb0134

Browse files
peffgitster
authored andcommitted
refactor git_getpass into generic prompt function
This will allow callers to specify more options (e.g., leaving echo on). The original git_getpass becomes a slim wrapper around the new function. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d3c58b8 commit 1cb0134

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

prompt.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,13 @@
33
#include "strbuf.h"
44
#include "prompt.h"
55

6-
char *git_getpass(const char *prompt)
6+
static char *do_askpass(const char *cmd, const char *prompt)
77
{
8-
const char *askpass;
98
struct child_process pass;
109
const char *args[3];
1110
static struct strbuf buffer = STRBUF_INIT;
1211

13-
askpass = getenv("GIT_ASKPASS");
14-
if (!askpass)
15-
askpass = askpass_program;
16-
if (!askpass)
17-
askpass = getenv("SSH_ASKPASS");
18-
if (!askpass || !(*askpass)) {
19-
char *result = getpass(prompt);
20-
if (!result)
21-
die_errno("Could not read password");
22-
return result;
23-
}
24-
25-
args[0] = askpass;
12+
args[0] = cmd;
2613
args[1] = prompt;
2714
args[2] = NULL;
2815

@@ -35,7 +22,7 @@ char *git_getpass(const char *prompt)
3522

3623
strbuf_reset(&buffer);
3724
if (strbuf_read(&buffer, pass.out, 20) < 0)
38-
die("failed to read password from %s\n", askpass);
25+
die("failed to get '%s' from %s\n", prompt, cmd);
3926

4027
close(pass.out);
4128

@@ -46,3 +33,30 @@ char *git_getpass(const char *prompt)
4633

4734
return buffer.buf;
4835
}
36+
37+
char *git_prompt(const char *prompt, int flags)
38+
{
39+
char *r;
40+
41+
if (flags & PROMPT_ASKPASS) {
42+
const char *askpass;
43+
44+
askpass = getenv("GIT_ASKPASS");
45+
if (!askpass)
46+
askpass = askpass_program;
47+
if (!askpass)
48+
askpass = getenv("SSH_ASKPASS");
49+
if (askpass && *askpass)
50+
return do_askpass(askpass, prompt);
51+
}
52+
53+
r = getpass(prompt);
54+
if (!r)
55+
die_errno("could not read '%s'", prompt);
56+
return r;
57+
}
58+
59+
char *git_getpass(const char *prompt)
60+
{
61+
return git_prompt(prompt, PROMPT_ASKPASS);
62+
}

prompt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#ifndef PROMPT_H
22
#define PROMPT_H
33

4+
#define PROMPT_ASKPASS (1<<0)
5+
6+
char *git_prompt(const char *prompt, int flags);
47
char *git_getpass(const char *prompt);
58

69
#endif /* PROMPT_H */

0 commit comments

Comments
 (0)