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 <johannes.schinde...@gmx.de>
---
 config.c          | 14 +++++++++++++-
 t/t1300-config.sh |  2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/config.c b/config.c
index 2c7a10acdaa..6155d0651bd 100644
--- a/config.c
+++ b/config.c
@@ -2344,6 +2344,12 @@ static int store_aux_event(enum config_event_t type,
                        store->parsed[store->parsed_nr].is_keys_section =
                        cf->var.len - 1 == store->baselen &&
                        !strncasecmp(cf->var.buf, store->key, store->baselen);
+               if (store->is_keys_section) {
+                       store->section_seen = 1;
+                       ALLOC_GROW(store->seen, store->seen_nr + 1,
+                                  store->seen_alloc);
+                       store->seen[store->seen_nr] = store->parsed_nr;
+               }
        }
 
        store->parsed_nr++;
@@ -2778,7 +2784,13 @@ int git_config_set_multivar_in_file_gently(const char 
*config_filename,
 
                        new_line = 0;
                        if (!store.key_seen) {
-                               replace_end = copy_end = store.parsed[j].end;
+                               copy_end = store.parsed[j].end;
+                               /* include '\n' when copying section header */
+                               if (copy_end > 0 && copy_end < contents_sz &&
+                                   contents[copy_end - 1] != '\n' &&
+                                   contents[copy_end] == '\n')
+                                       copy_end++;
+                               replace_end = copy_end;
                        } else {
                                replace_end = store.parsed[j].end;
                                copy_end = store.parsed[j].begin;
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index d973fd53398..eef0bbe4f9f 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1506,7 +1506,7 @@ test_expect_success '--unset-all removes section if empty 
& uncommented' '
        test_line_count = 0 .git/config
 '
 
-test_expect_failure 'adding a key into an empty section reuses header' '
+test_expect_success 'adding a key into an empty section reuses header' '
        cat >.git/config <<-\EOF &&
        [section]
        EOF
-- 
2.17.0.windows.1.4.g7e4058d72e3

Reply via email to