2 #include "environment.h"
5 #include "repository.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 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
[10];
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
) {
1194 if ((word
>> offset
) == 0)
1197 offset
+= ewah_bit_ctz64(word
>> offset
);
1198 if (pos
+ offset
< reuse_packfile
->bitmap_pos
)
1200 if (pos
+ offset
>= reuse_packfile
->bitmap_pos
+ reuse_packfile
->bitmap_nr
)
1203 * Can use bit positions directly, even for MIDX
1204 * bitmaps. See comment in try_partial_reuse()
1207 write_reused_pack_one(reuse_packfile
->p
,
1208 pos
+ offset
- reuse_packfile
->bitmap_pos
,
1209 f
, pack_start
, &w_curs
);
1210 display_progress(progress_state
, ++written
);
1215 unuse_pack(&w_curs
);
1218 static void write_excluded_by_configs(void)
1220 struct oidset_iter iter
;
1221 const struct object_id
*oid
;
1223 oidset_iter_init(&excluded_by_config
, &iter
);
1224 while ((oid
= oidset_iter_next(&iter
))) {
1225 struct configured_exclusion
*ex
=
1226 oidmap_get(&configured_exclusions
, oid
);
1229 BUG("configured exclusion wasn't configured");
1230 write_in_full(1, ex
->pack_hash_hex
, strlen(ex
->pack_hash_hex
));
1231 write_in_full(1, " ", 1);
1232 write_in_full(1, ex
->uri
, strlen(ex
->uri
));
1233 write_in_full(1, "\n", 1);
1237 static const char no_split_warning
[] = N_(
1238 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
1241 static void write_pack_file(void)
1246 uint32_t nr_remaining
= nr_result
;
1247 time_t last_mtime
= 0;
1248 struct object_entry
**write_order
;
1250 if (progress
> pack_to_stdout
)
1251 progress_state
= start_progress(_("Writing objects"), nr_result
);
1252 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
1253 write_order
= compute_write_order();
1256 unsigned char hash
[GIT_MAX_RAWSZ
];
1257 char *pack_tmp_name
= NULL
;
1260 f
= hashfd_throughput(1, "<stdout>", progress_state
);
1262 f
= create_tmp_packfile(&pack_tmp_name
);
1264 offset
= write_pack_header(f
, nr_remaining
);
1266 if (reuse_packfiles_nr
) {
1267 assert(pack_to_stdout
);
1268 for (j
= 0; j
< reuse_packfiles_nr
; j
++) {
1269 reused_chunks_nr
= 0;
1270 write_reused_pack(&reuse_packfiles
[j
], f
);
1271 if (reused_chunks_nr
)
1272 reuse_packfiles_used_nr
++;
1274 offset
= hashfile_total(f
);
1278 for (; i
< to_pack
.nr_objects
; i
++) {
1279 struct object_entry
*e
= write_order
[i
];
1280 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
1282 display_progress(progress_state
, written
);
1285 if (pack_to_stdout
) {
1287 * We never fsync when writing to stdout since we may
1288 * not be writing to an actual pack file. For instance,
1289 * the upload-pack code passes a pipe here. Calling
1290 * fsync on a pipe results in unnecessary
1291 * synchronization with the reader on some platforms.
1293 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_NONE
,
1294 CSUM_HASH_IN_STREAM
| CSUM_CLOSE
);
1295 } else if (nr_written
== nr_remaining
) {
1296 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
,
1297 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
1300 * If we wrote the wrong number of entries in the
1301 * header, rewrite it like in fast-import.
1304 int fd
= finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
, 0);
1305 fixup_pack_header_footer(fd
, hash
, pack_tmp_name
,
1306 nr_written
, hash
, offset
);
1308 if (write_bitmap_index
) {
1309 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1310 warning(_(no_split_warning
));
1311 write_bitmap_index
= 0;
1315 if (!pack_to_stdout
) {
1317 struct strbuf tmpname
= STRBUF_INIT
;
1318 struct bitmap_writer bitmap_writer
;
1319 char *idx_tmp_name
= NULL
;
1322 * Packs are runtime accessed in their mtime
1323 * order since newer packs are more likely to contain
1324 * younger objects. So if we are creating multiple
1325 * packs then we should modify the mtime of later ones
1326 * to preserve this property.
1328 if (stat(pack_tmp_name
, &st
) < 0) {
1329 warning_errno(_("failed to stat %s"), pack_tmp_name
);
1330 } else if (!last_mtime
) {
1331 last_mtime
= st
.st_mtime
;
1334 utb
.actime
= st
.st_atime
;
1335 utb
.modtime
= --last_mtime
;
1336 if (utime(pack_tmp_name
, &utb
) < 0)
1337 warning_errno(_("failed utime() on %s"), pack_tmp_name
);
1340 strbuf_addf(&tmpname
, "%s-%s.", base_name
,
1343 if (write_bitmap_index
) {
1344 bitmap_writer_init(&bitmap_writer
,
1346 bitmap_writer_set_checksum(&bitmap_writer
, hash
);
1347 bitmap_writer_build_type_index(&bitmap_writer
,
1348 &to_pack
, written_list
, nr_written
);
1352 pack_idx_opts
.flags
|= WRITE_MTIMES
;
1354 stage_tmp_packfiles(&tmpname
, pack_tmp_name
,
1355 written_list
, nr_written
,
1356 &to_pack
, &pack_idx_opts
, hash
,
1359 if (write_bitmap_index
) {
1360 size_t tmpname_len
= tmpname
.len
;
1362 strbuf_addstr(&tmpname
, "bitmap");
1363 stop_progress(&progress_state
);
1365 bitmap_writer_show_progress(&bitmap_writer
,
1367 bitmap_writer_select_commits(&bitmap_writer
,
1369 indexed_commits_nr
);
1370 if (bitmap_writer_build(&bitmap_writer
, &to_pack
) < 0)
1371 die(_("failed to write bitmap index"));
1372 bitmap_writer_finish(&bitmap_writer
,
1373 written_list
, nr_written
,
1374 tmpname
.buf
, write_bitmap_options
);
1375 bitmap_writer_free(&bitmap_writer
);
1376 write_bitmap_index
= 0;
1377 strbuf_setlen(&tmpname
, tmpname_len
);
1380 rename_tmp_packfile_idx(&tmpname
, &idx_tmp_name
);
1383 strbuf_release(&tmpname
);
1384 free(pack_tmp_name
);
1385 puts(hash_to_hex(hash
));
1388 /* mark written objects as written to previous pack */
1389 for (j
= 0; j
< nr_written
; j
++) {
1390 written_list
[j
]->offset
= (off_t
)-1;
1392 nr_remaining
-= nr_written
;
1393 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
1397 stop_progress(&progress_state
);
1398 if (written
!= nr_result
)
1399 die(_("wrote %"PRIu32
" objects while expecting %"PRIu32
),
1400 written
, nr_result
);
1401 trace2_data_intmax("pack-objects", the_repository
,
1402 "write_pack_file/wrote", nr_result
);
1405 static int no_try_delta(const char *path
)
1407 static struct attr_check
*check
;
1410 check
= attr_check_initl("delta", NULL
);
1411 git_check_attr(the_repository
->index
, path
, check
);
1412 if (ATTR_FALSE(check
->items
[0].value
))
1418 * When adding an object, check whether we have already added it
1419 * to our packing list. If so, we can skip. However, if we are
1420 * being asked to excludei t, but the previous mention was to include
1421 * it, make sure to adjust its flags and tweak our numbers accordingly.
1423 * As an optimization, we pass out the index position where we would have
1424 * found the item, since that saves us from having to look it up again a
1425 * few lines later when we want to add the new entry.
1427 static int have_duplicate_entry(const struct object_id
*oid
,
1430 struct object_entry
*entry
;
1432 if (reuse_packfile_bitmap
&&
1433 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
))
1436 entry
= packlist_find(&to_pack
, oid
);
1441 if (!entry
->preferred_base
)
1443 entry
->preferred_base
= 1;
1449 static int want_found_object(const struct object_id
*oid
, int exclude
,
1450 struct packed_git
*p
)
1457 if (!is_pack_valid(p
))
1461 * When asked to do --local (do not include an object that appears in a
1462 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1463 * an object that appears in a pack marked with .keep), finding a pack
1464 * that matches the criteria is sufficient for us to decide to omit it.
1465 * However, even if this pack does not satisfy the criteria, we need to
1466 * make sure no copy of this object appears in _any_ pack that makes us
1467 * to omit the object, so we need to check all the packs.
1469 * We can however first check whether these options can possibly matter;
1470 * if they do not matter we know we want the object in generated pack.
1471 * Otherwise, we signal "-1" at the end to tell the caller that we do
1472 * not know either way, and it needs to check more packs.
1476 * Objects in packs borrowed from elsewhere are discarded regardless of
1477 * if they appear in other packs that weren't borrowed.
1479 if (local
&& !p
->pack_local
)
1483 * Then handle .keep first, as we have a fast(er) path there.
1485 if (ignore_packed_keep_on_disk
|| ignore_packed_keep_in_core
) {
1487 * Set the flags for the kept-pack cache to be the ones we want
1490 * That is, if we are ignoring objects in on-disk keep packs,
1491 * then we want to search through the on-disk keep and ignore
1495 if (ignore_packed_keep_on_disk
)
1496 flags
|= ON_DISK_KEEP_PACKS
;
1497 if (ignore_packed_keep_in_core
)
1498 flags
|= IN_CORE_KEEP_PACKS
;
1500 if (ignore_packed_keep_on_disk
&& p
->pack_keep
)
1502 if (ignore_packed_keep_in_core
&& p
->pack_keep_in_core
)
1504 if (has_object_kept_pack(oid
, flags
))
1509 * At this point we know definitively that either we don't care about
1510 * keep-packs, or the object is not in one. Keep checking other
1513 if (!local
|| !have_non_local_packs
)
1516 /* we don't know yet; keep looking for more packs */
1520 static int want_object_in_pack_one(struct packed_git
*p
,
1521 const struct object_id
*oid
,
1523 struct packed_git
**found_pack
,
1524 off_t
*found_offset
)
1528 if (p
== *found_pack
)
1529 offset
= *found_offset
;
1531 offset
= find_pack_entry_one(oid
->hash
, p
);
1535 if (!is_pack_valid(p
))
1537 *found_offset
= offset
;
1540 return want_found_object(oid
, exclude
, p
);
1546 * Check whether we want the object in the pack (e.g., we do not want
1547 * objects found in non-local stores if the "--local" option was used).
1549 * If the caller already knows an existing pack it wants to take the object
1550 * from, that is passed in *found_pack and *found_offset; otherwise this
1551 * function finds if there is any pack that has the object and returns the pack
1552 * and its offset in these variables.
1554 static int want_object_in_pack(const struct object_id
*oid
,
1556 struct packed_git
**found_pack
,
1557 off_t
*found_offset
)
1560 struct list_head
*pos
;
1561 struct multi_pack_index
*m
;
1563 if (!exclude
&& local
&& has_loose_object_nonlocal(oid
))
1567 * If we already know the pack object lives in, start checks from that
1568 * pack - in the usual case when neither --local was given nor .keep files
1569 * are present we will determine the answer right now.
1572 want
= want_found_object(oid
, exclude
, *found_pack
);
1580 for (m
= get_multi_pack_index(the_repository
); m
; m
= m
->next
) {
1581 struct pack_entry e
;
1582 if (fill_midx_entry(the_repository
, oid
, &e
, m
)) {
1583 want
= want_object_in_pack_one(e
.p
, oid
, exclude
, found_pack
, found_offset
);
1589 list_for_each(pos
, get_packed_git_mru(the_repository
)) {
1590 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1591 want
= want_object_in_pack_one(p
, oid
, exclude
, found_pack
, found_offset
);
1592 if (!exclude
&& want
> 0)
1594 get_packed_git_mru(the_repository
));
1599 if (uri_protocols
.nr
) {
1600 struct configured_exclusion
*ex
=
1601 oidmap_get(&configured_exclusions
, oid
);
1606 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1607 if (skip_prefix(ex
->uri
,
1608 uri_protocols
.items
[i
].string
,
1611 oidset_insert(&excluded_by_config
, oid
);
1621 static struct object_entry
*create_object_entry(const struct object_id
*oid
,
1622 enum object_type type
,
1626 struct packed_git
*found_pack
,
1629 struct object_entry
*entry
;
1631 entry
= packlist_alloc(&to_pack
, oid
);
1633 oe_set_type(entry
, type
);
1635 entry
->preferred_base
= 1;
1639 oe_set_in_pack(&to_pack
, entry
, found_pack
);
1640 entry
->in_pack_offset
= found_offset
;
1643 entry
->no_try_delta
= no_try_delta
;
1648 static const char no_closure_warning
[] = N_(
1649 "disabling bitmap writing, as some objects are not being packed"
1652 static int add_object_entry(const struct object_id
*oid
, enum object_type type
,
1653 const char *name
, int exclude
)
1655 struct packed_git
*found_pack
= NULL
;
1656 off_t found_offset
= 0;
1658 display_progress(progress_state
, ++nr_seen
);
1660 if (have_duplicate_entry(oid
, exclude
))
1663 if (!want_object_in_pack(oid
, exclude
, &found_pack
, &found_offset
)) {
1664 /* The pack is missing an object, so it will not have closure */
1665 if (write_bitmap_index
) {
1666 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1667 warning(_(no_closure_warning
));
1668 write_bitmap_index
= 0;
1673 create_object_entry(oid
, type
, pack_name_hash(name
),
1674 exclude
, name
&& no_try_delta(name
),
1675 found_pack
, found_offset
);
1679 static int add_object_entry_from_bitmap(const struct object_id
*oid
,
1680 enum object_type type
,
1681 int flags UNUSED
, uint32_t name_hash
,
1682 struct packed_git
*pack
, off_t offset
)
1684 display_progress(progress_state
, ++nr_seen
);
1686 if (have_duplicate_entry(oid
, 0))
1689 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
1692 create_object_entry(oid
, type
, name_hash
, 0, 0, pack
, offset
);
1696 struct pbase_tree_cache
{
1697 struct object_id oid
;
1701 unsigned long tree_size
;
1704 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1705 static int pbase_tree_cache_ix(const struct object_id
*oid
)
1707 return oid
->hash
[0] % ARRAY_SIZE(pbase_tree_cache
);
1709 static int pbase_tree_cache_ix_incr(int ix
)
1711 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1714 static struct pbase_tree
{
1715 struct pbase_tree
*next
;
1716 /* This is a phony "cache" entry; we are not
1717 * going to evict it or find it through _get()
1718 * mechanism -- this is for the toplevel node that
1719 * would almost always change with any commit.
1721 struct pbase_tree_cache pcache
;
1724 static struct pbase_tree_cache
*pbase_tree_get(const struct object_id
*oid
)
1726 struct pbase_tree_cache
*ent
, *nent
;
1729 enum object_type type
;
1731 int my_ix
= pbase_tree_cache_ix(oid
);
1732 int available_ix
= -1;
1734 /* pbase-tree-cache acts as a limited hashtable.
1735 * your object will be found at your index or within a few
1736 * slots after that slot if it is cached.
1738 for (neigh
= 0; neigh
< 8; neigh
++) {
1739 ent
= pbase_tree_cache
[my_ix
];
1740 if (ent
&& oideq(&ent
->oid
, oid
)) {
1744 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1745 ((0 <= available_ix
) &&
1746 (!ent
&& pbase_tree_cache
[available_ix
])))
1747 available_ix
= my_ix
;
1750 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1753 /* Did not find one. Either we got a bogus request or
1754 * we need to read and perhaps cache.
1756 data
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
1759 if (type
!= OBJ_TREE
) {
1764 /* We need to either cache or return a throwaway copy */
1766 if (available_ix
< 0)
1769 ent
= pbase_tree_cache
[available_ix
];
1770 my_ix
= available_ix
;
1774 nent
= xmalloc(sizeof(*nent
));
1775 nent
->temporary
= (available_ix
< 0);
1778 /* evict and reuse */
1779 free(ent
->tree_data
);
1782 oidcpy(&nent
->oid
, oid
);
1783 nent
->tree_data
= data
;
1784 nent
->tree_size
= size
;
1786 if (!nent
->temporary
)
1787 pbase_tree_cache
[my_ix
] = nent
;
1791 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1793 if (!cache
->temporary
) {
1797 free(cache
->tree_data
);
1801 static size_t name_cmp_len(const char *name
)
1803 return strcspn(name
, "\n/");
1806 static void add_pbase_object(struct tree_desc
*tree
,
1809 const char *fullname
)
1811 struct name_entry entry
;
1814 while (tree_entry(tree
,&entry
)) {
1815 if (S_ISGITLINK(entry
.mode
))
1817 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1818 memcmp(name
, entry
.path
, cmplen
);
1823 if (name
[cmplen
] != '/') {
1824 add_object_entry(&entry
.oid
,
1825 object_type(entry
.mode
),
1829 if (S_ISDIR(entry
.mode
)) {
1830 struct tree_desc sub
;
1831 struct pbase_tree_cache
*tree
;
1832 const char *down
= name
+cmplen
+1;
1833 size_t downlen
= name_cmp_len(down
);
1835 tree
= pbase_tree_get(&entry
.oid
);
1838 init_tree_desc(&sub
, &tree
->oid
,
1839 tree
->tree_data
, tree
->tree_size
);
1841 add_pbase_object(&sub
, down
, downlen
, fullname
);
1842 pbase_tree_put(tree
);
1847 static unsigned *done_pbase_paths
;
1848 static int done_pbase_paths_num
;
1849 static int done_pbase_paths_alloc
;
1850 static int done_pbase_path_pos(unsigned hash
)
1853 int hi
= done_pbase_paths_num
;
1855 int mi
= lo
+ (hi
- lo
) / 2;
1856 if (done_pbase_paths
[mi
] == hash
)
1858 if (done_pbase_paths
[mi
] < hash
)
1866 static int check_pbase_path(unsigned hash
)
1868 int pos
= done_pbase_path_pos(hash
);
1872 ALLOC_GROW(done_pbase_paths
,
1873 done_pbase_paths_num
+ 1,
1874 done_pbase_paths_alloc
);
1875 done_pbase_paths_num
++;
1876 if (pos
< done_pbase_paths_num
)
1877 MOVE_ARRAY(done_pbase_paths
+ pos
+ 1, done_pbase_paths
+ pos
,
1878 done_pbase_paths_num
- pos
- 1);
1879 done_pbase_paths
[pos
] = hash
;
1883 static void add_preferred_base_object(const char *name
)
1885 struct pbase_tree
*it
;
1887 unsigned hash
= pack_name_hash(name
);
1889 if (!num_preferred_base
|| check_pbase_path(hash
))
1892 cmplen
= name_cmp_len(name
);
1893 for (it
= pbase_tree
; it
; it
= it
->next
) {
1895 add_object_entry(&it
->pcache
.oid
, OBJ_TREE
, NULL
, 1);
1898 struct tree_desc tree
;
1899 init_tree_desc(&tree
, &it
->pcache
.oid
,
1900 it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1901 add_pbase_object(&tree
, name
, cmplen
, name
);
1906 static void add_preferred_base(struct object_id
*oid
)
1908 struct pbase_tree
*it
;
1911 struct object_id tree_oid
;
1913 if (window
<= num_preferred_base
++)
1916 data
= read_object_with_reference(the_repository
, oid
,
1917 OBJ_TREE
, &size
, &tree_oid
);
1921 for (it
= pbase_tree
; it
; it
= it
->next
) {
1922 if (oideq(&it
->pcache
.oid
, &tree_oid
)) {
1928 CALLOC_ARRAY(it
, 1);
1929 it
->next
= pbase_tree
;
1932 oidcpy(&it
->pcache
.oid
, &tree_oid
);
1933 it
->pcache
.tree_data
= data
;
1934 it
->pcache
.tree_size
= size
;
1937 static void cleanup_preferred_base(void)
1939 struct pbase_tree
*it
;
1945 struct pbase_tree
*tmp
= it
;
1947 free(tmp
->pcache
.tree_data
);
1951 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1952 if (!pbase_tree_cache
[i
])
1954 free(pbase_tree_cache
[i
]->tree_data
);
1955 FREE_AND_NULL(pbase_tree_cache
[i
]);
1958 FREE_AND_NULL(done_pbase_paths
);
1959 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1963 * Return 1 iff the object specified by "delta" can be sent
1964 * literally as a delta against the base in "base_sha1". If
1965 * so, then *base_out will point to the entry in our packing
1966 * list, or NULL if we must use the external-base list.
1968 * Depth value does not matter - find_deltas() will
1969 * never consider reused delta as the base object to
1970 * deltify other objects against, in order to avoid
1973 static int can_reuse_delta(const struct object_id
*base_oid
,
1974 struct object_entry
*delta
,
1975 struct object_entry
**base_out
)
1977 struct object_entry
*base
;
1980 * First see if we're already sending the base (or it's explicitly in
1981 * our "excluded" list).
1983 base
= packlist_find(&to_pack
, base_oid
);
1985 if (!in_same_island(&delta
->idx
.oid
, &base
->idx
.oid
))
1992 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1993 * even if it was buried too deep in history to make it into the
1996 if (thin
&& bitmap_has_oid_in_uninteresting(bitmap_git
, base_oid
)) {
1997 if (use_delta_islands
) {
1998 if (!in_same_island(&delta
->idx
.oid
, base_oid
))
2008 static void prefetch_to_pack(uint32_t object_index_start
) {
2009 struct oid_array to_fetch
= OID_ARRAY_INIT
;
2012 for (i
= object_index_start
; i
< to_pack
.nr_objects
; i
++) {
2013 struct object_entry
*entry
= to_pack
.objects
+ i
;
2015 if (!oid_object_info_extended(the_repository
,
2018 OBJECT_INFO_FOR_PREFETCH
))
2020 oid_array_append(&to_fetch
, &entry
->idx
.oid
);
2022 promisor_remote_get_direct(the_repository
,
2023 to_fetch
.oid
, to_fetch
.nr
);
2024 oid_array_clear(&to_fetch
);
2027 static void check_object(struct object_entry
*entry
, uint32_t object_index
)
2029 unsigned long canonical_size
;
2030 enum object_type type
;
2031 struct object_info oi
= {.typep
= &type
, .sizep
= &canonical_size
};
2033 if (IN_PACK(entry
)) {
2034 struct packed_git
*p
= IN_PACK(entry
);
2035 struct pack_window
*w_curs
= NULL
;
2037 struct object_id base_ref
;
2038 struct object_entry
*base_entry
;
2039 unsigned long used
, used_0
;
2040 unsigned long avail
;
2042 unsigned char *buf
, c
;
2043 enum object_type type
;
2044 unsigned long in_pack_size
;
2046 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
2049 * We want in_pack_type even if we do not reuse delta
2050 * since non-delta representations could still be reused.
2052 used
= unpack_object_header_buffer(buf
, avail
,
2059 BUG("invalid type %d", type
);
2060 entry
->in_pack_type
= type
;
2063 * Determine if this is a delta and if so whether we can
2064 * reuse it or not. Otherwise let's find out as cheaply as
2065 * possible what the actual type and size for this object is.
2067 switch (entry
->in_pack_type
) {
2069 /* Not a delta hence we've already got all we need. */
2070 oe_set_type(entry
, entry
->in_pack_type
);
2071 SET_SIZE(entry
, in_pack_size
);
2072 entry
->in_pack_header_size
= used
;
2073 if (oe_type(entry
) < OBJ_COMMIT
|| oe_type(entry
) > OBJ_BLOB
)
2075 unuse_pack(&w_curs
);
2078 if (reuse_delta
&& !entry
->preferred_base
) {
2080 use_pack(p
, &w_curs
,
2081 entry
->in_pack_offset
+ used
,
2083 the_repository
->hash_algo
);
2086 entry
->in_pack_header_size
= used
+ the_hash_algo
->rawsz
;
2089 buf
= use_pack(p
, &w_curs
,
2090 entry
->in_pack_offset
+ used
, NULL
);
2096 if (!ofs
|| MSB(ofs
, 7)) {
2097 error(_("delta base offset overflow in pack for %s"),
2098 oid_to_hex(&entry
->idx
.oid
));
2102 ofs
= (ofs
<< 7) + (c
& 127);
2104 ofs
= entry
->in_pack_offset
- ofs
;
2105 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
2106 error(_("delta base offset out of bound for %s"),
2107 oid_to_hex(&entry
->idx
.oid
));
2110 if (reuse_delta
&& !entry
->preferred_base
) {
2112 if (offset_to_pack_pos(p
, ofs
, &pos
) < 0)
2114 if (!nth_packed_object_id(&base_ref
, p
,
2115 pack_pos_to_index(p
, pos
)))
2118 entry
->in_pack_header_size
= used
+ used_0
;
2123 can_reuse_delta(&base_ref
, entry
, &base_entry
)) {
2124 oe_set_type(entry
, entry
->in_pack_type
);
2125 SET_SIZE(entry
, in_pack_size
); /* delta size */
2126 SET_DELTA_SIZE(entry
, in_pack_size
);
2129 SET_DELTA(entry
, base_entry
);
2130 entry
->delta_sibling_idx
= base_entry
->delta_child_idx
;
2131 SET_DELTA_CHILD(base_entry
, entry
);
2133 SET_DELTA_EXT(entry
, &base_ref
);
2136 unuse_pack(&w_curs
);
2140 if (oe_type(entry
)) {
2144 * This must be a delta and we already know what the
2145 * final object type is. Let's extract the actual
2146 * object size from the delta header.
2148 delta_pos
= entry
->in_pack_offset
+ entry
->in_pack_header_size
;
2149 canonical_size
= get_size_from_delta(p
, &w_curs
, delta_pos
);
2150 if (canonical_size
== 0)
2152 SET_SIZE(entry
, canonical_size
);
2153 unuse_pack(&w_curs
);
2158 * No choice but to fall back to the recursive delta walk
2159 * with oid_object_info() to find about the object type
2163 unuse_pack(&w_curs
);
2166 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2167 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0) {
2168 if (repo_has_promisor_remote(the_repository
)) {
2169 prefetch_to_pack(object_index
);
2170 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2171 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0)
2177 oe_set_type(entry
, type
);
2178 if (entry
->type_valid
) {
2179 SET_SIZE(entry
, canonical_size
);
2182 * Bad object type is checked in prepare_pack(). This is
2183 * to permit a missing preferred base object to be ignored
2184 * as a preferred base. Doing so can result in a larger
2185 * pack file, but the transfer will still take place.
2190 static int pack_offset_sort(const void *_a
, const void *_b
)
2192 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2193 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2194 const struct packed_git
*a_in_pack
= IN_PACK(a
);
2195 const struct packed_git
*b_in_pack
= IN_PACK(b
);
2197 /* avoid filesystem trashing with loose objects */
2198 if (!a_in_pack
&& !b_in_pack
)
2199 return oidcmp(&a
->idx
.oid
, &b
->idx
.oid
);
2201 if (a_in_pack
< b_in_pack
)
2203 if (a_in_pack
> b_in_pack
)
2205 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
2206 (a
->in_pack_offset
> b
->in_pack_offset
);
2210 * Drop an on-disk delta we were planning to reuse. Naively, this would
2211 * just involve blanking out the "delta" field, but we have to deal
2212 * with some extra book-keeping:
2214 * 1. Removing ourselves from the delta_sibling linked list.
2216 * 2. Updating our size/type to the non-delta representation. These were
2217 * either not recorded initially (size) or overwritten with the delta type
2218 * (type) when check_object() decided to reuse the delta.
2220 * 3. Resetting our delta depth, as we are now a base object.
2222 static void drop_reused_delta(struct object_entry
*entry
)
2224 unsigned *idx
= &to_pack
.objects
[entry
->delta_idx
- 1].delta_child_idx
;
2225 struct object_info oi
= OBJECT_INFO_INIT
;
2226 enum object_type type
;
2230 struct object_entry
*oe
= &to_pack
.objects
[*idx
- 1];
2233 *idx
= oe
->delta_sibling_idx
;
2235 idx
= &oe
->delta_sibling_idx
;
2237 SET_DELTA(entry
, NULL
);
2242 if (packed_object_info(the_repository
, IN_PACK(entry
), entry
->in_pack_offset
, &oi
) < 0) {
2244 * We failed to get the info from this pack for some reason;
2245 * fall back to oid_object_info, which may find another copy.
2246 * And if that fails, the error will be recorded in oe_type(entry)
2247 * and dealt with in prepare_pack().
2250 oid_object_info(the_repository
, &entry
->idx
.oid
, &size
));
2252 oe_set_type(entry
, type
);
2254 SET_SIZE(entry
, size
);
2258 * Follow the chain of deltas from this entry onward, throwing away any links
2259 * that cause us to hit a cycle (as determined by the DFS state flags in
2262 * We also detect too-long reused chains that would violate our --depth
2265 static void break_delta_chains(struct object_entry
*entry
)
2268 * The actual depth of each object we will write is stored as an int,
2269 * as it cannot exceed our int "depth" limit. But before we break
2270 * changes based no that limit, we may potentially go as deep as the
2271 * number of objects, which is elsewhere bounded to a uint32_t.
2273 uint32_t total_depth
;
2274 struct object_entry
*cur
, *next
;
2276 for (cur
= entry
, total_depth
= 0;
2278 cur
= DELTA(cur
), total_depth
++) {
2279 if (cur
->dfs_state
== DFS_DONE
) {
2281 * We've already seen this object and know it isn't
2282 * part of a cycle. We do need to append its depth
2285 total_depth
+= cur
->depth
;
2290 * We break cycles before looping, so an ACTIVE state (or any
2291 * other cruft which made its way into the state variable)
2294 if (cur
->dfs_state
!= DFS_NONE
)
2295 BUG("confusing delta dfs state in first pass: %d",
2299 * Now we know this is the first time we've seen the object. If
2300 * it's not a delta, we're done traversing, but we'll mark it
2301 * done to save time on future traversals.
2304 cur
->dfs_state
= DFS_DONE
;
2309 * Mark ourselves as active and see if the next step causes
2310 * us to cycle to another active object. It's important to do
2311 * this _before_ we loop, because it impacts where we make the
2312 * cut, and thus how our total_depth counter works.
2313 * E.g., We may see a partial loop like:
2315 * A -> B -> C -> D -> B
2317 * Cutting B->C breaks the cycle. But now the depth of A is
2318 * only 1, and our total_depth counter is at 3. The size of the
2319 * error is always one less than the size of the cycle we
2320 * broke. Commits C and D were "lost" from A's chain.
2322 * If we instead cut D->B, then the depth of A is correct at 3.
2323 * We keep all commits in the chain that we examined.
2325 cur
->dfs_state
= DFS_ACTIVE
;
2326 if (DELTA(cur
)->dfs_state
== DFS_ACTIVE
) {
2327 drop_reused_delta(cur
);
2328 cur
->dfs_state
= DFS_DONE
;
2334 * And now that we've gone all the way to the bottom of the chain, we
2335 * need to clear the active flags and set the depth fields as
2336 * appropriate. Unlike the loop above, which can quit when it drops a
2337 * delta, we need to keep going to look for more depth cuts. So we need
2338 * an extra "next" pointer to keep going after we reset cur->delta.
2340 for (cur
= entry
; cur
; cur
= next
) {
2344 * We should have a chain of zero or more ACTIVE states down to
2345 * a final DONE. We can quit after the DONE, because either it
2346 * has no bases, or we've already handled them in a previous
2349 if (cur
->dfs_state
== DFS_DONE
)
2351 else if (cur
->dfs_state
!= DFS_ACTIVE
)
2352 BUG("confusing delta dfs state in second pass: %d",
2356 * If the total_depth is more than depth, then we need to snip
2357 * the chain into two or more smaller chains that don't exceed
2358 * the maximum depth. Most of the resulting chains will contain
2359 * (depth + 1) entries (i.e., depth deltas plus one base), and
2360 * the last chain (i.e., the one containing entry) will contain
2361 * whatever entries are left over, namely
2362 * (total_depth % (depth + 1)) of them.
2364 * Since we are iterating towards decreasing depth, we need to
2365 * decrement total_depth as we go, and we need to write to the
2366 * entry what its final depth will be after all of the
2367 * snipping. Since we're snipping into chains of length (depth
2368 * + 1) entries, the final depth of an entry will be its
2369 * original depth modulo (depth + 1). Any time we encounter an
2370 * entry whose final depth is supposed to be zero, we snip it
2371 * from its delta base, thereby making it so.
2373 cur
->depth
= (total_depth
--) % (depth
+ 1);
2375 drop_reused_delta(cur
);
2377 cur
->dfs_state
= DFS_DONE
;
2381 static void get_object_details(void)
2384 struct object_entry
**sorted_by_offset
;
2387 progress_state
= start_progress(_("Counting objects"),
2388 to_pack
.nr_objects
);
2390 CALLOC_ARRAY(sorted_by_offset
, to_pack
.nr_objects
);
2391 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2392 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
2393 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
2395 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2396 struct object_entry
*entry
= sorted_by_offset
[i
];
2397 check_object(entry
, i
);
2398 if (entry
->type_valid
&&
2399 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
))
2400 entry
->no_try_delta
= 1;
2401 display_progress(progress_state
, i
+ 1);
2403 stop_progress(&progress_state
);
2406 * This must happen in a second pass, since we rely on the delta
2407 * information for the whole list being completed.
2409 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2410 break_delta_chains(&to_pack
.objects
[i
]);
2412 free(sorted_by_offset
);
2416 * We search for deltas in a list sorted by type, by filename hash, and then
2417 * by size, so that we see progressively smaller and smaller files.
2418 * That's because we prefer deltas to be from the bigger file
2419 * to the smaller -- deletes are potentially cheaper, but perhaps
2420 * more importantly, the bigger file is likely the more recent
2421 * one. The deepest deltas are therefore the oldest objects which are
2422 * less susceptible to be accessed often.
2424 static int type_size_sort(const void *_a
, const void *_b
)
2426 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2427 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2428 const enum object_type a_type
= oe_type(a
);
2429 const enum object_type b_type
= oe_type(b
);
2430 const unsigned long a_size
= SIZE(a
);
2431 const unsigned long b_size
= SIZE(b
);
2433 if (a_type
> b_type
)
2435 if (a_type
< b_type
)
2437 if (a
->hash
> b
->hash
)
2439 if (a
->hash
< b
->hash
)
2441 if (a
->preferred_base
> b
->preferred_base
)
2443 if (a
->preferred_base
< b
->preferred_base
)
2445 if (use_delta_islands
) {
2446 const int island_cmp
= island_delta_cmp(&a
->idx
.oid
, &b
->idx
.oid
);
2450 if (a_size
> b_size
)
2452 if (a_size
< b_size
)
2454 return a
< b
? -1 : (a
> b
); /* newest first */
2458 struct object_entry
*entry
;
2460 struct delta_index
*index
;
2464 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
2465 unsigned long delta_size
)
2467 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
2470 if (delta_size
< cache_max_small_delta_size
)
2473 /* cache delta, if objects are large enough compared to delta size */
2474 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
2480 /* Protect delta_cache_size */
2481 static pthread_mutex_t cache_mutex
;
2482 #define cache_lock() pthread_mutex_lock(&cache_mutex)
2483 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2486 * Protect object list partitioning (e.g. struct thread_param) and
2489 static pthread_mutex_t progress_mutex
;
2490 #define progress_lock() pthread_mutex_lock(&progress_mutex)
2491 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2494 * Access to struct object_entry is unprotected since each thread owns
2495 * a portion of the main object list. Just don't access object entries
2496 * ahead in the list because they can be stolen and would need
2497 * progress_mutex for protection.
2500 static inline int oe_size_less_than(struct packing_data
*pack
,
2501 const struct object_entry
*lhs
,
2504 if (lhs
->size_valid
)
2505 return lhs
->size_
< rhs
;
2506 if (rhs
< pack
->oe_size_limit
) /* rhs < 2^x <= lhs ? */
2508 return oe_get_size_slow(pack
, lhs
) < rhs
;
2511 static inline void oe_set_tree_depth(struct packing_data
*pack
,
2512 struct object_entry
*e
,
2513 unsigned int tree_depth
)
2515 if (!pack
->tree_depth
)
2516 CALLOC_ARRAY(pack
->tree_depth
, pack
->nr_alloc
);
2517 pack
->tree_depth
[e
- pack
->objects
] = tree_depth
;
2521 * Return the size of the object without doing any delta
2522 * reconstruction (so non-deltas are true object sizes, but deltas
2523 * return the size of the delta data).
2525 unsigned long oe_get_size_slow(struct packing_data
*pack
,
2526 const struct object_entry
*e
)
2528 struct packed_git
*p
;
2529 struct pack_window
*w_curs
;
2531 enum object_type type
;
2532 unsigned long used
, avail
, size
;
2534 if (e
->type_
!= OBJ_OFS_DELTA
&& e
->type_
!= OBJ_REF_DELTA
) {
2535 packing_data_lock(&to_pack
);
2536 if (oid_object_info(the_repository
, &e
->idx
.oid
, &size
) < 0)
2537 die(_("unable to get size of %s"),
2538 oid_to_hex(&e
->idx
.oid
));
2539 packing_data_unlock(&to_pack
);
2543 p
= oe_in_pack(pack
, e
);
2545 BUG("when e->type is a delta, it must belong to a pack");
2547 packing_data_lock(&to_pack
);
2549 buf
= use_pack(p
, &w_curs
, e
->in_pack_offset
, &avail
);
2550 used
= unpack_object_header_buffer(buf
, avail
, &type
, &size
);
2552 die(_("unable to parse object header of %s"),
2553 oid_to_hex(&e
->idx
.oid
));
2555 unuse_pack(&w_curs
);
2556 packing_data_unlock(&to_pack
);
2560 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
2561 unsigned max_depth
, unsigned long *mem_usage
)
2563 struct object_entry
*trg_entry
= trg
->entry
;
2564 struct object_entry
*src_entry
= src
->entry
;
2565 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
2567 enum object_type type
;
2570 /* Don't bother doing diffs between different types */
2571 if (oe_type(trg_entry
) != oe_type(src_entry
))
2575 * We do not bother to try a delta that we discarded on an
2576 * earlier try, but only when reusing delta data. Note that
2577 * src_entry that is marked as the preferred_base should always
2578 * be considered, as even if we produce a suboptimal delta against
2579 * it, we will still save the transfer cost, as we already know
2580 * the other side has it and we won't send src_entry at all.
2582 if (reuse_delta
&& IN_PACK(trg_entry
) &&
2583 IN_PACK(trg_entry
) == IN_PACK(src_entry
) &&
2584 !src_entry
->preferred_base
&&
2585 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
2586 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
2589 /* Let's not bust the allowed depth. */
2590 if (src
->depth
>= max_depth
)
2593 /* Now some size filtering heuristics. */
2594 trg_size
= SIZE(trg_entry
);
2595 if (!DELTA(trg_entry
)) {
2596 max_size
= trg_size
/2 - the_hash_algo
->rawsz
;
2599 max_size
= DELTA_SIZE(trg_entry
);
2600 ref_depth
= trg
->depth
;
2602 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
2603 (max_depth
- ref_depth
+ 1);
2606 src_size
= SIZE(src_entry
);
2607 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
2608 if (sizediff
>= max_size
)
2610 if (trg_size
< src_size
/ 32)
2613 if (!in_same_island(&trg
->entry
->idx
.oid
, &src
->entry
->idx
.oid
))
2616 /* Load data if not already done */
2618 packing_data_lock(&to_pack
);
2619 trg
->data
= repo_read_object_file(the_repository
,
2620 &trg_entry
->idx
.oid
, &type
,
2622 packing_data_unlock(&to_pack
);
2624 die(_("object %s cannot be read"),
2625 oid_to_hex(&trg_entry
->idx
.oid
));
2627 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2628 oid_to_hex(&trg_entry
->idx
.oid
), (uintmax_t)sz
,
2629 (uintmax_t)trg_size
);
2633 packing_data_lock(&to_pack
);
2634 src
->data
= repo_read_object_file(the_repository
,
2635 &src_entry
->idx
.oid
, &type
,
2637 packing_data_unlock(&to_pack
);
2639 if (src_entry
->preferred_base
) {
2640 static int warned
= 0;
2642 warning(_("object %s cannot be read"),
2643 oid_to_hex(&src_entry
->idx
.oid
));
2645 * Those objects are not included in the
2646 * resulting pack. Be resilient and ignore
2647 * them if they can't be read, in case the
2648 * pack could be created nevertheless.
2652 die(_("object %s cannot be read"),
2653 oid_to_hex(&src_entry
->idx
.oid
));
2656 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2657 oid_to_hex(&src_entry
->idx
.oid
), (uintmax_t)sz
,
2658 (uintmax_t)src_size
);
2662 src
->index
= create_delta_index(src
->data
, src_size
);
2664 static int warned
= 0;
2666 warning(_("suboptimal pack - out of memory"));
2669 *mem_usage
+= sizeof_delta_index(src
->index
);
2672 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
2676 if (DELTA(trg_entry
)) {
2677 /* Prefer only shallower same-sized deltas. */
2678 if (delta_size
== DELTA_SIZE(trg_entry
) &&
2679 src
->depth
+ 1 >= trg
->depth
) {
2686 * Handle memory allocation outside of the cache
2687 * accounting lock. Compiler will optimize the strangeness
2688 * away when NO_PTHREADS is defined.
2690 free(trg_entry
->delta_data
);
2692 if (trg_entry
->delta_data
) {
2693 delta_cache_size
-= DELTA_SIZE(trg_entry
);
2694 trg_entry
->delta_data
= NULL
;
2696 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
2697 delta_cache_size
+= delta_size
;
2699 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
2705 SET_DELTA(trg_entry
, src_entry
);
2706 SET_DELTA_SIZE(trg_entry
, delta_size
);
2707 trg
->depth
= src
->depth
+ 1;
2712 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
2714 struct object_entry
*child
= DELTA_CHILD(me
);
2717 const unsigned int c
= check_delta_limit(child
, n
+ 1);
2720 child
= DELTA_SIBLING(child
);
2725 static unsigned long free_unpacked(struct unpacked
*n
)
2727 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
2728 free_delta_index(n
->index
);
2731 freed_mem
+= SIZE(n
->entry
);
2732 FREE_AND_NULL(n
->data
);
2739 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
2740 int window
, int depth
, unsigned *processed
)
2742 uint32_t i
, idx
= 0, count
= 0;
2743 struct unpacked
*array
;
2744 unsigned long mem_usage
= 0;
2746 CALLOC_ARRAY(array
, window
);
2749 struct object_entry
*entry
;
2750 struct unpacked
*n
= array
+ idx
;
2751 int j
, max_depth
, best_base
= -1;
2760 if (!entry
->preferred_base
) {
2762 display_progress(progress_state
, *processed
);
2766 mem_usage
-= free_unpacked(n
);
2769 while (window_memory_limit
&&
2770 mem_usage
> window_memory_limit
&&
2772 const uint32_t tail
= (idx
+ window
- count
) % window
;
2773 mem_usage
-= free_unpacked(array
+ tail
);
2777 /* We do not compute delta to *create* objects we are not
2780 if (entry
->preferred_base
)
2784 * If the current object is at pack edge, take the depth the
2785 * objects that depend on the current object into account
2786 * otherwise they would become too deep.
2789 if (DELTA_CHILD(entry
)) {
2790 max_depth
-= check_delta_limit(entry
, 0);
2798 uint32_t other_idx
= idx
+ j
;
2800 if (other_idx
>= window
)
2801 other_idx
-= window
;
2802 m
= array
+ other_idx
;
2805 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2809 best_base
= other_idx
;
2813 * If we decided to cache the delta data, then it is best
2814 * to compress it right away. First because we have to do
2815 * it anyway, and doing it here while we're threaded will
2816 * save a lot of time in the non threaded write phase,
2817 * as well as allow for caching more deltas within
2818 * the same cache size limit.
2820 * But only if not writing to stdout, since in that case
2821 * the network is most likely throttling writes anyway,
2822 * and therefore it is best to go to the write phase ASAP
2823 * instead, as we can afford spending more time compressing
2824 * between writes at that moment.
2826 if (entry
->delta_data
&& !pack_to_stdout
) {
2829 size
= do_compress(&entry
->delta_data
, DELTA_SIZE(entry
));
2830 if (size
< (1U << OE_Z_DELTA_BITS
)) {
2831 entry
->z_delta_size
= size
;
2833 delta_cache_size
-= DELTA_SIZE(entry
);
2834 delta_cache_size
+= entry
->z_delta_size
;
2837 FREE_AND_NULL(entry
->delta_data
);
2838 entry
->z_delta_size
= 0;
2842 /* if we made n a delta, and if n is already at max
2843 * depth, leaving it in the window is pointless. we
2844 * should evict it first.
2846 if (DELTA(entry
) && max_depth
<= n
->depth
)
2850 * Move the best delta base up in the window, after the
2851 * currently deltified object, to keep it longer. It will
2852 * be the first base object to be attempted next.
2855 struct unpacked swap
= array
[best_base
];
2856 int dist
= (window
+ idx
- best_base
) % window
;
2857 int dst
= best_base
;
2859 int src
= (dst
+ 1) % window
;
2860 array
[dst
] = array
[src
];
2868 if (count
+ 1 < window
)
2874 for (i
= 0; i
< window
; ++i
) {
2875 free_delta_index(array
[i
].index
);
2876 free(array
[i
].data
);
2882 * The main object list is split into smaller lists, each is handed to
2885 * The main thread waits on the condition that (at least) one of the workers
2886 * has stopped working (which is indicated in the .working member of
2887 * struct thread_params).
2889 * When a work thread has completed its work, it sets .working to 0 and
2890 * signals the main thread and waits on the condition that .data_ready
2893 * The main thread steals half of the work from the worker that has
2894 * most work left to hand it to the idle worker.
2897 struct thread_params
{
2899 struct object_entry
**list
;
2906 pthread_mutex_t mutex
;
2907 pthread_cond_t cond
;
2908 unsigned *processed
;
2911 static pthread_cond_t progress_cond
;
2914 * Mutex and conditional variable can't be statically-initialized on Windows.
2916 static void init_threaded_search(void)
2918 pthread_mutex_init(&cache_mutex
, NULL
);
2919 pthread_mutex_init(&progress_mutex
, NULL
);
2920 pthread_cond_init(&progress_cond
, NULL
);
2923 static void cleanup_threaded_search(void)
2925 pthread_cond_destroy(&progress_cond
);
2926 pthread_mutex_destroy(&cache_mutex
);
2927 pthread_mutex_destroy(&progress_mutex
);
2930 static void *threaded_find_deltas(void *arg
)
2932 struct thread_params
*me
= arg
;
2935 while (me
->remaining
) {
2938 find_deltas(me
->list
, &me
->remaining
,
2939 me
->window
, me
->depth
, me
->processed
);
2943 pthread_cond_signal(&progress_cond
);
2947 * We must not set ->data_ready before we wait on the
2948 * condition because the main thread may have set it to 1
2949 * before we get here. In order to be sure that new
2950 * work is available if we see 1 in ->data_ready, it
2951 * was initialized to 0 before this thread was spawned
2952 * and we reset it to 0 right away.
2954 pthread_mutex_lock(&me
->mutex
);
2955 while (!me
->data_ready
)
2956 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2958 pthread_mutex_unlock(&me
->mutex
);
2963 /* leave ->working 1 so that this doesn't get more work assigned */
2967 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2968 int window
, int depth
, unsigned *processed
)
2970 struct thread_params
*p
;
2971 int i
, ret
, active_threads
= 0;
2973 init_threaded_search();
2975 if (delta_search_threads
<= 1) {
2976 find_deltas(list
, &list_size
, window
, depth
, processed
);
2977 cleanup_threaded_search();
2980 if (progress
> pack_to_stdout
)
2981 fprintf_ln(stderr
, _("Delta compression using up to %d threads"),
2982 delta_search_threads
);
2983 CALLOC_ARRAY(p
, delta_search_threads
);
2985 /* Partition the work amongst work threads. */
2986 for (i
= 0; i
< delta_search_threads
; i
++) {
2987 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
2989 /* don't use too small segments or no deltas will be found */
2990 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
2993 p
[i
].window
= window
;
2995 p
[i
].processed
= processed
;
2997 p
[i
].data_ready
= 0;
2999 /* try to split chunks on "path" boundaries */
3000 while (sub_size
&& sub_size
< list_size
&&
3001 list
[sub_size
]->hash
&&
3002 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
3006 p
[i
].list_size
= sub_size
;
3007 p
[i
].remaining
= sub_size
;
3010 list_size
-= sub_size
;
3013 /* Start work threads. */
3014 for (i
= 0; i
< delta_search_threads
; i
++) {
3015 if (!p
[i
].list_size
)
3017 pthread_mutex_init(&p
[i
].mutex
, NULL
);
3018 pthread_cond_init(&p
[i
].cond
, NULL
);
3019 ret
= pthread_create(&p
[i
].thread
, NULL
,
3020 threaded_find_deltas
, &p
[i
]);
3022 die(_("unable to create thread: %s"), strerror(ret
));
3027 * Now let's wait for work completion. Each time a thread is done
3028 * with its work, we steal half of the remaining work from the
3029 * thread with the largest number of unprocessed objects and give
3030 * it to that newly idle thread. This ensure good load balancing
3031 * until the remaining object list segments are simply too short
3032 * to be worth splitting anymore.
3034 while (active_threads
) {
3035 struct thread_params
*target
= NULL
;
3036 struct thread_params
*victim
= NULL
;
3037 unsigned sub_size
= 0;
3041 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
3046 pthread_cond_wait(&progress_cond
, &progress_mutex
);
3049 for (i
= 0; i
< delta_search_threads
; i
++)
3050 if (p
[i
].remaining
> 2*window
&&
3051 (!victim
|| victim
->remaining
< p
[i
].remaining
))
3054 sub_size
= victim
->remaining
/ 2;
3055 list
= victim
->list
+ victim
->list_size
- sub_size
;
3056 while (sub_size
&& list
[0]->hash
&&
3057 list
[0]->hash
== list
[-1]->hash
) {
3063 * It is possible for some "paths" to have
3064 * so many objects that no hash boundary
3065 * might be found. Let's just steal the
3066 * exact half in that case.
3068 sub_size
= victim
->remaining
/ 2;
3071 target
->list
= list
;
3072 victim
->list_size
-= sub_size
;
3073 victim
->remaining
-= sub_size
;
3075 target
->list_size
= sub_size
;
3076 target
->remaining
= sub_size
;
3077 target
->working
= 1;
3080 pthread_mutex_lock(&target
->mutex
);
3081 target
->data_ready
= 1;
3082 pthread_cond_signal(&target
->cond
);
3083 pthread_mutex_unlock(&target
->mutex
);
3086 pthread_join(target
->thread
, NULL
);
3087 pthread_cond_destroy(&target
->cond
);
3088 pthread_mutex_destroy(&target
->mutex
);
3092 cleanup_threaded_search();
3096 static int obj_is_packed(const struct object_id
*oid
)
3098 return packlist_find(&to_pack
, oid
) ||
3099 (reuse_packfile_bitmap
&&
3100 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
));
3103 static void add_tag_chain(const struct object_id
*oid
)
3108 * We catch duplicates already in add_object_entry(), but we'd
3109 * prefer to do this extra check to avoid having to parse the
3110 * tag at all if we already know that it's being packed (e.g., if
3111 * it was included via bitmaps, we would not have parsed it
3114 if (obj_is_packed(oid
))
3117 tag
= lookup_tag(the_repository
, oid
);
3119 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
3120 die(_("unable to pack objects reachable from tag %s"),
3123 add_object_entry(&tag
->object
.oid
, OBJ_TAG
, NULL
, 0);
3125 if (tag
->tagged
->type
!= OBJ_TAG
)
3128 tag
= (struct tag
*)tag
->tagged
;
3132 static int add_ref_tag(const char *tag UNUSED
, const struct object_id
*oid
,
3133 int flag UNUSED
, void *cb_data UNUSED
)
3135 struct object_id peeled
;
3137 if (!peel_iterated_oid(the_repository
, oid
, &peeled
) && obj_is_packed(&peeled
))
3142 static void prepare_pack(int window
, int depth
)
3144 struct object_entry
**delta_list
;
3145 uint32_t i
, nr_deltas
;
3148 if (use_delta_islands
)
3149 resolve_tree_islands(the_repository
, progress
, &to_pack
);
3151 get_object_details();
3154 * If we're locally repacking then we need to be doubly careful
3155 * from now on in order to make sure no stealth corruption gets
3156 * propagated to the new pack. Clients receiving streamed packs
3157 * should validate everything they get anyway so no need to incur
3158 * the additional cost here in that case.
3160 if (!pack_to_stdout
)
3161 do_check_packed_object_crc
= 1;
3163 if (!to_pack
.nr_objects
|| !window
|| !depth
)
3166 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
3169 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
3170 struct object_entry
*entry
= to_pack
.objects
+ i
;
3173 /* This happens if we decided to reuse existing
3174 * delta from a pack. "reuse_delta &&" is implied.
3178 if (!entry
->type_valid
||
3179 oe_size_less_than(&to_pack
, entry
, 50))
3182 if (entry
->no_try_delta
)
3185 if (!entry
->preferred_base
) {
3187 if (oe_type(entry
) < 0)
3188 die(_("unable to get type of object %s"),
3189 oid_to_hex(&entry
->idx
.oid
));
3191 if (oe_type(entry
) < 0) {
3193 * This object is not found, but we
3194 * don't have to include it anyway.
3200 delta_list
[n
++] = entry
;
3203 if (nr_deltas
&& n
> 1) {
3204 unsigned nr_done
= 0;
3207 progress_state
= start_progress(_("Compressing objects"),
3209 QSORT(delta_list
, n
, type_size_sort
);
3210 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
3211 stop_progress(&progress_state
);
3212 if (nr_done
!= nr_deltas
)
3213 die(_("inconsistency with delta count"));
3218 static int git_pack_config(const char *k
, const char *v
,
3219 const struct config_context
*ctx
, void *cb
)
3221 if (!strcmp(k
, "pack.window")) {
3222 window
= git_config_int(k
, v
, ctx
->kvi
);
3225 if (!strcmp(k
, "pack.windowmemory")) {
3226 window_memory_limit
= git_config_ulong(k
, v
, ctx
->kvi
);
3229 if (!strcmp(k
, "pack.depth")) {
3230 depth
= git_config_int(k
, v
, ctx
->kvi
);
3233 if (!strcmp(k
, "pack.deltacachesize")) {
3234 max_delta_cache_size
= git_config_int(k
, v
, ctx
->kvi
);
3237 if (!strcmp(k
, "pack.deltacachelimit")) {
3238 cache_max_small_delta_size
= git_config_int(k
, v
, ctx
->kvi
);
3241 if (!strcmp(k
, "pack.writebitmaphashcache")) {
3242 if (git_config_bool(k
, v
))
3243 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
3245 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
3248 if (!strcmp(k
, "pack.writebitmaplookuptable")) {
3249 if (git_config_bool(k
, v
))
3250 write_bitmap_options
|= BITMAP_OPT_LOOKUP_TABLE
;
3252 write_bitmap_options
&= ~BITMAP_OPT_LOOKUP_TABLE
;
3255 if (!strcmp(k
, "pack.usebitmaps")) {
3256 use_bitmap_index_default
= git_config_bool(k
, v
);
3259 if (!strcmp(k
, "pack.allowpackreuse")) {
3260 int res
= git_parse_maybe_bool_text(v
);
3262 if (!strcasecmp(v
, "single"))
3263 allow_pack_reuse
= SINGLE_PACK_REUSE
;
3264 else if (!strcasecmp(v
, "multi"))
3265 allow_pack_reuse
= MULTI_PACK_REUSE
;
3267 die(_("invalid pack.allowPackReuse value: '%s'"), v
);
3269 allow_pack_reuse
= SINGLE_PACK_REUSE
;
3271 allow_pack_reuse
= NO_PACK_REUSE
;
3275 if (!strcmp(k
, "pack.threads")) {
3276 delta_search_threads
= git_config_int(k
, v
, ctx
->kvi
);
3277 if (delta_search_threads
< 0)
3278 die(_("invalid number of threads specified (%d)"),
3279 delta_search_threads
);
3280 if (!HAVE_THREADS
&& delta_search_threads
!= 1) {
3281 warning(_("no threads support, ignoring %s"), k
);
3282 delta_search_threads
= 0;
3286 if (!strcmp(k
, "pack.indexversion")) {
3287 pack_idx_opts
.version
= git_config_int(k
, v
, ctx
->kvi
);
3288 if (pack_idx_opts
.version
> 2)
3289 die(_("bad pack.indexVersion=%"PRIu32
),
3290 pack_idx_opts
.version
);
3293 if (!strcmp(k
, "pack.writereverseindex")) {
3294 if (git_config_bool(k
, v
))
3295 pack_idx_opts
.flags
|= WRITE_REV
;
3297 pack_idx_opts
.flags
&= ~WRITE_REV
;
3300 if (!strcmp(k
, "uploadpack.blobpackfileuri")) {
3301 struct configured_exclusion
*ex
;
3302 const char *oid_end
, *pack_end
;
3304 * Stores the pack hash. This is not a true object ID, but is
3307 struct object_id pack_hash
;
3310 return config_error_nonbool(k
);
3312 ex
= xmalloc(sizeof(*ex
));
3313 if (parse_oid_hex(v
, &ex
->e
.oid
, &oid_end
) ||
3315 parse_oid_hex(oid_end
+ 1, &pack_hash
, &pack_end
) ||
3317 die(_("value of uploadpack.blobpackfileuri must be "
3318 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v
);
3319 if (oidmap_get(&configured_exclusions
, &ex
->e
.oid
))
3320 die(_("object already configured in another "
3321 "uploadpack.blobpackfileuri (got '%s')"), v
);
3322 ex
->pack_hash_hex
= xcalloc(1, pack_end
- oid_end
);
3323 memcpy(ex
->pack_hash_hex
, oid_end
+ 1, pack_end
- oid_end
- 1);
3324 ex
->uri
= xstrdup(pack_end
+ 1);
3325 oidmap_put(&configured_exclusions
, ex
);
3327 return git_default_config(k
, v
, ctx
, cb
);
3330 /* Counters for trace2 output when in --stdin-packs mode. */
3331 static int stdin_packs_found_nr
;
3332 static int stdin_packs_hints_nr
;
3334 static int add_object_entry_from_pack(const struct object_id
*oid
,
3335 struct packed_git
*p
,
3340 enum object_type type
= OBJ_NONE
;
3342 display_progress(progress_state
, ++nr_seen
);
3344 if (have_duplicate_entry(oid
, 0))
3347 ofs
= nth_packed_object_offset(p
, pos
);
3348 if (!want_object_in_pack(oid
, 0, &p
, &ofs
))
3352 struct rev_info
*revs
= _data
;
3353 struct object_info oi
= OBJECT_INFO_INIT
;
3356 if (packed_object_info(the_repository
, p
, ofs
, &oi
) < 0) {
3357 die(_("could not get type of object %s in pack %s"),
3358 oid_to_hex(oid
), p
->pack_name
);
3359 } else if (type
== OBJ_COMMIT
) {
3361 * commits in included packs are used as starting points for the
3362 * subsequent revision walk
3364 add_pending_oid(revs
, NULL
, oid
, 0);
3367 stdin_packs_found_nr
++;
3370 create_object_entry(oid
, type
, 0, 0, 0, p
, ofs
);
3375 static void show_commit_pack_hint(struct commit
*commit UNUSED
,
3378 /* nothing to do; commits don't have a namehash */
3381 static void show_object_pack_hint(struct object
*object
, const char *name
,
3384 struct object_entry
*oe
= packlist_find(&to_pack
, &object
->oid
);
3389 * Our 'to_pack' list was constructed by iterating all objects packed in
3390 * included packs, and so doesn't have a non-zero hash field that you
3391 * would typically pick up during a reachability traversal.
3393 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3394 * here using a now in order to perhaps improve the delta selection
3397 oe
->hash
= pack_name_hash(name
);
3398 oe
->no_try_delta
= name
&& no_try_delta(name
);
3400 stdin_packs_hints_nr
++;
3403 static int pack_mtime_cmp(const void *_a
, const void *_b
)
3405 struct packed_git
*a
= ((const struct string_list_item
*)_a
)->util
;
3406 struct packed_git
*b
= ((const struct string_list_item
*)_b
)->util
;
3409 * order packs by descending mtime so that objects are laid out
3410 * roughly as newest-to-oldest
3412 if (a
->mtime
< b
->mtime
)
3414 else if (b
->mtime
< a
->mtime
)
3420 static void read_packs_list_from_stdin(void)
3422 struct strbuf buf
= STRBUF_INIT
;
3423 struct string_list include_packs
= STRING_LIST_INIT_DUP
;
3424 struct string_list exclude_packs
= STRING_LIST_INIT_DUP
;
3425 struct string_list_item
*item
= NULL
;
3427 struct packed_git
*p
;
3428 struct rev_info revs
;
3430 repo_init_revisions(the_repository
, &revs
, NULL
);
3432 * Use a revision walk to fill in the namehash of objects in the include
3433 * packs. To save time, we'll avoid traversing through objects that are
3434 * in excluded packs.
3436 * That may cause us to avoid populating all of the namehash fields of
3437 * all included objects, but our goal is best-effort, since this is only
3438 * an optimization during delta selection.
3440 revs
.no_kept_objects
= 1;
3441 revs
.keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
3442 revs
.blob_objects
= 1;
3443 revs
.tree_objects
= 1;
3444 revs
.tag_objects
= 1;
3445 revs
.ignore_missing_links
= 1;
3447 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3451 if (*buf
.buf
== '^')
3452 string_list_append(&exclude_packs
, buf
.buf
+ 1);
3454 string_list_append(&include_packs
, buf
.buf
);
3459 string_list_sort(&include_packs
);
3460 string_list_remove_duplicates(&include_packs
, 0);
3461 string_list_sort(&exclude_packs
);
3462 string_list_remove_duplicates(&exclude_packs
, 0);
3464 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3465 const char *pack_name
= pack_basename(p
);
3467 if ((item
= string_list_lookup(&include_packs
, pack_name
)))
3469 if ((item
= string_list_lookup(&exclude_packs
, pack_name
)))
3474 * Arguments we got on stdin may not even be packs. First
3475 * check that to avoid segfaulting later on in
3476 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3478 * Since we first parsed our STDIN and then sorted the input
3479 * lines the pack we error on will be whatever line happens to
3480 * sort first. This is lazy, it's enough that we report one
3481 * bad case here, we don't need to report the first/last one,
3484 for_each_string_list_item(item
, &include_packs
) {
3485 struct packed_git
*p
= item
->util
;
3487 die(_("could not find pack '%s'"), item
->string
);
3488 if (!is_pack_valid(p
))
3489 die(_("packfile %s cannot be accessed"), p
->pack_name
);
3493 * Then, handle all of the excluded packs, marking them as
3494 * kept in-core so that later calls to add_object_entry()
3495 * discards any objects that are also found in excluded packs.
3497 for_each_string_list_item(item
, &exclude_packs
) {
3498 struct packed_git
*p
= item
->util
;
3500 die(_("could not find pack '%s'"), item
->string
);
3501 p
->pack_keep_in_core
= 1;
3505 * Order packs by ascending mtime; use QSORT directly to access the
3506 * string_list_item's ->util pointer, which string_list_sort() does not
3509 QSORT(include_packs
.items
, include_packs
.nr
, pack_mtime_cmp
);
3511 for_each_string_list_item(item
, &include_packs
) {
3512 struct packed_git
*p
= item
->util
;
3513 for_each_object_in_pack(p
,
3514 add_object_entry_from_pack
,
3516 FOR_EACH_OBJECT_PACK_ORDER
);
3519 if (prepare_revision_walk(&revs
))
3520 die(_("revision walk setup failed"));
3521 traverse_commit_list(&revs
,
3522 show_commit_pack_hint
,
3523 show_object_pack_hint
,
3526 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_found",
3527 stdin_packs_found_nr
);
3528 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_hints",
3529 stdin_packs_hints_nr
);
3531 strbuf_release(&buf
);
3532 string_list_clear(&include_packs
, 0);
3533 string_list_clear(&exclude_packs
, 0);
3536 static void add_cruft_object_entry(const struct object_id
*oid
, enum object_type type
,
3537 struct packed_git
*pack
, off_t offset
,
3538 const char *name
, uint32_t mtime
)
3540 struct object_entry
*entry
;
3542 display_progress(progress_state
, ++nr_seen
);
3544 entry
= packlist_find(&to_pack
, oid
);
3547 entry
->hash
= pack_name_hash(name
);
3548 entry
->no_try_delta
= no_try_delta(name
);
3551 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
3553 if (!pack
&& type
== OBJ_BLOB
&& !has_loose_object(oid
)) {
3555 * If a traversed tree has a missing blob then we want
3556 * to avoid adding that missing object to our pack.
3558 * This only applies to missing blobs, not trees,
3559 * because the traversal needs to parse sub-trees but
3562 * Note we only perform this check when we couldn't
3563 * already find the object in a pack, so we're really
3564 * limited to "ensure non-tip blobs which don't exist in
3565 * packs do exist via loose objects". Confused?
3570 entry
= create_object_entry(oid
, type
, pack_name_hash(name
),
3571 0, name
&& no_try_delta(name
),
3575 if (mtime
> oe_cruft_mtime(&to_pack
, entry
))
3576 oe_set_cruft_mtime(&to_pack
, entry
, mtime
);
3580 static void show_cruft_object(struct object
*obj
, const char *name
, void *data UNUSED
)
3583 * if we did not record it earlier, it's at least as old as our
3584 * expiration value. Rather than find it exactly, just use that
3585 * value. This may bump it forward from its real mtime, but it
3586 * will still be "too old" next time we run with the same
3589 * if obj does appear in the packing list, this call is a noop (or may
3590 * set the namehash).
3592 add_cruft_object_entry(&obj
->oid
, obj
->type
, NULL
, 0, name
, cruft_expiration
);
3595 static void show_cruft_commit(struct commit
*commit
, void *data
)
3597 show_cruft_object((struct object
*)commit
, NULL
, data
);
3600 static int cruft_include_check_obj(struct object
*obj
, void *data UNUSED
)
3602 return !has_object_kept_pack(&obj
->oid
, IN_CORE_KEEP_PACKS
);
3605 static int cruft_include_check(struct commit
*commit
, void *data
)
3607 return cruft_include_check_obj((struct object
*)commit
, data
);
3610 static void set_cruft_mtime(const struct object
*object
,
3611 struct packed_git
*pack
,
3612 off_t offset
, time_t mtime
)
3614 add_cruft_object_entry(&object
->oid
, object
->type
, pack
, offset
, NULL
,
3618 static void mark_pack_kept_in_core(struct string_list
*packs
, unsigned keep
)
3620 struct string_list_item
*item
= NULL
;
3621 for_each_string_list_item(item
, packs
) {
3622 struct packed_git
*p
= item
->util
;
3624 die(_("could not find pack '%s'"), item
->string
);
3625 p
->pack_keep_in_core
= keep
;
3629 static void add_unreachable_loose_objects(void);
3630 static void add_objects_in_unpacked_packs(void);
3632 static void enumerate_cruft_objects(void)
3635 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3637 add_objects_in_unpacked_packs();
3638 add_unreachable_loose_objects();
3640 stop_progress(&progress_state
);
3643 static void enumerate_and_traverse_cruft_objects(struct string_list
*fresh_packs
)
3645 struct packed_git
*p
;
3646 struct rev_info revs
;
3649 repo_init_revisions(the_repository
, &revs
, NULL
);
3651 revs
.tag_objects
= 1;
3652 revs
.tree_objects
= 1;
3653 revs
.blob_objects
= 1;
3655 revs
.include_check
= cruft_include_check
;
3656 revs
.include_check_obj
= cruft_include_check_obj
;
3658 revs
.ignore_missing_links
= 1;
3661 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3662 ret
= add_unseen_recent_objects_to_traversal(&revs
, cruft_expiration
,
3663 set_cruft_mtime
, 1);
3664 stop_progress(&progress_state
);
3667 die(_("unable to add cruft objects"));
3670 * Re-mark only the fresh packs as kept so that objects in
3671 * unknown packs do not halt the reachability traversal early.
3673 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
3674 p
->pack_keep_in_core
= 0;
3675 mark_pack_kept_in_core(fresh_packs
, 1);
3677 if (prepare_revision_walk(&revs
))
3678 die(_("revision walk setup failed"));
3680 progress_state
= start_progress(_("Traversing cruft objects"), 0);
3682 traverse_commit_list(&revs
, show_cruft_commit
, show_cruft_object
, NULL
);
3684 stop_progress(&progress_state
);
3687 static void read_cruft_objects(void)
3689 struct strbuf buf
= STRBUF_INIT
;
3690 struct string_list discard_packs
= STRING_LIST_INIT_DUP
;
3691 struct string_list fresh_packs
= STRING_LIST_INIT_DUP
;
3692 struct packed_git
*p
;
3694 ignore_packed_keep_in_core
= 1;
3696 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3700 if (*buf
.buf
== '-')
3701 string_list_append(&discard_packs
, buf
.buf
+ 1);
3703 string_list_append(&fresh_packs
, buf
.buf
);
3706 string_list_sort(&discard_packs
);
3707 string_list_sort(&fresh_packs
);
3709 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3710 const char *pack_name
= pack_basename(p
);
3711 struct string_list_item
*item
;
3713 item
= string_list_lookup(&fresh_packs
, pack_name
);
3715 item
= string_list_lookup(&discard_packs
, pack_name
);
3721 * This pack wasn't mentioned in either the "fresh" or
3722 * "discard" list, so the caller didn't know about it.
3724 * Mark it as kept so that its objects are ignored by
3725 * add_unseen_recent_objects_to_traversal(). We'll
3726 * unmark it before starting the traversal so it doesn't
3727 * halt the traversal early.
3729 p
->pack_keep_in_core
= 1;
3733 mark_pack_kept_in_core(&fresh_packs
, 1);
3734 mark_pack_kept_in_core(&discard_packs
, 0);
3736 if (cruft_expiration
)
3737 enumerate_and_traverse_cruft_objects(&fresh_packs
);
3739 enumerate_cruft_objects();
3741 strbuf_release(&buf
);
3742 string_list_clear(&discard_packs
, 0);
3743 string_list_clear(&fresh_packs
, 0);
3746 static void read_object_list_from_stdin(void)
3748 char line
[GIT_MAX_HEXSZ
+ 1 + PATH_MAX
+ 2];
3749 struct object_id oid
;
3753 if (!fgets(line
, sizeof(line
), stdin
)) {
3757 BUG("fgets returned NULL, not EOF, not error!");
3763 if (line
[0] == '-') {
3764 if (get_oid_hex(line
+1, &oid
))
3765 die(_("expected edge object ID, got garbage:\n %s"),
3767 add_preferred_base(&oid
);
3770 if (parse_oid_hex(line
, &oid
, &p
))
3771 die(_("expected object ID, got garbage:\n %s"), line
);
3773 add_preferred_base_object(p
+ 1);
3774 add_object_entry(&oid
, OBJ_NONE
, p
+ 1, 0);
3778 static void show_commit(struct commit
*commit
, void *data UNUSED
)
3780 add_object_entry(&commit
->object
.oid
, OBJ_COMMIT
, NULL
, 0);
3782 if (write_bitmap_index
)
3783 index_commit_for_bitmap(commit
);
3785 if (use_delta_islands
)
3786 propagate_island_marks(commit
);
3789 static void show_object(struct object
*obj
, const char *name
,
3792 add_preferred_base_object(name
);
3793 add_object_entry(&obj
->oid
, obj
->type
, name
, 0);
3795 if (use_delta_islands
) {
3798 struct object_entry
*ent
;
3800 /* the empty string is a root tree, which is depth 0 */
3801 depth
= *name
? 1 : 0;
3802 for (p
= strchr(name
, '/'); p
; p
= strchr(p
+ 1, '/'))
3805 ent
= packlist_find(&to_pack
, &obj
->oid
);
3806 if (ent
&& depth
> oe_tree_depth(&to_pack
, ent
))
3807 oe_set_tree_depth(&to_pack
, ent
, depth
);
3811 static void show_object__ma_allow_any(struct object
*obj
, const char *name
, void *data
)
3813 assert(arg_missing_action
== MA_ALLOW_ANY
);
3816 * Quietly ignore ALL missing objects. This avoids problems with
3817 * staging them now and getting an odd error later.
3819 if (!has_object(the_repository
, &obj
->oid
, 0))
3822 show_object(obj
, name
, data
);
3825 static void show_object__ma_allow_promisor(struct object
*obj
, const char *name
, void *data
)
3827 assert(arg_missing_action
== MA_ALLOW_PROMISOR
);
3830 * Quietly ignore EXPECTED missing objects. This avoids problems with
3831 * staging them now and getting an odd error later.
3833 if (!has_object(the_repository
, &obj
->oid
, 0) && is_promisor_object(&obj
->oid
))
3836 show_object(obj
, name
, data
);
3839 static int option_parse_missing_action(const struct option
*opt UNUSED
,
3840 const char *arg
, int unset
)
3845 if (!strcmp(arg
, "error")) {
3846 arg_missing_action
= MA_ERROR
;
3847 fn_show_object
= show_object
;
3851 if (!strcmp(arg
, "allow-any")) {
3852 arg_missing_action
= MA_ALLOW_ANY
;
3853 fetch_if_missing
= 0;
3854 fn_show_object
= show_object__ma_allow_any
;
3858 if (!strcmp(arg
, "allow-promisor")) {
3859 arg_missing_action
= MA_ALLOW_PROMISOR
;
3860 fetch_if_missing
= 0;
3861 fn_show_object
= show_object__ma_allow_promisor
;
3865 die(_("invalid value for '%s': '%s'"), "--missing", arg
);
3869 static void show_edge(struct commit
*commit
)
3871 add_preferred_base(&commit
->object
.oid
);
3874 static int add_object_in_unpacked_pack(const struct object_id
*oid
,
3875 struct packed_git
*pack
,
3883 if (pack
->is_cruft
) {
3884 if (load_pack_mtimes(pack
) < 0)
3885 die(_("could not load cruft pack .mtimes"));
3886 mtime
= nth_packed_mtime(pack
, pos
);
3888 mtime
= pack
->mtime
;
3890 offset
= nth_packed_object_offset(pack
, pos
);
3892 add_cruft_object_entry(oid
, OBJ_NONE
, pack
, offset
,
3895 add_object_entry(oid
, OBJ_NONE
, "", 0);
3900 static void add_objects_in_unpacked_packs(void)
3902 if (for_each_packed_object(add_object_in_unpacked_pack
, NULL
,
3903 FOR_EACH_OBJECT_PACK_ORDER
|
3904 FOR_EACH_OBJECT_LOCAL_ONLY
|
3905 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
|
3906 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS
))
3907 die(_("cannot open pack index"));
3910 static int add_loose_object(const struct object_id
*oid
, const char *path
,
3913 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
3916 warning(_("loose object at %s could not be examined"), path
);
3922 if (stat(path
, &st
) < 0) {
3923 if (errno
== ENOENT
)
3925 return error_errno("unable to stat %s", oid_to_hex(oid
));
3928 add_cruft_object_entry(oid
, type
, NULL
, 0, NULL
,
3931 add_object_entry(oid
, type
, "", 0);
3937 * We actually don't even have to worry about reachability here.
3938 * add_object_entry will weed out duplicates, so we just add every
3939 * loose object we find.
3941 static void add_unreachable_loose_objects(void)
3943 for_each_loose_file_in_objdir(get_object_directory(),
3948 static int has_sha1_pack_kept_or_nonlocal(const struct object_id
*oid
)
3950 static struct packed_git
*last_found
= (void *)1;
3951 struct packed_git
*p
;
3953 p
= (last_found
!= (void *)1) ? last_found
:
3954 get_all_packs(the_repository
);
3957 if ((!p
->pack_local
|| p
->pack_keep
||
3958 p
->pack_keep_in_core
) &&
3959 find_pack_entry_one(oid
->hash
, p
)) {
3963 if (p
== last_found
)
3964 p
= get_all_packs(the_repository
);
3967 if (p
== last_found
)
3974 * Store a list of sha1s that are should not be discarded
3975 * because they are either written too recently, or are
3976 * reachable from another object that was.
3978 * This is filled by get_object_list.
3980 static struct oid_array recent_objects
;
3982 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
3985 if (!unpack_unreachable_expiration
)
3987 if (mtime
> unpack_unreachable_expiration
)
3989 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
3994 static void loosen_unused_packed_objects(void)
3996 struct packed_git
*p
;
3998 uint32_t loosened_objects_nr
= 0;
3999 struct object_id oid
;
4001 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4002 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
4005 if (open_pack_index(p
))
4006 die(_("cannot open pack index"));
4008 for (i
= 0; i
< p
->num_objects
; i
++) {
4009 nth_packed_object_id(&oid
, p
, i
);
4010 if (!packlist_find(&to_pack
, &oid
) &&
4011 !has_sha1_pack_kept_or_nonlocal(&oid
) &&
4012 !loosened_object_can_be_discarded(&oid
, p
->mtime
)) {
4013 if (force_object_loose(&oid
, p
->mtime
))
4014 die(_("unable to force loose object"));
4015 loosened_objects_nr
++;
4020 trace2_data_intmax("pack-objects", the_repository
,
4021 "loosen_unused_packed_objects/loosened", loosened_objects_nr
);
4025 * This tracks any options which pack-reuse code expects to be on, or which a
4026 * reader of the pack might not understand, and which would therefore prevent
4027 * blind reuse of what we have on disk.
4029 static int pack_options_allow_reuse(void)
4031 return allow_pack_reuse
!= NO_PACK_REUSE
&&
4033 !ignore_packed_keep_on_disk
&&
4034 !ignore_packed_keep_in_core
&&
4035 (!local
|| !have_non_local_packs
) &&
4039 static int get_object_list_from_bitmap(struct rev_info
*revs
)
4041 if (!(bitmap_git
= prepare_bitmap_walk(revs
, 0)))
4044 if (pack_options_allow_reuse())
4045 reuse_partial_packfile_from_bitmap(bitmap_git
,
4047 &reuse_packfiles_nr
,
4048 &reuse_packfile_bitmap
,
4049 allow_pack_reuse
== MULTI_PACK_REUSE
);
4051 if (reuse_packfiles
) {
4052 reuse_packfile_objects
= bitmap_popcount(reuse_packfile_bitmap
);
4053 if (!reuse_packfile_objects
)
4054 BUG("expected non-empty reuse bitmap");
4056 nr_result
+= reuse_packfile_objects
;
4057 nr_seen
+= reuse_packfile_objects
;
4058 display_progress(progress_state
, nr_seen
);
4061 traverse_bitmap_commit_list(bitmap_git
, revs
,
4062 &add_object_entry_from_bitmap
);
4066 static void record_recent_object(struct object
*obj
,
4067 const char *name UNUSED
,
4070 oid_array_append(&recent_objects
, &obj
->oid
);
4073 static void record_recent_commit(struct commit
*commit
, void *data UNUSED
)
4075 oid_array_append(&recent_objects
, &commit
->object
.oid
);
4078 static int mark_bitmap_preferred_tip(const char *refname
,
4079 const struct object_id
*oid
,
4083 struct object_id peeled
;
4084 struct object
*object
;
4086 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
4089 object
= parse_object_or_die(oid
, refname
);
4090 if (object
->type
== OBJ_COMMIT
)
4091 object
->flags
|= NEEDS_BITMAP
;
4096 static void mark_bitmap_preferred_tips(void)
4098 struct string_list_item
*item
;
4099 const struct string_list
*preferred_tips
;
4101 preferred_tips
= bitmap_preferred_tips(the_repository
);
4102 if (!preferred_tips
)
4105 for_each_string_list_item(item
, preferred_tips
) {
4106 refs_for_each_ref_in(get_main_ref_store(the_repository
),
4107 item
->string
, mark_bitmap_preferred_tip
,
4112 static void get_object_list(struct rev_info
*revs
, int ac
, const char **av
)
4114 struct setup_revision_opt s_r_opt
= {
4115 .allow_exclude_promisor_objects
= 1,
4121 save_commit_buffer
= 0;
4122 setup_revisions(ac
, av
, revs
, &s_r_opt
);
4124 /* make sure shallows are read */
4125 is_repository_shallow(the_repository
);
4127 save_warning
= warn_on_object_refname_ambiguity
;
4128 warn_on_object_refname_ambiguity
= 0;
4130 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
4131 int len
= strlen(line
);
4132 if (len
&& line
[len
- 1] == '\n')
4137 if (!strcmp(line
, "--not")) {
4138 flags
^= UNINTERESTING
;
4139 write_bitmap_index
= 0;
4142 if (starts_with(line
, "--shallow ")) {
4143 struct object_id oid
;
4144 if (get_oid_hex(line
+ 10, &oid
))
4145 die("not an object name '%s'", line
+ 10);
4146 register_shallow(the_repository
, &oid
);
4147 use_bitmap_index
= 0;
4150 die(_("not a rev '%s'"), line
);
4152 if (handle_revision_arg(line
, revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
4153 die(_("bad revision '%s'"), line
);
4156 warn_on_object_refname_ambiguity
= save_warning
;
4158 if (use_bitmap_index
&& !get_object_list_from_bitmap(revs
))
4161 if (use_delta_islands
)
4162 load_delta_islands(the_repository
, progress
);
4164 if (write_bitmap_index
)
4165 mark_bitmap_preferred_tips();
4167 if (prepare_revision_walk(revs
))
4168 die(_("revision walk setup failed"));
4169 mark_edges_uninteresting(revs
, show_edge
, sparse
);
4171 if (!fn_show_object
)
4172 fn_show_object
= show_object
;
4173 traverse_commit_list(revs
,
4174 show_commit
, fn_show_object
,
4177 if (unpack_unreachable_expiration
) {
4178 revs
->ignore_missing_links
= 1;
4179 if (add_unseen_recent_objects_to_traversal(revs
,
4180 unpack_unreachable_expiration
, NULL
, 0))
4181 die(_("unable to add recent objects"));
4182 if (prepare_revision_walk(revs
))
4183 die(_("revision walk setup failed"));
4184 traverse_commit_list(revs
, record_recent_commit
,
4185 record_recent_object
, NULL
);
4188 if (keep_unreachable
)
4189 add_objects_in_unpacked_packs();
4190 if (pack_loose_unreachable
)
4191 add_unreachable_loose_objects();
4192 if (unpack_unreachable
)
4193 loosen_unused_packed_objects();
4195 oid_array_clear(&recent_objects
);
4198 static void add_extra_kept_packs(const struct string_list
*names
)
4200 struct packed_git
*p
;
4205 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4206 const char *name
= basename(p
->pack_name
);
4212 for (i
= 0; i
< names
->nr
; i
++)
4213 if (!fspathcmp(name
, names
->items
[i
].string
))
4216 if (i
< names
->nr
) {
4217 p
->pack_keep_in_core
= 1;
4218 ignore_packed_keep_in_core
= 1;
4224 static int option_parse_quiet(const struct option
*opt
, const char *arg
,
4227 int *val
= opt
->value
;
4229 BUG_ON_OPT_ARG(arg
);
4238 static int option_parse_index_version(const struct option
*opt
,
4239 const char *arg
, int unset
)
4241 struct pack_idx_option
*popts
= opt
->value
;
4243 const char *val
= arg
;
4245 BUG_ON_OPT_NEG(unset
);
4247 popts
->version
= strtoul(val
, &c
, 10);
4248 if (popts
->version
> 2)
4249 die(_("unsupported index version %s"), val
);
4250 if (*c
== ',' && c
[1])
4251 popts
->off32_limit
= strtoul(c
+1, &c
, 0);
4252 if (*c
|| popts
->off32_limit
& 0x80000000)
4253 die(_("bad index version '%s'"), val
);
4257 static int option_parse_unpack_unreachable(const struct option
*opt UNUSED
,
4258 const char *arg
, int unset
)
4261 unpack_unreachable
= 0;
4262 unpack_unreachable_expiration
= 0;
4265 unpack_unreachable
= 1;
4267 unpack_unreachable_expiration
= approxidate(arg
);
4272 static int option_parse_cruft_expiration(const struct option
*opt UNUSED
,
4273 const char *arg
, int unset
)
4277 cruft_expiration
= 0;
4281 cruft_expiration
= approxidate(arg
);
4286 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
4288 int use_internal_rev_list
= 0;
4290 int all_progress_implied
= 0;
4291 struct strvec rp
= STRVEC_INIT
;
4292 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
4293 int rev_list_index
= 0;
4294 int stdin_packs
= 0;
4295 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
4296 struct list_objects_filter_options filter_options
=
4297 LIST_OBJECTS_FILTER_INIT
;
4299 struct option pack_objects_options
[] = {
4300 OPT_CALLBACK_F('q', "quiet", &progress
, NULL
,
4301 N_("do not show progress meter"),
4302 PARSE_OPT_NOARG
, option_parse_quiet
),
4303 OPT_SET_INT(0, "progress", &progress
,
4304 N_("show progress meter"), 1),
4305 OPT_SET_INT(0, "all-progress", &progress
,
4306 N_("show progress meter during object writing phase"), 2),
4307 OPT_BOOL(0, "all-progress-implied",
4308 &all_progress_implied
,
4309 N_("similar to --all-progress when progress meter is shown")),
4310 OPT_CALLBACK_F(0, "index-version", &pack_idx_opts
, N_("<version>[,<offset>]"),
4311 N_("write the pack index file in the specified idx format version"),
4312 PARSE_OPT_NONEG
, option_parse_index_version
),
4313 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
4314 N_("maximum size of each output pack file")),
4315 OPT_BOOL(0, "local", &local
,
4316 N_("ignore borrowed objects from alternate object store")),
4317 OPT_BOOL(0, "incremental", &incremental
,
4318 N_("ignore packed objects")),
4319 OPT_INTEGER(0, "window", &window
,
4320 N_("limit pack window by objects")),
4321 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
4322 N_("limit pack window by memory in addition to object limit")),
4323 OPT_INTEGER(0, "depth", &depth
,
4324 N_("maximum length of delta chain allowed in the resulting pack")),
4325 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
4326 N_("reuse existing deltas")),
4327 OPT_BOOL(0, "reuse-object", &reuse_object
,
4328 N_("reuse existing objects")),
4329 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
4330 N_("use OFS_DELTA objects")),
4331 OPT_INTEGER(0, "threads", &delta_search_threads
,
4332 N_("use threads when searching for best delta matches")),
4333 OPT_BOOL(0, "non-empty", &non_empty
,
4334 N_("do not create an empty pack output")),
4335 OPT_BOOL(0, "revs", &use_internal_rev_list
,
4336 N_("read revision arguments from standard input")),
4337 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked
,
4338 N_("limit the objects to those that are not yet packed"),
4339 1, PARSE_OPT_NONEG
),
4340 OPT_SET_INT_F(0, "all", &rev_list_all
,
4341 N_("include objects reachable from any reference"),
4342 1, PARSE_OPT_NONEG
),
4343 OPT_SET_INT_F(0, "reflog", &rev_list_reflog
,
4344 N_("include objects referred by reflog entries"),
4345 1, PARSE_OPT_NONEG
),
4346 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index
,
4347 N_("include objects referred to by the index"),
4348 1, PARSE_OPT_NONEG
),
4349 OPT_BOOL(0, "stdin-packs", &stdin_packs
,
4350 N_("read packs from stdin")),
4351 OPT_BOOL(0, "stdout", &pack_to_stdout
,
4352 N_("output pack to stdout")),
4353 OPT_BOOL(0, "include-tag", &include_tag
,
4354 N_("include tag objects that refer to objects to be packed")),
4355 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
4356 N_("keep unreachable objects")),
4357 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
4358 N_("pack loose unreachable objects")),
4359 OPT_CALLBACK_F(0, "unpack-unreachable", NULL
, N_("time"),
4360 N_("unpack unreachable objects newer than <time>"),
4361 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
),
4362 OPT_BOOL(0, "cruft", &cruft
, N_("create a cruft pack")),
4363 OPT_CALLBACK_F(0, "cruft-expiration", NULL
, N_("time"),
4364 N_("expire cruft objects older than <time>"),
4365 PARSE_OPT_OPTARG
, option_parse_cruft_expiration
),
4366 OPT_BOOL(0, "sparse", &sparse
,
4367 N_("use the sparse reachability algorithm")),
4368 OPT_BOOL(0, "thin", &thin
,
4369 N_("create thin packs")),
4370 OPT_BOOL(0, "shallow", &shallow
,
4371 N_("create packs suitable for shallow fetches")),
4372 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk
,
4373 N_("ignore packs that have companion .keep file")),
4374 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
4375 N_("ignore this pack")),
4376 OPT_INTEGER(0, "compression", &pack_compression_level
,
4377 N_("pack compression level")),
4378 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents
,
4379 N_("do not hide commits by grafts")),
4380 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
4381 N_("use a bitmap index if available to speed up counting objects")),
4382 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index
,
4383 N_("write a bitmap index together with the pack index"),
4385 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4386 &write_bitmap_index
,
4387 N_("write a bitmap index if possible"),
4388 WRITE_BITMAP_QUIET
, PARSE_OPT_HIDDEN
),
4389 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
4390 OPT_CALLBACK_F(0, "missing", NULL
, N_("action"),
4391 N_("handling for missing objects"), PARSE_OPT_NONEG
,
4392 option_parse_missing_action
),
4393 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
4394 N_("do not pack objects in promisor packfiles")),
4395 OPT_BOOL(0, "delta-islands", &use_delta_islands
,
4396 N_("respect islands during delta compression")),
4397 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols
,
4399 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4403 if (DFS_NUM_STATES
> (1 << OE_DFS_STATE_BITS
))
4404 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4406 disable_replace_refs();
4408 sparse
= git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4409 if (the_repository
->gitdir
) {
4410 prepare_repo_settings(the_repository
);
4412 sparse
= the_repository
->settings
.pack_use_sparse
;
4413 if (the_repository
->settings
.pack_use_multi_pack_reuse
)
4414 allow_pack_reuse
= MULTI_PACK_REUSE
;
4417 reset_pack_idx_option(&pack_idx_opts
);
4418 pack_idx_opts
.flags
|= WRITE_REV
;
4419 git_config(git_pack_config
, NULL
);
4420 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX
, 0))
4421 pack_idx_opts
.flags
&= ~WRITE_REV
;
4423 progress
= isatty(2);
4424 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
4428 base_name
= argv
[0];
4431 if (pack_to_stdout
!= !base_name
|| argc
)
4432 usage_with_options(pack_usage
, pack_objects_options
);
4436 if (depth
>= (1 << OE_DEPTH_BITS
)) {
4437 warning(_("delta chain depth %d is too deep, forcing %d"),
4438 depth
, (1 << OE_DEPTH_BITS
) - 1);
4439 depth
= (1 << OE_DEPTH_BITS
) - 1;
4441 if (cache_max_small_delta_size
>= (1U << OE_Z_DELTA_BITS
)) {
4442 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4443 (1U << OE_Z_DELTA_BITS
) - 1);
4444 cache_max_small_delta_size
= (1U << OE_Z_DELTA_BITS
) - 1;
4449 strvec_push(&rp
, "pack-objects");
4451 use_internal_rev_list
= 1;
4452 strvec_push(&rp
, shallow
4453 ? "--objects-edge-aggressive"
4454 : "--objects-edge");
4456 strvec_push(&rp
, "--objects");
4459 use_internal_rev_list
= 1;
4460 strvec_push(&rp
, "--all");
4462 if (rev_list_reflog
) {
4463 use_internal_rev_list
= 1;
4464 strvec_push(&rp
, "--reflog");
4466 if (rev_list_index
) {
4467 use_internal_rev_list
= 1;
4468 strvec_push(&rp
, "--indexed-objects");
4470 if (rev_list_unpacked
&& !stdin_packs
) {
4471 use_internal_rev_list
= 1;
4472 strvec_push(&rp
, "--unpacked");
4475 if (exclude_promisor_objects
) {
4476 use_internal_rev_list
= 1;
4477 fetch_if_missing
= 0;
4478 strvec_push(&rp
, "--exclude-promisor-objects");
4480 if (unpack_unreachable
|| keep_unreachable
|| pack_loose_unreachable
)
4481 use_internal_rev_list
= 1;
4485 if (pack_compression_level
== -1)
4486 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
4487 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
4488 die(_("bad pack compression level %d"), pack_compression_level
);
4490 if (!delta_search_threads
) /* --threads=0 means autodetect */
4491 delta_search_threads
= online_cpus();
4493 if (!HAVE_THREADS
&& delta_search_threads
!= 1)
4494 warning(_("no threads support, ignoring --threads"));
4495 if (!pack_to_stdout
&& !pack_size_limit
)
4496 pack_size_limit
= pack_size_limit_cfg
;
4497 if (pack_to_stdout
&& pack_size_limit
)
4498 die(_("--max-pack-size cannot be used to build a pack for transfer"));
4499 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
4500 warning(_("minimum pack size limit is 1 MiB"));
4501 pack_size_limit
= 1024*1024;
4504 if (!pack_to_stdout
&& thin
)
4505 die(_("--thin cannot be used to build an indexable pack"));
4507 if (keep_unreachable
&& unpack_unreachable
)
4508 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
4509 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
4510 unpack_unreachable_expiration
= 0;
4512 if (stdin_packs
&& filter_options
.choice
)
4513 die(_("cannot use --filter with --stdin-packs"));
4515 if (stdin_packs
&& use_internal_rev_list
)
4516 die(_("cannot use internal rev list with --stdin-packs"));
4519 if (use_internal_rev_list
)
4520 die(_("cannot use internal rev list with --cruft"));
4522 die(_("cannot use --stdin-packs with --cruft"));
4526 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4528 * - to produce good pack (with bitmap index not-yet-packed objects are
4529 * packed in suboptimal order).
4531 * - to use more robust pack-generation codepath (avoiding possible
4532 * bugs in bitmap code and possible bitmap index corruption).
4534 if (!pack_to_stdout
)
4535 use_bitmap_index_default
= 0;
4537 if (use_bitmap_index
< 0)
4538 use_bitmap_index
= use_bitmap_index_default
;
4540 /* "hard" reasons not to use bitmaps; these just won't work at all */
4541 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow(the_repository
))
4542 use_bitmap_index
= 0;
4544 if (pack_to_stdout
|| !rev_list_all
)
4545 write_bitmap_index
= 0;
4547 if (use_delta_islands
)
4548 strvec_push(&rp
, "--topo-order");
4550 if (progress
&& all_progress_implied
)
4553 add_extra_kept_packs(&keep_pack_list
);
4554 if (ignore_packed_keep_on_disk
) {
4555 struct packed_git
*p
;
4556 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
4557 if (p
->pack_local
&& p
->pack_keep
)
4559 if (!p
) /* no keep-able packs found */
4560 ignore_packed_keep_on_disk
= 0;
4564 * unlike ignore_packed_keep_on_disk above, we do not
4565 * want to unset "local" based on looking at packs, as
4566 * it also covers non-local objects
4568 struct packed_git
*p
;
4569 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4570 if (!p
->pack_local
) {
4571 have_non_local_packs
= 1;
4577 trace2_region_enter("pack-objects", "enumerate-objects",
4579 prepare_packing_data(the_repository
, &to_pack
);
4581 if (progress
&& !cruft
)
4582 progress_state
= start_progress(_("Enumerating objects"), 0);
4584 /* avoids adding objects in excluded packs */
4585 ignore_packed_keep_in_core
= 1;
4586 read_packs_list_from_stdin();
4587 if (rev_list_unpacked
)
4588 add_unreachable_loose_objects();
4590 read_cruft_objects();
4591 } else if (!use_internal_rev_list
) {
4592 read_object_list_from_stdin();
4594 struct rev_info revs
;
4596 repo_init_revisions(the_repository
, &revs
, NULL
);
4597 list_objects_filter_copy(&revs
.filter
, &filter_options
);
4598 get_object_list(&revs
, rp
.nr
, rp
.v
);
4599 release_revisions(&revs
);
4601 cleanup_preferred_base();
4602 if (include_tag
&& nr_result
)
4603 refs_for_each_tag_ref(get_main_ref_store(the_repository
),
4605 stop_progress(&progress_state
);
4606 trace2_region_leave("pack-objects", "enumerate-objects",
4609 if (non_empty
&& !nr_result
)
4612 trace2_region_enter("pack-objects", "prepare-pack",
4614 prepare_pack(window
, depth
);
4615 trace2_region_leave("pack-objects", "prepare-pack",
4619 trace2_region_enter("pack-objects", "write-pack-file", the_repository
);
4620 write_excluded_by_configs();
4622 trace2_region_leave("pack-objects", "write-pack-file", the_repository
);
4626 _("Total %"PRIu32
" (delta %"PRIu32
"),"
4627 " reused %"PRIu32
" (delta %"PRIu32
"),"
4628 " pack-reused %"PRIu32
" (from %"PRIuMAX
")"),
4629 written
, written_delta
, reused
, reused_delta
,
4630 reuse_packfile_objects
,
4631 (uintmax_t)reuse_packfiles_used_nr
);
4633 trace2_data_intmax("pack-objects", the_repository
, "written", written
);
4634 trace2_data_intmax("pack-objects", the_repository
, "written/delta", written_delta
);
4635 trace2_data_intmax("pack-objects", the_repository
, "reused", reused
);
4636 trace2_data_intmax("pack-objects", the_repository
, "reused/delta", reused_delta
);
4637 trace2_data_intmax("pack-objects", the_repository
, "pack-reused", reuse_packfile_objects
);
4638 trace2_data_intmax("pack-objects", the_repository
, "packs-reused", reuse_packfiles_used_nr
);
4641 clear_packing_data(&to_pack
);
4642 list_objects_filter_release(&filter_options
);