]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/count-objects.c
Merge branch 'tk/stripspace'
[thirdparty/git.git] / builtin / count-objects.c
CommitLineData
c7432087
JH
1/*
2 * Builtin "git count-objects".
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6
7#include "cache.h"
8ca12c0d 8#include "dir.h"
c7432087 9#include "builtin.h"
833f3abd 10#include "parse-options.h"
c7432087 11
543c5caa 12static unsigned long garbage;
1a20dd49 13static off_t size_garbage;
4a1e693a
JK
14static int verbose;
15static unsigned long loose, packed, packed_loose;
16static off_t loose_size;
543c5caa
NTND
17
18static void real_report_garbage(const char *desc, const char *path)
19{
1a20dd49
NTND
20 struct stat st;
21 if (!stat(path, &st))
22 size_garbage += st.st_size;
543c5caa
NTND
23 warning("%s: %s", desc, path);
24 garbage++;
25}
26
4a1e693a 27static void loose_garbage(const char *path)
c7432087 28{
4a1e693a
JK
29 if (verbose)
30 report_garbage("garbage found", path);
31}
c7432087 32
4a1e693a
JK
33static int count_loose(const unsigned char *sha1, const char *path, void *data)
34{
35 struct stat st;
36
37 if (lstat(path, &st) || !S_ISREG(st.st_mode))
38 loose_garbage(path);
39 else {
40 loose_size += on_disk_bytes(st);
41 loose++;
42 if (verbose && has_sha1_pack(sha1))
43 packed_loose++;
c7432087 44 }
4a1e693a
JK
45 return 0;
46}
47
48static int count_cruft(const char *basename, const char *path, void *data)
49{
50 loose_garbage(path);
51 return 0;
c7432087
JH
52}
53
833f3abd 54static char const * const count_objects_usage[] = {
1918225d 55 N_("git count-objects [-v] [-H | --human-readable]"),
833f3abd
PH
56 NULL
57};
58
59int cmd_count_objects(int argc, const char **argv, const char *prefix)
c7432087 60{
4a1e693a 61 int human_readable = 0;
833f3abd 62 struct option opts[] = {
7adaddc2 63 OPT__VERBOSE(&verbose, N_("be verbose")),
1918225d
AP
64 OPT_BOOL('H', "human-readable", &human_readable,
65 N_("print sizes in human readable format")),
833f3abd
PH
66 OPT_END(),
67 };
c7432087 68
37782920 69 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
c7432087 70 /* we do not take arguments other than flags for now */
833f3abd
PH
71 if (argc)
72 usage_with_options(count_objects_usage, opts);
77a6d840 73 if (verbose) {
543c5caa 74 report_garbage = real_report_garbage;
77a6d840
NTND
75 report_linked_checkout_garbage();
76 }
4a1e693a
JK
77
78 for_each_loose_file_in_objdir(get_object_directory(),
79 count_loose, count_cruft, NULL, NULL);
80
c7432087
JH
81 if (verbose) {
82 struct packed_git *p;
ae72f685 83 unsigned long num_pack = 0;
c985ddf3 84 off_t size_pack = 0;
1918225d
AP
85 struct strbuf loose_buf = STRBUF_INIT;
86 struct strbuf pack_buf = STRBUF_INIT;
87 struct strbuf garbage_buf = STRBUF_INIT;
80fe7d2b
JH
88 if (!packed_git)
89 prepare_packed_git();
c7432087
JH
90 for (p = packed_git; p; p = p->next) {
91 if (!p->pack_local)
92 continue;
eaa86770 93 if (open_pack_index(p))
d079837e 94 continue;
57059091 95 packed += p->num_objects;
f2238249 96 size_pack += p->pack_size + p->index_size;
ae72f685 97 num_pack++;
c7432087 98 }
1918225d
AP
99
100 if (human_readable) {
101 strbuf_humanise_bytes(&loose_buf, loose_size);
102 strbuf_humanise_bytes(&pack_buf, size_pack);
103 strbuf_humanise_bytes(&garbage_buf, size_garbage);
104 } else {
105 strbuf_addf(&loose_buf, "%lu",
106 (unsigned long)(loose_size / 1024));
107 strbuf_addf(&pack_buf, "%lu",
108 (unsigned long)(size_pack / 1024));
109 strbuf_addf(&garbage_buf, "%lu",
110 (unsigned long)(size_garbage / 1024));
111 }
112
c7432087 113 printf("count: %lu\n", loose);
1918225d 114 printf("size: %s\n", loose_buf.buf);
c7432087 115 printf("in-pack: %lu\n", packed);
ae72f685 116 printf("packs: %lu\n", num_pack);
1918225d 117 printf("size-pack: %s\n", pack_buf.buf);
c7432087
JH
118 printf("prune-packable: %lu\n", packed_loose);
119 printf("garbage: %lu\n", garbage);
1918225d
AP
120 printf("size-garbage: %s\n", garbage_buf.buf);
121 strbuf_release(&loose_buf);
122 strbuf_release(&pack_buf);
123 strbuf_release(&garbage_buf);
124 } else {
125 struct strbuf buf = STRBUF_INIT;
126 if (human_readable)
127 strbuf_humanise_bytes(&buf, loose_size);
128 else
129 strbuf_addf(&buf, "%lu kilobytes",
130 (unsigned long)(loose_size / 1024));
131 printf("%lu objects, %s\n", loose, buf.buf);
132 strbuf_release(&buf);
c7432087 133 }
c7432087
JH
134 return 0;
135}