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