config: format bools or ints gently
Move the logic for formatting bool-or-int config values into a helper method and use gentle parsing when needed. 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
5fb7bdcca98e63fedee22f16a34ab5fadbee54e0
2 files changed
+34
-10
builtin/config.c
+31
-9
@@ -274,6 +274,34 @@ static int format_config_bool(struct strbuf *buf,
274
return 0;
275
}
276
277
+static int format_config_bool_or_int(struct strbuf *buf,
278
+ const char *key_,
279
+ const char *value_,
280
+ const struct key_value_info *kvi,
281
+ int gently)
282
+{
283
+ int v, is_bool = 0;
284
+
285
+ if (gently) {
286
+ v = git_parse_maybe_bool_text(value_);
287
+
288
+ if (v >= 0)
289
+ is_bool = 1;
290
+ else if (!git_parse_int(value_, &v))
291
+ return -1;
292
+ } else {
293
+ v = git_config_bool_or_int(key_, value_, kvi,
294
+ &is_bool);
295
+ }
296
+
297
+ if (is_bool)
298
+ strbuf_addstr(buf, v ? "true" : "false");
299
+ else
300
+ strbuf_addf(buf, "%d", v);
301
+
302
+ return 0;
303
+}
304
+
305
/*
306
* Format the configuration key-value pair (`key_`, `value_`) and
307
* append it into strbuf `buf`. Returns a negative value on failure,
@@ -303,15 +331,9 @@ static int format_config(const struct config_display_options *opts,
331
res = format_config_int64(buf, key_, value_, kvi, gently);
332
else if (opts->type == TYPE_BOOL)
333
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,
309
- &is_bool);
310
- if (is_bool)
311
- strbuf_addstr(buf, v ? "true" : "false");
312
- else
313
- strbuf_addf(buf, "%d", v);
314
- } else if (opts->type == TYPE_BOOL_OR_STR) {
334
+ else if (opts->type == TYPE_BOOL_OR_INT)
335
+ res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
336
+ else if (opts->type == TYPE_BOOL_OR_STR) {
337
int v = git_parse_maybe_bool(value_);
338
if (v < 0)
339
strbuf_addstr(buf, value_);
t/t1300-config.sh
+3
-1
@@ -2539,7 +2539,9 @@ test_expect_success 'list --type=bool-or-int shows only canonicalizable values'
2539
section.big=1048576
2540
EOF
2541
2542
- test_must_fail git config ${mode_prefix}list --type=bool-or-int
2542
+ git config ${mode_prefix}list --type=bool-or-int >actual 2>err &&
2543
+ test_cmp expect actual &&
2544
+ test_must_be_empty err
2545
'
2546
2547
test_expect_success 'list --type=path shows only canonicalizable path values' '