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, enum object_type type,
123 void *data, unsigned long size);
125 static void write_object(unsigned nr, enum object_type type,
126 void *buf, unsigned long size)
128 if (write_sha1_file(buf, size, typename(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, enum object_type 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, type, result, result_size);
150 static void added_object(unsigned nr, enum object_type type,
151 void *data, unsigned long size)
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 type, unsigned long size,
173 void *buf = get_data(size);
176 write_object(nr, type, buf, size);
180 static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
183 void *delta_data, *base;
184 unsigned long base_size;
185 unsigned char base_sha1[20];
187 if (type == OBJ_REF_DELTA) {
188 hashcpy(base_sha1, fill(20));
190 delta_data = get_data(delta_size);
191 if (dry_run || !delta_data) {
195 if (!has_sha1_file(base_sha1)) {
196 hashcpy(obj_list[nr].sha1, null_sha1);
197 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
201 unsigned base_found = 0;
202 unsigned char *pack, c;
203 unsigned long base_offset;
204 unsigned lo, mid, hi;
209 base_offset = c & 127;
212 if (!base_offset || base_offset & ~(~0UL >> 7))
213 die("offset value overflow for delta base object");
217 base_offset = (base_offset << 7) + (c & 127);
219 base_offset = obj_list[nr].offset - base_offset;
221 delta_data = get_data(delta_size);
222 if (dry_run || !delta_data) {
230 if (base_offset < obj_list[mid].offset) {
232 } else if (base_offset > obj_list[mid].offset) {
235 hashcpy(base_sha1, obj_list[mid].sha1);
236 base_found = !is_null_sha1(base_sha1);
241 /* The delta base object is itself a delta that
242 has not been resolved yet. */
243 hashcpy(obj_list[nr].sha1, null_sha1);
244 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
249 base = read_sha1_file(base_sha1, &type, &base_size);
251 error("failed to read delta-pack base object %s",
252 sha1_to_hex(base_sha1));
258 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
262 static void unpack_one(unsigned nr, unsigned total)
265 unsigned char *pack, c;
267 enum object_type type;
269 obj_list[nr].offset = consumed_bytes;
281 size += (c & 0x7f) << shift;
285 static unsigned long last_sec;
286 static unsigned last_percent;
288 unsigned percentage = ((nr+1) * 100) / total;
290 gettimeofday(&now, NULL);
291 if (percentage != last_percent || now.tv_sec != last_sec) {
292 last_sec = now.tv_sec;
293 last_percent = percentage;
294 fprintf(stderr, "%4u%% (%u/%u) done\r",
295 percentage, (nr+1), total);
303 unpack_non_delta_entry(type, size, nr);
307 unpack_delta_entry(type, size, nr);
310 error("bad object type %d", type);
318 static void unpack_all(void)
321 struct pack_header *hdr = fill(sizeof(struct pack_header));
322 unsigned nr_objects = ntohl(hdr->hdr_entries);
324 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
325 die("bad pack file");
326 if (!pack_version_ok(hdr->hdr_version))
327 die("unknown pack file version %d", ntohl(hdr->hdr_version));
328 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
330 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
331 use(sizeof(struct pack_header));
332 for (i = 0; i < nr_objects; i++)
333 unpack_one(i, nr_objects);
335 die("unresolved deltas left after unpacking");
338 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
341 unsigned char sha1[20];
343 git_config(git_default_config);
347 for (i = 1 ; i < argc; i++) {
348 const char *arg = argv[i];
351 if (!strcmp(arg, "-n")) {
355 if (!strcmp(arg, "-q")) {
359 if (!strcmp(arg, "-r")) {
363 if (!prefixcmp(arg, "--pack_header=")) {
364 struct pack_header *hdr;
367 hdr = (struct pack_header *)buffer;
368 hdr->hdr_signature = htonl(PACK_SIGNATURE);
369 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
372 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
381 /* We don't take any non-flag arguments now.. Maybe some day */
386 SHA1_Update(&ctx, buffer, offset);
387 SHA1_Final(sha1, &ctx);
388 if (hashcmp(fill(20), sha1))
389 die("final sha1 did not match");
392 /* Write the last part of the buffer to stdout */
394 int ret = xwrite(1, buffer + offset, len);
403 fprintf(stderr, "\n");