2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 static const char count_objects_usage
[] = "git-count-objects [-v]";
12 static void count_objects(DIR *d
, char *path
, int len
, int verbose
,
14 unsigned long *loose_size
,
15 unsigned long *packed_loose
,
16 unsigned long *garbage
)
19 while ((ent
= readdir(d
)) != NULL
) {
21 unsigned char sha1
[20];
25 if ((ent
->d_name
[0] == '.') &&
26 (ent
->d_name
[1] == 0 ||
27 ((ent
->d_name
[1] == '.') && (ent
->d_name
[2] == 0))))
29 for (cp
= ent
->d_name
; *cp
; cp
++) {
31 if (('0' <= ch
&& ch
<= '9') ||
32 ('a' <= ch
&& ch
<= 'f'))
37 if (cp
- ent
->d_name
!= 38)
41 memcpy(path
+ len
+ 3, ent
->d_name
, 38);
44 if (lstat(path
, &st
) || !S_ISREG(st
.st_mode
))
47 (*loose_size
) += xsize_t(st
.st_blocks
);
51 error("garbage found: %.*s/%s",
52 len
+ 2, path
, ent
->d_name
);
60 memcpy(hex
, path
+len
, 2);
61 memcpy(hex
+2, ent
->d_name
, 38);
63 if (get_sha1_hex(hex
, sha1
))
64 die("internal error");
65 if (has_sha1_pack(sha1
, NULL
))
70 int cmd_count_objects(int ac
, const char **av
, const char *prefix
)
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;
80 for (i
= 1; i
< ac
; i
++) {
81 const char *arg
= av
[i
];
84 else if (!strcmp(arg
, "-v"))
87 usage(count_objects_usage
);
90 /* we do not take arguments other than flags for now */
92 usage(count_objects_usage
);
93 memcpy(path
, objdir
, len
);
94 if (len
&& objdir
[len
-1] != '/')
96 for (i
= 0; i
< 256; i
++) {
98 sprintf(path
+ len
, "%02x", i
);
102 count_objects(d
, path
, len
, verbose
,
103 &loose
, &loose_size
, &packed_loose
, &garbage
);
107 struct packed_git
*p
;
108 unsigned long num_pack
= 0;
110 prepare_packed_git();
111 for (p
= packed_git
; p
; p
= p
->next
) {
114 if (!p
->index_data
&& open_pack_index(p
))
116 packed
+= p
->num_objects
;
119 printf("count: %lu\n", loose
);
120 printf("size: %lu\n", loose_size
/ 2);
121 printf("in-pack: %lu\n", packed
);
122 printf("packs: %lu\n", num_pack
);
123 printf("prune-packable: %lu\n", packed_loose
);
124 printf("garbage: %lu\n", garbage
);
127 printf("%lu objects, %lu kilobytes\n",
128 loose
, loose_size
/ 2);