diff mbox series

log-tree: use decimal_width()

Message ID 06783722-5184-41d5-8edd-94f97b2f2794@web.de (mailing list archive)
State New
Headers show
Series log-tree: use decimal_width() | expand

Commit Message

René Scharfe Aug. 3, 2024, 12:33 p.m. UTC
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>
---
 log-tree.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

--
2.46.0

Comments

Taylor Blau Aug. 4, 2024, 4:10 p.m. UTC | #1
On Sat, Aug 03, 2024 at 02:33:24PM +0200, René Scharfe wrote:
> ---
>  log-tree.c | 13 ++-----------
>  1 file changed, 2 insertions(+), 11 deletions(-)

This looks all very reasonable to me.

Some history (if interesting): lineno_width(), which was the precursor
to decimal_width() was introduced way back in cee7f245dc (git-pickaxe:
blame rewritten., 2006-10-19). The piece this patch removes was added in
e00de24b10 (format-patch -n: make sorting easier by padding number,
2007-02-09), but lineno_width() did not become part of pager.h until
ec7ff5ba27 (make lineno_width() from blame reusable for others,
2012-02-12), hence why it wasn't already used here.

Thanks,
Taylor
Junio C Hamano Aug. 5, 2024, 4:03 p.m. UTC | #2
Taylor Blau <me@ttaylorr.com> writes:

> On Sat, Aug 03, 2024 at 02:33:24PM +0200, René Scharfe wrote:
>> ---
>>  log-tree.c | 13 ++-----------
>>  1 file changed, 2 insertions(+), 11 deletions(-)
>
> This looks all very reasonable to me.
>
> Some history (if interesting): lineno_width(), which was the precursor
> to decimal_width() was introduced way back in cee7f245dc (git-pickaxe:
> blame rewritten., 2006-10-19). The piece this patch removes was added in
> e00de24b10 (format-patch -n: make sorting easier by padding number,
> 2007-02-09), but lineno_width() did not become part of pager.h until
> ec7ff5ba27 (make lineno_width() from blame reusable for others,
> 2012-02-12), hence why it wasn't already used here.

Thanks.

I was wondering why the decimal_width() helper is in pager.c in the
first place, as there is no logical reason to tie it to the pager
infrastructure.
diff mbox series

Patch

diff --git a/log-tree.c b/log-tree.c
index 576ef30d90..fdb24cbef2 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -31,6 +31,7 @@ 
 #include "tree.h"
 #include "wildmatch.h"
 #include "write-or-die.h"
+#include "pager.h"

 static struct decoration name_decoration = { "object names" };
 static int decoration_loaded;
@@ -411,16 +412,6 @@  void show_decorations(struct rev_info *opt, struct commit *commit)
 	strbuf_release(&sb);
 }

-static unsigned int digits_in_number(unsigned int number)
-{
-	unsigned int i = 10, result = 1;
-	while (i <= number) {
-		i *= 10;
-		result++;
-	}
-	return result;
-}
-
 void fmt_output_subject(struct strbuf *filename,
 			const char *subject,
 			struct rev_info *info)
@@ -464,7 +455,7 @@  void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
 		strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
 			    opt->subject_prefix,
 			    *opt->subject_prefix ? " " : "",
-			    digits_in_number(opt->total),
+			    decimal_width(opt->total),
 			    opt->nr, opt->total);
 	} else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
 		strbuf_addf(sb, "Subject: [%s] ",