1/*
2 * Builtin "git count-objects".
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6
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
14static unsigned long garbage;
15static off_t size_garbage;
16static int verbose;
17static unsigned long loose, packed, packed_loose;
18static off_t loose_size;
19
20static const char *bits_to_msg(unsigned seen_bits)
21{
22 switch (seen_bits) {
23 case 0:
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:
32 default:
33 return NULL;
34 }
35}
36
37static void real_report_garbage(unsigned seen_bits, const char *path)
38{
39 struct stat st;
40 const char *desc = bits_to_msg(seen_bits);
41
42 if (!desc)
43 return;
44
45 if (!stat(path, &st))
46 size_garbage += st.st_size;
47 warning("%s: %s", desc, path);
48 garbage++;
49}
50
51static void loose_garbage(const char *path)
52{
53 if (verbose)
54 report_garbage(PACKDIR_FILE_GARBAGE, path);
55}
56
57static int count_loose(const struct object_id *oid, const char *path, void *data)
58{
59 struct stat st;
60
61 if (lstat(path, &st) || !S_ISREG(st.st_mode))
62 loose_garbage(path);
63 else {
64 loose_size += on_disk_bytes(st);
65 loose++;
66 if (verbose && has_sha1_pack(oid->hash))
67 packed_loose++;
68 }
69 return 0;
70}
71
72static int count_cruft(const char *basename, const char *path, void *data)
73{
74 loose_garbage(path);
75 return 0;
76}
77
78static int print_alternate(struct alternate_object_database *alt, void *data)
79{
80 printf("alternate: ");
81 quote_c_style(alt->path, NULL, stdout, 0);
82 putchar('\n');
83 return 0;
84}
85
86static char const * const count_objects_usage[] = {
87 N_("git count-objects [-v] [-H | --human-readable]"),
88 NULL
89};
90
91int cmd_count_objects(int argc, const char **argv, const char *prefix)
92{
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")),
98 OPT_END(),
99 };
100
101 git_config(git_default_config, NULL);
102
103 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
104 /* we do not take arguments other than flags for now */
105 if (argc)
106 usage_with_options(count_objects_usage, opts);
107 if (verbose) {
108 report_garbage = real_report_garbage;
109 report_linked_checkout_garbage();
110 }
111
112 for_each_loose_file_in_objdir(get_object_directory(),
113 count_loose, count_cruft, NULL, NULL);
114
115 if (verbose) {
116 struct packed_git *p;
117 unsigned long num_pack = 0;
118 off_t size_pack = 0;
119 struct strbuf loose_buf = STRBUF_INIT;
120 struct strbuf pack_buf = STRBUF_INIT;
121 struct strbuf garbage_buf = STRBUF_INIT;
122 if (!packed_git)
123 prepare_packed_git();
124 for (p = packed_git; p; p = p->next) {
125 if (!p->pack_local)
126 continue;
127 if (open_pack_index(p))
128 continue;
129 packed += p->num_objects;
130 size_pack += p->pack_size + p->index_size;
131 num_pack++;
132 }
133
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);
138 } else {
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));
145 }
146
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);
159 } else {
160 struct strbuf buf = STRBUF_INIT;
161 if (human_readable)
162 strbuf_humanise_bytes(&buf, loose_size);
163 else
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);
168 }
169 return 0;
170}