git-core: Support retrieving passwords with GIT_ASKPASS
git tries to read a password from the terminal in imap-send and when talking to a http server that requires authentication. When a GUI is driving git, however, the end user is not paying attention to the terminal (there may not even be a terminal). GUI would appear to hang forever. Fix this problem by allowing a password-retrieving command to be specified in GIT_ASKPASS Signed-off-by: Frank Li <lznuaa@gmail.com> Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Frank Li committed
Mar 4, 2010 at 20:59 UTC
f206063b4b201ff48c87b886cbe21a068fcf6d49
4 files changed
+41
-3
cache.h
+1
@@ -877,6 +877,7 @@ struct ref {
877
extern struct ref *find_ref_by_name(const struct ref *list, const char *name);
878
879
#define CONNECT_VERBOSE (1u << 0)
880
+extern char *git_getpass(const char *prompt);
881
extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
882
extern int finish_connect(struct child_process *conn);
883
extern int path_match(const char *path, int nr, char **match);
connect.c
+37
@@ -647,3 +647,40 @@ int finish_connect(struct child_process *conn)
647
free(conn);
648
return code;
649
}
650
+
651
+char *git_getpass(const char *prompt)
652
+{
653
+ char *askpass;
654
+ struct child_process pass;
655
+ const char *args[3];
656
+ static struct strbuf buffer = STRBUF_INIT;
657
+
658
+ askpass = getenv("GIT_ASKPASS");
659
+
660
+ if (!askpass || !(*askpass))
661
+ return getpass(prompt);
662
+
663
+ args[0] = askpass;
664
+ args[1] = prompt;
665
+ args[2] = NULL;
666
+
667
+ memset(&pass, 0, sizeof(pass));
668
+ pass.argv = args;
669
+ pass.out = -1;
670
+
671
+ if (start_command(&pass))
672
+ exit(1);
673
+
674
+ strbuf_reset(&buffer);
675
+ if (strbuf_read(&buffer, pass.out, 20) < 0)
676
+ die("failed to read password from %s\n", askpass);
677
+
678
+ close(pass.out);
679
+
680
+ if (finish_command(&pass))
681
+ exit(1);
682
+
683
+ strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
684
+
685
+ return buffer.buf;
686
+}
http.c
+2
-2
@@ -204,7 +204,7 @@ static void init_curl_http_auth(CURL *result)
204
if (user_name) {
205
struct strbuf up = STRBUF_INIT;
206
if (!user_pass)
207
- user_pass = xstrdup(getpass("Password: "));
207
+ user_pass = xstrdup(git_getpass("Password: "));
208
strbuf_addf(&up, "%s:%s", user_name, user_pass);
209
curl_easy_setopt(result, CURLOPT_USERPWD,
210
strbuf_detach(&up, NULL));
@@ -219,7 +219,7 @@ static int has_cert_password(void)
219
return 0;
220
/* Only prompt the user once. */
221
ssl_cert_password_required = -1;
222
- ssl_cert_password = getpass("Certificate Password: ");
222
+ ssl_cert_password = git_getpass("Certificate Password: ");
223
if (ssl_cert_password != NULL) {
224
ssl_cert_password = xstrdup(ssl_cert_password);
225
return 1;
imap-send.c
+1
-1
@@ -1107,7 +1107,7 @@ static struct store *imap_open_store(struct imap_server_conf *srvc)
1107
if (!srvc->pass) {
1108
char prompt[80];
1109
sprintf(prompt, "Password (%s@%s): ", srvc->user, srvc->host);
1110
- arg = getpass(prompt);
1110
+ arg = git_getpass(prompt);
1111
if (!arg) {
1112
perror("getpass");
1113
exit(1);