Skip to content

Commit 1918225

Browse files
apelissegitster
authored andcommitted
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>
1 parent 079b546 commit 1918225

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

Documentation/git-count-objects.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-count-objects - Count unpacked number of objects and their disk consumption
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git count-objects' [-v]
11+
'git count-objects' [-v] [-H | --human-readable]
1212

1313
DESCRIPTION
1414
-----------
@@ -24,19 +24,25 @@ OPTIONS
2424
+
2525
count: the number of loose objects
2626
+
27-
size: disk space consumed by loose objects, in KiB
27+
size: disk space consumed by loose objects, in KiB (unless -H is specified)
2828
+
2929
in-pack: the number of in-pack objects
3030
+
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)
3232
+
3333
prune-packable: the number of loose objects that are also present in
3434
the packs. These objects could be pruned using `git prune-packed`.
3535
+
3636
garbage: the number of files in object database that are not valid
3737
loose objects nor valid packs
3838
+
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
4046

4147
GIT
4248
---

builtin/count-objects.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,22 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
7979
}
8080

8181
static char const * const count_objects_usage[] = {
82-
N_("git count-objects [-v]"),
82+
N_("git count-objects [-v] [-H | --human-readable]"),
8383
NULL
8484
};
8585

8686
int cmd_count_objects(int argc, const char **argv, const char *prefix)
8787
{
88-
int i, verbose = 0;
88+
int i, verbose = 0, human_readable = 0;
8989
const char *objdir = get_object_directory();
9090
int len = strlen(objdir);
9191
char *path = xmalloc(len + 50);
9292
unsigned long loose = 0, packed = 0, packed_loose = 0;
9393
off_t loose_size = 0;
9494
struct option opts[] = {
9595
OPT__VERBOSE(&verbose, N_("be verbose")),
96+
OPT_BOOL('H', "human-readable", &human_readable,
97+
N_("print sizes in human readable format")),
9698
OPT_END(),
9799
};
98100

@@ -119,6 +121,9 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
119121
struct packed_git *p;
120122
unsigned long num_pack = 0;
121123
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;
122127
if (!packed_git)
123128
prepare_packed_git();
124129
for (p = packed_git; p; p = p->next) {
@@ -130,17 +135,40 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
130135
size_pack += p->pack_size + p->index_size;
131136
num_pack++;
132137
}
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+
133152
printf("count: %lu\n", loose);
134-
printf("size: %lu\n", (unsigned long) (loose_size / 1024));
153+
printf("size: %s\n", loose_buf.buf);
135154
printf("in-pack: %lu\n", packed);
136155
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);
138157
printf("prune-packable: %lu\n", packed_loose);
139158
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);
141172
}
142-
else
143-
printf("%lu objects, %lu kilobytes\n",
144-
loose, (unsigned long) (loose_size / 1024));
145173
return 0;
146174
}

0 commit comments

Comments
 (0)