Skip to content

Commit

Permalink
move git_getpass to its own source file
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
peff authored and gitster committed Dec 13, 2011
1 parent 6c597ae commit d3c58b8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 45 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ LIB_H += parse-options.h
LIB_H += patch-ids.h
LIB_H += pkt-line.h
LIB_H += progress.h
LIB_H += prompt.h
LIB_H += quote.h
LIB_H += reflog-walk.h
LIB_H += refs.h
Expand Down Expand Up @@ -669,6 +670,7 @@ LIB_OBJS += pkt-line.o
LIB_OBJS += preload-index.o
LIB_OBJS += pretty.o
LIB_OBJS += progress.o
LIB_OBJS += prompt.o
LIB_OBJS += quote.o
LIB_OBJS += reachable.o
LIB_OBJS += read-cache.o
Expand Down
1 change: 0 additions & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,6 @@ struct ref {
extern struct ref *find_ref_by_name(const struct ref *list, const char *name);

#define CONNECT_VERBOSE (1u << 0)
extern char *git_getpass(const char *prompt);
extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags);
extern int finish_connect(struct child_process *conn);
extern int git_connection_is_socket(struct child_process *conn);
Expand Down
44 changes: 0 additions & 44 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,47 +619,3 @@ int finish_connect(struct child_process *conn)
free(conn);
return code;
}

char *git_getpass(const char *prompt)
{
const char *askpass;
struct child_process pass;
const char *args[3];
static struct strbuf buffer = STRBUF_INIT;

askpass = getenv("GIT_ASKPASS");
if (!askpass)
askpass = askpass_program;
if (!askpass)
askpass = getenv("SSH_ASKPASS");
if (!askpass || !(*askpass)) {
char *result = getpass(prompt);
if (!result)
die_errno("Could not read password");
return result;
}

args[0] = askpass;
args[1] = prompt;
args[2] = NULL;

memset(&pass, 0, sizeof(pass));
pass.argv = args;
pass.out = -1;

if (start_command(&pass))
exit(1);

strbuf_reset(&buffer);
if (strbuf_read(&buffer, pass.out, 20) < 0)
die("failed to read password from %s\n", askpass);

close(pass.out);

if (finish_command(&pass))
exit(1);

strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));

return buffer.buf;
}
1 change: 1 addition & 0 deletions credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "string-list.h"
#include "run-command.h"
#include "url.h"
#include "prompt.h"

void credential_init(struct credential *c)
{
Expand Down
1 change: 1 addition & 0 deletions imap-send.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "cache.h"
#include "exec_cmd.h"
#include "run-command.h"
#include "prompt.h"
#ifdef NO_OPENSSL
typedef void *SSL;
#else
Expand Down
48 changes: 48 additions & 0 deletions prompt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "cache.h"
#include "run-command.h"
#include "strbuf.h"
#include "prompt.h"

char *git_getpass(const char *prompt)
{
const char *askpass;
struct child_process pass;
const char *args[3];
static struct strbuf buffer = STRBUF_INIT;

askpass = getenv("GIT_ASKPASS");
if (!askpass)
askpass = askpass_program;
if (!askpass)
askpass = getenv("SSH_ASKPASS");
if (!askpass || !(*askpass)) {
char *result = getpass(prompt);
if (!result)
die_errno("Could not read password");
return result;
}

args[0] = askpass;
args[1] = prompt;
args[2] = NULL;

memset(&pass, 0, sizeof(pass));
pass.argv = args;
pass.out = -1;

if (start_command(&pass))
exit(1);

strbuf_reset(&buffer);
if (strbuf_read(&buffer, pass.out, 20) < 0)
die("failed to read password from %s\n", askpass);

close(pass.out);

if (finish_command(&pass))
exit(1);

strbuf_setlen(&buffer, strcspn(buffer.buf, "\r\n"));

return buffer.buf;
}
6 changes: 6 additions & 0 deletions prompt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef PROMPT_H
#define PROMPT_H

char *git_getpass(const char *prompt);

#endif /* PROMPT_H */

0 comments on commit d3c58b8

Please sign in to comment.