Skip to content

Commit 48f8d9f

Browse files
peffgitster
authored andcommitted
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>
1 parent e3394fd commit 48f8d9f

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

cache.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,8 +1790,11 @@ extern int git_config_include(const char *name, const char *value, void *data);
17901790
*
17911791
* (i.e., what gets handed to a config_fn_t). The caller provides the section;
17921792
* we return -1 if it does not match, 0 otherwise. The subsection and key
1793-
* out-parameters are filled by the function (and subsection is NULL if it is
1793+
* out-parameters are filled by the function (and *subsection is NULL if it is
17941794
* missing).
1795+
*
1796+
* If the subsection pointer-to-pointer passed in is NULL, returns 0 only if
1797+
* there is no subsection at all.
17951798
*/
17961799
extern int parse_config_key(const char *var,
17971800
const char *section,

config.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2547,10 +2547,14 @@ int parse_config_key(const char *var,
25472547

25482548
/* Did we have a subsection at all? */
25492549
if (dot == var) {
2550-
*subsection = NULL;
2551-
*subsection_len = 0;
2550+
if (subsection) {
2551+
*subsection = NULL;
2552+
*subsection_len = 0;
2553+
}
25522554
}
25532555
else {
2556+
if (!subsection)
2557+
return -1;
25542558
*subsection = var + 1;
25552559
*subsection_len = dot - *subsection;
25562560
}

0 commit comments

Comments
 (0)