11 #include "csum-file.h"
12 #include "tree-walk.h"
15 #include "list-objects.h"
18 #ifdef THREADED_DELTA_SEARCH
22 static const char pack_usage
[] = "\
23 git-pack-objects [{ -q | --progress | --all-progress }] \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]*] [--reflog] \n\
28 [--stdout | base-name] [<ref-list | <object-list]";
31 struct pack_idx_entry idx
;
32 unsigned long size
; /* uncompressed size */
33 struct packed_git
*in_pack
; /* already in pack */
35 struct object_entry
*delta
; /* delta base object */
36 struct object_entry
*delta_child
; /* deltified objects who bases me */
37 struct object_entry
*delta_sibling
; /* other deltified objects who
38 * uses the same base as me
40 void *delta_data
; /* cached delta (uncompressed) */
41 unsigned long delta_size
; /* delta data size (uncompressed) */
42 unsigned int hash
; /* name hint hash */
43 enum object_type type
;
44 enum object_type in_pack_type
; /* could be delta */
45 unsigned char in_pack_header_size
;
46 unsigned char preferred_base
; /* we do not pack this, but is available
47 * to be used as the base object to delta
50 unsigned char no_try_delta
;
54 * Objects we are going to pack are collected in objects array (dynamically
55 * expanded). nr_objects & nr_alloc controls this array. They are stored
56 * in the order we see -- typically rev-list --objects order that gives us
57 * nice "minimum seek" order.
59 static struct object_entry
*objects
;
60 static struct object_entry
**written_list
;
61 static uint32_t nr_objects
, nr_alloc
, nr_result
, nr_written
;
64 static int no_reuse_delta
, no_reuse_object
;
66 static int incremental
;
67 static int allow_ofs_delta
;
68 static const char *pack_tmp_name
, *idx_tmp_name
;
69 static char tmpname
[PATH_MAX
];
70 static const char *base_name
;
71 static int progress
= 1;
72 static int window
= 10;
73 static uint32_t pack_size_limit
;
74 static int depth
= 50;
75 static int delta_search_threads
= 1;
76 static int pack_to_stdout
;
77 static int num_preferred_base
;
78 static struct progress progress_state
;
79 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
80 static int pack_compression_seen
;
82 static unsigned long delta_cache_size
= 0;
83 static unsigned long max_delta_cache_size
= 0;
84 static unsigned long cache_max_small_delta_size
= 1000;
86 static unsigned long window_memory_limit
= 0;
89 * The object names in objects array are hashed with this hashtable,
90 * to help looking up the entry by object name.
91 * This hashtable is built after all the objects are seen.
93 static int *object_ix
;
94 static int object_ix_hashsz
;
97 * Pack index for existing packs give us easy access to the offsets into
98 * corresponding pack file where each object's data starts, but the entries
99 * do not store the size of the compressed representation (uncompressed
100 * size is easily available by examining the pack entry header). It is
101 * also rather expensive to find the sha1 for an object given its offset.
103 * We build a hashtable of existing packs (pack_revindex), and keep reverse
104 * index here -- pack index file is sorted by object name mapping to offset;
105 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
106 * ordered by offset, so if you know the offset of an object, next offset
107 * is where its packed representation ends and the index_nr can be used to
108 * get the object sha1 from the main index.
110 struct revindex_entry
{
114 struct pack_revindex
{
115 struct packed_git
*p
;
116 struct revindex_entry
*revindex
;
118 static struct pack_revindex
*pack_revindex
;
119 static int pack_revindex_hashsz
;
124 static uint32_t written
, written_delta
;
125 static uint32_t reused
, reused_delta
;
127 static int pack_revindex_ix(struct packed_git
*p
)
129 unsigned long ui
= (unsigned long)p
;
132 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
133 i
= (int)(ui
% pack_revindex_hashsz
);
134 while (pack_revindex
[i
].p
) {
135 if (pack_revindex
[i
].p
== p
)
137 if (++i
== pack_revindex_hashsz
)
143 static void prepare_pack_ix(void)
146 struct packed_git
*p
;
147 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
151 pack_revindex_hashsz
= num
* 11;
152 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
153 for (p
= packed_git
; p
; p
= p
->next
) {
154 num
= pack_revindex_ix(p
);
156 pack_revindex
[num
].p
= p
;
158 /* revindex elements are lazily initialized */
161 static int cmp_offset(const void *a_
, const void *b_
)
163 const struct revindex_entry
*a
= a_
;
164 const struct revindex_entry
*b
= b_
;
165 return (a
->offset
< b
->offset
) ? -1 : (a
->offset
> b
->offset
) ? 1 : 0;
169 * Ordered list of offsets of objects in the pack.
171 static void prepare_pack_revindex(struct pack_revindex
*rix
)
173 struct packed_git
*p
= rix
->p
;
174 int num_ent
= p
->num_objects
;
176 const char *index
= p
->index_data
;
178 rix
->revindex
= xmalloc(sizeof(*rix
->revindex
) * (num_ent
+ 1));
181 if (p
->index_version
> 1) {
182 const uint32_t *off_32
=
183 (uint32_t *)(index
+ 8 + p
->num_objects
* (20 + 4));
184 const uint32_t *off_64
= off_32
+ p
->num_objects
;
185 for (i
= 0; i
< num_ent
; i
++) {
186 uint32_t off
= ntohl(*off_32
++);
187 if (!(off
& 0x80000000)) {
188 rix
->revindex
[i
].offset
= off
;
190 rix
->revindex
[i
].offset
=
191 ((uint64_t)ntohl(*off_64
++)) << 32;
192 rix
->revindex
[i
].offset
|=
195 rix
->revindex
[i
].nr
= i
;
198 for (i
= 0; i
< num_ent
; i
++) {
199 uint32_t hl
= *((uint32_t *)(index
+ 24 * i
));
200 rix
->revindex
[i
].offset
= ntohl(hl
);
201 rix
->revindex
[i
].nr
= i
;
205 /* This knows the pack format -- the 20-byte trailer
206 * follows immediately after the last object data.
208 rix
->revindex
[num_ent
].offset
= p
->pack_size
- 20;
209 rix
->revindex
[num_ent
].nr
= -1;
210 qsort(rix
->revindex
, num_ent
, sizeof(*rix
->revindex
), cmp_offset
);
213 static struct revindex_entry
* find_packed_object(struct packed_git
*p
,
218 struct pack_revindex
*rix
;
219 struct revindex_entry
*revindex
;
220 num
= pack_revindex_ix(p
);
222 die("internal error: pack revindex uninitialized");
223 rix
= &pack_revindex
[num
];
225 prepare_pack_revindex(rix
);
226 revindex
= rix
->revindex
;
228 hi
= p
->num_objects
+ 1;
230 int mi
= (lo
+ hi
) / 2;
231 if (revindex
[mi
].offset
== ofs
) {
232 return revindex
+ mi
;
234 else if (ofs
< revindex
[mi
].offset
)
239 die("internal error: pack revindex corrupt");
242 static const unsigned char *find_packed_object_name(struct packed_git
*p
,
245 struct revindex_entry
*entry
= find_packed_object(p
, ofs
);
246 return nth_packed_object_sha1(p
, entry
->nr
);
249 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
251 unsigned long othersize
, delta_size
;
252 enum object_type type
;
253 void *otherbuf
= read_sha1_file(entry
->delta
->idx
.sha1
, &type
, &othersize
);
257 die("unable to read %s", sha1_to_hex(entry
->delta
->idx
.sha1
));
258 delta_buf
= diff_delta(otherbuf
, othersize
,
259 buf
, size
, &delta_size
, 0);
260 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
261 die("delta size changed");
268 * The per-object header is a pretty dense thing, which is
269 * - first byte: low four bits are "size", then three bits of "type",
270 * and the high bit is "size continues".
271 * - each byte afterwards: low seven bits are size continuation,
272 * with the high bit being "size continues"
274 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
279 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
280 die("bad type %d", type
);
282 c
= (type
<< 4) | (size
& 15);
295 * we are going to reuse the existing object data as is. make
296 * sure it is not corrupt.
298 static int check_pack_inflate(struct packed_git
*p
,
299 struct pack_window
**w_curs
,
302 unsigned long expect
)
305 unsigned char fakebuf
[4096], *in
;
308 memset(&stream
, 0, sizeof(stream
));
309 inflateInit(&stream
);
311 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
313 stream
.next_out
= fakebuf
;
314 stream
.avail_out
= sizeof(fakebuf
);
315 st
= inflate(&stream
, Z_FINISH
);
316 offset
+= stream
.next_in
- in
;
317 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
319 return (st
== Z_STREAM_END
&&
320 stream
.total_out
== expect
&&
321 stream
.total_in
== len
) ? 0 : -1;
324 static int check_pack_crc(struct packed_git
*p
, struct pack_window
**w_curs
,
325 off_t offset
, off_t len
, unsigned int nr
)
327 const uint32_t *index_crc
;
328 uint32_t data_crc
= crc32(0, Z_NULL
, 0);
332 void *data
= use_pack(p
, w_curs
, offset
, &avail
);
335 data_crc
= crc32(data_crc
, data
, avail
);
340 index_crc
= p
->index_data
;
341 index_crc
+= 2 + 256 + p
->num_objects
* (20/4) + nr
;
343 return data_crc
!= ntohl(*index_crc
);
346 static void copy_pack_data(struct sha1file
*f
,
347 struct packed_git
*p
,
348 struct pack_window
**w_curs
,
356 in
= use_pack(p
, w_curs
, offset
, &avail
);
358 avail
= (unsigned int)len
;
359 sha1write(f
, in
, avail
);
365 static unsigned long write_object(struct sha1file
*f
,
366 struct object_entry
*entry
,
370 enum object_type type
;
372 unsigned char header
[10];
373 unsigned char dheader
[10];
376 enum object_type obj_type
;
378 /* write limit if limited packsize and not first object */
379 unsigned long limit
= pack_size_limit
&& nr_written
?
380 pack_size_limit
- write_offset
: 0;
382 int usable_delta
= !entry
->delta
? 0 :
383 /* yes if unlimited packfile */
384 !pack_size_limit
? 1 :
385 /* no if base written to previous pack */
386 entry
->delta
->idx
.offset
== (off_t
)-1 ? 0 :
387 /* otherwise double-check written to this
388 * pack, like we do below
390 entry
->delta
->idx
.offset
? 1 : 0;
395 obj_type
= entry
->type
;
397 to_reuse
= 0; /* explicit */
398 else if (!entry
->in_pack
)
399 to_reuse
= 0; /* can't reuse what we don't have */
400 else if (obj_type
== OBJ_REF_DELTA
|| obj_type
== OBJ_OFS_DELTA
)
401 /* check_object() decided it for us ... */
402 to_reuse
= usable_delta
;
403 /* ... but pack split may override that */
404 else if (obj_type
!= entry
->in_pack_type
)
405 to_reuse
= 0; /* pack has delta which is unusable */
406 else if (entry
->delta
)
407 to_reuse
= 0; /* we want to pack afresh */
409 to_reuse
= 1; /* we have it in-pack undeltified,
410 * and we do not need to deltify it.
415 unsigned long maxsize
;
418 buf
= read_sha1_file(entry
->idx
.sha1
, &obj_type
, &size
);
420 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
421 } else if (entry
->delta_data
) {
422 size
= entry
->delta_size
;
423 buf
= entry
->delta_data
;
424 entry
->delta_data
= NULL
;
425 obj_type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
426 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
428 buf
= read_sha1_file(entry
->idx
.sha1
, &type
, &size
);
430 die("unable to read %s", sha1_to_hex(entry
->idx
.sha1
));
431 buf
= delta_against(buf
, size
, entry
);
432 size
= entry
->delta_size
;
433 obj_type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
434 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
436 /* compress the data to store and put compressed length in datalen */
437 memset(&stream
, 0, sizeof(stream
));
438 deflateInit(&stream
, pack_compression_level
);
439 maxsize
= deflateBound(&stream
, size
);
440 out
= xmalloc(maxsize
);
442 stream
.next_in
= buf
;
443 stream
.avail_in
= size
;
444 stream
.next_out
= out
;
445 stream
.avail_out
= maxsize
;
446 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
449 datalen
= stream
.total_out
;
452 * The object header is a byte of 'type' followed by zero or
453 * more bytes of length.
455 hdrlen
= encode_header(obj_type
, size
, header
);
457 if (obj_type
== OBJ_OFS_DELTA
) {
459 * Deltas with relative base contain an additional
460 * encoding of the relative offset for the delta
461 * base from this object's position in the pack.
463 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
464 unsigned pos
= sizeof(dheader
) - 1;
465 dheader
[pos
] = ofs
& 127;
467 dheader
[--pos
] = 128 | (--ofs
& 127);
468 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
473 sha1write(f
, header
, hdrlen
);
474 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
475 hdrlen
+= sizeof(dheader
) - pos
;
476 } else if (obj_type
== OBJ_REF_DELTA
) {
478 * Deltas with a base reference contain
479 * an additional 20 bytes for the base sha1.
481 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
486 sha1write(f
, header
, hdrlen
);
487 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
490 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
495 sha1write(f
, header
, hdrlen
);
497 sha1write(f
, out
, datalen
);
502 struct packed_git
*p
= entry
->in_pack
;
503 struct pack_window
*w_curs
= NULL
;
504 struct revindex_entry
*revidx
;
508 obj_type
= (allow_ofs_delta
&& entry
->delta
->idx
.offset
) ?
509 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
512 hdrlen
= encode_header(obj_type
, entry
->size
, header
);
513 offset
= entry
->in_pack_offset
;
514 revidx
= find_packed_object(p
, offset
);
515 datalen
= revidx
[1].offset
- offset
;
516 if (!pack_to_stdout
&& p
->index_version
> 1 &&
517 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
))
518 die("bad packed object CRC for %s", sha1_to_hex(entry
->idx
.sha1
));
519 offset
+= entry
->in_pack_header_size
;
520 datalen
-= entry
->in_pack_header_size
;
521 if (obj_type
== OBJ_OFS_DELTA
) {
522 off_t ofs
= entry
->idx
.offset
- entry
->delta
->idx
.offset
;
523 unsigned pos
= sizeof(dheader
) - 1;
524 dheader
[pos
] = ofs
& 127;
526 dheader
[--pos
] = 128 | (--ofs
& 127);
527 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
)
529 sha1write(f
, header
, hdrlen
);
530 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
531 hdrlen
+= sizeof(dheader
) - pos
;
532 } else if (obj_type
== OBJ_REF_DELTA
) {
533 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
)
535 sha1write(f
, header
, hdrlen
);
536 sha1write(f
, entry
->delta
->idx
.sha1
, 20);
539 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
)
541 sha1write(f
, header
, hdrlen
);
544 if (!pack_to_stdout
&& p
->index_version
== 1 &&
545 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
))
546 die("corrupt packed object for %s", sha1_to_hex(entry
->idx
.sha1
));
547 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
555 entry
->idx
.crc32
= crc32_end(f
);
556 return hdrlen
+ datalen
;
559 static off_t
write_one(struct sha1file
*f
,
560 struct object_entry
*e
,
565 /* offset is non zero if object is written already. */
566 if (e
->idx
.offset
|| e
->preferred_base
)
569 /* if we are deltified, write out base object first. */
571 offset
= write_one(f
, e
->delta
, offset
);
576 e
->idx
.offset
= offset
;
577 size
= write_object(f
, e
, offset
);
582 written_list
[nr_written
++] = e
;
584 /* make sure off_t is sufficiently large not to wrap */
585 if (offset
> offset
+ size
)
586 die("pack too large for current definition of off_t");
587 return offset
+ size
;
590 static int open_object_dir_tmp(const char *path
)
592 snprintf(tmpname
, sizeof(tmpname
), "%s/%s", get_object_directory(), path
);
593 return xmkstemp(tmpname
);
596 /* forward declaration for write_pack_file */
597 static int adjust_perm(const char *path
, mode_t mode
);
599 static void write_pack_file(void)
603 off_t offset
, offset_one
, last_obj_offset
= 0;
604 struct pack_header hdr
;
605 int do_progress
= progress
>> pack_to_stdout
;
606 uint32_t nr_remaining
= nr_result
;
609 start_progress(&progress_state
, "Writing %u objects...", "", nr_result
);
610 written_list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
613 unsigned char sha1
[20];
615 if (pack_to_stdout
) {
616 f
= sha1fd(1, "<stdout>");
618 int fd
= open_object_dir_tmp("tmp_pack_XXXXXX");
619 pack_tmp_name
= xstrdup(tmpname
);
620 f
= sha1fd(fd
, pack_tmp_name
);
623 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
624 hdr
.hdr_version
= htonl(PACK_VERSION
);
625 hdr
.hdr_entries
= htonl(nr_remaining
);
626 sha1write(f
, &hdr
, sizeof(hdr
));
627 offset
= sizeof(hdr
);
629 for (; i
< nr_objects
; i
++) {
630 last_obj_offset
= offset
;
631 offset_one
= write_one(f
, objects
+ i
, offset
);
636 display_progress(&progress_state
, written
);
640 * Did we write the wrong # entries in the header?
641 * If so, rewrite it like in fast-import
643 if (pack_to_stdout
|| nr_written
== nr_remaining
) {
644 sha1close(f
, sha1
, 1);
646 sha1close(f
, sha1
, 0);
647 fixup_pack_header_footer(f
->fd
, sha1
, pack_tmp_name
, nr_written
);
651 if (!pack_to_stdout
) {
652 mode_t mode
= umask(0);
657 idx_tmp_name
= write_idx_file(NULL
,
658 (struct pack_idx_entry
**) written_list
, nr_written
, sha1
);
659 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.pack",
660 base_name
, sha1_to_hex(sha1
));
661 if (adjust_perm(pack_tmp_name
, mode
))
662 die("unable to make temporary pack file readable: %s",
664 if (rename(pack_tmp_name
, tmpname
))
665 die("unable to rename temporary pack file: %s",
667 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.idx",
668 base_name
, sha1_to_hex(sha1
));
669 if (adjust_perm(idx_tmp_name
, mode
))
670 die("unable to make temporary index file readable: %s",
672 if (rename(idx_tmp_name
, tmpname
))
673 die("unable to rename temporary index file: %s",
675 puts(sha1_to_hex(sha1
));
678 /* mark written objects as written to previous pack */
679 for (j
= 0; j
< nr_written
; j
++) {
680 written_list
[j
]->idx
.offset
= (off_t
)-1;
682 nr_remaining
-= nr_written
;
683 } while (nr_remaining
&& i
< nr_objects
);
687 stop_progress(&progress_state
);
688 if (written
!= nr_result
)
689 die("wrote %u objects while expecting %u", written
, nr_result
);
691 * We have scanned through [0 ... i). Since we have written
692 * the correct number of objects, the remaining [i ... nr_objects)
693 * items must be either already written (due to out-of-order delta base)
694 * or a preferred base. Count those which are neither and complain if any.
696 for (j
= 0; i
< nr_objects
; i
++) {
697 struct object_entry
*e
= objects
+ i
;
698 j
+= !e
->idx
.offset
&& !e
->preferred_base
;
701 die("wrote %u objects as expected but %u unwritten", written
, j
);
704 static int locate_object_entry_hash(const unsigned char *sha1
)
708 memcpy(&ui
, sha1
, sizeof(unsigned int));
709 i
= ui
% object_ix_hashsz
;
710 while (0 < object_ix
[i
]) {
711 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].idx
.sha1
))
713 if (++i
== object_ix_hashsz
)
719 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
723 if (!object_ix_hashsz
)
726 i
= locate_object_entry_hash(sha1
);
728 return &objects
[object_ix
[i
]-1];
732 static void rehash_objects(void)
735 struct object_entry
*oe
;
737 object_ix_hashsz
= nr_objects
* 3;
738 if (object_ix_hashsz
< 1024)
739 object_ix_hashsz
= 1024;
740 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
741 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
742 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
743 int ix
= locate_object_entry_hash(oe
->idx
.sha1
);
747 object_ix
[ix
] = i
+ 1;
751 static unsigned name_hash(const char *name
)
760 * This effectively just creates a sortable number from the
761 * last sixteen non-whitespace characters. Last characters
762 * count "most", so things that end in ".c" sort together.
764 while ((c
= *name
++) != 0) {
767 hash
= (hash
>> 2) + (c
<< 24);
772 static void setup_delta_attr_check(struct git_attr_check
*check
)
774 static struct git_attr
*attr_delta
;
777 attr_delta
= git_attr("delta", 5);
779 check
[0].attr
= attr_delta
;
782 static int no_try_delta(const char *path
)
784 struct git_attr_check check
[1];
786 setup_delta_attr_check(check
);
787 if (git_checkattr(path
, ARRAY_SIZE(check
), check
))
789 if (ATTR_FALSE(check
->value
))
794 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
795 const char *name
, int exclude
)
797 struct object_entry
*entry
;
798 struct packed_git
*p
, *found_pack
= NULL
;
799 off_t found_offset
= 0;
801 unsigned hash
= name_hash(name
);
803 ix
= nr_objects
? locate_object_entry_hash(sha1
) : -1;
806 entry
= objects
+ object_ix
[ix
] - 1;
807 if (!entry
->preferred_base
)
809 entry
->preferred_base
= 1;
814 for (p
= packed_git
; p
; p
= p
->next
) {
815 off_t offset
= find_pack_entry_one(sha1
, p
);
818 found_offset
= offset
;
825 if (local
&& !p
->pack_local
)
830 if (nr_objects
>= nr_alloc
) {
831 nr_alloc
= (nr_alloc
+ 1024) * 3 / 2;
832 objects
= xrealloc(objects
, nr_alloc
* sizeof(*entry
));
835 entry
= objects
+ nr_objects
++;
836 memset(entry
, 0, sizeof(*entry
));
837 hashcpy(entry
->idx
.sha1
, sha1
);
842 entry
->preferred_base
= 1;
846 entry
->in_pack
= found_pack
;
847 entry
->in_pack_offset
= found_offset
;
850 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
853 object_ix
[-1 - ix
] = nr_objects
;
856 display_progress(&progress_state
, nr_objects
);
858 if (name
&& no_try_delta(name
))
859 entry
->no_try_delta
= 1;
864 struct pbase_tree_cache
{
865 unsigned char sha1
[20];
869 unsigned long tree_size
;
872 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
873 static int pbase_tree_cache_ix(const unsigned char *sha1
)
875 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
877 static int pbase_tree_cache_ix_incr(int ix
)
879 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
882 static struct pbase_tree
{
883 struct pbase_tree
*next
;
884 /* This is a phony "cache" entry; we are not
885 * going to evict it nor find it through _get()
886 * mechanism -- this is for the toplevel node that
887 * would almost always change with any commit.
889 struct pbase_tree_cache pcache
;
892 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
894 struct pbase_tree_cache
*ent
, *nent
;
897 enum object_type type
;
899 int my_ix
= pbase_tree_cache_ix(sha1
);
900 int available_ix
= -1;
902 /* pbase-tree-cache acts as a limited hashtable.
903 * your object will be found at your index or within a few
904 * slots after that slot if it is cached.
906 for (neigh
= 0; neigh
< 8; neigh
++) {
907 ent
= pbase_tree_cache
[my_ix
];
908 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
912 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
913 ((0 <= available_ix
) &&
914 (!ent
&& pbase_tree_cache
[available_ix
])))
915 available_ix
= my_ix
;
918 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
921 /* Did not find one. Either we got a bogus request or
922 * we need to read and perhaps cache.
924 data
= read_sha1_file(sha1
, &type
, &size
);
927 if (type
!= OBJ_TREE
) {
932 /* We need to either cache or return a throwaway copy */
934 if (available_ix
< 0)
937 ent
= pbase_tree_cache
[available_ix
];
938 my_ix
= available_ix
;
942 nent
= xmalloc(sizeof(*nent
));
943 nent
->temporary
= (available_ix
< 0);
946 /* evict and reuse */
947 free(ent
->tree_data
);
950 hashcpy(nent
->sha1
, sha1
);
951 nent
->tree_data
= data
;
952 nent
->tree_size
= size
;
954 if (!nent
->temporary
)
955 pbase_tree_cache
[my_ix
] = nent
;
959 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
961 if (!cache
->temporary
) {
965 free(cache
->tree_data
);
969 static int name_cmp_len(const char *name
)
972 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
977 static void add_pbase_object(struct tree_desc
*tree
,
980 const char *fullname
)
982 struct name_entry entry
;
985 while (tree_entry(tree
,&entry
)) {
986 if (S_ISGITLINK(entry
.mode
))
988 cmp
= tree_entry_len(entry
.path
, entry
.sha1
) != cmplen
? 1 :
989 memcmp(name
, entry
.path
, cmplen
);
994 if (name
[cmplen
] != '/') {
995 add_object_entry(entry
.sha1
,
996 S_ISDIR(entry
.mode
) ? OBJ_TREE
: OBJ_BLOB
,
1000 if (S_ISDIR(entry
.mode
)) {
1001 struct tree_desc sub
;
1002 struct pbase_tree_cache
*tree
;
1003 const char *down
= name
+cmplen
+1;
1004 int downlen
= name_cmp_len(down
);
1006 tree
= pbase_tree_get(entry
.sha1
);
1009 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1011 add_pbase_object(&sub
, down
, downlen
, fullname
);
1012 pbase_tree_put(tree
);
1017 static unsigned *done_pbase_paths
;
1018 static int done_pbase_paths_num
;
1019 static int done_pbase_paths_alloc
;
1020 static int done_pbase_path_pos(unsigned hash
)
1023 int hi
= done_pbase_paths_num
;
1025 int mi
= (hi
+ lo
) / 2;
1026 if (done_pbase_paths
[mi
] == hash
)
1028 if (done_pbase_paths
[mi
] < hash
)
1036 static int check_pbase_path(unsigned hash
)
1038 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1042 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1043 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1044 done_pbase_paths
= xrealloc(done_pbase_paths
,
1045 done_pbase_paths_alloc
*
1048 done_pbase_paths_num
++;
1049 if (pos
< done_pbase_paths_num
)
1050 memmove(done_pbase_paths
+ pos
+ 1,
1051 done_pbase_paths
+ pos
,
1052 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1053 done_pbase_paths
[pos
] = hash
;
1057 static void add_preferred_base_object(const char *name
)
1059 struct pbase_tree
*it
;
1061 unsigned hash
= name_hash(name
);
1063 if (!num_preferred_base
|| check_pbase_path(hash
))
1066 cmplen
= name_cmp_len(name
);
1067 for (it
= pbase_tree
; it
; it
= it
->next
) {
1069 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, NULL
, 1);
1072 struct tree_desc tree
;
1073 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1074 add_pbase_object(&tree
, name
, cmplen
, name
);
1079 static void add_preferred_base(unsigned char *sha1
)
1081 struct pbase_tree
*it
;
1084 unsigned char tree_sha1
[20];
1086 if (window
<= num_preferred_base
++)
1089 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1093 for (it
= pbase_tree
; it
; it
= it
->next
) {
1094 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1100 it
= xcalloc(1, sizeof(*it
));
1101 it
->next
= pbase_tree
;
1104 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1105 it
->pcache
.tree_data
= data
;
1106 it
->pcache
.tree_size
= size
;
1109 static void check_object(struct object_entry
*entry
)
1111 if (entry
->in_pack
) {
1112 struct packed_git
*p
= entry
->in_pack
;
1113 struct pack_window
*w_curs
= NULL
;
1114 const unsigned char *base_ref
= NULL
;
1115 struct object_entry
*base_entry
;
1116 unsigned long used
, used_0
;
1119 unsigned char *buf
, c
;
1121 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1124 * We want in_pack_type even if we do not reuse delta
1125 * since non-delta representations could still be reused.
1127 used
= unpack_object_header_gently(buf
, avail
,
1128 &entry
->in_pack_type
,
1132 * Determine if this is a delta and if so whether we can
1133 * reuse it or not. Otherwise let's find out as cheaply as
1134 * possible what the actual type and size for this object is.
1136 switch (entry
->in_pack_type
) {
1138 /* Not a delta hence we've already got all we need. */
1139 entry
->type
= entry
->in_pack_type
;
1140 entry
->in_pack_header_size
= used
;
1141 unuse_pack(&w_curs
);
1144 if (!no_reuse_delta
&& !entry
->preferred_base
)
1145 base_ref
= use_pack(p
, &w_curs
,
1146 entry
->in_pack_offset
+ used
, NULL
);
1147 entry
->in_pack_header_size
= used
+ 20;
1150 buf
= use_pack(p
, &w_curs
,
1151 entry
->in_pack_offset
+ used
, NULL
);
1157 if (!ofs
|| MSB(ofs
, 7))
1158 die("delta base offset overflow in pack for %s",
1159 sha1_to_hex(entry
->idx
.sha1
));
1161 ofs
= (ofs
<< 7) + (c
& 127);
1163 if (ofs
>= entry
->in_pack_offset
)
1164 die("delta base offset out of bound for %s",
1165 sha1_to_hex(entry
->idx
.sha1
));
1166 ofs
= entry
->in_pack_offset
- ofs
;
1167 if (!no_reuse_delta
&& !entry
->preferred_base
)
1168 base_ref
= find_packed_object_name(p
, ofs
);
1169 entry
->in_pack_header_size
= used
+ used_0
;
1173 if (base_ref
&& (base_entry
= locate_object_entry(base_ref
))) {
1175 * If base_ref was set above that means we wish to
1176 * reuse delta data, and we even found that base
1177 * in the list of objects we want to pack. Goodie!
1179 * Depth value does not matter - find_deltas() will
1180 * never consider reused delta as the base object to
1181 * deltify other objects against, in order to avoid
1184 entry
->type
= entry
->in_pack_type
;
1185 entry
->delta
= base_entry
;
1186 entry
->delta_sibling
= base_entry
->delta_child
;
1187 base_entry
->delta_child
= entry
;
1188 unuse_pack(&w_curs
);
1194 * This must be a delta and we already know what the
1195 * final object type is. Let's extract the actual
1196 * object size from the delta header.
1198 entry
->size
= get_size_from_delta(p
, &w_curs
,
1199 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1200 unuse_pack(&w_curs
);
1205 * No choice but to fall back to the recursive delta walk
1206 * with sha1_object_info() to find about the object type
1209 unuse_pack(&w_curs
);
1212 entry
->type
= sha1_object_info(entry
->idx
.sha1
, &entry
->size
);
1213 if (entry
->type
< 0)
1214 die("unable to get type of object %s",
1215 sha1_to_hex(entry
->idx
.sha1
));
1218 static int pack_offset_sort(const void *_a
, const void *_b
)
1220 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1221 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1223 /* avoid filesystem trashing with loose objects */
1224 if (!a
->in_pack
&& !b
->in_pack
)
1225 return hashcmp(a
->idx
.sha1
, b
->idx
.sha1
);
1227 if (a
->in_pack
< b
->in_pack
)
1229 if (a
->in_pack
> b
->in_pack
)
1231 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1232 (a
->in_pack_offset
> b
->in_pack_offset
);
1235 static void get_object_details(void)
1238 struct object_entry
**sorted_by_offset
;
1240 sorted_by_offset
= xcalloc(nr_objects
, sizeof(struct object_entry
*));
1241 for (i
= 0; i
< nr_objects
; i
++)
1242 sorted_by_offset
[i
] = objects
+ i
;
1243 qsort(sorted_by_offset
, nr_objects
, sizeof(*sorted_by_offset
), pack_offset_sort
);
1246 for (i
= 0; i
< nr_objects
; i
++)
1247 check_object(sorted_by_offset
[i
]);
1248 free(sorted_by_offset
);
1251 static int type_size_sort(const void *_a
, const void *_b
)
1253 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1254 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1256 if (a
->type
< b
->type
)
1258 if (a
->type
> b
->type
)
1260 if (a
->hash
< b
->hash
)
1262 if (a
->hash
> b
->hash
)
1264 if (a
->preferred_base
< b
->preferred_base
)
1266 if (a
->preferred_base
> b
->preferred_base
)
1268 if (a
->size
< b
->size
)
1270 if (a
->size
> b
->size
)
1272 return a
> b
? -1 : (a
< b
); /* newest last */
1276 struct object_entry
*entry
;
1278 struct delta_index
*index
;
1282 static int delta_cacheable(unsigned long src_size
, unsigned long trg_size
,
1283 unsigned long delta_size
)
1285 if (max_delta_cache_size
&& delta_cache_size
+ delta_size
> max_delta_cache_size
)
1288 if (delta_size
< cache_max_small_delta_size
)
1291 /* cache delta, if objects are large enough compared to delta size */
1292 if ((src_size
>> 20) + (trg_size
>> 21) > (delta_size
>> 10))
1298 #ifdef THREADED_DELTA_SEARCH
1300 static pthread_mutex_t read_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1301 #define read_lock() pthread_mutex_lock(&read_mutex)
1302 #define read_unlock() pthread_mutex_unlock(&read_mutex)
1304 static pthread_mutex_t cache_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1305 #define cache_lock() pthread_mutex_lock(&cache_mutex)
1306 #define cache_unlock() pthread_mutex_unlock(&cache_mutex)
1308 static pthread_mutex_t progress_mutex
= PTHREAD_MUTEX_INITIALIZER
;
1309 #define progress_lock() pthread_mutex_lock(&progress_mutex)
1310 #define progress_unlock() pthread_mutex_unlock(&progress_mutex)
1314 #define read_lock() (void)0
1315 #define read_unlock() (void)0
1316 #define cache_lock() (void)0
1317 #define cache_unlock() (void)0
1318 #define progress_lock() (void)0
1319 #define progress_unlock() (void)0
1324 * We search for deltas _backwards_ in a list sorted by type and
1325 * by size, so that we see progressively smaller and smaller files.
1326 * That's because we prefer deltas to be from the bigger file
1327 * to the smaller - deletes are potentially cheaper, but perhaps
1328 * more importantly, the bigger file is likely the more recent
1331 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1332 unsigned max_depth
, unsigned long *mem_usage
)
1334 struct object_entry
*trg_entry
= trg
->entry
;
1335 struct object_entry
*src_entry
= src
->entry
;
1336 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1338 enum object_type type
;
1341 /* Don't bother doing diffs between different types */
1342 if (trg_entry
->type
!= src_entry
->type
)
1346 * We do not bother to try a delta that we discarded
1347 * on an earlier try, but only when reusing delta data.
1349 if (!no_reuse_delta
&& trg_entry
->in_pack
&&
1350 trg_entry
->in_pack
== src_entry
->in_pack
&&
1351 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1352 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1355 /* Let's not bust the allowed depth. */
1356 if (src
->depth
>= max_depth
)
1359 /* Now some size filtering heuristics. */
1360 trg_size
= trg_entry
->size
;
1361 if (!trg_entry
->delta
) {
1362 max_size
= trg_size
/2 - 20;
1365 max_size
= trg_entry
->delta_size
;
1366 ref_depth
= trg
->depth
;
1368 max_size
= max_size
* (max_depth
- src
->depth
) /
1369 (max_depth
- ref_depth
+ 1);
1372 src_size
= src_entry
->size
;
1373 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1374 if (sizediff
>= max_size
)
1376 if (trg_size
< src_size
/ 32)
1379 /* Load data if not already done */
1382 trg
->data
= read_sha1_file(trg_entry
->idx
.sha1
, &type
, &sz
);
1385 die("object %s cannot be read",
1386 sha1_to_hex(trg_entry
->idx
.sha1
));
1388 die("object %s inconsistent object length (%lu vs %lu)",
1389 sha1_to_hex(trg_entry
->idx
.sha1
), sz
, trg_size
);
1394 src
->data
= read_sha1_file(src_entry
->idx
.sha1
, &type
, &sz
);
1397 die("object %s cannot be read",
1398 sha1_to_hex(src_entry
->idx
.sha1
));
1400 die("object %s inconsistent object length (%lu vs %lu)",
1401 sha1_to_hex(src_entry
->idx
.sha1
), sz
, src_size
);
1405 src
->index
= create_delta_index(src
->data
, src_size
);
1407 static int warned
= 0;
1409 warning("suboptimal pack - out of memory");
1412 *mem_usage
+= sizeof_delta_index(src
->index
);
1415 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1419 if (trg_entry
->delta
) {
1420 /* Prefer only shallower same-sized deltas. */
1421 if (delta_size
== trg_entry
->delta_size
&&
1422 src
->depth
+ 1 >= trg
->depth
) {
1428 trg_entry
->delta
= src_entry
;
1429 trg_entry
->delta_size
= delta_size
;
1430 trg
->depth
= src
->depth
+ 1;
1433 * Handle memory allocation outside of the cache
1434 * accounting lock. Compiler will optimize the strangeness
1435 * away when THREADED_DELTA_SEARCH is not defined.
1437 if (trg_entry
->delta_data
)
1438 free(trg_entry
->delta_data
);
1440 if (trg_entry
->delta_data
) {
1441 delta_cache_size
-= trg_entry
->delta_size
;
1442 trg_entry
->delta_data
= NULL
;
1444 if (delta_cacheable(src_size
, trg_size
, delta_size
)) {
1445 delta_cache_size
+= trg_entry
->delta_size
;
1447 trg_entry
->delta_data
= xrealloc(delta_buf
, delta_size
);
1456 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1458 struct object_entry
*child
= me
->delta_child
;
1461 unsigned int c
= check_delta_limit(child
, n
+ 1);
1464 child
= child
->delta_sibling
;
1469 static unsigned long free_unpacked(struct unpacked
*n
)
1471 unsigned long freed_mem
= sizeof_delta_index(n
->index
);
1472 free_delta_index(n
->index
);
1475 freed_mem
+= n
->entry
->size
;
1484 static void find_deltas(struct object_entry
**list
, unsigned list_size
,
1485 int window
, int depth
, unsigned *processed
)
1487 uint32_t i
= list_size
, idx
= 0, count
= 0;
1488 unsigned int array_size
= window
* sizeof(struct unpacked
);
1489 struct unpacked
*array
;
1490 unsigned long mem_usage
= 0;
1492 array
= xmalloc(array_size
);
1493 memset(array
, 0, array_size
);
1496 struct object_entry
*entry
= list
[--i
];
1497 struct unpacked
*n
= array
+ idx
;
1498 int j
, max_depth
, best_base
= -1;
1500 mem_usage
-= free_unpacked(n
);
1503 while (window_memory_limit
&&
1504 mem_usage
> window_memory_limit
&&
1506 uint32_t tail
= (idx
+ window
- count
) % window
;
1507 mem_usage
-= free_unpacked(array
+ tail
);
1511 /* We do not compute delta to *create* objects we are not
1514 if (entry
->preferred_base
)
1520 display_progress(&progress_state
, *processed
);
1524 * If the current object is at pack edge, take the depth the
1525 * objects that depend on the current object into account
1526 * otherwise they would become too deep.
1529 if (entry
->delta_child
) {
1530 max_depth
-= check_delta_limit(entry
, 0);
1538 uint32_t other_idx
= idx
+ j
;
1540 if (other_idx
>= window
)
1541 other_idx
-= window
;
1542 m
= array
+ other_idx
;
1545 ret
= try_delta(n
, m
, max_depth
, &mem_usage
);
1549 best_base
= other_idx
;
1552 /* if we made n a delta, and if n is already at max
1553 * depth, leaving it in the window is pointless. we
1554 * should evict it first.
1556 if (entry
->delta
&& depth
<= n
->depth
)
1560 * Move the best delta base up in the window, after the
1561 * currently deltified object, to keep it longer. It will
1562 * be the first base object to be attempted next.
1565 struct unpacked swap
= array
[best_base
];
1566 int dist
= (window
+ idx
- best_base
) % window
;
1567 int dst
= best_base
;
1569 int src
= (dst
+ 1) % window
;
1570 array
[dst
] = array
[src
];
1578 if (count
+ 1 < window
)
1584 for (i
= 0; i
< window
; ++i
) {
1585 free_delta_index(array
[i
].index
);
1586 free(array
[i
].data
);
1591 #ifdef THREADED_DELTA_SEARCH
1593 struct thread_params
{
1595 struct object_entry
**list
;
1599 unsigned *processed
;
1602 static pthread_mutex_t data_request
= PTHREAD_MUTEX_INITIALIZER
;
1603 static pthread_mutex_t data_ready
= PTHREAD_MUTEX_INITIALIZER
;
1604 static pthread_mutex_t data_provider
= PTHREAD_MUTEX_INITIALIZER
;
1605 static struct thread_params
*data_requester
;
1607 static void *threaded_find_deltas(void *arg
)
1609 struct thread_params
*me
= arg
;
1612 pthread_mutex_lock(&data_request
);
1613 data_requester
= me
;
1614 pthread_mutex_unlock(&data_provider
);
1615 pthread_mutex_lock(&data_ready
);
1616 pthread_mutex_unlock(&data_request
);
1621 find_deltas(me
->list
, me
->list_size
,
1622 me
->window
, me
->depth
, me
->processed
);
1626 static void ll_find_deltas(struct object_entry
**list
, unsigned list_size
,
1627 int window
, int depth
, unsigned *processed
)
1629 struct thread_params
*target
, p
[delta_search_threads
];
1631 unsigned chunk_size
;
1633 if (delta_search_threads
<= 1) {
1634 find_deltas(list
, list_size
, window
, depth
, processed
);
1638 pthread_mutex_lock(&data_provider
);
1639 pthread_mutex_lock(&data_ready
);
1641 for (i
= 0; i
< delta_search_threads
; i
++) {
1642 p
[i
].window
= window
;
1644 p
[i
].processed
= processed
;
1645 ret
= pthread_create(&p
[i
].thread
, NULL
,
1646 threaded_find_deltas
, &p
[i
]);
1648 die("unable to create thread: %s", strerror(ret
));
1651 /* this should be auto-tuned somehow */
1652 chunk_size
= window
* 1000;
1655 unsigned sublist_size
= chunk_size
;
1656 if (sublist_size
> list_size
)
1657 sublist_size
= list_size
;
1659 /* try to split chunks on "path" boundaries */
1660 while (sublist_size
< list_size
&& list
[sublist_size
]->hash
&&
1661 list
[sublist_size
]->hash
== list
[sublist_size
-1]->hash
)
1664 pthread_mutex_lock(&data_provider
);
1665 target
= data_requester
;
1666 target
->list
= list
;
1667 target
->list_size
= sublist_size
;
1668 pthread_mutex_unlock(&data_ready
);
1670 list
+= sublist_size
;
1671 list_size
-= sublist_size
;
1672 if (!sublist_size
) {
1673 pthread_join(target
->thread
, NULL
);
1680 #define ll_find_deltas find_deltas
1683 static void prepare_pack(int window
, int depth
)
1685 struct object_entry
**delta_list
;
1686 uint32_t i
, n
, nr_deltas
;
1688 get_object_details();
1690 if (!nr_objects
|| !window
|| !depth
)
1693 delta_list
= xmalloc(nr_objects
* sizeof(*delta_list
));
1696 for (i
= 0; i
< nr_objects
; i
++) {
1697 struct object_entry
*entry
= objects
+ i
;
1700 /* This happens if we decided to reuse existing
1701 * delta from a pack. "!no_reuse_delta &&" is implied.
1705 if (entry
->size
< 50)
1708 if (entry
->no_try_delta
)
1711 if (!entry
->preferred_base
)
1714 delta_list
[n
++] = entry
;
1718 unsigned nr_done
= 0;
1720 start_progress(&progress_state
,
1721 "Deltifying %u objects...", "",
1723 qsort(delta_list
, n
, sizeof(*delta_list
), type_size_sort
);
1724 ll_find_deltas(delta_list
, n
, window
+1, depth
, &nr_done
);
1726 stop_progress(&progress_state
);
1727 if (nr_done
!= nr_deltas
)
1728 die("inconsistency with delta count");
1733 static int git_pack_config(const char *k
, const char *v
)
1735 if(!strcmp(k
, "pack.window")) {
1736 window
= git_config_int(k
, v
);
1739 if (!strcmp(k
, "pack.windowmemory")) {
1740 window_memory_limit
= git_config_ulong(k
, v
);
1743 if (!strcmp(k
, "pack.depth")) {
1744 depth
= git_config_int(k
, v
);
1747 if (!strcmp(k
, "pack.compression")) {
1748 int level
= git_config_int(k
, v
);
1750 level
= Z_DEFAULT_COMPRESSION
;
1751 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1752 die("bad pack compression level %d", level
);
1753 pack_compression_level
= level
;
1754 pack_compression_seen
= 1;
1757 if (!strcmp(k
, "pack.deltacachesize")) {
1758 max_delta_cache_size
= git_config_int(k
, v
);
1761 if (!strcmp(k
, "pack.deltacachelimit")) {
1762 cache_max_small_delta_size
= git_config_int(k
, v
);
1765 if (!strcmp(k
, "pack.threads")) {
1766 delta_search_threads
= git_config_int(k
, v
);
1767 if (delta_search_threads
< 1)
1768 die("invalid number of threads specified (%d)",
1769 delta_search_threads
);
1770 #ifndef THREADED_DELTA_SEARCH
1771 if (delta_search_threads
> 1)
1772 warning("no threads support, ignoring %s", k
);
1776 return git_default_config(k
, v
);
1779 static void read_object_list_from_stdin(void)
1781 char line
[40 + 1 + PATH_MAX
+ 2];
1782 unsigned char sha1
[20];
1785 if (!fgets(line
, sizeof(line
), stdin
)) {
1789 die("fgets returned NULL, not EOF, not error!");
1791 die("fgets: %s", strerror(errno
));
1795 if (line
[0] == '-') {
1796 if (get_sha1_hex(line
+1, sha1
))
1797 die("expected edge sha1, got garbage:\n %s",
1799 add_preferred_base(sha1
);
1802 if (get_sha1_hex(line
, sha1
))
1803 die("expected sha1, got garbage:\n %s", line
);
1805 add_preferred_base_object(line
+41);
1806 add_object_entry(sha1
, 0, line
+41, 0);
1810 static void show_commit(struct commit
*commit
)
1812 add_object_entry(commit
->object
.sha1
, OBJ_COMMIT
, NULL
, 0);
1815 static void show_object(struct object_array_entry
*p
)
1817 add_preferred_base_object(p
->name
);
1818 add_object_entry(p
->item
->sha1
, p
->item
->type
, p
->name
, 0);
1821 static void show_edge(struct commit
*commit
)
1823 add_preferred_base(commit
->object
.sha1
);
1826 static void get_object_list(int ac
, const char **av
)
1828 struct rev_info revs
;
1832 init_revisions(&revs
, NULL
);
1833 save_commit_buffer
= 0;
1834 track_object_refs
= 0;
1835 setup_revisions(ac
, av
, &revs
, NULL
);
1837 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
1838 int len
= strlen(line
);
1839 if (line
[len
- 1] == '\n')
1844 if (!strcmp(line
, "--not")) {
1845 flags
^= UNINTERESTING
;
1848 die("not a rev '%s'", line
);
1850 if (handle_revision_arg(line
, &revs
, flags
, 1))
1851 die("bad revision '%s'", line
);
1854 prepare_revision_walk(&revs
);
1855 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
1856 traverse_commit_list(&revs
, show_commit
, show_object
);
1859 static int adjust_perm(const char *path
, mode_t mode
)
1861 if (chmod(path
, mode
))
1863 return adjust_shared_perm(path
);
1866 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
1868 int use_internal_rev_list
= 0;
1872 int rp_ac_alloc
= 64;
1875 rp_av
= xcalloc(rp_ac_alloc
, sizeof(*rp_av
));
1877 rp_av
[0] = "pack-objects";
1878 rp_av
[1] = "--objects"; /* --thin will make it --objects-edge */
1881 git_config(git_pack_config
);
1882 if (!pack_compression_seen
&& core_compression_seen
)
1883 pack_compression_level
= core_compression_level
;
1885 progress
= isatty(2);
1886 for (i
= 1; i
< argc
; i
++) {
1887 const char *arg
= argv
[i
];
1892 if (!strcmp("--non-empty", arg
)) {
1896 if (!strcmp("--local", arg
)) {
1900 if (!strcmp("--incremental", arg
)) {
1904 if (!prefixcmp(arg
, "--compression=")) {
1906 int level
= strtoul(arg
+14, &end
, 0);
1907 if (!arg
[14] || *end
)
1910 level
= Z_DEFAULT_COMPRESSION
;
1911 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1912 die("bad pack compression level %d", level
);
1913 pack_compression_level
= level
;
1916 if (!prefixcmp(arg
, "--max-pack-size=")) {
1918 pack_size_limit
= strtoul(arg
+16, &end
, 0) * 1024 * 1024;
1919 if (!arg
[16] || *end
)
1923 if (!prefixcmp(arg
, "--window=")) {
1925 window
= strtoul(arg
+9, &end
, 0);
1926 if (!arg
[9] || *end
)
1930 if (!prefixcmp(arg
, "--window-memory=")) {
1931 if (!git_parse_ulong(arg
+16, &window_memory_limit
))
1935 if (!prefixcmp(arg
, "--threads=")) {
1937 delta_search_threads
= strtoul(arg
+10, &end
, 0);
1938 if (!arg
[10] || *end
|| delta_search_threads
< 1)
1940 #ifndef THREADED_DELTA_SEARCH
1941 if (delta_search_threads
> 1)
1942 warning("no threads support, "
1943 "ignoring %s", arg
);
1947 if (!prefixcmp(arg
, "--depth=")) {
1949 depth
= strtoul(arg
+8, &end
, 0);
1950 if (!arg
[8] || *end
)
1954 if (!strcmp("--progress", arg
)) {
1958 if (!strcmp("--all-progress", arg
)) {
1962 if (!strcmp("-q", arg
)) {
1966 if (!strcmp("--no-reuse-delta", arg
)) {
1970 if (!strcmp("--no-reuse-object", arg
)) {
1971 no_reuse_object
= no_reuse_delta
= 1;
1974 if (!strcmp("--delta-base-offset", arg
)) {
1975 allow_ofs_delta
= 1;
1978 if (!strcmp("--stdout", arg
)) {
1982 if (!strcmp("--revs", arg
)) {
1983 use_internal_rev_list
= 1;
1986 if (!strcmp("--unpacked", arg
) ||
1987 !prefixcmp(arg
, "--unpacked=") ||
1988 !strcmp("--reflog", arg
) ||
1989 !strcmp("--all", arg
)) {
1990 use_internal_rev_list
= 1;
1991 if (rp_ac
>= rp_ac_alloc
- 1) {
1992 rp_ac_alloc
= alloc_nr(rp_ac_alloc
);
1993 rp_av
= xrealloc(rp_av
,
1994 rp_ac_alloc
* sizeof(*rp_av
));
1996 rp_av
[rp_ac
++] = arg
;
1999 if (!strcmp("--thin", arg
)) {
2000 use_internal_rev_list
= 1;
2002 rp_av
[1] = "--objects-edge";
2005 if (!prefixcmp(arg
, "--index-version=")) {
2007 pack_idx_default_version
= strtoul(arg
+ 16, &c
, 10);
2008 if (pack_idx_default_version
> 2)
2011 pack_idx_off32_limit
= strtoul(c
+1, &c
, 0);
2012 if (*c
|| pack_idx_off32_limit
& 0x80000000)
2019 /* Traditionally "pack-objects [options] base extra" failed;
2020 * we would however want to take refs parameter that would
2021 * have been given to upstream rev-list ourselves, which means
2022 * we somehow want to say what the base name is. So the
2025 * pack-objects [options] base <refs...>
2027 * in other words, we would treat the first non-option as the
2028 * base_name and send everything else to the internal revision
2032 if (!pack_to_stdout
)
2033 base_name
= argv
[i
++];
2035 if (pack_to_stdout
!= !base_name
)
2038 if (pack_to_stdout
&& pack_size_limit
)
2039 die("--max-pack-size cannot be used to build a pack for transfer.");
2041 if (!pack_to_stdout
&& thin
)
2042 die("--thin cannot be used to build an indexable pack.");
2044 prepare_packed_git();
2047 start_progress(&progress_state
, "Generating pack...",
2048 "Counting objects: ", 0);
2049 if (!use_internal_rev_list
)
2050 read_object_list_from_stdin();
2052 rp_av
[rp_ac
] = NULL
;
2053 get_object_list(rp_ac
, rp_av
);
2056 stop_progress(&progress_state
);
2057 fprintf(stderr
, "Done counting %u objects.\n", nr_objects
);
2060 if (non_empty
&& !nr_result
)
2062 if (progress
&& (nr_objects
!= nr_result
))
2063 fprintf(stderr
, "Result has %u objects.\n", nr_result
);
2065 prepare_pack(window
, depth
);
2068 fprintf(stderr
, "Total %u (delta %u), reused %u (delta %u)\n",
2069 written
, written_delta
, reused
, reused_delta
);