1#include "builtin.h" 2#include "cache.h" 3#include "repository.h" 4#include "config.h" 5#include "attr.h" 6#include "object.h" 7#include "blob.h" 8#include "commit.h" 9#include "tag.h" 10#include "tree.h" 11#include "delta.h" 12#include "pack.h" 13#include "pack-revindex.h" 14#include "csum-file.h" 15#include "tree-walk.h" 16#include "diff.h" 17#include "revision.h" 18#include "list-objects.h" 19#include "list-objects-filter.h" 20#include "list-objects-filter-options.h" 21#include "pack-objects.h" 22#include "progress.h" 23#include "refs.h" 24#include "streaming.h" 25#include "thread-utils.h" 26#include "pack-bitmap.h" 27#include "reachable.h" 28#include "sha1-array.h" 29#include "argv-array.h" 30#include "list.h" 31#include "packfile.h" 32#include "object-store.h" 33 34#define IN_PACK(obj) oe_in_pack(&to_pack, obj) 35#define SIZE(obj) oe_size(&to_pack, obj) 36#define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size) 37#define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj) 38#define DELTA(obj) oe_delta(&to_pack, obj) 39#define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj) 40#define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj) 41#define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val) 42#define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val) 43#define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val) 44#define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val) 45 46static const char *pack_usage[] = { 47 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 48 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 49 NULL 50}; 51 52/* 53 * Objects we are going to pack are collected in the `to_pack` structure. 54 * It contains an array (dynamically expanded) of the object data, and a map 55 * that can resolve SHA1s to their position in the array. 56 */ 57static struct packing_data to_pack; 58 59static struct pack_idx_entry **written_list; 60static uint32_t nr_result, nr_written; 61 62static int non_empty; 63static int reuse_delta = 1, reuse_object = 1; 64static int keep_unreachable, unpack_unreachable, include_tag; 65static timestamp_t unpack_unreachable_expiration; 66static int pack_loose_unreachable; 67static int local; 68static int have_non_local_packs; 69static int incremental; 70static int ignore_packed_keep; 71static int allow_ofs_delta; 72static struct pack_idx_option pack_idx_opts; 73static const char *base_name; 74static int progress = 1; 75static int window = 10; 76static unsigned long pack_size_limit; 77static int depth = 50; 78static int delta_search_threads; 79static int pack_to_stdout; 80static int num_preferred_base; 81static struct progress *progress_state; 82 83static struct packed_git *reuse_packfile; 84static uint32_t reuse_packfile_objects; 85static off_t reuse_packfile_offset; 86 87static int use_bitmap_index_default = 1; 88static int use_bitmap_index = -1; 89static int write_bitmap_index; 90static uint16_t write_bitmap_options; 91 92static int exclude_promisor_objects; 93 94static unsigned long delta_cache_size = 0; 95static unsigned long max_delta_cache_size = 256 * 1024 * 1024; 96static unsigned long cache_max_small_delta_size = 1000; 97 98static unsigned long window_memory_limit = 0; 99 100static struct list_objects_filter_options filter_options; 101 102enum missing_action { 103 MA_ERROR = 0, /* fail if any missing objects are encountered */ 104 MA_ALLOW_ANY, /* silently allow ALL missing objects */ 105 MA_ALLOW_PROMISOR, /* silently allow all missing PROMISOR objects */ 106}; 107static enum missing_action arg_missing_action; 108static show_object_fn fn_show_object; 109 110/* 111 * stats 112 */ 113static uint32_t written, written_delta; 114static uint32_t reused, reused_delta; 115 116/* 117 * Indexed commits 118 */ 119static struct commit **indexed_commits; 120static unsigned int indexed_commits_nr; 121static unsigned int indexed_commits_alloc; 122 123static void index_commit_for_bitmap(struct commit *commit) 124{ 125 if (indexed_commits_nr >= indexed_commits_alloc) { 126 indexed_commits_alloc = (indexed_commits_alloc + 32) * 2; 127 REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 128 } 129 130 indexed_commits[indexed_commits_nr++] = commit; 131} 132 133static void *get_delta(struct object_entry *entry) 134{ 135 unsigned long size, base_size, delta_size; 136 void *buf, *base_buf, *delta_buf; 137 enum object_type type; 138 139 buf = read_object_file(&entry->idx.oid, &type, &size); 140 if (!buf) 141 die("unable to read %s", oid_to_hex(&entry->idx.oid)); 142 base_buf = read_object_file(&DELTA(entry)->idx.oid, &type, 143 &base_size); 144 if (!base_buf) 145 die("unable to read %s", 146 oid_to_hex(&DELTA(entry)->idx.oid)); 147 delta_buf = diff_delta(base_buf, base_size, 148 buf, size, &delta_size, 0); 149 if (!delta_buf || delta_size != DELTA_SIZE(entry)) 150 die("delta size changed"); 151 free(buf); 152 free(base_buf); 153 return delta_buf; 154} 155 156static unsigned long do_compress(void **pptr, unsigned long size) 157{ 158 git_zstream stream; 159 void *in, *out; 160 unsigned long maxsize; 161 162 git_deflate_init(&stream, pack_compression_level); 163 maxsize = git_deflate_bound(&stream, size); 164 165 in = *pptr; 166 out = xmalloc(maxsize); 167 *pptr = out; 168 169 stream.next_in = in; 170 stream.avail_in = size; 171 stream.next_out = out; 172 stream.avail_out = maxsize; 173 while (git_deflate(&stream, Z_FINISH) == Z_OK) 174 ; /* nothing */ 175 git_deflate_end(&stream); 176 177 free(in); 178 return stream.total_out; 179} 180 181static unsigned long write_large_blob_data(struct git_istream *st, struct hashfile *f, 182 const struct object_id *oid) 183{ 184 git_zstream stream; 185 unsigned char ibuf[1024 * 16]; 186 unsigned char obuf[1024 * 16]; 187 unsigned long olen = 0; 188 189 git_deflate_init(&stream, pack_compression_level); 190 191 for (;;) { 192 ssize_t readlen; 193 int zret = Z_OK; 194 readlen = read_istream(st, ibuf, sizeof(ibuf)); 195 if (readlen == -1) 196 die(_("unable to read %s"), oid_to_hex(oid)); 197 198 stream.next_in = ibuf; 199 stream.avail_in = readlen; 200 while ((stream.avail_in || readlen == 0) && 201 (zret == Z_OK || zret == Z_BUF_ERROR)) { 202 stream.next_out = obuf; 203 stream.avail_out = sizeof(obuf); 204 zret = git_deflate(&stream, readlen ? 0 : Z_FINISH); 205 hashwrite(f, obuf, stream.next_out - obuf); 206 olen += stream.next_out - obuf; 207 } 208 if (stream.avail_in) 209 die(_("deflate error (%d)"), zret); 210 if (readlen == 0) { 211 if (zret != Z_STREAM_END) 212 die(_("deflate error (%d)"), zret); 213 break; 214 } 215 } 216 git_deflate_end(&stream); 217 return olen; 218} 219 220/* 221 * we are going to reuse the existing object data as is. make 222 * sure it is not corrupt. 223 */ 224static int check_pack_inflate(struct packed_git *p, 225 struct pack_window **w_curs, 226 off_t offset, 227 off_t len, 228 unsigned long expect) 229{ 230 git_zstream stream; 231 unsigned char fakebuf[4096], *in; 232 int st; 233 234 memset(&stream, 0, sizeof(stream)); 235 git_inflate_init(&stream); 236 do { 237 in = use_pack(p, w_curs, offset, &stream.avail_in); 238 stream.next_in = in; 239 stream.next_out = fakebuf; 240 stream.avail_out = sizeof(fakebuf); 241 st = git_inflate(&stream, Z_FINISH); 242 offset += stream.next_in - in; 243 } while (st == Z_OK || st == Z_BUF_ERROR); 244 git_inflate_end(&stream); 245 return (st == Z_STREAM_END && 246 stream.total_out == expect && 247 stream.total_in == len) ? 0 : -1; 248} 249 250static void copy_pack_data(struct hashfile *f, 251 struct packed_git *p, 252 struct pack_window **w_curs, 253 off_t offset, 254 off_t len) 255{ 256 unsigned char *in; 257 unsigned long avail; 258 259 while (len) { 260 in = use_pack(p, w_curs, offset, &avail); 261 if (avail > len) 262 avail = (unsigned long)len; 263 hashwrite(f, in, avail); 264 offset += avail; 265 len -= avail; 266 } 267} 268 269/* Return 0 if we will bust the pack-size limit */ 270static unsigned long write_no_reuse_object(struct hashfile *f, struct object_entry *entry, 271 unsigned long limit, int usable_delta) 272{ 273 unsigned long size, datalen; 274 unsigned char header[MAX_PACK_OBJECT_HEADER], 275 dheader[MAX_PACK_OBJECT_HEADER]; 276 unsigned hdrlen; 277 enum object_type type; 278 void *buf; 279 struct git_istream *st = NULL; 280 281 if (!usable_delta) { 282 if (oe_type(entry) == OBJ_BLOB && 283 oe_size_greater_than(&to_pack, entry, big_file_threshold) && 284 (st = open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 285 buf = NULL; 286 else { 287 buf = read_object_file(&entry->idx.oid, &type, &size); 288 if (!buf) 289 die(_("unable to read %s"), 290 oid_to_hex(&entry->idx.oid)); 291 } 292 /* 293 * make sure no cached delta data remains from a 294 * previous attempt before a pack split occurred. 295 */ 296 FREE_AND_NULL(entry->delta_data); 297 entry->z_delta_size = 0; 298 } else if (entry->delta_data) { 299 size = DELTA_SIZE(entry); 300 buf = entry->delta_data; 301 entry->delta_data = NULL; 302 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ? 303 OBJ_OFS_DELTA : OBJ_REF_DELTA; 304 } else { 305 buf = get_delta(entry); 306 size = DELTA_SIZE(entry); 307 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ? 308 OBJ_OFS_DELTA : OBJ_REF_DELTA; 309 } 310 311 if (st) /* large blob case, just assume we don't compress well */ 312 datalen = size; 313 else if (entry->z_delta_size) 314 datalen = entry->z_delta_size; 315 else 316 datalen = do_compress(&buf, size); 317 318 /* 319 * The object header is a byte of 'type' followed by zero or 320 * more bytes of length. 321 */ 322 hdrlen = encode_in_pack_object_header(header, sizeof(header), 323 type, size); 324 325 if (type == OBJ_OFS_DELTA) { 326 /* 327 * Deltas with relative base contain an additional 328 * encoding of the relative offset for the delta 329 * base from this object's position in the pack. 330 */ 331 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset; 332 unsigned pos = sizeof(dheader) - 1; 333 dheader[pos] = ofs & 127; 334 while (ofs >>= 7) 335 dheader[--pos] = 128 | (--ofs & 127); 336 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { 337 if (st) 338 close_istream(st); 339 free(buf); 340 return 0; 341 } 342 hashwrite(f, header, hdrlen); 343 hashwrite(f, dheader + pos, sizeof(dheader) - pos); 344 hdrlen += sizeof(dheader) - pos; 345 } else if (type == OBJ_REF_DELTA) { 346 /* 347 * Deltas with a base reference contain 348 * an additional 20 bytes for the base sha1. 349 */ 350 if (limit && hdrlen + 20 + datalen + 20 >= limit) { 351 if (st) 352 close_istream(st); 353 free(buf); 354 return 0; 355 } 356 hashwrite(f, header, hdrlen); 357 hashwrite(f, DELTA(entry)->idx.oid.hash, 20); 358 hdrlen += 20; 359 } else { 360 if (limit && hdrlen + datalen + 20 >= limit) { 361 if (st) 362 close_istream(st); 363 free(buf); 364 return 0; 365 } 366 hashwrite(f, header, hdrlen); 367 } 368 if (st) { 369 datalen = write_large_blob_data(st, f, &entry->idx.oid); 370 close_istream(st); 371 } else { 372 hashwrite(f, buf, datalen); 373 free(buf); 374 } 375 376 return hdrlen + datalen; 377} 378 379/* Return 0 if we will bust the pack-size limit */ 380static off_t write_reuse_object(struct hashfile *f, struct object_entry *entry, 381 unsigned long limit, int usable_delta) 382{ 383 struct packed_git *p = IN_PACK(entry); 384 struct pack_window *w_curs = NULL; 385 struct revindex_entry *revidx; 386 off_t offset; 387 enum object_type type = oe_type(entry); 388 off_t datalen; 389 unsigned char header[MAX_PACK_OBJECT_HEADER], 390 dheader[MAX_PACK_OBJECT_HEADER]; 391 unsigned hdrlen; 392 unsigned long entry_size = SIZE(entry); 393 394 if (DELTA(entry)) 395 type = (allow_ofs_delta && DELTA(entry)->idx.offset) ? 396 OBJ_OFS_DELTA : OBJ_REF_DELTA; 397 hdrlen = encode_in_pack_object_header(header, sizeof(header), 398 type, entry_size); 399 400 offset = entry->in_pack_offset; 401 revidx = find_pack_revindex(p, offset); 402 datalen = revidx[1].offset - offset; 403 if (!pack_to_stdout && p->index_version > 1 && 404 check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 405 error("bad packed object CRC for %s", 406 oid_to_hex(&entry->idx.oid)); 407 unuse_pack(&w_curs); 408 return write_no_reuse_object(f, entry, limit, usable_delta); 409 } 410 411 offset += entry->in_pack_header_size; 412 datalen -= entry->in_pack_header_size; 413 414 if (!pack_to_stdout && p->index_version == 1 && 415 check_pack_inflate(p, &w_curs, offset, datalen, entry_size)) { 416 error("corrupt packed object for %s", 417 oid_to_hex(&entry->idx.oid)); 418 unuse_pack(&w_curs); 419 return write_no_reuse_object(f, entry, limit, usable_delta); 420 } 421 422 if (type == OBJ_OFS_DELTA) { 423 off_t ofs = entry->idx.offset - DELTA(entry)->idx.offset; 424 unsigned pos = sizeof(dheader) - 1; 425 dheader[pos] = ofs & 127; 426 while (ofs >>= 7) 427 dheader[--pos] = 128 | (--ofs & 127); 428 if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) { 429 unuse_pack(&w_curs); 430 return 0; 431 } 432 hashwrite(f, header, hdrlen); 433 hashwrite(f, dheader + pos, sizeof(dheader) - pos); 434 hdrlen += sizeof(dheader) - pos; 435 reused_delta++; 436 } else if (type == OBJ_REF_DELTA) { 437 if (limit && hdrlen + 20 + datalen + 20 >= limit) { 438 unuse_pack(&w_curs); 439 return 0; 440 } 441 hashwrite(f, header, hdrlen); 442 hashwrite(f, DELTA(entry)->idx.oid.hash, 20); 443 hdrlen += 20; 444 reused_delta++; 445 } else { 446 if (limit && hdrlen + datalen + 20 >= limit) { 447 unuse_pack(&w_curs); 448 return 0; 449 } 450 hashwrite(f, header, hdrlen); 451 } 452 copy_pack_data(f, p, &w_curs, offset, datalen); 453 unuse_pack(&w_curs); 454 reused++; 455 return hdrlen + datalen; 456} 457 458/* Return 0 if we will bust the pack-size limit */ 459static off_t write_object(struct hashfile *f, 460 struct object_entry *entry, 461 off_t write_offset) 462{ 463 unsigned long limit; 464 off_t len; 465 int usable_delta, to_reuse; 466 467 if (!pack_to_stdout) 468 crc32_begin(f); 469 470 /* apply size limit if limited packsize and not first object */ 471 if (!pack_size_limit || !nr_written) 472 limit = 0; 473 else if (pack_size_limit <= write_offset) 474 /* 475 * the earlier object did not fit the limit; avoid 476 * mistaking this with unlimited (i.e. limit = 0). 477 */ 478 limit = 1; 479 else 480 limit = pack_size_limit - write_offset; 481 482 if (!DELTA(entry)) 483 usable_delta = 0; /* no delta */ 484 else if (!pack_size_limit) 485 usable_delta = 1; /* unlimited packfile */ 486 else if (DELTA(entry)->idx.offset == (off_t)-1) 487 usable_delta = 0; /* base was written to another pack */ 488 else if (DELTA(entry)->idx.offset) 489 usable_delta = 1; /* base already exists in this pack */ 490 else 491 usable_delta = 0; /* base could end up in another pack */ 492 493 if (!reuse_object) 494 to_reuse = 0; /* explicit */ 495 else if (!IN_PACK(entry)) 496 to_reuse = 0; /* can't reuse what we don't have */ 497 else if (oe_type(entry) == OBJ_REF_DELTA || 498 oe_type(entry) == OBJ_OFS_DELTA) 499 /* check_object() decided it for us ... */ 500 to_reuse = usable_delta; 501 /* ... but pack split may override that */ 502 else if (oe_type(entry) != entry->in_pack_type) 503 to_reuse = 0; /* pack has delta which is unusable */ 504 else if (DELTA(entry)) 505 to_reuse = 0; /* we want to pack afresh */ 506 else 507 to_reuse = 1; /* we have it in-pack undeltified, 508 * and we do not need to deltify it. 509 */ 510 511 if (!to_reuse) 512 len = write_no_reuse_object(f, entry, limit, usable_delta); 513 else 514 len = write_reuse_object(f, entry, limit, usable_delta); 515 if (!len) 516 return 0; 517 518 if (usable_delta) 519 written_delta++; 520 written++; 521 if (!pack_to_stdout) 522 entry->idx.crc32 = crc32_end(f); 523 return len; 524} 525 526enum write_one_status { 527 WRITE_ONE_SKIP = -1, /* already written */ 528 WRITE_ONE_BREAK = 0, /* writing this will bust the limit; not written */ 529 WRITE_ONE_WRITTEN = 1, /* normal */ 530 WRITE_ONE_RECURSIVE = 2 /* already scheduled to be written */ 531}; 532 533static enum write_one_status write_one(struct hashfile *f, 534 struct object_entry *e, 535 off_t *offset) 536{ 537 off_t size; 538 int recursing; 539 540 /* 541 * we set offset to 1 (which is an impossible value) to mark 542 * the fact that this object is involved in "write its base 543 * first before writing a deltified object" recursion. 544 */ 545 recursing = (e->idx.offset == 1); 546 if (recursing) { 547 warning("recursive delta detected for object %s", 548 oid_to_hex(&e->idx.oid)); 549 return WRITE_ONE_RECURSIVE; 550 } else if (e->idx.offset || e->preferred_base) { 551 /* offset is non zero if object is written already. */ 552 return WRITE_ONE_SKIP; 553 } 554 555 /* if we are deltified, write out base object first. */ 556 if (DELTA(e)) { 557 e->idx.offset = 1; /* now recurse */ 558 switch (write_one(f, DELTA(e), offset)) { 559 case WRITE_ONE_RECURSIVE: 560 /* we cannot depend on this one */ 561 SET_DELTA(e, NULL); 562 break; 563 default: 564 break; 565 case WRITE_ONE_BREAK: 566 e->idx.offset = recursing; 567 return WRITE_ONE_BREAK; 568 } 569 } 570 571 e->idx.offset = *offset; 572 size = write_object(f, e, *offset); 573 if (!size) { 574 e->idx.offset = recursing; 575 return WRITE_ONE_BREAK; 576 } 577 written_list[nr_written++] = &e->idx; 578 579 /* make sure off_t is sufficiently large not to wrap */ 580 if (signed_add_overflows(*offset, size)) 581 die("pack too large for current definition of off_t"); 582 *offset += size; 583 return WRITE_ONE_WRITTEN; 584} 585 586static int mark_tagged(const char *path, const struct object_id *oid, int flag, 587 void *cb_data) 588{ 589 struct object_id peeled; 590 struct object_entry *entry = packlist_find(&to_pack, oid->hash, NULL); 591 592 if (entry) 593 entry->tagged = 1; 594 if (!peel_ref(path, &peeled)) { 595 entry = packlist_find(&to_pack, peeled.hash, NULL); 596 if (entry) 597 entry->tagged = 1; 598 } 599 return 0; 600} 601 602static inline void add_to_write_order(struct object_entry **wo, 603 unsigned int *endp, 604 struct object_entry *e) 605{ 606 if (e->filled) 607 return; 608 wo[(*endp)++] = e; 609 e->filled = 1; 610} 611 612static void add_descendants_to_write_order(struct object_entry **wo, 613 unsigned int *endp, 614 struct object_entry *e) 615{ 616 int add_to_order = 1; 617 while (e) { 618 if (add_to_order) { 619 struct object_entry *s; 620 /* add this node... */ 621 add_to_write_order(wo, endp, e); 622 /* all its siblings... */ 623 for (s = DELTA_SIBLING(e); s; s = DELTA_SIBLING(s)) { 624 add_to_write_order(wo, endp, s); 625 } 626 } 627 /* drop down a level to add left subtree nodes if possible */ 628 if (DELTA_CHILD(e)) { 629 add_to_order = 1; 630 e = DELTA_CHILD(e); 631 } else { 632 add_to_order = 0; 633 /* our sibling might have some children, it is next */ 634 if (DELTA_SIBLING(e)) { 635 e = DELTA_SIBLING(e); 636 continue; 637 } 638 /* go back to our parent node */ 639 e = DELTA(e); 640 while (e && !DELTA_SIBLING(e)) { 641 /* we're on the right side of a subtree, keep 642 * going up until we can go right again */ 643 e = DELTA(e); 644 } 645 if (!e) { 646 /* done- we hit our original root node */ 647 return; 648 } 649 /* pass it off to sibling at this level */ 650 e = DELTA_SIBLING(e); 651 } 652 }; 653} 654 655static void add_family_to_write_order(struct object_entry **wo, 656 unsigned int *endp, 657 struct object_entry *e) 658{ 659 struct object_entry *root; 660 661 for (root = e; DELTA(root); root = DELTA(root)) 662 ; /* nothing */ 663 add_descendants_to_write_order(wo, endp, root); 664} 665 666static struct object_entry **compute_write_order(void) 667{ 668 unsigned int i, wo_end, last_untagged; 669 670 struct object_entry **wo; 671 struct object_entry *objects = to_pack.objects; 672 673 for (i = 0; i < to_pack.nr_objects; i++) { 674 objects[i].tagged = 0; 675 objects[i].filled = 0; 676 SET_DELTA_CHILD(&objects[i], NULL); 677 SET_DELTA_SIBLING(&objects[i], NULL); 678 } 679 680 /* 681 * Fully connect delta_child/delta_sibling network. 682 * Make sure delta_sibling is sorted in the original 683 * recency order. 684 */ 685 for (i = to_pack.nr_objects; i > 0;) { 686 struct object_entry *e = &objects[--i]; 687 if (!DELTA(e)) 688 continue; 689 /* Mark me as the first child */ 690 e->delta_sibling_idx = DELTA(e)->delta_child_idx; 691 SET_DELTA_CHILD(DELTA(e), e); 692 } 693 694 /* 695 * Mark objects that are at the tip of tags. 696 */ 697 for_each_tag_ref(mark_tagged, NULL); 698 699 /* 700 * Give the objects in the original recency order until 701 * we see a tagged tip. 702 */ 703 ALLOC_ARRAY(wo, to_pack.nr_objects); 704 for (i = wo_end = 0; i < to_pack.nr_objects; i++) { 705 if (objects[i].tagged) 706 break; 707 add_to_write_order(wo, &wo_end, &objects[i]); 708 } 709 last_untagged = i; 710 711 /* 712 * Then fill all the tagged tips. 713 */ 714 for (; i < to_pack.nr_objects; i++) { 715 if (objects[i].tagged) 716 add_to_write_order(wo, &wo_end, &objects[i]); 717 } 718 719 /* 720 * And then all remaining commits and tags. 721 */ 722 for (i = last_untagged; i < to_pack.nr_objects; i++) { 723 if (oe_type(&objects[i]) != OBJ_COMMIT && 724 oe_type(&objects[i]) != OBJ_TAG) 725 continue; 726 add_to_write_order(wo, &wo_end, &objects[i]); 727 } 728 729 /* 730 * And then all the trees. 731 */ 732 for (i = last_untagged; i < to_pack.nr_objects; i++) { 733 if (oe_type(&objects[i]) != OBJ_TREE) 734 continue; 735 add_to_write_order(wo, &wo_end, &objects[i]); 736 } 737 738 /* 739 * Finally all the rest in really tight order 740 */ 741 for (i = last_untagged; i < to_pack.nr_objects; i++) { 742 if (!objects[i].filled) 743 add_family_to_write_order(wo, &wo_end, &objects[i]); 744 } 745 746 if (wo_end != to_pack.nr_objects) 747 die("ordered %u objects, expected %"PRIu32, wo_end, to_pack.nr_objects); 748 749 return wo; 750} 751 752static off_t write_reused_pack(struct hashfile *f) 753{ 754 unsigned char buffer[8192]; 755 off_t to_write, total; 756 int fd; 757 758 if (!is_pack_valid(reuse_packfile)) 759 die("packfile is invalid: %s", reuse_packfile->pack_name); 760 761 fd = git_open(reuse_packfile->pack_name); 762 if (fd < 0) 763 die_errno("unable to open packfile for reuse: %s", 764 reuse_packfile->pack_name); 765 766 if (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1) 767 die_errno("unable to seek in reused packfile"); 768 769 if (reuse_packfile_offset < 0) 770 reuse_packfile_offset = reuse_packfile->pack_size - 20; 771 772 total = to_write = reuse_packfile_offset - sizeof(struct pack_header); 773 774 while (to_write) { 775 int read_pack = xread(fd, buffer, sizeof(buffer)); 776 777 if (read_pack <= 0) 778 die_errno("unable to read from reused packfile"); 779 780 if (read_pack > to_write) 781 read_pack = to_write; 782 783 hashwrite(f, buffer, read_pack); 784 to_write -= read_pack; 785 786 /* 787 * We don't know the actual number of objects written, 788 * only how many bytes written, how many bytes total, and 789 * how many objects total. So we can fake it by pretending all 790 * objects we are writing are the same size. This gives us a 791 * smooth progress meter, and at the end it matches the true 792 * answer. 793 */ 794 written = reuse_packfile_objects * 795 (((double)(total - to_write)) / total); 796 display_progress(progress_state, written); 797 } 798 799 close(fd); 800 written = reuse_packfile_objects; 801 display_progress(progress_state, written); 802 return reuse_packfile_offset - sizeof(struct pack_header); 803} 804 805static const char no_split_warning[] = N_( 806"disabling bitmap writing, packs are split due to pack.packSizeLimit" 807); 808 809static void write_pack_file(void) 810{ 811 uint32_t i = 0, j; 812 struct hashfile *f; 813 off_t offset; 814 uint32_t nr_remaining = nr_result; 815 time_t last_mtime = 0; 816 struct object_entry **write_order; 817 818 if (progress > pack_to_stdout) 819 progress_state = start_progress(_("Writing objects"), nr_result); 820 ALLOC_ARRAY(written_list, to_pack.nr_objects); 821 write_order = compute_write_order(); 822 823 do { 824 struct object_id oid; 825 char *pack_tmp_name = NULL; 826 827 if (pack_to_stdout) 828 f = hashfd_throughput(1, "<stdout>", progress_state); 829 else 830 f = create_tmp_packfile(&pack_tmp_name); 831 832 offset = write_pack_header(f, nr_remaining); 833 834 if (reuse_packfile) { 835 off_t packfile_size; 836 assert(pack_to_stdout); 837 838 packfile_size = write_reused_pack(f); 839 offset += packfile_size; 840 } 841 842 nr_written = 0; 843 for (; i < to_pack.nr_objects; i++) { 844 struct object_entry *e = write_order[i]; 845 if (write_one(f, e, &offset) == WRITE_ONE_BREAK) 846 break; 847 display_progress(progress_state, written); 848 } 849 850 /* 851 * Did we write the wrong # entries in the header? 852 * If so, rewrite it like in fast-import 853 */ 854 if (pack_to_stdout) { 855 hashclose(f, oid.hash, CSUM_CLOSE); 856 } else if (nr_written == nr_remaining) { 857 hashclose(f, oid.hash, CSUM_FSYNC); 858 } else { 859 int fd = hashclose(f, oid.hash, 0); 860 fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 861 nr_written, oid.hash, offset); 862 close(fd); 863 if (write_bitmap_index) { 864 warning(_(no_split_warning)); 865 write_bitmap_index = 0; 866 } 867 } 868 869 if (!pack_to_stdout) { 870 struct stat st; 871 struct strbuf tmpname = STRBUF_INIT; 872 873 /* 874 * Packs are runtime accessed in their mtime 875 * order since newer packs are more likely to contain 876 * younger objects. So if we are creating multiple 877 * packs then we should modify the mtime of later ones 878 * to preserve this property. 879 */ 880 if (stat(pack_tmp_name, &st) < 0) { 881 warning_errno("failed to stat %s", pack_tmp_name); 882 } else if (!last_mtime) { 883 last_mtime = st.st_mtime; 884 } else { 885 struct utimbuf utb; 886 utb.actime = st.st_atime; 887 utb.modtime = --last_mtime; 888 if (utime(pack_tmp_name, &utb) < 0) 889 warning_errno("failed utime() on %s", pack_tmp_name); 890 } 891 892 strbuf_addf(&tmpname, "%s-", base_name); 893 894 if (write_bitmap_index) { 895 bitmap_writer_set_checksum(oid.hash); 896 bitmap_writer_build_type_index( 897 &to_pack, written_list, nr_written); 898 } 899 900 finish_tmp_packfile(&tmpname, pack_tmp_name, 901 written_list, nr_written, 902 &pack_idx_opts, oid.hash); 903 904 if (write_bitmap_index) { 905 strbuf_addf(&tmpname, "%s.bitmap", oid_to_hex(&oid)); 906 907 stop_progress(&progress_state); 908 909 bitmap_writer_show_progress(progress); 910 bitmap_writer_reuse_bitmaps(&to_pack); 911 bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 912 bitmap_writer_build(&to_pack); 913 bitmap_writer_finish(written_list, nr_written, 914 tmpname.buf, write_bitmap_options); 915 write_bitmap_index = 0; 916 } 917 918 strbuf_release(&tmpname); 919 free(pack_tmp_name); 920 puts(oid_to_hex(&oid)); 921 } 922 923 /* mark written objects as written to previous pack */ 924 for (j = 0; j < nr_written; j++) { 925 written_list[j]->offset = (off_t)-1; 926 } 927 nr_remaining -= nr_written; 928 } while (nr_remaining && i < to_pack.nr_objects); 929 930 free(written_list); 931 free(write_order); 932 stop_progress(&progress_state); 933 if (written != nr_result) 934 die("wrote %"PRIu32" objects while expecting %"PRIu32, 935 written, nr_result); 936} 937 938static int no_try_delta(const char *path) 939{ 940 static struct attr_check *check; 941 942 if (!check) 943 check = attr_check_initl("delta", NULL); 944 if (git_check_attr(path, check)) 945 return 0; 946 if (ATTR_FALSE(check->items[0].value)) 947 return 1; 948 return 0; 949} 950 951/* 952 * When adding an object, check whether we have already added it 953 * to our packing list. If so, we can skip. However, if we are 954 * being asked to excludei t, but the previous mention was to include 955 * it, make sure to adjust its flags and tweak our numbers accordingly. 956 * 957 * As an optimization, we pass out the index position where we would have 958 * found the item, since that saves us from having to look it up again a 959 * few lines later when we want to add the new entry. 960 */ 961static int have_duplicate_entry(const struct object_id *oid, 962 int exclude, 963 uint32_t *index_pos) 964{ 965 struct object_entry *entry; 966 967 entry = packlist_find(&to_pack, oid->hash, index_pos); 968 if (!entry) 969 return 0; 970 971 if (exclude) { 972 if (!entry->preferred_base) 973 nr_result--; 974 entry->preferred_base = 1; 975 } 976 977 return 1; 978} 979 980static int want_found_object(int exclude, struct packed_git *p) 981{ 982 if (exclude) 983 return 1; 984 if (incremental) 985 return 0; 986 987 /* 988 * When asked to do --local (do not include an object that appears in a 989 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 990 * an object that appears in a pack marked with .keep), finding a pack 991 * that matches the criteria is sufficient for us to decide to omit it. 992 * However, even if this pack does not satisfy the criteria, we need to 993 * make sure no copy of this object appears in _any_ pack that makes us 994 * to omit the object, so we need to check all the packs. 995 * 996 * We can however first check whether these options can possible matter; 997 * if they do not matter we know we want the object in generated pack. 998 * Otherwise, we signal "-1" at the end to tell the caller that we do 999 * not know either way, and it needs to check more packs.1000 */1001 if (!ignore_packed_keep &&1002 (!local || !have_non_local_packs))1003 return 1;10041005 if (local && !p->pack_local)1006 return 0;1007 if (ignore_packed_keep && p->pack_local && p->pack_keep)1008 return 0;10091010 /* we don't know yet; keep looking for more packs */1011 return -1;1012}10131014/*1015 * Check whether we want the object in the pack (e.g., we do not want1016 * objects found in non-local stores if the "--local" option was used).1017 *1018 * If the caller already knows an existing pack it wants to take the object1019 * from, that is passed in *found_pack and *found_offset; otherwise this1020 * function finds if there is any pack that has the object and returns the pack1021 * and its offset in these variables.1022 */1023static int want_object_in_pack(const struct object_id *oid,1024 int exclude,1025 struct packed_git **found_pack,1026 off_t *found_offset)1027{1028 int want;1029 struct list_head *pos;10301031 if (!exclude && local && has_loose_object_nonlocal(oid->hash))1032 return 0;10331034 /*1035 * If we already know the pack object lives in, start checks from that1036 * pack - in the usual case when neither --local was given nor .keep files1037 * are present we will determine the answer right now.1038 */1039 if (*found_pack) {1040 want = want_found_object(exclude, *found_pack);1041 if (want != -1)1042 return want;1043 }1044 list_for_each(pos, get_packed_git_mru(the_repository)) {1045 struct packed_git *p = list_entry(pos, struct packed_git, mru);1046 off_t offset;10471048 if (p == *found_pack)1049 offset = *found_offset;1050 else1051 offset = find_pack_entry_one(oid->hash, p);10521053 if (offset) {1054 if (!*found_pack) {1055 if (!is_pack_valid(p))1056 continue;1057 *found_offset = offset;1058 *found_pack = p;1059 }1060 want = want_found_object(exclude, p);1061 if (!exclude && want > 0)1062 list_move(&p->mru,1063 get_packed_git_mru(the_repository));1064 if (want != -1)1065 return want;1066 }1067 }10681069 return 1;1070}10711072static void create_object_entry(const struct object_id *oid,1073 enum object_type type,1074 uint32_t hash,1075 int exclude,1076 int no_try_delta,1077 uint32_t index_pos,1078 struct packed_git *found_pack,1079 off_t found_offset)1080{1081 struct object_entry *entry;10821083 entry = packlist_alloc(&to_pack, oid->hash, index_pos);1084 entry->hash = hash;1085 oe_set_type(entry, type);1086 if (exclude)1087 entry->preferred_base = 1;1088 else1089 nr_result++;1090 if (found_pack) {1091 oe_set_in_pack(&to_pack, entry, found_pack);1092 entry->in_pack_offset = found_offset;1093 }10941095 entry->no_try_delta = no_try_delta;1096}10971098static const char no_closure_warning[] = N_(1099"disabling bitmap writing, as some objects are not being packed"1100);11011102static int add_object_entry(const struct object_id *oid, enum object_type type,1103 const char *name, int exclude)1104{1105 struct packed_git *found_pack = NULL;1106 off_t found_offset = 0;1107 uint32_t index_pos;11081109 if (have_duplicate_entry(oid, exclude, &index_pos))1110 return 0;11111112 if (!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1113 /* The pack is missing an object, so it will not have closure */1114 if (write_bitmap_index) {1115 warning(_(no_closure_warning));1116 write_bitmap_index = 0;1117 }1118 return 0;1119 }11201121 create_object_entry(oid, type, pack_name_hash(name),1122 exclude, name && no_try_delta(name),1123 index_pos, found_pack, found_offset);11241125 display_progress(progress_state, nr_result);1126 return 1;1127}11281129static int add_object_entry_from_bitmap(const struct object_id *oid,1130 enum object_type type,1131 int flags, uint32_t name_hash,1132 struct packed_git *pack, off_t offset)1133{1134 uint32_t index_pos;11351136 if (have_duplicate_entry(oid, 0, &index_pos))1137 return 0;11381139 if (!want_object_in_pack(oid, 0, &pack, &offset))1140 return 0;11411142 create_object_entry(oid, type, name_hash, 0, 0, index_pos, pack, offset);11431144 display_progress(progress_state, nr_result);1145 return 1;1146}11471148struct pbase_tree_cache {1149 struct object_id oid;1150 int ref;1151 int temporary;1152 void *tree_data;1153 unsigned long tree_size;1154};11551156static struct pbase_tree_cache *(pbase_tree_cache[256]);1157static int pbase_tree_cache_ix(const struct object_id *oid)1158{1159 return oid->hash[0] % ARRAY_SIZE(pbase_tree_cache);1160}1161static int pbase_tree_cache_ix_incr(int ix)1162{1163 return (ix+1) % ARRAY_SIZE(pbase_tree_cache);1164}11651166static struct pbase_tree {1167 struct pbase_tree *next;1168 /* This is a phony "cache" entry; we are not1169 * going to evict it or find it through _get()1170 * mechanism -- this is for the toplevel node that1171 * would almost always change with any commit.1172 */1173 struct pbase_tree_cache pcache;1174} *pbase_tree;11751176static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1177{1178 struct pbase_tree_cache *ent, *nent;1179 void *data;1180 unsigned long size;1181 enum object_type type;1182 int neigh;1183 int my_ix = pbase_tree_cache_ix(oid);1184 int available_ix = -1;11851186 /* pbase-tree-cache acts as a limited hashtable.1187 * your object will be found at your index or within a few1188 * slots after that slot if it is cached.1189 */1190 for (neigh = 0; neigh < 8; neigh++) {1191 ent = pbase_tree_cache[my_ix];1192 if (ent && !oidcmp(&ent->oid, oid)) {1193 ent->ref++;1194 return ent;1195 }1196 else if (((available_ix < 0) && (!ent || !ent->ref)) ||1197 ((0 <= available_ix) &&1198 (!ent && pbase_tree_cache[available_ix])))1199 available_ix = my_ix;1200 if (!ent)1201 break;1202 my_ix = pbase_tree_cache_ix_incr(my_ix);1203 }12041205 /* Did not find one. Either we got a bogus request or1206 * we need to read and perhaps cache.1207 */1208 data = read_object_file(oid, &type, &size);1209 if (!data)1210 return NULL;1211 if (type != OBJ_TREE) {1212 free(data);1213 return NULL;1214 }12151216 /* We need to either cache or return a throwaway copy */12171218 if (available_ix < 0)1219 ent = NULL;1220 else {1221 ent = pbase_tree_cache[available_ix];1222 my_ix = available_ix;1223 }12241225 if (!ent) {1226 nent = xmalloc(sizeof(*nent));1227 nent->temporary = (available_ix < 0);1228 }1229 else {1230 /* evict and reuse */1231 free(ent->tree_data);1232 nent = ent;1233 }1234 oidcpy(&nent->oid, oid);1235 nent->tree_data = data;1236 nent->tree_size = size;1237 nent->ref = 1;1238 if (!nent->temporary)1239 pbase_tree_cache[my_ix] = nent;1240 return nent;1241}12421243static void pbase_tree_put(struct pbase_tree_cache *cache)1244{1245 if (!cache->temporary) {1246 cache->ref--;1247 return;1248 }1249 free(cache->tree_data);1250 free(cache);1251}12521253static int name_cmp_len(const char *name)1254{1255 int i;1256 for (i = 0; name[i] && name[i] != '\n' && name[i] != '/'; i++)1257 ;1258 return i;1259}12601261static void add_pbase_object(struct tree_desc *tree,1262 const char *name,1263 int cmplen,1264 const char *fullname)1265{1266 struct name_entry entry;1267 int cmp;12681269 while (tree_entry(tree,&entry)) {1270 if (S_ISGITLINK(entry.mode))1271 continue;1272 cmp = tree_entry_len(&entry) != cmplen ? 1 :1273 memcmp(name, entry.path, cmplen);1274 if (cmp > 0)1275 continue;1276 if (cmp < 0)1277 return;1278 if (name[cmplen] != '/') {1279 add_object_entry(entry.oid,1280 object_type(entry.mode),1281 fullname, 1);1282 return;1283 }1284 if (S_ISDIR(entry.mode)) {1285 struct tree_desc sub;1286 struct pbase_tree_cache *tree;1287 const char *down = name+cmplen+1;1288 int downlen = name_cmp_len(down);12891290 tree = pbase_tree_get(entry.oid);1291 if (!tree)1292 return;1293 init_tree_desc(&sub, tree->tree_data, tree->tree_size);12941295 add_pbase_object(&sub, down, downlen, fullname);1296 pbase_tree_put(tree);1297 }1298 }1299}13001301static unsigned *done_pbase_paths;1302static int done_pbase_paths_num;1303static int done_pbase_paths_alloc;1304static int done_pbase_path_pos(unsigned hash)1305{1306 int lo = 0;1307 int hi = done_pbase_paths_num;1308 while (lo < hi) {1309 int mi = lo + (hi - lo) / 2;1310 if (done_pbase_paths[mi] == hash)1311 return mi;1312 if (done_pbase_paths[mi] < hash)1313 hi = mi;1314 else1315 lo = mi + 1;1316 }1317 return -lo-1;1318}13191320static int check_pbase_path(unsigned hash)1321{1322 int pos = done_pbase_path_pos(hash);1323 if (0 <= pos)1324 return 1;1325 pos = -pos - 1;1326 ALLOC_GROW(done_pbase_paths,1327 done_pbase_paths_num + 1,1328 done_pbase_paths_alloc);1329 done_pbase_paths_num++;1330 if (pos < done_pbase_paths_num)1331 MOVE_ARRAY(done_pbase_paths + pos + 1, done_pbase_paths + pos,1332 done_pbase_paths_num - pos - 1);1333 done_pbase_paths[pos] = hash;1334 return 0;1335}13361337static void add_preferred_base_object(const char *name)1338{1339 struct pbase_tree *it;1340 int cmplen;1341 unsigned hash = pack_name_hash(name);13421343 if (!num_preferred_base || check_pbase_path(hash))1344 return;13451346 cmplen = name_cmp_len(name);1347 for (it = pbase_tree; it; it = it->next) {1348 if (cmplen == 0) {1349 add_object_entry(&it->pcache.oid, OBJ_TREE, NULL, 1);1350 }1351 else {1352 struct tree_desc tree;1353 init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1354 add_pbase_object(&tree, name, cmplen, name);1355 }1356 }1357}13581359static void add_preferred_base(struct object_id *oid)1360{1361 struct pbase_tree *it;1362 void *data;1363 unsigned long size;1364 struct object_id tree_oid;13651366 if (window <= num_preferred_base++)1367 return;13681369 data = read_object_with_reference(oid, tree_type, &size, &tree_oid);1370 if (!data)1371 return;13721373 for (it = pbase_tree; it; it = it->next) {1374 if (!oidcmp(&it->pcache.oid, &tree_oid)) {1375 free(data);1376 return;1377 }1378 }13791380 it = xcalloc(1, sizeof(*it));1381 it->next = pbase_tree;1382 pbase_tree = it;13831384 oidcpy(&it->pcache.oid, &tree_oid);1385 it->pcache.tree_data = data;1386 it->pcache.tree_size = size;1387}13881389static void cleanup_preferred_base(void)1390{1391 struct pbase_tree *it;1392 unsigned i;13931394 it = pbase_tree;1395 pbase_tree = NULL;1396 while (it) {1397 struct pbase_tree *tmp = it;1398 it = tmp->next;1399 free(tmp->pcache.tree_data);1400 free(tmp);1401 }14021403 for (i = 0; i < ARRAY_SIZE(pbase_tree_cache); i++) {1404 if (!pbase_tree_cache[i])1405 continue;1406 free(pbase_tree_cache[i]->tree_data);1407 FREE_AND_NULL(pbase_tree_cache[i]);1408 }14091410 FREE_AND_NULL(done_pbase_paths);1411 done_pbase_paths_num = done_pbase_paths_alloc = 0;1412}14131414static void check_object(struct object_entry *entry)1415{1416 unsigned long canonical_size;14171418 if (IN_PACK(entry)) {1419 struct packed_git *p = IN_PACK(entry);1420 struct pack_window *w_curs = NULL;1421 const unsigned char *base_ref = NULL;1422 struct object_entry *base_entry;1423 unsigned long used, used_0;1424 unsigned long avail;1425 off_t ofs;1426 unsigned char *buf, c;1427 enum object_type type;1428 unsigned long in_pack_size;14291430 buf = use_pack(p, &w_curs, entry->in_pack_offset, &avail);14311432 /*1433 * We want in_pack_type even if we do not reuse delta1434 * since non-delta representations could still be reused.1435 */1436 used = unpack_object_header_buffer(buf, avail,1437 &type,1438 &in_pack_size);1439 if (used == 0)1440 goto give_up;14411442 if (type < 0)1443 BUG("invalid type %d", type);1444 entry->in_pack_type = type;14451446 /*1447 * Determine if this is a delta and if so whether we can1448 * reuse it or not. Otherwise let's find out as cheaply as1449 * possible what the actual type and size for this object is.1450 */1451 switch (entry->in_pack_type) {1452 default:1453 /* Not a delta hence we've already got all we need. */1454 oe_set_type(entry, entry->in_pack_type);1455 SET_SIZE(entry, in_pack_size);1456 entry->in_pack_header_size = used;1457 if (oe_type(entry) < OBJ_COMMIT || oe_type(entry) > OBJ_BLOB)1458 goto give_up;1459 unuse_pack(&w_curs);1460 return;1461 case OBJ_REF_DELTA:1462 if (reuse_delta && !entry->preferred_base)1463 base_ref = use_pack(p, &w_curs,1464 entry->in_pack_offset + used, NULL);1465 entry->in_pack_header_size = used + 20;1466 break;1467 case OBJ_OFS_DELTA:1468 buf = use_pack(p, &w_curs,1469 entry->in_pack_offset + used, NULL);1470 used_0 = 0;1471 c = buf[used_0++];1472 ofs = c & 127;1473 while (c & 128) {1474 ofs += 1;1475 if (!ofs || MSB(ofs, 7)) {1476 error("delta base offset overflow in pack for %s",1477 oid_to_hex(&entry->idx.oid));1478 goto give_up;1479 }1480 c = buf[used_0++];1481 ofs = (ofs << 7) + (c & 127);1482 }1483 ofs = entry->in_pack_offset - ofs;1484 if (ofs <= 0 || ofs >= entry->in_pack_offset) {1485 error("delta base offset out of bound for %s",1486 oid_to_hex(&entry->idx.oid));1487 goto give_up;1488 }1489 if (reuse_delta && !entry->preferred_base) {1490 struct revindex_entry *revidx;1491 revidx = find_pack_revindex(p, ofs);1492 if (!revidx)1493 goto give_up;1494 base_ref = nth_packed_object_sha1(p, revidx->nr);1495 }1496 entry->in_pack_header_size = used + used_0;1497 break;1498 }14991500 if (base_ref && (base_entry = packlist_find(&to_pack, base_ref, NULL))) {1501 /*1502 * If base_ref was set above that means we wish to1503 * reuse delta data, and we even found that base1504 * in the list of objects we want to pack. Goodie!1505 *1506 * Depth value does not matter - find_deltas() will1507 * never consider reused delta as the base object to1508 * deltify other objects against, in order to avoid1509 * circular deltas.1510 */1511 oe_set_type(entry, entry->in_pack_type);1512 SET_SIZE(entry, in_pack_size); /* delta size */1513 SET_DELTA(entry, base_entry);1514 SET_DELTA_SIZE(entry, in_pack_size);1515 entry->delta_sibling_idx = base_entry->delta_child_idx;1516 SET_DELTA_CHILD(base_entry, entry);1517 unuse_pack(&w_curs);1518 return;1519 }15201521 if (oe_type(entry)) {1522 off_t delta_pos;15231524 /*1525 * This must be a delta and we already know what the1526 * final object type is. Let's extract the actual1527 * object size from the delta header.1528 */1529 delta_pos = entry->in_pack_offset + entry->in_pack_header_size;1530 canonical_size = get_size_from_delta(p, &w_curs, delta_pos);1531 if (canonical_size == 0)1532 goto give_up;1533 SET_SIZE(entry, canonical_size);1534 unuse_pack(&w_curs);1535 return;1536 }15371538 /*1539 * No choice but to fall back to the recursive delta walk1540 * with sha1_object_info() to find about the object type1541 * at this point...1542 */1543 give_up:1544 unuse_pack(&w_curs);1545 }15461547 oe_set_type(entry, oid_object_info(&entry->idx.oid, &canonical_size));1548 if (entry->type_valid) {1549 SET_SIZE(entry, canonical_size);1550 } else {1551 /*1552 * Bad object type is checked in prepare_pack(). This is1553 * to permit a missing preferred base object to be ignored1554 * as a preferred base. Doing so can result in a larger1555 * pack file, but the transfer will still take place.1556 */1557 }1558}15591560static int pack_offset_sort(const void *_a, const void *_b)1561{1562 const struct object_entry *a = *(struct object_entry **)_a;1563 const struct object_entry *b = *(struct object_entry **)_b;1564 const struct packed_git *a_in_pack = IN_PACK(a);1565 const struct packed_git *b_in_pack = IN_PACK(b);15661567 /* avoid filesystem trashing with loose objects */1568 if (!a_in_pack && !b_in_pack)1569 return oidcmp(&a->idx.oid, &b->idx.oid);15701571 if (a_in_pack < b_in_pack)1572 return -1;1573 if (a_in_pack > b_in_pack)1574 return 1;1575 return a->in_pack_offset < b->in_pack_offset ? -1 :1576 (a->in_pack_offset > b->in_pack_offset);1577}15781579/*1580 * Drop an on-disk delta we were planning to reuse. Naively, this would1581 * just involve blanking out the "delta" field, but we have to deal1582 * with some extra book-keeping:1583 *1584 * 1. Removing ourselves from the delta_sibling linked list.1585 *1586 * 2. Updating our size/type to the non-delta representation. These were1587 * either not recorded initially (size) or overwritten with the delta type1588 * (type) when check_object() decided to reuse the delta.1589 *1590 * 3. Resetting our delta depth, as we are now a base object.1591 */1592static void drop_reused_delta(struct object_entry *entry)1593{1594 unsigned *idx = &to_pack.objects[entry->delta_idx - 1].delta_child_idx;1595 struct object_info oi = OBJECT_INFO_INIT;1596 enum object_type type;1597 unsigned long size;15981599 while (*idx) {1600 struct object_entry *oe = &to_pack.objects[*idx - 1];16011602 if (oe == entry)1603 *idx = oe->delta_sibling_idx;1604 else1605 idx = &oe->delta_sibling_idx;1606 }1607 SET_DELTA(entry, NULL);1608 entry->depth = 0;16091610 oi.sizep = &size;1611 oi.typep = &type;1612 if (packed_object_info(IN_PACK(entry), entry->in_pack_offset, &oi) < 0) {1613 /*1614 * We failed to get the info from this pack for some reason;1615 * fall back to sha1_object_info, which may find another copy.1616 * And if that fails, the error will be recorded in oe_type(entry)1617 * and dealt with in prepare_pack().1618 */1619 oe_set_type(entry, oid_object_info(&entry->idx.oid, &size));1620 } else {1621 oe_set_type(entry, type);1622 }1623 SET_SIZE(entry, size);1624}16251626/*1627 * Follow the chain of deltas from this entry onward, throwing away any links1628 * that cause us to hit a cycle (as determined by the DFS state flags in1629 * the entries).1630 *1631 * We also detect too-long reused chains that would violate our --depth1632 * limit.1633 */1634static void break_delta_chains(struct object_entry *entry)1635{1636 /*1637 * The actual depth of each object we will write is stored as an int,1638 * as it cannot exceed our int "depth" limit. But before we break1639 * changes based no that limit, we may potentially go as deep as the1640 * number of objects, which is elsewhere bounded to a uint32_t.1641 */1642 uint32_t total_depth;1643 struct object_entry *cur, *next;16441645 for (cur = entry, total_depth = 0;1646 cur;1647 cur = DELTA(cur), total_depth++) {1648 if (cur->dfs_state == DFS_DONE) {1649 /*1650 * We've already seen this object and know it isn't1651 * part of a cycle. We do need to append its depth1652 * to our count.1653 */1654 total_depth += cur->depth;1655 break;1656 }16571658 /*1659 * We break cycles before looping, so an ACTIVE state (or any1660 * other cruft which made its way into the state variable)1661 * is a bug.1662 */1663 if (cur->dfs_state != DFS_NONE)1664 die("BUG: confusing delta dfs state in first pass: %d",1665 cur->dfs_state);16661667 /*1668 * Now we know this is the first time we've seen the object. If1669 * it's not a delta, we're done traversing, but we'll mark it1670 * done to save time on future traversals.1671 */1672 if (!DELTA(cur)) {1673 cur->dfs_state = DFS_DONE;1674 break;1675 }16761677 /*1678 * Mark ourselves as active and see if the next step causes1679 * us to cycle to another active object. It's important to do1680 * this _before_ we loop, because it impacts where we make the1681 * cut, and thus how our total_depth counter works.1682 * E.g., We may see a partial loop like:1683 *1684 * A -> B -> C -> D -> B1685 *1686 * Cutting B->C breaks the cycle. But now the depth of A is1687 * only 1, and our total_depth counter is at 3. The size of the1688 * error is always one less than the size of the cycle we1689 * broke. Commits C and D were "lost" from A's chain.1690 *1691 * If we instead cut D->B, then the depth of A is correct at 3.1692 * We keep all commits in the chain that we examined.1693 */1694 cur->dfs_state = DFS_ACTIVE;1695 if (DELTA(cur)->dfs_state == DFS_ACTIVE) {1696 drop_reused_delta(cur);1697 cur->dfs_state = DFS_DONE;1698 break;1699 }1700 }17011702 /*1703 * And now that we've gone all the way to the bottom of the chain, we1704 * need to clear the active flags and set the depth fields as1705 * appropriate. Unlike the loop above, which can quit when it drops a1706 * delta, we need to keep going to look for more depth cuts. So we need1707 * an extra "next" pointer to keep going after we reset cur->delta.1708 */1709 for (cur = entry; cur; cur = next) {1710 next = DELTA(cur);17111712 /*1713 * We should have a chain of zero or more ACTIVE states down to1714 * a final DONE. We can quit after the DONE, because either it1715 * has no bases, or we've already handled them in a previous1716 * call.1717 */1718 if (cur->dfs_state == DFS_DONE)1719 break;1720 else if (cur->dfs_state != DFS_ACTIVE)1721 die("BUG: confusing delta dfs state in second pass: %d",1722 cur->dfs_state);17231724 /*1725 * If the total_depth is more than depth, then we need to snip1726 * the chain into two or more smaller chains that don't exceed1727 * the maximum depth. Most of the resulting chains will contain1728 * (depth + 1) entries (i.e., depth deltas plus one base), and1729 * the last chain (i.e., the one containing entry) will contain1730 * whatever entries are left over, namely1731 * (total_depth % (depth + 1)) of them.1732 *1733 * Since we are iterating towards decreasing depth, we need to1734 * decrement total_depth as we go, and we need to write to the1735 * entry what its final depth will be after all of the1736 * snipping. Since we're snipping into chains of length (depth1737 * + 1) entries, the final depth of an entry will be its1738 * original depth modulo (depth + 1). Any time we encounter an1739 * entry whose final depth is supposed to be zero, we snip it1740 * from its delta base, thereby making it so.1741 */1742 cur->depth = (total_depth--) % (depth + 1);1743 if (!cur->depth)1744 drop_reused_delta(cur);17451746 cur->dfs_state = DFS_DONE;1747 }1748}17491750static void get_object_details(void)1751{1752 uint32_t i;1753 struct object_entry **sorted_by_offset;17541755 sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));1756 for (i = 0; i < to_pack.nr_objects; i++)1757 sorted_by_offset[i] = to_pack.objects + i;1758 QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17591760 for (i = 0; i < to_pack.nr_objects; i++) {1761 struct object_entry *entry = sorted_by_offset[i];1762 check_object(entry);1763 if (entry->type_valid &&1764 oe_size_greater_than(&to_pack, entry, big_file_threshold))1765 entry->no_try_delta = 1;1766 }17671768 /*1769 * This must happen in a second pass, since we rely on the delta1770 * information for the whole list being completed.1771 */1772 for (i = 0; i < to_pack.nr_objects; i++)1773 break_delta_chains(&to_pack.objects[i]);17741775 free(sorted_by_offset);1776}17771778/*1779 * We search for deltas in a list sorted by type, by filename hash, and then1780 * by size, so that we see progressively smaller and smaller files.1781 * That's because we prefer deltas to be from the bigger file1782 * to the smaller -- deletes are potentially cheaper, but perhaps1783 * more importantly, the bigger file is likely the more recent1784 * one. The deepest deltas are therefore the oldest objects which are1785 * less susceptible to be accessed often.1786 */1787static int type_size_sort(const void *_a, const void *_b)1788{1789 const struct object_entry *a = *(struct object_entry **)_a;1790 const struct object_entry *b = *(struct object_entry **)_b;1791 enum object_type a_type = oe_type(a);1792 enum object_type b_type = oe_type(b);1793 unsigned long a_size = SIZE(a);1794 unsigned long b_size = SIZE(b);17951796 if (a_type > b_type)1797 return -1;1798 if (a_type < b_type)1799 return 1;1800 if (a->hash > b->hash)1801 return -1;1802 if (a->hash < b->hash)1803 return 1;1804 if (a->preferred_base > b->preferred_base)1805 return -1;1806 if (a->preferred_base < b->preferred_base)1807 return 1;1808 if (a_size > b_size)1809 return -1;1810 if (a_size < b_size)1811 return 1;1812 return a < b ? -1 : (a > b); /* newest first */1813}18141815struct unpacked {1816 struct object_entry *entry;1817 void *data;1818 struct delta_index *index;1819 unsigned depth;1820};18211822static int delta_cacheable(unsigned long src_size, unsigned long trg_size,1823 unsigned long delta_size)1824{1825 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1826 return 0;18271828 if (delta_size < cache_max_small_delta_size)1829 return 1;18301831 /* cache delta, if objects are large enough compared to delta size */1832 if ((src_size >> 20) + (trg_size >> 21) > (delta_size >> 10))1833 return 1;18341835 return 0;1836}18371838#ifndef NO_PTHREADS18391840static pthread_mutex_t read_mutex;1841#define read_lock() pthread_mutex_lock(&read_mutex)1842#define read_unlock() pthread_mutex_unlock(&read_mutex)18431844static pthread_mutex_t cache_mutex;1845#define cache_lock() pthread_mutex_lock(&cache_mutex)1846#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18471848static pthread_mutex_t progress_mutex;1849#define progress_lock() pthread_mutex_lock(&progress_mutex)1850#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18511852#else18531854#define read_lock() (void)01855#define read_unlock() (void)01856#define cache_lock() (void)01857#define cache_unlock() (void)01858#define progress_lock() (void)01859#define progress_unlock() (void)018601861#endif18621863/*1864 * Return the size of the object without doing any delta1865 * reconstruction (so non-deltas are true object sizes, but deltas1866 * return the size of the delta data).1867 */1868unsigned long oe_get_size_slow(struct packing_data *pack,1869 const struct object_entry *e)1870{1871 struct packed_git *p;1872 struct pack_window *w_curs;1873 unsigned char *buf;1874 enum object_type type;1875 unsigned long used, avail, size;18761877 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {1878 read_lock();1879 if (oid_object_info(&e->idx.oid, &size) < 0)1880 die(_("unable to get size of %s"),1881 oid_to_hex(&e->idx.oid));1882 read_unlock();1883 return size;1884 }18851886 p = oe_in_pack(pack, e);1887 if (!p)1888 BUG("when e->type is a delta, it must belong to a pack");18891890 read_lock();1891 w_curs = NULL;1892 buf = use_pack(p, &w_curs, e->in_pack_offset, &avail);1893 used = unpack_object_header_buffer(buf, avail, &type, &size);1894 if (used == 0)1895 die(_("unable to parse object header of %s"),1896 oid_to_hex(&e->idx.oid));18971898 unuse_pack(&w_curs);1899 read_unlock();1900 return size;1901}19021903static int try_delta(struct unpacked *trg, struct unpacked *src,1904 unsigned max_depth, unsigned long *mem_usage)1905{1906 struct object_entry *trg_entry = trg->entry;1907 struct object_entry *src_entry = src->entry;1908 unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1909 unsigned ref_depth;1910 enum object_type type;1911 void *delta_buf;19121913 /* Don't bother doing diffs between different types */1914 if (oe_type(trg_entry) != oe_type(src_entry))1915 return -1;19161917 /*1918 * We do not bother to try a delta that we discarded on an1919 * earlier try, but only when reusing delta data. Note that1920 * src_entry that is marked as the preferred_base should always1921 * be considered, as even if we produce a suboptimal delta against1922 * it, we will still save the transfer cost, as we already know1923 * the other side has it and we won't send src_entry at all.1924 */1925 if (reuse_delta && IN_PACK(trg_entry) &&1926 IN_PACK(trg_entry) == IN_PACK(src_entry) &&1927 !src_entry->preferred_base &&1928 trg_entry->in_pack_type != OBJ_REF_DELTA &&1929 trg_entry->in_pack_type != OBJ_OFS_DELTA)1930 return 0;19311932 /* Let's not bust the allowed depth. */1933 if (src->depth >= max_depth)1934 return 0;19351936 /* Now some size filtering heuristics. */1937 trg_size = SIZE(trg_entry);1938 if (!DELTA(trg_entry)) {1939 max_size = trg_size/2 - 20;1940 ref_depth = 1;1941 } else {1942 max_size = DELTA_SIZE(trg_entry);1943 ref_depth = trg->depth;1944 }1945 max_size = (uint64_t)max_size * (max_depth - src->depth) /1946 (max_depth - ref_depth + 1);1947 if (max_size == 0)1948 return 0;1949 src_size = SIZE(src_entry);1950 sizediff = src_size < trg_size ? trg_size - src_size : 0;1951 if (sizediff >= max_size)1952 return 0;1953 if (trg_size < src_size / 32)1954 return 0;19551956 /* Load data if not already done */1957 if (!trg->data) {1958 read_lock();1959 trg->data = read_object_file(&trg_entry->idx.oid, &type, &sz);1960 read_unlock();1961 if (!trg->data)1962 die("object %s cannot be read",1963 oid_to_hex(&trg_entry->idx.oid));1964 if (sz != trg_size)1965 die("object %s inconsistent object length (%lu vs %lu)",1966 oid_to_hex(&trg_entry->idx.oid), sz,1967 trg_size);1968 *mem_usage += sz;1969 }1970 if (!src->data) {1971 read_lock();1972 src->data = read_object_file(&src_entry->idx.oid, &type, &sz);1973 read_unlock();1974 if (!src->data) {1975 if (src_entry->preferred_base) {1976 static int warned = 0;1977 if (!warned++)1978 warning("object %s cannot be read",1979 oid_to_hex(&src_entry->idx.oid));1980 /*1981 * Those objects are not included in the1982 * resulting pack. Be resilient and ignore1983 * them if they can't be read, in case the1984 * pack could be created nevertheless.1985 */1986 return 0;1987 }1988 die("object %s cannot be read",1989 oid_to_hex(&src_entry->idx.oid));1990 }1991 if (sz != src_size)1992 die("object %s inconsistent object length (%lu vs %lu)",1993 oid_to_hex(&src_entry->idx.oid), sz,1994 src_size);1995 *mem_usage += sz;1996 }1997 if (!src->index) {1998 src->index = create_delta_index(src->data, src_size);1999 if (!src->index) {2000 static int warned = 0;2001 if (!warned++)2002 warning("suboptimal pack - out of memory");2003 return 0;2004 }2005 *mem_usage += sizeof_delta_index(src->index);2006 }20072008 delta_buf = create_delta(src->index, trg->data, trg_size, &delta_size, max_size);2009 if (!delta_buf)2010 return 0;2011 if (delta_size >= (1U << OE_DELTA_SIZE_BITS)) {2012 free(delta_buf);2013 return 0;2014 }20152016 if (DELTA(trg_entry)) {2017 /* Prefer only shallower same-sized deltas. */2018 if (delta_size == DELTA_SIZE(trg_entry) &&2019 src->depth + 1 >= trg->depth) {2020 free(delta_buf);2021 return 0;2022 }2023 }20242025 /*2026 * Handle memory allocation outside of the cache2027 * accounting lock. Compiler will optimize the strangeness2028 * away when NO_PTHREADS is defined.2029 */2030 free(trg_entry->delta_data);2031 cache_lock();2032 if (trg_entry->delta_data) {2033 delta_cache_size -= DELTA_SIZE(trg_entry);2034 trg_entry->delta_data = NULL;2035 }2036 if (delta_cacheable(src_size, trg_size, delta_size)) {2037 delta_cache_size += delta_size;2038 cache_unlock();2039 trg_entry->delta_data = xrealloc(delta_buf, delta_size);2040 } else {2041 cache_unlock();2042 free(delta_buf);2043 }20442045 SET_DELTA(trg_entry, src_entry);2046 SET_DELTA_SIZE(trg_entry, delta_size);2047 trg->depth = src->depth + 1;20482049 return 1;2050}20512052static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)2053{2054 struct object_entry *child = DELTA_CHILD(me);2055 unsigned int m = n;2056 while (child) {2057 unsigned int c = check_delta_limit(child, n + 1);2058 if (m < c)2059 m = c;2060 child = DELTA_SIBLING(child);2061 }2062 return m;2063}20642065static unsigned long free_unpacked(struct unpacked *n)2066{2067 unsigned long freed_mem = sizeof_delta_index(n->index);2068 free_delta_index(n->index);2069 n->index = NULL;2070 if (n->data) {2071 freed_mem += SIZE(n->entry);2072 FREE_AND_NULL(n->data);2073 }2074 n->entry = NULL;2075 n->depth = 0;2076 return freed_mem;2077}20782079static void find_deltas(struct object_entry **list, unsigned *list_size,2080 int window, int depth, unsigned *processed)2081{2082 uint32_t i, idx = 0, count = 0;2083 struct unpacked *array;2084 unsigned long mem_usage = 0;20852086 array = xcalloc(window, sizeof(struct unpacked));20872088 for (;;) {2089 struct object_entry *entry;2090 struct unpacked *n = array + idx;2091 int j, max_depth, best_base = -1;20922093 progress_lock();2094 if (!*list_size) {2095 progress_unlock();2096 break;2097 }2098 entry = *list++;2099 (*list_size)--;2100 if (!entry->preferred_base) {2101 (*processed)++;2102 display_progress(progress_state, *processed);2103 }2104 progress_unlock();21052106 mem_usage -= free_unpacked(n);2107 n->entry = entry;21082109 while (window_memory_limit &&2110 mem_usage > window_memory_limit &&2111 count > 1) {2112 uint32_t tail = (idx + window - count) % window;2113 mem_usage -= free_unpacked(array + tail);2114 count--;2115 }21162117 /* We do not compute delta to *create* objects we are not2118 * going to pack.2119 */2120 if (entry->preferred_base)2121 goto next;21222123 /*2124 * If the current object is at pack edge, take the depth the2125 * objects that depend on the current object into account2126 * otherwise they would become too deep.2127 */2128 max_depth = depth;2129 if (DELTA_CHILD(entry)) {2130 max_depth -= check_delta_limit(entry, 0);2131 if (max_depth <= 0)2132 goto next;2133 }21342135 j = window;2136 while (--j > 0) {2137 int ret;2138 uint32_t other_idx = idx + j;2139 struct unpacked *m;2140 if (other_idx >= window)2141 other_idx -= window;2142 m = array + other_idx;2143 if (!m->entry)2144 break;2145 ret = try_delta(n, m, max_depth, &mem_usage);2146 if (ret < 0)2147 break;2148 else if (ret > 0)2149 best_base = other_idx;2150 }21512152 /*2153 * If we decided to cache the delta data, then it is best2154 * to compress it right away. First because we have to do2155 * it anyway, and doing it here while we're threaded will2156 * save a lot of time in the non threaded write phase,2157 * as well as allow for caching more deltas within2158 * the same cache size limit.2159 * ...2160 * But only if not writing to stdout, since in that case2161 * the network is most likely throttling writes anyway,2162 * and therefore it is best to go to the write phase ASAP2163 * instead, as we can afford spending more time compressing2164 * between writes at that moment.2165 */2166 if (entry->delta_data && !pack_to_stdout) {2167 unsigned long size;21682169 size = do_compress(&entry->delta_data, DELTA_SIZE(entry));2170 if (size < (1U << OE_Z_DELTA_BITS)) {2171 entry->z_delta_size = size;2172 cache_lock();2173 delta_cache_size -= DELTA_SIZE(entry);2174 delta_cache_size += entry->z_delta_size;2175 cache_unlock();2176 } else {2177 FREE_AND_NULL(entry->delta_data);2178 entry->z_delta_size = 0;2179 }2180 }21812182 /* if we made n a delta, and if n is already at max2183 * depth, leaving it in the window is pointless. we2184 * should evict it first.2185 */2186 if (DELTA(entry) && max_depth <= n->depth)2187 continue;21882189 /*2190 * Move the best delta base up in the window, after the2191 * currently deltified object, to keep it longer. It will2192 * be the first base object to be attempted next.2193 */2194 if (DELTA(entry)) {2195 struct unpacked swap = array[best_base];2196 int dist = (window + idx - best_base) % window;2197 int dst = best_base;2198 while (dist--) {2199 int src = (dst + 1) % window;2200 array[dst] = array[src];2201 dst = src;2202 }2203 array[dst] = swap;2204 }22052206 next:2207 idx++;2208 if (count + 1 < window)2209 count++;2210 if (idx >= window)2211 idx = 0;2212 }22132214 for (i = 0; i < window; ++i) {2215 free_delta_index(array[i].index);2216 free(array[i].data);2217 }2218 free(array);2219}22202221#ifndef NO_PTHREADS22222223static void try_to_free_from_threads(size_t size)2224{2225 read_lock();2226 release_pack_memory(size);2227 read_unlock();2228}22292230static try_to_free_t old_try_to_free_routine;22312232/*2233 * The main thread waits on the condition that (at least) one of the workers2234 * has stopped working (which is indicated in the .working member of2235 * struct thread_params).2236 * When a work thread has completed its work, it sets .working to 0 and2237 * signals the main thread and waits on the condition that .data_ready2238 * becomes 1.2239 */22402241struct thread_params {2242 pthread_t thread;2243 struct object_entry **list;2244 unsigned list_size;2245 unsigned remaining;2246 int window;2247 int depth;2248 int working;2249 int data_ready;2250 pthread_mutex_t mutex;2251 pthread_cond_t cond;2252 unsigned *processed;2253};22542255static pthread_cond_t progress_cond;22562257/*2258 * Mutex and conditional variable can't be statically-initialized on Windows.2259 */2260static void init_threaded_search(void)2261{2262 init_recursive_mutex(&read_mutex);2263 pthread_mutex_init(&cache_mutex, NULL);2264 pthread_mutex_init(&progress_mutex, NULL);2265 pthread_cond_init(&progress_cond, NULL);2266 old_try_to_free_routine = set_try_to_free_routine(try_to_free_from_threads);2267}22682269static void cleanup_threaded_search(void)2270{2271 set_try_to_free_routine(old_try_to_free_routine);2272 pthread_cond_destroy(&progress_cond);2273 pthread_mutex_destroy(&read_mutex);2274 pthread_mutex_destroy(&cache_mutex);2275 pthread_mutex_destroy(&progress_mutex);2276}22772278static void *threaded_find_deltas(void *arg)2279{2280 struct thread_params *me = arg;22812282 progress_lock();2283 while (me->remaining) {2284 progress_unlock();22852286 find_deltas(me->list, &me->remaining,2287 me->window, me->depth, me->processed);22882289 progress_lock();2290 me->working = 0;2291 pthread_cond_signal(&progress_cond);2292 progress_unlock();22932294 /*2295 * We must not set ->data_ready before we wait on the2296 * condition because the main thread may have set it to 12297 * before we get here. In order to be sure that new2298 * work is available if we see 1 in ->data_ready, it2299 * was initialized to 0 before this thread was spawned2300 * and we reset it to 0 right away.2301 */2302 pthread_mutex_lock(&me->mutex);2303 while (!me->data_ready)2304 pthread_cond_wait(&me->cond, &me->mutex);2305 me->data_ready = 0;2306 pthread_mutex_unlock(&me->mutex);23072308 progress_lock();2309 }2310 progress_unlock();2311 /* leave ->working 1 so that this doesn't get more work assigned */2312 return NULL;2313}23142315static void ll_find_deltas(struct object_entry **list, unsigned list_size,2316 int window, int depth, unsigned *processed)2317{2318 struct thread_params *p;2319 int i, ret, active_threads = 0;23202321 init_threaded_search();23222323 if (delta_search_threads <= 1) {2324 find_deltas(list, &list_size, window, depth, processed);2325 cleanup_threaded_search();2326 return;2327 }2328 if (progress > pack_to_stdout)2329 fprintf(stderr, "Delta compression using up to %d threads.\n",2330 delta_search_threads);2331 p = xcalloc(delta_search_threads, sizeof(*p));23322333 /* Partition the work amongst work threads. */2334 for (i = 0; i < delta_search_threads; i++) {2335 unsigned sub_size = list_size / (delta_search_threads - i);23362337 /* don't use too small segments or no deltas will be found */2338 if (sub_size < 2*window && i+1 < delta_search_threads)2339 sub_size = 0;23402341 p[i].window = window;2342 p[i].depth = depth;2343 p[i].processed = processed;2344 p[i].working = 1;2345 p[i].data_ready = 0;23462347 /* try to split chunks on "path" boundaries */2348 while (sub_size && sub_size < list_size &&2349 list[sub_size]->hash &&2350 list[sub_size]->hash == list[sub_size-1]->hash)2351 sub_size++;23522353 p[i].list = list;2354 p[i].list_size = sub_size;2355 p[i].remaining = sub_size;23562357 list += sub_size;2358 list_size -= sub_size;2359 }23602361 /* Start work threads. */2362 for (i = 0; i < delta_search_threads; i++) {2363 if (!p[i].list_size)2364 continue;2365 pthread_mutex_init(&p[i].mutex, NULL);2366 pthread_cond_init(&p[i].cond, NULL);2367 ret = pthread_create(&p[i].thread, NULL,2368 threaded_find_deltas, &p[i]);2369 if (ret)2370 die("unable to create thread: %s", strerror(ret));2371 active_threads++;2372 }23732374 /*2375 * Now let's wait for work completion. Each time a thread is done2376 * with its work, we steal half of the remaining work from the2377 * thread with the largest number of unprocessed objects and give2378 * it to that newly idle thread. This ensure good load balancing2379 * until the remaining object list segments are simply too short2380 * to be worth splitting anymore.2381 */2382 while (active_threads) {2383 struct thread_params *target = NULL;2384 struct thread_params *victim = NULL;2385 unsigned sub_size = 0;23862387 progress_lock();2388 for (;;) {2389 for (i = 0; !target && i < delta_search_threads; i++)2390 if (!p[i].working)2391 target = &p[i];2392 if (target)2393 break;2394 pthread_cond_wait(&progress_cond, &progress_mutex);2395 }23962397 for (i = 0; i < delta_search_threads; i++)2398 if (p[i].remaining > 2*window &&2399 (!victim || victim->remaining < p[i].remaining))2400 victim = &p[i];2401 if (victim) {2402 sub_size = victim->remaining / 2;2403 list = victim->list + victim->list_size - sub_size;2404 while (sub_size && list[0]->hash &&2405 list[0]->hash == list[-1]->hash) {2406 list++;2407 sub_size--;2408 }2409 if (!sub_size) {2410 /*2411 * It is possible for some "paths" to have2412 * so many objects that no hash boundary2413 * might be found. Let's just steal the2414 * exact half in that case.2415 */2416 sub_size = victim->remaining / 2;2417 list -= sub_size;2418 }2419 target->list = list;2420 victim->list_size -= sub_size;2421 victim->remaining -= sub_size;2422 }2423 target->list_size = sub_size;2424 target->remaining = sub_size;2425 target->working = 1;2426 progress_unlock();24272428 pthread_mutex_lock(&target->mutex);2429 target->data_ready = 1;2430 pthread_cond_signal(&target->cond);2431 pthread_mutex_unlock(&target->mutex);24322433 if (!sub_size) {2434 pthread_join(target->thread, NULL);2435 pthread_cond_destroy(&target->cond);2436 pthread_mutex_destroy(&target->mutex);2437 active_threads--;2438 }2439 }2440 cleanup_threaded_search();2441 free(p);2442}24432444#else2445#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2446#endif24472448static void add_tag_chain(const struct object_id *oid)2449{2450 struct tag *tag;24512452 /*2453 * We catch duplicates already in add_object_entry(), but we'd2454 * prefer to do this extra check to avoid having to parse the2455 * tag at all if we already know that it's being packed (e.g., if2456 * it was included via bitmaps, we would not have parsed it2457 * previously).2458 */2459 if (packlist_find(&to_pack, oid->hash, NULL))2460 return;24612462 tag = lookup_tag(oid);2463 while (1) {2464 if (!tag || parse_tag(tag) || !tag->tagged)2465 die("unable to pack objects reachable from tag %s",2466 oid_to_hex(oid));24672468 add_object_entry(&tag->object.oid, OBJ_TAG, NULL, 0);24692470 if (tag->tagged->type != OBJ_TAG)2471 return;24722473 tag = (struct tag *)tag->tagged;2474 }2475}24762477static int add_ref_tag(const char *path, const struct object_id *oid, int flag, void *cb_data)2478{2479 struct object_id peeled;24802481 if (starts_with(path, "refs/tags/") && /* is a tag? */2482 !peel_ref(path, &peeled) && /* peelable? */2483 packlist_find(&to_pack, peeled.hash, NULL)) /* object packed? */2484 add_tag_chain(oid);2485 return 0;2486}24872488static void prepare_pack(int window, int depth)2489{2490 struct object_entry **delta_list;2491 uint32_t i, nr_deltas;2492 unsigned n;24932494 get_object_details();24952496 /*2497 * If we're locally repacking then we need to be doubly careful2498 * from now on in order to make sure no stealth corruption gets2499 * propagated to the new pack. Clients receiving streamed packs2500 * should validate everything they get anyway so no need to incur2501 * the additional cost here in that case.2502 */2503 if (!pack_to_stdout)2504 do_check_packed_object_crc = 1;25052506 if (!to_pack.nr_objects || !window || !depth)2507 return;25082509 ALLOC_ARRAY(delta_list, to_pack.nr_objects);2510 nr_deltas = n = 0;25112512 for (i = 0; i < to_pack.nr_objects; i++) {2513 struct object_entry *entry = to_pack.objects + i;25142515 if (DELTA(entry))2516 /* This happens if we decided to reuse existing2517 * delta from a pack. "reuse_delta &&" is implied.2518 */2519 continue;25202521 if (!entry->type_valid ||2522 oe_size_less_than(&to_pack, entry, 50))2523 continue;25242525 if (entry->no_try_delta)2526 continue;25272528 if (!entry->preferred_base) {2529 nr_deltas++;2530 if (oe_type(entry) < 0)2531 die("unable to get type of object %s",2532 oid_to_hex(&entry->idx.oid));2533 } else {2534 if (oe_type(entry) < 0) {2535 /*2536 * This object is not found, but we2537 * don't have to include it anyway.2538 */2539 continue;2540 }2541 }25422543 delta_list[n++] = entry;2544 }25452546 if (nr_deltas && n > 1) {2547 unsigned nr_done = 0;2548 if (progress)2549 progress_state = start_progress(_("Compressing objects"),2550 nr_deltas);2551 QSORT(delta_list, n, type_size_sort);2552 ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2553 stop_progress(&progress_state);2554 if (nr_done != nr_deltas)2555 die("inconsistency with delta count");2556 }2557 free(delta_list);2558}25592560static int git_pack_config(const char *k, const char *v, void *cb)2561{2562 if (!strcmp(k, "pack.window")) {2563 window = git_config_int(k, v);2564 return 0;2565 }2566 if (!strcmp(k, "pack.windowmemory")) {2567 window_memory_limit = git_config_ulong(k, v);2568 return 0;2569 }2570 if (!strcmp(k, "pack.depth")) {2571 depth = git_config_int(k, v);2572 return 0;2573 }2574 if (!strcmp(k, "pack.deltacachesize")) {2575 max_delta_cache_size = git_config_int(k, v);2576 return 0;2577 }2578 if (!strcmp(k, "pack.deltacachelimit")) {2579 cache_max_small_delta_size = git_config_int(k, v);2580 return 0;2581 }2582 if (!strcmp(k, "pack.writebitmaphashcache")) {2583 if (git_config_bool(k, v))2584 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2585 else2586 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2587 }2588 if (!strcmp(k, "pack.usebitmaps")) {2589 use_bitmap_index_default = git_config_bool(k, v);2590 return 0;2591 }2592 if (!strcmp(k, "pack.threads")) {2593 delta_search_threads = git_config_int(k, v);2594 if (delta_search_threads < 0)2595 die("invalid number of threads specified (%d)",2596 delta_search_threads);2597#ifdef NO_PTHREADS2598 if (delta_search_threads != 1) {2599 warning("no threads support, ignoring %s", k);2600 delta_search_threads = 0;2601 }2602#endif2603 return 0;2604 }2605 if (!strcmp(k, "pack.indexversion")) {2606 pack_idx_opts.version = git_config_int(k, v);2607 if (pack_idx_opts.version > 2)2608 die("bad pack.indexversion=%"PRIu32,2609 pack_idx_opts.version);2610 return 0;2611 }2612 return git_default_config(k, v, cb);2613}26142615static void read_object_list_from_stdin(void)2616{2617 char line[GIT_MAX_HEXSZ + 1 + PATH_MAX + 2];2618 struct object_id oid;2619 const char *p;26202621 for (;;) {2622 if (!fgets(line, sizeof(line), stdin)) {2623 if (feof(stdin))2624 break;2625 if (!ferror(stdin))2626 die("fgets returned NULL, not EOF, not error!");2627 if (errno != EINTR)2628 die_errno("fgets");2629 clearerr(stdin);2630 continue;2631 }2632 if (line[0] == '-') {2633 if (get_oid_hex(line+1, &oid))2634 die("expected edge object ID, got garbage:\n %s",2635 line);2636 add_preferred_base(&oid);2637 continue;2638 }2639 if (parse_oid_hex(line, &oid, &p))2640 die("expected object ID, got garbage:\n %s", line);26412642 add_preferred_base_object(p + 1);2643 add_object_entry(&oid, OBJ_NONE, p + 1, 0);2644 }2645}26462647/* Remember to update object flag allocation in object.h */2648#define OBJECT_ADDED (1u<<20)26492650static void show_commit(struct commit *commit, void *data)2651{2652 add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL, 0);2653 commit->object.flags |= OBJECT_ADDED;26542655 if (write_bitmap_index)2656 index_commit_for_bitmap(commit);2657}26582659static void show_object(struct object *obj, const char *name, void *data)2660{2661 add_preferred_base_object(name);2662 add_object_entry(&obj->oid, obj->type, name, 0);2663 obj->flags |= OBJECT_ADDED;2664}26652666static void show_object__ma_allow_any(struct object *obj, const char *name, void *data)2667{2668 assert(arg_missing_action == MA_ALLOW_ANY);26692670 /*2671 * Quietly ignore ALL missing objects. This avoids problems with2672 * staging them now and getting an odd error later.2673 */2674 if (!has_object_file(&obj->oid))2675 return;26762677 show_object(obj, name, data);2678}26792680static void show_object__ma_allow_promisor(struct object *obj, const char *name, void *data)2681{2682 assert(arg_missing_action == MA_ALLOW_PROMISOR);26832684 /*2685 * Quietly ignore EXPECTED missing objects. This avoids problems with2686 * staging them now and getting an odd error later.2687 */2688 if (!has_object_file(&obj->oid) && is_promisor_object(&obj->oid))2689 return;26902691 show_object(obj, name, data);2692}26932694static int option_parse_missing_action(const struct option *opt,2695 const char *arg, int unset)2696{2697 assert(arg);2698 assert(!unset);26992700 if (!strcmp(arg, "error")) {2701 arg_missing_action = MA_ERROR;2702 fn_show_object = show_object;2703 return 0;2704 }27052706 if (!strcmp(arg, "allow-any")) {2707 arg_missing_action = MA_ALLOW_ANY;2708 fetch_if_missing = 0;2709 fn_show_object = show_object__ma_allow_any;2710 return 0;2711 }27122713 if (!strcmp(arg, "allow-promisor")) {2714 arg_missing_action = MA_ALLOW_PROMISOR;2715 fetch_if_missing = 0;2716 fn_show_object = show_object__ma_allow_promisor;2717 return 0;2718 }27192720 die(_("invalid value for --missing"));2721 return 0;2722}27232724static void show_edge(struct commit *commit)2725{2726 add_preferred_base(&commit->object.oid);2727}27282729struct in_pack_object {2730 off_t offset;2731 struct object *object;2732};27332734struct in_pack {2735 unsigned int alloc;2736 unsigned int nr;2737 struct in_pack_object *array;2738};27392740static void mark_in_pack_object(struct object *object, struct packed_git *p, struct in_pack *in_pack)2741{2742 in_pack->array[in_pack->nr].offset = find_pack_entry_one(object->oid.hash, p);2743 in_pack->array[in_pack->nr].object = object;2744 in_pack->nr++;2745}27462747/*2748 * Compare the objects in the offset order, in order to emulate the2749 * "git rev-list --objects" output that produced the pack originally.2750 */2751static int ofscmp(const void *a_, const void *b_)2752{2753 struct in_pack_object *a = (struct in_pack_object *)a_;2754 struct in_pack_object *b = (struct in_pack_object *)b_;27552756 if (a->offset < b->offset)2757 return -1;2758 else if (a->offset > b->offset)2759 return 1;2760 else2761 return oidcmp(&a->object->oid, &b->object->oid);2762}27632764static void add_objects_in_unpacked_packs(struct rev_info *revs)2765{2766 struct packed_git *p;2767 struct in_pack in_pack;2768 uint32_t i;27692770 memset(&in_pack, 0, sizeof(in_pack));27712772 for (p = get_packed_git(the_repository); p; p = p->next) {2773 struct object_id oid;2774 struct object *o;27752776 if (!p->pack_local || p->pack_keep)2777 continue;2778 if (open_pack_index(p))2779 die("cannot open pack index");27802781 ALLOC_GROW(in_pack.array,2782 in_pack.nr + p->num_objects,2783 in_pack.alloc);27842785 for (i = 0; i < p->num_objects; i++) {2786 nth_packed_object_oid(&oid, p, i);2787 o = lookup_unknown_object(oid.hash);2788 if (!(o->flags & OBJECT_ADDED))2789 mark_in_pack_object(o, p, &in_pack);2790 o->flags |= OBJECT_ADDED;2791 }2792 }27932794 if (in_pack.nr) {2795 QSORT(in_pack.array, in_pack.nr, ofscmp);2796 for (i = 0; i < in_pack.nr; i++) {2797 struct object *o = in_pack.array[i].object;2798 add_object_entry(&o->oid, o->type, "", 0);2799 }2800 }2801 free(in_pack.array);2802}28032804static int add_loose_object(const struct object_id *oid, const char *path,2805 void *data)2806{2807 enum object_type type = oid_object_info(oid, NULL);28082809 if (type < 0) {2810 warning("loose object at %s could not be examined", path);2811 return 0;2812 }28132814 add_object_entry(oid, type, "", 0);2815 return 0;2816}28172818/*2819 * We actually don't even have to worry about reachability here.2820 * add_object_entry will weed out duplicates, so we just add every2821 * loose object we find.2822 */2823static void add_unreachable_loose_objects(void)2824{2825 for_each_loose_file_in_objdir(get_object_directory(),2826 add_loose_object,2827 NULL, NULL, NULL);2828}28292830static int has_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2831{2832 static struct packed_git *last_found = (void *)1;2833 struct packed_git *p;28342835 p = (last_found != (void *)1) ? last_found :2836 get_packed_git(the_repository);28372838 while (p) {2839 if ((!p->pack_local || p->pack_keep) &&2840 find_pack_entry_one(oid->hash, p)) {2841 last_found = p;2842 return 1;2843 }2844 if (p == last_found)2845 p = get_packed_git(the_repository);2846 else2847 p = p->next;2848 if (p == last_found)2849 p = p->next;2850 }2851 return 0;2852}28532854/*2855 * Store a list of sha1s that are should not be discarded2856 * because they are either written too recently, or are2857 * reachable from another object that was.2858 *2859 * This is filled by get_object_list.2860 */2861static struct oid_array recent_objects;28622863static int loosened_object_can_be_discarded(const struct object_id *oid,2864 timestamp_t mtime)2865{2866 if (!unpack_unreachable_expiration)2867 return 0;2868 if (mtime > unpack_unreachable_expiration)2869 return 0;2870 if (oid_array_lookup(&recent_objects, oid) >= 0)2871 return 0;2872 return 1;2873}28742875static void loosen_unused_packed_objects(struct rev_info *revs)2876{2877 struct packed_git *p;2878 uint32_t i;2879 struct object_id oid;28802881 for (p = get_packed_git(the_repository); p; p = p->next) {2882 if (!p->pack_local || p->pack_keep)2883 continue;28842885 if (open_pack_index(p))2886 die("cannot open pack index");28872888 for (i = 0; i < p->num_objects; i++) {2889 nth_packed_object_oid(&oid, p, i);2890 if (!packlist_find(&to_pack, oid.hash, NULL) &&2891 !has_sha1_pack_kept_or_nonlocal(&oid) &&2892 !loosened_object_can_be_discarded(&oid, p->mtime))2893 if (force_object_loose(&oid, p->mtime))2894 die("unable to force loose object");2895 }2896 }2897}28982899/*2900 * This tracks any options which pack-reuse code expects to be on, or which a2901 * reader of the pack might not understand, and which would therefore prevent2902 * blind reuse of what we have on disk.2903 */2904static int pack_options_allow_reuse(void)2905{2906 return pack_to_stdout &&2907 allow_ofs_delta &&2908 !ignore_packed_keep &&2909 (!local || !have_non_local_packs) &&2910 !incremental;2911}29122913static int get_object_list_from_bitmap(struct rev_info *revs)2914{2915 if (prepare_bitmap_walk(revs) < 0)2916 return -1;29172918 if (pack_options_allow_reuse() &&2919 !reuse_partial_packfile_from_bitmap(2920 &reuse_packfile,2921 &reuse_packfile_objects,2922 &reuse_packfile_offset)) {2923 assert(reuse_packfile_objects);2924 nr_result += reuse_packfile_objects;2925 display_progress(progress_state, nr_result);2926 }29272928 traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2929 return 0;2930}29312932static void record_recent_object(struct object *obj,2933 const char *name,2934 void *data)2935{2936 oid_array_append(&recent_objects, &obj->oid);2937}29382939static void record_recent_commit(struct commit *commit, void *data)2940{2941 oid_array_append(&recent_objects, &commit->object.oid);2942}29432944static void get_object_list(int ac, const char **av)2945{2946 struct rev_info revs;2947 char line[1000];2948 int flags = 0;29492950 init_revisions(&revs, NULL);2951 save_commit_buffer = 0;2952 setup_revisions(ac, av, &revs, NULL);29532954 /* make sure shallows are read */2955 is_repository_shallow();29562957 while (fgets(line, sizeof(line), stdin) != NULL) {2958 int len = strlen(line);2959 if (len && line[len - 1] == '\n')2960 line[--len] = 0;2961 if (!len)2962 break;2963 if (*line == '-') {2964 if (!strcmp(line, "--not")) {2965 flags ^= UNINTERESTING;2966 write_bitmap_index = 0;2967 continue;2968 }2969 if (starts_with(line, "--shallow ")) {2970 struct object_id oid;2971 if (get_oid_hex(line + 10, &oid))2972 die("not an SHA-1 '%s'", line + 10);2973 register_shallow(&oid);2974 use_bitmap_index = 0;2975 continue;2976 }2977 die("not a rev '%s'", line);2978 }2979 if (handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2980 die("bad revision '%s'", line);2981 }29822983 if (use_bitmap_index && !get_object_list_from_bitmap(&revs))2984 return;29852986 if (prepare_revision_walk(&revs))2987 die("revision walk setup failed");2988 mark_edges_uninteresting(&revs, show_edge);29892990 if (!fn_show_object)2991 fn_show_object = show_object;2992 traverse_commit_list_filtered(&filter_options, &revs,2993 show_commit, fn_show_object, NULL,2994 NULL);29952996 if (unpack_unreachable_expiration) {2997 revs.ignore_missing_links = 1;2998 if (add_unseen_recent_objects_to_traversal(&revs,2999 unpack_unreachable_expiration))3000 die("unable to add recent objects");3001 if (prepare_revision_walk(&revs))3002 die("revision walk setup failed");3003 traverse_commit_list(&revs, record_recent_commit,3004 record_recent_object, NULL);3005 }30063007 if (keep_unreachable)3008 add_objects_in_unpacked_packs(&revs);3009 if (pack_loose_unreachable)3010 add_unreachable_loose_objects();3011 if (unpack_unreachable)3012 loosen_unused_packed_objects(&revs);30133014 oid_array_clear(&recent_objects);3015}30163017static int option_parse_index_version(const struct option *opt,3018 const char *arg, int unset)3019{3020 char *c;3021 const char *val = arg;3022 pack_idx_opts.version = strtoul(val, &c, 10);3023 if (pack_idx_opts.version > 2)3024 die(_("unsupported index version %s"), val);3025 if (*c == ',' && c[1])3026 pack_idx_opts.off32_limit = strtoul(c+1, &c, 0);3027 if (*c || pack_idx_opts.off32_limit & 0x80000000)3028 die(_("bad index version '%s'"), val);3029 return 0;3030}30313032static int option_parse_unpack_unreachable(const struct option *opt,3033 const char *arg, int unset)3034{3035 if (unset) {3036 unpack_unreachable = 0;3037 unpack_unreachable_expiration = 0;3038 }3039 else {3040 unpack_unreachable = 1;3041 if (arg)3042 unpack_unreachable_expiration = approxidate(arg);3043 }3044 return 0;3045}30463047int cmd_pack_objects(int argc, const char **argv, const char *prefix)3048{3049 int use_internal_rev_list = 0;3050 int thin = 0;3051 int shallow = 0;3052 int all_progress_implied = 0;3053 struct argv_array rp = ARGV_ARRAY_INIT;3054 int rev_list_unpacked = 0, rev_list_all = 0, rev_list_reflog = 0;3055 int rev_list_index = 0;3056 struct option pack_objects_options[] = {3057 OPT_SET_INT('q', "quiet", &progress,3058 N_("do not show progress meter"), 0),3059 OPT_SET_INT(0, "progress", &progress,3060 N_("show progress meter"), 1),3061 OPT_SET_INT(0, "all-progress", &progress,3062 N_("show progress meter during object writing phase"), 2),3063 OPT_BOOL(0, "all-progress-implied",3064 &all_progress_implied,3065 N_("similar to --all-progress when progress meter is shown")),3066 { OPTION_CALLBACK, 0, "index-version", NULL, N_("version[,offset]"),3067 N_("write the pack index file in the specified idx format version"),3068 0, option_parse_index_version },3069 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit,3070 N_("maximum size of each output pack file")),3071 OPT_BOOL(0, "local", &local,3072 N_("ignore borrowed objects from alternate object store")),3073 OPT_BOOL(0, "incremental", &incremental,3074 N_("ignore packed objects")),3075 OPT_INTEGER(0, "window", &window,3076 N_("limit pack window by objects")),3077 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit,3078 N_("limit pack window by memory in addition to object limit")),3079 OPT_INTEGER(0, "depth", &depth,3080 N_("maximum length of delta chain allowed in the resulting pack")),3081 OPT_BOOL(0, "reuse-delta", &reuse_delta,3082 N_("reuse existing deltas")),3083 OPT_BOOL(0, "reuse-object", &reuse_object,3084 N_("reuse existing objects")),3085 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta,3086 N_("use OFS_DELTA objects")),3087 OPT_INTEGER(0, "threads", &delta_search_threads,3088 N_("use threads when searching for best delta matches")),3089 OPT_BOOL(0, "non-empty", &non_empty,3090 N_("do not create an empty pack output")),3091 OPT_BOOL(0, "revs", &use_internal_rev_list,3092 N_("read revision arguments from standard input")),3093 { OPTION_SET_INT, 0, "unpacked", &rev_list_unpacked, NULL,3094 N_("limit the objects to those that are not yet packed"),3095 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3096 { OPTION_SET_INT, 0, "all", &rev_list_all, NULL,3097 N_("include objects reachable from any reference"),3098 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3099 { OPTION_SET_INT, 0, "reflog", &rev_list_reflog, NULL,3100 N_("include objects referred by reflog entries"),3101 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3102 { OPTION_SET_INT, 0, "indexed-objects", &rev_list_index, NULL,3103 N_("include objects referred to by the index"),3104 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },3105 OPT_BOOL(0, "stdout", &pack_to_stdout,3106 N_("output pack to stdout")),3107 OPT_BOOL(0, "include-tag", &include_tag,3108 N_("include tag objects that refer to objects to be packed")),3109 OPT_BOOL(0, "keep-unreachable", &keep_unreachable,3110 N_("keep unreachable objects")),3111 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable,3112 N_("pack loose unreachable objects")),3113 { OPTION_CALLBACK, 0, "unpack-unreachable", NULL, N_("time"),3114 N_("unpack unreachable objects newer than <time>"),3115 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3116 OPT_BOOL(0, "thin", &thin,3117 N_("create thin packs")),3118 OPT_BOOL(0, "shallow", &shallow,3119 N_("create packs suitable for shallow fetches")),3120 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep,3121 N_("ignore packs that have companion .keep file")),3122 OPT_INTEGER(0, "compression", &pack_compression_level,3123 N_("pack compression level")),3124 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents,3125 N_("do not hide commits by grafts"), 0),3126 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index,3127 N_("use a bitmap index if available to speed up counting objects")),3128 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index,3129 N_("write a bitmap index together with the pack index")),3130 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3131 { OPTION_CALLBACK, 0, "missing", NULL, N_("action"),3132 N_("handling for missing objects"), PARSE_OPT_NONEG,3133 option_parse_missing_action },3134 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects,3135 N_("do not pack objects in promisor packfiles")),3136 OPT_END(),3137 };31383139 if (DFS_NUM_STATES > (1 << OE_DFS_STATE_BITS))3140 BUG("too many dfs states, increase OE_DFS_STATE_BITS");31413142 check_replace_refs = 0;31433144 reset_pack_idx_option(&pack_idx_opts);3145 git_config(git_pack_config, NULL);31463147 progress = isatty(2);3148 argc = parse_options(argc, argv, prefix, pack_objects_options,3149 pack_usage, 0);31503151 if (argc) {3152 base_name = argv[0];3153 argc--;3154 }3155 if (pack_to_stdout != !base_name || argc)3156 usage_with_options(pack_usage, pack_objects_options);31573158 if (depth >= (1 << OE_DEPTH_BITS)) {3159 warning(_("delta chain depth %d is too deep, forcing %d"),3160 depth, (1 << OE_DEPTH_BITS) - 1);3161 depth = (1 << OE_DEPTH_BITS) - 1;3162 }3163 if (cache_max_small_delta_size >= (1U << OE_Z_DELTA_BITS)) {3164 warning(_("pack.deltaCacheLimit is too high, forcing %d"),3165 (1U << OE_Z_DELTA_BITS) - 1);3166 cache_max_small_delta_size = (1U << OE_Z_DELTA_BITS) - 1;3167 }31683169 argv_array_push(&rp, "pack-objects");3170 if (thin) {3171 use_internal_rev_list = 1;3172 argv_array_push(&rp, shallow3173 ? "--objects-edge-aggressive"3174 : "--objects-edge");3175 } else3176 argv_array_push(&rp, "--objects");31773178 if (rev_list_all) {3179 use_internal_rev_list = 1;3180 argv_array_push(&rp, "--all");3181 }3182 if (rev_list_reflog) {3183 use_internal_rev_list = 1;3184 argv_array_push(&rp, "--reflog");3185 }3186 if (rev_list_index) {3187 use_internal_rev_list = 1;3188 argv_array_push(&rp, "--indexed-objects");3189 }3190 if (rev_list_unpacked) {3191 use_internal_rev_list = 1;3192 argv_array_push(&rp, "--unpacked");3193 }31943195 if (exclude_promisor_objects) {3196 use_internal_rev_list = 1;3197 fetch_if_missing = 0;3198 argv_array_push(&rp, "--exclude-promisor-objects");3199 }32003201 if (!reuse_object)3202 reuse_delta = 0;3203 if (pack_compression_level == -1)3204 pack_compression_level = Z_DEFAULT_COMPRESSION;3205 else if (pack_compression_level < 0 || pack_compression_level > Z_BEST_COMPRESSION)3206 die("bad pack compression level %d", pack_compression_level);32073208 if (!delta_search_threads) /* --threads=0 means autodetect */3209 delta_search_threads = online_cpus();32103211#ifdef NO_PTHREADS3212 if (delta_search_threads != 1)3213 warning("no threads support, ignoring --threads");3214#endif3215 if (!pack_to_stdout && !pack_size_limit)3216 pack_size_limit = pack_size_limit_cfg;3217 if (pack_to_stdout && pack_size_limit)3218 die("--max-pack-size cannot be used to build a pack for transfer.");3219 if (pack_size_limit && pack_size_limit < 1024*1024) {3220 warning("minimum pack size limit is 1 MiB");3221 pack_size_limit = 1024*1024;3222 }32233224 if (!pack_to_stdout && thin)3225 die("--thin cannot be used to build an indexable pack.");32263227 if (keep_unreachable && unpack_unreachable)3228 die("--keep-unreachable and --unpack-unreachable are incompatible.");3229 if (!rev_list_all || !rev_list_reflog || !rev_list_index)3230 unpack_unreachable_expiration = 0;32313232 if (filter_options.choice) {3233 if (!pack_to_stdout)3234 die("cannot use --filter without --stdout.");3235 use_bitmap_index = 0;3236 }32373238 /*3239 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3240 *3241 * - to produce good pack (with bitmap index not-yet-packed objects are3242 * packed in suboptimal order).3243 *3244 * - to use more robust pack-generation codepath (avoiding possible3245 * bugs in bitmap code and possible bitmap index corruption).3246 */3247 if (!pack_to_stdout)3248 use_bitmap_index_default = 0;32493250 if (use_bitmap_index < 0)3251 use_bitmap_index = use_bitmap_index_default;32523253 /* "hard" reasons not to use bitmaps; these just won't work at all */3254 if (!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) || is_repository_shallow())3255 use_bitmap_index = 0;32563257 if (pack_to_stdout || !rev_list_all)3258 write_bitmap_index = 0;32593260 if (progress && all_progress_implied)3261 progress = 2;32623263 if (ignore_packed_keep) {3264 struct packed_git *p;3265 for (p = get_packed_git(the_repository); p; p = p->next)3266 if (p->pack_local && p->pack_keep)3267 break;3268 if (!p) /* no keep-able packs found */3269 ignore_packed_keep = 0;3270 }3271 if (local) {3272 /*3273 * unlike ignore_packed_keep above, we do not want to3274 * unset "local" based on looking at packs, as it3275 * also covers non-local objects3276 */3277 struct packed_git *p;3278 for (p = get_packed_git(the_repository); p; p = p->next) {3279 if (!p->pack_local) {3280 have_non_local_packs = 1;3281 break;3282 }3283 }3284 }32853286 prepare_packing_data(&to_pack);32873288 if (progress)3289 progress_state = start_progress(_("Counting objects"), 0);3290 if (!use_internal_rev_list)3291 read_object_list_from_stdin();3292 else {3293 get_object_list(rp.argc, rp.argv);3294 argv_array_clear(&rp);3295 }3296 cleanup_preferred_base();3297 if (include_tag && nr_result)3298 for_each_ref(add_ref_tag, NULL);3299 stop_progress(&progress_state);33003301 if (non_empty && !nr_result)3302 return 0;3303 if (nr_result)3304 prepare_pack(window, depth);3305 write_pack_file();3306 if (progress)3307 fprintf(stderr, "Total %"PRIu32" (delta %"PRIu32"),"3308 " reused %"PRIu32" (delta %"PRIu32")\n",3309 written, written_delta, reused, reused_delta);3310 return 0;3311}