]> git.ipfire.org Git - thirdparty/git.git/commitdiff
count-objects: add -H option to humanize sizes
authorAntoine Pelisse <apelisse@gmail.com>
Wed, 10 Apr 2013 19:03:24 +0000 (21:03 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 10 Apr 2013 20:27:26 +0000 (13:27 -0700)
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>
Documentation/git-count-objects.txt
builtin/count-objects.c

index da6e72e696d4af013b3c717d31a0e98cd41922d4..b300e846f13d6a7f340286b4624dc3b0da4ac740 100644 (file)
@@ -8,7 +8,7 @@ git-count-objects - Count unpacked number of objects and their disk consumption
 SYNOPSIS
 --------
 [verse]
-'git count-objects' [-v]
+'git count-objects' [-v] [-H | --human-readable]
 
 DESCRIPTION
 -----------
@@ -24,11 +24,11 @@ OPTIONS
 +
 count: the number of loose objects
 +
-size: disk space consumed by loose objects, in KiB
+size: disk space consumed by loose objects, in KiB (unless -H is specified)
 +
 in-pack: the number of in-pack objects
 +
-size-pack: disk space consumed by the packs, in KiB
+size-pack: disk space consumed by the packs, in KiB (unless -H is specified)
 +
 prune-packable: the number of loose objects that are also present in
 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`.
 garbage: the number of files in object database that are not valid
 loose objects nor valid packs
 +
-size-garbage: disk space consumed by garbage files, in KiB
+size-garbage: disk space consumed by garbage files, in KiB (unless -H is
+specified)
+
+-H::
+--human-readable::
+
+Print sizes in human readable format
 
 GIT
 ---
index 3a01a8d08591d93426d436ce2e1e17fccd3ebf78..a7f70cb858e6574562957b739b8a3d42f0353b40 100644 (file)
@@ -79,13 +79,13 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
 }
 
 static char const * const count_objects_usage[] = {
-       N_("git count-objects [-v]"),
+       N_("git count-objects [-v] [-H | --human-readable]"),
        NULL
 };
 
 int cmd_count_objects(int argc, const char **argv, const char *prefix)
 {
-       int i, verbose = 0;
+       int i, verbose = 0, human_readable = 0;
        const char *objdir = get_object_directory();
        int len = strlen(objdir);
        char *path = xmalloc(len + 50);
@@ -93,6 +93,8 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
        off_t loose_size = 0;
        struct option opts[] = {
                OPT__VERBOSE(&verbose, N_("be verbose")),
+               OPT_BOOL('H', "human-readable", &human_readable,
+                        N_("print sizes in human readable format")),
                OPT_END(),
        };
 
@@ -119,6 +121,9 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
                struct packed_git *p;
                unsigned long num_pack = 0;
                off_t size_pack = 0;
+               struct strbuf loose_buf = STRBUF_INIT;
+               struct strbuf pack_buf = STRBUF_INIT;
+               struct strbuf garbage_buf = STRBUF_INIT;
                if (!packed_git)
                        prepare_packed_git();
                for (p = packed_git; p; p = p->next) {
@@ -130,17 +135,40 @@ int cmd_count_objects(int argc, const char **argv, const char *prefix)
                        size_pack += p->pack_size + p->index_size;
                        num_pack++;
                }
+
+               if (human_readable) {
+                       strbuf_humanise_bytes(&loose_buf, loose_size);
+                       strbuf_humanise_bytes(&pack_buf, size_pack);
+                       strbuf_humanise_bytes(&garbage_buf, size_garbage);
+               } else {
+                       strbuf_addf(&loose_buf, "%lu",
+                                   (unsigned long)(loose_size / 1024));
+                       strbuf_addf(&pack_buf, "%lu",
+                                   (unsigned long)(size_pack / 1024));
+                       strbuf_addf(&garbage_buf, "%lu",
+                                   (unsigned long)(size_garbage / 1024));
+               }
+
                printf("count: %lu\n", loose);
-               printf("size: %lu\n", (unsigned long) (loose_size / 1024));
+               printf("size: %s\n", loose_buf.buf);
                printf("in-pack: %lu\n", packed);
                printf("packs: %lu\n", num_pack);
-               printf("size-pack: %lu\n", (unsigned long) (size_pack / 1024));
+               printf("size-pack: %s\n", pack_buf.buf);
                printf("prune-packable: %lu\n", packed_loose);
                printf("garbage: %lu\n", garbage);
-               printf("size-garbage: %lu\n", (unsigned long) (size_garbage / 1024));
+               printf("size-garbage: %s\n", garbage_buf.buf);
+               strbuf_release(&loose_buf);
+               strbuf_release(&pack_buf);
+               strbuf_release(&garbage_buf);
+       } else {
+               struct strbuf buf = STRBUF_INIT;
+               if (human_readable)
+                       strbuf_humanise_bytes(&buf, loose_size);
+               else
+                       strbuf_addf(&buf, "%lu kilobytes",
+                                   (unsigned long)(loose_size / 1024));
+               printf("%lu objects, %s\n", loose, buf.buf);
+               strbuf_release(&buf);
        }
-       else
-               printf("%lu objects, %lu kilobytes\n",
-                      loose, (unsigned long) (loose_size / 1024));
        return 0;
 }