Re: [PATCH v4] rev-list: support human-readable output for `--disk-usage`
- From
Jeff King <peff@peff.net>
- Date
- Aug 10, 2022, 21:20 UTC
- Message-ID
- <YvQhHOkjZatIqlFr@coredump.intra.peff.net>
- In-Reply-To
- <xmqqlerwm28n.fsf@gitster.g>
On Wed, Aug 10, 2022 at 10:34:32AM -0700, Junio C Hamano wrote:
Show 25 quoted lines
> > +static void print_disk_usage(off_t size)
> > +{
> > + struct strbuf sb = STRBUF_INIT;
> > + if (human_readable)
> > + strbuf_humanise_bytes(&sb, size);
> > + else
> > + strbuf_addf(&sb, "%"PRIuMAX, (uintmax_t)size);
> > + puts(sb.buf);
> > + strbuf_release(&sb);
> > +}
>
> Hmph, I am not sure if we want to make it a helper like this. The
> normal case does not need to prepare the string into a strbuf but
> just can send the output to the standard output stream.
>
> It is probably easy to fix, like so:
>
> if (!human_readable) {
> printf("%" PRIuMAX "\n", disk_usage);
> } else {
> strbuf sb = STRBUF_INIT;
> strbuf_humanise_bytes(&sb, disk_usage);
> puts(sb.buf);
> strbuf_release(&sb);
> }It was my suggestion to turn it into a helper, because the same code needs to be present in two distant spots (the bitmap and non-bitmap cases).
I don't care much between "printf directly vs strbuf" for the non-human case, but it was an earlier review suggestion to connect them. I do think the result is a little easier to follow, but mostly I want to make it clear that the author is getting stuck between warring review comments here. ;)
Show 14 quoted lines
> > @@ -481,8 +495,8 @@ static int try_bitmap_disk_usage(struct rev_info *revs,
> > if (!bitmap_git)
> > return -1;
> >
> > - printf("%"PRIuMAX"\n",
> > - (uintmax_t)get_disk_usage_from_bitmap(bitmap_git, revs));
> > + size_from_bitmap = get_disk_usage_from_bitmap(bitmap_git, revs);
> > + print_disk_usage(size_from_bitmap);
>
> It makes sense to make the function declare how it gets disk usage
> in its name, but once we call the function to get what we want,
> there is no need to keep saying we got it from bitmap. If we ever
> gained another function that obtains the disk usage from other
> means, then this part of the code would becomeKeep in mind that we are in try_bitmap_disk_usage() here. :) There is indeed similar code to use other means, but it's far away, and this code will always use bitmaps.
That said, I'd have just written:
print_disk_usage(get_disk_usage_from_bitmap(bitmap_git, revs));
since the variable is not otherwise used. But arguably that's harder to read.
-Peff