Merge branch 'jk/forbid-lf-in-git-url' into maint
[git/debian.git] / prompt.c
blob5ded21a017f1089c5a5f63c998c3b11631c0a5d6
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 const char *args[3];
12 static struct strbuf buffer = STRBUF_INIT;
13 int err = 0;
15 args[0] = cmd;
16 args[1] = prompt;
17 args[2] = NULL;
19 pass.argv = args;
20 pass.out = -1;
22 if (start_command(&pass))
23 return NULL;
25 strbuf_reset(&buffer);
26 if (strbuf_read(&buffer, pass.out, 20) < 0)
27 err = 1;
29 close(pass.out);
31 if (finish_command(&pass))
32 err = 1;
34 if (err) {
35 error("unable to read askpass response from '%s'", cmd);
36 strbuf_release(&buffer);
37 return NULL;
40 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
42 return buffer.buf;
45 char *git_prompt(const char *prompt, int flags)
47 char *r = NULL;
49 if (flags & PROMPT_ASKPASS) {
50 const char *askpass;
52 askpass = getenv("GIT_ASKPASS");
53 if (!askpass)
54 askpass = askpass_program;
55 if (!askpass)
56 askpass = getenv("SSH_ASKPASS");
57 if (askpass && *askpass)
58 r = do_askpass(askpass, prompt);
61 if (!r) {
62 const char *err;
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";
70 if (!r) {
71 /* prompts already contain ": " at the end */
72 die("could not read %s%s", prompt, err);
75 return r;
78 int git_read_line_interactively(struct strbuf *line)
80 int ret;
82 fflush(stdout);
83 ret = strbuf_getline_lf(line, stdin);
84 if (ret != EOF)
85 strbuf_trim_trailing_newline(line);
87 return ret;