"git diff <tree>{3,}": do not reverse order of arguments
[git] / builtin-unpack-objects.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "object.h"
4 #include "delta.h"
5 #include "pack.h"
6 #include "blob.h"
7 #include "commit.h"
8 #include "tag.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "progress.h"
12 #include "decorate.h"
13 #include "fsck.h"
14
15 static int dry_run, quiet, recover, has_errors, strict;
16 static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict] < pack-file";
17
18 /* We always read in 4kB chunks. */
19 static unsigned char buffer[4096];
20 static unsigned int offset, len;
21 static off_t consumed_bytes;
22 static SHA_CTX ctx;
23
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  */
29 struct obj_buffer {
30         char *buffer;
31         unsigned long size;
32 };
33
34 static struct decoration obj_decorate;
35
36 static struct obj_buffer *lookup_object_buffer(struct object *base)
37 {
38         return lookup_decoration(&obj_decorate, base);
39 }
40
41 static 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
51 /*
52  * Make sure at least "min" bytes are available in the buffer, and
53  * return the pointer to the buffer.
54  */
55 static void *fill(int min)
56 {
57         if (min <= len)
58                 return buffer + offset;
59         if (min > sizeof(buffer))
60                 die("cannot fill %d bytes", min);
61         if (offset) {
62                 SHA1_Update(&ctx, buffer, offset);
63                 memmove(buffer, buffer + offset, len);
64                 offset = 0;
65         }
66         do {
67                 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
68                 if (ret <= 0) {
69                         if (!ret)
70                                 die("early EOF");
71                         die("read error on input: %s", strerror(errno));
72                 }
73                 len += ret;
74         } while (len < min);
75         return buffer;
76 }
77
78 static void use(int bytes)
79 {
80         if (bytes > len)
81                 die("used more bytes than were available");
82         len -= bytes;
83         offset += bytes;
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");
88         consumed_bytes += bytes;
89 }
90
91 static void *get_data(unsigned long size)
92 {
93         z_stream stream;
94         void *buf = xmalloc(size);
95
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;
109                 if (ret != Z_OK) {
110                         error("inflate returned %d\n", ret);
111                         free(buf);
112                         buf = NULL;
113                         if (!recover)
114                                 exit(1);
115                         has_errors = 1;
116                         break;
117                 }
118                 stream.next_in = fill(1);
119                 stream.avail_in = len;
120         }
121         inflateEnd(&stream);
122         return buf;
123 }
124
125 struct delta_info {
126         unsigned char base_sha1[20];
127         unsigned nr;
128         off_t base_offset;
129         unsigned long size;
130         void *delta;
131         struct delta_info *next;
132 };
133
134 static struct delta_info *delta_list;
135
136 static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
137                               off_t base_offset,
138                               void *delta, unsigned long size)
139 {
140         struct delta_info *info = xmalloc(sizeof(*info));
141
142         hashcpy(info->base_sha1, base_sha1);
143         info->base_offset = base_offset;
144         info->size = size;
145         info->delta = delta;
146         info->nr = nr;
147         info->next = delta_list;
148         delta_list = info;
149 }
150
151 struct obj_info {
152         off_t offset;
153         unsigned char sha1[20];
154         struct object *obj;
155 };
156
157 #define FLAG_OPEN (1u<<20)
158 #define FLAG_WRITTEN (1u<<21)
159
160 static struct obj_info *obj_list;
161 unsigned nr_objects;
162
163 /*
164  * Called only from check_object() after it verified this object
165  * is Ok.
166  */
167 static 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
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  */
181 static 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
209 static 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 }
215
216 static void added_object(unsigned nr, enum object_type type,
217                          void *data, unsigned long size);
218
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  */
224 static void write_object(unsigned nr, enum object_type type,
225                          void *buf, unsigned long size)
226 {
227         if (!strict) {
228                 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
229                         die("failed to write object");
230                 added_object(nr, type, buf, size);
231                 free(buf);
232                 obj_list[nr].obj = NULL;
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");
237                 added_object(nr, type, buf, size);
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");
245                 obj_list[nr].obj = NULL;
246         } else {
247                 struct object *obj;
248                 int eaten;
249                 hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1);
250                 added_object(nr, type, buf, size);
251                 obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten);
252                 if (!obj)
253                         die("invalid %s", typename(type));
254                 add_object_buffer(obj, buf, size);
255                 obj->flags |= FLAG_OPEN;
256                 obj_list[nr].obj = obj;
257         }
258 }
259
260 static void resolve_delta(unsigned nr, enum object_type type,
261                           void *base, unsigned long base_size,
262                           void *delta, unsigned long delta_size)
263 {
264         void *result;
265         unsigned long result_size;
266
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);
273         write_object(nr, type, result, result_size);
274 }
275
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  */
280 static void added_object(unsigned nr, enum object_type type,
281                          void *data, unsigned long size)
282 {
283         struct delta_info **p = &delta_list;
284         struct delta_info *info;
285
286         while ((info = *p) != NULL) {
287                 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
288                     info->base_offset == obj_list[nr].offset) {
289                         *p = info->next;
290                         p = &delta_list;
291                         resolve_delta(info->nr, type, data, size,
292                                       info->delta, info->size);
293                         free(info);
294                         continue;
295                 }
296                 p = &info->next;
297         }
298 }
299
300 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
301                                    unsigned nr)
302 {
303         void *buf = get_data(size);
304
305         if (!dry_run && buf)
306                 write_object(nr, type, buf, size);
307         else
308                 free(buf);
309 }
310
311 static 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
327 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
328                                unsigned nr)
329 {
330         void *delta_data, *base;
331         unsigned long base_size;
332         unsigned char base_sha1[20];
333
334         if (type == OBJ_REF_DELTA) {
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                 }
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 */
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;
356                 off_t base_offset;
357                 unsigned lo, mid, hi;
358
359                 pack = fill(1);
360                 c = *pack;
361                 use(1);
362                 base_offset = c & 127;
363                 while (c & 128) {
364                         base_offset += 1;
365                         if (!base_offset || MSB(base_offset, 7))
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;
373
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) {
394                         /*
395                          * The delta base object is itself a delta that
396                          * has not been resolved yet.
397                          */
398                         hashcpy(obj_list[nr].sha1, null_sha1);
399                         add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
400                         return;
401                 }
402         }
403
404         if (resolve_against_held(nr, base_sha1, delta_data, delta_size))
405                 return;
406
407         base = read_sha1_file(base_sha1, &type, &base_size);
408         if (!base) {
409                 error("failed to read delta-pack base object %s",
410                       sha1_to_hex(base_sha1));
411                 if (!recover)
412                         exit(1);
413                 has_errors = 1;
414                 return;
415         }
416         resolve_delta(nr, type, base, base_size, delta_data, delta_size);
417         free(base);
418 }
419
420 static void unpack_one(unsigned nr)
421 {
422         unsigned shift;
423         unsigned char *pack, c;
424         unsigned long size;
425         enum object_type type;
426
427         obj_list[nr].offset = consumed_bytes;
428
429         pack = fill(1);
430         c = *pack;
431         use(1);
432         type = (c >> 4) & 7;
433         size = (c & 15);
434         shift = 4;
435         while (c & 0x80) {
436                 pack = fill(1);
437                 c = *pack;
438                 use(1);
439                 size += (c & 0x7f) << shift;
440                 shift += 7;
441         }
442
443         switch (type) {
444         case OBJ_COMMIT:
445         case OBJ_TREE:
446         case OBJ_BLOB:
447         case OBJ_TAG:
448                 unpack_non_delta_entry(type, size, nr);
449                 return;
450         case OBJ_REF_DELTA:
451         case OBJ_OFS_DELTA:
452                 unpack_delta_entry(type, size, nr);
453                 return;
454         default:
455                 error("bad object type %d", type);
456                 has_errors = 1;
457                 if (recover)
458                         return;
459                 exit(1);
460         }
461 }
462
463 static void unpack_all(void)
464 {
465         int i;
466         struct progress *progress = NULL;
467         struct pack_header *hdr = fill(sizeof(struct pack_header));
468
469         nr_objects = ntohl(hdr->hdr_entries);
470
471         if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
472                 die("bad pack file");
473         if (!pack_version_ok(hdr->hdr_version))
474                 die("unknown pack file version %"PRIu32,
475                         ntohl(hdr->hdr_version));
476         use(sizeof(struct pack_header));
477
478         if (!quiet)
479                 progress = start_progress("Unpacking objects", nr_objects);
480         obj_list = xmalloc(nr_objects * sizeof(*obj_list));
481         memset(obj_list, 0, nr_objects * sizeof(*obj_list));
482         for (i = 0; i < nr_objects; i++) {
483                 unpack_one(i);
484                 display_progress(progress, i + 1);
485         }
486         stop_progress(&progress);
487
488         if (delta_list)
489                 die("unresolved deltas left after unpacking");
490 }
491
492 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
493 {
494         int i;
495         unsigned char sha1[20];
496
497         git_config(git_default_config, NULL);
498
499         quiet = !isatty(2);
500
501         for (i = 1 ; i < argc; i++) {
502                 const char *arg = argv[i];
503
504                 if (*arg == '-') {
505                         if (!strcmp(arg, "-n")) {
506                                 dry_run = 1;
507                                 continue;
508                         }
509                         if (!strcmp(arg, "-q")) {
510                                 quiet = 1;
511                                 continue;
512                         }
513                         if (!strcmp(arg, "-r")) {
514                                 recover = 1;
515                                 continue;
516                         }
517                         if (!strcmp(arg, "--strict")) {
518                                 strict = 1;
519                                 continue;
520                         }
521                         if (!prefixcmp(arg, "--pack_header=")) {
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                         }
536                         usage(unpack_usage);
537                 }
538
539                 /* We don't take any non-flag arguments now.. Maybe some day */
540                 usage(unpack_usage);
541         }
542         SHA1_Init(&ctx);
543         unpack_all();
544         SHA1_Update(&ctx, buffer, offset);
545         SHA1_Final(sha1, &ctx);
546         if (strict)
547                 write_rest();
548         if (hashcmp(fill(20), sha1))
549                 die("final sha1 did not match");
550         use(20);
551
552         /* Write the last part of the buffer to stdout */
553         while (len) {
554                 int ret = xwrite(1, buffer + offset, len);
555                 if (ret <= 0)
556                         break;
557                 len -= ret;
558                 offset += ret;
559         }
560
561         /* All done */
562         return has_errors;
563 }