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 [ -q | --progress | --all-progress ]\n"
23 " [--all-progress-implied]\n"
24 " [--max-pack-size=<n>] [--local] [--incremental]\n"
25 " [--window=<n>] [--window-memory=<n>] [--depth=<n>]\n"
26 " [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset]\n"
27 " [--threads=<n>] [--non-empty] [--revs [--unpacked | --all]]\n"
28 " [--reflog] [--stdout | base-name] [--include-tag]\n"
29 " [--keep-unreachable | --unpack-unreachable]\n"
30 " [< ref-list | < object-list]";
33 struct pack_idx_entry idx
;
34 unsigned long size
; /* uncompressed size */
35 struct packed_git
*in_pack
; /* already in pack */
37 struct object_entry
*delta
; /* delta base object */
38 struct object_entry
*delta_child
; /* deltified objects who bases me */
39 struct object_entry
*delta_sibling
; /* other deltified objects who
40 * uses the same base as me
42 void *delta_data
; /* cached delta (uncompressed) */
43 unsigned long delta_size
; /* delta data size (uncompressed) */
44 unsigned long z_delta_size
; /* delta data size (compressed) */
45 unsigned int hash
; /* name hint hash */
46 enum object_type type
;
47 enum object_type in_pack_type
; /* could be delta */
48 unsigned char in_pack_header_size
;
49 unsigned char preferred_base
; /* we do not pack this, but is available
50 * to be used as the base object to delta
53 unsigned char no_try_delta
;
54 unsigned char tagged
; /* near the very tip of refs */
55 unsigned char filled
; /* assigned write-order */
59 * Objects we are going to pack are collected in objects array (dynamically
60 * expanded). nr_objects & nr_alloc controls this array. They are stored
61 * in the order we see -- typically rev-list --objects order that gives us
62 * nice "minimum seek" order.
64 static struct object_entry
*objects
;
65 static struct pack_idx_entry
**written_list
;
66 static uint32_t nr_objects
, nr_alloc
, nr_result
, nr_written
;
69 static int reuse_delta
= 1, reuse_object
= 1;
70 static int keep_unreachable
, unpack_unreachable
, include_tag
;
72 static int incremental
;
73 static int ignore_packed_keep
;
74 static int allow_ofs_delta
;
75 static struct pack_idx_option pack_idx_opts
;
76 static const char *base_name
;
77 static int progress
= 1;
78 static int window
= 10;
79 static unsigned long pack_size_limit
, pack_size_limit_cfg
;
80 static int depth
= 50;
81 static int delta_search_threads
;
82 static int pack_to_stdout
;
83 static int num_preferred_base
;
84 static struct progress
*progress_state
;
85 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
86 static int pack_compression_seen
;
88 static unsigned long delta_cache_size
= 0;
89 static unsigned long max_delta_cache_size
= 256 * 1024 * 1024;
90 static unsigned long cache_max_small_delta_size
= 1000;
92 static unsigned long window_memory_limit
= 0;
95 * The object names in objects array are hashed with this hashtable,
96 * to help looking up the entry by object name.
97 * This hashtable is built after all the objects are seen.
99 static int *object_ix
;
100 static int object_ix_hashsz
;
101 static struct object_entry
*locate_object_entry(const unsigned char *sha1
);
106 static uint32_t written
, written_delta
;
107 static uint32_t reused
, reused_delta
;
110 static void *get_delta(struct object_entry
*entry
)
112 unsigned long size
, base_size
, delta_size
;
113 void *buf
, *base_buf
, *delta_buf
;
114 enum object_type type
;
116 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
118 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
119 base_buf
= read_sha1_file(entry
->delta
->idx
.sha1
, &type
, &base_size
);
121 die("unable to read %s", sha1_to_hex(entry
->delta
->idx
.sha1
));
122 delta_buf
= diff_delta(base_buf
, base_size
,
123 buf
, size
, &delta_size
, 0);
124 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
125 die("delta size changed");
131 static unsigned long do_compress(void **pptr
, unsigned long size
)
135 unsigned long maxsize
;
137 memset(&stream
, 0, sizeof(stream
));
138 git_deflate_init(&stream
, pack_compression_level
);
139 maxsize
= git_deflate_bound(&stream
, size
);
142 out
= xmalloc(maxsize
);
146 stream
.avail_in
= size
;
147 stream
.next_out
= out
;
148 stream
.avail_out
= maxsize
;
149 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
151 git_deflate_end(&stream
);
154 return stream
.total_out
;
158 * we are going to reuse the existing object data as is. make
159 * sure it is not corrupt.
161 static int check_pack_inflate(struct packed_git
*p
,
162 struct pack_window
**w_curs
,
165 unsigned long expect
)
168 unsigned char fakebuf
[4096], *in
;
171 memset(&stream
, 0, sizeof(stream
));
172 git_inflate_init(&stream
);
174 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
176 stream
.next_out
= fakebuf
;
177 stream
.avail_out
= sizeof(fakebuf
);
178 st
= git_inflate(&stream
, Z_FINISH
);
179 offset
+= stream
.next_in
- in
;
180 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
181 git_inflate_end(&stream
);
182 return (st
== Z_STREAM_END
&&
183 stream
.total_out
== expect
&&
184 stream
.total_in
== len
) ? 0 : -1;
187 static void copy_pack_data(struct sha1file
*f
,
188 struct packed_git
*p
,
189 struct pack_window
**w_curs
,
197 in
= use_pack(p
, w_curs
, offset
, &avail
);
199 avail
= (unsigned long)len
;
200 sha1write(f
, in
, avail
);
206 /* Return 0 if we will bust the pack-size limit */
207 static unsigned long write_object(struct sha1file
*f
,
208 struct object_entry
*entry
,
211 unsigned long size
, limit
, datalen
;
213 unsigned char header
[10], dheader
[10];
215 enum object_type type
;
216 int usable_delta
, to_reuse
;
223 /* apply size limit if limited packsize and not first object */
224 if (!pack_size_limit
|| !nr_written
)
226 else if (pack_size_limit
<= write_offset
)
228 * the earlier object did not fit the limit; avoid
229 * mistaking this with unlimited (i.e. limit = 0).
233 limit
= pack_size_limit
- write_offset
;
236 usable_delta
= 0; /* no delta */
237 else if (!pack_size_limit
)
238 usable_delta
= 1; /* unlimited packfile */
239 else if (entry
->delta
->idx
.offset
== (off_t
)-1)
240 usable_delta
= 0; /* base was written to another pack */
241 else if (entry
->delta
->idx
.offset
)
242 usable_delta
= 1; /* base already exists in this pack */
244 usable_delta
= 0; /* base could end up in another pack */
247 to_reuse
= 0; /* explicit */
248 else if (!entry
->in_pack
)
249 to_reuse
= 0; /* can't reuse what we don't have */
250 else if (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
)
251 /* check_object() decided it for us ... */
252 to_reuse
= usable_delta
;
253 /* ... but pack split may override that */
254 else if (type
!= entry
->in_pack_type
)
255 to_reuse
= 0; /* pack has delta which is unusable */
256 else if (entry
->delta
)
257 to_reuse
= 0; /* we want to pack afresh */
259 to_reuse
= 1; /* we have it in-pack undeltified,
260 * and we do not need to deltify it.
266 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
268 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
270 * make sure no cached delta data remains from a
271 * previous attempt before a pack split occurred.
273 free(entry
->delta_data
);
274 entry
->delta_data
= NULL
;
275 entry
->z_delta_size
= 0;
276 } else if (entry
->delta_data
) {
277 size
= entry
->delta_size
;
278 buf
= entry
->delta_data
;
279 entry
->delta_data
= NULL
;
280 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
281 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
283 buf
= get_delta(entry
);
284 size
= entry
->delta_size
;
285 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
286 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
289 if (entry
->z_delta_size
)
290 datalen
= entry
->z_delta_size
;
292 datalen
= do_compress(&buf
, size
);
295 * The object header is a byte of 'type' followed by zero or
296 * more bytes of length.
298 hdrlen
= encode_in_pack_object_header(type
, size
, header
);
300 if (type
== OBJ_OFS_DELTA
) {
302 * Deltas with relative base contain an additional
303 * encoding of the relative offset for the delta
304 * base from this object's position in the pack.
306 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
307 unsigned pos
= sizeof(dheader
) - 1;
308 dheader
[pos
] = ofs
& 127;
310 dheader
[--pos
] = 128 | (--ofs
& 127);
311 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
315 sha1write(f
, header
, hdrlen
);
316 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
317 hdrlen
+= sizeof(dheader
) - pos
;
318 } else if (type
== OBJ_REF_DELTA
) {
320 * Deltas with a base reference contain
321 * an additional 20 bytes for the base sha1.
323 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
327 sha1write(f
, header
, hdrlen
);
328 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
331 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
335 sha1write(f
, header
, hdrlen
);
337 sha1write(f
, buf
, datalen
);
341 struct packed_git
*p
= entry
->in_pack
;
342 struct pack_window
*w_curs
= NULL
;
343 struct revindex_entry
*revidx
;
347 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
348 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
349 hdrlen
= encode_in_pack_object_header(type
, entry
->size
, header
);
351 offset
= entry
->in_pack_offset
;
352 revidx
= find_pack_revindex(p
, offset
);
353 datalen
= revidx
[1].offset
- offset
;
354 if (!pack_to_stdout
&& p
->index_version
> 1 &&
355 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
)) {
356 error("bad packed object CRC for %s", sha1_to_hex(entry
->idx
.sha1
));
361 offset
+= entry
->in_pack_header_size
;
362 datalen
-= entry
->in_pack_header_size
;
363 if (!pack_to_stdout
&& p
->index_version
== 1 &&
364 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
)) {
365 error("corrupt packed object for %s", sha1_to_hex(entry
->idx
.sha1
));
370 if (type
== OBJ_OFS_DELTA
) {
371 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
372 unsigned pos
= sizeof(dheader
) - 1;
373 dheader
[pos
] = ofs
& 127;
375 dheader
[--pos
] = 128 | (--ofs
& 127);
376 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
380 sha1write(f
, header
, hdrlen
);
381 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
382 hdrlen
+= sizeof(dheader
) - pos
;
384 } else if (type
== OBJ_REF_DELTA
) {
385 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
389 sha1write(f
, header
, hdrlen
);
390 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
394 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
398 sha1write(f
, header
, hdrlen
);
400 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
408 entry
->idx
.crc32
= crc32_end(f
);
409 return hdrlen
+ datalen
;
412 static int write_one(struct sha1file
*f
,
413 struct object_entry
*e
,
418 /* offset is non zero if object is written already. */
419 if (e
->idx
.offset
|| e
->preferred_base
)
422 /* if we are deltified, write out base object first. */
423 if (e
->delta
&& !write_one(f
, e
->delta
, offset
))
426 e
->idx
.offset
= *offset
;
427 size
= write_object(f
, e
, *offset
);
432 written_list
[nr_written
++] = &e
->idx
;
434 /* make sure off_t is sufficiently large not to wrap */
435 if (signed_add_overflows(*offset
, size
))
436 die("pack too large for current definition of off_t");
441 static int mark_tagged(const char *path
, const unsigned char *sha1
, int flag
,
444 unsigned char peeled
[20];
445 struct object_entry
*entry
= locate_object_entry(sha1
);
449 if (!peel_ref(path
, peeled
)) {
450 entry
= locate_object_entry(peeled
);
457 static inline void add_to_write_order(struct object_entry
**wo
,
459 struct object_entry
*e
)
467 static void add_descendants_to_write_order(struct object_entry
**wo
,
469 struct object_entry
*e
)
471 int add_to_order
= 1;
474 struct object_entry
*s
;
475 /* add this node... */
476 add_to_write_order(wo
, endp
, e
);
477 /* all its siblings... */
478 for (s
= e
->delta_sibling
; s
; s
= s
->delta_sibling
) {
479 add_to_write_order(wo
, endp
, s
);
482 /* drop down a level to add left subtree nodes if possible */
483 if (e
->delta_child
) {
488 /* our sibling might have some children, it is next */
489 if (e
->delta_sibling
) {
490 e
= e
->delta_sibling
;
493 /* go back to our parent node */
495 while (e
&& !e
->delta_sibling
) {
496 /* we're on the right side of a subtree, keep
497 * going up until we can go right again */
501 /* done- we hit our original root node */
504 /* pass it off to sibling at this level */
505 e
= e
->delta_sibling
;
510 static void add_family_to_write_order(struct object_entry
**wo
,
512 struct object_entry
*e
)
514 struct object_entry
*root
;
516 for (root
= e
; root
->delta
; root
= root
->delta
)
518 add_descendants_to_write_order(wo
, endp
, root
);
521 static struct object_entry
**compute_write_order(void)
523 unsigned int i
, wo_end
, last_untagged
;
525 struct object_entry
**wo
= xmalloc(nr_objects
* sizeof(*wo
));
527 for (i
= 0; i
< nr_objects
; i
++) {
528 objects
[i
].tagged
= 0;
529 objects
[i
].filled
= 0;
530 objects
[i
].delta_child
= NULL
;
531 objects
[i
].delta_sibling
= NULL
;
535 * Fully connect delta_child/delta_sibling network.
536 * Make sure delta_sibling is sorted in the original
539 for (i
= nr_objects
; i
> 0;) {
540 struct object_entry
*e
= &objects
[--i
];
543 /* Mark me as the first child */
544 e
->delta_sibling
= e
->delta
->delta_child
;
545 e
->delta
->delta_child
= e
;
549 * Mark objects that are at the tip of tags.
551 for_each_tag_ref(mark_tagged
, NULL
);
554 * Give the objects in the original recency order until
555 * we see a tagged tip.
557 for (i
= wo_end
= 0; i
< nr_objects
; i
++) {
558 if (objects
[i
].tagged
)
560 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
565 * Then fill all the tagged tips.
567 for (; i
< nr_objects
; i
++) {
568 if (objects
[i
].tagged
)
569 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
573 * And then all remaining commits and tags.
575 for (i
= last_untagged
; i
< nr_objects
; i
++) {
576 if (objects
[i
].type
!= OBJ_COMMIT
&&
577 objects
[i
].type
!= OBJ_TAG
)
579 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
583 * And then all the trees.
585 for (i
= last_untagged
; i
< nr_objects
; i
++) {
586 if (objects
[i
].type
!= OBJ_TREE
)
588 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
592 * Finally all the rest in really tight order
594 for (i
= last_untagged
; i
< nr_objects
; i
++) {
595 if (!objects
[i
].filled
)
596 add_family_to_write_order(wo
, &wo_end
, &objects
[i
]);
599 if (wo_end
!= nr_objects
)
600 die("ordered %u objects, expected %"PRIu32
, wo_end
, nr_objects
);
605 static void write_pack_file(void)
610 struct pack_header hdr
;
611 uint32_t nr_remaining
= nr_result
;
612 time_t last_mtime
= 0;
613 struct object_entry
**write_order
;
615 if (progress
> pack_to_stdout
)
616 progress_state
= start_progress("Writing objects", nr_result
);
617 written_list
= xmalloc(nr_objects
* sizeof(*written_list
));
618 write_order
= compute_write_order();
621 unsigned char sha1
[20];
622 char *pack_tmp_name
= NULL
;
624 if (pack_to_stdout
) {
625 f
= sha1fd_throughput(1, "<stdout>", progress_state
);
627 char tmpname
[PATH_MAX
];
629 fd
= odb_mkstemp(tmpname
, sizeof(tmpname
),
630 "pack/tmp_pack_XXXXXX");
631 pack_tmp_name
= xstrdup(tmpname
);
632 f
= sha1fd(fd
, pack_tmp_name
);
635 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
636 hdr
.hdr_version
= htonl(PACK_VERSION
);
637 hdr
.hdr_entries
= htonl(nr_remaining
);
638 sha1write(f
, &hdr
, sizeof(hdr
));
639 offset
= sizeof(hdr
);
641 for (; i
< nr_objects
; i
++) {
642 struct object_entry
*e
= write_order
[i
];
643 if (!write_one(f
, e
, &offset
))
645 display_progress(progress_state
, written
);
649 * Did we write the wrong # entries in the header?
650 * If so, rewrite it like in fast-import
652 if (pack_to_stdout
) {
653 sha1close(f
, sha1
, CSUM_CLOSE
);
654 } else if (nr_written
== nr_remaining
) {
655 sha1close(f
, sha1
, CSUM_FSYNC
);
657 int fd
= sha1close(f
, sha1
, 0);
658 fixup_pack_header_footer(fd
, sha1
, pack_tmp_name
,
659 nr_written
, sha1
, offset
);
663 if (!pack_to_stdout
) {
665 const char *idx_tmp_name
;
666 char tmpname
[PATH_MAX
];
668 idx_tmp_name
= write_idx_file(NULL
, written_list
, nr_written
,
669 &pack_idx_opts
, sha1
);
671 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.pack",
672 base_name
, sha1_to_hex(sha1
));
673 free_pack_by_name(tmpname
);
674 if (adjust_shared_perm(pack_tmp_name
))
675 die_errno("unable to make temporary pack file readable");
676 if (rename(pack_tmp_name
, tmpname
))
677 die_errno("unable to rename temporary pack file");
680 * Packs are runtime accessed in their mtime
681 * order since newer packs are more likely to contain
682 * younger objects. So if we are creating multiple
683 * packs then we should modify the mtime of later ones
684 * to preserve this property.
686 if (stat(tmpname
, &st
) < 0) {
687 warning("failed to stat %s: %s",
688 tmpname
, strerror(errno
));
689 } else if (!last_mtime
) {
690 last_mtime
= st
.st_mtime
;
693 utb
.actime
= st
.st_atime
;
694 utb
.modtime
= --last_mtime
;
695 if (utime(tmpname
, &utb
) < 0)
696 warning("failed utime() on %s: %s",
697 tmpname
, strerror(errno
));
700 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.idx",
701 base_name
, sha1_to_hex(sha1
));
702 if (adjust_shared_perm(idx_tmp_name
))
703 die_errno("unable to make temporary index file readable");
704 if (rename(idx_tmp_name
, tmpname
))
705 die_errno("unable to rename temporary index file");
707 free((void *) idx_tmp_name
);
709 puts(sha1_to_hex(sha1
));
712 /* mark written objects as written to previous pack */
713 for (j
= 0; j
< nr_written
; j
++) {
714 written_list
[j
]->offset
= (off_t
)-1;
716 nr_remaining
-= nr_written
;
717 } while (nr_remaining
&& i
< nr_objects
);
721 stop_progress(&progress_state
);
722 if (written
!= nr_result
)
723 die("wrote %"PRIu32
" objects while expecting %"PRIu32
,
727 static int locate_object_entry_hash(const unsigned char *sha1
)
731 memcpy(&ui
, sha1
, sizeof(unsigned int));
732 i
= ui
% object_ix_hashsz
;
733 while (0 < object_ix
[i
]) {
734 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].idx
.sha1
))
736 if (++i
== object_ix_hashsz
)
742 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
746 if (!object_ix_hashsz
)
749 i
= locate_object_entry_hash(sha1
);
751 return &objects
[object_ix
[i
]-1];
755 static void rehash_objects(void)
758 struct object_entry
*oe
;
760 object_ix_hashsz
= nr_objects
* 3;
761 if (object_ix_hashsz
< 1024)
762 object_ix_hashsz
= 1024;
763 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
764 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
765 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
766 int ix
= locate_object_entry_hash(oe
->idx
.sha1
);
770 object_ix
[ix
] = i
+ 1;
774 static unsigned name_hash(const char *name
)
776 unsigned c
, hash
= 0;
782 * This effectively just creates a sortable number from the
783 * last sixteen non-whitespace characters. Last characters
784 * count "most", so things that end in ".c" sort together.
786 while ((c
= *name
++) != 0) {
789 hash
= (hash
>> 2) + (c
<< 24);
794 static void setup_delta_attr_check(struct git_attr_check
*check
)
796 static struct git_attr
*attr_delta
;
799 attr_delta
= git_attr("delta");
801 check
[0].attr
= attr_delta
;
804 static int no_try_delta(const char *path
)
806 struct git_attr_check check
[1];
808 setup_delta_attr_check(check
);
809 if (git_check_attr(path
, ARRAY_SIZE(check
), check
))
811 if (ATTR_FALSE(check
->value
))
816 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
817 const char *name
, int exclude
)
819 struct object_entry
*entry
;
820 struct packed_git
*p
, *found_pack
= NULL
;
821 off_t found_offset
= 0;
823 unsigned hash
= name_hash(name
);
825 ix
= nr_objects
? locate_object_entry_hash(sha1
) : -1;
828 entry
= objects
+ object_ix
[ix
] - 1;
829 if (!entry
->preferred_base
)
831 entry
->preferred_base
= 1;
836 if (!exclude
&& local
&& has_loose_object_nonlocal(sha1
))
839 for (p
= packed_git
; p
; p
= p
->next
) {
840 off_t offset
= find_pack_entry_one(sha1
, p
);
843 if (!is_pack_valid(p
)) {
844 warning("packfile %s cannot be accessed", p
->pack_name
);
847 found_offset
= offset
;
854 if (local
&& !p
->pack_local
)
856 if (ignore_packed_keep
&& p
->pack_local
&& p
->pack_keep
)
861 if (nr_objects
>= nr_alloc
) {
862 nr_alloc
= (nr_alloc
+ 1024) * 3 / 2;
863 objects
= xrealloc(objects
, nr_alloc
* sizeof(*entry
));
866 entry
= objects
+ nr_objects
++;
867 memset(entry
, 0, sizeof(*entry
));
868 hashcpy(entry
->idx
.sha1
, sha1
);
873 entry
->preferred_base
= 1;
877 entry
->in_pack
= found_pack
;
878 entry
->in_pack_offset
= found_offset
;
881 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
884 object_ix
[-1 - ix
] = nr_objects
;
886 display_progress(progress_state
, nr_objects
);
888 if (name
&& no_try_delta(name
))
889 entry
->no_try_delta
= 1;
894 struct pbase_tree_cache
{
895 unsigned char sha1
[20];
899 unsigned long tree_size
;
902 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
903 static int pbase_tree_cache_ix(const unsigned char *sha1
)
905 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
907 static int pbase_tree_cache_ix_incr(int ix
)
909 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
912 static struct pbase_tree
{
913 struct pbase_tree
*next
;
914 /* This is a phony "cache" entry; we are not
915 * going to evict it nor find it through _get()
916 * mechanism -- this is for the toplevel node that
917 * would almost always change with any commit.
919 struct pbase_tree_cache pcache
;
922 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
924 struct pbase_tree_cache
*ent
, *nent
;
927 enum object_type type
;
929 int my_ix
= pbase_tree_cache_ix(sha1
);
930 int available_ix
= -1;
932 /* pbase-tree-cache acts as a limited hashtable.
933 * your object will be found at your index or within a few
934 * slots after that slot if it is cached.
936 for (neigh
= 0; neigh
< 8; neigh
++) {
937 ent
= pbase_tree_cache
[my_ix
];
938 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
942 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
943 ((0 <= available_ix
) &&
944 (!ent
&& pbase_tree_cache
[available_ix
])))
945 available_ix
= my_ix
;
948 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
951 /* Did not find one. Either we got a bogus request or
952 * we need to read and perhaps cache.
954 data
= read_sha1_file(sha1
, &type
, &size
);
957 if (type
!= OBJ_TREE
) {
962 /* We need to either cache or return a throwaway copy */
964 if (available_ix
< 0)
967 ent
= pbase_tree_cache
[available_ix
];
968 my_ix
= available_ix
;
972 nent
= xmalloc(sizeof(*nent
));
973 nent
->temporary
= (available_ix
< 0);
976 /* evict and reuse */
977 free(ent
->tree_data
);
980 hashcpy(nent
->sha1
, sha1
);
981 nent
->tree_data
= data
;
982 nent
->tree_size
= size
;
984 if (!nent
->temporary
)
985 pbase_tree_cache
[my_ix
] = nent
;
989 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
991 if (!cache
->temporary
) {
995 free(cache
->tree_data
);
999 static int name_cmp_len(const char *name
)
1002 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
1007 static void add_pbase_object(struct tree_desc
*tree
,
1010 const char *fullname
)
1012 struct name_entry entry
;
1015 while (tree_entry(tree
,&entry
)) {
1016 if (S_ISGITLINK(entry
.mode
))
1018 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
1019 memcmp(name
, entry
.path
, cmplen
);
1024 if (name
[cmplen
] != '/') {
1025 add_object_entry(entry
.sha1
,
1026 object_type(entry
.mode
),
1030 if (S_ISDIR(entry
.mode
)) {
1031 struct tree_desc sub
;
1032 struct pbase_tree_cache
*tree
;
1033 const char *down
= name
+cmplen
+1;
1034 int downlen
= name_cmp_len(down
);
1036 tree
= pbase_tree_get(entry
.sha1
);
1039 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1041 add_pbase_object(&sub
, down
, downlen
, fullname
);
1042 pbase_tree_put(tree
);
1047 static unsigned *done_pbase_paths
;
1048 static int done_pbase_paths_num
;
1049 static int done_pbase_paths_alloc
;
1050 static int done_pbase_path_pos(unsigned hash
)
1053 int hi
= done_pbase_paths_num
;
1055 int mi
= (hi
+ lo
) / 2;
1056 if (done_pbase_paths
[mi
] == hash
)
1058 if (done_pbase_paths
[mi
] < hash
)
1066 static int check_pbase_path(unsigned hash
)
1068 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1072 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1073 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1074 done_pbase_paths
= xrealloc(done_pbase_paths
,
1075 done_pbase_paths_alloc
*
1078 done_pbase_paths_num
++;
1079 if (pos
< done_pbase_paths_num
)
1080 memmove(done_pbase_paths
+ pos
+ 1,
1081 done_pbase_paths
+ pos
,
1082 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1083 done_pbase_paths
[pos
] = hash
;
1087 static void add_preferred_base_object(const char *name
)
1089 struct pbase_tree
*it
;
1091 unsigned hash
= name_hash(name
);
1093 if (!num_preferred_base
|| check_pbase_path(hash
))
1096 cmplen
= name_cmp_len(name
);
1097 for (it
= pbase_tree
; it
; it
= it
->next
) {
1099 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, NULL
, 1);
1102 struct tree_desc tree
;
1103 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1104 add_pbase_object(&tree
, name
, cmplen
, name
);
1109 static void add_preferred_base(unsigned char *sha1
)
1111 struct pbase_tree
*it
;
1114 unsigned char tree_sha1
[20];
1116 if (window
<= num_preferred_base
++)
1119 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1123 for (it
= pbase_tree
; it
; it
= it
->next
) {
1124 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1130 it
= xcalloc(1, sizeof(*it
));
1131 it
->next
= pbase_tree
;
1134 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1135 it
->pcache
.tree_data
= data
;
1136 it
->pcache
.tree_size
= size
;
1139 static void cleanup_preferred_base(void)
1141 struct pbase_tree
*it
;
1147 struct pbase_tree
*this = it
;
1149 free(this->pcache
.tree_data
);
1153 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1154 if (!pbase_tree_cache
[i
])
1156 free(pbase_tree_cache
[i
]->tree_data
);
1157 free(pbase_tree_cache
[i
]);
1158 pbase_tree_cache
[i
] = NULL
;
1161 free(done_pbase_paths
);
1162 done_pbase_paths
= NULL
;
1163 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1166 static void check_object(struct object_entry
*entry
)
1168 if (entry
->in_pack
) {
1169 struct packed_git
*p
= entry
->in_pack
;
1170 struct pack_window
*w_curs
= NULL
;
1171 const unsigned char *base_ref
= NULL
;
1172 struct object_entry
*base_entry
;
1173 unsigned long used
, used_0
;
1174 unsigned long avail
;
1176 unsigned char *buf
, c
;
1178 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1181 * We want in_pack_type even if we do not reuse delta
1182 * since non-delta representations could still be reused.
1184 used
= unpack_object_header_buffer(buf
, avail
,
1185 &entry
->in_pack_type
,
1191 * Determine if this is a delta and if so whether we can
1192 * reuse it or not. Otherwise let's find out as cheaply as
1193 * possible what the actual type and size for this object is.
1195 switch (entry
->in_pack_type
) {
1197 /* Not a delta hence we've already got all we need. */
1198 entry
->type
= entry
->in_pack_type
;
1199 entry
->in_pack_header_size
= used
;
1200 if (entry
->type
< OBJ_COMMIT
|| entry
->type
> OBJ_BLOB
)
1202 unuse_pack(&w_curs
);
1205 if (reuse_delta
&& !entry
->preferred_base
)
1206 base_ref
= use_pack(p
, &w_curs
,
1207 entry
->in_pack_offset
+ used
, NULL
);
1208 entry
->in_pack_header_size
= used
+ 20;
1211 buf
= use_pack(p
, &w_curs
,
1212 entry
->in_pack_offset
+ used
, NULL
);
1218 if (!ofs
|| MSB(ofs
, 7)) {
1219 error("delta base offset overflow in pack for %s",
1220 sha1_to_hex(entry
->idx
.sha1
));
1224 ofs
= (ofs
<< 7) + (c
& 127);
1226 ofs
= entry
->in_pack_offset
- ofs
;
1227 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
1228 error("delta base offset out of bound for %s",
1229 sha1_to_hex(entry
->idx
.sha1
));
1232 if (reuse_delta
&& !entry
->preferred_base
) {
1233 struct revindex_entry
*revidx
;
1234 revidx
= find_pack_revindex(p
, ofs
);
1237 base_ref
= nth_packed_object_sha1(p
, revidx
->nr
);
1239 entry
->in_pack_header_size
= used
+ used_0
;
1243 if (base_ref
&& (base_entry
= locate_object_entry(base_ref
))) {
1245 * If base_ref was set above that means we wish to
1246 * reuse delta data, and we even found that base
1247 * in the list of objects we want to pack. Goodie!
1249 * Depth value does not matter - find_deltas() will
1250 * never consider reused delta as the base object to
1251 * deltify other objects against, in order to avoid
1254 entry
->type
= entry
->in_pack_type
;
1255 entry
->delta
= base_entry
;
1256 entry
->delta_size
= entry
->size
;
1257 entry
->delta_sibling
= base_entry
->delta_child
;
1258 base_entry
->delta_child
= entry
;
1259 unuse_pack(&w_curs
);
1265 * This must be a delta and we already know what the
1266 * final object type is. Let's extract the actual
1267 * object size from the delta header.
1269 entry
->size
= get_size_from_delta(p
, &w_curs
,
1270 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1271 if (entry
->size
== 0)
1273 unuse_pack(&w_curs
);
1278 * No choice but to fall back to the recursive delta walk
1279 * with sha1_object_info() to find about the object type
1283 unuse_pack(&w_curs
);
1286 entry
->type
= sha1_object_info(entry
->idx
.sha1
, &entry
->size
);
1288 * The error condition is checked in prepare_pack(). This is
1289 * to permit a missing preferred base object to be ignored
1290 * as a preferred base. Doing so can result in a larger
1291 * pack file, but the transfer will still take place.
1295 static int pack_offset_sort(const void *_a
, const void *_b
)
1297 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1298 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1300 /* avoid filesystem trashing with loose objects */
1301 if (!a
->in_pack
&& !b
->in_pack
)
1302 return hashcmp(a
->idx
.sha1
, b
->idx
.sha1
);
1304 if (a
->in_pack
< b
->in_pack
)
1306 if (a
->in_pack
> b
->in_pack
)
1308 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1309 (a
->in_pack_offset
> b
->in_pack_offset
);
1312 static void get_object_details(void)
1315 struct object_entry
**sorted_by_offset
;
1317 sorted_by_offset
= xcalloc(nr_objects
, sizeof(struct object_entry
*));
1318 for (i
= 0; i
< nr_objects
; i
++)
1319 sorted_by_offset
[i
] = objects
+ i
;
1320 qsort(sorted_by_offset
, nr_objects
, sizeof(*sorted_by_offset
), pack_offset_sort
);
1322 for (i
= 0; i
< nr_objects
; i
++) {
1323 struct object_entry
*entry
= sorted_by_offset
[i
];
1324 check_object(entry
);
1325 if (big_file_threshold
<= entry
->size
)
1326 entry
->no_try_delta
= 1;
1329 free(sorted_by_offset
);
1333 * We search for deltas in a list sorted by type, by filename hash, and then
1334 * by size, so that we see progressively smaller and smaller files.
1335 * That's because we prefer deltas to be from the bigger file
1336 * to the smaller -- deletes are potentially cheaper, but perhaps
1337 * more importantly, the bigger file is likely the more recent
1338 * one. The deepest deltas are therefore the oldest objects which are
1339 * less susceptible to be accessed often.
1341 static int type_size_sort(const void *_a
, const void *_b
)
1343 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1344 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1346 if (a
->type
> b
->type
)
1348 if (a
->type
< b
->type
)
1350 if (a
->hash
> b
->hash
)
1352 if (a
->hash
< b
->hash
)
1354 if (a
->preferred_base
> b
->preferred_base
)
1356 if (a
->preferred_base
< b
->preferred_base
)
1358 if (a
->size
> b
->size
)
1360 if (a
->size
< b
->size
)
1362 return a
< b
? -1 : (a
> b
); /* newest first */
1366 struct object_entry
*entry
;
1368 struct delta_index
*index
;
1372 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
1373 unsigned long delta_size
)
1375 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
1378 if (delta_size
< cache_max_small_delta_size
)
1381 /* cache delta, if objects are large enough compared to delta size */
1382 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
1390 static pthread_mutex_t read_mutex
;
1391 #define read_lock() pthread_mutex_lock(&read_mutex)
1392 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1394 static pthread_mutex_t cache_mutex
;
1395 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1396 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1398 static pthread_mutex_t progress_mutex
;
1399 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1400 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1404 #define read_lock() (void)0
1405 #define read_unlock() (void)0
1406 #define cache_lock() (void)0
1407 #define cache_unlock() (void)0
1408 #define progress_lock() (void)0
1409 #define progress_unlock() (void)0
1413 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1414 unsigned max_depth
, unsigned long *mem_usage
)
1416 struct object_entry
*trg_entry
= trg
->entry
;
1417 struct object_entry
*src_entry
= src
->entry
;
1418 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1420 enum object_type type
;
1423 /* Don't bother doing diffs between different types */
1424 if (trg_entry
->type
!= src_entry
->type
)
1428 * We do not bother to try a delta that we discarded
1429 * on an earlier try, but only when reusing delta data.
1431 if (reuse_delta
&& trg_entry
->in_pack
&&
1432 trg_entry
->in_pack
== src_entry
->in_pack
&&
1433 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1434 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1437 /* Let's not bust the allowed depth. */
1438 if (src
->depth
>= max_depth
)
1441 /* Now some size filtering heuristics. */
1442 trg_size
= trg_entry
->size
;
1443 if (!trg_entry
->delta
) {
1444 max_size
= trg_size
/2 - 20;
1447 max_size
= trg_entry
->delta_size
;
1448 ref_depth
= trg
->depth
;
1450 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
1451 (max_depth
- ref_depth
+ 1);
1454 src_size
= src_entry
->size
;
1455 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1456 if (sizediff
>= max_size
)
1458 if (trg_size
< src_size
/ 32)
1461 /* Load data if not already done */
1464 trg
->data
= read_sha1_file(trg_entry
->idx
.sha1
, &type
, &sz
);
1467 die("object %s cannot be read",
1468 sha1_to_hex(trg_entry
->idx
.sha1
));
1470 die("object %s inconsistent object length (%lu vs %lu)",
1471 sha1_to_hex(trg_entry
->idx
.sha1
), sz
, trg_size
);
1476 src
->data
= read_sha1_file(src_entry
->idx
.sha1
, &type
, &sz
);
1479 if (src_entry
->preferred_base
) {
1480 static int warned
= 0;
1482 warning("object %s cannot be read",
1483 sha1_to_hex(src_entry
->idx
.sha1
));
1485 * Those objects are not included in the
1486 * resulting pack. Be resilient and ignore
1487 * them if they can't be read, in case the
1488 * pack could be created nevertheless.
1492 die("object %s cannot be read",
1493 sha1_to_hex(src_entry
->idx
.sha1
));
1496 die("object %s inconsistent object length (%lu vs %lu)",
1497 sha1_to_hex(src_entry
->idx
.sha1
), sz
, src_size
);
1501 src
->index
= create_delta_index(src
->data
, src_size
);
1503 static int warned
= 0;
1505 warning("suboptimal pack - out of memory");
1508 *mem_usage
+= sizeof_delta_index(src
->index
);
1511 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1515 if (trg_entry
->delta
) {
1516 /* Prefer only shallower same-sized deltas. */
1517 if (delta_size
== trg_entry
->delta_size
&&
1518 src
->depth
+ 1 >= trg
->depth
) {
1525 * Handle memory allocation outside of the cache
1526 * accounting lock. Compiler will optimize the strangeness
1527 * away when NO_PTHREADS is defined.
1529 free(trg_entry
->delta_data
);
1531 if (trg_entry
->delta_data
) {
1532 delta_cache_size
-= trg_entry
->delta_size
;
1533 trg_entry
->delta_data
= NULL
;
1535 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
1536 delta_cache_size
+= delta_size
;
1538 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
1544 trg_entry
->delta
= src_entry
;
1545 trg_entry
->delta_size
= delta_size
;
1546 trg
->depth
= src
->depth
+ 1;
1551 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1553 struct object_entry
*child
= me
->delta_child
;
1556 unsigned int c
= check_delta_limit(child
, n
+ 1);
1559 child
= child
->delta_sibling
;
1564 static unsigned long free_unpacked(struct unpacked
*n
)
1566 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
1567 free_delta_index(n
->index
);
1570 freed_mem
+= n
->entry
->size
;
1579 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
1580 int window
, int depth
, unsigned *processed
)
1582 uint32_t i
, idx
= 0, count
= 0;
1583 struct unpacked
*array
;
1584 unsigned long mem_usage
= 0;
1586 array
= xcalloc(window
, sizeof(struct unpacked
));
1589 struct object_entry
*entry
;
1590 struct unpacked
*n
= array
+ idx
;
1591 int j
, max_depth
, best_base
= -1;
1600 if (!entry
->preferred_base
) {
1602 display_progress(progress_state
, *processed
);
1606 mem_usage
-= free_unpacked(n
);
1609 while (window_memory_limit
&&
1610 mem_usage
> window_memory_limit
&&
1612 uint32_t tail
= (idx
+ window
- count
) % window
;
1613 mem_usage
-= free_unpacked(array
+ tail
);
1617 /* We do not compute delta to *create* objects we are not
1620 if (entry
->preferred_base
)
1624 * If the current object is at pack edge, take the depth the
1625 * objects that depend on the current object into account
1626 * otherwise they would become too deep.
1629 if (entry
->delta_child
) {
1630 max_depth
-= check_delta_limit(entry
, 0);
1638 uint32_t other_idx
= idx
+ j
;
1640 if (other_idx
>= window
)
1641 other_idx
-= window
;
1642 m
= array
+ other_idx
;
1645 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
1649 best_base
= other_idx
;
1653 * If we decided to cache the delta data, then it is best
1654 * to compress it right away. First because we have to do
1655 * it anyway, and doing it here while we're threaded will
1656 * save a lot of time in the non threaded write phase,
1657 * as well as allow for caching more deltas within
1658 * the same cache size limit.
1660 * But only if not writing to stdout, since in that case
1661 * the network is most likely throttling writes anyway,
1662 * and therefore it is best to go to the write phase ASAP
1663 * instead, as we can afford spending more time compressing
1664 * between writes at that moment.
1666 if (entry
->delta_data
&& !pack_to_stdout
) {
1667 entry
->z_delta_size
= do_compress(&entry
->delta_data
,
1670 delta_cache_size
-= entry
->delta_size
;
1671 delta_cache_size
+= entry
->z_delta_size
;
1675 /* if we made n a delta, and if n is already at max
1676 * depth, leaving it in the window is pointless. we
1677 * should evict it first.
1679 if (entry
->delta
&& max_depth
<= n
->depth
)
1683 * Move the best delta base up in the window, after the
1684 * currently deltified object, to keep it longer. It will
1685 * be the first base object to be attempted next.
1688 struct unpacked swap
= array
[best_base
];
1689 int dist
= (window
+ idx
- best_base
) % window
;
1690 int dst
= best_base
;
1692 int src
= (dst
+ 1) % window
;
1693 array
[dst
] = array
[src
];
1701 if (count
+ 1 < window
)
1707 for (i
= 0; i
< window
; ++i
) {
1708 free_delta_index(array
[i
].index
);
1709 free(array
[i
].data
);
1716 static void try_to_free_from_threads(size_t size
)
1719 release_pack_memory(size
, -1);
1723 static try_to_free_t old_try_to_free_routine
;
1726 * The main thread waits on the condition that (at least) one of the workers
1727 * has stopped working (which is indicated in the .working member of
1728 * struct thread_params).
1729 * When a work thread has completed its work, it sets .working to 0 and
1730 * signals the main thread and waits on the condition that .data_ready
1734 struct thread_params
{
1736 struct object_entry
**list
;
1743 pthread_mutex_t mutex
;
1744 pthread_cond_t cond
;
1745 unsigned *processed
;
1748 static pthread_cond_t progress_cond
;
1751 * Mutex and conditional variable can't be statically-initialized on Windows.
1753 static void init_threaded_search(void)
1755 init_recursive_mutex(&read_mutex
);
1756 pthread_mutex_init(&cache_mutex
, NULL
);
1757 pthread_mutex_init(&progress_mutex
, NULL
);
1758 pthread_cond_init(&progress_cond
, NULL
);
1759 old_try_to_free_routine
= set_try_to_free_routine(try_to_free_from_threads
);
1762 static void cleanup_threaded_search(void)
1764 set_try_to_free_routine(old_try_to_free_routine
);
1765 pthread_cond_destroy(&progress_cond
);
1766 pthread_mutex_destroy(&read_mutex
);
1767 pthread_mutex_destroy(&cache_mutex
);
1768 pthread_mutex_destroy(&progress_mutex
);
1771 static void *threaded_find_deltas(void *arg
)
1773 struct thread_params
*me
= arg
;
1775 while (me
->remaining
) {
1776 find_deltas(me
->list
, &me
->remaining
,
1777 me
->window
, me
->depth
, me
->processed
);
1781 pthread_cond_signal(&progress_cond
);
1785 * We must not set ->data_ready before we wait on the
1786 * condition because the main thread may have set it to 1
1787 * before we get here. In order to be sure that new
1788 * work is available if we see 1 in ->data_ready, it
1789 * was initialized to 0 before this thread was spawned
1790 * and we reset it to 0 right away.
1792 pthread_mutex_lock(&me
->mutex
);
1793 while (!me
->data_ready
)
1794 pthread_cond_wait(&me
->cond
, &me
->mutex
);
1796 pthread_mutex_unlock(&me
->mutex
);
1798 /* leave ->working 1 so that this doesn't get more work assigned */
1802 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
1803 int window
, int depth
, unsigned *processed
)
1805 struct thread_params
*p
;
1806 int i
, ret
, active_threads
= 0;
1808 init_threaded_search();
1810 if (!delta_search_threads
) /* --threads=0 means autodetect */
1811 delta_search_threads
= online_cpus();
1812 if (delta_search_threads
<= 1) {
1813 find_deltas(list
, &list_size
, window
, depth
, processed
);
1814 cleanup_threaded_search();
1817 if (progress
> pack_to_stdout
)
1818 fprintf(stderr
, "Delta compression using up to %d threads.\n",
1819 delta_search_threads
);
1820 p
= xcalloc(delta_search_threads
, sizeof(*p
));
1822 /* Partition the work amongst work threads. */
1823 for (i
= 0; i
< delta_search_threads
; i
++) {
1824 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
1826 /* don't use too small segments or no deltas will be found */
1827 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
1830 p
[i
].window
= window
;
1832 p
[i
].processed
= processed
;
1834 p
[i
].data_ready
= 0;
1836 /* try to split chunks on "path" boundaries */
1837 while (sub_size
&& sub_size
< list_size
&&
1838 list
[sub_size
]->hash
&&
1839 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
1843 p
[i
].list_size
= sub_size
;
1844 p
[i
].remaining
= sub_size
;
1847 list_size
-= sub_size
;
1850 /* Start work threads. */
1851 for (i
= 0; i
< delta_search_threads
; i
++) {
1852 if (!p
[i
].list_size
)
1854 pthread_mutex_init(&p
[i
].mutex
, NULL
);
1855 pthread_cond_init(&p
[i
].cond
, NULL
);
1856 ret
= pthread_create(&p
[i
].thread
, NULL
,
1857 threaded_find_deltas
, &p
[i
]);
1859 die("unable to create thread: %s", strerror(ret
));
1864 * Now let's wait for work completion. Each time a thread is done
1865 * with its work, we steal half of the remaining work from the
1866 * thread with the largest number of unprocessed objects and give
1867 * it to that newly idle thread. This ensure good load balancing
1868 * until the remaining object list segments are simply too short
1869 * to be worth splitting anymore.
1871 while (active_threads
) {
1872 struct thread_params
*target
= NULL
;
1873 struct thread_params
*victim
= NULL
;
1874 unsigned sub_size
= 0;
1878 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
1883 pthread_cond_wait(&progress_cond
, &progress_mutex
);
1886 for (i
= 0; i
< delta_search_threads
; i
++)
1887 if (p
[i
].remaining
> 2*window
&&
1888 (!victim
|| victim
->remaining
< p
[i
].remaining
))
1891 sub_size
= victim
->remaining
/ 2;
1892 list
= victim
->list
+ victim
->list_size
- sub_size
;
1893 while (sub_size
&& list
[0]->hash
&&
1894 list
[0]->hash
== list
[-1]->hash
) {
1900 * It is possible for some "paths" to have
1901 * so many objects that no hash boundary
1902 * might be found. Let's just steal the
1903 * exact half in that case.
1905 sub_size
= victim
->remaining
/ 2;
1908 target
->list
= list
;
1909 victim
->list_size
-= sub_size
;
1910 victim
->remaining
-= sub_size
;
1912 target
->list_size
= sub_size
;
1913 target
->remaining
= sub_size
;
1914 target
->working
= 1;
1917 pthread_mutex_lock(&target
->mutex
);
1918 target
->data_ready
= 1;
1919 pthread_cond_signal(&target
->cond
);
1920 pthread_mutex_unlock(&target
->mutex
);
1923 pthread_join(target
->thread
, NULL
);
1924 pthread_cond_destroy(&target
->cond
);
1925 pthread_mutex_destroy(&target
->mutex
);
1929 cleanup_threaded_search();
1934 #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
1937 static int add_ref_tag(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
1939 unsigned char peeled
[20];
1941 if (!prefixcmp(path
, "refs/tags/") && /* is a tag? */
1942 !peel_ref(path
, peeled
) && /* peelable? */
1943 !is_null_sha1(peeled
) && /* annotated tag? */
1944 locate_object_entry(peeled
)) /* object packed? */
1945 add_object_entry(sha1
, OBJ_TAG
, NULL
, 0);
1949 static void prepare_pack(int window
, int depth
)
1951 struct object_entry
**delta_list
;
1952 uint32_t i
, nr_deltas
;
1955 get_object_details();
1958 * If we're locally repacking then we need to be doubly careful
1959 * from now on in order to make sure no stealth corruption gets
1960 * propagated to the new pack. Clients receiving streamed packs
1961 * should validate everything they get anyway so no need to incur
1962 * the additional cost here in that case.
1964 if (!pack_to_stdout
)
1965 do_check_packed_object_crc
= 1;
1967 if (!nr_objects
|| !window
|| !depth
)
1970 delta_list
= xmalloc(nr_objects
* sizeof(*delta_list
));
1973 for (i
= 0; i
< nr_objects
; i
++) {
1974 struct object_entry
*entry
= objects
+ i
;
1977 /* This happens if we decided to reuse existing
1978 * delta from a pack. "reuse_delta &&" is implied.
1982 if (entry
->size
< 50)
1985 if (entry
->no_try_delta
)
1988 if (!entry
->preferred_base
) {
1990 if (entry
->type
< 0)
1991 die("unable to get type of object %s",
1992 sha1_to_hex(entry
->idx
.sha1
));
1994 if (entry
->type
< 0) {
1996 * This object is not found, but we
1997 * don't have to include it anyway.
2003 delta_list
[n
++] = entry
;
2006 if (nr_deltas
&& n
> 1) {
2007 unsigned nr_done
= 0;
2009 progress_state
= start_progress("Compressing objects",
2011 qsort(delta_list
, n
, sizeof(*delta_list
), type_size_sort
);
2012 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
2013 stop_progress(&progress_state
);
2014 if (nr_done
!= nr_deltas
)
2015 die("inconsistency with delta count");
2020 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2022 if (!strcmp(k
, "pack.window")) {
2023 window
= git_config_int(k
, v
);
2026 if (!strcmp(k
, "pack.windowmemory")) {
2027 window_memory_limit
= git_config_ulong(k
, v
);
2030 if (!strcmp(k
, "pack.depth")) {
2031 depth
= git_config_int(k
, v
);
2034 if (!strcmp(k
, "pack.compression")) {
2035 int level
= git_config_int(k
, v
);
2037 level
= Z_DEFAULT_COMPRESSION
;
2038 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
2039 die("bad pack compression level %d", level
);
2040 pack_compression_level
= level
;
2041 pack_compression_seen
= 1;
2044 if (!strcmp(k
, "pack.deltacachesize")) {
2045 max_delta_cache_size
= git_config_int(k
, v
);
2048 if (!strcmp(k
, "pack.deltacachelimit")) {
2049 cache_max_small_delta_size
= git_config_int(k
, v
);
2052 if (!strcmp(k
, "pack.threads")) {
2053 delta_search_threads
= git_config_int(k
, v
);
2054 if (delta_search_threads
< 0)
2055 die("invalid number of threads specified (%d)",
2056 delta_search_threads
);
2058 if (delta_search_threads
!= 1)
2059 warning("no threads support, ignoring %s", k
);
2063 if (!strcmp(k
, "pack.indexversion")) {
2064 pack_idx_opts
.version
= git_config_int(k
, v
);
2065 if (pack_idx_opts
.version
> 2)
2066 die("bad pack.indexversion=%"PRIu32
,
2067 pack_idx_opts
.version
);
2070 if (!strcmp(k
, "pack.packsizelimit")) {
2071 pack_size_limit_cfg
= git_config_ulong(k
, v
);
2074 return git_default_config(k
, v
, cb
);
2077 static void read_object_list_from_stdin(void)
2079 char line
[40 + 1 + PATH_MAX
+ 2];
2080 unsigned char sha1
[20];
2083 if (!fgets(line
, sizeof(line
), stdin
)) {
2087 die("fgets returned NULL, not EOF, not error!");
2093 if (line
[0] == '-') {
2094 if (get_sha1_hex(line
+1, sha1
))
2095 die("expected edge sha1, got garbage:\n %s",
2097 add_preferred_base(sha1
);
2100 if (get_sha1_hex(line
, sha1
))
2101 die("expected sha1, got garbage:\n %s", line
);
2103 add_preferred_base_object(line
+41);
2104 add_object_entry(sha1
, 0, line
+41, 0);
2108 #define OBJECT_ADDED (1u<<20)
2110 static void show_commit(struct commit
*commit
, void *data
)
2112 add_object_entry(commit
->object
.sha1
, OBJ_COMMIT
, NULL
, 0);
2113 commit
->object
.flags
|= OBJECT_ADDED
;
2116 static void show_object(struct object
*obj
,
2117 const struct name_path
*path
, const char *last
,
2120 char *name
= path_name(path
, last
);
2122 add_preferred_base_object(name
);
2123 add_object_entry(obj
->sha1
, obj
->type
, name
, 0);
2124 obj
->flags
|= OBJECT_ADDED
;
2127 * We will have generated the hash from the name,
2128 * but not saved a pointer to it - we can free it
2133 static void show_edge(struct commit
*commit
)
2135 add_preferred_base(commit
->object
.sha1
);
2138 struct in_pack_object
{
2140 struct object
*object
;
2146 struct in_pack_object
*array
;
2149 static void mark_in_pack_object(struct object
*object
, struct packed_git
*p
, struct in_pack
*in_pack
)
2151 in_pack
->array
[in_pack
->nr
].offset
= find_pack_entry_one(object
->sha1
, p
);
2152 in_pack
->array
[in_pack
->nr
].object
= object
;
2157 * Compare the objects in the offset order, in order to emulate the
2158 * "git rev-list --objects" output that produced the pack originally.
2160 static int ofscmp(const void *a_
, const void *b_
)
2162 struct in_pack_object
*a
= (struct in_pack_object
*)a_
;
2163 struct in_pack_object
*b
= (struct in_pack_object
*)b_
;
2165 if (a
->offset
< b
->offset
)
2167 else if (a
->offset
> b
->offset
)
2170 return hashcmp(a
->object
->sha1
, b
->object
->sha1
);
2173 static void add_objects_in_unpacked_packs(struct rev_info
*revs
)
2175 struct packed_git
*p
;
2176 struct in_pack in_pack
;
2179 memset(&in_pack
, 0, sizeof(in_pack
));
2181 for (p
= packed_git
; p
; p
= p
->next
) {
2182 const unsigned char *sha1
;
2185 if (!p
->pack_local
|| p
->pack_keep
)
2187 if (open_pack_index(p
))
2188 die("cannot open pack index");
2190 ALLOC_GROW(in_pack
.array
,
2191 in_pack
.nr
+ p
->num_objects
,
2194 for (i
= 0; i
< p
->num_objects
; i
++) {
2195 sha1
= nth_packed_object_sha1(p
, i
);
2196 o
= lookup_unknown_object(sha1
);
2197 if (!(o
->flags
& OBJECT_ADDED
))
2198 mark_in_pack_object(o
, p
, &in_pack
);
2199 o
->flags
|= OBJECT_ADDED
;
2204 qsort(in_pack
.array
, in_pack
.nr
, sizeof(in_pack
.array
[0]),
2206 for (i
= 0; i
< in_pack
.nr
; i
++) {
2207 struct object
*o
= in_pack
.array
[i
].object
;
2208 add_object_entry(o
->sha1
, o
->type
, "", 0);
2211 free(in_pack
.array
);
2214 static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1
)
2216 static struct packed_git
*last_found
= (void *)1;
2217 struct packed_git
*p
;
2219 p
= (last_found
!= (void *)1) ? last_found
: packed_git
;
2222 if ((!p
->pack_local
|| p
->pack_keep
) &&
2223 find_pack_entry_one(sha1
, p
)) {
2227 if (p
== last_found
)
2231 if (p
== last_found
)
2237 static void loosen_unused_packed_objects(struct rev_info
*revs
)
2239 struct packed_git
*p
;
2241 const unsigned char *sha1
;
2243 for (p
= packed_git
; p
; p
= p
->next
) {
2244 if (!p
->pack_local
|| p
->pack_keep
)
2247 if (open_pack_index(p
))
2248 die("cannot open pack index");
2250 for (i
= 0; i
< p
->num_objects
; i
++) {
2251 sha1
= nth_packed_object_sha1(p
, i
);
2252 if (!locate_object_entry(sha1
) &&
2253 !has_sha1_pack_kept_or_nonlocal(sha1
))
2254 if (force_object_loose(sha1
, p
->mtime
))
2255 die("unable to force loose object");
2260 static void get_object_list(int ac
, const char **av
)
2262 struct rev_info revs
;
2266 init_revisions(&revs
, NULL
);
2267 save_commit_buffer
= 0;
2268 setup_revisions(ac
, av
, &revs
, NULL
);
2270 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
2271 int len
= strlen(line
);
2272 if (len
&& line
[len
- 1] == '\n')
2277 if (!strcmp(line
, "--not")) {
2278 flags
^= UNINTERESTING
;
2281 die("not a rev '%s'", line
);
2283 if (handle_revision_arg(line
, &revs
, flags
, 1))
2284 die("bad revision '%s'", line
);
2287 if (prepare_revision_walk(&revs
))
2288 die("revision walk setup failed");
2289 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
2290 traverse_commit_list(&revs
, show_commit
, show_object
, NULL
);
2292 if (keep_unreachable
)
2293 add_objects_in_unpacked_packs(&revs
);
2294 if (unpack_unreachable
)
2295 loosen_unused_packed_objects(&revs
);
2298 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
2300 int use_internal_rev_list
= 0;
2302 int all_progress_implied
= 0;
2305 int rp_ac_alloc
= 64;
2308 read_replace_refs
= 0;
2310 rp_av
= xcalloc(rp_ac_alloc
, sizeof(*rp_av
));
2312 rp_av
[0] = "pack-objects";
2313 rp_av
[1] = "--objects"; /* --thin will make it --objects-edge */
2316 reset_pack_idx_option(&pack_idx_opts
);
2317 git_config(git_pack_config
, NULL
);
2318 if (!pack_compression_seen
&& core_compression_seen
)
2319 pack_compression_level
= core_compression_level
;
2321 progress
= isatty(2);
2322 for (i
= 1; i
< argc
; i
++) {
2323 const char *arg
= argv
[i
];
2328 if (!strcmp("--non-empty", arg
)) {
2332 if (!strcmp("--local", arg
)) {
2336 if (!strcmp("--incremental", arg
)) {
2340 if (!strcmp("--honor-pack-keep", arg
)) {
2341 ignore_packed_keep
= 1;
2344 if (!prefixcmp(arg
, "--compression=")) {
2346 int level
= strtoul(arg
+14, &end
, 0);
2347 if (!arg
[14] || *end
)
2350 level
= Z_DEFAULT_COMPRESSION
;
2351 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
2352 die("bad pack compression level %d", level
);
2353 pack_compression_level
= level
;
2356 if (!prefixcmp(arg
, "--max-pack-size=")) {
2357 pack_size_limit_cfg
= 0;
2358 if (!git_parse_ulong(arg
+16, &pack_size_limit
))
2362 if (!prefixcmp(arg
, "--window=")) {
2364 window
= strtoul(arg
+9, &end
, 0);
2365 if (!arg
[9] || *end
)
2369 if (!prefixcmp(arg
, "--window-memory=")) {
2370 if (!git_parse_ulong(arg
+16, &window_memory_limit
))
2374 if (!prefixcmp(arg
, "--threads=")) {
2376 delta_search_threads
= strtoul(arg
+10, &end
, 0);
2377 if (!arg
[10] || *end
|| delta_search_threads
< 0)
2380 if (delta_search_threads
!= 1)
2381 warning("no threads support, "
2382 "ignoring %s", arg
);
2386 if (!prefixcmp(arg
, "--depth=")) {
2388 depth
= strtoul(arg
+8, &end
, 0);
2389 if (!arg
[8] || *end
)
2393 if (!strcmp("--progress", arg
)) {
2397 if (!strcmp("--all-progress", arg
)) {
2401 if (!strcmp("--all-progress-implied", arg
)) {
2402 all_progress_implied
= 1;
2405 if (!strcmp("-q", arg
)) {
2409 if (!strcmp("--no-reuse-delta", arg
)) {
2413 if (!strcmp("--no-reuse-object", arg
)) {
2414 reuse_object
= reuse_delta
= 0;
2417 if (!strcmp("--delta-base-offset", arg
)) {
2418 allow_ofs_delta
= 1;
2421 if (!strcmp("--stdout", arg
)) {
2425 if (!strcmp("--revs", arg
)) {
2426 use_internal_rev_list
= 1;
2429 if (!strcmp("--keep-unreachable", arg
)) {
2430 keep_unreachable
= 1;
2433 if (!strcmp("--unpack-unreachable", arg
)) {
2434 unpack_unreachable
= 1;
2437 if (!strcmp("--include-tag", arg
)) {
2441 if (!strcmp("--unpacked", arg
) ||
2442 !strcmp("--reflog", arg
) ||
2443 !strcmp("--all", arg
)) {
2444 use_internal_rev_list
= 1;
2445 if (rp_ac
>= rp_ac_alloc
- 1) {
2446 rp_ac_alloc
= alloc_nr(rp_ac_alloc
);
2447 rp_av
= xrealloc(rp_av
,
2448 rp_ac_alloc
* sizeof(*rp_av
));
2450 rp_av
[rp_ac
++] = arg
;
2453 if (!strcmp("--thin", arg
)) {
2454 use_internal_rev_list
= 1;
2456 rp_av
[1] = "--objects-edge";
2459 if (!prefixcmp(arg
, "--index-version=")) {
2461 pack_idx_opts
.version
= strtoul(arg
+ 16, &c
, 10);
2462 if (pack_idx_opts
.version
> 2)
2465 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
2466 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
2470 if (!strcmp(arg
, "--keep-true-parents")) {
2471 grafts_replace_parents
= 0;
2477 /* Traditionally "pack-objects [options] base extra" failed;
2478 * we would however want to take refs parameter that would
2479 * have been given to upstream rev-list ourselves, which means
2480 * we somehow want to say what the base name is. So the
2483 * pack-objects [options] base <refs...>
2485 * in other words, we would treat the first non-option as the
2486 * base_name and send everything else to the internal revision
2490 if (!pack_to_stdout
)
2491 base_name
= argv
[i
++];
2493 if (pack_to_stdout
!= !base_name
)
2496 if (!pack_to_stdout
&& !pack_size_limit
)
2497 pack_size_limit
= pack_size_limit_cfg
;
2498 if (pack_to_stdout
&& pack_size_limit
)
2499 die("--max-pack-size cannot be used to build a pack for transfer.");
2500 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
2501 warning("minimum pack size limit is 1 MiB");
2502 pack_size_limit
= 1024*1024;
2505 if (!pack_to_stdout
&& thin
)
2506 die("--thin cannot be used to build an indexable pack.");
2508 if (keep_unreachable
&& unpack_unreachable
)
2509 die("--keep-unreachable and --unpack-unreachable are incompatible.");
2511 if (progress
&& all_progress_implied
)
2514 prepare_packed_git();
2517 progress_state
= start_progress("Counting objects", 0);
2518 if (!use_internal_rev_list
)
2519 read_object_list_from_stdin();
2521 rp_av
[rp_ac
] = NULL
;
2522 get_object_list(rp_ac
, rp_av
);
2524 cleanup_preferred_base();
2525 if (include_tag
&& nr_result
)
2526 for_each_ref(add_ref_tag
, NULL
);
2527 stop_progress(&progress_state
);
2529 if (non_empty
&& !nr_result
)
2532 prepare_pack(window
, depth
);
2535 fprintf(stderr
, "Total %"PRIu32
" (delta %"PRIu32
"),"
2536 " reused %"PRIu32
" (delta %"PRIu32
")\n",
2537 written
, written_delta
, reused
, reused_delta
);