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"
38 #define IN_PACK(obj) oe_in_pack(&to_pack, obj)
39 #define SIZE(obj) oe_size(&to_pack, obj)
40 #define SET_SIZE(obj,size) oe_set_size(&to_pack, obj, size)
41 #define DELTA_SIZE(obj) oe_delta_size(&to_pack, obj)
42 #define DELTA(obj) oe_delta(&to_pack, obj)
43 #define DELTA_CHILD(obj) oe_delta_child(&to_pack, obj)
44 #define DELTA_SIBLING(obj) oe_delta_sibling(&to_pack, obj)
45 #define SET_DELTA(obj, val) oe_set_delta(&to_pack, obj, val)
46 #define SET_DELTA_EXT(obj, oid) oe_set_delta_ext(&to_pack, obj, oid)
47 #define SET_DELTA_SIZE(obj, val) oe_set_delta_size(&to_pack, obj, val)
48 #define SET_DELTA_CHILD(obj, val) oe_set_delta_child(&to_pack, obj, val)
49 #define SET_DELTA_SIBLING(obj, val) oe_set_delta_sibling(&to_pack, obj, val)
51 static const char *pack_usage
[] = {
52 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
53 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
58 * Objects we are going to pack are collected in the `to_pack` structure.
59 * It contains an array (dynamically expanded) of the object data, and a map
60 * that can resolve SHA1s to their position in the array.
62 static struct packing_data to_pack
;
64 static struct pack_idx_entry
**written_list
;
65 static uint32_t nr_result
, nr_written
, nr_seen
;
66 static struct bitmap_index
*bitmap_git
;
67 static uint32_t write_layer
;
70 static int reuse_delta
= 1, reuse_object
= 1;
71 static int keep_unreachable
, unpack_unreachable
, include_tag
;
72 static timestamp_t unpack_unreachable_expiration
;
73 static int pack_loose_unreachable
;
75 static int have_non_local_packs
;
76 static int incremental
;
77 static int ignore_packed_keep_on_disk
;
78 static int ignore_packed_keep_in_core
;
79 static int allow_ofs_delta
;
80 static struct pack_idx_option pack_idx_opts
;
81 static const char *base_name
;
82 static int progress
= 1;
83 static int window
= 10;
84 static unsigned long pack_size_limit
;
85 static int depth
= 50;
86 static int delta_search_threads
;
87 static int pack_to_stdout
;
90 static int num_preferred_base
;
91 static struct progress
*progress_state
;
93 static struct packed_git
*reuse_packfile
;
94 static uint32_t reuse_packfile_objects
;
95 static struct bitmap
*reuse_packfile_bitmap
;
97 static int use_bitmap_index_default
= 1;
98 static int use_bitmap_index
= -1;
99 static int allow_pack_reuse
= 1;
101 WRITE_BITMAP_FALSE
= 0,
104 } write_bitmap_index
;
105 static uint16_t write_bitmap_options
= BITMAP_OPT_HASH_CACHE
;
107 static int exclude_promisor_objects
;
109 static int use_delta_islands
;
111 static unsigned long delta_cache_size
= 0;
112 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
113 static unsigned long cache_max_small_delta_size
= 1000;
115 static unsigned long window_memory_limit
= 0;
117 static struct list_objects_filter_options filter_options
;
119 enum missing_action
{
120 MA_ERROR
= 0, /* fail if any missing objects are encountered */
121 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
122 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
124 static enum missing_action arg_missing_action
;
125 static show_object_fn fn_show_object
;
130 static uint32_t written
, written_delta
;
131 static uint32_t reused
, reused_delta
;
136 static struct commit
**indexed_commits
;
137 static unsigned int indexed_commits_nr
;
138 static unsigned int indexed_commits_alloc
;
140 static void index_commit_for_bitmap(struct commit
*commit
)
142 if (indexed_commits_nr
>= indexed_commits_alloc
) {
143 indexed_commits_alloc
= (indexed_commits_alloc
+ 32) * 2;
144 REALLOC_ARRAY(indexed_commits
, indexed_commits_alloc
);
147 indexed_commits
[indexed_commits_nr
++] = commit
;
150 static void *get_delta(struct object_entry
*entry
)
152 unsigned long size
, base_size
, delta_size
;
153 void *buf
, *base_buf
, *delta_buf
;
154 enum object_type type
;
156 buf
= read_object_file(&entry
->idx
.oid
, &type
, &size
);
158 die(_("unable to read %s"), oid_to_hex(&entry
->idx
.oid
));
159 base_buf
= read_object_file(&DELTA(entry
)->idx
.oid
, &type
,
162 die("unable to read %s",
163 oid_to_hex(&DELTA(entry
)->idx
.oid
));
164 delta_buf
= diff_delta(base_buf
, base_size
,
165 buf
, size
, &delta_size
, 0);
167 * We succesfully computed this delta once but dropped it for
168 * memory reasons. Something is very wrong if this time we
169 * recompute and create a different delta.
171 if (!delta_buf
|| delta_size
!= DELTA_SIZE(entry
))
172 BUG("delta size changed");
178 static unsigned long do_compress(void **pptr
, unsigned long size
)
182 unsigned long maxsize
;
184 git_deflate_init(&stream
, pack_compression_level
);
185 maxsize
= git_deflate_bound(&stream
, size
);
188 out
= xmalloc(maxsize
);
192 stream
.avail_in
= size
;
193 stream
.next_out
= out
;
194 stream
.avail_out
= maxsize
;
195 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
197 git_deflate_end(&stream
);
200 return stream
.total_out
;
203 static unsigned long write_large_blob_data(struct git_istream
*st
, struct hashfile
*f
,
204 const struct object_id
*oid
)
207 unsigned char ibuf
[1024 * 16];
208 unsigned char obuf
[1024 * 16];
209 unsigned long olen
= 0;
211 git_deflate_init(&stream
, pack_compression_level
);
216 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
218 die(_("unable to read %s"), oid_to_hex(oid
));
220 stream
.next_in
= ibuf
;
221 stream
.avail_in
= readlen
;
222 while ((stream
.avail_in
|| readlen
== 0) &&
223 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
224 stream
.next_out
= obuf
;
225 stream
.avail_out
= sizeof(obuf
);
226 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
227 hashwrite(f
, obuf
, stream
.next_out
- obuf
);
228 olen
+= stream
.next_out
- obuf
;
231 die(_("deflate error (%d)"), zret
);
233 if (zret
!= Z_STREAM_END
)
234 die(_("deflate error (%d)"), zret
);
238 git_deflate_end(&stream
);
243 * we are going to reuse the existing object data as is. make
244 * sure it is not corrupt.
246 static int check_pack_inflate(struct packed_git
*p
,
247 struct pack_window
**w_curs
,
250 unsigned long expect
)
253 unsigned char fakebuf
[4096], *in
;
256 memset(&stream
, 0, sizeof(stream
));
257 git_inflate_init(&stream
);
259 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
261 stream
.next_out
= fakebuf
;
262 stream
.avail_out
= sizeof(fakebuf
);
263 st
= git_inflate(&stream
, Z_FINISH
);
264 offset
+= stream
.next_in
- in
;
265 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
266 git_inflate_end(&stream
);
267 return (st
== Z_STREAM_END
&&
268 stream
.total_out
== expect
&&
269 stream
.total_in
== len
) ? 0 : -1;
272 static void copy_pack_data(struct hashfile
*f
,
273 struct packed_git
*p
,
274 struct pack_window
**w_curs
,
282 in
= use_pack(p
, w_curs
, offset
, &avail
);
284 avail
= (unsigned long)len
;
285 hashwrite(f
, in
, avail
);
291 /* Return 0 if we will bust the pack-size limit */
292 static unsigned long write_no_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
293 unsigned long limit
, int usable_delta
)
295 unsigned long size
, datalen
;
296 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
297 dheader
[MAX_PACK_OBJECT_HEADER
];
299 enum object_type type
;
301 struct git_istream
*st
= NULL
;
302 const unsigned hashsz
= the_hash_algo
->rawsz
;
305 if (oe_type(entry
) == OBJ_BLOB
&&
306 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
) &&
307 (st
= open_istream(&entry
->idx
.oid
, &type
, &size
, NULL
)) != NULL
)
310 buf
= read_object_file(&entry
->idx
.oid
, &type
, &size
);
312 die(_("unable to read %s"),
313 oid_to_hex(&entry
->idx
.oid
));
316 * make sure no cached delta data remains from a
317 * previous attempt before a pack split occurred.
319 FREE_AND_NULL(entry
->delta_data
);
320 entry
->z_delta_size
= 0;
321 } else if (entry
->delta_data
) {
322 size
= DELTA_SIZE(entry
);
323 buf
= entry
->delta_data
;
324 entry
->delta_data
= NULL
;
325 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
326 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
328 buf
= get_delta(entry
);
329 size
= DELTA_SIZE(entry
);
330 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
331 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
334 if (st
) /* large blob case, just assume we don't compress well */
336 else if (entry
->z_delta_size
)
337 datalen
= entry
->z_delta_size
;
339 datalen
= do_compress(&buf
, size
);
342 * The object header is a byte of 'type' followed by zero or
343 * more bytes of length.
345 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
348 if (type
== OBJ_OFS_DELTA
) {
350 * Deltas with relative base contain an additional
351 * encoding of the relative offset for the delta
352 * base from this object's position in the pack.
354 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
355 unsigned pos
= sizeof(dheader
) - 1;
356 dheader
[pos
] = ofs
& 127;
358 dheader
[--pos
] = 128 | (--ofs
& 127);
359 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
365 hashwrite(f
, header
, hdrlen
);
366 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
367 hdrlen
+= sizeof(dheader
) - pos
;
368 } else if (type
== OBJ_REF_DELTA
) {
370 * Deltas with a base reference contain
371 * additional bytes for the base object ID.
373 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
379 hashwrite(f
, header
, hdrlen
);
380 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
383 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
389 hashwrite(f
, header
, hdrlen
);
392 datalen
= write_large_blob_data(st
, f
, &entry
->idx
.oid
);
395 hashwrite(f
, buf
, datalen
);
399 return hdrlen
+ datalen
;
402 /* Return 0 if we will bust the pack-size limit */
403 static off_t
write_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
404 unsigned long limit
, int usable_delta
)
406 struct packed_git
*p
= IN_PACK(entry
);
407 struct pack_window
*w_curs
= NULL
;
408 struct revindex_entry
*revidx
;
410 enum object_type type
= oe_type(entry
);
412 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
413 dheader
[MAX_PACK_OBJECT_HEADER
];
415 const unsigned hashsz
= the_hash_algo
->rawsz
;
416 unsigned long entry_size
= SIZE(entry
);
419 type
= (allow_ofs_delta
&& DELTA(entry
)->idx
.offset
) ?
420 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
421 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
424 offset
= entry
->in_pack_offset
;
425 revidx
= find_pack_revindex(p
, offset
);
426 datalen
= revidx
[1].offset
- offset
;
427 if (!pack_to_stdout
&& p
->index_version
> 1 &&
428 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
)) {
429 error(_("bad packed object CRC for %s"),
430 oid_to_hex(&entry
->idx
.oid
));
432 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
435 offset
+= entry
->in_pack_header_size
;
436 datalen
-= entry
->in_pack_header_size
;
438 if (!pack_to_stdout
&& p
->index_version
== 1 &&
439 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry_size
)) {
440 error(_("corrupt packed object for %s"),
441 oid_to_hex(&entry
->idx
.oid
));
443 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
446 if (type
== OBJ_OFS_DELTA
) {
447 off_t ofs
= entry
->idx
.offset
- DELTA(entry
)->idx
.offset
;
448 unsigned pos
= sizeof(dheader
) - 1;
449 dheader
[pos
] = ofs
& 127;
451 dheader
[--pos
] = 128 | (--ofs
& 127);
452 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ hashsz
>= limit
) {
456 hashwrite(f
, header
, hdrlen
);
457 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
458 hdrlen
+= sizeof(dheader
) - pos
;
460 } else if (type
== OBJ_REF_DELTA
) {
461 if (limit
&& hdrlen
+ hashsz
+ datalen
+ hashsz
>= limit
) {
465 hashwrite(f
, header
, hdrlen
);
466 hashwrite(f
, DELTA(entry
)->idx
.oid
.hash
, hashsz
);
470 if (limit
&& hdrlen
+ datalen
+ hashsz
>= limit
) {
474 hashwrite(f
, header
, hdrlen
);
476 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
479 return hdrlen
+ datalen
;
482 /* Return 0 if we will bust the pack-size limit */
483 static off_t
write_object(struct hashfile
*f
,
484 struct object_entry
*entry
,
489 int usable_delta
, to_reuse
;
494 /* apply size limit if limited packsize and not first object */
495 if (!pack_size_limit
|| !nr_written
)
497 else if (pack_size_limit
<= write_offset
)
499 * the earlier object did not fit the limit; avoid
500 * mistaking this with unlimited (i.e. limit = 0).
504 limit
= pack_size_limit
- write_offset
;
507 usable_delta
= 0; /* no delta */
508 else if (!pack_size_limit
)
509 usable_delta
= 1; /* unlimited packfile */
510 else if (DELTA(entry
)->idx
.offset
== (off_t
)-1)
511 usable_delta
= 0; /* base was written to another pack */
512 else if (DELTA(entry
)->idx
.offset
)
513 usable_delta
= 1; /* base already exists in this pack */
515 usable_delta
= 0; /* base could end up in another pack */
518 to_reuse
= 0; /* explicit */
519 else if (!IN_PACK(entry
))
520 to_reuse
= 0; /* can't reuse what we don't have */
521 else if (oe_type(entry
) == OBJ_REF_DELTA
||
522 oe_type(entry
) == OBJ_OFS_DELTA
)
523 /* check_object() decided it for us ... */
524 to_reuse
= usable_delta
;
525 /* ... but pack split may override that */
526 else if (oe_type(entry
) != entry
->in_pack_type
)
527 to_reuse
= 0; /* pack has delta which is unusable */
528 else if (DELTA(entry
))
529 to_reuse
= 0; /* we want to pack afresh */
531 to_reuse
= 1; /* we have it in-pack undeltified,
532 * and we do not need to deltify it.
536 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
538 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
546 entry
->idx
.crc32
= crc32_end(f
);
550 enum write_one_status
{
551 WRITE_ONE_SKIP
= -1, /* already written */
552 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
553 WRITE_ONE_WRITTEN
= 1, /* normal */
554 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
557 static enum write_one_status
write_one(struct hashfile
*f
,
558 struct object_entry
*e
,
565 * we set offset to 1 (which is an impossible value) to mark
566 * the fact that this object is involved in "write its base
567 * first before writing a deltified object" recursion.
569 recursing
= (e
->idx
.offset
== 1);
571 warning(_("recursive delta detected for object %s"),
572 oid_to_hex(&e
->idx
.oid
));
573 return WRITE_ONE_RECURSIVE
;
574 } else if (e
->idx
.offset
|| e
->preferred_base
) {
575 /* offset is non zero if object is written already. */
576 return WRITE_ONE_SKIP
;
579 /* if we are deltified, write out base object first. */
581 e
->idx
.offset
= 1; /* now recurse */
582 switch (write_one(f
, DELTA(e
), offset
)) {
583 case WRITE_ONE_RECURSIVE
:
584 /* we cannot depend on this one */
589 case WRITE_ONE_BREAK
:
590 e
->idx
.offset
= recursing
;
591 return WRITE_ONE_BREAK
;
595 e
->idx
.offset
= *offset
;
596 size
= write_object(f
, e
, *offset
);
598 e
->idx
.offset
= recursing
;
599 return WRITE_ONE_BREAK
;
601 written_list
[nr_written
++] = &e
->idx
;
603 /* make sure off_t is sufficiently large not to wrap */
604 if (signed_add_overflows(*offset
, size
))
605 die(_("pack too large for current definition of off_t"));
607 return WRITE_ONE_WRITTEN
;
610 static int mark_tagged(const char *path
, const struct object_id
*oid
, int flag
,
613 struct object_id peeled
;
614 struct object_entry
*entry
= packlist_find(&to_pack
, oid
, NULL
);
618 if (!peel_ref(path
, &peeled
)) {
619 entry
= packlist_find(&to_pack
, &peeled
, NULL
);
626 static inline void add_to_write_order(struct object_entry
**wo
,
628 struct object_entry
*e
)
630 if (e
->filled
|| oe_layer(&to_pack
, e
) != write_layer
)
636 static void add_descendants_to_write_order(struct object_entry
**wo
,
638 struct object_entry
*e
)
640 int add_to_order
= 1;
643 struct object_entry
*s
;
644 /* add this node... */
645 add_to_write_order(wo
, endp
, e
);
646 /* all its siblings... */
647 for (s
= DELTA_SIBLING(e
); s
; s
= DELTA_SIBLING(s
)) {
648 add_to_write_order(wo
, endp
, s
);
651 /* drop down a level to add left subtree nodes if possible */
652 if (DELTA_CHILD(e
)) {
657 /* our sibling might have some children, it is next */
658 if (DELTA_SIBLING(e
)) {
659 e
= DELTA_SIBLING(e
);
662 /* go back to our parent node */
664 while (e
&& !DELTA_SIBLING(e
)) {
665 /* we're on the right side of a subtree, keep
666 * going up until we can go right again */
670 /* done- we hit our original root node */
673 /* pass it off to sibling at this level */
674 e
= DELTA_SIBLING(e
);
679 static void add_family_to_write_order(struct object_entry
**wo
,
681 struct object_entry
*e
)
683 struct object_entry
*root
;
685 for (root
= e
; DELTA(root
); root
= DELTA(root
))
687 add_descendants_to_write_order(wo
, endp
, root
);
690 static void compute_layer_order(struct object_entry
**wo
, unsigned int *wo_end
)
692 unsigned int i
, last_untagged
;
693 struct object_entry
*objects
= to_pack
.objects
;
695 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
696 if (objects
[i
].tagged
)
698 add_to_write_order(wo
, wo_end
, &objects
[i
]);
703 * Then fill all the tagged tips.
705 for (; i
< to_pack
.nr_objects
; i
++) {
706 if (objects
[i
].tagged
)
707 add_to_write_order(wo
, wo_end
, &objects
[i
]);
711 * And then all remaining commits and tags.
713 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
714 if (oe_type(&objects
[i
]) != OBJ_COMMIT
&&
715 oe_type(&objects
[i
]) != OBJ_TAG
)
717 add_to_write_order(wo
, wo_end
, &objects
[i
]);
721 * And then all the trees.
723 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
724 if (oe_type(&objects
[i
]) != OBJ_TREE
)
726 add_to_write_order(wo
, wo_end
, &objects
[i
]);
730 * Finally all the rest in really tight order
732 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
733 if (!objects
[i
].filled
&& oe_layer(&to_pack
, &objects
[i
]) == write_layer
)
734 add_family_to_write_order(wo
, wo_end
, &objects
[i
]);
738 static struct object_entry
**compute_write_order(void)
740 uint32_t max_layers
= 1;
741 unsigned int i
, wo_end
;
743 struct object_entry
**wo
;
744 struct object_entry
*objects
= to_pack
.objects
;
746 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
747 objects
[i
].tagged
= 0;
748 objects
[i
].filled
= 0;
749 SET_DELTA_CHILD(&objects
[i
], NULL
);
750 SET_DELTA_SIBLING(&objects
[i
], NULL
);
754 * Fully connect delta_child/delta_sibling network.
755 * Make sure delta_sibling is sorted in the original
758 for (i
= to_pack
.nr_objects
; i
> 0;) {
759 struct object_entry
*e
= &objects
[--i
];
762 /* Mark me as the first child */
763 e
->delta_sibling_idx
= DELTA(e
)->delta_child_idx
;
764 SET_DELTA_CHILD(DELTA(e
), e
);
768 * Mark objects that are at the tip of tags.
770 for_each_tag_ref(mark_tagged
, NULL
);
772 if (use_delta_islands
)
773 max_layers
= compute_pack_layers(&to_pack
);
775 ALLOC_ARRAY(wo
, to_pack
.nr_objects
);
778 for (; write_layer
< max_layers
; ++write_layer
)
779 compute_layer_order(wo
, &wo_end
);
781 if (wo_end
!= to_pack
.nr_objects
)
782 die(_("ordered %u objects, expected %"PRIu32
),
783 wo_end
, to_pack
.nr_objects
);
790 * A reused set of objects. All objects in a chunk have the same
791 * relative position in the original packfile and the generated
795 static struct reused_chunk
{
796 /* The offset of the first object of this chunk in the original
799 /* The offset of the first object of this chunk in the generated
800 * packfile minus "original". */
803 static int reused_chunks_nr
;
804 static int reused_chunks_alloc
;
806 static void record_reused_object(off_t where
, off_t offset
)
808 if (reused_chunks_nr
&& reused_chunks
[reused_chunks_nr
-1].difference
== offset
)
811 ALLOC_GROW(reused_chunks
, reused_chunks_nr
+ 1,
812 reused_chunks_alloc
);
813 reused_chunks
[reused_chunks_nr
].original
= where
;
814 reused_chunks
[reused_chunks_nr
].difference
= offset
;
819 * Binary search to find the chunk that "where" is in. Note
820 * that we're not looking for an exact match, just the first
821 * chunk that contains it (which implicitly ends at the start
824 static off_t
find_reused_offset(off_t where
)
826 int lo
= 0, hi
= reused_chunks_nr
;
828 int mi
= lo
+ ((hi
- lo
) / 2);
829 if (where
== reused_chunks
[mi
].original
)
830 return reused_chunks
[mi
].difference
;
831 if (where
< reused_chunks
[mi
].original
)
838 * The first chunk starts at zero, so we can't have gone below
842 return reused_chunks
[lo
-1].difference
;
845 static void write_reused_pack_one(size_t pos
, struct hashfile
*out
,
846 struct pack_window
**w_curs
)
848 off_t offset
, next
, cur
;
849 enum object_type type
;
852 offset
= reuse_packfile
->revindex
[pos
].offset
;
853 next
= reuse_packfile
->revindex
[pos
+ 1].offset
;
855 record_reused_object(offset
, offset
- hashfile_total(out
));
858 type
= unpack_object_header(reuse_packfile
, w_curs
, &cur
, &size
);
861 if (type
== OBJ_OFS_DELTA
) {
865 unsigned char header
[MAX_PACK_OBJECT_HEADER
];
868 base_offset
= get_delta_base(reuse_packfile
, w_curs
, &cur
, type
, offset
);
869 assert(base_offset
!= 0);
871 /* Convert to REF_DELTA if we must... */
872 if (!allow_ofs_delta
) {
873 int base_pos
= find_revindex_position(reuse_packfile
, base_offset
);
874 const unsigned char *base_sha1
=
875 nth_packed_object_sha1(reuse_packfile
,
876 reuse_packfile
->revindex
[base_pos
].nr
);
878 len
= encode_in_pack_object_header(header
, sizeof(header
),
879 OBJ_REF_DELTA
, size
);
880 hashwrite(out
, header
, len
);
881 hashwrite(out
, base_sha1
, 20);
882 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
886 /* Otherwise see if we need to rewrite the offset... */
887 fixup
= find_reused_offset(offset
) -
888 find_reused_offset(base_offset
);
890 unsigned char ofs_header
[10];
892 off_t ofs
= offset
- base_offset
- fixup
;
894 len
= encode_in_pack_object_header(header
, sizeof(header
),
895 OBJ_OFS_DELTA
, size
);
897 i
= sizeof(ofs_header
) - 1;
898 ofs_header
[i
] = ofs
& 127;
900 ofs_header
[--i
] = 128 | (--ofs
& 127);
902 ofs_len
= sizeof(ofs_header
) - i
;
904 hashwrite(out
, header
, len
);
905 hashwrite(out
, ofs_header
+ sizeof(ofs_header
) - ofs_len
, ofs_len
);
906 copy_pack_data(out
, reuse_packfile
, w_curs
, cur
, next
- cur
);
910 /* ...otherwise we have no fixup, and can write it verbatim */
913 copy_pack_data(out
, reuse_packfile
, w_curs
, offset
, next
- offset
);
916 static size_t write_reused_pack_verbatim(struct hashfile
*out
,
917 struct pack_window
**w_curs
)
921 while (pos
< reuse_packfile_bitmap
->word_alloc
&&
922 reuse_packfile_bitmap
->words
[pos
] == (eword_t
)~0)
928 written
= (pos
* BITS_IN_EWORD
);
929 to_write
= reuse_packfile
->revindex
[written
].offset
930 - sizeof(struct pack_header
);
932 /* We're recording one chunk, not one object. */
933 record_reused_object(sizeof(struct pack_header
), 0);
935 copy_pack_data(out
, reuse_packfile
, w_curs
,
936 sizeof(struct pack_header
), to_write
);
938 display_progress(progress_state
, written
);
943 static void write_reused_pack(struct hashfile
*f
)
947 struct pack_window
*w_curs
= NULL
;
950 i
= write_reused_pack_verbatim(f
, &w_curs
);
952 for (; i
< reuse_packfile_bitmap
->word_alloc
; ++i
) {
953 eword_t word
= reuse_packfile_bitmap
->words
[i
];
954 size_t pos
= (i
* BITS_IN_EWORD
);
956 for (offset
= 0; offset
< BITS_IN_EWORD
; ++offset
) {
957 if ((word
>> offset
) == 0)
960 offset
+= ewah_bit_ctz64(word
>> offset
);
961 write_reused_pack_one(pos
+ offset
, f
, &w_curs
);
962 display_progress(progress_state
, ++written
);
969 static const char no_split_warning
[] = N_(
970 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
973 static void write_pack_file(void)
978 uint32_t nr_remaining
= nr_result
;
979 time_t last_mtime
= 0;
980 struct object_entry
**write_order
;
982 if (progress
> pack_to_stdout
)
983 progress_state
= start_progress(_("Writing objects"), nr_result
);
984 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
985 write_order
= compute_write_order();
988 struct object_id oid
;
989 char *pack_tmp_name
= NULL
;
992 f
= hashfd_throughput(1, "<stdout>", progress_state
);
994 f
= create_tmp_packfile(&pack_tmp_name
);
996 offset
= write_pack_header(f
, nr_remaining
);
998 if (reuse_packfile
) {
999 assert(pack_to_stdout
);
1000 write_reused_pack(f
);
1001 offset
= hashfile_total(f
);
1005 for (; i
< to_pack
.nr_objects
; i
++) {
1006 struct object_entry
*e
= write_order
[i
];
1007 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
1009 display_progress(progress_state
, written
);
1013 * Did we write the wrong # entries in the header?
1014 * If so, rewrite it like in fast-import
1016 if (pack_to_stdout
) {
1017 finalize_hashfile(f
, oid
.hash
, CSUM_HASH_IN_STREAM
| CSUM_CLOSE
);
1018 } else if (nr_written
== nr_remaining
) {
1019 finalize_hashfile(f
, oid
.hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
1021 int fd
= finalize_hashfile(f
, oid
.hash
, 0);
1022 fixup_pack_header_footer(fd
, oid
.hash
, pack_tmp_name
,
1023 nr_written
, oid
.hash
, offset
);
1025 if (write_bitmap_index
) {
1026 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1027 warning(_(no_split_warning
));
1028 write_bitmap_index
= 0;
1032 if (!pack_to_stdout
) {
1034 struct strbuf tmpname
= STRBUF_INIT
;
1037 * Packs are runtime accessed in their mtime
1038 * order since newer packs are more likely to contain
1039 * younger objects. So if we are creating multiple
1040 * packs then we should modify the mtime of later ones
1041 * to preserve this property.
1043 if (stat(pack_tmp_name
, &st
) < 0) {
1044 warning_errno(_("failed to stat %s"), pack_tmp_name
);
1045 } else if (!last_mtime
) {
1046 last_mtime
= st
.st_mtime
;
1049 utb
.actime
= st
.st_atime
;
1050 utb
.modtime
= --last_mtime
;
1051 if (utime(pack_tmp_name
, &utb
) < 0)
1052 warning_errno(_("failed utime() on %s"), pack_tmp_name
);
1055 strbuf_addf(&tmpname
, "%s-", base_name
);
1057 if (write_bitmap_index
) {
1058 bitmap_writer_set_checksum(oid
.hash
);
1059 bitmap_writer_build_type_index(
1060 &to_pack
, written_list
, nr_written
);
1063 finish_tmp_packfile(&tmpname
, pack_tmp_name
,
1064 written_list
, nr_written
,
1065 &pack_idx_opts
, oid
.hash
);
1067 if (write_bitmap_index
) {
1068 strbuf_addf(&tmpname
, "%s.bitmap", oid_to_hex(&oid
));
1070 stop_progress(&progress_state
);
1072 bitmap_writer_show_progress(progress
);
1073 bitmap_writer_reuse_bitmaps(&to_pack
);
1074 bitmap_writer_select_commits(indexed_commits
, indexed_commits_nr
, -1);
1075 bitmap_writer_build(&to_pack
);
1076 bitmap_writer_finish(written_list
, nr_written
,
1077 tmpname
.buf
, write_bitmap_options
);
1078 write_bitmap_index
= 0;
1081 strbuf_release(&tmpname
);
1082 free(pack_tmp_name
);
1083 puts(oid_to_hex(&oid
));
1086 /* mark written objects as written to previous pack */
1087 for (j
= 0; j
< nr_written
; j
++) {
1088 written_list
[j
]->offset
= (off_t
)-1;
1090 nr_remaining
-= nr_written
;
1091 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
1095 stop_progress(&progress_state
);
1096 if (written
!= nr_result
)
1097 die(_("wrote %"PRIu32
" objects while expecting %"PRIu32
),
1098 written
, nr_result
);
1099 trace2_data_intmax("pack-objects", the_repository
,
1100 "write_pack_file/wrote", nr_result
);
1103 static int no_try_delta(const char *path
)
1105 static struct attr_check
*check
;
1108 check
= attr_check_initl("delta", NULL
);
1109 git_check_attr(the_repository
->index
, path
, check
);
1110 if (ATTR_FALSE(check
->items
[0].value
))
1116 * When adding an object, check whether we have already added it
1117 * to our packing list. If so, we can skip. However, if we are
1118 * being asked to excludei t, but the previous mention was to include
1119 * it, make sure to adjust its flags and tweak our numbers accordingly.
1121 * As an optimization, we pass out the index position where we would have
1122 * found the item, since that saves us from having to look it up again a
1123 * few lines later when we want to add the new entry.
1125 static int have_duplicate_entry(const struct object_id
*oid
,
1127 uint32_t *index_pos
)
1129 struct object_entry
*entry
;
1131 if (reuse_packfile_bitmap
&&
1132 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
))
1135 entry
= packlist_find(&to_pack
, oid
, index_pos
);
1140 if (!entry
->preferred_base
)
1142 entry
->preferred_base
= 1;
1148 static int want_found_object(int exclude
, struct packed_git
*p
)
1156 * When asked to do --local (do not include an object that appears in a
1157 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
1158 * an object that appears in a pack marked with .keep), finding a pack
1159 * that matches the criteria is sufficient for us to decide to omit it.
1160 * However, even if this pack does not satisfy the criteria, we need to
1161 * make sure no copy of this object appears in _any_ pack that makes us
1162 * to omit the object, so we need to check all the packs.
1164 * We can however first check whether these options can possible matter;
1165 * if they do not matter we know we want the object in generated pack.
1166 * Otherwise, we signal "-1" at the end to tell the caller that we do
1167 * not know either way, and it needs to check more packs.
1169 if (!ignore_packed_keep_on_disk
&&
1170 !ignore_packed_keep_in_core
&&
1171 (!local
|| !have_non_local_packs
))
1174 if (local
&& !p
->pack_local
)
1176 if (p
->pack_local
&&
1177 ((ignore_packed_keep_on_disk
&& p
->pack_keep
) ||
1178 (ignore_packed_keep_in_core
&& p
->pack_keep_in_core
)))
1181 /* we don't know yet; keep looking for more packs */
1186 * Check whether we want the object in the pack (e.g., we do not want
1187 * objects found in non-local stores if the "--local" option was used).
1189 * If the caller already knows an existing pack it wants to take the object
1190 * from, that is passed in *found_pack and *found_offset; otherwise this
1191 * function finds if there is any pack that has the object and returns the pack
1192 * and its offset in these variables.
1194 static int want_object_in_pack(const struct object_id
*oid
,
1196 struct packed_git
**found_pack
,
1197 off_t
*found_offset
)
1200 struct list_head
*pos
;
1201 struct multi_pack_index
*m
;
1203 if (!exclude
&& local
&& has_loose_object_nonlocal(oid
))
1207 * If we already know the pack object lives in, start checks from that
1208 * pack - in the usual case when neither --local was given nor .keep files
1209 * are present we will determine the answer right now.
1212 want
= want_found_object(exclude
, *found_pack
);
1217 for (m
= get_multi_pack_index(the_repository
); m
; m
= m
->next
) {
1218 struct pack_entry e
;
1219 if (fill_midx_entry(the_repository
, oid
, &e
, m
)) {
1220 struct packed_git
*p
= e
.p
;
1223 if (p
== *found_pack
)
1224 offset
= *found_offset
;
1226 offset
= find_pack_entry_one(oid
->hash
, p
);
1230 if (!is_pack_valid(p
))
1232 *found_offset
= offset
;
1235 want
= want_found_object(exclude
, p
);
1242 list_for_each(pos
, get_packed_git_mru(the_repository
)) {
1243 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1246 if (p
== *found_pack
)
1247 offset
= *found_offset
;
1249 offset
= find_pack_entry_one(oid
->hash
, p
);
1253 if (!is_pack_valid(p
))
1255 *found_offset
= offset
;
1258 want
= want_found_object(exclude
, p
);
1259 if (!exclude
&& want
> 0)
1261 get_packed_git_mru(the_repository
));
1270 static void create_object_entry(const struct object_id
*oid
,
1271 enum object_type type
,
1276 struct packed_git
*found_pack
,
1279 struct object_entry
*entry
;
1281 entry
= packlist_alloc(&to_pack
, oid
->hash
, index_pos
);
1283 oe_set_type(entry
, type
);
1285 entry
->preferred_base
= 1;
1289 oe_set_in_pack(&to_pack
, entry
, found_pack
);
1290 entry
->in_pack_offset
= found_offset
;
1293 entry
->no_try_delta
= no_try_delta
;
1296 static const char no_closure_warning
[] = N_(
1297 "disabling bitmap writing, as some objects are not being packed"
1300 static int add_object_entry(const struct object_id
*oid
, enum object_type type
,
1301 const char *name
, int exclude
)
1303 struct packed_git
*found_pack
= NULL
;
1304 off_t found_offset
= 0;
1307 display_progress(progress_state
, ++nr_seen
);
1309 if (have_duplicate_entry(oid
, exclude
, &index_pos
))
1312 if (!want_object_in_pack(oid
, exclude
, &found_pack
, &found_offset
)) {
1313 /* The pack is missing an object, so it will not have closure */
1314 if (write_bitmap_index
) {
1315 if (write_bitmap_index
!= WRITE_BITMAP_QUIET
)
1316 warning(_(no_closure_warning
));
1317 write_bitmap_index
= 0;
1322 create_object_entry(oid
, type
, pack_name_hash(name
),
1323 exclude
, name
&& no_try_delta(name
),
1324 index_pos
, found_pack
, found_offset
);
1328 static int add_object_entry_from_bitmap(const struct object_id
*oid
,
1329 enum object_type type
,
1330 int flags
, uint32_t name_hash
,
1331 struct packed_git
*pack
, off_t offset
)
1335 display_progress(progress_state
, ++nr_seen
);
1337 if (have_duplicate_entry(oid
, 0, &index_pos
))
1340 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
1343 create_object_entry(oid
, type
, name_hash
, 0, 0, index_pos
, pack
, offset
);
1347 struct pbase_tree_cache
{
1348 struct object_id oid
;
1352 unsigned long tree_size
;
1355 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1356 static int pbase_tree_cache_ix(const struct object_id
*oid
)
1358 return oid
->hash
[0] % ARRAY_SIZE(pbase_tree_cache
);
1360 static int pbase_tree_cache_ix_incr(int ix
)
1362 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1365 static struct pbase_tree
{
1366 struct pbase_tree
*next
;
1367 /* This is a phony "cache" entry; we are not
1368 * going to evict it or find it through _get()
1369 * mechanism -- this is for the toplevel node that
1370 * would almost always change with any commit.
1372 struct pbase_tree_cache pcache
;
1375 static struct pbase_tree_cache
*pbase_tree_get(const struct object_id
*oid
)
1377 struct pbase_tree_cache
*ent
, *nent
;
1380 enum object_type type
;
1382 int my_ix
= pbase_tree_cache_ix(oid
);
1383 int available_ix
= -1;
1385 /* pbase-tree-cache acts as a limited hashtable.
1386 * your object will be found at your index or within a few
1387 * slots after that slot if it is cached.
1389 for (neigh
= 0; neigh
< 8; neigh
++) {
1390 ent
= pbase_tree_cache
[my_ix
];
1391 if (ent
&& oideq(&ent
->oid
, oid
)) {
1395 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1396 ((0 <= available_ix
) &&
1397 (!ent
&& pbase_tree_cache
[available_ix
])))
1398 available_ix
= my_ix
;
1401 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1404 /* Did not find one. Either we got a bogus request or
1405 * we need to read and perhaps cache.
1407 data
= read_object_file(oid
, &type
, &size
);
1410 if (type
!= OBJ_TREE
) {
1415 /* We need to either cache or return a throwaway copy */
1417 if (available_ix
< 0)
1420 ent
= pbase_tree_cache
[available_ix
];
1421 my_ix
= available_ix
;
1425 nent
= xmalloc(sizeof(*nent
));
1426 nent
->temporary
= (available_ix
< 0);
1429 /* evict and reuse */
1430 free(ent
->tree_data
);
1433 oidcpy(&nent
->oid
, oid
);
1434 nent
->tree_data
= data
;
1435 nent
->tree_size
= size
;
1437 if (!nent
->temporary
)
1438 pbase_tree_cache
[my_ix
] = nent
;
1442 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1444 if (!cache
->temporary
) {
1448 free(cache
->tree_data
);
1452 static int name_cmp_len(const char *name
)
1455 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
1460 static void add_pbase_object(struct tree_desc
*tree
,
1463 const char *fullname
)
1465 struct name_entry entry
;
1468 while (tree_entry(tree
,&entry
)) {
1469 if (S_ISGITLINK(entry
.mode
))
1471 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1472 memcmp(name
, entry
.path
, cmplen
);
1477 if (name
[cmplen
] != '/') {
1478 add_object_entry(&entry
.oid
,
1479 object_type(entry
.mode
),
1483 if (S_ISDIR(entry
.mode
)) {
1484 struct tree_desc sub
;
1485 struct pbase_tree_cache
*tree
;
1486 const char *down
= name
+cmplen
+1;
1487 int downlen
= name_cmp_len(down
);
1489 tree
= pbase_tree_get(&entry
.oid
);
1492 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1494 add_pbase_object(&sub
, down
, downlen
, fullname
);
1495 pbase_tree_put(tree
);
1500 static unsigned *done_pbase_paths
;
1501 static int done_pbase_paths_num
;
1502 static int done_pbase_paths_alloc
;
1503 static int done_pbase_path_pos(unsigned hash
)
1506 int hi
= done_pbase_paths_num
;
1508 int mi
= lo
+ (hi
- lo
) / 2;
1509 if (done_pbase_paths
[mi
] == hash
)
1511 if (done_pbase_paths
[mi
] < hash
)
1519 static int check_pbase_path(unsigned hash
)
1521 int pos
= done_pbase_path_pos(hash
);
1525 ALLOC_GROW(done_pbase_paths
,
1526 done_pbase_paths_num
+ 1,
1527 done_pbase_paths_alloc
);
1528 done_pbase_paths_num
++;
1529 if (pos
< done_pbase_paths_num
)
1530 MOVE_ARRAY(done_pbase_paths
+ pos
+ 1, done_pbase_paths
+ pos
,
1531 done_pbase_paths_num
- pos
- 1);
1532 done_pbase_paths
[pos
] = hash
;
1536 static void add_preferred_base_object(const char *name
)
1538 struct pbase_tree
*it
;
1540 unsigned hash
= pack_name_hash(name
);
1542 if (!num_preferred_base
|| check_pbase_path(hash
))
1545 cmplen
= name_cmp_len(name
);
1546 for (it
= pbase_tree
; it
; it
= it
->next
) {
1548 add_object_entry(&it
->pcache
.oid
, OBJ_TREE
, NULL
, 1);
1551 struct tree_desc tree
;
1552 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1553 add_pbase_object(&tree
, name
, cmplen
, name
);
1558 static void add_preferred_base(struct object_id
*oid
)
1560 struct pbase_tree
*it
;
1563 struct object_id tree_oid
;
1565 if (window
<= num_preferred_base
++)
1568 data
= read_object_with_reference(the_repository
, oid
,
1569 tree_type
, &size
, &tree_oid
);
1573 for (it
= pbase_tree
; it
; it
= it
->next
) {
1574 if (oideq(&it
->pcache
.oid
, &tree_oid
)) {
1580 it
= xcalloc(1, sizeof(*it
));
1581 it
->next
= pbase_tree
;
1584 oidcpy(&it
->pcache
.oid
, &tree_oid
);
1585 it
->pcache
.tree_data
= data
;
1586 it
->pcache
.tree_size
= size
;
1589 static void cleanup_preferred_base(void)
1591 struct pbase_tree
*it
;
1597 struct pbase_tree
*tmp
= it
;
1599 free(tmp
->pcache
.tree_data
);
1603 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1604 if (!pbase_tree_cache
[i
])
1606 free(pbase_tree_cache
[i
]->tree_data
);
1607 FREE_AND_NULL(pbase_tree_cache
[i
]);
1610 FREE_AND_NULL(done_pbase_paths
);
1611 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1615 * Return 1 iff the object specified by "delta" can be sent
1616 * literally as a delta against the base in "base_sha1". If
1617 * so, then *base_out will point to the entry in our packing
1618 * list, or NULL if we must use the external-base list.
1620 * Depth value does not matter - find_deltas() will
1621 * never consider reused delta as the base object to
1622 * deltify other objects against, in order to avoid
1625 static int can_reuse_delta(const unsigned char *base_sha1
,
1626 struct object_entry
*delta
,
1627 struct object_entry
**base_out
)
1629 struct object_entry
*base
;
1630 struct object_id base_oid
;
1635 oidread(&base_oid
, base_sha1
);
1638 * First see if we're already sending the base (or it's explicitly in
1639 * our "excluded" list).
1641 base
= packlist_find(&to_pack
, &base_oid
, NULL
);
1643 if (!in_same_island(&delta
->idx
.oid
, &base
->idx
.oid
))
1650 * Otherwise, reachability bitmaps may tell us if the receiver has it,
1651 * even if it was buried too deep in history to make it into the
1654 if (thin
&& bitmap_has_oid_in_uninteresting(bitmap_git
, &base_oid
)) {
1655 if (use_delta_islands
) {
1656 if (!in_same_island(&delta
->idx
.oid
, &base_oid
))
1666 static void check_object(struct object_entry
*entry
)
1668 unsigned long canonical_size
;
1670 if (IN_PACK(entry
)) {
1671 struct packed_git
*p
= IN_PACK(entry
);
1672 struct pack_window
*w_curs
= NULL
;
1673 const unsigned char *base_ref
= NULL
;
1674 struct object_entry
*base_entry
;
1675 unsigned long used
, used_0
;
1676 unsigned long avail
;
1678 unsigned char *buf
, c
;
1679 enum object_type type
;
1680 unsigned long in_pack_size
;
1682 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1685 * We want in_pack_type even if we do not reuse delta
1686 * since non-delta representations could still be reused.
1688 used
= unpack_object_header_buffer(buf
, avail
,
1695 BUG("invalid type %d", type
);
1696 entry
->in_pack_type
= type
;
1699 * Determine if this is a delta and if so whether we can
1700 * reuse it or not. Otherwise let's find out as cheaply as
1701 * possible what the actual type and size for this object is.
1703 switch (entry
->in_pack_type
) {
1705 /* Not a delta hence we've already got all we need. */
1706 oe_set_type(entry
, entry
->in_pack_type
);
1707 SET_SIZE(entry
, in_pack_size
);
1708 entry
->in_pack_header_size
= used
;
1709 if (oe_type(entry
) < OBJ_COMMIT
|| oe_type(entry
) > OBJ_BLOB
)
1711 unuse_pack(&w_curs
);
1714 if (reuse_delta
&& !entry
->preferred_base
)
1715 base_ref
= use_pack(p
, &w_curs
,
1716 entry
->in_pack_offset
+ used
, NULL
);
1717 entry
->in_pack_header_size
= used
+ the_hash_algo
->rawsz
;
1720 buf
= use_pack(p
, &w_curs
,
1721 entry
->in_pack_offset
+ used
, NULL
);
1727 if (!ofs
|| MSB(ofs
, 7)) {
1728 error(_("delta base offset overflow in pack for %s"),
1729 oid_to_hex(&entry
->idx
.oid
));
1733 ofs
= (ofs
<< 7) + (c
& 127);
1735 ofs
= entry
->in_pack_offset
- ofs
;
1736 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
1737 error(_("delta base offset out of bound for %s"),
1738 oid_to_hex(&entry
->idx
.oid
));
1741 if (reuse_delta
&& !entry
->preferred_base
) {
1742 struct revindex_entry
*revidx
;
1743 revidx
= find_pack_revindex(p
, ofs
);
1746 base_ref
= nth_packed_object_sha1(p
, revidx
->nr
);
1748 entry
->in_pack_header_size
= used
+ used_0
;
1752 if (can_reuse_delta(base_ref
, entry
, &base_entry
)) {
1753 oe_set_type(entry
, entry
->in_pack_type
);
1754 SET_SIZE(entry
, in_pack_size
); /* delta size */
1755 SET_DELTA_SIZE(entry
, in_pack_size
);
1758 SET_DELTA(entry
, base_entry
);
1759 entry
->delta_sibling_idx
= base_entry
->delta_child_idx
;
1760 SET_DELTA_CHILD(base_entry
, entry
);
1762 SET_DELTA_EXT(entry
, base_ref
);
1765 unuse_pack(&w_curs
);
1769 if (oe_type(entry
)) {
1773 * This must be a delta and we already know what the
1774 * final object type is. Let's extract the actual
1775 * object size from the delta header.
1777 delta_pos
= entry
->in_pack_offset
+ entry
->in_pack_header_size
;
1778 canonical_size
= get_size_from_delta(p
, &w_curs
, delta_pos
);
1779 if (canonical_size
== 0)
1781 SET_SIZE(entry
, canonical_size
);
1782 unuse_pack(&w_curs
);
1787 * No choice but to fall back to the recursive delta walk
1788 * with oid_object_info() to find about the object type
1792 unuse_pack(&w_curs
);
1796 oid_object_info(the_repository
, &entry
->idx
.oid
, &canonical_size
));
1797 if (entry
->type_valid
) {
1798 SET_SIZE(entry
, canonical_size
);
1801 * Bad object type is checked in prepare_pack(). This is
1802 * to permit a missing preferred base object to be ignored
1803 * as a preferred base. Doing so can result in a larger
1804 * pack file, but the transfer will still take place.
1809 static int pack_offset_sort(const void *_a
, const void *_b
)
1811 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1812 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1813 const struct packed_git
*a_in_pack
= IN_PACK(a
);
1814 const struct packed_git
*b_in_pack
= IN_PACK(b
);
1816 /* avoid filesystem trashing with loose objects */
1817 if (!a_in_pack
&& !b_in_pack
)
1818 return oidcmp(&a
->idx
.oid
, &b
->idx
.oid
);
1820 if (a_in_pack
< b_in_pack
)
1822 if (a_in_pack
> b_in_pack
)
1824 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1825 (a
->in_pack_offset
> b
->in_pack_offset
);
1829 * Drop an on-disk delta we were planning to reuse. Naively, this would
1830 * just involve blanking out the "delta" field, but we have to deal
1831 * with some extra book-keeping:
1833 * 1. Removing ourselves from the delta_sibling linked list.
1835 * 2. Updating our size/type to the non-delta representation. These were
1836 * either not recorded initially (size) or overwritten with the delta type
1837 * (type) when check_object() decided to reuse the delta.
1839 * 3. Resetting our delta depth, as we are now a base object.
1841 static void drop_reused_delta(struct object_entry
*entry
)
1843 unsigned *idx
= &to_pack
.objects
[entry
->delta_idx
- 1].delta_child_idx
;
1844 struct object_info oi
= OBJECT_INFO_INIT
;
1845 enum object_type type
;
1849 struct object_entry
*oe
= &to_pack
.objects
[*idx
- 1];
1852 *idx
= oe
->delta_sibling_idx
;
1854 idx
= &oe
->delta_sibling_idx
;
1856 SET_DELTA(entry
, NULL
);
1861 if (packed_object_info(the_repository
, IN_PACK(entry
), entry
->in_pack_offset
, &oi
) < 0) {
1863 * We failed to get the info from this pack for some reason;
1864 * fall back to oid_object_info, which may find another copy.
1865 * And if that fails, the error will be recorded in oe_type(entry)
1866 * and dealt with in prepare_pack().
1869 oid_object_info(the_repository
, &entry
->idx
.oid
, &size
));
1871 oe_set_type(entry
, type
);
1873 SET_SIZE(entry
, size
);
1877 * Follow the chain of deltas from this entry onward, throwing away any links
1878 * that cause us to hit a cycle (as determined by the DFS state flags in
1881 * We also detect too-long reused chains that would violate our --depth
1884 static void break_delta_chains(struct object_entry
*entry
)
1887 * The actual depth of each object we will write is stored as an int,
1888 * as it cannot exceed our int "depth" limit. But before we break
1889 * changes based no that limit, we may potentially go as deep as the
1890 * number of objects, which is elsewhere bounded to a uint32_t.
1892 uint32_t total_depth
;
1893 struct object_entry
*cur
, *next
;
1895 for (cur
= entry
, total_depth
= 0;
1897 cur
= DELTA(cur
), total_depth
++) {
1898 if (cur
->dfs_state
== DFS_DONE
) {
1900 * We've already seen this object and know it isn't
1901 * part of a cycle. We do need to append its depth
1904 total_depth
+= cur
->depth
;
1909 * We break cycles before looping, so an ACTIVE state (or any
1910 * other cruft which made its way into the state variable)
1913 if (cur
->dfs_state
!= DFS_NONE
)
1914 BUG("confusing delta dfs state in first pass: %d",
1918 * Now we know this is the first time we've seen the object. If
1919 * it's not a delta, we're done traversing, but we'll mark it
1920 * done to save time on future traversals.
1923 cur
->dfs_state
= DFS_DONE
;
1928 * Mark ourselves as active and see if the next step causes
1929 * us to cycle to another active object. It's important to do
1930 * this _before_ we loop, because it impacts where we make the
1931 * cut, and thus how our total_depth counter works.
1932 * E.g., We may see a partial loop like:
1934 * A -> B -> C -> D -> B
1936 * Cutting B->C breaks the cycle. But now the depth of A is
1937 * only 1, and our total_depth counter is at 3. The size of the
1938 * error is always one less than the size of the cycle we
1939 * broke. Commits C and D were "lost" from A's chain.
1941 * If we instead cut D->B, then the depth of A is correct at 3.
1942 * We keep all commits in the chain that we examined.
1944 cur
->dfs_state
= DFS_ACTIVE
;
1945 if (DELTA(cur
)->dfs_state
== DFS_ACTIVE
) {
1946 drop_reused_delta(cur
);
1947 cur
->dfs_state
= DFS_DONE
;
1953 * And now that we've gone all the way to the bottom of the chain, we
1954 * need to clear the active flags and set the depth fields as
1955 * appropriate. Unlike the loop above, which can quit when it drops a
1956 * delta, we need to keep going to look for more depth cuts. So we need
1957 * an extra "next" pointer to keep going after we reset cur->delta.
1959 for (cur
= entry
; cur
; cur
= next
) {
1963 * We should have a chain of zero or more ACTIVE states down to
1964 * a final DONE. We can quit after the DONE, because either it
1965 * has no bases, or we've already handled them in a previous
1968 if (cur
->dfs_state
== DFS_DONE
)
1970 else if (cur
->dfs_state
!= DFS_ACTIVE
)
1971 BUG("confusing delta dfs state in second pass: %d",
1975 * If the total_depth is more than depth, then we need to snip
1976 * the chain into two or more smaller chains that don't exceed
1977 * the maximum depth. Most of the resulting chains will contain
1978 * (depth + 1) entries (i.e., depth deltas plus one base), and
1979 * the last chain (i.e., the one containing entry) will contain
1980 * whatever entries are left over, namely
1981 * (total_depth % (depth + 1)) of them.
1983 * Since we are iterating towards decreasing depth, we need to
1984 * decrement total_depth as we go, and we need to write to the
1985 * entry what its final depth will be after all of the
1986 * snipping. Since we're snipping into chains of length (depth
1987 * + 1) entries, the final depth of an entry will be its
1988 * original depth modulo (depth + 1). Any time we encounter an
1989 * entry whose final depth is supposed to be zero, we snip it
1990 * from its delta base, thereby making it so.
1992 cur
->depth
= (total_depth
--) % (depth
+ 1);
1994 drop_reused_delta(cur
);
1996 cur
->dfs_state
= DFS_DONE
;
2000 static void get_object_details(void)
2003 struct object_entry
**sorted_by_offset
;
2006 progress_state
= start_progress(_("Counting objects"),
2007 to_pack
.nr_objects
);
2009 sorted_by_offset
= xcalloc(to_pack
.nr_objects
, sizeof(struct object_entry
*));
2010 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2011 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
2012 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
2014 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2015 struct object_entry
*entry
= sorted_by_offset
[i
];
2016 check_object(entry
);
2017 if (entry
->type_valid
&&
2018 oe_size_greater_than(&to_pack
, entry
, big_file_threshold
))
2019 entry
->no_try_delta
= 1;
2020 display_progress(progress_state
, i
+ 1);
2022 stop_progress(&progress_state
);
2025 * This must happen in a second pass, since we rely on the delta
2026 * information for the whole list being completed.
2028 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
2029 break_delta_chains(&to_pack
.objects
[i
]);
2031 free(sorted_by_offset
);
2035 * We search for deltas in a list sorted by type, by filename hash, and then
2036 * by size, so that we see progressively smaller and smaller files.
2037 * That's because we prefer deltas to be from the bigger file
2038 * to the smaller -- deletes are potentially cheaper, but perhaps
2039 * more importantly, the bigger file is likely the more recent
2040 * one. The deepest deltas are therefore the oldest objects which are
2041 * less susceptible to be accessed often.
2043 static int type_size_sort(const void *_a
, const void *_b
)
2045 const struct object_entry
*a
= *(struct object_entry
**)_a
;
2046 const struct object_entry
*b
= *(struct object_entry
**)_b
;
2047 const enum object_type a_type
= oe_type(a
);
2048 const enum object_type b_type
= oe_type(b
);
2049 const unsigned long a_size
= SIZE(a
);
2050 const unsigned long b_size
= SIZE(b
);
2052 if (a_type
> b_type
)
2054 if (a_type
< b_type
)
2056 if (a
->hash
> b
->hash
)
2058 if (a
->hash
< b
->hash
)
2060 if (a
->preferred_base
> b
->preferred_base
)
2062 if (a
->preferred_base
< b
->preferred_base
)
2064 if (use_delta_islands
) {
2065 const int island_cmp
= island_delta_cmp(&a
->idx
.oid
, &b
->idx
.oid
);
2069 if (a_size
> b_size
)
2071 if (a_size
< b_size
)
2073 return a
< b
? -1 : (a
> b
); /* newest first */
2077 struct object_entry
*entry
;
2079 struct delta_index
*index
;
2083 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
2084 unsigned long delta_size
)
2086 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
2089 if (delta_size
< cache_max_small_delta_size
)
2092 /* cache delta, if objects are large enough compared to delta size */
2093 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
2099 /* Protect delta_cache_size */
2100 static pthread_mutex_t cache_mutex
;
2101 #define cache_lock() pthread_mutex_lock(&cache_mutex)
2102 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
2105 * Protect object list partitioning (e.g. struct thread_param) and
2108 static pthread_mutex_t progress_mutex
;
2109 #define progress_lock() pthread_mutex_lock(&progress_mutex)
2110 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
2113 * Access to struct object_entry is unprotected since each thread owns
2114 * a portion of the main object list. Just don't access object entries
2115 * ahead in the list because they can be stolen and would need
2116 * progress_mutex for protection.
2120 * Return the size of the object without doing any delta
2121 * reconstruction (so non-deltas are true object sizes, but deltas
2122 * return the size of the delta data).
2124 unsigned long oe_get_size_slow(struct packing_data
*pack
,
2125 const struct object_entry
*e
)
2127 struct packed_git
*p
;
2128 struct pack_window
*w_curs
;
2130 enum object_type type
;
2131 unsigned long used
, avail
, size
;
2133 if (e
->type_
!= OBJ_OFS_DELTA
&& e
->type_
!= OBJ_REF_DELTA
) {
2134 packing_data_lock(&to_pack
);
2135 if (oid_object_info(the_repository
, &e
->idx
.oid
, &size
) < 0)
2136 die(_("unable to get size of %s"),
2137 oid_to_hex(&e
->idx
.oid
));
2138 packing_data_unlock(&to_pack
);
2142 p
= oe_in_pack(pack
, e
);
2144 BUG("when e->type is a delta, it must belong to a pack");
2146 packing_data_lock(&to_pack
);
2148 buf
= use_pack(p
, &w_curs
, e
->in_pack_offset
, &avail
);
2149 used
= unpack_object_header_buffer(buf
, avail
, &type
, &size
);
2151 die(_("unable to parse object header of %s"),
2152 oid_to_hex(&e
->idx
.oid
));
2154 unuse_pack(&w_curs
);
2155 packing_data_unlock(&to_pack
);
2159 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
2160 unsigned max_depth
, unsigned long *mem_usage
)
2162 struct object_entry
*trg_entry
= trg
->entry
;
2163 struct object_entry
*src_entry
= src
->entry
;
2164 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
2166 enum object_type type
;
2169 /* Don't bother doing diffs between different types */
2170 if (oe_type(trg_entry
) != oe_type(src_entry
))
2174 * We do not bother to try a delta that we discarded on an
2175 * earlier try, but only when reusing delta data. Note that
2176 * src_entry that is marked as the preferred_base should always
2177 * be considered, as even if we produce a suboptimal delta against
2178 * it, we will still save the transfer cost, as we already know
2179 * the other side has it and we won't send src_entry at all.
2181 if (reuse_delta
&& IN_PACK(trg_entry
) &&
2182 IN_PACK(trg_entry
) == IN_PACK(src_entry
) &&
2183 !src_entry
->preferred_base
&&
2184 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
2185 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
2188 /* Let's not bust the allowed depth. */
2189 if (src
->depth
>= max_depth
)
2192 /* Now some size filtering heuristics. */
2193 trg_size
= SIZE(trg_entry
);
2194 if (!DELTA(trg_entry
)) {
2195 max_size
= trg_size
/2 - the_hash_algo
->rawsz
;
2198 max_size
= DELTA_SIZE(trg_entry
);
2199 ref_depth
= trg
->depth
;
2201 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
2202 (max_depth
- ref_depth
+ 1);
2205 src_size
= SIZE(src_entry
);
2206 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
2207 if (sizediff
>= max_size
)
2209 if (trg_size
< src_size
/ 32)
2212 if (!in_same_island(&trg
->entry
->idx
.oid
, &src
->entry
->idx
.oid
))
2215 /* Load data if not already done */
2217 packing_data_lock(&to_pack
);
2218 trg
->data
= read_object_file(&trg_entry
->idx
.oid
, &type
, &sz
);
2219 packing_data_unlock(&to_pack
);
2221 die(_("object %s cannot be read"),
2222 oid_to_hex(&trg_entry
->idx
.oid
));
2224 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2225 oid_to_hex(&trg_entry
->idx
.oid
), (uintmax_t)sz
,
2226 (uintmax_t)trg_size
);
2230 packing_data_lock(&to_pack
);
2231 src
->data
= read_object_file(&src_entry
->idx
.oid
, &type
, &sz
);
2232 packing_data_unlock(&to_pack
);
2234 if (src_entry
->preferred_base
) {
2235 static int warned
= 0;
2237 warning(_("object %s cannot be read"),
2238 oid_to_hex(&src_entry
->idx
.oid
));
2240 * Those objects are not included in the
2241 * resulting pack. Be resilient and ignore
2242 * them if they can't be read, in case the
2243 * pack could be created nevertheless.
2247 die(_("object %s cannot be read"),
2248 oid_to_hex(&src_entry
->idx
.oid
));
2251 die(_("object %s inconsistent object length (%"PRIuMAX
" vs %"PRIuMAX
")"),
2252 oid_to_hex(&src_entry
->idx
.oid
), (uintmax_t)sz
,
2253 (uintmax_t)src_size
);
2257 src
->index
= create_delta_index(src
->data
, src_size
);
2259 static int warned
= 0;
2261 warning(_("suboptimal pack - out of memory"));
2264 *mem_usage
+= sizeof_delta_index(src
->index
);
2267 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
2271 if (DELTA(trg_entry
)) {
2272 /* Prefer only shallower same-sized deltas. */
2273 if (delta_size
== DELTA_SIZE(trg_entry
) &&
2274 src
->depth
+ 1 >= trg
->depth
) {
2281 * Handle memory allocation outside of the cache
2282 * accounting lock. Compiler will optimize the strangeness
2283 * away when NO_PTHREADS is defined.
2285 free(trg_entry
->delta_data
);
2287 if (trg_entry
->delta_data
) {
2288 delta_cache_size
-= DELTA_SIZE(trg_entry
);
2289 trg_entry
->delta_data
= NULL
;
2291 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
2292 delta_cache_size
+= delta_size
;
2294 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
2300 SET_DELTA(trg_entry
, src_entry
);
2301 SET_DELTA_SIZE(trg_entry
, delta_size
);
2302 trg
->depth
= src
->depth
+ 1;
2307 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
2309 struct object_entry
*child
= DELTA_CHILD(me
);
2312 const unsigned int c
= check_delta_limit(child
, n
+ 1);
2315 child
= DELTA_SIBLING(child
);
2320 static unsigned long free_unpacked(struct unpacked
*n
)
2322 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
2323 free_delta_index(n
->index
);
2326 freed_mem
+= SIZE(n
->entry
);
2327 FREE_AND_NULL(n
->data
);
2334 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
2335 int window
, int depth
, unsigned *processed
)
2337 uint32_t i
, idx
= 0, count
= 0;
2338 struct unpacked
*array
;
2339 unsigned long mem_usage
= 0;
2341 array
= xcalloc(window
, sizeof(struct unpacked
));
2344 struct object_entry
*entry
;
2345 struct unpacked
*n
= array
+ idx
;
2346 int j
, max_depth
, best_base
= -1;
2355 if (!entry
->preferred_base
) {
2357 display_progress(progress_state
, *processed
);
2361 mem_usage
-= free_unpacked(n
);
2364 while (window_memory_limit
&&
2365 mem_usage
> window_memory_limit
&&
2367 const uint32_t tail
= (idx
+ window
- count
) % window
;
2368 mem_usage
-= free_unpacked(array
+ tail
);
2372 /* We do not compute delta to *create* objects we are not
2375 if (entry
->preferred_base
)
2379 * If the current object is at pack edge, take the depth the
2380 * objects that depend on the current object into account
2381 * otherwise they would become too deep.
2384 if (DELTA_CHILD(entry
)) {
2385 max_depth
-= check_delta_limit(entry
, 0);
2393 uint32_t other_idx
= idx
+ j
;
2395 if (other_idx
>= window
)
2396 other_idx
-= window
;
2397 m
= array
+ other_idx
;
2400 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2404 best_base
= other_idx
;
2408 * If we decided to cache the delta data, then it is best
2409 * to compress it right away. First because we have to do
2410 * it anyway, and doing it here while we're threaded will
2411 * save a lot of time in the non threaded write phase,
2412 * as well as allow for caching more deltas within
2413 * the same cache size limit.
2415 * But only if not writing to stdout, since in that case
2416 * the network is most likely throttling writes anyway,
2417 * and therefore it is best to go to the write phase ASAP
2418 * instead, as we can afford spending more time compressing
2419 * between writes at that moment.
2421 if (entry
->delta_data
&& !pack_to_stdout
) {
2424 size
= do_compress(&entry
->delta_data
, DELTA_SIZE(entry
));
2425 if (size
< (1U << OE_Z_DELTA_BITS
)) {
2426 entry
->z_delta_size
= size
;
2428 delta_cache_size
-= DELTA_SIZE(entry
);
2429 delta_cache_size
+= entry
->z_delta_size
;
2432 FREE_AND_NULL(entry
->delta_data
);
2433 entry
->z_delta_size
= 0;
2437 /* if we made n a delta, and if n is already at max
2438 * depth, leaving it in the window is pointless. we
2439 * should evict it first.
2441 if (DELTA(entry
) && max_depth
<= n
->depth
)
2445 * Move the best delta base up in the window, after the
2446 * currently deltified object, to keep it longer. It will
2447 * be the first base object to be attempted next.
2450 struct unpacked swap
= array
[best_base
];
2451 int dist
= (window
+ idx
- best_base
) % window
;
2452 int dst
= best_base
;
2454 int src
= (dst
+ 1) % window
;
2455 array
[dst
] = array
[src
];
2463 if (count
+ 1 < window
)
2469 for (i
= 0; i
< window
; ++i
) {
2470 free_delta_index(array
[i
].index
);
2471 free(array
[i
].data
);
2476 static void try_to_free_from_threads(size_t size
)
2478 packing_data_lock(&to_pack
);
2479 release_pack_memory(size
);
2480 packing_data_unlock(&to_pack
);
2483 static try_to_free_t old_try_to_free_routine
;
2486 * The main object list is split into smaller lists, each is handed to
2489 * The main thread waits on the condition that (at least) one of the workers
2490 * has stopped working (which is indicated in the .working member of
2491 * struct thread_params).
2493 * When a work thread has completed its work, it sets .working to 0 and
2494 * signals the main thread and waits on the condition that .data_ready
2497 * The main thread steals half of the work from the worker that has
2498 * most work left to hand it to the idle worker.
2501 struct thread_params
{
2503 struct object_entry
**list
;
2510 pthread_mutex_t mutex
;
2511 pthread_cond_t cond
;
2512 unsigned *processed
;
2515 static pthread_cond_t progress_cond
;
2518 * Mutex and conditional variable can't be statically-initialized on Windows.
2520 static void init_threaded_search(void)
2522 pthread_mutex_init(&cache_mutex
, NULL
);
2523 pthread_mutex_init(&progress_mutex
, NULL
);
2524 pthread_cond_init(&progress_cond
, NULL
);
2525 old_try_to_free_routine
= set_try_to_free_routine(try_to_free_from_threads
);
2528 static void cleanup_threaded_search(void)
2530 set_try_to_free_routine(old_try_to_free_routine
);
2531 pthread_cond_destroy(&progress_cond
);
2532 pthread_mutex_destroy(&cache_mutex
);
2533 pthread_mutex_destroy(&progress_mutex
);
2536 static void *threaded_find_deltas(void *arg
)
2538 struct thread_params
*me
= arg
;
2541 while (me
->remaining
) {
2544 find_deltas(me
->list
, &me
->remaining
,
2545 me
->window
, me
->depth
, me
->processed
);
2549 pthread_cond_signal(&progress_cond
);
2553 * We must not set ->data_ready before we wait on the
2554 * condition because the main thread may have set it to 1
2555 * before we get here. In order to be sure that new
2556 * work is available if we see 1 in ->data_ready, it
2557 * was initialized to 0 before this thread was spawned
2558 * and we reset it to 0 right away.
2560 pthread_mutex_lock(&me
->mutex
);
2561 while (!me
->data_ready
)
2562 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2564 pthread_mutex_unlock(&me
->mutex
);
2569 /* leave ->working 1 so that this doesn't get more work assigned */
2573 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2574 int window
, int depth
, unsigned *processed
)
2576 struct thread_params
*p
;
2577 int i
, ret
, active_threads
= 0;
2579 init_threaded_search();
2581 if (delta_search_threads
<= 1) {
2582 find_deltas(list
, &list_size
, window
, depth
, processed
);
2583 cleanup_threaded_search();
2586 if (progress
> pack_to_stdout
)
2587 fprintf_ln(stderr
, _("Delta compression using up to %d threads"),
2588 delta_search_threads
);
2589 p
= xcalloc(delta_search_threads
, sizeof(*p
));
2591 /* Partition the work amongst work threads. */
2592 for (i
= 0; i
< delta_search_threads
; i
++) {
2593 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
2595 /* don't use too small segments or no deltas will be found */
2596 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
2599 p
[i
].window
= window
;
2601 p
[i
].processed
= processed
;
2603 p
[i
].data_ready
= 0;
2605 /* try to split chunks on "path" boundaries */
2606 while (sub_size
&& sub_size
< list_size
&&
2607 list
[sub_size
]->hash
&&
2608 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
2612 p
[i
].list_size
= sub_size
;
2613 p
[i
].remaining
= sub_size
;
2616 list_size
-= sub_size
;
2619 /* Start work threads. */
2620 for (i
= 0; i
< delta_search_threads
; i
++) {
2621 if (!p
[i
].list_size
)
2623 pthread_mutex_init(&p
[i
].mutex
, NULL
);
2624 pthread_cond_init(&p
[i
].cond
, NULL
);
2625 ret
= pthread_create(&p
[i
].thread
, NULL
,
2626 threaded_find_deltas
, &p
[i
]);
2628 die(_("unable to create thread: %s"), strerror(ret
));
2633 * Now let's wait for work completion. Each time a thread is done
2634 * with its work, we steal half of the remaining work from the
2635 * thread with the largest number of unprocessed objects and give
2636 * it to that newly idle thread. This ensure good load balancing
2637 * until the remaining object list segments are simply too short
2638 * to be worth splitting anymore.
2640 while (active_threads
) {
2641 struct thread_params
*target
= NULL
;
2642 struct thread_params
*victim
= NULL
;
2643 unsigned sub_size
= 0;
2647 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
2652 pthread_cond_wait(&progress_cond
, &progress_mutex
);
2655 for (i
= 0; i
< delta_search_threads
; i
++)
2656 if (p
[i
].remaining
> 2*window
&&
2657 (!victim
|| victim
->remaining
< p
[i
].remaining
))
2660 sub_size
= victim
->remaining
/ 2;
2661 list
= victim
->list
+ victim
->list_size
- sub_size
;
2662 while (sub_size
&& list
[0]->hash
&&
2663 list
[0]->hash
== list
[-1]->hash
) {
2669 * It is possible for some "paths" to have
2670 * so many objects that no hash boundary
2671 * might be found. Let's just steal the
2672 * exact half in that case.
2674 sub_size
= victim
->remaining
/ 2;
2677 target
->list
= list
;
2678 victim
->list_size
-= sub_size
;
2679 victim
->remaining
-= sub_size
;
2681 target
->list_size
= sub_size
;
2682 target
->remaining
= sub_size
;
2683 target
->working
= 1;
2686 pthread_mutex_lock(&target
->mutex
);
2687 target
->data_ready
= 1;
2688 pthread_cond_signal(&target
->cond
);
2689 pthread_mutex_unlock(&target
->mutex
);
2692 pthread_join(target
->thread
, NULL
);
2693 pthread_cond_destroy(&target
->cond
);
2694 pthread_mutex_destroy(&target
->mutex
);
2698 cleanup_threaded_search();
2702 static int obj_is_packed(const struct object_id
*oid
)
2704 return packlist_find(&to_pack
, oid
, NULL
) ||
2705 (reuse_packfile_bitmap
&&
2706 bitmap_walk_contains(bitmap_git
, reuse_packfile_bitmap
, oid
));
2709 static void add_tag_chain(const struct object_id
*oid
)
2714 * We catch duplicates already in add_object_entry(), but we'd
2715 * prefer to do this extra check to avoid having to parse the
2716 * tag at all if we already know that it's being packed (e.g., if
2717 * it was included via bitmaps, we would not have parsed it
2720 if (obj_is_packed(oid
))
2723 tag
= lookup_tag(the_repository
, oid
);
2725 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
2726 die(_("unable to pack objects reachable from tag %s"),
2729 add_object_entry(&tag
->object
.oid
, OBJ_TAG
, NULL
, 0);
2731 if (tag
->tagged
->type
!= OBJ_TAG
)
2734 tag
= (struct tag
*)tag
->tagged
;
2738 static int add_ref_tag(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
2740 struct object_id peeled
;
2742 if (starts_with(path
, "refs/tags/") && /* is a tag? */
2743 !peel_ref(path
, &peeled
) && /* peelable? */
2744 obj_is_packed(&peeled
)) /* object packed? */
2749 static void prepare_pack(int window
, int depth
)
2751 struct object_entry
**delta_list
;
2752 uint32_t i
, nr_deltas
;
2755 if (use_delta_islands
)
2756 resolve_tree_islands(the_repository
, progress
, &to_pack
);
2758 get_object_details();
2761 * If we're locally repacking then we need to be doubly careful
2762 * from now on in order to make sure no stealth corruption gets
2763 * propagated to the new pack. Clients receiving streamed packs
2764 * should validate everything they get anyway so no need to incur
2765 * the additional cost here in that case.
2767 if (!pack_to_stdout
)
2768 do_check_packed_object_crc
= 1;
2770 if (!to_pack
.nr_objects
|| !window
|| !depth
)
2773 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
2776 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2777 struct object_entry
*entry
= to_pack
.objects
+ i
;
2780 /* This happens if we decided to reuse existing
2781 * delta from a pack. "reuse_delta &&" is implied.
2785 if (!entry
->type_valid
||
2786 oe_size_less_than(&to_pack
, entry
, 50))
2789 if (entry
->no_try_delta
)
2792 if (!entry
->preferred_base
) {
2794 if (oe_type(entry
) < 0)
2795 die(_("unable to get type of object %s"),
2796 oid_to_hex(&entry
->idx
.oid
));
2798 if (oe_type(entry
) < 0) {
2800 * This object is not found, but we
2801 * don't have to include it anyway.
2807 delta_list
[n
++] = entry
;
2810 if (nr_deltas
&& n
> 1) {
2811 unsigned nr_done
= 0;
2814 progress_state
= start_progress(_("Compressing objects"),
2816 QSORT(delta_list
, n
, type_size_sort
);
2817 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
2818 stop_progress(&progress_state
);
2819 if (nr_done
!= nr_deltas
)
2820 die(_("inconsistency with delta count"));
2825 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2827 if (!strcmp(k
, "pack.window")) {
2828 window
= git_config_int(k
, v
);
2831 if (!strcmp(k
, "pack.windowmemory")) {
2832 window_memory_limit
= git_config_ulong(k
, v
);
2835 if (!strcmp(k
, "pack.depth")) {
2836 depth
= git_config_int(k
, v
);
2839 if (!strcmp(k
, "pack.deltacachesize")) {
2840 max_delta_cache_size
= git_config_int(k
, v
);
2843 if (!strcmp(k
, "pack.deltacachelimit")) {
2844 cache_max_small_delta_size
= git_config_int(k
, v
);
2847 if (!strcmp(k
, "pack.writebitmaphashcache")) {
2848 if (git_config_bool(k
, v
))
2849 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
2851 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
2853 if (!strcmp(k
, "pack.usebitmaps")) {
2854 use_bitmap_index_default
= git_config_bool(k
, v
);
2857 if (!strcmp(k
, "pack.allowpackreuse")) {
2858 allow_pack_reuse
= git_config_bool(k
, v
);
2861 if (!strcmp(k
, "pack.usesparse")) {
2862 sparse
= git_config_bool(k
, v
);
2865 if (!strcmp(k
, "pack.threads")) {
2866 delta_search_threads
= git_config_int(k
, v
);
2867 if (delta_search_threads
< 0)
2868 die(_("invalid number of threads specified (%d)"),
2869 delta_search_threads
);
2870 if (!HAVE_THREADS
&& delta_search_threads
!= 1) {
2871 warning(_("no threads support, ignoring %s"), k
);
2872 delta_search_threads
= 0;
2876 if (!strcmp(k
, "pack.indexversion")) {
2877 pack_idx_opts
.version
= git_config_int(k
, v
);
2878 if (pack_idx_opts
.version
> 2)
2879 die(_("bad pack.indexversion=%"PRIu32
),
2880 pack_idx_opts
.version
);
2883 return git_default_config(k
, v
, cb
);
2886 static void read_object_list_from_stdin(void)
2888 char line
[GIT_MAX_HEXSZ
+ 1 + PATH_MAX
+ 2];
2889 struct object_id oid
;
2893 if (!fgets(line
, sizeof(line
), stdin
)) {
2897 die("BUG: fgets returned NULL, not EOF, not error!");
2903 if (line
[0] == '-') {
2904 if (get_oid_hex(line
+1, &oid
))
2905 die(_("expected edge object ID, got garbage:\n %s"),
2907 add_preferred_base(&oid
);
2910 if (parse_oid_hex(line
, &oid
, &p
))
2911 die(_("expected object ID, got garbage:\n %s"), line
);
2913 add_preferred_base_object(p
+ 1);
2914 add_object_entry(&oid
, OBJ_NONE
, p
+ 1, 0);
2918 /* Remember to update object flag allocation in object.h */
2919 #define OBJECT_ADDED (1u<<20)
2921 static void show_commit(struct commit
*commit
, void *data
)
2923 add_object_entry(&commit
->object
.oid
, OBJ_COMMIT
, NULL
, 0);
2924 commit
->object
.flags
|= OBJECT_ADDED
;
2926 if (write_bitmap_index
)
2927 index_commit_for_bitmap(commit
);
2929 if (use_delta_islands
)
2930 propagate_island_marks(commit
);
2933 static void show_object(struct object
*obj
, const char *name
, void *data
)
2935 add_preferred_base_object(name
);
2936 add_object_entry(&obj
->oid
, obj
->type
, name
, 0);
2937 obj
->flags
|= OBJECT_ADDED
;
2939 if (use_delta_islands
) {
2942 struct object_entry
*ent
;
2944 /* the empty string is a root tree, which is depth 0 */
2945 depth
= *name
? 1 : 0;
2946 for (p
= strchr(name
, '/'); p
; p
= strchr(p
+ 1, '/'))
2949 ent
= packlist_find(&to_pack
, &obj
->oid
, NULL
);
2950 if (ent
&& depth
> oe_tree_depth(&to_pack
, ent
))
2951 oe_set_tree_depth(&to_pack
, ent
, depth
);
2955 static void show_object__ma_allow_any(struct object
*obj
, const char *name
, void *data
)
2957 assert(arg_missing_action
== MA_ALLOW_ANY
);
2960 * Quietly ignore ALL missing objects. This avoids problems with
2961 * staging them now and getting an odd error later.
2963 if (!has_object_file(&obj
->oid
))
2966 show_object(obj
, name
, data
);
2969 static void show_object__ma_allow_promisor(struct object
*obj
, const char *name
, void *data
)
2971 assert(arg_missing_action
== MA_ALLOW_PROMISOR
);
2974 * Quietly ignore EXPECTED missing objects. This avoids problems with
2975 * staging them now and getting an odd error later.
2977 if (!has_object_file(&obj
->oid
) && is_promisor_object(&obj
->oid
))
2980 show_object(obj
, name
, data
);
2983 static int option_parse_missing_action(const struct option
*opt
,
2984 const char *arg
, int unset
)
2989 if (!strcmp(arg
, "error")) {
2990 arg_missing_action
= MA_ERROR
;
2991 fn_show_object
= show_object
;
2995 if (!strcmp(arg
, "allow-any")) {
2996 arg_missing_action
= MA_ALLOW_ANY
;
2997 fetch_if_missing
= 0;
2998 fn_show_object
= show_object__ma_allow_any
;
3002 if (!strcmp(arg
, "allow-promisor")) {
3003 arg_missing_action
= MA_ALLOW_PROMISOR
;
3004 fetch_if_missing
= 0;
3005 fn_show_object
= show_object__ma_allow_promisor
;
3009 die(_("invalid value for --missing"));
3013 static void show_edge(struct commit
*commit
)
3015 add_preferred_base(&commit
->object
.oid
);
3018 struct in_pack_object
{
3020 struct object
*object
;
3026 struct in_pack_object
*array
;
3029 static void mark_in_pack_object(struct object
*object
, struct packed_git
*p
, struct in_pack
*in_pack
)
3031 in_pack
->array
[in_pack
->nr
].offset
= find_pack_entry_one(object
->oid
.hash
, p
);
3032 in_pack
->array
[in_pack
->nr
].object
= object
;
3037 * Compare the objects in the offset order, in order to emulate the
3038 * "git rev-list --objects" output that produced the pack originally.
3040 static int ofscmp(const void *a_
, const void *b_
)
3042 struct in_pack_object
*a
= (struct in_pack_object
*)a_
;
3043 struct in_pack_object
*b
= (struct in_pack_object
*)b_
;
3045 if (a
->offset
< b
->offset
)
3047 else if (a
->offset
> b
->offset
)
3050 return oidcmp(&a
->object
->oid
, &b
->object
->oid
);
3053 static void add_objects_in_unpacked_packs(void)
3055 struct packed_git
*p
;
3056 struct in_pack in_pack
;
3059 memset(&in_pack
, 0, sizeof(in_pack
));
3061 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3062 struct object_id oid
;
3065 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
3067 if (open_pack_index(p
))
3068 die(_("cannot open pack index"));
3070 ALLOC_GROW(in_pack
.array
,
3071 in_pack
.nr
+ p
->num_objects
,
3074 for (i
= 0; i
< p
->num_objects
; i
++) {
3075 nth_packed_object_oid(&oid
, p
, i
);
3076 o
= lookup_unknown_object(&oid
);
3077 if (!(o
->flags
& OBJECT_ADDED
))
3078 mark_in_pack_object(o
, p
, &in_pack
);
3079 o
->flags
|= OBJECT_ADDED
;
3084 QSORT(in_pack
.array
, in_pack
.nr
, ofscmp
);
3085 for (i
= 0; i
< in_pack
.nr
; i
++) {
3086 struct object
*o
= in_pack
.array
[i
].object
;
3087 add_object_entry(&o
->oid
, o
->type
, "", 0);
3090 free(in_pack
.array
);
3093 static int add_loose_object(const struct object_id
*oid
, const char *path
,
3096 enum object_type type
= oid_object_info(the_repository
, oid
, NULL
);
3099 warning(_("loose object at %s could not be examined"), path
);
3103 add_object_entry(oid
, type
, "", 0);
3108 * We actually don't even have to worry about reachability here.
3109 * add_object_entry will weed out duplicates, so we just add every
3110 * loose object we find.
3112 static void add_unreachable_loose_objects(void)
3114 for_each_loose_file_in_objdir(get_object_directory(),
3119 static int has_sha1_pack_kept_or_nonlocal(const struct object_id
*oid
)
3121 static struct packed_git
*last_found
= (void *)1;
3122 struct packed_git
*p
;
3124 p
= (last_found
!= (void *)1) ? last_found
:
3125 get_all_packs(the_repository
);
3128 if ((!p
->pack_local
|| p
->pack_keep
||
3129 p
->pack_keep_in_core
) &&
3130 find_pack_entry_one(oid
->hash
, p
)) {
3134 if (p
== last_found
)
3135 p
= get_all_packs(the_repository
);
3138 if (p
== last_found
)
3145 * Store a list of sha1s that are should not be discarded
3146 * because they are either written too recently, or are
3147 * reachable from another object that was.
3149 * This is filled by get_object_list.
3151 static struct oid_array recent_objects
;
3153 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
3156 if (!unpack_unreachable_expiration
)
3158 if (mtime
> unpack_unreachable_expiration
)
3160 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
3165 static void loosen_unused_packed_objects(void)
3167 struct packed_git
*p
;
3169 struct object_id oid
;
3171 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3172 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
3175 if (open_pack_index(p
))
3176 die(_("cannot open pack index"));
3178 for (i
= 0; i
< p
->num_objects
; i
++) {
3179 nth_packed_object_oid(&oid
, p
, i
);
3180 if (!packlist_find(&to_pack
, &oid
, NULL
) &&
3181 !has_sha1_pack_kept_or_nonlocal(&oid
) &&
3182 !loosened_object_can_be_discarded(&oid
, p
->mtime
))
3183 if (force_object_loose(&oid
, p
->mtime
))
3184 die(_("unable to force loose object"));
3190 * This tracks any options which pack-reuse code expects to be on, or which a
3191 * reader of the pack might not understand, and which would therefore prevent
3192 * blind reuse of what we have on disk.
3194 static int pack_options_allow_reuse(void)
3196 return allow_pack_reuse
&&
3198 !ignore_packed_keep_on_disk
&&
3199 !ignore_packed_keep_in_core
&&
3200 (!local
|| !have_non_local_packs
) &&
3204 static int get_object_list_from_bitmap(struct rev_info
*revs
)
3206 if (!(bitmap_git
= prepare_bitmap_walk(revs
)))
3209 if (pack_options_allow_reuse() &&
3210 !reuse_partial_packfile_from_bitmap(
3213 &reuse_packfile_objects
,
3214 &reuse_packfile_bitmap
)) {
3215 assert(reuse_packfile_objects
);
3216 nr_result
+= reuse_packfile_objects
;
3217 display_progress(progress_state
, nr_result
);
3220 traverse_bitmap_commit_list(bitmap_git
, &add_object_entry_from_bitmap
);
3224 static void record_recent_object(struct object
*obj
,
3228 oid_array_append(&recent_objects
, &obj
->oid
);
3231 static void record_recent_commit(struct commit
*commit
, void *data
)
3233 oid_array_append(&recent_objects
, &commit
->object
.oid
);
3236 static void get_object_list(int ac
, const char **av
)
3238 struct rev_info revs
;
3239 struct setup_revision_opt s_r_opt
= {
3240 .allow_exclude_promisor_objects
= 1,
3246 repo_init_revisions(the_repository
, &revs
, NULL
);
3247 save_commit_buffer
= 0;
3248 setup_revisions(ac
, av
, &revs
, &s_r_opt
);
3250 /* make sure shallows are read */
3251 is_repository_shallow(the_repository
);
3253 save_warning
= warn_on_object_refname_ambiguity
;
3254 warn_on_object_refname_ambiguity
= 0;
3256 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
3257 int len
= strlen(line
);
3258 if (len
&& line
[len
- 1] == '\n')
3263 if (!strcmp(line
, "--not")) {
3264 flags
^= UNINTERESTING
;
3265 write_bitmap_index
= 0;
3268 if (starts_with(line
, "--shallow ")) {
3269 struct object_id oid
;
3270 if (get_oid_hex(line
+ 10, &oid
))
3271 die("not an SHA-1 '%s'", line
+ 10);
3272 register_shallow(the_repository
, &oid
);
3273 use_bitmap_index
= 0;
3276 die(_("not a rev '%s'"), line
);
3278 if (handle_revision_arg(line
, &revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
3279 die(_("bad revision '%s'"), line
);
3282 warn_on_object_refname_ambiguity
= save_warning
;
3284 if (use_bitmap_index
&& !get_object_list_from_bitmap(&revs
))
3287 if (use_delta_islands
)
3288 load_delta_islands(the_repository
, progress
);
3290 if (prepare_revision_walk(&revs
))
3291 die(_("revision walk setup failed"));
3292 mark_edges_uninteresting(&revs
, show_edge
, sparse
);
3294 if (!fn_show_object
)
3295 fn_show_object
= show_object
;
3296 traverse_commit_list_filtered(&filter_options
, &revs
,
3297 show_commit
, fn_show_object
, NULL
,
3300 if (unpack_unreachable_expiration
) {
3301 revs
.ignore_missing_links
= 1;
3302 if (add_unseen_recent_objects_to_traversal(&revs
,
3303 unpack_unreachable_expiration
))
3304 die(_("unable to add recent objects"));
3305 if (prepare_revision_walk(&revs
))
3306 die(_("revision walk setup failed"));
3307 traverse_commit_list(&revs
, record_recent_commit
,
3308 record_recent_object
, NULL
);
3311 if (keep_unreachable
)
3312 add_objects_in_unpacked_packs();
3313 if (pack_loose_unreachable
)
3314 add_unreachable_loose_objects();
3315 if (unpack_unreachable
)
3316 loosen_unused_packed_objects();
3318 oid_array_clear(&recent_objects
);
3321 static void add_extra_kept_packs(const struct string_list
*names
)
3323 struct packed_git
*p
;
3328 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3329 const char *name
= basename(p
->pack_name
);
3335 for (i
= 0; i
< names
->nr
; i
++)
3336 if (!fspathcmp(name
, names
->items
[i
].string
))
3339 if (i
< names
->nr
) {
3340 p
->pack_keep_in_core
= 1;
3341 ignore_packed_keep_in_core
= 1;
3347 static int option_parse_index_version(const struct option
*opt
,
3348 const char *arg
, int unset
)
3351 const char *val
= arg
;
3353 BUG_ON_OPT_NEG(unset
);
3355 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
3356 if (pack_idx_opts
.version
> 2)
3357 die(_("unsupported index version %s"), val
);
3358 if (*c
== ',' && c
[1])
3359 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
3360 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
3361 die(_("bad index version '%s'"), val
);
3365 static int option_parse_unpack_unreachable(const struct option
*opt
,
3366 const char *arg
, int unset
)
3369 unpack_unreachable
= 0;
3370 unpack_unreachable_expiration
= 0;
3373 unpack_unreachable
= 1;
3375 unpack_unreachable_expiration
= approxidate(arg
);
3380 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
3382 int use_internal_rev_list
= 0;
3384 int all_progress_implied
= 0;
3385 struct argv_array rp
= ARGV_ARRAY_INIT
;
3386 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
3387 int rev_list_index
= 0;
3388 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
3389 struct option pack_objects_options
[] = {
3390 OPT_SET_INT('q', "quiet", &progress
,
3391 N_("do not show progress meter"), 0),
3392 OPT_SET_INT(0, "progress", &progress
,
3393 N_("show progress meter"), 1),
3394 OPT_SET_INT(0, "all-progress", &progress
,
3395 N_("show progress meter during object writing phase"), 2),
3396 OPT_BOOL(0, "all-progress-implied",
3397 &all_progress_implied
,
3398 N_("similar to --all-progress when progress meter is shown")),
3399 { OPTION_CALLBACK
, 0, "index-version", NULL
, N_("<version>[,<offset>]"),
3400 N_("write the pack index file in the specified idx format version"),
3401 PARSE_OPT_NONEG
, option_parse_index_version
},
3402 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
3403 N_("maximum size of each output pack file")),
3404 OPT_BOOL(0, "local", &local
,
3405 N_("ignore borrowed objects from alternate object store")),
3406 OPT_BOOL(0, "incremental", &incremental
,
3407 N_("ignore packed objects")),
3408 OPT_INTEGER(0, "window", &window
,
3409 N_("limit pack window by objects")),
3410 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
3411 N_("limit pack window by memory in addition to object limit")),
3412 OPT_INTEGER(0, "depth", &depth
,
3413 N_("maximum length of delta chain allowed in the resulting pack")),
3414 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
3415 N_("reuse existing deltas")),
3416 OPT_BOOL(0, "reuse-object", &reuse_object
,
3417 N_("reuse existing objects")),
3418 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
3419 N_("use OFS_DELTA objects")),
3420 OPT_INTEGER(0, "threads", &delta_search_threads
,
3421 N_("use threads when searching for best delta matches")),
3422 OPT_BOOL(0, "non-empty", &non_empty
,
3423 N_("do not create an empty pack output")),
3424 OPT_BOOL(0, "revs", &use_internal_rev_list
,
3425 N_("read revision arguments from standard input")),
3426 OPT_SET_INT_F(0, "unpacked", &rev_list_unpacked
,
3427 N_("limit the objects to those that are not yet packed"),
3428 1, PARSE_OPT_NONEG
),
3429 OPT_SET_INT_F(0, "all", &rev_list_all
,
3430 N_("include objects reachable from any reference"),
3431 1, PARSE_OPT_NONEG
),
3432 OPT_SET_INT_F(0, "reflog", &rev_list_reflog
,
3433 N_("include objects referred by reflog entries"),
3434 1, PARSE_OPT_NONEG
),
3435 OPT_SET_INT_F(0, "indexed-objects", &rev_list_index
,
3436 N_("include objects referred to by the index"),
3437 1, PARSE_OPT_NONEG
),
3438 OPT_BOOL(0, "stdout", &pack_to_stdout
,
3439 N_("output pack to stdout")),
3440 OPT_BOOL(0, "include-tag", &include_tag
,
3441 N_("include tag objects that refer to objects to be packed")),
3442 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
3443 N_("keep unreachable objects")),
3444 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
3445 N_("pack loose unreachable objects")),
3446 { OPTION_CALLBACK
, 0, "unpack-unreachable", NULL
, N_("time"),
3447 N_("unpack unreachable objects newer than <time>"),
3448 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
},
3449 OPT_BOOL(0, "sparse", &sparse
,
3450 N_("use the sparse reachability algorithm")),
3451 OPT_BOOL(0, "thin", &thin
,
3452 N_("create thin packs")),
3453 OPT_BOOL(0, "shallow", &shallow
,
3454 N_("create packs suitable for shallow fetches")),
3455 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk
,
3456 N_("ignore packs that have companion .keep file")),
3457 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
3458 N_("ignore this pack")),
3459 OPT_INTEGER(0, "compression", &pack_compression_level
,
3460 N_("pack compression level")),
3461 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents
,
3462 N_("do not hide commits by grafts"), 0),
3463 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
3464 N_("use a bitmap index if available to speed up counting objects")),
3465 OPT_SET_INT(0, "write-bitmap-index", &write_bitmap_index
,
3466 N_("write a bitmap index together with the pack index"),
3468 OPT_SET_INT_F(0, "write-bitmap-index-quiet",
3469 &write_bitmap_index
,
3470 N_("write a bitmap index if possible"),
3471 WRITE_BITMAP_QUIET
, PARSE_OPT_HIDDEN
),
3472 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
3473 { OPTION_CALLBACK
, 0, "missing", NULL
, N_("action"),
3474 N_("handling for missing objects"), PARSE_OPT_NONEG
,
3475 option_parse_missing_action
},
3476 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
3477 N_("do not pack objects in promisor packfiles")),
3478 OPT_BOOL(0, "delta-islands", &use_delta_islands
,
3479 N_("respect islands during delta compression")),
3483 if (DFS_NUM_STATES
> (1 << OE_DFS_STATE_BITS
))
3484 BUG("too many dfs states, increase OE_DFS_STATE_BITS");
3486 read_replace_refs
= 0;
3488 sparse
= git_env_bool("GIT_TEST_PACK_SPARSE", 0);
3489 reset_pack_idx_option(&pack_idx_opts
);
3490 git_config(git_pack_config
, NULL
);
3492 progress
= isatty(2);
3493 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
3497 base_name
= argv
[0];
3500 if (pack_to_stdout
!= !base_name
|| argc
)
3501 usage_with_options(pack_usage
, pack_objects_options
);
3503 if (depth
>= (1 << OE_DEPTH_BITS
)) {
3504 warning(_("delta chain depth %d is too deep, forcing %d"),
3505 depth
, (1 << OE_DEPTH_BITS
) - 1);
3506 depth
= (1 << OE_DEPTH_BITS
) - 1;
3508 if (cache_max_small_delta_size
>= (1U << OE_Z_DELTA_BITS
)) {
3509 warning(_("pack.deltaCacheLimit is too high, forcing %d"),
3510 (1U << OE_Z_DELTA_BITS
) - 1);
3511 cache_max_small_delta_size
= (1U << OE_Z_DELTA_BITS
) - 1;
3514 argv_array_push(&rp
, "pack-objects");
3516 use_internal_rev_list
= 1;
3517 argv_array_push(&rp
, shallow
3518 ? "--objects-edge-aggressive"
3519 : "--objects-edge");
3521 argv_array_push(&rp
, "--objects");
3524 use_internal_rev_list
= 1;
3525 argv_array_push(&rp
, "--all");
3527 if (rev_list_reflog
) {
3528 use_internal_rev_list
= 1;
3529 argv_array_push(&rp
, "--reflog");
3531 if (rev_list_index
) {
3532 use_internal_rev_list
= 1;
3533 argv_array_push(&rp
, "--indexed-objects");
3535 if (rev_list_unpacked
) {
3536 use_internal_rev_list
= 1;
3537 argv_array_push(&rp
, "--unpacked");
3540 if (exclude_promisor_objects
) {
3541 use_internal_rev_list
= 1;
3542 fetch_if_missing
= 0;
3543 argv_array_push(&rp
, "--exclude-promisor-objects");
3545 if (unpack_unreachable
|| keep_unreachable
|| pack_loose_unreachable
)
3546 use_internal_rev_list
= 1;
3550 if (pack_compression_level
== -1)
3551 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
3552 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
3553 die(_("bad pack compression level %d"), pack_compression_level
);
3555 if (!delta_search_threads
) /* --threads=0 means autodetect */
3556 delta_search_threads
= online_cpus();
3558 if (!HAVE_THREADS
&& delta_search_threads
!= 1)
3559 warning(_("no threads support, ignoring --threads"));
3560 if (!pack_to_stdout
&& !pack_size_limit
)
3561 pack_size_limit
= pack_size_limit_cfg
;
3562 if (pack_to_stdout
&& pack_size_limit
)
3563 die(_("--max-pack-size cannot be used to build a pack for transfer"));
3564 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
3565 warning(_("minimum pack size limit is 1 MiB"));
3566 pack_size_limit
= 1024*1024;
3569 if (!pack_to_stdout
&& thin
)
3570 die(_("--thin cannot be used to build an indexable pack"));
3572 if (keep_unreachable
&& unpack_unreachable
)
3573 die(_("--keep-unreachable and --unpack-unreachable are incompatible"));
3574 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
3575 unpack_unreachable_expiration
= 0;
3577 if (filter_options
.choice
) {
3578 if (!pack_to_stdout
)
3579 die(_("cannot use --filter without --stdout"));
3580 use_bitmap_index
= 0;
3584 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
3586 * - to produce good pack (with bitmap index not-yet-packed objects are
3587 * packed in suboptimal order).
3589 * - to use more robust pack-generation codepath (avoiding possible
3590 * bugs in bitmap code and possible bitmap index corruption).
3592 if (!pack_to_stdout
)
3593 use_bitmap_index_default
= 0;
3595 if (use_bitmap_index
< 0)
3596 use_bitmap_index
= use_bitmap_index_default
;
3598 /* "hard" reasons not to use bitmaps; these just won't work at all */
3599 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow(the_repository
))
3600 use_bitmap_index
= 0;
3602 if (pack_to_stdout
|| !rev_list_all
)
3603 write_bitmap_index
= 0;
3605 if (use_delta_islands
)
3606 argv_array_push(&rp
, "--topo-order");
3608 if (progress
&& all_progress_implied
)
3611 add_extra_kept_packs(&keep_pack_list
);
3612 if (ignore_packed_keep_on_disk
) {
3613 struct packed_git
*p
;
3614 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
)
3615 if (p
->pack_local
&& p
->pack_keep
)
3617 if (!p
) /* no keep-able packs found */
3618 ignore_packed_keep_on_disk
= 0;
3622 * unlike ignore_packed_keep_on_disk above, we do not
3623 * want to unset "local" based on looking at packs, as
3624 * it also covers non-local objects
3626 struct packed_git
*p
;
3627 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
3628 if (!p
->pack_local
) {
3629 have_non_local_packs
= 1;
3635 trace2_region_enter("pack-objects", "enumerate-objects",
3637 prepare_packing_data(the_repository
, &to_pack
);
3640 progress_state
= start_progress(_("Enumerating objects"), 0);
3641 if (!use_internal_rev_list
)
3642 read_object_list_from_stdin();
3644 get_object_list(rp
.argc
, rp
.argv
);
3645 argv_array_clear(&rp
);
3647 cleanup_preferred_base();
3648 if (include_tag
&& nr_result
)
3649 for_each_ref(add_ref_tag
, NULL
);
3650 stop_progress(&progress_state
);
3651 trace2_region_leave("pack-objects", "enumerate-objects",
3654 if (non_empty
&& !nr_result
)
3657 trace2_region_enter("pack-objects", "prepare-pack",
3659 prepare_pack(window
, depth
);
3660 trace2_region_leave("pack-objects", "prepare-pack",
3664 trace2_region_enter("pack-objects", "write-pack-file", the_repository
);
3666 trace2_region_leave("pack-objects", "write-pack-file", the_repository
);
3670 _("Total %"PRIu32
" (delta %"PRIu32
"),"
3671 " reused %"PRIu32
" (delta %"PRIu32
"),"
3672 " pack-reused %"PRIu32
),
3673 written
, written_delta
, reused
, reused_delta
,
3674 reuse_packfile_objects
);