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 void real_report_garbage(const char *desc
, const char *path
)
19 size_garbage
+= st
.st_size
;
20 warning("%s: %s", desc
, path
);
24 static void count_objects(DIR *d
, char *path
, int len
, int verbose
,
27 unsigned long *packed_loose
)
30 while ((ent
= readdir(d
)) != NULL
) {
32 unsigned char sha1
[20];
36 if (is_dot_or_dotdot(ent
->d_name
))
38 for (cp
= ent
->d_name
; *cp
; cp
++) {
40 if (('0' <= ch
&& ch
<= '9') ||
41 ('a' <= ch
&& ch
<= 'f'))
46 if (cp
- ent
->d_name
!= 38)
50 memcpy(path
+ len
+ 3, ent
->d_name
, 38);
53 if (lstat(path
, &st
) || !S_ISREG(st
.st_mode
))
56 (*loose_size
) += xsize_t(on_disk_bytes(st
));
60 struct strbuf sb
= STRBUF_INIT
;
61 strbuf_addf(&sb
, "%.*s/%s",
62 len
+ 2, path
, ent
->d_name
);
63 report_garbage("garbage found", sb
.buf
);
71 memcpy(hex
, path
+len
, 2);
72 memcpy(hex
+2, ent
->d_name
, 38);
74 if (get_sha1_hex(hex
, sha1
))
75 die("internal error");
76 if (has_sha1_pack(sha1
))
81 static char const * const count_objects_usage
[] = {
82 N_("git count-objects [-v] [-H | --human-readable]"),
86 int cmd_count_objects(int argc
, const char **argv
, const char *prefix
)
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);
92 unsigned long loose
= 0, packed
= 0, packed_loose
= 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")),
101 argc
= parse_options(argc
, argv
, prefix
, opts
, count_objects_usage
, 0);
102 /* we do not take arguments other than flags for now */
104 usage_with_options(count_objects_usage
, opts
);
106 report_garbage
= real_report_garbage
;
107 report_linked_checkout_garbage();
109 memcpy(path
, objdir
, len
);
110 if (len
&& objdir
[len
-1] != '/')
112 for (i
= 0; i
< 256; i
++) {
114 sprintf(path
+ len
, "%02x", i
);
118 count_objects(d
, path
, len
, verbose
,
119 &loose
, &loose_size
, &packed_loose
);
123 struct packed_git
*p
;
124 unsigned long num_pack
= 0;
126 struct strbuf loose_buf
= STRBUF_INIT
;
127 struct strbuf pack_buf
= STRBUF_INIT
;
128 struct strbuf garbage_buf
= STRBUF_INIT
;
130 prepare_packed_git();
131 for (p
= packed_git
; p
; p
= p
->next
) {
134 if (open_pack_index(p
))
136 packed
+= p
->num_objects
;
137 size_pack
+= p
->pack_size
+ p
->index_size
;
141 if (human_readable
) {
142 strbuf_humanise_bytes(&loose_buf
, loose_size
);
143 strbuf_humanise_bytes(&pack_buf
, size_pack
);
144 strbuf_humanise_bytes(&garbage_buf
, size_garbage
);
146 strbuf_addf(&loose_buf
, "%lu",
147 (unsigned long)(loose_size
/ 1024));
148 strbuf_addf(&pack_buf
, "%lu",
149 (unsigned long)(size_pack
/ 1024));
150 strbuf_addf(&garbage_buf
, "%lu",
151 (unsigned long)(size_garbage
/ 1024));
154 printf("count: %lu\n", loose
);
155 printf("size: %s\n", loose_buf
.buf
);
156 printf("in-pack: %lu\n", packed
);
157 printf("packs: %lu\n", num_pack
);
158 printf("size-pack: %s\n", pack_buf
.buf
);
159 printf("prune-packable: %lu\n", packed_loose
);
160 printf("garbage: %lu\n", garbage
);
161 printf("size-garbage: %s\n", garbage_buf
.buf
);
162 strbuf_release(&loose_buf
);
163 strbuf_release(&pack_buf
);
164 strbuf_release(&garbage_buf
);
166 struct strbuf buf
= STRBUF_INIT
;
168 strbuf_humanise_bytes(&buf
, loose_size
);
170 strbuf_addf(&buf
, "%lu kilobytes",
171 (unsigned long)(loose_size
/ 1024));
172 printf("%lu objects, %s\n", loose
, buf
.buf
);
173 strbuf_release(&buf
);