2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 #include "parse-options.h"
13 static unsigned long garbage;
14 static off_t size_garbage;
16 static unsigned long loose, packed, packed_loose;
17 static off_t loose_size;
19 static const char *bits_to_msg(unsigned seen_bits)
23 return "no corresponding .idx or .pack";
24 case PACKDIR_FILE_GARBAGE:
25 return "garbage found";
26 case PACKDIR_FILE_PACK:
27 return "no corresponding .idx";
28 case PACKDIR_FILE_IDX:
29 return "no corresponding .pack";
30 case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
36 static void real_report_garbage(unsigned seen_bits, const char *path)
39 const char *desc = bits_to_msg(seen_bits);
45 size_garbage += st.st_size;
46 warning("%s: %s", desc, path);
50 static void loose_garbage(const char *path)
53 report_garbage(PACKDIR_FILE_GARBAGE, path);
56 static int count_loose(const unsigned char *sha1, const char *path, void *data)
60 if (lstat(path, &st) || !S_ISREG(st.st_mode))
63 loose_size += on_disk_bytes(st);
65 if (verbose && has_sha1_pack(sha1))
71 static int count_cruft(const char *basename, const char *path, void *data)
77 static int print_alternate(struct alternate_object_database *alt, void *data)
79 printf("alternate: ");
80 quote_c_style(alt->path, NULL, stdout, 0);
85 static char const * const count_objects_usage[] = {
86 N_("git count-objects [-v] [-H | --human-readable]"),
90 int cmd_count_objects(int argc, const char **argv, const char *prefix)
92 int human_readable = 0;
93 struct option opts[] = {
94 OPT__VERBOSE(&verbose, N_("be verbose")),
95 OPT_BOOL('H', "human-readable", &human_readable,
96 N_("print sizes in human readable format")),
100 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
101 /* we do not take arguments other than flags for now */
103 usage_with_options(count_objects_usage, opts);
105 report_garbage = real_report_garbage;
106 report_linked_checkout_garbage();
109 for_each_loose_file_in_objdir(get_object_directory(),
110 count_loose, count_cruft, NULL, NULL);
113 struct packed_git *p;
114 unsigned long num_pack = 0;
116 struct strbuf loose_buf = STRBUF_INIT;
117 struct strbuf pack_buf = STRBUF_INIT;
118 struct strbuf garbage_buf = STRBUF_INIT;
120 prepare_packed_git();
121 for (p = packed_git; p; p = p->next) {
124 if (open_pack_index(p))
126 packed += p->num_objects;
127 size_pack += p->pack_size + p->index_size;
131 if (human_readable) {
132 strbuf_humanise_bytes(&loose_buf, loose_size);
133 strbuf_humanise_bytes(&pack_buf, size_pack);
134 strbuf_humanise_bytes(&garbage_buf, size_garbage);
136 strbuf_addf(&loose_buf, "%lu",
137 (unsigned long)(loose_size / 1024));
138 strbuf_addf(&pack_buf, "%lu",
139 (unsigned long)(size_pack / 1024));
140 strbuf_addf(&garbage_buf, "%lu",
141 (unsigned long)(size_garbage / 1024));
144 printf("count: %lu\n", loose);
145 printf("size: %s\n", loose_buf.buf);
146 printf("in-pack: %lu\n", packed);
147 printf("packs: %lu\n", num_pack);
148 printf("size-pack: %s\n", pack_buf.buf);
149 printf("prune-packable: %lu\n", packed_loose);
150 printf("garbage: %lu\n", garbage);
151 printf("size-garbage: %s\n", garbage_buf.buf);
152 foreach_alt_odb(print_alternate, NULL);
153 strbuf_release(&loose_buf);
154 strbuf_release(&pack_buf);
155 strbuf_release(&garbage_buf);
157 struct strbuf buf = STRBUF_INIT;
159 strbuf_humanise_bytes(&buf, loose_size);
161 strbuf_addf(&buf, "%lu kilobytes",
162 (unsigned long)(loose_size / 1024));
163 printf("%lu objects, %s\n", loose, buf.buf);
164 strbuf_release(&buf);