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 34static const char*pack_usage[] = { 35N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 36N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 37 NULL 38}; 39 40/* 41 * Objects we are going to pack are collected in the `to_pack` structure. 42 * It contains an array (dynamically expanded) of the object data, and a map 43 * that can resolve SHA1s to their position in the array. 44 */ 45static struct packing_data to_pack; 46 47static struct pack_idx_entry **written_list; 48static uint32_t nr_result, nr_written; 49 50static int non_empty; 51static int reuse_delta =1, reuse_object =1; 52static int keep_unreachable, unpack_unreachable, include_tag; 53static timestamp_t unpack_unreachable_expiration; 54static int pack_loose_unreachable; 55static int local; 56static int have_non_local_packs; 57static int incremental; 58static int ignore_packed_keep; 59static int allow_ofs_delta; 60static struct pack_idx_option pack_idx_opts; 61static const char*base_name; 62static int progress =1; 63static int window =10; 64static unsigned long pack_size_limit; 65static int depth =50; 66static int delta_search_threads; 67static int pack_to_stdout; 68static int num_preferred_base; 69static struct progress *progress_state; 70 71static struct packed_git *reuse_packfile; 72static uint32_t reuse_packfile_objects; 73static off_t reuse_packfile_offset; 74 75static int use_bitmap_index_default =1; 76static int use_bitmap_index = -1; 77static int write_bitmap_index; 78static uint16_t write_bitmap_options; 79 80static int exclude_promisor_objects; 81 82static unsigned long delta_cache_size =0; 83static unsigned long max_delta_cache_size =256*1024*1024; 84static unsigned long cache_max_small_delta_size =1000; 85 86static unsigned long window_memory_limit =0; 87 88static struct list_objects_filter_options filter_options; 89 90enum missing_action { 91 MA_ERROR =0,/* fail if any missing objects are encountered */ 92 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 93 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 94}; 95static enum missing_action arg_missing_action; 96static show_object_fn fn_show_object; 97 98/* 99 * stats 100 */ 101static uint32_t written, written_delta; 102static uint32_t reused, reused_delta; 103 104/* 105 * Indexed commits 106 */ 107static struct commit **indexed_commits; 108static unsigned int indexed_commits_nr; 109static unsigned int indexed_commits_alloc; 110 111static voidindex_commit_for_bitmap(struct commit *commit) 112{ 113if(indexed_commits_nr >= indexed_commits_alloc) { 114 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 115REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 116} 117 118 indexed_commits[indexed_commits_nr++] = commit; 119} 120 121static void*get_delta(struct object_entry *entry) 122{ 123unsigned long size, base_size, delta_size; 124void*buf, *base_buf, *delta_buf; 125enum object_type type; 126 127 buf =read_object_file(&entry->idx.oid, &type, &size); 128if(!buf) 129die("unable to read%s",oid_to_hex(&entry->idx.oid)); 130 base_buf =read_object_file(&entry->delta->idx.oid, &type, &base_size); 131if(!base_buf) 132die("unable to read%s", 133oid_to_hex(&entry->delta->idx.oid)); 134 delta_buf =diff_delta(base_buf, base_size, 135 buf, size, &delta_size,0); 136if(!delta_buf || delta_size != entry->delta_size) 137die("delta size changed"); 138free(buf); 139free(base_buf); 140return delta_buf; 141} 142 143static unsigned longdo_compress(void**pptr,unsigned long size) 144{ 145 git_zstream stream; 146void*in, *out; 147unsigned long maxsize; 148 149git_deflate_init(&stream, pack_compression_level); 150 maxsize =git_deflate_bound(&stream, size); 151 152 in = *pptr; 153 out =xmalloc(maxsize); 154*pptr = out; 155 156 stream.next_in = in; 157 stream.avail_in = size; 158 stream.next_out = out; 159 stream.avail_out = maxsize; 160while(git_deflate(&stream, Z_FINISH) == Z_OK) 161;/* nothing */ 162git_deflate_end(&stream); 163 164free(in); 165return stream.total_out; 166} 167 168static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 169const struct object_id *oid) 170{ 171 git_zstream stream; 172unsigned char ibuf[1024*16]; 173unsigned char obuf[1024*16]; 174unsigned long olen =0; 175 176git_deflate_init(&stream, pack_compression_level); 177 178for(;;) { 179 ssize_t readlen; 180int zret = Z_OK; 181 readlen =read_istream(st, ibuf,sizeof(ibuf)); 182if(readlen == -1) 183die(_("unable to read%s"),oid_to_hex(oid)); 184 185 stream.next_in = ibuf; 186 stream.avail_in = readlen; 187while((stream.avail_in || readlen ==0) && 188(zret == Z_OK || zret == Z_BUF_ERROR)) { 189 stream.next_out = obuf; 190 stream.avail_out =sizeof(obuf); 191 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 192hashwrite(f, obuf, stream.next_out - obuf); 193 olen += stream.next_out - obuf; 194} 195if(stream.avail_in) 196die(_("deflate error (%d)"), zret); 197if(readlen ==0) { 198if(zret != Z_STREAM_END) 199die(_("deflate error (%d)"), zret); 200break; 201} 202} 203git_deflate_end(&stream); 204return olen; 205} 206 207/* 208 * we are going to reuse the existing object data as is. make 209 * sure it is not corrupt. 210 */ 211static intcheck_pack_inflate(struct packed_git *p, 212struct pack_window **w_curs, 213 off_t offset, 214 off_t len, 215unsigned long expect) 216{ 217 git_zstream stream; 218unsigned char fakebuf[4096], *in; 219int st; 220 221memset(&stream,0,sizeof(stream)); 222git_inflate_init(&stream); 223do{ 224 in =use_pack(p, w_curs, offset, &stream.avail_in); 225 stream.next_in = in; 226 stream.next_out = fakebuf; 227 stream.avail_out =sizeof(fakebuf); 228 st =git_inflate(&stream, Z_FINISH); 229 offset += stream.next_in - in; 230}while(st == Z_OK || st == Z_BUF_ERROR); 231git_inflate_end(&stream); 232return(st == Z_STREAM_END && 233 stream.total_out == expect && 234 stream.total_in == len) ?0: -1; 235} 236 237static voidcopy_pack_data(struct hashfile *f, 238struct packed_git *p, 239struct pack_window **w_curs, 240 off_t offset, 241 off_t len) 242{ 243unsigned char*in; 244unsigned long avail; 245 246while(len) { 247 in =use_pack(p, w_curs, offset, &avail); 248if(avail > len) 249 avail = (unsigned long)len; 250hashwrite(f, in, avail); 251 offset += avail; 252 len -= avail; 253} 254} 255 256/* Return 0 if we will bust the pack-size limit */ 257static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 258unsigned long limit,int usable_delta) 259{ 260unsigned long size, datalen; 261unsigned char header[MAX_PACK_OBJECT_HEADER], 262 dheader[MAX_PACK_OBJECT_HEADER]; 263unsigned hdrlen; 264enum object_type type; 265void*buf; 266struct git_istream *st = NULL; 267const unsigned hashsz = the_hash_algo->rawsz; 268 269if(!usable_delta) { 270if(entry->type == OBJ_BLOB && 271 entry->size > big_file_threshold && 272(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 273 buf = NULL; 274else{ 275 buf =read_object_file(&entry->idx.oid, &type, &size); 276if(!buf) 277die(_("unable to read%s"), 278oid_to_hex(&entry->idx.oid)); 279} 280/* 281 * make sure no cached delta data remains from a 282 * previous attempt before a pack split occurred. 283 */ 284FREE_AND_NULL(entry->delta_data); 285 entry->z_delta_size =0; 286}else if(entry->delta_data) { 287 size = entry->delta_size; 288 buf = entry->delta_data; 289 entry->delta_data = NULL; 290 type = (allow_ofs_delta && entry->delta->idx.offset) ? 291 OBJ_OFS_DELTA : OBJ_REF_DELTA; 292}else{ 293 buf =get_delta(entry); 294 size = entry->delta_size; 295 type = (allow_ofs_delta && entry->delta->idx.offset) ? 296 OBJ_OFS_DELTA : OBJ_REF_DELTA; 297} 298 299if(st)/* large blob case, just assume we don't compress well */ 300 datalen = size; 301else if(entry->z_delta_size) 302 datalen = entry->z_delta_size; 303else 304 datalen =do_compress(&buf, size); 305 306/* 307 * The object header is a byte of 'type' followed by zero or 308 * more bytes of length. 309 */ 310 hdrlen =encode_in_pack_object_header(header,sizeof(header), 311 type, size); 312 313if(type == OBJ_OFS_DELTA) { 314/* 315 * Deltas with relative base contain an additional 316 * encoding of the relative offset for the delta 317 * base from this object's position in the pack. 318 */ 319 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 320unsigned pos =sizeof(dheader) -1; 321 dheader[pos] = ofs &127; 322while(ofs >>=7) 323 dheader[--pos] =128| (--ofs &127); 324if(limit && hdrlen +sizeof(dheader) - pos + datalen + hashsz >= limit) { 325if(st) 326close_istream(st); 327free(buf); 328return0; 329} 330hashwrite(f, header, hdrlen); 331hashwrite(f, dheader + pos,sizeof(dheader) - pos); 332 hdrlen +=sizeof(dheader) - pos; 333}else if(type == OBJ_REF_DELTA) { 334/* 335 * Deltas with a base reference contain 336 * additional bytes for the base object ID. 337 */ 338if(limit && hdrlen + hashsz + datalen + hashsz >= limit) { 339if(st) 340close_istream(st); 341free(buf); 342return0; 343} 344hashwrite(f, header, hdrlen); 345hashwrite(f, entry->delta->idx.oid.hash, hashsz); 346 hdrlen += hashsz; 347}else{ 348if(limit && hdrlen + datalen + hashsz >= limit) { 349if(st) 350close_istream(st); 351free(buf); 352return0; 353} 354hashwrite(f, header, hdrlen); 355} 356if(st) { 357 datalen =write_large_blob_data(st, f, &entry->idx.oid); 358close_istream(st); 359}else{ 360hashwrite(f, buf, datalen); 361free(buf); 362} 363 364return hdrlen + datalen; 365} 366 367/* Return 0 if we will bust the pack-size limit */ 368static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 369unsigned long limit,int usable_delta) 370{ 371struct packed_git *p = entry->in_pack; 372struct pack_window *w_curs = NULL; 373struct revindex_entry *revidx; 374 off_t offset; 375enum object_type type = entry->type; 376 off_t datalen; 377unsigned char header[MAX_PACK_OBJECT_HEADER], 378 dheader[MAX_PACK_OBJECT_HEADER]; 379unsigned hdrlen; 380const unsigned hashsz = the_hash_algo->rawsz; 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 + hashsz >= 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 + hashsz + datalen + hashsz >= limit) { 426unuse_pack(&w_curs); 427return0; 428} 429hashwrite(f, header, hdrlen); 430hashwrite(f, entry->delta->idx.oid.hash, hashsz); 431 hdrlen += hashsz; 432 reused_delta++; 433}else{ 434if(limit && hdrlen + datalen + hashsz >= 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 - the_hash_algo->rawsz; 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) { 842hashclose(f, oid.hash, CSUM_CLOSE); 843}else if(nr_written == nr_remaining) { 844hashclose(f, oid.hash, CSUM_FSYNC); 845}else{ 846int fd =hashclose(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 && 988(!local || !have_non_local_packs)) 989return1; 990 991if(local && !p->pack_local) 992return0; 993if(ignore_packed_keep && p->pack_local && p->pack_keep) 994return0; 995 996/* we don't know yet; keep looking for more packs */ 997return-1; 998} 9991000/*1001 * Check whether we want the object in the pack (e.g., we do not want1002 * objects found in non-local stores if the "--local" option was used).1003 *1004 * If the caller already knows an existing pack it wants to take the object1005 * from, that is passed in *found_pack and *found_offset; otherwise this1006 * function finds if there is any pack that has the object and returns the pack1007 * and its offset in these variables.1008 */1009static intwant_object_in_pack(const struct object_id *oid,1010int exclude,1011struct packed_git **found_pack,1012 off_t *found_offset)1013{1014int want;1015struct list_head *pos;10161017if(!exclude && local &&has_loose_object_nonlocal(oid))1018return0;10191020/*1021 * If we already know the pack object lives in, start checks from that1022 * pack - in the usual case when neither --local was given nor .keep files1023 * are present we will determine the answer right now.1024 */1025if(*found_pack) {1026 want =want_found_object(exclude, *found_pack);1027if(want != -1)1028return want;1029}1030list_for_each(pos,get_packed_git_mru(the_repository)) {1031struct packed_git *p =list_entry(pos,struct packed_git, mru);1032 off_t offset;10331034if(p == *found_pack)1035 offset = *found_offset;1036else1037 offset =find_pack_entry_one(oid->hash, p);10381039if(offset) {1040if(!*found_pack) {1041if(!is_pack_valid(p))1042continue;1043*found_offset = offset;1044*found_pack = p;1045}1046 want =want_found_object(exclude, p);1047if(!exclude && want >0)1048list_move(&p->mru,1049get_packed_git_mru(the_repository));1050if(want != -1)1051return want;1052}1053}10541055return1;1056}10571058static voidcreate_object_entry(const struct object_id *oid,1059enum object_type type,1060uint32_t hash,1061int exclude,1062int no_try_delta,1063uint32_t index_pos,1064struct packed_git *found_pack,1065 off_t found_offset)1066{1067struct object_entry *entry;10681069 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1070 entry->hash = hash;1071if(type)1072 entry->type = type;1073if(exclude)1074 entry->preferred_base =1;1075else1076 nr_result++;1077if(found_pack) {1078 entry->in_pack = found_pack;1079 entry->in_pack_offset = found_offset;1080}10811082 entry->no_try_delta = no_try_delta;1083}10841085static const char no_closure_warning[] =N_(1086"disabling bitmap writing, as some objects are not being packed"1087);10881089static intadd_object_entry(const struct object_id *oid,enum object_type type,1090const char*name,int exclude)1091{1092struct packed_git *found_pack = NULL;1093 off_t found_offset =0;1094uint32_t index_pos;10951096if(have_duplicate_entry(oid, exclude, &index_pos))1097return0;10981099if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1100/* The pack is missing an object, so it will not have closure */1101if(write_bitmap_index) {1102warning(_(no_closure_warning));1103 write_bitmap_index =0;1104}1105return0;1106}11071108create_object_entry(oid, type,pack_name_hash(name),1109 exclude, name &&no_try_delta(name),1110 index_pos, found_pack, found_offset);11111112display_progress(progress_state, nr_result);1113return1;1114}11151116static intadd_object_entry_from_bitmap(const struct object_id *oid,1117enum object_type type,1118int flags,uint32_t name_hash,1119struct packed_git *pack, off_t offset)1120{1121uint32_t index_pos;11221123if(have_duplicate_entry(oid,0, &index_pos))1124return0;11251126if(!want_object_in_pack(oid,0, &pack, &offset))1127return0;11281129create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);11301131display_progress(progress_state, nr_result);1132return1;1133}11341135struct pbase_tree_cache {1136struct object_id oid;1137int ref;1138int temporary;1139void*tree_data;1140unsigned long tree_size;1141};11421143static struct pbase_tree_cache *(pbase_tree_cache[256]);1144static intpbase_tree_cache_ix(const struct object_id *oid)1145{1146return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1147}1148static intpbase_tree_cache_ix_incr(int ix)1149{1150return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1151}11521153static struct pbase_tree {1154struct pbase_tree *next;1155/* This is a phony "cache" entry; we are not1156 * going to evict it or find it through _get()1157 * mechanism -- this is for the toplevel node that1158 * would almost always change with any commit.1159 */1160struct pbase_tree_cache pcache;1161} *pbase_tree;11621163static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1164{1165struct pbase_tree_cache *ent, *nent;1166void*data;1167unsigned long size;1168enum object_type type;1169int neigh;1170int my_ix =pbase_tree_cache_ix(oid);1171int available_ix = -1;11721173/* pbase-tree-cache acts as a limited hashtable.1174 * your object will be found at your index or within a few1175 * slots after that slot if it is cached.1176 */1177for(neigh =0; neigh <8; neigh++) {1178 ent = pbase_tree_cache[my_ix];1179if(ent && !oidcmp(&ent->oid, oid)) {1180 ent->ref++;1181return ent;1182}1183else if(((available_ix <0) && (!ent || !ent->ref)) ||1184((0<= available_ix) &&1185(!ent && pbase_tree_cache[available_ix])))1186 available_ix = my_ix;1187if(!ent)1188break;1189 my_ix =pbase_tree_cache_ix_incr(my_ix);1190}11911192/* Did not find one. Either we got a bogus request or1193 * we need to read and perhaps cache.1194 */1195 data =read_object_file(oid, &type, &size);1196if(!data)1197return NULL;1198if(type != OBJ_TREE) {1199free(data);1200return NULL;1201}12021203/* We need to either cache or return a throwaway copy */12041205if(available_ix <0)1206 ent = NULL;1207else{1208 ent = pbase_tree_cache[available_ix];1209 my_ix = available_ix;1210}12111212if(!ent) {1213 nent =xmalloc(sizeof(*nent));1214 nent->temporary = (available_ix <0);1215}1216else{1217/* evict and reuse */1218free(ent->tree_data);1219 nent = ent;1220}1221oidcpy(&nent->oid, oid);1222 nent->tree_data = data;1223 nent->tree_size = size;1224 nent->ref =1;1225if(!nent->temporary)1226 pbase_tree_cache[my_ix] = nent;1227return nent;1228}12291230static voidpbase_tree_put(struct pbase_tree_cache *cache)1231{1232if(!cache->temporary) {1233 cache->ref--;1234return;1235}1236free(cache->tree_data);1237free(cache);1238}12391240static intname_cmp_len(const char*name)1241{1242int i;1243for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1244;1245return i;1246}12471248static voidadd_pbase_object(struct tree_desc *tree,1249const char*name,1250int cmplen,1251const char*fullname)1252{1253struct name_entry entry;1254int cmp;12551256while(tree_entry(tree,&entry)) {1257if(S_ISGITLINK(entry.mode))1258continue;1259 cmp =tree_entry_len(&entry) != cmplen ?1:1260memcmp(name, entry.path, cmplen);1261if(cmp >0)1262continue;1263if(cmp <0)1264return;1265if(name[cmplen] !='/') {1266add_object_entry(entry.oid,1267object_type(entry.mode),1268 fullname,1);1269return;1270}1271if(S_ISDIR(entry.mode)) {1272struct tree_desc sub;1273struct pbase_tree_cache *tree;1274const char*down = name+cmplen+1;1275int downlen =name_cmp_len(down);12761277 tree =pbase_tree_get(entry.oid);1278if(!tree)1279return;1280init_tree_desc(&sub, tree->tree_data, tree->tree_size);12811282add_pbase_object(&sub, down, downlen, fullname);1283pbase_tree_put(tree);1284}1285}1286}12871288static unsigned*done_pbase_paths;1289static int done_pbase_paths_num;1290static int done_pbase_paths_alloc;1291static intdone_pbase_path_pos(unsigned hash)1292{1293int lo =0;1294int hi = done_pbase_paths_num;1295while(lo < hi) {1296int mi = lo + (hi - lo) /2;1297if(done_pbase_paths[mi] == hash)1298return mi;1299if(done_pbase_paths[mi] < hash)1300 hi = mi;1301else1302 lo = mi +1;1303}1304return-lo-1;1305}13061307static intcheck_pbase_path(unsigned hash)1308{1309int pos =done_pbase_path_pos(hash);1310if(0<= pos)1311return1;1312 pos = -pos -1;1313ALLOC_GROW(done_pbase_paths,1314 done_pbase_paths_num +1,1315 done_pbase_paths_alloc);1316 done_pbase_paths_num++;1317if(pos < done_pbase_paths_num)1318MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1319 done_pbase_paths_num - pos -1);1320 done_pbase_paths[pos] = hash;1321return0;1322}13231324static voidadd_preferred_base_object(const char*name)1325{1326struct pbase_tree *it;1327int cmplen;1328unsigned hash =pack_name_hash(name);13291330if(!num_preferred_base ||check_pbase_path(hash))1331return;13321333 cmplen =name_cmp_len(name);1334for(it = pbase_tree; it; it = it->next) {1335if(cmplen ==0) {1336add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1337}1338else{1339struct tree_desc tree;1340init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1341add_pbase_object(&tree, name, cmplen, name);1342}1343}1344}13451346static voidadd_preferred_base(struct object_id *oid)1347{1348struct pbase_tree *it;1349void*data;1350unsigned long size;1351struct object_id tree_oid;13521353if(window <= num_preferred_base++)1354return;13551356 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1357if(!data)1358return;13591360for(it = pbase_tree; it; it = it->next) {1361if(!oidcmp(&it->pcache.oid, &tree_oid)) {1362free(data);1363return;1364}1365}13661367 it =xcalloc(1,sizeof(*it));1368 it->next = pbase_tree;1369 pbase_tree = it;13701371oidcpy(&it->pcache.oid, &tree_oid);1372 it->pcache.tree_data = data;1373 it->pcache.tree_size = size;1374}13751376static voidcleanup_preferred_base(void)1377{1378struct pbase_tree *it;1379unsigned i;13801381 it = pbase_tree;1382 pbase_tree = NULL;1383while(it) {1384struct pbase_tree *tmp = it;1385 it = tmp->next;1386free(tmp->pcache.tree_data);1387free(tmp);1388}13891390for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1391if(!pbase_tree_cache[i])1392continue;1393free(pbase_tree_cache[i]->tree_data);1394FREE_AND_NULL(pbase_tree_cache[i]);1395}13961397FREE_AND_NULL(done_pbase_paths);1398 done_pbase_paths_num = done_pbase_paths_alloc =0;1399}14001401static voidcheck_object(struct object_entry *entry)1402{1403if(entry->in_pack) {1404struct packed_git *p = entry->in_pack;1405struct pack_window *w_curs = NULL;1406const unsigned char*base_ref = NULL;1407struct object_entry *base_entry;1408unsigned long used, used_0;1409unsigned long avail;1410 off_t ofs;1411unsigned char*buf, c;14121413 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14141415/*1416 * We want in_pack_type even if we do not reuse delta1417 * since non-delta representations could still be reused.1418 */1419 used =unpack_object_header_buffer(buf, avail,1420&entry->in_pack_type,1421&entry->size);1422if(used ==0)1423goto give_up;14241425/*1426 * Determine if this is a delta and if so whether we can1427 * reuse it or not. Otherwise let's find out as cheaply as1428 * possible what the actual type and size for this object is.1429 */1430switch(entry->in_pack_type) {1431default:1432/* Not a delta hence we've already got all we need. */1433 entry->type = entry->in_pack_type;1434 entry->in_pack_header_size = used;1435if(entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB)1436goto give_up;1437unuse_pack(&w_curs);1438return;1439case OBJ_REF_DELTA:1440if(reuse_delta && !entry->preferred_base)1441 base_ref =use_pack(p, &w_curs,1442 entry->in_pack_offset + used, NULL);1443 entry->in_pack_header_size = used + the_hash_algo->rawsz;1444break;1445case OBJ_OFS_DELTA:1446 buf =use_pack(p, &w_curs,1447 entry->in_pack_offset + used, NULL);1448 used_0 =0;1449 c = buf[used_0++];1450 ofs = c &127;1451while(c &128) {1452 ofs +=1;1453if(!ofs ||MSB(ofs,7)) {1454error("delta base offset overflow in pack for%s",1455oid_to_hex(&entry->idx.oid));1456goto give_up;1457}1458 c = buf[used_0++];1459 ofs = (ofs <<7) + (c &127);1460}1461 ofs = entry->in_pack_offset - ofs;1462if(ofs <=0|| ofs >= entry->in_pack_offset) {1463error("delta base offset out of bound for%s",1464oid_to_hex(&entry->idx.oid));1465goto give_up;1466}1467if(reuse_delta && !entry->preferred_base) {1468struct revindex_entry *revidx;1469 revidx =find_pack_revindex(p, ofs);1470if(!revidx)1471goto give_up;1472 base_ref =nth_packed_object_sha1(p, revidx->nr);1473}1474 entry->in_pack_header_size = used + used_0;1475break;1476}14771478if(base_ref && (base_entry =packlist_find(&to_pack, base_ref, NULL))) {1479/*1480 * If base_ref was set above that means we wish to1481 * reuse delta data, and we even found that base1482 * in the list of objects we want to pack. Goodie!1483 *1484 * Depth value does not matter - find_deltas() will1485 * never consider reused delta as the base object to1486 * deltify other objects against, in order to avoid1487 * circular deltas.1488 */1489 entry->type = entry->in_pack_type;1490 entry->delta = base_entry;1491 entry->delta_size = entry->size;1492 entry->delta_sibling = base_entry->delta_child;1493 base_entry->delta_child = entry;1494unuse_pack(&w_curs);1495return;1496}14971498if(entry->type) {1499/*1500 * This must be a delta and we already know what the1501 * final object type is. Let's extract the actual1502 * object size from the delta header.1503 */1504 entry->size =get_size_from_delta(p, &w_curs,1505 entry->in_pack_offset + entry->in_pack_header_size);1506if(entry->size ==0)1507goto give_up;1508unuse_pack(&w_curs);1509return;1510}15111512/*1513 * No choice but to fall back to the recursive delta walk1514 * with sha1_object_info() to find about the object type1515 * at this point...1516 */1517 give_up:1518unuse_pack(&w_curs);1519}15201521 entry->type =oid_object_info(&entry->idx.oid, &entry->size);1522/*1523 * The error condition is checked in prepare_pack(). This is1524 * to permit a missing preferred base object to be ignored1525 * as a preferred base. Doing so can result in a larger1526 * pack file, but the transfer will still take place.1527 */1528}15291530static intpack_offset_sort(const void*_a,const void*_b)1531{1532const struct object_entry *a = *(struct object_entry **)_a;1533const struct object_entry *b = *(struct object_entry **)_b;15341535/* avoid filesystem trashing with loose objects */1536if(!a->in_pack && !b->in_pack)1537returnoidcmp(&a->idx.oid, &b->idx.oid);15381539if(a->in_pack < b->in_pack)1540return-1;1541if(a->in_pack > b->in_pack)1542return1;1543return a->in_pack_offset < b->in_pack_offset ? -1:1544(a->in_pack_offset > b->in_pack_offset);1545}15461547/*1548 * Drop an on-disk delta we were planning to reuse. Naively, this would1549 * just involve blanking out the "delta" field, but we have to deal1550 * with some extra book-keeping:1551 *1552 * 1. Removing ourselves from the delta_sibling linked list.1553 *1554 * 2. Updating our size/type to the non-delta representation. These were1555 * either not recorded initially (size) or overwritten with the delta type1556 * (type) when check_object() decided to reuse the delta.1557 *1558 * 3. Resetting our delta depth, as we are now a base object.1559 */1560static voiddrop_reused_delta(struct object_entry *entry)1561{1562struct object_entry **p = &entry->delta->delta_child;1563struct object_info oi = OBJECT_INFO_INIT;15641565while(*p) {1566if(*p == entry)1567*p = (*p)->delta_sibling;1568else1569 p = &(*p)->delta_sibling;1570}1571 entry->delta = NULL;1572 entry->depth =0;15731574 oi.sizep = &entry->size;1575 oi.typep = &entry->type;1576if(packed_object_info(entry->in_pack, entry->in_pack_offset, &oi) <0) {1577/*1578 * We failed to get the info from this pack for some reason;1579 * fall back to sha1_object_info, which may find another copy.1580 * And if that fails, the error will be recorded in entry->type1581 * and dealt with in prepare_pack().1582 */1583 entry->type =oid_object_info(&entry->idx.oid, &entry->size);1584}1585}15861587/*1588 * Follow the chain of deltas from this entry onward, throwing away any links1589 * that cause us to hit a cycle (as determined by the DFS state flags in1590 * the entries).1591 *1592 * We also detect too-long reused chains that would violate our --depth1593 * limit.1594 */1595static voidbreak_delta_chains(struct object_entry *entry)1596{1597/*1598 * The actual depth of each object we will write is stored as an int,1599 * as it cannot exceed our int "depth" limit. But before we break1600 * changes based no that limit, we may potentially go as deep as the1601 * number of objects, which is elsewhere bounded to a uint32_t.1602 */1603uint32_t total_depth;1604struct object_entry *cur, *next;16051606for(cur = entry, total_depth =0;1607 cur;1608 cur = cur->delta, total_depth++) {1609if(cur->dfs_state == DFS_DONE) {1610/*1611 * We've already seen this object and know it isn't1612 * part of a cycle. We do need to append its depth1613 * to our count.1614 */1615 total_depth += cur->depth;1616break;1617}16181619/*1620 * We break cycles before looping, so an ACTIVE state (or any1621 * other cruft which made its way into the state variable)1622 * is a bug.1623 */1624if(cur->dfs_state != DFS_NONE)1625die("BUG: confusing delta dfs state in first pass:%d",1626 cur->dfs_state);16271628/*1629 * Now we know this is the first time we've seen the object. If1630 * it's not a delta, we're done traversing, but we'll mark it1631 * done to save time on future traversals.1632 */1633if(!cur->delta) {1634 cur->dfs_state = DFS_DONE;1635break;1636}16371638/*1639 * Mark ourselves as active and see if the next step causes1640 * us to cycle to another active object. It's important to do1641 * this _before_ we loop, because it impacts where we make the1642 * cut, and thus how our total_depth counter works.1643 * E.g., We may see a partial loop like:1644 *1645 * A -> B -> C -> D -> B1646 *1647 * Cutting B->C breaks the cycle. But now the depth of A is1648 * only 1, and our total_depth counter is at 3. The size of the1649 * error is always one less than the size of the cycle we1650 * broke. Commits C and D were "lost" from A's chain.1651 *1652 * If we instead cut D->B, then the depth of A is correct at 3.1653 * We keep all commits in the chain that we examined.1654 */1655 cur->dfs_state = DFS_ACTIVE;1656if(cur->delta->dfs_state == DFS_ACTIVE) {1657drop_reused_delta(cur);1658 cur->dfs_state = DFS_DONE;1659break;1660}1661}16621663/*1664 * And now that we've gone all the way to the bottom of the chain, we1665 * need to clear the active flags and set the depth fields as1666 * appropriate. Unlike the loop above, which can quit when it drops a1667 * delta, we need to keep going to look for more depth cuts. So we need1668 * an extra "next" pointer to keep going after we reset cur->delta.1669 */1670for(cur = entry; cur; cur = next) {1671 next = cur->delta;16721673/*1674 * We should have a chain of zero or more ACTIVE states down to1675 * a final DONE. We can quit after the DONE, because either it1676 * has no bases, or we've already handled them in a previous1677 * call.1678 */1679if(cur->dfs_state == DFS_DONE)1680break;1681else if(cur->dfs_state != DFS_ACTIVE)1682die("BUG: confusing delta dfs state in second pass:%d",1683 cur->dfs_state);16841685/*1686 * If the total_depth is more than depth, then we need to snip1687 * the chain into two or more smaller chains that don't exceed1688 * the maximum depth. Most of the resulting chains will contain1689 * (depth + 1) entries (i.e., depth deltas plus one base), and1690 * the last chain (i.e., the one containing entry) will contain1691 * whatever entries are left over, namely1692 * (total_depth % (depth + 1)) of them.1693 *1694 * Since we are iterating towards decreasing depth, we need to1695 * decrement total_depth as we go, and we need to write to the1696 * entry what its final depth will be after all of the1697 * snipping. Since we're snipping into chains of length (depth1698 * + 1) entries, the final depth of an entry will be its1699 * original depth modulo (depth + 1). Any time we encounter an1700 * entry whose final depth is supposed to be zero, we snip it1701 * from its delta base, thereby making it so.1702 */1703 cur->depth = (total_depth--) % (depth +1);1704if(!cur->depth)1705drop_reused_delta(cur);17061707 cur->dfs_state = DFS_DONE;1708}1709}17101711static voidget_object_details(void)1712{1713uint32_t i;1714struct object_entry **sorted_by_offset;17151716 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1717for(i =0; i < to_pack.nr_objects; i++)1718 sorted_by_offset[i] = to_pack.objects + i;1719QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17201721for(i =0; i < to_pack.nr_objects; i++) {1722struct object_entry *entry = sorted_by_offset[i];1723check_object(entry);1724if(big_file_threshold < entry->size)1725 entry->no_try_delta =1;1726}17271728/*1729 * This must happen in a second pass, since we rely on the delta1730 * information for the whole list being completed.1731 */1732for(i =0; i < to_pack.nr_objects; i++)1733break_delta_chains(&to_pack.objects[i]);17341735free(sorted_by_offset);1736}17371738/*1739 * We search for deltas in a list sorted by type, by filename hash, and then1740 * by size, so that we see progressively smaller and smaller files.1741 * That's because we prefer deltas to be from the bigger file1742 * to the smaller -- deletes are potentially cheaper, but perhaps1743 * more importantly, the bigger file is likely the more recent1744 * one. The deepest deltas are therefore the oldest objects which are1745 * less susceptible to be accessed often.1746 */1747static inttype_size_sort(const void*_a,const void*_b)1748{1749const struct object_entry *a = *(struct object_entry **)_a;1750const struct object_entry *b = *(struct object_entry **)_b;17511752if(a->type > b->type)1753return-1;1754if(a->type < b->type)1755return1;1756if(a->hash > b->hash)1757return-1;1758if(a->hash < b->hash)1759return1;1760if(a->preferred_base > b->preferred_base)1761return-1;1762if(a->preferred_base < b->preferred_base)1763return1;1764if(a->size > b->size)1765return-1;1766if(a->size < b->size)1767return1;1768return a < b ? -1: (a > b);/* newest first */1769}17701771struct unpacked {1772struct object_entry *entry;1773void*data;1774struct delta_index *index;1775unsigned depth;1776};17771778static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1779unsigned long delta_size)1780{1781if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1782return0;17831784if(delta_size < cache_max_small_delta_size)1785return1;17861787/* cache delta, if objects are large enough compared to delta size */1788if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1789return1;17901791return0;1792}17931794#ifndef NO_PTHREADS17951796static pthread_mutex_t read_mutex;1797#define read_lock() pthread_mutex_lock(&read_mutex)1798#define read_unlock() pthread_mutex_unlock(&read_mutex)17991800static pthread_mutex_t cache_mutex;1801#define cache_lock() pthread_mutex_lock(&cache_mutex)1802#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18031804static pthread_mutex_t progress_mutex;1805#define progress_lock() pthread_mutex_lock(&progress_mutex)1806#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18071808#else18091810#define read_lock() (void)01811#define read_unlock() (void)01812#define cache_lock() (void)01813#define cache_unlock() (void)01814#define progress_lock() (void)01815#define progress_unlock() (void)018161817#endif18181819static inttry_delta(struct unpacked *trg,struct unpacked *src,1820unsigned max_depth,unsigned long*mem_usage)1821{1822struct object_entry *trg_entry = trg->entry;1823struct object_entry *src_entry = src->entry;1824unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1825unsigned ref_depth;1826enum object_type type;1827void*delta_buf;18281829/* Don't bother doing diffs between different types */1830if(trg_entry->type != src_entry->type)1831return-1;18321833/*1834 * We do not bother to try a delta that we discarded on an1835 * earlier try, but only when reusing delta data. Note that1836 * src_entry that is marked as the preferred_base should always1837 * be considered, as even if we produce a suboptimal delta against1838 * it, we will still save the transfer cost, as we already know1839 * the other side has it and we won't send src_entry at all.1840 */1841if(reuse_delta && trg_entry->in_pack &&1842 trg_entry->in_pack == src_entry->in_pack &&1843!src_entry->preferred_base &&1844 trg_entry->in_pack_type != OBJ_REF_DELTA &&1845 trg_entry->in_pack_type != OBJ_OFS_DELTA)1846return0;18471848/* Let's not bust the allowed depth. */1849if(src->depth >= max_depth)1850return0;18511852/* Now some size filtering heuristics. */1853 trg_size = trg_entry->size;1854if(!trg_entry->delta) {1855 max_size = trg_size/2- the_hash_algo->rawsz;1856 ref_depth =1;1857}else{1858 max_size = trg_entry->delta_size;1859 ref_depth = trg->depth;1860}1861 max_size = (uint64_t)max_size * (max_depth - src->depth) /1862(max_depth - ref_depth +1);1863if(max_size ==0)1864return0;1865 src_size = src_entry->size;1866 sizediff = src_size < trg_size ? trg_size - src_size :0;1867if(sizediff >= max_size)1868return0;1869if(trg_size < src_size /32)1870return0;18711872/* Load data if not already done */1873if(!trg->data) {1874read_lock();1875 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1876read_unlock();1877if(!trg->data)1878die("object%scannot be read",1879oid_to_hex(&trg_entry->idx.oid));1880if(sz != trg_size)1881die("object%sinconsistent object length (%lu vs%lu)",1882oid_to_hex(&trg_entry->idx.oid), sz,1883 trg_size);1884*mem_usage += sz;1885}1886if(!src->data) {1887read_lock();1888 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);1889read_unlock();1890if(!src->data) {1891if(src_entry->preferred_base) {1892static int warned =0;1893if(!warned++)1894warning("object%scannot be read",1895oid_to_hex(&src_entry->idx.oid));1896/*1897 * Those objects are not included in the1898 * resulting pack. Be resilient and ignore1899 * them if they can't be read, in case the1900 * pack could be created nevertheless.1901 */1902return0;1903}1904die("object%scannot be read",1905oid_to_hex(&src_entry->idx.oid));1906}1907if(sz != src_size)1908die("object%sinconsistent object length (%lu vs%lu)",1909oid_to_hex(&src_entry->idx.oid), sz,1910 src_size);1911*mem_usage += sz;1912}1913if(!src->index) {1914 src->index =create_delta_index(src->data, src_size);1915if(!src->index) {1916static int warned =0;1917if(!warned++)1918warning("suboptimal pack - out of memory");1919return0;1920}1921*mem_usage +=sizeof_delta_index(src->index);1922}19231924 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);1925if(!delta_buf)1926return0;19271928if(trg_entry->delta) {1929/* Prefer only shallower same-sized deltas. */1930if(delta_size == trg_entry->delta_size &&1931 src->depth +1>= trg->depth) {1932free(delta_buf);1933return0;1934}1935}19361937/*1938 * Handle memory allocation outside of the cache1939 * accounting lock. Compiler will optimize the strangeness1940 * away when NO_PTHREADS is defined.1941 */1942free(trg_entry->delta_data);1943cache_lock();1944if(trg_entry->delta_data) {1945 delta_cache_size -= trg_entry->delta_size;1946 trg_entry->delta_data = NULL;1947}1948if(delta_cacheable(src_size, trg_size, delta_size)) {1949 delta_cache_size += delta_size;1950cache_unlock();1951 trg_entry->delta_data =xrealloc(delta_buf, delta_size);1952}else{1953cache_unlock();1954free(delta_buf);1955}19561957 trg_entry->delta = src_entry;1958 trg_entry->delta_size = delta_size;1959 trg->depth = src->depth +1;19601961return1;1962}19631964static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)1965{1966struct object_entry *child = me->delta_child;1967unsigned int m = n;1968while(child) {1969unsigned int c =check_delta_limit(child, n +1);1970if(m < c)1971 m = c;1972 child = child->delta_sibling;1973}1974return m;1975}19761977static unsigned longfree_unpacked(struct unpacked *n)1978{1979unsigned long freed_mem =sizeof_delta_index(n->index);1980free_delta_index(n->index);1981 n->index = NULL;1982if(n->data) {1983 freed_mem += n->entry->size;1984FREE_AND_NULL(n->data);1985}1986 n->entry = NULL;1987 n->depth =0;1988return freed_mem;1989}19901991static voidfind_deltas(struct object_entry **list,unsigned*list_size,1992int window,int depth,unsigned*processed)1993{1994uint32_t i, idx =0, count =0;1995struct unpacked *array;1996unsigned long mem_usage =0;19971998 array =xcalloc(window,sizeof(struct unpacked));19992000for(;;) {2001struct object_entry *entry;2002struct unpacked *n = array + idx;2003int j, max_depth, best_base = -1;20042005progress_lock();2006if(!*list_size) {2007progress_unlock();2008break;2009}2010 entry = *list++;2011(*list_size)--;2012if(!entry->preferred_base) {2013(*processed)++;2014display_progress(progress_state, *processed);2015}2016progress_unlock();20172018 mem_usage -=free_unpacked(n);2019 n->entry = entry;20202021while(window_memory_limit &&2022 mem_usage > window_memory_limit &&2023 count >1) {2024uint32_t tail = (idx + window - count) % window;2025 mem_usage -=free_unpacked(array + tail);2026 count--;2027}20282029/* We do not compute delta to *create* objects we are not2030 * going to pack.2031 */2032if(entry->preferred_base)2033goto next;20342035/*2036 * If the current object is at pack edge, take the depth the2037 * objects that depend on the current object into account2038 * otherwise they would become too deep.2039 */2040 max_depth = depth;2041if(entry->delta_child) {2042 max_depth -=check_delta_limit(entry,0);2043if(max_depth <=0)2044goto next;2045}20462047 j = window;2048while(--j >0) {2049int ret;2050uint32_t other_idx = idx + j;2051struct unpacked *m;2052if(other_idx >= window)2053 other_idx -= window;2054 m = array + other_idx;2055if(!m->entry)2056break;2057 ret =try_delta(n, m, max_depth, &mem_usage);2058if(ret <0)2059break;2060else if(ret >0)2061 best_base = other_idx;2062}20632064/*2065 * If we decided to cache the delta data, then it is best2066 * to compress it right away. First because we have to do2067 * it anyway, and doing it here while we're threaded will2068 * save a lot of time in the non threaded write phase,2069 * as well as allow for caching more deltas within2070 * the same cache size limit.2071 * ...2072 * But only if not writing to stdout, since in that case2073 * the network is most likely throttling writes anyway,2074 * and therefore it is best to go to the write phase ASAP2075 * instead, as we can afford spending more time compressing2076 * between writes at that moment.2077 */2078if(entry->delta_data && !pack_to_stdout) {2079 entry->z_delta_size =do_compress(&entry->delta_data,2080 entry->delta_size);2081cache_lock();2082 delta_cache_size -= entry->delta_size;2083 delta_cache_size += entry->z_delta_size;2084cache_unlock();2085}20862087/* if we made n a delta, and if n is already at max2088 * depth, leaving it in the window is pointless. we2089 * should evict it first.2090 */2091if(entry->delta && max_depth <= n->depth)2092continue;20932094/*2095 * Move the best delta base up in the window, after the2096 * currently deltified object, to keep it longer. It will2097 * be the first base object to be attempted next.2098 */2099if(entry->delta) {2100struct unpacked swap = array[best_base];2101int dist = (window + idx - best_base) % window;2102int dst = best_base;2103while(dist--) {2104int src = (dst +1) % window;2105 array[dst] = array[src];2106 dst = src;2107}2108 array[dst] = swap;2109}21102111 next:2112 idx++;2113if(count +1< window)2114 count++;2115if(idx >= window)2116 idx =0;2117}21182119for(i =0; i < window; ++i) {2120free_delta_index(array[i].index);2121free(array[i].data);2122}2123free(array);2124}21252126#ifndef NO_PTHREADS21272128static voidtry_to_free_from_threads(size_t size)2129{2130read_lock();2131release_pack_memory(size);2132read_unlock();2133}21342135static try_to_free_t old_try_to_free_routine;21362137/*2138 * The main thread waits on the condition that (at least) one of the workers2139 * has stopped working (which is indicated in the .working member of2140 * struct thread_params).2141 * When a work thread has completed its work, it sets .working to 0 and2142 * signals the main thread and waits on the condition that .data_ready2143 * becomes 1.2144 */21452146struct thread_params {2147 pthread_t thread;2148struct object_entry **list;2149unsigned list_size;2150unsigned remaining;2151int window;2152int depth;2153int working;2154int data_ready;2155 pthread_mutex_t mutex;2156 pthread_cond_t cond;2157unsigned*processed;2158};21592160static pthread_cond_t progress_cond;21612162/*2163 * Mutex and conditional variable can't be statically-initialized on Windows.2164 */2165static voidinit_threaded_search(void)2166{2167init_recursive_mutex(&read_mutex);2168pthread_mutex_init(&cache_mutex, NULL);2169pthread_mutex_init(&progress_mutex, NULL);2170pthread_cond_init(&progress_cond, NULL);2171 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2172}21732174static voidcleanup_threaded_search(void)2175{2176set_try_to_free_routine(old_try_to_free_routine);2177pthread_cond_destroy(&progress_cond);2178pthread_mutex_destroy(&read_mutex);2179pthread_mutex_destroy(&cache_mutex);2180pthread_mutex_destroy(&progress_mutex);2181}21822183static void*threaded_find_deltas(void*arg)2184{2185struct thread_params *me = arg;21862187progress_lock();2188while(me->remaining) {2189progress_unlock();21902191find_deltas(me->list, &me->remaining,2192 me->window, me->depth, me->processed);21932194progress_lock();2195 me->working =0;2196pthread_cond_signal(&progress_cond);2197progress_unlock();21982199/*2200 * We must not set ->data_ready before we wait on the2201 * condition because the main thread may have set it to 12202 * before we get here. In order to be sure that new2203 * work is available if we see 1 in ->data_ready, it2204 * was initialized to 0 before this thread was spawned2205 * and we reset it to 0 right away.2206 */2207pthread_mutex_lock(&me->mutex);2208while(!me->data_ready)2209pthread_cond_wait(&me->cond, &me->mutex);2210 me->data_ready =0;2211pthread_mutex_unlock(&me->mutex);22122213progress_lock();2214}2215progress_unlock();2216/* leave ->working 1 so that this doesn't get more work assigned */2217return NULL;2218}22192220static voidll_find_deltas(struct object_entry **list,unsigned list_size,2221int window,int depth,unsigned*processed)2222{2223struct thread_params *p;2224int i, ret, active_threads =0;22252226init_threaded_search();22272228if(delta_search_threads <=1) {2229find_deltas(list, &list_size, window, depth, processed);2230cleanup_threaded_search();2231return;2232}2233if(progress > pack_to_stdout)2234fprintf(stderr,"Delta compression using up to%dthreads.\n",2235 delta_search_threads);2236 p =xcalloc(delta_search_threads,sizeof(*p));22372238/* Partition the work amongst work threads. */2239for(i =0; i < delta_search_threads; i++) {2240unsigned sub_size = list_size / (delta_search_threads - i);22412242/* don't use too small segments or no deltas will be found */2243if(sub_size <2*window && i+1< delta_search_threads)2244 sub_size =0;22452246 p[i].window = window;2247 p[i].depth = depth;2248 p[i].processed = processed;2249 p[i].working =1;2250 p[i].data_ready =0;22512252/* try to split chunks on "path" boundaries */2253while(sub_size && sub_size < list_size &&2254 list[sub_size]->hash &&2255 list[sub_size]->hash == list[sub_size-1]->hash)2256 sub_size++;22572258 p[i].list = list;2259 p[i].list_size = sub_size;2260 p[i].remaining = sub_size;22612262 list += sub_size;2263 list_size -= sub_size;2264}22652266/* Start work threads. */2267for(i =0; i < delta_search_threads; i++) {2268if(!p[i].list_size)2269continue;2270pthread_mutex_init(&p[i].mutex, NULL);2271pthread_cond_init(&p[i].cond, NULL);2272 ret =pthread_create(&p[i].thread, NULL,2273 threaded_find_deltas, &p[i]);2274if(ret)2275die("unable to create thread:%s",strerror(ret));2276 active_threads++;2277}22782279/*2280 * Now let's wait for work completion. Each time a thread is done2281 * with its work, we steal half of the remaining work from the2282 * thread with the largest number of unprocessed objects and give2283 * it to that newly idle thread. This ensure good load balancing2284 * until the remaining object list segments are simply too short2285 * to be worth splitting anymore.2286 */2287while(active_threads) {2288struct thread_params *target = NULL;2289struct thread_params *victim = NULL;2290unsigned sub_size =0;22912292progress_lock();2293for(;;) {2294for(i =0; !target && i < delta_search_threads; i++)2295if(!p[i].working)2296 target = &p[i];2297if(target)2298break;2299pthread_cond_wait(&progress_cond, &progress_mutex);2300}23012302for(i =0; i < delta_search_threads; i++)2303if(p[i].remaining >2*window &&2304(!victim || victim->remaining < p[i].remaining))2305 victim = &p[i];2306if(victim) {2307 sub_size = victim->remaining /2;2308 list = victim->list + victim->list_size - sub_size;2309while(sub_size && list[0]->hash &&2310 list[0]->hash == list[-1]->hash) {2311 list++;2312 sub_size--;2313}2314if(!sub_size) {2315/*2316 * It is possible for some "paths" to have2317 * so many objects that no hash boundary2318 * might be found. Let's just steal the2319 * exact half in that case.2320 */2321 sub_size = victim->remaining /2;2322 list -= sub_size;2323}2324 target->list = list;2325 victim->list_size -= sub_size;2326 victim->remaining -= sub_size;2327}2328 target->list_size = sub_size;2329 target->remaining = sub_size;2330 target->working =1;2331progress_unlock();23322333pthread_mutex_lock(&target->mutex);2334 target->data_ready =1;2335pthread_cond_signal(&target->cond);2336pthread_mutex_unlock(&target->mutex);23372338if(!sub_size) {2339pthread_join(target->thread, NULL);2340pthread_cond_destroy(&target->cond);2341pthread_mutex_destroy(&target->mutex);2342 active_threads--;2343}2344}2345cleanup_threaded_search();2346free(p);2347}23482349#else2350#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2351#endif23522353static voidadd_tag_chain(const struct object_id *oid)2354{2355struct tag *tag;23562357/*2358 * We catch duplicates already in add_object_entry(), but we'd2359 * prefer to do this extra check to avoid having to parse the2360 * tag at all if we already know that it's being packed (e.g., if2361 * it was included via bitmaps, we would not have parsed it2362 * previously).2363 */2364if(packlist_find(&to_pack, oid->hash, NULL))2365return;23662367 tag =lookup_tag(oid);2368while(1) {2369if(!tag ||parse_tag(tag) || !tag->tagged)2370die("unable to pack objects reachable from tag%s",2371oid_to_hex(oid));23722373add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);23742375if(tag->tagged->type != OBJ_TAG)2376return;23772378 tag = (struct tag *)tag->tagged;2379}2380}23812382static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2383{2384struct object_id peeled;23852386if(starts_with(path,"refs/tags/") &&/* is a tag? */2387!peel_ref(path, &peeled) &&/* peelable? */2388packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2389add_tag_chain(oid);2390return0;2391}23922393static voidprepare_pack(int window,int depth)2394{2395struct object_entry **delta_list;2396uint32_t i, nr_deltas;2397unsigned n;23982399get_object_details();24002401/*2402 * If we're locally repacking then we need to be doubly careful2403 * from now on in order to make sure no stealth corruption gets2404 * propagated to the new pack. Clients receiving streamed packs2405 * should validate everything they get anyway so no need to incur2406 * the additional cost here in that case.2407 */2408if(!pack_to_stdout)2409 do_check_packed_object_crc =1;24102411if(!to_pack.nr_objects || !window || !depth)2412return;24132414ALLOC_ARRAY(delta_list, to_pack.nr_objects);2415 nr_deltas = n =0;24162417for(i =0; i < to_pack.nr_objects; i++) {2418struct object_entry *entry = to_pack.objects + i;24192420if(entry->delta)2421/* This happens if we decided to reuse existing2422 * delta from a pack. "reuse_delta &&" is implied.2423 */2424continue;24252426if(entry->size <50)2427continue;24282429if(entry->no_try_delta)2430continue;24312432if(!entry->preferred_base) {2433 nr_deltas++;2434if(entry->type <0)2435die("unable to get type of object%s",2436oid_to_hex(&entry->idx.oid));2437}else{2438if(entry->type <0) {2439/*2440 * This object is not found, but we2441 * don't have to include it anyway.2442 */2443continue;2444}2445}24462447 delta_list[n++] = entry;2448}24492450if(nr_deltas && n >1) {2451unsigned nr_done =0;2452if(progress)2453 progress_state =start_progress(_("Compressing objects"),2454 nr_deltas);2455QSORT(delta_list, n, type_size_sort);2456ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2457stop_progress(&progress_state);2458if(nr_done != nr_deltas)2459die("inconsistency with delta count");2460}2461free(delta_list);2462}24632464static intgit_pack_config(const char*k,const char*v,void*cb)2465{2466if(!strcmp(k,"pack.window")) {2467 window =git_config_int(k, v);2468return0;2469}2470if(!strcmp(k,"pack.windowmemory")) {2471 window_memory_limit =git_config_ulong(k, v);2472return0;2473}2474if(!strcmp(k,"pack.depth")) {2475 depth =git_config_int(k, v);2476return0;2477}2478if(!strcmp(k,"pack.deltacachesize")) {2479 max_delta_cache_size =git_config_int(k, v);2480return0;2481}2482if(!strcmp(k,"pack.deltacachelimit")) {2483 cache_max_small_delta_size =git_config_int(k, v);2484return0;2485}2486if(!strcmp(k,"pack.writebitmaphashcache")) {2487if(git_config_bool(k, v))2488 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2489else2490 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2491}2492if(!strcmp(k,"pack.usebitmaps")) {2493 use_bitmap_index_default =git_config_bool(k, v);2494return0;2495}2496if(!strcmp(k,"pack.threads")) {2497 delta_search_threads =git_config_int(k, v);2498if(delta_search_threads <0)2499die("invalid number of threads specified (%d)",2500 delta_search_threads);2501#ifdef NO_PTHREADS2502if(delta_search_threads !=1) {2503warning("no threads support, ignoring%s", k);2504 delta_search_threads =0;2505}2506#endif2507return0;2508}2509if(!strcmp(k,"pack.indexversion")) {2510 pack_idx_opts.version =git_config_int(k, v);2511if(pack_idx_opts.version >2)2512die("bad pack.indexversion=%"PRIu32,2513 pack_idx_opts.version);2514return0;2515}2516returngit_default_config(k, v, cb);2517}25182519static voidread_object_list_from_stdin(void)2520{2521char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2522struct object_id oid;2523const char*p;25242525for(;;) {2526if(!fgets(line,sizeof(line), stdin)) {2527if(feof(stdin))2528break;2529if(!ferror(stdin))2530die("fgets returned NULL, not EOF, not error!");2531if(errno != EINTR)2532die_errno("fgets");2533clearerr(stdin);2534continue;2535}2536if(line[0] =='-') {2537if(get_oid_hex(line+1, &oid))2538die("expected edge object ID, got garbage:\n%s",2539 line);2540add_preferred_base(&oid);2541continue;2542}2543if(parse_oid_hex(line, &oid, &p))2544die("expected object ID, got garbage:\n%s", line);25452546add_preferred_base_object(p +1);2547add_object_entry(&oid,0, p +1,0);2548}2549}25502551/* Remember to update object flag allocation in object.h */2552#define OBJECT_ADDED (1u<<20)25532554static voidshow_commit(struct commit *commit,void*data)2555{2556add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2557 commit->object.flags |= OBJECT_ADDED;25582559if(write_bitmap_index)2560index_commit_for_bitmap(commit);2561}25622563static voidshow_object(struct object *obj,const char*name,void*data)2564{2565add_preferred_base_object(name);2566add_object_entry(&obj->oid, obj->type, name,0);2567 obj->flags |= OBJECT_ADDED;2568}25692570static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2571{2572assert(arg_missing_action == MA_ALLOW_ANY);25732574/*2575 * Quietly ignore ALL missing objects. This avoids problems with2576 * staging them now and getting an odd error later.2577 */2578if(!has_object_file(&obj->oid))2579return;25802581show_object(obj, name, data);2582}25832584static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2585{2586assert(arg_missing_action == MA_ALLOW_PROMISOR);25872588/*2589 * Quietly ignore EXPECTED missing objects. This avoids problems with2590 * staging them now and getting an odd error later.2591 */2592if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2593return;25942595show_object(obj, name, data);2596}25972598static intoption_parse_missing_action(const struct option *opt,2599const char*arg,int unset)2600{2601assert(arg);2602assert(!unset);26032604if(!strcmp(arg,"error")) {2605 arg_missing_action = MA_ERROR;2606 fn_show_object = show_object;2607return0;2608}26092610if(!strcmp(arg,"allow-any")) {2611 arg_missing_action = MA_ALLOW_ANY;2612 fetch_if_missing =0;2613 fn_show_object = show_object__ma_allow_any;2614return0;2615}26162617if(!strcmp(arg,"allow-promisor")) {2618 arg_missing_action = MA_ALLOW_PROMISOR;2619 fetch_if_missing =0;2620 fn_show_object = show_object__ma_allow_promisor;2621return0;2622}26232624die(_("invalid value for --missing"));2625return0;2626}26272628static voidshow_edge(struct commit *commit)2629{2630add_preferred_base(&commit->object.oid);2631}26322633struct in_pack_object {2634 off_t offset;2635struct object *object;2636};26372638struct in_pack {2639unsigned int alloc;2640unsigned int nr;2641struct in_pack_object *array;2642};26432644static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2645{2646 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2647 in_pack->array[in_pack->nr].object = object;2648 in_pack->nr++;2649}26502651/*2652 * Compare the objects in the offset order, in order to emulate the2653 * "git rev-list --objects" output that produced the pack originally.2654 */2655static intofscmp(const void*a_,const void*b_)2656{2657struct in_pack_object *a = (struct in_pack_object *)a_;2658struct in_pack_object *b = (struct in_pack_object *)b_;26592660if(a->offset < b->offset)2661return-1;2662else if(a->offset > b->offset)2663return1;2664else2665returnoidcmp(&a->object->oid, &b->object->oid);2666}26672668static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2669{2670struct packed_git *p;2671struct in_pack in_pack;2672uint32_t i;26732674memset(&in_pack,0,sizeof(in_pack));26752676for(p =get_packed_git(the_repository); p; p = p->next) {2677struct object_id oid;2678struct object *o;26792680if(!p->pack_local || p->pack_keep)2681continue;2682if(open_pack_index(p))2683die("cannot open pack index");26842685ALLOC_GROW(in_pack.array,2686 in_pack.nr + p->num_objects,2687 in_pack.alloc);26882689for(i =0; i < p->num_objects; i++) {2690nth_packed_object_oid(&oid, p, i);2691 o =lookup_unknown_object(oid.hash);2692if(!(o->flags & OBJECT_ADDED))2693mark_in_pack_object(o, p, &in_pack);2694 o->flags |= OBJECT_ADDED;2695}2696}26972698if(in_pack.nr) {2699QSORT(in_pack.array, in_pack.nr, ofscmp);2700for(i =0; i < in_pack.nr; i++) {2701struct object *o = in_pack.array[i].object;2702add_object_entry(&o->oid, o->type,"",0);2703}2704}2705free(in_pack.array);2706}27072708static intadd_loose_object(const struct object_id *oid,const char*path,2709void*data)2710{2711enum object_type type =oid_object_info(oid, NULL);27122713if(type <0) {2714warning("loose object at%scould not be examined", path);2715return0;2716}27172718add_object_entry(oid, type,"",0);2719return0;2720}27212722/*2723 * We actually don't even have to worry about reachability here.2724 * add_object_entry will weed out duplicates, so we just add every2725 * loose object we find.2726 */2727static voidadd_unreachable_loose_objects(void)2728{2729for_each_loose_file_in_objdir(get_object_directory(),2730 add_loose_object,2731 NULL, NULL, NULL);2732}27332734static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2735{2736static struct packed_git *last_found = (void*)1;2737struct packed_git *p;27382739 p = (last_found != (void*)1) ? last_found :2740get_packed_git(the_repository);27412742while(p) {2743if((!p->pack_local || p->pack_keep) &&2744find_pack_entry_one(oid->hash, p)) {2745 last_found = p;2746return1;2747}2748if(p == last_found)2749 p =get_packed_git(the_repository);2750else2751 p = p->next;2752if(p == last_found)2753 p = p->next;2754}2755return0;2756}27572758/*2759 * Store a list of sha1s that are should not be discarded2760 * because they are either written too recently, or are2761 * reachable from another object that was.2762 *2763 * This is filled by get_object_list.2764 */2765static struct oid_array recent_objects;27662767static intloosened_object_can_be_discarded(const struct object_id *oid,2768 timestamp_t mtime)2769{2770if(!unpack_unreachable_expiration)2771return0;2772if(mtime > unpack_unreachable_expiration)2773return0;2774if(oid_array_lookup(&recent_objects, oid) >=0)2775return0;2776return1;2777}27782779static voidloosen_unused_packed_objects(struct rev_info *revs)2780{2781struct packed_git *p;2782uint32_t i;2783struct object_id oid;27842785for(p =get_packed_git(the_repository); p; p = p->next) {2786if(!p->pack_local || p->pack_keep)2787continue;27882789if(open_pack_index(p))2790die("cannot open pack index");27912792for(i =0; i < p->num_objects; i++) {2793nth_packed_object_oid(&oid, p, i);2794if(!packlist_find(&to_pack, oid.hash, NULL) &&2795!has_sha1_pack_kept_or_nonlocal(&oid) &&2796!loosened_object_can_be_discarded(&oid, p->mtime))2797if(force_object_loose(&oid, p->mtime))2798die("unable to force loose object");2799}2800}2801}28022803/*2804 * This tracks any options which pack-reuse code expects to be on, or which a2805 * reader of the pack might not understand, and which would therefore prevent2806 * blind reuse of what we have on disk.2807 */2808static intpack_options_allow_reuse(void)2809{2810return pack_to_stdout &&2811 allow_ofs_delta &&2812!ignore_packed_keep &&2813(!local || !have_non_local_packs) &&2814!incremental;2815}28162817static intget_object_list_from_bitmap(struct rev_info *revs)2818{2819if(prepare_bitmap_walk(revs) <0)2820return-1;28212822if(pack_options_allow_reuse() &&2823!reuse_partial_packfile_from_bitmap(2824&reuse_packfile,2825&reuse_packfile_objects,2826&reuse_packfile_offset)) {2827assert(reuse_packfile_objects);2828 nr_result += reuse_packfile_objects;2829display_progress(progress_state, nr_result);2830}28312832traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2833return0;2834}28352836static voidrecord_recent_object(struct object *obj,2837const char*name,2838void*data)2839{2840oid_array_append(&recent_objects, &obj->oid);2841}28422843static voidrecord_recent_commit(struct commit *commit,void*data)2844{2845oid_array_append(&recent_objects, &commit->object.oid);2846}28472848static voidget_object_list(int ac,const char**av)2849{2850struct rev_info revs;2851char line[1000];2852int flags =0;28532854init_revisions(&revs, NULL);2855 save_commit_buffer =0;2856setup_revisions(ac, av, &revs, NULL);28572858/* make sure shallows are read */2859is_repository_shallow();28602861while(fgets(line,sizeof(line), stdin) != NULL) {2862int len =strlen(line);2863if(len && line[len -1] =='\n')2864 line[--len] =0;2865if(!len)2866break;2867if(*line =='-') {2868if(!strcmp(line,"--not")) {2869 flags ^= UNINTERESTING;2870 write_bitmap_index =0;2871continue;2872}2873if(starts_with(line,"--shallow ")) {2874struct object_id oid;2875if(get_oid_hex(line +10, &oid))2876die("not an SHA-1 '%s'", line +10);2877register_shallow(&oid);2878 use_bitmap_index =0;2879continue;2880}2881die("not a rev '%s'", line);2882}2883if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2884die("bad revision '%s'", line);2885}28862887if(use_bitmap_index && !get_object_list_from_bitmap(&revs))2888return;28892890if(prepare_revision_walk(&revs))2891die("revision walk setup failed");2892mark_edges_uninteresting(&revs, show_edge);28932894if(!fn_show_object)2895 fn_show_object = show_object;2896traverse_commit_list_filtered(&filter_options, &revs,2897 show_commit, fn_show_object, NULL,2898 NULL);28992900if(unpack_unreachable_expiration) {2901 revs.ignore_missing_links =1;2902if(add_unseen_recent_objects_to_traversal(&revs,2903 unpack_unreachable_expiration))2904die("unable to add recent objects");2905if(prepare_revision_walk(&revs))2906die("revision walk setup failed");2907traverse_commit_list(&revs, record_recent_commit,2908 record_recent_object, NULL);2909}29102911if(keep_unreachable)2912add_objects_in_unpacked_packs(&revs);2913if(pack_loose_unreachable)2914add_unreachable_loose_objects();2915if(unpack_unreachable)2916loosen_unused_packed_objects(&revs);29172918oid_array_clear(&recent_objects);2919}29202921static intoption_parse_index_version(const struct option *opt,2922const char*arg,int unset)2923{2924char*c;2925const char*val = arg;2926 pack_idx_opts.version =strtoul(val, &c,10);2927if(pack_idx_opts.version >2)2928die(_("unsupported index version%s"), val);2929if(*c ==','&& c[1])2930 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);2931if(*c || pack_idx_opts.off32_limit &0x80000000)2932die(_("bad index version '%s'"), val);2933return0;2934}29352936static intoption_parse_unpack_unreachable(const struct option *opt,2937const char*arg,int unset)2938{2939if(unset) {2940 unpack_unreachable =0;2941 unpack_unreachable_expiration =0;2942}2943else{2944 unpack_unreachable =1;2945if(arg)2946 unpack_unreachable_expiration =approxidate(arg);2947}2948return0;2949}29502951intcmd_pack_objects(int argc,const char**argv,const char*prefix)2952{2953int use_internal_rev_list =0;2954int thin =0;2955int shallow =0;2956int all_progress_implied =0;2957struct argv_array rp = ARGV_ARRAY_INIT;2958int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;2959int rev_list_index =0;2960struct option pack_objects_options[] = {2961OPT_SET_INT('q',"quiet", &progress,2962N_("do not show progress meter"),0),2963OPT_SET_INT(0,"progress", &progress,2964N_("show progress meter"),1),2965OPT_SET_INT(0,"all-progress", &progress,2966N_("show progress meter during object writing phase"),2),2967OPT_BOOL(0,"all-progress-implied",2968&all_progress_implied,2969N_("similar to --all-progress when progress meter is shown")),2970{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),2971N_("write the pack index file in the specified idx format version"),29720, option_parse_index_version },2973OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,2974N_("maximum size of each output pack file")),2975OPT_BOOL(0,"local", &local,2976N_("ignore borrowed objects from alternate object store")),2977OPT_BOOL(0,"incremental", &incremental,2978N_("ignore packed objects")),2979OPT_INTEGER(0,"window", &window,2980N_("limit pack window by objects")),2981OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,2982N_("limit pack window by memory in addition to object limit")),2983OPT_INTEGER(0,"depth", &depth,2984N_("maximum length of delta chain allowed in the resulting pack")),2985OPT_BOOL(0,"reuse-delta", &reuse_delta,2986N_("reuse existing deltas")),2987OPT_BOOL(0,"reuse-object", &reuse_object,2988N_("reuse existing objects")),2989OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,2990N_("use OFS_DELTA objects")),2991OPT_INTEGER(0,"threads", &delta_search_threads,2992N_("use threads when searching for best delta matches")),2993OPT_BOOL(0,"non-empty", &non_empty,2994N_("do not create an empty pack output")),2995OPT_BOOL(0,"revs", &use_internal_rev_list,2996N_("read revision arguments from standard input")),2997{ OPTION_SET_INT,0,"unpacked", &rev_list_unpacked, NULL,2998N_("limit the objects to those that are not yet packed"),2999 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3000{ OPTION_SET_INT,0,"all", &rev_list_all, NULL,3001N_("include objects reachable from any reference"),3002 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3003{ OPTION_SET_INT,0,"reflog", &rev_list_reflog, NULL,3004N_("include objects referred by reflog entries"),3005 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3006{ OPTION_SET_INT,0,"indexed-objects", &rev_list_index, NULL,3007N_("include objects referred to by the index"),3008 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3009OPT_BOOL(0,"stdout", &pack_to_stdout,3010N_("output pack to stdout")),3011OPT_BOOL(0,"include-tag", &include_tag,3012N_("include tag objects that refer to objects to be packed")),3013OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3014N_("keep unreachable objects")),3015OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3016N_("pack loose unreachable objects")),3017{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3018N_("unpack unreachable objects newer than <time>"),3019 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3020OPT_BOOL(0,"thin", &thin,3021N_("create thin packs")),3022OPT_BOOL(0,"shallow", &shallow,3023N_("create packs suitable for shallow fetches")),3024OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep,3025N_("ignore packs that have companion .keep file")),3026OPT_INTEGER(0,"compression", &pack_compression_level,3027N_("pack compression level")),3028OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3029N_("do not hide commits by grafts"),0),3030OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3031N_("use a bitmap index if available to speed up counting objects")),3032OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3033N_("write a bitmap index together with the pack index")),3034OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3035{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3036N_("handling for missing objects"), PARSE_OPT_NONEG,3037 option_parse_missing_action },3038OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3039N_("do not pack objects in promisor packfiles")),3040OPT_END(),3041};30423043 check_replace_refs =0;30443045reset_pack_idx_option(&pack_idx_opts);3046git_config(git_pack_config, NULL);30473048 progress =isatty(2);3049 argc =parse_options(argc, argv, prefix, pack_objects_options,3050 pack_usage,0);30513052if(argc) {3053 base_name = argv[0];3054 argc--;3055}3056if(pack_to_stdout != !base_name || argc)3057usage_with_options(pack_usage, pack_objects_options);30583059argv_array_push(&rp,"pack-objects");3060if(thin) {3061 use_internal_rev_list =1;3062argv_array_push(&rp, shallow3063?"--objects-edge-aggressive"3064:"--objects-edge");3065}else3066argv_array_push(&rp,"--objects");30673068if(rev_list_all) {3069 use_internal_rev_list =1;3070argv_array_push(&rp,"--all");3071}3072if(rev_list_reflog) {3073 use_internal_rev_list =1;3074argv_array_push(&rp,"--reflog");3075}3076if(rev_list_index) {3077 use_internal_rev_list =1;3078argv_array_push(&rp,"--indexed-objects");3079}3080if(rev_list_unpacked) {3081 use_internal_rev_list =1;3082argv_array_push(&rp,"--unpacked");3083}30843085if(exclude_promisor_objects) {3086 use_internal_rev_list =1;3087 fetch_if_missing =0;3088argv_array_push(&rp,"--exclude-promisor-objects");3089}30903091if(!reuse_object)3092 reuse_delta =0;3093if(pack_compression_level == -1)3094 pack_compression_level = Z_DEFAULT_COMPRESSION;3095else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3096die("bad pack compression level%d", pack_compression_level);30973098if(!delta_search_threads)/* --threads=0 means autodetect */3099 delta_search_threads =online_cpus();31003101#ifdef NO_PTHREADS3102if(delta_search_threads !=1)3103warning("no threads support, ignoring --threads");3104#endif3105if(!pack_to_stdout && !pack_size_limit)3106 pack_size_limit = pack_size_limit_cfg;3107if(pack_to_stdout && pack_size_limit)3108die("--max-pack-size cannot be used to build a pack for transfer.");3109if(pack_size_limit && pack_size_limit <1024*1024) {3110warning("minimum pack size limit is 1 MiB");3111 pack_size_limit =1024*1024;3112}31133114if(!pack_to_stdout && thin)3115die("--thin cannot be used to build an indexable pack.");31163117if(keep_unreachable && unpack_unreachable)3118die("--keep-unreachable and --unpack-unreachable are incompatible.");3119if(!rev_list_all || !rev_list_reflog || !rev_list_index)3120 unpack_unreachable_expiration =0;31213122if(filter_options.choice) {3123if(!pack_to_stdout)3124die("cannot use --filter without --stdout.");3125 use_bitmap_index =0;3126}31273128/*3129 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3130 *3131 * - to produce good pack (with bitmap index not-yet-packed objects are3132 * packed in suboptimal order).3133 *3134 * - to use more robust pack-generation codepath (avoiding possible3135 * bugs in bitmap code and possible bitmap index corruption).3136 */3137if(!pack_to_stdout)3138 use_bitmap_index_default =0;31393140if(use_bitmap_index <0)3141 use_bitmap_index = use_bitmap_index_default;31423143/* "hard" reasons not to use bitmaps; these just won't work at all */3144if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3145 use_bitmap_index =0;31463147if(pack_to_stdout || !rev_list_all)3148 write_bitmap_index =0;31493150if(progress && all_progress_implied)3151 progress =2;31523153if(ignore_packed_keep) {3154struct packed_git *p;3155for(p =get_packed_git(the_repository); p; p = p->next)3156if(p->pack_local && p->pack_keep)3157break;3158if(!p)/* no keep-able packs found */3159 ignore_packed_keep =0;3160}3161if(local) {3162/*3163 * unlike ignore_packed_keep above, we do not want to3164 * unset "local" based on looking at packs, as it3165 * also covers non-local objects3166 */3167struct packed_git *p;3168for(p =get_packed_git(the_repository); p; p = p->next) {3169if(!p->pack_local) {3170 have_non_local_packs =1;3171break;3172}3173}3174}31753176if(progress)3177 progress_state =start_progress(_("Counting objects"),0);3178if(!use_internal_rev_list)3179read_object_list_from_stdin();3180else{3181get_object_list(rp.argc, rp.argv);3182argv_array_clear(&rp);3183}3184cleanup_preferred_base();3185if(include_tag && nr_result)3186for_each_ref(add_ref_tag, NULL);3187stop_progress(&progress_state);31883189if(non_empty && !nr_result)3190return0;3191if(nr_result)3192prepare_pack(window, depth);3193write_pack_file();3194if(progress)3195fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3196" reused %"PRIu32" (delta %"PRIu32")\n",3197 written, written_delta, reused, reused_delta);3198return0;3199}