]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/count-objects.c
b9e6b55a9a92955eba7afca3e585fdf634d34303
2 * Builtin "git count-objects".
4 * Copyright (c) 2006 Junio C Hamano
10 #include "environment.h"
12 #include "repository.h"
13 #include "parse-options.h"
16 #include "object-store.h"
18 static unsigned long garbage
;
19 static off_t size_garbage
;
21 static unsigned long loose
, packed
, packed_loose
;
22 static off_t loose_size
;
24 static const char *bits_to_msg(unsigned seen_bits
)
28 return "no corresponding .idx or .pack";
29 case PACKDIR_FILE_GARBAGE
:
30 return "garbage found";
31 case PACKDIR_FILE_PACK
:
32 return "no corresponding .idx";
33 case PACKDIR_FILE_IDX
:
34 return "no corresponding .pack";
35 case PACKDIR_FILE_PACK
|PACKDIR_FILE_IDX
:
41 static void real_report_garbage(unsigned seen_bits
, const char *path
)
44 const char *desc
= bits_to_msg(seen_bits
);
50 size_garbage
+= st
.st_size
;
51 warning("%s: %s", desc
, path
);
55 static void loose_garbage(const char *path
)
58 report_garbage(PACKDIR_FILE_GARBAGE
, path
);
61 static int count_loose(const struct object_id
*oid
, const char *path
,
66 if (lstat(path
, &st
) || !S_ISREG(st
.st_mode
))
69 loose_size
+= on_disk_bytes(st
);
71 if (verbose
&& has_object_pack(oid
))
77 static int count_cruft(const char *basename UNUSED
, const char *path
,
84 static int print_alternate(struct object_directory
*odb
, void *data
)
86 printf("alternate: ");
87 quote_c_style(odb
->path
, NULL
, stdout
, 0);
92 static char const * const count_objects_usage
[] = {
93 "git count-objects [-v] [-H | --human-readable]",
97 int cmd_count_objects(int argc
, const char **argv
, const char *prefix
)
99 int human_readable
= 0;
100 struct option opts
[] = {
101 OPT__VERBOSE(&verbose
, N_("be verbose")),
102 OPT_BOOL('H', "human-readable", &human_readable
,
103 N_("print sizes in human readable format")),
107 git_config(git_default_config
, NULL
);
109 argc
= parse_options(argc
, argv
, prefix
, opts
, count_objects_usage
, 0);
110 /* we do not take arguments other than flags for now */
112 usage_with_options(count_objects_usage
, opts
);
114 report_garbage
= real_report_garbage
;
115 report_linked_checkout_garbage();
118 for_each_loose_file_in_objdir(get_object_directory(),
119 count_loose
, count_cruft
, NULL
, NULL
);
122 struct packed_git
*p
;
123 unsigned long num_pack
= 0;
125 struct strbuf loose_buf
= STRBUF_INIT
;
126 struct strbuf pack_buf
= STRBUF_INIT
;
127 struct strbuf garbage_buf
= STRBUF_INIT
;
129 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
132 if (open_pack_index(p
))
134 packed
+= p
->num_objects
;
135 size_pack
+= p
->pack_size
+ p
->index_size
;
139 if (human_readable
) {
140 strbuf_humanise_bytes(&loose_buf
, loose_size
);
141 strbuf_humanise_bytes(&pack_buf
, size_pack
);
142 strbuf_humanise_bytes(&garbage_buf
, size_garbage
);
144 strbuf_addf(&loose_buf
, "%lu",
145 (unsigned long)(loose_size
/ 1024));
146 strbuf_addf(&pack_buf
, "%lu",
147 (unsigned long)(size_pack
/ 1024));
148 strbuf_addf(&garbage_buf
, "%lu",
149 (unsigned long)(size_garbage
/ 1024));
152 printf("count: %lu\n", loose
);
153 printf("size: %s\n", loose_buf
.buf
);
154 printf("in-pack: %lu\n", packed
);
155 printf("packs: %lu\n", num_pack
);
156 printf("size-pack: %s\n", pack_buf
.buf
);
157 printf("prune-packable: %lu\n", packed_loose
);
158 printf("garbage: %lu\n", garbage
);
159 printf("size-garbage: %s\n", garbage_buf
.buf
);
160 foreach_alt_odb(print_alternate
, NULL
);
161 strbuf_release(&loose_buf
);
162 strbuf_release(&pack_buf
);
163 strbuf_release(&garbage_buf
);
165 struct strbuf buf
= STRBUF_INIT
;
167 strbuf_humanise_bytes(&buf
, loose_size
);
169 strbuf_addf(&buf
, "%lu kilobytes",
170 (unsigned long)(loose_size
/ 1024));
171 printf("%lu objects, %s\n", loose
, buf
.buf
);
172 strbuf_release(&buf
);