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#include"dir.h" 34 35#define IN_PACK(obj) oe_in_pack(&to_pack, obj) 36#define SIZE(obj) oe_size(&to_pack, obj) 37#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size) 38#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj) 39#define DELTA(obj) oe_delta(&to_pack, obj) 40#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj) 41#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj) 42#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val) 43#define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid) 44#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val) 45#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val) 46#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val) 47 48static const char*pack_usage[] = { 49N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 50N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 51 NULL 52}; 53 54/* 55 * Objects we are going to pack are collected in the `to_pack` structure. 56 * It contains an array (dynamically expanded) of the object data, and a map 57 * that can resolve SHA1s to their position in the array. 58 */ 59static struct packing_data to_pack; 60 61static struct pack_idx_entry **written_list; 62static uint32_t nr_result, nr_written, nr_seen; 63static struct bitmap_index *bitmap_git; 64 65static int non_empty; 66static int reuse_delta =1, reuse_object =1; 67static int keep_unreachable, unpack_unreachable, include_tag; 68static timestamp_t unpack_unreachable_expiration; 69static int pack_loose_unreachable; 70static int local; 71static int have_non_local_packs; 72static int incremental; 73static int ignore_packed_keep_on_disk; 74static int ignore_packed_keep_in_core; 75static int allow_ofs_delta; 76static struct pack_idx_option pack_idx_opts; 77static const char*base_name; 78static int progress =1; 79static int window =10; 80static unsigned long pack_size_limit; 81static int depth =50; 82static int delta_search_threads; 83static int pack_to_stdout; 84static int thin; 85static int num_preferred_base; 86static struct progress *progress_state; 87 88static struct packed_git *reuse_packfile; 89static uint32_t reuse_packfile_objects; 90static off_t reuse_packfile_offset; 91 92static int use_bitmap_index_default =1; 93static int use_bitmap_index = -1; 94static int write_bitmap_index; 95static uint16_t write_bitmap_options; 96 97static int exclude_promisor_objects; 98 99static unsigned long delta_cache_size =0; 100static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; 101static unsigned long cache_max_small_delta_size =1000; 102 103static unsigned long window_memory_limit =0; 104 105static struct list_objects_filter_options filter_options; 106 107enum missing_action { 108 MA_ERROR =0,/* fail if any missing objects are encountered */ 109 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 110 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 111}; 112static enum missing_action arg_missing_action; 113static show_object_fn fn_show_object; 114 115/* 116 * stats 117 */ 118static uint32_t written, written_delta; 119static uint32_t reused, reused_delta; 120 121/* 122 * Indexed commits 123 */ 124static struct commit **indexed_commits; 125static unsigned int indexed_commits_nr; 126static unsigned int indexed_commits_alloc; 127 128static voidindex_commit_for_bitmap(struct commit *commit) 129{ 130if(indexed_commits_nr >= indexed_commits_alloc) { 131 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 132REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 133} 134 135 indexed_commits[indexed_commits_nr++] = commit; 136} 137 138static void*get_delta(struct object_entry *entry) 139{ 140unsigned long size, base_size, delta_size; 141void*buf, *base_buf, *delta_buf; 142enum object_type type; 143 144 buf =read_object_file(&entry->idx.oid, &type, &size); 145if(!buf) 146die(_("unable to read%s"),oid_to_hex(&entry->idx.oid)); 147 base_buf =read_object_file(&DELTA(entry)->idx.oid, &type, 148&base_size); 149if(!base_buf) 150die("unable to read%s", 151oid_to_hex(&DELTA(entry)->idx.oid)); 152 delta_buf =diff_delta(base_buf, base_size, 153 buf, size, &delta_size,0); 154/* 155 * We succesfully computed this delta once but dropped it for 156 * memory reasons. Something is very wrong if this time we 157 * recompute and create a different delta. 158 */ 159if(!delta_buf || delta_size !=DELTA_SIZE(entry)) 160BUG("delta size changed"); 161free(buf); 162free(base_buf); 163return delta_buf; 164} 165 166static unsigned longdo_compress(void**pptr,unsigned long size) 167{ 168 git_zstream stream; 169void*in, *out; 170unsigned long maxsize; 171 172git_deflate_init(&stream, pack_compression_level); 173 maxsize =git_deflate_bound(&stream, size); 174 175 in = *pptr; 176 out =xmalloc(maxsize); 177*pptr = out; 178 179 stream.next_in = in; 180 stream.avail_in = size; 181 stream.next_out = out; 182 stream.avail_out = maxsize; 183while(git_deflate(&stream, Z_FINISH) == Z_OK) 184;/* nothing */ 185git_deflate_end(&stream); 186 187free(in); 188return stream.total_out; 189} 190 191static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 192const struct object_id *oid) 193{ 194 git_zstream stream; 195unsigned char ibuf[1024*16]; 196unsigned char obuf[1024*16]; 197unsigned long olen =0; 198 199git_deflate_init(&stream, pack_compression_level); 200 201for(;;) { 202 ssize_t readlen; 203int zret = Z_OK; 204 readlen =read_istream(st, ibuf,sizeof(ibuf)); 205if(readlen == -1) 206die(_("unable to read%s"),oid_to_hex(oid)); 207 208 stream.next_in = ibuf; 209 stream.avail_in = readlen; 210while((stream.avail_in || readlen ==0) && 211(zret == Z_OK || zret == Z_BUF_ERROR)) { 212 stream.next_out = obuf; 213 stream.avail_out =sizeof(obuf); 214 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 215hashwrite(f, obuf, stream.next_out - obuf); 216 olen += stream.next_out - obuf; 217} 218if(stream.avail_in) 219die(_("deflate error (%d)"), zret); 220if(readlen ==0) { 221if(zret != Z_STREAM_END) 222die(_("deflate error (%d)"), zret); 223break; 224} 225} 226git_deflate_end(&stream); 227return olen; 228} 229 230/* 231 * we are going to reuse the existing object data as is. make 232 * sure it is not corrupt. 233 */ 234static intcheck_pack_inflate(struct packed_git *p, 235struct pack_window **w_curs, 236 off_t offset, 237 off_t len, 238unsigned long expect) 239{ 240 git_zstream stream; 241unsigned char fakebuf[4096], *in; 242int st; 243 244memset(&stream,0,sizeof(stream)); 245git_inflate_init(&stream); 246do{ 247 in =use_pack(p, w_curs, offset, &stream.avail_in); 248 stream.next_in = in; 249 stream.next_out = fakebuf; 250 stream.avail_out =sizeof(fakebuf); 251 st =git_inflate(&stream, Z_FINISH); 252 offset += stream.next_in - in; 253}while(st == Z_OK || st == Z_BUF_ERROR); 254git_inflate_end(&stream); 255return(st == Z_STREAM_END && 256 stream.total_out == expect && 257 stream.total_in == len) ?0: -1; 258} 259 260static voidcopy_pack_data(struct hashfile *f, 261struct packed_git *p, 262struct pack_window **w_curs, 263 off_t offset, 264 off_t len) 265{ 266unsigned char*in; 267unsigned long avail; 268 269while(len) { 270 in =use_pack(p, w_curs, offset, &avail); 271if(avail > len) 272 avail = (unsigned long)len; 273hashwrite(f, in, avail); 274 offset += avail; 275 len -= avail; 276} 277} 278 279/* Return 0 if we will bust the pack-size limit */ 280static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 281unsigned long limit,int usable_delta) 282{ 283unsigned long size, datalen; 284unsigned char header[MAX_PACK_OBJECT_HEADER], 285 dheader[MAX_PACK_OBJECT_HEADER]; 286unsigned hdrlen; 287enum object_type type; 288void*buf; 289struct git_istream *st = NULL; 290const unsigned hashsz = the_hash_algo->rawsz; 291 292if(!usable_delta) { 293if(oe_type(entry) == OBJ_BLOB && 294oe_size_greater_than(&to_pack, entry, big_file_threshold) && 295(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 296 buf = NULL; 297else{ 298 buf =read_object_file(&entry->idx.oid, &type, &size); 299if(!buf) 300die(_("unable to read%s"), 301oid_to_hex(&entry->idx.oid)); 302} 303/* 304 * make sure no cached delta data remains from a 305 * previous attempt before a pack split occurred. 306 */ 307FREE_AND_NULL(entry->delta_data); 308 entry->z_delta_size =0; 309}else if(entry->delta_data) { 310 size =DELTA_SIZE(entry); 311 buf = entry->delta_data; 312 entry->delta_data = NULL; 313 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 314 OBJ_OFS_DELTA : OBJ_REF_DELTA; 315}else{ 316 buf =get_delta(entry); 317 size =DELTA_SIZE(entry); 318 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 319 OBJ_OFS_DELTA : OBJ_REF_DELTA; 320} 321 322if(st)/* large blob case, just assume we don't compress well */ 323 datalen = size; 324else if(entry->z_delta_size) 325 datalen = entry->z_delta_size; 326else 327 datalen =do_compress(&buf, size); 328 329/* 330 * The object header is a byte of 'type' followed by zero or 331 * more bytes of length. 332 */ 333 hdrlen =encode_in_pack_object_header(header,sizeof(header), 334 type, size); 335 336if(type == OBJ_OFS_DELTA) { 337/* 338 * Deltas with relative base contain an additional 339 * encoding of the relative offset for the delta 340 * base from this object's position in the pack. 341 */ 342 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 343unsigned pos =sizeof(dheader) -1; 344 dheader[pos] = ofs &127; 345while(ofs >>=7) 346 dheader[--pos] =128| (--ofs &127); 347if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 348if(st) 349close_istream(st); 350free(buf); 351return0; 352} 353hashwrite(f, header, hdrlen); 354hashwrite(f, dheader + pos,sizeof(dheader) - pos); 355 hdrlen +=sizeof(dheader) - pos; 356}else if(type == OBJ_REF_DELTA) { 357/* 358 * Deltas with a base reference contain 359 * additional bytes for the base object ID. 360 */ 361if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 362if(st) 363close_istream(st); 364free(buf); 365return0; 366} 367hashwrite(f, header, hdrlen); 368hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 369 hdrlen += hashsz; 370}else{ 371if(limit && hdrlen + datalen + hashsz >= limit) { 372if(st) 373close_istream(st); 374free(buf); 375return0; 376} 377hashwrite(f, header, hdrlen); 378} 379if(st) { 380 datalen =write_large_blob_data(st, f, &entry->idx.oid); 381close_istream(st); 382}else{ 383hashwrite(f, buf, datalen); 384free(buf); 385} 386 387return hdrlen + datalen; 388} 389 390/* Return 0 if we will bust the pack-size limit */ 391static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 392unsigned long limit,int usable_delta) 393{ 394struct packed_git *p =IN_PACK(entry); 395struct pack_window *w_curs = NULL; 396struct revindex_entry *revidx; 397 off_t offset; 398enum object_type type =oe_type(entry); 399 off_t datalen; 400unsigned char header[MAX_PACK_OBJECT_HEADER], 401 dheader[MAX_PACK_OBJECT_HEADER]; 402unsigned hdrlen; 403const unsigned hashsz = the_hash_algo->rawsz; 404unsigned long entry_size =SIZE(entry); 405 406if(DELTA(entry)) 407 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 408 OBJ_OFS_DELTA : OBJ_REF_DELTA; 409 hdrlen =encode_in_pack_object_header(header,sizeof(header), 410 type, entry_size); 411 412 offset = entry->in_pack_offset; 413 revidx =find_pack_revindex(p, offset); 414 datalen = revidx[1].offset - offset; 415if(!pack_to_stdout && p->index_version >1&& 416check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 417error(_("bad packed object CRC for%s"), 418oid_to_hex(&entry->idx.oid)); 419unuse_pack(&w_curs); 420returnwrite_no_reuse_object(f, entry, limit, usable_delta); 421} 422 423 offset += entry->in_pack_header_size; 424 datalen -= entry->in_pack_header_size; 425 426if(!pack_to_stdout && p->index_version ==1&& 427check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 428error(_("corrupt packed object for%s"), 429oid_to_hex(&entry->idx.oid)); 430unuse_pack(&w_curs); 431returnwrite_no_reuse_object(f, entry, limit, usable_delta); 432} 433 434if(type == OBJ_OFS_DELTA) { 435 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 436unsigned pos =sizeof(dheader) -1; 437 dheader[pos] = ofs &127; 438while(ofs >>=7) 439 dheader[--pos] =128| (--ofs &127); 440if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 441unuse_pack(&w_curs); 442return0; 443} 444hashwrite(f, header, hdrlen); 445hashwrite(f, dheader + pos,sizeof(dheader) - pos); 446 hdrlen +=sizeof(dheader) - pos; 447 reused_delta++; 448}else if(type == OBJ_REF_DELTA) { 449if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 450unuse_pack(&w_curs); 451return0; 452} 453hashwrite(f, header, hdrlen); 454hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 455 hdrlen += hashsz; 456 reused_delta++; 457}else{ 458if(limit && hdrlen + datalen + hashsz >= limit) { 459unuse_pack(&w_curs); 460return0; 461} 462hashwrite(f, header, hdrlen); 463} 464copy_pack_data(f, p, &w_curs, offset, datalen); 465unuse_pack(&w_curs); 466 reused++; 467return hdrlen + datalen; 468} 469 470/* Return 0 if we will bust the pack-size limit */ 471static off_t write_object(struct hashfile *f, 472struct object_entry *entry, 473 off_t write_offset) 474{ 475unsigned long limit; 476 off_t len; 477int usable_delta, to_reuse; 478 479if(!pack_to_stdout) 480crc32_begin(f); 481 482/* apply size limit if limited packsize and not first object */ 483if(!pack_size_limit || !nr_written) 484 limit =0; 485else if(pack_size_limit <= write_offset) 486/* 487 * the earlier object did not fit the limit; avoid 488 * mistaking this with unlimited (i.e. limit = 0). 489 */ 490 limit =1; 491else 492 limit = pack_size_limit - write_offset; 493 494if(!DELTA(entry)) 495 usable_delta =0;/* no delta */ 496else if(!pack_size_limit) 497 usable_delta =1;/* unlimited packfile */ 498else if(DELTA(entry)->idx.offset == (off_t)-1) 499 usable_delta =0;/* base was written to another pack */ 500else if(DELTA(entry)->idx.offset) 501 usable_delta =1;/* base already exists in this pack */ 502else 503 usable_delta =0;/* base could end up in another pack */ 504 505if(!reuse_object) 506 to_reuse =0;/* explicit */ 507else if(!IN_PACK(entry)) 508 to_reuse =0;/* can't reuse what we don't have */ 509else if(oe_type(entry) == OBJ_REF_DELTA || 510oe_type(entry) == OBJ_OFS_DELTA) 511/* check_object() decided it for us ... */ 512 to_reuse = usable_delta; 513/* ... but pack split may override that */ 514else if(oe_type(entry) != entry->in_pack_type) 515 to_reuse =0;/* pack has delta which is unusable */ 516else if(DELTA(entry)) 517 to_reuse =0;/* we want to pack afresh */ 518else 519 to_reuse =1;/* we have it in-pack undeltified, 520 * and we do not need to deltify it. 521 */ 522 523if(!to_reuse) 524 len =write_no_reuse_object(f, entry, limit, usable_delta); 525else 526 len =write_reuse_object(f, entry, limit, usable_delta); 527if(!len) 528return0; 529 530if(usable_delta) 531 written_delta++; 532 written++; 533if(!pack_to_stdout) 534 entry->idx.crc32 =crc32_end(f); 535return len; 536} 537 538enum write_one_status { 539 WRITE_ONE_SKIP = -1,/* already written */ 540 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 541 WRITE_ONE_WRITTEN =1,/* normal */ 542 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 543}; 544 545static enum write_one_status write_one(struct hashfile *f, 546struct object_entry *e, 547 off_t *offset) 548{ 549 off_t size; 550int recursing; 551 552/* 553 * we set offset to 1 (which is an impossible value) to mark 554 * the fact that this object is involved in "write its base 555 * first before writing a deltified object" recursion. 556 */ 557 recursing = (e->idx.offset ==1); 558if(recursing) { 559warning(_("recursive delta detected for object%s"), 560oid_to_hex(&e->idx.oid)); 561return WRITE_ONE_RECURSIVE; 562}else if(e->idx.offset || e->preferred_base) { 563/* offset is non zero if object is written already. */ 564return WRITE_ONE_SKIP; 565} 566 567/* if we are deltified, write out base object first. */ 568if(DELTA(e)) { 569 e->idx.offset =1;/* now recurse */ 570switch(write_one(f,DELTA(e), offset)) { 571case WRITE_ONE_RECURSIVE: 572/* we cannot depend on this one */ 573SET_DELTA(e, NULL); 574break; 575default: 576break; 577case WRITE_ONE_BREAK: 578 e->idx.offset = recursing; 579return WRITE_ONE_BREAK; 580} 581} 582 583 e->idx.offset = *offset; 584 size =write_object(f, e, *offset); 585if(!size) { 586 e->idx.offset = recursing; 587return WRITE_ONE_BREAK; 588} 589 written_list[nr_written++] = &e->idx; 590 591/* make sure off_t is sufficiently large not to wrap */ 592if(signed_add_overflows(*offset, size)) 593die(_("pack too large for current definition of off_t")); 594*offset += size; 595return WRITE_ONE_WRITTEN; 596} 597 598static intmark_tagged(const char*path,const struct object_id *oid,int flag, 599void*cb_data) 600{ 601struct object_id peeled; 602struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 603 604if(entry) 605 entry->tagged =1; 606if(!peel_ref(path, &peeled)) { 607 entry =packlist_find(&to_pack, peeled.hash, NULL); 608if(entry) 609 entry->tagged =1; 610} 611return0; 612} 613 614staticinlinevoidadd_to_write_order(struct object_entry **wo, 615unsigned int*endp, 616struct object_entry *e) 617{ 618if(e->filled) 619return; 620 wo[(*endp)++] = e; 621 e->filled =1; 622} 623 624static voidadd_descendants_to_write_order(struct object_entry **wo, 625unsigned int*endp, 626struct object_entry *e) 627{ 628int add_to_order =1; 629while(e) { 630if(add_to_order) { 631struct object_entry *s; 632/* add this node... */ 633add_to_write_order(wo, endp, e); 634/* all its siblings... */ 635for(s =DELTA_SIBLING(e); s; s =DELTA_SIBLING(s)) { 636add_to_write_order(wo, endp, s); 637} 638} 639/* drop down a level to add left subtree nodes if possible */ 640if(DELTA_CHILD(e)) { 641 add_to_order =1; 642 e =DELTA_CHILD(e); 643}else{ 644 add_to_order =0; 645/* our sibling might have some children, it is next */ 646if(DELTA_SIBLING(e)) { 647 e =DELTA_SIBLING(e); 648continue; 649} 650/* go back to our parent node */ 651 e =DELTA(e); 652while(e && !DELTA_SIBLING(e)) { 653/* we're on the right side of a subtree, keep 654 * going up until we can go right again */ 655 e =DELTA(e); 656} 657if(!e) { 658/* done- we hit our original root node */ 659return; 660} 661/* pass it off to sibling at this level */ 662 e =DELTA_SIBLING(e); 663} 664}; 665} 666 667static voidadd_family_to_write_order(struct object_entry **wo, 668unsigned int*endp, 669struct object_entry *e) 670{ 671struct object_entry *root; 672 673for(root = e;DELTA(root); root =DELTA(root)) 674;/* nothing */ 675add_descendants_to_write_order(wo, endp, root); 676} 677 678static struct object_entry **compute_write_order(void) 679{ 680unsigned int i, wo_end, last_untagged; 681 682struct object_entry **wo; 683struct object_entry *objects = to_pack.objects; 684 685for(i =0; i < to_pack.nr_objects; i++) { 686 objects[i].tagged =0; 687 objects[i].filled =0; 688SET_DELTA_CHILD(&objects[i], NULL); 689SET_DELTA_SIBLING(&objects[i], NULL); 690} 691 692/* 693 * Fully connect delta_child/delta_sibling network. 694 * Make sure delta_sibling is sorted in the original 695 * recency order. 696 */ 697for(i = to_pack.nr_objects; i >0;) { 698struct object_entry *e = &objects[--i]; 699if(!DELTA(e)) 700continue; 701/* Mark me as the first child */ 702 e->delta_sibling_idx =DELTA(e)->delta_child_idx; 703SET_DELTA_CHILD(DELTA(e), e); 704} 705 706/* 707 * Mark objects that are at the tip of tags. 708 */ 709for_each_tag_ref(mark_tagged, NULL); 710 711/* 712 * Give the objects in the original recency order until 713 * we see a tagged tip. 714 */ 715ALLOC_ARRAY(wo, to_pack.nr_objects); 716for(i = wo_end =0; i < to_pack.nr_objects; i++) { 717if(objects[i].tagged) 718break; 719add_to_write_order(wo, &wo_end, &objects[i]); 720} 721 last_untagged = i; 722 723/* 724 * Then fill all the tagged tips. 725 */ 726for(; i < to_pack.nr_objects; i++) { 727if(objects[i].tagged) 728add_to_write_order(wo, &wo_end, &objects[i]); 729} 730 731/* 732 * And then all remaining commits and tags. 733 */ 734for(i = last_untagged; i < to_pack.nr_objects; i++) { 735if(oe_type(&objects[i]) != OBJ_COMMIT && 736oe_type(&objects[i]) != OBJ_TAG) 737continue; 738add_to_write_order(wo, &wo_end, &objects[i]); 739} 740 741/* 742 * And then all the trees. 743 */ 744for(i = last_untagged; i < to_pack.nr_objects; i++) { 745if(oe_type(&objects[i]) != OBJ_TREE) 746continue; 747add_to_write_order(wo, &wo_end, &objects[i]); 748} 749 750/* 751 * Finally all the rest in really tight order 752 */ 753for(i = last_untagged; i < to_pack.nr_objects; i++) { 754if(!objects[i].filled) 755add_family_to_write_order(wo, &wo_end, &objects[i]); 756} 757 758if(wo_end != to_pack.nr_objects) 759die(_("ordered%uobjects, expected %"PRIu32), 760 wo_end, to_pack.nr_objects); 761 762return wo; 763} 764 765static off_t write_reused_pack(struct hashfile *f) 766{ 767unsigned char buffer[8192]; 768 off_t to_write, total; 769int fd; 770 771if(!is_pack_valid(reuse_packfile)) 772die(_("packfile is invalid:%s"), reuse_packfile->pack_name); 773 774 fd =git_open(reuse_packfile->pack_name); 775if(fd <0) 776die_errno(_("unable to open packfile for reuse:%s"), 777 reuse_packfile->pack_name); 778 779if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 780die_errno(_("unable to seek in reused packfile")); 781 782if(reuse_packfile_offset <0) 783 reuse_packfile_offset = reuse_packfile->pack_size - the_hash_algo->rawsz; 784 785 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 786 787while(to_write) { 788int read_pack =xread(fd, buffer,sizeof(buffer)); 789 790if(read_pack <=0) 791die_errno(_("unable to read from reused packfile")); 792 793if(read_pack > to_write) 794 read_pack = to_write; 795 796hashwrite(f, buffer, read_pack); 797 to_write -= read_pack; 798 799/* 800 * We don't know the actual number of objects written, 801 * only how many bytes written, how many bytes total, and 802 * how many objects total. So we can fake it by pretending all 803 * objects we are writing are the same size. This gives us a 804 * smooth progress meter, and at the end it matches the true 805 * answer. 806 */ 807 written = reuse_packfile_objects * 808(((double)(total - to_write)) / total); 809display_progress(progress_state, written); 810} 811 812close(fd); 813 written = reuse_packfile_objects; 814display_progress(progress_state, written); 815return reuse_packfile_offset -sizeof(struct pack_header); 816} 817 818static const char no_split_warning[] =N_( 819"disabling bitmap writing, packs are split due to pack.packSizeLimit" 820); 821 822static voidwrite_pack_file(void) 823{ 824uint32_t i =0, j; 825struct hashfile *f; 826 off_t offset; 827uint32_t nr_remaining = nr_result; 828time_t last_mtime =0; 829struct object_entry **write_order; 830 831if(progress > pack_to_stdout) 832 progress_state =start_progress(_("Writing objects"), nr_result); 833ALLOC_ARRAY(written_list, to_pack.nr_objects); 834 write_order =compute_write_order(); 835 836do{ 837struct object_id oid; 838char*pack_tmp_name = NULL; 839 840if(pack_to_stdout) 841 f =hashfd_throughput(1,"<stdout>", progress_state); 842else 843 f =create_tmp_packfile(&pack_tmp_name); 844 845 offset =write_pack_header(f, nr_remaining); 846 847if(reuse_packfile) { 848 off_t packfile_size; 849assert(pack_to_stdout); 850 851 packfile_size =write_reused_pack(f); 852 offset += packfile_size; 853} 854 855 nr_written =0; 856for(; i < to_pack.nr_objects; i++) { 857struct object_entry *e = write_order[i]; 858if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 859break; 860display_progress(progress_state, written); 861} 862 863/* 864 * Did we write the wrong # entries in the header? 865 * If so, rewrite it like in fast-import 866 */ 867if(pack_to_stdout) { 868finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 869}else if(nr_written == nr_remaining) { 870finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 871}else{ 872int fd =finalize_hashfile(f, oid.hash,0); 873fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 874 nr_written, oid.hash, offset); 875close(fd); 876if(write_bitmap_index) { 877warning(_(no_split_warning)); 878 write_bitmap_index =0; 879} 880} 881 882if(!pack_to_stdout) { 883struct stat st; 884struct strbuf tmpname = STRBUF_INIT; 885 886/* 887 * Packs are runtime accessed in their mtime 888 * order since newer packs are more likely to contain 889 * younger objects. So if we are creating multiple 890 * packs then we should modify the mtime of later ones 891 * to preserve this property. 892 */ 893if(stat(pack_tmp_name, &st) <0) { 894warning_errno(_("failed to stat%s"), pack_tmp_name); 895}else if(!last_mtime) { 896 last_mtime = st.st_mtime; 897}else{ 898struct utimbuf utb; 899 utb.actime = st.st_atime; 900 utb.modtime = --last_mtime; 901if(utime(pack_tmp_name, &utb) <0) 902warning_errno(_("failed utime() on%s"), pack_tmp_name); 903} 904 905strbuf_addf(&tmpname,"%s-", base_name); 906 907if(write_bitmap_index) { 908bitmap_writer_set_checksum(oid.hash); 909bitmap_writer_build_type_index( 910&to_pack, written_list, nr_written); 911} 912 913finish_tmp_packfile(&tmpname, pack_tmp_name, 914 written_list, nr_written, 915&pack_idx_opts, oid.hash); 916 917if(write_bitmap_index) { 918strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 919 920stop_progress(&progress_state); 921 922bitmap_writer_show_progress(progress); 923bitmap_writer_reuse_bitmaps(&to_pack); 924bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 925bitmap_writer_build(&to_pack); 926bitmap_writer_finish(written_list, nr_written, 927 tmpname.buf, write_bitmap_options); 928 write_bitmap_index =0; 929} 930 931strbuf_release(&tmpname); 932free(pack_tmp_name); 933puts(oid_to_hex(&oid)); 934} 935 936/* mark written objects as written to previous pack */ 937for(j =0; j < nr_written; j++) { 938 written_list[j]->offset = (off_t)-1; 939} 940 nr_remaining -= nr_written; 941}while(nr_remaining && i < to_pack.nr_objects); 942 943free(written_list); 944free(write_order); 945stop_progress(&progress_state); 946if(written != nr_result) 947die(_("wrote %"PRIu32" objects while expecting %"PRIu32), 948 written, nr_result); 949} 950 951static intno_try_delta(const char*path) 952{ 953static struct attr_check *check; 954 955if(!check) 956 check =attr_check_initl("delta", NULL); 957if(git_check_attr(&the_index, path, check)) 958return0; 959if(ATTR_FALSE(check->items[0].value)) 960return1; 961return0; 962} 963 964/* 965 * When adding an object, check whether we have already added it 966 * to our packing list. If so, we can skip. However, if we are 967 * being asked to excludei t, but the previous mention was to include 968 * it, make sure to adjust its flags and tweak our numbers accordingly. 969 * 970 * As an optimization, we pass out the index position where we would have 971 * found the item, since that saves us from having to look it up again a 972 * few lines later when we want to add the new entry. 973 */ 974static inthave_duplicate_entry(const struct object_id *oid, 975int exclude, 976uint32_t*index_pos) 977{ 978struct object_entry *entry; 979 980 entry =packlist_find(&to_pack, oid->hash, index_pos); 981if(!entry) 982return0; 983 984if(exclude) { 985if(!entry->preferred_base) 986 nr_result--; 987 entry->preferred_base =1; 988} 989 990return1; 991} 992 993static intwant_found_object(int exclude,struct packed_git *p) 994{ 995if(exclude) 996return1; 997if(incremental) 998return0; 9991000/*1001 * When asked to do --local (do not include an object that appears in a1002 * pack we borrow from elsewhere) or --honor-pack-keep (do not include1003 * an object that appears in a pack marked with .keep), finding a pack1004 * that matches the criteria is sufficient for us to decide to omit it.1005 * However, even if this pack does not satisfy the criteria, we need to1006 * make sure no copy of this object appears in _any_ pack that makes us1007 * to omit the object, so we need to check all the packs.1008 *1009 * We can however first check whether these options can possible matter;1010 * if they do not matter we know we want the object in generated pack.1011 * Otherwise, we signal "-1" at the end to tell the caller that we do1012 * not know either way, and it needs to check more packs.1013 */1014if(!ignore_packed_keep_on_disk &&1015!ignore_packed_keep_in_core &&1016(!local || !have_non_local_packs))1017return1;10181019if(local && !p->pack_local)1020return0;1021if(p->pack_local &&1022((ignore_packed_keep_on_disk && p->pack_keep) ||1023(ignore_packed_keep_in_core && p->pack_keep_in_core)))1024return0;10251026/* we don't know yet; keep looking for more packs */1027return-1;1028}10291030/*1031 * Check whether we want the object in the pack (e.g., we do not want1032 * objects found in non-local stores if the "--local" option was used).1033 *1034 * If the caller already knows an existing pack it wants to take the object1035 * from, that is passed in *found_pack and *found_offset; otherwise this1036 * function finds if there is any pack that has the object and returns the pack1037 * and its offset in these variables.1038 */1039static intwant_object_in_pack(const struct object_id *oid,1040int exclude,1041struct packed_git **found_pack,1042 off_t *found_offset)1043{1044int want;1045struct list_head *pos;10461047if(!exclude && local &&has_loose_object_nonlocal(oid))1048return0;10491050/*1051 * If we already know the pack object lives in, start checks from that1052 * pack - in the usual case when neither --local was given nor .keep files1053 * are present we will determine the answer right now.1054 */1055if(*found_pack) {1056 want =want_found_object(exclude, *found_pack);1057if(want != -1)1058return want;1059}1060list_for_each(pos,get_packed_git_mru(the_repository)) {1061struct packed_git *p =list_entry(pos,struct packed_git, mru);1062 off_t offset;10631064if(p == *found_pack)1065 offset = *found_offset;1066else1067 offset =find_pack_entry_one(oid->hash, p);10681069if(offset) {1070if(!*found_pack) {1071if(!is_pack_valid(p))1072continue;1073*found_offset = offset;1074*found_pack = p;1075}1076 want =want_found_object(exclude, p);1077if(!exclude && want >0)1078list_move(&p->mru,1079get_packed_git_mru(the_repository));1080if(want != -1)1081return want;1082}1083}10841085return1;1086}10871088static voidcreate_object_entry(const struct object_id *oid,1089enum object_type type,1090uint32_t hash,1091int exclude,1092int no_try_delta,1093uint32_t index_pos,1094struct packed_git *found_pack,1095 off_t found_offset)1096{1097struct object_entry *entry;10981099 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1100 entry->hash = hash;1101oe_set_type(entry, type);1102if(exclude)1103 entry->preferred_base =1;1104else1105 nr_result++;1106if(found_pack) {1107oe_set_in_pack(&to_pack, entry, found_pack);1108 entry->in_pack_offset = found_offset;1109}11101111 entry->no_try_delta = no_try_delta;1112}11131114static const char no_closure_warning[] =N_(1115"disabling bitmap writing, as some objects are not being packed"1116);11171118static intadd_object_entry(const struct object_id *oid,enum object_type type,1119const char*name,int exclude)1120{1121struct packed_git *found_pack = NULL;1122 off_t found_offset =0;1123uint32_t index_pos;11241125display_progress(progress_state, ++nr_seen);11261127if(have_duplicate_entry(oid, exclude, &index_pos))1128return0;11291130if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1131/* The pack is missing an object, so it will not have closure */1132if(write_bitmap_index) {1133warning(_(no_closure_warning));1134 write_bitmap_index =0;1135}1136return0;1137}11381139create_object_entry(oid, type,pack_name_hash(name),1140 exclude, name &&no_try_delta(name),1141 index_pos, found_pack, found_offset);1142return1;1143}11441145static intadd_object_entry_from_bitmap(const struct object_id *oid,1146enum object_type type,1147int flags,uint32_t name_hash,1148struct packed_git *pack, off_t offset)1149{1150uint32_t index_pos;11511152display_progress(progress_state, ++nr_seen);11531154if(have_duplicate_entry(oid,0, &index_pos))1155return0;11561157if(!want_object_in_pack(oid,0, &pack, &offset))1158return0;11591160create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1161return1;1162}11631164struct pbase_tree_cache {1165struct object_id oid;1166int ref;1167int temporary;1168void*tree_data;1169unsigned long tree_size;1170};11711172static struct pbase_tree_cache *(pbase_tree_cache[256]);1173static intpbase_tree_cache_ix(const struct object_id *oid)1174{1175return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1176}1177static intpbase_tree_cache_ix_incr(int ix)1178{1179return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1180}11811182static struct pbase_tree {1183struct pbase_tree *next;1184/* This is a phony "cache" entry; we are not1185 * going to evict it or find it through _get()1186 * mechanism -- this is for the toplevel node that1187 * would almost always change with any commit.1188 */1189struct pbase_tree_cache pcache;1190} *pbase_tree;11911192static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1193{1194struct pbase_tree_cache *ent, *nent;1195void*data;1196unsigned long size;1197enum object_type type;1198int neigh;1199int my_ix =pbase_tree_cache_ix(oid);1200int available_ix = -1;12011202/* pbase-tree-cache acts as a limited hashtable.1203 * your object will be found at your index or within a few1204 * slots after that slot if it is cached.1205 */1206for(neigh =0; neigh <8; neigh++) {1207 ent = pbase_tree_cache[my_ix];1208if(ent && !oidcmp(&ent->oid, oid)) {1209 ent->ref++;1210return ent;1211}1212else if(((available_ix <0) && (!ent || !ent->ref)) ||1213((0<= available_ix) &&1214(!ent && pbase_tree_cache[available_ix])))1215 available_ix = my_ix;1216if(!ent)1217break;1218 my_ix =pbase_tree_cache_ix_incr(my_ix);1219}12201221/* Did not find one. Either we got a bogus request or1222 * we need to read and perhaps cache.1223 */1224 data =read_object_file(oid, &type, &size);1225if(!data)1226return NULL;1227if(type != OBJ_TREE) {1228free(data);1229return NULL;1230}12311232/* We need to either cache or return a throwaway copy */12331234if(available_ix <0)1235 ent = NULL;1236else{1237 ent = pbase_tree_cache[available_ix];1238 my_ix = available_ix;1239}12401241if(!ent) {1242 nent =xmalloc(sizeof(*nent));1243 nent->temporary = (available_ix <0);1244}1245else{1246/* evict and reuse */1247free(ent->tree_data);1248 nent = ent;1249}1250oidcpy(&nent->oid, oid);1251 nent->tree_data = data;1252 nent->tree_size = size;1253 nent->ref =1;1254if(!nent->temporary)1255 pbase_tree_cache[my_ix] = nent;1256return nent;1257}12581259static voidpbase_tree_put(struct pbase_tree_cache *cache)1260{1261if(!cache->temporary) {1262 cache->ref--;1263return;1264}1265free(cache->tree_data);1266free(cache);1267}12681269static intname_cmp_len(const char*name)1270{1271int i;1272for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1273;1274return i;1275}12761277static voidadd_pbase_object(struct tree_desc *tree,1278const char*name,1279int cmplen,1280const char*fullname)1281{1282struct name_entry entry;1283int cmp;12841285while(tree_entry(tree,&entry)) {1286if(S_ISGITLINK(entry.mode))1287continue;1288 cmp =tree_entry_len(&entry) != cmplen ?1:1289memcmp(name, entry.path, cmplen);1290if(cmp >0)1291continue;1292if(cmp <0)1293return;1294if(name[cmplen] !='/') {1295add_object_entry(entry.oid,1296object_type(entry.mode),1297 fullname,1);1298return;1299}1300if(S_ISDIR(entry.mode)) {1301struct tree_desc sub;1302struct pbase_tree_cache *tree;1303const char*down = name+cmplen+1;1304int downlen =name_cmp_len(down);13051306 tree =pbase_tree_get(entry.oid);1307if(!tree)1308return;1309init_tree_desc(&sub, tree->tree_data, tree->tree_size);13101311add_pbase_object(&sub, down, downlen, fullname);1312pbase_tree_put(tree);1313}1314}1315}13161317static unsigned*done_pbase_paths;1318static int done_pbase_paths_num;1319static int done_pbase_paths_alloc;1320static intdone_pbase_path_pos(unsigned hash)1321{1322int lo =0;1323int hi = done_pbase_paths_num;1324while(lo < hi) {1325int mi = lo + (hi - lo) /2;1326if(done_pbase_paths[mi] == hash)1327return mi;1328if(done_pbase_paths[mi] < hash)1329 hi = mi;1330else1331 lo = mi +1;1332}1333return-lo-1;1334}13351336static intcheck_pbase_path(unsigned hash)1337{1338int pos =done_pbase_path_pos(hash);1339if(0<= pos)1340return1;1341 pos = -pos -1;1342ALLOC_GROW(done_pbase_paths,1343 done_pbase_paths_num +1,1344 done_pbase_paths_alloc);1345 done_pbase_paths_num++;1346if(pos < done_pbase_paths_num)1347MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1348 done_pbase_paths_num - pos -1);1349 done_pbase_paths[pos] = hash;1350return0;1351}13521353static voidadd_preferred_base_object(const char*name)1354{1355struct pbase_tree *it;1356int cmplen;1357unsigned hash =pack_name_hash(name);13581359if(!num_preferred_base ||check_pbase_path(hash))1360return;13611362 cmplen =name_cmp_len(name);1363for(it = pbase_tree; it; it = it->next) {1364if(cmplen ==0) {1365add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1366}1367else{1368struct tree_desc tree;1369init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1370add_pbase_object(&tree, name, cmplen, name);1371}1372}1373}13741375static voidadd_preferred_base(struct object_id *oid)1376{1377struct pbase_tree *it;1378void*data;1379unsigned long size;1380struct object_id tree_oid;13811382if(window <= num_preferred_base++)1383return;13841385 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1386if(!data)1387return;13881389for(it = pbase_tree; it; it = it->next) {1390if(!oidcmp(&it->pcache.oid, &tree_oid)) {1391free(data);1392return;1393}1394}13951396 it =xcalloc(1,sizeof(*it));1397 it->next = pbase_tree;1398 pbase_tree = it;13991400oidcpy(&it->pcache.oid, &tree_oid);1401 it->pcache.tree_data = data;1402 it->pcache.tree_size = size;1403}14041405static voidcleanup_preferred_base(void)1406{1407struct pbase_tree *it;1408unsigned i;14091410 it = pbase_tree;1411 pbase_tree = NULL;1412while(it) {1413struct pbase_tree *tmp = it;1414 it = tmp->next;1415free(tmp->pcache.tree_data);1416free(tmp);1417}14181419for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1420if(!pbase_tree_cache[i])1421continue;1422free(pbase_tree_cache[i]->tree_data);1423FREE_AND_NULL(pbase_tree_cache[i]);1424}14251426FREE_AND_NULL(done_pbase_paths);1427 done_pbase_paths_num = done_pbase_paths_alloc =0;1428}14291430static voidcheck_object(struct object_entry *entry)1431{1432unsigned long canonical_size;14331434if(IN_PACK(entry)) {1435struct packed_git *p =IN_PACK(entry);1436struct pack_window *w_curs = NULL;1437const unsigned char*base_ref = NULL;1438struct object_entry *base_entry;1439unsigned long used, used_0;1440unsigned long avail;1441 off_t ofs;1442unsigned char*buf, c;1443enum object_type type;1444unsigned long in_pack_size;14451446 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14471448/*1449 * We want in_pack_type even if we do not reuse delta1450 * since non-delta representations could still be reused.1451 */1452 used =unpack_object_header_buffer(buf, avail,1453&type,1454&in_pack_size);1455if(used ==0)1456goto give_up;14571458if(type <0)1459BUG("invalid type%d", type);1460 entry->in_pack_type = type;14611462/*1463 * Determine if this is a delta and if so whether we can1464 * reuse it or not. Otherwise let's find out as cheaply as1465 * possible what the actual type and size for this object is.1466 */1467switch(entry->in_pack_type) {1468default:1469/* Not a delta hence we've already got all we need. */1470oe_set_type(entry, entry->in_pack_type);1471SET_SIZE(entry, in_pack_size);1472 entry->in_pack_header_size = used;1473if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1474goto give_up;1475unuse_pack(&w_curs);1476return;1477case OBJ_REF_DELTA:1478if(reuse_delta && !entry->preferred_base)1479 base_ref =use_pack(p, &w_curs,1480 entry->in_pack_offset + used, NULL);1481 entry->in_pack_header_size = used + the_hash_algo->rawsz;1482break;1483case OBJ_OFS_DELTA:1484 buf =use_pack(p, &w_curs,1485 entry->in_pack_offset + used, NULL);1486 used_0 =0;1487 c = buf[used_0++];1488 ofs = c &127;1489while(c &128) {1490 ofs +=1;1491if(!ofs ||MSB(ofs,7)) {1492error(_("delta base offset overflow in pack for%s"),1493oid_to_hex(&entry->idx.oid));1494goto give_up;1495}1496 c = buf[used_0++];1497 ofs = (ofs <<7) + (c &127);1498}1499 ofs = entry->in_pack_offset - ofs;1500if(ofs <=0|| ofs >= entry->in_pack_offset) {1501error(_("delta base offset out of bound for%s"),1502oid_to_hex(&entry->idx.oid));1503goto give_up;1504}1505if(reuse_delta && !entry->preferred_base) {1506struct revindex_entry *revidx;1507 revidx =find_pack_revindex(p, ofs);1508if(!revidx)1509goto give_up;1510 base_ref =nth_packed_object_sha1(p, revidx->nr);1511}1512 entry->in_pack_header_size = used + used_0;1513break;1514}15151516if(base_ref && (1517(base_entry =packlist_find(&to_pack, base_ref, NULL)) ||1518(thin &&1519bitmap_has_sha1_in_uninteresting(bitmap_git, base_ref)))) {1520/*1521 * If base_ref was set above that means we wish to1522 * reuse delta data, and either we found that object in1523 * the list of objects we want to pack, or it's one we1524 * know the receiver has.1525 *1526 * Depth value does not matter - find_deltas() will1527 * never consider reused delta as the base object to1528 * deltify other objects against, in order to avoid1529 * circular deltas.1530 */1531oe_set_type(entry, entry->in_pack_type);1532SET_SIZE(entry, in_pack_size);/* delta size */1533SET_DELTA_SIZE(entry, in_pack_size);15341535if(base_entry) {1536SET_DELTA(entry, base_entry);1537 entry->delta_sibling_idx = base_entry->delta_child_idx;1538SET_DELTA_CHILD(base_entry, entry);1539}else{1540SET_DELTA_EXT(entry, base_ref);1541}15421543unuse_pack(&w_curs);1544return;1545}15461547if(oe_type(entry)) {1548 off_t delta_pos;15491550/*1551 * This must be a delta and we already know what the1552 * final object type is. Let's extract the actual1553 * object size from the delta header.1554 */1555 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1556 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1557if(canonical_size ==0)1558goto give_up;1559SET_SIZE(entry, canonical_size);1560unuse_pack(&w_curs);1561return;1562}15631564/*1565 * No choice but to fall back to the recursive delta walk1566 * with sha1_object_info() to find about the object type1567 * at this point...1568 */1569 give_up:1570unuse_pack(&w_curs);1571}15721573oe_set_type(entry,1574oid_object_info(the_repository, &entry->idx.oid, &canonical_size));1575if(entry->type_valid) {1576SET_SIZE(entry, canonical_size);1577}else{1578/*1579 * Bad object type is checked in prepare_pack(). This is1580 * to permit a missing preferred base object to be ignored1581 * as a preferred base. Doing so can result in a larger1582 * pack file, but the transfer will still take place.1583 */1584}1585}15861587static intpack_offset_sort(const void*_a,const void*_b)1588{1589const struct object_entry *a = *(struct object_entry **)_a;1590const struct object_entry *b = *(struct object_entry **)_b;1591const struct packed_git *a_in_pack =IN_PACK(a);1592const struct packed_git *b_in_pack =IN_PACK(b);15931594/* avoid filesystem trashing with loose objects */1595if(!a_in_pack && !b_in_pack)1596returnoidcmp(&a->idx.oid, &b->idx.oid);15971598if(a_in_pack < b_in_pack)1599return-1;1600if(a_in_pack > b_in_pack)1601return1;1602return a->in_pack_offset < b->in_pack_offset ? -1:1603(a->in_pack_offset > b->in_pack_offset);1604}16051606/*1607 * Drop an on-disk delta we were planning to reuse. Naively, this would1608 * just involve blanking out the "delta" field, but we have to deal1609 * with some extra book-keeping:1610 *1611 * 1. Removing ourselves from the delta_sibling linked list.1612 *1613 * 2. Updating our size/type to the non-delta representation. These were1614 * either not recorded initially (size) or overwritten with the delta type1615 * (type) when check_object() decided to reuse the delta.1616 *1617 * 3. Resetting our delta depth, as we are now a base object.1618 */1619static voiddrop_reused_delta(struct object_entry *entry)1620{1621unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1622struct object_info oi = OBJECT_INFO_INIT;1623enum object_type type;1624unsigned long size;16251626while(*idx) {1627struct object_entry *oe = &to_pack.objects[*idx -1];16281629if(oe == entry)1630*idx = oe->delta_sibling_idx;1631else1632 idx = &oe->delta_sibling_idx;1633}1634SET_DELTA(entry, NULL);1635 entry->depth =0;16361637 oi.sizep = &size;1638 oi.typep = &type;1639if(packed_object_info(the_repository,IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1640/*1641 * We failed to get the info from this pack for some reason;1642 * fall back to sha1_object_info, which may find another copy.1643 * And if that fails, the error will be recorded in oe_type(entry)1644 * and dealt with in prepare_pack().1645 */1646oe_set_type(entry,1647oid_object_info(the_repository, &entry->idx.oid, &size));1648}else{1649oe_set_type(entry, type);1650}1651SET_SIZE(entry, size);1652}16531654/*1655 * Follow the chain of deltas from this entry onward, throwing away any links1656 * that cause us to hit a cycle (as determined by the DFS state flags in1657 * the entries).1658 *1659 * We also detect too-long reused chains that would violate our --depth1660 * limit.1661 */1662static voidbreak_delta_chains(struct object_entry *entry)1663{1664/*1665 * The actual depth of each object we will write is stored as an int,1666 * as it cannot exceed our int "depth" limit. But before we break1667 * changes based no that limit, we may potentially go as deep as the1668 * number of objects, which is elsewhere bounded to a uint32_t.1669 */1670uint32_t total_depth;1671struct object_entry *cur, *next;16721673for(cur = entry, total_depth =0;1674 cur;1675 cur =DELTA(cur), total_depth++) {1676if(cur->dfs_state == DFS_DONE) {1677/*1678 * We've already seen this object and know it isn't1679 * part of a cycle. We do need to append its depth1680 * to our count.1681 */1682 total_depth += cur->depth;1683break;1684}16851686/*1687 * We break cycles before looping, so an ACTIVE state (or any1688 * other cruft which made its way into the state variable)1689 * is a bug.1690 */1691if(cur->dfs_state != DFS_NONE)1692BUG("confusing delta dfs state in first pass:%d",1693 cur->dfs_state);16941695/*1696 * Now we know this is the first time we've seen the object. If1697 * it's not a delta, we're done traversing, but we'll mark it1698 * done to save time on future traversals.1699 */1700if(!DELTA(cur)) {1701 cur->dfs_state = DFS_DONE;1702break;1703}17041705/*1706 * Mark ourselves as active and see if the next step causes1707 * us to cycle to another active object. It's important to do1708 * this _before_ we loop, because it impacts where we make the1709 * cut, and thus how our total_depth counter works.1710 * E.g., We may see a partial loop like:1711 *1712 * A -> B -> C -> D -> B1713 *1714 * Cutting B->C breaks the cycle. But now the depth of A is1715 * only 1, and our total_depth counter is at 3. The size of the1716 * error is always one less than the size of the cycle we1717 * broke. Commits C and D were "lost" from A's chain.1718 *1719 * If we instead cut D->B, then the depth of A is correct at 3.1720 * We keep all commits in the chain that we examined.1721 */1722 cur->dfs_state = DFS_ACTIVE;1723if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1724drop_reused_delta(cur);1725 cur->dfs_state = DFS_DONE;1726break;1727}1728}17291730/*1731 * And now that we've gone all the way to the bottom of the chain, we1732 * need to clear the active flags and set the depth fields as1733 * appropriate. Unlike the loop above, which can quit when it drops a1734 * delta, we need to keep going to look for more depth cuts. So we need1735 * an extra "next" pointer to keep going after we reset cur->delta.1736 */1737for(cur = entry; cur; cur = next) {1738 next =DELTA(cur);17391740/*1741 * We should have a chain of zero or more ACTIVE states down to1742 * a final DONE. We can quit after the DONE, because either it1743 * has no bases, or we've already handled them in a previous1744 * call.1745 */1746if(cur->dfs_state == DFS_DONE)1747break;1748else if(cur->dfs_state != DFS_ACTIVE)1749BUG("confusing delta dfs state in second pass:%d",1750 cur->dfs_state);17511752/*1753 * If the total_depth is more than depth, then we need to snip1754 * the chain into two or more smaller chains that don't exceed1755 * the maximum depth. Most of the resulting chains will contain1756 * (depth + 1) entries (i.e., depth deltas plus one base), and1757 * the last chain (i.e., the one containing entry) will contain1758 * whatever entries are left over, namely1759 * (total_depth % (depth + 1)) of them.1760 *1761 * Since we are iterating towards decreasing depth, we need to1762 * decrement total_depth as we go, and we need to write to the1763 * entry what its final depth will be after all of the1764 * snipping. Since we're snipping into chains of length (depth1765 * + 1) entries, the final depth of an entry will be its1766 * original depth modulo (depth + 1). Any time we encounter an1767 * entry whose final depth is supposed to be zero, we snip it1768 * from its delta base, thereby making it so.1769 */1770 cur->depth = (total_depth--) % (depth +1);1771if(!cur->depth)1772drop_reused_delta(cur);17731774 cur->dfs_state = DFS_DONE;1775}1776}17771778static voidget_object_details(void)1779{1780uint32_t i;1781struct object_entry **sorted_by_offset;17821783if(progress)1784 progress_state =start_progress(_("Counting objects"),1785 to_pack.nr_objects);17861787 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1788for(i =0; i < to_pack.nr_objects; i++)1789 sorted_by_offset[i] = to_pack.objects + i;1790QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17911792for(i =0; i < to_pack.nr_objects; i++) {1793struct object_entry *entry = sorted_by_offset[i];1794check_object(entry);1795if(entry->type_valid &&1796oe_size_greater_than(&to_pack, entry, big_file_threshold))1797 entry->no_try_delta =1;1798display_progress(progress_state, i +1);1799}1800stop_progress(&progress_state);18011802/*1803 * This must happen in a second pass, since we rely on the delta1804 * information for the whole list being completed.1805 */1806for(i =0; i < to_pack.nr_objects; i++)1807break_delta_chains(&to_pack.objects[i]);18081809free(sorted_by_offset);1810}18111812/*1813 * We search for deltas in a list sorted by type, by filename hash, and then1814 * by size, so that we see progressively smaller and smaller files.1815 * That's because we prefer deltas to be from the bigger file1816 * to the smaller -- deletes are potentially cheaper, but perhaps1817 * more importantly, the bigger file is likely the more recent1818 * one. The deepest deltas are therefore the oldest objects which are1819 * less susceptible to be accessed often.1820 */1821static inttype_size_sort(const void*_a,const void*_b)1822{1823const struct object_entry *a = *(struct object_entry **)_a;1824const struct object_entry *b = *(struct object_entry **)_b;1825enum object_type a_type =oe_type(a);1826enum object_type b_type =oe_type(b);1827unsigned long a_size =SIZE(a);1828unsigned long b_size =SIZE(b);18291830if(a_type > b_type)1831return-1;1832if(a_type < b_type)1833return1;1834if(a->hash > b->hash)1835return-1;1836if(a->hash < b->hash)1837return1;1838if(a->preferred_base > b->preferred_base)1839return-1;1840if(a->preferred_base < b->preferred_base)1841return1;1842if(a_size > b_size)1843return-1;1844if(a_size < b_size)1845return1;1846return a < b ? -1: (a > b);/* newest first */1847}18481849struct unpacked {1850struct object_entry *entry;1851void*data;1852struct delta_index *index;1853unsigned depth;1854};18551856static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1857unsigned long delta_size)1858{1859if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1860return0;18611862if(delta_size < cache_max_small_delta_size)1863return1;18641865/* cache delta, if objects are large enough compared to delta size */1866if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1867return1;18681869return0;1870}18711872#ifndef NO_PTHREADS18731874/* Protect access to object database */1875static pthread_mutex_t read_mutex;1876#define read_lock() pthread_mutex_lock(&read_mutex)1877#define read_unlock() pthread_mutex_unlock(&read_mutex)18781879/* Protect delta_cache_size */1880static pthread_mutex_t cache_mutex;1881#define cache_lock() pthread_mutex_lock(&cache_mutex)1882#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18831884/*1885 * Protect object list partitioning (e.g. struct thread_param) and1886 * progress_state1887 */1888static pthread_mutex_t progress_mutex;1889#define progress_lock() pthread_mutex_lock(&progress_mutex)1890#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18911892/*1893 * Access to struct object_entry is unprotected since each thread owns1894 * a portion of the main object list. Just don't access object entries1895 * ahead in the list because they can be stolen and would need1896 * progress_mutex for protection.1897 */1898#else18991900#define read_lock() (void)01901#define read_unlock() (void)01902#define cache_lock() (void)01903#define cache_unlock() (void)01904#define progress_lock() (void)01905#define progress_unlock() (void)019061907#endif19081909/*1910 * Return the size of the object without doing any delta1911 * reconstruction (so non-deltas are true object sizes, but deltas1912 * return the size of the delta data).1913 */1914unsigned longoe_get_size_slow(struct packing_data *pack,1915const struct object_entry *e)1916{1917struct packed_git *p;1918struct pack_window *w_curs;1919unsigned char*buf;1920enum object_type type;1921unsigned long used, avail, size;19221923if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1924read_lock();1925if(oid_object_info(the_repository, &e->idx.oid, &size) <0)1926die(_("unable to get size of%s"),1927oid_to_hex(&e->idx.oid));1928read_unlock();1929return size;1930}19311932 p =oe_in_pack(pack, e);1933if(!p)1934BUG("when e->type is a delta, it must belong to a pack");19351936read_lock();1937 w_curs = NULL;1938 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);1939 used =unpack_object_header_buffer(buf, avail, &type, &size);1940if(used ==0)1941die(_("unable to parse object header of%s"),1942oid_to_hex(&e->idx.oid));19431944unuse_pack(&w_curs);1945read_unlock();1946return size;1947}19481949static inttry_delta(struct unpacked *trg,struct unpacked *src,1950unsigned max_depth,unsigned long*mem_usage)1951{1952struct object_entry *trg_entry = trg->entry;1953struct object_entry *src_entry = src->entry;1954unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1955unsigned ref_depth;1956enum object_type type;1957void*delta_buf;19581959/* Don't bother doing diffs between different types */1960if(oe_type(trg_entry) !=oe_type(src_entry))1961return-1;19621963/*1964 * We do not bother to try a delta that we discarded on an1965 * earlier try, but only when reusing delta data. Note that1966 * src_entry that is marked as the preferred_base should always1967 * be considered, as even if we produce a suboptimal delta against1968 * it, we will still save the transfer cost, as we already know1969 * the other side has it and we won't send src_entry at all.1970 */1971if(reuse_delta &&IN_PACK(trg_entry) &&1972IN_PACK(trg_entry) ==IN_PACK(src_entry) &&1973!src_entry->preferred_base &&1974 trg_entry->in_pack_type != OBJ_REF_DELTA &&1975 trg_entry->in_pack_type != OBJ_OFS_DELTA)1976return0;19771978/* Let's not bust the allowed depth. */1979if(src->depth >= max_depth)1980return0;19811982/* Now some size filtering heuristics. */1983 trg_size =SIZE(trg_entry);1984if(!DELTA(trg_entry)) {1985 max_size = trg_size/2- the_hash_algo->rawsz;1986 ref_depth =1;1987}else{1988 max_size =DELTA_SIZE(trg_entry);1989 ref_depth = trg->depth;1990}1991 max_size = (uint64_t)max_size * (max_depth - src->depth) /1992(max_depth - ref_depth +1);1993if(max_size ==0)1994return0;1995 src_size =SIZE(src_entry);1996 sizediff = src_size < trg_size ? trg_size - src_size :0;1997if(sizediff >= max_size)1998return0;1999if(trg_size < src_size /32)2000return0;20012002/* Load data if not already done */2003if(!trg->data) {2004read_lock();2005 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);2006read_unlock();2007if(!trg->data)2008die(_("object%scannot be read"),2009oid_to_hex(&trg_entry->idx.oid));2010if(sz != trg_size)2011die(_("object%sinconsistent object length (%lu vs%lu)"),2012oid_to_hex(&trg_entry->idx.oid), sz,2013 trg_size);2014*mem_usage += sz;2015}2016if(!src->data) {2017read_lock();2018 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);2019read_unlock();2020if(!src->data) {2021if(src_entry->preferred_base) {2022static int warned =0;2023if(!warned++)2024warning(_("object%scannot be read"),2025oid_to_hex(&src_entry->idx.oid));2026/*2027 * Those objects are not included in the2028 * resulting pack. Be resilient and ignore2029 * them if they can't be read, in case the2030 * pack could be created nevertheless.2031 */2032return0;2033}2034die(_("object%scannot be read"),2035oid_to_hex(&src_entry->idx.oid));2036}2037if(sz != src_size)2038die(_("object%sinconsistent object length (%lu vs%lu)"),2039oid_to_hex(&src_entry->idx.oid), sz,2040 src_size);2041*mem_usage += sz;2042}2043if(!src->index) {2044 src->index =create_delta_index(src->data, src_size);2045if(!src->index) {2046static int warned =0;2047if(!warned++)2048warning(_("suboptimal pack - out of memory"));2049return0;2050}2051*mem_usage +=sizeof_delta_index(src->index);2052}20532054 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2055if(!delta_buf)2056return0;2057if(delta_size >= (1U<< OE_DELTA_SIZE_BITS)) {2058free(delta_buf);2059return0;2060}20612062if(DELTA(trg_entry)) {2063/* Prefer only shallower same-sized deltas. */2064if(delta_size ==DELTA_SIZE(trg_entry) &&2065 src->depth +1>= trg->depth) {2066free(delta_buf);2067return0;2068}2069}20702071/*2072 * Handle memory allocation outside of the cache2073 * accounting lock. Compiler will optimize the strangeness2074 * away when NO_PTHREADS is defined.2075 */2076free(trg_entry->delta_data);2077cache_lock();2078if(trg_entry->delta_data) {2079 delta_cache_size -=DELTA_SIZE(trg_entry);2080 trg_entry->delta_data = NULL;2081}2082if(delta_cacheable(src_size, trg_size, delta_size)) {2083 delta_cache_size += delta_size;2084cache_unlock();2085 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2086}else{2087cache_unlock();2088free(delta_buf);2089}20902091SET_DELTA(trg_entry, src_entry);2092SET_DELTA_SIZE(trg_entry, delta_size);2093 trg->depth = src->depth +1;20942095return1;2096}20972098static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2099{2100struct object_entry *child =DELTA_CHILD(me);2101unsigned int m = n;2102while(child) {2103unsigned int c =check_delta_limit(child, n +1);2104if(m < c)2105 m = c;2106 child =DELTA_SIBLING(child);2107}2108return m;2109}21102111static unsigned longfree_unpacked(struct unpacked *n)2112{2113unsigned long freed_mem =sizeof_delta_index(n->index);2114free_delta_index(n->index);2115 n->index = NULL;2116if(n->data) {2117 freed_mem +=SIZE(n->entry);2118FREE_AND_NULL(n->data);2119}2120 n->entry = NULL;2121 n->depth =0;2122return freed_mem;2123}21242125static voidfind_deltas(struct object_entry **list,unsigned*list_size,2126int window,int depth,unsigned*processed)2127{2128uint32_t i, idx =0, count =0;2129struct unpacked *array;2130unsigned long mem_usage =0;21312132 array =xcalloc(window,sizeof(struct unpacked));21332134for(;;) {2135struct object_entry *entry;2136struct unpacked *n = array + idx;2137int j, max_depth, best_base = -1;21382139progress_lock();2140if(!*list_size) {2141progress_unlock();2142break;2143}2144 entry = *list++;2145(*list_size)--;2146if(!entry->preferred_base) {2147(*processed)++;2148display_progress(progress_state, *processed);2149}2150progress_unlock();21512152 mem_usage -=free_unpacked(n);2153 n->entry = entry;21542155while(window_memory_limit &&2156 mem_usage > window_memory_limit &&2157 count >1) {2158uint32_t tail = (idx + window - count) % window;2159 mem_usage -=free_unpacked(array + tail);2160 count--;2161}21622163/* We do not compute delta to *create* objects we are not2164 * going to pack.2165 */2166if(entry->preferred_base)2167goto next;21682169/*2170 * If the current object is at pack edge, take the depth the2171 * objects that depend on the current object into account2172 * otherwise they would become too deep.2173 */2174 max_depth = depth;2175if(DELTA_CHILD(entry)) {2176 max_depth -=check_delta_limit(entry,0);2177if(max_depth <=0)2178goto next;2179}21802181 j = window;2182while(--j >0) {2183int ret;2184uint32_t other_idx = idx + j;2185struct unpacked *m;2186if(other_idx >= window)2187 other_idx -= window;2188 m = array + other_idx;2189if(!m->entry)2190break;2191 ret =try_delta(n, m, max_depth, &mem_usage);2192if(ret <0)2193break;2194else if(ret >0)2195 best_base = other_idx;2196}21972198/*2199 * If we decided to cache the delta data, then it is best2200 * to compress it right away. First because we have to do2201 * it anyway, and doing it here while we're threaded will2202 * save a lot of time in the non threaded write phase,2203 * as well as allow for caching more deltas within2204 * the same cache size limit.2205 * ...2206 * But only if not writing to stdout, since in that case2207 * the network is most likely throttling writes anyway,2208 * and therefore it is best to go to the write phase ASAP2209 * instead, as we can afford spending more time compressing2210 * between writes at that moment.2211 */2212if(entry->delta_data && !pack_to_stdout) {2213unsigned long size;22142215 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2216if(size < (1U<< OE_Z_DELTA_BITS)) {2217 entry->z_delta_size = size;2218cache_lock();2219 delta_cache_size -=DELTA_SIZE(entry);2220 delta_cache_size += entry->z_delta_size;2221cache_unlock();2222}else{2223FREE_AND_NULL(entry->delta_data);2224 entry->z_delta_size =0;2225}2226}22272228/* if we made n a delta, and if n is already at max2229 * depth, leaving it in the window is pointless. we2230 * should evict it first.2231 */2232if(DELTA(entry) && max_depth <= n->depth)2233continue;22342235/*2236 * Move the best delta base up in the window, after the2237 * currently deltified object, to keep it longer. It will2238 * be the first base object to be attempted next.2239 */2240if(DELTA(entry)) {2241struct unpacked swap = array[best_base];2242int dist = (window + idx - best_base) % window;2243int dst = best_base;2244while(dist--) {2245int src = (dst +1) % window;2246 array[dst] = array[src];2247 dst = src;2248}2249 array[dst] = swap;2250}22512252 next:2253 idx++;2254if(count +1< window)2255 count++;2256if(idx >= window)2257 idx =0;2258}22592260for(i =0; i < window; ++i) {2261free_delta_index(array[i].index);2262free(array[i].data);2263}2264free(array);2265}22662267#ifndef NO_PTHREADS22682269static voidtry_to_free_from_threads(size_t size)2270{2271read_lock();2272release_pack_memory(size);2273read_unlock();2274}22752276static try_to_free_t old_try_to_free_routine;22772278/*2279 * The main object list is split into smaller lists, each is handed to2280 * one worker.2281 *2282 * The main thread waits on the condition that (at least) one of the workers2283 * has stopped working (which is indicated in the .working member of2284 * struct thread_params).2285 *2286 * When a work thread has completed its work, it sets .working to 0 and2287 * signals the main thread and waits on the condition that .data_ready2288 * becomes 1.2289 *2290 * The main thread steals half of the work from the worker that has2291 * most work left to hand it to the idle worker.2292 */22932294struct thread_params {2295 pthread_t thread;2296struct object_entry **list;2297unsigned list_size;2298unsigned remaining;2299int window;2300int depth;2301int working;2302int data_ready;2303 pthread_mutex_t mutex;2304 pthread_cond_t cond;2305unsigned*processed;2306};23072308static pthread_cond_t progress_cond;23092310/*2311 * Mutex and conditional variable can't be statically-initialized on Windows.2312 */2313static voidinit_threaded_search(void)2314{2315init_recursive_mutex(&read_mutex);2316pthread_mutex_init(&cache_mutex, NULL);2317pthread_mutex_init(&progress_mutex, NULL);2318pthread_cond_init(&progress_cond, NULL);2319 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2320}23212322static voidcleanup_threaded_search(void)2323{2324set_try_to_free_routine(old_try_to_free_routine);2325pthread_cond_destroy(&progress_cond);2326pthread_mutex_destroy(&read_mutex);2327pthread_mutex_destroy(&cache_mutex);2328pthread_mutex_destroy(&progress_mutex);2329}23302331static void*threaded_find_deltas(void*arg)2332{2333struct thread_params *me = arg;23342335progress_lock();2336while(me->remaining) {2337progress_unlock();23382339find_deltas(me->list, &me->remaining,2340 me->window, me->depth, me->processed);23412342progress_lock();2343 me->working =0;2344pthread_cond_signal(&progress_cond);2345progress_unlock();23462347/*2348 * We must not set ->data_ready before we wait on the2349 * condition because the main thread may have set it to 12350 * before we get here. In order to be sure that new2351 * work is available if we see 1 in ->data_ready, it2352 * was initialized to 0 before this thread was spawned2353 * and we reset it to 0 right away.2354 */2355pthread_mutex_lock(&me->mutex);2356while(!me->data_ready)2357pthread_cond_wait(&me->cond, &me->mutex);2358 me->data_ready =0;2359pthread_mutex_unlock(&me->mutex);23602361progress_lock();2362}2363progress_unlock();2364/* leave ->working 1 so that this doesn't get more work assigned */2365return NULL;2366}23672368static voidll_find_deltas(struct object_entry **list,unsigned list_size,2369int window,int depth,unsigned*processed)2370{2371struct thread_params *p;2372int i, ret, active_threads =0;23732374init_threaded_search();23752376if(delta_search_threads <=1) {2377find_deltas(list, &list_size, window, depth, processed);2378cleanup_threaded_search();2379return;2380}2381if(progress > pack_to_stdout)2382fprintf_ln(stderr,_("Delta compression using up to%dthreads"),2383 delta_search_threads);2384 p =xcalloc(delta_search_threads,sizeof(*p));23852386/* Partition the work amongst work threads. */2387for(i =0; i < delta_search_threads; i++) {2388unsigned sub_size = list_size / (delta_search_threads - i);23892390/* don't use too small segments or no deltas will be found */2391if(sub_size <2*window && i+1< delta_search_threads)2392 sub_size =0;23932394 p[i].window = window;2395 p[i].depth = depth;2396 p[i].processed = processed;2397 p[i].working =1;2398 p[i].data_ready =0;23992400/* try to split chunks on "path" boundaries */2401while(sub_size && sub_size < list_size &&2402 list[sub_size]->hash &&2403 list[sub_size]->hash == list[sub_size-1]->hash)2404 sub_size++;24052406 p[i].list = list;2407 p[i].list_size = sub_size;2408 p[i].remaining = sub_size;24092410 list += sub_size;2411 list_size -= sub_size;2412}24132414/* Start work threads. */2415for(i =0; i < delta_search_threads; i++) {2416if(!p[i].list_size)2417continue;2418pthread_mutex_init(&p[i].mutex, NULL);2419pthread_cond_init(&p[i].cond, NULL);2420 ret =pthread_create(&p[i].thread, NULL,2421 threaded_find_deltas, &p[i]);2422if(ret)2423die(_("unable to create thread:%s"),strerror(ret));2424 active_threads++;2425}24262427/*2428 * Now let's wait for work completion. Each time a thread is done2429 * with its work, we steal half of the remaining work from the2430 * thread with the largest number of unprocessed objects and give2431 * it to that newly idle thread. This ensure good load balancing2432 * until the remaining object list segments are simply too short2433 * to be worth splitting anymore.2434 */2435while(active_threads) {2436struct thread_params *target = NULL;2437struct thread_params *victim = NULL;2438unsigned sub_size =0;24392440progress_lock();2441for(;;) {2442for(i =0; !target && i < delta_search_threads; i++)2443if(!p[i].working)2444 target = &p[i];2445if(target)2446break;2447pthread_cond_wait(&progress_cond, &progress_mutex);2448}24492450for(i =0; i < delta_search_threads; i++)2451if(p[i].remaining >2*window &&2452(!victim || victim->remaining < p[i].remaining))2453 victim = &p[i];2454if(victim) {2455 sub_size = victim->remaining /2;2456 list = victim->list + victim->list_size - sub_size;2457while(sub_size && list[0]->hash &&2458 list[0]->hash == list[-1]->hash) {2459 list++;2460 sub_size--;2461}2462if(!sub_size) {2463/*2464 * It is possible for some "paths" to have2465 * so many objects that no hash boundary2466 * might be found. Let's just steal the2467 * exact half in that case.2468 */2469 sub_size = victim->remaining /2;2470 list -= sub_size;2471}2472 target->list = list;2473 victim->list_size -= sub_size;2474 victim->remaining -= sub_size;2475}2476 target->list_size = sub_size;2477 target->remaining = sub_size;2478 target->working =1;2479progress_unlock();24802481pthread_mutex_lock(&target->mutex);2482 target->data_ready =1;2483pthread_cond_signal(&target->cond);2484pthread_mutex_unlock(&target->mutex);24852486if(!sub_size) {2487pthread_join(target->thread, NULL);2488pthread_cond_destroy(&target->cond);2489pthread_mutex_destroy(&target->mutex);2490 active_threads--;2491}2492}2493cleanup_threaded_search();2494free(p);2495}24962497#else2498#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2499#endif25002501static voidadd_tag_chain(const struct object_id *oid)2502{2503struct tag *tag;25042505/*2506 * We catch duplicates already in add_object_entry(), but we'd2507 * prefer to do this extra check to avoid having to parse the2508 * tag at all if we already know that it's being packed (e.g., if2509 * it was included via bitmaps, we would not have parsed it2510 * previously).2511 */2512if(packlist_find(&to_pack, oid->hash, NULL))2513return;25142515 tag =lookup_tag(the_repository, oid);2516while(1) {2517if(!tag ||parse_tag(tag) || !tag->tagged)2518die(_("unable to pack objects reachable from tag%s"),2519oid_to_hex(oid));25202521add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);25222523if(tag->tagged->type != OBJ_TAG)2524return;25252526 tag = (struct tag *)tag->tagged;2527}2528}25292530static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2531{2532struct object_id peeled;25332534if(starts_with(path,"refs/tags/") &&/* is a tag? */2535!peel_ref(path, &peeled) &&/* peelable? */2536packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2537add_tag_chain(oid);2538return0;2539}25402541static voidprepare_pack(int window,int depth)2542{2543struct object_entry **delta_list;2544uint32_t i, nr_deltas;2545unsigned n;25462547get_object_details();25482549/*2550 * If we're locally repacking then we need to be doubly careful2551 * from now on in order to make sure no stealth corruption gets2552 * propagated to the new pack. Clients receiving streamed packs2553 * should validate everything they get anyway so no need to incur2554 * the additional cost here in that case.2555 */2556if(!pack_to_stdout)2557 do_check_packed_object_crc =1;25582559if(!to_pack.nr_objects || !window || !depth)2560return;25612562ALLOC_ARRAY(delta_list, to_pack.nr_objects);2563 nr_deltas = n =0;25642565for(i =0; i < to_pack.nr_objects; i++) {2566struct object_entry *entry = to_pack.objects + i;25672568if(DELTA(entry))2569/* This happens if we decided to reuse existing2570 * delta from a pack. "reuse_delta &&" is implied.2571 */2572continue;25732574if(!entry->type_valid ||2575oe_size_less_than(&to_pack, entry,50))2576continue;25772578if(entry->no_try_delta)2579continue;25802581if(!entry->preferred_base) {2582 nr_deltas++;2583if(oe_type(entry) <0)2584die(_("unable to get type of object%s"),2585oid_to_hex(&entry->idx.oid));2586}else{2587if(oe_type(entry) <0) {2588/*2589 * This object is not found, but we2590 * don't have to include it anyway.2591 */2592continue;2593}2594}25952596 delta_list[n++] = entry;2597}25982599if(nr_deltas && n >1) {2600unsigned nr_done =0;2601if(progress)2602 progress_state =start_progress(_("Compressing objects"),2603 nr_deltas);2604QSORT(delta_list, n, type_size_sort);2605ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2606stop_progress(&progress_state);2607if(nr_done != nr_deltas)2608die(_("inconsistency with delta count"));2609}2610free(delta_list);2611}26122613static intgit_pack_config(const char*k,const char*v,void*cb)2614{2615if(!strcmp(k,"pack.window")) {2616 window =git_config_int(k, v);2617return0;2618}2619if(!strcmp(k,"pack.windowmemory")) {2620 window_memory_limit =git_config_ulong(k, v);2621return0;2622}2623if(!strcmp(k,"pack.depth")) {2624 depth =git_config_int(k, v);2625return0;2626}2627if(!strcmp(k,"pack.deltacachesize")) {2628 max_delta_cache_size =git_config_int(k, v);2629return0;2630}2631if(!strcmp(k,"pack.deltacachelimit")) {2632 cache_max_small_delta_size =git_config_int(k, v);2633return0;2634}2635if(!strcmp(k,"pack.writebitmaphashcache")) {2636if(git_config_bool(k, v))2637 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2638else2639 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2640}2641if(!strcmp(k,"pack.usebitmaps")) {2642 use_bitmap_index_default =git_config_bool(k, v);2643return0;2644}2645if(!strcmp(k,"pack.threads")) {2646 delta_search_threads =git_config_int(k, v);2647if(delta_search_threads <0)2648die(_("invalid number of threads specified (%d)"),2649 delta_search_threads);2650#ifdef NO_PTHREADS2651if(delta_search_threads !=1) {2652warning(_("no threads support, ignoring%s"), k);2653 delta_search_threads =0;2654}2655#endif2656return0;2657}2658if(!strcmp(k,"pack.indexversion")) {2659 pack_idx_opts.version =git_config_int(k, v);2660if(pack_idx_opts.version >2)2661die(_("bad pack.indexversion=%"PRIu32),2662 pack_idx_opts.version);2663return0;2664}2665returngit_default_config(k, v, cb);2666}26672668static voidread_object_list_from_stdin(void)2669{2670char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2671struct object_id oid;2672const char*p;26732674for(;;) {2675if(!fgets(line,sizeof(line), stdin)) {2676if(feof(stdin))2677break;2678if(!ferror(stdin))2679die("BUG: fgets returned NULL, not EOF, not error!");2680if(errno != EINTR)2681die_errno("fgets");2682clearerr(stdin);2683continue;2684}2685if(line[0] =='-') {2686if(get_oid_hex(line+1, &oid))2687die(_("expected edge object ID, got garbage:\n%s"),2688 line);2689add_preferred_base(&oid);2690continue;2691}2692if(parse_oid_hex(line, &oid, &p))2693die(_("expected object ID, got garbage:\n%s"), line);26942695add_preferred_base_object(p +1);2696add_object_entry(&oid, OBJ_NONE, p +1,0);2697}2698}26992700/* Remember to update object flag allocation in object.h */2701#define OBJECT_ADDED (1u<<20)27022703static voidshow_commit(struct commit *commit,void*data)2704{2705add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2706 commit->object.flags |= OBJECT_ADDED;27072708if(write_bitmap_index)2709index_commit_for_bitmap(commit);2710}27112712static voidshow_object(struct object *obj,const char*name,void*data)2713{2714add_preferred_base_object(name);2715add_object_entry(&obj->oid, obj->type, name,0);2716 obj->flags |= OBJECT_ADDED;2717}27182719static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2720{2721assert(arg_missing_action == MA_ALLOW_ANY);27222723/*2724 * Quietly ignore ALL missing objects. This avoids problems with2725 * staging them now and getting an odd error later.2726 */2727if(!has_object_file(&obj->oid))2728return;27292730show_object(obj, name, data);2731}27322733static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2734{2735assert(arg_missing_action == MA_ALLOW_PROMISOR);27362737/*2738 * Quietly ignore EXPECTED missing objects. This avoids problems with2739 * staging them now and getting an odd error later.2740 */2741if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2742return;27432744show_object(obj, name, data);2745}27462747static intoption_parse_missing_action(const struct option *opt,2748const char*arg,int unset)2749{2750assert(arg);2751assert(!unset);27522753if(!strcmp(arg,"error")) {2754 arg_missing_action = MA_ERROR;2755 fn_show_object = show_object;2756return0;2757}27582759if(!strcmp(arg,"allow-any")) {2760 arg_missing_action = MA_ALLOW_ANY;2761 fetch_if_missing =0;2762 fn_show_object = show_object__ma_allow_any;2763return0;2764}27652766if(!strcmp(arg,"allow-promisor")) {2767 arg_missing_action = MA_ALLOW_PROMISOR;2768 fetch_if_missing =0;2769 fn_show_object = show_object__ma_allow_promisor;2770return0;2771}27722773die(_("invalid value for --missing"));2774return0;2775}27762777static voidshow_edge(struct commit *commit)2778{2779add_preferred_base(&commit->object.oid);2780}27812782struct in_pack_object {2783 off_t offset;2784struct object *object;2785};27862787struct in_pack {2788unsigned int alloc;2789unsigned int nr;2790struct in_pack_object *array;2791};27922793static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2794{2795 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2796 in_pack->array[in_pack->nr].object = object;2797 in_pack->nr++;2798}27992800/*2801 * Compare the objects in the offset order, in order to emulate the2802 * "git rev-list --objects" output that produced the pack originally.2803 */2804static intofscmp(const void*a_,const void*b_)2805{2806struct in_pack_object *a = (struct in_pack_object *)a_;2807struct in_pack_object *b = (struct in_pack_object *)b_;28082809if(a->offset < b->offset)2810return-1;2811else if(a->offset > b->offset)2812return1;2813else2814returnoidcmp(&a->object->oid, &b->object->oid);2815}28162817static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2818{2819struct packed_git *p;2820struct in_pack in_pack;2821uint32_t i;28222823memset(&in_pack,0,sizeof(in_pack));28242825for(p =get_packed_git(the_repository); p; p = p->next) {2826struct object_id oid;2827struct object *o;28282829if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2830continue;2831if(open_pack_index(p))2832die(_("cannot open pack index"));28332834ALLOC_GROW(in_pack.array,2835 in_pack.nr + p->num_objects,2836 in_pack.alloc);28372838for(i =0; i < p->num_objects; i++) {2839nth_packed_object_oid(&oid, p, i);2840 o =lookup_unknown_object(oid.hash);2841if(!(o->flags & OBJECT_ADDED))2842mark_in_pack_object(o, p, &in_pack);2843 o->flags |= OBJECT_ADDED;2844}2845}28462847if(in_pack.nr) {2848QSORT(in_pack.array, in_pack.nr, ofscmp);2849for(i =0; i < in_pack.nr; i++) {2850struct object *o = in_pack.array[i].object;2851add_object_entry(&o->oid, o->type,"",0);2852}2853}2854free(in_pack.array);2855}28562857static intadd_loose_object(const struct object_id *oid,const char*path,2858void*data)2859{2860enum object_type type =oid_object_info(the_repository, oid, NULL);28612862if(type <0) {2863warning(_("loose object at%scould not be examined"), path);2864return0;2865}28662867add_object_entry(oid, type,"",0);2868return0;2869}28702871/*2872 * We actually don't even have to worry about reachability here.2873 * add_object_entry will weed out duplicates, so we just add every2874 * loose object we find.2875 */2876static voidadd_unreachable_loose_objects(void)2877{2878for_each_loose_file_in_objdir(get_object_directory(),2879 add_loose_object,2880 NULL, NULL, NULL);2881}28822883static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2884{2885static struct packed_git *last_found = (void*)1;2886struct packed_git *p;28872888 p = (last_found != (void*)1) ? last_found :2889get_packed_git(the_repository);28902891while(p) {2892if((!p->pack_local || p->pack_keep ||2893 p->pack_keep_in_core) &&2894find_pack_entry_one(oid->hash, p)) {2895 last_found = p;2896return1;2897}2898if(p == last_found)2899 p =get_packed_git(the_repository);2900else2901 p = p->next;2902if(p == last_found)2903 p = p->next;2904}2905return0;2906}29072908/*2909 * Store a list of sha1s that are should not be discarded2910 * because they are either written too recently, or are2911 * reachable from another object that was.2912 *2913 * This is filled by get_object_list.2914 */2915static struct oid_array recent_objects;29162917static intloosened_object_can_be_discarded(const struct object_id *oid,2918 timestamp_t mtime)2919{2920if(!unpack_unreachable_expiration)2921return0;2922if(mtime > unpack_unreachable_expiration)2923return0;2924if(oid_array_lookup(&recent_objects, oid) >=0)2925return0;2926return1;2927}29282929static voidloosen_unused_packed_objects(struct rev_info *revs)2930{2931struct packed_git *p;2932uint32_t i;2933struct object_id oid;29342935for(p =get_packed_git(the_repository); p; p = p->next) {2936if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2937continue;29382939if(open_pack_index(p))2940die(_("cannot open pack index"));29412942for(i =0; i < p->num_objects; i++) {2943nth_packed_object_oid(&oid, p, i);2944if(!packlist_find(&to_pack, oid.hash, NULL) &&2945!has_sha1_pack_kept_or_nonlocal(&oid) &&2946!loosened_object_can_be_discarded(&oid, p->mtime))2947if(force_object_loose(&oid, p->mtime))2948die(_("unable to force loose object"));2949}2950}2951}29522953/*2954 * This tracks any options which pack-reuse code expects to be on, or which a2955 * reader of the pack might not understand, and which would therefore prevent2956 * blind reuse of what we have on disk.2957 */2958static intpack_options_allow_reuse(void)2959{2960return pack_to_stdout &&2961 allow_ofs_delta &&2962!ignore_packed_keep_on_disk &&2963!ignore_packed_keep_in_core &&2964(!local || !have_non_local_packs) &&2965!incremental;2966}29672968static intget_object_list_from_bitmap(struct rev_info *revs)2969{2970if(!(bitmap_git =prepare_bitmap_walk(revs)))2971return-1;29722973if(pack_options_allow_reuse() &&2974!reuse_partial_packfile_from_bitmap(2975 bitmap_git,2976&reuse_packfile,2977&reuse_packfile_objects,2978&reuse_packfile_offset)) {2979assert(reuse_packfile_objects);2980 nr_result += reuse_packfile_objects;2981display_progress(progress_state, nr_result);2982}29832984traverse_bitmap_commit_list(bitmap_git, &add_object_entry_from_bitmap);2985return0;2986}29872988static voidrecord_recent_object(struct object *obj,2989const char*name,2990void*data)2991{2992oid_array_append(&recent_objects, &obj->oid);2993}29942995static voidrecord_recent_commit(struct commit *commit,void*data)2996{2997oid_array_append(&recent_objects, &commit->object.oid);2998}29993000static voidget_object_list(int ac,const char**av)3001{3002struct rev_info revs;3003char line[1000];3004int flags =0;30053006init_revisions(&revs, NULL);3007 save_commit_buffer =0;3008setup_revisions(ac, av, &revs, NULL);30093010/* make sure shallows are read */3011is_repository_shallow(the_repository);30123013while(fgets(line,sizeof(line), stdin) != NULL) {3014int len =strlen(line);3015if(len && line[len -1] =='\n')3016 line[--len] =0;3017if(!len)3018break;3019if(*line =='-') {3020if(!strcmp(line,"--not")) {3021 flags ^= UNINTERESTING;3022 write_bitmap_index =0;3023continue;3024}3025if(starts_with(line,"--shallow ")) {3026struct object_id oid;3027if(get_oid_hex(line +10, &oid))3028die("not an SHA-1 '%s'", line +10);3029register_shallow(the_repository, &oid);3030 use_bitmap_index =0;3031continue;3032}3033die(_("not a rev '%s'"), line);3034}3035if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))3036die(_("bad revision '%s'"), line);3037}30383039if(use_bitmap_index && !get_object_list_from_bitmap(&revs))3040return;30413042if(prepare_revision_walk(&revs))3043die(_("revision walk setup failed"));3044mark_edges_uninteresting(&revs, show_edge);30453046if(!fn_show_object)3047 fn_show_object = show_object;3048traverse_commit_list_filtered(&filter_options, &revs,3049 show_commit, fn_show_object, NULL,3050 NULL);30513052if(unpack_unreachable_expiration) {3053 revs.ignore_missing_links =1;3054if(add_unseen_recent_objects_to_traversal(&revs,3055 unpack_unreachable_expiration))3056die(_("unable to add recent objects"));3057if(prepare_revision_walk(&revs))3058die(_("revision walk setup failed"));3059traverse_commit_list(&revs, record_recent_commit,3060 record_recent_object, NULL);3061}30623063if(keep_unreachable)3064add_objects_in_unpacked_packs(&revs);3065if(pack_loose_unreachable)3066add_unreachable_loose_objects();3067if(unpack_unreachable)3068loosen_unused_packed_objects(&revs);30693070oid_array_clear(&recent_objects);3071}30723073static voidadd_extra_kept_packs(const struct string_list *names)3074{3075struct packed_git *p;30763077if(!names->nr)3078return;30793080for(p =get_packed_git(the_repository); p; p = p->next) {3081const char*name =basename(p->pack_name);3082int i;30833084if(!p->pack_local)3085continue;30863087for(i =0; i < names->nr; i++)3088if(!fspathcmp(name, names->items[i].string))3089break;30903091if(i < names->nr) {3092 p->pack_keep_in_core =1;3093 ignore_packed_keep_in_core =1;3094continue;3095}3096}3097}30983099static intoption_parse_index_version(const struct option *opt,3100const char*arg,int unset)3101{3102char*c;3103const char*val = arg;3104 pack_idx_opts.version =strtoul(val, &c,10);3105if(pack_idx_opts.version >2)3106die(_("unsupported index version%s"), val);3107if(*c ==','&& c[1])3108 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3109if(*c || pack_idx_opts.off32_limit &0x80000000)3110die(_("bad index version '%s'"), val);3111return0;3112}31133114static intoption_parse_unpack_unreachable(const struct option *opt,3115const char*arg,int unset)3116{3117if(unset) {3118 unpack_unreachable =0;3119 unpack_unreachable_expiration =0;3120}3121else{3122 unpack_unreachable =1;3123if(arg)3124 unpack_unreachable_expiration =approxidate(arg);3125}3126return0;3127}31283129intcmd_pack_objects(int argc,const char**argv,const char*prefix)3130{3131int use_internal_rev_list =0;3132int shallow =0;3133int all_progress_implied =0;3134struct argv_array rp = ARGV_ARRAY_INIT;3135int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3136int rev_list_index =0;3137struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3138struct option pack_objects_options[] = {3139OPT_SET_INT('q',"quiet", &progress,3140N_("do not show progress meter"),0),3141OPT_SET_INT(0,"progress", &progress,3142N_("show progress meter"),1),3143OPT_SET_INT(0,"all-progress", &progress,3144N_("show progress meter during object writing phase"),2),3145OPT_BOOL(0,"all-progress-implied",3146&all_progress_implied,3147N_("similar to --all-progress when progress meter is shown")),3148{ OPTION_CALLBACK,0,"index-version", NULL,N_("<version>[,<offset>]"),3149N_("write the pack index file in the specified idx format version"),31500, option_parse_index_version },3151OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3152N_("maximum size of each output pack file")),3153OPT_BOOL(0,"local", &local,3154N_("ignore borrowed objects from alternate object store")),3155OPT_BOOL(0,"incremental", &incremental,3156N_("ignore packed objects")),3157OPT_INTEGER(0,"window", &window,3158N_("limit pack window by objects")),3159OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3160N_("limit pack window by memory in addition to object limit")),3161OPT_INTEGER(0,"depth", &depth,3162N_("maximum length of delta chain allowed in the resulting pack")),3163OPT_BOOL(0,"reuse-delta", &reuse_delta,3164N_("reuse existing deltas")),3165OPT_BOOL(0,"reuse-object", &reuse_object,3166N_("reuse existing objects")),3167OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3168N_("use OFS_DELTA objects")),3169OPT_INTEGER(0,"threads", &delta_search_threads,3170N_("use threads when searching for best delta matches")),3171OPT_BOOL(0,"non-empty", &non_empty,3172N_("do not create an empty pack output")),3173OPT_BOOL(0,"revs", &use_internal_rev_list,3174N_("read revision arguments from standard input")),3175OPT_SET_INT_F(0,"unpacked", &rev_list_unpacked,3176N_("limit the objects to those that are not yet packed"),31771, PARSE_OPT_NONEG),3178OPT_SET_INT_F(0,"all", &rev_list_all,3179N_("include objects reachable from any reference"),31801, PARSE_OPT_NONEG),3181OPT_SET_INT_F(0,"reflog", &rev_list_reflog,3182N_("include objects referred by reflog entries"),31831, PARSE_OPT_NONEG),3184OPT_SET_INT_F(0,"indexed-objects", &rev_list_index,3185N_("include objects referred to by the index"),31861, PARSE_OPT_NONEG),3187OPT_BOOL(0,"stdout", &pack_to_stdout,3188N_("output pack to stdout")),3189OPT_BOOL(0,"include-tag", &include_tag,3190N_("include tag objects that refer to objects to be packed")),3191OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3192N_("keep unreachable objects")),3193OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3194N_("pack loose unreachable objects")),3195{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3196N_("unpack unreachable objects newer than <time>"),3197 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3198OPT_BOOL(0,"thin", &thin,3199N_("create thin packs")),3200OPT_BOOL(0,"shallow", &shallow,3201N_("create packs suitable for shallow fetches")),3202OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3203N_("ignore packs that have companion .keep file")),3204OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3205N_("ignore this pack")),3206OPT_INTEGER(0,"compression", &pack_compression_level,3207N_("pack compression level")),3208OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3209N_("do not hide commits by grafts"),0),3210OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3211N_("use a bitmap index if available to speed up counting objects")),3212OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3213N_("write a bitmap index together with the pack index")),3214OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3215{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3216N_("handling for missing objects"), PARSE_OPT_NONEG,3217 option_parse_missing_action },3218OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3219N_("do not pack objects in promisor packfiles")),3220OPT_END(),3221};32223223if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3224BUG("too many dfs states, increase OE_DFS_STATE_BITS");32253226 read_replace_refs =0;32273228reset_pack_idx_option(&pack_idx_opts);3229git_config(git_pack_config, NULL);32303231 progress =isatty(2);3232 argc =parse_options(argc, argv, prefix, pack_objects_options,3233 pack_usage,0);32343235if(argc) {3236 base_name = argv[0];3237 argc--;3238}3239if(pack_to_stdout != !base_name || argc)3240usage_with_options(pack_usage, pack_objects_options);32413242if(depth >= (1<< OE_DEPTH_BITS)) {3243warning(_("delta chain depth%dis too deep, forcing%d"),3244 depth, (1<< OE_DEPTH_BITS) -1);3245 depth = (1<< OE_DEPTH_BITS) -1;3246}3247if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3248warning(_("pack.deltaCacheLimit is too high, forcing%d"),3249(1U<< OE_Z_DELTA_BITS) -1);3250 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3251}32523253argv_array_push(&rp,"pack-objects");3254if(thin) {3255 use_internal_rev_list =1;3256argv_array_push(&rp, shallow3257?"--objects-edge-aggressive"3258:"--objects-edge");3259}else3260argv_array_push(&rp,"--objects");32613262if(rev_list_all) {3263 use_internal_rev_list =1;3264argv_array_push(&rp,"--all");3265}3266if(rev_list_reflog) {3267 use_internal_rev_list =1;3268argv_array_push(&rp,"--reflog");3269}3270if(rev_list_index) {3271 use_internal_rev_list =1;3272argv_array_push(&rp,"--indexed-objects");3273}3274if(rev_list_unpacked) {3275 use_internal_rev_list =1;3276argv_array_push(&rp,"--unpacked");3277}32783279if(exclude_promisor_objects) {3280 use_internal_rev_list =1;3281 fetch_if_missing =0;3282argv_array_push(&rp,"--exclude-promisor-objects");3283}3284if(unpack_unreachable || keep_unreachable || pack_loose_unreachable)3285 use_internal_rev_list =1;32863287if(!reuse_object)3288 reuse_delta =0;3289if(pack_compression_level == -1)3290 pack_compression_level = Z_DEFAULT_COMPRESSION;3291else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3292die(_("bad pack compression level%d"), pack_compression_level);32933294if(!delta_search_threads)/* --threads=0 means autodetect */3295 delta_search_threads =online_cpus();32963297#ifdef NO_PTHREADS3298if(delta_search_threads !=1)3299warning(_("no threads support, ignoring --threads"));3300#endif3301if(!pack_to_stdout && !pack_size_limit)3302 pack_size_limit = pack_size_limit_cfg;3303if(pack_to_stdout && pack_size_limit)3304die(_("--max-pack-size cannot be used to build a pack for transfer"));3305if(pack_size_limit && pack_size_limit <1024*1024) {3306warning(_("minimum pack size limit is 1 MiB"));3307 pack_size_limit =1024*1024;3308}33093310if(!pack_to_stdout && thin)3311die(_("--thin cannot be used to build an indexable pack"));33123313if(keep_unreachable && unpack_unreachable)3314die(_("--keep-unreachable and --unpack-unreachable are incompatible"));3315if(!rev_list_all || !rev_list_reflog || !rev_list_index)3316 unpack_unreachable_expiration =0;33173318if(filter_options.choice) {3319if(!pack_to_stdout)3320die(_("cannot use --filter without --stdout"));3321 use_bitmap_index =0;3322}33233324/*3325 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3326 *3327 * - to produce good pack (with bitmap index not-yet-packed objects are3328 * packed in suboptimal order).3329 *3330 * - to use more robust pack-generation codepath (avoiding possible3331 * bugs in bitmap code and possible bitmap index corruption).3332 */3333if(!pack_to_stdout)3334 use_bitmap_index_default =0;33353336if(use_bitmap_index <0)3337 use_bitmap_index = use_bitmap_index_default;33383339/* "hard" reasons not to use bitmaps; these just won't work at all */3340if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow(the_repository))3341 use_bitmap_index =0;33423343if(pack_to_stdout || !rev_list_all)3344 write_bitmap_index =0;33453346if(progress && all_progress_implied)3347 progress =2;33483349add_extra_kept_packs(&keep_pack_list);3350if(ignore_packed_keep_on_disk) {3351struct packed_git *p;3352for(p =get_packed_git(the_repository); p; p = p->next)3353if(p->pack_local && p->pack_keep)3354break;3355if(!p)/* no keep-able packs found */3356 ignore_packed_keep_on_disk =0;3357}3358if(local) {3359/*3360 * unlike ignore_packed_keep_on_disk above, we do not3361 * want to unset "local" based on looking at packs, as3362 * it also covers non-local objects3363 */3364struct packed_git *p;3365for(p =get_packed_git(the_repository); p; p = p->next) {3366if(!p->pack_local) {3367 have_non_local_packs =1;3368break;3369}3370}3371}33723373prepare_packing_data(&to_pack);33743375if(progress)3376 progress_state =start_progress(_("Enumerating objects"),0);3377if(!use_internal_rev_list)3378read_object_list_from_stdin();3379else{3380get_object_list(rp.argc, rp.argv);3381argv_array_clear(&rp);3382}3383cleanup_preferred_base();3384if(include_tag && nr_result)3385for_each_ref(add_ref_tag, NULL);3386stop_progress(&progress_state);33873388if(non_empty && !nr_result)3389return0;3390if(nr_result)3391prepare_pack(window, depth);3392write_pack_file();3393if(progress)3394fprintf_ln(stderr,3395_("Total %"PRIu32" (delta %"PRIu32"),"3396" reused %"PRIu32" (delta %"PRIu32")"),3397 written, written_delta, reused, reused_delta);3398return0;3399}