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 long offset, len, consumed_bytes;
22 * Make sure at least "min" bytes are available in the buffer, and
23 * return the pointer to the buffer.
25 static void * fill(int min)
28 return buffer + offset;
29 if (min > sizeof(buffer))
30 die("cannot fill %d bytes", min);
32 SHA1_Update(&ctx, buffer, offset);
33 memcpy(buffer, buffer + offset, len);
37 int ret = xread(0, buffer + len, sizeof(buffer) - len);
41 die("read error on input: %s", strerror(errno));
48 static void use(int bytes)
51 die("used more bytes than were available");
54 consumed_bytes += bytes;
57 static void *get_data(unsigned long size)
60 void *buf = xmalloc(size);
62 memset(&stream, 0, sizeof(stream));
64 stream.next_out = buf;
65 stream.avail_out = size;
66 stream.next_in = fill(1);
67 stream.avail_in = len;
71 int ret = inflate(&stream, 0);
72 use(len - stream.avail_in);
73 if (stream.total_out == size && ret == Z_STREAM_END)
76 error("inflate returned %d\n", ret);
84 stream.next_in = fill(1);
85 stream.avail_in = len;
92 unsigned char base_sha1[20];
93 unsigned long base_offset;
97 struct delta_info *next;
100 static struct delta_info *delta_list;
102 static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
103 unsigned long base_offset,
104 void *delta, unsigned long size)
106 struct delta_info *info = xmalloc(sizeof(*info));
108 hashcpy(info->base_sha1, base_sha1);
109 info->base_offset = base_offset;
113 info->next = delta_list;
118 unsigned long offset;
119 unsigned char sha1[20];
122 static struct obj_info *obj_list;
124 static void added_object(unsigned nr, const char *type, void *data,
127 static void write_object(unsigned nr, void *buf, unsigned long size,
130 if (write_sha1_file(buf, size, type, obj_list[nr].sha1) < 0)
131 die("failed to write object");
132 added_object(nr, type, buf, size);
135 static void resolve_delta(unsigned nr, const char *type,
136 void *base, unsigned long base_size,
137 void *delta, unsigned long delta_size)
140 unsigned long result_size;
142 result = patch_delta(base, base_size,
146 die("failed to apply delta");
148 write_object(nr, result, result_size, type);
152 static void added_object(unsigned nr, const char *type, void *data,
155 struct delta_info **p = &delta_list;
156 struct delta_info *info;
158 while ((info = *p) != NULL) {
159 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
160 info->base_offset == obj_list[nr].offset) {
163 resolve_delta(info->nr, type, data, size,
164 info->delta, info->size);
172 static void unpack_non_delta_entry(enum object_type kind, unsigned long size,
175 void *buf = get_data(size);
179 case OBJ_COMMIT: type = commit_type; break;
180 case OBJ_TREE: type = tree_type; break;
181 case OBJ_BLOB: type = blob_type; break;
182 case OBJ_TAG: type = tag_type; break;
183 default: die("bad type %d", kind);
186 write_object(nr, buf, size, type);
190 static void unpack_delta_entry(enum object_type kind, unsigned long delta_size,
193 void *delta_data, *base;
194 unsigned long base_size;
196 unsigned char base_sha1[20];
198 if (kind == OBJ_REF_DELTA) {
199 hashcpy(base_sha1, fill(20));
201 delta_data = get_data(delta_size);
202 if (dry_run || !delta_data) {
206 if (!has_sha1_file(base_sha1)) {
207 hashcpy(obj_list[nr].sha1, null_sha1);
208 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
212 unsigned base_found = 0;
213 unsigned char *pack, c;
214 unsigned long base_offset;
215 unsigned lo, mid, hi;
220 base_offset = c & 127;
223 if (!base_offset || base_offset & ~(~0UL >> 7))
224 die("offset value overflow for delta base object");
228 base_offset = (base_offset << 7) + (c & 127);
230 base_offset = obj_list[nr].offset - base_offset;
232 delta_data = get_data(delta_size);
233 if (dry_run || !delta_data) {
241 if (base_offset < obj_list[mid].offset) {
243 } else if (base_offset > obj_list[mid].offset) {
246 hashcpy(base_sha1, obj_list[mid].sha1);
247 base_found = !is_null_sha1(base_sha1);
252 /* The delta base object is itself a delta that
253 has not been resolved yet. */
254 hashcpy(obj_list[nr].sha1, null_sha1);
255 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
260 base = read_sha1_file(base_sha1, type, &base_size);
262 error("failed to read delta-pack base object %s",
263 sha1_to_hex(base_sha1));
269 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
273 static void unpack_one(unsigned nr, unsigned total)
276 unsigned char *pack, c;
278 enum object_type type;
280 obj_list[nr].offset = consumed_bytes;
292 size += (c & 0x7f) << shift;
296 static unsigned long last_sec;
297 static unsigned last_percent;
299 unsigned percentage = ((nr+1) * 100) / total;
301 gettimeofday(&now, NULL);
302 if (percentage != last_percent || now.tv_sec != last_sec) {
303 last_sec = now.tv_sec;
304 last_percent = percentage;
305 fprintf(stderr, "%4u%% (%u/%u) done\r",
306 percentage, (nr+1), total);
314 unpack_non_delta_entry(type, size, nr);
318 unpack_delta_entry(type, size, nr);
321 error("bad object type %d", type);
329 static void unpack_all(void)
332 struct pack_header *hdr = fill(sizeof(struct pack_header));
333 unsigned nr_objects = ntohl(hdr->hdr_entries);
335 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
336 die("bad pack file");
337 if (!pack_version_ok(hdr->hdr_version))
338 die("unknown pack file version %d", ntohl(hdr->hdr_version));
339 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
341 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
342 use(sizeof(struct pack_header));
343 for (i = 0; i < nr_objects; i++)
344 unpack_one(i, nr_objects);
346 die("unresolved deltas left after unpacking");
349 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
352 unsigned char sha1[20];
354 git_config(git_default_config);
358 for (i = 1 ; i < argc; i++) {
359 const char *arg = argv[i];
362 if (!strcmp(arg, "-n")) {
366 if (!strcmp(arg, "-q")) {
370 if (!strcmp(arg, "-r")) {
377 /* We don't take any non-flag arguments now.. Maybe some day */
382 SHA1_Update(&ctx, buffer, offset);
383 SHA1_Final(sha1, &ctx);
384 if (hashcmp(fill(20), sha1))
385 die("final sha1 did not match");
388 /* Write the last part of the buffer to stdout */
390 int ret = xwrite(1, buffer + offset, len);
399 fprintf(stderr, "\n");