diff-merges: cleanup func_by_opt()
[git/debian.git] / prompt.c
blob50df17279d1d5b2615fb8dd5a13853a2ea87b8eb
1 #include "cache.h"
2 #include "config.h"
3 #include "run-command.h"
4 #include "strbuf.h"
5 #include "prompt.h"
6 #include "compat/terminal.h"
8 static char *do_askpass(const char *cmd, const char *prompt)
10 struct child_process pass = CHILD_PROCESS_INIT;
11 static struct strbuf buffer = STRBUF_INIT;
12 int err = 0;
14 strvec_push(&pass.args, cmd);
15 strvec_push(&pass.args, prompt);
17 pass.out = -1;
19 if (start_command(&pass))
20 return NULL;
22 strbuf_reset(&buffer);
23 if (strbuf_read(&buffer, pass.out, 20) < 0)
24 err = 1;
26 close(pass.out);
28 if (finish_command(&pass))
29 err = 1;
31 if (err) {
32 error("unable to read askpass response from '%s'", cmd);
33 strbuf_release(&buffer);
34 return NULL;
37 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
39 return buffer.buf;
42 char *git_prompt(const char *prompt, int flags)
44 char *r = NULL;
46 if (flags & PROMPT_ASKPASS) {
47 const char *askpass;
49 askpass = getenv("GIT_ASKPASS");
50 if (!askpass)
51 askpass = askpass_program;
52 if (!askpass)
53 askpass = getenv("SSH_ASKPASS");
54 if (askpass && *askpass)
55 r = do_askpass(askpass, prompt);
58 if (!r) {
59 const char *err;
61 if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) {
62 r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
63 err = strerror(errno);
64 } else {
65 err = "terminal prompts disabled";
67 if (!r) {
68 /* prompts already contain ": " at the end */
69 die("could not read %s%s", prompt, err);
72 return r;
75 int git_read_line_interactively(struct strbuf *line)
77 int ret;
79 fflush(stdout);
80 ret = strbuf_getline_lf(line, stdin);
81 if (ret != EOF)
82 strbuf_trim_trailing_newline(line);
84 return ret;