Git fork
at reftables-rust 82 lines 1.6 kB view raw
1#define USE_THE_REPOSITORY_VARIABLE 2 3#include "git-compat-util.h" 4#include "parse.h" 5#include "environment.h" 6#include "run-command.h" 7#include "strbuf.h" 8#include "prompt.h" 9#include "compat/terminal.h" 10 11static char *do_askpass(const char *cmd, const char *prompt) 12{ 13 struct child_process pass = CHILD_PROCESS_INIT; 14 static struct strbuf buffer = STRBUF_INIT; 15 int err = 0; 16 17 strvec_push(&pass.args, cmd); 18 strvec_push(&pass.args, prompt); 19 20 pass.out = -1; 21 22 if (start_command(&pass)) 23 return NULL; 24 25 strbuf_reset(&buffer); 26 if (strbuf_read(&buffer, pass.out, 20) < 0) 27 err = 1; 28 29 close(pass.out); 30 31 if (finish_command(&pass)) 32 err = 1; 33 34 if (err) { 35 error("unable to read askpass response from '%s'", cmd); 36 strbuf_release(&buffer); 37 return NULL; 38 } 39 40 strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n")); 41 42 return buffer.buf; 43} 44 45char *git_prompt(const char *prompt, int flags) 46{ 47 char *r = NULL; 48 49 if (flags & PROMPT_ASKPASS) { 50 const char *askpass; 51 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); 59 } 60 61 if (!r) { 62 const char *err; 63 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"; 69 } 70 if (!r) { 71 /* prompts already contain ": " at the end */ 72 die("could not read %s%s", prompt, err); 73 } 74 } 75 return r; 76} 77 78int git_read_line_interactively(struct strbuf *line) 79{ 80 fflush(stdout); 81 return strbuf_getline(line, stdin); 82}