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 "reachable.h"
28 #include "sha1-array.h"
29 #include "argv-array.h"
32 #include "object-store.h"
35 static const char *pack_usage
[] = {
36 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
37 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
42 * Objects we are going to pack are collected in the `to_pack` structure.
43 * It contains an array (dynamically expanded) of the object data, and a map
44 * that can resolve SHA1s to their position in the array.
46 static struct packing_data to_pack
;
48 static struct pack_idx_entry
**written_list
;
49 static uint32_t nr_result
, nr_written
, nr_seen
;
52 static int reuse_delta
= 1, reuse_object
= 1;
53 static int keep_unreachable
, unpack_unreachable
, include_tag
;
54 static timestamp_t unpack_unreachable_expiration
;
55 static int pack_loose_unreachable
;
57 static int have_non_local_packs
;
58 static int incremental
;
59 static int ignore_packed_keep_on_disk
;
60 static int ignore_packed_keep_in_core
;
61 static int allow_ofs_delta
;
62 static struct pack_idx_option pack_idx_opts
;
63 static const char *base_name
;
64 static int progress
= 1;
65 static int window
= 10;
66 static unsigned long pack_size_limit
;
67 static int depth
= 50;
68 static int delta_search_threads
;
69 static int pack_to_stdout
;
70 static int num_preferred_base
;
71 static struct progress
*progress_state
;
73 static struct packed_git
*reuse_packfile
;
74 static uint32_t reuse_packfile_objects
;
75 static off_t reuse_packfile_offset
;
77 static int use_bitmap_index_default
= 1;
78 static int use_bitmap_index
= -1;
79 static int write_bitmap_index
;
80 static uint16_t write_bitmap_options
;
82 static int exclude_promisor_objects
;
84 static unsigned long delta_cache_size
= 0;
85 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
86 static unsigned long cache_max_small_delta_size
= 1000;
88 static unsigned long window_memory_limit
= 0;
90 static struct list_objects_filter_options filter_options
;
93 MA_ERROR
= 0, /* fail if any missing objects are encountered */
94 MA_ALLOW_ANY
, /* silently allow ALL missing objects */
95 MA_ALLOW_PROMISOR
, /* silently allow all missing PROMISOR objects */
97 static enum missing_action arg_missing_action
;
98 static show_object_fn fn_show_object
;
103 static uint32_t written
, written_delta
;
104 static uint32_t reused
, reused_delta
;
109 static struct commit
**indexed_commits
;
110 static unsigned int indexed_commits_nr
;
111 static unsigned int indexed_commits_alloc
;
113 static void index_commit_for_bitmap(struct commit
*commit
)
115 if (indexed_commits_nr
>= indexed_commits_alloc
) {
116 indexed_commits_alloc
= (indexed_commits_alloc
+ 32) * 2;
117 REALLOC_ARRAY(indexed_commits
, indexed_commits_alloc
);
120 indexed_commits
[indexed_commits_nr
++] = commit
;
123 static void *get_delta(struct object_entry
*entry
)
125 unsigned long size
, base_size
, delta_size
;
126 void *buf
, *base_buf
, *delta_buf
;
127 enum object_type type
;
129 buf
= read_object_file(&entry
->idx
.oid
, &type
, &size
);
131 die("unable to read %s", oid_to_hex(&entry
->idx
.oid
));
132 base_buf
= read_object_file(&entry
->delta
->idx
.oid
, &type
, &base_size
);
134 die("unable to read %s",
135 oid_to_hex(&entry
->delta
->idx
.oid
));
136 delta_buf
= diff_delta(base_buf
, base_size
,
137 buf
, size
, &delta_size
, 0);
138 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
139 die("delta size changed");
145 static unsigned long do_compress(void **pptr
, unsigned long size
)
149 unsigned long maxsize
;
151 git_deflate_init(&stream
, pack_compression_level
);
152 maxsize
= git_deflate_bound(&stream
, size
);
155 out
= xmalloc(maxsize
);
159 stream
.avail_in
= size
;
160 stream
.next_out
= out
;
161 stream
.avail_out
= maxsize
;
162 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
164 git_deflate_end(&stream
);
167 return stream
.total_out
;
170 static unsigned long write_large_blob_data(struct git_istream
*st
, struct hashfile
*f
,
171 const struct object_id
*oid
)
174 unsigned char ibuf
[1024 * 16];
175 unsigned char obuf
[1024 * 16];
176 unsigned long olen
= 0;
178 git_deflate_init(&stream
, pack_compression_level
);
183 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
185 die(_("unable to read %s"), oid_to_hex(oid
));
187 stream
.next_in
= ibuf
;
188 stream
.avail_in
= readlen
;
189 while ((stream
.avail_in
|| readlen
== 0) &&
190 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
191 stream
.next_out
= obuf
;
192 stream
.avail_out
= sizeof(obuf
);
193 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
194 hashwrite(f
, obuf
, stream
.next_out
- obuf
);
195 olen
+= stream
.next_out
- obuf
;
198 die(_("deflate error (%d)"), zret
);
200 if (zret
!= Z_STREAM_END
)
201 die(_("deflate error (%d)"), zret
);
205 git_deflate_end(&stream
);
210 * we are going to reuse the existing object data as is. make
211 * sure it is not corrupt.
213 static int check_pack_inflate(struct packed_git
*p
,
214 struct pack_window
**w_curs
,
217 unsigned long expect
)
220 unsigned char fakebuf
[4096], *in
;
223 memset(&stream
, 0, sizeof(stream
));
224 git_inflate_init(&stream
);
226 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
228 stream
.next_out
= fakebuf
;
229 stream
.avail_out
= sizeof(fakebuf
);
230 st
= git_inflate(&stream
, Z_FINISH
);
231 offset
+= stream
.next_in
- in
;
232 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
233 git_inflate_end(&stream
);
234 return (st
== Z_STREAM_END
&&
235 stream
.total_out
== expect
&&
236 stream
.total_in
== len
) ? 0 : -1;
239 static void copy_pack_data(struct hashfile
*f
,
240 struct packed_git
*p
,
241 struct pack_window
**w_curs
,
249 in
= use_pack(p
, w_curs
, offset
, &avail
);
251 avail
= (unsigned long)len
;
252 hashwrite(f
, in
, avail
);
258 /* Return 0 if we will bust the pack-size limit */
259 static unsigned long write_no_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
260 unsigned long limit
, int usable_delta
)
262 unsigned long size
, datalen
;
263 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
264 dheader
[MAX_PACK_OBJECT_HEADER
];
266 enum object_type type
;
268 struct git_istream
*st
= NULL
;
271 if (entry
->type
== OBJ_BLOB
&&
272 entry
->size
> big_file_threshold
&&
273 (st
= open_istream(&entry
->idx
.oid
, &type
, &size
, NULL
)) != NULL
)
276 buf
= read_object_file(&entry
->idx
.oid
, &type
, &size
);
278 die(_("unable to read %s"),
279 oid_to_hex(&entry
->idx
.oid
));
282 * make sure no cached delta data remains from a
283 * previous attempt before a pack split occurred.
285 FREE_AND_NULL(entry
->delta_data
);
286 entry
->z_delta_size
= 0;
287 } else if (entry
->delta_data
) {
288 size
= entry
->delta_size
;
289 buf
= entry
->delta_data
;
290 entry
->delta_data
= NULL
;
291 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
292 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
294 buf
= get_delta(entry
);
295 size
= entry
->delta_size
;
296 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
297 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
300 if (st
) /* large blob case, just assume we don't compress well */
302 else if (entry
->z_delta_size
)
303 datalen
= entry
->z_delta_size
;
305 datalen
= do_compress(&buf
, size
);
308 * The object header is a byte of 'type' followed by zero or
309 * more bytes of length.
311 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
314 if (type
== OBJ_OFS_DELTA
) {
316 * Deltas with relative base contain an additional
317 * encoding of the relative offset for the delta
318 * base from this object's position in the pack.
320 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
321 unsigned pos
= sizeof(dheader
) - 1;
322 dheader
[pos
] = ofs
& 127;
324 dheader
[--pos
] = 128 | (--ofs
& 127);
325 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
331 hashwrite(f
, header
, hdrlen
);
332 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
333 hdrlen
+= sizeof(dheader
) - pos
;
334 } else if (type
== OBJ_REF_DELTA
) {
336 * Deltas with a base reference contain
337 * an additional 20 bytes for the base sha1.
339 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
345 hashwrite(f
, header
, hdrlen
);
346 hashwrite(f
, entry
->delta
->idx
.oid
.hash
, 20);
349 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
355 hashwrite(f
, header
, hdrlen
);
358 datalen
= write_large_blob_data(st
, f
, &entry
->idx
.oid
);
361 hashwrite(f
, buf
, datalen
);
365 return hdrlen
+ datalen
;
368 /* Return 0 if we will bust the pack-size limit */
369 static off_t
write_reuse_object(struct hashfile
*f
, struct object_entry
*entry
,
370 unsigned long limit
, int usable_delta
)
372 struct packed_git
*p
= entry
->in_pack
;
373 struct pack_window
*w_curs
= NULL
;
374 struct revindex_entry
*revidx
;
376 enum object_type type
= entry
->type
;
378 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
379 dheader
[MAX_PACK_OBJECT_HEADER
];
383 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
384 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
385 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
388 offset
= entry
->in_pack_offset
;
389 revidx
= find_pack_revindex(p
, offset
);
390 datalen
= revidx
[1].offset
- offset
;
391 if (!pack_to_stdout
&& p
->index_version
> 1 &&
392 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
)) {
393 error("bad packed object CRC for %s",
394 oid_to_hex(&entry
->idx
.oid
));
396 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
399 offset
+= entry
->in_pack_header_size
;
400 datalen
-= entry
->in_pack_header_size
;
402 if (!pack_to_stdout
&& p
->index_version
== 1 &&
403 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
)) {
404 error("corrupt packed object for %s",
405 oid_to_hex(&entry
->idx
.oid
));
407 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
410 if (type
== OBJ_OFS_DELTA
) {
411 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
412 unsigned pos
= sizeof(dheader
) - 1;
413 dheader
[pos
] = ofs
& 127;
415 dheader
[--pos
] = 128 | (--ofs
& 127);
416 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
420 hashwrite(f
, header
, hdrlen
);
421 hashwrite(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
422 hdrlen
+= sizeof(dheader
) - pos
;
424 } else if (type
== OBJ_REF_DELTA
) {
425 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
429 hashwrite(f
, header
, hdrlen
);
430 hashwrite(f
, entry
->delta
->idx
.oid
.hash
, 20);
434 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
438 hashwrite(f
, header
, hdrlen
);
440 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
443 return hdrlen
+ datalen
;
446 /* Return 0 if we will bust the pack-size limit */
447 static off_t
write_object(struct hashfile
*f
,
448 struct object_entry
*entry
,
453 int usable_delta
, to_reuse
;
458 /* apply size limit if limited packsize and not first object */
459 if (!pack_size_limit
|| !nr_written
)
461 else if (pack_size_limit
<= write_offset
)
463 * the earlier object did not fit the limit; avoid
464 * mistaking this with unlimited (i.e. limit = 0).
468 limit
= pack_size_limit
- write_offset
;
471 usable_delta
= 0; /* no delta */
472 else if (!pack_size_limit
)
473 usable_delta
= 1; /* unlimited packfile */
474 else if (entry
->delta
->idx
.offset
== (off_t
)-1)
475 usable_delta
= 0; /* base was written to another pack */
476 else if (entry
->delta
->idx
.offset
)
477 usable_delta
= 1; /* base already exists in this pack */
479 usable_delta
= 0; /* base could end up in another pack */
482 to_reuse
= 0; /* explicit */
483 else if (!entry
->in_pack
)
484 to_reuse
= 0; /* can't reuse what we don't have */
485 else if (entry
->type
== OBJ_REF_DELTA
|| entry
->type
== OBJ_OFS_DELTA
)
486 /* check_object() decided it for us ... */
487 to_reuse
= usable_delta
;
488 /* ... but pack split may override that */
489 else if (entry
->type
!= entry
->in_pack_type
)
490 to_reuse
= 0; /* pack has delta which is unusable */
491 else if (entry
->delta
)
492 to_reuse
= 0; /* we want to pack afresh */
494 to_reuse
= 1; /* we have it in-pack undeltified,
495 * and we do not need to deltify it.
499 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
501 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
509 entry
->idx
.crc32
= crc32_end(f
);
513 enum write_one_status
{
514 WRITE_ONE_SKIP
= -1, /* already written */
515 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
516 WRITE_ONE_WRITTEN
= 1, /* normal */
517 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
520 static enum write_one_status
write_one(struct hashfile
*f
,
521 struct object_entry
*e
,
528 * we set offset to 1 (which is an impossible value) to mark
529 * the fact that this object is involved in "write its base
530 * first before writing a deltified object" recursion.
532 recursing
= (e
->idx
.offset
== 1);
534 warning("recursive delta detected for object %s",
535 oid_to_hex(&e
->idx
.oid
));
536 return WRITE_ONE_RECURSIVE
;
537 } else if (e
->idx
.offset
|| e
->preferred_base
) {
538 /* offset is non zero if object is written already. */
539 return WRITE_ONE_SKIP
;
542 /* if we are deltified, write out base object first. */
544 e
->idx
.offset
= 1; /* now recurse */
545 switch (write_one(f
, e
->delta
, offset
)) {
546 case WRITE_ONE_RECURSIVE
:
547 /* we cannot depend on this one */
552 case WRITE_ONE_BREAK
:
553 e
->idx
.offset
= recursing
;
554 return WRITE_ONE_BREAK
;
558 e
->idx
.offset
= *offset
;
559 size
= write_object(f
, e
, *offset
);
561 e
->idx
.offset
= recursing
;
562 return WRITE_ONE_BREAK
;
564 written_list
[nr_written
++] = &e
->idx
;
566 /* make sure off_t is sufficiently large not to wrap */
567 if (signed_add_overflows(*offset
, size
))
568 die("pack too large for current definition of off_t");
570 return WRITE_ONE_WRITTEN
;
573 static int mark_tagged(const char *path
, const struct object_id
*oid
, int flag
,
576 struct object_id peeled
;
577 struct object_entry
*entry
= packlist_find(&to_pack
, oid
->hash
, NULL
);
581 if (!peel_ref(path
, &peeled
)) {
582 entry
= packlist_find(&to_pack
, peeled
.hash
, NULL
);
589 static inline void add_to_write_order(struct object_entry
**wo
,
591 struct object_entry
*e
)
599 static void add_descendants_to_write_order(struct object_entry
**wo
,
601 struct object_entry
*e
)
603 int add_to_order
= 1;
606 struct object_entry
*s
;
607 /* add this node... */
608 add_to_write_order(wo
, endp
, e
);
609 /* all its siblings... */
610 for (s
= e
->delta_sibling
; s
; s
= s
->delta_sibling
) {
611 add_to_write_order(wo
, endp
, s
);
614 /* drop down a level to add left subtree nodes if possible */
615 if (e
->delta_child
) {
620 /* our sibling might have some children, it is next */
621 if (e
->delta_sibling
) {
622 e
= e
->delta_sibling
;
625 /* go back to our parent node */
627 while (e
&& !e
->delta_sibling
) {
628 /* we're on the right side of a subtree, keep
629 * going up until we can go right again */
633 /* done- we hit our original root node */
636 /* pass it off to sibling at this level */
637 e
= e
->delta_sibling
;
642 static void add_family_to_write_order(struct object_entry
**wo
,
644 struct object_entry
*e
)
646 struct object_entry
*root
;
648 for (root
= e
; root
->delta
; root
= root
->delta
)
650 add_descendants_to_write_order(wo
, endp
, root
);
653 static struct object_entry
**compute_write_order(void)
655 unsigned int i
, wo_end
, last_untagged
;
657 struct object_entry
**wo
;
658 struct object_entry
*objects
= to_pack
.objects
;
660 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
661 objects
[i
].tagged
= 0;
662 objects
[i
].filled
= 0;
663 objects
[i
].delta_child
= NULL
;
664 objects
[i
].delta_sibling
= NULL
;
668 * Fully connect delta_child/delta_sibling network.
669 * Make sure delta_sibling is sorted in the original
672 for (i
= to_pack
.nr_objects
; i
> 0;) {
673 struct object_entry
*e
= &objects
[--i
];
676 /* Mark me as the first child */
677 e
->delta_sibling
= e
->delta
->delta_child
;
678 e
->delta
->delta_child
= e
;
682 * Mark objects that are at the tip of tags.
684 for_each_tag_ref(mark_tagged
, NULL
);
687 * Give the objects in the original recency order until
688 * we see a tagged tip.
690 ALLOC_ARRAY(wo
, to_pack
.nr_objects
);
691 for (i
= wo_end
= 0; i
< to_pack
.nr_objects
; i
++) {
692 if (objects
[i
].tagged
)
694 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
699 * Then fill all the tagged tips.
701 for (; i
< to_pack
.nr_objects
; i
++) {
702 if (objects
[i
].tagged
)
703 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
707 * And then all remaining commits and tags.
709 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
710 if (objects
[i
].type
!= OBJ_COMMIT
&&
711 objects
[i
].type
!= OBJ_TAG
)
713 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
717 * And then all the trees.
719 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
720 if (objects
[i
].type
!= OBJ_TREE
)
722 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
726 * Finally all the rest in really tight order
728 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
729 if (!objects
[i
].filled
)
730 add_family_to_write_order(wo
, &wo_end
, &objects
[i
]);
733 if (wo_end
!= to_pack
.nr_objects
)
734 die("ordered %u objects, expected %"PRIu32
, wo_end
, to_pack
.nr_objects
);
739 static off_t
write_reused_pack(struct hashfile
*f
)
741 unsigned char buffer
[8192];
742 off_t to_write
, total
;
745 if (!is_pack_valid(reuse_packfile
))
746 die("packfile is invalid: %s", reuse_packfile
->pack_name
);
748 fd
= git_open(reuse_packfile
->pack_name
);
750 die_errno("unable to open packfile for reuse: %s",
751 reuse_packfile
->pack_name
);
753 if (lseek(fd
, sizeof(struct pack_header
), SEEK_SET
) == -1)
754 die_errno("unable to seek in reused packfile");
756 if (reuse_packfile_offset
< 0)
757 reuse_packfile_offset
= reuse_packfile
->pack_size
- 20;
759 total
= to_write
= reuse_packfile_offset
- sizeof(struct pack_header
);
762 int read_pack
= xread(fd
, buffer
, sizeof(buffer
));
765 die_errno("unable to read from reused packfile");
767 if (read_pack
> to_write
)
768 read_pack
= to_write
;
770 hashwrite(f
, buffer
, read_pack
);
771 to_write
-= read_pack
;
774 * We don't know the actual number of objects written,
775 * only how many bytes written, how many bytes total, and
776 * how many objects total. So we can fake it by pretending all
777 * objects we are writing are the same size. This gives us a
778 * smooth progress meter, and at the end it matches the true
781 written
= reuse_packfile_objects
*
782 (((double)(total
- to_write
)) / total
);
783 display_progress(progress_state
, written
);
787 written
= reuse_packfile_objects
;
788 display_progress(progress_state
, written
);
789 return reuse_packfile_offset
- sizeof(struct pack_header
);
792 static const char no_split_warning
[] = N_(
793 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
796 static void write_pack_file(void)
801 uint32_t nr_remaining
= nr_result
;
802 time_t last_mtime
= 0;
803 struct object_entry
**write_order
;
805 if (progress
> pack_to_stdout
)
806 progress_state
= start_progress(_("Writing objects"), nr_result
);
807 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
808 write_order
= compute_write_order();
811 struct object_id oid
;
812 char *pack_tmp_name
= NULL
;
815 f
= hashfd_throughput(1, "<stdout>", progress_state
);
817 f
= create_tmp_packfile(&pack_tmp_name
);
819 offset
= write_pack_header(f
, nr_remaining
);
821 if (reuse_packfile
) {
823 assert(pack_to_stdout
);
825 packfile_size
= write_reused_pack(f
);
826 offset
+= packfile_size
;
830 for (; i
< to_pack
.nr_objects
; i
++) {
831 struct object_entry
*e
= write_order
[i
];
832 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
834 display_progress(progress_state
, written
);
838 * Did we write the wrong # entries in the header?
839 * If so, rewrite it like in fast-import
841 if (pack_to_stdout
) {
842 hashclose(f
, oid
.hash
, CSUM_CLOSE
);
843 } else if (nr_written
== nr_remaining
) {
844 hashclose(f
, oid
.hash
, CSUM_FSYNC
);
846 int fd
= hashclose(f
, oid
.hash
, 0);
847 fixup_pack_header_footer(fd
, oid
.hash
, pack_tmp_name
,
848 nr_written
, oid
.hash
, offset
);
850 if (write_bitmap_index
) {
851 warning(_(no_split_warning
));
852 write_bitmap_index
= 0;
856 if (!pack_to_stdout
) {
858 struct strbuf tmpname
= STRBUF_INIT
;
861 * Packs are runtime accessed in their mtime
862 * order since newer packs are more likely to contain
863 * younger objects. So if we are creating multiple
864 * packs then we should modify the mtime of later ones
865 * to preserve this property.
867 if (stat(pack_tmp_name
, &st
) < 0) {
868 warning_errno("failed to stat %s", pack_tmp_name
);
869 } else if (!last_mtime
) {
870 last_mtime
= st
.st_mtime
;
873 utb
.actime
= st
.st_atime
;
874 utb
.modtime
= --last_mtime
;
875 if (utime(pack_tmp_name
, &utb
) < 0)
876 warning_errno("failed utime() on %s", pack_tmp_name
);
879 strbuf_addf(&tmpname
, "%s-", base_name
);
881 if (write_bitmap_index
) {
882 bitmap_writer_set_checksum(oid
.hash
);
883 bitmap_writer_build_type_index(written_list
, nr_written
);
886 finish_tmp_packfile(&tmpname
, pack_tmp_name
,
887 written_list
, nr_written
,
888 &pack_idx_opts
, oid
.hash
);
890 if (write_bitmap_index
) {
891 strbuf_addf(&tmpname
, "%s.bitmap", oid_to_hex(&oid
));
893 stop_progress(&progress_state
);
895 bitmap_writer_show_progress(progress
);
896 bitmap_writer_reuse_bitmaps(&to_pack
);
897 bitmap_writer_select_commits(indexed_commits
, indexed_commits_nr
, -1);
898 bitmap_writer_build(&to_pack
);
899 bitmap_writer_finish(written_list
, nr_written
,
900 tmpname
.buf
, write_bitmap_options
);
901 write_bitmap_index
= 0;
904 strbuf_release(&tmpname
);
906 puts(oid_to_hex(&oid
));
909 /* mark written objects as written to previous pack */
910 for (j
= 0; j
< nr_written
; j
++) {
911 written_list
[j
]->offset
= (off_t
)-1;
913 nr_remaining
-= nr_written
;
914 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
918 stop_progress(&progress_state
);
919 if (written
!= nr_result
)
920 die("wrote %"PRIu32
" objects while expecting %"PRIu32
,
924 static int no_try_delta(const char *path
)
926 static struct attr_check
*check
;
929 check
= attr_check_initl("delta", NULL
);
930 if (git_check_attr(path
, check
))
932 if (ATTR_FALSE(check
->items
[0].value
))
938 * When adding an object, check whether we have already added it
939 * to our packing list. If so, we can skip. However, if we are
940 * being asked to excludei t, but the previous mention was to include
941 * it, make sure to adjust its flags and tweak our numbers accordingly.
943 * As an optimization, we pass out the index position where we would have
944 * found the item, since that saves us from having to look it up again a
945 * few lines later when we want to add the new entry.
947 static int have_duplicate_entry(const struct object_id
*oid
,
951 struct object_entry
*entry
;
953 entry
= packlist_find(&to_pack
, oid
->hash
, index_pos
);
958 if (!entry
->preferred_base
)
960 entry
->preferred_base
= 1;
966 static int want_found_object(int exclude
, struct packed_git
*p
)
974 * When asked to do --local (do not include an object that appears in a
975 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
976 * an object that appears in a pack marked with .keep), finding a pack
977 * that matches the criteria is sufficient for us to decide to omit it.
978 * However, even if this pack does not satisfy the criteria, we need to
979 * make sure no copy of this object appears in _any_ pack that makes us
980 * to omit the object, so we need to check all the packs.
982 * We can however first check whether these options can possible matter;
983 * if they do not matter we know we want the object in generated pack.
984 * Otherwise, we signal "-1" at the end to tell the caller that we do
985 * not know either way, and it needs to check more packs.
987 if (!ignore_packed_keep_on_disk
&&
988 !ignore_packed_keep_in_core
&&
989 (!local
|| !have_non_local_packs
))
992 if (local
&& !p
->pack_local
)
995 ((ignore_packed_keep_on_disk
&& p
->pack_keep
) ||
996 (ignore_packed_keep_in_core
&& p
->pack_keep_in_core
)))
999 /* we don't know yet; keep looking for more packs */
1004 * Check whether we want the object in the pack (e.g., we do not want
1005 * objects found in non-local stores if the "--local" option was used).
1007 * If the caller already knows an existing pack it wants to take the object
1008 * from, that is passed in *found_pack and *found_offset; otherwise this
1009 * function finds if there is any pack that has the object and returns the pack
1010 * and its offset in these variables.
1012 static int want_object_in_pack(const struct object_id
*oid
,
1014 struct packed_git
**found_pack
,
1015 off_t
*found_offset
)
1018 struct list_head
*pos
;
1020 if (!exclude
&& local
&& has_loose_object_nonlocal(oid
->hash
))
1024 * If we already know the pack object lives in, start checks from that
1025 * pack - in the usual case when neither --local was given nor .keep files
1026 * are present we will determine the answer right now.
1029 want
= want_found_object(exclude
, *found_pack
);
1033 list_for_each(pos
, get_packed_git_mru(the_repository
)) {
1034 struct packed_git
*p
= list_entry(pos
, struct packed_git
, mru
);
1037 if (p
== *found_pack
)
1038 offset
= *found_offset
;
1040 offset
= find_pack_entry_one(oid
->hash
, p
);
1044 if (!is_pack_valid(p
))
1046 *found_offset
= offset
;
1049 want
= want_found_object(exclude
, p
);
1050 if (!exclude
&& want
> 0)
1052 get_packed_git_mru(the_repository
));
1061 static void create_object_entry(const struct object_id
*oid
,
1062 enum object_type type
,
1067 struct packed_git
*found_pack
,
1070 struct object_entry
*entry
;
1072 entry
= packlist_alloc(&to_pack
, oid
->hash
, index_pos
);
1077 entry
->preferred_base
= 1;
1081 entry
->in_pack
= found_pack
;
1082 entry
->in_pack_offset
= found_offset
;
1085 entry
->no_try_delta
= no_try_delta
;
1088 static const char no_closure_warning
[] = N_(
1089 "disabling bitmap writing, as some objects are not being packed"
1092 static int add_object_entry(const struct object_id
*oid
, enum object_type type
,
1093 const char *name
, int exclude
)
1095 struct packed_git
*found_pack
= NULL
;
1096 off_t found_offset
= 0;
1099 display_progress(progress_state
, ++nr_seen
);
1101 if (have_duplicate_entry(oid
, exclude
, &index_pos
))
1104 if (!want_object_in_pack(oid
, exclude
, &found_pack
, &found_offset
)) {
1105 /* The pack is missing an object, so it will not have closure */
1106 if (write_bitmap_index
) {
1107 warning(_(no_closure_warning
));
1108 write_bitmap_index
= 0;
1113 create_object_entry(oid
, type
, pack_name_hash(name
),
1114 exclude
, name
&& no_try_delta(name
),
1115 index_pos
, found_pack
, found_offset
);
1119 static int add_object_entry_from_bitmap(const struct object_id
*oid
,
1120 enum object_type type
,
1121 int flags
, uint32_t name_hash
,
1122 struct packed_git
*pack
, off_t offset
)
1126 display_progress(progress_state
, ++nr_seen
);
1128 if (have_duplicate_entry(oid
, 0, &index_pos
))
1131 if (!want_object_in_pack(oid
, 0, &pack
, &offset
))
1134 create_object_entry(oid
, type
, name_hash
, 0, 0, index_pos
, pack
, offset
);
1138 struct pbase_tree_cache
{
1139 struct object_id oid
;
1143 unsigned long tree_size
;
1146 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1147 static int pbase_tree_cache_ix(const struct object_id
*oid
)
1149 return oid
->hash
[0] % ARRAY_SIZE(pbase_tree_cache
);
1151 static int pbase_tree_cache_ix_incr(int ix
)
1153 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1156 static struct pbase_tree
{
1157 struct pbase_tree
*next
;
1158 /* This is a phony "cache" entry; we are not
1159 * going to evict it or find it through _get()
1160 * mechanism -- this is for the toplevel node that
1161 * would almost always change with any commit.
1163 struct pbase_tree_cache pcache
;
1166 static struct pbase_tree_cache
*pbase_tree_get(const struct object_id
*oid
)
1168 struct pbase_tree_cache
*ent
, *nent
;
1171 enum object_type type
;
1173 int my_ix
= pbase_tree_cache_ix(oid
);
1174 int available_ix
= -1;
1176 /* pbase-tree-cache acts as a limited hashtable.
1177 * your object will be found at your index or within a few
1178 * slots after that slot if it is cached.
1180 for (neigh
= 0; neigh
< 8; neigh
++) {
1181 ent
= pbase_tree_cache
[my_ix
];
1182 if (ent
&& !oidcmp(&ent
->oid
, oid
)) {
1186 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1187 ((0 <= available_ix
) &&
1188 (!ent
&& pbase_tree_cache
[available_ix
])))
1189 available_ix
= my_ix
;
1192 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1195 /* Did not find one. Either we got a bogus request or
1196 * we need to read and perhaps cache.
1198 data
= read_object_file(oid
, &type
, &size
);
1201 if (type
!= OBJ_TREE
) {
1206 /* We need to either cache or return a throwaway copy */
1208 if (available_ix
< 0)
1211 ent
= pbase_tree_cache
[available_ix
];
1212 my_ix
= available_ix
;
1216 nent
= xmalloc(sizeof(*nent
));
1217 nent
->temporary
= (available_ix
< 0);
1220 /* evict and reuse */
1221 free(ent
->tree_data
);
1224 oidcpy(&nent
->oid
, oid
);
1225 nent
->tree_data
= data
;
1226 nent
->tree_size
= size
;
1228 if (!nent
->temporary
)
1229 pbase_tree_cache
[my_ix
] = nent
;
1233 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1235 if (!cache
->temporary
) {
1239 free(cache
->tree_data
);
1243 static int name_cmp_len(const char *name
)
1246 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
1251 static void add_pbase_object(struct tree_desc
*tree
,
1254 const char *fullname
)
1256 struct name_entry entry
;
1259 while (tree_entry(tree
,&entry
)) {
1260 if (S_ISGITLINK(entry
.mode
))
1262 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1263 memcmp(name
, entry
.path
, cmplen
);
1268 if (name
[cmplen
] != '/') {
1269 add_object_entry(entry
.oid
,
1270 object_type(entry
.mode
),
1274 if (S_ISDIR(entry
.mode
)) {
1275 struct tree_desc sub
;
1276 struct pbase_tree_cache
*tree
;
1277 const char *down
= name
+cmplen
+1;
1278 int downlen
= name_cmp_len(down
);
1280 tree
= pbase_tree_get(entry
.oid
);
1283 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1285 add_pbase_object(&sub
, down
, downlen
, fullname
);
1286 pbase_tree_put(tree
);
1291 static unsigned *done_pbase_paths
;
1292 static int done_pbase_paths_num
;
1293 static int done_pbase_paths_alloc
;
1294 static int done_pbase_path_pos(unsigned hash
)
1297 int hi
= done_pbase_paths_num
;
1299 int mi
= lo
+ (hi
- lo
) / 2;
1300 if (done_pbase_paths
[mi
] == hash
)
1302 if (done_pbase_paths
[mi
] < hash
)
1310 static int check_pbase_path(unsigned hash
)
1312 int pos
= done_pbase_path_pos(hash
);
1316 ALLOC_GROW(done_pbase_paths
,
1317 done_pbase_paths_num
+ 1,
1318 done_pbase_paths_alloc
);
1319 done_pbase_paths_num
++;
1320 if (pos
< done_pbase_paths_num
)
1321 MOVE_ARRAY(done_pbase_paths
+ pos
+ 1, done_pbase_paths
+ pos
,
1322 done_pbase_paths_num
- pos
- 1);
1323 done_pbase_paths
[pos
] = hash
;
1327 static void add_preferred_base_object(const char *name
)
1329 struct pbase_tree
*it
;
1331 unsigned hash
= pack_name_hash(name
);
1333 if (!num_preferred_base
|| check_pbase_path(hash
))
1336 cmplen
= name_cmp_len(name
);
1337 for (it
= pbase_tree
; it
; it
= it
->next
) {
1339 add_object_entry(&it
->pcache
.oid
, OBJ_TREE
, NULL
, 1);
1342 struct tree_desc tree
;
1343 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1344 add_pbase_object(&tree
, name
, cmplen
, name
);
1349 static void add_preferred_base(struct object_id
*oid
)
1351 struct pbase_tree
*it
;
1354 struct object_id tree_oid
;
1356 if (window
<= num_preferred_base
++)
1359 data
= read_object_with_reference(oid
, tree_type
, &size
, &tree_oid
);
1363 for (it
= pbase_tree
; it
; it
= it
->next
) {
1364 if (!oidcmp(&it
->pcache
.oid
, &tree_oid
)) {
1370 it
= xcalloc(1, sizeof(*it
));
1371 it
->next
= pbase_tree
;
1374 oidcpy(&it
->pcache
.oid
, &tree_oid
);
1375 it
->pcache
.tree_data
= data
;
1376 it
->pcache
.tree_size
= size
;
1379 static void cleanup_preferred_base(void)
1381 struct pbase_tree
*it
;
1387 struct pbase_tree
*tmp
= it
;
1389 free(tmp
->pcache
.tree_data
);
1393 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1394 if (!pbase_tree_cache
[i
])
1396 free(pbase_tree_cache
[i
]->tree_data
);
1397 FREE_AND_NULL(pbase_tree_cache
[i
]);
1400 FREE_AND_NULL(done_pbase_paths
);
1401 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1404 static void check_object(struct object_entry
*entry
)
1406 if (entry
->in_pack
) {
1407 struct packed_git
*p
= entry
->in_pack
;
1408 struct pack_window
*w_curs
= NULL
;
1409 const unsigned char *base_ref
= NULL
;
1410 struct object_entry
*base_entry
;
1411 unsigned long used
, used_0
;
1412 unsigned long avail
;
1414 unsigned char *buf
, c
;
1416 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1419 * We want in_pack_type even if we do not reuse delta
1420 * since non-delta representations could still be reused.
1422 used
= unpack_object_header_buffer(buf
, avail
,
1423 &entry
->in_pack_type
,
1429 * Determine if this is a delta and if so whether we can
1430 * reuse it or not. Otherwise let's find out as cheaply as
1431 * possible what the actual type and size for this object is.
1433 switch (entry
->in_pack_type
) {
1435 /* Not a delta hence we've already got all we need. */
1436 entry
->type
= entry
->in_pack_type
;
1437 entry
->in_pack_header_size
= used
;
1438 if (entry
->type
< OBJ_COMMIT
|| entry
->type
> OBJ_BLOB
)
1440 unuse_pack(&w_curs
);
1443 if (reuse_delta
&& !entry
->preferred_base
)
1444 base_ref
= use_pack(p
, &w_curs
,
1445 entry
->in_pack_offset
+ used
, NULL
);
1446 entry
->in_pack_header_size
= used
+ 20;
1449 buf
= use_pack(p
, &w_curs
,
1450 entry
->in_pack_offset
+ used
, NULL
);
1456 if (!ofs
|| MSB(ofs
, 7)) {
1457 error("delta base offset overflow in pack for %s",
1458 oid_to_hex(&entry
->idx
.oid
));
1462 ofs
= (ofs
<< 7) + (c
& 127);
1464 ofs
= entry
->in_pack_offset
- ofs
;
1465 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
1466 error("delta base offset out of bound for %s",
1467 oid_to_hex(&entry
->idx
.oid
));
1470 if (reuse_delta
&& !entry
->preferred_base
) {
1471 struct revindex_entry
*revidx
;
1472 revidx
= find_pack_revindex(p
, ofs
);
1475 base_ref
= nth_packed_object_sha1(p
, revidx
->nr
);
1477 entry
->in_pack_header_size
= used
+ used_0
;
1481 if (base_ref
&& (base_entry
= packlist_find(&to_pack
, base_ref
, NULL
))) {
1483 * If base_ref was set above that means we wish to
1484 * reuse delta data, and we even found that base
1485 * in the list of objects we want to pack. Goodie!
1487 * Depth value does not matter - find_deltas() will
1488 * never consider reused delta as the base object to
1489 * deltify other objects against, in order to avoid
1492 entry
->type
= entry
->in_pack_type
;
1493 entry
->delta
= base_entry
;
1494 entry
->delta_size
= entry
->size
;
1495 entry
->delta_sibling
= base_entry
->delta_child
;
1496 base_entry
->delta_child
= entry
;
1497 unuse_pack(&w_curs
);
1503 * This must be a delta and we already know what the
1504 * final object type is. Let's extract the actual
1505 * object size from the delta header.
1507 entry
->size
= get_size_from_delta(p
, &w_curs
,
1508 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1509 if (entry
->size
== 0)
1511 unuse_pack(&w_curs
);
1516 * No choice but to fall back to the recursive delta walk
1517 * with sha1_object_info() to find about the object type
1521 unuse_pack(&w_curs
);
1524 entry
->type
= oid_object_info(&entry
->idx
.oid
, &entry
->size
);
1526 * The error condition is checked in prepare_pack(). This is
1527 * to permit a missing preferred base object to be ignored
1528 * as a preferred base. Doing so can result in a larger
1529 * pack file, but the transfer will still take place.
1533 static int pack_offset_sort(const void *_a
, const void *_b
)
1535 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1536 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1538 /* avoid filesystem trashing with loose objects */
1539 if (!a
->in_pack
&& !b
->in_pack
)
1540 return oidcmp(&a
->idx
.oid
, &b
->idx
.oid
);
1542 if (a
->in_pack
< b
->in_pack
)
1544 if (a
->in_pack
> b
->in_pack
)
1546 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1547 (a
->in_pack_offset
> b
->in_pack_offset
);
1551 * Drop an on-disk delta we were planning to reuse. Naively, this would
1552 * just involve blanking out the "delta" field, but we have to deal
1553 * with some extra book-keeping:
1555 * 1. Removing ourselves from the delta_sibling linked list.
1557 * 2. Updating our size/type to the non-delta representation. These were
1558 * either not recorded initially (size) or overwritten with the delta type
1559 * (type) when check_object() decided to reuse the delta.
1561 * 3. Resetting our delta depth, as we are now a base object.
1563 static void drop_reused_delta(struct object_entry
*entry
)
1565 struct object_entry
**p
= &entry
->delta
->delta_child
;
1566 struct object_info oi
= OBJECT_INFO_INIT
;
1570 *p
= (*p
)->delta_sibling
;
1572 p
= &(*p
)->delta_sibling
;
1574 entry
->delta
= NULL
;
1577 oi
.sizep
= &entry
->size
;
1578 oi
.typep
= &entry
->type
;
1579 if (packed_object_info(entry
->in_pack
, entry
->in_pack_offset
, &oi
) < 0) {
1581 * We failed to get the info from this pack for some reason;
1582 * fall back to sha1_object_info, which may find another copy.
1583 * And if that fails, the error will be recorded in entry->type
1584 * and dealt with in prepare_pack().
1586 entry
->type
= oid_object_info(&entry
->idx
.oid
, &entry
->size
);
1591 * Follow the chain of deltas from this entry onward, throwing away any links
1592 * that cause us to hit a cycle (as determined by the DFS state flags in
1595 * We also detect too-long reused chains that would violate our --depth
1598 static void break_delta_chains(struct object_entry
*entry
)
1601 * The actual depth of each object we will write is stored as an int,
1602 * as it cannot exceed our int "depth" limit. But before we break
1603 * changes based no that limit, we may potentially go as deep as the
1604 * number of objects, which is elsewhere bounded to a uint32_t.
1606 uint32_t total_depth
;
1607 struct object_entry
*cur
, *next
;
1609 for (cur
= entry
, total_depth
= 0;
1611 cur
= cur
->delta
, total_depth
++) {
1612 if (cur
->dfs_state
== DFS_DONE
) {
1614 * We've already seen this object and know it isn't
1615 * part of a cycle. We do need to append its depth
1618 total_depth
+= cur
->depth
;
1623 * We break cycles before looping, so an ACTIVE state (or any
1624 * other cruft which made its way into the state variable)
1627 if (cur
->dfs_state
!= DFS_NONE
)
1628 die("BUG: confusing delta dfs state in first pass: %d",
1632 * Now we know this is the first time we've seen the object. If
1633 * it's not a delta, we're done traversing, but we'll mark it
1634 * done to save time on future traversals.
1637 cur
->dfs_state
= DFS_DONE
;
1642 * Mark ourselves as active and see if the next step causes
1643 * us to cycle to another active object. It's important to do
1644 * this _before_ we loop, because it impacts where we make the
1645 * cut, and thus how our total_depth counter works.
1646 * E.g., We may see a partial loop like:
1648 * A -> B -> C -> D -> B
1650 * Cutting B->C breaks the cycle. But now the depth of A is
1651 * only 1, and our total_depth counter is at 3. The size of the
1652 * error is always one less than the size of the cycle we
1653 * broke. Commits C and D were "lost" from A's chain.
1655 * If we instead cut D->B, then the depth of A is correct at 3.
1656 * We keep all commits in the chain that we examined.
1658 cur
->dfs_state
= DFS_ACTIVE
;
1659 if (cur
->delta
->dfs_state
== DFS_ACTIVE
) {
1660 drop_reused_delta(cur
);
1661 cur
->dfs_state
= DFS_DONE
;
1667 * And now that we've gone all the way to the bottom of the chain, we
1668 * need to clear the active flags and set the depth fields as
1669 * appropriate. Unlike the loop above, which can quit when it drops a
1670 * delta, we need to keep going to look for more depth cuts. So we need
1671 * an extra "next" pointer to keep going after we reset cur->delta.
1673 for (cur
= entry
; cur
; cur
= next
) {
1677 * We should have a chain of zero or more ACTIVE states down to
1678 * a final DONE. We can quit after the DONE, because either it
1679 * has no bases, or we've already handled them in a previous
1682 if (cur
->dfs_state
== DFS_DONE
)
1684 else if (cur
->dfs_state
!= DFS_ACTIVE
)
1685 die("BUG: confusing delta dfs state in second pass: %d",
1689 * If the total_depth is more than depth, then we need to snip
1690 * the chain into two or more smaller chains that don't exceed
1691 * the maximum depth. Most of the resulting chains will contain
1692 * (depth + 1) entries (i.e., depth deltas plus one base), and
1693 * the last chain (i.e., the one containing entry) will contain
1694 * whatever entries are left over, namely
1695 * (total_depth % (depth + 1)) of them.
1697 * Since we are iterating towards decreasing depth, we need to
1698 * decrement total_depth as we go, and we need to write to the
1699 * entry what its final depth will be after all of the
1700 * snipping. Since we're snipping into chains of length (depth
1701 * + 1) entries, the final depth of an entry will be its
1702 * original depth modulo (depth + 1). Any time we encounter an
1703 * entry whose final depth is supposed to be zero, we snip it
1704 * from its delta base, thereby making it so.
1706 cur
->depth
= (total_depth
--) % (depth
+ 1);
1708 drop_reused_delta(cur
);
1710 cur
->dfs_state
= DFS_DONE
;
1714 static void get_object_details(void)
1717 struct object_entry
**sorted_by_offset
;
1720 progress_state
= start_progress(_("Counting objects"),
1721 to_pack
.nr_objects
);
1723 sorted_by_offset
= xcalloc(to_pack
.nr_objects
, sizeof(struct object_entry
*));
1724 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
1725 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
1726 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
1728 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
1729 struct object_entry
*entry
= sorted_by_offset
[i
];
1730 check_object(entry
);
1731 if (big_file_threshold
< entry
->size
)
1732 entry
->no_try_delta
= 1;
1733 display_progress(progress_state
, i
+ 1);
1735 stop_progress(&progress_state
);
1738 * This must happen in a second pass, since we rely on the delta
1739 * information for the whole list being completed.
1741 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
1742 break_delta_chains(&to_pack
.objects
[i
]);
1744 free(sorted_by_offset
);
1748 * We search for deltas in a list sorted by type, by filename hash, and then
1749 * by size, so that we see progressively smaller and smaller files.
1750 * That's because we prefer deltas to be from the bigger file
1751 * to the smaller -- deletes are potentially cheaper, but perhaps
1752 * more importantly, the bigger file is likely the more recent
1753 * one. The deepest deltas are therefore the oldest objects which are
1754 * less susceptible to be accessed often.
1756 static int type_size_sort(const void *_a
, const void *_b
)
1758 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1759 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1761 if (a
->type
> b
->type
)
1763 if (a
->type
< b
->type
)
1765 if (a
->hash
> b
->hash
)
1767 if (a
->hash
< b
->hash
)
1769 if (a
->preferred_base
> b
->preferred_base
)
1771 if (a
->preferred_base
< b
->preferred_base
)
1773 if (a
->size
> b
->size
)
1775 if (a
->size
< b
->size
)
1777 return a
< b
? -1 : (a
> b
); /* newest first */
1781 struct object_entry
*entry
;
1783 struct delta_index
*index
;
1787 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
1788 unsigned long delta_size
)
1790 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
1793 if (delta_size
< cache_max_small_delta_size
)
1796 /* cache delta, if objects are large enough compared to delta size */
1797 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
1805 static pthread_mutex_t read_mutex
;
1806 #define read_lock() pthread_mutex_lock(&read_mutex)
1807 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1809 static pthread_mutex_t cache_mutex
;
1810 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1811 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1813 static pthread_mutex_t progress_mutex
;
1814 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1815 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1819 #define read_lock() (void)0
1820 #define read_unlock() (void)0
1821 #define cache_lock() (void)0
1822 #define cache_unlock() (void)0
1823 #define progress_lock() (void)0
1824 #define progress_unlock() (void)0
1828 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1829 unsigned max_depth
, unsigned long *mem_usage
)
1831 struct object_entry
*trg_entry
= trg
->entry
;
1832 struct object_entry
*src_entry
= src
->entry
;
1833 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1835 enum object_type type
;
1838 /* Don't bother doing diffs between different types */
1839 if (trg_entry
->type
!= src_entry
->type
)
1843 * We do not bother to try a delta that we discarded on an
1844 * earlier try, but only when reusing delta data. Note that
1845 * src_entry that is marked as the preferred_base should always
1846 * be considered, as even if we produce a suboptimal delta against
1847 * it, we will still save the transfer cost, as we already know
1848 * the other side has it and we won't send src_entry at all.
1850 if (reuse_delta
&& trg_entry
->in_pack
&&
1851 trg_entry
->in_pack
== src_entry
->in_pack
&&
1852 !src_entry
->preferred_base
&&
1853 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1854 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1857 /* Let's not bust the allowed depth. */
1858 if (src
->depth
>= max_depth
)
1861 /* Now some size filtering heuristics. */
1862 trg_size
= trg_entry
->size
;
1863 if (!trg_entry
->delta
) {
1864 max_size
= trg_size
/2 - 20;
1867 max_size
= trg_entry
->delta_size
;
1868 ref_depth
= trg
->depth
;
1870 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
1871 (max_depth
- ref_depth
+ 1);
1874 src_size
= src_entry
->size
;
1875 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1876 if (sizediff
>= max_size
)
1878 if (trg_size
< src_size
/ 32)
1881 /* Load data if not already done */
1884 trg
->data
= read_object_file(&trg_entry
->idx
.oid
, &type
, &sz
);
1887 die("object %s cannot be read",
1888 oid_to_hex(&trg_entry
->idx
.oid
));
1890 die("object %s inconsistent object length (%lu vs %lu)",
1891 oid_to_hex(&trg_entry
->idx
.oid
), sz
,
1897 src
->data
= read_object_file(&src_entry
->idx
.oid
, &type
, &sz
);
1900 if (src_entry
->preferred_base
) {
1901 static int warned
= 0;
1903 warning("object %s cannot be read",
1904 oid_to_hex(&src_entry
->idx
.oid
));
1906 * Those objects are not included in the
1907 * resulting pack. Be resilient and ignore
1908 * them if they can't be read, in case the
1909 * pack could be created nevertheless.
1913 die("object %s cannot be read",
1914 oid_to_hex(&src_entry
->idx
.oid
));
1917 die("object %s inconsistent object length (%lu vs %lu)",
1918 oid_to_hex(&src_entry
->idx
.oid
), sz
,
1923 src
->index
= create_delta_index(src
->data
, src_size
);
1925 static int warned
= 0;
1927 warning("suboptimal pack - out of memory");
1930 *mem_usage
+= sizeof_delta_index(src
->index
);
1933 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1937 if (trg_entry
->delta
) {
1938 /* Prefer only shallower same-sized deltas. */
1939 if (delta_size
== trg_entry
->delta_size
&&
1940 src
->depth
+ 1 >= trg
->depth
) {
1947 * Handle memory allocation outside of the cache
1948 * accounting lock. Compiler will optimize the strangeness
1949 * away when NO_PTHREADS is defined.
1951 free(trg_entry
->delta_data
);
1953 if (trg_entry
->delta_data
) {
1954 delta_cache_size
-= trg_entry
->delta_size
;
1955 trg_entry
->delta_data
= NULL
;
1957 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
1958 delta_cache_size
+= delta_size
;
1960 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
1966 trg_entry
->delta
= src_entry
;
1967 trg_entry
->delta_size
= delta_size
;
1968 trg
->depth
= src
->depth
+ 1;
1973 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1975 struct object_entry
*child
= me
->delta_child
;
1978 unsigned int c
= check_delta_limit(child
, n
+ 1);
1981 child
= child
->delta_sibling
;
1986 static unsigned long free_unpacked(struct unpacked
*n
)
1988 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
1989 free_delta_index(n
->index
);
1992 freed_mem
+= n
->entry
->size
;
1993 FREE_AND_NULL(n
->data
);
2000 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
2001 int window
, int depth
, unsigned *processed
)
2003 uint32_t i
, idx
= 0, count
= 0;
2004 struct unpacked
*array
;
2005 unsigned long mem_usage
= 0;
2007 array
= xcalloc(window
, sizeof(struct unpacked
));
2010 struct object_entry
*entry
;
2011 struct unpacked
*n
= array
+ idx
;
2012 int j
, max_depth
, best_base
= -1;
2021 if (!entry
->preferred_base
) {
2023 display_progress(progress_state
, *processed
);
2027 mem_usage
-= free_unpacked(n
);
2030 while (window_memory_limit
&&
2031 mem_usage
> window_memory_limit
&&
2033 uint32_t tail
= (idx
+ window
- count
) % window
;
2034 mem_usage
-= free_unpacked(array
+ tail
);
2038 /* We do not compute delta to *create* objects we are not
2041 if (entry
->preferred_base
)
2045 * If the current object is at pack edge, take the depth the
2046 * objects that depend on the current object into account
2047 * otherwise they would become too deep.
2050 if (entry
->delta_child
) {
2051 max_depth
-= check_delta_limit(entry
, 0);
2059 uint32_t other_idx
= idx
+ j
;
2061 if (other_idx
>= window
)
2062 other_idx
-= window
;
2063 m
= array
+ other_idx
;
2066 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2070 best_base
= other_idx
;
2074 * If we decided to cache the delta data, then it is best
2075 * to compress it right away. First because we have to do
2076 * it anyway, and doing it here while we're threaded will
2077 * save a lot of time in the non threaded write phase,
2078 * as well as allow for caching more deltas within
2079 * the same cache size limit.
2081 * But only if not writing to stdout, since in that case
2082 * the network is most likely throttling writes anyway,
2083 * and therefore it is best to go to the write phase ASAP
2084 * instead, as we can afford spending more time compressing
2085 * between writes at that moment.
2087 if (entry
->delta_data
&& !pack_to_stdout
) {
2088 entry
->z_delta_size
= do_compress(&entry
->delta_data
,
2091 delta_cache_size
-= entry
->delta_size
;
2092 delta_cache_size
+= entry
->z_delta_size
;
2096 /* if we made n a delta, and if n is already at max
2097 * depth, leaving it in the window is pointless. we
2098 * should evict it first.
2100 if (entry
->delta
&& max_depth
<= n
->depth
)
2104 * Move the best delta base up in the window, after the
2105 * currently deltified object, to keep it longer. It will
2106 * be the first base object to be attempted next.
2109 struct unpacked swap
= array
[best_base
];
2110 int dist
= (window
+ idx
- best_base
) % window
;
2111 int dst
= best_base
;
2113 int src
= (dst
+ 1) % window
;
2114 array
[dst
] = array
[src
];
2122 if (count
+ 1 < window
)
2128 for (i
= 0; i
< window
; ++i
) {
2129 free_delta_index(array
[i
].index
);
2130 free(array
[i
].data
);
2137 static void try_to_free_from_threads(size_t size
)
2140 release_pack_memory(size
);
2144 static try_to_free_t old_try_to_free_routine
;
2147 * The main thread waits on the condition that (at least) one of the workers
2148 * has stopped working (which is indicated in the .working member of
2149 * struct thread_params).
2150 * When a work thread has completed its work, it sets .working to 0 and
2151 * signals the main thread and waits on the condition that .data_ready
2155 struct thread_params
{
2157 struct object_entry
**list
;
2164 pthread_mutex_t mutex
;
2165 pthread_cond_t cond
;
2166 unsigned *processed
;
2169 static pthread_cond_t progress_cond
;
2172 * Mutex and conditional variable can't be statically-initialized on Windows.
2174 static void init_threaded_search(void)
2176 init_recursive_mutex(&read_mutex
);
2177 pthread_mutex_init(&cache_mutex
, NULL
);
2178 pthread_mutex_init(&progress_mutex
, NULL
);
2179 pthread_cond_init(&progress_cond
, NULL
);
2180 old_try_to_free_routine
= set_try_to_free_routine(try_to_free_from_threads
);
2183 static void cleanup_threaded_search(void)
2185 set_try_to_free_routine(old_try_to_free_routine
);
2186 pthread_cond_destroy(&progress_cond
);
2187 pthread_mutex_destroy(&read_mutex
);
2188 pthread_mutex_destroy(&cache_mutex
);
2189 pthread_mutex_destroy(&progress_mutex
);
2192 static void *threaded_find_deltas(void *arg
)
2194 struct thread_params
*me
= arg
;
2197 while (me
->remaining
) {
2200 find_deltas(me
->list
, &me
->remaining
,
2201 me
->window
, me
->depth
, me
->processed
);
2205 pthread_cond_signal(&progress_cond
);
2209 * We must not set ->data_ready before we wait on the
2210 * condition because the main thread may have set it to 1
2211 * before we get here. In order to be sure that new
2212 * work is available if we see 1 in ->data_ready, it
2213 * was initialized to 0 before this thread was spawned
2214 * and we reset it to 0 right away.
2216 pthread_mutex_lock(&me
->mutex
);
2217 while (!me
->data_ready
)
2218 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2220 pthread_mutex_unlock(&me
->mutex
);
2225 /* leave ->working 1 so that this doesn't get more work assigned */
2229 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2230 int window
, int depth
, unsigned *processed
)
2232 struct thread_params
*p
;
2233 int i
, ret
, active_threads
= 0;
2235 init_threaded_search();
2237 if (delta_search_threads
<= 1) {
2238 find_deltas(list
, &list_size
, window
, depth
, processed
);
2239 cleanup_threaded_search();
2242 if (progress
> pack_to_stdout
)
2243 fprintf(stderr
, "Delta compression using up to %d threads.\n",
2244 delta_search_threads
);
2245 p
= xcalloc(delta_search_threads
, sizeof(*p
));
2247 /* Partition the work amongst work threads. */
2248 for (i
= 0; i
< delta_search_threads
; i
++) {
2249 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
2251 /* don't use too small segments or no deltas will be found */
2252 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
2255 p
[i
].window
= window
;
2257 p
[i
].processed
= processed
;
2259 p
[i
].data_ready
= 0;
2261 /* try to split chunks on "path" boundaries */
2262 while (sub_size
&& sub_size
< list_size
&&
2263 list
[sub_size
]->hash
&&
2264 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
2268 p
[i
].list_size
= sub_size
;
2269 p
[i
].remaining
= sub_size
;
2272 list_size
-= sub_size
;
2275 /* Start work threads. */
2276 for (i
= 0; i
< delta_search_threads
; i
++) {
2277 if (!p
[i
].list_size
)
2279 pthread_mutex_init(&p
[i
].mutex
, NULL
);
2280 pthread_cond_init(&p
[i
].cond
, NULL
);
2281 ret
= pthread_create(&p
[i
].thread
, NULL
,
2282 threaded_find_deltas
, &p
[i
]);
2284 die("unable to create thread: %s", strerror(ret
));
2289 * Now let's wait for work completion. Each time a thread is done
2290 * with its work, we steal half of the remaining work from the
2291 * thread with the largest number of unprocessed objects and give
2292 * it to that newly idle thread. This ensure good load balancing
2293 * until the remaining object list segments are simply too short
2294 * to be worth splitting anymore.
2296 while (active_threads
) {
2297 struct thread_params
*target
= NULL
;
2298 struct thread_params
*victim
= NULL
;
2299 unsigned sub_size
= 0;
2303 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
2308 pthread_cond_wait(&progress_cond
, &progress_mutex
);
2311 for (i
= 0; i
< delta_search_threads
; i
++)
2312 if (p
[i
].remaining
> 2*window
&&
2313 (!victim
|| victim
->remaining
< p
[i
].remaining
))
2316 sub_size
= victim
->remaining
/ 2;
2317 list
= victim
->list
+ victim
->list_size
- sub_size
;
2318 while (sub_size
&& list
[0]->hash
&&
2319 list
[0]->hash
== list
[-1]->hash
) {
2325 * It is possible for some "paths" to have
2326 * so many objects that no hash boundary
2327 * might be found. Let's just steal the
2328 * exact half in that case.
2330 sub_size
= victim
->remaining
/ 2;
2333 target
->list
= list
;
2334 victim
->list_size
-= sub_size
;
2335 victim
->remaining
-= sub_size
;
2337 target
->list_size
= sub_size
;
2338 target
->remaining
= sub_size
;
2339 target
->working
= 1;
2342 pthread_mutex_lock(&target
->mutex
);
2343 target
->data_ready
= 1;
2344 pthread_cond_signal(&target
->cond
);
2345 pthread_mutex_unlock(&target
->mutex
);
2348 pthread_join(target
->thread
, NULL
);
2349 pthread_cond_destroy(&target
->cond
);
2350 pthread_mutex_destroy(&target
->mutex
);
2354 cleanup_threaded_search();
2359 #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
2362 static void add_tag_chain(const struct object_id
*oid
)
2367 * We catch duplicates already in add_object_entry(), but we'd
2368 * prefer to do this extra check to avoid having to parse the
2369 * tag at all if we already know that it's being packed (e.g., if
2370 * it was included via bitmaps, we would not have parsed it
2373 if (packlist_find(&to_pack
, oid
->hash
, NULL
))
2376 tag
= lookup_tag(oid
);
2378 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
2379 die("unable to pack objects reachable from tag %s",
2382 add_object_entry(&tag
->object
.oid
, OBJ_TAG
, NULL
, 0);
2384 if (tag
->tagged
->type
!= OBJ_TAG
)
2387 tag
= (struct tag
*)tag
->tagged
;
2391 static int add_ref_tag(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
2393 struct object_id peeled
;
2395 if (starts_with(path
, "refs/tags/") && /* is a tag? */
2396 !peel_ref(path
, &peeled
) && /* peelable? */
2397 packlist_find(&to_pack
, peeled
.hash
, NULL
)) /* object packed? */
2402 static void prepare_pack(int window
, int depth
)
2404 struct object_entry
**delta_list
;
2405 uint32_t i
, nr_deltas
;
2408 get_object_details();
2411 * If we're locally repacking then we need to be doubly careful
2412 * from now on in order to make sure no stealth corruption gets
2413 * propagated to the new pack. Clients receiving streamed packs
2414 * should validate everything they get anyway so no need to incur
2415 * the additional cost here in that case.
2417 if (!pack_to_stdout
)
2418 do_check_packed_object_crc
= 1;
2420 if (!to_pack
.nr_objects
|| !window
|| !depth
)
2423 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
2426 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2427 struct object_entry
*entry
= to_pack
.objects
+ i
;
2430 /* This happens if we decided to reuse existing
2431 * delta from a pack. "reuse_delta &&" is implied.
2435 if (entry
->size
< 50)
2438 if (entry
->no_try_delta
)
2441 if (!entry
->preferred_base
) {
2443 if (entry
->type
< 0)
2444 die("unable to get type of object %s",
2445 oid_to_hex(&entry
->idx
.oid
));
2447 if (entry
->type
< 0) {
2449 * This object is not found, but we
2450 * don't have to include it anyway.
2456 delta_list
[n
++] = entry
;
2459 if (nr_deltas
&& n
> 1) {
2460 unsigned nr_done
= 0;
2462 progress_state
= start_progress(_("Compressing objects"),
2464 QSORT(delta_list
, n
, type_size_sort
);
2465 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
2466 stop_progress(&progress_state
);
2467 if (nr_done
!= nr_deltas
)
2468 die("inconsistency with delta count");
2473 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2475 if (!strcmp(k
, "pack.window")) {
2476 window
= git_config_int(k
, v
);
2479 if (!strcmp(k
, "pack.windowmemory")) {
2480 window_memory_limit
= git_config_ulong(k
, v
);
2483 if (!strcmp(k
, "pack.depth")) {
2484 depth
= git_config_int(k
, v
);
2487 if (!strcmp(k
, "pack.deltacachesize")) {
2488 max_delta_cache_size
= git_config_int(k
, v
);
2491 if (!strcmp(k
, "pack.deltacachelimit")) {
2492 cache_max_small_delta_size
= git_config_int(k
, v
);
2495 if (!strcmp(k
, "pack.writebitmaphashcache")) {
2496 if (git_config_bool(k
, v
))
2497 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
2499 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
2501 if (!strcmp(k
, "pack.usebitmaps")) {
2502 use_bitmap_index_default
= git_config_bool(k
, v
);
2505 if (!strcmp(k
, "pack.threads")) {
2506 delta_search_threads
= git_config_int(k
, v
);
2507 if (delta_search_threads
< 0)
2508 die("invalid number of threads specified (%d)",
2509 delta_search_threads
);
2511 if (delta_search_threads
!= 1) {
2512 warning("no threads support, ignoring %s", k
);
2513 delta_search_threads
= 0;
2518 if (!strcmp(k
, "pack.indexversion")) {
2519 pack_idx_opts
.version
= git_config_int(k
, v
);
2520 if (pack_idx_opts
.version
> 2)
2521 die("bad pack.indexversion=%"PRIu32
,
2522 pack_idx_opts
.version
);
2525 return git_default_config(k
, v
, cb
);
2528 static void read_object_list_from_stdin(void)
2530 char line
[GIT_MAX_HEXSZ
+ 1 + PATH_MAX
+ 2];
2531 struct object_id oid
;
2535 if (!fgets(line
, sizeof(line
), stdin
)) {
2539 die("fgets returned NULL, not EOF, not error!");
2545 if (line
[0] == '-') {
2546 if (get_oid_hex(line
+1, &oid
))
2547 die("expected edge object ID, got garbage:\n %s",
2549 add_preferred_base(&oid
);
2552 if (parse_oid_hex(line
, &oid
, &p
))
2553 die("expected object ID, got garbage:\n %s", line
);
2555 add_preferred_base_object(p
+ 1);
2556 add_object_entry(&oid
, 0, p
+ 1, 0);
2560 /* Remember to update object flag allocation in object.h */
2561 #define OBJECT_ADDED (1u<<20)
2563 static void show_commit(struct commit
*commit
, void *data
)
2565 add_object_entry(&commit
->object
.oid
, OBJ_COMMIT
, NULL
, 0);
2566 commit
->object
.flags
|= OBJECT_ADDED
;
2568 if (write_bitmap_index
)
2569 index_commit_for_bitmap(commit
);
2572 static void show_object(struct object
*obj
, const char *name
, void *data
)
2574 add_preferred_base_object(name
);
2575 add_object_entry(&obj
->oid
, obj
->type
, name
, 0);
2576 obj
->flags
|= OBJECT_ADDED
;
2579 static void show_object__ma_allow_any(struct object
*obj
, const char *name
, void *data
)
2581 assert(arg_missing_action
== MA_ALLOW_ANY
);
2584 * Quietly ignore ALL missing objects. This avoids problems with
2585 * staging them now and getting an odd error later.
2587 if (!has_object_file(&obj
->oid
))
2590 show_object(obj
, name
, data
);
2593 static void show_object__ma_allow_promisor(struct object
*obj
, const char *name
, void *data
)
2595 assert(arg_missing_action
== MA_ALLOW_PROMISOR
);
2598 * Quietly ignore EXPECTED missing objects. This avoids problems with
2599 * staging them now and getting an odd error later.
2601 if (!has_object_file(&obj
->oid
) && is_promisor_object(&obj
->oid
))
2604 show_object(obj
, name
, data
);
2607 static int option_parse_missing_action(const struct option
*opt
,
2608 const char *arg
, int unset
)
2613 if (!strcmp(arg
, "error")) {
2614 arg_missing_action
= MA_ERROR
;
2615 fn_show_object
= show_object
;
2619 if (!strcmp(arg
, "allow-any")) {
2620 arg_missing_action
= MA_ALLOW_ANY
;
2621 fetch_if_missing
= 0;
2622 fn_show_object
= show_object__ma_allow_any
;
2626 if (!strcmp(arg
, "allow-promisor")) {
2627 arg_missing_action
= MA_ALLOW_PROMISOR
;
2628 fetch_if_missing
= 0;
2629 fn_show_object
= show_object__ma_allow_promisor
;
2633 die(_("invalid value for --missing"));
2637 static void show_edge(struct commit
*commit
)
2639 add_preferred_base(&commit
->object
.oid
);
2642 struct in_pack_object
{
2644 struct object
*object
;
2650 struct in_pack_object
*array
;
2653 static void mark_in_pack_object(struct object
*object
, struct packed_git
*p
, struct in_pack
*in_pack
)
2655 in_pack
->array
[in_pack
->nr
].offset
= find_pack_entry_one(object
->oid
.hash
, p
);
2656 in_pack
->array
[in_pack
->nr
].object
= object
;
2661 * Compare the objects in the offset order, in order to emulate the
2662 * "git rev-list --objects" output that produced the pack originally.
2664 static int ofscmp(const void *a_
, const void *b_
)
2666 struct in_pack_object
*a
= (struct in_pack_object
*)a_
;
2667 struct in_pack_object
*b
= (struct in_pack_object
*)b_
;
2669 if (a
->offset
< b
->offset
)
2671 else if (a
->offset
> b
->offset
)
2674 return oidcmp(&a
->object
->oid
, &b
->object
->oid
);
2677 static void add_objects_in_unpacked_packs(struct rev_info
*revs
)
2679 struct packed_git
*p
;
2680 struct in_pack in_pack
;
2683 memset(&in_pack
, 0, sizeof(in_pack
));
2685 for (p
= get_packed_git(the_repository
); p
; p
= p
->next
) {
2686 struct object_id oid
;
2689 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
2691 if (open_pack_index(p
))
2692 die("cannot open pack index");
2694 ALLOC_GROW(in_pack
.array
,
2695 in_pack
.nr
+ p
->num_objects
,
2698 for (i
= 0; i
< p
->num_objects
; i
++) {
2699 nth_packed_object_oid(&oid
, p
, i
);
2700 o
= lookup_unknown_object(oid
.hash
);
2701 if (!(o
->flags
& OBJECT_ADDED
))
2702 mark_in_pack_object(o
, p
, &in_pack
);
2703 o
->flags
|= OBJECT_ADDED
;
2708 QSORT(in_pack
.array
, in_pack
.nr
, ofscmp
);
2709 for (i
= 0; i
< in_pack
.nr
; i
++) {
2710 struct object
*o
= in_pack
.array
[i
].object
;
2711 add_object_entry(&o
->oid
, o
->type
, "", 0);
2714 free(in_pack
.array
);
2717 static int add_loose_object(const struct object_id
*oid
, const char *path
,
2720 enum object_type type
= oid_object_info(oid
, NULL
);
2723 warning("loose object at %s could not be examined", path
);
2727 add_object_entry(oid
, type
, "", 0);
2732 * We actually don't even have to worry about reachability here.
2733 * add_object_entry will weed out duplicates, so we just add every
2734 * loose object we find.
2736 static void add_unreachable_loose_objects(void)
2738 for_each_loose_file_in_objdir(get_object_directory(),
2743 static int has_sha1_pack_kept_or_nonlocal(const struct object_id
*oid
)
2745 static struct packed_git
*last_found
= (void *)1;
2746 struct packed_git
*p
;
2748 p
= (last_found
!= (void *)1) ? last_found
:
2749 get_packed_git(the_repository
);
2752 if ((!p
->pack_local
|| p
->pack_keep
||
2753 p
->pack_keep_in_core
) &&
2754 find_pack_entry_one(oid
->hash
, p
)) {
2758 if (p
== last_found
)
2759 p
= get_packed_git(the_repository
);
2762 if (p
== last_found
)
2769 * Store a list of sha1s that are should not be discarded
2770 * because they are either written too recently, or are
2771 * reachable from another object that was.
2773 * This is filled by get_object_list.
2775 static struct oid_array recent_objects
;
2777 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
2780 if (!unpack_unreachable_expiration
)
2782 if (mtime
> unpack_unreachable_expiration
)
2784 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
2789 static void loosen_unused_packed_objects(struct rev_info
*revs
)
2791 struct packed_git
*p
;
2793 struct object_id oid
;
2795 for (p
= get_packed_git(the_repository
); p
; p
= p
->next
) {
2796 if (!p
->pack_local
|| p
->pack_keep
|| p
->pack_keep_in_core
)
2799 if (open_pack_index(p
))
2800 die("cannot open pack index");
2802 for (i
= 0; i
< p
->num_objects
; i
++) {
2803 nth_packed_object_oid(&oid
, p
, i
);
2804 if (!packlist_find(&to_pack
, oid
.hash
, NULL
) &&
2805 !has_sha1_pack_kept_or_nonlocal(&oid
) &&
2806 !loosened_object_can_be_discarded(&oid
, p
->mtime
))
2807 if (force_object_loose(&oid
, p
->mtime
))
2808 die("unable to force loose object");
2814 * This tracks any options which pack-reuse code expects to be on, or which a
2815 * reader of the pack might not understand, and which would therefore prevent
2816 * blind reuse of what we have on disk.
2818 static int pack_options_allow_reuse(void)
2820 return pack_to_stdout
&&
2822 !ignore_packed_keep_on_disk
&&
2823 !ignore_packed_keep_in_core
&&
2824 (!local
|| !have_non_local_packs
) &&
2828 static int get_object_list_from_bitmap(struct rev_info
*revs
)
2830 if (prepare_bitmap_walk(revs
) < 0)
2833 if (pack_options_allow_reuse() &&
2834 !reuse_partial_packfile_from_bitmap(
2836 &reuse_packfile_objects
,
2837 &reuse_packfile_offset
)) {
2838 assert(reuse_packfile_objects
);
2839 nr_result
+= reuse_packfile_objects
;
2840 display_progress(progress_state
, nr_result
);
2843 traverse_bitmap_commit_list(&add_object_entry_from_bitmap
);
2847 static void record_recent_object(struct object
*obj
,
2851 oid_array_append(&recent_objects
, &obj
->oid
);
2854 static void record_recent_commit(struct commit
*commit
, void *data
)
2856 oid_array_append(&recent_objects
, &commit
->object
.oid
);
2859 static void get_object_list(int ac
, const char **av
)
2861 struct rev_info revs
;
2865 init_revisions(&revs
, NULL
);
2866 save_commit_buffer
= 0;
2867 setup_revisions(ac
, av
, &revs
, NULL
);
2869 /* make sure shallows are read */
2870 is_repository_shallow();
2872 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
2873 int len
= strlen(line
);
2874 if (len
&& line
[len
- 1] == '\n')
2879 if (!strcmp(line
, "--not")) {
2880 flags
^= UNINTERESTING
;
2881 write_bitmap_index
= 0;
2884 if (starts_with(line
, "--shallow ")) {
2885 struct object_id oid
;
2886 if (get_oid_hex(line
+ 10, &oid
))
2887 die("not an SHA-1 '%s'", line
+ 10);
2888 register_shallow(&oid
);
2889 use_bitmap_index
= 0;
2892 die("not a rev '%s'", line
);
2894 if (handle_revision_arg(line
, &revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
2895 die("bad revision '%s'", line
);
2898 if (use_bitmap_index
&& !get_object_list_from_bitmap(&revs
))
2901 if (prepare_revision_walk(&revs
))
2902 die("revision walk setup failed");
2903 mark_edges_uninteresting(&revs
, show_edge
);
2905 if (!fn_show_object
)
2906 fn_show_object
= show_object
;
2907 traverse_commit_list_filtered(&filter_options
, &revs
,
2908 show_commit
, fn_show_object
, NULL
,
2911 if (unpack_unreachable_expiration
) {
2912 revs
.ignore_missing_links
= 1;
2913 if (add_unseen_recent_objects_to_traversal(&revs
,
2914 unpack_unreachable_expiration
))
2915 die("unable to add recent objects");
2916 if (prepare_revision_walk(&revs
))
2917 die("revision walk setup failed");
2918 traverse_commit_list(&revs
, record_recent_commit
,
2919 record_recent_object
, NULL
);
2922 if (keep_unreachable
)
2923 add_objects_in_unpacked_packs(&revs
);
2924 if (pack_loose_unreachable
)
2925 add_unreachable_loose_objects();
2926 if (unpack_unreachable
)
2927 loosen_unused_packed_objects(&revs
);
2929 oid_array_clear(&recent_objects
);
2932 static void add_extra_kept_packs(const struct string_list
*names
)
2934 struct packed_git
*p
;
2939 for (p
= get_packed_git(the_repository
); p
; p
= p
->next
) {
2940 const char *name
= basename(p
->pack_name
);
2946 for (i
= 0; i
< names
->nr
; i
++)
2947 if (!fspathcmp(name
, names
->items
[i
].string
))
2950 if (i
< names
->nr
) {
2951 p
->pack_keep_in_core
= 1;
2952 ignore_packed_keep_in_core
= 1;
2958 static int option_parse_index_version(const struct option
*opt
,
2959 const char *arg
, int unset
)
2962 const char *val
= arg
;
2963 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
2964 if (pack_idx_opts
.version
> 2)
2965 die(_("unsupported index version %s"), val
);
2966 if (*c
== ',' && c
[1])
2967 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
2968 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
2969 die(_("bad index version '%s'"), val
);
2973 static int option_parse_unpack_unreachable(const struct option
*opt
,
2974 const char *arg
, int unset
)
2977 unpack_unreachable
= 0;
2978 unpack_unreachable_expiration
= 0;
2981 unpack_unreachable
= 1;
2983 unpack_unreachable_expiration
= approxidate(arg
);
2988 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
2990 int use_internal_rev_list
= 0;
2993 int all_progress_implied
= 0;
2994 struct argv_array rp
= ARGV_ARRAY_INIT
;
2995 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
2996 int rev_list_index
= 0;
2997 struct string_list keep_pack_list
= STRING_LIST_INIT_NODUP
;
2998 struct option pack_objects_options
[] = {
2999 OPT_SET_INT('q', "quiet", &progress
,
3000 N_("do not show progress meter"), 0),
3001 OPT_SET_INT(0, "progress", &progress
,
3002 N_("show progress meter"), 1),
3003 OPT_SET_INT(0, "all-progress", &progress
,
3004 N_("show progress meter during object writing phase"), 2),
3005 OPT_BOOL(0, "all-progress-implied",
3006 &all_progress_implied
,
3007 N_("similar to --all-progress when progress meter is shown")),
3008 { OPTION_CALLBACK
, 0, "index-version", NULL
, N_("version[,offset]"),
3009 N_("write the pack index file in the specified idx format version"),
3010 0, option_parse_index_version
},
3011 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
3012 N_("maximum size of each output pack file")),
3013 OPT_BOOL(0, "local", &local
,
3014 N_("ignore borrowed objects from alternate object store")),
3015 OPT_BOOL(0, "incremental", &incremental
,
3016 N_("ignore packed objects")),
3017 OPT_INTEGER(0, "window", &window
,
3018 N_("limit pack window by objects")),
3019 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
3020 N_("limit pack window by memory in addition to object limit")),
3021 OPT_INTEGER(0, "depth", &depth
,
3022 N_("maximum length of delta chain allowed in the resulting pack")),
3023 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
3024 N_("reuse existing deltas")),
3025 OPT_BOOL(0, "reuse-object", &reuse_object
,
3026 N_("reuse existing objects")),
3027 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
3028 N_("use OFS_DELTA objects")),
3029 OPT_INTEGER(0, "threads", &delta_search_threads
,
3030 N_("use threads when searching for best delta matches")),
3031 OPT_BOOL(0, "non-empty", &non_empty
,
3032 N_("do not create an empty pack output")),
3033 OPT_BOOL(0, "revs", &use_internal_rev_list
,
3034 N_("read revision arguments from standard input")),
3035 { OPTION_SET_INT
, 0, "unpacked", &rev_list_unpacked
, NULL
,
3036 N_("limit the objects to those that are not yet packed"),
3037 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
3038 { OPTION_SET_INT
, 0, "all", &rev_list_all
, NULL
,
3039 N_("include objects reachable from any reference"),
3040 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
3041 { OPTION_SET_INT
, 0, "reflog", &rev_list_reflog
, NULL
,
3042 N_("include objects referred by reflog entries"),
3043 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
3044 { OPTION_SET_INT
, 0, "indexed-objects", &rev_list_index
, NULL
,
3045 N_("include objects referred to by the index"),
3046 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
3047 OPT_BOOL(0, "stdout", &pack_to_stdout
,
3048 N_("output pack to stdout")),
3049 OPT_BOOL(0, "include-tag", &include_tag
,
3050 N_("include tag objects that refer to objects to be packed")),
3051 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
3052 N_("keep unreachable objects")),
3053 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
3054 N_("pack loose unreachable objects")),
3055 { OPTION_CALLBACK
, 0, "unpack-unreachable", NULL
, N_("time"),
3056 N_("unpack unreachable objects newer than <time>"),
3057 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
},
3058 OPT_BOOL(0, "thin", &thin
,
3059 N_("create thin packs")),
3060 OPT_BOOL(0, "shallow", &shallow
,
3061 N_("create packs suitable for shallow fetches")),
3062 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep_on_disk
,
3063 N_("ignore packs that have companion .keep file")),
3064 OPT_STRING_LIST(0, "keep-pack", &keep_pack_list
, N_("name"),
3065 N_("ignore this pack")),
3066 OPT_INTEGER(0, "compression", &pack_compression_level
,
3067 N_("pack compression level")),
3068 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents
,
3069 N_("do not hide commits by grafts"), 0),
3070 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
3071 N_("use a bitmap index if available to speed up counting objects")),
3072 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index
,
3073 N_("write a bitmap index together with the pack index")),
3074 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
3075 { OPTION_CALLBACK
, 0, "missing", NULL
, N_("action"),
3076 N_("handling for missing objects"), PARSE_OPT_NONEG
,
3077 option_parse_missing_action
},
3078 OPT_BOOL(0, "exclude-promisor-objects", &exclude_promisor_objects
,
3079 N_("do not pack objects in promisor packfiles")),
3083 check_replace_refs
= 0;
3085 reset_pack_idx_option(&pack_idx_opts
);
3086 git_config(git_pack_config
, NULL
);
3088 progress
= isatty(2);
3089 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
3093 base_name
= argv
[0];
3096 if (pack_to_stdout
!= !base_name
|| argc
)
3097 usage_with_options(pack_usage
, pack_objects_options
);
3099 argv_array_push(&rp
, "pack-objects");
3101 use_internal_rev_list
= 1;
3102 argv_array_push(&rp
, shallow
3103 ? "--objects-edge-aggressive"
3104 : "--objects-edge");
3106 argv_array_push(&rp
, "--objects");
3109 use_internal_rev_list
= 1;
3110 argv_array_push(&rp
, "--all");
3112 if (rev_list_reflog
) {
3113 use_internal_rev_list
= 1;
3114 argv_array_push(&rp
, "--reflog");
3116 if (rev_list_index
) {
3117 use_internal_rev_list
= 1;
3118 argv_array_push(&rp
, "--indexed-objects");
3120 if (rev_list_unpacked
) {
3121 use_internal_rev_list
= 1;
3122 argv_array_push(&rp
, "--unpacked");
3125 if (exclude_promisor_objects
) {
3126 use_internal_rev_list
= 1;
3127 fetch_if_missing
= 0;
3128 argv_array_push(&rp
, "--exclude-promisor-objects");
3133 if (pack_compression_level
== -1)
3134 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
3135 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
3136 die("bad pack compression level %d", pack_compression_level
);
3138 if (!delta_search_threads
) /* --threads=0 means autodetect */
3139 delta_search_threads
= online_cpus();
3142 if (delta_search_threads
!= 1)
3143 warning("no threads support, ignoring --threads");
3145 if (!pack_to_stdout
&& !pack_size_limit
)
3146 pack_size_limit
= pack_size_limit_cfg
;
3147 if (pack_to_stdout
&& pack_size_limit
)
3148 die("--max-pack-size cannot be used to build a pack for transfer.");
3149 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
3150 warning("minimum pack size limit is 1 MiB");
3151 pack_size_limit
= 1024*1024;
3154 if (!pack_to_stdout
&& thin
)
3155 die("--thin cannot be used to build an indexable pack.");
3157 if (keep_unreachable
&& unpack_unreachable
)
3158 die("--keep-unreachable and --unpack-unreachable are incompatible.");
3159 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
3160 unpack_unreachable_expiration
= 0;
3162 if (filter_options
.choice
) {
3163 if (!pack_to_stdout
)
3164 die("cannot use --filter without --stdout.");
3165 use_bitmap_index
= 0;
3169 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
3171 * - to produce good pack (with bitmap index not-yet-packed objects are
3172 * packed in suboptimal order).
3174 * - to use more robust pack-generation codepath (avoiding possible
3175 * bugs in bitmap code and possible bitmap index corruption).
3177 if (!pack_to_stdout
)
3178 use_bitmap_index_default
= 0;
3180 if (use_bitmap_index
< 0)
3181 use_bitmap_index
= use_bitmap_index_default
;
3183 /* "hard" reasons not to use bitmaps; these just won't work at all */
3184 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow())
3185 use_bitmap_index
= 0;
3187 if (pack_to_stdout
|| !rev_list_all
)
3188 write_bitmap_index
= 0;
3190 if (progress
&& all_progress_implied
)
3193 add_extra_kept_packs(&keep_pack_list
);
3194 if (ignore_packed_keep_on_disk
) {
3195 struct packed_git
*p
;
3196 for (p
= get_packed_git(the_repository
); p
; p
= p
->next
)
3197 if (p
->pack_local
&& p
->pack_keep
)
3199 if (!p
) /* no keep-able packs found */
3200 ignore_packed_keep_on_disk
= 0;
3204 * unlike ignore_packed_keep_on_disk above, we do not
3205 * want to unset "local" based on looking at packs, as
3206 * it also covers non-local objects
3208 struct packed_git
*p
;
3209 for (p
= get_packed_git(the_repository
); p
; p
= p
->next
) {
3210 if (!p
->pack_local
) {
3211 have_non_local_packs
= 1;
3218 progress_state
= start_progress(_("Enumerating objects"), 0);
3219 if (!use_internal_rev_list
)
3220 read_object_list_from_stdin();
3222 get_object_list(rp
.argc
, rp
.argv
);
3223 argv_array_clear(&rp
);
3225 cleanup_preferred_base();
3226 if (include_tag
&& nr_result
)
3227 for_each_ref(add_ref_tag
, NULL
);
3228 stop_progress(&progress_state
);
3230 if (non_empty
&& !nr_result
)
3233 prepare_pack(window
, depth
);
3236 fprintf(stderr
, "Total %"PRIu32
" (delta %"PRIu32
"),"
3237 " reused %"PRIu32
" (delta %"PRIu32
")\n",
3238 written
, written_delta
, reused
, reused_delta
);