Merge branch 'jk/rm-removed-paths'
[git/jnareb-git.git] / builtin / count-objects.c
blob3a01a8d08591d93426d436ce2e1e17fccd3ebf78
1 /*
2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
5 */
7 #include "cache.h"
8 #include "dir.h"
9 #include "builtin.h"
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)
17 struct stat st;
18 if (!stat(path, &st))
19 size_garbage += st.st_size;
20 warning("%s: %s", desc, path);
21 garbage++;
24 static void count_objects(DIR *d, char *path, int len, int verbose,
25 unsigned long *loose,
26 off_t *loose_size,
27 unsigned long *packed_loose)
29 struct dirent *ent;
30 while ((ent = readdir(d)) != NULL) {
31 char hex[41];
32 unsigned char sha1[20];
33 const char *cp;
34 int bad = 0;
36 if (is_dot_or_dotdot(ent->d_name))
37 continue;
38 for (cp = ent->d_name; *cp; cp++) {
39 int ch = *cp;
40 if (('0' <= ch && ch <= '9') ||
41 ('a' <= ch && ch <= 'f'))
42 continue;
43 bad = 1;
44 break;
46 if (cp - ent->d_name != 38)
47 bad = 1;
48 else {
49 struct stat st;
50 memcpy(path + len + 3, ent->d_name, 38);
51 path[len + 2] = '/';
52 path[len + 41] = 0;
53 if (lstat(path, &st) || !S_ISREG(st.st_mode))
54 bad = 1;
55 else
56 (*loose_size) += xsize_t(on_disk_bytes(st));
58 if (bad) {
59 if (verbose) {
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);
64 strbuf_release(&sb);
66 continue;
68 (*loose)++;
69 if (!verbose)
70 continue;
71 memcpy(hex, path+len, 2);
72 memcpy(hex+2, ent->d_name, 38);
73 hex[40] = 0;
74 if (get_sha1_hex(hex, sha1))
75 die("internal error");
76 if (has_sha1_pack(sha1))
77 (*packed_loose)++;
81 static char const * const count_objects_usage[] = {
82 N_("git count-objects [-v]"),
83 NULL
86 int cmd_count_objects(int argc, const char **argv, const char *prefix)
88 int i, verbose = 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;
93 off_t loose_size = 0;
94 struct option opts[] = {
95 OPT__VERBOSE(&verbose, N_("be verbose")),
96 OPT_END(),
99 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
100 /* we do not take arguments other than flags for now */
101 if (argc)
102 usage_with_options(count_objects_usage, opts);
103 if (verbose)
104 report_garbage = real_report_garbage;
105 memcpy(path, objdir, len);
106 if (len && objdir[len-1] != '/')
107 path[len++] = '/';
108 for (i = 0; i < 256; i++) {
109 DIR *d;
110 sprintf(path + len, "%02x", i);
111 d = opendir(path);
112 if (!d)
113 continue;
114 count_objects(d, path, len, verbose,
115 &loose, &loose_size, &packed_loose);
116 closedir(d);
118 if (verbose) {
119 struct packed_git *p;
120 unsigned long num_pack = 0;
121 off_t size_pack = 0;
122 if (!packed_git)
123 prepare_packed_git();
124 for (p = packed_git; p; p = p->next) {
125 if (!p->pack_local)
126 continue;
127 if (open_pack_index(p))
128 continue;
129 packed += p->num_objects;
130 size_pack += p->pack_size + p->index_size;
131 num_pack++;
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));
142 else
143 printf("%lu objects, %lu kilobytes\n",
144 loose, (unsigned long) (loose_size / 1024));
145 return 0;