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