fsck.c: fix bogus "empty tree" check
[git/git.git] / builtin-unpack-objects.c
CommitLineData
64413630 1#include "builtin.h"
bad50dc8 2#include "cache.h"
74536958 3#include "object.h"
8ee378a0 4#include "delta.h"
a733cb60 5#include "pack.h"
8e440259
PE
6#include "blob.h"
7#include "commit.h"
8#include "tag.h"
9#include "tree.h"
96a02f8f 10#include "progress.h"
2add1e6d 11#include "decorate.h"
bad50dc8 12
c95b3ad9
JH
13static int dry_run, quiet, recover, has_errors;
14static const char unpack_usage[] = "git-unpack-objects [-n] [-q] [-r] < pack-file";
bad50dc8 15
67e5a5ec
LT
16/* We always read in 4kB chunks. */
17static unsigned char buffer[4096];
d7dd0223
NP
18static unsigned int offset, len;
19static off_t consumed_bytes;
67e5a5ec 20static SHA_CTX ctx;
bad50dc8 21
2add1e6d
MK
22struct obj_buffer {
23 char *buffer;
24 unsigned long size;
25};
26
27static struct decoration obj_decorate;
28
29static struct obj_buffer *lookup_object_buffer(struct object *base)
30{
31 return lookup_decoration(&obj_decorate, base);
32}
33
67e5a5ec
LT
34/*
35 * Make sure at least "min" bytes are available in the buffer, and
36 * return the pointer to the buffer.
37 */
79a65697 38static void *fill(int min)
67e5a5ec
LT
39{
40 if (min <= len)
41 return buffer + offset;
67e5a5ec
LT
42 if (min > sizeof(buffer))
43 die("cannot fill %d bytes", min);
44 if (offset) {
45 SHA1_Update(&ctx, buffer, offset);
79a65697 46 memmove(buffer, buffer + offset, len);
67e5a5ec
LT
47 offset = 0;
48 }
49 do {
8a912bcb 50 ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len);
67e5a5ec
LT
51 if (ret <= 0) {
52 if (!ret)
53 die("early EOF");
67e5a5ec
LT
54 die("read error on input: %s", strerror(errno));
55 }
56 len += ret;
57 } while (len < min);
58 return buffer;
59}
74536958 60
67e5a5ec
LT
61static void use(int bytes)
62{
63 if (bytes > len)
64 die("used more bytes than were available");
65 len -= bytes;
66 offset += bytes;
d7dd0223
NP
67
68 /* make sure off_t is sufficiently large not to wrap */
69 if (consumed_bytes > consumed_bytes + bytes)
70 die("pack too large for current definition of off_t");
209c554a 71 consumed_bytes += bytes;
67e5a5ec 72}
bad50dc8 73
67e5a5ec 74static void *get_data(unsigned long size)
bad50dc8 75{
67e5a5ec
LT
76 z_stream stream;
77 void *buf = xmalloc(size);
78
67e5a5ec
LT
79 memset(&stream, 0, sizeof(stream));
80
81 stream.next_out = buf;
82 stream.avail_out = size;
83 stream.next_in = fill(1);
84 stream.avail_in = len;
85 inflateInit(&stream);
86
87 for (;;) {
88 int ret = inflate(&stream, 0);
89 use(len - stream.avail_in);
90 if (stream.total_out == size && ret == Z_STREAM_END)
91 break;
f986f2c8
JH
92 if (ret != Z_OK) {
93 error("inflate returned %d\n", ret);
94 free(buf);
95 buf = NULL;
3b67d291 96 if (!recover)
f986f2c8
JH
97 exit(1);
98 has_errors = 1;
99 break;
100 }
67e5a5ec
LT
101 stream.next_in = fill(1);
102 stream.avail_in = len;
103 }
ee639140 104 inflateEnd(&stream);
67e5a5ec 105 return buf;
bad50dc8
LT
106}
107
67e5a5ec
LT
108struct delta_info {
109 unsigned char base_sha1[20];
d7dd0223
NP
110 unsigned nr;
111 off_t base_offset;
67e5a5ec
LT
112 unsigned long size;
113 void *delta;
114 struct delta_info *next;
115};
116
117static struct delta_info *delta_list;
118
209c554a 119static void add_delta_to_list(unsigned nr, unsigned const char *base_sha1,
d7dd0223 120 off_t base_offset,
209c554a 121 void *delta, unsigned long size)
bad50dc8 122{
67e5a5ec 123 struct delta_info *info = xmalloc(sizeof(*info));
bad50dc8 124
e702496e 125 hashcpy(info->base_sha1, base_sha1);
209c554a 126 info->base_offset = base_offset;
67e5a5ec
LT
127 info->size = size;
128 info->delta = delta;
209c554a 129 info->nr = nr;
67e5a5ec
LT
130 info->next = delta_list;
131 delta_list = info;
bad50dc8
LT
132}
133
209c554a 134struct obj_info {
d7dd0223 135 off_t offset;
209c554a
NP
136 unsigned char sha1[20];
137};
138
139static struct obj_info *obj_list;
67e5a5ec 140
21666f1a
NP
141static void added_object(unsigned nr, enum object_type type,
142 void *data, unsigned long size);
209c554a 143
21666f1a
NP
144static void write_object(unsigned nr, enum object_type type,
145 void *buf, unsigned long size)
cca7081a 146{
c95b3ad9
JH
147 if (write_sha1_file(buf, size, typename(type), obj_list[nr].sha1) < 0)
148 die("failed to write object");
209c554a 149 added_object(nr, type, buf, size);
cca7081a
LT
150}
151
21666f1a 152static void resolve_delta(unsigned nr, enum object_type type,
f986f2c8
JH
153 void *base, unsigned long base_size,
154 void *delta, unsigned long delta_size)
bad50dc8 155{
67e5a5ec
LT
156 void *result;
157 unsigned long result_size;
bad50dc8 158
67e5a5ec
LT
159 result = patch_delta(base, base_size,
160 delta, delta_size,
161 &result_size);
162 if (!result)
163 die("failed to apply delta");
164 free(delta);
21666f1a 165 write_object(nr, type, result, result_size);
c95b3ad9 166 free(result);
bad50dc8
LT
167}
168
21666f1a
NP
169static void added_object(unsigned nr, enum object_type type,
170 void *data, unsigned long size)
74536958 171{
67e5a5ec
LT
172 struct delta_info **p = &delta_list;
173 struct delta_info *info;
174
175 while ((info = *p) != NULL) {
209c554a
NP
176 if (!hashcmp(info->base_sha1, obj_list[nr].sha1) ||
177 info->base_offset == obj_list[nr].offset) {
67e5a5ec
LT
178 *p = info->next;
179 p = &delta_list;
209c554a
NP
180 resolve_delta(info->nr, type, data, size,
181 info->delta, info->size);
67e5a5ec
LT
182 free(info);
183 continue;
184 }
185 p = &info->next;
186 }
187}
188
21666f1a 189static void unpack_non_delta_entry(enum object_type type, unsigned long size,
209c554a 190 unsigned nr)
67e5a5ec
LT
191{
192 void *buf = get_data(size);
8ee378a0 193
f986f2c8 194 if (!dry_run && buf)
21666f1a 195 write_object(nr, type, buf, size);
c95b3ad9 196 free(buf);
8ee378a0
JH
197}
198
21666f1a 199static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
209c554a 200 unsigned nr)
8ee378a0 201{
67e5a5ec
LT
202 void *delta_data, *base;
203 unsigned long base_size;
67e5a5ec 204 unsigned char base_sha1[20];
2add1e6d 205 struct object *obj;
8ee378a0 206
21666f1a 207 if (type == OBJ_REF_DELTA) {
209c554a
NP
208 hashcpy(base_sha1, fill(20));
209 use(20);
210 delta_data = get_data(delta_size);
211 if (dry_run || !delta_data) {
212 free(delta_data);
213 return;
214 }
215 if (!has_sha1_file(base_sha1)) {
216 hashcpy(obj_list[nr].sha1, null_sha1);
217 add_delta_to_list(nr, base_sha1, 0, delta_data, delta_size);
218 return;
219 }
220 } else {
221 unsigned base_found = 0;
222 unsigned char *pack, c;
d7dd0223 223 off_t base_offset;
209c554a 224 unsigned lo, mid, hi;
c4fb06c0 225
209c554a
NP
226 pack = fill(1);
227 c = *pack;
228 use(1);
229 base_offset = c & 127;
230 while (c & 128) {
231 base_offset += 1;
8723f216 232 if (!base_offset || MSB(base_offset, 7))
209c554a
NP
233 die("offset value overflow for delta base object");
234 pack = fill(1);
235 c = *pack;
236 use(1);
237 base_offset = (base_offset << 7) + (c & 127);
238 }
239 base_offset = obj_list[nr].offset - base_offset;
8ee378a0 240
209c554a
NP
241 delta_data = get_data(delta_size);
242 if (dry_run || !delta_data) {
243 free(delta_data);
244 return;
245 }
246 lo = 0;
247 hi = nr;
248 while (lo < hi) {
249 mid = (lo + hi)/2;
250 if (base_offset < obj_list[mid].offset) {
251 hi = mid;
252 } else if (base_offset > obj_list[mid].offset) {
253 lo = mid + 1;
254 } else {
255 hashcpy(base_sha1, obj_list[mid].sha1);
256 base_found = !is_null_sha1(base_sha1);
257 break;
258 }
259 }
260 if (!base_found) {
261 /* The delta base object is itself a delta that
262 has not been resolved yet. */
263 hashcpy(obj_list[nr].sha1, null_sha1);
264 add_delta_to_list(nr, null_sha1, base_offset, delta_data, delta_size);
265 return;
2add1e6d
MK
266 }
267 }
268
269 obj = lookup_object(base_sha1);
270 if (obj) {
271 struct obj_buffer *obj_buf = lookup_object_buffer(obj);
272 if (obj_buf) {
273 resolve_delta(nr, obj->type, obj_buf->buffer, obj_buf->size, delta_data, delta_size);
274 return;
209c554a 275 }
8ee378a0 276 }
209c554a 277
21666f1a 278 base = read_sha1_file(base_sha1, &type, &base_size);
f986f2c8
JH
279 if (!base) {
280 error("failed to read delta-pack base object %s",
281 sha1_to_hex(base_sha1));
3b67d291 282 if (!recover)
f986f2c8
JH
283 exit(1);
284 has_errors = 1;
285 return;
286 }
209c554a 287 resolve_delta(nr, type, base, base_size, delta_data, delta_size);
ee639140 288 free(base);
8ee378a0
JH
289}
290
96a02f8f 291static void unpack_one(unsigned nr)
8ee378a0 292{
01247d87 293 unsigned shift;
a733cb60 294 unsigned char *pack, c;
67e5a5ec
LT
295 unsigned long size;
296 enum object_type type;
74536958 297
209c554a
NP
298 obj_list[nr].offset = consumed_bytes;
299
67e5a5ec
LT
300 pack = fill(1);
301 c = *pack;
302 use(1);
a733cb60
LT
303 type = (c >> 4) & 7;
304 size = (c & 15);
01247d87 305 shift = 4;
a733cb60 306 while (c & 0x80) {
67e5a5ec 307 pack = fill(1);
209c554a 308 c = *pack;
67e5a5ec 309 use(1);
01247d87
LT
310 size += (c & 0x7f) << shift;
311 shift += 7;
a733cb60 312 }
96a02f8f 313
a733cb60
LT
314 switch (type) {
315 case OBJ_COMMIT:
316 case OBJ_TREE:
317 case OBJ_BLOB:
318 case OBJ_TAG:
209c554a 319 unpack_non_delta_entry(type, size, nr);
a733cb60 320 return;
eb32d236 321 case OBJ_REF_DELTA:
209c554a
NP
322 case OBJ_OFS_DELTA:
323 unpack_delta_entry(type, size, nr);
a733cb60 324 return;
67e5a5ec 325 default:
f986f2c8
JH
326 error("bad object type %d", type);
327 has_errors = 1;
3b67d291 328 if (recover)
f986f2c8
JH
329 return;
330 exit(1);
74536958
LT
331 }
332}
333
74536958
LT
334static void unpack_all(void)
335{
67e5a5ec 336 int i;
dc6a0757 337 struct progress *progress = NULL;
67e5a5ec 338 struct pack_header *hdr = fill(sizeof(struct pack_header));
c95b3ad9 339 unsigned nr_objects = ntohl(hdr->hdr_entries);
67e5a5ec
LT
340
341 if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
342 die("bad pack file");
d60fc1c8
NP
343 if (!pack_version_ok(hdr->hdr_version))
344 die("unknown pack file version %d", ntohl(hdr->hdr_version));
96a02f8f 345 use(sizeof(struct pack_header));
67e5a5ec 346
13aaf148 347 if (!quiet)
dc6a0757 348 progress = start_progress("Unpacking objects", nr_objects);
209c554a 349 obj_list = xmalloc(nr_objects * sizeof(*obj_list));
96a02f8f
NP
350 for (i = 0; i < nr_objects; i++) {
351 unpack_one(i);
4d4fcc54 352 display_progress(progress, i + 1);
96a02f8f 353 }
4d4fcc54 354 stop_progress(&progress);
96a02f8f 355
67e5a5ec
LT
356 if (delta_list)
357 die("unresolved deltas left after unpacking");
74536958
LT
358}
359
64413630 360int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
bad50dc8
LT
361{
362 int i;
67e5a5ec 363 unsigned char sha1[20];
bad50dc8 364
8e273641 365 git_config(git_default_config);
53228a5f 366
476e8011
JH
367 quiet = !isatty(2);
368
bad50dc8
LT
369 for (i = 1 ; i < argc; i++) {
370 const char *arg = argv[i];
371
372 if (*arg == '-') {
74536958
LT
373 if (!strcmp(arg, "-n")) {
374 dry_run = 1;
375 continue;
376 }
d36f7b80
LT
377 if (!strcmp(arg, "-q")) {
378 quiet = 1;
379 continue;
380 }
f986f2c8 381 if (!strcmp(arg, "-r")) {
3b67d291 382 recover = 1;
f986f2c8
JH
383 continue;
384 }
cc44c765 385 if (!prefixcmp(arg, "--pack_header=")) {
bed006fb
NP
386 struct pack_header *hdr;
387 char *c;
388
389 hdr = (struct pack_header *)buffer;
390 hdr->hdr_signature = htonl(PACK_SIGNATURE);
391 hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));
392 if (*c != ',')
393 die("bad %s", arg);
394 hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));
395 if (*c)
396 die("bad %s", arg);
397 len = sizeof(*hdr);
398 continue;
399 }
bad50dc8
LT
400 usage(unpack_usage);
401 }
67e5a5ec
LT
402
403 /* We don't take any non-flag arguments now.. Maybe some day */
bad50dc8 404 usage(unpack_usage);
67e5a5ec
LT
405 }
406 SHA1_Init(&ctx);
74536958 407 unpack_all();
67e5a5ec
LT
408 SHA1_Update(&ctx, buffer, offset);
409 SHA1_Final(sha1, &ctx);
a89fccd2 410 if (hashcmp(fill(20), sha1))
67e5a5ec
LT
411 die("final sha1 did not match");
412 use(20);
413
414 /* Write the last part of the buffer to stdout */
415 while (len) {
1c15afb9
JH
416 int ret = xwrite(1, buffer + offset, len);
417 if (ret <= 0)
67e5a5ec 418 break;
67e5a5ec
LT
419 len -= ret;
420 offset += ret;
421 }
422
423 /* All done */
f986f2c8 424 return has_errors;
bad50dc8 425}