]> git.ipfire.org Git - thirdparty/git.git/blob - prompt.c
Merge branch 'ja/perf-use-specified-shell'
[thirdparty/git.git] / prompt.c
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"
7
8 static char *do_askpass(const char *cmd, const char *prompt)
9 {
10 struct child_process pass = CHILD_PROCESS_INIT;
11 static struct strbuf buffer = STRBUF_INIT;
12 int err = 0;
13
14 strvec_push(&pass.args, cmd);
15 strvec_push(&pass.args, prompt);
16
17 pass.out = -1;
18
19 if (start_command(&pass))
20 return NULL;
21
22 strbuf_reset(&buffer);
23 if (strbuf_read(&buffer, pass.out, 20) < 0)
24 err = 1;
25
26 close(pass.out);
27
28 if (finish_command(&pass))
29 err = 1;
30
31 if (err) {
32 error("unable to read askpass response from '%s'", cmd);
33 strbuf_release(&buffer);
34 return NULL;
35 }
36
37 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
38
39 return buffer.buf;
40 }
41
42 char *git_prompt(const char *prompt, int flags)
43 {
44 char *r = NULL;
45
46 if (flags & PROMPT_ASKPASS) {
47 const char *askpass;
48
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);
56 }
57
58 if (!r) {
59 const char *err;
60
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";
66 }
67 if (!r) {
68 /* prompts already contain ": " at the end */
69 die("could not read %s%s", prompt, err);
70 }
71 }
72 return r;
73 }
74
75 int git_read_line_interactively(struct strbuf *line)
76 {
77 int ret;
78
79 fflush(stdout);
80 ret = strbuf_getline_lf(line, stdin);
81 if (ret != EOF)
82 strbuf_trim_trailing_newline(line);
83
84 return ret;
85 }