🌐 AI搜索 & 代理 主页
blob: 8935fe4dfb9414f101bf603aaf866ac6a1f6e027 [file] [log] [blame]
Elijah Newren98750582023-03-21 06:26:041#include "git-compat-util.h"
Calvin Wanb1bda752023-09-29 21:20:512#include "parse.h"
Elijah Newren32a8f512023-03-21 06:26:033#include "environment.h"
Jeff Kingd3c58b82011-12-10 10:40:544#include "run-command.h"
5#include "strbuf.h"
6#include "prompt.h"
Jeff Kinga5090252011-12-10 10:41:087#include "compat/terminal.h"
Jeff Kingd3c58b82011-12-10 10:40:548
Jeff King1cb01342011-12-10 10:40:579static char *do_askpass(const char *cmd, const char *prompt)
Jeff Kingd3c58b82011-12-10 10:40:5410{
René Scharfed3180272014-08-19 19:09:3511 struct child_process pass = CHILD_PROCESS_INIT;
Jeff Kingd3c58b82011-12-10 10:40:5412 static struct strbuf buffer = STRBUF_INIT;
Jeff King84d72732012-02-03 22:16:0213 int err = 0;
Jeff Kingd3c58b82011-12-10 10:40:5414
Ævar Arnfjörð Bjarmason7f146092021-11-25 22:52:2115 strvec_push(&pass.args, cmd);
16 strvec_push(&pass.args, prompt);
Jeff Kingd3c58b82011-12-10 10:40:5417
Jeff Kingd3c58b82011-12-10 10:40:5418 pass.out = -1;
19
20 if (start_command(&pass))
Jeff King84d72732012-02-03 22:16:0221 return NULL;
Jeff Kingd3c58b82011-12-10 10:40:5422
Jeff Kinge1c1a322014-01-02 03:03:3023 strbuf_reset(&buffer);
Jeff Kingd3c58b82011-12-10 10:40:5424 if (strbuf_read(&buffer, pass.out, 20) < 0)
Jeff King84d72732012-02-03 22:16:0225 err = 1;
Jeff Kingd3c58b82011-12-10 10:40:5426
27 close(pass.out);
28
29 if (finish_command(&pass))
Jeff King84d72732012-02-03 22:16:0230 err = 1;
31
32 if (err) {
33 error("unable to read askpass response from '%s'", cmd);
34 strbuf_release(&buffer);
35 return NULL;
36 }
Jeff Kingd3c58b82011-12-10 10:40:5437
38 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
39
Jeff Kinge1c1a322014-01-02 03:03:3040 return buffer.buf;
Jeff Kingd3c58b82011-12-10 10:40:5441}
Jeff King1cb01342011-12-10 10:40:5742
43char *git_prompt(const char *prompt, int flags)
44{
Jeff King84d72732012-02-03 22:16:0245 char *r = NULL;
Jeff King1cb01342011-12-10 10:40:5746
47 if (flags & PROMPT_ASKPASS) {
48 const char *askpass;
49
50 askpass = getenv("GIT_ASKPASS");
51 if (!askpass)
52 askpass = askpass_program;
53 if (!askpass)
54 askpass = getenv("SSH_ASKPASS");
55 if (askpass && *askpass)
Jeff King84d72732012-02-03 22:16:0256 r = do_askpass(askpass, prompt);
Jeff King1cb01342011-12-10 10:40:5757 }
58
Jeff King84d72732012-02-03 22:16:0259 if (!r) {
Jeff Kinge652c0e2014-12-04 03:52:2960 const char *err;
61
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";
67 }
68 if (!r) {
69 /* prompts already contain ": " at the end */
70 die("could not read %s%s", prompt, err);
71 }
Jeff King84d72732012-02-03 22:16:0272 }
Jeff King1cb01342011-12-10 10:40:5773 return r;
74}
Johannes Schindelin08d383f2020-04-10 11:27:5075
76int git_read_line_interactively(struct strbuf *line)
77{
마누엘1f09aed2020-04-10 11:27:5178 int ret;
Johannes Schindelin08d383f2020-04-10 11:27:5079
마누엘1f09aed2020-04-10 11:27:5180 fflush(stdout);
81 ret = strbuf_getline_lf(line, stdin);
Johannes Schindelin08d383f2020-04-10 11:27:5082 if (ret != EOF)
83 strbuf_trim_trailing_newline(line);
84
85 return ret;
86}