Skip to content

Commit d3c58b8

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6c597ae commit d3c58b8

File tree

7 files changed

+58
-45
lines changed

7 files changed

+58
-45
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ LIB_H += parse-options.h
563563
LIB_H += patch-ids.h
564564
LIB_H += pkt-line.h
565565
LIB_H += progress.h
566+
LIB_H += prompt.h
566567
LIB_H += quote.h
567568
LIB_H += reflog-walk.h
568569
LIB_H += refs.h
@@ -669,6 +670,7 @@ LIB_OBJS += pkt-line.o
669670
LIB_OBJS += preload-index.o
670671
LIB_OBJS += pretty.o
671672
LIB_OBJS += progress.o
673+
LIB_OBJS += prompt.o
672674
LIB_OBJS += quote.o
673675
LIB_OBJS += reachable.o
674676
LIB_OBJS += read-cache.o

cache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,6 @@ struct ref {
10241024
extern struct ref *find_ref_by_name(const struct ref *list, const char *name);
10251025

10261026
#define CONNECT_VERBOSE (1u << 0)
1027-
extern char *git_getpass(const char *prompt);
10281027
extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
10291028
extern int finish_connect(struct child_process *conn);
10301029
extern int git_connection_is_socket(struct child_process *conn);

connect.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -619,47 +619,3 @@ int finish_connect(struct child_process *conn)
619619
free(conn);
620620
return code;
621621
}
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "string-list.h"
44
#include "run-command.h"
55
#include "url.h"
6+
#include "prompt.h"
67

78
void credential_init(struct credential *c)
89
{

imap-send.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "cache.h"
2626
#include "exec_cmd.h"
2727
#include "run-command.h"
28+
#include "prompt.h"
2829
#ifdef NO_OPENSSL
2930
typedef void *SSL;
3031
#else

prompt.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

0 commit comments

Comments
 (0)