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 34#define IN_PACK(obj) oe_in_pack(&to_pack, obj) 35#define SIZE(obj) oe_size(&to_pack, obj) 36#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size) 37#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj) 38#define DELTA(obj) oe_delta(&to_pack, obj) 39#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj) 40#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj) 41#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val) 42#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val) 43#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val) 44#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val) 45 46static const char*pack_usage[] = { 47N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 48N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 49 NULL 50}; 51 52/* 53 * Objects we are going to pack are collected in the `to_pack` structure. 54 * It contains an array (dynamically expanded) of the object data, and a map 55 * that can resolve SHA1s to their position in the array. 56 */ 57static struct packing_data to_pack; 58 59static struct pack_idx_entry **written_list; 60static uint32_t nr_result, nr_written; 61 62static int non_empty; 63static int reuse_delta =1, reuse_object =1; 64static int keep_unreachable, unpack_unreachable, include_tag; 65static timestamp_t unpack_unreachable_expiration; 66static int pack_loose_unreachable; 67static int local; 68static int have_non_local_packs; 69static int incremental; 70static int ignore_packed_keep; 71static int allow_ofs_delta; 72static struct pack_idx_option pack_idx_opts; 73static const char*base_name; 74static int progress =1; 75static int window =10; 76static unsigned long pack_size_limit; 77static int depth =50; 78static int delta_search_threads; 79static int pack_to_stdout; 80static int num_preferred_base; 81static struct progress *progress_state; 82 83static struct packed_git *reuse_packfile; 84static uint32_t reuse_packfile_objects; 85static off_t reuse_packfile_offset; 86 87static int use_bitmap_index_default =1; 88static int use_bitmap_index = -1; 89static int write_bitmap_index; 90static uint16_t write_bitmap_options; 91 92static int exclude_promisor_objects; 93 94static unsigned long delta_cache_size =0; 95static unsigned long max_delta_cache_size =256*1024*1024; 96static unsigned long cache_max_small_delta_size =1000; 97 98static unsigned long window_memory_limit =0; 99 100static struct list_objects_filter_options filter_options; 101 102enum missing_action { 103 MA_ERROR =0,/* fail if any missing objects are encountered */ 104 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 105 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 106}; 107static enum missing_action arg_missing_action; 108static show_object_fn fn_show_object; 109 110/* 111 * stats 112 */ 113static uint32_t written, written_delta; 114static uint32_t reused, reused_delta; 115 116/* 117 * Indexed commits 118 */ 119static struct commit **indexed_commits; 120static unsigned int indexed_commits_nr; 121static unsigned int indexed_commits_alloc; 122 123static voidindex_commit_for_bitmap(struct commit *commit) 124{ 125if(indexed_commits_nr >= indexed_commits_alloc) { 126 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 127REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 128} 129 130 indexed_commits[indexed_commits_nr++] = commit; 131} 132 133static void*get_delta(struct object_entry *entry) 134{ 135unsigned long size, base_size, delta_size; 136void*buf, *base_buf, *delta_buf; 137enum object_type type; 138 139 buf =read_object_file(&entry->idx.oid, &type, &size); 140if(!buf) 141die("unable to read%s",oid_to_hex(&entry->idx.oid)); 142 base_buf =read_object_file(&DELTA(entry)->idx.oid, &type, 143&base_size); 144if(!base_buf) 145die("unable to read%s", 146oid_to_hex(&DELTA(entry)->idx.oid)); 147 delta_buf =diff_delta(base_buf, base_size, 148 buf, size, &delta_size,0); 149if(!delta_buf || delta_size !=DELTA_SIZE(entry)) 150die("delta size changed"); 151free(buf); 152free(base_buf); 153return delta_buf; 154} 155 156static unsigned longdo_compress(void**pptr,unsigned long size) 157{ 158 git_zstream stream; 159void*in, *out; 160unsigned long maxsize; 161 162git_deflate_init(&stream, pack_compression_level); 163 maxsize =git_deflate_bound(&stream, size); 164 165 in = *pptr; 166 out =xmalloc(maxsize); 167*pptr = out; 168 169 stream.next_in = in; 170 stream.avail_in = size; 171 stream.next_out = out; 172 stream.avail_out = maxsize; 173while(git_deflate(&stream, Z_FINISH) == Z_OK) 174;/* nothing */ 175git_deflate_end(&stream); 176 177free(in); 178return stream.total_out; 179} 180 181static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 182const struct object_id *oid) 183{ 184 git_zstream stream; 185unsigned char ibuf[1024*16]; 186unsigned char obuf[1024*16]; 187unsigned long olen =0; 188 189git_deflate_init(&stream, pack_compression_level); 190 191for(;;) { 192 ssize_t readlen; 193int zret = Z_OK; 194 readlen =read_istream(st, ibuf,sizeof(ibuf)); 195if(readlen == -1) 196die(_("unable to read%s"),oid_to_hex(oid)); 197 198 stream.next_in = ibuf; 199 stream.avail_in = readlen; 200while((stream.avail_in || readlen ==0) && 201(zret == Z_OK || zret == Z_BUF_ERROR)) { 202 stream.next_out = obuf; 203 stream.avail_out =sizeof(obuf); 204 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 205hashwrite(f, obuf, stream.next_out - obuf); 206 olen += stream.next_out - obuf; 207} 208if(stream.avail_in) 209die(_("deflate error (%d)"), zret); 210if(readlen ==0) { 211if(zret != Z_STREAM_END) 212die(_("deflate error (%d)"), zret); 213break; 214} 215} 216git_deflate_end(&stream); 217return olen; 218} 219 220/* 221 * we are going to reuse the existing object data as is. make 222 * sure it is not corrupt. 223 */ 224static intcheck_pack_inflate(struct packed_git *p, 225struct pack_window **w_curs, 226 off_t offset, 227 off_t len, 228unsigned long expect) 229{ 230 git_zstream stream; 231unsigned char fakebuf[4096], *in; 232int st; 233 234memset(&stream,0,sizeof(stream)); 235git_inflate_init(&stream); 236do{ 237 in =use_pack(p, w_curs, offset, &stream.avail_in); 238 stream.next_in = in; 239 stream.next_out = fakebuf; 240 stream.avail_out =sizeof(fakebuf); 241 st =git_inflate(&stream, Z_FINISH); 242 offset += stream.next_in - in; 243}while(st == Z_OK || st == Z_BUF_ERROR); 244git_inflate_end(&stream); 245return(st == Z_STREAM_END && 246 stream.total_out == expect && 247 stream.total_in == len) ?0: -1; 248} 249 250static voidcopy_pack_data(struct hashfile *f, 251struct packed_git *p, 252struct pack_window **w_curs, 253 off_t offset, 254 off_t len) 255{ 256unsigned char*in; 257unsigned long avail; 258 259while(len) { 260 in =use_pack(p, w_curs, offset, &avail); 261if(avail > len) 262 avail = (unsigned long)len; 263hashwrite(f, in, avail); 264 offset += avail; 265 len -= avail; 266} 267} 268 269/* Return 0 if we will bust the pack-size limit */ 270static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 271unsigned long limit,int usable_delta) 272{ 273unsigned long size, datalen; 274unsigned char header[MAX_PACK_OBJECT_HEADER], 275 dheader[MAX_PACK_OBJECT_HEADER]; 276unsigned hdrlen; 277enum object_type type; 278void*buf; 279struct git_istream *st = NULL; 280 281if(!usable_delta) { 282if(oe_type(entry) == OBJ_BLOB && 283oe_size_greater_than(&to_pack, entry, big_file_threshold) && 284(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 285 buf = NULL; 286else{ 287 buf =read_object_file(&entry->idx.oid, &type, &size); 288if(!buf) 289die(_("unable to read%s"), 290oid_to_hex(&entry->idx.oid)); 291} 292/* 293 * make sure no cached delta data remains from a 294 * previous attempt before a pack split occurred. 295 */ 296FREE_AND_NULL(entry->delta_data); 297 entry->z_delta_size =0; 298}else if(entry->delta_data) { 299 size =DELTA_SIZE(entry); 300 buf = entry->delta_data; 301 entry->delta_data = NULL; 302 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 303 OBJ_OFS_DELTA : OBJ_REF_DELTA; 304}else{ 305 buf =get_delta(entry); 306 size =DELTA_SIZE(entry); 307 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 308 OBJ_OFS_DELTA : OBJ_REF_DELTA; 309} 310 311if(st)/* large blob case, just assume we don't compress well */ 312 datalen = size; 313else if(entry->z_delta_size) 314 datalen = entry->z_delta_size; 315else 316 datalen =do_compress(&buf, size); 317 318/* 319 * The object header is a byte of 'type' followed by zero or 320 * more bytes of length. 321 */ 322 hdrlen =encode_in_pack_object_header(header,sizeof(header), 323 type, size); 324 325if(type == OBJ_OFS_DELTA) { 326/* 327 * Deltas with relative base contain an additional 328 * encoding of the relative offset for the delta 329 * base from this object's position in the pack. 330 */ 331 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 332unsigned pos =sizeof(dheader) -1; 333 dheader[pos] = ofs &127; 334while(ofs >>=7) 335 dheader[--pos] =128| (--ofs &127); 336if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 337if(st) 338close_istream(st); 339free(buf); 340return0; 341} 342hashwrite(f, header, hdrlen); 343hashwrite(f, dheader + pos,sizeof(dheader) - pos); 344 hdrlen +=sizeof(dheader) - pos; 345}else if(type == OBJ_REF_DELTA) { 346/* 347 * Deltas with a base reference contain 348 * an additional 20 bytes for the base sha1. 349 */ 350if(limit && hdrlen +20+ datalen +20>= limit) { 351if(st) 352close_istream(st); 353free(buf); 354return0; 355} 356hashwrite(f, header, hdrlen); 357hashwrite(f,DELTA(entry)->idx.oid.hash,20); 358 hdrlen +=20; 359}else{ 360if(limit && hdrlen + datalen +20>= limit) { 361if(st) 362close_istream(st); 363free(buf); 364return0; 365} 366hashwrite(f, header, hdrlen); 367} 368if(st) { 369 datalen =write_large_blob_data(st, f, &entry->idx.oid); 370close_istream(st); 371}else{ 372hashwrite(f, buf, datalen); 373free(buf); 374} 375 376return hdrlen + datalen; 377} 378 379/* Return 0 if we will bust the pack-size limit */ 380static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 381unsigned long limit,int usable_delta) 382{ 383struct packed_git *p =IN_PACK(entry); 384struct pack_window *w_curs = NULL; 385struct revindex_entry *revidx; 386 off_t offset; 387enum object_type type =oe_type(entry); 388 off_t datalen; 389unsigned char header[MAX_PACK_OBJECT_HEADER], 390 dheader[MAX_PACK_OBJECT_HEADER]; 391unsigned hdrlen; 392unsigned long entry_size =SIZE(entry); 393 394if(DELTA(entry)) 395 type = (allow_ofs_delta &&DELTA(entry)->idx.offset) ? 396 OBJ_OFS_DELTA : OBJ_REF_DELTA; 397 hdrlen =encode_in_pack_object_header(header,sizeof(header), 398 type, entry_size); 399 400 offset = entry->in_pack_offset; 401 revidx =find_pack_revindex(p, offset); 402 datalen = revidx[1].offset - offset; 403if(!pack_to_stdout && p->index_version >1&& 404check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 405error("bad packed object CRC for%s", 406oid_to_hex(&entry->idx.oid)); 407unuse_pack(&w_curs); 408returnwrite_no_reuse_object(f, entry, limit, usable_delta); 409} 410 411 offset += entry->in_pack_header_size; 412 datalen -= entry->in_pack_header_size; 413 414if(!pack_to_stdout && p->index_version ==1&& 415check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 416error("corrupt packed object for%s", 417oid_to_hex(&entry->idx.oid)); 418unuse_pack(&w_curs); 419returnwrite_no_reuse_object(f, entry, limit, usable_delta); 420} 421 422if(type == OBJ_OFS_DELTA) { 423 off_t ofs = entry->idx.offset -DELTA(entry)->idx.offset; 424unsigned pos =sizeof(dheader) -1; 425 dheader[pos] = ofs &127; 426while(ofs >>=7) 427 dheader[--pos] =128| (--ofs &127); 428if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 429unuse_pack(&w_curs); 430return0; 431} 432hashwrite(f, header, hdrlen); 433hashwrite(f, dheader + pos,sizeof(dheader) - pos); 434 hdrlen +=sizeof(dheader) - pos; 435 reused_delta++; 436}else if(type == OBJ_REF_DELTA) { 437if(limit && hdrlen +20+ datalen +20>= limit) { 438unuse_pack(&w_curs); 439return0; 440} 441hashwrite(f, header, hdrlen); 442hashwrite(f,DELTA(entry)->idx.oid.hash,20); 443 hdrlen +=20; 444 reused_delta++; 445}else{ 446if(limit && hdrlen + datalen +20>= limit) { 447unuse_pack(&w_curs); 448return0; 449} 450hashwrite(f, header, hdrlen); 451} 452copy_pack_data(f, p, &w_curs, offset, datalen); 453unuse_pack(&w_curs); 454 reused++; 455return hdrlen + datalen; 456} 457 458/* Return 0 if we will bust the pack-size limit */ 459static off_t write_object(struct hashfile *f, 460struct object_entry *entry, 461 off_t write_offset) 462{ 463unsigned long limit; 464 off_t len; 465int usable_delta, to_reuse; 466 467if(!pack_to_stdout) 468crc32_begin(f); 469 470/* apply size limit if limited packsize and not first object */ 471if(!pack_size_limit || !nr_written) 472 limit =0; 473else if(pack_size_limit <= write_offset) 474/* 475 * the earlier object did not fit the limit; avoid 476 * mistaking this with unlimited (i.e. limit = 0). 477 */ 478 limit =1; 479else 480 limit = pack_size_limit - write_offset; 481 482if(!DELTA(entry)) 483 usable_delta =0;/* no delta */ 484else if(!pack_size_limit) 485 usable_delta =1;/* unlimited packfile */ 486else if(DELTA(entry)->idx.offset == (off_t)-1) 487 usable_delta =0;/* base was written to another pack */ 488else if(DELTA(entry)->idx.offset) 489 usable_delta =1;/* base already exists in this pack */ 490else 491 usable_delta =0;/* base could end up in another pack */ 492 493if(!reuse_object) 494 to_reuse =0;/* explicit */ 495else if(!IN_PACK(entry)) 496 to_reuse =0;/* can't reuse what we don't have */ 497else if(oe_type(entry) == OBJ_REF_DELTA || 498oe_type(entry) == OBJ_OFS_DELTA) 499/* check_object() decided it for us ... */ 500 to_reuse = usable_delta; 501/* ... but pack split may override that */ 502else if(oe_type(entry) != entry->in_pack_type) 503 to_reuse =0;/* pack has delta which is unusable */ 504else if(DELTA(entry)) 505 to_reuse =0;/* we want to pack afresh */ 506else 507 to_reuse =1;/* we have it in-pack undeltified, 508 * and we do not need to deltify it. 509 */ 510 511if(!to_reuse) 512 len =write_no_reuse_object(f, entry, limit, usable_delta); 513else 514 len =write_reuse_object(f, entry, limit, usable_delta); 515if(!len) 516return0; 517 518if(usable_delta) 519 written_delta++; 520 written++; 521if(!pack_to_stdout) 522 entry->idx.crc32 =crc32_end(f); 523return len; 524} 525 526enum write_one_status { 527 WRITE_ONE_SKIP = -1,/* already written */ 528 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 529 WRITE_ONE_WRITTEN =1,/* normal */ 530 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 531}; 532 533static enum write_one_status write_one(struct hashfile *f, 534struct object_entry *e, 535 off_t *offset) 536{ 537 off_t size; 538int recursing; 539 540/* 541 * we set offset to 1 (which is an impossible value) to mark 542 * the fact that this object is involved in "write its base 543 * first before writing a deltified object" recursion. 544 */ 545 recursing = (e->idx.offset ==1); 546if(recursing) { 547warning("recursive delta detected for object%s", 548oid_to_hex(&e->idx.oid)); 549return WRITE_ONE_RECURSIVE; 550}else if(e->idx.offset || e->preferred_base) { 551/* offset is non zero if object is written already. */ 552return WRITE_ONE_SKIP; 553} 554 555/* if we are deltified, write out base object first. */ 556if(DELTA(e)) { 557 e->idx.offset =1;/* now recurse */ 558switch(write_one(f,DELTA(e), offset)) { 559case WRITE_ONE_RECURSIVE: 560/* we cannot depend on this one */ 561SET_DELTA(e, NULL); 562break; 563default: 564break; 565case WRITE_ONE_BREAK: 566 e->idx.offset = recursing; 567return WRITE_ONE_BREAK; 568} 569} 570 571 e->idx.offset = *offset; 572 size =write_object(f, e, *offset); 573if(!size) { 574 e->idx.offset = recursing; 575return WRITE_ONE_BREAK; 576} 577 written_list[nr_written++] = &e->idx; 578 579/* make sure off_t is sufficiently large not to wrap */ 580if(signed_add_overflows(*offset, size)) 581die("pack too large for current definition of off_t"); 582*offset += size; 583return WRITE_ONE_WRITTEN; 584} 585 586static intmark_tagged(const char*path,const struct object_id *oid,int flag, 587void*cb_data) 588{ 589struct object_id peeled; 590struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 591 592if(entry) 593 entry->tagged =1; 594if(!peel_ref(path, &peeled)) { 595 entry =packlist_find(&to_pack, peeled.hash, NULL); 596if(entry) 597 entry->tagged =1; 598} 599return0; 600} 601 602staticinlinevoidadd_to_write_order(struct object_entry **wo, 603unsigned int*endp, 604struct object_entry *e) 605{ 606if(e->filled) 607return; 608 wo[(*endp)++] = e; 609 e->filled =1; 610} 611 612static voidadd_descendants_to_write_order(struct object_entry **wo, 613unsigned int*endp, 614struct object_entry *e) 615{ 616int add_to_order =1; 617while(e) { 618if(add_to_order) { 619struct object_entry *s; 620/* add this node... */ 621add_to_write_order(wo, endp, e); 622/* all its siblings... */ 623for(s =DELTA_SIBLING(e); s; s =DELTA_SIBLING(s)) { 624add_to_write_order(wo, endp, s); 625} 626} 627/* drop down a level to add left subtree nodes if possible */ 628if(DELTA_CHILD(e)) { 629 add_to_order =1; 630 e =DELTA_CHILD(e); 631}else{ 632 add_to_order =0; 633/* our sibling might have some children, it is next */ 634if(DELTA_SIBLING(e)) { 635 e =DELTA_SIBLING(e); 636continue; 637} 638/* go back to our parent node */ 639 e =DELTA(e); 640while(e && !DELTA_SIBLING(e)) { 641/* we're on the right side of a subtree, keep 642 * going up until we can go right again */ 643 e =DELTA(e); 644} 645if(!e) { 646/* done- we hit our original root node */ 647return; 648} 649/* pass it off to sibling at this level */ 650 e =DELTA_SIBLING(e); 651} 652}; 653} 654 655static voidadd_family_to_write_order(struct object_entry **wo, 656unsigned int*endp, 657struct object_entry *e) 658{ 659struct object_entry *root; 660 661for(root = e;DELTA(root); root =DELTA(root)) 662;/* nothing */ 663add_descendants_to_write_order(wo, endp, root); 664} 665 666static struct object_entry **compute_write_order(void) 667{ 668unsigned int i, wo_end, last_untagged; 669 670struct object_entry **wo; 671struct object_entry *objects = to_pack.objects; 672 673for(i =0; i < to_pack.nr_objects; i++) { 674 objects[i].tagged =0; 675 objects[i].filled =0; 676SET_DELTA_CHILD(&objects[i], NULL); 677SET_DELTA_SIBLING(&objects[i], NULL); 678} 679 680/* 681 * Fully connect delta_child/delta_sibling network. 682 * Make sure delta_sibling is sorted in the original 683 * recency order. 684 */ 685for(i = to_pack.nr_objects; i >0;) { 686struct object_entry *e = &objects[--i]; 687if(!DELTA(e)) 688continue; 689/* Mark me as the first child */ 690 e->delta_sibling_idx =DELTA(e)->delta_child_idx; 691SET_DELTA_CHILD(DELTA(e), e); 692} 693 694/* 695 * Mark objects that are at the tip of tags. 696 */ 697for_each_tag_ref(mark_tagged, NULL); 698 699/* 700 * Give the objects in the original recency order until 701 * we see a tagged tip. 702 */ 703ALLOC_ARRAY(wo, to_pack.nr_objects); 704for(i = wo_end =0; i < to_pack.nr_objects; i++) { 705if(objects[i].tagged) 706break; 707add_to_write_order(wo, &wo_end, &objects[i]); 708} 709 last_untagged = i; 710 711/* 712 * Then fill all the tagged tips. 713 */ 714for(; i < to_pack.nr_objects; i++) { 715if(objects[i].tagged) 716add_to_write_order(wo, &wo_end, &objects[i]); 717} 718 719/* 720 * And then all remaining commits and tags. 721 */ 722for(i = last_untagged; i < to_pack.nr_objects; i++) { 723if(oe_type(&objects[i]) != OBJ_COMMIT && 724oe_type(&objects[i]) != OBJ_TAG) 725continue; 726add_to_write_order(wo, &wo_end, &objects[i]); 727} 728 729/* 730 * And then all the trees. 731 */ 732for(i = last_untagged; i < to_pack.nr_objects; i++) { 733if(oe_type(&objects[i]) != OBJ_TREE) 734continue; 735add_to_write_order(wo, &wo_end, &objects[i]); 736} 737 738/* 739 * Finally all the rest in really tight order 740 */ 741for(i = last_untagged; i < to_pack.nr_objects; i++) { 742if(!objects[i].filled) 743add_family_to_write_order(wo, &wo_end, &objects[i]); 744} 745 746if(wo_end != to_pack.nr_objects) 747die("ordered%uobjects, expected %"PRIu32, wo_end, to_pack.nr_objects); 748 749return wo; 750} 751 752static off_t write_reused_pack(struct hashfile *f) 753{ 754unsigned char buffer[8192]; 755 off_t to_write, total; 756int fd; 757 758if(!is_pack_valid(reuse_packfile)) 759die("packfile is invalid:%s", reuse_packfile->pack_name); 760 761 fd =git_open(reuse_packfile->pack_name); 762if(fd <0) 763die_errno("unable to open packfile for reuse:%s", 764 reuse_packfile->pack_name); 765 766if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 767die_errno("unable to seek in reused packfile"); 768 769if(reuse_packfile_offset <0) 770 reuse_packfile_offset = reuse_packfile->pack_size -20; 771 772 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 773 774while(to_write) { 775int read_pack =xread(fd, buffer,sizeof(buffer)); 776 777if(read_pack <=0) 778die_errno("unable to read from reused packfile"); 779 780if(read_pack > to_write) 781 read_pack = to_write; 782 783hashwrite(f, buffer, read_pack); 784 to_write -= read_pack; 785 786/* 787 * We don't know the actual number of objects written, 788 * only how many bytes written, how many bytes total, and 789 * how many objects total. So we can fake it by pretending all 790 * objects we are writing are the same size. This gives us a 791 * smooth progress meter, and at the end it matches the true 792 * answer. 793 */ 794 written = reuse_packfile_objects * 795(((double)(total - to_write)) / total); 796display_progress(progress_state, written); 797} 798 799close(fd); 800 written = reuse_packfile_objects; 801display_progress(progress_state, written); 802return reuse_packfile_offset -sizeof(struct pack_header); 803} 804 805static const char no_split_warning[] =N_( 806"disabling bitmap writing, packs are split due to pack.packSizeLimit" 807); 808 809static voidwrite_pack_file(void) 810{ 811uint32_t i =0, j; 812struct hashfile *f; 813 off_t offset; 814uint32_t nr_remaining = nr_result; 815time_t last_mtime =0; 816struct object_entry **write_order; 817 818if(progress > pack_to_stdout) 819 progress_state =start_progress(_("Writing objects"), nr_result); 820ALLOC_ARRAY(written_list, to_pack.nr_objects); 821 write_order =compute_write_order(); 822 823do{ 824struct object_id oid; 825char*pack_tmp_name = NULL; 826 827if(pack_to_stdout) 828 f =hashfd_throughput(1,"<stdout>", progress_state); 829else 830 f =create_tmp_packfile(&pack_tmp_name); 831 832 offset =write_pack_header(f, nr_remaining); 833 834if(reuse_packfile) { 835 off_t packfile_size; 836assert(pack_to_stdout); 837 838 packfile_size =write_reused_pack(f); 839 offset += packfile_size; 840} 841 842 nr_written =0; 843for(; i < to_pack.nr_objects; i++) { 844struct object_entry *e = write_order[i]; 845if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 846break; 847display_progress(progress_state, written); 848} 849 850/* 851 * Did we write the wrong # entries in the header? 852 * If so, rewrite it like in fast-import 853 */ 854if(pack_to_stdout) { 855hashclose(f, oid.hash, CSUM_CLOSE); 856}else if(nr_written == nr_remaining) { 857hashclose(f, oid.hash, CSUM_FSYNC); 858}else{ 859int fd =hashclose(f, oid.hash,0); 860fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 861 nr_written, oid.hash, offset); 862close(fd); 863if(write_bitmap_index) { 864warning(_(no_split_warning)); 865 write_bitmap_index =0; 866} 867} 868 869if(!pack_to_stdout) { 870struct stat st; 871struct strbuf tmpname = STRBUF_INIT; 872 873/* 874 * Packs are runtime accessed in their mtime 875 * order since newer packs are more likely to contain 876 * younger objects. So if we are creating multiple 877 * packs then we should modify the mtime of later ones 878 * to preserve this property. 879 */ 880if(stat(pack_tmp_name, &st) <0) { 881warning_errno("failed to stat%s", pack_tmp_name); 882}else if(!last_mtime) { 883 last_mtime = st.st_mtime; 884}else{ 885struct utimbuf utb; 886 utb.actime = st.st_atime; 887 utb.modtime = --last_mtime; 888if(utime(pack_tmp_name, &utb) <0) 889warning_errno("failed utime() on%s", pack_tmp_name); 890} 891 892strbuf_addf(&tmpname,"%s-", base_name); 893 894if(write_bitmap_index) { 895bitmap_writer_set_checksum(oid.hash); 896bitmap_writer_build_type_index( 897&to_pack, written_list, nr_written); 898} 899 900finish_tmp_packfile(&tmpname, pack_tmp_name, 901 written_list, nr_written, 902&pack_idx_opts, oid.hash); 903 904if(write_bitmap_index) { 905strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 906 907stop_progress(&progress_state); 908 909bitmap_writer_show_progress(progress); 910bitmap_writer_reuse_bitmaps(&to_pack); 911bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 912bitmap_writer_build(&to_pack); 913bitmap_writer_finish(written_list, nr_written, 914 tmpname.buf, write_bitmap_options); 915 write_bitmap_index =0; 916} 917 918strbuf_release(&tmpname); 919free(pack_tmp_name); 920puts(oid_to_hex(&oid)); 921} 922 923/* mark written objects as written to previous pack */ 924for(j =0; j < nr_written; j++) { 925 written_list[j]->offset = (off_t)-1; 926} 927 nr_remaining -= nr_written; 928}while(nr_remaining && i < to_pack.nr_objects); 929 930free(written_list); 931free(write_order); 932stop_progress(&progress_state); 933if(written != nr_result) 934die("wrote %"PRIu32" objects while expecting %"PRIu32, 935 written, nr_result); 936} 937 938static intno_try_delta(const char*path) 939{ 940static struct attr_check *check; 941 942if(!check) 943 check =attr_check_initl("delta", NULL); 944if(git_check_attr(path, check)) 945return0; 946if(ATTR_FALSE(check->items[0].value)) 947return1; 948return0; 949} 950 951/* 952 * When adding an object, check whether we have already added it 953 * to our packing list. If so, we can skip. However, if we are 954 * being asked to excludei t, but the previous mention was to include 955 * it, make sure to adjust its flags and tweak our numbers accordingly. 956 * 957 * As an optimization, we pass out the index position where we would have 958 * found the item, since that saves us from having to look it up again a 959 * few lines later when we want to add the new entry. 960 */ 961static inthave_duplicate_entry(const struct object_id *oid, 962int exclude, 963uint32_t*index_pos) 964{ 965struct object_entry *entry; 966 967 entry =packlist_find(&to_pack, oid->hash, index_pos); 968if(!entry) 969return0; 970 971if(exclude) { 972if(!entry->preferred_base) 973 nr_result--; 974 entry->preferred_base =1; 975} 976 977return1; 978} 979 980static intwant_found_object(int exclude,struct packed_git *p) 981{ 982if(exclude) 983return1; 984if(incremental) 985return0; 986 987/* 988 * When asked to do --local (do not include an object that appears in a 989 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 990 * an object that appears in a pack marked with .keep), finding a pack 991 * that matches the criteria is sufficient for us to decide to omit it. 992 * However, even if this pack does not satisfy the criteria, we need to 993 * make sure no copy of this object appears in _any_ pack that makes us 994 * to omit the object, so we need to check all the packs. 995 * 996 * We can however first check whether these options can possible matter; 997 * if they do not matter we know we want the object in generated pack. 998 * Otherwise, we signal "-1" at the end to tell the caller that we do 999 * not know either way, and it needs to check more packs.1000 */1001if(!ignore_packed_keep &&1002(!local || !have_non_local_packs))1003return1;10041005if(local && !p->pack_local)1006return0;1007if(ignore_packed_keep && p->pack_local && p->pack_keep)1008return0;10091010/* we don't know yet; keep looking for more packs */1011return-1;1012}10131014/*1015 * Check whether we want the object in the pack (e.g., we do not want1016 * objects found in non-local stores if the "--local" option was used).1017 *1018 * If the caller already knows an existing pack it wants to take the object1019 * from, that is passed in *found_pack and *found_offset; otherwise this1020 * function finds if there is any pack that has the object and returns the pack1021 * and its offset in these variables.1022 */1023static intwant_object_in_pack(const struct object_id *oid,1024int exclude,1025struct packed_git **found_pack,1026 off_t *found_offset)1027{1028int want;1029struct list_head *pos;10301031if(!exclude && local &&has_loose_object_nonlocal(oid->hash))1032return0;10331034/*1035 * If we already know the pack object lives in, start checks from that1036 * pack - in the usual case when neither --local was given nor .keep files1037 * are present we will determine the answer right now.1038 */1039if(*found_pack) {1040 want =want_found_object(exclude, *found_pack);1041if(want != -1)1042return want;1043}1044list_for_each(pos,get_packed_git_mru(the_repository)) {1045struct packed_git *p =list_entry(pos,struct packed_git, mru);1046 off_t offset;10471048if(p == *found_pack)1049 offset = *found_offset;1050else1051 offset =find_pack_entry_one(oid->hash, p);10521053if(offset) {1054if(!*found_pack) {1055if(!is_pack_valid(p))1056continue;1057*found_offset = offset;1058*found_pack = p;1059}1060 want =want_found_object(exclude, p);1061if(!exclude && want >0)1062list_move(&p->mru,1063get_packed_git_mru(the_repository));1064if(want != -1)1065return want;1066}1067}10681069return1;1070}10711072static voidcreate_object_entry(const struct object_id *oid,1073enum object_type type,1074uint32_t hash,1075int exclude,1076int no_try_delta,1077uint32_t index_pos,1078struct packed_git *found_pack,1079 off_t found_offset)1080{1081struct object_entry *entry;10821083 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1084 entry->hash = hash;1085oe_set_type(entry, type);1086if(exclude)1087 entry->preferred_base =1;1088else1089 nr_result++;1090if(found_pack) {1091oe_set_in_pack(&to_pack, entry, found_pack);1092 entry->in_pack_offset = found_offset;1093}10941095 entry->no_try_delta = no_try_delta;1096}10971098static const char no_closure_warning[] =N_(1099"disabling bitmap writing, as some objects are not being packed"1100);11011102static intadd_object_entry(const struct object_id *oid,enum object_type type,1103const char*name,int exclude)1104{1105struct packed_git *found_pack = NULL;1106 off_t found_offset =0;1107uint32_t index_pos;11081109if(have_duplicate_entry(oid, exclude, &index_pos))1110return0;11111112if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1113/* The pack is missing an object, so it will not have closure */1114if(write_bitmap_index) {1115warning(_(no_closure_warning));1116 write_bitmap_index =0;1117}1118return0;1119}11201121create_object_entry(oid, type,pack_name_hash(name),1122 exclude, name &&no_try_delta(name),1123 index_pos, found_pack, found_offset);11241125display_progress(progress_state, nr_result);1126return1;1127}11281129static intadd_object_entry_from_bitmap(const struct object_id *oid,1130enum object_type type,1131int flags,uint32_t name_hash,1132struct packed_git *pack, off_t offset)1133{1134uint32_t index_pos;11351136if(have_duplicate_entry(oid,0, &index_pos))1137return0;11381139if(!want_object_in_pack(oid,0, &pack, &offset))1140return0;11411142create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);11431144display_progress(progress_state, nr_result);1145return1;1146}11471148struct pbase_tree_cache {1149struct object_id oid;1150int ref;1151int temporary;1152void*tree_data;1153unsigned long tree_size;1154};11551156static struct pbase_tree_cache *(pbase_tree_cache[256]);1157static intpbase_tree_cache_ix(const struct object_id *oid)1158{1159return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1160}1161static intpbase_tree_cache_ix_incr(int ix)1162{1163return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1164}11651166static struct pbase_tree {1167struct pbase_tree *next;1168/* This is a phony "cache" entry; we are not1169 * going to evict it or find it through _get()1170 * mechanism -- this is for the toplevel node that1171 * would almost always change with any commit.1172 */1173struct pbase_tree_cache pcache;1174} *pbase_tree;11751176static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1177{1178struct pbase_tree_cache *ent, *nent;1179void*data;1180unsigned long size;1181enum object_type type;1182int neigh;1183int my_ix =pbase_tree_cache_ix(oid);1184int available_ix = -1;11851186/* pbase-tree-cache acts as a limited hashtable.1187 * your object will be found at your index or within a few1188 * slots after that slot if it is cached.1189 */1190for(neigh =0; neigh <8; neigh++) {1191 ent = pbase_tree_cache[my_ix];1192if(ent && !oidcmp(&ent->oid, oid)) {1193 ent->ref++;1194return ent;1195}1196else if(((available_ix <0) && (!ent || !ent->ref)) ||1197((0<= available_ix) &&1198(!ent && pbase_tree_cache[available_ix])))1199 available_ix = my_ix;1200if(!ent)1201break;1202 my_ix =pbase_tree_cache_ix_incr(my_ix);1203}12041205/* Did not find one. Either we got a bogus request or1206 * we need to read and perhaps cache.1207 */1208 data =read_object_file(oid, &type, &size);1209if(!data)1210return NULL;1211if(type != OBJ_TREE) {1212free(data);1213return NULL;1214}12151216/* We need to either cache or return a throwaway copy */12171218if(available_ix <0)1219 ent = NULL;1220else{1221 ent = pbase_tree_cache[available_ix];1222 my_ix = available_ix;1223}12241225if(!ent) {1226 nent =xmalloc(sizeof(*nent));1227 nent->temporary = (available_ix <0);1228}1229else{1230/* evict and reuse */1231free(ent->tree_data);1232 nent = ent;1233}1234oidcpy(&nent->oid, oid);1235 nent->tree_data = data;1236 nent->tree_size = size;1237 nent->ref =1;1238if(!nent->temporary)1239 pbase_tree_cache[my_ix] = nent;1240return nent;1241}12421243static voidpbase_tree_put(struct pbase_tree_cache *cache)1244{1245if(!cache->temporary) {1246 cache->ref--;1247return;1248}1249free(cache->tree_data);1250free(cache);1251}12521253static intname_cmp_len(const char*name)1254{1255int i;1256for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1257;1258return i;1259}12601261static voidadd_pbase_object(struct tree_desc *tree,1262const char*name,1263int cmplen,1264const char*fullname)1265{1266struct name_entry entry;1267int cmp;12681269while(tree_entry(tree,&entry)) {1270if(S_ISGITLINK(entry.mode))1271continue;1272 cmp =tree_entry_len(&entry) != cmplen ?1:1273memcmp(name, entry.path, cmplen);1274if(cmp >0)1275continue;1276if(cmp <0)1277return;1278if(name[cmplen] !='/') {1279add_object_entry(entry.oid,1280object_type(entry.mode),1281 fullname,1);1282return;1283}1284if(S_ISDIR(entry.mode)) {1285struct tree_desc sub;1286struct pbase_tree_cache *tree;1287const char*down = name+cmplen+1;1288int downlen =name_cmp_len(down);12891290 tree =pbase_tree_get(entry.oid);1291if(!tree)1292return;1293init_tree_desc(&sub, tree->tree_data, tree->tree_size);12941295add_pbase_object(&sub, down, downlen, fullname);1296pbase_tree_put(tree);1297}1298}1299}13001301static unsigned*done_pbase_paths;1302static int done_pbase_paths_num;1303static int done_pbase_paths_alloc;1304static intdone_pbase_path_pos(unsigned hash)1305{1306int lo =0;1307int hi = done_pbase_paths_num;1308while(lo < hi) {1309int mi = lo + (hi - lo) /2;1310if(done_pbase_paths[mi] == hash)1311return mi;1312if(done_pbase_paths[mi] < hash)1313 hi = mi;1314else1315 lo = mi +1;1316}1317return-lo-1;1318}13191320static intcheck_pbase_path(unsigned hash)1321{1322int pos =done_pbase_path_pos(hash);1323if(0<= pos)1324return1;1325 pos = -pos -1;1326ALLOC_GROW(done_pbase_paths,1327 done_pbase_paths_num +1,1328 done_pbase_paths_alloc);1329 done_pbase_paths_num++;1330if(pos < done_pbase_paths_num)1331MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1332 done_pbase_paths_num - pos -1);1333 done_pbase_paths[pos] = hash;1334return0;1335}13361337static voidadd_preferred_base_object(const char*name)1338{1339struct pbase_tree *it;1340int cmplen;1341unsigned hash =pack_name_hash(name);13421343if(!num_preferred_base ||check_pbase_path(hash))1344return;13451346 cmplen =name_cmp_len(name);1347for(it = pbase_tree; it; it = it->next) {1348if(cmplen ==0) {1349add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1350}1351else{1352struct tree_desc tree;1353init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1354add_pbase_object(&tree, name, cmplen, name);1355}1356}1357}13581359static voidadd_preferred_base(struct object_id *oid)1360{1361struct pbase_tree *it;1362void*data;1363unsigned long size;1364struct object_id tree_oid;13651366if(window <= num_preferred_base++)1367return;13681369 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1370if(!data)1371return;13721373for(it = pbase_tree; it; it = it->next) {1374if(!oidcmp(&it->pcache.oid, &tree_oid)) {1375free(data);1376return;1377}1378}13791380 it =xcalloc(1,sizeof(*it));1381 it->next = pbase_tree;1382 pbase_tree = it;13831384oidcpy(&it->pcache.oid, &tree_oid);1385 it->pcache.tree_data = data;1386 it->pcache.tree_size = size;1387}13881389static voidcleanup_preferred_base(void)1390{1391struct pbase_tree *it;1392unsigned i;13931394 it = pbase_tree;1395 pbase_tree = NULL;1396while(it) {1397struct pbase_tree *tmp = it;1398 it = tmp->next;1399free(tmp->pcache.tree_data);1400free(tmp);1401}14021403for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1404if(!pbase_tree_cache[i])1405continue;1406free(pbase_tree_cache[i]->tree_data);1407FREE_AND_NULL(pbase_tree_cache[i]);1408}14091410FREE_AND_NULL(done_pbase_paths);1411 done_pbase_paths_num = done_pbase_paths_alloc =0;1412}14131414static voidcheck_object(struct object_entry *entry)1415{1416unsigned long canonical_size;14171418if(IN_PACK(entry)) {1419struct packed_git *p =IN_PACK(entry);1420struct pack_window *w_curs = NULL;1421const unsigned char*base_ref = NULL;1422struct object_entry *base_entry;1423unsigned long used, used_0;1424unsigned long avail;1425 off_t ofs;1426unsigned char*buf, c;1427enum object_type type;1428unsigned long in_pack_size;14291430 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14311432/*1433 * We want in_pack_type even if we do not reuse delta1434 * since non-delta representations could still be reused.1435 */1436 used =unpack_object_header_buffer(buf, avail,1437&type,1438&in_pack_size);1439if(used ==0)1440goto give_up;14411442if(type <0)1443BUG("invalid type%d", type);1444 entry->in_pack_type = type;14451446/*1447 * Determine if this is a delta and if so whether we can1448 * reuse it or not. Otherwise let's find out as cheaply as1449 * possible what the actual type and size for this object is.1450 */1451switch(entry->in_pack_type) {1452default:1453/* Not a delta hence we've already got all we need. */1454oe_set_type(entry, entry->in_pack_type);1455SET_SIZE(entry, in_pack_size);1456 entry->in_pack_header_size = used;1457if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > OBJ_BLOB)1458goto give_up;1459unuse_pack(&w_curs);1460return;1461case OBJ_REF_DELTA:1462if(reuse_delta && !entry->preferred_base)1463 base_ref =use_pack(p, &w_curs,1464 entry->in_pack_offset + used, NULL);1465 entry->in_pack_header_size = used +20;1466break;1467case OBJ_OFS_DELTA:1468 buf =use_pack(p, &w_curs,1469 entry->in_pack_offset + used, NULL);1470 used_0 =0;1471 c = buf[used_0++];1472 ofs = c &127;1473while(c &128) {1474 ofs +=1;1475if(!ofs ||MSB(ofs,7)) {1476error("delta base offset overflow in pack for%s",1477oid_to_hex(&entry->idx.oid));1478goto give_up;1479}1480 c = buf[used_0++];1481 ofs = (ofs <<7) + (c &127);1482}1483 ofs = entry->in_pack_offset - ofs;1484if(ofs <=0|| ofs >= entry->in_pack_offset) {1485error("delta base offset out of bound for%s",1486oid_to_hex(&entry->idx.oid));1487goto give_up;1488}1489if(reuse_delta && !entry->preferred_base) {1490struct revindex_entry *revidx;1491 revidx =find_pack_revindex(p, ofs);1492if(!revidx)1493goto give_up;1494 base_ref =nth_packed_object_sha1(p, revidx->nr);1495}1496 entry->in_pack_header_size = used + used_0;1497break;1498}14991500if(base_ref && (base_entry =packlist_find(&to_pack, base_ref, NULL))) {1501/*1502 * If base_ref was set above that means we wish to1503 * reuse delta data, and we even found that base1504 * in the list of objects we want to pack. Goodie!1505 *1506 * Depth value does not matter - find_deltas() will1507 * never consider reused delta as the base object to1508 * deltify other objects against, in order to avoid1509 * circular deltas.1510 */1511oe_set_type(entry, entry->in_pack_type);1512SET_SIZE(entry, in_pack_size);/* delta size */1513SET_DELTA(entry, base_entry);1514SET_DELTA_SIZE(entry, in_pack_size);1515 entry->delta_sibling_idx = base_entry->delta_child_idx;1516SET_DELTA_CHILD(base_entry, entry);1517unuse_pack(&w_curs);1518return;1519}15201521if(oe_type(entry)) {1522 off_t delta_pos;15231524/*1525 * This must be a delta and we already know what the1526 * final object type is. Let's extract the actual1527 * object size from the delta header.1528 */1529 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1530 canonical_size =get_size_from_delta(p, &w_curs, delta_pos);1531if(canonical_size ==0)1532goto give_up;1533SET_SIZE(entry, canonical_size);1534unuse_pack(&w_curs);1535return;1536}15371538/*1539 * No choice but to fall back to the recursive delta walk1540 * with sha1_object_info() to find about the object type1541 * at this point...1542 */1543 give_up:1544unuse_pack(&w_curs);1545}15461547oe_set_type(entry,oid_object_info(&entry->idx.oid, &canonical_size));1548if(entry->type_valid) {1549SET_SIZE(entry, canonical_size);1550}else{1551/*1552 * Bad object type is checked in prepare_pack(). This is1553 * to permit a missing preferred base object to be ignored1554 * as a preferred base. Doing so can result in a larger1555 * pack file, but the transfer will still take place.1556 */1557}1558}15591560static intpack_offset_sort(const void*_a,const void*_b)1561{1562const struct object_entry *a = *(struct object_entry **)_a;1563const struct object_entry *b = *(struct object_entry **)_b;1564const struct packed_git *a_in_pack =IN_PACK(a);1565const struct packed_git *b_in_pack =IN_PACK(b);15661567/* avoid filesystem trashing with loose objects */1568if(!a_in_pack && !b_in_pack)1569returnoidcmp(&a->idx.oid, &b->idx.oid);15701571if(a_in_pack < b_in_pack)1572return-1;1573if(a_in_pack > b_in_pack)1574return1;1575return a->in_pack_offset < b->in_pack_offset ? -1:1576(a->in_pack_offset > b->in_pack_offset);1577}15781579/*1580 * Drop an on-disk delta we were planning to reuse. Naively, this would1581 * just involve blanking out the "delta" field, but we have to deal1582 * with some extra book-keeping:1583 *1584 * 1. Removing ourselves from the delta_sibling linked list.1585 *1586 * 2. Updating our size/type to the non-delta representation. These were1587 * either not recorded initially (size) or overwritten with the delta type1588 * (type) when check_object() decided to reuse the delta.1589 *1590 * 3. Resetting our delta depth, as we are now a base object.1591 */1592static voiddrop_reused_delta(struct object_entry *entry)1593{1594unsigned*idx = &to_pack.objects[entry->delta_idx -1].delta_child_idx;1595struct object_info oi = OBJECT_INFO_INIT;1596enum object_type type;1597unsigned long size;15981599while(*idx) {1600struct object_entry *oe = &to_pack.objects[*idx -1];16011602if(oe == entry)1603*idx = oe->delta_sibling_idx;1604else1605 idx = &oe->delta_sibling_idx;1606}1607SET_DELTA(entry, NULL);1608 entry->depth =0;16091610 oi.sizep = &size;1611 oi.typep = &type;1612if(packed_object_info(IN_PACK(entry), entry->in_pack_offset, &oi) <0) {1613/*1614 * We failed to get the info from this pack for some reason;1615 * fall back to sha1_object_info, which may find another copy.1616 * And if that fails, the error will be recorded in oe_type(entry)1617 * and dealt with in prepare_pack().1618 */1619oe_set_type(entry,oid_object_info(&entry->idx.oid, &size));1620}else{1621oe_set_type(entry, type);1622}1623SET_SIZE(entry, size);1624}16251626/*1627 * Follow the chain of deltas from this entry onward, throwing away any links1628 * that cause us to hit a cycle (as determined by the DFS state flags in1629 * the entries).1630 *1631 * We also detect too-long reused chains that would violate our --depth1632 * limit.1633 */1634static voidbreak_delta_chains(struct object_entry *entry)1635{1636/*1637 * The actual depth of each object we will write is stored as an int,1638 * as it cannot exceed our int "depth" limit. But before we break1639 * changes based no that limit, we may potentially go as deep as the1640 * number of objects, which is elsewhere bounded to a uint32_t.1641 */1642uint32_t total_depth;1643struct object_entry *cur, *next;16441645for(cur = entry, total_depth =0;1646 cur;1647 cur =DELTA(cur), total_depth++) {1648if(cur->dfs_state == DFS_DONE) {1649/*1650 * We've already seen this object and know it isn't1651 * part of a cycle. We do need to append its depth1652 * to our count.1653 */1654 total_depth += cur->depth;1655break;1656}16571658/*1659 * We break cycles before looping, so an ACTIVE state (or any1660 * other cruft which made its way into the state variable)1661 * is a bug.1662 */1663if(cur->dfs_state != DFS_NONE)1664die("BUG: confusing delta dfs state in first pass:%d",1665 cur->dfs_state);16661667/*1668 * Now we know this is the first time we've seen the object. If1669 * it's not a delta, we're done traversing, but we'll mark it1670 * done to save time on future traversals.1671 */1672if(!DELTA(cur)) {1673 cur->dfs_state = DFS_DONE;1674break;1675}16761677/*1678 * Mark ourselves as active and see if the next step causes1679 * us to cycle to another active object. It's important to do1680 * this _before_ we loop, because it impacts where we make the1681 * cut, and thus how our total_depth counter works.1682 * E.g., We may see a partial loop like:1683 *1684 * A -> B -> C -> D -> B1685 *1686 * Cutting B->C breaks the cycle. But now the depth of A is1687 * only 1, and our total_depth counter is at 3. The size of the1688 * error is always one less than the size of the cycle we1689 * broke. Commits C and D were "lost" from A's chain.1690 *1691 * If we instead cut D->B, then the depth of A is correct at 3.1692 * We keep all commits in the chain that we examined.1693 */1694 cur->dfs_state = DFS_ACTIVE;1695if(DELTA(cur)->dfs_state == DFS_ACTIVE) {1696drop_reused_delta(cur);1697 cur->dfs_state = DFS_DONE;1698break;1699}1700}17011702/*1703 * And now that we've gone all the way to the bottom of the chain, we1704 * need to clear the active flags and set the depth fields as1705 * appropriate. Unlike the loop above, which can quit when it drops a1706 * delta, we need to keep going to look for more depth cuts. So we need1707 * an extra "next" pointer to keep going after we reset cur->delta.1708 */1709for(cur = entry; cur; cur = next) {1710 next =DELTA(cur);17111712/*1713 * We should have a chain of zero or more ACTIVE states down to1714 * a final DONE. We can quit after the DONE, because either it1715 * has no bases, or we've already handled them in a previous1716 * call.1717 */1718if(cur->dfs_state == DFS_DONE)1719break;1720else if(cur->dfs_state != DFS_ACTIVE)1721die("BUG: confusing delta dfs state in second pass:%d",1722 cur->dfs_state);17231724/*1725 * If the total_depth is more than depth, then we need to snip1726 * the chain into two or more smaller chains that don't exceed1727 * the maximum depth. Most of the resulting chains will contain1728 * (depth + 1) entries (i.e., depth deltas plus one base), and1729 * the last chain (i.e., the one containing entry) will contain1730 * whatever entries are left over, namely1731 * (total_depth % (depth + 1)) of them.1732 *1733 * Since we are iterating towards decreasing depth, we need to1734 * decrement total_depth as we go, and we need to write to the1735 * entry what its final depth will be after all of the1736 * snipping. Since we're snipping into chains of length (depth1737 * + 1) entries, the final depth of an entry will be its1738 * original depth modulo (depth + 1). Any time we encounter an1739 * entry whose final depth is supposed to be zero, we snip it1740 * from its delta base, thereby making it so.1741 */1742 cur->depth = (total_depth--) % (depth +1);1743if(!cur->depth)1744drop_reused_delta(cur);17451746 cur->dfs_state = DFS_DONE;1747}1748}17491750static voidget_object_details(void)1751{1752uint32_t i;1753struct object_entry **sorted_by_offset;17541755 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1756for(i =0; i < to_pack.nr_objects; i++)1757 sorted_by_offset[i] = to_pack.objects + i;1758QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17591760for(i =0; i < to_pack.nr_objects; i++) {1761struct object_entry *entry = sorted_by_offset[i];1762check_object(entry);1763if(entry->type_valid &&1764oe_size_greater_than(&to_pack, entry, big_file_threshold))1765 entry->no_try_delta =1;1766}17671768/*1769 * This must happen in a second pass, since we rely on the delta1770 * information for the whole list being completed.1771 */1772for(i =0; i < to_pack.nr_objects; i++)1773break_delta_chains(&to_pack.objects[i]);17741775free(sorted_by_offset);1776}17771778/*1779 * We search for deltas in a list sorted by type, by filename hash, and then1780 * by size, so that we see progressively smaller and smaller files.1781 * That's because we prefer deltas to be from the bigger file1782 * to the smaller -- deletes are potentially cheaper, but perhaps1783 * more importantly, the bigger file is likely the more recent1784 * one. The deepest deltas are therefore the oldest objects which are1785 * less susceptible to be accessed often.1786 */1787static inttype_size_sort(const void*_a,const void*_b)1788{1789const struct object_entry *a = *(struct object_entry **)_a;1790const struct object_entry *b = *(struct object_entry **)_b;1791enum object_type a_type =oe_type(a);1792enum object_type b_type =oe_type(b);1793unsigned long a_size =SIZE(a);1794unsigned long b_size =SIZE(b);17951796if(a_type > b_type)1797return-1;1798if(a_type < b_type)1799return1;1800if(a->hash > b->hash)1801return-1;1802if(a->hash < b->hash)1803return1;1804if(a->preferred_base > b->preferred_base)1805return-1;1806if(a->preferred_base < b->preferred_base)1807return1;1808if(a_size > b_size)1809return-1;1810if(a_size < b_size)1811return1;1812return a < b ? -1: (a > b);/* newest first */1813}18141815struct unpacked {1816struct object_entry *entry;1817void*data;1818struct delta_index *index;1819unsigned depth;1820};18211822static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1823unsigned long delta_size)1824{1825if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1826return0;18271828if(delta_size < cache_max_small_delta_size)1829return1;18301831/* cache delta, if objects are large enough compared to delta size */1832if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1833return1;18341835return0;1836}18371838#ifndef NO_PTHREADS18391840static pthread_mutex_t read_mutex;1841#define read_lock() pthread_mutex_lock(&read_mutex)1842#define read_unlock() pthread_mutex_unlock(&read_mutex)18431844static pthread_mutex_t cache_mutex;1845#define cache_lock() pthread_mutex_lock(&cache_mutex)1846#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18471848static pthread_mutex_t progress_mutex;1849#define progress_lock() pthread_mutex_lock(&progress_mutex)1850#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18511852#else18531854#define read_lock() (void)01855#define read_unlock() (void)01856#define cache_lock() (void)01857#define cache_unlock() (void)01858#define progress_lock() (void)01859#define progress_unlock() (void)018601861#endif18621863/*1864 * Return the size of the object without doing any delta1865 * reconstruction (so non-deltas are true object sizes, but deltas1866 * return the size of the delta data).1867 */1868unsigned longoe_get_size_slow(struct packing_data *pack,1869const struct object_entry *e)1870{1871struct packed_git *p;1872struct pack_window *w_curs;1873unsigned char*buf;1874enum object_type type;1875unsigned long used, avail, size;18761877if(e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1878read_lock();1879if(oid_object_info(&e->idx.oid, &size) <0)1880die(_("unable to get size of%s"),1881oid_to_hex(&e->idx.oid));1882read_unlock();1883return size;1884}18851886 p =oe_in_pack(pack, e);1887if(!p)1888BUG("when e->type is a delta, it must belong to a pack");18891890read_lock();1891 w_curs = NULL;1892 buf =use_pack(p, &w_curs, e->in_pack_offset, &avail);1893 used =unpack_object_header_buffer(buf, avail, &type, &size);1894if(used ==0)1895die(_("unable to parse object header of%s"),1896oid_to_hex(&e->idx.oid));18971898unuse_pack(&w_curs);1899read_unlock();1900return size;1901}19021903static inttry_delta(struct unpacked *trg,struct unpacked *src,1904unsigned max_depth,unsigned long*mem_usage)1905{1906struct object_entry *trg_entry = trg->entry;1907struct object_entry *src_entry = src->entry;1908unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1909unsigned ref_depth;1910enum object_type type;1911void*delta_buf;19121913/* Don't bother doing diffs between different types */1914if(oe_type(trg_entry) !=oe_type(src_entry))1915return-1;19161917/*1918 * We do not bother to try a delta that we discarded on an1919 * earlier try, but only when reusing delta data. Note that1920 * src_entry that is marked as the preferred_base should always1921 * be considered, as even if we produce a suboptimal delta against1922 * it, we will still save the transfer cost, as we already know1923 * the other side has it and we won't send src_entry at all.1924 */1925if(reuse_delta &&IN_PACK(trg_entry) &&1926IN_PACK(trg_entry) ==IN_PACK(src_entry) &&1927!src_entry->preferred_base &&1928 trg_entry->in_pack_type != OBJ_REF_DELTA &&1929 trg_entry->in_pack_type != OBJ_OFS_DELTA)1930return0;19311932/* Let's not bust the allowed depth. */1933if(src->depth >= max_depth)1934return0;19351936/* Now some size filtering heuristics. */1937 trg_size =SIZE(trg_entry);1938if(!DELTA(trg_entry)) {1939 max_size = trg_size/2-20;1940 ref_depth =1;1941}else{1942 max_size =DELTA_SIZE(trg_entry);1943 ref_depth = trg->depth;1944}1945 max_size = (uint64_t)max_size * (max_depth - src->depth) /1946(max_depth - ref_depth +1);1947if(max_size ==0)1948return0;1949 src_size =SIZE(src_entry);1950 sizediff = src_size < trg_size ? trg_size - src_size :0;1951if(sizediff >= max_size)1952return0;1953if(trg_size < src_size /32)1954return0;19551956/* Load data if not already done */1957if(!trg->data) {1958read_lock();1959 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1960read_unlock();1961if(!trg->data)1962die("object%scannot be read",1963oid_to_hex(&trg_entry->idx.oid));1964if(sz != trg_size)1965die("object%sinconsistent object length (%lu vs%lu)",1966oid_to_hex(&trg_entry->idx.oid), sz,1967 trg_size);1968*mem_usage += sz;1969}1970if(!src->data) {1971read_lock();1972 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);1973read_unlock();1974if(!src->data) {1975if(src_entry->preferred_base) {1976static int warned =0;1977if(!warned++)1978warning("object%scannot be read",1979oid_to_hex(&src_entry->idx.oid));1980/*1981 * Those objects are not included in the1982 * resulting pack. Be resilient and ignore1983 * them if they can't be read, in case the1984 * pack could be created nevertheless.1985 */1986return0;1987}1988die("object%scannot be read",1989oid_to_hex(&src_entry->idx.oid));1990}1991if(sz != src_size)1992die("object%sinconsistent object length (%lu vs%lu)",1993oid_to_hex(&src_entry->idx.oid), sz,1994 src_size);1995*mem_usage += sz;1996}1997if(!src->index) {1998 src->index =create_delta_index(src->data, src_size);1999if(!src->index) {2000static int warned =0;2001if(!warned++)2002warning("suboptimal pack - out of memory");2003return0;2004}2005*mem_usage +=sizeof_delta_index(src->index);2006}20072008 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2009if(!delta_buf)2010return0;20112012if(DELTA(trg_entry)) {2013/* Prefer only shallower same-sized deltas. */2014if(delta_size ==DELTA_SIZE(trg_entry) &&2015 src->depth +1>= trg->depth) {2016free(delta_buf);2017return0;2018}2019}20202021/*2022 * Handle memory allocation outside of the cache2023 * accounting lock. Compiler will optimize the strangeness2024 * away when NO_PTHREADS is defined.2025 */2026free(trg_entry->delta_data);2027cache_lock();2028if(trg_entry->delta_data) {2029 delta_cache_size -=DELTA_SIZE(trg_entry);2030 trg_entry->delta_data = NULL;2031}2032if(delta_cacheable(src_size, trg_size, delta_size)) {2033 delta_cache_size += delta_size;2034cache_unlock();2035 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2036}else{2037cache_unlock();2038free(delta_buf);2039}20402041SET_DELTA(trg_entry, src_entry);2042SET_DELTA_SIZE(trg_entry, delta_size);2043 trg->depth = src->depth +1;20442045return1;2046}20472048static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2049{2050struct object_entry *child =DELTA_CHILD(me);2051unsigned int m = n;2052while(child) {2053unsigned int c =check_delta_limit(child, n +1);2054if(m < c)2055 m = c;2056 child =DELTA_SIBLING(child);2057}2058return m;2059}20602061static unsigned longfree_unpacked(struct unpacked *n)2062{2063unsigned long freed_mem =sizeof_delta_index(n->index);2064free_delta_index(n->index);2065 n->index = NULL;2066if(n->data) {2067 freed_mem +=SIZE(n->entry);2068FREE_AND_NULL(n->data);2069}2070 n->entry = NULL;2071 n->depth =0;2072return freed_mem;2073}20742075static voidfind_deltas(struct object_entry **list,unsigned*list_size,2076int window,int depth,unsigned*processed)2077{2078uint32_t i, idx =0, count =0;2079struct unpacked *array;2080unsigned long mem_usage =0;20812082 array =xcalloc(window,sizeof(struct unpacked));20832084for(;;) {2085struct object_entry *entry;2086struct unpacked *n = array + idx;2087int j, max_depth, best_base = -1;20882089progress_lock();2090if(!*list_size) {2091progress_unlock();2092break;2093}2094 entry = *list++;2095(*list_size)--;2096if(!entry->preferred_base) {2097(*processed)++;2098display_progress(progress_state, *processed);2099}2100progress_unlock();21012102 mem_usage -=free_unpacked(n);2103 n->entry = entry;21042105while(window_memory_limit &&2106 mem_usage > window_memory_limit &&2107 count >1) {2108uint32_t tail = (idx + window - count) % window;2109 mem_usage -=free_unpacked(array + tail);2110 count--;2111}21122113/* We do not compute delta to *create* objects we are not2114 * going to pack.2115 */2116if(entry->preferred_base)2117goto next;21182119/*2120 * If the current object is at pack edge, take the depth the2121 * objects that depend on the current object into account2122 * otherwise they would become too deep.2123 */2124 max_depth = depth;2125if(DELTA_CHILD(entry)) {2126 max_depth -=check_delta_limit(entry,0);2127if(max_depth <=0)2128goto next;2129}21302131 j = window;2132while(--j >0) {2133int ret;2134uint32_t other_idx = idx + j;2135struct unpacked *m;2136if(other_idx >= window)2137 other_idx -= window;2138 m = array + other_idx;2139if(!m->entry)2140break;2141 ret =try_delta(n, m, max_depth, &mem_usage);2142if(ret <0)2143break;2144else if(ret >0)2145 best_base = other_idx;2146}21472148/*2149 * If we decided to cache the delta data, then it is best2150 * to compress it right away. First because we have to do2151 * it anyway, and doing it here while we're threaded will2152 * save a lot of time in the non threaded write phase,2153 * as well as allow for caching more deltas within2154 * the same cache size limit.2155 * ...2156 * But only if not writing to stdout, since in that case2157 * the network is most likely throttling writes anyway,2158 * and therefore it is best to go to the write phase ASAP2159 * instead, as we can afford spending more time compressing2160 * between writes at that moment.2161 */2162if(entry->delta_data && !pack_to_stdout) {2163unsigned long size;21642165 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2166if(size < (1U<< OE_Z_DELTA_BITS)) {2167 entry->z_delta_size = size;2168cache_lock();2169 delta_cache_size -=DELTA_SIZE(entry);2170 delta_cache_size += entry->z_delta_size;2171cache_unlock();2172}else{2173FREE_AND_NULL(entry->delta_data);2174 entry->z_delta_size =0;2175}2176}21772178/* if we made n a delta, and if n is already at max2179 * depth, leaving it in the window is pointless. we2180 * should evict it first.2181 */2182if(DELTA(entry) && max_depth <= n->depth)2183continue;21842185/*2186 * Move the best delta base up in the window, after the2187 * currently deltified object, to keep it longer. It will2188 * be the first base object to be attempted next.2189 */2190if(DELTA(entry)) {2191struct unpacked swap = array[best_base];2192int dist = (window + idx - best_base) % window;2193int dst = best_base;2194while(dist--) {2195int src = (dst +1) % window;2196 array[dst] = array[src];2197 dst = src;2198}2199 array[dst] = swap;2200}22012202 next:2203 idx++;2204if(count +1< window)2205 count++;2206if(idx >= window)2207 idx =0;2208}22092210for(i =0; i < window; ++i) {2211free_delta_index(array[i].index);2212free(array[i].data);2213}2214free(array);2215}22162217#ifndef NO_PTHREADS22182219static voidtry_to_free_from_threads(size_t size)2220{2221read_lock();2222release_pack_memory(size);2223read_unlock();2224}22252226static try_to_free_t old_try_to_free_routine;22272228/*2229 * The main thread waits on the condition that (at least) one of the workers2230 * has stopped working (which is indicated in the .working member of2231 * struct thread_params).2232 * When a work thread has completed its work, it sets .working to 0 and2233 * signals the main thread and waits on the condition that .data_ready2234 * becomes 1.2235 */22362237struct thread_params {2238 pthread_t thread;2239struct object_entry **list;2240unsigned list_size;2241unsigned remaining;2242int window;2243int depth;2244int working;2245int data_ready;2246 pthread_mutex_t mutex;2247 pthread_cond_t cond;2248unsigned*processed;2249};22502251static pthread_cond_t progress_cond;22522253/*2254 * Mutex and conditional variable can't be statically-initialized on Windows.2255 */2256static voidinit_threaded_search(void)2257{2258init_recursive_mutex(&read_mutex);2259pthread_mutex_init(&cache_mutex, NULL);2260pthread_mutex_init(&progress_mutex, NULL);2261pthread_cond_init(&progress_cond, NULL);2262pthread_mutex_init(&to_pack.lock, NULL);2263 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2264}22652266static voidcleanup_threaded_search(void)2267{2268set_try_to_free_routine(old_try_to_free_routine);2269pthread_cond_destroy(&progress_cond);2270pthread_mutex_destroy(&read_mutex);2271pthread_mutex_destroy(&cache_mutex);2272pthread_mutex_destroy(&progress_mutex);2273}22742275static void*threaded_find_deltas(void*arg)2276{2277struct thread_params *me = arg;22782279progress_lock();2280while(me->remaining) {2281progress_unlock();22822283find_deltas(me->list, &me->remaining,2284 me->window, me->depth, me->processed);22852286progress_lock();2287 me->working =0;2288pthread_cond_signal(&progress_cond);2289progress_unlock();22902291/*2292 * We must not set ->data_ready before we wait on the2293 * condition because the main thread may have set it to 12294 * before we get here. In order to be sure that new2295 * work is available if we see 1 in ->data_ready, it2296 * was initialized to 0 before this thread was spawned2297 * and we reset it to 0 right away.2298 */2299pthread_mutex_lock(&me->mutex);2300while(!me->data_ready)2301pthread_cond_wait(&me->cond, &me->mutex);2302 me->data_ready =0;2303pthread_mutex_unlock(&me->mutex);23042305progress_lock();2306}2307progress_unlock();2308/* leave ->working 1 so that this doesn't get more work assigned */2309return NULL;2310}23112312static voidll_find_deltas(struct object_entry **list,unsigned list_size,2313int window,int depth,unsigned*processed)2314{2315struct thread_params *p;2316int i, ret, active_threads =0;23172318init_threaded_search();23192320if(delta_search_threads <=1) {2321find_deltas(list, &list_size, window, depth, processed);2322cleanup_threaded_search();2323return;2324}2325if(progress > pack_to_stdout)2326fprintf(stderr,"Delta compression using up to%dthreads.\n",2327 delta_search_threads);2328 p =xcalloc(delta_search_threads,sizeof(*p));23292330/* Partition the work amongst work threads. */2331for(i =0; i < delta_search_threads; i++) {2332unsigned sub_size = list_size / (delta_search_threads - i);23332334/* don't use too small segments or no deltas will be found */2335if(sub_size <2*window && i+1< delta_search_threads)2336 sub_size =0;23372338 p[i].window = window;2339 p[i].depth = depth;2340 p[i].processed = processed;2341 p[i].working =1;2342 p[i].data_ready =0;23432344/* try to split chunks on "path" boundaries */2345while(sub_size && sub_size < list_size &&2346 list[sub_size]->hash &&2347 list[sub_size]->hash == list[sub_size-1]->hash)2348 sub_size++;23492350 p[i].list = list;2351 p[i].list_size = sub_size;2352 p[i].remaining = sub_size;23532354 list += sub_size;2355 list_size -= sub_size;2356}23572358/* Start work threads. */2359for(i =0; i < delta_search_threads; i++) {2360if(!p[i].list_size)2361continue;2362pthread_mutex_init(&p[i].mutex, NULL);2363pthread_cond_init(&p[i].cond, NULL);2364 ret =pthread_create(&p[i].thread, NULL,2365 threaded_find_deltas, &p[i]);2366if(ret)2367die("unable to create thread:%s",strerror(ret));2368 active_threads++;2369}23702371/*2372 * Now let's wait for work completion. Each time a thread is done2373 * with its work, we steal half of the remaining work from the2374 * thread with the largest number of unprocessed objects and give2375 * it to that newly idle thread. This ensure good load balancing2376 * until the remaining object list segments are simply too short2377 * to be worth splitting anymore.2378 */2379while(active_threads) {2380struct thread_params *target = NULL;2381struct thread_params *victim = NULL;2382unsigned sub_size =0;23832384progress_lock();2385for(;;) {2386for(i =0; !target && i < delta_search_threads; i++)2387if(!p[i].working)2388 target = &p[i];2389if(target)2390break;2391pthread_cond_wait(&progress_cond, &progress_mutex);2392}23932394for(i =0; i < delta_search_threads; i++)2395if(p[i].remaining >2*window &&2396(!victim || victim->remaining < p[i].remaining))2397 victim = &p[i];2398if(victim) {2399 sub_size = victim->remaining /2;2400 list = victim->list + victim->list_size - sub_size;2401while(sub_size && list[0]->hash &&2402 list[0]->hash == list[-1]->hash) {2403 list++;2404 sub_size--;2405}2406if(!sub_size) {2407/*2408 * It is possible for some "paths" to have2409 * so many objects that no hash boundary2410 * might be found. Let's just steal the2411 * exact half in that case.2412 */2413 sub_size = victim->remaining /2;2414 list -= sub_size;2415}2416 target->list = list;2417 victim->list_size -= sub_size;2418 victim->remaining -= sub_size;2419}2420 target->list_size = sub_size;2421 target->remaining = sub_size;2422 target->working =1;2423progress_unlock();24242425pthread_mutex_lock(&target->mutex);2426 target->data_ready =1;2427pthread_cond_signal(&target->cond);2428pthread_mutex_unlock(&target->mutex);24292430if(!sub_size) {2431pthread_join(target->thread, NULL);2432pthread_cond_destroy(&target->cond);2433pthread_mutex_destroy(&target->mutex);2434 active_threads--;2435}2436}2437cleanup_threaded_search();2438free(p);2439}24402441#else2442#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2443#endif24442445static voidadd_tag_chain(const struct object_id *oid)2446{2447struct tag *tag;24482449/*2450 * We catch duplicates already in add_object_entry(), but we'd2451 * prefer to do this extra check to avoid having to parse the2452 * tag at all if we already know that it's being packed (e.g., if2453 * it was included via bitmaps, we would not have parsed it2454 * previously).2455 */2456if(packlist_find(&to_pack, oid->hash, NULL))2457return;24582459 tag =lookup_tag(oid);2460while(1) {2461if(!tag ||parse_tag(tag) || !tag->tagged)2462die("unable to pack objects reachable from tag%s",2463oid_to_hex(oid));24642465add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);24662467if(tag->tagged->type != OBJ_TAG)2468return;24692470 tag = (struct tag *)tag->tagged;2471}2472}24732474static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2475{2476struct object_id peeled;24772478if(starts_with(path,"refs/tags/") &&/* is a tag? */2479!peel_ref(path, &peeled) &&/* peelable? */2480packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2481add_tag_chain(oid);2482return0;2483}24842485static voidprepare_pack(int window,int depth)2486{2487struct object_entry **delta_list;2488uint32_t i, nr_deltas;2489unsigned n;24902491get_object_details();24922493/*2494 * If we're locally repacking then we need to be doubly careful2495 * from now on in order to make sure no stealth corruption gets2496 * propagated to the new pack. Clients receiving streamed packs2497 * should validate everything they get anyway so no need to incur2498 * the additional cost here in that case.2499 */2500if(!pack_to_stdout)2501 do_check_packed_object_crc =1;25022503if(!to_pack.nr_objects || !window || !depth)2504return;25052506ALLOC_ARRAY(delta_list, to_pack.nr_objects);2507 nr_deltas = n =0;25082509for(i =0; i < to_pack.nr_objects; i++) {2510struct object_entry *entry = to_pack.objects + i;25112512if(DELTA(entry))2513/* This happens if we decided to reuse existing2514 * delta from a pack. "reuse_delta &&" is implied.2515 */2516continue;25172518if(!entry->type_valid ||2519oe_size_less_than(&to_pack, entry,50))2520continue;25212522if(entry->no_try_delta)2523continue;25242525if(!entry->preferred_base) {2526 nr_deltas++;2527if(oe_type(entry) <0)2528die("unable to get type of object%s",2529oid_to_hex(&entry->idx.oid));2530}else{2531if(oe_type(entry) <0) {2532/*2533 * This object is not found, but we2534 * don't have to include it anyway.2535 */2536continue;2537}2538}25392540 delta_list[n++] = entry;2541}25422543if(nr_deltas && n >1) {2544unsigned nr_done =0;2545if(progress)2546 progress_state =start_progress(_("Compressing objects"),2547 nr_deltas);2548QSORT(delta_list, n, type_size_sort);2549ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2550stop_progress(&progress_state);2551if(nr_done != nr_deltas)2552die("inconsistency with delta count");2553}2554free(delta_list);2555}25562557static intgit_pack_config(const char*k,const char*v,void*cb)2558{2559if(!strcmp(k,"pack.window")) {2560 window =git_config_int(k, v);2561return0;2562}2563if(!strcmp(k,"pack.windowmemory")) {2564 window_memory_limit =git_config_ulong(k, v);2565return0;2566}2567if(!strcmp(k,"pack.depth")) {2568 depth =git_config_int(k, v);2569return0;2570}2571if(!strcmp(k,"pack.deltacachesize")) {2572 max_delta_cache_size =git_config_int(k, v);2573return0;2574}2575if(!strcmp(k,"pack.deltacachelimit")) {2576 cache_max_small_delta_size =git_config_int(k, v);2577return0;2578}2579if(!strcmp(k,"pack.writebitmaphashcache")) {2580if(git_config_bool(k, v))2581 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2582else2583 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2584}2585if(!strcmp(k,"pack.usebitmaps")) {2586 use_bitmap_index_default =git_config_bool(k, v);2587return0;2588}2589if(!strcmp(k,"pack.threads")) {2590 delta_search_threads =git_config_int(k, v);2591if(delta_search_threads <0)2592die("invalid number of threads specified (%d)",2593 delta_search_threads);2594#ifdef NO_PTHREADS2595if(delta_search_threads !=1) {2596warning("no threads support, ignoring%s", k);2597 delta_search_threads =0;2598}2599#endif2600return0;2601}2602if(!strcmp(k,"pack.indexversion")) {2603 pack_idx_opts.version =git_config_int(k, v);2604if(pack_idx_opts.version >2)2605die("bad pack.indexversion=%"PRIu32,2606 pack_idx_opts.version);2607return0;2608}2609returngit_default_config(k, v, cb);2610}26112612static voidread_object_list_from_stdin(void)2613{2614char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2615struct object_id oid;2616const char*p;26172618for(;;) {2619if(!fgets(line,sizeof(line), stdin)) {2620if(feof(stdin))2621break;2622if(!ferror(stdin))2623die("fgets returned NULL, not EOF, not error!");2624if(errno != EINTR)2625die_errno("fgets");2626clearerr(stdin);2627continue;2628}2629if(line[0] =='-') {2630if(get_oid_hex(line+1, &oid))2631die("expected edge object ID, got garbage:\n%s",2632 line);2633add_preferred_base(&oid);2634continue;2635}2636if(parse_oid_hex(line, &oid, &p))2637die("expected object ID, got garbage:\n%s", line);26382639add_preferred_base_object(p +1);2640add_object_entry(&oid, OBJ_NONE, p +1,0);2641}2642}26432644/* Remember to update object flag allocation in object.h */2645#define OBJECT_ADDED (1u<<20)26462647static voidshow_commit(struct commit *commit,void*data)2648{2649add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2650 commit->object.flags |= OBJECT_ADDED;26512652if(write_bitmap_index)2653index_commit_for_bitmap(commit);2654}26552656static voidshow_object(struct object *obj,const char*name,void*data)2657{2658add_preferred_base_object(name);2659add_object_entry(&obj->oid, obj->type, name,0);2660 obj->flags |= OBJECT_ADDED;2661}26622663static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2664{2665assert(arg_missing_action == MA_ALLOW_ANY);26662667/*2668 * Quietly ignore ALL missing objects. This avoids problems with2669 * staging them now and getting an odd error later.2670 */2671if(!has_object_file(&obj->oid))2672return;26732674show_object(obj, name, data);2675}26762677static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2678{2679assert(arg_missing_action == MA_ALLOW_PROMISOR);26802681/*2682 * Quietly ignore EXPECTED missing objects. This avoids problems with2683 * staging them now and getting an odd error later.2684 */2685if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2686return;26872688show_object(obj, name, data);2689}26902691static intoption_parse_missing_action(const struct option *opt,2692const char*arg,int unset)2693{2694assert(arg);2695assert(!unset);26962697if(!strcmp(arg,"error")) {2698 arg_missing_action = MA_ERROR;2699 fn_show_object = show_object;2700return0;2701}27022703if(!strcmp(arg,"allow-any")) {2704 arg_missing_action = MA_ALLOW_ANY;2705 fetch_if_missing =0;2706 fn_show_object = show_object__ma_allow_any;2707return0;2708}27092710if(!strcmp(arg,"allow-promisor")) {2711 arg_missing_action = MA_ALLOW_PROMISOR;2712 fetch_if_missing =0;2713 fn_show_object = show_object__ma_allow_promisor;2714return0;2715}27162717die(_("invalid value for --missing"));2718return0;2719}27202721static voidshow_edge(struct commit *commit)2722{2723add_preferred_base(&commit->object.oid);2724}27252726struct in_pack_object {2727 off_t offset;2728struct object *object;2729};27302731struct in_pack {2732unsigned int alloc;2733unsigned int nr;2734struct in_pack_object *array;2735};27362737static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2738{2739 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2740 in_pack->array[in_pack->nr].object = object;2741 in_pack->nr++;2742}27432744/*2745 * Compare the objects in the offset order, in order to emulate the2746 * "git rev-list --objects" output that produced the pack originally.2747 */2748static intofscmp(const void*a_,const void*b_)2749{2750struct in_pack_object *a = (struct in_pack_object *)a_;2751struct in_pack_object *b = (struct in_pack_object *)b_;27522753if(a->offset < b->offset)2754return-1;2755else if(a->offset > b->offset)2756return1;2757else2758returnoidcmp(&a->object->oid, &b->object->oid);2759}27602761static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2762{2763struct packed_git *p;2764struct in_pack in_pack;2765uint32_t i;27662767memset(&in_pack,0,sizeof(in_pack));27682769for(p =get_packed_git(the_repository); p; p = p->next) {2770struct object_id oid;2771struct object *o;27722773if(!p->pack_local || p->pack_keep)2774continue;2775if(open_pack_index(p))2776die("cannot open pack index");27772778ALLOC_GROW(in_pack.array,2779 in_pack.nr + p->num_objects,2780 in_pack.alloc);27812782for(i =0; i < p->num_objects; i++) {2783nth_packed_object_oid(&oid, p, i);2784 o =lookup_unknown_object(oid.hash);2785if(!(o->flags & OBJECT_ADDED))2786mark_in_pack_object(o, p, &in_pack);2787 o->flags |= OBJECT_ADDED;2788}2789}27902791if(in_pack.nr) {2792QSORT(in_pack.array, in_pack.nr, ofscmp);2793for(i =0; i < in_pack.nr; i++) {2794struct object *o = in_pack.array[i].object;2795add_object_entry(&o->oid, o->type,"",0);2796}2797}2798free(in_pack.array);2799}28002801static intadd_loose_object(const struct object_id *oid,const char*path,2802void*data)2803{2804enum object_type type =oid_object_info(oid, NULL);28052806if(type <0) {2807warning("loose object at%scould not be examined", path);2808return0;2809}28102811add_object_entry(oid, type,"",0);2812return0;2813}28142815/*2816 * We actually don't even have to worry about reachability here.2817 * add_object_entry will weed out duplicates, so we just add every2818 * loose object we find.2819 */2820static voidadd_unreachable_loose_objects(void)2821{2822for_each_loose_file_in_objdir(get_object_directory(),2823 add_loose_object,2824 NULL, NULL, NULL);2825}28262827static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2828{2829static struct packed_git *last_found = (void*)1;2830struct packed_git *p;28312832 p = (last_found != (void*)1) ? last_found :2833get_packed_git(the_repository);28342835while(p) {2836if((!p->pack_local || p->pack_keep) &&2837find_pack_entry_one(oid->hash, p)) {2838 last_found = p;2839return1;2840}2841if(p == last_found)2842 p =get_packed_git(the_repository);2843else2844 p = p->next;2845if(p == last_found)2846 p = p->next;2847}2848return0;2849}28502851/*2852 * Store a list of sha1s that are should not be discarded2853 * because they are either written too recently, or are2854 * reachable from another object that was.2855 *2856 * This is filled by get_object_list.2857 */2858static struct oid_array recent_objects;28592860static intloosened_object_can_be_discarded(const struct object_id *oid,2861 timestamp_t mtime)2862{2863if(!unpack_unreachable_expiration)2864return0;2865if(mtime > unpack_unreachable_expiration)2866return0;2867if(oid_array_lookup(&recent_objects, oid) >=0)2868return0;2869return1;2870}28712872static voidloosen_unused_packed_objects(struct rev_info *revs)2873{2874struct packed_git *p;2875uint32_t i;2876struct object_id oid;28772878for(p =get_packed_git(the_repository); p; p = p->next) {2879if(!p->pack_local || p->pack_keep)2880continue;28812882if(open_pack_index(p))2883die("cannot open pack index");28842885for(i =0; i < p->num_objects; i++) {2886nth_packed_object_oid(&oid, p, i);2887if(!packlist_find(&to_pack, oid.hash, NULL) &&2888!has_sha1_pack_kept_or_nonlocal(&oid) &&2889!loosened_object_can_be_discarded(&oid, p->mtime))2890if(force_object_loose(&oid, p->mtime))2891die("unable to force loose object");2892}2893}2894}28952896/*2897 * This tracks any options which pack-reuse code expects to be on, or which a2898 * reader of the pack might not understand, and which would therefore prevent2899 * blind reuse of what we have on disk.2900 */2901static intpack_options_allow_reuse(void)2902{2903return pack_to_stdout &&2904 allow_ofs_delta &&2905!ignore_packed_keep &&2906(!local || !have_non_local_packs) &&2907!incremental;2908}29092910static intget_object_list_from_bitmap(struct rev_info *revs)2911{2912if(prepare_bitmap_walk(revs) <0)2913return-1;29142915if(pack_options_allow_reuse() &&2916!reuse_partial_packfile_from_bitmap(2917&reuse_packfile,2918&reuse_packfile_objects,2919&reuse_packfile_offset)) {2920assert(reuse_packfile_objects);2921 nr_result += reuse_packfile_objects;2922display_progress(progress_state, nr_result);2923}29242925traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2926return0;2927}29282929static voidrecord_recent_object(struct object *obj,2930const char*name,2931void*data)2932{2933oid_array_append(&recent_objects, &obj->oid);2934}29352936static voidrecord_recent_commit(struct commit *commit,void*data)2937{2938oid_array_append(&recent_objects, &commit->object.oid);2939}29402941static voidget_object_list(int ac,const char**av)2942{2943struct rev_info revs;2944char line[1000];2945int flags =0;29462947init_revisions(&revs, NULL);2948 save_commit_buffer =0;2949setup_revisions(ac, av, &revs, NULL);29502951/* make sure shallows are read */2952is_repository_shallow();29532954while(fgets(line,sizeof(line), stdin) != NULL) {2955int len =strlen(line);2956if(len && line[len -1] =='\n')2957 line[--len] =0;2958if(!len)2959break;2960if(*line =='-') {2961if(!strcmp(line,"--not")) {2962 flags ^= UNINTERESTING;2963 write_bitmap_index =0;2964continue;2965}2966if(starts_with(line,"--shallow ")) {2967struct object_id oid;2968if(get_oid_hex(line +10, &oid))2969die("not an SHA-1 '%s'", line +10);2970register_shallow(&oid);2971 use_bitmap_index =0;2972continue;2973}2974die("not a rev '%s'", line);2975}2976if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2977die("bad revision '%s'", line);2978}29792980if(use_bitmap_index && !get_object_list_from_bitmap(&revs))2981return;29822983if(prepare_revision_walk(&revs))2984die("revision walk setup failed");2985mark_edges_uninteresting(&revs, show_edge);29862987if(!fn_show_object)2988 fn_show_object = show_object;2989traverse_commit_list_filtered(&filter_options, &revs,2990 show_commit, fn_show_object, NULL,2991 NULL);29922993if(unpack_unreachable_expiration) {2994 revs.ignore_missing_links =1;2995if(add_unseen_recent_objects_to_traversal(&revs,2996 unpack_unreachable_expiration))2997die("unable to add recent objects");2998if(prepare_revision_walk(&revs))2999die("revision walk setup failed");3000traverse_commit_list(&revs, record_recent_commit,3001 record_recent_object, NULL);3002}30033004if(keep_unreachable)3005add_objects_in_unpacked_packs(&revs);3006if(pack_loose_unreachable)3007add_unreachable_loose_objects();3008if(unpack_unreachable)3009loosen_unused_packed_objects(&revs);30103011oid_array_clear(&recent_objects);3012}30133014static intoption_parse_index_version(const struct option *opt,3015const char*arg,int unset)3016{3017char*c;3018const char*val = arg;3019 pack_idx_opts.version =strtoul(val, &c,10);3020if(pack_idx_opts.version >2)3021die(_("unsupported index version%s"), val);3022if(*c ==','&& c[1])3023 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3024if(*c || pack_idx_opts.off32_limit &0x80000000)3025die(_("bad index version '%s'"), val);3026return0;3027}30283029static intoption_parse_unpack_unreachable(const struct option *opt,3030const char*arg,int unset)3031{3032if(unset) {3033 unpack_unreachable =0;3034 unpack_unreachable_expiration =0;3035}3036else{3037 unpack_unreachable =1;3038if(arg)3039 unpack_unreachable_expiration =approxidate(arg);3040}3041return0;3042}30433044intcmd_pack_objects(int argc,const char**argv,const char*prefix)3045{3046int use_internal_rev_list =0;3047int thin =0;3048int shallow =0;3049int all_progress_implied =0;3050struct argv_array rp = ARGV_ARRAY_INIT;3051int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3052int rev_list_index =0;3053struct option pack_objects_options[] = {3054OPT_SET_INT('q',"quiet", &progress,3055N_("do not show progress meter"),0),3056OPT_SET_INT(0,"progress", &progress,3057N_("show progress meter"),1),3058OPT_SET_INT(0,"all-progress", &progress,3059N_("show progress meter during object writing phase"),2),3060OPT_BOOL(0,"all-progress-implied",3061&all_progress_implied,3062N_("similar to --all-progress when progress meter is shown")),3063{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),3064N_("write the pack index file in the specified idx format version"),30650, option_parse_index_version },3066OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3067N_("maximum size of each output pack file")),3068OPT_BOOL(0,"local", &local,3069N_("ignore borrowed objects from alternate object store")),3070OPT_BOOL(0,"incremental", &incremental,3071N_("ignore packed objects")),3072OPT_INTEGER(0,"window", &window,3073N_("limit pack window by objects")),3074OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3075N_("limit pack window by memory in addition to object limit")),3076OPT_INTEGER(0,"depth", &depth,3077N_("maximum length of delta chain allowed in the resulting pack")),3078OPT_BOOL(0,"reuse-delta", &reuse_delta,3079N_("reuse existing deltas")),3080OPT_BOOL(0,"reuse-object", &reuse_object,3081N_("reuse existing objects")),3082OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3083N_("use OFS_DELTA objects")),3084OPT_INTEGER(0,"threads", &delta_search_threads,3085N_("use threads when searching for best delta matches")),3086OPT_BOOL(0,"non-empty", &non_empty,3087N_("do not create an empty pack output")),3088OPT_BOOL(0,"revs", &use_internal_rev_list,3089N_("read revision arguments from standard input")),3090{ OPTION_SET_INT,0,"unpacked", &rev_list_unpacked, NULL,3091N_("limit the objects to those that are not yet packed"),3092 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3093{ OPTION_SET_INT,0,"all", &rev_list_all, NULL,3094N_("include objects reachable from any reference"),3095 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3096{ OPTION_SET_INT,0,"reflog", &rev_list_reflog, NULL,3097N_("include objects referred by reflog entries"),3098 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3099{ OPTION_SET_INT,0,"indexed-objects", &rev_list_index, NULL,3100N_("include objects referred to by the index"),3101 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3102OPT_BOOL(0,"stdout", &pack_to_stdout,3103N_("output pack to stdout")),3104OPT_BOOL(0,"include-tag", &include_tag,3105N_("include tag objects that refer to objects to be packed")),3106OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3107N_("keep unreachable objects")),3108OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3109N_("pack loose unreachable objects")),3110{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3111N_("unpack unreachable objects newer than <time>"),3112 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3113OPT_BOOL(0,"thin", &thin,3114N_("create thin packs")),3115OPT_BOOL(0,"shallow", &shallow,3116N_("create packs suitable for shallow fetches")),3117OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep,3118N_("ignore packs that have companion .keep file")),3119OPT_INTEGER(0,"compression", &pack_compression_level,3120N_("pack compression level")),3121OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3122N_("do not hide commits by grafts"),0),3123OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3124N_("use a bitmap index if available to speed up counting objects")),3125OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3126N_("write a bitmap index together with the pack index")),3127OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3128{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3129N_("handling for missing objects"), PARSE_OPT_NONEG,3130 option_parse_missing_action },3131OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3132N_("do not pack objects in promisor packfiles")),3133OPT_END(),3134};31353136if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3137BUG("too many dfs states, increase OE_DFS_STATE_BITS");31383139 check_replace_refs =0;31403141reset_pack_idx_option(&pack_idx_opts);3142git_config(git_pack_config, NULL);31433144 progress =isatty(2);3145 argc =parse_options(argc, argv, prefix, pack_objects_options,3146 pack_usage,0);31473148if(argc) {3149 base_name = argv[0];3150 argc--;3151}3152if(pack_to_stdout != !base_name || argc)3153usage_with_options(pack_usage, pack_objects_options);31543155if(depth >= (1<< OE_DEPTH_BITS)) {3156warning(_("delta chain depth%dis too deep, forcing%d"),3157 depth, (1<< OE_DEPTH_BITS) -1);3158 depth = (1<< OE_DEPTH_BITS) -1;3159}3160if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3161warning(_("pack.deltaCacheLimit is too high, forcing%d"),3162(1U<< OE_Z_DELTA_BITS) -1);3163 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3164}31653166argv_array_push(&rp,"pack-objects");3167if(thin) {3168 use_internal_rev_list =1;3169argv_array_push(&rp, shallow3170?"--objects-edge-aggressive"3171:"--objects-edge");3172}else3173argv_array_push(&rp,"--objects");31743175if(rev_list_all) {3176 use_internal_rev_list =1;3177argv_array_push(&rp,"--all");3178}3179if(rev_list_reflog) {3180 use_internal_rev_list =1;3181argv_array_push(&rp,"--reflog");3182}3183if(rev_list_index) {3184 use_internal_rev_list =1;3185argv_array_push(&rp,"--indexed-objects");3186}3187if(rev_list_unpacked) {3188 use_internal_rev_list =1;3189argv_array_push(&rp,"--unpacked");3190}31913192if(exclude_promisor_objects) {3193 use_internal_rev_list =1;3194 fetch_if_missing =0;3195argv_array_push(&rp,"--exclude-promisor-objects");3196}31973198if(!reuse_object)3199 reuse_delta =0;3200if(pack_compression_level == -1)3201 pack_compression_level = Z_DEFAULT_COMPRESSION;3202else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3203die("bad pack compression level%d", pack_compression_level);32043205if(!delta_search_threads)/* --threads=0 means autodetect */3206 delta_search_threads =online_cpus();32073208#ifdef NO_PTHREADS3209if(delta_search_threads !=1)3210warning("no threads support, ignoring --threads");3211#endif3212if(!pack_to_stdout && !pack_size_limit)3213 pack_size_limit = pack_size_limit_cfg;3214if(pack_to_stdout && pack_size_limit)3215die("--max-pack-size cannot be used to build a pack for transfer.");3216if(pack_size_limit && pack_size_limit <1024*1024) {3217warning("minimum pack size limit is 1 MiB");3218 pack_size_limit =1024*1024;3219}32203221if(!pack_to_stdout && thin)3222die("--thin cannot be used to build an indexable pack.");32233224if(keep_unreachable && unpack_unreachable)3225die("--keep-unreachable and --unpack-unreachable are incompatible.");3226if(!rev_list_all || !rev_list_reflog || !rev_list_index)3227 unpack_unreachable_expiration =0;32283229if(filter_options.choice) {3230if(!pack_to_stdout)3231die("cannot use --filter without --stdout.");3232 use_bitmap_index =0;3233}32343235/*3236 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3237 *3238 * - to produce good pack (with bitmap index not-yet-packed objects are3239 * packed in suboptimal order).3240 *3241 * - to use more robust pack-generation codepath (avoiding possible3242 * bugs in bitmap code and possible bitmap index corruption).3243 */3244if(!pack_to_stdout)3245 use_bitmap_index_default =0;32463247if(use_bitmap_index <0)3248 use_bitmap_index = use_bitmap_index_default;32493250/* "hard" reasons not to use bitmaps; these just won't work at all */3251if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3252 use_bitmap_index =0;32533254if(pack_to_stdout || !rev_list_all)3255 write_bitmap_index =0;32563257if(progress && all_progress_implied)3258 progress =2;32593260if(ignore_packed_keep) {3261struct packed_git *p;3262for(p =get_packed_git(the_repository); p; p = p->next)3263if(p->pack_local && p->pack_keep)3264break;3265if(!p)/* no keep-able packs found */3266 ignore_packed_keep =0;3267}3268if(local) {3269/*3270 * unlike ignore_packed_keep above, we do not want to3271 * unset "local" based on looking at packs, as it3272 * also covers non-local objects3273 */3274struct packed_git *p;3275for(p =get_packed_git(the_repository); p; p = p->next) {3276if(!p->pack_local) {3277 have_non_local_packs =1;3278break;3279}3280}3281}32823283prepare_packing_data(&to_pack);32843285if(progress)3286 progress_state =start_progress(_("Counting objects"),0);3287if(!use_internal_rev_list)3288read_object_list_from_stdin();3289else{3290get_object_list(rp.argc, rp.argv);3291argv_array_clear(&rp);3292}3293cleanup_preferred_base();3294if(include_tag && nr_result)3295for_each_ref(add_ref_tag, NULL);3296stop_progress(&progress_state);32973298if(non_empty && !nr_result)3299return0;3300if(nr_result)3301prepare_pack(window, depth);3302write_pack_file();3303if(progress)3304fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3305" reused %"PRIu32" (delta %"PRIu32")\n",3306 written, written_delta, reused, reused_delta);3307return0;3308}