Skip to content

Commit 48fc882

Browse files
committed
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>
1 parent d14937e commit 48fc882

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

builtin/config.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,32 +393,53 @@ static int format_config(const struct config_display_options *opts,
393393
show_config_origin(opts, kvi, buf);
394394
if (opts->show_keys)
395395
strbuf_addstr(buf, key_);
396-
if (!opts->omit_values) {
397-
if (opts->show_keys)
398-
strbuf_addch(buf, opts->key_delim);
399-
400-
if (opts->type == TYPE_INT)
401-
res = format_config_int64(buf, key_, value_, kvi, gently);
402-
else if (opts->type == TYPE_BOOL)
403-
res = format_config_bool(buf, key_, value_, gently);
404-
else if (opts->type == TYPE_BOOL_OR_INT)
405-
res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
406-
else if (opts->type == TYPE_BOOL_OR_STR)
407-
res = format_config_bool_or_str(buf, value_);
408-
else if (opts->type == TYPE_PATH)
409-
res = format_config_path(buf, key_, value_, gently);
410-
else if (opts->type == TYPE_EXPIRY_DATE)
411-
res = format_config_expiry_date(buf, key_, value_, gently);
412-
else if (opts->type == TYPE_COLOR)
413-
res = format_config_color(buf, key_, value_, gently);
414-
else if (value_) {
396+
397+
if (opts->omit_values)
398+
goto terminator;
399+
400+
if (opts->show_keys)
401+
strbuf_addch(buf, opts->key_delim);
402+
403+
switch (opts->type) {
404+
case TYPE_INT:
405+
res = format_config_int64(buf, key_, value_, kvi, gently);
406+
break;
407+
408+
case TYPE_BOOL:
409+
res = format_config_bool(buf, key_, value_, gently);
410+
break;
411+
412+
case TYPE_BOOL_OR_INT:
413+
res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
414+
break;
415+
416+
case TYPE_BOOL_OR_STR:
417+
res = format_config_bool_or_str(buf, value_);
418+
break;
419+
420+
case TYPE_PATH:
421+
res = format_config_path(buf, key_, value_, gently);
422+
break;
423+
424+
case TYPE_EXPIRY_DATE:
425+
res = format_config_expiry_date(buf, key_, value_, gently);
426+
break;
427+
428+
case TYPE_COLOR:
429+
res = format_config_color(buf, key_, value_, gently);
430+
break;
431+
432+
default:
433+
if (value_) {
415434
strbuf_addstr(buf, value_);
416435
} else {
417436
/* Just show the key name; back out delimiter */
418437
if (opts->show_keys)
419438
strbuf_setlen(buf, buf->len - 1);
420439
}
421440
}
441+
442+
terminator:
422443
strbuf_addch(buf, opts->term);
423444
return res;
424445
}

0 commit comments

Comments
 (0)