http-fetch: make `-a` standard behaviour
[git/debian.git] / builtin / count-objects.c
blob33343818c830bf64452640b14b2ce7b876221022
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"
15 static unsigned long garbage;
16 static off_t size_garbage;
17 static int verbose;
18 static unsigned long loose, packed, packed_loose;
19 static off_t loose_size;
21 static const char *bits_to_msg(unsigned seen_bits)
23 switch (seen_bits) {
24 case 0:
25 return "no corresponding .idx or .pack";
26 case PACKDIR_FILE_GARBAGE:
27 return "garbage found";
28 case PACKDIR_FILE_PACK:
29 return "no corresponding .idx";
30 case PACKDIR_FILE_IDX:
31 return "no corresponding .pack";
32 case PACKDIR_FILE_PACK|PACKDIR_FILE_IDX:
33 default:
34 return NULL;
38 static void real_report_garbage(unsigned seen_bits, const char *path)
40 struct stat st;
41 const char *desc = bits_to_msg(seen_bits);
43 if (!desc)
44 return;
46 if (!stat(path, &st))
47 size_garbage += st.st_size;
48 warning("%s: %s", desc, path);
49 garbage++;
52 static void loose_garbage(const char *path)
54 if (verbose)
55 report_garbage(PACKDIR_FILE_GARBAGE, path);
58 static int count_loose(const struct object_id *oid, const char *path, void *data)
60 struct stat st;
62 if (lstat(path, &st) || !S_ISREG(st.st_mode))
63 loose_garbage(path);
64 else {
65 loose_size += on_disk_bytes(st);
66 loose++;
67 if (verbose && has_sha1_pack(oid->hash))
68 packed_loose++;
70 return 0;
73 static int count_cruft(const char *basename, const char *path, void *data)
75 loose_garbage(path);
76 return 0;
79 static int print_alternate(struct alternate_object_database *alt, void *data)
81 printf("alternate: ");
82 quote_c_style(alt->path, NULL, stdout, 0);
83 putchar('\n');
84 return 0;
87 static char const * const count_objects_usage[] = {
88 N_("git count-objects [-v] [-H | --human-readable]"),
89 NULL
92 int cmd_count_objects(int argc, const char **argv, const char *prefix)
94 int human_readable = 0;
95 struct option opts[] = {
96 OPT__VERBOSE(&verbose, N_("be verbose")),
97 OPT_BOOL('H', "human-readable", &human_readable,
98 N_("print sizes in human readable format")),
99 OPT_END(),
102 git_config(git_default_config, NULL);
104 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
105 /* we do not take arguments other than flags for now */
106 if (argc)
107 usage_with_options(count_objects_usage, opts);
108 if (verbose) {
109 report_garbage = real_report_garbage;
110 report_linked_checkout_garbage();
113 for_each_loose_file_in_objdir(get_object_directory(),
114 count_loose, count_cruft, NULL, NULL);
116 if (verbose) {
117 struct packed_git *p;
118 unsigned long num_pack = 0;
119 off_t size_pack = 0;
120 struct strbuf loose_buf = STRBUF_INIT;
121 struct strbuf pack_buf = STRBUF_INIT;
122 struct strbuf garbage_buf = STRBUF_INIT;
123 if (!packed_git)
124 prepare_packed_git();
125 for (p = packed_git; p; p = p->next) {
126 if (!p->pack_local)
127 continue;
128 if (open_pack_index(p))
129 continue;
130 packed += p->num_objects;
131 size_pack += p->pack_size + p->index_size;
132 num_pack++;
135 if (human_readable) {
136 strbuf_humanise_bytes(&loose_buf, loose_size);
137 strbuf_humanise_bytes(&pack_buf, size_pack);
138 strbuf_humanise_bytes(&garbage_buf, size_garbage);
139 } else {
140 strbuf_addf(&loose_buf, "%lu",
141 (unsigned long)(loose_size / 1024));
142 strbuf_addf(&pack_buf, "%lu",
143 (unsigned long)(size_pack / 1024));
144 strbuf_addf(&garbage_buf, "%lu",
145 (unsigned long)(size_garbage / 1024));
148 printf("count: %lu\n", loose);
149 printf("size: %s\n", loose_buf.buf);
150 printf("in-pack: %lu\n", packed);
151 printf("packs: %lu\n", num_pack);
152 printf("size-pack: %s\n", pack_buf.buf);
153 printf("prune-packable: %lu\n", packed_loose);
154 printf("garbage: %lu\n", garbage);
155 printf("size-garbage: %s\n", garbage_buf.buf);
156 foreach_alt_odb(print_alternate, NULL);
157 strbuf_release(&loose_buf);
158 strbuf_release(&pack_buf);
159 strbuf_release(&garbage_buf);
160 } else {
161 struct strbuf buf = STRBUF_INIT;
162 if (human_readable)
163 strbuf_humanise_bytes(&buf, loose_size);
164 else
165 strbuf_addf(&buf, "%lu kilobytes",
166 (unsigned long)(loose_size / 1024));
167 printf("%lu objects, %s\n", loose, buf.buf);
168 strbuf_release(&buf);
170 return 0;