git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] parse_config_key: allow matching single-level config

From
Jeff King <peff@peff.net>
Date
Feb 24, 2017, 21:08 UTC
Message-ID
<20170224210802.rpr5vdpqhsp3pt5v@sigill.intra.peff.net>
In-Reply-To
<20170224210643.max6z2ykm3gbg7lw@sigill.intra.peff.net>

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>
---
 cache.h  | 5 ++++-
 config.c | 8 ++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/cache.h b/cache.h
index 61fc86e6d..647a78f3f 100644
--- a/cache.h
+++ b/cache.h
@@ -1819,8 +1819,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,
diff --git a/config.c b/config.c
index 1b08a75a7..13c8b21ea 100644
--- a/config.c
+++ b/config.c
@@ -2552,10 +2552,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;
 	}
-- 
2.12.0.616.g5f622f3b1
Previous: Jeff KingNext: Junio C Hamano
Message 8 of 16 in “refs: parse_hide_refs_config to use parse_config_key”
  1. refs: parse_hide_refs_config to use parse_config_keyStefan Beller, Feb 24, 2017
  2. Jeff KingFeb 24, 2017
  3. refs: parse_hide_refs_config to use parse_config_keyStefan Beller, Feb 24, 2017
  4. Jeff KingFeb 24, 2017
  5. Junio C HamanoFeb 24, 2017
  6. Jeff KingFeb 24, 2017
  7. 1/3 parse_config_key: use skip_prefix instead of starts_withJeff King, Feb 24, 2017
  8. 2/3 parse_config_key: allow matching single-level configJeff King, Feb 24, 2017
  9. Junio C HamanoFeb 24, 2017
  10. Junio C HamanoFeb 24, 2017
  11. Jeff KingFeb 24, 2017
  12. 3/3 parse_hide_refs_config: tell parse_config_key we don't want a subsectionJeff King, Feb 24, 2017
  13. Stefan BellerFeb 24, 2017
  14. Junio C HamanoFeb 24, 2017
  15. Jeff KingFeb 24, 2017
  16. Junio C HamanoFeb 24, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.