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