Skip to content

Commit

Permalink
Merge branch 'jk/git-prompt'
Browse files Browse the repository at this point in the history
* jk/git-prompt:
  contrib: add credential helper for OS X Keychain
  Makefile: OS X has /dev/tty
  Makefile: linux has /dev/tty
  credential: use git_prompt instead of git_getpass
  prompt: use git_terminal_prompt
  add generic terminal prompt function
  refactor git_getpass into generic prompt function
  move git_getpass to its own source file
  imap-send: don't check return value of git_getpass
  imap-send: avoid buffer overflow

Conflicts:
	Makefile
  • Loading branch information
gitster committed Dec 22, 2011
2 parents 200888e + 34961d3 commit ded408f
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 60 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ all::
#
# Define NO_REGEX if you have no or inferior regex support in your C library.
#
# Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
# user.
#
# Define GETTEXT_POISON if you are debugging the choice of strings marked
# for translation. In a GETTEXT_POISON build, you can turn all strings marked
# for translation into gibberish by setting the GIT_GETTEXT_POISON variable
Expand Down Expand Up @@ -543,6 +546,7 @@ LIB_H += compat/bswap.h
LIB_H += compat/cygwin.h
LIB_H += compat/mingw.h
LIB_H += compat/obstack.h
LIB_H += compat/terminal.h
LIB_H += compat/win32/pthread.h
LIB_H += compat/win32/syslog.h
LIB_H += compat/win32/poll.h
Expand Down Expand Up @@ -585,6 +589,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 @@ -632,6 +637,7 @@ LIB_OBJS += color.o
LIB_OBJS += combine-diff.o
LIB_OBJS += commit.o
LIB_OBJS += compat/obstack.o
LIB_OBJS += compat/terminal.o
LIB_OBJS += config.o
LIB_OBJS += connect.o
LIB_OBJS += connected.o
Expand Down Expand Up @@ -694,6 +700,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 Expand Up @@ -856,6 +863,7 @@ ifeq ($(uname_S),Linux)
NO_MKSTEMPS = YesPlease
HAVE_PATHS_H = YesPlease
LIBC_CONTAINS_LIBINTL = YesPlease
HAVE_DEV_TTY = YesPlease
endif
ifeq ($(uname_S),GNU/kFreeBSD)
NO_STRLCPY = YesPlease
Expand Down Expand Up @@ -917,6 +925,7 @@ ifeq ($(uname_S),Darwin)
endif
NO_MEMMEM = YesPlease
USE_ST_TIMESPEC = YesPlease
HAVE_DEV_TTY = YesPlease
endif
ifeq ($(uname_S),SunOS)
NEEDS_SOCKET = YesPlease
Expand Down Expand Up @@ -1685,6 +1694,10 @@ ifdef HAVE_LIBCHARSET_H
BASIC_CFLAGS += -DHAVE_LIBCHARSET_H
endif

ifdef HAVE_DEV_TTY
BASIC_CFLAGS += -DHAVE_DEV_TTY
endif

ifdef DIR_HAS_BSD_GROUP_SEMANTICS
COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS
endif
Expand Down
1 change: 0 additions & 1 deletion cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,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
81 changes: 81 additions & 0 deletions compat/terminal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "git-compat-util.h"
#include "compat/terminal.h"
#include "sigchain.h"
#include "strbuf.h"

#ifdef HAVE_DEV_TTY

static int term_fd = -1;
static struct termios old_term;

static void restore_term(void)
{
if (term_fd < 0)
return;

tcsetattr(term_fd, TCSAFLUSH, &old_term);
term_fd = -1;
}

static void restore_term_on_signal(int sig)
{
restore_term();
sigchain_pop(sig);
raise(sig);
}

char *git_terminal_prompt(const char *prompt, int echo)
{
static struct strbuf buf = STRBUF_INIT;
int r;
FILE *fh;

fh = fopen("/dev/tty", "w+");
if (!fh)
return NULL;

if (!echo) {
struct termios t;

if (tcgetattr(fileno(fh), &t) < 0) {
fclose(fh);
return NULL;
}

old_term = t;
term_fd = fileno(fh);
sigchain_push_common(restore_term_on_signal);

t.c_lflag &= ~ECHO;
if (tcsetattr(fileno(fh), TCSAFLUSH, &t) < 0) {
term_fd = -1;
fclose(fh);
return NULL;
}
}

fputs(prompt, fh);
fflush(fh);

r = strbuf_getline(&buf, fh, '\n');
if (!echo) {
putc('\n', fh);
fflush(fh);
}

restore_term();
fclose(fh);

if (r == EOF)
return NULL;
return buf.buf;
}

#else

char *git_terminal_prompt(const char *prompt, int echo)
{
return getpass(prompt);
}

#endif
6 changes: 6 additions & 0 deletions compat/terminal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef COMPAT_TERMINAL_H
#define COMPAT_TERMINAL_H

char *git_terminal_prompt(const char *prompt, int echo);

#endif /* COMPAT_TERMINAL_H */
44 changes: 0 additions & 44 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,47 +608,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 contrib/credential/osxkeychain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git-credential-osxkeychain
14 changes: 14 additions & 0 deletions contrib/credential/osxkeychain/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
all:: git-credential-osxkeychain

CC = gcc
RM = rm -f
CFLAGS = -g -Wall

git-credential-osxkeychain: git-credential-osxkeychain.o
$(CC) -o $@ $< -Wl,-framework -Wl,Security

git-credential-osxkeychain.o: git-credential-osxkeychain.c
$(CC) -c $(CFLAGS) $<

clean:
$(RM) git-credential-osxkeychain git-credential-osxkeychain.o
Loading

0 comments on commit ded408f

Please sign in to comment.