11 static int dry_run, quiet, recover, has_errors;
12 static const char unpack_usage[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
14 /* We always read in 4kB chunks. */
15 static unsigned char buffer[4096];
16 static unsigned long offset, len, consumed_bytes;
20 * Make sure at least "min" bytes are available in the buffer, and
21 * return the pointer to the buffer.
23 static void *fill(int min)
26 return buffer + offset;
27 if (min > sizeof(buffer))
28 die("cannot fill %d bytes", min);
30 SHA1_Update(&ctx, buffer, offset);
31 memmove(buffer, buffer + offset, len);
35 int ret = xread(0, buffer + len, sizeof(buffer) - len);
39 die("read error on input: %s", strerror(errno));
46 static void use(int bytes)
49 die("used more bytes than were available");
52 consumed_bytes += bytes;
55 static void *get_data(unsigned long size)
58 void *buf = xmalloc(size);
60 memset(&stream, 0, sizeof(stream));
62 stream.next_out = buf;
63 stream.avail_out = size;
64 stream.next_in = fill(1);
65 stream.avail_in = len;
69 int ret = inflate(&stream, 0);
70 use(len - stream.avail_in);
71 if (stream.total_out == size && ret == Z_STREAM_END)
74 error("inflate returned %d\n", ret);
82 stream.next_in = fill(1);
83 stream.avail_in = len;
90 unsigned char base_sha1[20];
91 unsigned long base_offset;
95 struct delta_info *next;
98 static struct delta_info *delta_list;
100 static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
101 unsigned long base_offset,
102 void *delta, unsigned long size)
104 struct delta_info *info = xmalloc(sizeof(*info));
106 hashcpy(info->base_sha1, base_sha1);
107 info->base_offset = base_offset;
111 info->next = delta_list;
116 unsigned long offset;
117 unsigned char sha1[20];
120 static struct obj_info *obj_list;
122 static void added_object(unsigned nr, const char *type, void *data,
125 static void write_object(unsigned nr, void *buf, unsigned long size,
128 if (write_sha1_file(buf, size, type, obj_list[nr].sha1) < 0)
129 die("failed to write object");
130 added_object(nr, type, buf, size);
133 static void resolve_delta(unsigned nr, const char *type,
134 void *base, unsigned long base_size,
135 void *delta, unsigned long delta_size)
138 unsigned long result_size;
140 result = patch_delta(base, base_size,
144 die("failed to apply delta");
146 write_object(nr, result, result_size, type);
150 static void added_object(unsigned nr, const char *type, void *data,
153 struct delta_info **p = &delta_list;
154 struct delta_info *info;
156 while ((info = *p) != NULL) {
157 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
158 info->base_offset == obj_list[nr].offset) {
161 resolve_delta(info->nr, type, data, size,
162 info->delta, info->size);
170 static void unpack_non_delta_entry(enum object_type kind, unsigned long size,
173 void *buf = get_data(size);
177 case OBJ_COMMIT: type = commit_type; break;
178 case OBJ_TREE: type = tree_type; break;
179 case OBJ_BLOB: type = blob_type; break;
180 case OBJ_TAG: type = tag_type; break;
181 default: die("bad type %d", kind);
184 write_object(nr, buf, size, type);
188 static void unpack_delta_entry(enum object_type kind, unsigned long delta_size,
191 void *delta_data, *base;
192 unsigned long base_size;
194 unsigned char base_sha1[20];
196 if (kind == OBJ_REF_DELTA) {
197 hashcpy(base_sha1, fill(20));
199 delta_data = get_data(delta_size);
200 if (dry_run || !delta_data) {
204 if (!has_sha1_file(base_sha1)) {
205 hashcpy(obj_list[nr].sha1, null_sha1);
206 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
210 unsigned base_found = 0;
211 unsigned char *pack, c;
212 unsigned long base_offset;
213 unsigned lo, mid, hi;
218 base_offset = c & 127;
221 if (!base_offset || base_offset & ~(~0UL >> 7))
222 die("offset value overflow for delta base object");
226 base_offset = (base_offset << 7) + (c & 127);
228 base_offset = obj_list[nr].offset - base_offset;
230 delta_data = get_data(delta_size);
231 if (dry_run || !delta_data) {
239 if (base_offset < obj_list[mid].offset) {
241 } else if (base_offset > obj_list[mid].offset) {
244 hashcpy(base_sha1, obj_list[mid].sha1);
245 base_found = !is_null_sha1(base_sha1);
250 /* The delta base object is itself a delta that
251 has not been resolved yet. */
252 hashcpy(obj_list[nr].sha1, null_sha1);
253 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
258 base = read_sha1_file(base_sha1, type, &base_size);
260 error("failed to read delta-pack base object %s",
261 sha1_to_hex(base_sha1));
267 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
271 static void unpack_one(unsigned nr, unsigned total)
274 unsigned char *pack, c;
276 enum object_type type;
278 obj_list[nr].offset = consumed_bytes;
290 size += (c & 0x7f) << shift;
294 static unsigned long last_sec;
295 static unsigned last_percent;
297 unsigned percentage = ((nr+1) * 100) / total;
299 gettimeofday(&now, NULL);
300 if (percentage != last_percent || now.tv_sec != last_sec) {
301 last_sec = now.tv_sec;
302 last_percent = percentage;
303 fprintf(stderr, "%4u%% (%u/%u) done\r",
304 percentage, (nr+1), total);
312 unpack_non_delta_entry(type, size, nr);
316 unpack_delta_entry(type, size, nr);
319 error("bad object type %d", type);
327 static void unpack_all(void)
330 struct pack_header *hdr = fill(sizeof(struct pack_header));
331 unsigned nr_objects = ntohl(hdr->hdr_entries);
333 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
334 die("bad pack file");
335 if (!pack_version_ok(hdr->hdr_version))
336 die("unknown pack file version %d", ntohl(hdr->hdr_version));
337 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
339 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
340 use(sizeof(struct pack_header));
341 for (i = 0; i < nr_objects; i++)
342 unpack_one(i, nr_objects);
344 die("unresolved deltas left after unpacking");
347 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
350 unsigned char sha1[20];
352 git_config(git_default_config);
356 for (i = 1 ; i < argc; i++) {
357 const char *arg = argv[i];
360 if (!strcmp(arg, "-n")) {
364 if (!strcmp(arg, "-q")) {
368 if (!strcmp(arg, "-r")) {
372 if (!strncmp(arg, "--pack_header=", 14)) {
373 struct pack_header *hdr;
376 hdr = (struct pack_header *)buffer;
377 hdr->hdr_signature = htonl(PACK_SIGNATURE);
378 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
381 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
390 /* We don't take any non-flag arguments now.. Maybe some day */
395 SHA1_Update(&ctx, buffer, offset);
396 SHA1_Final(sha1, &ctx);
397 if (hashcmp(fill(20), sha1))
398 die("final sha1 did not match");
401 /* Write the last part of the buffer to stdout */
403 int ret = xwrite(1, buffer + offset, len);
412 fprintf(stderr, "\n");