3 #include "repository.h"
13 #include "pack-revindex.h"
14 #include "csum-file.h"
15 #include "tree-walk.h"
18 #include "list-objects.h"
19 #include "list-objects-filter.h"
20 #include "list-objects-filter-options.h"
21 #include "pack-objects.h"
24 #include "streaming.h"
25 #include "thread-utils.h"
26 #include "pack-bitmap.h"
27 #include "delta-islands.h"
28 #include "reachable.h"
29 #include "sha1-array.h"
30 #include "argv-array.h"
33 #include "object-store.h"
37 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
38 #define SIZE(obj) oe_size(&to_pack, obj)
39 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
40 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
41 #define DELTA(obj) oe_delta(&to_pack, obj)
42 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
43 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
44 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
45 #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
46 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
47 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
48 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
50 static const char *pack_usage
[] = {
51 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
52 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
57 * Objects we are going to pack are collected in the `to_pack` structure.
58 * It contains an array (dynamically expanded) of the object data, and a map
59 * that can resolve SHA1s to their position in the array.
61 static struct packing_data to_pack
;
63 static struct pack_idx_entry
**written_list
;
64 static uint32_t nr_result
, nr_written
, nr_seen
;
65 static struct bitmap_index
*bitmap_git
;
66 static uint32_t write_layer
;
69 static int reuse_delta
= 1, reuse_object
= 1;
70 static int keep_unreachable
, unpack_unreachable
, include_tag
;
71 static timestamp_t unpack_unreachable_expiration
;
72 static int pack_loose_unreachable
;
74 static int have_non_local_packs
;
75 static int incremental
;
76 static int ignore_packed_keep_on_disk
;
77 static int ignore_packed_keep_in_core
;
78 static int allow_ofs_delta
;
79 static struct pack_idx_option pack_idx_opts
;
80 static const char *base_name
;
81 static int progress
= 1;
82 static int window
= 10;
83 static unsigned long pack_size_limit
;
84 static int depth
= 50;
85 static int delta_search_threads
;
86 static int pack_to_stdout
;
88 static int num_preferred_base
;
89 static struct progress
*progress_state
;
91 static struct packed_git
*reuse_packfile
;
92 static uint32_t reuse_packfile_objects
;
93 static off_t reuse_packfile_offset
;
95 static int use_bitmap_index_default
= 1;
96 static int use_bitmap_index
= -1;
97 static int write_bitmap_index
;
98 static uint16_t write_bitmap_options
;
100 static int exclude_promisor_objects
;
102 static int use_delta_islands
;
104 static unsigned long delta_cache_size
= 0;
105 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
106 static unsigned long cache_max_small_delta_size
= 1000;
108 static unsigned long window_memory_limit
= 0;
110 static struct list_objects_filter_options filter_options
;
112 enum missing_action
{
113 MA_ERROR
= 0, /* fail if any missing objects are encountered */
114 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
115 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
117 static enum missing_action arg_missing_action
;
118 static show_object_fn fn_show_object
;
123 static uint32_t written
, written_delta
;
124 static uint32_t reused
, reused_delta
;
129 static struct commit
**indexed_commits
;
130 static unsigned int indexed_commits_nr
;
131 static unsigned int indexed_commits_alloc
;
133 static void index_commit_for_bitmap(struct commit
*commit
)
135 if (indexed_commits_nr
>= indexed_commits_alloc
) {
136 indexed_commits_alloc
= (indexed_commits_alloc
+ 32) * 2;
137 REALLOC_ARRAY(indexed_commits
, indexed_commits_alloc
);
140 indexed_commits
[indexed_commits_nr
++] = commit
;
143 static void *get_delta(struct object_entry
*entry
)
145 unsigned long size
, base_size
, delta_size
;
146 void *buf
, *base_buf
, *delta_buf
;
147 enum object_type type
;
149 buf
= read_object_file(&entry
->idx
.oid
, &type
, &size
);
151 die(_("unable to read %s"), oid_to_hex(&entry
->idx
.oid
));
152 base_buf
= read_object_file(&DELTA(entry
)->idx
.oid
, &type
,
155 die("unable to read %s",
156 oid_to_hex(&DELTA(entry
)->idx
.oid
));
157 delta_buf
= diff_delta(base_buf
, base_size
,
158 buf
, size
, &delta_size
, 0);
160 * We succesfully computed this delta once but dropped it for
161 * memory reasons. Something is very wrong if this time we
162 * recompute and create a different delta.
164 if (!delta_buf
|| delta_size
!= DELTA_SIZE(entry
))
165 BUG("delta size changed");
171 static unsigned long do_compress(void **pptr
, unsigned long size
)
175 unsigned long maxsize
;
177 git_deflate_init(&stream
, pack_compression_level
);
178 maxsize
= git_deflate_bound(&stream
, size
);
181 out
= xmalloc(maxsize
);
185 stream
.avail_in
= size
;
186 stream
.next_out
= out
;
187 stream
.avail_out
= maxsize
;
188 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
190 git_deflate_end(&stream
);
193 return stream
.total_out
;
196 static unsigned long write_large_blob_data(struct git_istream
*st
, struct hashfile
*f
,
197 const struct object_id
*oid
)
200 unsigned char ibuf
[1024 * 16];
201 unsigned char obuf
[1024 * 16];
202 unsigned long olen
= 0;
204 git_deflate_init(&stream
, pack_compression_level
);
209 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
211 die(_("unable to read %s"), oid_to_hex(oid
));
213 stream
.next_in
= ibuf
;
214 stream
.avail_in
= readlen
;
215 while ((stream
.avail_in
|| readlen
== 0) &&
216 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
217 stream
.next_out
= obuf
;
218 stream
.avail_out
= sizeof(obuf
);
219 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
220 hashwrite(f
, obuf
, stream
.next_out
- obuf
);
221 olen
+= stream
.next_out
- obuf
;
224 die(_("deflate error (%d)"), zret
);
226 if (zret
!= Z_STREAM_END
)
227 die(_("deflate error (%d)"), zret
);
231 git_deflate_end(&stream
);
236 * we are going to reuse the existing object data as is. make
237 * sure it is not corrupt.
239 static int check_pack_inflate(struct packed_git
*p
,
240 struct pack_window
**w_curs
,
243 unsigned long expect
)
246 unsigned char fakebuf
[4096], *in
;
249 memset(&stream
, 0, sizeof(stream
));
250 git_inflate_init(&stream
);
252 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
254 stream
.next_out
= fakebuf
;
255 stream
.avail_out
= sizeof(fakebuf
);
256 st
= git_inflate(&stream
, Z_FINISH
);
257 offset
+= stream
.next_in
- in
;
258 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
259 git_inflate_end(&stream
);
260 return (st
== Z_STREAM_END
&&
261 stream
.total_out
== expect
&&
262 stream
.total_in
== len
) ? 0 : -1;
265 static void copy_pack_data(struct hashfile
*f
,
266 struct packed_git
*p
,
267 struct pack_window
**w_curs
,
275 in
= use_pack(p
, w_curs
, offset
, &avail
);
277 avail
= (unsigned long)len
;
278 hashwrite(f
, in
, avail
);
284 /* Return 0 if we will bust the pack-size limit */
285 static unsigned long write_no_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
286 unsigned long limit
, int usable_delta
)
288 unsigned long size
, datalen
;
289 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
290 dheader
[MAX_PACK_OBJECT_HEADER
];
292 enum object_type type
;
294 struct git_istream
*st
= NULL
;
295 const unsigned hashsz
= the_hash_algo
->rawsz
;
298 if (oe_type(entry
) == OBJ_BLOB
&&
299 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
) &&
300 (st
= open_istream(&entry
->idx
.oid
, &type
, &size
, NULL
)) != NULL
)
303 buf
= read_object_file(&entry
->idx
.oid
, &type
, &size
);
305 die(_("unable to read %s"),
306 oid_to_hex(&entry
->idx
.oid
));
309 * make sure no cached delta data remains from a
310 * previous attempt before a pack split occurred.
312 FREE_AND_NULL(entry
->delta_data
);
313 entry
->z_delta_size
= 0;
314 } else if (entry
->delta_data
) {
315 size
= DELTA_SIZE(entry
);
316 buf
= entry
->delta_data
;
317 entry
->delta_data
= NULL
;
318 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
319 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
321 buf
= get_delta(entry
);
322 size
= DELTA_SIZE(entry
);
323 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
324 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
327 if (st
) /* large blob case, just assume we don't compress well */
329 else if (entry
->z_delta_size
)
330 datalen
= entry
->z_delta_size
;
332 datalen
= do_compress(&buf
, size
);
335 * The object header is a byte of 'type' followed by zero or
336 * more bytes of length.
338 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
341 if (type
== OBJ_OFS_DELTA
) {
343 * Deltas with relative base contain an additional
344 * encoding of the relative offset for the delta
345 * base from this object's position in the pack.
347 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
348 unsigned pos
= sizeof(dheader
) - 1;
349 dheader
[pos
] = ofs
& 127;
351 dheader
[--pos
] = 128 | (--ofs
& 127);
352 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
358 hashwrite(f
, header
, hdrlen
);
359 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
360 hdrlen
+= sizeof(dheader
) - pos
;
361 } else if (type
== OBJ_REF_DELTA
) {
363 * Deltas with a base reference contain
364 * additional bytes for the base object ID.
366 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
372 hashwrite(f
, header
, hdrlen
);
373 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
376 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
382 hashwrite(f
, header
, hdrlen
);
385 datalen
= write_large_blob_data(st
, f
, &entry
->idx
.oid
);
388 hashwrite(f
, buf
, datalen
);
392 return hdrlen
+ datalen
;
395 /* Return 0 if we will bust the pack-size limit */
396 static off_t
write_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
397 unsigned long limit
, int usable_delta
)
399 struct packed_git
*p
= IN_PACK(entry
);
400 struct pack_window
*w_curs
= NULL
;
401 struct revindex_entry
*revidx
;
403 enum object_type type
= oe_type(entry
);
405 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
406 dheader
[MAX_PACK_OBJECT_HEADER
];
408 const unsigned hashsz
= the_hash_algo
->rawsz
;
409 unsigned long entry_size
= SIZE(entry
);
412 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
413 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
414 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
417 offset
= entry
->in_pack_offset
;
418 revidx
= find_pack_revindex(p
, offset
);
419 datalen
= revidx
[1].offset
- offset
;
420 if (!pack_to_stdout
&& p
->index_version
> 1 &&
421 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
)) {
422 error(_("bad packed object CRC for %s"),
423 oid_to_hex(&entry
->idx
.oid
));
425 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
428 offset
+= entry
->in_pack_header_size
;
429 datalen
-= entry
->in_pack_header_size
;
431 if (!pack_to_stdout
&& p
->index_version
== 1 &&
432 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry_size
)) {
433 error(_("corrupt packed object for %s"),
434 oid_to_hex(&entry
->idx
.oid
));
436 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
439 if (type
== OBJ_OFS_DELTA
) {
440 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
441 unsigned pos
= sizeof(dheader
) - 1;
442 dheader
[pos
] = ofs
& 127;
444 dheader
[--pos
] = 128 | (--ofs
& 127);
445 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
449 hashwrite(f
, header
, hdrlen
);
450 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
451 hdrlen
+= sizeof(dheader
) - pos
;
453 } else if (type
== OBJ_REF_DELTA
) {
454 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
458 hashwrite(f
, header
, hdrlen
);
459 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
463 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
467 hashwrite(f
, header
, hdrlen
);
469 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
472 return hdrlen
+ datalen
;
475 /* Return 0 if we will bust the pack-size limit */
476 static off_t
write_object(struct hashfile
*f
,
477 struct object_entry
*entry
,
482 int usable_delta
, to_reuse
;
487 /* apply size limit if limited packsize and not first object */
488 if (!pack_size_limit
|| !nr_written
)
490 else if (pack_size_limit
<= write_offset
)
492 * the earlier object did not fit the limit; avoid
493 * mistaking this with unlimited (i.e. limit = 0).
497 limit
= pack_size_limit
- write_offset
;
500 usable_delta
= 0; /* no delta */
501 else if (!pack_size_limit
)
502 usable_delta
= 1; /* unlimited packfile */
503 else if (DELTA(entry
)->idx
.offset
== (off_t
)-1)
504 usable_delta
= 0; /* base was written to another pack */
505 else if (DELTA(entry
)->idx
.offset
)
506 usable_delta
= 1; /* base already exists in this pack */
508 usable_delta
= 0; /* base could end up in another pack */
511 to_reuse
= 0; /* explicit */
512 else if (!IN_PACK(entry
))
513 to_reuse
= 0; /* can't reuse what we don't have */
514 else if (oe_type(entry
) == OBJ_REF_DELTA
||
515 oe_type(entry
) == OBJ_OFS_DELTA
)
516 /* check_object() decided it for us ... */
517 to_reuse
= usable_delta
;
518 /* ... but pack split may override that */
519 else if (oe_type(entry
) != entry
->in_pack_type
)
520 to_reuse
= 0; /* pack has delta which is unusable */
521 else if (DELTA(entry
))
522 to_reuse
= 0; /* we want to pack afresh */
524 to_reuse
= 1; /* we have it in-pack undeltified,
525 * and we do not need to deltify it.
529 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
531 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
539 entry
->idx
.crc32
= crc32_end(f
);
543 enum write_one_status
{
544 WRITE_ONE_SKIP
= -1, /* already written */
545 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
546 WRITE_ONE_WRITTEN
= 1, /* normal */
547 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
550 static enum write_one_status
write_one(struct hashfile
*f
,
551 struct object_entry
*e
,
558 * we set offset to 1 (which is an impossible value) to mark
559 * the fact that this object is involved in "write its base
560 * first before writing a deltified object" recursion.
562 recursing
= (e
->idx
.offset
== 1);
564 warning(_("recursive delta detected for object %s"),
565 oid_to_hex(&e
->idx
.oid
));
566 return WRITE_ONE_RECURSIVE
;
567 } else if (e
->idx
.offset
|| e
->preferred_base
) {
568 /* offset is non zero if object is written already. */
569 return WRITE_ONE_SKIP
;
572 /* if we are deltified, write out base object first. */
574 e
->idx
.offset
= 1; /* now recurse */
575 switch (write_one(f
, DELTA(e
), offset
)) {
576 case WRITE_ONE_RECURSIVE
:
577 /* we cannot depend on this one */
582 case WRITE_ONE_BREAK
:
583 e
->idx
.offset
= recursing
;
584 return WRITE_ONE_BREAK
;
588 e
->idx
.offset
= *offset
;
589 size
= write_object(f
, e
, *offset
);
591 e
->idx
.offset
= recursing
;
592 return WRITE_ONE_BREAK
;
594 written_list
[nr_written
++] = &e
->idx
;
596 /* make sure off_t is sufficiently large not to wrap */
597 if (signed_add_overflows(*offset
, size
))
598 die(_("pack too large for current definition of off_t"));
600 return WRITE_ONE_WRITTEN
;
603 static int mark_tagged(const char *path
, const struct object_id
*oid
, int flag
,
606 struct object_id peeled
;
607 struct object_entry
*entry
= packlist_find(&to_pack
, oid
->hash
, NULL
);
611 if (!peel_ref(path
, &peeled
)) {
612 entry
= packlist_find(&to_pack
, peeled
.hash
, NULL
);
619 static inline void add_to_write_order(struct object_entry
**wo
,
621 struct object_entry
*e
)
623 if (e
->filled
|| oe_layer(&to_pack
, e
) != write_layer
)
629 static void add_descendants_to_write_order(struct object_entry
**wo
,
631 struct object_entry
*e
)
633 int add_to_order
= 1;
636 struct object_entry
*s
;
637 /* add this node... */
638 add_to_write_order(wo
, endp
, e
);
639 /* all its siblings... */
640 for (s
= DELTA_SIBLING(e
); s
; s
= DELTA_SIBLING(s
)) {
641 add_to_write_order(wo
, endp
, s
);
644 /* drop down a level to add left subtree nodes if possible */
645 if (DELTA_CHILD(e
)) {
650 /* our sibling might have some children, it is next */
651 if (DELTA_SIBLING(e
)) {
652 e
= DELTA_SIBLING(e
);
655 /* go back to our parent node */
657 while (e
&& !DELTA_SIBLING(e
)) {
658 /* we're on the right side of a subtree, keep
659 * going up until we can go right again */
663 /* done- we hit our original root node */
666 /* pass it off to sibling at this level */
667 e
= DELTA_SIBLING(e
);
672 static void add_family_to_write_order(struct object_entry
**wo
,
674 struct object_entry
*e
)
676 struct object_entry
*root
;
678 for (root
= e
; DELTA(root
); root
= DELTA(root
))
680 add_descendants_to_write_order(wo
, endp
, root
);
683 static void compute_layer_order(struct object_entry
**wo
, unsigned int *wo_end
)
685 unsigned int i
, last_untagged
;
686 struct object_entry
*objects
= to_pack
.objects
;
688 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
689 if (objects
[i
].tagged
)
691 add_to_write_order(wo
, wo_end
, &objects
[i
]);
696 * Then fill all the tagged tips.
698 for (; i
< to_pack
.nr_objects
; i
++) {
699 if (objects
[i
].tagged
)
700 add_to_write_order(wo
, wo_end
, &objects
[i
]);
704 * And then all remaining commits and tags.
706 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
707 if (oe_type(&objects
[i
]) != OBJ_COMMIT
&&
708 oe_type(&objects
[i
]) != OBJ_TAG
)
710 add_to_write_order(wo
, wo_end
, &objects
[i
]);
714 * And then all the trees.
716 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
717 if (oe_type(&objects
[i
]) != OBJ_TREE
)
719 add_to_write_order(wo
, wo_end
, &objects
[i
]);
723 * Finally all the rest in really tight order
725 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
726 if (!objects
[i
].filled
&& oe_layer(&to_pack
, &objects
[i
]) == write_layer
)
727 add_family_to_write_order(wo
, wo_end
, &objects
[i
]);
731 static struct object_entry
**compute_write_order(void)
733 uint32_t max_layers
= 1;
734 unsigned int i
, wo_end
;
736 struct object_entry
**wo
;
737 struct object_entry
*objects
= to_pack
.objects
;
739 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
740 objects
[i
].tagged
= 0;
741 objects
[i
].filled
= 0;
742 SET_DELTA_CHILD(&objects
[i
], NULL
);
743 SET_DELTA_SIBLING(&objects
[i
], NULL
);
747 * Fully connect delta_child/delta_sibling network.
748 * Make sure delta_sibling is sorted in the original
751 for (i
= to_pack
.nr_objects
; i
> 0;) {
752 struct object_entry
*e
= &objects
[--i
];
755 /* Mark me as the first child */
756 e
->delta_sibling_idx
= DELTA(e
)->delta_child_idx
;
757 SET_DELTA_CHILD(DELTA(e
), e
);
761 * Mark objects that are at the tip of tags.
763 for_each_tag_ref(mark_tagged
, NULL
);
765 if (use_delta_islands
)
766 max_layers
= compute_pack_layers(&to_pack
);
768 ALLOC_ARRAY(wo
, to_pack
.nr_objects
);
771 for (; write_layer
< max_layers
; ++write_layer
)
772 compute_layer_order(wo
, &wo_end
);
774 if (wo_end
!= to_pack
.nr_objects
)
775 die(_("ordered %u objects, expected %"PRIu32
),
776 wo_end
, to_pack
.nr_objects
);
781 static off_t
write_reused_pack(struct hashfile
*f
)
783 unsigned char buffer
[8192];
784 off_t to_write
, total
;
787 if (!is_pack_valid(reuse_packfile
))
788 die(_("packfile is invalid: %s"), reuse_packfile
->pack_name
);
790 fd
= git_open(reuse_packfile
->pack_name
);
792 die_errno(_("unable to open packfile for reuse: %s"),
793 reuse_packfile
->pack_name
);
795 if (lseek(fd
, sizeof(struct pack_header
), SEEK_SET
) == -1)
796 die_errno(_("unable to seek in reused packfile"));
798 if (reuse_packfile_offset
< 0)
799 reuse_packfile_offset
= reuse_packfile
->pack_size
- the_hash_algo
->rawsz
;
801 total
= to_write
= reuse_packfile_offset
- sizeof(struct pack_header
);
804 int read_pack
= xread(fd
, buffer
, sizeof(buffer
));
807 die_errno(_("unable to read from reused packfile"));
809 if (read_pack
> to_write
)
810 read_pack
= to_write
;
812 hashwrite(f
, buffer
, read_pack
);
813 to_write
-= read_pack
;
816 * We don't know the actual number of objects written,
817 * only how many bytes written, how many bytes total, and
818 * how many objects total. So we can fake it by pretending all
819 * objects we are writing are the same size. This gives us a
820 * smooth progress meter, and at the end it matches the true
823 written
= reuse_packfile_objects
*
824 (((double)(total
- to_write
)) / total
);
825 display_progress(progress_state
, written
);
829 written
= reuse_packfile_objects
;
830 display_progress(progress_state
, written
);
831 return reuse_packfile_offset
- sizeof(struct pack_header
);
834 static const char no_split_warning
[] = N_(
835 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
838 static void write_pack_file(void)
843 uint32_t nr_remaining
= nr_result
;
844 time_t last_mtime
= 0;
845 struct object_entry
**write_order
;
847 if (progress
> pack_to_stdout
)
848 progress_state
= start_progress(_("Writing objects"), nr_result
);
849 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
850 write_order
= compute_write_order();
853 struct object_id oid
;
854 char *pack_tmp_name
= NULL
;
857 f
= hashfd_throughput(1, "<stdout>", progress_state
);
859 f
= create_tmp_packfile(&pack_tmp_name
);
861 offset
= write_pack_header(f
, nr_remaining
);
863 if (reuse_packfile
) {
865 assert(pack_to_stdout
);
867 packfile_size
= write_reused_pack(f
);
868 offset
+= packfile_size
;
872 for (; i
< to_pack
.nr_objects
; i
++) {
873 struct object_entry
*e
= write_order
[i
];
874 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
876 display_progress(progress_state
, written
);
880 * Did we write the wrong # entries in the header?
881 * If so, rewrite it like in fast-import
883 if (pack_to_stdout
) {
884 finalize_hashfile(f
, oid
.hash
, CSUM_HASH_IN_STREAM
| CSUM_CLOSE
);
885 } else if (nr_written
== nr_remaining
) {
886 finalize_hashfile(f
, oid
.hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
888 int fd
= finalize_hashfile(f
, oid
.hash
, 0);
889 fixup_pack_header_footer(fd
, oid
.hash
, pack_tmp_name
,
890 nr_written
, oid
.hash
, offset
);
892 if (write_bitmap_index
) {
893 warning(_(no_split_warning
));
894 write_bitmap_index
= 0;
898 if (!pack_to_stdout
) {
900 struct strbuf tmpname
= STRBUF_INIT
;
903 * Packs are runtime accessed in their mtime
904 * order since newer packs are more likely to contain
905 * younger objects. So if we are creating multiple
906 * packs then we should modify the mtime of later ones
907 * to preserve this property.
909 if (stat(pack_tmp_name
, &st
) < 0) {
910 warning_errno(_("failed to stat %s"), pack_tmp_name
);
911 } else if (!last_mtime
) {
912 last_mtime
= st
.st_mtime
;
915 utb
.actime
= st
.st_atime
;
916 utb
.modtime
= --last_mtime
;
917 if (utime(pack_tmp_name
, &utb
) < 0)
918 warning_errno(_("failed utime() on %s"), pack_tmp_name
);
921 strbuf_addf(&tmpname
, "%s-", base_name
);
923 if (write_bitmap_index
) {
924 bitmap_writer_set_checksum(oid
.hash
);
925 bitmap_writer_build_type_index(
926 &to_pack
, written_list
, nr_written
);
929 finish_tmp_packfile(&tmpname
, pack_tmp_name
,
930 written_list
, nr_written
,
931 &pack_idx_opts
, oid
.hash
);
933 if (write_bitmap_index
) {
934 strbuf_addf(&tmpname
, "%s.bitmap", oid_to_hex(&oid
));
936 stop_progress(&progress_state
);
938 bitmap_writer_show_progress(progress
);
939 bitmap_writer_reuse_bitmaps(&to_pack
);
940 bitmap_writer_select_commits(indexed_commits
, indexed_commits_nr
, -1);
941 bitmap_writer_build(&to_pack
);
942 bitmap_writer_finish(written_list
, nr_written
,
943 tmpname
.buf
, write_bitmap_options
);
944 write_bitmap_index
= 0;
947 strbuf_release(&tmpname
);
949 puts(oid_to_hex(&oid
));
952 /* mark written objects as written to previous pack */
953 for (j
= 0; j
< nr_written
; j
++) {
954 written_list
[j
]->offset
= (off_t
)-1;
956 nr_remaining
-= nr_written
;
957 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
961 stop_progress(&progress_state
);
962 if (written
!= nr_result
)
963 die(_("wrote %"PRIu32
" objects while expecting %"PRIu32
),
967 static int no_try_delta(const char *path
)
969 static struct attr_check
*check
;
972 check
= attr_check_initl("delta", NULL
);
973 git_check_attr(&the_index
, path
, check
);
974 if (ATTR_FALSE(check
->items
[0].value
))
980 * When adding an object, check whether we have already added it
981 * to our packing list. If so, we can skip. However, if we are
982 * being asked to excludei t, but the previous mention was to include
983 * it, make sure to adjust its flags and tweak our numbers accordingly.
985 * As an optimization, we pass out the index position where we would have
986 * found the item, since that saves us from having to look it up again a
987 * few lines later when we want to add the new entry.
989 static int have_duplicate_entry(const struct object_id
*oid
,
993 struct object_entry
*entry
;
995 entry
= packlist_find(&to_pack
, oid
->hash
, index_pos
);
1000 if (!entry
->preferred_base
)
1002 entry
->preferred_base
= 1;
1008 static int want_found_object(int exclude
, struct packed_git
*p
)
1016 * When asked to do --local (do not include an object that appears in a
1017 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1018 * an object that appears in a pack marked with .keep), finding a pack
1019 * that matches the criteria is sufficient for us to decide to omit it.
1020 * However, even if this pack does not satisfy the criteria, we need to
1021 * make sure no copy of this object appears in _any_ pack that makes us
1022 * to omit the object, so we need to check all the packs.
1024 * We can however first check whether these options can possible matter;
1025 * if they do not matter we know we want the object in generated pack.
1026 * Otherwise, we signal "-1" at the end to tell the caller that we do
1027 * not know either way, and it needs to check more packs.
1029 if (!ignore_packed_keep_on_disk
&&
1030 !ignore_packed_keep_in_core
&&
1031 (!local
|| !have_non_local_packs
))
1034 if (local
&& !p
->pack_local
)
1036 if (p
->pack_local
&&
1037 ((ignore_packed_keep_on_disk
&& p
->pack_keep
) ||
1038 (ignore_packed_keep_in_core
&& p
->pack_keep_in_core
)))
1041 /* we don't know yet; keep looking for more packs */
1046 * Check whether we want the object in the pack (e.g., we do not want
1047 * objects found in non-local stores if the "--local" option was used).
1049 * If the caller already knows an existing pack it wants to take the object
1050 * from, that is passed in *found_pack and *found_offset; otherwise this
1051 * function finds if there is any pack that has the object and returns the pack
1052 * and its offset in these variables.
1054 static int want_object_in_pack(const struct object_id
*oid
,
1056 struct packed_git
**found_pack
,
1057 off_t
*found_offset
)
1060 struct list_head
*pos
;
1061 struct multi_pack_index
*m
;
1063 if (!exclude
&& local
&& has_loose_object_nonlocal(oid
))
1067 * If we already know the pack object lives in, start checks from that
1068 * pack - in the usual case when neither --local was given nor .keep files
1069 * are present we will determine the answer right now.
1072 want
= want_found_object(exclude
, *found_pack
);
1077 for (m
= get_multi_pack_index(the_repository
); m
; m
= m
->next
) {
1078 struct pack_entry e
;
1079 if (fill_midx_entry(oid
, &e
, m
)) {
1080 struct packed_git
*p
= e
.p
;
1083 if (p
== *found_pack
)
1084 offset
= *found_offset
;
1086 offset
= find_pack_entry_one(oid
->hash
, p
);
1090 if (!is_pack_valid(p
))
1092 *found_offset
= offset
;
1095 want
= want_found_object(exclude
, p
);
1102 list_for_each(pos
, get_packed_git_mru(the_repository
)) {
1103 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1106 if (p
== *found_pack
)
1107 offset
= *found_offset
;
1109 offset
= find_pack_entry_one(oid
->hash
, p
);
1113 if (!is_pack_valid(p
))
1115 *found_offset
= offset
;
1118 want
= want_found_object(exclude
, p
);
1119 if (!exclude
&& want
> 0)
1121 get_packed_git_mru(the_repository
));
1130 static void create_object_entry(const struct object_id
*oid
,
1131 enum object_type type
,
1136 struct packed_git
*found_pack
,
1139 struct object_entry
*entry
;
1141 entry
= packlist_alloc(&to_pack
, oid
->hash
, index_pos
);
1143 oe_set_type(entry
, type
);
1145 entry
->preferred_base
= 1;
1149 oe_set_in_pack(&to_pack
, entry
, found_pack
);
1150 entry
->in_pack_offset
= found_offset
;
1153 entry
->no_try_delta
= no_try_delta
;
1156 static const char no_closure_warning
[] = N_(
1157 "disabling bitmap writing, as some objects are not being packed"
1160 static int add_object_entry(const struct object_id
*oid
, enum object_type type
,
1161 const char *name
, int exclude
)
1163 struct packed_git
*found_pack
= NULL
;
1164 off_t found_offset
= 0;
1167 display_progress(progress_state
, ++nr_seen
);
1169 if (have_duplicate_entry(oid
, exclude
, &index_pos
))
1172 if (!want_object_in_pack(oid
, exclude
, &found_pack
, &found_offset
)) {
1173 /* The pack is missing an object, so it will not have closure */
1174 if (write_bitmap_index
) {
1175 warning(_(no_closure_warning
));
1176 write_bitmap_index
= 0;
1181 create_object_entry(oid
, type
, pack_name_hash(name
),
1182 exclude
, name
&& no_try_delta(name
),
1183 index_pos
, found_pack
, found_offset
);
1187 static int add_object_entry_from_bitmap(const struct object_id
*oid
,
1188 enum object_type type
,
1189 int flags
, uint32_t name_hash
,
1190 struct packed_git
*pack
, off_t offset
)
1194 display_progress(progress_state
, ++nr_seen
);
1196 if (have_duplicate_entry(oid
, 0, &index_pos
))
1199 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
1202 create_object_entry(oid
, type
, name_hash
, 0, 0, index_pos
, pack
, offset
);
1206 struct pbase_tree_cache
{
1207 struct object_id oid
;
1211 unsigned long tree_size
;
1214 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1215 static int pbase_tree_cache_ix(const struct object_id
*oid
)
1217 return oid
->hash
[0] % ARRAY_SIZE(pbase_tree_cache
);
1219 static int pbase_tree_cache_ix_incr(int ix
)
1221 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1224 static struct pbase_tree
{
1225 struct pbase_tree
*next
;
1226 /* This is a phony "cache" entry; we are not
1227 * going to evict it or find it through _get()
1228 * mechanism -- this is for the toplevel node that
1229 * would almost always change with any commit.
1231 struct pbase_tree_cache pcache
;
1234 static struct pbase_tree_cache
*pbase_tree_get(const struct object_id
*oid
)
1236 struct pbase_tree_cache
*ent
, *nent
;
1239 enum object_type type
;
1241 int my_ix
= pbase_tree_cache_ix(oid
);
1242 int available_ix
= -1;
1244 /* pbase-tree-cache acts as a limited hashtable.
1245 * your object will be found at your index or within a few
1246 * slots after that slot if it is cached.
1248 for (neigh
= 0; neigh
< 8; neigh
++) {
1249 ent
= pbase_tree_cache
[my_ix
];
1250 if (ent
&& oideq(&ent
->oid
, oid
)) {
1254 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1255 ((0 <= available_ix
) &&
1256 (!ent
&& pbase_tree_cache
[available_ix
])))
1257 available_ix
= my_ix
;
1260 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1263 /* Did not find one. Either we got a bogus request or
1264 * we need to read and perhaps cache.
1266 data
= read_object_file(oid
, &type
, &size
);
1269 if (type
!= OBJ_TREE
) {
1274 /* We need to either cache or return a throwaway copy */
1276 if (available_ix
< 0)
1279 ent
= pbase_tree_cache
[available_ix
];
1280 my_ix
= available_ix
;
1284 nent
= xmalloc(sizeof(*nent
));
1285 nent
->temporary
= (available_ix
< 0);
1288 /* evict and reuse */
1289 free(ent
->tree_data
);
1292 oidcpy(&nent
->oid
, oid
);
1293 nent
->tree_data
= data
;
1294 nent
->tree_size
= size
;
1296 if (!nent
->temporary
)
1297 pbase_tree_cache
[my_ix
] = nent
;
1301 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1303 if (!cache
->temporary
) {
1307 free(cache
->tree_data
);
1311 static int name_cmp_len(const char *name
)
1314 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
1319 static void add_pbase_object(struct tree_desc
*tree
,
1322 const char *fullname
)
1324 struct name_entry entry
;
1327 while (tree_entry(tree
,&entry
)) {
1328 if (S_ISGITLINK(entry
.mode
))
1330 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1331 memcmp(name
, entry
.path
, cmplen
);
1336 if (name
[cmplen
] != '/') {
1337 add_object_entry(entry
.oid
,
1338 object_type(entry
.mode
),
1342 if (S_ISDIR(entry
.mode
)) {
1343 struct tree_desc sub
;
1344 struct pbase_tree_cache
*tree
;
1345 const char *down
= name
+cmplen
+1;
1346 int downlen
= name_cmp_len(down
);
1348 tree
= pbase_tree_get(entry
.oid
);
1351 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1353 add_pbase_object(&sub
, down
, downlen
, fullname
);
1354 pbase_tree_put(tree
);
1359 static unsigned *done_pbase_paths
;
1360 static int done_pbase_paths_num
;
1361 static int done_pbase_paths_alloc
;
1362 static int done_pbase_path_pos(unsigned hash
)
1365 int hi
= done_pbase_paths_num
;
1367 int mi
= lo
+ (hi
- lo
) / 2;
1368 if (done_pbase_paths
[mi
] == hash
)
1370 if (done_pbase_paths
[mi
] < hash
)
1378 static int check_pbase_path(unsigned hash
)
1380 int pos
= done_pbase_path_pos(hash
);
1384 ALLOC_GROW(done_pbase_paths
,
1385 done_pbase_paths_num
+ 1,
1386 done_pbase_paths_alloc
);
1387 done_pbase_paths_num
++;
1388 if (pos
< done_pbase_paths_num
)
1389 MOVE_ARRAY(done_pbase_paths
+ pos
+ 1, done_pbase_paths
+ pos
,
1390 done_pbase_paths_num
- pos
- 1);
1391 done_pbase_paths
[pos
] = hash
;
1395 static void add_preferred_base_object(const char *name
)
1397 struct pbase_tree
*it
;
1399 unsigned hash
= pack_name_hash(name
);
1401 if (!num_preferred_base
|| check_pbase_path(hash
))
1404 cmplen
= name_cmp_len(name
);
1405 for (it
= pbase_tree
; it
; it
= it
->next
) {
1407 add_object_entry(&it
->pcache
.oid
, OBJ_TREE
, NULL
, 1);
1410 struct tree_desc tree
;
1411 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1412 add_pbase_object(&tree
, name
, cmplen
, name
);
1417 static void add_preferred_base(struct object_id
*oid
)
1419 struct pbase_tree
*it
;
1422 struct object_id tree_oid
;
1424 if (window
<= num_preferred_base
++)
1427 data
= read_object_with_reference(oid
, tree_type
, &size
, &tree_oid
);
1431 for (it
= pbase_tree
; it
; it
= it
->next
) {
1432 if (oideq(&it
->pcache
.oid
, &tree_oid
)) {
1438 it
= xcalloc(1, sizeof(*it
));
1439 it
->next
= pbase_tree
;
1442 oidcpy(&it
->pcache
.oid
, &tree_oid
);
1443 it
->pcache
.tree_data
= data
;
1444 it
->pcache
.tree_size
= size
;
1447 static void cleanup_preferred_base(void)
1449 struct pbase_tree
*it
;
1455 struct pbase_tree
*tmp
= it
;
1457 free(tmp
->pcache
.tree_data
);
1461 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1462 if (!pbase_tree_cache
[i
])
1464 free(pbase_tree_cache
[i
]->tree_data
);
1465 FREE_AND_NULL(pbase_tree_cache
[i
]);
1468 FREE_AND_NULL(done_pbase_paths
);
1469 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1473 * Return 1 iff the object specified by "delta" can be sent
1474 * literally as a delta against the base in "base_sha1". If
1475 * so, then *base_out will point to the entry in our packing
1476 * list, or NULL if we must use the external-base list.
1478 * Depth value does not matter - find_deltas() will
1479 * never consider reused delta as the base object to
1480 * deltify other objects against, in order to avoid
1483 static int can_reuse_delta(const unsigned char *base_sha1
,
1484 struct object_entry
*delta
,
1485 struct object_entry
**base_out
)
1487 struct object_entry
*base
;
1493 * First see if we're already sending the base (or it's explicitly in
1494 * our "excluded" list).
1496 base
= packlist_find(&to_pack
, base_sha1
, NULL
);
1498 if (!in_same_island(&delta
->idx
.oid
, &base
->idx
.oid
))
1505 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1506 * even if it was buried too deep in history to make it into the
1509 if (thin
&& bitmap_has_sha1_in_uninteresting(bitmap_git
, base_sha1
)) {
1510 if (use_delta_islands
) {
1511 struct object_id base_oid
;
1512 hashcpy(base_oid
.hash
, base_sha1
);
1513 if (!in_same_island(&delta
->idx
.oid
, &base_oid
))
1523 static void check_object(struct object_entry
*entry
)
1525 unsigned long canonical_size
;
1527 if (IN_PACK(entry
)) {
1528 struct packed_git
*p
= IN_PACK(entry
);
1529 struct pack_window
*w_curs
= NULL
;
1530 const unsigned char *base_ref
= NULL
;
1531 struct object_entry
*base_entry
;
1532 unsigned long used
, used_0
;
1533 unsigned long avail
;
1535 unsigned char *buf
, c
;
1536 enum object_type type
;
1537 unsigned long in_pack_size
;
1539 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1542 * We want in_pack_type even if we do not reuse delta
1543 * since non-delta representations could still be reused.
1545 used
= unpack_object_header_buffer(buf
, avail
,
1552 BUG("invalid type %d", type
);
1553 entry
->in_pack_type
= type
;
1556 * Determine if this is a delta and if so whether we can
1557 * reuse it or not. Otherwise let's find out as cheaply as
1558 * possible what the actual type and size for this object is.
1560 switch (entry
->in_pack_type
) {
1562 /* Not a delta hence we've already got all we need. */
1563 oe_set_type(entry
, entry
->in_pack_type
);
1564 SET_SIZE(entry
, in_pack_size
);
1565 entry
->in_pack_header_size
= used
;
1566 if (oe_type(entry
) < OBJ_COMMIT
|| oe_type(entry
) > OBJ_BLOB
)
1568 unuse_pack(&w_curs
);
1571 if (reuse_delta
&& !entry
->preferred_base
)
1572 base_ref
= use_pack(p
, &w_curs
,
1573 entry
->in_pack_offset
+ used
, NULL
);
1574 entry
->in_pack_header_size
= used
+ the_hash_algo
->rawsz
;
1577 buf
= use_pack(p
, &w_curs
,
1578 entry
->in_pack_offset
+ used
, NULL
);
1584 if (!ofs
|| MSB(ofs
, 7)) {
1585 error(_("delta base offset overflow in pack for %s"),
1586 oid_to_hex(&entry
->idx
.oid
));
1590 ofs
= (ofs
<< 7) + (c
& 127);
1592 ofs
= entry
->in_pack_offset
- ofs
;
1593 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
1594 error(_("delta base offset out of bound for %s"),
1595 oid_to_hex(&entry
->idx
.oid
));
1598 if (reuse_delta
&& !entry
->preferred_base
) {
1599 struct revindex_entry
*revidx
;
1600 revidx
= find_pack_revindex(p
, ofs
);
1603 base_ref
= nth_packed_object_sha1(p
, revidx
->nr
);
1605 entry
->in_pack_header_size
= used
+ used_0
;
1609 if (can_reuse_delta(base_ref
, entry
, &base_entry
)) {
1610 oe_set_type(entry
, entry
->in_pack_type
);
1611 SET_SIZE(entry
, in_pack_size
); /* delta size */
1612 SET_DELTA_SIZE(entry
, in_pack_size
);
1615 SET_DELTA(entry
, base_entry
);
1616 entry
->delta_sibling_idx
= base_entry
->delta_child_idx
;
1617 SET_DELTA_CHILD(base_entry
, entry
);
1619 SET_DELTA_EXT(entry
, base_ref
);
1622 unuse_pack(&w_curs
);
1626 if (oe_type(entry
)) {
1630 * This must be a delta and we already know what the
1631 * final object type is. Let's extract the actual
1632 * object size from the delta header.
1634 delta_pos
= entry
->in_pack_offset
+ entry
->in_pack_header_size
;
1635 canonical_size
= get_size_from_delta(p
, &w_curs
, delta_pos
);
1636 if (canonical_size
== 0)
1638 SET_SIZE(entry
, canonical_size
);
1639 unuse_pack(&w_curs
);
1644 * No choice but to fall back to the recursive delta walk
1645 * with sha1_object_info() to find about the object type
1649 unuse_pack(&w_curs
);
1653 oid_object_info(the_repository
, &entry
->idx
.oid
, &canonical_size
));
1654 if (entry
->type_valid
) {
1655 SET_SIZE(entry
, canonical_size
);
1658 * Bad object type is checked in prepare_pack(). This is
1659 * to permit a missing preferred base object to be ignored
1660 * as a preferred base. Doing so can result in a larger
1661 * pack file, but the transfer will still take place.
1666 static int pack_offset_sort(const void *_a
, const void *_b
)
1668 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1669 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1670 const struct packed_git
*a_in_pack
= IN_PACK(a
);
1671 const struct packed_git
*b_in_pack
= IN_PACK(b
);
1673 /* avoid filesystem trashing with loose objects */
1674 if (!a_in_pack
&& !b_in_pack
)
1675 return oidcmp(&a
->idx
.oid
, &b
->idx
.oid
);
1677 if (a_in_pack
< b_in_pack
)
1679 if (a_in_pack
> b_in_pack
)
1681 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1682 (a
->in_pack_offset
> b
->in_pack_offset
);
1686 * Drop an on-disk delta we were planning to reuse. Naively, this would
1687 * just involve blanking out the "delta" field, but we have to deal
1688 * with some extra book-keeping:
1690 * 1. Removing ourselves from the delta_sibling linked list.
1692 * 2. Updating our size/type to the non-delta representation. These were
1693 * either not recorded initially (size) or overwritten with the delta type
1694 * (type) when check_object() decided to reuse the delta.
1696 * 3. Resetting our delta depth, as we are now a base object.
1698 static void drop_reused_delta(struct object_entry
*entry
)
1700 unsigned *idx
= &to_pack
.objects
[entry
->delta_idx
- 1].delta_child_idx
;
1701 struct object_info oi
= OBJECT_INFO_INIT
;
1702 enum object_type type
;
1706 struct object_entry
*oe
= &to_pack
.objects
[*idx
- 1];
1709 *idx
= oe
->delta_sibling_idx
;
1711 idx
= &oe
->delta_sibling_idx
;
1713 SET_DELTA(entry
, NULL
);
1718 if (packed_object_info(the_repository
, IN_PACK(entry
), entry
->in_pack_offset
, &oi
) < 0) {
1720 * We failed to get the info from this pack for some reason;
1721 * fall back to sha1_object_info, which may find another copy.
1722 * And if that fails, the error will be recorded in oe_type(entry)
1723 * and dealt with in prepare_pack().
1726 oid_object_info(the_repository
, &entry
->idx
.oid
, &size
));
1728 oe_set_type(entry
, type
);
1730 SET_SIZE(entry
, size
);
1734 * Follow the chain of deltas from this entry onward, throwing away any links
1735 * that cause us to hit a cycle (as determined by the DFS state flags in
1738 * We also detect too-long reused chains that would violate our --depth
1741 static void break_delta_chains(struct object_entry
*entry
)
1744 * The actual depth of each object we will write is stored as an int,
1745 * as it cannot exceed our int "depth" limit. But before we break
1746 * changes based no that limit, we may potentially go as deep as the
1747 * number of objects, which is elsewhere bounded to a uint32_t.
1749 uint32_t total_depth
;
1750 struct object_entry
*cur
, *next
;
1752 for (cur
= entry
, total_depth
= 0;
1754 cur
= DELTA(cur
), total_depth
++) {
1755 if (cur
->dfs_state
== DFS_DONE
) {
1757 * We've already seen this object and know it isn't
1758 * part of a cycle. We do need to append its depth
1761 total_depth
+= cur
->depth
;
1766 * We break cycles before looping, so an ACTIVE state (or any
1767 * other cruft which made its way into the state variable)
1770 if (cur
->dfs_state
!= DFS_NONE
)
1771 BUG("confusing delta dfs state in first pass: %d",
1775 * Now we know this is the first time we've seen the object. If
1776 * it's not a delta, we're done traversing, but we'll mark it
1777 * done to save time on future traversals.
1780 cur
->dfs_state
= DFS_DONE
;
1785 * Mark ourselves as active and see if the next step causes
1786 * us to cycle to another active object. It's important to do
1787 * this _before_ we loop, because it impacts where we make the
1788 * cut, and thus how our total_depth counter works.
1789 * E.g., We may see a partial loop like:
1791 * A -> B -> C -> D -> B
1793 * Cutting B->C breaks the cycle. But now the depth of A is
1794 * only 1, and our total_depth counter is at 3. The size of the
1795 * error is always one less than the size of the cycle we
1796 * broke. Commits C and D were "lost" from A's chain.
1798 * If we instead cut D->B, then the depth of A is correct at 3.
1799 * We keep all commits in the chain that we examined.
1801 cur
->dfs_state
= DFS_ACTIVE
;
1802 if (DELTA(cur
)->dfs_state
== DFS_ACTIVE
) {
1803 drop_reused_delta(cur
);
1804 cur
->dfs_state
= DFS_DONE
;
1810 * And now that we've gone all the way to the bottom of the chain, we
1811 * need to clear the active flags and set the depth fields as
1812 * appropriate. Unlike the loop above, which can quit when it drops a
1813 * delta, we need to keep going to look for more depth cuts. So we need
1814 * an extra "next" pointer to keep going after we reset cur->delta.
1816 for (cur
= entry
; cur
; cur
= next
) {
1820 * We should have a chain of zero or more ACTIVE states down to
1821 * a final DONE. We can quit after the DONE, because either it
1822 * has no bases, or we've already handled them in a previous
1825 if (cur
->dfs_state
== DFS_DONE
)
1827 else if (cur
->dfs_state
!= DFS_ACTIVE
)
1828 BUG("confusing delta dfs state in second pass: %d",
1832 * If the total_depth is more than depth, then we need to snip
1833 * the chain into two or more smaller chains that don't exceed
1834 * the maximum depth. Most of the resulting chains will contain
1835 * (depth + 1) entries (i.e., depth deltas plus one base), and
1836 * the last chain (i.e., the one containing entry) will contain
1837 * whatever entries are left over, namely
1838 * (total_depth % (depth + 1)) of them.
1840 * Since we are iterating towards decreasing depth, we need to
1841 * decrement total_depth as we go, and we need to write to the
1842 * entry what its final depth will be after all of the
1843 * snipping. Since we're snipping into chains of length (depth
1844 * + 1) entries, the final depth of an entry will be its
1845 * original depth modulo (depth + 1). Any time we encounter an
1846 * entry whose final depth is supposed to be zero, we snip it
1847 * from its delta base, thereby making it so.
1849 cur
->depth
= (total_depth
--) % (depth
+ 1);
1851 drop_reused_delta(cur
);
1853 cur
->dfs_state
= DFS_DONE
;
1857 static void get_object_details(void)
1860 struct object_entry
**sorted_by_offset
;
1863 progress_state
= start_progress(_("Counting objects"),
1864 to_pack
.nr_objects
);
1866 sorted_by_offset
= xcalloc(to_pack
.nr_objects
, sizeof(struct object_entry
*));
1867 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
1868 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
1869 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
1871 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
1872 struct object_entry
*entry
= sorted_by_offset
[i
];
1873 check_object(entry
);
1874 if (entry
->type_valid
&&
1875 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
))
1876 entry
->no_try_delta
= 1;
1877 display_progress(progress_state
, i
+ 1);
1879 stop_progress(&progress_state
);
1882 * This must happen in a second pass, since we rely on the delta
1883 * information for the whole list being completed.
1885 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
1886 break_delta_chains(&to_pack
.objects
[i
]);
1888 free(sorted_by_offset
);
1892 * We search for deltas in a list sorted by type, by filename hash, and then
1893 * by size, so that we see progressively smaller and smaller files.
1894 * That's because we prefer deltas to be from the bigger file
1895 * to the smaller -- deletes are potentially cheaper, but perhaps
1896 * more importantly, the bigger file is likely the more recent
1897 * one. The deepest deltas are therefore the oldest objects which are
1898 * less susceptible to be accessed often.
1900 static int type_size_sort(const void *_a
, const void *_b
)
1902 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1903 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1904 enum object_type a_type
= oe_type(a
);
1905 enum object_type b_type
= oe_type(b
);
1906 unsigned long a_size
= SIZE(a
);
1907 unsigned long b_size
= SIZE(b
);
1909 if (a_type
> b_type
)
1911 if (a_type
< b_type
)
1913 if (a
->hash
> b
->hash
)
1915 if (a
->hash
< b
->hash
)
1917 if (a
->preferred_base
> b
->preferred_base
)
1919 if (a
->preferred_base
< b
->preferred_base
)
1921 if (use_delta_islands
) {
1922 int island_cmp
= island_delta_cmp(&a
->idx
.oid
, &b
->idx
.oid
);
1926 if (a_size
> b_size
)
1928 if (a_size
< b_size
)
1930 return a
< b
? -1 : (a
> b
); /* newest first */
1934 struct object_entry
*entry
;
1936 struct delta_index
*index
;
1940 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
1941 unsigned long delta_size
)
1943 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
1946 if (delta_size
< cache_max_small_delta_size
)
1949 /* cache delta, if objects are large enough compared to delta size */
1950 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
1958 /* Protect access to object database */
1959 static pthread_mutex_t read_mutex
;
1960 #define read_lock() pthread_mutex_lock(&read_mutex)
1961 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1963 /* Protect delta_cache_size */
1964 static pthread_mutex_t cache_mutex
;
1965 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1966 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1969 * Protect object list partitioning (e.g. struct thread_param) and
1972 static pthread_mutex_t progress_mutex
;
1973 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1974 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1977 * Access to struct object_entry is unprotected since each thread owns
1978 * a portion of the main object list. Just don't access object entries
1979 * ahead in the list because they can be stolen and would need
1980 * progress_mutex for protection.
1984 #define read_lock() (void)0
1985 #define read_unlock() (void)0
1986 #define cache_lock() (void)0
1987 #define cache_unlock() (void)0
1988 #define progress_lock() (void)0
1989 #define progress_unlock() (void)0
1994 * Return the size of the object without doing any delta
1995 * reconstruction (so non-deltas are true object sizes, but deltas
1996 * return the size of the delta data).
1998 unsigned long oe_get_size_slow(struct packing_data
*pack
,
1999 const struct object_entry
*e
)
2001 struct packed_git
*p
;
2002 struct pack_window
*w_curs
;
2004 enum object_type type
;
2005 unsigned long used
, avail
, size
;
2007 if (e
->type_
!= OBJ_OFS_DELTA
&& e
->type_
!= OBJ_REF_DELTA
) {
2009 if (oid_object_info(the_repository
, &e
->idx
.oid
, &size
) < 0)
2010 die(_("unable to get size of %s"),
2011 oid_to_hex(&e
->idx
.oid
));
2016 p
= oe_in_pack(pack
, e
);
2018 BUG("when e->type is a delta, it must belong to a pack");
2022 buf
= use_pack(p
, &w_curs
, e
->in_pack_offset
, &avail
);
2023 used
= unpack_object_header_buffer(buf
, avail
, &type
, &size
);
2025 die(_("unable to parse object header of %s"),
2026 oid_to_hex(&e
->idx
.oid
));
2028 unuse_pack(&w_curs
);
2033 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
2034 unsigned max_depth
, unsigned long *mem_usage
)
2036 struct object_entry
*trg_entry
= trg
->entry
;
2037 struct object_entry
*src_entry
= src
->entry
;
2038 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
2040 enum object_type type
;
2043 /* Don't bother doing diffs between different types */
2044 if (oe_type(trg_entry
) != oe_type(src_entry
))
2048 * We do not bother to try a delta that we discarded on an
2049 * earlier try, but only when reusing delta data. Note that
2050 * src_entry that is marked as the preferred_base should always
2051 * be considered, as even if we produce a suboptimal delta against
2052 * it, we will still save the transfer cost, as we already know
2053 * the other side has it and we won't send src_entry at all.
2055 if (reuse_delta
&& IN_PACK(trg_entry
) &&
2056 IN_PACK(trg_entry
) == IN_PACK(src_entry
) &&
2057 !src_entry
->preferred_base
&&
2058 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
2059 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
2062 /* Let's not bust the allowed depth. */
2063 if (src
->depth
>= max_depth
)
2066 /* Now some size filtering heuristics. */
2067 trg_size
= SIZE(trg_entry
);
2068 if (!DELTA(trg_entry
)) {
2069 max_size
= trg_size
/2 - the_hash_algo
->rawsz
;
2072 max_size
= DELTA_SIZE(trg_entry
);
2073 ref_depth
= trg
->depth
;
2075 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
2076 (max_depth
- ref_depth
+ 1);
2079 src_size
= SIZE(src_entry
);
2080 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
2081 if (sizediff
>= max_size
)
2083 if (trg_size
< src_size
/ 32)
2086 if (!in_same_island(&trg
->entry
->idx
.oid
, &src
->entry
->idx
.oid
))
2089 /* Load data if not already done */
2092 trg
->data
= read_object_file(&trg_entry
->idx
.oid
, &type
, &sz
);
2095 die(_("object %s cannot be read"),
2096 oid_to_hex(&trg_entry
->idx
.oid
));
2098 die(_("object %s inconsistent object length (%lu vs %lu)"),
2099 oid_to_hex(&trg_entry
->idx
.oid
), sz
,
2105 src
->data
= read_object_file(&src_entry
->idx
.oid
, &type
, &sz
);
2108 if (src_entry
->preferred_base
) {
2109 static int warned
= 0;
2111 warning(_("object %s cannot be read"),
2112 oid_to_hex(&src_entry
->idx
.oid
));
2114 * Those objects are not included in the
2115 * resulting pack. Be resilient and ignore
2116 * them if they can't be read, in case the
2117 * pack could be created nevertheless.
2121 die(_("object %s cannot be read"),
2122 oid_to_hex(&src_entry
->idx
.oid
));
2125 die(_("object %s inconsistent object length (%lu vs %lu)"),
2126 oid_to_hex(&src_entry
->idx
.oid
), sz
,
2131 src
->index
= create_delta_index(src
->data
, src_size
);
2133 static int warned
= 0;
2135 warning(_("suboptimal pack - out of memory"));
2138 *mem_usage
+= sizeof_delta_index(src
->index
);
2141 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
2145 if (DELTA(trg_entry
)) {
2146 /* Prefer only shallower same-sized deltas. */
2147 if (delta_size
== DELTA_SIZE(trg_entry
) &&
2148 src
->depth
+ 1 >= trg
->depth
) {
2155 * Handle memory allocation outside of the cache
2156 * accounting lock. Compiler will optimize the strangeness
2157 * away when NO_PTHREADS is defined.
2159 free(trg_entry
->delta_data
);
2161 if (trg_entry
->delta_data
) {
2162 delta_cache_size
-= DELTA_SIZE(trg_entry
);
2163 trg_entry
->delta_data
= NULL
;
2165 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
2166 delta_cache_size
+= delta_size
;
2168 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
2174 SET_DELTA(trg_entry
, src_entry
);
2175 SET_DELTA_SIZE(trg_entry
, delta_size
);
2176 trg
->depth
= src
->depth
+ 1;
2181 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
2183 struct object_entry
*child
= DELTA_CHILD(me
);
2186 unsigned int c
= check_delta_limit(child
, n
+ 1);
2189 child
= DELTA_SIBLING(child
);
2194 static unsigned long free_unpacked(struct unpacked
*n
)
2196 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
2197 free_delta_index(n
->index
);
2200 freed_mem
+= SIZE(n
->entry
);
2201 FREE_AND_NULL(n
->data
);
2208 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
2209 int window
, int depth
, unsigned *processed
)
2211 uint32_t i
, idx
= 0, count
= 0;
2212 struct unpacked
*array
;
2213 unsigned long mem_usage
= 0;
2215 array
= xcalloc(window
, sizeof(struct unpacked
));
2218 struct object_entry
*entry
;
2219 struct unpacked
*n
= array
+ idx
;
2220 int j
, max_depth
, best_base
= -1;
2229 if (!entry
->preferred_base
) {
2231 display_progress(progress_state
, *processed
);
2235 mem_usage
-= free_unpacked(n
);
2238 while (window_memory_limit
&&
2239 mem_usage
> window_memory_limit
&&
2241 uint32_t tail
= (idx
+ window
- count
) % window
;
2242 mem_usage
-= free_unpacked(array
+ tail
);
2246 /* We do not compute delta to *create* objects we are not
2249 if (entry
->preferred_base
)
2253 * If the current object is at pack edge, take the depth the
2254 * objects that depend on the current object into account
2255 * otherwise they would become too deep.
2258 if (DELTA_CHILD(entry
)) {
2259 max_depth
-= check_delta_limit(entry
, 0);
2267 uint32_t other_idx
= idx
+ j
;
2269 if (other_idx
>= window
)
2270 other_idx
-= window
;
2271 m
= array
+ other_idx
;
2274 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2278 best_base
= other_idx
;
2282 * If we decided to cache the delta data, then it is best
2283 * to compress it right away. First because we have to do
2284 * it anyway, and doing it here while we're threaded will
2285 * save a lot of time in the non threaded write phase,
2286 * as well as allow for caching more deltas within
2287 * the same cache size limit.
2289 * But only if not writing to stdout, since in that case
2290 * the network is most likely throttling writes anyway,
2291 * and therefore it is best to go to the write phase ASAP
2292 * instead, as we can afford spending more time compressing
2293 * between writes at that moment.
2295 if (entry
->delta_data
&& !pack_to_stdout
) {
2298 size
= do_compress(&entry
->delta_data
, DELTA_SIZE(entry
));
2299 if (size
< (1U << OE_Z_DELTA_BITS
)) {
2300 entry
->z_delta_size
= size
;
2302 delta_cache_size
-= DELTA_SIZE(entry
);
2303 delta_cache_size
+= entry
->z_delta_size
;
2306 FREE_AND_NULL(entry
->delta_data
);
2307 entry
->z_delta_size
= 0;
2311 /* if we made n a delta, and if n is already at max
2312 * depth, leaving it in the window is pointless. we
2313 * should evict it first.
2315 if (DELTA(entry
) && max_depth
<= n
->depth
)
2319 * Move the best delta base up in the window, after the
2320 * currently deltified object, to keep it longer. It will
2321 * be the first base object to be attempted next.
2324 struct unpacked swap
= array
[best_base
];
2325 int dist
= (window
+ idx
- best_base
) % window
;
2326 int dst
= best_base
;
2328 int src
= (dst
+ 1) % window
;
2329 array
[dst
] = array
[src
];
2337 if (count
+ 1 < window
)
2343 for (i
= 0; i
< window
; ++i
) {
2344 free_delta_index(array
[i
].index
);
2345 free(array
[i
].data
);
2352 static void try_to_free_from_threads(size_t size
)
2355 release_pack_memory(size
);
2359 static try_to_free_t old_try_to_free_routine
;
2362 * The main object list is split into smaller lists, each is handed to
2365 * The main thread waits on the condition that (at least) one of the workers
2366 * has stopped working (which is indicated in the .working member of
2367 * struct thread_params).
2369 * When a work thread has completed its work, it sets .working to 0 and
2370 * signals the main thread and waits on the condition that .data_ready
2373 * The main thread steals half of the work from the worker that has
2374 * most work left to hand it to the idle worker.
2377 struct thread_params
{
2379 struct object_entry
**list
;
2386 pthread_mutex_t mutex
;
2387 pthread_cond_t cond
;
2388 unsigned *processed
;
2391 static pthread_cond_t progress_cond
;
2394 * Mutex and conditional variable can't be statically-initialized on Windows.
2396 static void init_threaded_search(void)
2398 init_recursive_mutex(&read_mutex
);
2399 pthread_mutex_init(&cache_mutex
, NULL
);
2400 pthread_mutex_init(&progress_mutex
, NULL
);
2401 pthread_cond_init(&progress_cond
, NULL
);
2402 old_try_to_free_routine
= set_try_to_free_routine(try_to_free_from_threads
);
2405 static void cleanup_threaded_search(void)
2407 set_try_to_free_routine(old_try_to_free_routine
);
2408 pthread_cond_destroy(&progress_cond
);
2409 pthread_mutex_destroy(&read_mutex
);
2410 pthread_mutex_destroy(&cache_mutex
);
2411 pthread_mutex_destroy(&progress_mutex
);
2414 static void *threaded_find_deltas(void *arg
)
2416 struct thread_params
*me
= arg
;
2419 while (me
->remaining
) {
2422 find_deltas(me
->list
, &me
->remaining
,
2423 me
->window
, me
->depth
, me
->processed
);
2427 pthread_cond_signal(&progress_cond
);
2431 * We must not set ->data_ready before we wait on the
2432 * condition because the main thread may have set it to 1
2433 * before we get here. In order to be sure that new
2434 * work is available if we see 1 in ->data_ready, it
2435 * was initialized to 0 before this thread was spawned
2436 * and we reset it to 0 right away.
2438 pthread_mutex_lock(&me
->mutex
);
2439 while (!me
->data_ready
)
2440 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2442 pthread_mutex_unlock(&me
->mutex
);
2447 /* leave ->working 1 so that this doesn't get more work assigned */
2451 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2452 int window
, int depth
, unsigned *processed
)
2454 struct thread_params
*p
;
2455 int i
, ret
, active_threads
= 0;
2457 init_threaded_search();
2459 if (delta_search_threads
<= 1) {
2460 find_deltas(list
, &list_size
, window
, depth
, processed
);
2461 cleanup_threaded_search();
2464 if (progress
> pack_to_stdout
)
2465 fprintf_ln(stderr
, _("Delta compression using up to %d threads"),
2466 delta_search_threads
);
2467 p
= xcalloc(delta_search_threads
, sizeof(*p
));
2469 /* Partition the work amongst work threads. */
2470 for (i
= 0; i
< delta_search_threads
; i
++) {
2471 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
2473 /* don't use too small segments or no deltas will be found */
2474 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
2477 p
[i
].window
= window
;
2479 p
[i
].processed
= processed
;
2481 p
[i
].data_ready
= 0;
2483 /* try to split chunks on "path" boundaries */
2484 while (sub_size
&& sub_size
< list_size
&&
2485 list
[sub_size
]->hash
&&
2486 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
2490 p
[i
].list_size
= sub_size
;
2491 p
[i
].remaining
= sub_size
;
2494 list_size
-= sub_size
;
2497 /* Start work threads. */
2498 for (i
= 0; i
< delta_search_threads
; i
++) {
2499 if (!p
[i
].list_size
)
2501 pthread_mutex_init(&p
[i
].mutex
, NULL
);
2502 pthread_cond_init(&p
[i
].cond
, NULL
);
2503 ret
= pthread_create(&p
[i
].thread
, NULL
,
2504 threaded_find_deltas
, &p
[i
]);
2506 die(_("unable to create thread: %s"), strerror(ret
));
2511 * Now let's wait for work completion. Each time a thread is done
2512 * with its work, we steal half of the remaining work from the
2513 * thread with the largest number of unprocessed objects and give
2514 * it to that newly idle thread. This ensure good load balancing
2515 * until the remaining object list segments are simply too short
2516 * to be worth splitting anymore.
2518 while (active_threads
) {
2519 struct thread_params
*target
= NULL
;
2520 struct thread_params
*victim
= NULL
;
2521 unsigned sub_size
= 0;
2525 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
2530 pthread_cond_wait(&progress_cond
, &progress_mutex
);
2533 for (i
= 0; i
< delta_search_threads
; i
++)
2534 if (p
[i
].remaining
> 2*window
&&
2535 (!victim
|| victim
->remaining
< p
[i
].remaining
))
2538 sub_size
= victim
->remaining
/ 2;
2539 list
= victim
->list
+ victim
->list_size
- sub_size
;
2540 while (sub_size
&& list
[0]->hash
&&
2541 list
[0]->hash
== list
[-1]->hash
) {
2547 * It is possible for some "paths" to have
2548 * so many objects that no hash boundary
2549 * might be found. Let's just steal the
2550 * exact half in that case.
2552 sub_size
= victim
->remaining
/ 2;
2555 target
->list
= list
;
2556 victim
->list_size
-= sub_size
;
2557 victim
->remaining
-= sub_size
;
2559 target
->list_size
= sub_size
;
2560 target
->remaining
= sub_size
;
2561 target
->working
= 1;
2564 pthread_mutex_lock(&target
->mutex
);
2565 target
->data_ready
= 1;
2566 pthread_cond_signal(&target
->cond
);
2567 pthread_mutex_unlock(&target
->mutex
);
2570 pthread_join(target
->thread
, NULL
);
2571 pthread_cond_destroy(&target
->cond
);
2572 pthread_mutex_destroy(&target
->mutex
);
2576 cleanup_threaded_search();
2581 #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
2584 static void add_tag_chain(const struct object_id
*oid
)
2589 * We catch duplicates already in add_object_entry(), but we'd
2590 * prefer to do this extra check to avoid having to parse the
2591 * tag at all if we already know that it's being packed (e.g., if
2592 * it was included via bitmaps, we would not have parsed it
2595 if (packlist_find(&to_pack
, oid
->hash
, NULL
))
2598 tag
= lookup_tag(the_repository
, oid
);
2600 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
2601 die(_("unable to pack objects reachable from tag %s"),
2604 add_object_entry(&tag
->object
.oid
, OBJ_TAG
, NULL
, 0);
2606 if (tag
->tagged
->type
!= OBJ_TAG
)
2609 tag
= (struct tag
*)tag
->tagged
;
2613 static int add_ref_tag(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
2615 struct object_id peeled
;
2617 if (starts_with(path
, "refs/tags/") && /* is a tag? */
2618 !peel_ref(path
, &peeled
) && /* peelable? */
2619 packlist_find(&to_pack
, peeled
.hash
, NULL
)) /* object packed? */
2624 static void prepare_pack(int window
, int depth
)
2626 struct object_entry
**delta_list
;
2627 uint32_t i
, nr_deltas
;
2630 if (use_delta_islands
)
2631 resolve_tree_islands(progress
, &to_pack
);
2633 get_object_details();
2636 * If we're locally repacking then we need to be doubly careful
2637 * from now on in order to make sure no stealth corruption gets
2638 * propagated to the new pack. Clients receiving streamed packs
2639 * should validate everything they get anyway so no need to incur
2640 * the additional cost here in that case.
2642 if (!pack_to_stdout
)
2643 do_check_packed_object_crc
= 1;
2645 if (!to_pack
.nr_objects
|| !window
|| !depth
)
2648 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
2651 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2652 struct object_entry
*entry
= to_pack
.objects
+ i
;
2655 /* This happens if we decided to reuse existing
2656 * delta from a pack. "reuse_delta &&" is implied.
2660 if (!entry
->type_valid
||
2661 oe_size_less_than(&to_pack
, entry
, 50))
2664 if (entry
->no_try_delta
)
2667 if (!entry
->preferred_base
) {
2669 if (oe_type(entry
) < 0)
2670 die(_("unable to get type of object %s"),
2671 oid_to_hex(&entry
->idx
.oid
));
2673 if (oe_type(entry
) < 0) {
2675 * This object is not found, but we
2676 * don't have to include it anyway.
2682 delta_list
[n
++] = entry
;
2685 if (nr_deltas
&& n
> 1) {
2686 unsigned nr_done
= 0;
2688 progress_state
= start_progress(_("Compressing objects"),
2690 QSORT(delta_list
, n
, type_size_sort
);
2691 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
2692 stop_progress(&progress_state
);
2693 if (nr_done
!= nr_deltas
)
2694 die(_("inconsistency with delta count"));
2699 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2701 if (!strcmp(k
, "pack.window")) {
2702 window
= git_config_int(k
, v
);
2705 if (!strcmp(k
, "pack.windowmemory")) {
2706 window_memory_limit
= git_config_ulong(k
, v
);
2709 if (!strcmp(k
, "pack.depth")) {
2710 depth
= git_config_int(k
, v
);
2713 if (!strcmp(k
, "pack.deltacachesize")) {
2714 max_delta_cache_size
= git_config_int(k
, v
);
2717 if (!strcmp(k
, "pack.deltacachelimit")) {
2718 cache_max_small_delta_size
= git_config_int(k
, v
);
2721 if (!strcmp(k
, "pack.writebitmaphashcache")) {
2722 if (git_config_bool(k
, v
))
2723 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
2725 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
2727 if (!strcmp(k
, "pack.usebitmaps")) {
2728 use_bitmap_index_default
= git_config_bool(k
, v
);
2731 if (!strcmp(k
, "pack.threads")) {
2732 delta_search_threads
= git_config_int(k
, v
);
2733 if (delta_search_threads
< 0)
2734 die(_("invalid number of threads specified (%d)"),
2735 delta_search_threads
);
2737 if (delta_search_threads
!= 1) {
2738 warning(_("no threads support, ignoring %s"), k
);
2739 delta_search_threads
= 0;
2744 if (!strcmp(k
, "pack.indexversion")) {
2745 pack_idx_opts
.version
= git_config_int(k
, v
);
2746 if (pack_idx_opts
.version
> 2)
2747 die(_("bad pack.indexversion=%"PRIu32
),
2748 pack_idx_opts
.version
);
2751 return git_default_config(k
, v
, cb
);
2754 static void read_object_list_from_stdin(void)
2756 char line
[GIT_MAX_HEXSZ
+ 1 + PATH_MAX
+ 2];
2757 struct object_id oid
;
2761 if (!fgets(line
, sizeof(line
), stdin
)) {
2765 die("BUG: fgets returned NULL, not EOF, not error!");
2771 if (line
[0] == '-') {
2772 if (get_oid_hex(line
+1, &oid
))
2773 die(_("expected edge object ID, got garbage:\n %s"),
2775 add_preferred_base(&oid
);
2778 if (parse_oid_hex(line
, &oid
, &p
))
2779 die(_("expected object ID, got garbage:\n %s"), line
);
2781 add_preferred_base_object(p
+ 1);
2782 add_object_entry(&oid
, OBJ_NONE
, p
+ 1, 0);
2786 /* Remember to update object flag allocation in object.h */
2787 #define OBJECT_ADDED (1u<<20)
2789 static void show_commit(struct commit
*commit
, void *data
)
2791 add_object_entry(&commit
->object
.oid
, OBJ_COMMIT
, NULL
, 0);
2792 commit
->object
.flags
|= OBJECT_ADDED
;
2794 if (write_bitmap_index
)
2795 index_commit_for_bitmap(commit
);
2797 if (use_delta_islands
)
2798 propagate_island_marks(commit
);
2801 static void show_object(struct object
*obj
, const char *name
, void *data
)
2803 add_preferred_base_object(name
);
2804 add_object_entry(&obj
->oid
, obj
->type
, name
, 0);
2805 obj
->flags
|= OBJECT_ADDED
;
2807 if (use_delta_islands
) {
2810 struct object_entry
*ent
;
2812 for (p
= strchr(name
, '/'); p
; p
= strchr(p
+ 1, '/'))
2815 ent
= packlist_find(&to_pack
, obj
->oid
.hash
, NULL
);
2816 if (ent
&& depth
> oe_tree_depth(&to_pack
, ent
))
2817 oe_set_tree_depth(&to_pack
, ent
, depth
);
2821 static void show_object__ma_allow_any(struct object
*obj
, const char *name
, void *data
)
2823 assert(arg_missing_action
== MA_ALLOW_ANY
);
2826 * Quietly ignore ALL missing objects. This avoids problems with
2827 * staging them now and getting an odd error later.
2829 if (!has_object_file(&obj
->oid
))
2832 show_object(obj
, name
, data
);
2835 static void show_object__ma_allow_promisor(struct object
*obj
, const char *name
, void *data
)
2837 assert(arg_missing_action
== MA_ALLOW_PROMISOR
);
2840 * Quietly ignore EXPECTED missing objects. This avoids problems with
2841 * staging them now and getting an odd error later.
2843 if (!has_object_file(&obj
->oid
) && is_promisor_object(&obj
->oid
))
2846 show_object(obj
, name
, data
);
2849 static int option_parse_missing_action(const struct option
*opt
,
2850 const char *arg
, int unset
)
2855 if (!strcmp(arg
, "error")) {
2856 arg_missing_action
= MA_ERROR
;
2857 fn_show_object
= show_object
;
2861 if (!strcmp(arg
, "allow-any")) {
2862 arg_missing_action
= MA_ALLOW_ANY
;
2863 fetch_if_missing
= 0;
2864 fn_show_object
= show_object__ma_allow_any
;
2868 if (!strcmp(arg
, "allow-promisor")) {
2869 arg_missing_action
= MA_ALLOW_PROMISOR
;
2870 fetch_if_missing
= 0;
2871 fn_show_object
= show_object__ma_allow_promisor
;
2875 die(_("invalid value for --missing"));
2879 static void show_edge(struct commit
*commit
)
2881 add_preferred_base(&commit
->object
.oid
);
2884 struct in_pack_object
{
2886 struct object
*object
;
2892 struct in_pack_object
*array
;
2895 static void mark_in_pack_object(struct object
*object
, struct packed_git
*p
, struct in_pack
*in_pack
)
2897 in_pack
->array
[in_pack
->nr
].offset
= find_pack_entry_one(object
->oid
.hash
, p
);
2898 in_pack
->array
[in_pack
->nr
].object
= object
;
2903 * Compare the objects in the offset order, in order to emulate the
2904 * "git rev-list --objects" output that produced the pack originally.
2906 static int ofscmp(const void *a_
, const void *b_
)
2908 struct in_pack_object
*a
= (struct in_pack_object
*)a_
;
2909 struct in_pack_object
*b
= (struct in_pack_object
*)b_
;
2911 if (a
->offset
< b
->offset
)
2913 else if (a
->offset
> b
->offset
)
2916 return oidcmp(&a
->object
->oid
, &b
->object
->oid
);
2919 static void add_objects_in_unpacked_packs(struct rev_info
*revs
)
2921 struct packed_git
*p
;
2922 struct in_pack in_pack
;
2925 memset(&in_pack
, 0, sizeof(in_pack
));
2927 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
2928 struct object_id oid
;
2931 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
2933 if (open_pack_index(p
))
2934 die(_("cannot open pack index"));
2936 ALLOC_GROW(in_pack
.array
,
2937 in_pack
.nr
+ p
->num_objects
,
2940 for (i
= 0; i
< p
->num_objects
; i
++) {
2941 nth_packed_object_oid(&oid
, p
, i
);
2942 o
= lookup_unknown_object(oid
.hash
);
2943 if (!(o
->flags
& OBJECT_ADDED
))
2944 mark_in_pack_object(o
, p
, &in_pack
);
2945 o
->flags
|= OBJECT_ADDED
;
2950 QSORT(in_pack
.array
, in_pack
.nr
, ofscmp
);
2951 for (i
= 0; i
< in_pack
.nr
; i
++) {
2952 struct object
*o
= in_pack
.array
[i
].object
;
2953 add_object_entry(&o
->oid
, o
->type
, "", 0);
2956 free(in_pack
.array
);
2959 static int add_loose_object(const struct object_id
*oid
, const char *path
,
2962 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
2965 warning(_("loose object at %s could not be examined"), path
);
2969 add_object_entry(oid
, type
, "", 0);
2974 * We actually don't even have to worry about reachability here.
2975 * add_object_entry will weed out duplicates, so we just add every
2976 * loose object we find.
2978 static void add_unreachable_loose_objects(void)
2980 for_each_loose_file_in_objdir(get_object_directory(),
2985 static int has_sha1_pack_kept_or_nonlocal(const struct object_id
*oid
)
2987 static struct packed_git
*last_found
= (void *)1;
2988 struct packed_git
*p
;
2990 p
= (last_found
!= (void *)1) ? last_found
:
2991 get_all_packs(the_repository
);
2994 if ((!p
->pack_local
|| p
->pack_keep
||
2995 p
->pack_keep_in_core
) &&
2996 find_pack_entry_one(oid
->hash
, p
)) {
3000 if (p
== last_found
)
3001 p
= get_all_packs(the_repository
);
3004 if (p
== last_found
)
3011 * Store a list of sha1s that are should not be discarded
3012 * because they are either written too recently, or are
3013 * reachable from another object that was.
3015 * This is filled by get_object_list.
3017 static struct oid_array recent_objects
;
3019 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
3022 if (!unpack_unreachable_expiration
)
3024 if (mtime
> unpack_unreachable_expiration
)
3026 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
3031 static void loosen_unused_packed_objects(struct rev_info
*revs
)
3033 struct packed_git
*p
;
3035 struct object_id oid
;
3037 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3038 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
3041 if (open_pack_index(p
))
3042 die(_("cannot open pack index"));
3044 for (i
= 0; i
< p
->num_objects
; i
++) {
3045 nth_packed_object_oid(&oid
, p
, i
);
3046 if (!packlist_find(&to_pack
, oid
.hash
, NULL
) &&
3047 !has_sha1_pack_kept_or_nonlocal(&oid
) &&
3048 !loosened_object_can_be_discarded(&oid
, p
->mtime
))
3049 if (force_object_loose(&oid
, p
->mtime
))
3050 die(_("unable to force loose object"));
3056 * This tracks any options which pack-reuse code expects to be on, or which a
3057 * reader of the pack might not understand, and which would therefore prevent
3058 * blind reuse of what we have on disk.
3060 static int pack_options_allow_reuse(void)
3062 return pack_to_stdout
&&
3064 !ignore_packed_keep_on_disk
&&
3065 !ignore_packed_keep_in_core
&&
3066 (!local
|| !have_non_local_packs
) &&
3070 static int get_object_list_from_bitmap(struct rev_info
*revs
)
3072 if (!(bitmap_git
= prepare_bitmap_walk(revs
)))
3075 if (pack_options_allow_reuse() &&
3076 !reuse_partial_packfile_from_bitmap(
3079 &reuse_packfile_objects
,
3080 &reuse_packfile_offset
)) {
3081 assert(reuse_packfile_objects
);
3082 nr_result
+= reuse_packfile_objects
;
3083 display_progress(progress_state
, nr_result
);
3086 traverse_bitmap_commit_list(bitmap_git
, &add_object_entry_from_bitmap
);
3090 static void record_recent_object(struct object
*obj
,
3094 oid_array_append(&recent_objects
, &obj
->oid
);
3097 static void record_recent_commit(struct commit
*commit
, void *data
)
3099 oid_array_append(&recent_objects
, &commit
->object
.oid
);
3102 static void get_object_list(int ac
, const char **av
)
3104 struct rev_info revs
;
3108 repo_init_revisions(the_repository
, &revs
, NULL
);
3109 save_commit_buffer
= 0;
3110 setup_revisions(ac
, av
, &revs
, NULL
);
3112 /* make sure shallows are read */
3113 is_repository_shallow(the_repository
);
3115 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
3116 int len
= strlen(line
);
3117 if (len
&& line
[len
- 1] == '\n')
3122 if (!strcmp(line
, "--not")) {
3123 flags
^= UNINTERESTING
;
3124 write_bitmap_index
= 0;
3127 if (starts_with(line
, "--shallow ")) {
3128 struct object_id oid
;
3129 if (get_oid_hex(line
+ 10, &oid
))
3130 die("not an SHA-1 '%s'", line
+ 10);
3131 register_shallow(the_repository
, &oid
);
3132 use_bitmap_index
= 0;
3135 die(_("not a rev '%s'"), line
);
3137 if (handle_revision_arg(line
, &revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
3138 die(_("bad revision '%s'"), line
);
3141 if (use_bitmap_index
&& !get_object_list_from_bitmap(&revs
))
3144 if (use_delta_islands
)
3145 load_delta_islands();
3147 if (prepare_revision_walk(&revs
))
3148 die(_("revision walk setup failed"));
3149 mark_edges_uninteresting(&revs
, show_edge
);
3151 if (!fn_show_object
)
3152 fn_show_object
= show_object
;
3153 traverse_commit_list_filtered(&filter_options
, &revs
,
3154 show_commit
, fn_show_object
, NULL
,
3157 if (unpack_unreachable_expiration
) {
3158 revs
.ignore_missing_links
= 1;
3159 if (add_unseen_recent_objects_to_traversal(&revs
,
3160 unpack_unreachable_expiration
))
3161 die(_("unable to add recent objects"));
3162 if (prepare_revision_walk(&revs
))
3163 die(_("revision walk setup failed"));
3164 traverse_commit_list(&revs
, record_recent_commit
,
3165 record_recent_object
, NULL
);
3168 if (keep_unreachable
)
3169 add_objects_in_unpacked_packs(&revs
);
3170 if (pack_loose_unreachable
)
3171 add_unreachable_loose_objects();
3172 if (unpack_unreachable
)
3173 loosen_unused_packed_objects(&revs
);
3175 oid_array_clear(&recent_objects
);
3178 static void add_extra_kept_packs(const struct string_list
*names
)
3180 struct packed_git
*p
;
3185 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3186 const char *name
= basename(p
->pack_name
);
3192 for (i
= 0; i
< names
->nr
; i
++)
3193 if (!fspathcmp(name
, names
->items
[i
].string
))
3196 if (i
< names
->nr
) {
3197 p
->pack_keep_in_core
= 1;
3198 ignore_packed_keep_in_core
= 1;
3204 static int option_parse_index_version(const struct option
*opt
,
3205 const char *arg
, int unset
)
3208 const char *val
= arg
;
3209 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
3210 if (pack_idx_opts
.version
> 2)
3211 die(_("unsupported index version %s"), val
);
3212 if (*c
== ',' && c
[1])
3213 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
3214 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
3215 die(_("bad index version '%s'"), val
);
3219 static int option_parse_unpack_unreachable(const struct option
*opt
,
3220 const char *arg
, int unset
)
3223 unpack_unreachable
= 0;
3224 unpack_unreachable_expiration
= 0;
3227 unpack_unreachable
= 1;
3229 unpack_unreachable_expiration
= approxidate(arg
);
3234 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
3236 int use_internal_rev_list
= 0;
3238 int all_progress_implied
= 0;
3239 struct argv_array rp
= ARGV_ARRAY_INIT
;
3240 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
3241 int rev_list_index
= 0;
3242 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
3243 struct option pack_objects_options
[] = {
3244 OPT_SET_INT('q', "quiet", &progress
,
3245 N_("do not show progress meter"), 0),
3246 OPT_SET_INT(0, "progress", &progress
,
3247 N_("show progress meter"), 1),
3248 OPT_SET_INT(0, "all-progress", &progress
,
3249 N_("show progress meter during object writing phase"), 2),
3250 OPT_BOOL(0, "all-progress-implied",
3251 &all_progress_implied
,
3252 N_("similar to --all-progress when progress meter is shown")),
3253 { OPTION_CALLBACK
, 0, "index-version", NULL
, N_("<version>[,<offset>]"),
3254 N_("write the pack index file in the specified idx format version"),
3255 PARSE_OPT_NONEG
, option_parse_index_version
},
3256 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
3257 N_("maximum size of each output pack file")),
3258 OPT_BOOL(0, "local", &local
,
3259 N_("ignore borrowed objects from alternate object store")),
3260 OPT_BOOL(0, "incremental", &incremental
,
3261 N_("ignore packed objects")),
3262 OPT_INTEGER(0, "window", &window
,
3263 N_("limit pack window by objects")),
3264 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
3265 N_("limit pack window by memory in addition to object limit")),
3266 OPT_INTEGER(0, "depth", &depth
,
3267 N_("maximum length of delta chain allowed in the resulting pack")),
3268 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
3269 N_("reuse existing deltas")),
3270 OPT_BOOL(0, "reuse-object", &reuse_object
,
3271 N_("reuse existing objects")),
3272 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
3273 N_("use OFS_DELTA objects")),
3274 OPT_INTEGER(0, "threads", &delta_search_threads
,
3275 N_("use threads when searching for best delta matches")),
3276 OPT_BOOL(0, "non-empty", &non_empty
,
3277 N_("do not create an empty pack output")),
3278 OPT_BOOL(0, "revs", &use_internal_rev_list
,
3279 N_("read revision arguments from standard input")),
3280 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked
,
3281 N_("limit the objects to those that are not yet packed"),
3282 1, PARSE_OPT_NONEG
),
3283 OPT_SET_INT_F(0, "all", &rev_list_all
,
3284 N_("include objects reachable from any reference"),
3285 1, PARSE_OPT_NONEG
),
3286 OPT_SET_INT_F(0, "reflog", &rev_list_reflog
,
3287 N_("include objects referred by reflog entries"),
3288 1, PARSE_OPT_NONEG
),
3289 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index
,
3290 N_("include objects referred to by the index"),
3291 1, PARSE_OPT_NONEG
),
3292 OPT_BOOL(0, "stdout", &pack_to_stdout
,
3293 N_("output pack to stdout")),
3294 OPT_BOOL(0, "include-tag", &include_tag
,
3295 N_("include tag objects that refer to objects to be packed")),
3296 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
3297 N_("keep unreachable objects")),
3298 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
3299 N_("pack loose unreachable objects")),
3300 { OPTION_CALLBACK
, 0, "unpack-unreachable", NULL
, N_("time"),
3301 N_("unpack unreachable objects newer than <time>"),
3302 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
},
3303 OPT_BOOL(0, "thin", &thin
,
3304 N_("create thin packs")),
3305 OPT_BOOL(0, "shallow", &shallow
,
3306 N_("create packs suitable for shallow fetches")),
3307 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk
,
3308 N_("ignore packs that have companion .keep file")),
3309 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
3310 N_("ignore this pack")),
3311 OPT_INTEGER(0, "compression", &pack_compression_level
,
3312 N_("pack compression level")),
3313 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents
,
3314 N_("do not hide commits by grafts"), 0),
3315 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
3316 N_("use a bitmap index if available to speed up counting objects")),
3317 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index
,
3318 N_("write a bitmap index together with the pack index")),
3319 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
3320 { OPTION_CALLBACK
, 0, "missing", NULL
, N_("action"),
3321 N_("handling for missing objects"), PARSE_OPT_NONEG
,
3322 option_parse_missing_action
},
3323 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
3324 N_("do not pack objects in promisor packfiles")),
3325 OPT_BOOL(0, "delta-islands", &use_delta_islands
,
3326 N_("respect islands during delta compression")),
3330 if (DFS_NUM_STATES
> (1 << OE_DFS_STATE_BITS
))
3331 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
3333 read_replace_refs
= 0;
3335 reset_pack_idx_option(&pack_idx_opts
);
3336 git_config(git_pack_config
, NULL
);
3338 progress
= isatty(2);
3339 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
3343 base_name
= argv
[0];
3346 if (pack_to_stdout
!= !base_name
|| argc
)
3347 usage_with_options(pack_usage
, pack_objects_options
);
3349 if (depth
>= (1 << OE_DEPTH_BITS
)) {
3350 warning(_("delta chain depth %d is too deep, forcing %d"),
3351 depth
, (1 << OE_DEPTH_BITS
) - 1);
3352 depth
= (1 << OE_DEPTH_BITS
) - 1;
3354 if (cache_max_small_delta_size
>= (1U << OE_Z_DELTA_BITS
)) {
3355 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
3356 (1U << OE_Z_DELTA_BITS
) - 1);
3357 cache_max_small_delta_size
= (1U << OE_Z_DELTA_BITS
) - 1;
3360 argv_array_push(&rp
, "pack-objects");
3362 use_internal_rev_list
= 1;
3363 argv_array_push(&rp
, shallow
3364 ? "--objects-edge-aggressive"
3365 : "--objects-edge");
3367 argv_array_push(&rp
, "--objects");
3370 use_internal_rev_list
= 1;
3371 argv_array_push(&rp
, "--all");
3373 if (rev_list_reflog
) {
3374 use_internal_rev_list
= 1;
3375 argv_array_push(&rp
, "--reflog");
3377 if (rev_list_index
) {
3378 use_internal_rev_list
= 1;
3379 argv_array_push(&rp
, "--indexed-objects");
3381 if (rev_list_unpacked
) {
3382 use_internal_rev_list
= 1;
3383 argv_array_push(&rp
, "--unpacked");
3386 if (exclude_promisor_objects
) {
3387 use_internal_rev_list
= 1;
3388 fetch_if_missing
= 0;
3389 argv_array_push(&rp
, "--exclude-promisor-objects");
3391 if (unpack_unreachable
|| keep_unreachable
|| pack_loose_unreachable
)
3392 use_internal_rev_list
= 1;
3396 if (pack_compression_level
== -1)
3397 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
3398 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
3399 die(_("bad pack compression level %d"), pack_compression_level
);
3401 if (!delta_search_threads
) /* --threads=0 means autodetect */
3402 delta_search_threads
= online_cpus();
3405 if (delta_search_threads
!= 1)
3406 warning(_("no threads support, ignoring --threads"));
3408 if (!pack_to_stdout
&& !pack_size_limit
)
3409 pack_size_limit
= pack_size_limit_cfg
;
3410 if (pack_to_stdout
&& pack_size_limit
)
3411 die(_("--max-pack-size cannot be used to build a pack for transfer"));
3412 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
3413 warning(_("minimum pack size limit is 1 MiB"));
3414 pack_size_limit
= 1024*1024;
3417 if (!pack_to_stdout
&& thin
)
3418 die(_("--thin cannot be used to build an indexable pack"));
3420 if (keep_unreachable
&& unpack_unreachable
)
3421 die(_("--keep-unreachable and --unpack-unreachable are incompatible"));
3422 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
3423 unpack_unreachable_expiration
= 0;
3425 if (filter_options
.choice
) {
3426 if (!pack_to_stdout
)
3427 die(_("cannot use --filter without --stdout"));
3428 use_bitmap_index
= 0;
3432 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
3434 * - to produce good pack (with bitmap index not-yet-packed objects are
3435 * packed in suboptimal order).
3437 * - to use more robust pack-generation codepath (avoiding possible
3438 * bugs in bitmap code and possible bitmap index corruption).
3440 if (!pack_to_stdout
)
3441 use_bitmap_index_default
= 0;
3443 if (use_bitmap_index
< 0)
3444 use_bitmap_index
= use_bitmap_index_default
;
3446 /* "hard" reasons not to use bitmaps; these just won't work at all */
3447 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow(the_repository
))
3448 use_bitmap_index
= 0;
3450 if (pack_to_stdout
|| !rev_list_all
)
3451 write_bitmap_index
= 0;
3453 if (use_delta_islands
)
3454 argv_array_push(&rp
, "--topo-order");
3456 if (progress
&& all_progress_implied
)
3459 add_extra_kept_packs(&keep_pack_list
);
3460 if (ignore_packed_keep_on_disk
) {
3461 struct packed_git
*p
;
3462 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
3463 if (p
->pack_local
&& p
->pack_keep
)
3465 if (!p
) /* no keep-able packs found */
3466 ignore_packed_keep_on_disk
= 0;
3470 * unlike ignore_packed_keep_on_disk above, we do not
3471 * want to unset "local" based on looking at packs, as
3472 * it also covers non-local objects
3474 struct packed_git
*p
;
3475 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3476 if (!p
->pack_local
) {
3477 have_non_local_packs
= 1;
3483 prepare_packing_data(&to_pack
);
3486 progress_state
= start_progress(_("Enumerating objects"), 0);
3487 if (!use_internal_rev_list
)
3488 read_object_list_from_stdin();
3490 get_object_list(rp
.argc
, rp
.argv
);
3491 argv_array_clear(&rp
);
3493 cleanup_preferred_base();
3494 if (include_tag
&& nr_result
)
3495 for_each_ref(add_ref_tag
, NULL
);
3496 stop_progress(&progress_state
);
3498 if (non_empty
&& !nr_result
)
3501 prepare_pack(window
, depth
);
3505 _("Total %"PRIu32
" (delta %"PRIu32
"),"
3506 " reused %"PRIu32
" (delta %"PRIu32
")"),
3507 written
, written_delta
, reused
, reused_delta
);