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