Skip to content

Commit 102edda

Browse files
committed
Merge branch 'ta/config-add-to-empty-or-true-fix' into maint
"git config --add section.var val" used to lose existing section.var whose value was an empty string. * ta/config-add-to-empty-or-true-fix: config: avoid a funny sentinel value "a^" make config --add behave correctly for empty and NULL values
2 parents 421ec4f + c1063be commit 102edda

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

builtin/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
586586
check_argc(argc, 2, 2);
587587
value = normalize_value(argv[0], argv[1]);
588588
return git_config_set_multivar_in_file(given_config_source.file,
589-
argv[0], value, "^$", 0);
589+
argv[0], value,
590+
CONFIG_REGEX_NONE, 0);
590591
}
591592
else if (actions == ACTION_REPLACE_ALL) {
592593
check_write();

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,8 @@ extern int update_server_info(int);
12811281
#define CONFIG_INVALID_PATTERN 6
12821282
#define CONFIG_GENERIC_ERROR 7
12831283

1284+
#define CONFIG_REGEX_NONE ((void *)1)
1285+
12841286
struct git_config_source {
12851287
unsigned int use_stdin:1;
12861288
const char *file;

config.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,10 +1236,15 @@ static struct {
12361236

12371237
static int matches(const char *key, const char *value)
12381238
{
1239-
return !strcmp(key, store.key) &&
1240-
(store.value_regex == NULL ||
1241-
(store.do_not_match ^
1242-
!regexec(store.value_regex, value, 0, NULL, 0)));
1239+
if (strcmp(key, store.key))
1240+
return 0; /* not ours */
1241+
if (!store.value_regex)
1242+
return 1; /* always matches */
1243+
if (store.value_regex == CONFIG_REGEX_NONE)
1244+
return 0; /* never matches */
1245+
1246+
return store.do_not_match ^
1247+
(value && !regexec(store.value_regex, value, 0, NULL, 0));
12431248
}
12441249

12451250
static int store_aux(const char *key, const char *value, void *cb)
@@ -1501,6 +1506,8 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
15011506
/*
15021507
* If value==NULL, unset in (remove from) config,
15031508
* if value_regex!=NULL, disregard key/value pairs where value does not match.
1509+
* if value_regex==CONFIG_REGEX_NONE, do not match any existing values
1510+
* (only add a new one)
15041511
* if multi_replace==0, nothing, or only one matching key/value is replaced,
15051512
* else all matching key/values (regardless how many) are removed,
15061513
* before the new pair is written.
@@ -1584,6 +1591,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
15841591

15851592
if (value_regex == NULL)
15861593
store.value_regex = NULL;
1594+
else if (value_regex == CONFIG_REGEX_NONE)
1595+
store.value_regex = CONFIG_REGEX_NONE;
15871596
else {
15881597
if (value_regex[0] == '!') {
15891598
store.do_not_match = 1;
@@ -1615,7 +1624,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
16151624
if (git_config_from_file(store_aux, config_filename, NULL)) {
16161625
error("invalid config file %s", config_filename);
16171626
free(store.key);
1618-
if (store.value_regex != NULL) {
1627+
if (store.value_regex != NULL &&
1628+
store.value_regex != CONFIG_REGEX_NONE) {
16191629
regfree(store.value_regex);
16201630
free(store.value_regex);
16211631
}
@@ -1624,7 +1634,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
16241634
}
16251635

16261636
free(store.key);
1627-
if (store.value_regex != NULL) {
1637+
if (store.value_regex != NULL &&
1638+
store.value_regex != CONFIG_REGEX_NONE) {
16281639
regfree(store.value_regex);
16291640
free(store.value_regex);
16301641
}

t/t1303-wacky-config.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ test_expect_success 'unset many entries' '
111111
test_must_fail git config section.key
112112
'
113113

114+
test_expect_success '--add appends new value after existing empty value' '
115+
cat >expect <<-\EOF &&
116+
117+
118+
fool
119+
roll
120+
EOF
121+
cp .git/config .git/config.old &&
122+
test_when_finished "mv .git/config.old .git/config" &&
123+
cat >.git/config <<-\EOF &&
124+
[foo]
125+
baz
126+
baz =
127+
baz = fool
128+
EOF
129+
git config --add foo.baz roll &&
130+
git config --get-all foo.baz >output &&
131+
test_cmp expect output
132+
'
133+
114134
test_done

0 commit comments

Comments
 (0)