]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/count-objects.c
Merge branch '2.16' of https://github.com/ChrisADR/git-po
[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"
c7432087 10#include "builtin.h"
833f3abd 11#include "parse-options.h"
5fe849d6 12#include "quote.h"
0317f455 13#include "packfile.h"
c7432087 14
543c5caa 15static unsigned long garbage;
1a20dd49 16static off_t size_garbage;
4a1e693a
JK
17static int verbose;
18static unsigned long loose, packed, packed_loose;
19static off_t loose_size;
543c5caa 20
0a489b06
JH
21static const char *bits_to_msg(unsigned seen_bits)
22{
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;
35 }
36}
37
38static void real_report_garbage(unsigned seen_bits, const char *path)
543c5caa 39{
1a20dd49 40 struct stat st;
0a489b06
JH
41 const char *desc = bits_to_msg(seen_bits);
42
43 if (!desc)
44 return;
45
1a20dd49
NTND
46 if (!stat(path, &st))
47 size_garbage += st.st_size;
543c5caa
NTND
48 warning("%s: %s", desc, path);
49 garbage++;
50}
51
4a1e693a 52static void loose_garbage(const char *path)
c7432087 53{
4a1e693a 54 if (verbose)
0a489b06 55 report_garbage(PACKDIR_FILE_GARBAGE, path);
4a1e693a 56}
c7432087 57
76c1d9a0 58static int count_loose(const struct object_id *oid, const char *path, void *data)
4a1e693a
JK
59{
60 struct stat st;
61
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++;
76c1d9a0 67 if (verbose && has_sha1_pack(oid->hash))
4a1e693a 68 packed_loose++;
c7432087 69 }
4a1e693a
JK
70 return 0;
71}
72
73static int count_cruft(const char *basename, const char *path, void *data)
74{
75 loose_garbage(path);
76 return 0;
c7432087
JH
77}
78
5fe849d6
JK
79static int print_alternate(struct alternate_object_database *alt, void *data)
80{
81 printf("alternate: ");
82 quote_c_style(alt->path, NULL, stdout, 0);
83 putchar('\n');
84 return 0;
85}
86
833f3abd 87static char const * const count_objects_usage[] = {
1918225d 88 N_("git count-objects [-v] [-H | --human-readable]"),
833f3abd
PH
89 NULL
90};
91
92int cmd_count_objects(int argc, const char **argv, const char *prefix)
c7432087 93{
4a1e693a 94 int human_readable = 0;
833f3abd 95 struct option opts[] = {
7adaddc2 96 OPT__VERBOSE(&verbose, N_("be verbose")),
1918225d
AP
97 OPT_BOOL('H', "human-readable", &human_readable,
98 N_("print sizes in human readable format")),
833f3abd
PH
99 OPT_END(),
100 };
c7432087 101
ea0fc3b4
JK
102 git_config(git_default_config, NULL);
103
37782920 104 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
c7432087 105 /* we do not take arguments other than flags for now */
833f3abd
PH
106 if (argc)
107 usage_with_options(count_objects_usage, opts);
77a6d840 108 if (verbose) {
543c5caa 109 report_garbage = real_report_garbage;
77a6d840
NTND
110 report_linked_checkout_garbage();
111 }
4a1e693a
JK
112
113 for_each_loose_file_in_objdir(get_object_directory(),
114 count_loose, count_cruft, NULL, NULL);
115
c7432087
JH
116 if (verbose) {
117 struct packed_git *p;
ae72f685 118 unsigned long num_pack = 0;
c985ddf3 119 off_t size_pack = 0;
1918225d
AP
120 struct strbuf loose_buf = STRBUF_INIT;
121 struct strbuf pack_buf = STRBUF_INIT;
122 struct strbuf garbage_buf = STRBUF_INIT;
80fe7d2b
JH
123 if (!packed_git)
124 prepare_packed_git();
c7432087
JH
125 for (p = packed_git; p; p = p->next) {
126 if (!p->pack_local)
127 continue;
eaa86770 128 if (open_pack_index(p))
d079837e 129 continue;
57059091 130 packed += p->num_objects;
f2238249 131 size_pack += p->pack_size + p->index_size;
ae72f685 132 num_pack++;
c7432087 133 }
1918225d
AP
134
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));
146 }
147
c7432087 148 printf("count: %lu\n", loose);
1918225d 149 printf("size: %s\n", loose_buf.buf);
c7432087 150 printf("in-pack: %lu\n", packed);
ae72f685 151 printf("packs: %lu\n", num_pack);
1918225d 152 printf("size-pack: %s\n", pack_buf.buf);
c7432087
JH
153 printf("prune-packable: %lu\n", packed_loose);
154 printf("garbage: %lu\n", garbage);
1918225d 155 printf("size-garbage: %s\n", garbage_buf.buf);
5fe849d6 156 foreach_alt_odb(print_alternate, NULL);
1918225d
AP
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);
c7432087 169 }
c7432087
JH
170 return 0;
171}