object-store: free alt_odb_list
[alt-git.git] / builtin / count-objects.c
blobced8958e43db8fe2d50b07b81e69f1854fcad46f
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 "builtin.h"
11 #include "parse-options.h"
12 #include "quote.h"
13 #include "packfile.h"
14 #include "object-store.h"
16 static unsigned long garbage;
17 static off_t size_garbage;
18 static int verbose;
19 static unsigned long loose, packed, packed_loose;
20 static off_t loose_size;
22 static const char *bits_to_msg(unsigned seen_bits)
24 switch (seen_bits) {
25 case 0:
26 return "no corresponding .idx or .pack";
27 case PACKDIR_FILE_GARBAGE:
28 return "garbage found";
29 case PACKDIR_FILE_PACK:
30 return "no corresponding .idx";
31 case PACKDIR_FILE_IDX:
32 return "no corresponding .pack";
33 case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
34 default:
35 return NULL;
39 static void real_report_garbage(unsigned seen_bits, const char *path)
41 struct stat st;
42 const char *desc = bits_to_msg(seen_bits);
44 if (!desc)
45 return;
47 if (!stat(path, &st))
48 size_garbage += st.st_size;
49 warning("%s: %s", desc, path);
50 garbage++;
53 static void loose_garbage(const char *path)
55 if (verbose)
56 report_garbage(PACKDIR_FILE_GARBAGE, path);
59 static int count_loose(const struct object_id *oid, const char *path, void *data)
61 struct stat st;
63 if (lstat(path, &st) || !S_ISREG(st.st_mode))
64 loose_garbage(path);
65 else {
66 loose_size += on_disk_bytes(st);
67 loose++;
68 if (verbose && has_sha1_pack(oid->hash))
69 packed_loose++;
71 return 0;
74 static int count_cruft(const char *basename, const char *path, void *data)
76 loose_garbage(path);
77 return 0;
80 static int print_alternate(struct alternate_object_database *alt, void *data)
82 printf("alternate: ");
83 quote_c_style(alt->path, NULL, stdout, 0);
84 putchar('\n');
85 return 0;
88 static char const * const count_objects_usage[] = {
89 N_("git count-objects [-v] [-H | --human-readable]"),
90 NULL
93 int cmd_count_objects(int argc, const char **argv, const char *prefix)
95 int human_readable = 0;
96 struct option opts[] = {
97 OPT__VERBOSE(&verbose, N_("be verbose")),
98 OPT_BOOL('H', "human-readable", &human_readable,
99 N_("print sizes in human readable format")),
100 OPT_END(),
103 git_config(git_default_config, NULL);
105 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
106 /* we do not take arguments other than flags for now */
107 if (argc)
108 usage_with_options(count_objects_usage, opts);
109 if (verbose) {
110 report_garbage = real_report_garbage;
111 report_linked_checkout_garbage();
114 for_each_loose_file_in_objdir(get_object_directory(),
115 count_loose, count_cruft, NULL, NULL);
117 if (verbose) {
118 struct packed_git *p;
119 unsigned long num_pack = 0;
120 off_t size_pack = 0;
121 struct strbuf loose_buf = STRBUF_INIT;
122 struct strbuf pack_buf = STRBUF_INIT;
123 struct strbuf garbage_buf = STRBUF_INIT;
124 if (!packed_git)
125 prepare_packed_git();
126 for (p = packed_git; p; p = p->next) {
127 if (!p->pack_local)
128 continue;
129 if (open_pack_index(p))
130 continue;
131 packed += p->num_objects;
132 size_pack += p->pack_size + p->index_size;
133 num_pack++;
136 if (human_readable) {
137 strbuf_humanise_bytes(&loose_buf, loose_size);
138 strbuf_humanise_bytes(&pack_buf, size_pack);
139 strbuf_humanise_bytes(&garbage_buf, size_garbage);
140 } else {
141 strbuf_addf(&loose_buf, "%lu",
142 (unsigned long)(loose_size / 1024));
143 strbuf_addf(&pack_buf, "%lu",
144 (unsigned long)(size_pack / 1024));
145 strbuf_addf(&garbage_buf, "%lu",
146 (unsigned long)(size_garbage / 1024));
149 printf("count: %lu\n", loose);
150 printf("size: %s\n", loose_buf.buf);
151 printf("in-pack: %lu\n", packed);
152 printf("packs: %lu\n", num_pack);
153 printf("size-pack: %s\n", pack_buf.buf);
154 printf("prune-packable: %lu\n", packed_loose);
155 printf("garbage: %lu\n", garbage);
156 printf("size-garbage: %s\n", garbage_buf.buf);
157 foreach_alt_odb(print_alternate, NULL);
158 strbuf_release(&loose_buf);
159 strbuf_release(&pack_buf);
160 strbuf_release(&garbage_buf);
161 } else {
162 struct strbuf buf = STRBUF_INIT;
163 if (human_readable)
164 strbuf_humanise_bytes(&buf, loose_size);
165 else
166 strbuf_addf(&buf, "%lu kilobytes",
167 (unsigned long)(loose_size / 1024));
168 printf("%lu objects, %s\n", loose, buf.buf);
169 strbuf_release(&buf);
171 return 0;