diff options
author | Junio C Hamano <gitster@pobox.com> | 2011-12-22 11:27:23 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2011-12-22 11:27:23 -0800 |
commit | ded408fd20e2fedb76850c9fa9bbaa26b888aa7c (patch) | |
tree | f048693eb0afb979809bee23bd03f3f2dd53564a /prompt.c | |
parent | Merge branch 'jk/http-push-to-empty' (diff) | |
parent | contrib: add credential helper for OS X Keychain (diff) | |
download | tgif-ded408fd20e2fedb76850c9fa9bbaa26b888aa7c.tar.xz |
Merge branch 'jk/git-prompt'
* jk/git-prompt:
contrib: add credential helper for OS X Keychain
Makefile: OS X has /dev/tty
Makefile: linux has /dev/tty
credential: use git_prompt instead of git_getpass
prompt: use git_terminal_prompt
add generic terminal prompt function
refactor git_getpass into generic prompt function
move git_getpass to its own source file
imap-send: don't check return value of git_getpass
imap-send: avoid buffer overflow
Conflicts:
Makefile
Diffstat (limited to 'prompt.c')
-rw-r--r-- | prompt.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/prompt.c b/prompt.c new file mode 100644 index 0000000000..72ab9de2f9 --- /dev/null +++ b/prompt.c @@ -0,0 +1,63 @@ +#include "cache.h" +#include "run-command.h" +#include "strbuf.h" +#include "prompt.h" +#include "compat/terminal.h" + +static char *do_askpass(const char *cmd, const char *prompt) +{ + struct child_process pass; + const char *args[3]; + static struct strbuf buffer = STRBUF_INIT; + + args[0] = cmd; + args[1] = prompt; + args[2] = NULL; + + memset(&pass, 0, sizeof(pass)); + pass.argv = args; + pass.out = -1; + + if (start_command(&pass)) + exit(1); + + strbuf_reset(&buffer); + if (strbuf_read(&buffer, pass.out, 20) < 0) + die("failed to get '%s' from %s\n", prompt, cmd); + + close(pass.out); + + if (finish_command(&pass)) + exit(1); + + strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n")); + + return buffer.buf; +} + +char *git_prompt(const char *prompt, int flags) +{ + char *r; + + if (flags & PROMPT_ASKPASS) { + const char *askpass; + + askpass = getenv("GIT_ASKPASS"); + if (!askpass) + askpass = askpass_program; + if (!askpass) + askpass = getenv("SSH_ASKPASS"); + if (askpass && *askpass) + return do_askpass(askpass, prompt); + } + + r = git_terminal_prompt(prompt, flags & PROMPT_ECHO); + if (!r) + die_errno("could not read '%s'", prompt); + return r; +} + +char *git_getpass(const char *prompt) +{ + return git_prompt(prompt, PROMPT_ASKPASS); +} |