2 #include "environment.h"
5 #include "repository.h"
15 #include "pack-revindex.h"
16 #include "csum-file.h"
17 #include "tree-walk.h"
20 #include "list-objects.h"
21 #include "list-objects-filter.h"
22 #include "list-objects-filter-options.h"
23 #include "pack-objects.h"
26 #include "streaming.h"
27 #include "thread-utils.h"
28 #include "pack-bitmap.h"
29 #include "delta-islands.h"
30 #include "reachable.h"
31 #include "oid-array.h"
35 #include "object-file.h"
36 #include "object-store-ll.h"
37 #include "replace-object.h"
42 #include "promisor-remote.h"
43 #include "pack-mtimes.h"
44 #include "parse-options.h"
47 * Objects we are going to pack are collected in the `to_pack` structure.
48 * It contains an array (dynamically expanded) of the object data, and a map
49 * that can resolve SHA1s to their position in the array.
51 static struct packing_data to_pack
;
53 static inline struct object_entry
*oe_delta(
54 const struct packing_data
*pack
,
55 const struct object_entry
*e
)
60 return &pack
->ext_bases
[e
->delta_idx
- 1];
62 return &pack
->objects
[e
->delta_idx
- 1];
65 static inline unsigned long oe_delta_size(struct packing_data
*pack
,
66 const struct object_entry
*e
)
68 if (e
->delta_size_valid
)
69 return e
->delta_size_
;
72 * pack->delta_size[] can't be NULL because oe_set_delta_size()
73 * must have been called when a new delta is saved with
75 * If oe_delta() returns NULL (i.e. default state, which means
76 * delta_size_valid is also false), then the caller must never
77 * call oe_delta_size().
79 return pack
->delta_size
[e
- pack
->objects
];
82 unsigned long oe_get_size_slow(struct packing_data
*pack
,
83 const struct object_entry
*e
);
85 static inline unsigned long oe_size(struct packing_data
*pack
,
86 const struct object_entry
*e
)
91 return oe_get_size_slow(pack
, e
);
94 static inline void oe_set_delta(struct packing_data
*pack
,
95 struct object_entry
*e
,
96 struct object_entry
*delta
)
99 e
->delta_idx
= (delta
- pack
->objects
) + 1;
104 static inline struct object_entry
*oe_delta_sibling(
105 const struct packing_data
*pack
,
106 const struct object_entry
*e
)
108 if (e
->delta_sibling_idx
)
109 return &pack
->objects
[e
->delta_sibling_idx
- 1];
113 static inline struct object_entry
*oe_delta_child(
114 const struct packing_data
*pack
,
115 const struct object_entry
*e
)
117 if (e
->delta_child_idx
)
118 return &pack
->objects
[e
->delta_child_idx
- 1];
122 static inline void oe_set_delta_child(struct packing_data
*pack
,
123 struct object_entry
*e
,
124 struct object_entry
*delta
)
127 e
->delta_child_idx
= (delta
- pack
->objects
) + 1;
129 e
->delta_child_idx
= 0;
132 static inline void oe_set_delta_sibling(struct packing_data
*pack
,
133 struct object_entry
*e
,
134 struct object_entry
*delta
)
137 e
->delta_sibling_idx
= (delta
- pack
->objects
) + 1;
139 e
->delta_sibling_idx
= 0;
142 static inline void oe_set_size(struct packing_data
*pack
,
143 struct object_entry
*e
,
146 if (size
< pack
->oe_size_limit
) {
151 if (oe_get_size_slow(pack
, e
) != size
)
152 BUG("'size' is supposed to be the object size!");
156 static inline void oe_set_delta_size(struct packing_data
*pack
,
157 struct object_entry
*e
,
160 if (size
< pack
->oe_delta_size_limit
) {
161 e
->delta_size_
= size
;
162 e
->delta_size_valid
= 1;
164 packing_data_lock(pack
);
165 if (!pack
->delta_size
)
166 ALLOC_ARRAY(pack
->delta_size
, pack
->nr_alloc
);
167 packing_data_unlock(pack
);
169 pack
->delta_size
[e
- pack
->objects
] = size
;
170 e
->delta_size_valid
= 0;
174 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
175 #define SIZE(obj) oe_size(&to_pack, obj)
176 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
177 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
178 #define DELTA(obj) oe_delta(&to_pack, obj)
179 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
180 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
181 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
182 #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
183 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
184 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
185 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
187 static const char *pack_usage
[] = {
188 N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
189 N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
193 static struct pack_idx_entry
**written_list
;
194 static uint32_t nr_result
, nr_written
, nr_seen
;
195 static struct bitmap_index
*bitmap_git
;
196 static uint32_t write_layer
;
198 static int non_empty
;
199 static int reuse_delta
= 1, reuse_object
= 1;
200 static int keep_unreachable
, unpack_unreachable
, include_tag
;
201 static timestamp_t unpack_unreachable_expiration
;
202 static int pack_loose_unreachable
;
204 static timestamp_t cruft_expiration
;
206 static int have_non_local_packs
;
207 static int incremental
;
208 static int ignore_packed_keep_on_disk
;
209 static int ignore_packed_keep_in_core
;
210 static int allow_ofs_delta
;
211 static struct pack_idx_option pack_idx_opts
;
212 static const char *base_name
;
213 static int progress
= 1;
214 static int window
= 10;
215 static unsigned long pack_size_limit
;
216 static int depth
= 50;
217 static int delta_search_threads
;
218 static int pack_to_stdout
;
221 static int num_preferred_base
;
222 static struct progress
*progress_state
;
224 static struct packed_git
*reuse_packfile
;
225 static uint32_t reuse_packfile_objects
;
226 static struct bitmap
*reuse_packfile_bitmap
;
228 static int use_bitmap_index_default
= 1;
229 static int use_bitmap_index
= -1;
230 static int allow_pack_reuse
= 1;
232 WRITE_BITMAP_FALSE
= 0,
235 } write_bitmap_index
;
236 static uint16_t write_bitmap_options
= BITMAP_OPT_HASH_CACHE
;
238 static int exclude_promisor_objects
;
240 static int use_delta_islands
;
242 static unsigned long delta_cache_size
= 0;
243 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
244 static unsigned long cache_max_small_delta_size
= 1000;
246 static unsigned long window_memory_limit
= 0;
248 static struct string_list uri_protocols
= STRING_LIST_INIT_NODUP
;
250 enum missing_action
{
251 MA_ERROR
= 0, /* fail if any missing objects are encountered */
252 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
253 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
255 static enum missing_action arg_missing_action
;
256 static show_object_fn fn_show_object
;
258 struct configured_exclusion
{
259 struct oidmap_entry e
;
263 static struct oidmap configured_exclusions
;
265 static struct oidset excluded_by_config
;
270 static uint32_t written
, written_delta
;
271 static uint32_t reused
, reused_delta
;
276 static struct commit
**indexed_commits
;
277 static unsigned int indexed_commits_nr
;
278 static unsigned int indexed_commits_alloc
;
280 static void index_commit_for_bitmap(struct commit
*commit
)
282 if (indexed_commits_nr
>= indexed_commits_alloc
) {
283 indexed_commits_alloc
= (indexed_commits_alloc
+ 32) * 2;
284 REALLOC_ARRAY(indexed_commits
, indexed_commits_alloc
);
287 indexed_commits
[indexed_commits_nr
++] = commit
;
290 static void *get_delta(struct object_entry
*entry
)
292 unsigned long size
, base_size
, delta_size
;
293 void *buf
, *base_buf
, *delta_buf
;
294 enum object_type type
;
296 buf
= repo_read_object_file(the_repository
, &entry
->idx
.oid
, &type
,
299 die(_("unable to read %s"), oid_to_hex(&entry
->idx
.oid
));
300 base_buf
= repo_read_object_file(the_repository
,
301 &DELTA(entry
)->idx
.oid
, &type
,
304 die("unable to read %s",
305 oid_to_hex(&DELTA(entry
)->idx
.oid
));
306 delta_buf
= diff_delta(base_buf
, base_size
,
307 buf
, size
, &delta_size
, 0);
309 * We successfully computed this delta once but dropped it for
310 * memory reasons. Something is very wrong if this time we
311 * recompute and create a different delta.
313 if (!delta_buf
|| delta_size
!= DELTA_SIZE(entry
))
314 BUG("delta size changed");
320 static unsigned long do_compress(void **pptr
, unsigned long size
)
324 unsigned long maxsize
;
326 git_deflate_init(&stream
, pack_compression_level
);
327 maxsize
= git_deflate_bound(&stream
, size
);
330 out
= xmalloc(maxsize
);
334 stream
.avail_in
= size
;
335 stream
.next_out
= out
;
336 stream
.avail_out
= maxsize
;
337 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
339 git_deflate_end(&stream
);
342 return stream
.total_out
;
345 static unsigned long write_large_blob_data(struct git_istream
*st
, struct hashfile
*f
,
346 const struct object_id
*oid
)
349 unsigned char ibuf
[1024 * 16];
350 unsigned char obuf
[1024 * 16];
351 unsigned long olen
= 0;
353 git_deflate_init(&stream
, pack_compression_level
);
358 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
360 die(_("unable to read %s"), oid_to_hex(oid
));
362 stream
.next_in
= ibuf
;
363 stream
.avail_in
= readlen
;
364 while ((stream
.avail_in
|| readlen
== 0) &&
365 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
366 stream
.next_out
= obuf
;
367 stream
.avail_out
= sizeof(obuf
);
368 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
369 hashwrite(f
, obuf
, stream
.next_out
- obuf
);
370 olen
+= stream
.next_out
- obuf
;
373 die(_("deflate error (%d)"), zret
);
375 if (zret
!= Z_STREAM_END
)
376 die(_("deflate error (%d)"), zret
);
380 git_deflate_end(&stream
);
385 * we are going to reuse the existing object data as is. make
386 * sure it is not corrupt.
388 static int check_pack_inflate(struct packed_git
*p
,
389 struct pack_window
**w_curs
,
392 unsigned long expect
)
395 unsigned char fakebuf
[4096], *in
;
398 memset(&stream
, 0, sizeof(stream
));
399 git_inflate_init(&stream
);
401 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
403 stream
.next_out
= fakebuf
;
404 stream
.avail_out
= sizeof(fakebuf
);
405 st
= git_inflate(&stream
, Z_FINISH
);
406 offset
+= stream
.next_in
- in
;
407 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
408 git_inflate_end(&stream
);
409 return (st
== Z_STREAM_END
&&
410 stream
.total_out
== expect
&&
411 stream
.total_in
== len
) ? 0 : -1;
414 static void copy_pack_data(struct hashfile
*f
,
415 struct packed_git
*p
,
416 struct pack_window
**w_curs
,
424 in
= use_pack(p
, w_curs
, offset
, &avail
);
426 avail
= (unsigned long)len
;
427 hashwrite(f
, in
, avail
);
433 static inline int oe_size_greater_than(struct packing_data
*pack
,
434 const struct object_entry
*lhs
,
438 return lhs
->size_
> rhs
;
439 if (rhs
< pack
->oe_size_limit
) /* rhs < 2^x <= lhs ? */
441 return oe_get_size_slow(pack
, lhs
) > rhs
;
444 /* Return 0 if we will bust the pack-size limit */
445 static unsigned long write_no_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
446 unsigned long limit
, int usable_delta
)
448 unsigned long size
, datalen
;
449 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
450 dheader
[MAX_PACK_OBJECT_HEADER
];
452 enum object_type type
;
454 struct git_istream
*st
= NULL
;
455 const unsigned hashsz
= the_hash_algo
->rawsz
;
458 if (oe_type(entry
) == OBJ_BLOB
&&
459 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
) &&
460 (st
= open_istream(the_repository
, &entry
->idx
.oid
, &type
,
461 &size
, NULL
)) != NULL
)
464 buf
= repo_read_object_file(the_repository
,
465 &entry
->idx
.oid
, &type
,
468 die(_("unable to read %s"),
469 oid_to_hex(&entry
->idx
.oid
));
472 * make sure no cached delta data remains from a
473 * previous attempt before a pack split occurred.
475 FREE_AND_NULL(entry
->delta_data
);
476 entry
->z_delta_size
= 0;
477 } else if (entry
->delta_data
) {
478 size
= DELTA_SIZE(entry
);
479 buf
= entry
->delta_data
;
480 entry
->delta_data
= NULL
;
481 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
482 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
484 buf
= get_delta(entry
);
485 size
= DELTA_SIZE(entry
);
486 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
487 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
490 if (st
) /* large blob case, just assume we don't compress well */
492 else if (entry
->z_delta_size
)
493 datalen
= entry
->z_delta_size
;
495 datalen
= do_compress(&buf
, size
);
498 * The object header is a byte of 'type' followed by zero or
499 * more bytes of length.
501 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
504 if (type
== OBJ_OFS_DELTA
) {
506 * Deltas with relative base contain an additional
507 * encoding of the relative offset for the delta
508 * base from this object's position in the pack.
510 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
511 unsigned pos
= sizeof(dheader
) - 1;
512 dheader
[pos
] = ofs
& 127;
514 dheader
[--pos
] = 128 | (--ofs
& 127);
515 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
521 hashwrite(f
, header
, hdrlen
);
522 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
523 hdrlen
+= sizeof(dheader
) - pos
;
524 } else if (type
== OBJ_REF_DELTA
) {
526 * Deltas with a base reference contain
527 * additional bytes for the base object ID.
529 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
535 hashwrite(f
, header
, hdrlen
);
536 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
539 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
545 hashwrite(f
, header
, hdrlen
);
548 datalen
= write_large_blob_data(st
, f
, &entry
->idx
.oid
);
551 hashwrite(f
, buf
, datalen
);
555 return hdrlen
+ datalen
;
558 /* Return 0 if we will bust the pack-size limit */
559 static off_t
write_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
560 unsigned long limit
, int usable_delta
)
562 struct packed_git
*p
= IN_PACK(entry
);
563 struct pack_window
*w_curs
= NULL
;
566 enum object_type type
= oe_type(entry
);
568 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
569 dheader
[MAX_PACK_OBJECT_HEADER
];
571 const unsigned hashsz
= the_hash_algo
->rawsz
;
572 unsigned long entry_size
= SIZE(entry
);
575 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
576 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
577 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
580 offset
= entry
->in_pack_offset
;
581 if (offset_to_pack_pos(p
, offset
, &pos
) < 0)
582 die(_("write_reuse_object: could not locate %s, expected at "
583 "offset %"PRIuMAX
" in pack %s"),
584 oid_to_hex(&entry
->idx
.oid
), (uintmax_t)offset
,
586 datalen
= pack_pos_to_offset(p
, pos
+ 1) - offset
;
587 if (!pack_to_stdout
&& p
->index_version
> 1 &&
588 check_pack_crc(p
, &w_curs
, offset
, datalen
,
589 pack_pos_to_index(p
, pos
))) {
590 error(_("bad packed object CRC for %s"),
591 oid_to_hex(&entry
->idx
.oid
));
593 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
596 offset
+= entry
->in_pack_header_size
;
597 datalen
-= entry
->in_pack_header_size
;
599 if (!pack_to_stdout
&& p
->index_version
== 1 &&
600 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry_size
)) {
601 error(_("corrupt packed object for %s"),
602 oid_to_hex(&entry
->idx
.oid
));
604 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
607 if (type
== OBJ_OFS_DELTA
) {
608 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
609 unsigned pos
= sizeof(dheader
) - 1;
610 dheader
[pos
] = ofs
& 127;
612 dheader
[--pos
] = 128 | (--ofs
& 127);
613 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
617 hashwrite(f
, header
, hdrlen
);
618 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
619 hdrlen
+= sizeof(dheader
) - pos
;
621 } else if (type
== OBJ_REF_DELTA
) {
622 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
626 hashwrite(f
, header
, hdrlen
);
627 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
631 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
635 hashwrite(f
, header
, hdrlen
);
637 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
640 return hdrlen
+ datalen
;
643 /* Return 0 if we will bust the pack-size limit */
644 static off_t
write_object(struct hashfile
*f
,
645 struct object_entry
*entry
,
650 int usable_delta
, to_reuse
;
655 /* apply size limit if limited packsize and not first object */
656 if (!pack_size_limit
|| !nr_written
)
658 else if (pack_size_limit
<= write_offset
)
660 * the earlier object did not fit the limit; avoid
661 * mistaking this with unlimited (i.e. limit = 0).
665 limit
= pack_size_limit
- write_offset
;
668 usable_delta
= 0; /* no delta */
669 else if (!pack_size_limit
)
670 usable_delta
= 1; /* unlimited packfile */
671 else if (DELTA(entry
)->idx
.offset
== (off_t
)-1)
672 usable_delta
= 0; /* base was written to another pack */
673 else if (DELTA(entry
)->idx
.offset
)
674 usable_delta
= 1; /* base already exists in this pack */
676 usable_delta
= 0; /* base could end up in another pack */
679 to_reuse
= 0; /* explicit */
680 else if (!IN_PACK(entry
))
681 to_reuse
= 0; /* can't reuse what we don't have */
682 else if (oe_type(entry
) == OBJ_REF_DELTA
||
683 oe_type(entry
) == OBJ_OFS_DELTA
)
684 /* check_object() decided it for us ... */
685 to_reuse
= usable_delta
;
686 /* ... but pack split may override that */
687 else if (oe_type(entry
) != entry
->in_pack_type
)
688 to_reuse
= 0; /* pack has delta which is unusable */
689 else if (DELTA(entry
))
690 to_reuse
= 0; /* we want to pack afresh */
692 to_reuse
= 1; /* we have it in-pack undeltified,
693 * and we do not need to deltify it.
697 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
699 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
707 entry
->idx
.crc32
= crc32_end(f
);
711 enum write_one_status
{
712 WRITE_ONE_SKIP
= -1, /* already written */
713 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
714 WRITE_ONE_WRITTEN
= 1, /* normal */
715 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
718 static enum write_one_status
write_one(struct hashfile
*f
,
719 struct object_entry
*e
,
726 * we set offset to 1 (which is an impossible value) to mark
727 * the fact that this object is involved in "write its base
728 * first before writing a deltified object" recursion.
730 recursing
= (e
->idx
.offset
== 1);
732 warning(_("recursive delta detected for object %s"),
733 oid_to_hex(&e
->idx
.oid
));
734 return WRITE_ONE_RECURSIVE
;
735 } else if (e
->idx
.offset
|| e
->preferred_base
) {
736 /* offset is non zero if object is written already. */
737 return WRITE_ONE_SKIP
;
740 /* if we are deltified, write out base object first. */
742 e
->idx
.offset
= 1; /* now recurse */
743 switch (write_one(f
, DELTA(e
), offset
)) {
744 case WRITE_ONE_RECURSIVE
:
745 /* we cannot depend on this one */
750 case WRITE_ONE_BREAK
:
751 e
->idx
.offset
= recursing
;
752 return WRITE_ONE_BREAK
;
756 e
->idx
.offset
= *offset
;
757 size
= write_object(f
, e
, *offset
);
759 e
->idx
.offset
= recursing
;
760 return WRITE_ONE_BREAK
;
762 written_list
[nr_written
++] = &e
->idx
;
764 /* make sure off_t is sufficiently large not to wrap */
765 if (signed_add_overflows(*offset
, size
))
766 die(_("pack too large for current definition of off_t"));
768 return WRITE_ONE_WRITTEN
;
771 static int mark_tagged(const char *path UNUSED
, const struct object_id
*oid
,
772 int flag UNUSED
, void *cb_data UNUSED
)
774 struct object_id peeled
;
775 struct object_entry
*entry
= packlist_find(&to_pack
, oid
);
779 if (!peel_iterated_oid(oid
, &peeled
)) {
780 entry
= packlist_find(&to_pack
, &peeled
);
787 static inline unsigned char oe_layer(struct packing_data
*pack
,
788 struct object_entry
*e
)
792 return pack
->layer
[e
- pack
->objects
];
795 static inline void add_to_write_order(struct object_entry
**wo
,
797 struct object_entry
*e
)
799 if (e
->filled
|| oe_layer(&to_pack
, e
) != write_layer
)
805 static void add_descendants_to_write_order(struct object_entry
**wo
,
807 struct object_entry
*e
)
809 int add_to_order
= 1;
812 struct object_entry
*s
;
813 /* add this node... */
814 add_to_write_order(wo
, endp
, e
);
815 /* all its siblings... */
816 for (s
= DELTA_SIBLING(e
); s
; s
= DELTA_SIBLING(s
)) {
817 add_to_write_order(wo
, endp
, s
);
820 /* drop down a level to add left subtree nodes if possible */
821 if (DELTA_CHILD(e
)) {
826 /* our sibling might have some children, it is next */
827 if (DELTA_SIBLING(e
)) {
828 e
= DELTA_SIBLING(e
);
831 /* go back to our parent node */
833 while (e
&& !DELTA_SIBLING(e
)) {
834 /* we're on the right side of a subtree, keep
835 * going up until we can go right again */
839 /* done- we hit our original root node */
842 /* pass it off to sibling at this level */
843 e
= DELTA_SIBLING(e
);
848 static void add_family_to_write_order(struct object_entry
**wo
,
850 struct object_entry
*e
)
852 struct object_entry
*root
;
854 for (root
= e
; DELTA(root
); root
= DELTA(root
))
856 add_descendants_to_write_order(wo
, endp
, root
);
859 static void compute_layer_order(struct object_entry
**wo
, unsigned int *wo_end
)
861 unsigned int i
, last_untagged
;
862 struct object_entry
*objects
= to_pack
.objects
;
864 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
865 if (objects
[i
].tagged
)
867 add_to_write_order(wo
, wo_end
, &objects
[i
]);
872 * Then fill all the tagged tips.
874 for (; i
< to_pack
.nr_objects
; i
++) {
875 if (objects
[i
].tagged
)
876 add_to_write_order(wo
, wo_end
, &objects
[i
]);
880 * And then all remaining commits and tags.
882 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
883 if (oe_type(&objects
[i
]) != OBJ_COMMIT
&&
884 oe_type(&objects
[i
]) != OBJ_TAG
)
886 add_to_write_order(wo
, wo_end
, &objects
[i
]);
890 * And then all the trees.
892 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
893 if (oe_type(&objects
[i
]) != OBJ_TREE
)
895 add_to_write_order(wo
, wo_end
, &objects
[i
]);
899 * Finally all the rest in really tight order
901 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
902 if (!objects
[i
].filled
&& oe_layer(&to_pack
, &objects
[i
]) == write_layer
)
903 add_family_to_write_order(wo
, wo_end
, &objects
[i
]);
907 static struct object_entry
**compute_write_order(void)
909 uint32_t max_layers
= 1;
910 unsigned int i
, wo_end
;
912 struct object_entry
**wo
;
913 struct object_entry
*objects
= to_pack
.objects
;
915 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
916 objects
[i
].tagged
= 0;
917 objects
[i
].filled
= 0;
918 SET_DELTA_CHILD(&objects
[i
], NULL
);
919 SET_DELTA_SIBLING(&objects
[i
], NULL
);
923 * Fully connect delta_child/delta_sibling network.
924 * Make sure delta_sibling is sorted in the original
927 for (i
= to_pack
.nr_objects
; i
> 0;) {
928 struct object_entry
*e
= &objects
[--i
];
931 /* Mark me as the first child */
932 e
->delta_sibling_idx
= DELTA(e
)->delta_child_idx
;
933 SET_DELTA_CHILD(DELTA(e
), e
);
937 * Mark objects that are at the tip of tags.
939 for_each_tag_ref(mark_tagged
, NULL
);
941 if (use_delta_islands
) {
942 max_layers
= compute_pack_layers(&to_pack
);
946 ALLOC_ARRAY(wo
, to_pack
.nr_objects
);
949 for (; write_layer
< max_layers
; ++write_layer
)
950 compute_layer_order(wo
, &wo_end
);
952 if (wo_end
!= to_pack
.nr_objects
)
953 die(_("ordered %u objects, expected %"PRIu32
),
954 wo_end
, to_pack
.nr_objects
);
961 * A reused set of objects. All objects in a chunk have the same
962 * relative position in the original packfile and the generated
966 static struct reused_chunk
{
967 /* The offset of the first object of this chunk in the original
970 /* The difference for "original" minus the offset of the first object of
971 * this chunk in the generated packfile. */
974 static int reused_chunks_nr
;
975 static int reused_chunks_alloc
;
977 static void record_reused_object(off_t where
, off_t offset
)
979 if (reused_chunks_nr
&& reused_chunks
[reused_chunks_nr
-1].difference
== offset
)
982 ALLOC_GROW(reused_chunks
, reused_chunks_nr
+ 1,
983 reused_chunks_alloc
);
984 reused_chunks
[reused_chunks_nr
].original
= where
;
985 reused_chunks
[reused_chunks_nr
].difference
= offset
;
990 * Binary search to find the chunk that "where" is in. Note
991 * that we're not looking for an exact match, just the first
992 * chunk that contains it (which implicitly ends at the start
995 static off_t
find_reused_offset(off_t where
)
997 int lo
= 0, hi
= reused_chunks_nr
;
999 int mi
= lo
+ ((hi
- lo
) / 2);
1000 if (where
== reused_chunks
[mi
].original
)
1001 return reused_chunks
[mi
].difference
;
1002 if (where
< reused_chunks
[mi
].original
)
1009 * The first chunk starts at zero, so we can't have gone below
1013 return reused_chunks
[lo
-1].difference
;
1016 static void write_reused_pack_one(size_t pos
, struct hashfile
*out
,
1017 struct pack_window
**w_curs
)
1019 off_t offset
, next
, cur
;
1020 enum object_type type
;
1023 offset
= pack_pos_to_offset(reuse_packfile
, pos
);
1024 next
= pack_pos_to_offset(reuse_packfile
, pos
+ 1);
1026 record_reused_object(offset
, offset
- hashfile_total(out
));
1029 type
= unpack_object_header(reuse_packfile
, w_curs
, &cur
, &size
);
1032 if (type
== OBJ_OFS_DELTA
) {
1036 unsigned char header
[MAX_PACK_OBJECT_HEADER
];
1039 base_offset
= get_delta_base(reuse_packfile
, w_curs
, &cur
, type
, offset
);
1040 assert(base_offset
!= 0);
1042 /* Convert to REF_DELTA if we must... */
1043 if (!allow_ofs_delta
) {
1045 struct object_id base_oid
;
1047 if (offset_to_pack_pos(reuse_packfile
, base_offset
, &base_pos
) < 0)
1048 die(_("expected object at offset %"PRIuMAX
" "
1050 (uintmax_t)base_offset
,
1051 reuse_packfile
->pack_name
);
1053 nth_packed_object_id(&base_oid
, reuse_packfile
,
1054 pack_pos_to_index(reuse_packfile
, base_pos
));
1056 len
= encode_in_pack_object_header(header
, sizeof(header
),
1057 OBJ_REF_DELTA
, size
);
1058 hashwrite(out
, header
, len
);
1059 hashwrite(out
, base_oid
.hash
, the_hash_algo
->rawsz
);
1060 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
1064 /* Otherwise see if we need to rewrite the offset... */
1065 fixup
= find_reused_offset(offset
) -
1066 find_reused_offset(base_offset
);
1068 unsigned char ofs_header
[10];
1069 unsigned i
, ofs_len
;
1070 off_t ofs
= offset
- base_offset
- fixup
;
1072 len
= encode_in_pack_object_header(header
, sizeof(header
),
1073 OBJ_OFS_DELTA
, size
);
1075 i
= sizeof(ofs_header
) - 1;
1076 ofs_header
[i
] = ofs
& 127;
1078 ofs_header
[--i
] = 128 | (--ofs
& 127);
1080 ofs_len
= sizeof(ofs_header
) - i
;
1082 hashwrite(out
, header
, len
);
1083 hashwrite(out
, ofs_header
+ sizeof(ofs_header
) - ofs_len
, ofs_len
);
1084 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
1088 /* ...otherwise we have no fixup, and can write it verbatim */
1091 copy_pack_data(out
, reuse_packfile
, w_curs
, offset
, next
- offset
);
1094 static size_t write_reused_pack_verbatim(struct hashfile
*out
,
1095 struct pack_window
**w_curs
)
1099 while (pos
< reuse_packfile_bitmap
->word_alloc
&&
1100 reuse_packfile_bitmap
->words
[pos
] == (eword_t
)~0)
1106 written
= (pos
* BITS_IN_EWORD
);
1107 to_write
= pack_pos_to_offset(reuse_packfile
, written
)
1108 - sizeof(struct pack_header
);
1110 /* We're recording one chunk, not one object. */
1111 record_reused_object(sizeof(struct pack_header
), 0);
1113 copy_pack_data(out
, reuse_packfile
, w_curs
,
1114 sizeof(struct pack_header
), to_write
);
1116 display_progress(progress_state
, written
);
1121 static void write_reused_pack(struct hashfile
*f
)
1125 struct pack_window
*w_curs
= NULL
;
1127 if (allow_ofs_delta
)
1128 i
= write_reused_pack_verbatim(f
, &w_curs
);
1130 for (; i
< reuse_packfile_bitmap
->word_alloc
; ++i
) {
1131 eword_t word
= reuse_packfile_bitmap
->words
[i
];
1132 size_t pos
= (i
* BITS_IN_EWORD
);
1134 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1135 if ((word
>> offset
) == 0)
1138 offset
+= ewah_bit_ctz64(word
>> offset
);
1140 * Can use bit positions directly, even for MIDX
1141 * bitmaps. See comment in try_partial_reuse()
1144 write_reused_pack_one(pos
+ offset
, f
, &w_curs
);
1145 display_progress(progress_state
, ++written
);
1149 unuse_pack(&w_curs
);
1152 static void write_excluded_by_configs(void)
1154 struct oidset_iter iter
;
1155 const struct object_id
*oid
;
1157 oidset_iter_init(&excluded_by_config
, &iter
);
1158 while ((oid
= oidset_iter_next(&iter
))) {
1159 struct configured_exclusion
*ex
=
1160 oidmap_get(&configured_exclusions
, oid
);
1163 BUG("configured exclusion wasn't configured");
1164 write_in_full(1, ex
->pack_hash_hex
, strlen(ex
->pack_hash_hex
));
1165 write_in_full(1, " ", 1);
1166 write_in_full(1, ex
->uri
, strlen(ex
->uri
));
1167 write_in_full(1, "\n", 1);
1171 static const char no_split_warning
[] = N_(
1172 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
1175 static void write_pack_file(void)
1180 uint32_t nr_remaining
= nr_result
;
1181 time_t last_mtime
= 0;
1182 struct object_entry
**write_order
;
1184 if (progress
> pack_to_stdout
)
1185 progress_state
= start_progress(_("Writing objects"), nr_result
);
1186 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
1187 write_order
= compute_write_order();
1190 unsigned char hash
[GIT_MAX_RAWSZ
];
1191 char *pack_tmp_name
= NULL
;
1194 f
= hashfd_throughput(1, "<stdout>", progress_state
);
1196 f
= create_tmp_packfile(&pack_tmp_name
);
1198 offset
= write_pack_header(f
, nr_remaining
);
1200 if (reuse_packfile
) {
1201 assert(pack_to_stdout
);
1202 write_reused_pack(f
);
1203 offset
= hashfile_total(f
);
1207 for (; i
< to_pack
.nr_objects
; i
++) {
1208 struct object_entry
*e
= write_order
[i
];
1209 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
1211 display_progress(progress_state
, written
);
1214 if (pack_to_stdout
) {
1216 * We never fsync when writing to stdout since we may
1217 * not be writing to an actual pack file. For instance,
1218 * the upload-pack code passes a pipe here. Calling
1219 * fsync on a pipe results in unnecessary
1220 * synchronization with the reader on some platforms.
1222 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_NONE
,
1223 CSUM_HASH_IN_STREAM
| CSUM_CLOSE
);
1224 } else if (nr_written
== nr_remaining
) {
1225 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
,
1226 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
1229 * If we wrote the wrong number of entries in the
1230 * header, rewrite it like in fast-import.
1233 int fd
= finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
, 0);
1234 fixup_pack_header_footer(fd
, hash
, pack_tmp_name
,
1235 nr_written
, hash
, offset
);
1237 if (write_bitmap_index
) {
1238 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1239 warning(_(no_split_warning
));
1240 write_bitmap_index
= 0;
1244 if (!pack_to_stdout
) {
1246 struct strbuf tmpname
= STRBUF_INIT
;
1247 char *idx_tmp_name
= NULL
;
1250 * Packs are runtime accessed in their mtime
1251 * order since newer packs are more likely to contain
1252 * younger objects. So if we are creating multiple
1253 * packs then we should modify the mtime of later ones
1254 * to preserve this property.
1256 if (stat(pack_tmp_name
, &st
) < 0) {
1257 warning_errno(_("failed to stat %s"), pack_tmp_name
);
1258 } else if (!last_mtime
) {
1259 last_mtime
= st
.st_mtime
;
1262 utb
.actime
= st
.st_atime
;
1263 utb
.modtime
= --last_mtime
;
1264 if (utime(pack_tmp_name
, &utb
) < 0)
1265 warning_errno(_("failed utime() on %s"), pack_tmp_name
);
1268 strbuf_addf(&tmpname
, "%s-%s.", base_name
,
1271 if (write_bitmap_index
) {
1272 bitmap_writer_set_checksum(hash
);
1273 bitmap_writer_build_type_index(
1274 &to_pack
, written_list
, nr_written
);
1278 pack_idx_opts
.flags
|= WRITE_MTIMES
;
1280 stage_tmp_packfiles(&tmpname
, pack_tmp_name
,
1281 written_list
, nr_written
,
1282 &to_pack
, &pack_idx_opts
, hash
,
1285 if (write_bitmap_index
) {
1286 size_t tmpname_len
= tmpname
.len
;
1288 strbuf_addstr(&tmpname
, "bitmap");
1289 stop_progress(&progress_state
);
1291 bitmap_writer_show_progress(progress
);
1292 bitmap_writer_select_commits(indexed_commits
, indexed_commits_nr
, -1);
1293 if (bitmap_writer_build(&to_pack
) < 0)
1294 die(_("failed to write bitmap index"));
1295 bitmap_writer_finish(written_list
, nr_written
,
1296 tmpname
.buf
, write_bitmap_options
);
1297 write_bitmap_index
= 0;
1298 strbuf_setlen(&tmpname
, tmpname_len
);
1301 rename_tmp_packfile_idx(&tmpname
, &idx_tmp_name
);
1304 strbuf_release(&tmpname
);
1305 free(pack_tmp_name
);
1306 puts(hash_to_hex(hash
));
1309 /* mark written objects as written to previous pack */
1310 for (j
= 0; j
< nr_written
; j
++) {
1311 written_list
[j
]->offset
= (off_t
)-1;
1313 nr_remaining
-= nr_written
;
1314 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
1318 stop_progress(&progress_state
);
1319 if (written
!= nr_result
)
1320 die(_("wrote %"PRIu32
" objects while expecting %"PRIu32
),
1321 written
, nr_result
);
1322 trace2_data_intmax("pack-objects", the_repository
,
1323 "write_pack_file/wrote", nr_result
);
1326 static int no_try_delta(const char *path
)
1328 static struct attr_check
*check
;
1331 check
= attr_check_initl("delta", NULL
);
1332 git_check_attr(the_repository
->index
, path
, check
);
1333 if (ATTR_FALSE(check
->items
[0].value
))
1339 * When adding an object, check whether we have already added it
1340 * to our packing list. If so, we can skip. However, if we are
1341 * being asked to excludei t, but the previous mention was to include
1342 * it, make sure to adjust its flags and tweak our numbers accordingly.
1344 * As an optimization, we pass out the index position where we would have
1345 * found the item, since that saves us from having to look it up again a
1346 * few lines later when we want to add the new entry.
1348 static int have_duplicate_entry(const struct object_id
*oid
,
1351 struct object_entry
*entry
;
1353 if (reuse_packfile_bitmap
&&
1354 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
))
1357 entry
= packlist_find(&to_pack
, oid
);
1362 if (!entry
->preferred_base
)
1364 entry
->preferred_base
= 1;
1370 static int want_found_object(const struct object_id
*oid
, int exclude
,
1371 struct packed_git
*p
)
1378 if (!is_pack_valid(p
))
1382 * When asked to do --local (do not include an object that appears in a
1383 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1384 * an object that appears in a pack marked with .keep), finding a pack
1385 * that matches the criteria is sufficient for us to decide to omit it.
1386 * However, even if this pack does not satisfy the criteria, we need to
1387 * make sure no copy of this object appears in _any_ pack that makes us
1388 * to omit the object, so we need to check all the packs.
1390 * We can however first check whether these options can possibly matter;
1391 * if they do not matter we know we want the object in generated pack.
1392 * Otherwise, we signal "-1" at the end to tell the caller that we do
1393 * not know either way, and it needs to check more packs.
1397 * Objects in packs borrowed from elsewhere are discarded regardless of
1398 * if they appear in other packs that weren't borrowed.
1400 if (local
&& !p
->pack_local
)
1404 * Then handle .keep first, as we have a fast(er) path there.
1406 if (ignore_packed_keep_on_disk
|| ignore_packed_keep_in_core
) {
1408 * Set the flags for the kept-pack cache to be the ones we want
1411 * That is, if we are ignoring objects in on-disk keep packs,
1412 * then we want to search through the on-disk keep and ignore
1416 if (ignore_packed_keep_on_disk
)
1417 flags
|= ON_DISK_KEEP_PACKS
;
1418 if (ignore_packed_keep_in_core
)
1419 flags
|= IN_CORE_KEEP_PACKS
;
1421 if (ignore_packed_keep_on_disk
&& p
->pack_keep
)
1423 if (ignore_packed_keep_in_core
&& p
->pack_keep_in_core
)
1425 if (has_object_kept_pack(oid
, flags
))
1430 * At this point we know definitively that either we don't care about
1431 * keep-packs, or the object is not in one. Keep checking other
1434 if (!local
|| !have_non_local_packs
)
1437 /* we don't know yet; keep looking for more packs */
1441 static int want_object_in_pack_one(struct packed_git
*p
,
1442 const struct object_id
*oid
,
1444 struct packed_git
**found_pack
,
1445 off_t
*found_offset
)
1449 if (p
== *found_pack
)
1450 offset
= *found_offset
;
1452 offset
= find_pack_entry_one(oid
->hash
, p
);
1456 if (!is_pack_valid(p
))
1458 *found_offset
= offset
;
1461 return want_found_object(oid
, exclude
, p
);
1467 * Check whether we want the object in the pack (e.g., we do not want
1468 * objects found in non-local stores if the "--local" option was used).
1470 * If the caller already knows an existing pack it wants to take the object
1471 * from, that is passed in *found_pack and *found_offset; otherwise this
1472 * function finds if there is any pack that has the object and returns the pack
1473 * and its offset in these variables.
1475 static int want_object_in_pack(const struct object_id
*oid
,
1477 struct packed_git
**found_pack
,
1478 off_t
*found_offset
)
1481 struct list_head
*pos
;
1482 struct multi_pack_index
*m
;
1484 if (!exclude
&& local
&& has_loose_object_nonlocal(oid
))
1488 * If we already know the pack object lives in, start checks from that
1489 * pack - in the usual case when neither --local was given nor .keep files
1490 * are present we will determine the answer right now.
1493 want
= want_found_object(oid
, exclude
, *found_pack
);
1501 for (m
= get_multi_pack_index(the_repository
); m
; m
= m
->next
) {
1502 struct pack_entry e
;
1503 if (fill_midx_entry(the_repository
, oid
, &e
, m
)) {
1504 want
= want_object_in_pack_one(e
.p
, oid
, exclude
, found_pack
, found_offset
);
1510 list_for_each(pos
, get_packed_git_mru(the_repository
)) {
1511 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1512 want
= want_object_in_pack_one(p
, oid
, exclude
, found_pack
, found_offset
);
1513 if (!exclude
&& want
> 0)
1515 get_packed_git_mru(the_repository
));
1520 if (uri_protocols
.nr
) {
1521 struct configured_exclusion
*ex
=
1522 oidmap_get(&configured_exclusions
, oid
);
1527 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1528 if (skip_prefix(ex
->uri
,
1529 uri_protocols
.items
[i
].string
,
1532 oidset_insert(&excluded_by_config
, oid
);
1542 static struct object_entry
*create_object_entry(const struct object_id
*oid
,
1543 enum object_type type
,
1547 struct packed_git
*found_pack
,
1550 struct object_entry
*entry
;
1552 entry
= packlist_alloc(&to_pack
, oid
);
1554 oe_set_type(entry
, type
);
1556 entry
->preferred_base
= 1;
1560 oe_set_in_pack(&to_pack
, entry
, found_pack
);
1561 entry
->in_pack_offset
= found_offset
;
1564 entry
->no_try_delta
= no_try_delta
;
1569 static const char no_closure_warning
[] = N_(
1570 "disabling bitmap writing, as some objects are not being packed"
1573 static int add_object_entry(const struct object_id
*oid
, enum object_type type
,
1574 const char *name
, int exclude
)
1576 struct packed_git
*found_pack
= NULL
;
1577 off_t found_offset
= 0;
1579 display_progress(progress_state
, ++nr_seen
);
1581 if (have_duplicate_entry(oid
, exclude
))
1584 if (!want_object_in_pack(oid
, exclude
, &found_pack
, &found_offset
)) {
1585 /* The pack is missing an object, so it will not have closure */
1586 if (write_bitmap_index
) {
1587 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1588 warning(_(no_closure_warning
));
1589 write_bitmap_index
= 0;
1594 create_object_entry(oid
, type
, pack_name_hash(name
),
1595 exclude
, name
&& no_try_delta(name
),
1596 found_pack
, found_offset
);
1600 static int add_object_entry_from_bitmap(const struct object_id
*oid
,
1601 enum object_type type
,
1602 int flags UNUSED
, uint32_t name_hash
,
1603 struct packed_git
*pack
, off_t offset
)
1605 display_progress(progress_state
, ++nr_seen
);
1607 if (have_duplicate_entry(oid
, 0))
1610 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
1613 create_object_entry(oid
, type
, name_hash
, 0, 0, pack
, offset
);
1617 struct pbase_tree_cache
{
1618 struct object_id oid
;
1622 unsigned long tree_size
;
1625 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1626 static int pbase_tree_cache_ix(const struct object_id
*oid
)
1628 return oid
->hash
[0] % ARRAY_SIZE(pbase_tree_cache
);
1630 static int pbase_tree_cache_ix_incr(int ix
)
1632 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1635 static struct pbase_tree
{
1636 struct pbase_tree
*next
;
1637 /* This is a phony "cache" entry; we are not
1638 * going to evict it or find it through _get()
1639 * mechanism -- this is for the toplevel node that
1640 * would almost always change with any commit.
1642 struct pbase_tree_cache pcache
;
1645 static struct pbase_tree_cache
*pbase_tree_get(const struct object_id
*oid
)
1647 struct pbase_tree_cache
*ent
, *nent
;
1650 enum object_type type
;
1652 int my_ix
= pbase_tree_cache_ix(oid
);
1653 int available_ix
= -1;
1655 /* pbase-tree-cache acts as a limited hashtable.
1656 * your object will be found at your index or within a few
1657 * slots after that slot if it is cached.
1659 for (neigh
= 0; neigh
< 8; neigh
++) {
1660 ent
= pbase_tree_cache
[my_ix
];
1661 if (ent
&& oideq(&ent
->oid
, oid
)) {
1665 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1666 ((0 <= available_ix
) &&
1667 (!ent
&& pbase_tree_cache
[available_ix
])))
1668 available_ix
= my_ix
;
1671 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1674 /* Did not find one. Either we got a bogus request or
1675 * we need to read and perhaps cache.
1677 data
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
1680 if (type
!= OBJ_TREE
) {
1685 /* We need to either cache or return a throwaway copy */
1687 if (available_ix
< 0)
1690 ent
= pbase_tree_cache
[available_ix
];
1691 my_ix
= available_ix
;
1695 nent
= xmalloc(sizeof(*nent
));
1696 nent
->temporary
= (available_ix
< 0);
1699 /* evict and reuse */
1700 free(ent
->tree_data
);
1703 oidcpy(&nent
->oid
, oid
);
1704 nent
->tree_data
= data
;
1705 nent
->tree_size
= size
;
1707 if (!nent
->temporary
)
1708 pbase_tree_cache
[my_ix
] = nent
;
1712 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1714 if (!cache
->temporary
) {
1718 free(cache
->tree_data
);
1722 static size_t name_cmp_len(const char *name
)
1724 return strcspn(name
, "\n/");
1727 static void add_pbase_object(struct tree_desc
*tree
,
1730 const char *fullname
)
1732 struct name_entry entry
;
1735 while (tree_entry(tree
,&entry
)) {
1736 if (S_ISGITLINK(entry
.mode
))
1738 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1739 memcmp(name
, entry
.path
, cmplen
);
1744 if (name
[cmplen
] != '/') {
1745 add_object_entry(&entry
.oid
,
1746 object_type(entry
.mode
),
1750 if (S_ISDIR(entry
.mode
)) {
1751 struct tree_desc sub
;
1752 struct pbase_tree_cache
*tree
;
1753 const char *down
= name
+cmplen
+1;
1754 size_t downlen
= name_cmp_len(down
);
1756 tree
= pbase_tree_get(&entry
.oid
);
1759 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1761 add_pbase_object(&sub
, down
, downlen
, fullname
);
1762 pbase_tree_put(tree
);
1767 static unsigned *done_pbase_paths
;
1768 static int done_pbase_paths_num
;
1769 static int done_pbase_paths_alloc
;
1770 static int done_pbase_path_pos(unsigned hash
)
1773 int hi
= done_pbase_paths_num
;
1775 int mi
= lo
+ (hi
- lo
) / 2;
1776 if (done_pbase_paths
[mi
] == hash
)
1778 if (done_pbase_paths
[mi
] < hash
)
1786 static int check_pbase_path(unsigned hash
)
1788 int pos
= done_pbase_path_pos(hash
);
1792 ALLOC_GROW(done_pbase_paths
,
1793 done_pbase_paths_num
+ 1,
1794 done_pbase_paths_alloc
);
1795 done_pbase_paths_num
++;
1796 if (pos
< done_pbase_paths_num
)
1797 MOVE_ARRAY(done_pbase_paths
+ pos
+ 1, done_pbase_paths
+ pos
,
1798 done_pbase_paths_num
- pos
- 1);
1799 done_pbase_paths
[pos
] = hash
;
1803 static void add_preferred_base_object(const char *name
)
1805 struct pbase_tree
*it
;
1807 unsigned hash
= pack_name_hash(name
);
1809 if (!num_preferred_base
|| check_pbase_path(hash
))
1812 cmplen
= name_cmp_len(name
);
1813 for (it
= pbase_tree
; it
; it
= it
->next
) {
1815 add_object_entry(&it
->pcache
.oid
, OBJ_TREE
, NULL
, 1);
1818 struct tree_desc tree
;
1819 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1820 add_pbase_object(&tree
, name
, cmplen
, name
);
1825 static void add_preferred_base(struct object_id
*oid
)
1827 struct pbase_tree
*it
;
1830 struct object_id tree_oid
;
1832 if (window
<= num_preferred_base
++)
1835 data
= read_object_with_reference(the_repository
, oid
,
1836 OBJ_TREE
, &size
, &tree_oid
);
1840 for (it
= pbase_tree
; it
; it
= it
->next
) {
1841 if (oideq(&it
->pcache
.oid
, &tree_oid
)) {
1847 CALLOC_ARRAY(it
, 1);
1848 it
->next
= pbase_tree
;
1851 oidcpy(&it
->pcache
.oid
, &tree_oid
);
1852 it
->pcache
.tree_data
= data
;
1853 it
->pcache
.tree_size
= size
;
1856 static void cleanup_preferred_base(void)
1858 struct pbase_tree
*it
;
1864 struct pbase_tree
*tmp
= it
;
1866 free(tmp
->pcache
.tree_data
);
1870 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1871 if (!pbase_tree_cache
[i
])
1873 free(pbase_tree_cache
[i
]->tree_data
);
1874 FREE_AND_NULL(pbase_tree_cache
[i
]);
1877 FREE_AND_NULL(done_pbase_paths
);
1878 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1882 * Return 1 iff the object specified by "delta" can be sent
1883 * literally as a delta against the base in "base_sha1". If
1884 * so, then *base_out will point to the entry in our packing
1885 * list, or NULL if we must use the external-base list.
1887 * Depth value does not matter - find_deltas() will
1888 * never consider reused delta as the base object to
1889 * deltify other objects against, in order to avoid
1892 static int can_reuse_delta(const struct object_id
*base_oid
,
1893 struct object_entry
*delta
,
1894 struct object_entry
**base_out
)
1896 struct object_entry
*base
;
1899 * First see if we're already sending the base (or it's explicitly in
1900 * our "excluded" list).
1902 base
= packlist_find(&to_pack
, base_oid
);
1904 if (!in_same_island(&delta
->idx
.oid
, &base
->idx
.oid
))
1911 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1912 * even if it was buried too deep in history to make it into the
1915 if (thin
&& bitmap_has_oid_in_uninteresting(bitmap_git
, base_oid
)) {
1916 if (use_delta_islands
) {
1917 if (!in_same_island(&delta
->idx
.oid
, base_oid
))
1927 static void prefetch_to_pack(uint32_t object_index_start
) {
1928 struct oid_array to_fetch
= OID_ARRAY_INIT
;
1931 for (i
= object_index_start
; i
< to_pack
.nr_objects
; i
++) {
1932 struct object_entry
*entry
= to_pack
.objects
+ i
;
1934 if (!oid_object_info_extended(the_repository
,
1937 OBJECT_INFO_FOR_PREFETCH
))
1939 oid_array_append(&to_fetch
, &entry
->idx
.oid
);
1941 promisor_remote_get_direct(the_repository
,
1942 to_fetch
.oid
, to_fetch
.nr
);
1943 oid_array_clear(&to_fetch
);
1946 static void check_object(struct object_entry
*entry
, uint32_t object_index
)
1948 unsigned long canonical_size
;
1949 enum object_type type
;
1950 struct object_info oi
= {.typep
= &type
, .sizep
= &canonical_size
};
1952 if (IN_PACK(entry
)) {
1953 struct packed_git
*p
= IN_PACK(entry
);
1954 struct pack_window
*w_curs
= NULL
;
1956 struct object_id base_ref
;
1957 struct object_entry
*base_entry
;
1958 unsigned long used
, used_0
;
1959 unsigned long avail
;
1961 unsigned char *buf
, c
;
1962 enum object_type type
;
1963 unsigned long in_pack_size
;
1965 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1968 * We want in_pack_type even if we do not reuse delta
1969 * since non-delta representations could still be reused.
1971 used
= unpack_object_header_buffer(buf
, avail
,
1978 BUG("invalid type %d", type
);
1979 entry
->in_pack_type
= type
;
1982 * Determine if this is a delta and if so whether we can
1983 * reuse it or not. Otherwise let's find out as cheaply as
1984 * possible what the actual type and size for this object is.
1986 switch (entry
->in_pack_type
) {
1988 /* Not a delta hence we've already got all we need. */
1989 oe_set_type(entry
, entry
->in_pack_type
);
1990 SET_SIZE(entry
, in_pack_size
);
1991 entry
->in_pack_header_size
= used
;
1992 if (oe_type(entry
) < OBJ_COMMIT
|| oe_type(entry
) > OBJ_BLOB
)
1994 unuse_pack(&w_curs
);
1997 if (reuse_delta
&& !entry
->preferred_base
) {
1999 use_pack(p
, &w_curs
,
2000 entry
->in_pack_offset
+ used
,
2004 entry
->in_pack_header_size
= used
+ the_hash_algo
->rawsz
;
2007 buf
= use_pack(p
, &w_curs
,
2008 entry
->in_pack_offset
+ used
, NULL
);
2014 if (!ofs
|| MSB(ofs
, 7)) {
2015 error(_("delta base offset overflow in pack for %s"),
2016 oid_to_hex(&entry
->idx
.oid
));
2020 ofs
= (ofs
<< 7) + (c
& 127);
2022 ofs
= entry
->in_pack_offset
- ofs
;
2023 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
2024 error(_("delta base offset out of bound for %s"),
2025 oid_to_hex(&entry
->idx
.oid
));
2028 if (reuse_delta
&& !entry
->preferred_base
) {
2030 if (offset_to_pack_pos(p
, ofs
, &pos
) < 0)
2032 if (!nth_packed_object_id(&base_ref
, p
,
2033 pack_pos_to_index(p
, pos
)))
2036 entry
->in_pack_header_size
= used
+ used_0
;
2041 can_reuse_delta(&base_ref
, entry
, &base_entry
)) {
2042 oe_set_type(entry
, entry
->in_pack_type
);
2043 SET_SIZE(entry
, in_pack_size
); /* delta size */
2044 SET_DELTA_SIZE(entry
, in_pack_size
);
2047 SET_DELTA(entry
, base_entry
);
2048 entry
->delta_sibling_idx
= base_entry
->delta_child_idx
;
2049 SET_DELTA_CHILD(base_entry
, entry
);
2051 SET_DELTA_EXT(entry
, &base_ref
);
2054 unuse_pack(&w_curs
);
2058 if (oe_type(entry
)) {
2062 * This must be a delta and we already know what the
2063 * final object type is. Let's extract the actual
2064 * object size from the delta header.
2066 delta_pos
= entry
->in_pack_offset
+ entry
->in_pack_header_size
;
2067 canonical_size
= get_size_from_delta(p
, &w_curs
, delta_pos
);
2068 if (canonical_size
== 0)
2070 SET_SIZE(entry
, canonical_size
);
2071 unuse_pack(&w_curs
);
2076 * No choice but to fall back to the recursive delta walk
2077 * with oid_object_info() to find about the object type
2081 unuse_pack(&w_curs
);
2084 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2085 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0) {
2086 if (repo_has_promisor_remote(the_repository
)) {
2087 prefetch_to_pack(object_index
);
2088 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2089 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0)
2095 oe_set_type(entry
, type
);
2096 if (entry
->type_valid
) {
2097 SET_SIZE(entry
, canonical_size
);
2100 * Bad object type is checked in prepare_pack(). This is
2101 * to permit a missing preferred base object to be ignored
2102 * as a preferred base. Doing so can result in a larger
2103 * pack file, but the transfer will still take place.
2108 static int pack_offset_sort(const void *_a
, const void *_b
)
2110 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2111 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2112 const struct packed_git
*a_in_pack
= IN_PACK(a
);
2113 const struct packed_git
*b_in_pack
= IN_PACK(b
);
2115 /* avoid filesystem trashing with loose objects */
2116 if (!a_in_pack
&& !b_in_pack
)
2117 return oidcmp(&a
->idx
.oid
, &b
->idx
.oid
);
2119 if (a_in_pack
< b_in_pack
)
2121 if (a_in_pack
> b_in_pack
)
2123 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
2124 (a
->in_pack_offset
> b
->in_pack_offset
);
2128 * Drop an on-disk delta we were planning to reuse. Naively, this would
2129 * just involve blanking out the "delta" field, but we have to deal
2130 * with some extra book-keeping:
2132 * 1. Removing ourselves from the delta_sibling linked list.
2134 * 2. Updating our size/type to the non-delta representation. These were
2135 * either not recorded initially (size) or overwritten with the delta type
2136 * (type) when check_object() decided to reuse the delta.
2138 * 3. Resetting our delta depth, as we are now a base object.
2140 static void drop_reused_delta(struct object_entry
*entry
)
2142 unsigned *idx
= &to_pack
.objects
[entry
->delta_idx
- 1].delta_child_idx
;
2143 struct object_info oi
= OBJECT_INFO_INIT
;
2144 enum object_type type
;
2148 struct object_entry
*oe
= &to_pack
.objects
[*idx
- 1];
2151 *idx
= oe
->delta_sibling_idx
;
2153 idx
= &oe
->delta_sibling_idx
;
2155 SET_DELTA(entry
, NULL
);
2160 if (packed_object_info(the_repository
, IN_PACK(entry
), entry
->in_pack_offset
, &oi
) < 0) {
2162 * We failed to get the info from this pack for some reason;
2163 * fall back to oid_object_info, which may find another copy.
2164 * And if that fails, the error will be recorded in oe_type(entry)
2165 * and dealt with in prepare_pack().
2168 oid_object_info(the_repository
, &entry
->idx
.oid
, &size
));
2170 oe_set_type(entry
, type
);
2172 SET_SIZE(entry
, size
);
2176 * Follow the chain of deltas from this entry onward, throwing away any links
2177 * that cause us to hit a cycle (as determined by the DFS state flags in
2180 * We also detect too-long reused chains that would violate our --depth
2183 static void break_delta_chains(struct object_entry
*entry
)
2186 * The actual depth of each object we will write is stored as an int,
2187 * as it cannot exceed our int "depth" limit. But before we break
2188 * changes based no that limit, we may potentially go as deep as the
2189 * number of objects, which is elsewhere bounded to a uint32_t.
2191 uint32_t total_depth
;
2192 struct object_entry
*cur
, *next
;
2194 for (cur
= entry
, total_depth
= 0;
2196 cur
= DELTA(cur
), total_depth
++) {
2197 if (cur
->dfs_state
== DFS_DONE
) {
2199 * We've already seen this object and know it isn't
2200 * part of a cycle. We do need to append its depth
2203 total_depth
+= cur
->depth
;
2208 * We break cycles before looping, so an ACTIVE state (or any
2209 * other cruft which made its way into the state variable)
2212 if (cur
->dfs_state
!= DFS_NONE
)
2213 BUG("confusing delta dfs state in first pass: %d",
2217 * Now we know this is the first time we've seen the object. If
2218 * it's not a delta, we're done traversing, but we'll mark it
2219 * done to save time on future traversals.
2222 cur
->dfs_state
= DFS_DONE
;
2227 * Mark ourselves as active and see if the next step causes
2228 * us to cycle to another active object. It's important to do
2229 * this _before_ we loop, because it impacts where we make the
2230 * cut, and thus how our total_depth counter works.
2231 * E.g., We may see a partial loop like:
2233 * A -> B -> C -> D -> B
2235 * Cutting B->C breaks the cycle. But now the depth of A is
2236 * only 1, and our total_depth counter is at 3. The size of the
2237 * error is always one less than the size of the cycle we
2238 * broke. Commits C and D were "lost" from A's chain.
2240 * If we instead cut D->B, then the depth of A is correct at 3.
2241 * We keep all commits in the chain that we examined.
2243 cur
->dfs_state
= DFS_ACTIVE
;
2244 if (DELTA(cur
)->dfs_state
== DFS_ACTIVE
) {
2245 drop_reused_delta(cur
);
2246 cur
->dfs_state
= DFS_DONE
;
2252 * And now that we've gone all the way to the bottom of the chain, we
2253 * need to clear the active flags and set the depth fields as
2254 * appropriate. Unlike the loop above, which can quit when it drops a
2255 * delta, we need to keep going to look for more depth cuts. So we need
2256 * an extra "next" pointer to keep going after we reset cur->delta.
2258 for (cur
= entry
; cur
; cur
= next
) {
2262 * We should have a chain of zero or more ACTIVE states down to
2263 * a final DONE. We can quit after the DONE, because either it
2264 * has no bases, or we've already handled them in a previous
2267 if (cur
->dfs_state
== DFS_DONE
)
2269 else if (cur
->dfs_state
!= DFS_ACTIVE
)
2270 BUG("confusing delta dfs state in second pass: %d",
2274 * If the total_depth is more than depth, then we need to snip
2275 * the chain into two or more smaller chains that don't exceed
2276 * the maximum depth. Most of the resulting chains will contain
2277 * (depth + 1) entries (i.e., depth deltas plus one base), and
2278 * the last chain (i.e., the one containing entry) will contain
2279 * whatever entries are left over, namely
2280 * (total_depth % (depth + 1)) of them.
2282 * Since we are iterating towards decreasing depth, we need to
2283 * decrement total_depth as we go, and we need to write to the
2284 * entry what its final depth will be after all of the
2285 * snipping. Since we're snipping into chains of length (depth
2286 * + 1) entries, the final depth of an entry will be its
2287 * original depth modulo (depth + 1). Any time we encounter an
2288 * entry whose final depth is supposed to be zero, we snip it
2289 * from its delta base, thereby making it so.
2291 cur
->depth
= (total_depth
--) % (depth
+ 1);
2293 drop_reused_delta(cur
);
2295 cur
->dfs_state
= DFS_DONE
;
2299 static void get_object_details(void)
2302 struct object_entry
**sorted_by_offset
;
2305 progress_state
= start_progress(_("Counting objects"),
2306 to_pack
.nr_objects
);
2308 CALLOC_ARRAY(sorted_by_offset
, to_pack
.nr_objects
);
2309 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2310 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
2311 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
2313 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2314 struct object_entry
*entry
= sorted_by_offset
[i
];
2315 check_object(entry
, i
);
2316 if (entry
->type_valid
&&
2317 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
))
2318 entry
->no_try_delta
= 1;
2319 display_progress(progress_state
, i
+ 1);
2321 stop_progress(&progress_state
);
2324 * This must happen in a second pass, since we rely on the delta
2325 * information for the whole list being completed.
2327 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2328 break_delta_chains(&to_pack
.objects
[i
]);
2330 free(sorted_by_offset
);
2334 * We search for deltas in a list sorted by type, by filename hash, and then
2335 * by size, so that we see progressively smaller and smaller files.
2336 * That's because we prefer deltas to be from the bigger file
2337 * to the smaller -- deletes are potentially cheaper, but perhaps
2338 * more importantly, the bigger file is likely the more recent
2339 * one. The deepest deltas are therefore the oldest objects which are
2340 * less susceptible to be accessed often.
2342 static int type_size_sort(const void *_a
, const void *_b
)
2344 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2345 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2346 const enum object_type a_type
= oe_type(a
);
2347 const enum object_type b_type
= oe_type(b
);
2348 const unsigned long a_size
= SIZE(a
);
2349 const unsigned long b_size
= SIZE(b
);
2351 if (a_type
> b_type
)
2353 if (a_type
< b_type
)
2355 if (a
->hash
> b
->hash
)
2357 if (a
->hash
< b
->hash
)
2359 if (a
->preferred_base
> b
->preferred_base
)
2361 if (a
->preferred_base
< b
->preferred_base
)
2363 if (use_delta_islands
) {
2364 const int island_cmp
= island_delta_cmp(&a
->idx
.oid
, &b
->idx
.oid
);
2368 if (a_size
> b_size
)
2370 if (a_size
< b_size
)
2372 return a
< b
? -1 : (a
> b
); /* newest first */
2376 struct object_entry
*entry
;
2378 struct delta_index
*index
;
2382 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
2383 unsigned long delta_size
)
2385 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
2388 if (delta_size
< cache_max_small_delta_size
)
2391 /* cache delta, if objects are large enough compared to delta size */
2392 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
2398 /* Protect delta_cache_size */
2399 static pthread_mutex_t cache_mutex
;
2400 #define cache_lock() pthread_mutex_lock(&cache_mutex)
2401 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2404 * Protect object list partitioning (e.g. struct thread_param) and
2407 static pthread_mutex_t progress_mutex
;
2408 #define progress_lock() pthread_mutex_lock(&progress_mutex)
2409 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2412 * Access to struct object_entry is unprotected since each thread owns
2413 * a portion of the main object list. Just don't access object entries
2414 * ahead in the list because they can be stolen and would need
2415 * progress_mutex for protection.
2418 static inline int oe_size_less_than(struct packing_data
*pack
,
2419 const struct object_entry
*lhs
,
2422 if (lhs
->size_valid
)
2423 return lhs
->size_
< rhs
;
2424 if (rhs
< pack
->oe_size_limit
) /* rhs < 2^x <= lhs ? */
2426 return oe_get_size_slow(pack
, lhs
) < rhs
;
2429 static inline void oe_set_tree_depth(struct packing_data
*pack
,
2430 struct object_entry
*e
,
2431 unsigned int tree_depth
)
2433 if (!pack
->tree_depth
)
2434 CALLOC_ARRAY(pack
->tree_depth
, pack
->nr_alloc
);
2435 pack
->tree_depth
[e
- pack
->objects
] = tree_depth
;
2439 * Return the size of the object without doing any delta
2440 * reconstruction (so non-deltas are true object sizes, but deltas
2441 * return the size of the delta data).
2443 unsigned long oe_get_size_slow(struct packing_data
*pack
,
2444 const struct object_entry
*e
)
2446 struct packed_git
*p
;
2447 struct pack_window
*w_curs
;
2449 enum object_type type
;
2450 unsigned long used
, avail
, size
;
2452 if (e
->type_
!= OBJ_OFS_DELTA
&& e
->type_
!= OBJ_REF_DELTA
) {
2453 packing_data_lock(&to_pack
);
2454 if (oid_object_info(the_repository
, &e
->idx
.oid
, &size
) < 0)
2455 die(_("unable to get size of %s"),
2456 oid_to_hex(&e
->idx
.oid
));
2457 packing_data_unlock(&to_pack
);
2461 p
= oe_in_pack(pack
, e
);
2463 BUG("when e->type is a delta, it must belong to a pack");
2465 packing_data_lock(&to_pack
);
2467 buf
= use_pack(p
, &w_curs
, e
->in_pack_offset
, &avail
);
2468 used
= unpack_object_header_buffer(buf
, avail
, &type
, &size
);
2470 die(_("unable to parse object header of %s"),
2471 oid_to_hex(&e
->idx
.oid
));
2473 unuse_pack(&w_curs
);
2474 packing_data_unlock(&to_pack
);
2478 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
2479 unsigned max_depth
, unsigned long *mem_usage
)
2481 struct object_entry
*trg_entry
= trg
->entry
;
2482 struct object_entry
*src_entry
= src
->entry
;
2483 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
2485 enum object_type type
;
2488 /* Don't bother doing diffs between different types */
2489 if (oe_type(trg_entry
) != oe_type(src_entry
))
2493 * We do not bother to try a delta that we discarded on an
2494 * earlier try, but only when reusing delta data. Note that
2495 * src_entry that is marked as the preferred_base should always
2496 * be considered, as even if we produce a suboptimal delta against
2497 * it, we will still save the transfer cost, as we already know
2498 * the other side has it and we won't send src_entry at all.
2500 if (reuse_delta
&& IN_PACK(trg_entry
) &&
2501 IN_PACK(trg_entry
) == IN_PACK(src_entry
) &&
2502 !src_entry
->preferred_base
&&
2503 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
2504 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
2507 /* Let's not bust the allowed depth. */
2508 if (src
->depth
>= max_depth
)
2511 /* Now some size filtering heuristics. */
2512 trg_size
= SIZE(trg_entry
);
2513 if (!DELTA(trg_entry
)) {
2514 max_size
= trg_size
/2 - the_hash_algo
->rawsz
;
2517 max_size
= DELTA_SIZE(trg_entry
);
2518 ref_depth
= trg
->depth
;
2520 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
2521 (max_depth
- ref_depth
+ 1);
2524 src_size
= SIZE(src_entry
);
2525 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
2526 if (sizediff
>= max_size
)
2528 if (trg_size
< src_size
/ 32)
2531 if (!in_same_island(&trg
->entry
->idx
.oid
, &src
->entry
->idx
.oid
))
2534 /* Load data if not already done */
2536 packing_data_lock(&to_pack
);
2537 trg
->data
= repo_read_object_file(the_repository
,
2538 &trg_entry
->idx
.oid
, &type
,
2540 packing_data_unlock(&to_pack
);
2542 die(_("object %s cannot be read"),
2543 oid_to_hex(&trg_entry
->idx
.oid
));
2545 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2546 oid_to_hex(&trg_entry
->idx
.oid
), (uintmax_t)sz
,
2547 (uintmax_t)trg_size
);
2551 packing_data_lock(&to_pack
);
2552 src
->data
= repo_read_object_file(the_repository
,
2553 &src_entry
->idx
.oid
, &type
,
2555 packing_data_unlock(&to_pack
);
2557 if (src_entry
->preferred_base
) {
2558 static int warned
= 0;
2560 warning(_("object %s cannot be read"),
2561 oid_to_hex(&src_entry
->idx
.oid
));
2563 * Those objects are not included in the
2564 * resulting pack. Be resilient and ignore
2565 * them if they can't be read, in case the
2566 * pack could be created nevertheless.
2570 die(_("object %s cannot be read"),
2571 oid_to_hex(&src_entry
->idx
.oid
));
2574 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2575 oid_to_hex(&src_entry
->idx
.oid
), (uintmax_t)sz
,
2576 (uintmax_t)src_size
);
2580 src
->index
= create_delta_index(src
->data
, src_size
);
2582 static int warned
= 0;
2584 warning(_("suboptimal pack - out of memory"));
2587 *mem_usage
+= sizeof_delta_index(src
->index
);
2590 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
2594 if (DELTA(trg_entry
)) {
2595 /* Prefer only shallower same-sized deltas. */
2596 if (delta_size
== DELTA_SIZE(trg_entry
) &&
2597 src
->depth
+ 1 >= trg
->depth
) {
2604 * Handle memory allocation outside of the cache
2605 * accounting lock. Compiler will optimize the strangeness
2606 * away when NO_PTHREADS is defined.
2608 free(trg_entry
->delta_data
);
2610 if (trg_entry
->delta_data
) {
2611 delta_cache_size
-= DELTA_SIZE(trg_entry
);
2612 trg_entry
->delta_data
= NULL
;
2614 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
2615 delta_cache_size
+= delta_size
;
2617 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
2623 SET_DELTA(trg_entry
, src_entry
);
2624 SET_DELTA_SIZE(trg_entry
, delta_size
);
2625 trg
->depth
= src
->depth
+ 1;
2630 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
2632 struct object_entry
*child
= DELTA_CHILD(me
);
2635 const unsigned int c
= check_delta_limit(child
, n
+ 1);
2638 child
= DELTA_SIBLING(child
);
2643 static unsigned long free_unpacked(struct unpacked
*n
)
2645 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
2646 free_delta_index(n
->index
);
2649 freed_mem
+= SIZE(n
->entry
);
2650 FREE_AND_NULL(n
->data
);
2657 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
2658 int window
, int depth
, unsigned *processed
)
2660 uint32_t i
, idx
= 0, count
= 0;
2661 struct unpacked
*array
;
2662 unsigned long mem_usage
= 0;
2664 CALLOC_ARRAY(array
, window
);
2667 struct object_entry
*entry
;
2668 struct unpacked
*n
= array
+ idx
;
2669 int j
, max_depth
, best_base
= -1;
2678 if (!entry
->preferred_base
) {
2680 display_progress(progress_state
, *processed
);
2684 mem_usage
-= free_unpacked(n
);
2687 while (window_memory_limit
&&
2688 mem_usage
> window_memory_limit
&&
2690 const uint32_t tail
= (idx
+ window
- count
) % window
;
2691 mem_usage
-= free_unpacked(array
+ tail
);
2695 /* We do not compute delta to *create* objects we are not
2698 if (entry
->preferred_base
)
2702 * If the current object is at pack edge, take the depth the
2703 * objects that depend on the current object into account
2704 * otherwise they would become too deep.
2707 if (DELTA_CHILD(entry
)) {
2708 max_depth
-= check_delta_limit(entry
, 0);
2716 uint32_t other_idx
= idx
+ j
;
2718 if (other_idx
>= window
)
2719 other_idx
-= window
;
2720 m
= array
+ other_idx
;
2723 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2727 best_base
= other_idx
;
2731 * If we decided to cache the delta data, then it is best
2732 * to compress it right away. First because we have to do
2733 * it anyway, and doing it here while we're threaded will
2734 * save a lot of time in the non threaded write phase,
2735 * as well as allow for caching more deltas within
2736 * the same cache size limit.
2738 * But only if not writing to stdout, since in that case
2739 * the network is most likely throttling writes anyway,
2740 * and therefore it is best to go to the write phase ASAP
2741 * instead, as we can afford spending more time compressing
2742 * between writes at that moment.
2744 if (entry
->delta_data
&& !pack_to_stdout
) {
2747 size
= do_compress(&entry
->delta_data
, DELTA_SIZE(entry
));
2748 if (size
< (1U << OE_Z_DELTA_BITS
)) {
2749 entry
->z_delta_size
= size
;
2751 delta_cache_size
-= DELTA_SIZE(entry
);
2752 delta_cache_size
+= entry
->z_delta_size
;
2755 FREE_AND_NULL(entry
->delta_data
);
2756 entry
->z_delta_size
= 0;
2760 /* if we made n a delta, and if n is already at max
2761 * depth, leaving it in the window is pointless. we
2762 * should evict it first.
2764 if (DELTA(entry
) && max_depth
<= n
->depth
)
2768 * Move the best delta base up in the window, after the
2769 * currently deltified object, to keep it longer. It will
2770 * be the first base object to be attempted next.
2773 struct unpacked swap
= array
[best_base
];
2774 int dist
= (window
+ idx
- best_base
) % window
;
2775 int dst
= best_base
;
2777 int src
= (dst
+ 1) % window
;
2778 array
[dst
] = array
[src
];
2786 if (count
+ 1 < window
)
2792 for (i
= 0; i
< window
; ++i
) {
2793 free_delta_index(array
[i
].index
);
2794 free(array
[i
].data
);
2800 * The main object list is split into smaller lists, each is handed to
2803 * The main thread waits on the condition that (at least) one of the workers
2804 * has stopped working (which is indicated in the .working member of
2805 * struct thread_params).
2807 * When a work thread has completed its work, it sets .working to 0 and
2808 * signals the main thread and waits on the condition that .data_ready
2811 * The main thread steals half of the work from the worker that has
2812 * most work left to hand it to the idle worker.
2815 struct thread_params
{
2817 struct object_entry
**list
;
2824 pthread_mutex_t mutex
;
2825 pthread_cond_t cond
;
2826 unsigned *processed
;
2829 static pthread_cond_t progress_cond
;
2832 * Mutex and conditional variable can't be statically-initialized on Windows.
2834 static void init_threaded_search(void)
2836 pthread_mutex_init(&cache_mutex
, NULL
);
2837 pthread_mutex_init(&progress_mutex
, NULL
);
2838 pthread_cond_init(&progress_cond
, NULL
);
2841 static void cleanup_threaded_search(void)
2843 pthread_cond_destroy(&progress_cond
);
2844 pthread_mutex_destroy(&cache_mutex
);
2845 pthread_mutex_destroy(&progress_mutex
);
2848 static void *threaded_find_deltas(void *arg
)
2850 struct thread_params
*me
= arg
;
2853 while (me
->remaining
) {
2856 find_deltas(me
->list
, &me
->remaining
,
2857 me
->window
, me
->depth
, me
->processed
);
2861 pthread_cond_signal(&progress_cond
);
2865 * We must not set ->data_ready before we wait on the
2866 * condition because the main thread may have set it to 1
2867 * before we get here. In order to be sure that new
2868 * work is available if we see 1 in ->data_ready, it
2869 * was initialized to 0 before this thread was spawned
2870 * and we reset it to 0 right away.
2872 pthread_mutex_lock(&me
->mutex
);
2873 while (!me
->data_ready
)
2874 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2876 pthread_mutex_unlock(&me
->mutex
);
2881 /* leave ->working 1 so that this doesn't get more work assigned */
2885 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2886 int window
, int depth
, unsigned *processed
)
2888 struct thread_params
*p
;
2889 int i
, ret
, active_threads
= 0;
2891 init_threaded_search();
2893 if (delta_search_threads
<= 1) {
2894 find_deltas(list
, &list_size
, window
, depth
, processed
);
2895 cleanup_threaded_search();
2898 if (progress
> pack_to_stdout
)
2899 fprintf_ln(stderr
, _("Delta compression using up to %d threads"),
2900 delta_search_threads
);
2901 CALLOC_ARRAY(p
, delta_search_threads
);
2903 /* Partition the work amongst work threads. */
2904 for (i
= 0; i
< delta_search_threads
; i
++) {
2905 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
2907 /* don't use too small segments or no deltas will be found */
2908 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
2911 p
[i
].window
= window
;
2913 p
[i
].processed
= processed
;
2915 p
[i
].data_ready
= 0;
2917 /* try to split chunks on "path" boundaries */
2918 while (sub_size
&& sub_size
< list_size
&&
2919 list
[sub_size
]->hash
&&
2920 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
2924 p
[i
].list_size
= sub_size
;
2925 p
[i
].remaining
= sub_size
;
2928 list_size
-= sub_size
;
2931 /* Start work threads. */
2932 for (i
= 0; i
< delta_search_threads
; i
++) {
2933 if (!p
[i
].list_size
)
2935 pthread_mutex_init(&p
[i
].mutex
, NULL
);
2936 pthread_cond_init(&p
[i
].cond
, NULL
);
2937 ret
= pthread_create(&p
[i
].thread
, NULL
,
2938 threaded_find_deltas
, &p
[i
]);
2940 die(_("unable to create thread: %s"), strerror(ret
));
2945 * Now let's wait for work completion. Each time a thread is done
2946 * with its work, we steal half of the remaining work from the
2947 * thread with the largest number of unprocessed objects and give
2948 * it to that newly idle thread. This ensure good load balancing
2949 * until the remaining object list segments are simply too short
2950 * to be worth splitting anymore.
2952 while (active_threads
) {
2953 struct thread_params
*target
= NULL
;
2954 struct thread_params
*victim
= NULL
;
2955 unsigned sub_size
= 0;
2959 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
2964 pthread_cond_wait(&progress_cond
, &progress_mutex
);
2967 for (i
= 0; i
< delta_search_threads
; i
++)
2968 if (p
[i
].remaining
> 2*window
&&
2969 (!victim
|| victim
->remaining
< p
[i
].remaining
))
2972 sub_size
= victim
->remaining
/ 2;
2973 list
= victim
->list
+ victim
->list_size
- sub_size
;
2974 while (sub_size
&& list
[0]->hash
&&
2975 list
[0]->hash
== list
[-1]->hash
) {
2981 * It is possible for some "paths" to have
2982 * so many objects that no hash boundary
2983 * might be found. Let's just steal the
2984 * exact half in that case.
2986 sub_size
= victim
->remaining
/ 2;
2989 target
->list
= list
;
2990 victim
->list_size
-= sub_size
;
2991 victim
->remaining
-= sub_size
;
2993 target
->list_size
= sub_size
;
2994 target
->remaining
= sub_size
;
2995 target
->working
= 1;
2998 pthread_mutex_lock(&target
->mutex
);
2999 target
->data_ready
= 1;
3000 pthread_cond_signal(&target
->cond
);
3001 pthread_mutex_unlock(&target
->mutex
);
3004 pthread_join(target
->thread
, NULL
);
3005 pthread_cond_destroy(&target
->cond
);
3006 pthread_mutex_destroy(&target
->mutex
);
3010 cleanup_threaded_search();
3014 static int obj_is_packed(const struct object_id
*oid
)
3016 return packlist_find(&to_pack
, oid
) ||
3017 (reuse_packfile_bitmap
&&
3018 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
));
3021 static void add_tag_chain(const struct object_id
*oid
)
3026 * We catch duplicates already in add_object_entry(), but we'd
3027 * prefer to do this extra check to avoid having to parse the
3028 * tag at all if we already know that it's being packed (e.g., if
3029 * it was included via bitmaps, we would not have parsed it
3032 if (obj_is_packed(oid
))
3035 tag
= lookup_tag(the_repository
, oid
);
3037 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
3038 die(_("unable to pack objects reachable from tag %s"),
3041 add_object_entry(&tag
->object
.oid
, OBJ_TAG
, NULL
, 0);
3043 if (tag
->tagged
->type
!= OBJ_TAG
)
3046 tag
= (struct tag
*)tag
->tagged
;
3050 static int add_ref_tag(const char *tag UNUSED
, const struct object_id
*oid
,
3051 int flag UNUSED
, void *cb_data UNUSED
)
3053 struct object_id peeled
;
3055 if (!peel_iterated_oid(oid
, &peeled
) && obj_is_packed(&peeled
))
3060 static void prepare_pack(int window
, int depth
)
3062 struct object_entry
**delta_list
;
3063 uint32_t i
, nr_deltas
;
3066 if (use_delta_islands
)
3067 resolve_tree_islands(the_repository
, progress
, &to_pack
);
3069 get_object_details();
3072 * If we're locally repacking then we need to be doubly careful
3073 * from now on in order to make sure no stealth corruption gets
3074 * propagated to the new pack. Clients receiving streamed packs
3075 * should validate everything they get anyway so no need to incur
3076 * the additional cost here in that case.
3078 if (!pack_to_stdout
)
3079 do_check_packed_object_crc
= 1;
3081 if (!to_pack
.nr_objects
|| !window
|| !depth
)
3084 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
3087 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
3088 struct object_entry
*entry
= to_pack
.objects
+ i
;
3091 /* This happens if we decided to reuse existing
3092 * delta from a pack. "reuse_delta &&" is implied.
3096 if (!entry
->type_valid
||
3097 oe_size_less_than(&to_pack
, entry
, 50))
3100 if (entry
->no_try_delta
)
3103 if (!entry
->preferred_base
) {
3105 if (oe_type(entry
) < 0)
3106 die(_("unable to get type of object %s"),
3107 oid_to_hex(&entry
->idx
.oid
));
3109 if (oe_type(entry
) < 0) {
3111 * This object is not found, but we
3112 * don't have to include it anyway.
3118 delta_list
[n
++] = entry
;
3121 if (nr_deltas
&& n
> 1) {
3122 unsigned nr_done
= 0;
3125 progress_state
= start_progress(_("Compressing objects"),
3127 QSORT(delta_list
, n
, type_size_sort
);
3128 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
3129 stop_progress(&progress_state
);
3130 if (nr_done
!= nr_deltas
)
3131 die(_("inconsistency with delta count"));
3136 static int git_pack_config(const char *k
, const char *v
,
3137 const struct config_context
*ctx
, void *cb
)
3139 if (!strcmp(k
, "pack.window")) {
3140 window
= git_config_int(k
, v
, ctx
->kvi
);
3143 if (!strcmp(k
, "pack.windowmemory")) {
3144 window_memory_limit
= git_config_ulong(k
, v
, ctx
->kvi
);
3147 if (!strcmp(k
, "pack.depth")) {
3148 depth
= git_config_int(k
, v
, ctx
->kvi
);
3151 if (!strcmp(k
, "pack.deltacachesize")) {
3152 max_delta_cache_size
= git_config_int(k
, v
, ctx
->kvi
);
3155 if (!strcmp(k
, "pack.deltacachelimit")) {
3156 cache_max_small_delta_size
= git_config_int(k
, v
, ctx
->kvi
);
3159 if (!strcmp(k
, "pack.writebitmaphashcache")) {
3160 if (git_config_bool(k
, v
))
3161 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
3163 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
3166 if (!strcmp(k
, "pack.writebitmaplookuptable")) {
3167 if (git_config_bool(k
, v
))
3168 write_bitmap_options
|= BITMAP_OPT_LOOKUP_TABLE
;
3170 write_bitmap_options
&= ~BITMAP_OPT_LOOKUP_TABLE
;
3173 if (!strcmp(k
, "pack.usebitmaps")) {
3174 use_bitmap_index_default
= git_config_bool(k
, v
);
3177 if (!strcmp(k
, "pack.allowpackreuse")) {
3178 allow_pack_reuse
= git_config_bool(k
, v
);
3181 if (!strcmp(k
, "pack.threads")) {
3182 delta_search_threads
= git_config_int(k
, v
, ctx
->kvi
);
3183 if (delta_search_threads
< 0)
3184 die(_("invalid number of threads specified (%d)"),
3185 delta_search_threads
);
3186 if (!HAVE_THREADS
&& delta_search_threads
!= 1) {
3187 warning(_("no threads support, ignoring %s"), k
);
3188 delta_search_threads
= 0;
3192 if (!strcmp(k
, "pack.indexversion")) {
3193 pack_idx_opts
.version
= git_config_int(k
, v
, ctx
->kvi
);
3194 if (pack_idx_opts
.version
> 2)
3195 die(_("bad pack.indexVersion=%"PRIu32
),
3196 pack_idx_opts
.version
);
3199 if (!strcmp(k
, "pack.writereverseindex")) {
3200 if (git_config_bool(k
, v
))
3201 pack_idx_opts
.flags
|= WRITE_REV
;
3203 pack_idx_opts
.flags
&= ~WRITE_REV
;
3206 if (!strcmp(k
, "uploadpack.blobpackfileuri")) {
3207 struct configured_exclusion
*ex
= xmalloc(sizeof(*ex
));
3208 const char *oid_end
, *pack_end
;
3210 * Stores the pack hash. This is not a true object ID, but is
3213 struct object_id pack_hash
;
3215 if (parse_oid_hex(v
, &ex
->e
.oid
, &oid_end
) ||
3217 parse_oid_hex(oid_end
+ 1, &pack_hash
, &pack_end
) ||
3219 die(_("value of uploadpack.blobpackfileuri must be "
3220 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v
);
3221 if (oidmap_get(&configured_exclusions
, &ex
->e
.oid
))
3222 die(_("object already configured in another "
3223 "uploadpack.blobpackfileuri (got '%s')"), v
);
3224 ex
->pack_hash_hex
= xcalloc(1, pack_end
- oid_end
);
3225 memcpy(ex
->pack_hash_hex
, oid_end
+ 1, pack_end
- oid_end
- 1);
3226 ex
->uri
= xstrdup(pack_end
+ 1);
3227 oidmap_put(&configured_exclusions
, ex
);
3229 return git_default_config(k
, v
, ctx
, cb
);
3232 /* Counters for trace2 output when in --stdin-packs mode. */
3233 static int stdin_packs_found_nr
;
3234 static int stdin_packs_hints_nr
;
3236 static int add_object_entry_from_pack(const struct object_id
*oid
,
3237 struct packed_git
*p
,
3242 enum object_type type
= OBJ_NONE
;
3244 display_progress(progress_state
, ++nr_seen
);
3246 if (have_duplicate_entry(oid
, 0))
3249 ofs
= nth_packed_object_offset(p
, pos
);
3250 if (!want_object_in_pack(oid
, 0, &p
, &ofs
))
3254 struct rev_info
*revs
= _data
;
3255 struct object_info oi
= OBJECT_INFO_INIT
;
3258 if (packed_object_info(the_repository
, p
, ofs
, &oi
) < 0) {
3259 die(_("could not get type of object %s in pack %s"),
3260 oid_to_hex(oid
), p
->pack_name
);
3261 } else if (type
== OBJ_COMMIT
) {
3263 * commits in included packs are used as starting points for the
3264 * subsequent revision walk
3266 add_pending_oid(revs
, NULL
, oid
, 0);
3269 stdin_packs_found_nr
++;
3272 create_object_entry(oid
, type
, 0, 0, 0, p
, ofs
);
3277 static void show_commit_pack_hint(struct commit
*commit UNUSED
,
3280 /* nothing to do; commits don't have a namehash */
3283 static void show_object_pack_hint(struct object
*object
, const char *name
,
3286 struct object_entry
*oe
= packlist_find(&to_pack
, &object
->oid
);
3291 * Our 'to_pack' list was constructed by iterating all objects packed in
3292 * included packs, and so doesn't have a non-zero hash field that you
3293 * would typically pick up during a reachability traversal.
3295 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3296 * here using a now in order to perhaps improve the delta selection
3299 oe
->hash
= pack_name_hash(name
);
3300 oe
->no_try_delta
= name
&& no_try_delta(name
);
3302 stdin_packs_hints_nr
++;
3305 static int pack_mtime_cmp(const void *_a
, const void *_b
)
3307 struct packed_git
*a
= ((const struct string_list_item
*)_a
)->util
;
3308 struct packed_git
*b
= ((const struct string_list_item
*)_b
)->util
;
3311 * order packs by descending mtime so that objects are laid out
3312 * roughly as newest-to-oldest
3314 if (a
->mtime
< b
->mtime
)
3316 else if (b
->mtime
< a
->mtime
)
3322 static void read_packs_list_from_stdin(void)
3324 struct strbuf buf
= STRBUF_INIT
;
3325 struct string_list include_packs
= STRING_LIST_INIT_DUP
;
3326 struct string_list exclude_packs
= STRING_LIST_INIT_DUP
;
3327 struct string_list_item
*item
= NULL
;
3329 struct packed_git
*p
;
3330 struct rev_info revs
;
3332 repo_init_revisions(the_repository
, &revs
, NULL
);
3334 * Use a revision walk to fill in the namehash of objects in the include
3335 * packs. To save time, we'll avoid traversing through objects that are
3336 * in excluded packs.
3338 * That may cause us to avoid populating all of the namehash fields of
3339 * all included objects, but our goal is best-effort, since this is only
3340 * an optimization during delta selection.
3342 revs
.no_kept_objects
= 1;
3343 revs
.keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
3344 revs
.blob_objects
= 1;
3345 revs
.tree_objects
= 1;
3346 revs
.tag_objects
= 1;
3347 revs
.ignore_missing_links
= 1;
3349 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3353 if (*buf
.buf
== '^')
3354 string_list_append(&exclude_packs
, buf
.buf
+ 1);
3356 string_list_append(&include_packs
, buf
.buf
);
3361 string_list_sort(&include_packs
);
3362 string_list_remove_duplicates(&include_packs
, 0);
3363 string_list_sort(&exclude_packs
);
3364 string_list_remove_duplicates(&exclude_packs
, 0);
3366 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3367 const char *pack_name
= pack_basename(p
);
3369 if ((item
= string_list_lookup(&include_packs
, pack_name
)))
3371 if ((item
= string_list_lookup(&exclude_packs
, pack_name
)))
3376 * Arguments we got on stdin may not even be packs. First
3377 * check that to avoid segfaulting later on in
3378 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3380 * Since we first parsed our STDIN and then sorted the input
3381 * lines the pack we error on will be whatever line happens to
3382 * sort first. This is lazy, it's enough that we report one
3383 * bad case here, we don't need to report the first/last one,
3386 for_each_string_list_item(item
, &include_packs
) {
3387 struct packed_git
*p
= item
->util
;
3389 die(_("could not find pack '%s'"), item
->string
);
3390 if (!is_pack_valid(p
))
3391 die(_("packfile %s cannot be accessed"), p
->pack_name
);
3395 * Then, handle all of the excluded packs, marking them as
3396 * kept in-core so that later calls to add_object_entry()
3397 * discards any objects that are also found in excluded packs.
3399 for_each_string_list_item(item
, &exclude_packs
) {
3400 struct packed_git
*p
= item
->util
;
3402 die(_("could not find pack '%s'"), item
->string
);
3403 p
->pack_keep_in_core
= 1;
3407 * Order packs by ascending mtime; use QSORT directly to access the
3408 * string_list_item's ->util pointer, which string_list_sort() does not
3411 QSORT(include_packs
.items
, include_packs
.nr
, pack_mtime_cmp
);
3413 for_each_string_list_item(item
, &include_packs
) {
3414 struct packed_git
*p
= item
->util
;
3415 for_each_object_in_pack(p
,
3416 add_object_entry_from_pack
,
3418 FOR_EACH_OBJECT_PACK_ORDER
);
3421 if (prepare_revision_walk(&revs
))
3422 die(_("revision walk setup failed"));
3423 traverse_commit_list(&revs
,
3424 show_commit_pack_hint
,
3425 show_object_pack_hint
,
3428 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_found",
3429 stdin_packs_found_nr
);
3430 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_hints",
3431 stdin_packs_hints_nr
);
3433 strbuf_release(&buf
);
3434 string_list_clear(&include_packs
, 0);
3435 string_list_clear(&exclude_packs
, 0);
3438 static void add_cruft_object_entry(const struct object_id
*oid
, enum object_type type
,
3439 struct packed_git
*pack
, off_t offset
,
3440 const char *name
, uint32_t mtime
)
3442 struct object_entry
*entry
;
3444 display_progress(progress_state
, ++nr_seen
);
3446 entry
= packlist_find(&to_pack
, oid
);
3449 entry
->hash
= pack_name_hash(name
);
3450 entry
->no_try_delta
= no_try_delta(name
);
3453 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
3455 if (!pack
&& type
== OBJ_BLOB
&& !has_loose_object(oid
)) {
3457 * If a traversed tree has a missing blob then we want
3458 * to avoid adding that missing object to our pack.
3460 * This only applies to missing blobs, not trees,
3461 * because the traversal needs to parse sub-trees but
3464 * Note we only perform this check when we couldn't
3465 * already find the object in a pack, so we're really
3466 * limited to "ensure non-tip blobs which don't exist in
3467 * packs do exist via loose objects". Confused?
3472 entry
= create_object_entry(oid
, type
, pack_name_hash(name
),
3473 0, name
&& no_try_delta(name
),
3477 if (mtime
> oe_cruft_mtime(&to_pack
, entry
))
3478 oe_set_cruft_mtime(&to_pack
, entry
, mtime
);
3482 static void show_cruft_object(struct object
*obj
, const char *name
, void *data UNUSED
)
3485 * if we did not record it earlier, it's at least as old as our
3486 * expiration value. Rather than find it exactly, just use that
3487 * value. This may bump it forward from its real mtime, but it
3488 * will still be "too old" next time we run with the same
3491 * if obj does appear in the packing list, this call is a noop (or may
3492 * set the namehash).
3494 add_cruft_object_entry(&obj
->oid
, obj
->type
, NULL
, 0, name
, cruft_expiration
);
3497 static void show_cruft_commit(struct commit
*commit
, void *data
)
3499 show_cruft_object((struct object
*)commit
, NULL
, data
);
3502 static int cruft_include_check_obj(struct object
*obj
, void *data UNUSED
)
3504 return !has_object_kept_pack(&obj
->oid
, IN_CORE_KEEP_PACKS
);
3507 static int cruft_include_check(struct commit
*commit
, void *data
)
3509 return cruft_include_check_obj((struct object
*)commit
, data
);
3512 static void set_cruft_mtime(const struct object
*object
,
3513 struct packed_git
*pack
,
3514 off_t offset
, time_t mtime
)
3516 add_cruft_object_entry(&object
->oid
, object
->type
, pack
, offset
, NULL
,
3520 static void mark_pack_kept_in_core(struct string_list
*packs
, unsigned keep
)
3522 struct string_list_item
*item
= NULL
;
3523 for_each_string_list_item(item
, packs
) {
3524 struct packed_git
*p
= item
->util
;
3526 die(_("could not find pack '%s'"), item
->string
);
3527 p
->pack_keep_in_core
= keep
;
3531 static void add_unreachable_loose_objects(void);
3532 static void add_objects_in_unpacked_packs(void);
3534 static void enumerate_cruft_objects(void)
3537 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3539 add_objects_in_unpacked_packs();
3540 add_unreachable_loose_objects();
3542 stop_progress(&progress_state
);
3545 static void enumerate_and_traverse_cruft_objects(struct string_list
*fresh_packs
)
3547 struct packed_git
*p
;
3548 struct rev_info revs
;
3551 repo_init_revisions(the_repository
, &revs
, NULL
);
3553 revs
.tag_objects
= 1;
3554 revs
.tree_objects
= 1;
3555 revs
.blob_objects
= 1;
3557 revs
.include_check
= cruft_include_check
;
3558 revs
.include_check_obj
= cruft_include_check_obj
;
3560 revs
.ignore_missing_links
= 1;
3563 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3564 ret
= add_unseen_recent_objects_to_traversal(&revs
, cruft_expiration
,
3565 set_cruft_mtime
, 1);
3566 stop_progress(&progress_state
);
3569 die(_("unable to add cruft objects"));
3572 * Re-mark only the fresh packs as kept so that objects in
3573 * unknown packs do not halt the reachability traversal early.
3575 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
3576 p
->pack_keep_in_core
= 0;
3577 mark_pack_kept_in_core(fresh_packs
, 1);
3579 if (prepare_revision_walk(&revs
))
3580 die(_("revision walk setup failed"));
3582 progress_state
= start_progress(_("Traversing cruft objects"), 0);
3584 traverse_commit_list(&revs
, show_cruft_commit
, show_cruft_object
, NULL
);
3586 stop_progress(&progress_state
);
3589 static void read_cruft_objects(void)
3591 struct strbuf buf
= STRBUF_INIT
;
3592 struct string_list discard_packs
= STRING_LIST_INIT_DUP
;
3593 struct string_list fresh_packs
= STRING_LIST_INIT_DUP
;
3594 struct packed_git
*p
;
3596 ignore_packed_keep_in_core
= 1;
3598 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3602 if (*buf
.buf
== '-')
3603 string_list_append(&discard_packs
, buf
.buf
+ 1);
3605 string_list_append(&fresh_packs
, buf
.buf
);
3609 string_list_sort(&discard_packs
);
3610 string_list_sort(&fresh_packs
);
3612 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3613 const char *pack_name
= pack_basename(p
);
3614 struct string_list_item
*item
;
3616 item
= string_list_lookup(&fresh_packs
, pack_name
);
3618 item
= string_list_lookup(&discard_packs
, pack_name
);
3624 * This pack wasn't mentioned in either the "fresh" or
3625 * "discard" list, so the caller didn't know about it.
3627 * Mark it as kept so that its objects are ignored by
3628 * add_unseen_recent_objects_to_traversal(). We'll
3629 * unmark it before starting the traversal so it doesn't
3630 * halt the traversal early.
3632 p
->pack_keep_in_core
= 1;
3636 mark_pack_kept_in_core(&fresh_packs
, 1);
3637 mark_pack_kept_in_core(&discard_packs
, 0);
3639 if (cruft_expiration
)
3640 enumerate_and_traverse_cruft_objects(&fresh_packs
);
3642 enumerate_cruft_objects();
3644 strbuf_release(&buf
);
3645 string_list_clear(&discard_packs
, 0);
3646 string_list_clear(&fresh_packs
, 0);
3649 static void read_object_list_from_stdin(void)
3651 char line
[GIT_MAX_HEXSZ
+ 1 + PATH_MAX
+ 2];
3652 struct object_id oid
;
3656 if (!fgets(line
, sizeof(line
), stdin
)) {
3660 BUG("fgets returned NULL, not EOF, not error!");
3666 if (line
[0] == '-') {
3667 if (get_oid_hex(line
+1, &oid
))
3668 die(_("expected edge object ID, got garbage:\n %s"),
3670 add_preferred_base(&oid
);
3673 if (parse_oid_hex(line
, &oid
, &p
))
3674 die(_("expected object ID, got garbage:\n %s"), line
);
3676 add_preferred_base_object(p
+ 1);
3677 add_object_entry(&oid
, OBJ_NONE
, p
+ 1, 0);
3681 static void show_commit(struct commit
*commit
, void *data UNUSED
)
3683 add_object_entry(&commit
->object
.oid
, OBJ_COMMIT
, NULL
, 0);
3685 if (write_bitmap_index
)
3686 index_commit_for_bitmap(commit
);
3688 if (use_delta_islands
)
3689 propagate_island_marks(commit
);
3692 static void show_object(struct object
*obj
, const char *name
,
3695 add_preferred_base_object(name
);
3696 add_object_entry(&obj
->oid
, obj
->type
, name
, 0);
3698 if (use_delta_islands
) {
3701 struct object_entry
*ent
;
3703 /* the empty string is a root tree, which is depth 0 */
3704 depth
= *name
? 1 : 0;
3705 for (p
= strchr(name
, '/'); p
; p
= strchr(p
+ 1, '/'))
3708 ent
= packlist_find(&to_pack
, &obj
->oid
);
3709 if (ent
&& depth
> oe_tree_depth(&to_pack
, ent
))
3710 oe_set_tree_depth(&to_pack
, ent
, depth
);
3714 static void show_object__ma_allow_any(struct object
*obj
, const char *name
, void *data
)
3716 assert(arg_missing_action
== MA_ALLOW_ANY
);
3719 * Quietly ignore ALL missing objects. This avoids problems with
3720 * staging them now and getting an odd error later.
3722 if (!has_object(the_repository
, &obj
->oid
, 0))
3725 show_object(obj
, name
, data
);
3728 static void show_object__ma_allow_promisor(struct object
*obj
, const char *name
, void *data
)
3730 assert(arg_missing_action
== MA_ALLOW_PROMISOR
);
3733 * Quietly ignore EXPECTED missing objects. This avoids problems with
3734 * staging them now and getting an odd error later.
3736 if (!has_object(the_repository
, &obj
->oid
, 0) && is_promisor_object(&obj
->oid
))
3739 show_object(obj
, name
, data
);
3742 static int option_parse_missing_action(const struct option
*opt
,
3743 const char *arg
, int unset
)
3748 if (!strcmp(arg
, "error")) {
3749 arg_missing_action
= MA_ERROR
;
3750 fn_show_object
= show_object
;
3754 if (!strcmp(arg
, "allow-any")) {
3755 arg_missing_action
= MA_ALLOW_ANY
;
3756 fetch_if_missing
= 0;
3757 fn_show_object
= show_object__ma_allow_any
;
3761 if (!strcmp(arg
, "allow-promisor")) {
3762 arg_missing_action
= MA_ALLOW_PROMISOR
;
3763 fetch_if_missing
= 0;
3764 fn_show_object
= show_object__ma_allow_promisor
;
3768 die(_("invalid value for '%s': '%s'"), "--missing", arg
);
3772 static void show_edge(struct commit
*commit
)
3774 add_preferred_base(&commit
->object
.oid
);
3777 static int add_object_in_unpacked_pack(const struct object_id
*oid
,
3778 struct packed_git
*pack
,
3786 if (pack
->is_cruft
) {
3787 if (load_pack_mtimes(pack
) < 0)
3788 die(_("could not load cruft pack .mtimes"));
3789 mtime
= nth_packed_mtime(pack
, pos
);
3791 mtime
= pack
->mtime
;
3793 offset
= nth_packed_object_offset(pack
, pos
);
3795 add_cruft_object_entry(oid
, OBJ_NONE
, pack
, offset
,
3798 add_object_entry(oid
, OBJ_NONE
, "", 0);
3803 static void add_objects_in_unpacked_packs(void)
3805 if (for_each_packed_object(add_object_in_unpacked_pack
, NULL
,
3806 FOR_EACH_OBJECT_PACK_ORDER
|
3807 FOR_EACH_OBJECT_LOCAL_ONLY
|
3808 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
|
3809 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS
))
3810 die(_("cannot open pack index"));
3813 static int add_loose_object(const struct object_id
*oid
, const char *path
,
3816 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
3819 warning(_("loose object at %s could not be examined"), path
);
3825 if (stat(path
, &st
) < 0) {
3826 if (errno
== ENOENT
)
3828 return error_errno("unable to stat %s", oid_to_hex(oid
));
3831 add_cruft_object_entry(oid
, type
, NULL
, 0, NULL
,
3834 add_object_entry(oid
, type
, "", 0);
3840 * We actually don't even have to worry about reachability here.
3841 * add_object_entry will weed out duplicates, so we just add every
3842 * loose object we find.
3844 static void add_unreachable_loose_objects(void)
3846 for_each_loose_file_in_objdir(get_object_directory(),
3851 static int has_sha1_pack_kept_or_nonlocal(const struct object_id
*oid
)
3853 static struct packed_git
*last_found
= (void *)1;
3854 struct packed_git
*p
;
3856 p
= (last_found
!= (void *)1) ? last_found
:
3857 get_all_packs(the_repository
);
3860 if ((!p
->pack_local
|| p
->pack_keep
||
3861 p
->pack_keep_in_core
) &&
3862 find_pack_entry_one(oid
->hash
, p
)) {
3866 if (p
== last_found
)
3867 p
= get_all_packs(the_repository
);
3870 if (p
== last_found
)
3877 * Store a list of sha1s that are should not be discarded
3878 * because they are either written too recently, or are
3879 * reachable from another object that was.
3881 * This is filled by get_object_list.
3883 static struct oid_array recent_objects
;
3885 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
3888 if (!unpack_unreachable_expiration
)
3890 if (mtime
> unpack_unreachable_expiration
)
3892 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
3897 static void loosen_unused_packed_objects(void)
3899 struct packed_git
*p
;
3901 uint32_t loosened_objects_nr
= 0;
3902 struct object_id oid
;
3904 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3905 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
3908 if (open_pack_index(p
))
3909 die(_("cannot open pack index"));
3911 for (i
= 0; i
< p
->num_objects
; i
++) {
3912 nth_packed_object_id(&oid
, p
, i
);
3913 if (!packlist_find(&to_pack
, &oid
) &&
3914 !has_sha1_pack_kept_or_nonlocal(&oid
) &&
3915 !loosened_object_can_be_discarded(&oid
, p
->mtime
)) {
3916 if (force_object_loose(&oid
, p
->mtime
))
3917 die(_("unable to force loose object"));
3918 loosened_objects_nr
++;
3923 trace2_data_intmax("pack-objects", the_repository
,
3924 "loosen_unused_packed_objects/loosened", loosened_objects_nr
);
3928 * This tracks any options which pack-reuse code expects to be on, or which a
3929 * reader of the pack might not understand, and which would therefore prevent
3930 * blind reuse of what we have on disk.
3932 static int pack_options_allow_reuse(void)
3934 return allow_pack_reuse
&&
3936 !ignore_packed_keep_on_disk
&&
3937 !ignore_packed_keep_in_core
&&
3938 (!local
|| !have_non_local_packs
) &&
3942 static int get_object_list_from_bitmap(struct rev_info
*revs
)
3944 if (!(bitmap_git
= prepare_bitmap_walk(revs
, 0)))
3947 if (pack_options_allow_reuse() &&
3948 !reuse_partial_packfile_from_bitmap(
3951 &reuse_packfile_objects
,
3952 &reuse_packfile_bitmap
)) {
3953 assert(reuse_packfile_objects
);
3954 nr_result
+= reuse_packfile_objects
;
3955 nr_seen
+= reuse_packfile_objects
;
3956 display_progress(progress_state
, nr_seen
);
3959 traverse_bitmap_commit_list(bitmap_git
, revs
,
3960 &add_object_entry_from_bitmap
);
3964 static void record_recent_object(struct object
*obj
,
3965 const char *name UNUSED
,
3968 oid_array_append(&recent_objects
, &obj
->oid
);
3971 static void record_recent_commit(struct commit
*commit
, void *data UNUSED
)
3973 oid_array_append(&recent_objects
, &commit
->object
.oid
);
3976 static int mark_bitmap_preferred_tip(const char *refname
,
3977 const struct object_id
*oid
,
3981 struct object_id peeled
;
3982 struct object
*object
;
3984 if (!peel_iterated_oid(oid
, &peeled
))
3987 object
= parse_object_or_die(oid
, refname
);
3988 if (object
->type
== OBJ_COMMIT
)
3989 object
->flags
|= NEEDS_BITMAP
;
3994 static void mark_bitmap_preferred_tips(void)
3996 struct string_list_item
*item
;
3997 const struct string_list
*preferred_tips
;
3999 preferred_tips
= bitmap_preferred_tips(the_repository
);
4000 if (!preferred_tips
)
4003 for_each_string_list_item(item
, preferred_tips
) {
4004 for_each_ref_in(item
->string
, mark_bitmap_preferred_tip
, NULL
);
4008 static void get_object_list(struct rev_info
*revs
, int ac
, const char **av
)
4010 struct setup_revision_opt s_r_opt
= {
4011 .allow_exclude_promisor_objects
= 1,
4017 save_commit_buffer
= 0;
4018 setup_revisions(ac
, av
, revs
, &s_r_opt
);
4020 /* make sure shallows are read */
4021 is_repository_shallow(the_repository
);
4023 save_warning
= warn_on_object_refname_ambiguity
;
4024 warn_on_object_refname_ambiguity
= 0;
4026 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
4027 int len
= strlen(line
);
4028 if (len
&& line
[len
- 1] == '\n')
4033 if (!strcmp(line
, "--not")) {
4034 flags
^= UNINTERESTING
;
4035 write_bitmap_index
= 0;
4038 if (starts_with(line
, "--shallow ")) {
4039 struct object_id oid
;
4040 if (get_oid_hex(line
+ 10, &oid
))
4041 die("not an object name '%s'", line
+ 10);
4042 register_shallow(the_repository
, &oid
);
4043 use_bitmap_index
= 0;
4046 die(_("not a rev '%s'"), line
);
4048 if (handle_revision_arg(line
, revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
4049 die(_("bad revision '%s'"), line
);
4052 warn_on_object_refname_ambiguity
= save_warning
;
4054 if (use_bitmap_index
&& !get_object_list_from_bitmap(revs
))
4057 if (use_delta_islands
)
4058 load_delta_islands(the_repository
, progress
);
4060 if (write_bitmap_index
)
4061 mark_bitmap_preferred_tips();
4063 if (prepare_revision_walk(revs
))
4064 die(_("revision walk setup failed"));
4065 mark_edges_uninteresting(revs
, show_edge
, sparse
);
4067 if (!fn_show_object
)
4068 fn_show_object
= show_object
;
4069 traverse_commit_list(revs
,
4070 show_commit
, fn_show_object
,
4073 if (unpack_unreachable_expiration
) {
4074 revs
->ignore_missing_links
= 1;
4075 if (add_unseen_recent_objects_to_traversal(revs
,
4076 unpack_unreachable_expiration
, NULL
, 0))
4077 die(_("unable to add recent objects"));
4078 if (prepare_revision_walk(revs
))
4079 die(_("revision walk setup failed"));
4080 traverse_commit_list(revs
, record_recent_commit
,
4081 record_recent_object
, NULL
);
4084 if (keep_unreachable
)
4085 add_objects_in_unpacked_packs();
4086 if (pack_loose_unreachable
)
4087 add_unreachable_loose_objects();
4088 if (unpack_unreachable
)
4089 loosen_unused_packed_objects();
4091 oid_array_clear(&recent_objects
);
4094 static void add_extra_kept_packs(const struct string_list
*names
)
4096 struct packed_git
*p
;
4101 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4102 const char *name
= basename(p
->pack_name
);
4108 for (i
= 0; i
< names
->nr
; i
++)
4109 if (!fspathcmp(name
, names
->items
[i
].string
))
4112 if (i
< names
->nr
) {
4113 p
->pack_keep_in_core
= 1;
4114 ignore_packed_keep_in_core
= 1;
4120 static int option_parse_quiet(const struct option
*opt
, const char *arg
,
4123 BUG_ON_OPT_ARG(arg
);
4132 static int option_parse_index_version(const struct option
*opt
,
4133 const char *arg
, int unset
)
4136 const char *val
= arg
;
4138 BUG_ON_OPT_NEG(unset
);
4140 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
4141 if (pack_idx_opts
.version
> 2)
4142 die(_("unsupported index version %s"), val
);
4143 if (*c
== ',' && c
[1])
4144 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
4145 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
4146 die(_("bad index version '%s'"), val
);
4150 static int option_parse_unpack_unreachable(const struct option
*opt
,
4151 const char *arg
, int unset
)
4154 unpack_unreachable
= 0;
4155 unpack_unreachable_expiration
= 0;
4158 unpack_unreachable
= 1;
4160 unpack_unreachable_expiration
= approxidate(arg
);
4165 static int option_parse_cruft_expiration(const struct option
*opt
,
4166 const char *arg
, int unset
)
4170 cruft_expiration
= 0;
4174 cruft_expiration
= approxidate(arg
);
4179 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
4181 int use_internal_rev_list
= 0;
4183 int all_progress_implied
= 0;
4184 struct strvec rp
= STRVEC_INIT
;
4185 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
4186 int rev_list_index
= 0;
4187 int stdin_packs
= 0;
4188 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
4189 struct list_objects_filter_options filter_options
=
4190 LIST_OBJECTS_FILTER_INIT
;
4192 struct option pack_objects_options
[] = {
4193 OPT_CALLBACK_F('q', "quiet", NULL
, NULL
,
4194 N_("do not show progress meter"),
4195 PARSE_OPT_NOARG
, option_parse_quiet
),
4196 OPT_SET_INT(0, "progress", &progress
,
4197 N_("show progress meter"), 1),
4198 OPT_SET_INT(0, "all-progress", &progress
,
4199 N_("show progress meter during object writing phase"), 2),
4200 OPT_BOOL(0, "all-progress-implied",
4201 &all_progress_implied
,
4202 N_("similar to --all-progress when progress meter is shown")),
4203 OPT_CALLBACK_F(0, "index-version", NULL
, N_("<version>[,<offset>]"),
4204 N_("write the pack index file in the specified idx format version"),
4205 PARSE_OPT_NONEG
, option_parse_index_version
),
4206 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
4207 N_("maximum size of each output pack file")),
4208 OPT_BOOL(0, "local", &local
,
4209 N_("ignore borrowed objects from alternate object store")),
4210 OPT_BOOL(0, "incremental", &incremental
,
4211 N_("ignore packed objects")),
4212 OPT_INTEGER(0, "window", &window
,
4213 N_("limit pack window by objects")),
4214 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
4215 N_("limit pack window by memory in addition to object limit")),
4216 OPT_INTEGER(0, "depth", &depth
,
4217 N_("maximum length of delta chain allowed in the resulting pack")),
4218 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
4219 N_("reuse existing deltas")),
4220 OPT_BOOL(0, "reuse-object", &reuse_object
,
4221 N_("reuse existing objects")),
4222 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
4223 N_("use OFS_DELTA objects")),
4224 OPT_INTEGER(0, "threads", &delta_search_threads
,
4225 N_("use threads when searching for best delta matches")),
4226 OPT_BOOL(0, "non-empty", &non_empty
,
4227 N_("do not create an empty pack output")),
4228 OPT_BOOL(0, "revs", &use_internal_rev_list
,
4229 N_("read revision arguments from standard input")),
4230 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked
,
4231 N_("limit the objects to those that are not yet packed"),
4232 1, PARSE_OPT_NONEG
),
4233 OPT_SET_INT_F(0, "all", &rev_list_all
,
4234 N_("include objects reachable from any reference"),
4235 1, PARSE_OPT_NONEG
),
4236 OPT_SET_INT_F(0, "reflog", &rev_list_reflog
,
4237 N_("include objects referred by reflog entries"),
4238 1, PARSE_OPT_NONEG
),
4239 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index
,
4240 N_("include objects referred to by the index"),
4241 1, PARSE_OPT_NONEG
),
4242 OPT_BOOL(0, "stdin-packs", &stdin_packs
,
4243 N_("read packs from stdin")),
4244 OPT_BOOL(0, "stdout", &pack_to_stdout
,
4245 N_("output pack to stdout")),
4246 OPT_BOOL(0, "include-tag", &include_tag
,
4247 N_("include tag objects that refer to objects to be packed")),
4248 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
4249 N_("keep unreachable objects")),
4250 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
4251 N_("pack loose unreachable objects")),
4252 OPT_CALLBACK_F(0, "unpack-unreachable", NULL
, N_("time"),
4253 N_("unpack unreachable objects newer than <time>"),
4254 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
),
4255 OPT_BOOL(0, "cruft", &cruft
, N_("create a cruft pack")),
4256 OPT_CALLBACK_F(0, "cruft-expiration", NULL
, N_("time"),
4257 N_("expire cruft objects older than <time>"),
4258 PARSE_OPT_OPTARG
, option_parse_cruft_expiration
),
4259 OPT_BOOL(0, "sparse", &sparse
,
4260 N_("use the sparse reachability algorithm")),
4261 OPT_BOOL(0, "thin", &thin
,
4262 N_("create thin packs")),
4263 OPT_BOOL(0, "shallow", &shallow
,
4264 N_("create packs suitable for shallow fetches")),
4265 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk
,
4266 N_("ignore packs that have companion .keep file")),
4267 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
4268 N_("ignore this pack")),
4269 OPT_INTEGER(0, "compression", &pack_compression_level
,
4270 N_("pack compression level")),
4271 OPT_BOOL(0, "keep-true-parents", &grafts_keep_true_parents
,
4272 N_("do not hide commits by grafts")),
4273 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
4274 N_("use a bitmap index if available to speed up counting objects")),
4275 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index
,
4276 N_("write a bitmap index together with the pack index"),
4278 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4279 &write_bitmap_index
,
4280 N_("write a bitmap index if possible"),
4281 WRITE_BITMAP_QUIET
, PARSE_OPT_HIDDEN
),
4282 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
4283 OPT_CALLBACK_F(0, "missing", NULL
, N_("action"),
4284 N_("handling for missing objects"), PARSE_OPT_NONEG
,
4285 option_parse_missing_action
),
4286 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
4287 N_("do not pack objects in promisor packfiles")),
4288 OPT_BOOL(0, "delta-islands", &use_delta_islands
,
4289 N_("respect islands during delta compression")),
4290 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols
,
4292 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4296 if (DFS_NUM_STATES
> (1 << OE_DFS_STATE_BITS
))
4297 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4299 disable_replace_refs();
4301 sparse
= git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4302 if (the_repository
->gitdir
) {
4303 prepare_repo_settings(the_repository
);
4305 sparse
= the_repository
->settings
.pack_use_sparse
;
4308 reset_pack_idx_option(&pack_idx_opts
);
4309 pack_idx_opts
.flags
|= WRITE_REV
;
4310 git_config(git_pack_config
, NULL
);
4311 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX
, 0))
4312 pack_idx_opts
.flags
&= ~WRITE_REV
;
4314 progress
= isatty(2);
4315 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
4319 base_name
= argv
[0];
4322 if (pack_to_stdout
!= !base_name
|| argc
)
4323 usage_with_options(pack_usage
, pack_objects_options
);
4327 if (depth
>= (1 << OE_DEPTH_BITS
)) {
4328 warning(_("delta chain depth %d is too deep, forcing %d"),
4329 depth
, (1 << OE_DEPTH_BITS
) - 1);
4330 depth
= (1 << OE_DEPTH_BITS
) - 1;
4332 if (cache_max_small_delta_size
>= (1U << OE_Z_DELTA_BITS
)) {
4333 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4334 (1U << OE_Z_DELTA_BITS
) - 1);
4335 cache_max_small_delta_size
= (1U << OE_Z_DELTA_BITS
) - 1;
4340 strvec_push(&rp
, "pack-objects");
4342 use_internal_rev_list
= 1;
4343 strvec_push(&rp
, shallow
4344 ? "--objects-edge-aggressive"
4345 : "--objects-edge");
4347 strvec_push(&rp
, "--objects");
4350 use_internal_rev_list
= 1;
4351 strvec_push(&rp
, "--all");
4353 if (rev_list_reflog
) {
4354 use_internal_rev_list
= 1;
4355 strvec_push(&rp
, "--reflog");
4357 if (rev_list_index
) {
4358 use_internal_rev_list
= 1;
4359 strvec_push(&rp
, "--indexed-objects");
4361 if (rev_list_unpacked
&& !stdin_packs
) {
4362 use_internal_rev_list
= 1;
4363 strvec_push(&rp
, "--unpacked");
4366 if (exclude_promisor_objects
) {
4367 use_internal_rev_list
= 1;
4368 fetch_if_missing
= 0;
4369 strvec_push(&rp
, "--exclude-promisor-objects");
4371 if (unpack_unreachable
|| keep_unreachable
|| pack_loose_unreachable
)
4372 use_internal_rev_list
= 1;
4376 if (pack_compression_level
== -1)
4377 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
4378 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
4379 die(_("bad pack compression level %d"), pack_compression_level
);
4381 if (!delta_search_threads
) /* --threads=0 means autodetect */
4382 delta_search_threads
= online_cpus();
4384 if (!HAVE_THREADS
&& delta_search_threads
!= 1)
4385 warning(_("no threads support, ignoring --threads"));
4386 if (!pack_to_stdout
&& !pack_size_limit
&& !cruft
)
4387 pack_size_limit
= pack_size_limit_cfg
;
4388 if (pack_to_stdout
&& pack_size_limit
)
4389 die(_("--max-pack-size cannot be used to build a pack for transfer"));
4390 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
4391 warning(_("minimum pack size limit is 1 MiB"));
4392 pack_size_limit
= 1024*1024;
4395 if (!pack_to_stdout
&& thin
)
4396 die(_("--thin cannot be used to build an indexable pack"));
4398 if (keep_unreachable
&& unpack_unreachable
)
4399 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
4400 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
4401 unpack_unreachable_expiration
= 0;
4403 if (filter_options
.choice
) {
4404 if (!pack_to_stdout
)
4405 die(_("cannot use --filter without --stdout"));
4407 die(_("cannot use --filter with --stdin-packs"));
4410 if (stdin_packs
&& use_internal_rev_list
)
4411 die(_("cannot use internal rev list with --stdin-packs"));
4414 if (use_internal_rev_list
)
4415 die(_("cannot use internal rev list with --cruft"));
4417 die(_("cannot use --stdin-packs with --cruft"));
4418 if (pack_size_limit
)
4419 die(_("cannot use --max-pack-size with --cruft"));
4423 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4425 * - to produce good pack (with bitmap index not-yet-packed objects are
4426 * packed in suboptimal order).
4428 * - to use more robust pack-generation codepath (avoiding possible
4429 * bugs in bitmap code and possible bitmap index corruption).
4431 if (!pack_to_stdout
)
4432 use_bitmap_index_default
= 0;
4434 if (use_bitmap_index
< 0)
4435 use_bitmap_index
= use_bitmap_index_default
;
4437 /* "hard" reasons not to use bitmaps; these just won't work at all */
4438 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow(the_repository
))
4439 use_bitmap_index
= 0;
4441 if (pack_to_stdout
|| !rev_list_all
)
4442 write_bitmap_index
= 0;
4444 if (use_delta_islands
)
4445 strvec_push(&rp
, "--topo-order");
4447 if (progress
&& all_progress_implied
)
4450 add_extra_kept_packs(&keep_pack_list
);
4451 if (ignore_packed_keep_on_disk
) {
4452 struct packed_git
*p
;
4453 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
4454 if (p
->pack_local
&& p
->pack_keep
)
4456 if (!p
) /* no keep-able packs found */
4457 ignore_packed_keep_on_disk
= 0;
4461 * unlike ignore_packed_keep_on_disk above, we do not
4462 * want to unset "local" based on looking at packs, as
4463 * it also covers non-local objects
4465 struct packed_git
*p
;
4466 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4467 if (!p
->pack_local
) {
4468 have_non_local_packs
= 1;
4474 trace2_region_enter("pack-objects", "enumerate-objects",
4476 prepare_packing_data(the_repository
, &to_pack
);
4478 if (progress
&& !cruft
)
4479 progress_state
= start_progress(_("Enumerating objects"), 0);
4481 /* avoids adding objects in excluded packs */
4482 ignore_packed_keep_in_core
= 1;
4483 read_packs_list_from_stdin();
4484 if (rev_list_unpacked
)
4485 add_unreachable_loose_objects();
4487 read_cruft_objects();
4488 } else if (!use_internal_rev_list
) {
4489 read_object_list_from_stdin();
4491 struct rev_info revs
;
4493 repo_init_revisions(the_repository
, &revs
, NULL
);
4494 list_objects_filter_copy(&revs
.filter
, &filter_options
);
4495 get_object_list(&revs
, rp
.nr
, rp
.v
);
4496 release_revisions(&revs
);
4498 cleanup_preferred_base();
4499 if (include_tag
&& nr_result
)
4500 for_each_tag_ref(add_ref_tag
, NULL
);
4501 stop_progress(&progress_state
);
4502 trace2_region_leave("pack-objects", "enumerate-objects",
4505 if (non_empty
&& !nr_result
)
4508 trace2_region_enter("pack-objects", "prepare-pack",
4510 prepare_pack(window
, depth
);
4511 trace2_region_leave("pack-objects", "prepare-pack",
4515 trace2_region_enter("pack-objects", "write-pack-file", the_repository
);
4516 write_excluded_by_configs();
4518 trace2_region_leave("pack-objects", "write-pack-file", the_repository
);
4522 _("Total %"PRIu32
" (delta %"PRIu32
"),"
4523 " reused %"PRIu32
" (delta %"PRIu32
"),"
4524 " pack-reused %"PRIu32
),
4525 written
, written_delta
, reused
, reused_delta
,
4526 reuse_packfile_objects
);
4529 list_objects_filter_release(&filter_options
);