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"delta-islands.h" 28#include"reachable.h" 29#include"sha1-array.h" 30#include"argv-array.h" 31#include"list.h" 32#include"packfile.h" 33#include"object-store.h" 34#include"dir.h" 35#include"midx.h" 36 37#define IN_PACK(obj) oe_in_pack(&to_pack, obj) 38#define SIZE(obj) oe_size(&to_pack, obj) 39#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size) 40#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj) 41#define DELTA(obj) oe_delta(&to_pack, obj) 42#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj) 43#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj) 44#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val) 45#define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid) 46#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val) 47#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val) 48#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val) 49 50static const char*pack_usage[] = { 51N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 52N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 53 NULL 54}; 55 56/* 57 * Objects we are going to pack are collected in the `to_pack` structure. 58 * It contains an array (dynamically expanded) of the object data, and a map 59 * that can resolve SHA1s to their position in the array. 60 */ 61static struct packing_data to_pack; 62 63static struct pack_idx_entry **written_list; 64static uint32_t nr_result, nr_written, nr_seen; 65static struct bitmap_index *bitmap_git; 66static uint32_t write_layer; 67 68static int non_empty; 69static int reuse_delta =1, reuse_object =1; 70static int keep_unreachable, unpack_unreachable, include_tag; 71static timestamp_t unpack_unreachable_expiration; 72static int pack_loose_unreachable; 73static int local; 74static int have_non_local_packs; 75static int incremental; 76static int ignore_packed_keep_on_disk; 77static int ignore_packed_keep_in_core; 78static int allow_ofs_delta; 79static struct pack_idx_option pack_idx_opts; 80static const char*base_name; 81static int progress =1; 82static int window =10; 83static unsigned long pack_size_limit; 84static int depth =50; 85static int delta_search_threads; 86static int pack_to_stdout; 87static int thin; 88static int num_preferred_base; 89static struct progress *progress_state; 90 91static struct packed_git *reuse_packfile; 92static uint32_t reuse_packfile_objects; 93static off_t reuse_packfile_offset; 94 95static int use_bitmap_index_default =1; 96static int use_bitmap_index = -1; 97static int write_bitmap_index; 98static uint16_t write_bitmap_options; 99 100static int exclude_promisor_objects; 101 102static int use_delta_islands; 103 104static unsigned long delta_cache_size =0; 105static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; 106static unsigned long cache_max_small_delta_size =1000; 107 108static unsigned long window_memory_limit =0; 109 110static struct list_objects_filter_options filter_options; 111 112enum missing_action { 113 MA_ERROR =0,/* fail if any missing objects are encountered */ 114 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 115 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 116}; 117static enum missing_action arg_missing_action; 118static show_object_fn fn_show_object; 119 120/* 121 * stats 122 */ 123static uint32_t written, written_delta; 124static uint32_t reused, reused_delta; 125 126/* 127 * Indexed commits 128 */ 129static struct commit **indexed_commits; 130static unsigned int indexed_commits_nr; 131static unsigned int indexed_commits_alloc; 132 133static voidindex_commit_for_bitmap(struct commit *commit) 134{ 135if(indexed_commits_nr >= indexed_commits_alloc) { 136 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 137REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 138} 139 140 indexed_commits[indexed_commits_nr++] = commit; 141} 142 143static void*get_delta(struct object_entry *entry) 144{ 145unsigned long size, base_size, delta_size; 146void*buf, *base_buf, *delta_buf; 147enum object_type type; 148 149 buf =read_object_file(&entry->idx.oid, &type, &size); 150if(!buf) 151die(_("unable to read%s"),oid_to_hex(&entry->idx.oid)); 152 base_buf =read_object_file(&DELTA(entry)->idx.oid, &type, 153&base_size); 154if(!base_buf) 155die("unable to read%s", 156oid_to_hex(&DELTA(entry)->idx.oid)); 157 delta_buf =diff_delta(base_buf, base_size, 158 buf, size, &delta_size,0); 159/* 160 * We succesfully computed this delta once but dropped it for 161 * memory reasons. Something is very wrong if this time we 162 * recompute and create a different delta. 163 */ 164if(!delta_buf || delta_size !=DELTA_SIZE(entry)) 165BUG("delta size changed"); 166free(buf); 167free(base_buf); 168return delta_buf; 169} 170 171static unsigned longdo_compress(void**pptr,unsigned long size) 172{ 173 git_zstream stream; 174void*in, *out; 175unsigned long maxsize; 176 177git_deflate_init(&stream, pack_compression_level); 178 maxsize =git_deflate_bound(&stream, size); 179 180 in = *pptr; 181 out =xmalloc(maxsize); 182*pptr = out; 183 184 stream.next_in = in; 185 stream.avail_in = size; 186 stream.next_out = out; 187 stream.avail_out = maxsize; 188while(git_deflate(&stream, Z_FINISH) == Z_OK) 189;/* nothing */ 190git_deflate_end(&stream); 191 192free(in); 193return stream.total_out; 194} 195 196static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 197const struct object_id *oid) 198{ 199 git_zstream stream; 200unsigned char ibuf[1024*16]; 201unsigned char obuf[1024*16]; 202unsigned long olen =0; 203 204git_deflate_init(&stream, pack_compression_level); 205 206for(;;) { 207 ssize_t readlen; 208int zret = Z_OK; 209 readlen =read_istream(st, ibuf,sizeof(ibuf)); 210if(readlen == -1) 211die(_("unable to read%s"),oid_to_hex(oid)); 212 213 stream.next_in = ibuf; 214 stream.avail_in = readlen; 215while((stream.avail_in || readlen ==0) && 216(zret == Z_OK || zret == Z_BUF_ERROR)) { 217 stream.next_out = obuf; 218 stream.avail_out =sizeof(obuf); 219 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 220hashwrite(f, obuf, stream.next_out - obuf); 221 olen += stream.next_out - obuf; 222} 223if(stream.avail_in) 224die(_("deflate error (%d)"), zret); 225if(readlen ==0) { 226if(zret != Z_STREAM_END) 227die(_("deflate error (%d)"), zret); 228break; 229} 230} 231git_deflate_end(&stream); 232return olen; 233} 234 235/* 236 * we are going to reuse the existing object data as is. make 237 * sure it is not corrupt. 238 */ 239static intcheck_pack_inflate(struct packed_git *p, 240struct pack_window **w_curs, 241 off_t offset, 242 off_t len, 243unsigned long expect) 244{ 245 git_zstream stream; 246unsigned char fakebuf[4096], *in; 247int st; 248 249memset(&stream,0,sizeof(stream)); 250git_inflate_init(&stream); 251do{ 252 in =use_pack(p, w_curs, offset, &stream.avail_in); 253 stream.next_in = in; 254 stream.next_out = fakebuf; 255 stream.avail_out =sizeof(fakebuf); 256 st =git_inflate(&stream, Z_FINISH); 257 offset += stream.next_in - in; 258}while(st == Z_OK || st == Z_BUF_ERROR); 259git_inflate_end(&stream); 260return(st == Z_STREAM_END && 261 stream.total_out == expect && 262 stream.total_in == len) ?0: -1; 263} 264 265static voidcopy_pack_data(struct hashfile *f, 266struct packed_git *p, 267struct pack_window **w_curs, 268 off_t offset, 269 off_t len) 270{ 271unsigned char*in; 272unsigned long avail; 273 274while(len) { 275 in =use_pack(p, w_curs, offset, &avail); 276if(avail > len) 277 avail = (unsigned long)len; 278hashwrite(f, in, avail); 279 offset += avail; 280 len -= avail; 281} 282} 283 284/* Return 0 if we will bust the pack-size limit */ 285static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 286unsigned long limit,int usable_delta) 287{ 288unsigned long size, datalen; 289unsigned char header[MAX_PACK_OBJECT_HEADER], 290 dheader[MAX_PACK_OBJECT_HEADER]; 291unsigned hdrlen; 292enum object_type type; 293void*buf; 294struct git_istream *st = NULL; 295const unsigned hashsz = the_hash_algo->rawsz; 296 297if(!usable_delta) { 298if(oe_type(entry) == OBJ_BLOB && 299oe_size_greater_than(&to_pack, entry, big_file_threshold) && 300(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 301 buf = NULL; 302else{ 303 buf =read_object_file(&entry->idx.oid, &type, &size); 304if(!buf) 305die(_("unable to read%s"), 306oid_to_hex(&entry->idx.oid)); 307} 308/* 309 * make sure no cached delta data remains from a 310 * previous attempt before a pack split occurred. 311 */ 312FREE_AND_NULL(entry->delta_data); 313 entry->z_delta_size =0; 314}else if(entry->delta_data) { 315 size =DELTA_SIZE(entry); 316 buf = entry->delta_data; 317 entry->delta_data = NULL; 318 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 319 OBJ_OFS_DELTA : OBJ_REF_DELTA; 320}else{ 321 buf =get_delta(entry); 322 size =DELTA_SIZE(entry); 323 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 324 OBJ_OFS_DELTA : OBJ_REF_DELTA; 325} 326 327if(st)/* large blob case, just assume we don't compress well */ 328 datalen = size; 329else if(entry->z_delta_size) 330 datalen = entry->z_delta_size; 331else 332 datalen =do_compress(&buf, size); 333 334/* 335 * The object header is a byte of 'type' followed by zero or 336 * more bytes of length. 337 */ 338 hdrlen =encode_in_pack_object_header(header,sizeof(header), 339 type, size); 340 341if(type == OBJ_OFS_DELTA) { 342/* 343 * Deltas with relative base contain an additional 344 * encoding of the relative offset for the delta 345 * base from this object's position in the pack. 346 */ 347 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 348unsigned pos =sizeof(dheader) -1; 349 dheader[pos] = ofs &127; 350while(ofs >>=7) 351 dheader[--pos] =128| (--ofs &127); 352if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 353if(st) 354close_istream(st); 355free(buf); 356return0; 357} 358hashwrite(f, header, hdrlen); 359hashwrite(f, dheader + pos,sizeof(dheader) - pos); 360 hdrlen +=sizeof(dheader) - pos; 361}else if(type == OBJ_REF_DELTA) { 362/* 363 * Deltas with a base reference contain 364 * additional bytes for the base object ID. 365 */ 366if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 367if(st) 368close_istream(st); 369free(buf); 370return0; 371} 372hashwrite(f, header, hdrlen); 373hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 374 hdrlen += hashsz; 375}else{ 376if(limit && hdrlen + datalen + hashsz >= limit) { 377if(st) 378close_istream(st); 379free(buf); 380return0; 381} 382hashwrite(f, header, hdrlen); 383} 384if(st) { 385 datalen =write_large_blob_data(st, f, &entry->idx.oid); 386close_istream(st); 387}else{ 388hashwrite(f, buf, datalen); 389free(buf); 390} 391 392return hdrlen + datalen; 393} 394 395/* Return 0 if we will bust the pack-size limit */ 396static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 397unsigned long limit,int usable_delta) 398{ 399struct packed_git *p =IN_PACK(entry); 400struct pack_window *w_curs = NULL; 401struct revindex_entry *revidx; 402 off_t offset; 403enum object_type type =oe_type(entry); 404 off_t datalen; 405unsigned char header[MAX_PACK_OBJECT_HEADER], 406 dheader[MAX_PACK_OBJECT_HEADER]; 407unsigned hdrlen; 408const unsigned hashsz = the_hash_algo->rawsz; 409unsigned long entry_size =SIZE(entry); 410 411if(DELTA(entry)) 412 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 413 OBJ_OFS_DELTA : OBJ_REF_DELTA; 414 hdrlen =encode_in_pack_object_header(header,sizeof(header), 415 type, entry_size); 416 417 offset = entry->in_pack_offset; 418 revidx =find_pack_revindex(p, offset); 419 datalen = revidx[1].offset - offset; 420if(!pack_to_stdout && p->index_version >1&& 421check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 422error(_("bad packed object CRC for%s"), 423oid_to_hex(&entry->idx.oid)); 424unuse_pack(&w_curs); 425returnwrite_no_reuse_object(f, entry, limit, usable_delta); 426} 427 428 offset += entry->in_pack_header_size; 429 datalen -= entry->in_pack_header_size; 430 431if(!pack_to_stdout && p->index_version ==1&& 432check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 433error(_("corrupt packed object for%s"), 434oid_to_hex(&entry->idx.oid)); 435unuse_pack(&w_curs); 436returnwrite_no_reuse_object(f, entry, limit, usable_delta); 437} 438 439if(type == OBJ_OFS_DELTA) { 440 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 441unsigned pos =sizeof(dheader) -1; 442 dheader[pos] = ofs &127; 443while(ofs >>=7) 444 dheader[--pos] =128| (--ofs &127); 445if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 446unuse_pack(&w_curs); 447return0; 448} 449hashwrite(f, header, hdrlen); 450hashwrite(f, dheader + pos,sizeof(dheader) - pos); 451 hdrlen +=sizeof(dheader) - pos; 452 reused_delta++; 453}else if(type == OBJ_REF_DELTA) { 454if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 455unuse_pack(&w_curs); 456return0; 457} 458hashwrite(f, header, hdrlen); 459hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 460 hdrlen += hashsz; 461 reused_delta++; 462}else{ 463if(limit && hdrlen + datalen + hashsz >= limit) { 464unuse_pack(&w_curs); 465return0; 466} 467hashwrite(f, header, hdrlen); 468} 469copy_pack_data(f, p, &w_curs, offset, datalen); 470unuse_pack(&w_curs); 471 reused++; 472return hdrlen + datalen; 473} 474 475/* Return 0 if we will bust the pack-size limit */ 476static off_t write_object(struct hashfile *f, 477struct object_entry *entry, 478 off_t write_offset) 479{ 480unsigned long limit; 481 off_t len; 482int usable_delta, to_reuse; 483 484if(!pack_to_stdout) 485crc32_begin(f); 486 487/* apply size limit if limited packsize and not first object */ 488if(!pack_size_limit || !nr_written) 489 limit =0; 490else if(pack_size_limit <= write_offset) 491/* 492 * the earlier object did not fit the limit; avoid 493 * mistaking this with unlimited (i.e. limit = 0). 494 */ 495 limit =1; 496else 497 limit = pack_size_limit - write_offset; 498 499if(!DELTA(entry)) 500 usable_delta =0;/* no delta */ 501else if(!pack_size_limit) 502 usable_delta =1;/* unlimited packfile */ 503else if(DELTA(entry)->idx.offset == (off_t)-1) 504 usable_delta =0;/* base was written to another pack */ 505else if(DELTA(entry)->idx.offset) 506 usable_delta =1;/* base already exists in this pack */ 507else 508 usable_delta =0;/* base could end up in another pack */ 509 510if(!reuse_object) 511 to_reuse =0;/* explicit */ 512else if(!IN_PACK(entry)) 513 to_reuse =0;/* can't reuse what we don't have */ 514else if(oe_type(entry) == OBJ_REF_DELTA || 515oe_type(entry) == OBJ_OFS_DELTA) 516/* check_object() decided it for us ... */ 517 to_reuse = usable_delta; 518/* ... but pack split may override that */ 519else if(oe_type(entry) != entry->in_pack_type) 520 to_reuse =0;/* pack has delta which is unusable */ 521else if(DELTA(entry)) 522 to_reuse =0;/* we want to pack afresh */ 523else 524 to_reuse =1;/* we have it in-pack undeltified, 525 * and we do not need to deltify it. 526 */ 527 528if(!to_reuse) 529 len =write_no_reuse_object(f, entry, limit, usable_delta); 530else 531 len =write_reuse_object(f, entry, limit, usable_delta); 532if(!len) 533return0; 534 535if(usable_delta) 536 written_delta++; 537 written++; 538if(!pack_to_stdout) 539 entry->idx.crc32 =crc32_end(f); 540return len; 541} 542 543enum write_one_status { 544 WRITE_ONE_SKIP = -1,/* already written */ 545 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 546 WRITE_ONE_WRITTEN =1,/* normal */ 547 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 548}; 549 550static enum write_one_status write_one(struct hashfile *f, 551struct object_entry *e, 552 off_t *offset) 553{ 554 off_t size; 555int recursing; 556 557/* 558 * we set offset to 1 (which is an impossible value) to mark 559 * the fact that this object is involved in "write its base 560 * first before writing a deltified object" recursion. 561 */ 562 recursing = (e->idx.offset ==1); 563if(recursing) { 564warning(_("recursive delta detected for object%s"), 565oid_to_hex(&e->idx.oid)); 566return WRITE_ONE_RECURSIVE; 567}else if(e->idx.offset || e->preferred_base) { 568/* offset is non zero if object is written already. */ 569return WRITE_ONE_SKIP; 570} 571 572/* if we are deltified, write out base object first. */ 573if(DELTA(e)) { 574 e->idx.offset =1;/* now recurse */ 575switch(write_one(f,DELTA(e), offset)) { 576case WRITE_ONE_RECURSIVE: 577/* we cannot depend on this one */ 578SET_DELTA(e, NULL); 579break; 580default: 581break; 582case WRITE_ONE_BREAK: 583 e->idx.offset = recursing; 584return WRITE_ONE_BREAK; 585} 586} 587 588 e->idx.offset = *offset; 589 size =write_object(f, e, *offset); 590if(!size) { 591 e->idx.offset = recursing; 592return WRITE_ONE_BREAK; 593} 594 written_list[nr_written++] = &e->idx; 595 596/* make sure off_t is sufficiently large not to wrap */ 597if(signed_add_overflows(*offset, size)) 598die(_("pack too large for current definition of off_t")); 599*offset += size; 600return WRITE_ONE_WRITTEN; 601} 602 603static intmark_tagged(const char*path,const struct object_id *oid,int flag, 604void*cb_data) 605{ 606struct object_id peeled; 607struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 608 609if(entry) 610 entry->tagged =1; 611if(!peel_ref(path, &peeled)) { 612 entry =packlist_find(&to_pack, peeled.hash, NULL); 613if(entry) 614 entry->tagged =1; 615} 616return0; 617} 618 619staticinlinevoidadd_to_write_order(struct object_entry **wo, 620unsigned int*endp, 621struct object_entry *e) 622{ 623if(e->filled ||oe_layer(&to_pack, e) != write_layer) 624return; 625 wo[(*endp)++] = e; 626 e->filled =1; 627} 628 629static voidadd_descendants_to_write_order(struct object_entry **wo, 630unsigned int*endp, 631struct object_entry *e) 632{ 633int add_to_order =1; 634while(e) { 635if(add_to_order) { 636struct object_entry *s; 637/* add this node... */ 638add_to_write_order(wo, endp, e); 639/* all its siblings... */ 640for(s =DELTA_SIBLING(e); s; s =DELTA_SIBLING(s)) { 641add_to_write_order(wo, endp, s); 642} 643} 644/* drop down a level to add left subtree nodes if possible */ 645if(DELTA_CHILD(e)) { 646 add_to_order =1; 647 e =DELTA_CHILD(e); 648}else{ 649 add_to_order =0; 650/* our sibling might have some children, it is next */ 651if(DELTA_SIBLING(e)) { 652 e =DELTA_SIBLING(e); 653continue; 654} 655/* go back to our parent node */ 656 e =DELTA(e); 657while(e && !DELTA_SIBLING(e)) { 658/* we're on the right side of a subtree, keep 659 * going up until we can go right again */ 660 e =DELTA(e); 661} 662if(!e) { 663/* done- we hit our original root node */ 664return; 665} 666/* pass it off to sibling at this level */ 667 e =DELTA_SIBLING(e); 668} 669}; 670} 671 672static voidadd_family_to_write_order(struct object_entry **wo, 673unsigned int*endp, 674struct object_entry *e) 675{ 676struct object_entry *root; 677 678for(root = e;DELTA(root); root =DELTA(root)) 679;/* nothing */ 680add_descendants_to_write_order(wo, endp, root); 681} 682 683static voidcompute_layer_order(struct object_entry **wo,unsigned int*wo_end) 684{ 685unsigned int i, last_untagged; 686struct object_entry *objects = to_pack.objects; 687 688for(i =0; i < to_pack.nr_objects; i++) { 689if(objects[i].tagged) 690break; 691add_to_write_order(wo, wo_end, &objects[i]); 692} 693 last_untagged = i; 694 695/* 696 * Then fill all the tagged tips. 697 */ 698for(; i < to_pack.nr_objects; i++) { 699if(objects[i].tagged) 700add_to_write_order(wo, wo_end, &objects[i]); 701} 702 703/* 704 * And then all remaining commits and tags. 705 */ 706for(i = last_untagged; i < to_pack.nr_objects; i++) { 707if(oe_type(&objects[i]) != OBJ_COMMIT && 708oe_type(&objects[i]) != OBJ_TAG) 709continue; 710add_to_write_order(wo, wo_end, &objects[i]); 711} 712 713/* 714 * And then all the trees. 715 */ 716for(i = last_untagged; i < to_pack.nr_objects; i++) { 717if(oe_type(&objects[i]) != OBJ_TREE) 718continue; 719add_to_write_order(wo, wo_end, &objects[i]); 720} 721 722/* 723 * Finally all the rest in really tight order 724 */ 725for(i = last_untagged; i < to_pack.nr_objects; i++) { 726if(!objects[i].filled &&oe_layer(&to_pack, &objects[i]) == write_layer) 727add_family_to_write_order(wo, wo_end, &objects[i]); 728} 729} 730 731static struct object_entry **compute_write_order(void) 732{ 733uint32_t max_layers =1; 734unsigned int i, wo_end; 735 736struct object_entry **wo; 737struct object_entry *objects = to_pack.objects; 738 739for(i =0; i < to_pack.nr_objects; i++) { 740 objects[i].tagged =0; 741 objects[i].filled =0; 742SET_DELTA_CHILD(&objects[i], NULL); 743SET_DELTA_SIBLING(&objects[i], NULL); 744} 745 746/* 747 * Fully connect delta_child/delta_sibling network. 748 * Make sure delta_sibling is sorted in the original 749 * recency order. 750 */ 751for(i = to_pack.nr_objects; i >0;) { 752struct object_entry *e = &objects[--i]; 753if(!DELTA(e)) 754continue; 755/* Mark me as the first child */ 756 e->delta_sibling_idx =DELTA(e)->delta_child_idx; 757SET_DELTA_CHILD(DELTA(e), e); 758} 759 760/* 761 * Mark objects that are at the tip of tags. 762 */ 763for_each_tag_ref(mark_tagged, NULL); 764 765if(use_delta_islands) 766 max_layers =compute_pack_layers(&to_pack); 767 768ALLOC_ARRAY(wo, to_pack.nr_objects); 769 wo_end =0; 770 771for(; write_layer < max_layers; ++write_layer) 772compute_layer_order(wo, &wo_end); 773 774if(wo_end != to_pack.nr_objects) 775die(_("ordered%uobjects, expected %"PRIu32), 776 wo_end, to_pack.nr_objects); 777 778return wo; 779} 780 781static off_t write_reused_pack(struct hashfile *f) 782{ 783unsigned char buffer[8192]; 784 off_t to_write, total; 785int fd; 786 787if(!is_pack_valid(reuse_packfile)) 788die(_("packfile is invalid:%s"), reuse_packfile->pack_name); 789 790 fd =git_open(reuse_packfile->pack_name); 791if(fd <0) 792die_errno(_("unable to open packfile for reuse:%s"), 793 reuse_packfile->pack_name); 794 795if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 796die_errno(_("unable to seek in reused packfile")); 797 798if(reuse_packfile_offset <0) 799 reuse_packfile_offset = reuse_packfile->pack_size - the_hash_algo->rawsz; 800 801 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 802 803while(to_write) { 804int read_pack =xread(fd, buffer,sizeof(buffer)); 805 806if(read_pack <=0) 807die_errno(_("unable to read from reused packfile")); 808 809if(read_pack > to_write) 810 read_pack = to_write; 811 812hashwrite(f, buffer, read_pack); 813 to_write -= read_pack; 814 815/* 816 * We don't know the actual number of objects written, 817 * only how many bytes written, how many bytes total, and 818 * how many objects total. So we can fake it by pretending all 819 * objects we are writing are the same size. This gives us a 820 * smooth progress meter, and at the end it matches the true 821 * answer. 822 */ 823 written = reuse_packfile_objects * 824(((double)(total - to_write)) / total); 825display_progress(progress_state, written); 826} 827 828close(fd); 829 written = reuse_packfile_objects; 830display_progress(progress_state, written); 831return reuse_packfile_offset -sizeof(struct pack_header); 832} 833 834static const char no_split_warning[] =N_( 835"disabling bitmap writing, packs are split due to pack.packSizeLimit" 836); 837 838static voidwrite_pack_file(void) 839{ 840uint32_t i =0, j; 841struct hashfile *f; 842 off_t offset; 843uint32_t nr_remaining = nr_result; 844time_t last_mtime =0; 845struct object_entry **write_order; 846 847if(progress > pack_to_stdout) 848 progress_state =start_progress(_("Writing objects"), nr_result); 849ALLOC_ARRAY(written_list, to_pack.nr_objects); 850 write_order =compute_write_order(); 851 852do{ 853struct object_id oid; 854char*pack_tmp_name = NULL; 855 856if(pack_to_stdout) 857 f =hashfd_throughput(1,"<stdout>", progress_state); 858else 859 f =create_tmp_packfile(&pack_tmp_name); 860 861 offset =write_pack_header(f, nr_remaining); 862 863if(reuse_packfile) { 864 off_t packfile_size; 865assert(pack_to_stdout); 866 867 packfile_size =write_reused_pack(f); 868 offset += packfile_size; 869} 870 871 nr_written =0; 872for(; i < to_pack.nr_objects; i++) { 873struct object_entry *e = write_order[i]; 874if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 875break; 876display_progress(progress_state, written); 877} 878 879/* 880 * Did we write the wrong # entries in the header? 881 * If so, rewrite it like in fast-import 882 */ 883if(pack_to_stdout) { 884finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 885}else if(nr_written == nr_remaining) { 886finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 887}else{ 888int fd =finalize_hashfile(f, oid.hash,0); 889fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 890 nr_written, oid.hash, offset); 891close(fd); 892if(write_bitmap_index) { 893warning(_(no_split_warning)); 894 write_bitmap_index =0; 895} 896} 897 898if(!pack_to_stdout) { 899struct stat st; 900struct strbuf tmpname = STRBUF_INIT; 901 902/* 903 * Packs are runtime accessed in their mtime 904 * order since newer packs are more likely to contain 905 * younger objects. So if we are creating multiple 906 * packs then we should modify the mtime of later ones 907 * to preserve this property. 908 */ 909if(stat(pack_tmp_name, &st) <0) { 910warning_errno(_("failed to stat%s"), pack_tmp_name); 911}else if(!last_mtime) { 912 last_mtime = st.st_mtime; 913}else{ 914struct utimbuf utb; 915 utb.actime = st.st_atime; 916 utb.modtime = --last_mtime; 917if(utime(pack_tmp_name, &utb) <0) 918warning_errno(_("failed utime() on%s"), pack_tmp_name); 919} 920 921strbuf_addf(&tmpname,"%s-", base_name); 922 923if(write_bitmap_index) { 924bitmap_writer_set_checksum(oid.hash); 925bitmap_writer_build_type_index( 926&to_pack, written_list, nr_written); 927} 928 929finish_tmp_packfile(&tmpname, pack_tmp_name, 930 written_list, nr_written, 931&pack_idx_opts, oid.hash); 932 933if(write_bitmap_index) { 934strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 935 936stop_progress(&progress_state); 937 938bitmap_writer_show_progress(progress); 939bitmap_writer_reuse_bitmaps(&to_pack); 940bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 941bitmap_writer_build(&to_pack); 942bitmap_writer_finish(written_list, nr_written, 943 tmpname.buf, write_bitmap_options); 944 write_bitmap_index =0; 945} 946 947strbuf_release(&tmpname); 948free(pack_tmp_name); 949puts(oid_to_hex(&oid)); 950} 951 952/* mark written objects as written to previous pack */ 953for(j =0; j < nr_written; j++) { 954 written_list[j]->offset = (off_t)-1; 955} 956 nr_remaining -= nr_written; 957}while(nr_remaining && i < to_pack.nr_objects); 958 959free(written_list); 960free(write_order); 961stop_progress(&progress_state); 962if(written != nr_result) 963die(_("wrote %"PRIu32" objects while expecting %"PRIu32), 964 written, nr_result); 965} 966 967static intno_try_delta(const char*path) 968{ 969static struct attr_check *check; 970 971if(!check) 972 check =attr_check_initl("delta", NULL); 973if(git_check_attr(&the_index, path, check)) 974return0; 975if(ATTR_FALSE(check->items[0].value)) 976return1; 977return0; 978} 979 980/* 981 * When adding an object, check whether we have already added it 982 * to our packing list. If so, we can skip. However, if we are 983 * being asked to excludei t, but the previous mention was to include 984 * it, make sure to adjust its flags and tweak our numbers accordingly. 985 * 986 * As an optimization, we pass out the index position where we would have 987 * found the item, since that saves us from having to look it up again a 988 * few lines later when we want to add the new entry. 989 */ 990static inthave_duplicate_entry(const struct object_id *oid, 991int exclude, 992uint32_t*index_pos) 993{ 994struct object_entry *entry; 995 996 entry =packlist_find(&to_pack, oid->hash, index_pos); 997if(!entry) 998return0; 9991000if(exclude) {1001if(!entry->preferred_base)1002 nr_result--;1003 entry->preferred_base =1;1004}10051006return1;1007}10081009static intwant_found_object(int exclude,struct packed_git *p)1010{1011if(exclude)1012return1;1013if(incremental)1014return0;10151016/*1017 * When asked to do --local (do not include an object that appears in a1018 * pack we borrow from elsewhere) or --honor-pack-keep (do not include1019 * an object that appears in a pack marked with .keep), finding a pack1020 * that matches the criteria is sufficient for us to decide to omit it.1021 * However, even if this pack does not satisfy the criteria, we need to1022 * make sure no copy of this object appears in _any_ pack that makes us1023 * to omit the object, so we need to check all the packs.1024 *1025 * We can however first check whether these options can possible matter;1026 * if they do not matter we know we want the object in generated pack.1027 * Otherwise, we signal "-1" at the end to tell the caller that we do1028 * not know either way, and it needs to check more packs.1029 */1030if(!ignore_packed_keep_on_disk &&1031!ignore_packed_keep_in_core &&1032(!local || !have_non_local_packs))1033return1;10341035if(local && !p->pack_local)1036return0;1037if(p->pack_local &&1038((ignore_packed_keep_on_disk && p->pack_keep) ||1039(ignore_packed_keep_in_core && p->pack_keep_in_core)))1040return0;10411042/* we don't know yet; keep looking for more packs */1043return-1;1044}10451046/*1047 * Check whether we want the object in the pack (e.g., we do not want1048 * objects found in non-local stores if the "--local" option was used).1049 *1050 * If the caller already knows an existing pack it wants to take the object1051 * from, that is passed in *found_pack and *found_offset; otherwise this1052 * function finds if there is any pack that has the object and returns the pack1053 * and its offset in these variables.1054 */1055static intwant_object_in_pack(const struct object_id *oid,1056int exclude,1057struct packed_git **found_pack,1058 off_t *found_offset)1059{1060int want;1061struct list_head *pos;1062struct multi_pack_index *m;10631064if(!exclude && local &&has_loose_object_nonlocal(oid))1065return0;10661067/*1068 * If we already know the pack object lives in, start checks from that1069 * pack - in the usual case when neither --local was given nor .keep files1070 * are present we will determine the answer right now.1071 */1072if(*found_pack) {1073 want =want_found_object(exclude, *found_pack);1074if(want != -1)1075return want;1076}10771078for(m =get_multi_pack_index(the_repository); m; m = m->next) {1079struct pack_entry e;1080if(fill_midx_entry(oid, &e, m)) {1081struct packed_git *p = e.p;1082 off_t offset;10831084if(p == *found_pack)1085 offset = *found_offset;1086else1087 offset =find_pack_entry_one(oid->hash, p);10881089if(offset) {1090if(!*found_pack) {1091if(!is_pack_valid(p))1092continue;1093*found_offset = offset;1094*found_pack = p;1095}1096 want =want_found_object(exclude, p);1097if(want != -1)1098return want;1099}1100}1101}11021103list_for_each(pos,get_packed_git_mru(the_repository)) {1104struct packed_git *p =list_entry(pos,struct packed_git, mru);1105 off_t offset;11061107if(p == *found_pack)1108 offset = *found_offset;1109else1110 offset =find_pack_entry_one(oid->hash, p);11111112if(offset) {1113if(!*found_pack) {1114if(!is_pack_valid(p))1115continue;1116*found_offset = offset;1117*found_pack = p;1118}1119 want =want_found_object(exclude, p);1120if(!exclude && want >0)1121list_move(&p->mru,1122get_packed_git_mru(the_repository));1123if(want != -1)1124return want;1125}1126}11271128return1;1129}11301131static voidcreate_object_entry(const struct object_id *oid,1132enum object_type type,1133uint32_t hash,1134int exclude,1135int no_try_delta,1136uint32_t index_pos,1137struct packed_git *found_pack,1138 off_t found_offset)1139{1140struct object_entry *entry;11411142 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1143 entry->hash = hash;1144oe_set_type(entry, type);1145if(exclude)1146 entry->preferred_base =1;1147else1148 nr_result++;1149if(found_pack) {1150oe_set_in_pack(&to_pack, entry, found_pack);1151 entry->in_pack_offset = found_offset;1152}11531154 entry->no_try_delta = no_try_delta;1155}11561157static const char no_closure_warning[] =N_(1158"disabling bitmap writing, as some objects are not being packed"1159);11601161static intadd_object_entry(const struct object_id *oid,enum object_type type,1162const char*name,int exclude)1163{1164struct packed_git *found_pack = NULL;1165 off_t found_offset =0;1166uint32_t index_pos;11671168display_progress(progress_state, ++nr_seen);11691170if(have_duplicate_entry(oid, exclude, &index_pos))1171return0;11721173if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1174/* The pack is missing an object, so it will not have closure */1175if(write_bitmap_index) {1176warning(_(no_closure_warning));1177 write_bitmap_index =0;1178}1179return0;1180}11811182create_object_entry(oid, type,pack_name_hash(name),1183 exclude, name &&no_try_delta(name),1184 index_pos, found_pack, found_offset);1185return1;1186}11871188static intadd_object_entry_from_bitmap(const struct object_id *oid,1189enum object_type type,1190int flags,uint32_t name_hash,1191struct packed_git *pack, off_t offset)1192{1193uint32_t index_pos;11941195display_progress(progress_state, ++nr_seen);11961197if(have_duplicate_entry(oid,0, &index_pos))1198return0;11991200if(!want_object_in_pack(oid,0, &pack, &offset))1201return0;12021203create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1204return1;1205}12061207struct pbase_tree_cache {1208struct object_id oid;1209int ref;1210int temporary;1211void*tree_data;1212unsigned long tree_size;1213};12141215static struct pbase_tree_cache *(pbase_tree_cache[256]);1216static intpbase_tree_cache_ix(const struct object_id *oid)1217{1218return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1219}1220static intpbase_tree_cache_ix_incr(int ix)1221{1222return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1223}12241225static struct pbase_tree {1226struct pbase_tree *next;1227/* This is a phony "cache" entry; we are not1228 * going to evict it or find it through _get()1229 * mechanism -- this is for the toplevel node that1230 * would almost always change with any commit.1231 */1232struct pbase_tree_cache pcache;1233} *pbase_tree;12341235static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1236{1237struct pbase_tree_cache *ent, *nent;1238void*data;1239unsigned long size;1240enum object_type type;1241int neigh;1242int my_ix =pbase_tree_cache_ix(oid);1243int available_ix = -1;12441245/* pbase-tree-cache acts as a limited hashtable.1246 * your object will be found at your index or within a few1247 * slots after that slot if it is cached.1248 */1249for(neigh =0; neigh <8; neigh++) {1250 ent = pbase_tree_cache[my_ix];1251if(ent &&oideq(&ent->oid, oid)) {1252 ent->ref++;1253return ent;1254}1255else if(((available_ix <0) && (!ent || !ent->ref)) ||1256((0<= available_ix) &&1257(!ent && pbase_tree_cache[available_ix])))1258 available_ix = my_ix;1259if(!ent)1260break;1261 my_ix =pbase_tree_cache_ix_incr(my_ix);1262}12631264/* Did not find one. Either we got a bogus request or1265 * we need to read and perhaps cache.1266 */1267 data =read_object_file(oid, &type, &size);1268if(!data)1269return NULL;1270if(type != OBJ_TREE) {1271free(data);1272return NULL;1273}12741275/* We need to either cache or return a throwaway copy */12761277if(available_ix <0)1278 ent = NULL;1279else{1280 ent = pbase_tree_cache[available_ix];1281 my_ix = available_ix;1282}12831284if(!ent) {1285 nent =xmalloc(sizeof(*nent));1286 nent->temporary = (available_ix <0);1287}1288else{1289/* evict and reuse */1290free(ent->tree_data);1291 nent = ent;1292}1293oidcpy(&nent->oid, oid);1294 nent->tree_data = data;1295 nent->tree_size = size;1296 nent->ref =1;1297if(!nent->temporary)1298 pbase_tree_cache[my_ix] = nent;1299return nent;1300}13011302static voidpbase_tree_put(struct pbase_tree_cache *cache)1303{1304if(!cache->temporary) {1305 cache->ref--;1306return;1307}1308free(cache->tree_data);1309free(cache);1310}13111312static intname_cmp_len(const char*name)1313{1314int i;1315for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1316;1317return i;1318}13191320static voidadd_pbase_object(struct tree_desc *tree,1321const char*name,1322int cmplen,1323const char*fullname)1324{1325struct name_entry entry;1326int cmp;13271328while(tree_entry(tree,&entry)) {1329if(S_ISGITLINK(entry.mode))1330continue;1331 cmp =tree_entry_len(&entry) != cmplen ?1:1332memcmp(name, entry.path, cmplen);1333if(cmp >0)1334continue;1335if(cmp <0)1336return;1337if(name[cmplen] !='/') {1338add_object_entry(entry.oid,1339object_type(entry.mode),1340 fullname,1);1341return;1342}1343if(S_ISDIR(entry.mode)) {1344struct tree_desc sub;1345struct pbase_tree_cache *tree;1346const char*down = name+cmplen+1;1347int downlen =name_cmp_len(down);13481349 tree =pbase_tree_get(entry.oid);1350if(!tree)1351return;1352init_tree_desc(&sub, tree->tree_data, tree->tree_size);13531354add_pbase_object(&sub, down, downlen, fullname);1355pbase_tree_put(tree);1356}1357}1358}13591360static unsigned*done_pbase_paths;1361static int done_pbase_paths_num;1362static int done_pbase_paths_alloc;1363static intdone_pbase_path_pos(unsigned hash)1364{1365int lo =0;1366int hi = done_pbase_paths_num;1367while(lo < hi) {1368int mi = lo + (hi - lo) /2;1369if(done_pbase_paths[mi] == hash)1370return mi;1371if(done_pbase_paths[mi] < hash)1372 hi = mi;1373else1374 lo = mi +1;1375}1376return-lo-1;1377}13781379static intcheck_pbase_path(unsigned hash)1380{1381int pos =done_pbase_path_pos(hash);1382if(0<= pos)1383return1;1384 pos = -pos -1;1385ALLOC_GROW(done_pbase_paths,1386 done_pbase_paths_num +1,1387 done_pbase_paths_alloc);1388 done_pbase_paths_num++;1389if(pos < done_pbase_paths_num)1390MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1391 done_pbase_paths_num - pos -1);1392 done_pbase_paths[pos] = hash;1393return0;1394}13951396static voidadd_preferred_base_object(const char*name)1397{1398struct pbase_tree *it;1399int cmplen;1400unsigned hash =pack_name_hash(name);14011402if(!num_preferred_base ||check_pbase_path(hash))1403return;14041405 cmplen =name_cmp_len(name);1406for(it = pbase_tree; it; it = it->next) {1407if(cmplen ==0) {1408add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1409}1410else{1411struct tree_desc tree;1412init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1413add_pbase_object(&tree, name, cmplen, name);1414}1415}1416}14171418static voidadd_preferred_base(struct object_id *oid)1419{1420struct pbase_tree *it;1421void*data;1422unsigned long size;1423struct object_id tree_oid;14241425if(window <= num_preferred_base++)1426return;14271428 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1429if(!data)1430return;14311432for(it = pbase_tree; it; it = it->next) {1433if(oideq(&it->pcache.oid, &tree_oid)) {1434free(data);1435return;1436}1437}14381439 it =xcalloc(1,sizeof(*it));1440 it->next = pbase_tree;1441 pbase_tree = it;14421443oidcpy(&it->pcache.oid, &tree_oid);1444 it->pcache.tree_data = data;1445 it->pcache.tree_size = size;1446}14471448static voidcleanup_preferred_base(void)1449{1450struct pbase_tree *it;1451unsigned i;14521453 it = pbase_tree;1454 pbase_tree = NULL;1455while(it) {1456struct pbase_tree *tmp = it;1457 it = tmp->next;1458free(tmp->pcache.tree_data);1459free(tmp);1460}14611462for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1463if(!pbase_tree_cache[i])1464continue;1465free(pbase_tree_cache[i]->tree_data);1466FREE_AND_NULL(pbase_tree_cache[i]);1467}14681469FREE_AND_NULL(done_pbase_paths);1470 done_pbase_paths_num = done_pbase_paths_alloc =0;1471}14721473static voidcheck_object(struct object_entry *entry)1474{1475unsigned long canonical_size;14761477if(IN_PACK(entry)) {1478struct packed_git *p =IN_PACK(entry);1479struct pack_window *w_curs = NULL;1480const unsigned char*base_ref = NULL;1481struct object_entry *base_entry;1482unsigned long used, used_0;1483unsigned long avail;1484 off_t ofs;1485unsigned char*buf, c;1486enum object_type type;1487unsigned long in_pack_size;14881489 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14901491/*1492 * We want in_pack_type even if we do not reuse delta1493 * since non-delta representations could still be reused.1494 */1495 used =unpack_object_header_buffer(buf, avail,1496&type,1497&in_pack_size);1498if(used ==0)1499goto give_up;15001501if(type <0)1502BUG("invalid type%d", type);1503 entry->in_pack_type = type;15041505/*1506 * Determine if this is a delta and if so whether we can1507 * reuse it or not. Otherwise let's find out as cheaply as1508 * possible what the actual type and size for this object is.1509 */1510switch(entry->in_pack_type) {1511default:1512/* Not a delta hence we've already got all we need. */1513oe_set_type(entry, entry->in_pack_type);1514SET_SIZE(entry, in_pack_size);1515 entry->in_pack_header_size = used;1516if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1517goto give_up;1518unuse_pack(&w_curs);1519return;1520case OBJ_REF_DELTA:1521if(reuse_delta && !entry->preferred_base)1522 base_ref =use_pack(p, &w_curs,1523 entry->in_pack_offset + used, NULL);1524 entry->in_pack_header_size = used + the_hash_algo->rawsz;1525break;1526case OBJ_OFS_DELTA:1527 buf =use_pack(p, &w_curs,1528 entry->in_pack_offset + used, NULL);1529 used_0 =0;1530 c = buf[used_0++];1531 ofs = c &127;1532while(c &128) {1533 ofs +=1;1534if(!ofs ||MSB(ofs,7)) {1535error(_("delta base offset overflow in pack for%s"),1536oid_to_hex(&entry->idx.oid));1537goto give_up;1538}1539 c = buf[used_0++];1540 ofs = (ofs <<7) + (c &127);1541}1542 ofs = entry->in_pack_offset - ofs;1543if(ofs <=0|| ofs >= entry->in_pack_offset) {1544error(_("delta base offset out of bound for%s"),1545oid_to_hex(&entry->idx.oid));1546goto give_up;1547}1548if(reuse_delta && !entry->preferred_base) {1549struct revindex_entry *revidx;1550 revidx =find_pack_revindex(p, ofs);1551if(!revidx)1552goto give_up;1553 base_ref =nth_packed_object_sha1(p, revidx->nr);1554}1555 entry->in_pack_header_size = used + used_0;1556break;1557}15581559if(base_ref && (1560(base_entry =packlist_find(&to_pack, base_ref, NULL)) ||1561(thin &&1562bitmap_has_sha1_in_uninteresting(bitmap_git, base_ref))) &&1563in_same_island(&entry->idx.oid, &base_entry->idx.oid)) {1564/*1565 * If base_ref was set above that means we wish to1566 * reuse delta data, and either we found that object in1567 * the list of objects we want to pack, or it's one we1568 * know the receiver has.1569 *1570 * Depth value does not matter - find_deltas() will1571 * never consider reused delta as the base object to1572 * deltify other objects against, in order to avoid1573 * circular deltas.1574 */1575oe_set_type(entry, entry->in_pack_type);1576SET_SIZE(entry, in_pack_size);/* delta size */1577SET_DELTA_SIZE(entry, in_pack_size);15781579if(base_entry) {1580SET_DELTA(entry, base_entry);1581 entry->delta_sibling_idx = base_entry->delta_child_idx;1582SET_DELTA_CHILD(base_entry, entry);1583}else{1584SET_DELTA_EXT(entry, base_ref);1585}15861587unuse_pack(&w_curs);1588return;1589}15901591if(oe_type(entry)) {1592 off_t delta_pos;15931594/*1595 * This must be a delta and we already know what the1596 * final object type is. Let's extract the actual1597 * object size from the delta header.1598 */1599 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1600 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1601if(canonical_size ==0)1602goto give_up;1603SET_SIZE(entry, canonical_size);1604unuse_pack(&w_curs);1605return;1606}16071608/*1609 * No choice but to fall back to the recursive delta walk1610 * with sha1_object_info() to find about the object type1611 * at this point...1612 */1613 give_up:1614unuse_pack(&w_curs);1615}16161617oe_set_type(entry,1618oid_object_info(the_repository, &entry->idx.oid, &canonical_size));1619if(entry->type_valid) {1620SET_SIZE(entry, canonical_size);1621}else{1622/*1623 * Bad object type is checked in prepare_pack(). This is1624 * to permit a missing preferred base object to be ignored1625 * as a preferred base. Doing so can result in a larger1626 * pack file, but the transfer will still take place.1627 */1628}1629}16301631static intpack_offset_sort(const void*_a,const void*_b)1632{1633const struct object_entry *a = *(struct object_entry **)_a;1634const struct object_entry *b = *(struct object_entry **)_b;1635const struct packed_git *a_in_pack =IN_PACK(a);1636const struct packed_git *b_in_pack =IN_PACK(b);16371638/* avoid filesystem trashing with loose objects */1639if(!a_in_pack && !b_in_pack)1640returnoidcmp(&a->idx.oid, &b->idx.oid);16411642if(a_in_pack < b_in_pack)1643return-1;1644if(a_in_pack > b_in_pack)1645return1;1646return a->in_pack_offset < b->in_pack_offset ? -1:1647(a->in_pack_offset > b->in_pack_offset);1648}16491650/*1651 * Drop an on-disk delta we were planning to reuse. Naively, this would1652 * just involve blanking out the "delta" field, but we have to deal1653 * with some extra book-keeping:1654 *1655 * 1. Removing ourselves from the delta_sibling linked list.1656 *1657 * 2. Updating our size/type to the non-delta representation. These were1658 * either not recorded initially (size) or overwritten with the delta type1659 * (type) when check_object() decided to reuse the delta.1660 *1661 * 3. Resetting our delta depth, as we are now a base object.1662 */1663static voiddrop_reused_delta(struct object_entry *entry)1664{1665unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1666struct object_info oi = OBJECT_INFO_INIT;1667enum object_type type;1668unsigned long size;16691670while(*idx) {1671struct object_entry *oe = &to_pack.objects[*idx -1];16721673if(oe == entry)1674*idx = oe->delta_sibling_idx;1675else1676 idx = &oe->delta_sibling_idx;1677}1678SET_DELTA(entry, NULL);1679 entry->depth =0;16801681 oi.sizep = &size;1682 oi.typep = &type;1683if(packed_object_info(the_repository,IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1684/*1685 * We failed to get the info from this pack for some reason;1686 * fall back to sha1_object_info, which may find another copy.1687 * And if that fails, the error will be recorded in oe_type(entry)1688 * and dealt with in prepare_pack().1689 */1690oe_set_type(entry,1691oid_object_info(the_repository, &entry->idx.oid, &size));1692}else{1693oe_set_type(entry, type);1694}1695SET_SIZE(entry, size);1696}16971698/*1699 * Follow the chain of deltas from this entry onward, throwing away any links1700 * that cause us to hit a cycle (as determined by the DFS state flags in1701 * the entries).1702 *1703 * We also detect too-long reused chains that would violate our --depth1704 * limit.1705 */1706static voidbreak_delta_chains(struct object_entry *entry)1707{1708/*1709 * The actual depth of each object we will write is stored as an int,1710 * as it cannot exceed our int "depth" limit. But before we break1711 * changes based no that limit, we may potentially go as deep as the1712 * number of objects, which is elsewhere bounded to a uint32_t.1713 */1714uint32_t total_depth;1715struct object_entry *cur, *next;17161717for(cur = entry, total_depth =0;1718 cur;1719 cur =DELTA(cur), total_depth++) {1720if(cur->dfs_state == DFS_DONE) {1721/*1722 * We've already seen this object and know it isn't1723 * part of a cycle. We do need to append its depth1724 * to our count.1725 */1726 total_depth += cur->depth;1727break;1728}17291730/*1731 * We break cycles before looping, so an ACTIVE state (or any1732 * other cruft which made its way into the state variable)1733 * is a bug.1734 */1735if(cur->dfs_state != DFS_NONE)1736BUG("confusing delta dfs state in first pass:%d",1737 cur->dfs_state);17381739/*1740 * Now we know this is the first time we've seen the object. If1741 * it's not a delta, we're done traversing, but we'll mark it1742 * done to save time on future traversals.1743 */1744if(!DELTA(cur)) {1745 cur->dfs_state = DFS_DONE;1746break;1747}17481749/*1750 * Mark ourselves as active and see if the next step causes1751 * us to cycle to another active object. It's important to do1752 * this _before_ we loop, because it impacts where we make the1753 * cut, and thus how our total_depth counter works.1754 * E.g., We may see a partial loop like:1755 *1756 * A -> B -> C -> D -> B1757 *1758 * Cutting B->C breaks the cycle. But now the depth of A is1759 * only 1, and our total_depth counter is at 3. The size of the1760 * error is always one less than the size of the cycle we1761 * broke. Commits C and D were "lost" from A's chain.1762 *1763 * If we instead cut D->B, then the depth of A is correct at 3.1764 * We keep all commits in the chain that we examined.1765 */1766 cur->dfs_state = DFS_ACTIVE;1767if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1768drop_reused_delta(cur);1769 cur->dfs_state = DFS_DONE;1770break;1771}1772}17731774/*1775 * And now that we've gone all the way to the bottom of the chain, we1776 * need to clear the active flags and set the depth fields as1777 * appropriate. Unlike the loop above, which can quit when it drops a1778 * delta, we need to keep going to look for more depth cuts. So we need1779 * an extra "next" pointer to keep going after we reset cur->delta.1780 */1781for(cur = entry; cur; cur = next) {1782 next =DELTA(cur);17831784/*1785 * We should have a chain of zero or more ACTIVE states down to1786 * a final DONE. We can quit after the DONE, because either it1787 * has no bases, or we've already handled them in a previous1788 * call.1789 */1790if(cur->dfs_state == DFS_DONE)1791break;1792else if(cur->dfs_state != DFS_ACTIVE)1793BUG("confusing delta dfs state in second pass:%d",1794 cur->dfs_state);17951796/*1797 * If the total_depth is more than depth, then we need to snip1798 * the chain into two or more smaller chains that don't exceed1799 * the maximum depth. Most of the resulting chains will contain1800 * (depth + 1) entries (i.e., depth deltas plus one base), and1801 * the last chain (i.e., the one containing entry) will contain1802 * whatever entries are left over, namely1803 * (total_depth % (depth + 1)) of them.1804 *1805 * Since we are iterating towards decreasing depth, we need to1806 * decrement total_depth as we go, and we need to write to the1807 * entry what its final depth will be after all of the1808 * snipping. Since we're snipping into chains of length (depth1809 * + 1) entries, the final depth of an entry will be its1810 * original depth modulo (depth + 1). Any time we encounter an1811 * entry whose final depth is supposed to be zero, we snip it1812 * from its delta base, thereby making it so.1813 */1814 cur->depth = (total_depth--) % (depth +1);1815if(!cur->depth)1816drop_reused_delta(cur);18171818 cur->dfs_state = DFS_DONE;1819}1820}18211822static voidget_object_details(void)1823{1824uint32_t i;1825struct object_entry **sorted_by_offset;18261827if(progress)1828 progress_state =start_progress(_("Counting objects"),1829 to_pack.nr_objects);18301831 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1832for(i =0; i < to_pack.nr_objects; i++)1833 sorted_by_offset[i] = to_pack.objects + i;1834QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);18351836for(i =0; i < to_pack.nr_objects; i++) {1837struct object_entry *entry = sorted_by_offset[i];1838check_object(entry);1839if(entry->type_valid &&1840oe_size_greater_than(&to_pack, entry, big_file_threshold))1841 entry->no_try_delta =1;1842display_progress(progress_state, i +1);1843}1844stop_progress(&progress_state);18451846/*1847 * This must happen in a second pass, since we rely on the delta1848 * information for the whole list being completed.1849 */1850for(i =0; i < to_pack.nr_objects; i++)1851break_delta_chains(&to_pack.objects[i]);18521853free(sorted_by_offset);1854}18551856/*1857 * We search for deltas in a list sorted by type, by filename hash, and then1858 * by size, so that we see progressively smaller and smaller files.1859 * That's because we prefer deltas to be from the bigger file1860 * to the smaller -- deletes are potentially cheaper, but perhaps1861 * more importantly, the bigger file is likely the more recent1862 * one. The deepest deltas are therefore the oldest objects which are1863 * less susceptible to be accessed often.1864 */1865static inttype_size_sort(const void*_a,const void*_b)1866{1867const struct object_entry *a = *(struct object_entry **)_a;1868const struct object_entry *b = *(struct object_entry **)_b;1869enum object_type a_type =oe_type(a);1870enum object_type b_type =oe_type(b);1871unsigned long a_size =SIZE(a);1872unsigned long b_size =SIZE(b);18731874if(a_type > b_type)1875return-1;1876if(a_type < b_type)1877return1;1878if(a->hash > b->hash)1879return-1;1880if(a->hash < b->hash)1881return1;1882if(a->preferred_base > b->preferred_base)1883return-1;1884if(a->preferred_base < b->preferred_base)1885return1;1886if(use_delta_islands) {1887int island_cmp =island_delta_cmp(&a->idx.oid, &b->idx.oid);1888if(island_cmp)1889return island_cmp;1890}1891if(a_size > b_size)1892return-1;1893if(a_size < b_size)1894return1;1895return a < b ? -1: (a > b);/* newest first */1896}18971898struct unpacked {1899struct object_entry *entry;1900void*data;1901struct delta_index *index;1902unsigned depth;1903};19041905static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1906unsigned long delta_size)1907{1908if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1909return0;19101911if(delta_size < cache_max_small_delta_size)1912return1;19131914/* cache delta, if objects are large enough compared to delta size */1915if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1916return1;19171918return0;1919}19201921#ifndef NO_PTHREADS19221923/* Protect access to object database */1924static pthread_mutex_t read_mutex;1925#define read_lock() pthread_mutex_lock(&read_mutex)1926#define read_unlock() pthread_mutex_unlock(&read_mutex)19271928/* Protect delta_cache_size */1929static pthread_mutex_t cache_mutex;1930#define cache_lock() pthread_mutex_lock(&cache_mutex)1931#define cache_unlock() pthread_mutex_unlock(&cache_mutex)19321933/*1934 * Protect object list partitioning (e.g. struct thread_param) and1935 * progress_state1936 */1937static pthread_mutex_t progress_mutex;1938#define progress_lock() pthread_mutex_lock(&progress_mutex)1939#define progress_unlock() pthread_mutex_unlock(&progress_mutex)19401941/*1942 * Access to struct object_entry is unprotected since each thread owns1943 * a portion of the main object list. Just don't access object entries1944 * ahead in the list because they can be stolen and would need1945 * progress_mutex for protection.1946 */1947#else19481949#define read_lock() (void)01950#define read_unlock() (void)01951#define cache_lock() (void)01952#define cache_unlock() (void)01953#define progress_lock() (void)01954#define progress_unlock() (void)019551956#endif19571958/*1959 * Return the size of the object without doing any delta1960 * reconstruction (so non-deltas are true object sizes, but deltas1961 * return the size of the delta data).1962 */1963unsigned longoe_get_size_slow(struct packing_data *pack,1964const struct object_entry *e)1965{1966struct packed_git *p;1967struct pack_window *w_curs;1968unsigned char*buf;1969enum object_type type;1970unsigned long used, avail, size;19711972if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1973read_lock();1974if(oid_object_info(the_repository, &e->idx.oid, &size) <0)1975die(_("unable to get size of%s"),1976oid_to_hex(&e->idx.oid));1977read_unlock();1978return size;1979}19801981 p =oe_in_pack(pack, e);1982if(!p)1983BUG("when e->type is a delta, it must belong to a pack");19841985read_lock();1986 w_curs = NULL;1987 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);1988 used =unpack_object_header_buffer(buf, avail, &type, &size);1989if(used ==0)1990die(_("unable to parse object header of%s"),1991oid_to_hex(&e->idx.oid));19921993unuse_pack(&w_curs);1994read_unlock();1995return size;1996}19971998static inttry_delta(struct unpacked *trg,struct unpacked *src,1999unsigned max_depth,unsigned long*mem_usage)2000{2001struct object_entry *trg_entry = trg->entry;2002struct object_entry *src_entry = src->entry;2003unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;2004unsigned ref_depth;2005enum object_type type;2006void*delta_buf;20072008/* Don't bother doing diffs between different types */2009if(oe_type(trg_entry) !=oe_type(src_entry))2010return-1;20112012/*2013 * We do not bother to try a delta that we discarded on an2014 * earlier try, but only when reusing delta data. Note that2015 * src_entry that is marked as the preferred_base should always2016 * be considered, as even if we produce a suboptimal delta against2017 * it, we will still save the transfer cost, as we already know2018 * the other side has it and we won't send src_entry at all.2019 */2020if(reuse_delta &&IN_PACK(trg_entry) &&2021IN_PACK(trg_entry) ==IN_PACK(src_entry) &&2022!src_entry->preferred_base &&2023 trg_entry->in_pack_type != OBJ_REF_DELTA &&2024 trg_entry->in_pack_type != OBJ_OFS_DELTA)2025return0;20262027/* Let's not bust the allowed depth. */2028if(src->depth >= max_depth)2029return0;20302031/* Now some size filtering heuristics. */2032 trg_size =SIZE(trg_entry);2033if(!DELTA(trg_entry)) {2034 max_size = trg_size/2- the_hash_algo->rawsz;2035 ref_depth =1;2036}else{2037 max_size =DELTA_SIZE(trg_entry);2038 ref_depth = trg->depth;2039}2040 max_size = (uint64_t)max_size * (max_depth - src->depth) /2041(max_depth - ref_depth +1);2042if(max_size ==0)2043return0;2044 src_size =SIZE(src_entry);2045 sizediff = src_size < trg_size ? trg_size - src_size :0;2046if(sizediff >= max_size)2047return0;2048if(trg_size < src_size /32)2049return0;20502051if(!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))2052return0;20532054/* Load data if not already done */2055if(!trg->data) {2056read_lock();2057 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);2058read_unlock();2059if(!trg->data)2060die(_("object%scannot be read"),2061oid_to_hex(&trg_entry->idx.oid));2062if(sz != trg_size)2063die(_("object%sinconsistent object length (%lu vs%lu)"),2064oid_to_hex(&trg_entry->idx.oid), sz,2065 trg_size);2066*mem_usage += sz;2067}2068if(!src->data) {2069read_lock();2070 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);2071read_unlock();2072if(!src->data) {2073if(src_entry->preferred_base) {2074static int warned =0;2075if(!warned++)2076warning(_("object%scannot be read"),2077oid_to_hex(&src_entry->idx.oid));2078/*2079 * Those objects are not included in the2080 * resulting pack. Be resilient and ignore2081 * them if they can't be read, in case the2082 * pack could be created nevertheless.2083 */2084return0;2085}2086die(_("object%scannot be read"),2087oid_to_hex(&src_entry->idx.oid));2088}2089if(sz != src_size)2090die(_("object%sinconsistent object length (%lu vs%lu)"),2091oid_to_hex(&src_entry->idx.oid), sz,2092 src_size);2093*mem_usage += sz;2094}2095if(!src->index) {2096 src->index =create_delta_index(src->data, src_size);2097if(!src->index) {2098static int warned =0;2099if(!warned++)2100warning(_("suboptimal pack - out of memory"));2101return0;2102}2103*mem_usage +=sizeof_delta_index(src->index);2104}21052106 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2107if(!delta_buf)2108return0;21092110if(DELTA(trg_entry)) {2111/* Prefer only shallower same-sized deltas. */2112if(delta_size ==DELTA_SIZE(trg_entry) &&2113 src->depth +1>= trg->depth) {2114free(delta_buf);2115return0;2116}2117}21182119/*2120 * Handle memory allocation outside of the cache2121 * accounting lock. Compiler will optimize the strangeness2122 * away when NO_PTHREADS is defined.2123 */2124free(trg_entry->delta_data);2125cache_lock();2126if(trg_entry->delta_data) {2127 delta_cache_size -=DELTA_SIZE(trg_entry);2128 trg_entry->delta_data = NULL;2129}2130if(delta_cacheable(src_size, trg_size, delta_size)) {2131 delta_cache_size += delta_size;2132cache_unlock();2133 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2134}else{2135cache_unlock();2136free(delta_buf);2137}21382139SET_DELTA(trg_entry, src_entry);2140SET_DELTA_SIZE(trg_entry, delta_size);2141 trg->depth = src->depth +1;21422143return1;2144}21452146static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2147{2148struct object_entry *child =DELTA_CHILD(me);2149unsigned int m = n;2150while(child) {2151unsigned int c =check_delta_limit(child, n +1);2152if(m < c)2153 m = c;2154 child =DELTA_SIBLING(child);2155}2156return m;2157}21582159static unsigned longfree_unpacked(struct unpacked *n)2160{2161unsigned long freed_mem =sizeof_delta_index(n->index);2162free_delta_index(n->index);2163 n->index = NULL;2164if(n->data) {2165 freed_mem +=SIZE(n->entry);2166FREE_AND_NULL(n->data);2167}2168 n->entry = NULL;2169 n->depth =0;2170return freed_mem;2171}21722173static voidfind_deltas(struct object_entry **list,unsigned*list_size,2174int window,int depth,unsigned*processed)2175{2176uint32_t i, idx =0, count =0;2177struct unpacked *array;2178unsigned long mem_usage =0;21792180 array =xcalloc(window,sizeof(struct unpacked));21812182for(;;) {2183struct object_entry *entry;2184struct unpacked *n = array + idx;2185int j, max_depth, best_base = -1;21862187progress_lock();2188if(!*list_size) {2189progress_unlock();2190break;2191}2192 entry = *list++;2193(*list_size)--;2194if(!entry->preferred_base) {2195(*processed)++;2196display_progress(progress_state, *processed);2197}2198progress_unlock();21992200 mem_usage -=free_unpacked(n);2201 n->entry = entry;22022203while(window_memory_limit &&2204 mem_usage > window_memory_limit &&2205 count >1) {2206uint32_t tail = (idx + window - count) % window;2207 mem_usage -=free_unpacked(array + tail);2208 count--;2209}22102211/* We do not compute delta to *create* objects we are not2212 * going to pack.2213 */2214if(entry->preferred_base)2215goto next;22162217/*2218 * If the current object is at pack edge, take the depth the2219 * objects that depend on the current object into account2220 * otherwise they would become too deep.2221 */2222 max_depth = depth;2223if(DELTA_CHILD(entry)) {2224 max_depth -=check_delta_limit(entry,0);2225if(max_depth <=0)2226goto next;2227}22282229 j = window;2230while(--j >0) {2231int ret;2232uint32_t other_idx = idx + j;2233struct unpacked *m;2234if(other_idx >= window)2235 other_idx -= window;2236 m = array + other_idx;2237if(!m->entry)2238break;2239 ret =try_delta(n, m, max_depth, &mem_usage);2240if(ret <0)2241break;2242else if(ret >0)2243 best_base = other_idx;2244}22452246/*2247 * If we decided to cache the delta data, then it is best2248 * to compress it right away. First because we have to do2249 * it anyway, and doing it here while we're threaded will2250 * save a lot of time in the non threaded write phase,2251 * as well as allow for caching more deltas within2252 * the same cache size limit.2253 * ...2254 * But only if not writing to stdout, since in that case2255 * the network is most likely throttling writes anyway,2256 * and therefore it is best to go to the write phase ASAP2257 * instead, as we can afford spending more time compressing2258 * between writes at that moment.2259 */2260if(entry->delta_data && !pack_to_stdout) {2261unsigned long size;22622263 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2264if(size < (1U<< OE_Z_DELTA_BITS)) {2265 entry->z_delta_size = size;2266cache_lock();2267 delta_cache_size -=DELTA_SIZE(entry);2268 delta_cache_size += entry->z_delta_size;2269cache_unlock();2270}else{2271FREE_AND_NULL(entry->delta_data);2272 entry->z_delta_size =0;2273}2274}22752276/* if we made n a delta, and if n is already at max2277 * depth, leaving it in the window is pointless. we2278 * should evict it first.2279 */2280if(DELTA(entry) && max_depth <= n->depth)2281continue;22822283/*2284 * Move the best delta base up in the window, after the2285 * currently deltified object, to keep it longer. It will2286 * be the first base object to be attempted next.2287 */2288if(DELTA(entry)) {2289struct unpacked swap = array[best_base];2290int dist = (window + idx - best_base) % window;2291int dst = best_base;2292while(dist--) {2293int src = (dst +1) % window;2294 array[dst] = array[src];2295 dst = src;2296}2297 array[dst] = swap;2298}22992300 next:2301 idx++;2302if(count +1< window)2303 count++;2304if(idx >= window)2305 idx =0;2306}23072308for(i =0; i < window; ++i) {2309free_delta_index(array[i].index);2310free(array[i].data);2311}2312free(array);2313}23142315#ifndef NO_PTHREADS23162317static voidtry_to_free_from_threads(size_t size)2318{2319read_lock();2320release_pack_memory(size);2321read_unlock();2322}23232324static try_to_free_t old_try_to_free_routine;23252326/*2327 * The main object list is split into smaller lists, each is handed to2328 * one worker.2329 *2330 * The main thread waits on the condition that (at least) one of the workers2331 * has stopped working (which is indicated in the .working member of2332 * struct thread_params).2333 *2334 * When a work thread has completed its work, it sets .working to 0 and2335 * signals the main thread and waits on the condition that .data_ready2336 * becomes 1.2337 *2338 * The main thread steals half of the work from the worker that has2339 * most work left to hand it to the idle worker.2340 */23412342struct thread_params {2343 pthread_t thread;2344struct object_entry **list;2345unsigned list_size;2346unsigned remaining;2347int window;2348int depth;2349int working;2350int data_ready;2351 pthread_mutex_t mutex;2352 pthread_cond_t cond;2353unsigned*processed;2354};23552356static pthread_cond_t progress_cond;23572358/*2359 * Mutex and conditional variable can't be statically-initialized on Windows.2360 */2361static voidinit_threaded_search(void)2362{2363init_recursive_mutex(&read_mutex);2364pthread_mutex_init(&cache_mutex, NULL);2365pthread_mutex_init(&progress_mutex, NULL);2366pthread_cond_init(&progress_cond, NULL);2367pthread_mutex_init(&to_pack.lock, NULL);2368 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2369}23702371static voidcleanup_threaded_search(void)2372{2373set_try_to_free_routine(old_try_to_free_routine);2374pthread_cond_destroy(&progress_cond);2375pthread_mutex_destroy(&read_mutex);2376pthread_mutex_destroy(&cache_mutex);2377pthread_mutex_destroy(&progress_mutex);2378}23792380static void*threaded_find_deltas(void*arg)2381{2382struct thread_params *me = arg;23832384progress_lock();2385while(me->remaining) {2386progress_unlock();23872388find_deltas(me->list, &me->remaining,2389 me->window, me->depth, me->processed);23902391progress_lock();2392 me->working =0;2393pthread_cond_signal(&progress_cond);2394progress_unlock();23952396/*2397 * We must not set ->data_ready before we wait on the2398 * condition because the main thread may have set it to 12399 * before we get here. In order to be sure that new2400 * work is available if we see 1 in ->data_ready, it2401 * was initialized to 0 before this thread was spawned2402 * and we reset it to 0 right away.2403 */2404pthread_mutex_lock(&me->mutex);2405while(!me->data_ready)2406pthread_cond_wait(&me->cond, &me->mutex);2407 me->data_ready =0;2408pthread_mutex_unlock(&me->mutex);24092410progress_lock();2411}2412progress_unlock();2413/* leave ->working 1 so that this doesn't get more work assigned */2414return NULL;2415}24162417static voidll_find_deltas(struct object_entry **list,unsigned list_size,2418int window,int depth,unsigned*processed)2419{2420struct thread_params *p;2421int i, ret, active_threads =0;24222423init_threaded_search();24242425if(delta_search_threads <=1) {2426find_deltas(list, &list_size, window, depth, processed);2427cleanup_threaded_search();2428return;2429}2430if(progress > pack_to_stdout)2431fprintf_ln(stderr,_("Delta compression using up to%dthreads"),2432 delta_search_threads);2433 p =xcalloc(delta_search_threads,sizeof(*p));24342435/* Partition the work amongst work threads. */2436for(i =0; i < delta_search_threads; i++) {2437unsigned sub_size = list_size / (delta_search_threads - i);24382439/* don't use too small segments or no deltas will be found */2440if(sub_size <2*window && i+1< delta_search_threads)2441 sub_size =0;24422443 p[i].window = window;2444 p[i].depth = depth;2445 p[i].processed = processed;2446 p[i].working =1;2447 p[i].data_ready =0;24482449/* try to split chunks on "path" boundaries */2450while(sub_size && sub_size < list_size &&2451 list[sub_size]->hash &&2452 list[sub_size]->hash == list[sub_size-1]->hash)2453 sub_size++;24542455 p[i].list = list;2456 p[i].list_size = sub_size;2457 p[i].remaining = sub_size;24582459 list += sub_size;2460 list_size -= sub_size;2461}24622463/* Start work threads. */2464for(i =0; i < delta_search_threads; i++) {2465if(!p[i].list_size)2466continue;2467pthread_mutex_init(&p[i].mutex, NULL);2468pthread_cond_init(&p[i].cond, NULL);2469 ret =pthread_create(&p[i].thread, NULL,2470 threaded_find_deltas, &p[i]);2471if(ret)2472die(_("unable to create thread:%s"),strerror(ret));2473 active_threads++;2474}24752476/*2477 * Now let's wait for work completion. Each time a thread is done2478 * with its work, we steal half of the remaining work from the2479 * thread with the largest number of unprocessed objects and give2480 * it to that newly idle thread. This ensure good load balancing2481 * until the remaining object list segments are simply too short2482 * to be worth splitting anymore.2483 */2484while(active_threads) {2485struct thread_params *target = NULL;2486struct thread_params *victim = NULL;2487unsigned sub_size =0;24882489progress_lock();2490for(;;) {2491for(i =0; !target && i < delta_search_threads; i++)2492if(!p[i].working)2493 target = &p[i];2494if(target)2495break;2496pthread_cond_wait(&progress_cond, &progress_mutex);2497}24982499for(i =0; i < delta_search_threads; i++)2500if(p[i].remaining >2*window &&2501(!victim || victim->remaining < p[i].remaining))2502 victim = &p[i];2503if(victim) {2504 sub_size = victim->remaining /2;2505 list = victim->list + victim->list_size - sub_size;2506while(sub_size && list[0]->hash &&2507 list[0]->hash == list[-1]->hash) {2508 list++;2509 sub_size--;2510}2511if(!sub_size) {2512/*2513 * It is possible for some "paths" to have2514 * so many objects that no hash boundary2515 * might be found. Let's just steal the2516 * exact half in that case.2517 */2518 sub_size = victim->remaining /2;2519 list -= sub_size;2520}2521 target->list = list;2522 victim->list_size -= sub_size;2523 victim->remaining -= sub_size;2524}2525 target->list_size = sub_size;2526 target->remaining = sub_size;2527 target->working =1;2528progress_unlock();25292530pthread_mutex_lock(&target->mutex);2531 target->data_ready =1;2532pthread_cond_signal(&target->cond);2533pthread_mutex_unlock(&target->mutex);25342535if(!sub_size) {2536pthread_join(target->thread, NULL);2537pthread_cond_destroy(&target->cond);2538pthread_mutex_destroy(&target->mutex);2539 active_threads--;2540}2541}2542cleanup_threaded_search();2543free(p);2544}25452546#else2547#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2548#endif25492550static voidadd_tag_chain(const struct object_id *oid)2551{2552struct tag *tag;25532554/*2555 * We catch duplicates already in add_object_entry(), but we'd2556 * prefer to do this extra check to avoid having to parse the2557 * tag at all if we already know that it's being packed (e.g., if2558 * it was included via bitmaps, we would not have parsed it2559 * previously).2560 */2561if(packlist_find(&to_pack, oid->hash, NULL))2562return;25632564 tag =lookup_tag(the_repository, oid);2565while(1) {2566if(!tag ||parse_tag(tag) || !tag->tagged)2567die(_("unable to pack objects reachable from tag%s"),2568oid_to_hex(oid));25692570add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);25712572if(tag->tagged->type != OBJ_TAG)2573return;25742575 tag = (struct tag *)tag->tagged;2576}2577}25782579static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2580{2581struct object_id peeled;25822583if(starts_with(path,"refs/tags/") &&/* is a tag? */2584!peel_ref(path, &peeled) &&/* peelable? */2585packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2586add_tag_chain(oid);2587return0;2588}25892590static voidprepare_pack(int window,int depth)2591{2592struct object_entry **delta_list;2593uint32_t i, nr_deltas;2594unsigned n;25952596if(use_delta_islands)2597resolve_tree_islands(progress, &to_pack);25982599get_object_details();26002601/*2602 * If we're locally repacking then we need to be doubly careful2603 * from now on in order to make sure no stealth corruption gets2604 * propagated to the new pack. Clients receiving streamed packs2605 * should validate everything they get anyway so no need to incur2606 * the additional cost here in that case.2607 */2608if(!pack_to_stdout)2609 do_check_packed_object_crc =1;26102611if(!to_pack.nr_objects || !window || !depth)2612return;26132614ALLOC_ARRAY(delta_list, to_pack.nr_objects);2615 nr_deltas = n =0;26162617for(i =0; i < to_pack.nr_objects; i++) {2618struct object_entry *entry = to_pack.objects + i;26192620if(DELTA(entry))2621/* This happens if we decided to reuse existing2622 * delta from a pack. "reuse_delta &&" is implied.2623 */2624continue;26252626if(!entry->type_valid ||2627oe_size_less_than(&to_pack, entry,50))2628continue;26292630if(entry->no_try_delta)2631continue;26322633if(!entry->preferred_base) {2634 nr_deltas++;2635if(oe_type(entry) <0)2636die(_("unable to get type of object%s"),2637oid_to_hex(&entry->idx.oid));2638}else{2639if(oe_type(entry) <0) {2640/*2641 * This object is not found, but we2642 * don't have to include it anyway.2643 */2644continue;2645}2646}26472648 delta_list[n++] = entry;2649}26502651if(nr_deltas && n >1) {2652unsigned nr_done =0;2653if(progress)2654 progress_state =start_progress(_("Compressing objects"),2655 nr_deltas);2656QSORT(delta_list, n, type_size_sort);2657ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2658stop_progress(&progress_state);2659if(nr_done != nr_deltas)2660die(_("inconsistency with delta count"));2661}2662free(delta_list);2663}26642665static intgit_pack_config(const char*k,const char*v,void*cb)2666{2667if(!strcmp(k,"pack.window")) {2668 window =git_config_int(k, v);2669return0;2670}2671if(!strcmp(k,"pack.windowmemory")) {2672 window_memory_limit =git_config_ulong(k, v);2673return0;2674}2675if(!strcmp(k,"pack.depth")) {2676 depth =git_config_int(k, v);2677return0;2678}2679if(!strcmp(k,"pack.deltacachesize")) {2680 max_delta_cache_size =git_config_int(k, v);2681return0;2682}2683if(!strcmp(k,"pack.deltacachelimit")) {2684 cache_max_small_delta_size =git_config_int(k, v);2685return0;2686}2687if(!strcmp(k,"pack.writebitmaphashcache")) {2688if(git_config_bool(k, v))2689 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2690else2691 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2692}2693if(!strcmp(k,"pack.usebitmaps")) {2694 use_bitmap_index_default =git_config_bool(k, v);2695return0;2696}2697if(!strcmp(k,"pack.threads")) {2698 delta_search_threads =git_config_int(k, v);2699if(delta_search_threads <0)2700die(_("invalid number of threads specified (%d)"),2701 delta_search_threads);2702#ifdef NO_PTHREADS2703if(delta_search_threads !=1) {2704warning(_("no threads support, ignoring%s"), k);2705 delta_search_threads =0;2706}2707#endif2708return0;2709}2710if(!strcmp(k,"pack.indexversion")) {2711 pack_idx_opts.version =git_config_int(k, v);2712if(pack_idx_opts.version >2)2713die(_("bad pack.indexversion=%"PRIu32),2714 pack_idx_opts.version);2715return0;2716}2717returngit_default_config(k, v, cb);2718}27192720static voidread_object_list_from_stdin(void)2721{2722char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2723struct object_id oid;2724const char*p;27252726for(;;) {2727if(!fgets(line,sizeof(line), stdin)) {2728if(feof(stdin))2729break;2730if(!ferror(stdin))2731die("BUG: fgets returned NULL, not EOF, not error!");2732if(errno != EINTR)2733die_errno("fgets");2734clearerr(stdin);2735continue;2736}2737if(line[0] =='-') {2738if(get_oid_hex(line+1, &oid))2739die(_("expected edge object ID, got garbage:\n%s"),2740 line);2741add_preferred_base(&oid);2742continue;2743}2744if(parse_oid_hex(line, &oid, &p))2745die(_("expected object ID, got garbage:\n%s"), line);27462747add_preferred_base_object(p +1);2748add_object_entry(&oid, OBJ_NONE, p +1,0);2749}2750}27512752/* Remember to update object flag allocation in object.h */2753#define OBJECT_ADDED (1u<<20)27542755static voidshow_commit(struct commit *commit,void*data)2756{2757add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2758 commit->object.flags |= OBJECT_ADDED;27592760if(write_bitmap_index)2761index_commit_for_bitmap(commit);27622763if(use_delta_islands)2764propagate_island_marks(commit);2765}27662767static voidshow_object(struct object *obj,const char*name,void*data)2768{2769add_preferred_base_object(name);2770add_object_entry(&obj->oid, obj->type, name,0);2771 obj->flags |= OBJECT_ADDED;27722773if(use_delta_islands) {2774const char*p;2775unsigned depth =0;2776struct object_entry *ent;27772778for(p =strchr(name,'/'); p; p =strchr(p +1,'/'))2779 depth++;27802781 ent =packlist_find(&to_pack, obj->oid.hash, NULL);2782if(ent && depth >oe_tree_depth(&to_pack, ent))2783oe_set_tree_depth(&to_pack, ent, depth);2784}2785}27862787static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2788{2789assert(arg_missing_action == MA_ALLOW_ANY);27902791/*2792 * Quietly ignore ALL missing objects. This avoids problems with2793 * staging them now and getting an odd error later.2794 */2795if(!has_object_file(&obj->oid))2796return;27972798show_object(obj, name, data);2799}28002801static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2802{2803assert(arg_missing_action == MA_ALLOW_PROMISOR);28042805/*2806 * Quietly ignore EXPECTED missing objects. This avoids problems with2807 * staging them now and getting an odd error later.2808 */2809if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2810return;28112812show_object(obj, name, data);2813}28142815static intoption_parse_missing_action(const struct option *opt,2816const char*arg,int unset)2817{2818assert(arg);2819assert(!unset);28202821if(!strcmp(arg,"error")) {2822 arg_missing_action = MA_ERROR;2823 fn_show_object = show_object;2824return0;2825}28262827if(!strcmp(arg,"allow-any")) {2828 arg_missing_action = MA_ALLOW_ANY;2829 fetch_if_missing =0;2830 fn_show_object = show_object__ma_allow_any;2831return0;2832}28332834if(!strcmp(arg,"allow-promisor")) {2835 arg_missing_action = MA_ALLOW_PROMISOR;2836 fetch_if_missing =0;2837 fn_show_object = show_object__ma_allow_promisor;2838return0;2839}28402841die(_("invalid value for --missing"));2842return0;2843}28442845static voidshow_edge(struct commit *commit)2846{2847add_preferred_base(&commit->object.oid);2848}28492850struct in_pack_object {2851 off_t offset;2852struct object *object;2853};28542855struct in_pack {2856unsigned int alloc;2857unsigned int nr;2858struct in_pack_object *array;2859};28602861static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2862{2863 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2864 in_pack->array[in_pack->nr].object = object;2865 in_pack->nr++;2866}28672868/*2869 * Compare the objects in the offset order, in order to emulate the2870 * "git rev-list --objects" output that produced the pack originally.2871 */2872static intofscmp(const void*a_,const void*b_)2873{2874struct in_pack_object *a = (struct in_pack_object *)a_;2875struct in_pack_object *b = (struct in_pack_object *)b_;28762877if(a->offset < b->offset)2878return-1;2879else if(a->offset > b->offset)2880return1;2881else2882returnoidcmp(&a->object->oid, &b->object->oid);2883}28842885static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2886{2887struct packed_git *p;2888struct in_pack in_pack;2889uint32_t i;28902891memset(&in_pack,0,sizeof(in_pack));28922893for(p =get_all_packs(the_repository); p; p = p->next) {2894struct object_id oid;2895struct object *o;28962897if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2898continue;2899if(open_pack_index(p))2900die(_("cannot open pack index"));29012902ALLOC_GROW(in_pack.array,2903 in_pack.nr + p->num_objects,2904 in_pack.alloc);29052906for(i =0; i < p->num_objects; i++) {2907nth_packed_object_oid(&oid, p, i);2908 o =lookup_unknown_object(oid.hash);2909if(!(o->flags & OBJECT_ADDED))2910mark_in_pack_object(o, p, &in_pack);2911 o->flags |= OBJECT_ADDED;2912}2913}29142915if(in_pack.nr) {2916QSORT(in_pack.array, in_pack.nr, ofscmp);2917for(i =0; i < in_pack.nr; i++) {2918struct object *o = in_pack.array[i].object;2919add_object_entry(&o->oid, o->type,"",0);2920}2921}2922free(in_pack.array);2923}29242925static intadd_loose_object(const struct object_id *oid,const char*path,2926void*data)2927{2928enum object_type type =oid_object_info(the_repository, oid, NULL);29292930if(type <0) {2931warning(_("loose object at%scould not be examined"), path);2932return0;2933}29342935add_object_entry(oid, type,"",0);2936return0;2937}29382939/*2940 * We actually don't even have to worry about reachability here.2941 * add_object_entry will weed out duplicates, so we just add every2942 * loose object we find.2943 */2944static voidadd_unreachable_loose_objects(void)2945{2946for_each_loose_file_in_objdir(get_object_directory(),2947 add_loose_object,2948 NULL, NULL, NULL);2949}29502951static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2952{2953static struct packed_git *last_found = (void*)1;2954struct packed_git *p;29552956 p = (last_found != (void*)1) ? last_found :2957get_all_packs(the_repository);29582959while(p) {2960if((!p->pack_local || p->pack_keep ||2961 p->pack_keep_in_core) &&2962find_pack_entry_one(oid->hash, p)) {2963 last_found = p;2964return1;2965}2966if(p == last_found)2967 p =get_all_packs(the_repository);2968else2969 p = p->next;2970if(p == last_found)2971 p = p->next;2972}2973return0;2974}29752976/*2977 * Store a list of sha1s that are should not be discarded2978 * because they are either written too recently, or are2979 * reachable from another object that was.2980 *2981 * This is filled by get_object_list.2982 */2983static struct oid_array recent_objects;29842985static intloosened_object_can_be_discarded(const struct object_id *oid,2986 timestamp_t mtime)2987{2988if(!unpack_unreachable_expiration)2989return0;2990if(mtime > unpack_unreachable_expiration)2991return0;2992if(oid_array_lookup(&recent_objects, oid) >=0)2993return0;2994return1;2995}29962997static voidloosen_unused_packed_objects(struct rev_info *revs)2998{2999struct packed_git *p;3000uint32_t i;3001struct object_id oid;30023003for(p =get_all_packs(the_repository); p; p = p->next) {3004if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)3005continue;30063007if(open_pack_index(p))3008die(_("cannot open pack index"));30093010for(i =0; i < p->num_objects; i++) {3011nth_packed_object_oid(&oid, p, i);3012if(!packlist_find(&to_pack, oid.hash, NULL) &&3013!has_sha1_pack_kept_or_nonlocal(&oid) &&3014!loosened_object_can_be_discarded(&oid, p->mtime))3015if(force_object_loose(&oid, p->mtime))3016die(_("unable to force loose object"));3017}3018}3019}30203021/*3022 * This tracks any options which pack-reuse code expects to be on, or which a3023 * reader of the pack might not understand, and which would therefore prevent3024 * blind reuse of what we have on disk.3025 */3026static intpack_options_allow_reuse(void)3027{3028return pack_to_stdout &&3029 allow_ofs_delta &&3030!ignore_packed_keep_on_disk &&3031!ignore_packed_keep_in_core &&3032(!local || !have_non_local_packs) &&3033!incremental;3034}30353036static intget_object_list_from_bitmap(struct rev_info *revs)3037{3038if(!(bitmap_git =prepare_bitmap_walk(revs)))3039return-1;30403041if(pack_options_allow_reuse() &&3042!reuse_partial_packfile_from_bitmap(3043 bitmap_git,3044&reuse_packfile,3045&reuse_packfile_objects,3046&reuse_packfile_offset)) {3047assert(reuse_packfile_objects);3048 nr_result += reuse_packfile_objects;3049display_progress(progress_state, nr_result);3050}30513052traverse_bitmap_commit_list(bitmap_git, &add_object_entry_from_bitmap);3053return0;3054}30553056static voidrecord_recent_object(struct object *obj,3057const char*name,3058void*data)3059{3060oid_array_append(&recent_objects, &obj->oid);3061}30623063static voidrecord_recent_commit(struct commit *commit,void*data)3064{3065oid_array_append(&recent_objects, &commit->object.oid);3066}30673068static voidget_object_list(int ac,const char**av)3069{3070struct rev_info revs;3071char line[1000];3072int flags =0;30733074init_revisions(&revs, NULL);3075 save_commit_buffer =0;3076setup_revisions(ac, av, &revs, NULL);30773078/* make sure shallows are read */3079is_repository_shallow(the_repository);30803081while(fgets(line,sizeof(line), stdin) != NULL) {3082int len =strlen(line);3083if(len && line[len -1] =='\n')3084 line[--len] =0;3085if(!len)3086break;3087if(*line =='-') {3088if(!strcmp(line,"--not")) {3089 flags ^= UNINTERESTING;3090 write_bitmap_index =0;3091continue;3092}3093if(starts_with(line,"--shallow ")) {3094struct object_id oid;3095if(get_oid_hex(line +10, &oid))3096die("not an SHA-1 '%s'", line +10);3097register_shallow(the_repository, &oid);3098 use_bitmap_index =0;3099continue;3100}3101die(_("not a rev '%s'"), line);3102}3103if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))3104die(_("bad revision '%s'"), line);3105}31063107if(use_bitmap_index && !get_object_list_from_bitmap(&revs))3108return;31093110if(use_delta_islands)3111load_delta_islands();31123113if(prepare_revision_walk(&revs))3114die(_("revision walk setup failed"));3115mark_edges_uninteresting(&revs, show_edge);31163117if(!fn_show_object)3118 fn_show_object = show_object;3119traverse_commit_list_filtered(&filter_options, &revs,3120 show_commit, fn_show_object, NULL,3121 NULL);31223123if(unpack_unreachable_expiration) {3124 revs.ignore_missing_links =1;3125if(add_unseen_recent_objects_to_traversal(&revs,3126 unpack_unreachable_expiration))3127die(_("unable to add recent objects"));3128if(prepare_revision_walk(&revs))3129die(_("revision walk setup failed"));3130traverse_commit_list(&revs, record_recent_commit,3131 record_recent_object, NULL);3132}31333134if(keep_unreachable)3135add_objects_in_unpacked_packs(&revs);3136if(pack_loose_unreachable)3137add_unreachable_loose_objects();3138if(unpack_unreachable)3139loosen_unused_packed_objects(&revs);31403141oid_array_clear(&recent_objects);3142}31433144static voidadd_extra_kept_packs(const struct string_list *names)3145{3146struct packed_git *p;31473148if(!names->nr)3149return;31503151for(p =get_all_packs(the_repository); p; p = p->next) {3152const char*name =basename(p->pack_name);3153int i;31543155if(!p->pack_local)3156continue;31573158for(i =0; i < names->nr; i++)3159if(!fspathcmp(name, names->items[i].string))3160break;31613162if(i < names->nr) {3163 p->pack_keep_in_core =1;3164 ignore_packed_keep_in_core =1;3165continue;3166}3167}3168}31693170static intoption_parse_index_version(const struct option *opt,3171const char*arg,int unset)3172{3173char*c;3174const char*val = arg;3175 pack_idx_opts.version =strtoul(val, &c,10);3176if(pack_idx_opts.version >2)3177die(_("unsupported index version%s"), val);3178if(*c ==','&& c[1])3179 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3180if(*c || pack_idx_opts.off32_limit &0x80000000)3181die(_("bad index version '%s'"), val);3182return0;3183}31843185static intoption_parse_unpack_unreachable(const struct option *opt,3186const char*arg,int unset)3187{3188if(unset) {3189 unpack_unreachable =0;3190 unpack_unreachable_expiration =0;3191}3192else{3193 unpack_unreachable =1;3194if(arg)3195 unpack_unreachable_expiration =approxidate(arg);3196}3197return0;3198}31993200intcmd_pack_objects(int argc,const char**argv,const char*prefix)3201{3202int use_internal_rev_list =0;3203int shallow =0;3204int all_progress_implied =0;3205struct argv_array rp = ARGV_ARRAY_INIT;3206int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3207int rev_list_index =0;3208struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3209struct option pack_objects_options[] = {3210OPT_SET_INT('q',"quiet", &progress,3211N_("do not show progress meter"),0),3212OPT_SET_INT(0,"progress", &progress,3213N_("show progress meter"),1),3214OPT_SET_INT(0,"all-progress", &progress,3215N_("show progress meter during object writing phase"),2),3216OPT_BOOL(0,"all-progress-implied",3217&all_progress_implied,3218N_("similar to --all-progress when progress meter is shown")),3219{ OPTION_CALLBACK,0,"index-version", NULL,N_("<version>[,<offset>]"),3220N_("write the pack index file in the specified idx format version"),32210, option_parse_index_version },3222OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3223N_("maximum size of each output pack file")),3224OPT_BOOL(0,"local", &local,3225N_("ignore borrowed objects from alternate object store")),3226OPT_BOOL(0,"incremental", &incremental,3227N_("ignore packed objects")),3228OPT_INTEGER(0,"window", &window,3229N_("limit pack window by objects")),3230OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3231N_("limit pack window by memory in addition to object limit")),3232OPT_INTEGER(0,"depth", &depth,3233N_("maximum length of delta chain allowed in the resulting pack")),3234OPT_BOOL(0,"reuse-delta", &reuse_delta,3235N_("reuse existing deltas")),3236OPT_BOOL(0,"reuse-object", &reuse_object,3237N_("reuse existing objects")),3238OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3239N_("use OFS_DELTA objects")),3240OPT_INTEGER(0,"threads", &delta_search_threads,3241N_("use threads when searching for best delta matches")),3242OPT_BOOL(0,"non-empty", &non_empty,3243N_("do not create an empty pack output")),3244OPT_BOOL(0,"revs", &use_internal_rev_list,3245N_("read revision arguments from standard input")),3246OPT_SET_INT_F(0,"unpacked", &rev_list_unpacked,3247N_("limit the objects to those that are not yet packed"),32481, PARSE_OPT_NONEG),3249OPT_SET_INT_F(0,"all", &rev_list_all,3250N_("include objects reachable from any reference"),32511, PARSE_OPT_NONEG),3252OPT_SET_INT_F(0,"reflog", &rev_list_reflog,3253N_("include objects referred by reflog entries"),32541, PARSE_OPT_NONEG),3255OPT_SET_INT_F(0,"indexed-objects", &rev_list_index,3256N_("include objects referred to by the index"),32571, PARSE_OPT_NONEG),3258OPT_BOOL(0,"stdout", &pack_to_stdout,3259N_("output pack to stdout")),3260OPT_BOOL(0,"include-tag", &include_tag,3261N_("include tag objects that refer to objects to be packed")),3262OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3263N_("keep unreachable objects")),3264OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3265N_("pack loose unreachable objects")),3266{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3267N_("unpack unreachable objects newer than <time>"),3268 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3269OPT_BOOL(0,"thin", &thin,3270N_("create thin packs")),3271OPT_BOOL(0,"shallow", &shallow,3272N_("create packs suitable for shallow fetches")),3273OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3274N_("ignore packs that have companion .keep file")),3275OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3276N_("ignore this pack")),3277OPT_INTEGER(0,"compression", &pack_compression_level,3278N_("pack compression level")),3279OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3280N_("do not hide commits by grafts"),0),3281OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3282N_("use a bitmap index if available to speed up counting objects")),3283OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3284N_("write a bitmap index together with the pack index")),3285OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3286{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3287N_("handling for missing objects"), PARSE_OPT_NONEG,3288 option_parse_missing_action },3289OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3290N_("do not pack objects in promisor packfiles")),3291OPT_BOOL(0,"delta-islands", &use_delta_islands,3292N_("respect islands during delta compression")),3293OPT_END(),3294};32953296if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3297BUG("too many dfs states, increase OE_DFS_STATE_BITS");32983299 read_replace_refs =0;33003301reset_pack_idx_option(&pack_idx_opts);3302git_config(git_pack_config, NULL);33033304 progress =isatty(2);3305 argc =parse_options(argc, argv, prefix, pack_objects_options,3306 pack_usage,0);33073308if(argc) {3309 base_name = argv[0];3310 argc--;3311}3312if(pack_to_stdout != !base_name || argc)3313usage_with_options(pack_usage, pack_objects_options);33143315if(depth >= (1<< OE_DEPTH_BITS)) {3316warning(_("delta chain depth%dis too deep, forcing%d"),3317 depth, (1<< OE_DEPTH_BITS) -1);3318 depth = (1<< OE_DEPTH_BITS) -1;3319}3320if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3321warning(_("pack.deltaCacheLimit is too high, forcing%d"),3322(1U<< OE_Z_DELTA_BITS) -1);3323 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3324}33253326argv_array_push(&rp,"pack-objects");3327if(thin) {3328 use_internal_rev_list =1;3329argv_array_push(&rp, shallow3330?"--objects-edge-aggressive"3331:"--objects-edge");3332}else3333argv_array_push(&rp,"--objects");33343335if(rev_list_all) {3336 use_internal_rev_list =1;3337argv_array_push(&rp,"--all");3338}3339if(rev_list_reflog) {3340 use_internal_rev_list =1;3341argv_array_push(&rp,"--reflog");3342}3343if(rev_list_index) {3344 use_internal_rev_list =1;3345argv_array_push(&rp,"--indexed-objects");3346}3347if(rev_list_unpacked) {3348 use_internal_rev_list =1;3349argv_array_push(&rp,"--unpacked");3350}33513352if(exclude_promisor_objects) {3353 use_internal_rev_list =1;3354 fetch_if_missing =0;3355argv_array_push(&rp,"--exclude-promisor-objects");3356}3357if(unpack_unreachable || keep_unreachable || pack_loose_unreachable)3358 use_internal_rev_list =1;33593360if(!reuse_object)3361 reuse_delta =0;3362if(pack_compression_level == -1)3363 pack_compression_level = Z_DEFAULT_COMPRESSION;3364else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3365die(_("bad pack compression level%d"), pack_compression_level);33663367if(!delta_search_threads)/* --threads=0 means autodetect */3368 delta_search_threads =online_cpus();33693370#ifdef NO_PTHREADS3371if(delta_search_threads !=1)3372warning(_("no threads support, ignoring --threads"));3373#endif3374if(!pack_to_stdout && !pack_size_limit)3375 pack_size_limit = pack_size_limit_cfg;3376if(pack_to_stdout && pack_size_limit)3377die(_("--max-pack-size cannot be used to build a pack for transfer"));3378if(pack_size_limit && pack_size_limit <1024*1024) {3379warning(_("minimum pack size limit is 1 MiB"));3380 pack_size_limit =1024*1024;3381}33823383if(!pack_to_stdout && thin)3384die(_("--thin cannot be used to build an indexable pack"));33853386if(keep_unreachable && unpack_unreachable)3387die(_("--keep-unreachable and --unpack-unreachable are incompatible"));3388if(!rev_list_all || !rev_list_reflog || !rev_list_index)3389 unpack_unreachable_expiration =0;33903391if(filter_options.choice) {3392if(!pack_to_stdout)3393die(_("cannot use --filter without --stdout"));3394 use_bitmap_index =0;3395}33963397/*3398 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3399 *3400 * - to produce good pack (with bitmap index not-yet-packed objects are3401 * packed in suboptimal order).3402 *3403 * - to use more robust pack-generation codepath (avoiding possible3404 * bugs in bitmap code and possible bitmap index corruption).3405 */3406if(!pack_to_stdout)3407 use_bitmap_index_default =0;34083409if(use_bitmap_index <0)3410 use_bitmap_index = use_bitmap_index_default;34113412/* "hard" reasons not to use bitmaps; these just won't work at all */3413if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow(the_repository))3414 use_bitmap_index =0;34153416if(pack_to_stdout || !rev_list_all)3417 write_bitmap_index =0;34183419if(use_delta_islands)3420argv_array_push(&rp,"--topo-order");34213422if(progress && all_progress_implied)3423 progress =2;34243425add_extra_kept_packs(&keep_pack_list);3426if(ignore_packed_keep_on_disk) {3427struct packed_git *p;3428for(p =get_all_packs(the_repository); p; p = p->next)3429if(p->pack_local && p->pack_keep)3430break;3431if(!p)/* no keep-able packs found */3432 ignore_packed_keep_on_disk =0;3433}3434if(local) {3435/*3436 * unlike ignore_packed_keep_on_disk above, we do not3437 * want to unset "local" based on looking at packs, as3438 * it also covers non-local objects3439 */3440struct packed_git *p;3441for(p =get_all_packs(the_repository); p; p = p->next) {3442if(!p->pack_local) {3443 have_non_local_packs =1;3444break;3445}3446}3447}34483449prepare_packing_data(&to_pack);34503451if(progress)3452 progress_state =start_progress(_("Enumerating objects"),0);3453if(!use_internal_rev_list)3454read_object_list_from_stdin();3455else{3456get_object_list(rp.argc, rp.argv);3457argv_array_clear(&rp);3458}3459cleanup_preferred_base();3460if(include_tag && nr_result)3461for_each_ref(add_ref_tag, NULL);3462stop_progress(&progress_state);34633464if(non_empty && !nr_result)3465return0;3466if(nr_result)3467prepare_pack(window, depth);3468write_pack_file();3469if(progress)3470fprintf_ln(stderr,3471_("Total %"PRIu32" (delta %"PRIu32"),"3472" reused %"PRIu32" (delta %"PRIu32")"),3473 written, written_delta, reused, reused_delta);3474return0;3475}