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;2011if(delta_size >= (1U<< OE_DELTA_SIZE_BITS)) {2012free(delta_buf);2013return0;2014}20152016if(DELTA(trg_entry)) {2017/* Prefer only shallower same-sized deltas. */2018if(delta_size ==DELTA_SIZE(trg_entry) &&2019 src->depth +1>= trg->depth) {2020free(delta_buf);2021return0;2022}2023}20242025/*2026 * Handle memory allocation outside of the cache2027 * accounting lock. Compiler will optimize the strangeness2028 * away when NO_PTHREADS is defined.2029 */2030free(trg_entry->delta_data);2031cache_lock();2032if(trg_entry->delta_data) {2033 delta_cache_size -=DELTA_SIZE(trg_entry);2034 trg_entry->delta_data = NULL;2035}2036if(delta_cacheable(src_size, trg_size, delta_size)) {2037 delta_cache_size += delta_size;2038cache_unlock();2039 trg_entry->delta_data =xrealloc(delta_buf, delta_size);2040}else{2041cache_unlock();2042free(delta_buf);2043}20442045SET_DELTA(trg_entry, src_entry);2046SET_DELTA_SIZE(trg_entry, delta_size);2047 trg->depth = src->depth +1;20482049return1;2050}20512052static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)2053{2054struct object_entry *child =DELTA_CHILD(me);2055unsigned int m = n;2056while(child) {2057unsigned int c =check_delta_limit(child, n +1);2058if(m < c)2059 m = c;2060 child =DELTA_SIBLING(child);2061}2062return m;2063}20642065static unsigned longfree_unpacked(struct unpacked *n)2066{2067unsigned long freed_mem =sizeof_delta_index(n->index);2068free_delta_index(n->index);2069 n->index = NULL;2070if(n->data) {2071 freed_mem +=SIZE(n->entry);2072FREE_AND_NULL(n->data);2073}2074 n->entry = NULL;2075 n->depth =0;2076return freed_mem;2077}20782079static voidfind_deltas(struct object_entry **list,unsigned*list_size,2080int window,int depth,unsigned*processed)2081{2082uint32_t i, idx =0, count =0;2083struct unpacked *array;2084unsigned long mem_usage =0;20852086 array =xcalloc(window,sizeof(struct unpacked));20872088for(;;) {2089struct object_entry *entry;2090struct unpacked *n = array + idx;2091int j, max_depth, best_base = -1;20922093progress_lock();2094if(!*list_size) {2095progress_unlock();2096break;2097}2098 entry = *list++;2099(*list_size)--;2100if(!entry->preferred_base) {2101(*processed)++;2102display_progress(progress_state, *processed);2103}2104progress_unlock();21052106 mem_usage -=free_unpacked(n);2107 n->entry = entry;21082109while(window_memory_limit &&2110 mem_usage > window_memory_limit &&2111 count >1) {2112uint32_t tail = (idx + window - count) % window;2113 mem_usage -=free_unpacked(array + tail);2114 count--;2115}21162117/* We do not compute delta to *create* objects we are not2118 * going to pack.2119 */2120if(entry->preferred_base)2121goto next;21222123/*2124 * If the current object is at pack edge, take the depth the2125 * objects that depend on the current object into account2126 * otherwise they would become too deep.2127 */2128 max_depth = depth;2129if(DELTA_CHILD(entry)) {2130 max_depth -=check_delta_limit(entry,0);2131if(max_depth <=0)2132goto next;2133}21342135 j = window;2136while(--j >0) {2137int ret;2138uint32_t other_idx = idx + j;2139struct unpacked *m;2140if(other_idx >= window)2141 other_idx -= window;2142 m = array + other_idx;2143if(!m->entry)2144break;2145 ret =try_delta(n, m, max_depth, &mem_usage);2146if(ret <0)2147break;2148else if(ret >0)2149 best_base = other_idx;2150}21512152/*2153 * If we decided to cache the delta data, then it is best2154 * to compress it right away. First because we have to do2155 * it anyway, and doing it here while we're threaded will2156 * save a lot of time in the non threaded write phase,2157 * as well as allow for caching more deltas within2158 * the same cache size limit.2159 * ...2160 * But only if not writing to stdout, since in that case2161 * the network is most likely throttling writes anyway,2162 * and therefore it is best to go to the write phase ASAP2163 * instead, as we can afford spending more time compressing2164 * between writes at that moment.2165 */2166if(entry->delta_data && !pack_to_stdout) {2167unsigned long size;21682169 size =do_compress(&entry->delta_data,DELTA_SIZE(entry));2170if(size < (1U<< OE_Z_DELTA_BITS)) {2171 entry->z_delta_size = size;2172cache_lock();2173 delta_cache_size -=DELTA_SIZE(entry);2174 delta_cache_size += entry->z_delta_size;2175cache_unlock();2176}else{2177FREE_AND_NULL(entry->delta_data);2178 entry->z_delta_size =0;2179}2180}21812182/* if we made n a delta, and if n is already at max2183 * depth, leaving it in the window is pointless. we2184 * should evict it first.2185 */2186if(DELTA(entry) && max_depth <= n->depth)2187continue;21882189/*2190 * Move the best delta base up in the window, after the2191 * currently deltified object, to keep it longer. It will2192 * be the first base object to be attempted next.2193 */2194if(DELTA(entry)) {2195struct unpacked swap = array[best_base];2196int dist = (window + idx - best_base) % window;2197int dst = best_base;2198while(dist--) {2199int src = (dst +1) % window;2200 array[dst] = array[src];2201 dst = src;2202}2203 array[dst] = swap;2204}22052206 next:2207 idx++;2208if(count +1< window)2209 count++;2210if(idx >= window)2211 idx =0;2212}22132214for(i =0; i < window; ++i) {2215free_delta_index(array[i].index);2216free(array[i].data);2217}2218free(array);2219}22202221#ifndef NO_PTHREADS22222223static voidtry_to_free_from_threads(size_t size)2224{2225read_lock();2226release_pack_memory(size);2227read_unlock();2228}22292230static try_to_free_t old_try_to_free_routine;22312232/*2233 * The main thread waits on the condition that (at least) one of the workers2234 * has stopped working (which is indicated in the .working member of2235 * struct thread_params).2236 * When a work thread has completed its work, it sets .working to 0 and2237 * signals the main thread and waits on the condition that .data_ready2238 * becomes 1.2239 */22402241struct thread_params {2242 pthread_t thread;2243struct object_entry **list;2244unsigned list_size;2245unsigned remaining;2246int window;2247int depth;2248int working;2249int data_ready;2250 pthread_mutex_t mutex;2251 pthread_cond_t cond;2252unsigned*processed;2253};22542255static pthread_cond_t progress_cond;22562257/*2258 * Mutex and conditional variable can't be statically-initialized on Windows.2259 */2260static voidinit_threaded_search(void)2261{2262init_recursive_mutex(&read_mutex);2263pthread_mutex_init(&cache_mutex, NULL);2264pthread_mutex_init(&progress_mutex, NULL);2265pthread_cond_init(&progress_cond, NULL);2266 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2267}22682269static voidcleanup_threaded_search(void)2270{2271set_try_to_free_routine(old_try_to_free_routine);2272pthread_cond_destroy(&progress_cond);2273pthread_mutex_destroy(&read_mutex);2274pthread_mutex_destroy(&cache_mutex);2275pthread_mutex_destroy(&progress_mutex);2276}22772278static void*threaded_find_deltas(void*arg)2279{2280struct thread_params *me = arg;22812282progress_lock();2283while(me->remaining) {2284progress_unlock();22852286find_deltas(me->list, &me->remaining,2287 me->window, me->depth, me->processed);22882289progress_lock();2290 me->working =0;2291pthread_cond_signal(&progress_cond);2292progress_unlock();22932294/*2295 * We must not set ->data_ready before we wait on the2296 * condition because the main thread may have set it to 12297 * before we get here. In order to be sure that new2298 * work is available if we see 1 in ->data_ready, it2299 * was initialized to 0 before this thread was spawned2300 * and we reset it to 0 right away.2301 */2302pthread_mutex_lock(&me->mutex);2303while(!me->data_ready)2304pthread_cond_wait(&me->cond, &me->mutex);2305 me->data_ready =0;2306pthread_mutex_unlock(&me->mutex);23072308progress_lock();2309}2310progress_unlock();2311/* leave ->working 1 so that this doesn't get more work assigned */2312return NULL;2313}23142315static voidll_find_deltas(struct object_entry **list,unsigned list_size,2316int window,int depth,unsigned*processed)2317{2318struct thread_params *p;2319int i, ret, active_threads =0;23202321init_threaded_search();23222323if(delta_search_threads <=1) {2324find_deltas(list, &list_size, window, depth, processed);2325cleanup_threaded_search();2326return;2327}2328if(progress > pack_to_stdout)2329fprintf(stderr,"Delta compression using up to%dthreads.\n",2330 delta_search_threads);2331 p =xcalloc(delta_search_threads,sizeof(*p));23322333/* Partition the work amongst work threads. */2334for(i =0; i < delta_search_threads; i++) {2335unsigned sub_size = list_size / (delta_search_threads - i);23362337/* don't use too small segments or no deltas will be found */2338if(sub_size <2*window && i+1< delta_search_threads)2339 sub_size =0;23402341 p[i].window = window;2342 p[i].depth = depth;2343 p[i].processed = processed;2344 p[i].working =1;2345 p[i].data_ready =0;23462347/* try to split chunks on "path" boundaries */2348while(sub_size && sub_size < list_size &&2349 list[sub_size]->hash &&2350 list[sub_size]->hash == list[sub_size-1]->hash)2351 sub_size++;23522353 p[i].list = list;2354 p[i].list_size = sub_size;2355 p[i].remaining = sub_size;23562357 list += sub_size;2358 list_size -= sub_size;2359}23602361/* Start work threads. */2362for(i =0; i < delta_search_threads; i++) {2363if(!p[i].list_size)2364continue;2365pthread_mutex_init(&p[i].mutex, NULL);2366pthread_cond_init(&p[i].cond, NULL);2367 ret =pthread_create(&p[i].thread, NULL,2368 threaded_find_deltas, &p[i]);2369if(ret)2370die("unable to create thread:%s",strerror(ret));2371 active_threads++;2372}23732374/*2375 * Now let's wait for work completion. Each time a thread is done2376 * with its work, we steal half of the remaining work from the2377 * thread with the largest number of unprocessed objects and give2378 * it to that newly idle thread. This ensure good load balancing2379 * until the remaining object list segments are simply too short2380 * to be worth splitting anymore.2381 */2382while(active_threads) {2383struct thread_params *target = NULL;2384struct thread_params *victim = NULL;2385unsigned sub_size =0;23862387progress_lock();2388for(;;) {2389for(i =0; !target && i < delta_search_threads; i++)2390if(!p[i].working)2391 target = &p[i];2392if(target)2393break;2394pthread_cond_wait(&progress_cond, &progress_mutex);2395}23962397for(i =0; i < delta_search_threads; i++)2398if(p[i].remaining >2*window &&2399(!victim || victim->remaining < p[i].remaining))2400 victim = &p[i];2401if(victim) {2402 sub_size = victim->remaining /2;2403 list = victim->list + victim->list_size - sub_size;2404while(sub_size && list[0]->hash &&2405 list[0]->hash == list[-1]->hash) {2406 list++;2407 sub_size--;2408}2409if(!sub_size) {2410/*2411 * It is possible for some "paths" to have2412 * so many objects that no hash boundary2413 * might be found. Let's just steal the2414 * exact half in that case.2415 */2416 sub_size = victim->remaining /2;2417 list -= sub_size;2418}2419 target->list = list;2420 victim->list_size -= sub_size;2421 victim->remaining -= sub_size;2422}2423 target->list_size = sub_size;2424 target->remaining = sub_size;2425 target->working =1;2426progress_unlock();24272428pthread_mutex_lock(&target->mutex);2429 target->data_ready =1;2430pthread_cond_signal(&target->cond);2431pthread_mutex_unlock(&target->mutex);24322433if(!sub_size) {2434pthread_join(target->thread, NULL);2435pthread_cond_destroy(&target->cond);2436pthread_mutex_destroy(&target->mutex);2437 active_threads--;2438}2439}2440cleanup_threaded_search();2441free(p);2442}24432444#else2445#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2446#endif24472448static voidadd_tag_chain(const struct object_id *oid)2449{2450struct tag *tag;24512452/*2453 * We catch duplicates already in add_object_entry(), but we'd2454 * prefer to do this extra check to avoid having to parse the2455 * tag at all if we already know that it's being packed (e.g., if2456 * it was included via bitmaps, we would not have parsed it2457 * previously).2458 */2459if(packlist_find(&to_pack, oid->hash, NULL))2460return;24612462 tag =lookup_tag(oid);2463while(1) {2464if(!tag ||parse_tag(tag) || !tag->tagged)2465die("unable to pack objects reachable from tag%s",2466oid_to_hex(oid));24672468add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);24692470if(tag->tagged->type != OBJ_TAG)2471return;24722473 tag = (struct tag *)tag->tagged;2474}2475}24762477static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2478{2479struct object_id peeled;24802481if(starts_with(path,"refs/tags/") &&/* is a tag? */2482!peel_ref(path, &peeled) &&/* peelable? */2483packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2484add_tag_chain(oid);2485return0;2486}24872488static voidprepare_pack(int window,int depth)2489{2490struct object_entry **delta_list;2491uint32_t i, nr_deltas;2492unsigned n;24932494get_object_details();24952496/*2497 * If we're locally repacking then we need to be doubly careful2498 * from now on in order to make sure no stealth corruption gets2499 * propagated to the new pack. Clients receiving streamed packs2500 * should validate everything they get anyway so no need to incur2501 * the additional cost here in that case.2502 */2503if(!pack_to_stdout)2504 do_check_packed_object_crc =1;25052506if(!to_pack.nr_objects || !window || !depth)2507return;25082509ALLOC_ARRAY(delta_list, to_pack.nr_objects);2510 nr_deltas = n =0;25112512for(i =0; i < to_pack.nr_objects; i++) {2513struct object_entry *entry = to_pack.objects + i;25142515if(DELTA(entry))2516/* This happens if we decided to reuse existing2517 * delta from a pack. "reuse_delta &&" is implied.2518 */2519continue;25202521if(!entry->type_valid ||2522oe_size_less_than(&to_pack, entry,50))2523continue;25242525if(entry->no_try_delta)2526continue;25272528if(!entry->preferred_base) {2529 nr_deltas++;2530if(oe_type(entry) <0)2531die("unable to get type of object%s",2532oid_to_hex(&entry->idx.oid));2533}else{2534if(oe_type(entry) <0) {2535/*2536 * This object is not found, but we2537 * don't have to include it anyway.2538 */2539continue;2540}2541}25422543 delta_list[n++] = entry;2544}25452546if(nr_deltas && n >1) {2547unsigned nr_done =0;2548if(progress)2549 progress_state =start_progress(_("Compressing objects"),2550 nr_deltas);2551QSORT(delta_list, n, type_size_sort);2552ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2553stop_progress(&progress_state);2554if(nr_done != nr_deltas)2555die("inconsistency with delta count");2556}2557free(delta_list);2558}25592560static intgit_pack_config(const char*k,const char*v,void*cb)2561{2562if(!strcmp(k,"pack.window")) {2563 window =git_config_int(k, v);2564return0;2565}2566if(!strcmp(k,"pack.windowmemory")) {2567 window_memory_limit =git_config_ulong(k, v);2568return0;2569}2570if(!strcmp(k,"pack.depth")) {2571 depth =git_config_int(k, v);2572return0;2573}2574if(!strcmp(k,"pack.deltacachesize")) {2575 max_delta_cache_size =git_config_int(k, v);2576return0;2577}2578if(!strcmp(k,"pack.deltacachelimit")) {2579 cache_max_small_delta_size =git_config_int(k, v);2580return0;2581}2582if(!strcmp(k,"pack.writebitmaphashcache")) {2583if(git_config_bool(k, v))2584 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2585else2586 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2587}2588if(!strcmp(k,"pack.usebitmaps")) {2589 use_bitmap_index_default =git_config_bool(k, v);2590return0;2591}2592if(!strcmp(k,"pack.threads")) {2593 delta_search_threads =git_config_int(k, v);2594if(delta_search_threads <0)2595die("invalid number of threads specified (%d)",2596 delta_search_threads);2597#ifdef NO_PTHREADS2598if(delta_search_threads !=1) {2599warning("no threads support, ignoring%s", k);2600 delta_search_threads =0;2601}2602#endif2603return0;2604}2605if(!strcmp(k,"pack.indexversion")) {2606 pack_idx_opts.version =git_config_int(k, v);2607if(pack_idx_opts.version >2)2608die("bad pack.indexversion=%"PRIu32,2609 pack_idx_opts.version);2610return0;2611}2612returngit_default_config(k, v, cb);2613}26142615static voidread_object_list_from_stdin(void)2616{2617char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2618struct object_id oid;2619const char*p;26202621for(;;) {2622if(!fgets(line,sizeof(line), stdin)) {2623if(feof(stdin))2624break;2625if(!ferror(stdin))2626die("fgets returned NULL, not EOF, not error!");2627if(errno != EINTR)2628die_errno("fgets");2629clearerr(stdin);2630continue;2631}2632if(line[0] =='-') {2633if(get_oid_hex(line+1, &oid))2634die("expected edge object ID, got garbage:\n%s",2635 line);2636add_preferred_base(&oid);2637continue;2638}2639if(parse_oid_hex(line, &oid, &p))2640die("expected object ID, got garbage:\n%s", line);26412642add_preferred_base_object(p +1);2643add_object_entry(&oid, OBJ_NONE, p +1,0);2644}2645}26462647/* Remember to update object flag allocation in object.h */2648#define OBJECT_ADDED (1u<<20)26492650static voidshow_commit(struct commit *commit,void*data)2651{2652add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2653 commit->object.flags |= OBJECT_ADDED;26542655if(write_bitmap_index)2656index_commit_for_bitmap(commit);2657}26582659static voidshow_object(struct object *obj,const char*name,void*data)2660{2661add_preferred_base_object(name);2662add_object_entry(&obj->oid, obj->type, name,0);2663 obj->flags |= OBJECT_ADDED;2664}26652666static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2667{2668assert(arg_missing_action == MA_ALLOW_ANY);26692670/*2671 * Quietly ignore ALL missing objects. This avoids problems with2672 * staging them now and getting an odd error later.2673 */2674if(!has_object_file(&obj->oid))2675return;26762677show_object(obj, name, data);2678}26792680static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2681{2682assert(arg_missing_action == MA_ALLOW_PROMISOR);26832684/*2685 * Quietly ignore EXPECTED missing objects. This avoids problems with2686 * staging them now and getting an odd error later.2687 */2688if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2689return;26902691show_object(obj, name, data);2692}26932694static intoption_parse_missing_action(const struct option *opt,2695const char*arg,int unset)2696{2697assert(arg);2698assert(!unset);26992700if(!strcmp(arg,"error")) {2701 arg_missing_action = MA_ERROR;2702 fn_show_object = show_object;2703return0;2704}27052706if(!strcmp(arg,"allow-any")) {2707 arg_missing_action = MA_ALLOW_ANY;2708 fetch_if_missing =0;2709 fn_show_object = show_object__ma_allow_any;2710return0;2711}27122713if(!strcmp(arg,"allow-promisor")) {2714 arg_missing_action = MA_ALLOW_PROMISOR;2715 fetch_if_missing =0;2716 fn_show_object = show_object__ma_allow_promisor;2717return0;2718}27192720die(_("invalid value for --missing"));2721return0;2722}27232724static voidshow_edge(struct commit *commit)2725{2726add_preferred_base(&commit->object.oid);2727}27282729struct in_pack_object {2730 off_t offset;2731struct object *object;2732};27332734struct in_pack {2735unsigned int alloc;2736unsigned int nr;2737struct in_pack_object *array;2738};27392740static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2741{2742 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2743 in_pack->array[in_pack->nr].object = object;2744 in_pack->nr++;2745}27462747/*2748 * Compare the objects in the offset order, in order to emulate the2749 * "git rev-list --objects" output that produced the pack originally.2750 */2751static intofscmp(const void*a_,const void*b_)2752{2753struct in_pack_object *a = (struct in_pack_object *)a_;2754struct in_pack_object *b = (struct in_pack_object *)b_;27552756if(a->offset < b->offset)2757return-1;2758else if(a->offset > b->offset)2759return1;2760else2761returnoidcmp(&a->object->oid, &b->object->oid);2762}27632764static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2765{2766struct packed_git *p;2767struct in_pack in_pack;2768uint32_t i;27692770memset(&in_pack,0,sizeof(in_pack));27712772for(p =get_packed_git(the_repository); p; p = p->next) {2773struct object_id oid;2774struct object *o;27752776if(!p->pack_local || p->pack_keep)2777continue;2778if(open_pack_index(p))2779die("cannot open pack index");27802781ALLOC_GROW(in_pack.array,2782 in_pack.nr + p->num_objects,2783 in_pack.alloc);27842785for(i =0; i < p->num_objects; i++) {2786nth_packed_object_oid(&oid, p, i);2787 o =lookup_unknown_object(oid.hash);2788if(!(o->flags & OBJECT_ADDED))2789mark_in_pack_object(o, p, &in_pack);2790 o->flags |= OBJECT_ADDED;2791}2792}27932794if(in_pack.nr) {2795QSORT(in_pack.array, in_pack.nr, ofscmp);2796for(i =0; i < in_pack.nr; i++) {2797struct object *o = in_pack.array[i].object;2798add_object_entry(&o->oid, o->type,"",0);2799}2800}2801free(in_pack.array);2802}28032804static intadd_loose_object(const struct object_id *oid,const char*path,2805void*data)2806{2807enum object_type type =oid_object_info(oid, NULL);28082809if(type <0) {2810warning("loose object at%scould not be examined", path);2811return0;2812}28132814add_object_entry(oid, type,"",0);2815return0;2816}28172818/*2819 * We actually don't even have to worry about reachability here.2820 * add_object_entry will weed out duplicates, so we just add every2821 * loose object we find.2822 */2823static voidadd_unreachable_loose_objects(void)2824{2825for_each_loose_file_in_objdir(get_object_directory(),2826 add_loose_object,2827 NULL, NULL, NULL);2828}28292830static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2831{2832static struct packed_git *last_found = (void*)1;2833struct packed_git *p;28342835 p = (last_found != (void*)1) ? last_found :2836get_packed_git(the_repository);28372838while(p) {2839if((!p->pack_local || p->pack_keep) &&2840find_pack_entry_one(oid->hash, p)) {2841 last_found = p;2842return1;2843}2844if(p == last_found)2845 p =get_packed_git(the_repository);2846else2847 p = p->next;2848if(p == last_found)2849 p = p->next;2850}2851return0;2852}28532854/*2855 * Store a list of sha1s that are should not be discarded2856 * because they are either written too recently, or are2857 * reachable from another object that was.2858 *2859 * This is filled by get_object_list.2860 */2861static struct oid_array recent_objects;28622863static intloosened_object_can_be_discarded(const struct object_id *oid,2864 timestamp_t mtime)2865{2866if(!unpack_unreachable_expiration)2867return0;2868if(mtime > unpack_unreachable_expiration)2869return0;2870if(oid_array_lookup(&recent_objects, oid) >=0)2871return0;2872return1;2873}28742875static voidloosen_unused_packed_objects(struct rev_info *revs)2876{2877struct packed_git *p;2878uint32_t i;2879struct object_id oid;28802881for(p =get_packed_git(the_repository); p; p = p->next) {2882if(!p->pack_local || p->pack_keep)2883continue;28842885if(open_pack_index(p))2886die("cannot open pack index");28872888for(i =0; i < p->num_objects; i++) {2889nth_packed_object_oid(&oid, p, i);2890if(!packlist_find(&to_pack, oid.hash, NULL) &&2891!has_sha1_pack_kept_or_nonlocal(&oid) &&2892!loosened_object_can_be_discarded(&oid, p->mtime))2893if(force_object_loose(&oid, p->mtime))2894die("unable to force loose object");2895}2896}2897}28982899/*2900 * This tracks any options which pack-reuse code expects to be on, or which a2901 * reader of the pack might not understand, and which would therefore prevent2902 * blind reuse of what we have on disk.2903 */2904static intpack_options_allow_reuse(void)2905{2906return pack_to_stdout &&2907 allow_ofs_delta &&2908!ignore_packed_keep &&2909(!local || !have_non_local_packs) &&2910!incremental;2911}29122913static intget_object_list_from_bitmap(struct rev_info *revs)2914{2915if(prepare_bitmap_walk(revs) <0)2916return-1;29172918if(pack_options_allow_reuse() &&2919!reuse_partial_packfile_from_bitmap(2920&reuse_packfile,2921&reuse_packfile_objects,2922&reuse_packfile_offset)) {2923assert(reuse_packfile_objects);2924 nr_result += reuse_packfile_objects;2925display_progress(progress_state, nr_result);2926}29272928traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2929return0;2930}29312932static voidrecord_recent_object(struct object *obj,2933const char*name,2934void*data)2935{2936oid_array_append(&recent_objects, &obj->oid);2937}29382939static voidrecord_recent_commit(struct commit *commit,void*data)2940{2941oid_array_append(&recent_objects, &commit->object.oid);2942}29432944static voidget_object_list(int ac,const char**av)2945{2946struct rev_info revs;2947char line[1000];2948int flags =0;29492950init_revisions(&revs, NULL);2951 save_commit_buffer =0;2952setup_revisions(ac, av, &revs, NULL);29532954/* make sure shallows are read */2955is_repository_shallow();29562957while(fgets(line,sizeof(line), stdin) != NULL) {2958int len =strlen(line);2959if(len && line[len -1] =='\n')2960 line[--len] =0;2961if(!len)2962break;2963if(*line =='-') {2964if(!strcmp(line,"--not")) {2965 flags ^= UNINTERESTING;2966 write_bitmap_index =0;2967continue;2968}2969if(starts_with(line,"--shallow ")) {2970struct object_id oid;2971if(get_oid_hex(line +10, &oid))2972die("not an SHA-1 '%s'", line +10);2973register_shallow(&oid);2974 use_bitmap_index =0;2975continue;2976}2977die("not a rev '%s'", line);2978}2979if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2980die("bad revision '%s'", line);2981}29822983if(use_bitmap_index && !get_object_list_from_bitmap(&revs))2984return;29852986if(prepare_revision_walk(&revs))2987die("revision walk setup failed");2988mark_edges_uninteresting(&revs, show_edge);29892990if(!fn_show_object)2991 fn_show_object = show_object;2992traverse_commit_list_filtered(&filter_options, &revs,2993 show_commit, fn_show_object, NULL,2994 NULL);29952996if(unpack_unreachable_expiration) {2997 revs.ignore_missing_links =1;2998if(add_unseen_recent_objects_to_traversal(&revs,2999 unpack_unreachable_expiration))3000die("unable to add recent objects");3001if(prepare_revision_walk(&revs))3002die("revision walk setup failed");3003traverse_commit_list(&revs, record_recent_commit,3004 record_recent_object, NULL);3005}30063007if(keep_unreachable)3008add_objects_in_unpacked_packs(&revs);3009if(pack_loose_unreachable)3010add_unreachable_loose_objects();3011if(unpack_unreachable)3012loosen_unused_packed_objects(&revs);30133014oid_array_clear(&recent_objects);3015}30163017static intoption_parse_index_version(const struct option *opt,3018const char*arg,int unset)3019{3020char*c;3021const char*val = arg;3022 pack_idx_opts.version =strtoul(val, &c,10);3023if(pack_idx_opts.version >2)3024die(_("unsupported index version%s"), val);3025if(*c ==','&& c[1])3026 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);3027if(*c || pack_idx_opts.off32_limit &0x80000000)3028die(_("bad index version '%s'"), val);3029return0;3030}30313032static intoption_parse_unpack_unreachable(const struct option *opt,3033const char*arg,int unset)3034{3035if(unset) {3036 unpack_unreachable =0;3037 unpack_unreachable_expiration =0;3038}3039else{3040 unpack_unreachable =1;3041if(arg)3042 unpack_unreachable_expiration =approxidate(arg);3043}3044return0;3045}30463047intcmd_pack_objects(int argc,const char**argv,const char*prefix)3048{3049int use_internal_rev_list =0;3050int thin =0;3051int shallow =0;3052int all_progress_implied =0;3053struct argv_array rp = ARGV_ARRAY_INIT;3054int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;3055int rev_list_index =0;3056struct option pack_objects_options[] = {3057OPT_SET_INT('q',"quiet", &progress,3058N_("do not show progress meter"),0),3059OPT_SET_INT(0,"progress", &progress,3060N_("show progress meter"),1),3061OPT_SET_INT(0,"all-progress", &progress,3062N_("show progress meter during object writing phase"),2),3063OPT_BOOL(0,"all-progress-implied",3064&all_progress_implied,3065N_("similar to --all-progress when progress meter is shown")),3066{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),3067N_("write the pack index file in the specified idx format version"),30680, option_parse_index_version },3069OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3070N_("maximum size of each output pack file")),3071OPT_BOOL(0,"local", &local,3072N_("ignore borrowed objects from alternate object store")),3073OPT_BOOL(0,"incremental", &incremental,3074N_("ignore packed objects")),3075OPT_INTEGER(0,"window", &window,3076N_("limit pack window by objects")),3077OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3078N_("limit pack window by memory in addition to object limit")),3079OPT_INTEGER(0,"depth", &depth,3080N_("maximum length of delta chain allowed in the resulting pack")),3081OPT_BOOL(0,"reuse-delta", &reuse_delta,3082N_("reuse existing deltas")),3083OPT_BOOL(0,"reuse-object", &reuse_object,3084N_("reuse existing objects")),3085OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3086N_("use OFS_DELTA objects")),3087OPT_INTEGER(0,"threads", &delta_search_threads,3088N_("use threads when searching for best delta matches")),3089OPT_BOOL(0,"non-empty", &non_empty,3090N_("do not create an empty pack output")),3091OPT_BOOL(0,"revs", &use_internal_rev_list,3092N_("read revision arguments from standard input")),3093{ OPTION_SET_INT,0,"unpacked", &rev_list_unpacked, NULL,3094N_("limit the objects to those that are not yet packed"),3095 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3096{ OPTION_SET_INT,0,"all", &rev_list_all, NULL,3097N_("include objects reachable from any reference"),3098 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3099{ OPTION_SET_INT,0,"reflog", &rev_list_reflog, NULL,3100N_("include objects referred by reflog entries"),3101 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3102{ OPTION_SET_INT,0,"indexed-objects", &rev_list_index, NULL,3103N_("include objects referred to by the index"),3104 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3105OPT_BOOL(0,"stdout", &pack_to_stdout,3106N_("output pack to stdout")),3107OPT_BOOL(0,"include-tag", &include_tag,3108N_("include tag objects that refer to objects to be packed")),3109OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3110N_("keep unreachable objects")),3111OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3112N_("pack loose unreachable objects")),3113{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3114N_("unpack unreachable objects newer than <time>"),3115 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3116OPT_BOOL(0,"thin", &thin,3117N_("create thin packs")),3118OPT_BOOL(0,"shallow", &shallow,3119N_("create packs suitable for shallow fetches")),3120OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep,3121N_("ignore packs that have companion .keep file")),3122OPT_INTEGER(0,"compression", &pack_compression_level,3123N_("pack compression level")),3124OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3125N_("do not hide commits by grafts"),0),3126OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3127N_("use a bitmap index if available to speed up counting objects")),3128OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3129N_("write a bitmap index together with the pack index")),3130OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3131{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3132N_("handling for missing objects"), PARSE_OPT_NONEG,3133 option_parse_missing_action },3134OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3135N_("do not pack objects in promisor packfiles")),3136OPT_END(),3137};31383139if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3140BUG("too many dfs states, increase OE_DFS_STATE_BITS");31413142 check_replace_refs =0;31433144reset_pack_idx_option(&pack_idx_opts);3145git_config(git_pack_config, NULL);31463147 progress =isatty(2);3148 argc =parse_options(argc, argv, prefix, pack_objects_options,3149 pack_usage,0);31503151if(argc) {3152 base_name = argv[0];3153 argc--;3154}3155if(pack_to_stdout != !base_name || argc)3156usage_with_options(pack_usage, pack_objects_options);31573158if(depth >= (1<< OE_DEPTH_BITS)) {3159warning(_("delta chain depth%dis too deep, forcing%d"),3160 depth, (1<< OE_DEPTH_BITS) -1);3161 depth = (1<< OE_DEPTH_BITS) -1;3162}3163if(cache_max_small_delta_size >= (1U<< OE_Z_DELTA_BITS)) {3164warning(_("pack.deltaCacheLimit is too high, forcing%d"),3165(1U<< OE_Z_DELTA_BITS) -1);3166 cache_max_small_delta_size = (1U<< OE_Z_DELTA_BITS) -1;3167}31683169argv_array_push(&rp,"pack-objects");3170if(thin) {3171 use_internal_rev_list =1;3172argv_array_push(&rp, shallow3173?"--objects-edge-aggressive"3174:"--objects-edge");3175}else3176argv_array_push(&rp,"--objects");31773178if(rev_list_all) {3179 use_internal_rev_list =1;3180argv_array_push(&rp,"--all");3181}3182if(rev_list_reflog) {3183 use_internal_rev_list =1;3184argv_array_push(&rp,"--reflog");3185}3186if(rev_list_index) {3187 use_internal_rev_list =1;3188argv_array_push(&rp,"--indexed-objects");3189}3190if(rev_list_unpacked) {3191 use_internal_rev_list =1;3192argv_array_push(&rp,"--unpacked");3193}31943195if(exclude_promisor_objects) {3196 use_internal_rev_list =1;3197 fetch_if_missing =0;3198argv_array_push(&rp,"--exclude-promisor-objects");3199}32003201if(!reuse_object)3202 reuse_delta =0;3203if(pack_compression_level == -1)3204 pack_compression_level = Z_DEFAULT_COMPRESSION;3205else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3206die("bad pack compression level%d", pack_compression_level);32073208if(!delta_search_threads)/* --threads=0 means autodetect */3209 delta_search_threads =online_cpus();32103211#ifdef NO_PTHREADS3212if(delta_search_threads !=1)3213warning("no threads support, ignoring --threads");3214#endif3215if(!pack_to_stdout && !pack_size_limit)3216 pack_size_limit = pack_size_limit_cfg;3217if(pack_to_stdout && pack_size_limit)3218die("--max-pack-size cannot be used to build a pack for transfer.");3219if(pack_size_limit && pack_size_limit <1024*1024) {3220warning("minimum pack size limit is 1 MiB");3221 pack_size_limit =1024*1024;3222}32233224if(!pack_to_stdout && thin)3225die("--thin cannot be used to build an indexable pack.");32263227if(keep_unreachable && unpack_unreachable)3228die("--keep-unreachable and --unpack-unreachable are incompatible.");3229if(!rev_list_all || !rev_list_reflog || !rev_list_index)3230 unpack_unreachable_expiration =0;32313232if(filter_options.choice) {3233if(!pack_to_stdout)3234die("cannot use --filter without --stdout.");3235 use_bitmap_index =0;3236}32373238/*3239 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3240 *3241 * - to produce good pack (with bitmap index not-yet-packed objects are3242 * packed in suboptimal order).3243 *3244 * - to use more robust pack-generation codepath (avoiding possible3245 * bugs in bitmap code and possible bitmap index corruption).3246 */3247if(!pack_to_stdout)3248 use_bitmap_index_default =0;32493250if(use_bitmap_index <0)3251 use_bitmap_index = use_bitmap_index_default;32523253/* "hard" reasons not to use bitmaps; these just won't work at all */3254if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3255 use_bitmap_index =0;32563257if(pack_to_stdout || !rev_list_all)3258 write_bitmap_index =0;32593260if(progress && all_progress_implied)3261 progress =2;32623263if(ignore_packed_keep) {3264struct packed_git *p;3265for(p =get_packed_git(the_repository); p; p = p->next)3266if(p->pack_local && p->pack_keep)3267break;3268if(!p)/* no keep-able packs found */3269 ignore_packed_keep =0;3270}3271if(local) {3272/*3273 * unlike ignore_packed_keep above, we do not want to3274 * unset "local" based on looking at packs, as it3275 * also covers non-local objects3276 */3277struct packed_git *p;3278for(p =get_packed_git(the_repository); p; p = p->next) {3279if(!p->pack_local) {3280 have_non_local_packs =1;3281break;3282}3283}3284}32853286prepare_packing_data(&to_pack);32873288if(progress)3289 progress_state =start_progress(_("Counting objects"),0);3290if(!use_internal_rev_list)3291read_object_list_from_stdin();3292else{3293get_object_list(rp.argc, rp.argv);3294argv_array_clear(&rp);3295}3296cleanup_preferred_base();3297if(include_tag && nr_result)3298for_each_ref(add_ref_tag, NULL);3299stop_progress(&progress_state);33003301if(non_empty && !nr_result)3302return0;3303if(nr_result)3304prepare_pack(window, depth);3305write_pack_file();3306if(progress)3307fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3308" reused %"PRIu32" (delta %"PRIu32")\n",3309 written, written_delta, reused, reused_delta);3310return0;3311}