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