11 #include "pack-revindex.h"
12 #include "csum-file.h"
13 #include "tree-walk.h"
16 #include "list-objects.h"
17 #include "pack-objects.h"
20 #include "streaming.h"
21 #include "thread-utils.h"
23 static const char *pack_usage
[] = {
24 N_("git pack-objects --stdout [options...] [< ref-list | < object-list]"),
25 N_("git pack-objects [options...] base-name [< ref-list | < object-list]"),
30 * Objects we are going to pack are collected in the `to_pack` structure.
31 * It contains an array (dynamically expanded) of the object data, and a map
32 * that can resolve SHA1s to their position in the array.
34 static struct packing_data to_pack
;
36 static struct pack_idx_entry
**written_list
;
37 static uint32_t nr_result
, nr_written
;
40 static int reuse_delta
= 1, reuse_object
= 1;
41 static int keep_unreachable
, unpack_unreachable
, include_tag
;
42 static unsigned long unpack_unreachable_expiration
;
44 static int incremental
;
45 static int ignore_packed_keep
;
46 static int allow_ofs_delta
;
47 static struct pack_idx_option pack_idx_opts
;
48 static const char *base_name
;
49 static int progress
= 1;
50 static int window
= 10;
51 static unsigned long pack_size_limit
;
52 static int depth
= 50;
53 static int delta_search_threads
;
54 static int pack_to_stdout
;
55 static int num_preferred_base
;
56 static struct progress
*progress_state
;
57 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
58 static int pack_compression_seen
;
60 static unsigned long delta_cache_size
= 0;
61 static unsigned long max_delta_cache_size
= 256 * 1024 * 1024;
62 static unsigned long cache_max_small_delta_size
= 1000;
64 static unsigned long window_memory_limit
= 0;
69 static uint32_t written
, written_delta
;
70 static uint32_t reused
, reused_delta
;
72 static void *get_delta(struct object_entry
*entry
)
74 unsigned long size
, base_size
, delta_size
;
75 void *buf
, *base_buf
, *delta_buf
;
76 enum object_type type
;
78 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
80 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
81 base_buf
= read_sha1_file(entry
->delta
->idx
.sha1
, &type
, &base_size
);
83 die("unable to read %s", sha1_to_hex(entry
->delta
->idx
.sha1
));
84 delta_buf
= diff_delta(base_buf
, base_size
,
85 buf
, size
, &delta_size
, 0);
86 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
87 die("delta size changed");
93 static unsigned long do_compress(void **pptr
, unsigned long size
)
97 unsigned long maxsize
;
99 memset(&stream
, 0, sizeof(stream
));
100 git_deflate_init(&stream
, pack_compression_level
);
101 maxsize
= git_deflate_bound(&stream
, size
);
104 out
= xmalloc(maxsize
);
108 stream
.avail_in
= size
;
109 stream
.next_out
= out
;
110 stream
.avail_out
= maxsize
;
111 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
113 git_deflate_end(&stream
);
116 return stream
.total_out
;
119 static unsigned long write_large_blob_data(struct git_istream
*st
, struct sha1file
*f
,
120 const unsigned char *sha1
)
123 unsigned char ibuf
[1024 * 16];
124 unsigned char obuf
[1024 * 16];
125 unsigned long olen
= 0;
127 memset(&stream
, 0, sizeof(stream
));
128 git_deflate_init(&stream
, pack_compression_level
);
133 readlen
= read_istream(st
, ibuf
, sizeof(ibuf
));
135 die(_("unable to read %s"), sha1_to_hex(sha1
));
137 stream
.next_in
= ibuf
;
138 stream
.avail_in
= readlen
;
139 while ((stream
.avail_in
|| readlen
== 0) &&
140 (zret
== Z_OK
|| zret
== Z_BUF_ERROR
)) {
141 stream
.next_out
= obuf
;
142 stream
.avail_out
= sizeof(obuf
);
143 zret
= git_deflate(&stream
, readlen
? 0 : Z_FINISH
);
144 sha1write(f
, obuf
, stream
.next_out
- obuf
);
145 olen
+= stream
.next_out
- obuf
;
148 die(_("deflate error (%d)"), zret
);
150 if (zret
!= Z_STREAM_END
)
151 die(_("deflate error (%d)"), zret
);
155 git_deflate_end(&stream
);
160 * we are going to reuse the existing object data as is. make
161 * sure it is not corrupt.
163 static int check_pack_inflate(struct packed_git
*p
,
164 struct pack_window
**w_curs
,
167 unsigned long expect
)
170 unsigned char fakebuf
[4096], *in
;
173 memset(&stream
, 0, sizeof(stream
));
174 git_inflate_init(&stream
);
176 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
178 stream
.next_out
= fakebuf
;
179 stream
.avail_out
= sizeof(fakebuf
);
180 st
= git_inflate(&stream
, Z_FINISH
);
181 offset
+= stream
.next_in
- in
;
182 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
183 git_inflate_end(&stream
);
184 return (st
== Z_STREAM_END
&&
185 stream
.total_out
== expect
&&
186 stream
.total_in
== len
) ? 0 : -1;
189 static void copy_pack_data(struct sha1file
*f
,
190 struct packed_git
*p
,
191 struct pack_window
**w_curs
,
199 in
= use_pack(p
, w_curs
, offset
, &avail
);
201 avail
= (unsigned long)len
;
202 sha1write(f
, in
, avail
);
208 /* Return 0 if we will bust the pack-size limit */
209 static unsigned long write_no_reuse_object(struct sha1file
*f
, struct object_entry
*entry
,
210 unsigned long limit
, int usable_delta
)
212 unsigned long size
, datalen
;
213 unsigned char header
[10], dheader
[10];
215 enum object_type type
;
217 struct git_istream
*st
= NULL
;
220 if (entry
->type
== OBJ_BLOB
&&
221 entry
->size
> big_file_threshold
&&
222 (st
= open_istream(entry
->idx
.sha1
, &type
, &size
, NULL
)) != NULL
)
225 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
227 die(_("unable to read %s"), sha1_to_hex(entry
->idx
.sha1
));
230 * make sure no cached delta data remains from a
231 * previous attempt before a pack split occurred.
233 free(entry
->delta_data
);
234 entry
->delta_data
= NULL
;
235 entry
->z_delta_size
= 0;
236 } else if (entry
->delta_data
) {
237 size
= entry
->delta_size
;
238 buf
= entry
->delta_data
;
239 entry
->delta_data
= NULL
;
240 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
241 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
243 buf
= get_delta(entry
);
244 size
= entry
->delta_size
;
245 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
246 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
249 if (st
) /* large blob case, just assume we don't compress well */
251 else if (entry
->z_delta_size
)
252 datalen
= entry
->z_delta_size
;
254 datalen
= do_compress(&buf
, size
);
257 * The object header is a byte of 'type' followed by zero or
258 * more bytes of length.
260 hdrlen
= encode_in_pack_object_header(type
, size
, header
);
262 if (type
== OBJ_OFS_DELTA
) {
264 * Deltas with relative base contain an additional
265 * encoding of the relative offset for the delta
266 * base from this object's position in the pack.
268 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
269 unsigned pos
= sizeof(dheader
) - 1;
270 dheader
[pos
] = ofs
& 127;
272 dheader
[--pos
] = 128 | (--ofs
& 127);
273 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
279 sha1write(f
, header
, hdrlen
);
280 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
281 hdrlen
+= sizeof(dheader
) - pos
;
282 } else if (type
== OBJ_REF_DELTA
) {
284 * Deltas with a base reference contain
285 * an additional 20 bytes for the base sha1.
287 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
293 sha1write(f
, header
, hdrlen
);
294 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
297 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
303 sha1write(f
, header
, hdrlen
);
306 datalen
= write_large_blob_data(st
, f
, entry
->idx
.sha1
);
309 sha1write(f
, buf
, datalen
);
313 return hdrlen
+ datalen
;
316 /* Return 0 if we will bust the pack-size limit */
317 static unsigned long write_reuse_object(struct sha1file
*f
, struct object_entry
*entry
,
318 unsigned long limit
, int usable_delta
)
320 struct packed_git
*p
= entry
->in_pack
;
321 struct pack_window
*w_curs
= NULL
;
322 struct revindex_entry
*revidx
;
324 enum object_type type
= entry
->type
;
325 unsigned long datalen
;
326 unsigned char header
[10], dheader
[10];
330 type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
331 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
332 hdrlen
= encode_in_pack_object_header(type
, entry
->size
, header
);
334 offset
= entry
->in_pack_offset
;
335 revidx
= find_pack_revindex(p
, offset
);
336 datalen
= revidx
[1].offset
- offset
;
337 if (!pack_to_stdout
&& p
->index_version
> 1 &&
338 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
)) {
339 error("bad packed object CRC for %s", sha1_to_hex(entry
->idx
.sha1
));
341 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
344 offset
+= entry
->in_pack_header_size
;
345 datalen
-= entry
->in_pack_header_size
;
347 if (!pack_to_stdout
&& p
->index_version
== 1 &&
348 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
)) {
349 error("corrupt packed object for %s", sha1_to_hex(entry
->idx
.sha1
));
351 return write_no_reuse_object(f
, entry
, limit
, usable_delta
);
354 if (type
== OBJ_OFS_DELTA
) {
355 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
356 unsigned pos
= sizeof(dheader
) - 1;
357 dheader
[pos
] = ofs
& 127;
359 dheader
[--pos
] = 128 | (--ofs
& 127);
360 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
364 sha1write(f
, header
, hdrlen
);
365 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
366 hdrlen
+= sizeof(dheader
) - pos
;
368 } else if (type
== OBJ_REF_DELTA
) {
369 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
373 sha1write(f
, header
, hdrlen
);
374 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
378 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
382 sha1write(f
, header
, hdrlen
);
384 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
387 return hdrlen
+ datalen
;
390 /* Return 0 if we will bust the pack-size limit */
391 static unsigned long write_object(struct sha1file
*f
,
392 struct object_entry
*entry
,
395 unsigned long limit
, len
;
396 int usable_delta
, to_reuse
;
401 /* apply size limit if limited packsize and not first object */
402 if (!pack_size_limit
|| !nr_written
)
404 else if (pack_size_limit
<= write_offset
)
406 * the earlier object did not fit the limit; avoid
407 * mistaking this with unlimited (i.e. limit = 0).
411 limit
= pack_size_limit
- write_offset
;
414 usable_delta
= 0; /* no delta */
415 else if (!pack_size_limit
)
416 usable_delta
= 1; /* unlimited packfile */
417 else if (entry
->delta
->idx
.offset
== (off_t
)-1)
418 usable_delta
= 0; /* base was written to another pack */
419 else if (entry
->delta
->idx
.offset
)
420 usable_delta
= 1; /* base already exists in this pack */
422 usable_delta
= 0; /* base could end up in another pack */
425 to_reuse
= 0; /* explicit */
426 else if (!entry
->in_pack
)
427 to_reuse
= 0; /* can't reuse what we don't have */
428 else if (entry
->type
== OBJ_REF_DELTA
|| entry
->type
== OBJ_OFS_DELTA
)
429 /* check_object() decided it for us ... */
430 to_reuse
= usable_delta
;
431 /* ... but pack split may override that */
432 else if (entry
->type
!= entry
->in_pack_type
)
433 to_reuse
= 0; /* pack has delta which is unusable */
434 else if (entry
->delta
)
435 to_reuse
= 0; /* we want to pack afresh */
437 to_reuse
= 1; /* we have it in-pack undeltified,
438 * and we do not need to deltify it.
442 len
= write_no_reuse_object(f
, entry
, limit
, usable_delta
);
444 len
= write_reuse_object(f
, entry
, limit
, usable_delta
);
452 entry
->idx
.crc32
= crc32_end(f
);
456 enum write_one_status
{
457 WRITE_ONE_SKIP
= -1, /* already written */
458 WRITE_ONE_BREAK
= 0, /* writing this will bust the limit; not written */
459 WRITE_ONE_WRITTEN
= 1, /* normal */
460 WRITE_ONE_RECURSIVE
= 2 /* already scheduled to be written */
463 static enum write_one_status
write_one(struct sha1file
*f
,
464 struct object_entry
*e
,
471 * we set offset to 1 (which is an impossible value) to mark
472 * the fact that this object is involved in "write its base
473 * first before writing a deltified object" recursion.
475 recursing
= (e
->idx
.offset
== 1);
477 warning("recursive delta detected for object %s",
478 sha1_to_hex(e
->idx
.sha1
));
479 return WRITE_ONE_RECURSIVE
;
480 } else if (e
->idx
.offset
|| e
->preferred_base
) {
481 /* offset is non zero if object is written already. */
482 return WRITE_ONE_SKIP
;
485 /* if we are deltified, write out base object first. */
487 e
->idx
.offset
= 1; /* now recurse */
488 switch (write_one(f
, e
->delta
, offset
)) {
489 case WRITE_ONE_RECURSIVE
:
490 /* we cannot depend on this one */
495 case WRITE_ONE_BREAK
:
496 e
->idx
.offset
= recursing
;
497 return WRITE_ONE_BREAK
;
501 e
->idx
.offset
= *offset
;
502 size
= write_object(f
, e
, *offset
);
504 e
->idx
.offset
= recursing
;
505 return WRITE_ONE_BREAK
;
507 written_list
[nr_written
++] = &e
->idx
;
509 /* make sure off_t is sufficiently large not to wrap */
510 if (signed_add_overflows(*offset
, size
))
511 die("pack too large for current definition of off_t");
513 return WRITE_ONE_WRITTEN
;
516 static int mark_tagged(const char *path
, const unsigned char *sha1
, int flag
,
519 unsigned char peeled
[20];
520 struct object_entry
*entry
= packlist_find(&to_pack
, sha1
, NULL
);
524 if (!peel_ref(path
, peeled
)) {
525 entry
= packlist_find(&to_pack
, peeled
, NULL
);
532 static inline void add_to_write_order(struct object_entry
**wo
,
534 struct object_entry
*e
)
542 static void add_descendants_to_write_order(struct object_entry
**wo
,
544 struct object_entry
*e
)
546 int add_to_order
= 1;
549 struct object_entry
*s
;
550 /* add this node... */
551 add_to_write_order(wo
, endp
, e
);
552 /* all its siblings... */
553 for (s
= e
->delta_sibling
; s
; s
= s
->delta_sibling
) {
554 add_to_write_order(wo
, endp
, s
);
557 /* drop down a level to add left subtree nodes if possible */
558 if (e
->delta_child
) {
563 /* our sibling might have some children, it is next */
564 if (e
->delta_sibling
) {
565 e
= e
->delta_sibling
;
568 /* go back to our parent node */
570 while (e
&& !e
->delta_sibling
) {
571 /* we're on the right side of a subtree, keep
572 * going up until we can go right again */
576 /* done- we hit our original root node */
579 /* pass it off to sibling at this level */
580 e
= e
->delta_sibling
;
585 static void add_family_to_write_order(struct object_entry
**wo
,
587 struct object_entry
*e
)
589 struct object_entry
*root
;
591 for (root
= e
; root
->delta
; root
= root
->delta
)
593 add_descendants_to_write_order(wo
, endp
, root
);
596 static struct object_entry
**compute_write_order(void)
598 unsigned int i
, wo_end
, last_untagged
;
600 struct object_entry
**wo
= xmalloc(to_pack
.nr_objects
* sizeof(*wo
));
601 struct object_entry
*objects
= to_pack
.objects
;
603 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
604 objects
[i
].tagged
= 0;
605 objects
[i
].filled
= 0;
606 objects
[i
].delta_child
= NULL
;
607 objects
[i
].delta_sibling
= NULL
;
611 * Fully connect delta_child/delta_sibling network.
612 * Make sure delta_sibling is sorted in the original
615 for (i
= to_pack
.nr_objects
; i
> 0;) {
616 struct object_entry
*e
= &objects
[--i
];
619 /* Mark me as the first child */
620 e
->delta_sibling
= e
->delta
->delta_child
;
621 e
->delta
->delta_child
= e
;
625 * Mark objects that are at the tip of tags.
627 for_each_tag_ref(mark_tagged
, NULL
);
630 * Give the objects in the original recency order until
631 * we see a tagged tip.
633 for (i
= wo_end
= 0; i
< to_pack
.nr_objects
; i
++) {
634 if (objects
[i
].tagged
)
636 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
641 * Then fill all the tagged tips.
643 for (; i
< to_pack
.nr_objects
; i
++) {
644 if (objects
[i
].tagged
)
645 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
649 * And then all remaining commits and tags.
651 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
652 if (objects
[i
].type
!= OBJ_COMMIT
&&
653 objects
[i
].type
!= OBJ_TAG
)
655 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
659 * And then all the trees.
661 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
662 if (objects
[i
].type
!= OBJ_TREE
)
664 add_to_write_order(wo
, &wo_end
, &objects
[i
]);
668 * Finally all the rest in really tight order
670 for (i
= last_untagged
; i
< to_pack
.nr_objects
; i
++) {
671 if (!objects
[i
].filled
)
672 add_family_to_write_order(wo
, &wo_end
, &objects
[i
]);
675 if (wo_end
!= to_pack
.nr_objects
)
676 die("ordered %u objects, expected %"PRIu32
, wo_end
, to_pack
.nr_objects
);
681 static void write_pack_file(void)
686 uint32_t nr_remaining
= nr_result
;
687 time_t last_mtime
= 0;
688 struct object_entry
**write_order
;
690 if (progress
> pack_to_stdout
)
691 progress_state
= start_progress("Writing objects", nr_result
);
692 written_list
= xmalloc(to_pack
.nr_objects
* sizeof(*written_list
));
693 write_order
= compute_write_order();
696 unsigned char sha1
[20];
697 char *pack_tmp_name
= NULL
;
700 f
= sha1fd_throughput(1, "<stdout>", progress_state
);
702 f
= create_tmp_packfile(&pack_tmp_name
);
704 offset
= write_pack_header(f
, nr_remaining
);
706 die_errno("unable to write pack header");
708 for (; i
< to_pack
.nr_objects
; i
++) {
709 struct object_entry
*e
= write_order
[i
];
710 if (write_one(f
, e
, &offset
) == WRITE_ONE_BREAK
)
712 display_progress(progress_state
, written
);
716 * Did we write the wrong # entries in the header?
717 * If so, rewrite it like in fast-import
719 if (pack_to_stdout
) {
720 sha1close(f
, sha1
, CSUM_CLOSE
);
721 } else if (nr_written
== nr_remaining
) {
722 sha1close(f
, sha1
, CSUM_FSYNC
);
724 int fd
= sha1close(f
, sha1
, 0);
725 fixup_pack_header_footer(fd
, sha1
, pack_tmp_name
,
726 nr_written
, sha1
, offset
);
730 if (!pack_to_stdout
) {
732 char tmpname
[PATH_MAX
];
735 * Packs are runtime accessed in their mtime
736 * order since newer packs are more likely to contain
737 * younger objects. So if we are creating multiple
738 * packs then we should modify the mtime of later ones
739 * to preserve this property.
741 if (stat(pack_tmp_name
, &st
) < 0) {
742 warning("failed to stat %s: %s",
743 pack_tmp_name
, strerror(errno
));
744 } else if (!last_mtime
) {
745 last_mtime
= st
.st_mtime
;
748 utb
.actime
= st
.st_atime
;
749 utb
.modtime
= --last_mtime
;
750 if (utime(pack_tmp_name
, &utb
) < 0)
751 warning("failed utime() on %s: %s",
752 tmpname
, strerror(errno
));
755 /* Enough space for "-<sha-1>.pack"? */
756 if (sizeof(tmpname
) <= strlen(base_name
) + 50)
757 die("pack base name '%s' too long", base_name
);
758 snprintf(tmpname
, sizeof(tmpname
), "%s-", base_name
);
759 finish_tmp_packfile(tmpname
, pack_tmp_name
,
760 written_list
, nr_written
,
761 &pack_idx_opts
, sha1
);
763 puts(sha1_to_hex(sha1
));
766 /* mark written objects as written to previous pack */
767 for (j
= 0; j
< nr_written
; j
++) {
768 written_list
[j
]->offset
= (off_t
)-1;
770 nr_remaining
-= nr_written
;
771 } while (nr_remaining
&& i
< to_pack
.nr_objects
);
775 stop_progress(&progress_state
);
776 if (written
!= nr_result
)
777 die("wrote %"PRIu32
" objects while expecting %"PRIu32
,
781 static void setup_delta_attr_check(struct git_attr_check
*check
)
783 static struct git_attr
*attr_delta
;
786 attr_delta
= git_attr("delta");
788 check
[0].attr
= attr_delta
;
791 static int no_try_delta(const char *path
)
793 struct git_attr_check check
[1];
795 setup_delta_attr_check(check
);
796 if (git_check_attr(path
, ARRAY_SIZE(check
), check
))
798 if (ATTR_FALSE(check
->value
))
803 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
804 const char *name
, int exclude
)
806 struct object_entry
*entry
;
807 struct packed_git
*p
, *found_pack
= NULL
;
808 off_t found_offset
= 0;
809 uint32_t hash
= pack_name_hash(name
);
812 entry
= packlist_find(&to_pack
, sha1
, &index_pos
);
815 if (!entry
->preferred_base
)
817 entry
->preferred_base
= 1;
822 if (!exclude
&& local
&& has_loose_object_nonlocal(sha1
))
825 for (p
= packed_git
; p
; p
= p
->next
) {
826 off_t offset
= find_pack_entry_one(sha1
, p
);
829 if (!is_pack_valid(p
)) {
830 warning("packfile %s cannot be accessed", p
->pack_name
);
833 found_offset
= offset
;
840 if (local
&& !p
->pack_local
)
842 if (ignore_packed_keep
&& p
->pack_local
&& p
->pack_keep
)
847 entry
= packlist_alloc(&to_pack
, sha1
, index_pos
);
852 entry
->preferred_base
= 1;
856 entry
->in_pack
= found_pack
;
857 entry
->in_pack_offset
= found_offset
;
860 display_progress(progress_state
, to_pack
.nr_objects
);
862 if (name
&& no_try_delta(name
))
863 entry
->no_try_delta
= 1;
868 struct pbase_tree_cache
{
869 unsigned char sha1
[20];
873 unsigned long tree_size
;
876 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
877 static int pbase_tree_cache_ix(const unsigned char *sha1
)
879 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
881 static int pbase_tree_cache_ix_incr(int ix
)
883 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
886 static struct pbase_tree
{
887 struct pbase_tree
*next
;
888 /* This is a phony "cache" entry; we are not
889 * going to evict it nor find it through _get()
890 * mechanism -- this is for the toplevel node that
891 * would almost always change with any commit.
893 struct pbase_tree_cache pcache
;
896 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
898 struct pbase_tree_cache
*ent
, *nent
;
901 enum object_type type
;
903 int my_ix
= pbase_tree_cache_ix(sha1
);
904 int available_ix
= -1;
906 /* pbase-tree-cache acts as a limited hashtable.
907 * your object will be found at your index or within a few
908 * slots after that slot if it is cached.
910 for (neigh
= 0; neigh
< 8; neigh
++) {
911 ent
= pbase_tree_cache
[my_ix
];
912 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
916 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
917 ((0 <= available_ix
) &&
918 (!ent
&& pbase_tree_cache
[available_ix
])))
919 available_ix
= my_ix
;
922 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
925 /* Did not find one. Either we got a bogus request or
926 * we need to read and perhaps cache.
928 data
= read_sha1_file(sha1
, &type
, &size
);
931 if (type
!= OBJ_TREE
) {
936 /* We need to either cache or return a throwaway copy */
938 if (available_ix
< 0)
941 ent
= pbase_tree_cache
[available_ix
];
942 my_ix
= available_ix
;
946 nent
= xmalloc(sizeof(*nent
));
947 nent
->temporary
= (available_ix
< 0);
950 /* evict and reuse */
951 free(ent
->tree_data
);
954 hashcpy(nent
->sha1
, sha1
);
955 nent
->tree_data
= data
;
956 nent
->tree_size
= size
;
958 if (!nent
->temporary
)
959 pbase_tree_cache
[my_ix
] = nent
;
963 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
965 if (!cache
->temporary
) {
969 free(cache
->tree_data
);
973 static int name_cmp_len(const char *name
)
976 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
981 static void add_pbase_object(struct tree_desc
*tree
,
984 const char *fullname
)
986 struct name_entry entry
;
989 while (tree_entry(tree
,&entry
)) {
990 if (S_ISGITLINK(entry
.mode
))
992 cmp
= tree_entry_len(&entry
) != cmplen
? 1 :
993 memcmp(name
, entry
.path
, cmplen
);
998 if (name
[cmplen
] != '/') {
999 add_object_entry(entry
.sha1
,
1000 object_type(entry
.mode
),
1004 if (S_ISDIR(entry
.mode
)) {
1005 struct tree_desc sub
;
1006 struct pbase_tree_cache
*tree
;
1007 const char *down
= name
+cmplen
+1;
1008 int downlen
= name_cmp_len(down
);
1010 tree
= pbase_tree_get(entry
.sha1
);
1013 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1015 add_pbase_object(&sub
, down
, downlen
, fullname
);
1016 pbase_tree_put(tree
);
1021 static unsigned *done_pbase_paths
;
1022 static int done_pbase_paths_num
;
1023 static int done_pbase_paths_alloc
;
1024 static int done_pbase_path_pos(unsigned hash
)
1027 int hi
= done_pbase_paths_num
;
1029 int mi
= (hi
+ lo
) / 2;
1030 if (done_pbase_paths
[mi
] == hash
)
1032 if (done_pbase_paths
[mi
] < hash
)
1040 static int check_pbase_path(unsigned hash
)
1042 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1046 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1047 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1048 done_pbase_paths
= xrealloc(done_pbase_paths
,
1049 done_pbase_paths_alloc
*
1052 done_pbase_paths_num
++;
1053 if (pos
< done_pbase_paths_num
)
1054 memmove(done_pbase_paths
+ pos
+ 1,
1055 done_pbase_paths
+ pos
,
1056 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1057 done_pbase_paths
[pos
] = hash
;
1061 static void add_preferred_base_object(const char *name
)
1063 struct pbase_tree
*it
;
1065 unsigned hash
= pack_name_hash(name
);
1067 if (!num_preferred_base
|| check_pbase_path(hash
))
1070 cmplen
= name_cmp_len(name
);
1071 for (it
= pbase_tree
; it
; it
= it
->next
) {
1073 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, NULL
, 1);
1076 struct tree_desc tree
;
1077 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1078 add_pbase_object(&tree
, name
, cmplen
, name
);
1083 static void add_preferred_base(unsigned char *sha1
)
1085 struct pbase_tree
*it
;
1088 unsigned char tree_sha1
[20];
1090 if (window
<= num_preferred_base
++)
1093 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1097 for (it
= pbase_tree
; it
; it
= it
->next
) {
1098 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1104 it
= xcalloc(1, sizeof(*it
));
1105 it
->next
= pbase_tree
;
1108 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1109 it
->pcache
.tree_data
= data
;
1110 it
->pcache
.tree_size
= size
;
1113 static void cleanup_preferred_base(void)
1115 struct pbase_tree
*it
;
1121 struct pbase_tree
*this = it
;
1123 free(this->pcache
.tree_data
);
1127 for (i
= 0; i
< ARRAY_SIZE(pbase_tree_cache
); i
++) {
1128 if (!pbase_tree_cache
[i
])
1130 free(pbase_tree_cache
[i
]->tree_data
);
1131 free(pbase_tree_cache
[i
]);
1132 pbase_tree_cache
[i
] = NULL
;
1135 free(done_pbase_paths
);
1136 done_pbase_paths
= NULL
;
1137 done_pbase_paths_num
= done_pbase_paths_alloc
= 0;
1140 static void check_object(struct object_entry
*entry
)
1142 if (entry
->in_pack
) {
1143 struct packed_git
*p
= entry
->in_pack
;
1144 struct pack_window
*w_curs
= NULL
;
1145 const unsigned char *base_ref
= NULL
;
1146 struct object_entry
*base_entry
;
1147 unsigned long used
, used_0
;
1148 unsigned long avail
;
1150 unsigned char *buf
, c
;
1152 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1155 * We want in_pack_type even if we do not reuse delta
1156 * since non-delta representations could still be reused.
1158 used
= unpack_object_header_buffer(buf
, avail
,
1159 &entry
->in_pack_type
,
1165 * Determine if this is a delta and if so whether we can
1166 * reuse it or not. Otherwise let's find out as cheaply as
1167 * possible what the actual type and size for this object is.
1169 switch (entry
->in_pack_type
) {
1171 /* Not a delta hence we've already got all we need. */
1172 entry
->type
= entry
->in_pack_type
;
1173 entry
->in_pack_header_size
= used
;
1174 if (entry
->type
< OBJ_COMMIT
|| entry
->type
> OBJ_BLOB
)
1176 unuse_pack(&w_curs
);
1179 if (reuse_delta
&& !entry
->preferred_base
)
1180 base_ref
= use_pack(p
, &w_curs
,
1181 entry
->in_pack_offset
+ used
, NULL
);
1182 entry
->in_pack_header_size
= used
+ 20;
1185 buf
= use_pack(p
, &w_curs
,
1186 entry
->in_pack_offset
+ used
, NULL
);
1192 if (!ofs
|| MSB(ofs
, 7)) {
1193 error("delta base offset overflow in pack for %s",
1194 sha1_to_hex(entry
->idx
.sha1
));
1198 ofs
= (ofs
<< 7) + (c
& 127);
1200 ofs
= entry
->in_pack_offset
- ofs
;
1201 if (ofs
<= 0 || ofs
>= entry
->in_pack_offset
) {
1202 error("delta base offset out of bound for %s",
1203 sha1_to_hex(entry
->idx
.sha1
));
1206 if (reuse_delta
&& !entry
->preferred_base
) {
1207 struct revindex_entry
*revidx
;
1208 revidx
= find_pack_revindex(p
, ofs
);
1211 base_ref
= nth_packed_object_sha1(p
, revidx
->nr
);
1213 entry
->in_pack_header_size
= used
+ used_0
;
1217 if (base_ref
&& (base_entry
= packlist_find(&to_pack
, base_ref
, NULL
))) {
1219 * If base_ref was set above that means we wish to
1220 * reuse delta data, and we even found that base
1221 * in the list of objects we want to pack. Goodie!
1223 * Depth value does not matter - find_deltas() will
1224 * never consider reused delta as the base object to
1225 * deltify other objects against, in order to avoid
1228 entry
->type
= entry
->in_pack_type
;
1229 entry
->delta
= base_entry
;
1230 entry
->delta_size
= entry
->size
;
1231 entry
->delta_sibling
= base_entry
->delta_child
;
1232 base_entry
->delta_child
= entry
;
1233 unuse_pack(&w_curs
);
1239 * This must be a delta and we already know what the
1240 * final object type is. Let's extract the actual
1241 * object size from the delta header.
1243 entry
->size
= get_size_from_delta(p
, &w_curs
,
1244 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1245 if (entry
->size
== 0)
1247 unuse_pack(&w_curs
);
1252 * No choice but to fall back to the recursive delta walk
1253 * with sha1_object_info() to find about the object type
1257 unuse_pack(&w_curs
);
1260 entry
->type
= sha1_object_info(entry
->idx
.sha1
, &entry
->size
);
1262 * The error condition is checked in prepare_pack(). This is
1263 * to permit a missing preferred base object to be ignored
1264 * as a preferred base. Doing so can result in a larger
1265 * pack file, but the transfer will still take place.
1269 static int pack_offset_sort(const void *_a
, const void *_b
)
1271 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1272 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1274 /* avoid filesystem trashing with loose objects */
1275 if (!a
->in_pack
&& !b
->in_pack
)
1276 return hashcmp(a
->idx
.sha1
, b
->idx
.sha1
);
1278 if (a
->in_pack
< b
->in_pack
)
1280 if (a
->in_pack
> b
->in_pack
)
1282 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1283 (a
->in_pack_offset
> b
->in_pack_offset
);
1286 static void get_object_details(void)
1289 struct object_entry
**sorted_by_offset
;
1291 sorted_by_offset
= xcalloc(to_pack
.nr_objects
, sizeof(struct object_entry
*));
1292 for (i
= 0; i
< to_pack
.nr_objects
; i
++)
1293 sorted_by_offset
[i
] = to_pack
.objects
+ i
;
1294 qsort(sorted_by_offset
, to_pack
.nr_objects
, sizeof(*sorted_by_offset
), pack_offset_sort
);
1296 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
1297 struct object_entry
*entry
= sorted_by_offset
[i
];
1298 check_object(entry
);
1299 if (big_file_threshold
< entry
->size
)
1300 entry
->no_try_delta
= 1;
1303 free(sorted_by_offset
);
1307 * We search for deltas in a list sorted by type, by filename hash, and then
1308 * by size, so that we see progressively smaller and smaller files.
1309 * That's because we prefer deltas to be from the bigger file
1310 * to the smaller -- deletes are potentially cheaper, but perhaps
1311 * more importantly, the bigger file is likely the more recent
1312 * one. The deepest deltas are therefore the oldest objects which are
1313 * less susceptible to be accessed often.
1315 static int type_size_sort(const void *_a
, const void *_b
)
1317 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1318 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1320 if (a
->type
> b
->type
)
1322 if (a
->type
< b
->type
)
1324 if (a
->hash
> b
->hash
)
1326 if (a
->hash
< b
->hash
)
1328 if (a
->preferred_base
> b
->preferred_base
)
1330 if (a
->preferred_base
< b
->preferred_base
)
1332 if (a
->size
> b
->size
)
1334 if (a
->size
< b
->size
)
1336 return a
< b
? -1 : (a
> b
); /* newest first */
1340 struct object_entry
*entry
;
1342 struct delta_index
*index
;
1346 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
1347 unsigned long delta_size
)
1349 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
1352 if (delta_size
< cache_max_small_delta_size
)
1355 /* cache delta, if objects are large enough compared to delta size */
1356 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
1364 static pthread_mutex_t read_mutex
;
1365 #define read_lock() pthread_mutex_lock(&read_mutex)
1366 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1368 static pthread_mutex_t cache_mutex
;
1369 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1370 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1372 static pthread_mutex_t progress_mutex
;
1373 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1374 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1378 #define read_lock() (void)0
1379 #define read_unlock() (void)0
1380 #define cache_lock() (void)0
1381 #define cache_unlock() (void)0
1382 #define progress_lock() (void)0
1383 #define progress_unlock() (void)0
1387 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1388 unsigned max_depth
, unsigned long *mem_usage
)
1390 struct object_entry
*trg_entry
= trg
->entry
;
1391 struct object_entry
*src_entry
= src
->entry
;
1392 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1394 enum object_type type
;
1397 /* Don't bother doing diffs between different types */
1398 if (trg_entry
->type
!= src_entry
->type
)
1402 * We do not bother to try a delta that we discarded on an
1403 * earlier try, but only when reusing delta data. Note that
1404 * src_entry that is marked as the preferred_base should always
1405 * be considered, as even if we produce a suboptimal delta against
1406 * it, we will still save the transfer cost, as we already know
1407 * the other side has it and we won't send src_entry at all.
1409 if (reuse_delta
&& trg_entry
->in_pack
&&
1410 trg_entry
->in_pack
== src_entry
->in_pack
&&
1411 !src_entry
->preferred_base
&&
1412 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1413 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1416 /* Let's not bust the allowed depth. */
1417 if (src
->depth
>= max_depth
)
1420 /* Now some size filtering heuristics. */
1421 trg_size
= trg_entry
->size
;
1422 if (!trg_entry
->delta
) {
1423 max_size
= trg_size
/2 - 20;
1426 max_size
= trg_entry
->delta_size
;
1427 ref_depth
= trg
->depth
;
1429 max_size
= (uint64_t)max_size
* (max_depth
- src
->depth
) /
1430 (max_depth
- ref_depth
+ 1);
1433 src_size
= src_entry
->size
;
1434 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1435 if (sizediff
>= max_size
)
1437 if (trg_size
< src_size
/ 32)
1440 /* Load data if not already done */
1443 trg
->data
= read_sha1_file(trg_entry
->idx
.sha1
, &type
, &sz
);
1446 die("object %s cannot be read",
1447 sha1_to_hex(trg_entry
->idx
.sha1
));
1449 die("object %s inconsistent object length (%lu vs %lu)",
1450 sha1_to_hex(trg_entry
->idx
.sha1
), sz
, trg_size
);
1455 src
->data
= read_sha1_file(src_entry
->idx
.sha1
, &type
, &sz
);
1458 if (src_entry
->preferred_base
) {
1459 static int warned
= 0;
1461 warning("object %s cannot be read",
1462 sha1_to_hex(src_entry
->idx
.sha1
));
1464 * Those objects are not included in the
1465 * resulting pack. Be resilient and ignore
1466 * them if they can't be read, in case the
1467 * pack could be created nevertheless.
1471 die("object %s cannot be read",
1472 sha1_to_hex(src_entry
->idx
.sha1
));
1475 die("object %s inconsistent object length (%lu vs %lu)",
1476 sha1_to_hex(src_entry
->idx
.sha1
), sz
, src_size
);
1480 src
->index
= create_delta_index(src
->data
, src_size
);
1482 static int warned
= 0;
1484 warning("suboptimal pack - out of memory");
1487 *mem_usage
+= sizeof_delta_index(src
->index
);
1490 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1494 if (trg_entry
->delta
) {
1495 /* Prefer only shallower same-sized deltas. */
1496 if (delta_size
== trg_entry
->delta_size
&&
1497 src
->depth
+ 1 >= trg
->depth
) {
1504 * Handle memory allocation outside of the cache
1505 * accounting lock. Compiler will optimize the strangeness
1506 * away when NO_PTHREADS is defined.
1508 free(trg_entry
->delta_data
);
1510 if (trg_entry
->delta_data
) {
1511 delta_cache_size
-= trg_entry
->delta_size
;
1512 trg_entry
->delta_data
= NULL
;
1514 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
1515 delta_cache_size
+= delta_size
;
1517 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
1523 trg_entry
->delta
= src_entry
;
1524 trg_entry
->delta_size
= delta_size
;
1525 trg
->depth
= src
->depth
+ 1;
1530 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1532 struct object_entry
*child
= me
->delta_child
;
1535 unsigned int c
= check_delta_limit(child
, n
+ 1);
1538 child
= child
->delta_sibling
;
1543 static unsigned long free_unpacked(struct unpacked
*n
)
1545 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
1546 free_delta_index(n
->index
);
1549 freed_mem
+= n
->entry
->size
;
1558 static void find_deltas(struct object_entry
**list
, unsigned *list_size
,
1559 int window
, int depth
, unsigned *processed
)
1561 uint32_t i
, idx
= 0, count
= 0;
1562 struct unpacked
*array
;
1563 unsigned long mem_usage
= 0;
1565 array
= xcalloc(window
, sizeof(struct unpacked
));
1568 struct object_entry
*entry
;
1569 struct unpacked
*n
= array
+ idx
;
1570 int j
, max_depth
, best_base
= -1;
1579 if (!entry
->preferred_base
) {
1581 display_progress(progress_state
, *processed
);
1585 mem_usage
-= free_unpacked(n
);
1588 while (window_memory_limit
&&
1589 mem_usage
> window_memory_limit
&&
1591 uint32_t tail
= (idx
+ window
- count
) % window
;
1592 mem_usage
-= free_unpacked(array
+ tail
);
1596 /* We do not compute delta to *create* objects we are not
1599 if (entry
->preferred_base
)
1603 * If the current object is at pack edge, take the depth the
1604 * objects that depend on the current object into account
1605 * otherwise they would become too deep.
1608 if (entry
->delta_child
) {
1609 max_depth
-= check_delta_limit(entry
, 0);
1617 uint32_t other_idx
= idx
+ j
;
1619 if (other_idx
>= window
)
1620 other_idx
-= window
;
1621 m
= array
+ other_idx
;
1624 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
1628 best_base
= other_idx
;
1632 * If we decided to cache the delta data, then it is best
1633 * to compress it right away. First because we have to do
1634 * it anyway, and doing it here while we're threaded will
1635 * save a lot of time in the non threaded write phase,
1636 * as well as allow for caching more deltas within
1637 * the same cache size limit.
1639 * But only if not writing to stdout, since in that case
1640 * the network is most likely throttling writes anyway,
1641 * and therefore it is best to go to the write phase ASAP
1642 * instead, as we can afford spending more time compressing
1643 * between writes at that moment.
1645 if (entry
->delta_data
&& !pack_to_stdout
) {
1646 entry
->z_delta_size
= do_compress(&entry
->delta_data
,
1649 delta_cache_size
-= entry
->delta_size
;
1650 delta_cache_size
+= entry
->z_delta_size
;
1654 /* if we made n a delta, and if n is already at max
1655 * depth, leaving it in the window is pointless. we
1656 * should evict it first.
1658 if (entry
->delta
&& max_depth
<= n
->depth
)
1662 * Move the best delta base up in the window, after the
1663 * currently deltified object, to keep it longer. It will
1664 * be the first base object to be attempted next.
1667 struct unpacked swap
= array
[best_base
];
1668 int dist
= (window
+ idx
- best_base
) % window
;
1669 int dst
= best_base
;
1671 int src
= (dst
+ 1) % window
;
1672 array
[dst
] = array
[src
];
1680 if (count
+ 1 < window
)
1686 for (i
= 0; i
< window
; ++i
) {
1687 free_delta_index(array
[i
].index
);
1688 free(array
[i
].data
);
1695 static void try_to_free_from_threads(size_t size
)
1698 release_pack_memory(size
);
1702 static try_to_free_t old_try_to_free_routine
;
1705 * The main thread waits on the condition that (at least) one of the workers
1706 * has stopped working (which is indicated in the .working member of
1707 * struct thread_params).
1708 * When a work thread has completed its work, it sets .working to 0 and
1709 * signals the main thread and waits on the condition that .data_ready
1713 struct thread_params
{
1715 struct object_entry
**list
;
1722 pthread_mutex_t mutex
;
1723 pthread_cond_t cond
;
1724 unsigned *processed
;
1727 static pthread_cond_t progress_cond
;
1730 * Mutex and conditional variable can't be statically-initialized on Windows.
1732 static void init_threaded_search(void)
1734 init_recursive_mutex(&read_mutex
);
1735 pthread_mutex_init(&cache_mutex
, NULL
);
1736 pthread_mutex_init(&progress_mutex
, NULL
);
1737 pthread_cond_init(&progress_cond
, NULL
);
1738 old_try_to_free_routine
= set_try_to_free_routine(try_to_free_from_threads
);
1741 static void cleanup_threaded_search(void)
1743 set_try_to_free_routine(old_try_to_free_routine
);
1744 pthread_cond_destroy(&progress_cond
);
1745 pthread_mutex_destroy(&read_mutex
);
1746 pthread_mutex_destroy(&cache_mutex
);
1747 pthread_mutex_destroy(&progress_mutex
);
1750 static void *threaded_find_deltas(void *arg
)
1752 struct thread_params
*me
= arg
;
1754 while (me
->remaining
) {
1755 find_deltas(me
->list
, &me
->remaining
,
1756 me
->window
, me
->depth
, me
->processed
);
1760 pthread_cond_signal(&progress_cond
);
1764 * We must not set ->data_ready before we wait on the
1765 * condition because the main thread may have set it to 1
1766 * before we get here. In order to be sure that new
1767 * work is available if we see 1 in ->data_ready, it
1768 * was initialized to 0 before this thread was spawned
1769 * and we reset it to 0 right away.
1771 pthread_mutex_lock(&me
->mutex
);
1772 while (!me
->data_ready
)
1773 pthread_cond_wait(&me
->cond
, &me
->mutex
);
1775 pthread_mutex_unlock(&me
->mutex
);
1777 /* leave ->working 1 so that this doesn't get more work assigned */
1781 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
1782 int window
, int depth
, unsigned *processed
)
1784 struct thread_params
*p
;
1785 int i
, ret
, active_threads
= 0;
1787 init_threaded_search();
1789 if (!delta_search_threads
) /* --threads=0 means autodetect */
1790 delta_search_threads
= online_cpus();
1791 if (delta_search_threads
<= 1) {
1792 find_deltas(list
, &list_size
, window
, depth
, processed
);
1793 cleanup_threaded_search();
1796 if (progress
> pack_to_stdout
)
1797 fprintf(stderr
, "Delta compression using up to %d threads.\n",
1798 delta_search_threads
);
1799 p
= xcalloc(delta_search_threads
, sizeof(*p
));
1801 /* Partition the work amongst work threads. */
1802 for (i
= 0; i
< delta_search_threads
; i
++) {
1803 unsigned sub_size
= list_size
/ (delta_search_threads
- i
);
1805 /* don't use too small segments or no deltas will be found */
1806 if (sub_size
< 2*window
&& i
+1 < delta_search_threads
)
1809 p
[i
].window
= window
;
1811 p
[i
].processed
= processed
;
1813 p
[i
].data_ready
= 0;
1815 /* try to split chunks on "path" boundaries */
1816 while (sub_size
&& sub_size
< list_size
&&
1817 list
[sub_size
]->hash
&&
1818 list
[sub_size
]->hash
== list
[sub_size
-1]->hash
)
1822 p
[i
].list_size
= sub_size
;
1823 p
[i
].remaining
= sub_size
;
1826 list_size
-= sub_size
;
1829 /* Start work threads. */
1830 for (i
= 0; i
< delta_search_threads
; i
++) {
1831 if (!p
[i
].list_size
)
1833 pthread_mutex_init(&p
[i
].mutex
, NULL
);
1834 pthread_cond_init(&p
[i
].cond
, NULL
);
1835 ret
= pthread_create(&p
[i
].thread
, NULL
,
1836 threaded_find_deltas
, &p
[i
]);
1838 die("unable to create thread: %s", strerror(ret
));
1843 * Now let's wait for work completion. Each time a thread is done
1844 * with its work, we steal half of the remaining work from the
1845 * thread with the largest number of unprocessed objects and give
1846 * it to that newly idle thread. This ensure good load balancing
1847 * until the remaining object list segments are simply too short
1848 * to be worth splitting anymore.
1850 while (active_threads
) {
1851 struct thread_params
*target
= NULL
;
1852 struct thread_params
*victim
= NULL
;
1853 unsigned sub_size
= 0;
1857 for (i
= 0; !target
&& i
< delta_search_threads
; i
++)
1862 pthread_cond_wait(&progress_cond
, &progress_mutex
);
1865 for (i
= 0; i
< delta_search_threads
; i
++)
1866 if (p
[i
].remaining
> 2*window
&&
1867 (!victim
|| victim
->remaining
< p
[i
].remaining
))
1870 sub_size
= victim
->remaining
/ 2;
1871 list
= victim
->list
+ victim
->list_size
- sub_size
;
1872 while (sub_size
&& list
[0]->hash
&&
1873 list
[0]->hash
== list
[-1]->hash
) {
1879 * It is possible for some "paths" to have
1880 * so many objects that no hash boundary
1881 * might be found. Let's just steal the
1882 * exact half in that case.
1884 sub_size
= victim
->remaining
/ 2;
1887 target
->list
= list
;
1888 victim
->list_size
-= sub_size
;
1889 victim
->remaining
-= sub_size
;
1891 target
->list_size
= sub_size
;
1892 target
->remaining
= sub_size
;
1893 target
->working
= 1;
1896 pthread_mutex_lock(&target
->mutex
);
1897 target
->data_ready
= 1;
1898 pthread_cond_signal(&target
->cond
);
1899 pthread_mutex_unlock(&target
->mutex
);
1902 pthread_join(target
->thread
, NULL
);
1903 pthread_cond_destroy(&target
->cond
);
1904 pthread_mutex_destroy(&target
->mutex
);
1908 cleanup_threaded_search();
1913 #define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)
1916 static int add_ref_tag(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
1918 unsigned char peeled
[20];
1920 if (!prefixcmp(path
, "refs/tags/") && /* is a tag? */
1921 !peel_ref(path
, peeled
) && /* peelable? */
1922 packlist_find(&to_pack
, peeled
, NULL
)) /* object packed? */
1923 add_object_entry(sha1
, OBJ_TAG
, NULL
, 0);
1927 static void prepare_pack(int window
, int depth
)
1929 struct object_entry
**delta_list
;
1930 uint32_t i
, nr_deltas
;
1933 get_object_details();
1936 * If we're locally repacking then we need to be doubly careful
1937 * from now on in order to make sure no stealth corruption gets
1938 * propagated to the new pack. Clients receiving streamed packs
1939 * should validate everything they get anyway so no need to incur
1940 * the additional cost here in that case.
1942 if (!pack_to_stdout
)
1943 do_check_packed_object_crc
= 1;
1945 if (!to_pack
.nr_objects
|| !window
|| !depth
)
1948 delta_list
= xmalloc(to_pack
.nr_objects
* sizeof(*delta_list
));
1951 for (i
= 0; i
< to_pack
.nr_objects
; i
++) {
1952 struct object_entry
*entry
= to_pack
.objects
+ i
;
1955 /* This happens if we decided to reuse existing
1956 * delta from a pack. "reuse_delta &&" is implied.
1960 if (entry
->size
< 50)
1963 if (entry
->no_try_delta
)
1966 if (!entry
->preferred_base
) {
1968 if (entry
->type
< 0)
1969 die("unable to get type of object %s",
1970 sha1_to_hex(entry
->idx
.sha1
));
1972 if (entry
->type
< 0) {
1974 * This object is not found, but we
1975 * don't have to include it anyway.
1981 delta_list
[n
++] = entry
;
1984 if (nr_deltas
&& n
> 1) {
1985 unsigned nr_done
= 0;
1987 progress_state
= start_progress("Compressing objects",
1989 qsort(delta_list
, n
, sizeof(*delta_list
), type_size_sort
);
1990 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
1991 stop_progress(&progress_state
);
1992 if (nr_done
!= nr_deltas
)
1993 die("inconsistency with delta count");
1998 static int git_pack_config(const char *k
, const char *v
, void *cb
)
2000 if (!strcmp(k
, "pack.window")) {
2001 window
= git_config_int(k
, v
);
2004 if (!strcmp(k
, "pack.windowmemory")) {
2005 window_memory_limit
= git_config_ulong(k
, v
);
2008 if (!strcmp(k
, "pack.depth")) {
2009 depth
= git_config_int(k
, v
);
2012 if (!strcmp(k
, "pack.compression")) {
2013 int level
= git_config_int(k
, v
);
2015 level
= Z_DEFAULT_COMPRESSION
;
2016 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
2017 die("bad pack compression level %d", level
);
2018 pack_compression_level
= level
;
2019 pack_compression_seen
= 1;
2022 if (!strcmp(k
, "pack.deltacachesize")) {
2023 max_delta_cache_size
= git_config_int(k
, v
);
2026 if (!strcmp(k
, "pack.deltacachelimit")) {
2027 cache_max_small_delta_size
= git_config_int(k
, v
);
2030 if (!strcmp(k
, "pack.threads")) {
2031 delta_search_threads
= git_config_int(k
, v
);
2032 if (delta_search_threads
< 0)
2033 die("invalid number of threads specified (%d)",
2034 delta_search_threads
);
2036 if (delta_search_threads
!= 1)
2037 warning("no threads support, ignoring %s", k
);
2041 if (!strcmp(k
, "pack.indexversion")) {
2042 pack_idx_opts
.version
= git_config_int(k
, v
);
2043 if (pack_idx_opts
.version
> 2)
2044 die("bad pack.indexversion=%"PRIu32
,
2045 pack_idx_opts
.version
);
2048 return git_default_config(k
, v
, cb
);
2051 static void read_object_list_from_stdin(void)
2053 char line
[40 + 1 + PATH_MAX
+ 2];
2054 unsigned char sha1
[20];
2057 if (!fgets(line
, sizeof(line
), stdin
)) {
2061 die("fgets returned NULL, not EOF, not error!");
2067 if (line
[0] == '-') {
2068 if (get_sha1_hex(line
+1, sha1
))
2069 die("expected edge sha1, got garbage:\n %s",
2071 add_preferred_base(sha1
);
2074 if (get_sha1_hex(line
, sha1
))
2075 die("expected sha1, got garbage:\n %s", line
);
2077 add_preferred_base_object(line
+41);
2078 add_object_entry(sha1
, 0, line
+41, 0);
2082 #define OBJECT_ADDED (1u<<20)
2084 static void show_commit(struct commit
*commit
, void *data
)
2086 add_object_entry(commit
->object
.sha1
, OBJ_COMMIT
, NULL
, 0);
2087 commit
->object
.flags
|= OBJECT_ADDED
;
2090 static void show_object(struct object
*obj
,
2091 const struct name_path
*path
, const char *last
,
2094 char *name
= path_name(path
, last
);
2096 add_preferred_base_object(name
);
2097 add_object_entry(obj
->sha1
, obj
->type
, name
, 0);
2098 obj
->flags
|= OBJECT_ADDED
;
2101 * We will have generated the hash from the name,
2102 * but not saved a pointer to it - we can free it
2107 static void show_edge(struct commit
*commit
)
2109 add_preferred_base(commit
->object
.sha1
);
2112 struct in_pack_object
{
2114 struct object
*object
;
2120 struct in_pack_object
*array
;
2123 static void mark_in_pack_object(struct object
*object
, struct packed_git
*p
, struct in_pack
*in_pack
)
2125 in_pack
->array
[in_pack
->nr
].offset
= find_pack_entry_one(object
->sha1
, p
);
2126 in_pack
->array
[in_pack
->nr
].object
= object
;
2131 * Compare the objects in the offset order, in order to emulate the
2132 * "git rev-list --objects" output that produced the pack originally.
2134 static int ofscmp(const void *a_
, const void *b_
)
2136 struct in_pack_object
*a
= (struct in_pack_object
*)a_
;
2137 struct in_pack_object
*b
= (struct in_pack_object
*)b_
;
2139 if (a
->offset
< b
->offset
)
2141 else if (a
->offset
> b
->offset
)
2144 return hashcmp(a
->object
->sha1
, b
->object
->sha1
);
2147 static void add_objects_in_unpacked_packs(struct rev_info
*revs
)
2149 struct packed_git
*p
;
2150 struct in_pack in_pack
;
2153 memset(&in_pack
, 0, sizeof(in_pack
));
2155 for (p
= packed_git
; p
; p
= p
->next
) {
2156 const unsigned char *sha1
;
2159 if (!p
->pack_local
|| p
->pack_keep
)
2161 if (open_pack_index(p
))
2162 die("cannot open pack index");
2164 ALLOC_GROW(in_pack
.array
,
2165 in_pack
.nr
+ p
->num_objects
,
2168 for (i
= 0; i
< p
->num_objects
; i
++) {
2169 sha1
= nth_packed_object_sha1(p
, i
);
2170 o
= lookup_unknown_object(sha1
);
2171 if (!(o
->flags
& OBJECT_ADDED
))
2172 mark_in_pack_object(o
, p
, &in_pack
);
2173 o
->flags
|= OBJECT_ADDED
;
2178 qsort(in_pack
.array
, in_pack
.nr
, sizeof(in_pack
.array
[0]),
2180 for (i
= 0; i
< in_pack
.nr
; i
++) {
2181 struct object
*o
= in_pack
.array
[i
].object
;
2182 add_object_entry(o
->sha1
, o
->type
, "", 0);
2185 free(in_pack
.array
);
2188 static int has_sha1_pack_kept_or_nonlocal(const unsigned char *sha1
)
2190 static struct packed_git
*last_found
= (void *)1;
2191 struct packed_git
*p
;
2193 p
= (last_found
!= (void *)1) ? last_found
: packed_git
;
2196 if ((!p
->pack_local
|| p
->pack_keep
) &&
2197 find_pack_entry_one(sha1
, p
)) {
2201 if (p
== last_found
)
2205 if (p
== last_found
)
2211 static void loosen_unused_packed_objects(struct rev_info
*revs
)
2213 struct packed_git
*p
;
2215 const unsigned char *sha1
;
2217 for (p
= packed_git
; p
; p
= p
->next
) {
2218 if (!p
->pack_local
|| p
->pack_keep
)
2221 if (unpack_unreachable_expiration
&&
2222 p
->mtime
< unpack_unreachable_expiration
)
2225 if (open_pack_index(p
))
2226 die("cannot open pack index");
2228 for (i
= 0; i
< p
->num_objects
; i
++) {
2229 sha1
= nth_packed_object_sha1(p
, i
);
2230 if (!packlist_find(&to_pack
, sha1
, NULL
) &&
2231 !has_sha1_pack_kept_or_nonlocal(sha1
))
2232 if (force_object_loose(sha1
, p
->mtime
))
2233 die("unable to force loose object");
2238 static void get_object_list(int ac
, const char **av
)
2240 struct rev_info revs
;
2244 init_revisions(&revs
, NULL
);
2245 save_commit_buffer
= 0;
2246 setup_revisions(ac
, av
, &revs
, NULL
);
2248 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
2249 int len
= strlen(line
);
2250 if (len
&& line
[len
- 1] == '\n')
2255 if (!strcmp(line
, "--not")) {
2256 flags
^= UNINTERESTING
;
2259 die("not a rev '%s'", line
);
2261 if (handle_revision_arg(line
, &revs
, flags
, REVARG_CANNOT_BE_FILENAME
))
2262 die("bad revision '%s'", line
);
2265 if (prepare_revision_walk(&revs
))
2266 die("revision walk setup failed");
2267 mark_edges_uninteresting(&revs
, show_edge
);
2268 traverse_commit_list(&revs
, show_commit
, show_object
, NULL
);
2270 if (keep_unreachable
)
2271 add_objects_in_unpacked_packs(&revs
);
2272 if (unpack_unreachable
)
2273 loosen_unused_packed_objects(&revs
);
2276 static int option_parse_index_version(const struct option
*opt
,
2277 const char *arg
, int unset
)
2280 const char *val
= arg
;
2281 pack_idx_opts
.version
= strtoul(val
, &c
, 10);
2282 if (pack_idx_opts
.version
> 2)
2283 die(_("unsupported index version %s"), val
);
2284 if (*c
== ',' && c
[1])
2285 pack_idx_opts
.off32_limit
= strtoul(c
+1, &c
, 0);
2286 if (*c
|| pack_idx_opts
.off32_limit
& 0x80000000)
2287 die(_("bad index version '%s'"), val
);
2291 static int option_parse_unpack_unreachable(const struct option
*opt
,
2292 const char *arg
, int unset
)
2295 unpack_unreachable
= 0;
2296 unpack_unreachable_expiration
= 0;
2299 unpack_unreachable
= 1;
2301 unpack_unreachable_expiration
= approxidate(arg
);
2306 static int option_parse_ulong(const struct option
*opt
,
2307 const char *arg
, int unset
)
2310 die(_("option %s does not accept negative form"),
2313 if (!git_parse_ulong(arg
, opt
->value
))
2314 die(_("unable to parse value '%s' for option %s"),
2315 arg
, opt
->long_name
);
2319 #define OPT_ULONG(s, l, v, h) \
2320 { OPTION_CALLBACK, (s), (l), (v), "n", (h), \
2321 PARSE_OPT_NONEG, option_parse_ulong }
2323 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
2325 int use_internal_rev_list
= 0;
2327 int all_progress_implied
= 0;
2328 const char *rp_av
[6];
2330 int rev_list_unpacked
= 0, rev_list_all
= 0, rev_list_reflog
= 0;
2331 struct option pack_objects_options
[] = {
2332 OPT_SET_INT('q', "quiet", &progress
,
2333 N_("do not show progress meter"), 0),
2334 OPT_SET_INT(0, "progress", &progress
,
2335 N_("show progress meter"), 1),
2336 OPT_SET_INT(0, "all-progress", &progress
,
2337 N_("show progress meter during object writing phase"), 2),
2338 OPT_BOOL(0, "all-progress-implied",
2339 &all_progress_implied
,
2340 N_("similar to --all-progress when progress meter is shown")),
2341 { OPTION_CALLBACK
, 0, "index-version", NULL
, N_("version[,offset]"),
2342 N_("write the pack index file in the specified idx format version"),
2343 0, option_parse_index_version
},
2344 OPT_ULONG(0, "max-pack-size", &pack_size_limit
,
2345 N_("maximum size of each output pack file")),
2346 OPT_BOOL(0, "local", &local
,
2347 N_("ignore borrowed objects from alternate object store")),
2348 OPT_BOOL(0, "incremental", &incremental
,
2349 N_("ignore packed objects")),
2350 OPT_INTEGER(0, "window", &window
,
2351 N_("limit pack window by objects")),
2352 OPT_ULONG(0, "window-memory", &window_memory_limit
,
2353 N_("limit pack window by memory in addition to object limit")),
2354 OPT_INTEGER(0, "depth", &depth
,
2355 N_("maximum length of delta chain allowed in the resulting pack")),
2356 OPT_BOOL(0, "reuse-delta", &reuse_delta
,
2357 N_("reuse existing deltas")),
2358 OPT_BOOL(0, "reuse-object", &reuse_object
,
2359 N_("reuse existing objects")),
2360 OPT_BOOL(0, "delta-base-offset", &allow_ofs_delta
,
2361 N_("use OFS_DELTA objects")),
2362 OPT_INTEGER(0, "threads", &delta_search_threads
,
2363 N_("use threads when searching for best delta matches")),
2364 OPT_BOOL(0, "non-empty", &non_empty
,
2365 N_("do not create an empty pack output")),
2366 OPT_BOOL(0, "revs", &use_internal_rev_list
,
2367 N_("read revision arguments from standard input")),
2368 { OPTION_SET_INT
, 0, "unpacked", &rev_list_unpacked
, NULL
,
2369 N_("limit the objects to those that are not yet packed"),
2370 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2371 { OPTION_SET_INT
, 0, "all", &rev_list_all
, NULL
,
2372 N_("include objects reachable from any reference"),
2373 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2374 { OPTION_SET_INT
, 0, "reflog", &rev_list_reflog
, NULL
,
2375 N_("include objects referred by reflog entries"),
2376 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
2377 OPT_BOOL(0, "stdout", &pack_to_stdout
,
2378 N_("output pack to stdout")),
2379 OPT_BOOL(0, "include-tag", &include_tag
,
2380 N_("include tag objects that refer to objects to be packed")),
2381 OPT_BOOL(0, "keep-unreachable", &keep_unreachable
,
2382 N_("keep unreachable objects")),
2383 { OPTION_CALLBACK
, 0, "unpack-unreachable", NULL
, N_("time"),
2384 N_("unpack unreachable objects newer than <time>"),
2385 PARSE_OPT_OPTARG
, option_parse_unpack_unreachable
},
2386 OPT_BOOL(0, "thin", &thin
,
2387 N_("create thin packs")),
2388 OPT_BOOL(0, "honor-pack-keep", &ignore_packed_keep
,
2389 N_("ignore packs that have companion .keep file")),
2390 OPT_INTEGER(0, "compression", &pack_compression_level
,
2391 N_("pack compression level")),
2392 OPT_SET_INT(0, "keep-true-parents", &grafts_replace_parents
,
2393 N_("do not hide commits by grafts"), 0),
2397 read_replace_refs
= 0;
2399 reset_pack_idx_option(&pack_idx_opts
);
2400 git_config(git_pack_config
, NULL
);
2401 if (!pack_compression_seen
&& core_compression_seen
)
2402 pack_compression_level
= core_compression_level
;
2404 progress
= isatty(2);
2405 argc
= parse_options(argc
, argv
, prefix
, pack_objects_options
,
2409 base_name
= argv
[0];
2412 if (pack_to_stdout
!= !base_name
|| argc
)
2413 usage_with_options(pack_usage
, pack_objects_options
);
2415 rp_av
[rp_ac
++] = "pack-objects";
2417 use_internal_rev_list
= 1;
2418 rp_av
[rp_ac
++] = "--objects-edge";
2420 rp_av
[rp_ac
++] = "--objects";
2423 use_internal_rev_list
= 1;
2424 rp_av
[rp_ac
++] = "--all";
2426 if (rev_list_reflog
) {
2427 use_internal_rev_list
= 1;
2428 rp_av
[rp_ac
++] = "--reflog";
2430 if (rev_list_unpacked
) {
2431 use_internal_rev_list
= 1;
2432 rp_av
[rp_ac
++] = "--unpacked";
2437 if (pack_compression_level
== -1)
2438 pack_compression_level
= Z_DEFAULT_COMPRESSION
;
2439 else if (pack_compression_level
< 0 || pack_compression_level
> Z_BEST_COMPRESSION
)
2440 die("bad pack compression level %d", pack_compression_level
);
2442 if (delta_search_threads
!= 1)
2443 warning("no threads support, ignoring --threads");
2445 if (!pack_to_stdout
&& !pack_size_limit
)
2446 pack_size_limit
= pack_size_limit_cfg
;
2447 if (pack_to_stdout
&& pack_size_limit
)
2448 die("--max-pack-size cannot be used to build a pack for transfer.");
2449 if (pack_size_limit
&& pack_size_limit
< 1024*1024) {
2450 warning("minimum pack size limit is 1 MiB");
2451 pack_size_limit
= 1024*1024;
2454 if (!pack_to_stdout
&& thin
)
2455 die("--thin cannot be used to build an indexable pack.");
2457 if (keep_unreachable
&& unpack_unreachable
)
2458 die("--keep-unreachable and --unpack-unreachable are incompatible.");
2460 if (progress
&& all_progress_implied
)
2463 prepare_packed_git();
2466 progress_state
= start_progress("Counting objects", 0);
2467 if (!use_internal_rev_list
)
2468 read_object_list_from_stdin();
2470 rp_av
[rp_ac
] = NULL
;
2471 get_object_list(rp_ac
, rp_av
);
2473 cleanup_preferred_base();
2474 if (include_tag
&& nr_result
)
2475 for_each_ref(add_ref_tag
, NULL
);
2476 stop_progress(&progress_state
);
2478 if (non_empty
&& !nr_result
)
2481 prepare_pack(window
, depth
);
2484 fprintf(stderr
, "Total %"PRIu32
" (delta %"PRIu32
"),"
2485 " reused %"PRIu32
" (delta %"PRIu32
")\n",
2486 written
, written_delta
, reused
, reused_delta
);