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