| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "git-compat-util.h" |
| 4 | #include "parse.h" |
| 5 | #include "environment.h" |
| 6 | #include "repository.h" |
| 7 | #include "run-command.h" |
| 8 | #include "strbuf.h" |
| 9 | #include "prompt.h" |
| 10 | #include "compat/terminal.h" |
| 11 | |
| 12 | static char *do_askpass(const char *cmd, const char *prompt) |
| 13 | { |
| 14 | struct child_process pass = CHILD_PROCESS_INIT; |
| 15 | static struct strbuf buffer = STRBUF_INIT; |
| 16 | int err = 0; |
| 17 | |
| 18 | strvec_push(&pass.args, cmd); |
| 19 | strvec_push(&pass.args, prompt); |
| 20 | |
| 21 | pass.out = -1; |
| 22 | |
| 23 | if (start_command(&pass)) |
| 24 | return NULL; |
| 25 | |
| 26 | strbuf_reset(&buffer); |
| 27 | if (strbuf_read(&buffer, pass.out, 20) < 0) |
| 28 | err = 1; |
| 29 | |
| 30 | close(pass.out); |
| 31 | |
| 32 | if (finish_command(&pass)) |
| 33 | err = 1; |
| 34 | |
| 35 | if (err) { |
| 36 | error("unable to read askpass response from '%s'", cmd); |
| 37 | strbuf_release(&buffer); |
| 38 | return NULL; |
| 39 | } |
| 40 | |
| 41 | strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n")); |
| 42 | |
| 43 | return buffer.buf; |
| 44 | } |
| 45 | |
| 46 | char *git_prompt(const char *prompt, int flags) |
| 47 | { |
| 48 | char *r = NULL; |
| 49 | |
| 50 | if (flags & PROMPT_ASKPASS) { |
| 51 | const char *askpass; |
| 52 | |
| 53 | askpass = getenv("GIT_ASKPASS"); |
| 54 | if (!askpass) |
| 55 | askpass = repo_config_values(the_repository)->askpass_program; |
| 56 | if (!askpass) |
| 57 | askpass = getenv("SSH_ASKPASS"); |
| 58 | if (askpass && *askpass) |
| 59 | r = do_askpass(askpass, prompt); |
| 60 | } |
| 61 | |
| 62 | if (!r) { |
| 63 | const char *err; |
| 64 | |
| 65 | if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) { |
| 66 | r = git_terminal_prompt(prompt, flags & PROMPT_ECHO); |
| 67 | err = strerror(errno); |
| 68 | } else { |
| 69 | err = "terminal prompts disabled"; |
| 70 | } |
| 71 | if (!r) { |
| 72 | /* prompts already contain ": " at the end */ |
| 73 | die("could not read %s%s", prompt, err); |
| 74 | } |
| 75 | } |
| 76 | return r; |
| 77 | } |
| 78 | |
| 79 | int git_read_line_interactively(struct strbuf *line) |
| 80 | { |
| 81 | fflush(stdout); |
| 82 | return strbuf_getline(line, stdin); |
| 83 | } |