Initial bulk commit for "Git on MSys"
[msysgit/historical-msysgit.git] / git / builtin-count-objects.c
bloba57000869dd9e2f8b387f338a91fa4bb05fbe1ad
1 /*
2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
5 */
7 #include "cache.h"
8 #include "builtin.h"
10 static const char count_objects_usage[] = "git-count-objects [-v]";
12 static void count_objects(DIR *d, char *path, int len, int verbose,
13 unsigned long *loose,
14 unsigned long *loose_size,
15 unsigned long *packed_loose,
16 unsigned long *garbage)
18 struct dirent *ent;
19 while ((ent = readdir(d)) != NULL) {
20 char hex[41];
21 unsigned char sha1[20];
22 const char *cp;
23 int bad = 0;
25 if ((ent->d_name[0] == '.') &&
26 (ent->d_name[1] == 0 ||
27 ((ent->d_name[1] == '.') && (ent->d_name[2] == 0))))
28 continue;
29 for (cp = ent->d_name; *cp; cp++) {
30 int ch = *cp;
31 if (('0' <= ch && ch <= '9') ||
32 ('a' <= ch && ch <= 'f'))
33 continue;
34 bad = 1;
35 break;
37 if (cp - ent->d_name != 38)
38 bad = 1;
39 else {
40 struct stat st;
41 memcpy(path + len + 3, ent->d_name, 38);
42 path[len + 2] = '/';
43 path[len + 41] = 0;
44 if (lstat(path, &st) || !S_ISREG(st.st_mode))
45 bad = 1;
46 else
47 #ifndef NO_ST_BLOCKS
48 (*loose_size) += xsize_t(st.st_blocks);
49 #else
50 (*loose_size) += xsize_t((st.st_size+511)/512);
51 #endif
53 if (bad) {
54 if (verbose) {
55 error("garbage found: %.*s/%s",
56 len + 2, path, ent->d_name);
57 (*garbage)++;
59 continue;
61 (*loose)++;
62 if (!verbose)
63 continue;
64 memcpy(hex, path+len, 2);
65 memcpy(hex+2, ent->d_name, 38);
66 hex[40] = 0;
67 if (get_sha1_hex(hex, sha1))
68 die("internal error");
69 if (has_sha1_pack(sha1, NULL))
70 (*packed_loose)++;
74 int cmd_count_objects(int ac, const char **av, const char *prefix)
76 int i;
77 int verbose = 0;
78 const char *objdir = get_object_directory();
79 int len = strlen(objdir);
80 char *path = xmalloc(len + 50);
81 unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
82 unsigned long loose_size = 0;
84 for (i = 1; i < ac; i++) {
85 const char *arg = av[i];
86 if (*arg != '-')
87 break;
88 else if (!strcmp(arg, "-v"))
89 verbose = 1;
90 else
91 usage(count_objects_usage);
94 /* we do not take arguments other than flags for now */
95 if (i < ac)
96 usage(count_objects_usage);
97 memcpy(path, objdir, len);
98 if (len && objdir[len-1] != '/')
99 path[len++] = '/';
100 for (i = 0; i < 256; i++) {
101 DIR *d;
102 sprintf(path + len, "%02x", i);
103 d = opendir(path);
104 if (!d)
105 continue;
106 count_objects(d, path, len, verbose,
107 &loose, &loose_size, &packed_loose, &garbage);
108 closedir(d);
110 if (verbose) {
111 struct packed_git *p;
112 unsigned long num_pack = 0;
113 if (!packed_git)
114 prepare_packed_git();
115 for (p = packed_git; p; p = p->next) {
116 if (!p->pack_local)
117 continue;
118 if (open_pack_index(p))
119 continue;
120 packed += p->num_objects;
121 num_pack++;
123 printf("count: %lu\n", loose);
124 printf("size: %lu\n", loose_size / 2);
125 printf("in-pack: %lu\n", packed);
126 printf("packs: %lu\n", num_pack);
127 printf("prune-packable: %lu\n", packed_loose);
128 printf("garbage: %lu\n", garbage);
130 else
131 printf("%lu objects, %lu kilobytes\n",
132 loose, loose_size / 2);
133 return 0;