13 static int dry_run, quiet;
14 static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
16 /* We always read in 4kB chunks. */
17 static unsigned char buffer[4096];
18 static unsigned long offset, len;
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");
56 static void *get_data(unsigned long size)
59 void *buf = xmalloc(size);
61 memset(&stream, 0, sizeof(stream));
63 stream.next_out = buf;
64 stream.avail_out = size;
65 stream.next_in = fill(1);
66 stream.avail_in = len;
70 int ret = inflate(&stream, 0);
71 use(len - stream.avail_in);
72 if (stream.total_out == size && ret == Z_STREAM_END)
75 die("inflate returned %d\n", ret);
76 stream.next_in = fill(1);
77 stream.avail_in = len;
84 unsigned char base_sha1[20];
87 struct delta_info *next;
90 static struct delta_info *delta_list;
92 static void add_delta_to_list(unsigned char *base_sha1, void *delta, unsigned long size)
94 struct delta_info *info = xmalloc(sizeof(*info));
96 hashcpy(info->base_sha1, base_sha1);
99 info->next = delta_list;
103 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size);
105 static void write_object(void *buf, unsigned long size, const char *type)
107 unsigned char sha1[20];
108 if (write_sha1_file(buf, size, type, sha1) < 0)
109 die("failed to write object");
110 added_object(sha1, type, buf, size);
113 static int resolve_delta(const char *type,
114 void *base, unsigned long base_size,
115 void *delta, unsigned long delta_size)
118 unsigned long result_size;
120 result = patch_delta(base, base_size,
124 die("failed to apply delta");
126 write_object(result, result_size, type);
131 static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
133 struct delta_info **p = &delta_list;
134 struct delta_info *info;
136 while ((info = *p) != NULL) {
137 if (!hashcmp(info->base_sha1, sha1)) {
140 resolve_delta(type, data, size, info->delta, info->size);
148 static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
150 void *buf = get_data(size);
154 case OBJ_COMMIT: type = commit_type; break;
155 case OBJ_TREE: type = tree_type; break;
156 case OBJ_BLOB: type = blob_type; break;
157 case OBJ_TAG: type = tag_type; break;
158 default: die("bad type %d", kind);
161 write_object(buf, size, type);
166 static int unpack_delta_entry(unsigned long delta_size)
168 void *delta_data, *base;
169 unsigned long base_size;
171 unsigned char base_sha1[20];
174 hashcpy(base_sha1, fill(20));
177 delta_data = get_data(delta_size);
183 if (!has_sha1_file(base_sha1)) {
184 add_delta_to_list(base_sha1, delta_data, delta_size);
187 base = read_sha1_file(base_sha1, type, &base_size);
189 die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
190 result = resolve_delta(type, base, base_size, delta_data, delta_size);
195 static void unpack_one(unsigned nr, unsigned total)
198 unsigned char *pack, c;
200 enum object_type type;
212 size += (c & 0x7f) << shift;
216 static unsigned long last_sec;
217 static unsigned last_percent;
219 unsigned percentage = (nr * 100) / total;
221 gettimeofday(&now, NULL);
222 if (percentage != last_percent || now.tv_sec != last_sec) {
223 last_sec = now.tv_sec;
224 last_percent = percentage;
225 fprintf(stderr, "%4u%% (%u/%u) done\r", percentage, nr, total);
233 unpack_non_delta_entry(type, size);
236 unpack_delta_entry(size);
239 die("bad object type %d", type);
243 static void unpack_all(void)
246 struct pack_header *hdr = fill(sizeof(struct pack_header));
247 unsigned nr_objects = ntohl(hdr->hdr_entries);
249 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
250 die("bad pack file");
251 if (!pack_version_ok(hdr->hdr_version))
252 die("unknown pack file version %d", ntohl(hdr->hdr_version));
253 fprintf(stderr, "Unpacking %d objects\n", nr_objects);
255 use(sizeof(struct pack_header));
256 for (i = 0; i < nr_objects; i++)
257 unpack_one(i+1, nr_objects);
259 die("unresolved deltas left after unpacking");
262 int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
265 unsigned char sha1[20];
267 git_config(git_default_config);
271 for (i = 1 ; i < argc; i++) {
272 const char *arg = argv[i];
275 if (!strcmp(arg, "-n")) {
279 if (!strcmp(arg, "-q")) {
286 /* We don't take any non-flag arguments now.. Maybe some day */
291 SHA1_Update(&ctx, buffer, offset);
292 SHA1_Final(sha1, &ctx);
293 if (hashcmp(fill(20), sha1))
294 die("final sha1 did not match");
297 /* Write the last part of the buffer to stdout */
299 int ret = xwrite(1, buffer + offset, len);
308 fprintf(stderr, "\n");