blob: e47ef0b1af06a1abd63f7818c363db6c859a9bf6 [file] [log] [blame]
Junio C Hamanoc7432082006-04-27 15:37:18 -07001/*
2 * Builtin "git count-objects".
3 *
4 * Copyright (c) 2006 Junio C Hamano
5 */
6
7#include "cache.h"
Alexander Potashev8ca12c02009-01-10 15:07:50 +03008#include "dir.h"
Junio C Hamanoc7432082006-04-27 15:37:18 -07009#include "builtin.h"
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020010#include "parse-options.h"
Junio C Hamanoc7432082006-04-27 15:37:18 -070011
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070012static unsigned long garbage;
Nguyễn Thái Ngọc Duy1a20dd42013-02-13 16:13:19 +070013static off_t size_garbage;
Jeff King4a1e6932014-10-15 18:41:11 -040014static int verbose;
15static unsigned long loose, packed, packed_loose;
16static off_t loose_size;
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070017
18static void real_report_garbage(const char *desc, const char *path)
19{
Nguyễn Thái Ngọc Duy1a20dd42013-02-13 16:13:19 +070020 struct stat st;
21 if (!stat(path, &st))
22 size_garbage += st.st_size;
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070023 warning("%s: %s", desc, path);
24 garbage++;
25}
26
Jeff King4a1e6932014-10-15 18:41:11 -040027static void loose_garbage(const char *path)
Junio C Hamanoc7432082006-04-27 15:37:18 -070028{
Jeff King4a1e6932014-10-15 18:41:11 -040029 if (verbose)
30 report_garbage("garbage found", path);
31}
Junio C Hamanoc7432082006-04-27 15:37:18 -070032
Jeff King4a1e6932014-10-15 18:41:11 -040033static 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++;
Junio C Hamanoc7432082006-04-27 15:37:18 -070044 }
Jeff King4a1e6932014-10-15 18:41:11 -040045 return 0;
46}
47
48static int count_cruft(const char *basename, const char *path, void *data)
49{
50 loose_garbage(path);
51 return 0;
Junio C Hamanoc7432082006-04-27 15:37:18 -070052}
53
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020054static char const * const count_objects_usage[] = {
Antoine Pelisse19182252013-04-10 21:03:24 +020055 N_("git count-objects [-v] [-H | --human-readable]"),
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020056 NULL
57};
58
59int cmd_count_objects(int argc, const char **argv, const char *prefix)
Junio C Hamanoc7432082006-04-27 15:37:18 -070060{
Jeff King4a1e6932014-10-15 18:41:11 -040061 int human_readable = 0;
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020062 struct option opts[] = {
Nguyễn Thái Ngọc Duy7adaddc2012-08-20 19:32:06 +070063 OPT__VERBOSE(&verbose, N_("be verbose")),
Antoine Pelisse19182252013-04-10 21:03:24 +020064 OPT_BOOL('H', "human-readable", &human_readable,
65 N_("print sizes in human readable format")),
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020066 OPT_END(),
67 };
Junio C Hamanoc7432082006-04-27 15:37:18 -070068
Stephen Boyd37782922009-05-23 11:53:12 -070069 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
Junio C Hamanoc7432082006-04-27 15:37:18 -070070 /* we do not take arguments other than flags for now */
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020071 if (argc)
72 usage_with_options(count_objects_usage, opts);
Nguyễn Thái Ngọc Duy543c5ca2013-02-15 19:07:10 +070073 if (verbose)
74 report_garbage = real_report_garbage;
Jeff King4a1e6932014-10-15 18:41:11 -040075
76 for_each_loose_file_in_objdir(get_object_directory(),
77 count_loose, count_cruft, NULL, NULL);
78
Junio C Hamanoc7432082006-04-27 15:37:18 -070079 if (verbose) {
80 struct packed_git *p;
Junio C Hamanoae72f682006-12-27 01:04:03 -080081 unsigned long num_pack = 0;
Andreas Schwabc985ddf2009-12-29 20:09:15 +010082 off_t size_pack = 0;
Antoine Pelisse19182252013-04-10 21:03:24 +020083 struct strbuf loose_buf = STRBUF_INIT;
84 struct strbuf pack_buf = STRBUF_INIT;
85 struct strbuf garbage_buf = STRBUF_INIT;
Junio C Hamano80fe7d22006-05-02 23:03:15 -070086 if (!packed_git)
87 prepare_packed_git();
Junio C Hamanoc7432082006-04-27 15:37:18 -070088 for (p = packed_git; p; p = p->next) {
89 if (!p->pack_local)
90 continue;
Shawn O. Pearceeaa86772007-05-30 02:12:28 -040091 if (open_pack_index(p))
Shawn O. Pearced0798372007-05-26 01:24:19 -040092 continue;
Nicolas Pitre57059092007-04-09 01:06:28 -040093 packed += p->num_objects;
Marcus Griepf2238242008-08-15 00:20:20 -040094 size_pack += p->pack_size + p->index_size;
Junio C Hamanoae72f682006-12-27 01:04:03 -080095 num_pack++;
Junio C Hamanoc7432082006-04-27 15:37:18 -070096 }
Antoine Pelisse19182252013-04-10 21:03:24 +020097
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
Junio C Hamanoc7432082006-04-27 15:37:18 -0700111 printf("count: %lu\n", loose);
Antoine Pelisse19182252013-04-10 21:03:24 +0200112 printf("size: %s\n", loose_buf.buf);
Junio C Hamanoc7432082006-04-27 15:37:18 -0700113 printf("in-pack: %lu\n", packed);
Junio C Hamanoae72f682006-12-27 01:04:03 -0800114 printf("packs: %lu\n", num_pack);
Antoine Pelisse19182252013-04-10 21:03:24 +0200115 printf("size-pack: %s\n", pack_buf.buf);
Junio C Hamanoc7432082006-04-27 15:37:18 -0700116 printf("prune-packable: %lu\n", packed_loose);
117 printf("garbage: %lu\n", garbage);
Antoine Pelisse19182252013-04-10 21:03:24 +0200118 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);
Junio C Hamanoc7432082006-04-27 15:37:18 -0700131 }
Junio C Hamanoc7432082006-04-27 15:37:18 -0700132 return 0;
133}