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 int offset, len;
17 static off_t consumed_bytes;
21 * Make sure at least "min" bytes are available in the buffer, and
22 * return the pointer to the buffer.
24 static void *fill(int min)
27 return buffer + offset;
28 if (min > sizeof(buffer))
29 die("cannot fill %d bytes", min);
31 SHA1_Update(&ctx, buffer, offset);
32 memmove(buffer, buffer + offset, len);
36 int ret = xread(0, buffer + len, sizeof(buffer) - len);
40 die("read error on input: %s", strerror(errno));
47 static void use(int bytes)
50 die("used more bytes than were available");
54 /* make sure off_t is sufficiently large not to wrap */
55 if (consumed_bytes > consumed_bytes + bytes)
56 die("pack too large for current definition of off_t");
57 consumed_bytes += bytes;
60 static void *get_data(unsigned long size)
63 void *buf = xmalloc(size);
65 memset(&stream, 0, sizeof(stream));
67 stream.next_out = buf;
68 stream.avail_out = size;
69 stream.next_in = fill(1);
70 stream.avail_in = len;
74 int ret = inflate(&stream, 0);
75 use(len - stream.avail_in);
76 if (stream.total_out == size && ret == Z_STREAM_END)
79 error("inflate returned %d\n", ret);
87 stream.next_in = fill(1);
88 stream.avail_in = len;
95 unsigned char base_sha1[20];
100 struct delta_info *next;
103 static struct delta_info *delta_list;
105 static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
107 void *delta, unsigned long size)
109 struct delta_info *info = xmalloc(sizeof(*info));
111 hashcpy(info->base_sha1, base_sha1);
112 info->base_offset = base_offset;
116 info->next = delta_list;
122 unsigned char sha1[20];
125 static struct obj_info *obj_list;
127 static void added_object(unsigned nr, enum object_type type,
128 void *data, unsigned long size);
130 static void write_object(unsigned nr, enum object_type type,
131 void *buf, unsigned long size)
133 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
134 die("failed to write object");
135 added_object(nr, type, buf, size);
138 static void resolve_delta(unsigned nr, enum object_type type,
139 void *base, unsigned long base_size,
140 void *delta, unsigned long delta_size)
143 unsigned long result_size;
145 result = patch_delta(base, base_size,
149 die("failed to apply delta");
151 write_object(nr, type, result, result_size);
155 static void added_object(unsigned nr, enum object_type type,
156 void *data, unsigned long size)
158 struct delta_info **p = &delta_list;
159 struct delta_info *info;
161 while ((info = *p) != NULL) {
162 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
163 info->base_offset == obj_list[nr].offset) {
166 resolve_delta(info->nr, type, data, size,
167 info->delta, info->size);
175 static void unpack_non_delta_entry(enum object_type type, unsigned long size,
178 void *buf = get_data(size);
181 write_object(nr, type, buf, size);
185 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
188 void *delta_data, *base;
189 unsigned long base_size;
190 unsigned char base_sha1[20];
192 if (type == OBJ_REF_DELTA) {
193 hashcpy(base_sha1, fill(20));
195 delta_data = get_data(delta_size);
196 if (dry_run || !delta_data) {
200 if (!has_sha1_file(base_sha1)) {
201 hashcpy(obj_list[nr].sha1, null_sha1);
202 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
206 unsigned base_found = 0;
207 unsigned char *pack, c;
209 unsigned lo, mid, hi;
214 base_offset = c & 127;
217 if (!base_offset || MSB(base_offset, 7))
218 die("offset value overflow for delta base object");
222 base_offset = (base_offset << 7) + (c & 127);
224 base_offset = obj_list[nr].offset - base_offset;
226 delta_data = get_data(delta_size);
227 if (dry_run || !delta_data) {
235 if (base_offset < obj_list[mid].offset) {
237 } else if (base_offset > obj_list[mid].offset) {
240 hashcpy(base_sha1, obj_list[mid].sha1);
241 base_found = !is_null_sha1(base_sha1);
246 /* The delta base object is itself a delta that
247 has not been resolved yet. */
248 hashcpy(obj_list[nr].sha1, null_sha1);
249 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
254 base = read_sha1_file(base_sha1, &type, &base_size);
256 error("failed to read delta-pack base object %s",
257 sha1_to_hex(base_sha1));
263 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
267 static void unpack_one(unsigned nr, unsigned total)
270 unsigned char *pack, c;
272 enum object_type type;
274 obj_list[nr].offset = consumed_bytes;
286 size += (c & 0x7f) << shift;
290 static unsigned long last_sec;
291 static unsigned last_percent;
293 unsigned percentage = ((nr+1) * 100) / total;
295 gettimeofday(&now, NULL);
296 if (percentage != last_percent || now.tv_sec != last_sec) {
297 last_sec = now.tv_sec;
298 last_percent = percentage;
299 fprintf(stderr, "%4u%% (%u/%u) done\r",
300 percentage, (nr+1), total);
308 unpack_non_delta_entry(type, size, nr);
312 unpack_delta_entry(type, size, nr);
315 error("bad object type %d", type);
323 static void unpack_all(void)
326 struct pack_header *hdr = fill(sizeof(struct pack_header));
327 unsigned nr_objects = ntohl(hdr->hdr_entries);
329 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
330 die("bad pack file");
331 if (!pack_version_ok(hdr->hdr_version))
332 die("unknown pack file version %d", ntohl(hdr->hdr_version));
333 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
335 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
336 use(sizeof(struct pack_header));
337 for (i = 0; i < nr_objects; i++)
338 unpack_one(i, nr_objects);
340 die("unresolved deltas left after unpacking");
343 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
346 unsigned char sha1[20];
348 git_config(git_default_config);
352 for (i = 1 ; i < argc; i++) {
353 const char *arg = argv[i];
356 if (!strcmp(arg, "-n")) {
360 if (!strcmp(arg, "-q")) {
364 if (!strcmp(arg, "-r")) {
368 if (!prefixcmp(arg, "--pack_header=")) {
369 struct pack_header *hdr;
372 hdr = (struct pack_header *)buffer;
373 hdr->hdr_signature = htonl(PACK_SIGNATURE);
374 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
377 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
386 /* We don't take any non-flag arguments now.. Maybe some day */
391 SHA1_Update(&ctx, buffer, offset);
392 SHA1_Final(sha1, &ctx);
393 if (hashcmp(fill(20), sha1))
394 die("final sha1 did not match");
397 /* Write the last part of the buffer to stdout */
399 int ret = xwrite(1, buffer + offset, len);
408 fprintf(stderr, "\n");