@matheusbsbs / matheus-bagliano / commits / c74320872b
Fork 0

built-in count-objects.

Also it learned to do -v (verbose) to report: - number of loose objects - disk occupied by loose objects - number of objects in local packs - number of loose objects that are also in pack - unrecognised garbage in .git/objects/??/. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 27, 2006 at 15:37 UTC c74320872b445104fe5c265e60785d9d26d94cc5
4 files changed +126 -1
Makefile
+1 -1
@@ -214,7 +214,7 @@ LIB_OBJS = \
214 $(DIFF_OBJS)
215
216 BUILTIN_OBJS = \
217 - builtin-log.o builtin-help.o
217 + builtin-log.o builtin-help.o builtin-count.o
218
219 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
220 LIBS = $(GITLIBS) -lz
builtin-count.c new
+123
@@ -0,0 +1,123 @@
1 +/*
2 + * Builtin "git count-objects".
3 + *
4 + * Copyright (c) 2006 Junio C Hamano
5 + */
6 +
7 +#include "cache.h"
8 +#include "builtin.h"
9 +
10 +static const char count_objects_usage[] = "git-count-objects [-v]";
11 +
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)
17 +{
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;
24 +
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;
36 + }
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 + (*loose_size) += st.st_blocks;
48 + }
49 + if (bad) {
50 + if (verbose) {
51 + error("garbage found: %.*s/%s",
52 + len + 2, path, ent->d_name);
53 + (*garbage)++;
54 + }
55 + continue;
56 + }
57 + (*loose)++;
58 + if (!verbose)
59 + continue;
60 + memcpy(hex, path+len, 2);
61 + memcpy(hex+2, ent->d_name, 38);
62 + hex[40] = 0;
63 + if (get_sha1_hex(hex, sha1))
64 + die("internal error");
65 + if (has_sha1_pack(sha1))
66 + (*packed_loose)++;
67 + }
68 +}
69 +
70 +int cmd_count_objects(int ac, const char **av, char **ep)
71 +{
72 + int i;
73 + int verbose = 0;
74 + const char *objdir = get_object_directory();
75 + int len = strlen(objdir);
76 + char *path = xmalloc(len + 50);
77 + unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
78 + unsigned long loose_size = 0;
79 +
80 + for (i = 1; i < ac; i++) {
81 + const char *arg = av[i];
82 + if (*arg != '-')
83 + break;
84 + else if (!strcmp(arg, "-v"))
85 + verbose = 1;
86 + else
87 + usage(count_objects_usage);
88 + }
89 +
90 + /* we do not take arguments other than flags for now */
91 + if (i < ac)
92 + usage(count_objects_usage);
93 + memcpy(path, objdir, len);
94 + if (len && objdir[len-1] != '/')
95 + path[len++] = '/';
96 + for (i = 0; i < 256; i++) {
97 + DIR *d;
98 + sprintf(path + len, "%02x", i);
99 + d = opendir(path);
100 + if (!d)
101 + continue;
102 + count_objects(d, path, len, verbose,
103 + &loose, &loose_size, &packed_loose, &garbage);
104 + closedir(d);
105 + }
106 + if (verbose) {
107 + struct packed_git *p;
108 + for (p = packed_git; p; p = p->next) {
109 + if (!p->pack_local)
110 + continue;
111 + packed += num_packed_objects(p);
112 + }
113 + printf("count: %lu\n", loose);
114 + printf("size: %lu\n", loose_size / 2);
115 + printf("in-pack: %lu\n", packed);
116 + printf("prune-packable: %lu\n", packed_loose);
117 + printf("garbage: %lu\n", garbage);
118 + }
119 + else
120 + printf("%lu objects, %lu kilobytes\n",
121 + loose, loose_size / 2);
122 + return 0;
123 +}
builtin.h
+1
@@ -19,5 +19,6 @@ extern int cmd_version(int argc, const char **argv, char **envp);
19 extern int cmd_whatchanged(int argc, const char **argv, char **envp);
20 extern int cmd_show(int argc, const char **argv, char **envp);
21 extern int cmd_log(int argc, const char **argv, char **envp);
22 +extern int cmd_count_objects(int argc, const char **argv, char **envp);
23
24 #endif
git.c
+1
@@ -46,6 +46,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
46 { "log", cmd_log },
47 { "whatchanged", cmd_whatchanged },
48 { "show", cmd_show },
49 + { "count-objects", cmd_count_objects },
50 };
51 int i;
52