]>
Commit | Line | Data |
---|---|---|
c7432087 JH |
1 | /* |
2 | * Builtin "git count-objects". | |
3 | * | |
4 | * Copyright (c) 2006 Junio C Hamano | |
5 | */ | |
03eae9af | 6 | #define USE_THE_REPOSITORY_VARIABLE |
bc5c5ec0 | 7 | #include "builtin.h" |
b2141fc1 | 8 | #include "config.h" |
8ca12c0d | 9 | #include "dir.h" |
f394e093 | 10 | #include "gettext.h" |
c339932b | 11 | #include "path.h" |
833f3abd | 12 | #include "parse-options.h" |
5fe849d6 | 13 | #include "quote.h" |
0317f455 | 14 | #include "packfile.h" |
1a793261 | 15 | #include "object-file.h" |
c7432087 | 16 | |
543c5caa | 17 | static unsigned long garbage; |
1a20dd49 | 18 | static off_t size_garbage; |
4a1e693a JK |
19 | static int verbose; |
20 | static unsigned long loose, packed, packed_loose; | |
21 | static off_t loose_size; | |
543c5caa | 22 | |
0a489b06 JH |
23 | static 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 | ||
40 | static 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 | 54 | static void loose_garbage(const char *path) |
c7432087 | 55 | { |
4a1e693a | 56 | if (verbose) |
0a489b06 | 57 | report_garbage(PACKDIR_FILE_GARBAGE, path); |
4a1e693a | 58 | } |
c7432087 | 59 | |
be252d33 JK |
60 | static int count_loose(const struct object_id *oid, const char *path, |
61 | void *data UNUSED) | |
4a1e693a JK |
62 | { |
63 | struct stat st; | |
64 | ||
65 | if (lstat(path, &st) || !S_ISREG(st.st_mode)) | |
66 | loose_garbage(path); | |
67 | else { | |
68 | loose_size += on_disk_bytes(st); | |
69 | loose++; | |
cc656f4e | 70 | if (verbose && has_object_pack(the_repository, oid)) |
4a1e693a | 71 | packed_loose++; |
c7432087 | 72 | } |
4a1e693a JK |
73 | return 0; |
74 | } | |
75 | ||
be252d33 JK |
76 | static int count_cruft(const char *basename UNUSED, const char *path, |
77 | void *data UNUSED) | |
4a1e693a JK |
78 | { |
79 | loose_garbage(path); | |
80 | return 0; | |
c7432087 JH |
81 | } |
82 | ||
506d35f1 | 83 | static int print_alternate(struct object_directory *odb, void *data UNUSED) |
5fe849d6 JK |
84 | { |
85 | printf("alternate: "); | |
263db403 | 86 | quote_c_style(odb->path, NULL, stdout, 0); |
5fe849d6 JK |
87 | putchar('\n'); |
88 | return 0; | |
89 | } | |
90 | ||
833f3abd | 91 | static char const * const count_objects_usage[] = { |
959d670d | 92 | "git count-objects [-v] [-H | --human-readable]", |
833f3abd PH |
93 | NULL |
94 | }; | |
95 | ||
9b1cb507 JC |
96 | int cmd_count_objects(int argc, |
97 | const char **argv, | |
98 | const char *prefix, | |
99 | struct repository *repo UNUSED) | |
c7432087 | 100 | { |
4a1e693a | 101 | int human_readable = 0; |
833f3abd | 102 | struct option opts[] = { |
7adaddc2 | 103 | OPT__VERBOSE(&verbose, N_("be verbose")), |
1918225d AP |
104 | OPT_BOOL('H', "human-readable", &human_readable, |
105 | N_("print sizes in human readable format")), | |
833f3abd PH |
106 | OPT_END(), |
107 | }; | |
c7432087 | 108 | |
ea0fc3b4 JK |
109 | git_config(git_default_config, NULL); |
110 | ||
37782920 | 111 | argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0); |
c7432087 | 112 | /* we do not take arguments other than flags for now */ |
833f3abd PH |
113 | if (argc) |
114 | usage_with_options(count_objects_usage, opts); | |
77a6d840 | 115 | if (verbose) { |
543c5caa | 116 | report_garbage = real_report_garbage; |
78f2210b | 117 | report_linked_checkout_garbage(the_repository); |
77a6d840 | 118 | } |
4a1e693a | 119 | |
a3673f48 | 120 | for_each_loose_file_in_objdir(repo_get_object_directory(the_repository), |
4a1e693a JK |
121 | count_loose, count_cruft, NULL, NULL); |
122 | ||
c7432087 JH |
123 | if (verbose) { |
124 | struct packed_git *p; | |
ae72f685 | 125 | unsigned long num_pack = 0; |
c985ddf3 | 126 | off_t size_pack = 0; |
1918225d AP |
127 | struct strbuf loose_buf = STRBUF_INIT; |
128 | struct strbuf pack_buf = STRBUF_INIT; | |
129 | struct strbuf garbage_buf = STRBUF_INIT; | |
464416a2 | 130 | |
454ea2e4 | 131 | for (p = get_all_packs(the_repository); p; p = p->next) { |
c7432087 JH |
132 | if (!p->pack_local) |
133 | continue; | |
eaa86770 | 134 | if (open_pack_index(p)) |
d079837e | 135 | continue; |
57059091 | 136 | packed += p->num_objects; |
f2238249 | 137 | size_pack += p->pack_size + p->index_size; |
ae72f685 | 138 | num_pack++; |
c7432087 | 139 | } |
1918225d AP |
140 | |
141 | if (human_readable) { | |
142 | strbuf_humanise_bytes(&loose_buf, loose_size); | |
143 | strbuf_humanise_bytes(&pack_buf, size_pack); | |
144 | strbuf_humanise_bytes(&garbage_buf, size_garbage); | |
145 | } else { | |
146 | strbuf_addf(&loose_buf, "%lu", | |
147 | (unsigned long)(loose_size / 1024)); | |
148 | strbuf_addf(&pack_buf, "%lu", | |
149 | (unsigned long)(size_pack / 1024)); | |
150 | strbuf_addf(&garbage_buf, "%lu", | |
151 | (unsigned long)(size_garbage / 1024)); | |
152 | } | |
153 | ||
c7432087 | 154 | printf("count: %lu\n", loose); |
1918225d | 155 | printf("size: %s\n", loose_buf.buf); |
c7432087 | 156 | printf("in-pack: %lu\n", packed); |
ae72f685 | 157 | printf("packs: %lu\n", num_pack); |
1918225d | 158 | printf("size-pack: %s\n", pack_buf.buf); |
c7432087 JH |
159 | printf("prune-packable: %lu\n", packed_loose); |
160 | printf("garbage: %lu\n", garbage); | |
1918225d | 161 | printf("size-garbage: %s\n", garbage_buf.buf); |
5fe849d6 | 162 | foreach_alt_odb(print_alternate, NULL); |
1918225d AP |
163 | strbuf_release(&loose_buf); |
164 | strbuf_release(&pack_buf); | |
165 | strbuf_release(&garbage_buf); | |
166 | } else { | |
167 | struct strbuf buf = STRBUF_INIT; | |
168 | if (human_readable) | |
169 | strbuf_humanise_bytes(&buf, loose_size); | |
170 | else | |
171 | strbuf_addf(&buf, "%lu kilobytes", | |
172 | (unsigned long)(loose_size / 1024)); | |
173 | printf("%lu objects, %s\n", loose, buf.buf); | |
174 | strbuf_release(&buf); | |
c7432087 | 175 | } |
c7432087 JH |
176 | return 0; |
177 | } |