git am: skip pine's internal folder data
[git/mergetool.git] / pack-check.c
blobd7dd62bb8346c4cac8dbd7334e999a450c21c5ab
1 #include "cache.h"
2 #include "pack.h"
4 struct idx_entry
6 const unsigned char *sha1;
7 off_t offset;
8 };
10 static int compare_entries(const void *e1, const void *e2)
12 const struct idx_entry *entry1 = e1;
13 const struct idx_entry *entry2 = e2;
14 if (entry1->offset < entry2->offset)
15 return -1;
16 if (entry1->offset > entry2->offset)
17 return 1;
18 return 0;
21 static int verify_packfile(struct packed_git *p,
22 struct pack_window **w_curs)
24 off_t index_size = p->index_size;
25 const unsigned char *index_base = p->index_data;
26 SHA_CTX ctx;
27 unsigned char sha1[20];
28 off_t offset = 0, pack_sig = p->pack_size - 20;
29 uint32_t nr_objects, i;
30 int err;
31 struct idx_entry *entries;
33 /* Note that the pack header checks are actually performed by
34 * use_pack when it first opens the pack file. If anything
35 * goes wrong during those checks then the call will die out
36 * immediately.
39 SHA1_Init(&ctx);
40 while (offset < pack_sig) {
41 unsigned int remaining;
42 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
43 offset += remaining;
44 if (offset > pack_sig)
45 remaining -= (unsigned int)(offset - pack_sig);
46 SHA1_Update(&ctx, in, remaining);
48 SHA1_Final(sha1, &ctx);
49 if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL)))
50 return error("Packfile %s SHA1 mismatch with itself",
51 p->pack_name);
52 if (hashcmp(sha1, index_base + index_size - 40))
53 return error("Packfile %s SHA1 mismatch with idx",
54 p->pack_name);
55 unuse_pack(w_curs);
57 /* Make sure everything reachable from idx is valid. Since we
58 * have verified that nr_objects matches between idx and pack,
59 * we do not do scan-streaming check on the pack file.
61 nr_objects = p->num_objects;
62 entries = xmalloc(nr_objects * sizeof(*entries));
63 /* first sort entries by pack offset, since unpacking them is more efficient that way */
64 for (i = 0; i < nr_objects; i++) {
65 entries[i].sha1 = nth_packed_object_sha1(p, i);
66 if (!entries[i].sha1)
67 die("internal error pack-check nth-packed-object");
68 entries[i].offset = find_pack_entry_one(entries[i].sha1, p);
69 if (!entries[i].offset)
70 die("internal error pack-check find-pack-entry-one");
72 qsort(entries, nr_objects, sizeof(*entries), compare_entries);
74 for (i = 0, err = 0; i < nr_objects; i++) {
75 void *data;
76 enum object_type type;
77 unsigned long size;
79 data = unpack_entry(p, entries[i].offset, &type, &size);
80 if (!data) {
81 err = error("cannot unpack %s from %s",
82 sha1_to_hex(entries[i].sha1), p->pack_name);
83 continue;
85 if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
86 err = error("packed %s from %s is corrupt",
87 sha1_to_hex(entries[i].sha1), p->pack_name);
88 free(data);
89 continue;
91 free(data);
93 free(entries);
95 return err;
99 #define MAX_CHAIN 50
101 static void show_pack_info(struct packed_git *p)
103 uint32_t nr_objects, i, chain_histogram[MAX_CHAIN+1];
104 nr_objects = p->num_objects;
105 memset(chain_histogram, 0, sizeof(chain_histogram));
107 for (i = 0; i < nr_objects; i++) {
108 const unsigned char *sha1;
109 unsigned char base_sha1[20];
110 const char *type;
111 unsigned long size;
112 unsigned long store_size;
113 off_t offset;
114 unsigned int delta_chain_length;
116 sha1 = nth_packed_object_sha1(p, i);
117 if (!sha1)
118 die("internal error pack-check nth-packed-object");
119 offset = find_pack_entry_one(sha1, p);
120 if (!offset)
121 die("internal error pack-check find-pack-entry-one");
123 type = packed_object_info_detail(p, offset, &size, &store_size,
124 &delta_chain_length,
125 base_sha1);
126 printf("%s ", sha1_to_hex(sha1));
127 if (!delta_chain_length)
128 printf("%-6s %lu %"PRIuMAX"\n",
129 type, size, (uintmax_t)offset);
130 else {
131 printf("%-6s %lu %"PRIuMAX" %u %s\n",
132 type, size, (uintmax_t)offset,
133 delta_chain_length, sha1_to_hex(base_sha1));
134 if (delta_chain_length <= MAX_CHAIN)
135 chain_histogram[delta_chain_length]++;
136 else
137 chain_histogram[0]++;
141 for (i = 0; i <= MAX_CHAIN; i++) {
142 if (!chain_histogram[i])
143 continue;
144 printf("chain length = %d: %d object%s\n", i,
145 chain_histogram[i], chain_histogram[i] > 1 ? "s" : "");
147 if (chain_histogram[0])
148 printf("chain length > %d: %d object%s\n", MAX_CHAIN,
149 chain_histogram[0], chain_histogram[0] > 1 ? "s" : "");
152 int verify_pack(struct packed_git *p, int verbose)
154 off_t index_size;
155 const unsigned char *index_base;
156 SHA_CTX ctx;
157 unsigned char sha1[20];
158 int ret;
160 if (open_pack_index(p))
161 return error("packfile %s index not opened", p->pack_name);
162 index_size = p->index_size;
163 index_base = p->index_data;
165 ret = 0;
166 /* Verify SHA1 sum of the index file */
167 SHA1_Init(&ctx);
168 SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
169 SHA1_Final(sha1, &ctx);
170 if (hashcmp(sha1, index_base + index_size - 20))
171 ret = error("Packfile index for %s SHA1 mismatch",
172 p->pack_name);
174 if (!ret) {
175 /* Verify pack file */
176 struct pack_window *w_curs = NULL;
177 ret = verify_packfile(p, &w_curs);
178 unuse_pack(&w_curs);
181 if (verbose) {
182 if (ret)
183 printf("%s: bad\n", p->pack_name);
184 else {
185 show_pack_info(p);
186 printf("%s: ok\n", p->pack_name);
190 return ret;