Skip to content

Commit

Permalink
clone: handle empty config values in -c
Browse files Browse the repository at this point in the history
"git clone --config" uses the following incantation to add an item to
a config file, instead of replacing an existing value:

	git_config_set_multivar_gently(key, value, "^$", 0)

As long as no existing value matches the regex ^$, that works as
intended and adds to the config.  When a value is empty, though, it
replaces the existing value.

Noticed while trying to set credential.helper during a clone to use a
specific helper without inheriting from ~/.gitconfig and
/etc/gitconfig.  That is, I ran

	git clone -c credential.helper= \
		-c credential.helper=myhelper \
		https://example.com/repo

intending to produce the configuration

	[credential]
		helper =
		helper = myhelper

Without this patch, the 'helper =' line is not included and the
credential helper from /etc/gitconfig gets used.

Signed-off-by: Jonathan Nieder <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
jrn authored and gitster committed May 2, 2017
1 parent 3b9e3c2 commit db4eca1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,9 @@ static int checkout(int submodule_progress)

static int write_one_config(const char *key, const char *value, void *data)
{
return git_config_set_multivar_gently(key, value ? value : "true", "^$", 0);
return git_config_set_multivar_gently(key,
value ? value : "true",
CONFIG_REGEX_NONE, 0);
}

static void write_config(struct string_list *config)
Expand Down
8 changes: 8 additions & 0 deletions t/t5611-clone-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ test_expect_success 'clone -c can set multi-keys' '
test_cmp expect actual
'

test_expect_success 'clone -c can set multi-keys, including some empty' '
rm -rf child &&
git clone -c credential.helper= -c credential.helper=hi . child &&
printf "%s\n" "" hi >expect &&
git --git-dir=child/.git config --get-all credential.helper >actual &&
test_cmp expect actual
'

test_expect_success 'clone -c without a value is boolean true' '
rm -rf child &&
git clone -c core.foo . child &&
Expand Down

0 comments on commit db4eca1

Please sign in to comment.