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