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