blob: 1b0b6c84ea4279df56b82c5a406adbea59e58513 [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
12static void count_objects(DIR *d, char *path, int len, int verbose,
13 unsigned long *loose,
14 unsigned long *loose_size,
15 unsigned long *packed_loose,
16 unsigned long *garbage)
17{
18 struct dirent *ent;
19 while ((ent = readdir(d)) != NULL) {
20 char hex[41];
21 unsigned char sha1[20];
22 const char *cp;
23 int bad = 0;
24
Alexander Potashev8ca12c02009-01-10 15:07:50 +030025 if (is_dot_or_dotdot(ent->d_name))
Junio C Hamanoc7432082006-04-27 15:37:18 -070026 continue;
27 for (cp = ent->d_name; *cp; cp++) {
28 int ch = *cp;
29 if (('0' <= ch && ch <= '9') ||
30 ('a' <= ch && ch <= 'f'))
31 continue;
32 bad = 1;
33 break;
34 }
35 if (cp - ent->d_name != 38)
36 bad = 1;
37 else {
38 struct stat st;
39 memcpy(path + len + 3, ent->d_name, 38);
40 path[len + 2] = '/';
41 path[len + 41] = 0;
42 if (lstat(path, &st) || !S_ISREG(st.st_mode))
43 bad = 1;
44 else
Junio C Hamanofdb2a2a2008-08-18 21:57:16 +020045 (*loose_size) += xsize_t(on_disk_bytes(st));
Junio C Hamanoc7432082006-04-27 15:37:18 -070046 }
47 if (bad) {
48 if (verbose) {
49 error("garbage found: %.*s/%s",
50 len + 2, path, ent->d_name);
51 (*garbage)++;
52 }
53 continue;
54 }
55 (*loose)++;
56 if (!verbose)
57 continue;
58 memcpy(hex, path+len, 2);
59 memcpy(hex+2, ent->d_name, 38);
60 hex[40] = 0;
61 if (get_sha1_hex(hex, sha1))
62 die("internal error");
Junio C Hamanocd673c12009-02-27 23:15:53 -080063 if (has_sha1_pack(sha1))
Junio C Hamanoc7432082006-04-27 15:37:18 -070064 (*packed_loose)++;
65 }
66}
67
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020068static char const * const count_objects_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +020069 "git count-objects [-v]",
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020070 NULL
71};
72
73int cmd_count_objects(int argc, const char **argv, const char *prefix)
Junio C Hamanoc7432082006-04-27 15:37:18 -070074{
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020075 int i, verbose = 0;
Junio C Hamanoc7432082006-04-27 15:37:18 -070076 const char *objdir = get_object_directory();
77 int len = strlen(objdir);
78 char *path = xmalloc(len + 50);
79 unsigned long loose = 0, packed = 0, packed_loose = 0, garbage = 0;
80 unsigned long loose_size = 0;
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020081 struct option opts[] = {
82 OPT__VERBOSE(&verbose),
83 OPT_END(),
84 };
Junio C Hamanoc7432082006-04-27 15:37:18 -070085
Stephen Boyd37782922009-05-23 11:53:12 -070086 argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0);
Junio C Hamanoc7432082006-04-27 15:37:18 -070087 /* we do not take arguments other than flags for now */
Pierre Habouzit833f3ab2007-10-15 22:38:51 +020088 if (argc)
89 usage_with_options(count_objects_usage, opts);
Junio C Hamanoc7432082006-04-27 15:37:18 -070090 memcpy(path, objdir, len);
91 if (len && objdir[len-1] != '/')
92 path[len++] = '/';
93 for (i = 0; i < 256; i++) {
94 DIR *d;
95 sprintf(path + len, "%02x", i);
96 d = opendir(path);
97 if (!d)
98 continue;
99 count_objects(d, path, len, verbose,
100 &loose, &loose_size, &packed_loose, &garbage);
101 closedir(d);
102 }
103 if (verbose) {
104 struct packed_git *p;
Junio C Hamanoae72f682006-12-27 01:04:03 -0800105 unsigned long num_pack = 0;
Marcus Griepf2238242008-08-15 00:20:20 -0400106 unsigned long size_pack = 0;
Junio C Hamano80fe7d22006-05-02 23:03:15 -0700107 if (!packed_git)
108 prepare_packed_git();
Junio C Hamanoc7432082006-04-27 15:37:18 -0700109 for (p = packed_git; p; p = p->next) {
110 if (!p->pack_local)
111 continue;
Shawn O. Pearceeaa86772007-05-30 02:12:28 -0400112 if (open_pack_index(p))
Shawn O. Pearced0798372007-05-26 01:24:19 -0400113 continue;
Nicolas Pitre57059092007-04-09 01:06:28 -0400114 packed += p->num_objects;
Marcus Griepf2238242008-08-15 00:20:20 -0400115 size_pack += p->pack_size + p->index_size;
Junio C Hamanoae72f682006-12-27 01:04:03 -0800116 num_pack++;
Junio C Hamanoc7432082006-04-27 15:37:18 -0700117 }
118 printf("count: %lu\n", loose);
Junio C Hamanofdb2a2a2008-08-18 21:57:16 +0200119 printf("size: %lu\n", loose_size / 1024);
Junio C Hamanoc7432082006-04-27 15:37:18 -0700120 printf("in-pack: %lu\n", packed);
Junio C Hamanoae72f682006-12-27 01:04:03 -0800121 printf("packs: %lu\n", num_pack);
Marcus Griepf2238242008-08-15 00:20:20 -0400122 printf("size-pack: %lu\n", size_pack / 1024);
Junio C Hamanoc7432082006-04-27 15:37:18 -0700123 printf("prune-packable: %lu\n", packed_loose);
124 printf("garbage: %lu\n", garbage);
125 }
126 else
127 printf("%lu objects, %lu kilobytes\n",
Mikael Magnussonbfd083b2008-09-08 19:56:32 +0200128 loose, loose_size / 1024);
Junio C Hamanoc7432082006-04-27 15:37:18 -0700129 return 0;
130}