]>
Commit | Line | Data |
---|---|---|
2ad9d4cb | 1 | #include "builtin.h" |
9cf6d335 SV |
2 | #include "delta.h" |
3 | #include "pack.h" | |
4 | #include "csum-file.h" | |
8e440259 PE |
5 | #include "blob.h" |
6 | #include "commit.h" | |
7 | #include "tag.h" | |
8 | #include "tree.h" | |
96a02f8f | 9 | #include "progress.h" |
0153be05 | 10 | #include "fsck.h" |
2fb3f6db | 11 | #include "exec_cmd.h" |
9cf6d335 SV |
12 | |
13 | static const char index_pack_usage[] = | |
e337a04d | 14 | "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])"; |
9cf6d335 | 15 | |
9cba13ca | 16 | struct object_entry { |
aa7e44bf | 17 | struct pack_idx_entry idx; |
2d477051 NP |
18 | unsigned long size; |
19 | unsigned int hdr_size; | |
9cf6d335 SV |
20 | enum object_type type; |
21 | enum object_type real_type; | |
38a45561 JH |
22 | unsigned delta_depth; |
23 | int base_object_no; | |
9cf6d335 SV |
24 | }; |
25 | ||
53dda6ff NP |
26 | union delta_base { |
27 | unsigned char sha1[20]; | |
d7dd0223 | 28 | off_t offset; |
53dda6ff NP |
29 | }; |
30 | ||
f41aebd4 | 31 | struct base_data { |
4a438cab SP |
32 | struct base_data *base; |
33 | struct base_data *child; | |
03993e13 | 34 | struct object_entry *obj; |
f41aebd4 SP |
35 | void *data; |
36 | unsigned long size; | |
2baad220 NTND |
37 | int ref_first, ref_last; |
38 | int ofs_first, ofs_last; | |
f41aebd4 SP |
39 | }; |
40 | ||
3c552873 NP |
41 | /* |
42 | * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want | |
43 | * to memcmp() only the first 20 bytes. | |
44 | */ | |
45 | #define UNION_BASE_SZ 20 | |
46 | ||
0153be05 MK |
47 | #define FLAG_LINK (1u<<20) |
48 | #define FLAG_CHECKED (1u<<21) | |
49 | ||
9cba13ca | 50 | struct delta_entry { |
53dda6ff | 51 | union delta_base base; |
636171cb | 52 | int obj_no; |
9cf6d335 SV |
53 | }; |
54 | ||
9cf6d335 SV |
55 | static struct object_entry *objects; |
56 | static struct delta_entry *deltas; | |
4a438cab | 57 | static struct base_data *base_cache; |
92392b4a | 58 | static size_t base_cache_used; |
9cf6d335 SV |
59 | static int nr_objects; |
60 | static int nr_deltas; | |
636171cb | 61 | static int nr_resolved_deltas; |
9cf6d335 | 62 | |
e42797f5 | 63 | static int from_stdin; |
0153be05 | 64 | static int strict; |
3c9af366 NP |
65 | static int verbose; |
66 | ||
dc6a0757 | 67 | static struct progress *progress; |
e42797f5 | 68 | |
2d477051 NP |
69 | /* We always read in 4kB chunks. */ |
70 | static unsigned char input_buffer[4096]; | |
d7dd0223 NP |
71 | static unsigned int input_offset, input_len; |
72 | static off_t consumed_bytes; | |
d1a0ed18 | 73 | static unsigned deepest_delta; |
9126f009 | 74 | static git_SHA_CTX input_ctx; |
ee5743ce | 75 | static uint32_t input_crc32; |
6d2fa7f1 | 76 | static int input_fd, output_fd, pack_fd; |
2d477051 | 77 | |
0153be05 MK |
78 | static int mark_link(struct object *obj, int type, void *data) |
79 | { | |
80 | if (!obj) | |
81 | return -1; | |
82 | ||
83 | if (type != OBJ_ANY && obj->type != type) | |
c2b97ecf | 84 | die(_("object type mismatch at %s"), sha1_to_hex(obj->sha1)); |
0153be05 MK |
85 | |
86 | obj->flags |= FLAG_LINK; | |
87 | return 0; | |
88 | } | |
89 | ||
90 | /* The content of each linked object must have been checked | |
91 | or it must be already present in the object database */ | |
92 | static void check_object(struct object *obj) | |
93 | { | |
94 | if (!obj) | |
95 | return; | |
96 | ||
97 | if (!(obj->flags & FLAG_LINK)) | |
98 | return; | |
99 | ||
100 | if (!(obj->flags & FLAG_CHECKED)) { | |
101 | unsigned long size; | |
102 | int type = sha1_object_info(obj->sha1, &size); | |
103 | if (type != obj->type || type <= 0) | |
c2b97ecf | 104 | die(_("object of unexpected type")); |
0153be05 MK |
105 | obj->flags |= FLAG_CHECKED; |
106 | return; | |
107 | } | |
108 | } | |
109 | ||
110 | static void check_objects(void) | |
111 | { | |
112 | unsigned i, max; | |
113 | ||
114 | max = get_max_object_index(); | |
115 | for (i = 0; i < max; i++) | |
116 | check_object(get_indexed_object(i)); | |
117 | } | |
118 | ||
119 | ||
636171cb | 120 | /* Discard current buffer used content. */ |
a6e8a767 | 121 | static void flush(void) |
636171cb NP |
122 | { |
123 | if (input_offset) { | |
124 | if (output_fd >= 0) | |
125 | write_or_die(output_fd, input_buffer, input_offset); | |
9126f009 | 126 | git_SHA1_Update(&input_ctx, input_buffer, input_offset); |
554a2636 | 127 | memmove(input_buffer, input_buffer + input_offset, input_len); |
636171cb NP |
128 | input_offset = 0; |
129 | } | |
130 | } | |
131 | ||
2d477051 NP |
132 | /* |
133 | * Make sure at least "min" bytes are available in the buffer, and | |
134 | * return the pointer to the buffer. | |
135 | */ | |
b89c4e93 | 136 | static void *fill(int min) |
9cf6d335 | 137 | { |
2d477051 NP |
138 | if (min <= input_len) |
139 | return input_buffer + input_offset; | |
140 | if (min > sizeof(input_buffer)) | |
c2b97ecf NTND |
141 | die(Q_("cannot fill %d byte", |
142 | "cannot fill %d bytes", | |
143 | min), | |
144 | min); | |
636171cb | 145 | flush(); |
2d477051 | 146 | do { |
8a912bcb | 147 | ssize_t ret = xread(input_fd, input_buffer + input_len, |
2d477051 NP |
148 | sizeof(input_buffer) - input_len); |
149 | if (ret <= 0) { | |
150 | if (!ret) | |
c2b97ecf NTND |
151 | die(_("early EOF")); |
152 | die_errno(_("read error on input")); | |
2d477051 NP |
153 | } |
154 | input_len += ret; | |
218558af NP |
155 | if (from_stdin) |
156 | display_throughput(progress, consumed_bytes + input_len); | |
2d477051 NP |
157 | } while (input_len < min); |
158 | return input_buffer; | |
159 | } | |
160 | ||
161 | static void use(int bytes) | |
162 | { | |
163 | if (bytes > input_len) | |
c2b97ecf | 164 | die(_("used more bytes than were available")); |
ee5743ce | 165 | input_crc32 = crc32(input_crc32, input_buffer + input_offset, bytes); |
2d477051 NP |
166 | input_len -= bytes; |
167 | input_offset += bytes; | |
d7dd0223 NP |
168 | |
169 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 170 | if (signed_add_overflows(consumed_bytes, bytes)) |
c2b97ecf | 171 | die(_("pack too large for current definition of off_t")); |
2d477051 NP |
172 | consumed_bytes += bytes; |
173 | } | |
9cf6d335 | 174 | |
3bb72562 | 175 | static const char *open_pack_file(const char *pack_name) |
2d477051 | 176 | { |
e42797f5 NP |
177 | if (from_stdin) { |
178 | input_fd = 0; | |
179 | if (!pack_name) { | |
ab1900a3 ÆAB |
180 | static char tmp_file[PATH_MAX]; |
181 | output_fd = odb_mkstemp(tmp_file, sizeof(tmp_file), | |
6e180cdc | 182 | "pack/tmp_pack_XXXXXX"); |
ab1900a3 | 183 | pack_name = xstrdup(tmp_file); |
e42797f5 NP |
184 | } else |
185 | output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600); | |
186 | if (output_fd < 0) | |
c2b97ecf | 187 | die_errno(_("unable to create '%s'"), pack_name); |
6d2fa7f1 | 188 | pack_fd = output_fd; |
e42797f5 NP |
189 | } else { |
190 | input_fd = open(pack_name, O_RDONLY); | |
191 | if (input_fd < 0) | |
c2b97ecf | 192 | die_errno(_("cannot open packfile '%s'"), pack_name); |
e42797f5 | 193 | output_fd = -1; |
6d2fa7f1 | 194 | pack_fd = input_fd; |
e42797f5 | 195 | } |
9126f009 | 196 | git_SHA1_Init(&input_ctx); |
e42797f5 | 197 | return pack_name; |
9cf6d335 SV |
198 | } |
199 | ||
200 | static void parse_pack_header(void) | |
201 | { | |
2d477051 | 202 | struct pack_header *hdr = fill(sizeof(struct pack_header)); |
9cf6d335 SV |
203 | |
204 | /* Header consistency check */ | |
9cf6d335 | 205 | if (hdr->hdr_signature != htonl(PACK_SIGNATURE)) |
c2b97ecf | 206 | die(_("pack signature mismatch")); |
d60fc1c8 | 207 | if (!pack_version_ok(hdr->hdr_version)) |
6e1c2344 RJ |
208 | die("pack version %"PRIu32" unsupported", |
209 | ntohl(hdr->hdr_version)); | |
9cf6d335 SV |
210 | |
211 | nr_objects = ntohl(hdr->hdr_entries); | |
2d477051 | 212 | use(sizeof(struct pack_header)); |
9cf6d335 SV |
213 | } |
214 | ||
a4f3131c EFL |
215 | static NORETURN void bad_object(unsigned long offset, const char *format, |
216 | ...) __attribute__((format (printf, 2, 3))); | |
9cf6d335 | 217 | |
c2e86add | 218 | static NORETURN void bad_object(unsigned long offset, const char *format, ...) |
9cf6d335 SV |
219 | { |
220 | va_list params; | |
221 | char buf[1024]; | |
222 | ||
223 | va_start(params, format); | |
224 | vsnprintf(buf, sizeof(buf), format, params); | |
225 | va_end(params); | |
c2b97ecf | 226 | die(_("pack has bad object at offset %lu: %s"), offset, buf); |
9cf6d335 SV |
227 | } |
228 | ||
2baad220 NTND |
229 | static struct base_data *alloc_base_data(void) |
230 | { | |
231 | struct base_data *base = xmalloc(sizeof(struct base_data)); | |
232 | memset(base, 0, sizeof(*base)); | |
233 | base->ref_last = -1; | |
234 | base->ofs_last = -1; | |
235 | return base; | |
236 | } | |
237 | ||
6a87ed97 NP |
238 | static void free_base_data(struct base_data *c) |
239 | { | |
240 | if (c->data) { | |
241 | free(c->data); | |
242 | c->data = NULL; | |
243 | base_cache_used -= c->size; | |
244 | } | |
245 | } | |
246 | ||
92392b4a SP |
247 | static void prune_base_data(struct base_data *retain) |
248 | { | |
8e24cbae | 249 | struct base_data *b; |
92392b4a SP |
250 | for (b = base_cache; |
251 | base_cache_used > delta_base_cache_limit && b; | |
252 | b = b->child) { | |
6a87ed97 NP |
253 | if (b->data && b != retain) |
254 | free_base_data(b); | |
92392b4a SP |
255 | } |
256 | } | |
257 | ||
4a438cab SP |
258 | static void link_base_data(struct base_data *base, struct base_data *c) |
259 | { | |
260 | if (base) | |
261 | base->child = c; | |
262 | else | |
263 | base_cache = c; | |
264 | ||
265 | c->base = base; | |
266 | c->child = NULL; | |
9441b61d NP |
267 | if (c->data) |
268 | base_cache_used += c->size; | |
92392b4a | 269 | prune_base_data(c); |
4a438cab SP |
270 | } |
271 | ||
272 | static void unlink_base_data(struct base_data *c) | |
273 | { | |
274 | struct base_data *base = c->base; | |
275 | if (base) | |
276 | base->child = NULL; | |
277 | else | |
278 | base_cache = NULL; | |
6a87ed97 | 279 | free_base_data(c); |
4a438cab SP |
280 | } |
281 | ||
2d477051 | 282 | static void *unpack_entry_data(unsigned long offset, unsigned long size) |
9cf6d335 | 283 | { |
7ce4721a | 284 | int status; |
ef49a7a0 | 285 | git_zstream stream; |
9cf6d335 SV |
286 | void *buf = xmalloc(size); |
287 | ||
288 | memset(&stream, 0, sizeof(stream)); | |
7ce4721a | 289 | git_inflate_init(&stream); |
9cf6d335 SV |
290 | stream.next_out = buf; |
291 | stream.avail_out = size; | |
9cf6d335 | 292 | |
7ce4721a | 293 | do { |
2d477051 NP |
294 | stream.next_in = fill(1); |
295 | stream.avail_in = input_len; | |
7ce4721a NP |
296 | status = git_inflate(&stream, 0); |
297 | use(input_len - stream.avail_in); | |
298 | } while (status == Z_OK); | |
299 | if (stream.total_out != size || status != Z_STREAM_END) | |
c2b97ecf | 300 | bad_object(offset, _("inflate returned %d"), status); |
39c68542 | 301 | git_inflate_end(&stream); |
9cf6d335 SV |
302 | return buf; |
303 | } | |
304 | ||
2d477051 | 305 | static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base) |
9cf6d335 | 306 | { |
48fb7deb LT |
307 | unsigned char *p; |
308 | unsigned long size, c; | |
d7dd0223 | 309 | off_t base_offset; |
9cf6d335 | 310 | unsigned shift; |
ee5743ce | 311 | void *data; |
9cf6d335 | 312 | |
aa7e44bf | 313 | obj->idx.offset = consumed_bytes; |
1e4cd68c | 314 | input_crc32 = crc32(0, NULL, 0); |
2d477051 NP |
315 | |
316 | p = fill(1); | |
317 | c = *p; | |
318 | use(1); | |
319 | obj->type = (c >> 4) & 7; | |
9cf6d335 SV |
320 | size = (c & 15); |
321 | shift = 4; | |
322 | while (c & 0x80) { | |
2d477051 NP |
323 | p = fill(1); |
324 | c = *p; | |
325 | use(1); | |
48fb7deb | 326 | size += (c & 0x7f) << shift; |
9cf6d335 SV |
327 | shift += 7; |
328 | } | |
2d477051 | 329 | obj->size = size; |
9cf6d335 | 330 | |
2d477051 | 331 | switch (obj->type) { |
eb32d236 | 332 | case OBJ_REF_DELTA: |
2d477051 NP |
333 | hashcpy(delta_base->sha1, fill(20)); |
334 | use(20); | |
53dda6ff NP |
335 | break; |
336 | case OBJ_OFS_DELTA: | |
337 | memset(delta_base, 0, sizeof(*delta_base)); | |
2d477051 NP |
338 | p = fill(1); |
339 | c = *p; | |
340 | use(1); | |
53dda6ff NP |
341 | base_offset = c & 127; |
342 | while (c & 128) { | |
343 | base_offset += 1; | |
8723f216 | 344 | if (!base_offset || MSB(base_offset, 7)) |
c2b97ecf | 345 | bad_object(obj->idx.offset, _("offset value overflow for delta base object")); |
2d477051 NP |
346 | p = fill(1); |
347 | c = *p; | |
348 | use(1); | |
53dda6ff NP |
349 | base_offset = (base_offset << 7) + (c & 127); |
350 | } | |
aa7e44bf | 351 | delta_base->offset = obj->idx.offset - base_offset; |
d8f32556 | 352 | if (delta_base->offset <= 0 || delta_base->offset >= obj->idx.offset) |
c2b97ecf | 353 | bad_object(obj->idx.offset, _("delta base offset is out of bound")); |
53dda6ff | 354 | break; |
9cf6d335 SV |
355 | case OBJ_COMMIT: |
356 | case OBJ_TREE: | |
357 | case OBJ_BLOB: | |
358 | case OBJ_TAG: | |
9cf6d335 SV |
359 | break; |
360 | default: | |
c2b97ecf | 361 | bad_object(obj->idx.offset, _("unknown object type %d"), obj->type); |
9cf6d335 | 362 | } |
aa7e44bf | 363 | obj->hdr_size = consumed_bytes - obj->idx.offset; |
2d477051 | 364 | |
aa7e44bf GB |
365 | data = unpack_entry_data(obj->idx.offset, obj->size); |
366 | obj->idx.crc32 = input_crc32; | |
ee5743ce | 367 | return data; |
2d477051 NP |
368 | } |
369 | ||
b89c4e93 | 370 | static void *get_data_from_pack(struct object_entry *obj) |
2d477051 | 371 | { |
a91ef6e7 | 372 | off_t from = obj[0].idx.offset + obj[0].hdr_size; |
aa7e44bf | 373 | unsigned long len = obj[1].idx.offset - from; |
776ea370 | 374 | unsigned char *data, *inbuf; |
ef49a7a0 | 375 | git_zstream stream; |
776ea370 | 376 | int status; |
9cf6d335 | 377 | |
2d477051 | 378 | data = xmalloc(obj->size); |
776ea370 NP |
379 | inbuf = xmalloc((len < 64*1024) ? len : 64*1024); |
380 | ||
2d477051 | 381 | memset(&stream, 0, sizeof(stream)); |
776ea370 | 382 | git_inflate_init(&stream); |
2d477051 NP |
383 | stream.next_out = data; |
384 | stream.avail_out = obj->size; | |
776ea370 NP |
385 | |
386 | do { | |
387 | ssize_t n = (len < 64*1024) ? len : 64*1024; | |
388 | n = pread(pack_fd, inbuf, n, from); | |
389 | if (n < 0) | |
c2b97ecf | 390 | die_errno(_("cannot pread pack file")); |
776ea370 | 391 | if (!n) |
c2b97ecf NTND |
392 | die(Q_("premature end of pack file, %lu byte missing", |
393 | "premature end of pack file, %lu bytes missing", | |
394 | len), | |
395 | len); | |
776ea370 NP |
396 | from += n; |
397 | len -= n; | |
398 | stream.next_in = inbuf; | |
399 | stream.avail_in = n; | |
400 | status = git_inflate(&stream, 0); | |
401 | } while (len && status == Z_OK && !stream.avail_in); | |
402 | ||
403 | /* This has been inflated OK when first encountered, so... */ | |
404 | if (status != Z_STREAM_END || stream.total_out != obj->size) | |
c2b97ecf | 405 | die(_("serious inflate inconsistency")); |
776ea370 NP |
406 | |
407 | git_inflate_end(&stream); | |
408 | free(inbuf); | |
9cf6d335 SV |
409 | return data; |
410 | } | |
411 | ||
7218a215 JH |
412 | static int compare_delta_bases(const union delta_base *base1, |
413 | const union delta_base *base2, | |
414 | enum object_type type1, | |
415 | enum object_type type2) | |
416 | { | |
417 | int cmp = type1 - type2; | |
418 | if (cmp) | |
419 | return cmp; | |
420 | return memcmp(base1, base2, UNION_BASE_SZ); | |
421 | } | |
422 | ||
423 | static int find_delta(const union delta_base *base, enum object_type type) | |
9cf6d335 SV |
424 | { |
425 | int first = 0, last = nr_deltas; | |
426 | ||
427 | while (first < last) { | |
428 | int next = (first + last) / 2; | |
429 | struct delta_entry *delta = &deltas[next]; | |
430 | int cmp; | |
431 | ||
7218a215 JH |
432 | cmp = compare_delta_bases(base, &delta->base, |
433 | type, objects[delta->obj_no].type); | |
9cf6d335 SV |
434 | if (!cmp) |
435 | return next; | |
436 | if (cmp < 0) { | |
437 | last = next; | |
438 | continue; | |
439 | } | |
440 | first = next+1; | |
441 | } | |
442 | return -first-1; | |
443 | } | |
444 | ||
6a87ed97 | 445 | static void find_delta_children(const union delta_base *base, |
7218a215 JH |
446 | int *first_index, int *last_index, |
447 | enum object_type type) | |
9cf6d335 | 448 | { |
7218a215 | 449 | int first = find_delta(base, type); |
9cf6d335 SV |
450 | int last = first; |
451 | int end = nr_deltas - 1; | |
452 | ||
6a87ed97 NP |
453 | if (first < 0) { |
454 | *first_index = 0; | |
455 | *last_index = -1; | |
456 | return; | |
457 | } | |
3c552873 | 458 | while (first > 0 && !memcmp(&deltas[first - 1].base, base, UNION_BASE_SZ)) |
9cf6d335 | 459 | --first; |
3c552873 | 460 | while (last < end && !memcmp(&deltas[last + 1].base, base, UNION_BASE_SZ)) |
9cf6d335 SV |
461 | ++last; |
462 | *first_index = first; | |
463 | *last_index = last; | |
9cf6d335 SV |
464 | } |
465 | ||
466 | static void sha1_object(const void *data, unsigned long size, | |
9096c660 | 467 | enum object_type type, unsigned char *sha1) |
9cf6d335 | 468 | { |
ce9fbf16 | 469 | hash_sha1_file(data, size, typename(type), sha1); |
9096c660 | 470 | if (has_sha1_file(sha1)) { |
8685da42 NP |
471 | void *has_data; |
472 | enum object_type has_type; | |
473 | unsigned long has_size; | |
474 | has_data = read_sha1_file(sha1, &has_type, &has_size); | |
475 | if (!has_data) | |
c2b97ecf | 476 | die(_("cannot read existing object %s"), sha1_to_hex(sha1)); |
8685da42 NP |
477 | if (size != has_size || type != has_type || |
478 | memcmp(data, has_data, size) != 0) | |
c2b97ecf | 479 | die(_("SHA1 COLLISION FOUND WITH %s !"), sha1_to_hex(sha1)); |
bbf4b41b | 480 | free(has_data); |
8685da42 | 481 | } |
0153be05 MK |
482 | if (strict) { |
483 | if (type == OBJ_BLOB) { | |
484 | struct blob *blob = lookup_blob(sha1); | |
485 | if (blob) | |
486 | blob->object.flags |= FLAG_CHECKED; | |
487 | else | |
c2b97ecf | 488 | die(_("invalid blob object %s"), sha1_to_hex(sha1)); |
0153be05 MK |
489 | } else { |
490 | struct object *obj; | |
491 | int eaten; | |
492 | void *buf = (void *) data; | |
493 | ||
494 | /* | |
495 | * we do not need to free the memory here, as the | |
496 | * buf is deleted by the caller. | |
497 | */ | |
498 | obj = parse_object_buffer(sha1, type, size, buf, &eaten); | |
499 | if (!obj) | |
c2b97ecf | 500 | die(_("invalid %s"), typename(type)); |
0153be05 | 501 | if (fsck_object(obj, 1, fsck_error_function)) |
c2b97ecf | 502 | die(_("Error in object")); |
2af202be | 503 | if (fsck_walk(obj, mark_link, NULL)) |
c2b97ecf | 504 | die(_("Not all child objects of %s are reachable"), sha1_to_hex(obj->sha1)); |
0153be05 MK |
505 | |
506 | if (obj->type == OBJ_TREE) { | |
507 | struct tree *item = (struct tree *) obj; | |
508 | item->buffer = NULL; | |
509 | } | |
510 | if (obj->type == OBJ_COMMIT) { | |
511 | struct commit *commit = (struct commit *) obj; | |
512 | commit->buffer = NULL; | |
513 | } | |
514 | obj->flags |= FLAG_CHECKED; | |
515 | } | |
516 | } | |
9cf6d335 SV |
517 | } |
518 | ||
4f8ec74e JH |
519 | static int is_delta_type(enum object_type type) |
520 | { | |
521 | return (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA); | |
522 | } | |
523 | ||
20e95d0a NTND |
524 | /* |
525 | * This function is part of find_unresolved_deltas(). There are two | |
526 | * walkers going in the opposite ways. | |
527 | * | |
528 | * The first one in find_unresolved_deltas() traverses down from | |
529 | * parent node to children, deflating nodes along the way. However, | |
530 | * memory for deflated nodes is limited by delta_base_cache_limit, so | |
531 | * at some point parent node's deflated content may be freed. | |
532 | * | |
533 | * The second walker is this function, which goes from current node up | |
534 | * to top parent if necessary to deflate the node. In normal | |
535 | * situation, its parent node would be already deflated, so it just | |
536 | * needs to apply delta. | |
537 | * | |
538 | * In the worst case scenario, parent node is no longer deflated because | |
539 | * we're running out of delta_base_cache_limit; we need to re-deflate | |
540 | * parents, possibly up to the top base. | |
541 | * | |
542 | * All deflated objects here are subject to be freed if we exceed | |
543 | * delta_base_cache_limit, just like in find_unresolved_deltas(), we | |
544 | * just need to make sure the last node is not freed. | |
545 | */ | |
92392b4a SP |
546 | static void *get_base_data(struct base_data *c) |
547 | { | |
548 | if (!c->data) { | |
549 | struct object_entry *obj = c->obj; | |
20e95d0a NTND |
550 | struct base_data **delta = NULL; |
551 | int delta_nr = 0, delta_alloc = 0; | |
92392b4a | 552 | |
20e95d0a NTND |
553 | while (is_delta_type(c->obj->type) && !c->data) { |
554 | ALLOC_GROW(delta, delta_nr + 1, delta_alloc); | |
555 | delta[delta_nr++] = c; | |
556 | c = c->base; | |
557 | } | |
558 | if (!delta_nr) { | |
559 | c->data = get_data_from_pack(obj); | |
560 | c->size = obj->size; | |
561 | base_cache_used += c->size; | |
562 | prune_base_data(c); | |
563 | } | |
564 | for (; delta_nr > 0; delta_nr--) { | |
565 | void *base, *raw; | |
566 | c = delta[delta_nr - 1]; | |
567 | obj = c->obj; | |
568 | base = get_base_data(c->base); | |
569 | raw = get_data_from_pack(obj); | |
92392b4a SP |
570 | c->data = patch_delta( |
571 | base, c->base->size, | |
572 | raw, obj->size, | |
573 | &c->size); | |
574 | free(raw); | |
575 | if (!c->data) | |
c2b97ecf | 576 | bad_object(obj->idx.offset, _("failed to apply delta")); |
20e95d0a NTND |
577 | base_cache_used += c->size; |
578 | prune_base_data(c); | |
9441b61d | 579 | } |
20e95d0a | 580 | free(delta); |
92392b4a SP |
581 | } |
582 | return c->data; | |
583 | } | |
584 | ||
f41aebd4 | 585 | static void resolve_delta(struct object_entry *delta_obj, |
9441b61d | 586 | struct base_data *base, struct base_data *result) |
9cf6d335 | 587 | { |
ce3f6dc6 | 588 | void *base_data, *delta_data; |
9cf6d335 | 589 | |
ce3f6dc6 | 590 | delta_obj->real_type = base->obj->real_type; |
38a45561 | 591 | delta_obj->delta_depth = base->obj->delta_depth + 1; |
d1a0ed18 JH |
592 | if (deepest_delta < delta_obj->delta_depth) |
593 | deepest_delta = delta_obj->delta_depth; | |
38a45561 | 594 | delta_obj->base_object_no = base->obj - objects; |
636171cb | 595 | delta_data = get_data_from_pack(delta_obj); |
ce3f6dc6 | 596 | base_data = get_base_data(base); |
9441b61d | 597 | result->obj = delta_obj; |
ce3f6dc6 NP |
598 | result->data = patch_delta(base_data, base->size, |
599 | delta_data, delta_obj->size, &result->size); | |
9cf6d335 | 600 | free(delta_data); |
9441b61d | 601 | if (!result->data) |
c2b97ecf | 602 | bad_object(delta_obj->idx.offset, _("failed to apply delta")); |
9441b61d NP |
603 | sha1_object(result->data, result->size, delta_obj->real_type, |
604 | delta_obj->idx.sha1); | |
636171cb | 605 | nr_resolved_deltas++; |
9441b61d NP |
606 | } |
607 | ||
2baad220 NTND |
608 | static struct base_data *find_unresolved_deltas_1(struct base_data *base, |
609 | struct base_data *prev_base) | |
9441b61d | 610 | { |
2baad220 | 611 | if (base->ref_last == -1 && base->ofs_last == -1) { |
9441b61d NP |
612 | union delta_base base_spec; |
613 | ||
614 | hashcpy(base_spec.sha1, base->obj->idx.sha1); | |
7218a215 | 615 | find_delta_children(&base_spec, |
2baad220 | 616 | &base->ref_first, &base->ref_last, OBJ_REF_DELTA); |
53dda6ff | 617 | |
9441b61d NP |
618 | memset(&base_spec, 0, sizeof(base_spec)); |
619 | base_spec.offset = base->obj->idx.offset; | |
7218a215 | 620 | find_delta_children(&base_spec, |
2baad220 | 621 | &base->ofs_first, &base->ofs_last, OBJ_OFS_DELTA); |
4a438cab | 622 | |
2baad220 NTND |
623 | if (base->ref_last == -1 && base->ofs_last == -1) { |
624 | free(base->data); | |
625 | return NULL; | |
626 | } | |
53dda6ff | 627 | |
2baad220 NTND |
628 | link_base_data(prev_base, base); |
629 | } | |
4a438cab | 630 | |
2baad220 NTND |
631 | if (base->ref_first <= base->ref_last) { |
632 | struct object_entry *child = objects + deltas[base->ref_first].obj_no; | |
633 | struct base_data *result = alloc_base_data(); | |
7218a215 JH |
634 | |
635 | assert(child->real_type == OBJ_REF_DELTA); | |
2baad220 NTND |
636 | resolve_delta(child, base, result); |
637 | if (base->ref_first == base->ref_last && base->ofs_last == -1) | |
7218a215 | 638 | free_base_data(base); |
2baad220 NTND |
639 | |
640 | base->ref_first++; | |
641 | return result; | |
53dda6ff NP |
642 | } |
643 | ||
2baad220 NTND |
644 | if (base->ofs_first <= base->ofs_last) { |
645 | struct object_entry *child = objects + deltas[base->ofs_first].obj_no; | |
646 | struct base_data *result = alloc_base_data(); | |
7218a215 JH |
647 | |
648 | assert(child->real_type == OBJ_OFS_DELTA); | |
2baad220 NTND |
649 | resolve_delta(child, base, result); |
650 | if (base->ofs_first == base->ofs_last) | |
7218a215 | 651 | free_base_data(base); |
2baad220 NTND |
652 | |
653 | base->ofs_first++; | |
654 | return result; | |
9cf6d335 | 655 | } |
53dda6ff | 656 | |
9441b61d | 657 | unlink_base_data(base); |
2baad220 NTND |
658 | return NULL; |
659 | } | |
660 | ||
661 | static void find_unresolved_deltas(struct base_data *base) | |
662 | { | |
663 | struct base_data *new_base, *prev_base = NULL; | |
664 | for (;;) { | |
665 | new_base = find_unresolved_deltas_1(base, prev_base); | |
666 | ||
667 | if (new_base) { | |
668 | prev_base = base; | |
669 | base = new_base; | |
670 | } else { | |
671 | free(base); | |
672 | base = prev_base; | |
673 | if (!base) | |
674 | return; | |
675 | prev_base = base->base; | |
676 | } | |
677 | } | |
9cf6d335 SV |
678 | } |
679 | ||
680 | static int compare_delta_entry(const void *a, const void *b) | |
681 | { | |
682 | const struct delta_entry *delta_a = a; | |
683 | const struct delta_entry *delta_b = b; | |
7218a215 JH |
684 | |
685 | /* group by type (ref vs ofs) and then by value (sha-1 or offset) */ | |
686 | return compare_delta_bases(&delta_a->base, &delta_b->base, | |
687 | objects[delta_a->obj_no].type, | |
688 | objects[delta_b->obj_no].type); | |
9cf6d335 SV |
689 | } |
690 | ||
2d477051 NP |
691 | /* Parse all objects and return the pack content SHA1 hash */ |
692 | static void parse_pack_objects(unsigned char *sha1) | |
9cf6d335 | 693 | { |
96a02f8f | 694 | int i; |
53dda6ff | 695 | struct delta_entry *delta = deltas; |
2d477051 | 696 | struct stat st; |
9cf6d335 SV |
697 | |
698 | /* | |
699 | * First pass: | |
700 | * - find locations of all objects; | |
701 | * - calculate SHA1 of all non-delta objects; | |
b89c4e93 | 702 | * - remember base (SHA1 or offset) for all deltas. |
9cf6d335 | 703 | */ |
13aaf148 | 704 | if (verbose) |
29e63ed3 | 705 | progress = start_progress( |
c2b97ecf | 706 | from_stdin ? _("Receiving objects") : _("Indexing objects"), |
29e63ed3 | 707 | nr_objects); |
9cf6d335 SV |
708 | for (i = 0; i < nr_objects; i++) { |
709 | struct object_entry *obj = &objects[i]; | |
f41aebd4 | 710 | void *data = unpack_raw_entry(obj, &delta->base); |
9cf6d335 | 711 | obj->real_type = obj->type; |
4f8ec74e | 712 | if (is_delta_type(obj->type)) { |
53dda6ff | 713 | nr_deltas++; |
636171cb | 714 | delta->obj_no = i; |
53dda6ff | 715 | delta++; |
9cf6d335 | 716 | } else |
aa7e44bf | 717 | sha1_object(data, obj->size, obj->type, obj->idx.sha1); |
9cf6d335 | 718 | free(data); |
4d4fcc54 | 719 | display_progress(progress, i+1); |
9cf6d335 | 720 | } |
aa7e44bf | 721 | objects[i].idx.offset = consumed_bytes; |
4d4fcc54 | 722 | stop_progress(&progress); |
2d477051 NP |
723 | |
724 | /* Check pack integrity */ | |
636171cb | 725 | flush(); |
9126f009 | 726 | git_SHA1_Final(sha1, &input_ctx); |
2d477051 | 727 | if (hashcmp(fill(20), sha1)) |
c2b97ecf | 728 | die(_("pack is corrupted (SHA1 mismatch)")); |
9bee2478 | 729 | use(20); |
2d477051 NP |
730 | |
731 | /* If input_fd is a file, we should have reached its end now. */ | |
732 | if (fstat(input_fd, &st)) | |
c2b97ecf | 733 | die_errno(_("cannot fstat packfile")); |
fa257b05 JS |
734 | if (S_ISREG(st.st_mode) && |
735 | lseek(input_fd, 0, SEEK_CUR) - input_len != st.st_size) | |
c2b97ecf | 736 | die(_("pack has junk at the end")); |
9cf6d335 | 737 | |
3c9af366 NP |
738 | if (!nr_deltas) |
739 | return; | |
740 | ||
53dda6ff | 741 | /* Sort deltas by base SHA1/offset for fast searching */ |
9cf6d335 SV |
742 | qsort(deltas, nr_deltas, sizeof(struct delta_entry), |
743 | compare_delta_entry); | |
744 | ||
745 | /* | |
746 | * Second pass: | |
747 | * - for all non-delta objects, look if it is used as a base for | |
748 | * deltas; | |
749 | * - if used as a base, uncompress the object and apply all deltas, | |
750 | * recursively checking if the resulting object is used as a base | |
751 | * for some more deltas. | |
752 | */ | |
13aaf148 | 753 | if (verbose) |
c2b97ecf | 754 | progress = start_progress(_("Resolving deltas"), nr_deltas); |
9cf6d335 SV |
755 | for (i = 0; i < nr_objects; i++) { |
756 | struct object_entry *obj = &objects[i]; | |
2baad220 | 757 | struct base_data *base_obj = alloc_base_data(); |
9cf6d335 | 758 | |
4f8ec74e | 759 | if (is_delta_type(obj->type)) |
9cf6d335 | 760 | continue; |
2baad220 NTND |
761 | base_obj->obj = obj; |
762 | base_obj->data = NULL; | |
763 | find_unresolved_deltas(base_obj); | |
4d4fcc54 | 764 | display_progress(progress, nr_resolved_deltas); |
9cf6d335 | 765 | } |
636171cb NP |
766 | } |
767 | ||
8522148f | 768 | static int write_compressed(struct sha1file *f, void *in, unsigned int size) |
636171cb | 769 | { |
ef49a7a0 | 770 | git_zstream stream; |
7734d7f2 NP |
771 | int status; |
772 | unsigned char outbuf[4096]; | |
636171cb NP |
773 | |
774 | memset(&stream, 0, sizeof(stream)); | |
55bb5c91 | 775 | git_deflate_init(&stream, zlib_compression_level); |
636171cb NP |
776 | stream.next_in = in; |
777 | stream.avail_in = size; | |
636171cb | 778 | |
7734d7f2 NP |
779 | do { |
780 | stream.next_out = outbuf; | |
781 | stream.avail_out = sizeof(outbuf); | |
55bb5c91 | 782 | status = git_deflate(&stream, Z_FINISH); |
7734d7f2 NP |
783 | sha1write(f, outbuf, sizeof(outbuf) - stream.avail_out); |
784 | } while (status == Z_OK); | |
785 | ||
786 | if (status != Z_STREAM_END) | |
c2b97ecf | 787 | die(_("unable to deflate appended object (%d)"), status); |
636171cb | 788 | size = stream.total_out; |
55bb5c91 | 789 | git_deflate_end(&stream); |
636171cb NP |
790 | return size; |
791 | } | |
792 | ||
8522148f | 793 | static struct object_entry *append_obj_to_pack(struct sha1file *f, |
03993e13 | 794 | const unsigned char *sha1, void *buf, |
636171cb NP |
795 | unsigned long size, enum object_type type) |
796 | { | |
797 | struct object_entry *obj = &objects[nr_objects++]; | |
798 | unsigned char header[10]; | |
799 | unsigned long s = size; | |
800 | int n = 0; | |
801 | unsigned char c = (type << 4) | (s & 15); | |
802 | s >>= 4; | |
803 | while (s) { | |
804 | header[n++] = c | 0x80; | |
805 | c = s & 0x7f; | |
806 | s >>= 7; | |
807 | } | |
808 | header[n++] = c; | |
8522148f NP |
809 | crc32_begin(f); |
810 | sha1write(f, header, n); | |
72de2883 BS |
811 | obj[0].size = size; |
812 | obj[0].hdr_size = n; | |
813 | obj[0].type = type; | |
814 | obj[0].real_type = type; | |
aa7e44bf | 815 | obj[1].idx.offset = obj[0].idx.offset + n; |
8522148f NP |
816 | obj[1].idx.offset += write_compressed(f, buf, size); |
817 | obj[0].idx.crc32 = crc32_end(f); | |
838cd346 | 818 | sha1flush(f); |
aa7e44bf | 819 | hashcpy(obj->idx.sha1, sha1); |
03993e13 | 820 | return obj; |
636171cb NP |
821 | } |
822 | ||
823 | static int delta_pos_compare(const void *_a, const void *_b) | |
824 | { | |
825 | struct delta_entry *a = *(struct delta_entry **)_a; | |
826 | struct delta_entry *b = *(struct delta_entry **)_b; | |
827 | return a->obj_no - b->obj_no; | |
828 | } | |
9cf6d335 | 829 | |
8522148f | 830 | static void fix_unresolved_deltas(struct sha1file *f, int nr_unresolved) |
636171cb NP |
831 | { |
832 | struct delta_entry **sorted_by_pos; | |
96a02f8f | 833 | int i, n = 0; |
636171cb NP |
834 | |
835 | /* | |
836 | * Since many unresolved deltas may well be themselves base objects | |
837 | * for more unresolved deltas, we really want to include the | |
838 | * smallest number of base objects that would cover as much delta | |
839 | * as possible by picking the | |
840 | * trunc deltas first, allowing for other deltas to resolve without | |
841 | * additional base objects. Since most base objects are to be found | |
842 | * before deltas depending on them, a good heuristic is to start | |
843 | * resolving deltas in the same order as their position in the pack. | |
844 | */ | |
845 | sorted_by_pos = xmalloc(nr_unresolved * sizeof(*sorted_by_pos)); | |
9cf6d335 | 846 | for (i = 0; i < nr_deltas; i++) { |
636171cb NP |
847 | if (objects[deltas[i].obj_no].real_type != OBJ_REF_DELTA) |
848 | continue; | |
849 | sorted_by_pos[n++] = &deltas[i]; | |
9cf6d335 | 850 | } |
636171cb NP |
851 | qsort(sorted_by_pos, n, sizeof(*sorted_by_pos), delta_pos_compare); |
852 | ||
853 | for (i = 0; i < n; i++) { | |
854 | struct delta_entry *d = sorted_by_pos[i]; | |
21666f1a | 855 | enum object_type type; |
2baad220 | 856 | struct base_data *base_obj = alloc_base_data(); |
636171cb NP |
857 | |
858 | if (objects[d->obj_no].real_type != OBJ_REF_DELTA) | |
859 | continue; | |
2baad220 NTND |
860 | base_obj->data = read_sha1_file(d->base.sha1, &type, &base_obj->size); |
861 | if (!base_obj->data) | |
636171cb | 862 | continue; |
636171cb | 863 | |
2baad220 NTND |
864 | if (check_sha1_signature(d->base.sha1, base_obj->data, |
865 | base_obj->size, typename(type))) | |
c2b97ecf | 866 | die(_("local object %s is corrupt"), sha1_to_hex(d->base.sha1)); |
2baad220 NTND |
867 | base_obj->obj = append_obj_to_pack(f, d->base.sha1, |
868 | base_obj->data, base_obj->size, type); | |
869 | find_unresolved_deltas(base_obj); | |
4d4fcc54 | 870 | display_progress(progress, nr_resolved_deltas); |
636171cb NP |
871 | } |
872 | free(sorted_by_pos); | |
873 | } | |
874 | ||
e42797f5 NP |
875 | static void final(const char *final_pack_name, const char *curr_pack_name, |
876 | const char *final_index_name, const char *curr_index_name, | |
b8077709 | 877 | const char *keep_name, const char *keep_msg, |
e42797f5 NP |
878 | unsigned char *sha1) |
879 | { | |
3a55602e | 880 | const char *report = "pack"; |
e42797f5 NP |
881 | char name[PATH_MAX]; |
882 | int err; | |
883 | ||
884 | if (!from_stdin) { | |
885 | close(input_fd); | |
886 | } else { | |
4c81b03e | 887 | fsync_or_die(output_fd, curr_pack_name); |
e42797f5 NP |
888 | err = close(output_fd); |
889 | if (err) | |
c2b97ecf | 890 | die_errno(_("error while closing pack file")); |
e42797f5 NP |
891 | } |
892 | ||
b8077709 SP |
893 | if (keep_msg) { |
894 | int keep_fd, keep_msg_len = strlen(keep_msg); | |
6e180cdc JH |
895 | |
896 | if (!keep_name) | |
897 | keep_fd = odb_pack_keep(name, sizeof(name), sha1); | |
898 | else | |
899 | keep_fd = open(keep_name, O_RDWR|O_CREAT|O_EXCL, 0600); | |
900 | ||
9ca4a201 NP |
901 | if (keep_fd < 0) { |
902 | if (errno != EEXIST) | |
c2b97ecf | 903 | die_errno(_("cannot write keep file '%s'"), |
d824cbba | 904 | keep_name); |
9ca4a201 NP |
905 | } else { |
906 | if (keep_msg_len > 0) { | |
907 | write_or_die(keep_fd, keep_msg, keep_msg_len); | |
908 | write_or_die(keep_fd, "\n", 1); | |
909 | } | |
91c8d590 | 910 | if (close(keep_fd) != 0) |
c2b97ecf | 911 | die_errno(_("cannot close written keep file '%s'"), |
d824cbba | 912 | keep_name); |
576162a4 | 913 | report = "keep"; |
b8077709 | 914 | } |
b8077709 SP |
915 | } |
916 | ||
e42797f5 NP |
917 | if (final_pack_name != curr_pack_name) { |
918 | if (!final_pack_name) { | |
919 | snprintf(name, sizeof(name), "%s/pack/pack-%s.pack", | |
920 | get_object_directory(), sha1_to_hex(sha1)); | |
921 | final_pack_name = name; | |
922 | } | |
923 | if (move_temp_to_file(curr_pack_name, final_pack_name)) | |
c2b97ecf | 924 | die(_("cannot store pack file")); |
fb8b1936 | 925 | } else if (from_stdin) |
33b65030 | 926 | chmod(final_pack_name, 0444); |
e42797f5 | 927 | |
e42797f5 NP |
928 | if (final_index_name != curr_index_name) { |
929 | if (!final_index_name) { | |
930 | snprintf(name, sizeof(name), "%s/pack/pack-%s.idx", | |
931 | get_object_directory(), sha1_to_hex(sha1)); | |
932 | final_index_name = name; | |
933 | } | |
934 | if (move_temp_to_file(curr_index_name, final_index_name)) | |
c2b97ecf | 935 | die(_("cannot store index file")); |
fb8b1936 JH |
936 | } else |
937 | chmod(final_index_name, 0444); | |
576162a4 NP |
938 | |
939 | if (!from_stdin) { | |
940 | printf("%s\n", sha1_to_hex(sha1)); | |
941 | } else { | |
942 | char buf[48]; | |
943 | int len = snprintf(buf, sizeof(buf), "%s\t%s\n", | |
944 | report, sha1_to_hex(sha1)); | |
d1b2ddc8 | 945 | write_or_die(1, buf, len); |
576162a4 NP |
946 | |
947 | /* | |
948 | * Let's just mimic git-unpack-objects here and write | |
949 | * the last part of the input buffer to stdout. | |
950 | */ | |
951 | while (input_len) { | |
952 | err = xwrite(1, input_buffer + input_offset, input_len); | |
953 | if (err <= 0) | |
954 | break; | |
955 | input_len -= err; | |
956 | input_offset += err; | |
957 | } | |
958 | } | |
9cf6d335 SV |
959 | } |
960 | ||
ef90d6d4 | 961 | static int git_index_pack_config(const char *k, const char *v, void *cb) |
4d00bda2 | 962 | { |
ebcfb379 JH |
963 | struct pack_idx_option *opts = cb; |
964 | ||
4d00bda2 | 965 | if (!strcmp(k, "pack.indexversion")) { |
ebcfb379 JH |
966 | opts->version = git_config_int(k, v); |
967 | if (opts->version > 2) | |
968 | die("bad pack.indexversion=%"PRIu32, opts->version); | |
4d00bda2 NP |
969 | return 0; |
970 | } | |
ef90d6d4 | 971 | return git_default_config(k, v, cb); |
4d00bda2 NP |
972 | } |
973 | ||
3c9fc074 JH |
974 | static int cmp_uint32(const void *a_, const void *b_) |
975 | { | |
976 | uint32_t a = *((uint32_t *)a_); | |
977 | uint32_t b = *((uint32_t *)b_); | |
978 | ||
979 | return (a < b) ? -1 : (a != b); | |
980 | } | |
981 | ||
982 | static void read_v2_anomalous_offsets(struct packed_git *p, | |
983 | struct pack_idx_option *opts) | |
984 | { | |
985 | const uint32_t *idx1, *idx2; | |
986 | uint32_t i; | |
987 | ||
988 | /* The address of the 4-byte offset table */ | |
989 | idx1 = (((const uint32_t *)p->index_data) | |
990 | + 2 /* 8-byte header */ | |
991 | + 256 /* fan out */ | |
992 | + 5 * p->num_objects /* 20-byte SHA-1 table */ | |
993 | + p->num_objects /* CRC32 table */ | |
994 | ); | |
995 | ||
996 | /* The address of the 8-byte offset table */ | |
997 | idx2 = idx1 + p->num_objects; | |
998 | ||
999 | for (i = 0; i < p->num_objects; i++) { | |
1000 | uint32_t off = ntohl(idx1[i]); | |
1001 | if (!(off & 0x80000000)) | |
1002 | continue; | |
1003 | off = off & 0x7fffffff; | |
1004 | if (idx2[off * 2]) | |
1005 | continue; | |
1006 | /* | |
1007 | * The real offset is ntohl(idx2[off * 2]) in high 4 | |
1008 | * octets, and ntohl(idx2[off * 2 + 1]) in low 4 | |
1009 | * octets. But idx2[off * 2] is Zero!!! | |
1010 | */ | |
1011 | ALLOC_GROW(opts->anomaly, opts->anomaly_nr + 1, opts->anomaly_alloc); | |
1012 | opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]); | |
1013 | } | |
1014 | ||
1015 | if (1 < opts->anomaly_nr) | |
1016 | qsort(opts->anomaly, opts->anomaly_nr, sizeof(uint32_t), cmp_uint32); | |
1017 | } | |
1018 | ||
e337a04d JH |
1019 | static void read_idx_option(struct pack_idx_option *opts, const char *pack_name) |
1020 | { | |
1021 | struct packed_git *p = add_packed_git(pack_name, strlen(pack_name), 1); | |
1022 | ||
1023 | if (!p) | |
c2b97ecf | 1024 | die(_("Cannot open existing pack file '%s'"), pack_name); |
e337a04d | 1025 | if (open_pack_index(p)) |
c2b97ecf | 1026 | die(_("Cannot open existing pack idx file for '%s'"), pack_name); |
e337a04d JH |
1027 | |
1028 | /* Read the attributes from the existing idx file */ | |
1029 | opts->version = p->index_version; | |
1030 | ||
3c9fc074 JH |
1031 | if (opts->version == 2) |
1032 | read_v2_anomalous_offsets(p, opts); | |
1033 | ||
e337a04d JH |
1034 | /* |
1035 | * Get rid of the idx file as we do not need it anymore. | |
1036 | * NEEDSWORK: extract this bit from free_pack_by_name() in | |
1037 | * sha1_file.c, perhaps? It shouldn't matter very much as we | |
1038 | * know we haven't installed this pack (hence we never have | |
1039 | * read anything from it). | |
1040 | */ | |
1041 | close_pack_index(p); | |
1042 | free(p); | |
1043 | } | |
1044 | ||
38a45561 JH |
1045 | static void show_pack_info(int stat_only) |
1046 | { | |
d1a0ed18 JH |
1047 | int i, baseobjects = nr_objects - nr_deltas; |
1048 | unsigned long *chain_histogram = NULL; | |
1049 | ||
1050 | if (deepest_delta) | |
1051 | chain_histogram = xcalloc(deepest_delta, sizeof(unsigned long)); | |
1052 | ||
38a45561 JH |
1053 | for (i = 0; i < nr_objects; i++) { |
1054 | struct object_entry *obj = &objects[i]; | |
1055 | ||
d1a0ed18 JH |
1056 | if (is_delta_type(obj->type)) |
1057 | chain_histogram[obj->delta_depth - 1]++; | |
38a45561 JH |
1058 | if (stat_only) |
1059 | continue; | |
1060 | printf("%s %-6s %lu %lu %"PRIuMAX, | |
1061 | sha1_to_hex(obj->idx.sha1), | |
1062 | typename(obj->real_type), obj->size, | |
1063 | (unsigned long)(obj[1].idx.offset - obj->idx.offset), | |
1064 | (uintmax_t)obj->idx.offset); | |
1065 | if (is_delta_type(obj->type)) { | |
1066 | struct object_entry *bobj = &objects[obj->base_object_no]; | |
1067 | printf(" %u %s", obj->delta_depth, sha1_to_hex(bobj->idx.sha1)); | |
1068 | } | |
1069 | putchar('\n'); | |
1070 | } | |
d1a0ed18 JH |
1071 | |
1072 | if (baseobjects) | |
c2b97ecf NTND |
1073 | printf_ln(Q_("non delta: %d object", |
1074 | "non delta: %d objects", | |
1075 | baseobjects), | |
1076 | baseobjects); | |
d1a0ed18 JH |
1077 | for (i = 0; i < deepest_delta; i++) { |
1078 | if (!chain_histogram[i]) | |
1079 | continue; | |
c2b97ecf NTND |
1080 | printf_ln(Q_("chain length = %d: %lu object", |
1081 | "chain length = %d: %lu objects", | |
1082 | chain_histogram[i]), | |
1083 | i + 1, | |
1084 | chain_histogram[i]); | |
d1a0ed18 | 1085 | } |
38a45561 JH |
1086 | } |
1087 | ||
3bb72562 | 1088 | int cmd_index_pack(int argc, const char **argv, const char *prefix) |
9cf6d335 | 1089 | { |
38a45561 | 1090 | int i, fix_thin_pack = 0, verify = 0, stat_only = 0, stat = 0; |
3bb72562 LT |
1091 | const char *curr_pack, *curr_index; |
1092 | const char *index_name = NULL, *pack_name = NULL; | |
b8077709 SP |
1093 | const char *keep_name = NULL, *keep_msg = NULL; |
1094 | char *index_name_buf = NULL, *keep_name_buf = NULL; | |
aa7e44bf | 1095 | struct pack_idx_entry **idx_objects; |
ebcfb379 | 1096 | struct pack_idx_option opts; |
8522148f | 1097 | unsigned char pack_sha1[20]; |
9cf6d335 | 1098 | |
99caeed0 JN |
1099 | if (argc == 2 && !strcmp(argv[1], "-h")) |
1100 | usage(index_pack_usage); | |
1101 | ||
6e2a09d2 NE |
1102 | read_replace_refs = 0; |
1103 | ||
ebcfb379 JH |
1104 | reset_pack_idx_option(&opts); |
1105 | git_config(git_index_pack_config, &opts); | |
3f8099fc | 1106 | if (prefix && chdir(prefix)) |
c2b97ecf | 1107 | die(_("Cannot come back to cwd")); |
4d00bda2 | 1108 | |
9cf6d335 | 1109 | for (i = 1; i < argc; i++) { |
3bb72562 | 1110 | const char *arg = argv[i]; |
9cf6d335 SV |
1111 | |
1112 | if (*arg == '-') { | |
e42797f5 NP |
1113 | if (!strcmp(arg, "--stdin")) { |
1114 | from_stdin = 1; | |
636171cb NP |
1115 | } else if (!strcmp(arg, "--fix-thin")) { |
1116 | fix_thin_pack = 1; | |
0153be05 MK |
1117 | } else if (!strcmp(arg, "--strict")) { |
1118 | strict = 1; | |
e337a04d JH |
1119 | } else if (!strcmp(arg, "--verify")) { |
1120 | verify = 1; | |
38a45561 JH |
1121 | } else if (!strcmp(arg, "--verify-stat")) { |
1122 | verify = 1; | |
1123 | stat = 1; | |
1124 | } else if (!strcmp(arg, "--verify-stat-only")) { | |
1125 | verify = 1; | |
1126 | stat = 1; | |
1127 | stat_only = 1; | |
b8077709 SP |
1128 | } else if (!strcmp(arg, "--keep")) { |
1129 | keep_msg = ""; | |
cc44c765 | 1130 | } else if (!prefixcmp(arg, "--keep=")) { |
b8077709 | 1131 | keep_msg = arg + 7; |
cc44c765 | 1132 | } else if (!prefixcmp(arg, "--pack_header=")) { |
bed006fb NP |
1133 | struct pack_header *hdr; |
1134 | char *c; | |
1135 | ||
1136 | hdr = (struct pack_header *)input_buffer; | |
1137 | hdr->hdr_signature = htonl(PACK_SIGNATURE); | |
1138 | hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); | |
1139 | if (*c != ',') | |
c2b97ecf | 1140 | die(_("bad %s"), arg); |
bed006fb NP |
1141 | hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); |
1142 | if (*c) | |
c2b97ecf | 1143 | die(_("bad %s"), arg); |
bed006fb | 1144 | input_len = sizeof(*hdr); |
3c9af366 NP |
1145 | } else if (!strcmp(arg, "-v")) { |
1146 | verbose = 1; | |
e42797f5 | 1147 | } else if (!strcmp(arg, "-o")) { |
9cf6d335 SV |
1148 | if (index_name || (i+1) >= argc) |
1149 | usage(index_pack_usage); | |
1150 | index_name = argv[++i]; | |
4ba7d711 NP |
1151 | } else if (!prefixcmp(arg, "--index-version=")) { |
1152 | char *c; | |
ebcfb379 JH |
1153 | opts.version = strtoul(arg + 16, &c, 10); |
1154 | if (opts.version > 2) | |
c2b97ecf | 1155 | die(_("bad %s"), arg); |
4ba7d711 | 1156 | if (*c == ',') |
ebcfb379 JH |
1157 | opts.off32_limit = strtoul(c+1, &c, 0); |
1158 | if (*c || opts.off32_limit & 0x80000000) | |
c2b97ecf | 1159 | die(_("bad %s"), arg); |
9cf6d335 SV |
1160 | } else |
1161 | usage(index_pack_usage); | |
1162 | continue; | |
1163 | } | |
1164 | ||
1165 | if (pack_name) | |
1166 | usage(index_pack_usage); | |
1167 | pack_name = arg; | |
1168 | } | |
1169 | ||
e42797f5 | 1170 | if (!pack_name && !from_stdin) |
9cf6d335 | 1171 | usage(index_pack_usage); |
636171cb | 1172 | if (fix_thin_pack && !from_stdin) |
c2b97ecf | 1173 | die(_("--fix-thin cannot be used without --stdin")); |
e42797f5 | 1174 | if (!index_name && pack_name) { |
9cf6d335 | 1175 | int len = strlen(pack_name); |
5bb1cda5 | 1176 | if (!has_extension(pack_name, ".pack")) |
c2b97ecf | 1177 | die(_("packfile name '%s' does not end with '.pack'"), |
9cf6d335 | 1178 | pack_name); |
6689f087 | 1179 | index_name_buf = xmalloc(len); |
9cf6d335 SV |
1180 | memcpy(index_name_buf, pack_name, len - 5); |
1181 | strcpy(index_name_buf + len - 5, ".idx"); | |
1182 | index_name = index_name_buf; | |
1183 | } | |
b8077709 SP |
1184 | if (keep_msg && !keep_name && pack_name) { |
1185 | int len = strlen(pack_name); | |
1186 | if (!has_extension(pack_name, ".pack")) | |
c2b97ecf | 1187 | die(_("packfile name '%s' does not end with '.pack'"), |
b8077709 SP |
1188 | pack_name); |
1189 | keep_name_buf = xmalloc(len); | |
1190 | memcpy(keep_name_buf, pack_name, len - 5); | |
1191 | strcpy(keep_name_buf + len - 5, ".keep"); | |
1192 | keep_name = keep_name_buf; | |
1193 | } | |
e337a04d JH |
1194 | if (verify) { |
1195 | if (!index_name) | |
c2b97ecf | 1196 | die(_("--verify with no packfile name given")); |
e337a04d | 1197 | read_idx_option(&opts, index_name); |
68be2fea | 1198 | opts.flags |= WRITE_IDX_VERIFY | WRITE_IDX_STRICT; |
e337a04d | 1199 | } |
68be2fea JH |
1200 | if (strict) |
1201 | opts.flags |= WRITE_IDX_STRICT; | |
9cf6d335 | 1202 | |
e42797f5 | 1203 | curr_pack = open_pack_file(pack_name); |
9cf6d335 | 1204 | parse_pack_header(); |
38a45561 JH |
1205 | objects = xcalloc(nr_objects + 1, sizeof(struct object_entry)); |
1206 | deltas = xcalloc(nr_objects, sizeof(struct delta_entry)); | |
8522148f | 1207 | parse_pack_objects(pack_sha1); |
96a02f8f | 1208 | if (nr_deltas == nr_resolved_deltas) { |
4d4fcc54 | 1209 | stop_progress(&progress); |
96a02f8f NP |
1210 | /* Flush remaining pack final 20-byte SHA1. */ |
1211 | flush(); | |
1212 | } else { | |
636171cb | 1213 | if (fix_thin_pack) { |
8522148f NP |
1214 | struct sha1file *f; |
1215 | unsigned char read_sha1[20], tail_sha1[20]; | |
a984a06a | 1216 | char msg[48]; |
636171cb | 1217 | int nr_unresolved = nr_deltas - nr_resolved_deltas; |
3c9af366 | 1218 | int nr_objects_initial = nr_objects; |
636171cb | 1219 | if (nr_unresolved <= 0) |
c2b97ecf | 1220 | die(_("confusion beyond insanity")); |
636171cb NP |
1221 | objects = xrealloc(objects, |
1222 | (nr_objects + nr_unresolved + 1) | |
1223 | * sizeof(*objects)); | |
8522148f NP |
1224 | f = sha1fd(output_fd, curr_pack); |
1225 | fix_unresolved_deltas(f, nr_unresolved); | |
a984a06a NP |
1226 | sprintf(msg, "completed with %d local objects", |
1227 | nr_objects - nr_objects_initial); | |
1228 | stop_progress_msg(&progress, msg); | |
8522148f NP |
1229 | sha1close(f, tail_sha1, 0); |
1230 | hashcpy(read_sha1, pack_sha1); | |
1231 | fixup_pack_header_footer(output_fd, pack_sha1, | |
abeb40e5 | 1232 | curr_pack, nr_objects, |
8522148f NP |
1233 | read_sha1, consumed_bytes-20); |
1234 | if (hashcmp(read_sha1, tail_sha1) != 0) | |
1235 | die("Unexpected tail checksum for %s " | |
1236 | "(disk corruption?)", curr_pack); | |
636171cb NP |
1237 | } |
1238 | if (nr_deltas != nr_resolved_deltas) | |
c2b97ecf NTND |
1239 | die(Q_("pack has %d unresolved delta", |
1240 | "pack has %d unresolved deltas", | |
1241 | nr_deltas - nr_resolved_deltas), | |
636171cb | 1242 | nr_deltas - nr_resolved_deltas); |
636171cb | 1243 | } |
9cf6d335 | 1244 | free(deltas); |
0153be05 MK |
1245 | if (strict) |
1246 | check_objects(); | |
aa7e44bf | 1247 | |
38a45561 JH |
1248 | if (stat) |
1249 | show_pack_info(stat_only); | |
1250 | ||
aa7e44bf GB |
1251 | idx_objects = xmalloc((nr_objects) * sizeof(struct pack_idx_entry *)); |
1252 | for (i = 0; i < nr_objects; i++) | |
1253 | idx_objects[i] = &objects[i].idx; | |
ebcfb379 | 1254 | curr_index = write_idx_file(index_name, idx_objects, nr_objects, &opts, pack_sha1); |
aa7e44bf GB |
1255 | free(idx_objects); |
1256 | ||
e337a04d JH |
1257 | if (!verify) |
1258 | final(pack_name, curr_pack, | |
1259 | index_name, curr_index, | |
1260 | keep_name, keep_msg, | |
1261 | pack_sha1); | |
1262 | else | |
1263 | close(input_fd); | |
9cf6d335 SV |
1264 | free(objects); |
1265 | free(index_name_buf); | |
b8077709 | 1266 | free(keep_name_buf); |
c85228ed | 1267 | if (pack_name == NULL) |
3bb72562 | 1268 | free((void *) curr_pack); |
c85228ed | 1269 | if (index_name == NULL) |
3bb72562 | 1270 | free((void *) curr_index); |
9cf6d335 SV |
1271 | |
1272 | return 0; | |
1273 | } |