git
/
prompt.c
86 строк · 1.6 Кб
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"8
9static char *do_askpass(const char *cmd, const char *prompt)10{
11struct child_process pass = CHILD_PROCESS_INIT;12static struct strbuf buffer = STRBUF_INIT;13int err = 0;14
15strvec_push(&pass.args, cmd);16strvec_push(&pass.args, prompt);17
18pass.out = -1;19
20if (start_command(&pass))21return NULL;22
23strbuf_reset(&buffer);24if (strbuf_read(&buffer, pass.out, 20) < 0)25err = 1;26
27close(pass.out);28
29if (finish_command(&pass))30err = 1;31
32if (err) {33error("unable to read askpass response from '%s'", cmd);34strbuf_release(&buffer);35return NULL;36}37
38strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));39
40return buffer.buf;41}
42
43char *git_prompt(const char *prompt, int flags)44{
45char *r = NULL;46
47if (flags & PROMPT_ASKPASS) {48const char *askpass;49
50askpass = getenv("GIT_ASKPASS");51if (!askpass)52askpass = askpass_program;53if (!askpass)54askpass = getenv("SSH_ASKPASS");55if (askpass && *askpass)56r = do_askpass(askpass, prompt);57}58
59if (!r) {60const char *err;61
62if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) {63r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);64err = strerror(errno);65} else {66err = "terminal prompts disabled";67}68if (!r) {69/* prompts already contain ": " at the end */70die("could not read %s%s", prompt, err);71}72}73return r;74}
75
76int git_read_line_interactively(struct strbuf *line)77{
78int ret;79
80fflush(stdout);81ret = strbuf_getline_lf(line, stdin);82if (ret != EOF)83strbuf_trim_trailing_newline(line);84
85return ret;86}
87