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

[PATCH v2] config: preserve <subsection> case for one-shot config on the command line

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 21, 2017, 17:17 UTC
Message-ID
<xmqqd1ebh3ak.fsf_-_@gitster.mtv.corp.google.com>
In-Reply-To
<xmqqino3h3zv.fsf@gitster.mtv.corp.google.com>

The "git -c <var>=<val> cmd" mechanism is to pretend that a configuration variable <var> is set to <val> while the cmd is running. The code to do so however downcased <var> in its entirety, which is wrong for a three-level <section>.<subsection>.<variable>.

The <subsection> part needs to stay as-is.
Reported-by: Lars Schneider <larsxschneider@gmail.com>
Diagnosed-by: Jonathan Tan <jonathantanmy@google.com>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Changes from v1:
   - update the comment before the second loop to find the last
     dot.
   - move two new tests into existing t1300 (at least for now).
   - make it clear that the second of the new tests check two
     aspects of "three level vars" by setting up the expectation for
     the first half near the actual tests and adding comments on
     what it tests before the second half.
 config.c               | 30 +++++++++++++++++++++++++++++-
 t/t1300-repo-config.sh | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 1 deletion(-)
diff --git a/config.c b/config.c
index 0dfed682b8..4128debc71 100644
--- a/config.c
+++ b/config.c
@@ -199,6 +199,34 @@ void git_config_push_parameter(const char *text)
 	strbuf_release(&env);
 }
 
+/*
+ * downcase the <section> and <variable> in <section>.<variable> or
+ * <section>.<subsection>.<variable> and do so in place.  <subsection>
+ * is left intact.
+ */
+static void canonicalize_config_variable_name(char *varname)
+{
+	char *cp, *last_dot;
+
+	/* downcase the first segment */
+	for (cp = varname; *cp; cp++) {
+		if (*cp == '.')
+			break;
+		*cp = tolower(*cp);
+	}
+	if (!*cp)
+		return;
+
+	/* find the last dot (we start from the first dot we just found) */
+	for (last_dot = cp; *cp; cp++)
+		if (*cp == '.')
+			last_dot = cp;
+
+	/* downcase the last segment */
+	for (cp = last_dot; *cp; cp++)
+		*cp = tolower(*cp);
+}
+
 int git_config_parse_parameter(const char *text,
 			       config_fn_t fn, void *data)
 {
@@ -221,7 +249,7 @@ int git_config_parse_parameter(const char *text,
 		strbuf_list_free(pair);
 		return error("bogus config parameter: %s", text);
 	}
-	strbuf_tolower(pair[0]);
+	canonicalize_config_variable_name(pair[0]->buf);
 	if (fn(pair[0]->buf, value, data) < 0) {
 		strbuf_list_free(pair);
 		return -1;
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 923bfc5a26..7a16f66a9d 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -1097,6 +1097,52 @@ test_expect_success 'multiple git -c appends config' '
 	test_cmp expect actual
 '
 
+test_expect_success 'last one wins: two level vars' '
+
+	# sec.var and sec.VAR are the same variable, as the first
+	# and the last level of a configuration variable name is
+	# case insensitive.
+
+	echo VAL >expect &&
+
+	git -c sec.var=val -c sec.VAR=VAL config --get sec.var >actual &&
+	test_cmp expect actual &&
+	git -c SEC.var=val -c sec.var=VAL config --get sec.var >actual &&
+	test_cmp expect actual &&
+
+	git -c sec.var=val -c sec.VAR=VAL config --get SEC.var >actual &&
+	test_cmp expect actual &&
+	git -c SEC.var=val -c sec.var=VAL config --get sec.VAR >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'last one wins: three level vars' '
+
+	# v.a.r and v.A.r are not the same variable, as the middle
+	# level of a three-level configuration variable name is
+	# case sensitive.
+
+	echo val >expect &&
+	git -c v.a.r=val -c v.A.r=VAL config --get v.a.r >actual &&
+	test_cmp expect actual &&
+	git -c v.a.r=val -c v.A.r=VAL config --get V.a.R >actual &&
+	test_cmp expect actual &&
+
+	# v.a.r and V.a.R are the same variable, as the first
+	# and the last level of a configuration variable name is
+	# case insensitive.
+
+	echo VAL >expect &&
+	git -c v.a.r=val -c v.a.R=VAL config --get v.a.r >actual &&
+	test_cmp expect actual &&
+	git -c v.a.r=val -c V.a.r=VAL config --get v.a.r >actual &&
+	test_cmp expect actual &&
+	git -c v.a.r=val -c v.a.R=VAL config --get V.a.R >actual &&
+	test_cmp expect actual &&
+	git -c v.a.r=val -c V.a.r=VAL config --get V.a.R >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'git -c is not confused by empty environment' '
 	GIT_CONFIG_PARAMETERS="" git -c x.one=1 config --list
 '
-- 
2.12.0-rc2-221-ga92f6f5816
Previous: Junio C HamanoNext: Jeff King
Message 26 of 41 in “[BUG] submodule config does not apply to upper case submodules?”
  1. Lars SchneiderFeb 15, 2017
  2. t7400: cleanup "submodule add clone shallow submodule" testLars Schneider, Feb 15, 2017
  3. Stefan BellerFeb 15, 2017
  4. Junio C HamanoFeb 15, 2017
  5. Stefan BellerFeb 15, 2017
  6. Junio C HamanoFeb 15, 2017
  7. Jonathan TanFeb 15, 2017
  8. Junio C HamanoFeb 15, 2017
  9. Junio C HamanoFeb 15, 2017
  10. Stefan BellerFeb 15, 2017
  11. Junio C HamanoFeb 15, 2017
  12. Stefan BellerFeb 15, 2017
  13. Junio C HamanoFeb 15, 2017
  14. Jeff KingFeb 16, 2017
  15. Jonathan TanFeb 15, 2017
  16. Junio C HamanoFeb 16, 2017
  17. config: preserve <subsection> case for one-shot config on the command lineJunio C Hamano, Feb 15, 2017
  18. Lars SchneiderFeb 16, 2017
  19. Junio C HamanoFeb 16, 2017
  20. Jeff KingFeb 16, 2017
  21. Junio C HamanoFeb 17, 2017
  22. Junio C HamanoFeb 20, 2017
  23. Lars SchneiderFeb 20, 2017
  24. Jeff KingFeb 21, 2017
  25. Junio C HamanoFeb 21, 2017
  26. config: preserve <subsection> case for one-shot config on the command lineJunio C Hamano, Feb 21, 2017
  27. Jeff KingFeb 21, 2017
  28. Stefan BellerFeb 21, 2017
  29. config: reject invalid VAR in 'git -c VAR=VAL command'Junio C Hamano, Feb 21, 2017
  30. Stefan BellerFeb 21, 2017
  31. Junio C HamanoFeb 21, 2017
  32. config: reject invalid VAR in 'git -c VAR=VAL command'Junio C Hamano, Feb 21, 2017
  33. Junio C HamanoFeb 22, 2017
  34. Jeff KingFeb 23, 2017
  35. Junio C HamanoFeb 23, 2017
  36. Junio C HamanoFeb 23, 2017
  37. Jeff KingFeb 24, 2017
  38. Junio C HamanoFeb 24, 2017
  39. Jeff KingFeb 24, 2017
  40. Junio C HamanoFeb 24, 2017
  41. Jeff KingFeb 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.