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; 282 283if(!usable_delta) { 284if(oe_type(entry) == OBJ_BLOB && 285oe_size_greater_than(&to_pack, entry, big_file_threshold) && 286(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 287 buf = NULL; 288else{ 289 buf =read_object_file(&entry->idx.oid, &type, &size); 290if(!buf) 291die(_("unable to read%s"), 292oid_to_hex(&entry->idx.oid)); 293} 294/* 295 * make sure no cached delta data remains from a 296 * previous attempt before a pack split occurred. 297 */ 298FREE_AND_NULL(entry->delta_data); 299 entry->z_delta_size =0; 300}else if(entry->delta_data) { 301 size =DELTA_SIZE(entry); 302 buf = entry->delta_data; 303 entry->delta_data = NULL; 304 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 305 OBJ_OFS_DELTA : OBJ_REF_DELTA; 306}else{ 307 buf =get_delta(entry); 308 size =DELTA_SIZE(entry); 309 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 310 OBJ_OFS_DELTA : OBJ_REF_DELTA; 311} 312 313if(st)/* large blob case, just assume we don't compress well */ 314 datalen = size; 315else if(entry->z_delta_size) 316 datalen = entry->z_delta_size; 317else 318 datalen =do_compress(&buf, size); 319 320/* 321 * The object header is a byte of 'type' followed by zero or 322 * more bytes of length. 323 */ 324 hdrlen =encode_in_pack_object_header(header,sizeof(header), 325 type, size); 326 327if(type == OBJ_OFS_DELTA) { 328/* 329 * Deltas with relative base contain an additional 330 * encoding of the relative offset for the delta 331 * base from this object's position in the pack. 332 */ 333 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 334unsigned pos =sizeof(dheader) -1; 335 dheader[pos] = ofs &127; 336while(ofs >>=7) 337 dheader[--pos] =128| (--ofs &127); 338if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 339if(st) 340close_istream(st); 341free(buf); 342return0; 343} 344hashwrite(f, header, hdrlen); 345hashwrite(f, dheader + pos,sizeof(dheader) - pos); 346 hdrlen +=sizeof(dheader) - pos; 347}else if(type == OBJ_REF_DELTA) { 348/* 349 * Deltas with a base reference contain 350 * an additional 20 bytes for the base sha1. 351 */ 352if(limit && hdrlen +20+ datalen +20>= limit) { 353if(st) 354close_istream(st); 355free(buf); 356return0; 357} 358hashwrite(f, header, hdrlen); 359hashwrite(f,DELTA(entry)->idx.oid.hash,20); 360 hdrlen +=20; 361}else{ 362if(limit && hdrlen + datalen +20>= limit) { 363if(st) 364close_istream(st); 365free(buf); 366return0; 367} 368hashwrite(f, header, hdrlen); 369} 370if(st) { 371 datalen =write_large_blob_data(st, f, &entry->idx.oid); 372close_istream(st); 373}else{ 374hashwrite(f, buf, datalen); 375free(buf); 376} 377 378return hdrlen + datalen; 379} 380 381/* Return 0 if we will bust the pack-size limit */ 382static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 383unsigned long limit,int usable_delta) 384{ 385struct packed_git *p =IN_PACK(entry); 386struct pack_window *w_curs = NULL; 387struct revindex_entry *revidx; 388 off_t offset; 389enum object_type type =oe_type(entry); 390 off_t datalen; 391unsigned char header[MAX_PACK_OBJECT_HEADER], 392 dheader[MAX_PACK_OBJECT_HEADER]; 393unsigned hdrlen; 394unsigned long entry_size =SIZE(entry); 395 396if(DELTA(entry)) 397 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 398 OBJ_OFS_DELTA : OBJ_REF_DELTA; 399 hdrlen =encode_in_pack_object_header(header,sizeof(header), 400 type, entry_size); 401 402 offset = entry->in_pack_offset; 403 revidx =find_pack_revindex(p, offset); 404 datalen = revidx[1].offset - offset; 405if(!pack_to_stdout && p->index_version >1&& 406check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 407error("bad packed object CRC for%s", 408oid_to_hex(&entry->idx.oid)); 409unuse_pack(&w_curs); 410returnwrite_no_reuse_object(f, entry, limit, usable_delta); 411} 412 413 offset += entry->in_pack_header_size; 414 datalen -= entry->in_pack_header_size; 415 416if(!pack_to_stdout && p->index_version ==1&& 417check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 418error("corrupt packed object for%s", 419oid_to_hex(&entry->idx.oid)); 420unuse_pack(&w_curs); 421returnwrite_no_reuse_object(f, entry, limit, usable_delta); 422} 423 424if(type == OBJ_OFS_DELTA) { 425 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 426unsigned pos =sizeof(dheader) -1; 427 dheader[pos] = ofs &127; 428while(ofs >>=7) 429 dheader[--pos] =128| (--ofs &127); 430if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 431unuse_pack(&w_curs); 432return0; 433} 434hashwrite(f, header, hdrlen); 435hashwrite(f, dheader + pos,sizeof(dheader) - pos); 436 hdrlen +=sizeof(dheader) - pos; 437 reused_delta++; 438}else if(type == OBJ_REF_DELTA) { 439if(limit && hdrlen +20+ datalen +20>= limit) { 440unuse_pack(&w_curs); 441return0; 442} 443hashwrite(f, header, hdrlen); 444hashwrite(f,DELTA(entry)->idx.oid.hash,20); 445 hdrlen +=20; 446 reused_delta++; 447}else{ 448if(limit && hdrlen + datalen +20>= limit) { 449unuse_pack(&w_curs); 450return0; 451} 452hashwrite(f, header, hdrlen); 453} 454copy_pack_data(f, p, &w_curs, offset, datalen); 455unuse_pack(&w_curs); 456 reused++; 457return hdrlen + datalen; 458} 459 460/* Return 0 if we will bust the pack-size limit */ 461static off_t write_object(struct hashfile *f, 462struct object_entry *entry, 463 off_t write_offset) 464{ 465unsigned long limit; 466 off_t len; 467int usable_delta, to_reuse; 468 469if(!pack_to_stdout) 470crc32_begin(f); 471 472/* apply size limit if limited packsize and not first object */ 473if(!pack_size_limit || !nr_written) 474 limit =0; 475else if(pack_size_limit <= write_offset) 476/* 477 * the earlier object did not fit the limit; avoid 478 * mistaking this with unlimited (i.e. limit = 0). 479 */ 480 limit =1; 481else 482 limit = pack_size_limit - write_offset; 483 484if(!DELTA(entry)) 485 usable_delta =0;/* no delta */ 486else if(!pack_size_limit) 487 usable_delta =1;/* unlimited packfile */ 488else if(DELTA(entry)->idx.offset == (off_t)-1) 489 usable_delta =0;/* base was written to another pack */ 490else if(DELTA(entry)->idx.offset) 491 usable_delta =1;/* base already exists in this pack */ 492else 493 usable_delta =0;/* base could end up in another pack */ 494 495if(!reuse_object) 496 to_reuse =0;/* explicit */ 497else if(!IN_PACK(entry)) 498 to_reuse =0;/* can't reuse what we don't have */ 499else if(oe_type(entry) == OBJ_REF_DELTA || 500oe_type(entry) == OBJ_OFS_DELTA) 501/* check_object() decided it for us ... */ 502 to_reuse = usable_delta; 503/* ... but pack split may override that */ 504else if(oe_type(entry) != entry->in_pack_type) 505 to_reuse =0;/* pack has delta which is unusable */ 506else if(DELTA(entry)) 507 to_reuse =0;/* we want to pack afresh */ 508else 509 to_reuse =1;/* we have it in-pack undeltified, 510 * and we do not need to deltify it. 511 */ 512 513if(!to_reuse) 514 len =write_no_reuse_object(f, entry, limit, usable_delta); 515else 516 len =write_reuse_object(f, entry, limit, usable_delta); 517if(!len) 518return0; 519 520if(usable_delta) 521 written_delta++; 522 written++; 523if(!pack_to_stdout) 524 entry->idx.crc32 =crc32_end(f); 525return len; 526} 527 528enum write_one_status { 529 WRITE_ONE_SKIP = -1,/* already written */ 530 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 531 WRITE_ONE_WRITTEN =1,/* normal */ 532 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 533}; 534 535static enum write_one_status write_one(struct hashfile *f, 536struct object_entry *e, 537 off_t *offset) 538{ 539 off_t size; 540int recursing; 541 542/* 543 * we set offset to 1 (which is an impossible value) to mark 544 * the fact that this object is involved in "write its base 545 * first before writing a deltified object" recursion. 546 */ 547 recursing = (e->idx.offset ==1); 548if(recursing) { 549warning("recursive delta detected for object%s", 550oid_to_hex(&e->idx.oid)); 551return WRITE_ONE_RECURSIVE; 552}else if(e->idx.offset || e->preferred_base) { 553/* offset is non zero if object is written already. */ 554return WRITE_ONE_SKIP; 555} 556 557/* if we are deltified, write out base object first. */ 558if(DELTA(e)) { 559 e->idx.offset =1;/* now recurse */ 560switch(write_one(f,DELTA(e), offset)) { 561case WRITE_ONE_RECURSIVE: 562/* we cannot depend on this one */ 563SET_DELTA(e, NULL); 564break; 565default: 566break; 567case WRITE_ONE_BREAK: 568 e->idx.offset = recursing; 569return WRITE_ONE_BREAK; 570} 571} 572 573 e->idx.offset = *offset; 574 size =write_object(f, e, *offset); 575if(!size) { 576 e->idx.offset = recursing; 577return WRITE_ONE_BREAK; 578} 579 written_list[nr_written++] = &e->idx; 580 581/* make sure off_t is sufficiently large not to wrap */ 582if(signed_add_overflows(*offset, size)) 583die("pack too large for current definition of off_t"); 584*offset += size; 585return WRITE_ONE_WRITTEN; 586} 587 588static intmark_tagged(const char*path,const struct object_id *oid,int flag, 589void*cb_data) 590{ 591struct object_id peeled; 592struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 593 594if(entry) 595 entry->tagged =1; 596if(!peel_ref(path, &peeled)) { 597 entry =packlist_find(&to_pack, peeled.hash, NULL); 598if(entry) 599 entry->tagged =1; 600} 601return0; 602} 603 604staticinlinevoidadd_to_write_order(struct object_entry **wo, 605unsigned int*endp, 606struct object_entry *e) 607{ 608if(e->filled) 609return; 610 wo[(*endp)++] = e; 611 e->filled =1; 612} 613 614static voidadd_descendants_to_write_order(struct object_entry **wo, 615unsigned int*endp, 616struct object_entry *e) 617{ 618int add_to_order =1; 619while(e) { 620if(add_to_order) { 621struct object_entry *s; 622/* add this node... */ 623add_to_write_order(wo, endp, e); 624/* all its siblings... */ 625for(s =DELTA_SIBLING(e); s; s =DELTA_SIBLING(s)) { 626add_to_write_order(wo, endp, s); 627} 628} 629/* drop down a level to add left subtree nodes if possible */ 630if(DELTA_CHILD(e)) { 631 add_to_order =1; 632 e =DELTA_CHILD(e); 633}else{ 634 add_to_order =0; 635/* our sibling might have some children, it is next */ 636if(DELTA_SIBLING(e)) { 637 e =DELTA_SIBLING(e); 638continue; 639} 640/* go back to our parent node */ 641 e =DELTA(e); 642while(e && !DELTA_SIBLING(e)) { 643/* we're on the right side of a subtree, keep 644 * going up until we can go right again */ 645 e =DELTA(e); 646} 647if(!e) { 648/* done- we hit our original root node */ 649return; 650} 651/* pass it off to sibling at this level */ 652 e =DELTA_SIBLING(e); 653} 654}; 655} 656 657static voidadd_family_to_write_order(struct object_entry **wo, 658unsigned int*endp, 659struct object_entry *e) 660{ 661struct object_entry *root; 662 663for(root = e;DELTA(root); root =DELTA(root)) 664;/* nothing */ 665add_descendants_to_write_order(wo, endp, root); 666} 667 668static struct object_entry **compute_write_order(void) 669{ 670unsigned int i, wo_end, last_untagged; 671 672struct object_entry **wo; 673struct object_entry *objects = to_pack.objects; 674 675for(i =0; i < to_pack.nr_objects; i++) { 676 objects[i].tagged =0; 677 objects[i].filled =0; 678SET_DELTA_CHILD(&objects[i], NULL); 679SET_DELTA_SIBLING(&objects[i], NULL); 680} 681 682/* 683 * Fully connect delta_child/delta_sibling network. 684 * Make sure delta_sibling is sorted in the original 685 * recency order. 686 */ 687for(i = to_pack.nr_objects; i >0;) { 688struct object_entry *e = &objects[--i]; 689if(!DELTA(e)) 690continue; 691/* Mark me as the first child */ 692 e->delta_sibling_idx =DELTA(e)->delta_child_idx; 693SET_DELTA_CHILD(DELTA(e), e); 694} 695 696/* 697 * Mark objects that are at the tip of tags. 698 */ 699for_each_tag_ref(mark_tagged, NULL); 700 701/* 702 * Give the objects in the original recency order until 703 * we see a tagged tip. 704 */ 705ALLOC_ARRAY(wo, to_pack.nr_objects); 706for(i = wo_end =0; i < to_pack.nr_objects; i++) { 707if(objects[i].tagged) 708break; 709add_to_write_order(wo, &wo_end, &objects[i]); 710} 711 last_untagged = i; 712 713/* 714 * Then fill all the tagged tips. 715 */ 716for(; i < to_pack.nr_objects; i++) { 717if(objects[i].tagged) 718add_to_write_order(wo, &wo_end, &objects[i]); 719} 720 721/* 722 * And then all remaining commits and tags. 723 */ 724for(i = last_untagged; i < to_pack.nr_objects; i++) { 725if(oe_type(&objects[i]) != OBJ_COMMIT && 726oe_type(&objects[i]) != OBJ_TAG) 727continue; 728add_to_write_order(wo, &wo_end, &objects[i]); 729} 730 731/* 732 * And then all the trees. 733 */ 734for(i = last_untagged; i < to_pack.nr_objects; i++) { 735if(oe_type(&objects[i]) != OBJ_TREE) 736continue; 737add_to_write_order(wo, &wo_end, &objects[i]); 738} 739 740/* 741 * Finally all the rest in really tight order 742 */ 743for(i = last_untagged; i < to_pack.nr_objects; i++) { 744if(!objects[i].filled) 745add_family_to_write_order(wo, &wo_end, &objects[i]); 746} 747 748if(wo_end != to_pack.nr_objects) 749die("ordered%uobjects, expected %"PRIu32, wo_end, to_pack.nr_objects); 750 751return wo; 752} 753 754static off_t write_reused_pack(struct hashfile *f) 755{ 756unsigned char buffer[8192]; 757 off_t to_write, total; 758int fd; 759 760if(!is_pack_valid(reuse_packfile)) 761die("packfile is invalid:%s", reuse_packfile->pack_name); 762 763 fd =git_open(reuse_packfile->pack_name); 764if(fd <0) 765die_errno("unable to open packfile for reuse:%s", 766 reuse_packfile->pack_name); 767 768if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 769die_errno("unable to seek in reused packfile"); 770 771if(reuse_packfile_offset <0) 772 reuse_packfile_offset = reuse_packfile->pack_size -20; 773 774 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 775 776while(to_write) { 777int read_pack =xread(fd, buffer,sizeof(buffer)); 778 779if(read_pack <=0) 780die_errno("unable to read from reused packfile"); 781 782if(read_pack > to_write) 783 read_pack = to_write; 784 785hashwrite(f, buffer, read_pack); 786 to_write -= read_pack; 787 788/* 789 * We don't know the actual number of objects written, 790 * only how many bytes written, how many bytes total, and 791 * how many objects total. So we can fake it by pretending all 792 * objects we are writing are the same size. This gives us a 793 * smooth progress meter, and at the end it matches the true 794 * answer. 795 */ 796 written = reuse_packfile_objects * 797(((double)(total - to_write)) / total); 798display_progress(progress_state, written); 799} 800 801close(fd); 802 written = reuse_packfile_objects; 803display_progress(progress_state, written); 804return reuse_packfile_offset -sizeof(struct pack_header); 805} 806 807static const char no_split_warning[] =N_( 808"disabling bitmap writing, packs are split due to pack.packSizeLimit" 809); 810 811static voidwrite_pack_file(void) 812{ 813uint32_t i =0, j; 814struct hashfile *f; 815 off_t offset; 816uint32_t nr_remaining = nr_result; 817time_t last_mtime =0; 818struct object_entry **write_order; 819 820if(progress > pack_to_stdout) 821 progress_state =start_progress(_("Writing objects"), nr_result); 822ALLOC_ARRAY(written_list, to_pack.nr_objects); 823 write_order =compute_write_order(); 824 825do{ 826struct object_id oid; 827char*pack_tmp_name = NULL; 828 829if(pack_to_stdout) 830 f =hashfd_throughput(1,"<stdout>", progress_state); 831else 832 f =create_tmp_packfile(&pack_tmp_name); 833 834 offset =write_pack_header(f, nr_remaining); 835 836if(reuse_packfile) { 837 off_t packfile_size; 838assert(pack_to_stdout); 839 840 packfile_size =write_reused_pack(f); 841 offset += packfile_size; 842} 843 844 nr_written =0; 845for(; i < to_pack.nr_objects; i++) { 846struct object_entry *e = write_order[i]; 847if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 848break; 849display_progress(progress_state, written); 850} 851 852/* 853 * Did we write the wrong # entries in the header? 854 * If so, rewrite it like in fast-import 855 */ 856if(pack_to_stdout) { 857finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 858}else if(nr_written == nr_remaining) { 859finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 860}else{ 861int fd =finalize_hashfile(f, oid.hash,0); 862fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 863 nr_written, oid.hash, offset); 864close(fd); 865if(write_bitmap_index) { 866warning(_(no_split_warning)); 867 write_bitmap_index =0; 868} 869} 870 871if(!pack_to_stdout) { 872struct stat st; 873struct strbuf tmpname = STRBUF_INIT; 874 875/* 876 * Packs are runtime accessed in their mtime 877 * order since newer packs are more likely to contain 878 * younger objects. So if we are creating multiple 879 * packs then we should modify the mtime of later ones 880 * to preserve this property. 881 */ 882if(stat(pack_tmp_name, &st) <0) { 883warning_errno("failed to stat%s", pack_tmp_name); 884}else if(!last_mtime) { 885 last_mtime = st.st_mtime; 886}else{ 887struct utimbuf utb; 888 utb.actime = st.st_atime; 889 utb.modtime = --last_mtime; 890if(utime(pack_tmp_name, &utb) <0) 891warning_errno("failed utime() on%s", pack_tmp_name); 892} 893 894strbuf_addf(&tmpname,"%s-", base_name); 895 896if(write_bitmap_index) { 897bitmap_writer_set_checksum(oid.hash); 898bitmap_writer_build_type_index( 899&to_pack, written_list, nr_written); 900} 901 902finish_tmp_packfile(&tmpname, pack_tmp_name, 903 written_list, nr_written, 904&pack_idx_opts, oid.hash); 905 906if(write_bitmap_index) { 907strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 908 909stop_progress(&progress_state); 910 911bitmap_writer_show_progress(progress); 912bitmap_writer_reuse_bitmaps(&to_pack); 913bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 914bitmap_writer_build(&to_pack); 915bitmap_writer_finish(written_list, nr_written, 916 tmpname.buf, write_bitmap_options); 917 write_bitmap_index =0; 918} 919 920strbuf_release(&tmpname); 921free(pack_tmp_name); 922puts(oid_to_hex(&oid)); 923} 924 925/* mark written objects as written to previous pack */ 926for(j =0; j < nr_written; j++) { 927 written_list[j]->offset = (off_t)-1; 928} 929 nr_remaining -= nr_written; 930}while(nr_remaining && i < to_pack.nr_objects); 931 932free(written_list); 933free(write_order); 934stop_progress(&progress_state); 935if(written != nr_result) 936die("wrote %"PRIu32" objects while expecting %"PRIu32, 937 written, nr_result); 938} 939 940static intno_try_delta(const char*path) 941{ 942static struct attr_check *check; 943 944if(!check) 945 check =attr_check_initl("delta", NULL); 946if(git_check_attr(path, check)) 947return0; 948if(ATTR_FALSE(check->items[0].value)) 949return1; 950return0; 951} 952 953/* 954 * When adding an object, check whether we have already added it 955 * to our packing list. If so, we can skip. However, if we are 956 * being asked to excludei t, but the previous mention was to include 957 * it, make sure to adjust its flags and tweak our numbers accordingly. 958 * 959 * As an optimization, we pass out the index position where we would have 960 * found the item, since that saves us from having to look it up again a 961 * few lines later when we want to add the new entry. 962 */ 963static inthave_duplicate_entry(const struct object_id *oid, 964int exclude, 965uint32_t*index_pos) 966{ 967struct object_entry *entry; 968 969 entry =packlist_find(&to_pack, oid->hash, index_pos); 970if(!entry) 971return0; 972 973if(exclude) { 974if(!entry->preferred_base) 975 nr_result--; 976 entry->preferred_base =1; 977} 978 979return1; 980} 981 982static intwant_found_object(int exclude,struct packed_git *p) 983{ 984if(exclude) 985return1; 986if(incremental) 987return0; 988 989/* 990 * When asked to do --local (do not include an object that appears in a 991 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 992 * an object that appears in a pack marked with .keep), finding a pack 993 * that matches the criteria is sufficient for us to decide to omit it. 994 * However, even if this pack does not satisfy the criteria, we need to 995 * make sure no copy of this object appears in _any_ pack that makes us 996 * to omit the object, so we need to check all the packs. 997 * 998 * We can however first check whether these options can possible matter; 999 * if they do not matter we know we want the object in generated pack.1000 * Otherwise, we signal "-1" at the end to tell the caller that we do1001 * not know either way, and it needs to check more packs.1002 */1003if(!ignore_packed_keep_on_disk &&1004!ignore_packed_keep_in_core &&1005(!local || !have_non_local_packs))1006return1;10071008if(local && !p->pack_local)1009return0;1010if(p->pack_local &&1011((ignore_packed_keep_on_disk && p->pack_keep) ||1012(ignore_packed_keep_in_core && p->pack_keep_in_core)))1013return0;10141015/* we don't know yet; keep looking for more packs */1016return-1;1017}10181019/*1020 * Check whether we want the object in the pack (e.g., we do not want1021 * objects found in non-local stores if the "--local" option was used).1022 *1023 * If the caller already knows an existing pack it wants to take the object1024 * from, that is passed in *found_pack and *found_offset; otherwise this1025 * function finds if there is any pack that has the object and returns the pack1026 * and its offset in these variables.1027 */1028static intwant_object_in_pack(const struct object_id *oid,1029int exclude,1030struct packed_git **found_pack,1031 off_t *found_offset)1032{1033int want;1034struct list_head *pos;10351036if(!exclude && local &&has_loose_object_nonlocal(oid->hash))1037return0;10381039/*1040 * If we already know the pack object lives in, start checks from that1041 * pack - in the usual case when neither --local was given nor .keep files1042 * are present we will determine the answer right now.1043 */1044if(*found_pack) {1045 want =want_found_object(exclude, *found_pack);1046if(want != -1)1047return want;1048}1049list_for_each(pos,get_packed_git_mru(the_repository)) {1050struct packed_git *p =list_entry(pos,struct packed_git, mru);1051 off_t offset;10521053if(p == *found_pack)1054 offset = *found_offset;1055else1056 offset =find_pack_entry_one(oid->hash, p);10571058if(offset) {1059if(!*found_pack) {1060if(!is_pack_valid(p))1061continue;1062*found_offset = offset;1063*found_pack = p;1064}1065 want =want_found_object(exclude, p);1066if(!exclude && want >0)1067list_move(&p->mru,1068get_packed_git_mru(the_repository));1069if(want != -1)1070return want;1071}1072}10731074return1;1075}10761077static voidcreate_object_entry(const struct object_id *oid,1078enum object_type type,1079uint32_t hash,1080int exclude,1081int no_try_delta,1082uint32_t index_pos,1083struct packed_git *found_pack,1084 off_t found_offset)1085{1086struct object_entry *entry;10871088 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1089 entry->hash = hash;1090oe_set_type(entry, type);1091if(exclude)1092 entry->preferred_base =1;1093else1094 nr_result++;1095if(found_pack) {1096oe_set_in_pack(&to_pack, entry, found_pack);1097 entry->in_pack_offset = found_offset;1098}10991100 entry->no_try_delta = no_try_delta;1101}11021103static const char no_closure_warning[] =N_(1104"disabling bitmap writing, as some objects are not being packed"1105);11061107static intadd_object_entry(const struct object_id *oid,enum object_type type,1108const char*name,int exclude)1109{1110struct packed_git *found_pack = NULL;1111 off_t found_offset =0;1112uint32_t index_pos;11131114display_progress(progress_state, ++nr_seen);11151116if(have_duplicate_entry(oid, exclude, &index_pos))1117return0;11181119if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1120/* The pack is missing an object, so it will not have closure */1121if(write_bitmap_index) {1122warning(_(no_closure_warning));1123 write_bitmap_index =0;1124}1125return0;1126}11271128create_object_entry(oid, type,pack_name_hash(name),1129 exclude, name &&no_try_delta(name),1130 index_pos, found_pack, found_offset);1131return1;1132}11331134static intadd_object_entry_from_bitmap(const struct object_id *oid,1135enum object_type type,1136int flags,uint32_t name_hash,1137struct packed_git *pack, off_t offset)1138{1139uint32_t index_pos;11401141display_progress(progress_state, ++nr_seen);11421143if(have_duplicate_entry(oid,0, &index_pos))1144return0;11451146if(!want_object_in_pack(oid,0, &pack, &offset))1147return0;11481149create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1150return1;1151}11521153struct pbase_tree_cache {1154struct object_id oid;1155int ref;1156int temporary;1157void*tree_data;1158unsigned long tree_size;1159};11601161static struct pbase_tree_cache *(pbase_tree_cache[256]);1162static intpbase_tree_cache_ix(const struct object_id *oid)1163{1164return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1165}1166static intpbase_tree_cache_ix_incr(int ix)1167{1168return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1169}11701171static struct pbase_tree {1172struct pbase_tree *next;1173/* This is a phony "cache" entry; we are not1174 * going to evict it or find it through _get()1175 * mechanism -- this is for the toplevel node that1176 * would almost always change with any commit.1177 */1178struct pbase_tree_cache pcache;1179} *pbase_tree;11801181static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1182{1183struct pbase_tree_cache *ent, *nent;1184void*data;1185unsigned long size;1186enum object_type type;1187int neigh;1188int my_ix =pbase_tree_cache_ix(oid);1189int available_ix = -1;11901191/* pbase-tree-cache acts as a limited hashtable.1192 * your object will be found at your index or within a few1193 * slots after that slot if it is cached.1194 */1195for(neigh =0; neigh <8; neigh++) {1196 ent = pbase_tree_cache[my_ix];1197if(ent && !oidcmp(&ent->oid, oid)) {1198 ent->ref++;1199return ent;1200}1201else if(((available_ix <0) && (!ent || !ent->ref)) ||1202((0<= available_ix) &&1203(!ent && pbase_tree_cache[available_ix])))1204 available_ix = my_ix;1205if(!ent)1206break;1207 my_ix =pbase_tree_cache_ix_incr(my_ix);1208}12091210/* Did not find one. Either we got a bogus request or1211 * we need to read and perhaps cache.1212 */1213 data =read_object_file(oid, &type, &size);1214if(!data)1215return NULL;1216if(type != OBJ_TREE) {1217free(data);1218return NULL;1219}12201221/* We need to either cache or return a throwaway copy */12221223if(available_ix <0)1224 ent = NULL;1225else{1226 ent = pbase_tree_cache[available_ix];1227 my_ix = available_ix;1228}12291230if(!ent) {1231 nent =xmalloc(sizeof(*nent));1232 nent->temporary = (available_ix <0);1233}1234else{1235/* evict and reuse */1236free(ent->tree_data);1237 nent = ent;1238}1239oidcpy(&nent->oid, oid);1240 nent->tree_data = data;1241 nent->tree_size = size;1242 nent->ref =1;1243if(!nent->temporary)1244 pbase_tree_cache[my_ix] = nent;1245return nent;1246}12471248static voidpbase_tree_put(struct pbase_tree_cache *cache)1249{1250if(!cache->temporary) {1251 cache->ref--;1252return;1253}1254free(cache->tree_data);1255free(cache);1256}12571258static intname_cmp_len(const char*name)1259{1260int i;1261for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1262;1263return i;1264}12651266static voidadd_pbase_object(struct tree_desc *tree,1267const char*name,1268int cmplen,1269const char*fullname)1270{1271struct name_entry entry;1272int cmp;12731274while(tree_entry(tree,&entry)) {1275if(S_ISGITLINK(entry.mode))1276continue;1277 cmp =tree_entry_len(&entry) != cmplen ?1:1278memcmp(name, entry.path, cmplen);1279if(cmp >0)1280continue;1281if(cmp <0)1282return;1283if(name[cmplen] !='/') {1284add_object_entry(entry.oid,1285object_type(entry.mode),1286 fullname,1);1287return;1288}1289if(S_ISDIR(entry.mode)) {1290struct tree_desc sub;1291struct pbase_tree_cache *tree;1292const char*down = name+cmplen+1;1293int downlen =name_cmp_len(down);12941295 tree =pbase_tree_get(entry.oid);1296if(!tree)1297return;1298init_tree_desc(&sub, tree->tree_data, tree->tree_size);12991300add_pbase_object(&sub, down, downlen, fullname);1301pbase_tree_put(tree);1302}1303}1304}13051306static unsigned*done_pbase_paths;1307static int done_pbase_paths_num;1308static int done_pbase_paths_alloc;1309static intdone_pbase_path_pos(unsigned hash)1310{1311int lo =0;1312int hi = done_pbase_paths_num;1313while(lo < hi) {1314int mi = lo + (hi - lo) /2;1315if(done_pbase_paths[mi] == hash)1316return mi;1317if(done_pbase_paths[mi] < hash)1318 hi = mi;1319else1320 lo = mi +1;1321}1322return-lo-1;1323}13241325static intcheck_pbase_path(unsigned hash)1326{1327int pos =done_pbase_path_pos(hash);1328if(0<= pos)1329return1;1330 pos = -pos -1;1331ALLOC_GROW(done_pbase_paths,1332 done_pbase_paths_num +1,1333 done_pbase_paths_alloc);1334 done_pbase_paths_num++;1335if(pos < done_pbase_paths_num)1336MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1337 done_pbase_paths_num - pos -1);1338 done_pbase_paths[pos] = hash;1339return0;1340}13411342static voidadd_preferred_base_object(const char*name)1343{1344struct pbase_tree *it;1345int cmplen;1346unsigned hash =pack_name_hash(name);13471348if(!num_preferred_base ||check_pbase_path(hash))1349return;13501351 cmplen =name_cmp_len(name);1352for(it = pbase_tree; it; it = it->next) {1353if(cmplen ==0) {1354add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1355}1356else{1357struct tree_desc tree;1358init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1359add_pbase_object(&tree, name, cmplen, name);1360}1361}1362}13631364static voidadd_preferred_base(struct object_id *oid)1365{1366struct pbase_tree *it;1367void*data;1368unsigned long size;1369struct object_id tree_oid;13701371if(window <= num_preferred_base++)1372return;13731374 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1375if(!data)1376return;13771378for(it = pbase_tree; it; it = it->next) {1379if(!oidcmp(&it->pcache.oid, &tree_oid)) {1380free(data);1381return;1382}1383}13841385 it =xcalloc(1,sizeof(*it));1386 it->next = pbase_tree;1387 pbase_tree = it;13881389oidcpy(&it->pcache.oid, &tree_oid);1390 it->pcache.tree_data = data;1391 it->pcache.tree_size = size;1392}13931394static voidcleanup_preferred_base(void)1395{1396struct pbase_tree *it;1397unsigned i;13981399 it = pbase_tree;1400 pbase_tree = NULL;1401while(it) {1402struct pbase_tree *tmp = it;1403 it = tmp->next;1404free(tmp->pcache.tree_data);1405free(tmp);1406}14071408for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1409if(!pbase_tree_cache[i])1410continue;1411free(pbase_tree_cache[i]->tree_data);1412FREE_AND_NULL(pbase_tree_cache[i]);1413}14141415FREE_AND_NULL(done_pbase_paths);1416 done_pbase_paths_num = done_pbase_paths_alloc =0;1417}14181419static voidcheck_object(struct object_entry *entry)1420{1421unsigned long canonical_size;14221423if(IN_PACK(entry)) {1424struct packed_git *p =IN_PACK(entry);1425struct pack_window *w_curs = NULL;1426const unsigned char*base_ref = NULL;1427struct object_entry *base_entry;1428unsigned long used, used_0;1429unsigned long avail;1430 off_t ofs;1431unsigned char*buf, c;1432enum object_type type;1433unsigned long in_pack_size;14341435 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14361437/*1438 * We want in_pack_type even if we do not reuse delta1439 * since non-delta representations could still be reused.1440 */1441 used =unpack_object_header_buffer(buf, avail,1442&type,1443&in_pack_size);1444if(used ==0)1445goto give_up;14461447if(type <0)1448BUG("invalid type%d", type);1449 entry->in_pack_type = type;14501451/*1452 * Determine if this is a delta and if so whether we can1453 * reuse it or not. Otherwise let's find out as cheaply as1454 * possible what the actual type and size for this object is.1455 */1456switch(entry->in_pack_type) {1457default:1458/* Not a delta hence we've already got all we need. */1459oe_set_type(entry, entry->in_pack_type);1460SET_SIZE(entry, in_pack_size);1461 entry->in_pack_header_size = used;1462if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1463goto give_up;1464unuse_pack(&w_curs);1465return;1466case OBJ_REF_DELTA:1467if(reuse_delta && !entry->preferred_base)1468 base_ref =use_pack(p, &w_curs,1469 entry->in_pack_offset + used, NULL);1470 entry->in_pack_header_size = used +20;1471break;1472case OBJ_OFS_DELTA:1473 buf =use_pack(p, &w_curs,1474 entry->in_pack_offset + used, NULL);1475 used_0 =0;1476 c = buf[used_0++];1477 ofs = c &127;1478while(c &128) {1479 ofs +=1;1480if(!ofs ||MSB(ofs,7)) {1481error("delta base offset overflow in pack for%s",1482oid_to_hex(&entry->idx.oid));1483goto give_up;1484}1485 c = buf[used_0++];1486 ofs = (ofs <<7) + (c &127);1487}1488 ofs = entry->in_pack_offset - ofs;1489if(ofs <=0|| ofs >= entry->in_pack_offset) {1490error("delta base offset out of bound for%s",1491oid_to_hex(&entry->idx.oid));1492goto give_up;1493}1494if(reuse_delta && !entry->preferred_base) {1495struct revindex_entry *revidx;1496 revidx =find_pack_revindex(p, ofs);1497if(!revidx)1498goto give_up;1499 base_ref =nth_packed_object_sha1(p, revidx->nr);1500}1501 entry->in_pack_header_size = used + used_0;1502break;1503}15041505if(base_ref && (base_entry =packlist_find(&to_pack, base_ref, NULL))) {1506/*1507 * If base_ref was set above that means we wish to1508 * reuse delta data, and we even found that base1509 * in the list of objects we want to pack. Goodie!1510 *1511 * Depth value does not matter - find_deltas() will1512 * never consider reused delta as the base object to1513 * deltify other objects against, in order to avoid1514 * circular deltas.1515 */1516oe_set_type(entry, entry->in_pack_type);1517SET_SIZE(entry, in_pack_size);/* delta size */1518SET_DELTA(entry, base_entry);1519SET_DELTA_SIZE(entry, in_pack_size);1520 entry->delta_sibling_idx = base_entry->delta_child_idx;1521SET_DELTA_CHILD(base_entry, entry);1522unuse_pack(&w_curs);1523return;1524}15251526if(oe_type(entry)) {1527 off_t delta_pos;15281529/*1530 * This must be a delta and we already know what the1531 * final object type is. Let's extract the actual1532 * object size from the delta header.1533 */1534 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1535 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1536if(canonical_size ==0)1537goto give_up;1538SET_SIZE(entry, canonical_size);1539unuse_pack(&w_curs);1540return;1541}15421543/*1544 * No choice but to fall back to the recursive delta walk1545 * with sha1_object_info() to find about the object type1546 * at this point...1547 */1548 give_up:1549unuse_pack(&w_curs);1550}15511552oe_set_type(entry,1553oid_object_info(the_repository, &entry->idx.oid, &canonical_size));1554if(entry->type_valid) {1555SET_SIZE(entry, canonical_size);1556}else{1557/*1558 * Bad object type is checked in prepare_pack(). This is1559 * to permit a missing preferred base object to be ignored1560 * as a preferred base. Doing so can result in a larger1561 * pack file, but the transfer will still take place.1562 */1563}1564}15651566static intpack_offset_sort(const void*_a,const void*_b)1567{1568const struct object_entry *a = *(struct object_entry **)_a;1569const struct object_entry *b = *(struct object_entry **)_b;1570const struct packed_git *a_in_pack =IN_PACK(a);1571const struct packed_git *b_in_pack =IN_PACK(b);15721573/* avoid filesystem trashing with loose objects */1574if(!a_in_pack && !b_in_pack)1575returnoidcmp(&a->idx.oid, &b->idx.oid);15761577if(a_in_pack < b_in_pack)1578return-1;1579if(a_in_pack > b_in_pack)1580return1;1581return a->in_pack_offset < b->in_pack_offset ? -1:1582(a->in_pack_offset > b->in_pack_offset);1583}15841585/*1586 * Drop an on-disk delta we were planning to reuse. Naively, this would1587 * just involve blanking out the "delta" field, but we have to deal1588 * with some extra book-keeping:1589 *1590 * 1. Removing ourselves from the delta_sibling linked list.1591 *1592 * 2. Updating our size/type to the non-delta representation. These were1593 * either not recorded initially (size) or overwritten with the delta type1594 * (type) when check_object() decided to reuse the delta.1595 *1596 * 3. Resetting our delta depth, as we are now a base object.1597 */1598static voiddrop_reused_delta(struct object_entry *entry)1599{1600unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1601struct object_info oi = OBJECT_INFO_INIT;1602enum object_type type;1603unsigned long size;16041605while(*idx) {1606struct object_entry *oe = &to_pack.objects[*idx -1];16071608if(oe == entry)1609*idx = oe->delta_sibling_idx;1610else1611 idx = &oe->delta_sibling_idx;1612}1613SET_DELTA(entry, NULL);1614 entry->depth =0;16151616 oi.sizep = &size;1617 oi.typep = &type;1618if(packed_object_info(the_repository,IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1619/*1620 * We failed to get the info from this pack for some reason;1621 * fall back to sha1_object_info, which may find another copy.1622 * And if that fails, the error will be recorded in oe_type(entry)1623 * and dealt with in prepare_pack().1624 */1625oe_set_type(entry,1626oid_object_info(the_repository, &entry->idx.oid, &size));1627}else{1628oe_set_type(entry, type);1629}1630SET_SIZE(entry, size);1631}16321633/*1634 * Follow the chain of deltas from this entry onward, throwing away any links1635 * that cause us to hit a cycle (as determined by the DFS state flags in1636 * the entries).1637 *1638 * We also detect too-long reused chains that would violate our --depth1639 * limit.1640 */1641static voidbreak_delta_chains(struct object_entry *entry)1642{1643/*1644 * The actual depth of each object we will write is stored as an int,1645 * as it cannot exceed our int "depth" limit. But before we break1646 * changes based no that limit, we may potentially go as deep as the1647 * number of objects, which is elsewhere bounded to a uint32_t.1648 */1649uint32_t total_depth;1650struct object_entry *cur, *next;16511652for(cur = entry, total_depth =0;1653 cur;1654 cur =DELTA(cur), total_depth++) {1655if(cur->dfs_state == DFS_DONE) {1656/*1657 * We've already seen this object and know it isn't1658 * part of a cycle. We do need to append its depth1659 * to our count.1660 */1661 total_depth += cur->depth;1662break;1663}16641665/*1666 * We break cycles before looping, so an ACTIVE state (or any1667 * other cruft which made its way into the state variable)1668 * is a bug.1669 */1670if(cur->dfs_state != DFS_NONE)1671die("BUG: confusing delta dfs state in first pass:%d",1672 cur->dfs_state);16731674/*1675 * Now we know this is the first time we've seen the object. If1676 * it's not a delta, we're done traversing, but we'll mark it1677 * done to save time on future traversals.1678 */1679if(!DELTA(cur)) {1680 cur->dfs_state = DFS_DONE;1681break;1682}16831684/*1685 * Mark ourselves as active and see if the next step causes1686 * us to cycle to another active object. It's important to do1687 * this _before_ we loop, because it impacts where we make the1688 * cut, and thus how our total_depth counter works.1689 * E.g., We may see a partial loop like:1690 *1691 * A -> B -> C -> D -> B1692 *1693 * Cutting B->C breaks the cycle. But now the depth of A is1694 * only 1, and our total_depth counter is at 3. The size of the1695 * error is always one less than the size of the cycle we1696 * broke. Commits C and D were "lost" from A's chain.1697 *1698 * If we instead cut D->B, then the depth of A is correct at 3.1699 * We keep all commits in the chain that we examined.1700 */1701 cur->dfs_state = DFS_ACTIVE;1702if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1703drop_reused_delta(cur);1704 cur->dfs_state = DFS_DONE;1705break;1706}1707}17081709/*1710 * And now that we've gone all the way to the bottom of the chain, we1711 * need to clear the active flags and set the depth fields as1712 * appropriate. Unlike the loop above, which can quit when it drops a1713 * delta, we need to keep going to look for more depth cuts. So we need1714 * an extra "next" pointer to keep going after we reset cur->delta.1715 */1716for(cur = entry; cur; cur = next) {1717 next =DELTA(cur);17181719/*1720 * We should have a chain of zero or more ACTIVE states down to1721 * a final DONE. We can quit after the DONE, because either it1722 * has no bases, or we've already handled them in a previous1723 * call.1724 */1725if(cur->dfs_state == DFS_DONE)1726break;1727else if(cur->dfs_state != DFS_ACTIVE)1728die("BUG: confusing delta dfs state in second pass:%d",1729 cur->dfs_state);17301731/*1732 * If the total_depth is more than depth, then we need to snip1733 * the chain into two or more smaller chains that don't exceed1734 * the maximum depth. Most of the resulting chains will contain1735 * (depth + 1) entries (i.e., depth deltas plus one base), and1736 * the last chain (i.e., the one containing entry) will contain1737 * whatever entries are left over, namely1738 * (total_depth % (depth + 1)) of them.1739 *1740 * Since we are iterating towards decreasing depth, we need to1741 * decrement total_depth as we go, and we need to write to the1742 * entry what its final depth will be after all of the1743 * snipping. Since we're snipping into chains of length (depth1744 * + 1) entries, the final depth of an entry will be its1745 * original depth modulo (depth + 1). Any time we encounter an1746 * entry whose final depth is supposed to be zero, we snip it1747 * from its delta base, thereby making it so.1748 */1749 cur->depth = (total_depth--) % (depth +1);1750if(!cur->depth)1751drop_reused_delta(cur);17521753 cur->dfs_state = DFS_DONE;1754}1755}17561757static voidget_object_details(void)1758{1759uint32_t i;1760struct object_entry **sorted_by_offset;17611762if(progress)1763 progress_state =start_progress(_("Counting objects"),1764 to_pack.nr_objects);17651766 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1767for(i =0; i < to_pack.nr_objects; i++)1768 sorted_by_offset[i] = to_pack.objects + i;1769QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17701771for(i =0; i < to_pack.nr_objects; i++) {1772struct object_entry *entry = sorted_by_offset[i];1773check_object(entry);1774if(entry->type_valid &&1775oe_size_greater_than(&to_pack, entry, big_file_threshold))1776 entry->no_try_delta =1;1777display_progress(progress_state, i +1);1778}1779stop_progress(&progress_state);17801781/*1782 * This must happen in a second pass, since we rely on the delta1783 * information for the whole list being completed.1784 */1785for(i =0; i < to_pack.nr_objects; i++)1786break_delta_chains(&to_pack.objects[i]);17871788free(sorted_by_offset);1789}17901791/*1792 * We search for deltas in a list sorted by type, by filename hash, and then1793 * by size, so that we see progressively smaller and smaller files.1794 * That's because we prefer deltas to be from the bigger file1795 * to the smaller -- deletes are potentially cheaper, but perhaps1796 * more importantly, the bigger file is likely the more recent1797 * one. The deepest deltas are therefore the oldest objects which are1798 * less susceptible to be accessed often.1799 */1800static inttype_size_sort(const void*_a,const void*_b)1801{1802const struct object_entry *a = *(struct object_entry **)_a;1803const struct object_entry *b = *(struct object_entry **)_b;1804enum object_type a_type =oe_type(a);1805enum object_type b_type =oe_type(b);1806unsigned long a_size =SIZE(a);1807unsigned long b_size =SIZE(b);18081809if(a_type > b_type)1810return-1;1811if(a_type < b_type)1812return1;1813if(a->hash > b->hash)1814return-1;1815if(a->hash < b->hash)1816return1;1817if(a->preferred_base > b->preferred_base)1818return-1;1819if(a->preferred_base < b->preferred_base)1820return1;1821if(a_size > b_size)1822return-1;1823if(a_size < b_size)1824return1;1825return a < b ? -1: (a > b);/* newest first */1826}18271828struct unpacked {1829struct object_entry *entry;1830void*data;1831struct delta_index *index;1832unsigned depth;1833};18341835static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1836unsigned long delta_size)1837{1838if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1839return0;18401841if(delta_size < cache_max_small_delta_size)1842return1;18431844/* cache delta, if objects are large enough compared to delta size */1845if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1846return1;18471848return0;1849}18501851#ifndef NO_PTHREADS18521853static pthread_mutex_t read_mutex;1854#define read_lock() pthread_mutex_lock(&read_mutex)1855#define read_unlock() pthread_mutex_unlock(&read_mutex)18561857static pthread_mutex_t cache_mutex;1858#define cache_lock() pthread_mutex_lock(&cache_mutex)1859#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18601861static pthread_mutex_t progress_mutex;1862#define progress_lock() pthread_mutex_lock(&progress_mutex)1863#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18641865#else18661867#define read_lock() (void)01868#define read_unlock() (void)01869#define cache_lock() (void)01870#define cache_unlock() (void)01871#define progress_lock() (void)01872#define progress_unlock() (void)018731874#endif18751876/*1877 * Return the size of the object without doing any delta1878 * reconstruction (so non-deltas are true object sizes, but deltas1879 * return the size of the delta data).1880 */1881unsigned longoe_get_size_slow(struct packing_data *pack,1882const struct object_entry *e)1883{1884struct packed_git *p;1885struct pack_window *w_curs;1886unsigned char*buf;1887enum object_type type;1888unsigned long used, avail, size;18891890if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1891read_lock();1892if(oid_object_info(the_repository, &e->idx.oid, &size) <0)1893die(_("unable to get size of%s"),1894oid_to_hex(&e->idx.oid));1895read_unlock();1896return size;1897}18981899 p =oe_in_pack(pack, e);1900if(!p)1901BUG("when e->type is a delta, it must belong to a pack");19021903read_lock();1904 w_curs = NULL;1905 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);1906 used =unpack_object_header_buffer(buf, avail, &type, &size);1907if(used ==0)1908die(_("unable to parse object header of%s"),1909oid_to_hex(&e->idx.oid));19101911unuse_pack(&w_curs);1912read_unlock();1913return size;1914}19151916static inttry_delta(struct unpacked *trg,struct unpacked *src,1917unsigned max_depth,unsigned long*mem_usage)1918{1919struct object_entry *trg_entry = trg->entry;1920struct object_entry *src_entry = src->entry;1921unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1922unsigned ref_depth;1923enum object_type type;1924void*delta_buf;19251926/* Don't bother doing diffs between different types */1927if(oe_type(trg_entry) !=oe_type(src_entry))1928return-1;19291930/*1931 * We do not bother to try a delta that we discarded on an1932 * earlier try, but only when reusing delta data. Note that1933 * src_entry that is marked as the preferred_base should always1934 * be considered, as even if we produce a suboptimal delta against1935 * it, we will still save the transfer cost, as we already know1936 * the other side has it and we won't send src_entry at all.1937 */1938if(reuse_delta &&IN_PACK(trg_entry) &&1939IN_PACK(trg_entry) ==IN_PACK(src_entry) &&1940!src_entry->preferred_base &&1941 trg_entry->in_pack_type != OBJ_REF_DELTA &&1942 trg_entry->in_pack_type != OBJ_OFS_DELTA)1943return0;19441945/* Let's not bust the allowed depth. */1946if(src->depth >= max_depth)1947return0;19481949/* Now some size filtering heuristics. */1950 trg_size =SIZE(trg_entry);1951if(!DELTA(trg_entry)) {1952 max_size = trg_size/2-20;1953 ref_depth =1;1954}else{1955 max_size =DELTA_SIZE(trg_entry);1956 ref_depth = trg->depth;1957}1958 max_size = (uint64_t)max_size * (max_depth - src->depth) /1959(max_depth - ref_depth +1);1960if(max_size ==0)1961return0;1962 src_size =SIZE(src_entry);1963 sizediff = src_size < trg_size ? trg_size - src_size :0;1964if(sizediff >= max_size)1965return0;1966if(trg_size < src_size /32)1967return0;19681969/* Load data if not already done */1970if(!trg->data) {1971read_lock();1972 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1973read_unlock();1974if(!trg->data)1975die("object%scannot be read",1976oid_to_hex(&trg_entry->idx.oid));1977if(sz != trg_size)1978die("object%sinconsistent object length (%lu vs%lu)",1979oid_to_hex(&trg_entry->idx.oid), sz,1980 trg_size);1981*mem_usage += sz;1982}1983if(!src->data) {1984read_lock();1985 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);1986read_unlock();1987if(!src->data) {1988if(src_entry->preferred_base) {1989static int warned =0;1990if(!warned++)1991warning("object%scannot be read",1992oid_to_hex(&src_entry->idx.oid));1993/*1994 * Those objects are not included in the1995 * resulting pack. Be resilient and ignore1996 * them if they can't be read, in case the1997 * pack could be created nevertheless.1998 */1999return0;2000}2001die("object%scannot be read",2002oid_to_hex(&src_entry->idx.oid));2003}2004if(sz != src_size)2005die("object%sinconsistent object length (%lu vs%lu)",2006oid_to_hex(&src_entry->idx.oid), sz,2007 src_size);2008*mem_usage += sz;2009}2010if(!src->index) {2011 src->index =create_delta_index(src->data, src_size);2012if(!src->index) {2013static int warned =0;2014if(!warned++)2015warning("suboptimal pack - out of memory");2016return0;2017}2018*mem_usage +=sizeof_delta_index(src->index);2019}20202021 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2022if(!delta_buf)2023return0;2024if(delta_size >= (1U<< OE_DELTA_SIZE_BITS)) {2025free(delta_buf);2026return0;2027}20282029if(DELTA(trg_entry)) {2030/* Prefer only shallower same-sized deltas. */2031if(delta_size ==DELTA_SIZE(trg_entry) &&2032 src->depth +1>= trg->depth) {2033free(delta_buf);2034return0;2035}2036}20372038/*2039 * Handle memory allocation outside of the cache2040 * accounting lock. Compiler will optimize the strangeness2041 * away when NO_PTHREADS is defined.2042 */2043free(trg_entry->delta_data);2044cache_lock();2045if(trg_entry->delta_data) {2046 delta_cache_size -=DELTA_SIZE(trg_entry);2047 trg_entry->delta_data = NULL;2048}2049if(delta_cacheable(src_size, trg_size, delta_size)) {2050 delta_cache_size += delta_size;2051cache_unlock();2052 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2053}else{2054cache_unlock();2055free(delta_buf);2056}20572058SET_DELTA(trg_entry, src_entry);2059SET_DELTA_SIZE(trg_entry, delta_size);2060 trg->depth = src->depth +1;20612062return1;2063}20642065static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2066{2067struct object_entry *child =DELTA_CHILD(me);2068unsigned int m = n;2069while(child) {2070unsigned int c =check_delta_limit(child, n +1);2071if(m < c)2072 m = c;2073 child =DELTA_SIBLING(child);2074}2075return m;2076}20772078static unsigned longfree_unpacked(struct unpacked *n)2079{2080unsigned long freed_mem =sizeof_delta_index(n->index);2081free_delta_index(n->index);2082 n->index = NULL;2083if(n->data) {2084 freed_mem +=SIZE(n->entry);2085FREE_AND_NULL(n->data);2086}2087 n->entry = NULL;2088 n->depth =0;2089return freed_mem;2090}20912092static voidfind_deltas(struct object_entry **list,unsigned*list_size,2093int window,int depth,unsigned*processed)2094{2095uint32_t i, idx =0, count =0;2096struct unpacked *array;2097unsigned long mem_usage =0;20982099 array =xcalloc(window,sizeof(struct unpacked));21002101for(;;) {2102struct object_entry *entry;2103struct unpacked *n = array + idx;2104int j, max_depth, best_base = -1;21052106progress_lock();2107if(!*list_size) {2108progress_unlock();2109break;2110}2111 entry = *list++;2112(*list_size)--;2113if(!entry->preferred_base) {2114(*processed)++;2115display_progress(progress_state, *processed);2116}2117progress_unlock();21182119 mem_usage -=free_unpacked(n);2120 n->entry = entry;21212122while(window_memory_limit &&2123 mem_usage > window_memory_limit &&2124 count >1) {2125uint32_t tail = (idx + window - count) % window;2126 mem_usage -=free_unpacked(array + tail);2127 count--;2128}21292130/* We do not compute delta to *create* objects we are not2131 * going to pack.2132 */2133if(entry->preferred_base)2134goto next;21352136/*2137 * If the current object is at pack edge, take the depth the2138 * objects that depend on the current object into account2139 * otherwise they would become too deep.2140 */2141 max_depth = depth;2142if(DELTA_CHILD(entry)) {2143 max_depth -=check_delta_limit(entry,0);2144if(max_depth <=0)2145goto next;2146}21472148 j = window;2149while(--j >0) {2150int ret;2151uint32_t other_idx = idx + j;2152struct unpacked *m;2153if(other_idx >= window)2154 other_idx -= window;2155 m = array + other_idx;2156if(!m->entry)2157break;2158 ret =try_delta(n, m, max_depth, &mem_usage);2159if(ret <0)2160break;2161else if(ret >0)2162 best_base = other_idx;2163}21642165/*2166 * If we decided to cache the delta data, then it is best2167 * to compress it right away. First because we have to do2168 * it anyway, and doing it here while we're threaded will2169 * save a lot of time in the non threaded write phase,2170 * as well as allow for caching more deltas within2171 * the same cache size limit.2172 * ...2173 * But only if not writing to stdout, since in that case2174 * the network is most likely throttling writes anyway,2175 * and therefore it is best to go to the write phase ASAP2176 * instead, as we can afford spending more time compressing2177 * between writes at that moment.2178 */2179if(entry->delta_data && !pack_to_stdout) {2180unsigned long size;21812182 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2183if(size < (1U<< OE_Z_DELTA_BITS)) {2184 entry->z_delta_size = size;2185cache_lock();2186 delta_cache_size -=DELTA_SIZE(entry);2187 delta_cache_size += entry->z_delta_size;2188cache_unlock();2189}else{2190FREE_AND_NULL(entry->delta_data);2191 entry->z_delta_size =0;2192}2193}21942195/* if we made n a delta, and if n is already at max2196 * depth, leaving it in the window is pointless. we2197 * should evict it first.2198 */2199if(DELTA(entry) && max_depth <= n->depth)2200continue;22012202/*2203 * Move the best delta base up in the window, after the2204 * currently deltified object, to keep it longer. It will2205 * be the first base object to be attempted next.2206 */2207if(DELTA(entry)) {2208struct unpacked swap = array[best_base];2209int dist = (window + idx - best_base) % window;2210int dst = best_base;2211while(dist--) {2212int src = (dst +1) % window;2213 array[dst] = array[src];2214 dst = src;2215}2216 array[dst] = swap;2217}22182219 next:2220 idx++;2221if(count +1< window)2222 count++;2223if(idx >= window)2224 idx =0;2225}22262227for(i =0; i < window; ++i) {2228free_delta_index(array[i].index);2229free(array[i].data);2230}2231free(array);2232}22332234#ifndef NO_PTHREADS22352236static voidtry_to_free_from_threads(size_t size)2237{2238read_lock();2239release_pack_memory(size);2240read_unlock();2241}22422243static try_to_free_t old_try_to_free_routine;22442245/*2246 * The main thread waits on the condition that (at least) one of the workers2247 * has stopped working (which is indicated in the .working member of2248 * struct thread_params).2249 * When a work thread has completed its work, it sets .working to 0 and2250 * signals the main thread and waits on the condition that .data_ready2251 * becomes 1.2252 */22532254struct thread_params {2255 pthread_t thread;2256struct object_entry **list;2257unsigned list_size;2258unsigned remaining;2259int window;2260int depth;2261int working;2262int data_ready;2263 pthread_mutex_t mutex;2264 pthread_cond_t cond;2265unsigned*processed;2266};22672268static pthread_cond_t progress_cond;22692270/*2271 * Mutex and conditional variable can't be statically-initialized on Windows.2272 */2273static voidinit_threaded_search(void)2274{2275init_recursive_mutex(&read_mutex);2276pthread_mutex_init(&cache_mutex, NULL);2277pthread_mutex_init(&progress_mutex, NULL);2278pthread_cond_init(&progress_cond, NULL);2279 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2280}22812282static voidcleanup_threaded_search(void)2283{2284set_try_to_free_routine(old_try_to_free_routine);2285pthread_cond_destroy(&progress_cond);2286pthread_mutex_destroy(&read_mutex);2287pthread_mutex_destroy(&cache_mutex);2288pthread_mutex_destroy(&progress_mutex);2289}22902291static void*threaded_find_deltas(void*arg)2292{2293struct thread_params *me = arg;22942295progress_lock();2296while(me->remaining) {2297progress_unlock();22982299find_deltas(me->list, &me->remaining,2300 me->window, me->depth, me->processed);23012302progress_lock();2303 me->working =0;2304pthread_cond_signal(&progress_cond);2305progress_unlock();23062307/*2308 * We must not set ->data_ready before we wait on the2309 * condition because the main thread may have set it to 12310 * before we get here. In order to be sure that new2311 * work is available if we see 1 in ->data_ready, it2312 * was initialized to 0 before this thread was spawned2313 * and we reset it to 0 right away.2314 */2315pthread_mutex_lock(&me->mutex);2316while(!me->data_ready)2317pthread_cond_wait(&me->cond, &me->mutex);2318 me->data_ready =0;2319pthread_mutex_unlock(&me->mutex);23202321progress_lock();2322}2323progress_unlock();2324/* leave ->working 1 so that this doesn't get more work assigned */2325return NULL;2326}23272328static voidll_find_deltas(struct object_entry **list,unsigned list_size,2329int window,int depth,unsigned*processed)2330{2331struct thread_params *p;2332int i, ret, active_threads =0;23332334init_threaded_search();23352336if(delta_search_threads <=1) {2337find_deltas(list, &list_size, window, depth, processed);2338cleanup_threaded_search();2339return;2340}2341if(progress > pack_to_stdout)2342fprintf(stderr,"Delta compression using up to%dthreads.\n",2343 delta_search_threads);2344 p =xcalloc(delta_search_threads,sizeof(*p));23452346/* Partition the work amongst work threads. */2347for(i =0; i < delta_search_threads; i++) {2348unsigned sub_size = list_size / (delta_search_threads - i);23492350/* don't use too small segments or no deltas will be found */2351if(sub_size <2*window && i+1< delta_search_threads)2352 sub_size =0;23532354 p[i].window = window;2355 p[i].depth = depth;2356 p[i].processed = processed;2357 p[i].working =1;2358 p[i].data_ready =0;23592360/* try to split chunks on "path" boundaries */2361while(sub_size && sub_size < list_size &&2362 list[sub_size]->hash &&2363 list[sub_size]->hash == list[sub_size-1]->hash)2364 sub_size++;23652366 p[i].list = list;2367 p[i].list_size = sub_size;2368 p[i].remaining = sub_size;23692370 list += sub_size;2371 list_size -= sub_size;2372}23732374/* Start work threads. */2375for(i =0; i < delta_search_threads; i++) {2376if(!p[i].list_size)2377continue;2378pthread_mutex_init(&p[i].mutex, NULL);2379pthread_cond_init(&p[i].cond, NULL);2380 ret =pthread_create(&p[i].thread, NULL,2381 threaded_find_deltas, &p[i]);2382if(ret)2383die("unable to create thread:%s",strerror(ret));2384 active_threads++;2385}23862387/*2388 * Now let's wait for work completion. Each time a thread is done2389 * with its work, we steal half of the remaining work from the2390 * thread with the largest number of unprocessed objects and give2391 * it to that newly idle thread. This ensure good load balancing2392 * until the remaining object list segments are simply too short2393 * to be worth splitting anymore.2394 */2395while(active_threads) {2396struct thread_params *target = NULL;2397struct thread_params *victim = NULL;2398unsigned sub_size =0;23992400progress_lock();2401for(;;) {2402for(i =0; !target && i < delta_search_threads; i++)2403if(!p[i].working)2404 target = &p[i];2405if(target)2406break;2407pthread_cond_wait(&progress_cond, &progress_mutex);2408}24092410for(i =0; i < delta_search_threads; i++)2411if(p[i].remaining >2*window &&2412(!victim || victim->remaining < p[i].remaining))2413 victim = &p[i];2414if(victim) {2415 sub_size = victim->remaining /2;2416 list = victim->list + victim->list_size - sub_size;2417while(sub_size && list[0]->hash &&2418 list[0]->hash == list[-1]->hash) {2419 list++;2420 sub_size--;2421}2422if(!sub_size) {2423/*2424 * It is possible for some "paths" to have2425 * so many objects that no hash boundary2426 * might be found. Let's just steal the2427 * exact half in that case.2428 */2429 sub_size = victim->remaining /2;2430 list -= sub_size;2431}2432 target->list = list;2433 victim->list_size -= sub_size;2434 victim->remaining -= sub_size;2435}2436 target->list_size = sub_size;2437 target->remaining = sub_size;2438 target->working =1;2439progress_unlock();24402441pthread_mutex_lock(&target->mutex);2442 target->data_ready =1;2443pthread_cond_signal(&target->cond);2444pthread_mutex_unlock(&target->mutex);24452446if(!sub_size) {2447pthread_join(target->thread, NULL);2448pthread_cond_destroy(&target->cond);2449pthread_mutex_destroy(&target->mutex);2450 active_threads--;2451}2452}2453cleanup_threaded_search();2454free(p);2455}24562457#else2458#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2459#endif24602461static voidadd_tag_chain(const struct object_id *oid)2462{2463struct tag *tag;24642465/*2466 * We catch duplicates already in add_object_entry(), but we'd2467 * prefer to do this extra check to avoid having to parse the2468 * tag at all if we already know that it's being packed (e.g., if2469 * it was included via bitmaps, we would not have parsed it2470 * previously).2471 */2472if(packlist_find(&to_pack, oid->hash, NULL))2473return;24742475 tag =lookup_tag(oid);2476while(1) {2477if(!tag ||parse_tag(tag) || !tag->tagged)2478die("unable to pack objects reachable from tag%s",2479oid_to_hex(oid));24802481add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);24822483if(tag->tagged->type != OBJ_TAG)2484return;24852486 tag = (struct tag *)tag->tagged;2487}2488}24892490static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2491{2492struct object_id peeled;24932494if(starts_with(path,"refs/tags/") &&/* is a tag? */2495!peel_ref(path, &peeled) &&/* peelable? */2496packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2497add_tag_chain(oid);2498return0;2499}25002501static voidprepare_pack(int window,int depth)2502{2503struct object_entry **delta_list;2504uint32_t i, nr_deltas;2505unsigned n;25062507get_object_details();25082509/*2510 * If we're locally repacking then we need to be doubly careful2511 * from now on in order to make sure no stealth corruption gets2512 * propagated to the new pack. Clients receiving streamed packs2513 * should validate everything they get anyway so no need to incur2514 * the additional cost here in that case.2515 */2516if(!pack_to_stdout)2517 do_check_packed_object_crc =1;25182519if(!to_pack.nr_objects || !window || !depth)2520return;25212522ALLOC_ARRAY(delta_list, to_pack.nr_objects);2523 nr_deltas = n =0;25242525for(i =0; i < to_pack.nr_objects; i++) {2526struct object_entry *entry = to_pack.objects + i;25272528if(DELTA(entry))2529/* This happens if we decided to reuse existing2530 * delta from a pack. "reuse_delta &&" is implied.2531 */2532continue;25332534if(!entry->type_valid ||2535oe_size_less_than(&to_pack, entry,50))2536continue;25372538if(entry->no_try_delta)2539continue;25402541if(!entry->preferred_base) {2542 nr_deltas++;2543if(oe_type(entry) <0)2544die("unable to get type of object%s",2545oid_to_hex(&entry->idx.oid));2546}else{2547if(oe_type(entry) <0) {2548/*2549 * This object is not found, but we2550 * don't have to include it anyway.2551 */2552continue;2553}2554}25552556 delta_list[n++] = entry;2557}25582559if(nr_deltas && n >1) {2560unsigned nr_done =0;2561if(progress)2562 progress_state =start_progress(_("Compressing objects"),2563 nr_deltas);2564QSORT(delta_list, n, type_size_sort);2565ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2566stop_progress(&progress_state);2567if(nr_done != nr_deltas)2568die("inconsistency with delta count");2569}2570free(delta_list);2571}25722573static intgit_pack_config(const char*k,const char*v,void*cb)2574{2575if(!strcmp(k,"pack.window")) {2576 window =git_config_int(k, v);2577return0;2578}2579if(!strcmp(k,"pack.windowmemory")) {2580 window_memory_limit =git_config_ulong(k, v);2581return0;2582}2583if(!strcmp(k,"pack.depth")) {2584 depth =git_config_int(k, v);2585return0;2586}2587if(!strcmp(k,"pack.deltacachesize")) {2588 max_delta_cache_size =git_config_int(k, v);2589return0;2590}2591if(!strcmp(k,"pack.deltacachelimit")) {2592 cache_max_small_delta_size =git_config_int(k, v);2593return0;2594}2595if(!strcmp(k,"pack.writebitmaphashcache")) {2596if(git_config_bool(k, v))2597 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2598else2599 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2600}2601if(!strcmp(k,"pack.usebitmaps")) {2602 use_bitmap_index_default =git_config_bool(k, v);2603return0;2604}2605if(!strcmp(k,"pack.threads")) {2606 delta_search_threads =git_config_int(k, v);2607if(delta_search_threads <0)2608die("invalid number of threads specified (%d)",2609 delta_search_threads);2610#ifdef NO_PTHREADS2611if(delta_search_threads !=1) {2612warning("no threads support, ignoring%s", k);2613 delta_search_threads =0;2614}2615#endif2616return0;2617}2618if(!strcmp(k,"pack.indexversion")) {2619 pack_idx_opts.version =git_config_int(k, v);2620if(pack_idx_opts.version >2)2621die("bad pack.indexversion=%"PRIu32,2622 pack_idx_opts.version);2623return0;2624}2625returngit_default_config(k, v, cb);2626}26272628static voidread_object_list_from_stdin(void)2629{2630char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2631struct object_id oid;2632const char*p;26332634for(;;) {2635if(!fgets(line,sizeof(line), stdin)) {2636if(feof(stdin))2637break;2638if(!ferror(stdin))2639die("fgets returned NULL, not EOF, not error!");2640if(errno != EINTR)2641die_errno("fgets");2642clearerr(stdin);2643continue;2644}2645if(line[0] =='-') {2646if(get_oid_hex(line+1, &oid))2647die("expected edge object ID, got garbage:\n%s",2648 line);2649add_preferred_base(&oid);2650continue;2651}2652if(parse_oid_hex(line, &oid, &p))2653die("expected object ID, got garbage:\n%s", line);26542655add_preferred_base_object(p +1);2656add_object_entry(&oid, OBJ_NONE, p +1,0);2657}2658}26592660/* Remember to update object flag allocation in object.h */2661#define OBJECT_ADDED (1u<<20)26622663static voidshow_commit(struct commit *commit,void*data)2664{2665add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2666 commit->object.flags |= OBJECT_ADDED;26672668if(write_bitmap_index)2669index_commit_for_bitmap(commit);2670}26712672static voidshow_object(struct object *obj,const char*name,void*data)2673{2674add_preferred_base_object(name);2675add_object_entry(&obj->oid, obj->type, name,0);2676 obj->flags |= OBJECT_ADDED;2677}26782679static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2680{2681assert(arg_missing_action == MA_ALLOW_ANY);26822683/*2684 * Quietly ignore ALL missing objects. This avoids problems with2685 * staging them now and getting an odd error later.2686 */2687if(!has_object_file(&obj->oid))2688return;26892690show_object(obj, name, data);2691}26922693static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2694{2695assert(arg_missing_action == MA_ALLOW_PROMISOR);26962697/*2698 * Quietly ignore EXPECTED missing objects. This avoids problems with2699 * staging them now and getting an odd error later.2700 */2701if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2702return;27032704show_object(obj, name, data);2705}27062707static intoption_parse_missing_action(const struct option *opt,2708const char*arg,int unset)2709{2710assert(arg);2711assert(!unset);27122713if(!strcmp(arg,"error")) {2714 arg_missing_action = MA_ERROR;2715 fn_show_object = show_object;2716return0;2717}27182719if(!strcmp(arg,"allow-any")) {2720 arg_missing_action = MA_ALLOW_ANY;2721 fetch_if_missing =0;2722 fn_show_object = show_object__ma_allow_any;2723return0;2724}27252726if(!strcmp(arg,"allow-promisor")) {2727 arg_missing_action = MA_ALLOW_PROMISOR;2728 fetch_if_missing =0;2729 fn_show_object = show_object__ma_allow_promisor;2730return0;2731}27322733die(_("invalid value for --missing"));2734return0;2735}27362737static voidshow_edge(struct commit *commit)2738{2739add_preferred_base(&commit->object.oid);2740}27412742struct in_pack_object {2743 off_t offset;2744struct object *object;2745};27462747struct in_pack {2748unsigned int alloc;2749unsigned int nr;2750struct in_pack_object *array;2751};27522753static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2754{2755 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2756 in_pack->array[in_pack->nr].object = object;2757 in_pack->nr++;2758}27592760/*2761 * Compare the objects in the offset order, in order to emulate the2762 * "git rev-list --objects" output that produced the pack originally.2763 */2764static intofscmp(const void*a_,const void*b_)2765{2766struct in_pack_object *a = (struct in_pack_object *)a_;2767struct in_pack_object *b = (struct in_pack_object *)b_;27682769if(a->offset < b->offset)2770return-1;2771else if(a->offset > b->offset)2772return1;2773else2774returnoidcmp(&a->object->oid, &b->object->oid);2775}27762777static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2778{2779struct packed_git *p;2780struct in_pack in_pack;2781uint32_t i;27822783memset(&in_pack,0,sizeof(in_pack));27842785for(p =get_packed_git(the_repository); p; p = p->next) {2786struct object_id oid;2787struct object *o;27882789if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2790continue;2791if(open_pack_index(p))2792die("cannot open pack index");27932794ALLOC_GROW(in_pack.array,2795 in_pack.nr + p->num_objects,2796 in_pack.alloc);27972798for(i =0; i < p->num_objects; i++) {2799nth_packed_object_oid(&oid, p, i);2800 o =lookup_unknown_object(oid.hash);2801if(!(o->flags & OBJECT_ADDED))2802mark_in_pack_object(o, p, &in_pack);2803 o->flags |= OBJECT_ADDED;2804}2805}28062807if(in_pack.nr) {2808QSORT(in_pack.array, in_pack.nr, ofscmp);2809for(i =0; i < in_pack.nr; i++) {2810struct object *o = in_pack.array[i].object;2811add_object_entry(&o->oid, o->type,"",0);2812}2813}2814free(in_pack.array);2815}28162817static intadd_loose_object(const struct object_id *oid,const char*path,2818void*data)2819{2820enum object_type type =oid_object_info(the_repository, oid, NULL);28212822if(type <0) {2823warning("loose object at%scould not be examined", path);2824return0;2825}28262827add_object_entry(oid, type,"",0);2828return0;2829}28302831/*2832 * We actually don't even have to worry about reachability here.2833 * add_object_entry will weed out duplicates, so we just add every2834 * loose object we find.2835 */2836static voidadd_unreachable_loose_objects(void)2837{2838for_each_loose_file_in_objdir(get_object_directory(),2839 add_loose_object,2840 NULL, NULL, NULL);2841}28422843static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2844{2845static struct packed_git *last_found = (void*)1;2846struct packed_git *p;28472848 p = (last_found != (void*)1) ? last_found :2849get_packed_git(the_repository);28502851while(p) {2852if((!p->pack_local || p->pack_keep ||2853 p->pack_keep_in_core) &&2854find_pack_entry_one(oid->hash, p)) {2855 last_found = p;2856return1;2857}2858if(p == last_found)2859 p =get_packed_git(the_repository);2860else2861 p = p->next;2862if(p == last_found)2863 p = p->next;2864}2865return0;2866}28672868/*2869 * Store a list of sha1s that are should not be discarded2870 * because they are either written too recently, or are2871 * reachable from another object that was.2872 *2873 * This is filled by get_object_list.2874 */2875static struct oid_array recent_objects;28762877static intloosened_object_can_be_discarded(const struct object_id *oid,2878 timestamp_t mtime)2879{2880if(!unpack_unreachable_expiration)2881return0;2882if(mtime > unpack_unreachable_expiration)2883return0;2884if(oid_array_lookup(&recent_objects, oid) >=0)2885return0;2886return1;2887}28882889static voidloosen_unused_packed_objects(struct rev_info *revs)2890{2891struct packed_git *p;2892uint32_t i;2893struct object_id oid;28942895for(p =get_packed_git(the_repository); p; p = p->next) {2896if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2897continue;28982899if(open_pack_index(p))2900die("cannot open pack index");29012902for(i =0; i < p->num_objects; i++) {2903nth_packed_object_oid(&oid, p, i);2904if(!packlist_find(&to_pack, oid.hash, NULL) &&2905!has_sha1_pack_kept_or_nonlocal(&oid) &&2906!loosened_object_can_be_discarded(&oid, p->mtime))2907if(force_object_loose(&oid, p->mtime))2908die("unable to force loose object");2909}2910}2911}29122913/*2914 * This tracks any options which pack-reuse code expects to be on, or which a2915 * reader of the pack might not understand, and which would therefore prevent2916 * blind reuse of what we have on disk.2917 */2918static intpack_options_allow_reuse(void)2919{2920return pack_to_stdout &&2921 allow_ofs_delta &&2922!ignore_packed_keep_on_disk &&2923!ignore_packed_keep_in_core &&2924(!local || !have_non_local_packs) &&2925!incremental;2926}29272928static intget_object_list_from_bitmap(struct rev_info *revs)2929{2930if(prepare_bitmap_walk(revs) <0)2931return-1;29322933if(pack_options_allow_reuse() &&2934!reuse_partial_packfile_from_bitmap(2935&reuse_packfile,2936&reuse_packfile_objects,2937&reuse_packfile_offset)) {2938assert(reuse_packfile_objects);2939 nr_result += reuse_packfile_objects;2940display_progress(progress_state, nr_result);2941}29422943traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2944return0;2945}29462947static voidrecord_recent_object(struct object *obj,2948const char*name,2949void*data)2950{2951oid_array_append(&recent_objects, &obj->oid);2952}29532954static voidrecord_recent_commit(struct commit *commit,void*data)2955{2956oid_array_append(&recent_objects, &commit->object.oid);2957}29582959static voidget_object_list(int ac,const char**av)2960{2961struct rev_info revs;2962char line[1000];2963int flags =0;29642965init_revisions(&revs, NULL);2966 save_commit_buffer =0;2967setup_revisions(ac, av, &revs, NULL);29682969/* make sure shallows are read */2970is_repository_shallow();29712972while(fgets(line,sizeof(line), stdin) != NULL) {2973int len =strlen(line);2974if(len && line[len -1] =='\n')2975 line[--len] =0;2976if(!len)2977break;2978if(*line =='-') {2979if(!strcmp(line,"--not")) {2980 flags ^= UNINTERESTING;2981 write_bitmap_index =0;2982continue;2983}2984if(starts_with(line,"--shallow ")) {2985struct object_id oid;2986if(get_oid_hex(line +10, &oid))2987die("not an SHA-1 '%s'", line +10);2988register_shallow(&oid);2989 use_bitmap_index =0;2990continue;2991}2992die("not a rev '%s'", line);2993}2994if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2995die("bad revision '%s'", line);2996}29972998if(use_bitmap_index && !get_object_list_from_bitmap(&revs))2999return;30003001if(prepare_revision_walk(&revs))3002die("revision walk setup failed");3003mark_edges_uninteresting(&revs, show_edge);30043005if(!fn_show_object)3006 fn_show_object = show_object;3007traverse_commit_list_filtered(&filter_options, &revs,3008 show_commit, fn_show_object, NULL,3009 NULL);30103011if(unpack_unreachable_expiration) {3012 revs.ignore_missing_links =1;3013if(add_unseen_recent_objects_to_traversal(&revs,3014 unpack_unreachable_expiration))3015die("unable to add recent objects");3016if(prepare_revision_walk(&revs))3017die("revision walk setup failed");3018traverse_commit_list(&revs, record_recent_commit,3019 record_recent_object, NULL);3020}30213022if(keep_unreachable)3023add_objects_in_unpacked_packs(&revs);3024if(pack_loose_unreachable)3025add_unreachable_loose_objects();3026if(unpack_unreachable)3027loosen_unused_packed_objects(&revs);30283029oid_array_clear(&recent_objects);3030}30313032static voidadd_extra_kept_packs(const struct string_list *names)3033{3034struct packed_git *p;30353036if(!names->nr)3037return;30383039for(p =get_packed_git(the_repository); p; p = p->next) {3040const char*name =basename(p->pack_name);3041int i;30423043if(!p->pack_local)3044continue;30453046for(i =0; i < names->nr; i++)3047if(!fspathcmp(name, names->items[i].string))3048break;30493050if(i < names->nr) {3051 p->pack_keep_in_core =1;3052 ignore_packed_keep_in_core =1;3053continue;3054}3055}3056}30573058static intoption_parse_index_version(const struct option *opt,3059const char*arg,int unset)3060{3061char*c;3062const char*val = arg;3063 pack_idx_opts.version =strtoul(val, &c,10);3064if(pack_idx_opts.version >2)3065die(_("unsupported index version%s"), val);3066if(*c ==','&& c[1])3067 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3068if(*c || pack_idx_opts.off32_limit &0x80000000)3069die(_("bad index version '%s'"), val);3070return0;3071}30723073static intoption_parse_unpack_unreachable(const struct option *opt,3074const char*arg,int unset)3075{3076if(unset) {3077 unpack_unreachable =0;3078 unpack_unreachable_expiration =0;3079}3080else{3081 unpack_unreachable =1;3082if(arg)3083 unpack_unreachable_expiration =approxidate(arg);3084}3085return0;3086}30873088intcmd_pack_objects(int argc,const char**argv,const char*prefix)3089{3090int use_internal_rev_list =0;3091int thin =0;3092int shallow =0;3093int all_progress_implied =0;3094struct argv_array rp = ARGV_ARRAY_INIT;3095int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3096int rev_list_index =0;3097struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3098struct option pack_objects_options[] = {3099OPT_SET_INT('q',"quiet", &progress,3100N_("do not show progress meter"),0),3101OPT_SET_INT(0,"progress", &progress,3102N_("show progress meter"),1),3103OPT_SET_INT(0,"all-progress", &progress,3104N_("show progress meter during object writing phase"),2),3105OPT_BOOL(0,"all-progress-implied",3106&all_progress_implied,3107N_("similar to --all-progress when progress meter is shown")),3108{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),3109N_("write the pack index file in the specified idx format version"),31100, option_parse_index_version },3111OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3112N_("maximum size of each output pack file")),3113OPT_BOOL(0,"local", &local,3114N_("ignore borrowed objects from alternate object store")),3115OPT_BOOL(0,"incremental", &incremental,3116N_("ignore packed objects")),3117OPT_INTEGER(0,"window", &window,3118N_("limit pack window by objects")),3119OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3120N_("limit pack window by memory in addition to object limit")),3121OPT_INTEGER(0,"depth", &depth,3122N_("maximum length of delta chain allowed in the resulting pack")),3123OPT_BOOL(0,"reuse-delta", &reuse_delta,3124N_("reuse existing deltas")),3125OPT_BOOL(0,"reuse-object", &reuse_object,3126N_("reuse existing objects")),3127OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3128N_("use OFS_DELTA objects")),3129OPT_INTEGER(0,"threads", &delta_search_threads,3130N_("use threads when searching for best delta matches")),3131OPT_BOOL(0,"non-empty", &non_empty,3132N_("do not create an empty pack output")),3133OPT_BOOL(0,"revs", &use_internal_rev_list,3134N_("read revision arguments from standard input")),3135{ OPTION_SET_INT,0,"unpacked", &rev_list_unpacked, NULL,3136N_("limit the objects to those that are not yet packed"),3137 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3138{ OPTION_SET_INT,0,"all", &rev_list_all, NULL,3139N_("include objects reachable from any reference"),3140 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3141{ OPTION_SET_INT,0,"reflog", &rev_list_reflog, NULL,3142N_("include objects referred by reflog entries"),3143 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3144{ OPTION_SET_INT,0,"indexed-objects", &rev_list_index, NULL,3145N_("include objects referred to by the index"),3146 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3147OPT_BOOL(0,"stdout", &pack_to_stdout,3148N_("output pack to stdout")),3149OPT_BOOL(0,"include-tag", &include_tag,3150N_("include tag objects that refer to objects to be packed")),3151OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3152N_("keep unreachable objects")),3153OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3154N_("pack loose unreachable objects")),3155{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3156N_("unpack unreachable objects newer than <time>"),3157 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3158OPT_BOOL(0,"thin", &thin,3159N_("create thin packs")),3160OPT_BOOL(0,"shallow", &shallow,3161N_("create packs suitable for shallow fetches")),3162OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3163N_("ignore packs that have companion .keep file")),3164OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3165N_("ignore this pack")),3166OPT_INTEGER(0,"compression", &pack_compression_level,3167N_("pack compression level")),3168OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3169N_("do not hide commits by grafts"),0),3170OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3171N_("use a bitmap index if available to speed up counting objects")),3172OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3173N_("write a bitmap index together with the pack index")),3174OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3175{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3176N_("handling for missing objects"), PARSE_OPT_NONEG,3177 option_parse_missing_action },3178OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3179N_("do not pack objects in promisor packfiles")),3180OPT_END(),3181};31823183if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3184BUG("too many dfs states, increase OE_DFS_STATE_BITS");31853186 check_replace_refs =0;31873188reset_pack_idx_option(&pack_idx_opts);3189git_config(git_pack_config, NULL);31903191 progress =isatty(2);3192 argc =parse_options(argc, argv, prefix, pack_objects_options,3193 pack_usage,0);31943195if(argc) {3196 base_name = argv[0];3197 argc--;3198}3199if(pack_to_stdout != !base_name || argc)3200usage_with_options(pack_usage, pack_objects_options);32013202if(depth >= (1<< OE_DEPTH_BITS)) {3203warning(_("delta chain depth%dis too deep, forcing%d"),3204 depth, (1<< OE_DEPTH_BITS) -1);3205 depth = (1<< OE_DEPTH_BITS) -1;3206}3207if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3208warning(_("pack.deltaCacheLimit is too high, forcing%d"),3209(1U<< OE_Z_DELTA_BITS) -1);3210 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3211}32123213argv_array_push(&rp,"pack-objects");3214if(thin) {3215 use_internal_rev_list =1;3216argv_array_push(&rp, shallow3217?"--objects-edge-aggressive"3218:"--objects-edge");3219}else3220argv_array_push(&rp,"--objects");32213222if(rev_list_all) {3223 use_internal_rev_list =1;3224argv_array_push(&rp,"--all");3225}3226if(rev_list_reflog) {3227 use_internal_rev_list =1;3228argv_array_push(&rp,"--reflog");3229}3230if(rev_list_index) {3231 use_internal_rev_list =1;3232argv_array_push(&rp,"--indexed-objects");3233}3234if(rev_list_unpacked) {3235 use_internal_rev_list =1;3236argv_array_push(&rp,"--unpacked");3237}32383239if(exclude_promisor_objects) {3240 use_internal_rev_list =1;3241 fetch_if_missing =0;3242argv_array_push(&rp,"--exclude-promisor-objects");3243}3244if(unpack_unreachable || keep_unreachable || pack_loose_unreachable)3245 use_internal_rev_list =1;32463247if(!reuse_object)3248 reuse_delta =0;3249if(pack_compression_level == -1)3250 pack_compression_level = Z_DEFAULT_COMPRESSION;3251else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3252die("bad pack compression level%d", pack_compression_level);32533254if(!delta_search_threads)/* --threads=0 means autodetect */3255 delta_search_threads =online_cpus();32563257#ifdef NO_PTHREADS3258if(delta_search_threads !=1)3259warning("no threads support, ignoring --threads");3260#endif3261if(!pack_to_stdout && !pack_size_limit)3262 pack_size_limit = pack_size_limit_cfg;3263if(pack_to_stdout && pack_size_limit)3264die("--max-pack-size cannot be used to build a pack for transfer.");3265if(pack_size_limit && pack_size_limit <1024*1024) {3266warning("minimum pack size limit is 1 MiB");3267 pack_size_limit =1024*1024;3268}32693270if(!pack_to_stdout && thin)3271die("--thin cannot be used to build an indexable pack.");32723273if(keep_unreachable && unpack_unreachable)3274die("--keep-unreachable and --unpack-unreachable are incompatible.");3275if(!rev_list_all || !rev_list_reflog || !rev_list_index)3276 unpack_unreachable_expiration =0;32773278if(filter_options.choice) {3279if(!pack_to_stdout)3280die("cannot use --filter without --stdout.");3281 use_bitmap_index =0;3282}32833284/*3285 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3286 *3287 * - to produce good pack (with bitmap index not-yet-packed objects are3288 * packed in suboptimal order).3289 *3290 * - to use more robust pack-generation codepath (avoiding possible3291 * bugs in bitmap code and possible bitmap index corruption).3292 */3293if(!pack_to_stdout)3294 use_bitmap_index_default =0;32953296if(use_bitmap_index <0)3297 use_bitmap_index = use_bitmap_index_default;32983299/* "hard" reasons not to use bitmaps; these just won't work at all */3300if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3301 use_bitmap_index =0;33023303if(pack_to_stdout || !rev_list_all)3304 write_bitmap_index =0;33053306if(progress && all_progress_implied)3307 progress =2;33083309add_extra_kept_packs(&keep_pack_list);3310if(ignore_packed_keep_on_disk) {3311struct packed_git *p;3312for(p =get_packed_git(the_repository); p; p = p->next)3313if(p->pack_local && p->pack_keep)3314break;3315if(!p)/* no keep-able packs found */3316 ignore_packed_keep_on_disk =0;3317}3318if(local) {3319/*3320 * unlike ignore_packed_keep_on_disk above, we do not3321 * want to unset "local" based on looking at packs, as3322 * it also covers non-local objects3323 */3324struct packed_git *p;3325for(p =get_packed_git(the_repository); p; p = p->next) {3326if(!p->pack_local) {3327 have_non_local_packs =1;3328break;3329}3330}3331}33323333prepare_packing_data(&to_pack);33343335if(progress)3336 progress_state =start_progress(_("Enumerating objects"),0);3337if(!use_internal_rev_list)3338read_object_list_from_stdin();3339else{3340get_object_list(rp.argc, rp.argv);3341argv_array_clear(&rp);3342}3343cleanup_preferred_base();3344if(include_tag && nr_result)3345for_each_ref(add_ref_tag, NULL);3346stop_progress(&progress_state);33473348if(non_empty && !nr_result)3349return0;3350if(nr_result)3351prepare_pack(window, depth);3352write_pack_file();3353if(progress)3354fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3355" reused %"PRIu32" (delta %"PRIu32")\n",3356 written, written_delta, reused, reused_delta);3357return0;3358}