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