blob: 5ded21a017f1089c5a5f63c998c3b11631c0a5d6 [file] [log] [blame]
Jeff Kingd3c58b82011-12-10 05:40:54 -05001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Jeff Kingd3c58b82011-12-10 05:40:54 -05003#include "run-command.h"
4#include "strbuf.h"
5#include "prompt.h"
Jeff Kinga5090252011-12-10 05:41:08 -05006#include "compat/terminal.h"
Jeff Kingd3c58b82011-12-10 05:40:54 -05007
Jeff King1cb01342011-12-10 05:40:57 -05008static char *do_askpass(const char *cmd, const char *prompt)
Jeff Kingd3c58b82011-12-10 05:40:54 -05009{
René Scharfed3180272014-08-19 21:09:35 +020010 struct child_process pass = CHILD_PROCESS_INIT;
Jeff Kingd3c58b82011-12-10 05:40:54 -050011 const char *args[3];
12 static struct strbuf buffer = STRBUF_INIT;
Jeff King84d72732012-02-03 17:16:02 -050013 int err = 0;
Jeff Kingd3c58b82011-12-10 05:40:54 -050014
Jeff King1cb01342011-12-10 05:40:57 -050015 args[0] = cmd;
Jeff Kingd3c58b82011-12-10 05:40:54 -050016 args[1] = prompt;
17 args[2] = NULL;
18
Jeff Kingd3c58b82011-12-10 05:40:54 -050019 pass.argv = args;
20 pass.out = -1;
21
22 if (start_command(&pass))
Jeff King84d72732012-02-03 17:16:02 -050023 return NULL;
Jeff Kingd3c58b82011-12-10 05:40:54 -050024
Jeff Kinge1c1a322014-01-01 22:03:30 -050025 strbuf_reset(&buffer);
Jeff Kingd3c58b82011-12-10 05:40:54 -050026 if (strbuf_read(&buffer, pass.out, 20) < 0)
Jeff King84d72732012-02-03 17:16:02 -050027 err = 1;
Jeff Kingd3c58b82011-12-10 05:40:54 -050028
29 close(pass.out);
30
31 if (finish_command(&pass))
Jeff King84d72732012-02-03 17:16:02 -050032 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 Kingd3c58b82011-12-10 05:40:54 -050039
40 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
41
Jeff Kinge1c1a322014-01-01 22:03:30 -050042 return buffer.buf;
Jeff Kingd3c58b82011-12-10 05:40:54 -050043}
Jeff King1cb01342011-12-10 05:40:57 -050044
45char *git_prompt(const char *prompt, int flags)
46{
Jeff King84d72732012-02-03 17:16:02 -050047 char *r = NULL;
Jeff King1cb01342011-12-10 05:40:57 -050048
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 King84d72732012-02-03 17:16:02 -050058 r = do_askpass(askpass, prompt);
Jeff King1cb01342011-12-10 05:40:57 -050059 }
60
Jeff King84d72732012-02-03 17:16:02 -050061 if (!r) {
Jeff Kinge652c0e2014-12-03 22:52:29 -050062 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 King84d72732012-02-03 17:16:02 -050074 }
Jeff King1cb01342011-12-10 05:40:57 -050075 return r;
76}
Johannes Schindelin08d383f2020-04-10 11:27:50 +000077
78int git_read_line_interactively(struct strbuf *line)
79{
마누엘1f09aed2020-04-10 11:27:51 +000080 int ret;
Johannes Schindelin08d383f2020-04-10 11:27:50 +000081
마누엘1f09aed2020-04-10 11:27:51 +000082 fflush(stdout);
83 ret = strbuf_getline_lf(line, stdin);
Johannes Schindelin08d383f2020-04-10 11:27:50 +000084 if (ret != EOF)
85 strbuf_trim_trailing_newline(line);
86
87 return ret;
88}