]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/count-objects.c
a88c0c9c09af6425ec613e6b4049f4657945bd58
[thirdparty/git.git] / builtin / count-objects.c
1 /*
2 * Builtin "git count-objects".
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6 #define USE_THE_REPOSITORY_VARIABLE
7 #include "builtin.h"
8 #include "config.h"
9 #include "dir.h"
10 #include "gettext.h"
11 #include "path.h"
12 #include "parse-options.h"
13 #include "quote.h"
14 #include "packfile.h"
15 #include "object-file.h"
16
17 static unsigned long garbage;
18 static off_t size_garbage;
19 static int verbose;
20 static unsigned long loose, packed, packed_loose;
21 static off_t loose_size;
22
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)
41 {
42 struct stat st;
43 const char *desc = bits_to_msg(seen_bits);
44
45 if (!desc)
46 return;
47
48 if (!stat(path, &st))
49 size_garbage += st.st_size;
50 warning("%s: %s", desc, path);
51 garbage++;
52 }
53
54 static void loose_garbage(const char *path)
55 {
56 if (verbose)
57 report_garbage(PACKDIR_FILE_GARBAGE, path);
58 }
59
60 static int count_loose(const struct object_id *oid, const char *path,
61 void *data UNUSED)
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++;
70 if (verbose && has_object_pack(the_repository, oid))
71 packed_loose++;
72 }
73 return 0;
74 }
75
76 static int count_cruft(const char *basename UNUSED, const char *path,
77 void *data UNUSED)
78 {
79 loose_garbage(path);
80 return 0;
81 }
82
83 static int print_alternate(struct object_directory *odb, void *data UNUSED)
84 {
85 printf("alternate: ");
86 quote_c_style(odb->path, NULL, stdout, 0);
87 putchar('\n');
88 return 0;
89 }
90
91 static char const * const count_objects_usage[] = {
92 "git count-objects [-v] [-H | --human-readable]",
93 NULL
94 };
95
96 int cmd_count_objects(int argc,
97 const char **argv,
98 const char *prefix,
99 struct repository *repo UNUSED)
100 {
101 int human_readable = 0;
102 struct option opts[] = {
103 OPT__VERBOSE(&verbose, N_("be verbose")),
104 OPT_BOOL('H', "human-readable", &human_readable,
105 N_("print sizes in human readable format")),
106 OPT_END(),
107 };
108
109 git_config(git_default_config, NULL);
110
111 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
112 /* we do not take arguments other than flags for now */
113 if (argc)
114 usage_with_options(count_objects_usage, opts);
115 if (verbose) {
116 report_garbage = real_report_garbage;
117 report_linked_checkout_garbage(the_repository);
118 }
119
120 for_each_loose_file_in_objdir(repo_get_object_directory(the_repository),
121 count_loose, count_cruft, NULL, NULL);
122
123 if (verbose) {
124 struct packed_git *p;
125 unsigned long num_pack = 0;
126 off_t size_pack = 0;
127 struct strbuf loose_buf = STRBUF_INIT;
128 struct strbuf pack_buf = STRBUF_INIT;
129 struct strbuf garbage_buf = STRBUF_INIT;
130
131 for (p = get_all_packs(the_repository); p; p = p->next) {
132 if (!p->pack_local)
133 continue;
134 if (open_pack_index(p))
135 continue;
136 packed += p->num_objects;
137 size_pack += p->pack_size + p->index_size;
138 num_pack++;
139 }
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
154 printf("count: %lu\n", loose);
155 printf("size: %s\n", loose_buf.buf);
156 printf("in-pack: %lu\n", packed);
157 printf("packs: %lu\n", num_pack);
158 printf("size-pack: %s\n", pack_buf.buf);
159 printf("prune-packable: %lu\n", packed_loose);
160 printf("garbage: %lu\n", garbage);
161 printf("size-garbage: %s\n", garbage_buf.buf);
162 foreach_alt_odb(print_alternate, NULL);
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);
175 }
176 return 0;
177 }