Skip to content
/ git Public
forked from git/git

Commit

Permalink
Merge branch 'jk/credential-quit'
Browse files Browse the repository at this point in the history
Credential helpers are asked in turn until one of them give
positive response, which is cumbersome to turn off when you need to
run Git in an automated setting.  The credential helper interface
learned to allow a helper to say "stop, don't ask other helpers."
Also GIT_TERMINAL_PROMPT environment can be set to false to disable
our built-in prompt mechanism for passwords.

* jk/credential-quit:
  prompt: respect GIT_TERMINAL_PROMPT to disable terminal prompts
  credential: let helpers tell us to quit
  • Loading branch information
gitster committed Dec 22, 2014
2 parents 2f17ecb + e652c0e commit 86362f7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Documentation/git.txt
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,10 @@ for further details.
and read the password from its STDOUT. See also the 'core.askpass'
option in linkgit:git-config[1].

'GIT_TERMINAL_PROMPT'::
If this environment variable is set to `0`, git will not prompt
on the terminal (e.g., when asking for HTTP authentication).

'GIT_CONFIG_NOSYSTEM'::
Whether to skip reading settings from the system-wide
`$(prefix)/etc/gitconfig` file. This environment variable can
Expand Down
5 changes: 4 additions & 1 deletion Documentation/technical/api-credentials.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,10 @@ FORMAT` in linkgit:git-credential[7] for a detailed specification).
For a `get` operation, the helper should produce a list of attributes
on stdout in the same format. A helper is free to produce a subset, or
even no values at all if it has nothing useful to provide. Any provided
attributes will overwrite those already known about by Git.
attributes will overwrite those already known about by Git. If a helper
outputs a `quit` attribute with a value of `true` or `1`, no further
helpers will be consulted, nor will the user be prompted (if no
credential has been provided, the operation will then fail).

For a `store` or `erase` operation, the helper's output is ignored.
If it fails to perform the requested operation, it may complain to
Expand Down
5 changes: 5 additions & 0 deletions credential.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ int credential_read(struct credential *c, FILE *fp)
c->path = xstrdup(value);
} else if (!strcmp(key, "url")) {
credential_from_url(c, value);
} else if (!strcmp(key, "quit")) {
c->quit = !!git_config_bool("quit", value);
}
/*
* Ignore other lines; we don't know what they mean, but
Expand Down Expand Up @@ -274,6 +276,9 @@ void credential_fill(struct credential *c)
credential_do(c, c->helpers.items[i].string, "get");
if (c->username && c->password)
return;
if (c->quit)
die("credential helper '%s' told us to quit",
c->helpers.items[i].string);
}

credential_getpass(c);
Expand Down
1 change: 1 addition & 0 deletions credential.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct credential {
struct string_list helpers;
unsigned approved:1,
configured:1,
quit:1,
use_http_path:1;

char *username;
Expand Down
16 changes: 12 additions & 4 deletions prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ char *git_prompt(const char *prompt, int flags)
r = do_askpass(askpass, prompt);
}

if (!r)
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
if (!r) {
/* prompts already contain ": " at the end */
die("could not read %s%s", prompt, strerror(errno));
const char *err;

if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) {
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
err = strerror(errno);
} else {
err = "terminal prompts disabled";
}
if (!r) {
/* prompts already contain ": " at the end */
die("could not read %s%s", prompt, err);
}
}
return r;
}
Expand Down
9 changes: 9 additions & 0 deletions t/t0300-credentials.sh
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,13 @@ test_expect_success 'http paths can be part of context' '
EOF
'

test_expect_success 'helpers can abort the process' '
test_must_fail git \
-c credential.helper="!f() { echo quit=1; }; f" \
-c credential.helper="verbatim foo bar" \
credential fill >stdout &&
>expect &&
test_cmp expect stdout
'

test_done

0 comments on commit 86362f7

Please sign in to comment.