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