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 sparse; 88static int thin; 89static int num_preferred_base; 90static struct progress *progress_state; 91 92static struct packed_git *reuse_packfile; 93static uint32_t reuse_packfile_objects; 94static off_t reuse_packfile_offset; 95 96static int use_bitmap_index_default =1; 97static int use_bitmap_index = -1; 98static int write_bitmap_index; 99static uint16_t write_bitmap_options; 100 101static int exclude_promisor_objects; 102 103static int use_delta_islands; 104 105static unsigned long delta_cache_size =0; 106static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; 107static unsigned long cache_max_small_delta_size =1000; 108 109static unsigned long window_memory_limit =0; 110 111static struct list_objects_filter_options filter_options; 112 113enum missing_action { 114 MA_ERROR =0,/* fail if any missing objects are encountered */ 115 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 116 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 117}; 118static enum missing_action arg_missing_action; 119static show_object_fn fn_show_object; 120 121/* 122 * stats 123 */ 124static uint32_t written, written_delta; 125static uint32_t reused, reused_delta; 126 127/* 128 * Indexed commits 129 */ 130static struct commit **indexed_commits; 131static unsigned int indexed_commits_nr; 132static unsigned int indexed_commits_alloc; 133 134static voidindex_commit_for_bitmap(struct commit *commit) 135{ 136if(indexed_commits_nr >= indexed_commits_alloc) { 137 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 138REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 139} 140 141 indexed_commits[indexed_commits_nr++] = commit; 142} 143 144static void*get_delta(struct object_entry *entry) 145{ 146unsigned long size, base_size, delta_size; 147void*buf, *base_buf, *delta_buf; 148enum object_type type; 149 150 buf =read_object_file(&entry->idx.oid, &type, &size); 151if(!buf) 152die(_("unable to read%s"),oid_to_hex(&entry->idx.oid)); 153 base_buf =read_object_file(&DELTA(entry)->idx.oid, &type, 154&base_size); 155if(!base_buf) 156die("unable to read%s", 157oid_to_hex(&DELTA(entry)->idx.oid)); 158 delta_buf =diff_delta(base_buf, base_size, 159 buf, size, &delta_size,0); 160/* 161 * We succesfully computed this delta once but dropped it for 162 * memory reasons. Something is very wrong if this time we 163 * recompute and create a different delta. 164 */ 165if(!delta_buf || delta_size !=DELTA_SIZE(entry)) 166BUG("delta size changed"); 167free(buf); 168free(base_buf); 169return delta_buf; 170} 171 172static unsigned longdo_compress(void**pptr,unsigned long size) 173{ 174 git_zstream stream; 175void*in, *out; 176unsigned long maxsize; 177 178git_deflate_init(&stream, pack_compression_level); 179 maxsize =git_deflate_bound(&stream, size); 180 181 in = *pptr; 182 out =xmalloc(maxsize); 183*pptr = out; 184 185 stream.next_in = in; 186 stream.avail_in = size; 187 stream.next_out = out; 188 stream.avail_out = maxsize; 189while(git_deflate(&stream, Z_FINISH) == Z_OK) 190;/* nothing */ 191git_deflate_end(&stream); 192 193free(in); 194return stream.total_out; 195} 196 197static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 198const struct object_id *oid) 199{ 200 git_zstream stream; 201unsigned char ibuf[1024*16]; 202unsigned char obuf[1024*16]; 203unsigned long olen =0; 204 205git_deflate_init(&stream, pack_compression_level); 206 207for(;;) { 208 ssize_t readlen; 209int zret = Z_OK; 210 readlen =read_istream(st, ibuf,sizeof(ibuf)); 211if(readlen == -1) 212die(_("unable to read%s"),oid_to_hex(oid)); 213 214 stream.next_in = ibuf; 215 stream.avail_in = readlen; 216while((stream.avail_in || readlen ==0) && 217(zret == Z_OK || zret == Z_BUF_ERROR)) { 218 stream.next_out = obuf; 219 stream.avail_out =sizeof(obuf); 220 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 221hashwrite(f, obuf, stream.next_out - obuf); 222 olen += stream.next_out - obuf; 223} 224if(stream.avail_in) 225die(_("deflate error (%d)"), zret); 226if(readlen ==0) { 227if(zret != Z_STREAM_END) 228die(_("deflate error (%d)"), zret); 229break; 230} 231} 232git_deflate_end(&stream); 233return olen; 234} 235 236/* 237 * we are going to reuse the existing object data as is. make 238 * sure it is not corrupt. 239 */ 240static intcheck_pack_inflate(struct packed_git *p, 241struct pack_window **w_curs, 242 off_t offset, 243 off_t len, 244unsigned long expect) 245{ 246 git_zstream stream; 247unsigned char fakebuf[4096], *in; 248int st; 249 250memset(&stream,0,sizeof(stream)); 251git_inflate_init(&stream); 252do{ 253 in =use_pack(p, w_curs, offset, &stream.avail_in); 254 stream.next_in = in; 255 stream.next_out = fakebuf; 256 stream.avail_out =sizeof(fakebuf); 257 st =git_inflate(&stream, Z_FINISH); 258 offset += stream.next_in - in; 259}while(st == Z_OK || st == Z_BUF_ERROR); 260git_inflate_end(&stream); 261return(st == Z_STREAM_END && 262 stream.total_out == expect && 263 stream.total_in == len) ?0: -1; 264} 265 266static voidcopy_pack_data(struct hashfile *f, 267struct packed_git *p, 268struct pack_window **w_curs, 269 off_t offset, 270 off_t len) 271{ 272unsigned char*in; 273unsigned long avail; 274 275while(len) { 276 in =use_pack(p, w_curs, offset, &avail); 277if(avail > len) 278 avail = (unsigned long)len; 279hashwrite(f, in, avail); 280 offset += avail; 281 len -= avail; 282} 283} 284 285/* Return 0 if we will bust the pack-size limit */ 286static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 287unsigned long limit,int usable_delta) 288{ 289unsigned long size, datalen; 290unsigned char header[MAX_PACK_OBJECT_HEADER], 291 dheader[MAX_PACK_OBJECT_HEADER]; 292unsigned hdrlen; 293enum object_type type; 294void*buf; 295struct git_istream *st = NULL; 296const unsigned hashsz = the_hash_algo->rawsz; 297 298if(!usable_delta) { 299if(oe_type(entry) == OBJ_BLOB && 300oe_size_greater_than(&to_pack, entry, big_file_threshold) && 301(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 302 buf = NULL; 303else{ 304 buf =read_object_file(&entry->idx.oid, &type, &size); 305if(!buf) 306die(_("unable to read%s"), 307oid_to_hex(&entry->idx.oid)); 308} 309/* 310 * make sure no cached delta data remains from a 311 * previous attempt before a pack split occurred. 312 */ 313FREE_AND_NULL(entry->delta_data); 314 entry->z_delta_size =0; 315}else if(entry->delta_data) { 316 size =DELTA_SIZE(entry); 317 buf = entry->delta_data; 318 entry->delta_data = NULL; 319 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 320 OBJ_OFS_DELTA : OBJ_REF_DELTA; 321}else{ 322 buf =get_delta(entry); 323 size =DELTA_SIZE(entry); 324 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 325 OBJ_OFS_DELTA : OBJ_REF_DELTA; 326} 327 328if(st)/* large blob case, just assume we don't compress well */ 329 datalen = size; 330else if(entry->z_delta_size) 331 datalen = entry->z_delta_size; 332else 333 datalen =do_compress(&buf, size); 334 335/* 336 * The object header is a byte of 'type' followed by zero or 337 * more bytes of length. 338 */ 339 hdrlen =encode_in_pack_object_header(header,sizeof(header), 340 type, size); 341 342if(type == OBJ_OFS_DELTA) { 343/* 344 * Deltas with relative base contain an additional 345 * encoding of the relative offset for the delta 346 * base from this object's position in the pack. 347 */ 348 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 349unsigned pos =sizeof(dheader) -1; 350 dheader[pos] = ofs &127; 351while(ofs >>=7) 352 dheader[--pos] =128| (--ofs &127); 353if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 354if(st) 355close_istream(st); 356free(buf); 357return0; 358} 359hashwrite(f, header, hdrlen); 360hashwrite(f, dheader + pos,sizeof(dheader) - pos); 361 hdrlen +=sizeof(dheader) - pos; 362}else if(type == OBJ_REF_DELTA) { 363/* 364 * Deltas with a base reference contain 365 * additional bytes for the base object ID. 366 */ 367if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 368if(st) 369close_istream(st); 370free(buf); 371return0; 372} 373hashwrite(f, header, hdrlen); 374hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 375 hdrlen += hashsz; 376}else{ 377if(limit && hdrlen + datalen + hashsz >= limit) { 378if(st) 379close_istream(st); 380free(buf); 381return0; 382} 383hashwrite(f, header, hdrlen); 384} 385if(st) { 386 datalen =write_large_blob_data(st, f, &entry->idx.oid); 387close_istream(st); 388}else{ 389hashwrite(f, buf, datalen); 390free(buf); 391} 392 393return hdrlen + datalen; 394} 395 396/* Return 0 if we will bust the pack-size limit */ 397static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 398unsigned long limit,int usable_delta) 399{ 400struct packed_git *p =IN_PACK(entry); 401struct pack_window *w_curs = NULL; 402struct revindex_entry *revidx; 403 off_t offset; 404enum object_type type =oe_type(entry); 405 off_t datalen; 406unsigned char header[MAX_PACK_OBJECT_HEADER], 407 dheader[MAX_PACK_OBJECT_HEADER]; 408unsigned hdrlen; 409const unsigned hashsz = the_hash_algo->rawsz; 410unsigned long entry_size =SIZE(entry); 411 412if(DELTA(entry)) 413 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 414 OBJ_OFS_DELTA : OBJ_REF_DELTA; 415 hdrlen =encode_in_pack_object_header(header,sizeof(header), 416 type, entry_size); 417 418 offset = entry->in_pack_offset; 419 revidx =find_pack_revindex(p, offset); 420 datalen = revidx[1].offset - offset; 421if(!pack_to_stdout && p->index_version >1&& 422check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 423error(_("bad packed object CRC for%s"), 424oid_to_hex(&entry->idx.oid)); 425unuse_pack(&w_curs); 426returnwrite_no_reuse_object(f, entry, limit, usable_delta); 427} 428 429 offset += entry->in_pack_header_size; 430 datalen -= entry->in_pack_header_size; 431 432if(!pack_to_stdout && p->index_version ==1&& 433check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 434error(_("corrupt packed object for%s"), 435oid_to_hex(&entry->idx.oid)); 436unuse_pack(&w_curs); 437returnwrite_no_reuse_object(f, entry, limit, usable_delta); 438} 439 440if(type == OBJ_OFS_DELTA) { 441 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 442unsigned pos =sizeof(dheader) -1; 443 dheader[pos] = ofs &127; 444while(ofs >>=7) 445 dheader[--pos] =128| (--ofs &127); 446if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 447unuse_pack(&w_curs); 448return0; 449} 450hashwrite(f, header, hdrlen); 451hashwrite(f, dheader + pos,sizeof(dheader) - pos); 452 hdrlen +=sizeof(dheader) - pos; 453 reused_delta++; 454}else if(type == OBJ_REF_DELTA) { 455if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 456unuse_pack(&w_curs); 457return0; 458} 459hashwrite(f, header, hdrlen); 460hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 461 hdrlen += hashsz; 462 reused_delta++; 463}else{ 464if(limit && hdrlen + datalen + hashsz >= limit) { 465unuse_pack(&w_curs); 466return0; 467} 468hashwrite(f, header, hdrlen); 469} 470copy_pack_data(f, p, &w_curs, offset, datalen); 471unuse_pack(&w_curs); 472 reused++; 473return hdrlen + datalen; 474} 475 476/* Return 0 if we will bust the pack-size limit */ 477static off_t write_object(struct hashfile *f, 478struct object_entry *entry, 479 off_t write_offset) 480{ 481unsigned long limit; 482 off_t len; 483int usable_delta, to_reuse; 484 485if(!pack_to_stdout) 486crc32_begin(f); 487 488/* apply size limit if limited packsize and not first object */ 489if(!pack_size_limit || !nr_written) 490 limit =0; 491else if(pack_size_limit <= write_offset) 492/* 493 * the earlier object did not fit the limit; avoid 494 * mistaking this with unlimited (i.e. limit = 0). 495 */ 496 limit =1; 497else 498 limit = pack_size_limit - write_offset; 499 500if(!DELTA(entry)) 501 usable_delta =0;/* no delta */ 502else if(!pack_size_limit) 503 usable_delta =1;/* unlimited packfile */ 504else if(DELTA(entry)->idx.offset == (off_t)-1) 505 usable_delta =0;/* base was written to another pack */ 506else if(DELTA(entry)->idx.offset) 507 usable_delta =1;/* base already exists in this pack */ 508else 509 usable_delta =0;/* base could end up in another pack */ 510 511if(!reuse_object) 512 to_reuse =0;/* explicit */ 513else if(!IN_PACK(entry)) 514 to_reuse =0;/* can't reuse what we don't have */ 515else if(oe_type(entry) == OBJ_REF_DELTA || 516oe_type(entry) == OBJ_OFS_DELTA) 517/* check_object() decided it for us ... */ 518 to_reuse = usable_delta; 519/* ... but pack split may override that */ 520else if(oe_type(entry) != entry->in_pack_type) 521 to_reuse =0;/* pack has delta which is unusable */ 522else if(DELTA(entry)) 523 to_reuse =0;/* we want to pack afresh */ 524else 525 to_reuse =1;/* we have it in-pack undeltified, 526 * and we do not need to deltify it. 527 */ 528 529if(!to_reuse) 530 len =write_no_reuse_object(f, entry, limit, usable_delta); 531else 532 len =write_reuse_object(f, entry, limit, usable_delta); 533if(!len) 534return0; 535 536if(usable_delta) 537 written_delta++; 538 written++; 539if(!pack_to_stdout) 540 entry->idx.crc32 =crc32_end(f); 541return len; 542} 543 544enum write_one_status { 545 WRITE_ONE_SKIP = -1,/* already written */ 546 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 547 WRITE_ONE_WRITTEN =1,/* normal */ 548 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 549}; 550 551static enum write_one_status write_one(struct hashfile *f, 552struct object_entry *e, 553 off_t *offset) 554{ 555 off_t size; 556int recursing; 557 558/* 559 * we set offset to 1 (which is an impossible value) to mark 560 * the fact that this object is involved in "write its base 561 * first before writing a deltified object" recursion. 562 */ 563 recursing = (e->idx.offset ==1); 564if(recursing) { 565warning(_("recursive delta detected for object%s"), 566oid_to_hex(&e->idx.oid)); 567return WRITE_ONE_RECURSIVE; 568}else if(e->idx.offset || e->preferred_base) { 569/* offset is non zero if object is written already. */ 570return WRITE_ONE_SKIP; 571} 572 573/* if we are deltified, write out base object first. */ 574if(DELTA(e)) { 575 e->idx.offset =1;/* now recurse */ 576switch(write_one(f,DELTA(e), offset)) { 577case WRITE_ONE_RECURSIVE: 578/* we cannot depend on this one */ 579SET_DELTA(e, NULL); 580break; 581default: 582break; 583case WRITE_ONE_BREAK: 584 e->idx.offset = recursing; 585return WRITE_ONE_BREAK; 586} 587} 588 589 e->idx.offset = *offset; 590 size =write_object(f, e, *offset); 591if(!size) { 592 e->idx.offset = recursing; 593return WRITE_ONE_BREAK; 594} 595 written_list[nr_written++] = &e->idx; 596 597/* make sure off_t is sufficiently large not to wrap */ 598if(signed_add_overflows(*offset, size)) 599die(_("pack too large for current definition of off_t")); 600*offset += size; 601return WRITE_ONE_WRITTEN; 602} 603 604static intmark_tagged(const char*path,const struct object_id *oid,int flag, 605void*cb_data) 606{ 607struct object_id peeled; 608struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 609 610if(entry) 611 entry->tagged =1; 612if(!peel_ref(path, &peeled)) { 613 entry =packlist_find(&to_pack, peeled.hash, NULL); 614if(entry) 615 entry->tagged =1; 616} 617return0; 618} 619 620staticinlinevoidadd_to_write_order(struct object_entry **wo, 621unsigned int*endp, 622struct object_entry *e) 623{ 624if(e->filled ||oe_layer(&to_pack, e) != write_layer) 625return; 626 wo[(*endp)++] = e; 627 e->filled =1; 628} 629 630static voidadd_descendants_to_write_order(struct object_entry **wo, 631unsigned int*endp, 632struct object_entry *e) 633{ 634int add_to_order =1; 635while(e) { 636if(add_to_order) { 637struct object_entry *s; 638/* add this node... */ 639add_to_write_order(wo, endp, e); 640/* all its siblings... */ 641for(s =DELTA_SIBLING(e); s; s =DELTA_SIBLING(s)) { 642add_to_write_order(wo, endp, s); 643} 644} 645/* drop down a level to add left subtree nodes if possible */ 646if(DELTA_CHILD(e)) { 647 add_to_order =1; 648 e =DELTA_CHILD(e); 649}else{ 650 add_to_order =0; 651/* our sibling might have some children, it is next */ 652if(DELTA_SIBLING(e)) { 653 e =DELTA_SIBLING(e); 654continue; 655} 656/* go back to our parent node */ 657 e =DELTA(e); 658while(e && !DELTA_SIBLING(e)) { 659/* we're on the right side of a subtree, keep 660 * going up until we can go right again */ 661 e =DELTA(e); 662} 663if(!e) { 664/* done- we hit our original root node */ 665return; 666} 667/* pass it off to sibling at this level */ 668 e =DELTA_SIBLING(e); 669} 670}; 671} 672 673static voidadd_family_to_write_order(struct object_entry **wo, 674unsigned int*endp, 675struct object_entry *e) 676{ 677struct object_entry *root; 678 679for(root = e;DELTA(root); root =DELTA(root)) 680;/* nothing */ 681add_descendants_to_write_order(wo, endp, root); 682} 683 684static voidcompute_layer_order(struct object_entry **wo,unsigned int*wo_end) 685{ 686unsigned int i, last_untagged; 687struct object_entry *objects = to_pack.objects; 688 689for(i =0; i < to_pack.nr_objects; i++) { 690if(objects[i].tagged) 691break; 692add_to_write_order(wo, wo_end, &objects[i]); 693} 694 last_untagged = i; 695 696/* 697 * Then fill all the tagged tips. 698 */ 699for(; i < to_pack.nr_objects; i++) { 700if(objects[i].tagged) 701add_to_write_order(wo, wo_end, &objects[i]); 702} 703 704/* 705 * And then all remaining commits and tags. 706 */ 707for(i = last_untagged; i < to_pack.nr_objects; i++) { 708if(oe_type(&objects[i]) != OBJ_COMMIT && 709oe_type(&objects[i]) != OBJ_TAG) 710continue; 711add_to_write_order(wo, wo_end, &objects[i]); 712} 713 714/* 715 * And then all the trees. 716 */ 717for(i = last_untagged; i < to_pack.nr_objects; i++) { 718if(oe_type(&objects[i]) != OBJ_TREE) 719continue; 720add_to_write_order(wo, wo_end, &objects[i]); 721} 722 723/* 724 * Finally all the rest in really tight order 725 */ 726for(i = last_untagged; i < to_pack.nr_objects; i++) { 727if(!objects[i].filled &&oe_layer(&to_pack, &objects[i]) == write_layer) 728add_family_to_write_order(wo, wo_end, &objects[i]); 729} 730} 731 732static struct object_entry **compute_write_order(void) 733{ 734uint32_t max_layers =1; 735unsigned int i, wo_end; 736 737struct object_entry **wo; 738struct object_entry *objects = to_pack.objects; 739 740for(i =0; i < to_pack.nr_objects; i++) { 741 objects[i].tagged =0; 742 objects[i].filled =0; 743SET_DELTA_CHILD(&objects[i], NULL); 744SET_DELTA_SIBLING(&objects[i], NULL); 745} 746 747/* 748 * Fully connect delta_child/delta_sibling network. 749 * Make sure delta_sibling is sorted in the original 750 * recency order. 751 */ 752for(i = to_pack.nr_objects; i >0;) { 753struct object_entry *e = &objects[--i]; 754if(!DELTA(e)) 755continue; 756/* Mark me as the first child */ 757 e->delta_sibling_idx =DELTA(e)->delta_child_idx; 758SET_DELTA_CHILD(DELTA(e), e); 759} 760 761/* 762 * Mark objects that are at the tip of tags. 763 */ 764for_each_tag_ref(mark_tagged, NULL); 765 766if(use_delta_islands) 767 max_layers =compute_pack_layers(&to_pack); 768 769ALLOC_ARRAY(wo, to_pack.nr_objects); 770 wo_end =0; 771 772for(; write_layer < max_layers; ++write_layer) 773compute_layer_order(wo, &wo_end); 774 775if(wo_end != to_pack.nr_objects) 776die(_("ordered%uobjects, expected %"PRIu32), 777 wo_end, to_pack.nr_objects); 778 779return wo; 780} 781 782static off_t write_reused_pack(struct hashfile *f) 783{ 784unsigned char buffer[8192]; 785 off_t to_write, total; 786int fd; 787 788if(!is_pack_valid(reuse_packfile)) 789die(_("packfile is invalid:%s"), reuse_packfile->pack_name); 790 791 fd =git_open(reuse_packfile->pack_name); 792if(fd <0) 793die_errno(_("unable to open packfile for reuse:%s"), 794 reuse_packfile->pack_name); 795 796if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 797die_errno(_("unable to seek in reused packfile")); 798 799if(reuse_packfile_offset <0) 800 reuse_packfile_offset = reuse_packfile->pack_size - the_hash_algo->rawsz; 801 802 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 803 804while(to_write) { 805int read_pack =xread(fd, buffer,sizeof(buffer)); 806 807if(read_pack <=0) 808die_errno(_("unable to read from reused packfile")); 809 810if(read_pack > to_write) 811 read_pack = to_write; 812 813hashwrite(f, buffer, read_pack); 814 to_write -= read_pack; 815 816/* 817 * We don't know the actual number of objects written, 818 * only how many bytes written, how many bytes total, and 819 * how many objects total. So we can fake it by pretending all 820 * objects we are writing are the same size. This gives us a 821 * smooth progress meter, and at the end it matches the true 822 * answer. 823 */ 824 written = reuse_packfile_objects * 825(((double)(total - to_write)) / total); 826display_progress(progress_state, written); 827} 828 829close(fd); 830 written = reuse_packfile_objects; 831display_progress(progress_state, written); 832return reuse_packfile_offset -sizeof(struct pack_header); 833} 834 835static const char no_split_warning[] =N_( 836"disabling bitmap writing, packs are split due to pack.packSizeLimit" 837); 838 839static voidwrite_pack_file(void) 840{ 841uint32_t i =0, j; 842struct hashfile *f; 843 off_t offset; 844uint32_t nr_remaining = nr_result; 845time_t last_mtime =0; 846struct object_entry **write_order; 847 848if(progress > pack_to_stdout) 849 progress_state =start_progress(_("Writing objects"), nr_result); 850ALLOC_ARRAY(written_list, to_pack.nr_objects); 851 write_order =compute_write_order(); 852 853do{ 854struct object_id oid; 855char*pack_tmp_name = NULL; 856 857if(pack_to_stdout) 858 f =hashfd_throughput(1,"<stdout>", progress_state); 859else 860 f =create_tmp_packfile(&pack_tmp_name); 861 862 offset =write_pack_header(f, nr_remaining); 863 864if(reuse_packfile) { 865 off_t packfile_size; 866assert(pack_to_stdout); 867 868 packfile_size =write_reused_pack(f); 869 offset += packfile_size; 870} 871 872 nr_written =0; 873for(; i < to_pack.nr_objects; i++) { 874struct object_entry *e = write_order[i]; 875if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 876break; 877display_progress(progress_state, written); 878} 879 880/* 881 * Did we write the wrong # entries in the header? 882 * If so, rewrite it like in fast-import 883 */ 884if(pack_to_stdout) { 885finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 886}else if(nr_written == nr_remaining) { 887finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 888}else{ 889int fd =finalize_hashfile(f, oid.hash,0); 890fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 891 nr_written, oid.hash, offset); 892close(fd); 893if(write_bitmap_index) { 894warning(_(no_split_warning)); 895 write_bitmap_index =0; 896} 897} 898 899if(!pack_to_stdout) { 900struct stat st; 901struct strbuf tmpname = STRBUF_INIT; 902 903/* 904 * Packs are runtime accessed in their mtime 905 * order since newer packs are more likely to contain 906 * younger objects. So if we are creating multiple 907 * packs then we should modify the mtime of later ones 908 * to preserve this property. 909 */ 910if(stat(pack_tmp_name, &st) <0) { 911warning_errno(_("failed to stat%s"), pack_tmp_name); 912}else if(!last_mtime) { 913 last_mtime = st.st_mtime; 914}else{ 915struct utimbuf utb; 916 utb.actime = st.st_atime; 917 utb.modtime = --last_mtime; 918if(utime(pack_tmp_name, &utb) <0) 919warning_errno(_("failed utime() on%s"), pack_tmp_name); 920} 921 922strbuf_addf(&tmpname,"%s-", base_name); 923 924if(write_bitmap_index) { 925bitmap_writer_set_checksum(oid.hash); 926bitmap_writer_build_type_index( 927&to_pack, written_list, nr_written); 928} 929 930finish_tmp_packfile(&tmpname, pack_tmp_name, 931 written_list, nr_written, 932&pack_idx_opts, oid.hash); 933 934if(write_bitmap_index) { 935strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 936 937stop_progress(&progress_state); 938 939bitmap_writer_show_progress(progress); 940bitmap_writer_reuse_bitmaps(&to_pack); 941bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 942bitmap_writer_build(&to_pack); 943bitmap_writer_finish(written_list, nr_written, 944 tmpname.buf, write_bitmap_options); 945 write_bitmap_index =0; 946} 947 948strbuf_release(&tmpname); 949free(pack_tmp_name); 950puts(oid_to_hex(&oid)); 951} 952 953/* mark written objects as written to previous pack */ 954for(j =0; j < nr_written; j++) { 955 written_list[j]->offset = (off_t)-1; 956} 957 nr_remaining -= nr_written; 958}while(nr_remaining && i < to_pack.nr_objects); 959 960free(written_list); 961free(write_order); 962stop_progress(&progress_state); 963if(written != nr_result) 964die(_("wrote %"PRIu32" objects while expecting %"PRIu32), 965 written, nr_result); 966} 967 968static intno_try_delta(const char*path) 969{ 970static struct attr_check *check; 971 972if(!check) 973 check =attr_check_initl("delta", NULL); 974git_check_attr(the_repository->index, path, check); 975if(ATTR_FALSE(check->items[0].value)) 976return1; 977return0; 978} 979 980/* 981 * When adding an object, check whether we have already added it 982 * to our packing list. If so, we can skip. However, if we are 983 * being asked to excludei t, but the previous mention was to include 984 * it, make sure to adjust its flags and tweak our numbers accordingly. 985 * 986 * As an optimization, we pass out the index position where we would have 987 * found the item, since that saves us from having to look it up again a 988 * few lines later when we want to add the new entry. 989 */ 990static inthave_duplicate_entry(const struct object_id *oid, 991int exclude, 992uint32_t*index_pos) 993{ 994struct object_entry *entry; 995 996 entry =packlist_find(&to_pack, oid->hash, index_pos); 997if(!entry) 998return0; 9991000if(exclude) {1001if(!entry->preferred_base)1002 nr_result--;1003 entry->preferred_base =1;1004}10051006return1;1007}10081009static intwant_found_object(int exclude,struct packed_git *p)1010{1011if(exclude)1012return1;1013if(incremental)1014return0;10151016/*1017 * When asked to do --local (do not include an object that appears in a1018 * pack we borrow from elsewhere) or --honor-pack-keep (do not include1019 * an object that appears in a pack marked with .keep), finding a pack1020 * that matches the criteria is sufficient for us to decide to omit it.1021 * However, even if this pack does not satisfy the criteria, we need to1022 * make sure no copy of this object appears in _any_ pack that makes us1023 * to omit the object, so we need to check all the packs.1024 *1025 * We can however first check whether these options can possible matter;1026 * if they do not matter we know we want the object in generated pack.1027 * Otherwise, we signal "-1" at the end to tell the caller that we do1028 * not know either way, and it needs to check more packs.1029 */1030if(!ignore_packed_keep_on_disk &&1031!ignore_packed_keep_in_core &&1032(!local || !have_non_local_packs))1033return1;10341035if(local && !p->pack_local)1036return0;1037if(p->pack_local &&1038((ignore_packed_keep_on_disk && p->pack_keep) ||1039(ignore_packed_keep_in_core && p->pack_keep_in_core)))1040return0;10411042/* we don't know yet; keep looking for more packs */1043return-1;1044}10451046/*1047 * Check whether we want the object in the pack (e.g., we do not want1048 * objects found in non-local stores if the "--local" option was used).1049 *1050 * If the caller already knows an existing pack it wants to take the object1051 * from, that is passed in *found_pack and *found_offset; otherwise this1052 * function finds if there is any pack that has the object and returns the pack1053 * and its offset in these variables.1054 */1055static intwant_object_in_pack(const struct object_id *oid,1056int exclude,1057struct packed_git **found_pack,1058 off_t *found_offset)1059{1060int want;1061struct list_head *pos;1062struct multi_pack_index *m;10631064if(!exclude && local &&has_loose_object_nonlocal(oid))1065return0;10661067/*1068 * If we already know the pack object lives in, start checks from that1069 * pack - in the usual case when neither --local was given nor .keep files1070 * are present we will determine the answer right now.1071 */1072if(*found_pack) {1073 want =want_found_object(exclude, *found_pack);1074if(want != -1)1075return want;1076}10771078for(m =get_multi_pack_index(the_repository); m; m = m->next) {1079struct pack_entry e;1080if(fill_midx_entry(oid, &e, m)) {1081struct packed_git *p = e.p;1082 off_t offset;10831084if(p == *found_pack)1085 offset = *found_offset;1086else1087 offset =find_pack_entry_one(oid->hash, p);10881089if(offset) {1090if(!*found_pack) {1091if(!is_pack_valid(p))1092continue;1093*found_offset = offset;1094*found_pack = p;1095}1096 want =want_found_object(exclude, p);1097if(want != -1)1098return want;1099}1100}1101}11021103list_for_each(pos,get_packed_git_mru(the_repository)) {1104struct packed_git *p =list_entry(pos,struct packed_git, mru);1105 off_t offset;11061107if(p == *found_pack)1108 offset = *found_offset;1109else1110 offset =find_pack_entry_one(oid->hash, p);11111112if(offset) {1113if(!*found_pack) {1114if(!is_pack_valid(p))1115continue;1116*found_offset = offset;1117*found_pack = p;1118}1119 want =want_found_object(exclude, p);1120if(!exclude && want >0)1121list_move(&p->mru,1122get_packed_git_mru(the_repository));1123if(want != -1)1124return want;1125}1126}11271128return1;1129}11301131static voidcreate_object_entry(const struct object_id *oid,1132enum object_type type,1133uint32_t hash,1134int exclude,1135int no_try_delta,1136uint32_t index_pos,1137struct packed_git *found_pack,1138 off_t found_offset)1139{1140struct object_entry *entry;11411142 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1143 entry->hash = hash;1144oe_set_type(entry, type);1145if(exclude)1146 entry->preferred_base =1;1147else1148 nr_result++;1149if(found_pack) {1150oe_set_in_pack(&to_pack, entry, found_pack);1151 entry->in_pack_offset = found_offset;1152}11531154 entry->no_try_delta = no_try_delta;1155}11561157static const char no_closure_warning[] =N_(1158"disabling bitmap writing, as some objects are not being packed"1159);11601161static intadd_object_entry(const struct object_id *oid,enum object_type type,1162const char*name,int exclude)1163{1164struct packed_git *found_pack = NULL;1165 off_t found_offset =0;1166uint32_t index_pos;11671168display_progress(progress_state, ++nr_seen);11691170if(have_duplicate_entry(oid, exclude, &index_pos))1171return0;11721173if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1174/* The pack is missing an object, so it will not have closure */1175if(write_bitmap_index) {1176warning(_(no_closure_warning));1177 write_bitmap_index =0;1178}1179return0;1180}11811182create_object_entry(oid, type,pack_name_hash(name),1183 exclude, name &&no_try_delta(name),1184 index_pos, found_pack, found_offset);1185return1;1186}11871188static intadd_object_entry_from_bitmap(const struct object_id *oid,1189enum object_type type,1190int flags,uint32_t name_hash,1191struct packed_git *pack, off_t offset)1192{1193uint32_t index_pos;11941195display_progress(progress_state, ++nr_seen);11961197if(have_duplicate_entry(oid,0, &index_pos))1198return0;11991200if(!want_object_in_pack(oid,0, &pack, &offset))1201return0;12021203create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1204return1;1205}12061207struct pbase_tree_cache {1208struct object_id oid;1209int ref;1210int temporary;1211void*tree_data;1212unsigned long tree_size;1213};12141215static struct pbase_tree_cache *(pbase_tree_cache[256]);1216static intpbase_tree_cache_ix(const struct object_id *oid)1217{1218return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1219}1220static intpbase_tree_cache_ix_incr(int ix)1221{1222return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1223}12241225static struct pbase_tree {1226struct pbase_tree *next;1227/* This is a phony "cache" entry; we are not1228 * going to evict it or find it through _get()1229 * mechanism -- this is for the toplevel node that1230 * would almost always change with any commit.1231 */1232struct pbase_tree_cache pcache;1233} *pbase_tree;12341235static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1236{1237struct pbase_tree_cache *ent, *nent;1238void*data;1239unsigned long size;1240enum object_type type;1241int neigh;1242int my_ix =pbase_tree_cache_ix(oid);1243int available_ix = -1;12441245/* pbase-tree-cache acts as a limited hashtable.1246 * your object will be found at your index or within a few1247 * slots after that slot if it is cached.1248 */1249for(neigh =0; neigh <8; neigh++) {1250 ent = pbase_tree_cache[my_ix];1251if(ent &&oideq(&ent->oid, oid)) {1252 ent->ref++;1253return ent;1254}1255else if(((available_ix <0) && (!ent || !ent->ref)) ||1256((0<= available_ix) &&1257(!ent && pbase_tree_cache[available_ix])))1258 available_ix = my_ix;1259if(!ent)1260break;1261 my_ix =pbase_tree_cache_ix_incr(my_ix);1262}12631264/* Did not find one. Either we got a bogus request or1265 * we need to read and perhaps cache.1266 */1267 data =read_object_file(oid, &type, &size);1268if(!data)1269return NULL;1270if(type != OBJ_TREE) {1271free(data);1272return NULL;1273}12741275/* We need to either cache or return a throwaway copy */12761277if(available_ix <0)1278 ent = NULL;1279else{1280 ent = pbase_tree_cache[available_ix];1281 my_ix = available_ix;1282}12831284if(!ent) {1285 nent =xmalloc(sizeof(*nent));1286 nent->temporary = (available_ix <0);1287}1288else{1289/* evict and reuse */1290free(ent->tree_data);1291 nent = ent;1292}1293oidcpy(&nent->oid, oid);1294 nent->tree_data = data;1295 nent->tree_size = size;1296 nent->ref =1;1297if(!nent->temporary)1298 pbase_tree_cache[my_ix] = nent;1299return nent;1300}13011302static voidpbase_tree_put(struct pbase_tree_cache *cache)1303{1304if(!cache->temporary) {1305 cache->ref--;1306return;1307}1308free(cache->tree_data);1309free(cache);1310}13111312static intname_cmp_len(const char*name)1313{1314int i;1315for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1316;1317return i;1318}13191320static voidadd_pbase_object(struct tree_desc *tree,1321const char*name,1322int cmplen,1323const char*fullname)1324{1325struct name_entry entry;1326int cmp;13271328while(tree_entry(tree,&entry)) {1329if(S_ISGITLINK(entry.mode))1330continue;1331 cmp =tree_entry_len(&entry) != cmplen ?1:1332memcmp(name, entry.path, cmplen);1333if(cmp >0)1334continue;1335if(cmp <0)1336return;1337if(name[cmplen] !='/') {1338add_object_entry(&entry.oid,1339object_type(entry.mode),1340 fullname,1);1341return;1342}1343if(S_ISDIR(entry.mode)) {1344struct tree_desc sub;1345struct pbase_tree_cache *tree;1346const char*down = name+cmplen+1;1347int downlen =name_cmp_len(down);13481349 tree =pbase_tree_get(&entry.oid);1350if(!tree)1351return;1352init_tree_desc(&sub, tree->tree_data, tree->tree_size);13531354add_pbase_object(&sub, down, downlen, fullname);1355pbase_tree_put(tree);1356}1357}1358}13591360static unsigned*done_pbase_paths;1361static int done_pbase_paths_num;1362static int done_pbase_paths_alloc;1363static intdone_pbase_path_pos(unsigned hash)1364{1365int lo =0;1366int hi = done_pbase_paths_num;1367while(lo < hi) {1368int mi = lo + (hi - lo) /2;1369if(done_pbase_paths[mi] == hash)1370return mi;1371if(done_pbase_paths[mi] < hash)1372 hi = mi;1373else1374 lo = mi +1;1375}1376return-lo-1;1377}13781379static intcheck_pbase_path(unsigned hash)1380{1381int pos =done_pbase_path_pos(hash);1382if(0<= pos)1383return1;1384 pos = -pos -1;1385ALLOC_GROW(done_pbase_paths,1386 done_pbase_paths_num +1,1387 done_pbase_paths_alloc);1388 done_pbase_paths_num++;1389if(pos < done_pbase_paths_num)1390MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1391 done_pbase_paths_num - pos -1);1392 done_pbase_paths[pos] = hash;1393return0;1394}13951396static voidadd_preferred_base_object(const char*name)1397{1398struct pbase_tree *it;1399int cmplen;1400unsigned hash =pack_name_hash(name);14011402if(!num_preferred_base ||check_pbase_path(hash))1403return;14041405 cmplen =name_cmp_len(name);1406for(it = pbase_tree; it; it = it->next) {1407if(cmplen ==0) {1408add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1409}1410else{1411struct tree_desc tree;1412init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1413add_pbase_object(&tree, name, cmplen, name);1414}1415}1416}14171418static voidadd_preferred_base(struct object_id *oid)1419{1420struct pbase_tree *it;1421void*data;1422unsigned long size;1423struct object_id tree_oid;14241425if(window <= num_preferred_base++)1426return;14271428 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1429if(!data)1430return;14311432for(it = pbase_tree; it; it = it->next) {1433if(oideq(&it->pcache.oid, &tree_oid)) {1434free(data);1435return;1436}1437}14381439 it =xcalloc(1,sizeof(*it));1440 it->next = pbase_tree;1441 pbase_tree = it;14421443oidcpy(&it->pcache.oid, &tree_oid);1444 it->pcache.tree_data = data;1445 it->pcache.tree_size = size;1446}14471448static voidcleanup_preferred_base(void)1449{1450struct pbase_tree *it;1451unsigned i;14521453 it = pbase_tree;1454 pbase_tree = NULL;1455while(it) {1456struct pbase_tree *tmp = it;1457 it = tmp->next;1458free(tmp->pcache.tree_data);1459free(tmp);1460}14611462for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1463if(!pbase_tree_cache[i])1464continue;1465free(pbase_tree_cache[i]->tree_data);1466FREE_AND_NULL(pbase_tree_cache[i]);1467}14681469FREE_AND_NULL(done_pbase_paths);1470 done_pbase_paths_num = done_pbase_paths_alloc =0;1471}14721473/*1474 * Return 1 iff the object specified by "delta" can be sent1475 * literally as a delta against the base in "base_sha1". If1476 * so, then *base_out will point to the entry in our packing1477 * list, or NULL if we must use the external-base list.1478 *1479 * Depth value does not matter - find_deltas() will1480 * never consider reused delta as the base object to1481 * deltify other objects against, in order to avoid1482 * circular deltas.1483 */1484static intcan_reuse_delta(const unsigned char*base_sha1,1485struct object_entry *delta,1486struct object_entry **base_out)1487{1488struct object_entry *base;14891490if(!base_sha1)1491return0;14921493/*1494 * First see if we're already sending the base (or it's explicitly in1495 * our "excluded" list).1496 */1497 base =packlist_find(&to_pack, base_sha1, NULL);1498if(base) {1499if(!in_same_island(&delta->idx.oid, &base->idx.oid))1500return0;1501*base_out = base;1502return1;1503}15041505/*1506 * Otherwise, reachability bitmaps may tell us if the receiver has it,1507 * even if it was buried too deep in history to make it into the1508 * packing list.1509 */1510if(thin &&bitmap_has_sha1_in_uninteresting(bitmap_git, base_sha1)) {1511if(use_delta_islands) {1512struct object_id base_oid;1513hashcpy(base_oid.hash, base_sha1);1514if(!in_same_island(&delta->idx.oid, &base_oid))1515return0;1516}1517*base_out = NULL;1518return1;1519}15201521return0;1522}15231524static voidcheck_object(struct object_entry *entry)1525{1526unsigned long canonical_size;15271528if(IN_PACK(entry)) {1529struct packed_git *p =IN_PACK(entry);1530struct pack_window *w_curs = NULL;1531const unsigned char*base_ref = NULL;1532struct object_entry *base_entry;1533unsigned long used, used_0;1534unsigned long avail;1535 off_t ofs;1536unsigned char*buf, c;1537enum object_type type;1538unsigned long in_pack_size;15391540 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);15411542/*1543 * We want in_pack_type even if we do not reuse delta1544 * since non-delta representations could still be reused.1545 */1546 used =unpack_object_header_buffer(buf, avail,1547&type,1548&in_pack_size);1549if(used ==0)1550goto give_up;15511552if(type <0)1553BUG("invalid type%d", type);1554 entry->in_pack_type = type;15551556/*1557 * Determine if this is a delta and if so whether we can1558 * reuse it or not. Otherwise let's find out as cheaply as1559 * possible what the actual type and size for this object is.1560 */1561switch(entry->in_pack_type) {1562default:1563/* Not a delta hence we've already got all we need. */1564oe_set_type(entry, entry->in_pack_type);1565SET_SIZE(entry, in_pack_size);1566 entry->in_pack_header_size = used;1567if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1568goto give_up;1569unuse_pack(&w_curs);1570return;1571case OBJ_REF_DELTA:1572if(reuse_delta && !entry->preferred_base)1573 base_ref =use_pack(p, &w_curs,1574 entry->in_pack_offset + used, NULL);1575 entry->in_pack_header_size = used + the_hash_algo->rawsz;1576break;1577case OBJ_OFS_DELTA:1578 buf =use_pack(p, &w_curs,1579 entry->in_pack_offset + used, NULL);1580 used_0 =0;1581 c = buf[used_0++];1582 ofs = c &127;1583while(c &128) {1584 ofs +=1;1585if(!ofs ||MSB(ofs,7)) {1586error(_("delta base offset overflow in pack for%s"),1587oid_to_hex(&entry->idx.oid));1588goto give_up;1589}1590 c = buf[used_0++];1591 ofs = (ofs <<7) + (c &127);1592}1593 ofs = entry->in_pack_offset - ofs;1594if(ofs <=0|| ofs >= entry->in_pack_offset) {1595error(_("delta base offset out of bound for%s"),1596oid_to_hex(&entry->idx.oid));1597goto give_up;1598}1599if(reuse_delta && !entry->preferred_base) {1600struct revindex_entry *revidx;1601 revidx =find_pack_revindex(p, ofs);1602if(!revidx)1603goto give_up;1604 base_ref =nth_packed_object_sha1(p, revidx->nr);1605}1606 entry->in_pack_header_size = used + used_0;1607break;1608}16091610if(can_reuse_delta(base_ref, entry, &base_entry)) {1611oe_set_type(entry, entry->in_pack_type);1612SET_SIZE(entry, in_pack_size);/* delta size */1613SET_DELTA_SIZE(entry, in_pack_size);16141615if(base_entry) {1616SET_DELTA(entry, base_entry);1617 entry->delta_sibling_idx = base_entry->delta_child_idx;1618SET_DELTA_CHILD(base_entry, entry);1619}else{1620SET_DELTA_EXT(entry, base_ref);1621}16221623unuse_pack(&w_curs);1624return;1625}16261627if(oe_type(entry)) {1628 off_t delta_pos;16291630/*1631 * This must be a delta and we already know what the1632 * final object type is. Let's extract the actual1633 * object size from the delta header.1634 */1635 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1636 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1637if(canonical_size ==0)1638goto give_up;1639SET_SIZE(entry, canonical_size);1640unuse_pack(&w_curs);1641return;1642}16431644/*1645 * No choice but to fall back to the recursive delta walk1646 * with oid_object_info() to find about the object type1647 * at this point...1648 */1649 give_up:1650unuse_pack(&w_curs);1651}16521653oe_set_type(entry,1654oid_object_info(the_repository, &entry->idx.oid, &canonical_size));1655if(entry->type_valid) {1656SET_SIZE(entry, canonical_size);1657}else{1658/*1659 * Bad object type is checked in prepare_pack(). This is1660 * to permit a missing preferred base object to be ignored1661 * as a preferred base. Doing so can result in a larger1662 * pack file, but the transfer will still take place.1663 */1664}1665}16661667static intpack_offset_sort(const void*_a,const void*_b)1668{1669const struct object_entry *a = *(struct object_entry **)_a;1670const struct object_entry *b = *(struct object_entry **)_b;1671const struct packed_git *a_in_pack =IN_PACK(a);1672const struct packed_git *b_in_pack =IN_PACK(b);16731674/* avoid filesystem trashing with loose objects */1675if(!a_in_pack && !b_in_pack)1676returnoidcmp(&a->idx.oid, &b->idx.oid);16771678if(a_in_pack < b_in_pack)1679return-1;1680if(a_in_pack > b_in_pack)1681return1;1682return a->in_pack_offset < b->in_pack_offset ? -1:1683(a->in_pack_offset > b->in_pack_offset);1684}16851686/*1687 * Drop an on-disk delta we were planning to reuse. Naively, this would1688 * just involve blanking out the "delta" field, but we have to deal1689 * with some extra book-keeping:1690 *1691 * 1. Removing ourselves from the delta_sibling linked list.1692 *1693 * 2. Updating our size/type to the non-delta representation. These were1694 * either not recorded initially (size) or overwritten with the delta type1695 * (type) when check_object() decided to reuse the delta.1696 *1697 * 3. Resetting our delta depth, as we are now a base object.1698 */1699static voiddrop_reused_delta(struct object_entry *entry)1700{1701unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1702struct object_info oi = OBJECT_INFO_INIT;1703enum object_type type;1704unsigned long size;17051706while(*idx) {1707struct object_entry *oe = &to_pack.objects[*idx -1];17081709if(oe == entry)1710*idx = oe->delta_sibling_idx;1711else1712 idx = &oe->delta_sibling_idx;1713}1714SET_DELTA(entry, NULL);1715 entry->depth =0;17161717 oi.sizep = &size;1718 oi.typep = &type;1719if(packed_object_info(the_repository,IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1720/*1721 * We failed to get the info from this pack for some reason;1722 * fall back to oid_object_info, which may find another copy.1723 * And if that fails, the error will be recorded in oe_type(entry)1724 * and dealt with in prepare_pack().1725 */1726oe_set_type(entry,1727oid_object_info(the_repository, &entry->idx.oid, &size));1728}else{1729oe_set_type(entry, type);1730}1731SET_SIZE(entry, size);1732}17331734/*1735 * Follow the chain of deltas from this entry onward, throwing away any links1736 * that cause us to hit a cycle (as determined by the DFS state flags in1737 * the entries).1738 *1739 * We also detect too-long reused chains that would violate our --depth1740 * limit.1741 */1742static voidbreak_delta_chains(struct object_entry *entry)1743{1744/*1745 * The actual depth of each object we will write is stored as an int,1746 * as it cannot exceed our int "depth" limit. But before we break1747 * changes based no that limit, we may potentially go as deep as the1748 * number of objects, which is elsewhere bounded to a uint32_t.1749 */1750uint32_t total_depth;1751struct object_entry *cur, *next;17521753for(cur = entry, total_depth =0;1754 cur;1755 cur =DELTA(cur), total_depth++) {1756if(cur->dfs_state == DFS_DONE) {1757/*1758 * We've already seen this object and know it isn't1759 * part of a cycle. We do need to append its depth1760 * to our count.1761 */1762 total_depth += cur->depth;1763break;1764}17651766/*1767 * We break cycles before looping, so an ACTIVE state (or any1768 * other cruft which made its way into the state variable)1769 * is a bug.1770 */1771if(cur->dfs_state != DFS_NONE)1772BUG("confusing delta dfs state in first pass:%d",1773 cur->dfs_state);17741775/*1776 * Now we know this is the first time we've seen the object. If1777 * it's not a delta, we're done traversing, but we'll mark it1778 * done to save time on future traversals.1779 */1780if(!DELTA(cur)) {1781 cur->dfs_state = DFS_DONE;1782break;1783}17841785/*1786 * Mark ourselves as active and see if the next step causes1787 * us to cycle to another active object. It's important to do1788 * this _before_ we loop, because it impacts where we make the1789 * cut, and thus how our total_depth counter works.1790 * E.g., We may see a partial loop like:1791 *1792 * A -> B -> C -> D -> B1793 *1794 * Cutting B->C breaks the cycle. But now the depth of A is1795 * only 1, and our total_depth counter is at 3. The size of the1796 * error is always one less than the size of the cycle we1797 * broke. Commits C and D were "lost" from A's chain.1798 *1799 * If we instead cut D->B, then the depth of A is correct at 3.1800 * We keep all commits in the chain that we examined.1801 */1802 cur->dfs_state = DFS_ACTIVE;1803if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1804drop_reused_delta(cur);1805 cur->dfs_state = DFS_DONE;1806break;1807}1808}18091810/*1811 * And now that we've gone all the way to the bottom of the chain, we1812 * need to clear the active flags and set the depth fields as1813 * appropriate. Unlike the loop above, which can quit when it drops a1814 * delta, we need to keep going to look for more depth cuts. So we need1815 * an extra "next" pointer to keep going after we reset cur->delta.1816 */1817for(cur = entry; cur; cur = next) {1818 next =DELTA(cur);18191820/*1821 * We should have a chain of zero or more ACTIVE states down to1822 * a final DONE. We can quit after the DONE, because either it1823 * has no bases, or we've already handled them in a previous1824 * call.1825 */1826if(cur->dfs_state == DFS_DONE)1827break;1828else if(cur->dfs_state != DFS_ACTIVE)1829BUG("confusing delta dfs state in second pass:%d",1830 cur->dfs_state);18311832/*1833 * If the total_depth is more than depth, then we need to snip1834 * the chain into two or more smaller chains that don't exceed1835 * the maximum depth. Most of the resulting chains will contain1836 * (depth + 1) entries (i.e., depth deltas plus one base), and1837 * the last chain (i.e., the one containing entry) will contain1838 * whatever entries are left over, namely1839 * (total_depth % (depth + 1)) of them.1840 *1841 * Since we are iterating towards decreasing depth, we need to1842 * decrement total_depth as we go, and we need to write to the1843 * entry what its final depth will be after all of the1844 * snipping. Since we're snipping into chains of length (depth1845 * + 1) entries, the final depth of an entry will be its1846 * original depth modulo (depth + 1). Any time we encounter an1847 * entry whose final depth is supposed to be zero, we snip it1848 * from its delta base, thereby making it so.1849 */1850 cur->depth = (total_depth--) % (depth +1);1851if(!cur->depth)1852drop_reused_delta(cur);18531854 cur->dfs_state = DFS_DONE;1855}1856}18571858static voidget_object_details(void)1859{1860uint32_t i;1861struct object_entry **sorted_by_offset;18621863if(progress)1864 progress_state =start_progress(_("Counting objects"),1865 to_pack.nr_objects);18661867 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1868for(i =0; i < to_pack.nr_objects; i++)1869 sorted_by_offset[i] = to_pack.objects + i;1870QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);18711872for(i =0; i < to_pack.nr_objects; i++) {1873struct object_entry *entry = sorted_by_offset[i];1874check_object(entry);1875if(entry->type_valid &&1876oe_size_greater_than(&to_pack, entry, big_file_threshold))1877 entry->no_try_delta =1;1878display_progress(progress_state, i +1);1879}1880stop_progress(&progress_state);18811882/*1883 * This must happen in a second pass, since we rely on the delta1884 * information for the whole list being completed.1885 */1886for(i =0; i < to_pack.nr_objects; i++)1887break_delta_chains(&to_pack.objects[i]);18881889free(sorted_by_offset);1890}18911892/*1893 * We search for deltas in a list sorted by type, by filename hash, and then1894 * by size, so that we see progressively smaller and smaller files.1895 * That's because we prefer deltas to be from the bigger file1896 * to the smaller -- deletes are potentially cheaper, but perhaps1897 * more importantly, the bigger file is likely the more recent1898 * one. The deepest deltas are therefore the oldest objects which are1899 * less susceptible to be accessed often.1900 */1901static inttype_size_sort(const void*_a,const void*_b)1902{1903const struct object_entry *a = *(struct object_entry **)_a;1904const struct object_entry *b = *(struct object_entry **)_b;1905const enum object_type a_type =oe_type(a);1906const enum object_type b_type =oe_type(b);1907const unsigned long a_size =SIZE(a);1908const unsigned long b_size =SIZE(b);19091910if(a_type > b_type)1911return-1;1912if(a_type < b_type)1913return1;1914if(a->hash > b->hash)1915return-1;1916if(a->hash < b->hash)1917return1;1918if(a->preferred_base > b->preferred_base)1919return-1;1920if(a->preferred_base < b->preferred_base)1921return1;1922if(use_delta_islands) {1923const int island_cmp =island_delta_cmp(&a->idx.oid, &b->idx.oid);1924if(island_cmp)1925return island_cmp;1926}1927if(a_size > b_size)1928return-1;1929if(a_size < b_size)1930return1;1931return a < b ? -1: (a > b);/* newest first */1932}19331934struct unpacked {1935struct object_entry *entry;1936void*data;1937struct delta_index *index;1938unsigned depth;1939};19401941static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1942unsigned long delta_size)1943{1944if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1945return0;19461947if(delta_size < cache_max_small_delta_size)1948return1;19491950/* cache delta, if objects are large enough compared to delta size */1951if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1952return1;19531954return0;1955}19561957/* Protect delta_cache_size */1958static pthread_mutex_t cache_mutex;1959#define cache_lock() pthread_mutex_lock(&cache_mutex)1960#define cache_unlock() pthread_mutex_unlock(&cache_mutex)19611962/*1963 * Protect object list partitioning (e.g. struct thread_param) and1964 * progress_state1965 */1966static pthread_mutex_t progress_mutex;1967#define progress_lock() pthread_mutex_lock(&progress_mutex)1968#define progress_unlock() pthread_mutex_unlock(&progress_mutex)19691970/*1971 * Access to struct object_entry is unprotected since each thread owns1972 * a portion of the main object list. Just don't access object entries1973 * ahead in the list because they can be stolen and would need1974 * progress_mutex for protection.1975 */19761977/*1978 * Return the size of the object without doing any delta1979 * reconstruction (so non-deltas are true object sizes, but deltas1980 * return the size of the delta data).1981 */1982unsigned longoe_get_size_slow(struct packing_data *pack,1983const struct object_entry *e)1984{1985struct packed_git *p;1986struct pack_window *w_curs;1987unsigned char*buf;1988enum object_type type;1989unsigned long used, avail, size;19901991if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1992packing_data_lock(&to_pack);1993if(oid_object_info(the_repository, &e->idx.oid, &size) <0)1994die(_("unable to get size of%s"),1995oid_to_hex(&e->idx.oid));1996packing_data_unlock(&to_pack);1997return size;1998}19992000 p =oe_in_pack(pack, e);2001if(!p)2002BUG("when e->type is a delta, it must belong to a pack");20032004packing_data_lock(&to_pack);2005 w_curs = NULL;2006 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);2007 used =unpack_object_header_buffer(buf, avail, &type, &size);2008if(used ==0)2009die(_("unable to parse object header of%s"),2010oid_to_hex(&e->idx.oid));20112012unuse_pack(&w_curs);2013packing_data_unlock(&to_pack);2014return size;2015}20162017static inttry_delta(struct unpacked *trg,struct unpacked *src,2018unsigned max_depth,unsigned long*mem_usage)2019{2020struct object_entry *trg_entry = trg->entry;2021struct object_entry *src_entry = src->entry;2022unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;2023unsigned ref_depth;2024enum object_type type;2025void*delta_buf;20262027/* Don't bother doing diffs between different types */2028if(oe_type(trg_entry) !=oe_type(src_entry))2029return-1;20302031/*2032 * We do not bother to try a delta that we discarded on an2033 * earlier try, but only when reusing delta data. Note that2034 * src_entry that is marked as the preferred_base should always2035 * be considered, as even if we produce a suboptimal delta against2036 * it, we will still save the transfer cost, as we already know2037 * the other side has it and we won't send src_entry at all.2038 */2039if(reuse_delta &&IN_PACK(trg_entry) &&2040IN_PACK(trg_entry) ==IN_PACK(src_entry) &&2041!src_entry->preferred_base &&2042 trg_entry->in_pack_type != OBJ_REF_DELTA &&2043 trg_entry->in_pack_type != OBJ_OFS_DELTA)2044return0;20452046/* Let's not bust the allowed depth. */2047if(src->depth >= max_depth)2048return0;20492050/* Now some size filtering heuristics. */2051 trg_size =SIZE(trg_entry);2052if(!DELTA(trg_entry)) {2053 max_size = trg_size/2- the_hash_algo->rawsz;2054 ref_depth =1;2055}else{2056 max_size =DELTA_SIZE(trg_entry);2057 ref_depth = trg->depth;2058}2059 max_size = (uint64_t)max_size * (max_depth - src->depth) /2060(max_depth - ref_depth +1);2061if(max_size ==0)2062return0;2063 src_size =SIZE(src_entry);2064 sizediff = src_size < trg_size ? trg_size - src_size :0;2065if(sizediff >= max_size)2066return0;2067if(trg_size < src_size /32)2068return0;20692070if(!in_same_island(&trg->entry->idx.oid, &src->entry->idx.oid))2071return0;20722073/* Load data if not already done */2074if(!trg->data) {2075packing_data_lock(&to_pack);2076 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);2077packing_data_unlock(&to_pack);2078if(!trg->data)2079die(_("object%scannot be read"),2080oid_to_hex(&trg_entry->idx.oid));2081if(sz != trg_size)2082die(_("object%sinconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),2083oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,2084(uintmax_t)trg_size);2085*mem_usage += sz;2086}2087if(!src->data) {2088packing_data_lock(&to_pack);2089 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);2090packing_data_unlock(&to_pack);2091if(!src->data) {2092if(src_entry->preferred_base) {2093static int warned =0;2094if(!warned++)2095warning(_("object%scannot be read"),2096oid_to_hex(&src_entry->idx.oid));2097/*2098 * Those objects are not included in the2099 * resulting pack. Be resilient and ignore2100 * them if they can't be read, in case the2101 * pack could be created nevertheless.2102 */2103return0;2104}2105die(_("object%scannot be read"),2106oid_to_hex(&src_entry->idx.oid));2107}2108if(sz != src_size)2109die(_("object%sinconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),2110oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,2111(uintmax_t)src_size);2112*mem_usage += sz;2113}2114if(!src->index) {2115 src->index =create_delta_index(src->data, src_size);2116if(!src->index) {2117static int warned =0;2118if(!warned++)2119warning(_("suboptimal pack - out of memory"));2120return0;2121}2122*mem_usage +=sizeof_delta_index(src->index);2123}21242125 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2126if(!delta_buf)2127return0;21282129if(DELTA(trg_entry)) {2130/* Prefer only shallower same-sized deltas. */2131if(delta_size ==DELTA_SIZE(trg_entry) &&2132 src->depth +1>= trg->depth) {2133free(delta_buf);2134return0;2135}2136}21372138/*2139 * Handle memory allocation outside of the cache2140 * accounting lock. Compiler will optimize the strangeness2141 * away when NO_PTHREADS is defined.2142 */2143free(trg_entry->delta_data);2144cache_lock();2145if(trg_entry->delta_data) {2146 delta_cache_size -=DELTA_SIZE(trg_entry);2147 trg_entry->delta_data = NULL;2148}2149if(delta_cacheable(src_size, trg_size, delta_size)) {2150 delta_cache_size += delta_size;2151cache_unlock();2152 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2153}else{2154cache_unlock();2155free(delta_buf);2156}21572158SET_DELTA(trg_entry, src_entry);2159SET_DELTA_SIZE(trg_entry, delta_size);2160 trg->depth = src->depth +1;21612162return1;2163}21642165static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2166{2167struct object_entry *child =DELTA_CHILD(me);2168unsigned int m = n;2169while(child) {2170const unsigned int c =check_delta_limit(child, n +1);2171if(m < c)2172 m = c;2173 child =DELTA_SIBLING(child);2174}2175return m;2176}21772178static unsigned longfree_unpacked(struct unpacked *n)2179{2180unsigned long freed_mem =sizeof_delta_index(n->index);2181free_delta_index(n->index);2182 n->index = NULL;2183if(n->data) {2184 freed_mem +=SIZE(n->entry);2185FREE_AND_NULL(n->data);2186}2187 n->entry = NULL;2188 n->depth =0;2189return freed_mem;2190}21912192static voidfind_deltas(struct object_entry **list,unsigned*list_size,2193int window,int depth,unsigned*processed)2194{2195uint32_t i, idx =0, count =0;2196struct unpacked *array;2197unsigned long mem_usage =0;21982199 array =xcalloc(window,sizeof(struct unpacked));22002201for(;;) {2202struct object_entry *entry;2203struct unpacked *n = array + idx;2204int j, max_depth, best_base = -1;22052206progress_lock();2207if(!*list_size) {2208progress_unlock();2209break;2210}2211 entry = *list++;2212(*list_size)--;2213if(!entry->preferred_base) {2214(*processed)++;2215display_progress(progress_state, *processed);2216}2217progress_unlock();22182219 mem_usage -=free_unpacked(n);2220 n->entry = entry;22212222while(window_memory_limit &&2223 mem_usage > window_memory_limit &&2224 count >1) {2225const uint32_t tail = (idx + window - count) % window;2226 mem_usage -=free_unpacked(array + tail);2227 count--;2228}22292230/* We do not compute delta to *create* objects we are not2231 * going to pack.2232 */2233if(entry->preferred_base)2234goto next;22352236/*2237 * If the current object is at pack edge, take the depth the2238 * objects that depend on the current object into account2239 * otherwise they would become too deep.2240 */2241 max_depth = depth;2242if(DELTA_CHILD(entry)) {2243 max_depth -=check_delta_limit(entry,0);2244if(max_depth <=0)2245goto next;2246}22472248 j = window;2249while(--j >0) {2250int ret;2251uint32_t other_idx = idx + j;2252struct unpacked *m;2253if(other_idx >= window)2254 other_idx -= window;2255 m = array + other_idx;2256if(!m->entry)2257break;2258 ret =try_delta(n, m, max_depth, &mem_usage);2259if(ret <0)2260break;2261else if(ret >0)2262 best_base = other_idx;2263}22642265/*2266 * If we decided to cache the delta data, then it is best2267 * to compress it right away. First because we have to do2268 * it anyway, and doing it here while we're threaded will2269 * save a lot of time in the non threaded write phase,2270 * as well as allow for caching more deltas within2271 * the same cache size limit.2272 * ...2273 * But only if not writing to stdout, since in that case2274 * the network is most likely throttling writes anyway,2275 * and therefore it is best to go to the write phase ASAP2276 * instead, as we can afford spending more time compressing2277 * between writes at that moment.2278 */2279if(entry->delta_data && !pack_to_stdout) {2280unsigned long size;22812282 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2283if(size < (1U<< OE_Z_DELTA_BITS)) {2284 entry->z_delta_size = size;2285cache_lock();2286 delta_cache_size -=DELTA_SIZE(entry);2287 delta_cache_size += entry->z_delta_size;2288cache_unlock();2289}else{2290FREE_AND_NULL(entry->delta_data);2291 entry->z_delta_size =0;2292}2293}22942295/* if we made n a delta, and if n is already at max2296 * depth, leaving it in the window is pointless. we2297 * should evict it first.2298 */2299if(DELTA(entry) && max_depth <= n->depth)2300continue;23012302/*2303 * Move the best delta base up in the window, after the2304 * currently deltified object, to keep it longer. It will2305 * be the first base object to be attempted next.2306 */2307if(DELTA(entry)) {2308struct unpacked swap = array[best_base];2309int dist = (window + idx - best_base) % window;2310int dst = best_base;2311while(dist--) {2312int src = (dst +1) % window;2313 array[dst] = array[src];2314 dst = src;2315}2316 array[dst] = swap;2317}23182319 next:2320 idx++;2321if(count +1< window)2322 count++;2323if(idx >= window)2324 idx =0;2325}23262327for(i =0; i < window; ++i) {2328free_delta_index(array[i].index);2329free(array[i].data);2330}2331free(array);2332}23332334static voidtry_to_free_from_threads(size_t size)2335{2336packing_data_lock(&to_pack);2337release_pack_memory(size);2338packing_data_unlock(&to_pack);2339}23402341static try_to_free_t old_try_to_free_routine;23422343/*2344 * The main object list is split into smaller lists, each is handed to2345 * one worker.2346 *2347 * The main thread waits on the condition that (at least) one of the workers2348 * has stopped working (which is indicated in the .working member of2349 * struct thread_params).2350 *2351 * When a work thread has completed its work, it sets .working to 0 and2352 * signals the main thread and waits on the condition that .data_ready2353 * becomes 1.2354 *2355 * The main thread steals half of the work from the worker that has2356 * most work left to hand it to the idle worker.2357 */23582359struct thread_params {2360 pthread_t thread;2361struct object_entry **list;2362unsigned list_size;2363unsigned remaining;2364int window;2365int depth;2366int working;2367int data_ready;2368 pthread_mutex_t mutex;2369 pthread_cond_t cond;2370unsigned*processed;2371};23722373static pthread_cond_t progress_cond;23742375/*2376 * Mutex and conditional variable can't be statically-initialized on Windows.2377 */2378static voidinit_threaded_search(void)2379{2380pthread_mutex_init(&cache_mutex, NULL);2381pthread_mutex_init(&progress_mutex, NULL);2382pthread_cond_init(&progress_cond, NULL);2383 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2384}23852386static voidcleanup_threaded_search(void)2387{2388set_try_to_free_routine(old_try_to_free_routine);2389pthread_cond_destroy(&progress_cond);2390pthread_mutex_destroy(&cache_mutex);2391pthread_mutex_destroy(&progress_mutex);2392}23932394static void*threaded_find_deltas(void*arg)2395{2396struct thread_params *me = arg;23972398progress_lock();2399while(me->remaining) {2400progress_unlock();24012402find_deltas(me->list, &me->remaining,2403 me->window, me->depth, me->processed);24042405progress_lock();2406 me->working =0;2407pthread_cond_signal(&progress_cond);2408progress_unlock();24092410/*2411 * We must not set ->data_ready before we wait on the2412 * condition because the main thread may have set it to 12413 * before we get here. In order to be sure that new2414 * work is available if we see 1 in ->data_ready, it2415 * was initialized to 0 before this thread was spawned2416 * and we reset it to 0 right away.2417 */2418pthread_mutex_lock(&me->mutex);2419while(!me->data_ready)2420pthread_cond_wait(&me->cond, &me->mutex);2421 me->data_ready =0;2422pthread_mutex_unlock(&me->mutex);24232424progress_lock();2425}2426progress_unlock();2427/* leave ->working 1 so that this doesn't get more work assigned */2428return NULL;2429}24302431static voidll_find_deltas(struct object_entry **list,unsigned list_size,2432int window,int depth,unsigned*processed)2433{2434struct thread_params *p;2435int i, ret, active_threads =0;24362437init_threaded_search();24382439if(delta_search_threads <=1) {2440find_deltas(list, &list_size, window, depth, processed);2441cleanup_threaded_search();2442return;2443}2444if(progress > pack_to_stdout)2445fprintf_ln(stderr,_("Delta compression using up to%dthreads"),2446 delta_search_threads);2447 p =xcalloc(delta_search_threads,sizeof(*p));24482449/* Partition the work amongst work threads. */2450for(i =0; i < delta_search_threads; i++) {2451unsigned sub_size = list_size / (delta_search_threads - i);24522453/* don't use too small segments or no deltas will be found */2454if(sub_size <2*window && i+1< delta_search_threads)2455 sub_size =0;24562457 p[i].window = window;2458 p[i].depth = depth;2459 p[i].processed = processed;2460 p[i].working =1;2461 p[i].data_ready =0;24622463/* try to split chunks on "path" boundaries */2464while(sub_size && sub_size < list_size &&2465 list[sub_size]->hash &&2466 list[sub_size]->hash == list[sub_size-1]->hash)2467 sub_size++;24682469 p[i].list = list;2470 p[i].list_size = sub_size;2471 p[i].remaining = sub_size;24722473 list += sub_size;2474 list_size -= sub_size;2475}24762477/* Start work threads. */2478for(i =0; i < delta_search_threads; i++) {2479if(!p[i].list_size)2480continue;2481pthread_mutex_init(&p[i].mutex, NULL);2482pthread_cond_init(&p[i].cond, NULL);2483 ret =pthread_create(&p[i].thread, NULL,2484 threaded_find_deltas, &p[i]);2485if(ret)2486die(_("unable to create thread:%s"),strerror(ret));2487 active_threads++;2488}24892490/*2491 * Now let's wait for work completion. Each time a thread is done2492 * with its work, we steal half of the remaining work from the2493 * thread with the largest number of unprocessed objects and give2494 * it to that newly idle thread. This ensure good load balancing2495 * until the remaining object list segments are simply too short2496 * to be worth splitting anymore.2497 */2498while(active_threads) {2499struct thread_params *target = NULL;2500struct thread_params *victim = NULL;2501unsigned sub_size =0;25022503progress_lock();2504for(;;) {2505for(i =0; !target && i < delta_search_threads; i++)2506if(!p[i].working)2507 target = &p[i];2508if(target)2509break;2510pthread_cond_wait(&progress_cond, &progress_mutex);2511}25122513for(i =0; i < delta_search_threads; i++)2514if(p[i].remaining >2*window &&2515(!victim || victim->remaining < p[i].remaining))2516 victim = &p[i];2517if(victim) {2518 sub_size = victim->remaining /2;2519 list = victim->list + victim->list_size - sub_size;2520while(sub_size && list[0]->hash &&2521 list[0]->hash == list[-1]->hash) {2522 list++;2523 sub_size--;2524}2525if(!sub_size) {2526/*2527 * It is possible for some "paths" to have2528 * so many objects that no hash boundary2529 * might be found. Let's just steal the2530 * exact half in that case.2531 */2532 sub_size = victim->remaining /2;2533 list -= sub_size;2534}2535 target->list = list;2536 victim->list_size -= sub_size;2537 victim->remaining -= sub_size;2538}2539 target->list_size = sub_size;2540 target->remaining = sub_size;2541 target->working =1;2542progress_unlock();25432544pthread_mutex_lock(&target->mutex);2545 target->data_ready =1;2546pthread_cond_signal(&target->cond);2547pthread_mutex_unlock(&target->mutex);25482549if(!sub_size) {2550pthread_join(target->thread, NULL);2551pthread_cond_destroy(&target->cond);2552pthread_mutex_destroy(&target->mutex);2553 active_threads--;2554}2555}2556cleanup_threaded_search();2557free(p);2558}25592560static voidadd_tag_chain(const struct object_id *oid)2561{2562struct tag *tag;25632564/*2565 * We catch duplicates already in add_object_entry(), but we'd2566 * prefer to do this extra check to avoid having to parse the2567 * tag at all if we already know that it's being packed (e.g., if2568 * it was included via bitmaps, we would not have parsed it2569 * previously).2570 */2571if(packlist_find(&to_pack, oid->hash, NULL))2572return;25732574 tag =lookup_tag(the_repository, oid);2575while(1) {2576if(!tag ||parse_tag(tag) || !tag->tagged)2577die(_("unable to pack objects reachable from tag%s"),2578oid_to_hex(oid));25792580add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);25812582if(tag->tagged->type != OBJ_TAG)2583return;25842585 tag = (struct tag *)tag->tagged;2586}2587}25882589static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2590{2591struct object_id peeled;25922593if(starts_with(path,"refs/tags/") &&/* is a tag? */2594!peel_ref(path, &peeled) &&/* peelable? */2595packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2596add_tag_chain(oid);2597return0;2598}25992600static voidprepare_pack(int window,int depth)2601{2602struct object_entry **delta_list;2603uint32_t i, nr_deltas;2604unsigned n;26052606if(use_delta_islands)2607resolve_tree_islands(the_repository, progress, &to_pack);26082609get_object_details();26102611/*2612 * If we're locally repacking then we need to be doubly careful2613 * from now on in order to make sure no stealth corruption gets2614 * propagated to the new pack. Clients receiving streamed packs2615 * should validate everything they get anyway so no need to incur2616 * the additional cost here in that case.2617 */2618if(!pack_to_stdout)2619 do_check_packed_object_crc =1;26202621if(!to_pack.nr_objects || !window || !depth)2622return;26232624ALLOC_ARRAY(delta_list, to_pack.nr_objects);2625 nr_deltas = n =0;26262627for(i =0; i < to_pack.nr_objects; i++) {2628struct object_entry *entry = to_pack.objects + i;26292630if(DELTA(entry))2631/* This happens if we decided to reuse existing2632 * delta from a pack. "reuse_delta &&" is implied.2633 */2634continue;26352636if(!entry->type_valid ||2637oe_size_less_than(&to_pack, entry,50))2638continue;26392640if(entry->no_try_delta)2641continue;26422643if(!entry->preferred_base) {2644 nr_deltas++;2645if(oe_type(entry) <0)2646die(_("unable to get type of object%s"),2647oid_to_hex(&entry->idx.oid));2648}else{2649if(oe_type(entry) <0) {2650/*2651 * This object is not found, but we2652 * don't have to include it anyway.2653 */2654continue;2655}2656}26572658 delta_list[n++] = entry;2659}26602661if(nr_deltas && n >1) {2662unsigned nr_done =0;2663if(progress)2664 progress_state =start_progress(_("Compressing objects"),2665 nr_deltas);2666QSORT(delta_list, n, type_size_sort);2667ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2668stop_progress(&progress_state);2669if(nr_done != nr_deltas)2670die(_("inconsistency with delta count"));2671}2672free(delta_list);2673}26742675static intgit_pack_config(const char*k,const char*v,void*cb)2676{2677if(!strcmp(k,"pack.window")) {2678 window =git_config_int(k, v);2679return0;2680}2681if(!strcmp(k,"pack.windowmemory")) {2682 window_memory_limit =git_config_ulong(k, v);2683return0;2684}2685if(!strcmp(k,"pack.depth")) {2686 depth =git_config_int(k, v);2687return0;2688}2689if(!strcmp(k,"pack.deltacachesize")) {2690 max_delta_cache_size =git_config_int(k, v);2691return0;2692}2693if(!strcmp(k,"pack.deltacachelimit")) {2694 cache_max_small_delta_size =git_config_int(k, v);2695return0;2696}2697if(!strcmp(k,"pack.writebitmaphashcache")) {2698if(git_config_bool(k, v))2699 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2700else2701 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2702}2703if(!strcmp(k,"pack.usebitmaps")) {2704 use_bitmap_index_default =git_config_bool(k, v);2705return0;2706}2707if(!strcmp(k,"pack.usesparse")) {2708 sparse =git_config_bool(k, v);2709return0;2710}2711if(!strcmp(k,"pack.threads")) {2712 delta_search_threads =git_config_int(k, v);2713if(delta_search_threads <0)2714die(_("invalid number of threads specified (%d)"),2715 delta_search_threads);2716if(!HAVE_THREADS && delta_search_threads !=1) {2717warning(_("no threads support, ignoring%s"), k);2718 delta_search_threads =0;2719}2720return0;2721}2722if(!strcmp(k,"pack.indexversion")) {2723 pack_idx_opts.version =git_config_int(k, v);2724if(pack_idx_opts.version >2)2725die(_("bad pack.indexversion=%"PRIu32),2726 pack_idx_opts.version);2727return0;2728}2729returngit_default_config(k, v, cb);2730}27312732static voidread_object_list_from_stdin(void)2733{2734char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2735struct object_id oid;2736const char*p;27372738for(;;) {2739if(!fgets(line,sizeof(line), stdin)) {2740if(feof(stdin))2741break;2742if(!ferror(stdin))2743die("BUG: fgets returned NULL, not EOF, not error!");2744if(errno != EINTR)2745die_errno("fgets");2746clearerr(stdin);2747continue;2748}2749if(line[0] =='-') {2750if(get_oid_hex(line+1, &oid))2751die(_("expected edge object ID, got garbage:\n%s"),2752 line);2753add_preferred_base(&oid);2754continue;2755}2756if(parse_oid_hex(line, &oid, &p))2757die(_("expected object ID, got garbage:\n%s"), line);27582759add_preferred_base_object(p +1);2760add_object_entry(&oid, OBJ_NONE, p +1,0);2761}2762}27632764/* Remember to update object flag allocation in object.h */2765#define OBJECT_ADDED (1u<<20)27662767static voidshow_commit(struct commit *commit,void*data)2768{2769add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2770 commit->object.flags |= OBJECT_ADDED;27712772if(write_bitmap_index)2773index_commit_for_bitmap(commit);27742775if(use_delta_islands)2776propagate_island_marks(commit);2777}27782779static voidshow_object(struct object *obj,const char*name,void*data)2780{2781add_preferred_base_object(name);2782add_object_entry(&obj->oid, obj->type, name,0);2783 obj->flags |= OBJECT_ADDED;27842785if(use_delta_islands) {2786const char*p;2787unsigned depth;2788struct object_entry *ent;27892790/* the empty string is a root tree, which is depth 0 */2791 depth = *name ?1:0;2792for(p =strchr(name,'/'); p; p =strchr(p +1,'/'))2793 depth++;27942795 ent =packlist_find(&to_pack, obj->oid.hash, NULL);2796if(ent && depth >oe_tree_depth(&to_pack, ent))2797oe_set_tree_depth(&to_pack, ent, depth);2798}2799}28002801static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2802{2803assert(arg_missing_action == MA_ALLOW_ANY);28042805/*2806 * Quietly ignore ALL missing objects. This avoids problems with2807 * staging them now and getting an odd error later.2808 */2809if(!has_object_file(&obj->oid))2810return;28112812show_object(obj, name, data);2813}28142815static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2816{2817assert(arg_missing_action == MA_ALLOW_PROMISOR);28182819/*2820 * Quietly ignore EXPECTED missing objects. This avoids problems with2821 * staging them now and getting an odd error later.2822 */2823if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2824return;28252826show_object(obj, name, data);2827}28282829static intoption_parse_missing_action(const struct option *opt,2830const char*arg,int unset)2831{2832assert(arg);2833assert(!unset);28342835if(!strcmp(arg,"error")) {2836 arg_missing_action = MA_ERROR;2837 fn_show_object = show_object;2838return0;2839}28402841if(!strcmp(arg,"allow-any")) {2842 arg_missing_action = MA_ALLOW_ANY;2843 fetch_if_missing =0;2844 fn_show_object = show_object__ma_allow_any;2845return0;2846}28472848if(!strcmp(arg,"allow-promisor")) {2849 arg_missing_action = MA_ALLOW_PROMISOR;2850 fetch_if_missing =0;2851 fn_show_object = show_object__ma_allow_promisor;2852return0;2853}28542855die(_("invalid value for --missing"));2856return0;2857}28582859static voidshow_edge(struct commit *commit)2860{2861add_preferred_base(&commit->object.oid);2862}28632864struct in_pack_object {2865 off_t offset;2866struct object *object;2867};28682869struct in_pack {2870unsigned int alloc;2871unsigned int nr;2872struct in_pack_object *array;2873};28742875static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2876{2877 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2878 in_pack->array[in_pack->nr].object = object;2879 in_pack->nr++;2880}28812882/*2883 * Compare the objects in the offset order, in order to emulate the2884 * "git rev-list --objects" output that produced the pack originally.2885 */2886static intofscmp(const void*a_,const void*b_)2887{2888struct in_pack_object *a = (struct in_pack_object *)a_;2889struct in_pack_object *b = (struct in_pack_object *)b_;28902891if(a->offset < b->offset)2892return-1;2893else if(a->offset > b->offset)2894return1;2895else2896returnoidcmp(&a->object->oid, &b->object->oid);2897}28982899static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2900{2901struct packed_git *p;2902struct in_pack in_pack;2903uint32_t i;29042905memset(&in_pack,0,sizeof(in_pack));29062907for(p =get_all_packs(the_repository); p; p = p->next) {2908struct object_id oid;2909struct object *o;29102911if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2912continue;2913if(open_pack_index(p))2914die(_("cannot open pack index"));29152916ALLOC_GROW(in_pack.array,2917 in_pack.nr + p->num_objects,2918 in_pack.alloc);29192920for(i =0; i < p->num_objects; i++) {2921nth_packed_object_oid(&oid, p, i);2922 o =lookup_unknown_object(oid.hash);2923if(!(o->flags & OBJECT_ADDED))2924mark_in_pack_object(o, p, &in_pack);2925 o->flags |= OBJECT_ADDED;2926}2927}29282929if(in_pack.nr) {2930QSORT(in_pack.array, in_pack.nr, ofscmp);2931for(i =0; i < in_pack.nr; i++) {2932struct object *o = in_pack.array[i].object;2933add_object_entry(&o->oid, o->type,"",0);2934}2935}2936free(in_pack.array);2937}29382939static intadd_loose_object(const struct object_id *oid,const char*path,2940void*data)2941{2942enum object_type type =oid_object_info(the_repository, oid, NULL);29432944if(type <0) {2945warning(_("loose object at%scould not be examined"), path);2946return0;2947}29482949add_object_entry(oid, type,"",0);2950return0;2951}29522953/*2954 * We actually don't even have to worry about reachability here.2955 * add_object_entry will weed out duplicates, so we just add every2956 * loose object we find.2957 */2958static voidadd_unreachable_loose_objects(void)2959{2960for_each_loose_file_in_objdir(get_object_directory(),2961 add_loose_object,2962 NULL, NULL, NULL);2963}29642965static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2966{2967static struct packed_git *last_found = (void*)1;2968struct packed_git *p;29692970 p = (last_found != (void*)1) ? last_found :2971get_all_packs(the_repository);29722973while(p) {2974if((!p->pack_local || p->pack_keep ||2975 p->pack_keep_in_core) &&2976find_pack_entry_one(oid->hash, p)) {2977 last_found = p;2978return1;2979}2980if(p == last_found)2981 p =get_all_packs(the_repository);2982else2983 p = p->next;2984if(p == last_found)2985 p = p->next;2986}2987return0;2988}29892990/*2991 * Store a list of sha1s that are should not be discarded2992 * because they are either written too recently, or are2993 * reachable from another object that was.2994 *2995 * This is filled by get_object_list.2996 */2997static struct oid_array recent_objects;29982999static intloosened_object_can_be_discarded(const struct object_id *oid,3000 timestamp_t mtime)3001{3002if(!unpack_unreachable_expiration)3003return0;3004if(mtime > unpack_unreachable_expiration)3005return0;3006if(oid_array_lookup(&recent_objects, oid) >=0)3007return0;3008return1;3009}30103011static voidloosen_unused_packed_objects(struct rev_info *revs)3012{3013struct packed_git *p;3014uint32_t i;3015struct object_id oid;30163017for(p =get_all_packs(the_repository); p; p = p->next) {3018if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)3019continue;30203021if(open_pack_index(p))3022die(_("cannot open pack index"));30233024for(i =0; i < p->num_objects; i++) {3025nth_packed_object_oid(&oid, p, i);3026if(!packlist_find(&to_pack, oid.hash, NULL) &&3027!has_sha1_pack_kept_or_nonlocal(&oid) &&3028!loosened_object_can_be_discarded(&oid, p->mtime))3029if(force_object_loose(&oid, p->mtime))3030die(_("unable to force loose object"));3031}3032}3033}30343035/*3036 * This tracks any options which pack-reuse code expects to be on, or which a3037 * reader of the pack might not understand, and which would therefore prevent3038 * blind reuse of what we have on disk.3039 */3040static intpack_options_allow_reuse(void)3041{3042return pack_to_stdout &&3043 allow_ofs_delta &&3044!ignore_packed_keep_on_disk &&3045!ignore_packed_keep_in_core &&3046(!local || !have_non_local_packs) &&3047!incremental;3048}30493050static intget_object_list_from_bitmap(struct rev_info *revs)3051{3052if(!(bitmap_git =prepare_bitmap_walk(revs)))3053return-1;30543055if(pack_options_allow_reuse() &&3056!reuse_partial_packfile_from_bitmap(3057 bitmap_git,3058&reuse_packfile,3059&reuse_packfile_objects,3060&reuse_packfile_offset)) {3061assert(reuse_packfile_objects);3062 nr_result += reuse_packfile_objects;3063display_progress(progress_state, nr_result);3064}30653066traverse_bitmap_commit_list(bitmap_git, &add_object_entry_from_bitmap);3067return0;3068}30693070static voidrecord_recent_object(struct object *obj,3071const char*name,3072void*data)3073{3074oid_array_append(&recent_objects, &obj->oid);3075}30763077static voidrecord_recent_commit(struct commit *commit,void*data)3078{3079oid_array_append(&recent_objects, &commit->object.oid);3080}30813082static voidget_object_list(int ac,const char**av)3083{3084struct rev_info revs;3085struct setup_revision_opt s_r_opt = {3086.allow_exclude_promisor_objects =1,3087};3088char line[1000];3089int flags =0;3090int save_warning;30913092repo_init_revisions(the_repository, &revs, NULL);3093 save_commit_buffer =0;3094setup_revisions(ac, av, &revs, &s_r_opt);30953096/* make sure shallows are read */3097is_repository_shallow(the_repository);30983099 save_warning = warn_on_object_refname_ambiguity;3100 warn_on_object_refname_ambiguity =0;31013102while(fgets(line,sizeof(line), stdin) != NULL) {3103int len =strlen(line);3104if(len && line[len -1] =='\n')3105 line[--len] =0;3106if(!len)3107break;3108if(*line =='-') {3109if(!strcmp(line,"--not")) {3110 flags ^= UNINTERESTING;3111 write_bitmap_index =0;3112continue;3113}3114if(starts_with(line,"--shallow ")) {3115struct object_id oid;3116if(get_oid_hex(line +10, &oid))3117die("not an SHA-1 '%s'", line +10);3118register_shallow(the_repository, &oid);3119 use_bitmap_index =0;3120continue;3121}3122die(_("not a rev '%s'"), line);3123}3124if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))3125die(_("bad revision '%s'"), line);3126}31273128 warn_on_object_refname_ambiguity = save_warning;31293130if(use_bitmap_index && !get_object_list_from_bitmap(&revs))3131return;31323133if(use_delta_islands)3134load_delta_islands(the_repository);31353136if(prepare_revision_walk(&revs))3137die(_("revision walk setup failed"));3138mark_edges_uninteresting(&revs, show_edge, sparse);31393140if(!fn_show_object)3141 fn_show_object = show_object;3142traverse_commit_list_filtered(&filter_options, &revs,3143 show_commit, fn_show_object, NULL,3144 NULL);31453146if(unpack_unreachable_expiration) {3147 revs.ignore_missing_links =1;3148if(add_unseen_recent_objects_to_traversal(&revs,3149 unpack_unreachable_expiration))3150die(_("unable to add recent objects"));3151if(prepare_revision_walk(&revs))3152die(_("revision walk setup failed"));3153traverse_commit_list(&revs, record_recent_commit,3154 record_recent_object, NULL);3155}31563157if(keep_unreachable)3158add_objects_in_unpacked_packs(&revs);3159if(pack_loose_unreachable)3160add_unreachable_loose_objects();3161if(unpack_unreachable)3162loosen_unused_packed_objects(&revs);31633164oid_array_clear(&recent_objects);3165}31663167static voidadd_extra_kept_packs(const struct string_list *names)3168{3169struct packed_git *p;31703171if(!names->nr)3172return;31733174for(p =get_all_packs(the_repository); p; p = p->next) {3175const char*name =basename(p->pack_name);3176int i;31773178if(!p->pack_local)3179continue;31803181for(i =0; i < names->nr; i++)3182if(!fspathcmp(name, names->items[i].string))3183break;31843185if(i < names->nr) {3186 p->pack_keep_in_core =1;3187 ignore_packed_keep_in_core =1;3188continue;3189}3190}3191}31923193static intoption_parse_index_version(const struct option *opt,3194const char*arg,int unset)3195{3196char*c;3197const char*val = arg;31983199BUG_ON_OPT_NEG(unset);32003201 pack_idx_opts.version =strtoul(val, &c,10);3202if(pack_idx_opts.version >2)3203die(_("unsupported index version%s"), val);3204if(*c ==','&& c[1])3205 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3206if(*c || pack_idx_opts.off32_limit &0x80000000)3207die(_("bad index version '%s'"), val);3208return0;3209}32103211static intoption_parse_unpack_unreachable(const struct option *opt,3212const char*arg,int unset)3213{3214if(unset) {3215 unpack_unreachable =0;3216 unpack_unreachable_expiration =0;3217}3218else{3219 unpack_unreachable =1;3220if(arg)3221 unpack_unreachable_expiration =approxidate(arg);3222}3223return0;3224}32253226intcmd_pack_objects(int argc,const char**argv,const char*prefix)3227{3228int use_internal_rev_list =0;3229int shallow =0;3230int all_progress_implied =0;3231struct argv_array rp = ARGV_ARRAY_INIT;3232int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3233int rev_list_index =0;3234struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3235struct option pack_objects_options[] = {3236OPT_SET_INT('q',"quiet", &progress,3237N_("do not show progress meter"),0),3238OPT_SET_INT(0,"progress", &progress,3239N_("show progress meter"),1),3240OPT_SET_INT(0,"all-progress", &progress,3241N_("show progress meter during object writing phase"),2),3242OPT_BOOL(0,"all-progress-implied",3243&all_progress_implied,3244N_("similar to --all-progress when progress meter is shown")),3245{ OPTION_CALLBACK,0,"index-version", NULL,N_("<version>[,<offset>]"),3246N_("write the pack index file in the specified idx format version"),3247 PARSE_OPT_NONEG, option_parse_index_version },3248OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3249N_("maximum size of each output pack file")),3250OPT_BOOL(0,"local", &local,3251N_("ignore borrowed objects from alternate object store")),3252OPT_BOOL(0,"incremental", &incremental,3253N_("ignore packed objects")),3254OPT_INTEGER(0,"window", &window,3255N_("limit pack window by objects")),3256OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3257N_("limit pack window by memory in addition to object limit")),3258OPT_INTEGER(0,"depth", &depth,3259N_("maximum length of delta chain allowed in the resulting pack")),3260OPT_BOOL(0,"reuse-delta", &reuse_delta,3261N_("reuse existing deltas")),3262OPT_BOOL(0,"reuse-object", &reuse_object,3263N_("reuse existing objects")),3264OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3265N_("use OFS_DELTA objects")),3266OPT_INTEGER(0,"threads", &delta_search_threads,3267N_("use threads when searching for best delta matches")),3268OPT_BOOL(0,"non-empty", &non_empty,3269N_("do not create an empty pack output")),3270OPT_BOOL(0,"revs", &use_internal_rev_list,3271N_("read revision arguments from standard input")),3272OPT_SET_INT_F(0,"unpacked", &rev_list_unpacked,3273N_("limit the objects to those that are not yet packed"),32741, PARSE_OPT_NONEG),3275OPT_SET_INT_F(0,"all", &rev_list_all,3276N_("include objects reachable from any reference"),32771, PARSE_OPT_NONEG),3278OPT_SET_INT_F(0,"reflog", &rev_list_reflog,3279N_("include objects referred by reflog entries"),32801, PARSE_OPT_NONEG),3281OPT_SET_INT_F(0,"indexed-objects", &rev_list_index,3282N_("include objects referred to by the index"),32831, PARSE_OPT_NONEG),3284OPT_BOOL(0,"stdout", &pack_to_stdout,3285N_("output pack to stdout")),3286OPT_BOOL(0,"include-tag", &include_tag,3287N_("include tag objects that refer to objects to be packed")),3288OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3289N_("keep unreachable objects")),3290OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3291N_("pack loose unreachable objects")),3292{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3293N_("unpack unreachable objects newer than <time>"),3294 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3295OPT_BOOL(0,"sparse", &sparse,3296N_("use the sparse reachability algorithm")),3297OPT_BOOL(0,"thin", &thin,3298N_("create thin packs")),3299OPT_BOOL(0,"shallow", &shallow,3300N_("create packs suitable for shallow fetches")),3301OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3302N_("ignore packs that have companion .keep file")),3303OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3304N_("ignore this pack")),3305OPT_INTEGER(0,"compression", &pack_compression_level,3306N_("pack compression level")),3307OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3308N_("do not hide commits by grafts"),0),3309OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3310N_("use a bitmap index if available to speed up counting objects")),3311OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3312N_("write a bitmap index together with the pack index")),3313OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3314{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3315N_("handling for missing objects"), PARSE_OPT_NONEG,3316 option_parse_missing_action },3317OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3318N_("do not pack objects in promisor packfiles")),3319OPT_BOOL(0,"delta-islands", &use_delta_islands,3320N_("respect islands during delta compression")),3321OPT_END(),3322};33233324if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3325BUG("too many dfs states, increase OE_DFS_STATE_BITS");33263327 read_replace_refs =0;33283329 sparse =git_env_bool("GIT_TEST_PACK_SPARSE",0);3330reset_pack_idx_option(&pack_idx_opts);3331git_config(git_pack_config, NULL);33323333 progress =isatty(2);3334 argc =parse_options(argc, argv, prefix, pack_objects_options,3335 pack_usage,0);33363337if(argc) {3338 base_name = argv[0];3339 argc--;3340}3341if(pack_to_stdout != !base_name || argc)3342usage_with_options(pack_usage, pack_objects_options);33433344if(depth >= (1<< OE_DEPTH_BITS)) {3345warning(_("delta chain depth%dis too deep, forcing%d"),3346 depth, (1<< OE_DEPTH_BITS) -1);3347 depth = (1<< OE_DEPTH_BITS) -1;3348}3349if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3350warning(_("pack.deltaCacheLimit is too high, forcing%d"),3351(1U<< OE_Z_DELTA_BITS) -1);3352 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3353}33543355argv_array_push(&rp,"pack-objects");3356if(thin) {3357 use_internal_rev_list =1;3358argv_array_push(&rp, shallow3359?"--objects-edge-aggressive"3360:"--objects-edge");3361}else3362argv_array_push(&rp,"--objects");33633364if(rev_list_all) {3365 use_internal_rev_list =1;3366argv_array_push(&rp,"--all");3367}3368if(rev_list_reflog) {3369 use_internal_rev_list =1;3370argv_array_push(&rp,"--reflog");3371}3372if(rev_list_index) {3373 use_internal_rev_list =1;3374argv_array_push(&rp,"--indexed-objects");3375}3376if(rev_list_unpacked) {3377 use_internal_rev_list =1;3378argv_array_push(&rp,"--unpacked");3379}33803381if(exclude_promisor_objects) {3382 use_internal_rev_list =1;3383 fetch_if_missing =0;3384argv_array_push(&rp,"--exclude-promisor-objects");3385}3386if(unpack_unreachable || keep_unreachable || pack_loose_unreachable)3387 use_internal_rev_list =1;33883389if(!reuse_object)3390 reuse_delta =0;3391if(pack_compression_level == -1)3392 pack_compression_level = Z_DEFAULT_COMPRESSION;3393else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3394die(_("bad pack compression level%d"), pack_compression_level);33953396if(!delta_search_threads)/* --threads=0 means autodetect */3397 delta_search_threads =online_cpus();33983399if(!HAVE_THREADS && delta_search_threads !=1)3400warning(_("no threads support, ignoring --threads"));3401if(!pack_to_stdout && !pack_size_limit)3402 pack_size_limit = pack_size_limit_cfg;3403if(pack_to_stdout && pack_size_limit)3404die(_("--max-pack-size cannot be used to build a pack for transfer"));3405if(pack_size_limit && pack_size_limit <1024*1024) {3406warning(_("minimum pack size limit is 1 MiB"));3407 pack_size_limit =1024*1024;3408}34093410if(!pack_to_stdout && thin)3411die(_("--thin cannot be used to build an indexable pack"));34123413if(keep_unreachable && unpack_unreachable)3414die(_("--keep-unreachable and --unpack-unreachable are incompatible"));3415if(!rev_list_all || !rev_list_reflog || !rev_list_index)3416 unpack_unreachable_expiration =0;34173418if(filter_options.choice) {3419if(!pack_to_stdout)3420die(_("cannot use --filter without --stdout"));3421 use_bitmap_index =0;3422}34233424/*3425 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3426 *3427 * - to produce good pack (with bitmap index not-yet-packed objects are3428 * packed in suboptimal order).3429 *3430 * - to use more robust pack-generation codepath (avoiding possible3431 * bugs in bitmap code and possible bitmap index corruption).3432 */3433if(!pack_to_stdout)3434 use_bitmap_index_default =0;34353436if(use_bitmap_index <0)3437 use_bitmap_index = use_bitmap_index_default;34383439/* "hard" reasons not to use bitmaps; these just won't work at all */3440if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow(the_repository))3441 use_bitmap_index =0;34423443if(pack_to_stdout || !rev_list_all)3444 write_bitmap_index =0;34453446if(use_delta_islands)3447argv_array_push(&rp,"--topo-order");34483449if(progress && all_progress_implied)3450 progress =2;34513452add_extra_kept_packs(&keep_pack_list);3453if(ignore_packed_keep_on_disk) {3454struct packed_git *p;3455for(p =get_all_packs(the_repository); p; p = p->next)3456if(p->pack_local && p->pack_keep)3457break;3458if(!p)/* no keep-able packs found */3459 ignore_packed_keep_on_disk =0;3460}3461if(local) {3462/*3463 * unlike ignore_packed_keep_on_disk above, we do not3464 * want to unset "local" based on looking at packs, as3465 * it also covers non-local objects3466 */3467struct packed_git *p;3468for(p =get_all_packs(the_repository); p; p = p->next) {3469if(!p->pack_local) {3470 have_non_local_packs =1;3471break;3472}3473}3474}34753476prepare_packing_data(the_repository, &to_pack);34773478if(progress)3479 progress_state =start_progress(_("Enumerating objects"),0);3480if(!use_internal_rev_list)3481read_object_list_from_stdin();3482else{3483get_object_list(rp.argc, rp.argv);3484argv_array_clear(&rp);3485}3486cleanup_preferred_base();3487if(include_tag && nr_result)3488for_each_ref(add_ref_tag, NULL);3489stop_progress(&progress_state);34903491if(non_empty && !nr_result)3492return0;3493if(nr_result)3494prepare_pack(window, depth);3495write_pack_file();3496if(progress)3497fprintf_ln(stderr,3498_("Total %"PRIu32" (delta %"PRIu32"),"3499" reused %"PRIu32" (delta %"PRIu32")"),3500 written, written_delta, reused, reused_delta);3501return0;3502}