move git_getpass to its own source file

This is currently in connect.c, but really has nothing to do with the git protocol itself. Let's make a new source file all about prompting the user, which will make it cleaner to refactor. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 10, 2011 at 05:40 UTC d3c58b83aee2007ca76dc5d1242c09b6f7989c76
7 files changed +58 -45
Makefile
+2
@@ -563,6 +563,7 @@ LIB_H += parse-options.h
563 LIB_H += patch-ids.h
564 LIB_H += pkt-line.h
565 LIB_H += progress.h
566 +LIB_H += prompt.h
567 LIB_H += quote.h
568 LIB_H += reflog-walk.h
569 LIB_H += refs.h
@@ -669,6 +670,7 @@ LIB_OBJS += pkt-line.o
670 LIB_OBJS += preload-index.o
671 LIB_OBJS += pretty.o
672 LIB_OBJS += progress.o
673 +LIB_OBJS += prompt.o
674 LIB_OBJS += quote.o
675 LIB_OBJS += reachable.o
676 LIB_OBJS += read-cache.o
cache.h
-1
@@ -1024,7 +1024,6 @@ struct ref {
1024 extern struct ref *find_ref_by_name(const struct ref *list, const char *name);
1025
1026 #define CONNECT_VERBOSE (1u << 0)
1027 -extern char *git_getpass(const char *prompt);
1027 extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
1028 extern int finish_connect(struct child_process *conn);
1029 extern int git_connection_is_socket(struct child_process *conn);
connect.c
-44
@@ -619,47 +619,3 @@ int finish_connect(struct child_process *conn)
619 free(conn);
620 return code;
621 }
622 -
623 -char *git_getpass(const char *prompt)
624 -{
625 - const char *askpass;
626 - struct child_process pass;
627 - const char *args[3];
628 - static struct strbuf buffer = STRBUF_INIT;
629 -
630 - askpass = getenv("GIT_ASKPASS");
631 - if (!askpass)
632 - askpass = askpass_program;
633 - if (!askpass)
634 - askpass = getenv("SSH_ASKPASS");
635 - if (!askpass || !(*askpass)) {
636 - char *result = getpass(prompt);
637 - if (!result)
638 - die_errno("Could not read password");
639 - return result;
640 - }
641 -
642 - args[0] = askpass;
643 - args[1] = prompt;
644 - args[2] = NULL;
645 -
646 - memset(&pass, 0, sizeof(pass));
647 - pass.argv = args;
648 - pass.out = -1;
649 -
650 - if (start_command(&pass))
651 - exit(1);
652 -
653 - strbuf_reset(&buffer);
654 - if (strbuf_read(&buffer, pass.out, 20) < 0)
655 - die("failed to read password from %s\n", askpass);
656 -
657 - close(pass.out);
658 -
659 - if (finish_command(&pass))
660 - exit(1);
661 -
662 - strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
663 -
664 - return buffer.buf;
665 -}
credential.c
+1
@@ -3,6 +3,7 @@
3 #include "string-list.h"
4 #include "run-command.h"
5 #include "url.h"
6 +#include "prompt.h"
7
8 void credential_init(struct credential *c)
9 {
imap-send.c
+1
@@ -25,6 +25,7 @@
25 #include "cache.h"
26 #include "exec_cmd.h"
27 #include "run-command.h"
28 +#include "prompt.h"
29 #ifdef NO_OPENSSL
30 typedef void *SSL;
31 #else
prompt.c new
+48
@@ -0,0 +1,48 @@
1 +#include "cache.h"
2 +#include "run-command.h"
3 +#include "strbuf.h"
4 +#include "prompt.h"
5 +
6 +char *git_getpass(const char *prompt)
7 +{
8 + const char *askpass;
9 + struct child_process pass;
10 + const char *args[3];
11 + static struct strbuf buffer = STRBUF_INIT;
12 +
13 + askpass = getenv("GIT_ASKPASS");
14 + if (!askpass)
15 + askpass = askpass_program;
16 + if (!askpass)
17 + askpass = getenv("SSH_ASKPASS");
18 + if (!askpass || !(*askpass)) {
19 + char *result = getpass(prompt);
20 + if (!result)
21 + die_errno("Could not read password");
22 + return result;
23 + }
24 +
25 + args[0] = askpass;
26 + args[1] = prompt;
27 + args[2] = NULL;
28 +
29 + memset(&pass, 0, sizeof(pass));
30 + pass.argv = args;
31 + pass.out = -1;
32 +
33 + if (start_command(&pass))
34 + exit(1);
35 +
36 + strbuf_reset(&buffer);
37 + if (strbuf_read(&buffer, pass.out, 20) < 0)
38 + die("failed to read password from %s\n", askpass);
39 +
40 + close(pass.out);
41 +
42 + if (finish_command(&pass))
43 + exit(1);
44 +
45 + strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));
46 +
47 + return buffer.buf;
48 +}
prompt.h new
+6
@@ -0,0 +1,6 @@
1 +#ifndef PROMPT_H
2 +#define PROMPT_H
3 +
4 +char *git_getpass(const char *prompt);
5 +
6 +#endif /* PROMPT_H */