11 #include "pack-revindex.h"
12 #include "csum-file.h"
13 #include "tree-walk.h"
16 #include "list-objects.h"
17 #include "pack-objects.h"
20 #include "streaming.h"
21 #include "thread-utils.h"
22 #include "pack-bitmap.h"
23 #include "reachable.h"
24 #include "sha1-array.h"
25 #include "argv-array.h"
28 static const char *pack_usage
[] = {
29 N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"),
30 N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"),
35 * Objects we are going to pack are collected in the `to_pack` structure.
36 * It contains an array (dynamically expanded) of the object data, and a map
37 * that can resolve SHA1s to their position in the array.
39 static struct packing_data to_pack
;
41 static struct pack_idx_entry
**written_list
;
42 static uint32_t nr_result
, nr_written
;
45 static int reuse_delta
= 1, reuse_object
= 1;
46 static int keep_unreachable
, unpack_unreachable
, include_tag
;
47 static unsigned long unpack_unreachable_expiration
;
48 static int pack_loose_unreachable
;
50 static int have_non_local_packs
;
51 static int incremental
;
52 static int ignore_packed_keep
;
53 static int allow_ofs_delta
;
54 static struct pack_idx_option pack_idx_opts
;
55 static const char *base_name
;
56 static int progress
= 1;
57 static int window
= 10;
58 static unsigned long pack_size_limit
;
59 static int depth
= 50;
60 static int delta_search_threads
;
61 static int pack_to_stdout
;
62 static int num_preferred_base
;
63 static struct progress
*progress_state
;
65 static struct packed_git
*reuse_packfile
;
66 static uint32_t reuse_packfile_objects
;
67 static off_t reuse_packfile_offset
;
69 static int use_bitmap_index_default
= 1;
70 static int use_bitmap_index
= -1;
71 static int write_bitmap_index
;
72 static uint16_t write_bitmap_options
;
74 static unsigned long delta_cache_size
= 0;
75 static unsigned long max_delta_cache_size
= 256 * 1024 * 1024;
76 static unsigned long cache_max_small_delta_size
= 1000;
78 static unsigned long window_memory_limit
= 0;
83 static uint32_t written
, written_delta
;
84 static uint32_t reused
, reused_delta
;
89 static struct commit
**indexed_commits
;
90 static unsigned int indexed_commits_nr
;
91 static unsigned int indexed_commits_alloc
;
93 static void index_commit_for_bitmap(struct commit
*commit
)
95 if (indexed_commits_nr
>= indexed_commits_alloc
) {
96 indexed_commits_alloc
= (indexed_commits_alloc
+ 32) * 2;
97 REALLOC_ARRAY(indexed_commits
, indexed_commits_alloc
);
100 indexed_commits
[indexed_commits_nr
++] = commit
;
103 static void *get_delta(struct object_entry
*entry
)
105 unsigned long size
, base_size
, delta_size
;
106 void *buf
, *base_buf
, *delta_buf
;
107 enum object_type type
;
109 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
111 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
112 base_buf
= read_sha1_file(entry
->delta
->idx
.sha1
, &type
, &base_size
);
114 die("unable to read %s", sha1_to_hex(entry
->delta
->idx
.sha1
));
115 delta_buf
= diff_delta(base_buf
, base_size
,
116 buf
, size
, &delta_size
, 0);
117 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
118 die("delta size changed");
124 static unsigned long do_compress(void **pptr
, unsigned long size
)
128 unsigned long maxsize
;
130 git_deflate_init(&stream
, pack_compression_level
);
131 maxsize
= git_deflate_bound(&stream
, size
);
134 out
= xmalloc(maxsize
);
138 stream
.avail_in
= size
;
139 stream
.next_out
= out
;
140 stream
.avail_out
= maxsize
;
141 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
143 git_deflate_end(&stream
);
146 return stream
.total_out
;
149 static unsigned long write_large_blob_data(struct git_istream
*st
, struct sha1file
*f
,
150 const unsigned char *sha1
)
153 unsigned char ibuf
[1024 * 16];
154 unsigned char obuf
[1024 * 16];
155 unsigned long olen
= 0;
157 git_deflate_init(&stream
, pack_compression_level
);
162 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
164 die(_("unable to read %s"), sha1_to_hex(sha1
));
166 stream
.next_in
= ibuf
;
167 stream
.avail_in
= readlen
;
168 while ((stream
.avail_in
|| readlen
== 0) &&
169 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
170 stream
.next_out
= obuf
;
171 stream
.avail_out
= sizeof(obuf
);
172 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
173 sha1write(f
, obuf
, stream
.next_out
- obuf
);
174 olen
+= stream
.next_out
- obuf
;
177 die(_("deflate error (%d)"), zret
);
179 if (zret
!= Z_STREAM_END
)
180 die(_("deflate error (%d)"), zret
);
184 git_deflate_end(&stream
);
189 * we are going to reuse the existing object data as is. make
190 * sure it is not corrupt.
192 static int check_pack_inflate(struct packed_git
*p
,
193 struct pack_window
**w_curs
,
196 unsigned long expect
)
199 unsigned char fakebuf
[4096], *in
;
202 memset(&stream
, 0, sizeof(stream
));
203 git_inflate_init(&stream
);
205 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
207 stream
.next_out
= fakebuf
;
208 stream
.avail_out
= sizeof(fakebuf
);
209 st
= git_inflate(&stream
, Z_FINISH
);
210 offset
+= stream
.next_in
- in
;
211 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
212 git_inflate_end(&stream
);
213 return (st
== Z_STREAM_END
&&
214 stream
.total_out
== expect
&&
215 stream
.total_in
== len
) ? 0 : -1;
218 static void copy_pack_data(struct sha1file
*f
,
219 struct packed_git
*p
,
220 struct pack_window
**w_curs
,
228 in
= use_pack(p
, w_curs
, offset
, &avail
);
230 avail
= (unsigned long)len
;
231 sha1write(f
, in
, avail
);
237 /* Return 0 if we will bust the pack-size limit */
238 static unsigned long write_no_reuse_object(struct sha1file
*f
, struct object_entry
*entry
,
239 unsigned long limit
, int usable_delta
)
241 unsigned long size
, datalen
;
242 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
243 dheader
[MAX_PACK_OBJECT_HEADER
];
245 enum object_type type
;
247 struct git_istream
*st
= NULL
;
250 if (entry
->type
== OBJ_BLOB
&&
251 entry
->size
> big_file_threshold
&&
252 (st
= open_istream(entry
->idx
.sha1
, &type
, &size
, NULL
)) != NULL
)
255 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
257 die(_("unable to read %s"), sha1_to_hex(entry
->idx
.sha1
));
260 * make sure no cached delta data remains from a
261 * previous attempt before a pack split occurred.
263 free(entry
->delta_data
);
264 entry
->delta_data
= NULL
;
265 entry
->z_delta_size
= 0;
266 } else if (entry
->delta_data
) {
267 size
= entry
->delta_size
;
268 buf
= entry
->delta_data
;
269 entry
->delta_data
= NULL
;
270 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
271 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
273 buf
= get_delta(entry
);
274 size
= entry
->delta_size
;
275 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
276 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
279 if (st
) /* large blob case, just assume we don't compress well */
281 else if (entry
->z_delta_size
)
282 datalen
= entry
->z_delta_size
;
284 datalen
= do_compress(&buf
, size
);
287 * The object header is a byte of 'type' followed by zero or
288 * more bytes of length.
290 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
293 if (type
== OBJ_OFS_DELTA
) {
295 * Deltas with relative base contain an additional
296 * encoding of the relative offset for the delta
297 * base from this object's position in the pack.
299 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
300 unsigned pos
= sizeof(dheader
) - 1;
301 dheader
[pos
] = ofs
& 127;
303 dheader
[--pos
] = 128 | (--ofs
& 127);
304 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
310 sha1write(f
, header
, hdrlen
);
311 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
312 hdrlen
+= sizeof(dheader
) - pos
;
313 } else if (type
== OBJ_REF_DELTA
) {
315 * Deltas with a base reference contain
316 * an additional 20 bytes for the base sha1.
318 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
324 sha1write(f
, header
, hdrlen
);
325 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
328 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
334 sha1write(f
, header
, hdrlen
);
337 datalen
= write_large_blob_data(st
, f
, entry
->idx
.sha1
);
340 sha1write(f
, buf
, datalen
);
344 return hdrlen
+ datalen
;
347 /* Return 0 if we will bust the pack-size limit */
348 static off_t
write_reuse_object(struct sha1file
*f
, struct object_entry
*entry
,
349 unsigned long limit
, int usable_delta
)
351 struct packed_git
*p
= entry
->in_pack
;
352 struct pack_window
*w_curs
= NULL
;
353 struct revindex_entry
*revidx
;
355 enum object_type type
= entry
->type
;
357 unsigned char header
[MAX_PACK_OBJECT_HEADER
],
358 dheader
[MAX_PACK_OBJECT_HEADER
];
362 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
363 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
364 hdrlen
= encode_in_pack_object_header(header
, sizeof(header
),
367 offset
= entry
->in_pack_offset
;
368 revidx
= find_pack_revindex(p
, offset
);
369 datalen
= revidx
[1].offset
- offset
;
370 if (!pack_to_stdout
&& p
->index_version
> 1 &&
371 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
)) {
372 error("bad packed object CRC for %s", sha1_to_hex(entry
->idx
.sha1
));
374 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
377 offset
+= entry
->in_pack_header_size
;
378 datalen
-= entry
->in_pack_header_size
;
380 if (!pack_to_stdout
&& p
->index_version
== 1 &&
381 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
)) {
382 error("corrupt packed object for %s", sha1_to_hex(entry
->idx
.sha1
));
384 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
387 if (type
== OBJ_OFS_DELTA
) {
388 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
389 unsigned pos
= sizeof(dheader
) - 1;
390 dheader
[pos
] = ofs
& 127;
392 dheader
[--pos
] = 128 | (--ofs
& 127);
393 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
397 sha1write(f
, header
, hdrlen
);
398 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
399 hdrlen
+= sizeof(dheader
) - pos
;
401 } else if (type
== OBJ_REF_DELTA
) {
402 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
406 sha1write(f
, header
, hdrlen
);
407 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
411 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
415 sha1write(f
, header
, hdrlen
);
417 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
420 return hdrlen
+ datalen
;
423 /* Return 0 if we will bust the pack-size limit */
424 static off_t
write_object(struct sha1file
*f
,
425 struct object_entry
*entry
,
430 int usable_delta
, to_reuse
;
435 /* apply size limit if limited packsize and not first object */
436 if (!pack_size_limit
|| !nr_written
)
438 else if (pack_size_limit
<= write_offset
)
440 * the earlier object did not fit the limit; avoid
441 * mistaking this with unlimited (i.e. limit = 0).
445 limit
= pack_size_limit
- write_offset
;
448 usable_delta
= 0; /* no delta */
449 else if (!pack_size_limit
)
450 usable_delta
= 1; /* unlimited packfile */
451 else if (entry
->delta
->idx
.offset
== (off_t
)-1)
452 usable_delta
= 0; /* base was written to another pack */
453 else if (entry
->delta
->idx
.offset
)
454 usable_delta
= 1; /* base already exists in this pack */
456 usable_delta
= 0; /* base could end up in another pack */
459 to_reuse
= 0; /* explicit */
460 else if (!entry
->in_pack
)
461 to_reuse
= 0; /* can't reuse what we don't have */
462 else if (entry
->type
== OBJ_REF_DELTA
|| entry
->type
== OBJ_OFS_DELTA
)
463 /* check_object() decided it for us ... */
464 to_reuse
= usable_delta
;
465 /* ... but pack split may override that */
466 else if (entry
->type
!= entry
->in_pack_type
)
467 to_reuse
= 0; /* pack has delta which is unusable */
468 else if (entry
->delta
)
469 to_reuse
= 0; /* we want to pack afresh */
471 to_reuse
= 1; /* we have it in-pack undeltified,
472 * and we do not need to deltify it.
476 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
478 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
486 entry
->idx
.crc32
= crc32_end(f
);
490 enum write_one_status
{
491 WRITE_ONE_SKIP
= -1, /* already written */
492 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
493 WRITE_ONE_WRITTEN
= 1, /* normal */
494 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
497 static enum write_one_status
write_one(struct sha1file
*f
,
498 struct object_entry
*e
,
505 * we set offset to 1 (which is an impossible value) to mark
506 * the fact that this object is involved in "write its base
507 * first before writing a deltified object" recursion.
509 recursing
= (e
->idx
.offset
== 1);
511 warning("recursive delta detected for object %s",
512 sha1_to_hex(e
->idx
.sha1
));
513 return WRITE_ONE_RECURSIVE
;
514 } else if (e
->idx
.offset
|| e
->preferred_base
) {
515 /* offset is non zero if object is written already. */
516 return WRITE_ONE_SKIP
;
519 /* if we are deltified, write out base object first. */
521 e
->idx
.offset
= 1; /* now recurse */
522 switch (write_one(f
, e
->delta
, offset
)) {
523 case WRITE_ONE_RECURSIVE
:
524 /* we cannot depend on this one */
529 case WRITE_ONE_BREAK
:
530 e
->idx
.offset
= recursing
;
531 return WRITE_ONE_BREAK
;
535 e
->idx
.offset
= *offset
;
536 size
= write_object(f
, e
, *offset
);
538 e
->idx
.offset
= recursing
;
539 return WRITE_ONE_BREAK
;
541 written_list
[nr_written
++] = &e
->idx
;
543 /* make sure off_t is sufficiently large not to wrap */
544 if (signed_add_overflows(*offset
, size
))
545 die("pack too large for current definition of off_t");
547 return WRITE_ONE_WRITTEN
;
550 static int mark_tagged(const char *path
, const struct object_id
*oid
, int flag
,
553 unsigned char peeled
[20];
554 struct object_entry
*entry
= packlist_find(&to_pack
, oid
->hash
, NULL
);
558 if (!peel_ref(path
, peeled
)) {
559 entry
= packlist_find(&to_pack
, peeled
, NULL
);
566 static inline void add_to_write_order(struct object_entry
**wo
,
568 struct object_entry
*e
)
576 static void add_descendants_to_write_order(struct object_entry
**wo
,
578 struct object_entry
*e
)
580 int add_to_order
= 1;
583 struct object_entry
*s
;
584 /* add this node... */
585 add_to_write_order(wo
, endp
, e
);
586 /* all its siblings... */
587 for (s
= e
->delta_sibling
; s
; s
= s
->delta_sibling
) {
588 add_to_write_order(wo
, endp
, s
);
591 /* drop down a level to add left subtree nodes if possible */
592 if (e
->delta_child
) {
597 /* our sibling might have some children, it is next */
598 if (e
->delta_sibling
) {
599 e
= e
->delta_sibling
;
602 /* go back to our parent node */
604 while (e
&& !e
->delta_sibling
) {
605 /* we're on the right side of a subtree, keep
606 * going up until we can go right again */
610 /* done- we hit our original root node */
613 /* pass it off to sibling at this level */
614 e
= e
->delta_sibling
;
619 static void add_family_to_write_order(struct object_entry
**wo
,
621 struct object_entry
*e
)
623 struct object_entry
*root
;
625 for (root
= e
; root
->delta
; root
= root
->delta
)
627 add_descendants_to_write_order(wo
, endp
, root
);
630 static struct object_entry
**compute_write_order(void)
632 unsigned int i
, wo_end
, last_untagged
;
634 struct object_entry
**wo
;
635 struct object_entry
*objects
= to_pack
.objects
;
637 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
638 objects
[i
].tagged
= 0;
639 objects
[i
].filled
= 0;
640 objects
[i
].delta_child
= NULL
;
641 objects
[i
].delta_sibling
= NULL
;
645 * Fully connect delta_child/delta_sibling network.
646 * Make sure delta_sibling is sorted in the original
649 for (i
= to_pack
.nr_objects
; i
> 0;) {
650 struct object_entry
*e
= &objects
[--i
];
653 /* Mark me as the first child */
654 e
->delta_sibling
= e
->delta
->delta_child
;
655 e
->delta
->delta_child
= e
;
659 * Mark objects that are at the tip of tags.
661 for_each_tag_ref(mark_tagged
, NULL
);
664 * Give the objects in the original recency order until
665 * we see a tagged tip.
667 ALLOC_ARRAY(wo
, to_pack
.nr_objects
);
668 for (i
= wo_end
= 0; i
< to_pack
.nr_objects
; i
++) {
669 if (objects
[i
].tagged
)
671 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
676 * Then fill all the tagged tips.
678 for (; i
< to_pack
.nr_objects
; i
++) {
679 if (objects
[i
].tagged
)
680 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
684 * And then all remaining commits and tags.
686 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
687 if (objects
[i
].type
!= OBJ_COMMIT
&&
688 objects
[i
].type
!= OBJ_TAG
)
690 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
694 * And then all the trees.
696 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
697 if (objects
[i
].type
!= OBJ_TREE
)
699 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
703 * Finally all the rest in really tight order
705 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
706 if (!objects
[i
].filled
)
707 add_family_to_write_order(wo
, &wo_end
, &objects
[i
]);
710 if (wo_end
!= to_pack
.nr_objects
)
711 die("ordered %u objects, expected %"PRIu32
, wo_end
, to_pack
.nr_objects
);
716 static off_t
write_reused_pack(struct sha1file
*f
)
718 unsigned char buffer
[8192];
719 off_t to_write
, total
;
722 if (!is_pack_valid(reuse_packfile
))
723 die("packfile is invalid: %s", reuse_packfile
->pack_name
);
725 fd
= git_open(reuse_packfile
->pack_name
);
727 die_errno("unable to open packfile for reuse: %s",
728 reuse_packfile
->pack_name
);
730 if (lseek(fd
, sizeof(struct pack_header
), SEEK_SET
) == -1)
731 die_errno("unable to seek in reused packfile");
733 if (reuse_packfile_offset
< 0)
734 reuse_packfile_offset
= reuse_packfile
->pack_size
- 20;
736 total
= to_write
= reuse_packfile_offset
- sizeof(struct pack_header
);
739 int read_pack
= xread(fd
, buffer
, sizeof(buffer
));
742 die_errno("unable to read from reused packfile");
744 if (read_pack
> to_write
)
745 read_pack
= to_write
;
747 sha1write(f
, buffer
, read_pack
);
748 to_write
-= read_pack
;
751 * We don't know the actual number of objects written,
752 * only how many bytes written, how many bytes total, and
753 * how many objects total. So we can fake it by pretending all
754 * objects we are writing are the same size. This gives us a
755 * smooth progress meter, and at the end it matches the true
758 written
= reuse_packfile_objects
*
759 (((double)(total
- to_write
)) / total
);
760 display_progress(progress_state
, written
);
764 written
= reuse_packfile_objects
;
765 display_progress(progress_state
, written
);
766 return reuse_packfile_offset
- sizeof(struct pack_header
);
769 static const char no_split_warning
[] = N_(
770 "disabling bitmap writing, packs are split due to pack.packSizeLimit"
773 static void write_pack_file(void)
778 uint32_t nr_remaining
= nr_result
;
779 time_t last_mtime
= 0;
780 struct object_entry
**write_order
;
782 if (progress
> pack_to_stdout
)
783 progress_state
= start_progress(_("Writing objects"), nr_result
);
784 ALLOC_ARRAY(written_list
, to_pack
.nr_objects
);
785 write_order
= compute_write_order();
788 unsigned char sha1
[20];
789 char *pack_tmp_name
= NULL
;
792 f
= sha1fd_throughput(1, "<stdout>", progress_state
);
794 f
= create_tmp_packfile(&pack_tmp_name
);
796 offset
= write_pack_header(f
, nr_remaining
);
798 if (reuse_packfile
) {
800 assert(pack_to_stdout
);
802 packfile_size
= write_reused_pack(f
);
803 offset
+= packfile_size
;
807 for (; i
< to_pack
.nr_objects
; i
++) {
808 struct object_entry
*e
= write_order
[i
];
809 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
811 display_progress(progress_state
, written
);
815 * Did we write the wrong # entries in the header?
816 * If so, rewrite it like in fast-import
818 if (pack_to_stdout
) {
819 sha1close(f
, sha1
, CSUM_CLOSE
);
820 } else if (nr_written
== nr_remaining
) {
821 sha1close(f
, sha1
, CSUM_FSYNC
);
823 int fd
= sha1close(f
, sha1
, 0);
824 fixup_pack_header_footer(fd
, sha1
, pack_tmp_name
,
825 nr_written
, sha1
, offset
);
827 if (write_bitmap_index
) {
828 warning(_(no_split_warning
));
829 write_bitmap_index
= 0;
833 if (!pack_to_stdout
) {
835 struct strbuf tmpname
= STRBUF_INIT
;
838 * Packs are runtime accessed in their mtime
839 * order since newer packs are more likely to contain
840 * younger objects. So if we are creating multiple
841 * packs then we should modify the mtime of later ones
842 * to preserve this property.
844 if (stat(pack_tmp_name
, &st
) < 0) {
845 warning_errno("failed to stat %s", pack_tmp_name
);
846 } else if (!last_mtime
) {
847 last_mtime
= st
.st_mtime
;
850 utb
.actime
= st
.st_atime
;
851 utb
.modtime
= --last_mtime
;
852 if (utime(pack_tmp_name
, &utb
) < 0)
853 warning_errno("failed utime() on %s", pack_tmp_name
);
856 strbuf_addf(&tmpname
, "%s-", base_name
);
858 if (write_bitmap_index
) {
859 bitmap_writer_set_checksum(sha1
);
860 bitmap_writer_build_type_index(written_list
, nr_written
);
863 finish_tmp_packfile(&tmpname
, pack_tmp_name
,
864 written_list
, nr_written
,
865 &pack_idx_opts
, sha1
);
867 if (write_bitmap_index
) {
868 strbuf_addf(&tmpname
, "%s.bitmap", sha1_to_hex(sha1
));
870 stop_progress(&progress_state
);
872 bitmap_writer_show_progress(progress
);
873 bitmap_writer_reuse_bitmaps(&to_pack
);
874 bitmap_writer_select_commits(indexed_commits
, indexed_commits_nr
, -1);
875 bitmap_writer_build(&to_pack
);
876 bitmap_writer_finish(written_list
, nr_written
,
877 tmpname
.buf
, write_bitmap_options
);
878 write_bitmap_index
= 0;
881 strbuf_release(&tmpname
);
883 puts(sha1_to_hex(sha1
));
886 /* mark written objects as written to previous pack */
887 for (j
= 0; j
< nr_written
; j
++) {
888 written_list
[j
]->offset
= (off_t
)-1;
890 nr_remaining
-= nr_written
;
891 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
895 stop_progress(&progress_state
);
896 if (written
!= nr_result
)
897 die("wrote %"PRIu32
" objects while expecting %"PRIu32
,
901 static int no_try_delta(const char *path
)
903 static struct attr_check
*check
;
906 check
= attr_check_initl("delta", NULL
);
907 if (git_check_attr(path
, check
))
909 if (ATTR_FALSE(check
->items
[0].value
))
915 * When adding an object, check whether we have already added it
916 * to our packing list. If so, we can skip. However, if we are
917 * being asked to excludei t, but the previous mention was to include
918 * it, make sure to adjust its flags and tweak our numbers accordingly.
920 * As an optimization, we pass out the index position where we would have
921 * found the item, since that saves us from having to look it up again a
922 * few lines later when we want to add the new entry.
924 static int have_duplicate_entry(const unsigned char *sha1
,
928 struct object_entry
*entry
;
930 entry
= packlist_find(&to_pack
, sha1
, index_pos
);
935 if (!entry
->preferred_base
)
937 entry
->preferred_base
= 1;
943 static int want_found_object(int exclude
, struct packed_git
*p
)
951 * When asked to do --local (do not include an object that appears in a
952 * pack we borrow from elsewhere) or --honor-pack-keep (do not include
953 * an object that appears in a pack marked with .keep), finding a pack
954 * that matches the criteria is sufficient for us to decide to omit it.
955 * However, even if this pack does not satisfy the criteria, we need to
956 * make sure no copy of this object appears in _any_ pack that makes us
957 * to omit the object, so we need to check all the packs.
959 * We can however first check whether these options can possible matter;
960 * if they do not matter we know we want the object in generated pack.
961 * Otherwise, we signal "-1" at the end to tell the caller that we do
962 * not know either way, and it needs to check more packs.
964 if (!ignore_packed_keep
&&
965 (!local
|| !have_non_local_packs
))
968 if (local
&& !p
->pack_local
)
970 if (ignore_packed_keep
&& p
->pack_local
&& p
->pack_keep
)
973 /* we don't know yet; keep looking for more packs */
978 * Check whether we want the object in the pack (e.g., we do not want
979 * objects found in non-local stores if the "--local" option was used).
981 * If the caller already knows an existing pack it wants to take the object
982 * from, that is passed in *found_pack and *found_offset; otherwise this
983 * function finds if there is any pack that has the object and returns the pack
984 * and its offset in these variables.
986 static int want_object_in_pack(const unsigned char *sha1
,
988 struct packed_git
**found_pack
,
991 struct mru_entry
*entry
;
994 if (!exclude
&& local
&& has_loose_object_nonlocal(sha1
))
998 * If we already know the pack object lives in, start checks from that
999 * pack - in the usual case when neither --local was given nor .keep files
1000 * are present we will determine the answer right now.
1003 want
= want_found_object(exclude
, *found_pack
);
1008 for (entry
= packed_git_mru
->head
; entry
; entry
= entry
->next
) {
1009 struct packed_git
*p
= entry
->item
;
1012 if (p
== *found_pack
)
1013 offset
= *found_offset
;
1015 offset
= find_pack_entry_one(sha1
, p
);
1019 if (!is_pack_valid(p
))
1021 *found_offset
= offset
;
1024 want
= want_found_object(exclude
, p
);
1025 if (!exclude
&& want
> 0)
1026 mru_mark(packed_git_mru
, entry
);
1035 static void create_object_entry(const unsigned char *sha1
,
1036 enum object_type type
,
1041 struct packed_git
*found_pack
,
1044 struct object_entry
*entry
;
1046 entry
= packlist_alloc(&to_pack
, sha1
, index_pos
);
1051 entry
->preferred_base
= 1;
1055 entry
->in_pack
= found_pack
;
1056 entry
->in_pack_offset
= found_offset
;
1059 entry
->no_try_delta
= no_try_delta
;
1062 static const char no_closure_warning
[] = N_(
1063 "disabling bitmap writing, as some objects are not being packed"
1066 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
1067 const char *name
, int exclude
)
1069 struct packed_git
*found_pack
= NULL
;
1070 off_t found_offset
= 0;
1073 if (have_duplicate_entry(sha1
, exclude
, &index_pos
))
1076 if (!want_object_in_pack(sha1
, exclude
, &found_pack
, &found_offset
)) {
1077 /* The pack is missing an object, so it will not have closure */
1078 if (write_bitmap_index
) {
1079 warning(_(no_closure_warning
));
1080 write_bitmap_index
= 0;
1085 create_object_entry(sha1
, type
, pack_name_hash(name
),
1086 exclude
, name
&& no_try_delta(name
),
1087 index_pos
, found_pack
, found_offset
);
1089 display_progress(progress_state
, nr_result
);
1093 static int add_object_entry_from_bitmap(const unsigned char *sha1
,
1094 enum object_type type
,
1095 int flags
, uint32_t name_hash
,
1096 struct packed_git
*pack
, off_t offset
)
1100 if (have_duplicate_entry(sha1
, 0, &index_pos
))
1103 if (!want_object_in_pack(sha1
, 0, &pack
, &offset
))
1106 create_object_entry(sha1
, type
, name_hash
, 0, 0, index_pos
, pack
, offset
);
1108 display_progress(progress_state
, nr_result
);
1112 struct pbase_tree_cache
{
1113 unsigned char sha1
[20];
1117 unsigned long tree_size
;
1120 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
1121 static int pbase_tree_cache_ix(const unsigned char *sha1
)
1123 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
1125 static int pbase_tree_cache_ix_incr(int ix
)
1127 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
1130 static struct pbase_tree
{
1131 struct pbase_tree
*next
;
1132 /* This is a phony "cache" entry; we are not
1133 * going to evict it or find it through _get()
1134 * mechanism -- this is for the toplevel node that
1135 * would almost always change with any commit.
1137 struct pbase_tree_cache pcache
;
1140 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
1142 struct pbase_tree_cache
*ent
, *nent
;
1145 enum object_type type
;
1147 int my_ix
= pbase_tree_cache_ix(sha1
);
1148 int available_ix
= -1;
1150 /* pbase-tree-cache acts as a limited hashtable.
1151 * your object will be found at your index or within a few
1152 * slots after that slot if it is cached.
1154 for (neigh
= 0; neigh
< 8; neigh
++) {
1155 ent
= pbase_tree_cache
[my_ix
];
1156 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
1160 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
1161 ((0 <= available_ix
) &&
1162 (!ent
&& pbase_tree_cache
[available_ix
])))
1163 available_ix
= my_ix
;
1166 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
1169 /* Did not find one. Either we got a bogus request or
1170 * we need to read and perhaps cache.
1172 data
= read_sha1_file(sha1
, &type
, &size
);
1175 if (type
!= OBJ_TREE
) {
1180 /* We need to either cache or return a throwaway copy */
1182 if (available_ix
< 0)
1185 ent
= pbase_tree_cache
[available_ix
];
1186 my_ix
= available_ix
;
1190 nent
= xmalloc(sizeof(*nent
));
1191 nent
->temporary
= (available_ix
< 0);
1194 /* evict and reuse */
1195 free(ent
->tree_data
);
1198 hashcpy(nent
->sha1
, sha1
);
1199 nent
->tree_data
= data
;
1200 nent
->tree_size
= size
;
1202 if (!nent
->temporary
)
1203 pbase_tree_cache
[my_ix
] = nent
;
1207 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1209 if (!cache
->temporary
) {
1213 free(cache
->tree_data
);
1217 static int name_cmp_len(const char *name
)
1220 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
1225 static void add_pbase_object(struct tree_desc
*tree
,
1228 const char *fullname
)
1230 struct name_entry entry
;
1233 while (tree_entry(tree
,&entry
)) {
1234 if (S_ISGITLINK(entry
.mode
))
1236 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1237 memcmp(name
, entry
.path
, cmplen
);
1242 if (name
[cmplen
] != '/') {
1243 add_object_entry(entry
.oid
->hash
,
1244 object_type(entry
.mode
),
1248 if (S_ISDIR(entry
.mode
)) {
1249 struct tree_desc sub
;
1250 struct pbase_tree_cache
*tree
;
1251 const char *down
= name
+cmplen
+1;
1252 int downlen
= name_cmp_len(down
);
1254 tree
= pbase_tree_get(entry
.oid
->hash
);
1257 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1259 add_pbase_object(&sub
, down
, downlen
, fullname
);
1260 pbase_tree_put(tree
);
1265 static unsigned *done_pbase_paths
;
1266 static int done_pbase_paths_num
;
1267 static int done_pbase_paths_alloc
;
1268 static int done_pbase_path_pos(unsigned hash
)
1271 int hi
= done_pbase_paths_num
;
1273 int mi
= (hi
+ lo
) / 2;
1274 if (done_pbase_paths
[mi
] == hash
)
1276 if (done_pbase_paths
[mi
] < hash
)
1284 static int check_pbase_path(unsigned hash
)
1286 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1290 ALLOC_GROW(done_pbase_paths
,
1291 done_pbase_paths_num
+ 1,
1292 done_pbase_paths_alloc
);
1293 done_pbase_paths_num
++;
1294 if (pos
< done_pbase_paths_num
)
1295 memmove(done_pbase_paths
+ pos
+ 1,
1296 done_pbase_paths
+ pos
,
1297 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1298 done_pbase_paths
[pos
] = hash
;
1302 static void add_preferred_base_object(const char *name
)
1304 struct pbase_tree
*it
;
1306 unsigned hash
= pack_name_hash(name
);
1308 if (!num_preferred_base
|| check_pbase_path(hash
))
1311 cmplen
= name_cmp_len(name
);
1312 for (it
= pbase_tree
; it
; it
= it
->next
) {
1314 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, NULL
, 1);
1317 struct tree_desc tree
;
1318 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1319 add_pbase_object(&tree
, name
, cmplen
, name
);
1324 static void add_preferred_base(unsigned char *sha1
)
1326 struct pbase_tree
*it
;
1329 unsigned char tree_sha1
[20];
1331 if (window
<= num_preferred_base
++)
1334 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1338 for (it
= pbase_tree
; it
; it
= it
->next
) {
1339 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1345 it
= xcalloc(1, sizeof(*it
));
1346 it
->next
= pbase_tree
;
1349 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1350 it
->pcache
.tree_data
= data
;
1351 it
->pcache
.tree_size
= size
;
1354 static void cleanup_preferred_base(void)
1356 struct pbase_tree
*it
;
1362 struct pbase_tree
*this = it
;
1364 free(this->pcache
.tree_data
);
1368 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1369 if (!pbase_tree_cache
[i
])
1371 free(pbase_tree_cache
[i
]->tree_data
);
1372 free(pbase_tree_cache
[i
]);
1373 pbase_tree_cache
[i
] = NULL
;
1376 free(done_pbase_paths
);
1377 done_pbase_paths
= NULL
;
1378 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1381 static void check_object(struct object_entry
*entry
)
1383 if (entry
->in_pack
) {
1384 struct packed_git
*p
= entry
->in_pack
;
1385 struct pack_window
*w_curs
= NULL
;
1386 const unsigned char *base_ref
= NULL
;
1387 struct object_entry
*base_entry
;
1388 unsigned long used
, used_0
;
1389 unsigned long avail
;
1391 unsigned char *buf
, c
;
1393 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1396 * We want in_pack_type even if we do not reuse delta
1397 * since non-delta representations could still be reused.
1399 used
= unpack_object_header_buffer(buf
, avail
,
1400 &entry
->in_pack_type
,
1406 * Determine if this is a delta and if so whether we can
1407 * reuse it or not. Otherwise let's find out as cheaply as
1408 * possible what the actual type and size for this object is.
1410 switch (entry
->in_pack_type
) {
1412 /* Not a delta hence we've already got all we need. */
1413 entry
->type
= entry
->in_pack_type
;
1414 entry
->in_pack_header_size
= used
;
1415 if (entry
->type
< OBJ_COMMIT
|| entry
->type
> OBJ_BLOB
)
1417 unuse_pack(&w_curs
);
1420 if (reuse_delta
&& !entry
->preferred_base
)
1421 base_ref
= use_pack(p
, &w_curs
,
1422 entry
->in_pack_offset
+ used
, NULL
);
1423 entry
->in_pack_header_size
= used
+ 20;
1426 buf
= use_pack(p
, &w_curs
,
1427 entry
->in_pack_offset
+ used
, NULL
);
1433 if (!ofs
|| MSB(ofs
, 7)) {
1434 error("delta base offset overflow in pack for %s",
1435 sha1_to_hex(entry
->idx
.sha1
));
1439 ofs
= (ofs
<< 7) + (c
& 127);
1441 ofs
= entry
->in_pack_offset
- ofs
;
1442 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
1443 error("delta base offset out of bound for %s",
1444 sha1_to_hex(entry
->idx
.sha1
));
1447 if (reuse_delta
&& !entry
->preferred_base
) {
1448 struct revindex_entry
*revidx
;
1449 revidx
= find_pack_revindex(p
, ofs
);
1452 base_ref
= nth_packed_object_sha1(p
, revidx
->nr
);
1454 entry
->in_pack_header_size
= used
+ used_0
;
1458 if (base_ref
&& (base_entry
= packlist_find(&to_pack
, base_ref
, NULL
))) {
1460 * If base_ref was set above that means we wish to
1461 * reuse delta data, and we even found that base
1462 * in the list of objects we want to pack. Goodie!
1464 * Depth value does not matter - find_deltas() will
1465 * never consider reused delta as the base object to
1466 * deltify other objects against, in order to avoid
1469 entry
->type
= entry
->in_pack_type
;
1470 entry
->delta
= base_entry
;
1471 entry
->delta_size
= entry
->size
;
1472 entry
->delta_sibling
= base_entry
->delta_child
;
1473 base_entry
->delta_child
= entry
;
1474 unuse_pack(&w_curs
);
1480 * This must be a delta and we already know what the
1481 * final object type is. Let's extract the actual
1482 * object size from the delta header.
1484 entry
->size
= get_size_from_delta(p
, &w_curs
,
1485 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1486 if (entry
->size
== 0)
1488 unuse_pack(&w_curs
);
1493 * No choice but to fall back to the recursive delta walk
1494 * with sha1_object_info() to find about the object type
1498 unuse_pack(&w_curs
);
1501 entry
->type
= sha1_object_info(entry
->idx
.sha1
, &entry
->size
);
1503 * The error condition is checked in prepare_pack(). This is
1504 * to permit a missing preferred base object to be ignored
1505 * as a preferred base. Doing so can result in a larger
1506 * pack file, but the transfer will still take place.
1510 static int pack_offset_sort(const void *_a
, const void *_b
)
1512 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1513 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1515 /* avoid filesystem trashing with loose objects */
1516 if (!a
->in_pack
&& !b
->in_pack
)
1517 return hashcmp(a
->idx
.sha1
, b
->idx
.sha1
);
1519 if (a
->in_pack
< b
->in_pack
)
1521 if (a
->in_pack
> b
->in_pack
)
1523 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1524 (a
->in_pack_offset
> b
->in_pack_offset
);
1528 * Drop an on-disk delta we were planning to reuse. Naively, this would
1529 * just involve blanking out the "delta" field, but we have to deal
1530 * with some extra book-keeping:
1532 * 1. Removing ourselves from the delta_sibling linked list.
1534 * 2. Updating our size/type to the non-delta representation. These were
1535 * either not recorded initially (size) or overwritten with the delta type
1536 * (type) when check_object() decided to reuse the delta.
1538 * 3. Resetting our delta depth, as we are now a base object.
1540 static void drop_reused_delta(struct object_entry
*entry
)
1542 struct object_entry
**p
= &entry
->delta
->delta_child
;
1543 struct object_info oi
= OBJECT_INFO_INIT
;
1547 *p
= (*p
)->delta_sibling
;
1549 p
= &(*p
)->delta_sibling
;
1551 entry
->delta
= NULL
;
1554 oi
.sizep
= &entry
->size
;
1555 oi
.typep
= &entry
->type
;
1556 if (packed_object_info(entry
->in_pack
, entry
->in_pack_offset
, &oi
) < 0) {
1558 * We failed to get the info from this pack for some reason;
1559 * fall back to sha1_object_info, which may find another copy.
1560 * And if that fails, the error will be recorded in entry->type
1561 * and dealt with in prepare_pack().
1563 entry
->type
= sha1_object_info(entry
->idx
.sha1
, &entry
->size
);
1568 * Follow the chain of deltas from this entry onward, throwing away any links
1569 * that cause us to hit a cycle (as determined by the DFS state flags in
1572 * We also detect too-long reused chains that would violate our --depth
1575 static void break_delta_chains(struct object_entry
*entry
)
1578 * The actual depth of each object we will write is stored as an int,
1579 * as it cannot exceed our int "depth" limit. But before we break
1580 * changes based no that limit, we may potentially go as deep as the
1581 * number of objects, which is elsewhere bounded to a uint32_t.
1583 uint32_t total_depth
;
1584 struct object_entry
*cur
, *next
;
1586 for (cur
= entry
, total_depth
= 0;
1588 cur
= cur
->delta
, total_depth
++) {
1589 if (cur
->dfs_state
== DFS_DONE
) {
1591 * We've already seen this object and know it isn't
1592 * part of a cycle. We do need to append its depth
1595 total_depth
+= cur
->depth
;
1600 * We break cycles before looping, so an ACTIVE state (or any
1601 * other cruft which made its way into the state variable)
1604 if (cur
->dfs_state
!= DFS_NONE
)
1605 die("BUG: confusing delta dfs state in first pass: %d",
1609 * Now we know this is the first time we've seen the object. If
1610 * it's not a delta, we're done traversing, but we'll mark it
1611 * done to save time on future traversals.
1614 cur
->dfs_state
= DFS_DONE
;
1619 * Mark ourselves as active and see if the next step causes
1620 * us to cycle to another active object. It's important to do
1621 * this _before_ we loop, because it impacts where we make the
1622 * cut, and thus how our total_depth counter works.
1623 * E.g., We may see a partial loop like:
1625 * A -> B -> C -> D -> B
1627 * Cutting B->C breaks the cycle. But now the depth of A is
1628 * only 1, and our total_depth counter is at 3. The size of the
1629 * error is always one less than the size of the cycle we
1630 * broke. Commits C and D were "lost" from A's chain.
1632 * If we instead cut D->B, then the depth of A is correct at 3.
1633 * We keep all commits in the chain that we examined.
1635 cur
->dfs_state
= DFS_ACTIVE
;
1636 if (cur
->delta
->dfs_state
== DFS_ACTIVE
) {
1637 drop_reused_delta(cur
);
1638 cur
->dfs_state
= DFS_DONE
;
1644 * And now that we've gone all the way to the bottom of the chain, we
1645 * need to clear the active flags and set the depth fields as
1646 * appropriate. Unlike the loop above, which can quit when it drops a
1647 * delta, we need to keep going to look for more depth cuts. So we need
1648 * an extra "next" pointer to keep going after we reset cur->delta.
1650 for (cur
= entry
; cur
; cur
= next
) {
1654 * We should have a chain of zero or more ACTIVE states down to
1655 * a final DONE. We can quit after the DONE, because either it
1656 * has no bases, or we've already handled them in a previous
1659 if (cur
->dfs_state
== DFS_DONE
)
1661 else if (cur
->dfs_state
!= DFS_ACTIVE
)
1662 die("BUG: confusing delta dfs state in second pass: %d",
1666 * If the total_depth is more than depth, then we need to snip
1667 * the chain into two or more smaller chains that don't exceed
1668 * the maximum depth. Most of the resulting chains will contain
1669 * (depth + 1) entries (i.e., depth deltas plus one base), and
1670 * the last chain (i.e., the one containing entry) will contain
1671 * whatever entries are left over, namely
1672 * (total_depth % (depth + 1)) of them.
1674 * Since we are iterating towards decreasing depth, we need to
1675 * decrement total_depth as we go, and we need to write to the
1676 * entry what its final depth will be after all of the
1677 * snipping. Since we're snipping into chains of length (depth
1678 * + 1) entries, the final depth of an entry will be its
1679 * original depth modulo (depth + 1). Any time we encounter an
1680 * entry whose final depth is supposed to be zero, we snip it
1681 * from its delta base, thereby making it so.
1683 cur
->depth
= (total_depth
--) % (depth
+ 1);
1685 drop_reused_delta(cur
);
1687 cur
->dfs_state
= DFS_DONE
;
1691 static void get_object_details(void)
1694 struct object_entry
**sorted_by_offset
;
1696 sorted_by_offset
= xcalloc(to_pack
.nr_objects
, sizeof(struct object_entry
*));
1697 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
1698 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
1699 QSORT(sorted_by_offset
, to_pack
.nr_objects
, pack_offset_sort
);
1701 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
1702 struct object_entry
*entry
= sorted_by_offset
[i
];
1703 check_object(entry
);
1704 if (big_file_threshold
< entry
->size
)
1705 entry
->no_try_delta
= 1;
1709 * This must happen in a second pass, since we rely on the delta
1710 * information for the whole list being completed.
1712 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
1713 break_delta_chains(&to_pack
.objects
[i
]);
1715 free(sorted_by_offset
);
1719 * We search for deltas in a list sorted by type, by filename hash, and then
1720 * by size, so that we see progressively smaller and smaller files.
1721 * That's because we prefer deltas to be from the bigger file
1722 * to the smaller -- deletes are potentially cheaper, but perhaps
1723 * more importantly, the bigger file is likely the more recent
1724 * one. The deepest deltas are therefore the oldest objects which are
1725 * less susceptible to be accessed often.
1727 static int type_size_sort(const void *_a
, const void *_b
)
1729 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1730 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1732 if (a
->type
> b
->type
)
1734 if (a
->type
< b
->type
)
1736 if (a
->hash
> b
->hash
)
1738 if (a
->hash
< b
->hash
)
1740 if (a
->preferred_base
> b
->preferred_base
)
1742 if (a
->preferred_base
< b
->preferred_base
)
1744 if (a
->size
> b
->size
)
1746 if (a
->size
< b
->size
)
1748 return a
< b
? -1 : (a
> b
); /* newest first */
1752 struct object_entry
*entry
;
1754 struct delta_index
*index
;
1758 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
1759 unsigned long delta_size
)
1761 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
1764 if (delta_size
< cache_max_small_delta_size
)
1767 /* cache delta, if objects are large enough compared to delta size */
1768 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
1776 static pthread_mutex_t read_mutex
;
1777 #define read_lock() pthread_mutex_lock(&read_mutex)
1778 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1780 static pthread_mutex_t cache_mutex
;
1781 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1782 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1784 static pthread_mutex_t progress_mutex
;
1785 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1786 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1790 #define read_lock() (void)0
1791 #define read_unlock() (void)0
1792 #define cache_lock() (void)0
1793 #define cache_unlock() (void)0
1794 #define progress_lock() (void)0
1795 #define progress_unlock() (void)0
1799 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1800 unsigned max_depth
, unsigned long *mem_usage
)
1802 struct object_entry
*trg_entry
= trg
->entry
;
1803 struct object_entry
*src_entry
= src
->entry
;
1804 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1806 enum object_type type
;
1809 /* Don't bother doing diffs between different types */
1810 if (trg_entry
->type
!= src_entry
->type
)
1814 * We do not bother to try a delta that we discarded on an
1815 * earlier try, but only when reusing delta data. Note that
1816 * src_entry that is marked as the preferred_base should always
1817 * be considered, as even if we produce a suboptimal delta against
1818 * it, we will still save the transfer cost, as we already know
1819 * the other side has it and we won't send src_entry at all.
1821 if (reuse_delta
&& trg_entry
->in_pack
&&
1822 trg_entry
->in_pack
== src_entry
->in_pack
&&
1823 !src_entry
->preferred_base
&&
1824 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1825 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1828 /* Let's not bust the allowed depth. */
1829 if (src
->depth
>= max_depth
)
1832 /* Now some size filtering heuristics. */
1833 trg_size
= trg_entry
->size
;
1834 if (!trg_entry
->delta
) {
1835 max_size
= trg_size
/2 - 20;
1838 max_size
= trg_entry
->delta_size
;
1839 ref_depth
= trg
->depth
;
1841 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
1842 (max_depth
- ref_depth
+ 1);
1845 src_size
= src_entry
->size
;
1846 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1847 if (sizediff
>= max_size
)
1849 if (trg_size
< src_size
/ 32)
1852 /* Load data if not already done */
1855 trg
->data
= read_sha1_file(trg_entry
->idx
.sha1
, &type
, &sz
);
1858 die("object %s cannot be read",
1859 sha1_to_hex(trg_entry
->idx
.sha1
));
1861 die("object %s inconsistent object length (%lu vs %lu)",
1862 sha1_to_hex(trg_entry
->idx
.sha1
), sz
, trg_size
);
1867 src
->data
= read_sha1_file(src_entry
->idx
.sha1
, &type
, &sz
);
1870 if (src_entry
->preferred_base
) {
1871 static int warned
= 0;
1873 warning("object %s cannot be read",
1874 sha1_to_hex(src_entry
->idx
.sha1
));
1876 * Those objects are not included in the
1877 * resulting pack. Be resilient and ignore
1878 * them if they can't be read, in case the
1879 * pack could be created nevertheless.
1883 die("object %s cannot be read",
1884 sha1_to_hex(src_entry
->idx
.sha1
));
1887 die("object %s inconsistent object length (%lu vs %lu)",
1888 sha1_to_hex(src_entry
->idx
.sha1
), sz
, src_size
);
1892 src
->index
= create_delta_index(src
->data
, src_size
);
1894 static int warned
= 0;
1896 warning("suboptimal pack - out of memory");
1899 *mem_usage
+= sizeof_delta_index(src
->index
);
1902 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1906 if (trg_entry
->delta
) {
1907 /* Prefer only shallower same-sized deltas. */
1908 if (delta_size
== trg_entry
->delta_size
&&
1909 src
->depth
+ 1 >= trg
->depth
) {
1916 * Handle memory allocation outside of the cache
1917 * accounting lock. Compiler will optimize the strangeness
1918 * away when NO_PTHREADS is defined.
1920 free(trg_entry
->delta_data
);
1922 if (trg_entry
->delta_data
) {
1923 delta_cache_size
-= trg_entry
->delta_size
;
1924 trg_entry
->delta_data
= NULL
;
1926 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
1927 delta_cache_size
+= delta_size
;
1929 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
1935 trg_entry
->delta
= src_entry
;
1936 trg_entry
->delta_size
= delta_size
;
1937 trg
->depth
= src
->depth
+ 1;
1942 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1944 struct object_entry
*child
= me
->delta_child
;
1947 unsigned int c
= check_delta_limit(child
, n
+ 1);
1950 child
= child
->delta_sibling
;
1955 static unsigned long free_unpacked(struct unpacked
*n
)
1957 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
1958 free_delta_index(n
->index
);
1961 freed_mem
+= n
->entry
->size
;
1970 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
1971 int window
, int depth
, unsigned *processed
)
1973 uint32_t i
, idx
= 0, count
= 0;
1974 struct unpacked
*array
;
1975 unsigned long mem_usage
= 0;
1977 array
= xcalloc(window
, sizeof(struct unpacked
));
1980 struct object_entry
*entry
;
1981 struct unpacked
*n
= array
+ idx
;
1982 int j
, max_depth
, best_base
= -1;
1991 if (!entry
->preferred_base
) {
1993 display_progress(progress_state
, *processed
);
1997 mem_usage
-= free_unpacked(n
);
2000 while (window_memory_limit
&&
2001 mem_usage
> window_memory_limit
&&
2003 uint32_t tail
= (idx
+ window
- count
) % window
;
2004 mem_usage
-= free_unpacked(array
+ tail
);
2008 /* We do not compute delta to *create* objects we are not
2011 if (entry
->preferred_base
)
2015 * If the current object is at pack edge, take the depth the
2016 * objects that depend on the current object into account
2017 * otherwise they would become too deep.
2020 if (entry
->delta_child
) {
2021 max_depth
-= check_delta_limit(entry
, 0);
2029 uint32_t other_idx
= idx
+ j
;
2031 if (other_idx
>= window
)
2032 other_idx
-= window
;
2033 m
= array
+ other_idx
;
2036 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
2040 best_base
= other_idx
;
2044 * If we decided to cache the delta data, then it is best
2045 * to compress it right away. First because we have to do
2046 * it anyway, and doing it here while we're threaded will
2047 * save a lot of time in the non threaded write phase,
2048 * as well as allow for caching more deltas within
2049 * the same cache size limit.
2051 * But only if not writing to stdout, since in that case
2052 * the network is most likely throttling writes anyway,
2053 * and therefore it is best to go to the write phase ASAP
2054 * instead, as we can afford spending more time compressing
2055 * between writes at that moment.
2057 if (entry
->delta_data
&& !pack_to_stdout
) {
2058 entry
->z_delta_size
= do_compress(&entry
->delta_data
,
2061 delta_cache_size
-= entry
->delta_size
;
2062 delta_cache_size
+= entry
->z_delta_size
;
2066 /* if we made n a delta, and if n is already at max
2067 * depth, leaving it in the window is pointless. we
2068 * should evict it first.
2070 if (entry
->delta
&& max_depth
<= n
->depth
)
2074 * Move the best delta base up in the window, after the
2075 * currently deltified object, to keep it longer. It will
2076 * be the first base object to be attempted next.
2079 struct unpacked swap
= array
[best_base
];
2080 int dist
= (window
+ idx
- best_base
) % window
;
2081 int dst
= best_base
;
2083 int src
= (dst
+ 1) % window
;
2084 array
[dst
] = array
[src
];
2092 if (count
+ 1 < window
)
2098 for (i
= 0; i
< window
; ++i
) {
2099 free_delta_index(array
[i
].index
);
2100 free(array
[i
].data
);
2107 static void try_to_free_from_threads(size_t size
)
2110 release_pack_memory(size
);
2114 static try_to_free_t old_try_to_free_routine
;
2117 * The main thread waits on the condition that (at least) one of the workers
2118 * has stopped working (which is indicated in the .working member of
2119 * struct thread_params).
2120 * When a work thread has completed its work, it sets .working to 0 and
2121 * signals the main thread and waits on the condition that .data_ready
2125 struct thread_params
{
2127 struct object_entry
**list
;
2134 pthread_mutex_t mutex
;
2135 pthread_cond_t cond
;
2136 unsigned *processed
;
2139 static pthread_cond_t progress_cond
;
2142 * Mutex and conditional variable can't be statically-initialized on Windows.
2144 static void init_threaded_search(void)
2146 init_recursive_mutex(&read_mutex
);
2147 pthread_mutex_init(&cache_mutex
, NULL
);
2148 pthread_mutex_init(&progress_mutex
, NULL
);
2149 pthread_cond_init(&progress_cond
, NULL
);
2150 old_try_to_free_routine
= set_try_to_free_routine(try_to_free_from_threads
);
2153 static void cleanup_threaded_search(void)
2155 set_try_to_free_routine(old_try_to_free_routine
);
2156 pthread_cond_destroy(&progress_cond
);
2157 pthread_mutex_destroy(&read_mutex
);
2158 pthread_mutex_destroy(&cache_mutex
);
2159 pthread_mutex_destroy(&progress_mutex
);
2162 static void *threaded_find_deltas(void *arg
)
2164 struct thread_params
*me
= arg
;
2166 while (me
->remaining
) {
2167 find_deltas(me
->list
, &me
->remaining
,
2168 me
->window
, me
->depth
, me
->processed
);
2172 pthread_cond_signal(&progress_cond
);
2176 * We must not set ->data_ready before we wait on the
2177 * condition because the main thread may have set it to 1
2178 * before we get here. In order to be sure that new
2179 * work is available if we see 1 in ->data_ready, it
2180 * was initialized to 0 before this thread was spawned
2181 * and we reset it to 0 right away.
2183 pthread_mutex_lock(&me
->mutex
);
2184 while (!me
->data_ready
)
2185 pthread_cond_wait(&me
->cond
, &me
->mutex
);
2187 pthread_mutex_unlock(&me
->mutex
);
2189 /* leave ->working 1 so that this doesn't get more work assigned */
2193 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
2194 int window
, int depth
, unsigned *processed
)
2196 struct thread_params
*p
;
2197 int i
, ret
, active_threads
= 0;
2199 init_threaded_search();
2201 if (delta_search_threads
<= 1) {
2202 find_deltas(list
, &list_size
, window
, depth
, processed
);
2203 cleanup_threaded_search();
2206 if (progress
> pack_to_stdout
)
2207 fprintf(stderr
, "Delta compression using up to %d threads.\n",
2208 delta_search_threads
);
2209 p
= xcalloc(delta_search_threads
, sizeof(*p
));
2211 /* Partition the work amongst work threads. */
2212 for (i
= 0; i
< delta_search_threads
; i
++) {
2213 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
2215 /* don't use too small segments or no deltas will be found */
2216 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
2219 p
[i
].window
= window
;
2221 p
[i
].processed
= processed
;
2223 p
[i
].data_ready
= 0;
2225 /* try to split chunks on "path" boundaries */
2226 while (sub_size
&& sub_size
< list_size
&&
2227 list
[sub_size
]->hash
&&
2228 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
2232 p
[i
].list_size
= sub_size
;
2233 p
[i
].remaining
= sub_size
;
2236 list_size
-= sub_size
;
2239 /* Start work threads. */
2240 for (i
= 0; i
< delta_search_threads
; i
++) {
2241 if (!p
[i
].list_size
)
2243 pthread_mutex_init(&p
[i
].mutex
, NULL
);
2244 pthread_cond_init(&p
[i
].cond
, NULL
);
2245 ret
= pthread_create(&p
[i
].thread
, NULL
,
2246 threaded_find_deltas
, &p
[i
]);
2248 die("unable to create thread: %s", strerror(ret
));
2253 * Now let's wait for work completion. Each time a thread is done
2254 * with its work, we steal half of the remaining work from the
2255 * thread with the largest number of unprocessed objects and give
2256 * it to that newly idle thread. This ensure good load balancing
2257 * until the remaining object list segments are simply too short
2258 * to be worth splitting anymore.
2260 while (active_threads
) {
2261 struct thread_params
*target
= NULL
;
2262 struct thread_params
*victim
= NULL
;
2263 unsigned sub_size
= 0;
2267 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
2272 pthread_cond_wait(&progress_cond
, &progress_mutex
);
2275 for (i
= 0; i
< delta_search_threads
; i
++)
2276 if (p
[i
].remaining
> 2*window
&&
2277 (!victim
|| victim
->remaining
< p
[i
].remaining
))
2280 sub_size
= victim
->remaining
/ 2;
2281 list
= victim
->list
+ victim
->list_size
- sub_size
;
2282 while (sub_size
&& list
[0]->hash
&&
2283 list
[0]->hash
== list
[-1]->hash
) {
2289 * It is possible for some "paths" to have
2290 * so many objects that no hash boundary
2291 * might be found. Let's just steal the
2292 * exact half in that case.
2294 sub_size
= victim
->remaining
/ 2;
2297 target
->list
= list
;
2298 victim
->list_size
-= sub_size
;
2299 victim
->remaining
-= sub_size
;
2301 target
->list_size
= sub_size
;
2302 target
->remaining
= sub_size
;
2303 target
->working
= 1;
2306 pthread_mutex_lock(&target
->mutex
);
2307 target
->data_ready
= 1;
2308 pthread_cond_signal(&target
->cond
);
2309 pthread_mutex_unlock(&target
->mutex
);
2312 pthread_join(target
->thread
, NULL
);
2313 pthread_cond_destroy(&target
->cond
);
2314 pthread_mutex_destroy(&target
->mutex
);
2318 cleanup_threaded_search();
2323 #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
2326 static void add_tag_chain(const struct object_id
*oid
)
2331 * We catch duplicates already in add_object_entry(), but we'd
2332 * prefer to do this extra check to avoid having to parse the
2333 * tag at all if we already know that it's being packed (e.g., if
2334 * it was included via bitmaps, we would not have parsed it
2337 if (packlist_find(&to_pack
, oid
->hash
, NULL
))
2340 tag
= lookup_tag(oid
->hash
);
2342 if (!tag
|| parse_tag(tag
) || !tag
->tagged
)
2343 die("unable to pack objects reachable from tag %s",
2346 add_object_entry(tag
->object
.oid
.hash
, OBJ_TAG
, NULL
, 0);
2348 if (tag
->tagged
->type
!= OBJ_TAG
)
2351 tag
= (struct tag
*)tag
->tagged
;
2355 static int add_ref_tag(const char *path
, const struct object_id
*oid
, int flag
, void *cb_data
)
2357 struct object_id peeled
;
2359 if (starts_with(path
, "refs/tags/") && /* is a tag? */
2360 !peel_ref(path
, peeled
.hash
) && /* peelable? */
2361 packlist_find(&to_pack
, peeled
.hash
, NULL
)) /* object packed? */
2366 static void prepare_pack(int window
, int depth
)
2368 struct object_entry
**delta_list
;
2369 uint32_t i
, nr_deltas
;
2372 get_object_details();
2375 * If we're locally repacking then we need to be doubly careful
2376 * from now on in order to make sure no stealth corruption gets
2377 * propagated to the new pack. Clients receiving streamed packs
2378 * should validate everything they get anyway so no need to incur
2379 * the additional cost here in that case.
2381 if (!pack_to_stdout
)
2382 do_check_packed_object_crc
= 1;
2384 if (!to_pack
.nr_objects
|| !window
|| !depth
)
2387 ALLOC_ARRAY(delta_list
, to_pack
.nr_objects
);
2390 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
2391 struct object_entry
*entry
= to_pack
.objects
+ i
;
2394 /* This happens if we decided to reuse existing
2395 * delta from a pack. "reuse_delta &&" is implied.
2399 if (entry
->size
< 50)
2402 if (entry
->no_try_delta
)
2405 if (!entry
->preferred_base
) {
2407 if (entry
->type
< 0)
2408 die("unable to get type of object %s",
2409 sha1_to_hex(entry
->idx
.sha1
));
2411 if (entry
->type
< 0) {
2413 * This object is not found, but we
2414 * don't have to include it anyway.
2420 delta_list
[n
++] = entry
;
2423 if (nr_deltas
&& n
> 1) {
2424 unsigned nr_done
= 0;
2426 progress_state
= start_progress(_("Compressing objects"),
2428 QSORT(delta_list
, n
, type_size_sort
);
2429 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
2430 stop_progress(&progress_state
);
2431 if (nr_done
!= nr_deltas
)
2432 die("inconsistency with delta count");
2437 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2439 if (!strcmp(k
, "pack.window")) {
2440 window
= git_config_int(k
, v
);
2443 if (!strcmp(k
, "pack.windowmemory")) {
2444 window_memory_limit
= git_config_ulong(k
, v
);
2447 if (!strcmp(k
, "pack.depth")) {
2448 depth
= git_config_int(k
, v
);
2451 if (!strcmp(k
, "pack.deltacachesize")) {
2452 max_delta_cache_size
= git_config_int(k
, v
);
2455 if (!strcmp(k
, "pack.deltacachelimit")) {
2456 cache_max_small_delta_size
= git_config_int(k
, v
);
2459 if (!strcmp(k
, "pack.writebitmaphashcache")) {
2460 if (git_config_bool(k
, v
))
2461 write_bitmap_options
|= BITMAP_OPT_HASH_CACHE
;
2463 write_bitmap_options
&= ~BITMAP_OPT_HASH_CACHE
;
2465 if (!strcmp(k
, "pack.usebitmaps")) {
2466 use_bitmap_index_default
= git_config_bool(k
, v
);
2469 if (!strcmp(k
, "pack.threads")) {
2470 delta_search_threads
= git_config_int(k
, v
);
2471 if (delta_search_threads
< 0)
2472 die("invalid number of threads specified (%d)",
2473 delta_search_threads
);
2475 if (delta_search_threads
!= 1)
2476 warning("no threads support, ignoring %s", k
);
2480 if (!strcmp(k
, "pack.indexversion")) {
2481 pack_idx_opts
.version
= git_config_int(k
, v
);
2482 if (pack_idx_opts
.version
> 2)
2483 die("bad pack.indexversion=%"PRIu32
,
2484 pack_idx_opts
.version
);
2487 return git_default_config(k
, v
, cb
);
2490 static void read_object_list_from_stdin(void)
2492 char line
[40 + 1 + PATH_MAX
+ 2];
2493 unsigned char sha1
[20];
2496 if (!fgets(line
, sizeof(line
), stdin
)) {
2500 die("fgets returned NULL, not EOF, not error!");
2506 if (line
[0] == '-') {
2507 if (get_sha1_hex(line
+1, sha1
))
2508 die("expected edge sha1, got garbage:\n %s",
2510 add_preferred_base(sha1
);
2513 if (get_sha1_hex(line
, sha1
))
2514 die("expected sha1, got garbage:\n %s", line
);
2516 add_preferred_base_object(line
+41);
2517 add_object_entry(sha1
, 0, line
+41, 0);
2521 #define OBJECT_ADDED (1u<<20)
2523 static void show_commit(struct commit
*commit
, void *data
)
2525 add_object_entry(commit
->object
.oid
.hash
, OBJ_COMMIT
, NULL
, 0);
2526 commit
->object
.flags
|= OBJECT_ADDED
;
2528 if (write_bitmap_index
)
2529 index_commit_for_bitmap(commit
);
2532 static void show_object(struct object
*obj
, const char *name
, void *data
)
2534 add_preferred_base_object(name
);
2535 add_object_entry(obj
->oid
.hash
, obj
->type
, name
, 0);
2536 obj
->flags
|= OBJECT_ADDED
;
2539 static void show_edge(struct commit
*commit
)
2541 add_preferred_base(commit
->object
.oid
.hash
);
2544 struct in_pack_object
{
2546 struct object
*object
;
2552 struct in_pack_object
*array
;
2555 static void mark_in_pack_object(struct object
*object
, struct packed_git
*p
, struct in_pack
*in_pack
)
2557 in_pack
->array
[in_pack
->nr
].offset
= find_pack_entry_one(object
->oid
.hash
, p
);
2558 in_pack
->array
[in_pack
->nr
].object
= object
;
2563 * Compare the objects in the offset order, in order to emulate the
2564 * "git rev-list --objects" output that produced the pack originally.
2566 static int ofscmp(const void *a_
, const void *b_
)
2568 struct in_pack_object
*a
= (struct in_pack_object
*)a_
;
2569 struct in_pack_object
*b
= (struct in_pack_object
*)b_
;
2571 if (a
->offset
< b
->offset
)
2573 else if (a
->offset
> b
->offset
)
2576 return oidcmp(&a
->object
->oid
, &b
->object
->oid
);
2579 static void add_objects_in_unpacked_packs(struct rev_info
*revs
)
2581 struct packed_git
*p
;
2582 struct in_pack in_pack
;
2585 memset(&in_pack
, 0, sizeof(in_pack
));
2587 for (p
= packed_git
; p
; p
= p
->next
) {
2588 const unsigned char *sha1
;
2591 if (!p
->pack_local
|| p
->pack_keep
)
2593 if (open_pack_index(p
))
2594 die("cannot open pack index");
2596 ALLOC_GROW(in_pack
.array
,
2597 in_pack
.nr
+ p
->num_objects
,
2600 for (i
= 0; i
< p
->num_objects
; i
++) {
2601 sha1
= nth_packed_object_sha1(p
, i
);
2602 o
= lookup_unknown_object(sha1
);
2603 if (!(o
->flags
& OBJECT_ADDED
))
2604 mark_in_pack_object(o
, p
, &in_pack
);
2605 o
->flags
|= OBJECT_ADDED
;
2610 QSORT(in_pack
.array
, in_pack
.nr
, ofscmp
);
2611 for (i
= 0; i
< in_pack
.nr
; i
++) {
2612 struct object
*o
= in_pack
.array
[i
].object
;
2613 add_object_entry(o
->oid
.hash
, o
->type
, "", 0);
2616 free(in_pack
.array
);
2619 static int add_loose_object(const struct object_id
*oid
, const char *path
,
2622 enum object_type type
= sha1_object_info(oid
->hash
, NULL
);
2625 warning("loose object at %s could not be examined", path
);
2629 add_object_entry(oid
->hash
, type
, "", 0);
2634 * We actually don't even have to worry about reachability here.
2635 * add_object_entry will weed out duplicates, so we just add every
2636 * loose object we find.
2638 static void add_unreachable_loose_objects(void)
2640 for_each_loose_file_in_objdir(get_object_directory(),
2645 static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1
)
2647 static struct packed_git
*last_found
= (void *)1;
2648 struct packed_git
*p
;
2650 p
= (last_found
!= (void *)1) ? last_found
: packed_git
;
2653 if ((!p
->pack_local
|| p
->pack_keep
) &&
2654 find_pack_entry_one(sha1
, p
)) {
2658 if (p
== last_found
)
2662 if (p
== last_found
)
2669 * Store a list of sha1s that are should not be discarded
2670 * because they are either written too recently, or are
2671 * reachable from another object that was.
2673 * This is filled by get_object_list.
2675 static struct oid_array recent_objects
;
2677 static int loosened_object_can_be_discarded(const struct object_id
*oid
,
2678 unsigned long mtime
)
2680 if (!unpack_unreachable_expiration
)
2682 if (mtime
> unpack_unreachable_expiration
)
2684 if (oid_array_lookup(&recent_objects
, oid
) >= 0)
2689 static void loosen_unused_packed_objects(struct rev_info
*revs
)
2691 struct packed_git
*p
;
2693 struct object_id oid
;
2695 for (p
= packed_git
; p
; p
= p
->next
) {
2696 if (!p
->pack_local
|| p
->pack_keep
)
2699 if (open_pack_index(p
))
2700 die("cannot open pack index");
2702 for (i
= 0; i
< p
->num_objects
; i
++) {
2703 nth_packed_object_oid(&oid
, p
, i
);
2704 if (!packlist_find(&to_pack
, oid
.hash
, NULL
) &&
2705 !has_sha1_pack_kept_or_nonlocal(oid
.hash
) &&
2706 !loosened_object_can_be_discarded(&oid
, p
->mtime
))
2707 if (force_object_loose(oid
.hash
, p
->mtime
))
2708 die("unable to force loose object");
2714 * This tracks any options which pack-reuse code expects to be on, or which a
2715 * reader of the pack might not understand, and which would therefore prevent
2716 * blind reuse of what we have on disk.
2718 static int pack_options_allow_reuse(void)
2720 return pack_to_stdout
&&
2722 !ignore_packed_keep
&&
2723 (!local
|| !have_non_local_packs
) &&
2727 static int get_object_list_from_bitmap(struct rev_info
*revs
)
2729 if (prepare_bitmap_walk(revs
) < 0)
2732 if (pack_options_allow_reuse() &&
2733 !reuse_partial_packfile_from_bitmap(
2735 &reuse_packfile_objects
,
2736 &reuse_packfile_offset
)) {
2737 assert(reuse_packfile_objects
);
2738 nr_result
+= reuse_packfile_objects
;
2739 display_progress(progress_state
, nr_result
);
2742 traverse_bitmap_commit_list(&add_object_entry_from_bitmap
);
2746 static void record_recent_object(struct object
*obj
,
2750 oid_array_append(&recent_objects
, &obj
->oid
);
2753 static void record_recent_commit(struct commit
*commit
, void *data
)
2755 oid_array_append(&recent_objects
, &commit
->object
.oid
);
2758 static void get_object_list(int ac
, const char **av
)
2760 struct rev_info revs
;
2764 init_revisions(&revs
, NULL
);
2765 save_commit_buffer
= 0;
2766 setup_revisions(ac
, av
, &revs
, NULL
);
2768 /* make sure shallows are read */
2769 is_repository_shallow();
2771 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
2772 int len
= strlen(line
);
2773 if (len
&& line
[len
- 1] == '\n')
2778 if (!strcmp(line
, "--not")) {
2779 flags
^= UNINTERESTING
;
2780 write_bitmap_index
= 0;
2783 if (starts_with(line
, "--shallow ")) {
2784 unsigned char sha1
[20];
2785 if (get_sha1_hex(line
+ 10, sha1
))
2786 die("not an SHA-1 '%s'", line
+ 10);
2787 register_shallow(sha1
);
2788 use_bitmap_index
= 0;
2791 die("not a rev '%s'", line
);
2793 if (handle_revision_arg(line
, &revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
2794 die("bad revision '%s'", line
);
2797 if (use_bitmap_index
&& !get_object_list_from_bitmap(&revs
))
2800 if (prepare_revision_walk(&revs
))
2801 die("revision walk setup failed");
2802 mark_edges_uninteresting(&revs
, show_edge
);
2803 traverse_commit_list(&revs
, show_commit
, show_object
, NULL
);
2805 if (unpack_unreachable_expiration
) {
2806 revs
.ignore_missing_links
= 1;
2807 if (add_unseen_recent_objects_to_traversal(&revs
,
2808 unpack_unreachable_expiration
))
2809 die("unable to add recent objects");
2810 if (prepare_revision_walk(&revs
))
2811 die("revision walk setup failed");
2812 traverse_commit_list(&revs
, record_recent_commit
,
2813 record_recent_object
, NULL
);
2816 if (keep_unreachable
)
2817 add_objects_in_unpacked_packs(&revs
);
2818 if (pack_loose_unreachable
)
2819 add_unreachable_loose_objects();
2820 if (unpack_unreachable
)
2821 loosen_unused_packed_objects(&revs
);
2823 oid_array_clear(&recent_objects
);
2826 static int option_parse_index_version(const struct option
*opt
,
2827 const char *arg
, int unset
)
2830 const char *val
= arg
;
2831 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
2832 if (pack_idx_opts
.version
> 2)
2833 die(_("unsupported index version %s"), val
);
2834 if (*c
== ',' && c
[1])
2835 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
2836 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
2837 die(_("bad index version '%s'"), val
);
2841 static int option_parse_unpack_unreachable(const struct option
*opt
,
2842 const char *arg
, int unset
)
2845 unpack_unreachable
= 0;
2846 unpack_unreachable_expiration
= 0;
2849 unpack_unreachable
= 1;
2851 unpack_unreachable_expiration
= approxidate(arg
);
2856 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
2858 int use_internal_rev_list
= 0;
2861 int all_progress_implied
= 0;
2862 struct argv_array rp
= ARGV_ARRAY_INIT
;
2863 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
2864 int rev_list_index
= 0;
2865 struct option pack_objects_options
[] = {
2866 OPT_SET_INT('q', "quiet", &progress
,
2867 N_("do not show progress meter"), 0),
2868 OPT_SET_INT(0, "progress", &progress
,
2869 N_("show progress meter"), 1),
2870 OPT_SET_INT(0, "all-progress", &progress
,
2871 N_("show progress meter during object writing phase"), 2),
2872 OPT_BOOL(0, "all-progress-implied",
2873 &all_progress_implied
,
2874 N_("similar to --all-progress when progress meter is shown")),
2875 { OPTION_CALLBACK
, 0, "index-version", NULL
, N_("version[,offset]"),
2876 N_("write the pack index file in the specified idx format version"),
2877 0, option_parse_index_version
},
2878 OPT_MAGNITUDE(0, "max-pack-size", &pack_size_limit
,
2879 N_("maximum size of each output pack file")),
2880 OPT_BOOL(0, "local", &local
,
2881 N_("ignore borrowed objects from alternate object store")),
2882 OPT_BOOL(0, "incremental", &incremental
,
2883 N_("ignore packed objects")),
2884 OPT_INTEGER(0, "window", &window
,
2885 N_("limit pack window by objects")),
2886 OPT_MAGNITUDE(0, "window-memory", &window_memory_limit
,
2887 N_("limit pack window by memory in addition to object limit")),
2888 OPT_INTEGER(0, "depth", &depth
,
2889 N_("maximum length of delta chain allowed in the resulting pack")),
2890 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
2891 N_("reuse existing deltas")),
2892 OPT_BOOL(0, "reuse-object", &reuse_object
,
2893 N_("reuse existing objects")),
2894 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
2895 N_("use OFS_DELTA objects")),
2896 OPT_INTEGER(0, "threads", &delta_search_threads
,
2897 N_("use threads when searching for best delta matches")),
2898 OPT_BOOL(0, "non-empty", &non_empty
,
2899 N_("do not create an empty pack output")),
2900 OPT_BOOL(0, "revs", &use_internal_rev_list
,
2901 N_("read revision arguments from standard input")),
2902 { OPTION_SET_INT
, 0, "unpacked", &rev_list_unpacked
, NULL
,
2903 N_("limit the objects to those that are not yet packed"),
2904 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2905 { OPTION_SET_INT
, 0, "all", &rev_list_all
, NULL
,
2906 N_("include objects reachable from any reference"),
2907 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2908 { OPTION_SET_INT
, 0, "reflog", &rev_list_reflog
, NULL
,
2909 N_("include objects referred by reflog entries"),
2910 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2911 { OPTION_SET_INT
, 0, "indexed-objects", &rev_list_index
, NULL
,
2912 N_("include objects referred to by the index"),
2913 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2914 OPT_BOOL(0, "stdout", &pack_to_stdout
,
2915 N_("output pack to stdout")),
2916 OPT_BOOL(0, "include-tag", &include_tag
,
2917 N_("include tag objects that refer to objects to be packed")),
2918 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
2919 N_("keep unreachable objects")),
2920 OPT_BOOL(0, "pack-loose-unreachable", &pack_loose_unreachable
,
2921 N_("pack loose unreachable objects")),
2922 { OPTION_CALLBACK
, 0, "unpack-unreachable", NULL
, N_("time"),
2923 N_("unpack unreachable objects newer than <time>"),
2924 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
},
2925 OPT_BOOL(0, "thin", &thin
,
2926 N_("create thin packs")),
2927 OPT_BOOL(0, "shallow", &shallow
,
2928 N_("create packs suitable for shallow fetches")),
2929 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep
,
2930 N_("ignore packs that have companion .keep file")),
2931 OPT_INTEGER(0, "compression", &pack_compression_level
,
2932 N_("pack compression level")),
2933 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents
,
2934 N_("do not hide commits by grafts"), 0),
2935 OPT_BOOL(0, "use-bitmap-index", &use_bitmap_index
,
2936 N_("use a bitmap index if available to speed up counting objects")),
2937 OPT_BOOL(0, "write-bitmap-index", &write_bitmap_index
,
2938 N_("write a bitmap index together with the pack index")),
2942 check_replace_refs
= 0;
2944 reset_pack_idx_option(&pack_idx_opts
);
2945 git_config(git_pack_config
, NULL
);
2947 progress
= isatty(2);
2948 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
2952 base_name
= argv
[0];
2955 if (pack_to_stdout
!= !base_name
|| argc
)
2956 usage_with_options(pack_usage
, pack_objects_options
);
2958 argv_array_push(&rp
, "pack-objects");
2960 use_internal_rev_list
= 1;
2961 argv_array_push(&rp
, shallow
2962 ? "--objects-edge-aggressive"
2963 : "--objects-edge");
2965 argv_array_push(&rp
, "--objects");
2968 use_internal_rev_list
= 1;
2969 argv_array_push(&rp
, "--all");
2971 if (rev_list_reflog
) {
2972 use_internal_rev_list
= 1;
2973 argv_array_push(&rp
, "--reflog");
2975 if (rev_list_index
) {
2976 use_internal_rev_list
= 1;
2977 argv_array_push(&rp
, "--indexed-objects");
2979 if (rev_list_unpacked
) {
2980 use_internal_rev_list
= 1;
2981 argv_array_push(&rp
, "--unpacked");
2986 if (pack_compression_level
== -1)
2987 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
2988 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
2989 die("bad pack compression level %d", pack_compression_level
);
2991 if (!delta_search_threads
) /* --threads=0 means autodetect */
2992 delta_search_threads
= online_cpus();
2995 if (delta_search_threads
!= 1)
2996 warning("no threads support, ignoring --threads");
2998 if (!pack_to_stdout
&& !pack_size_limit
)
2999 pack_size_limit
= pack_size_limit_cfg
;
3000 if (pack_to_stdout
&& pack_size_limit
)
3001 die("--max-pack-size cannot be used to build a pack for transfer.");
3002 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
3003 warning("minimum pack size limit is 1 MiB");
3004 pack_size_limit
= 1024*1024;
3007 if (!pack_to_stdout
&& thin
)
3008 die("--thin cannot be used to build an indexable pack.");
3010 if (keep_unreachable
&& unpack_unreachable
)
3011 die("--keep-unreachable and --unpack-unreachable are incompatible.");
3012 if (!rev_list_all
|| !rev_list_reflog
|| !rev_list_index
)
3013 unpack_unreachable_expiration
= 0;
3016 * "soft" reasons not to use bitmaps - for on-disk repack by default we want
3018 * - to produce good pack (with bitmap index not-yet-packed objects are
3019 * packed in suboptimal order).
3021 * - to use more robust pack-generation codepath (avoiding possible
3022 * bugs in bitmap code and possible bitmap index corruption).
3024 if (!pack_to_stdout
)
3025 use_bitmap_index_default
= 0;
3027 if (use_bitmap_index
< 0)
3028 use_bitmap_index
= use_bitmap_index_default
;
3030 /* "hard" reasons not to use bitmaps; these just won't work at all */
3031 if (!use_internal_rev_list
|| (!pack_to_stdout
&& write_bitmap_index
) || is_repository_shallow())
3032 use_bitmap_index
= 0;
3034 if (pack_to_stdout
|| !rev_list_all
)
3035 write_bitmap_index
= 0;
3037 if (progress
&& all_progress_implied
)
3040 prepare_packed_git();
3041 if (ignore_packed_keep
) {
3042 struct packed_git
*p
;
3043 for (p
= packed_git
; p
; p
= p
->next
)
3044 if (p
->pack_local
&& p
->pack_keep
)
3046 if (!p
) /* no keep-able packs found */
3047 ignore_packed_keep
= 0;
3051 * unlike ignore_packed_keep above, we do not want to
3052 * unset "local" based on looking at packs, as it
3053 * also covers non-local objects
3055 struct packed_git
*p
;
3056 for (p
= packed_git
; p
; p
= p
->next
) {
3057 if (!p
->pack_local
) {
3058 have_non_local_packs
= 1;
3065 progress_state
= start_progress(_("Counting objects"), 0);
3066 if (!use_internal_rev_list
)
3067 read_object_list_from_stdin();
3069 get_object_list(rp
.argc
, rp
.argv
);
3070 argv_array_clear(&rp
);
3072 cleanup_preferred_base();
3073 if (include_tag
&& nr_result
)
3074 for_each_ref(add_ref_tag
, NULL
);
3075 stop_progress(&progress_state
);
3077 if (non_empty
&& !nr_result
)
3080 prepare_pack(window
, depth
);
3083 fprintf(stderr
, "Total %"PRIu32
" (delta %"PRIu32
"),"
3084 " reused %"PRIu32
" (delta %"PRIu32
")\n",
3085 written
, written_delta
, reused
, reused_delta
);