2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 #include "parse-options.h"
12 static unsigned long garbage;
14 static void real_report_garbage(const char *desc, const char *path)
16 warning("%s: %s", desc, path);
20 static void count_objects(DIR *d, char *path, int len, int verbose,
23 unsigned long *packed_loose,
24 unsigned long *garbage)
27 while ((ent = readdir(d)) != NULL) {
29 unsigned char sha1[20];
33 if (is_dot_or_dotdot(ent->d_name))
35 for (cp = ent->d_name; *cp; cp++) {
37 if (('0' <= ch && ch <= '9') ||
38 ('a' <= ch && ch <= 'f'))
43 if (cp - ent->d_name != 38)
47 memcpy(path + len + 3, ent->d_name, 38);
50 if (lstat(path, &st) || !S_ISREG(st.st_mode))
53 (*loose_size) += xsize_t(on_disk_bytes(st));
57 error("garbage found: %.*s/%s",
58 len + 2, path, ent->d_name);
66 memcpy(hex, path+len, 2);
67 memcpy(hex+2, ent->d_name, 38);
69 if (get_sha1_hex(hex, sha1))
70 die("internal error");
71 if (has_sha1_pack(sha1))
76 static char const * const count_objects_usage[] = {
77 N_("git count-objects [-v]"),
81 int cmd_count_objects(int argc, const char **argv, const char *prefix)
84 const char *objdir = get_object_directory();
85 int len = strlen(objdir);
86 char *path = xmalloc(len + 50);
87 unsigned long loose = 0, packed = 0, packed_loose = 0;
89 struct option opts[] = {
90 OPT__VERBOSE(&verbose, N_("be verbose")),
94 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
95 /* we do not take arguments other than flags for now */
97 usage_with_options(count_objects_usage, opts);
99 report_garbage = real_report_garbage;
100 memcpy(path, objdir, len);
101 if (len && objdir[len-1] != '/')
103 for (i = 0; i < 256; i++) {
105 sprintf(path + len, "%02x", i);
109 count_objects(d, path, len, verbose,
110 &loose, &loose_size, &packed_loose, &garbage);
114 struct packed_git *p;
115 unsigned long num_pack = 0;
118 prepare_packed_git();
119 for (p = packed_git; p; p = p->next) {
122 if (open_pack_index(p))
124 packed += p->num_objects;
125 size_pack += p->pack_size + p->index_size;
128 printf("count: %lu\n", loose);
129 printf("size: %lu\n", (unsigned long) (loose_size / 1024));
130 printf("in-pack: %lu\n", packed);
131 printf("packs: %lu\n", num_pack);
132 printf("size-pack: %lu\n", (unsigned long) (size_pack / 1024));
133 printf("prune-packable: %lu\n", packed_loose);
134 printf("garbage: %lu\n", garbage);
137 printf("%lu objects, %lu kilobytes\n",
138 loose, (unsigned long) (loose_size / 1024));