Skip to content

Commit 3497717

Browse files
committed
Merge branch 'tr/config-multivalue-lift-max'
* tr/config-multivalue-lift-max: config: arbitrary number of matches for --unset and --replace-all
2 parents e66ef7a + 83786fa commit 3497717

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

config.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,15 +1210,14 @@ int git_config(config_fn_t fn, void *data)
12101210
* Find all the stuff for git_config_set() below.
12111211
*/
12121212

1213-
#define MAX_MATCHES 512
1214-
12151213
static struct {
12161214
int baselen;
12171215
char *key;
12181216
int do_not_match;
12191217
regex_t *value_regex;
12201218
int multi_replace;
1221-
size_t offset[MAX_MATCHES];
1219+
size_t *offset;
1220+
unsigned int offset_alloc;
12221221
enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
12231222
int seen;
12241223
} store;
@@ -1241,11 +1240,11 @@ static int store_aux(const char *key, const char *value, void *cb)
12411240
if (matches(key, value)) {
12421241
if (store.seen == 1 && store.multi_replace == 0) {
12431242
warning("%s has multiple values", key);
1244-
} else if (store.seen >= MAX_MATCHES) {
1245-
error("too many matches for %s", key);
1246-
return 1;
12471243
}
12481244

1245+
ALLOC_GROW(store.offset, store.seen + 1,
1246+
store.offset_alloc);
1247+
12491248
store.offset[store.seen] = cf->do_ftell(cf);
12501249
store.seen++;
12511250
}
@@ -1273,18 +1272,25 @@ static int store_aux(const char *key, const char *value, void *cb)
12731272
* Do not increment matches: this is no match, but we
12741273
* just made sure we are in the desired section.
12751274
*/
1275+
ALLOC_GROW(store.offset, store.seen + 1,
1276+
store.offset_alloc);
12761277
store.offset[store.seen] = cf->do_ftell(cf);
12771278
/* fallthru */
12781279
case SECTION_END_SEEN:
12791280
case START:
12801281
if (matches(key, value)) {
1282+
ALLOC_GROW(store.offset, store.seen + 1,
1283+
store.offset_alloc);
12811284
store.offset[store.seen] = cf->do_ftell(cf);
12821285
store.state = KEY_SEEN;
12831286
store.seen++;
12841287
} else {
12851288
if (strrchr(key, '.') - key == store.baselen &&
12861289
!strncmp(key, store.key, store.baselen)) {
12871290
store.state = SECTION_SEEN;
1291+
ALLOC_GROW(store.offset,
1292+
store.seen + 1,
1293+
store.offset_alloc);
12881294
store.offset[store.seen] = cf->do_ftell(cf);
12891295
}
12901296
}
@@ -1583,6 +1589,7 @@ int git_config_set_multivar_in_file(const char *config_filename,
15831589
}
15841590
}
15851591

1592+
ALLOC_GROW(store.offset, 1, store.offset_alloc);
15861593
store.offset[0] = 0;
15871594
store.state = START;
15881595
store.seen = 0;

t/t1303-wacky-config.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,28 @@
33
test_description='Test wacky input to git config'
44
. ./test-lib.sh
55

6+
# Leaving off the newline is intentional!
67
setup() {
78
(printf "[section]\n" &&
89
printf " key = foo") >.git/config
910
}
1011

12+
# 'check section.key value' verifies that the entry for section.key is
13+
# 'value'
1114
check() {
1215
echo "$2" >expected
1316
git config --get "$1" >actual 2>&1
1417
test_cmp actual expected
1518
}
1619

20+
# 'check section.key regex value' verifies that the entry for
21+
# section.key *that matches 'regex'* is 'value'
22+
check_regex() {
23+
echo "$3" >expected
24+
git config --get "$1" "$2" >actual 2>&1
25+
test_cmp actual expected
26+
}
27+
1728
test_expect_success 'modify same key' '
1829
setup &&
1930
git config section.key bar &&
@@ -47,4 +58,57 @@ test_expect_success 'do not crash on special long config line' '
4758
check section.key "$LONG_VALUE"
4859
'
4960

61+
setup_many() {
62+
setup &&
63+
# This time we want the newline so that we can tack on more
64+
# entries.
65+
echo >>.git/config &&
66+
# Semi-efficient way of concatenating 5^5 = 3125 lines. Note
67+
# that because 'setup' already put one line, this means 3126
68+
# entries for section.key in the config file.
69+
cat >5to1 <<-\EOF &&
70+
key = foo
71+
key = foo
72+
key = foo
73+
key = foo
74+
key = foo
75+
EOF
76+
cat 5to1 5to1 5to1 5to1 5to1 >5to2 && # 25
77+
cat 5to2 5to2 5to2 5to2 5to2 >5to3 && # 125
78+
cat 5to3 5to3 5to3 5to3 5to3 >5to4 && # 635
79+
cat 5to4 5to4 5to4 5to4 5to4 >>.git/config # 3125
80+
}
81+
82+
test_expect_success 'get many entries' '
83+
setup_many &&
84+
git config --get-all section.key >actual &&
85+
test_line_count = 3126 actual
86+
'
87+
88+
test_expect_success 'get many entries by regex' '
89+
setup_many &&
90+
git config --get-regexp "sec.*ke." >actual &&
91+
test_line_count = 3126 actual
92+
'
93+
94+
test_expect_success 'add and replace one of many entries' '
95+
setup_many &&
96+
git config --add section.key bar &&
97+
check_regex section.key "b.*r" bar &&
98+
git config section.key beer "b.*r" &&
99+
check_regex section.key "b.*r" beer
100+
'
101+
102+
test_expect_success 'replace many entries' '
103+
setup_many &&
104+
git config --replace-all section.key bar &&
105+
check section.key bar
106+
'
107+
108+
test_expect_success 'unset many entries' '
109+
setup_many &&
110+
git config --unset-all section.key &&
111+
test_must_fail git config section.key
112+
'
113+
50114
test_done

0 commit comments

Comments
 (0)