rev-list: support human-readable output for `--disk-usage`

The '--disk-usage' option for git-rev-list was introduced in 16950f8384 (rev-list: add --disk-usage option for calculating disk usage, 2021-02-09). This is very useful for people inspect their git repo's objects usage infomation, but the resulting number is quit hard for a human to read. Teach git rev-list to output a human readable result when using '--disk-usage'. Signed-off-by: Li Linchao <lilinchao@oschina.cn> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Li Linchao committed Aug 11, 2022 at 04:47 UTC 9096451acdf065c3dbcf609dcefe51cd68aa5d1e
3 files changed +57 -4
Documentation/rev-list-options.txt
+3
@@ -242,6 +242,7 @@ ifdef::git-rev-list[]
242 to `/dev/null` as the output does not have to be formatted.
243
244 --disk-usage::
245 +--disk-usage=human::
246 Suppress normal output; instead, print the sum of the bytes used
247 for on-disk storage by the selected commits or objects. This is
248 equivalent to piping the output into `git cat-file
@@ -249,6 +250,8 @@ ifdef::git-rev-list[]
250 faster (especially with `--use-bitmap-index`). See the `CAVEATS`
251 section in linkgit:git-cat-file[1] for the limitations of what
252 "on-disk storage" means.
253 + With the optional value `human`, on-disk storage size is shown
254 + in human-readable string(e.g. 12.24 Kib, 3.50 Mib).
255 endif::git-rev-list[]
256
257 --cherry-mark::
builtin/rev-list.c
+32 -4
@@ -46,6 +46,7 @@ static const char rev_list_usage[] =
46 " --parents\n"
47 " --children\n"
48 " --objects | --objects-edge\n"
49 +" --disk-usage[=human]\n"
50 " --unpacked\n"
51 " --header | --pretty\n"
52 " --[no-]object-names\n"
@@ -81,6 +82,7 @@ static int arg_show_object_names = 1;
82
83 static int show_disk_usage;
84 static off_t total_disk_usage;
85 +static int human_readable;
86
87 static off_t get_object_disk_usage(struct object *obj)
88 {
@@ -368,6 +370,17 @@ static int show_object_fast(
370 return 1;
371 }
372
373 +static void print_disk_usage(off_t size)
374 +{
375 + struct strbuf sb = STRBUF_INIT;
376 + if (human_readable)
377 + strbuf_humanise_bytes(&sb, size);
378 + else
379 + strbuf_addf(&sb, "%"PRIuMAX, (uintmax_t)size);
380 + puts(sb.buf);
381 + strbuf_release(&sb);
382 +}
383 +
384 static inline int parse_missing_action_value(const char *value)
385 {
386 if (!strcmp(value, "error")) {
@@ -473,6 +486,7 @@ static int try_bitmap_disk_usage(struct rev_info *revs,
486 int filter_provided_objects)
487 {
488 struct bitmap_index *bitmap_git;
489 + off_t size_from_bitmap;
490
491 if (!show_disk_usage)
492 return -1;
@@ -481,8 +495,8 @@ static int try_bitmap_disk_usage(struct rev_info *revs,
495 if (!bitmap_git)
496 return -1;
497
484 - printf("%"PRIuMAX"\n",
485 - (uintmax_t)get_disk_usage_from_bitmap(bitmap_git, revs));
498 + size_from_bitmap = get_disk_usage_from_bitmap(bitmap_git, revs);
499 + print_disk_usage(size_from_bitmap);
500 return 0;
501 }
502
@@ -624,7 +638,21 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
638 continue;
639 }
640
627 - if (!strcmp(arg, "--disk-usage")) {
641 + if (skip_prefix(arg, "--disk-usage", &arg)) {
642 + if (*arg == '=') {
643 + if (!strcmp(++arg, "human")) {
644 + human_readable = 1;
645 + } else
646 + die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
647 + "--disk-usage=<format>", arg, "human");
648 + } else if (*arg) {
649 + /*
650 + * Arguably should goto a label to continue chain of ifs?
651 + * Doesn't matter unless we try to add --disk-usage-foo
652 + * afterwards.
653 + */
654 + usage(rev_list_usage);
655 + }
656 show_disk_usage = 1;
657 info.flags |= REV_LIST_QUIET;
658 continue;
@@ -753,7 +781,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
781 }
782
783 if (show_disk_usage)
756 - printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
784 + print_disk_usage(total_disk_usage);
785
786 cleanup:
787 release_revisions(&revs);
t/t6115-rev-list-du.sh
+22
@@ -48,4 +48,26 @@ check_du HEAD
48 check_du --objects HEAD
49 check_du --objects HEAD^..HEAD
50
51 +# As mentioned above, don't use hardcode sizes as actual size, but use the
52 +# output from git cat-file.
53 +test_expect_success 'rev-list --disk-usage=human' '
54 + git rev-list --objects HEAD --disk-usage=human >actual &&
55 + disk_usage_slow --objects HEAD >actual_size &&
56 + grep "$(cat actual_size) bytes" actual
57 +'
58 +
59 +test_expect_success 'rev-list --disk-usage=human with bitmaps' '
60 + git rev-list --objects HEAD --use-bitmap-index --disk-usage=human >actual &&
61 + disk_usage_slow --objects HEAD >actual_size &&
62 + grep "$(cat actual_size) bytes" actual
63 +'
64 +
65 +test_expect_success 'rev-list use --disk-usage unproperly' '
66 + test_must_fail git rev-list --objects HEAD --disk-usage=typo 2>err &&
67 + cat >expect <<-\EOF &&
68 + fatal: invalid value for '\''--disk-usage=<format>'\'': '\''typo'\'', the only allowed format is '\''human'\''
69 + EOF
70 + test_cmp err expect
71 +'
72 +
73 test_done