]>
Commit | Line | Data |
---|---|---|
64413630 | 1 | #include "builtin.h" |
bad50dc8 | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
74536958 | 4 | #include "object.h" |
8ee378a0 | 5 | #include "delta.h" |
a733cb60 | 6 | #include "pack.h" |
8e440259 PE |
7 | #include "blob.h" |
8 | #include "commit.h" | |
9 | #include "tag.h" | |
10 | #include "tree.h" | |
b41860bf | 11 | #include "tree-walk.h" |
96a02f8f | 12 | #include "progress.h" |
2add1e6d | 13 | #include "decorate.h" |
b41860bf | 14 | #include "fsck.h" |
bad50dc8 | 15 | |
b41860bf | 16 | static int dry_run, quiet, recover, has_errors, strict; |
33e8fc87 | 17 | static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]"; |
bad50dc8 | 18 | |
67e5a5ec LT |
19 | /* We always read in 4kB chunks. */ |
20 | static unsigned char buffer[4096]; | |
d7dd0223 NP |
21 | static unsigned int offset, len; |
22 | static off_t consumed_bytes; | |
5ad21867 | 23 | static off_t max_input_size; |
3206b6bd | 24 | static git_hash_ctx ctx; |
22410549 | 25 | static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; |
bad50dc8 | 26 | |
f2898cfa JH |
27 | /* |
28 | * When running under --strict mode, objects whose reachability are | |
29 | * suspect are kept in core without getting written in the object | |
30 | * store. | |
31 | */ | |
2add1e6d MK |
32 | struct obj_buffer { |
33 | char *buffer; | |
34 | unsigned long size; | |
35 | }; | |
36 | ||
37 | static struct decoration obj_decorate; | |
38 | ||
39 | static struct obj_buffer *lookup_object_buffer(struct object *base) | |
40 | { | |
41 | return lookup_decoration(&obj_decorate, base); | |
42 | } | |
43 | ||
b41860bf MK |
44 | static void add_object_buffer(struct object *object, char *buffer, unsigned long size) |
45 | { | |
46 | struct obj_buffer *obj; | |
47 | obj = xcalloc(1, sizeof(struct obj_buffer)); | |
48 | obj->buffer = buffer; | |
49 | obj->size = size; | |
50 | if (add_decoration(&obj_decorate, object, obj)) | |
f2fd0760 | 51 | die("object %s tried to add buffer twice!", oid_to_hex(&object->oid)); |
b41860bf MK |
52 | } |
53 | ||
67e5a5ec LT |
54 | /* |
55 | * Make sure at least "min" bytes are available in the buffer, and | |
56 | * return the pointer to the buffer. | |
57 | */ | |
79a65697 | 58 | static void *fill(int min) |
67e5a5ec LT |
59 | { |
60 | if (min <= len) | |
61 | return buffer + offset; | |
67e5a5ec LT |
62 | if (min > sizeof(buffer)) |
63 | die("cannot fill %d bytes", min); | |
64 | if (offset) { | |
3206b6bd | 65 | the_hash_algo->update_fn(&ctx, buffer, offset); |
79a65697 | 66 | memmove(buffer, buffer + offset, len); |
67e5a5ec LT |
67 | offset = 0; |
68 | } | |
69 | do { | |
8a912bcb | 70 | ssize_t ret = xread(0, buffer + len, sizeof(buffer) - len); |
67e5a5ec LT |
71 | if (ret <= 0) { |
72 | if (!ret) | |
73 | die("early EOF"); | |
d824cbba | 74 | die_errno("read error on input"); |
67e5a5ec LT |
75 | } |
76 | len += ret; | |
77 | } while (len < min); | |
78 | return buffer; | |
79 | } | |
74536958 | 80 | |
67e5a5ec LT |
81 | static void use(int bytes) |
82 | { | |
83 | if (bytes > len) | |
84 | die("used more bytes than were available"); | |
85 | len -= bytes; | |
86 | offset += bytes; | |
d7dd0223 NP |
87 | |
88 | /* make sure off_t is sufficiently large not to wrap */ | |
c03c8315 | 89 | if (signed_add_overflows(consumed_bytes, bytes)) |
d7dd0223 | 90 | die("pack too large for current definition of off_t"); |
209c554a | 91 | consumed_bytes += bytes; |
5ad21867 CC |
92 | if (max_input_size && consumed_bytes > max_input_size) |
93 | die(_("pack exceeds maximum allowed size")); | |
67e5a5ec | 94 | } |
bad50dc8 | 95 | |
67e5a5ec | 96 | static void *get_data(unsigned long size) |
bad50dc8 | 97 | { |
ef49a7a0 | 98 | git_zstream stream; |
a1e920a0 | 99 | void *buf = xmallocz(size); |
67e5a5ec | 100 | |
67e5a5ec LT |
101 | memset(&stream, 0, sizeof(stream)); |
102 | ||
103 | stream.next_out = buf; | |
104 | stream.avail_out = size; | |
105 | stream.next_in = fill(1); | |
106 | stream.avail_in = len; | |
39c68542 | 107 | git_inflate_init(&stream); |
67e5a5ec LT |
108 | |
109 | for (;;) { | |
39c68542 | 110 | int ret = git_inflate(&stream, 0); |
67e5a5ec LT |
111 | use(len - stream.avail_in); |
112 | if (stream.total_out == size && ret == Z_STREAM_END) | |
113 | break; | |
f986f2c8 | 114 | if (ret != Z_OK) { |
82247e9b | 115 | error("inflate returned %d", ret); |
6a83d902 | 116 | FREE_AND_NULL(buf); |
3b67d291 | 117 | if (!recover) |
f986f2c8 JH |
118 | exit(1); |
119 | has_errors = 1; | |
120 | break; | |
121 | } | |
67e5a5ec LT |
122 | stream.next_in = fill(1); |
123 | stream.avail_in = len; | |
124 | } | |
39c68542 | 125 | git_inflate_end(&stream); |
67e5a5ec | 126 | return buf; |
bad50dc8 LT |
127 | } |
128 | ||
67e5a5ec | 129 | struct delta_info { |
834bc47b | 130 | struct object_id base_oid; |
d7dd0223 NP |
131 | unsigned nr; |
132 | off_t base_offset; | |
67e5a5ec LT |
133 | unsigned long size; |
134 | void *delta; | |
135 | struct delta_info *next; | |
136 | }; | |
137 | ||
138 | static struct delta_info *delta_list; | |
139 | ||
834bc47b | 140 | static void add_delta_to_list(unsigned nr, const struct object_id *base_oid, |
d7dd0223 | 141 | off_t base_offset, |
209c554a | 142 | void *delta, unsigned long size) |
bad50dc8 | 143 | { |
67e5a5ec | 144 | struct delta_info *info = xmalloc(sizeof(*info)); |
bad50dc8 | 145 | |
834bc47b | 146 | oidcpy(&info->base_oid, base_oid); |
209c554a | 147 | info->base_offset = base_offset; |
67e5a5ec LT |
148 | info->size = size; |
149 | info->delta = delta; | |
209c554a | 150 | info->nr = nr; |
67e5a5ec LT |
151 | info->next = delta_list; |
152 | delta_list = info; | |
bad50dc8 LT |
153 | } |
154 | ||
209c554a | 155 | struct obj_info { |
d7dd0223 | 156 | off_t offset; |
834bc47b | 157 | struct object_id oid; |
b41860bf | 158 | struct object *obj; |
209c554a NP |
159 | }; |
160 | ||
95308d64 | 161 | /* Remember to update object flag allocation in object.h */ |
b41860bf MK |
162 | #define FLAG_OPEN (1u<<20) |
163 | #define FLAG_WRITTEN (1u<<21) | |
164 | ||
209c554a | 165 | static struct obj_info *obj_list; |
2af202be | 166 | static unsigned nr_objects; |
b41860bf | 167 | |
f2898cfa JH |
168 | /* |
169 | * Called only from check_object() after it verified this object | |
170 | * is Ok. | |
171 | */ | |
90a398bb | 172 | static void write_cached_object(struct object *obj, struct obj_buffer *obj_buf) |
b41860bf | 173 | { |
834bc47b | 174 | struct object_id oid; |
90a398bb | 175 | |
a09c985e | 176 | if (write_object_file(obj_buf->buffer, obj_buf->size, |
169c9c01 | 177 | type_name(obj->type), &oid) < 0) |
f2fd0760 | 178 | die("failed to write object %s", oid_to_hex(&obj->oid)); |
b41860bf MK |
179 | obj->flags |= FLAG_WRITTEN; |
180 | } | |
181 | ||
f2898cfa JH |
182 | /* |
183 | * At the very end of the processing, write_rest() scans the objects | |
184 | * that have reachability requirements and calls this function. | |
185 | * Verify its reachability and validity recursively and write it out. | |
186 | */ | |
22410549 | 187 | static int check_object(struct object *obj, int type, void *data, struct fsck_options *options) |
b41860bf | 188 | { |
90a398bb JS |
189 | struct obj_buffer *obj_buf; |
190 | ||
b41860bf | 191 | if (!obj) |
9a217391 | 192 | return 1; |
b41860bf MK |
193 | |
194 | if (obj->flags & FLAG_WRITTEN) | |
9a217391 | 195 | return 0; |
b41860bf MK |
196 | |
197 | if (type != OBJ_ANY && obj->type != type) | |
198 | die("object type mismatch"); | |
199 | ||
200 | if (!(obj->flags & FLAG_OPEN)) { | |
201 | unsigned long size; | |
0df8e965 | 202 | int type = oid_object_info(the_repository, &obj->oid, &size); |
b41860bf MK |
203 | if (type != obj->type || type <= 0) |
204 | die("object of unexpected type"); | |
205 | obj->flags |= FLAG_WRITTEN; | |
9a217391 | 206 | return 0; |
b41860bf MK |
207 | } |
208 | ||
90a398bb JS |
209 | obj_buf = lookup_object_buffer(obj); |
210 | if (!obj_buf) | |
f2fd0760 | 211 | die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid)); |
22410549 | 212 | if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options)) |
db5a58c1 | 213 | die("fsck error in packed object"); |
22410549 JS |
214 | fsck_options.walk = check_object; |
215 | if (fsck_walk(obj, NULL, &fsck_options)) | |
f2fd0760 | 216 | die("Error on reachable objects of %s", oid_to_hex(&obj->oid)); |
90a398bb | 217 | write_cached_object(obj, obj_buf); |
9a217391 | 218 | return 0; |
b41860bf MK |
219 | } |
220 | ||
221 | static void write_rest(void) | |
222 | { | |
223 | unsigned i; | |
9a217391 JH |
224 | for (i = 0; i < nr_objects; i++) { |
225 | if (obj_list[i].obj) | |
22410549 | 226 | check_object(obj_list[i].obj, OBJ_ANY, NULL, NULL); |
9a217391 | 227 | } |
b41860bf | 228 | } |
67e5a5ec | 229 | |
21666f1a NP |
230 | static void added_object(unsigned nr, enum object_type type, |
231 | void *data, unsigned long size); | |
209c554a | 232 | |
f2898cfa JH |
233 | /* |
234 | * Write out nr-th object from the list, now we know the contents | |
235 | * of it. Under --strict, this buffers structured objects in-core, | |
236 | * to be checked at the end. | |
237 | */ | |
21666f1a NP |
238 | static void write_object(unsigned nr, enum object_type type, |
239 | void *buf, unsigned long size) | |
cca7081a | 240 | { |
b41860bf | 241 | if (!strict) { |
169c9c01 | 242 | if (write_object_file(buf, size, type_name(type), |
a09c985e | 243 | &obj_list[nr].oid) < 0) |
b41860bf | 244 | die("failed to write object"); |
f2898cfa | 245 | added_object(nr, type, buf, size); |
b41860bf | 246 | free(buf); |
f2898cfa | 247 | obj_list[nr].obj = NULL; |
b41860bf MK |
248 | } else if (type == OBJ_BLOB) { |
249 | struct blob *blob; | |
169c9c01 | 250 | if (write_object_file(buf, size, type_name(type), |
a09c985e | 251 | &obj_list[nr].oid) < 0) |
b41860bf | 252 | die("failed to write object"); |
f2898cfa | 253 | added_object(nr, type, buf, size); |
b41860bf MK |
254 | free(buf); |
255 | ||
3aca1fc6 | 256 | blob = lookup_blob(&obj_list[nr].oid); |
b41860bf MK |
257 | if (blob) |
258 | blob->object.flags |= FLAG_WRITTEN; | |
259 | else | |
260 | die("invalid blob object"); | |
f2898cfa | 261 | obj_list[nr].obj = NULL; |
b41860bf MK |
262 | } else { |
263 | struct object *obj; | |
264 | int eaten; | |
169c9c01 | 265 | hash_object_file(buf, size, type_name(type), &obj_list[nr].oid); |
f2898cfa | 266 | added_object(nr, type, buf, size); |
c251c83d | 267 | obj = parse_object_buffer(&obj_list[nr].oid, type, size, buf, |
268 | &eaten); | |
b41860bf | 269 | if (!obj) |
debca9d2 | 270 | die("invalid %s", type_name(type)); |
b41860bf MK |
271 | add_object_buffer(obj, buf, size); |
272 | obj->flags |= FLAG_OPEN; | |
273 | obj_list[nr].obj = obj; | |
274 | } | |
cca7081a LT |
275 | } |
276 | ||
21666f1a | 277 | static void resolve_delta(unsigned nr, enum object_type type, |
f986f2c8 JH |
278 | void *base, unsigned long base_size, |
279 | void *delta, unsigned long delta_size) | |
bad50dc8 | 280 | { |
67e5a5ec LT |
281 | void *result; |
282 | unsigned long result_size; | |
bad50dc8 | 283 | |
67e5a5ec LT |
284 | result = patch_delta(base, base_size, |
285 | delta, delta_size, | |
286 | &result_size); | |
287 | if (!result) | |
288 | die("failed to apply delta"); | |
289 | free(delta); | |
21666f1a | 290 | write_object(nr, type, result, result_size); |
bad50dc8 LT |
291 | } |
292 | ||
f2898cfa JH |
293 | /* |
294 | * We now know the contents of an object (which is nr-th in the pack); | |
295 | * resolve all the deltified objects that are based on it. | |
296 | */ | |
21666f1a NP |
297 | static void added_object(unsigned nr, enum object_type type, |
298 | void *data, unsigned long size) | |
74536958 | 299 | { |
67e5a5ec LT |
300 | struct delta_info **p = &delta_list; |
301 | struct delta_info *info; | |
302 | ||
303 | while ((info = *p) != NULL) { | |
834bc47b | 304 | if (!oidcmp(&info->base_oid, &obj_list[nr].oid) || |
209c554a | 305 | info->base_offset == obj_list[nr].offset) { |
67e5a5ec LT |
306 | *p = info->next; |
307 | p = &delta_list; | |
209c554a NP |
308 | resolve_delta(info->nr, type, data, size, |
309 | info->delta, info->size); | |
67e5a5ec LT |
310 | free(info); |
311 | continue; | |
312 | } | |
313 | p = &info->next; | |
314 | } | |
315 | } | |
316 | ||
21666f1a | 317 | static void unpack_non_delta_entry(enum object_type type, unsigned long size, |
209c554a | 318 | unsigned nr) |
67e5a5ec LT |
319 | { |
320 | void *buf = get_data(size); | |
8ee378a0 | 321 | |
f986f2c8 | 322 | if (!dry_run && buf) |
21666f1a | 323 | write_object(nr, type, buf, size); |
b41860bf MK |
324 | else |
325 | free(buf); | |
8ee378a0 JH |
326 | } |
327 | ||
834bc47b | 328 | static int resolve_against_held(unsigned nr, const struct object_id *base, |
f2898cfa JH |
329 | void *delta_data, unsigned long delta_size) |
330 | { | |
331 | struct object *obj; | |
332 | struct obj_buffer *obj_buffer; | |
834bc47b | 333 | obj = lookup_object(base->hash); |
f2898cfa JH |
334 | if (!obj) |
335 | return 0; | |
336 | obj_buffer = lookup_object_buffer(obj); | |
337 | if (!obj_buffer) | |
338 | return 0; | |
339 | resolve_delta(nr, obj->type, obj_buffer->buffer, | |
340 | obj_buffer->size, delta_data, delta_size); | |
341 | return 1; | |
342 | } | |
343 | ||
21666f1a | 344 | static void unpack_delta_entry(enum object_type type, unsigned long delta_size, |
209c554a | 345 | unsigned nr) |
8ee378a0 | 346 | { |
67e5a5ec LT |
347 | void *delta_data, *base; |
348 | unsigned long base_size; | |
834bc47b | 349 | struct object_id base_oid; |
8ee378a0 | 350 | |
21666f1a | 351 | if (type == OBJ_REF_DELTA) { |
3206b6bd | 352 | hashcpy(base_oid.hash, fill(the_hash_algo->rawsz)); |
353 | use(the_hash_algo->rawsz); | |
209c554a NP |
354 | delta_data = get_data(delta_size); |
355 | if (dry_run || !delta_data) { | |
356 | free(delta_data); | |
357 | return; | |
358 | } | |
834bc47b | 359 | if (has_object_file(&base_oid)) |
f2898cfa | 360 | ; /* Ok we have this one */ |
834bc47b | 361 | else if (resolve_against_held(nr, &base_oid, |
f2898cfa JH |
362 | delta_data, delta_size)) |
363 | return; /* we are done */ | |
364 | else { | |
365 | /* cannot resolve yet --- queue it */ | |
834bc47b | 366 | oidclr(&obj_list[nr].oid); |
367 | add_delta_to_list(nr, &base_oid, 0, delta_data, delta_size); | |
209c554a NP |
368 | return; |
369 | } | |
370 | } else { | |
371 | unsigned base_found = 0; | |
372 | unsigned char *pack, c; | |
d7dd0223 | 373 | off_t base_offset; |
209c554a | 374 | unsigned lo, mid, hi; |
c4fb06c0 | 375 | |
209c554a NP |
376 | pack = fill(1); |
377 | c = *pack; | |
378 | use(1); | |
379 | base_offset = c & 127; | |
380 | while (c & 128) { | |
381 | base_offset += 1; | |
8723f216 | 382 | if (!base_offset || MSB(base_offset, 7)) |
209c554a NP |
383 | die("offset value overflow for delta base object"); |
384 | pack = fill(1); | |
385 | c = *pack; | |
386 | use(1); | |
387 | base_offset = (base_offset << 7) + (c & 127); | |
388 | } | |
389 | base_offset = obj_list[nr].offset - base_offset; | |
d8f32556 NP |
390 | if (base_offset <= 0 || base_offset >= obj_list[nr].offset) |
391 | die("offset value out of bound for delta base object"); | |
8ee378a0 | 392 | |
209c554a NP |
393 | delta_data = get_data(delta_size); |
394 | if (dry_run || !delta_data) { | |
395 | free(delta_data); | |
396 | return; | |
397 | } | |
398 | lo = 0; | |
399 | hi = nr; | |
400 | while (lo < hi) { | |
19716b21 | 401 | mid = lo + (hi - lo) / 2; |
209c554a NP |
402 | if (base_offset < obj_list[mid].offset) { |
403 | hi = mid; | |
404 | } else if (base_offset > obj_list[mid].offset) { | |
405 | lo = mid + 1; | |
406 | } else { | |
834bc47b | 407 | oidcpy(&base_oid, &obj_list[mid].oid); |
408 | base_found = !is_null_oid(&base_oid); | |
209c554a NP |
409 | break; |
410 | } | |
411 | } | |
412 | if (!base_found) { | |
f2898cfa JH |
413 | /* |
414 | * The delta base object is itself a delta that | |
415 | * has not been resolved yet. | |
416 | */ | |
834bc47b | 417 | oidclr(&obj_list[nr].oid); |
418 | add_delta_to_list(nr, &null_oid, base_offset, delta_data, delta_size); | |
209c554a | 419 | return; |
2add1e6d MK |
420 | } |
421 | } | |
422 | ||
834bc47b | 423 | if (resolve_against_held(nr, &base_oid, delta_data, delta_size)) |
f2898cfa | 424 | return; |
209c554a | 425 | |
b4f5aca4 | 426 | base = read_object_file(&base_oid, &type, &base_size); |
f986f2c8 JH |
427 | if (!base) { |
428 | error("failed to read delta-pack base object %s", | |
834bc47b | 429 | oid_to_hex(&base_oid)); |
3b67d291 | 430 | if (!recover) |
f986f2c8 JH |
431 | exit(1); |
432 | has_errors = 1; | |
433 | return; | |
434 | } | |
209c554a | 435 | resolve_delta(nr, type, base, base_size, delta_data, delta_size); |
ee639140 | 436 | free(base); |
8ee378a0 JH |
437 | } |
438 | ||
96a02f8f | 439 | static void unpack_one(unsigned nr) |
8ee378a0 | 440 | { |
01247d87 | 441 | unsigned shift; |
48fb7deb LT |
442 | unsigned char *pack; |
443 | unsigned long size, c; | |
67e5a5ec | 444 | enum object_type type; |
74536958 | 445 | |
209c554a NP |
446 | obj_list[nr].offset = consumed_bytes; |
447 | ||
67e5a5ec LT |
448 | pack = fill(1); |
449 | c = *pack; | |
450 | use(1); | |
a733cb60 LT |
451 | type = (c >> 4) & 7; |
452 | size = (c & 15); | |
01247d87 | 453 | shift = 4; |
a733cb60 | 454 | while (c & 0x80) { |
67e5a5ec | 455 | pack = fill(1); |
209c554a | 456 | c = *pack; |
67e5a5ec | 457 | use(1); |
01247d87 LT |
458 | size += (c & 0x7f) << shift; |
459 | shift += 7; | |
a733cb60 | 460 | } |
96a02f8f | 461 | |
a733cb60 LT |
462 | switch (type) { |
463 | case OBJ_COMMIT: | |
464 | case OBJ_TREE: | |
465 | case OBJ_BLOB: | |
466 | case OBJ_TAG: | |
209c554a | 467 | unpack_non_delta_entry(type, size, nr); |
a733cb60 | 468 | return; |
eb32d236 | 469 | case OBJ_REF_DELTA: |
209c554a NP |
470 | case OBJ_OFS_DELTA: |
471 | unpack_delta_entry(type, size, nr); | |
a733cb60 | 472 | return; |
67e5a5ec | 473 | default: |
f986f2c8 JH |
474 | error("bad object type %d", type); |
475 | has_errors = 1; | |
3b67d291 | 476 | if (recover) |
f986f2c8 JH |
477 | return; |
478 | exit(1); | |
74536958 LT |
479 | } |
480 | } | |
481 | ||
74536958 LT |
482 | static void unpack_all(void) |
483 | { | |
67e5a5ec | 484 | int i; |
dc6a0757 | 485 | struct progress *progress = NULL; |
67e5a5ec | 486 | struct pack_header *hdr = fill(sizeof(struct pack_header)); |
b41860bf MK |
487 | |
488 | nr_objects = ntohl(hdr->hdr_entries); | |
67e5a5ec LT |
489 | |
490 | if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE) | |
491 | die("bad pack file"); | |
d60fc1c8 | 492 | if (!pack_version_ok(hdr->hdr_version)) |
6e1c2344 RJ |
493 | die("unknown pack file version %"PRIu32, |
494 | ntohl(hdr->hdr_version)); | |
96a02f8f | 495 | use(sizeof(struct pack_header)); |
67e5a5ec | 496 | |
13aaf148 | 497 | if (!quiet) |
754dbc43 | 498 | progress = start_progress(_("Unpacking objects"), nr_objects); |
19d4b416 | 499 | obj_list = xcalloc(nr_objects, sizeof(*obj_list)); |
96a02f8f NP |
500 | for (i = 0; i < nr_objects; i++) { |
501 | unpack_one(i); | |
4d4fcc54 | 502 | display_progress(progress, i + 1); |
96a02f8f | 503 | } |
4d4fcc54 | 504 | stop_progress(&progress); |
96a02f8f | 505 | |
67e5a5ec LT |
506 | if (delta_list) |
507 | die("unresolved deltas left after unpacking"); | |
74536958 LT |
508 | } |
509 | ||
64413630 | 510 | int cmd_unpack_objects(int argc, const char **argv, const char *prefix) |
bad50dc8 LT |
511 | { |
512 | int i; | |
834bc47b | 513 | struct object_id oid; |
bad50dc8 | 514 | |
afc711b8 | 515 | check_replace_refs = 0; |
dae556bd | 516 | |
ef90d6d4 | 517 | git_config(git_default_config, NULL); |
53228a5f | 518 | |
476e8011 JH |
519 | quiet = !isatty(2); |
520 | ||
bad50dc8 LT |
521 | for (i = 1 ; i < argc; i++) { |
522 | const char *arg = argv[i]; | |
523 | ||
524 | if (*arg == '-') { | |
74536958 LT |
525 | if (!strcmp(arg, "-n")) { |
526 | dry_run = 1; | |
527 | continue; | |
528 | } | |
d36f7b80 LT |
529 | if (!strcmp(arg, "-q")) { |
530 | quiet = 1; | |
531 | continue; | |
532 | } | |
f986f2c8 | 533 | if (!strcmp(arg, "-r")) { |
3b67d291 | 534 | recover = 1; |
f986f2c8 JH |
535 | continue; |
536 | } | |
b41860bf MK |
537 | if (!strcmp(arg, "--strict")) { |
538 | strict = 1; | |
539 | continue; | |
540 | } | |
5d477a33 JS |
541 | if (skip_prefix(arg, "--strict=", &arg)) { |
542 | strict = 1; | |
543 | fsck_set_msg_types(&fsck_options, arg); | |
544 | continue; | |
545 | } | |
59556548 | 546 | if (starts_with(arg, "--pack_header=")) { |
bed006fb NP |
547 | struct pack_header *hdr; |
548 | char *c; | |
549 | ||
550 | hdr = (struct pack_header *)buffer; | |
551 | hdr->hdr_signature = htonl(PACK_SIGNATURE); | |
552 | hdr->hdr_version = htonl(strtoul(arg + 14, &c, 10)); | |
553 | if (*c != ',') | |
554 | die("bad %s", arg); | |
555 | hdr->hdr_entries = htonl(strtoul(c + 1, &c, 10)); | |
556 | if (*c) | |
557 | die("bad %s", arg); | |
558 | len = sizeof(*hdr); | |
559 | continue; | |
560 | } | |
5ad21867 CC |
561 | if (skip_prefix(arg, "--max-input-size=", &arg)) { |
562 | max_input_size = strtoumax(arg, NULL, 10); | |
563 | continue; | |
564 | } | |
bad50dc8 LT |
565 | usage(unpack_usage); |
566 | } | |
67e5a5ec LT |
567 | |
568 | /* We don't take any non-flag arguments now.. Maybe some day */ | |
bad50dc8 | 569 | usage(unpack_usage); |
67e5a5ec | 570 | } |
3206b6bd | 571 | the_hash_algo->init_fn(&ctx); |
74536958 | 572 | unpack_all(); |
3206b6bd | 573 | the_hash_algo->update_fn(&ctx, buffer, offset); |
574 | the_hash_algo->final_fn(oid.hash, &ctx); | |
6e328d6c | 575 | if (strict) { |
b41860bf | 576 | write_rest(); |
6e328d6c JK |
577 | if (fsck_finish(&fsck_options)) |
578 | die(_("fsck error in pack objects")); | |
579 | } | |
3206b6bd | 580 | if (hashcmp(fill(the_hash_algo->rawsz), oid.hash)) |
67e5a5ec | 581 | die("final sha1 did not match"); |
3206b6bd | 582 | use(the_hash_algo->rawsz); |
67e5a5ec LT |
583 | |
584 | /* Write the last part of the buffer to stdout */ | |
585 | while (len) { | |
1c15afb9 JH |
586 | int ret = xwrite(1, buffer + offset, len); |
587 | if (ret <= 0) | |
67e5a5ec | 588 | break; |
67e5a5ec LT |
589 | len -= ret; |
590 | offset += ret; | |
591 | } | |
592 | ||
593 | /* All done */ | |
f986f2c8 | 594 | return has_errors; |
bad50dc8 | 595 | } |