aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerrick Stolee <stolee@gmail.com>2026-02-23 12:26:47 +0000
committerJunio C Hamano <gitster@pobox.com>2026-02-23 13:23:40 -0800
commit53959a8ba22d80f78daa693dfc2f76fd3afe80e2 (patch)
tree3a0c49f9ba13cbf6646f9f920e039d7c6e46f64b
parentd744923fefb294c835d18883bac62f85ff55fc9f (diff)
downloadgit-53959a8ba22d80f78daa693dfc2f76fd3afe80e2.tar.xz
config: format bools gently
Move the logic for formatting bool config values into a helper method and use gentle parsing when needed. This makes 'git config list --type=bool' not fail when coming across a non-boolean value. Such unparseable values are filtered out quietly. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--builtin/config.c21
-rwxr-xr-xt/t1300-config.sh4
2 files changed, 22 insertions, 3 deletions
diff --git a/builtin/config.c b/builtin/config.c
index 448b148563..d8b38c51d3 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -256,6 +256,24 @@ static int format_config_int64(struct strbuf *buf,
return 0;
}
+static int format_config_bool(struct strbuf *buf,
+ const char *key_,
+ const char *value_,
+ int gently)
+{
+ int v = 0;
+ if (gently) {
+ if ((v = git_parse_maybe_bool(value_)) < 0)
+ return -1;
+ } else {
+ /* may die() */
+ v = git_config_bool(key_, value_);
+ }
+
+ strbuf_addstr(buf, v ? "true" : "false");
+ return 0;
+}
+
/*
* Format the configuration key-value pair (`key_`, `value_`) and
* append it into strbuf `buf`. Returns a negative value on failure,
@@ -284,8 +302,7 @@ static int format_config(const struct config_display_options *opts,
if (opts->type == TYPE_INT)
res = format_config_int64(buf, key_, value_, kvi, gently);
else if (opts->type == TYPE_BOOL)
- strbuf_addstr(buf, git_config_bool(key_, value_) ?
- "true" : "false");
+ res = format_config_bool(buf, key_, value_, gently);
else if (opts->type == TYPE_BOOL_OR_INT) {
int is_bool, v;
v = git_config_bool_or_int(key_, value_, kvi,
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 05a812fd6d..568cfaa3c5 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -2527,7 +2527,9 @@ test_expect_success 'list --type=bool shows only canonicalizable bool values' '
section.big=true
EOF
- test_must_fail git config ${mode_prefix}list --type=bool
+ git config ${mode_prefix}list --type=bool >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
'
test_expect_success 'list --type=bool-or-int shows only canonicalizable values' '