]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: format bools or ints gently
authorDerrick Stolee <stolee@gmail.com>
Mon, 23 Feb 2026 12:26:48 +0000 (12:26 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Feb 2026 21:23:41 +0000 (13:23 -0800)
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>
builtin/config.c
t/t1300-config.sh

index d8b38c51d3ae64ff6a588a323c510cfbdbdc5a47..491a880e5683e22e5c5a97d265580aa967b7954e 100644 (file)
@@ -274,6 +274,34 @@ static int format_config_bool(struct strbuf *buf,
        return 0;
 }
 
+static int format_config_bool_or_int(struct strbuf *buf,
+                                    const char *key_,
+                                    const char *value_,
+                                    const struct key_value_info *kvi,
+                                    int gently)
+{
+       int v, is_bool = 0;
+
+       if (gently) {
+               v = git_parse_maybe_bool_text(value_);
+
+               if (v >= 0)
+                       is_bool = 1;
+               else if (!git_parse_int(value_, &v))
+                       return -1;
+       } else {
+               v = git_config_bool_or_int(key_, value_, kvi,
+                                          &is_bool);
+       }
+
+       if (is_bool)
+               strbuf_addstr(buf, v ? "true" : "false");
+       else
+               strbuf_addf(buf, "%d", v);
+
+       return 0;
+}
+
 /*
  * Format the configuration key-value pair (`key_`, `value_`) and
  * 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,
                        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) {
-                       int is_bool, v;
-                       v = git_config_bool_or_int(key_, value_, kvi,
-                                                  &is_bool);
-                       if (is_bool)
-                               strbuf_addstr(buf, v ? "true" : "false");
-                       else
-                               strbuf_addf(buf, "%d", v);
-               } else if (opts->type == TYPE_BOOL_OR_STR) {
+               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) {
                        int v = git_parse_maybe_bool(value_);
                        if (v < 0)
                                strbuf_addstr(buf, value_);
index 568cfaa3c5629548e1d0baf8eb2aff870336b2d6..1fc8e788ee9cc5978814b4a28da2e5c1ad0d96a3 100755 (executable)
@@ -2539,7 +2539,9 @@ test_expect_success 'list --type=bool-or-int shows only canonicalizable values'
        section.big=1048576
        EOF
 
-       test_must_fail git config ${mode_prefix}list --type=bool-or-int
+       git config ${mode_prefix}list --type=bool-or-int >actual 2>err &&
+       test_cmp expect actual &&
+       test_must_be_empty err
 '
 
 test_expect_success 'list --type=path shows only canonicalizable path values' '