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

[PATCH v6 1/2] config: add git_config_key_is_valid() for quiet validation

From
Harald Nordgren via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 2, 2026, 18:43 UTC
Message-ID
<7400ca41bbacab40c28f21f63327a9808e568e05.1780425808.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2302.v6.git.git.1780425808.gitgitgadget@gmail.com>
From: Harald Nordgren <haraldnordgren@gmail.com>

Move the body of git_config_parse_key() into a static helper do_parse_config_key() that takes a "quiet" flag and treats store_key as optional. git_config_parse_key() becomes a thin wrapper.

Add git_config_key_is_valid() for callers that only need to know whether a key is well-formed.

Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
 config.c | 38 +++++++++++++++++++++++++++++---------
 config.h |  2 ++
 2 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/config.c b/config.c
index a1b92fe083..45144f73c5 100644
--- a/config.c
+++ b/config.c
@@ -536,11 +536,14 @@ static inline int iskeychar(int c)
  * -2 if there is no section name in the key.
  *
  * store_key - pointer to char* which will hold a copy of the key with
- *             lowercase section and variable name
+ *             lowercase section and variable name, can be NULL to skip
+ *             allocation when only validation is needed
  * baselen - pointer to size_t which will hold the length of the
  *           section + subsection part, can be NULL
+ * quiet - when non-zero, suppress error() reports on rejection
  */
-int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
+static int do_parse_config_key(const char *key, char **store_key,
+			       size_t *baselen_, int quiet)
 {
 	size_t i, baselen;
 	int dot;
@@ -552,12 +555,14 @@ int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
 	 */
 
 	if (last_dot == NULL || last_dot == key) {
-		error(_("key does not contain a section: %s"), key);
+		if (!quiet)
+			error(_("key does not contain a section: %s"), key);
 		return -CONFIG_NO_SECTION_OR_NAME;
 	}
 
 	if (!last_dot[1]) {
-		error(_("key does not contain variable name: %s"), key);
+		if (!quiet)
+			error(_("key does not contain variable name: %s"), key);
 		return -CONFIG_NO_SECTION_OR_NAME;
 	}
 
@@ -568,7 +573,8 @@ int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
 	/*
 	 * Validate the key and while at it, lower case it for matching.
 	 */
-	*store_key = xmallocz(strlen(key));
+	if (store_key)
+		*store_key = xmallocz(strlen(key));
 
 	dot = 0;
 	for (i = 0; key[i]; i++) {
@@ -579,24 +585,38 @@ int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
 		if (!dot || i > baselen) {
 			if (!iskeychar(c) ||
 			    (i == baselen + 1 && !isalpha(c))) {
-				error(_("invalid key: %s"), key);
+				if (!quiet)
+					error(_("invalid key: %s"), key);
 				goto out_free_ret_1;
 			}
 			c = tolower(c);
 		} else if (c == '\n') {
-			error(_("invalid key (newline): %s"), key);
+			if (!quiet)
+				error(_("invalid key (newline): %s"), key);
 			goto out_free_ret_1;
 		}
-		(*store_key)[i] = c;
+		if (store_key)
+			(*store_key)[i] = c;
 	}
 
 	return 0;
 
 out_free_ret_1:
-	FREE_AND_NULL(*store_key);
+	if (store_key)
+		FREE_AND_NULL(*store_key);
 	return -CONFIG_INVALID_KEY;
 }
 
+int git_config_parse_key(const char *key, char **store_key, size_t *baselen_)
+{
+	return do_parse_config_key(key, store_key, baselen_, 0);
+}
+
+int git_config_key_is_valid(const char *key)
+{
+	return !do_parse_config_key(key, NULL, NULL, 1);
+}
+
 static int config_parse_pair(const char *key, const char *value,
 			     struct key_value_info *kvi,
 			     config_fn_t fn, void *data)
diff --git a/config.h b/config.h
index bf47fb3afc..31fe3e2961 100644
--- a/config.h
+++ b/config.h
@@ -343,6 +343,8 @@ void repo_config_set(struct repository *, const char *, const char *);
 
 int git_config_parse_key(const char *, char **, size_t *);
 
+int git_config_key_is_valid(const char *);
+
 /*
  * The following macros specify flag bits that alter the behavior
  * of the repo_config_set_multivar*() methods.
-- 
gitgitgadget
Previous: Harald Nordgren via GitGitGadgetNext: Harald Nordgren via GitGitGadget
Message 24 of 26 in “config: suggest the correct form when key contains "="”
  1. config: suggest the correct form when key contains "="Harald Nordgren via GitGitGadget, May 13, 2026
  2. Junio C HamanoMay 14, 2026
  3. fetch: add fetch.pruneLocalBranches configHarald Nordgren, May 14, 2026
  4. Junio C HamanoMay 15, 2026
  5. Email issuesHarald Nordgren, May 15, 2026
  6. Kristoffer HaugsbakkMay 15, 2026
  7. Harald NordgrenMay 15, 2026
  8. Harald NordgrenMay 16, 2026
  9. config: suggest the correct form when key contains "=" in set contextHarald Nordgren via GitGitGadget, May 16, 2026
  10. config: suggest the correct form when key contains "=" in set contextHarald Nordgren via GitGitGadget, May 25, 2026
  11. Junio C HamanoMay 25, 2026
  12. config: improve diagnostic for "set" with missing valueHarald Nordgren via GitGitGadget, May 26, 2026
  13. Harald NordgrenMay 26, 2026
  14. Junio C HamanoJun 1, 2026
  15. Junio C HamanoJun 1, 2026
  16. 0/2 config: suggest the correct form when key contains "="Harald Nordgren via GitGitGadget, Jun 2, 2026
  17. 1/2 config: let git_config_parse_key() validate quietlyHarald Nordgren via GitGitGadget, Jun 2, 2026
  18. Junio C HamanoJun 2, 2026
  19. Harald NordgrenJun 2, 2026
  20. Junio C HamanoJun 4, 2026
  21. 2/2 config: improve diagnostic for "set" with missing valueHarald Nordgren via GitGitGadget, Jun 2, 2026
  22. Junio C HamanoJun 2, 2026
  23. 0/2 config: suggest the correct form when key contains "="Harald Nordgren via GitGitGadget, Jun 2, 2026
  24. 1/2 config: add git_config_key_is_valid() for quiet validationHarald Nordgren via GitGitGadget, Jun 2, 2026
  25. 2/2 config: improve diagnostic for "set" with missing valueHarald Nordgren via GitGitGadget, Jun 2, 2026
  26. Junio C HamanoJun 4, 2026

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.