summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2021-01-12 12:27:14 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-01-15 21:03:45 (GMT)
commitd8d77153eafdb0fc334e827976f09e4bdff26b58 (patch)
tree9ad261640d42b1c2e5cf36830f6c797e711ee547 /t
parentb9d147fb150c5e0960bc43ad5f3f843487f816f7 (diff)
downloadgit-d8d77153eafdb0fc334e827976f09e4bdff26b58.zip
git-d8d77153eafdb0fc334e827976f09e4bdff26b58.tar.gz
git-d8d77153eafdb0fc334e827976f09e4bdff26b58.tar.bz2
config: allow specifying config entries via envvar pairs
While we currently have the `GIT_CONFIG_PARAMETERS` environment variable which can be used to pass runtime configuration data to git processes, it's an internal implementation detail and not supposed to be used by end users. Next to being for internal use only, this way of passing config entries has a major downside: the config keys need to be parsed as they contain both key and value in a single variable. As such, it is left to the user to escape any potentially harmful characters in the value, which is quite hard to do if values are controlled by a third party. This commit thus adds a new way of adding config entries via the environment which gets rid of this shortcoming. If the user passes the `GIT_CONFIG_COUNT=$n` environment variable, Git will parse environment variable pairs `GIT_CONFIG_KEY_$i` and `GIT_CONFIG_VALUE_$i` for each `i` in `[0,n)`. While the same can be achieved with `git -c <name>=<value>`, one may wish to not do so for potentially 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. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 't')
-rwxr-xr-xt/t1300-config.sh115
1 files changed, 114 insertions, 1 deletions
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index cc68b42..51a0621 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1424,6 +1424,117 @@ test_expect_success '--config-env handles keys with equals' '
test_cmp expect actual
'
+test_expect_success 'git config handles environment config pairs' '
+ GIT_CONFIG_COUNT=2 \
+ GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="foo" \
+ GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="bar" \
+ git config --get-regexp "pair.*" >actual &&
+ cat >expect <<-EOF &&
+ pair.one foo
+ pair.two bar
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git config ignores pairs without count' '
+ test_must_fail env GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ git config pair.one 2>error &&
+ test_must_be_empty error
+'
+
+test_expect_success 'git config ignores pairs with zero count' '
+ test_must_fail env \
+ GIT_CONFIG_COUNT=0 \
+ GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ git config pair.one
+'
+
+test_expect_success 'git config ignores pairs exceeding count' '
+ GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="value" \
+ git config --get-regexp "pair.*" >actual &&
+ cat >expect <<-EOF &&
+ pair.one value
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git config ignores pairs with zero count' '
+ test_must_fail env \
+ GIT_CONFIG_COUNT=0 GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ git config pair.one >error &&
+ test_must_be_empty error
+'
+
+test_expect_success 'git config ignores pairs with empty count' '
+ test_must_fail env \
+ GIT_CONFIG_COUNT= GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ git config pair.one >error &&
+ test_must_be_empty error
+'
+
+test_expect_success 'git config fails with invalid count' '
+ test_must_fail env GIT_CONFIG_COUNT=10a git config --list 2>error &&
+ test_i18ngrep "bogus count" error &&
+ test_must_fail env GIT_CONFIG_COUNT=9999999999999999 git config --list 2>error &&
+ test_i18ngrep "too many entries" error
+'
+
+test_expect_success 'git config fails with missing config key' '
+ test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_VALUE_0="value" \
+ git config --list 2>error &&
+ test_i18ngrep "missing config key" error
+'
+
+test_expect_success 'git config fails with missing config value' '
+ test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="pair.one" \
+ git config --list 2>error &&
+ test_i18ngrep "missing config value" error
+'
+
+test_expect_success 'git config fails with invalid config pair key' '
+ test_must_fail env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0= GIT_CONFIG_VALUE_0=value \
+ git config --list &&
+ test_must_fail env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0=missing-section GIT_CONFIG_VALUE_0=value \
+ git config --list
+'
+
+test_expect_success 'environment overrides config file' '
+ test_when_finished "rm -f .git/config" &&
+ cat >.git/config <<-EOF &&
+ [pair]
+ one = value
+ EOF
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=override \
+ git config pair.one >actual &&
+ cat >expect <<-EOF &&
+ override
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_CONFIG_PARAMETERS overrides environment config' '
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \
+ GIT_CONFIG_PARAMETERS="${SQ}pair.one=override${SQ}" \
+ git config pair.one >actual &&
+ cat >expect <<-EOF &&
+ override
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'command line overrides environment config' '
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \
+ git -c pair.one=override config pair.one >actual &&
+ cat >expect <<-EOF &&
+ override
+ EOF
+ test_cmp expect actual
+'
+
test_expect_success 'git config --edit works' '
git config -f tmp test.value no &&
echo test.value=yes >expect &&
@@ -1769,9 +1880,11 @@ test_expect_success '--show-origin with --list' '
file:.git/config user.override=local
file:.git/config include.path=../include/relative.include
file:.git/../include/relative.include user.relative=include
+ command line: user.environ=true
command line: user.cmdline=true
EOF
- git -c user.cmdline=true config --list --show-origin >output &&
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.environ GIT_CONFIG_VALUE_0=true\
+ git -c user.cmdline=true config --list --show-origin >output &&
test_cmp expect output
'