Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 2 | #include "cache.h" |
| 3 | #include "pack.h" |
Nicolas Pitre | 4b480c6 | 2008-08-22 15:45:53 -0400 | [diff] [blame] | 4 | #include "pack-revindex.h" |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 5 | #include "parse-options.h" |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 6 | |
| 7 | #define MAX_CHAIN 50 |
| 8 | |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 9 | #define VERIFY_PACK_VERBOSE 01 |
| 10 | #define VERIFY_PACK_STAT_ONLY 02 |
| 11 | |
| 12 | static void show_pack_info(struct packed_git *p, unsigned int flags) |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 13 | { |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 14 | uint32_t nr_objects, i; |
| 15 | int cnt; |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 16 | int stat_only = flags & VERIFY_PACK_STAT_ONLY; |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 17 | unsigned long chain_histogram[MAX_CHAIN+1], baseobjects; |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 18 | |
| 19 | nr_objects = p->num_objects; |
| 20 | memset(chain_histogram, 0, sizeof(chain_histogram)); |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 21 | baseobjects = 0; |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 22 | |
| 23 | for (i = 0; i < nr_objects; i++) { |
| 24 | const unsigned char *sha1; |
| 25 | unsigned char base_sha1[20]; |
| 26 | const char *type; |
| 27 | unsigned long size; |
| 28 | unsigned long store_size; |
| 29 | off_t offset; |
| 30 | unsigned int delta_chain_length; |
| 31 | |
| 32 | sha1 = nth_packed_object_sha1(p, i); |
| 33 | if (!sha1) |
| 34 | die("internal error pack-check nth-packed-object"); |
| 35 | offset = nth_packed_object_offset(p, i); |
| 36 | type = packed_object_info_detail(p, offset, &size, &store_size, |
| 37 | &delta_chain_length, |
| 38 | base_sha1); |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 39 | if (!stat_only) |
| 40 | printf("%s ", sha1_to_hex(sha1)); |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 41 | if (!delta_chain_length) { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 42 | if (!stat_only) |
| 43 | printf("%-6s %lu %lu %"PRIuMAX"\n", |
| 44 | type, size, store_size, (uintmax_t)offset); |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 45 | baseobjects++; |
| 46 | } |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 47 | else { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 48 | if (!stat_only) |
| 49 | printf("%-6s %lu %lu %"PRIuMAX" %u %s\n", |
| 50 | type, size, store_size, (uintmax_t)offset, |
| 51 | delta_chain_length, sha1_to_hex(base_sha1)); |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 52 | if (delta_chain_length <= MAX_CHAIN) |
| 53 | chain_histogram[delta_chain_length]++; |
| 54 | else |
| 55 | chain_histogram[0]++; |
| 56 | } |
| 57 | } |
| 58 | |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 59 | if (baseobjects) |
| 60 | printf("non delta: %lu object%s\n", |
| 61 | baseobjects, baseobjects > 1 ? "s" : ""); |
| 62 | |
| 63 | for (cnt = 1; cnt <= MAX_CHAIN; cnt++) { |
| 64 | if (!chain_histogram[cnt]) |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 65 | continue; |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 66 | printf("chain length = %d: %lu object%s\n", cnt, |
| 67 | chain_histogram[cnt], |
| 68 | chain_histogram[cnt] > 1 ? "s" : ""); |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 69 | } |
| 70 | if (chain_histogram[0]) |
Junio C Hamano | 262b04f | 2009-08-07 15:36:31 -0700 | [diff] [blame] | 71 | printf("chain length > %d: %lu object%s\n", MAX_CHAIN, |
| 72 | chain_histogram[0], |
| 73 | chain_histogram[0] > 1 ? "s" : ""); |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 76 | static int verify_one_pack(const char *path, unsigned int flags) |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 77 | { |
Rene Scharfe | ae9c86f | 2006-08-10 17:02:32 +0200 | [diff] [blame] | 78 | char arg[PATH_MAX]; |
| 79 | int len; |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 80 | int verbose = flags & VERIFY_PACK_VERBOSE; |
| 81 | int stat_only = flags & VERIFY_PACK_STAT_ONLY; |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 82 | struct packed_git *pack; |
| 83 | int err; |
Rene Scharfe | ae9c86f | 2006-08-10 17:02:32 +0200 | [diff] [blame] | 84 | |
| 85 | len = strlcpy(arg, path, PATH_MAX); |
| 86 | if (len >= PATH_MAX) |
| 87 | return error("name too long: %s", path); |
| 88 | |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 89 | /* |
| 90 | * In addition to "foo.idx" we accept "foo.pack" and "foo"; |
| 91 | * normalize these forms to "foo.idx" for add_packed_git(). |
| 92 | */ |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 93 | if (has_extension(arg, ".pack")) { |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 94 | strcpy(arg + len - 5, ".idx"); |
| 95 | len--; |
Rene Scharfe | 5bb1cda | 2006-08-11 14:01:45 +0200 | [diff] [blame] | 96 | } else if (!has_extension(arg, ".idx")) { |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 97 | if (len + 4 >= PATH_MAX) |
| 98 | return error("name too long: %s.idx", arg); |
| 99 | strcpy(arg + len, ".idx"); |
| 100 | len += 4; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 101 | } |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 102 | |
Rene Scharfe | f711ab5 | 2006-08-10 17:02:36 +0200 | [diff] [blame] | 103 | /* |
| 104 | * add_packed_git() uses our buffer (containing "foo.idx") to |
| 105 | * build the pack filename ("foo.pack"). Make sure it fits. |
| 106 | */ |
| 107 | if (len + 1 >= PATH_MAX) { |
| 108 | arg[len - 4] = '\0'; |
| 109 | return error("name too long: %s.pack", arg); |
| 110 | } |
| 111 | |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 112 | pack = add_packed_git(arg, len, 1); |
| 113 | if (!pack) |
Rene Scharfe | fc5fc50 | 2006-08-10 17:02:34 +0200 | [diff] [blame] | 114 | return error("packfile %s not found.", arg); |
| 115 | |
Nicolas Pitre | 3408146 | 2008-02-28 00:25:18 -0500 | [diff] [blame] | 116 | install_packed_git(pack); |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 117 | |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 118 | if (!stat_only) |
| 119 | err = verify_pack(pack); |
| 120 | else |
| 121 | err = open_pack_index(pack); |
| 122 | |
| 123 | if (verbose || stat_only) { |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 124 | if (err) |
| 125 | printf("%s: bad\n", pack->pack_name); |
| 126 | else { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 127 | show_pack_info(pack, flags); |
| 128 | if (!stat_only) |
| 129 | printf("%s: ok\n", pack->pack_name); |
Nicolas Pitre | 77d3ece | 2008-06-24 23:18:17 -0400 | [diff] [blame] | 130 | } |
| 131 | } |
Rene Scharfe | d0d619c | 2006-08-10 17:02:35 +0200 | [diff] [blame] | 132 | |
| 133 | return err; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 136 | static const char * const verify_pack_usage[] = { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 137 | "git verify-pack [-v|--verbose] [-s|--stat-only] <pack>...", |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 138 | NULL |
| 139 | }; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 140 | |
Rene Scharfe | 2e3ed67 | 2006-08-10 17:02:38 +0200 | [diff] [blame] | 141 | int cmd_verify_pack(int argc, const char **argv, const char *prefix) |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 142 | { |
Rene Scharfe | 0eaf22f | 2006-08-10 17:02:37 +0200 | [diff] [blame] | 143 | int err = 0; |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 144 | unsigned int flags = 0; |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 145 | int i; |
| 146 | const struct option verify_pack_options[] = { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 147 | OPT_BIT('v', "verbose", &flags, "verbose", |
| 148 | VERIFY_PACK_VERBOSE), |
| 149 | OPT_BIT('s', "stat-only", &flags, "show statistics only", |
| 150 | VERIFY_PACK_STAT_ONLY), |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 151 | OPT_END() |
| 152 | }; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 153 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 154 | git_config(git_default_config, NULL); |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 155 | argc = parse_options(argc, argv, prefix, verify_pack_options, |
| 156 | verify_pack_usage, 0); |
| 157 | if (argc < 1) |
| 158 | usage_with_options(verify_pack_usage, verify_pack_options); |
| 159 | for (i = 0; i < argc; i++) { |
Junio C Hamano | 6c4f3ec | 2009-08-07 15:45:30 -0700 | [diff] [blame] | 160 | if (verify_one_pack(argv[i], flags)) |
Stephen Boyd | c9c3c67 | 2009-07-07 22:15:40 -0700 | [diff] [blame] | 161 | err = 1; |
| 162 | discard_revindex(); |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 163 | } |
Rene Scharfe | 6f05b57 | 2006-08-10 17:02:31 +0200 | [diff] [blame] | 164 | |
Rene Scharfe | 0eaf22f | 2006-08-10 17:02:37 +0200 | [diff] [blame] | 165 | return err; |
Junio C Hamano | f925339 | 2005-06-29 02:51:27 -0700 | [diff] [blame] | 166 | } |