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); 973git_check_attr(&the_index, path, check); 974if(ATTR_FALSE(check->items[0].value)) 975return1; 976return0; 977} 978 979/* 980 * When adding an object, check whether we have already added it 981 * to our packing list. If so, we can skip. However, if we are 982 * being asked to excludei t, but the previous mention was to include 983 * it, make sure to adjust its flags and tweak our numbers accordingly. 984 * 985 * As an optimization, we pass out the index position where we would have 986 * found the item, since that saves us from having to look it up again a 987 * few lines later when we want to add the new entry. 988 */ 989static inthave_duplicate_entry(const struct object_id *oid, 990int exclude, 991uint32_t*index_pos) 992{ 993struct object_entry *entry; 994 995 entry =packlist_find(&to_pack, oid->hash, index_pos); 996if(!entry) 997return0; 998 999if(exclude) {1000if(!entry->preferred_base)1001 nr_result--;1002 entry->preferred_base =1;1003}10041005return1;1006}10071008static intwant_found_object(int exclude,struct packed_git *p)1009{1010if(exclude)1011return1;1012if(incremental)1013return0;10141015/*1016 * When asked to do --local (do not include an object that appears in a1017 * pack we borrow from elsewhere) or --honor-pack-keep (do not include1018 * an object that appears in a pack marked with .keep), finding a pack1019 * that matches the criteria is sufficient for us to decide to omit it.1020 * However, even if this pack does not satisfy the criteria, we need to1021 * make sure no copy of this object appears in _any_ pack that makes us1022 * to omit the object, so we need to check all the packs.1023 *1024 * We can however first check whether these options can possible matter;1025 * if they do not matter we know we want the object in generated pack.1026 * Otherwise, we signal "-1" at the end to tell the caller that we do1027 * not know either way, and it needs to check more packs.1028 */1029if(!ignore_packed_keep_on_disk &&1030!ignore_packed_keep_in_core &&1031(!local || !have_non_local_packs))1032return1;10331034if(local && !p->pack_local)1035return0;1036if(p->pack_local &&1037((ignore_packed_keep_on_disk && p->pack_keep) ||1038(ignore_packed_keep_in_core && p->pack_keep_in_core)))1039return0;10401041/* we don't know yet; keep looking for more packs */1042return-1;1043}10441045/*1046 * Check whether we want the object in the pack (e.g., we do not want1047 * objects found in non-local stores if the "--local" option was used).1048 *1049 * If the caller already knows an existing pack it wants to take the object1050 * from, that is passed in *found_pack and *found_offset; otherwise this1051 * function finds if there is any pack that has the object and returns the pack1052 * and its offset in these variables.1053 */1054static intwant_object_in_pack(const struct object_id *oid,1055int exclude,1056struct packed_git **found_pack,1057 off_t *found_offset)1058{1059int want;1060struct list_head *pos;1061struct multi_pack_index *m;10621063if(!exclude && local &&has_loose_object_nonlocal(oid))1064return0;10651066/*1067 * If we already know the pack object lives in, start checks from that1068 * pack - in the usual case when neither --local was given nor .keep files1069 * are present we will determine the answer right now.1070 */1071if(*found_pack) {1072 want =want_found_object(exclude, *found_pack);1073if(want != -1)1074return want;1075}10761077for(m =get_multi_pack_index(the_repository); m; m = m->next) {1078struct pack_entry e;1079if(fill_midx_entry(oid, &e, m)) {1080struct packed_git *p = e.p;1081 off_t offset;10821083if(p == *found_pack)1084 offset = *found_offset;1085else1086 offset =find_pack_entry_one(oid->hash, p);10871088if(offset) {1089if(!*found_pack) {1090if(!is_pack_valid(p))1091continue;1092*found_offset = offset;1093*found_pack = p;1094}1095 want =want_found_object(exclude, p);1096if(want != -1)1097return want;1098}1099}1100}11011102list_for_each(pos,get_packed_git_mru(the_repository)) {1103struct packed_git *p =list_entry(pos,struct packed_git, mru);1104 off_t offset;11051106if(p == *found_pack)1107 offset = *found_offset;1108else1109 offset =find_pack_entry_one(oid->hash, p);11101111if(offset) {1112if(!*found_pack) {1113if(!is_pack_valid(p))1114continue;1115*found_offset = offset;1116*found_pack = p;1117}1118 want =want_found_object(exclude, p);1119if(!exclude && want >0)1120list_move(&p->mru,1121get_packed_git_mru(the_repository));1122if(want != -1)1123return want;1124}1125}11261127return1;1128}11291130static voidcreate_object_entry(const struct object_id *oid,1131enum object_type type,1132uint32_t hash,1133int exclude,1134int no_try_delta,1135uint32_t index_pos,1136struct packed_git *found_pack,1137 off_t found_offset)1138{1139struct object_entry *entry;11401141 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1142 entry->hash = hash;1143oe_set_type(entry, type);1144if(exclude)1145 entry->preferred_base =1;1146else1147 nr_result++;1148if(found_pack) {1149oe_set_in_pack(&to_pack, entry, found_pack);1150 entry->in_pack_offset = found_offset;1151}11521153 entry->no_try_delta = no_try_delta;1154}11551156static const char no_closure_warning[] =N_(1157"disabling bitmap writing, as some objects are not being packed"1158);11591160static intadd_object_entry(const struct object_id *oid,enum object_type type,1161const char*name,int exclude)1162{1163struct packed_git *found_pack = NULL;1164 off_t found_offset =0;1165uint32_t index_pos;11661167display_progress(progress_state, ++nr_seen);11681169if(have_duplicate_entry(oid, exclude, &index_pos))1170return0;11711172if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1173/* The pack is missing an object, so it will not have closure */1174if(write_bitmap_index) {1175warning(_(no_closure_warning));1176 write_bitmap_index =0;1177}1178return0;1179}11801181create_object_entry(oid, type,pack_name_hash(name),1182 exclude, name &&no_try_delta(name),1183 index_pos, found_pack, found_offset);1184return1;1185}11861187static intadd_object_entry_from_bitmap(const struct object_id *oid,1188enum object_type type,1189int flags,uint32_t name_hash,1190struct packed_git *pack, off_t offset)1191{1192uint32_t index_pos;11931194display_progress(progress_state, ++nr_seen);11951196if(have_duplicate_entry(oid,0, &index_pos))1197return0;11981199if(!want_object_in_pack(oid,0, &pack, &offset))1200return0;12011202create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1203return1;1204}12051206struct pbase_tree_cache {1207struct object_id oid;1208int ref;1209int temporary;1210void*tree_data;1211unsigned long tree_size;1212};12131214static struct pbase_tree_cache *(pbase_tree_cache[256]);1215static intpbase_tree_cache_ix(const struct object_id *oid)1216{1217return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1218}1219static intpbase_tree_cache_ix_incr(int ix)1220{1221return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1222}12231224static struct pbase_tree {1225struct pbase_tree *next;1226/* This is a phony "cache" entry; we are not1227 * going to evict it or find it through _get()1228 * mechanism -- this is for the toplevel node that1229 * would almost always change with any commit.1230 */1231struct pbase_tree_cache pcache;1232} *pbase_tree;12331234static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1235{1236struct pbase_tree_cache *ent, *nent;1237void*data;1238unsigned long size;1239enum object_type type;1240int neigh;1241int my_ix =pbase_tree_cache_ix(oid);1242int available_ix = -1;12431244/* pbase-tree-cache acts as a limited hashtable.1245 * your object will be found at your index or within a few1246 * slots after that slot if it is cached.1247 */1248for(neigh =0; neigh <8; neigh++) {1249 ent = pbase_tree_cache[my_ix];1250if(ent &&oideq(&ent->oid, oid)) {1251 ent->ref++;1252return ent;1253}1254else if(((available_ix <0) && (!ent || !ent->ref)) ||1255((0<= available_ix) &&1256(!ent && pbase_tree_cache[available_ix])))1257 available_ix = my_ix;1258if(!ent)1259break;1260 my_ix =pbase_tree_cache_ix_incr(my_ix);1261}12621263/* Did not find one. Either we got a bogus request or1264 * we need to read and perhaps cache.1265 */1266 data =read_object_file(oid, &type, &size);1267if(!data)1268return NULL;1269if(type != OBJ_TREE) {1270free(data);1271return NULL;1272}12731274/* We need to either cache or return a throwaway copy */12751276if(available_ix <0)1277 ent = NULL;1278else{1279 ent = pbase_tree_cache[available_ix];1280 my_ix = available_ix;1281}12821283if(!ent) {1284 nent =xmalloc(sizeof(*nent));1285 nent->temporary = (available_ix <0);1286}1287else{1288/* evict and reuse */1289free(ent->tree_data);1290 nent = ent;1291}1292oidcpy(&nent->oid, oid);1293 nent->tree_data = data;1294 nent->tree_size = size;1295 nent->ref =1;1296if(!nent->temporary)1297 pbase_tree_cache[my_ix] = nent;1298return nent;1299}13001301static voidpbase_tree_put(struct pbase_tree_cache *cache)1302{1303if(!cache->temporary) {1304 cache->ref--;1305return;1306}1307free(cache->tree_data);1308free(cache);1309}13101311static intname_cmp_len(const char*name)1312{1313int i;1314for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1315;1316return i;1317}13181319static voidadd_pbase_object(struct tree_desc *tree,1320const char*name,1321int cmplen,1322const char*fullname)1323{1324struct name_entry entry;1325int cmp;13261327while(tree_entry(tree,&entry)) {1328if(S_ISGITLINK(entry.mode))1329continue;1330 cmp =tree_entry_len(&entry) != cmplen ?1:1331memcmp(name, entry.path, cmplen);1332if(cmp >0)1333continue;1334if(cmp <0)1335return;1336if(name[cmplen] !='/') {1337add_object_entry(entry.oid,1338object_type(entry.mode),1339 fullname,1);1340return;1341}1342if(S_ISDIR(entry.mode)) {1343struct tree_desc sub;1344struct pbase_tree_cache *tree;1345const char*down = name+cmplen+1;1346int downlen =name_cmp_len(down);13471348 tree =pbase_tree_get(entry.oid);1349if(!tree)1350return;1351init_tree_desc(&sub, tree->tree_data, tree->tree_size);13521353add_pbase_object(&sub, down, downlen, fullname);1354pbase_tree_put(tree);1355}1356}1357}13581359static unsigned*done_pbase_paths;1360static int done_pbase_paths_num;1361static int done_pbase_paths_alloc;1362static intdone_pbase_path_pos(unsigned hash)1363{1364int lo =0;1365int hi = done_pbase_paths_num;1366while(lo < hi) {1367int mi = lo + (hi - lo) /2;1368if(done_pbase_paths[mi] == hash)1369return mi;1370if(done_pbase_paths[mi] < hash)1371 hi = mi;1372else1373 lo = mi +1;1374}1375return-lo-1;1376}13771378static intcheck_pbase_path(unsigned hash)1379{1380int pos =done_pbase_path_pos(hash);1381if(0<= pos)1382return1;1383 pos = -pos -1;1384ALLOC_GROW(done_pbase_paths,1385 done_pbase_paths_num +1,1386 done_pbase_paths_alloc);1387 done_pbase_paths_num++;1388if(pos < done_pbase_paths_num)1389MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1390 done_pbase_paths_num - pos -1);1391 done_pbase_paths[pos] = hash;1392return0;1393}13941395static voidadd_preferred_base_object(const char*name)1396{1397struct pbase_tree *it;1398int cmplen;1399unsigned hash =pack_name_hash(name);14001401if(!num_preferred_base ||check_pbase_path(hash))1402return;14031404 cmplen =name_cmp_len(name);1405for(it = pbase_tree; it; it = it->next) {1406if(cmplen ==0) {1407add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1408}1409else{1410struct tree_desc tree;1411init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1412add_pbase_object(&tree, name, cmplen, name);1413}1414}1415}14161417static voidadd_preferred_base(struct object_id *oid)1418{1419struct pbase_tree *it;1420void*data;1421unsigned long size;1422struct object_id tree_oid;14231424if(window <= num_preferred_base++)1425return;14261427 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1428if(!data)1429return;14301431for(it = pbase_tree; it; it = it->next) {1432if(oideq(&it->pcache.oid, &tree_oid)) {1433free(data);1434return;1435}1436}14371438 it =xcalloc(1,sizeof(*it));1439 it->next = pbase_tree;1440 pbase_tree = it;14411442oidcpy(&it->pcache.oid, &tree_oid);1443 it->pcache.tree_data = data;1444 it->pcache.tree_size = size;1445}14461447static voidcleanup_preferred_base(void)1448{1449struct pbase_tree *it;1450unsigned i;14511452 it = pbase_tree;1453 pbase_tree = NULL;1454while(it) {1455struct pbase_tree *tmp = it;1456 it = tmp->next;1457free(tmp->pcache.tree_data);1458free(tmp);1459}14601461for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1462if(!pbase_tree_cache[i])1463continue;1464free(pbase_tree_cache[i]->tree_data);1465FREE_AND_NULL(pbase_tree_cache[i]);1466}14671468FREE_AND_NULL(done_pbase_paths);1469 done_pbase_paths_num = done_pbase_paths_alloc =0;1470}14711472/*1473 * Return 1 iff the object specified by "delta" can be sent1474 * literally as a delta against the base in "base_sha1". If1475 * so, then *base_out will point to the entry in our packing1476 * list, or NULL if we must use the external-base list.1477 *1478 * Depth value does not matter - find_deltas() will1479 * never consider reused delta as the base object to1480 * deltify other objects against, in order to avoid1481 * circular deltas.1482 */1483static intcan_reuse_delta(const unsigned char*base_sha1,1484struct object_entry *delta,1485struct object_entry **base_out)1486{1487struct object_entry *base;14881489if(!base_sha1)1490return0;14911492/*1493 * First see if we're already sending the base (or it's explicitly in1494 * our "excluded" list).1495 */1496 base =packlist_find(&to_pack, base_sha1, NULL);1497if(base) {1498if(!in_same_island(&delta->idx.oid, &base->idx.oid))1499return0;1500*base_out = base;1501return1;1502}15031504/*1505 * Otherwise, reachability bitmaps may tell us if the receiver has it,1506 * even if it was buried too deep in history to make it into the1507 * packing list.1508 */1509if(thin &&bitmap_has_sha1_in_uninteresting(bitmap_git, base_sha1)) {1510if(use_delta_islands) {1511struct object_id base_oid;1512hashcpy(base_oid.hash, base_sha1);1513if(!in_same_island(&delta->idx.oid, &base_oid))1514return0;1515}1516*base_out = NULL;1517return1;1518}15191520return0;1521}15221523static voidcheck_object(struct object_entry *entry)1524{1525unsigned long canonical_size;15261527if(IN_PACK(entry)) {1528struct packed_git *p =IN_PACK(entry);1529struct pack_window *w_curs = NULL;1530const unsigned char*base_ref = NULL;1531struct object_entry *base_entry;1532unsigned long used, used_0;1533unsigned long avail;1534 off_t ofs;1535unsigned char*buf, c;1536enum object_type type;1537unsigned long in_pack_size;15381539 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);15401541/*1542 * We want in_pack_type even if we do not reuse delta1543 * since non-delta representations could still be reused.1544 */1545 used =unpack_object_header_buffer(buf, avail,1546&type,1547&in_pack_size);1548if(used ==0)1549goto give_up;15501551if(type <0)1552BUG("invalid type%d", type);1553 entry->in_pack_type = type;15541555/*1556 * Determine if this is a delta and if so whether we can1557 * reuse it or not. Otherwise let's find out as cheaply as1558 * possible what the actual type and size for this object is.1559 */1560switch(entry->in_pack_type) {1561default:1562/* Not a delta hence we've already got all we need. */1563oe_set_type(entry, entry->in_pack_type);1564SET_SIZE(entry, in_pack_size);1565 entry->in_pack_header_size = used;1566if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1567goto give_up;1568unuse_pack(&w_curs);1569return;1570case OBJ_REF_DELTA:1571if(reuse_delta && !entry->preferred_base)1572 base_ref =use_pack(p, &w_curs,1573 entry->in_pack_offset + used, NULL);1574 entry->in_pack_header_size = used + the_hash_algo->rawsz;1575break;1576case OBJ_OFS_DELTA:1577 buf =use_pack(p, &w_curs,1578 entry->in_pack_offset + used, NULL);1579 used_0 =0;1580 c = buf[used_0++];1581 ofs = c &127;1582while(c &128) {1583 ofs +=1;1584if(!ofs ||MSB(ofs,7)) {1585error(_("delta base offset overflow in pack for%s"),1586oid_to_hex(&entry->idx.oid));1587goto give_up;1588}1589 c = buf[used_0++];1590 ofs = (ofs <<7) + (c &127);1591}1592 ofs = entry->in_pack_offset - ofs;1593if(ofs <=0|| ofs >= entry->in_pack_offset) {1594error(_("delta base offset out of bound for%s"),1595oid_to_hex(&entry->idx.oid));1596goto give_up;1597}1598if(reuse_delta && !entry->preferred_base) {1599struct revindex_entry *revidx;1600 revidx =find_pack_revindex(p, ofs);1601if(!revidx)1602goto give_up;1603 base_ref =nth_packed_object_sha1(p, revidx->nr);1604}1605 entry->in_pack_header_size = used + used_0;1606break;1607}16081609if(can_reuse_delta(base_ref, entry, &base_entry)) {1610oe_set_type(entry, entry->in_pack_type);1611SET_SIZE(entry, in_pack_size);/* delta size */1612SET_DELTA_SIZE(entry, in_pack_size);16131614if(base_entry) {1615SET_DELTA(entry, base_entry);1616 entry->delta_sibling_idx = base_entry->delta_child_idx;1617SET_DELTA_CHILD(base_entry, entry);1618}else{1619SET_DELTA_EXT(entry, base_ref);1620}16211622unuse_pack(&w_curs);1623return;1624}16251626if(oe_type(entry)) {1627 off_t delta_pos;16281629/*1630 * This must be a delta and we already know what the1631 * final object type is. Let's extract the actual1632 * object size from the delta header.1633 */1634 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1635 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1636if(canonical_size ==0)1637goto give_up;1638SET_SIZE(entry, canonical_size);1639unuse_pack(&w_curs);1640return;1641}16421643/*1644 * No choice but to fall back to the recursive delta walk1645 * with sha1_object_info() to find about the object type1646 * at this point...1647 */1648 give_up:1649unuse_pack(&w_curs);1650}16511652oe_set_type(entry,1653oid_object_info(the_repository, &entry->idx.oid, &canonical_size));1654if(entry->type_valid) {1655SET_SIZE(entry, canonical_size);1656}else{1657/*1658 * Bad object type is checked in prepare_pack(). This is1659 * to permit a missing preferred base object to be ignored1660 * as a preferred base. Doing so can result in a larger1661 * pack file, but the transfer will still take place.1662 */1663}1664}16651666static intpack_offset_sort(const void*_a,const void*_b)1667{1668const struct object_entry *a = *(struct object_entry **)_a;1669const struct object_entry *b = *(struct object_entry **)_b;1670const struct packed_git *a_in_pack =IN_PACK(a);1671const struct packed_git *b_in_pack =IN_PACK(b);16721673/* avoid filesystem trashing with loose objects */1674if(!a_in_pack && !b_in_pack)1675returnoidcmp(&a->idx.oid, &b->idx.oid);16761677if(a_in_pack < b_in_pack)1678return-1;1679if(a_in_pack > b_in_pack)1680return1;1681return a->in_pack_offset < b->in_pack_offset ? -1:1682(a->in_pack_offset > b->in_pack_offset);1683}16841685/*1686 * Drop an on-disk delta we were planning to reuse. Naively, this would1687 * just involve blanking out the "delta" field, but we have to deal1688 * with some extra book-keeping:1689 *1690 * 1. Removing ourselves from the delta_sibling linked list.1691 *1692 * 2. Updating our size/type to the non-delta representation. These were1693 * either not recorded initially (size) or overwritten with the delta type1694 * (type) when check_object() decided to reuse the delta.1695 *1696 * 3. Resetting our delta depth, as we are now a base object.1697 */1698static voiddrop_reused_delta(struct object_entry *entry)1699{1700unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1701struct object_info oi = OBJECT_INFO_INIT;1702enum object_type type;1703unsigned long size;17041705while(*idx) {1706struct object_entry *oe = &to_pack.objects[*idx -1];17071708if(oe == entry)1709*idx = oe->delta_sibling_idx;1710else1711 idx = &oe->delta_sibling_idx;1712}1713SET_DELTA(entry, NULL);1714 entry->depth =0;17151716 oi.sizep = &size;1717 oi.typep = &type;1718if(packed_object_info(the_repository,IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1719/*1720 * We failed to get the info from this pack for some reason;1721 * fall back to sha1_object_info, which may find another copy.1722 * And if that fails, the error will be recorded in oe_type(entry)1723 * and dealt with in prepare_pack().1724 */1725oe_set_type(entry,1726oid_object_info(the_repository, &entry->idx.oid, &size));1727}else{1728oe_set_type(entry, type);1729}1730SET_SIZE(entry, size);1731}17321733/*1734 * Follow the chain of deltas from this entry onward, throwing away any links1735 * that cause us to hit a cycle (as determined by the DFS state flags in1736 * the entries).1737 *1738 * We also detect too-long reused chains that would violate our --depth1739 * limit.1740 */1741static voidbreak_delta_chains(struct object_entry *entry)1742{1743/*1744 * The actual depth of each object we will write is stored as an int,1745 * as it cannot exceed our int "depth" limit. But before we break1746 * changes based no that limit, we may potentially go as deep as the1747 * number of objects, which is elsewhere bounded to a uint32_t.1748 */1749uint32_t total_depth;1750struct object_entry *cur, *next;17511752for(cur = entry, total_depth =0;1753 cur;1754 cur =DELTA(cur), total_depth++) {1755if(cur->dfs_state == DFS_DONE) {1756/*1757 * We've already seen this object and know it isn't1758 * part of a cycle. We do need to append its depth1759 * to our count.1760 */1761 total_depth += cur->depth;1762break;1763}17641765/*1766 * We break cycles before looping, so an ACTIVE state (or any1767 * other cruft which made its way into the state variable)1768 * is a bug.1769 */1770if(cur->dfs_state != DFS_NONE)1771BUG("confusing delta dfs state in first pass:%d",1772 cur->dfs_state);17731774/*1775 * Now we know this is the first time we've seen the object. If1776 * it's not a delta, we're done traversing, but we'll mark it1777 * done to save time on future traversals.1778 */1779if(!DELTA(cur)) {1780 cur->dfs_state = DFS_DONE;1781break;1782}17831784/*1785 * Mark ourselves as active and see if the next step causes1786 * us to cycle to another active object. It's important to do1787 * this _before_ we loop, because it impacts where we make the1788 * cut, and thus how our total_depth counter works.1789 * E.g., We may see a partial loop like:1790 *1791 * A -> B -> C -> D -> B1792 *1793 * Cutting B->C breaks the cycle. But now the depth of A is1794 * only 1, and our total_depth counter is at 3. The size of the1795 * error is always one less than the size of the cycle we1796 * broke. Commits C and D were "lost" from A's chain.1797 *1798 * If we instead cut D->B, then the depth of A is correct at 3.1799 * We keep all commits in the chain that we examined.1800 */1801 cur->dfs_state = DFS_ACTIVE;1802if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1803drop_reused_delta(cur);1804 cur->dfs_state = DFS_DONE;1805break;1806}1807}18081809/*1810 * And now that we've gone all the way to the bottom of the chain, we1811 * need to clear the active flags and set the depth fields as1812 * appropriate. Unlike the loop above, which can quit when it drops a1813 * delta, we need to keep going to look for more depth cuts. So we need1814 * an extra "next" pointer to keep going after we reset cur->delta.1815 */1816for(cur = entry; cur; cur = next) {1817 next =DELTA(cur);18181819/*1820 * We should have a chain of zero or more ACTIVE states down to1821 * a final DONE. We can quit after the DONE, because either it1822 * has no bases, or we've already handled them in a previous1823 * call.1824 */1825if(cur->dfs_state == DFS_DONE)1826break;1827else if(cur->dfs_state != DFS_ACTIVE)1828BUG("confusing delta dfs state in second pass:%d",1829 cur->dfs_state);18301831/*1832 * If the total_depth is more than depth, then we need to snip1833 * the chain into two or more smaller chains that don't exceed1834 * the maximum depth. Most of the resulting chains will contain1835 * (depth + 1) entries (i.e., depth deltas plus one base), and1836 * the last chain (i.e., the one containing entry) will contain1837 * whatever entries are left over, namely1838 * (total_depth % (depth + 1)) of them.1839 *1840 * Since we are iterating towards decreasing depth, we need to1841 * decrement total_depth as we go, and we need to write to the1842 * entry what its final depth will be after all of the1843 * snipping. Since we're snipping into chains of length (depth1844 * + 1) entries, the final depth of an entry will be its1845 * original depth modulo (depth + 1). Any time we encounter an1846 * entry whose final depth is supposed to be zero, we snip it1847 * from its delta base, thereby making it so.1848 */1849 cur->depth = (total_depth--) % (depth +1);1850if(!cur->depth)1851drop_reused_delta(cur);18521853 cur->dfs_state = DFS_DONE;1854}1855}18561857static voidget_object_details(void)1858{1859uint32_t i;1860struct object_entry **sorted_by_offset;18611862if(progress)1863 progress_state =start_progress(_("Counting objects"),1864 to_pack.nr_objects);18651866 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1867for(i =0; i < to_pack.nr_objects; i++)1868 sorted_by_offset[i] = to_pack.objects + i;1869QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);18701871for(i =0; i < to_pack.nr_objects; i++) {1872struct object_entry *entry = sorted_by_offset[i];1873check_object(entry);1874if(entry->type_valid &&1875oe_size_greater_than(&to_pack, entry, big_file_threshold))1876 entry->no_try_delta =1;1877display_progress(progress_state, i +1);1878}1879stop_progress(&progress_state);18801881/*1882 * This must happen in a second pass, since we rely on the delta1883 * information for the whole list being completed.1884 */1885for(i =0; i < to_pack.nr_objects; i++)1886break_delta_chains(&to_pack.objects[i]);18871888free(sorted_by_offset);1889}18901891/*1892 * We search for deltas in a list sorted by type, by filename hash, and then1893 * by size, so that we see progressively smaller and smaller files.1894 * That's because we prefer deltas to be from the bigger file1895 * to the smaller -- deletes are potentially cheaper, but perhaps1896 * more importantly, the bigger file is likely the more recent1897 * one. The deepest deltas are therefore the oldest objects which are1898 * less susceptible to be accessed often.1899 */1900static inttype_size_sort(const void*_a,const void*_b)1901{1902const struct object_entry *a = *(struct object_entry **)_a;1903const struct object_entry *b = *(struct object_entry **)_b;1904enum object_type a_type =oe_type(a);1905enum object_type b_type =oe_type(b);1906unsigned long a_size =SIZE(a);1907unsigned long b_size =SIZE(b);19081909if(a_type > b_type)1910return-1;1911if(a_type < b_type)1912return1;1913if(a->hash > b->hash)1914return-1;1915if(a->hash < b->hash)1916return1;1917if(a->preferred_base > b->preferred_base)1918return-1;1919if(a->preferred_base < b->preferred_base)1920return1;1921if(use_delta_islands) {1922int island_cmp =island_delta_cmp(&a->idx.oid, &b->idx.oid);1923if(island_cmp)1924return island_cmp;1925}1926if(a_size > b_size)1927return-1;1928if(a_size < b_size)1929return1;1930return a < b ? -1: (a > b);/* newest first */1931}19321933struct unpacked {1934struct object_entry *entry;1935void*data;1936struct delta_index *index;1937unsigned depth;1938};19391940static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1941unsigned long delta_size)1942{1943if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1944return0;19451946if(delta_size < cache_max_small_delta_size)1947return1;19481949/* cache delta, if objects are large enough compared to delta size */1950if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1951return1;19521953return0;1954}19551956#ifndef NO_PTHREADS19571958/* Protect access to object database */1959static pthread_mutex_t read_mutex;1960#define read_lock() pthread_mutex_lock(&read_mutex)1961#define read_unlock() pthread_mutex_unlock(&read_mutex)19621963/* Protect delta_cache_size */1964static pthread_mutex_t cache_mutex;1965#define cache_lock() pthread_mutex_lock(&cache_mutex)1966#define cache_unlock() pthread_mutex_unlock(&cache_mutex)19671968/*1969 * Protect object list partitioning (e.g. struct thread_param) and1970 * progress_state1971 */1972static pthread_mutex_t progress_mutex;1973#define progress_lock() pthread_mutex_lock(&progress_mutex)1974#define progress_unlock() pthread_mutex_unlock(&progress_mutex)19751976/*1977 * Access to struct object_entry is unprotected since each thread owns1978 * a portion of the main object list. Just don't access object entries1979 * ahead in the list because they can be stolen and would need1980 * progress_mutex for protection.1981 */1982#else19831984#define read_lock() (void)01985#define read_unlock() (void)01986#define cache_lock() (void)01987#define cache_unlock() (void)01988#define progress_lock() (void)01989#define progress_unlock() (void)019901991#endif19921993/*1994 * Return the size of the object without doing any delta1995 * reconstruction (so non-deltas are true object sizes, but deltas1996 * return the size of the delta data).1997 */1998unsigned longoe_get_size_slow(struct packing_data *pack,1999const struct object_entry *e)2000{2001struct packed_git *p;2002struct pack_window *w_curs;2003unsigned char*buf;2004enum object_type type;2005unsigned long used, avail, size;20062007if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {2008read_lock();2009if(oid_object_info(the_repository, &e->idx.oid, &size) <0)2010die(_("unable to get size of%s"),2011oid_to_hex(&e->idx.oid));2012read_unlock();2013return size;2014}20152016 p =oe_in_pack(pack, e);2017if(!p)2018BUG("when e->type is a delta, it must belong to a pack");20192020read_lock();2021 w_curs = NULL;2022 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);2023 used =unpack_object_header_buffer(buf, avail, &type, &size);2024if(used ==0)2025die(_("unable to parse object header of%s"),2026oid_to_hex(&e->idx.oid));20272028unuse_pack(&w_curs);2029read_unlock();2030return size;2031}20322033static inttry_delta(struct unpacked *trg,struct unpacked *src,2034unsigned max_depth,unsigned long*mem_usage)2035{2036struct object_entry *trg_entry = trg->entry;2037struct object_entry *src_entry = src->entry;2038unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;2039unsigned ref_depth;2040enum object_type type;2041void*delta_buf;20422043/* Don't bother doing diffs between different types */2044if(oe_type(trg_entry) !=oe_type(src_entry))2045return-1;20462047/*2048 * We do not bother to try a delta that we discarded on an2049 * earlier try, but only when reusing delta data. Note that2050 * src_entry that is marked as the preferred_base should always2051 * be considered, as even if we produce a suboptimal delta against2052 * it, we will still save the transfer cost, as we already know2053 * the other side has it and we won't send src_entry at all.2054 */2055if(reuse_delta &&IN_PACK(trg_entry) &&2056IN_PACK(trg_entry) ==IN_PACK(src_entry) &&2057!src_entry->preferred_base &&2058 trg_entry->in_pack_type != OBJ_REF_DELTA &&2059 trg_entry->in_pack_type != OBJ_OFS_DELTA)2060return0;20612062/* Let's not bust the allowed depth. */2063if(src->depth >= max_depth)2064return0;20652066/* Now some size filtering heuristics. */2067 trg_size =SIZE(trg_entry);2068if(!DELTA(trg_entry)) {2069 max_size = trg_size/2- the_hash_algo->rawsz;2070 ref_depth =1;2071}else{2072 max_size =DELTA_SIZE(trg_entry);2073 ref_depth = trg->depth;2074}2075 max_size = (uint64_t)max_size * (max_depth - src->depth) /2076(max_depth - ref_depth +1);2077if(max_size ==0)2078return0;2079 src_size =SIZE(src_entry);2080 sizediff = src_size < trg_size ? trg_size - src_size :0;2081if(sizediff >= max_size)2082return0;2083if(trg_size < src_size /32)2084return0;20852086if(!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))2087return0;20882089/* Load data if not already done */2090if(!trg->data) {2091read_lock();2092 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);2093read_unlock();2094if(!trg->data)2095die(_("object%scannot be read"),2096oid_to_hex(&trg_entry->idx.oid));2097if(sz != trg_size)2098die(_("object%sinconsistent object length (%lu vs%lu)"),2099oid_to_hex(&trg_entry->idx.oid), sz,2100 trg_size);2101*mem_usage += sz;2102}2103if(!src->data) {2104read_lock();2105 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);2106read_unlock();2107if(!src->data) {2108if(src_entry->preferred_base) {2109static int warned =0;2110if(!warned++)2111warning(_("object%scannot be read"),2112oid_to_hex(&src_entry->idx.oid));2113/*2114 * Those objects are not included in the2115 * resulting pack. Be resilient and ignore2116 * them if they can't be read, in case the2117 * pack could be created nevertheless.2118 */2119return0;2120}2121die(_("object%scannot be read"),2122oid_to_hex(&src_entry->idx.oid));2123}2124if(sz != src_size)2125die(_("object%sinconsistent object length (%lu vs%lu)"),2126oid_to_hex(&src_entry->idx.oid), sz,2127 src_size);2128*mem_usage += sz;2129}2130if(!src->index) {2131 src->index =create_delta_index(src->data, src_size);2132if(!src->index) {2133static int warned =0;2134if(!warned++)2135warning(_("suboptimal pack - out of memory"));2136return0;2137}2138*mem_usage +=sizeof_delta_index(src->index);2139}21402141 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2142if(!delta_buf)2143return0;21442145if(DELTA(trg_entry)) {2146/* Prefer only shallower same-sized deltas. */2147if(delta_size ==DELTA_SIZE(trg_entry) &&2148 src->depth +1>= trg->depth) {2149free(delta_buf);2150return0;2151}2152}21532154/*2155 * Handle memory allocation outside of the cache2156 * accounting lock. Compiler will optimize the strangeness2157 * away when NO_PTHREADS is defined.2158 */2159free(trg_entry->delta_data);2160cache_lock();2161if(trg_entry->delta_data) {2162 delta_cache_size -=DELTA_SIZE(trg_entry);2163 trg_entry->delta_data = NULL;2164}2165if(delta_cacheable(src_size, trg_size, delta_size)) {2166 delta_cache_size += delta_size;2167cache_unlock();2168 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2169}else{2170cache_unlock();2171free(delta_buf);2172}21732174SET_DELTA(trg_entry, src_entry);2175SET_DELTA_SIZE(trg_entry, delta_size);2176 trg->depth = src->depth +1;21772178return1;2179}21802181static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2182{2183struct object_entry *child =DELTA_CHILD(me);2184unsigned int m = n;2185while(child) {2186unsigned int c =check_delta_limit(child, n +1);2187if(m < c)2188 m = c;2189 child =DELTA_SIBLING(child);2190}2191return m;2192}21932194static unsigned longfree_unpacked(struct unpacked *n)2195{2196unsigned long freed_mem =sizeof_delta_index(n->index);2197free_delta_index(n->index);2198 n->index = NULL;2199if(n->data) {2200 freed_mem +=SIZE(n->entry);2201FREE_AND_NULL(n->data);2202}2203 n->entry = NULL;2204 n->depth =0;2205return freed_mem;2206}22072208static voidfind_deltas(struct object_entry **list,unsigned*list_size,2209int window,int depth,unsigned*processed)2210{2211uint32_t i, idx =0, count =0;2212struct unpacked *array;2213unsigned long mem_usage =0;22142215 array =xcalloc(window,sizeof(struct unpacked));22162217for(;;) {2218struct object_entry *entry;2219struct unpacked *n = array + idx;2220int j, max_depth, best_base = -1;22212222progress_lock();2223if(!*list_size) {2224progress_unlock();2225break;2226}2227 entry = *list++;2228(*list_size)--;2229if(!entry->preferred_base) {2230(*processed)++;2231display_progress(progress_state, *processed);2232}2233progress_unlock();22342235 mem_usage -=free_unpacked(n);2236 n->entry = entry;22372238while(window_memory_limit &&2239 mem_usage > window_memory_limit &&2240 count >1) {2241uint32_t tail = (idx + window - count) % window;2242 mem_usage -=free_unpacked(array + tail);2243 count--;2244}22452246/* We do not compute delta to *create* objects we are not2247 * going to pack.2248 */2249if(entry->preferred_base)2250goto next;22512252/*2253 * If the current object is at pack edge, take the depth the2254 * objects that depend on the current object into account2255 * otherwise they would become too deep.2256 */2257 max_depth = depth;2258if(DELTA_CHILD(entry)) {2259 max_depth -=check_delta_limit(entry,0);2260if(max_depth <=0)2261goto next;2262}22632264 j = window;2265while(--j >0) {2266int ret;2267uint32_t other_idx = idx + j;2268struct unpacked *m;2269if(other_idx >= window)2270 other_idx -= window;2271 m = array + other_idx;2272if(!m->entry)2273break;2274 ret =try_delta(n, m, max_depth, &mem_usage);2275if(ret <0)2276break;2277else if(ret >0)2278 best_base = other_idx;2279}22802281/*2282 * If we decided to cache the delta data, then it is best2283 * to compress it right away. First because we have to do2284 * it anyway, and doing it here while we're threaded will2285 * save a lot of time in the non threaded write phase,2286 * as well as allow for caching more deltas within2287 * the same cache size limit.2288 * ...2289 * But only if not writing to stdout, since in that case2290 * the network is most likely throttling writes anyway,2291 * and therefore it is best to go to the write phase ASAP2292 * instead, as we can afford spending more time compressing2293 * between writes at that moment.2294 */2295if(entry->delta_data && !pack_to_stdout) {2296unsigned long size;22972298 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2299if(size < (1U<< OE_Z_DELTA_BITS)) {2300 entry->z_delta_size = size;2301cache_lock();2302 delta_cache_size -=DELTA_SIZE(entry);2303 delta_cache_size += entry->z_delta_size;2304cache_unlock();2305}else{2306FREE_AND_NULL(entry->delta_data);2307 entry->z_delta_size =0;2308}2309}23102311/* if we made n a delta, and if n is already at max2312 * depth, leaving it in the window is pointless. we2313 * should evict it first.2314 */2315if(DELTA(entry) && max_depth <= n->depth)2316continue;23172318/*2319 * Move the best delta base up in the window, after the2320 * currently deltified object, to keep it longer. It will2321 * be the first base object to be attempted next.2322 */2323if(DELTA(entry)) {2324struct unpacked swap = array[best_base];2325int dist = (window + idx - best_base) % window;2326int dst = best_base;2327while(dist--) {2328int src = (dst +1) % window;2329 array[dst] = array[src];2330 dst = src;2331}2332 array[dst] = swap;2333}23342335 next:2336 idx++;2337if(count +1< window)2338 count++;2339if(idx >= window)2340 idx =0;2341}23422343for(i =0; i < window; ++i) {2344free_delta_index(array[i].index);2345free(array[i].data);2346}2347free(array);2348}23492350#ifndef NO_PTHREADS23512352static voidtry_to_free_from_threads(size_t size)2353{2354read_lock();2355release_pack_memory(size);2356read_unlock();2357}23582359static try_to_free_t old_try_to_free_routine;23602361/*2362 * The main object list is split into smaller lists, each is handed to2363 * one worker.2364 *2365 * The main thread waits on the condition that (at least) one of the workers2366 * has stopped working (which is indicated in the .working member of2367 * struct thread_params).2368 *2369 * When a work thread has completed its work, it sets .working to 0 and2370 * signals the main thread and waits on the condition that .data_ready2371 * becomes 1.2372 *2373 * The main thread steals half of the work from the worker that has2374 * most work left to hand it to the idle worker.2375 */23762377struct thread_params {2378 pthread_t thread;2379struct object_entry **list;2380unsigned list_size;2381unsigned remaining;2382int window;2383int depth;2384int working;2385int data_ready;2386 pthread_mutex_t mutex;2387 pthread_cond_t cond;2388unsigned*processed;2389};23902391static pthread_cond_t progress_cond;23922393/*2394 * Mutex and conditional variable can't be statically-initialized on Windows.2395 */2396static voidinit_threaded_search(void)2397{2398init_recursive_mutex(&read_mutex);2399pthread_mutex_init(&cache_mutex, NULL);2400pthread_mutex_init(&progress_mutex, NULL);2401pthread_cond_init(&progress_cond, NULL);2402 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2403}24042405static voidcleanup_threaded_search(void)2406{2407set_try_to_free_routine(old_try_to_free_routine);2408pthread_cond_destroy(&progress_cond);2409pthread_mutex_destroy(&read_mutex);2410pthread_mutex_destroy(&cache_mutex);2411pthread_mutex_destroy(&progress_mutex);2412}24132414static void*threaded_find_deltas(void*arg)2415{2416struct thread_params *me = arg;24172418progress_lock();2419while(me->remaining) {2420progress_unlock();24212422find_deltas(me->list, &me->remaining,2423 me->window, me->depth, me->processed);24242425progress_lock();2426 me->working =0;2427pthread_cond_signal(&progress_cond);2428progress_unlock();24292430/*2431 * We must not set ->data_ready before we wait on the2432 * condition because the main thread may have set it to 12433 * before we get here. In order to be sure that new2434 * work is available if we see 1 in ->data_ready, it2435 * was initialized to 0 before this thread was spawned2436 * and we reset it to 0 right away.2437 */2438pthread_mutex_lock(&me->mutex);2439while(!me->data_ready)2440pthread_cond_wait(&me->cond, &me->mutex);2441 me->data_ready =0;2442pthread_mutex_unlock(&me->mutex);24432444progress_lock();2445}2446progress_unlock();2447/* leave ->working 1 so that this doesn't get more work assigned */2448return NULL;2449}24502451static voidll_find_deltas(struct object_entry **list,unsigned list_size,2452int window,int depth,unsigned*processed)2453{2454struct thread_params *p;2455int i, ret, active_threads =0;24562457init_threaded_search();24582459if(delta_search_threads <=1) {2460find_deltas(list, &list_size, window, depth, processed);2461cleanup_threaded_search();2462return;2463}2464if(progress > pack_to_stdout)2465fprintf_ln(stderr,_("Delta compression using up to%dthreads"),2466 delta_search_threads);2467 p =xcalloc(delta_search_threads,sizeof(*p));24682469/* Partition the work amongst work threads. */2470for(i =0; i < delta_search_threads; i++) {2471unsigned sub_size = list_size / (delta_search_threads - i);24722473/* don't use too small segments or no deltas will be found */2474if(sub_size <2*window && i+1< delta_search_threads)2475 sub_size =0;24762477 p[i].window = window;2478 p[i].depth = depth;2479 p[i].processed = processed;2480 p[i].working =1;2481 p[i].data_ready =0;24822483/* try to split chunks on "path" boundaries */2484while(sub_size && sub_size < list_size &&2485 list[sub_size]->hash &&2486 list[sub_size]->hash == list[sub_size-1]->hash)2487 sub_size++;24882489 p[i].list = list;2490 p[i].list_size = sub_size;2491 p[i].remaining = sub_size;24922493 list += sub_size;2494 list_size -= sub_size;2495}24962497/* Start work threads. */2498for(i =0; i < delta_search_threads; i++) {2499if(!p[i].list_size)2500continue;2501pthread_mutex_init(&p[i].mutex, NULL);2502pthread_cond_init(&p[i].cond, NULL);2503 ret =pthread_create(&p[i].thread, NULL,2504 threaded_find_deltas, &p[i]);2505if(ret)2506die(_("unable to create thread:%s"),strerror(ret));2507 active_threads++;2508}25092510/*2511 * Now let's wait for work completion. Each time a thread is done2512 * with its work, we steal half of the remaining work from the2513 * thread with the largest number of unprocessed objects and give2514 * it to that newly idle thread. This ensure good load balancing2515 * until the remaining object list segments are simply too short2516 * to be worth splitting anymore.2517 */2518while(active_threads) {2519struct thread_params *target = NULL;2520struct thread_params *victim = NULL;2521unsigned sub_size =0;25222523progress_lock();2524for(;;) {2525for(i =0; !target && i < delta_search_threads; i++)2526if(!p[i].working)2527 target = &p[i];2528if(target)2529break;2530pthread_cond_wait(&progress_cond, &progress_mutex);2531}25322533for(i =0; i < delta_search_threads; i++)2534if(p[i].remaining >2*window &&2535(!victim || victim->remaining < p[i].remaining))2536 victim = &p[i];2537if(victim) {2538 sub_size = victim->remaining /2;2539 list = victim->list + victim->list_size - sub_size;2540while(sub_size && list[0]->hash &&2541 list[0]->hash == list[-1]->hash) {2542 list++;2543 sub_size--;2544}2545if(!sub_size) {2546/*2547 * It is possible for some "paths" to have2548 * so many objects that no hash boundary2549 * might be found. Let's just steal the2550 * exact half in that case.2551 */2552 sub_size = victim->remaining /2;2553 list -= sub_size;2554}2555 target->list = list;2556 victim->list_size -= sub_size;2557 victim->remaining -= sub_size;2558}2559 target->list_size = sub_size;2560 target->remaining = sub_size;2561 target->working =1;2562progress_unlock();25632564pthread_mutex_lock(&target->mutex);2565 target->data_ready =1;2566pthread_cond_signal(&target->cond);2567pthread_mutex_unlock(&target->mutex);25682569if(!sub_size) {2570pthread_join(target->thread, NULL);2571pthread_cond_destroy(&target->cond);2572pthread_mutex_destroy(&target->mutex);2573 active_threads--;2574}2575}2576cleanup_threaded_search();2577free(p);2578}25792580#else2581#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2582#endif25832584static voidadd_tag_chain(const struct object_id *oid)2585{2586struct tag *tag;25872588/*2589 * We catch duplicates already in add_object_entry(), but we'd2590 * prefer to do this extra check to avoid having to parse the2591 * tag at all if we already know that it's being packed (e.g., if2592 * it was included via bitmaps, we would not have parsed it2593 * previously).2594 */2595if(packlist_find(&to_pack, oid->hash, NULL))2596return;25972598 tag =lookup_tag(the_repository, oid);2599while(1) {2600if(!tag ||parse_tag(tag) || !tag->tagged)2601die(_("unable to pack objects reachable from tag%s"),2602oid_to_hex(oid));26032604add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);26052606if(tag->tagged->type != OBJ_TAG)2607return;26082609 tag = (struct tag *)tag->tagged;2610}2611}26122613static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2614{2615struct object_id peeled;26162617if(starts_with(path,"refs/tags/") &&/* is a tag? */2618!peel_ref(path, &peeled) &&/* peelable? */2619packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2620add_tag_chain(oid);2621return0;2622}26232624static voidprepare_pack(int window,int depth)2625{2626struct object_entry **delta_list;2627uint32_t i, nr_deltas;2628unsigned n;26292630if(use_delta_islands)2631resolve_tree_islands(progress, &to_pack);26322633get_object_details();26342635/*2636 * If we're locally repacking then we need to be doubly careful2637 * from now on in order to make sure no stealth corruption gets2638 * propagated to the new pack. Clients receiving streamed packs2639 * should validate everything they get anyway so no need to incur2640 * the additional cost here in that case.2641 */2642if(!pack_to_stdout)2643 do_check_packed_object_crc =1;26442645if(!to_pack.nr_objects || !window || !depth)2646return;26472648ALLOC_ARRAY(delta_list, to_pack.nr_objects);2649 nr_deltas = n =0;26502651for(i =0; i < to_pack.nr_objects; i++) {2652struct object_entry *entry = to_pack.objects + i;26532654if(DELTA(entry))2655/* This happens if we decided to reuse existing2656 * delta from a pack. "reuse_delta &&" is implied.2657 */2658continue;26592660if(!entry->type_valid ||2661oe_size_less_than(&to_pack, entry,50))2662continue;26632664if(entry->no_try_delta)2665continue;26662667if(!entry->preferred_base) {2668 nr_deltas++;2669if(oe_type(entry) <0)2670die(_("unable to get type of object%s"),2671oid_to_hex(&entry->idx.oid));2672}else{2673if(oe_type(entry) <0) {2674/*2675 * This object is not found, but we2676 * don't have to include it anyway.2677 */2678continue;2679}2680}26812682 delta_list[n++] = entry;2683}26842685if(nr_deltas && n >1) {2686unsigned nr_done =0;2687if(progress)2688 progress_state =start_progress(_("Compressing objects"),2689 nr_deltas);2690QSORT(delta_list, n, type_size_sort);2691ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2692stop_progress(&progress_state);2693if(nr_done != nr_deltas)2694die(_("inconsistency with delta count"));2695}2696free(delta_list);2697}26982699static intgit_pack_config(const char*k,const char*v,void*cb)2700{2701if(!strcmp(k,"pack.window")) {2702 window =git_config_int(k, v);2703return0;2704}2705if(!strcmp(k,"pack.windowmemory")) {2706 window_memory_limit =git_config_ulong(k, v);2707return0;2708}2709if(!strcmp(k,"pack.depth")) {2710 depth =git_config_int(k, v);2711return0;2712}2713if(!strcmp(k,"pack.deltacachesize")) {2714 max_delta_cache_size =git_config_int(k, v);2715return0;2716}2717if(!strcmp(k,"pack.deltacachelimit")) {2718 cache_max_small_delta_size =git_config_int(k, v);2719return0;2720}2721if(!strcmp(k,"pack.writebitmaphashcache")) {2722if(git_config_bool(k, v))2723 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2724else2725 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2726}2727if(!strcmp(k,"pack.usebitmaps")) {2728 use_bitmap_index_default =git_config_bool(k, v);2729return0;2730}2731if(!strcmp(k,"pack.threads")) {2732 delta_search_threads =git_config_int(k, v);2733if(delta_search_threads <0)2734die(_("invalid number of threads specified (%d)"),2735 delta_search_threads);2736#ifdef NO_PTHREADS2737if(delta_search_threads !=1) {2738warning(_("no threads support, ignoring%s"), k);2739 delta_search_threads =0;2740}2741#endif2742return0;2743}2744if(!strcmp(k,"pack.indexversion")) {2745 pack_idx_opts.version =git_config_int(k, v);2746if(pack_idx_opts.version >2)2747die(_("bad pack.indexversion=%"PRIu32),2748 pack_idx_opts.version);2749return0;2750}2751returngit_default_config(k, v, cb);2752}27532754static voidread_object_list_from_stdin(void)2755{2756char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2757struct object_id oid;2758const char*p;27592760for(;;) {2761if(!fgets(line,sizeof(line), stdin)) {2762if(feof(stdin))2763break;2764if(!ferror(stdin))2765die("BUG: fgets returned NULL, not EOF, not error!");2766if(errno != EINTR)2767die_errno("fgets");2768clearerr(stdin);2769continue;2770}2771if(line[0] =='-') {2772if(get_oid_hex(line+1, &oid))2773die(_("expected edge object ID, got garbage:\n%s"),2774 line);2775add_preferred_base(&oid);2776continue;2777}2778if(parse_oid_hex(line, &oid, &p))2779die(_("expected object ID, got garbage:\n%s"), line);27802781add_preferred_base_object(p +1);2782add_object_entry(&oid, OBJ_NONE, p +1,0);2783}2784}27852786/* Remember to update object flag allocation in object.h */2787#define OBJECT_ADDED (1u<<20)27882789static voidshow_commit(struct commit *commit,void*data)2790{2791add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2792 commit->object.flags |= OBJECT_ADDED;27932794if(write_bitmap_index)2795index_commit_for_bitmap(commit);27962797if(use_delta_islands)2798propagate_island_marks(commit);2799}28002801static voidshow_object(struct object *obj,const char*name,void*data)2802{2803add_preferred_base_object(name);2804add_object_entry(&obj->oid, obj->type, name,0);2805 obj->flags |= OBJECT_ADDED;28062807if(use_delta_islands) {2808const char*p;2809unsigned depth =0;2810struct object_entry *ent;28112812for(p =strchr(name,'/'); p; p =strchr(p +1,'/'))2813 depth++;28142815 ent =packlist_find(&to_pack, obj->oid.hash, NULL);2816if(ent && depth >oe_tree_depth(&to_pack, ent))2817oe_set_tree_depth(&to_pack, ent, depth);2818}2819}28202821static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2822{2823assert(arg_missing_action == MA_ALLOW_ANY);28242825/*2826 * Quietly ignore ALL missing objects. This avoids problems with2827 * staging them now and getting an odd error later.2828 */2829if(!has_object_file(&obj->oid))2830return;28312832show_object(obj, name, data);2833}28342835static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2836{2837assert(arg_missing_action == MA_ALLOW_PROMISOR);28382839/*2840 * Quietly ignore EXPECTED missing objects. This avoids problems with2841 * staging them now and getting an odd error later.2842 */2843if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2844return;28452846show_object(obj, name, data);2847}28482849static intoption_parse_missing_action(const struct option *opt,2850const char*arg,int unset)2851{2852assert(arg);2853assert(!unset);28542855if(!strcmp(arg,"error")) {2856 arg_missing_action = MA_ERROR;2857 fn_show_object = show_object;2858return0;2859}28602861if(!strcmp(arg,"allow-any")) {2862 arg_missing_action = MA_ALLOW_ANY;2863 fetch_if_missing =0;2864 fn_show_object = show_object__ma_allow_any;2865return0;2866}28672868if(!strcmp(arg,"allow-promisor")) {2869 arg_missing_action = MA_ALLOW_PROMISOR;2870 fetch_if_missing =0;2871 fn_show_object = show_object__ma_allow_promisor;2872return0;2873}28742875die(_("invalid value for --missing"));2876return0;2877}28782879static voidshow_edge(struct commit *commit)2880{2881add_preferred_base(&commit->object.oid);2882}28832884struct in_pack_object {2885 off_t offset;2886struct object *object;2887};28882889struct in_pack {2890unsigned int alloc;2891unsigned int nr;2892struct in_pack_object *array;2893};28942895static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2896{2897 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2898 in_pack->array[in_pack->nr].object = object;2899 in_pack->nr++;2900}29012902/*2903 * Compare the objects in the offset order, in order to emulate the2904 * "git rev-list --objects" output that produced the pack originally.2905 */2906static intofscmp(const void*a_,const void*b_)2907{2908struct in_pack_object *a = (struct in_pack_object *)a_;2909struct in_pack_object *b = (struct in_pack_object *)b_;29102911if(a->offset < b->offset)2912return-1;2913else if(a->offset > b->offset)2914return1;2915else2916returnoidcmp(&a->object->oid, &b->object->oid);2917}29182919static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2920{2921struct packed_git *p;2922struct in_pack in_pack;2923uint32_t i;29242925memset(&in_pack,0,sizeof(in_pack));29262927for(p =get_all_packs(the_repository); p; p = p->next) {2928struct object_id oid;2929struct object *o;29302931if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2932continue;2933if(open_pack_index(p))2934die(_("cannot open pack index"));29352936ALLOC_GROW(in_pack.array,2937 in_pack.nr + p->num_objects,2938 in_pack.alloc);29392940for(i =0; i < p->num_objects; i++) {2941nth_packed_object_oid(&oid, p, i);2942 o =lookup_unknown_object(oid.hash);2943if(!(o->flags & OBJECT_ADDED))2944mark_in_pack_object(o, p, &in_pack);2945 o->flags |= OBJECT_ADDED;2946}2947}29482949if(in_pack.nr) {2950QSORT(in_pack.array, in_pack.nr, ofscmp);2951for(i =0; i < in_pack.nr; i++) {2952struct object *o = in_pack.array[i].object;2953add_object_entry(&o->oid, o->type,"",0);2954}2955}2956free(in_pack.array);2957}29582959static intadd_loose_object(const struct object_id *oid,const char*path,2960void*data)2961{2962enum object_type type =oid_object_info(the_repository, oid, NULL);29632964if(type <0) {2965warning(_("loose object at%scould not be examined"), path);2966return0;2967}29682969add_object_entry(oid, type,"",0);2970return0;2971}29722973/*2974 * We actually don't even have to worry about reachability here.2975 * add_object_entry will weed out duplicates, so we just add every2976 * loose object we find.2977 */2978static voidadd_unreachable_loose_objects(void)2979{2980for_each_loose_file_in_objdir(get_object_directory(),2981 add_loose_object,2982 NULL, NULL, NULL);2983}29842985static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2986{2987static struct packed_git *last_found = (void*)1;2988struct packed_git *p;29892990 p = (last_found != (void*)1) ? last_found :2991get_all_packs(the_repository);29922993while(p) {2994if((!p->pack_local || p->pack_keep ||2995 p->pack_keep_in_core) &&2996find_pack_entry_one(oid->hash, p)) {2997 last_found = p;2998return1;2999}3000if(p == last_found)3001 p =get_all_packs(the_repository);3002else3003 p = p->next;3004if(p == last_found)3005 p = p->next;3006}3007return0;3008}30093010/*3011 * Store a list of sha1s that are should not be discarded3012 * because they are either written too recently, or are3013 * reachable from another object that was.3014 *3015 * This is filled by get_object_list.3016 */3017static struct oid_array recent_objects;30183019static intloosened_object_can_be_discarded(const struct object_id *oid,3020 timestamp_t mtime)3021{3022if(!unpack_unreachable_expiration)3023return0;3024if(mtime > unpack_unreachable_expiration)3025return0;3026if(oid_array_lookup(&recent_objects, oid) >=0)3027return0;3028return1;3029}30303031static voidloosen_unused_packed_objects(struct rev_info *revs)3032{3033struct packed_git *p;3034uint32_t i;3035struct object_id oid;30363037for(p =get_all_packs(the_repository); p; p = p->next) {3038if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)3039continue;30403041if(open_pack_index(p))3042die(_("cannot open pack index"));30433044for(i =0; i < p->num_objects; i++) {3045nth_packed_object_oid(&oid, p, i);3046if(!packlist_find(&to_pack, oid.hash, NULL) &&3047!has_sha1_pack_kept_or_nonlocal(&oid) &&3048!loosened_object_can_be_discarded(&oid, p->mtime))3049if(force_object_loose(&oid, p->mtime))3050die(_("unable to force loose object"));3051}3052}3053}30543055/*3056 * This tracks any options which pack-reuse code expects to be on, or which a3057 * reader of the pack might not understand, and which would therefore prevent3058 * blind reuse of what we have on disk.3059 */3060static intpack_options_allow_reuse(void)3061{3062return pack_to_stdout &&3063 allow_ofs_delta &&3064!ignore_packed_keep_on_disk &&3065!ignore_packed_keep_in_core &&3066(!local || !have_non_local_packs) &&3067!incremental;3068}30693070static intget_object_list_from_bitmap(struct rev_info *revs)3071{3072if(!(bitmap_git =prepare_bitmap_walk(revs)))3073return-1;30743075if(pack_options_allow_reuse() &&3076!reuse_partial_packfile_from_bitmap(3077 bitmap_git,3078&reuse_packfile,3079&reuse_packfile_objects,3080&reuse_packfile_offset)) {3081assert(reuse_packfile_objects);3082 nr_result += reuse_packfile_objects;3083display_progress(progress_state, nr_result);3084}30853086traverse_bitmap_commit_list(bitmap_git, &add_object_entry_from_bitmap);3087return0;3088}30893090static voidrecord_recent_object(struct object *obj,3091const char*name,3092void*data)3093{3094oid_array_append(&recent_objects, &obj->oid);3095}30963097static voidrecord_recent_commit(struct commit *commit,void*data)3098{3099oid_array_append(&recent_objects, &commit->object.oid);3100}31013102static voidget_object_list(int ac,const char**av)3103{3104struct rev_info revs;3105char line[1000];3106int flags =0;31073108repo_init_revisions(the_repository, &revs, NULL);3109 save_commit_buffer =0;3110 revs.allow_exclude_promisor_objects_opt =1;3111setup_revisions(ac, av, &revs, NULL);31123113/* make sure shallows are read */3114is_repository_shallow(the_repository);31153116while(fgets(line,sizeof(line), stdin) != NULL) {3117int len =strlen(line);3118if(len && line[len -1] =='\n')3119 line[--len] =0;3120if(!len)3121break;3122if(*line =='-') {3123if(!strcmp(line,"--not")) {3124 flags ^= UNINTERESTING;3125 write_bitmap_index =0;3126continue;3127}3128if(starts_with(line,"--shallow ")) {3129struct object_id oid;3130if(get_oid_hex(line +10, &oid))3131die("not an SHA-1 '%s'", line +10);3132register_shallow(the_repository, &oid);3133 use_bitmap_index =0;3134continue;3135}3136die(_("not a rev '%s'"), line);3137}3138if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))3139die(_("bad revision '%s'"), line);3140}31413142if(use_bitmap_index && !get_object_list_from_bitmap(&revs))3143return;31443145if(use_delta_islands)3146load_delta_islands();31473148if(prepare_revision_walk(&revs))3149die(_("revision walk setup failed"));3150mark_edges_uninteresting(&revs, show_edge);31513152if(!fn_show_object)3153 fn_show_object = show_object;3154traverse_commit_list_filtered(&filter_options, &revs,3155 show_commit, fn_show_object, NULL,3156 NULL);31573158if(unpack_unreachable_expiration) {3159 revs.ignore_missing_links =1;3160if(add_unseen_recent_objects_to_traversal(&revs,3161 unpack_unreachable_expiration))3162die(_("unable to add recent objects"));3163if(prepare_revision_walk(&revs))3164die(_("revision walk setup failed"));3165traverse_commit_list(&revs, record_recent_commit,3166 record_recent_object, NULL);3167}31683169if(keep_unreachable)3170add_objects_in_unpacked_packs(&revs);3171if(pack_loose_unreachable)3172add_unreachable_loose_objects();3173if(unpack_unreachable)3174loosen_unused_packed_objects(&revs);31753176oid_array_clear(&recent_objects);3177}31783179static voidadd_extra_kept_packs(const struct string_list *names)3180{3181struct packed_git *p;31823183if(!names->nr)3184return;31853186for(p =get_all_packs(the_repository); p; p = p->next) {3187const char*name =basename(p->pack_name);3188int i;31893190if(!p->pack_local)3191continue;31923193for(i =0; i < names->nr; i++)3194if(!fspathcmp(name, names->items[i].string))3195break;31963197if(i < names->nr) {3198 p->pack_keep_in_core =1;3199 ignore_packed_keep_in_core =1;3200continue;3201}3202}3203}32043205static intoption_parse_index_version(const struct option *opt,3206const char*arg,int unset)3207{3208char*c;3209const char*val = arg;3210 pack_idx_opts.version =strtoul(val, &c,10);3211if(pack_idx_opts.version >2)3212die(_("unsupported index version%s"), val);3213if(*c ==','&& c[1])3214 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3215if(*c || pack_idx_opts.off32_limit &0x80000000)3216die(_("bad index version '%s'"), val);3217return0;3218}32193220static intoption_parse_unpack_unreachable(const struct option *opt,3221const char*arg,int unset)3222{3223if(unset) {3224 unpack_unreachable =0;3225 unpack_unreachable_expiration =0;3226}3227else{3228 unpack_unreachable =1;3229if(arg)3230 unpack_unreachable_expiration =approxidate(arg);3231}3232return0;3233}32343235intcmd_pack_objects(int argc,const char**argv,const char*prefix)3236{3237int use_internal_rev_list =0;3238int shallow =0;3239int all_progress_implied =0;3240struct argv_array rp = ARGV_ARRAY_INIT;3241int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3242int rev_list_index =0;3243struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3244struct option pack_objects_options[] = {3245OPT_SET_INT('q',"quiet", &progress,3246N_("do not show progress meter"),0),3247OPT_SET_INT(0,"progress", &progress,3248N_("show progress meter"),1),3249OPT_SET_INT(0,"all-progress", &progress,3250N_("show progress meter during object writing phase"),2),3251OPT_BOOL(0,"all-progress-implied",3252&all_progress_implied,3253N_("similar to --all-progress when progress meter is shown")),3254{ OPTION_CALLBACK,0,"index-version", NULL,N_("<version>[,<offset>]"),3255N_("write the pack index file in the specified idx format version"),32560, option_parse_index_version },3257OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3258N_("maximum size of each output pack file")),3259OPT_BOOL(0,"local", &local,3260N_("ignore borrowed objects from alternate object store")),3261OPT_BOOL(0,"incremental", &incremental,3262N_("ignore packed objects")),3263OPT_INTEGER(0,"window", &window,3264N_("limit pack window by objects")),3265OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3266N_("limit pack window by memory in addition to object limit")),3267OPT_INTEGER(0,"depth", &depth,3268N_("maximum length of delta chain allowed in the resulting pack")),3269OPT_BOOL(0,"reuse-delta", &reuse_delta,3270N_("reuse existing deltas")),3271OPT_BOOL(0,"reuse-object", &reuse_object,3272N_("reuse existing objects")),3273OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3274N_("use OFS_DELTA objects")),3275OPT_INTEGER(0,"threads", &delta_search_threads,3276N_("use threads when searching for best delta matches")),3277OPT_BOOL(0,"non-empty", &non_empty,3278N_("do not create an empty pack output")),3279OPT_BOOL(0,"revs", &use_internal_rev_list,3280N_("read revision arguments from standard input")),3281OPT_SET_INT_F(0,"unpacked", &rev_list_unpacked,3282N_("limit the objects to those that are not yet packed"),32831, PARSE_OPT_NONEG),3284OPT_SET_INT_F(0,"all", &rev_list_all,3285N_("include objects reachable from any reference"),32861, PARSE_OPT_NONEG),3287OPT_SET_INT_F(0,"reflog", &rev_list_reflog,3288N_("include objects referred by reflog entries"),32891, PARSE_OPT_NONEG),3290OPT_SET_INT_F(0,"indexed-objects", &rev_list_index,3291N_("include objects referred to by the index"),32921, PARSE_OPT_NONEG),3293OPT_BOOL(0,"stdout", &pack_to_stdout,3294N_("output pack to stdout")),3295OPT_BOOL(0,"include-tag", &include_tag,3296N_("include tag objects that refer to objects to be packed")),3297OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3298N_("keep unreachable objects")),3299OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3300N_("pack loose unreachable objects")),3301{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3302N_("unpack unreachable objects newer than <time>"),3303 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3304OPT_BOOL(0,"thin", &thin,3305N_("create thin packs")),3306OPT_BOOL(0,"shallow", &shallow,3307N_("create packs suitable for shallow fetches")),3308OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3309N_("ignore packs that have companion .keep file")),3310OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3311N_("ignore this pack")),3312OPT_INTEGER(0,"compression", &pack_compression_level,3313N_("pack compression level")),3314OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3315N_("do not hide commits by grafts"),0),3316OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3317N_("use a bitmap index if available to speed up counting objects")),3318OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3319N_("write a bitmap index together with the pack index")),3320OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3321{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3322N_("handling for missing objects"), PARSE_OPT_NONEG,3323 option_parse_missing_action },3324OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3325N_("do not pack objects in promisor packfiles")),3326OPT_BOOL(0,"delta-islands", &use_delta_islands,3327N_("respect islands during delta compression")),3328OPT_END(),3329};33303331if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3332BUG("too many dfs states, increase OE_DFS_STATE_BITS");33333334 read_replace_refs =0;33353336reset_pack_idx_option(&pack_idx_opts);3337git_config(git_pack_config, NULL);33383339 progress =isatty(2);3340 argc =parse_options(argc, argv, prefix, pack_objects_options,3341 pack_usage,0);33423343if(argc) {3344 base_name = argv[0];3345 argc--;3346}3347if(pack_to_stdout != !base_name || argc)3348usage_with_options(pack_usage, pack_objects_options);33493350if(depth >= (1<< OE_DEPTH_BITS)) {3351warning(_("delta chain depth%dis too deep, forcing%d"),3352 depth, (1<< OE_DEPTH_BITS) -1);3353 depth = (1<< OE_DEPTH_BITS) -1;3354}3355if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3356warning(_("pack.deltaCacheLimit is too high, forcing%d"),3357(1U<< OE_Z_DELTA_BITS) -1);3358 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3359}33603361argv_array_push(&rp,"pack-objects");3362if(thin) {3363 use_internal_rev_list =1;3364argv_array_push(&rp, shallow3365?"--objects-edge-aggressive"3366:"--objects-edge");3367}else3368argv_array_push(&rp,"--objects");33693370if(rev_list_all) {3371 use_internal_rev_list =1;3372argv_array_push(&rp,"--all");3373}3374if(rev_list_reflog) {3375 use_internal_rev_list =1;3376argv_array_push(&rp,"--reflog");3377}3378if(rev_list_index) {3379 use_internal_rev_list =1;3380argv_array_push(&rp,"--indexed-objects");3381}3382if(rev_list_unpacked) {3383 use_internal_rev_list =1;3384argv_array_push(&rp,"--unpacked");3385}33863387if(exclude_promisor_objects) {3388 use_internal_rev_list =1;3389 fetch_if_missing =0;3390argv_array_push(&rp,"--exclude-promisor-objects");3391}3392if(unpack_unreachable || keep_unreachable || pack_loose_unreachable)3393 use_internal_rev_list =1;33943395if(!reuse_object)3396 reuse_delta =0;3397if(pack_compression_level == -1)3398 pack_compression_level = Z_DEFAULT_COMPRESSION;3399else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3400die(_("bad pack compression level%d"), pack_compression_level);34013402if(!delta_search_threads)/* --threads=0 means autodetect */3403 delta_search_threads =online_cpus();34043405#ifdef NO_PTHREADS3406if(delta_search_threads !=1)3407warning(_("no threads support, ignoring --threads"));3408#endif3409if(!pack_to_stdout && !pack_size_limit)3410 pack_size_limit = pack_size_limit_cfg;3411if(pack_to_stdout && pack_size_limit)3412die(_("--max-pack-size cannot be used to build a pack for transfer"));3413if(pack_size_limit && pack_size_limit <1024*1024) {3414warning(_("minimum pack size limit is 1 MiB"));3415 pack_size_limit =1024*1024;3416}34173418if(!pack_to_stdout && thin)3419die(_("--thin cannot be used to build an indexable pack"));34203421if(keep_unreachable && unpack_unreachable)3422die(_("--keep-unreachable and --unpack-unreachable are incompatible"));3423if(!rev_list_all || !rev_list_reflog || !rev_list_index)3424 unpack_unreachable_expiration =0;34253426if(filter_options.choice) {3427if(!pack_to_stdout)3428die(_("cannot use --filter without --stdout"));3429 use_bitmap_index =0;3430}34313432/*3433 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3434 *3435 * - to produce good pack (with bitmap index not-yet-packed objects are3436 * packed in suboptimal order).3437 *3438 * - to use more robust pack-generation codepath (avoiding possible3439 * bugs in bitmap code and possible bitmap index corruption).3440 */3441if(!pack_to_stdout)3442 use_bitmap_index_default =0;34433444if(use_bitmap_index <0)3445 use_bitmap_index = use_bitmap_index_default;34463447/* "hard" reasons not to use bitmaps; these just won't work at all */3448if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow(the_repository))3449 use_bitmap_index =0;34503451if(pack_to_stdout || !rev_list_all)3452 write_bitmap_index =0;34533454if(use_delta_islands)3455argv_array_push(&rp,"--topo-order");34563457if(progress && all_progress_implied)3458 progress =2;34593460add_extra_kept_packs(&keep_pack_list);3461if(ignore_packed_keep_on_disk) {3462struct packed_git *p;3463for(p =get_all_packs(the_repository); p; p = p->next)3464if(p->pack_local && p->pack_keep)3465break;3466if(!p)/* no keep-able packs found */3467 ignore_packed_keep_on_disk =0;3468}3469if(local) {3470/*3471 * unlike ignore_packed_keep_on_disk above, we do not3472 * want to unset "local" based on looking at packs, as3473 * it also covers non-local objects3474 */3475struct packed_git *p;3476for(p =get_all_packs(the_repository); p; p = p->next) {3477if(!p->pack_local) {3478 have_non_local_packs =1;3479break;3480}3481}3482}34833484prepare_packing_data(&to_pack);34853486if(progress)3487 progress_state =start_progress(_("Enumerating objects"),0);3488if(!use_internal_rev_list)3489read_object_list_from_stdin();3490else{3491get_object_list(rp.argc, rp.argv);3492argv_array_clear(&rp);3493}3494cleanup_preferred_base();3495if(include_tag && nr_result)3496for_each_ref(add_ref_tag, NULL);3497stop_progress(&progress_state);34983499if(non_empty && !nr_result)3500return0;3501if(nr_result)3502prepare_pack(window, depth);3503write_pack_file();3504if(progress)3505fprintf_ln(stderr,3506_("Total %"PRIu32" (delta %"PRIu32"),"3507" reused %"PRIu32" (delta %"PRIu32")"),3508 written, written_delta, reused, reused_delta);3509return0;3510}