2011-12-10 18:40:54 +08:00
|
|
|
#include "cache.h"
|
|
|
|
#include "run-command.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "prompt.h"
|
2011-12-10 18:41:08 +08:00
|
|
|
#include "compat/terminal.h"
|
2011-12-10 18:40:54 +08:00
|
|
|
|
2011-12-10 18:40:57 +08:00
|
|
|
static char *do_askpass(const char *cmd, const char *prompt)
|
2011-12-10 18:40:54 +08:00
|
|
|
{
|
|
|
|
struct child_process pass;
|
|
|
|
const char *args[3];
|
|
|
|
static struct strbuf buffer = STRBUF_INIT;
|
|
|
|
|
2011-12-10 18:40:57 +08:00
|
|
|
args[0] = cmd;
|
2011-12-10 18:40:54 +08:00
|
|
|
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)
|
2011-12-10 18:40:57 +08:00
|
|
|
die("failed to get '%s' from %s\n", prompt, cmd);
|
2011-12-10 18:40:54 +08:00
|
|
|
|
|
|
|
close(pass.out);
|
|
|
|
|
|
|
|
if (finish_command(&pass))
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
|
|
|
|
|
|
|
|
return buffer.buf;
|
|
|
|
}
|
2011-12-10 18:40:57 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-12-10 18:41:08 +08:00
|
|
|
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
|
2011-12-10 18:40:57 +08:00
|
|
|
if (!r)
|
|
|
|
die_errno("could not read '%s'", prompt);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *git_getpass(const char *prompt)
|
|
|
|
{
|
|
|
|
return git_prompt(prompt, PROMPT_ASKPASS);
|
|
|
|
}
|