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; 267 268if(!usable_delta) { 269if(oe_type(entry) == OBJ_BLOB && 270 entry->size > big_file_threshold && 271(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 272 buf = NULL; 273else{ 274 buf =read_object_file(&entry->idx.oid, &type, &size); 275if(!buf) 276die(_("unable to read%s"), 277oid_to_hex(&entry->idx.oid)); 278} 279/* 280 * make sure no cached delta data remains from a 281 * previous attempt before a pack split occurred. 282 */ 283FREE_AND_NULL(entry->delta_data); 284 entry->z_delta_size =0; 285}else if(entry->delta_data) { 286 size = entry->delta_size; 287 buf = entry->delta_data; 288 entry->delta_data = NULL; 289 type = (allow_ofs_delta && entry->delta->idx.offset) ? 290 OBJ_OFS_DELTA : OBJ_REF_DELTA; 291}else{ 292 buf =get_delta(entry); 293 size = entry->delta_size; 294 type = (allow_ofs_delta && entry->delta->idx.offset) ? 295 OBJ_OFS_DELTA : OBJ_REF_DELTA; 296} 297 298if(st)/* large blob case, just assume we don't compress well */ 299 datalen = size; 300else if(entry->z_delta_size) 301 datalen = entry->z_delta_size; 302else 303 datalen =do_compress(&buf, size); 304 305/* 306 * The object header is a byte of 'type' followed by zero or 307 * more bytes of length. 308 */ 309 hdrlen =encode_in_pack_object_header(header,sizeof(header), 310 type, size); 311 312if(type == OBJ_OFS_DELTA) { 313/* 314 * Deltas with relative base contain an additional 315 * encoding of the relative offset for the delta 316 * base from this object's position in the pack. 317 */ 318 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 319unsigned pos =sizeof(dheader) -1; 320 dheader[pos] = ofs &127; 321while(ofs >>=7) 322 dheader[--pos] =128| (--ofs &127); 323if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 324if(st) 325close_istream(st); 326free(buf); 327return0; 328} 329hashwrite(f, header, hdrlen); 330hashwrite(f, dheader + pos,sizeof(dheader) - pos); 331 hdrlen +=sizeof(dheader) - pos; 332}else if(type == OBJ_REF_DELTA) { 333/* 334 * Deltas with a base reference contain 335 * an additional 20 bytes for the base sha1. 336 */ 337if(limit && hdrlen +20+ datalen +20>= limit) { 338if(st) 339close_istream(st); 340free(buf); 341return0; 342} 343hashwrite(f, header, hdrlen); 344hashwrite(f, entry->delta->idx.oid.hash,20); 345 hdrlen +=20; 346}else{ 347if(limit && hdrlen + datalen +20>= limit) { 348if(st) 349close_istream(st); 350free(buf); 351return0; 352} 353hashwrite(f, header, hdrlen); 354} 355if(st) { 356 datalen =write_large_blob_data(st, f, &entry->idx.oid); 357close_istream(st); 358}else{ 359hashwrite(f, buf, datalen); 360free(buf); 361} 362 363return hdrlen + datalen; 364} 365 366/* Return 0 if we will bust the pack-size limit */ 367static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 368unsigned long limit,int usable_delta) 369{ 370struct packed_git *p = entry->in_pack; 371struct pack_window *w_curs = NULL; 372struct revindex_entry *revidx; 373 off_t offset; 374enum object_type type =oe_type(entry); 375 off_t datalen; 376unsigned char header[MAX_PACK_OBJECT_HEADER], 377 dheader[MAX_PACK_OBJECT_HEADER]; 378unsigned hdrlen; 379 380if(entry->delta) 381 type = (allow_ofs_delta && entry->delta->idx.offset) ? 382 OBJ_OFS_DELTA : OBJ_REF_DELTA; 383 hdrlen =encode_in_pack_object_header(header,sizeof(header), 384 type, entry->size); 385 386 offset = entry->in_pack_offset; 387 revidx =find_pack_revindex(p, offset); 388 datalen = revidx[1].offset - offset; 389if(!pack_to_stdout && p->index_version >1&& 390check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 391error("bad packed object CRC for%s", 392oid_to_hex(&entry->idx.oid)); 393unuse_pack(&w_curs); 394returnwrite_no_reuse_object(f, entry, limit, usable_delta); 395} 396 397 offset += entry->in_pack_header_size; 398 datalen -= entry->in_pack_header_size; 399 400if(!pack_to_stdout && p->index_version ==1&& 401check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) { 402error("corrupt packed object for%s", 403oid_to_hex(&entry->idx.oid)); 404unuse_pack(&w_curs); 405returnwrite_no_reuse_object(f, entry, limit, usable_delta); 406} 407 408if(type == OBJ_OFS_DELTA) { 409 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 410unsigned pos =sizeof(dheader) -1; 411 dheader[pos] = ofs &127; 412while(ofs >>=7) 413 dheader[--pos] =128| (--ofs &127); 414if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 415unuse_pack(&w_curs); 416return0; 417} 418hashwrite(f, header, hdrlen); 419hashwrite(f, dheader + pos,sizeof(dheader) - pos); 420 hdrlen +=sizeof(dheader) - pos; 421 reused_delta++; 422}else if(type == OBJ_REF_DELTA) { 423if(limit && hdrlen +20+ datalen +20>= limit) { 424unuse_pack(&w_curs); 425return0; 426} 427hashwrite(f, header, hdrlen); 428hashwrite(f, entry->delta->idx.oid.hash,20); 429 hdrlen +=20; 430 reused_delta++; 431}else{ 432if(limit && hdrlen + datalen +20>= limit) { 433unuse_pack(&w_curs); 434return0; 435} 436hashwrite(f, header, hdrlen); 437} 438copy_pack_data(f, p, &w_curs, offset, datalen); 439unuse_pack(&w_curs); 440 reused++; 441return hdrlen + datalen; 442} 443 444/* Return 0 if we will bust the pack-size limit */ 445static off_t write_object(struct hashfile *f, 446struct object_entry *entry, 447 off_t write_offset) 448{ 449unsigned long limit; 450 off_t len; 451int usable_delta, to_reuse; 452 453if(!pack_to_stdout) 454crc32_begin(f); 455 456/* apply size limit if limited packsize and not first object */ 457if(!pack_size_limit || !nr_written) 458 limit =0; 459else if(pack_size_limit <= write_offset) 460/* 461 * the earlier object did not fit the limit; avoid 462 * mistaking this with unlimited (i.e. limit = 0). 463 */ 464 limit =1; 465else 466 limit = pack_size_limit - write_offset; 467 468if(!entry->delta) 469 usable_delta =0;/* no delta */ 470else if(!pack_size_limit) 471 usable_delta =1;/* unlimited packfile */ 472else if(entry->delta->idx.offset == (off_t)-1) 473 usable_delta =0;/* base was written to another pack */ 474else if(entry->delta->idx.offset) 475 usable_delta =1;/* base already exists in this pack */ 476else 477 usable_delta =0;/* base could end up in another pack */ 478 479if(!reuse_object) 480 to_reuse =0;/* explicit */ 481else if(!entry->in_pack) 482 to_reuse =0;/* can't reuse what we don't have */ 483else if(oe_type(entry) == OBJ_REF_DELTA || 484oe_type(entry) == OBJ_OFS_DELTA) 485/* check_object() decided it for us ... */ 486 to_reuse = usable_delta; 487/* ... but pack split may override that */ 488else if(oe_type(entry) != entry->in_pack_type) 489 to_reuse =0;/* pack has delta which is unusable */ 490else if(entry->delta) 491 to_reuse =0;/* we want to pack afresh */ 492else 493 to_reuse =1;/* we have it in-pack undeltified, 494 * and we do not need to deltify it. 495 */ 496 497if(!to_reuse) 498 len =write_no_reuse_object(f, entry, limit, usable_delta); 499else 500 len =write_reuse_object(f, entry, limit, usable_delta); 501if(!len) 502return0; 503 504if(usable_delta) 505 written_delta++; 506 written++; 507if(!pack_to_stdout) 508 entry->idx.crc32 =crc32_end(f); 509return len; 510} 511 512enum write_one_status { 513 WRITE_ONE_SKIP = -1,/* already written */ 514 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 515 WRITE_ONE_WRITTEN =1,/* normal */ 516 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 517}; 518 519static enum write_one_status write_one(struct hashfile *f, 520struct object_entry *e, 521 off_t *offset) 522{ 523 off_t size; 524int recursing; 525 526/* 527 * we set offset to 1 (which is an impossible value) to mark 528 * the fact that this object is involved in "write its base 529 * first before writing a deltified object" recursion. 530 */ 531 recursing = (e->idx.offset ==1); 532if(recursing) { 533warning("recursive delta detected for object%s", 534oid_to_hex(&e->idx.oid)); 535return WRITE_ONE_RECURSIVE; 536}else if(e->idx.offset || e->preferred_base) { 537/* offset is non zero if object is written already. */ 538return WRITE_ONE_SKIP; 539} 540 541/* if we are deltified, write out base object first. */ 542if(e->delta) { 543 e->idx.offset =1;/* now recurse */ 544switch(write_one(f, e->delta, offset)) { 545case WRITE_ONE_RECURSIVE: 546/* we cannot depend on this one */ 547 e->delta = NULL; 548break; 549default: 550break; 551case WRITE_ONE_BREAK: 552 e->idx.offset = recursing; 553return WRITE_ONE_BREAK; 554} 555} 556 557 e->idx.offset = *offset; 558 size =write_object(f, e, *offset); 559if(!size) { 560 e->idx.offset = recursing; 561return WRITE_ONE_BREAK; 562} 563 written_list[nr_written++] = &e->idx; 564 565/* make sure off_t is sufficiently large not to wrap */ 566if(signed_add_overflows(*offset, size)) 567die("pack too large for current definition of off_t"); 568*offset += size; 569return WRITE_ONE_WRITTEN; 570} 571 572static intmark_tagged(const char*path,const struct object_id *oid,int flag, 573void*cb_data) 574{ 575struct object_id peeled; 576struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 577 578if(entry) 579 entry->tagged =1; 580if(!peel_ref(path, &peeled)) { 581 entry =packlist_find(&to_pack, peeled.hash, NULL); 582if(entry) 583 entry->tagged =1; 584} 585return0; 586} 587 588staticinlinevoidadd_to_write_order(struct object_entry **wo, 589unsigned int*endp, 590struct object_entry *e) 591{ 592if(e->filled) 593return; 594 wo[(*endp)++] = e; 595 e->filled =1; 596} 597 598static voidadd_descendants_to_write_order(struct object_entry **wo, 599unsigned int*endp, 600struct object_entry *e) 601{ 602int add_to_order =1; 603while(e) { 604if(add_to_order) { 605struct object_entry *s; 606/* add this node... */ 607add_to_write_order(wo, endp, e); 608/* all its siblings... */ 609for(s = e->delta_sibling; s; s = s->delta_sibling) { 610add_to_write_order(wo, endp, s); 611} 612} 613/* drop down a level to add left subtree nodes if possible */ 614if(e->delta_child) { 615 add_to_order =1; 616 e = e->delta_child; 617}else{ 618 add_to_order =0; 619/* our sibling might have some children, it is next */ 620if(e->delta_sibling) { 621 e = e->delta_sibling; 622continue; 623} 624/* go back to our parent node */ 625 e = e->delta; 626while(e && !e->delta_sibling) { 627/* we're on the right side of a subtree, keep 628 * going up until we can go right again */ 629 e = e->delta; 630} 631if(!e) { 632/* done- we hit our original root node */ 633return; 634} 635/* pass it off to sibling at this level */ 636 e = e->delta_sibling; 637} 638}; 639} 640 641static voidadd_family_to_write_order(struct object_entry **wo, 642unsigned int*endp, 643struct object_entry *e) 644{ 645struct object_entry *root; 646 647for(root = e; root->delta; root = root->delta) 648;/* nothing */ 649add_descendants_to_write_order(wo, endp, root); 650} 651 652static struct object_entry **compute_write_order(void) 653{ 654unsigned int i, wo_end, last_untagged; 655 656struct object_entry **wo; 657struct object_entry *objects = to_pack.objects; 658 659for(i =0; i < to_pack.nr_objects; i++) { 660 objects[i].tagged =0; 661 objects[i].filled =0; 662 objects[i].delta_child = NULL; 663 objects[i].delta_sibling = NULL; 664} 665 666/* 667 * Fully connect delta_child/delta_sibling network. 668 * Make sure delta_sibling is sorted in the original 669 * recency order. 670 */ 671for(i = to_pack.nr_objects; i >0;) { 672struct object_entry *e = &objects[--i]; 673if(!e->delta) 674continue; 675/* Mark me as the first child */ 676 e->delta_sibling = e->delta->delta_child; 677 e->delta->delta_child = e; 678} 679 680/* 681 * Mark objects that are at the tip of tags. 682 */ 683for_each_tag_ref(mark_tagged, NULL); 684 685/* 686 * Give the objects in the original recency order until 687 * we see a tagged tip. 688 */ 689ALLOC_ARRAY(wo, to_pack.nr_objects); 690for(i = wo_end =0; i < to_pack.nr_objects; i++) { 691if(objects[i].tagged) 692break; 693add_to_write_order(wo, &wo_end, &objects[i]); 694} 695 last_untagged = i; 696 697/* 698 * Then fill all the tagged tips. 699 */ 700for(; i < to_pack.nr_objects; i++) { 701if(objects[i].tagged) 702add_to_write_order(wo, &wo_end, &objects[i]); 703} 704 705/* 706 * And then all remaining commits and tags. 707 */ 708for(i = last_untagged; i < to_pack.nr_objects; i++) { 709if(oe_type(&objects[i]) != OBJ_COMMIT && 710oe_type(&objects[i]) != OBJ_TAG) 711continue; 712add_to_write_order(wo, &wo_end, &objects[i]); 713} 714 715/* 716 * And then all the trees. 717 */ 718for(i = last_untagged; i < to_pack.nr_objects; i++) { 719if(oe_type(&objects[i]) != OBJ_TREE) 720continue; 721add_to_write_order(wo, &wo_end, &objects[i]); 722} 723 724/* 725 * Finally all the rest in really tight order 726 */ 727for(i = last_untagged; i < to_pack.nr_objects; i++) { 728if(!objects[i].filled) 729add_family_to_write_order(wo, &wo_end, &objects[i]); 730} 731 732if(wo_end != to_pack.nr_objects) 733die("ordered%uobjects, expected %"PRIu32, wo_end, to_pack.nr_objects); 734 735return wo; 736} 737 738static off_t write_reused_pack(struct hashfile *f) 739{ 740unsigned char buffer[8192]; 741 off_t to_write, total; 742int fd; 743 744if(!is_pack_valid(reuse_packfile)) 745die("packfile is invalid:%s", reuse_packfile->pack_name); 746 747 fd =git_open(reuse_packfile->pack_name); 748if(fd <0) 749die_errno("unable to open packfile for reuse:%s", 750 reuse_packfile->pack_name); 751 752if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 753die_errno("unable to seek in reused packfile"); 754 755if(reuse_packfile_offset <0) 756 reuse_packfile_offset = reuse_packfile->pack_size -20; 757 758 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 759 760while(to_write) { 761int read_pack =xread(fd, buffer,sizeof(buffer)); 762 763if(read_pack <=0) 764die_errno("unable to read from reused packfile"); 765 766if(read_pack > to_write) 767 read_pack = to_write; 768 769hashwrite(f, buffer, read_pack); 770 to_write -= read_pack; 771 772/* 773 * We don't know the actual number of objects written, 774 * only how many bytes written, how many bytes total, and 775 * how many objects total. So we can fake it by pretending all 776 * objects we are writing are the same size. This gives us a 777 * smooth progress meter, and at the end it matches the true 778 * answer. 779 */ 780 written = reuse_packfile_objects * 781(((double)(total - to_write)) / total); 782display_progress(progress_state, written); 783} 784 785close(fd); 786 written = reuse_packfile_objects; 787display_progress(progress_state, written); 788return reuse_packfile_offset -sizeof(struct pack_header); 789} 790 791static const char no_split_warning[] =N_( 792"disabling bitmap writing, packs are split due to pack.packSizeLimit" 793); 794 795static voidwrite_pack_file(void) 796{ 797uint32_t i =0, j; 798struct hashfile *f; 799 off_t offset; 800uint32_t nr_remaining = nr_result; 801time_t last_mtime =0; 802struct object_entry **write_order; 803 804if(progress > pack_to_stdout) 805 progress_state =start_progress(_("Writing objects"), nr_result); 806ALLOC_ARRAY(written_list, to_pack.nr_objects); 807 write_order =compute_write_order(); 808 809do{ 810struct object_id oid; 811char*pack_tmp_name = NULL; 812 813if(pack_to_stdout) 814 f =hashfd_throughput(1,"<stdout>", progress_state); 815else 816 f =create_tmp_packfile(&pack_tmp_name); 817 818 offset =write_pack_header(f, nr_remaining); 819 820if(reuse_packfile) { 821 off_t packfile_size; 822assert(pack_to_stdout); 823 824 packfile_size =write_reused_pack(f); 825 offset += packfile_size; 826} 827 828 nr_written =0; 829for(; i < to_pack.nr_objects; i++) { 830struct object_entry *e = write_order[i]; 831if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 832break; 833display_progress(progress_state, written); 834} 835 836/* 837 * Did we write the wrong # entries in the header? 838 * If so, rewrite it like in fast-import 839 */ 840if(pack_to_stdout) { 841hashclose(f, oid.hash, CSUM_CLOSE); 842}else if(nr_written == nr_remaining) { 843hashclose(f, oid.hash, CSUM_FSYNC); 844}else{ 845int fd =hashclose(f, oid.hash,0); 846fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 847 nr_written, oid.hash, offset); 848close(fd); 849if(write_bitmap_index) { 850warning(_(no_split_warning)); 851 write_bitmap_index =0; 852} 853} 854 855if(!pack_to_stdout) { 856struct stat st; 857struct strbuf tmpname = STRBUF_INIT; 858 859/* 860 * Packs are runtime accessed in their mtime 861 * order since newer packs are more likely to contain 862 * younger objects. So if we are creating multiple 863 * packs then we should modify the mtime of later ones 864 * to preserve this property. 865 */ 866if(stat(pack_tmp_name, &st) <0) { 867warning_errno("failed to stat%s", pack_tmp_name); 868}else if(!last_mtime) { 869 last_mtime = st.st_mtime; 870}else{ 871struct utimbuf utb; 872 utb.actime = st.st_atime; 873 utb.modtime = --last_mtime; 874if(utime(pack_tmp_name, &utb) <0) 875warning_errno("failed utime() on%s", pack_tmp_name); 876} 877 878strbuf_addf(&tmpname,"%s-", base_name); 879 880if(write_bitmap_index) { 881bitmap_writer_set_checksum(oid.hash); 882bitmap_writer_build_type_index(written_list, nr_written); 883} 884 885finish_tmp_packfile(&tmpname, pack_tmp_name, 886 written_list, nr_written, 887&pack_idx_opts, oid.hash); 888 889if(write_bitmap_index) { 890strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 891 892stop_progress(&progress_state); 893 894bitmap_writer_show_progress(progress); 895bitmap_writer_reuse_bitmaps(&to_pack); 896bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 897bitmap_writer_build(&to_pack); 898bitmap_writer_finish(written_list, nr_written, 899 tmpname.buf, write_bitmap_options); 900 write_bitmap_index =0; 901} 902 903strbuf_release(&tmpname); 904free(pack_tmp_name); 905puts(oid_to_hex(&oid)); 906} 907 908/* mark written objects as written to previous pack */ 909for(j =0; j < nr_written; j++) { 910 written_list[j]->offset = (off_t)-1; 911} 912 nr_remaining -= nr_written; 913}while(nr_remaining && i < to_pack.nr_objects); 914 915free(written_list); 916free(write_order); 917stop_progress(&progress_state); 918if(written != nr_result) 919die("wrote %"PRIu32" objects while expecting %"PRIu32, 920 written, nr_result); 921} 922 923static intno_try_delta(const char*path) 924{ 925static struct attr_check *check; 926 927if(!check) 928 check =attr_check_initl("delta", NULL); 929if(git_check_attr(path, check)) 930return0; 931if(ATTR_FALSE(check->items[0].value)) 932return1; 933return0; 934} 935 936/* 937 * When adding an object, check whether we have already added it 938 * to our packing list. If so, we can skip. However, if we are 939 * being asked to excludei t, but the previous mention was to include 940 * it, make sure to adjust its flags and tweak our numbers accordingly. 941 * 942 * As an optimization, we pass out the index position where we would have 943 * found the item, since that saves us from having to look it up again a 944 * few lines later when we want to add the new entry. 945 */ 946static inthave_duplicate_entry(const struct object_id *oid, 947int exclude, 948uint32_t*index_pos) 949{ 950struct object_entry *entry; 951 952 entry =packlist_find(&to_pack, oid->hash, index_pos); 953if(!entry) 954return0; 955 956if(exclude) { 957if(!entry->preferred_base) 958 nr_result--; 959 entry->preferred_base =1; 960} 961 962return1; 963} 964 965static intwant_found_object(int exclude,struct packed_git *p) 966{ 967if(exclude) 968return1; 969if(incremental) 970return0; 971 972/* 973 * When asked to do --local (do not include an object that appears in a 974 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 975 * an object that appears in a pack marked with .keep), finding a pack 976 * that matches the criteria is sufficient for us to decide to omit it. 977 * However, even if this pack does not satisfy the criteria, we need to 978 * make sure no copy of this object appears in _any_ pack that makes us 979 * to omit the object, so we need to check all the packs. 980 * 981 * We can however first check whether these options can possible matter; 982 * if they do not matter we know we want the object in generated pack. 983 * Otherwise, we signal "-1" at the end to tell the caller that we do 984 * not know either way, and it needs to check more packs. 985 */ 986if(!ignore_packed_keep && 987(!local || !have_non_local_packs)) 988return1; 989 990if(local && !p->pack_local) 991return0; 992if(ignore_packed_keep && p->pack_local && p->pack_keep) 993return0; 994 995/* we don't know yet; keep looking for more packs */ 996return-1; 997} 998 999/*1000 * Check whether we want the object in the pack (e.g., we do not want1001 * objects found in non-local stores if the "--local" option was used).1002 *1003 * If the caller already knows an existing pack it wants to take the object1004 * from, that is passed in *found_pack and *found_offset; otherwise this1005 * function finds if there is any pack that has the object and returns the pack1006 * and its offset in these variables.1007 */1008static intwant_object_in_pack(const struct object_id *oid,1009int exclude,1010struct packed_git **found_pack,1011 off_t *found_offset)1012{1013int want;1014struct list_head *pos;10151016if(!exclude && local &&has_loose_object_nonlocal(oid->hash))1017return0;10181019/*1020 * If we already know the pack object lives in, start checks from that1021 * pack - in the usual case when neither --local was given nor .keep files1022 * are present we will determine the answer right now.1023 */1024if(*found_pack) {1025 want =want_found_object(exclude, *found_pack);1026if(want != -1)1027return want;1028}1029list_for_each(pos,get_packed_git_mru(the_repository)) {1030struct packed_git *p =list_entry(pos,struct packed_git, mru);1031 off_t offset;10321033if(p == *found_pack)1034 offset = *found_offset;1035else1036 offset =find_pack_entry_one(oid->hash, p);10371038if(offset) {1039if(!*found_pack) {1040if(!is_pack_valid(p))1041continue;1042*found_offset = offset;1043*found_pack = p;1044}1045 want =want_found_object(exclude, p);1046if(!exclude && want >0)1047list_move(&p->mru,1048get_packed_git_mru(the_repository));1049if(want != -1)1050return want;1051}1052}10531054return1;1055}10561057static voidcreate_object_entry(const struct object_id *oid,1058enum object_type type,1059uint32_t hash,1060int exclude,1061int no_try_delta,1062uint32_t index_pos,1063struct packed_git *found_pack,1064 off_t found_offset)1065{1066struct object_entry *entry;10671068 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1069 entry->hash = hash;1070oe_set_type(entry, type);1071if(exclude)1072 entry->preferred_base =1;1073else1074 nr_result++;1075if(found_pack) {1076 entry->in_pack = found_pack;1077 entry->in_pack_offset = found_offset;1078}10791080 entry->no_try_delta = no_try_delta;1081}10821083static const char no_closure_warning[] =N_(1084"disabling bitmap writing, as some objects are not being packed"1085);10861087static intadd_object_entry(const struct object_id *oid,enum object_type type,1088const char*name,int exclude)1089{1090struct packed_git *found_pack = NULL;1091 off_t found_offset =0;1092uint32_t index_pos;10931094if(have_duplicate_entry(oid, exclude, &index_pos))1095return0;10961097if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1098/* The pack is missing an object, so it will not have closure */1099if(write_bitmap_index) {1100warning(_(no_closure_warning));1101 write_bitmap_index =0;1102}1103return0;1104}11051106create_object_entry(oid, type,pack_name_hash(name),1107 exclude, name &&no_try_delta(name),1108 index_pos, found_pack, found_offset);11091110display_progress(progress_state, nr_result);1111return1;1112}11131114static intadd_object_entry_from_bitmap(const struct object_id *oid,1115enum object_type type,1116int flags,uint32_t name_hash,1117struct packed_git *pack, off_t offset)1118{1119uint32_t index_pos;11201121if(have_duplicate_entry(oid,0, &index_pos))1122return0;11231124if(!want_object_in_pack(oid,0, &pack, &offset))1125return0;11261127create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);11281129display_progress(progress_state, nr_result);1130return1;1131}11321133struct pbase_tree_cache {1134struct object_id oid;1135int ref;1136int temporary;1137void*tree_data;1138unsigned long tree_size;1139};11401141static struct pbase_tree_cache *(pbase_tree_cache[256]);1142static intpbase_tree_cache_ix(const struct object_id *oid)1143{1144return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1145}1146static intpbase_tree_cache_ix_incr(int ix)1147{1148return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1149}11501151static struct pbase_tree {1152struct pbase_tree *next;1153/* This is a phony "cache" entry; we are not1154 * going to evict it or find it through _get()1155 * mechanism -- this is for the toplevel node that1156 * would almost always change with any commit.1157 */1158struct pbase_tree_cache pcache;1159} *pbase_tree;11601161static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1162{1163struct pbase_tree_cache *ent, *nent;1164void*data;1165unsigned long size;1166enum object_type type;1167int neigh;1168int my_ix =pbase_tree_cache_ix(oid);1169int available_ix = -1;11701171/* pbase-tree-cache acts as a limited hashtable.1172 * your object will be found at your index or within a few1173 * slots after that slot if it is cached.1174 */1175for(neigh =0; neigh <8; neigh++) {1176 ent = pbase_tree_cache[my_ix];1177if(ent && !oidcmp(&ent->oid, oid)) {1178 ent->ref++;1179return ent;1180}1181else if(((available_ix <0) && (!ent || !ent->ref)) ||1182((0<= available_ix) &&1183(!ent && pbase_tree_cache[available_ix])))1184 available_ix = my_ix;1185if(!ent)1186break;1187 my_ix =pbase_tree_cache_ix_incr(my_ix);1188}11891190/* Did not find one. Either we got a bogus request or1191 * we need to read and perhaps cache.1192 */1193 data =read_object_file(oid, &type, &size);1194if(!data)1195return NULL;1196if(type != OBJ_TREE) {1197free(data);1198return NULL;1199}12001201/* We need to either cache or return a throwaway copy */12021203if(available_ix <0)1204 ent = NULL;1205else{1206 ent = pbase_tree_cache[available_ix];1207 my_ix = available_ix;1208}12091210if(!ent) {1211 nent =xmalloc(sizeof(*nent));1212 nent->temporary = (available_ix <0);1213}1214else{1215/* evict and reuse */1216free(ent->tree_data);1217 nent = ent;1218}1219oidcpy(&nent->oid, oid);1220 nent->tree_data = data;1221 nent->tree_size = size;1222 nent->ref =1;1223if(!nent->temporary)1224 pbase_tree_cache[my_ix] = nent;1225return nent;1226}12271228static voidpbase_tree_put(struct pbase_tree_cache *cache)1229{1230if(!cache->temporary) {1231 cache->ref--;1232return;1233}1234free(cache->tree_data);1235free(cache);1236}12371238static intname_cmp_len(const char*name)1239{1240int i;1241for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1242;1243return i;1244}12451246static voidadd_pbase_object(struct tree_desc *tree,1247const char*name,1248int cmplen,1249const char*fullname)1250{1251struct name_entry entry;1252int cmp;12531254while(tree_entry(tree,&entry)) {1255if(S_ISGITLINK(entry.mode))1256continue;1257 cmp =tree_entry_len(&entry) != cmplen ?1:1258memcmp(name, entry.path, cmplen);1259if(cmp >0)1260continue;1261if(cmp <0)1262return;1263if(name[cmplen] !='/') {1264add_object_entry(entry.oid,1265object_type(entry.mode),1266 fullname,1);1267return;1268}1269if(S_ISDIR(entry.mode)) {1270struct tree_desc sub;1271struct pbase_tree_cache *tree;1272const char*down = name+cmplen+1;1273int downlen =name_cmp_len(down);12741275 tree =pbase_tree_get(entry.oid);1276if(!tree)1277return;1278init_tree_desc(&sub, tree->tree_data, tree->tree_size);12791280add_pbase_object(&sub, down, downlen, fullname);1281pbase_tree_put(tree);1282}1283}1284}12851286static unsigned*done_pbase_paths;1287static int done_pbase_paths_num;1288static int done_pbase_paths_alloc;1289static intdone_pbase_path_pos(unsigned hash)1290{1291int lo =0;1292int hi = done_pbase_paths_num;1293while(lo < hi) {1294int mi = lo + (hi - lo) /2;1295if(done_pbase_paths[mi] == hash)1296return mi;1297if(done_pbase_paths[mi] < hash)1298 hi = mi;1299else1300 lo = mi +1;1301}1302return-lo-1;1303}13041305static intcheck_pbase_path(unsigned hash)1306{1307int pos =done_pbase_path_pos(hash);1308if(0<= pos)1309return1;1310 pos = -pos -1;1311ALLOC_GROW(done_pbase_paths,1312 done_pbase_paths_num +1,1313 done_pbase_paths_alloc);1314 done_pbase_paths_num++;1315if(pos < done_pbase_paths_num)1316MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1317 done_pbase_paths_num - pos -1);1318 done_pbase_paths[pos] = hash;1319return0;1320}13211322static voidadd_preferred_base_object(const char*name)1323{1324struct pbase_tree *it;1325int cmplen;1326unsigned hash =pack_name_hash(name);13271328if(!num_preferred_base ||check_pbase_path(hash))1329return;13301331 cmplen =name_cmp_len(name);1332for(it = pbase_tree; it; it = it->next) {1333if(cmplen ==0) {1334add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1335}1336else{1337struct tree_desc tree;1338init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1339add_pbase_object(&tree, name, cmplen, name);1340}1341}1342}13431344static voidadd_preferred_base(struct object_id *oid)1345{1346struct pbase_tree *it;1347void*data;1348unsigned long size;1349struct object_id tree_oid;13501351if(window <= num_preferred_base++)1352return;13531354 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1355if(!data)1356return;13571358for(it = pbase_tree; it; it = it->next) {1359if(!oidcmp(&it->pcache.oid, &tree_oid)) {1360free(data);1361return;1362}1363}13641365 it =xcalloc(1,sizeof(*it));1366 it->next = pbase_tree;1367 pbase_tree = it;13681369oidcpy(&it->pcache.oid, &tree_oid);1370 it->pcache.tree_data = data;1371 it->pcache.tree_size = size;1372}13731374static voidcleanup_preferred_base(void)1375{1376struct pbase_tree *it;1377unsigned i;13781379 it = pbase_tree;1380 pbase_tree = NULL;1381while(it) {1382struct pbase_tree *tmp = it;1383 it = tmp->next;1384free(tmp->pcache.tree_data);1385free(tmp);1386}13871388for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1389if(!pbase_tree_cache[i])1390continue;1391free(pbase_tree_cache[i]->tree_data);1392FREE_AND_NULL(pbase_tree_cache[i]);1393}13941395FREE_AND_NULL(done_pbase_paths);1396 done_pbase_paths_num = done_pbase_paths_alloc =0;1397}13981399static voidcheck_object(struct object_entry *entry)1400{1401if(entry->in_pack) {1402struct packed_git *p = entry->in_pack;1403struct pack_window *w_curs = NULL;1404const unsigned char*base_ref = NULL;1405struct object_entry *base_entry;1406unsigned long used, used_0;1407unsigned long avail;1408 off_t ofs;1409unsigned char*buf, c;1410enum object_type type;14111412 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14131414/*1415 * We want in_pack_type even if we do not reuse delta1416 * since non-delta representations could still be reused.1417 */1418 used =unpack_object_header_buffer(buf, avail,1419&type,1420&entry->size);1421if(used ==0)1422goto give_up;14231424if(type <0)1425BUG("invalid type%d", type);1426 entry->in_pack_type = type;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. */1436oe_set_type(entry, entry->in_pack_type);1437 entry->in_pack_header_size = used;1438if(oe_type(entry) < OBJ_COMMIT ||oe_type(entry) > 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 */1492oe_set_type(entry, 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(oe_type(entry)) {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}15231524oe_set_type(entry,oid_object_info(&entry->idx.oid, &entry->size));1525/*1526 * The error condition is checked in prepare_pack(). This is1527 * to permit a missing preferred base object to be ignored1528 * as a preferred base. Doing so can result in a larger1529 * pack file, but the transfer will still take place.1530 */1531}15321533static intpack_offset_sort(const void*_a,const void*_b)1534{1535const struct object_entry *a = *(struct object_entry **)_a;1536const struct object_entry *b = *(struct object_entry **)_b;15371538/* avoid filesystem trashing with loose objects */1539if(!a->in_pack && !b->in_pack)1540returnoidcmp(&a->idx.oid, &b->idx.oid);15411542if(a->in_pack < b->in_pack)1543return-1;1544if(a->in_pack > b->in_pack)1545return1;1546return a->in_pack_offset < b->in_pack_offset ? -1:1547(a->in_pack_offset > b->in_pack_offset);1548}15491550/*1551 * Drop an on-disk delta we were planning to reuse. Naively, this would1552 * just involve blanking out the "delta" field, but we have to deal1553 * with some extra book-keeping:1554 *1555 * 1. Removing ourselves from the delta_sibling linked list.1556 *1557 * 2. Updating our size/type to the non-delta representation. These were1558 * either not recorded initially (size) or overwritten with the delta type1559 * (type) when check_object() decided to reuse the delta.1560 *1561 * 3. Resetting our delta depth, as we are now a base object.1562 */1563static voiddrop_reused_delta(struct object_entry *entry)1564{1565struct object_entry **p = &entry->delta->delta_child;1566struct object_info oi = OBJECT_INFO_INIT;1567enum object_type type;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 = &type;1580if(packed_object_info(entry->in_pack, entry->in_pack_offset, &oi) <0) {1581/*1582 * We failed to get the info from this pack for some reason;1583 * fall back to sha1_object_info, which may find another copy.1584 * And if that fails, the error will be recorded in oe_type(entry)1585 * and dealt with in prepare_pack().1586 */1587oe_set_type(entry,oid_object_info(&entry->idx.oid,1588&entry->size));1589}else{1590oe_set_type(entry, type);1591}1592}15931594/*1595 * Follow the chain of deltas from this entry onward, throwing away any links1596 * that cause us to hit a cycle (as determined by the DFS state flags in1597 * the entries).1598 *1599 * We also detect too-long reused chains that would violate our --depth1600 * limit.1601 */1602static voidbreak_delta_chains(struct object_entry *entry)1603{1604/*1605 * The actual depth of each object we will write is stored as an int,1606 * as it cannot exceed our int "depth" limit. But before we break1607 * changes based no that limit, we may potentially go as deep as the1608 * number of objects, which is elsewhere bounded to a uint32_t.1609 */1610uint32_t total_depth;1611struct object_entry *cur, *next;16121613for(cur = entry, total_depth =0;1614 cur;1615 cur = cur->delta, total_depth++) {1616if(cur->dfs_state == DFS_DONE) {1617/*1618 * We've already seen this object and know it isn't1619 * part of a cycle. We do need to append its depth1620 * to our count.1621 */1622 total_depth += cur->depth;1623break;1624}16251626/*1627 * We break cycles before looping, so an ACTIVE state (or any1628 * other cruft which made its way into the state variable)1629 * is a bug.1630 */1631if(cur->dfs_state != DFS_NONE)1632die("BUG: confusing delta dfs state in first pass:%d",1633 cur->dfs_state);16341635/*1636 * Now we know this is the first time we've seen the object. If1637 * it's not a delta, we're done traversing, but we'll mark it1638 * done to save time on future traversals.1639 */1640if(!cur->delta) {1641 cur->dfs_state = DFS_DONE;1642break;1643}16441645/*1646 * Mark ourselves as active and see if the next step causes1647 * us to cycle to another active object. It's important to do1648 * this _before_ we loop, because it impacts where we make the1649 * cut, and thus how our total_depth counter works.1650 * E.g., We may see a partial loop like:1651 *1652 * A -> B -> C -> D -> B1653 *1654 * Cutting B->C breaks the cycle. But now the depth of A is1655 * only 1, and our total_depth counter is at 3. The size of the1656 * error is always one less than the size of the cycle we1657 * broke. Commits C and D were "lost" from A's chain.1658 *1659 * If we instead cut D->B, then the depth of A is correct at 3.1660 * We keep all commits in the chain that we examined.1661 */1662 cur->dfs_state = DFS_ACTIVE;1663if(cur->delta->dfs_state == DFS_ACTIVE) {1664drop_reused_delta(cur);1665 cur->dfs_state = DFS_DONE;1666break;1667}1668}16691670/*1671 * And now that we've gone all the way to the bottom of the chain, we1672 * need to clear the active flags and set the depth fields as1673 * appropriate. Unlike the loop above, which can quit when it drops a1674 * delta, we need to keep going to look for more depth cuts. So we need1675 * an extra "next" pointer to keep going after we reset cur->delta.1676 */1677for(cur = entry; cur; cur = next) {1678 next = cur->delta;16791680/*1681 * We should have a chain of zero or more ACTIVE states down to1682 * a final DONE. We can quit after the DONE, because either it1683 * has no bases, or we've already handled them in a previous1684 * call.1685 */1686if(cur->dfs_state == DFS_DONE)1687break;1688else if(cur->dfs_state != DFS_ACTIVE)1689die("BUG: confusing delta dfs state in second pass:%d",1690 cur->dfs_state);16911692/*1693 * If the total_depth is more than depth, then we need to snip1694 * the chain into two or more smaller chains that don't exceed1695 * the maximum depth. Most of the resulting chains will contain1696 * (depth + 1) entries (i.e., depth deltas plus one base), and1697 * the last chain (i.e., the one containing entry) will contain1698 * whatever entries are left over, namely1699 * (total_depth % (depth + 1)) of them.1700 *1701 * Since we are iterating towards decreasing depth, we need to1702 * decrement total_depth as we go, and we need to write to the1703 * entry what its final depth will be after all of the1704 * snipping. Since we're snipping into chains of length (depth1705 * + 1) entries, the final depth of an entry will be its1706 * original depth modulo (depth + 1). Any time we encounter an1707 * entry whose final depth is supposed to be zero, we snip it1708 * from its delta base, thereby making it so.1709 */1710 cur->depth = (total_depth--) % (depth +1);1711if(!cur->depth)1712drop_reused_delta(cur);17131714 cur->dfs_state = DFS_DONE;1715}1716}17171718static voidget_object_details(void)1719{1720uint32_t i;1721struct object_entry **sorted_by_offset;17221723 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1724for(i =0; i < to_pack.nr_objects; i++)1725 sorted_by_offset[i] = to_pack.objects + i;1726QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17271728for(i =0; i < to_pack.nr_objects; i++) {1729struct object_entry *entry = sorted_by_offset[i];1730check_object(entry);1731if(big_file_threshold < entry->size)1732 entry->no_try_delta =1;1733}17341735/*1736 * This must happen in a second pass, since we rely on the delta1737 * information for the whole list being completed.1738 */1739for(i =0; i < to_pack.nr_objects; i++)1740break_delta_chains(&to_pack.objects[i]);17411742free(sorted_by_offset);1743}17441745/*1746 * We search for deltas in a list sorted by type, by filename hash, and then1747 * by size, so that we see progressively smaller and smaller files.1748 * That's because we prefer deltas to be from the bigger file1749 * to the smaller -- deletes are potentially cheaper, but perhaps1750 * more importantly, the bigger file is likely the more recent1751 * one. The deepest deltas are therefore the oldest objects which are1752 * less susceptible to be accessed often.1753 */1754static inttype_size_sort(const void*_a,const void*_b)1755{1756const struct object_entry *a = *(struct object_entry **)_a;1757const struct object_entry *b = *(struct object_entry **)_b;1758enum object_type a_type =oe_type(a);1759enum object_type b_type =oe_type(b);17601761if(a_type > b_type)1762return-1;1763if(a_type < b_type)1764return1;1765if(a->hash > b->hash)1766return-1;1767if(a->hash < b->hash)1768return1;1769if(a->preferred_base > b->preferred_base)1770return-1;1771if(a->preferred_base < b->preferred_base)1772return1;1773if(a->size > b->size)1774return-1;1775if(a->size < b->size)1776return1;1777return a < b ? -1: (a > b);/* newest first */1778}17791780struct unpacked {1781struct object_entry *entry;1782void*data;1783struct delta_index *index;1784unsigned depth;1785};17861787static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1788unsigned long delta_size)1789{1790if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1791return0;17921793if(delta_size < cache_max_small_delta_size)1794return1;17951796/* cache delta, if objects are large enough compared to delta size */1797if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1798return1;17991800return0;1801}18021803#ifndef NO_PTHREADS18041805static pthread_mutex_t read_mutex;1806#define read_lock() pthread_mutex_lock(&read_mutex)1807#define read_unlock() pthread_mutex_unlock(&read_mutex)18081809static pthread_mutex_t cache_mutex;1810#define cache_lock() pthread_mutex_lock(&cache_mutex)1811#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18121813static pthread_mutex_t progress_mutex;1814#define progress_lock() pthread_mutex_lock(&progress_mutex)1815#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18161817#else18181819#define read_lock() (void)01820#define read_unlock() (void)01821#define cache_lock() (void)01822#define cache_unlock() (void)01823#define progress_lock() (void)01824#define progress_unlock() (void)018251826#endif18271828static inttry_delta(struct unpacked *trg,struct unpacked *src,1829unsigned max_depth,unsigned long*mem_usage)1830{1831struct object_entry *trg_entry = trg->entry;1832struct object_entry *src_entry = src->entry;1833unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1834unsigned ref_depth;1835enum object_type type;1836void*delta_buf;18371838/* Don't bother doing diffs between different types */1839if(oe_type(trg_entry) !=oe_type(src_entry))1840return-1;18411842/*1843 * We do not bother to try a delta that we discarded on an1844 * earlier try, but only when reusing delta data. Note that1845 * src_entry that is marked as the preferred_base should always1846 * be considered, as even if we produce a suboptimal delta against1847 * it, we will still save the transfer cost, as we already know1848 * the other side has it and we won't send src_entry at all.1849 */1850if(reuse_delta && trg_entry->in_pack &&1851 trg_entry->in_pack == src_entry->in_pack &&1852!src_entry->preferred_base &&1853 trg_entry->in_pack_type != OBJ_REF_DELTA &&1854 trg_entry->in_pack_type != OBJ_OFS_DELTA)1855return0;18561857/* Let's not bust the allowed depth. */1858if(src->depth >= max_depth)1859return0;18601861/* Now some size filtering heuristics. */1862 trg_size = trg_entry->size;1863if(!trg_entry->delta) {1864 max_size = trg_size/2-20;1865 ref_depth =1;1866}else{1867 max_size = trg_entry->delta_size;1868 ref_depth = trg->depth;1869}1870 max_size = (uint64_t)max_size * (max_depth - src->depth) /1871(max_depth - ref_depth +1);1872if(max_size ==0)1873return0;1874 src_size = src_entry->size;1875 sizediff = src_size < trg_size ? trg_size - src_size :0;1876if(sizediff >= max_size)1877return0;1878if(trg_size < src_size /32)1879return0;18801881/* Load data if not already done */1882if(!trg->data) {1883read_lock();1884 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1885read_unlock();1886if(!trg->data)1887die("object%scannot be read",1888oid_to_hex(&trg_entry->idx.oid));1889if(sz != trg_size)1890die("object%sinconsistent object length (%lu vs%lu)",1891oid_to_hex(&trg_entry->idx.oid), sz,1892 trg_size);1893*mem_usage += sz;1894}1895if(!src->data) {1896read_lock();1897 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);1898read_unlock();1899if(!src->data) {1900if(src_entry->preferred_base) {1901static int warned =0;1902if(!warned++)1903warning("object%scannot be read",1904oid_to_hex(&src_entry->idx.oid));1905/*1906 * Those objects are not included in the1907 * resulting pack. Be resilient and ignore1908 * them if they can't be read, in case the1909 * pack could be created nevertheless.1910 */1911return0;1912}1913die("object%scannot be read",1914oid_to_hex(&src_entry->idx.oid));1915}1916if(sz != src_size)1917die("object%sinconsistent object length (%lu vs%lu)",1918oid_to_hex(&src_entry->idx.oid), sz,1919 src_size);1920*mem_usage += sz;1921}1922if(!src->index) {1923 src->index =create_delta_index(src->data, src_size);1924if(!src->index) {1925static int warned =0;1926if(!warned++)1927warning("suboptimal pack - out of memory");1928return0;1929}1930*mem_usage +=sizeof_delta_index(src->index);1931}19321933 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);1934if(!delta_buf)1935return0;19361937if(trg_entry->delta) {1938/* Prefer only shallower same-sized deltas. */1939if(delta_size == trg_entry->delta_size &&1940 src->depth +1>= trg->depth) {1941free(delta_buf);1942return0;1943}1944}19451946/*1947 * Handle memory allocation outside of the cache1948 * accounting lock. Compiler will optimize the strangeness1949 * away when NO_PTHREADS is defined.1950 */1951free(trg_entry->delta_data);1952cache_lock();1953if(trg_entry->delta_data) {1954 delta_cache_size -= trg_entry->delta_size;1955 trg_entry->delta_data = NULL;1956}1957if(delta_cacheable(src_size, trg_size, delta_size)) {1958 delta_cache_size += delta_size;1959cache_unlock();1960 trg_entry->delta_data =xrealloc(delta_buf, delta_size);1961}else{1962cache_unlock();1963free(delta_buf);1964}19651966 trg_entry->delta = src_entry;1967 trg_entry->delta_size = delta_size;1968 trg->depth = src->depth +1;19691970return1;1971}19721973static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)1974{1975struct object_entry *child = me->delta_child;1976unsigned int m = n;1977while(child) {1978unsigned int c =check_delta_limit(child, n +1);1979if(m < c)1980 m = c;1981 child = child->delta_sibling;1982}1983return m;1984}19851986static unsigned longfree_unpacked(struct unpacked *n)1987{1988unsigned long freed_mem =sizeof_delta_index(n->index);1989free_delta_index(n->index);1990 n->index = NULL;1991if(n->data) {1992 freed_mem += n->entry->size;1993FREE_AND_NULL(n->data);1994}1995 n->entry = NULL;1996 n->depth =0;1997return freed_mem;1998}19992000static voidfind_deltas(struct object_entry **list,unsigned*list_size,2001int window,int depth,unsigned*processed)2002{2003uint32_t i, idx =0, count =0;2004struct unpacked *array;2005unsigned long mem_usage =0;20062007 array =xcalloc(window,sizeof(struct unpacked));20082009for(;;) {2010struct object_entry *entry;2011struct unpacked *n = array + idx;2012int j, max_depth, best_base = -1;20132014progress_lock();2015if(!*list_size) {2016progress_unlock();2017break;2018}2019 entry = *list++;2020(*list_size)--;2021if(!entry->preferred_base) {2022(*processed)++;2023display_progress(progress_state, *processed);2024}2025progress_unlock();20262027 mem_usage -=free_unpacked(n);2028 n->entry = entry;20292030while(window_memory_limit &&2031 mem_usage > window_memory_limit &&2032 count >1) {2033uint32_t tail = (idx + window - count) % window;2034 mem_usage -=free_unpacked(array + tail);2035 count--;2036}20372038/* We do not compute delta to *create* objects we are not2039 * going to pack.2040 */2041if(entry->preferred_base)2042goto next;20432044/*2045 * If the current object is at pack edge, take the depth the2046 * objects that depend on the current object into account2047 * otherwise they would become too deep.2048 */2049 max_depth = depth;2050if(entry->delta_child) {2051 max_depth -=check_delta_limit(entry,0);2052if(max_depth <=0)2053goto next;2054}20552056 j = window;2057while(--j >0) {2058int ret;2059uint32_t other_idx = idx + j;2060struct unpacked *m;2061if(other_idx >= window)2062 other_idx -= window;2063 m = array + other_idx;2064if(!m->entry)2065break;2066 ret =try_delta(n, m, max_depth, &mem_usage);2067if(ret <0)2068break;2069else if(ret >0)2070 best_base = other_idx;2071}20722073/*2074 * If we decided to cache the delta data, then it is best2075 * to compress it right away. First because we have to do2076 * it anyway, and doing it here while we're threaded will2077 * save a lot of time in the non threaded write phase,2078 * as well as allow for caching more deltas within2079 * the same cache size limit.2080 * ...2081 * But only if not writing to stdout, since in that case2082 * the network is most likely throttling writes anyway,2083 * and therefore it is best to go to the write phase ASAP2084 * instead, as we can afford spending more time compressing2085 * between writes at that moment.2086 */2087if(entry->delta_data && !pack_to_stdout) {2088 entry->z_delta_size =do_compress(&entry->delta_data,2089 entry->delta_size);2090cache_lock();2091 delta_cache_size -= entry->delta_size;2092 delta_cache_size += entry->z_delta_size;2093cache_unlock();2094}20952096/* if we made n a delta, and if n is already at max2097 * depth, leaving it in the window is pointless. we2098 * should evict it first.2099 */2100if(entry->delta && max_depth <= n->depth)2101continue;21022103/*2104 * Move the best delta base up in the window, after the2105 * currently deltified object, to keep it longer. It will2106 * be the first base object to be attempted next.2107 */2108if(entry->delta) {2109struct unpacked swap = array[best_base];2110int dist = (window + idx - best_base) % window;2111int dst = best_base;2112while(dist--) {2113int src = (dst +1) % window;2114 array[dst] = array[src];2115 dst = src;2116}2117 array[dst] = swap;2118}21192120 next:2121 idx++;2122if(count +1< window)2123 count++;2124if(idx >= window)2125 idx =0;2126}21272128for(i =0; i < window; ++i) {2129free_delta_index(array[i].index);2130free(array[i].data);2131}2132free(array);2133}21342135#ifndef NO_PTHREADS21362137static voidtry_to_free_from_threads(size_t size)2138{2139read_lock();2140release_pack_memory(size);2141read_unlock();2142}21432144static try_to_free_t old_try_to_free_routine;21452146/*2147 * The main thread waits on the condition that (at least) one of the workers2148 * has stopped working (which is indicated in the .working member of2149 * struct thread_params).2150 * When a work thread has completed its work, it sets .working to 0 and2151 * signals the main thread and waits on the condition that .data_ready2152 * becomes 1.2153 */21542155struct thread_params {2156 pthread_t thread;2157struct object_entry **list;2158unsigned list_size;2159unsigned remaining;2160int window;2161int depth;2162int working;2163int data_ready;2164 pthread_mutex_t mutex;2165 pthread_cond_t cond;2166unsigned*processed;2167};21682169static pthread_cond_t progress_cond;21702171/*2172 * Mutex and conditional variable can't be statically-initialized on Windows.2173 */2174static voidinit_threaded_search(void)2175{2176init_recursive_mutex(&read_mutex);2177pthread_mutex_init(&cache_mutex, NULL);2178pthread_mutex_init(&progress_mutex, NULL);2179pthread_cond_init(&progress_cond, NULL);2180 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2181}21822183static voidcleanup_threaded_search(void)2184{2185set_try_to_free_routine(old_try_to_free_routine);2186pthread_cond_destroy(&progress_cond);2187pthread_mutex_destroy(&read_mutex);2188pthread_mutex_destroy(&cache_mutex);2189pthread_mutex_destroy(&progress_mutex);2190}21912192static void*threaded_find_deltas(void*arg)2193{2194struct thread_params *me = arg;21952196progress_lock();2197while(me->remaining) {2198progress_unlock();21992200find_deltas(me->list, &me->remaining,2201 me->window, me->depth, me->processed);22022203progress_lock();2204 me->working =0;2205pthread_cond_signal(&progress_cond);2206progress_unlock();22072208/*2209 * We must not set ->data_ready before we wait on the2210 * condition because the main thread may have set it to 12211 * before we get here. In order to be sure that new2212 * work is available if we see 1 in ->data_ready, it2213 * was initialized to 0 before this thread was spawned2214 * and we reset it to 0 right away.2215 */2216pthread_mutex_lock(&me->mutex);2217while(!me->data_ready)2218pthread_cond_wait(&me->cond, &me->mutex);2219 me->data_ready =0;2220pthread_mutex_unlock(&me->mutex);22212222progress_lock();2223}2224progress_unlock();2225/* leave ->working 1 so that this doesn't get more work assigned */2226return NULL;2227}22282229static voidll_find_deltas(struct object_entry **list,unsigned list_size,2230int window,int depth,unsigned*processed)2231{2232struct thread_params *p;2233int i, ret, active_threads =0;22342235init_threaded_search();22362237if(delta_search_threads <=1) {2238find_deltas(list, &list_size, window, depth, processed);2239cleanup_threaded_search();2240return;2241}2242if(progress > pack_to_stdout)2243fprintf(stderr,"Delta compression using up to%dthreads.\n",2244 delta_search_threads);2245 p =xcalloc(delta_search_threads,sizeof(*p));22462247/* Partition the work amongst work threads. */2248for(i =0; i < delta_search_threads; i++) {2249unsigned sub_size = list_size / (delta_search_threads - i);22502251/* don't use too small segments or no deltas will be found */2252if(sub_size <2*window && i+1< delta_search_threads)2253 sub_size =0;22542255 p[i].window = window;2256 p[i].depth = depth;2257 p[i].processed = processed;2258 p[i].working =1;2259 p[i].data_ready =0;22602261/* try to split chunks on "path" boundaries */2262while(sub_size && sub_size < list_size &&2263 list[sub_size]->hash &&2264 list[sub_size]->hash == list[sub_size-1]->hash)2265 sub_size++;22662267 p[i].list = list;2268 p[i].list_size = sub_size;2269 p[i].remaining = sub_size;22702271 list += sub_size;2272 list_size -= sub_size;2273}22742275/* Start work threads. */2276for(i =0; i < delta_search_threads; i++) {2277if(!p[i].list_size)2278continue;2279pthread_mutex_init(&p[i].mutex, NULL);2280pthread_cond_init(&p[i].cond, NULL);2281 ret =pthread_create(&p[i].thread, NULL,2282 threaded_find_deltas, &p[i]);2283if(ret)2284die("unable to create thread:%s",strerror(ret));2285 active_threads++;2286}22872288/*2289 * Now let's wait for work completion. Each time a thread is done2290 * with its work, we steal half of the remaining work from the2291 * thread with the largest number of unprocessed objects and give2292 * it to that newly idle thread. This ensure good load balancing2293 * until the remaining object list segments are simply too short2294 * to be worth splitting anymore.2295 */2296while(active_threads) {2297struct thread_params *target = NULL;2298struct thread_params *victim = NULL;2299unsigned sub_size =0;23002301progress_lock();2302for(;;) {2303for(i =0; !target && i < delta_search_threads; i++)2304if(!p[i].working)2305 target = &p[i];2306if(target)2307break;2308pthread_cond_wait(&progress_cond, &progress_mutex);2309}23102311for(i =0; i < delta_search_threads; i++)2312if(p[i].remaining >2*window &&2313(!victim || victim->remaining < p[i].remaining))2314 victim = &p[i];2315if(victim) {2316 sub_size = victim->remaining /2;2317 list = victim->list + victim->list_size - sub_size;2318while(sub_size && list[0]->hash &&2319 list[0]->hash == list[-1]->hash) {2320 list++;2321 sub_size--;2322}2323if(!sub_size) {2324/*2325 * It is possible for some "paths" to have2326 * so many objects that no hash boundary2327 * might be found. Let's just steal the2328 * exact half in that case.2329 */2330 sub_size = victim->remaining /2;2331 list -= sub_size;2332}2333 target->list = list;2334 victim->list_size -= sub_size;2335 victim->remaining -= sub_size;2336}2337 target->list_size = sub_size;2338 target->remaining = sub_size;2339 target->working =1;2340progress_unlock();23412342pthread_mutex_lock(&target->mutex);2343 target->data_ready =1;2344pthread_cond_signal(&target->cond);2345pthread_mutex_unlock(&target->mutex);23462347if(!sub_size) {2348pthread_join(target->thread, NULL);2349pthread_cond_destroy(&target->cond);2350pthread_mutex_destroy(&target->mutex);2351 active_threads--;2352}2353}2354cleanup_threaded_search();2355free(p);2356}23572358#else2359#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2360#endif23612362static voidadd_tag_chain(const struct object_id *oid)2363{2364struct tag *tag;23652366/*2367 * We catch duplicates already in add_object_entry(), but we'd2368 * prefer to do this extra check to avoid having to parse the2369 * tag at all if we already know that it's being packed (e.g., if2370 * it was included via bitmaps, we would not have parsed it2371 * previously).2372 */2373if(packlist_find(&to_pack, oid->hash, NULL))2374return;23752376 tag =lookup_tag(oid);2377while(1) {2378if(!tag ||parse_tag(tag) || !tag->tagged)2379die("unable to pack objects reachable from tag%s",2380oid_to_hex(oid));23812382add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);23832384if(tag->tagged->type != OBJ_TAG)2385return;23862387 tag = (struct tag *)tag->tagged;2388}2389}23902391static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2392{2393struct object_id peeled;23942395if(starts_with(path,"refs/tags/") &&/* is a tag? */2396!peel_ref(path, &peeled) &&/* peelable? */2397packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2398add_tag_chain(oid);2399return0;2400}24012402static voidprepare_pack(int window,int depth)2403{2404struct object_entry **delta_list;2405uint32_t i, nr_deltas;2406unsigned n;24072408get_object_details();24092410/*2411 * If we're locally repacking then we need to be doubly careful2412 * from now on in order to make sure no stealth corruption gets2413 * propagated to the new pack. Clients receiving streamed packs2414 * should validate everything they get anyway so no need to incur2415 * the additional cost here in that case.2416 */2417if(!pack_to_stdout)2418 do_check_packed_object_crc =1;24192420if(!to_pack.nr_objects || !window || !depth)2421return;24222423ALLOC_ARRAY(delta_list, to_pack.nr_objects);2424 nr_deltas = n =0;24252426for(i =0; i < to_pack.nr_objects; i++) {2427struct object_entry *entry = to_pack.objects + i;24282429if(entry->delta)2430/* This happens if we decided to reuse existing2431 * delta from a pack. "reuse_delta &&" is implied.2432 */2433continue;24342435if(entry->size <50)2436continue;24372438if(entry->no_try_delta)2439continue;24402441if(!entry->preferred_base) {2442 nr_deltas++;2443if(oe_type(entry) <0)2444die("unable to get type of object%s",2445oid_to_hex(&entry->idx.oid));2446}else{2447if(oe_type(entry) <0) {2448/*2449 * This object is not found, but we2450 * don't have to include it anyway.2451 */2452continue;2453}2454}24552456 delta_list[n++] = entry;2457}24582459if(nr_deltas && n >1) {2460unsigned nr_done =0;2461if(progress)2462 progress_state =start_progress(_("Compressing objects"),2463 nr_deltas);2464QSORT(delta_list, n, type_size_sort);2465ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2466stop_progress(&progress_state);2467if(nr_done != nr_deltas)2468die("inconsistency with delta count");2469}2470free(delta_list);2471}24722473static intgit_pack_config(const char*k,const char*v,void*cb)2474{2475if(!strcmp(k,"pack.window")) {2476 window =git_config_int(k, v);2477return0;2478}2479if(!strcmp(k,"pack.windowmemory")) {2480 window_memory_limit =git_config_ulong(k, v);2481return0;2482}2483if(!strcmp(k,"pack.depth")) {2484 depth =git_config_int(k, v);2485return0;2486}2487if(!strcmp(k,"pack.deltacachesize")) {2488 max_delta_cache_size =git_config_int(k, v);2489return0;2490}2491if(!strcmp(k,"pack.deltacachelimit")) {2492 cache_max_small_delta_size =git_config_int(k, v);2493return0;2494}2495if(!strcmp(k,"pack.writebitmaphashcache")) {2496if(git_config_bool(k, v))2497 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2498else2499 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2500}2501if(!strcmp(k,"pack.usebitmaps")) {2502 use_bitmap_index_default =git_config_bool(k, v);2503return0;2504}2505if(!strcmp(k,"pack.threads")) {2506 delta_search_threads =git_config_int(k, v);2507if(delta_search_threads <0)2508die("invalid number of threads specified (%d)",2509 delta_search_threads);2510#ifdef NO_PTHREADS2511if(delta_search_threads !=1) {2512warning("no threads support, ignoring%s", k);2513 delta_search_threads =0;2514}2515#endif2516return0;2517}2518if(!strcmp(k,"pack.indexversion")) {2519 pack_idx_opts.version =git_config_int(k, v);2520if(pack_idx_opts.version >2)2521die("bad pack.indexversion=%"PRIu32,2522 pack_idx_opts.version);2523return0;2524}2525returngit_default_config(k, v, cb);2526}25272528static voidread_object_list_from_stdin(void)2529{2530char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2531struct object_id oid;2532const char*p;25332534for(;;) {2535if(!fgets(line,sizeof(line), stdin)) {2536if(feof(stdin))2537break;2538if(!ferror(stdin))2539die("fgets returned NULL, not EOF, not error!");2540if(errno != EINTR)2541die_errno("fgets");2542clearerr(stdin);2543continue;2544}2545if(line[0] =='-') {2546if(get_oid_hex(line+1, &oid))2547die("expected edge object ID, got garbage:\n%s",2548 line);2549add_preferred_base(&oid);2550continue;2551}2552if(parse_oid_hex(line, &oid, &p))2553die("expected object ID, got garbage:\n%s", line);25542555add_preferred_base_object(p +1);2556add_object_entry(&oid, OBJ_NONE, p +1,0);2557}2558}25592560/* Remember to update object flag allocation in object.h */2561#define OBJECT_ADDED (1u<<20)25622563static voidshow_commit(struct commit *commit,void*data)2564{2565add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2566 commit->object.flags |= OBJECT_ADDED;25672568if(write_bitmap_index)2569index_commit_for_bitmap(commit);2570}25712572static voidshow_object(struct object *obj,const char*name,void*data)2573{2574add_preferred_base_object(name);2575add_object_entry(&obj->oid, obj->type, name,0);2576 obj->flags |= OBJECT_ADDED;2577}25782579static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2580{2581assert(arg_missing_action == MA_ALLOW_ANY);25822583/*2584 * Quietly ignore ALL missing objects. This avoids problems with2585 * staging them now and getting an odd error later.2586 */2587if(!has_object_file(&obj->oid))2588return;25892590show_object(obj, name, data);2591}25922593static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2594{2595assert(arg_missing_action == MA_ALLOW_PROMISOR);25962597/*2598 * Quietly ignore EXPECTED missing objects. This avoids problems with2599 * staging them now and getting an odd error later.2600 */2601if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2602return;26032604show_object(obj, name, data);2605}26062607static intoption_parse_missing_action(const struct option *opt,2608const char*arg,int unset)2609{2610assert(arg);2611assert(!unset);26122613if(!strcmp(arg,"error")) {2614 arg_missing_action = MA_ERROR;2615 fn_show_object = show_object;2616return0;2617}26182619if(!strcmp(arg,"allow-any")) {2620 arg_missing_action = MA_ALLOW_ANY;2621 fetch_if_missing =0;2622 fn_show_object = show_object__ma_allow_any;2623return0;2624}26252626if(!strcmp(arg,"allow-promisor")) {2627 arg_missing_action = MA_ALLOW_PROMISOR;2628 fetch_if_missing =0;2629 fn_show_object = show_object__ma_allow_promisor;2630return0;2631}26322633die(_("invalid value for --missing"));2634return0;2635}26362637static voidshow_edge(struct commit *commit)2638{2639add_preferred_base(&commit->object.oid);2640}26412642struct in_pack_object {2643 off_t offset;2644struct object *object;2645};26462647struct in_pack {2648unsigned int alloc;2649unsigned int nr;2650struct in_pack_object *array;2651};26522653static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2654{2655 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2656 in_pack->array[in_pack->nr].object = object;2657 in_pack->nr++;2658}26592660/*2661 * Compare the objects in the offset order, in order to emulate the2662 * "git rev-list --objects" output that produced the pack originally.2663 */2664static intofscmp(const void*a_,const void*b_)2665{2666struct in_pack_object *a = (struct in_pack_object *)a_;2667struct in_pack_object *b = (struct in_pack_object *)b_;26682669if(a->offset < b->offset)2670return-1;2671else if(a->offset > b->offset)2672return1;2673else2674returnoidcmp(&a->object->oid, &b->object->oid);2675}26762677static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2678{2679struct packed_git *p;2680struct in_pack in_pack;2681uint32_t i;26822683memset(&in_pack,0,sizeof(in_pack));26842685for(p =get_packed_git(the_repository); p; p = p->next) {2686struct object_id oid;2687struct object *o;26882689if(!p->pack_local || p->pack_keep)2690continue;2691if(open_pack_index(p))2692die("cannot open pack index");26932694ALLOC_GROW(in_pack.array,2695 in_pack.nr + p->num_objects,2696 in_pack.alloc);26972698for(i =0; i < p->num_objects; i++) {2699nth_packed_object_oid(&oid, p, i);2700 o =lookup_unknown_object(oid.hash);2701if(!(o->flags & OBJECT_ADDED))2702mark_in_pack_object(o, p, &in_pack);2703 o->flags |= OBJECT_ADDED;2704}2705}27062707if(in_pack.nr) {2708QSORT(in_pack.array, in_pack.nr, ofscmp);2709for(i =0; i < in_pack.nr; i++) {2710struct object *o = in_pack.array[i].object;2711add_object_entry(&o->oid, o->type,"",0);2712}2713}2714free(in_pack.array);2715}27162717static intadd_loose_object(const struct object_id *oid,const char*path,2718void*data)2719{2720enum object_type type =oid_object_info(oid, NULL);27212722if(type <0) {2723warning("loose object at%scould not be examined", path);2724return0;2725}27262727add_object_entry(oid, type,"",0);2728return0;2729}27302731/*2732 * We actually don't even have to worry about reachability here.2733 * add_object_entry will weed out duplicates, so we just add every2734 * loose object we find.2735 */2736static voidadd_unreachable_loose_objects(void)2737{2738for_each_loose_file_in_objdir(get_object_directory(),2739 add_loose_object,2740 NULL, NULL, NULL);2741}27422743static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2744{2745static struct packed_git *last_found = (void*)1;2746struct packed_git *p;27472748 p = (last_found != (void*)1) ? last_found :2749get_packed_git(the_repository);27502751while(p) {2752if((!p->pack_local || p->pack_keep) &&2753find_pack_entry_one(oid->hash, p)) {2754 last_found = p;2755return1;2756}2757if(p == last_found)2758 p =get_packed_git(the_repository);2759else2760 p = p->next;2761if(p == last_found)2762 p = p->next;2763}2764return0;2765}27662767/*2768 * Store a list of sha1s that are should not be discarded2769 * because they are either written too recently, or are2770 * reachable from another object that was.2771 *2772 * This is filled by get_object_list.2773 */2774static struct oid_array recent_objects;27752776static intloosened_object_can_be_discarded(const struct object_id *oid,2777 timestamp_t mtime)2778{2779if(!unpack_unreachable_expiration)2780return0;2781if(mtime > unpack_unreachable_expiration)2782return0;2783if(oid_array_lookup(&recent_objects, oid) >=0)2784return0;2785return1;2786}27872788static voidloosen_unused_packed_objects(struct rev_info *revs)2789{2790struct packed_git *p;2791uint32_t i;2792struct object_id oid;27932794for(p =get_packed_git(the_repository); p; p = p->next) {2795if(!p->pack_local || p->pack_keep)2796continue;27972798if(open_pack_index(p))2799die("cannot open pack index");28002801for(i =0; i < p->num_objects; i++) {2802nth_packed_object_oid(&oid, p, i);2803if(!packlist_find(&to_pack, oid.hash, NULL) &&2804!has_sha1_pack_kept_or_nonlocal(&oid) &&2805!loosened_object_can_be_discarded(&oid, p->mtime))2806if(force_object_loose(&oid, p->mtime))2807die("unable to force loose object");2808}2809}2810}28112812/*2813 * This tracks any options which pack-reuse code expects to be on, or which a2814 * reader of the pack might not understand, and which would therefore prevent2815 * blind reuse of what we have on disk.2816 */2817static intpack_options_allow_reuse(void)2818{2819return pack_to_stdout &&2820 allow_ofs_delta &&2821!ignore_packed_keep &&2822(!local || !have_non_local_packs) &&2823!incremental;2824}28252826static intget_object_list_from_bitmap(struct rev_info *revs)2827{2828if(prepare_bitmap_walk(revs) <0)2829return-1;28302831if(pack_options_allow_reuse() &&2832!reuse_partial_packfile_from_bitmap(2833&reuse_packfile,2834&reuse_packfile_objects,2835&reuse_packfile_offset)) {2836assert(reuse_packfile_objects);2837 nr_result += reuse_packfile_objects;2838display_progress(progress_state, nr_result);2839}28402841traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2842return0;2843}28442845static voidrecord_recent_object(struct object *obj,2846const char*name,2847void*data)2848{2849oid_array_append(&recent_objects, &obj->oid);2850}28512852static voidrecord_recent_commit(struct commit *commit,void*data)2853{2854oid_array_append(&recent_objects, &commit->object.oid);2855}28562857static voidget_object_list(int ac,const char**av)2858{2859struct rev_info revs;2860char line[1000];2861int flags =0;28622863init_revisions(&revs, NULL);2864 save_commit_buffer =0;2865setup_revisions(ac, av, &revs, NULL);28662867/* make sure shallows are read */2868is_repository_shallow();28692870while(fgets(line,sizeof(line), stdin) != NULL) {2871int len =strlen(line);2872if(len && line[len -1] =='\n')2873 line[--len] =0;2874if(!len)2875break;2876if(*line =='-') {2877if(!strcmp(line,"--not")) {2878 flags ^= UNINTERESTING;2879 write_bitmap_index =0;2880continue;2881}2882if(starts_with(line,"--shallow ")) {2883struct object_id oid;2884if(get_oid_hex(line +10, &oid))2885die("not an SHA-1 '%s'", line +10);2886register_shallow(&oid);2887 use_bitmap_index =0;2888continue;2889}2890die("not a rev '%s'", line);2891}2892if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2893die("bad revision '%s'", line);2894}28952896if(use_bitmap_index && !get_object_list_from_bitmap(&revs))2897return;28982899if(prepare_revision_walk(&revs))2900die("revision walk setup failed");2901mark_edges_uninteresting(&revs, show_edge);29022903if(!fn_show_object)2904 fn_show_object = show_object;2905traverse_commit_list_filtered(&filter_options, &revs,2906 show_commit, fn_show_object, NULL,2907 NULL);29082909if(unpack_unreachable_expiration) {2910 revs.ignore_missing_links =1;2911if(add_unseen_recent_objects_to_traversal(&revs,2912 unpack_unreachable_expiration))2913die("unable to add recent objects");2914if(prepare_revision_walk(&revs))2915die("revision walk setup failed");2916traverse_commit_list(&revs, record_recent_commit,2917 record_recent_object, NULL);2918}29192920if(keep_unreachable)2921add_objects_in_unpacked_packs(&revs);2922if(pack_loose_unreachable)2923add_unreachable_loose_objects();2924if(unpack_unreachable)2925loosen_unused_packed_objects(&revs);29262927oid_array_clear(&recent_objects);2928}29292930static intoption_parse_index_version(const struct option *opt,2931const char*arg,int unset)2932{2933char*c;2934const char*val = arg;2935 pack_idx_opts.version =strtoul(val, &c,10);2936if(pack_idx_opts.version >2)2937die(_("unsupported index version%s"), val);2938if(*c ==','&& c[1])2939 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);2940if(*c || pack_idx_opts.off32_limit &0x80000000)2941die(_("bad index version '%s'"), val);2942return0;2943}29442945static intoption_parse_unpack_unreachable(const struct option *opt,2946const char*arg,int unset)2947{2948if(unset) {2949 unpack_unreachable =0;2950 unpack_unreachable_expiration =0;2951}2952else{2953 unpack_unreachable =1;2954if(arg)2955 unpack_unreachable_expiration =approxidate(arg);2956}2957return0;2958}29592960intcmd_pack_objects(int argc,const char**argv,const char*prefix)2961{2962int use_internal_rev_list =0;2963int thin =0;2964int shallow =0;2965int all_progress_implied =0;2966struct argv_array rp = ARGV_ARRAY_INIT;2967int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;2968int rev_list_index =0;2969struct option pack_objects_options[] = {2970OPT_SET_INT('q',"quiet", &progress,2971N_("do not show progress meter"),0),2972OPT_SET_INT(0,"progress", &progress,2973N_("show progress meter"),1),2974OPT_SET_INT(0,"all-progress", &progress,2975N_("show progress meter during object writing phase"),2),2976OPT_BOOL(0,"all-progress-implied",2977&all_progress_implied,2978N_("similar to --all-progress when progress meter is shown")),2979{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),2980N_("write the pack index file in the specified idx format version"),29810, option_parse_index_version },2982OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,2983N_("maximum size of each output pack file")),2984OPT_BOOL(0,"local", &local,2985N_("ignore borrowed objects from alternate object store")),2986OPT_BOOL(0,"incremental", &incremental,2987N_("ignore packed objects")),2988OPT_INTEGER(0,"window", &window,2989N_("limit pack window by objects")),2990OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,2991N_("limit pack window by memory in addition to object limit")),2992OPT_INTEGER(0,"depth", &depth,2993N_("maximum length of delta chain allowed in the resulting pack")),2994OPT_BOOL(0,"reuse-delta", &reuse_delta,2995N_("reuse existing deltas")),2996OPT_BOOL(0,"reuse-object", &reuse_object,2997N_("reuse existing objects")),2998OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,2999N_("use OFS_DELTA objects")),3000OPT_INTEGER(0,"threads", &delta_search_threads,3001N_("use threads when searching for best delta matches")),3002OPT_BOOL(0,"non-empty", &non_empty,3003N_("do not create an empty pack output")),3004OPT_BOOL(0,"revs", &use_internal_rev_list,3005N_("read revision arguments from standard input")),3006{ OPTION_SET_INT,0,"unpacked", &rev_list_unpacked, NULL,3007N_("limit the objects to those that are not yet packed"),3008 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3009{ OPTION_SET_INT,0,"all", &rev_list_all, NULL,3010N_("include objects reachable from any reference"),3011 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3012{ OPTION_SET_INT,0,"reflog", &rev_list_reflog, NULL,3013N_("include objects referred by reflog entries"),3014 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3015{ OPTION_SET_INT,0,"indexed-objects", &rev_list_index, NULL,3016N_("include objects referred to by the index"),3017 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3018OPT_BOOL(0,"stdout", &pack_to_stdout,3019N_("output pack to stdout")),3020OPT_BOOL(0,"include-tag", &include_tag,3021N_("include tag objects that refer to objects to be packed")),3022OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3023N_("keep unreachable objects")),3024OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3025N_("pack loose unreachable objects")),3026{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3027N_("unpack unreachable objects newer than <time>"),3028 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3029OPT_BOOL(0,"thin", &thin,3030N_("create thin packs")),3031OPT_BOOL(0,"shallow", &shallow,3032N_("create packs suitable for shallow fetches")),3033OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep,3034N_("ignore packs that have companion .keep file")),3035OPT_INTEGER(0,"compression", &pack_compression_level,3036N_("pack compression level")),3037OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3038N_("do not hide commits by grafts"),0),3039OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3040N_("use a bitmap index if available to speed up counting objects")),3041OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3042N_("write a bitmap index together with the pack index")),3043OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3044{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3045N_("handling for missing objects"), PARSE_OPT_NONEG,3046 option_parse_missing_action },3047OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3048N_("do not pack objects in promisor packfiles")),3049OPT_END(),3050};30513052if(DFS_NUM_STATES > (1<< OE_DFS_STATE_BITS))3053BUG("too many dfs states, increase OE_DFS_STATE_BITS");30543055 check_replace_refs =0;30563057reset_pack_idx_option(&pack_idx_opts);3058git_config(git_pack_config, NULL);30593060 progress =isatty(2);3061 argc =parse_options(argc, argv, prefix, pack_objects_options,3062 pack_usage,0);30633064if(argc) {3065 base_name = argv[0];3066 argc--;3067}3068if(pack_to_stdout != !base_name || argc)3069usage_with_options(pack_usage, pack_objects_options);30703071if(depth >= (1<< OE_DEPTH_BITS)) {3072warning(_("delta chain depth%dis too deep, forcing%d"),3073 depth, (1<< OE_DEPTH_BITS) -1);3074 depth = (1<< OE_DEPTH_BITS) -1;3075}30763077argv_array_push(&rp,"pack-objects");3078if(thin) {3079 use_internal_rev_list =1;3080argv_array_push(&rp, shallow3081?"--objects-edge-aggressive"3082:"--objects-edge");3083}else3084argv_array_push(&rp,"--objects");30853086if(rev_list_all) {3087 use_internal_rev_list =1;3088argv_array_push(&rp,"--all");3089}3090if(rev_list_reflog) {3091 use_internal_rev_list =1;3092argv_array_push(&rp,"--reflog");3093}3094if(rev_list_index) {3095 use_internal_rev_list =1;3096argv_array_push(&rp,"--indexed-objects");3097}3098if(rev_list_unpacked) {3099 use_internal_rev_list =1;3100argv_array_push(&rp,"--unpacked");3101}31023103if(exclude_promisor_objects) {3104 use_internal_rev_list =1;3105 fetch_if_missing =0;3106argv_array_push(&rp,"--exclude-promisor-objects");3107}31083109if(!reuse_object)3110 reuse_delta =0;3111if(pack_compression_level == -1)3112 pack_compression_level = Z_DEFAULT_COMPRESSION;3113else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3114die("bad pack compression level%d", pack_compression_level);31153116if(!delta_search_threads)/* --threads=0 means autodetect */3117 delta_search_threads =online_cpus();31183119#ifdef NO_PTHREADS3120if(delta_search_threads !=1)3121warning("no threads support, ignoring --threads");3122#endif3123if(!pack_to_stdout && !pack_size_limit)3124 pack_size_limit = pack_size_limit_cfg;3125if(pack_to_stdout && pack_size_limit)3126die("--max-pack-size cannot be used to build a pack for transfer.");3127if(pack_size_limit && pack_size_limit <1024*1024) {3128warning("minimum pack size limit is 1 MiB");3129 pack_size_limit =1024*1024;3130}31313132if(!pack_to_stdout && thin)3133die("--thin cannot be used to build an indexable pack.");31343135if(keep_unreachable && unpack_unreachable)3136die("--keep-unreachable and --unpack-unreachable are incompatible.");3137if(!rev_list_all || !rev_list_reflog || !rev_list_index)3138 unpack_unreachable_expiration =0;31393140if(filter_options.choice) {3141if(!pack_to_stdout)3142die("cannot use --filter without --stdout.");3143 use_bitmap_index =0;3144}31453146/*3147 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3148 *3149 * - to produce good pack (with bitmap index not-yet-packed objects are3150 * packed in suboptimal order).3151 *3152 * - to use more robust pack-generation codepath (avoiding possible3153 * bugs in bitmap code and possible bitmap index corruption).3154 */3155if(!pack_to_stdout)3156 use_bitmap_index_default =0;31573158if(use_bitmap_index <0)3159 use_bitmap_index = use_bitmap_index_default;31603161/* "hard" reasons not to use bitmaps; these just won't work at all */3162if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3163 use_bitmap_index =0;31643165if(pack_to_stdout || !rev_list_all)3166 write_bitmap_index =0;31673168if(progress && all_progress_implied)3169 progress =2;31703171if(ignore_packed_keep) {3172struct packed_git *p;3173for(p =get_packed_git(the_repository); p; p = p->next)3174if(p->pack_local && p->pack_keep)3175break;3176if(!p)/* no keep-able packs found */3177 ignore_packed_keep =0;3178}3179if(local) {3180/*3181 * unlike ignore_packed_keep above, we do not want to3182 * unset "local" based on looking at packs, as it3183 * also covers non-local objects3184 */3185struct packed_git *p;3186for(p =get_packed_git(the_repository); p; p = p->next) {3187if(!p->pack_local) {3188 have_non_local_packs =1;3189break;3190}3191}3192}31933194if(progress)3195 progress_state =start_progress(_("Counting objects"),0);3196if(!use_internal_rev_list)3197read_object_list_from_stdin();3198else{3199get_object_list(rp.argc, rp.argv);3200argv_array_clear(&rp);3201}3202cleanup_preferred_base();3203if(include_tag && nr_result)3204for_each_ref(add_ref_tag, NULL);3205stop_progress(&progress_state);32063207if(non_empty && !nr_result)3208return0;3209if(nr_result)3210prepare_pack(window, depth);3211write_pack_file();3212if(progress)3213fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3214" reused %"PRIu32" (delta %"PRIu32")\n",3215 written, written_delta, reused, reused_delta);3216return0;3217}