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_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val) 44#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val) 45#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val) 46 47static const char*pack_usage[] = { 48N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 49N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 50 NULL 51}; 52 53/* 54 * Objects we are going to pack are collected in the `to_pack` structure. 55 * It contains an array (dynamically expanded) of the object data, and a map 56 * that can resolve SHA1s to their position in the array. 57 */ 58static struct packing_data to_pack; 59 60static struct pack_idx_entry **written_list; 61static uint32_t nr_result, nr_written, nr_seen; 62 63static int non_empty; 64static int reuse_delta =1, reuse_object =1; 65static int keep_unreachable, unpack_unreachable, include_tag; 66static timestamp_t unpack_unreachable_expiration; 67static int pack_loose_unreachable; 68static int local; 69static int have_non_local_packs; 70static int incremental; 71static int ignore_packed_keep_on_disk; 72static int ignore_packed_keep_in_core; 73static int allow_ofs_delta; 74static struct pack_idx_option pack_idx_opts; 75static const char*base_name; 76static int progress =1; 77static int window =10; 78static unsigned long pack_size_limit; 79static int depth =50; 80static int delta_search_threads; 81static int pack_to_stdout; 82static int num_preferred_base; 83static struct progress *progress_state; 84 85static struct packed_git *reuse_packfile; 86static uint32_t reuse_packfile_objects; 87static off_t reuse_packfile_offset; 88 89static int use_bitmap_index_default =1; 90static int use_bitmap_index = -1; 91static int write_bitmap_index; 92static uint16_t write_bitmap_options; 93 94static int exclude_promisor_objects; 95 96static unsigned long delta_cache_size =0; 97static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; 98static unsigned long cache_max_small_delta_size =1000; 99 100static unsigned long window_memory_limit =0; 101 102static struct list_objects_filter_options filter_options; 103 104enum missing_action { 105 MA_ERROR =0,/* fail if any missing objects are encountered */ 106 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 107 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 108}; 109static enum missing_action arg_missing_action; 110static show_object_fn fn_show_object; 111 112/* 113 * stats 114 */ 115static uint32_t written, written_delta; 116static uint32_t reused, reused_delta; 117 118/* 119 * Indexed commits 120 */ 121static struct commit **indexed_commits; 122static unsigned int indexed_commits_nr; 123static unsigned int indexed_commits_alloc; 124 125static voidindex_commit_for_bitmap(struct commit *commit) 126{ 127if(indexed_commits_nr >= indexed_commits_alloc) { 128 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 129REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 130} 131 132 indexed_commits[indexed_commits_nr++] = commit; 133} 134 135static void*get_delta(struct object_entry *entry) 136{ 137unsigned long size, base_size, delta_size; 138void*buf, *base_buf, *delta_buf; 139enum object_type type; 140 141 buf =read_object_file(&entry->idx.oid, &type, &size); 142if(!buf) 143die("unable to read%s",oid_to_hex(&entry->idx.oid)); 144 base_buf =read_object_file(&DELTA(entry)->idx.oid, &type, 145&base_size); 146if(!base_buf) 147die("unable to read%s", 148oid_to_hex(&DELTA(entry)->idx.oid)); 149 delta_buf =diff_delta(base_buf, base_size, 150 buf, size, &delta_size,0); 151if(!delta_buf || delta_size !=DELTA_SIZE(entry)) 152die("delta size changed"); 153free(buf); 154free(base_buf); 155return delta_buf; 156} 157 158static unsigned longdo_compress(void**pptr,unsigned long size) 159{ 160 git_zstream stream; 161void*in, *out; 162unsigned long maxsize; 163 164git_deflate_init(&stream, pack_compression_level); 165 maxsize =git_deflate_bound(&stream, size); 166 167 in = *pptr; 168 out =xmalloc(maxsize); 169*pptr = out; 170 171 stream.next_in = in; 172 stream.avail_in = size; 173 stream.next_out = out; 174 stream.avail_out = maxsize; 175while(git_deflate(&stream, Z_FINISH) == Z_OK) 176;/* nothing */ 177git_deflate_end(&stream); 178 179free(in); 180return stream.total_out; 181} 182 183static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 184const struct object_id *oid) 185{ 186 git_zstream stream; 187unsigned char ibuf[1024*16]; 188unsigned char obuf[1024*16]; 189unsigned long olen =0; 190 191git_deflate_init(&stream, pack_compression_level); 192 193for(;;) { 194 ssize_t readlen; 195int zret = Z_OK; 196 readlen =read_istream(st, ibuf,sizeof(ibuf)); 197if(readlen == -1) 198die(_("unable to read%s"),oid_to_hex(oid)); 199 200 stream.next_in = ibuf; 201 stream.avail_in = readlen; 202while((stream.avail_in || readlen ==0) && 203(zret == Z_OK || zret == Z_BUF_ERROR)) { 204 stream.next_out = obuf; 205 stream.avail_out =sizeof(obuf); 206 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 207hashwrite(f, obuf, stream.next_out - obuf); 208 olen += stream.next_out - obuf; 209} 210if(stream.avail_in) 211die(_("deflate error (%d)"), zret); 212if(readlen ==0) { 213if(zret != Z_STREAM_END) 214die(_("deflate error (%d)"), zret); 215break; 216} 217} 218git_deflate_end(&stream); 219return olen; 220} 221 222/* 223 * we are going to reuse the existing object data as is. make 224 * sure it is not corrupt. 225 */ 226static intcheck_pack_inflate(struct packed_git *p, 227struct pack_window **w_curs, 228 off_t offset, 229 off_t len, 230unsigned long expect) 231{ 232 git_zstream stream; 233unsigned char fakebuf[4096], *in; 234int st; 235 236memset(&stream,0,sizeof(stream)); 237git_inflate_init(&stream); 238do{ 239 in =use_pack(p, w_curs, offset, &stream.avail_in); 240 stream.next_in = in; 241 stream.next_out = fakebuf; 242 stream.avail_out =sizeof(fakebuf); 243 st =git_inflate(&stream, Z_FINISH); 244 offset += stream.next_in - in; 245}while(st == Z_OK || st == Z_BUF_ERROR); 246git_inflate_end(&stream); 247return(st == Z_STREAM_END && 248 stream.total_out == expect && 249 stream.total_in == len) ?0: -1; 250} 251 252static voidcopy_pack_data(struct hashfile *f, 253struct packed_git *p, 254struct pack_window **w_curs, 255 off_t offset, 256 off_t len) 257{ 258unsigned char*in; 259unsigned long avail; 260 261while(len) { 262 in =use_pack(p, w_curs, offset, &avail); 263if(avail > len) 264 avail = (unsigned long)len; 265hashwrite(f, in, avail); 266 offset += avail; 267 len -= avail; 268} 269} 270 271/* Return 0 if we will bust the pack-size limit */ 272static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 273unsigned long limit,int usable_delta) 274{ 275unsigned long size, datalen; 276unsigned char header[MAX_PACK_OBJECT_HEADER], 277 dheader[MAX_PACK_OBJECT_HEADER]; 278unsigned hdrlen; 279enum object_type type; 280void*buf; 281struct git_istream *st = NULL; 282const unsigned hashsz = the_hash_algo->rawsz; 283 284if(!usable_delta) { 285if(oe_type(entry) == OBJ_BLOB && 286oe_size_greater_than(&to_pack, entry, big_file_threshold) && 287(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 288 buf = NULL; 289else{ 290 buf =read_object_file(&entry->idx.oid, &type, &size); 291if(!buf) 292die(_("unable to read%s"), 293oid_to_hex(&entry->idx.oid)); 294} 295/* 296 * make sure no cached delta data remains from a 297 * previous attempt before a pack split occurred. 298 */ 299FREE_AND_NULL(entry->delta_data); 300 entry->z_delta_size =0; 301}else if(entry->delta_data) { 302 size =DELTA_SIZE(entry); 303 buf = entry->delta_data; 304 entry->delta_data = NULL; 305 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 306 OBJ_OFS_DELTA : OBJ_REF_DELTA; 307}else{ 308 buf =get_delta(entry); 309 size =DELTA_SIZE(entry); 310 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 311 OBJ_OFS_DELTA : OBJ_REF_DELTA; 312} 313 314if(st)/* large blob case, just assume we don't compress well */ 315 datalen = size; 316else if(entry->z_delta_size) 317 datalen = entry->z_delta_size; 318else 319 datalen =do_compress(&buf, size); 320 321/* 322 * The object header is a byte of 'type' followed by zero or 323 * more bytes of length. 324 */ 325 hdrlen =encode_in_pack_object_header(header,sizeof(header), 326 type, size); 327 328if(type == OBJ_OFS_DELTA) { 329/* 330 * Deltas with relative base contain an additional 331 * encoding of the relative offset for the delta 332 * base from this object's position in the pack. 333 */ 334 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 335unsigned pos =sizeof(dheader) -1; 336 dheader[pos] = ofs &127; 337while(ofs >>=7) 338 dheader[--pos] =128| (--ofs &127); 339if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 340if(st) 341close_istream(st); 342free(buf); 343return0; 344} 345hashwrite(f, header, hdrlen); 346hashwrite(f, dheader + pos,sizeof(dheader) - pos); 347 hdrlen +=sizeof(dheader) - pos; 348}else if(type == OBJ_REF_DELTA) { 349/* 350 * Deltas with a base reference contain 351 * additional bytes for the base object ID. 352 */ 353if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 354if(st) 355close_istream(st); 356free(buf); 357return0; 358} 359hashwrite(f, header, hdrlen); 360hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 361 hdrlen += hashsz; 362}else{ 363if(limit && hdrlen + datalen + hashsz >= limit) { 364if(st) 365close_istream(st); 366free(buf); 367return0; 368} 369hashwrite(f, header, hdrlen); 370} 371if(st) { 372 datalen =write_large_blob_data(st, f, &entry->idx.oid); 373close_istream(st); 374}else{ 375hashwrite(f, buf, datalen); 376free(buf); 377} 378 379return hdrlen + datalen; 380} 381 382/* Return 0 if we will bust the pack-size limit */ 383static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 384unsigned long limit,int usable_delta) 385{ 386struct packed_git *p =IN_PACK(entry); 387struct pack_window *w_curs = NULL; 388struct revindex_entry *revidx; 389 off_t offset; 390enum object_type type =oe_type(entry); 391 off_t datalen; 392unsigned char header[MAX_PACK_OBJECT_HEADER], 393 dheader[MAX_PACK_OBJECT_HEADER]; 394unsigned hdrlen; 395const unsigned hashsz = the_hash_algo->rawsz; 396unsigned long entry_size =SIZE(entry); 397 398if(DELTA(entry)) 399 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 400 OBJ_OFS_DELTA : OBJ_REF_DELTA; 401 hdrlen =encode_in_pack_object_header(header,sizeof(header), 402 type, entry_size); 403 404 offset = entry->in_pack_offset; 405 revidx =find_pack_revindex(p, offset); 406 datalen = revidx[1].offset - offset; 407if(!pack_to_stdout && p->index_version >1&& 408check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 409error("bad packed object CRC for%s", 410oid_to_hex(&entry->idx.oid)); 411unuse_pack(&w_curs); 412returnwrite_no_reuse_object(f, entry, limit, usable_delta); 413} 414 415 offset += entry->in_pack_header_size; 416 datalen -= entry->in_pack_header_size; 417 418if(!pack_to_stdout && p->index_version ==1&& 419check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 420error("corrupt packed object for%s", 421oid_to_hex(&entry->idx.oid)); 422unuse_pack(&w_curs); 423returnwrite_no_reuse_object(f, entry, limit, usable_delta); 424} 425 426if(type == OBJ_OFS_DELTA) { 427 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 428unsigned pos =sizeof(dheader) -1; 429 dheader[pos] = ofs &127; 430while(ofs >>=7) 431 dheader[--pos] =128| (--ofs &127); 432if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 433unuse_pack(&w_curs); 434return0; 435} 436hashwrite(f, header, hdrlen); 437hashwrite(f, dheader + pos,sizeof(dheader) - pos); 438 hdrlen +=sizeof(dheader) - pos; 439 reused_delta++; 440}else if(type == OBJ_REF_DELTA) { 441if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 442unuse_pack(&w_curs); 443return0; 444} 445hashwrite(f, header, hdrlen); 446hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 447 hdrlen += hashsz; 448 reused_delta++; 449}else{ 450if(limit && hdrlen + datalen + hashsz >= limit) { 451unuse_pack(&w_curs); 452return0; 453} 454hashwrite(f, header, hdrlen); 455} 456copy_pack_data(f, p, &w_curs, offset, datalen); 457unuse_pack(&w_curs); 458 reused++; 459return hdrlen + datalen; 460} 461 462/* Return 0 if we will bust the pack-size limit */ 463static off_t write_object(struct hashfile *f, 464struct object_entry *entry, 465 off_t write_offset) 466{ 467unsigned long limit; 468 off_t len; 469int usable_delta, to_reuse; 470 471if(!pack_to_stdout) 472crc32_begin(f); 473 474/* apply size limit if limited packsize and not first object */ 475if(!pack_size_limit || !nr_written) 476 limit =0; 477else if(pack_size_limit <= write_offset) 478/* 479 * the earlier object did not fit the limit; avoid 480 * mistaking this with unlimited (i.e. limit = 0). 481 */ 482 limit =1; 483else 484 limit = pack_size_limit - write_offset; 485 486if(!DELTA(entry)) 487 usable_delta =0;/* no delta */ 488else if(!pack_size_limit) 489 usable_delta =1;/* unlimited packfile */ 490else if(DELTA(entry)->idx.offset == (off_t)-1) 491 usable_delta =0;/* base was written to another pack */ 492else if(DELTA(entry)->idx.offset) 493 usable_delta =1;/* base already exists in this pack */ 494else 495 usable_delta =0;/* base could end up in another pack */ 496 497if(!reuse_object) 498 to_reuse =0;/* explicit */ 499else if(!IN_PACK(entry)) 500 to_reuse =0;/* can't reuse what we don't have */ 501else if(oe_type(entry) == OBJ_REF_DELTA || 502oe_type(entry) == OBJ_OFS_DELTA) 503/* check_object() decided it for us ... */ 504 to_reuse = usable_delta; 505/* ... but pack split may override that */ 506else if(oe_type(entry) != entry->in_pack_type) 507 to_reuse =0;/* pack has delta which is unusable */ 508else if(DELTA(entry)) 509 to_reuse =0;/* we want to pack afresh */ 510else 511 to_reuse =1;/* we have it in-pack undeltified, 512 * and we do not need to deltify it. 513 */ 514 515if(!to_reuse) 516 len =write_no_reuse_object(f, entry, limit, usable_delta); 517else 518 len =write_reuse_object(f, entry, limit, usable_delta); 519if(!len) 520return0; 521 522if(usable_delta) 523 written_delta++; 524 written++; 525if(!pack_to_stdout) 526 entry->idx.crc32 =crc32_end(f); 527return len; 528} 529 530enum write_one_status { 531 WRITE_ONE_SKIP = -1,/* already written */ 532 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 533 WRITE_ONE_WRITTEN =1,/* normal */ 534 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 535}; 536 537static enum write_one_status write_one(struct hashfile *f, 538struct object_entry *e, 539 off_t *offset) 540{ 541 off_t size; 542int recursing; 543 544/* 545 * we set offset to 1 (which is an impossible value) to mark 546 * the fact that this object is involved in "write its base 547 * first before writing a deltified object" recursion. 548 */ 549 recursing = (e->idx.offset ==1); 550if(recursing) { 551warning("recursive delta detected for object%s", 552oid_to_hex(&e->idx.oid)); 553return WRITE_ONE_RECURSIVE; 554}else if(e->idx.offset || e->preferred_base) { 555/* offset is non zero if object is written already. */ 556return WRITE_ONE_SKIP; 557} 558 559/* if we are deltified, write out base object first. */ 560if(DELTA(e)) { 561 e->idx.offset =1;/* now recurse */ 562switch(write_one(f,DELTA(e), offset)) { 563case WRITE_ONE_RECURSIVE: 564/* we cannot depend on this one */ 565SET_DELTA(e, NULL); 566break; 567default: 568break; 569case WRITE_ONE_BREAK: 570 e->idx.offset = recursing; 571return WRITE_ONE_BREAK; 572} 573} 574 575 e->idx.offset = *offset; 576 size =write_object(f, e, *offset); 577if(!size) { 578 e->idx.offset = recursing; 579return WRITE_ONE_BREAK; 580} 581 written_list[nr_written++] = &e->idx; 582 583/* make sure off_t is sufficiently large not to wrap */ 584if(signed_add_overflows(*offset, size)) 585die("pack too large for current definition of off_t"); 586*offset += size; 587return WRITE_ONE_WRITTEN; 588} 589 590static intmark_tagged(const char*path,const struct object_id *oid,int flag, 591void*cb_data) 592{ 593struct object_id peeled; 594struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 595 596if(entry) 597 entry->tagged =1; 598if(!peel_ref(path, &peeled)) { 599 entry =packlist_find(&to_pack, peeled.hash, NULL); 600if(entry) 601 entry->tagged =1; 602} 603return0; 604} 605 606staticinlinevoidadd_to_write_order(struct object_entry **wo, 607unsigned int*endp, 608struct object_entry *e) 609{ 610if(e->filled) 611return; 612 wo[(*endp)++] = e; 613 e->filled =1; 614} 615 616static voidadd_descendants_to_write_order(struct object_entry **wo, 617unsigned int*endp, 618struct object_entry *e) 619{ 620int add_to_order =1; 621while(e) { 622if(add_to_order) { 623struct object_entry *s; 624/* add this node... */ 625add_to_write_order(wo, endp, e); 626/* all its siblings... */ 627for(s =DELTA_SIBLING(e); s; s =DELTA_SIBLING(s)) { 628add_to_write_order(wo, endp, s); 629} 630} 631/* drop down a level to add left subtree nodes if possible */ 632if(DELTA_CHILD(e)) { 633 add_to_order =1; 634 e =DELTA_CHILD(e); 635}else{ 636 add_to_order =0; 637/* our sibling might have some children, it is next */ 638if(DELTA_SIBLING(e)) { 639 e =DELTA_SIBLING(e); 640continue; 641} 642/* go back to our parent node */ 643 e =DELTA(e); 644while(e && !DELTA_SIBLING(e)) { 645/* we're on the right side of a subtree, keep 646 * going up until we can go right again */ 647 e =DELTA(e); 648} 649if(!e) { 650/* done- we hit our original root node */ 651return; 652} 653/* pass it off to sibling at this level */ 654 e =DELTA_SIBLING(e); 655} 656}; 657} 658 659static voidadd_family_to_write_order(struct object_entry **wo, 660unsigned int*endp, 661struct object_entry *e) 662{ 663struct object_entry *root; 664 665for(root = e;DELTA(root); root =DELTA(root)) 666;/* nothing */ 667add_descendants_to_write_order(wo, endp, root); 668} 669 670static struct object_entry **compute_write_order(void) 671{ 672unsigned int i, wo_end, last_untagged; 673 674struct object_entry **wo; 675struct object_entry *objects = to_pack.objects; 676 677for(i =0; i < to_pack.nr_objects; i++) { 678 objects[i].tagged =0; 679 objects[i].filled =0; 680SET_DELTA_CHILD(&objects[i], NULL); 681SET_DELTA_SIBLING(&objects[i], NULL); 682} 683 684/* 685 * Fully connect delta_child/delta_sibling network. 686 * Make sure delta_sibling is sorted in the original 687 * recency order. 688 */ 689for(i = to_pack.nr_objects; i >0;) { 690struct object_entry *e = &objects[--i]; 691if(!DELTA(e)) 692continue; 693/* Mark me as the first child */ 694 e->delta_sibling_idx =DELTA(e)->delta_child_idx; 695SET_DELTA_CHILD(DELTA(e), e); 696} 697 698/* 699 * Mark objects that are at the tip of tags. 700 */ 701for_each_tag_ref(mark_tagged, NULL); 702 703/* 704 * Give the objects in the original recency order until 705 * we see a tagged tip. 706 */ 707ALLOC_ARRAY(wo, to_pack.nr_objects); 708for(i = wo_end =0; i < to_pack.nr_objects; i++) { 709if(objects[i].tagged) 710break; 711add_to_write_order(wo, &wo_end, &objects[i]); 712} 713 last_untagged = i; 714 715/* 716 * Then fill all the tagged tips. 717 */ 718for(; i < to_pack.nr_objects; i++) { 719if(objects[i].tagged) 720add_to_write_order(wo, &wo_end, &objects[i]); 721} 722 723/* 724 * And then all remaining commits and tags. 725 */ 726for(i = last_untagged; i < to_pack.nr_objects; i++) { 727if(oe_type(&objects[i]) != OBJ_COMMIT && 728oe_type(&objects[i]) != OBJ_TAG) 729continue; 730add_to_write_order(wo, &wo_end, &objects[i]); 731} 732 733/* 734 * And then all the trees. 735 */ 736for(i = last_untagged; i < to_pack.nr_objects; i++) { 737if(oe_type(&objects[i]) != OBJ_TREE) 738continue; 739add_to_write_order(wo, &wo_end, &objects[i]); 740} 741 742/* 743 * Finally all the rest in really tight order 744 */ 745for(i = last_untagged; i < to_pack.nr_objects; i++) { 746if(!objects[i].filled) 747add_family_to_write_order(wo, &wo_end, &objects[i]); 748} 749 750if(wo_end != to_pack.nr_objects) 751die("ordered%uobjects, expected %"PRIu32, wo_end, to_pack.nr_objects); 752 753return wo; 754} 755 756static off_t write_reused_pack(struct hashfile *f) 757{ 758unsigned char buffer[8192]; 759 off_t to_write, total; 760int fd; 761 762if(!is_pack_valid(reuse_packfile)) 763die("packfile is invalid:%s", reuse_packfile->pack_name); 764 765 fd =git_open(reuse_packfile->pack_name); 766if(fd <0) 767die_errno("unable to open packfile for reuse:%s", 768 reuse_packfile->pack_name); 769 770if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 771die_errno("unable to seek in reused packfile"); 772 773if(reuse_packfile_offset <0) 774 reuse_packfile_offset = reuse_packfile->pack_size - the_hash_algo->rawsz; 775 776 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 777 778while(to_write) { 779int read_pack =xread(fd, buffer,sizeof(buffer)); 780 781if(read_pack <=0) 782die_errno("unable to read from reused packfile"); 783 784if(read_pack > to_write) 785 read_pack = to_write; 786 787hashwrite(f, buffer, read_pack); 788 to_write -= read_pack; 789 790/* 791 * We don't know the actual number of objects written, 792 * only how many bytes written, how many bytes total, and 793 * how many objects total. So we can fake it by pretending all 794 * objects we are writing are the same size. This gives us a 795 * smooth progress meter, and at the end it matches the true 796 * answer. 797 */ 798 written = reuse_packfile_objects * 799(((double)(total - to_write)) / total); 800display_progress(progress_state, written); 801} 802 803close(fd); 804 written = reuse_packfile_objects; 805display_progress(progress_state, written); 806return reuse_packfile_offset -sizeof(struct pack_header); 807} 808 809static const char no_split_warning[] =N_( 810"disabling bitmap writing, packs are split due to pack.packSizeLimit" 811); 812 813static voidwrite_pack_file(void) 814{ 815uint32_t i =0, j; 816struct hashfile *f; 817 off_t offset; 818uint32_t nr_remaining = nr_result; 819time_t last_mtime =0; 820struct object_entry **write_order; 821 822if(progress > pack_to_stdout) 823 progress_state =start_progress(_("Writing objects"), nr_result); 824ALLOC_ARRAY(written_list, to_pack.nr_objects); 825 write_order =compute_write_order(); 826 827do{ 828struct object_id oid; 829char*pack_tmp_name = NULL; 830 831if(pack_to_stdout) 832 f =hashfd_throughput(1,"<stdout>", progress_state); 833else 834 f =create_tmp_packfile(&pack_tmp_name); 835 836 offset =write_pack_header(f, nr_remaining); 837 838if(reuse_packfile) { 839 off_t packfile_size; 840assert(pack_to_stdout); 841 842 packfile_size =write_reused_pack(f); 843 offset += packfile_size; 844} 845 846 nr_written =0; 847for(; i < to_pack.nr_objects; i++) { 848struct object_entry *e = write_order[i]; 849if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 850break; 851display_progress(progress_state, written); 852} 853 854/* 855 * Did we write the wrong # entries in the header? 856 * If so, rewrite it like in fast-import 857 */ 858if(pack_to_stdout) { 859finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 860}else if(nr_written == nr_remaining) { 861finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 862}else{ 863int fd =finalize_hashfile(f, oid.hash,0); 864fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 865 nr_written, oid.hash, offset); 866close(fd); 867if(write_bitmap_index) { 868warning(_(no_split_warning)); 869 write_bitmap_index =0; 870} 871} 872 873if(!pack_to_stdout) { 874struct stat st; 875struct strbuf tmpname = STRBUF_INIT; 876 877/* 878 * Packs are runtime accessed in their mtime 879 * order since newer packs are more likely to contain 880 * younger objects. So if we are creating multiple 881 * packs then we should modify the mtime of later ones 882 * to preserve this property. 883 */ 884if(stat(pack_tmp_name, &st) <0) { 885warning_errno("failed to stat%s", pack_tmp_name); 886}else if(!last_mtime) { 887 last_mtime = st.st_mtime; 888}else{ 889struct utimbuf utb; 890 utb.actime = st.st_atime; 891 utb.modtime = --last_mtime; 892if(utime(pack_tmp_name, &utb) <0) 893warning_errno("failed utime() on%s", pack_tmp_name); 894} 895 896strbuf_addf(&tmpname,"%s-", base_name); 897 898if(write_bitmap_index) { 899bitmap_writer_set_checksum(oid.hash); 900bitmap_writer_build_type_index( 901&to_pack, written_list, nr_written); 902} 903 904finish_tmp_packfile(&tmpname, pack_tmp_name, 905 written_list, nr_written, 906&pack_idx_opts, oid.hash); 907 908if(write_bitmap_index) { 909strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 910 911stop_progress(&progress_state); 912 913bitmap_writer_show_progress(progress); 914bitmap_writer_reuse_bitmaps(&to_pack); 915bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 916bitmap_writer_build(&to_pack); 917bitmap_writer_finish(written_list, nr_written, 918 tmpname.buf, write_bitmap_options); 919 write_bitmap_index =0; 920} 921 922strbuf_release(&tmpname); 923free(pack_tmp_name); 924puts(oid_to_hex(&oid)); 925} 926 927/* mark written objects as written to previous pack */ 928for(j =0; j < nr_written; j++) { 929 written_list[j]->offset = (off_t)-1; 930} 931 nr_remaining -= nr_written; 932}while(nr_remaining && i < to_pack.nr_objects); 933 934free(written_list); 935free(write_order); 936stop_progress(&progress_state); 937if(written != nr_result) 938die("wrote %"PRIu32" objects while expecting %"PRIu32, 939 written, nr_result); 940} 941 942static intno_try_delta(const char*path) 943{ 944static struct attr_check *check; 945 946if(!check) 947 check =attr_check_initl("delta", NULL); 948if(git_check_attr(path, check)) 949return0; 950if(ATTR_FALSE(check->items[0].value)) 951return1; 952return0; 953} 954 955/* 956 * When adding an object, check whether we have already added it 957 * to our packing list. If so, we can skip. However, if we are 958 * being asked to excludei t, but the previous mention was to include 959 * it, make sure to adjust its flags and tweak our numbers accordingly. 960 * 961 * As an optimization, we pass out the index position where we would have 962 * found the item, since that saves us from having to look it up again a 963 * few lines later when we want to add the new entry. 964 */ 965static inthave_duplicate_entry(const struct object_id *oid, 966int exclude, 967uint32_t*index_pos) 968{ 969struct object_entry *entry; 970 971 entry =packlist_find(&to_pack, oid->hash, index_pos); 972if(!entry) 973return0; 974 975if(exclude) { 976if(!entry->preferred_base) 977 nr_result--; 978 entry->preferred_base =1; 979} 980 981return1; 982} 983 984static intwant_found_object(int exclude,struct packed_git *p) 985{ 986if(exclude) 987return1; 988if(incremental) 989return0; 990 991/* 992 * When asked to do --local (do not include an object that appears in a 993 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 994 * an object that appears in a pack marked with .keep), finding a pack 995 * that matches the criteria is sufficient for us to decide to omit it. 996 * However, even if this pack does not satisfy the criteria, we need to 997 * make sure no copy of this object appears in _any_ pack that makes us 998 * to omit the object, so we need to check all the packs. 999 *1000 * We can however first check whether these options can possible matter;1001 * if they do not matter we know we want the object in generated pack.1002 * Otherwise, we signal "-1" at the end to tell the caller that we do1003 * not know either way, and it needs to check more packs.1004 */1005if(!ignore_packed_keep_on_disk &&1006!ignore_packed_keep_in_core &&1007(!local || !have_non_local_packs))1008return1;10091010if(local && !p->pack_local)1011return0;1012if(p->pack_local &&1013((ignore_packed_keep_on_disk && p->pack_keep) ||1014(ignore_packed_keep_in_core && p->pack_keep_in_core)))1015return0;10161017/* we don't know yet; keep looking for more packs */1018return-1;1019}10201021/*1022 * Check whether we want the object in the pack (e.g., we do not want1023 * objects found in non-local stores if the "--local" option was used).1024 *1025 * If the caller already knows an existing pack it wants to take the object1026 * from, that is passed in *found_pack and *found_offset; otherwise this1027 * function finds if there is any pack that has the object and returns the pack1028 * and its offset in these variables.1029 */1030static intwant_object_in_pack(const struct object_id *oid,1031int exclude,1032struct packed_git **found_pack,1033 off_t *found_offset)1034{1035int want;1036struct list_head *pos;10371038if(!exclude && local &&has_loose_object_nonlocal(oid))1039return0;10401041/*1042 * If we already know the pack object lives in, start checks from that1043 * pack - in the usual case when neither --local was given nor .keep files1044 * are present we will determine the answer right now.1045 */1046if(*found_pack) {1047 want =want_found_object(exclude, *found_pack);1048if(want != -1)1049return want;1050}1051list_for_each(pos,get_packed_git_mru(the_repository)) {1052struct packed_git *p =list_entry(pos,struct packed_git, mru);1053 off_t offset;10541055if(p == *found_pack)1056 offset = *found_offset;1057else1058 offset =find_pack_entry_one(oid->hash, p);10591060if(offset) {1061if(!*found_pack) {1062if(!is_pack_valid(p))1063continue;1064*found_offset = offset;1065*found_pack = p;1066}1067 want =want_found_object(exclude, p);1068if(!exclude && want >0)1069list_move(&p->mru,1070get_packed_git_mru(the_repository));1071if(want != -1)1072return want;1073}1074}10751076return1;1077}10781079static voidcreate_object_entry(const struct object_id *oid,1080enum object_type type,1081uint32_t hash,1082int exclude,1083int no_try_delta,1084uint32_t index_pos,1085struct packed_git *found_pack,1086 off_t found_offset)1087{1088struct object_entry *entry;10891090 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1091 entry->hash = hash;1092oe_set_type(entry, type);1093if(exclude)1094 entry->preferred_base =1;1095else1096 nr_result++;1097if(found_pack) {1098oe_set_in_pack(&to_pack, entry, found_pack);1099 entry->in_pack_offset = found_offset;1100}11011102 entry->no_try_delta = no_try_delta;1103}11041105static const char no_closure_warning[] =N_(1106"disabling bitmap writing, as some objects are not being packed"1107);11081109static intadd_object_entry(const struct object_id *oid,enum object_type type,1110const char*name,int exclude)1111{1112struct packed_git *found_pack = NULL;1113 off_t found_offset =0;1114uint32_t index_pos;11151116display_progress(progress_state, ++nr_seen);11171118if(have_duplicate_entry(oid, exclude, &index_pos))1119return0;11201121if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1122/* The pack is missing an object, so it will not have closure */1123if(write_bitmap_index) {1124warning(_(no_closure_warning));1125 write_bitmap_index =0;1126}1127return0;1128}11291130create_object_entry(oid, type,pack_name_hash(name),1131 exclude, name &&no_try_delta(name),1132 index_pos, found_pack, found_offset);1133return1;1134}11351136static intadd_object_entry_from_bitmap(const struct object_id *oid,1137enum object_type type,1138int flags,uint32_t name_hash,1139struct packed_git *pack, off_t offset)1140{1141uint32_t index_pos;11421143display_progress(progress_state, ++nr_seen);11441145if(have_duplicate_entry(oid,0, &index_pos))1146return0;11471148if(!want_object_in_pack(oid,0, &pack, &offset))1149return0;11501151create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1152return1;1153}11541155struct pbase_tree_cache {1156struct object_id oid;1157int ref;1158int temporary;1159void*tree_data;1160unsigned long tree_size;1161};11621163static struct pbase_tree_cache *(pbase_tree_cache[256]);1164static intpbase_tree_cache_ix(const struct object_id *oid)1165{1166return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1167}1168static intpbase_tree_cache_ix_incr(int ix)1169{1170return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1171}11721173static struct pbase_tree {1174struct pbase_tree *next;1175/* This is a phony "cache" entry; we are not1176 * going to evict it or find it through _get()1177 * mechanism -- this is for the toplevel node that1178 * would almost always change with any commit.1179 */1180struct pbase_tree_cache pcache;1181} *pbase_tree;11821183static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1184{1185struct pbase_tree_cache *ent, *nent;1186void*data;1187unsigned long size;1188enum object_type type;1189int neigh;1190int my_ix =pbase_tree_cache_ix(oid);1191int available_ix = -1;11921193/* pbase-tree-cache acts as a limited hashtable.1194 * your object will be found at your index or within a few1195 * slots after that slot if it is cached.1196 */1197for(neigh =0; neigh <8; neigh++) {1198 ent = pbase_tree_cache[my_ix];1199if(ent && !oidcmp(&ent->oid, oid)) {1200 ent->ref++;1201return ent;1202}1203else if(((available_ix <0) && (!ent || !ent->ref)) ||1204((0<= available_ix) &&1205(!ent && pbase_tree_cache[available_ix])))1206 available_ix = my_ix;1207if(!ent)1208break;1209 my_ix =pbase_tree_cache_ix_incr(my_ix);1210}12111212/* Did not find one. Either we got a bogus request or1213 * we need to read and perhaps cache.1214 */1215 data =read_object_file(oid, &type, &size);1216if(!data)1217return NULL;1218if(type != OBJ_TREE) {1219free(data);1220return NULL;1221}12221223/* We need to either cache or return a throwaway copy */12241225if(available_ix <0)1226 ent = NULL;1227else{1228 ent = pbase_tree_cache[available_ix];1229 my_ix = available_ix;1230}12311232if(!ent) {1233 nent =xmalloc(sizeof(*nent));1234 nent->temporary = (available_ix <0);1235}1236else{1237/* evict and reuse */1238free(ent->tree_data);1239 nent = ent;1240}1241oidcpy(&nent->oid, oid);1242 nent->tree_data = data;1243 nent->tree_size = size;1244 nent->ref =1;1245if(!nent->temporary)1246 pbase_tree_cache[my_ix] = nent;1247return nent;1248}12491250static voidpbase_tree_put(struct pbase_tree_cache *cache)1251{1252if(!cache->temporary) {1253 cache->ref--;1254return;1255}1256free(cache->tree_data);1257free(cache);1258}12591260static intname_cmp_len(const char*name)1261{1262int i;1263for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1264;1265return i;1266}12671268static voidadd_pbase_object(struct tree_desc *tree,1269const char*name,1270int cmplen,1271const char*fullname)1272{1273struct name_entry entry;1274int cmp;12751276while(tree_entry(tree,&entry)) {1277if(S_ISGITLINK(entry.mode))1278continue;1279 cmp =tree_entry_len(&entry) != cmplen ?1:1280memcmp(name, entry.path, cmplen);1281if(cmp >0)1282continue;1283if(cmp <0)1284return;1285if(name[cmplen] !='/') {1286add_object_entry(entry.oid,1287object_type(entry.mode),1288 fullname,1);1289return;1290}1291if(S_ISDIR(entry.mode)) {1292struct tree_desc sub;1293struct pbase_tree_cache *tree;1294const char*down = name+cmplen+1;1295int downlen =name_cmp_len(down);12961297 tree =pbase_tree_get(entry.oid);1298if(!tree)1299return;1300init_tree_desc(&sub, tree->tree_data, tree->tree_size);13011302add_pbase_object(&sub, down, downlen, fullname);1303pbase_tree_put(tree);1304}1305}1306}13071308static unsigned*done_pbase_paths;1309static int done_pbase_paths_num;1310static int done_pbase_paths_alloc;1311static intdone_pbase_path_pos(unsigned hash)1312{1313int lo =0;1314int hi = done_pbase_paths_num;1315while(lo < hi) {1316int mi = lo + (hi - lo) /2;1317if(done_pbase_paths[mi] == hash)1318return mi;1319if(done_pbase_paths[mi] < hash)1320 hi = mi;1321else1322 lo = mi +1;1323}1324return-lo-1;1325}13261327static intcheck_pbase_path(unsigned hash)1328{1329int pos =done_pbase_path_pos(hash);1330if(0<= pos)1331return1;1332 pos = -pos -1;1333ALLOC_GROW(done_pbase_paths,1334 done_pbase_paths_num +1,1335 done_pbase_paths_alloc);1336 done_pbase_paths_num++;1337if(pos < done_pbase_paths_num)1338MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1339 done_pbase_paths_num - pos -1);1340 done_pbase_paths[pos] = hash;1341return0;1342}13431344static voidadd_preferred_base_object(const char*name)1345{1346struct pbase_tree *it;1347int cmplen;1348unsigned hash =pack_name_hash(name);13491350if(!num_preferred_base ||check_pbase_path(hash))1351return;13521353 cmplen =name_cmp_len(name);1354for(it = pbase_tree; it; it = it->next) {1355if(cmplen ==0) {1356add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1357}1358else{1359struct tree_desc tree;1360init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1361add_pbase_object(&tree, name, cmplen, name);1362}1363}1364}13651366static voidadd_preferred_base(struct object_id *oid)1367{1368struct pbase_tree *it;1369void*data;1370unsigned long size;1371struct object_id tree_oid;13721373if(window <= num_preferred_base++)1374return;13751376 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1377if(!data)1378return;13791380for(it = pbase_tree; it; it = it->next) {1381if(!oidcmp(&it->pcache.oid, &tree_oid)) {1382free(data);1383return;1384}1385}13861387 it =xcalloc(1,sizeof(*it));1388 it->next = pbase_tree;1389 pbase_tree = it;13901391oidcpy(&it->pcache.oid, &tree_oid);1392 it->pcache.tree_data = data;1393 it->pcache.tree_size = size;1394}13951396static voidcleanup_preferred_base(void)1397{1398struct pbase_tree *it;1399unsigned i;14001401 it = pbase_tree;1402 pbase_tree = NULL;1403while(it) {1404struct pbase_tree *tmp = it;1405 it = tmp->next;1406free(tmp->pcache.tree_data);1407free(tmp);1408}14091410for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1411if(!pbase_tree_cache[i])1412continue;1413free(pbase_tree_cache[i]->tree_data);1414FREE_AND_NULL(pbase_tree_cache[i]);1415}14161417FREE_AND_NULL(done_pbase_paths);1418 done_pbase_paths_num = done_pbase_paths_alloc =0;1419}14201421static voidcheck_object(struct object_entry *entry)1422{1423unsigned long canonical_size;14241425if(IN_PACK(entry)) {1426struct packed_git *p =IN_PACK(entry);1427struct pack_window *w_curs = NULL;1428const unsigned char*base_ref = NULL;1429struct object_entry *base_entry;1430unsigned long used, used_0;1431unsigned long avail;1432 off_t ofs;1433unsigned char*buf, c;1434enum object_type type;1435unsigned long in_pack_size;14361437 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14381439/*1440 * We want in_pack_type even if we do not reuse delta1441 * since non-delta representations could still be reused.1442 */1443 used =unpack_object_header_buffer(buf, avail,1444&type,1445&in_pack_size);1446if(used ==0)1447goto give_up;14481449if(type <0)1450BUG("invalid type%d", type);1451 entry->in_pack_type = type;14521453/*1454 * Determine if this is a delta and if so whether we can1455 * reuse it or not. Otherwise let's find out as cheaply as1456 * possible what the actual type and size for this object is.1457 */1458switch(entry->in_pack_type) {1459default:1460/* Not a delta hence we've already got all we need. */1461oe_set_type(entry, entry->in_pack_type);1462SET_SIZE(entry, in_pack_size);1463 entry->in_pack_header_size = used;1464if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1465goto give_up;1466unuse_pack(&w_curs);1467return;1468case OBJ_REF_DELTA:1469if(reuse_delta && !entry->preferred_base)1470 base_ref =use_pack(p, &w_curs,1471 entry->in_pack_offset + used, NULL);1472 entry->in_pack_header_size = used + the_hash_algo->rawsz;1473break;1474case OBJ_OFS_DELTA:1475 buf =use_pack(p, &w_curs,1476 entry->in_pack_offset + used, NULL);1477 used_0 =0;1478 c = buf[used_0++];1479 ofs = c &127;1480while(c &128) {1481 ofs +=1;1482if(!ofs ||MSB(ofs,7)) {1483error("delta base offset overflow in pack for%s",1484oid_to_hex(&entry->idx.oid));1485goto give_up;1486}1487 c = buf[used_0++];1488 ofs = (ofs <<7) + (c &127);1489}1490 ofs = entry->in_pack_offset - ofs;1491if(ofs <=0|| ofs >= entry->in_pack_offset) {1492error("delta base offset out of bound for%s",1493oid_to_hex(&entry->idx.oid));1494goto give_up;1495}1496if(reuse_delta && !entry->preferred_base) {1497struct revindex_entry *revidx;1498 revidx =find_pack_revindex(p, ofs);1499if(!revidx)1500goto give_up;1501 base_ref =nth_packed_object_sha1(p, revidx->nr);1502}1503 entry->in_pack_header_size = used + used_0;1504break;1505}15061507if(base_ref && (base_entry =packlist_find(&to_pack, base_ref, NULL))) {1508/*1509 * If base_ref was set above that means we wish to1510 * reuse delta data, and we even found that base1511 * in the list of objects we want to pack. Goodie!1512 *1513 * Depth value does not matter - find_deltas() will1514 * never consider reused delta as the base object to1515 * deltify other objects against, in order to avoid1516 * circular deltas.1517 */1518oe_set_type(entry, entry->in_pack_type);1519SET_SIZE(entry, in_pack_size);/* delta size */1520SET_DELTA(entry, base_entry);1521SET_DELTA_SIZE(entry, in_pack_size);1522 entry->delta_sibling_idx = base_entry->delta_child_idx;1523SET_DELTA_CHILD(base_entry, entry);1524unuse_pack(&w_curs);1525return;1526}15271528if(oe_type(entry)) {1529 off_t delta_pos;15301531/*1532 * This must be a delta and we already know what the1533 * final object type is. Let's extract the actual1534 * object size from the delta header.1535 */1536 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1537 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1538if(canonical_size ==0)1539goto give_up;1540SET_SIZE(entry, canonical_size);1541unuse_pack(&w_curs);1542return;1543}15441545/*1546 * No choice but to fall back to the recursive delta walk1547 * with sha1_object_info() to find about the object type1548 * at this point...1549 */1550 give_up:1551unuse_pack(&w_curs);1552}15531554oe_set_type(entry,1555oid_object_info(the_repository, &entry->idx.oid, &canonical_size));1556if(entry->type_valid) {1557SET_SIZE(entry, canonical_size);1558}else{1559/*1560 * Bad object type is checked in prepare_pack(). This is1561 * to permit a missing preferred base object to be ignored1562 * as a preferred base. Doing so can result in a larger1563 * pack file, but the transfer will still take place.1564 */1565}1566}15671568static intpack_offset_sort(const void*_a,const void*_b)1569{1570const struct object_entry *a = *(struct object_entry **)_a;1571const struct object_entry *b = *(struct object_entry **)_b;1572const struct packed_git *a_in_pack =IN_PACK(a);1573const struct packed_git *b_in_pack =IN_PACK(b);15741575/* avoid filesystem trashing with loose objects */1576if(!a_in_pack && !b_in_pack)1577returnoidcmp(&a->idx.oid, &b->idx.oid);15781579if(a_in_pack < b_in_pack)1580return-1;1581if(a_in_pack > b_in_pack)1582return1;1583return a->in_pack_offset < b->in_pack_offset ? -1:1584(a->in_pack_offset > b->in_pack_offset);1585}15861587/*1588 * Drop an on-disk delta we were planning to reuse. Naively, this would1589 * just involve blanking out the "delta" field, but we have to deal1590 * with some extra book-keeping:1591 *1592 * 1. Removing ourselves from the delta_sibling linked list.1593 *1594 * 2. Updating our size/type to the non-delta representation. These were1595 * either not recorded initially (size) or overwritten with the delta type1596 * (type) when check_object() decided to reuse the delta.1597 *1598 * 3. Resetting our delta depth, as we are now a base object.1599 */1600static voiddrop_reused_delta(struct object_entry *entry)1601{1602unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1603struct object_info oi = OBJECT_INFO_INIT;1604enum object_type type;1605unsigned long size;16061607while(*idx) {1608struct object_entry *oe = &to_pack.objects[*idx -1];16091610if(oe == entry)1611*idx = oe->delta_sibling_idx;1612else1613 idx = &oe->delta_sibling_idx;1614}1615SET_DELTA(entry, NULL);1616 entry->depth =0;16171618 oi.sizep = &size;1619 oi.typep = &type;1620if(packed_object_info(the_repository,IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1621/*1622 * We failed to get the info from this pack for some reason;1623 * fall back to sha1_object_info, which may find another copy.1624 * And if that fails, the error will be recorded in oe_type(entry)1625 * and dealt with in prepare_pack().1626 */1627oe_set_type(entry,1628oid_object_info(the_repository, &entry->idx.oid, &size));1629}else{1630oe_set_type(entry, type);1631}1632SET_SIZE(entry, size);1633}16341635/*1636 * Follow the chain of deltas from this entry onward, throwing away any links1637 * that cause us to hit a cycle (as determined by the DFS state flags in1638 * the entries).1639 *1640 * We also detect too-long reused chains that would violate our --depth1641 * limit.1642 */1643static voidbreak_delta_chains(struct object_entry *entry)1644{1645/*1646 * The actual depth of each object we will write is stored as an int,1647 * as it cannot exceed our int "depth" limit. But before we break1648 * changes based no that limit, we may potentially go as deep as the1649 * number of objects, which is elsewhere bounded to a uint32_t.1650 */1651uint32_t total_depth;1652struct object_entry *cur, *next;16531654for(cur = entry, total_depth =0;1655 cur;1656 cur =DELTA(cur), total_depth++) {1657if(cur->dfs_state == DFS_DONE) {1658/*1659 * We've already seen this object and know it isn't1660 * part of a cycle. We do need to append its depth1661 * to our count.1662 */1663 total_depth += cur->depth;1664break;1665}16661667/*1668 * We break cycles before looping, so an ACTIVE state (or any1669 * other cruft which made its way into the state variable)1670 * is a bug.1671 */1672if(cur->dfs_state != DFS_NONE)1673BUG("confusing delta dfs state in first pass:%d",1674 cur->dfs_state);16751676/*1677 * Now we know this is the first time we've seen the object. If1678 * it's not a delta, we're done traversing, but we'll mark it1679 * done to save time on future traversals.1680 */1681if(!DELTA(cur)) {1682 cur->dfs_state = DFS_DONE;1683break;1684}16851686/*1687 * Mark ourselves as active and see if the next step causes1688 * us to cycle to another active object. It's important to do1689 * this _before_ we loop, because it impacts where we make the1690 * cut, and thus how our total_depth counter works.1691 * E.g., We may see a partial loop like:1692 *1693 * A -> B -> C -> D -> B1694 *1695 * Cutting B->C breaks the cycle. But now the depth of A is1696 * only 1, and our total_depth counter is at 3. The size of the1697 * error is always one less than the size of the cycle we1698 * broke. Commits C and D were "lost" from A's chain.1699 *1700 * If we instead cut D->B, then the depth of A is correct at 3.1701 * We keep all commits in the chain that we examined.1702 */1703 cur->dfs_state = DFS_ACTIVE;1704if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1705drop_reused_delta(cur);1706 cur->dfs_state = DFS_DONE;1707break;1708}1709}17101711/*1712 * And now that we've gone all the way to the bottom of the chain, we1713 * need to clear the active flags and set the depth fields as1714 * appropriate. Unlike the loop above, which can quit when it drops a1715 * delta, we need to keep going to look for more depth cuts. So we need1716 * an extra "next" pointer to keep going after we reset cur->delta.1717 */1718for(cur = entry; cur; cur = next) {1719 next =DELTA(cur);17201721/*1722 * We should have a chain of zero or more ACTIVE states down to1723 * a final DONE. We can quit after the DONE, because either it1724 * has no bases, or we've already handled them in a previous1725 * call.1726 */1727if(cur->dfs_state == DFS_DONE)1728break;1729else if(cur->dfs_state != DFS_ACTIVE)1730BUG("confusing delta dfs state in second pass:%d",1731 cur->dfs_state);17321733/*1734 * If the total_depth is more than depth, then we need to snip1735 * the chain into two or more smaller chains that don't exceed1736 * the maximum depth. Most of the resulting chains will contain1737 * (depth + 1) entries (i.e., depth deltas plus one base), and1738 * the last chain (i.e., the one containing entry) will contain1739 * whatever entries are left over, namely1740 * (total_depth % (depth + 1)) of them.1741 *1742 * Since we are iterating towards decreasing depth, we need to1743 * decrement total_depth as we go, and we need to write to the1744 * entry what its final depth will be after all of the1745 * snipping. Since we're snipping into chains of length (depth1746 * + 1) entries, the final depth of an entry will be its1747 * original depth modulo (depth + 1). Any time we encounter an1748 * entry whose final depth is supposed to be zero, we snip it1749 * from its delta base, thereby making it so.1750 */1751 cur->depth = (total_depth--) % (depth +1);1752if(!cur->depth)1753drop_reused_delta(cur);17541755 cur->dfs_state = DFS_DONE;1756}1757}17581759static voidget_object_details(void)1760{1761uint32_t i;1762struct object_entry **sorted_by_offset;17631764if(progress)1765 progress_state =start_progress(_("Counting objects"),1766 to_pack.nr_objects);17671768 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1769for(i =0; i < to_pack.nr_objects; i++)1770 sorted_by_offset[i] = to_pack.objects + i;1771QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17721773for(i =0; i < to_pack.nr_objects; i++) {1774struct object_entry *entry = sorted_by_offset[i];1775check_object(entry);1776if(entry->type_valid &&1777oe_size_greater_than(&to_pack, entry, big_file_threshold))1778 entry->no_try_delta =1;1779display_progress(progress_state, i +1);1780}1781stop_progress(&progress_state);17821783/*1784 * This must happen in a second pass, since we rely on the delta1785 * information for the whole list being completed.1786 */1787for(i =0; i < to_pack.nr_objects; i++)1788break_delta_chains(&to_pack.objects[i]);17891790free(sorted_by_offset);1791}17921793/*1794 * We search for deltas in a list sorted by type, by filename hash, and then1795 * by size, so that we see progressively smaller and smaller files.1796 * That's because we prefer deltas to be from the bigger file1797 * to the smaller -- deletes are potentially cheaper, but perhaps1798 * more importantly, the bigger file is likely the more recent1799 * one. The deepest deltas are therefore the oldest objects which are1800 * less susceptible to be accessed often.1801 */1802static inttype_size_sort(const void*_a,const void*_b)1803{1804const struct object_entry *a = *(struct object_entry **)_a;1805const struct object_entry *b = *(struct object_entry **)_b;1806enum object_type a_type =oe_type(a);1807enum object_type b_type =oe_type(b);1808unsigned long a_size =SIZE(a);1809unsigned long b_size =SIZE(b);18101811if(a_type > b_type)1812return-1;1813if(a_type < b_type)1814return1;1815if(a->hash > b->hash)1816return-1;1817if(a->hash < b->hash)1818return1;1819if(a->preferred_base > b->preferred_base)1820return-1;1821if(a->preferred_base < b->preferred_base)1822return1;1823if(a_size > b_size)1824return-1;1825if(a_size < b_size)1826return1;1827return a < b ? -1: (a > b);/* newest first */1828}18291830struct unpacked {1831struct object_entry *entry;1832void*data;1833struct delta_index *index;1834unsigned depth;1835};18361837static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1838unsigned long delta_size)1839{1840if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1841return0;18421843if(delta_size < cache_max_small_delta_size)1844return1;18451846/* cache delta, if objects are large enough compared to delta size */1847if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1848return1;18491850return0;1851}18521853#ifndef NO_PTHREADS18541855/* Protect access to object database */1856static pthread_mutex_t read_mutex;1857#define read_lock() pthread_mutex_lock(&read_mutex)1858#define read_unlock() pthread_mutex_unlock(&read_mutex)18591860/* Protect delta_cache_size */1861static pthread_mutex_t cache_mutex;1862#define cache_lock() pthread_mutex_lock(&cache_mutex)1863#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18641865/*1866 * Protect object list partitioning (e.g. struct thread_param) and1867 * progress_state1868 */1869static pthread_mutex_t progress_mutex;1870#define progress_lock() pthread_mutex_lock(&progress_mutex)1871#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18721873/*1874 * Access to struct object_entry is unprotected since each thread owns1875 * a portion of the main object list. Just don't access object entries1876 * ahead in the list because they can be stolen and would need1877 * progress_mutex for protection.1878 */1879#else18801881#define read_lock() (void)01882#define read_unlock() (void)01883#define cache_lock() (void)01884#define cache_unlock() (void)01885#define progress_lock() (void)01886#define progress_unlock() (void)018871888#endif18891890/*1891 * Return the size of the object without doing any delta1892 * reconstruction (so non-deltas are true object sizes, but deltas1893 * return the size of the delta data).1894 */1895unsigned longoe_get_size_slow(struct packing_data *pack,1896const struct object_entry *e)1897{1898struct packed_git *p;1899struct pack_window *w_curs;1900unsigned char*buf;1901enum object_type type;1902unsigned long used, avail, size;19031904if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1905read_lock();1906if(oid_object_info(the_repository, &e->idx.oid, &size) <0)1907die(_("unable to get size of%s"),1908oid_to_hex(&e->idx.oid));1909read_unlock();1910return size;1911}19121913 p =oe_in_pack(pack, e);1914if(!p)1915BUG("when e->type is a delta, it must belong to a pack");19161917read_lock();1918 w_curs = NULL;1919 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);1920 used =unpack_object_header_buffer(buf, avail, &type, &size);1921if(used ==0)1922die(_("unable to parse object header of%s"),1923oid_to_hex(&e->idx.oid));19241925unuse_pack(&w_curs);1926read_unlock();1927return size;1928}19291930static inttry_delta(struct unpacked *trg,struct unpacked *src,1931unsigned max_depth,unsigned long*mem_usage)1932{1933struct object_entry *trg_entry = trg->entry;1934struct object_entry *src_entry = src->entry;1935unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1936unsigned ref_depth;1937enum object_type type;1938void*delta_buf;19391940/* Don't bother doing diffs between different types */1941if(oe_type(trg_entry) !=oe_type(src_entry))1942return-1;19431944/*1945 * We do not bother to try a delta that we discarded on an1946 * earlier try, but only when reusing delta data. Note that1947 * src_entry that is marked as the preferred_base should always1948 * be considered, as even if we produce a suboptimal delta against1949 * it, we will still save the transfer cost, as we already know1950 * the other side has it and we won't send src_entry at all.1951 */1952if(reuse_delta &&IN_PACK(trg_entry) &&1953IN_PACK(trg_entry) ==IN_PACK(src_entry) &&1954!src_entry->preferred_base &&1955 trg_entry->in_pack_type != OBJ_REF_DELTA &&1956 trg_entry->in_pack_type != OBJ_OFS_DELTA)1957return0;19581959/* Let's not bust the allowed depth. */1960if(src->depth >= max_depth)1961return0;19621963/* Now some size filtering heuristics. */1964 trg_size =SIZE(trg_entry);1965if(!DELTA(trg_entry)) {1966 max_size = trg_size/2- the_hash_algo->rawsz;1967 ref_depth =1;1968}else{1969 max_size =DELTA_SIZE(trg_entry);1970 ref_depth = trg->depth;1971}1972 max_size = (uint64_t)max_size * (max_depth - src->depth) /1973(max_depth - ref_depth +1);1974if(max_size ==0)1975return0;1976 src_size =SIZE(src_entry);1977 sizediff = src_size < trg_size ? trg_size - src_size :0;1978if(sizediff >= max_size)1979return0;1980if(trg_size < src_size /32)1981return0;19821983/* Load data if not already done */1984if(!trg->data) {1985read_lock();1986 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1987read_unlock();1988if(!trg->data)1989die("object%scannot be read",1990oid_to_hex(&trg_entry->idx.oid));1991if(sz != trg_size)1992die("object%sinconsistent object length (%lu vs%lu)",1993oid_to_hex(&trg_entry->idx.oid), sz,1994 trg_size);1995*mem_usage += sz;1996}1997if(!src->data) {1998read_lock();1999 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);2000read_unlock();2001if(!src->data) {2002if(src_entry->preferred_base) {2003static int warned =0;2004if(!warned++)2005warning("object%scannot be read",2006oid_to_hex(&src_entry->idx.oid));2007/*2008 * Those objects are not included in the2009 * resulting pack. Be resilient and ignore2010 * them if they can't be read, in case the2011 * pack could be created nevertheless.2012 */2013return0;2014}2015die("object%scannot be read",2016oid_to_hex(&src_entry->idx.oid));2017}2018if(sz != src_size)2019die("object%sinconsistent object length (%lu vs%lu)",2020oid_to_hex(&src_entry->idx.oid), sz,2021 src_size);2022*mem_usage += sz;2023}2024if(!src->index) {2025 src->index =create_delta_index(src->data, src_size);2026if(!src->index) {2027static int warned =0;2028if(!warned++)2029warning("suboptimal pack - out of memory");2030return0;2031}2032*mem_usage +=sizeof_delta_index(src->index);2033}20342035 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2036if(!delta_buf)2037return0;2038if(delta_size >= (1U<< OE_DELTA_SIZE_BITS)) {2039free(delta_buf);2040return0;2041}20422043if(DELTA(trg_entry)) {2044/* Prefer only shallower same-sized deltas. */2045if(delta_size ==DELTA_SIZE(trg_entry) &&2046 src->depth +1>= trg->depth) {2047free(delta_buf);2048return0;2049}2050}20512052/*2053 * Handle memory allocation outside of the cache2054 * accounting lock. Compiler will optimize the strangeness2055 * away when NO_PTHREADS is defined.2056 */2057free(trg_entry->delta_data);2058cache_lock();2059if(trg_entry->delta_data) {2060 delta_cache_size -=DELTA_SIZE(trg_entry);2061 trg_entry->delta_data = NULL;2062}2063if(delta_cacheable(src_size, trg_size, delta_size)) {2064 delta_cache_size += delta_size;2065cache_unlock();2066 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2067}else{2068cache_unlock();2069free(delta_buf);2070}20712072SET_DELTA(trg_entry, src_entry);2073SET_DELTA_SIZE(trg_entry, delta_size);2074 trg->depth = src->depth +1;20752076return1;2077}20782079static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2080{2081struct object_entry *child =DELTA_CHILD(me);2082unsigned int m = n;2083while(child) {2084unsigned int c =check_delta_limit(child, n +1);2085if(m < c)2086 m = c;2087 child =DELTA_SIBLING(child);2088}2089return m;2090}20912092static unsigned longfree_unpacked(struct unpacked *n)2093{2094unsigned long freed_mem =sizeof_delta_index(n->index);2095free_delta_index(n->index);2096 n->index = NULL;2097if(n->data) {2098 freed_mem +=SIZE(n->entry);2099FREE_AND_NULL(n->data);2100}2101 n->entry = NULL;2102 n->depth =0;2103return freed_mem;2104}21052106static voidfind_deltas(struct object_entry **list,unsigned*list_size,2107int window,int depth,unsigned*processed)2108{2109uint32_t i, idx =0, count =0;2110struct unpacked *array;2111unsigned long mem_usage =0;21122113 array =xcalloc(window,sizeof(struct unpacked));21142115for(;;) {2116struct object_entry *entry;2117struct unpacked *n = array + idx;2118int j, max_depth, best_base = -1;21192120progress_lock();2121if(!*list_size) {2122progress_unlock();2123break;2124}2125 entry = *list++;2126(*list_size)--;2127if(!entry->preferred_base) {2128(*processed)++;2129display_progress(progress_state, *processed);2130}2131progress_unlock();21322133 mem_usage -=free_unpacked(n);2134 n->entry = entry;21352136while(window_memory_limit &&2137 mem_usage > window_memory_limit &&2138 count >1) {2139uint32_t tail = (idx + window - count) % window;2140 mem_usage -=free_unpacked(array + tail);2141 count--;2142}21432144/* We do not compute delta to *create* objects we are not2145 * going to pack.2146 */2147if(entry->preferred_base)2148goto next;21492150/*2151 * If the current object is at pack edge, take the depth the2152 * objects that depend on the current object into account2153 * otherwise they would become too deep.2154 */2155 max_depth = depth;2156if(DELTA_CHILD(entry)) {2157 max_depth -=check_delta_limit(entry,0);2158if(max_depth <=0)2159goto next;2160}21612162 j = window;2163while(--j >0) {2164int ret;2165uint32_t other_idx = idx + j;2166struct unpacked *m;2167if(other_idx >= window)2168 other_idx -= window;2169 m = array + other_idx;2170if(!m->entry)2171break;2172 ret =try_delta(n, m, max_depth, &mem_usage);2173if(ret <0)2174break;2175else if(ret >0)2176 best_base = other_idx;2177}21782179/*2180 * If we decided to cache the delta data, then it is best2181 * to compress it right away. First because we have to do2182 * it anyway, and doing it here while we're threaded will2183 * save a lot of time in the non threaded write phase,2184 * as well as allow for caching more deltas within2185 * the same cache size limit.2186 * ...2187 * But only if not writing to stdout, since in that case2188 * the network is most likely throttling writes anyway,2189 * and therefore it is best to go to the write phase ASAP2190 * instead, as we can afford spending more time compressing2191 * between writes at that moment.2192 */2193if(entry->delta_data && !pack_to_stdout) {2194unsigned long size;21952196 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2197if(size < (1U<< OE_Z_DELTA_BITS)) {2198 entry->z_delta_size = size;2199cache_lock();2200 delta_cache_size -=DELTA_SIZE(entry);2201 delta_cache_size += entry->z_delta_size;2202cache_unlock();2203}else{2204FREE_AND_NULL(entry->delta_data);2205 entry->z_delta_size =0;2206}2207}22082209/* if we made n a delta, and if n is already at max2210 * depth, leaving it in the window is pointless. we2211 * should evict it first.2212 */2213if(DELTA(entry) && max_depth <= n->depth)2214continue;22152216/*2217 * Move the best delta base up in the window, after the2218 * currently deltified object, to keep it longer. It will2219 * be the first base object to be attempted next.2220 */2221if(DELTA(entry)) {2222struct unpacked swap = array[best_base];2223int dist = (window + idx - best_base) % window;2224int dst = best_base;2225while(dist--) {2226int src = (dst +1) % window;2227 array[dst] = array[src];2228 dst = src;2229}2230 array[dst] = swap;2231}22322233 next:2234 idx++;2235if(count +1< window)2236 count++;2237if(idx >= window)2238 idx =0;2239}22402241for(i =0; i < window; ++i) {2242free_delta_index(array[i].index);2243free(array[i].data);2244}2245free(array);2246}22472248#ifndef NO_PTHREADS22492250static voidtry_to_free_from_threads(size_t size)2251{2252read_lock();2253release_pack_memory(size);2254read_unlock();2255}22562257static try_to_free_t old_try_to_free_routine;22582259/*2260 * The main object list is split into smaller lists, each is handed to2261 * one worker.2262 *2263 * The main thread waits on the condition that (at least) one of the workers2264 * has stopped working (which is indicated in the .working member of2265 * struct thread_params).2266 *2267 * When a work thread has completed its work, it sets .working to 0 and2268 * signals the main thread and waits on the condition that .data_ready2269 * becomes 1.2270 *2271 * The main thread steals half of the work from the worker that has2272 * most work left to hand it to the idle worker.2273 */22742275struct thread_params {2276 pthread_t thread;2277struct object_entry **list;2278unsigned list_size;2279unsigned remaining;2280int window;2281int depth;2282int working;2283int data_ready;2284 pthread_mutex_t mutex;2285 pthread_cond_t cond;2286unsigned*processed;2287};22882289static pthread_cond_t progress_cond;22902291/*2292 * Mutex and conditional variable can't be statically-initialized on Windows.2293 */2294static voidinit_threaded_search(void)2295{2296init_recursive_mutex(&read_mutex);2297pthread_mutex_init(&cache_mutex, NULL);2298pthread_mutex_init(&progress_mutex, NULL);2299pthread_cond_init(&progress_cond, NULL);2300 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2301}23022303static voidcleanup_threaded_search(void)2304{2305set_try_to_free_routine(old_try_to_free_routine);2306pthread_cond_destroy(&progress_cond);2307pthread_mutex_destroy(&read_mutex);2308pthread_mutex_destroy(&cache_mutex);2309pthread_mutex_destroy(&progress_mutex);2310}23112312static void*threaded_find_deltas(void*arg)2313{2314struct thread_params *me = arg;23152316progress_lock();2317while(me->remaining) {2318progress_unlock();23192320find_deltas(me->list, &me->remaining,2321 me->window, me->depth, me->processed);23222323progress_lock();2324 me->working =0;2325pthread_cond_signal(&progress_cond);2326progress_unlock();23272328/*2329 * We must not set ->data_ready before we wait on the2330 * condition because the main thread may have set it to 12331 * before we get here. In order to be sure that new2332 * work is available if we see 1 in ->data_ready, it2333 * was initialized to 0 before this thread was spawned2334 * and we reset it to 0 right away.2335 */2336pthread_mutex_lock(&me->mutex);2337while(!me->data_ready)2338pthread_cond_wait(&me->cond, &me->mutex);2339 me->data_ready =0;2340pthread_mutex_unlock(&me->mutex);23412342progress_lock();2343}2344progress_unlock();2345/* leave ->working 1 so that this doesn't get more work assigned */2346return NULL;2347}23482349static voidll_find_deltas(struct object_entry **list,unsigned list_size,2350int window,int depth,unsigned*processed)2351{2352struct thread_params *p;2353int i, ret, active_threads =0;23542355init_threaded_search();23562357if(delta_search_threads <=1) {2358find_deltas(list, &list_size, window, depth, processed);2359cleanup_threaded_search();2360return;2361}2362if(progress > pack_to_stdout)2363fprintf(stderr,"Delta compression using up to%dthreads.\n",2364 delta_search_threads);2365 p =xcalloc(delta_search_threads,sizeof(*p));23662367/* Partition the work amongst work threads. */2368for(i =0; i < delta_search_threads; i++) {2369unsigned sub_size = list_size / (delta_search_threads - i);23702371/* don't use too small segments or no deltas will be found */2372if(sub_size <2*window && i+1< delta_search_threads)2373 sub_size =0;23742375 p[i].window = window;2376 p[i].depth = depth;2377 p[i].processed = processed;2378 p[i].working =1;2379 p[i].data_ready =0;23802381/* try to split chunks on "path" boundaries */2382while(sub_size && sub_size < list_size &&2383 list[sub_size]->hash &&2384 list[sub_size]->hash == list[sub_size-1]->hash)2385 sub_size++;23862387 p[i].list = list;2388 p[i].list_size = sub_size;2389 p[i].remaining = sub_size;23902391 list += sub_size;2392 list_size -= sub_size;2393}23942395/* Start work threads. */2396for(i =0; i < delta_search_threads; i++) {2397if(!p[i].list_size)2398continue;2399pthread_mutex_init(&p[i].mutex, NULL);2400pthread_cond_init(&p[i].cond, NULL);2401 ret =pthread_create(&p[i].thread, NULL,2402 threaded_find_deltas, &p[i]);2403if(ret)2404die("unable to create thread:%s",strerror(ret));2405 active_threads++;2406}24072408/*2409 * Now let's wait for work completion. Each time a thread is done2410 * with its work, we steal half of the remaining work from the2411 * thread with the largest number of unprocessed objects and give2412 * it to that newly idle thread. This ensure good load balancing2413 * until the remaining object list segments are simply too short2414 * to be worth splitting anymore.2415 */2416while(active_threads) {2417struct thread_params *target = NULL;2418struct thread_params *victim = NULL;2419unsigned sub_size =0;24202421progress_lock();2422for(;;) {2423for(i =0; !target && i < delta_search_threads; i++)2424if(!p[i].working)2425 target = &p[i];2426if(target)2427break;2428pthread_cond_wait(&progress_cond, &progress_mutex);2429}24302431for(i =0; i < delta_search_threads; i++)2432if(p[i].remaining >2*window &&2433(!victim || victim->remaining < p[i].remaining))2434 victim = &p[i];2435if(victim) {2436 sub_size = victim->remaining /2;2437 list = victim->list + victim->list_size - sub_size;2438while(sub_size && list[0]->hash &&2439 list[0]->hash == list[-1]->hash) {2440 list++;2441 sub_size--;2442}2443if(!sub_size) {2444/*2445 * It is possible for some "paths" to have2446 * so many objects that no hash boundary2447 * might be found. Let's just steal the2448 * exact half in that case.2449 */2450 sub_size = victim->remaining /2;2451 list -= sub_size;2452}2453 target->list = list;2454 victim->list_size -= sub_size;2455 victim->remaining -= sub_size;2456}2457 target->list_size = sub_size;2458 target->remaining = sub_size;2459 target->working =1;2460progress_unlock();24612462pthread_mutex_lock(&target->mutex);2463 target->data_ready =1;2464pthread_cond_signal(&target->cond);2465pthread_mutex_unlock(&target->mutex);24662467if(!sub_size) {2468pthread_join(target->thread, NULL);2469pthread_cond_destroy(&target->cond);2470pthread_mutex_destroy(&target->mutex);2471 active_threads--;2472}2473}2474cleanup_threaded_search();2475free(p);2476}24772478#else2479#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2480#endif24812482static voidadd_tag_chain(const struct object_id *oid)2483{2484struct tag *tag;24852486/*2487 * We catch duplicates already in add_object_entry(), but we'd2488 * prefer to do this extra check to avoid having to parse the2489 * tag at all if we already know that it's being packed (e.g., if2490 * it was included via bitmaps, we would not have parsed it2491 * previously).2492 */2493if(packlist_find(&to_pack, oid->hash, NULL))2494return;24952496 tag =lookup_tag(oid);2497while(1) {2498if(!tag ||parse_tag(tag) || !tag->tagged)2499die("unable to pack objects reachable from tag%s",2500oid_to_hex(oid));25012502add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);25032504if(tag->tagged->type != OBJ_TAG)2505return;25062507 tag = (struct tag *)tag->tagged;2508}2509}25102511static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2512{2513struct object_id peeled;25142515if(starts_with(path,"refs/tags/") &&/* is a tag? */2516!peel_ref(path, &peeled) &&/* peelable? */2517packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2518add_tag_chain(oid);2519return0;2520}25212522static voidprepare_pack(int window,int depth)2523{2524struct object_entry **delta_list;2525uint32_t i, nr_deltas;2526unsigned n;25272528get_object_details();25292530/*2531 * If we're locally repacking then we need to be doubly careful2532 * from now on in order to make sure no stealth corruption gets2533 * propagated to the new pack. Clients receiving streamed packs2534 * should validate everything they get anyway so no need to incur2535 * the additional cost here in that case.2536 */2537if(!pack_to_stdout)2538 do_check_packed_object_crc =1;25392540if(!to_pack.nr_objects || !window || !depth)2541return;25422543ALLOC_ARRAY(delta_list, to_pack.nr_objects);2544 nr_deltas = n =0;25452546for(i =0; i < to_pack.nr_objects; i++) {2547struct object_entry *entry = to_pack.objects + i;25482549if(DELTA(entry))2550/* This happens if we decided to reuse existing2551 * delta from a pack. "reuse_delta &&" is implied.2552 */2553continue;25542555if(!entry->type_valid ||2556oe_size_less_than(&to_pack, entry,50))2557continue;25582559if(entry->no_try_delta)2560continue;25612562if(!entry->preferred_base) {2563 nr_deltas++;2564if(oe_type(entry) <0)2565die("unable to get type of object%s",2566oid_to_hex(&entry->idx.oid));2567}else{2568if(oe_type(entry) <0) {2569/*2570 * This object is not found, but we2571 * don't have to include it anyway.2572 */2573continue;2574}2575}25762577 delta_list[n++] = entry;2578}25792580if(nr_deltas && n >1) {2581unsigned nr_done =0;2582if(progress)2583 progress_state =start_progress(_("Compressing objects"),2584 nr_deltas);2585QSORT(delta_list, n, type_size_sort);2586ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2587stop_progress(&progress_state);2588if(nr_done != nr_deltas)2589die("inconsistency with delta count");2590}2591free(delta_list);2592}25932594static intgit_pack_config(const char*k,const char*v,void*cb)2595{2596if(!strcmp(k,"pack.window")) {2597 window =git_config_int(k, v);2598return0;2599}2600if(!strcmp(k,"pack.windowmemory")) {2601 window_memory_limit =git_config_ulong(k, v);2602return0;2603}2604if(!strcmp(k,"pack.depth")) {2605 depth =git_config_int(k, v);2606return0;2607}2608if(!strcmp(k,"pack.deltacachesize")) {2609 max_delta_cache_size =git_config_int(k, v);2610return0;2611}2612if(!strcmp(k,"pack.deltacachelimit")) {2613 cache_max_small_delta_size =git_config_int(k, v);2614return0;2615}2616if(!strcmp(k,"pack.writebitmaphashcache")) {2617if(git_config_bool(k, v))2618 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2619else2620 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2621}2622if(!strcmp(k,"pack.usebitmaps")) {2623 use_bitmap_index_default =git_config_bool(k, v);2624return0;2625}2626if(!strcmp(k,"pack.threads")) {2627 delta_search_threads =git_config_int(k, v);2628if(delta_search_threads <0)2629die("invalid number of threads specified (%d)",2630 delta_search_threads);2631#ifdef NO_PTHREADS2632if(delta_search_threads !=1) {2633warning("no threads support, ignoring%s", k);2634 delta_search_threads =0;2635}2636#endif2637return0;2638}2639if(!strcmp(k,"pack.indexversion")) {2640 pack_idx_opts.version =git_config_int(k, v);2641if(pack_idx_opts.version >2)2642die("bad pack.indexversion=%"PRIu32,2643 pack_idx_opts.version);2644return0;2645}2646returngit_default_config(k, v, cb);2647}26482649static voidread_object_list_from_stdin(void)2650{2651char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2652struct object_id oid;2653const char*p;26542655for(;;) {2656if(!fgets(line,sizeof(line), stdin)) {2657if(feof(stdin))2658break;2659if(!ferror(stdin))2660die("fgets returned NULL, not EOF, not error!");2661if(errno != EINTR)2662die_errno("fgets");2663clearerr(stdin);2664continue;2665}2666if(line[0] =='-') {2667if(get_oid_hex(line+1, &oid))2668die("expected edge object ID, got garbage:\n%s",2669 line);2670add_preferred_base(&oid);2671continue;2672}2673if(parse_oid_hex(line, &oid, &p))2674die("expected object ID, got garbage:\n%s", line);26752676add_preferred_base_object(p +1);2677add_object_entry(&oid, OBJ_NONE, p +1,0);2678}2679}26802681/* Remember to update object flag allocation in object.h */2682#define OBJECT_ADDED (1u<<20)26832684static voidshow_commit(struct commit *commit,void*data)2685{2686add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2687 commit->object.flags |= OBJECT_ADDED;26882689if(write_bitmap_index)2690index_commit_for_bitmap(commit);2691}26922693static voidshow_object(struct object *obj,const char*name,void*data)2694{2695add_preferred_base_object(name);2696add_object_entry(&obj->oid, obj->type, name,0);2697 obj->flags |= OBJECT_ADDED;2698}26992700static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2701{2702assert(arg_missing_action == MA_ALLOW_ANY);27032704/*2705 * Quietly ignore ALL missing objects. This avoids problems with2706 * staging them now and getting an odd error later.2707 */2708if(!has_object_file(&obj->oid))2709return;27102711show_object(obj, name, data);2712}27132714static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2715{2716assert(arg_missing_action == MA_ALLOW_PROMISOR);27172718/*2719 * Quietly ignore EXPECTED missing objects. This avoids problems with2720 * staging them now and getting an odd error later.2721 */2722if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2723return;27242725show_object(obj, name, data);2726}27272728static intoption_parse_missing_action(const struct option *opt,2729const char*arg,int unset)2730{2731assert(arg);2732assert(!unset);27332734if(!strcmp(arg,"error")) {2735 arg_missing_action = MA_ERROR;2736 fn_show_object = show_object;2737return0;2738}27392740if(!strcmp(arg,"allow-any")) {2741 arg_missing_action = MA_ALLOW_ANY;2742 fetch_if_missing =0;2743 fn_show_object = show_object__ma_allow_any;2744return0;2745}27462747if(!strcmp(arg,"allow-promisor")) {2748 arg_missing_action = MA_ALLOW_PROMISOR;2749 fetch_if_missing =0;2750 fn_show_object = show_object__ma_allow_promisor;2751return0;2752}27532754die(_("invalid value for --missing"));2755return0;2756}27572758static voidshow_edge(struct commit *commit)2759{2760add_preferred_base(&commit->object.oid);2761}27622763struct in_pack_object {2764 off_t offset;2765struct object *object;2766};27672768struct in_pack {2769unsigned int alloc;2770unsigned int nr;2771struct in_pack_object *array;2772};27732774static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2775{2776 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2777 in_pack->array[in_pack->nr].object = object;2778 in_pack->nr++;2779}27802781/*2782 * Compare the objects in the offset order, in order to emulate the2783 * "git rev-list --objects" output that produced the pack originally.2784 */2785static intofscmp(const void*a_,const void*b_)2786{2787struct in_pack_object *a = (struct in_pack_object *)a_;2788struct in_pack_object *b = (struct in_pack_object *)b_;27892790if(a->offset < b->offset)2791return-1;2792else if(a->offset > b->offset)2793return1;2794else2795returnoidcmp(&a->object->oid, &b->object->oid);2796}27972798static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2799{2800struct packed_git *p;2801struct in_pack in_pack;2802uint32_t i;28032804memset(&in_pack,0,sizeof(in_pack));28052806for(p =get_packed_git(the_repository); p; p = p->next) {2807struct object_id oid;2808struct object *o;28092810if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2811continue;2812if(open_pack_index(p))2813die("cannot open pack index");28142815ALLOC_GROW(in_pack.array,2816 in_pack.nr + p->num_objects,2817 in_pack.alloc);28182819for(i =0; i < p->num_objects; i++) {2820nth_packed_object_oid(&oid, p, i);2821 o =lookup_unknown_object(oid.hash);2822if(!(o->flags & OBJECT_ADDED))2823mark_in_pack_object(o, p, &in_pack);2824 o->flags |= OBJECT_ADDED;2825}2826}28272828if(in_pack.nr) {2829QSORT(in_pack.array, in_pack.nr, ofscmp);2830for(i =0; i < in_pack.nr; i++) {2831struct object *o = in_pack.array[i].object;2832add_object_entry(&o->oid, o->type,"",0);2833}2834}2835free(in_pack.array);2836}28372838static intadd_loose_object(const struct object_id *oid,const char*path,2839void*data)2840{2841enum object_type type =oid_object_info(the_repository, oid, NULL);28422843if(type <0) {2844warning("loose object at%scould not be examined", path);2845return0;2846}28472848add_object_entry(oid, type,"",0);2849return0;2850}28512852/*2853 * We actually don't even have to worry about reachability here.2854 * add_object_entry will weed out duplicates, so we just add every2855 * loose object we find.2856 */2857static voidadd_unreachable_loose_objects(void)2858{2859for_each_loose_file_in_objdir(get_object_directory(),2860 add_loose_object,2861 NULL, NULL, NULL);2862}28632864static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2865{2866static struct packed_git *last_found = (void*)1;2867struct packed_git *p;28682869 p = (last_found != (void*)1) ? last_found :2870get_packed_git(the_repository);28712872while(p) {2873if((!p->pack_local || p->pack_keep ||2874 p->pack_keep_in_core) &&2875find_pack_entry_one(oid->hash, p)) {2876 last_found = p;2877return1;2878}2879if(p == last_found)2880 p =get_packed_git(the_repository);2881else2882 p = p->next;2883if(p == last_found)2884 p = p->next;2885}2886return0;2887}28882889/*2890 * Store a list of sha1s that are should not be discarded2891 * because they are either written too recently, or are2892 * reachable from another object that was.2893 *2894 * This is filled by get_object_list.2895 */2896static struct oid_array recent_objects;28972898static intloosened_object_can_be_discarded(const struct object_id *oid,2899 timestamp_t mtime)2900{2901if(!unpack_unreachable_expiration)2902return0;2903if(mtime > unpack_unreachable_expiration)2904return0;2905if(oid_array_lookup(&recent_objects, oid) >=0)2906return0;2907return1;2908}29092910static voidloosen_unused_packed_objects(struct rev_info *revs)2911{2912struct packed_git *p;2913uint32_t i;2914struct object_id oid;29152916for(p =get_packed_git(the_repository); p; p = p->next) {2917if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2918continue;29192920if(open_pack_index(p))2921die("cannot open pack index");29222923for(i =0; i < p->num_objects; i++) {2924nth_packed_object_oid(&oid, p, i);2925if(!packlist_find(&to_pack, oid.hash, NULL) &&2926!has_sha1_pack_kept_or_nonlocal(&oid) &&2927!loosened_object_can_be_discarded(&oid, p->mtime))2928if(force_object_loose(&oid, p->mtime))2929die("unable to force loose object");2930}2931}2932}29332934/*2935 * This tracks any options which pack-reuse code expects to be on, or which a2936 * reader of the pack might not understand, and which would therefore prevent2937 * blind reuse of what we have on disk.2938 */2939static intpack_options_allow_reuse(void)2940{2941return pack_to_stdout &&2942 allow_ofs_delta &&2943!ignore_packed_keep_on_disk &&2944!ignore_packed_keep_in_core &&2945(!local || !have_non_local_packs) &&2946!incremental;2947}29482949static intget_object_list_from_bitmap(struct rev_info *revs)2950{2951if(prepare_bitmap_walk(revs) <0)2952return-1;29532954if(pack_options_allow_reuse() &&2955!reuse_partial_packfile_from_bitmap(2956&reuse_packfile,2957&reuse_packfile_objects,2958&reuse_packfile_offset)) {2959assert(reuse_packfile_objects);2960 nr_result += reuse_packfile_objects;2961display_progress(progress_state, nr_result);2962}29632964traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2965return0;2966}29672968static voidrecord_recent_object(struct object *obj,2969const char*name,2970void*data)2971{2972oid_array_append(&recent_objects, &obj->oid);2973}29742975static voidrecord_recent_commit(struct commit *commit,void*data)2976{2977oid_array_append(&recent_objects, &commit->object.oid);2978}29792980static voidget_object_list(int ac,const char**av)2981{2982struct rev_info revs;2983char line[1000];2984int flags =0;29852986init_revisions(&revs, NULL);2987 save_commit_buffer =0;2988setup_revisions(ac, av, &revs, NULL);29892990/* make sure shallows are read */2991is_repository_shallow();29922993while(fgets(line,sizeof(line), stdin) != NULL) {2994int len =strlen(line);2995if(len && line[len -1] =='\n')2996 line[--len] =0;2997if(!len)2998break;2999if(*line =='-') {3000if(!strcmp(line,"--not")) {3001 flags ^= UNINTERESTING;3002 write_bitmap_index =0;3003continue;3004}3005if(starts_with(line,"--shallow ")) {3006struct object_id oid;3007if(get_oid_hex(line +10, &oid))3008die("not an SHA-1 '%s'", line +10);3009register_shallow(&oid);3010 use_bitmap_index =0;3011continue;3012}3013die("not a rev '%s'", line);3014}3015if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))3016die("bad revision '%s'", line);3017}30183019if(use_bitmap_index && !get_object_list_from_bitmap(&revs))3020return;30213022if(prepare_revision_walk(&revs))3023die("revision walk setup failed");3024mark_edges_uninteresting(&revs, show_edge);30253026if(!fn_show_object)3027 fn_show_object = show_object;3028traverse_commit_list_filtered(&filter_options, &revs,3029 show_commit, fn_show_object, NULL,3030 NULL);30313032if(unpack_unreachable_expiration) {3033 revs.ignore_missing_links =1;3034if(add_unseen_recent_objects_to_traversal(&revs,3035 unpack_unreachable_expiration))3036die("unable to add recent objects");3037if(prepare_revision_walk(&revs))3038die("revision walk setup failed");3039traverse_commit_list(&revs, record_recent_commit,3040 record_recent_object, NULL);3041}30423043if(keep_unreachable)3044add_objects_in_unpacked_packs(&revs);3045if(pack_loose_unreachable)3046add_unreachable_loose_objects();3047if(unpack_unreachable)3048loosen_unused_packed_objects(&revs);30493050oid_array_clear(&recent_objects);3051}30523053static voidadd_extra_kept_packs(const struct string_list *names)3054{3055struct packed_git *p;30563057if(!names->nr)3058return;30593060for(p =get_packed_git(the_repository); p; p = p->next) {3061const char*name =basename(p->pack_name);3062int i;30633064if(!p->pack_local)3065continue;30663067for(i =0; i < names->nr; i++)3068if(!fspathcmp(name, names->items[i].string))3069break;30703071if(i < names->nr) {3072 p->pack_keep_in_core =1;3073 ignore_packed_keep_in_core =1;3074continue;3075}3076}3077}30783079static intoption_parse_index_version(const struct option *opt,3080const char*arg,int unset)3081{3082char*c;3083const char*val = arg;3084 pack_idx_opts.version =strtoul(val, &c,10);3085if(pack_idx_opts.version >2)3086die(_("unsupported index version%s"), val);3087if(*c ==','&& c[1])3088 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3089if(*c || pack_idx_opts.off32_limit &0x80000000)3090die(_("bad index version '%s'"), val);3091return0;3092}30933094static intoption_parse_unpack_unreachable(const struct option *opt,3095const char*arg,int unset)3096{3097if(unset) {3098 unpack_unreachable =0;3099 unpack_unreachable_expiration =0;3100}3101else{3102 unpack_unreachable =1;3103if(arg)3104 unpack_unreachable_expiration =approxidate(arg);3105}3106return0;3107}31083109intcmd_pack_objects(int argc,const char**argv,const char*prefix)3110{3111int use_internal_rev_list =0;3112int thin =0;3113int shallow =0;3114int all_progress_implied =0;3115struct argv_array rp = ARGV_ARRAY_INIT;3116int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3117int rev_list_index =0;3118struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3119struct option pack_objects_options[] = {3120OPT_SET_INT('q',"quiet", &progress,3121N_("do not show progress meter"),0),3122OPT_SET_INT(0,"progress", &progress,3123N_("show progress meter"),1),3124OPT_SET_INT(0,"all-progress", &progress,3125N_("show progress meter during object writing phase"),2),3126OPT_BOOL(0,"all-progress-implied",3127&all_progress_implied,3128N_("similar to --all-progress when progress meter is shown")),3129{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),3130N_("write the pack index file in the specified idx format version"),31310, option_parse_index_version },3132OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3133N_("maximum size of each output pack file")),3134OPT_BOOL(0,"local", &local,3135N_("ignore borrowed objects from alternate object store")),3136OPT_BOOL(0,"incremental", &incremental,3137N_("ignore packed objects")),3138OPT_INTEGER(0,"window", &window,3139N_("limit pack window by objects")),3140OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3141N_("limit pack window by memory in addition to object limit")),3142OPT_INTEGER(0,"depth", &depth,3143N_("maximum length of delta chain allowed in the resulting pack")),3144OPT_BOOL(0,"reuse-delta", &reuse_delta,3145N_("reuse existing deltas")),3146OPT_BOOL(0,"reuse-object", &reuse_object,3147N_("reuse existing objects")),3148OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3149N_("use OFS_DELTA objects")),3150OPT_INTEGER(0,"threads", &delta_search_threads,3151N_("use threads when searching for best delta matches")),3152OPT_BOOL(0,"non-empty", &non_empty,3153N_("do not create an empty pack output")),3154OPT_BOOL(0,"revs", &use_internal_rev_list,3155N_("read revision arguments from standard input")),3156OPT_SET_INT_F(0,"unpacked", &rev_list_unpacked,3157N_("limit the objects to those that are not yet packed"),31581, PARSE_OPT_NONEG),3159OPT_SET_INT_F(0,"all", &rev_list_all,3160N_("include objects reachable from any reference"),31611, PARSE_OPT_NONEG),3162OPT_SET_INT_F(0,"reflog", &rev_list_reflog,3163N_("include objects referred by reflog entries"),31641, PARSE_OPT_NONEG),3165OPT_SET_INT_F(0,"indexed-objects", &rev_list_index,3166N_("include objects referred to by the index"),31671, PARSE_OPT_NONEG),3168OPT_BOOL(0,"stdout", &pack_to_stdout,3169N_("output pack to stdout")),3170OPT_BOOL(0,"include-tag", &include_tag,3171N_("include tag objects that refer to objects to be packed")),3172OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3173N_("keep unreachable objects")),3174OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3175N_("pack loose unreachable objects")),3176{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3177N_("unpack unreachable objects newer than <time>"),3178 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3179OPT_BOOL(0,"thin", &thin,3180N_("create thin packs")),3181OPT_BOOL(0,"shallow", &shallow,3182N_("create packs suitable for shallow fetches")),3183OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3184N_("ignore packs that have companion .keep file")),3185OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3186N_("ignore this pack")),3187OPT_INTEGER(0,"compression", &pack_compression_level,3188N_("pack compression level")),3189OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3190N_("do not hide commits by grafts"),0),3191OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3192N_("use a bitmap index if available to speed up counting objects")),3193OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3194N_("write a bitmap index together with the pack index")),3195OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3196{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3197N_("handling for missing objects"), PARSE_OPT_NONEG,3198 option_parse_missing_action },3199OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3200N_("do not pack objects in promisor packfiles")),3201OPT_END(),3202};32033204if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3205BUG("too many dfs states, increase OE_DFS_STATE_BITS");32063207 check_replace_refs =0;32083209reset_pack_idx_option(&pack_idx_opts);3210git_config(git_pack_config, NULL);32113212 progress =isatty(2);3213 argc =parse_options(argc, argv, prefix, pack_objects_options,3214 pack_usage,0);32153216if(argc) {3217 base_name = argv[0];3218 argc--;3219}3220if(pack_to_stdout != !base_name || argc)3221usage_with_options(pack_usage, pack_objects_options);32223223if(depth >= (1<< OE_DEPTH_BITS)) {3224warning(_("delta chain depth%dis too deep, forcing%d"),3225 depth, (1<< OE_DEPTH_BITS) -1);3226 depth = (1<< OE_DEPTH_BITS) -1;3227}3228if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3229warning(_("pack.deltaCacheLimit is too high, forcing%d"),3230(1U<< OE_Z_DELTA_BITS) -1);3231 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3232}32333234argv_array_push(&rp,"pack-objects");3235if(thin) {3236 use_internal_rev_list =1;3237argv_array_push(&rp, shallow3238?"--objects-edge-aggressive"3239:"--objects-edge");3240}else3241argv_array_push(&rp,"--objects");32423243if(rev_list_all) {3244 use_internal_rev_list =1;3245argv_array_push(&rp,"--all");3246}3247if(rev_list_reflog) {3248 use_internal_rev_list =1;3249argv_array_push(&rp,"--reflog");3250}3251if(rev_list_index) {3252 use_internal_rev_list =1;3253argv_array_push(&rp,"--indexed-objects");3254}3255if(rev_list_unpacked) {3256 use_internal_rev_list =1;3257argv_array_push(&rp,"--unpacked");3258}32593260if(exclude_promisor_objects) {3261 use_internal_rev_list =1;3262 fetch_if_missing =0;3263argv_array_push(&rp,"--exclude-promisor-objects");3264}3265if(unpack_unreachable || keep_unreachable || pack_loose_unreachable)3266 use_internal_rev_list =1;32673268if(!reuse_object)3269 reuse_delta =0;3270if(pack_compression_level == -1)3271 pack_compression_level = Z_DEFAULT_COMPRESSION;3272else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3273die("bad pack compression level%d", pack_compression_level);32743275if(!delta_search_threads)/* --threads=0 means autodetect */3276 delta_search_threads =online_cpus();32773278#ifdef NO_PTHREADS3279if(delta_search_threads !=1)3280warning("no threads support, ignoring --threads");3281#endif3282if(!pack_to_stdout && !pack_size_limit)3283 pack_size_limit = pack_size_limit_cfg;3284if(pack_to_stdout && pack_size_limit)3285die("--max-pack-size cannot be used to build a pack for transfer.");3286if(pack_size_limit && pack_size_limit <1024*1024) {3287warning("minimum pack size limit is 1 MiB");3288 pack_size_limit =1024*1024;3289}32903291if(!pack_to_stdout && thin)3292die("--thin cannot be used to build an indexable pack.");32933294if(keep_unreachable && unpack_unreachable)3295die("--keep-unreachable and --unpack-unreachable are incompatible.");3296if(!rev_list_all || !rev_list_reflog || !rev_list_index)3297 unpack_unreachable_expiration =0;32983299if(filter_options.choice) {3300if(!pack_to_stdout)3301die("cannot use --filter without --stdout.");3302 use_bitmap_index =0;3303}33043305/*3306 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3307 *3308 * - to produce good pack (with bitmap index not-yet-packed objects are3309 * packed in suboptimal order).3310 *3311 * - to use more robust pack-generation codepath (avoiding possible3312 * bugs in bitmap code and possible bitmap index corruption).3313 */3314if(!pack_to_stdout)3315 use_bitmap_index_default =0;33163317if(use_bitmap_index <0)3318 use_bitmap_index = use_bitmap_index_default;33193320/* "hard" reasons not to use bitmaps; these just won't work at all */3321if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3322 use_bitmap_index =0;33233324if(pack_to_stdout || !rev_list_all)3325 write_bitmap_index =0;33263327if(progress && all_progress_implied)3328 progress =2;33293330add_extra_kept_packs(&keep_pack_list);3331if(ignore_packed_keep_on_disk) {3332struct packed_git *p;3333for(p =get_packed_git(the_repository); p; p = p->next)3334if(p->pack_local && p->pack_keep)3335break;3336if(!p)/* no keep-able packs found */3337 ignore_packed_keep_on_disk =0;3338}3339if(local) {3340/*3341 * unlike ignore_packed_keep_on_disk above, we do not3342 * want to unset "local" based on looking at packs, as3343 * it also covers non-local objects3344 */3345struct packed_git *p;3346for(p =get_packed_git(the_repository); p; p = p->next) {3347if(!p->pack_local) {3348 have_non_local_packs =1;3349break;3350}3351}3352}33533354prepare_packing_data(&to_pack);33553356if(progress)3357 progress_state =start_progress(_("Enumerating objects"),0);3358if(!use_internal_rev_list)3359read_object_list_from_stdin();3360else{3361get_object_list(rp.argc, rp.argv);3362argv_array_clear(&rp);3363}3364cleanup_preferred_base();3365if(include_tag && nr_result)3366for_each_ref(add_ref_tag, NULL);3367stop_progress(&progress_state);33683369if(non_empty && !nr_result)3370return0;3371if(nr_result)3372prepare_pack(window, depth);3373write_pack_file();3374if(progress)3375fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3376" reused %"PRIu32" (delta %"PRIu32")\n",3377 written, written_delta, reused, reused_delta);3378return0;3379}