git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] prompt: fall back to terminal if askpass fails

From
Jeff King <peff@peff.net>
Date
Feb 3, 2012, 22:16 UTC
Message-ID
<20120203221602.GB8065@sigill.intra.peff.net>
In-Reply-To
<20120203213654.GD1890@sigill.intra.peff.net>

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>
---
Arguably, the terminal-failure error message shouldn't even bother
reporting errno. I can't imagine it failing for any reason other than
ENODEV, and the message would probably be less confusing as:
  fatal: could not prompt on terminal for Password for...
 prompt.c |   24 +++++++++++++++++-------
 1 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/prompt.c b/prompt.c
index 64f817b..d851807 100644
--- a/prompt.c
+++ b/prompt.c
@@ -9,6 +9,7 @@ static char *do_askpass(const char *cmd, const char *prompt)
 	struct child_process pass;
 	const char *args[3];
 	static struct strbuf buffer = STRBUF_INIT;
+	int err = 0;
 
 	args[0] = cmd;
 	args[1]	= prompt;
@@ -19,15 +20,21 @@ static char *do_askpass(const char *cmd, const char *prompt)
 	pass.out = -1;
 
 	if (start_command(&pass))
-		exit(1);
+		return NULL;
 
 	if (strbuf_read(&buffer, pass.out, 20) < 0)
-		die("failed to get '%s' from %s\n", prompt, cmd);
+		err = 1;
 
 	close(pass.out);
 
 	if (finish_command(&pass))
-		exit(1);
+		err = 1;
+
+	if (err) {
+		error("unable to read askpass response from '%s'", cmd);
+		strbuf_release(&buffer);
+		return NULL;
+	}
 
 	strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
 
@@ -36,7 +43,7 @@ static char *do_askpass(const char *cmd, const char *prompt)
 
 char *git_prompt(const char *prompt, int flags)
 {
-	char *r;
+	char *r = NULL;
 
 	if (flags & PROMPT_ASKPASS) {
 		const char *askpass;
@@ -47,12 +54,15 @@ char *git_prompt(const char *prompt, int flags)
 		if (!askpass)
 			askpass = getenv("SSH_ASKPASS");
 		if (askpass && *askpass)
-			return do_askpass(askpass, prompt);
+			r = do_askpass(askpass, prompt);
 	}
 
-	r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
 	if (!r)
-		die_errno("could not read '%s'", prompt);
+		r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
+	if (!r) {
+		/* prompts already contain ": " at the end */
+		die("could not read %s%s", prompt, strerror(errno));
+	}
 	return r;
 }
 
-- 
1.7.9.rc1.28.gf4be5
Previous: Jeff KingNext: Junio C Hamano
Message 8 of 12 in “Push from an SSH Terminal”
  1. Feanil PatelFeb 3, 2012
  2. Neal GroothuisFeb 3, 2012
  3. Feanil PatelFeb 3, 2012
  4. Neal GroothuisFeb 3, 2012
  5. Jeff KingFeb 3, 2012
  6. Jeff KingFeb 3, 2012
  7. 1/2 prompt: clean up strbuf usageJeff King, Feb 3, 2012
  8. 2/2 prompt: fall back to terminal if askpass failsJeff King, Feb 3, 2012
  9. Junio C HamanoFeb 4, 2012
  10. Jeff KingFeb 4, 2012
  11. Junio C HamanoFeb 4, 2012
  12. Jeff KingFeb 3, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.