10 #include "csum-file.h"
11 #include "tree-walk.h"
14 #include "list-objects.h"
16 static const char pack_usage
[] = "\
17 git-pack-objects [{ -q | --progress | --all-progress }] \n\
18 [--local] [--incremental] [--window=N] [--depth=N] \n\
19 [--no-reuse-delta] [--delta-base-offset] [--non-empty] \n\
20 [--revs [--unpacked | --all]*] [--reflog] [--stdout | base-name] \n\
21 [<ref-list | <object-list]";
24 unsigned char sha1
[20];
25 unsigned long size
; /* uncompressed size */
26 off_t offset
; /* offset into the final pack file;
27 * nonzero if already written.
29 unsigned int depth
; /* delta depth */
30 unsigned int hash
; /* name hint hash */
31 enum object_type type
;
32 enum object_type in_pack_type
; /* could be delta */
33 unsigned long delta_size
; /* delta data size (uncompressed) */
34 #define in_pack_header_size delta_size /* only when reusing pack data */
35 struct object_entry
*delta
; /* delta base object */
36 struct packed_git
*in_pack
; /* already in pack */
38 struct object_entry
*delta_child
; /* deltified objects who bases me */
39 struct object_entry
*delta_sibling
; /* other deltified objects who
40 * uses the same base as me
42 int preferred_base
; /* we do not pack this, but is encouraged to
43 * be used as the base objectto delta huge
46 uint32_t crc32
; /* crc of raw pack data for this object */
50 * Objects we are going to pack are collected in objects array (dynamically
51 * expanded). nr_objects & nr_alloc controls this array. They are stored
52 * in the order we see -- typically rev-list --objects order that gives us
53 * nice "minimum seek" order.
55 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
56 * elements in the objects array. The former is used to build the pack
57 * index (lists object names in the ascending order to help offset lookup),
58 * and the latter is used to group similar things together by try_delta()
63 static int no_reuse_delta
;
65 static int incremental
;
66 static int allow_ofs_delta
;
68 static struct object_entry
*objects
;
69 static uint32_t nr_objects
, nr_alloc
, nr_result
;
70 static const char *pack_tmp_name
, *idx_tmp_name
;
71 static char tmpname
[PATH_MAX
];
72 static unsigned char pack_file_sha1
[20];
73 static int progress
= 1;
74 static volatile sig_atomic_t progress_update
;
75 static int window
= 10;
76 static int pack_to_stdout
;
77 static int num_preferred_base
;
80 * The object names in objects array are hashed with this hashtable,
81 * to help looking up the entry by object name. Binary search from
82 * sorted_by_sha is also possible but this was easier to code and faster.
83 * This hashtable is built after all the objects are seen.
85 static int *object_ix
;
86 static int object_ix_hashsz
;
89 * Pack index for existing packs give us easy access to the offsets into
90 * corresponding pack file where each object's data starts, but the entries
91 * do not store the size of the compressed representation (uncompressed
92 * size is easily available by examining the pack entry header). It is
93 * also rather expensive to find the sha1 for an object given its offset.
95 * We build a hashtable of existing packs (pack_revindex), and keep reverse
96 * index here -- pack index file is sorted by object name mapping to offset;
97 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
98 * ordered by offset, so if you know the offset of an object, next offset
99 * is where its packed representation ends and the index_nr can be used to
100 * get the object sha1 from the main index.
102 struct revindex_entry
{
106 struct pack_revindex
{
107 struct packed_git
*p
;
108 struct revindex_entry
*revindex
;
110 static struct pack_revindex
*pack_revindex
;
111 static int pack_revindex_hashsz
;
116 static uint32_t written
, written_delta
;
117 static uint32_t reused
, reused_delta
;
119 static int pack_revindex_ix(struct packed_git
*p
)
121 unsigned long ui
= (unsigned long)p
;
124 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
125 i
= (int)(ui
% pack_revindex_hashsz
);
126 while (pack_revindex
[i
].p
) {
127 if (pack_revindex
[i
].p
== p
)
129 if (++i
== pack_revindex_hashsz
)
135 static void prepare_pack_ix(void)
138 struct packed_git
*p
;
139 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
143 pack_revindex_hashsz
= num
* 11;
144 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
145 for (p
= packed_git
; p
; p
= p
->next
) {
146 num
= pack_revindex_ix(p
);
148 pack_revindex
[num
].p
= p
;
150 /* revindex elements are lazily initialized */
153 static int cmp_offset(const void *a_
, const void *b_
)
155 const struct revindex_entry
*a
= a_
;
156 const struct revindex_entry
*b
= b_
;
157 return (a
->offset
< b
->offset
) ? -1 : (a
->offset
> b
->offset
) ? 1 : 0;
161 * Ordered list of offsets of objects in the pack.
163 static void prepare_pack_revindex(struct pack_revindex
*rix
)
165 struct packed_git
*p
= rix
->p
;
166 int num_ent
= p
->num_objects
;
168 const char *index
= p
->index_data
;
170 rix
->revindex
= xmalloc(sizeof(*rix
->revindex
) * (num_ent
+ 1));
173 if (p
->index_version
> 1) {
174 const uint32_t *off_32
=
175 (uint32_t *)(index
+ 8 + p
->num_objects
* (20 + 4));
176 const uint32_t *off_64
= off_32
+ p
->num_objects
;
177 for (i
= 0; i
< num_ent
; i
++) {
178 uint32_t off
= ntohl(*off_32
++);
179 if (!(off
& 0x80000000)) {
180 rix
->revindex
[i
].offset
= off
;
182 rix
->revindex
[i
].offset
=
183 ((uint64_t)ntohl(*off_64
++)) << 32;
184 rix
->revindex
[i
].offset
|=
187 rix
->revindex
[i
].nr
= i
;
190 for (i
= 0; i
< num_ent
; i
++) {
191 uint32_t hl
= *((uint32_t *)(index
+ 24 * i
));
192 rix
->revindex
[i
].offset
= ntohl(hl
);
193 rix
->revindex
[i
].nr
= i
;
197 /* This knows the pack format -- the 20-byte trailer
198 * follows immediately after the last object data.
200 rix
->revindex
[num_ent
].offset
= p
->pack_size
- 20;
201 rix
->revindex
[num_ent
].nr
= -1;
202 qsort(rix
->revindex
, num_ent
, sizeof(*rix
->revindex
), cmp_offset
);
205 static struct revindex_entry
* find_packed_object(struct packed_git
*p
,
210 struct pack_revindex
*rix
;
211 struct revindex_entry
*revindex
;
212 num
= pack_revindex_ix(p
);
214 die("internal error: pack revindex uninitialized");
215 rix
= &pack_revindex
[num
];
217 prepare_pack_revindex(rix
);
218 revindex
= rix
->revindex
;
220 hi
= p
->num_objects
+ 1;
222 int mi
= (lo
+ hi
) / 2;
223 if (revindex
[mi
].offset
== ofs
) {
224 return revindex
+ mi
;
226 else if (ofs
< revindex
[mi
].offset
)
231 die("internal error: pack revindex corrupt");
234 static const unsigned char *find_packed_object_name(struct packed_git
*p
,
237 struct revindex_entry
*entry
= find_packed_object(p
, ofs
);
238 return nth_packed_object_sha1(p
, entry
->nr
);
241 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
243 unsigned long othersize
, delta_size
;
244 enum object_type type
;
245 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, &type
, &othersize
);
249 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
250 delta_buf
= diff_delta(otherbuf
, othersize
,
251 buf
, size
, &delta_size
, 0);
252 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
253 die("delta size changed");
260 * The per-object header is a pretty dense thing, which is
261 * - first byte: low four bits are "size", then three bits of "type",
262 * and the high bit is "size continues".
263 * - each byte afterwards: low seven bits are size continuation,
264 * with the high bit being "size continues"
266 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
271 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
272 die("bad type %d", type
);
274 c
= (type
<< 4) | (size
& 15);
287 * we are going to reuse the existing object data as is. make
288 * sure it is not corrupt.
290 static int check_pack_inflate(struct packed_git
*p
,
291 struct pack_window
**w_curs
,
294 unsigned long expect
)
297 unsigned char fakebuf
[4096], *in
;
300 memset(&stream
, 0, sizeof(stream
));
301 inflateInit(&stream
);
303 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
305 stream
.next_out
= fakebuf
;
306 stream
.avail_out
= sizeof(fakebuf
);
307 st
= inflate(&stream
, Z_FINISH
);
308 offset
+= stream
.next_in
- in
;
309 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
311 return (st
== Z_STREAM_END
&&
312 stream
.total_out
== expect
&&
313 stream
.total_in
== len
) ? 0 : -1;
316 static int check_pack_crc(struct packed_git
*p
, struct pack_window
**w_curs
,
317 off_t offset
, off_t len
, unsigned int nr
)
319 const uint32_t *index_crc
;
320 uint32_t data_crc
= crc32(0, Z_NULL
, 0);
324 void *data
= use_pack(p
, w_curs
, offset
, &avail
);
327 data_crc
= crc32(data_crc
, data
, avail
);
332 index_crc
= p
->index_data
;
333 index_crc
+= 2 + 256 + p
->num_objects
* (20/4) + nr
;
335 return data_crc
!= ntohl(*index_crc
);
338 static void copy_pack_data(struct sha1file
*f
,
339 struct packed_git
*p
,
340 struct pack_window
**w_curs
,
348 in
= use_pack(p
, w_curs
, offset
, &avail
);
350 avail
= (unsigned int)len
;
351 sha1write(f
, in
, avail
);
357 static int check_loose_inflate(unsigned char *data
, unsigned long len
, unsigned long expect
)
360 unsigned char fakebuf
[4096];
363 memset(&stream
, 0, sizeof(stream
));
364 stream
.next_in
= data
;
365 stream
.avail_in
= len
;
366 stream
.next_out
= fakebuf
;
367 stream
.avail_out
= sizeof(fakebuf
);
368 inflateInit(&stream
);
371 st
= inflate(&stream
, Z_FINISH
);
372 if (st
== Z_STREAM_END
|| st
== Z_OK
) {
373 st
= (stream
.total_out
== expect
&&
374 stream
.total_in
== len
) ? 0 : -1;
377 if (st
!= Z_BUF_ERROR
) {
381 stream
.next_out
= fakebuf
;
382 stream
.avail_out
= sizeof(fakebuf
);
388 static int revalidate_loose_object(struct object_entry
*entry
,
390 unsigned long mapsize
)
392 /* we already know this is a loose object with new type header. */
393 enum object_type type
;
394 unsigned long size
, used
;
399 used
= unpack_object_header_gently(map
, mapsize
, &type
, &size
);
404 return check_loose_inflate(map
, mapsize
, size
);
407 static unsigned long write_object(struct sha1file
*f
,
408 struct object_entry
*entry
)
411 enum object_type type
;
413 unsigned char header
[10];
416 enum object_type obj_type
;
422 obj_type
= entry
->type
;
423 if (! entry
->in_pack
)
424 to_reuse
= 0; /* can't reuse what we don't have */
425 else if (obj_type
== OBJ_REF_DELTA
|| obj_type
== OBJ_OFS_DELTA
)
426 to_reuse
= 1; /* check_object() decided it for us */
427 else if (obj_type
!= entry
->in_pack_type
)
428 to_reuse
= 0; /* pack has delta which is unusable */
429 else if (entry
->delta
)
430 to_reuse
= 0; /* we want to pack afresh */
432 to_reuse
= 1; /* we have it in-pack undeltified,
433 * and we do not need to deltify it.
436 if (!entry
->in_pack
&& !entry
->delta
) {
438 unsigned long mapsize
;
439 map
= map_sha1_file(entry
->sha1
, &mapsize
);
440 if (map
&& !legacy_loose_object(map
)) {
441 /* We can copy straight into the pack file */
442 if (revalidate_loose_object(entry
, map
, mapsize
))
443 die("corrupt loose object %s",
444 sha1_to_hex(entry
->sha1
));
445 sha1write(f
, map
, mapsize
);
446 munmap(map
, mapsize
);
452 munmap(map
, mapsize
);
456 buf
= read_sha1_file(entry
->sha1
, &type
, &size
);
458 die("unable to read %s", sha1_to_hex(entry
->sha1
));
459 if (size
!= entry
->size
)
460 die("object %s size inconsistency (%lu vs %lu)",
461 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
463 buf
= delta_against(buf
, size
, entry
);
464 size
= entry
->delta_size
;
465 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
466 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
469 * The object header is a byte of 'type' followed by zero or
470 * more bytes of length.
472 hdrlen
= encode_header(obj_type
, size
, header
);
473 sha1write(f
, header
, hdrlen
);
475 if (obj_type
== OBJ_OFS_DELTA
) {
477 * Deltas with relative base contain an additional
478 * encoding of the relative offset for the delta
479 * base from this object's position in the pack.
481 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
482 unsigned pos
= sizeof(header
) - 1;
483 header
[pos
] = ofs
& 127;
485 header
[--pos
] = 128 | (--ofs
& 127);
486 sha1write(f
, header
+ pos
, sizeof(header
) - pos
);
487 hdrlen
+= sizeof(header
) - pos
;
488 } else if (obj_type
== OBJ_REF_DELTA
) {
490 * Deltas with a base reference contain
491 * an additional 20 bytes for the base sha1.
493 sha1write(f
, entry
->delta
->sha1
, 20);
496 datalen
= sha1write_compressed(f
, buf
, size
);
500 struct packed_git
*p
= entry
->in_pack
;
501 struct pack_window
*w_curs
= NULL
;
502 struct revindex_entry
*revidx
;
506 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
507 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
510 hdrlen
= encode_header(obj_type
, entry
->size
, header
);
511 sha1write(f
, header
, hdrlen
);
512 if (obj_type
== OBJ_OFS_DELTA
) {
513 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
514 unsigned pos
= sizeof(header
) - 1;
515 header
[pos
] = ofs
& 127;
517 header
[--pos
] = 128 | (--ofs
& 127);
518 sha1write(f
, header
+ pos
, sizeof(header
) - pos
);
519 hdrlen
+= sizeof(header
) - pos
;
520 } else if (obj_type
== OBJ_REF_DELTA
) {
521 sha1write(f
, entry
->delta
->sha1
, 20);
525 offset
= entry
->in_pack_offset
;
526 revidx
= find_packed_object(p
, offset
);
527 datalen
= revidx
[1].offset
- offset
;
528 if (!pack_to_stdout
&& p
->index_version
> 1 &&
529 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
))
530 die("bad packed object CRC for %s", sha1_to_hex(entry
->sha1
));
531 offset
+= entry
->in_pack_header_size
;
532 datalen
-= entry
->in_pack_header_size
;
533 if (!pack_to_stdout
&& p
->index_version
== 1 &&
534 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
))
535 die("corrupt packed object for %s", sha1_to_hex(entry
->sha1
));
536 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
544 entry
->crc32
= crc32_end(f
);
545 return hdrlen
+ datalen
;
548 static off_t
write_one(struct sha1file
*f
,
549 struct object_entry
*e
,
554 /* offset is non zero if object is written already. */
555 if (e
->offset
|| e
->preferred_base
)
558 /* if we are deltified, write out base object first. */
560 offset
= write_one(f
, e
->delta
, offset
);
563 size
= write_object(f
, e
);
565 /* make sure off_t is sufficiently large not to wrap */
566 if (offset
> offset
+ size
)
567 die("pack too large for current definition of off_t");
568 return offset
+ size
;
571 static off_t
write_pack_file(void)
575 off_t offset
, last_obj_offset
= 0;
576 struct pack_header hdr
;
577 unsigned last_percent
= 999;
578 int do_progress
= progress
;
580 if (pack_to_stdout
) {
581 f
= sha1fd(1, "<stdout>");
585 snprintf(tmpname
, sizeof(tmpname
), "tmp_pack_XXXXXX");
586 fd
= mkstemp(tmpname
);
588 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
589 pack_tmp_name
= xstrdup(tmpname
);
590 f
= sha1fd(fd
, pack_tmp_name
);
594 fprintf(stderr
, "Writing %u objects.\n", nr_result
);
596 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
597 hdr
.hdr_version
= htonl(PACK_VERSION
);
598 hdr
.hdr_entries
= htonl(nr_result
);
599 sha1write(f
, &hdr
, sizeof(hdr
));
600 offset
= sizeof(hdr
);
603 for (i
= 0; i
< nr_objects
; i
++) {
604 last_obj_offset
= offset
;
605 offset
= write_one(f
, objects
+ i
, offset
);
607 unsigned percent
= written
* 100 / nr_result
;
608 if (progress_update
|| percent
!= last_percent
) {
609 fprintf(stderr
, "%4u%% (%u/%u) done\r",
610 percent
, written
, nr_result
);
612 last_percent
= percent
;
619 if (written
!= nr_result
)
620 die("wrote %u objects while expecting %u", written
, nr_result
);
621 sha1close(f
, pack_file_sha1
, 1);
623 return last_obj_offset
;
626 static int sha1_sort(const void *_a
, const void *_b
)
628 const struct object_entry
*a
= *(struct object_entry
**)_a
;
629 const struct object_entry
*b
= *(struct object_entry
**)_b
;
630 return hashcmp(a
->sha1
, b
->sha1
);
633 static uint32_t index_default_version
= 1;
634 static uint32_t index_off32_limit
= 0x7fffffff;
636 static void write_index_file(off_t last_obj_offset
, unsigned char *sha1
)
639 struct object_entry
**sorted_by_sha
, **list
, **last
;
641 uint32_t i
, index_version
;
645 snprintf(tmpname
, sizeof(tmpname
), "tmp_idx_XXXXXX");
646 fd
= mkstemp(tmpname
);
648 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
649 idx_tmp_name
= xstrdup(tmpname
);
650 f
= sha1fd(fd
, idx_tmp_name
);
655 xcalloc(nr_result
, sizeof(struct object_entry
*));
656 for (i
= 0; i
< nr_objects
; i
++)
657 if (!objects
[i
].preferred_base
)
658 sorted_by_sha
[j
++] = objects
+ i
;
660 die("listed %u objects while expecting %u", j
, nr_result
);
661 qsort(sorted_by_sha
, nr_result
, sizeof(*sorted_by_sha
), sha1_sort
);
662 list
= sorted_by_sha
;
663 last
= sorted_by_sha
+ nr_result
;
665 sorted_by_sha
= list
= last
= NULL
;
667 /* if last object's offset is >= 2^31 we should use index V2 */
668 index_version
= (last_obj_offset
>> 31) ? 2 : index_default_version
;
670 /* index versions 2 and above need a header */
671 if (index_version
>= 2) {
672 struct pack_idx_header hdr
;
673 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
674 hdr
.idx_version
= htonl(index_version
);
675 sha1write(f
, &hdr
, sizeof(hdr
));
679 * Write the first-level table (the list is sorted,
680 * but we use a 256-entry lookup to be able to avoid
681 * having to do eight extra binary search iterations).
683 for (i
= 0; i
< 256; i
++) {
684 struct object_entry
**next
= list
;
685 while (next
< last
) {
686 struct object_entry
*entry
= *next
;
687 if (entry
->sha1
[0] != i
)
691 array
[i
] = htonl(next
- sorted_by_sha
);
694 sha1write(f
, array
, 256 * 4);
696 /* Compute the SHA1 hash of sorted object names. */
699 /* Write the actual SHA1 entries. */
700 list
= sorted_by_sha
;
701 for (i
= 0; i
< nr_result
; i
++) {
702 struct object_entry
*entry
= *list
++;
703 if (index_version
< 2) {
704 uint32_t offset
= htonl(entry
->offset
);
705 sha1write(f
, &offset
, 4);
707 sha1write(f
, entry
->sha1
, 20);
708 SHA1_Update(&ctx
, entry
->sha1
, 20);
711 if (index_version
>= 2) {
712 unsigned int nr_large_offset
= 0;
714 /* write the crc32 table */
715 list
= sorted_by_sha
;
716 for (i
= 0; i
< nr_objects
; i
++) {
717 struct object_entry
*entry
= *list
++;
718 uint32_t crc32_val
= htonl(entry
->crc32
);
719 sha1write(f
, &crc32_val
, 4);
722 /* write the 32-bit offset table */
723 list
= sorted_by_sha
;
724 for (i
= 0; i
< nr_objects
; i
++) {
725 struct object_entry
*entry
= *list
++;
726 uint32_t offset
= (entry
->offset
<= index_off32_limit
) ?
727 entry
->offset
: (0x80000000 | nr_large_offset
++);
728 offset
= htonl(offset
);
729 sha1write(f
, &offset
, 4);
732 /* write the large offset table */
733 list
= sorted_by_sha
;
734 while (nr_large_offset
) {
735 struct object_entry
*entry
= *list
++;
736 uint64_t offset
= entry
->offset
;
737 if (offset
> index_off32_limit
) {
739 split
[0] = htonl(offset
>> 32);
740 split
[1] = htonl(offset
& 0xffffffff);
741 sha1write(f
, split
, 8);
747 sha1write(f
, pack_file_sha1
, 20);
748 sha1close(f
, NULL
, 1);
750 SHA1_Final(sha1
, &ctx
);
753 static int locate_object_entry_hash(const unsigned char *sha1
)
757 memcpy(&ui
, sha1
, sizeof(unsigned int));
758 i
= ui
% object_ix_hashsz
;
759 while (0 < object_ix
[i
]) {
760 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].sha1
))
762 if (++i
== object_ix_hashsz
)
768 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
772 if (!object_ix_hashsz
)
775 i
= locate_object_entry_hash(sha1
);
777 return &objects
[object_ix
[i
]-1];
781 static void rehash_objects(void)
784 struct object_entry
*oe
;
786 object_ix_hashsz
= nr_objects
* 3;
787 if (object_ix_hashsz
< 1024)
788 object_ix_hashsz
= 1024;
789 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
790 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
791 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
792 int ix
= locate_object_entry_hash(oe
->sha1
);
796 object_ix
[ix
] = i
+ 1;
800 static unsigned name_hash(const char *name
)
806 * This effectively just creates a sortable number from the
807 * last sixteen non-whitespace characters. Last characters
808 * count "most", so things that end in ".c" sort together.
810 while ((c
= *name
++) != 0) {
813 hash
= (hash
>> 2) + (c
<< 24);
818 static int add_object_entry(const unsigned char *sha1
, unsigned hash
, int exclude
)
820 struct object_entry
*entry
;
821 struct packed_git
*p
, *found_pack
= NULL
;
822 off_t found_offset
= 0;
825 ix
= nr_objects
? locate_object_entry_hash(sha1
) : -1;
828 entry
= objects
+ object_ix
[ix
] - 1;
829 if (!entry
->preferred_base
)
831 entry
->preferred_base
= 1;
837 for (p
= packed_git
; p
; p
= p
->next
) {
838 off_t offset
= find_pack_entry_one(sha1
, p
);
842 if (local
&& !p
->pack_local
)
845 found_offset
= offset
;
852 if (nr_objects
>= nr_alloc
) {
853 nr_alloc
= (nr_alloc
+ 1024) * 3 / 2;
854 objects
= xrealloc(objects
, nr_alloc
* sizeof(*entry
));
857 entry
= objects
+ nr_objects
++;
858 memset(entry
, 0, sizeof(*entry
));
859 hashcpy(entry
->sha1
, sha1
);
862 entry
->preferred_base
= 1;
866 entry
->in_pack
= found_pack
;
867 entry
->in_pack_offset
= found_offset
;
870 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
873 object_ix
[-1 - ix
] = nr_objects
;
875 if (progress_update
) {
876 fprintf(stderr
, "Counting objects...%u\r", nr_objects
);
883 struct pbase_tree_cache
{
884 unsigned char sha1
[20];
888 unsigned long tree_size
;
891 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
892 static int pbase_tree_cache_ix(const unsigned char *sha1
)
894 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
896 static int pbase_tree_cache_ix_incr(int ix
)
898 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
901 static struct pbase_tree
{
902 struct pbase_tree
*next
;
903 /* This is a phony "cache" entry; we are not
904 * going to evict it nor find it through _get()
905 * mechanism -- this is for the toplevel node that
906 * would almost always change with any commit.
908 struct pbase_tree_cache pcache
;
911 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
913 struct pbase_tree_cache
*ent
, *nent
;
916 enum object_type type
;
918 int my_ix
= pbase_tree_cache_ix(sha1
);
919 int available_ix
= -1;
921 /* pbase-tree-cache acts as a limited hashtable.
922 * your object will be found at your index or within a few
923 * slots after that slot if it is cached.
925 for (neigh
= 0; neigh
< 8; neigh
++) {
926 ent
= pbase_tree_cache
[my_ix
];
927 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
931 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
932 ((0 <= available_ix
) &&
933 (!ent
&& pbase_tree_cache
[available_ix
])))
934 available_ix
= my_ix
;
937 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
940 /* Did not find one. Either we got a bogus request or
941 * we need to read and perhaps cache.
943 data
= read_sha1_file(sha1
, &type
, &size
);
946 if (type
!= OBJ_TREE
) {
951 /* We need to either cache or return a throwaway copy */
953 if (available_ix
< 0)
956 ent
= pbase_tree_cache
[available_ix
];
957 my_ix
= available_ix
;
961 nent
= xmalloc(sizeof(*nent
));
962 nent
->temporary
= (available_ix
< 0);
965 /* evict and reuse */
966 free(ent
->tree_data
);
969 hashcpy(nent
->sha1
, sha1
);
970 nent
->tree_data
= data
;
971 nent
->tree_size
= size
;
973 if (!nent
->temporary
)
974 pbase_tree_cache
[my_ix
] = nent
;
978 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
980 if (!cache
->temporary
) {
984 free(cache
->tree_data
);
988 static int name_cmp_len(const char *name
)
991 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
996 static void add_pbase_object(struct tree_desc
*tree
,
999 const char *fullname
)
1001 struct name_entry entry
;
1004 while (tree_entry(tree
,&entry
)) {
1005 cmp
= tree_entry_len(entry
.path
, entry
.sha1
) != cmplen
? 1 :
1006 memcmp(name
, entry
.path
, cmplen
);
1011 if (name
[cmplen
] != '/') {
1012 unsigned hash
= name_hash(fullname
);
1013 add_object_entry(entry
.sha1
, hash
, 1);
1016 if (S_ISDIR(entry
.mode
)) {
1017 struct tree_desc sub
;
1018 struct pbase_tree_cache
*tree
;
1019 const char *down
= name
+cmplen
+1;
1020 int downlen
= name_cmp_len(down
);
1022 tree
= pbase_tree_get(entry
.sha1
);
1025 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1027 add_pbase_object(&sub
, down
, downlen
, fullname
);
1028 pbase_tree_put(tree
);
1033 static unsigned *done_pbase_paths
;
1034 static int done_pbase_paths_num
;
1035 static int done_pbase_paths_alloc
;
1036 static int done_pbase_path_pos(unsigned hash
)
1039 int hi
= done_pbase_paths_num
;
1041 int mi
= (hi
+ lo
) / 2;
1042 if (done_pbase_paths
[mi
] == hash
)
1044 if (done_pbase_paths
[mi
] < hash
)
1052 static int check_pbase_path(unsigned hash
)
1054 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1058 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1059 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1060 done_pbase_paths
= xrealloc(done_pbase_paths
,
1061 done_pbase_paths_alloc
*
1064 done_pbase_paths_num
++;
1065 if (pos
< done_pbase_paths_num
)
1066 memmove(done_pbase_paths
+ pos
+ 1,
1067 done_pbase_paths
+ pos
,
1068 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1069 done_pbase_paths
[pos
] = hash
;
1073 static void add_preferred_base_object(const char *name
, unsigned hash
)
1075 struct pbase_tree
*it
;
1078 if (!num_preferred_base
|| check_pbase_path(hash
))
1081 cmplen
= name_cmp_len(name
);
1082 for (it
= pbase_tree
; it
; it
= it
->next
) {
1084 add_object_entry(it
->pcache
.sha1
, 0, 1);
1087 struct tree_desc tree
;
1088 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1089 add_pbase_object(&tree
, name
, cmplen
, name
);
1094 static void add_preferred_base(unsigned char *sha1
)
1096 struct pbase_tree
*it
;
1099 unsigned char tree_sha1
[20];
1101 if (window
<= num_preferred_base
++)
1104 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1108 for (it
= pbase_tree
; it
; it
= it
->next
) {
1109 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1115 it
= xcalloc(1, sizeof(*it
));
1116 it
->next
= pbase_tree
;
1119 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1120 it
->pcache
.tree_data
= data
;
1121 it
->pcache
.tree_size
= size
;
1124 static void check_object(struct object_entry
*entry
)
1126 if (entry
->in_pack
&& !entry
->preferred_base
) {
1127 struct packed_git
*p
= entry
->in_pack
;
1128 struct pack_window
*w_curs
= NULL
;
1129 unsigned long size
, used
;
1132 struct object_entry
*base_entry
= NULL
;
1134 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1136 /* We want in_pack_type even if we do not reuse delta.
1137 * There is no point not reusing non-delta representations.
1139 used
= unpack_object_header_gently(buf
, avail
,
1140 &entry
->in_pack_type
, &size
);
1142 /* Check if it is delta, and the base is also an object
1143 * we are going to pack. If so we will reuse the existing
1146 if (!no_reuse_delta
) {
1148 const unsigned char *base_name
;
1150 unsigned long used_0
;
1151 /* there is at least 20 bytes left in the pack */
1152 switch (entry
->in_pack_type
) {
1154 base_name
= use_pack(p
, &w_curs
,
1155 entry
->in_pack_offset
+ used
, NULL
);
1159 buf
= use_pack(p
, &w_curs
,
1160 entry
->in_pack_offset
+ used
, NULL
);
1166 if (!ofs
|| MSB(ofs
, 7))
1167 die("delta base offset overflow in pack for %s",
1168 sha1_to_hex(entry
->sha1
));
1170 ofs
= (ofs
<< 7) + (c
& 127);
1172 if (ofs
>= entry
->in_pack_offset
)
1173 die("delta base offset out of bound for %s",
1174 sha1_to_hex(entry
->sha1
));
1175 ofs
= entry
->in_pack_offset
- ofs
;
1176 base_name
= find_packed_object_name(p
, ofs
);
1183 base_entry
= locate_object_entry(base_name
);
1185 unuse_pack(&w_curs
);
1186 entry
->in_pack_header_size
= used
;
1190 /* Depth value does not matter - find_deltas()
1191 * will never consider reused delta as the
1192 * base object to deltify other objects
1193 * against, in order to avoid circular deltas.
1196 /* uncompressed size of the delta data */
1198 entry
->delta
= base_entry
;
1199 entry
->type
= entry
->in_pack_type
;
1201 entry
->delta_sibling
= base_entry
->delta_child
;
1202 base_entry
->delta_child
= entry
;
1206 /* Otherwise we would do the usual */
1209 entry
->type
= sha1_object_info(entry
->sha1
, &entry
->size
);
1210 if (entry
->type
< 0)
1211 die("unable to get type of object %s",
1212 sha1_to_hex(entry
->sha1
));
1215 static void get_object_details(void)
1218 struct object_entry
*entry
;
1221 for (i
= 0, entry
= objects
; i
< nr_objects
; i
++, entry
++)
1222 check_object(entry
);
1225 static int type_size_sort(const void *_a
, const void *_b
)
1227 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1228 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1230 if (a
->type
< b
->type
)
1232 if (a
->type
> b
->type
)
1234 if (a
->hash
< b
->hash
)
1236 if (a
->hash
> b
->hash
)
1238 if (a
->preferred_base
< b
->preferred_base
)
1240 if (a
->preferred_base
> b
->preferred_base
)
1242 if (a
->size
< b
->size
)
1244 if (a
->size
> b
->size
)
1246 return a
> b
? -1 : (a
< b
); /* newest last */
1250 struct object_entry
*entry
;
1252 struct delta_index
*index
;
1256 * We search for deltas _backwards_ in a list sorted by type and
1257 * by size, so that we see progressively smaller and smaller files.
1258 * That's because we prefer deltas to be from the bigger file
1259 * to the smaller - deletes are potentially cheaper, but perhaps
1260 * more importantly, the bigger file is likely the more recent
1263 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1266 struct object_entry
*trg_entry
= trg
->entry
;
1267 struct object_entry
*src_entry
= src
->entry
;
1268 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1269 enum object_type type
;
1272 /* Don't bother doing diffs between different types */
1273 if (trg_entry
->type
!= src_entry
->type
)
1276 /* We do not compute delta to *create* objects we are not
1279 if (trg_entry
->preferred_base
)
1283 * We do not bother to try a delta that we discarded
1284 * on an earlier try, but only when reusing delta data.
1286 if (!no_reuse_delta
&& trg_entry
->in_pack
&&
1287 trg_entry
->in_pack
== src_entry
->in_pack
&&
1288 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1289 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1292 /* Let's not bust the allowed depth. */
1293 if (src_entry
->depth
>= max_depth
)
1296 /* Now some size filtering heuristics. */
1297 trg_size
= trg_entry
->size
;
1298 max_size
= trg_size
/2 - 20;
1299 max_size
= max_size
* (max_depth
- src_entry
->depth
) / max_depth
;
1302 if (trg_entry
->delta
&& trg_entry
->delta_size
<= max_size
)
1303 max_size
= trg_entry
->delta_size
-1;
1304 src_size
= src_entry
->size
;
1305 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1306 if (sizediff
>= max_size
)
1309 /* Load data if not already done */
1311 trg
->data
= read_sha1_file(trg_entry
->sha1
, &type
, &sz
);
1313 die("object %s inconsistent object length (%lu vs %lu)",
1314 sha1_to_hex(trg_entry
->sha1
), sz
, trg_size
);
1317 src
->data
= read_sha1_file(src_entry
->sha1
, &type
, &sz
);
1319 die("object %s inconsistent object length (%lu vs %lu)",
1320 sha1_to_hex(src_entry
->sha1
), sz
, src_size
);
1323 src
->index
= create_delta_index(src
->data
, src_size
);
1325 die("out of memory");
1328 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1332 trg_entry
->delta
= src_entry
;
1333 trg_entry
->delta_size
= delta_size
;
1334 trg_entry
->depth
= src_entry
->depth
+ 1;
1339 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1341 struct object_entry
*child
= me
->delta_child
;
1344 unsigned int c
= check_delta_limit(child
, n
+ 1);
1347 child
= child
->delta_sibling
;
1352 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
1354 uint32_t i
= nr_objects
, idx
= 0, processed
= 0;
1355 unsigned int array_size
= window
* sizeof(struct unpacked
);
1356 struct unpacked
*array
;
1357 unsigned last_percent
= 999;
1362 array
= xmalloc(array_size
);
1363 memset(array
, 0, array_size
);
1365 fprintf(stderr
, "Deltifying %u objects.\n", nr_result
);
1368 struct object_entry
*entry
= list
[--i
];
1369 struct unpacked
*n
= array
+ idx
;
1372 if (!entry
->preferred_base
)
1376 unsigned percent
= processed
* 100 / nr_result
;
1377 if (percent
!= last_percent
|| progress_update
) {
1378 fprintf(stderr
, "%4u%% (%u/%u) done\r",
1379 percent
, processed
, nr_result
);
1380 progress_update
= 0;
1381 last_percent
= percent
;
1386 /* This happens if we decided to reuse existing
1387 * delta from a pack. "!no_reuse_delta &&" is implied.
1391 if (entry
->size
< 50)
1393 free_delta_index(n
->index
);
1400 * If the current object is at pack edge, take the depth the
1401 * objects that depend on the current object into account
1402 * otherwise they would become too deep.
1405 if (entry
->delta_child
) {
1406 max_depth
-= check_delta_limit(entry
, 0);
1413 uint32_t other_idx
= idx
+ j
;
1415 if (other_idx
>= window
)
1416 other_idx
-= window
;
1417 m
= array
+ other_idx
;
1420 if (try_delta(n
, m
, max_depth
) < 0)
1424 /* if we made n a delta, and if n is already at max
1425 * depth, leaving it in the window is pointless. we
1426 * should evict it first.
1428 if (entry
->delta
&& depth
<= entry
->depth
)
1438 fputc('\n', stderr
);
1440 for (i
= 0; i
< window
; ++i
) {
1441 free_delta_index(array
[i
].index
);
1442 free(array
[i
].data
);
1447 static void prepare_pack(int window
, int depth
)
1449 struct object_entry
**delta_list
;
1452 get_object_details();
1454 if (!window
|| !depth
)
1457 delta_list
= xmalloc(nr_objects
* sizeof(*delta_list
));
1458 for (i
= 0; i
< nr_objects
; i
++)
1459 delta_list
[i
] = objects
+ i
;
1460 qsort(delta_list
, nr_objects
, sizeof(*delta_list
), type_size_sort
);
1461 find_deltas(delta_list
, window
+1, depth
);
1465 static void progress_interval(int signum
)
1467 progress_update
= 1;
1470 static void setup_progress_signal(void)
1472 struct sigaction sa
;
1475 memset(&sa
, 0, sizeof(sa
));
1476 sa
.sa_handler
= progress_interval
;
1477 sigemptyset(&sa
.sa_mask
);
1478 sa
.sa_flags
= SA_RESTART
;
1479 sigaction(SIGALRM
, &sa
, NULL
);
1481 v
.it_interval
.tv_sec
= 1;
1482 v
.it_interval
.tv_usec
= 0;
1483 v
.it_value
= v
.it_interval
;
1484 setitimer(ITIMER_REAL
, &v
, NULL
);
1487 static int git_pack_config(const char *k
, const char *v
)
1489 if(!strcmp(k
, "pack.window")) {
1490 window
= git_config_int(k
, v
);
1493 return git_default_config(k
, v
);
1496 static void read_object_list_from_stdin(void)
1498 char line
[40 + 1 + PATH_MAX
+ 2];
1499 unsigned char sha1
[20];
1503 if (!fgets(line
, sizeof(line
), stdin
)) {
1507 die("fgets returned NULL, not EOF, not error!");
1509 die("fgets: %s", strerror(errno
));
1513 if (line
[0] == '-') {
1514 if (get_sha1_hex(line
+1, sha1
))
1515 die("expected edge sha1, got garbage:\n %s",
1517 add_preferred_base(sha1
);
1520 if (get_sha1_hex(line
, sha1
))
1521 die("expected sha1, got garbage:\n %s", line
);
1523 hash
= name_hash(line
+41);
1524 add_preferred_base_object(line
+41, hash
);
1525 add_object_entry(sha1
, hash
, 0);
1529 static void show_commit(struct commit
*commit
)
1531 add_object_entry(commit
->object
.sha1
, 0, 0);
1534 static void show_object(struct object_array_entry
*p
)
1536 unsigned hash
= name_hash(p
->name
);
1537 add_preferred_base_object(p
->name
, hash
);
1538 add_object_entry(p
->item
->sha1
, hash
, 0);
1541 static void show_edge(struct commit
*commit
)
1543 add_preferred_base(commit
->object
.sha1
);
1546 static void get_object_list(int ac
, const char **av
)
1548 struct rev_info revs
;
1552 init_revisions(&revs
, NULL
);
1553 save_commit_buffer
= 0;
1554 track_object_refs
= 0;
1555 setup_revisions(ac
, av
, &revs
, NULL
);
1557 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
1558 int len
= strlen(line
);
1559 if (line
[len
- 1] == '\n')
1564 if (!strcmp(line
, "--not")) {
1565 flags
^= UNINTERESTING
;
1568 die("not a rev '%s'", line
);
1570 if (handle_revision_arg(line
, &revs
, flags
, 1))
1571 die("bad revision '%s'", line
);
1574 prepare_revision_walk(&revs
);
1575 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
1576 traverse_commit_list(&revs
, show_commit
, show_object
);
1579 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
1582 int use_internal_rev_list
= 0;
1585 off_t last_obj_offset
;
1586 const char *base_name
= NULL
;
1588 int rp_ac_alloc
= 64;
1591 rp_av
= xcalloc(rp_ac_alloc
, sizeof(*rp_av
));
1593 rp_av
[0] = "pack-objects";
1594 rp_av
[1] = "--objects"; /* --thin will make it --objects-edge */
1597 git_config(git_pack_config
);
1599 progress
= isatty(2);
1600 for (i
= 1; i
< argc
; i
++) {
1601 const char *arg
= argv
[i
];
1606 if (!strcmp("--non-empty", arg
)) {
1610 if (!strcmp("--local", arg
)) {
1614 if (!strcmp("--incremental", arg
)) {
1618 if (!prefixcmp(arg
, "--window=")) {
1620 window
= strtoul(arg
+9, &end
, 0);
1621 if (!arg
[9] || *end
)
1625 if (!prefixcmp(arg
, "--depth=")) {
1627 depth
= strtoul(arg
+8, &end
, 0);
1628 if (!arg
[8] || *end
)
1632 if (!strcmp("--progress", arg
)) {
1636 if (!strcmp("--all-progress", arg
)) {
1640 if (!strcmp("-q", arg
)) {
1644 if (!strcmp("--no-reuse-delta", arg
)) {
1648 if (!strcmp("--delta-base-offset", arg
)) {
1649 allow_ofs_delta
= 1;
1652 if (!strcmp("--stdout", arg
)) {
1656 if (!strcmp("--revs", arg
)) {
1657 use_internal_rev_list
= 1;
1660 if (!strcmp("--unpacked", arg
) ||
1661 !prefixcmp(arg
, "--unpacked=") ||
1662 !strcmp("--reflog", arg
) ||
1663 !strcmp("--all", arg
)) {
1664 use_internal_rev_list
= 1;
1665 if (rp_ac
>= rp_ac_alloc
- 1) {
1666 rp_ac_alloc
= alloc_nr(rp_ac_alloc
);
1667 rp_av
= xrealloc(rp_av
,
1668 rp_ac_alloc
* sizeof(*rp_av
));
1670 rp_av
[rp_ac
++] = arg
;
1673 if (!strcmp("--thin", arg
)) {
1674 use_internal_rev_list
= 1;
1676 rp_av
[1] = "--objects-edge";
1679 if (!prefixcmp(arg
, "--index-version=")) {
1681 index_default_version
= strtoul(arg
+ 16, &c
, 10);
1682 if (index_default_version
> 2)
1685 index_off32_limit
= strtoul(c
+1, &c
, 0);
1686 if (*c
|| index_off32_limit
& 0x80000000)
1693 /* Traditionally "pack-objects [options] base extra" failed;
1694 * we would however want to take refs parameter that would
1695 * have been given to upstream rev-list ourselves, which means
1696 * we somehow want to say what the base name is. So the
1699 * pack-objects [options] base <refs...>
1701 * in other words, we would treat the first non-option as the
1702 * base_name and send everything else to the internal revision
1706 if (!pack_to_stdout
)
1707 base_name
= argv
[i
++];
1709 if (pack_to_stdout
!= !base_name
)
1712 if (!pack_to_stdout
&& thin
)
1713 die("--thin cannot be used to build an indexable pack.");
1715 prepare_packed_git();
1718 fprintf(stderr
, "Generating pack...\n");
1719 setup_progress_signal();
1722 if (!use_internal_rev_list
)
1723 read_object_list_from_stdin();
1725 rp_av
[rp_ac
] = NULL
;
1726 get_object_list(rp_ac
, rp_av
);
1730 fprintf(stderr
, "Done counting %u objects.\n", nr_objects
);
1731 if (non_empty
&& !nr_result
)
1733 if (progress
&& (nr_objects
!= nr_result
))
1734 fprintf(stderr
, "Result has %u objects.\n", nr_result
);
1736 prepare_pack(window
, depth
);
1737 if (progress
== 1 && pack_to_stdout
) {
1738 /* the other end usually displays progress itself */
1739 struct itimerval v
= {{0,},};
1740 setitimer(ITIMER_REAL
, &v
, NULL
);
1741 signal(SIGALRM
, SIG_IGN
);
1742 progress_update
= 0;
1744 last_obj_offset
= write_pack_file();
1745 if (!pack_to_stdout
) {
1746 unsigned char object_list_sha1
[20];
1747 write_index_file(last_obj_offset
, object_list_sha1
);
1748 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.pack",
1749 base_name
, sha1_to_hex(object_list_sha1
));
1750 if (rename(pack_tmp_name
, tmpname
))
1751 die("unable to rename temporary pack file: %s",
1753 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.idx",
1754 base_name
, sha1_to_hex(object_list_sha1
));
1755 if (rename(idx_tmp_name
, tmpname
))
1756 die("unable to rename temporary index file: %s",
1758 puts(sha1_to_hex(object_list_sha1
));
1761 fprintf(stderr
, "Total %u (delta %u), reused %u (delta %u)\n",
1762 written
, written_delta
, reused
, reused_delta
);