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