config: fix case sensitive subsection names on writing
authorStefan Beller <sbeller@google.com>
Wed, 8 Aug 2018 19:50:19 +0000 (12:50 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 8 Aug 2018 20:26:48 +0000 (13:26 -0700)
A user reported a submodule issue regarding a section mix-up,
but it could be boiled down to the following test case:

$ git init test && cd test
$ git config foo."Bar".key test
$ git config foo."bar".key test
$ tail -n 3 .git/config
[foo "Bar"]
key = test
key = test

Sub sections are case sensitive and we have a test for correctly reading
them. However we do not have a test for writing out config correctly with
case sensitive subsection names, which is why this went unnoticed in
6ae996f2acf (git_config_set: make use of the config parser's event
stream, 2018-04-09)

Unfortunately we have to make a distinction between old style configuration
that looks like

[foo.Bar]
key = test

and the new quoted style as seen above. The old style is documented as
case-agnostic, hence we need to keep 'strncasecmp'; although the
resulting setting for the old style config differs from the configuration.
That will be fixed in a follow up patch.

Reported-by: JP Sugarbroad <jpsugar@google.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c
t/t1300-config.sh
index 0205052978825bc77a91823ea24014ce45f3fabb..27e800c7ce8b343923433e9d1f6f96ff12ad4065 100644 (file)
--- a/config.c
+++ b/config.c
@@ -35,6 +35,7 @@ struct config_source {
        int eof;
        struct strbuf value;
        struct strbuf var;
+       unsigned subsection_case_sensitive : 1;
 
        int (*do_fgetc)(struct config_source *c);
        int (*do_ungetc)(int c, struct config_source *conf);
@@ -603,6 +604,7 @@ static int get_value(config_fn_t fn, void *data, struct strbuf *name)
 
 static int get_extended_base_var(struct strbuf *name, int c)
 {
+       cf->subsection_case_sensitive = 0;
        do {
                if (c == '\n')
                        goto error_incomplete_line;
@@ -639,6 +641,7 @@ static int get_extended_base_var(struct strbuf *name, int c)
 
 static int get_base_var(struct strbuf *name)
 {
+       cf->subsection_case_sensitive = 1;
        for (;;) {
                int c = get_next_char();
                if (cf->eof)
@@ -2328,14 +2331,21 @@ static int store_aux_event(enum config_event_t type,
        store->parsed[store->parsed_nr].type = type;
 
        if (type == CONFIG_EVENT_SECTION) {
+               int (*cmpfn)(const char *, const char *, size_t);
+
                if (cf->var.len < 2 || cf->var.buf[cf->var.len - 1] != '.')
                        return error("invalid section name '%s'", cf->var.buf);
 
+               if (cf->subsection_case_sensitive)
+                       cmpfn = strncasecmp;
+               else
+                       cmpfn = strncmp;
+
                /* Is this the section we were looking for? */
                store->is_keys_section =
                        store->parsed[store->parsed_nr].is_keys_section =
                        cf->var.len - 1 == store->baselen &&
-                       !strncasecmp(cf->var.buf, store->key, store->baselen);
+                       !cmpfn(cf->var.buf, store->key, store->baselen);
                if (store->is_keys_section) {
                        store->section_seen = 1;
                        ALLOC_GROW(store->seen, store->seen_nr + 1,
index c15c19bf78d441b2a01b54f1bf93f307a466960c..77c5899d0002dcdca04611974271bf49e35e08b8 100755 (executable)
@@ -1260,6 +1260,7 @@ test_expect_success 'setting different case sensitive subsections ' '
                Qc = v2
                [d "e"]
                f = v1
+               [d "E"]
                Qf = v2
        EOF
        # exact match