Merge branch 'ps/reflog-list' into HEAD
[alt-git.git] / prompt.c
blob8935fe4dfb9414f101bf603aaf866ac6a1f6e027
1 #include "git-compat-util.h"
2 #include "parse.h"
3 #include "environment.h"
4 #include "run-command.h"
5 #include "strbuf.h"
6 #include "prompt.h"
7 #include "compat/terminal.h"
9 static char *do_askpass(const char *cmd, const char *prompt)
11 struct child_process pass = CHILD_PROCESS_INIT;
12 static struct strbuf buffer = STRBUF_INIT;
13 int err = 0;
15 strvec_push(&pass.args, cmd);
16 strvec_push(&pass.args, prompt);
18 pass.out = -1;
20 if (start_command(&pass))
21 return NULL;
23 strbuf_reset(&buffer);
24 if (strbuf_read(&buffer, pass.out, 20) < 0)
25 err = 1;
27 close(pass.out);
29 if (finish_command(&pass))
30 err = 1;
32 if (err) {
33 error("unable to read askpass response from '%s'", cmd);
34 strbuf_release(&buffer);
35 return NULL;
38 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
40 return buffer.buf;
43 char *git_prompt(const char *prompt, int flags)
45 char *r = NULL;
47 if (flags & PROMPT_ASKPASS) {
48 const char *askpass;
50 askpass = getenv("GIT_ASKPASS");
51 if (!askpass)
52 askpass = askpass_program;
53 if (!askpass)
54 askpass = getenv("SSH_ASKPASS");
55 if (askpass && *askpass)
56 r = do_askpass(askpass, prompt);
59 if (!r) {
60 const char *err;
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";
68 if (!r) {
69 /* prompts already contain ": " at the end */
70 die("could not read %s%s", prompt, err);
73 return r;
76 int git_read_line_interactively(struct strbuf *line)
78 int ret;
80 fflush(stdout);
81 ret = strbuf_getline_lf(line, stdin);
82 if (ret != EOF)
83 strbuf_trim_trailing_newline(line);
85 return ret;