diff options
author | Jeff King <peff@peff.net> | 2017-02-24 16:08:02 -0500 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-02-24 13:22:11 -0800 |
commit | 48f8d9f7323d0927cd955f129babbf93eda50473 (patch) | |
tree | 3cf0d91d83bebacdd976e987e39c60f9833ff0b3 /config.c | |
parent | e3394fdce79411fd51e20082c0faf7061007bc1c (diff) | |
download | git-48f8d9f7323d0927cd955f129babbf93eda50473.tar.gz |
parse_config_key: allow matching single-level config
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>
Diffstat (limited to 'config.c')
-rw-r--r-- | config.c | 8 |
1 files changed, 6 insertions, 2 deletions
@@ -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; } |