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