2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
9 #include "parse-options.h"
11 static void count_objects(DIR *d, char *path, int len, int verbose,
13 unsigned long *loose_size,
14 unsigned long *packed_loose,
15 unsigned long *garbage)
18 while ((ent = readdir(d)) != NULL) {
20 unsigned char sha1[20];
24 if ((ent->d_name[0] == '.') &&
25 (ent->d_name[1] == 0 ||
26 ((ent->d_name[1] == '.') && (ent->d_name[2] == 0))))
28 for (cp = ent->d_name; *cp; cp++) {
30 if (('0' <= ch && ch <= '9') ||
31 ('a' <= ch && ch <= 'f'))
36 if (cp - ent->d_name != 38)
40 memcpy(path + len + 3, ent->d_name, 38);
43 if (lstat(path, &st) || !S_ISREG(st.st_mode))
46 (*loose_size) += xsize_t(on_disk_bytes(st));
50 error("garbage found: %.*s/%s",
51 len + 2, path, ent->d_name);
59 memcpy(hex, path+len, 2);
60 memcpy(hex+2, ent->d_name, 38);
62 if (get_sha1_hex(hex, sha1))
63 die("internal error");
64 if (has_sha1_pack(sha1, NULL))
69 static char const * const count_objects_usage[] = {
70 "git count-objects [-v]",
74 int cmd_count_objects(int argc, const char **argv, const char *prefix)
77 const char *objdir = get_object_directory();
78 int len = strlen(objdir);
79 char *path = xmalloc(len + 50);
80 unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
81 unsigned long loose_size = 0;
82 struct option opts[] = {
83 OPT__VERBOSE(&verbose),
87 argc = parse_options(argc, argv, opts, count_objects_usage, 0);
88 /* we do not take arguments other than flags for now */
90 usage_with_options(count_objects_usage, opts);
91 memcpy(path, objdir, len);
92 if (len && objdir[len-1] != '/')
94 for (i = 0; i < 256; i++) {
96 sprintf(path + len, "%02x", i);
100 count_objects(d, path, len, verbose,
101 &loose, &loose_size, &packed_loose, &garbage);
105 struct packed_git *p;
106 unsigned long num_pack = 0;
107 unsigned long size_pack = 0;
109 prepare_packed_git();
110 for (p = packed_git; p; p = p->next) {
113 if (open_pack_index(p))
115 packed += p->num_objects;
116 size_pack += p->pack_size + p->index_size;
119 printf("count: %lu\n", loose);
120 printf("size: %lu\n", loose_size / 1024);
121 printf("in-pack: %lu\n", packed);
122 printf("packs: %lu\n", num_pack);
123 printf("size-pack: %lu\n", size_pack / 1024);
124 printf("prune-packable: %lu\n", packed_loose);
125 printf("garbage: %lu\n", garbage);
128 printf("%lu objects, %lu kilobytes\n",
129 loose, loose_size / 2);