From: "Li Linchao via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Li Linchao <lilinchao@oschina.cn>, Li Linchao <lilinchao@oschina.cn>
Subject: [PATCH v5] rev-list: support human-readable output for `--disk-usage`
Date: Thu, 11 Aug 2022 04:47:54 +0000 [thread overview]
Message-ID: <pull.1313.v5.git.1660193274336.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1313.v4.git.1660130072657.gitgitgadget@gmail.com>
From: Li Linchao <lilinchao@oschina.cn>
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>
---
rev-list: support human-readable output for disk-usage
Signed-off-by: Li Linchao lilinchao@oschina.cn Cc: Jeff King
peff@peff.net cc: Ævar Arnfjörð Bjarmason avarab@gmail.com cc: Johannes
Sixt j6t@kdbg.org
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1313%2FCactusinhand%2Fllc%2Fadd-human-readable-option-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1313/Cactusinhand/llc/add-human-readable-option-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/1313
Range-diff vs v4:
1: e56da057a9a ! 1: 7fbf4b636d6 rev-list: support human-readable output for `--disk-usage`
@@ Documentation/rev-list-options.txt: ifdef::git-rev-list[]
faster (especially with `--use-bitmap-index`). See the `CAVEATS`
section in linkgit:git-cat-file[1] for the limitations of what
"on-disk storage" means.
-+ When it accepts a value `human`, like: `--disk-usage=human`, this
-+ means to print objects size in human readable format.
++ With the optional value `human`, on-disk storage size is shown
++ in human-readable string(e.g. 12.24 Kib, 3.50 Mib).
endif::git-rev-list[]
--cherry-mark::
@@ builtin/rev-list.c: static const char rev_list_usage[] =
" --parents\n"
" --children\n"
" --objects | --objects-edge\n"
-+" --disk-usage | --disk-usage=human\n"
++" --disk-usage[=human]\n"
" --unpacked\n"
" --header | --pretty\n"
" --[no-]object-names\n"
@@ builtin/rev-list.c: int cmd_rev_list(int argc, const char **argv, const char *pr
+ if (!strcmp(++arg, "human")) {
+ human_readable = 1;
+ } else
-+ die(_("invalid value for '%s': '%s', try --disk-usage=human"), "--disk-usage", arg);
++ die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
++ "--disk-usage=<format>", arg, "human");
+ } else if (*arg) {
+ /*
-+ * Arguably should goto a label to continue chain of ifs?
-+ * Doesn't matter unless we try to add --disk-usage-foo
-+ * afterwards
-+ */
++ * Arguably should goto a label to continue chain of ifs?
++ * Doesn't matter unless we try to add --disk-usage-foo
++ * afterwards.
++ */
+ usage(rev_list_usage);
+ }
show_disk_usage = 1;
@@ t/t6115-rev-list-du.sh: check_du HEAD
+test_expect_success 'rev-list use --disk-usage unproperly' '
+ test_must_fail git rev-list --objects HEAD --disk-usage=typo 2>err &&
+ cat >expect <<-\EOF &&
-+ fatal: invalid value for '\''--disk-usage'\'': '\''typo'\'', try --disk-usage=human
++ fatal: invalid value for '\''--disk-usage=<format>'\'': '\''typo'\'', the only allowed format is '\''human'\''
+ EOF
+ test_cmp err expect
+'
Documentation/rev-list-options.txt | 3 +++
builtin/rev-list.c | 36 ++++++++++++++++++++++++++----
t/t6115-rev-list-du.sh | 22 ++++++++++++++++++
3 files changed, 57 insertions(+), 4 deletions(-)
diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt
index 195e74eec63..bd08d18576f 100644
--- a/Documentation/rev-list-options.txt
+++ b/Documentation/rev-list-options.txt
@@ -242,6 +242,7 @@ ifdef::git-rev-list[]
to `/dev/null` as the output does not have to be formatted.
--disk-usage::
+--disk-usage=human::
Suppress normal output; instead, print the sum of the bytes used
for on-disk storage by the selected commits or objects. This is
equivalent to piping the output into `git cat-file
@@ -249,6 +250,8 @@ ifdef::git-rev-list[]
faster (especially with `--use-bitmap-index`). See the `CAVEATS`
section in linkgit:git-cat-file[1] for the limitations of what
"on-disk storage" means.
+ With the optional value `human`, on-disk storage size is shown
+ in human-readable string(e.g. 12.24 Kib, 3.50 Mib).
endif::git-rev-list[]
--cherry-mark::
diff --git a/builtin/rev-list.c b/builtin/rev-list.c
index 30fd8e83eaf..fba6f5d51f3 100644
--- a/builtin/rev-list.c
+++ b/builtin/rev-list.c
@@ -46,6 +46,7 @@ static const char rev_list_usage[] =
" --parents\n"
" --children\n"
" --objects | --objects-edge\n"
+" --disk-usage[=human]\n"
" --unpacked\n"
" --header | --pretty\n"
" --[no-]object-names\n"
@@ -81,6 +82,7 @@ static int arg_show_object_names = 1;
static int show_disk_usage;
static off_t total_disk_usage;
+static int human_readable;
static off_t get_object_disk_usage(struct object *obj)
{
@@ -368,6 +370,17 @@ static int show_object_fast(
return 1;
}
+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);
+}
+
static inline int parse_missing_action_value(const char *value)
{
if (!strcmp(value, "error")) {
@@ -473,6 +486,7 @@ static int try_bitmap_disk_usage(struct rev_info *revs,
int filter_provided_objects)
{
struct bitmap_index *bitmap_git;
+ off_t size_from_bitmap;
if (!show_disk_usage)
return -1;
@@ -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);
return 0;
}
@@ -624,7 +638,21 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
continue;
}
- if (!strcmp(arg, "--disk-usage")) {
+ if (skip_prefix(arg, "--disk-usage", &arg)) {
+ if (*arg == '=') {
+ if (!strcmp(++arg, "human")) {
+ human_readable = 1;
+ } else
+ die(_("invalid value for '%s': '%s', the only allowed format is '%s'"),
+ "--disk-usage=<format>", arg, "human");
+ } else if (*arg) {
+ /*
+ * Arguably should goto a label to continue chain of ifs?
+ * Doesn't matter unless we try to add --disk-usage-foo
+ * afterwards.
+ */
+ usage(rev_list_usage);
+ }
show_disk_usage = 1;
info.flags |= REV_LIST_QUIET;
continue;
@@ -753,7 +781,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
}
if (show_disk_usage)
- printf("%"PRIuMAX"\n", (uintmax_t)total_disk_usage);
+ print_disk_usage(total_disk_usage);
cleanup:
release_revisions(&revs);
diff --git a/t/t6115-rev-list-du.sh b/t/t6115-rev-list-du.sh
index b4aef32b713..d59111dedec 100755
--- a/t/t6115-rev-list-du.sh
+++ b/t/t6115-rev-list-du.sh
@@ -48,4 +48,26 @@ check_du HEAD
check_du --objects HEAD
check_du --objects HEAD^..HEAD
+# As mentioned above, don't use hardcode sizes as actual size, but use the
+# output from git cat-file.
+test_expect_success 'rev-list --disk-usage=human' '
+ git rev-list --objects HEAD --disk-usage=human >actual &&
+ disk_usage_slow --objects HEAD >actual_size &&
+ grep "$(cat actual_size) bytes" actual
+'
+
+test_expect_success 'rev-list --disk-usage=human with bitmaps' '
+ git rev-list --objects HEAD --use-bitmap-index --disk-usage=human >actual &&
+ disk_usage_slow --objects HEAD >actual_size &&
+ grep "$(cat actual_size) bytes" actual
+'
+
+test_expect_success 'rev-list use --disk-usage unproperly' '
+ test_must_fail git rev-list --objects HEAD --disk-usage=typo 2>err &&
+ cat >expect <<-\EOF &&
+ fatal: invalid value for '\''--disk-usage=<format>'\'': '\''typo'\'', the only allowed format is '\''human'\''
+ EOF
+ test_cmp err expect
+'
+
test_done
base-commit: 679aad9e82d0dfd8ef3d1f98fa4629665496cec9
--
gitgitgadget
next prev parent reply other threads:[~2022-08-11 4:48 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-05 7:54 [PATCH] rev-list: support `--human-readable` option when applied `disk-usage` Li Linchao via GitGitGadget
2022-08-05 10:03 ` Ævar Arnfjörð Bjarmason
2022-08-05 11:01 ` lilinchao
2022-08-08 8:35 ` [PATCH v2] rev-list: support human-readable output for `--disk-usage` Li Linchao via GitGitGadget
2022-08-08 9:37 ` lilinchao
2022-08-09 13:22 ` Jeff King
2022-08-09 16:46 ` lilinchao
2022-08-10 6:01 ` [PATCH v3] " Li Linchao via GitGitGadget
2022-08-10 7:18 ` Johannes Sixt
2022-08-10 11:14 ` [PATCH v4] " Li Linchao via GitGitGadget
2022-08-10 17:34 ` Junio C Hamano
2022-08-10 21:20 ` Jeff King
2022-08-10 21:25 ` Junio C Hamano
2022-08-11 5:20 ` Junio C Hamano
2022-08-11 8:38 ` Jeff King
2022-08-11 4:47 ` Li Linchao via GitGitGadget [this message]
2022-08-11 20:49 ` [PATCH v5] " Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=pull.1313.v5.git.1660193274336.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=lilinchao@oschina.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).