2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 #include "environment.h"
13 #include "repository.h"
14 #include "parse-options.h"
17 #include "object-store-ll.h"
19 static unsigned long garbage
;
20 static off_t size_garbage
;
22 static unsigned long loose
, packed
, packed_loose
;
23 static off_t loose_size
;
25 static const char *bits_to_msg(unsigned seen_bits
)
29 return "no corresponding .idx or .pack";
30 case PACKDIR_FILE_GARBAGE
:
31 return "garbage found";
32 case PACKDIR_FILE_PACK
:
33 return "no corresponding .idx";
34 case PACKDIR_FILE_IDX
:
35 return "no corresponding .pack";
36 case PACKDIR_FILE_PACK
|PACKDIR_FILE_IDX
:
42 static void real_report_garbage(unsigned seen_bits
, const char *path
)
45 const char *desc
= bits_to_msg(seen_bits
);
51 size_garbage
+= st
.st_size
;
52 warning("%s: %s", desc
, path
);
56 static void loose_garbage(const char *path
)
59 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
62 static int count_loose(const struct object_id
*oid
, const char *path
,
67 if (lstat(path
, &st
) || !S_ISREG(st
.st_mode
))
70 loose_size
+= on_disk_bytes(st
);
72 if (verbose
&& has_object_pack(oid
))
78 static int count_cruft(const char *basename UNUSED
, const char *path
,
85 static int print_alternate(struct object_directory
*odb
, void *data UNUSED
)
87 printf("alternate: ");
88 quote_c_style(odb
->path
, NULL
, stdout
, 0);
93 static char const * const count_objects_usage
[] = {
94 "git count-objects [-v] [-H | --human-readable]",
98 int cmd_count_objects(int argc
, const char **argv
, const char *prefix
)
100 int human_readable
= 0;
101 struct option opts
[] = {
102 OPT__VERBOSE(&verbose
, N_("be verbose")),
103 OPT_BOOL('H', "human-readable", &human_readable
,
104 N_("print sizes in human readable format")),
108 git_config(git_default_config
, NULL
);
110 argc
= parse_options(argc
, argv
, prefix
, opts
, count_objects_usage
, 0);
111 /* we do not take arguments other than flags for now */
113 usage_with_options(count_objects_usage
, opts
);
115 report_garbage
= real_report_garbage
;
116 report_linked_checkout_garbage();
119 for_each_loose_file_in_objdir(get_object_directory(),
120 count_loose
, count_cruft
, NULL
, NULL
);
123 struct packed_git
*p
;
124 unsigned long num_pack
= 0;
126 struct strbuf loose_buf
= STRBUF_INIT
;
127 struct strbuf pack_buf
= STRBUF_INIT
;
128 struct strbuf garbage_buf
= STRBUF_INIT
;
130 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
133 if (open_pack_index(p
))
135 packed
+= p
->num_objects
;
136 size_pack
+= p
->pack_size
+ p
->index_size
;
140 if (human_readable
) {
141 strbuf_humanise_bytes(&loose_buf
, loose_size
);
142 strbuf_humanise_bytes(&pack_buf
, size_pack
);
143 strbuf_humanise_bytes(&garbage_buf
, size_garbage
);
145 strbuf_addf(&loose_buf
, "%lu",
146 (unsigned long)(loose_size
/ 1024));
147 strbuf_addf(&pack_buf
, "%lu",
148 (unsigned long)(size_pack
/ 1024));
149 strbuf_addf(&garbage_buf
, "%lu",
150 (unsigned long)(size_garbage
/ 1024));
153 printf("count: %lu\n", loose
);
154 printf("size: %s\n", loose_buf
.buf
);
155 printf("in-pack: %lu\n", packed
);
156 printf("packs: %lu\n", num_pack
);
157 printf("size-pack: %s\n", pack_buf
.buf
);
158 printf("prune-packable: %lu\n", packed_loose
);
159 printf("garbage: %lu\n", garbage
);
160 printf("size-garbage: %s\n", garbage_buf
.buf
);
161 foreach_alt_odb(print_alternate
, NULL
);
162 strbuf_release(&loose_buf
);
163 strbuf_release(&pack_buf
);
164 strbuf_release(&garbage_buf
);
166 struct strbuf buf
= STRBUF_INIT
;
168 strbuf_humanise_bytes(&buf
, loose_size
);
170 strbuf_addf(&buf
, "%lu kilobytes",
171 (unsigned long)(loose_size
/ 1024));
172 printf("%lu objects, %s\n", loose
, buf
.buf
);
173 strbuf_release(&buf
);