prompt.c 1.1 KB
Newer Older
1 2 3 4 5
#include "cache.h"
#include "run-command.h"
#include "strbuf.h"
#include "prompt.h"

6
static char *do_askpass(const char *cmd, const char *prompt)
7 8 9 10 11
{
	struct child_process pass;
	const char *args[3];
	static struct strbuf buffer = STRBUF_INIT;

12
	args[0] = cmd;
13 14 15 16 17 18 19 20 21 22 23 24
	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)
25
		die("failed to get '%s' from %s\n", prompt, cmd);
26 27 28 29 30 31 32 33 34 35

	close(pass.out);

	if (finish_command(&pass))
		exit(1);

	strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));

	return buffer.buf;
}
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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 = getpass(prompt);
	if (!r)
		die_errno("could not read '%s'", prompt);
	return r;
}

char *git_getpass(const char *prompt)
{
	return git_prompt(prompt, PROMPT_ASKPASS);
}