blob: 1e51865c52231e80cfdbbb19c8b6fa86ee8855d2 [file] [log] [blame]
Matthias Kestenholz64413632006-08-03 17:24:37 +02001#include "builtin.h"
Linus Torvaldsbad50dc2005-06-25 15:27:14 -07002#include "cache.h"
Linus Torvalds74536952005-06-25 15:59:31 -07003#include "object.h"
Junio C Hamano8ee378a2005-06-26 04:29:18 -07004#include "delta.h"
Linus Torvaldsa733cb62005-06-28 14:21:02 -07005#include "pack.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02006#include "blob.h"
7#include "commit.h"
8#include "tag.h"
9#include "tree.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040010#include "progress.h"
Linus Torvaldsbad50dc2005-06-25 15:27:14 -070011
Junio C Hamano3b67d292006-09-13 12:59:20 -070012static int dry_run, quiet, recover, has_errors;
13static const char unpack_usage[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
Linus Torvaldsbad50dc2005-06-25 15:27:14 -070014
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070015/* We always read in 4kB chunks. */
16static unsigned char buffer[4096];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040017static unsigned int offset, len;
18static off_t consumed_bytes;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070019static SHA_CTX ctx;
20
21/*
22 * Make sure at least "min" bytes are available in the buffer, and
23 * return the pointer to the buffer.
24 */
Edgar Toernig79a65692006-10-30 17:44:27 -080025static void *fill(int min)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070026{
27 if (min <= len)
28 return buffer + offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070029 if (min > sizeof(buffer))
30 die("cannot fill %d bytes", min);
31 if (offset) {
32 SHA1_Update(&ctx, buffer, offset);
Edgar Toernig79a65692006-10-30 17:44:27 -080033 memmove(buffer, buffer + offset, len);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070034 offset = 0;
35 }
36 do {
Johan Herland8a912bc2007-05-15 14:49:22 +020037 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070038 if (ret <= 0) {
39 if (!ret)
40 die("early EOF");
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070041 die("read error on input: %s", strerror(errno));
42 }
43 len += ret;
44 } while (len < min);
45 return buffer;
46}
47
48static void use(int bytes)
49{
50 if (bytes > len)
51 die("used more bytes than were available");
52 len -= bytes;
53 offset += bytes;
Nicolas Pitred7dd0222007-04-09 01:06:30 -040054
55 /* make sure off_t is sufficiently large not to wrap */
56 if (consumed_bytes > consumed_bytes + bytes)
57 die("pack too large for current definition of off_t");
Nicolas Pitre209c5542006-09-21 00:07:39 -040058 consumed_bytes += bytes;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070059}
60
61static void *get_data(unsigned long size)
62{
63 z_stream stream;
64 void *buf = xmalloc(size);
65
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070066 memset(&stream, 0, sizeof(stream));
67
68 stream.next_out = buf;
69 stream.avail_out = size;
70 stream.next_in = fill(1);
71 stream.avail_in = len;
72 inflateInit(&stream);
73
74 for (;;) {
75 int ret = inflate(&stream, 0);
76 use(len - stream.avail_in);
77 if (stream.total_out == size && ret == Z_STREAM_END)
78 break;
Junio C Hamanof986f2c2006-09-03 22:55:54 -070079 if (ret != Z_OK) {
80 error("inflate returned %d\n", ret);
81 free(buf);
82 buf = NULL;
Junio C Hamano3b67d292006-09-13 12:59:20 -070083 if (!recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -070084 exit(1);
85 has_errors = 1;
86 break;
87 }
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070088 stream.next_in = fill(1);
89 stream.avail_in = len;
90 }
Sergey Vlasovee639142005-08-03 16:11:00 +040091 inflateEnd(&stream);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070092 return buf;
93}
94
95struct delta_info {
96 unsigned char base_sha1[20];
Nicolas Pitred7dd0222007-04-09 01:06:30 -040097 unsigned nr;
98 off_t base_offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -070099 unsigned long size;
100 void *delta;
101 struct delta_info *next;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700102};
103
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700104static struct delta_info *delta_list;
Linus Torvalds74536952005-06-25 15:59:31 -0700105
Nicolas Pitre209c5542006-09-21 00:07:39 -0400106static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400107 off_t base_offset,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400108 void *delta, unsigned long size)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700109{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700110 struct delta_info *info = xmalloc(sizeof(*info));
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700111
Shawn Pearcee7024962006-08-23 02:49:00 -0400112 hashcpy(info->base_sha1, base_sha1);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400113 info->base_offset = base_offset;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700114 info->size = size;
115 info->delta = delta;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400116 info->nr = nr;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700117 info->next = delta_list;
118 delta_list = info;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700119}
120
Nicolas Pitre209c5542006-09-21 00:07:39 -0400121struct obj_info {
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400122 off_t offset;
Linus Torvaldscca70812005-06-29 09:38:02 -0700123 unsigned char sha1[20];
Nicolas Pitre209c5542006-09-21 00:07:39 -0400124};
125
126static struct obj_info *obj_list;
127
Nicolas Pitre21666f12007-02-26 14:55:59 -0500128static void added_object(unsigned nr, enum object_type type,
129 void *data, unsigned long size);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400130
Nicolas Pitre21666f12007-02-26 14:55:59 -0500131static void write_object(unsigned nr, enum object_type type,
132 void *buf, unsigned long size)
Nicolas Pitre209c5542006-09-21 00:07:39 -0400133{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500134 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
Linus Torvaldscca70812005-06-29 09:38:02 -0700135 die("failed to write object");
Nicolas Pitre209c5542006-09-21 00:07:39 -0400136 added_object(nr, type, buf, size);
Linus Torvaldscca70812005-06-29 09:38:02 -0700137}
138
Nicolas Pitre21666f12007-02-26 14:55:59 -0500139static void resolve_delta(unsigned nr, enum object_type type,
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700140 void *base, unsigned long base_size,
141 void *delta, unsigned long delta_size)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700142{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700143 void *result;
144 unsigned long result_size;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700145
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700146 result = patch_delta(base, base_size,
147 delta, delta_size,
148 &result_size);
149 if (!result)
150 die("failed to apply delta");
151 free(delta);
Nicolas Pitre21666f12007-02-26 14:55:59 -0500152 write_object(nr, type, result, result_size);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700153 free(result);
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700154}
155
Nicolas Pitre21666f12007-02-26 14:55:59 -0500156static void added_object(unsigned nr, enum object_type type,
157 void *data, unsigned long size)
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700158{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700159 struct delta_info **p = &delta_list;
160 struct delta_info *info;
161
162 while ((info = *p) != NULL) {
Nicolas Pitre209c5542006-09-21 00:07:39 -0400163 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
164 info->base_offset == obj_list[nr].offset) {
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700165 *p = info->next;
166 p = &delta_list;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400167 resolve_delta(info->nr, type, data, size,
168 info->delta, info->size);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700169 free(info);
170 continue;
171 }
172 p = &info->next;
173 }
174}
175
Nicolas Pitre21666f12007-02-26 14:55:59 -0500176static void unpack_non_delta_entry(enum object_type type, unsigned long size,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400177 unsigned nr)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700178{
179 void *buf = get_data(size);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700180
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700181 if (!dry_run && buf)
Nicolas Pitre21666f12007-02-26 14:55:59 -0500182 write_object(nr, type, buf, size);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700183 free(buf);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700184}
185
Nicolas Pitre21666f12007-02-26 14:55:59 -0500186static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
Nicolas Pitre209c5542006-09-21 00:07:39 -0400187 unsigned nr)
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700188{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700189 void *delta_data, *base;
190 unsigned long base_size;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700191 unsigned char base_sha1[20];
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700192
Nicolas Pitre21666f12007-02-26 14:55:59 -0500193 if (type == OBJ_REF_DELTA) {
Nicolas Pitre209c5542006-09-21 00:07:39 -0400194 hashcpy(base_sha1, fill(20));
195 use(20);
196 delta_data = get_data(delta_size);
197 if (dry_run || !delta_data) {
198 free(delta_data);
199 return;
200 }
201 if (!has_sha1_file(base_sha1)) {
202 hashcpy(obj_list[nr].sha1, null_sha1);
203 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
204 return;
205 }
206 } else {
207 unsigned base_found = 0;
208 unsigned char *pack, c;
Nicolas Pitred7dd0222007-04-09 01:06:30 -0400209 off_t base_offset;
Nicolas Pitre209c5542006-09-21 00:07:39 -0400210 unsigned lo, mid, hi;
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700211
Nicolas Pitre209c5542006-09-21 00:07:39 -0400212 pack = fill(1);
213 c = *pack;
214 use(1);
215 base_offset = c & 127;
216 while (c & 128) {
217 base_offset += 1;
Nicolas Pitre8723f212007-04-09 01:06:29 -0400218 if (!base_offset || MSB(base_offset, 7))
Nicolas Pitre209c5542006-09-21 00:07:39 -0400219 die("offset value overflow for delta base object");
220 pack = fill(1);
221 c = *pack;
222 use(1);
223 base_offset = (base_offset << 7) + (c & 127);
224 }
225 base_offset = obj_list[nr].offset - base_offset;
226
227 delta_data = get_data(delta_size);
228 if (dry_run || !delta_data) {
229 free(delta_data);
230 return;
231 }
232 lo = 0;
233 hi = nr;
234 while (lo < hi) {
235 mid = (lo + hi)/2;
236 if (base_offset < obj_list[mid].offset) {
237 hi = mid;
238 } else if (base_offset > obj_list[mid].offset) {
239 lo = mid + 1;
240 } else {
241 hashcpy(base_sha1, obj_list[mid].sha1);
242 base_found = !is_null_sha1(base_sha1);
243 break;
244 }
245 }
246 if (!base_found) {
247 /* The delta base object is itself a delta that
248 has not been resolved yet. */
249 hashcpy(obj_list[nr].sha1, null_sha1);
250 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
251 return;
252 }
Linus Torvaldsdddafff2005-06-29 09:53:20 -0700253 }
Linus Torvaldsc4fb06c2005-06-26 08:40:08 -0700254
Nicolas Pitre21666f12007-02-26 14:55:59 -0500255 base = read_sha1_file(base_sha1, &type, &base_size);
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700256 if (!base) {
257 error("failed to read delta-pack base object %s",
258 sha1_to_hex(base_sha1));
Junio C Hamano3b67d292006-09-13 12:59:20 -0700259 if (!recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700260 exit(1);
261 has_errors = 1;
262 return;
263 }
Nicolas Pitre209c5542006-09-21 00:07:39 -0400264 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
Sergey Vlasovee639142005-08-03 16:11:00 +0400265 free(base);
Junio C Hamano8ee378a2005-06-26 04:29:18 -0700266}
267
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400268static void unpack_one(unsigned nr)
Linus Torvalds74536952005-06-25 15:59:31 -0700269{
Linus Torvalds01247d82005-06-28 22:15:57 -0700270 unsigned shift;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700271 unsigned char *pack, c;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700272 unsigned long size;
273 enum object_type type;
Linus Torvalds74536952005-06-25 15:59:31 -0700274
Nicolas Pitre209c5542006-09-21 00:07:39 -0400275 obj_list[nr].offset = consumed_bytes;
276
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700277 pack = fill(1);
278 c = *pack;
279 use(1);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700280 type = (c >> 4) & 7;
281 size = (c & 15);
Linus Torvalds01247d82005-06-28 22:15:57 -0700282 shift = 4;
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700283 while (c & 0x80) {
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700284 pack = fill(1);
Nicolas Pitre209c5542006-09-21 00:07:39 -0400285 c = *pack;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700286 use(1);
Linus Torvalds01247d82005-06-28 22:15:57 -0700287 size += (c & 0x7f) << shift;
288 shift += 7;
Linus Torvalds74536952005-06-25 15:59:31 -0700289 }
Linus Torvaldsd36f7b82005-07-09 10:43:02 -0700290
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700291 switch (type) {
292 case OBJ_COMMIT:
293 case OBJ_TREE:
294 case OBJ_BLOB:
295 case OBJ_TAG:
Nicolas Pitre209c5542006-09-21 00:07:39 -0400296 unpack_non_delta_entry(type, size, nr);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700297 return;
Nicolas Pitreeb32d232006-09-21 00:06:49 -0400298 case OBJ_REF_DELTA:
Nicolas Pitre209c5542006-09-21 00:07:39 -0400299 case OBJ_OFS_DELTA:
300 unpack_delta_entry(type, size, nr);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700301 return;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700302 default:
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700303 error("bad object type %d", type);
304 has_errors = 1;
Junio C Hamano3b67d292006-09-13 12:59:20 -0700305 if (recover)
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700306 return;
307 exit(1);
Linus Torvaldsa733cb62005-06-28 14:21:02 -0700308 }
Linus Torvalds74536952005-06-25 15:59:31 -0700309}
310
Linus Torvalds74536952005-06-25 15:59:31 -0700311static void unpack_all(void)
312{
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700313 int i;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400314 struct progress *progress = NULL;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700315 struct pack_header *hdr = fill(sizeof(struct pack_header));
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700316 unsigned nr_objects = ntohl(hdr->hdr_entries);
Linus Torvalds74536952005-06-25 15:59:31 -0700317
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700318 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
319 die("bad pack file");
Nicolas Pitred60fc1c2006-02-09 17:50:04 -0500320 if (!pack_version_ok(hdr->hdr_version))
321 die("unknown pack file version %d", ntohl(hdr->hdr_version));
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700322 use(sizeof(struct pack_header));
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400323
Nicolas Pitre13aaf142007-04-20 14:10:07 -0400324 if (!quiet)
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400325 progress = start_progress("Unpacking objects", nr_objects);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400326 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
327 for (i = 0; i < nr_objects; i++) {
328 unpack_one(i);
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400329 display_progress(progress, i + 1);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400330 }
Nicolas Pitre4d4fcc52007-10-30 14:57:33 -0400331 stop_progress(&progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400332
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700333 if (delta_list)
334 die("unresolved deltas left after unpacking");
Linus Torvalds74536952005-06-25 15:59:31 -0700335}
336
Matthias Kestenholz64413632006-08-03 17:24:37 +0200337int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700338{
339 int i;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700340 unsigned char sha1[20];
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700341
Junio C Hamano8e273642006-07-17 15:10:10 -0700342 git_config(git_default_config);
Junio C Hamano53228a52005-11-26 00:50:02 -0800343
Junio C Hamano476e8012006-01-06 18:53:16 -0800344 quiet = !isatty(2);
345
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700346 for (i = 1 ; i < argc; i++) {
347 const char *arg = argv[i];
348
349 if (*arg == '-') {
Linus Torvalds74536952005-06-25 15:59:31 -0700350 if (!strcmp(arg, "-n")) {
351 dry_run = 1;
352 continue;
353 }
Linus Torvaldsd36f7b82005-07-09 10:43:02 -0700354 if (!strcmp(arg, "-q")) {
355 quiet = 1;
356 continue;
357 }
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700358 if (!strcmp(arg, "-r")) {
Junio C Hamano3b67d292006-09-13 12:59:20 -0700359 recover = 1;
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700360 continue;
361 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800362 if (!prefixcmp(arg, "--pack_header=")) {
Nicolas Pitrebed006f2006-11-01 17:06:20 -0500363 struct pack_header *hdr;
364 char *c;
365
366 hdr = (struct pack_header *)buffer;
367 hdr->hdr_signature = htonl(PACK_SIGNATURE);
368 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
369 if (*c != ',')
370 die("bad %s", arg);
371 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
372 if (*c)
373 die("bad %s", arg);
374 len = sizeof(*hdr);
375 continue;
376 }
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700377 usage(unpack_usage);
378 }
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700379
380 /* We don't take any non-flag arguments now.. Maybe some day */
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700381 usage(unpack_usage);
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700382 }
383 SHA1_Init(&ctx);
Linus Torvalds74536952005-06-25 15:59:31 -0700384 unpack_all();
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700385 SHA1_Update(&ctx, buffer, offset);
386 SHA1_Final(sha1, &ctx);
David Rientjesa89fccd2006-08-17 11:54:57 -0700387 if (hashcmp(fill(20), sha1))
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700388 die("final sha1 did not match");
389 use(20);
390
391 /* Write the last part of the buffer to stdout */
392 while (len) {
Junio C Hamano1c15afb2005-12-19 16:18:28 -0800393 int ret = xwrite(1, buffer + offset, len);
394 if (ret <= 0)
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700395 break;
Linus Torvalds67e5a5e2005-06-28 20:34:23 -0700396 len -= ret;
397 offset += ret;
398 }
399
400 /* All done */
Junio C Hamanof986f2c2006-09-03 22:55:54 -0700401 return has_errors;
Linus Torvaldsbad50dc2005-06-25 15:27:14 -0700402}