Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git count-objects". |
| 3 | * |
| 4 | * Copyright (c) 2006 Junio C Hamano |
| 5 | */ |
| 6 | |
| 7 | #include "cache.h" |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 8 | #include "dir.h" |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 9 | #include "builtin.h" |
Pierre Habouzit | 833f3ab | 2007-10-15 22:38:51 +0200 | [diff] [blame] | 10 | #include "parse-options.h" |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 11 | |
| 12 | static 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 Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 25 | if (is_dot_or_dotdot(ent->d_name)) |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 26 | 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 Hamano | fdb2a2a | 2008-08-18 21:57:16 +0200 | [diff] [blame] | 45 | (*loose_size) += xsize_t(on_disk_bytes(st)); |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 46 | } |
| 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 Hamano | cd673c1 | 2009-02-27 23:15:53 -0800 | [diff] [blame] | 63 | if (has_sha1_pack(sha1)) |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 64 | (*packed_loose)++; |
| 65 | } |
| 66 | } |
| 67 | |
Pierre Habouzit | 833f3ab | 2007-10-15 22:38:51 +0200 | [diff] [blame] | 68 | static char const * const count_objects_usage[] = { |
Stephan Beyer | 1b1dd23 | 2008-07-13 15:36:15 +0200 | [diff] [blame] | 69 | "git count-objects [-v]", |
Pierre Habouzit | 833f3ab | 2007-10-15 22:38:51 +0200 | [diff] [blame] | 70 | NULL |
| 71 | }; |
| 72 | |
| 73 | int cmd_count_objects(int argc, const char **argv, const char *prefix) |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 74 | { |
Pierre Habouzit | 833f3ab | 2007-10-15 22:38:51 +0200 | [diff] [blame] | 75 | int i, verbose = 0; |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 76 | 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 Habouzit | 833f3ab | 2007-10-15 22:38:51 +0200 | [diff] [blame] | 81 | struct option opts[] = { |
| 82 | OPT__VERBOSE(&verbose), |
| 83 | OPT_END(), |
| 84 | }; |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 85 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 86 | argc = parse_options(argc, argv, prefix, opts, count_objects_usage, 0); |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 87 | /* we do not take arguments other than flags for now */ |
Pierre Habouzit | 833f3ab | 2007-10-15 22:38:51 +0200 | [diff] [blame] | 88 | if (argc) |
| 89 | usage_with_options(count_objects_usage, opts); |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 90 | 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 Hamano | ae72f68 | 2006-12-27 01:04:03 -0800 | [diff] [blame] | 105 | unsigned long num_pack = 0; |
Marcus Griep | f223824 | 2008-08-15 00:20:20 -0400 | [diff] [blame] | 106 | unsigned long size_pack = 0; |
Junio C Hamano | 80fe7d2 | 2006-05-02 23:03:15 -0700 | [diff] [blame] | 107 | if (!packed_git) |
| 108 | prepare_packed_git(); |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 109 | for (p = packed_git; p; p = p->next) { |
| 110 | if (!p->pack_local) |
| 111 | continue; |
Shawn O. Pearce | eaa8677 | 2007-05-30 02:12:28 -0400 | [diff] [blame] | 112 | if (open_pack_index(p)) |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 113 | continue; |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 114 | packed += p->num_objects; |
Marcus Griep | f223824 | 2008-08-15 00:20:20 -0400 | [diff] [blame] | 115 | size_pack += p->pack_size + p->index_size; |
Junio C Hamano | ae72f68 | 2006-12-27 01:04:03 -0800 | [diff] [blame] | 116 | num_pack++; |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 117 | } |
| 118 | printf("count: %lu\n", loose); |
Junio C Hamano | fdb2a2a | 2008-08-18 21:57:16 +0200 | [diff] [blame] | 119 | printf("size: %lu\n", loose_size / 1024); |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 120 | printf("in-pack: %lu\n", packed); |
Junio C Hamano | ae72f68 | 2006-12-27 01:04:03 -0800 | [diff] [blame] | 121 | printf("packs: %lu\n", num_pack); |
Marcus Griep | f223824 | 2008-08-15 00:20:20 -0400 | [diff] [blame] | 122 | printf("size-pack: %lu\n", size_pack / 1024); |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 123 | printf("prune-packable: %lu\n", packed_loose); |
| 124 | printf("garbage: %lu\n", garbage); |
| 125 | } |
| 126 | else |
| 127 | printf("%lu objects, %lu kilobytes\n", |
Mikael Magnusson | bfd083b | 2008-09-08 19:56:32 +0200 | [diff] [blame] | 128 | loose, loose_size / 1024); |
Junio C Hamano | c743208 | 2006-04-27 15:37:18 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |