]> git.ipfire.org Git - thirdparty/git.git/blobdiff - t/t1300-config.sh
Merge branch 'jt/trace2-BUG'
[thirdparty/git.git] / t / t1300-config.sh
index 1a4156c70434f34ef7693d60b5932d92d18845b0..936d72041babf0b07d65f47ba12fe59c7e07b48e 100755 (executable)
@@ -5,6 +5,9 @@
 
 test_description='Test git config in different settings'
 
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 . ./test-lib.sh
 
 test_expect_success 'clear default config' '
@@ -1046,8 +1049,8 @@ test_expect_success 'check split_cmdline return' "
        echo foo > foo &&
        git add foo &&
        git commit -m 'initial commit' &&
-       git config branch.master.mergeoptions 'echo \"' &&
-       test_must_fail git merge master
+       git config branch.main.mergeoptions 'echo \"' &&
+       test_must_fail git merge main
 "
 
 test_expect_success 'git -c "key=value" support' '
@@ -1286,6 +1289,58 @@ test_expect_success 'git -c is not confused by empty environment' '
        GIT_CONFIG_PARAMETERS="" git -c x.one=1 config --list
 '
 
+test_expect_success 'GIT_CONFIG_PARAMETERS handles old-style entries' '
+       v="${SQ}key.one=foo${SQ}" &&
+       v="$v  ${SQ}key.two=bar${SQ}" &&
+       v="$v ${SQ}key.ambiguous=section.whatever=value${SQ}" &&
+       GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+       cat >expect <<-EOF &&
+       key.one foo
+       key.two bar
+       key.ambiguous section.whatever=value
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'GIT_CONFIG_PARAMETERS handles new-style entries' '
+       v="${SQ}key.one${SQ}=${SQ}foo${SQ}" &&
+       v="$v  ${SQ}key.two${SQ}=${SQ}bar${SQ}" &&
+       v="$v ${SQ}key.ambiguous=section.whatever${SQ}=${SQ}value${SQ}" &&
+       GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+       cat >expect <<-EOF &&
+       key.one foo
+       key.two bar
+       key.ambiguous=section.whatever value
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'old and new-style entries can mix' '
+       v="${SQ}key.oldone=oldfoo${SQ}" &&
+       v="$v ${SQ}key.newone${SQ}=${SQ}newfoo${SQ}" &&
+       v="$v ${SQ}key.oldtwo=oldbar${SQ}" &&
+       v="$v ${SQ}key.newtwo${SQ}=${SQ}newbar${SQ}" &&
+       GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+       cat >expect <<-EOF &&
+       key.oldone oldfoo
+       key.newone newfoo
+       key.oldtwo oldbar
+       key.newtwo newbar
+       EOF
+       test_cmp expect actual
+'
+
+test_expect_success 'old and new bools with ambiguous subsection' '
+       v="${SQ}key.with=equals.oldbool${SQ}" &&
+       v="$v ${SQ}key.with=equals.newbool${SQ}=" &&
+       GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+       cat >expect <<-EOF &&
+       key.with equals.oldbool
+       key.with=equals.newbool
+       EOF
+       test_cmp expect actual
+'
+
 test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' '
        cat >expect <<-\EOF &&
        env.one one
@@ -1308,6 +1363,173 @@ test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' '
                git config --get-regexp "env.*"
 '
 
+test_expect_success 'git --config-env=key=envvar support' '
+       cat >expect <<-\EOF &&
+       value
+       value
+       false
+       EOF
+       {
+               ENVVAR=value git --config-env=core.name=ENVVAR config core.name &&
+               ENVVAR=value git --config-env=foo.CamelCase=ENVVAR config foo.camelcase &&
+               ENVVAR= git --config-env=foo.flag=ENVVAR config --bool foo.flag
+       } >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'git --config-env fails with invalid parameters' '
+       test_must_fail git --config-env=foo.flag config --bool foo.flag 2>error &&
+       test_i18ngrep "invalid config format: foo.flag" error &&
+       test_must_fail git --config-env=foo.flag= config --bool foo.flag 2>error &&
+       test_i18ngrep "missing environment variable name for configuration ${SQ}foo.flag${SQ}" error &&
+       sane_unset NONEXISTENT &&
+       test_must_fail git --config-env=foo.flag=NONEXISTENT config --bool foo.flag 2>error &&
+       test_i18ngrep "missing environment variable ${SQ}NONEXISTENT${SQ} for configuration ${SQ}foo.flag${SQ}" error
+'
+
+test_expect_success 'git -c and --config-env work together' '
+       cat >expect <<-\EOF &&
+       bar.cmd cmd-value
+       bar.env env-value
+       EOF
+       ENVVAR=env-value git \
+               -c bar.cmd=cmd-value \
+               --config-env=bar.env=ENVVAR \
+               config --get-regexp "^bar.*" >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'git -c and --config-env override each other' '
+       cat >expect <<-\EOF &&
+       env
+       cmd
+       EOF
+       {
+               ENVVAR=env git -c bar.bar=cmd --config-env=bar.bar=ENVVAR config bar.bar &&
+               ENVVAR=env git --config-env=bar.bar=ENVVAR -c bar.bar=cmd config bar.bar
+       } >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success '--config-env handles keys with equals' '
+       echo value=with=equals >expect &&
+       ENVVAR=value=with=equals git \
+               --config-env=section.subsection=with=equals.key=ENVVAR \
+               config section.subsection=with=equals.key >actual &&
+       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 &&
@@ -1653,9 +1875,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
 '
 
@@ -1761,11 +1985,11 @@ test_expect_success '--show-origin blob' '
 
 test_expect_success '--show-origin blob ref' '
        cat >expect <<-\EOF &&
-       blob:master:custom.conf user.custom=true
+       blob:main:custom.conf   user.custom=true
        EOF
        git add "$CUSTOM_CONFIG_FILE" &&
        git commit -m "new config file" &&
-       git config --blob=master:"$CUSTOM_CONFIG_FILE" --show-origin --list >output &&
+       git config --blob=main:"$CUSTOM_CONFIG_FILE" --show-origin --list >output &&
        test_cmp expect output
 '