1#include"builtin.h" 2#include"cache.h" 3#include"repository.h" 4#include"config.h" 5#include"attr.h" 6#include"object.h" 7#include"blob.h" 8#include"commit.h" 9#include"tag.h" 10#include"tree.h" 11#include"delta.h" 12#include"pack.h" 13#include"pack-revindex.h" 14#include"csum-file.h" 15#include"tree-walk.h" 16#include"diff.h" 17#include"revision.h" 18#include"list-objects.h" 19#include"list-objects-filter.h" 20#include"list-objects-filter-options.h" 21#include"pack-objects.h" 22#include"progress.h" 23#include"refs.h" 24#include"streaming.h" 25#include"thread-utils.h" 26#include"pack-bitmap.h" 27#include"reachable.h" 28#include"sha1-array.h" 29#include"argv-array.h" 30#include"list.h" 31#include"packfile.h" 32#include"object-store.h" 33#include"dir.h" 34 35static const char*pack_usage[] = { 36N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 37N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 38 NULL 39}; 40 41/* 42 * Objects we are going to pack are collected in the `to_pack` structure. 43 * It contains an array (dynamically expanded) of the object data, and a map 44 * that can resolve SHA1s to their position in the array. 45 */ 46static struct packing_data to_pack; 47 48static struct pack_idx_entry **written_list; 49static uint32_t nr_result, nr_written, nr_seen; 50 51static int non_empty; 52static int reuse_delta =1, reuse_object =1; 53static int keep_unreachable, unpack_unreachable, include_tag; 54static timestamp_t unpack_unreachable_expiration; 55static int pack_loose_unreachable; 56static int local; 57static int have_non_local_packs; 58static int incremental; 59static int ignore_packed_keep_on_disk; 60static int ignore_packed_keep_in_core; 61static int allow_ofs_delta; 62static struct pack_idx_option pack_idx_opts; 63static const char*base_name; 64static int progress =1; 65static int window =10; 66static unsigned long pack_size_limit; 67static int depth =50; 68static int delta_search_threads; 69static int pack_to_stdout; 70static int num_preferred_base; 71static struct progress *progress_state; 72 73static struct packed_git *reuse_packfile; 74static uint32_t reuse_packfile_objects; 75static off_t reuse_packfile_offset; 76 77static int use_bitmap_index_default =1; 78static int use_bitmap_index = -1; 79static int write_bitmap_index; 80static uint16_t write_bitmap_options; 81 82static int exclude_promisor_objects; 83 84static unsigned long delta_cache_size =0; 85static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE; 86static unsigned long cache_max_small_delta_size =1000; 87 88static unsigned long window_memory_limit =0; 89 90static struct list_objects_filter_options filter_options; 91 92enum missing_action { 93 MA_ERROR =0,/* fail if any missing objects are encountered */ 94 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 95 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 96}; 97static enum missing_action arg_missing_action; 98static show_object_fn fn_show_object; 99 100/* 101 * stats 102 */ 103static uint32_t written, written_delta; 104static uint32_t reused, reused_delta; 105 106/* 107 * Indexed commits 108 */ 109static struct commit **indexed_commits; 110static unsigned int indexed_commits_nr; 111static unsigned int indexed_commits_alloc; 112 113static voidindex_commit_for_bitmap(struct commit *commit) 114{ 115if(indexed_commits_nr >= indexed_commits_alloc) { 116 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 117REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 118} 119 120 indexed_commits[indexed_commits_nr++] = commit; 121} 122 123static void*get_delta(struct object_entry *entry) 124{ 125unsigned long size, base_size, delta_size; 126void*buf, *base_buf, *delta_buf; 127enum object_type type; 128 129 buf =read_object_file(&entry->idx.oid, &type, &size); 130if(!buf) 131die("unable to read%s",oid_to_hex(&entry->idx.oid)); 132 base_buf =read_object_file(&entry->delta->idx.oid, &type, &base_size); 133if(!base_buf) 134die("unable to read%s", 135oid_to_hex(&entry->delta->idx.oid)); 136 delta_buf =diff_delta(base_buf, base_size, 137 buf, size, &delta_size,0); 138if(!delta_buf || delta_size != entry->delta_size) 139die("delta size changed"); 140free(buf); 141free(base_buf); 142return delta_buf; 143} 144 145static unsigned longdo_compress(void**pptr,unsigned long size) 146{ 147 git_zstream stream; 148void*in, *out; 149unsigned long maxsize; 150 151git_deflate_init(&stream, pack_compression_level); 152 maxsize =git_deflate_bound(&stream, size); 153 154 in = *pptr; 155 out =xmalloc(maxsize); 156*pptr = out; 157 158 stream.next_in = in; 159 stream.avail_in = size; 160 stream.next_out = out; 161 stream.avail_out = maxsize; 162while(git_deflate(&stream, Z_FINISH) == Z_OK) 163;/* nothing */ 164git_deflate_end(&stream); 165 166free(in); 167return stream.total_out; 168} 169 170static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 171const struct object_id *oid) 172{ 173 git_zstream stream; 174unsigned char ibuf[1024*16]; 175unsigned char obuf[1024*16]; 176unsigned long olen =0; 177 178git_deflate_init(&stream, pack_compression_level); 179 180for(;;) { 181 ssize_t readlen; 182int zret = Z_OK; 183 readlen =read_istream(st, ibuf,sizeof(ibuf)); 184if(readlen == -1) 185die(_("unable to read%s"),oid_to_hex(oid)); 186 187 stream.next_in = ibuf; 188 stream.avail_in = readlen; 189while((stream.avail_in || readlen ==0) && 190(zret == Z_OK || zret == Z_BUF_ERROR)) { 191 stream.next_out = obuf; 192 stream.avail_out =sizeof(obuf); 193 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 194hashwrite(f, obuf, stream.next_out - obuf); 195 olen += stream.next_out - obuf; 196} 197if(stream.avail_in) 198die(_("deflate error (%d)"), zret); 199if(readlen ==0) { 200if(zret != Z_STREAM_END) 201die(_("deflate error (%d)"), zret); 202break; 203} 204} 205git_deflate_end(&stream); 206return olen; 207} 208 209/* 210 * we are going to reuse the existing object data as is. make 211 * sure it is not corrupt. 212 */ 213static intcheck_pack_inflate(struct packed_git *p, 214struct pack_window **w_curs, 215 off_t offset, 216 off_t len, 217unsigned long expect) 218{ 219 git_zstream stream; 220unsigned char fakebuf[4096], *in; 221int st; 222 223memset(&stream,0,sizeof(stream)); 224git_inflate_init(&stream); 225do{ 226 in =use_pack(p, w_curs, offset, &stream.avail_in); 227 stream.next_in = in; 228 stream.next_out = fakebuf; 229 stream.avail_out =sizeof(fakebuf); 230 st =git_inflate(&stream, Z_FINISH); 231 offset += stream.next_in - in; 232}while(st == Z_OK || st == Z_BUF_ERROR); 233git_inflate_end(&stream); 234return(st == Z_STREAM_END && 235 stream.total_out == expect && 236 stream.total_in == len) ?0: -1; 237} 238 239static voidcopy_pack_data(struct hashfile *f, 240struct packed_git *p, 241struct pack_window **w_curs, 242 off_t offset, 243 off_t len) 244{ 245unsigned char*in; 246unsigned long avail; 247 248while(len) { 249 in =use_pack(p, w_curs, offset, &avail); 250if(avail > len) 251 avail = (unsigned long)len; 252hashwrite(f, in, avail); 253 offset += avail; 254 len -= avail; 255} 256} 257 258/* Return 0 if we will bust the pack-size limit */ 259static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 260unsigned long limit,int usable_delta) 261{ 262unsigned long size, datalen; 263unsigned char header[MAX_PACK_OBJECT_HEADER], 264 dheader[MAX_PACK_OBJECT_HEADER]; 265unsigned hdrlen; 266enum object_type type; 267void*buf; 268struct git_istream *st = NULL; 269 270if(!usable_delta) { 271if(entry->type == OBJ_BLOB && 272 entry->size > big_file_threshold && 273(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 274 buf = NULL; 275else{ 276 buf =read_object_file(&entry->idx.oid, &type, &size); 277if(!buf) 278die(_("unable to read%s"), 279oid_to_hex(&entry->idx.oid)); 280} 281/* 282 * make sure no cached delta data remains from a 283 * previous attempt before a pack split occurred. 284 */ 285FREE_AND_NULL(entry->delta_data); 286 entry->z_delta_size =0; 287}else if(entry->delta_data) { 288 size = entry->delta_size; 289 buf = entry->delta_data; 290 entry->delta_data = NULL; 291 type = (allow_ofs_delta && entry->delta->idx.offset) ? 292 OBJ_OFS_DELTA : OBJ_REF_DELTA; 293}else{ 294 buf =get_delta(entry); 295 size = entry->delta_size; 296 type = (allow_ofs_delta && entry->delta->idx.offset) ? 297 OBJ_OFS_DELTA : OBJ_REF_DELTA; 298} 299 300if(st)/* large blob case, just assume we don't compress well */ 301 datalen = size; 302else if(entry->z_delta_size) 303 datalen = entry->z_delta_size; 304else 305 datalen =do_compress(&buf, size); 306 307/* 308 * The object header is a byte of 'type' followed by zero or 309 * more bytes of length. 310 */ 311 hdrlen =encode_in_pack_object_header(header,sizeof(header), 312 type, size); 313 314if(type == OBJ_OFS_DELTA) { 315/* 316 * Deltas with relative base contain an additional 317 * encoding of the relative offset for the delta 318 * base from this object's position in the pack. 319 */ 320 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 321unsigned pos =sizeof(dheader) -1; 322 dheader[pos] = ofs &127; 323while(ofs >>=7) 324 dheader[--pos] =128| (--ofs &127); 325if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 326if(st) 327close_istream(st); 328free(buf); 329return0; 330} 331hashwrite(f, header, hdrlen); 332hashwrite(f, dheader + pos,sizeof(dheader) - pos); 333 hdrlen +=sizeof(dheader) - pos; 334}else if(type == OBJ_REF_DELTA) { 335/* 336 * Deltas with a base reference contain 337 * an additional 20 bytes for the base sha1. 338 */ 339if(limit && hdrlen +20+ datalen +20>= limit) { 340if(st) 341close_istream(st); 342free(buf); 343return0; 344} 345hashwrite(f, header, hdrlen); 346hashwrite(f, entry->delta->idx.oid.hash,20); 347 hdrlen +=20; 348}else{ 349if(limit && hdrlen + datalen +20>= limit) { 350if(st) 351close_istream(st); 352free(buf); 353return0; 354} 355hashwrite(f, header, hdrlen); 356} 357if(st) { 358 datalen =write_large_blob_data(st, f, &entry->idx.oid); 359close_istream(st); 360}else{ 361hashwrite(f, buf, datalen); 362free(buf); 363} 364 365return hdrlen + datalen; 366} 367 368/* Return 0 if we will bust the pack-size limit */ 369static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 370unsigned long limit,int usable_delta) 371{ 372struct packed_git *p = entry->in_pack; 373struct pack_window *w_curs = NULL; 374struct revindex_entry *revidx; 375 off_t offset; 376enum object_type type = entry->type; 377 off_t datalen; 378unsigned char header[MAX_PACK_OBJECT_HEADER], 379 dheader[MAX_PACK_OBJECT_HEADER]; 380unsigned hdrlen; 381 382if(entry->delta) 383 type = (allow_ofs_delta && entry->delta->idx.offset) ? 384 OBJ_OFS_DELTA : OBJ_REF_DELTA; 385 hdrlen =encode_in_pack_object_header(header,sizeof(header), 386 type, entry->size); 387 388 offset = entry->in_pack_offset; 389 revidx =find_pack_revindex(p, offset); 390 datalen = revidx[1].offset - offset; 391if(!pack_to_stdout && p->index_version >1&& 392check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 393error("bad packed object CRC for%s", 394oid_to_hex(&entry->idx.oid)); 395unuse_pack(&w_curs); 396returnwrite_no_reuse_object(f, entry, limit, usable_delta); 397} 398 399 offset += entry->in_pack_header_size; 400 datalen -= entry->in_pack_header_size; 401 402if(!pack_to_stdout && p->index_version ==1&& 403check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) { 404error("corrupt packed object for%s", 405oid_to_hex(&entry->idx.oid)); 406unuse_pack(&w_curs); 407returnwrite_no_reuse_object(f, entry, limit, usable_delta); 408} 409 410if(type == OBJ_OFS_DELTA) { 411 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 412unsigned pos =sizeof(dheader) -1; 413 dheader[pos] = ofs &127; 414while(ofs >>=7) 415 dheader[--pos] =128| (--ofs &127); 416if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 417unuse_pack(&w_curs); 418return0; 419} 420hashwrite(f, header, hdrlen); 421hashwrite(f, dheader + pos,sizeof(dheader) - pos); 422 hdrlen +=sizeof(dheader) - pos; 423 reused_delta++; 424}else if(type == OBJ_REF_DELTA) { 425if(limit && hdrlen +20+ datalen +20>= limit) { 426unuse_pack(&w_curs); 427return0; 428} 429hashwrite(f, header, hdrlen); 430hashwrite(f, entry->delta->idx.oid.hash,20); 431 hdrlen +=20; 432 reused_delta++; 433}else{ 434if(limit && hdrlen + datalen +20>= limit) { 435unuse_pack(&w_curs); 436return0; 437} 438hashwrite(f, header, hdrlen); 439} 440copy_pack_data(f, p, &w_curs, offset, datalen); 441unuse_pack(&w_curs); 442 reused++; 443return hdrlen + datalen; 444} 445 446/* Return 0 if we will bust the pack-size limit */ 447static off_t write_object(struct hashfile *f, 448struct object_entry *entry, 449 off_t write_offset) 450{ 451unsigned long limit; 452 off_t len; 453int usable_delta, to_reuse; 454 455if(!pack_to_stdout) 456crc32_begin(f); 457 458/* apply size limit if limited packsize and not first object */ 459if(!pack_size_limit || !nr_written) 460 limit =0; 461else if(pack_size_limit <= write_offset) 462/* 463 * the earlier object did not fit the limit; avoid 464 * mistaking this with unlimited (i.e. limit = 0). 465 */ 466 limit =1; 467else 468 limit = pack_size_limit - write_offset; 469 470if(!entry->delta) 471 usable_delta =0;/* no delta */ 472else if(!pack_size_limit) 473 usable_delta =1;/* unlimited packfile */ 474else if(entry->delta->idx.offset == (off_t)-1) 475 usable_delta =0;/* base was written to another pack */ 476else if(entry->delta->idx.offset) 477 usable_delta =1;/* base already exists in this pack */ 478else 479 usable_delta =0;/* base could end up in another pack */ 480 481if(!reuse_object) 482 to_reuse =0;/* explicit */ 483else if(!entry->in_pack) 484 to_reuse =0;/* can't reuse what we don't have */ 485else if(entry->type == OBJ_REF_DELTA || entry->type == OBJ_OFS_DELTA) 486/* check_object() decided it for us ... */ 487 to_reuse = usable_delta; 488/* ... but pack split may override that */ 489else if(entry->type != entry->in_pack_type) 490 to_reuse =0;/* pack has delta which is unusable */ 491else if(entry->delta) 492 to_reuse =0;/* we want to pack afresh */ 493else 494 to_reuse =1;/* we have it in-pack undeltified, 495 * and we do not need to deltify it. 496 */ 497 498if(!to_reuse) 499 len =write_no_reuse_object(f, entry, limit, usable_delta); 500else 501 len =write_reuse_object(f, entry, limit, usable_delta); 502if(!len) 503return0; 504 505if(usable_delta) 506 written_delta++; 507 written++; 508if(!pack_to_stdout) 509 entry->idx.crc32 =crc32_end(f); 510return len; 511} 512 513enum write_one_status { 514 WRITE_ONE_SKIP = -1,/* already written */ 515 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 516 WRITE_ONE_WRITTEN =1,/* normal */ 517 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 518}; 519 520static enum write_one_status write_one(struct hashfile *f, 521struct object_entry *e, 522 off_t *offset) 523{ 524 off_t size; 525int recursing; 526 527/* 528 * we set offset to 1 (which is an impossible value) to mark 529 * the fact that this object is involved in "write its base 530 * first before writing a deltified object" recursion. 531 */ 532 recursing = (e->idx.offset ==1); 533if(recursing) { 534warning("recursive delta detected for object%s", 535oid_to_hex(&e->idx.oid)); 536return WRITE_ONE_RECURSIVE; 537}else if(e->idx.offset || e->preferred_base) { 538/* offset is non zero if object is written already. */ 539return WRITE_ONE_SKIP; 540} 541 542/* if we are deltified, write out base object first. */ 543if(e->delta) { 544 e->idx.offset =1;/* now recurse */ 545switch(write_one(f, e->delta, offset)) { 546case WRITE_ONE_RECURSIVE: 547/* we cannot depend on this one */ 548 e->delta = NULL; 549break; 550default: 551break; 552case WRITE_ONE_BREAK: 553 e->idx.offset = recursing; 554return WRITE_ONE_BREAK; 555} 556} 557 558 e->idx.offset = *offset; 559 size =write_object(f, e, *offset); 560if(!size) { 561 e->idx.offset = recursing; 562return WRITE_ONE_BREAK; 563} 564 written_list[nr_written++] = &e->idx; 565 566/* make sure off_t is sufficiently large not to wrap */ 567if(signed_add_overflows(*offset, size)) 568die("pack too large for current definition of off_t"); 569*offset += size; 570return WRITE_ONE_WRITTEN; 571} 572 573static intmark_tagged(const char*path,const struct object_id *oid,int flag, 574void*cb_data) 575{ 576struct object_id peeled; 577struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 578 579if(entry) 580 entry->tagged =1; 581if(!peel_ref(path, &peeled)) { 582 entry =packlist_find(&to_pack, peeled.hash, NULL); 583if(entry) 584 entry->tagged =1; 585} 586return0; 587} 588 589staticinlinevoidadd_to_write_order(struct object_entry **wo, 590unsigned int*endp, 591struct object_entry *e) 592{ 593if(e->filled) 594return; 595 wo[(*endp)++] = e; 596 e->filled =1; 597} 598 599static voidadd_descendants_to_write_order(struct object_entry **wo, 600unsigned int*endp, 601struct object_entry *e) 602{ 603int add_to_order =1; 604while(e) { 605if(add_to_order) { 606struct object_entry *s; 607/* add this node... */ 608add_to_write_order(wo, endp, e); 609/* all its siblings... */ 610for(s = e->delta_sibling; s; s = s->delta_sibling) { 611add_to_write_order(wo, endp, s); 612} 613} 614/* drop down a level to add left subtree nodes if possible */ 615if(e->delta_child) { 616 add_to_order =1; 617 e = e->delta_child; 618}else{ 619 add_to_order =0; 620/* our sibling might have some children, it is next */ 621if(e->delta_sibling) { 622 e = e->delta_sibling; 623continue; 624} 625/* go back to our parent node */ 626 e = e->delta; 627while(e && !e->delta_sibling) { 628/* we're on the right side of a subtree, keep 629 * going up until we can go right again */ 630 e = e->delta; 631} 632if(!e) { 633/* done- we hit our original root node */ 634return; 635} 636/* pass it off to sibling at this level */ 637 e = e->delta_sibling; 638} 639}; 640} 641 642static voidadd_family_to_write_order(struct object_entry **wo, 643unsigned int*endp, 644struct object_entry *e) 645{ 646struct object_entry *root; 647 648for(root = e; root->delta; root = root->delta) 649;/* nothing */ 650add_descendants_to_write_order(wo, endp, root); 651} 652 653static struct object_entry **compute_write_order(void) 654{ 655unsigned int i, wo_end, last_untagged; 656 657struct object_entry **wo; 658struct object_entry *objects = to_pack.objects; 659 660for(i =0; i < to_pack.nr_objects; i++) { 661 objects[i].tagged =0; 662 objects[i].filled =0; 663 objects[i].delta_child = NULL; 664 objects[i].delta_sibling = NULL; 665} 666 667/* 668 * Fully connect delta_child/delta_sibling network. 669 * Make sure delta_sibling is sorted in the original 670 * recency order. 671 */ 672for(i = to_pack.nr_objects; i >0;) { 673struct object_entry *e = &objects[--i]; 674if(!e->delta) 675continue; 676/* Mark me as the first child */ 677 e->delta_sibling = e->delta->delta_child; 678 e->delta->delta_child = e; 679} 680 681/* 682 * Mark objects that are at the tip of tags. 683 */ 684for_each_tag_ref(mark_tagged, NULL); 685 686/* 687 * Give the objects in the original recency order until 688 * we see a tagged tip. 689 */ 690ALLOC_ARRAY(wo, to_pack.nr_objects); 691for(i = wo_end =0; i < to_pack.nr_objects; i++) { 692if(objects[i].tagged) 693break; 694add_to_write_order(wo, &wo_end, &objects[i]); 695} 696 last_untagged = i; 697 698/* 699 * Then fill all the tagged tips. 700 */ 701for(; i < to_pack.nr_objects; i++) { 702if(objects[i].tagged) 703add_to_write_order(wo, &wo_end, &objects[i]); 704} 705 706/* 707 * And then all remaining commits and tags. 708 */ 709for(i = last_untagged; i < to_pack.nr_objects; i++) { 710if(objects[i].type != OBJ_COMMIT && 711 objects[i].type != OBJ_TAG) 712continue; 713add_to_write_order(wo, &wo_end, &objects[i]); 714} 715 716/* 717 * And then all the trees. 718 */ 719for(i = last_untagged; i < to_pack.nr_objects; i++) { 720if(objects[i].type != OBJ_TREE) 721continue; 722add_to_write_order(wo, &wo_end, &objects[i]); 723} 724 725/* 726 * Finally all the rest in really tight order 727 */ 728for(i = last_untagged; i < to_pack.nr_objects; i++) { 729if(!objects[i].filled) 730add_family_to_write_order(wo, &wo_end, &objects[i]); 731} 732 733if(wo_end != to_pack.nr_objects) 734die("ordered%uobjects, expected %"PRIu32, wo_end, to_pack.nr_objects); 735 736return wo; 737} 738 739static off_t write_reused_pack(struct hashfile *f) 740{ 741unsigned char buffer[8192]; 742 off_t to_write, total; 743int fd; 744 745if(!is_pack_valid(reuse_packfile)) 746die("packfile is invalid:%s", reuse_packfile->pack_name); 747 748 fd =git_open(reuse_packfile->pack_name); 749if(fd <0) 750die_errno("unable to open packfile for reuse:%s", 751 reuse_packfile->pack_name); 752 753if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 754die_errno("unable to seek in reused packfile"); 755 756if(reuse_packfile_offset <0) 757 reuse_packfile_offset = reuse_packfile->pack_size -20; 758 759 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 760 761while(to_write) { 762int read_pack =xread(fd, buffer,sizeof(buffer)); 763 764if(read_pack <=0) 765die_errno("unable to read from reused packfile"); 766 767if(read_pack > to_write) 768 read_pack = to_write; 769 770hashwrite(f, buffer, read_pack); 771 to_write -= read_pack; 772 773/* 774 * We don't know the actual number of objects written, 775 * only how many bytes written, how many bytes total, and 776 * how many objects total. So we can fake it by pretending all 777 * objects we are writing are the same size. This gives us a 778 * smooth progress meter, and at the end it matches the true 779 * answer. 780 */ 781 written = reuse_packfile_objects * 782(((double)(total - to_write)) / total); 783display_progress(progress_state, written); 784} 785 786close(fd); 787 written = reuse_packfile_objects; 788display_progress(progress_state, written); 789return reuse_packfile_offset -sizeof(struct pack_header); 790} 791 792static const char no_split_warning[] =N_( 793"disabling bitmap writing, packs are split due to pack.packSizeLimit" 794); 795 796static voidwrite_pack_file(void) 797{ 798uint32_t i =0, j; 799struct hashfile *f; 800 off_t offset; 801uint32_t nr_remaining = nr_result; 802time_t last_mtime =0; 803struct object_entry **write_order; 804 805if(progress > pack_to_stdout) 806 progress_state =start_progress(_("Writing objects"), nr_result); 807ALLOC_ARRAY(written_list, to_pack.nr_objects); 808 write_order =compute_write_order(); 809 810do{ 811struct object_id oid; 812char*pack_tmp_name = NULL; 813 814if(pack_to_stdout) 815 f =hashfd_throughput(1,"<stdout>", progress_state); 816else 817 f =create_tmp_packfile(&pack_tmp_name); 818 819 offset =write_pack_header(f, nr_remaining); 820 821if(reuse_packfile) { 822 off_t packfile_size; 823assert(pack_to_stdout); 824 825 packfile_size =write_reused_pack(f); 826 offset += packfile_size; 827} 828 829 nr_written =0; 830for(; i < to_pack.nr_objects; i++) { 831struct object_entry *e = write_order[i]; 832if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 833break; 834display_progress(progress_state, written); 835} 836 837/* 838 * Did we write the wrong # entries in the header? 839 * If so, rewrite it like in fast-import 840 */ 841if(pack_to_stdout) { 842finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 843}else if(nr_written == nr_remaining) { 844finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 845}else{ 846int fd =finalize_hashfile(f, oid.hash,0); 847fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 848 nr_written, oid.hash, offset); 849close(fd); 850if(write_bitmap_index) { 851warning(_(no_split_warning)); 852 write_bitmap_index =0; 853} 854} 855 856if(!pack_to_stdout) { 857struct stat st; 858struct strbuf tmpname = STRBUF_INIT; 859 860/* 861 * Packs are runtime accessed in their mtime 862 * order since newer packs are more likely to contain 863 * younger objects. So if we are creating multiple 864 * packs then we should modify the mtime of later ones 865 * to preserve this property. 866 */ 867if(stat(pack_tmp_name, &st) <0) { 868warning_errno("failed to stat%s", pack_tmp_name); 869}else if(!last_mtime) { 870 last_mtime = st.st_mtime; 871}else{ 872struct utimbuf utb; 873 utb.actime = st.st_atime; 874 utb.modtime = --last_mtime; 875if(utime(pack_tmp_name, &utb) <0) 876warning_errno("failed utime() on%s", pack_tmp_name); 877} 878 879strbuf_addf(&tmpname,"%s-", base_name); 880 881if(write_bitmap_index) { 882bitmap_writer_set_checksum(oid.hash); 883bitmap_writer_build_type_index(written_list, nr_written); 884} 885 886finish_tmp_packfile(&tmpname, pack_tmp_name, 887 written_list, nr_written, 888&pack_idx_opts, oid.hash); 889 890if(write_bitmap_index) { 891strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 892 893stop_progress(&progress_state); 894 895bitmap_writer_show_progress(progress); 896bitmap_writer_reuse_bitmaps(&to_pack); 897bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 898bitmap_writer_build(&to_pack); 899bitmap_writer_finish(written_list, nr_written, 900 tmpname.buf, write_bitmap_options); 901 write_bitmap_index =0; 902} 903 904strbuf_release(&tmpname); 905free(pack_tmp_name); 906puts(oid_to_hex(&oid)); 907} 908 909/* mark written objects as written to previous pack */ 910for(j =0; j < nr_written; j++) { 911 written_list[j]->offset = (off_t)-1; 912} 913 nr_remaining -= nr_written; 914}while(nr_remaining && i < to_pack.nr_objects); 915 916free(written_list); 917free(write_order); 918stop_progress(&progress_state); 919if(written != nr_result) 920die("wrote %"PRIu32" objects while expecting %"PRIu32, 921 written, nr_result); 922} 923 924static intno_try_delta(const char*path) 925{ 926static struct attr_check *check; 927 928if(!check) 929 check =attr_check_initl("delta", NULL); 930if(git_check_attr(path, check)) 931return0; 932if(ATTR_FALSE(check->items[0].value)) 933return1; 934return0; 935} 936 937/* 938 * When adding an object, check whether we have already added it 939 * to our packing list. If so, we can skip. However, if we are 940 * being asked to excludei t, but the previous mention was to include 941 * it, make sure to adjust its flags and tweak our numbers accordingly. 942 * 943 * As an optimization, we pass out the index position where we would have 944 * found the item, since that saves us from having to look it up again a 945 * few lines later when we want to add the new entry. 946 */ 947static inthave_duplicate_entry(const struct object_id *oid, 948int exclude, 949uint32_t*index_pos) 950{ 951struct object_entry *entry; 952 953 entry =packlist_find(&to_pack, oid->hash, index_pos); 954if(!entry) 955return0; 956 957if(exclude) { 958if(!entry->preferred_base) 959 nr_result--; 960 entry->preferred_base =1; 961} 962 963return1; 964} 965 966static intwant_found_object(int exclude,struct packed_git *p) 967{ 968if(exclude) 969return1; 970if(incremental) 971return0; 972 973/* 974 * When asked to do --local (do not include an object that appears in a 975 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 976 * an object that appears in a pack marked with .keep), finding a pack 977 * that matches the criteria is sufficient for us to decide to omit it. 978 * However, even if this pack does not satisfy the criteria, we need to 979 * make sure no copy of this object appears in _any_ pack that makes us 980 * to omit the object, so we need to check all the packs. 981 * 982 * We can however first check whether these options can possible matter; 983 * if they do not matter we know we want the object in generated pack. 984 * Otherwise, we signal "-1" at the end to tell the caller that we do 985 * not know either way, and it needs to check more packs. 986 */ 987if(!ignore_packed_keep_on_disk && 988!ignore_packed_keep_in_core && 989(!local || !have_non_local_packs)) 990return1; 991 992if(local && !p->pack_local) 993return0; 994if(p->pack_local && 995((ignore_packed_keep_on_disk && p->pack_keep) || 996(ignore_packed_keep_in_core && p->pack_keep_in_core))) 997return0; 998 999/* we don't know yet; keep looking for more packs */1000return-1;1001}10021003/*1004 * Check whether we want the object in the pack (e.g., we do not want1005 * objects found in non-local stores if the "--local" option was used).1006 *1007 * If the caller already knows an existing pack it wants to take the object1008 * from, that is passed in *found_pack and *found_offset; otherwise this1009 * function finds if there is any pack that has the object and returns the pack1010 * and its offset in these variables.1011 */1012static intwant_object_in_pack(const struct object_id *oid,1013int exclude,1014struct packed_git **found_pack,1015 off_t *found_offset)1016{1017int want;1018struct list_head *pos;10191020if(!exclude && local &&has_loose_object_nonlocal(oid->hash))1021return0;10221023/*1024 * If we already know the pack object lives in, start checks from that1025 * pack - in the usual case when neither --local was given nor .keep files1026 * are present we will determine the answer right now.1027 */1028if(*found_pack) {1029 want =want_found_object(exclude, *found_pack);1030if(want != -1)1031return want;1032}1033list_for_each(pos,get_packed_git_mru(the_repository)) {1034struct packed_git *p =list_entry(pos,struct packed_git, mru);1035 off_t offset;10361037if(p == *found_pack)1038 offset = *found_offset;1039else1040 offset =find_pack_entry_one(oid->hash, p);10411042if(offset) {1043if(!*found_pack) {1044if(!is_pack_valid(p))1045continue;1046*found_offset = offset;1047*found_pack = p;1048}1049 want =want_found_object(exclude, p);1050if(!exclude && want >0)1051list_move(&p->mru,1052get_packed_git_mru(the_repository));1053if(want != -1)1054return want;1055}1056}10571058return1;1059}10601061static voidcreate_object_entry(const struct object_id *oid,1062enum object_type type,1063uint32_t hash,1064int exclude,1065int no_try_delta,1066uint32_t index_pos,1067struct packed_git *found_pack,1068 off_t found_offset)1069{1070struct object_entry *entry;10711072 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1073 entry->hash = hash;1074if(type)1075 entry->type = type;1076if(exclude)1077 entry->preferred_base =1;1078else1079 nr_result++;1080if(found_pack) {1081 entry->in_pack = found_pack;1082 entry->in_pack_offset = found_offset;1083}10841085 entry->no_try_delta = no_try_delta;1086}10871088static const char no_closure_warning[] =N_(1089"disabling bitmap writing, as some objects are not being packed"1090);10911092static intadd_object_entry(const struct object_id *oid,enum object_type type,1093const char*name,int exclude)1094{1095struct packed_git *found_pack = NULL;1096 off_t found_offset =0;1097uint32_t index_pos;10981099display_progress(progress_state, ++nr_seen);11001101if(have_duplicate_entry(oid, exclude, &index_pos))1102return0;11031104if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1105/* The pack is missing an object, so it will not have closure */1106if(write_bitmap_index) {1107warning(_(no_closure_warning));1108 write_bitmap_index =0;1109}1110return0;1111}11121113create_object_entry(oid, type,pack_name_hash(name),1114 exclude, name &&no_try_delta(name),1115 index_pos, found_pack, found_offset);1116return1;1117}11181119static intadd_object_entry_from_bitmap(const struct object_id *oid,1120enum object_type type,1121int flags,uint32_t name_hash,1122struct packed_git *pack, off_t offset)1123{1124uint32_t index_pos;11251126display_progress(progress_state, ++nr_seen);11271128if(have_duplicate_entry(oid,0, &index_pos))1129return0;11301131if(!want_object_in_pack(oid,0, &pack, &offset))1132return0;11331134create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);1135return1;1136}11371138struct pbase_tree_cache {1139struct object_id oid;1140int ref;1141int temporary;1142void*tree_data;1143unsigned long tree_size;1144};11451146static struct pbase_tree_cache *(pbase_tree_cache[256]);1147static intpbase_tree_cache_ix(const struct object_id *oid)1148{1149return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1150}1151static intpbase_tree_cache_ix_incr(int ix)1152{1153return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1154}11551156static struct pbase_tree {1157struct pbase_tree *next;1158/* This is a phony "cache" entry; we are not1159 * going to evict it or find it through _get()1160 * mechanism -- this is for the toplevel node that1161 * would almost always change with any commit.1162 */1163struct pbase_tree_cache pcache;1164} *pbase_tree;11651166static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1167{1168struct pbase_tree_cache *ent, *nent;1169void*data;1170unsigned long size;1171enum object_type type;1172int neigh;1173int my_ix =pbase_tree_cache_ix(oid);1174int available_ix = -1;11751176/* pbase-tree-cache acts as a limited hashtable.1177 * your object will be found at your index or within a few1178 * slots after that slot if it is cached.1179 */1180for(neigh =0; neigh <8; neigh++) {1181 ent = pbase_tree_cache[my_ix];1182if(ent && !oidcmp(&ent->oid, oid)) {1183 ent->ref++;1184return ent;1185}1186else if(((available_ix <0) && (!ent || !ent->ref)) ||1187((0<= available_ix) &&1188(!ent && pbase_tree_cache[available_ix])))1189 available_ix = my_ix;1190if(!ent)1191break;1192 my_ix =pbase_tree_cache_ix_incr(my_ix);1193}11941195/* Did not find one. Either we got a bogus request or1196 * we need to read and perhaps cache.1197 */1198 data =read_object_file(oid, &type, &size);1199if(!data)1200return NULL;1201if(type != OBJ_TREE) {1202free(data);1203return NULL;1204}12051206/* We need to either cache or return a throwaway copy */12071208if(available_ix <0)1209 ent = NULL;1210else{1211 ent = pbase_tree_cache[available_ix];1212 my_ix = available_ix;1213}12141215if(!ent) {1216 nent =xmalloc(sizeof(*nent));1217 nent->temporary = (available_ix <0);1218}1219else{1220/* evict and reuse */1221free(ent->tree_data);1222 nent = ent;1223}1224oidcpy(&nent->oid, oid);1225 nent->tree_data = data;1226 nent->tree_size = size;1227 nent->ref =1;1228if(!nent->temporary)1229 pbase_tree_cache[my_ix] = nent;1230return nent;1231}12321233static voidpbase_tree_put(struct pbase_tree_cache *cache)1234{1235if(!cache->temporary) {1236 cache->ref--;1237return;1238}1239free(cache->tree_data);1240free(cache);1241}12421243static intname_cmp_len(const char*name)1244{1245int i;1246for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1247;1248return i;1249}12501251static voidadd_pbase_object(struct tree_desc *tree,1252const char*name,1253int cmplen,1254const char*fullname)1255{1256struct name_entry entry;1257int cmp;12581259while(tree_entry(tree,&entry)) {1260if(S_ISGITLINK(entry.mode))1261continue;1262 cmp =tree_entry_len(&entry) != cmplen ?1:1263memcmp(name, entry.path, cmplen);1264if(cmp >0)1265continue;1266if(cmp <0)1267return;1268if(name[cmplen] !='/') {1269add_object_entry(entry.oid,1270object_type(entry.mode),1271 fullname,1);1272return;1273}1274if(S_ISDIR(entry.mode)) {1275struct tree_desc sub;1276struct pbase_tree_cache *tree;1277const char*down = name+cmplen+1;1278int downlen =name_cmp_len(down);12791280 tree =pbase_tree_get(entry.oid);1281if(!tree)1282return;1283init_tree_desc(&sub, tree->tree_data, tree->tree_size);12841285add_pbase_object(&sub, down, downlen, fullname);1286pbase_tree_put(tree);1287}1288}1289}12901291static unsigned*done_pbase_paths;1292static int done_pbase_paths_num;1293static int done_pbase_paths_alloc;1294static intdone_pbase_path_pos(unsigned hash)1295{1296int lo =0;1297int hi = done_pbase_paths_num;1298while(lo < hi) {1299int mi = lo + (hi - lo) /2;1300if(done_pbase_paths[mi] == hash)1301return mi;1302if(done_pbase_paths[mi] < hash)1303 hi = mi;1304else1305 lo = mi +1;1306}1307return-lo-1;1308}13091310static intcheck_pbase_path(unsigned hash)1311{1312int pos =done_pbase_path_pos(hash);1313if(0<= pos)1314return1;1315 pos = -pos -1;1316ALLOC_GROW(done_pbase_paths,1317 done_pbase_paths_num +1,1318 done_pbase_paths_alloc);1319 done_pbase_paths_num++;1320if(pos < done_pbase_paths_num)1321MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1322 done_pbase_paths_num - pos -1);1323 done_pbase_paths[pos] = hash;1324return0;1325}13261327static voidadd_preferred_base_object(const char*name)1328{1329struct pbase_tree *it;1330int cmplen;1331unsigned hash =pack_name_hash(name);13321333if(!num_preferred_base ||check_pbase_path(hash))1334return;13351336 cmplen =name_cmp_len(name);1337for(it = pbase_tree; it; it = it->next) {1338if(cmplen ==0) {1339add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1340}1341else{1342struct tree_desc tree;1343init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1344add_pbase_object(&tree, name, cmplen, name);1345}1346}1347}13481349static voidadd_preferred_base(struct object_id *oid)1350{1351struct pbase_tree *it;1352void*data;1353unsigned long size;1354struct object_id tree_oid;13551356if(window <= num_preferred_base++)1357return;13581359 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1360if(!data)1361return;13621363for(it = pbase_tree; it; it = it->next) {1364if(!oidcmp(&it->pcache.oid, &tree_oid)) {1365free(data);1366return;1367}1368}13691370 it =xcalloc(1,sizeof(*it));1371 it->next = pbase_tree;1372 pbase_tree = it;13731374oidcpy(&it->pcache.oid, &tree_oid);1375 it->pcache.tree_data = data;1376 it->pcache.tree_size = size;1377}13781379static voidcleanup_preferred_base(void)1380{1381struct pbase_tree *it;1382unsigned i;13831384 it = pbase_tree;1385 pbase_tree = NULL;1386while(it) {1387struct pbase_tree *tmp = it;1388 it = tmp->next;1389free(tmp->pcache.tree_data);1390free(tmp);1391}13921393for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1394if(!pbase_tree_cache[i])1395continue;1396free(pbase_tree_cache[i]->tree_data);1397FREE_AND_NULL(pbase_tree_cache[i]);1398}13991400FREE_AND_NULL(done_pbase_paths);1401 done_pbase_paths_num = done_pbase_paths_alloc =0;1402}14031404static voidcheck_object(struct object_entry *entry)1405{1406if(entry->in_pack) {1407struct packed_git *p = entry->in_pack;1408struct pack_window *w_curs = NULL;1409const unsigned char*base_ref = NULL;1410struct object_entry *base_entry;1411unsigned long used, used_0;1412unsigned long avail;1413 off_t ofs;1414unsigned char*buf, c;14151416 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14171418/*1419 * We want in_pack_type even if we do not reuse delta1420 * since non-delta representations could still be reused.1421 */1422 used =unpack_object_header_buffer(buf, avail,1423&entry->in_pack_type,1424&entry->size);1425if(used ==0)1426goto give_up;14271428/*1429 * Determine if this is a delta and if so whether we can1430 * reuse it or not. Otherwise let's find out as cheaply as1431 * possible what the actual type and size for this object is.1432 */1433switch(entry->in_pack_type) {1434default:1435/* Not a delta hence we've already got all we need. */1436 entry->type = entry->in_pack_type;1437 entry->in_pack_header_size = used;1438if(entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB)1439goto give_up;1440unuse_pack(&w_curs);1441return;1442case OBJ_REF_DELTA:1443if(reuse_delta && !entry->preferred_base)1444 base_ref =use_pack(p, &w_curs,1445 entry->in_pack_offset + used, NULL);1446 entry->in_pack_header_size = used +20;1447break;1448case OBJ_OFS_DELTA:1449 buf =use_pack(p, &w_curs,1450 entry->in_pack_offset + used, NULL);1451 used_0 =0;1452 c = buf[used_0++];1453 ofs = c &127;1454while(c &128) {1455 ofs +=1;1456if(!ofs ||MSB(ofs,7)) {1457error("delta base offset overflow in pack for%s",1458oid_to_hex(&entry->idx.oid));1459goto give_up;1460}1461 c = buf[used_0++];1462 ofs = (ofs <<7) + (c &127);1463}1464 ofs = entry->in_pack_offset - ofs;1465if(ofs <=0|| ofs >= entry->in_pack_offset) {1466error("delta base offset out of bound for%s",1467oid_to_hex(&entry->idx.oid));1468goto give_up;1469}1470if(reuse_delta && !entry->preferred_base) {1471struct revindex_entry *revidx;1472 revidx =find_pack_revindex(p, ofs);1473if(!revidx)1474goto give_up;1475 base_ref =nth_packed_object_sha1(p, revidx->nr);1476}1477 entry->in_pack_header_size = used + used_0;1478break;1479}14801481if(base_ref && (base_entry =packlist_find(&to_pack, base_ref, NULL))) {1482/*1483 * If base_ref was set above that means we wish to1484 * reuse delta data, and we even found that base1485 * in the list of objects we want to pack. Goodie!1486 *1487 * Depth value does not matter - find_deltas() will1488 * never consider reused delta as the base object to1489 * deltify other objects against, in order to avoid1490 * circular deltas.1491 */1492 entry->type = entry->in_pack_type;1493 entry->delta = base_entry;1494 entry->delta_size = entry->size;1495 entry->delta_sibling = base_entry->delta_child;1496 base_entry->delta_child = entry;1497unuse_pack(&w_curs);1498return;1499}15001501if(entry->type) {1502/*1503 * This must be a delta and we already know what the1504 * final object type is. Let's extract the actual1505 * object size from the delta header.1506 */1507 entry->size =get_size_from_delta(p, &w_curs,1508 entry->in_pack_offset + entry->in_pack_header_size);1509if(entry->size ==0)1510goto give_up;1511unuse_pack(&w_curs);1512return;1513}15141515/*1516 * No choice but to fall back to the recursive delta walk1517 * with sha1_object_info() to find about the object type1518 * at this point...1519 */1520 give_up:1521unuse_pack(&w_curs);1522}15231524 entry->type =oid_object_info(the_repository, &entry->idx.oid,1525&entry->size);1526/*1527 * The error condition is checked in prepare_pack(). This is1528 * to permit a missing preferred base object to be ignored1529 * as a preferred base. Doing so can result in a larger1530 * pack file, but the transfer will still take place.1531 */1532}15331534static intpack_offset_sort(const void*_a,const void*_b)1535{1536const struct object_entry *a = *(struct object_entry **)_a;1537const struct object_entry *b = *(struct object_entry **)_b;15381539/* avoid filesystem trashing with loose objects */1540if(!a->in_pack && !b->in_pack)1541returnoidcmp(&a->idx.oid, &b->idx.oid);15421543if(a->in_pack < b->in_pack)1544return-1;1545if(a->in_pack > b->in_pack)1546return1;1547return a->in_pack_offset < b->in_pack_offset ? -1:1548(a->in_pack_offset > b->in_pack_offset);1549}15501551/*1552 * Drop an on-disk delta we were planning to reuse. Naively, this would1553 * just involve blanking out the "delta" field, but we have to deal1554 * with some extra book-keeping:1555 *1556 * 1. Removing ourselves from the delta_sibling linked list.1557 *1558 * 2. Updating our size/type to the non-delta representation. These were1559 * either not recorded initially (size) or overwritten with the delta type1560 * (type) when check_object() decided to reuse the delta.1561 *1562 * 3. Resetting our delta depth, as we are now a base object.1563 */1564static voiddrop_reused_delta(struct object_entry *entry)1565{1566struct object_entry **p = &entry->delta->delta_child;1567struct object_info oi = OBJECT_INFO_INIT;15681569while(*p) {1570if(*p == entry)1571*p = (*p)->delta_sibling;1572else1573 p = &(*p)->delta_sibling;1574}1575 entry->delta = NULL;1576 entry->depth =0;15771578 oi.sizep = &entry->size;1579 oi.typep = &entry->type;1580if(packed_object_info(the_repository, entry->in_pack,1581 entry->in_pack_offset, &oi) <0) {1582/*1583 * We failed to get the info from this pack for some reason;1584 * fall back to sha1_object_info, which may find another copy.1585 * And if that fails, the error will be recorded in entry->type1586 * and dealt with in prepare_pack().1587 */1588 entry->type =oid_object_info(the_repository, &entry->idx.oid,1589&entry->size);1590}1591}15921593/*1594 * Follow the chain of deltas from this entry onward, throwing away any links1595 * that cause us to hit a cycle (as determined by the DFS state flags in1596 * the entries).1597 *1598 * We also detect too-long reused chains that would violate our --depth1599 * limit.1600 */1601static voidbreak_delta_chains(struct object_entry *entry)1602{1603/*1604 * The actual depth of each object we will write is stored as an int,1605 * as it cannot exceed our int "depth" limit. But before we break1606 * changes based no that limit, we may potentially go as deep as the1607 * number of objects, which is elsewhere bounded to a uint32_t.1608 */1609uint32_t total_depth;1610struct object_entry *cur, *next;16111612for(cur = entry, total_depth =0;1613 cur;1614 cur = cur->delta, total_depth++) {1615if(cur->dfs_state == DFS_DONE) {1616/*1617 * We've already seen this object and know it isn't1618 * part of a cycle. We do need to append its depth1619 * to our count.1620 */1621 total_depth += cur->depth;1622break;1623}16241625/*1626 * We break cycles before looping, so an ACTIVE state (or any1627 * other cruft which made its way into the state variable)1628 * is a bug.1629 */1630if(cur->dfs_state != DFS_NONE)1631die("BUG: confusing delta dfs state in first pass:%d",1632 cur->dfs_state);16331634/*1635 * Now we know this is the first time we've seen the object. If1636 * it's not a delta, we're done traversing, but we'll mark it1637 * done to save time on future traversals.1638 */1639if(!cur->delta) {1640 cur->dfs_state = DFS_DONE;1641break;1642}16431644/*1645 * Mark ourselves as active and see if the next step causes1646 * us to cycle to another active object. It's important to do1647 * this _before_ we loop, because it impacts where we make the1648 * cut, and thus how our total_depth counter works.1649 * E.g., We may see a partial loop like:1650 *1651 * A -> B -> C -> D -> B1652 *1653 * Cutting B->C breaks the cycle. But now the depth of A is1654 * only 1, and our total_depth counter is at 3. The size of the1655 * error is always one less than the size of the cycle we1656 * broke. Commits C and D were "lost" from A's chain.1657 *1658 * If we instead cut D->B, then the depth of A is correct at 3.1659 * We keep all commits in the chain that we examined.1660 */1661 cur->dfs_state = DFS_ACTIVE;1662if(cur->delta->dfs_state == DFS_ACTIVE) {1663drop_reused_delta(cur);1664 cur->dfs_state = DFS_DONE;1665break;1666}1667}16681669/*1670 * And now that we've gone all the way to the bottom of the chain, we1671 * need to clear the active flags and set the depth fields as1672 * appropriate. Unlike the loop above, which can quit when it drops a1673 * delta, we need to keep going to look for more depth cuts. So we need1674 * an extra "next" pointer to keep going after we reset cur->delta.1675 */1676for(cur = entry; cur; cur = next) {1677 next = cur->delta;16781679/*1680 * We should have a chain of zero or more ACTIVE states down to1681 * a final DONE. We can quit after the DONE, because either it1682 * has no bases, or we've already handled them in a previous1683 * call.1684 */1685if(cur->dfs_state == DFS_DONE)1686break;1687else if(cur->dfs_state != DFS_ACTIVE)1688die("BUG: confusing delta dfs state in second pass:%d",1689 cur->dfs_state);16901691/*1692 * If the total_depth is more than depth, then we need to snip1693 * the chain into two or more smaller chains that don't exceed1694 * the maximum depth. Most of the resulting chains will contain1695 * (depth + 1) entries (i.e., depth deltas plus one base), and1696 * the last chain (i.e., the one containing entry) will contain1697 * whatever entries are left over, namely1698 * (total_depth % (depth + 1)) of them.1699 *1700 * Since we are iterating towards decreasing depth, we need to1701 * decrement total_depth as we go, and we need to write to the1702 * entry what its final depth will be after all of the1703 * snipping. Since we're snipping into chains of length (depth1704 * + 1) entries, the final depth of an entry will be its1705 * original depth modulo (depth + 1). Any time we encounter an1706 * entry whose final depth is supposed to be zero, we snip it1707 * from its delta base, thereby making it so.1708 */1709 cur->depth = (total_depth--) % (depth +1);1710if(!cur->depth)1711drop_reused_delta(cur);17121713 cur->dfs_state = DFS_DONE;1714}1715}17161717static voidget_object_details(void)1718{1719uint32_t i;1720struct object_entry **sorted_by_offset;17211722if(progress)1723 progress_state =start_progress(_("Counting objects"),1724 to_pack.nr_objects);17251726 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1727for(i =0; i < to_pack.nr_objects; i++)1728 sorted_by_offset[i] = to_pack.objects + i;1729QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17301731for(i =0; i < to_pack.nr_objects; i++) {1732struct object_entry *entry = sorted_by_offset[i];1733check_object(entry);1734if(big_file_threshold < entry->size)1735 entry->no_try_delta =1;1736display_progress(progress_state, i +1);1737}1738stop_progress(&progress_state);17391740/*1741 * This must happen in a second pass, since we rely on the delta1742 * information for the whole list being completed.1743 */1744for(i =0; i < to_pack.nr_objects; i++)1745break_delta_chains(&to_pack.objects[i]);17461747free(sorted_by_offset);1748}17491750/*1751 * We search for deltas in a list sorted by type, by filename hash, and then1752 * by size, so that we see progressively smaller and smaller files.1753 * That's because we prefer deltas to be from the bigger file1754 * to the smaller -- deletes are potentially cheaper, but perhaps1755 * more importantly, the bigger file is likely the more recent1756 * one. The deepest deltas are therefore the oldest objects which are1757 * less susceptible to be accessed often.1758 */1759static inttype_size_sort(const void*_a,const void*_b)1760{1761const struct object_entry *a = *(struct object_entry **)_a;1762const struct object_entry *b = *(struct object_entry **)_b;17631764if(a->type > b->type)1765return-1;1766if(a->type < b->type)1767return1;1768if(a->hash > b->hash)1769return-1;1770if(a->hash < b->hash)1771return1;1772if(a->preferred_base > b->preferred_base)1773return-1;1774if(a->preferred_base < b->preferred_base)1775return1;1776if(a->size > b->size)1777return-1;1778if(a->size < b->size)1779return1;1780return a < b ? -1: (a > b);/* newest first */1781}17821783struct unpacked {1784struct object_entry *entry;1785void*data;1786struct delta_index *index;1787unsigned depth;1788};17891790static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1791unsigned long delta_size)1792{1793if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1794return0;17951796if(delta_size < cache_max_small_delta_size)1797return1;17981799/* cache delta, if objects are large enough compared to delta size */1800if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1801return1;18021803return0;1804}18051806#ifndef NO_PTHREADS18071808static pthread_mutex_t read_mutex;1809#define read_lock() pthread_mutex_lock(&read_mutex)1810#define read_unlock() pthread_mutex_unlock(&read_mutex)18111812static pthread_mutex_t cache_mutex;1813#define cache_lock() pthread_mutex_lock(&cache_mutex)1814#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18151816static pthread_mutex_t progress_mutex;1817#define progress_lock() pthread_mutex_lock(&progress_mutex)1818#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18191820#else18211822#define read_lock() (void)01823#define read_unlock() (void)01824#define cache_lock() (void)01825#define cache_unlock() (void)01826#define progress_lock() (void)01827#define progress_unlock() (void)018281829#endif18301831static inttry_delta(struct unpacked *trg,struct unpacked *src,1832unsigned max_depth,unsigned long*mem_usage)1833{1834struct object_entry *trg_entry = trg->entry;1835struct object_entry *src_entry = src->entry;1836unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1837unsigned ref_depth;1838enum object_type type;1839void*delta_buf;18401841/* Don't bother doing diffs between different types */1842if(trg_entry->type != src_entry->type)1843return-1;18441845/*1846 * We do not bother to try a delta that we discarded on an1847 * earlier try, but only when reusing delta data. Note that1848 * src_entry that is marked as the preferred_base should always1849 * be considered, as even if we produce a suboptimal delta against1850 * it, we will still save the transfer cost, as we already know1851 * the other side has it and we won't send src_entry at all.1852 */1853if(reuse_delta && trg_entry->in_pack &&1854 trg_entry->in_pack == src_entry->in_pack &&1855!src_entry->preferred_base &&1856 trg_entry->in_pack_type != OBJ_REF_DELTA &&1857 trg_entry->in_pack_type != OBJ_OFS_DELTA)1858return0;18591860/* Let's not bust the allowed depth. */1861if(src->depth >= max_depth)1862return0;18631864/* Now some size filtering heuristics. */1865 trg_size = trg_entry->size;1866if(!trg_entry->delta) {1867 max_size = trg_size/2-20;1868 ref_depth =1;1869}else{1870 max_size = trg_entry->delta_size;1871 ref_depth = trg->depth;1872}1873 max_size = (uint64_t)max_size * (max_depth - src->depth) /1874(max_depth - ref_depth +1);1875if(max_size ==0)1876return0;1877 src_size = src_entry->size;1878 sizediff = src_size < trg_size ? trg_size - src_size :0;1879if(sizediff >= max_size)1880return0;1881if(trg_size < src_size /32)1882return0;18831884/* Load data if not already done */1885if(!trg->data) {1886read_lock();1887 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1888read_unlock();1889if(!trg->data)1890die("object%scannot be read",1891oid_to_hex(&trg_entry->idx.oid));1892if(sz != trg_size)1893die("object%sinconsistent object length (%lu vs%lu)",1894oid_to_hex(&trg_entry->idx.oid), sz,1895 trg_size);1896*mem_usage += sz;1897}1898if(!src->data) {1899read_lock();1900 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);1901read_unlock();1902if(!src->data) {1903if(src_entry->preferred_base) {1904static int warned =0;1905if(!warned++)1906warning("object%scannot be read",1907oid_to_hex(&src_entry->idx.oid));1908/*1909 * Those objects are not included in the1910 * resulting pack. Be resilient and ignore1911 * them if they can't be read, in case the1912 * pack could be created nevertheless.1913 */1914return0;1915}1916die("object%scannot be read",1917oid_to_hex(&src_entry->idx.oid));1918}1919if(sz != src_size)1920die("object%sinconsistent object length (%lu vs%lu)",1921oid_to_hex(&src_entry->idx.oid), sz,1922 src_size);1923*mem_usage += sz;1924}1925if(!src->index) {1926 src->index =create_delta_index(src->data, src_size);1927if(!src->index) {1928static int warned =0;1929if(!warned++)1930warning("suboptimal pack - out of memory");1931return0;1932}1933*mem_usage +=sizeof_delta_index(src->index);1934}19351936 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);1937if(!delta_buf)1938return0;19391940if(trg_entry->delta) {1941/* Prefer only shallower same-sized deltas. */1942if(delta_size == trg_entry->delta_size &&1943 src->depth +1>= trg->depth) {1944free(delta_buf);1945return0;1946}1947}19481949/*1950 * Handle memory allocation outside of the cache1951 * accounting lock. Compiler will optimize the strangeness1952 * away when NO_PTHREADS is defined.1953 */1954free(trg_entry->delta_data);1955cache_lock();1956if(trg_entry->delta_data) {1957 delta_cache_size -= trg_entry->delta_size;1958 trg_entry->delta_data = NULL;1959}1960if(delta_cacheable(src_size, trg_size, delta_size)) {1961 delta_cache_size += delta_size;1962cache_unlock();1963 trg_entry->delta_data =xrealloc(delta_buf, delta_size);1964}else{1965cache_unlock();1966free(delta_buf);1967}19681969 trg_entry->delta = src_entry;1970 trg_entry->delta_size = delta_size;1971 trg->depth = src->depth +1;19721973return1;1974}19751976static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)1977{1978struct object_entry *child = me->delta_child;1979unsigned int m = n;1980while(child) {1981unsigned int c =check_delta_limit(child, n +1);1982if(m < c)1983 m = c;1984 child = child->delta_sibling;1985}1986return m;1987}19881989static unsigned longfree_unpacked(struct unpacked *n)1990{1991unsigned long freed_mem =sizeof_delta_index(n->index);1992free_delta_index(n->index);1993 n->index = NULL;1994if(n->data) {1995 freed_mem += n->entry->size;1996FREE_AND_NULL(n->data);1997}1998 n->entry = NULL;1999 n->depth =0;2000return freed_mem;2001}20022003static voidfind_deltas(struct object_entry **list,unsigned*list_size,2004int window,int depth,unsigned*processed)2005{2006uint32_t i, idx =0, count =0;2007struct unpacked *array;2008unsigned long mem_usage =0;20092010 array =xcalloc(window,sizeof(struct unpacked));20112012for(;;) {2013struct object_entry *entry;2014struct unpacked *n = array + idx;2015int j, max_depth, best_base = -1;20162017progress_lock();2018if(!*list_size) {2019progress_unlock();2020break;2021}2022 entry = *list++;2023(*list_size)--;2024if(!entry->preferred_base) {2025(*processed)++;2026display_progress(progress_state, *processed);2027}2028progress_unlock();20292030 mem_usage -=free_unpacked(n);2031 n->entry = entry;20322033while(window_memory_limit &&2034 mem_usage > window_memory_limit &&2035 count >1) {2036uint32_t tail = (idx + window - count) % window;2037 mem_usage -=free_unpacked(array + tail);2038 count--;2039}20402041/* We do not compute delta to *create* objects we are not2042 * going to pack.2043 */2044if(entry->preferred_base)2045goto next;20462047/*2048 * If the current object is at pack edge, take the depth the2049 * objects that depend on the current object into account2050 * otherwise they would become too deep.2051 */2052 max_depth = depth;2053if(entry->delta_child) {2054 max_depth -=check_delta_limit(entry,0);2055if(max_depth <=0)2056goto next;2057}20582059 j = window;2060while(--j >0) {2061int ret;2062uint32_t other_idx = idx + j;2063struct unpacked *m;2064if(other_idx >= window)2065 other_idx -= window;2066 m = array + other_idx;2067if(!m->entry)2068break;2069 ret =try_delta(n, m, max_depth, &mem_usage);2070if(ret <0)2071break;2072else if(ret >0)2073 best_base = other_idx;2074}20752076/*2077 * If we decided to cache the delta data, then it is best2078 * to compress it right away. First because we have to do2079 * it anyway, and doing it here while we're threaded will2080 * save a lot of time in the non threaded write phase,2081 * as well as allow for caching more deltas within2082 * the same cache size limit.2083 * ...2084 * But only if not writing to stdout, since in that case2085 * the network is most likely throttling writes anyway,2086 * and therefore it is best to go to the write phase ASAP2087 * instead, as we can afford spending more time compressing2088 * between writes at that moment.2089 */2090if(entry->delta_data && !pack_to_stdout) {2091 entry->z_delta_size =do_compress(&entry->delta_data,2092 entry->delta_size);2093cache_lock();2094 delta_cache_size -= entry->delta_size;2095 delta_cache_size += entry->z_delta_size;2096cache_unlock();2097}20982099/* if we made n a delta, and if n is already at max2100 * depth, leaving it in the window is pointless. we2101 * should evict it first.2102 */2103if(entry->delta && max_depth <= n->depth)2104continue;21052106/*2107 * Move the best delta base up in the window, after the2108 * currently deltified object, to keep it longer. It will2109 * be the first base object to be attempted next.2110 */2111if(entry->delta) {2112struct unpacked swap = array[best_base];2113int dist = (window + idx - best_base) % window;2114int dst = best_base;2115while(dist--) {2116int src = (dst +1) % window;2117 array[dst] = array[src];2118 dst = src;2119}2120 array[dst] = swap;2121}21222123 next:2124 idx++;2125if(count +1< window)2126 count++;2127if(idx >= window)2128 idx =0;2129}21302131for(i =0; i < window; ++i) {2132free_delta_index(array[i].index);2133free(array[i].data);2134}2135free(array);2136}21372138#ifndef NO_PTHREADS21392140static voidtry_to_free_from_threads(size_t size)2141{2142read_lock();2143release_pack_memory(size);2144read_unlock();2145}21462147static try_to_free_t old_try_to_free_routine;21482149/*2150 * The main thread waits on the condition that (at least) one of the workers2151 * has stopped working (which is indicated in the .working member of2152 * struct thread_params).2153 * When a work thread has completed its work, it sets .working to 0 and2154 * signals the main thread and waits on the condition that .data_ready2155 * becomes 1.2156 */21572158struct thread_params {2159 pthread_t thread;2160struct object_entry **list;2161unsigned list_size;2162unsigned remaining;2163int window;2164int depth;2165int working;2166int data_ready;2167 pthread_mutex_t mutex;2168 pthread_cond_t cond;2169unsigned*processed;2170};21712172static pthread_cond_t progress_cond;21732174/*2175 * Mutex and conditional variable can't be statically-initialized on Windows.2176 */2177static voidinit_threaded_search(void)2178{2179init_recursive_mutex(&read_mutex);2180pthread_mutex_init(&cache_mutex, NULL);2181pthread_mutex_init(&progress_mutex, NULL);2182pthread_cond_init(&progress_cond, NULL);2183 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2184}21852186static voidcleanup_threaded_search(void)2187{2188set_try_to_free_routine(old_try_to_free_routine);2189pthread_cond_destroy(&progress_cond);2190pthread_mutex_destroy(&read_mutex);2191pthread_mutex_destroy(&cache_mutex);2192pthread_mutex_destroy(&progress_mutex);2193}21942195static void*threaded_find_deltas(void*arg)2196{2197struct thread_params *me = arg;21982199progress_lock();2200while(me->remaining) {2201progress_unlock();22022203find_deltas(me->list, &me->remaining,2204 me->window, me->depth, me->processed);22052206progress_lock();2207 me->working =0;2208pthread_cond_signal(&progress_cond);2209progress_unlock();22102211/*2212 * We must not set ->data_ready before we wait on the2213 * condition because the main thread may have set it to 12214 * before we get here. In order to be sure that new2215 * work is available if we see 1 in ->data_ready, it2216 * was initialized to 0 before this thread was spawned2217 * and we reset it to 0 right away.2218 */2219pthread_mutex_lock(&me->mutex);2220while(!me->data_ready)2221pthread_cond_wait(&me->cond, &me->mutex);2222 me->data_ready =0;2223pthread_mutex_unlock(&me->mutex);22242225progress_lock();2226}2227progress_unlock();2228/* leave ->working 1 so that this doesn't get more work assigned */2229return NULL;2230}22312232static voidll_find_deltas(struct object_entry **list,unsigned list_size,2233int window,int depth,unsigned*processed)2234{2235struct thread_params *p;2236int i, ret, active_threads =0;22372238init_threaded_search();22392240if(delta_search_threads <=1) {2241find_deltas(list, &list_size, window, depth, processed);2242cleanup_threaded_search();2243return;2244}2245if(progress > pack_to_stdout)2246fprintf(stderr,"Delta compression using up to%dthreads.\n",2247 delta_search_threads);2248 p =xcalloc(delta_search_threads,sizeof(*p));22492250/* Partition the work amongst work threads. */2251for(i =0; i < delta_search_threads; i++) {2252unsigned sub_size = list_size / (delta_search_threads - i);22532254/* don't use too small segments or no deltas will be found */2255if(sub_size <2*window && i+1< delta_search_threads)2256 sub_size =0;22572258 p[i].window = window;2259 p[i].depth = depth;2260 p[i].processed = processed;2261 p[i].working =1;2262 p[i].data_ready =0;22632264/* try to split chunks on "path" boundaries */2265while(sub_size && sub_size < list_size &&2266 list[sub_size]->hash &&2267 list[sub_size]->hash == list[sub_size-1]->hash)2268 sub_size++;22692270 p[i].list = list;2271 p[i].list_size = sub_size;2272 p[i].remaining = sub_size;22732274 list += sub_size;2275 list_size -= sub_size;2276}22772278/* Start work threads. */2279for(i =0; i < delta_search_threads; i++) {2280if(!p[i].list_size)2281continue;2282pthread_mutex_init(&p[i].mutex, NULL);2283pthread_cond_init(&p[i].cond, NULL);2284 ret =pthread_create(&p[i].thread, NULL,2285 threaded_find_deltas, &p[i]);2286if(ret)2287die("unable to create thread:%s",strerror(ret));2288 active_threads++;2289}22902291/*2292 * Now let's wait for work completion. Each time a thread is done2293 * with its work, we steal half of the remaining work from the2294 * thread with the largest number of unprocessed objects and give2295 * it to that newly idle thread. This ensure good load balancing2296 * until the remaining object list segments are simply too short2297 * to be worth splitting anymore.2298 */2299while(active_threads) {2300struct thread_params *target = NULL;2301struct thread_params *victim = NULL;2302unsigned sub_size =0;23032304progress_lock();2305for(;;) {2306for(i =0; !target && i < delta_search_threads; i++)2307if(!p[i].working)2308 target = &p[i];2309if(target)2310break;2311pthread_cond_wait(&progress_cond, &progress_mutex);2312}23132314for(i =0; i < delta_search_threads; i++)2315if(p[i].remaining >2*window &&2316(!victim || victim->remaining < p[i].remaining))2317 victim = &p[i];2318if(victim) {2319 sub_size = victim->remaining /2;2320 list = victim->list + victim->list_size - sub_size;2321while(sub_size && list[0]->hash &&2322 list[0]->hash == list[-1]->hash) {2323 list++;2324 sub_size--;2325}2326if(!sub_size) {2327/*2328 * It is possible for some "paths" to have2329 * so many objects that no hash boundary2330 * might be found. Let's just steal the2331 * exact half in that case.2332 */2333 sub_size = victim->remaining /2;2334 list -= sub_size;2335}2336 target->list = list;2337 victim->list_size -= sub_size;2338 victim->remaining -= sub_size;2339}2340 target->list_size = sub_size;2341 target->remaining = sub_size;2342 target->working =1;2343progress_unlock();23442345pthread_mutex_lock(&target->mutex);2346 target->data_ready =1;2347pthread_cond_signal(&target->cond);2348pthread_mutex_unlock(&target->mutex);23492350if(!sub_size) {2351pthread_join(target->thread, NULL);2352pthread_cond_destroy(&target->cond);2353pthread_mutex_destroy(&target->mutex);2354 active_threads--;2355}2356}2357cleanup_threaded_search();2358free(p);2359}23602361#else2362#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2363#endif23642365static voidadd_tag_chain(const struct object_id *oid)2366{2367struct tag *tag;23682369/*2370 * We catch duplicates already in add_object_entry(), but we'd2371 * prefer to do this extra check to avoid having to parse the2372 * tag at all if we already know that it's being packed (e.g., if2373 * it was included via bitmaps, we would not have parsed it2374 * previously).2375 */2376if(packlist_find(&to_pack, oid->hash, NULL))2377return;23782379 tag =lookup_tag(oid);2380while(1) {2381if(!tag ||parse_tag(tag) || !tag->tagged)2382die("unable to pack objects reachable from tag%s",2383oid_to_hex(oid));23842385add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);23862387if(tag->tagged->type != OBJ_TAG)2388return;23892390 tag = (struct tag *)tag->tagged;2391}2392}23932394static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2395{2396struct object_id peeled;23972398if(starts_with(path,"refs/tags/") &&/* is a tag? */2399!peel_ref(path, &peeled) &&/* peelable? */2400packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2401add_tag_chain(oid);2402return0;2403}24042405static voidprepare_pack(int window,int depth)2406{2407struct object_entry **delta_list;2408uint32_t i, nr_deltas;2409unsigned n;24102411get_object_details();24122413/*2414 * If we're locally repacking then we need to be doubly careful2415 * from now on in order to make sure no stealth corruption gets2416 * propagated to the new pack. Clients receiving streamed packs2417 * should validate everything they get anyway so no need to incur2418 * the additional cost here in that case.2419 */2420if(!pack_to_stdout)2421 do_check_packed_object_crc =1;24222423if(!to_pack.nr_objects || !window || !depth)2424return;24252426ALLOC_ARRAY(delta_list, to_pack.nr_objects);2427 nr_deltas = n =0;24282429for(i =0; i < to_pack.nr_objects; i++) {2430struct object_entry *entry = to_pack.objects + i;24312432if(entry->delta)2433/* This happens if we decided to reuse existing2434 * delta from a pack. "reuse_delta &&" is implied.2435 */2436continue;24372438if(entry->size <50)2439continue;24402441if(entry->no_try_delta)2442continue;24432444if(!entry->preferred_base) {2445 nr_deltas++;2446if(entry->type <0)2447die("unable to get type of object%s",2448oid_to_hex(&entry->idx.oid));2449}else{2450if(entry->type <0) {2451/*2452 * This object is not found, but we2453 * don't have to include it anyway.2454 */2455continue;2456}2457}24582459 delta_list[n++] = entry;2460}24612462if(nr_deltas && n >1) {2463unsigned nr_done =0;2464if(progress)2465 progress_state =start_progress(_("Compressing objects"),2466 nr_deltas);2467QSORT(delta_list, n, type_size_sort);2468ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2469stop_progress(&progress_state);2470if(nr_done != nr_deltas)2471die("inconsistency with delta count");2472}2473free(delta_list);2474}24752476static intgit_pack_config(const char*k,const char*v,void*cb)2477{2478if(!strcmp(k,"pack.window")) {2479 window =git_config_int(k, v);2480return0;2481}2482if(!strcmp(k,"pack.windowmemory")) {2483 window_memory_limit =git_config_ulong(k, v);2484return0;2485}2486if(!strcmp(k,"pack.depth")) {2487 depth =git_config_int(k, v);2488return0;2489}2490if(!strcmp(k,"pack.deltacachesize")) {2491 max_delta_cache_size =git_config_int(k, v);2492return0;2493}2494if(!strcmp(k,"pack.deltacachelimit")) {2495 cache_max_small_delta_size =git_config_int(k, v);2496return0;2497}2498if(!strcmp(k,"pack.writebitmaphashcache")) {2499if(git_config_bool(k, v))2500 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2501else2502 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2503}2504if(!strcmp(k,"pack.usebitmaps")) {2505 use_bitmap_index_default =git_config_bool(k, v);2506return0;2507}2508if(!strcmp(k,"pack.threads")) {2509 delta_search_threads =git_config_int(k, v);2510if(delta_search_threads <0)2511die("invalid number of threads specified (%d)",2512 delta_search_threads);2513#ifdef NO_PTHREADS2514if(delta_search_threads !=1) {2515warning("no threads support, ignoring%s", k);2516 delta_search_threads =0;2517}2518#endif2519return0;2520}2521if(!strcmp(k,"pack.indexversion")) {2522 pack_idx_opts.version =git_config_int(k, v);2523if(pack_idx_opts.version >2)2524die("bad pack.indexversion=%"PRIu32,2525 pack_idx_opts.version);2526return0;2527}2528returngit_default_config(k, v, cb);2529}25302531static voidread_object_list_from_stdin(void)2532{2533char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2534struct object_id oid;2535const char*p;25362537for(;;) {2538if(!fgets(line,sizeof(line), stdin)) {2539if(feof(stdin))2540break;2541if(!ferror(stdin))2542die("fgets returned NULL, not EOF, not error!");2543if(errno != EINTR)2544die_errno("fgets");2545clearerr(stdin);2546continue;2547}2548if(line[0] =='-') {2549if(get_oid_hex(line+1, &oid))2550die("expected edge object ID, got garbage:\n%s",2551 line);2552add_preferred_base(&oid);2553continue;2554}2555if(parse_oid_hex(line, &oid, &p))2556die("expected object ID, got garbage:\n%s", line);25572558add_preferred_base_object(p +1);2559add_object_entry(&oid,0, p +1,0);2560}2561}25622563/* Remember to update object flag allocation in object.h */2564#define OBJECT_ADDED (1u<<20)25652566static voidshow_commit(struct commit *commit,void*data)2567{2568add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2569 commit->object.flags |= OBJECT_ADDED;25702571if(write_bitmap_index)2572index_commit_for_bitmap(commit);2573}25742575static voidshow_object(struct object *obj,const char*name,void*data)2576{2577add_preferred_base_object(name);2578add_object_entry(&obj->oid, obj->type, name,0);2579 obj->flags |= OBJECT_ADDED;2580}25812582static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2583{2584assert(arg_missing_action == MA_ALLOW_ANY);25852586/*2587 * Quietly ignore ALL missing objects. This avoids problems with2588 * staging them now and getting an odd error later.2589 */2590if(!has_object_file(&obj->oid))2591return;25922593show_object(obj, name, data);2594}25952596static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2597{2598assert(arg_missing_action == MA_ALLOW_PROMISOR);25992600/*2601 * Quietly ignore EXPECTED missing objects. This avoids problems with2602 * staging them now and getting an odd error later.2603 */2604if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2605return;26062607show_object(obj, name, data);2608}26092610static intoption_parse_missing_action(const struct option *opt,2611const char*arg,int unset)2612{2613assert(arg);2614assert(!unset);26152616if(!strcmp(arg,"error")) {2617 arg_missing_action = MA_ERROR;2618 fn_show_object = show_object;2619return0;2620}26212622if(!strcmp(arg,"allow-any")) {2623 arg_missing_action = MA_ALLOW_ANY;2624 fetch_if_missing =0;2625 fn_show_object = show_object__ma_allow_any;2626return0;2627}26282629if(!strcmp(arg,"allow-promisor")) {2630 arg_missing_action = MA_ALLOW_PROMISOR;2631 fetch_if_missing =0;2632 fn_show_object = show_object__ma_allow_promisor;2633return0;2634}26352636die(_("invalid value for --missing"));2637return0;2638}26392640static voidshow_edge(struct commit *commit)2641{2642add_preferred_base(&commit->object.oid);2643}26442645struct in_pack_object {2646 off_t offset;2647struct object *object;2648};26492650struct in_pack {2651unsigned int alloc;2652unsigned int nr;2653struct in_pack_object *array;2654};26552656static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2657{2658 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2659 in_pack->array[in_pack->nr].object = object;2660 in_pack->nr++;2661}26622663/*2664 * Compare the objects in the offset order, in order to emulate the2665 * "git rev-list --objects" output that produced the pack originally.2666 */2667static intofscmp(const void*a_,const void*b_)2668{2669struct in_pack_object *a = (struct in_pack_object *)a_;2670struct in_pack_object *b = (struct in_pack_object *)b_;26712672if(a->offset < b->offset)2673return-1;2674else if(a->offset > b->offset)2675return1;2676else2677returnoidcmp(&a->object->oid, &b->object->oid);2678}26792680static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2681{2682struct packed_git *p;2683struct in_pack in_pack;2684uint32_t i;26852686memset(&in_pack,0,sizeof(in_pack));26872688for(p =get_packed_git(the_repository); p; p = p->next) {2689struct object_id oid;2690struct object *o;26912692if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2693continue;2694if(open_pack_index(p))2695die("cannot open pack index");26962697ALLOC_GROW(in_pack.array,2698 in_pack.nr + p->num_objects,2699 in_pack.alloc);27002701for(i =0; i < p->num_objects; i++) {2702nth_packed_object_oid(&oid, p, i);2703 o =lookup_unknown_object(oid.hash);2704if(!(o->flags & OBJECT_ADDED))2705mark_in_pack_object(o, p, &in_pack);2706 o->flags |= OBJECT_ADDED;2707}2708}27092710if(in_pack.nr) {2711QSORT(in_pack.array, in_pack.nr, ofscmp);2712for(i =0; i < in_pack.nr; i++) {2713struct object *o = in_pack.array[i].object;2714add_object_entry(&o->oid, o->type,"",0);2715}2716}2717free(in_pack.array);2718}27192720static intadd_loose_object(const struct object_id *oid,const char*path,2721void*data)2722{2723enum object_type type =oid_object_info(the_repository, oid, NULL);27242725if(type <0) {2726warning("loose object at%scould not be examined", path);2727return0;2728}27292730add_object_entry(oid, type,"",0);2731return0;2732}27332734/*2735 * We actually don't even have to worry about reachability here.2736 * add_object_entry will weed out duplicates, so we just add every2737 * loose object we find.2738 */2739static voidadd_unreachable_loose_objects(void)2740{2741for_each_loose_file_in_objdir(get_object_directory(),2742 add_loose_object,2743 NULL, NULL, NULL);2744}27452746static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2747{2748static struct packed_git *last_found = (void*)1;2749struct packed_git *p;27502751 p = (last_found != (void*)1) ? last_found :2752get_packed_git(the_repository);27532754while(p) {2755if((!p->pack_local || p->pack_keep ||2756 p->pack_keep_in_core) &&2757find_pack_entry_one(oid->hash, p)) {2758 last_found = p;2759return1;2760}2761if(p == last_found)2762 p =get_packed_git(the_repository);2763else2764 p = p->next;2765if(p == last_found)2766 p = p->next;2767}2768return0;2769}27702771/*2772 * Store a list of sha1s that are should not be discarded2773 * because they are either written too recently, or are2774 * reachable from another object that was.2775 *2776 * This is filled by get_object_list.2777 */2778static struct oid_array recent_objects;27792780static intloosened_object_can_be_discarded(const struct object_id *oid,2781 timestamp_t mtime)2782{2783if(!unpack_unreachable_expiration)2784return0;2785if(mtime > unpack_unreachable_expiration)2786return0;2787if(oid_array_lookup(&recent_objects, oid) >=0)2788return0;2789return1;2790}27912792static voidloosen_unused_packed_objects(struct rev_info *revs)2793{2794struct packed_git *p;2795uint32_t i;2796struct object_id oid;27972798for(p =get_packed_git(the_repository); p; p = p->next) {2799if(!p->pack_local || p->pack_keep || p->pack_keep_in_core)2800continue;28012802if(open_pack_index(p))2803die("cannot open pack index");28042805for(i =0; i < p->num_objects; i++) {2806nth_packed_object_oid(&oid, p, i);2807if(!packlist_find(&to_pack, oid.hash, NULL) &&2808!has_sha1_pack_kept_or_nonlocal(&oid) &&2809!loosened_object_can_be_discarded(&oid, p->mtime))2810if(force_object_loose(&oid, p->mtime))2811die("unable to force loose object");2812}2813}2814}28152816/*2817 * This tracks any options which pack-reuse code expects to be on, or which a2818 * reader of the pack might not understand, and which would therefore prevent2819 * blind reuse of what we have on disk.2820 */2821static intpack_options_allow_reuse(void)2822{2823return pack_to_stdout &&2824 allow_ofs_delta &&2825!ignore_packed_keep_on_disk &&2826!ignore_packed_keep_in_core &&2827(!local || !have_non_local_packs) &&2828!incremental;2829}28302831static intget_object_list_from_bitmap(struct rev_info *revs)2832{2833if(prepare_bitmap_walk(revs) <0)2834return-1;28352836if(pack_options_allow_reuse() &&2837!reuse_partial_packfile_from_bitmap(2838&reuse_packfile,2839&reuse_packfile_objects,2840&reuse_packfile_offset)) {2841assert(reuse_packfile_objects);2842 nr_result += reuse_packfile_objects;2843display_progress(progress_state, nr_result);2844}28452846traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2847return0;2848}28492850static voidrecord_recent_object(struct object *obj,2851const char*name,2852void*data)2853{2854oid_array_append(&recent_objects, &obj->oid);2855}28562857static voidrecord_recent_commit(struct commit *commit,void*data)2858{2859oid_array_append(&recent_objects, &commit->object.oid);2860}28612862static voidget_object_list(int ac,const char**av)2863{2864struct rev_info revs;2865char line[1000];2866int flags =0;28672868init_revisions(&revs, NULL);2869 save_commit_buffer =0;2870setup_revisions(ac, av, &revs, NULL);28712872/* make sure shallows are read */2873is_repository_shallow();28742875while(fgets(line,sizeof(line), stdin) != NULL) {2876int len =strlen(line);2877if(len && line[len -1] =='\n')2878 line[--len] =0;2879if(!len)2880break;2881if(*line =='-') {2882if(!strcmp(line,"--not")) {2883 flags ^= UNINTERESTING;2884 write_bitmap_index =0;2885continue;2886}2887if(starts_with(line,"--shallow ")) {2888struct object_id oid;2889if(get_oid_hex(line +10, &oid))2890die("not an SHA-1 '%s'", line +10);2891register_shallow(&oid);2892 use_bitmap_index =0;2893continue;2894}2895die("not a rev '%s'", line);2896}2897if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2898die("bad revision '%s'", line);2899}29002901if(use_bitmap_index && !get_object_list_from_bitmap(&revs))2902return;29032904if(prepare_revision_walk(&revs))2905die("revision walk setup failed");2906mark_edges_uninteresting(&revs, show_edge);29072908if(!fn_show_object)2909 fn_show_object = show_object;2910traverse_commit_list_filtered(&filter_options, &revs,2911 show_commit, fn_show_object, NULL,2912 NULL);29132914if(unpack_unreachable_expiration) {2915 revs.ignore_missing_links =1;2916if(add_unseen_recent_objects_to_traversal(&revs,2917 unpack_unreachable_expiration))2918die("unable to add recent objects");2919if(prepare_revision_walk(&revs))2920die("revision walk setup failed");2921traverse_commit_list(&revs, record_recent_commit,2922 record_recent_object, NULL);2923}29242925if(keep_unreachable)2926add_objects_in_unpacked_packs(&revs);2927if(pack_loose_unreachable)2928add_unreachable_loose_objects();2929if(unpack_unreachable)2930loosen_unused_packed_objects(&revs);29312932oid_array_clear(&recent_objects);2933}29342935static voidadd_extra_kept_packs(const struct string_list *names)2936{2937struct packed_git *p;29382939if(!names->nr)2940return;29412942for(p =get_packed_git(the_repository); p; p = p->next) {2943const char*name =basename(p->pack_name);2944int i;29452946if(!p->pack_local)2947continue;29482949for(i =0; i < names->nr; i++)2950if(!fspathcmp(name, names->items[i].string))2951break;29522953if(i < names->nr) {2954 p->pack_keep_in_core =1;2955 ignore_packed_keep_in_core =1;2956continue;2957}2958}2959}29602961static intoption_parse_index_version(const struct option *opt,2962const char*arg,int unset)2963{2964char*c;2965const char*val = arg;2966 pack_idx_opts.version =strtoul(val, &c,10);2967if(pack_idx_opts.version >2)2968die(_("unsupported index version%s"), val);2969if(*c ==','&& c[1])2970 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);2971if(*c || pack_idx_opts.off32_limit &0x80000000)2972die(_("bad index version '%s'"), val);2973return0;2974}29752976static intoption_parse_unpack_unreachable(const struct option *opt,2977const char*arg,int unset)2978{2979if(unset) {2980 unpack_unreachable =0;2981 unpack_unreachable_expiration =0;2982}2983else{2984 unpack_unreachable =1;2985if(arg)2986 unpack_unreachable_expiration =approxidate(arg);2987}2988return0;2989}29902991intcmd_pack_objects(int argc,const char**argv,const char*prefix)2992{2993int use_internal_rev_list =0;2994int thin =0;2995int shallow =0;2996int all_progress_implied =0;2997struct argv_array rp = ARGV_ARRAY_INIT;2998int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;2999int rev_list_index =0;3000struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;3001struct option pack_objects_options[] = {3002OPT_SET_INT('q',"quiet", &progress,3003N_("do not show progress meter"),0),3004OPT_SET_INT(0,"progress", &progress,3005N_("show progress meter"),1),3006OPT_SET_INT(0,"all-progress", &progress,3007N_("show progress meter during object writing phase"),2),3008OPT_BOOL(0,"all-progress-implied",3009&all_progress_implied,3010N_("similar to --all-progress when progress meter is shown")),3011{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),3012N_("write the pack index file in the specified idx format version"),30130, option_parse_index_version },3014OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,3015N_("maximum size of each output pack file")),3016OPT_BOOL(0,"local", &local,3017N_("ignore borrowed objects from alternate object store")),3018OPT_BOOL(0,"incremental", &incremental,3019N_("ignore packed objects")),3020OPT_INTEGER(0,"window", &window,3021N_("limit pack window by objects")),3022OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,3023N_("limit pack window by memory in addition to object limit")),3024OPT_INTEGER(0,"depth", &depth,3025N_("maximum length of delta chain allowed in the resulting pack")),3026OPT_BOOL(0,"reuse-delta", &reuse_delta,3027N_("reuse existing deltas")),3028OPT_BOOL(0,"reuse-object", &reuse_object,3029N_("reuse existing objects")),3030OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,3031N_("use OFS_DELTA objects")),3032OPT_INTEGER(0,"threads", &delta_search_threads,3033N_("use threads when searching for best delta matches")),3034OPT_BOOL(0,"non-empty", &non_empty,3035N_("do not create an empty pack output")),3036OPT_BOOL(0,"revs", &use_internal_rev_list,3037N_("read revision arguments from standard input")),3038{ OPTION_SET_INT,0,"unpacked", &rev_list_unpacked, NULL,3039N_("limit the objects to those that are not yet packed"),3040 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3041{ OPTION_SET_INT,0,"all", &rev_list_all, NULL,3042N_("include objects reachable from any reference"),3043 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3044{ OPTION_SET_INT,0,"reflog", &rev_list_reflog, NULL,3045N_("include objects referred by reflog entries"),3046 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3047{ OPTION_SET_INT,0,"indexed-objects", &rev_list_index, NULL,3048N_("include objects referred to by the index"),3049 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3050OPT_BOOL(0,"stdout", &pack_to_stdout,3051N_("output pack to stdout")),3052OPT_BOOL(0,"include-tag", &include_tag,3053N_("include tag objects that refer to objects to be packed")),3054OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3055N_("keep unreachable objects")),3056OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3057N_("pack loose unreachable objects")),3058{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3059N_("unpack unreachable objects newer than <time>"),3060 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3061OPT_BOOL(0,"thin", &thin,3062N_("create thin packs")),3063OPT_BOOL(0,"shallow", &shallow,3064N_("create packs suitable for shallow fetches")),3065OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep_on_disk,3066N_("ignore packs that have companion .keep file")),3067OPT_STRING_LIST(0,"keep-pack", &keep_pack_list,N_("name"),3068N_("ignore this pack")),3069OPT_INTEGER(0,"compression", &pack_compression_level,3070N_("pack compression level")),3071OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3072N_("do not hide commits by grafts"),0),3073OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3074N_("use a bitmap index if available to speed up counting objects")),3075OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3076N_("write a bitmap index together with the pack index")),3077OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3078{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3079N_("handling for missing objects"), PARSE_OPT_NONEG,3080 option_parse_missing_action },3081OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3082N_("do not pack objects in promisor packfiles")),3083OPT_END(),3084};30853086 check_replace_refs =0;30873088reset_pack_idx_option(&pack_idx_opts);3089git_config(git_pack_config, NULL);30903091 progress =isatty(2);3092 argc =parse_options(argc, argv, prefix, pack_objects_options,3093 pack_usage,0);30943095if(argc) {3096 base_name = argv[0];3097 argc--;3098}3099if(pack_to_stdout != !base_name || argc)3100usage_with_options(pack_usage, pack_objects_options);31013102argv_array_push(&rp,"pack-objects");3103if(thin) {3104 use_internal_rev_list =1;3105argv_array_push(&rp, shallow3106?"--objects-edge-aggressive"3107:"--objects-edge");3108}else3109argv_array_push(&rp,"--objects");31103111if(rev_list_all) {3112 use_internal_rev_list =1;3113argv_array_push(&rp,"--all");3114}3115if(rev_list_reflog) {3116 use_internal_rev_list =1;3117argv_array_push(&rp,"--reflog");3118}3119if(rev_list_index) {3120 use_internal_rev_list =1;3121argv_array_push(&rp,"--indexed-objects");3122}3123if(rev_list_unpacked) {3124 use_internal_rev_list =1;3125argv_array_push(&rp,"--unpacked");3126}31273128if(exclude_promisor_objects) {3129 use_internal_rev_list =1;3130 fetch_if_missing =0;3131argv_array_push(&rp,"--exclude-promisor-objects");3132}31333134if(!reuse_object)3135 reuse_delta =0;3136if(pack_compression_level == -1)3137 pack_compression_level = Z_DEFAULT_COMPRESSION;3138else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3139die("bad pack compression level%d", pack_compression_level);31403141if(!delta_search_threads)/* --threads=0 means autodetect */3142 delta_search_threads =online_cpus();31433144#ifdef NO_PTHREADS3145if(delta_search_threads !=1)3146warning("no threads support, ignoring --threads");3147#endif3148if(!pack_to_stdout && !pack_size_limit)3149 pack_size_limit = pack_size_limit_cfg;3150if(pack_to_stdout && pack_size_limit)3151die("--max-pack-size cannot be used to build a pack for transfer.");3152if(pack_size_limit && pack_size_limit <1024*1024) {3153warning("minimum pack size limit is 1 MiB");3154 pack_size_limit =1024*1024;3155}31563157if(!pack_to_stdout && thin)3158die("--thin cannot be used to build an indexable pack.");31593160if(keep_unreachable && unpack_unreachable)3161die("--keep-unreachable and --unpack-unreachable are incompatible.");3162if(!rev_list_all || !rev_list_reflog || !rev_list_index)3163 unpack_unreachable_expiration =0;31643165if(filter_options.choice) {3166if(!pack_to_stdout)3167die("cannot use --filter without --stdout.");3168 use_bitmap_index =0;3169}31703171/*3172 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3173 *3174 * - to produce good pack (with bitmap index not-yet-packed objects are3175 * packed in suboptimal order).3176 *3177 * - to use more robust pack-generation codepath (avoiding possible3178 * bugs in bitmap code and possible bitmap index corruption).3179 */3180if(!pack_to_stdout)3181 use_bitmap_index_default =0;31823183if(use_bitmap_index <0)3184 use_bitmap_index = use_bitmap_index_default;31853186/* "hard" reasons not to use bitmaps; these just won't work at all */3187if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3188 use_bitmap_index =0;31893190if(pack_to_stdout || !rev_list_all)3191 write_bitmap_index =0;31923193if(progress && all_progress_implied)3194 progress =2;31953196add_extra_kept_packs(&keep_pack_list);3197if(ignore_packed_keep_on_disk) {3198struct packed_git *p;3199for(p =get_packed_git(the_repository); p; p = p->next)3200if(p->pack_local && p->pack_keep)3201break;3202if(!p)/* no keep-able packs found */3203 ignore_packed_keep_on_disk =0;3204}3205if(local) {3206/*3207 * unlike ignore_packed_keep_on_disk above, we do not3208 * want to unset "local" based on looking at packs, as3209 * it also covers non-local objects3210 */3211struct packed_git *p;3212for(p =get_packed_git(the_repository); p; p = p->next) {3213if(!p->pack_local) {3214 have_non_local_packs =1;3215break;3216}3217}3218}32193220if(progress)3221 progress_state =start_progress(_("Enumerating objects"),0);3222if(!use_internal_rev_list)3223read_object_list_from_stdin();3224else{3225get_object_list(rp.argc, rp.argv);3226argv_array_clear(&rp);3227}3228cleanup_preferred_base();3229if(include_tag && nr_result)3230for_each_ref(add_ref_tag, NULL);3231stop_progress(&progress_state);32323233if(non_empty && !nr_result)3234return0;3235if(nr_result)3236prepare_pack(window, depth);3237write_pack_file();3238if(progress)3239fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3240" reused %"PRIu32" (delta %"PRIu32")\n",3241 written, written_delta, reused, reused_delta);3242return0;3243}