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]"),
86 int cmd_count_objects(int argc
, const char **argv
, const char *prefix
)
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")),
99 argc
= parse_options(argc
, argv
, prefix
, opts
, count_objects_usage
, 0);
100 /* we do not take arguments other than flags for now */
102 usage_with_options(count_objects_usage
, opts
);
104 report_garbage
= real_report_garbage
;
105 memcpy(path
, objdir
, len
);
106 if (len
&& objdir
[len
-1] != '/')
108 for (i
= 0; i
< 256; i
++) {
110 sprintf(path
+ len
, "%02x", i
);
114 count_objects(d
, path
, len
, verbose
,
115 &loose
, &loose_size
, &packed_loose
);
119 struct packed_git
*p
;
120 unsigned long num_pack
= 0;
123 prepare_packed_git();
124 for (p
= packed_git
; p
; p
= p
->next
) {
127 if (open_pack_index(p
))
129 packed
+= p
->num_objects
;
130 size_pack
+= p
->pack_size
+ p
->index_size
;
133 printf("count: %lu\n", loose
);
134 printf("size: %lu\n", (unsigned long) (loose_size
/ 1024));
135 printf("in-pack: %lu\n", packed
);
136 printf("packs: %lu\n", num_pack
);
137 printf("size-pack: %lu\n", (unsigned long) (size_pack
/ 1024));
138 printf("prune-packable: %lu\n", packed_loose
);
139 printf("garbage: %lu\n", garbage
);
140 printf("size-garbage: %lu\n", (unsigned long) (size_garbage
/ 1024));
143 printf("%lu objects, %lu kilobytes\n",
144 loose
, (unsigned long) (loose_size
/ 1024));