Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 3 | #include "run-command.h" |
| 4 | #include "strbuf.h" |
| 5 | #include "prompt.h" |
Jeff King | a509025 | 2011-12-10 05:41:08 -0500 | [diff] [blame] | 6 | #include "compat/terminal.h" |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 7 | |
Jeff King | 1cb0134 | 2011-12-10 05:40:57 -0500 | [diff] [blame] | 8 | static char *do_askpass(const char *cmd, const char *prompt) |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 9 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 10 | struct child_process pass = CHILD_PROCESS_INIT; |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 11 | const char *args[3]; |
| 12 | static struct strbuf buffer = STRBUF_INIT; |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 13 | int err = 0; |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 14 | |
Jeff King | 1cb0134 | 2011-12-10 05:40:57 -0500 | [diff] [blame] | 15 | args[0] = cmd; |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 16 | args[1] = prompt; |
| 17 | args[2] = NULL; |
| 18 | |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 19 | pass.argv = args; |
| 20 | pass.out = -1; |
| 21 | |
| 22 | if (start_command(&pass)) |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 23 | return NULL; |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 24 | |
Jeff King | e1c1a32 | 2014-01-01 22:03:30 -0500 | [diff] [blame] | 25 | strbuf_reset(&buffer); |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 26 | if (strbuf_read(&buffer, pass.out, 20) < 0) |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 27 | err = 1; |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 28 | |
| 29 | close(pass.out); |
| 30 | |
| 31 | if (finish_command(&pass)) |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 32 | err = 1; |
| 33 | |
| 34 | if (err) { |
| 35 | error("unable to read askpass response from '%s'", cmd); |
| 36 | strbuf_release(&buffer); |
| 37 | return NULL; |
| 38 | } |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 39 | |
| 40 | strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n")); |
| 41 | |
Jeff King | e1c1a32 | 2014-01-01 22:03:30 -0500 | [diff] [blame] | 42 | return buffer.buf; |
Jeff King | d3c58b8 | 2011-12-10 05:40:54 -0500 | [diff] [blame] | 43 | } |
Jeff King | 1cb0134 | 2011-12-10 05:40:57 -0500 | [diff] [blame] | 44 | |
| 45 | char *git_prompt(const char *prompt, int flags) |
| 46 | { |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 47 | char *r = NULL; |
Jeff King | 1cb0134 | 2011-12-10 05:40:57 -0500 | [diff] [blame] | 48 | |
| 49 | if (flags & PROMPT_ASKPASS) { |
| 50 | const char *askpass; |
| 51 | |
| 52 | askpass = getenv("GIT_ASKPASS"); |
| 53 | if (!askpass) |
| 54 | askpass = askpass_program; |
| 55 | if (!askpass) |
| 56 | askpass = getenv("SSH_ASKPASS"); |
| 57 | if (askpass && *askpass) |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 58 | r = do_askpass(askpass, prompt); |
Jeff King | 1cb0134 | 2011-12-10 05:40:57 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 61 | if (!r) { |
Jeff King | e652c0e | 2014-12-03 22:52:29 -0500 | [diff] [blame] | 62 | const char *err; |
| 63 | |
| 64 | if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) { |
| 65 | r = git_terminal_prompt(prompt, flags & PROMPT_ECHO); |
| 66 | err = strerror(errno); |
| 67 | } else { |
| 68 | err = "terminal prompts disabled"; |
| 69 | } |
| 70 | if (!r) { |
| 71 | /* prompts already contain ": " at the end */ |
| 72 | die("could not read %s%s", prompt, err); |
| 73 | } |
Jeff King | 84d7273 | 2012-02-03 17:16:02 -0500 | [diff] [blame] | 74 | } |
Jeff King | 1cb0134 | 2011-12-10 05:40:57 -0500 | [diff] [blame] | 75 | return r; |
| 76 | } |
Johannes Schindelin | 08d383f | 2020-04-10 11:27:50 +0000 | [diff] [blame] | 77 | |
| 78 | int git_read_line_interactively(struct strbuf *line) |
| 79 | { |
마누엘 | 1f09aed | 2020-04-10 11:27:51 +0000 | [diff] [blame] | 80 | int ret; |
Johannes Schindelin | 08d383f | 2020-04-10 11:27:50 +0000 | [diff] [blame] | 81 | |
마누엘 | 1f09aed | 2020-04-10 11:27:51 +0000 | [diff] [blame] | 82 | fflush(stdout); |
| 83 | ret = strbuf_getline_lf(line, stdin); |
Johannes Schindelin | 08d383f | 2020-04-10 11:27:50 +0000 | [diff] [blame] | 84 | if (ret != EOF) |
| 85 | strbuf_trim_trailing_newline(line); |
| 86 | |
| 87 | return ret; |
| 88 | } |