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