Skip to content

Commit 5aa0c0d

Browse files
peffdscho
authored andcommitted
git_config_push_parameter: handle empty GIT_CONFIG_PARAMETERS
The "git -c var=value" option stuffs the config value into $GIT_CONFIG_PARAMETERS, so that sub-processes can see it. When the config is later read via git_config() or similar, we parse it back out of that variable. The parsing end is a little bit picky; it assumes that each entry was generated with sq_quote_buf(), and that there is no extraneous whitespace. On the generating end, we are careful to append to an existing $GIT_CONFIG_PARAMETERS variable if it exists. However, our test for "should we add a space separator" is too liberal: it will add one even if the environment variable exists but is empty. As a result, you might end up with: GIT_CONFIG_PARAMETERS=" 'core.foo=bar'" which the parser will choke on. This was hard to trigger in older versions of git, since we only set the variable when we had something to put into it (though you could certainly trigger it manually). But since 14111fc (git: submodule honor -c credential.* from command line, 2016-02-29), the submodule code will unconditionally put the $GIT_CONFIG_PARAMETERS variable into the environment of any operation in the submodule, whether it is empty or not. So any of those operations which themselves use "git -c" will generate the unparseable value and fail. We can easily fix it by catching this case on the generating side. While we're adding a test, let's also check that multiple layers of "git -c" work, which was previously not tested at all. Reported-by: Shin Fan <[email protected]> Signed-off-by: Jeff King <[email protected]> Reviewed-by: Jonathan Nieder <[email protected]> Tested-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent eb7995d commit 5aa0c0d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void git_config_push_parameter(const char *text)
162162
{
163163
struct strbuf env = STRBUF_INIT;
164164
const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
165-
if (old) {
165+
if (old && *old) {
166166
strbuf_addstr(&env, old);
167167
strbuf_addch(&env, ' ');
168168
}

t/t1300-repo-config.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,20 @@ test_expect_success 'git -c complains about empty key and value' '
10871087
test_must_fail git -c "" rev-parse
10881088
'
10891089

1090+
test_expect_success 'multiple git -c appends config' '
1091+
test_config alias.x "!git -c x.two=2 config --get-regexp ^x\.*" &&
1092+
cat >expect <<-\EOF &&
1093+
x.one 1
1094+
x.two 2
1095+
EOF
1096+
git -c x.one=1 x >actual &&
1097+
test_cmp expect actual
1098+
'
1099+
1100+
test_expect_success 'git -c is not confused by empty environment' '
1101+
GIT_CONFIG_PARAMETERS="" git -c x.one=1 config --list
1102+
'
1103+
10901104
test_expect_success 'git config --edit works' '
10911105
git config -f tmp test.value no &&
10921106
echo test.value=yes >expect &&

0 commit comments

Comments
 (0)