blob: 557148a693c058f51222cb3d996c309791d43d8b [file] [log] [blame]
Matthias Kestenholz64413632006-08-03 17:24:37 +02001#include "builtin.h"
Linus Torvaldsbad50dc2005-06-25 15:27:14 -07002#include "cache.h"
Linus Torvalds74536952005-06-25 15:59:31 -07003#include "object.h"
Junio C Hamano8ee378a2005-06-26 04:29:18 -07004#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -07005#include "pack.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02006#include "blob.h"
7#include "commit.h"
8#include "tag.h"
9#include "tree.h"
Martin Koeglerb41860b2008-02-25 22:46:11 +010010#include "tree-walk.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040011#include "progress.h"
Martin Koegler2add1e62008-02-25 22:46:10 +010012#include "decorate.h"
Martin Koeglerb41860b2008-02-25 22:46:11 +010013#include "fsck.h"
Linus Torvaldsbad50dc2005-06-25 15:27:14 -070014
Martin Koeglerb41860b2008-02-25 22:46:11 +010015static int dry_run, quiet, recover, has_errors, strict;
Heikki Orsilaf18d2442008-09-13 20:18:36 +030016static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict] < pack-file";
Linus Torvaldsbad50dc2005-06-25 15:27:14 -070017
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070018/* We always read in 4kB chunks. */
19static unsigned char buffer[4096];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040020static unsigned int offset, len;
21static off_t consumed_bytes;
Nicolas Pitre9126f002008-10-01 14:05:20 -040022static git_SHA_CTX ctx;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070023
Junio C Hamanof2898cf2008-03-04 23:46:51 -080024/*
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 */
Martin Koegler2add1e62008-02-25 22:46:10 +010029struct 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
Martin Koeglerb41860b2008-02-25 22:46:11 +010041static 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
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070051/*
52 * Make sure at least "min" bytes are available in the buffer, and
53 * return the pointer to the buffer.
54 */
Edgar Toernig79a65692006-10-30 17:44:27 -080055static void *fill(int min)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070056{
57 if (min <= len)
58 return buffer + offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070059 if (min > sizeof(buffer))
60 die("cannot fill %d bytes", min);
61 if (offset) {
Nicolas Pitre9126f002008-10-01 14:05:20 -040062 git_SHA1_Update(&ctx, buffer, offset);
Edgar Toernig79a65692006-10-30 17:44:27 -080063 memmove(buffer, buffer + offset, len);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070064 offset = 0;
65 }
66 do {
Johan Herland8a912bc2007-05-15 14:49:22 +020067 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070068 if (ret <= 0) {
69 if (!ret)
70 die("early EOF");
Thomas Rastd824cbb2009-06-27 17:58:46 +020071 die_errno("read error on input");
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070072 }
73 len += ret;
74 } while (len < min);
75 return buffer;
76}
77
78static void use(int bytes)
79{
80 if (bytes > len)
81 die("used more bytes than were available");
82 len -= bytes;
83 offset += bytes;
Nicolas Pitred7dd0222007-04-09 01:06:30 -040084
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");
Nicolas Pitre209c5542006-09-21 00:07:39 -040088 consumed_bytes += bytes;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070089}
90
91static void *get_data(unsigned long size)
92{
93 z_stream stream;
94 void *buf = xmalloc(size);
95
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070096 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;
Linus Torvalds39c68542009-01-07 19:54:47 -0800102 git_inflate_init(&stream);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700103
104 for (;;) {
Linus Torvalds39c68542009-01-07 19:54:47 -0800105 int ret = git_inflate(&stream, 0);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700106 use(len - stream.avail_in);
107 if (stream.total_out == size && ret == Z_STREAM_END)
108 break;
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700109 if (ret != Z_OK) {
110 error("inflate returned %d\n", ret);
111 free(buf);
112 buf = NULL;
Junio C Hamano3b67d292006-09-13 12:59:20 -0700113 if (!recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700114 exit(1);
115 has_errors = 1;
116 break;
117 }
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700118 stream.next_in = fill(1);
119 stream.avail_in = len;
120 }
Linus Torvalds39c68542009-01-07 19:54:47 -0800121 git_inflate_end(&stream);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700122 return buf;
123}
124
125struct delta_info {
126 unsigned char base_sha1[20];
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400127 unsigned nr;
128 off_t base_offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700129 unsigned long size;
130 void *delta;
131 struct delta_info *next;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700132};
133
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700134static struct delta_info *delta_list;
Linus Torvalds74536952005-06-25 15:59:31 -0700135
Nicolas Pitre209c5542006-09-21 00:07:39 -0400136static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400137 off_t base_offset,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400138 void *delta, unsigned long size)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700139{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700140 struct delta_info *info = xmalloc(sizeof(*info));
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700141
Shawn Pearcee7024962006-08-23 02:49:00 -0400142 hashcpy(info->base_sha1, base_sha1);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400143 info->base_offset = base_offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700144 info->size = size;
145 info->delta = delta;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400146 info->nr = nr;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700147 info->next = delta_list;
148 delta_list = info;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700149}
150
Nicolas Pitre209c5542006-09-21 00:07:39 -0400151struct obj_info {
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400152 off_t offset;
Linus Torvaldscca70812005-06-29 09:38:02 -0700153 unsigned char sha1[20];
Martin Koeglerb41860b2008-02-25 22:46:11 +0100154 struct object *obj;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400155};
156
Martin Koeglerb41860b2008-02-25 22:46:11 +0100157#define FLAG_OPEN (1u<<20)
158#define FLAG_WRITTEN (1u<<21)
159
Nicolas Pitre209c5542006-09-21 00:07:39 -0400160static struct obj_info *obj_list;
Linus Torvalds2af202b2009-06-18 10:28:43 -0700161static unsigned nr_objects;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100162
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800163/*
164 * Called only from check_object() after it verified this object
165 * is Ok.
166 */
Martin Koeglerb41860b2008-02-25 22:46:11 +0100167static 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
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800176/*
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 */
Martin Koeglerb41860b2008-02-25 22:46:11 +0100181static 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");
Linus Torvalds2af202b2009-06-18 10:28:43 -0700203 if (!fsck_walk(obj, check_object, NULL))
Martin Koeglerb41860b2008-02-25 22:46:11 +0100204 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++)
Linus Torvalds2af202b2009-06-18 10:28:43 -0700213 check_object(obj_list[i].obj, OBJ_ANY, NULL);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100214}
Nicolas Pitre209c5542006-09-21 00:07:39 -0400215
Nicolas Pitre21666f12007-02-26 14:55:59 -0500216static void added_object(unsigned nr, enum object_type type,
217 void *data, unsigned long size);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400218
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800219/*
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 */
Nicolas Pitre21666f12007-02-26 14:55:59 -0500224static void write_object(unsigned nr, enum object_type type,
225 void *buf, unsigned long size)
Nicolas Pitre209c5542006-09-21 00:07:39 -0400226{
Martin Koeglerb41860b2008-02-25 22:46:11 +0100227 if (!strict) {
228 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
229 die("failed to write object");
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800230 added_object(nr, type, buf, size);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100231 free(buf);
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800232 obj_list[nr].obj = NULL;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100233 } 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");
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800237 added_object(nr, type, buf, size);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100238 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");
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800245 obj_list[nr].obj = NULL;
Martin Koeglerb41860b2008-02-25 22:46:11 +0100246 } else {
247 struct object *obj;
248 int eaten;
249 hash_sha1_file(buf, size, typename(type), obj_list[nr].sha1);
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800250 added_object(nr, type, buf, size);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100251 obj = parse_object_buffer(obj_list[nr].sha1, type, size, buf, &eaten);
252 if (!obj)
253 die("invalid %s", typename(type));
Martin Koeglerb41860b2008-02-25 22:46:11 +0100254 add_object_buffer(obj, buf, size);
255 obj->flags |= FLAG_OPEN;
256 obj_list[nr].obj = obj;
257 }
Linus Torvaldscca70812005-06-29 09:38:02 -0700258}
259
Nicolas Pitre21666f12007-02-26 14:55:59 -0500260static void resolve_delta(unsigned nr, enum object_type type,
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700261 void *base, unsigned long base_size,
262 void *delta, unsigned long delta_size)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700263{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700264 void *result;
265 unsigned long result_size;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700266
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700267 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);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500273 write_object(nr, type, result, result_size);
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700274}
275
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800276/*
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 */
Nicolas Pitre21666f12007-02-26 14:55:59 -0500280static void added_object(unsigned nr, enum object_type type,
281 void *data, unsigned long size)
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700282{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700283 struct delta_info **p = &delta_list;
284 struct delta_info *info;
285
286 while ((info = *p) != NULL) {
Nicolas Pitre209c5542006-09-21 00:07:39 -0400287 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
288 info->base_offset == obj_list[nr].offset) {
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700289 *p = info->next;
290 p = &delta_list;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400291 resolve_delta(info->nr, type, data, size,
292 info->delta, info->size);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700293 free(info);
294 continue;
295 }
296 p = &info->next;
297 }
298}
299
Nicolas Pitre21666f12007-02-26 14:55:59 -0500300static void unpack_non_delta_entry(enum object_type type, unsigned long size,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400301 unsigned nr)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700302{
303 void *buf = get_data(size);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700304
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700305 if (!dry_run && buf)
Nicolas Pitre21666f12007-02-26 14:55:59 -0500306 write_object(nr, type, buf, size);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100307 else
308 free(buf);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700309}
310
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800311static 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
Nicolas Pitre21666f12007-02-26 14:55:59 -0500327static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400328 unsigned nr)
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700329{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700330 void *delta_data, *base;
331 unsigned long base_size;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700332 unsigned char base_sha1[20];
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700333
Nicolas Pitre21666f12007-02-26 14:55:59 -0500334 if (type == OBJ_REF_DELTA) {
Nicolas Pitre209c5542006-09-21 00:07:39 -0400335 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 }
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800342 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 */
Nicolas Pitre209c5542006-09-21 00:07:39 -0400349 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;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400356 off_t base_offset;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400357 unsigned lo, mid, hi;
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700358
Nicolas Pitre209c5542006-09-21 00:07:39 -0400359 pack = fill(1);
360 c = *pack;
361 use(1);
362 base_offset = c & 127;
363 while (c & 128) {
364 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -0400365 if (!base_offset || MSB(base_offset, 7))
Nicolas Pitre209c5542006-09-21 00:07:39 -0400366 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;
Nicolas Pitred8f32552008-10-29 19:02:45 -0400373 if (base_offset <= 0 || base_offset >= obj_list[nr].offset)
374 die("offset value out of bound for delta base object");
Nicolas Pitre209c5542006-09-21 00:07:39 -0400375
376 delta_data = get_data(delta_size);
377 if (dry_run || !delta_data) {
378 free(delta_data);
379 return;
380 }
381 lo = 0;
382 hi = nr;
383 while (lo < hi) {
384 mid = (lo + hi)/2;
385 if (base_offset < obj_list[mid].offset) {
386 hi = mid;
387 } else if (base_offset > obj_list[mid].offset) {
388 lo = mid + 1;
389 } else {
390 hashcpy(base_sha1, obj_list[mid].sha1);
391 base_found = !is_null_sha1(base_sha1);
392 break;
393 }
394 }
395 if (!base_found) {
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800396 /*
397 * The delta base object is itself a delta that
398 * has not been resolved yet.
399 */
Nicolas Pitre209c5542006-09-21 00:07:39 -0400400 hashcpy(obj_list[nr].sha1, null_sha1);
401 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
402 return;
403 }
Linus Torvaldsdddafff2005-06-29 09:53:20 -0700404 }
Linus Torvaldsc4fb06c2005-06-26 08:40:08 -0700405
Junio C Hamanof2898cf2008-03-04 23:46:51 -0800406 if (resolve_against_held(nr, base_sha1, delta_data, delta_size))
407 return;
Martin Koegler2add1e62008-02-25 22:46:10 +0100408
Nicolas Pitre21666f12007-02-26 14:55:59 -0500409 base = read_sha1_file(base_sha1, &type, &base_size);
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700410 if (!base) {
411 error("failed to read delta-pack base object %s",
412 sha1_to_hex(base_sha1));
Junio C Hamano3b67d292006-09-13 12:59:20 -0700413 if (!recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700414 exit(1);
415 has_errors = 1;
416 return;
417 }
Nicolas Pitre209c5542006-09-21 00:07:39 -0400418 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
Sergey Vlasovee639142005-08-03 16:11:00 +0400419 free(base);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700420}
421
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400422static void unpack_one(unsigned nr)
Linus Torvalds74536952005-06-25 15:59:31 -0700423{
Linus Torvalds01247d82005-06-28 22:15:57 -0700424 unsigned shift;
Linus Torvalds48fb7de2009-06-17 17:22:27 -0700425 unsigned char *pack;
426 unsigned long size, c;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700427 enum object_type type;
Linus Torvalds74536952005-06-25 15:59:31 -0700428
Nicolas Pitre209c5542006-09-21 00:07:39 -0400429 obj_list[nr].offset = consumed_bytes;
430
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700431 pack = fill(1);
432 c = *pack;
433 use(1);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700434 type = (c >> 4) & 7;
435 size = (c & 15);
Linus Torvalds01247d82005-06-28 22:15:57 -0700436 shift = 4;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700437 while (c & 0x80) {
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700438 pack = fill(1);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400439 c = *pack;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700440 use(1);
Linus Torvalds01247d82005-06-28 22:15:57 -0700441 size += (c & 0x7f) << shift;
442 shift += 7;
Linus Torvalds74536952005-06-25 15:59:31 -0700443 }
Linus Torvaldsd36f7b82005-07-09 10:43:02 -0700444
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700445 switch (type) {
446 case OBJ_COMMIT:
447 case OBJ_TREE:
448 case OBJ_BLOB:
449 case OBJ_TAG:
Nicolas Pitre209c5542006-09-21 00:07:39 -0400450 unpack_non_delta_entry(type, size, nr);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700451 return;
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400452 case OBJ_REF_DELTA:
Nicolas Pitre209c5542006-09-21 00:07:39 -0400453 case OBJ_OFS_DELTA:
454 unpack_delta_entry(type, size, nr);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700455 return;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700456 default:
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700457 error("bad object type %d", type);
458 has_errors = 1;
Junio C Hamano3b67d292006-09-13 12:59:20 -0700459 if (recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700460 return;
461 exit(1);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700462 }
Linus Torvalds74536952005-06-25 15:59:31 -0700463}
464
Linus Torvalds74536952005-06-25 15:59:31 -0700465static void unpack_all(void)
466{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700467 int i;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400468 struct progress *progress = NULL;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700469 struct pack_header *hdr = fill(sizeof(struct pack_header));
Martin Koeglerb41860b2008-02-25 22:46:11 +0100470
471 nr_objects = ntohl(hdr->hdr_entries);
Linus Torvalds74536952005-06-25 15:59:31 -0700472
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700473 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
474 die("bad pack file");
Nicolas Pitred60fc1c2006-02-09 17:50:04 -0500475 if (!pack_version_ok(hdr->hdr_version))
Ramsay Jones6e1c2342008-07-03 16:52:09 +0100476 die("unknown pack file version %"PRIu32,
477 ntohl(hdr->hdr_version));
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700478 use(sizeof(struct pack_header));
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400479
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400480 if (!quiet)
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400481 progress = start_progress("Unpacking objects", nr_objects);
Brandon Casey19d4b412008-10-06 18:39:10 -0500482 obj_list = xcalloc(nr_objects, sizeof(*obj_list));
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400483 for (i = 0; i < nr_objects; i++) {
484 unpack_one(i);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400485 display_progress(progress, i + 1);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400486 }
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400487 stop_progress(&progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400488
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700489 if (delta_list)
490 die("unresolved deltas left after unpacking");
Linus Torvalds74536952005-06-25 15:59:31 -0700491}
492
Matthias Kestenholz64413632006-08-03 17:24:37 +0200493int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700494{
495 int i;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700496 unsigned char sha1[20];
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700497
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100498 git_config(git_default_config, NULL);
Junio C Hamano53228a52005-11-26 00:50:02 -0800499
Junio C Hamano476e8012006-01-06 18:53:16 -0800500 quiet = !isatty(2);
501
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700502 for (i = 1 ; i < argc; i++) {
503 const char *arg = argv[i];
504
505 if (*arg == '-') {
Linus Torvalds74536952005-06-25 15:59:31 -0700506 if (!strcmp(arg, "-n")) {
507 dry_run = 1;
508 continue;
509 }
Linus Torvaldsd36f7b82005-07-09 10:43:02 -0700510 if (!strcmp(arg, "-q")) {
511 quiet = 1;
512 continue;
513 }
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700514 if (!strcmp(arg, "-r")) {
Junio C Hamano3b67d292006-09-13 12:59:20 -0700515 recover = 1;
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700516 continue;
517 }
Martin Koeglerb41860b2008-02-25 22:46:11 +0100518 if (!strcmp(arg, "--strict")) {
519 strict = 1;
520 continue;
521 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800522 if (!prefixcmp(arg, "--pack_header=")) {
Nicolas Pitrebed006f2006-11-01 17:06:20 -0500523 struct pack_header *hdr;
524 char *c;
525
526 hdr = (struct pack_header *)buffer;
527 hdr->hdr_signature = htonl(PACK_SIGNATURE);
528 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
529 if (*c != ',')
530 die("bad %s", arg);
531 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
532 if (*c)
533 die("bad %s", arg);
534 len = sizeof(*hdr);
535 continue;
536 }
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700537 usage(unpack_usage);
538 }
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700539
540 /* We don't take any non-flag arguments now.. Maybe some day */
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700541 usage(unpack_usage);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700542 }
Nicolas Pitre9126f002008-10-01 14:05:20 -0400543 git_SHA1_Init(&ctx);
Linus Torvalds74536952005-06-25 15:59:31 -0700544 unpack_all();
Nicolas Pitre9126f002008-10-01 14:05:20 -0400545 git_SHA1_Update(&ctx, buffer, offset);
546 git_SHA1_Final(sha1, &ctx);
Martin Koeglerb41860b2008-02-25 22:46:11 +0100547 if (strict)
548 write_rest();
David Rientjesa89fccd2006-08-17 11:54:57 -0700549 if (hashcmp(fill(20), sha1))
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700550 die("final sha1 did not match");
551 use(20);
552
553 /* Write the last part of the buffer to stdout */
554 while (len) {
Junio C Hamano1c15afb2005-12-19 16:18:28 -0800555 int ret = xwrite(1, buffer + offset, len);
556 if (ret <= 0)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700557 break;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700558 len -= ret;
559 offset += ret;
560 }
561
562 /* All done */
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700563 return has_errors;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700564}