1#include"builtin.h" 2#include"cache.h" 3#include"repository.h" 4#include"config.h" 5#include"attr.h" 6#include"object.h" 7#include"blob.h" 8#include"commit.h" 9#include"tag.h" 10#include"tree.h" 11#include"delta.h" 12#include"pack.h" 13#include"pack-revindex.h" 14#include"csum-file.h" 15#include"tree-walk.h" 16#include"diff.h" 17#include"revision.h" 18#include"list-objects.h" 19#include"list-objects-filter.h" 20#include"list-objects-filter-options.h" 21#include"pack-objects.h" 22#include"progress.h" 23#include"refs.h" 24#include"streaming.h" 25#include"thread-utils.h" 26#include"pack-bitmap.h" 27#include"reachable.h" 28#include"sha1-array.h" 29#include"argv-array.h" 30#include"list.h" 31#include"packfile.h" 32#include"object-store.h" 33#include"dir.h" 34 35#define IN_PACK(obj) oe_in_pack(&to_pack, obj) 36#define SIZE(obj) oe_size(&to_pack, obj) 37#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size) 38#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj) 39#define DELTA(obj) oe_delta(&to_pack, obj) 40#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj) 41#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj) 42#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val) 43#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val) 44#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val) 45#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val) 46 47static const char*pack_usage[] = { 48N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 49N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 50 NULL 51}; 52 53/* 54 * Objects we are going to pack are collected in the `to_pack` structure. 55 * It contains an array (dynamically expanded) of the object data, and a map 56 * that can resolve SHA1s to their position in the array. 57 */ 58static struct packing_data to_pack; 59 60static struct pack_idx_entry **written_list; 61static uint32_t nr_result, nr_written, nr_seen; 62 63static int non_empty; 64static int reuse_delta =1, reuse_object =1; 65static int keep_unreachable, unpack_unreachable, include_tag; 66static timestamp_t unpack_unreachable_expiration; 67static int pack_loose_unreachable; 68static int local; 69static int have_non_local_packs; 70static int incremental; 71static int ignore_packed_keep_on_disk; 72static int ignore_packed_keep_in_core; 73static int allow_ofs_delta; 74static struct pack_idx_option pack_idx_opts; 75static const char*base_name; 76static int progress =1; 77static int window =10; 78static unsigned long pack_size_limit; 79static int depth =50; 80static int delta_search_threads; 81static int pack_to_stdout; 82static int num_preferred_base; 83static struct progress *progress_state; 84 85static struct packed_git *reuse_packfile; 86static uint32_t reuse_packfile_objects; 87static off_t reuse_packfile_offset; 88 89static int use_bitmap_index_default =1; 90static int use_bitmap_index = -1; 91static int write_bitmap_index; 92static uint16_t write_bitmap_options; 93 94static int exclude_promisor_objects; 95 96static unsigned long delta_cache_size =0; 97static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; 98static unsigned long cache_max_small_delta_size =1000; 99 100static unsigned long window_memory_limit =0; 101 102static struct list_objects_filter_options filter_options; 103 104enum missing_action { 105 MA_ERROR =0,/* fail if any missing objects are encountered */ 106 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 107 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 108}; 109static enum missing_action arg_missing_action; 110static show_object_fn fn_show_object; 111 112/* 113 * stats 114 */ 115static uint32_t written, written_delta; 116static uint32_t reused, reused_delta; 117 118/* 119 * Indexed commits 120 */ 121static struct commit **indexed_commits; 122static unsigned int indexed_commits_nr; 123static unsigned int indexed_commits_alloc; 124 125static voidindex_commit_for_bitmap(struct commit *commit) 126{ 127if(indexed_commits_nr >= indexed_commits_alloc) { 128 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 129REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 130} 131 132 indexed_commits[indexed_commits_nr++] = commit; 133} 134 135static void*get_delta(struct object_entry *entry) 136{ 137unsigned long size, base_size, delta_size; 138void*buf, *base_buf, *delta_buf; 139enum object_type type; 140 141 buf =read_object_file(&entry->idx.oid, &type, &size); 142if(!buf) 143die(_("unable to read%s"),oid_to_hex(&entry->idx.oid)); 144 base_buf =read_object_file(&DELTA(entry)->idx.oid, &type, 145&base_size); 146if(!base_buf) 147die("unable to read%s", 148oid_to_hex(&DELTA(entry)->idx.oid)); 149 delta_buf =diff_delta(base_buf, base_size, 150 buf, size, &delta_size,0); 151/* 152 * We succesfully computed this delta once but dropped it for 153 * memory reasons. Something is very wrong if this time we 154 * recompute and create a different delta. 155 */ 156if(!delta_buf || delta_size !=DELTA_SIZE(entry)) 157BUG("delta size changed"); 158free(buf); 159free(base_buf); 160return delta_buf; 161} 162 163static unsigned longdo_compress(void**pptr,unsigned long size) 164{ 165 git_zstream stream; 166void*in, *out; 167unsigned long maxsize; 168 169git_deflate_init(&stream, pack_compression_level); 170 maxsize =git_deflate_bound(&stream, size); 171 172 in = *pptr; 173 out =xmalloc(maxsize); 174*pptr = out; 175 176 stream.next_in = in; 177 stream.avail_in = size; 178 stream.next_out = out; 179 stream.avail_out = maxsize; 180while(git_deflate(&stream, Z_FINISH) == Z_OK) 181;/* nothing */ 182git_deflate_end(&stream); 183 184free(in); 185return stream.total_out; 186} 187 188static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 189const struct object_id *oid) 190{ 191 git_zstream stream; 192unsigned char ibuf[1024*16]; 193unsigned char obuf[1024*16]; 194unsigned long olen =0; 195 196git_deflate_init(&stream, pack_compression_level); 197 198for(;;) { 199 ssize_t readlen; 200int zret = Z_OK; 201 readlen =read_istream(st, ibuf,sizeof(ibuf)); 202if(readlen == -1) 203die(_("unable to read%s"),oid_to_hex(oid)); 204 205 stream.next_in = ibuf; 206 stream.avail_in = readlen; 207while((stream.avail_in || readlen ==0) && 208(zret == Z_OK || zret == Z_BUF_ERROR)) { 209 stream.next_out = obuf; 210 stream.avail_out =sizeof(obuf); 211 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 212hashwrite(f, obuf, stream.next_out - obuf); 213 olen += stream.next_out - obuf; 214} 215if(stream.avail_in) 216die(_("deflate error (%d)"), zret); 217if(readlen ==0) { 218if(zret != Z_STREAM_END) 219die(_("deflate error (%d)"), zret); 220break; 221} 222} 223git_deflate_end(&stream); 224return olen; 225} 226 227/* 228 * we are going to reuse the existing object data as is. make 229 * sure it is not corrupt. 230 */ 231static intcheck_pack_inflate(struct packed_git *p, 232struct pack_window **w_curs, 233 off_t offset, 234 off_t len, 235unsigned long expect) 236{ 237 git_zstream stream; 238unsigned char fakebuf[4096], *in; 239int st; 240 241memset(&stream,0,sizeof(stream)); 242git_inflate_init(&stream); 243do{ 244 in =use_pack(p, w_curs, offset, &stream.avail_in); 245 stream.next_in = in; 246 stream.next_out = fakebuf; 247 stream.avail_out =sizeof(fakebuf); 248 st =git_inflate(&stream, Z_FINISH); 249 offset += stream.next_in - in; 250}while(st == Z_OK || st == Z_BUF_ERROR); 251git_inflate_end(&stream); 252return(st == Z_STREAM_END && 253 stream.total_out == expect && 254 stream.total_in == len) ?0: -1; 255} 256 257static voidcopy_pack_data(struct hashfile *f, 258struct packed_git *p, 259struct pack_window **w_curs, 260 off_t offset, 261 off_t len) 262{ 263unsigned char*in; 264unsigned long avail; 265 266while(len) { 267 in =use_pack(p, w_curs, offset, &avail); 268if(avail > len) 269 avail = (unsigned long)len; 270hashwrite(f, in, avail); 271 offset += avail; 272 len -= avail; 273} 274} 275 276/* Return 0 if we will bust the pack-size limit */ 277static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 278unsigned long limit,int usable_delta) 279{ 280unsigned long size, datalen; 281unsigned char header[MAX_PACK_OBJECT_HEADER], 282 dheader[MAX_PACK_OBJECT_HEADER]; 283unsigned hdrlen; 284enum object_type type; 285void*buf; 286struct git_istream *st = NULL; 287const unsigned hashsz = the_hash_algo->rawsz; 288 289if(!usable_delta) { 290if(oe_type(entry) == OBJ_BLOB && 291oe_size_greater_than(&to_pack, entry, big_file_threshold) && 292(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 293 buf = NULL; 294else{ 295 buf =read_object_file(&entry->idx.oid, &type, &size); 296if(!buf) 297die(_("unable to read%s"), 298oid_to_hex(&entry->idx.oid)); 299} 300/* 301 * make sure no cached delta data remains from a 302 * previous attempt before a pack split occurred. 303 */ 304FREE_AND_NULL(entry->delta_data); 305 entry->z_delta_size =0; 306}else if(entry->delta_data) { 307 size =DELTA_SIZE(entry); 308 buf = entry->delta_data; 309 entry->delta_data = NULL; 310 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 311 OBJ_OFS_DELTA : OBJ_REF_DELTA; 312}else{ 313 buf =get_delta(entry); 314 size =DELTA_SIZE(entry); 315 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 316 OBJ_OFS_DELTA : OBJ_REF_DELTA; 317} 318 319if(st)/* large blob case, just assume we don't compress well */ 320 datalen = size; 321else if(entry->z_delta_size) 322 datalen = entry->z_delta_size; 323else 324 datalen =do_compress(&buf, size); 325 326/* 327 * The object header is a byte of 'type' followed by zero or 328 * more bytes of length. 329 */ 330 hdrlen =encode_in_pack_object_header(header,sizeof(header), 331 type, size); 332 333if(type == OBJ_OFS_DELTA) { 334/* 335 * Deltas with relative base contain an additional 336 * encoding of the relative offset for the delta 337 * base from this object's position in the pack. 338 */ 339 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 340unsigned pos =sizeof(dheader) -1; 341 dheader[pos] = ofs &127; 342while(ofs >>=7) 343 dheader[--pos] =128| (--ofs &127); 344if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 345if(st) 346close_istream(st); 347free(buf); 348return0; 349} 350hashwrite(f, header, hdrlen); 351hashwrite(f, dheader + pos,sizeof(dheader) - pos); 352 hdrlen +=sizeof(dheader) - pos; 353}else if(type == OBJ_REF_DELTA) { 354/* 355 * Deltas with a base reference contain 356 * additional bytes for the base object ID. 357 */ 358if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 359if(st) 360close_istream(st); 361free(buf); 362return0; 363} 364hashwrite(f, header, hdrlen); 365hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 366 hdrlen += hashsz; 367}else{ 368if(limit && hdrlen + datalen + hashsz >= limit) { 369if(st) 370close_istream(st); 371free(buf); 372return0; 373} 374hashwrite(f, header, hdrlen); 375} 376if(st) { 377 datalen =write_large_blob_data(st, f, &entry->idx.oid); 378close_istream(st); 379}else{ 380hashwrite(f, buf, datalen); 381free(buf); 382} 383 384return hdrlen + datalen; 385} 386 387/* Return 0 if we will bust the pack-size limit */ 388static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 389unsigned long limit,int usable_delta) 390{ 391struct packed_git *p =IN_PACK(entry); 392struct pack_window *w_curs = NULL; 393struct revindex_entry *revidx; 394 off_t offset; 395enum object_type type =oe_type(entry); 396 off_t datalen; 397unsigned char header[MAX_PACK_OBJECT_HEADER], 398 dheader[MAX_PACK_OBJECT_HEADER]; 399unsigned hdrlen; 400const unsigned hashsz = the_hash_algo->rawsz; 401unsigned long entry_size =SIZE(entry); 402 403if(DELTA(entry)) 404 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 405 OBJ_OFS_DELTA : OBJ_REF_DELTA; 406 hdrlen =encode_in_pack_object_header(header,sizeof(header), 407 type, entry_size); 408 409 offset = entry->in_pack_offset; 410 revidx =find_pack_revindex(p, offset); 411 datalen = revidx[1].offset - offset; 412if(!pack_to_stdout && p->index_version >1&& 413check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 414error(_("bad packed object CRC for%s"), 415oid_to_hex(&entry->idx.oid)); 416unuse_pack(&w_curs); 417returnwrite_no_reuse_object(f, entry, limit, usable_delta); 418} 419 420 offset += entry->in_pack_header_size; 421 datalen -= entry->in_pack_header_size; 422 423if(!pack_to_stdout && p->index_version ==1&& 424check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 425error(_("corrupt packed object for%s"), 426oid_to_hex(&entry->idx.oid)); 427unuse_pack(&w_curs); 428returnwrite_no_reuse_object(f, entry, limit, usable_delta); 429} 430 431if(type == OBJ_OFS_DELTA) { 432 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 433unsigned pos =sizeof(dheader) -1; 434 dheader[pos] = ofs &127; 435while(ofs >>=7) 436 dheader[--pos] =128| (--ofs &127); 437if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 438unuse_pack(&w_curs); 439return0; 440} 441hashwrite(f, header, hdrlen); 442hashwrite(f, dheader + pos,sizeof(dheader) - pos); 443 hdrlen +=sizeof(dheader) - pos; 444 reused_delta++; 445}else if(type == OBJ_REF_DELTA) { 446if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 447unuse_pack(&w_curs); 448return0; 449} 450hashwrite(f, header, hdrlen); 451hashwrite(f,DELTA(entry)->idx.oid.hash, hashsz); 452 hdrlen += hashsz; 453 reused_delta++; 454}else{ 455if(limit && hdrlen + datalen + hashsz >= limit) { 456unuse_pack(&w_curs); 457return0; 458} 459hashwrite(f, header, hdrlen); 460} 461copy_pack_data(f, p, &w_curs, offset, datalen); 462unuse_pack(&w_curs); 463 reused++; 464return hdrlen + datalen; 465} 466 467/* Return 0 if we will bust the pack-size limit */ 468static off_t write_object(struct hashfile *f, 469struct object_entry *entry, 470 off_t write_offset) 471{ 472unsigned long limit; 473 off_t len; 474int usable_delta, to_reuse; 475 476if(!pack_to_stdout) 477crc32_begin(f); 478 479/* apply size limit if limited packsize and not first object */ 480if(!pack_size_limit || !nr_written) 481 limit =0; 482else if(pack_size_limit <= write_offset) 483/* 484 * the earlier object did not fit the limit; avoid 485 * mistaking this with unlimited (i.e. limit = 0). 486 */ 487 limit =1; 488else 489 limit = pack_size_limit - write_offset; 490 491if(!DELTA(entry)) 492 usable_delta =0;/* no delta */ 493else if(!pack_size_limit) 494 usable_delta =1;/* unlimited packfile */ 495else if(DELTA(entry)->idx.offset == (off_t)-1) 496 usable_delta =0;/* base was written to another pack */ 497else if(DELTA(entry)->idx.offset) 498 usable_delta =1;/* base already exists in this pack */ 499else 500 usable_delta =0;/* base could end up in another pack */ 501 502if(!reuse_object) 503 to_reuse =0;/* explicit */ 504else if(!IN_PACK(entry)) 505 to_reuse =0;/* can't reuse what we don't have */ 506else if(oe_type(entry) == OBJ_REF_DELTA || 507oe_type(entry) == OBJ_OFS_DELTA) 508/* check_object() decided it for us ... */ 509 to_reuse = usable_delta; 510/* ... but pack split may override that */ 511else if(oe_type(entry) != entry->in_pack_type) 512 to_reuse =0;/* pack has delta which is unusable */ 513else if(DELTA(entry)) 514 to_reuse =0;/* we want to pack afresh */ 515else 516 to_reuse =1;/* we have it in-pack undeltified, 517 * and we do not need to deltify it. 518 */ 519 520if(!to_reuse) 521 len =write_no_reuse_object(f, entry, limit, usable_delta); 522else 523 len =write_reuse_object(f, entry, limit, usable_delta); 524if(!len) 525return0; 526 527if(usable_delta) 528 written_delta++; 529 written++; 530if(!pack_to_stdout) 531 entry->idx.crc32 =crc32_end(f); 532return len; 533} 534 535enum write_one_status { 536 WRITE_ONE_SKIP = -1,/* already written */ 537 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 538 WRITE_ONE_WRITTEN =1,/* normal */ 539 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 540}; 541 542static enum write_one_status write_one(struct hashfile *f, 543struct object_entry *e, 544 off_t *offset) 545{ 546 off_t size; 547int recursing; 548 549/* 550 * we set offset to 1 (which is an impossible value) to mark 551 * the fact that this object is involved in "write its base 552 * first before writing a deltified object" recursion. 553 */ 554 recursing = (e->idx.offset ==1); 555if(recursing) { 556warning(_("recursive delta detected for object%s"), 557oid_to_hex(&e->idx.oid)); 558return WRITE_ONE_RECURSIVE; 559}else if(e->idx.offset || e->preferred_base) { 560/* offset is non zero if object is written already. */ 561return WRITE_ONE_SKIP; 562} 563 564/* if we are deltified, write out base object first. */ 565if(DELTA(e)) { 566 e->idx.offset =1;/* now recurse */ 567switch(write_one(f,DELTA(e), offset)) { 568case WRITE_ONE_RECURSIVE: 569/* we cannot depend on this one */ 570SET_DELTA(e, NULL); 571break; 572default: 573break; 574case WRITE_ONE_BREAK: 575 e->idx.offset = recursing; 576return WRITE_ONE_BREAK; 577} 578} 579 580 e->idx.offset = *offset; 581 size =write_object(f, e, *offset); 582if(!size) { 583 e->idx.offset = recursing; 584return WRITE_ONE_BREAK; 585} 586 written_list[nr_written++] = &e->idx; 587 588/* make sure off_t is sufficiently large not to wrap */ 589if(signed_add_overflows(*offset, size)) 590die(_("pack too large for current definition of off_t")); 591*offset += size; 592return WRITE_ONE_WRITTEN; 593} 594 595static intmark_tagged(const char*path,const struct object_id *oid,int flag, 596void*cb_data) 597{ 598struct object_id peeled; 599struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 600 601if(entry) 602 entry->tagged =1; 603if(!peel_ref(path, &peeled)) { 604 entry =packlist_find(&to_pack, peeled.hash, NULL); 605if(entry) 606 entry->tagged =1; 607} 608return0; 609} 610 611staticinlinevoidadd_to_write_order(struct object_entry **wo, 612unsigned int*endp, 613struct object_entry *e) 614{ 615if(e->filled) 616return; 617 wo[(*endp)++] = e; 618 e->filled =1; 619} 620 621static voidadd_descendants_to_write_order(struct object_entry **wo, 622unsigned int*endp, 623struct object_entry *e) 624{ 625int add_to_order =1; 626while(e) { 627if(add_to_order) { 628struct object_entry *s; 629/* add this node... */ 630add_to_write_order(wo, endp, e); 631/* all its siblings... */ 632for(s =DELTA_SIBLING(e); s; s =DELTA_SIBLING(s)) { 633add_to_write_order(wo, endp, s); 634} 635} 636/* drop down a level to add left subtree nodes if possible */ 637if(DELTA_CHILD(e)) { 638 add_to_order =1; 639 e =DELTA_CHILD(e); 640}else{ 641 add_to_order =0; 642/* our sibling might have some children, it is next */ 643if(DELTA_SIBLING(e)) { 644 e =DELTA_SIBLING(e); 645continue; 646} 647/* go back to our parent node */ 648 e =DELTA(e); 649while(e && !DELTA_SIBLING(e)) { 650/* we're on the right side of a subtree, keep 651 * going up until we can go right again */ 652 e =DELTA(e); 653} 654if(!e) { 655/* done- we hit our original root node */ 656return; 657} 658/* pass it off to sibling at this level */ 659 e =DELTA_SIBLING(e); 660} 661}; 662} 663 664static voidadd_family_to_write_order(struct object_entry **wo, 665unsigned int*endp, 666struct object_entry *e) 667{ 668struct object_entry *root; 669 670for(root = e;DELTA(root); root =DELTA(root)) 671;/* nothing */ 672add_descendants_to_write_order(wo, endp, root); 673} 674 675static struct object_entry **compute_write_order(void) 676{ 677unsigned int i, wo_end, last_untagged; 678 679struct object_entry **wo; 680struct object_entry *objects = to_pack.objects; 681 682for(i =0; i < to_pack.nr_objects; i++) { 683 objects[i].tagged =0; 684 objects[i].filled =0; 685SET_DELTA_CHILD(&objects[i], NULL); 686SET_DELTA_SIBLING(&objects[i], NULL); 687} 688 689/* 690 * Fully connect delta_child/delta_sibling network. 691 * Make sure delta_sibling is sorted in the original 692 * recency order. 693 */ 694for(i = to_pack.nr_objects; i >0;) { 695struct object_entry *e = &objects[--i]; 696if(!DELTA(e)) 697continue; 698/* Mark me as the first child */ 699 e->delta_sibling_idx =DELTA(e)->delta_child_idx; 700SET_DELTA_CHILD(DELTA(e), e); 701} 702 703/* 704 * Mark objects that are at the tip of tags. 705 */ 706for_each_tag_ref(mark_tagged, NULL); 707 708/* 709 * Give the objects in the original recency order until 710 * we see a tagged tip. 711 */ 712ALLOC_ARRAY(wo, to_pack.nr_objects); 713for(i = wo_end =0; i < to_pack.nr_objects; i++) { 714if(objects[i].tagged) 715break; 716add_to_write_order(wo, &wo_end, &objects[i]); 717} 718 last_untagged = i; 719 720/* 721 * Then fill all the tagged tips. 722 */ 723for(; i < to_pack.nr_objects; i++) { 724if(objects[i].tagged) 725add_to_write_order(wo, &wo_end, &objects[i]); 726} 727 728/* 729 * And then all remaining commits and tags. 730 */ 731for(i = last_untagged; i < to_pack.nr_objects; i++) { 732if(oe_type(&objects[i]) != OBJ_COMMIT && 733oe_type(&objects[i]) != OBJ_TAG) 734continue; 735add_to_write_order(wo, &wo_end, &objects[i]); 736} 737 738/* 739 * And then all the trees. 740 */ 741for(i = last_untagged; i < to_pack.nr_objects; i++) { 742if(oe_type(&objects[i]) != OBJ_TREE) 743continue; 744add_to_write_order(wo, &wo_end, &objects[i]); 745} 746 747/* 748 * Finally all the rest in really tight order 749 */ 750for(i = last_untagged; i < to_pack.nr_objects; i++) { 751if(!objects[i].filled) 752add_family_to_write_order(wo, &wo_end, &objects[i]); 753} 754 755if(wo_end != to_pack.nr_objects) 756die(_("ordered%uobjects, expected %"PRIu32), 757 wo_end, to_pack.nr_objects); 758 759return wo; 760} 761 762static off_t write_reused_pack(struct hashfile *f) 763{ 764unsigned char buffer[8192]; 765 off_t to_write, total; 766int fd; 767 768if(!is_pack_valid(reuse_packfile)) 769die(_("packfile is invalid:%s"), reuse_packfile->pack_name); 770 771 fd =git_open(reuse_packfile->pack_name); 772if(fd <0) 773die_errno(_("unable to open packfile for reuse:%s"), 774 reuse_packfile->pack_name); 775 776if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 777die_errno(_("unable to seek in reused packfile")); 778 779if(reuse_packfile_offset <0) 780 reuse_packfile_offset = reuse_packfile->pack_size - the_hash_algo->rawsz; 781 782 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 783 784while(to_write) { 785int read_pack =xread(fd, buffer,sizeof(buffer)); 786 787if(read_pack <=0) 788die_errno(_("unable to read from reused packfile")); 789 790if(read_pack > to_write) 791 read_pack = to_write; 792 793hashwrite(f, buffer, read_pack); 794 to_write -= read_pack; 795 796/* 797 * We don't know the actual number of objects written, 798 * only how many bytes written, how many bytes total, and 799 * how many objects total. So we can fake it by pretending all 800 * objects we are writing are the same size. This gives us a 801 * smooth progress meter, and at the end it matches the true 802 * answer. 803 */ 804 written = reuse_packfile_objects * 805(((double)(total - to_write)) / total); 806display_progress(progress_state, written); 807} 808 809close(fd); 810 written = reuse_packfile_objects; 811display_progress(progress_state, written); 812return reuse_packfile_offset -sizeof(struct pack_header); 813} 814 815static const char no_split_warning[] =N_( 816"disabling bitmap writing, packs are split due to pack.packSizeLimit" 817); 818 819static voidwrite_pack_file(void) 820{ 821uint32_t i =0, j; 822struct hashfile *f; 823 off_t offset; 824uint32_t nr_remaining = nr_result; 825time_t last_mtime =0; 826struct object_entry **write_order; 827 828if(progress > pack_to_stdout) 829 progress_state =start_progress(_("Writing objects"), nr_result); 830ALLOC_ARRAY(written_list, to_pack.nr_objects); 831 write_order =compute_write_order(); 832 833do{ 834struct object_id oid; 835char*pack_tmp_name = NULL; 836 837if(pack_to_stdout) 838 f =hashfd_throughput(1,"<stdout>", progress_state); 839else 840 f =create_tmp_packfile(&pack_tmp_name); 841 842 offset =write_pack_header(f, nr_remaining); 843 844if(reuse_packfile) { 845 off_t packfile_size; 846assert(pack_to_stdout); 847 848 packfile_size =write_reused_pack(f); 849 offset += packfile_size; 850} 851 852 nr_written =0; 853for(; i < to_pack.nr_objects; i++) { 854struct object_entry *e = write_order[i]; 855if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 856break; 857display_progress(progress_state, written); 858} 859 860/* 861 * Did we write the wrong # entries in the header? 862 * If so, rewrite it like in fast-import 863 */ 864if(pack_to_stdout) { 865finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 866}else if(nr_written == nr_remaining) { 867finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 868}else{ 869int fd =finalize_hashfile(f, oid.hash,0); 870fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 871 nr_written, oid.hash, offset); 872close(fd); 873if(write_bitmap_index) { 874warning(_(no_split_warning)); 875 write_bitmap_index =0; 876} 877} 878 879if(!pack_to_stdout) { 880struct stat st; 881struct strbuf tmpname = STRBUF_INIT; 882 883/* 884 * Packs are runtime accessed in their mtime 885 * order since newer packs are more likely to contain 886 * younger objects. So if we are creating multiple 887 * packs then we should modify the mtime of later ones 888 * to preserve this property. 889 */ 890if(stat(pack_tmp_name, &st) <0) { 891warning_errno(_("failed to stat%s"), pack_tmp_name); 892}else if(!last_mtime) { 893 last_mtime = st.st_mtime; 894}else{ 895struct utimbuf utb; 896 utb.actime = st.st_atime; 897 utb.modtime = --last_mtime; 898if(utime(pack_tmp_name, &utb) <0) 899warning_errno(_("failed utime() on%s"), pack_tmp_name); 900} 901 902strbuf_addf(&tmpname,"%s-", base_name); 903 904if(write_bitmap_index) { 905bitmap_writer_set_checksum(oid.hash); 906bitmap_writer_build_type_index( 907&to_pack, written_list, nr_written); 908} 909 910finish_tmp_packfile(&tmpname, pack_tmp_name, 911 written_list, nr_written, 912&pack_idx_opts, oid.hash); 913 914if(write_bitmap_index) { 915strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 916 917stop_progress(&progress_state); 918 919bitmap_writer_show_progress(progress); 920bitmap_writer_reuse_bitmaps(&to_pack); 921bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 922bitmap_writer_build(&to_pack); 923bitmap_writer_finish(written_list, nr_written, 924 tmpname.buf, write_bitmap_options); 925 write_bitmap_index =0; 926} 927 928strbuf_release(&tmpname); 929free(pack_tmp_name); 930puts(oid_to_hex(&oid)); 931} 932 933/* mark written objects as written to previous pack */ 934for(j =0; j < nr_written; j++) { 935 written_list[j]->offset = (off_t)-1; 936} 937 nr_remaining -= nr_written; 938}while(nr_remaining && i < to_pack.nr_objects); 939 940free(written_list); 941free(write_order); 942stop_progress(&progress_state); 943if(written != nr_result) 944die(_("wrote %"PRIu32" objects while expecting %"PRIu32), 945 written, nr_result); 946} 947 948static intno_try_delta(const char*path) 949{ 950static struct attr_check *check; 951 952if(!check) 953 check =attr_check_initl("delta", NULL); 954git_check_attr(&the_index, path, check); 955if(ATTR_FALSE(check->items[0].value)) 956return1; 957return0; 958} 959 960/* 961 * When adding an object, check whether we have already added it 962 * to our packing list. If so, we can skip. However, if we are 963 * being asked to excludei t, but the previous mention was to include 964 * it, make sure to adjust its flags and tweak our numbers accordingly. 965 * 966 * As an optimization, we pass out the index position where we would have 967 * found the item, since that saves us from having to look it up again a 968 * few lines later when we want to add the new entry. 969 */ 970static inthave_duplicate_entry(const struct object_id *oid, 971int exclude, 972uint32_t*index_pos) 973{ 974struct object_entry *entry; 975 976 entry =packlist_find(&to_pack, oid->hash, index_pos); 977if(!entry) 978return0; 979 980if(exclude) { 981if(!entry->preferred_base) 982 nr_result--; 983 entry->preferred_base =1; 984} 985 986return1; 987} 988 989static intwant_found_object(int exclude,struct packed_git *p) 990{ 991if(exclude) 992return1; 993if(incremental) 994return0; 995 996/* 997 * When asked to do --local (do not include an object that appears in a 998 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 999 * an object that appears in a pack marked with .keep), finding a pack1000 * that matches the criteria is sufficient for us to decide to omit it.1001 * However, even if this pack does not satisfy the criteria, we need to1002 * make sure no copy of this object appears in _any_ pack that makes us1003 * to omit the object, so we need to check all the packs.1004 *1005 * We can however first check whether these options can possible matter;1006 * if they do not matter we know we want the object in generated pack.1007 * Otherwise, we signal "-1" at the end to tell the caller that we do1008 * not know either way, and it needs to check more packs.1009 */1010if(!ignore_packed_keep_on_disk &&1011!ignore_packed_keep_in_core &&1012(!local || !have_non_local_packs))1013return1;10141015if(local && !p->pack_local)1016return0;1017if(p->pack_local &&1018((ignore_packed_keep_on_disk && p->pack_keep) ||1019(ignore_packed_keep_in_core && p->pack_keep_in_core)))1020return0;10211022/* we don't know yet; keep looking for more packs */1023return-1;1024}10251026/*1027 * Check whether we want the object in the pack (e.g., we do not want1028 * objects found in non-local stores if the "--local" option was used).1029 *1030 * If the caller already knows an existing pack it wants to take the object1031 * from, that is passed in *found_pack and *found_offset; otherwise this1032 * function finds if there is any pack that has the object and returns the pack1033 * and its offset in these variables.1034 */1035static intwant_object_in_pack(const struct object_id *oid,1036int exclude,1037struct packed_git **found_pack,1038 off_t *found_offset)1039{1040int want;1041struct list_head *pos;10421043if(!exclude && local &&has_loose_object_nonlocal(oid))1044return0;10451046/*1047 * If we already know the pack object lives in, start checks from that1048 * pack - in the usual case when neither --local was given nor .keep files1049 * are present we will determine the answer right now.1050 */1051if(*found_pack) {1052 want =want_found_object(exclude, *found_pack);1053if(want != -1)1054return want;1055}1056list_for_each(pos,get_packed_git_mru(the_repository)) {1057struct packed_git *p =list_entry(pos,struct packed_git, mru);1058 off_t offset;10591060if(p == *found_pack)1061 offset = *found_offset;1062else1063 offset =find_pack_entry_one(oid->hash, p);10641065if(offset) {1066if(!*found_pack) {1067if(!is_pack_valid(p))1068continue;1069*found_offset = offset;1070*found_pack = p;1071}1072 want =want_found_object(exclude, p);1073if(!exclude && want >0)1074list_move(&p->mru,1075get_packed_git_mru(the_repository));1076if(want != -1)1077return want;1078}1079}10801081return1;1082}10831084static voidcreate_object_entry(const struct object_id *oid,1085enum object_type type,1086uint32_t hash,1087int exclude,1088int no_try_delta,1089uint32_t index_pos,1090struct packed_git *found_pack,1091 off_t found_offset)1092{1093struct object_entry *entry;10941095 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1096 entry->hash = hash;1097oe_set_type(entry, type);1098if(exclude)1099 entry->preferred_base =1;1100else1101 nr_result++;1102if(found_pack) {1103oe_set_in_pack(&to_pack, entry, found_pack);1104 entry->in_pack_offset = found_offset;1105}11061107 entry->no_try_delta = no_try_delta;1108}11091110static const char no_closure_warning[] =N_(1111"disabling bitmap writing, as some objects are not being packed"1112);11131114static intadd_object_entry(const struct object_id *oid,enum object_type type,1115const char*name,int exclude)1116{1117struct packed_git *found_pack = NULL;1118 off_t found_offset =0;1119uint32_t index_pos;11201121display_progress(progress_state, ++nr_seen);11221123if(have_duplicate_entry(oid, exclude, &index_pos))1124return0;11251126if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1127/* The pack is missing an object, so it will not have closure */1128if(write_bitmap_index) {1129warning(_(no_closure_warning));1130 write_bitmap_index =0;1131}1132return0;1133}11341135create_object_entry(oid, type,pack_name_hash(name),1136 exclude, name &&no_try_delta(name),1137 index_pos, found_pack, found_offset);1138return1;1139}11401141static intadd_object_entry_from_bitmap(const struct object_id *oid,1142enum object_type type,1143int flags,uint32_t name_hash,1144struct packed_git *pack, off_t offset)1145{1146uint32_t index_pos;11471148display_progress(progress_state, ++nr_seen);11491150if(have_duplicate_entry(oid,0, &index_pos))1151return0;11521153if(!want_object_in_pack(oid,0, &pack, &offset))1154return0;11551156create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1157return1;1158}11591160struct pbase_tree_cache {1161struct object_id oid;1162int ref;1163int temporary;1164void*tree_data;1165unsigned long tree_size;1166};11671168static struct pbase_tree_cache *(pbase_tree_cache[256]);1169static intpbase_tree_cache_ix(const struct object_id *oid)1170{1171return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1172}1173static intpbase_tree_cache_ix_incr(int ix)1174{1175return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1176}11771178static struct pbase_tree {1179struct pbase_tree *next;1180/* This is a phony "cache" entry; we are not1181 * going to evict it or find it through _get()1182 * mechanism -- this is for the toplevel node that1183 * would almost always change with any commit.1184 */1185struct pbase_tree_cache pcache;1186} *pbase_tree;11871188static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1189{1190struct pbase_tree_cache *ent, *nent;1191void*data;1192unsigned long size;1193enum object_type type;1194int neigh;1195int my_ix =pbase_tree_cache_ix(oid);1196int available_ix = -1;11971198/* pbase-tree-cache acts as a limited hashtable.1199 * your object will be found at your index or within a few1200 * slots after that slot if it is cached.1201 */1202for(neigh =0; neigh <8; neigh++) {1203 ent = pbase_tree_cache[my_ix];1204if(ent && !oidcmp(&ent->oid, oid)) {1205 ent->ref++;1206return ent;1207}1208else if(((available_ix <0) && (!ent || !ent->ref)) ||1209((0<= available_ix) &&1210(!ent && pbase_tree_cache[available_ix])))1211 available_ix = my_ix;1212if(!ent)1213break;1214 my_ix =pbase_tree_cache_ix_incr(my_ix);1215}12161217/* Did not find one. Either we got a bogus request or1218 * we need to read and perhaps cache.1219 */1220 data =read_object_file(oid, &type, &size);1221if(!data)1222return NULL;1223if(type != OBJ_TREE) {1224free(data);1225return NULL;1226}12271228/* We need to either cache or return a throwaway copy */12291230if(available_ix <0)1231 ent = NULL;1232else{1233 ent = pbase_tree_cache[available_ix];1234 my_ix = available_ix;1235}12361237if(!ent) {1238 nent =xmalloc(sizeof(*nent));1239 nent->temporary = (available_ix <0);1240}1241else{1242/* evict and reuse */1243free(ent->tree_data);1244 nent = ent;1245}1246oidcpy(&nent->oid, oid);1247 nent->tree_data = data;1248 nent->tree_size = size;1249 nent->ref =1;1250if(!nent->temporary)1251 pbase_tree_cache[my_ix] = nent;1252return nent;1253}12541255static voidpbase_tree_put(struct pbase_tree_cache *cache)1256{1257if(!cache->temporary) {1258 cache->ref--;1259return;1260}1261free(cache->tree_data);1262free(cache);1263}12641265static intname_cmp_len(const char*name)1266{1267int i;1268for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1269;1270return i;1271}12721273static voidadd_pbase_object(struct tree_desc *tree,1274const char*name,1275int cmplen,1276const char*fullname)1277{1278struct name_entry entry;1279int cmp;12801281while(tree_entry(tree,&entry)) {1282if(S_ISGITLINK(entry.mode))1283continue;1284 cmp =tree_entry_len(&entry) != cmplen ?1:1285memcmp(name, entry.path, cmplen);1286if(cmp >0)1287continue;1288if(cmp <0)1289return;1290if(name[cmplen] !='/') {1291add_object_entry(entry.oid,1292object_type(entry.mode),1293 fullname,1);1294return;1295}1296if(S_ISDIR(entry.mode)) {1297struct tree_desc sub;1298struct pbase_tree_cache *tree;1299const char*down = name+cmplen+1;1300int downlen =name_cmp_len(down);13011302 tree =pbase_tree_get(entry.oid);1303if(!tree)1304return;1305init_tree_desc(&sub, tree->tree_data, tree->tree_size);13061307add_pbase_object(&sub, down, downlen, fullname);1308pbase_tree_put(tree);1309}1310}1311}13121313static unsigned*done_pbase_paths;1314static int done_pbase_paths_num;1315static int done_pbase_paths_alloc;1316static intdone_pbase_path_pos(unsigned hash)1317{1318int lo =0;1319int hi = done_pbase_paths_num;1320while(lo < hi) {1321int mi = lo + (hi - lo) /2;1322if(done_pbase_paths[mi] == hash)1323return mi;1324if(done_pbase_paths[mi] < hash)1325 hi = mi;1326else1327 lo = mi +1;1328}1329return-lo-1;1330}13311332static intcheck_pbase_path(unsigned hash)1333{1334int pos =done_pbase_path_pos(hash);1335if(0<= pos)1336return1;1337 pos = -pos -1;1338ALLOC_GROW(done_pbase_paths,1339 done_pbase_paths_num +1,1340 done_pbase_paths_alloc);1341 done_pbase_paths_num++;1342if(pos < done_pbase_paths_num)1343MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1344 done_pbase_paths_num - pos -1);1345 done_pbase_paths[pos] = hash;1346return0;1347}13481349static voidadd_preferred_base_object(const char*name)1350{1351struct pbase_tree *it;1352int cmplen;1353unsigned hash =pack_name_hash(name);13541355if(!num_preferred_base ||check_pbase_path(hash))1356return;13571358 cmplen =name_cmp_len(name);1359for(it = pbase_tree; it; it = it->next) {1360if(cmplen ==0) {1361add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1362}1363else{1364struct tree_desc tree;1365init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1366add_pbase_object(&tree, name, cmplen, name);1367}1368}1369}13701371static voidadd_preferred_base(struct object_id *oid)1372{1373struct pbase_tree *it;1374void*data;1375unsigned long size;1376struct object_id tree_oid;13771378if(window <= num_preferred_base++)1379return;13801381 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1382if(!data)1383return;13841385for(it = pbase_tree; it; it = it->next) {1386if(!oidcmp(&it->pcache.oid, &tree_oid)) {1387free(data);1388return;1389}1390}13911392 it =xcalloc(1,sizeof(*it));1393 it->next = pbase_tree;1394 pbase_tree = it;13951396oidcpy(&it->pcache.oid, &tree_oid);1397 it->pcache.tree_data = data;1398 it->pcache.tree_size = size;1399}14001401static voidcleanup_preferred_base(void)1402{1403struct pbase_tree *it;1404unsigned i;14051406 it = pbase_tree;1407 pbase_tree = NULL;1408while(it) {1409struct pbase_tree *tmp = it;1410 it = tmp->next;1411free(tmp->pcache.tree_data);1412free(tmp);1413}14141415for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1416if(!pbase_tree_cache[i])1417continue;1418free(pbase_tree_cache[i]->tree_data);1419FREE_AND_NULL(pbase_tree_cache[i]);1420}14211422FREE_AND_NULL(done_pbase_paths);1423 done_pbase_paths_num = done_pbase_paths_alloc =0;1424}14251426static voidcheck_object(struct object_entry *entry)1427{1428unsigned long canonical_size;14291430if(IN_PACK(entry)) {1431struct packed_git *p =IN_PACK(entry);1432struct pack_window *w_curs = NULL;1433const unsigned char*base_ref = NULL;1434struct object_entry *base_entry;1435unsigned long used, used_0;1436unsigned long avail;1437 off_t ofs;1438unsigned char*buf, c;1439enum object_type type;1440unsigned long in_pack_size;14411442 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14431444/*1445 * We want in_pack_type even if we do not reuse delta1446 * since non-delta representations could still be reused.1447 */1448 used =unpack_object_header_buffer(buf, avail,1449&type,1450&in_pack_size);1451if(used ==0)1452goto give_up;14531454if(type <0)1455BUG("invalid type%d", type);1456 entry->in_pack_type = type;14571458/*1459 * Determine if this is a delta and if so whether we can1460 * reuse it or not. Otherwise let's find out as cheaply as1461 * possible what the actual type and size for this object is.1462 */1463switch(entry->in_pack_type) {1464default:1465/* Not a delta hence we've already got all we need. */1466oe_set_type(entry, entry->in_pack_type);1467SET_SIZE(entry, in_pack_size);1468 entry->in_pack_header_size = used;1469if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1470goto give_up;1471unuse_pack(&w_curs);1472return;1473case OBJ_REF_DELTA:1474if(reuse_delta && !entry->preferred_base)1475 base_ref =use_pack(p, &w_curs,1476 entry->in_pack_offset + used, NULL);1477 entry->in_pack_header_size = used + the_hash_algo->rawsz;1478break;1479case OBJ_OFS_DELTA:1480 buf =use_pack(p, &w_curs,1481 entry->in_pack_offset + used, NULL);1482 used_0 =0;1483 c = buf[used_0++];1484 ofs = c &127;1485while(c &128) {1486 ofs +=1;1487if(!ofs ||MSB(ofs,7)) {1488error(_("delta base offset overflow in pack for%s"),1489oid_to_hex(&entry->idx.oid));1490goto give_up;1491}1492 c = buf[used_0++];1493 ofs = (ofs <<7) + (c &127);1494}1495 ofs = entry->in_pack_offset - ofs;1496if(ofs <=0|| ofs >= entry->in_pack_offset) {1497error(_("delta base offset out of bound for%s"),1498oid_to_hex(&entry->idx.oid));1499goto give_up;1500}1501if(reuse_delta && !entry->preferred_base) {1502struct revindex_entry *revidx;1503 revidx =find_pack_revindex(p, ofs);1504if(!revidx)1505goto give_up;1506 base_ref =nth_packed_object_sha1(p, revidx->nr);1507}1508 entry->in_pack_header_size = used + used_0;1509break;1510}15111512if(base_ref && (base_entry =packlist_find(&to_pack, base_ref, NULL))) {1513/*1514 * If base_ref was set above that means we wish to1515 * reuse delta data, and we even found that base1516 * in the list of objects we want to pack. Goodie!1517 *1518 * Depth value does not matter - find_deltas() will1519 * never consider reused delta as the base object to1520 * deltify other objects against, in order to avoid1521 * circular deltas.1522 */1523oe_set_type(entry, entry->in_pack_type);1524SET_SIZE(entry, in_pack_size);/* delta size */1525SET_DELTA(entry, base_entry);1526SET_DELTA_SIZE(entry, in_pack_size);1527 entry->delta_sibling_idx = base_entry->delta_child_idx;1528SET_DELTA_CHILD(base_entry, entry);1529unuse_pack(&w_curs);1530return;1531}15321533if(oe_type(entry)) {1534 off_t delta_pos;15351536/*1537 * This must be a delta and we already know what the1538 * final object type is. Let's extract the actual1539 * object size from the delta header.1540 */1541 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1542 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1543if(canonical_size ==0)1544goto give_up;1545SET_SIZE(entry, canonical_size);1546unuse_pack(&w_curs);1547return;1548}15491550/*1551 * No choice but to fall back to the recursive delta walk1552 * with sha1_object_info() to find about the object type1553 * at this point...1554 */1555 give_up:1556unuse_pack(&w_curs);1557}15581559oe_set_type(entry,1560oid_object_info(the_repository, &entry->idx.oid, &canonical_size));1561if(entry->type_valid) {1562SET_SIZE(entry, canonical_size);1563}else{1564/*1565 * Bad object type is checked in prepare_pack(). This is1566 * to permit a missing preferred base object to be ignored1567 * as a preferred base. Doing so can result in a larger1568 * pack file, but the transfer will still take place.1569 */1570}1571}15721573static intpack_offset_sort(const void*_a,const void*_b)1574{1575const struct object_entry *a = *(struct object_entry **)_a;1576const struct object_entry *b = *(struct object_entry **)_b;1577const struct packed_git *a_in_pack =IN_PACK(a);1578const struct packed_git *b_in_pack =IN_PACK(b);15791580/* avoid filesystem trashing with loose objects */1581if(!a_in_pack && !b_in_pack)1582returnoidcmp(&a->idx.oid, &b->idx.oid);15831584if(a_in_pack < b_in_pack)1585return-1;1586if(a_in_pack > b_in_pack)1587return1;1588return a->in_pack_offset < b->in_pack_offset ? -1:1589(a->in_pack_offset > b->in_pack_offset);1590}15911592/*1593 * Drop an on-disk delta we were planning to reuse. Naively, this would1594 * just involve blanking out the "delta" field, but we have to deal1595 * with some extra book-keeping:1596 *1597 * 1. Removing ourselves from the delta_sibling linked list.1598 *1599 * 2. Updating our size/type to the non-delta representation. These were1600 * either not recorded initially (size) or overwritten with the delta type1601 * (type) when check_object() decided to reuse the delta.1602 *1603 * 3. Resetting our delta depth, as we are now a base object.1604 */1605static voiddrop_reused_delta(struct object_entry *entry)1606{1607unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1608struct object_info oi = OBJECT_INFO_INIT;1609enum object_type type;1610unsigned long size;16111612while(*idx) {1613struct object_entry *oe = &to_pack.objects[*idx -1];16141615if(oe == entry)1616*idx = oe->delta_sibling_idx;1617else1618 idx = &oe->delta_sibling_idx;1619}1620SET_DELTA(entry, NULL);1621 entry->depth =0;16221623 oi.sizep = &size;1624 oi.typep = &type;1625if(packed_object_info(the_repository,IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1626/*1627 * We failed to get the info from this pack for some reason;1628 * fall back to sha1_object_info, which may find another copy.1629 * And if that fails, the error will be recorded in oe_type(entry)1630 * and dealt with in prepare_pack().1631 */1632oe_set_type(entry,1633oid_object_info(the_repository, &entry->idx.oid, &size));1634}else{1635oe_set_type(entry, type);1636}1637SET_SIZE(entry, size);1638}16391640/*1641 * Follow the chain of deltas from this entry onward, throwing away any links1642 * that cause us to hit a cycle (as determined by the DFS state flags in1643 * the entries).1644 *1645 * We also detect too-long reused chains that would violate our --depth1646 * limit.1647 */1648static voidbreak_delta_chains(struct object_entry *entry)1649{1650/*1651 * The actual depth of each object we will write is stored as an int,1652 * as it cannot exceed our int "depth" limit. But before we break1653 * changes based no that limit, we may potentially go as deep as the1654 * number of objects, which is elsewhere bounded to a uint32_t.1655 */1656uint32_t total_depth;1657struct object_entry *cur, *next;16581659for(cur = entry, total_depth =0;1660 cur;1661 cur =DELTA(cur), total_depth++) {1662if(cur->dfs_state == DFS_DONE) {1663/*1664 * We've already seen this object and know it isn't1665 * part of a cycle. We do need to append its depth1666 * to our count.1667 */1668 total_depth += cur->depth;1669break;1670}16711672/*1673 * We break cycles before looping, so an ACTIVE state (or any1674 * other cruft which made its way into the state variable)1675 * is a bug.1676 */1677if(cur->dfs_state != DFS_NONE)1678BUG("confusing delta dfs state in first pass:%d",1679 cur->dfs_state);16801681/*1682 * Now we know this is the first time we've seen the object. If1683 * it's not a delta, we're done traversing, but we'll mark it1684 * done to save time on future traversals.1685 */1686if(!DELTA(cur)) {1687 cur->dfs_state = DFS_DONE;1688break;1689}16901691/*1692 * Mark ourselves as active and see if the next step causes1693 * us to cycle to another active object. It's important to do1694 * this _before_ we loop, because it impacts where we make the1695 * cut, and thus how our total_depth counter works.1696 * E.g., We may see a partial loop like:1697 *1698 * A -> B -> C -> D -> B1699 *1700 * Cutting B->C breaks the cycle. But now the depth of A is1701 * only 1, and our total_depth counter is at 3. The size of the1702 * error is always one less than the size of the cycle we1703 * broke. Commits C and D were "lost" from A's chain.1704 *1705 * If we instead cut D->B, then the depth of A is correct at 3.1706 * We keep all commits in the chain that we examined.1707 */1708 cur->dfs_state = DFS_ACTIVE;1709if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1710drop_reused_delta(cur);1711 cur->dfs_state = DFS_DONE;1712break;1713}1714}17151716/*1717 * And now that we've gone all the way to the bottom of the chain, we1718 * need to clear the active flags and set the depth fields as1719 * appropriate. Unlike the loop above, which can quit when it drops a1720 * delta, we need to keep going to look for more depth cuts. So we need1721 * an extra "next" pointer to keep going after we reset cur->delta.1722 */1723for(cur = entry; cur; cur = next) {1724 next =DELTA(cur);17251726/*1727 * We should have a chain of zero or more ACTIVE states down to1728 * a final DONE. We can quit after the DONE, because either it1729 * has no bases, or we've already handled them in a previous1730 * call.1731 */1732if(cur->dfs_state == DFS_DONE)1733break;1734else if(cur->dfs_state != DFS_ACTIVE)1735BUG("confusing delta dfs state in second pass:%d",1736 cur->dfs_state);17371738/*1739 * If the total_depth is more than depth, then we need to snip1740 * the chain into two or more smaller chains that don't exceed1741 * the maximum depth. Most of the resulting chains will contain1742 * (depth + 1) entries (i.e., depth deltas plus one base), and1743 * the last chain (i.e., the one containing entry) will contain1744 * whatever entries are left over, namely1745 * (total_depth % (depth + 1)) of them.1746 *1747 * Since we are iterating towards decreasing depth, we need to1748 * decrement total_depth as we go, and we need to write to the1749 * entry what its final depth will be after all of the1750 * snipping. Since we're snipping into chains of length (depth1751 * + 1) entries, the final depth of an entry will be its1752 * original depth modulo (depth + 1). Any time we encounter an1753 * entry whose final depth is supposed to be zero, we snip it1754 * from its delta base, thereby making it so.1755 */1756 cur->depth = (total_depth--) % (depth +1);1757if(!cur->depth)1758drop_reused_delta(cur);17591760 cur->dfs_state = DFS_DONE;1761}1762}17631764static voidget_object_details(void)1765{1766uint32_t i;1767struct object_entry **sorted_by_offset;17681769if(progress)1770 progress_state =start_progress(_("Counting objects"),1771 to_pack.nr_objects);17721773 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1774for(i =0; i < to_pack.nr_objects; i++)1775 sorted_by_offset[i] = to_pack.objects + i;1776QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17771778for(i =0; i < to_pack.nr_objects; i++) {1779struct object_entry *entry = sorted_by_offset[i];1780check_object(entry);1781if(entry->type_valid &&1782oe_size_greater_than(&to_pack, entry, big_file_threshold))1783 entry->no_try_delta =1;1784display_progress(progress_state, i +1);1785}1786stop_progress(&progress_state);17871788/*1789 * This must happen in a second pass, since we rely on the delta1790 * information for the whole list being completed.1791 */1792for(i =0; i < to_pack.nr_objects; i++)1793break_delta_chains(&to_pack.objects[i]);17941795free(sorted_by_offset);1796}17971798/*1799 * We search for deltas in a list sorted by type, by filename hash, and then1800 * by size, so that we see progressively smaller and smaller files.1801 * That's because we prefer deltas to be from the bigger file1802 * to the smaller -- deletes are potentially cheaper, but perhaps1803 * more importantly, the bigger file is likely the more recent1804 * one. The deepest deltas are therefore the oldest objects which are1805 * less susceptible to be accessed often.1806 */1807static inttype_size_sort(const void*_a,const void*_b)1808{1809const struct object_entry *a = *(struct object_entry **)_a;1810const struct object_entry *b = *(struct object_entry **)_b;1811enum object_type a_type =oe_type(a);1812enum object_type b_type =oe_type(b);1813unsigned long a_size =SIZE(a);1814unsigned long b_size =SIZE(b);18151816if(a_type > b_type)1817return-1;1818if(a_type < b_type)1819return1;1820if(a->hash > b->hash)1821return-1;1822if(a->hash < b->hash)1823return1;1824if(a->preferred_base > b->preferred_base)1825return-1;1826if(a->preferred_base < b->preferred_base)1827return1;1828if(a_size > b_size)1829return-1;1830if(a_size < b_size)1831return1;1832return a < b ? -1: (a > b);/* newest first */1833}18341835struct unpacked {1836struct object_entry *entry;1837void*data;1838struct delta_index *index;1839unsigned depth;1840};18411842static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1843unsigned long delta_size)1844{1845if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1846return0;18471848if(delta_size < cache_max_small_delta_size)1849return1;18501851/* cache delta, if objects are large enough compared to delta size */1852if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1853return1;18541855return0;1856}18571858#ifndef NO_PTHREADS18591860/* Protect access to object database */1861static pthread_mutex_t read_mutex;1862#define read_lock() pthread_mutex_lock(&read_mutex)1863#define read_unlock() pthread_mutex_unlock(&read_mutex)18641865/* Protect delta_cache_size */1866static pthread_mutex_t cache_mutex;1867#define cache_lock() pthread_mutex_lock(&cache_mutex)1868#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18691870/*1871 * Protect object list partitioning (e.g. struct thread_param) and1872 * progress_state1873 */1874static pthread_mutex_t progress_mutex;1875#define progress_lock() pthread_mutex_lock(&progress_mutex)1876#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18771878/*1879 * Access to struct object_entry is unprotected since each thread owns1880 * a portion of the main object list. Just don't access object entries1881 * ahead in the list because they can be stolen and would need1882 * progress_mutex for protection.1883 */1884#else18851886#define read_lock() (void)01887#define read_unlock() (void)01888#define cache_lock() (void)01889#define cache_unlock() (void)01890#define progress_lock() (void)01891#define progress_unlock() (void)018921893#endif18941895/*1896 * Return the size of the object without doing any delta1897 * reconstruction (so non-deltas are true object sizes, but deltas1898 * return the size of the delta data).1899 */1900unsigned longoe_get_size_slow(struct packing_data *pack,1901const struct object_entry *e)1902{1903struct packed_git *p;1904struct pack_window *w_curs;1905unsigned char*buf;1906enum object_type type;1907unsigned long used, avail, size;19081909if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1910read_lock();1911if(oid_object_info(the_repository, &e->idx.oid, &size) <0)1912die(_("unable to get size of%s"),1913oid_to_hex(&e->idx.oid));1914read_unlock();1915return size;1916}19171918 p =oe_in_pack(pack, e);1919if(!p)1920BUG("when e->type is a delta, it must belong to a pack");19211922read_lock();1923 w_curs = NULL;1924 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);1925 used =unpack_object_header_buffer(buf, avail, &type, &size);1926if(used ==0)1927die(_("unable to parse object header of%s"),1928oid_to_hex(&e->idx.oid));19291930unuse_pack(&w_curs);1931read_unlock();1932return size;1933}19341935static inttry_delta(struct unpacked *trg,struct unpacked *src,1936unsigned max_depth,unsigned long*mem_usage)1937{1938struct object_entry *trg_entry = trg->entry;1939struct object_entry *src_entry = src->entry;1940unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1941unsigned ref_depth;1942enum object_type type;1943void*delta_buf;19441945/* Don't bother doing diffs between different types */1946if(oe_type(trg_entry) !=oe_type(src_entry))1947return-1;19481949/*1950 * We do not bother to try a delta that we discarded on an1951 * earlier try, but only when reusing delta data. Note that1952 * src_entry that is marked as the preferred_base should always1953 * be considered, as even if we produce a suboptimal delta against1954 * it, we will still save the transfer cost, as we already know1955 * the other side has it and we won't send src_entry at all.1956 */1957if(reuse_delta &&IN_PACK(trg_entry) &&1958IN_PACK(trg_entry) ==IN_PACK(src_entry) &&1959!src_entry->preferred_base &&1960 trg_entry->in_pack_type != OBJ_REF_DELTA &&1961 trg_entry->in_pack_type != OBJ_OFS_DELTA)1962return0;19631964/* Let's not bust the allowed depth. */1965if(src->depth >= max_depth)1966return0;19671968/* Now some size filtering heuristics. */1969 trg_size =SIZE(trg_entry);1970if(!DELTA(trg_entry)) {1971 max_size = trg_size/2- the_hash_algo->rawsz;1972 ref_depth =1;1973}else{1974 max_size =DELTA_SIZE(trg_entry);1975 ref_depth = trg->depth;1976}1977 max_size = (uint64_t)max_size * (max_depth - src->depth) /1978(max_depth - ref_depth +1);1979if(max_size ==0)1980return0;1981 src_size =SIZE(src_entry);1982 sizediff = src_size < trg_size ? trg_size - src_size :0;1983if(sizediff >= max_size)1984return0;1985if(trg_size < src_size /32)1986return0;19871988/* Load data if not already done */1989if(!trg->data) {1990read_lock();1991 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1992read_unlock();1993if(!trg->data)1994die(_("object%scannot be read"),1995oid_to_hex(&trg_entry->idx.oid));1996if(sz != trg_size)1997die(_("object%sinconsistent object length (%lu vs%lu)"),1998oid_to_hex(&trg_entry->idx.oid), sz,1999 trg_size);2000*mem_usage += sz;2001}2002if(!src->data) {2003read_lock();2004 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);2005read_unlock();2006if(!src->data) {2007if(src_entry->preferred_base) {2008static int warned =0;2009if(!warned++)2010warning(_("object%scannot be read"),2011oid_to_hex(&src_entry->idx.oid));2012/*2013 * Those objects are not included in the2014 * resulting pack. Be resilient and ignore2015 * them if they can't be read, in case the2016 * pack could be created nevertheless.2017 */2018return0;2019}2020die(_("object%scannot be read"),2021oid_to_hex(&src_entry->idx.oid));2022}2023if(sz != src_size)2024die(_("object%sinconsistent object length (%lu vs%lu)"),2025oid_to_hex(&src_entry->idx.oid), sz,2026 src_size);2027*mem_usage += sz;2028}2029if(!src->index) {2030 src->index =create_delta_index(src->data, src_size);2031if(!src->index) {2032static int warned =0;2033if(!warned++)2034warning(_("suboptimal pack - out of memory"));2035return0;2036}2037*mem_usage +=sizeof_delta_index(src->index);2038}20392040 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2041if(!delta_buf)2042return0;20432044if(DELTA(trg_entry)) {2045/* Prefer only shallower same-sized deltas. */2046if(delta_size ==DELTA_SIZE(trg_entry) &&2047 src->depth +1>= trg->depth) {2048free(delta_buf);2049return0;2050}2051}20522053/*2054 * Handle memory allocation outside of the cache2055 * accounting lock. Compiler will optimize the strangeness2056 * away when NO_PTHREADS is defined.2057 */2058free(trg_entry->delta_data);2059cache_lock();2060if(trg_entry->delta_data) {2061 delta_cache_size -=DELTA_SIZE(trg_entry);2062 trg_entry->delta_data = NULL;2063}2064if(delta_cacheable(src_size, trg_size, delta_size)) {2065 delta_cache_size += delta_size;2066cache_unlock();2067 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2068}else{2069cache_unlock();2070free(delta_buf);2071}20722073SET_DELTA(trg_entry, src_entry);2074SET_DELTA_SIZE(trg_entry, delta_size);2075 trg->depth = src->depth +1;20762077return1;2078}20792080static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2081{2082struct object_entry *child =DELTA_CHILD(me);2083unsigned int m = n;2084while(child) {2085unsigned int c =check_delta_limit(child, n +1);2086if(m < c)2087 m = c;2088 child =DELTA_SIBLING(child);2089}2090return m;2091}20922093static unsigned longfree_unpacked(struct unpacked *n)2094{2095unsigned long freed_mem =sizeof_delta_index(n->index);2096free_delta_index(n->index);2097 n->index = NULL;2098if(n->data) {2099 freed_mem +=SIZE(n->entry);2100FREE_AND_NULL(n->data);2101}2102 n->entry = NULL;2103 n->depth =0;2104return freed_mem;2105}21062107static voidfind_deltas(struct object_entry **list,unsigned*list_size,2108int window,int depth,unsigned*processed)2109{2110uint32_t i, idx =0, count =0;2111struct unpacked *array;2112unsigned long mem_usage =0;21132114 array =xcalloc(window,sizeof(struct unpacked));21152116for(;;) {2117struct object_entry *entry;2118struct unpacked *n = array + idx;2119int j, max_depth, best_base = -1;21202121progress_lock();2122if(!*list_size) {2123progress_unlock();2124break;2125}2126 entry = *list++;2127(*list_size)--;2128if(!entry->preferred_base) {2129(*processed)++;2130display_progress(progress_state, *processed);2131}2132progress_unlock();21332134 mem_usage -=free_unpacked(n);2135 n->entry = entry;21362137while(window_memory_limit &&2138 mem_usage > window_memory_limit &&2139 count >1) {2140uint32_t tail = (idx + window - count) % window;2141 mem_usage -=free_unpacked(array + tail);2142 count--;2143}21442145/* We do not compute delta to *create* objects we are not2146 * going to pack.2147 */2148if(entry->preferred_base)2149goto next;21502151/*2152 * If the current object is at pack edge, take the depth the2153 * objects that depend on the current object into account2154 * otherwise they would become too deep.2155 */2156 max_depth = depth;2157if(DELTA_CHILD(entry)) {2158 max_depth -=check_delta_limit(entry,0);2159if(max_depth <=0)2160goto next;2161}21622163 j = window;2164while(--j >0) {2165int ret;2166uint32_t other_idx = idx + j;2167struct unpacked *m;2168if(other_idx >= window)2169 other_idx -= window;2170 m = array + other_idx;2171if(!m->entry)2172break;2173 ret =try_delta(n, m, max_depth, &mem_usage);2174if(ret <0)2175break;2176else if(ret >0)2177 best_base = other_idx;2178}21792180/*2181 * If we decided to cache the delta data, then it is best2182 * to compress it right away. First because we have to do2183 * it anyway, and doing it here while we're threaded will2184 * save a lot of time in the non threaded write phase,2185 * as well as allow for caching more deltas within2186 * the same cache size limit.2187 * ...2188 * But only if not writing to stdout, since in that case2189 * the network is most likely throttling writes anyway,2190 * and therefore it is best to go to the write phase ASAP2191 * instead, as we can afford spending more time compressing2192 * between writes at that moment.2193 */2194if(entry->delta_data && !pack_to_stdout) {2195unsigned long size;21962197 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2198if(size < (1U<< OE_Z_DELTA_BITS)) {2199 entry->z_delta_size = size;2200cache_lock();2201 delta_cache_size -=DELTA_SIZE(entry);2202 delta_cache_size += entry->z_delta_size;2203cache_unlock();2204}else{2205FREE_AND_NULL(entry->delta_data);2206 entry->z_delta_size =0;2207}2208}22092210/* if we made n a delta, and if n is already at max2211 * depth, leaving it in the window is pointless. we2212 * should evict it first.2213 */2214if(DELTA(entry) && max_depth <= n->depth)2215continue;22162217/*2218 * Move the best delta base up in the window, after the2219 * currently deltified object, to keep it longer. It will2220 * be the first base object to be attempted next.2221 */2222if(DELTA(entry)) {2223struct unpacked swap = array[best_base];2224int dist = (window + idx - best_base) % window;2225int dst = best_base;2226while(dist--) {2227int src = (dst +1) % window;2228 array[dst] = array[src];2229 dst = src;2230}2231 array[dst] = swap;2232}22332234 next:2235 idx++;2236if(count +1< window)2237 count++;2238if(idx >= window)2239 idx =0;2240}22412242for(i =0; i < window; ++i) {2243free_delta_index(array[i].index);2244free(array[i].data);2245}2246free(array);2247}22482249#ifndef NO_PTHREADS22502251static voidtry_to_free_from_threads(size_t size)2252{2253read_lock();2254release_pack_memory(size);2255read_unlock();2256}22572258static try_to_free_t old_try_to_free_routine;22592260/*2261 * The main object list is split into smaller lists, each is handed to2262 * one worker.2263 *2264 * The main thread waits on the condition that (at least) one of the workers2265 * has stopped working (which is indicated in the .working member of2266 * struct thread_params).2267 *2268 * When a work thread has completed its work, it sets .working to 0 and2269 * signals the main thread and waits on the condition that .data_ready2270 * becomes 1.2271 *2272 * The main thread steals half of the work from the worker that has2273 * most work left to hand it to the idle worker.2274 */22752276struct thread_params {2277 pthread_t thread;2278struct object_entry **list;2279unsigned list_size;2280unsigned remaining;2281int window;2282int depth;2283int working;2284int data_ready;2285 pthread_mutex_t mutex;2286 pthread_cond_t cond;2287unsigned*processed;2288};22892290static pthread_cond_t progress_cond;22912292/*2293 * Mutex and conditional variable can't be statically-initialized on Windows.2294 */2295static voidinit_threaded_search(void)2296{2297init_recursive_mutex(&read_mutex);2298pthread_mutex_init(&cache_mutex, NULL);2299pthread_mutex_init(&progress_mutex, NULL);2300pthread_cond_init(&progress_cond, NULL);2301 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2302}23032304static voidcleanup_threaded_search(void)2305{2306set_try_to_free_routine(old_try_to_free_routine);2307pthread_cond_destroy(&progress_cond);2308pthread_mutex_destroy(&read_mutex);2309pthread_mutex_destroy(&cache_mutex);2310pthread_mutex_destroy(&progress_mutex);2311}23122313static void*threaded_find_deltas(void*arg)2314{2315struct thread_params *me = arg;23162317progress_lock();2318while(me->remaining) {2319progress_unlock();23202321find_deltas(me->list, &me->remaining,2322 me->window, me->depth, me->processed);23232324progress_lock();2325 me->working =0;2326pthread_cond_signal(&progress_cond);2327progress_unlock();23282329/*2330 * We must not set ->data_ready before we wait on the2331 * condition because the main thread may have set it to 12332 * before we get here. In order to be sure that new2333 * work is available if we see 1 in ->data_ready, it2334 * was initialized to 0 before this thread was spawned2335 * and we reset it to 0 right away.2336 */2337pthread_mutex_lock(&me->mutex);2338while(!me->data_ready)2339pthread_cond_wait(&me->cond, &me->mutex);2340 me->data_ready =0;2341pthread_mutex_unlock(&me->mutex);23422343progress_lock();2344}2345progress_unlock();2346/* leave ->working 1 so that this doesn't get more work assigned */2347return NULL;2348}23492350static voidll_find_deltas(struct object_entry **list,unsigned list_size,2351int window,int depth,unsigned*processed)2352{2353struct thread_params *p;2354int i, ret, active_threads =0;23552356init_threaded_search();23572358if(delta_search_threads <=1) {2359find_deltas(list, &list_size, window, depth, processed);2360cleanup_threaded_search();2361return;2362}2363if(progress > pack_to_stdout)2364fprintf_ln(stderr,_("Delta compression using up to%dthreads"),2365 delta_search_threads);2366 p =xcalloc(delta_search_threads,sizeof(*p));23672368/* Partition the work amongst work threads. */2369for(i =0; i < delta_search_threads; i++) {2370unsigned sub_size = list_size / (delta_search_threads - i);23712372/* don't use too small segments or no deltas will be found */2373if(sub_size <2*window && i+1< delta_search_threads)2374 sub_size =0;23752376 p[i].window = window;2377 p[i].depth = depth;2378 p[i].processed = processed;2379 p[i].working =1;2380 p[i].data_ready =0;23812382/* try to split chunks on "path" boundaries */2383while(sub_size && sub_size < list_size &&2384 list[sub_size]->hash &&2385 list[sub_size]->hash == list[sub_size-1]->hash)2386 sub_size++;23872388 p[i].list = list;2389 p[i].list_size = sub_size;2390 p[i].remaining = sub_size;23912392 list += sub_size;2393 list_size -= sub_size;2394}23952396/* Start work threads. */2397for(i =0; i < delta_search_threads; i++) {2398if(!p[i].list_size)2399continue;2400pthread_mutex_init(&p[i].mutex, NULL);2401pthread_cond_init(&p[i].cond, NULL);2402 ret =pthread_create(&p[i].thread, NULL,2403 threaded_find_deltas, &p[i]);2404if(ret)2405die(_("unable to create thread:%s"),strerror(ret));2406 active_threads++;2407}24082409/*2410 * Now let's wait for work completion. Each time a thread is done2411 * with its work, we steal half of the remaining work from the2412 * thread with the largest number of unprocessed objects and give2413 * it to that newly idle thread. This ensure good load balancing2414 * until the remaining object list segments are simply too short2415 * to be worth splitting anymore.2416 */2417while(active_threads) {2418struct thread_params *target = NULL;2419struct thread_params *victim = NULL;2420unsigned sub_size =0;24212422progress_lock();2423for(;;) {2424for(i =0; !target && i < delta_search_threads; i++)2425if(!p[i].working)2426 target = &p[i];2427if(target)2428break;2429pthread_cond_wait(&progress_cond, &progress_mutex);2430}24312432for(i =0; i < delta_search_threads; i++)2433if(p[i].remaining >2*window &&2434(!victim || victim->remaining < p[i].remaining))2435 victim = &p[i];2436if(victim) {2437 sub_size = victim->remaining /2;2438 list = victim->list + victim->list_size - sub_size;2439while(sub_size && list[0]->hash &&2440 list[0]->hash == list[-1]->hash) {2441 list++;2442 sub_size--;2443}2444if(!sub_size) {2445/*2446 * It is possible for some "paths" to have2447 * so many objects that no hash boundary2448 * might be found. Let's just steal the2449 * exact half in that case.2450 */2451 sub_size = victim->remaining /2;2452 list -= sub_size;2453}2454 target->list = list;2455 victim->list_size -= sub_size;2456 victim->remaining -= sub_size;2457}2458 target->list_size = sub_size;2459 target->remaining = sub_size;2460 target->working =1;2461progress_unlock();24622463pthread_mutex_lock(&target->mutex);2464 target->data_ready =1;2465pthread_cond_signal(&target->cond);2466pthread_mutex_unlock(&target->mutex);24672468if(!sub_size) {2469pthread_join(target->thread, NULL);2470pthread_cond_destroy(&target->cond);2471pthread_mutex_destroy(&target->mutex);2472 active_threads--;2473}2474}2475cleanup_threaded_search();2476free(p);2477}24782479#else2480#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2481#endif24822483static voidadd_tag_chain(const struct object_id *oid)2484{2485struct tag *tag;24862487/*2488 * We catch duplicates already in add_object_entry(), but we'd2489 * prefer to do this extra check to avoid having to parse the2490 * tag at all if we already know that it's being packed (e.g., if2491 * it was included via bitmaps, we would not have parsed it2492 * previously).2493 */2494if(packlist_find(&to_pack, oid->hash, NULL))2495return;24962497 tag =lookup_tag(the_repository, oid);2498while(1) {2499if(!tag ||parse_tag(tag) || !tag->tagged)2500die(_("unable to pack objects reachable from tag%s"),2501oid_to_hex(oid));25022503add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);25042505if(tag->tagged->type != OBJ_TAG)2506return;25072508 tag = (struct tag *)tag->tagged;2509}2510}25112512static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2513{2514struct object_id peeled;25152516if(starts_with(path,"refs/tags/") &&/* is a tag? */2517!peel_ref(path, &peeled) &&/* peelable? */2518packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2519add_tag_chain(oid);2520return0;2521}25222523static voidprepare_pack(int window,int depth)2524{2525struct object_entry **delta_list;2526uint32_t i, nr_deltas;2527unsigned n;25282529get_object_details();25302531/*2532 * If we're locally repacking then we need to be doubly careful2533 * from now on in order to make sure no stealth corruption gets2534 * propagated to the new pack. Clients receiving streamed packs2535 * should validate everything they get anyway so no need to incur2536 * the additional cost here in that case.2537 */2538if(!pack_to_stdout)2539 do_check_packed_object_crc =1;25402541if(!to_pack.nr_objects || !window || !depth)2542return;25432544ALLOC_ARRAY(delta_list, to_pack.nr_objects);2545 nr_deltas = n =0;25462547for(i =0; i < to_pack.nr_objects; i++) {2548struct object_entry *entry = to_pack.objects + i;25492550if(DELTA(entry))2551/* This happens if we decided to reuse existing2552 * delta from a pack. "reuse_delta &&" is implied.2553 */2554continue;25552556if(!entry->type_valid ||2557oe_size_less_than(&to_pack, entry,50))2558continue;25592560if(entry->no_try_delta)2561continue;25622563if(!entry->preferred_base) {2564 nr_deltas++;2565if(oe_type(entry) <0)2566die(_("unable to get type of object%s"),2567oid_to_hex(&entry->idx.oid));2568}else{2569if(oe_type(entry) <0) {2570/*2571 * This object is not found, but we2572 * don't have to include it anyway.2573 */2574continue;2575}2576}25772578 delta_list[n++] = entry;2579}25802581if(nr_deltas && n >1) {2582unsigned nr_done =0;2583if(progress)2584 progress_state =start_progress(_("Compressing objects"),2585 nr_deltas);2586QSORT(delta_list, n, type_size_sort);2587ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2588stop_progress(&progress_state);2589if(nr_done != nr_deltas)2590die(_("inconsistency with delta count"));2591}2592free(delta_list);2593}25942595static intgit_pack_config(const char*k,const char*v,void*cb)2596{2597if(!strcmp(k,"pack.window")) {2598 window =git_config_int(k, v);2599return0;2600}2601if(!strcmp(k,"pack.windowmemory")) {2602 window_memory_limit =git_config_ulong(k, v);2603return0;2604}2605if(!strcmp(k,"pack.depth")) {2606 depth =git_config_int(k, v);2607return0;2608}2609if(!strcmp(k,"pack.deltacachesize")) {2610 max_delta_cache_size =git_config_int(k, v);2611return0;2612}2613if(!strcmp(k,"pack.deltacachelimit")) {2614 cache_max_small_delta_size =git_config_int(k, v);2615return0;2616}2617if(!strcmp(k,"pack.writebitmaphashcache")) {2618if(git_config_bool(k, v))2619 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2620else2621 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2622}2623if(!strcmp(k,"pack.usebitmaps")) {2624 use_bitmap_index_default =git_config_bool(k, v);2625return0;2626}2627if(!strcmp(k,"pack.threads")) {2628 delta_search_threads =git_config_int(k, v);2629if(delta_search_threads <0)2630die(_("invalid number of threads specified (%d)"),2631 delta_search_threads);2632#ifdef NO_PTHREADS2633if(delta_search_threads !=1) {2634warning(_("no threads support, ignoring%s"), k);2635 delta_search_threads =0;2636}2637#endif2638return0;2639}2640if(!strcmp(k,"pack.indexversion")) {2641 pack_idx_opts.version =git_config_int(k, v);2642if(pack_idx_opts.version >2)2643die(_("bad pack.indexversion=%"PRIu32),2644 pack_idx_opts.version);2645return0;2646}2647returngit_default_config(k, v, cb);2648}26492650static voidread_object_list_from_stdin(void)2651{2652char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2653struct object_id oid;2654const char*p;26552656for(;;) {2657if(!fgets(line,sizeof(line), stdin)) {2658if(feof(stdin))2659break;2660if(!ferror(stdin))2661die("BUG: fgets returned NULL, not EOF, not error!");2662if(errno != EINTR)2663die_errno("fgets");2664clearerr(stdin);2665continue;2666}2667if(line[0] =='-') {2668if(get_oid_hex(line+1, &oid))2669die(_("expected edge object ID, got garbage:\n%s"),2670 line);2671add_preferred_base(&oid);2672continue;2673}2674if(parse_oid_hex(line, &oid, &p))2675die(_("expected object ID, got garbage:\n%s"), line);26762677add_preferred_base_object(p +1);2678add_object_entry(&oid, OBJ_NONE, p +1,0);2679}2680}26812682/* Remember to update object flag allocation in object.h */2683#define OBJECT_ADDED (1u<<20)26842685static voidshow_commit(struct commit *commit,void*data)2686{2687add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2688 commit->object.flags |= OBJECT_ADDED;26892690if(write_bitmap_index)2691index_commit_for_bitmap(commit);2692}26932694static voidshow_object(struct object *obj,const char*name,void*data)2695{2696add_preferred_base_object(name);2697add_object_entry(&obj->oid, obj->type, name,0);2698 obj->flags |= OBJECT_ADDED;2699}27002701static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2702{2703assert(arg_missing_action == MA_ALLOW_ANY);27042705/*2706 * Quietly ignore ALL missing objects. This avoids problems with2707 * staging them now and getting an odd error later.2708 */2709if(!has_object_file(&obj->oid))2710return;27112712show_object(obj, name, data);2713}27142715static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2716{2717assert(arg_missing_action == MA_ALLOW_PROMISOR);27182719/*2720 * Quietly ignore EXPECTED missing objects. This avoids problems with2721 * staging them now and getting an odd error later.2722 */2723if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2724return;27252726show_object(obj, name, data);2727}27282729static intoption_parse_missing_action(const struct option *opt,2730const char*arg,int unset)2731{2732assert(arg);2733assert(!unset);27342735if(!strcmp(arg,"error")) {2736 arg_missing_action = MA_ERROR;2737 fn_show_object = show_object;2738return0;2739}27402741if(!strcmp(arg,"allow-any")) {2742 arg_missing_action = MA_ALLOW_ANY;2743 fetch_if_missing =0;2744 fn_show_object = show_object__ma_allow_any;2745return0;2746}27472748if(!strcmp(arg,"allow-promisor")) {2749 arg_missing_action = MA_ALLOW_PROMISOR;2750 fetch_if_missing =0;2751 fn_show_object = show_object__ma_allow_promisor;2752return0;2753}27542755die(_("invalid value for --missing"));2756return0;2757}27582759static voidshow_edge(struct commit *commit)2760{2761add_preferred_base(&commit->object.oid);2762}27632764struct in_pack_object {2765 off_t offset;2766struct object *object;2767};27682769struct in_pack {2770unsigned int alloc;2771unsigned int nr;2772struct in_pack_object *array;2773};27742775static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2776{2777 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2778 in_pack->array[in_pack->nr].object = object;2779 in_pack->nr++;2780}27812782/*2783 * Compare the objects in the offset order, in order to emulate the2784 * "git rev-list --objects" output that produced the pack originally.2785 */2786static intofscmp(const void*a_,const void*b_)2787{2788struct in_pack_object *a = (struct in_pack_object *)a_;2789struct in_pack_object *b = (struct in_pack_object *)b_;27902791if(a->offset < b->offset)2792return-1;2793else if(a->offset > b->offset)2794return1;2795else2796returnoidcmp(&a->object->oid, &b->object->oid);2797}27982799static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2800{2801struct packed_git *p;2802struct in_pack in_pack;2803uint32_t i;28042805memset(&in_pack,0,sizeof(in_pack));28062807for(p =get_packed_git(the_repository); p; p = p->next) {2808struct object_id oid;2809struct object *o;28102811if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2812continue;2813if(open_pack_index(p))2814die(_("cannot open pack index"));28152816ALLOC_GROW(in_pack.array,2817 in_pack.nr + p->num_objects,2818 in_pack.alloc);28192820for(i =0; i < p->num_objects; i++) {2821nth_packed_object_oid(&oid, p, i);2822 o =lookup_unknown_object(oid.hash);2823if(!(o->flags & OBJECT_ADDED))2824mark_in_pack_object(o, p, &in_pack);2825 o->flags |= OBJECT_ADDED;2826}2827}28282829if(in_pack.nr) {2830QSORT(in_pack.array, in_pack.nr, ofscmp);2831for(i =0; i < in_pack.nr; i++) {2832struct object *o = in_pack.array[i].object;2833add_object_entry(&o->oid, o->type,"",0);2834}2835}2836free(in_pack.array);2837}28382839static intadd_loose_object(const struct object_id *oid,const char*path,2840void*data)2841{2842enum object_type type =oid_object_info(the_repository, oid, NULL);28432844if(type <0) {2845warning(_("loose object at%scould not be examined"), path);2846return0;2847}28482849add_object_entry(oid, type,"",0);2850return0;2851}28522853/*2854 * We actually don't even have to worry about reachability here.2855 * add_object_entry will weed out duplicates, so we just add every2856 * loose object we find.2857 */2858static voidadd_unreachable_loose_objects(void)2859{2860for_each_loose_file_in_objdir(get_object_directory(),2861 add_loose_object,2862 NULL, NULL, NULL);2863}28642865static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2866{2867static struct packed_git *last_found = (void*)1;2868struct packed_git *p;28692870 p = (last_found != (void*)1) ? last_found :2871get_packed_git(the_repository);28722873while(p) {2874if((!p->pack_local || p->pack_keep ||2875 p->pack_keep_in_core) &&2876find_pack_entry_one(oid->hash, p)) {2877 last_found = p;2878return1;2879}2880if(p == last_found)2881 p =get_packed_git(the_repository);2882else2883 p = p->next;2884if(p == last_found)2885 p = p->next;2886}2887return0;2888}28892890/*2891 * Store a list of sha1s that are should not be discarded2892 * because they are either written too recently, or are2893 * reachable from another object that was.2894 *2895 * This is filled by get_object_list.2896 */2897static struct oid_array recent_objects;28982899static intloosened_object_can_be_discarded(const struct object_id *oid,2900 timestamp_t mtime)2901{2902if(!unpack_unreachable_expiration)2903return0;2904if(mtime > unpack_unreachable_expiration)2905return0;2906if(oid_array_lookup(&recent_objects, oid) >=0)2907return0;2908return1;2909}29102911static voidloosen_unused_packed_objects(struct rev_info *revs)2912{2913struct packed_git *p;2914uint32_t i;2915struct object_id oid;29162917for(p =get_packed_git(the_repository); p; p = p->next) {2918if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2919continue;29202921if(open_pack_index(p))2922die(_("cannot open pack index"));29232924for(i =0; i < p->num_objects; i++) {2925nth_packed_object_oid(&oid, p, i);2926if(!packlist_find(&to_pack, oid.hash, NULL) &&2927!has_sha1_pack_kept_or_nonlocal(&oid) &&2928!loosened_object_can_be_discarded(&oid, p->mtime))2929if(force_object_loose(&oid, p->mtime))2930die(_("unable to force loose object"));2931}2932}2933}29342935/*2936 * This tracks any options which pack-reuse code expects to be on, or which a2937 * reader of the pack might not understand, and which would therefore prevent2938 * blind reuse of what we have on disk.2939 */2940static intpack_options_allow_reuse(void)2941{2942return pack_to_stdout &&2943 allow_ofs_delta &&2944!ignore_packed_keep_on_disk &&2945!ignore_packed_keep_in_core &&2946(!local || !have_non_local_packs) &&2947!incremental;2948}29492950static intget_object_list_from_bitmap(struct rev_info *revs)2951{2952struct bitmap_index *bitmap_git;2953if(!(bitmap_git =prepare_bitmap_walk(revs)))2954return-1;29552956if(pack_options_allow_reuse() &&2957!reuse_partial_packfile_from_bitmap(2958 bitmap_git,2959&reuse_packfile,2960&reuse_packfile_objects,2961&reuse_packfile_offset)) {2962assert(reuse_packfile_objects);2963 nr_result += reuse_packfile_objects;2964display_progress(progress_state, nr_result);2965}29662967traverse_bitmap_commit_list(bitmap_git, &add_object_entry_from_bitmap);2968free_bitmap_index(bitmap_git);2969return0;2970}29712972static voidrecord_recent_object(struct object *obj,2973const char*name,2974void*data)2975{2976oid_array_append(&recent_objects, &obj->oid);2977}29782979static voidrecord_recent_commit(struct commit *commit,void*data)2980{2981oid_array_append(&recent_objects, &commit->object.oid);2982}29832984static voidget_object_list(int ac,const char**av)2985{2986struct rev_info revs;2987char line[1000];2988int flags =0;29892990init_revisions(&revs, NULL);2991 save_commit_buffer =0;2992 revs.allow_exclude_promisor_objects_opt =1;2993setup_revisions(ac, av, &revs, NULL);29942995/* make sure shallows are read */2996is_repository_shallow(the_repository);29972998while(fgets(line,sizeof(line), stdin) != NULL) {2999int len =strlen(line);3000if(len && line[len -1] =='\n')3001 line[--len] =0;3002if(!len)3003break;3004if(*line =='-') {3005if(!strcmp(line,"--not")) {3006 flags ^= UNINTERESTING;3007 write_bitmap_index =0;3008continue;3009}3010if(starts_with(line,"--shallow ")) {3011struct object_id oid;3012if(get_oid_hex(line +10, &oid))3013die("not an SHA-1 '%s'", line +10);3014register_shallow(the_repository, &oid);3015 use_bitmap_index =0;3016continue;3017}3018die(_("not a rev '%s'"), line);3019}3020if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))3021die(_("bad revision '%s'"), line);3022}30233024if(use_bitmap_index && !get_object_list_from_bitmap(&revs))3025return;30263027if(prepare_revision_walk(&revs))3028die(_("revision walk setup failed"));3029mark_edges_uninteresting(&revs, show_edge);30303031if(!fn_show_object)3032 fn_show_object = show_object;3033traverse_commit_list_filtered(&filter_options, &revs,3034 show_commit, fn_show_object, NULL,3035 NULL);30363037if(unpack_unreachable_expiration) {3038 revs.ignore_missing_links =1;3039if(add_unseen_recent_objects_to_traversal(&revs,3040 unpack_unreachable_expiration))3041die(_("unable to add recent objects"));3042if(prepare_revision_walk(&revs))3043die(_("revision walk setup failed"));3044traverse_commit_list(&revs, record_recent_commit,3045 record_recent_object, NULL);3046}30473048if(keep_unreachable)3049add_objects_in_unpacked_packs(&revs);3050if(pack_loose_unreachable)3051add_unreachable_loose_objects();3052if(unpack_unreachable)3053loosen_unused_packed_objects(&revs);30543055oid_array_clear(&recent_objects);3056}30573058static voidadd_extra_kept_packs(const struct string_list *names)3059{3060struct packed_git *p;30613062if(!names->nr)3063return;30643065for(p =get_packed_git(the_repository); p; p = p->next) {3066const char*name =basename(p->pack_name);3067int i;30683069if(!p->pack_local)3070continue;30713072for(i =0; i < names->nr; i++)3073if(!fspathcmp(name, names->items[i].string))3074break;30753076if(i < names->nr) {3077 p->pack_keep_in_core =1;3078 ignore_packed_keep_in_core =1;3079continue;3080}3081}3082}30833084static intoption_parse_index_version(const struct option *opt,3085const char*arg,int unset)3086{3087char*c;3088const char*val = arg;3089 pack_idx_opts.version =strtoul(val, &c,10);3090if(pack_idx_opts.version >2)3091die(_("unsupported index version%s"), val);3092if(*c ==','&& c[1])3093 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3094if(*c || pack_idx_opts.off32_limit &0x80000000)3095die(_("bad index version '%s'"), val);3096return0;3097}30983099static intoption_parse_unpack_unreachable(const struct option *opt,3100const char*arg,int unset)3101{3102if(unset) {3103 unpack_unreachable =0;3104 unpack_unreachable_expiration =0;3105}3106else{3107 unpack_unreachable =1;3108if(arg)3109 unpack_unreachable_expiration =approxidate(arg);3110}3111return0;3112}31133114intcmd_pack_objects(int argc,const char**argv,const char*prefix)3115{3116int use_internal_rev_list =0;3117int thin =0;3118int shallow =0;3119int all_progress_implied =0;3120struct argv_array rp = ARGV_ARRAY_INIT;3121int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3122int rev_list_index =0;3123struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3124struct option pack_objects_options[] = {3125OPT_SET_INT('q',"quiet", &progress,3126N_("do not show progress meter"),0),3127OPT_SET_INT(0,"progress", &progress,3128N_("show progress meter"),1),3129OPT_SET_INT(0,"all-progress", &progress,3130N_("show progress meter during object writing phase"),2),3131OPT_BOOL(0,"all-progress-implied",3132&all_progress_implied,3133N_("similar to --all-progress when progress meter is shown")),3134{ OPTION_CALLBACK,0,"index-version", NULL,N_("<version>[,<offset>]"),3135N_("write the pack index file in the specified idx format version"),31360, option_parse_index_version },3137OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3138N_("maximum size of each output pack file")),3139OPT_BOOL(0,"local", &local,3140N_("ignore borrowed objects from alternate object store")),3141OPT_BOOL(0,"incremental", &incremental,3142N_("ignore packed objects")),3143OPT_INTEGER(0,"window", &window,3144N_("limit pack window by objects")),3145OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3146N_("limit pack window by memory in addition to object limit")),3147OPT_INTEGER(0,"depth", &depth,3148N_("maximum length of delta chain allowed in the resulting pack")),3149OPT_BOOL(0,"reuse-delta", &reuse_delta,3150N_("reuse existing deltas")),3151OPT_BOOL(0,"reuse-object", &reuse_object,3152N_("reuse existing objects")),3153OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3154N_("use OFS_DELTA objects")),3155OPT_INTEGER(0,"threads", &delta_search_threads,3156N_("use threads when searching for best delta matches")),3157OPT_BOOL(0,"non-empty", &non_empty,3158N_("do not create an empty pack output")),3159OPT_BOOL(0,"revs", &use_internal_rev_list,3160N_("read revision arguments from standard input")),3161OPT_SET_INT_F(0,"unpacked", &rev_list_unpacked,3162N_("limit the objects to those that are not yet packed"),31631, PARSE_OPT_NONEG),3164OPT_SET_INT_F(0,"all", &rev_list_all,3165N_("include objects reachable from any reference"),31661, PARSE_OPT_NONEG),3167OPT_SET_INT_F(0,"reflog", &rev_list_reflog,3168N_("include objects referred by reflog entries"),31691, PARSE_OPT_NONEG),3170OPT_SET_INT_F(0,"indexed-objects", &rev_list_index,3171N_("include objects referred to by the index"),31721, PARSE_OPT_NONEG),3173OPT_BOOL(0,"stdout", &pack_to_stdout,3174N_("output pack to stdout")),3175OPT_BOOL(0,"include-tag", &include_tag,3176N_("include tag objects that refer to objects to be packed")),3177OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3178N_("keep unreachable objects")),3179OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3180N_("pack loose unreachable objects")),3181{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3182N_("unpack unreachable objects newer than <time>"),3183 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3184OPT_BOOL(0,"thin", &thin,3185N_("create thin packs")),3186OPT_BOOL(0,"shallow", &shallow,3187N_("create packs suitable for shallow fetches")),3188OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3189N_("ignore packs that have companion .keep file")),3190OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3191N_("ignore this pack")),3192OPT_INTEGER(0,"compression", &pack_compression_level,3193N_("pack compression level")),3194OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3195N_("do not hide commits by grafts"),0),3196OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3197N_("use a bitmap index if available to speed up counting objects")),3198OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3199N_("write a bitmap index together with the pack index")),3200OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3201{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3202N_("handling for missing objects"), PARSE_OPT_NONEG,3203 option_parse_missing_action },3204OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3205N_("do not pack objects in promisor packfiles")),3206OPT_END(),3207};32083209if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3210BUG("too many dfs states, increase OE_DFS_STATE_BITS");32113212 read_replace_refs =0;32133214reset_pack_idx_option(&pack_idx_opts);3215git_config(git_pack_config, NULL);32163217 progress =isatty(2);3218 argc =parse_options(argc, argv, prefix, pack_objects_options,3219 pack_usage,0);32203221if(argc) {3222 base_name = argv[0];3223 argc--;3224}3225if(pack_to_stdout != !base_name || argc)3226usage_with_options(pack_usage, pack_objects_options);32273228if(depth >= (1<< OE_DEPTH_BITS)) {3229warning(_("delta chain depth%dis too deep, forcing%d"),3230 depth, (1<< OE_DEPTH_BITS) -1);3231 depth = (1<< OE_DEPTH_BITS) -1;3232}3233if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3234warning(_("pack.deltaCacheLimit is too high, forcing%d"),3235(1U<< OE_Z_DELTA_BITS) -1);3236 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3237}32383239argv_array_push(&rp,"pack-objects");3240if(thin) {3241 use_internal_rev_list =1;3242argv_array_push(&rp, shallow3243?"--objects-edge-aggressive"3244:"--objects-edge");3245}else3246argv_array_push(&rp,"--objects");32473248if(rev_list_all) {3249 use_internal_rev_list =1;3250argv_array_push(&rp,"--all");3251}3252if(rev_list_reflog) {3253 use_internal_rev_list =1;3254argv_array_push(&rp,"--reflog");3255}3256if(rev_list_index) {3257 use_internal_rev_list =1;3258argv_array_push(&rp,"--indexed-objects");3259}3260if(rev_list_unpacked) {3261 use_internal_rev_list =1;3262argv_array_push(&rp,"--unpacked");3263}32643265if(exclude_promisor_objects) {3266 use_internal_rev_list =1;3267 fetch_if_missing =0;3268argv_array_push(&rp,"--exclude-promisor-objects");3269}3270if(unpack_unreachable || keep_unreachable || pack_loose_unreachable)3271 use_internal_rev_list =1;32723273if(!reuse_object)3274 reuse_delta =0;3275if(pack_compression_level == -1)3276 pack_compression_level = Z_DEFAULT_COMPRESSION;3277else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3278die(_("bad pack compression level%d"), pack_compression_level);32793280if(!delta_search_threads)/* --threads=0 means autodetect */3281 delta_search_threads =online_cpus();32823283#ifdef NO_PTHREADS3284if(delta_search_threads !=1)3285warning(_("no threads support, ignoring --threads"));3286#endif3287if(!pack_to_stdout && !pack_size_limit)3288 pack_size_limit = pack_size_limit_cfg;3289if(pack_to_stdout && pack_size_limit)3290die(_("--max-pack-size cannot be used to build a pack for transfer"));3291if(pack_size_limit && pack_size_limit <1024*1024) {3292warning(_("minimum pack size limit is 1 MiB"));3293 pack_size_limit =1024*1024;3294}32953296if(!pack_to_stdout && thin)3297die(_("--thin cannot be used to build an indexable pack"));32983299if(keep_unreachable && unpack_unreachable)3300die(_("--keep-unreachable and --unpack-unreachable are incompatible"));3301if(!rev_list_all || !rev_list_reflog || !rev_list_index)3302 unpack_unreachable_expiration =0;33033304if(filter_options.choice) {3305if(!pack_to_stdout)3306die(_("cannot use --filter without --stdout"));3307 use_bitmap_index =0;3308}33093310/*3311 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3312 *3313 * - to produce good pack (with bitmap index not-yet-packed objects are3314 * packed in suboptimal order).3315 *3316 * - to use more robust pack-generation codepath (avoiding possible3317 * bugs in bitmap code and possible bitmap index corruption).3318 */3319if(!pack_to_stdout)3320 use_bitmap_index_default =0;33213322if(use_bitmap_index <0)3323 use_bitmap_index = use_bitmap_index_default;33243325/* "hard" reasons not to use bitmaps; these just won't work at all */3326if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow(the_repository))3327 use_bitmap_index =0;33283329if(pack_to_stdout || !rev_list_all)3330 write_bitmap_index =0;33313332if(progress && all_progress_implied)3333 progress =2;33343335add_extra_kept_packs(&keep_pack_list);3336if(ignore_packed_keep_on_disk) {3337struct packed_git *p;3338for(p =get_packed_git(the_repository); p; p = p->next)3339if(p->pack_local && p->pack_keep)3340break;3341if(!p)/* no keep-able packs found */3342 ignore_packed_keep_on_disk =0;3343}3344if(local) {3345/*3346 * unlike ignore_packed_keep_on_disk above, we do not3347 * want to unset "local" based on looking at packs, as3348 * it also covers non-local objects3349 */3350struct packed_git *p;3351for(p =get_packed_git(the_repository); p; p = p->next) {3352if(!p->pack_local) {3353 have_non_local_packs =1;3354break;3355}3356}3357}33583359prepare_packing_data(&to_pack);33603361if(progress)3362 progress_state =start_progress(_("Enumerating objects"),0);3363if(!use_internal_rev_list)3364read_object_list_from_stdin();3365else{3366get_object_list(rp.argc, rp.argv);3367argv_array_clear(&rp);3368}3369cleanup_preferred_base();3370if(include_tag && nr_result)3371for_each_ref(add_ref_tag, NULL);3372stop_progress(&progress_state);33733374if(non_empty && !nr_result)3375return0;3376if(nr_result)3377prepare_pack(window, depth);3378write_pack_file();3379if(progress)3380fprintf_ln(stderr,3381_("Total %"PRIu32" (delta %"PRIu32"),"3382" reused %"PRIu32" (delta %"PRIu32")"),3383 written, written_delta, reused, reused_delta);3384return0;3385}