Code

am: format is in $patch_format, not parse_patch
[git.git] / builtin-verify-pack.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "pack.h"
4 #include "pack-revindex.h"
5 #include "parse-options.h"
7 #define MAX_CHAIN 50
9 static void show_pack_info(struct packed_git *p)
10 {
11         uint32_t nr_objects, i, chain_histogram[MAX_CHAIN+1];
13         nr_objects = p->num_objects;
14         memset(chain_histogram, 0, sizeof(chain_histogram));
16         for (i = 0; i < nr_objects; i++) {
17                 const unsigned char *sha1;
18                 unsigned char base_sha1[20];
19                 const char *type;
20                 unsigned long size;
21                 unsigned long store_size;
22                 off_t offset;
23                 unsigned int delta_chain_length;
25                 sha1 = nth_packed_object_sha1(p, i);
26                 if (!sha1)
27                         die("internal error pack-check nth-packed-object");
28                 offset = nth_packed_object_offset(p, i);
29                 type = packed_object_info_detail(p, offset, &size, &store_size,
30                                                  &delta_chain_length,
31                                                  base_sha1);
32                 printf("%s ", sha1_to_hex(sha1));
33                 if (!delta_chain_length)
34                         printf("%-6s %lu %lu %"PRIuMAX"\n",
35                                type, size, store_size, (uintmax_t)offset);
36                 else {
37                         printf("%-6s %lu %lu %"PRIuMAX" %u %s\n",
38                                type, size, store_size, (uintmax_t)offset,
39                                delta_chain_length, sha1_to_hex(base_sha1));
40                         if (delta_chain_length <= MAX_CHAIN)
41                                 chain_histogram[delta_chain_length]++;
42                         else
43                                 chain_histogram[0]++;
44                 }
45         }
47         for (i = 0; i <= MAX_CHAIN; i++) {
48                 if (!chain_histogram[i])
49                         continue;
50                 printf("chain length = %"PRIu32": %"PRIu32" object%s\n", i,
51                        chain_histogram[i], chain_histogram[i] > 1 ? "s" : "");
52         }
53         if (chain_histogram[0])
54                 printf("chain length > %d: %"PRIu32" object%s\n", MAX_CHAIN,
55                        chain_histogram[0], chain_histogram[0] > 1 ? "s" : "");
56 }
58 static int verify_one_pack(const char *path, int verbose)
59 {
60         char arg[PATH_MAX];
61         int len;
62         struct packed_git *pack;
63         int err;
65         len = strlcpy(arg, path, PATH_MAX);
66         if (len >= PATH_MAX)
67                 return error("name too long: %s", path);
69         /*
70          * In addition to "foo.idx" we accept "foo.pack" and "foo";
71          * normalize these forms to "foo.idx" for add_packed_git().
72          */
73         if (has_extension(arg, ".pack")) {
74                 strcpy(arg + len - 5, ".idx");
75                 len--;
76         } else if (!has_extension(arg, ".idx")) {
77                 if (len + 4 >= PATH_MAX)
78                         return error("name too long: %s.idx", arg);
79                 strcpy(arg + len, ".idx");
80                 len += 4;
81         }
83         /*
84          * add_packed_git() uses our buffer (containing "foo.idx") to
85          * build the pack filename ("foo.pack").  Make sure it fits.
86          */
87         if (len + 1 >= PATH_MAX) {
88                 arg[len - 4] = '\0';
89                 return error("name too long: %s.pack", arg);
90         }
92         pack = add_packed_git(arg, len, 1);
93         if (!pack)
94                 return error("packfile %s not found.", arg);
96         install_packed_git(pack);
97         err = verify_pack(pack);
99         if (verbose) {
100                 if (err)
101                         printf("%s: bad\n", pack->pack_name);
102                 else {
103                         show_pack_info(pack);
104                         printf("%s: ok\n", pack->pack_name);
105                 }
106         }
108         return err;
111 static const char * const verify_pack_usage[] = {
112         "git verify-pack [-v|--verbose] <pack>...",
113         NULL
114 };
116 int cmd_verify_pack(int argc, const char **argv, const char *prefix)
118         int err = 0;
119         int verbose = 0;
120         int i;
121         const struct option verify_pack_options[] = {
122                 OPT__VERBOSE(&verbose),
123                 OPT_END()
124         };
126         git_config(git_default_config, NULL);
127         argc = parse_options(argc, argv, prefix, verify_pack_options,
128                              verify_pack_usage, 0);
129         if (argc < 1)
130                 usage_with_options(verify_pack_usage, verify_pack_options);
131         for (i = 0; i < argc; i++) {
132                 if (verify_one_pack(argv[i], verbose))
133                         err = 1;
134                 discard_revindex();
135         }
137         return err;