2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 #include "parse-options.h"
12 static unsigned long garbage
;
13 static off_t size_garbage
;
15 static unsigned long loose
, packed
, packed_loose
;
16 static off_t loose_size
;
18 static void real_report_garbage(const char *desc
, const char *path
)
22 size_garbage
+= st
.st_size
;
23 warning("%s: %s", desc
, path
);
27 static void loose_garbage(const char *path
)
30 report_garbage("garbage found", path
);
33 static int count_loose(const unsigned char *sha1
, const char *path
, void *data
)
37 if (lstat(path
, &st
) || !S_ISREG(st
.st_mode
))
40 loose_size
+= on_disk_bytes(st
);
42 if (verbose
&& has_sha1_pack(sha1
))
48 static int count_cruft(const char *basename
, const char *path
, void *data
)
54 static char const * const count_objects_usage
[] = {
55 N_("git count-objects [-v] [-H | --human-readable]"),
59 int cmd_count_objects(int argc
, const char **argv
, const char *prefix
)
61 int human_readable
= 0;
62 struct option opts
[] = {
63 OPT__VERBOSE(&verbose
, N_("be verbose")),
64 OPT_BOOL('H', "human-readable", &human_readable
,
65 N_("print sizes in human readable format")),
69 argc
= parse_options(argc
, argv
, prefix
, opts
, count_objects_usage
, 0);
70 /* we do not take arguments other than flags for now */
72 usage_with_options(count_objects_usage
, opts
);
74 report_garbage
= real_report_garbage
;
75 report_linked_checkout_garbage();
78 for_each_loose_file_in_objdir(get_object_directory(),
79 count_loose
, count_cruft
, NULL
, NULL
);
83 unsigned long num_pack
= 0;
85 struct strbuf loose_buf
= STRBUF_INIT
;
86 struct strbuf pack_buf
= STRBUF_INIT
;
87 struct strbuf garbage_buf
= STRBUF_INIT
;
90 for (p
= packed_git
; p
; p
= p
->next
) {
93 if (open_pack_index(p
))
95 packed
+= p
->num_objects
;
96 size_pack
+= p
->pack_size
+ p
->index_size
;
100 if (human_readable
) {
101 strbuf_humanise_bytes(&loose_buf
, loose_size
);
102 strbuf_humanise_bytes(&pack_buf
, size_pack
);
103 strbuf_humanise_bytes(&garbage_buf
, size_garbage
);
105 strbuf_addf(&loose_buf
, "%lu",
106 (unsigned long)(loose_size
/ 1024));
107 strbuf_addf(&pack_buf
, "%lu",
108 (unsigned long)(size_pack
/ 1024));
109 strbuf_addf(&garbage_buf
, "%lu",
110 (unsigned long)(size_garbage
/ 1024));
113 printf("count: %lu\n", loose
);
114 printf("size: %s\n", loose_buf
.buf
);
115 printf("in-pack: %lu\n", packed
);
116 printf("packs: %lu\n", num_pack
);
117 printf("size-pack: %s\n", pack_buf
.buf
);
118 printf("prune-packable: %lu\n", packed_loose
);
119 printf("garbage: %lu\n", garbage
);
120 printf("size-garbage: %s\n", garbage_buf
.buf
);
121 strbuf_release(&loose_buf
);
122 strbuf_release(&pack_buf
);
123 strbuf_release(&garbage_buf
);
125 struct strbuf buf
= STRBUF_INIT
;
127 strbuf_humanise_bytes(&buf
, loose_size
);
129 strbuf_addf(&buf
, "%lu kilobytes",
130 (unsigned long)(loose_size
/ 1024));
131 printf("%lu objects, %s\n", loose
, buf
.buf
);
132 strbuf_release(&buf
);