@@ -79,20 +79,22 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
7979}
8080
8181static 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
8686int 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