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