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

[PATCH 1/2] log.decorate: accept 0/1 bool values

From
Jeff King <peff@peff.net>
Date
Nov 17, 2010, 17:00 UTC
Message-ID
<20101117170045.GA4108@sigill.intra.peff.net>

We explicitly document "0" and "1" as synonyms for "false" and "true" in boolean config options. However, we don't actually handle those values in git_config_maybe_bool.

In most cases this works fine, as we call git_config_bool, which in turn calls git_config_bool_or_int, which in turn calls git_config_maybe_bool. Values of 0/1 are considered "not bool", but their integer values end up being converted to the corresponding boolean values.

However, the log.decorate code looks for maybe_bool explicitly, so that it can fall back to the "short" and "full" strings. It does not handle 0/1 at all, and considers them invalid values.

We cannot simply add 0/1 support to git_config_maybe_bool. That would confuse git_config_bool_or_int, which may want to distinguish the integer values "0" and "1" from bools.

Signed-off-by: Jeff King <peff@peff.net>
---
I don't know that anyone explicitly cares about distinguishing "0" from
"false", but we do expose this interface via "git config --bool-or-int",
and we test for it explicitly in t1300. So I kept the existing behavior
intact in that instance.

Note that another way to solve this would be to check explicitly for "short" or "full" and then just call git_config_bool itself. That works in this case, but not for an open-ended case (i.e., where you can take either a bool, or any arbitrary string). And I needed that case for my patch 2/2. :)

 config.c       |   16 ++++++++++++++--
 t/t4202-log.sh |    9 +++++++++
 2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/config.c b/config.c
index 4b0a820..9918b93 100644
--- a/config.c
+++ b/config.c
@@ -410,7 +410,7 @@ unsigned long git_config_ulong(const char *name, const char *value)
 	return ret;
 }
 
-int git_config_maybe_bool(const char *name, const char *value)
+static int git_config_maybe_bool_text(const char *name, const char *value)
 {
 	if (!value)
 		return 1;
@@ -427,9 +427,21 @@ int git_config_maybe_bool(const char *name, const char *value)
 	return -1;
 }
 
+int git_config_maybe_bool(const char *name, const char *value)
+{
+	int v = git_config_maybe_bool_text(name, value);
+	if (0 <= v)
+		return v;
+	if (!strcmp(value, "0"))
+		return 0;
+	if (!strcmp(value, "1"))
+		return 1;
+	return -1;
+}
+
 int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
 {
-	int v = git_config_maybe_bool(name, value);
+	int v = git_config_maybe_bool_text(name, value);
 	if (0 <= v) {
 		*is_bool = 1;
 		return v;
diff --git a/t/t4202-log.sh b/t/t4202-log.sh
index 2e51356..2043bb8 100755
--- a/t/t4202-log.sh
+++ b/t/t4202-log.sh
@@ -422,6 +422,15 @@ test_expect_success 'log.decorate configuration' '
 	test_cmp expect.full actual &&
 
 	git config --unset-all log.decorate &&
+	git config log.decorate 1 &&
+	git log --oneline >actual &&
+	test_cmp expect.short actual &&
+	git log --oneline --decorate=full >actual &&
+	test_cmp expect.full actual &&
+	git log --oneline --decorate=no >actual &&
+	test_cmp expect.none actual &&
+
+	git config --unset-all log.decorate &&
 	git config log.decorate short &&
 	git log --oneline >actual &&
 	test_cmp expect.short actual &&
-- 
1.7.3.2.362.g308e9
Next: Jeff King
Message 1 of 6 in “log.decorate: accept 0/1 bool values”
  1. 1/2 log.decorate: accept 0/1 bool valuesJeff King, Nov 17, 2010
  2. 2/2 allow command-specific pagers in pager.<cmd>Jeff King, Nov 17, 2010
  3. Junio C HamanoNov 17, 2010
  4. Jeff KingNov 17, 2010
  5. Junio C HamanoNov 18, 2010
  6. Jeff KingNov 18, 2010

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.