11 #include "pack-revindex.h"
12 #include "csum-file.h"
13 #include "tree-walk.h"
16 #include "list-objects.h"
19 #include "thread-utils.h"
21 static const char *pack_usage
[] = {
22 "git pack-objects --stdout [options...] [< ref-list | < object-list]",
23 "git pack-objects [options...] base-name [< ref-list | < object-list]",
28 struct pack_idx_entry idx
;
29 unsigned long size
; /* uncompressed size */
30 struct packed_git
*in_pack
; /* already in pack */
32 struct object_entry
*delta
; /* delta base object */
33 struct object_entry
*delta_child
; /* deltified objects who bases me */
34 struct object_entry
*delta_sibling
; /* other deltified objects who
35 * uses the same base as me
37 void *delta_data
; /* cached delta (uncompressed) */
38 unsigned long delta_size
; /* delta data size (uncompressed) */
39 unsigned long z_delta_size
; /* delta data size (compressed) */
40 unsigned int hash
; /* name hint hash */
41 enum object_type type
;
42 enum object_type in_pack_type
; /* could be delta */
43 unsigned char in_pack_header_size
;
44 unsigned char preferred_base
; /* we do not pack this, but is available
45 * to be used as the base object to delta
48 unsigned char no_try_delta
;
49 unsigned char tagged
; /* near the very tip of refs */
50 unsigned char filled
; /* assigned write-order */
54 * Objects we are going to pack are collected in objects array (dynamically
55 * expanded). nr_objects & nr_alloc controls this array. They are stored
56 * in the order we see -- typically rev-list --objects order that gives us
57 * nice "minimum seek" order.
59 static struct object_entry
*objects
;
60 static struct pack_idx_entry
**written_list
;
61 static uint32_t nr_objects
, nr_alloc
, nr_result
, nr_written
;
64 static int reuse_delta
= 1, reuse_object
= 1;
65 static int keep_unreachable
, unpack_unreachable
, include_tag
;
66 static unsigned long unpack_unreachable_expiration
;
68 static int incremental
;
69 static int ignore_packed_keep
;
70 static int allow_ofs_delta
;
71 static struct pack_idx_option pack_idx_opts
;
72 static const char *base_name
;
73 static int progress
= 1;
74 static int window
= 10;
75 static unsigned long pack_size_limit
;
76 static int depth
= 50;
77 static int delta_search_threads
;
78 static int pack_to_stdout
;
79 static int num_preferred_base
;
80 static struct progress
*progress_state
;
81 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
82 static int pack_compression_seen
;
84 static unsigned long delta_cache_size
= 0;
85 static unsigned long max_delta_cache_size
= 256 * 1024 * 1024;
86 static unsigned long cache_max_small_delta_size
= 1000;
88 static unsigned long window_memory_limit
= 0;
91 * The object names in objects array are hashed with this hashtable,
92 * to help looking up the entry by object name.
93 * This hashtable is built after all the objects are seen.
95 static int *object_ix
;
96 static int object_ix_hashsz
;
97 static struct object_entry
*locate_object_entry(const unsigned char *sha1
);
102 static uint32_t written
, written_delta
;
103 static uint32_t reused
, reused_delta
;
106 static void *get_delta(struct object_entry
*entry
)
108 unsigned long size
, base_size
, delta_size
;
109 void *buf
, *base_buf
, *delta_buf
;
110 enum object_type type
;
112 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
114 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
115 base_buf
= read_sha1_file(entry
->delta
->idx
.sha1
, &type
, &base_size
);
117 die("unable to read %s", sha1_to_hex(entry
->delta
->idx
.sha1
));
118 delta_buf
= diff_delta(base_buf
, base_size
,
119 buf
, size
, &delta_size
, 0);
120 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
121 die("delta size changed");
127 static unsigned long do_compress(void **pptr
, unsigned long size
)
131 unsigned long maxsize
;
133 memset(&stream
, 0, sizeof(stream
));
134 git_deflate_init(&stream
, pack_compression_level
);
135 maxsize
= git_deflate_bound(&stream
, size
);
138 out
= xmalloc(maxsize
);
142 stream
.avail_in
= size
;
143 stream
.next_out
= out
;
144 stream
.avail_out
= maxsize
;
145 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
147 git_deflate_end(&stream
);
150 return stream
.total_out
;
154 * we are going to reuse the existing object data as is. make
155 * sure it is not corrupt.
157 static int check_pack_inflate(struct packed_git
*p
,
158 struct pack_window
**w_curs
,
161 unsigned long expect
)
164 unsigned char fakebuf
[4096], *in
;
167 memset(&stream
, 0, sizeof(stream
));
168 git_inflate_init(&stream
);
170 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
172 stream
.next_out
= fakebuf
;
173 stream
.avail_out
= sizeof(fakebuf
);
174 st
= git_inflate(&stream
, Z_FINISH
);
175 offset
+= stream
.next_in
- in
;
176 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
177 git_inflate_end(&stream
);
178 return (st
== Z_STREAM_END
&&
179 stream
.total_out
== expect
&&
180 stream
.total_in
== len
) ? 0 : -1;
183 static void copy_pack_data(struct sha1file
*f
,
184 struct packed_git
*p
,
185 struct pack_window
**w_curs
,
193 in
= use_pack(p
, w_curs
, offset
, &avail
);
195 avail
= (unsigned long)len
;
196 sha1write(f
, in
, avail
);
202 /* Return 0 if we will bust the pack-size limit */
203 static unsigned long write_object(struct sha1file
*f
,
204 struct object_entry
*entry
,
207 unsigned long size
, limit
, datalen
;
209 unsigned char header
[10], dheader
[10];
211 enum object_type type
;
212 int usable_delta
, to_reuse
;
219 /* apply size limit if limited packsize and not first object */
220 if (!pack_size_limit
|| !nr_written
)
222 else if (pack_size_limit
<= write_offset
)
224 * the earlier object did not fit the limit; avoid
225 * mistaking this with unlimited (i.e. limit = 0).
229 limit
= pack_size_limit
- write_offset
;
232 usable_delta
= 0; /* no delta */
233 else if (!pack_size_limit
)
234 usable_delta
= 1; /* unlimited packfile */
235 else if (entry
->delta
->idx
.offset
== (off_t
)-1)
236 usable_delta
= 0; /* base was written to another pack */
237 else if (entry
->delta
->idx
.offset
)
238 usable_delta
= 1; /* base already exists in this pack */
240 usable_delta
= 0; /* base could end up in another pack */
243 to_reuse
= 0; /* explicit */
244 else if (!entry
->in_pack
)
245 to_reuse
= 0; /* can't reuse what we don't have */
246 else if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
)
247 /* check_object() decided it for us ... */
248 to_reuse
= usable_delta
;
249 /* ... but pack split may override that */
250 else if (type
!= entry
->in_pack_type
)
251 to_reuse
= 0; /* pack has delta which is unusable */
252 else if (entry
->delta
)
253 to_reuse
= 0; /* we want to pack afresh */
255 to_reuse
= 1; /* we have it in-pack undeltified,
256 * and we do not need to deltify it.
262 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
264 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
266 * make sure no cached delta data remains from a
267 * previous attempt before a pack split occurred.
269 free(entry
->delta_data
);
270 entry
->delta_data
= NULL
;
271 entry
->z_delta_size
= 0;
272 } else if (entry
->delta_data
) {
273 size
= entry
->delta_size
;
274 buf
= entry
->delta_data
;
275 entry
->delta_data
= NULL
;
276 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
277 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
279 buf
= get_delta(entry
);
280 size
= entry
->delta_size
;
281 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
282 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
285 if (entry
->z_delta_size
)
286 datalen
= entry
->z_delta_size
;
288 datalen
= do_compress(&buf
, size
);
291 * The object header is a byte of 'type' followed by zero or
292 * more bytes of length.
294 hdrlen
= encode_in_pack_object_header(type
, size
, header
);
296 if (type
== OBJ_OFS_DELTA
) {
298 * Deltas with relative base contain an additional
299 * encoding of the relative offset for the delta
300 * base from this object's position in the pack.
302 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
303 unsigned pos
= sizeof(dheader
) - 1;
304 dheader
[pos
] = ofs
& 127;
306 dheader
[--pos
] = 128 | (--ofs
& 127);
307 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
311 sha1write(f
, header
, hdrlen
);
312 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
313 hdrlen
+= sizeof(dheader
) - pos
;
314 } else if (type
== OBJ_REF_DELTA
) {
316 * Deltas with a base reference contain
317 * an additional 20 bytes for the base sha1.
319 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
323 sha1write(f
, header
, hdrlen
);
324 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
327 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
331 sha1write(f
, header
, hdrlen
);
333 sha1write(f
, buf
, datalen
);
337 struct packed_git
*p
= entry
->in_pack
;
338 struct pack_window
*w_curs
= NULL
;
339 struct revindex_entry
*revidx
;
343 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
344 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
345 hdrlen
= encode_in_pack_object_header(type
, entry
->size
, header
);
347 offset
= entry
->in_pack_offset
;
348 revidx
= find_pack_revindex(p
, offset
);
349 datalen
= revidx
[1].offset
- offset
;
350 if (!pack_to_stdout
&& p
->index_version
> 1 &&
351 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
)) {
352 error("bad packed object CRC for %s", sha1_to_hex(entry
->idx
.sha1
));
357 offset
+= entry
->in_pack_header_size
;
358 datalen
-= entry
->in_pack_header_size
;
359 if (!pack_to_stdout
&& p
->index_version
== 1 &&
360 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
)) {
361 error("corrupt packed object for %s", sha1_to_hex(entry
->idx
.sha1
));
366 if (type
== OBJ_OFS_DELTA
) {
367 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
368 unsigned pos
= sizeof(dheader
) - 1;
369 dheader
[pos
] = ofs
& 127;
371 dheader
[--pos
] = 128 | (--ofs
& 127);
372 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
376 sha1write(f
, header
, hdrlen
);
377 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
378 hdrlen
+= sizeof(dheader
) - pos
;
380 } else if (type
== OBJ_REF_DELTA
) {
381 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
385 sha1write(f
, header
, hdrlen
);
386 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
390 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
394 sha1write(f
, header
, hdrlen
);
396 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
404 entry
->idx
.crc32
= crc32_end(f
);
405 return hdrlen
+ datalen
;
408 enum write_one_status
{
409 WRITE_ONE_SKIP
= -1, /* already written */
410 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
411 WRITE_ONE_WRITTEN
= 1, /* normal */
412 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
415 static enum write_one_status
write_one(struct sha1file
*f
,
416 struct object_entry
*e
,
423 * we set offset to 1 (which is an impossible value) to mark
424 * the fact that this object is involved in "write its base
425 * first before writing a deltified object" recursion.
427 recursing
= (e
->idx
.offset
== 1);
429 warning("recursive delta detected for object %s",
430 sha1_to_hex(e
->idx
.sha1
));
431 return WRITE_ONE_RECURSIVE
;
432 } else if (e
->idx
.offset
|| e
->preferred_base
) {
433 /* offset is non zero if object is written already. */
434 return WRITE_ONE_SKIP
;
437 /* if we are deltified, write out base object first. */
439 e
->idx
.offset
= 1; /* now recurse */
440 switch (write_one(f
, e
->delta
, offset
)) {
441 case WRITE_ONE_RECURSIVE
:
442 /* we cannot depend on this one */
447 case WRITE_ONE_BREAK
:
448 e
->idx
.offset
= recursing
;
449 return WRITE_ONE_BREAK
;
453 e
->idx
.offset
= *offset
;
454 size
= write_object(f
, e
, *offset
);
456 e
->idx
.offset
= recursing
;
457 return WRITE_ONE_BREAK
;
459 written_list
[nr_written
++] = &e
->idx
;
461 /* make sure off_t is sufficiently large not to wrap */
462 if (signed_add_overflows(*offset
, size
))
463 die("pack too large for current definition of off_t");
465 return WRITE_ONE_WRITTEN
;
468 static int mark_tagged(const char *path
, const unsigned char *sha1
, int flag
,
471 unsigned char peeled
[20];
472 struct object_entry
*entry
= locate_object_entry(sha1
);
476 if (!peel_ref(path
, peeled
)) {
477 entry
= locate_object_entry(peeled
);
484 static inline void add_to_write_order(struct object_entry
**wo
,
486 struct object_entry
*e
)
494 static void add_descendants_to_write_order(struct object_entry
**wo
,
496 struct object_entry
*e
)
498 int add_to_order
= 1;
501 struct object_entry
*s
;
502 /* add this node... */
503 add_to_write_order(wo
, endp
, e
);
504 /* all its siblings... */
505 for (s
= e
->delta_sibling
; s
; s
= s
->delta_sibling
) {
506 add_to_write_order(wo
, endp
, s
);
509 /* drop down a level to add left subtree nodes if possible */
510 if (e
->delta_child
) {
515 /* our sibling might have some children, it is next */
516 if (e
->delta_sibling
) {
517 e
= e
->delta_sibling
;
520 /* go back to our parent node */
522 while (e
&& !e
->delta_sibling
) {
523 /* we're on the right side of a subtree, keep
524 * going up until we can go right again */
528 /* done- we hit our original root node */
531 /* pass it off to sibling at this level */
532 e
= e
->delta_sibling
;
537 static void add_family_to_write_order(struct object_entry
**wo
,
539 struct object_entry
*e
)
541 struct object_entry
*root
;
543 for (root
= e
; root
->delta
; root
= root
->delta
)
545 add_descendants_to_write_order(wo
, endp
, root
);
548 static struct object_entry
**compute_write_order(void)
550 unsigned int i
, wo_end
, last_untagged
;
552 struct object_entry
**wo
= xmalloc(nr_objects
* sizeof(*wo
));
554 for (i
= 0; i
< nr_objects
; i
++) {
555 objects
[i
].tagged
= 0;
556 objects
[i
].filled
= 0;
557 objects
[i
].delta_child
= NULL
;
558 objects
[i
].delta_sibling
= NULL
;
562 * Fully connect delta_child/delta_sibling network.
563 * Make sure delta_sibling is sorted in the original
566 for (i
= nr_objects
; i
> 0;) {
567 struct object_entry
*e
= &objects
[--i
];
570 /* Mark me as the first child */
571 e
->delta_sibling
= e
->delta
->delta_child
;
572 e
->delta
->delta_child
= e
;
576 * Mark objects that are at the tip of tags.
578 for_each_tag_ref(mark_tagged
, NULL
);
581 * Give the objects in the original recency order until
582 * we see a tagged tip.
584 for (i
= wo_end
= 0; i
< nr_objects
; i
++) {
585 if (objects
[i
].tagged
)
587 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
592 * Then fill all the tagged tips.
594 for (; i
< nr_objects
; i
++) {
595 if (objects
[i
].tagged
)
596 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
600 * And then all remaining commits and tags.
602 for (i
= last_untagged
; i
< nr_objects
; i
++) {
603 if (objects
[i
].type
!= OBJ_COMMIT
&&
604 objects
[i
].type
!= OBJ_TAG
)
606 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
610 * And then all the trees.
612 for (i
= last_untagged
; i
< nr_objects
; i
++) {
613 if (objects
[i
].type
!= OBJ_TREE
)
615 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
619 * Finally all the rest in really tight order
621 for (i
= last_untagged
; i
< nr_objects
; i
++) {
622 if (!objects
[i
].filled
)
623 add_family_to_write_order(wo
, &wo_end
, &objects
[i
]);
626 if (wo_end
!= nr_objects
)
627 die("ordered %u objects, expected %"PRIu32
, wo_end
, nr_objects
);
632 static void write_pack_file(void)
637 uint32_t nr_remaining
= nr_result
;
638 time_t last_mtime
= 0;
639 struct object_entry
**write_order
;
641 if (progress
> pack_to_stdout
)
642 progress_state
= start_progress("Writing objects", nr_result
);
643 written_list
= xmalloc(nr_objects
* sizeof(*written_list
));
644 write_order
= compute_write_order();
647 unsigned char sha1
[20];
648 char *pack_tmp_name
= NULL
;
651 f
= sha1fd_throughput(1, "<stdout>", progress_state
);
653 f
= create_tmp_packfile(&pack_tmp_name
);
655 offset
= write_pack_header(f
, nr_remaining
);
657 die_errno("unable to write pack header");
659 for (; i
< nr_objects
; i
++) {
660 struct object_entry
*e
= write_order
[i
];
661 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
663 display_progress(progress_state
, written
);
667 * Did we write the wrong # entries in the header?
668 * If so, rewrite it like in fast-import
670 if (pack_to_stdout
) {
671 sha1close(f
, sha1
, CSUM_CLOSE
);
672 } else if (nr_written
== nr_remaining
) {
673 sha1close(f
, sha1
, CSUM_FSYNC
);
675 int fd
= sha1close(f
, sha1
, 0);
676 fixup_pack_header_footer(fd
, sha1
, pack_tmp_name
,
677 nr_written
, sha1
, offset
);
681 if (!pack_to_stdout
) {
683 char tmpname
[PATH_MAX
];
686 * Packs are runtime accessed in their mtime
687 * order since newer packs are more likely to contain
688 * younger objects. So if we are creating multiple
689 * packs then we should modify the mtime of later ones
690 * to preserve this property.
692 if (stat(pack_tmp_name
, &st
) < 0) {
693 warning("failed to stat %s: %s",
694 pack_tmp_name
, strerror(errno
));
695 } else if (!last_mtime
) {
696 last_mtime
= st
.st_mtime
;
699 utb
.actime
= st
.st_atime
;
700 utb
.modtime
= --last_mtime
;
701 if (utime(pack_tmp_name
, &utb
) < 0)
702 warning("failed utime() on %s: %s",
703 tmpname
, strerror(errno
));
706 /* Enough space for "-<sha-1>.pack"? */
707 if (sizeof(tmpname
) <= strlen(base_name
) + 50)
708 die("pack base name '%s' too long", base_name
);
709 snprintf(tmpname
, sizeof(tmpname
), "%s-", base_name
);
710 finish_tmp_packfile(tmpname
, pack_tmp_name
,
711 written_list
, nr_written
,
712 &pack_idx_opts
, sha1
);
714 puts(sha1_to_hex(sha1
));
717 /* mark written objects as written to previous pack */
718 for (j
= 0; j
< nr_written
; j
++) {
719 written_list
[j
]->offset
= (off_t
)-1;
721 nr_remaining
-= nr_written
;
722 } while (nr_remaining
&& i
< nr_objects
);
726 stop_progress(&progress_state
);
727 if (written
!= nr_result
)
728 die("wrote %"PRIu32
" objects while expecting %"PRIu32
,
732 static int locate_object_entry_hash(const unsigned char *sha1
)
736 memcpy(&ui
, sha1
, sizeof(unsigned int));
737 i
= ui
% object_ix_hashsz
;
738 while (0 < object_ix
[i
]) {
739 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].idx
.sha1
))
741 if (++i
== object_ix_hashsz
)
747 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
751 if (!object_ix_hashsz
)
754 i
= locate_object_entry_hash(sha1
);
756 return &objects
[object_ix
[i
]-1];
760 static void rehash_objects(void)
763 struct object_entry
*oe
;
765 object_ix_hashsz
= nr_objects
* 3;
766 if (object_ix_hashsz
< 1024)
767 object_ix_hashsz
= 1024;
768 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
769 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
770 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
771 int ix
= locate_object_entry_hash(oe
->idx
.sha1
);
775 object_ix
[ix
] = i
+ 1;
779 static unsigned name_hash(const char *name
)
781 unsigned c
, hash
= 0;
787 * This effectively just creates a sortable number from the
788 * last sixteen non-whitespace characters. Last characters
789 * count "most", so things that end in ".c" sort together.
791 while ((c
= *name
++) != 0) {
794 hash
= (hash
>> 2) + (c
<< 24);
799 static void setup_delta_attr_check(struct git_attr_check
*check
)
801 static struct git_attr
*attr_delta
;
804 attr_delta
= git_attr("delta");
806 check
[0].attr
= attr_delta
;
809 static int no_try_delta(const char *path
)
811 struct git_attr_check check
[1];
813 setup_delta_attr_check(check
);
814 if (git_check_attr(path
, ARRAY_SIZE(check
), check
))
816 if (ATTR_FALSE(check
->value
))
821 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
822 const char *name
, int exclude
)
824 struct object_entry
*entry
;
825 struct packed_git
*p
, *found_pack
= NULL
;
826 off_t found_offset
= 0;
828 unsigned hash
= name_hash(name
);
830 ix
= nr_objects
? locate_object_entry_hash(sha1
) : -1;
833 entry
= objects
+ object_ix
[ix
] - 1;
834 if (!entry
->preferred_base
)
836 entry
->preferred_base
= 1;
841 if (!exclude
&& local
&& has_loose_object_nonlocal(sha1
))
844 for (p
= packed_git
; p
; p
= p
->next
) {
845 off_t offset
= find_pack_entry_one(sha1
, p
);
848 if (!is_pack_valid(p
)) {
849 warning("packfile %s cannot be accessed", p
->pack_name
);
852 found_offset
= offset
;
859 if (local
&& !p
->pack_local
)
861 if (ignore_packed_keep
&& p
->pack_local
&& p
->pack_keep
)
866 if (nr_objects
>= nr_alloc
) {
867 nr_alloc
= (nr_alloc
+ 1024) * 3 / 2;
868 objects
= xrealloc(objects
, nr_alloc
* sizeof(*entry
));
871 entry
= objects
+ nr_objects
++;
872 memset(entry
, 0, sizeof(*entry
));
873 hashcpy(entry
->idx
.sha1
, sha1
);
878 entry
->preferred_base
= 1;
882 entry
->in_pack
= found_pack
;
883 entry
->in_pack_offset
= found_offset
;
886 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
889 object_ix
[-1 - ix
] = nr_objects
;
891 display_progress(progress_state
, nr_objects
);
893 if (name
&& no_try_delta(name
))
894 entry
->no_try_delta
= 1;
899 struct pbase_tree_cache
{
900 unsigned char sha1
[20];
904 unsigned long tree_size
;
907 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
908 static int pbase_tree_cache_ix(const unsigned char *sha1
)
910 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
912 static int pbase_tree_cache_ix_incr(int ix
)
914 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
917 static struct pbase_tree
{
918 struct pbase_tree
*next
;
919 /* This is a phony "cache" entry; we are not
920 * going to evict it nor find it through _get()
921 * mechanism -- this is for the toplevel node that
922 * would almost always change with any commit.
924 struct pbase_tree_cache pcache
;
927 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
929 struct pbase_tree_cache
*ent
, *nent
;
932 enum object_type type
;
934 int my_ix
= pbase_tree_cache_ix(sha1
);
935 int available_ix
= -1;
937 /* pbase-tree-cache acts as a limited hashtable.
938 * your object will be found at your index or within a few
939 * slots after that slot if it is cached.
941 for (neigh
= 0; neigh
< 8; neigh
++) {
942 ent
= pbase_tree_cache
[my_ix
];
943 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
947 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
948 ((0 <= available_ix
) &&
949 (!ent
&& pbase_tree_cache
[available_ix
])))
950 available_ix
= my_ix
;
953 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
956 /* Did not find one. Either we got a bogus request or
957 * we need to read and perhaps cache.
959 data
= read_sha1_file(sha1
, &type
, &size
);
962 if (type
!= OBJ_TREE
) {
967 /* We need to either cache or return a throwaway copy */
969 if (available_ix
< 0)
972 ent
= pbase_tree_cache
[available_ix
];
973 my_ix
= available_ix
;
977 nent
= xmalloc(sizeof(*nent
));
978 nent
->temporary
= (available_ix
< 0);
981 /* evict and reuse */
982 free(ent
->tree_data
);
985 hashcpy(nent
->sha1
, sha1
);
986 nent
->tree_data
= data
;
987 nent
->tree_size
= size
;
989 if (!nent
->temporary
)
990 pbase_tree_cache
[my_ix
] = nent
;
994 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
996 if (!cache
->temporary
) {
1000 free(cache
->tree_data
);
1004 static int name_cmp_len(const char *name
)
1007 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
1012 static void add_pbase_object(struct tree_desc
*tree
,
1015 const char *fullname
)
1017 struct name_entry entry
;
1020 while (tree_entry(tree
,&entry
)) {
1021 if (S_ISGITLINK(entry
.mode
))
1023 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1024 memcmp(name
, entry
.path
, cmplen
);
1029 if (name
[cmplen
] != '/') {
1030 add_object_entry(entry
.sha1
,
1031 object_type(entry
.mode
),
1035 if (S_ISDIR(entry
.mode
)) {
1036 struct tree_desc sub
;
1037 struct pbase_tree_cache
*tree
;
1038 const char *down
= name
+cmplen
+1;
1039 int downlen
= name_cmp_len(down
);
1041 tree
= pbase_tree_get(entry
.sha1
);
1044 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1046 add_pbase_object(&sub
, down
, downlen
, fullname
);
1047 pbase_tree_put(tree
);
1052 static unsigned *done_pbase_paths
;
1053 static int done_pbase_paths_num
;
1054 static int done_pbase_paths_alloc
;
1055 static int done_pbase_path_pos(unsigned hash
)
1058 int hi
= done_pbase_paths_num
;
1060 int mi
= (hi
+ lo
) / 2;
1061 if (done_pbase_paths
[mi
] == hash
)
1063 if (done_pbase_paths
[mi
] < hash
)
1071 static int check_pbase_path(unsigned hash
)
1073 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1077 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1078 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1079 done_pbase_paths
= xrealloc(done_pbase_paths
,
1080 done_pbase_paths_alloc
*
1083 done_pbase_paths_num
++;
1084 if (pos
< done_pbase_paths_num
)
1085 memmove(done_pbase_paths
+ pos
+ 1,
1086 done_pbase_paths
+ pos
,
1087 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1088 done_pbase_paths
[pos
] = hash
;
1092 static void add_preferred_base_object(const char *name
)
1094 struct pbase_tree
*it
;
1096 unsigned hash
= name_hash(name
);
1098 if (!num_preferred_base
|| check_pbase_path(hash
))
1101 cmplen
= name_cmp_len(name
);
1102 for (it
= pbase_tree
; it
; it
= it
->next
) {
1104 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, NULL
, 1);
1107 struct tree_desc tree
;
1108 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1109 add_pbase_object(&tree
, name
, cmplen
, name
);
1114 static void add_preferred_base(unsigned char *sha1
)
1116 struct pbase_tree
*it
;
1119 unsigned char tree_sha1
[20];
1121 if (window
<= num_preferred_base
++)
1124 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1128 for (it
= pbase_tree
; it
; it
= it
->next
) {
1129 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1135 it
= xcalloc(1, sizeof(*it
));
1136 it
->next
= pbase_tree
;
1139 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1140 it
->pcache
.tree_data
= data
;
1141 it
->pcache
.tree_size
= size
;
1144 static void cleanup_preferred_base(void)
1146 struct pbase_tree
*it
;
1152 struct pbase_tree
*this = it
;
1154 free(this->pcache
.tree_data
);
1158 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1159 if (!pbase_tree_cache
[i
])
1161 free(pbase_tree_cache
[i
]->tree_data
);
1162 free(pbase_tree_cache
[i
]);
1163 pbase_tree_cache
[i
] = NULL
;
1166 free(done_pbase_paths
);
1167 done_pbase_paths
= NULL
;
1168 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1171 static void check_object(struct object_entry
*entry
)
1173 if (entry
->in_pack
) {
1174 struct packed_git
*p
= entry
->in_pack
;
1175 struct pack_window
*w_curs
= NULL
;
1176 const unsigned char *base_ref
= NULL
;
1177 struct object_entry
*base_entry
;
1178 unsigned long used
, used_0
;
1179 unsigned long avail
;
1181 unsigned char *buf
, c
;
1183 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1186 * We want in_pack_type even if we do not reuse delta
1187 * since non-delta representations could still be reused.
1189 used
= unpack_object_header_buffer(buf
, avail
,
1190 &entry
->in_pack_type
,
1196 * Determine if this is a delta and if so whether we can
1197 * reuse it or not. Otherwise let's find out as cheaply as
1198 * possible what the actual type and size for this object is.
1200 switch (entry
->in_pack_type
) {
1202 /* Not a delta hence we've already got all we need. */
1203 entry
->type
= entry
->in_pack_type
;
1204 entry
->in_pack_header_size
= used
;
1205 if (entry
->type
< OBJ_COMMIT
|| entry
->type
> OBJ_BLOB
)
1207 unuse_pack(&w_curs
);
1210 if (reuse_delta
&& !entry
->preferred_base
)
1211 base_ref
= use_pack(p
, &w_curs
,
1212 entry
->in_pack_offset
+ used
, NULL
);
1213 entry
->in_pack_header_size
= used
+ 20;
1216 buf
= use_pack(p
, &w_curs
,
1217 entry
->in_pack_offset
+ used
, NULL
);
1223 if (!ofs
|| MSB(ofs
, 7)) {
1224 error("delta base offset overflow in pack for %s",
1225 sha1_to_hex(entry
->idx
.sha1
));
1229 ofs
= (ofs
<< 7) + (c
& 127);
1231 ofs
= entry
->in_pack_offset
- ofs
;
1232 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
1233 error("delta base offset out of bound for %s",
1234 sha1_to_hex(entry
->idx
.sha1
));
1237 if (reuse_delta
&& !entry
->preferred_base
) {
1238 struct revindex_entry
*revidx
;
1239 revidx
= find_pack_revindex(p
, ofs
);
1242 base_ref
= nth_packed_object_sha1(p
, revidx
->nr
);
1244 entry
->in_pack_header_size
= used
+ used_0
;
1248 if (base_ref
&& (base_entry
= locate_object_entry(base_ref
))) {
1250 * If base_ref was set above that means we wish to
1251 * reuse delta data, and we even found that base
1252 * in the list of objects we want to pack. Goodie!
1254 * Depth value does not matter - find_deltas() will
1255 * never consider reused delta as the base object to
1256 * deltify other objects against, in order to avoid
1259 entry
->type
= entry
->in_pack_type
;
1260 entry
->delta
= base_entry
;
1261 entry
->delta_size
= entry
->size
;
1262 entry
->delta_sibling
= base_entry
->delta_child
;
1263 base_entry
->delta_child
= entry
;
1264 unuse_pack(&w_curs
);
1270 * This must be a delta and we already know what the
1271 * final object type is. Let's extract the actual
1272 * object size from the delta header.
1274 entry
->size
= get_size_from_delta(p
, &w_curs
,
1275 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1276 if (entry
->size
== 0)
1278 unuse_pack(&w_curs
);
1283 * No choice but to fall back to the recursive delta walk
1284 * with sha1_object_info() to find about the object type
1288 unuse_pack(&w_curs
);
1291 entry
->type
= sha1_object_info(entry
->idx
.sha1
, &entry
->size
);
1293 * The error condition is checked in prepare_pack(). This is
1294 * to permit a missing preferred base object to be ignored
1295 * as a preferred base. Doing so can result in a larger
1296 * pack file, but the transfer will still take place.
1300 static int pack_offset_sort(const void *_a
, const void *_b
)
1302 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1303 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1305 /* avoid filesystem trashing with loose objects */
1306 if (!a
->in_pack
&& !b
->in_pack
)
1307 return hashcmp(a
->idx
.sha1
, b
->idx
.sha1
);
1309 if (a
->in_pack
< b
->in_pack
)
1311 if (a
->in_pack
> b
->in_pack
)
1313 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1314 (a
->in_pack_offset
> b
->in_pack_offset
);
1317 static void get_object_details(void)
1320 struct object_entry
**sorted_by_offset
;
1322 sorted_by_offset
= xcalloc(nr_objects
, sizeof(struct object_entry
*));
1323 for (i
= 0; i
< nr_objects
; i
++)
1324 sorted_by_offset
[i
] = objects
+ i
;
1325 qsort(sorted_by_offset
, nr_objects
, sizeof(*sorted_by_offset
), pack_offset_sort
);
1327 for (i
= 0; i
< nr_objects
; i
++) {
1328 struct object_entry
*entry
= sorted_by_offset
[i
];
1329 check_object(entry
);
1330 if (big_file_threshold
<= entry
->size
)
1331 entry
->no_try_delta
= 1;
1334 free(sorted_by_offset
);
1338 * We search for deltas in a list sorted by type, by filename hash, and then
1339 * by size, so that we see progressively smaller and smaller files.
1340 * That's because we prefer deltas to be from the bigger file
1341 * to the smaller -- deletes are potentially cheaper, but perhaps
1342 * more importantly, the bigger file is likely the more recent
1343 * one. The deepest deltas are therefore the oldest objects which are
1344 * less susceptible to be accessed often.
1346 static int type_size_sort(const void *_a
, const void *_b
)
1348 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1349 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1351 if (a
->type
> b
->type
)
1353 if (a
->type
< b
->type
)
1355 if (a
->hash
> b
->hash
)
1357 if (a
->hash
< b
->hash
)
1359 if (a
->preferred_base
> b
->preferred_base
)
1361 if (a
->preferred_base
< b
->preferred_base
)
1363 if (a
->size
> b
->size
)
1365 if (a
->size
< b
->size
)
1367 return a
< b
? -1 : (a
> b
); /* newest first */
1371 struct object_entry
*entry
;
1373 struct delta_index
*index
;
1377 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
1378 unsigned long delta_size
)
1380 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
1383 if (delta_size
< cache_max_small_delta_size
)
1386 /* cache delta, if objects are large enough compared to delta size */
1387 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
1395 static pthread_mutex_t read_mutex
;
1396 #define read_lock() pthread_mutex_lock(&read_mutex)
1397 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1399 static pthread_mutex_t cache_mutex
;
1400 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1401 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1403 static pthread_mutex_t progress_mutex
;
1404 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1405 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1409 #define read_lock() (void)0
1410 #define read_unlock() (void)0
1411 #define cache_lock() (void)0
1412 #define cache_unlock() (void)0
1413 #define progress_lock() (void)0
1414 #define progress_unlock() (void)0
1418 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1419 unsigned max_depth
, unsigned long *mem_usage
)
1421 struct object_entry
*trg_entry
= trg
->entry
;
1422 struct object_entry
*src_entry
= src
->entry
;
1423 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1425 enum object_type type
;
1428 /* Don't bother doing diffs between different types */
1429 if (trg_entry
->type
!= src_entry
->type
)
1433 * We do not bother to try a delta that we discarded on an
1434 * earlier try, but only when reusing delta data. Note that
1435 * src_entry that is marked as the preferred_base should always
1436 * be considered, as even if we produce a suboptimal delta against
1437 * it, we will still save the transfer cost, as we already know
1438 * the other side has it and we won't send src_entry at all.
1440 if (reuse_delta
&& trg_entry
->in_pack
&&
1441 trg_entry
->in_pack
== src_entry
->in_pack
&&
1442 !src_entry
->preferred_base
&&
1443 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1444 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1447 /* Let's not bust the allowed depth. */
1448 if (src
->depth
>= max_depth
)
1451 /* Now some size filtering heuristics. */
1452 trg_size
= trg_entry
->size
;
1453 if (!trg_entry
->delta
) {
1454 max_size
= trg_size
/2 - 20;
1457 max_size
= trg_entry
->delta_size
;
1458 ref_depth
= trg
->depth
;
1460 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
1461 (max_depth
- ref_depth
+ 1);
1464 src_size
= src_entry
->size
;
1465 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1466 if (sizediff
>= max_size
)
1468 if (trg_size
< src_size
/ 32)
1471 /* Load data if not already done */
1474 trg
->data
= read_sha1_file(trg_entry
->idx
.sha1
, &type
, &sz
);
1477 die("object %s cannot be read",
1478 sha1_to_hex(trg_entry
->idx
.sha1
));
1480 die("object %s inconsistent object length (%lu vs %lu)",
1481 sha1_to_hex(trg_entry
->idx
.sha1
), sz
, trg_size
);
1486 src
->data
= read_sha1_file(src_entry
->idx
.sha1
, &type
, &sz
);
1489 if (src_entry
->preferred_base
) {
1490 static int warned
= 0;
1492 warning("object %s cannot be read",
1493 sha1_to_hex(src_entry
->idx
.sha1
));
1495 * Those objects are not included in the
1496 * resulting pack. Be resilient and ignore
1497 * them if they can't be read, in case the
1498 * pack could be created nevertheless.
1502 die("object %s cannot be read",
1503 sha1_to_hex(src_entry
->idx
.sha1
));
1506 die("object %s inconsistent object length (%lu vs %lu)",
1507 sha1_to_hex(src_entry
->idx
.sha1
), sz
, src_size
);
1511 src
->index
= create_delta_index(src
->data
, src_size
);
1513 static int warned
= 0;
1515 warning("suboptimal pack - out of memory");
1518 *mem_usage
+= sizeof_delta_index(src
->index
);
1521 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1525 if (trg_entry
->delta
) {
1526 /* Prefer only shallower same-sized deltas. */
1527 if (delta_size
== trg_entry
->delta_size
&&
1528 src
->depth
+ 1 >= trg
->depth
) {
1535 * Handle memory allocation outside of the cache
1536 * accounting lock. Compiler will optimize the strangeness
1537 * away when NO_PTHREADS is defined.
1539 free(trg_entry
->delta_data
);
1541 if (trg_entry
->delta_data
) {
1542 delta_cache_size
-= trg_entry
->delta_size
;
1543 trg_entry
->delta_data
= NULL
;
1545 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
1546 delta_cache_size
+= delta_size
;
1548 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
1554 trg_entry
->delta
= src_entry
;
1555 trg_entry
->delta_size
= delta_size
;
1556 trg
->depth
= src
->depth
+ 1;
1561 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1563 struct object_entry
*child
= me
->delta_child
;
1566 unsigned int c
= check_delta_limit(child
, n
+ 1);
1569 child
= child
->delta_sibling
;
1574 static unsigned long free_unpacked(struct unpacked
*n
)
1576 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
1577 free_delta_index(n
->index
);
1580 freed_mem
+= n
->entry
->size
;
1589 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
1590 int window
, int depth
, unsigned *processed
)
1592 uint32_t i
, idx
= 0, count
= 0;
1593 struct unpacked
*array
;
1594 unsigned long mem_usage
= 0;
1596 array
= xcalloc(window
, sizeof(struct unpacked
));
1599 struct object_entry
*entry
;
1600 struct unpacked
*n
= array
+ idx
;
1601 int j
, max_depth
, best_base
= -1;
1610 if (!entry
->preferred_base
) {
1612 display_progress(progress_state
, *processed
);
1616 mem_usage
-= free_unpacked(n
);
1619 while (window_memory_limit
&&
1620 mem_usage
> window_memory_limit
&&
1622 uint32_t tail
= (idx
+ window
- count
) % window
;
1623 mem_usage
-= free_unpacked(array
+ tail
);
1627 /* We do not compute delta to *create* objects we are not
1630 if (entry
->preferred_base
)
1634 * If the current object is at pack edge, take the depth the
1635 * objects that depend on the current object into account
1636 * otherwise they would become too deep.
1639 if (entry
->delta_child
) {
1640 max_depth
-= check_delta_limit(entry
, 0);
1648 uint32_t other_idx
= idx
+ j
;
1650 if (other_idx
>= window
)
1651 other_idx
-= window
;
1652 m
= array
+ other_idx
;
1655 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
1659 best_base
= other_idx
;
1663 * If we decided to cache the delta data, then it is best
1664 * to compress it right away. First because we have to do
1665 * it anyway, and doing it here while we're threaded will
1666 * save a lot of time in the non threaded write phase,
1667 * as well as allow for caching more deltas within
1668 * the same cache size limit.
1670 * But only if not writing to stdout, since in that case
1671 * the network is most likely throttling writes anyway,
1672 * and therefore it is best to go to the write phase ASAP
1673 * instead, as we can afford spending more time compressing
1674 * between writes at that moment.
1676 if (entry
->delta_data
&& !pack_to_stdout
) {
1677 entry
->z_delta_size
= do_compress(&entry
->delta_data
,
1680 delta_cache_size
-= entry
->delta_size
;
1681 delta_cache_size
+= entry
->z_delta_size
;
1685 /* if we made n a delta, and if n is already at max
1686 * depth, leaving it in the window is pointless. we
1687 * should evict it first.
1689 if (entry
->delta
&& max_depth
<= n
->depth
)
1693 * Move the best delta base up in the window, after the
1694 * currently deltified object, to keep it longer. It will
1695 * be the first base object to be attempted next.
1698 struct unpacked swap
= array
[best_base
];
1699 int dist
= (window
+ idx
- best_base
) % window
;
1700 int dst
= best_base
;
1702 int src
= (dst
+ 1) % window
;
1703 array
[dst
] = array
[src
];
1711 if (count
+ 1 < window
)
1717 for (i
= 0; i
< window
; ++i
) {
1718 free_delta_index(array
[i
].index
);
1719 free(array
[i
].data
);
1726 static void try_to_free_from_threads(size_t size
)
1729 release_pack_memory(size
, -1);
1733 static try_to_free_t old_try_to_free_routine
;
1736 * The main thread waits on the condition that (at least) one of the workers
1737 * has stopped working (which is indicated in the .working member of
1738 * struct thread_params).
1739 * When a work thread has completed its work, it sets .working to 0 and
1740 * signals the main thread and waits on the condition that .data_ready
1744 struct thread_params
{
1746 struct object_entry
**list
;
1753 pthread_mutex_t mutex
;
1754 pthread_cond_t cond
;
1755 unsigned *processed
;
1758 static pthread_cond_t progress_cond
;
1761 * Mutex and conditional variable can't be statically-initialized on Windows.
1763 static void init_threaded_search(void)
1765 init_recursive_mutex(&read_mutex
);
1766 pthread_mutex_init(&cache_mutex
, NULL
);
1767 pthread_mutex_init(&progress_mutex
, NULL
);
1768 pthread_cond_init(&progress_cond
, NULL
);
1769 old_try_to_free_routine
= set_try_to_free_routine(try_to_free_from_threads
);
1772 static void cleanup_threaded_search(void)
1774 set_try_to_free_routine(old_try_to_free_routine
);
1775 pthread_cond_destroy(&progress_cond
);
1776 pthread_mutex_destroy(&read_mutex
);
1777 pthread_mutex_destroy(&cache_mutex
);
1778 pthread_mutex_destroy(&progress_mutex
);
1781 static void *threaded_find_deltas(void *arg
)
1783 struct thread_params
*me
= arg
;
1785 while (me
->remaining
) {
1786 find_deltas(me
->list
, &me
->remaining
,
1787 me
->window
, me
->depth
, me
->processed
);
1791 pthread_cond_signal(&progress_cond
);
1795 * We must not set ->data_ready before we wait on the
1796 * condition because the main thread may have set it to 1
1797 * before we get here. In order to be sure that new
1798 * work is available if we see 1 in ->data_ready, it
1799 * was initialized to 0 before this thread was spawned
1800 * and we reset it to 0 right away.
1802 pthread_mutex_lock(&me
->mutex
);
1803 while (!me
->data_ready
)
1804 pthread_cond_wait(&me
->cond
, &me
->mutex
);
1806 pthread_mutex_unlock(&me
->mutex
);
1808 /* leave ->working 1 so that this doesn't get more work assigned */
1812 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
1813 int window
, int depth
, unsigned *processed
)
1815 struct thread_params
*p
;
1816 int i
, ret
, active_threads
= 0;
1818 init_threaded_search();
1820 if (!delta_search_threads
) /* --threads=0 means autodetect */
1821 delta_search_threads
= online_cpus();
1822 if (delta_search_threads
<= 1) {
1823 find_deltas(list
, &list_size
, window
, depth
, processed
);
1824 cleanup_threaded_search();
1827 if (progress
> pack_to_stdout
)
1828 fprintf(stderr
, "Delta compression using up to %d threads.\n",
1829 delta_search_threads
);
1830 p
= xcalloc(delta_search_threads
, sizeof(*p
));
1832 /* Partition the work amongst work threads. */
1833 for (i
= 0; i
< delta_search_threads
; i
++) {
1834 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
1836 /* don't use too small segments or no deltas will be found */
1837 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
1840 p
[i
].window
= window
;
1842 p
[i
].processed
= processed
;
1844 p
[i
].data_ready
= 0;
1846 /* try to split chunks on "path" boundaries */
1847 while (sub_size
&& sub_size
< list_size
&&
1848 list
[sub_size
]->hash
&&
1849 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
1853 p
[i
].list_size
= sub_size
;
1854 p
[i
].remaining
= sub_size
;
1857 list_size
-= sub_size
;
1860 /* Start work threads. */
1861 for (i
= 0; i
< delta_search_threads
; i
++) {
1862 if (!p
[i
].list_size
)
1864 pthread_mutex_init(&p
[i
].mutex
, NULL
);
1865 pthread_cond_init(&p
[i
].cond
, NULL
);
1866 ret
= pthread_create(&p
[i
].thread
, NULL
,
1867 threaded_find_deltas
, &p
[i
]);
1869 die("unable to create thread: %s", strerror(ret
));
1874 * Now let's wait for work completion. Each time a thread is done
1875 * with its work, we steal half of the remaining work from the
1876 * thread with the largest number of unprocessed objects and give
1877 * it to that newly idle thread. This ensure good load balancing
1878 * until the remaining object list segments are simply too short
1879 * to be worth splitting anymore.
1881 while (active_threads
) {
1882 struct thread_params
*target
= NULL
;
1883 struct thread_params
*victim
= NULL
;
1884 unsigned sub_size
= 0;
1888 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
1893 pthread_cond_wait(&progress_cond
, &progress_mutex
);
1896 for (i
= 0; i
< delta_search_threads
; i
++)
1897 if (p
[i
].remaining
> 2*window
&&
1898 (!victim
|| victim
->remaining
< p
[i
].remaining
))
1901 sub_size
= victim
->remaining
/ 2;
1902 list
= victim
->list
+ victim
->list_size
- sub_size
;
1903 while (sub_size
&& list
[0]->hash
&&
1904 list
[0]->hash
== list
[-1]->hash
) {
1910 * It is possible for some "paths" to have
1911 * so many objects that no hash boundary
1912 * might be found. Let's just steal the
1913 * exact half in that case.
1915 sub_size
= victim
->remaining
/ 2;
1918 target
->list
= list
;
1919 victim
->list_size
-= sub_size
;
1920 victim
->remaining
-= sub_size
;
1922 target
->list_size
= sub_size
;
1923 target
->remaining
= sub_size
;
1924 target
->working
= 1;
1927 pthread_mutex_lock(&target
->mutex
);
1928 target
->data_ready
= 1;
1929 pthread_cond_signal(&target
->cond
);
1930 pthread_mutex_unlock(&target
->mutex
);
1933 pthread_join(target
->thread
, NULL
);
1934 pthread_cond_destroy(&target
->cond
);
1935 pthread_mutex_destroy(&target
->mutex
);
1939 cleanup_threaded_search();
1944 #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
1947 static int add_ref_tag(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
1949 unsigned char peeled
[20];
1951 if (!prefixcmp(path
, "refs/tags/") && /* is a tag? */
1952 !peel_ref(path
, peeled
) && /* peelable? */
1953 !is_null_sha1(peeled
) && /* annotated tag? */
1954 locate_object_entry(peeled
)) /* object packed? */
1955 add_object_entry(sha1
, OBJ_TAG
, NULL
, 0);
1959 static void prepare_pack(int window
, int depth
)
1961 struct object_entry
**delta_list
;
1962 uint32_t i
, nr_deltas
;
1965 get_object_details();
1968 * If we're locally repacking then we need to be doubly careful
1969 * from now on in order to make sure no stealth corruption gets
1970 * propagated to the new pack. Clients receiving streamed packs
1971 * should validate everything they get anyway so no need to incur
1972 * the additional cost here in that case.
1974 if (!pack_to_stdout
)
1975 do_check_packed_object_crc
= 1;
1977 if (!nr_objects
|| !window
|| !depth
)
1980 delta_list
= xmalloc(nr_objects
* sizeof(*delta_list
));
1983 for (i
= 0; i
< nr_objects
; i
++) {
1984 struct object_entry
*entry
= objects
+ i
;
1987 /* This happens if we decided to reuse existing
1988 * delta from a pack. "reuse_delta &&" is implied.
1992 if (entry
->size
< 50)
1995 if (entry
->no_try_delta
)
1998 if (!entry
->preferred_base
) {
2000 if (entry
->type
< 0)
2001 die("unable to get type of object %s",
2002 sha1_to_hex(entry
->idx
.sha1
));
2004 if (entry
->type
< 0) {
2006 * This object is not found, but we
2007 * don't have to include it anyway.
2013 delta_list
[n
++] = entry
;
2016 if (nr_deltas
&& n
> 1) {
2017 unsigned nr_done
= 0;
2019 progress_state
= start_progress("Compressing objects",
2021 qsort(delta_list
, n
, sizeof(*delta_list
), type_size_sort
);
2022 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
2023 stop_progress(&progress_state
);
2024 if (nr_done
!= nr_deltas
)
2025 die("inconsistency with delta count");
2030 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2032 if (!strcmp(k
, "pack.window")) {
2033 window
= git_config_int(k
, v
);
2036 if (!strcmp(k
, "pack.windowmemory")) {
2037 window_memory_limit
= git_config_ulong(k
, v
);
2040 if (!strcmp(k
, "pack.depth")) {
2041 depth
= git_config_int(k
, v
);
2044 if (!strcmp(k
, "pack.compression")) {
2045 int level
= git_config_int(k
, v
);
2047 level
= Z_DEFAULT_COMPRESSION
;
2048 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
2049 die("bad pack compression level %d", level
);
2050 pack_compression_level
= level
;
2051 pack_compression_seen
= 1;
2054 if (!strcmp(k
, "pack.deltacachesize")) {
2055 max_delta_cache_size
= git_config_int(k
, v
);
2058 if (!strcmp(k
, "pack.deltacachelimit")) {
2059 cache_max_small_delta_size
= git_config_int(k
, v
);
2062 if (!strcmp(k
, "pack.threads")) {
2063 delta_search_threads
= git_config_int(k
, v
);
2064 if (delta_search_threads
< 0)
2065 die("invalid number of threads specified (%d)",
2066 delta_search_threads
);
2068 if (delta_search_threads
!= 1)
2069 warning("no threads support, ignoring %s", k
);
2073 if (!strcmp(k
, "pack.indexversion")) {
2074 pack_idx_opts
.version
= git_config_int(k
, v
);
2075 if (pack_idx_opts
.version
> 2)
2076 die("bad pack.indexversion=%"PRIu32
,
2077 pack_idx_opts
.version
);
2080 return git_default_config(k
, v
, cb
);
2083 static void read_object_list_from_stdin(void)
2085 char line
[40 + 1 + PATH_MAX
+ 2];
2086 unsigned char sha1
[20];
2089 if (!fgets(line
, sizeof(line
), stdin
)) {
2093 die("fgets returned NULL, not EOF, not error!");
2099 if (line
[0] == '-') {
2100 if (get_sha1_hex(line
+1, sha1
))
2101 die("expected edge sha1, got garbage:\n %s",
2103 add_preferred_base(sha1
);
2106 if (get_sha1_hex(line
, sha1
))
2107 die("expected sha1, got garbage:\n %s", line
);
2109 add_preferred_base_object(line
+41);
2110 add_object_entry(sha1
, 0, line
+41, 0);
2114 #define OBJECT_ADDED (1u<<20)
2116 static void show_commit(struct commit
*commit
, void *data
)
2118 add_object_entry(commit
->object
.sha1
, OBJ_COMMIT
, NULL
, 0);
2119 commit
->object
.flags
|= OBJECT_ADDED
;
2122 static void show_object(struct object
*obj
,
2123 const struct name_path
*path
, const char *last
,
2126 char *name
= path_name(path
, last
);
2128 add_preferred_base_object(name
);
2129 add_object_entry(obj
->sha1
, obj
->type
, name
, 0);
2130 obj
->flags
|= OBJECT_ADDED
;
2133 * We will have generated the hash from the name,
2134 * but not saved a pointer to it - we can free it
2139 static void show_edge(struct commit
*commit
)
2141 add_preferred_base(commit
->object
.sha1
);
2144 struct in_pack_object
{
2146 struct object
*object
;
2152 struct in_pack_object
*array
;
2155 static void mark_in_pack_object(struct object
*object
, struct packed_git
*p
, struct in_pack
*in_pack
)
2157 in_pack
->array
[in_pack
->nr
].offset
= find_pack_entry_one(object
->sha1
, p
);
2158 in_pack
->array
[in_pack
->nr
].object
= object
;
2163 * Compare the objects in the offset order, in order to emulate the
2164 * "git rev-list --objects" output that produced the pack originally.
2166 static int ofscmp(const void *a_
, const void *b_
)
2168 struct in_pack_object
*a
= (struct in_pack_object
*)a_
;
2169 struct in_pack_object
*b
= (struct in_pack_object
*)b_
;
2171 if (a
->offset
< b
->offset
)
2173 else if (a
->offset
> b
->offset
)
2176 return hashcmp(a
->object
->sha1
, b
->object
->sha1
);
2179 static void add_objects_in_unpacked_packs(struct rev_info
*revs
)
2181 struct packed_git
*p
;
2182 struct in_pack in_pack
;
2185 memset(&in_pack
, 0, sizeof(in_pack
));
2187 for (p
= packed_git
; p
; p
= p
->next
) {
2188 const unsigned char *sha1
;
2191 if (!p
->pack_local
|| p
->pack_keep
)
2193 if (open_pack_index(p
))
2194 die("cannot open pack index");
2196 ALLOC_GROW(in_pack
.array
,
2197 in_pack
.nr
+ p
->num_objects
,
2200 for (i
= 0; i
< p
->num_objects
; i
++) {
2201 sha1
= nth_packed_object_sha1(p
, i
);
2202 o
= lookup_unknown_object(sha1
);
2203 if (!(o
->flags
& OBJECT_ADDED
))
2204 mark_in_pack_object(o
, p
, &in_pack
);
2205 o
->flags
|= OBJECT_ADDED
;
2210 qsort(in_pack
.array
, in_pack
.nr
, sizeof(in_pack
.array
[0]),
2212 for (i
= 0; i
< in_pack
.nr
; i
++) {
2213 struct object
*o
= in_pack
.array
[i
].object
;
2214 add_object_entry(o
->sha1
, o
->type
, "", 0);
2217 free(in_pack
.array
);
2220 static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1
)
2222 static struct packed_git
*last_found
= (void *)1;
2223 struct packed_git
*p
;
2225 p
= (last_found
!= (void *)1) ? last_found
: packed_git
;
2228 if ((!p
->pack_local
|| p
->pack_keep
) &&
2229 find_pack_entry_one(sha1
, p
)) {
2233 if (p
== last_found
)
2237 if (p
== last_found
)
2243 static void loosen_unused_packed_objects(struct rev_info
*revs
)
2245 struct packed_git
*p
;
2247 const unsigned char *sha1
;
2249 for (p
= packed_git
; p
; p
= p
->next
) {
2250 if (!p
->pack_local
|| p
->pack_keep
)
2253 if (unpack_unreachable_expiration
&&
2254 p
->mtime
< unpack_unreachable_expiration
)
2257 if (open_pack_index(p
))
2258 die("cannot open pack index");
2260 for (i
= 0; i
< p
->num_objects
; i
++) {
2261 sha1
= nth_packed_object_sha1(p
, i
);
2262 if (!locate_object_entry(sha1
) &&
2263 !has_sha1_pack_kept_or_nonlocal(sha1
))
2264 if (force_object_loose(sha1
, p
->mtime
))
2265 die("unable to force loose object");
2270 static void get_object_list(int ac
, const char **av
)
2272 struct rev_info revs
;
2276 init_revisions(&revs
, NULL
);
2277 save_commit_buffer
= 0;
2278 setup_revisions(ac
, av
, &revs
, NULL
);
2280 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
2281 int len
= strlen(line
);
2282 if (len
&& line
[len
- 1] == '\n')
2287 if (!strcmp(line
, "--not")) {
2288 flags
^= UNINTERESTING
;
2291 die("not a rev '%s'", line
);
2293 if (handle_revision_arg(line
, &revs
, flags
, 1))
2294 die("bad revision '%s'", line
);
2297 if (prepare_revision_walk(&revs
))
2298 die("revision walk setup failed");
2299 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
2300 traverse_commit_list(&revs
, show_commit
, show_object
, NULL
);
2302 if (keep_unreachable
)
2303 add_objects_in_unpacked_packs(&revs
);
2304 if (unpack_unreachable
)
2305 loosen_unused_packed_objects(&revs
);
2308 static int option_parse_index_version(const struct option
*opt
,
2309 const char *arg
, int unset
)
2312 const char *val
= arg
;
2313 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
2314 if (pack_idx_opts
.version
> 2)
2315 die(_("unsupported index version %s"), val
);
2316 if (*c
== ',' && c
[1])
2317 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
2318 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
2319 die(_("bad index version '%s'"), val
);
2323 static int option_parse_unpack_unreachable(const struct option
*opt
,
2324 const char *arg
, int unset
)
2327 unpack_unreachable
= 0;
2328 unpack_unreachable_expiration
= 0;
2331 unpack_unreachable
= 1;
2333 unpack_unreachable_expiration
= approxidate(arg
);
2338 static int option_parse_ulong(const struct option
*opt
,
2339 const char *arg
, int unset
)
2342 die(_("option %s does not accept negative form"),
2345 if (!git_parse_ulong(arg
, opt
->value
))
2346 die(_("unable to parse value '%s' for option %s"),
2347 arg
, opt
->long_name
);
2351 #define OPT_ULONG(s, l, v, h) \
2352 { OPTION_CALLBACK, (s), (l), (v), "n", (h), \
2353 PARSE_OPT_NONEG, option_parse_ulong }
2355 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
2357 int use_internal_rev_list
= 0;
2359 int all_progress_implied
= 0;
2360 const char *rp_av
[6];
2362 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
2363 struct option pack_objects_options
[] = {
2364 OPT_SET_INT('q', "quiet", &progress
,
2365 "do not show progress meter", 0),
2366 OPT_SET_INT(0, "progress", &progress
,
2367 "show progress meter", 1),
2368 OPT_SET_INT(0, "all-progress", &progress
,
2369 "show progress meter during object writing phase", 2),
2370 OPT_BOOL(0, "all-progress-implied",
2371 &all_progress_implied
,
2372 "similar to --all-progress when progress meter is shown"),
2373 { OPTION_CALLBACK
, 0, "index-version", NULL
, "version[,offset]",
2374 "write the pack index file in the specified idx format version",
2375 0, option_parse_index_version
},
2376 OPT_ULONG(0, "max-pack-size", &pack_size_limit
,
2377 "maximum size of each output pack file"),
2378 OPT_BOOL(0, "local", &local
,
2379 "ignore borrowed objects from alternate object store"),
2380 OPT_BOOL(0, "incremental", &incremental
,
2381 "ignore packed objects"),
2382 OPT_INTEGER(0, "window", &window
,
2383 "limit pack window by objects"),
2384 OPT_ULONG(0, "window-memory", &window_memory_limit
,
2385 "limit pack window by memory in addition to object limit"),
2386 OPT_INTEGER(0, "depth", &depth
,
2387 "maximum length of delta chain allowed in the resulting pack"),
2388 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
2389 "reuse existing deltas"),
2390 OPT_BOOL(0, "reuse-object", &reuse_object
,
2391 "reuse existing objects"),
2392 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
2393 "use OFS_DELTA objects"),
2394 OPT_INTEGER(0, "threads", &delta_search_threads
,
2395 "use threads when searching for best delta matches"),
2396 OPT_BOOL(0, "non-empty", &non_empty
,
2397 "do not create an empty pack output"),
2398 OPT_BOOL(0, "revs", &use_internal_rev_list
,
2399 "read revision arguments from standard input"),
2400 { OPTION_SET_INT
, 0, "unpacked", &rev_list_unpacked
, NULL
,
2401 "limit the objects to those that are not yet packed",
2402 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2403 { OPTION_SET_INT
, 0, "all", &rev_list_all
, NULL
,
2404 "include objects reachable from any reference",
2405 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2406 { OPTION_SET_INT
, 0, "reflog", &rev_list_reflog
, NULL
,
2407 "include objects referred by reflog entries",
2408 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2409 OPT_BOOL(0, "stdout", &pack_to_stdout
,
2410 "output pack to stdout"),
2411 OPT_BOOL(0, "include-tag", &include_tag
,
2412 "include tag objects that refer to objects to be packed"),
2413 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
2414 "keep unreachable objects"),
2415 { OPTION_CALLBACK
, 0, "unpack-unreachable", NULL
, "time",
2416 "unpack unreachable objects newer than <time>",
2417 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
},
2418 OPT_BOOL(0, "thin", &thin
,
2419 "create thin packs"),
2420 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep
,
2421 "ignore packs that have companion .keep file"),
2422 OPT_INTEGER(0, "compression", &pack_compression_level
,
2423 "pack compression level"),
2424 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents
,
2425 "do not hide commits by grafts", 0),
2429 read_replace_refs
= 0;
2431 reset_pack_idx_option(&pack_idx_opts
);
2432 git_config(git_pack_config
, NULL
);
2433 if (!pack_compression_seen
&& core_compression_seen
)
2434 pack_compression_level
= core_compression_level
;
2436 progress
= isatty(2);
2437 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
2441 base_name
= argv
[0];
2444 if (pack_to_stdout
!= !base_name
|| argc
)
2445 usage_with_options(pack_usage
, pack_objects_options
);
2447 rp_av
[rp_ac
++] = "pack-objects";
2449 use_internal_rev_list
= 1;
2450 rp_av
[rp_ac
++] = "--objects-edge";
2452 rp_av
[rp_ac
++] = "--objects";
2455 use_internal_rev_list
= 1;
2456 rp_av
[rp_ac
++] = "--all";
2458 if (rev_list_reflog
) {
2459 use_internal_rev_list
= 1;
2460 rp_av
[rp_ac
++] = "--reflog";
2462 if (rev_list_unpacked
) {
2463 use_internal_rev_list
= 1;
2464 rp_av
[rp_ac
++] = "--unpacked";
2469 if (pack_compression_level
== -1)
2470 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
2471 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
2472 die("bad pack compression level %d", pack_compression_level
);
2474 if (delta_search_threads
!= 1)
2475 warning("no threads support, ignoring --threads");
2477 if (!pack_to_stdout
&& !pack_size_limit
)
2478 pack_size_limit
= pack_size_limit_cfg
;
2479 if (pack_to_stdout
&& pack_size_limit
)
2480 die("--max-pack-size cannot be used to build a pack for transfer.");
2481 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
2482 warning("minimum pack size limit is 1 MiB");
2483 pack_size_limit
= 1024*1024;
2486 if (!pack_to_stdout
&& thin
)
2487 die("--thin cannot be used to build an indexable pack.");
2489 if (keep_unreachable
&& unpack_unreachable
)
2490 die("--keep-unreachable and --unpack-unreachable are incompatible.");
2492 if (progress
&& all_progress_implied
)
2495 prepare_packed_git();
2498 progress_state
= start_progress("Counting objects", 0);
2499 if (!use_internal_rev_list
)
2500 read_object_list_from_stdin();
2502 rp_av
[rp_ac
] = NULL
;
2503 get_object_list(rp_ac
, rp_av
);
2505 cleanup_preferred_base();
2506 if (include_tag
&& nr_result
)
2507 for_each_ref(add_ref_tag
, NULL
);
2508 stop_progress(&progress_state
);
2510 if (non_empty
&& !nr_result
)
2513 prepare_pack(window
, depth
);
2516 fprintf(stderr
, "Total %"PRIu32
" (delta %"PRIu32
"),"
2517 " reused %"PRIu32
" (delta %"PRIu32
")\n",
2518 written
, written_delta
, reused
, reused_delta
);