]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/count-objects.c
Merge branch 'jc/bisect-doc' into maint-2.43
[thirdparty/git.git] / builtin / count-objects.c
CommitLineData
c7432087
JH
1/*
2 * Builtin "git count-objects".
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6
bc5c5ec0 7#include "builtin.h"
b2141fc1 8#include "config.h"
8ca12c0d 9#include "dir.h"
32a8f510 10#include "environment.h"
f394e093 11#include "gettext.h"
c339932b 12#include "path.h"
a80d72db 13#include "repository.h"
833f3abd 14#include "parse-options.h"
5fe849d6 15#include "quote.h"
0317f455 16#include "packfile.h"
a034e910 17#include "object-store-ll.h"
c7432087 18
543c5caa 19static unsigned long garbage;
1a20dd49 20static off_t size_garbage;
4a1e693a
JK
21static int verbose;
22static unsigned long loose, packed, packed_loose;
23static off_t loose_size;
543c5caa 24
0a489b06
JH
25static const char *bits_to_msg(unsigned seen_bits)
26{
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;
39 }
40}
41
42static void real_report_garbage(unsigned seen_bits, const char *path)
543c5caa 43{
1a20dd49 44 struct stat st;
0a489b06
JH
45 const char *desc = bits_to_msg(seen_bits);
46
47 if (!desc)
48 return;
49
1a20dd49
NTND
50 if (!stat(path, &st))
51 size_garbage += st.st_size;
543c5caa
NTND
52 warning("%s: %s", desc, path);
53 garbage++;
54}
55
4a1e693a 56static void loose_garbage(const char *path)
c7432087 57{
4a1e693a 58 if (verbose)
0a489b06 59 report_garbage(PACKDIR_FILE_GARBAGE, path);
4a1e693a 60}
c7432087 61
be252d33
JK
62static int count_loose(const struct object_id *oid, const char *path,
63 void *data UNUSED)
4a1e693a
JK
64{
65 struct stat st;
66
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++;
14c3c80c 72 if (verbose && has_object_pack(oid))
4a1e693a 73 packed_loose++;
c7432087 74 }
4a1e693a
JK
75 return 0;
76}
77
be252d33
JK
78static int count_cruft(const char *basename UNUSED, const char *path,
79 void *data UNUSED)
4a1e693a
JK
80{
81 loose_garbage(path);
82 return 0;
c7432087
JH
83}
84
506d35f1 85static int print_alternate(struct object_directory *odb, void *data UNUSED)
5fe849d6
JK
86{
87 printf("alternate: ");
263db403 88 quote_c_style(odb->path, NULL, stdout, 0);
5fe849d6
JK
89 putchar('\n');
90 return 0;
91}
92
833f3abd 93static char const * const count_objects_usage[] = {
959d670d 94 "git count-objects [-v] [-H | --human-readable]",
833f3abd
PH
95 NULL
96};
97
98int cmd_count_objects(int argc, const char **argv, const char *prefix)
c7432087 99{
4a1e693a 100 int human_readable = 0;
833f3abd 101 struct option opts[] = {
7adaddc2 102 OPT__VERBOSE(&verbose, N_("be verbose")),
1918225d
AP
103 OPT_BOOL('H', "human-readable", &human_readable,
104 N_("print sizes in human readable format")),
833f3abd
PH
105 OPT_END(),
106 };
c7432087 107
ea0fc3b4
JK
108 git_config(git_default_config, NULL);
109
37782920 110 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
c7432087 111 /* we do not take arguments other than flags for now */
833f3abd
PH
112 if (argc)
113 usage_with_options(count_objects_usage, opts);
77a6d840 114 if (verbose) {
543c5caa 115 report_garbage = real_report_garbage;
77a6d840
NTND
116 report_linked_checkout_garbage();
117 }
4a1e693a
JK
118
119 for_each_loose_file_in_objdir(get_object_directory(),
120 count_loose, count_cruft, NULL, NULL);
121
c7432087
JH
122 if (verbose) {
123 struct packed_git *p;
ae72f685 124 unsigned long num_pack = 0;
c985ddf3 125 off_t size_pack = 0;
1918225d
AP
126 struct strbuf loose_buf = STRBUF_INIT;
127 struct strbuf pack_buf = STRBUF_INIT;
128 struct strbuf garbage_buf = STRBUF_INIT;
464416a2 129
454ea2e4 130 for (p = get_all_packs(the_repository); p; p = p->next) {
c7432087
JH
131 if (!p->pack_local)
132 continue;
eaa86770 133 if (open_pack_index(p))
d079837e 134 continue;
57059091 135 packed += p->num_objects;
f2238249 136 size_pack += p->pack_size + p->index_size;
ae72f685 137 num_pack++;
c7432087 138 }
1918225d
AP
139
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));
151 }
152
c7432087 153 printf("count: %lu\n", loose);
1918225d 154 printf("size: %s\n", loose_buf.buf);
c7432087 155 printf("in-pack: %lu\n", packed);
ae72f685 156 printf("packs: %lu\n", num_pack);
1918225d 157 printf("size-pack: %s\n", pack_buf.buf);
c7432087
JH
158 printf("prune-packable: %lu\n", packed_loose);
159 printf("garbage: %lu\n", garbage);
1918225d 160 printf("size-garbage: %s\n", garbage_buf.buf);
5fe849d6 161 foreach_alt_odb(print_alternate, NULL);
1918225d
AP
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);
c7432087 174 }
c7432087
JH
175 return 0;
176}