config: add new way to pass config via --config-env

While it's already possible to pass runtime configuration via `git -c
<key>=<value>`, it may be undesirable to use when the value contains
sensitive information. E.g. if one wants to set `http.extraHeader` to
contain an authentication token, doing so via `-c` would trivially leak
those credentials via e.g. ps(1), which typically also shows command
arguments.

To enable this usecase without leaking credentials, this commit
introduces a new switch `--config-env=<key>=<envvar>`. Instead of
directly passing a value for the given key, it instead allows the user
to specify the name of an environment variable. The value of that
variable will then be used as value of the key.

Co-authored-by: Jeff King <peff@peff.net>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Patrick Steinhardt
2021-01-12 13:26:45 +01:00
committed by Junio C Hamano
parent b0812b6ac0
commit ce81b1da23
5 changed files with 100 additions and 2 deletions

View File

@ -345,6 +345,31 @@ void git_config_push_parameter(const char *text)
strbuf_release(&env);
}
void git_config_push_env(const char *spec)
{
struct strbuf buf = STRBUF_INIT;
const char *env_name;
const char *env_value;
env_name = strrchr(spec, '=');
if (!env_name)
die(_("invalid config format: %s"), spec);
env_name++;
if (!*env_name)
die(_("missing environment variable name for configuration '%.*s'"),
(int)(env_name - spec - 1), spec);
env_value = getenv(env_name);
if (!env_value)
die(_("missing environment variable '%s' for configuration '%.*s'"),
env_name, (int)(env_name - spec - 1), spec);
strbuf_add(&buf, spec, env_name - spec);
strbuf_addstr(&buf, env_value);
git_config_push_parameter(buf.buf);
strbuf_release(&buf);
}
static inline int iskeychar(int c)
{
return isalnum(c) || c == '-';