]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/count-objects.c
2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 #include "repository.h"
12 #include "parse-options.h"
15 #include "object-store.h"
17 static unsigned long garbage
;
18 static off_t size_garbage
;
20 static unsigned long loose
, packed
, packed_loose
;
21 static off_t loose_size
;
23 static const char *bits_to_msg(unsigned seen_bits
)
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
:
40 static void real_report_garbage(unsigned seen_bits
, const char *path
)
43 const char *desc
= bits_to_msg(seen_bits
);
49 size_garbage
+= st
.st_size
;
50 warning("%s: %s", desc
, path
);
54 static void loose_garbage(const char *path
)
57 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
60 static int count_loose(const struct object_id
*oid
, const char *path
,
65 if (lstat(path
, &st
) || !S_ISREG(st
.st_mode
))
68 loose_size
+= on_disk_bytes(st
);
70 if (verbose
&& has_object_pack(oid
))
76 static int count_cruft(const char *basename UNUSED
, const char *path
,
83 static int print_alternate(struct object_directory
*odb
, void *data
)
85 printf("alternate: ");
86 quote_c_style(odb
->path
, NULL
, stdout
, 0);
91 static char const * const count_objects_usage
[] = {
92 "git count-objects [-v] [-H | --human-readable]",
96 int cmd_count_objects(int argc
, const char **argv
, const char *prefix
)
98 int human_readable
= 0;
99 struct option opts
[] = {
100 OPT__VERBOSE(&verbose
, N_("be verbose")),
101 OPT_BOOL('H', "human-readable", &human_readable
,
102 N_("print sizes in human readable format")),
106 git_config(git_default_config
, NULL
);
108 argc
= parse_options(argc
, argv
, prefix
, opts
, count_objects_usage
, 0);
109 /* we do not take arguments other than flags for now */
111 usage_with_options(count_objects_usage
, opts
);
113 report_garbage
= real_report_garbage
;
114 report_linked_checkout_garbage();
117 for_each_loose_file_in_objdir(get_object_directory(),
118 count_loose
, count_cruft
, NULL
, NULL
);
121 struct packed_git
*p
;
122 unsigned long num_pack
= 0;
124 struct strbuf loose_buf
= STRBUF_INIT
;
125 struct strbuf pack_buf
= STRBUF_INIT
;
126 struct strbuf garbage_buf
= STRBUF_INIT
;
128 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
131 if (open_pack_index(p
))
133 packed
+= p
->num_objects
;
134 size_pack
+= p
->pack_size
+ p
->index_size
;
138 if (human_readable
) {
139 strbuf_humanise_bytes(&loose_buf
, loose_size
);
140 strbuf_humanise_bytes(&pack_buf
, size_pack
);
141 strbuf_humanise_bytes(&garbage_buf
, size_garbage
);
143 strbuf_addf(&loose_buf
, "%lu",
144 (unsigned long)(loose_size
/ 1024));
145 strbuf_addf(&pack_buf
, "%lu",
146 (unsigned long)(size_pack
/ 1024));
147 strbuf_addf(&garbage_buf
, "%lu",
148 (unsigned long)(size_garbage
/ 1024));
151 printf("count: %lu\n", loose
);
152 printf("size: %s\n", loose_buf
.buf
);
153 printf("in-pack: %lu\n", packed
);
154 printf("packs: %lu\n", num_pack
);
155 printf("size-pack: %s\n", pack_buf
.buf
);
156 printf("prune-packable: %lu\n", packed_loose
);
157 printf("garbage: %lu\n", garbage
);
158 printf("size-garbage: %s\n", garbage_buf
.buf
);
159 foreach_alt_odb(print_alternate
, NULL
);
160 strbuf_release(&loose_buf
);
161 strbuf_release(&pack_buf
);
162 strbuf_release(&garbage_buf
);
164 struct strbuf buf
= STRBUF_INIT
;
166 strbuf_humanise_bytes(&buf
, loose_size
);
168 strbuf_addf(&buf
, "%lu kilobytes",
169 (unsigned long)(loose_size
/ 1024));
170 printf("%lu objects, %s\n", loose
, buf
.buf
);
171 strbuf_release(&buf
);