Skip to content

Commit c71d8bb

Browse files
dschogitster
authored andcommitted
git_config_set: reuse empty sections
It can happen quite easily that the last setting in a config section is removed, and to avoid confusion when there are comments in the config about that section, we keep a lone section header, i.e. an empty section. Now that we use the `event_fn` callback, it is easy to add support for re-using empty sections, so let's do that. Note: t5512-ls-remote requires that this change is applied *after* the patch "git config --unset: remove empty sections (in the common case)": without that patch, there would be empty `transfer` and `uploadpack` sections ready for reuse, but in the *wrong* order (and sconsequently, t5512's "overrides work between mixed transfer/upload-pack hideRefs" would fail). Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 22aedfc commit c71d8bb

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

config.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,12 @@ static int store_aux_event(enum config_event_t type,
23362336
store->parsed[store->parsed_nr].is_keys_section =
23372337
cf->var.len - 1 == store->baselen &&
23382338
!strncasecmp(cf->var.buf, store->key, store->baselen);
2339+
if (store->is_keys_section) {
2340+
store->section_seen = 1;
2341+
ALLOC_GROW(store->seen, store->seen_nr + 1,
2342+
store->seen_alloc);
2343+
store->seen[store->seen_nr] = store->parsed_nr;
2344+
}
23392345
}
23402346

23412347
store->parsed_nr++;
@@ -2770,7 +2776,13 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
27702776

27712777
new_line = 0;
27722778
if (!store.key_seen) {
2773-
replace_end = copy_end = store.parsed[j].end;
2779+
copy_end = store.parsed[j].end;
2780+
/* include '\n' when copying section header */
2781+
if (copy_end > 0 && copy_end < contents_sz &&
2782+
contents[copy_end - 1] != '\n' &&
2783+
contents[copy_end] == '\n')
2784+
copy_end++;
2785+
replace_end = copy_end;
27742786
} else {
27752787
replace_end = store.parsed[j].end;
27762788
copy_end = store.parsed[j].begin;

t/t1300-config.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ test_expect_success '--unset-all removes section if empty & uncommented' '
14831483
test_line_count = 0 .git/config
14841484
'
14851485

1486-
test_expect_failure 'adding a key into an empty section reuses header' '
1486+
test_expect_success 'adding a key into an empty section reuses header' '
14871487
cat >.git/config <<-\EOF &&
14881488
[section]
14891489
EOF

0 commit comments

Comments
 (0)