config: format int64s gently

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>

Derrick Stolee committed Feb 23, 2026 at 12:26 UTC d744923fefb294c835d18883bac62f85ff55fc9f
2 files changed +26 -5
builtin/config.c
+23 -4
@@ -237,6 +237,25 @@ struct strbuf_list {
237 int alloc;
238 };
239
240 +static int format_config_int64(struct strbuf *buf,
241 + const char *key_,
242 + const char *value_,
243 + const struct key_value_info *kvi,
244 + int gently)
245 +{
246 + int64_t v = 0;
247 + if (gently) {
248 + if (!git_parse_int64(value_, &v))
249 + return -1;
250 + } else {
251 + /* may die() */
252 + v = git_config_int64(key_, value_ ? value_ : "", kvi);
253 + }
254 +
255 + strbuf_addf(buf, "%"PRId64, v);
256 + return 0;
257 +}
258 +
259 /*
260 * Format the configuration key-value pair (`key_`, `value_`) and
261 * append it into strbuf `buf`. Returns a negative value on failure,
@@ -249,8 +268,9 @@ struct strbuf_list {
268 static int format_config(const struct config_display_options *opts,
269 struct strbuf *buf, const char *key_,
270 const char *value_, const struct key_value_info *kvi,
252 - int gently UNUSED)
271 + int gently)
272 {
273 + int res = 0;
274 if (opts->show_scope)
275 show_config_scope(opts, kvi, buf);
276 if (opts->show_origin)
@@ -262,8 +282,7 @@ static int format_config(const struct config_display_options *opts,
282 strbuf_addch(buf, opts->key_delim);
283
284 if (opts->type == TYPE_INT)
265 - strbuf_addf(buf, "%"PRId64,
266 - git_config_int64(key_, value_ ? value_ : "", kvi));
285 + res = format_config_int64(buf, key_, value_, kvi, gently);
286 else if (opts->type == TYPE_BOOL)
287 strbuf_addstr(buf, git_config_bool(key_, value_) ?
288 "true" : "false");
@@ -309,7 +328,7 @@ static int format_config(const struct config_display_options *opts,
328 }
329 }
330 strbuf_addch(buf, opts->term);
312 - return 0;
331 + return res;
332 }
333
334 static int show_all_config(const char *key_, const char *value_,
t/t1300-config.sh
+3 -1
@@ -2515,7 +2515,9 @@ test_expect_success 'list --type=int shows only canonicalizable int values' '
2515 section.big=1048576
2516 EOF
2517
2518 - test_must_fail git config ${mode_prefix}list --type=int
2518 + git config ${mode_prefix}list --type=int >actual 2>err &&
2519 + test_cmp expect actual &&
2520 + test_must_be_empty err
2521 '
2522
2523 test_expect_success 'list --type=bool shows only canonicalizable bool values' '