2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
11 #include "parse-options.h"
14 static unsigned long garbage;
15 static off_t size_garbage;
17 static unsigned long loose, packed, packed_loose;
18 static off_t loose_size;
20 static const char *bits_to_msg(unsigned seen_bits)
24 return "no corresponding .idx or .pack";
25 case PACKDIR_FILE_GARBAGE:
26 return "garbage found";
27 case PACKDIR_FILE_PACK:
28 return "no corresponding .idx";
29 case PACKDIR_FILE_IDX:
30 return "no corresponding .pack";
31 case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
37 static void real_report_garbage(unsigned seen_bits, const char *path)
40 const char *desc = bits_to_msg(seen_bits);
46 size_garbage += st.st_size;
47 warning("%s: %s", desc, path);
51 static void loose_garbage(const char *path)
54 report_garbage(PACKDIR_FILE_GARBAGE, path);
57 static int count_loose(const struct object_id *oid, const char *path, void *data)
61 if (lstat(path, &st) || !S_ISREG(st.st_mode))
64 loose_size += on_disk_bytes(st);
66 if (verbose && has_sha1_pack(oid->hash))
72 static int count_cruft(const char *basename, const char *path, void *data)
78 static int print_alternate(struct alternate_object_database *alt, void *data)
80 printf("alternate: ");
81 quote_c_style(alt->path, NULL, stdout, 0);
86 static char const * const count_objects_usage[] = {
87 N_("git count-objects [-v] [-H | --human-readable]"),
91 int cmd_count_objects(int argc, const char **argv, const char *prefix)
93 int human_readable = 0;
94 struct option opts[] = {
95 OPT__VERBOSE(&verbose, N_("be verbose")),
96 OPT_BOOL('H', "human-readable", &human_readable,
97 N_("print sizes in human readable format")),
101 git_config(git_default_config, NULL);
103 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
104 /* we do not take arguments other than flags for now */
106 usage_with_options(count_objects_usage, opts);
108 report_garbage = real_report_garbage;
109 report_linked_checkout_garbage();
112 for_each_loose_file_in_objdir(get_object_directory(),
113 count_loose, count_cruft, NULL, NULL);
116 struct packed_git *p;
117 unsigned long num_pack = 0;
119 struct strbuf loose_buf = STRBUF_INIT;
120 struct strbuf pack_buf = STRBUF_INIT;
121 struct strbuf garbage_buf = STRBUF_INIT;
123 prepare_packed_git();
124 for (p = packed_git; p; p = p->next) {
127 if (open_pack_index(p))
129 packed += p->num_objects;
130 size_pack += p->pack_size + p->index_size;
134 if (human_readable) {
135 strbuf_humanise_bytes(&loose_buf, loose_size);
136 strbuf_humanise_bytes(&pack_buf, size_pack);
137 strbuf_humanise_bytes(&garbage_buf, size_garbage);
139 strbuf_addf(&loose_buf, "%lu",
140 (unsigned long)(loose_size / 1024));
141 strbuf_addf(&pack_buf, "%lu",
142 (unsigned long)(size_pack / 1024));
143 strbuf_addf(&garbage_buf, "%lu",
144 (unsigned long)(size_garbage / 1024));
147 printf("count: %lu\n", loose);
148 printf("size: %s\n", loose_buf.buf);
149 printf("in-pack: %lu\n", packed);
150 printf("packs: %lu\n", num_pack);
151 printf("size-pack: %s\n", pack_buf.buf);
152 printf("prune-packable: %lu\n", packed_loose);
153 printf("garbage: %lu\n", garbage);
154 printf("size-garbage: %s\n", garbage_buf.buf);
155 foreach_alt_odb(print_alternate, NULL);
156 strbuf_release(&loose_buf);
157 strbuf_release(&pack_buf);
158 strbuf_release(&garbage_buf);
160 struct strbuf buf = STRBUF_INIT;
162 strbuf_humanise_bytes(&buf, loose_size);
164 strbuf_addf(&buf, "%lu kilobytes",
165 (unsigned long)(loose_size / 1024));
166 printf("%lu objects, %s\n", loose, buf.buf);
167 strbuf_release(&buf);