base85 debug code: Fix length byte calculation
[git/dscho.git] / builtin-verify-pack.c
blobe8fd68bfac024ebc2d994fb743d965faed2c5595
1 #include "builtin.h"
2 #include "cache.h"
3 #include "pack.h"
4 #include "pack-revindex.h"
6 #define MAX_CHAIN 50
8 static void show_pack_info(struct packed_git *p)
10 uint32_t nr_objects, i;
11 int cnt;
12 unsigned long chain_histogram[MAX_CHAIN+1], baseobjects;
14 nr_objects = p->num_objects;
15 memset(chain_histogram, 0, sizeof(chain_histogram));
16 baseobjects = 0;
18 for (i = 0; i < nr_objects; i++) {
19 const unsigned char *sha1;
20 unsigned char base_sha1[20];
21 const char *type;
22 unsigned long size;
23 unsigned long store_size;
24 off_t offset;
25 unsigned int delta_chain_length;
27 sha1 = nth_packed_object_sha1(p, i);
28 if (!sha1)
29 die("internal error pack-check nth-packed-object");
30 offset = nth_packed_object_offset(p, i);
31 type = packed_object_info_detail(p, offset, &size, &store_size,
32 &delta_chain_length,
33 base_sha1);
34 printf("%s ", sha1_to_hex(sha1));
35 if (!delta_chain_length) {
36 printf("%-6s %lu %lu %"PRIuMAX"\n",
37 type, size, store_size, (uintmax_t)offset);
38 baseobjects++;
40 else {
41 printf("%-6s %lu %lu %"PRIuMAX" %u %s\n",
42 type, size, store_size, (uintmax_t)offset,
43 delta_chain_length, sha1_to_hex(base_sha1));
44 if (delta_chain_length <= MAX_CHAIN)
45 chain_histogram[delta_chain_length]++;
46 else
47 chain_histogram[0]++;
51 if (baseobjects)
52 printf("non delta: %lu object%s\n",
53 baseobjects, baseobjects > 1 ? "s" : "");
55 for (cnt = 1; cnt <= MAX_CHAIN; cnt++) {
56 if (!chain_histogram[cnt])
57 continue;
58 printf("chain length = %d: %lu object%s\n", cnt,
59 chain_histogram[cnt],
60 chain_histogram[cnt] > 1 ? "s" : "");
62 if (chain_histogram[0])
63 printf("chain length > %d: %lu object%s\n", MAX_CHAIN,
64 chain_histogram[0],
65 chain_histogram[0] > 1 ? "s" : "");
68 static int verify_one_pack(const char *path, int verbose)
70 char arg[PATH_MAX];
71 int len;
72 struct packed_git *pack;
73 int err;
75 len = strlcpy(arg, path, PATH_MAX);
76 if (len >= PATH_MAX)
77 return error("name too long: %s", path);
80 * In addition to "foo.idx" we accept "foo.pack" and "foo";
81 * normalize these forms to "foo.idx" for add_packed_git().
83 if (has_extension(arg, ".pack")) {
84 strcpy(arg + len - 5, ".idx");
85 len--;
86 } else if (!has_extension(arg, ".idx")) {
87 if (len + 4 >= PATH_MAX)
88 return error("name too long: %s.idx", arg);
89 strcpy(arg + len, ".idx");
90 len += 4;
94 * add_packed_git() uses our buffer (containing "foo.idx") to
95 * build the pack filename ("foo.pack"). Make sure it fits.
97 if (len + 1 >= PATH_MAX) {
98 arg[len - 4] = '\0';
99 return error("name too long: %s.pack", arg);
102 pack = add_packed_git(arg, len, 1);
103 if (!pack)
104 return error("packfile %s not found.", arg);
106 install_packed_git(pack);
107 err = verify_pack(pack);
109 if (verbose) {
110 if (err)
111 printf("%s: bad\n", pack->pack_name);
112 else {
113 show_pack_info(pack);
114 printf("%s: ok\n", pack->pack_name);
118 return err;
121 static const char verify_pack_usage[] = "git-verify-pack [-v] <pack>...";
123 int cmd_verify_pack(int argc, const char **argv, const char *prefix)
125 int err = 0;
126 int verbose = 0;
127 int no_more_options = 0;
128 int nothing_done = 1;
130 git_config(git_default_config, NULL);
131 while (1 < argc) {
132 if (!no_more_options && argv[1][0] == '-') {
133 if (!strcmp("-v", argv[1]))
134 verbose = 1;
135 else if (!strcmp("--", argv[1]))
136 no_more_options = 1;
137 else
138 usage(verify_pack_usage);
140 else {
141 if (verify_one_pack(argv[1], verbose))
142 err = 1;
143 discard_revindex();
144 nothing_done = 0;
146 argc--; argv++;
149 if (nothing_done)
150 usage(verify_pack_usage);
152 return err;