config: restructure format_config()

The recent changes have replaced the bodies of most if/else-if cases with simple helper method calls. This makes it easy to adapt the structure into a clearer switch statement, leaving a simple if/else in the default case. Make things a little simpler to read by reducing the nesting depth via a new goto statement when we want to skip values. 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 645f92a3e9179ebf1ed42dc4fa05cc8dd71e3e9c
1 file changed +45 -19
builtin/config.c
+45 -19
@@ -124,6 +124,7 @@ struct config_display_options {
124 .key_delim = ' ', \
125 }
126
127 +#define TYPE_NONE 0
128 #define TYPE_BOOL 1
129 #define TYPE_INT 2
130 #define TYPE_BOOL_OR_INT 3
@@ -390,32 +391,57 @@ static int format_config(const struct config_display_options *opts,
391 show_config_origin(opts, kvi, buf);
392 if (opts->show_keys)
393 strbuf_addstr(buf, key_);
393 - if (!opts->omit_values) {
394 - if (opts->show_keys)
395 - strbuf_addch(buf, opts->key_delim);
396 -
397 - if (opts->type == TYPE_INT)
398 - res = format_config_int64(buf, key_, value_, kvi, gently);
399 - else if (opts->type == TYPE_BOOL)
400 - res = format_config_bool(buf, key_, value_, gently);
401 - else if (opts->type == TYPE_BOOL_OR_INT)
402 - res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
403 - else if (opts->type == TYPE_BOOL_OR_STR)
404 - res = format_config_bool_or_str(buf, value_);
405 - else if (opts->type == TYPE_PATH)
406 - res = format_config_path(buf, key_, value_, gently);
407 - else if (opts->type == TYPE_EXPIRY_DATE)
408 - res = format_config_expiry_date(buf, key_, value_, gently);
409 - else if (opts->type == TYPE_COLOR)
410 - res = format_config_color(buf, key_, value_, gently);
411 - else if (value_) {
394 +
395 + if (opts->omit_values)
396 + goto terminator;
397 +
398 + if (opts->show_keys)
399 + strbuf_addch(buf, opts->key_delim);
400 +
401 + switch (opts->type) {
402 + case TYPE_INT:
403 + res = format_config_int64(buf, key_, value_, kvi, gently);
404 + break;
405 +
406 + case TYPE_BOOL:
407 + res = format_config_bool(buf, key_, value_, gently);
408 + break;
409 +
410 + case TYPE_BOOL_OR_INT:
411 + res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
412 + break;
413 +
414 + case TYPE_BOOL_OR_STR:
415 + res = format_config_bool_or_str(buf, value_);
416 + break;
417 +
418 + case TYPE_PATH:
419 + res = format_config_path(buf, key_, value_, gently);
420 + break;
421 +
422 + case TYPE_EXPIRY_DATE:
423 + res = format_config_expiry_date(buf, key_, value_, gently);
424 + break;
425 +
426 + case TYPE_COLOR:
427 + res = format_config_color(buf, key_, value_, gently);
428 + break;
429 +
430 + case TYPE_NONE:
431 + if (value_) {
432 strbuf_addstr(buf, value_);
433 } else {
434 /* Just show the key name; back out delimiter */
435 if (opts->show_keys)
436 strbuf_setlen(buf, buf->len - 1);
437 }
438 + break;
439 +
440 + default:
441 + BUG("undefined type %d", opts->type);
442 }
443 +
444 +terminator:
445 strbuf_addch(buf, opts->term);
446 return res;
447 }