Skip to content

Commit 86362f7

Browse files
committed
Merge branch 'jk/credential-quit'
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
2 parents 2f17ecb + e652c0e commit 86362f7

File tree

6 files changed

+35
-5
lines changed

6 files changed

+35
-5
lines changed

‎Documentation/git.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,10 @@ for further details.
913913
and read the password from its STDOUT. See also the 'core.askpass'
914914
option in linkgit:git-config[1].
915915

916+
'GIT_TERMINAL_PROMPT'::
917+
If this environment variable is set to `0`, git will not prompt
918+
on the terminal (e.g., when asking for HTTP authentication).
919+
916920
'GIT_CONFIG_NOSYSTEM'::
917921
Whether to skip reading settings from the system-wide
918922
`$(prefix)/etc/gitconfig` file. This environment variable can

‎Documentation/technical/api-credentials.txt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ FORMAT` in linkgit:git-credential[7] for a detailed specification).
248248
For a `get` operation, the helper should produce a list of attributes
249249
on stdout in the same format. A helper is free to produce a subset, or
250250
even no values at all if it has nothing useful to provide. Any provided
251-
attributes will overwrite those already known about by Git.
251+
attributes will overwrite those already known about by Git. If a helper
252+
outputs a `quit` attribute with a value of `true` or `1`, no further
253+
helpers will be consulted, nor will the user be prompted (if no
254+
credential has been provided, the operation will then fail).
252255

253256
For a `store` or `erase` operation, the helper's output is ignored.
254257
If it fails to perform the requested operation, it may complain to

‎credential.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ int credential_read(struct credential *c, FILE *fp)
173173
c->path = xstrdup(value);
174174
} else if (!strcmp(key, "url")) {
175175
credential_from_url(c, value);
176+
} else if (!strcmp(key, "quit")) {
177+
c->quit = !!git_config_bool("quit", value);
176178
}
177179
/*
178180
* Ignore other lines; we don't know what they mean, but
@@ -274,6 +276,9 @@ void credential_fill(struct credential *c)
274276
credential_do(c, c->helpers.items[i].string, "get");
275277
if (c->username && c->password)
276278
return;
279+
if (c->quit)
280+
die("credential helper '%s' told us to quit",
281+
c->helpers.items[i].string);
277282
}
278283

279284
credential_getpass(c);

‎credential.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct credential {
77
struct string_list helpers;
88
unsigned approved:1,
99
configured:1,
10+
quit:1,
1011
use_http_path:1;
1112

1213
char *username;

‎prompt.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,19 @@ char *git_prompt(const char *prompt, int flags)
5757
r = do_askpass(askpass, prompt);
5858
}
5959

60-
if (!r)
61-
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
6260
if (!r) {
63-
/* prompts already contain ": " at the end */
64-
die("could not read %s%s", prompt, strerror(errno));
61+
const char *err;
62+
63+
if (git_env_bool("GIT_TERMINAL_PROMPT", 1)) {
64+
r = git_terminal_prompt(prompt, flags & PROMPT_ECHO);
65+
err = strerror(errno);
66+
} else {
67+
err = "terminal prompts disabled";
68+
}
69+
if (!r) {
70+
/* prompts already contain ": " at the end */
71+
die("could not read %s%s", prompt, err);
72+
}
6573
}
6674
return r;
6775
}

‎t/t0300-credentials.sh‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,13 @@ test_expect_success 'http paths can be part of context' '
289289
EOF
290290
'
291291

292+
test_expect_success 'helpers can abort the process' '
293+
test_must_fail git \
294+
-c credential.helper="!f() { echo quit=1; }; f" \
295+
-c credential.helper="verbatim foo bar" \
296+
credential fill >stdout &&
297+
>expect &&
298+
test_cmp expect stdout
299+
'
300+
292301
test_done

0 commit comments

Comments
 (0)