1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "environment.h"
13 #include "pack-revindex.h"
14 #include "csum-file.h"
15 #include "tree-walk.h"
18 #include "list-objects.h"
19 #include "list-objects-filter-options.h"
20 #include "pack-objects.h"
23 #include "streaming.h"
24 #include "thread-utils.h"
25 #include "pack-bitmap.h"
26 #include "delta-islands.h"
27 #include "reachable.h"
28 #include "oid-array.h"
32 #include "object-file.h"
33 #include "object-store-ll.h"
34 #include "replace-object.h"
39 #include "promisor-remote.h"
40 #include "pack-mtimes.h"
41 #include "parse-options.h"
44 * Objects we are going to pack are collected in the `to_pack` structure.
45 * It contains an array (dynamically expanded) of the object data, and a map
46 * that can resolve SHA1s to their position in the array.
48 static struct packing_data to_pack
;
50 static inline struct object_entry
*oe_delta(
51 const struct packing_data
*pack
,
52 const struct object_entry
*e
)
57 return &pack
->ext_bases
[e
->delta_idx
- 1];
59 return &pack
->objects
[e
->delta_idx
- 1];
62 static inline unsigned long oe_delta_size(struct packing_data
*pack
,
63 const struct object_entry
*e
)
65 if (e
->delta_size_valid
)
66 return e
->delta_size_
;
69 * pack->delta_size[] can't be NULL because oe_set_delta_size()
70 * must have been called when a new delta is saved with
72 * If oe_delta() returns NULL (i.e. default state, which means
73 * delta_size_valid is also false), then the caller must never
74 * call oe_delta_size().
76 return pack
->delta_size
[e
- pack
->objects
];
79 unsigned long oe_get_size_slow(struct packing_data
*pack
,
80 const struct object_entry
*e
);
82 static inline unsigned long oe_size(struct packing_data
*pack
,
83 const struct object_entry
*e
)
88 return oe_get_size_slow(pack
, e
);
91 static inline void oe_set_delta(struct packing_data
*pack
,
92 struct object_entry
*e
,
93 struct object_entry
*delta
)
96 e
->delta_idx
= (delta
- pack
->objects
) + 1;
101 static inline struct object_entry
*oe_delta_sibling(
102 const struct packing_data
*pack
,
103 const struct object_entry
*e
)
105 if (e
->delta_sibling_idx
)
106 return &pack
->objects
[e
->delta_sibling_idx
- 1];
110 static inline struct object_entry
*oe_delta_child(
111 const struct packing_data
*pack
,
112 const struct object_entry
*e
)
114 if (e
->delta_child_idx
)
115 return &pack
->objects
[e
->delta_child_idx
- 1];
119 static inline void oe_set_delta_child(struct packing_data
*pack
,
120 struct object_entry
*e
,
121 struct object_entry
*delta
)
124 e
->delta_child_idx
= (delta
- pack
->objects
) + 1;
126 e
->delta_child_idx
= 0;
129 static inline void oe_set_delta_sibling(struct packing_data
*pack
,
130 struct object_entry
*e
,
131 struct object_entry
*delta
)
134 e
->delta_sibling_idx
= (delta
- pack
->objects
) + 1;
136 e
->delta_sibling_idx
= 0;
139 static inline void oe_set_size(struct packing_data
*pack
,
140 struct object_entry
*e
,
143 if (size
< pack
->oe_size_limit
) {
148 if (oe_get_size_slow(pack
, e
) != size
)
149 BUG("'size' is supposed to be the object size!");
153 static inline void oe_set_delta_size(struct packing_data
*pack
,
154 struct object_entry
*e
,
157 if (size
< pack
->oe_delta_size_limit
) {
158 e
->delta_size_
= size
;
159 e
->delta_size_valid
= 1;
161 packing_data_lock(pack
);
162 if (!pack
->delta_size
)
163 ALLOC_ARRAY(pack
->delta_size
, pack
->nr_alloc
);
164 packing_data_unlock(pack
);
166 pack
->delta_size
[e
- pack
->objects
] = size
;
167 e
->delta_size_valid
= 0;
171 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
172 #define SIZE(obj) oe_size(&to_pack, obj)
173 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
174 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
175 #define DELTA(obj) oe_delta(&to_pack, obj)
176 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
177 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
178 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
179 #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
180 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
181 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
182 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
184 static const char *pack_usage
[] = {
185 N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
186 N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
190 static struct pack_idx_entry
**written_list
;
191 static uint32_t nr_result
, nr_written
, nr_seen
;
192 static struct bitmap_index
*bitmap_git
;
193 static uint32_t write_layer
;
195 static int non_empty
;
196 static int reuse_delta
= 1, reuse_object
= 1;
197 static int keep_unreachable
, unpack_unreachable
, include_tag
;
198 static timestamp_t unpack_unreachable_expiration
;
199 static int pack_loose_unreachable
;
201 static timestamp_t cruft_expiration
;
203 static int have_non_local_packs
;
204 static int incremental
;
205 static int ignore_packed_keep_on_disk
;
206 static int ignore_packed_keep_in_core
;
207 static int allow_ofs_delta
;
208 static struct pack_idx_option pack_idx_opts
;
209 static const char *base_name
;
210 static int progress
= 1;
211 static int window
= 10;
212 static unsigned long pack_size_limit
;
213 static int depth
= 50;
214 static int delta_search_threads
;
215 static int pack_to_stdout
;
218 static int num_preferred_base
;
219 static struct progress
*progress_state
;
221 static struct bitmapped_pack
*reuse_packfiles
;
222 static size_t reuse_packfiles_nr
;
223 static size_t reuse_packfiles_used_nr
;
224 static uint32_t reuse_packfile_objects
;
225 static struct bitmap
*reuse_packfile_bitmap
;
227 static int use_bitmap_index_default
= 1;
228 static int use_bitmap_index
= -1;
233 } allow_pack_reuse
= SINGLE_PACK_REUSE
;
235 WRITE_BITMAP_FALSE
= 0,
238 } write_bitmap_index
;
239 static uint16_t write_bitmap_options
= BITMAP_OPT_HASH_CACHE
;
241 static int exclude_promisor_objects
;
243 static int use_delta_islands
;
245 static unsigned long delta_cache_size
= 0;
246 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
247 static unsigned long cache_max_small_delta_size
= 1000;
249 static unsigned long window_memory_limit
= 0;
251 static struct string_list uri_protocols
= STRING_LIST_INIT_NODUP
;
253 enum missing_action
{
254 MA_ERROR
= 0, /* fail if any missing objects are encountered */
255 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
256 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
258 static enum missing_action arg_missing_action
;
259 static show_object_fn fn_show_object
;
261 struct configured_exclusion
{
262 struct oidmap_entry e
;
266 static struct oidmap configured_exclusions
;
268 static struct oidset excluded_by_config
;
273 static uint32_t written
, written_delta
;
274 static uint32_t reused
, reused_delta
;
279 static struct commit
**indexed_commits
;
280 static unsigned int indexed_commits_nr
;
281 static unsigned int indexed_commits_alloc
;
283 static void index_commit_for_bitmap(struct commit
*commit
)
285 if (indexed_commits_nr
>= indexed_commits_alloc
) {
286 indexed_commits_alloc
= (indexed_commits_alloc
+ 32) * 2;
287 REALLOC_ARRAY(indexed_commits
, indexed_commits_alloc
);
290 indexed_commits
[indexed_commits_nr
++] = commit
;
293 static void *get_delta(struct object_entry
*entry
)
295 unsigned long size
, base_size
, delta_size
;
296 void *buf
, *base_buf
, *delta_buf
;
297 enum object_type type
;
299 buf
= repo_read_object_file(the_repository
, &entry
->idx
.oid
, &type
,
302 die(_("unable to read %s"), oid_to_hex(&entry
->idx
.oid
));
303 base_buf
= repo_read_object_file(the_repository
,
304 &DELTA(entry
)->idx
.oid
, &type
,
307 die("unable to read %s",
308 oid_to_hex(&DELTA(entry
)->idx
.oid
));
309 delta_buf
= diff_delta(base_buf
, base_size
,
310 buf
, size
, &delta_size
, 0);
312 * We successfully computed this delta once but dropped it for
313 * memory reasons. Something is very wrong if this time we
314 * recompute and create a different delta.
316 if (!delta_buf
|| delta_size
!= DELTA_SIZE(entry
))
317 BUG("delta size changed");
323 static unsigned long do_compress(void **pptr
, unsigned long size
)
327 unsigned long maxsize
;
329 git_deflate_init(&stream
, pack_compression_level
);
330 maxsize
= git_deflate_bound(&stream
, size
);
333 out
= xmalloc(maxsize
);
337 stream
.avail_in
= size
;
338 stream
.next_out
= out
;
339 stream
.avail_out
= maxsize
;
340 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
342 git_deflate_end(&stream
);
345 return stream
.total_out
;
348 static unsigned long write_large_blob_data(struct git_istream
*st
, struct hashfile
*f
,
349 const struct object_id
*oid
)
352 unsigned char ibuf
[1024 * 16];
353 unsigned char obuf
[1024 * 16];
354 unsigned long olen
= 0;
356 git_deflate_init(&stream
, pack_compression_level
);
361 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
363 die(_("unable to read %s"), oid_to_hex(oid
));
365 stream
.next_in
= ibuf
;
366 stream
.avail_in
= readlen
;
367 while ((stream
.avail_in
|| readlen
== 0) &&
368 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
369 stream
.next_out
= obuf
;
370 stream
.avail_out
= sizeof(obuf
);
371 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
372 hashwrite(f
, obuf
, stream
.next_out
- obuf
);
373 olen
+= stream
.next_out
- obuf
;
376 die(_("deflate error (%d)"), zret
);
378 if (zret
!= Z_STREAM_END
)
379 die(_("deflate error (%d)"), zret
);
383 git_deflate_end(&stream
);
388 * we are going to reuse the existing object data as is. make
389 * sure it is not corrupt.
391 static int check_pack_inflate(struct packed_git
*p
,
392 struct pack_window
**w_curs
,
395 unsigned long expect
)
398 unsigned char fakebuf
[4096], *in
;
401 memset(&stream
, 0, sizeof(stream
));
402 git_inflate_init(&stream
);
404 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
406 stream
.next_out
= fakebuf
;
407 stream
.avail_out
= sizeof(fakebuf
);
408 st
= git_inflate(&stream
, Z_FINISH
);
409 offset
+= stream
.next_in
- in
;
410 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
411 git_inflate_end(&stream
);
412 return (st
== Z_STREAM_END
&&
413 stream
.total_out
== expect
&&
414 stream
.total_in
== len
) ? 0 : -1;
417 static void copy_pack_data(struct hashfile
*f
,
418 struct packed_git
*p
,
419 struct pack_window
**w_curs
,
427 in
= use_pack(p
, w_curs
, offset
, &avail
);
429 avail
= (unsigned long)len
;
430 hashwrite(f
, in
, avail
);
436 static inline int oe_size_greater_than(struct packing_data
*pack
,
437 const struct object_entry
*lhs
,
441 return lhs
->size_
> rhs
;
442 if (rhs
< pack
->oe_size_limit
) /* rhs < 2^x <= lhs ? */
444 return oe_get_size_slow(pack
, lhs
) > rhs
;
447 /* Return 0 if we will bust the pack-size limit */
448 static unsigned long write_no_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
449 unsigned long limit
, int usable_delta
)
451 unsigned long size
, datalen
;
452 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
453 dheader
[MAX_PACK_OBJECT_HEADER
];
455 enum object_type type
;
457 struct git_istream
*st
= NULL
;
458 const unsigned hashsz
= the_hash_algo
->rawsz
;
461 if (oe_type(entry
) == OBJ_BLOB
&&
462 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
) &&
463 (st
= open_istream(the_repository
, &entry
->idx
.oid
, &type
,
464 &size
, NULL
)) != NULL
)
467 buf
= repo_read_object_file(the_repository
,
468 &entry
->idx
.oid
, &type
,
471 die(_("unable to read %s"),
472 oid_to_hex(&entry
->idx
.oid
));
475 * make sure no cached delta data remains from a
476 * previous attempt before a pack split occurred.
478 FREE_AND_NULL(entry
->delta_data
);
479 entry
->z_delta_size
= 0;
480 } else if (entry
->delta_data
) {
481 size
= DELTA_SIZE(entry
);
482 buf
= entry
->delta_data
;
483 entry
->delta_data
= NULL
;
484 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
485 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
487 buf
= get_delta(entry
);
488 size
= DELTA_SIZE(entry
);
489 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
490 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
493 if (st
) /* large blob case, just assume we don't compress well */
495 else if (entry
->z_delta_size
)
496 datalen
= entry
->z_delta_size
;
498 datalen
= do_compress(&buf
, size
);
501 * The object header is a byte of 'type' followed by zero or
502 * more bytes of length.
504 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
507 if (type
== OBJ_OFS_DELTA
) {
509 * Deltas with relative base contain an additional
510 * encoding of the relative offset for the delta
511 * base from this object's position in the pack.
513 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
514 unsigned pos
= sizeof(dheader
) - 1;
515 dheader
[pos
] = ofs
& 127;
517 dheader
[--pos
] = 128 | (--ofs
& 127);
518 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
524 hashwrite(f
, header
, hdrlen
);
525 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
526 hdrlen
+= sizeof(dheader
) - pos
;
527 } else if (type
== OBJ_REF_DELTA
) {
529 * Deltas with a base reference contain
530 * additional bytes for the base object ID.
532 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
538 hashwrite(f
, header
, hdrlen
);
539 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
542 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
548 hashwrite(f
, header
, hdrlen
);
551 datalen
= write_large_blob_data(st
, f
, &entry
->idx
.oid
);
554 hashwrite(f
, buf
, datalen
);
558 return hdrlen
+ datalen
;
561 /* Return 0 if we will bust the pack-size limit */
562 static off_t
write_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
563 unsigned long limit
, int usable_delta
)
565 struct packed_git
*p
= IN_PACK(entry
);
566 struct pack_window
*w_curs
= NULL
;
569 enum object_type type
= oe_type(entry
);
571 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
572 dheader
[MAX_PACK_OBJECT_HEADER
];
574 const unsigned hashsz
= the_hash_algo
->rawsz
;
575 unsigned long entry_size
= SIZE(entry
);
578 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
579 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
580 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
583 offset
= entry
->in_pack_offset
;
584 if (offset_to_pack_pos(p
, offset
, &pos
) < 0)
585 die(_("write_reuse_object: could not locate %s, expected at "
586 "offset %"PRIuMAX
" in pack %s"),
587 oid_to_hex(&entry
->idx
.oid
), (uintmax_t)offset
,
589 datalen
= pack_pos_to_offset(p
, pos
+ 1) - offset
;
590 if (!pack_to_stdout
&& p
->index_version
> 1 &&
591 check_pack_crc(p
, &w_curs
, offset
, datalen
,
592 pack_pos_to_index(p
, pos
))) {
593 error(_("bad packed object CRC for %s"),
594 oid_to_hex(&entry
->idx
.oid
));
596 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
599 offset
+= entry
->in_pack_header_size
;
600 datalen
-= entry
->in_pack_header_size
;
602 if (!pack_to_stdout
&& p
->index_version
== 1 &&
603 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry_size
)) {
604 error(_("corrupt packed object for %s"),
605 oid_to_hex(&entry
->idx
.oid
));
607 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
610 if (type
== OBJ_OFS_DELTA
) {
611 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
612 unsigned pos
= sizeof(dheader
) - 1;
613 dheader
[pos
] = ofs
& 127;
615 dheader
[--pos
] = 128 | (--ofs
& 127);
616 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
620 hashwrite(f
, header
, hdrlen
);
621 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
622 hdrlen
+= sizeof(dheader
) - pos
;
624 } else if (type
== OBJ_REF_DELTA
) {
625 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
629 hashwrite(f
, header
, hdrlen
);
630 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
634 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
638 hashwrite(f
, header
, hdrlen
);
640 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
643 return hdrlen
+ datalen
;
646 /* Return 0 if we will bust the pack-size limit */
647 static off_t
write_object(struct hashfile
*f
,
648 struct object_entry
*entry
,
653 int usable_delta
, to_reuse
;
658 /* apply size limit if limited packsize and not first object */
659 if (!pack_size_limit
|| !nr_written
)
661 else if (pack_size_limit
<= write_offset
)
663 * the earlier object did not fit the limit; avoid
664 * mistaking this with unlimited (i.e. limit = 0).
668 limit
= pack_size_limit
- write_offset
;
671 usable_delta
= 0; /* no delta */
672 else if (!pack_size_limit
)
673 usable_delta
= 1; /* unlimited packfile */
674 else if (DELTA(entry
)->idx
.offset
== (off_t
)-1)
675 usable_delta
= 0; /* base was written to another pack */
676 else if (DELTA(entry
)->idx
.offset
)
677 usable_delta
= 1; /* base already exists in this pack */
679 usable_delta
= 0; /* base could end up in another pack */
682 to_reuse
= 0; /* explicit */
683 else if (!IN_PACK(entry
))
684 to_reuse
= 0; /* can't reuse what we don't have */
685 else if (oe_type(entry
) == OBJ_REF_DELTA
||
686 oe_type(entry
) == OBJ_OFS_DELTA
)
687 /* check_object() decided it for us ... */
688 to_reuse
= usable_delta
;
689 /* ... but pack split may override that */
690 else if (oe_type(entry
) != entry
->in_pack_type
)
691 to_reuse
= 0; /* pack has delta which is unusable */
692 else if (DELTA(entry
))
693 to_reuse
= 0; /* we want to pack afresh */
695 to_reuse
= 1; /* we have it in-pack undeltified,
696 * and we do not need to deltify it.
700 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
702 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
710 entry
->idx
.crc32
= crc32_end(f
);
714 enum write_one_status
{
715 WRITE_ONE_SKIP
= -1, /* already written */
716 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
717 WRITE_ONE_WRITTEN
= 1, /* normal */
718 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
721 static enum write_one_status
write_one(struct hashfile
*f
,
722 struct object_entry
*e
,
729 * we set offset to 1 (which is an impossible value) to mark
730 * the fact that this object is involved in "write its base
731 * first before writing a deltified object" recursion.
733 recursing
= (e
->idx
.offset
== 1);
735 warning(_("recursive delta detected for object %s"),
736 oid_to_hex(&e
->idx
.oid
));
737 return WRITE_ONE_RECURSIVE
;
738 } else if (e
->idx
.offset
|| e
->preferred_base
) {
739 /* offset is non zero if object is written already. */
740 return WRITE_ONE_SKIP
;
743 /* if we are deltified, write out base object first. */
745 e
->idx
.offset
= 1; /* now recurse */
746 switch (write_one(f
, DELTA(e
), offset
)) {
747 case WRITE_ONE_RECURSIVE
:
748 /* we cannot depend on this one */
753 case WRITE_ONE_BREAK
:
754 e
->idx
.offset
= recursing
;
755 return WRITE_ONE_BREAK
;
759 e
->idx
.offset
= *offset
;
760 size
= write_object(f
, e
, *offset
);
762 e
->idx
.offset
= recursing
;
763 return WRITE_ONE_BREAK
;
765 written_list
[nr_written
++] = &e
->idx
;
767 /* make sure off_t is sufficiently large not to wrap */
768 if (signed_add_overflows(*offset
, size
))
769 die(_("pack too large for current definition of off_t"));
771 return WRITE_ONE_WRITTEN
;
774 static int mark_tagged(const char *path UNUSED
, const char *referent UNUSED
, const struct object_id
*oid
,
775 int flag UNUSED
, void *cb_data UNUSED
)
777 struct object_id peeled
;
778 struct object_entry
*entry
= packlist_find(&to_pack
, oid
);
782 if (!peel_iterated_oid(the_repository
, oid
, &peeled
)) {
783 entry
= packlist_find(&to_pack
, &peeled
);
790 static inline unsigned char oe_layer(struct packing_data
*pack
,
791 struct object_entry
*e
)
795 return pack
->layer
[e
- pack
->objects
];
798 static inline void add_to_write_order(struct object_entry
**wo
,
800 struct object_entry
*e
)
802 if (e
->filled
|| oe_layer(&to_pack
, e
) != write_layer
)
808 static void add_descendants_to_write_order(struct object_entry
**wo
,
810 struct object_entry
*e
)
812 int add_to_order
= 1;
815 struct object_entry
*s
;
816 /* add this node... */
817 add_to_write_order(wo
, endp
, e
);
818 /* all its siblings... */
819 for (s
= DELTA_SIBLING(e
); s
; s
= DELTA_SIBLING(s
)) {
820 add_to_write_order(wo
, endp
, s
);
823 /* drop down a level to add left subtree nodes if possible */
824 if (DELTA_CHILD(e
)) {
829 /* our sibling might have some children, it is next */
830 if (DELTA_SIBLING(e
)) {
831 e
= DELTA_SIBLING(e
);
834 /* go back to our parent node */
836 while (e
&& !DELTA_SIBLING(e
)) {
837 /* we're on the right side of a subtree, keep
838 * going up until we can go right again */
842 /* done- we hit our original root node */
845 /* pass it off to sibling at this level */
846 e
= DELTA_SIBLING(e
);
851 static void add_family_to_write_order(struct object_entry
**wo
,
853 struct object_entry
*e
)
855 struct object_entry
*root
;
857 for (root
= e
; DELTA(root
); root
= DELTA(root
))
859 add_descendants_to_write_order(wo
, endp
, root
);
862 static void compute_layer_order(struct object_entry
**wo
, unsigned int *wo_end
)
864 unsigned int i
, last_untagged
;
865 struct object_entry
*objects
= to_pack
.objects
;
867 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
868 if (objects
[i
].tagged
)
870 add_to_write_order(wo
, wo_end
, &objects
[i
]);
875 * Then fill all the tagged tips.
877 for (; i
< to_pack
.nr_objects
; i
++) {
878 if (objects
[i
].tagged
)
879 add_to_write_order(wo
, wo_end
, &objects
[i
]);
883 * And then all remaining commits and tags.
885 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
886 if (oe_type(&objects
[i
]) != OBJ_COMMIT
&&
887 oe_type(&objects
[i
]) != OBJ_TAG
)
889 add_to_write_order(wo
, wo_end
, &objects
[i
]);
893 * And then all the trees.
895 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
896 if (oe_type(&objects
[i
]) != OBJ_TREE
)
898 add_to_write_order(wo
, wo_end
, &objects
[i
]);
902 * Finally all the rest in really tight order
904 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
905 if (!objects
[i
].filled
&& oe_layer(&to_pack
, &objects
[i
]) == write_layer
)
906 add_family_to_write_order(wo
, wo_end
, &objects
[i
]);
910 static struct object_entry
**compute_write_order(void)
912 uint32_t max_layers
= 1;
913 unsigned int i
, wo_end
;
915 struct object_entry
**wo
;
916 struct object_entry
*objects
= to_pack
.objects
;
918 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
919 objects
[i
].tagged
= 0;
920 objects
[i
].filled
= 0;
921 SET_DELTA_CHILD(&objects
[i
], NULL
);
922 SET_DELTA_SIBLING(&objects
[i
], NULL
);
926 * Fully connect delta_child/delta_sibling network.
927 * Make sure delta_sibling is sorted in the original
930 for (i
= to_pack
.nr_objects
; i
> 0;) {
931 struct object_entry
*e
= &objects
[--i
];
934 /* Mark me as the first child */
935 e
->delta_sibling_idx
= DELTA(e
)->delta_child_idx
;
936 SET_DELTA_CHILD(DELTA(e
), e
);
940 * Mark objects that are at the tip of tags.
942 refs_for_each_tag_ref(get_main_ref_store(the_repository
), mark_tagged
,
945 if (use_delta_islands
) {
946 max_layers
= compute_pack_layers(&to_pack
);
950 ALLOC_ARRAY(wo
, to_pack
.nr_objects
);
953 for (; write_layer
< max_layers
; ++write_layer
)
954 compute_layer_order(wo
, &wo_end
);
956 if (wo_end
!= to_pack
.nr_objects
)
957 die(_("ordered %u objects, expected %"PRIu32
),
958 wo_end
, to_pack
.nr_objects
);
965 * A reused set of objects. All objects in a chunk have the same
966 * relative position in the original packfile and the generated
970 static struct reused_chunk
{
971 /* The offset of the first object of this chunk in the original
974 /* The difference for "original" minus the offset of the first object of
975 * this chunk in the generated packfile. */
978 static int reused_chunks_nr
;
979 static int reused_chunks_alloc
;
981 static void record_reused_object(off_t where
, off_t offset
)
983 if (reused_chunks_nr
&& reused_chunks
[reused_chunks_nr
-1].difference
== offset
)
986 ALLOC_GROW(reused_chunks
, reused_chunks_nr
+ 1,
987 reused_chunks_alloc
);
988 reused_chunks
[reused_chunks_nr
].original
= where
;
989 reused_chunks
[reused_chunks_nr
].difference
= offset
;
994 * Binary search to find the chunk that "where" is in. Note
995 * that we're not looking for an exact match, just the first
996 * chunk that contains it (which implicitly ends at the start
999 static off_t
find_reused_offset(off_t where
)
1001 int lo
= 0, hi
= reused_chunks_nr
;
1003 int mi
= lo
+ ((hi
- lo
) / 2);
1004 if (where
== reused_chunks
[mi
].original
)
1005 return reused_chunks
[mi
].difference
;
1006 if (where
< reused_chunks
[mi
].original
)
1013 * The first chunk starts at zero, so we can't have gone below
1017 return reused_chunks
[lo
-1].difference
;
1020 static void write_reused_pack_one(struct packed_git
*reuse_packfile
,
1021 size_t pos
, struct hashfile
*out
,
1023 struct pack_window
**w_curs
)
1025 off_t offset
, next
, cur
;
1026 enum object_type type
;
1029 offset
= pack_pos_to_offset(reuse_packfile
, pos
);
1030 next
= pack_pos_to_offset(reuse_packfile
, pos
+ 1);
1032 record_reused_object(offset
,
1033 offset
- (hashfile_total(out
) - pack_start
));
1036 type
= unpack_object_header(reuse_packfile
, w_curs
, &cur
, &size
);
1039 if (type
== OBJ_OFS_DELTA
) {
1043 unsigned char header
[MAX_PACK_OBJECT_HEADER
];
1046 base_offset
= get_delta_base(reuse_packfile
, w_curs
, &cur
, type
, offset
);
1047 assert(base_offset
!= 0);
1049 /* Convert to REF_DELTA if we must... */
1050 if (!allow_ofs_delta
) {
1052 struct object_id base_oid
;
1054 if (offset_to_pack_pos(reuse_packfile
, base_offset
, &base_pos
) < 0)
1055 die(_("expected object at offset %"PRIuMAX
" "
1057 (uintmax_t)base_offset
,
1058 reuse_packfile
->pack_name
);
1060 nth_packed_object_id(&base_oid
, reuse_packfile
,
1061 pack_pos_to_index(reuse_packfile
, base_pos
));
1063 len
= encode_in_pack_object_header(header
, sizeof(header
),
1064 OBJ_REF_DELTA
, size
);
1065 hashwrite(out
, header
, len
);
1066 hashwrite(out
, base_oid
.hash
, the_hash_algo
->rawsz
);
1067 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
1071 /* Otherwise see if we need to rewrite the offset... */
1072 fixup
= find_reused_offset(offset
) -
1073 find_reused_offset(base_offset
);
1075 unsigned char ofs_header
[MAX_PACK_OBJECT_HEADER
];
1076 unsigned i
, ofs_len
;
1077 off_t ofs
= offset
- base_offset
- fixup
;
1079 len
= encode_in_pack_object_header(header
, sizeof(header
),
1080 OBJ_OFS_DELTA
, size
);
1082 i
= sizeof(ofs_header
) - 1;
1083 ofs_header
[i
] = ofs
& 127;
1085 ofs_header
[--i
] = 128 | (--ofs
& 127);
1087 ofs_len
= sizeof(ofs_header
) - i
;
1089 hashwrite(out
, header
, len
);
1090 hashwrite(out
, ofs_header
+ sizeof(ofs_header
) - ofs_len
, ofs_len
);
1091 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
1095 /* ...otherwise we have no fixup, and can write it verbatim */
1098 copy_pack_data(out
, reuse_packfile
, w_curs
, offset
, next
- offset
);
1101 static size_t write_reused_pack_verbatim(struct bitmapped_pack
*reuse_packfile
,
1102 struct hashfile
*out
,
1104 struct pack_window
**w_curs
)
1106 size_t pos
= reuse_packfile
->bitmap_pos
;
1109 if (pos
% BITS_IN_EWORD
) {
1110 size_t word_pos
= (pos
/ BITS_IN_EWORD
);
1111 size_t offset
= pos
% BITS_IN_EWORD
;
1113 eword_t word
= reuse_packfile_bitmap
->words
[word_pos
];
1115 if (offset
+ reuse_packfile
->bitmap_nr
< BITS_IN_EWORD
)
1116 last
= offset
+ reuse_packfile
->bitmap_nr
;
1118 last
= BITS_IN_EWORD
;
1120 for (; offset
< last
; offset
++) {
1121 if (word
>> offset
== 0)
1123 if (!bitmap_get(reuse_packfile_bitmap
,
1124 word_pos
* BITS_IN_EWORD
+ offset
))
1128 pos
+= BITS_IN_EWORD
- (pos
% BITS_IN_EWORD
);
1132 * Now we're going to copy as many whole eword_t's as possible.
1133 * "end" is the index of the last whole eword_t we copy, but
1134 * there may be additional bits to process. Those are handled
1135 * individually by write_reused_pack().
1137 * Begin by advancing to the first word boundary in range of the
1138 * bit positions occupied by objects in "reuse_packfile". Then
1139 * pick the last word boundary in the same range. If we have at
1140 * least one word's worth of bits to process, continue on.
1142 end
= reuse_packfile
->bitmap_pos
+ reuse_packfile
->bitmap_nr
;
1143 if (end
% BITS_IN_EWORD
)
1144 end
-= end
% BITS_IN_EWORD
;
1146 return reuse_packfile
->bitmap_pos
/ BITS_IN_EWORD
;
1149 reuse_packfile_bitmap
->words
[pos
/ BITS_IN_EWORD
] == (eword_t
)~0)
1150 pos
+= BITS_IN_EWORD
;
1155 if (reuse_packfile
->bitmap_pos
< pos
) {
1156 off_t pack_start_off
= pack_pos_to_offset(reuse_packfile
->p
, 0);
1157 off_t pack_end_off
= pack_pos_to_offset(reuse_packfile
->p
,
1158 pos
- reuse_packfile
->bitmap_pos
);
1160 written
+= pos
- reuse_packfile
->bitmap_pos
;
1162 /* We're recording one chunk, not one object. */
1163 record_reused_object(pack_start_off
,
1164 pack_start_off
- (hashfile_total(out
) - pack_start
));
1166 copy_pack_data(out
, reuse_packfile
->p
, w_curs
,
1167 pack_start_off
, pack_end_off
- pack_start_off
);
1169 display_progress(progress_state
, written
);
1171 if (pos
% BITS_IN_EWORD
)
1172 BUG("attempted to jump past a word boundary to %"PRIuMAX
,
1174 return pos
/ BITS_IN_EWORD
;
1177 static void write_reused_pack(struct bitmapped_pack
*reuse_packfile
,
1180 size_t i
= reuse_packfile
->bitmap_pos
/ BITS_IN_EWORD
;
1182 off_t pack_start
= hashfile_total(f
) - sizeof(struct pack_header
);
1183 struct pack_window
*w_curs
= NULL
;
1185 if (allow_ofs_delta
)
1186 i
= write_reused_pack_verbatim(reuse_packfile
, f
, pack_start
,
1189 for (; i
< reuse_packfile_bitmap
->word_alloc
; ++i
) {
1190 eword_t word
= reuse_packfile_bitmap
->words
[i
];
1191 size_t pos
= (i
* BITS_IN_EWORD
);
1193 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1195 if ((word
>> offset
) == 0)
1198 offset
+= ewah_bit_ctz64(word
>> offset
);
1199 if (pos
+ offset
< reuse_packfile
->bitmap_pos
)
1201 if (pos
+ offset
>= reuse_packfile
->bitmap_pos
+ reuse_packfile
->bitmap_nr
)
1204 if (reuse_packfile
->bitmap_pos
) {
1206 * When doing multi-pack reuse on a
1207 * non-preferred pack, translate bit positions
1208 * from the MIDX pseudo-pack order back to their
1209 * pack-relative positions before attempting
1212 struct multi_pack_index
*m
= reuse_packfile
->from_midx
;
1217 BUG("non-zero bitmap position without MIDX");
1219 midx_pos
= pack_pos_to_midx(m
, pos
+ offset
);
1220 pack_ofs
= nth_midxed_offset(m
, midx_pos
);
1222 if (offset_to_pack_pos(reuse_packfile
->p
,
1223 pack_ofs
, &pack_pos
) < 0)
1224 BUG("could not find expected object at offset %"PRIuMAX
" in pack %s",
1225 (uintmax_t)pack_ofs
,
1226 pack_basename(reuse_packfile
->p
));
1229 * Can use bit positions directly, even for MIDX
1230 * bitmaps. See comment in try_partial_reuse()
1233 pack_pos
= pos
+ offset
;
1236 write_reused_pack_one(reuse_packfile
->p
, pack_pos
, f
,
1237 pack_start
, &w_curs
);
1238 display_progress(progress_state
, ++written
);
1243 unuse_pack(&w_curs
);
1246 static void write_excluded_by_configs(void)
1248 struct oidset_iter iter
;
1249 const struct object_id
*oid
;
1251 oidset_iter_init(&excluded_by_config
, &iter
);
1252 while ((oid
= oidset_iter_next(&iter
))) {
1253 struct configured_exclusion
*ex
=
1254 oidmap_get(&configured_exclusions
, oid
);
1257 BUG("configured exclusion wasn't configured");
1258 write_in_full(1, ex
->pack_hash_hex
, strlen(ex
->pack_hash_hex
));
1259 write_in_full(1, " ", 1);
1260 write_in_full(1, ex
->uri
, strlen(ex
->uri
));
1261 write_in_full(1, "\n", 1);
1265 static const char no_split_warning
[] = N_(
1266 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
1269 static void write_pack_file(void)
1274 uint32_t nr_remaining
= nr_result
;
1275 time_t last_mtime
= 0;
1276 struct object_entry
**write_order
;
1278 if (progress
> pack_to_stdout
)
1279 progress_state
= start_progress(_("Writing objects"), nr_result
);
1280 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
1281 write_order
= compute_write_order();
1284 unsigned char hash
[GIT_MAX_RAWSZ
];
1285 char *pack_tmp_name
= NULL
;
1288 f
= hashfd_throughput(1, "<stdout>", progress_state
);
1290 f
= create_tmp_packfile(&pack_tmp_name
);
1292 offset
= write_pack_header(f
, nr_remaining
);
1294 if (reuse_packfiles_nr
) {
1295 assert(pack_to_stdout
);
1296 for (j
= 0; j
< reuse_packfiles_nr
; j
++) {
1297 reused_chunks_nr
= 0;
1298 write_reused_pack(&reuse_packfiles
[j
], f
);
1299 if (reused_chunks_nr
)
1300 reuse_packfiles_used_nr
++;
1302 offset
= hashfile_total(f
);
1306 for (; i
< to_pack
.nr_objects
; i
++) {
1307 struct object_entry
*e
= write_order
[i
];
1308 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
1310 display_progress(progress_state
, written
);
1313 if (pack_to_stdout
) {
1315 * We never fsync when writing to stdout since we may
1316 * not be writing to an actual pack file. For instance,
1317 * the upload-pack code passes a pipe here. Calling
1318 * fsync on a pipe results in unnecessary
1319 * synchronization with the reader on some platforms.
1321 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_NONE
,
1322 CSUM_HASH_IN_STREAM
| CSUM_CLOSE
);
1323 } else if (nr_written
== nr_remaining
) {
1324 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
,
1325 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
1328 * If we wrote the wrong number of entries in the
1329 * header, rewrite it like in fast-import.
1332 int fd
= finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
, 0);
1333 fixup_pack_header_footer(fd
, hash
, pack_tmp_name
,
1334 nr_written
, hash
, offset
);
1336 if (write_bitmap_index
) {
1337 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1338 warning(_(no_split_warning
));
1339 write_bitmap_index
= 0;
1343 if (!pack_to_stdout
) {
1345 struct strbuf tmpname
= STRBUF_INIT
;
1346 struct bitmap_writer bitmap_writer
;
1347 char *idx_tmp_name
= NULL
;
1350 * Packs are runtime accessed in their mtime
1351 * order since newer packs are more likely to contain
1352 * younger objects. So if we are creating multiple
1353 * packs then we should modify the mtime of later ones
1354 * to preserve this property.
1356 if (stat(pack_tmp_name
, &st
) < 0) {
1357 warning_errno(_("failed to stat %s"), pack_tmp_name
);
1358 } else if (!last_mtime
) {
1359 last_mtime
= st
.st_mtime
;
1362 utb
.actime
= st
.st_atime
;
1363 utb
.modtime
= --last_mtime
;
1364 if (utime(pack_tmp_name
, &utb
) < 0)
1365 warning_errno(_("failed utime() on %s"), pack_tmp_name
);
1368 strbuf_addf(&tmpname
, "%s-%s.", base_name
,
1371 if (write_bitmap_index
) {
1372 bitmap_writer_init(&bitmap_writer
,
1373 the_repository
, &to_pack
);
1374 bitmap_writer_set_checksum(&bitmap_writer
, hash
);
1375 bitmap_writer_build_type_index(&bitmap_writer
,
1380 pack_idx_opts
.flags
|= WRITE_MTIMES
;
1382 stage_tmp_packfiles(&tmpname
, pack_tmp_name
,
1383 written_list
, nr_written
,
1384 &to_pack
, &pack_idx_opts
, hash
,
1387 if (write_bitmap_index
) {
1388 size_t tmpname_len
= tmpname
.len
;
1390 strbuf_addstr(&tmpname
, "bitmap");
1391 stop_progress(&progress_state
);
1393 bitmap_writer_show_progress(&bitmap_writer
,
1395 bitmap_writer_select_commits(&bitmap_writer
,
1397 indexed_commits_nr
);
1398 if (bitmap_writer_build(&bitmap_writer
) < 0)
1399 die(_("failed to write bitmap index"));
1400 bitmap_writer_finish(&bitmap_writer
,
1402 tmpname
.buf
, write_bitmap_options
);
1403 bitmap_writer_free(&bitmap_writer
);
1404 write_bitmap_index
= 0;
1405 strbuf_setlen(&tmpname
, tmpname_len
);
1408 rename_tmp_packfile_idx(&tmpname
, &idx_tmp_name
);
1411 strbuf_release(&tmpname
);
1412 free(pack_tmp_name
);
1413 puts(hash_to_hex(hash
));
1416 /* mark written objects as written to previous pack */
1417 for (j
= 0; j
< nr_written
; j
++) {
1418 written_list
[j
]->offset
= (off_t
)-1;
1420 nr_remaining
-= nr_written
;
1421 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
1425 stop_progress(&progress_state
);
1426 if (written
!= nr_result
)
1427 die(_("wrote %"PRIu32
" objects while expecting %"PRIu32
),
1428 written
, nr_result
);
1429 trace2_data_intmax("pack-objects", the_repository
,
1430 "write_pack_file/wrote", nr_result
);
1433 static int no_try_delta(const char *path
)
1435 static struct attr_check
*check
;
1438 check
= attr_check_initl("delta", NULL
);
1439 git_check_attr(the_repository
->index
, path
, check
);
1440 if (ATTR_FALSE(check
->items
[0].value
))
1446 * When adding an object, check whether we have already added it
1447 * to our packing list. If so, we can skip. However, if we are
1448 * being asked to excludei t, but the previous mention was to include
1449 * it, make sure to adjust its flags and tweak our numbers accordingly.
1451 * As an optimization, we pass out the index position where we would have
1452 * found the item, since that saves us from having to look it up again a
1453 * few lines later when we want to add the new entry.
1455 static int have_duplicate_entry(const struct object_id
*oid
,
1458 struct object_entry
*entry
;
1460 if (reuse_packfile_bitmap
&&
1461 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
))
1464 entry
= packlist_find(&to_pack
, oid
);
1469 if (!entry
->preferred_base
)
1471 entry
->preferred_base
= 1;
1477 static int want_found_object(const struct object_id
*oid
, int exclude
,
1478 struct packed_git
*p
)
1485 if (!is_pack_valid(p
))
1489 * When asked to do --local (do not include an object that appears in a
1490 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1491 * an object that appears in a pack marked with .keep), finding a pack
1492 * that matches the criteria is sufficient for us to decide to omit it.
1493 * However, even if this pack does not satisfy the criteria, we need to
1494 * make sure no copy of this object appears in _any_ pack that makes us
1495 * to omit the object, so we need to check all the packs.
1497 * We can however first check whether these options can possibly matter;
1498 * if they do not matter we know we want the object in generated pack.
1499 * Otherwise, we signal "-1" at the end to tell the caller that we do
1500 * not know either way, and it needs to check more packs.
1504 * Objects in packs borrowed from elsewhere are discarded regardless of
1505 * if they appear in other packs that weren't borrowed.
1507 if (local
&& !p
->pack_local
)
1511 * Then handle .keep first, as we have a fast(er) path there.
1513 if (ignore_packed_keep_on_disk
|| ignore_packed_keep_in_core
) {
1515 * Set the flags for the kept-pack cache to be the ones we want
1518 * That is, if we are ignoring objects in on-disk keep packs,
1519 * then we want to search through the on-disk keep and ignore
1523 if (ignore_packed_keep_on_disk
)
1524 flags
|= ON_DISK_KEEP_PACKS
;
1525 if (ignore_packed_keep_in_core
)
1526 flags
|= IN_CORE_KEEP_PACKS
;
1528 if (ignore_packed_keep_on_disk
&& p
->pack_keep
)
1530 if (ignore_packed_keep_in_core
&& p
->pack_keep_in_core
)
1532 if (has_object_kept_pack(oid
, flags
))
1537 * At this point we know definitively that either we don't care about
1538 * keep-packs, or the object is not in one. Keep checking other
1541 if (!local
|| !have_non_local_packs
)
1544 /* we don't know yet; keep looking for more packs */
1548 static int want_object_in_pack_one(struct packed_git
*p
,
1549 const struct object_id
*oid
,
1551 struct packed_git
**found_pack
,
1552 off_t
*found_offset
)
1556 if (p
== *found_pack
)
1557 offset
= *found_offset
;
1559 offset
= find_pack_entry_one(oid
->hash
, p
);
1563 if (!is_pack_valid(p
))
1565 *found_offset
= offset
;
1568 return want_found_object(oid
, exclude
, p
);
1574 * Check whether we want the object in the pack (e.g., we do not want
1575 * objects found in non-local stores if the "--local" option was used).
1577 * If the caller already knows an existing pack it wants to take the object
1578 * from, that is passed in *found_pack and *found_offset; otherwise this
1579 * function finds if there is any pack that has the object and returns the pack
1580 * and its offset in these variables.
1582 static int want_object_in_pack(const struct object_id
*oid
,
1584 struct packed_git
**found_pack
,
1585 off_t
*found_offset
)
1588 struct list_head
*pos
;
1589 struct multi_pack_index
*m
;
1591 if (!exclude
&& local
&& has_loose_object_nonlocal(oid
))
1595 * If we already know the pack object lives in, start checks from that
1596 * pack - in the usual case when neither --local was given nor .keep files
1597 * are present we will determine the answer right now.
1600 want
= want_found_object(oid
, exclude
, *found_pack
);
1608 for (m
= get_multi_pack_index(the_repository
); m
; m
= m
->next
) {
1609 struct pack_entry e
;
1610 if (fill_midx_entry(the_repository
, oid
, &e
, m
)) {
1611 want
= want_object_in_pack_one(e
.p
, oid
, exclude
, found_pack
, found_offset
);
1617 list_for_each(pos
, get_packed_git_mru(the_repository
)) {
1618 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1619 want
= want_object_in_pack_one(p
, oid
, exclude
, found_pack
, found_offset
);
1620 if (!exclude
&& want
> 0)
1622 get_packed_git_mru(the_repository
));
1627 if (uri_protocols
.nr
) {
1628 struct configured_exclusion
*ex
=
1629 oidmap_get(&configured_exclusions
, oid
);
1634 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1635 if (skip_prefix(ex
->uri
,
1636 uri_protocols
.items
[i
].string
,
1639 oidset_insert(&excluded_by_config
, oid
);
1649 static struct object_entry
*create_object_entry(const struct object_id
*oid
,
1650 enum object_type type
,
1654 struct packed_git
*found_pack
,
1657 struct object_entry
*entry
;
1659 entry
= packlist_alloc(&to_pack
, oid
);
1661 oe_set_type(entry
, type
);
1663 entry
->preferred_base
= 1;
1667 oe_set_in_pack(&to_pack
, entry
, found_pack
);
1668 entry
->in_pack_offset
= found_offset
;
1671 entry
->no_try_delta
= no_try_delta
;
1676 static const char no_closure_warning
[] = N_(
1677 "disabling bitmap writing, as some objects are not being packed"
1680 static int add_object_entry(const struct object_id
*oid
, enum object_type type
,
1681 const char *name
, int exclude
)
1683 struct packed_git
*found_pack
= NULL
;
1684 off_t found_offset
= 0;
1686 display_progress(progress_state
, ++nr_seen
);
1688 if (have_duplicate_entry(oid
, exclude
))
1691 if (!want_object_in_pack(oid
, exclude
, &found_pack
, &found_offset
)) {
1692 /* The pack is missing an object, so it will not have closure */
1693 if (write_bitmap_index
) {
1694 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1695 warning(_(no_closure_warning
));
1696 write_bitmap_index
= 0;
1701 create_object_entry(oid
, type
, pack_name_hash(name
),
1702 exclude
, name
&& no_try_delta(name
),
1703 found_pack
, found_offset
);
1707 static int add_object_entry_from_bitmap(const struct object_id
*oid
,
1708 enum object_type type
,
1709 int flags UNUSED
, uint32_t name_hash
,
1710 struct packed_git
*pack
, off_t offset
)
1712 display_progress(progress_state
, ++nr_seen
);
1714 if (have_duplicate_entry(oid
, 0))
1717 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
1720 create_object_entry(oid
, type
, name_hash
, 0, 0, pack
, offset
);
1724 struct pbase_tree_cache
{
1725 struct object_id oid
;
1729 unsigned long tree_size
;
1732 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1733 static int pbase_tree_cache_ix(const struct object_id
*oid
)
1735 return oid
->hash
[0] % ARRAY_SIZE(pbase_tree_cache
);
1737 static int pbase_tree_cache_ix_incr(int ix
)
1739 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1742 static struct pbase_tree
{
1743 struct pbase_tree
*next
;
1744 /* This is a phony "cache" entry; we are not
1745 * going to evict it or find it through _get()
1746 * mechanism -- this is for the toplevel node that
1747 * would almost always change with any commit.
1749 struct pbase_tree_cache pcache
;
1752 static struct pbase_tree_cache
*pbase_tree_get(const struct object_id
*oid
)
1754 struct pbase_tree_cache
*ent
, *nent
;
1757 enum object_type type
;
1759 int my_ix
= pbase_tree_cache_ix(oid
);
1760 int available_ix
= -1;
1762 /* pbase-tree-cache acts as a limited hashtable.
1763 * your object will be found at your index or within a few
1764 * slots after that slot if it is cached.
1766 for (neigh
= 0; neigh
< 8; neigh
++) {
1767 ent
= pbase_tree_cache
[my_ix
];
1768 if (ent
&& oideq(&ent
->oid
, oid
)) {
1772 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1773 ((0 <= available_ix
) &&
1774 (!ent
&& pbase_tree_cache
[available_ix
])))
1775 available_ix
= my_ix
;
1778 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1781 /* Did not find one. Either we got a bogus request or
1782 * we need to read and perhaps cache.
1784 data
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
1787 if (type
!= OBJ_TREE
) {
1792 /* We need to either cache or return a throwaway copy */
1794 if (available_ix
< 0)
1797 ent
= pbase_tree_cache
[available_ix
];
1798 my_ix
= available_ix
;
1802 nent
= xmalloc(sizeof(*nent
));
1803 nent
->temporary
= (available_ix
< 0);
1806 /* evict and reuse */
1807 free(ent
->tree_data
);
1810 oidcpy(&nent
->oid
, oid
);
1811 nent
->tree_data
= data
;
1812 nent
->tree_size
= size
;
1814 if (!nent
->temporary
)
1815 pbase_tree_cache
[my_ix
] = nent
;
1819 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1821 if (!cache
->temporary
) {
1825 free(cache
->tree_data
);
1829 static size_t name_cmp_len(const char *name
)
1831 return strcspn(name
, "\n/");
1834 static void add_pbase_object(struct tree_desc
*tree
,
1837 const char *fullname
)
1839 struct name_entry entry
;
1842 while (tree_entry(tree
,&entry
)) {
1843 if (S_ISGITLINK(entry
.mode
))
1845 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1846 memcmp(name
, entry
.path
, cmplen
);
1851 if (name
[cmplen
] != '/') {
1852 add_object_entry(&entry
.oid
,
1853 object_type(entry
.mode
),
1857 if (S_ISDIR(entry
.mode
)) {
1858 struct tree_desc sub
;
1859 struct pbase_tree_cache
*tree
;
1860 const char *down
= name
+cmplen
+1;
1861 size_t downlen
= name_cmp_len(down
);
1863 tree
= pbase_tree_get(&entry
.oid
);
1866 init_tree_desc(&sub
, &tree
->oid
,
1867 tree
->tree_data
, tree
->tree_size
);
1869 add_pbase_object(&sub
, down
, downlen
, fullname
);
1870 pbase_tree_put(tree
);
1875 static unsigned *done_pbase_paths
;
1876 static int done_pbase_paths_num
;
1877 static int done_pbase_paths_alloc
;
1878 static int done_pbase_path_pos(unsigned hash
)
1881 int hi
= done_pbase_paths_num
;
1883 int mi
= lo
+ (hi
- lo
) / 2;
1884 if (done_pbase_paths
[mi
] == hash
)
1886 if (done_pbase_paths
[mi
] < hash
)
1894 static int check_pbase_path(unsigned hash
)
1896 int pos
= done_pbase_path_pos(hash
);
1900 ALLOC_GROW(done_pbase_paths
,
1901 done_pbase_paths_num
+ 1,
1902 done_pbase_paths_alloc
);
1903 done_pbase_paths_num
++;
1904 if (pos
< done_pbase_paths_num
)
1905 MOVE_ARRAY(done_pbase_paths
+ pos
+ 1, done_pbase_paths
+ pos
,
1906 done_pbase_paths_num
- pos
- 1);
1907 done_pbase_paths
[pos
] = hash
;
1911 static void add_preferred_base_object(const char *name
)
1913 struct pbase_tree
*it
;
1915 unsigned hash
= pack_name_hash(name
);
1917 if (!num_preferred_base
|| check_pbase_path(hash
))
1920 cmplen
= name_cmp_len(name
);
1921 for (it
= pbase_tree
; it
; it
= it
->next
) {
1923 add_object_entry(&it
->pcache
.oid
, OBJ_TREE
, NULL
, 1);
1926 struct tree_desc tree
;
1927 init_tree_desc(&tree
, &it
->pcache
.oid
,
1928 it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1929 add_pbase_object(&tree
, name
, cmplen
, name
);
1934 static void add_preferred_base(struct object_id
*oid
)
1936 struct pbase_tree
*it
;
1939 struct object_id tree_oid
;
1941 if (window
<= num_preferred_base
++)
1944 data
= read_object_with_reference(the_repository
, oid
,
1945 OBJ_TREE
, &size
, &tree_oid
);
1949 for (it
= pbase_tree
; it
; it
= it
->next
) {
1950 if (oideq(&it
->pcache
.oid
, &tree_oid
)) {
1956 CALLOC_ARRAY(it
, 1);
1957 it
->next
= pbase_tree
;
1960 oidcpy(&it
->pcache
.oid
, &tree_oid
);
1961 it
->pcache
.tree_data
= data
;
1962 it
->pcache
.tree_size
= size
;
1965 static void cleanup_preferred_base(void)
1967 struct pbase_tree
*it
;
1973 struct pbase_tree
*tmp
= it
;
1975 free(tmp
->pcache
.tree_data
);
1979 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1980 if (!pbase_tree_cache
[i
])
1982 free(pbase_tree_cache
[i
]->tree_data
);
1983 FREE_AND_NULL(pbase_tree_cache
[i
]);
1986 FREE_AND_NULL(done_pbase_paths
);
1987 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1991 * Return 1 iff the object specified by "delta" can be sent
1992 * literally as a delta against the base in "base_sha1". If
1993 * so, then *base_out will point to the entry in our packing
1994 * list, or NULL if we must use the external-base list.
1996 * Depth value does not matter - find_deltas() will
1997 * never consider reused delta as the base object to
1998 * deltify other objects against, in order to avoid
2001 static int can_reuse_delta(const struct object_id
*base_oid
,
2002 struct object_entry
*delta
,
2003 struct object_entry
**base_out
)
2005 struct object_entry
*base
;
2008 * First see if we're already sending the base (or it's explicitly in
2009 * our "excluded" list).
2011 base
= packlist_find(&to_pack
, base_oid
);
2013 if (!in_same_island(&delta
->idx
.oid
, &base
->idx
.oid
))
2020 * Otherwise, reachability bitmaps may tell us if the receiver has it,
2021 * even if it was buried too deep in history to make it into the
2024 if (thin
&& bitmap_has_oid_in_uninteresting(bitmap_git
, base_oid
)) {
2025 if (use_delta_islands
) {
2026 if (!in_same_island(&delta
->idx
.oid
, base_oid
))
2036 static void prefetch_to_pack(uint32_t object_index_start
) {
2037 struct oid_array to_fetch
= OID_ARRAY_INIT
;
2040 for (i
= object_index_start
; i
< to_pack
.nr_objects
; i
++) {
2041 struct object_entry
*entry
= to_pack
.objects
+ i
;
2043 if (!oid_object_info_extended(the_repository
,
2046 OBJECT_INFO_FOR_PREFETCH
))
2048 oid_array_append(&to_fetch
, &entry
->idx
.oid
);
2050 promisor_remote_get_direct(the_repository
,
2051 to_fetch
.oid
, to_fetch
.nr
);
2052 oid_array_clear(&to_fetch
);
2055 static void check_object(struct object_entry
*entry
, uint32_t object_index
)
2057 unsigned long canonical_size
;
2058 enum object_type type
;
2059 struct object_info oi
= {.typep
= &type
, .sizep
= &canonical_size
};
2061 if (IN_PACK(entry
)) {
2062 struct packed_git
*p
= IN_PACK(entry
);
2063 struct pack_window
*w_curs
= NULL
;
2065 struct object_id base_ref
;
2066 struct object_entry
*base_entry
;
2067 unsigned long used
, used_0
;
2068 unsigned long avail
;
2070 unsigned char *buf
, c
;
2071 enum object_type type
;
2072 unsigned long in_pack_size
;
2074 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
2077 * We want in_pack_type even if we do not reuse delta
2078 * since non-delta representations could still be reused.
2080 used
= unpack_object_header_buffer(buf
, avail
,
2087 BUG("invalid type %d", type
);
2088 entry
->in_pack_type
= type
;
2091 * Determine if this is a delta and if so whether we can
2092 * reuse it or not. Otherwise let's find out as cheaply as
2093 * possible what the actual type and size for this object is.
2095 switch (entry
->in_pack_type
) {
2097 /* Not a delta hence we've already got all we need. */
2098 oe_set_type(entry
, entry
->in_pack_type
);
2099 SET_SIZE(entry
, in_pack_size
);
2100 entry
->in_pack_header_size
= used
;
2101 if (oe_type(entry
) < OBJ_COMMIT
|| oe_type(entry
) > OBJ_BLOB
)
2103 unuse_pack(&w_curs
);
2106 if (reuse_delta
&& !entry
->preferred_base
) {
2108 use_pack(p
, &w_curs
,
2109 entry
->in_pack_offset
+ used
,
2111 the_repository
->hash_algo
);
2114 entry
->in_pack_header_size
= used
+ the_hash_algo
->rawsz
;
2117 buf
= use_pack(p
, &w_curs
,
2118 entry
->in_pack_offset
+ used
, NULL
);
2124 if (!ofs
|| MSB(ofs
, 7)) {
2125 error(_("delta base offset overflow in pack for %s"),
2126 oid_to_hex(&entry
->idx
.oid
));
2130 ofs
= (ofs
<< 7) + (c
& 127);
2132 ofs
= entry
->in_pack_offset
- ofs
;
2133 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
2134 error(_("delta base offset out of bound for %s"),
2135 oid_to_hex(&entry
->idx
.oid
));
2138 if (reuse_delta
&& !entry
->preferred_base
) {
2140 if (offset_to_pack_pos(p
, ofs
, &pos
) < 0)
2142 if (!nth_packed_object_id(&base_ref
, p
,
2143 pack_pos_to_index(p
, pos
)))
2146 entry
->in_pack_header_size
= used
+ used_0
;
2151 can_reuse_delta(&base_ref
, entry
, &base_entry
)) {
2152 oe_set_type(entry
, entry
->in_pack_type
);
2153 SET_SIZE(entry
, in_pack_size
); /* delta size */
2154 SET_DELTA_SIZE(entry
, in_pack_size
);
2157 SET_DELTA(entry
, base_entry
);
2158 entry
->delta_sibling_idx
= base_entry
->delta_child_idx
;
2159 SET_DELTA_CHILD(base_entry
, entry
);
2161 SET_DELTA_EXT(entry
, &base_ref
);
2164 unuse_pack(&w_curs
);
2168 if (oe_type(entry
)) {
2172 * This must be a delta and we already know what the
2173 * final object type is. Let's extract the actual
2174 * object size from the delta header.
2176 delta_pos
= entry
->in_pack_offset
+ entry
->in_pack_header_size
;
2177 canonical_size
= get_size_from_delta(p
, &w_curs
, delta_pos
);
2178 if (canonical_size
== 0)
2180 SET_SIZE(entry
, canonical_size
);
2181 unuse_pack(&w_curs
);
2186 * No choice but to fall back to the recursive delta walk
2187 * with oid_object_info() to find about the object type
2191 unuse_pack(&w_curs
);
2194 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2195 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0) {
2196 if (repo_has_promisor_remote(the_repository
)) {
2197 prefetch_to_pack(object_index
);
2198 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2199 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0)
2205 oe_set_type(entry
, type
);
2206 if (entry
->type_valid
) {
2207 SET_SIZE(entry
, canonical_size
);
2210 * Bad object type is checked in prepare_pack(). This is
2211 * to permit a missing preferred base object to be ignored
2212 * as a preferred base. Doing so can result in a larger
2213 * pack file, but the transfer will still take place.
2218 static int pack_offset_sort(const void *_a
, const void *_b
)
2220 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2221 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2222 const struct packed_git
*a_in_pack
= IN_PACK(a
);
2223 const struct packed_git
*b_in_pack
= IN_PACK(b
);
2225 /* avoid filesystem trashing with loose objects */
2226 if (!a_in_pack
&& !b_in_pack
)
2227 return oidcmp(&a
->idx
.oid
, &b
->idx
.oid
);
2229 if (a_in_pack
< b_in_pack
)
2231 if (a_in_pack
> b_in_pack
)
2233 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
2234 (a
->in_pack_offset
> b
->in_pack_offset
);
2238 * Drop an on-disk delta we were planning to reuse. Naively, this would
2239 * just involve blanking out the "delta" field, but we have to deal
2240 * with some extra book-keeping:
2242 * 1. Removing ourselves from the delta_sibling linked list.
2244 * 2. Updating our size/type to the non-delta representation. These were
2245 * either not recorded initially (size) or overwritten with the delta type
2246 * (type) when check_object() decided to reuse the delta.
2248 * 3. Resetting our delta depth, as we are now a base object.
2250 static void drop_reused_delta(struct object_entry
*entry
)
2252 unsigned *idx
= &to_pack
.objects
[entry
->delta_idx
- 1].delta_child_idx
;
2253 struct object_info oi
= OBJECT_INFO_INIT
;
2254 enum object_type type
;
2258 struct object_entry
*oe
= &to_pack
.objects
[*idx
- 1];
2261 *idx
= oe
->delta_sibling_idx
;
2263 idx
= &oe
->delta_sibling_idx
;
2265 SET_DELTA(entry
, NULL
);
2270 if (packed_object_info(the_repository
, IN_PACK(entry
), entry
->in_pack_offset
, &oi
) < 0) {
2272 * We failed to get the info from this pack for some reason;
2273 * fall back to oid_object_info, which may find another copy.
2274 * And if that fails, the error will be recorded in oe_type(entry)
2275 * and dealt with in prepare_pack().
2278 oid_object_info(the_repository
, &entry
->idx
.oid
, &size
));
2280 oe_set_type(entry
, type
);
2282 SET_SIZE(entry
, size
);
2286 * Follow the chain of deltas from this entry onward, throwing away any links
2287 * that cause us to hit a cycle (as determined by the DFS state flags in
2290 * We also detect too-long reused chains that would violate our --depth
2293 static void break_delta_chains(struct object_entry
*entry
)
2296 * The actual depth of each object we will write is stored as an int,
2297 * as it cannot exceed our int "depth" limit. But before we break
2298 * changes based no that limit, we may potentially go as deep as the
2299 * number of objects, which is elsewhere bounded to a uint32_t.
2301 uint32_t total_depth
;
2302 struct object_entry
*cur
, *next
;
2304 for (cur
= entry
, total_depth
= 0;
2306 cur
= DELTA(cur
), total_depth
++) {
2307 if (cur
->dfs_state
== DFS_DONE
) {
2309 * We've already seen this object and know it isn't
2310 * part of a cycle. We do need to append its depth
2313 total_depth
+= cur
->depth
;
2318 * We break cycles before looping, so an ACTIVE state (or any
2319 * other cruft which made its way into the state variable)
2322 if (cur
->dfs_state
!= DFS_NONE
)
2323 BUG("confusing delta dfs state in first pass: %d",
2327 * Now we know this is the first time we've seen the object. If
2328 * it's not a delta, we're done traversing, but we'll mark it
2329 * done to save time on future traversals.
2332 cur
->dfs_state
= DFS_DONE
;
2337 * Mark ourselves as active and see if the next step causes
2338 * us to cycle to another active object. It's important to do
2339 * this _before_ we loop, because it impacts where we make the
2340 * cut, and thus how our total_depth counter works.
2341 * E.g., We may see a partial loop like:
2343 * A -> B -> C -> D -> B
2345 * Cutting B->C breaks the cycle. But now the depth of A is
2346 * only 1, and our total_depth counter is at 3. The size of the
2347 * error is always one less than the size of the cycle we
2348 * broke. Commits C and D were "lost" from A's chain.
2350 * If we instead cut D->B, then the depth of A is correct at 3.
2351 * We keep all commits in the chain that we examined.
2353 cur
->dfs_state
= DFS_ACTIVE
;
2354 if (DELTA(cur
)->dfs_state
== DFS_ACTIVE
) {
2355 drop_reused_delta(cur
);
2356 cur
->dfs_state
= DFS_DONE
;
2362 * And now that we've gone all the way to the bottom of the chain, we
2363 * need to clear the active flags and set the depth fields as
2364 * appropriate. Unlike the loop above, which can quit when it drops a
2365 * delta, we need to keep going to look for more depth cuts. So we need
2366 * an extra "next" pointer to keep going after we reset cur->delta.
2368 for (cur
= entry
; cur
; cur
= next
) {
2372 * We should have a chain of zero or more ACTIVE states down to
2373 * a final DONE. We can quit after the DONE, because either it
2374 * has no bases, or we've already handled them in a previous
2377 if (cur
->dfs_state
== DFS_DONE
)
2379 else if (cur
->dfs_state
!= DFS_ACTIVE
)
2380 BUG("confusing delta dfs state in second pass: %d",
2384 * If the total_depth is more than depth, then we need to snip
2385 * the chain into two or more smaller chains that don't exceed
2386 * the maximum depth. Most of the resulting chains will contain
2387 * (depth + 1) entries (i.e., depth deltas plus one base), and
2388 * the last chain (i.e., the one containing entry) will contain
2389 * whatever entries are left over, namely
2390 * (total_depth % (depth + 1)) of them.
2392 * Since we are iterating towards decreasing depth, we need to
2393 * decrement total_depth as we go, and we need to write to the
2394 * entry what its final depth will be after all of the
2395 * snipping. Since we're snipping into chains of length (depth
2396 * + 1) entries, the final depth of an entry will be its
2397 * original depth modulo (depth + 1). Any time we encounter an
2398 * entry whose final depth is supposed to be zero, we snip it
2399 * from its delta base, thereby making it so.
2401 cur
->depth
= (total_depth
--) % (depth
+ 1);
2403 drop_reused_delta(cur
);
2405 cur
->dfs_state
= DFS_DONE
;
2409 static void get_object_details(void)
2412 struct object_entry
**sorted_by_offset
;
2415 progress_state
= start_progress(_("Counting objects"),
2416 to_pack
.nr_objects
);
2418 CALLOC_ARRAY(sorted_by_offset
, to_pack
.nr_objects
);
2419 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2420 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
2421 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
2423 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2424 struct object_entry
*entry
= sorted_by_offset
[i
];
2425 check_object(entry
, i
);
2426 if (entry
->type_valid
&&
2427 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
))
2428 entry
->no_try_delta
= 1;
2429 display_progress(progress_state
, i
+ 1);
2431 stop_progress(&progress_state
);
2434 * This must happen in a second pass, since we rely on the delta
2435 * information for the whole list being completed.
2437 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2438 break_delta_chains(&to_pack
.objects
[i
]);
2440 free(sorted_by_offset
);
2444 * We search for deltas in a list sorted by type, by filename hash, and then
2445 * by size, so that we see progressively smaller and smaller files.
2446 * That's because we prefer deltas to be from the bigger file
2447 * to the smaller -- deletes are potentially cheaper, but perhaps
2448 * more importantly, the bigger file is likely the more recent
2449 * one. The deepest deltas are therefore the oldest objects which are
2450 * less susceptible to be accessed often.
2452 static int type_size_sort(const void *_a
, const void *_b
)
2454 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2455 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2456 const enum object_type a_type
= oe_type(a
);
2457 const enum object_type b_type
= oe_type(b
);
2458 const unsigned long a_size
= SIZE(a
);
2459 const unsigned long b_size
= SIZE(b
);
2461 if (a_type
> b_type
)
2463 if (a_type
< b_type
)
2465 if (a
->hash
> b
->hash
)
2467 if (a
->hash
< b
->hash
)
2469 if (a
->preferred_base
> b
->preferred_base
)
2471 if (a
->preferred_base
< b
->preferred_base
)
2473 if (use_delta_islands
) {
2474 const int island_cmp
= island_delta_cmp(&a
->idx
.oid
, &b
->idx
.oid
);
2478 if (a_size
> b_size
)
2480 if (a_size
< b_size
)
2482 return a
< b
? -1 : (a
> b
); /* newest first */
2486 struct object_entry
*entry
;
2488 struct delta_index
*index
;
2492 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
2493 unsigned long delta_size
)
2495 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
2498 if (delta_size
< cache_max_small_delta_size
)
2501 /* cache delta, if objects are large enough compared to delta size */
2502 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
2508 /* Protect delta_cache_size */
2509 static pthread_mutex_t cache_mutex
;
2510 #define cache_lock() pthread_mutex_lock(&cache_mutex)
2511 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2514 * Protect object list partitioning (e.g. struct thread_param) and
2517 static pthread_mutex_t progress_mutex
;
2518 #define progress_lock() pthread_mutex_lock(&progress_mutex)
2519 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2522 * Access to struct object_entry is unprotected since each thread owns
2523 * a portion of the main object list. Just don't access object entries
2524 * ahead in the list because they can be stolen and would need
2525 * progress_mutex for protection.
2528 static inline int oe_size_less_than(struct packing_data
*pack
,
2529 const struct object_entry
*lhs
,
2532 if (lhs
->size_valid
)
2533 return lhs
->size_
< rhs
;
2534 if (rhs
< pack
->oe_size_limit
) /* rhs < 2^x <= lhs ? */
2536 return oe_get_size_slow(pack
, lhs
) < rhs
;
2539 static inline void oe_set_tree_depth(struct packing_data
*pack
,
2540 struct object_entry
*e
,
2541 unsigned int tree_depth
)
2543 if (!pack
->tree_depth
)
2544 CALLOC_ARRAY(pack
->tree_depth
, pack
->nr_alloc
);
2545 pack
->tree_depth
[e
- pack
->objects
] = tree_depth
;
2549 * Return the size of the object without doing any delta
2550 * reconstruction (so non-deltas are true object sizes, but deltas
2551 * return the size of the delta data).
2553 unsigned long oe_get_size_slow(struct packing_data
*pack
,
2554 const struct object_entry
*e
)
2556 struct packed_git
*p
;
2557 struct pack_window
*w_curs
;
2559 enum object_type type
;
2560 unsigned long used
, avail
, size
;
2562 if (e
->type_
!= OBJ_OFS_DELTA
&& e
->type_
!= OBJ_REF_DELTA
) {
2563 packing_data_lock(&to_pack
);
2564 if (oid_object_info(the_repository
, &e
->idx
.oid
, &size
) < 0)
2565 die(_("unable to get size of %s"),
2566 oid_to_hex(&e
->idx
.oid
));
2567 packing_data_unlock(&to_pack
);
2571 p
= oe_in_pack(pack
, e
);
2573 BUG("when e->type is a delta, it must belong to a pack");
2575 packing_data_lock(&to_pack
);
2577 buf
= use_pack(p
, &w_curs
, e
->in_pack_offset
, &avail
);
2578 used
= unpack_object_header_buffer(buf
, avail
, &type
, &size
);
2580 die(_("unable to parse object header of %s"),
2581 oid_to_hex(&e
->idx
.oid
));
2583 unuse_pack(&w_curs
);
2584 packing_data_unlock(&to_pack
);
2588 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
2589 unsigned max_depth
, unsigned long *mem_usage
)
2591 struct object_entry
*trg_entry
= trg
->entry
;
2592 struct object_entry
*src_entry
= src
->entry
;
2593 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
2595 enum object_type type
;
2598 /* Don't bother doing diffs between different types */
2599 if (oe_type(trg_entry
) != oe_type(src_entry
))
2603 * We do not bother to try a delta that we discarded on an
2604 * earlier try, but only when reusing delta data. Note that
2605 * src_entry that is marked as the preferred_base should always
2606 * be considered, as even if we produce a suboptimal delta against
2607 * it, we will still save the transfer cost, as we already know
2608 * the other side has it and we won't send src_entry at all.
2610 if (reuse_delta
&& IN_PACK(trg_entry
) &&
2611 IN_PACK(trg_entry
) == IN_PACK(src_entry
) &&
2612 !src_entry
->preferred_base
&&
2613 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
2614 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
2617 /* Let's not bust the allowed depth. */
2618 if (src
->depth
>= max_depth
)
2621 /* Now some size filtering heuristics. */
2622 trg_size
= SIZE(trg_entry
);
2623 if (!DELTA(trg_entry
)) {
2624 max_size
= trg_size
/2 - the_hash_algo
->rawsz
;
2627 max_size
= DELTA_SIZE(trg_entry
);
2628 ref_depth
= trg
->depth
;
2630 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
2631 (max_depth
- ref_depth
+ 1);
2634 src_size
= SIZE(src_entry
);
2635 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
2636 if (sizediff
>= max_size
)
2638 if (trg_size
< src_size
/ 32)
2641 if (!in_same_island(&trg
->entry
->idx
.oid
, &src
->entry
->idx
.oid
))
2644 /* Load data if not already done */
2646 packing_data_lock(&to_pack
);
2647 trg
->data
= repo_read_object_file(the_repository
,
2648 &trg_entry
->idx
.oid
, &type
,
2650 packing_data_unlock(&to_pack
);
2652 die(_("object %s cannot be read"),
2653 oid_to_hex(&trg_entry
->idx
.oid
));
2655 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2656 oid_to_hex(&trg_entry
->idx
.oid
), (uintmax_t)sz
,
2657 (uintmax_t)trg_size
);
2661 packing_data_lock(&to_pack
);
2662 src
->data
= repo_read_object_file(the_repository
,
2663 &src_entry
->idx
.oid
, &type
,
2665 packing_data_unlock(&to_pack
);
2667 if (src_entry
->preferred_base
) {
2668 static int warned
= 0;
2670 warning(_("object %s cannot be read"),
2671 oid_to_hex(&src_entry
->idx
.oid
));
2673 * Those objects are not included in the
2674 * resulting pack. Be resilient and ignore
2675 * them if they can't be read, in case the
2676 * pack could be created nevertheless.
2680 die(_("object %s cannot be read"),
2681 oid_to_hex(&src_entry
->idx
.oid
));
2684 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2685 oid_to_hex(&src_entry
->idx
.oid
), (uintmax_t)sz
,
2686 (uintmax_t)src_size
);
2690 src
->index
= create_delta_index(src
->data
, src_size
);
2692 static int warned
= 0;
2694 warning(_("suboptimal pack - out of memory"));
2697 *mem_usage
+= sizeof_delta_index(src
->index
);
2700 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
2704 if (DELTA(trg_entry
)) {
2705 /* Prefer only shallower same-sized deltas. */
2706 if (delta_size
== DELTA_SIZE(trg_entry
) &&
2707 src
->depth
+ 1 >= trg
->depth
) {
2714 * Handle memory allocation outside of the cache
2715 * accounting lock. Compiler will optimize the strangeness
2716 * away when NO_PTHREADS is defined.
2718 free(trg_entry
->delta_data
);
2720 if (trg_entry
->delta_data
) {
2721 delta_cache_size
-= DELTA_SIZE(trg_entry
);
2722 trg_entry
->delta_data
= NULL
;
2724 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
2725 delta_cache_size
+= delta_size
;
2727 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
2733 SET_DELTA(trg_entry
, src_entry
);
2734 SET_DELTA_SIZE(trg_entry
, delta_size
);
2735 trg
->depth
= src
->depth
+ 1;
2740 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
2742 struct object_entry
*child
= DELTA_CHILD(me
);
2745 const unsigned int c
= check_delta_limit(child
, n
+ 1);
2748 child
= DELTA_SIBLING(child
);
2753 static unsigned long free_unpacked(struct unpacked
*n
)
2755 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
2756 free_delta_index(n
->index
);
2759 freed_mem
+= SIZE(n
->entry
);
2760 FREE_AND_NULL(n
->data
);
2767 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
2768 int window
, int depth
, unsigned *processed
)
2770 uint32_t i
, idx
= 0, count
= 0;
2771 struct unpacked
*array
;
2772 unsigned long mem_usage
= 0;
2774 CALLOC_ARRAY(array
, window
);
2777 struct object_entry
*entry
;
2778 struct unpacked
*n
= array
+ idx
;
2779 int j
, max_depth
, best_base
= -1;
2788 if (!entry
->preferred_base
) {
2790 display_progress(progress_state
, *processed
);
2794 mem_usage
-= free_unpacked(n
);
2797 while (window_memory_limit
&&
2798 mem_usage
> window_memory_limit
&&
2800 const uint32_t tail
= (idx
+ window
- count
) % window
;
2801 mem_usage
-= free_unpacked(array
+ tail
);
2805 /* We do not compute delta to *create* objects we are not
2808 if (entry
->preferred_base
)
2812 * If the current object is at pack edge, take the depth the
2813 * objects that depend on the current object into account
2814 * otherwise they would become too deep.
2817 if (DELTA_CHILD(entry
)) {
2818 max_depth
-= check_delta_limit(entry
, 0);
2826 uint32_t other_idx
= idx
+ j
;
2828 if (other_idx
>= window
)
2829 other_idx
-= window
;
2830 m
= array
+ other_idx
;
2833 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2837 best_base
= other_idx
;
2841 * If we decided to cache the delta data, then it is best
2842 * to compress it right away. First because we have to do
2843 * it anyway, and doing it here while we're threaded will
2844 * save a lot of time in the non threaded write phase,
2845 * as well as allow for caching more deltas within
2846 * the same cache size limit.
2848 * But only if not writing to stdout, since in that case
2849 * the network is most likely throttling writes anyway,
2850 * and therefore it is best to go to the write phase ASAP
2851 * instead, as we can afford spending more time compressing
2852 * between writes at that moment.
2854 if (entry
->delta_data
&& !pack_to_stdout
) {
2857 size
= do_compress(&entry
->delta_data
, DELTA_SIZE(entry
));
2858 if (size
< (1U << OE_Z_DELTA_BITS
)) {
2859 entry
->z_delta_size
= size
;
2861 delta_cache_size
-= DELTA_SIZE(entry
);
2862 delta_cache_size
+= entry
->z_delta_size
;
2865 FREE_AND_NULL(entry
->delta_data
);
2866 entry
->z_delta_size
= 0;
2870 /* if we made n a delta, and if n is already at max
2871 * depth, leaving it in the window is pointless. we
2872 * should evict it first.
2874 if (DELTA(entry
) && max_depth
<= n
->depth
)
2878 * Move the best delta base up in the window, after the
2879 * currently deltified object, to keep it longer. It will
2880 * be the first base object to be attempted next.
2883 struct unpacked swap
= array
[best_base
];
2884 int dist
= (window
+ idx
- best_base
) % window
;
2885 int dst
= best_base
;
2887 int src
= (dst
+ 1) % window
;
2888 array
[dst
] = array
[src
];
2896 if (count
+ 1 < window
)
2902 for (i
= 0; i
< window
; ++i
) {
2903 free_delta_index(array
[i
].index
);
2904 free(array
[i
].data
);
2910 * The main object list is split into smaller lists, each is handed to
2913 * The main thread waits on the condition that (at least) one of the workers
2914 * has stopped working (which is indicated in the .working member of
2915 * struct thread_params).
2917 * When a work thread has completed its work, it sets .working to 0 and
2918 * signals the main thread and waits on the condition that .data_ready
2921 * The main thread steals half of the work from the worker that has
2922 * most work left to hand it to the idle worker.
2925 struct thread_params
{
2927 struct object_entry
**list
;
2934 pthread_mutex_t mutex
;
2935 pthread_cond_t cond
;
2936 unsigned *processed
;
2939 static pthread_cond_t progress_cond
;
2942 * Mutex and conditional variable can't be statically-initialized on Windows.
2944 static void init_threaded_search(void)
2946 pthread_mutex_init(&cache_mutex
, NULL
);
2947 pthread_mutex_init(&progress_mutex
, NULL
);
2948 pthread_cond_init(&progress_cond
, NULL
);
2951 static void cleanup_threaded_search(void)
2953 pthread_cond_destroy(&progress_cond
);
2954 pthread_mutex_destroy(&cache_mutex
);
2955 pthread_mutex_destroy(&progress_mutex
);
2958 static void *threaded_find_deltas(void *arg
)
2960 struct thread_params
*me
= arg
;
2963 while (me
->remaining
) {
2966 find_deltas(me
->list
, &me
->remaining
,
2967 me
->window
, me
->depth
, me
->processed
);
2971 pthread_cond_signal(&progress_cond
);
2975 * We must not set ->data_ready before we wait on the
2976 * condition because the main thread may have set it to 1
2977 * before we get here. In order to be sure that new
2978 * work is available if we see 1 in ->data_ready, it
2979 * was initialized to 0 before this thread was spawned
2980 * and we reset it to 0 right away.
2982 pthread_mutex_lock(&me
->mutex
);
2983 while (!me
->data_ready
)
2984 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2986 pthread_mutex_unlock(&me
->mutex
);
2991 /* leave ->working 1 so that this doesn't get more work assigned */
2995 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2996 int window
, int depth
, unsigned *processed
)
2998 struct thread_params
*p
;
2999 int i
, ret
, active_threads
= 0;
3001 init_threaded_search();
3003 if (delta_search_threads
<= 1) {
3004 find_deltas(list
, &list_size
, window
, depth
, processed
);
3005 cleanup_threaded_search();
3008 if (progress
> pack_to_stdout
)
3009 fprintf_ln(stderr
, _("Delta compression using up to %d threads"),
3010 delta_search_threads
);
3011 CALLOC_ARRAY(p
, delta_search_threads
);
3013 /* Partition the work amongst work threads. */
3014 for (i
= 0; i
< delta_search_threads
; i
++) {
3015 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
3017 /* don't use too small segments or no deltas will be found */
3018 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
3021 p
[i
].window
= window
;
3023 p
[i
].processed
= processed
;
3025 p
[i
].data_ready
= 0;
3027 /* try to split chunks on "path" boundaries */
3028 while (sub_size
&& sub_size
< list_size
&&
3029 list
[sub_size
]->hash
&&
3030 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
3034 p
[i
].list_size
= sub_size
;
3035 p
[i
].remaining
= sub_size
;
3038 list_size
-= sub_size
;
3041 /* Start work threads. */
3042 for (i
= 0; i
< delta_search_threads
; i
++) {
3043 if (!p
[i
].list_size
)
3045 pthread_mutex_init(&p
[i
].mutex
, NULL
);
3046 pthread_cond_init(&p
[i
].cond
, NULL
);
3047 ret
= pthread_create(&p
[i
].thread
, NULL
,
3048 threaded_find_deltas
, &p
[i
]);
3050 die(_("unable to create thread: %s"), strerror(ret
));
3055 * Now let's wait for work completion. Each time a thread is done
3056 * with its work, we steal half of the remaining work from the
3057 * thread with the largest number of unprocessed objects and give
3058 * it to that newly idle thread. This ensure good load balancing
3059 * until the remaining object list segments are simply too short
3060 * to be worth splitting anymore.
3062 while (active_threads
) {
3063 struct thread_params
*target
= NULL
;
3064 struct thread_params
*victim
= NULL
;
3065 unsigned sub_size
= 0;
3069 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
3074 pthread_cond_wait(&progress_cond
, &progress_mutex
);
3077 for (i
= 0; i
< delta_search_threads
; i
++)
3078 if (p
[i
].remaining
> 2*window
&&
3079 (!victim
|| victim
->remaining
< p
[i
].remaining
))
3082 sub_size
= victim
->remaining
/ 2;
3083 list
= victim
->list
+ victim
->list_size
- sub_size
;
3084 while (sub_size
&& list
[0]->hash
&&
3085 list
[0]->hash
== list
[-1]->hash
) {
3091 * It is possible for some "paths" to have
3092 * so many objects that no hash boundary
3093 * might be found. Let's just steal the
3094 * exact half in that case.
3096 sub_size
= victim
->remaining
/ 2;
3099 target
->list
= list
;
3100 victim
->list_size
-= sub_size
;
3101 victim
->remaining
-= sub_size
;
3103 target
->list_size
= sub_size
;
3104 target
->remaining
= sub_size
;
3105 target
->working
= 1;
3108 pthread_mutex_lock(&target
->mutex
);
3109 target
->data_ready
= 1;
3110 pthread_cond_signal(&target
->cond
);
3111 pthread_mutex_unlock(&target
->mutex
);
3114 pthread_join(target
->thread
, NULL
);
3115 pthread_cond_destroy(&target
->cond
);
3116 pthread_mutex_destroy(&target
->mutex
);
3120 cleanup_threaded_search();
3124 static int obj_is_packed(const struct object_id
*oid
)
3126 return packlist_find(&to_pack
, oid
) ||
3127 (reuse_packfile_bitmap
&&
3128 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
));
3131 static void add_tag_chain(const struct object_id
*oid
)
3136 * We catch duplicates already in add_object_entry(), but we'd
3137 * prefer to do this extra check to avoid having to parse the
3138 * tag at all if we already know that it's being packed (e.g., if
3139 * it was included via bitmaps, we would not have parsed it
3142 if (obj_is_packed(oid
))
3145 tag
= lookup_tag(the_repository
, oid
);
3147 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
3148 die(_("unable to pack objects reachable from tag %s"),
3151 add_object_entry(&tag
->object
.oid
, OBJ_TAG
, NULL
, 0);
3153 if (tag
->tagged
->type
!= OBJ_TAG
)
3156 tag
= (struct tag
*)tag
->tagged
;
3160 static int add_ref_tag(const char *tag UNUSED
, const char *referent UNUSED
, const struct object_id
*oid
,
3161 int flag UNUSED
, void *cb_data UNUSED
)
3163 struct object_id peeled
;
3165 if (!peel_iterated_oid(the_repository
, oid
, &peeled
) && obj_is_packed(&peeled
))
3170 static void prepare_pack(int window
, int depth
)
3172 struct object_entry
**delta_list
;
3173 uint32_t i
, nr_deltas
;
3176 if (use_delta_islands
)
3177 resolve_tree_islands(the_repository
, progress
, &to_pack
);
3179 get_object_details();
3182 * If we're locally repacking then we need to be doubly careful
3183 * from now on in order to make sure no stealth corruption gets
3184 * propagated to the new pack. Clients receiving streamed packs
3185 * should validate everything they get anyway so no need to incur
3186 * the additional cost here in that case.
3188 if (!pack_to_stdout
)
3189 do_check_packed_object_crc
= 1;
3191 if (!to_pack
.nr_objects
|| !window
|| !depth
)
3194 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
3197 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
3198 struct object_entry
*entry
= to_pack
.objects
+ i
;
3201 /* This happens if we decided to reuse existing
3202 * delta from a pack. "reuse_delta &&" is implied.
3206 if (!entry
->type_valid
||
3207 oe_size_less_than(&to_pack
, entry
, 50))
3210 if (entry
->no_try_delta
)
3213 if (!entry
->preferred_base
) {
3215 if (oe_type(entry
) < 0)
3216 die(_("unable to get type of object %s"),
3217 oid_to_hex(&entry
->idx
.oid
));
3219 if (oe_type(entry
) < 0) {
3221 * This object is not found, but we
3222 * don't have to include it anyway.
3228 delta_list
[n
++] = entry
;
3231 if (nr_deltas
&& n
> 1) {
3232 unsigned nr_done
= 0;
3235 progress_state
= start_progress(_("Compressing objects"),
3237 QSORT(delta_list
, n
, type_size_sort
);
3238 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
3239 stop_progress(&progress_state
);
3240 if (nr_done
!= nr_deltas
)
3241 die(_("inconsistency with delta count"));
3246 static int git_pack_config(const char *k
, const char *v
,
3247 const struct config_context
*ctx
, void *cb
)
3249 if (!strcmp(k
, "pack.window")) {
3250 window
= git_config_int(k
, v
, ctx
->kvi
);
3253 if (!strcmp(k
, "pack.windowmemory")) {
3254 window_memory_limit
= git_config_ulong(k
, v
, ctx
->kvi
);
3257 if (!strcmp(k
, "pack.depth")) {
3258 depth
= git_config_int(k
, v
, ctx
->kvi
);
3261 if (!strcmp(k
, "pack.deltacachesize")) {
3262 max_delta_cache_size
= git_config_int(k
, v
, ctx
->kvi
);
3265 if (!strcmp(k
, "pack.deltacachelimit")) {
3266 cache_max_small_delta_size
= git_config_int(k
, v
, ctx
->kvi
);
3269 if (!strcmp(k
, "pack.writebitmaphashcache")) {
3270 if (git_config_bool(k
, v
))
3271 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
3273 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
3276 if (!strcmp(k
, "pack.writebitmaplookuptable")) {
3277 if (git_config_bool(k
, v
))
3278 write_bitmap_options
|= BITMAP_OPT_LOOKUP_TABLE
;
3280 write_bitmap_options
&= ~BITMAP_OPT_LOOKUP_TABLE
;
3283 if (!strcmp(k
, "pack.usebitmaps")) {
3284 use_bitmap_index_default
= git_config_bool(k
, v
);
3287 if (!strcmp(k
, "pack.allowpackreuse")) {
3288 int res
= git_parse_maybe_bool_text(v
);
3290 if (!strcasecmp(v
, "single"))
3291 allow_pack_reuse
= SINGLE_PACK_REUSE
;
3292 else if (!strcasecmp(v
, "multi"))
3293 allow_pack_reuse
= MULTI_PACK_REUSE
;
3295 die(_("invalid pack.allowPackReuse value: '%s'"), v
);
3297 allow_pack_reuse
= SINGLE_PACK_REUSE
;
3299 allow_pack_reuse
= NO_PACK_REUSE
;
3303 if (!strcmp(k
, "pack.threads")) {
3304 delta_search_threads
= git_config_int(k
, v
, ctx
->kvi
);
3305 if (delta_search_threads
< 0)
3306 die(_("invalid number of threads specified (%d)"),
3307 delta_search_threads
);
3308 if (!HAVE_THREADS
&& delta_search_threads
!= 1) {
3309 warning(_("no threads support, ignoring %s"), k
);
3310 delta_search_threads
= 0;
3314 if (!strcmp(k
, "pack.indexversion")) {
3315 pack_idx_opts
.version
= git_config_int(k
, v
, ctx
->kvi
);
3316 if (pack_idx_opts
.version
> 2)
3317 die(_("bad pack.indexVersion=%"PRIu32
),
3318 pack_idx_opts
.version
);
3321 if (!strcmp(k
, "pack.writereverseindex")) {
3322 if (git_config_bool(k
, v
))
3323 pack_idx_opts
.flags
|= WRITE_REV
;
3325 pack_idx_opts
.flags
&= ~WRITE_REV
;
3328 if (!strcmp(k
, "uploadpack.blobpackfileuri")) {
3329 struct configured_exclusion
*ex
;
3330 const char *oid_end
, *pack_end
;
3332 * Stores the pack hash. This is not a true object ID, but is
3335 struct object_id pack_hash
;
3338 return config_error_nonbool(k
);
3340 ex
= xmalloc(sizeof(*ex
));
3341 if (parse_oid_hex(v
, &ex
->e
.oid
, &oid_end
) ||
3343 parse_oid_hex(oid_end
+ 1, &pack_hash
, &pack_end
) ||
3345 die(_("value of uploadpack.blobpackfileuri must be "
3346 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v
);
3347 if (oidmap_get(&configured_exclusions
, &ex
->e
.oid
))
3348 die(_("object already configured in another "
3349 "uploadpack.blobpackfileuri (got '%s')"), v
);
3350 ex
->pack_hash_hex
= xcalloc(1, pack_end
- oid_end
);
3351 memcpy(ex
->pack_hash_hex
, oid_end
+ 1, pack_end
- oid_end
- 1);
3352 ex
->uri
= xstrdup(pack_end
+ 1);
3353 oidmap_put(&configured_exclusions
, ex
);
3355 return git_default_config(k
, v
, ctx
, cb
);
3358 /* Counters for trace2 output when in --stdin-packs mode. */
3359 static int stdin_packs_found_nr
;
3360 static int stdin_packs_hints_nr
;
3362 static int add_object_entry_from_pack(const struct object_id
*oid
,
3363 struct packed_git
*p
,
3368 enum object_type type
= OBJ_NONE
;
3370 display_progress(progress_state
, ++nr_seen
);
3372 if (have_duplicate_entry(oid
, 0))
3375 ofs
= nth_packed_object_offset(p
, pos
);
3376 if (!want_object_in_pack(oid
, 0, &p
, &ofs
))
3380 struct rev_info
*revs
= _data
;
3381 struct object_info oi
= OBJECT_INFO_INIT
;
3384 if (packed_object_info(the_repository
, p
, ofs
, &oi
) < 0) {
3385 die(_("could not get type of object %s in pack %s"),
3386 oid_to_hex(oid
), p
->pack_name
);
3387 } else if (type
== OBJ_COMMIT
) {
3389 * commits in included packs are used as starting points for the
3390 * subsequent revision walk
3392 add_pending_oid(revs
, NULL
, oid
, 0);
3395 stdin_packs_found_nr
++;
3398 create_object_entry(oid
, type
, 0, 0, 0, p
, ofs
);
3403 static void show_commit_pack_hint(struct commit
*commit UNUSED
,
3406 /* nothing to do; commits don't have a namehash */
3409 static void show_object_pack_hint(struct object
*object
, const char *name
,
3412 struct object_entry
*oe
= packlist_find(&to_pack
, &object
->oid
);
3417 * Our 'to_pack' list was constructed by iterating all objects packed in
3418 * included packs, and so doesn't have a non-zero hash field that you
3419 * would typically pick up during a reachability traversal.
3421 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3422 * here using a now in order to perhaps improve the delta selection
3425 oe
->hash
= pack_name_hash(name
);
3426 oe
->no_try_delta
= name
&& no_try_delta(name
);
3428 stdin_packs_hints_nr
++;
3431 static int pack_mtime_cmp(const void *_a
, const void *_b
)
3433 struct packed_git
*a
= ((const struct string_list_item
*)_a
)->util
;
3434 struct packed_git
*b
= ((const struct string_list_item
*)_b
)->util
;
3437 * order packs by descending mtime so that objects are laid out
3438 * roughly as newest-to-oldest
3440 if (a
->mtime
< b
->mtime
)
3442 else if (b
->mtime
< a
->mtime
)
3448 static void read_packs_list_from_stdin(void)
3450 struct strbuf buf
= STRBUF_INIT
;
3451 struct string_list include_packs
= STRING_LIST_INIT_DUP
;
3452 struct string_list exclude_packs
= STRING_LIST_INIT_DUP
;
3453 struct string_list_item
*item
= NULL
;
3455 struct packed_git
*p
;
3456 struct rev_info revs
;
3458 repo_init_revisions(the_repository
, &revs
, NULL
);
3460 * Use a revision walk to fill in the namehash of objects in the include
3461 * packs. To save time, we'll avoid traversing through objects that are
3462 * in excluded packs.
3464 * That may cause us to avoid populating all of the namehash fields of
3465 * all included objects, but our goal is best-effort, since this is only
3466 * an optimization during delta selection.
3468 revs
.no_kept_objects
= 1;
3469 revs
.keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
3470 revs
.blob_objects
= 1;
3471 revs
.tree_objects
= 1;
3472 revs
.tag_objects
= 1;
3473 revs
.ignore_missing_links
= 1;
3475 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3479 if (*buf
.buf
== '^')
3480 string_list_append(&exclude_packs
, buf
.buf
+ 1);
3482 string_list_append(&include_packs
, buf
.buf
);
3487 string_list_sort(&include_packs
);
3488 string_list_remove_duplicates(&include_packs
, 0);
3489 string_list_sort(&exclude_packs
);
3490 string_list_remove_duplicates(&exclude_packs
, 0);
3492 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3493 const char *pack_name
= pack_basename(p
);
3495 if ((item
= string_list_lookup(&include_packs
, pack_name
)))
3497 if ((item
= string_list_lookup(&exclude_packs
, pack_name
)))
3502 * Arguments we got on stdin may not even be packs. First
3503 * check that to avoid segfaulting later on in
3504 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3506 * Since we first parsed our STDIN and then sorted the input
3507 * lines the pack we error on will be whatever line happens to
3508 * sort first. This is lazy, it's enough that we report one
3509 * bad case here, we don't need to report the first/last one,
3512 for_each_string_list_item(item
, &include_packs
) {
3513 struct packed_git
*p
= item
->util
;
3515 die(_("could not find pack '%s'"), item
->string
);
3516 if (!is_pack_valid(p
))
3517 die(_("packfile %s cannot be accessed"), p
->pack_name
);
3521 * Then, handle all of the excluded packs, marking them as
3522 * kept in-core so that later calls to add_object_entry()
3523 * discards any objects that are also found in excluded packs.
3525 for_each_string_list_item(item
, &exclude_packs
) {
3526 struct packed_git
*p
= item
->util
;
3528 die(_("could not find pack '%s'"), item
->string
);
3529 p
->pack_keep_in_core
= 1;
3533 * Order packs by ascending mtime; use QSORT directly to access the
3534 * string_list_item's ->util pointer, which string_list_sort() does not
3537 QSORT(include_packs
.items
, include_packs
.nr
, pack_mtime_cmp
);
3539 for_each_string_list_item(item
, &include_packs
) {
3540 struct packed_git
*p
= item
->util
;
3541 for_each_object_in_pack(p
,
3542 add_object_entry_from_pack
,
3544 FOR_EACH_OBJECT_PACK_ORDER
);
3547 if (prepare_revision_walk(&revs
))
3548 die(_("revision walk setup failed"));
3549 traverse_commit_list(&revs
,
3550 show_commit_pack_hint
,
3551 show_object_pack_hint
,
3554 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_found",
3555 stdin_packs_found_nr
);
3556 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_hints",
3557 stdin_packs_hints_nr
);
3559 strbuf_release(&buf
);
3560 string_list_clear(&include_packs
, 0);
3561 string_list_clear(&exclude_packs
, 0);
3564 static void add_cruft_object_entry(const struct object_id
*oid
, enum object_type type
,
3565 struct packed_git
*pack
, off_t offset
,
3566 const char *name
, uint32_t mtime
)
3568 struct object_entry
*entry
;
3570 display_progress(progress_state
, ++nr_seen
);
3572 entry
= packlist_find(&to_pack
, oid
);
3575 entry
->hash
= pack_name_hash(name
);
3576 entry
->no_try_delta
= no_try_delta(name
);
3579 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
3581 if (!pack
&& type
== OBJ_BLOB
&& !has_loose_object(oid
)) {
3583 * If a traversed tree has a missing blob then we want
3584 * to avoid adding that missing object to our pack.
3586 * This only applies to missing blobs, not trees,
3587 * because the traversal needs to parse sub-trees but
3590 * Note we only perform this check when we couldn't
3591 * already find the object in a pack, so we're really
3592 * limited to "ensure non-tip blobs which don't exist in
3593 * packs do exist via loose objects". Confused?
3598 entry
= create_object_entry(oid
, type
, pack_name_hash(name
),
3599 0, name
&& no_try_delta(name
),
3603 if (mtime
> oe_cruft_mtime(&to_pack
, entry
))
3604 oe_set_cruft_mtime(&to_pack
, entry
, mtime
);
3608 static void show_cruft_object(struct object
*obj
, const char *name
, void *data UNUSED
)
3611 * if we did not record it earlier, it's at least as old as our
3612 * expiration value. Rather than find it exactly, just use that
3613 * value. This may bump it forward from its real mtime, but it
3614 * will still be "too old" next time we run with the same
3617 * if obj does appear in the packing list, this call is a noop (or may
3618 * set the namehash).
3620 add_cruft_object_entry(&obj
->oid
, obj
->type
, NULL
, 0, name
, cruft_expiration
);
3623 static void show_cruft_commit(struct commit
*commit
, void *data
)
3625 show_cruft_object((struct object
*)commit
, NULL
, data
);
3628 static int cruft_include_check_obj(struct object
*obj
, void *data UNUSED
)
3630 return !has_object_kept_pack(&obj
->oid
, IN_CORE_KEEP_PACKS
);
3633 static int cruft_include_check(struct commit
*commit
, void *data
)
3635 return cruft_include_check_obj((struct object
*)commit
, data
);
3638 static void set_cruft_mtime(const struct object
*object
,
3639 struct packed_git
*pack
,
3640 off_t offset
, time_t mtime
)
3642 add_cruft_object_entry(&object
->oid
, object
->type
, pack
, offset
, NULL
,
3646 static void mark_pack_kept_in_core(struct string_list
*packs
, unsigned keep
)
3648 struct string_list_item
*item
= NULL
;
3649 for_each_string_list_item(item
, packs
) {
3650 struct packed_git
*p
= item
->util
;
3652 die(_("could not find pack '%s'"), item
->string
);
3653 p
->pack_keep_in_core
= keep
;
3657 static void add_unreachable_loose_objects(void);
3658 static void add_objects_in_unpacked_packs(void);
3660 static void enumerate_cruft_objects(void)
3663 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3665 add_objects_in_unpacked_packs();
3666 add_unreachable_loose_objects();
3668 stop_progress(&progress_state
);
3671 static void enumerate_and_traverse_cruft_objects(struct string_list
*fresh_packs
)
3673 struct packed_git
*p
;
3674 struct rev_info revs
;
3677 repo_init_revisions(the_repository
, &revs
, NULL
);
3679 revs
.tag_objects
= 1;
3680 revs
.tree_objects
= 1;
3681 revs
.blob_objects
= 1;
3683 revs
.include_check
= cruft_include_check
;
3684 revs
.include_check_obj
= cruft_include_check_obj
;
3686 revs
.ignore_missing_links
= 1;
3689 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3690 ret
= add_unseen_recent_objects_to_traversal(&revs
, cruft_expiration
,
3691 set_cruft_mtime
, 1);
3692 stop_progress(&progress_state
);
3695 die(_("unable to add cruft objects"));
3698 * Re-mark only the fresh packs as kept so that objects in
3699 * unknown packs do not halt the reachability traversal early.
3701 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
3702 p
->pack_keep_in_core
= 0;
3703 mark_pack_kept_in_core(fresh_packs
, 1);
3705 if (prepare_revision_walk(&revs
))
3706 die(_("revision walk setup failed"));
3708 progress_state
= start_progress(_("Traversing cruft objects"), 0);
3710 traverse_commit_list(&revs
, show_cruft_commit
, show_cruft_object
, NULL
);
3712 stop_progress(&progress_state
);
3715 static void read_cruft_objects(void)
3717 struct strbuf buf
= STRBUF_INIT
;
3718 struct string_list discard_packs
= STRING_LIST_INIT_DUP
;
3719 struct string_list fresh_packs
= STRING_LIST_INIT_DUP
;
3720 struct packed_git
*p
;
3722 ignore_packed_keep_in_core
= 1;
3724 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3728 if (*buf
.buf
== '-')
3729 string_list_append(&discard_packs
, buf
.buf
+ 1);
3731 string_list_append(&fresh_packs
, buf
.buf
);
3734 string_list_sort(&discard_packs
);
3735 string_list_sort(&fresh_packs
);
3737 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3738 const char *pack_name
= pack_basename(p
);
3739 struct string_list_item
*item
;
3741 item
= string_list_lookup(&fresh_packs
, pack_name
);
3743 item
= string_list_lookup(&discard_packs
, pack_name
);
3749 * This pack wasn't mentioned in either the "fresh" or
3750 * "discard" list, so the caller didn't know about it.
3752 * Mark it as kept so that its objects are ignored by
3753 * add_unseen_recent_objects_to_traversal(). We'll
3754 * unmark it before starting the traversal so it doesn't
3755 * halt the traversal early.
3757 p
->pack_keep_in_core
= 1;
3761 mark_pack_kept_in_core(&fresh_packs
, 1);
3762 mark_pack_kept_in_core(&discard_packs
, 0);
3764 if (cruft_expiration
)
3765 enumerate_and_traverse_cruft_objects(&fresh_packs
);
3767 enumerate_cruft_objects();
3769 strbuf_release(&buf
);
3770 string_list_clear(&discard_packs
, 0);
3771 string_list_clear(&fresh_packs
, 0);
3774 static void read_object_list_from_stdin(void)
3776 char line
[GIT_MAX_HEXSZ
+ 1 + PATH_MAX
+ 2];
3777 struct object_id oid
;
3781 if (!fgets(line
, sizeof(line
), stdin
)) {
3785 BUG("fgets returned NULL, not EOF, not error!");
3791 if (line
[0] == '-') {
3792 if (get_oid_hex(line
+1, &oid
))
3793 die(_("expected edge object ID, got garbage:\n %s"),
3795 add_preferred_base(&oid
);
3798 if (parse_oid_hex(line
, &oid
, &p
))
3799 die(_("expected object ID, got garbage:\n %s"), line
);
3801 add_preferred_base_object(p
+ 1);
3802 add_object_entry(&oid
, OBJ_NONE
, p
+ 1, 0);
3806 static void show_commit(struct commit
*commit
, void *data UNUSED
)
3808 add_object_entry(&commit
->object
.oid
, OBJ_COMMIT
, NULL
, 0);
3810 if (write_bitmap_index
)
3811 index_commit_for_bitmap(commit
);
3813 if (use_delta_islands
)
3814 propagate_island_marks(commit
);
3817 static void show_object(struct object
*obj
, const char *name
,
3820 add_preferred_base_object(name
);
3821 add_object_entry(&obj
->oid
, obj
->type
, name
, 0);
3823 if (use_delta_islands
) {
3826 struct object_entry
*ent
;
3828 /* the empty string is a root tree, which is depth 0 */
3829 depth
= *name
? 1 : 0;
3830 for (p
= strchr(name
, '/'); p
; p
= strchr(p
+ 1, '/'))
3833 ent
= packlist_find(&to_pack
, &obj
->oid
);
3834 if (ent
&& depth
> oe_tree_depth(&to_pack
, ent
))
3835 oe_set_tree_depth(&to_pack
, ent
, depth
);
3839 static void show_object__ma_allow_any(struct object
*obj
, const char *name
, void *data
)
3841 assert(arg_missing_action
== MA_ALLOW_ANY
);
3844 * Quietly ignore ALL missing objects. This avoids problems with
3845 * staging them now and getting an odd error later.
3847 if (!has_object(the_repository
, &obj
->oid
, 0))
3850 show_object(obj
, name
, data
);
3853 static void show_object__ma_allow_promisor(struct object
*obj
, const char *name
, void *data
)
3855 assert(arg_missing_action
== MA_ALLOW_PROMISOR
);
3858 * Quietly ignore EXPECTED missing objects. This avoids problems with
3859 * staging them now and getting an odd error later.
3861 if (!has_object(the_repository
, &obj
->oid
, 0) && is_promisor_object(&obj
->oid
))
3864 show_object(obj
, name
, data
);
3867 static int option_parse_missing_action(const struct option
*opt UNUSED
,
3868 const char *arg
, int unset
)
3873 if (!strcmp(arg
, "error")) {
3874 arg_missing_action
= MA_ERROR
;
3875 fn_show_object
= show_object
;
3879 if (!strcmp(arg
, "allow-any")) {
3880 arg_missing_action
= MA_ALLOW_ANY
;
3881 fetch_if_missing
= 0;
3882 fn_show_object
= show_object__ma_allow_any
;
3886 if (!strcmp(arg
, "allow-promisor")) {
3887 arg_missing_action
= MA_ALLOW_PROMISOR
;
3888 fetch_if_missing
= 0;
3889 fn_show_object
= show_object__ma_allow_promisor
;
3893 die(_("invalid value for '%s': '%s'"), "--missing", arg
);
3897 static void show_edge(struct commit
*commit
)
3899 add_preferred_base(&commit
->object
.oid
);
3902 static int add_object_in_unpacked_pack(const struct object_id
*oid
,
3903 struct packed_git
*pack
,
3911 if (pack
->is_cruft
) {
3912 if (load_pack_mtimes(pack
) < 0)
3913 die(_("could not load cruft pack .mtimes"));
3914 mtime
= nth_packed_mtime(pack
, pos
);
3916 mtime
= pack
->mtime
;
3918 offset
= nth_packed_object_offset(pack
, pos
);
3920 add_cruft_object_entry(oid
, OBJ_NONE
, pack
, offset
,
3923 add_object_entry(oid
, OBJ_NONE
, "", 0);
3928 static void add_objects_in_unpacked_packs(void)
3930 if (for_each_packed_object(add_object_in_unpacked_pack
, NULL
,
3931 FOR_EACH_OBJECT_PACK_ORDER
|
3932 FOR_EACH_OBJECT_LOCAL_ONLY
|
3933 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
|
3934 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS
))
3935 die(_("cannot open pack index"));
3938 static int add_loose_object(const struct object_id
*oid
, const char *path
,
3941 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
3944 warning(_("loose object at %s could not be examined"), path
);
3950 if (stat(path
, &st
) < 0) {
3951 if (errno
== ENOENT
)
3953 return error_errno("unable to stat %s", oid_to_hex(oid
));
3956 add_cruft_object_entry(oid
, type
, NULL
, 0, NULL
,
3959 add_object_entry(oid
, type
, "", 0);
3965 * We actually don't even have to worry about reachability here.
3966 * add_object_entry will weed out duplicates, so we just add every
3967 * loose object we find.
3969 static void add_unreachable_loose_objects(void)
3971 for_each_loose_file_in_objdir(repo_get_object_directory(the_repository
),
3976 static int has_sha1_pack_kept_or_nonlocal(const struct object_id
*oid
)
3978 static struct packed_git
*last_found
= (void *)1;
3979 struct packed_git
*p
;
3981 p
= (last_found
!= (void *)1) ? last_found
:
3982 get_all_packs(the_repository
);
3985 if ((!p
->pack_local
|| p
->pack_keep
||
3986 p
->pack_keep_in_core
) &&
3987 find_pack_entry_one(oid
->hash
, p
)) {
3991 if (p
== last_found
)
3992 p
= get_all_packs(the_repository
);
3995 if (p
== last_found
)
4002 * Store a list of sha1s that are should not be discarded
4003 * because they are either written too recently, or are
4004 * reachable from another object that was.
4006 * This is filled by get_object_list.
4008 static struct oid_array recent_objects
;
4010 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
4013 if (!unpack_unreachable_expiration
)
4015 if (mtime
> unpack_unreachable_expiration
)
4017 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
4022 static void loosen_unused_packed_objects(void)
4024 struct packed_git
*p
;
4026 uint32_t loosened_objects_nr
= 0;
4027 struct object_id oid
;
4029 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4030 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
4033 if (open_pack_index(p
))
4034 die(_("cannot open pack index"));
4036 for (i
= 0; i
< p
->num_objects
; i
++) {
4037 nth_packed_object_id(&oid
, p
, i
);
4038 if (!packlist_find(&to_pack
, &oid
) &&
4039 !has_sha1_pack_kept_or_nonlocal(&oid
) &&
4040 !loosened_object_can_be_discarded(&oid
, p
->mtime
)) {
4041 if (force_object_loose(&oid
, p
->mtime
))
4042 die(_("unable to force loose object"));
4043 loosened_objects_nr
++;
4048 trace2_data_intmax("pack-objects", the_repository
,
4049 "loosen_unused_packed_objects/loosened", loosened_objects_nr
);
4053 * This tracks any options which pack-reuse code expects to be on, or which a
4054 * reader of the pack might not understand, and which would therefore prevent
4055 * blind reuse of what we have on disk.
4057 static int pack_options_allow_reuse(void)
4059 return allow_pack_reuse
!= NO_PACK_REUSE
&&
4061 !ignore_packed_keep_on_disk
&&
4062 !ignore_packed_keep_in_core
&&
4063 (!local
|| !have_non_local_packs
) &&
4067 static int get_object_list_from_bitmap(struct rev_info
*revs
)
4069 if (!(bitmap_git
= prepare_bitmap_walk(revs
, 0)))
4072 if (pack_options_allow_reuse())
4073 reuse_partial_packfile_from_bitmap(bitmap_git
,
4075 &reuse_packfiles_nr
,
4076 &reuse_packfile_bitmap
,
4077 allow_pack_reuse
== MULTI_PACK_REUSE
);
4079 if (reuse_packfiles
) {
4080 reuse_packfile_objects
= bitmap_popcount(reuse_packfile_bitmap
);
4081 if (!reuse_packfile_objects
)
4082 BUG("expected non-empty reuse bitmap");
4084 nr_result
+= reuse_packfile_objects
;
4085 nr_seen
+= reuse_packfile_objects
;
4086 display_progress(progress_state
, nr_seen
);
4089 traverse_bitmap_commit_list(bitmap_git
, revs
,
4090 &add_object_entry_from_bitmap
);
4094 static void record_recent_object(struct object
*obj
,
4095 const char *name UNUSED
,
4098 oid_array_append(&recent_objects
, &obj
->oid
);
4101 static void record_recent_commit(struct commit
*commit
, void *data UNUSED
)
4103 oid_array_append(&recent_objects
, &commit
->object
.oid
);
4106 static int mark_bitmap_preferred_tip(const char *refname
,
4107 const char *referent UNUSED
,
4108 const struct object_id
*oid
,
4112 struct object_id peeled
;
4113 struct object
*object
;
4115 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
4118 object
= parse_object_or_die(oid
, refname
);
4119 if (object
->type
== OBJ_COMMIT
)
4120 object
->flags
|= NEEDS_BITMAP
;
4125 static void mark_bitmap_preferred_tips(void)
4127 struct string_list_item
*item
;
4128 const struct string_list
*preferred_tips
;
4130 preferred_tips
= bitmap_preferred_tips(the_repository
);
4131 if (!preferred_tips
)
4134 for_each_string_list_item(item
, preferred_tips
) {
4135 refs_for_each_ref_in(get_main_ref_store(the_repository
),
4136 item
->string
, mark_bitmap_preferred_tip
,
4141 static void get_object_list(struct rev_info
*revs
, int ac
, const char **av
)
4143 struct setup_revision_opt s_r_opt
= {
4144 .allow_exclude_promisor_objects
= 1,
4150 save_commit_buffer
= 0;
4151 setup_revisions(ac
, av
, revs
, &s_r_opt
);
4153 /* make sure shallows are read */
4154 is_repository_shallow(the_repository
);
4156 save_warning
= warn_on_object_refname_ambiguity
;
4157 warn_on_object_refname_ambiguity
= 0;
4159 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
4160 int len
= strlen(line
);
4161 if (len
&& line
[len
- 1] == '\n')
4166 if (!strcmp(line
, "--not")) {
4167 flags
^= UNINTERESTING
;
4168 write_bitmap_index
= 0;
4171 if (starts_with(line
, "--shallow ")) {
4172 struct object_id oid
;
4173 if (get_oid_hex(line
+ 10, &oid
))
4174 die("not an object name '%s'", line
+ 10);
4175 register_shallow(the_repository
, &oid
);
4176 use_bitmap_index
= 0;
4179 die(_("not a rev '%s'"), line
);
4181 if (handle_revision_arg(line
, revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
4182 die(_("bad revision '%s'"), line
);
4185 warn_on_object_refname_ambiguity
= save_warning
;
4187 if (use_bitmap_index
&& !get_object_list_from_bitmap(revs
))
4190 if (use_delta_islands
)
4191 load_delta_islands(the_repository
, progress
);
4193 if (write_bitmap_index
)
4194 mark_bitmap_preferred_tips();
4196 if (prepare_revision_walk(revs
))
4197 die(_("revision walk setup failed"));
4198 mark_edges_uninteresting(revs
, show_edge
, sparse
);
4200 if (!fn_show_object
)
4201 fn_show_object
= show_object
;
4202 traverse_commit_list(revs
,
4203 show_commit
, fn_show_object
,
4206 if (unpack_unreachable_expiration
) {
4207 revs
->ignore_missing_links
= 1;
4208 if (add_unseen_recent_objects_to_traversal(revs
,
4209 unpack_unreachable_expiration
, NULL
, 0))
4210 die(_("unable to add recent objects"));
4211 if (prepare_revision_walk(revs
))
4212 die(_("revision walk setup failed"));
4213 traverse_commit_list(revs
, record_recent_commit
,
4214 record_recent_object
, NULL
);
4217 if (keep_unreachable
)
4218 add_objects_in_unpacked_packs();
4219 if (pack_loose_unreachable
)
4220 add_unreachable_loose_objects();
4221 if (unpack_unreachable
)
4222 loosen_unused_packed_objects();
4224 oid_array_clear(&recent_objects
);
4227 static void add_extra_kept_packs(const struct string_list
*names
)
4229 struct packed_git
*p
;
4234 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4235 const char *name
= basename(p
->pack_name
);
4241 for (i
= 0; i
< names
->nr
; i
++)
4242 if (!fspathcmp(name
, names
->items
[i
].string
))
4245 if (i
< names
->nr
) {
4246 p
->pack_keep_in_core
= 1;
4247 ignore_packed_keep_in_core
= 1;
4253 static int option_parse_quiet(const struct option
*opt
, const char *arg
,
4256 int *val
= opt
->value
;
4258 BUG_ON_OPT_ARG(arg
);
4267 static int option_parse_index_version(const struct option
*opt
,
4268 const char *arg
, int unset
)
4270 struct pack_idx_option
*popts
= opt
->value
;
4272 const char *val
= arg
;
4274 BUG_ON_OPT_NEG(unset
);
4276 popts
->version
= strtoul(val
, &c
, 10);
4277 if (popts
->version
> 2)
4278 die(_("unsupported index version %s"), val
);
4279 if (*c
== ',' && c
[1])
4280 popts
->off32_limit
= strtoul(c
+1, &c
, 0);
4281 if (*c
|| popts
->off32_limit
& 0x80000000)
4282 die(_("bad index version '%s'"), val
);
4286 static int option_parse_unpack_unreachable(const struct option
*opt UNUSED
,
4287 const char *arg
, int unset
)
4290 unpack_unreachable
= 0;
4291 unpack_unreachable_expiration
= 0;
4294 unpack_unreachable
= 1;
4296 unpack_unreachable_expiration
= approxidate(arg
);
4301 static int option_parse_cruft_expiration(const struct option
*opt UNUSED
,
4302 const char *arg
, int unset
)
4306 cruft_expiration
= 0;
4310 cruft_expiration
= approxidate(arg
);
4315 int cmd_pack_objects(int argc
,
4318 struct repository
*repo UNUSED
)
4320 int use_internal_rev_list
= 0;
4322 int all_progress_implied
= 0;
4323 struct strvec rp
= STRVEC_INIT
;
4324 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
4325 int rev_list_index
= 0;
4326 int stdin_packs
= 0;
4327 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
4328 struct list_objects_filter_options filter_options
=
4329 LIST_OBJECTS_FILTER_INIT
;
4331 struct option pack_objects_options
[] = {
4332 OPT_CALLBACK_F('q', "quiet", &progress
, NULL
,
4333 N_("do not show progress meter"),
4334 PARSE_OPT_NOARG
, option_parse_quiet
),
4335 OPT_SET_INT(0, "progress", &progress
,
4336 N_("show progress meter"), 1),
4337 OPT_SET_INT(0, "all-progress", &progress
,
4338 N_("show progress meter during object writing phase"), 2),
4339 OPT_BOOL(0, "all-progress-implied",
4340 &all_progress_implied
,
4341 N_("similar to --all-progress when progress meter is shown")),
4342 OPT_CALLBACK_F(0, "index-version", &pack_idx_opts
, N_("<version>[,<offset>]"),
4343 N_("write the pack index file in the specified idx format version"),
4344 PARSE_OPT_NONEG
, option_parse_index_version
),
4345 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
4346 N_("maximum size of each output pack file")),
4347 OPT_BOOL(0, "local", &local
,
4348 N_("ignore borrowed objects from alternate object store")),
4349 OPT_BOOL(0, "incremental", &incremental
,
4350 N_("ignore packed objects")),
4351 OPT_INTEGER(0, "window", &window
,
4352 N_("limit pack window by objects")),
4353 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
4354 N_("limit pack window by memory in addition to object limit")),
4355 OPT_INTEGER(0, "depth", &depth
,
4356 N_("maximum length of delta chain allowed in the resulting pack")),
4357 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
4358 N_("reuse existing deltas")),
4359 OPT_BOOL(0, "reuse-object", &reuse_object
,
4360 N_("reuse existing objects")),
4361 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
4362 N_("use OFS_DELTA objects")),
4363 OPT_INTEGER(0, "threads", &delta_search_threads
,
4364 N_("use threads when searching for best delta matches")),
4365 OPT_BOOL(0, "non-empty", &non_empty
,
4366 N_("do not create an empty pack output")),
4367 OPT_BOOL(0, "revs", &use_internal_rev_list
,
4368 N_("read revision arguments from standard input")),
4369 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked
,
4370 N_("limit the objects to those that are not yet packed"),
4371 1, PARSE_OPT_NONEG
),
4372 OPT_SET_INT_F(0, "all", &rev_list_all
,
4373 N_("include objects reachable from any reference"),
4374 1, PARSE_OPT_NONEG
),
4375 OPT_SET_INT_F(0, "reflog", &rev_list_reflog
,
4376 N_("include objects referred by reflog entries"),
4377 1, PARSE_OPT_NONEG
),
4378 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index
,
4379 N_("include objects referred to by the index"),
4380 1, PARSE_OPT_NONEG
),
4381 OPT_BOOL(0, "stdin-packs", &stdin_packs
,
4382 N_("read packs from stdin")),
4383 OPT_BOOL(0, "stdout", &pack_to_stdout
,
4384 N_("output pack to stdout")),
4385 OPT_BOOL(0, "include-tag", &include_tag
,
4386 N_("include tag objects that refer to objects to be packed")),
4387 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
4388 N_("keep unreachable objects")),
4389 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
4390 N_("pack loose unreachable objects")),
4391 OPT_CALLBACK_F(0, "unpack-unreachable", NULL
, N_("time"),
4392 N_("unpack unreachable objects newer than <time>"),
4393 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
),
4394 OPT_BOOL(0, "cruft", &cruft
, N_("create a cruft pack")),
4395 OPT_CALLBACK_F(0, "cruft-expiration", NULL
, N_("time"),
4396 N_("expire cruft objects older than <time>"),
4397 PARSE_OPT_OPTARG
, option_parse_cruft_expiration
),
4398 OPT_BOOL(0, "sparse", &sparse
,
4399 N_("use the sparse reachability algorithm")),
4400 OPT_BOOL(0, "thin", &thin
,
4401 N_("create thin packs")),
4402 OPT_BOOL(0, "shallow", &shallow
,
4403 N_("create packs suitable for shallow fetches")),
4404 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk
,
4405 N_("ignore packs that have companion .keep file")),
4406 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
4407 N_("ignore this pack")),
4408 OPT_INTEGER(0, "compression", &pack_compression_level
,
4409 N_("pack compression level")),
4410 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents
,
4411 N_("do not hide commits by grafts")),
4412 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
4413 N_("use a bitmap index if available to speed up counting objects")),
4414 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index
,
4415 N_("write a bitmap index together with the pack index"),
4417 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4418 &write_bitmap_index
,
4419 N_("write a bitmap index if possible"),
4420 WRITE_BITMAP_QUIET
, PARSE_OPT_HIDDEN
),
4421 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
4422 OPT_CALLBACK_F(0, "missing", NULL
, N_("action"),
4423 N_("handling for missing objects"), PARSE_OPT_NONEG
,
4424 option_parse_missing_action
),
4425 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
4426 N_("do not pack objects in promisor packfiles")),
4427 OPT_BOOL(0, "delta-islands", &use_delta_islands
,
4428 N_("respect islands during delta compression")),
4429 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols
,
4431 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4435 if (DFS_NUM_STATES
> (1 << OE_DFS_STATE_BITS
))
4436 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4438 disable_replace_refs();
4440 sparse
= git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4441 if (the_repository
->gitdir
) {
4442 prepare_repo_settings(the_repository
);
4444 sparse
= the_repository
->settings
.pack_use_sparse
;
4445 if (the_repository
->settings
.pack_use_multi_pack_reuse
)
4446 allow_pack_reuse
= MULTI_PACK_REUSE
;
4449 reset_pack_idx_option(&pack_idx_opts
);
4450 pack_idx_opts
.flags
|= WRITE_REV
;
4451 git_config(git_pack_config
, NULL
);
4452 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX
, 0))
4453 pack_idx_opts
.flags
&= ~WRITE_REV
;
4455 progress
= isatty(2);
4456 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
4460 base_name
= argv
[0];
4463 if (pack_to_stdout
!= !base_name
|| argc
)
4464 usage_with_options(pack_usage
, pack_objects_options
);
4468 if (depth
>= (1 << OE_DEPTH_BITS
)) {
4469 warning(_("delta chain depth %d is too deep, forcing %d"),
4470 depth
, (1 << OE_DEPTH_BITS
) - 1);
4471 depth
= (1 << OE_DEPTH_BITS
) - 1;
4473 if (cache_max_small_delta_size
>= (1U << OE_Z_DELTA_BITS
)) {
4474 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4475 (1U << OE_Z_DELTA_BITS
) - 1);
4476 cache_max_small_delta_size
= (1U << OE_Z_DELTA_BITS
) - 1;
4481 strvec_push(&rp
, "pack-objects");
4483 use_internal_rev_list
= 1;
4484 strvec_push(&rp
, shallow
4485 ? "--objects-edge-aggressive"
4486 : "--objects-edge");
4488 strvec_push(&rp
, "--objects");
4491 use_internal_rev_list
= 1;
4492 strvec_push(&rp
, "--all");
4494 if (rev_list_reflog
) {
4495 use_internal_rev_list
= 1;
4496 strvec_push(&rp
, "--reflog");
4498 if (rev_list_index
) {
4499 use_internal_rev_list
= 1;
4500 strvec_push(&rp
, "--indexed-objects");
4502 if (rev_list_unpacked
&& !stdin_packs
) {
4503 use_internal_rev_list
= 1;
4504 strvec_push(&rp
, "--unpacked");
4507 if (exclude_promisor_objects
) {
4508 use_internal_rev_list
= 1;
4509 fetch_if_missing
= 0;
4510 strvec_push(&rp
, "--exclude-promisor-objects");
4512 if (unpack_unreachable
|| keep_unreachable
|| pack_loose_unreachable
)
4513 use_internal_rev_list
= 1;
4517 if (pack_compression_level
== -1)
4518 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
4519 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
4520 die(_("bad pack compression level %d"), pack_compression_level
);
4522 if (!delta_search_threads
) /* --threads=0 means autodetect */
4523 delta_search_threads
= online_cpus();
4525 if (!HAVE_THREADS
&& delta_search_threads
!= 1)
4526 warning(_("no threads support, ignoring --threads"));
4527 if (!pack_to_stdout
&& !pack_size_limit
)
4528 pack_size_limit
= pack_size_limit_cfg
;
4529 if (pack_to_stdout
&& pack_size_limit
)
4530 die(_("--max-pack-size cannot be used to build a pack for transfer"));
4531 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
4532 warning(_("minimum pack size limit is 1 MiB"));
4533 pack_size_limit
= 1024*1024;
4536 if (!pack_to_stdout
&& thin
)
4537 die(_("--thin cannot be used to build an indexable pack"));
4539 if (keep_unreachable
&& unpack_unreachable
)
4540 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
4541 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
4542 unpack_unreachable_expiration
= 0;
4544 if (stdin_packs
&& filter_options
.choice
)
4545 die(_("cannot use --filter with --stdin-packs"));
4547 if (stdin_packs
&& use_internal_rev_list
)
4548 die(_("cannot use internal rev list with --stdin-packs"));
4551 if (use_internal_rev_list
)
4552 die(_("cannot use internal rev list with --cruft"));
4554 die(_("cannot use --stdin-packs with --cruft"));
4558 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4560 * - to produce good pack (with bitmap index not-yet-packed objects are
4561 * packed in suboptimal order).
4563 * - to use more robust pack-generation codepath (avoiding possible
4564 * bugs in bitmap code and possible bitmap index corruption).
4566 if (!pack_to_stdout
)
4567 use_bitmap_index_default
= 0;
4569 if (use_bitmap_index
< 0)
4570 use_bitmap_index
= use_bitmap_index_default
;
4572 /* "hard" reasons not to use bitmaps; these just won't work at all */
4573 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow(the_repository
))
4574 use_bitmap_index
= 0;
4576 if (pack_to_stdout
|| !rev_list_all
)
4577 write_bitmap_index
= 0;
4579 if (use_delta_islands
)
4580 strvec_push(&rp
, "--topo-order");
4582 if (progress
&& all_progress_implied
)
4585 add_extra_kept_packs(&keep_pack_list
);
4586 if (ignore_packed_keep_on_disk
) {
4587 struct packed_git
*p
;
4588 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
4589 if (p
->pack_local
&& p
->pack_keep
)
4591 if (!p
) /* no keep-able packs found */
4592 ignore_packed_keep_on_disk
= 0;
4596 * unlike ignore_packed_keep_on_disk above, we do not
4597 * want to unset "local" based on looking at packs, as
4598 * it also covers non-local objects
4600 struct packed_git
*p
;
4601 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4602 if (!p
->pack_local
) {
4603 have_non_local_packs
= 1;
4609 trace2_region_enter("pack-objects", "enumerate-objects",
4611 prepare_packing_data(the_repository
, &to_pack
);
4613 if (progress
&& !cruft
)
4614 progress_state
= start_progress(_("Enumerating objects"), 0);
4616 /* avoids adding objects in excluded packs */
4617 ignore_packed_keep_in_core
= 1;
4618 read_packs_list_from_stdin();
4619 if (rev_list_unpacked
)
4620 add_unreachable_loose_objects();
4622 read_cruft_objects();
4623 } else if (!use_internal_rev_list
) {
4624 read_object_list_from_stdin();
4626 struct rev_info revs
;
4628 repo_init_revisions(the_repository
, &revs
, NULL
);
4629 list_objects_filter_copy(&revs
.filter
, &filter_options
);
4630 get_object_list(&revs
, rp
.nr
, rp
.v
);
4631 release_revisions(&revs
);
4633 cleanup_preferred_base();
4634 if (include_tag
&& nr_result
)
4635 refs_for_each_tag_ref(get_main_ref_store(the_repository
),
4637 stop_progress(&progress_state
);
4638 trace2_region_leave("pack-objects", "enumerate-objects",
4641 if (non_empty
&& !nr_result
)
4644 trace2_region_enter("pack-objects", "prepare-pack",
4646 prepare_pack(window
, depth
);
4647 trace2_region_leave("pack-objects", "prepare-pack",
4651 trace2_region_enter("pack-objects", "write-pack-file", the_repository
);
4652 write_excluded_by_configs();
4654 trace2_region_leave("pack-objects", "write-pack-file", the_repository
);
4658 _("Total %"PRIu32
" (delta %"PRIu32
"),"
4659 " reused %"PRIu32
" (delta %"PRIu32
"),"
4660 " pack-reused %"PRIu32
" (from %"PRIuMAX
")"),
4661 written
, written_delta
, reused
, reused_delta
,
4662 reuse_packfile_objects
,
4663 (uintmax_t)reuse_packfiles_used_nr
);
4665 trace2_data_intmax("pack-objects", the_repository
, "written", written
);
4666 trace2_data_intmax("pack-objects", the_repository
, "written/delta", written_delta
);
4667 trace2_data_intmax("pack-objects", the_repository
, "reused", reused
);
4668 trace2_data_intmax("pack-objects", the_repository
, "reused/delta", reused_delta
);
4669 trace2_data_intmax("pack-objects", the_repository
, "pack-reused", reuse_packfile_objects
);
4670 trace2_data_intmax("pack-objects", the_repository
, "packs-reused", reuse_packfiles_used_nr
);
4673 clear_packing_data(&to_pack
);
4674 list_objects_filter_release(&filter_options
);
4675 string_list_clear(&keep_pack_list
, 0);