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