Skip to content

Commit fddd8b4

Browse files
committed
Merge branch 'll/disk-usage-humanise'
"git rev-list --disk-usage" learned to take an optional value "human" to show the reported value in human-readable format, like "3.40MiB". * ll/disk-usage-humanise: rev-list: support human-readable output for `--disk-usage`
2 parents 9b9445c + 9096451 commit fddd8b4

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

Documentation/rev-list-options.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,16 @@ ifdef::git-rev-list[]
242242
to `/dev/null` as the output does not have to be formatted.
243243

244244
--disk-usage::
245+
--disk-usage=human::
245246
Suppress normal output; instead, print the sum of the bytes used
246247
for on-disk storage by the selected commits or objects. This is
247248
equivalent to piping the output into `git cat-file
248249
--batch-check='%(objectsize:disk)'`, except that it runs much
249250
faster (especially with `--use-bitmap-index`). See the `CAVEATS`
250251
section in linkgit:git-cat-file[1] for the limitations of what
251252
"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).
252255
endif::git-rev-list[]
253256

254257
--cherry-mark::

builtin/rev-list.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static const char rev_list_usage[] =
4646
" --parents\n"
4747
" --children\n"
4848
" --objects | --objects-edge\n"
49+
" --disk-usage[=human]\n"
4950
" --unpacked\n"
5051
" --header | --pretty\n"
5152
" --[no-]object-names\n"
@@ -81,6 +82,7 @@ static int arg_show_object_names = 1;
8182

8283
static int show_disk_usage;
8384
static off_t total_disk_usage;
85+
static int human_readable;
8486

8587
static off_t get_object_disk_usage(struct object *obj)
8688
{
@@ -368,6 +370,17 @@ static int show_object_fast(
368370
return 1;
369371
}
370372

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+
371384
static inline int parse_missing_action_value(const char *value)
372385
{
373386
if (!strcmp(value, "error")) {
@@ -473,6 +486,7 @@ static int try_bitmap_disk_usage(struct rev_info *revs,
473486
int filter_provided_objects)
474487
{
475488
struct bitmap_index *bitmap_git;
489+
off_t size_from_bitmap;
476490

477491
if (!show_disk_usage)
478492
return -1;
@@ -481,8 +495,8 @@ static int try_bitmap_disk_usage(struct rev_info *revs,
481495
if (!bitmap_git)
482496
return -1;
483497

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);
486500
return 0;
487501
}
488502

@@ -624,7 +638,21 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
624638
continue;
625639
}
626640

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+
}
628656
show_disk_usage = 1;
629657
info.flags |= REV_LIST_QUIET;
630658
continue;
@@ -753,7 +781,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
753781
}
754782

755783
if (show_disk_usage)
756-
printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
784+
print_disk_usage(total_disk_usage);
757785

758786
cleanup:
759787
release_revisions(&revs);

t/t6115-rev-list-du.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,26 @@ check_du HEAD
4848
check_du --objects HEAD
4949
check_du --objects HEAD^..HEAD
5050

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+
5173
test_done

0 commit comments

Comments
 (0)