log-tree: use decimal_width()

Reduce code duplication by calling decimal_width() to count the digits in the number of commits instead of calculating it locally. It also has the advantage of returning int, which is the exact type expected by the printf()-like function strbuf_addf() for field width arguments. Additionally, decimal_width() supports numbers bigger than 1410065407, which is (hopefully) just a theoretical advantage. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 3, 2024 at 14:33 UTC 0c4d5aa22d65fb395962bdc0165840f66b7b91ea
1 file changed +2 -11
log-tree.c
+2 -11
@@ -29,6 +29,7 @@
29 #include "tree.h"
30 #include "wildmatch.h"
31 #include "write-or-die.h"
32 +#include "pager.h"
33
34 static struct decoration name_decoration = { "object names" };
35 static int decoration_loaded;
@@ -406,16 +407,6 @@ void show_decorations(struct rev_info *opt, struct commit *commit)
407 strbuf_release(&sb);
408 }
409
409 -static unsigned int digits_in_number(unsigned int number)
410 -{
411 - unsigned int i = 10, result = 1;
412 - while (i <= number) {
413 - i *= 10;
414 - result++;
415 - }
416 - return result;
417 -}
418 -
410 void fmt_output_subject(struct strbuf *filename,
411 const char *subject,
412 struct rev_info *info)
@@ -459,7 +450,7 @@ void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
450 strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
451 opt->subject_prefix,
452 *opt->subject_prefix ? " " : "",
462 - digits_in_number(opt->total),
453 + decimal_width(opt->total),
454 opt->nr, opt->total);
455 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
456 strbuf_addf(sb, "Subject: [%s] ",