]>
Commit | Line | Data |
---|---|---|
d3c58b83 JK |
1 | #include "cache.h" |
2 | #include "run-command.h" | |
3 | #include "strbuf.h" | |
4 | #include "prompt.h" | |
a5090259 | 5 | #include "compat/terminal.h" |
d3c58b83 | 6 | |
1cb0134f | 7 | static char *do_askpass(const char *cmd, const char *prompt) |
d3c58b83 | 8 | { |
d3c58b83 JK |
9 | struct child_process pass; |
10 | const char *args[3]; | |
11 | static struct strbuf buffer = STRBUF_INIT; | |
84d72733 | 12 | int err = 0; |
d3c58b83 | 13 | |
1cb0134f | 14 | args[0] = cmd; |
d3c58b83 JK |
15 | args[1] = prompt; |
16 | args[2] = NULL; | |
17 | ||
18 | memset(&pass, 0, sizeof(pass)); | |
19 | pass.argv = args; | |
20 | pass.out = -1; | |
21 | ||
22 | if (start_command(&pass)) | |
84d72733 | 23 | return NULL; |
d3c58b83 | 24 | |
d3c58b83 | 25 | if (strbuf_read(&buffer, pass.out, 20) < 0) |
84d72733 | 26 | err = 1; |
d3c58b83 JK |
27 | |
28 | close(pass.out); | |
29 | ||
30 | if (finish_command(&pass)) | |
84d72733 JK |
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 | } | |
d3c58b83 JK |
38 | |
39 | strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n")); | |
40 | ||
31b49d9b | 41 | return strbuf_detach(&buffer, NULL); |
d3c58b83 | 42 | } |
1cb0134f JK |
43 | |
44 | char *git_prompt(const char *prompt, int flags) | |
45 | { | |
84d72733 | 46 | char *r = NULL; |
1cb0134f JK |
47 | |
48 | if (flags & PROMPT_ASKPASS) { | |
49 | const char *askpass; | |
50 | ||
51 | askpass = getenv("GIT_ASKPASS"); | |
52 | if (!askpass) | |
53 | askpass = askpass_program; | |
54 | if (!askpass) | |
55 | askpass = getenv("SSH_ASKPASS"); | |
56 | if (askpass && *askpass) | |
84d72733 | 57 | r = do_askpass(askpass, prompt); |
1cb0134f JK |
58 | } |
59 | ||
1cb0134f | 60 | if (!r) |
84d72733 JK |
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 | } | |
1cb0134f JK |
66 | return r; |
67 | } | |
68 | ||
69 | char *git_getpass(const char *prompt) | |
70 | { | |
71 | return git_prompt(prompt, PROMPT_ASKPASS); | |
72 | } |