13 static int dry_run, quiet, recover, has_errors;
14 static const char unpack_usage[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
16 /* We always read in 4kB chunks. */
17 static unsigned char buffer[4096];
18 static unsigned int offset, len;
19 static off_t consumed_bytes;
27 static struct decoration obj_decorate;
29 static struct obj_buffer *lookup_object_buffer(struct object *base)
31 return lookup_decoration(&obj_decorate, base);
35 * Make sure at least "min" bytes are available in the buffer, and
36 * return the pointer to the buffer.
38 static void *fill(int min)
41 return buffer + offset;
42 if (min > sizeof(buffer))
43 die("cannot fill %d bytes", min);
45 SHA1_Update(&ctx, buffer, offset);
46 memmove(buffer, buffer + offset, len);
50 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
54 die("read error on input: %s", strerror(errno));
61 static void use(int bytes)
64 die("used more bytes than were available");
68 /* make sure off_t is sufficiently large not to wrap */
69 if (consumed_bytes > consumed_bytes + bytes)
70 die("pack too large for current definition of off_t");
71 consumed_bytes += bytes;
74 static void *get_data(unsigned long size)
77 void *buf = xmalloc(size);
79 memset(&stream, 0, sizeof(stream));
81 stream.next_out = buf;
82 stream.avail_out = size;
83 stream.next_in = fill(1);
84 stream.avail_in = len;
88 int ret = inflate(&stream, 0);
89 use(len - stream.avail_in);
90 if (stream.total_out == size && ret == Z_STREAM_END)
93 error("inflate returned %d\n", ret);
101 stream.next_in = fill(1);
102 stream.avail_in = len;
109 unsigned char base_sha1[20];
114 struct delta_info *next;
117 static struct delta_info *delta_list;
119 static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
121 void *delta, unsigned long size)
123 struct delta_info *info = xmalloc(sizeof(*info));
125 hashcpy(info->base_sha1, base_sha1);
126 info->base_offset = base_offset;
130 info->next = delta_list;
136 unsigned char sha1[20];
139 static struct obj_info *obj_list;
141 static void added_object(unsigned nr, enum object_type type,
142 void *data, unsigned long size);
144 static void write_object(unsigned nr, enum object_type type,
145 void *buf, unsigned long size)
147 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
148 die("failed to write object");
149 added_object(nr, type, buf, size);
152 static void resolve_delta(unsigned nr, enum object_type type,
153 void *base, unsigned long base_size,
154 void *delta, unsigned long delta_size)
157 unsigned long result_size;
159 result = patch_delta(base, base_size,
163 die("failed to apply delta");
165 write_object(nr, type, result, result_size);
169 static void added_object(unsigned nr, enum object_type type,
170 void *data, unsigned long size)
172 struct delta_info **p = &delta_list;
173 struct delta_info *info;
175 while ((info = *p) != NULL) {
176 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
177 info->base_offset == obj_list[nr].offset) {
180 resolve_delta(info->nr, type, data, size,
181 info->delta, info->size);
189 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
192 void *buf = get_data(size);
195 write_object(nr, type, buf, size);
199 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
202 void *delta_data, *base;
203 unsigned long base_size;
204 unsigned char base_sha1[20];
207 if (type == OBJ_REF_DELTA) {
208 hashcpy(base_sha1, fill(20));
210 delta_data = get_data(delta_size);
211 if (dry_run || !delta_data) {
215 if (!has_sha1_file(base_sha1)) {
216 hashcpy(obj_list[nr].sha1, null_sha1);
217 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
221 unsigned base_found = 0;
222 unsigned char *pack, c;
224 unsigned lo, mid, hi;
229 base_offset = c & 127;
232 if (!base_offset || MSB(base_offset, 7))
233 die("offset value overflow for delta base object");
237 base_offset = (base_offset << 7) + (c & 127);
239 base_offset = obj_list[nr].offset - base_offset;
241 delta_data = get_data(delta_size);
242 if (dry_run || !delta_data) {
250 if (base_offset < obj_list[mid].offset) {
252 } else if (base_offset > obj_list[mid].offset) {
255 hashcpy(base_sha1, obj_list[mid].sha1);
256 base_found = !is_null_sha1(base_sha1);
261 /* The delta base object is itself a delta that
262 has not been resolved yet. */
263 hashcpy(obj_list[nr].sha1, null_sha1);
264 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
269 obj = lookup_object(base_sha1);
271 struct obj_buffer *obj_buf = lookup_object_buffer(obj);
273 resolve_delta(nr, obj->type, obj_buf->buffer, obj_buf->size, delta_data, delta_size);
278 base = read_sha1_file(base_sha1, &type, &base_size);
280 error("failed to read delta-pack base object %s",
281 sha1_to_hex(base_sha1));
287 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
291 static void unpack_one(unsigned nr)
294 unsigned char *pack, c;
296 enum object_type type;
298 obj_list[nr].offset = consumed_bytes;
310 size += (c & 0x7f) << shift;
319 unpack_non_delta_entry(type, size, nr);
323 unpack_delta_entry(type, size, nr);
326 error("bad object type %d", type);
334 static void unpack_all(void)
337 struct progress *progress = NULL;
338 struct pack_header *hdr = fill(sizeof(struct pack_header));
339 unsigned nr_objects = ntohl(hdr->hdr_entries);
341 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
342 die("bad pack file");
343 if (!pack_version_ok(hdr->hdr_version))
344 die("unknown pack file version %d", ntohl(hdr->hdr_version));
345 use(sizeof(struct pack_header));
348 progress = start_progress("Unpacking objects", nr_objects);
349 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
350 for (i = 0; i < nr_objects; i++) {
352 display_progress(progress, i + 1);
354 stop_progress(&progress);
357 die("unresolved deltas left after unpacking");
360 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
363 unsigned char sha1[20];
365 git_config(git_default_config);
369 for (i = 1 ; i < argc; i++) {
370 const char *arg = argv[i];
373 if (!strcmp(arg, "-n")) {
377 if (!strcmp(arg, "-q")) {
381 if (!strcmp(arg, "-r")) {
385 if (!prefixcmp(arg, "--pack_header=")) {
386 struct pack_header *hdr;
389 hdr = (struct pack_header *)buffer;
390 hdr->hdr_signature = htonl(PACK_SIGNATURE);
391 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
394 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
403 /* We don't take any non-flag arguments now.. Maybe some day */
408 SHA1_Update(&ctx, buffer, offset);
409 SHA1_Final(sha1, &ctx);
410 if (hashcmp(fill(20), sha1))
411 die("final sha1 did not match");
414 /* Write the last part of the buffer to stdout */
416 int ret = xwrite(1, buffer + offset, len);