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