count-objects: add -H option to humanize sizes

Use the new humanize() function to print loose objects size, pack size, and garbage size in verbose mode, or loose objects size in regular mode. This patch doesn't change the way anything is displayed when the option is not used. Also update the documentation. Signed-off-by: Antoine Pelisse <apelisse@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Antoine Pelisse committed Apr 10, 2013 at 21:03 UTC 1918225d2fce49c830b41b8c7907229638ae2825
2 files changed +46 -12
Documentation/git-count-objects.txt
+10 -4
@@ -8,7 +8,7 @@ git-count-objects - Count unpacked number of objects and their disk consumption
8 SYNOPSIS
9 --------
10 [verse]
11 -'git count-objects' [-v]
11 +'git count-objects' [-v] [-H | --human-readable]
12
13 DESCRIPTION
14 -----------
@@ -24,11 +24,11 @@ OPTIONS
24 +
25 count: the number of loose objects
26 +
27 -size: disk space consumed by loose objects, in KiB
27 +size: disk space consumed by loose objects, in KiB (unless -H is specified)
28 +
29 in-pack: the number of in-pack objects
30 +
31 -size-pack: disk space consumed by the packs, in KiB
31 +size-pack: disk space consumed by the packs, in KiB (unless -H is specified)
32 +
33 prune-packable: the number of loose objects that are also present in
34 the packs. These objects could be pruned using `git prune-packed`.
@@ -36,7 +36,13 @@ the packs. These objects could be pruned using `git prune-packed`.
36 garbage: the number of files in object database that are not valid
37 loose objects nor valid packs
38 +
39 -size-garbage: disk space consumed by garbage files, in KiB
39 +size-garbage: disk space consumed by garbage files, in KiB (unless -H is
40 +specified)
41 +
42 +-H::
43 +--human-readable::
44 +
45 +Print sizes in human readable format
46
47 GIT
48 ---
builtin/count-objects.c
+36 -8
@@ -79,13 +79,13 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
79 }
80
81 static char const * const count_objects_usage[] = {
82 - N_("git count-objects [-v]"),
82 + N_("git count-objects [-v] [-H | --human-readable]"),
83 NULL
84 };
85
86 int cmd_count_objects(int argc, const char **argv, const char *prefix)
87 {
88 - int i, verbose = 0;
88 + int i, verbose = 0, human_readable = 0;
89 const char *objdir = get_object_directory();
90 int len = strlen(objdir);
91 char *path = xmalloc(len + 50);
@@ -93,6 +93,8 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
93 off_t loose_size = 0;
94 struct option opts[] = {
95 OPT__VERBOSE(&verbose, N_("be verbose")),
96 + OPT_BOOL('H', "human-readable", &human_readable,
97 + N_("print sizes in human readable format")),
98 OPT_END(),
99 };
100
@@ -119,6 +121,9 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
121 struct packed_git *p;
122 unsigned long num_pack = 0;
123 off_t size_pack = 0;
124 + struct strbuf loose_buf = STRBUF_INIT;
125 + struct strbuf pack_buf = STRBUF_INIT;
126 + struct strbuf garbage_buf = STRBUF_INIT;
127 if (!packed_git)
128 prepare_packed_git();
129 for (p = packed_git; p; p = p->next) {
@@ -130,17 +135,40 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
135 size_pack += p->pack_size + p->index_size;
136 num_pack++;
137 }
138 +
139 + if (human_readable) {
140 + strbuf_humanise_bytes(&loose_buf, loose_size);
141 + strbuf_humanise_bytes(&pack_buf, size_pack);
142 + strbuf_humanise_bytes(&garbage_buf, size_garbage);
143 + } else {
144 + strbuf_addf(&loose_buf, "%lu",
145 + (unsigned long)(loose_size / 1024));
146 + strbuf_addf(&pack_buf, "%lu",
147 + (unsigned long)(size_pack / 1024));
148 + strbuf_addf(&garbage_buf, "%lu",
149 + (unsigned long)(size_garbage / 1024));
150 + }
151 +
152 printf("count: %lu\n", loose);
134 - printf("size: %lu\n", (unsigned long) (loose_size / 1024));
153 + printf("size: %s\n", loose_buf.buf);
154 printf("in-pack: %lu\n", packed);
155 printf("packs: %lu\n", num_pack);
137 - printf("size-pack: %lu\n", (unsigned long) (size_pack / 1024));
156 + printf("size-pack: %s\n", pack_buf.buf);
157 printf("prune-packable: %lu\n", packed_loose);
158 printf("garbage: %lu\n", garbage);
140 - printf("size-garbage: %lu\n", (unsigned long) (size_garbage / 1024));
159 + printf("size-garbage: %s\n", garbage_buf.buf);
160 + strbuf_release(&loose_buf);
161 + strbuf_release(&pack_buf);
162 + strbuf_release(&garbage_buf);
163 + } else {
164 + struct strbuf buf = STRBUF_INIT;
165 + if (human_readable)
166 + strbuf_humanise_bytes(&buf, loose_size);
167 + else
168 + strbuf_addf(&buf, "%lu kilobytes",
169 + (unsigned long)(loose_size / 1024));
170 + printf("%lu objects, %s\n", loose, buf.buf);
171 + strbuf_release(&buf);
172 }
142 - else
143 - printf("%lu objects, %lu kilobytes\n",
144 - loose, (unsigned long) (loose_size / 1024));
173 return 0;
174 }