3 #include "environment.h"
6 #include "repository.h"
16 #include "pack-revindex.h"
17 #include "csum-file.h"
18 #include "tree-walk.h"
21 #include "list-objects.h"
22 #include "list-objects-filter.h"
23 #include "list-objects-filter-options.h"
24 #include "pack-objects.h"
27 #include "streaming.h"
28 #include "thread-utils.h"
29 #include "pack-bitmap.h"
30 #include "delta-islands.h"
31 #include "reachable.h"
32 #include "oid-array.h"
36 #include "object-file.h"
37 #include "object-store.h"
38 #include "replace-object.h"
43 #include "promisor-remote.h"
44 #include "pack-mtimes.h"
45 #include "parse-options.h"
49 * Objects we are going to pack are collected in the `to_pack` structure.
50 * It contains an array (dynamically expanded) of the object data, and a map
51 * that can resolve SHA1s to their position in the array.
53 static struct packing_data to_pack
;
55 static inline struct object_entry
*oe_delta(
56 const struct packing_data
*pack
,
57 const struct object_entry
*e
)
62 return &pack
->ext_bases
[e
->delta_idx
- 1];
64 return &pack
->objects
[e
->delta_idx
- 1];
67 static inline unsigned long oe_delta_size(struct packing_data
*pack
,
68 const struct object_entry
*e
)
70 if (e
->delta_size_valid
)
71 return e
->delta_size_
;
74 * pack->delta_size[] can't be NULL because oe_set_delta_size()
75 * must have been called when a new delta is saved with
77 * If oe_delta() returns NULL (i.e. default state, which means
78 * delta_size_valid is also false), then the caller must never
79 * call oe_delta_size().
81 return pack
->delta_size
[e
- pack
->objects
];
84 unsigned long oe_get_size_slow(struct packing_data
*pack
,
85 const struct object_entry
*e
);
87 static inline unsigned long oe_size(struct packing_data
*pack
,
88 const struct object_entry
*e
)
93 return oe_get_size_slow(pack
, e
);
96 static inline void oe_set_delta(struct packing_data
*pack
,
97 struct object_entry
*e
,
98 struct object_entry
*delta
)
101 e
->delta_idx
= (delta
- pack
->objects
) + 1;
106 static inline struct object_entry
*oe_delta_sibling(
107 const struct packing_data
*pack
,
108 const struct object_entry
*e
)
110 if (e
->delta_sibling_idx
)
111 return &pack
->objects
[e
->delta_sibling_idx
- 1];
115 static inline struct object_entry
*oe_delta_child(
116 const struct packing_data
*pack
,
117 const struct object_entry
*e
)
119 if (e
->delta_child_idx
)
120 return &pack
->objects
[e
->delta_child_idx
- 1];
124 static inline void oe_set_delta_child(struct packing_data
*pack
,
125 struct object_entry
*e
,
126 struct object_entry
*delta
)
129 e
->delta_child_idx
= (delta
- pack
->objects
) + 1;
131 e
->delta_child_idx
= 0;
134 static inline void oe_set_delta_sibling(struct packing_data
*pack
,
135 struct object_entry
*e
,
136 struct object_entry
*delta
)
139 e
->delta_sibling_idx
= (delta
- pack
->objects
) + 1;
141 e
->delta_sibling_idx
= 0;
144 static inline void oe_set_size(struct packing_data
*pack
,
145 struct object_entry
*e
,
148 if (size
< pack
->oe_size_limit
) {
153 if (oe_get_size_slow(pack
, e
) != size
)
154 BUG("'size' is supposed to be the object size!");
158 static inline void oe_set_delta_size(struct packing_data
*pack
,
159 struct object_entry
*e
,
162 if (size
< pack
->oe_delta_size_limit
) {
163 e
->delta_size_
= size
;
164 e
->delta_size_valid
= 1;
166 packing_data_lock(pack
);
167 if (!pack
->delta_size
)
168 ALLOC_ARRAY(pack
->delta_size
, pack
->nr_alloc
);
169 packing_data_unlock(pack
);
171 pack
->delta_size
[e
- pack
->objects
] = size
;
172 e
->delta_size_valid
= 0;
176 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
177 #define SIZE(obj) oe_size(&to_pack, obj)
178 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
179 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
180 #define DELTA(obj) oe_delta(&to_pack, obj)
181 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
182 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
183 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
184 #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
185 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
186 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
187 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
189 static const char *pack_usage
[] = {
190 N_("git pack-objects --stdout [<options>] [< <ref-list> | < <object-list>]"),
191 N_("git pack-objects [<options>] <base-name> [< <ref-list> | < <object-list>]"),
195 static struct pack_idx_entry
**written_list
;
196 static uint32_t nr_result
, nr_written
, nr_seen
;
197 static struct bitmap_index
*bitmap_git
;
198 static uint32_t write_layer
;
200 static int non_empty
;
201 static int reuse_delta
= 1, reuse_object
= 1;
202 static int keep_unreachable
, unpack_unreachable
, include_tag
;
203 static timestamp_t unpack_unreachable_expiration
;
204 static int pack_loose_unreachable
;
206 static timestamp_t cruft_expiration
;
208 static int have_non_local_packs
;
209 static int incremental
;
210 static int ignore_packed_keep_on_disk
;
211 static int ignore_packed_keep_in_core
;
212 static int allow_ofs_delta
;
213 static struct pack_idx_option pack_idx_opts
;
214 static const char *base_name
;
215 static int progress
= 1;
216 static int window
= 10;
217 static unsigned long pack_size_limit
;
218 static int depth
= 50;
219 static int delta_search_threads
;
220 static int pack_to_stdout
;
223 static int num_preferred_base
;
224 static struct progress
*progress_state
;
226 static struct packed_git
*reuse_packfile
;
227 static uint32_t reuse_packfile_objects
;
228 static struct bitmap
*reuse_packfile_bitmap
;
230 static int use_bitmap_index_default
= 1;
231 static int use_bitmap_index
= -1;
232 static int allow_pack_reuse
= 1;
234 WRITE_BITMAP_FALSE
= 0,
237 } write_bitmap_index
;
238 static uint16_t write_bitmap_options
= BITMAP_OPT_HASH_CACHE
;
240 static int exclude_promisor_objects
;
242 static int use_delta_islands
;
244 static unsigned long delta_cache_size
= 0;
245 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
246 static unsigned long cache_max_small_delta_size
= 1000;
248 static unsigned long window_memory_limit
= 0;
250 static struct string_list uri_protocols
= STRING_LIST_INIT_NODUP
;
252 enum missing_action
{
253 MA_ERROR
= 0, /* fail if any missing objects are encountered */
254 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
255 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
257 static enum missing_action arg_missing_action
;
258 static show_object_fn fn_show_object
;
260 struct configured_exclusion
{
261 struct oidmap_entry e
;
265 static struct oidmap configured_exclusions
;
267 static struct oidset excluded_by_config
;
272 static uint32_t written
, written_delta
;
273 static uint32_t reused
, reused_delta
;
278 static struct commit
**indexed_commits
;
279 static unsigned int indexed_commits_nr
;
280 static unsigned int indexed_commits_alloc
;
282 static void index_commit_for_bitmap(struct commit
*commit
)
284 if (indexed_commits_nr
>= indexed_commits_alloc
) {
285 indexed_commits_alloc
= (indexed_commits_alloc
+ 32) * 2;
286 REALLOC_ARRAY(indexed_commits
, indexed_commits_alloc
);
289 indexed_commits
[indexed_commits_nr
++] = commit
;
292 static void *get_delta(struct object_entry
*entry
)
294 unsigned long size
, base_size
, delta_size
;
295 void *buf
, *base_buf
, *delta_buf
;
296 enum object_type type
;
298 buf
= repo_read_object_file(the_repository
, &entry
->idx
.oid
, &type
,
301 die(_("unable to read %s"), oid_to_hex(&entry
->idx
.oid
));
302 base_buf
= repo_read_object_file(the_repository
,
303 &DELTA(entry
)->idx
.oid
, &type
,
306 die("unable to read %s",
307 oid_to_hex(&DELTA(entry
)->idx
.oid
));
308 delta_buf
= diff_delta(base_buf
, base_size
,
309 buf
, size
, &delta_size
, 0);
311 * We successfully computed this delta once but dropped it for
312 * memory reasons. Something is very wrong if this time we
313 * recompute and create a different delta.
315 if (!delta_buf
|| delta_size
!= DELTA_SIZE(entry
))
316 BUG("delta size changed");
322 static unsigned long do_compress(void **pptr
, unsigned long size
)
326 unsigned long maxsize
;
328 git_deflate_init(&stream
, pack_compression_level
);
329 maxsize
= git_deflate_bound(&stream
, size
);
332 out
= xmalloc(maxsize
);
336 stream
.avail_in
= size
;
337 stream
.next_out
= out
;
338 stream
.avail_out
= maxsize
;
339 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
341 git_deflate_end(&stream
);
344 return stream
.total_out
;
347 static unsigned long write_large_blob_data(struct git_istream
*st
, struct hashfile
*f
,
348 const struct object_id
*oid
)
351 unsigned char ibuf
[1024 * 16];
352 unsigned char obuf
[1024 * 16];
353 unsigned long olen
= 0;
355 git_deflate_init(&stream
, pack_compression_level
);
360 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
362 die(_("unable to read %s"), oid_to_hex(oid
));
364 stream
.next_in
= ibuf
;
365 stream
.avail_in
= readlen
;
366 while ((stream
.avail_in
|| readlen
== 0) &&
367 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
368 stream
.next_out
= obuf
;
369 stream
.avail_out
= sizeof(obuf
);
370 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
371 hashwrite(f
, obuf
, stream
.next_out
- obuf
);
372 olen
+= stream
.next_out
- obuf
;
375 die(_("deflate error (%d)"), zret
);
377 if (zret
!= Z_STREAM_END
)
378 die(_("deflate error (%d)"), zret
);
382 git_deflate_end(&stream
);
387 * we are going to reuse the existing object data as is. make
388 * sure it is not corrupt.
390 static int check_pack_inflate(struct packed_git
*p
,
391 struct pack_window
**w_curs
,
394 unsigned long expect
)
397 unsigned char fakebuf
[4096], *in
;
400 memset(&stream
, 0, sizeof(stream
));
401 git_inflate_init(&stream
);
403 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
405 stream
.next_out
= fakebuf
;
406 stream
.avail_out
= sizeof(fakebuf
);
407 st
= git_inflate(&stream
, Z_FINISH
);
408 offset
+= stream
.next_in
- in
;
409 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
410 git_inflate_end(&stream
);
411 return (st
== Z_STREAM_END
&&
412 stream
.total_out
== expect
&&
413 stream
.total_in
== len
) ? 0 : -1;
416 static void copy_pack_data(struct hashfile
*f
,
417 struct packed_git
*p
,
418 struct pack_window
**w_curs
,
426 in
= use_pack(p
, w_curs
, offset
, &avail
);
428 avail
= (unsigned long)len
;
429 hashwrite(f
, in
, avail
);
435 static inline int oe_size_greater_than(struct packing_data
*pack
,
436 const struct object_entry
*lhs
,
440 return lhs
->size_
> rhs
;
441 if (rhs
< pack
->oe_size_limit
) /* rhs < 2^x <= lhs ? */
443 return oe_get_size_slow(pack
, lhs
) > rhs
;
446 /* Return 0 if we will bust the pack-size limit */
447 static unsigned long write_no_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
448 unsigned long limit
, int usable_delta
)
450 unsigned long size
, datalen
;
451 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
452 dheader
[MAX_PACK_OBJECT_HEADER
];
454 enum object_type type
;
456 struct git_istream
*st
= NULL
;
457 const unsigned hashsz
= the_hash_algo
->rawsz
;
460 if (oe_type(entry
) == OBJ_BLOB
&&
461 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
) &&
462 (st
= open_istream(the_repository
, &entry
->idx
.oid
, &type
,
463 &size
, NULL
)) != NULL
)
466 buf
= repo_read_object_file(the_repository
,
467 &entry
->idx
.oid
, &type
,
470 die(_("unable to read %s"),
471 oid_to_hex(&entry
->idx
.oid
));
474 * make sure no cached delta data remains from a
475 * previous attempt before a pack split occurred.
477 FREE_AND_NULL(entry
->delta_data
);
478 entry
->z_delta_size
= 0;
479 } else if (entry
->delta_data
) {
480 size
= DELTA_SIZE(entry
);
481 buf
= entry
->delta_data
;
482 entry
->delta_data
= NULL
;
483 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
484 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
486 buf
= get_delta(entry
);
487 size
= DELTA_SIZE(entry
);
488 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
489 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
492 if (st
) /* large blob case, just assume we don't compress well */
494 else if (entry
->z_delta_size
)
495 datalen
= entry
->z_delta_size
;
497 datalen
= do_compress(&buf
, size
);
500 * The object header is a byte of 'type' followed by zero or
501 * more bytes of length.
503 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
506 if (type
== OBJ_OFS_DELTA
) {
508 * Deltas with relative base contain an additional
509 * encoding of the relative offset for the delta
510 * base from this object's position in the pack.
512 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
513 unsigned pos
= sizeof(dheader
) - 1;
514 dheader
[pos
] = ofs
& 127;
516 dheader
[--pos
] = 128 | (--ofs
& 127);
517 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
523 hashwrite(f
, header
, hdrlen
);
524 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
525 hdrlen
+= sizeof(dheader
) - pos
;
526 } else if (type
== OBJ_REF_DELTA
) {
528 * Deltas with a base reference contain
529 * additional bytes for the base object ID.
531 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
537 hashwrite(f
, header
, hdrlen
);
538 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
541 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
547 hashwrite(f
, header
, hdrlen
);
550 datalen
= write_large_blob_data(st
, f
, &entry
->idx
.oid
);
553 hashwrite(f
, buf
, datalen
);
557 return hdrlen
+ datalen
;
560 /* Return 0 if we will bust the pack-size limit */
561 static off_t
write_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
562 unsigned long limit
, int usable_delta
)
564 struct packed_git
*p
= IN_PACK(entry
);
565 struct pack_window
*w_curs
= NULL
;
568 enum object_type type
= oe_type(entry
);
570 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
571 dheader
[MAX_PACK_OBJECT_HEADER
];
573 const unsigned hashsz
= the_hash_algo
->rawsz
;
574 unsigned long entry_size
= SIZE(entry
);
577 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
578 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
579 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
582 offset
= entry
->in_pack_offset
;
583 if (offset_to_pack_pos(p
, offset
, &pos
) < 0)
584 die(_("write_reuse_object: could not locate %s, expected at "
585 "offset %"PRIuMAX
" in pack %s"),
586 oid_to_hex(&entry
->idx
.oid
), (uintmax_t)offset
,
588 datalen
= pack_pos_to_offset(p
, pos
+ 1) - offset
;
589 if (!pack_to_stdout
&& p
->index_version
> 1 &&
590 check_pack_crc(p
, &w_curs
, offset
, datalen
,
591 pack_pos_to_index(p
, pos
))) {
592 error(_("bad packed object CRC for %s"),
593 oid_to_hex(&entry
->idx
.oid
));
595 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
598 offset
+= entry
->in_pack_header_size
;
599 datalen
-= entry
->in_pack_header_size
;
601 if (!pack_to_stdout
&& p
->index_version
== 1 &&
602 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry_size
)) {
603 error(_("corrupt packed object for %s"),
604 oid_to_hex(&entry
->idx
.oid
));
606 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
609 if (type
== OBJ_OFS_DELTA
) {
610 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
611 unsigned pos
= sizeof(dheader
) - 1;
612 dheader
[pos
] = ofs
& 127;
614 dheader
[--pos
] = 128 | (--ofs
& 127);
615 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
619 hashwrite(f
, header
, hdrlen
);
620 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
621 hdrlen
+= sizeof(dheader
) - pos
;
623 } else if (type
== OBJ_REF_DELTA
) {
624 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
628 hashwrite(f
, header
, hdrlen
);
629 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
633 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
637 hashwrite(f
, header
, hdrlen
);
639 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
642 return hdrlen
+ datalen
;
645 /* Return 0 if we will bust the pack-size limit */
646 static off_t
write_object(struct hashfile
*f
,
647 struct object_entry
*entry
,
652 int usable_delta
, to_reuse
;
657 /* apply size limit if limited packsize and not first object */
658 if (!pack_size_limit
|| !nr_written
)
660 else if (pack_size_limit
<= write_offset
)
662 * the earlier object did not fit the limit; avoid
663 * mistaking this with unlimited (i.e. limit = 0).
667 limit
= pack_size_limit
- write_offset
;
670 usable_delta
= 0; /* no delta */
671 else if (!pack_size_limit
)
672 usable_delta
= 1; /* unlimited packfile */
673 else if (DELTA(entry
)->idx
.offset
== (off_t
)-1)
674 usable_delta
= 0; /* base was written to another pack */
675 else if (DELTA(entry
)->idx
.offset
)
676 usable_delta
= 1; /* base already exists in this pack */
678 usable_delta
= 0; /* base could end up in another pack */
681 to_reuse
= 0; /* explicit */
682 else if (!IN_PACK(entry
))
683 to_reuse
= 0; /* can't reuse what we don't have */
684 else if (oe_type(entry
) == OBJ_REF_DELTA
||
685 oe_type(entry
) == OBJ_OFS_DELTA
)
686 /* check_object() decided it for us ... */
687 to_reuse
= usable_delta
;
688 /* ... but pack split may override that */
689 else if (oe_type(entry
) != entry
->in_pack_type
)
690 to_reuse
= 0; /* pack has delta which is unusable */
691 else if (DELTA(entry
))
692 to_reuse
= 0; /* we want to pack afresh */
694 to_reuse
= 1; /* we have it in-pack undeltified,
695 * and we do not need to deltify it.
699 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
701 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
709 entry
->idx
.crc32
= crc32_end(f
);
713 enum write_one_status
{
714 WRITE_ONE_SKIP
= -1, /* already written */
715 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
716 WRITE_ONE_WRITTEN
= 1, /* normal */
717 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
720 static enum write_one_status
write_one(struct hashfile
*f
,
721 struct object_entry
*e
,
728 * we set offset to 1 (which is an impossible value) to mark
729 * the fact that this object is involved in "write its base
730 * first before writing a deltified object" recursion.
732 recursing
= (e
->idx
.offset
== 1);
734 warning(_("recursive delta detected for object %s"),
735 oid_to_hex(&e
->idx
.oid
));
736 return WRITE_ONE_RECURSIVE
;
737 } else if (e
->idx
.offset
|| e
->preferred_base
) {
738 /* offset is non zero if object is written already. */
739 return WRITE_ONE_SKIP
;
742 /* if we are deltified, write out base object first. */
744 e
->idx
.offset
= 1; /* now recurse */
745 switch (write_one(f
, DELTA(e
), offset
)) {
746 case WRITE_ONE_RECURSIVE
:
747 /* we cannot depend on this one */
752 case WRITE_ONE_BREAK
:
753 e
->idx
.offset
= recursing
;
754 return WRITE_ONE_BREAK
;
758 e
->idx
.offset
= *offset
;
759 size
= write_object(f
, e
, *offset
);
761 e
->idx
.offset
= recursing
;
762 return WRITE_ONE_BREAK
;
764 written_list
[nr_written
++] = &e
->idx
;
766 /* make sure off_t is sufficiently large not to wrap */
767 if (signed_add_overflows(*offset
, size
))
768 die(_("pack too large for current definition of off_t"));
770 return WRITE_ONE_WRITTEN
;
773 static int mark_tagged(const char *path UNUSED
, const struct object_id
*oid
,
774 int flag UNUSED
, void *cb_data UNUSED
)
776 struct object_id peeled
;
777 struct object_entry
*entry
= packlist_find(&to_pack
, oid
);
781 if (!peel_iterated_oid(oid
, &peeled
)) {
782 entry
= packlist_find(&to_pack
, &peeled
);
789 static inline unsigned char oe_layer(struct packing_data
*pack
,
790 struct object_entry
*e
)
794 return pack
->layer
[e
- pack
->objects
];
797 static inline void add_to_write_order(struct object_entry
**wo
,
799 struct object_entry
*e
)
801 if (e
->filled
|| oe_layer(&to_pack
, e
) != write_layer
)
807 static void add_descendants_to_write_order(struct object_entry
**wo
,
809 struct object_entry
*e
)
811 int add_to_order
= 1;
814 struct object_entry
*s
;
815 /* add this node... */
816 add_to_write_order(wo
, endp
, e
);
817 /* all its siblings... */
818 for (s
= DELTA_SIBLING(e
); s
; s
= DELTA_SIBLING(s
)) {
819 add_to_write_order(wo
, endp
, s
);
822 /* drop down a level to add left subtree nodes if possible */
823 if (DELTA_CHILD(e
)) {
828 /* our sibling might have some children, it is next */
829 if (DELTA_SIBLING(e
)) {
830 e
= DELTA_SIBLING(e
);
833 /* go back to our parent node */
835 while (e
&& !DELTA_SIBLING(e
)) {
836 /* we're on the right side of a subtree, keep
837 * going up until we can go right again */
841 /* done- we hit our original root node */
844 /* pass it off to sibling at this level */
845 e
= DELTA_SIBLING(e
);
850 static void add_family_to_write_order(struct object_entry
**wo
,
852 struct object_entry
*e
)
854 struct object_entry
*root
;
856 for (root
= e
; DELTA(root
); root
= DELTA(root
))
858 add_descendants_to_write_order(wo
, endp
, root
);
861 static void compute_layer_order(struct object_entry
**wo
, unsigned int *wo_end
)
863 unsigned int i
, last_untagged
;
864 struct object_entry
*objects
= to_pack
.objects
;
866 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
867 if (objects
[i
].tagged
)
869 add_to_write_order(wo
, wo_end
, &objects
[i
]);
874 * Then fill all the tagged tips.
876 for (; i
< to_pack
.nr_objects
; i
++) {
877 if (objects
[i
].tagged
)
878 add_to_write_order(wo
, wo_end
, &objects
[i
]);
882 * And then all remaining commits and tags.
884 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
885 if (oe_type(&objects
[i
]) != OBJ_COMMIT
&&
886 oe_type(&objects
[i
]) != OBJ_TAG
)
888 add_to_write_order(wo
, wo_end
, &objects
[i
]);
892 * And then all the trees.
894 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
895 if (oe_type(&objects
[i
]) != OBJ_TREE
)
897 add_to_write_order(wo
, wo_end
, &objects
[i
]);
901 * Finally all the rest in really tight order
903 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
904 if (!objects
[i
].filled
&& oe_layer(&to_pack
, &objects
[i
]) == write_layer
)
905 add_family_to_write_order(wo
, wo_end
, &objects
[i
]);
909 static struct object_entry
**compute_write_order(void)
911 uint32_t max_layers
= 1;
912 unsigned int i
, wo_end
;
914 struct object_entry
**wo
;
915 struct object_entry
*objects
= to_pack
.objects
;
917 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
918 objects
[i
].tagged
= 0;
919 objects
[i
].filled
= 0;
920 SET_DELTA_CHILD(&objects
[i
], NULL
);
921 SET_DELTA_SIBLING(&objects
[i
], NULL
);
925 * Fully connect delta_child/delta_sibling network.
926 * Make sure delta_sibling is sorted in the original
929 for (i
= to_pack
.nr_objects
; i
> 0;) {
930 struct object_entry
*e
= &objects
[--i
];
933 /* Mark me as the first child */
934 e
->delta_sibling_idx
= DELTA(e
)->delta_child_idx
;
935 SET_DELTA_CHILD(DELTA(e
), e
);
939 * Mark objects that are at the tip of tags.
941 for_each_tag_ref(mark_tagged
, NULL
);
943 if (use_delta_islands
) {
944 max_layers
= compute_pack_layers(&to_pack
);
948 ALLOC_ARRAY(wo
, to_pack
.nr_objects
);
951 for (; write_layer
< max_layers
; ++write_layer
)
952 compute_layer_order(wo
, &wo_end
);
954 if (wo_end
!= to_pack
.nr_objects
)
955 die(_("ordered %u objects, expected %"PRIu32
),
956 wo_end
, to_pack
.nr_objects
);
963 * A reused set of objects. All objects in a chunk have the same
964 * relative position in the original packfile and the generated
968 static struct reused_chunk
{
969 /* The offset of the first object of this chunk in the original
972 /* The difference for "original" minus the offset of the first object of
973 * this chunk in the generated packfile. */
976 static int reused_chunks_nr
;
977 static int reused_chunks_alloc
;
979 static void record_reused_object(off_t where
, off_t offset
)
981 if (reused_chunks_nr
&& reused_chunks
[reused_chunks_nr
-1].difference
== offset
)
984 ALLOC_GROW(reused_chunks
, reused_chunks_nr
+ 1,
985 reused_chunks_alloc
);
986 reused_chunks
[reused_chunks_nr
].original
= where
;
987 reused_chunks
[reused_chunks_nr
].difference
= offset
;
992 * Binary search to find the chunk that "where" is in. Note
993 * that we're not looking for an exact match, just the first
994 * chunk that contains it (which implicitly ends at the start
997 static off_t
find_reused_offset(off_t where
)
999 int lo
= 0, hi
= reused_chunks_nr
;
1001 int mi
= lo
+ ((hi
- lo
) / 2);
1002 if (where
== reused_chunks
[mi
].original
)
1003 return reused_chunks
[mi
].difference
;
1004 if (where
< reused_chunks
[mi
].original
)
1011 * The first chunk starts at zero, so we can't have gone below
1015 return reused_chunks
[lo
-1].difference
;
1018 static void write_reused_pack_one(size_t pos
, struct hashfile
*out
,
1019 struct pack_window
**w_curs
)
1021 off_t offset
, next
, cur
;
1022 enum object_type type
;
1025 offset
= pack_pos_to_offset(reuse_packfile
, pos
);
1026 next
= pack_pos_to_offset(reuse_packfile
, pos
+ 1);
1028 record_reused_object(offset
, offset
- hashfile_total(out
));
1031 type
= unpack_object_header(reuse_packfile
, w_curs
, &cur
, &size
);
1034 if (type
== OBJ_OFS_DELTA
) {
1038 unsigned char header
[MAX_PACK_OBJECT_HEADER
];
1041 base_offset
= get_delta_base(reuse_packfile
, w_curs
, &cur
, type
, offset
);
1042 assert(base_offset
!= 0);
1044 /* Convert to REF_DELTA if we must... */
1045 if (!allow_ofs_delta
) {
1047 struct object_id base_oid
;
1049 if (offset_to_pack_pos(reuse_packfile
, base_offset
, &base_pos
) < 0)
1050 die(_("expected object at offset %"PRIuMAX
" "
1052 (uintmax_t)base_offset
,
1053 reuse_packfile
->pack_name
);
1055 nth_packed_object_id(&base_oid
, reuse_packfile
,
1056 pack_pos_to_index(reuse_packfile
, base_pos
));
1058 len
= encode_in_pack_object_header(header
, sizeof(header
),
1059 OBJ_REF_DELTA
, size
);
1060 hashwrite(out
, header
, len
);
1061 hashwrite(out
, base_oid
.hash
, the_hash_algo
->rawsz
);
1062 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
1066 /* Otherwise see if we need to rewrite the offset... */
1067 fixup
= find_reused_offset(offset
) -
1068 find_reused_offset(base_offset
);
1070 unsigned char ofs_header
[10];
1071 unsigned i
, ofs_len
;
1072 off_t ofs
= offset
- base_offset
- fixup
;
1074 len
= encode_in_pack_object_header(header
, sizeof(header
),
1075 OBJ_OFS_DELTA
, size
);
1077 i
= sizeof(ofs_header
) - 1;
1078 ofs_header
[i
] = ofs
& 127;
1080 ofs_header
[--i
] = 128 | (--ofs
& 127);
1082 ofs_len
= sizeof(ofs_header
) - i
;
1084 hashwrite(out
, header
, len
);
1085 hashwrite(out
, ofs_header
+ sizeof(ofs_header
) - ofs_len
, ofs_len
);
1086 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
1090 /* ...otherwise we have no fixup, and can write it verbatim */
1093 copy_pack_data(out
, reuse_packfile
, w_curs
, offset
, next
- offset
);
1096 static size_t write_reused_pack_verbatim(struct hashfile
*out
,
1097 struct pack_window
**w_curs
)
1101 while (pos
< reuse_packfile_bitmap
->word_alloc
&&
1102 reuse_packfile_bitmap
->words
[pos
] == (eword_t
)~0)
1108 written
= (pos
* BITS_IN_EWORD
);
1109 to_write
= pack_pos_to_offset(reuse_packfile
, written
)
1110 - sizeof(struct pack_header
);
1112 /* We're recording one chunk, not one object. */
1113 record_reused_object(sizeof(struct pack_header
), 0);
1115 copy_pack_data(out
, reuse_packfile
, w_curs
,
1116 sizeof(struct pack_header
), to_write
);
1118 display_progress(progress_state
, written
);
1123 static void write_reused_pack(struct hashfile
*f
)
1127 struct pack_window
*w_curs
= NULL
;
1129 if (allow_ofs_delta
)
1130 i
= write_reused_pack_verbatim(f
, &w_curs
);
1132 for (; i
< reuse_packfile_bitmap
->word_alloc
; ++i
) {
1133 eword_t word
= reuse_packfile_bitmap
->words
[i
];
1134 size_t pos
= (i
* BITS_IN_EWORD
);
1136 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
1137 if ((word
>> offset
) == 0)
1140 offset
+= ewah_bit_ctz64(word
>> offset
);
1142 * Can use bit positions directly, even for MIDX
1143 * bitmaps. See comment in try_partial_reuse()
1146 write_reused_pack_one(pos
+ offset
, f
, &w_curs
);
1147 display_progress(progress_state
, ++written
);
1151 unuse_pack(&w_curs
);
1154 static void write_excluded_by_configs(void)
1156 struct oidset_iter iter
;
1157 const struct object_id
*oid
;
1159 oidset_iter_init(&excluded_by_config
, &iter
);
1160 while ((oid
= oidset_iter_next(&iter
))) {
1161 struct configured_exclusion
*ex
=
1162 oidmap_get(&configured_exclusions
, oid
);
1165 BUG("configured exclusion wasn't configured");
1166 write_in_full(1, ex
->pack_hash_hex
, strlen(ex
->pack_hash_hex
));
1167 write_in_full(1, " ", 1);
1168 write_in_full(1, ex
->uri
, strlen(ex
->uri
));
1169 write_in_full(1, "\n", 1);
1173 static const char no_split_warning
[] = N_(
1174 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
1177 static void write_pack_file(void)
1182 uint32_t nr_remaining
= nr_result
;
1183 time_t last_mtime
= 0;
1184 struct object_entry
**write_order
;
1186 if (progress
> pack_to_stdout
)
1187 progress_state
= start_progress(_("Writing objects"), nr_result
);
1188 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
1189 write_order
= compute_write_order();
1192 unsigned char hash
[GIT_MAX_RAWSZ
];
1193 char *pack_tmp_name
= NULL
;
1196 f
= hashfd_throughput(1, "<stdout>", progress_state
);
1198 f
= create_tmp_packfile(&pack_tmp_name
);
1200 offset
= write_pack_header(f
, nr_remaining
);
1202 if (reuse_packfile
) {
1203 assert(pack_to_stdout
);
1204 write_reused_pack(f
);
1205 offset
= hashfile_total(f
);
1209 for (; i
< to_pack
.nr_objects
; i
++) {
1210 struct object_entry
*e
= write_order
[i
];
1211 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
1213 display_progress(progress_state
, written
);
1216 if (pack_to_stdout
) {
1218 * We never fsync when writing to stdout since we may
1219 * not be writing to an actual pack file. For instance,
1220 * the upload-pack code passes a pipe here. Calling
1221 * fsync on a pipe results in unnecessary
1222 * synchronization with the reader on some platforms.
1224 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_NONE
,
1225 CSUM_HASH_IN_STREAM
| CSUM_CLOSE
);
1226 } else if (nr_written
== nr_remaining
) {
1227 finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
,
1228 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
1231 * If we wrote the wrong number of entries in the
1232 * header, rewrite it like in fast-import.
1235 int fd
= finalize_hashfile(f
, hash
, FSYNC_COMPONENT_PACK
, 0);
1236 fixup_pack_header_footer(fd
, hash
, pack_tmp_name
,
1237 nr_written
, hash
, offset
);
1239 if (write_bitmap_index
) {
1240 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1241 warning(_(no_split_warning
));
1242 write_bitmap_index
= 0;
1246 if (!pack_to_stdout
) {
1248 struct strbuf tmpname
= STRBUF_INIT
;
1249 char *idx_tmp_name
= NULL
;
1252 * Packs are runtime accessed in their mtime
1253 * order since newer packs are more likely to contain
1254 * younger objects. So if we are creating multiple
1255 * packs then we should modify the mtime of later ones
1256 * to preserve this property.
1258 if (stat(pack_tmp_name
, &st
) < 0) {
1259 warning_errno(_("failed to stat %s"), pack_tmp_name
);
1260 } else if (!last_mtime
) {
1261 last_mtime
= st
.st_mtime
;
1264 utb
.actime
= st
.st_atime
;
1265 utb
.modtime
= --last_mtime
;
1266 if (utime(pack_tmp_name
, &utb
) < 0)
1267 warning_errno(_("failed utime() on %s"), pack_tmp_name
);
1270 strbuf_addf(&tmpname
, "%s-%s.", base_name
,
1273 if (write_bitmap_index
) {
1274 bitmap_writer_set_checksum(hash
);
1275 bitmap_writer_build_type_index(
1276 &to_pack
, written_list
, nr_written
);
1280 pack_idx_opts
.flags
|= WRITE_MTIMES
;
1282 stage_tmp_packfiles(&tmpname
, pack_tmp_name
,
1283 written_list
, nr_written
,
1284 &to_pack
, &pack_idx_opts
, hash
,
1287 if (write_bitmap_index
) {
1288 size_t tmpname_len
= tmpname
.len
;
1290 strbuf_addstr(&tmpname
, "bitmap");
1291 stop_progress(&progress_state
);
1293 bitmap_writer_show_progress(progress
);
1294 bitmap_writer_select_commits(indexed_commits
, indexed_commits_nr
, -1);
1295 if (bitmap_writer_build(&to_pack
) < 0)
1296 die(_("failed to write bitmap index"));
1297 bitmap_writer_finish(written_list
, nr_written
,
1298 tmpname
.buf
, write_bitmap_options
);
1299 write_bitmap_index
= 0;
1300 strbuf_setlen(&tmpname
, tmpname_len
);
1303 rename_tmp_packfile_idx(&tmpname
, &idx_tmp_name
);
1306 strbuf_release(&tmpname
);
1307 free(pack_tmp_name
);
1308 puts(hash_to_hex(hash
));
1311 /* mark written objects as written to previous pack */
1312 for (j
= 0; j
< nr_written
; j
++) {
1313 written_list
[j
]->offset
= (off_t
)-1;
1315 nr_remaining
-= nr_written
;
1316 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
1320 stop_progress(&progress_state
);
1321 if (written
!= nr_result
)
1322 die(_("wrote %"PRIu32
" objects while expecting %"PRIu32
),
1323 written
, nr_result
);
1324 trace2_data_intmax("pack-objects", the_repository
,
1325 "write_pack_file/wrote", nr_result
);
1328 static int no_try_delta(const char *path
)
1330 static struct attr_check
*check
;
1333 check
= attr_check_initl("delta", NULL
);
1334 git_check_attr(the_repository
->index
, path
, check
);
1335 if (ATTR_FALSE(check
->items
[0].value
))
1341 * When adding an object, check whether we have already added it
1342 * to our packing list. If so, we can skip. However, if we are
1343 * being asked to excludei t, but the previous mention was to include
1344 * it, make sure to adjust its flags and tweak our numbers accordingly.
1346 * As an optimization, we pass out the index position where we would have
1347 * found the item, since that saves us from having to look it up again a
1348 * few lines later when we want to add the new entry.
1350 static int have_duplicate_entry(const struct object_id
*oid
,
1353 struct object_entry
*entry
;
1355 if (reuse_packfile_bitmap
&&
1356 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
))
1359 entry
= packlist_find(&to_pack
, oid
);
1364 if (!entry
->preferred_base
)
1366 entry
->preferred_base
= 1;
1372 static int want_found_object(const struct object_id
*oid
, int exclude
,
1373 struct packed_git
*p
)
1380 if (!is_pack_valid(p
))
1384 * When asked to do --local (do not include an object that appears in a
1385 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1386 * an object that appears in a pack marked with .keep), finding a pack
1387 * that matches the criteria is sufficient for us to decide to omit it.
1388 * However, even if this pack does not satisfy the criteria, we need to
1389 * make sure no copy of this object appears in _any_ pack that makes us
1390 * to omit the object, so we need to check all the packs.
1392 * We can however first check whether these options can possibly matter;
1393 * if they do not matter we know we want the object in generated pack.
1394 * Otherwise, we signal "-1" at the end to tell the caller that we do
1395 * not know either way, and it needs to check more packs.
1399 * Objects in packs borrowed from elsewhere are discarded regardless of
1400 * if they appear in other packs that weren't borrowed.
1402 if (local
&& !p
->pack_local
)
1406 * Then handle .keep first, as we have a fast(er) path there.
1408 if (ignore_packed_keep_on_disk
|| ignore_packed_keep_in_core
) {
1410 * Set the flags for the kept-pack cache to be the ones we want
1413 * That is, if we are ignoring objects in on-disk keep packs,
1414 * then we want to search through the on-disk keep and ignore
1418 if (ignore_packed_keep_on_disk
)
1419 flags
|= ON_DISK_KEEP_PACKS
;
1420 if (ignore_packed_keep_in_core
)
1421 flags
|= IN_CORE_KEEP_PACKS
;
1423 if (ignore_packed_keep_on_disk
&& p
->pack_keep
)
1425 if (ignore_packed_keep_in_core
&& p
->pack_keep_in_core
)
1427 if (has_object_kept_pack(oid
, flags
))
1432 * At this point we know definitively that either we don't care about
1433 * keep-packs, or the object is not in one. Keep checking other
1436 if (!local
|| !have_non_local_packs
)
1439 /* we don't know yet; keep looking for more packs */
1443 static int want_object_in_pack_one(struct packed_git
*p
,
1444 const struct object_id
*oid
,
1446 struct packed_git
**found_pack
,
1447 off_t
*found_offset
)
1451 if (p
== *found_pack
)
1452 offset
= *found_offset
;
1454 offset
= find_pack_entry_one(oid
->hash
, p
);
1458 if (!is_pack_valid(p
))
1460 *found_offset
= offset
;
1463 return want_found_object(oid
, exclude
, p
);
1469 * Check whether we want the object in the pack (e.g., we do not want
1470 * objects found in non-local stores if the "--local" option was used).
1472 * If the caller already knows an existing pack it wants to take the object
1473 * from, that is passed in *found_pack and *found_offset; otherwise this
1474 * function finds if there is any pack that has the object and returns the pack
1475 * and its offset in these variables.
1477 static int want_object_in_pack(const struct object_id
*oid
,
1479 struct packed_git
**found_pack
,
1480 off_t
*found_offset
)
1483 struct list_head
*pos
;
1484 struct multi_pack_index
*m
;
1486 if (!exclude
&& local
&& has_loose_object_nonlocal(oid
))
1490 * If we already know the pack object lives in, start checks from that
1491 * pack - in the usual case when neither --local was given nor .keep files
1492 * are present we will determine the answer right now.
1495 want
= want_found_object(oid
, exclude
, *found_pack
);
1503 for (m
= get_multi_pack_index(the_repository
); m
; m
= m
->next
) {
1504 struct pack_entry e
;
1505 if (fill_midx_entry(the_repository
, oid
, &e
, m
)) {
1506 want
= want_object_in_pack_one(e
.p
, oid
, exclude
, found_pack
, found_offset
);
1512 list_for_each(pos
, get_packed_git_mru(the_repository
)) {
1513 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1514 want
= want_object_in_pack_one(p
, oid
, exclude
, found_pack
, found_offset
);
1515 if (!exclude
&& want
> 0)
1517 get_packed_git_mru(the_repository
));
1522 if (uri_protocols
.nr
) {
1523 struct configured_exclusion
*ex
=
1524 oidmap_get(&configured_exclusions
, oid
);
1529 for (i
= 0; i
< uri_protocols
.nr
; i
++) {
1530 if (skip_prefix(ex
->uri
,
1531 uri_protocols
.items
[i
].string
,
1534 oidset_insert(&excluded_by_config
, oid
);
1544 static struct object_entry
*create_object_entry(const struct object_id
*oid
,
1545 enum object_type type
,
1549 struct packed_git
*found_pack
,
1552 struct object_entry
*entry
;
1554 entry
= packlist_alloc(&to_pack
, oid
);
1556 oe_set_type(entry
, type
);
1558 entry
->preferred_base
= 1;
1562 oe_set_in_pack(&to_pack
, entry
, found_pack
);
1563 entry
->in_pack_offset
= found_offset
;
1566 entry
->no_try_delta
= no_try_delta
;
1571 static const char no_closure_warning
[] = N_(
1572 "disabling bitmap writing, as some objects are not being packed"
1575 static int add_object_entry(const struct object_id
*oid
, enum object_type type
,
1576 const char *name
, int exclude
)
1578 struct packed_git
*found_pack
= NULL
;
1579 off_t found_offset
= 0;
1581 display_progress(progress_state
, ++nr_seen
);
1583 if (have_duplicate_entry(oid
, exclude
))
1586 if (!want_object_in_pack(oid
, exclude
, &found_pack
, &found_offset
)) {
1587 /* The pack is missing an object, so it will not have closure */
1588 if (write_bitmap_index
) {
1589 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1590 warning(_(no_closure_warning
));
1591 write_bitmap_index
= 0;
1596 create_object_entry(oid
, type
, pack_name_hash(name
),
1597 exclude
, name
&& no_try_delta(name
),
1598 found_pack
, found_offset
);
1602 static int add_object_entry_from_bitmap(const struct object_id
*oid
,
1603 enum object_type type
,
1604 int flags UNUSED
, uint32_t name_hash
,
1605 struct packed_git
*pack
, off_t offset
)
1607 display_progress(progress_state
, ++nr_seen
);
1609 if (have_duplicate_entry(oid
, 0))
1612 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
1615 create_object_entry(oid
, type
, name_hash
, 0, 0, pack
, offset
);
1619 struct pbase_tree_cache
{
1620 struct object_id oid
;
1624 unsigned long tree_size
;
1627 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1628 static int pbase_tree_cache_ix(const struct object_id
*oid
)
1630 return oid
->hash
[0] % ARRAY_SIZE(pbase_tree_cache
);
1632 static int pbase_tree_cache_ix_incr(int ix
)
1634 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1637 static struct pbase_tree
{
1638 struct pbase_tree
*next
;
1639 /* This is a phony "cache" entry; we are not
1640 * going to evict it or find it through _get()
1641 * mechanism -- this is for the toplevel node that
1642 * would almost always change with any commit.
1644 struct pbase_tree_cache pcache
;
1647 static struct pbase_tree_cache
*pbase_tree_get(const struct object_id
*oid
)
1649 struct pbase_tree_cache
*ent
, *nent
;
1652 enum object_type type
;
1654 int my_ix
= pbase_tree_cache_ix(oid
);
1655 int available_ix
= -1;
1657 /* pbase-tree-cache acts as a limited hashtable.
1658 * your object will be found at your index or within a few
1659 * slots after that slot if it is cached.
1661 for (neigh
= 0; neigh
< 8; neigh
++) {
1662 ent
= pbase_tree_cache
[my_ix
];
1663 if (ent
&& oideq(&ent
->oid
, oid
)) {
1667 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1668 ((0 <= available_ix
) &&
1669 (!ent
&& pbase_tree_cache
[available_ix
])))
1670 available_ix
= my_ix
;
1673 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1676 /* Did not find one. Either we got a bogus request or
1677 * we need to read and perhaps cache.
1679 data
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
1682 if (type
!= OBJ_TREE
) {
1687 /* We need to either cache or return a throwaway copy */
1689 if (available_ix
< 0)
1692 ent
= pbase_tree_cache
[available_ix
];
1693 my_ix
= available_ix
;
1697 nent
= xmalloc(sizeof(*nent
));
1698 nent
->temporary
= (available_ix
< 0);
1701 /* evict and reuse */
1702 free(ent
->tree_data
);
1705 oidcpy(&nent
->oid
, oid
);
1706 nent
->tree_data
= data
;
1707 nent
->tree_size
= size
;
1709 if (!nent
->temporary
)
1710 pbase_tree_cache
[my_ix
] = nent
;
1714 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1716 if (!cache
->temporary
) {
1720 free(cache
->tree_data
);
1724 static size_t name_cmp_len(const char *name
)
1726 return strcspn(name
, "\n/");
1729 static void add_pbase_object(struct tree_desc
*tree
,
1732 const char *fullname
)
1734 struct name_entry entry
;
1737 while (tree_entry(tree
,&entry
)) {
1738 if (S_ISGITLINK(entry
.mode
))
1740 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1741 memcmp(name
, entry
.path
, cmplen
);
1746 if (name
[cmplen
] != '/') {
1747 add_object_entry(&entry
.oid
,
1748 object_type(entry
.mode
),
1752 if (S_ISDIR(entry
.mode
)) {
1753 struct tree_desc sub
;
1754 struct pbase_tree_cache
*tree
;
1755 const char *down
= name
+cmplen
+1;
1756 size_t downlen
= name_cmp_len(down
);
1758 tree
= pbase_tree_get(&entry
.oid
);
1761 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1763 add_pbase_object(&sub
, down
, downlen
, fullname
);
1764 pbase_tree_put(tree
);
1769 static unsigned *done_pbase_paths
;
1770 static int done_pbase_paths_num
;
1771 static int done_pbase_paths_alloc
;
1772 static int done_pbase_path_pos(unsigned hash
)
1775 int hi
= done_pbase_paths_num
;
1777 int mi
= lo
+ (hi
- lo
) / 2;
1778 if (done_pbase_paths
[mi
] == hash
)
1780 if (done_pbase_paths
[mi
] < hash
)
1788 static int check_pbase_path(unsigned hash
)
1790 int pos
= done_pbase_path_pos(hash
);
1794 ALLOC_GROW(done_pbase_paths
,
1795 done_pbase_paths_num
+ 1,
1796 done_pbase_paths_alloc
);
1797 done_pbase_paths_num
++;
1798 if (pos
< done_pbase_paths_num
)
1799 MOVE_ARRAY(done_pbase_paths
+ pos
+ 1, done_pbase_paths
+ pos
,
1800 done_pbase_paths_num
- pos
- 1);
1801 done_pbase_paths
[pos
] = hash
;
1805 static void add_preferred_base_object(const char *name
)
1807 struct pbase_tree
*it
;
1809 unsigned hash
= pack_name_hash(name
);
1811 if (!num_preferred_base
|| check_pbase_path(hash
))
1814 cmplen
= name_cmp_len(name
);
1815 for (it
= pbase_tree
; it
; it
= it
->next
) {
1817 add_object_entry(&it
->pcache
.oid
, OBJ_TREE
, NULL
, 1);
1820 struct tree_desc tree
;
1821 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1822 add_pbase_object(&tree
, name
, cmplen
, name
);
1827 static void add_preferred_base(struct object_id
*oid
)
1829 struct pbase_tree
*it
;
1832 struct object_id tree_oid
;
1834 if (window
<= num_preferred_base
++)
1837 data
= read_object_with_reference(the_repository
, oid
,
1838 OBJ_TREE
, &size
, &tree_oid
);
1842 for (it
= pbase_tree
; it
; it
= it
->next
) {
1843 if (oideq(&it
->pcache
.oid
, &tree_oid
)) {
1849 CALLOC_ARRAY(it
, 1);
1850 it
->next
= pbase_tree
;
1853 oidcpy(&it
->pcache
.oid
, &tree_oid
);
1854 it
->pcache
.tree_data
= data
;
1855 it
->pcache
.tree_size
= size
;
1858 static void cleanup_preferred_base(void)
1860 struct pbase_tree
*it
;
1866 struct pbase_tree
*tmp
= it
;
1868 free(tmp
->pcache
.tree_data
);
1872 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1873 if (!pbase_tree_cache
[i
])
1875 free(pbase_tree_cache
[i
]->tree_data
);
1876 FREE_AND_NULL(pbase_tree_cache
[i
]);
1879 FREE_AND_NULL(done_pbase_paths
);
1880 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1884 * Return 1 iff the object specified by "delta" can be sent
1885 * literally as a delta against the base in "base_sha1". If
1886 * so, then *base_out will point to the entry in our packing
1887 * list, or NULL if we must use the external-base list.
1889 * Depth value does not matter - find_deltas() will
1890 * never consider reused delta as the base object to
1891 * deltify other objects against, in order to avoid
1894 static int can_reuse_delta(const struct object_id
*base_oid
,
1895 struct object_entry
*delta
,
1896 struct object_entry
**base_out
)
1898 struct object_entry
*base
;
1901 * First see if we're already sending the base (or it's explicitly in
1902 * our "excluded" list).
1904 base
= packlist_find(&to_pack
, base_oid
);
1906 if (!in_same_island(&delta
->idx
.oid
, &base
->idx
.oid
))
1913 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1914 * even if it was buried too deep in history to make it into the
1917 if (thin
&& bitmap_has_oid_in_uninteresting(bitmap_git
, base_oid
)) {
1918 if (use_delta_islands
) {
1919 if (!in_same_island(&delta
->idx
.oid
, base_oid
))
1929 static void prefetch_to_pack(uint32_t object_index_start
) {
1930 struct oid_array to_fetch
= OID_ARRAY_INIT
;
1933 for (i
= object_index_start
; i
< to_pack
.nr_objects
; i
++) {
1934 struct object_entry
*entry
= to_pack
.objects
+ i
;
1936 if (!oid_object_info_extended(the_repository
,
1939 OBJECT_INFO_FOR_PREFETCH
))
1941 oid_array_append(&to_fetch
, &entry
->idx
.oid
);
1943 promisor_remote_get_direct(the_repository
,
1944 to_fetch
.oid
, to_fetch
.nr
);
1945 oid_array_clear(&to_fetch
);
1948 static void check_object(struct object_entry
*entry
, uint32_t object_index
)
1950 unsigned long canonical_size
;
1951 enum object_type type
;
1952 struct object_info oi
= {.typep
= &type
, .sizep
= &canonical_size
};
1954 if (IN_PACK(entry
)) {
1955 struct packed_git
*p
= IN_PACK(entry
);
1956 struct pack_window
*w_curs
= NULL
;
1958 struct object_id base_ref
;
1959 struct object_entry
*base_entry
;
1960 unsigned long used
, used_0
;
1961 unsigned long avail
;
1963 unsigned char *buf
, c
;
1964 enum object_type type
;
1965 unsigned long in_pack_size
;
1967 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1970 * We want in_pack_type even if we do not reuse delta
1971 * since non-delta representations could still be reused.
1973 used
= unpack_object_header_buffer(buf
, avail
,
1980 BUG("invalid type %d", type
);
1981 entry
->in_pack_type
= type
;
1984 * Determine if this is a delta and if so whether we can
1985 * reuse it or not. Otherwise let's find out as cheaply as
1986 * possible what the actual type and size for this object is.
1988 switch (entry
->in_pack_type
) {
1990 /* Not a delta hence we've already got all we need. */
1991 oe_set_type(entry
, entry
->in_pack_type
);
1992 SET_SIZE(entry
, in_pack_size
);
1993 entry
->in_pack_header_size
= used
;
1994 if (oe_type(entry
) < OBJ_COMMIT
|| oe_type(entry
) > OBJ_BLOB
)
1996 unuse_pack(&w_curs
);
1999 if (reuse_delta
&& !entry
->preferred_base
) {
2001 use_pack(p
, &w_curs
,
2002 entry
->in_pack_offset
+ used
,
2006 entry
->in_pack_header_size
= used
+ the_hash_algo
->rawsz
;
2009 buf
= use_pack(p
, &w_curs
,
2010 entry
->in_pack_offset
+ used
, NULL
);
2016 if (!ofs
|| MSB(ofs
, 7)) {
2017 error(_("delta base offset overflow in pack for %s"),
2018 oid_to_hex(&entry
->idx
.oid
));
2022 ofs
= (ofs
<< 7) + (c
& 127);
2024 ofs
= entry
->in_pack_offset
- ofs
;
2025 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
2026 error(_("delta base offset out of bound for %s"),
2027 oid_to_hex(&entry
->idx
.oid
));
2030 if (reuse_delta
&& !entry
->preferred_base
) {
2032 if (offset_to_pack_pos(p
, ofs
, &pos
) < 0)
2034 if (!nth_packed_object_id(&base_ref
, p
,
2035 pack_pos_to_index(p
, pos
)))
2038 entry
->in_pack_header_size
= used
+ used_0
;
2043 can_reuse_delta(&base_ref
, entry
, &base_entry
)) {
2044 oe_set_type(entry
, entry
->in_pack_type
);
2045 SET_SIZE(entry
, in_pack_size
); /* delta size */
2046 SET_DELTA_SIZE(entry
, in_pack_size
);
2049 SET_DELTA(entry
, base_entry
);
2050 entry
->delta_sibling_idx
= base_entry
->delta_child_idx
;
2051 SET_DELTA_CHILD(base_entry
, entry
);
2053 SET_DELTA_EXT(entry
, &base_ref
);
2056 unuse_pack(&w_curs
);
2060 if (oe_type(entry
)) {
2064 * This must be a delta and we already know what the
2065 * final object type is. Let's extract the actual
2066 * object size from the delta header.
2068 delta_pos
= entry
->in_pack_offset
+ entry
->in_pack_header_size
;
2069 canonical_size
= get_size_from_delta(p
, &w_curs
, delta_pos
);
2070 if (canonical_size
== 0)
2072 SET_SIZE(entry
, canonical_size
);
2073 unuse_pack(&w_curs
);
2078 * No choice but to fall back to the recursive delta walk
2079 * with oid_object_info() to find about the object type
2083 unuse_pack(&w_curs
);
2086 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2087 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0) {
2088 if (repo_has_promisor_remote(the_repository
)) {
2089 prefetch_to_pack(object_index
);
2090 if (oid_object_info_extended(the_repository
, &entry
->idx
.oid
, &oi
,
2091 OBJECT_INFO_SKIP_FETCH_OBJECT
| OBJECT_INFO_LOOKUP_REPLACE
) < 0)
2097 oe_set_type(entry
, type
);
2098 if (entry
->type_valid
) {
2099 SET_SIZE(entry
, canonical_size
);
2102 * Bad object type is checked in prepare_pack(). This is
2103 * to permit a missing preferred base object to be ignored
2104 * as a preferred base. Doing so can result in a larger
2105 * pack file, but the transfer will still take place.
2110 static int pack_offset_sort(const void *_a
, const void *_b
)
2112 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2113 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2114 const struct packed_git
*a_in_pack
= IN_PACK(a
);
2115 const struct packed_git
*b_in_pack
= IN_PACK(b
);
2117 /* avoid filesystem trashing with loose objects */
2118 if (!a_in_pack
&& !b_in_pack
)
2119 return oidcmp(&a
->idx
.oid
, &b
->idx
.oid
);
2121 if (a_in_pack
< b_in_pack
)
2123 if (a_in_pack
> b_in_pack
)
2125 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
2126 (a
->in_pack_offset
> b
->in_pack_offset
);
2130 * Drop an on-disk delta we were planning to reuse. Naively, this would
2131 * just involve blanking out the "delta" field, but we have to deal
2132 * with some extra book-keeping:
2134 * 1. Removing ourselves from the delta_sibling linked list.
2136 * 2. Updating our size/type to the non-delta representation. These were
2137 * either not recorded initially (size) or overwritten with the delta type
2138 * (type) when check_object() decided to reuse the delta.
2140 * 3. Resetting our delta depth, as we are now a base object.
2142 static void drop_reused_delta(struct object_entry
*entry
)
2144 unsigned *idx
= &to_pack
.objects
[entry
->delta_idx
- 1].delta_child_idx
;
2145 struct object_info oi
= OBJECT_INFO_INIT
;
2146 enum object_type type
;
2150 struct object_entry
*oe
= &to_pack
.objects
[*idx
- 1];
2153 *idx
= oe
->delta_sibling_idx
;
2155 idx
= &oe
->delta_sibling_idx
;
2157 SET_DELTA(entry
, NULL
);
2162 if (packed_object_info(the_repository
, IN_PACK(entry
), entry
->in_pack_offset
, &oi
) < 0) {
2164 * We failed to get the info from this pack for some reason;
2165 * fall back to oid_object_info, which may find another copy.
2166 * And if that fails, the error will be recorded in oe_type(entry)
2167 * and dealt with in prepare_pack().
2170 oid_object_info(the_repository
, &entry
->idx
.oid
, &size
));
2172 oe_set_type(entry
, type
);
2174 SET_SIZE(entry
, size
);
2178 * Follow the chain of deltas from this entry onward, throwing away any links
2179 * that cause us to hit a cycle (as determined by the DFS state flags in
2182 * We also detect too-long reused chains that would violate our --depth
2185 static void break_delta_chains(struct object_entry
*entry
)
2188 * The actual depth of each object we will write is stored as an int,
2189 * as it cannot exceed our int "depth" limit. But before we break
2190 * changes based no that limit, we may potentially go as deep as the
2191 * number of objects, which is elsewhere bounded to a uint32_t.
2193 uint32_t total_depth
;
2194 struct object_entry
*cur
, *next
;
2196 for (cur
= entry
, total_depth
= 0;
2198 cur
= DELTA(cur
), total_depth
++) {
2199 if (cur
->dfs_state
== DFS_DONE
) {
2201 * We've already seen this object and know it isn't
2202 * part of a cycle. We do need to append its depth
2205 total_depth
+= cur
->depth
;
2210 * We break cycles before looping, so an ACTIVE state (or any
2211 * other cruft which made its way into the state variable)
2214 if (cur
->dfs_state
!= DFS_NONE
)
2215 BUG("confusing delta dfs state in first pass: %d",
2219 * Now we know this is the first time we've seen the object. If
2220 * it's not a delta, we're done traversing, but we'll mark it
2221 * done to save time on future traversals.
2224 cur
->dfs_state
= DFS_DONE
;
2229 * Mark ourselves as active and see if the next step causes
2230 * us to cycle to another active object. It's important to do
2231 * this _before_ we loop, because it impacts where we make the
2232 * cut, and thus how our total_depth counter works.
2233 * E.g., We may see a partial loop like:
2235 * A -> B -> C -> D -> B
2237 * Cutting B->C breaks the cycle. But now the depth of A is
2238 * only 1, and our total_depth counter is at 3. The size of the
2239 * error is always one less than the size of the cycle we
2240 * broke. Commits C and D were "lost" from A's chain.
2242 * If we instead cut D->B, then the depth of A is correct at 3.
2243 * We keep all commits in the chain that we examined.
2245 cur
->dfs_state
= DFS_ACTIVE
;
2246 if (DELTA(cur
)->dfs_state
== DFS_ACTIVE
) {
2247 drop_reused_delta(cur
);
2248 cur
->dfs_state
= DFS_DONE
;
2254 * And now that we've gone all the way to the bottom of the chain, we
2255 * need to clear the active flags and set the depth fields as
2256 * appropriate. Unlike the loop above, which can quit when it drops a
2257 * delta, we need to keep going to look for more depth cuts. So we need
2258 * an extra "next" pointer to keep going after we reset cur->delta.
2260 for (cur
= entry
; cur
; cur
= next
) {
2264 * We should have a chain of zero or more ACTIVE states down to
2265 * a final DONE. We can quit after the DONE, because either it
2266 * has no bases, or we've already handled them in a previous
2269 if (cur
->dfs_state
== DFS_DONE
)
2271 else if (cur
->dfs_state
!= DFS_ACTIVE
)
2272 BUG("confusing delta dfs state in second pass: %d",
2276 * If the total_depth is more than depth, then we need to snip
2277 * the chain into two or more smaller chains that don't exceed
2278 * the maximum depth. Most of the resulting chains will contain
2279 * (depth + 1) entries (i.e., depth deltas plus one base), and
2280 * the last chain (i.e., the one containing entry) will contain
2281 * whatever entries are left over, namely
2282 * (total_depth % (depth + 1)) of them.
2284 * Since we are iterating towards decreasing depth, we need to
2285 * decrement total_depth as we go, and we need to write to the
2286 * entry what its final depth will be after all of the
2287 * snipping. Since we're snipping into chains of length (depth
2288 * + 1) entries, the final depth of an entry will be its
2289 * original depth modulo (depth + 1). Any time we encounter an
2290 * entry whose final depth is supposed to be zero, we snip it
2291 * from its delta base, thereby making it so.
2293 cur
->depth
= (total_depth
--) % (depth
+ 1);
2295 drop_reused_delta(cur
);
2297 cur
->dfs_state
= DFS_DONE
;
2301 static void get_object_details(void)
2304 struct object_entry
**sorted_by_offset
;
2307 progress_state
= start_progress(_("Counting objects"),
2308 to_pack
.nr_objects
);
2310 CALLOC_ARRAY(sorted_by_offset
, to_pack
.nr_objects
);
2311 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2312 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
2313 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
2315 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2316 struct object_entry
*entry
= sorted_by_offset
[i
];
2317 check_object(entry
, i
);
2318 if (entry
->type_valid
&&
2319 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
))
2320 entry
->no_try_delta
= 1;
2321 display_progress(progress_state
, i
+ 1);
2323 stop_progress(&progress_state
);
2326 * This must happen in a second pass, since we rely on the delta
2327 * information for the whole list being completed.
2329 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2330 break_delta_chains(&to_pack
.objects
[i
]);
2332 free(sorted_by_offset
);
2336 * We search for deltas in a list sorted by type, by filename hash, and then
2337 * by size, so that we see progressively smaller and smaller files.
2338 * That's because we prefer deltas to be from the bigger file
2339 * to the smaller -- deletes are potentially cheaper, but perhaps
2340 * more importantly, the bigger file is likely the more recent
2341 * one. The deepest deltas are therefore the oldest objects which are
2342 * less susceptible to be accessed often.
2344 static int type_size_sort(const void *_a
, const void *_b
)
2346 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2347 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2348 const enum object_type a_type
= oe_type(a
);
2349 const enum object_type b_type
= oe_type(b
);
2350 const unsigned long a_size
= SIZE(a
);
2351 const unsigned long b_size
= SIZE(b
);
2353 if (a_type
> b_type
)
2355 if (a_type
< b_type
)
2357 if (a
->hash
> b
->hash
)
2359 if (a
->hash
< b
->hash
)
2361 if (a
->preferred_base
> b
->preferred_base
)
2363 if (a
->preferred_base
< b
->preferred_base
)
2365 if (use_delta_islands
) {
2366 const int island_cmp
= island_delta_cmp(&a
->idx
.oid
, &b
->idx
.oid
);
2370 if (a_size
> b_size
)
2372 if (a_size
< b_size
)
2374 return a
< b
? -1 : (a
> b
); /* newest first */
2378 struct object_entry
*entry
;
2380 struct delta_index
*index
;
2384 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
2385 unsigned long delta_size
)
2387 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
2390 if (delta_size
< cache_max_small_delta_size
)
2393 /* cache delta, if objects are large enough compared to delta size */
2394 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
2400 /* Protect delta_cache_size */
2401 static pthread_mutex_t cache_mutex
;
2402 #define cache_lock() pthread_mutex_lock(&cache_mutex)
2403 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2406 * Protect object list partitioning (e.g. struct thread_param) and
2409 static pthread_mutex_t progress_mutex
;
2410 #define progress_lock() pthread_mutex_lock(&progress_mutex)
2411 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2414 * Access to struct object_entry is unprotected since each thread owns
2415 * a portion of the main object list. Just don't access object entries
2416 * ahead in the list because they can be stolen and would need
2417 * progress_mutex for protection.
2420 static inline int oe_size_less_than(struct packing_data
*pack
,
2421 const struct object_entry
*lhs
,
2424 if (lhs
->size_valid
)
2425 return lhs
->size_
< rhs
;
2426 if (rhs
< pack
->oe_size_limit
) /* rhs < 2^x <= lhs ? */
2428 return oe_get_size_slow(pack
, lhs
) < rhs
;
2431 static inline void oe_set_tree_depth(struct packing_data
*pack
,
2432 struct object_entry
*e
,
2433 unsigned int tree_depth
)
2435 if (!pack
->tree_depth
)
2436 CALLOC_ARRAY(pack
->tree_depth
, pack
->nr_alloc
);
2437 pack
->tree_depth
[e
- pack
->objects
] = tree_depth
;
2441 * Return the size of the object without doing any delta
2442 * reconstruction (so non-deltas are true object sizes, but deltas
2443 * return the size of the delta data).
2445 unsigned long oe_get_size_slow(struct packing_data
*pack
,
2446 const struct object_entry
*e
)
2448 struct packed_git
*p
;
2449 struct pack_window
*w_curs
;
2451 enum object_type type
;
2452 unsigned long used
, avail
, size
;
2454 if (e
->type_
!= OBJ_OFS_DELTA
&& e
->type_
!= OBJ_REF_DELTA
) {
2455 packing_data_lock(&to_pack
);
2456 if (oid_object_info(the_repository
, &e
->idx
.oid
, &size
) < 0)
2457 die(_("unable to get size of %s"),
2458 oid_to_hex(&e
->idx
.oid
));
2459 packing_data_unlock(&to_pack
);
2463 p
= oe_in_pack(pack
, e
);
2465 BUG("when e->type is a delta, it must belong to a pack");
2467 packing_data_lock(&to_pack
);
2469 buf
= use_pack(p
, &w_curs
, e
->in_pack_offset
, &avail
);
2470 used
= unpack_object_header_buffer(buf
, avail
, &type
, &size
);
2472 die(_("unable to parse object header of %s"),
2473 oid_to_hex(&e
->idx
.oid
));
2475 unuse_pack(&w_curs
);
2476 packing_data_unlock(&to_pack
);
2480 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
2481 unsigned max_depth
, unsigned long *mem_usage
)
2483 struct object_entry
*trg_entry
= trg
->entry
;
2484 struct object_entry
*src_entry
= src
->entry
;
2485 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
2487 enum object_type type
;
2490 /* Don't bother doing diffs between different types */
2491 if (oe_type(trg_entry
) != oe_type(src_entry
))
2495 * We do not bother to try a delta that we discarded on an
2496 * earlier try, but only when reusing delta data. Note that
2497 * src_entry that is marked as the preferred_base should always
2498 * be considered, as even if we produce a suboptimal delta against
2499 * it, we will still save the transfer cost, as we already know
2500 * the other side has it and we won't send src_entry at all.
2502 if (reuse_delta
&& IN_PACK(trg_entry
) &&
2503 IN_PACK(trg_entry
) == IN_PACK(src_entry
) &&
2504 !src_entry
->preferred_base
&&
2505 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
2506 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
2509 /* Let's not bust the allowed depth. */
2510 if (src
->depth
>= max_depth
)
2513 /* Now some size filtering heuristics. */
2514 trg_size
= SIZE(trg_entry
);
2515 if (!DELTA(trg_entry
)) {
2516 max_size
= trg_size
/2 - the_hash_algo
->rawsz
;
2519 max_size
= DELTA_SIZE(trg_entry
);
2520 ref_depth
= trg
->depth
;
2522 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
2523 (max_depth
- ref_depth
+ 1);
2526 src_size
= SIZE(src_entry
);
2527 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
2528 if (sizediff
>= max_size
)
2530 if (trg_size
< src_size
/ 32)
2533 if (!in_same_island(&trg
->entry
->idx
.oid
, &src
->entry
->idx
.oid
))
2536 /* Load data if not already done */
2538 packing_data_lock(&to_pack
);
2539 trg
->data
= repo_read_object_file(the_repository
,
2540 &trg_entry
->idx
.oid
, &type
,
2542 packing_data_unlock(&to_pack
);
2544 die(_("object %s cannot be read"),
2545 oid_to_hex(&trg_entry
->idx
.oid
));
2547 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2548 oid_to_hex(&trg_entry
->idx
.oid
), (uintmax_t)sz
,
2549 (uintmax_t)trg_size
);
2553 packing_data_lock(&to_pack
);
2554 src
->data
= repo_read_object_file(the_repository
,
2555 &src_entry
->idx
.oid
, &type
,
2557 packing_data_unlock(&to_pack
);
2559 if (src_entry
->preferred_base
) {
2560 static int warned
= 0;
2562 warning(_("object %s cannot be read"),
2563 oid_to_hex(&src_entry
->idx
.oid
));
2565 * Those objects are not included in the
2566 * resulting pack. Be resilient and ignore
2567 * them if they can't be read, in case the
2568 * pack could be created nevertheless.
2572 die(_("object %s cannot be read"),
2573 oid_to_hex(&src_entry
->idx
.oid
));
2576 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2577 oid_to_hex(&src_entry
->idx
.oid
), (uintmax_t)sz
,
2578 (uintmax_t)src_size
);
2582 src
->index
= create_delta_index(src
->data
, src_size
);
2584 static int warned
= 0;
2586 warning(_("suboptimal pack - out of memory"));
2589 *mem_usage
+= sizeof_delta_index(src
->index
);
2592 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
2596 if (DELTA(trg_entry
)) {
2597 /* Prefer only shallower same-sized deltas. */
2598 if (delta_size
== DELTA_SIZE(trg_entry
) &&
2599 src
->depth
+ 1 >= trg
->depth
) {
2606 * Handle memory allocation outside of the cache
2607 * accounting lock. Compiler will optimize the strangeness
2608 * away when NO_PTHREADS is defined.
2610 free(trg_entry
->delta_data
);
2612 if (trg_entry
->delta_data
) {
2613 delta_cache_size
-= DELTA_SIZE(trg_entry
);
2614 trg_entry
->delta_data
= NULL
;
2616 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
2617 delta_cache_size
+= delta_size
;
2619 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
2625 SET_DELTA(trg_entry
, src_entry
);
2626 SET_DELTA_SIZE(trg_entry
, delta_size
);
2627 trg
->depth
= src
->depth
+ 1;
2632 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
2634 struct object_entry
*child
= DELTA_CHILD(me
);
2637 const unsigned int c
= check_delta_limit(child
, n
+ 1);
2640 child
= DELTA_SIBLING(child
);
2645 static unsigned long free_unpacked(struct unpacked
*n
)
2647 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
2648 free_delta_index(n
->index
);
2651 freed_mem
+= SIZE(n
->entry
);
2652 FREE_AND_NULL(n
->data
);
2659 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
2660 int window
, int depth
, unsigned *processed
)
2662 uint32_t i
, idx
= 0, count
= 0;
2663 struct unpacked
*array
;
2664 unsigned long mem_usage
= 0;
2666 CALLOC_ARRAY(array
, window
);
2669 struct object_entry
*entry
;
2670 struct unpacked
*n
= array
+ idx
;
2671 int j
, max_depth
, best_base
= -1;
2680 if (!entry
->preferred_base
) {
2682 display_progress(progress_state
, *processed
);
2686 mem_usage
-= free_unpacked(n
);
2689 while (window_memory_limit
&&
2690 mem_usage
> window_memory_limit
&&
2692 const uint32_t tail
= (idx
+ window
- count
) % window
;
2693 mem_usage
-= free_unpacked(array
+ tail
);
2697 /* We do not compute delta to *create* objects we are not
2700 if (entry
->preferred_base
)
2704 * If the current object is at pack edge, take the depth the
2705 * objects that depend on the current object into account
2706 * otherwise they would become too deep.
2709 if (DELTA_CHILD(entry
)) {
2710 max_depth
-= check_delta_limit(entry
, 0);
2718 uint32_t other_idx
= idx
+ j
;
2720 if (other_idx
>= window
)
2721 other_idx
-= window
;
2722 m
= array
+ other_idx
;
2725 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2729 best_base
= other_idx
;
2733 * If we decided to cache the delta data, then it is best
2734 * to compress it right away. First because we have to do
2735 * it anyway, and doing it here while we're threaded will
2736 * save a lot of time in the non threaded write phase,
2737 * as well as allow for caching more deltas within
2738 * the same cache size limit.
2740 * But only if not writing to stdout, since in that case
2741 * the network is most likely throttling writes anyway,
2742 * and therefore it is best to go to the write phase ASAP
2743 * instead, as we can afford spending more time compressing
2744 * between writes at that moment.
2746 if (entry
->delta_data
&& !pack_to_stdout
) {
2749 size
= do_compress(&entry
->delta_data
, DELTA_SIZE(entry
));
2750 if (size
< (1U << OE_Z_DELTA_BITS
)) {
2751 entry
->z_delta_size
= size
;
2753 delta_cache_size
-= DELTA_SIZE(entry
);
2754 delta_cache_size
+= entry
->z_delta_size
;
2757 FREE_AND_NULL(entry
->delta_data
);
2758 entry
->z_delta_size
= 0;
2762 /* if we made n a delta, and if n is already at max
2763 * depth, leaving it in the window is pointless. we
2764 * should evict it first.
2766 if (DELTA(entry
) && max_depth
<= n
->depth
)
2770 * Move the best delta base up in the window, after the
2771 * currently deltified object, to keep it longer. It will
2772 * be the first base object to be attempted next.
2775 struct unpacked swap
= array
[best_base
];
2776 int dist
= (window
+ idx
- best_base
) % window
;
2777 int dst
= best_base
;
2779 int src
= (dst
+ 1) % window
;
2780 array
[dst
] = array
[src
];
2788 if (count
+ 1 < window
)
2794 for (i
= 0; i
< window
; ++i
) {
2795 free_delta_index(array
[i
].index
);
2796 free(array
[i
].data
);
2802 * The main object list is split into smaller lists, each is handed to
2805 * The main thread waits on the condition that (at least) one of the workers
2806 * has stopped working (which is indicated in the .working member of
2807 * struct thread_params).
2809 * When a work thread has completed its work, it sets .working to 0 and
2810 * signals the main thread and waits on the condition that .data_ready
2813 * The main thread steals half of the work from the worker that has
2814 * most work left to hand it to the idle worker.
2817 struct thread_params
{
2819 struct object_entry
**list
;
2826 pthread_mutex_t mutex
;
2827 pthread_cond_t cond
;
2828 unsigned *processed
;
2831 static pthread_cond_t progress_cond
;
2834 * Mutex and conditional variable can't be statically-initialized on Windows.
2836 static void init_threaded_search(void)
2838 pthread_mutex_init(&cache_mutex
, NULL
);
2839 pthread_mutex_init(&progress_mutex
, NULL
);
2840 pthread_cond_init(&progress_cond
, NULL
);
2843 static void cleanup_threaded_search(void)
2845 pthread_cond_destroy(&progress_cond
);
2846 pthread_mutex_destroy(&cache_mutex
);
2847 pthread_mutex_destroy(&progress_mutex
);
2850 static void *threaded_find_deltas(void *arg
)
2852 struct thread_params
*me
= arg
;
2855 while (me
->remaining
) {
2858 find_deltas(me
->list
, &me
->remaining
,
2859 me
->window
, me
->depth
, me
->processed
);
2863 pthread_cond_signal(&progress_cond
);
2867 * We must not set ->data_ready before we wait on the
2868 * condition because the main thread may have set it to 1
2869 * before we get here. In order to be sure that new
2870 * work is available if we see 1 in ->data_ready, it
2871 * was initialized to 0 before this thread was spawned
2872 * and we reset it to 0 right away.
2874 pthread_mutex_lock(&me
->mutex
);
2875 while (!me
->data_ready
)
2876 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2878 pthread_mutex_unlock(&me
->mutex
);
2883 /* leave ->working 1 so that this doesn't get more work assigned */
2887 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2888 int window
, int depth
, unsigned *processed
)
2890 struct thread_params
*p
;
2891 int i
, ret
, active_threads
= 0;
2893 init_threaded_search();
2895 if (delta_search_threads
<= 1) {
2896 find_deltas(list
, &list_size
, window
, depth
, processed
);
2897 cleanup_threaded_search();
2900 if (progress
> pack_to_stdout
)
2901 fprintf_ln(stderr
, _("Delta compression using up to %d threads"),
2902 delta_search_threads
);
2903 CALLOC_ARRAY(p
, delta_search_threads
);
2905 /* Partition the work amongst work threads. */
2906 for (i
= 0; i
< delta_search_threads
; i
++) {
2907 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
2909 /* don't use too small segments or no deltas will be found */
2910 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
2913 p
[i
].window
= window
;
2915 p
[i
].processed
= processed
;
2917 p
[i
].data_ready
= 0;
2919 /* try to split chunks on "path" boundaries */
2920 while (sub_size
&& sub_size
< list_size
&&
2921 list
[sub_size
]->hash
&&
2922 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
2926 p
[i
].list_size
= sub_size
;
2927 p
[i
].remaining
= sub_size
;
2930 list_size
-= sub_size
;
2933 /* Start work threads. */
2934 for (i
= 0; i
< delta_search_threads
; i
++) {
2935 if (!p
[i
].list_size
)
2937 pthread_mutex_init(&p
[i
].mutex
, NULL
);
2938 pthread_cond_init(&p
[i
].cond
, NULL
);
2939 ret
= pthread_create(&p
[i
].thread
, NULL
,
2940 threaded_find_deltas
, &p
[i
]);
2942 die(_("unable to create thread: %s"), strerror(ret
));
2947 * Now let's wait for work completion. Each time a thread is done
2948 * with its work, we steal half of the remaining work from the
2949 * thread with the largest number of unprocessed objects and give
2950 * it to that newly idle thread. This ensure good load balancing
2951 * until the remaining object list segments are simply too short
2952 * to be worth splitting anymore.
2954 while (active_threads
) {
2955 struct thread_params
*target
= NULL
;
2956 struct thread_params
*victim
= NULL
;
2957 unsigned sub_size
= 0;
2961 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
2966 pthread_cond_wait(&progress_cond
, &progress_mutex
);
2969 for (i
= 0; i
< delta_search_threads
; i
++)
2970 if (p
[i
].remaining
> 2*window
&&
2971 (!victim
|| victim
->remaining
< p
[i
].remaining
))
2974 sub_size
= victim
->remaining
/ 2;
2975 list
= victim
->list
+ victim
->list_size
- sub_size
;
2976 while (sub_size
&& list
[0]->hash
&&
2977 list
[0]->hash
== list
[-1]->hash
) {
2983 * It is possible for some "paths" to have
2984 * so many objects that no hash boundary
2985 * might be found. Let's just steal the
2986 * exact half in that case.
2988 sub_size
= victim
->remaining
/ 2;
2991 target
->list
= list
;
2992 victim
->list_size
-= sub_size
;
2993 victim
->remaining
-= sub_size
;
2995 target
->list_size
= sub_size
;
2996 target
->remaining
= sub_size
;
2997 target
->working
= 1;
3000 pthread_mutex_lock(&target
->mutex
);
3001 target
->data_ready
= 1;
3002 pthread_cond_signal(&target
->cond
);
3003 pthread_mutex_unlock(&target
->mutex
);
3006 pthread_join(target
->thread
, NULL
);
3007 pthread_cond_destroy(&target
->cond
);
3008 pthread_mutex_destroy(&target
->mutex
);
3012 cleanup_threaded_search();
3016 static int obj_is_packed(const struct object_id
*oid
)
3018 return packlist_find(&to_pack
, oid
) ||
3019 (reuse_packfile_bitmap
&&
3020 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
));
3023 static void add_tag_chain(const struct object_id
*oid
)
3028 * We catch duplicates already in add_object_entry(), but we'd
3029 * prefer to do this extra check to avoid having to parse the
3030 * tag at all if we already know that it's being packed (e.g., if
3031 * it was included via bitmaps, we would not have parsed it
3034 if (obj_is_packed(oid
))
3037 tag
= lookup_tag(the_repository
, oid
);
3039 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
3040 die(_("unable to pack objects reachable from tag %s"),
3043 add_object_entry(&tag
->object
.oid
, OBJ_TAG
, NULL
, 0);
3045 if (tag
->tagged
->type
!= OBJ_TAG
)
3048 tag
= (struct tag
*)tag
->tagged
;
3052 static int add_ref_tag(const char *tag UNUSED
, const struct object_id
*oid
,
3053 int flag UNUSED
, void *cb_data UNUSED
)
3055 struct object_id peeled
;
3057 if (!peel_iterated_oid(oid
, &peeled
) && obj_is_packed(&peeled
))
3062 static void prepare_pack(int window
, int depth
)
3064 struct object_entry
**delta_list
;
3065 uint32_t i
, nr_deltas
;
3068 if (use_delta_islands
)
3069 resolve_tree_islands(the_repository
, progress
, &to_pack
);
3071 get_object_details();
3074 * If we're locally repacking then we need to be doubly careful
3075 * from now on in order to make sure no stealth corruption gets
3076 * propagated to the new pack. Clients receiving streamed packs
3077 * should validate everything they get anyway so no need to incur
3078 * the additional cost here in that case.
3080 if (!pack_to_stdout
)
3081 do_check_packed_object_crc
= 1;
3083 if (!to_pack
.nr_objects
|| !window
|| !depth
)
3086 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
3089 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
3090 struct object_entry
*entry
= to_pack
.objects
+ i
;
3093 /* This happens if we decided to reuse existing
3094 * delta from a pack. "reuse_delta &&" is implied.
3098 if (!entry
->type_valid
||
3099 oe_size_less_than(&to_pack
, entry
, 50))
3102 if (entry
->no_try_delta
)
3105 if (!entry
->preferred_base
) {
3107 if (oe_type(entry
) < 0)
3108 die(_("unable to get type of object %s"),
3109 oid_to_hex(&entry
->idx
.oid
));
3111 if (oe_type(entry
) < 0) {
3113 * This object is not found, but we
3114 * don't have to include it anyway.
3120 delta_list
[n
++] = entry
;
3123 if (nr_deltas
&& n
> 1) {
3124 unsigned nr_done
= 0;
3127 progress_state
= start_progress(_("Compressing objects"),
3129 QSORT(delta_list
, n
, type_size_sort
);
3130 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
3131 stop_progress(&progress_state
);
3132 if (nr_done
!= nr_deltas
)
3133 die(_("inconsistency with delta count"));
3138 static int git_pack_config(const char *k
, const char *v
,
3139 const struct config_context
*ctx
, void *cb
)
3141 if (!strcmp(k
, "pack.window")) {
3142 window
= git_config_int(k
, v
, ctx
->kvi
);
3145 if (!strcmp(k
, "pack.windowmemory")) {
3146 window_memory_limit
= git_config_ulong(k
, v
, ctx
->kvi
);
3149 if (!strcmp(k
, "pack.depth")) {
3150 depth
= git_config_int(k
, v
, ctx
->kvi
);
3153 if (!strcmp(k
, "pack.deltacachesize")) {
3154 max_delta_cache_size
= git_config_int(k
, v
, ctx
->kvi
);
3157 if (!strcmp(k
, "pack.deltacachelimit")) {
3158 cache_max_small_delta_size
= git_config_int(k
, v
, ctx
->kvi
);
3161 if (!strcmp(k
, "pack.writebitmaphashcache")) {
3162 if (git_config_bool(k
, v
))
3163 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
3165 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
3168 if (!strcmp(k
, "pack.writebitmaplookuptable")) {
3169 if (git_config_bool(k
, v
))
3170 write_bitmap_options
|= BITMAP_OPT_LOOKUP_TABLE
;
3172 write_bitmap_options
&= ~BITMAP_OPT_LOOKUP_TABLE
;
3175 if (!strcmp(k
, "pack.usebitmaps")) {
3176 use_bitmap_index_default
= git_config_bool(k
, v
);
3179 if (!strcmp(k
, "pack.allowpackreuse")) {
3180 allow_pack_reuse
= git_config_bool(k
, v
);
3183 if (!strcmp(k
, "pack.threads")) {
3184 delta_search_threads
= git_config_int(k
, v
, ctx
->kvi
);
3185 if (delta_search_threads
< 0)
3186 die(_("invalid number of threads specified (%d)"),
3187 delta_search_threads
);
3188 if (!HAVE_THREADS
&& delta_search_threads
!= 1) {
3189 warning(_("no threads support, ignoring %s"), k
);
3190 delta_search_threads
= 0;
3194 if (!strcmp(k
, "pack.indexversion")) {
3195 pack_idx_opts
.version
= git_config_int(k
, v
, ctx
->kvi
);
3196 if (pack_idx_opts
.version
> 2)
3197 die(_("bad pack.indexVersion=%"PRIu32
),
3198 pack_idx_opts
.version
);
3201 if (!strcmp(k
, "pack.writereverseindex")) {
3202 if (git_config_bool(k
, v
))
3203 pack_idx_opts
.flags
|= WRITE_REV
;
3205 pack_idx_opts
.flags
&= ~WRITE_REV
;
3208 if (!strcmp(k
, "uploadpack.blobpackfileuri")) {
3209 struct configured_exclusion
*ex
= xmalloc(sizeof(*ex
));
3210 const char *oid_end
, *pack_end
;
3212 * Stores the pack hash. This is not a true object ID, but is
3215 struct object_id pack_hash
;
3217 if (parse_oid_hex(v
, &ex
->e
.oid
, &oid_end
) ||
3219 parse_oid_hex(oid_end
+ 1, &pack_hash
, &pack_end
) ||
3221 die(_("value of uploadpack.blobpackfileuri must be "
3222 "of the form '<object-hash> <pack-hash> <uri>' (got '%s')"), v
);
3223 if (oidmap_get(&configured_exclusions
, &ex
->e
.oid
))
3224 die(_("object already configured in another "
3225 "uploadpack.blobpackfileuri (got '%s')"), v
);
3226 ex
->pack_hash_hex
= xcalloc(1, pack_end
- oid_end
);
3227 memcpy(ex
->pack_hash_hex
, oid_end
+ 1, pack_end
- oid_end
- 1);
3228 ex
->uri
= xstrdup(pack_end
+ 1);
3229 oidmap_put(&configured_exclusions
, ex
);
3231 return git_default_config(k
, v
, ctx
, cb
);
3234 /* Counters for trace2 output when in --stdin-packs mode. */
3235 static int stdin_packs_found_nr
;
3236 static int stdin_packs_hints_nr
;
3238 static int add_object_entry_from_pack(const struct object_id
*oid
,
3239 struct packed_git
*p
,
3244 enum object_type type
= OBJ_NONE
;
3246 display_progress(progress_state
, ++nr_seen
);
3248 if (have_duplicate_entry(oid
, 0))
3251 ofs
= nth_packed_object_offset(p
, pos
);
3252 if (!want_object_in_pack(oid
, 0, &p
, &ofs
))
3256 struct rev_info
*revs
= _data
;
3257 struct object_info oi
= OBJECT_INFO_INIT
;
3260 if (packed_object_info(the_repository
, p
, ofs
, &oi
) < 0) {
3261 die(_("could not get type of object %s in pack %s"),
3262 oid_to_hex(oid
), p
->pack_name
);
3263 } else if (type
== OBJ_COMMIT
) {
3265 * commits in included packs are used as starting points for the
3266 * subsequent revision walk
3268 add_pending_oid(revs
, NULL
, oid
, 0);
3271 stdin_packs_found_nr
++;
3274 create_object_entry(oid
, type
, 0, 0, 0, p
, ofs
);
3279 static void show_commit_pack_hint(struct commit
*commit UNUSED
,
3282 /* nothing to do; commits don't have a namehash */
3285 static void show_object_pack_hint(struct object
*object
, const char *name
,
3288 struct object_entry
*oe
= packlist_find(&to_pack
, &object
->oid
);
3293 * Our 'to_pack' list was constructed by iterating all objects packed in
3294 * included packs, and so doesn't have a non-zero hash field that you
3295 * would typically pick up during a reachability traversal.
3297 * Make a best-effort attempt to fill in the ->hash and ->no_try_delta
3298 * here using a now in order to perhaps improve the delta selection
3301 oe
->hash
= pack_name_hash(name
);
3302 oe
->no_try_delta
= name
&& no_try_delta(name
);
3304 stdin_packs_hints_nr
++;
3307 static int pack_mtime_cmp(const void *_a
, const void *_b
)
3309 struct packed_git
*a
= ((const struct string_list_item
*)_a
)->util
;
3310 struct packed_git
*b
= ((const struct string_list_item
*)_b
)->util
;
3313 * order packs by descending mtime so that objects are laid out
3314 * roughly as newest-to-oldest
3316 if (a
->mtime
< b
->mtime
)
3318 else if (b
->mtime
< a
->mtime
)
3324 static void read_packs_list_from_stdin(void)
3326 struct strbuf buf
= STRBUF_INIT
;
3327 struct string_list include_packs
= STRING_LIST_INIT_DUP
;
3328 struct string_list exclude_packs
= STRING_LIST_INIT_DUP
;
3329 struct string_list_item
*item
= NULL
;
3331 struct packed_git
*p
;
3332 struct rev_info revs
;
3334 repo_init_revisions(the_repository
, &revs
, NULL
);
3336 * Use a revision walk to fill in the namehash of objects in the include
3337 * packs. To save time, we'll avoid traversing through objects that are
3338 * in excluded packs.
3340 * That may cause us to avoid populating all of the namehash fields of
3341 * all included objects, but our goal is best-effort, since this is only
3342 * an optimization during delta selection.
3344 revs
.no_kept_objects
= 1;
3345 revs
.keep_pack_cache_flags
|= IN_CORE_KEEP_PACKS
;
3346 revs
.blob_objects
= 1;
3347 revs
.tree_objects
= 1;
3348 revs
.tag_objects
= 1;
3349 revs
.ignore_missing_links
= 1;
3351 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3355 if (*buf
.buf
== '^')
3356 string_list_append(&exclude_packs
, buf
.buf
+ 1);
3358 string_list_append(&include_packs
, buf
.buf
);
3363 string_list_sort(&include_packs
);
3364 string_list_remove_duplicates(&include_packs
, 0);
3365 string_list_sort(&exclude_packs
);
3366 string_list_remove_duplicates(&exclude_packs
, 0);
3368 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3369 const char *pack_name
= pack_basename(p
);
3371 if ((item
= string_list_lookup(&include_packs
, pack_name
)))
3373 if ((item
= string_list_lookup(&exclude_packs
, pack_name
)))
3378 * Arguments we got on stdin may not even be packs. First
3379 * check that to avoid segfaulting later on in
3380 * e.g. pack_mtime_cmp(), excluded packs are handled below.
3382 * Since we first parsed our STDIN and then sorted the input
3383 * lines the pack we error on will be whatever line happens to
3384 * sort first. This is lazy, it's enough that we report one
3385 * bad case here, we don't need to report the first/last one,
3388 for_each_string_list_item(item
, &include_packs
) {
3389 struct packed_git
*p
= item
->util
;
3391 die(_("could not find pack '%s'"), item
->string
);
3392 if (!is_pack_valid(p
))
3393 die(_("packfile %s cannot be accessed"), p
->pack_name
);
3397 * Then, handle all of the excluded packs, marking them as
3398 * kept in-core so that later calls to add_object_entry()
3399 * discards any objects that are also found in excluded packs.
3401 for_each_string_list_item(item
, &exclude_packs
) {
3402 struct packed_git
*p
= item
->util
;
3404 die(_("could not find pack '%s'"), item
->string
);
3405 p
->pack_keep_in_core
= 1;
3409 * Order packs by ascending mtime; use QSORT directly to access the
3410 * string_list_item's ->util pointer, which string_list_sort() does not
3413 QSORT(include_packs
.items
, include_packs
.nr
, pack_mtime_cmp
);
3415 for_each_string_list_item(item
, &include_packs
) {
3416 struct packed_git
*p
= item
->util
;
3417 for_each_object_in_pack(p
,
3418 add_object_entry_from_pack
,
3420 FOR_EACH_OBJECT_PACK_ORDER
);
3423 if (prepare_revision_walk(&revs
))
3424 die(_("revision walk setup failed"));
3425 traverse_commit_list(&revs
,
3426 show_commit_pack_hint
,
3427 show_object_pack_hint
,
3430 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_found",
3431 stdin_packs_found_nr
);
3432 trace2_data_intmax("pack-objects", the_repository
, "stdin_packs_hints",
3433 stdin_packs_hints_nr
);
3435 strbuf_release(&buf
);
3436 string_list_clear(&include_packs
, 0);
3437 string_list_clear(&exclude_packs
, 0);
3440 static void add_cruft_object_entry(const struct object_id
*oid
, enum object_type type
,
3441 struct packed_git
*pack
, off_t offset
,
3442 const char *name
, uint32_t mtime
)
3444 struct object_entry
*entry
;
3446 display_progress(progress_state
, ++nr_seen
);
3448 entry
= packlist_find(&to_pack
, oid
);
3451 entry
->hash
= pack_name_hash(name
);
3452 entry
->no_try_delta
= no_try_delta(name
);
3455 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
3457 if (!pack
&& type
== OBJ_BLOB
&& !has_loose_object(oid
)) {
3459 * If a traversed tree has a missing blob then we want
3460 * to avoid adding that missing object to our pack.
3462 * This only applies to missing blobs, not trees,
3463 * because the traversal needs to parse sub-trees but
3466 * Note we only perform this check when we couldn't
3467 * already find the object in a pack, so we're really
3468 * limited to "ensure non-tip blobs which don't exist in
3469 * packs do exist via loose objects". Confused?
3474 entry
= create_object_entry(oid
, type
, pack_name_hash(name
),
3475 0, name
&& no_try_delta(name
),
3479 if (mtime
> oe_cruft_mtime(&to_pack
, entry
))
3480 oe_set_cruft_mtime(&to_pack
, entry
, mtime
);
3484 static void show_cruft_object(struct object
*obj
, const char *name
, void *data UNUSED
)
3487 * if we did not record it earlier, it's at least as old as our
3488 * expiration value. Rather than find it exactly, just use that
3489 * value. This may bump it forward from its real mtime, but it
3490 * will still be "too old" next time we run with the same
3493 * if obj does appear in the packing list, this call is a noop (or may
3494 * set the namehash).
3496 add_cruft_object_entry(&obj
->oid
, obj
->type
, NULL
, 0, name
, cruft_expiration
);
3499 static void show_cruft_commit(struct commit
*commit
, void *data
)
3501 show_cruft_object((struct object
*)commit
, NULL
, data
);
3504 static int cruft_include_check_obj(struct object
*obj
, void *data UNUSED
)
3506 return !has_object_kept_pack(&obj
->oid
, IN_CORE_KEEP_PACKS
);
3509 static int cruft_include_check(struct commit
*commit
, void *data
)
3511 return cruft_include_check_obj((struct object
*)commit
, data
);
3514 static void set_cruft_mtime(const struct object
*object
,
3515 struct packed_git
*pack
,
3516 off_t offset
, time_t mtime
)
3518 add_cruft_object_entry(&object
->oid
, object
->type
, pack
, offset
, NULL
,
3522 static void mark_pack_kept_in_core(struct string_list
*packs
, unsigned keep
)
3524 struct string_list_item
*item
= NULL
;
3525 for_each_string_list_item(item
, packs
) {
3526 struct packed_git
*p
= item
->util
;
3528 die(_("could not find pack '%s'"), item
->string
);
3529 p
->pack_keep_in_core
= keep
;
3533 static void add_unreachable_loose_objects(void);
3534 static void add_objects_in_unpacked_packs(void);
3536 static void enumerate_cruft_objects(void)
3539 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3541 add_objects_in_unpacked_packs();
3542 add_unreachable_loose_objects();
3544 stop_progress(&progress_state
);
3547 static void enumerate_and_traverse_cruft_objects(struct string_list
*fresh_packs
)
3549 struct packed_git
*p
;
3550 struct rev_info revs
;
3553 repo_init_revisions(the_repository
, &revs
, NULL
);
3555 revs
.tag_objects
= 1;
3556 revs
.tree_objects
= 1;
3557 revs
.blob_objects
= 1;
3559 revs
.include_check
= cruft_include_check
;
3560 revs
.include_check_obj
= cruft_include_check_obj
;
3562 revs
.ignore_missing_links
= 1;
3565 progress_state
= start_progress(_("Enumerating cruft objects"), 0);
3566 ret
= add_unseen_recent_objects_to_traversal(&revs
, cruft_expiration
,
3567 set_cruft_mtime
, 1);
3568 stop_progress(&progress_state
);
3571 die(_("unable to add cruft objects"));
3574 * Re-mark only the fresh packs as kept so that objects in
3575 * unknown packs do not halt the reachability traversal early.
3577 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
3578 p
->pack_keep_in_core
= 0;
3579 mark_pack_kept_in_core(fresh_packs
, 1);
3581 if (prepare_revision_walk(&revs
))
3582 die(_("revision walk setup failed"));
3584 progress_state
= start_progress(_("Traversing cruft objects"), 0);
3586 traverse_commit_list(&revs
, show_cruft_commit
, show_cruft_object
, NULL
);
3588 stop_progress(&progress_state
);
3591 static void read_cruft_objects(void)
3593 struct strbuf buf
= STRBUF_INIT
;
3594 struct string_list discard_packs
= STRING_LIST_INIT_DUP
;
3595 struct string_list fresh_packs
= STRING_LIST_INIT_DUP
;
3596 struct packed_git
*p
;
3598 ignore_packed_keep_in_core
= 1;
3600 while (strbuf_getline(&buf
, stdin
) != EOF
) {
3604 if (*buf
.buf
== '-')
3605 string_list_append(&discard_packs
, buf
.buf
+ 1);
3607 string_list_append(&fresh_packs
, buf
.buf
);
3611 string_list_sort(&discard_packs
);
3612 string_list_sort(&fresh_packs
);
3614 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3615 const char *pack_name
= pack_basename(p
);
3616 struct string_list_item
*item
;
3618 item
= string_list_lookup(&fresh_packs
, pack_name
);
3620 item
= string_list_lookup(&discard_packs
, pack_name
);
3626 * This pack wasn't mentioned in either the "fresh" or
3627 * "discard" list, so the caller didn't know about it.
3629 * Mark it as kept so that its objects are ignored by
3630 * add_unseen_recent_objects_to_traversal(). We'll
3631 * unmark it before starting the traversal so it doesn't
3632 * halt the traversal early.
3634 p
->pack_keep_in_core
= 1;
3638 mark_pack_kept_in_core(&fresh_packs
, 1);
3639 mark_pack_kept_in_core(&discard_packs
, 0);
3641 if (cruft_expiration
)
3642 enumerate_and_traverse_cruft_objects(&fresh_packs
);
3644 enumerate_cruft_objects();
3646 strbuf_release(&buf
);
3647 string_list_clear(&discard_packs
, 0);
3648 string_list_clear(&fresh_packs
, 0);
3651 static void read_object_list_from_stdin(void)
3653 char line
[GIT_MAX_HEXSZ
+ 1 + PATH_MAX
+ 2];
3654 struct object_id oid
;
3658 if (!fgets(line
, sizeof(line
), stdin
)) {
3662 BUG("fgets returned NULL, not EOF, not error!");
3668 if (line
[0] == '-') {
3669 if (get_oid_hex(line
+1, &oid
))
3670 die(_("expected edge object ID, got garbage:\n %s"),
3672 add_preferred_base(&oid
);
3675 if (parse_oid_hex(line
, &oid
, &p
))
3676 die(_("expected object ID, got garbage:\n %s"), line
);
3678 add_preferred_base_object(p
+ 1);
3679 add_object_entry(&oid
, OBJ_NONE
, p
+ 1, 0);
3683 static void show_commit(struct commit
*commit
, void *data UNUSED
)
3685 add_object_entry(&commit
->object
.oid
, OBJ_COMMIT
, NULL
, 0);
3687 if (write_bitmap_index
)
3688 index_commit_for_bitmap(commit
);
3690 if (use_delta_islands
)
3691 propagate_island_marks(commit
);
3694 static void show_object(struct object
*obj
, const char *name
,
3697 add_preferred_base_object(name
);
3698 add_object_entry(&obj
->oid
, obj
->type
, name
, 0);
3700 if (use_delta_islands
) {
3703 struct object_entry
*ent
;
3705 /* the empty string is a root tree, which is depth 0 */
3706 depth
= *name
? 1 : 0;
3707 for (p
= strchr(name
, '/'); p
; p
= strchr(p
+ 1, '/'))
3710 ent
= packlist_find(&to_pack
, &obj
->oid
);
3711 if (ent
&& depth
> oe_tree_depth(&to_pack
, ent
))
3712 oe_set_tree_depth(&to_pack
, ent
, depth
);
3716 static void show_object__ma_allow_any(struct object
*obj
, const char *name
, void *data
)
3718 assert(arg_missing_action
== MA_ALLOW_ANY
);
3721 * Quietly ignore ALL missing objects. This avoids problems with
3722 * staging them now and getting an odd error later.
3724 if (!has_object(the_repository
, &obj
->oid
, 0))
3727 show_object(obj
, name
, data
);
3730 static void show_object__ma_allow_promisor(struct object
*obj
, const char *name
, void *data
)
3732 assert(arg_missing_action
== MA_ALLOW_PROMISOR
);
3735 * Quietly ignore EXPECTED missing objects. This avoids problems with
3736 * staging them now and getting an odd error later.
3738 if (!has_object(the_repository
, &obj
->oid
, 0) && is_promisor_object(&obj
->oid
))
3741 show_object(obj
, name
, data
);
3744 static int option_parse_missing_action(const struct option
*opt
,
3745 const char *arg
, int unset
)
3750 if (!strcmp(arg
, "error")) {
3751 arg_missing_action
= MA_ERROR
;
3752 fn_show_object
= show_object
;
3756 if (!strcmp(arg
, "allow-any")) {
3757 arg_missing_action
= MA_ALLOW_ANY
;
3758 fetch_if_missing
= 0;
3759 fn_show_object
= show_object__ma_allow_any
;
3763 if (!strcmp(arg
, "allow-promisor")) {
3764 arg_missing_action
= MA_ALLOW_PROMISOR
;
3765 fetch_if_missing
= 0;
3766 fn_show_object
= show_object__ma_allow_promisor
;
3770 die(_("invalid value for '%s': '%s'"), "--missing", arg
);
3774 static void show_edge(struct commit
*commit
)
3776 add_preferred_base(&commit
->object
.oid
);
3779 static int add_object_in_unpacked_pack(const struct object_id
*oid
,
3780 struct packed_git
*pack
,
3788 if (pack
->is_cruft
) {
3789 if (load_pack_mtimes(pack
) < 0)
3790 die(_("could not load cruft pack .mtimes"));
3791 mtime
= nth_packed_mtime(pack
, pos
);
3793 mtime
= pack
->mtime
;
3795 offset
= nth_packed_object_offset(pack
, pos
);
3797 add_cruft_object_entry(oid
, OBJ_NONE
, pack
, offset
,
3800 add_object_entry(oid
, OBJ_NONE
, "", 0);
3805 static void add_objects_in_unpacked_packs(void)
3807 if (for_each_packed_object(add_object_in_unpacked_pack
, NULL
,
3808 FOR_EACH_OBJECT_PACK_ORDER
|
3809 FOR_EACH_OBJECT_LOCAL_ONLY
|
3810 FOR_EACH_OBJECT_SKIP_IN_CORE_KEPT_PACKS
|
3811 FOR_EACH_OBJECT_SKIP_ON_DISK_KEPT_PACKS
))
3812 die(_("cannot open pack index"));
3815 static int add_loose_object(const struct object_id
*oid
, const char *path
,
3818 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
3821 warning(_("loose object at %s could not be examined"), path
);
3827 if (stat(path
, &st
) < 0) {
3828 if (errno
== ENOENT
)
3830 return error_errno("unable to stat %s", oid_to_hex(oid
));
3833 add_cruft_object_entry(oid
, type
, NULL
, 0, NULL
,
3836 add_object_entry(oid
, type
, "", 0);
3842 * We actually don't even have to worry about reachability here.
3843 * add_object_entry will weed out duplicates, so we just add every
3844 * loose object we find.
3846 static void add_unreachable_loose_objects(void)
3848 for_each_loose_file_in_objdir(get_object_directory(),
3853 static int has_sha1_pack_kept_or_nonlocal(const struct object_id
*oid
)
3855 static struct packed_git
*last_found
= (void *)1;
3856 struct packed_git
*p
;
3858 p
= (last_found
!= (void *)1) ? last_found
:
3859 get_all_packs(the_repository
);
3862 if ((!p
->pack_local
|| p
->pack_keep
||
3863 p
->pack_keep_in_core
) &&
3864 find_pack_entry_one(oid
->hash
, p
)) {
3868 if (p
== last_found
)
3869 p
= get_all_packs(the_repository
);
3872 if (p
== last_found
)
3879 * Store a list of sha1s that are should not be discarded
3880 * because they are either written too recently, or are
3881 * reachable from another object that was.
3883 * This is filled by get_object_list.
3885 static struct oid_array recent_objects
;
3887 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
3890 if (!unpack_unreachable_expiration
)
3892 if (mtime
> unpack_unreachable_expiration
)
3894 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
3899 static void loosen_unused_packed_objects(void)
3901 struct packed_git
*p
;
3903 uint32_t loosened_objects_nr
= 0;
3904 struct object_id oid
;
3906 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3907 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
3910 if (open_pack_index(p
))
3911 die(_("cannot open pack index"));
3913 for (i
= 0; i
< p
->num_objects
; i
++) {
3914 nth_packed_object_id(&oid
, p
, i
);
3915 if (!packlist_find(&to_pack
, &oid
) &&
3916 !has_sha1_pack_kept_or_nonlocal(&oid
) &&
3917 !loosened_object_can_be_discarded(&oid
, p
->mtime
)) {
3918 if (force_object_loose(&oid
, p
->mtime
))
3919 die(_("unable to force loose object"));
3920 loosened_objects_nr
++;
3925 trace2_data_intmax("pack-objects", the_repository
,
3926 "loosen_unused_packed_objects/loosened", loosened_objects_nr
);
3930 * This tracks any options which pack-reuse code expects to be on, or which a
3931 * reader of the pack might not understand, and which would therefore prevent
3932 * blind reuse of what we have on disk.
3934 static int pack_options_allow_reuse(void)
3936 return allow_pack_reuse
&&
3938 !ignore_packed_keep_on_disk
&&
3939 !ignore_packed_keep_in_core
&&
3940 (!local
|| !have_non_local_packs
) &&
3944 static int get_object_list_from_bitmap(struct rev_info
*revs
)
3946 if (!(bitmap_git
= prepare_bitmap_walk(revs
, 0)))
3949 if (pack_options_allow_reuse() &&
3950 !reuse_partial_packfile_from_bitmap(
3953 &reuse_packfile_objects
,
3954 &reuse_packfile_bitmap
)) {
3955 assert(reuse_packfile_objects
);
3956 nr_result
+= reuse_packfile_objects
;
3957 nr_seen
+= reuse_packfile_objects
;
3958 display_progress(progress_state
, nr_seen
);
3961 traverse_bitmap_commit_list(bitmap_git
, revs
,
3962 &add_object_entry_from_bitmap
);
3966 static void record_recent_object(struct object
*obj
,
3967 const char *name UNUSED
,
3970 oid_array_append(&recent_objects
, &obj
->oid
);
3973 static void record_recent_commit(struct commit
*commit
, void *data UNUSED
)
3975 oid_array_append(&recent_objects
, &commit
->object
.oid
);
3978 static int mark_bitmap_preferred_tip(const char *refname
,
3979 const struct object_id
*oid
,
3983 struct object_id peeled
;
3984 struct object
*object
;
3986 if (!peel_iterated_oid(oid
, &peeled
))
3989 object
= parse_object_or_die(oid
, refname
);
3990 if (object
->type
== OBJ_COMMIT
)
3991 object
->flags
|= NEEDS_BITMAP
;
3996 static void mark_bitmap_preferred_tips(void)
3998 struct string_list_item
*item
;
3999 const struct string_list
*preferred_tips
;
4001 preferred_tips
= bitmap_preferred_tips(the_repository
);
4002 if (!preferred_tips
)
4005 for_each_string_list_item(item
, preferred_tips
) {
4006 for_each_ref_in(item
->string
, mark_bitmap_preferred_tip
, NULL
);
4010 static void get_object_list(struct rev_info
*revs
, int ac
, const char **av
)
4012 struct setup_revision_opt s_r_opt
= {
4013 .allow_exclude_promisor_objects
= 1,
4019 save_commit_buffer
= 0;
4020 setup_revisions(ac
, av
, revs
, &s_r_opt
);
4022 /* make sure shallows are read */
4023 is_repository_shallow(the_repository
);
4025 save_warning
= warn_on_object_refname_ambiguity
;
4026 warn_on_object_refname_ambiguity
= 0;
4028 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
4029 int len
= strlen(line
);
4030 if (len
&& line
[len
- 1] == '\n')
4035 if (!strcmp(line
, "--not")) {
4036 flags
^= UNINTERESTING
;
4037 write_bitmap_index
= 0;
4040 if (starts_with(line
, "--shallow ")) {
4041 struct object_id oid
;
4042 if (get_oid_hex(line
+ 10, &oid
))
4043 die("not an object name '%s'", line
+ 10);
4044 register_shallow(the_repository
, &oid
);
4045 use_bitmap_index
= 0;
4048 die(_("not a rev '%s'"), line
);
4050 if (handle_revision_arg(line
, revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
4051 die(_("bad revision '%s'"), line
);
4054 warn_on_object_refname_ambiguity
= save_warning
;
4056 if (use_bitmap_index
&& !get_object_list_from_bitmap(revs
))
4059 if (use_delta_islands
)
4060 load_delta_islands(the_repository
, progress
);
4062 if (write_bitmap_index
)
4063 mark_bitmap_preferred_tips();
4065 if (prepare_revision_walk(revs
))
4066 die(_("revision walk setup failed"));
4067 mark_edges_uninteresting(revs
, show_edge
, sparse
);
4069 if (!fn_show_object
)
4070 fn_show_object
= show_object
;
4071 traverse_commit_list(revs
,
4072 show_commit
, fn_show_object
,
4075 if (unpack_unreachable_expiration
) {
4076 revs
->ignore_missing_links
= 1;
4077 if (add_unseen_recent_objects_to_traversal(revs
,
4078 unpack_unreachable_expiration
, NULL
, 0))
4079 die(_("unable to add recent objects"));
4080 if (prepare_revision_walk(revs
))
4081 die(_("revision walk setup failed"));
4082 traverse_commit_list(revs
, record_recent_commit
,
4083 record_recent_object
, NULL
);
4086 if (keep_unreachable
)
4087 add_objects_in_unpacked_packs();
4088 if (pack_loose_unreachable
)
4089 add_unreachable_loose_objects();
4090 if (unpack_unreachable
)
4091 loosen_unused_packed_objects();
4093 oid_array_clear(&recent_objects
);
4096 static void add_extra_kept_packs(const struct string_list
*names
)
4098 struct packed_git
*p
;
4103 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4104 const char *name
= basename(p
->pack_name
);
4110 for (i
= 0; i
< names
->nr
; i
++)
4111 if (!fspathcmp(name
, names
->items
[i
].string
))
4114 if (i
< names
->nr
) {
4115 p
->pack_keep_in_core
= 1;
4116 ignore_packed_keep_in_core
= 1;
4122 static int option_parse_index_version(const struct option
*opt
,
4123 const char *arg
, int unset
)
4126 const char *val
= arg
;
4128 BUG_ON_OPT_NEG(unset
);
4130 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
4131 if (pack_idx_opts
.version
> 2)
4132 die(_("unsupported index version %s"), val
);
4133 if (*c
== ',' && c
[1])
4134 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
4135 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
4136 die(_("bad index version '%s'"), val
);
4140 static int option_parse_unpack_unreachable(const struct option
*opt
,
4141 const char *arg
, int unset
)
4144 unpack_unreachable
= 0;
4145 unpack_unreachable_expiration
= 0;
4148 unpack_unreachable
= 1;
4150 unpack_unreachable_expiration
= approxidate(arg
);
4155 static int option_parse_cruft_expiration(const struct option
*opt
,
4156 const char *arg
, int unset
)
4160 cruft_expiration
= 0;
4164 cruft_expiration
= approxidate(arg
);
4169 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
4171 int use_internal_rev_list
= 0;
4173 int all_progress_implied
= 0;
4174 struct strvec rp
= STRVEC_INIT
;
4175 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
4176 int rev_list_index
= 0;
4177 int stdin_packs
= 0;
4178 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
4179 struct list_objects_filter_options filter_options
=
4180 LIST_OBJECTS_FILTER_INIT
;
4182 struct option pack_objects_options
[] = {
4183 OPT_SET_INT('q', "quiet", &progress
,
4184 N_("do not show progress meter"), 0),
4185 OPT_SET_INT(0, "progress", &progress
,
4186 N_("show progress meter"), 1),
4187 OPT_SET_INT(0, "all-progress", &progress
,
4188 N_("show progress meter during object writing phase"), 2),
4189 OPT_BOOL(0, "all-progress-implied",
4190 &all_progress_implied
,
4191 N_("similar to --all-progress when progress meter is shown")),
4192 OPT_CALLBACK_F(0, "index-version", NULL
, N_("<version>[,<offset>]"),
4193 N_("write the pack index file in the specified idx format version"),
4194 PARSE_OPT_NONEG
, option_parse_index_version
),
4195 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
4196 N_("maximum size of each output pack file")),
4197 OPT_BOOL(0, "local", &local
,
4198 N_("ignore borrowed objects from alternate object store")),
4199 OPT_BOOL(0, "incremental", &incremental
,
4200 N_("ignore packed objects")),
4201 OPT_INTEGER(0, "window", &window
,
4202 N_("limit pack window by objects")),
4203 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
4204 N_("limit pack window by memory in addition to object limit")),
4205 OPT_INTEGER(0, "depth", &depth
,
4206 N_("maximum length of delta chain allowed in the resulting pack")),
4207 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
4208 N_("reuse existing deltas")),
4209 OPT_BOOL(0, "reuse-object", &reuse_object
,
4210 N_("reuse existing objects")),
4211 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
4212 N_("use OFS_DELTA objects")),
4213 OPT_INTEGER(0, "threads", &delta_search_threads
,
4214 N_("use threads when searching for best delta matches")),
4215 OPT_BOOL(0, "non-empty", &non_empty
,
4216 N_("do not create an empty pack output")),
4217 OPT_BOOL(0, "revs", &use_internal_rev_list
,
4218 N_("read revision arguments from standard input")),
4219 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked
,
4220 N_("limit the objects to those that are not yet packed"),
4221 1, PARSE_OPT_NONEG
),
4222 OPT_SET_INT_F(0, "all", &rev_list_all
,
4223 N_("include objects reachable from any reference"),
4224 1, PARSE_OPT_NONEG
),
4225 OPT_SET_INT_F(0, "reflog", &rev_list_reflog
,
4226 N_("include objects referred by reflog entries"),
4227 1, PARSE_OPT_NONEG
),
4228 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index
,
4229 N_("include objects referred to by the index"),
4230 1, PARSE_OPT_NONEG
),
4231 OPT_BOOL(0, "stdin-packs", &stdin_packs
,
4232 N_("read packs from stdin")),
4233 OPT_BOOL(0, "stdout", &pack_to_stdout
,
4234 N_("output pack to stdout")),
4235 OPT_BOOL(0, "include-tag", &include_tag
,
4236 N_("include tag objects that refer to objects to be packed")),
4237 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
4238 N_("keep unreachable objects")),
4239 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
4240 N_("pack loose unreachable objects")),
4241 OPT_CALLBACK_F(0, "unpack-unreachable", NULL
, N_("time"),
4242 N_("unpack unreachable objects newer than <time>"),
4243 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
),
4244 OPT_BOOL(0, "cruft", &cruft
, N_("create a cruft pack")),
4245 OPT_CALLBACK_F(0, "cruft-expiration", NULL
, N_("time"),
4246 N_("expire cruft objects older than <time>"),
4247 PARSE_OPT_OPTARG
, option_parse_cruft_expiration
),
4248 OPT_BOOL(0, "sparse", &sparse
,
4249 N_("use the sparse reachability algorithm")),
4250 OPT_BOOL(0, "thin", &thin
,
4251 N_("create thin packs")),
4252 OPT_BOOL(0, "shallow", &shallow
,
4253 N_("create packs suitable for shallow fetches")),
4254 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk
,
4255 N_("ignore packs that have companion .keep file")),
4256 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
4257 N_("ignore this pack")),
4258 OPT_INTEGER(0, "compression", &pack_compression_level
,
4259 N_("pack compression level")),
4260 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents
,
4261 N_("do not hide commits by grafts"), 0),
4262 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
4263 N_("use a bitmap index if available to speed up counting objects")),
4264 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index
,
4265 N_("write a bitmap index together with the pack index"),
4267 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
4268 &write_bitmap_index
,
4269 N_("write a bitmap index if possible"),
4270 WRITE_BITMAP_QUIET
, PARSE_OPT_HIDDEN
),
4271 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
4272 OPT_CALLBACK_F(0, "missing", NULL
, N_("action"),
4273 N_("handling for missing objects"), PARSE_OPT_NONEG
,
4274 option_parse_missing_action
),
4275 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
4276 N_("do not pack objects in promisor packfiles")),
4277 OPT_BOOL(0, "delta-islands", &use_delta_islands
,
4278 N_("respect islands during delta compression")),
4279 OPT_STRING_LIST(0, "uri-protocol", &uri_protocols
,
4281 N_("exclude any configured uploadpack.blobpackfileuri with this protocol")),
4285 if (DFS_NUM_STATES
> (1 << OE_DFS_STATE_BITS
))
4286 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
4288 disable_replace_refs();
4290 sparse
= git_env_bool("GIT_TEST_PACK_SPARSE", -1);
4291 if (the_repository
->gitdir
) {
4292 prepare_repo_settings(the_repository
);
4294 sparse
= the_repository
->settings
.pack_use_sparse
;
4297 reset_pack_idx_option(&pack_idx_opts
);
4298 pack_idx_opts
.flags
|= WRITE_REV
;
4299 git_config(git_pack_config
, NULL
);
4300 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX
, 0))
4301 pack_idx_opts
.flags
&= ~WRITE_REV
;
4303 progress
= isatty(2);
4304 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
4308 base_name
= argv
[0];
4311 if (pack_to_stdout
!= !base_name
|| argc
)
4312 usage_with_options(pack_usage
, pack_objects_options
);
4316 if (depth
>= (1 << OE_DEPTH_BITS
)) {
4317 warning(_("delta chain depth %d is too deep, forcing %d"),
4318 depth
, (1 << OE_DEPTH_BITS
) - 1);
4319 depth
= (1 << OE_DEPTH_BITS
) - 1;
4321 if (cache_max_small_delta_size
>= (1U << OE_Z_DELTA_BITS
)) {
4322 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
4323 (1U << OE_Z_DELTA_BITS
) - 1);
4324 cache_max_small_delta_size
= (1U << OE_Z_DELTA_BITS
) - 1;
4329 strvec_push(&rp
, "pack-objects");
4331 use_internal_rev_list
= 1;
4332 strvec_push(&rp
, shallow
4333 ? "--objects-edge-aggressive"
4334 : "--objects-edge");
4336 strvec_push(&rp
, "--objects");
4339 use_internal_rev_list
= 1;
4340 strvec_push(&rp
, "--all");
4342 if (rev_list_reflog
) {
4343 use_internal_rev_list
= 1;
4344 strvec_push(&rp
, "--reflog");
4346 if (rev_list_index
) {
4347 use_internal_rev_list
= 1;
4348 strvec_push(&rp
, "--indexed-objects");
4350 if (rev_list_unpacked
&& !stdin_packs
) {
4351 use_internal_rev_list
= 1;
4352 strvec_push(&rp
, "--unpacked");
4355 if (exclude_promisor_objects
) {
4356 use_internal_rev_list
= 1;
4357 fetch_if_missing
= 0;
4358 strvec_push(&rp
, "--exclude-promisor-objects");
4360 if (unpack_unreachable
|| keep_unreachable
|| pack_loose_unreachable
)
4361 use_internal_rev_list
= 1;
4365 if (pack_compression_level
== -1)
4366 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
4367 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
4368 die(_("bad pack compression level %d"), pack_compression_level
);
4370 if (!delta_search_threads
) /* --threads=0 means autodetect */
4371 delta_search_threads
= online_cpus();
4373 if (!HAVE_THREADS
&& delta_search_threads
!= 1)
4374 warning(_("no threads support, ignoring --threads"));
4375 if (!pack_to_stdout
&& !pack_size_limit
&& !cruft
)
4376 pack_size_limit
= pack_size_limit_cfg
;
4377 if (pack_to_stdout
&& pack_size_limit
)
4378 die(_("--max-pack-size cannot be used to build a pack for transfer"));
4379 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
4380 warning(_("minimum pack size limit is 1 MiB"));
4381 pack_size_limit
= 1024*1024;
4384 if (!pack_to_stdout
&& thin
)
4385 die(_("--thin cannot be used to build an indexable pack"));
4387 if (keep_unreachable
&& unpack_unreachable
)
4388 die(_("options '%s' and '%s' cannot be used together"), "--keep-unreachable", "--unpack-unreachable");
4389 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
4390 unpack_unreachable_expiration
= 0;
4392 if (filter_options
.choice
) {
4393 if (!pack_to_stdout
)
4394 die(_("cannot use --filter without --stdout"));
4396 die(_("cannot use --filter with --stdin-packs"));
4399 if (stdin_packs
&& use_internal_rev_list
)
4400 die(_("cannot use internal rev list with --stdin-packs"));
4403 if (use_internal_rev_list
)
4404 die(_("cannot use internal rev list with --cruft"));
4406 die(_("cannot use --stdin-packs with --cruft"));
4407 if (pack_size_limit
)
4408 die(_("cannot use --max-pack-size with --cruft"));
4412 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
4414 * - to produce good pack (with bitmap index not-yet-packed objects are
4415 * packed in suboptimal order).
4417 * - to use more robust pack-generation codepath (avoiding possible
4418 * bugs in bitmap code and possible bitmap index corruption).
4420 if (!pack_to_stdout
)
4421 use_bitmap_index_default
= 0;
4423 if (use_bitmap_index
< 0)
4424 use_bitmap_index
= use_bitmap_index_default
;
4426 /* "hard" reasons not to use bitmaps; these just won't work at all */
4427 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow(the_repository
))
4428 use_bitmap_index
= 0;
4430 if (pack_to_stdout
|| !rev_list_all
)
4431 write_bitmap_index
= 0;
4433 if (use_delta_islands
)
4434 strvec_push(&rp
, "--topo-order");
4436 if (progress
&& all_progress_implied
)
4439 add_extra_kept_packs(&keep_pack_list
);
4440 if (ignore_packed_keep_on_disk
) {
4441 struct packed_git
*p
;
4442 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
4443 if (p
->pack_local
&& p
->pack_keep
)
4445 if (!p
) /* no keep-able packs found */
4446 ignore_packed_keep_on_disk
= 0;
4450 * unlike ignore_packed_keep_on_disk above, we do not
4451 * want to unset "local" based on looking at packs, as
4452 * it also covers non-local objects
4454 struct packed_git
*p
;
4455 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
4456 if (!p
->pack_local
) {
4457 have_non_local_packs
= 1;
4463 trace2_region_enter("pack-objects", "enumerate-objects",
4465 prepare_packing_data(the_repository
, &to_pack
);
4467 if (progress
&& !cruft
)
4468 progress_state
= start_progress(_("Enumerating objects"), 0);
4470 /* avoids adding objects in excluded packs */
4471 ignore_packed_keep_in_core
= 1;
4472 read_packs_list_from_stdin();
4473 if (rev_list_unpacked
)
4474 add_unreachable_loose_objects();
4476 read_cruft_objects();
4477 } else if (!use_internal_rev_list
) {
4478 read_object_list_from_stdin();
4480 struct rev_info revs
;
4482 repo_init_revisions(the_repository
, &revs
, NULL
);
4483 list_objects_filter_copy(&revs
.filter
, &filter_options
);
4484 get_object_list(&revs
, rp
.nr
, rp
.v
);
4485 release_revisions(&revs
);
4487 cleanup_preferred_base();
4488 if (include_tag
&& nr_result
)
4489 for_each_tag_ref(add_ref_tag
, NULL
);
4490 stop_progress(&progress_state
);
4491 trace2_region_leave("pack-objects", "enumerate-objects",
4494 if (non_empty
&& !nr_result
)
4497 trace2_region_enter("pack-objects", "prepare-pack",
4499 prepare_pack(window
, depth
);
4500 trace2_region_leave("pack-objects", "prepare-pack",
4504 trace2_region_enter("pack-objects", "write-pack-file", the_repository
);
4505 write_excluded_by_configs();
4507 trace2_region_leave("pack-objects", "write-pack-file", the_repository
);
4511 _("Total %"PRIu32
" (delta %"PRIu32
"),"
4512 " reused %"PRIu32
" (delta %"PRIu32
"),"
4513 " pack-reused %"PRIu32
),
4514 written
, written_delta
, reused
, reused_delta
,
4515 reuse_packfile_objects
);
4518 list_objects_filter_release(&filter_options
);