Merge branch 'am/doc-sha256'
[git.git] / builtin / count-objects.c
blob2d4bb5e8d0a8dfce1290760f7cd99dac7ba39a3e
1 /*
2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
5 */
7 #include "builtin.h"
8 #include "config.h"
9 #include "dir.h"
10 #include "environment.h"
11 #include "gettext.h"
12 #include "path.h"
13 #include "repository.h"
14 #include "parse-options.h"
15 #include "quote.h"
16 #include "packfile.h"
17 #include "object-store-ll.h"
19 static unsigned long garbage;
20 static off_t size_garbage;
21 static int verbose;
22 static unsigned long loose, packed, packed_loose;
23 static off_t loose_size;
25 static const char *bits_to_msg(unsigned seen_bits)
27 switch (seen_bits) {
28 case 0:
29 return "no corresponding .idx or .pack";
30 case PACKDIR_FILE_GARBAGE:
31 return "garbage found";
32 case PACKDIR_FILE_PACK:
33 return "no corresponding .idx";
34 case PACKDIR_FILE_IDX:
35 return "no corresponding .pack";
36 case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
37 default:
38 return NULL;
42 static void real_report_garbage(unsigned seen_bits, const char *path)
44 struct stat st;
45 const char *desc = bits_to_msg(seen_bits);
47 if (!desc)
48 return;
50 if (!stat(path, &st))
51 size_garbage += st.st_size;
52 warning("%s: %s", desc, path);
53 garbage++;
56 static void loose_garbage(const char *path)
58 if (verbose)
59 report_garbage(PACKDIR_FILE_GARBAGE, path);
62 static int count_loose(const struct object_id *oid, const char *path,
63 void *data UNUSED)
65 struct stat st;
67 if (lstat(path, &st) || !S_ISREG(st.st_mode))
68 loose_garbage(path);
69 else {
70 loose_size += on_disk_bytes(st);
71 loose++;
72 if (verbose && has_object_pack(oid))
73 packed_loose++;
75 return 0;
78 static int count_cruft(const char *basename UNUSED, const char *path,
79 void *data UNUSED)
81 loose_garbage(path);
82 return 0;
85 static int print_alternate(struct object_directory *odb, void *data UNUSED)
87 printf("alternate: ");
88 quote_c_style(odb->path, NULL, stdout, 0);
89 putchar('\n');
90 return 0;
93 static char const * const count_objects_usage[] = {
94 "git count-objects [-v] [-H | --human-readable]",
95 NULL
98 int cmd_count_objects(int argc, const char **argv, const char *prefix)
100 int human_readable = 0;
101 struct option opts[] = {
102 OPT__VERBOSE(&verbose, N_("be verbose")),
103 OPT_BOOL('H', "human-readable", &human_readable,
104 N_("print sizes in human readable format")),
105 OPT_END(),
108 git_config(git_default_config, NULL);
110 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
111 /* we do not take arguments other than flags for now */
112 if (argc)
113 usage_with_options(count_objects_usage, opts);
114 if (verbose) {
115 report_garbage = real_report_garbage;
116 report_linked_checkout_garbage();
119 for_each_loose_file_in_objdir(get_object_directory(),
120 count_loose, count_cruft, NULL, NULL);
122 if (verbose) {
123 struct packed_git *p;
124 unsigned long num_pack = 0;
125 off_t size_pack = 0;
126 struct strbuf loose_buf = STRBUF_INIT;
127 struct strbuf pack_buf = STRBUF_INIT;
128 struct strbuf garbage_buf = STRBUF_INIT;
130 for (p = get_all_packs(the_repository); p; p = p->next) {
131 if (!p->pack_local)
132 continue;
133 if (open_pack_index(p))
134 continue;
135 packed += p->num_objects;
136 size_pack += p->pack_size + p->index_size;
137 num_pack++;
140 if (human_readable) {
141 strbuf_humanise_bytes(&loose_buf, loose_size);
142 strbuf_humanise_bytes(&pack_buf, size_pack);
143 strbuf_humanise_bytes(&garbage_buf, size_garbage);
144 } else {
145 strbuf_addf(&loose_buf, "%lu",
146 (unsigned long)(loose_size / 1024));
147 strbuf_addf(&pack_buf, "%lu",
148 (unsigned long)(size_pack / 1024));
149 strbuf_addf(&garbage_buf, "%lu",
150 (unsigned long)(size_garbage / 1024));
153 printf("count: %lu\n", loose);
154 printf("size: %s\n", loose_buf.buf);
155 printf("in-pack: %lu\n", packed);
156 printf("packs: %lu\n", num_pack);
157 printf("size-pack: %s\n", pack_buf.buf);
158 printf("prune-packable: %lu\n", packed_loose);
159 printf("garbage: %lu\n", garbage);
160 printf("size-garbage: %s\n", garbage_buf.buf);
161 foreach_alt_odb(print_alternate, NULL);
162 strbuf_release(&loose_buf);
163 strbuf_release(&pack_buf);
164 strbuf_release(&garbage_buf);
165 } else {
166 struct strbuf buf = STRBUF_INIT;
167 if (human_readable)
168 strbuf_humanise_bytes(&buf, loose_size);
169 else
170 strbuf_addf(&buf, "%lu kilobytes",
171 (unsigned long)(loose_size / 1024));
172 printf("%lu objects, %s\n", loose, buf.buf);
173 strbuf_release(&buf);
175 return 0;