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>

Derrick Stolee committed Feb 23, 2026 at 12:26 UTC 53959a8ba22d80f78daa693dfc2f76fd3afe80e2
2 files changed +22 -3
builtin/config.c
+19 -2
@@ -256,6 +256,24 @@ static int format_config_int64(struct strbuf *buf,
256 return 0;
257 }
258
259 +static int format_config_bool(struct strbuf *buf,
260 + const char *key_,
261 + const char *value_,
262 + int gently)
263 +{
264 + int v = 0;
265 + if (gently) {
266 + if ((v = git_parse_maybe_bool(value_)) < 0)
267 + return -1;
268 + } else {
269 + /* may die() */
270 + v = git_config_bool(key_, value_);
271 + }
272 +
273 + strbuf_addstr(buf, v ? "true" : "false");
274 + return 0;
275 +}
276 +
277 /*
278 * Format the configuration key-value pair (`key_`, `value_`) and
279 * 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,
302 if (opts->type == TYPE_INT)
303 res = format_config_int64(buf, key_, value_, kvi, gently);
304 else if (opts->type == TYPE_BOOL)
287 - strbuf_addstr(buf, git_config_bool(key_, value_) ?
288 - "true" : "false");
305 + res = format_config_bool(buf, key_, value_, gently);
306 else if (opts->type == TYPE_BOOL_OR_INT) {
307 int is_bool, v;
308 v = git_config_bool_or_int(key_, value_, kvi,
t/t1300-config.sh
+3 -1
@@ -2527,7 +2527,9 @@ test_expect_success 'list --type=bool shows only canonicalizable bool values' '
2527 section.big=true
2528 EOF
2529
2530 - test_must_fail git config ${mode_prefix}list --type=bool
2530 + git config ${mode_prefix}list --type=bool >actual 2>err &&
2531 + test_cmp expect actual &&
2532 + test_must_be_empty err
2533 '
2534
2535 test_expect_success 'list --type=bool-or-int shows only canonicalizable values' '