2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 #include "parse-options.h"
12 static void count_objects(DIR *d, char *path, int len, int verbose,
15 unsigned long *packed_loose,
16 unsigned long *garbage)
19 while ((ent = readdir(d)) != NULL) {
21 unsigned char sha1[20];
25 if (is_dot_or_dotdot(ent->d_name))
27 for (cp = ent->d_name; *cp; cp++) {
29 if (('0' <= ch && ch <= '9') ||
30 ('a' <= ch && ch <= 'f'))
35 if (cp - ent->d_name != 38)
39 memcpy(path + len + 3, ent->d_name, 38);
42 if (lstat(path, &st) || !S_ISREG(st.st_mode))
45 (*loose_size) += xsize_t(on_disk_bytes(st));
49 error("garbage found: %.*s/%s",
50 len + 2, path, ent->d_name);
58 memcpy(hex, path+len, 2);
59 memcpy(hex+2, ent->d_name, 38);
61 if (get_sha1_hex(hex, sha1))
62 die("internal error");
63 if (has_sha1_pack(sha1))
68 static char const * const count_objects_usage[] = {
69 N_("git count-objects [-v]"),
73 int cmd_count_objects(int argc, const char **argv, const char *prefix)
76 const char *objdir = get_object_directory();
77 int len = strlen(objdir);
78 char *path = xmalloc(len + 50);
79 unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
81 struct option opts[] = {
82 OPT__VERBOSE(&verbose, N_("be verbose")),
86 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
87 /* we do not take arguments other than flags for now */
89 usage_with_options(count_objects_usage, opts);
90 memcpy(path, objdir, len);
91 if (len && objdir[len-1] != '/')
93 for (i = 0; i < 256; i++) {
95 sprintf(path + len, "%02x", i);
99 count_objects(d, path, len, verbose,
100 &loose, &loose_size, &packed_loose, &garbage);
104 struct packed_git *p;
105 unsigned long num_pack = 0;
108 prepare_packed_git();
109 for (p = packed_git; p; p = p->next) {
112 if (open_pack_index(p))
114 packed += p->num_objects;
115 size_pack += p->pack_size + p->index_size;
118 printf("count: %lu\n", loose);
119 printf("size: %lu\n", (unsigned long) (loose_size / 1024));
120 printf("in-pack: %lu\n", packed);
121 printf("packs: %lu\n", num_pack);
122 printf("size-pack: %lu\n", (unsigned long) (size_pack / 1024));
123 printf("prune-packable: %lu\n", packed_loose);
124 printf("garbage: %lu\n", garbage);
127 printf("%lu objects, %lu kilobytes\n",
128 loose, (unsigned long) (loose_size / 1024));