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