1#include "builtin.h" 2#include "cache.h" 3#include "repository.h" 4#include "config.h" 5#include "attr.h" 6#include "object.h" 7#include "blob.h" 8#include "commit.h" 9#include "tag.h" 10#include "tree.h" 11#include "delta.h" 12#include "pack.h" 13#include "pack-revindex.h" 14#include "csum-file.h" 15#include "tree-walk.h" 16#include "diff.h" 17#include "revision.h" 18#include "list-objects.h" 19#include "list-objects-filter.h" 20#include "list-objects-filter-options.h" 21#include "pack-objects.h" 22#include "progress.h" 23#include "refs.h" 24#include "streaming.h" 25#include "thread-utils.h" 26#include "pack-bitmap.h" 27#include "reachable.h" 28#include "sha1-array.h" 29#include "argv-array.h" 30#include "list.h" 31#include "packfile.h" 32#include "object-store.h" 33 34static const char *pack_usage[] = { 35 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 36 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 37 NULL 38}; 39 40/* 41 * Objects we are going to pack are collected in the `to_pack` structure. 42 * It contains an array (dynamically expanded) of the object data, and a map 43 * that can resolve SHA1s to their position in the array. 44 */ 45static struct packing_data to_pack; 46 47static struct pack_idx_entry **written_list; 48static uint32_t nr_result, nr_written; 49 50static int non_empty; 51static int reuse_delta = 1, reuse_object = 1; 52static int keep_unreachable, unpack_unreachable, include_tag; 53static timestamp_t unpack_unreachable_expiration; 54static int pack_loose_unreachable; 55static int local; 56static int have_non_local_packs; 57static int incremental; 58static int ignore_packed_keep; 59static int allow_ofs_delta; 60static struct pack_idx_option pack_idx_opts; 61static const char *base_name; 62static int progress = 1; 63static int window = 10; 64static unsigned long pack_size_limit; 65static int depth = 50; 66static int delta_search_threads; 67static int pack_to_stdout; 68static int num_preferred_base; 69static struct progress *progress_state; 70 71static struct packed_git *reuse_packfile; 72static uint32_t reuse_packfile_objects; 73static off_t reuse_packfile_offset; 74 75static int use_bitmap_index_default = 1; 76static int use_bitmap_index = -1; 77static int write_bitmap_index; 78static uint16_t write_bitmap_options; 79 80static int exclude_promisor_objects; 81 82static unsigned long delta_cache_size = 0; 83static unsigned long max_delta_cache_size = 256 * 1024 * 1024; 84static unsigned long cache_max_small_delta_size = 1000; 85 86static unsigned long window_memory_limit = 0; 87 88static struct list_objects_filter_options filter_options; 89 90enum missing_action { 91 MA_ERROR = 0, /* fail if any missing objects are encountered */ 92 MA_ALLOW_ANY, /* silently allow ALL missing objects */ 93 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */ 94}; 95static enum missing_action arg_missing_action; 96static show_object_fn fn_show_object; 97 98/* 99 * stats 100 */ 101static uint32_t written, written_delta; 102static uint32_t reused, reused_delta; 103 104/* 105 * Indexed commits 106 */ 107static struct commit **indexed_commits; 108static unsigned int indexed_commits_nr; 109static unsigned int indexed_commits_alloc; 110 111static void index_commit_for_bitmap(struct commit *commit) 112{ 113 if (indexed_commits_nr >= indexed_commits_alloc) { 114 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2; 115 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 116 } 117 118 indexed_commits[indexed_commits_nr++] = commit; 119} 120 121static void *get_delta(struct object_entry *entry) 122{ 123 unsigned long size, base_size, delta_size; 124 void *buf, *base_buf, *delta_buf; 125 enum object_type type; 126 127 buf = read_object_file(&entry->idx.oid, &type, &size); 128 if (!buf) 129 die("unable to read %s", oid_to_hex(&entry->idx.oid)); 130 base_buf = read_object_file(&entry->delta->idx.oid, &type, &base_size); 131 if (!base_buf) 132 die("unable to read %s", 133 oid_to_hex(&entry->delta->idx.oid)); 134 delta_buf = diff_delta(base_buf, base_size, 135 buf, size, &delta_size, 0); 136 if (!delta_buf || delta_size != entry->delta_size) 137 die("delta size changed"); 138 free(buf); 139 free(base_buf); 140 return delta_buf; 141} 142 143static unsigned long do_compress(void **pptr, unsigned long size) 144{ 145 git_zstream stream; 146 void *in, *out; 147 unsigned long maxsize; 148 149 git_deflate_init(&stream, pack_compression_level); 150 maxsize = git_deflate_bound(&stream, size); 151 152 in = *pptr; 153 out = xmalloc(maxsize); 154 *pptr = out; 155 156 stream.next_in = in; 157 stream.avail_in = size; 158 stream.next_out = out; 159 stream.avail_out = maxsize; 160 while (git_deflate(&stream, Z_FINISH) == Z_OK) 161 ; /* nothing */ 162 git_deflate_end(&stream); 163 164 free(in); 165 return stream.total_out; 166} 167 168static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f, 169 const struct object_id *oid) 170{ 171 git_zstream stream; 172 unsigned char ibuf[1024 * 16]; 173 unsigned char obuf[1024 * 16]; 174 unsigned long olen = 0; 175 176 git_deflate_init(&stream, pack_compression_level); 177 178 for (;;) { 179 ssize_t readlen; 180 int zret = Z_OK; 181 readlen = read_istream(st, ibuf, sizeof(ibuf)); 182 if (readlen == -1) 183 die(_("unable to read %s"), oid_to_hex(oid)); 184 185 stream.next_in = ibuf; 186 stream.avail_in = readlen; 187 while ((stream.avail_in || readlen == 0) && 188 (zret == Z_OK || zret == Z_BUF_ERROR)) { 189 stream.next_out = obuf; 190 stream.avail_out = sizeof(obuf); 191 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH); 192 hashwrite(f, obuf, stream.next_out - obuf); 193 olen += stream.next_out - obuf; 194 } 195 if (stream.avail_in) 196 die(_("deflate error (%d)"), zret); 197 if (readlen == 0) { 198 if (zret != Z_STREAM_END) 199 die(_("deflate error (%d)"), zret); 200 break; 201 } 202 } 203 git_deflate_end(&stream); 204 return olen; 205} 206 207/* 208 * we are going to reuse the existing object data as is. make 209 * sure it is not corrupt. 210 */ 211static int check_pack_inflate(struct packed_git *p, 212 struct pack_window **w_curs, 213 off_t offset, 214 off_t len, 215 unsigned long expect) 216{ 217 git_zstream stream; 218 unsigned char fakebuf[4096], *in; 219 int st; 220 221 memset(&stream, 0, sizeof(stream)); 222 git_inflate_init(&stream); 223 do { 224 in = use_pack(p, w_curs, offset, &stream.avail_in); 225 stream.next_in = in; 226 stream.next_out = fakebuf; 227 stream.avail_out = sizeof(fakebuf); 228 st = git_inflate(&stream, Z_FINISH); 229 offset += stream.next_in - in; 230 } while (st == Z_OK || st == Z_BUF_ERROR); 231 git_inflate_end(&stream); 232 return (st == Z_STREAM_END && 233 stream.total_out == expect && 234 stream.total_in == len) ? 0 : -1; 235} 236 237static void copy_pack_data(struct hashfile *f, 238 struct packed_git *p, 239 struct pack_window **w_curs, 240 off_t offset, 241 off_t len) 242{ 243 unsigned char *in; 244 unsigned long avail; 245 246 while (len) { 247 in = use_pack(p, w_curs, offset, &avail); 248 if (avail > len) 249 avail = (unsigned long)len; 250 hashwrite(f, in, avail); 251 offset += avail; 252 len -= avail; 253 } 254} 255 256/* Return 0 if we will bust the pack-size limit */ 257static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry, 258 unsigned long limit, int usable_delta) 259{ 260 unsigned long size, datalen; 261 unsigned char header[MAX_PACK_OBJECT_HEADER], 262 dheader[MAX_PACK_OBJECT_HEADER]; 263 unsigned hdrlen; 264 enum object_type type; 265 void *buf; 266 struct git_istream *st = NULL; 267 268 if (!usable_delta) { 269 if (entry->type == OBJ_BLOB && 270 entry->size > big_file_threshold && 271 (st = open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 272 buf = NULL; 273 else { 274 buf = read_object_file(&entry->idx.oid, &type, &size); 275 if (!buf) 276 die(_("unable to read %s"), 277 oid_to_hex(&entry->idx.oid)); 278 } 279 /* 280 * make sure no cached delta data remains from a 281 * previous attempt before a pack split occurred. 282 */ 283 FREE_AND_NULL(entry->delta_data); 284 entry->z_delta_size = 0; 285 } else if (entry->delta_data) { 286 size = entry->delta_size; 287 buf = entry->delta_data; 288 entry->delta_data = NULL; 289 type = (allow_ofs_delta && entry->delta->idx.offset) ? 290 OBJ_OFS_DELTA : OBJ_REF_DELTA; 291 } else { 292 buf = get_delta(entry); 293 size = entry->delta_size; 294 type = (allow_ofs_delta && entry->delta->idx.offset) ? 295 OBJ_OFS_DELTA : OBJ_REF_DELTA; 296 } 297 298 if (st) /* large blob case, just assume we don't compress well */ 299 datalen = size; 300 else if (entry->z_delta_size) 301 datalen = entry->z_delta_size; 302 else 303 datalen = do_compress(&buf, size); 304 305 /* 306 * The object header is a byte of 'type' followed by zero or 307 * more bytes of length. 308 */ 309 hdrlen = encode_in_pack_object_header(header, sizeof(header), 310 type, size); 311 312 if (type == OBJ_OFS_DELTA) { 313 /* 314 * Deltas with relative base contain an additional 315 * encoding of the relative offset for the delta 316 * base from this object's position in the pack. 317 */ 318 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 319 unsigned pos = sizeof(dheader) - 1; 320 dheader[pos] = ofs & 127; 321 while (ofs >>= 7) 322 dheader[--pos] = 128 | (--ofs & 127); 323 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { 324 if (st) 325 close_istream(st); 326 free(buf); 327 return 0; 328 } 329 hashwrite(f, header, hdrlen); 330 hashwrite(f, dheader + pos, sizeof(dheader) - pos); 331 hdrlen += sizeof(dheader) - pos; 332 } else if (type == OBJ_REF_DELTA) { 333 /* 334 * Deltas with a base reference contain 335 * an additional 20 bytes for the base sha1. 336 */ 337 if (limit && hdrlen + 20 + datalen + 20 >= limit) { 338 if (st) 339 close_istream(st); 340 free(buf); 341 return 0; 342 } 343 hashwrite(f, header, hdrlen); 344 hashwrite(f, entry->delta->idx.oid.hash, 20); 345 hdrlen += 20; 346 } else { 347 if (limit && hdrlen + datalen + 20 >= limit) { 348 if (st) 349 close_istream(st); 350 free(buf); 351 return 0; 352 } 353 hashwrite(f, header, hdrlen); 354 } 355 if (st) { 356 datalen = write_large_blob_data(st, f, &entry->idx.oid); 357 close_istream(st); 358 } else { 359 hashwrite(f, buf, datalen); 360 free(buf); 361 } 362 363 return hdrlen + datalen; 364} 365 366/* Return 0 if we will bust the pack-size limit */ 367static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry, 368 unsigned long limit, int usable_delta) 369{ 370 struct packed_git *p = entry->in_pack; 371 struct pack_window *w_curs = NULL; 372 struct revindex_entry *revidx; 373 off_t offset; 374 enum object_type type = entry->type; 375 off_t datalen; 376 unsigned char header[MAX_PACK_OBJECT_HEADER], 377 dheader[MAX_PACK_OBJECT_HEADER]; 378 unsigned hdrlen; 379 380 if (entry->delta) 381 type = (allow_ofs_delta && entry->delta->idx.offset) ? 382 OBJ_OFS_DELTA : OBJ_REF_DELTA; 383 hdrlen = encode_in_pack_object_header(header, sizeof(header), 384 type, entry->size); 385 386 offset = entry->in_pack_offset; 387 revidx = find_pack_revindex(p, offset); 388 datalen = revidx[1].offset - offset; 389 if (!pack_to_stdout && p->index_version > 1 && 390 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 391 error("bad packed object CRC for %s", 392 oid_to_hex(&entry->idx.oid)); 393 unuse_pack(&w_curs); 394 return write_no_reuse_object(f, entry, limit, usable_delta); 395 } 396 397 offset += entry->in_pack_header_size; 398 datalen -= entry->in_pack_header_size; 399 400 if (!pack_to_stdout && p->index_version == 1 && 401 check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) { 402 error("corrupt packed object for %s", 403 oid_to_hex(&entry->idx.oid)); 404 unuse_pack(&w_curs); 405 return write_no_reuse_object(f, entry, limit, usable_delta); 406 } 407 408 if (type == OBJ_OFS_DELTA) { 409 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 410 unsigned pos = sizeof(dheader) - 1; 411 dheader[pos] = ofs & 127; 412 while (ofs >>= 7) 413 dheader[--pos] = 128 | (--ofs & 127); 414 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { 415 unuse_pack(&w_curs); 416 return 0; 417 } 418 hashwrite(f, header, hdrlen); 419 hashwrite(f, dheader + pos, sizeof(dheader) - pos); 420 hdrlen += sizeof(dheader) - pos; 421 reused_delta++; 422 } else if (type == OBJ_REF_DELTA) { 423 if (limit && hdrlen + 20 + datalen + 20 >= limit) { 424 unuse_pack(&w_curs); 425 return 0; 426 } 427 hashwrite(f, header, hdrlen); 428 hashwrite(f, entry->delta->idx.oid.hash, 20); 429 hdrlen += 20; 430 reused_delta++; 431 } else { 432 if (limit && hdrlen + datalen + 20 >= limit) { 433 unuse_pack(&w_curs); 434 return 0; 435 } 436 hashwrite(f, header, hdrlen); 437 } 438 copy_pack_data(f, p, &w_curs, offset, datalen); 439 unuse_pack(&w_curs); 440 reused++; 441 return hdrlen + datalen; 442} 443 444/* Return 0 if we will bust the pack-size limit */ 445static off_t write_object(struct hashfile *f, 446 struct object_entry *entry, 447 off_t write_offset) 448{ 449 unsigned long limit; 450 off_t len; 451 int usable_delta, to_reuse; 452 453 if (!pack_to_stdout) 454 crc32_begin(f); 455 456 /* apply size limit if limited packsize and not first object */ 457 if (!pack_size_limit || !nr_written) 458 limit = 0; 459 else if (pack_size_limit <= write_offset) 460 /* 461 * the earlier object did not fit the limit; avoid 462 * mistaking this with unlimited (i.e. limit = 0). 463 */ 464 limit = 1; 465 else 466 limit = pack_size_limit - write_offset; 467 468 if (!entry->delta) 469 usable_delta = 0; /* no delta */ 470 else if (!pack_size_limit) 471 usable_delta = 1; /* unlimited packfile */ 472 else if (entry->delta->idx.offset == (off_t)-1) 473 usable_delta = 0; /* base was written to another pack */ 474 else if (entry->delta->idx.offset) 475 usable_delta = 1; /* base already exists in this pack */ 476 else 477 usable_delta = 0; /* base could end up in another pack */ 478 479 if (!reuse_object) 480 to_reuse = 0; /* explicit */ 481 else if (!entry->in_pack) 482 to_reuse = 0; /* can't reuse what we don't have */ 483 else if (entry->type == OBJ_REF_DELTA || entry->type == OBJ_OFS_DELTA) 484 /* check_object() decided it for us ... */ 485 to_reuse = usable_delta; 486 /* ... but pack split may override that */ 487 else if (entry->type != entry->in_pack_type) 488 to_reuse = 0; /* pack has delta which is unusable */ 489 else if (entry->delta) 490 to_reuse = 0; /* we want to pack afresh */ 491 else 492 to_reuse = 1; /* we have it in-pack undeltified, 493 * and we do not need to deltify it. 494 */ 495 496 if (!to_reuse) 497 len = write_no_reuse_object(f, entry, limit, usable_delta); 498 else 499 len = write_reuse_object(f, entry, limit, usable_delta); 500 if (!len) 501 return 0; 502 503 if (usable_delta) 504 written_delta++; 505 written++; 506 if (!pack_to_stdout) 507 entry->idx.crc32 = crc32_end(f); 508 return len; 509} 510 511enum write_one_status { 512 WRITE_ONE_SKIP = -1, /* already written */ 513 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ 514 WRITE_ONE_WRITTEN = 1, /* normal */ 515 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ 516}; 517 518static enum write_one_status write_one(struct hashfile *f, 519 struct object_entry *e, 520 off_t *offset) 521{ 522 off_t size; 523 int recursing; 524 525 /* 526 * we set offset to 1 (which is an impossible value) to mark 527 * the fact that this object is involved in "write its base 528 * first before writing a deltified object" recursion. 529 */ 530 recursing = (e->idx.offset == 1); 531 if (recursing) { 532 warning("recursive delta detected for object %s", 533 oid_to_hex(&e->idx.oid)); 534 return WRITE_ONE_RECURSIVE; 535 } else if (e->idx.offset || e->preferred_base) { 536 /* offset is non zero if object is written already. */ 537 return WRITE_ONE_SKIP; 538 } 539 540 /* if we are deltified, write out base object first. */ 541 if (e->delta) { 542 e->idx.offset = 1; /* now recurse */ 543 switch (write_one(f, e->delta, offset)) { 544 case WRITE_ONE_RECURSIVE: 545 /* we cannot depend on this one */ 546 e->delta = NULL; 547 break; 548 default: 549 break; 550 case WRITE_ONE_BREAK: 551 e->idx.offset = recursing; 552 return WRITE_ONE_BREAK; 553 } 554 } 555 556 e->idx.offset = *offset; 557 size = write_object(f, e, *offset); 558 if (!size) { 559 e->idx.offset = recursing; 560 return WRITE_ONE_BREAK; 561 } 562 written_list[nr_written++] = &e->idx; 563 564 /* make sure off_t is sufficiently large not to wrap */ 565 if (signed_add_overflows(*offset, size)) 566 die("pack too large for current definition of off_t"); 567 *offset += size; 568 return WRITE_ONE_WRITTEN; 569} 570 571static int mark_tagged(const char *path, const struct object_id *oid, int flag, 572 void *cb_data) 573{ 574 struct object_id peeled; 575 struct object_entry *entry = packlist_find(&to_pack, oid->hash, NULL); 576 577 if (entry) 578 entry->tagged = 1; 579 if (!peel_ref(path, &peeled)) { 580 entry = packlist_find(&to_pack, peeled.hash, NULL); 581 if (entry) 582 entry->tagged = 1; 583 } 584 return 0; 585} 586 587static inline void add_to_write_order(struct object_entry **wo, 588 unsigned int *endp, 589 struct object_entry *e) 590{ 591 if (e->filled) 592 return; 593 wo[(*endp)++] = e; 594 e->filled = 1; 595} 596 597static void add_descendants_to_write_order(struct object_entry **wo, 598 unsigned int *endp, 599 struct object_entry *e) 600{ 601 int add_to_order = 1; 602 while (e) { 603 if (add_to_order) { 604 struct object_entry *s; 605 /* add this node... */ 606 add_to_write_order(wo, endp, e); 607 /* all its siblings... */ 608 for (s = e->delta_sibling; s; s = s->delta_sibling) { 609 add_to_write_order(wo, endp, s); 610 } 611 } 612 /* drop down a level to add left subtree nodes if possible */ 613 if (e->delta_child) { 614 add_to_order = 1; 615 e = e->delta_child; 616 } else { 617 add_to_order = 0; 618 /* our sibling might have some children, it is next */ 619 if (e->delta_sibling) { 620 e = e->delta_sibling; 621 continue; 622 } 623 /* go back to our parent node */ 624 e = e->delta; 625 while (e && !e->delta_sibling) { 626 /* we're on the right side of a subtree, keep 627 * going up until we can go right again */ 628 e = e->delta; 629 } 630 if (!e) { 631 /* done- we hit our original root node */ 632 return; 633 } 634 /* pass it off to sibling at this level */ 635 e = e->delta_sibling; 636 } 637 }; 638} 639 640static void add_family_to_write_order(struct object_entry **wo, 641 unsigned int *endp, 642 struct object_entry *e) 643{ 644 struct object_entry *root; 645 646 for (root = e; root->delta; root = root->delta) 647 ; /* nothing */ 648 add_descendants_to_write_order(wo, endp, root); 649} 650 651static struct object_entry **compute_write_order(void) 652{ 653 unsigned int i, wo_end, last_untagged; 654 655 struct object_entry **wo; 656 struct object_entry *objects = to_pack.objects; 657 658 for (i = 0; i < to_pack.nr_objects; i++) { 659 objects[i].tagged = 0; 660 objects[i].filled = 0; 661 objects[i].delta_child = NULL; 662 objects[i].delta_sibling = NULL; 663 } 664 665 /* 666 * Fully connect delta_child/delta_sibling network. 667 * Make sure delta_sibling is sorted in the original 668 * recency order. 669 */ 670 for (i = to_pack.nr_objects; i > 0;) { 671 struct object_entry *e = &objects[--i]; 672 if (!e->delta) 673 continue; 674 /* Mark me as the first child */ 675 e->delta_sibling = e->delta->delta_child; 676 e->delta->delta_child = e; 677 } 678 679 /* 680 * Mark objects that are at the tip of tags. 681 */ 682 for_each_tag_ref(mark_tagged, NULL); 683 684 /* 685 * Give the objects in the original recency order until 686 * we see a tagged tip. 687 */ 688 ALLOC_ARRAY(wo, to_pack.nr_objects); 689 for (i = wo_end = 0; i < to_pack.nr_objects; i++) { 690 if (objects[i].tagged) 691 break; 692 add_to_write_order(wo, &wo_end, &objects[i]); 693 } 694 last_untagged = i; 695 696 /* 697 * Then fill all the tagged tips. 698 */ 699 for (; i < to_pack.nr_objects; i++) { 700 if (objects[i].tagged) 701 add_to_write_order(wo, &wo_end, &objects[i]); 702 } 703 704 /* 705 * And then all remaining commits and tags. 706 */ 707 for (i = last_untagged; i < to_pack.nr_objects; i++) { 708 if (objects[i].type != OBJ_COMMIT && 709 objects[i].type != OBJ_TAG) 710 continue; 711 add_to_write_order(wo, &wo_end, &objects[i]); 712 } 713 714 /* 715 * And then all the trees. 716 */ 717 for (i = last_untagged; i < to_pack.nr_objects; i++) { 718 if (objects[i].type != OBJ_TREE) 719 continue; 720 add_to_write_order(wo, &wo_end, &objects[i]); 721 } 722 723 /* 724 * Finally all the rest in really tight order 725 */ 726 for (i = last_untagged; i < to_pack.nr_objects; i++) { 727 if (!objects[i].filled) 728 add_family_to_write_order(wo, &wo_end, &objects[i]); 729 } 730 731 if (wo_end != to_pack.nr_objects) 732 die("ordered %u objects, expected %"PRIu32, wo_end, to_pack.nr_objects); 733 734 return wo; 735} 736 737static off_t write_reused_pack(struct hashfile *f) 738{ 739 unsigned char buffer[8192]; 740 off_t to_write, total; 741 int fd; 742 743 if (!is_pack_valid(reuse_packfile)) 744 die("packfile is invalid: %s", reuse_packfile->pack_name); 745 746 fd = git_open(reuse_packfile->pack_name); 747 if (fd < 0) 748 die_errno("unable to open packfile for reuse: %s", 749 reuse_packfile->pack_name); 750 751 if (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1) 752 die_errno("unable to seek in reused packfile"); 753 754 if (reuse_packfile_offset < 0) 755 reuse_packfile_offset = reuse_packfile->pack_size - 20; 756 757 total = to_write = reuse_packfile_offset - sizeof(struct pack_header); 758 759 while (to_write) { 760 int read_pack = xread(fd, buffer, sizeof(buffer)); 761 762 if (read_pack <= 0) 763 die_errno("unable to read from reused packfile"); 764 765 if (read_pack > to_write) 766 read_pack = to_write; 767 768 hashwrite(f, buffer, read_pack); 769 to_write -= read_pack; 770 771 /* 772 * We don't know the actual number of objects written, 773 * only how many bytes written, how many bytes total, and 774 * how many objects total. So we can fake it by pretending all 775 * objects we are writing are the same size. This gives us a 776 * smooth progress meter, and at the end it matches the true 777 * answer. 778 */ 779 written = reuse_packfile_objects * 780 (((double)(total - to_write)) / total); 781 display_progress(progress_state, written); 782 } 783 784 close(fd); 785 written = reuse_packfile_objects; 786 display_progress(progress_state, written); 787 return reuse_packfile_offset - sizeof(struct pack_header); 788} 789 790static const char no_split_warning[] = N_( 791"disabling bitmap writing, packs are split due to pack.packSizeLimit" 792); 793 794static void write_pack_file(void) 795{ 796 uint32_t i = 0, j; 797 struct hashfile *f; 798 off_t offset; 799 uint32_t nr_remaining = nr_result; 800 time_t last_mtime = 0; 801 struct object_entry **write_order; 802 803 if (progress > pack_to_stdout) 804 progress_state = start_progress(_("Writing objects"), nr_result); 805 ALLOC_ARRAY(written_list, to_pack.nr_objects); 806 write_order = compute_write_order(); 807 808 do { 809 struct object_id oid; 810 char *pack_tmp_name = NULL; 811 812 if (pack_to_stdout) 813 f = hashfd_throughput(1, "<stdout>", progress_state); 814 else 815 f = create_tmp_packfile(&pack_tmp_name); 816 817 offset = write_pack_header(f, nr_remaining); 818 819 if (reuse_packfile) { 820 off_t packfile_size; 821 assert(pack_to_stdout); 822 823 packfile_size = write_reused_pack(f); 824 offset += packfile_size; 825 } 826 827 nr_written = 0; 828 for (; i < to_pack.nr_objects; i++) { 829 struct object_entry *e = write_order[i]; 830 if (write_one(f, e, &offset) == WRITE_ONE_BREAK) 831 break; 832 display_progress(progress_state, written); 833 } 834 835 /* 836 * Did we write the wrong # entries in the header? 837 * If so, rewrite it like in fast-import 838 */ 839 if (pack_to_stdout) { 840 hashclose(f, oid.hash, CSUM_CLOSE); 841 } else if (nr_written == nr_remaining) { 842 hashclose(f, oid.hash, CSUM_FSYNC); 843 } else { 844 int fd = hashclose(f, oid.hash, 0); 845 fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 846 nr_written, oid.hash, offset); 847 close(fd); 848 if (write_bitmap_index) { 849 warning(_(no_split_warning)); 850 write_bitmap_index = 0; 851 } 852 } 853 854 if (!pack_to_stdout) { 855 struct stat st; 856 struct strbuf tmpname = STRBUF_INIT; 857 858 /* 859 * Packs are runtime accessed in their mtime 860 * order since newer packs are more likely to contain 861 * younger objects. So if we are creating multiple 862 * packs then we should modify the mtime of later ones 863 * to preserve this property. 864 */ 865 if (stat(pack_tmp_name, &st) < 0) { 866 warning_errno("failed to stat %s", pack_tmp_name); 867 } else if (!last_mtime) { 868 last_mtime = st.st_mtime; 869 } else { 870 struct utimbuf utb; 871 utb.actime = st.st_atime; 872 utb.modtime = --last_mtime; 873 if (utime(pack_tmp_name, &utb) < 0) 874 warning_errno("failed utime() on %s", pack_tmp_name); 875 } 876 877 strbuf_addf(&tmpname, "%s-", base_name); 878 879 if (write_bitmap_index) { 880 bitmap_writer_set_checksum(oid.hash); 881 bitmap_writer_build_type_index(written_list, nr_written); 882 } 883 884 finish_tmp_packfile(&tmpname, pack_tmp_name, 885 written_list, nr_written, 886 &pack_idx_opts, oid.hash); 887 888 if (write_bitmap_index) { 889 strbuf_addf(&tmpname, "%s.bitmap", oid_to_hex(&oid)); 890 891 stop_progress(&progress_state); 892 893 bitmap_writer_show_progress(progress); 894 bitmap_writer_reuse_bitmaps(&to_pack); 895 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 896 bitmap_writer_build(&to_pack); 897 bitmap_writer_finish(written_list, nr_written, 898 tmpname.buf, write_bitmap_options); 899 write_bitmap_index = 0; 900 } 901 902 strbuf_release(&tmpname); 903 free(pack_tmp_name); 904 puts(oid_to_hex(&oid)); 905 } 906 907 /* mark written objects as written to previous pack */ 908 for (j = 0; j < nr_written; j++) { 909 written_list[j]->offset = (off_t)-1; 910 } 911 nr_remaining -= nr_written; 912 } while (nr_remaining && i < to_pack.nr_objects); 913 914 free(written_list); 915 free(write_order); 916 stop_progress(&progress_state); 917 if (written != nr_result) 918 die("wrote %"PRIu32" objects while expecting %"PRIu32, 919 written, nr_result); 920} 921 922static int no_try_delta(const char *path) 923{ 924 static struct attr_check *check; 925 926 if (!check) 927 check = attr_check_initl("delta", NULL); 928 if (git_check_attr(path, check)) 929 return 0; 930 if (ATTR_FALSE(check->items[0].value)) 931 return 1; 932 return 0; 933} 934 935/* 936 * When adding an object, check whether we have already added it 937 * to our packing list. If so, we can skip. However, if we are 938 * being asked to excludei t, but the previous mention was to include 939 * it, make sure to adjust its flags and tweak our numbers accordingly. 940 * 941 * As an optimization, we pass out the index position where we would have 942 * found the item, since that saves us from having to look it up again a 943 * few lines later when we want to add the new entry. 944 */ 945static int have_duplicate_entry(const struct object_id *oid, 946 int exclude, 947 uint32_t *index_pos) 948{ 949 struct object_entry *entry; 950 951 entry = packlist_find(&to_pack, oid->hash, index_pos); 952 if (!entry) 953 return 0; 954 955 if (exclude) { 956 if (!entry->preferred_base) 957 nr_result--; 958 entry->preferred_base = 1; 959 } 960 961 return 1; 962} 963 964static int want_found_object(int exclude, struct packed_git *p) 965{ 966 if (exclude) 967 return 1; 968 if (incremental) 969 return 0; 970 971 /* 972 * When asked to do --local (do not include an object that appears in a 973 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 974 * an object that appears in a pack marked with .keep), finding a pack 975 * that matches the criteria is sufficient for us to decide to omit it. 976 * However, even if this pack does not satisfy the criteria, we need to 977 * make sure no copy of this object appears in _any_ pack that makes us 978 * to omit the object, so we need to check all the packs. 979 * 980 * We can however first check whether these options can possible matter; 981 * if they do not matter we know we want the object in generated pack. 982 * Otherwise, we signal "-1" at the end to tell the caller that we do 983 * not know either way, and it needs to check more packs. 984 */ 985 if (!ignore_packed_keep && 986 (!local || !have_non_local_packs)) 987 return 1; 988 989 if (local && !p->pack_local) 990 return 0; 991 if (ignore_packed_keep && p->pack_local && p->pack_keep) 992 return 0; 993 994 /* we don't know yet; keep looking for more packs */ 995 return -1; 996} 997 998/* 999 * Check whether we want the object in the pack (e.g., we do not want1000 * objects found in non-local stores if the "--local" option was used).1001 *1002 * If the caller already knows an existing pack it wants to take the object1003 * from, that is passed in *found_pack and *found_offset; otherwise this1004 * function finds if there is any pack that has the object and returns the pack1005 * and its offset in these variables.1006 */1007static int want_object_in_pack(const struct object_id *oid,1008 int exclude,1009 struct packed_git **found_pack,1010 off_t *found_offset)1011{1012 int want;1013 struct list_head *pos;10141015 if (!exclude && local && has_loose_object_nonlocal(oid->hash))1016 return 0;10171018 /*1019 * If we already know the pack object lives in, start checks from that1020 * pack - in the usual case when neither --local was given nor .keep files1021 * are present we will determine the answer right now.1022 */1023 if (*found_pack) {1024 want = want_found_object(exclude, *found_pack);1025 if (want != -1)1026 return want;1027 }1028 list_for_each(pos, get_packed_git_mru(the_repository)) {1029 struct packed_git *p = list_entry(pos, struct packed_git, mru);1030 off_t offset;10311032 if (p == *found_pack)1033 offset = *found_offset;1034 else1035 offset = find_pack_entry_one(oid->hash, p);10361037 if (offset) {1038 if (!*found_pack) {1039 if (!is_pack_valid(p))1040 continue;1041 *found_offset = offset;1042 *found_pack = p;1043 }1044 want = want_found_object(exclude, p);1045 if (!exclude && want > 0)1046 list_move(&p->mru,1047 get_packed_git_mru(the_repository));1048 if (want != -1)1049 return want;1050 }1051 }10521053 return 1;1054}10551056static void create_object_entry(const struct object_id *oid,1057 enum object_type type,1058 uint32_t hash,1059 int exclude,1060 int no_try_delta,1061 uint32_t index_pos,1062 struct packed_git *found_pack,1063 off_t found_offset)1064{1065 struct object_entry *entry;10661067 entry = packlist_alloc(&to_pack, oid->hash, index_pos);1068 entry->hash = hash;1069 if (type)1070 entry->type = type;1071 if (exclude)1072 entry->preferred_base = 1;1073 else1074 nr_result++;1075 if (found_pack) {1076 entry->in_pack = found_pack;1077 entry->in_pack_offset = found_offset;1078 }10791080 entry->no_try_delta = no_try_delta;1081}10821083static const char no_closure_warning[] = N_(1084"disabling bitmap writing, as some objects are not being packed"1085);10861087static int add_object_entry(const struct object_id *oid, enum object_type type,1088 const char *name, int exclude)1089{1090 struct packed_git *found_pack = NULL;1091 off_t found_offset = 0;1092 uint32_t index_pos;10931094 if (have_duplicate_entry(oid, exclude, &index_pos))1095 return 0;10961097 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1098 /* The pack is missing an object, so it will not have closure */1099 if (write_bitmap_index) {1100 warning(_(no_closure_warning));1101 write_bitmap_index = 0;1102 }1103 return 0;1104 }11051106 create_object_entry(oid, type, pack_name_hash(name),1107 exclude, name && no_try_delta(name),1108 index_pos, found_pack, found_offset);11091110 display_progress(progress_state, nr_result);1111 return 1;1112}11131114static int add_object_entry_from_bitmap(const struct object_id *oid,1115 enum object_type type,1116 int flags, uint32_t name_hash,1117 struct packed_git *pack, off_t offset)1118{1119 uint32_t index_pos;11201121 if (have_duplicate_entry(oid, 0, &index_pos))1122 return 0;11231124 if (!want_object_in_pack(oid, 0, &pack, &offset))1125 return 0;11261127 create_object_entry(oid, type, name_hash, 0, 0, index_pos, pack, offset);11281129 display_progress(progress_state, nr_result);1130 return 1;1131}11321133struct pbase_tree_cache {1134 struct object_id oid;1135 int ref;1136 int temporary;1137 void *tree_data;1138 unsigned long tree_size;1139};11401141static struct pbase_tree_cache *(pbase_tree_cache[256]);1142static int pbase_tree_cache_ix(const struct object_id *oid)1143{1144 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);1145}1146static int pbase_tree_cache_ix_incr(int ix)1147{1148 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);1149}11501151static struct pbase_tree {1152 struct pbase_tree *next;1153 /* This is a phony "cache" entry; we are not1154 * going to evict it or find it through _get()1155 * mechanism -- this is for the toplevel node that1156 * would almost always change with any commit.1157 */1158 struct pbase_tree_cache pcache;1159} *pbase_tree;11601161static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1162{1163 struct pbase_tree_cache *ent, *nent;1164 void *data;1165 unsigned long size;1166 enum object_type type;1167 int neigh;1168 int my_ix = pbase_tree_cache_ix(oid);1169 int available_ix = -1;11701171 /* pbase-tree-cache acts as a limited hashtable.1172 * your object will be found at your index or within a few1173 * slots after that slot if it is cached.1174 */1175 for (neigh = 0; neigh < 8; neigh++) {1176 ent = pbase_tree_cache[my_ix];1177 if (ent && !oidcmp(&ent->oid, oid)) {1178 ent->ref++;1179 return ent;1180 }1181 else if (((available_ix < 0) && (!ent || !ent->ref)) ||1182 ((0 <= available_ix) &&1183 (!ent && pbase_tree_cache[available_ix])))1184 available_ix = my_ix;1185 if (!ent)1186 break;1187 my_ix = pbase_tree_cache_ix_incr(my_ix);1188 }11891190 /* Did not find one. Either we got a bogus request or1191 * we need to read and perhaps cache.1192 */1193 data = read_object_file(oid, &type, &size);1194 if (!data)1195 return NULL;1196 if (type != OBJ_TREE) {1197 free(data);1198 return NULL;1199 }12001201 /* We need to either cache or return a throwaway copy */12021203 if (available_ix < 0)1204 ent = NULL;1205 else {1206 ent = pbase_tree_cache[available_ix];1207 my_ix = available_ix;1208 }12091210 if (!ent) {1211 nent = xmalloc(sizeof(*nent));1212 nent->temporary = (available_ix < 0);1213 }1214 else {1215 /* evict and reuse */1216 free(ent->tree_data);1217 nent = ent;1218 }1219 oidcpy(&nent->oid, oid);1220 nent->tree_data = data;1221 nent->tree_size = size;1222 nent->ref = 1;1223 if (!nent->temporary)1224 pbase_tree_cache[my_ix] = nent;1225 return nent;1226}12271228static void pbase_tree_put(struct pbase_tree_cache *cache)1229{1230 if (!cache->temporary) {1231 cache->ref--;1232 return;1233 }1234 free(cache->tree_data);1235 free(cache);1236}12371238static int name_cmp_len(const char *name)1239{1240 int i;1241 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)1242 ;1243 return i;1244}12451246static void add_pbase_object(struct tree_desc *tree,1247 const char *name,1248 int cmplen,1249 const char *fullname)1250{1251 struct name_entry entry;1252 int cmp;12531254 while (tree_entry(tree,&entry)) {1255 if (S_ISGITLINK(entry.mode))1256 continue;1257 cmp = tree_entry_len(&entry) != cmplen ? 1 :1258 memcmp(name, entry.path, cmplen);1259 if (cmp > 0)1260 continue;1261 if (cmp < 0)1262 return;1263 if (name[cmplen] != '/') {1264 add_object_entry(entry.oid,1265 object_type(entry.mode),1266 fullname, 1);1267 return;1268 }1269 if (S_ISDIR(entry.mode)) {1270 struct tree_desc sub;1271 struct pbase_tree_cache *tree;1272 const char *down = name+cmplen+1;1273 int downlen = name_cmp_len(down);12741275 tree = pbase_tree_get(entry.oid);1276 if (!tree)1277 return;1278 init_tree_desc(&sub, tree->tree_data, tree->tree_size);12791280 add_pbase_object(&sub, down, downlen, fullname);1281 pbase_tree_put(tree);1282 }1283 }1284}12851286static unsigned *done_pbase_paths;1287static int done_pbase_paths_num;1288static int done_pbase_paths_alloc;1289static int done_pbase_path_pos(unsigned hash)1290{1291 int lo = 0;1292 int hi = done_pbase_paths_num;1293 while (lo < hi) {1294 int mi = lo + (hi - lo) / 2;1295 if (done_pbase_paths[mi] == hash)1296 return mi;1297 if (done_pbase_paths[mi] < hash)1298 hi = mi;1299 else1300 lo = mi + 1;1301 }1302 return -lo-1;1303}13041305static int check_pbase_path(unsigned hash)1306{1307 int pos = done_pbase_path_pos(hash);1308 if (0 <= pos)1309 return 1;1310 pos = -pos - 1;1311 ALLOC_GROW(done_pbase_paths,1312 done_pbase_paths_num + 1,1313 done_pbase_paths_alloc);1314 done_pbase_paths_num++;1315 if (pos < done_pbase_paths_num)1316 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,1317 done_pbase_paths_num - pos - 1);1318 done_pbase_paths[pos] = hash;1319 return 0;1320}13211322static void add_preferred_base_object(const char *name)1323{1324 struct pbase_tree *it;1325 int cmplen;1326 unsigned hash = pack_name_hash(name);13271328 if (!num_preferred_base || check_pbase_path(hash))1329 return;13301331 cmplen = name_cmp_len(name);1332 for (it = pbase_tree; it; it = it->next) {1333 if (cmplen == 0) {1334 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);1335 }1336 else {1337 struct tree_desc tree;1338 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1339 add_pbase_object(&tree, name, cmplen, name);1340 }1341 }1342}13431344static void add_preferred_base(struct object_id *oid)1345{1346 struct pbase_tree *it;1347 void *data;1348 unsigned long size;1349 struct object_id tree_oid;13501351 if (window <= num_preferred_base++)1352 return;13531354 data = read_object_with_reference(oid, tree_type, &size, &tree_oid);1355 if (!data)1356 return;13571358 for (it = pbase_tree; it; it = it->next) {1359 if (!oidcmp(&it->pcache.oid, &tree_oid)) {1360 free(data);1361 return;1362 }1363 }13641365 it = xcalloc(1, sizeof(*it));1366 it->next = pbase_tree;1367 pbase_tree = it;13681369 oidcpy(&it->pcache.oid, &tree_oid);1370 it->pcache.tree_data = data;1371 it->pcache.tree_size = size;1372}13731374static void cleanup_preferred_base(void)1375{1376 struct pbase_tree *it;1377 unsigned i;13781379 it = pbase_tree;1380 pbase_tree = NULL;1381 while (it) {1382 struct pbase_tree *tmp = it;1383 it = tmp->next;1384 free(tmp->pcache.tree_data);1385 free(tmp);1386 }13871388 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {1389 if (!pbase_tree_cache[i])1390 continue;1391 free(pbase_tree_cache[i]->tree_data);1392 FREE_AND_NULL(pbase_tree_cache[i]);1393 }13941395 FREE_AND_NULL(done_pbase_paths);1396 done_pbase_paths_num = done_pbase_paths_alloc = 0;1397}13981399static void check_object(struct object_entry *entry)1400{1401 if (entry->in_pack) {1402 struct packed_git *p = entry->in_pack;1403 struct pack_window *w_curs = NULL;1404 const unsigned char *base_ref = NULL;1405 struct object_entry *base_entry;1406 unsigned long used, used_0;1407 unsigned long avail;1408 off_t ofs;1409 unsigned char *buf, c;14101411 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);14121413 /*1414 * We want in_pack_type even if we do not reuse delta1415 * since non-delta representations could still be reused.1416 */1417 used = unpack_object_header_buffer(buf, avail,1418 &entry->in_pack_type,1419 &entry->size);1420 if (used == 0)1421 goto give_up;14221423 /*1424 * Determine if this is a delta and if so whether we can1425 * reuse it or not. Otherwise let's find out as cheaply as1426 * possible what the actual type and size for this object is.1427 */1428 switch (entry->in_pack_type) {1429 default:1430 /* Not a delta hence we've already got all we need. */1431 entry->type = entry->in_pack_type;1432 entry->in_pack_header_size = used;1433 if (entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB)1434 goto give_up;1435 unuse_pack(&w_curs);1436 return;1437 case OBJ_REF_DELTA:1438 if (reuse_delta && !entry->preferred_base)1439 base_ref = use_pack(p, &w_curs,1440 entry->in_pack_offset + used, NULL);1441 entry->in_pack_header_size = used + 20;1442 break;1443 case OBJ_OFS_DELTA:1444 buf = use_pack(p, &w_curs,1445 entry->in_pack_offset + used, NULL);1446 used_0 = 0;1447 c = buf[used_0++];1448 ofs = c & 127;1449 while (c & 128) {1450 ofs += 1;1451 if (!ofs || MSB(ofs, 7)) {1452 error("delta base offset overflow in pack for %s",1453 oid_to_hex(&entry->idx.oid));1454 goto give_up;1455 }1456 c = buf[used_0++];1457 ofs = (ofs << 7) + (c & 127);1458 }1459 ofs = entry->in_pack_offset - ofs;1460 if (ofs <= 0 || ofs >= entry->in_pack_offset) {1461 error("delta base offset out of bound for %s",1462 oid_to_hex(&entry->idx.oid));1463 goto give_up;1464 }1465 if (reuse_delta && !entry->preferred_base) {1466 struct revindex_entry *revidx;1467 revidx = find_pack_revindex(p, ofs);1468 if (!revidx)1469 goto give_up;1470 base_ref = nth_packed_object_sha1(p, revidx->nr);1471 }1472 entry->in_pack_header_size = used + used_0;1473 break;1474 }14751476 if (base_ref && (base_entry = packlist_find(&to_pack, base_ref, NULL))) {1477 /*1478 * If base_ref was set above that means we wish to1479 * reuse delta data, and we even found that base1480 * in the list of objects we want to pack. Goodie!1481 *1482 * Depth value does not matter - find_deltas() will1483 * never consider reused delta as the base object to1484 * deltify other objects against, in order to avoid1485 * circular deltas.1486 */1487 entry->type = entry->in_pack_type;1488 entry->delta = base_entry;1489 entry->delta_size = entry->size;1490 entry->delta_sibling = base_entry->delta_child;1491 base_entry->delta_child = entry;1492 unuse_pack(&w_curs);1493 return;1494 }14951496 if (entry->type) {1497 /*1498 * This must be a delta and we already know what the1499 * final object type is. Let's extract the actual1500 * object size from the delta header.1501 */1502 entry->size = get_size_from_delta(p, &w_curs,1503 entry->in_pack_offset + entry->in_pack_header_size);1504 if (entry->size == 0)1505 goto give_up;1506 unuse_pack(&w_curs);1507 return;1508 }15091510 /*1511 * No choice but to fall back to the recursive delta walk1512 * with sha1_object_info() to find about the object type1513 * at this point...1514 */1515 give_up:1516 unuse_pack(&w_curs);1517 }15181519 entry->type = oid_object_info(the_repository, &entry->idx.oid,1520 &entry->size);1521 /*1522 * The error condition is checked in prepare_pack(). This is1523 * to permit a missing preferred base object to be ignored1524 * as a preferred base. Doing so can result in a larger1525 * pack file, but the transfer will still take place.1526 */1527}15281529static int pack_offset_sort(const void *_a, const void *_b)1530{1531 const struct object_entry *a = *(struct object_entry **)_a;1532 const struct object_entry *b = *(struct object_entry **)_b;15331534 /* avoid filesystem trashing with loose objects */1535 if (!a->in_pack && !b->in_pack)1536 return oidcmp(&a->idx.oid, &b->idx.oid);15371538 if (a->in_pack < b->in_pack)1539 return -1;1540 if (a->in_pack > b->in_pack)1541 return 1;1542 return a->in_pack_offset < b->in_pack_offset ? -1 :1543 (a->in_pack_offset > b->in_pack_offset);1544}15451546/*1547 * Drop an on-disk delta we were planning to reuse. Naively, this would1548 * just involve blanking out the "delta" field, but we have to deal1549 * with some extra book-keeping:1550 *1551 * 1. Removing ourselves from the delta_sibling linked list.1552 *1553 * 2. Updating our size/type to the non-delta representation. These were1554 * either not recorded initially (size) or overwritten with the delta type1555 * (type) when check_object() decided to reuse the delta.1556 *1557 * 3. Resetting our delta depth, as we are now a base object.1558 */1559static void drop_reused_delta(struct object_entry *entry)1560{1561 struct object_entry **p = &entry->delta->delta_child;1562 struct object_info oi = OBJECT_INFO_INIT;15631564 while (*p) {1565 if (*p == entry)1566 *p = (*p)->delta_sibling;1567 else1568 p = &(*p)->delta_sibling;1569 }1570 entry->delta = NULL;1571 entry->depth = 0;15721573 oi.sizep = &entry->size;1574 oi.typep = &entry->type;1575 if (packed_object_info(the_repository, entry->in_pack,1576 entry->in_pack_offset, &oi) < 0) {1577 /*1578 * We failed to get the info from this pack for some reason;1579 * fall back to sha1_object_info, which may find another copy.1580 * And if that fails, the error will be recorded in entry->type1581 * and dealt with in prepare_pack().1582 */1583 entry->type = oid_object_info(the_repository, &entry->idx.oid,1584 &entry->size);1585 }1586}15871588/*1589 * Follow the chain of deltas from this entry onward, throwing away any links1590 * that cause us to hit a cycle (as determined by the DFS state flags in1591 * the entries).1592 *1593 * We also detect too-long reused chains that would violate our --depth1594 * limit.1595 */1596static void break_delta_chains(struct object_entry *entry)1597{1598 /*1599 * The actual depth of each object we will write is stored as an int,1600 * as it cannot exceed our int "depth" limit. But before we break1601 * changes based no that limit, we may potentially go as deep as the1602 * number of objects, which is elsewhere bounded to a uint32_t.1603 */1604 uint32_t total_depth;1605 struct object_entry *cur, *next;16061607 for (cur = entry, total_depth = 0;1608 cur;1609 cur = cur->delta, total_depth++) {1610 if (cur->dfs_state == DFS_DONE) {1611 /*1612 * We've already seen this object and know it isn't1613 * part of a cycle. We do need to append its depth1614 * to our count.1615 */1616 total_depth += cur->depth;1617 break;1618 }16191620 /*1621 * We break cycles before looping, so an ACTIVE state (or any1622 * other cruft which made its way into the state variable)1623 * is a bug.1624 */1625 if (cur->dfs_state != DFS_NONE)1626 die("BUG: confusing delta dfs state in first pass: %d",1627 cur->dfs_state);16281629 /*1630 * Now we know this is the first time we've seen the object. If1631 * it's not a delta, we're done traversing, but we'll mark it1632 * done to save time on future traversals.1633 */1634 if (!cur->delta) {1635 cur->dfs_state = DFS_DONE;1636 break;1637 }16381639 /*1640 * Mark ourselves as active and see if the next step causes1641 * us to cycle to another active object. It's important to do1642 * this _before_ we loop, because it impacts where we make the1643 * cut, and thus how our total_depth counter works.1644 * E.g., We may see a partial loop like:1645 *1646 * A -> B -> C -> D -> B1647 *1648 * Cutting B->C breaks the cycle. But now the depth of A is1649 * only 1, and our total_depth counter is at 3. The size of the1650 * error is always one less than the size of the cycle we1651 * broke. Commits C and D were "lost" from A's chain.1652 *1653 * If we instead cut D->B, then the depth of A is correct at 3.1654 * We keep all commits in the chain that we examined.1655 */1656 cur->dfs_state = DFS_ACTIVE;1657 if (cur->delta->dfs_state == DFS_ACTIVE) {1658 drop_reused_delta(cur);1659 cur->dfs_state = DFS_DONE;1660 break;1661 }1662 }16631664 /*1665 * And now that we've gone all the way to the bottom of the chain, we1666 * need to clear the active flags and set the depth fields as1667 * appropriate. Unlike the loop above, which can quit when it drops a1668 * delta, we need to keep going to look for more depth cuts. So we need1669 * an extra "next" pointer to keep going after we reset cur->delta.1670 */1671 for (cur = entry; cur; cur = next) {1672 next = cur->delta;16731674 /*1675 * We should have a chain of zero or more ACTIVE states down to1676 * a final DONE. We can quit after the DONE, because either it1677 * has no bases, or we've already handled them in a previous1678 * call.1679 */1680 if (cur->dfs_state == DFS_DONE)1681 break;1682 else if (cur->dfs_state != DFS_ACTIVE)1683 die("BUG: confusing delta dfs state in second pass: %d",1684 cur->dfs_state);16851686 /*1687 * If the total_depth is more than depth, then we need to snip1688 * the chain into two or more smaller chains that don't exceed1689 * the maximum depth. Most of the resulting chains will contain1690 * (depth + 1) entries (i.e., depth deltas plus one base), and1691 * the last chain (i.e., the one containing entry) will contain1692 * whatever entries are left over, namely1693 * (total_depth % (depth + 1)) of them.1694 *1695 * Since we are iterating towards decreasing depth, we need to1696 * decrement total_depth as we go, and we need to write to the1697 * entry what its final depth will be after all of the1698 * snipping. Since we're snipping into chains of length (depth1699 * + 1) entries, the final depth of an entry will be its1700 * original depth modulo (depth + 1). Any time we encounter an1701 * entry whose final depth is supposed to be zero, we snip it1702 * from its delta base, thereby making it so.1703 */1704 cur->depth = (total_depth--) % (depth + 1);1705 if (!cur->depth)1706 drop_reused_delta(cur);17071708 cur->dfs_state = DFS_DONE;1709 }1710}17111712static void get_object_details(void)1713{1714 uint32_t i;1715 struct object_entry **sorted_by_offset;17161717 sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));1718 for (i = 0; i < to_pack.nr_objects; i++)1719 sorted_by_offset[i] = to_pack.objects + i;1720 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17211722 for (i = 0; i < to_pack.nr_objects; i++) {1723 struct object_entry *entry = sorted_by_offset[i];1724 check_object(entry);1725 if (big_file_threshold < entry->size)1726 entry->no_try_delta = 1;1727 }17281729 /*1730 * This must happen in a second pass, since we rely on the delta1731 * information for the whole list being completed.1732 */1733 for (i = 0; i < to_pack.nr_objects; i++)1734 break_delta_chains(&to_pack.objects[i]);17351736 free(sorted_by_offset);1737}17381739/*1740 * We search for deltas in a list sorted by type, by filename hash, and then1741 * by size, so that we see progressively smaller and smaller files.1742 * That's because we prefer deltas to be from the bigger file1743 * to the smaller -- deletes are potentially cheaper, but perhaps1744 * more importantly, the bigger file is likely the more recent1745 * one. The deepest deltas are therefore the oldest objects which are1746 * less susceptible to be accessed often.1747 */1748static int type_size_sort(const void *_a, const void *_b)1749{1750 const struct object_entry *a = *(struct object_entry **)_a;1751 const struct object_entry *b = *(struct object_entry **)_b;17521753 if (a->type > b->type)1754 return -1;1755 if (a->type < b->type)1756 return 1;1757 if (a->hash > b->hash)1758 return -1;1759 if (a->hash < b->hash)1760 return 1;1761 if (a->preferred_base > b->preferred_base)1762 return -1;1763 if (a->preferred_base < b->preferred_base)1764 return 1;1765 if (a->size > b->size)1766 return -1;1767 if (a->size < b->size)1768 return 1;1769 return a < b ? -1 : (a > b); /* newest first */1770}17711772struct unpacked {1773 struct object_entry *entry;1774 void *data;1775 struct delta_index *index;1776 unsigned depth;1777};17781779static int delta_cacheable(unsigned long src_size, unsigned long trg_size,1780 unsigned long delta_size)1781{1782 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1783 return 0;17841785 if (delta_size < cache_max_small_delta_size)1786 return 1;17871788 /* cache delta, if objects are large enough compared to delta size */1789 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))1790 return 1;17911792 return 0;1793}17941795#ifndef NO_PTHREADS17961797static pthread_mutex_t read_mutex;1798#define read_lock() pthread_mutex_lock(&read_mutex)1799#define read_unlock() pthread_mutex_unlock(&read_mutex)18001801static pthread_mutex_t cache_mutex;1802#define cache_lock() pthread_mutex_lock(&cache_mutex)1803#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18041805static pthread_mutex_t progress_mutex;1806#define progress_lock() pthread_mutex_lock(&progress_mutex)1807#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18081809#else18101811#define read_lock() (void)01812#define read_unlock() (void)01813#define cache_lock() (void)01814#define cache_unlock() (void)01815#define progress_lock() (void)01816#define progress_unlock() (void)018171818#endif18191820static int try_delta(struct unpacked *trg, struct unpacked *src,1821 unsigned max_depth, unsigned long *mem_usage)1822{1823 struct object_entry *trg_entry = trg->entry;1824 struct object_entry *src_entry = src->entry;1825 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1826 unsigned ref_depth;1827 enum object_type type;1828 void *delta_buf;18291830 /* Don't bother doing diffs between different types */1831 if (trg_entry->type != src_entry->type)1832 return -1;18331834 /*1835 * We do not bother to try a delta that we discarded on an1836 * earlier try, but only when reusing delta data. Note that1837 * src_entry that is marked as the preferred_base should always1838 * be considered, as even if we produce a suboptimal delta against1839 * it, we will still save the transfer cost, as we already know1840 * the other side has it and we won't send src_entry at all.1841 */1842 if (reuse_delta && trg_entry->in_pack &&1843 trg_entry->in_pack == src_entry->in_pack &&1844 !src_entry->preferred_base &&1845 trg_entry->in_pack_type != OBJ_REF_DELTA &&1846 trg_entry->in_pack_type != OBJ_OFS_DELTA)1847 return 0;18481849 /* Let's not bust the allowed depth. */1850 if (src->depth >= max_depth)1851 return 0;18521853 /* Now some size filtering heuristics. */1854 trg_size = trg_entry->size;1855 if (!trg_entry->delta) {1856 max_size = trg_size/2 - 20;1857 ref_depth = 1;1858 } else {1859 max_size = trg_entry->delta_size;1860 ref_depth = trg->depth;1861 }1862 max_size = (uint64_t)max_size * (max_depth - src->depth) /1863 (max_depth - ref_depth + 1);1864 if (max_size == 0)1865 return 0;1866 src_size = src_entry->size;1867 sizediff = src_size < trg_size ? trg_size - src_size : 0;1868 if (sizediff >= max_size)1869 return 0;1870 if (trg_size < src_size / 32)1871 return 0;18721873 /* Load data if not already done */1874 if (!trg->data) {1875 read_lock();1876 trg->data = read_object_file(&trg_entry->idx.oid, &type, &sz);1877 read_unlock();1878 if (!trg->data)1879 die("object %s cannot be read",1880 oid_to_hex(&trg_entry->idx.oid));1881 if (sz != trg_size)1882 die("object %s inconsistent object length (%lu vs %lu)",1883 oid_to_hex(&trg_entry->idx.oid), sz,1884 trg_size);1885 *mem_usage += sz;1886 }1887 if (!src->data) {1888 read_lock();1889 src->data = read_object_file(&src_entry->idx.oid, &type, &sz);1890 read_unlock();1891 if (!src->data) {1892 if (src_entry->preferred_base) {1893 static int warned = 0;1894 if (!warned++)1895 warning("object %s cannot be read",1896 oid_to_hex(&src_entry->idx.oid));1897 /*1898 * Those objects are not included in the1899 * resulting pack. Be resilient and ignore1900 * them if they can't be read, in case the1901 * pack could be created nevertheless.1902 */1903 return 0;1904 }1905 die("object %s cannot be read",1906 oid_to_hex(&src_entry->idx.oid));1907 }1908 if (sz != src_size)1909 die("object %s inconsistent object length (%lu vs %lu)",1910 oid_to_hex(&src_entry->idx.oid), sz,1911 src_size);1912 *mem_usage += sz;1913 }1914 if (!src->index) {1915 src->index = create_delta_index(src->data, src_size);1916 if (!src->index) {1917 static int warned = 0;1918 if (!warned++)1919 warning("suboptimal pack - out of memory");1920 return 0;1921 }1922 *mem_usage += sizeof_delta_index(src->index);1923 }19241925 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);1926 if (!delta_buf)1927 return 0;19281929 if (trg_entry->delta) {1930 /* Prefer only shallower same-sized deltas. */1931 if (delta_size == trg_entry->delta_size &&1932 src->depth + 1 >= trg->depth) {1933 free(delta_buf);1934 return 0;1935 }1936 }19371938 /*1939 * Handle memory allocation outside of the cache1940 * accounting lock. Compiler will optimize the strangeness1941 * away when NO_PTHREADS is defined.1942 */1943 free(trg_entry->delta_data);1944 cache_lock();1945 if (trg_entry->delta_data) {1946 delta_cache_size -= trg_entry->delta_size;1947 trg_entry->delta_data = NULL;1948 }1949 if (delta_cacheable(src_size, trg_size, delta_size)) {1950 delta_cache_size += delta_size;1951 cache_unlock();1952 trg_entry->delta_data = xrealloc(delta_buf, delta_size);1953 } else {1954 cache_unlock();1955 free(delta_buf);1956 }19571958 trg_entry->delta = src_entry;1959 trg_entry->delta_size = delta_size;1960 trg->depth = src->depth + 1;19611962 return 1;1963}19641965static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)1966{1967 struct object_entry *child = me->delta_child;1968 unsigned int m = n;1969 while (child) {1970 unsigned int c = check_delta_limit(child, n + 1);1971 if (m < c)1972 m = c;1973 child = child->delta_sibling;1974 }1975 return m;1976}19771978static unsigned long free_unpacked(struct unpacked *n)1979{1980 unsigned long freed_mem = sizeof_delta_index(n->index);1981 free_delta_index(n->index);1982 n->index = NULL;1983 if (n->data) {1984 freed_mem += n->entry->size;1985 FREE_AND_NULL(n->data);1986 }1987 n->entry = NULL;1988 n->depth = 0;1989 return freed_mem;1990}19911992static void find_deltas(struct object_entry **list, unsigned *list_size,1993 int window, int depth, unsigned *processed)1994{1995 uint32_t i, idx = 0, count = 0;1996 struct unpacked *array;1997 unsigned long mem_usage = 0;19981999 array = xcalloc(window, sizeof(struct unpacked));20002001 for (;;) {2002 struct object_entry *entry;2003 struct unpacked *n = array + idx;2004 int j, max_depth, best_base = -1;20052006 progress_lock();2007 if (!*list_size) {2008 progress_unlock();2009 break;2010 }2011 entry = *list++;2012 (*list_size)--;2013 if (!entry->preferred_base) {2014 (*processed)++;2015 display_progress(progress_state, *processed);2016 }2017 progress_unlock();20182019 mem_usage -= free_unpacked(n);2020 n->entry = entry;20212022 while (window_memory_limit &&2023 mem_usage > window_memory_limit &&2024 count > 1) {2025 uint32_t tail = (idx + window - count) % window;2026 mem_usage -= free_unpacked(array + tail);2027 count--;2028 }20292030 /* We do not compute delta to *create* objects we are not2031 * going to pack.2032 */2033 if (entry->preferred_base)2034 goto next;20352036 /*2037 * If the current object is at pack edge, take the depth the2038 * objects that depend on the current object into account2039 * otherwise they would become too deep.2040 */2041 max_depth = depth;2042 if (entry->delta_child) {2043 max_depth -= check_delta_limit(entry, 0);2044 if (max_depth <= 0)2045 goto next;2046 }20472048 j = window;2049 while (--j > 0) {2050 int ret;2051 uint32_t other_idx = idx + j;2052 struct unpacked *m;2053 if (other_idx >= window)2054 other_idx -= window;2055 m = array + other_idx;2056 if (!m->entry)2057 break;2058 ret = try_delta(n, m, max_depth, &mem_usage);2059 if (ret < 0)2060 break;2061 else if (ret > 0)2062 best_base = other_idx;2063 }20642065 /*2066 * If we decided to cache the delta data, then it is best2067 * to compress it right away. First because we have to do2068 * it anyway, and doing it here while we're threaded will2069 * save a lot of time in the non threaded write phase,2070 * as well as allow for caching more deltas within2071 * the same cache size limit.2072 * ...2073 * But only if not writing to stdout, since in that case2074 * the network is most likely throttling writes anyway,2075 * and therefore it is best to go to the write phase ASAP2076 * instead, as we can afford spending more time compressing2077 * between writes at that moment.2078 */2079 if (entry->delta_data && !pack_to_stdout) {2080 entry->z_delta_size = do_compress(&entry->delta_data,2081 entry->delta_size);2082 cache_lock();2083 delta_cache_size -= entry->delta_size;2084 delta_cache_size += entry->z_delta_size;2085 cache_unlock();2086 }20872088 /* if we made n a delta, and if n is already at max2089 * depth, leaving it in the window is pointless. we2090 * should evict it first.2091 */2092 if (entry->delta && max_depth <= n->depth)2093 continue;20942095 /*2096 * Move the best delta base up in the window, after the2097 * currently deltified object, to keep it longer. It will2098 * be the first base object to be attempted next.2099 */2100 if (entry->delta) {2101 struct unpacked swap = array[best_base];2102 int dist = (window + idx - best_base) % window;2103 int dst = best_base;2104 while (dist--) {2105 int src = (dst + 1) % window;2106 array[dst] = array[src];2107 dst = src;2108 }2109 array[dst] = swap;2110 }21112112 next:2113 idx++;2114 if (count + 1 < window)2115 count++;2116 if (idx >= window)2117 idx = 0;2118 }21192120 for (i = 0; i < window; ++i) {2121 free_delta_index(array[i].index);2122 free(array[i].data);2123 }2124 free(array);2125}21262127#ifndef NO_PTHREADS21282129static void try_to_free_from_threads(size_t size)2130{2131 read_lock();2132 release_pack_memory(size);2133 read_unlock();2134}21352136static try_to_free_t old_try_to_free_routine;21372138/*2139 * The main thread waits on the condition that (at least) one of the workers2140 * has stopped working (which is indicated in the .working member of2141 * struct thread_params).2142 * When a work thread has completed its work, it sets .working to 0 and2143 * signals the main thread and waits on the condition that .data_ready2144 * becomes 1.2145 */21462147struct thread_params {2148 pthread_t thread;2149 struct object_entry **list;2150 unsigned list_size;2151 unsigned remaining;2152 int window;2153 int depth;2154 int working;2155 int data_ready;2156 pthread_mutex_t mutex;2157 pthread_cond_t cond;2158 unsigned *processed;2159};21602161static pthread_cond_t progress_cond;21622163/*2164 * Mutex and conditional variable can't be statically-initialized on Windows.2165 */2166static void init_threaded_search(void)2167{2168 init_recursive_mutex(&read_mutex);2169 pthread_mutex_init(&cache_mutex, NULL);2170 pthread_mutex_init(&progress_mutex, NULL);2171 pthread_cond_init(&progress_cond, NULL);2172 old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads);2173}21742175static void cleanup_threaded_search(void)2176{2177 set_try_to_free_routine(old_try_to_free_routine);2178 pthread_cond_destroy(&progress_cond);2179 pthread_mutex_destroy(&read_mutex);2180 pthread_mutex_destroy(&cache_mutex);2181 pthread_mutex_destroy(&progress_mutex);2182}21832184static void *threaded_find_deltas(void *arg)2185{2186 struct thread_params *me = arg;21872188 progress_lock();2189 while (me->remaining) {2190 progress_unlock();21912192 find_deltas(me->list, &me->remaining,2193 me->window, me->depth, me->processed);21942195 progress_lock();2196 me->working = 0;2197 pthread_cond_signal(&progress_cond);2198 progress_unlock();21992200 /*2201 * We must not set ->data_ready before we wait on the2202 * condition because the main thread may have set it to 12203 * before we get here. In order to be sure that new2204 * work is available if we see 1 in ->data_ready, it2205 * was initialized to 0 before this thread was spawned2206 * and we reset it to 0 right away.2207 */2208 pthread_mutex_lock(&me->mutex);2209 while (!me->data_ready)2210 pthread_cond_wait(&me->cond, &me->mutex);2211 me->data_ready = 0;2212 pthread_mutex_unlock(&me->mutex);22132214 progress_lock();2215 }2216 progress_unlock();2217 /* leave ->working 1 so that this doesn't get more work assigned */2218 return NULL;2219}22202221static void ll_find_deltas(struct object_entry **list, unsigned list_size,2222 int window, int depth, unsigned *processed)2223{2224 struct thread_params *p;2225 int i, ret, active_threads = 0;22262227 init_threaded_search();22282229 if (delta_search_threads <= 1) {2230 find_deltas(list, &list_size, window, depth, processed);2231 cleanup_threaded_search();2232 return;2233 }2234 if (progress > pack_to_stdout)2235 fprintf(stderr, "Delta compression using up to %d threads.\n",2236 delta_search_threads);2237 p = xcalloc(delta_search_threads, sizeof(*p));22382239 /* Partition the work amongst work threads. */2240 for (i = 0; i < delta_search_threads; i++) {2241 unsigned sub_size = list_size / (delta_search_threads - i);22422243 /* don't use too small segments or no deltas will be found */2244 if (sub_size < 2*window && i+1 < delta_search_threads)2245 sub_size = 0;22462247 p[i].window = window;2248 p[i].depth = depth;2249 p[i].processed = processed;2250 p[i].working = 1;2251 p[i].data_ready = 0;22522253 /* try to split chunks on "path" boundaries */2254 while (sub_size && sub_size < list_size &&2255 list[sub_size]->hash &&2256 list[sub_size]->hash == list[sub_size-1]->hash)2257 sub_size++;22582259 p[i].list = list;2260 p[i].list_size = sub_size;2261 p[i].remaining = sub_size;22622263 list += sub_size;2264 list_size -= sub_size;2265 }22662267 /* Start work threads. */2268 for (i = 0; i < delta_search_threads; i++) {2269 if (!p[i].list_size)2270 continue;2271 pthread_mutex_init(&p[i].mutex, NULL);2272 pthread_cond_init(&p[i].cond, NULL);2273 ret = pthread_create(&p[i].thread, NULL,2274 threaded_find_deltas, &p[i]);2275 if (ret)2276 die("unable to create thread: %s", strerror(ret));2277 active_threads++;2278 }22792280 /*2281 * Now let's wait for work completion. Each time a thread is done2282 * with its work, we steal half of the remaining work from the2283 * thread with the largest number of unprocessed objects and give2284 * it to that newly idle thread. This ensure good load balancing2285 * until the remaining object list segments are simply too short2286 * to be worth splitting anymore.2287 */2288 while (active_threads) {2289 struct thread_params *target = NULL;2290 struct thread_params *victim = NULL;2291 unsigned sub_size = 0;22922293 progress_lock();2294 for (;;) {2295 for (i = 0; !target && i < delta_search_threads; i++)2296 if (!p[i].working)2297 target = &p[i];2298 if (target)2299 break;2300 pthread_cond_wait(&progress_cond, &progress_mutex);2301 }23022303 for (i = 0; i < delta_search_threads; i++)2304 if (p[i].remaining > 2*window &&2305 (!victim || victim->remaining < p[i].remaining))2306 victim = &p[i];2307 if (victim) {2308 sub_size = victim->remaining / 2;2309 list = victim->list + victim->list_size - sub_size;2310 while (sub_size && list[0]->hash &&2311 list[0]->hash == list[-1]->hash) {2312 list++;2313 sub_size--;2314 }2315 if (!sub_size) {2316 /*2317 * It is possible for some "paths" to have2318 * so many objects that no hash boundary2319 * might be found. Let's just steal the2320 * exact half in that case.2321 */2322 sub_size = victim->remaining / 2;2323 list -= sub_size;2324 }2325 target->list = list;2326 victim->list_size -= sub_size;2327 victim->remaining -= sub_size;2328 }2329 target->list_size = sub_size;2330 target->remaining = sub_size;2331 target->working = 1;2332 progress_unlock();23332334 pthread_mutex_lock(&target->mutex);2335 target->data_ready = 1;2336 pthread_cond_signal(&target->cond);2337 pthread_mutex_unlock(&target->mutex);23382339 if (!sub_size) {2340 pthread_join(target->thread, NULL);2341 pthread_cond_destroy(&target->cond);2342 pthread_mutex_destroy(&target->mutex);2343 active_threads--;2344 }2345 }2346 cleanup_threaded_search();2347 free(p);2348}23492350#else2351#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2352#endif23532354static void add_tag_chain(const struct object_id *oid)2355{2356 struct tag *tag;23572358 /*2359 * We catch duplicates already in add_object_entry(), but we'd2360 * prefer to do this extra check to avoid having to parse the2361 * tag at all if we already know that it's being packed (e.g., if2362 * it was included via bitmaps, we would not have parsed it2363 * previously).2364 */2365 if (packlist_find(&to_pack, oid->hash, NULL))2366 return;23672368 tag = lookup_tag(oid);2369 while (1) {2370 if (!tag || parse_tag(tag) || !tag->tagged)2371 die("unable to pack objects reachable from tag %s",2372 oid_to_hex(oid));23732374 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);23752376 if (tag->tagged->type != OBJ_TAG)2377 return;23782379 tag = (struct tag *)tag->tagged;2380 }2381}23822383static int add_ref_tag(const char *path, const struct object_id *oid, int flag, void *cb_data)2384{2385 struct object_id peeled;23862387 if (starts_with(path, "refs/tags/") && /* is a tag? */2388 !peel_ref(path, &peeled) && /* peelable? */2389 packlist_find(&to_pack, peeled.hash, NULL)) /* object packed? */2390 add_tag_chain(oid);2391 return 0;2392}23932394static void prepare_pack(int window, int depth)2395{2396 struct object_entry **delta_list;2397 uint32_t i, nr_deltas;2398 unsigned n;23992400 get_object_details();24012402 /*2403 * If we're locally repacking then we need to be doubly careful2404 * from now on in order to make sure no stealth corruption gets2405 * propagated to the new pack. Clients receiving streamed packs2406 * should validate everything they get anyway so no need to incur2407 * the additional cost here in that case.2408 */2409 if (!pack_to_stdout)2410 do_check_packed_object_crc = 1;24112412 if (!to_pack.nr_objects || !window || !depth)2413 return;24142415 ALLOC_ARRAY(delta_list, to_pack.nr_objects);2416 nr_deltas = n = 0;24172418 for (i = 0; i < to_pack.nr_objects; i++) {2419 struct object_entry *entry = to_pack.objects + i;24202421 if (entry->delta)2422 /* This happens if we decided to reuse existing2423 * delta from a pack. "reuse_delta &&" is implied.2424 */2425 continue;24262427 if (entry->size < 50)2428 continue;24292430 if (entry->no_try_delta)2431 continue;24322433 if (!entry->preferred_base) {2434 nr_deltas++;2435 if (entry->type < 0)2436 die("unable to get type of object %s",2437 oid_to_hex(&entry->idx.oid));2438 } else {2439 if (entry->type < 0) {2440 /*2441 * This object is not found, but we2442 * don't have to include it anyway.2443 */2444 continue;2445 }2446 }24472448 delta_list[n++] = entry;2449 }24502451 if (nr_deltas && n > 1) {2452 unsigned nr_done = 0;2453 if (progress)2454 progress_state = start_progress(_("Compressing objects"),2455 nr_deltas);2456 QSORT(delta_list, n, type_size_sort);2457 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2458 stop_progress(&progress_state);2459 if (nr_done != nr_deltas)2460 die("inconsistency with delta count");2461 }2462 free(delta_list);2463}24642465static int git_pack_config(const char *k, const char *v, void *cb)2466{2467 if (!strcmp(k, "pack.window")) {2468 window = git_config_int(k, v);2469 return 0;2470 }2471 if (!strcmp(k, "pack.windowmemory")) {2472 window_memory_limit = git_config_ulong(k, v);2473 return 0;2474 }2475 if (!strcmp(k, "pack.depth")) {2476 depth = git_config_int(k, v);2477 return 0;2478 }2479 if (!strcmp(k, "pack.deltacachesize")) {2480 max_delta_cache_size = git_config_int(k, v);2481 return 0;2482 }2483 if (!strcmp(k, "pack.deltacachelimit")) {2484 cache_max_small_delta_size = git_config_int(k, v);2485 return 0;2486 }2487 if (!strcmp(k, "pack.writebitmaphashcache")) {2488 if (git_config_bool(k, v))2489 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2490 else2491 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2492 }2493 if (!strcmp(k, "pack.usebitmaps")) {2494 use_bitmap_index_default = git_config_bool(k, v);2495 return 0;2496 }2497 if (!strcmp(k, "pack.threads")) {2498 delta_search_threads = git_config_int(k, v);2499 if (delta_search_threads < 0)2500 die("invalid number of threads specified (%d)",2501 delta_search_threads);2502#ifdef NO_PTHREADS2503 if (delta_search_threads != 1) {2504 warning("no threads support, ignoring %s", k);2505 delta_search_threads = 0;2506 }2507#endif2508 return 0;2509 }2510 if (!strcmp(k, "pack.indexversion")) {2511 pack_idx_opts.version = git_config_int(k, v);2512 if (pack_idx_opts.version > 2)2513 die("bad pack.indexversion=%"PRIu32,2514 pack_idx_opts.version);2515 return 0;2516 }2517 return git_default_config(k, v, cb);2518}25192520static void read_object_list_from_stdin(void)2521{2522 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];2523 struct object_id oid;2524 const char *p;25252526 for (;;) {2527 if (!fgets(line, sizeof(line), stdin)) {2528 if (feof(stdin))2529 break;2530 if (!ferror(stdin))2531 die("fgets returned NULL, not EOF, not error!");2532 if (errno != EINTR)2533 die_errno("fgets");2534 clearerr(stdin);2535 continue;2536 }2537 if (line[0] == '-') {2538 if (get_oid_hex(line+1, &oid))2539 die("expected edge object ID, got garbage:\n %s",2540 line);2541 add_preferred_base(&oid);2542 continue;2543 }2544 if (parse_oid_hex(line, &oid, &p))2545 die("expected object ID, got garbage:\n %s", line);25462547 add_preferred_base_object(p + 1);2548 add_object_entry(&oid, 0, p + 1, 0);2549 }2550}25512552/* Remember to update object flag allocation in object.h */2553#define OBJECT_ADDED (1u<<20)25542555static void show_commit(struct commit *commit, void *data)2556{2557 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);2558 commit->object.flags |= OBJECT_ADDED;25592560 if (write_bitmap_index)2561 index_commit_for_bitmap(commit);2562}25632564static void show_object(struct object *obj, const char *name, void *data)2565{2566 add_preferred_base_object(name);2567 add_object_entry(&obj->oid, obj->type, name, 0);2568 obj->flags |= OBJECT_ADDED;2569}25702571static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)2572{2573 assert(arg_missing_action == MA_ALLOW_ANY);25742575 /*2576 * Quietly ignore ALL missing objects. This avoids problems with2577 * staging them now and getting an odd error later.2578 */2579 if (!has_object_file(&obj->oid))2580 return;25812582 show_object(obj, name, data);2583}25842585static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)2586{2587 assert(arg_missing_action == MA_ALLOW_PROMISOR);25882589 /*2590 * Quietly ignore EXPECTED missing objects. This avoids problems with2591 * staging them now and getting an odd error later.2592 */2593 if (!has_object_file(&obj->oid) && is_promisor_object(&obj->oid))2594 return;25952596 show_object(obj, name, data);2597}25982599static int option_parse_missing_action(const struct option *opt,2600 const char *arg, int unset)2601{2602 assert(arg);2603 assert(!unset);26042605 if (!strcmp(arg, "error")) {2606 arg_missing_action = MA_ERROR;2607 fn_show_object = show_object;2608 return 0;2609 }26102611 if (!strcmp(arg, "allow-any")) {2612 arg_missing_action = MA_ALLOW_ANY;2613 fetch_if_missing = 0;2614 fn_show_object = show_object__ma_allow_any;2615 return 0;2616 }26172618 if (!strcmp(arg, "allow-promisor")) {2619 arg_missing_action = MA_ALLOW_PROMISOR;2620 fetch_if_missing = 0;2621 fn_show_object = show_object__ma_allow_promisor;2622 return 0;2623 }26242625 die(_("invalid value for --missing"));2626 return 0;2627}26282629static void show_edge(struct commit *commit)2630{2631 add_preferred_base(&commit->object.oid);2632}26332634struct in_pack_object {2635 off_t offset;2636 struct object *object;2637};26382639struct in_pack {2640 unsigned int alloc;2641 unsigned int nr;2642 struct in_pack_object *array;2643};26442645static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)2646{2647 in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);2648 in_pack->array[in_pack->nr].object = object;2649 in_pack->nr++;2650}26512652/*2653 * Compare the objects in the offset order, in order to emulate the2654 * "git rev-list --objects" output that produced the pack originally.2655 */2656static int ofscmp(const void *a_, const void *b_)2657{2658 struct in_pack_object *a = (struct in_pack_object *)a_;2659 struct in_pack_object *b = (struct in_pack_object *)b_;26602661 if (a->offset < b->offset)2662 return -1;2663 else if (a->offset > b->offset)2664 return 1;2665 else2666 return oidcmp(&a->object->oid, &b->object->oid);2667}26682669static void add_objects_in_unpacked_packs(struct rev_info *revs)2670{2671 struct packed_git *p;2672 struct in_pack in_pack;2673 uint32_t i;26742675 memset(&in_pack, 0, sizeof(in_pack));26762677 for (p = get_packed_git(the_repository); p; p = p->next) {2678 struct object_id oid;2679 struct object *o;26802681 if (!p->pack_local || p->pack_keep)2682 continue;2683 if (open_pack_index(p))2684 die("cannot open pack index");26852686 ALLOC_GROW(in_pack.array,2687 in_pack.nr + p->num_objects,2688 in_pack.alloc);26892690 for (i = 0; i < p->num_objects; i++) {2691 nth_packed_object_oid(&oid, p, i);2692 o = lookup_unknown_object(oid.hash);2693 if (!(o->flags & OBJECT_ADDED))2694 mark_in_pack_object(o, p, &in_pack);2695 o->flags |= OBJECT_ADDED;2696 }2697 }26982699 if (in_pack.nr) {2700 QSORT(in_pack.array, in_pack.nr, ofscmp);2701 for (i = 0; i < in_pack.nr; i++) {2702 struct object *o = in_pack.array[i].object;2703 add_object_entry(&o->oid, o->type, "", 0);2704 }2705 }2706 free(in_pack.array);2707}27082709static int add_loose_object(const struct object_id *oid, const char *path,2710 void *data)2711{2712 enum object_type type = oid_object_info(the_repository, oid, NULL);27132714 if (type < 0) {2715 warning("loose object at %s could not be examined", path);2716 return 0;2717 }27182719 add_object_entry(oid, type, "", 0);2720 return 0;2721}27222723/*2724 * We actually don't even have to worry about reachability here.2725 * add_object_entry will weed out duplicates, so we just add every2726 * loose object we find.2727 */2728static void add_unreachable_loose_objects(void)2729{2730 for_each_loose_file_in_objdir(get_object_directory(),2731 add_loose_object,2732 NULL, NULL, NULL);2733}27342735static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2736{2737 static struct packed_git *last_found = (void *)1;2738 struct packed_git *p;27392740 p = (last_found != (void *)1) ? last_found :2741 get_packed_git(the_repository);27422743 while (p) {2744 if ((!p->pack_local || p->pack_keep) &&2745 find_pack_entry_one(oid->hash, p)) {2746 last_found = p;2747 return 1;2748 }2749 if (p == last_found)2750 p = get_packed_git(the_repository);2751 else2752 p = p->next;2753 if (p == last_found)2754 p = p->next;2755 }2756 return 0;2757}27582759/*2760 * Store a list of sha1s that are should not be discarded2761 * because they are either written too recently, or are2762 * reachable from another object that was.2763 *2764 * This is filled by get_object_list.2765 */2766static struct oid_array recent_objects;27672768static int loosened_object_can_be_discarded(const struct object_id *oid,2769 timestamp_t mtime)2770{2771 if (!unpack_unreachable_expiration)2772 return 0;2773 if (mtime > unpack_unreachable_expiration)2774 return 0;2775 if (oid_array_lookup(&recent_objects, oid) >= 0)2776 return 0;2777 return 1;2778}27792780static void loosen_unused_packed_objects(struct rev_info *revs)2781{2782 struct packed_git *p;2783 uint32_t i;2784 struct object_id oid;27852786 for (p = get_packed_git(the_repository); p; p = p->next) {2787 if (!p->pack_local || p->pack_keep)2788 continue;27892790 if (open_pack_index(p))2791 die("cannot open pack index");27922793 for (i = 0; i < p->num_objects; i++) {2794 nth_packed_object_oid(&oid, p, i);2795 if (!packlist_find(&to_pack, oid.hash, NULL) &&2796 !has_sha1_pack_kept_or_nonlocal(&oid) &&2797 !loosened_object_can_be_discarded(&oid, p->mtime))2798 if (force_object_loose(&oid, p->mtime))2799 die("unable to force loose object");2800 }2801 }2802}28032804/*2805 * This tracks any options which pack-reuse code expects to be on, or which a2806 * reader of the pack might not understand, and which would therefore prevent2807 * blind reuse of what we have on disk.2808 */2809static int pack_options_allow_reuse(void)2810{2811 return pack_to_stdout &&2812 allow_ofs_delta &&2813 !ignore_packed_keep &&2814 (!local || !have_non_local_packs) &&2815 !incremental;2816}28172818static int get_object_list_from_bitmap(struct rev_info *revs)2819{2820 if (prepare_bitmap_walk(revs) < 0)2821 return -1;28222823 if (pack_options_allow_reuse() &&2824 !reuse_partial_packfile_from_bitmap(2825 &reuse_packfile,2826 &reuse_packfile_objects,2827 &reuse_packfile_offset)) {2828 assert(reuse_packfile_objects);2829 nr_result += reuse_packfile_objects;2830 display_progress(progress_state, nr_result);2831 }28322833 traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2834 return 0;2835}28362837static void record_recent_object(struct object *obj,2838 const char *name,2839 void *data)2840{2841 oid_array_append(&recent_objects, &obj->oid);2842}28432844static void record_recent_commit(struct commit *commit, void *data)2845{2846 oid_array_append(&recent_objects, &commit->object.oid);2847}28482849static void get_object_list(int ac, const char **av)2850{2851 struct rev_info revs;2852 char line[1000];2853 int flags = 0;28542855 init_revisions(&revs, NULL);2856 save_commit_buffer = 0;2857 setup_revisions(ac, av, &revs, NULL);28582859 /* make sure shallows are read */2860 is_repository_shallow();28612862 while (fgets(line, sizeof(line), stdin) != NULL) {2863 int len = strlen(line);2864 if (len && line[len - 1] == '\n')2865 line[--len] = 0;2866 if (!len)2867 break;2868 if (*line == '-') {2869 if (!strcmp(line, "--not")) {2870 flags ^= UNINTERESTING;2871 write_bitmap_index = 0;2872 continue;2873 }2874 if (starts_with(line, "--shallow ")) {2875 struct object_id oid;2876 if (get_oid_hex(line + 10, &oid))2877 die("not an SHA-1 '%s'", line + 10);2878 register_shallow(&oid);2879 use_bitmap_index = 0;2880 continue;2881 }2882 die("not a rev '%s'", line);2883 }2884 if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2885 die("bad revision '%s'", line);2886 }28872888 if (use_bitmap_index && !get_object_list_from_bitmap(&revs))2889 return;28902891 if (prepare_revision_walk(&revs))2892 die("revision walk setup failed");2893 mark_edges_uninteresting(&revs, show_edge);28942895 if (!fn_show_object)2896 fn_show_object = show_object;2897 traverse_commit_list_filtered(&filter_options, &revs,2898 show_commit, fn_show_object, NULL,2899 NULL);29002901 if (unpack_unreachable_expiration) {2902 revs.ignore_missing_links = 1;2903 if (add_unseen_recent_objects_to_traversal(&revs,2904 unpack_unreachable_expiration))2905 die("unable to add recent objects");2906 if (prepare_revision_walk(&revs))2907 die("revision walk setup failed");2908 traverse_commit_list(&revs, record_recent_commit,2909 record_recent_object, NULL);2910 }29112912 if (keep_unreachable)2913 add_objects_in_unpacked_packs(&revs);2914 if (pack_loose_unreachable)2915 add_unreachable_loose_objects();2916 if (unpack_unreachable)2917 loosen_unused_packed_objects(&revs);29182919 oid_array_clear(&recent_objects);2920}29212922static int option_parse_index_version(const struct option *opt,2923 const char *arg, int unset)2924{2925 char *c;2926 const char *val = arg;2927 pack_idx_opts.version = strtoul(val, &c, 10);2928 if (pack_idx_opts.version > 2)2929 die(_("unsupported index version %s"), val);2930 if (*c == ',' && c[1])2931 pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);2932 if (*c || pack_idx_opts.off32_limit & 0x80000000)2933 die(_("bad index version '%s'"), val);2934 return 0;2935}29362937static int option_parse_unpack_unreachable(const struct option *opt,2938 const char *arg, int unset)2939{2940 if (unset) {2941 unpack_unreachable = 0;2942 unpack_unreachable_expiration = 0;2943 }2944 else {2945 unpack_unreachable = 1;2946 if (arg)2947 unpack_unreachable_expiration = approxidate(arg);2948 }2949 return 0;2950}29512952int cmd_pack_objects(int argc, const char **argv, const char *prefix)2953{2954 int use_internal_rev_list = 0;2955 int thin = 0;2956 int shallow = 0;2957 int all_progress_implied = 0;2958 struct argv_array rp = ARGV_ARRAY_INIT;2959 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;2960 int rev_list_index = 0;2961 struct option pack_objects_options[] = {2962 OPT_SET_INT('q', "quiet", &progress,2963 N_("do not show progress meter"), 0),2964 OPT_SET_INT(0, "progress", &progress,2965 N_("show progress meter"), 1),2966 OPT_SET_INT(0, "all-progress", &progress,2967 N_("show progress meter during object writing phase"), 2),2968 OPT_BOOL(0, "all-progress-implied",2969 &all_progress_implied,2970 N_("similar to --all-progress when progress meter is shown")),2971 { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"),2972 N_("write the pack index file in the specified idx format version"),2973 0, option_parse_index_version },2974 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,2975 N_("maximum size of each output pack file")),2976 OPT_BOOL(0, "local", &local,2977 N_("ignore borrowed objects from alternate object store")),2978 OPT_BOOL(0, "incremental", &incremental,2979 N_("ignore packed objects")),2980 OPT_INTEGER(0, "window", &window,2981 N_("limit pack window by objects")),2982 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,2983 N_("limit pack window by memory in addition to object limit")),2984 OPT_INTEGER(0, "depth", &depth,2985 N_("maximum length of delta chain allowed in the resulting pack")),2986 OPT_BOOL(0, "reuse-delta", &reuse_delta,2987 N_("reuse existing deltas")),2988 OPT_BOOL(0, "reuse-object", &reuse_object,2989 N_("reuse existing objects")),2990 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,2991 N_("use OFS_DELTA objects")),2992 OPT_INTEGER(0, "threads", &delta_search_threads,2993 N_("use threads when searching for best delta matches")),2994 OPT_BOOL(0, "non-empty", &non_empty,2995 N_("do not create an empty pack output")),2996 OPT_BOOL(0, "revs", &use_internal_rev_list,2997 N_("read revision arguments from standard input")),2998 { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL,2999 N_("limit the objects to those that are not yet packed"),3000 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3001 { OPTION_SET_INT, 0, "all", &rev_list_all, NULL,3002 N_("include objects reachable from any reference"),3003 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3004 { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL,3005 N_("include objects referred by reflog entries"),3006 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3007 { OPTION_SET_INT, 0, "indexed-objects", &rev_list_index, NULL,3008 N_("include objects referred to by the index"),3009 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3010 OPT_BOOL(0, "stdout", &pack_to_stdout,3011 N_("output pack to stdout")),3012 OPT_BOOL(0, "include-tag", &include_tag,3013 N_("include tag objects that refer to objects to be packed")),3014 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,3015 N_("keep unreachable objects")),3016 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,3017 N_("pack loose unreachable objects")),3018 { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"),3019 N_("unpack unreachable objects newer than <time>"),3020 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3021 OPT_BOOL(0, "thin", &thin,3022 N_("create thin packs")),3023 OPT_BOOL(0, "shallow", &shallow,3024 N_("create packs suitable for shallow fetches")),3025 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep,3026 N_("ignore packs that have companion .keep file")),3027 OPT_INTEGER(0, "compression", &pack_compression_level,3028 N_("pack compression level")),3029 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents,3030 N_("do not hide commits by grafts"), 0),3031 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,3032 N_("use a bitmap index if available to speed up counting objects")),3033 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index,3034 N_("write a bitmap index together with the pack index")),3035 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3036 { OPTION_CALLBACK, 0, "missing", NULL, N_("action"),3037 N_("handling for missing objects"), PARSE_OPT_NONEG,3038 option_parse_missing_action },3039 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,3040 N_("do not pack objects in promisor packfiles")),3041 OPT_END(),3042 };30433044 check_replace_refs = 0;30453046 reset_pack_idx_option(&pack_idx_opts);3047 git_config(git_pack_config, NULL);30483049 progress = isatty(2);3050 argc = parse_options(argc, argv, prefix, pack_objects_options,3051 pack_usage, 0);30523053 if (argc) {3054 base_name = argv[0];3055 argc--;3056 }3057 if (pack_to_stdout != !base_name || argc)3058 usage_with_options(pack_usage, pack_objects_options);30593060 argv_array_push(&rp, "pack-objects");3061 if (thin) {3062 use_internal_rev_list = 1;3063 argv_array_push(&rp, shallow3064 ? "--objects-edge-aggressive"3065 : "--objects-edge");3066 } else3067 argv_array_push(&rp, "--objects");30683069 if (rev_list_all) {3070 use_internal_rev_list = 1;3071 argv_array_push(&rp, "--all");3072 }3073 if (rev_list_reflog) {3074 use_internal_rev_list = 1;3075 argv_array_push(&rp, "--reflog");3076 }3077 if (rev_list_index) {3078 use_internal_rev_list = 1;3079 argv_array_push(&rp, "--indexed-objects");3080 }3081 if (rev_list_unpacked) {3082 use_internal_rev_list = 1;3083 argv_array_push(&rp, "--unpacked");3084 }30853086 if (exclude_promisor_objects) {3087 use_internal_rev_list = 1;3088 fetch_if_missing = 0;3089 argv_array_push(&rp, "--exclude-promisor-objects");3090 }30913092 if (!reuse_object)3093 reuse_delta = 0;3094 if (pack_compression_level == -1)3095 pack_compression_level = Z_DEFAULT_COMPRESSION;3096 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)3097 die("bad pack compression level %d", pack_compression_level);30983099 if (!delta_search_threads) /* --threads=0 means autodetect */3100 delta_search_threads = online_cpus();31013102#ifdef NO_PTHREADS3103 if (delta_search_threads != 1)3104 warning("no threads support, ignoring --threads");3105#endif3106 if (!pack_to_stdout && !pack_size_limit)3107 pack_size_limit = pack_size_limit_cfg;3108 if (pack_to_stdout && pack_size_limit)3109 die("--max-pack-size cannot be used to build a pack for transfer.");3110 if (pack_size_limit && pack_size_limit < 1024*1024) {3111 warning("minimum pack size limit is 1 MiB");3112 pack_size_limit = 1024*1024;3113 }31143115 if (!pack_to_stdout && thin)3116 die("--thin cannot be used to build an indexable pack.");31173118 if (keep_unreachable && unpack_unreachable)3119 die("--keep-unreachable and --unpack-unreachable are incompatible.");3120 if (!rev_list_all || !rev_list_reflog || !rev_list_index)3121 unpack_unreachable_expiration = 0;31223123 if (filter_options.choice) {3124 if (!pack_to_stdout)3125 die("cannot use --filter without --stdout.");3126 use_bitmap_index = 0;3127 }31283129 /*3130 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3131 *3132 * - to produce good pack (with bitmap index not-yet-packed objects are3133 * packed in suboptimal order).3134 *3135 * - to use more robust pack-generation codepath (avoiding possible3136 * bugs in bitmap code and possible bitmap index corruption).3137 */3138 if (!pack_to_stdout)3139 use_bitmap_index_default = 0;31403141 if (use_bitmap_index < 0)3142 use_bitmap_index = use_bitmap_index_default;31433144 /* "hard" reasons not to use bitmaps; these just won't work at all */3145 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow())3146 use_bitmap_index = 0;31473148 if (pack_to_stdout || !rev_list_all)3149 write_bitmap_index = 0;31503151 if (progress && all_progress_implied)3152 progress = 2;31533154 if (ignore_packed_keep) {3155 struct packed_git *p;3156 for (p = get_packed_git(the_repository); p; p = p->next)3157 if (p->pack_local && p->pack_keep)3158 break;3159 if (!p) /* no keep-able packs found */3160 ignore_packed_keep = 0;3161 }3162 if (local) {3163 /*3164 * unlike ignore_packed_keep above, we do not want to3165 * unset "local" based on looking at packs, as it3166 * also covers non-local objects3167 */3168 struct packed_git *p;3169 for (p = get_packed_git(the_repository); p; p = p->next) {3170 if (!p->pack_local) {3171 have_non_local_packs = 1;3172 break;3173 }3174 }3175 }31763177 if (progress)3178 progress_state = start_progress(_("Counting objects"), 0);3179 if (!use_internal_rev_list)3180 read_object_list_from_stdin();3181 else {3182 get_object_list(rp.argc, rp.argv);3183 argv_array_clear(&rp);3184 }3185 cleanup_preferred_base();3186 if (include_tag && nr_result)3187 for_each_ref(add_ref_tag, NULL);3188 stop_progress(&progress_state);31893190 if (non_empty && !nr_result)3191 return 0;3192 if (nr_result)3193 prepare_pack(window, depth);3194 write_pack_file();3195 if (progress)3196 fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32"),"3197 " reused %"PRIu32" (delta %"PRIu32")\n",3198 written, written_delta, reused, reused_delta);3199 return 0;3200}