]>
Commit | Line | Data |
---|---|---|
64413630 | 1 | #include "builtin.h" |
bad50dc8 | 2 | #include "cache.h" |
425d290c | 3 | #include "bulk-checkin.h" |
b2141fc1 | 4 | #include "config.h" |
cbd53a21 | 5 | #include "object-store.h" |
74536958 | 6 | #include "object.h" |
8ee378a0 | 7 | #include "delta.h" |
a733cb60 | 8 | #include "pack.h" |
8e440259 PE |
9 | #include "blob.h" |
10 | #include "commit.h" | |
11 | #include "tag.h" | |
12 | #include "tree.h" | |
b41860bf | 13 | #include "tree-walk.h" |
96a02f8f | 14 | #include "progress.h" |
2add1e6d | 15 | #include "decorate.h" |
b41860bf | 16 | #include "fsck.h" |
bad50dc8 | 17 | |
b41860bf | 18 | static int dry_run, quiet, recover, has_errors, strict; |
33e8fc87 | 19 | static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]"; |
bad50dc8 | 20 | |
67e5a5ec LT |
21 | /* We always read in 4kB chunks. */ |
22 | static unsigned char buffer[4096]; | |
d7dd0223 NP |
23 | static unsigned int offset, len; |
24 | static off_t consumed_bytes; | |
5ad21867 | 25 | static off_t max_input_size; |
3206b6bd | 26 | static git_hash_ctx ctx; |
22410549 | 27 | static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; |
bae60ba7 | 28 | static struct progress *progress; |
bad50dc8 | 29 | |
f2898cfa JH |
30 | /* |
31 | * When running under --strict mode, objects whose reachability are | |
32 | * suspect are kept in core without getting written in the object | |
33 | * store. | |
34 | */ | |
2add1e6d MK |
35 | struct obj_buffer { |
36 | char *buffer; | |
37 | unsigned long size; | |
38 | }; | |
39 | ||
40 | static struct decoration obj_decorate; | |
41 | ||
42 | static struct obj_buffer *lookup_object_buffer(struct object *base) | |
43 | { | |
44 | return lookup_decoration(&obj_decorate, base); | |
45 | } | |
46 | ||
b41860bf MK |
47 | static void add_object_buffer(struct object *object, char *buffer, unsigned long size) |
48 | { | |
49 | struct obj_buffer *obj; | |
ca56dadb | 50 | CALLOC_ARRAY(obj, 1); |
b41860bf MK |
51 | obj->buffer = buffer; |
52 | obj->size = size; | |
53 | if (add_decoration(&obj_decorate, object, obj)) | |
f2fd0760 | 54 | die("object %s tried to add buffer twice!", oid_to_hex(&object->oid)); |
b41860bf MK |
55 | } |
56 | ||
67e5a5ec LT |
57 | /* |
58 | * Make sure at least "min" bytes are available in the buffer, and | |
59 | * return the pointer to the buffer. | |
60 | */ | |
79a65697 | 61 | static void *fill(int min) |
67e5a5ec LT |
62 | { |
63 | if (min <= len) | |
64 | return buffer + offset; | |
67e5a5ec LT |
65 | if (min > sizeof(buffer)) |
66 | die("cannot fill %d bytes", min); | |
67 | if (offset) { | |
3206b6bd | 68 | the_hash_algo->update_fn(&ctx, buffer, offset); |
79a65697 | 69 | memmove(buffer, buffer + offset, len); |
67e5a5ec LT |
70 | offset = 0; |
71 | } | |
72 | do { | |
8a912bcb | 73 | ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len); |
67e5a5ec LT |
74 | if (ret <= 0) { |
75 | if (!ret) | |
76 | die("early EOF"); | |
d824cbba | 77 | die_errno("read error on input"); |
67e5a5ec LT |
78 | } |
79 | len += ret; | |
80 | } while (len < min); | |
81 | return buffer; | |
82 | } | |
74536958 | 83 | |
67e5a5ec LT |
84 | static void use(int bytes) |
85 | { | |
86 | if (bytes > len) | |
87 | die("used more bytes than were available"); | |
88 | len -= bytes; | |
89 | offset += bytes; | |
d7dd0223 NP |
90 | |
91 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 92 | if (signed_add_overflows(consumed_bytes, bytes)) |
d7dd0223 | 93 | die("pack too large for current definition of off_t"); |
209c554a | 94 | consumed_bytes += bytes; |
5ad21867 CC |
95 | if (max_input_size && consumed_bytes > max_input_size) |
96 | die(_("pack exceeds maximum allowed size")); | |
bae60ba7 | 97 | display_throughput(progress, consumed_bytes); |
67e5a5ec | 98 | } |
bad50dc8 | 99 | |
a1bf5ca2 HX |
100 | /* |
101 | * Decompress zstream from the standard input into a newly | |
102 | * allocated buffer of specified size and return the buffer. | |
103 | * The caller is responsible to free the returned buffer. | |
104 | * | |
105 | * But for dry_run mode, "get_data()" is only used to check the | |
106 | * integrity of data, and the returned buffer is not used at all. | |
107 | * Therefore, in dry_run mode, "get_data()" will release the small | |
108 | * allocated buffer which is reused to hold temporary zstream output | |
109 | * and return NULL instead of returning garbage data. | |
110 | */ | |
67e5a5ec | 111 | static void *get_data(unsigned long size) |
bad50dc8 | 112 | { |
ef49a7a0 | 113 | git_zstream stream; |
a1bf5ca2 HX |
114 | unsigned long bufsize = dry_run && size > 8192 ? 8192 : size; |
115 | void *buf = xmallocz(bufsize); | |
67e5a5ec | 116 | |
67e5a5ec LT |
117 | memset(&stream, 0, sizeof(stream)); |
118 | ||
119 | stream.next_out = buf; | |
a1bf5ca2 | 120 | stream.avail_out = bufsize; |
67e5a5ec LT |
121 | stream.next_in = fill(1); |
122 | stream.avail_in = len; | |
39c68542 | 123 | git_inflate_init(&stream); |
67e5a5ec LT |
124 | |
125 | for (;;) { | |
39c68542 | 126 | int ret = git_inflate(&stream, 0); |
67e5a5ec LT |
127 | use(len - stream.avail_in); |
128 | if (stream.total_out == size && ret == Z_STREAM_END) | |
129 | break; | |
f986f2c8 | 130 | if (ret != Z_OK) { |
82247e9b | 131 | error("inflate returned %d", ret); |
6a83d902 | 132 | FREE_AND_NULL(buf); |
3b67d291 | 133 | if (!recover) |
f986f2c8 JH |
134 | exit(1); |
135 | has_errors = 1; | |
136 | break; | |
137 | } | |
67e5a5ec LT |
138 | stream.next_in = fill(1); |
139 | stream.avail_in = len; | |
a1bf5ca2 HX |
140 | if (dry_run) { |
141 | /* reuse the buffer in dry_run mode */ | |
142 | stream.next_out = buf; | |
143 | stream.avail_out = bufsize > size - stream.total_out ? | |
144 | size - stream.total_out : | |
145 | bufsize; | |
146 | } | |
67e5a5ec | 147 | } |
39c68542 | 148 | git_inflate_end(&stream); |
a1bf5ca2 HX |
149 | if (dry_run) |
150 | FREE_AND_NULL(buf); | |
67e5a5ec | 151 | return buf; |
bad50dc8 LT |
152 | } |
153 | ||
67e5a5ec | 154 | struct delta_info { |
834bc47b | 155 | struct object_id base_oid; |
d7dd0223 NP |
156 | unsigned nr; |
157 | off_t base_offset; | |
67e5a5ec LT |
158 | unsigned long size; |
159 | void *delta; | |
160 | struct delta_info *next; | |
161 | }; | |
162 | ||
163 | static struct delta_info *delta_list; | |
164 | ||
834bc47b | 165 | static void add_delta_to_list(unsigned nr, const struct object_id *base_oid, |
d7dd0223 | 166 | off_t base_offset, |
209c554a | 167 | void *delta, unsigned long size) |
bad50dc8 | 168 | { |
67e5a5ec | 169 | struct delta_info *info = xmalloc(sizeof(*info)); |
bad50dc8 | 170 | |
834bc47b | 171 | oidcpy(&info->base_oid, base_oid); |
209c554a | 172 | info->base_offset = base_offset; |
67e5a5ec LT |
173 | info->size = size; |
174 | info->delta = delta; | |
209c554a | 175 | info->nr = nr; |
67e5a5ec LT |
176 | info->next = delta_list; |
177 | delta_list = info; | |
bad50dc8 LT |
178 | } |
179 | ||
209c554a | 180 | struct obj_info { |
d7dd0223 | 181 | off_t offset; |
834bc47b | 182 | struct object_id oid; |
b41860bf | 183 | struct object *obj; |
209c554a NP |
184 | }; |
185 | ||
95308d64 | 186 | /* Remember to update object flag allocation in object.h */ |
b41860bf MK |
187 | #define FLAG_OPEN (1u<<20) |
188 | #define FLAG_WRITTEN (1u<<21) | |
189 | ||
209c554a | 190 | static struct obj_info *obj_list; |
2af202be | 191 | static unsigned nr_objects; |
b41860bf | 192 | |
f2898cfa JH |
193 | /* |
194 | * Called only from check_object() after it verified this object | |
195 | * is Ok. | |
196 | */ | |
90a398bb | 197 | static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf) |
b41860bf | 198 | { |
834bc47b | 199 | struct object_id oid; |
90a398bb | 200 | |
a09c985e | 201 | if (write_object_file(obj_buf->buffer, obj_buf->size, |
c80d226a | 202 | obj->type, &oid) < 0) |
f2fd0760 | 203 | die("failed to write object %s", oid_to_hex(&obj->oid)); |
b41860bf MK |
204 | obj->flags |= FLAG_WRITTEN; |
205 | } | |
206 | ||
f2898cfa JH |
207 | /* |
208 | * At the very end of the processing, write_rest() scans the objects | |
209 | * that have reachability requirements and calls this function. | |
210 | * Verify its reachability and validity recursively and write it out. | |
211 | */ | |
a1aad716 ÆAB |
212 | static int check_object(struct object *obj, enum object_type type, |
213 | void *data, struct fsck_options *options) | |
b41860bf | 214 | { |
90a398bb JS |
215 | struct obj_buffer *obj_buf; |
216 | ||
b41860bf | 217 | if (!obj) |
9a217391 | 218 | return 1; |
b41860bf MK |
219 | |
220 | if (obj->flags & FLAG_WRITTEN) | |
9a217391 | 221 | return 0; |
b41860bf MK |
222 | |
223 | if (type != OBJ_ANY && obj->type != type) | |
224 | die("object type mismatch"); | |
225 | ||
226 | if (!(obj->flags & FLAG_OPEN)) { | |
227 | unsigned long size; | |
0df8e965 | 228 | int type = oid_object_info(the_repository, &obj->oid, &size); |
b41860bf MK |
229 | if (type != obj->type || type <= 0) |
230 | die("object of unexpected type"); | |
231 | obj->flags |= FLAG_WRITTEN; | |
9a217391 | 232 | return 0; |
b41860bf MK |
233 | } |
234 | ||
90a398bb JS |
235 | obj_buf = lookup_object_buffer(obj); |
236 | if (!obj_buf) | |
f2fd0760 | 237 | die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid)); |
22410549 | 238 | if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options)) |
db5a58c1 | 239 | die("fsck error in packed object"); |
22410549 JS |
240 | fsck_options.walk = check_object; |
241 | if (fsck_walk(obj, NULL, &fsck_options)) | |
f2fd0760 | 242 | die("Error on reachable objects of %s", oid_to_hex(&obj->oid)); |
90a398bb | 243 | write_cached_object(obj, obj_buf); |
9a217391 | 244 | return 0; |
b41860bf MK |
245 | } |
246 | ||
247 | static void write_rest(void) | |
248 | { | |
249 | unsigned i; | |
9a217391 JH |
250 | for (i = 0; i < nr_objects; i++) { |
251 | if (obj_list[i].obj) | |
22410549 | 252 | check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL); |
9a217391 | 253 | } |
b41860bf | 254 | } |
67e5a5ec | 255 | |
21666f1a NP |
256 | static void added_object(unsigned nr, enum object_type type, |
257 | void *data, unsigned long size); | |
209c554a | 258 | |
f2898cfa JH |
259 | /* |
260 | * Write out nr-th object from the list, now we know the contents | |
261 | * of it. Under --strict, this buffers structured objects in-core, | |
262 | * to be checked at the end. | |
263 | */ | |
21666f1a NP |
264 | static void write_object(unsigned nr, enum object_type type, |
265 | void *buf, unsigned long size) | |
cca7081a | 266 | { |
b41860bf | 267 | if (!strict) { |
c80d226a | 268 | if (write_object_file(buf, size, type, |
a09c985e | 269 | &obj_list[nr].oid) < 0) |
b41860bf | 270 | die("failed to write object"); |
f2898cfa | 271 | added_object(nr, type, buf, size); |
b41860bf | 272 | free(buf); |
f2898cfa | 273 | obj_list[nr].obj = NULL; |
b41860bf MK |
274 | } else if (type == OBJ_BLOB) { |
275 | struct blob *blob; | |
c80d226a | 276 | if (write_object_file(buf, size, type, |
a09c985e | 277 | &obj_list[nr].oid) < 0) |
b41860bf | 278 | die("failed to write object"); |
f2898cfa | 279 | added_object(nr, type, buf, size); |
b41860bf MK |
280 | free(buf); |
281 | ||
da14a7ff | 282 | blob = lookup_blob(the_repository, &obj_list[nr].oid); |
b41860bf MK |
283 | if (blob) |
284 | blob->object.flags |= FLAG_WRITTEN; | |
285 | else | |
286 | die("invalid blob object"); | |
f2898cfa | 287 | obj_list[nr].obj = NULL; |
b41860bf MK |
288 | } else { |
289 | struct object *obj; | |
290 | int eaten; | |
44439c1c | 291 | hash_object_file(the_hash_algo, buf, size, type, |
2dcde20e | 292 | &obj_list[nr].oid); |
f2898cfa | 293 | added_object(nr, type, buf, size); |
1ec5bfd2 SB |
294 | obj = parse_object_buffer(the_repository, &obj_list[nr].oid, |
295 | type, size, buf, | |
c251c83d | 296 | &eaten); |
b41860bf | 297 | if (!obj) |
debca9d2 | 298 | die("invalid %s", type_name(type)); |
b41860bf MK |
299 | add_object_buffer(obj, buf, size); |
300 | obj->flags |= FLAG_OPEN; | |
301 | obj_list[nr].obj = obj; | |
302 | } | |
cca7081a LT |
303 | } |
304 | ||
21666f1a | 305 | static void resolve_delta(unsigned nr, enum object_type type, |
f986f2c8 JH |
306 | void *base, unsigned long base_size, |
307 | void *delta, unsigned long delta_size) | |
bad50dc8 | 308 | { |
67e5a5ec LT |
309 | void *result; |
310 | unsigned long result_size; | |
bad50dc8 | 311 | |
67e5a5ec LT |
312 | result = patch_delta(base, base_size, |
313 | delta, delta_size, | |
314 | &result_size); | |
315 | if (!result) | |
316 | die("failed to apply delta"); | |
317 | free(delta); | |
21666f1a | 318 | write_object(nr, type, result, result_size); |
bad50dc8 LT |
319 | } |
320 | ||
f2898cfa JH |
321 | /* |
322 | * We now know the contents of an object (which is nr-th in the pack); | |
323 | * resolve all the deltified objects that are based on it. | |
324 | */ | |
21666f1a NP |
325 | static void added_object(unsigned nr, enum object_type type, |
326 | void *data, unsigned long size) | |
74536958 | 327 | { |
67e5a5ec LT |
328 | struct delta_info **p = &delta_list; |
329 | struct delta_info *info; | |
330 | ||
331 | while ((info = *p) != NULL) { | |
4a7e27e9 | 332 | if (oideq(&info->base_oid, &obj_list[nr].oid) || |
209c554a | 333 | info->base_offset == obj_list[nr].offset) { |
67e5a5ec LT |
334 | *p = info->next; |
335 | p = &delta_list; | |
209c554a NP |
336 | resolve_delta(info->nr, type, data, size, |
337 | info->delta, info->size); | |
67e5a5ec LT |
338 | free(info); |
339 | continue; | |
340 | } | |
341 | p = &info->next; | |
342 | } | |
343 | } | |
344 | ||
21666f1a | 345 | static void unpack_non_delta_entry(enum object_type type, unsigned long size, |
209c554a | 346 | unsigned nr) |
67e5a5ec LT |
347 | { |
348 | void *buf = get_data(size); | |
8ee378a0 | 349 | |
a1bf5ca2 | 350 | if (buf) |
21666f1a | 351 | write_object(nr, type, buf, size); |
8ee378a0 JH |
352 | } |
353 | ||
aaf81223 HX |
354 | struct input_zstream_data { |
355 | git_zstream *zstream; | |
356 | unsigned char buf[8192]; | |
357 | int status; | |
358 | }; | |
359 | ||
360 | static const void *feed_input_zstream(struct input_stream *in_stream, | |
361 | unsigned long *readlen) | |
362 | { | |
363 | struct input_zstream_data *data = in_stream->data; | |
364 | git_zstream *zstream = data->zstream; | |
365 | void *in = fill(1); | |
366 | ||
367 | if (in_stream->is_finished) { | |
368 | *readlen = 0; | |
369 | return NULL; | |
370 | } | |
371 | ||
372 | zstream->next_out = data->buf; | |
373 | zstream->avail_out = sizeof(data->buf); | |
374 | zstream->next_in = in; | |
375 | zstream->avail_in = len; | |
376 | ||
377 | data->status = git_inflate(zstream, 0); | |
378 | ||
379 | in_stream->is_finished = data->status != Z_OK; | |
380 | use(len - zstream->avail_in); | |
381 | *readlen = sizeof(data->buf) - zstream->avail_out; | |
382 | ||
383 | return data->buf; | |
384 | } | |
385 | ||
386 | static void stream_blob(unsigned long size, unsigned nr) | |
387 | { | |
388 | git_zstream zstream = { 0 }; | |
389 | struct input_zstream_data data = { 0 }; | |
390 | struct input_stream in_stream = { | |
391 | .read = feed_input_zstream, | |
392 | .data = &data, | |
393 | }; | |
394 | struct obj_info *info = &obj_list[nr]; | |
395 | ||
396 | data.zstream = &zstream; | |
397 | git_inflate_init(&zstream); | |
398 | ||
399 | if (stream_loose_object(&in_stream, size, &info->oid)) | |
400 | die(_("failed to write object in stream")); | |
401 | ||
402 | if (data.status != Z_STREAM_END) | |
403 | die(_("inflate returned (%d)"), data.status); | |
404 | git_inflate_end(&zstream); | |
405 | ||
406 | if (strict) { | |
407 | struct blob *blob = lookup_blob(the_repository, &info->oid); | |
408 | ||
409 | if (!blob) | |
410 | die(_("invalid blob object from stream")); | |
411 | blob->object.flags |= FLAG_WRITTEN; | |
412 | } | |
413 | info->obj = NULL; | |
414 | } | |
415 | ||
834bc47b | 416 | static int resolve_against_held(unsigned nr, const struct object_id *base, |
f2898cfa JH |
417 | void *delta_data, unsigned long delta_size) |
418 | { | |
419 | struct object *obj; | |
420 | struct obj_buffer *obj_buffer; | |
d0229abd | 421 | obj = lookup_object(the_repository, base); |
f2898cfa JH |
422 | if (!obj) |
423 | return 0; | |
424 | obj_buffer = lookup_object_buffer(obj); | |
425 | if (!obj_buffer) | |
426 | return 0; | |
427 | resolve_delta(nr, obj->type, obj_buffer->buffer, | |
428 | obj_buffer->size, delta_data, delta_size); | |
429 | return 1; | |
430 | } | |
431 | ||
21666f1a | 432 | static void unpack_delta_entry(enum object_type type, unsigned long delta_size, |
209c554a | 433 | unsigned nr) |
8ee378a0 | 434 | { |
67e5a5ec LT |
435 | void *delta_data, *base; |
436 | unsigned long base_size; | |
834bc47b | 437 | struct object_id base_oid; |
8ee378a0 | 438 | |
21666f1a | 439 | if (type == OBJ_REF_DELTA) { |
92e2cab9 | 440 | oidread(&base_oid, fill(the_hash_algo->rawsz)); |
3206b6bd | 441 | use(the_hash_algo->rawsz); |
209c554a | 442 | delta_data = get_data(delta_size); |
a1bf5ca2 | 443 | if (!delta_data) |
209c554a | 444 | return; |
834bc47b | 445 | if (has_object_file(&base_oid)) |
f2898cfa | 446 | ; /* Ok we have this one */ |
834bc47b | 447 | else if (resolve_against_held(nr, &base_oid, |
f2898cfa JH |
448 | delta_data, delta_size)) |
449 | return; /* we are done */ | |
450 | else { | |
451 | /* cannot resolve yet --- queue it */ | |
834bc47b | 452 | oidclr(&obj_list[nr].oid); |
453 | add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size); | |
209c554a NP |
454 | return; |
455 | } | |
456 | } else { | |
457 | unsigned base_found = 0; | |
458 | unsigned char *pack, c; | |
d7dd0223 | 459 | off_t base_offset; |
209c554a | 460 | unsigned lo, mid, hi; |
c4fb06c0 | 461 | |
209c554a NP |
462 | pack = fill(1); |
463 | c = *pack; | |
464 | use(1); | |
465 | base_offset = c & 127; | |
466 | while (c & 128) { | |
467 | base_offset += 1; | |
8723f216 | 468 | if (!base_offset || MSB(base_offset, 7)) |
209c554a NP |
469 | die("offset value overflow for delta base object"); |
470 | pack = fill(1); | |
471 | c = *pack; | |
472 | use(1); | |
473 | base_offset = (base_offset << 7) + (c & 127); | |
474 | } | |
475 | base_offset = obj_list[nr].offset - base_offset; | |
d8f32556 NP |
476 | if (base_offset <= 0 || base_offset >= obj_list[nr].offset) |
477 | die("offset value out of bound for delta base object"); | |
8ee378a0 | 478 | |
209c554a | 479 | delta_data = get_data(delta_size); |
a1bf5ca2 | 480 | if (!delta_data) |
209c554a | 481 | return; |
209c554a NP |
482 | lo = 0; |
483 | hi = nr; | |
484 | while (lo < hi) { | |
19716b21 | 485 | mid = lo + (hi - lo) / 2; |
209c554a NP |
486 | if (base_offset < obj_list[mid].offset) { |
487 | hi = mid; | |
488 | } else if (base_offset > obj_list[mid].offset) { | |
489 | lo = mid + 1; | |
490 | } else { | |
834bc47b | 491 | oidcpy(&base_oid, &obj_list[mid].oid); |
492 | base_found = !is_null_oid(&base_oid); | |
209c554a NP |
493 | break; |
494 | } | |
495 | } | |
496 | if (!base_found) { | |
f2898cfa JH |
497 | /* |
498 | * The delta base object is itself a delta that | |
499 | * has not been resolved yet. | |
500 | */ | |
834bc47b | 501 | oidclr(&obj_list[nr].oid); |
14228447 | 502 | add_delta_to_list(nr, null_oid(), base_offset, |
503 | delta_data, delta_size); | |
209c554a | 504 | return; |
2add1e6d MK |
505 | } |
506 | } | |
507 | ||
834bc47b | 508 | if (resolve_against_held(nr, &base_oid, delta_data, delta_size)) |
f2898cfa | 509 | return; |
209c554a | 510 | |
b4f5aca4 | 511 | base = read_object_file(&base_oid, &type, &base_size); |
f986f2c8 JH |
512 | if (!base) { |
513 | error("failed to read delta-pack base object %s", | |
834bc47b | 514 | oid_to_hex(&base_oid)); |
3b67d291 | 515 | if (!recover) |
f986f2c8 JH |
516 | exit(1); |
517 | has_errors = 1; | |
518 | return; | |
519 | } | |
209c554a | 520 | resolve_delta(nr, type, base, base_size, delta_data, delta_size); |
ee639140 | 521 | free(base); |
8ee378a0 JH |
522 | } |
523 | ||
96a02f8f | 524 | static void unpack_one(unsigned nr) |
8ee378a0 | 525 | { |
01247d87 | 526 | unsigned shift; |
48fb7deb LT |
527 | unsigned char *pack; |
528 | unsigned long size, c; | |
67e5a5ec | 529 | enum object_type type; |
74536958 | 530 | |
209c554a NP |
531 | obj_list[nr].offset = consumed_bytes; |
532 | ||
67e5a5ec LT |
533 | pack = fill(1); |
534 | c = *pack; | |
535 | use(1); | |
a733cb60 LT |
536 | type = (c >> 4) & 7; |
537 | size = (c & 15); | |
01247d87 | 538 | shift = 4; |
a733cb60 | 539 | while (c & 0x80) { |
67e5a5ec | 540 | pack = fill(1); |
209c554a | 541 | c = *pack; |
67e5a5ec | 542 | use(1); |
01247d87 LT |
543 | size += (c & 0x7f) << shift; |
544 | shift += 7; | |
a733cb60 | 545 | } |
96a02f8f | 546 | |
a733cb60 | 547 | switch (type) { |
aaf81223 HX |
548 | case OBJ_BLOB: |
549 | if (!dry_run && size > big_file_threshold) { | |
550 | stream_blob(size, nr); | |
551 | return; | |
552 | } | |
553 | /* fallthrough */ | |
a733cb60 LT |
554 | case OBJ_COMMIT: |
555 | case OBJ_TREE: | |
a733cb60 | 556 | case OBJ_TAG: |
209c554a | 557 | unpack_non_delta_entry(type, size, nr); |
a733cb60 | 558 | return; |
eb32d236 | 559 | case OBJ_REF_DELTA: |
209c554a NP |
560 | case OBJ_OFS_DELTA: |
561 | unpack_delta_entry(type, size, nr); | |
a733cb60 | 562 | return; |
67e5a5ec | 563 | default: |
f986f2c8 JH |
564 | error("bad object type %d", type); |
565 | has_errors = 1; | |
3b67d291 | 566 | if (recover) |
f986f2c8 JH |
567 | return; |
568 | exit(1); | |
74536958 LT |
569 | } |
570 | } | |
571 | ||
74536958 LT |
572 | static void unpack_all(void) |
573 | { | |
67e5a5ec LT |
574 | int i; |
575 | struct pack_header *hdr = fill(sizeof(struct pack_header)); | |
b41860bf MK |
576 | |
577 | nr_objects = ntohl(hdr->hdr_entries); | |
67e5a5ec LT |
578 | |
579 | if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE) | |
580 | die("bad pack file"); | |
d60fc1c8 | 581 | if (!pack_version_ok(hdr->hdr_version)) |
6e1c2344 RJ |
582 | die("unknown pack file version %"PRIu32, |
583 | ntohl(hdr->hdr_version)); | |
96a02f8f | 584 | use(sizeof(struct pack_header)); |
67e5a5ec | 585 | |
13aaf148 | 586 | if (!quiet) |
754dbc43 | 587 | progress = start_progress(_("Unpacking objects"), nr_objects); |
ca56dadb | 588 | CALLOC_ARRAY(obj_list, nr_objects); |
425d290c | 589 | begin_odb_transaction(); |
96a02f8f NP |
590 | for (i = 0; i < nr_objects; i++) { |
591 | unpack_one(i); | |
4d4fcc54 | 592 | display_progress(progress, i + 1); |
96a02f8f | 593 | } |
425d290c | 594 | end_odb_transaction(); |
4d4fcc54 | 595 | stop_progress(&progress); |
96a02f8f | 596 | |
67e5a5ec LT |
597 | if (delta_list) |
598 | die("unresolved deltas left after unpacking"); | |
74536958 LT |
599 | } |
600 | ||
64413630 | 601 | int cmd_unpack_objects(int argc, const char **argv, const char *prefix) |
bad50dc8 LT |
602 | { |
603 | int i; | |
834bc47b | 604 | struct object_id oid; |
bad50dc8 | 605 | |
6ebd1caf | 606 | read_replace_refs = 0; |
dae556bd | 607 | |
ef90d6d4 | 608 | git_config(git_default_config, NULL); |
53228a5f | 609 | |
476e8011 JH |
610 | quiet = !isatty(2); |
611 | ||
bad50dc8 LT |
612 | for (i = 1 ; i < argc; i++) { |
613 | const char *arg = argv[i]; | |
614 | ||
615 | if (*arg == '-') { | |
74536958 LT |
616 | if (!strcmp(arg, "-n")) { |
617 | dry_run = 1; | |
618 | continue; | |
619 | } | |
d36f7b80 LT |
620 | if (!strcmp(arg, "-q")) { |
621 | quiet = 1; | |
622 | continue; | |
623 | } | |
f986f2c8 | 624 | if (!strcmp(arg, "-r")) { |
3b67d291 | 625 | recover = 1; |
f986f2c8 JH |
626 | continue; |
627 | } | |
b41860bf MK |
628 | if (!strcmp(arg, "--strict")) { |
629 | strict = 1; | |
630 | continue; | |
631 | } | |
5d477a33 JS |
632 | if (skip_prefix(arg, "--strict=", &arg)) { |
633 | strict = 1; | |
634 | fsck_set_msg_types(&fsck_options, arg); | |
635 | continue; | |
636 | } | |
59556548 | 637 | if (starts_with(arg, "--pack_header=")) { |
bed006fb NP |
638 | struct pack_header *hdr; |
639 | char *c; | |
640 | ||
641 | hdr = (struct pack_header *)buffer; | |
642 | hdr->hdr_signature = htonl(PACK_SIGNATURE); | |
643 | hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); | |
644 | if (*c != ',') | |
645 | die("bad %s", arg); | |
646 | hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); | |
647 | if (*c) | |
648 | die("bad %s", arg); | |
649 | len = sizeof(*hdr); | |
650 | continue; | |
651 | } | |
5ad21867 CC |
652 | if (skip_prefix(arg, "--max-input-size=", &arg)) { |
653 | max_input_size = strtoumax(arg, NULL, 10); | |
654 | continue; | |
655 | } | |
bad50dc8 LT |
656 | usage(unpack_usage); |
657 | } | |
67e5a5ec LT |
658 | |
659 | /* We don't take any non-flag arguments now.. Maybe some day */ | |
bad50dc8 | 660 | usage(unpack_usage); |
67e5a5ec | 661 | } |
3206b6bd | 662 | the_hash_algo->init_fn(&ctx); |
74536958 | 663 | unpack_all(); |
3206b6bd | 664 | the_hash_algo->update_fn(&ctx, buffer, offset); |
5951bf46 | 665 | the_hash_algo->final_oid_fn(&oid, &ctx); |
6e328d6c | 666 | if (strict) { |
b41860bf | 667 | write_rest(); |
6e328d6c JK |
668 | if (fsck_finish(&fsck_options)) |
669 | die(_("fsck error in pack objects")); | |
670 | } | |
67947c34 | 671 | if (!hasheq(fill(the_hash_algo->rawsz), oid.hash)) |
67e5a5ec | 672 | die("final sha1 did not match"); |
3206b6bd | 673 | use(the_hash_algo->rawsz); |
67e5a5ec LT |
674 | |
675 | /* Write the last part of the buffer to stdout */ | |
676 | while (len) { | |
1c15afb9 JH |
677 | int ret = xwrite(1, buffer + offset, len); |
678 | if (ret <= 0) | |
67e5a5ec | 679 | break; |
67e5a5ec LT |
680 | len -= ret; |
681 | offset += ret; | |
682 | } | |
683 | ||
684 | /* All done */ | |
f986f2c8 | 685 | return has_errors; |
bad50dc8 | 686 | } |