2011-12-10 11:40:54 +01:00
|
|
|
#include "cache.h"
|
2017-06-14 20:07:36 +02:00
|
|
|
#include "config.h"
|
2011-12-10 11:40:54 +01:00
|
|
|
#include "run-command.h"
|
|
|
|
#include "strbuf.h"
|
|
|
|
#include "prompt.h"
|
2011-12-10 11:41:08 +01:00
|
|
|
#include "compat/terminal.h"
|
2011-12-10 11:40:54 +01:00
|
|
|
|
2011-12-10 11:40:57 +01:00
|
|
|
static char *do_askpass(const char *cmd, const char *prompt)
|
2011-12-10 11:40:54 +01:00
|
|
|
{
|
2014-08-19 21:09:35 +02:00
|
|
|
struct child_process pass = CHILD_PROCESS_INIT;
|
2011-12-10 11:40:54 +01:00
|
|
|
static struct strbuf buffer = STRBUF_INIT;
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
int err = 0;
|
2011-12-10 11:40:54 +01:00
|
|
|
|
2021-11-25 23:52:21 +01:00
|
|
|
strvec_push(&pass.args, cmd);
|
|
|
|
strvec_push(&pass.args, prompt);
|
2011-12-10 11:40:54 +01:00
|
|
|
|
|
|
|
pass.out = -1;
|
|
|
|
|
|
|
|
if (start_command(&pass))
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
return NULL;
|
2011-12-10 11:40:54 +01:00
|
|
|
|
2014-01-02 04:03:30 +01:00
|
|
|
strbuf_reset(&buffer);
|
2011-12-10 11:40:54 +01:00
|
|
|
if (strbuf_read(&buffer, pass.out, 20) < 0)
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
err = 1;
|
2011-12-10 11:40:54 +01:00
|
|
|
|
|
|
|
close(pass.out);
|
|
|
|
|
|
|
|
if (finish_command(&pass))
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
err = 1;
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
error("unable to read askpass response from '%s'", cmd);
|
|
|
|
strbuf_release(&buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-10 11:40:54 +01:00
|
|
|
|
|
|
|
strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
|
|
|
|
|
2014-01-02 04:03:30 +01:00
|
|
|
return buffer.buf;
|
2011-12-10 11:40:54 +01:00
|
|
|
}
|
2011-12-10 11:40:57 +01:00
|
|
|
|
|
|
|
char *git_prompt(const char *prompt, int flags)
|
|
|
|
{
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
char *r = NULL;
|
2011-12-10 11:40:57 +01:00
|
|
|
|
|
|
|
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)
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
r = do_askpass(askpass, prompt);
|
2011-12-10 11:40:57 +01:00
|
|
|
}
|
|
|
|
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
if (!r) {
|
2014-12-04 04:52:29 +01:00
|
|
|
const char *err;
|
|
|
|
|
|
|
|
if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) {
|
|
|
|
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
|
|
|
|
err = strerror(errno);
|
|
|
|
} else {
|
|
|
|
err = "terminal prompts disabled";
|
|
|
|
}
|
|
|
|
if (!r) {
|
|
|
|
/* prompts already contain ": " at the end */
|
|
|
|
die("could not read %s%s", prompt, err);
|
|
|
|
}
|
prompt: fall back to terminal if askpass fails
The current askpass code simply dies if calling an askpass
helper fails. Worse, in some failure modes it doesn't even
print an error (if start_command fails, then it prints its
own error; if reading fails, we print an error; but if the
command exits non-zero, finish_command fails and we print
nothing!).
Let's be more kind to the user by printing an error message
when askpass doesn't work out, and then falling back to the
terminal (which also may fail, of course, but we die already
there with a nice message).
While we're at it, let's clean up the existing error
messages a bit. Now that our prompts are very long and
contain quotes and colons themselves, our error messages are
hard to read.
So the new failure modes look like:
[before, with a terminal]
$ GIT_ASKPASS=false git push
$ echo $?
128
[before, with no terminal, and we must give up]
$ setsid git push
fatal: could not read 'Password for 'https://peff@github.com': ': No such device or address
[after, with a terminal]
$ GIT_ASKPASS=false git push
error: unable to read askpass response from 'false'
Password for 'https://peff@github.com':
[after, with no terminal, and we must give up]
$ GIT_ASKPASS=false setsid git push
error: unable to read askpass response from 'false'
fatal: could not read Password for 'https://peff@github.com': No such device or address
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2012-02-03 23:16:02 +01:00
|
|
|
}
|
2011-12-10 11:40:57 +01:00
|
|
|
return r;
|
|
|
|
}
|
2020-04-10 13:27:50 +02:00
|
|
|
|
|
|
|
int git_read_line_interactively(struct strbuf *line)
|
|
|
|
{
|
2020-04-10 13:27:51 +02:00
|
|
|
int ret;
|
2020-04-10 13:27:50 +02:00
|
|
|
|
2020-04-10 13:27:51 +02:00
|
|
|
fflush(stdout);
|
|
|
|
ret = strbuf_getline_lf(line, stdin);
|
2020-04-10 13:27:50 +02:00
|
|
|
if (ret != EOF)
|
|
|
|
strbuf_trim_trailing_newline(line);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|