parse_config_key: allow matching single-level config
authorJeff King <peff@peff.net>
Fri, 24 Feb 2017 21:08:02 +0000 (16:08 -0500)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 Feb 2017 21:22:11 +0000 (13:22 -0800)
The parse_config_key() function was introduced to make it
easier to match "section.subsection.key" variables. It also
handles the simpler "section.key", and the caller is
responsible for distinguishing the two from its
out-parameters.

Most callers who _only_ want "section.key" would just use a
strcmp(var, "section.key"), since there is no parsing
required. However, they may still use parse_config_key() if
their "section" variable isn't a constant (an example of
this is in parse_hide_refs_config).

Using the parse_config_key is a bit clunky, though:

const char *subsection;
int subsection_len;
const char *key;

if (!parse_config_key(var, section, &subsection, &subsection_len, &key) &&
!subsection) {
/* matched! */
}

Instead, let's treat a NULL subsection as an indication that
the caller does not expect one. That lets us write:

const char *key;

if (!parse_config_key(var, section, NULL, NULL, &key)) {
/* matched! */
}

Existing callers should be unaffected, as passing a NULL
subsection would currently segfault.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
config.c
diff --git a/cache.h b/cache.h
index 5c035dae57c4dfe710802021c4b4af6b6cb1ad8b..e7b0e6be951369c4c46a6b6ad3d33f72345d0f8d 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1790,8 +1790,11 @@ extern int git_config_include(const char *name, const char *value, void *data);
  *
  * (i.e., what gets handed to a config_fn_t). The caller provides the section;
  * we return -1 if it does not match, 0 otherwise. The subsection and key
- * out-parameters are filled by the function (and subsection is NULL if it is
+ * out-parameters are filled by the function (and *subsection is NULL if it is
  * missing).
+ *
+ * If the subsection pointer-to-pointer passed in is NULL, returns 0 only if
+ * there is no subsection at all.
  */
 extern int parse_config_key(const char *var,
                            const char *section,
index a23f260792f14be45cc8c50df358b49e9136c671..fd92738fbaf163c1e9019dfc05b5c41e9f079cc9 100644 (file)
--- a/config.c
+++ b/config.c
@@ -2547,10 +2547,14 @@ int parse_config_key(const char *var,
 
        /* Did we have a subsection at all? */
        if (dot == var) {
-               *subsection = NULL;
-               *subsection_len = 0;
+               if (subsection) {
+                       *subsection = NULL;
+                       *subsection_len = 0;
+               }
        }
        else {
+               if (!subsection)
+                       return -1;
                *subsection = var + 1;
                *subsection_len = dot - *subsection;
        }