]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: restructure format_config()
authorDerrick Stolee <stolee@gmail.com>
Mon, 23 Feb 2026 12:26:54 +0000 (12:26 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Feb 2026 21:23:41 +0000 (13:23 -0800)
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>
builtin/config.c

index 45304076dc47267a99b6472558155c8809462f77..2e8bc6590cbb1faeddbe9a8cc502c0840318cf61 100644 (file)
@@ -124,6 +124,7 @@ struct config_display_options {
        .key_delim = ' ', \
 }
 
+#define TYPE_NONE              0
 #define TYPE_BOOL              1
 #define TYPE_INT               2
 #define TYPE_BOOL_OR_INT       3
@@ -390,32 +391,57 @@ static int format_config(const struct config_display_options *opts,
                show_config_origin(opts, kvi, buf);
        if (opts->show_keys)
                strbuf_addstr(buf, key_);
-       if (!opts->omit_values) {
-               if (opts->show_keys)
-                       strbuf_addch(buf, opts->key_delim);
-
-               if (opts->type == TYPE_INT)
-                       res = format_config_int64(buf, key_, value_, kvi, gently);
-               else if (opts->type == TYPE_BOOL)
-                       res = format_config_bool(buf, key_, value_, gently);
-               else if (opts->type == TYPE_BOOL_OR_INT)
-                       res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
-               else if (opts->type == TYPE_BOOL_OR_STR)
-                       res = format_config_bool_or_str(buf, value_);
-               else if (opts->type == TYPE_PATH)
-                       res = format_config_path(buf, key_, value_, gently);
-               else if (opts->type == TYPE_EXPIRY_DATE)
-                       res = format_config_expiry_date(buf, key_, value_, gently);
-               else if (opts->type == TYPE_COLOR)
-                       res = format_config_color(buf, key_, value_, gently);
-               else if (value_) {
+
+       if (opts->omit_values)
+               goto terminator;
+
+       if (opts->show_keys)
+               strbuf_addch(buf, opts->key_delim);
+
+       switch (opts->type) {
+       case TYPE_INT:
+               res = format_config_int64(buf, key_, value_, kvi, gently);
+               break;
+
+       case TYPE_BOOL:
+               res = format_config_bool(buf, key_, value_, gently);
+               break;
+
+       case TYPE_BOOL_OR_INT:
+               res = format_config_bool_or_int(buf, key_, value_, kvi, gently);
+               break;
+
+       case TYPE_BOOL_OR_STR:
+               res = format_config_bool_or_str(buf, value_);
+               break;
+
+       case TYPE_PATH:
+               res = format_config_path(buf, key_, value_, gently);
+               break;
+
+       case TYPE_EXPIRY_DATE:
+               res = format_config_expiry_date(buf, key_, value_, gently);
+               break;
+
+       case TYPE_COLOR:
+               res = format_config_color(buf, key_, value_, gently);
+               break;
+
+       case TYPE_NONE:
+               if (value_) {
                        strbuf_addstr(buf, value_);
                } else {
                        /* Just show the key name; back out delimiter */
                        if (opts->show_keys)
                                strbuf_setlen(buf, buf->len - 1);
                }
+               break;
+
+       default:
+               BUG("undefined type %d", opts->type);
        }
+
+terminator:
        strbuf_addch(buf, opts->term);
        return res;
 }