]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: format int64s gently
authorDerrick Stolee <stolee@gmail.com>
Mon, 23 Feb 2026 12:26:46 +0000 (12:26 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Feb 2026 21:23:40 +0000 (13:23 -0800)
Move the logic for formatting int64 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 4c4c79188382a36b8f4a5d66e6eae791dedd86d6..448b1485637c58f89396903b9338f448cb0f3f12 100644 (file)
@@ -237,6 +237,25 @@ struct strbuf_list {
        int alloc;
 };
 
+static int format_config_int64(struct strbuf *buf,
+                              const char *key_,
+                              const char *value_,
+                              const struct key_value_info *kvi,
+                              int gently)
+{
+       int64_t v = 0;
+       if (gently) {
+               if (!git_parse_int64(value_, &v))
+                       return -1;
+       } else {
+               /* may die() */
+               v = git_config_int64(key_, value_ ? value_ : "", kvi);
+       }
+
+       strbuf_addf(buf, "%"PRId64, v);
+       return 0;
+}
+
 /*
  * Format the configuration key-value pair (`key_`, `value_`) and
  * append it into strbuf `buf`.  Returns a negative value on failure,
@@ -249,8 +268,9 @@ struct strbuf_list {
 static int format_config(const struct config_display_options *opts,
                         struct strbuf *buf, const char *key_,
                         const char *value_, const struct key_value_info *kvi,
-                        int gently UNUSED)
+                        int gently)
 {
+       int res = 0;
        if (opts->show_scope)
                show_config_scope(opts, kvi, buf);
        if (opts->show_origin)
@@ -262,8 +282,7 @@ static int format_config(const struct config_display_options *opts,
                        strbuf_addch(buf, opts->key_delim);
 
                if (opts->type == TYPE_INT)
-                       strbuf_addf(buf, "%"PRId64,
-                                   git_config_int64(key_, value_ ? value_ : "", kvi));
+                       res = format_config_int64(buf, key_, value_, kvi, gently);
                else if (opts->type == TYPE_BOOL)
                        strbuf_addstr(buf, git_config_bool(key_, value_) ?
                                      "true" : "false");
@@ -309,7 +328,7 @@ static int format_config(const struct config_display_options *opts,
                }
        }
        strbuf_addch(buf, opts->term);
-       return 0;
+       return res;
 }
 
 static int show_all_config(const char *key_, const char *value_,
index dc744c0baef10c58ace7f7d44a3ca9f3daec2cab..05a812fd6d27e8fe409c46b55b03f5b3aa691f73 100755 (executable)
@@ -2515,7 +2515,9 @@ test_expect_success 'list --type=int shows only canonicalizable int values' '
        section.big=1048576
        EOF
 
-       test_must_fail git config ${mode_prefix}list --type=int
+       git config ${mode_prefix}list --type=int >actual 2>err &&
+       test_cmp expect actual &&
+       test_must_be_empty err
 '
 
 test_expect_success 'list --type=bool shows only canonicalizable bool values' '