packfile: allow find_pack_entry to handle arbitrary repositories
[alt-git.git] / builtin / count-objects.c
blobea8bd9e2e27166aff8c408440ed9293fd3da9e4f
1 /*
2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
5 */
7 #include "cache.h"
8 #include "config.h"
9 #include "dir.h"
10 #include "repository.h"
11 #include "builtin.h"
12 #include "parse-options.h"
13 #include "quote.h"
14 #include "packfile.h"
15 #include "object-store.h"
17 static unsigned long garbage;
18 static off_t size_garbage;
19 static int verbose;
20 static unsigned long loose, packed, packed_loose;
21 static off_t loose_size;
23 static const char *bits_to_msg(unsigned seen_bits)
25 switch (seen_bits) {
26 case 0:
27 return "no corresponding .idx or .pack";
28 case PACKDIR_FILE_GARBAGE:
29 return "garbage found";
30 case PACKDIR_FILE_PACK:
31 return "no corresponding .idx";
32 case PACKDIR_FILE_IDX:
33 return "no corresponding .pack";
34 case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
35 default:
36 return NULL;
40 static void real_report_garbage(unsigned seen_bits, const char *path)
42 struct stat st;
43 const char *desc = bits_to_msg(seen_bits);
45 if (!desc)
46 return;
48 if (!stat(path, &st))
49 size_garbage += st.st_size;
50 warning("%s: %s", desc, path);
51 garbage++;
54 static void loose_garbage(const char *path)
56 if (verbose)
57 report_garbage(PACKDIR_FILE_GARBAGE, path);
60 static int count_loose(const struct object_id *oid, const char *path, void *data)
62 struct stat st;
64 if (lstat(path, &st) || !S_ISREG(st.st_mode))
65 loose_garbage(path);
66 else {
67 loose_size += on_disk_bytes(st);
68 loose++;
69 if (verbose && has_sha1_pack(oid->hash))
70 packed_loose++;
72 return 0;
75 static int count_cruft(const char *basename, const char *path, void *data)
77 loose_garbage(path);
78 return 0;
81 static int print_alternate(struct alternate_object_database *alt, void *data)
83 printf("alternate: ");
84 quote_c_style(alt->path, NULL, stdout, 0);
85 putchar('\n');
86 return 0;
89 static char const * const count_objects_usage[] = {
90 N_("git count-objects [-v] [-H | --human-readable]"),
91 NULL
94 int cmd_count_objects(int argc, const char **argv, const char *prefix)
96 int human_readable = 0;
97 struct option opts[] = {
98 OPT__VERBOSE(&verbose, N_("be verbose")),
99 OPT_BOOL('H', "human-readable", &human_readable,
100 N_("print sizes in human readable format")),
101 OPT_END(),
104 git_config(git_default_config, NULL);
106 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
107 /* we do not take arguments other than flags for now */
108 if (argc)
109 usage_with_options(count_objects_usage, opts);
110 if (verbose) {
111 report_garbage = real_report_garbage;
112 report_linked_checkout_garbage();
115 for_each_loose_file_in_objdir(get_object_directory(),
116 count_loose, count_cruft, NULL, NULL);
118 if (verbose) {
119 struct packed_git *p;
120 unsigned long num_pack = 0;
121 off_t size_pack = 0;
122 struct strbuf loose_buf = STRBUF_INIT;
123 struct strbuf pack_buf = STRBUF_INIT;
124 struct strbuf garbage_buf = STRBUF_INIT;
125 if (!get_packed_git(the_repository))
126 prepare_packed_git(the_repository);
127 for (p = get_packed_git(the_repository); p; p = p->next) {
128 if (!p->pack_local)
129 continue;
130 if (open_pack_index(p))
131 continue;
132 packed += p->num_objects;
133 size_pack += p->pack_size + p->index_size;
134 num_pack++;
137 if (human_readable) {
138 strbuf_humanise_bytes(&loose_buf, loose_size);
139 strbuf_humanise_bytes(&pack_buf, size_pack);
140 strbuf_humanise_bytes(&garbage_buf, size_garbage);
141 } else {
142 strbuf_addf(&loose_buf, "%lu",
143 (unsigned long)(loose_size / 1024));
144 strbuf_addf(&pack_buf, "%lu",
145 (unsigned long)(size_pack / 1024));
146 strbuf_addf(&garbage_buf, "%lu",
147 (unsigned long)(size_garbage / 1024));
150 printf("count: %lu\n", loose);
151 printf("size: %s\n", loose_buf.buf);
152 printf("in-pack: %lu\n", packed);
153 printf("packs: %lu\n", num_pack);
154 printf("size-pack: %s\n", pack_buf.buf);
155 printf("prune-packable: %lu\n", packed_loose);
156 printf("garbage: %lu\n", garbage);
157 printf("size-garbage: %s\n", garbage_buf.buf);
158 foreach_alt_odb(print_alternate, NULL);
159 strbuf_release(&loose_buf);
160 strbuf_release(&pack_buf);
161 strbuf_release(&garbage_buf);
162 } else {
163 struct strbuf buf = STRBUF_INIT;
164 if (human_readable)
165 strbuf_humanise_bytes(&buf, loose_size);
166 else
167 strbuf_addf(&buf, "%lu kilobytes",
168 (unsigned long)(loose_size / 1024));
169 printf("%lu objects, %s\n", loose, buf.buf);
170 strbuf_release(&buf);
172 return 0;