10 #include "csum-file.h"
11 #include "tree-walk.h"
14 #include "list-objects.h"
18 static const char pack_usage
[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] [--revs [--unpacked | --all]*] [--stdout | base-name] <ref-list | <object-list]";
21 unsigned char sha1
[20];
22 unsigned long size
; /* uncompressed size */
23 unsigned long offset
; /* offset into the final pack file;
24 * nonzero if already written.
26 unsigned int depth
; /* delta depth */
27 unsigned int delta_limit
; /* base adjustment for in-pack delta */
28 unsigned int hash
; /* name hint hash */
29 enum object_type type
;
30 enum object_type in_pack_type
; /* could be delta */
31 unsigned long delta_size
; /* delta data size (uncompressed) */
32 struct object_entry
*delta
; /* delta base object */
33 struct packed_git
*in_pack
; /* already in pack */
34 unsigned int in_pack_offset
;
35 struct object_entry
*delta_child
; /* deltified objects who bases me */
36 struct object_entry
*delta_sibling
; /* other deltified objects who
37 * uses the same base as me
39 int preferred_base
; /* we do not pack this, but is encouraged to
40 * be used as the base objectto delta huge
46 * Objects we are going to pack are collected in objects array (dynamically
47 * expanded). nr_objects & nr_alloc controls this array. They are stored
48 * in the order we see -- typically rev-list --objects order that gives us
49 * nice "minimum seek" order.
51 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
52 * elements in the objects array. The former is used to build the pack
53 * index (lists object names in the ascending order to help offset lookup),
54 * and the latter is used to group similar things together by try_delta()
58 static unsigned char object_list_sha1
[20];
60 static int no_reuse_delta
;
62 static int incremental
;
63 static struct object_entry
**sorted_by_sha
, **sorted_by_type
;
64 static struct object_entry
*objects
;
65 static int nr_objects
, nr_alloc
, nr_result
;
66 static const char *base_name
;
67 static unsigned char pack_file_sha1
[20];
68 static int progress
= 1;
69 static volatile sig_atomic_t progress_update
;
70 static int window
= 10;
71 static int pack_to_stdout
;
72 static int num_preferred_base
;
75 * The object names in objects array are hashed with this hashtable,
76 * to help looking up the entry by object name. Binary search from
77 * sorted_by_sha is also possible but this was easier to code and faster.
78 * This hashtable is built after all the objects are seen.
80 static int *object_ix
;
81 static int object_ix_hashsz
;
84 * Pack index for existing packs give us easy access to the offsets into
85 * corresponding pack file where each object's data starts, but the entries
86 * do not store the size of the compressed representation (uncompressed
87 * size is easily available by examining the pack entry header). We build
88 * a hashtable of existing packs (pack_revindex), and keep reverse index
89 * here -- pack index file is sorted by object name mapping to offset; this
90 * pack_revindex[].revindex array is an ordered list of offsets, so if you
91 * know the offset of an object, next offset is where its packed
92 * representation ends.
94 struct pack_revindex
{
96 unsigned long *revindex
;
97 } *pack_revindex
= NULL
;
98 static int pack_revindex_hashsz
;
104 static int written_delta
;
106 static int reused_delta
;
108 static int pack_revindex_ix(struct packed_git
*p
)
110 unsigned long ui
= (unsigned long)p
;
113 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
114 i
= (int)(ui
% pack_revindex_hashsz
);
115 while (pack_revindex
[i
].p
) {
116 if (pack_revindex
[i
].p
== p
)
118 if (++i
== pack_revindex_hashsz
)
124 static void prepare_pack_ix(void)
127 struct packed_git
*p
;
128 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
132 pack_revindex_hashsz
= num
* 11;
133 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
134 for (p
= packed_git
; p
; p
= p
->next
) {
135 num
= pack_revindex_ix(p
);
137 pack_revindex
[num
].p
= p
;
139 /* revindex elements are lazily initialized */
142 static int cmp_offset(const void *a_
, const void *b_
)
144 unsigned long a
= *(unsigned long *) a_
;
145 unsigned long b
= *(unsigned long *) b_
;
155 * Ordered list of offsets of objects in the pack.
157 static void prepare_pack_revindex(struct pack_revindex
*rix
)
159 struct packed_git
*p
= rix
->p
;
160 int num_ent
= num_packed_objects(p
);
162 void *index
= p
->index_base
+ 256;
164 rix
->revindex
= xmalloc(sizeof(unsigned long) * (num_ent
+ 1));
165 for (i
= 0; i
< num_ent
; i
++) {
166 unsigned int hl
= *((unsigned int *)((char *) index
+ 24*i
));
167 rix
->revindex
[i
] = ntohl(hl
);
169 /* This knows the pack format -- the 20-byte trailer
170 * follows immediately after the last object data.
172 rix
->revindex
[num_ent
] = p
->pack_size
- 20;
173 qsort(rix
->revindex
, num_ent
, sizeof(unsigned long), cmp_offset
);
176 static unsigned long find_packed_object_size(struct packed_git
*p
,
181 struct pack_revindex
*rix
;
182 unsigned long *revindex
;
183 num
= pack_revindex_ix(p
);
185 die("internal error: pack revindex uninitialized");
186 rix
= &pack_revindex
[num
];
188 prepare_pack_revindex(rix
);
189 revindex
= rix
->revindex
;
191 hi
= num_packed_objects(p
) + 1;
193 int mi
= (lo
+ hi
) / 2;
194 if (revindex
[mi
] == ofs
) {
195 return revindex
[mi
+1] - ofs
;
197 else if (ofs
< revindex
[mi
])
202 die("internal error: pack revindex corrupt");
205 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
207 unsigned long othersize
, delta_size
;
209 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, type
, &othersize
);
213 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
214 delta_buf
= diff_delta(otherbuf
, othersize
,
215 buf
, size
, &delta_size
, 0);
216 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
217 die("delta size changed");
224 * The per-object header is a pretty dense thing, which is
225 * - first byte: low four bits are "size", then three bits of "type",
226 * and the high bit is "size continues".
227 * - each byte afterwards: low seven bits are size continuation,
228 * with the high bit being "size continues"
230 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
235 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
236 die("bad type %d", type
);
238 c
= (type
<< 4) | (size
& 15);
250 static int check_inflate(unsigned char *data
, unsigned long len
, unsigned long expect
)
253 unsigned char fakebuf
[4096];
256 memset(&stream
, 0, sizeof(stream
));
257 stream
.next_in
= data
;
258 stream
.avail_in
= len
;
259 stream
.next_out
= fakebuf
;
260 stream
.avail_out
= sizeof(fakebuf
);
261 inflateInit(&stream
);
264 st
= inflate(&stream
, Z_FINISH
);
265 if (st
== Z_STREAM_END
|| st
== Z_OK
) {
266 st
= (stream
.total_out
== expect
&&
267 stream
.total_in
== len
) ? 0 : -1;
270 if (st
!= Z_BUF_ERROR
) {
274 stream
.next_out
= fakebuf
;
275 stream
.avail_out
= sizeof(fakebuf
);
282 * we are going to reuse the existing pack entry data. make
283 * sure it is not corrupt.
285 static int revalidate_pack_entry(struct object_entry
*entry
, unsigned char *data
, unsigned long len
)
287 enum object_type type
;
288 unsigned long size
, used
;
293 /* the caller has already called use_packed_git() for us,
294 * so it is safe to access the pack data from mmapped location.
295 * make sure the entry inflates correctly.
297 used
= unpack_object_header_gently(data
, len
, &type
, &size
);
300 if (type
== OBJ_DELTA
)
301 used
+= 20; /* skip base object name */
304 return check_inflate(data
, len
, entry
->size
);
307 static int revalidate_loose_object(struct object_entry
*entry
,
309 unsigned long mapsize
)
311 /* we already know this is a loose object with new type header. */
312 enum object_type type
;
313 unsigned long size
, used
;
318 used
= unpack_object_header_gently(map
, mapsize
, &type
, &size
);
323 return check_inflate(map
, mapsize
, size
);
326 static unsigned long write_object(struct sha1file
*f
,
327 struct object_entry
*entry
)
332 unsigned char header
[10];
333 unsigned hdrlen
, datalen
;
334 enum object_type obj_type
;
337 if (entry
->preferred_base
)
340 obj_type
= entry
->type
;
341 if (! entry
->in_pack
)
342 to_reuse
= 0; /* can't reuse what we don't have */
343 else if (obj_type
== OBJ_DELTA
)
344 to_reuse
= 1; /* check_object() decided it for us */
345 else if (obj_type
!= entry
->in_pack_type
)
346 to_reuse
= 0; /* pack has delta which is unusable */
347 else if (entry
->delta
)
348 to_reuse
= 0; /* we want to pack afresh */
350 to_reuse
= 1; /* we have it in-pack undeltified,
351 * and we do not need to deltify it.
354 if (!entry
->in_pack
&& !entry
->delta
) {
356 unsigned long mapsize
;
357 map
= map_sha1_file(entry
->sha1
, &mapsize
);
358 if (map
&& !legacy_loose_object(map
)) {
359 /* We can copy straight into the pack file */
360 if (revalidate_loose_object(entry
, map
, mapsize
))
361 die("corrupt loose object %s",
362 sha1_to_hex(entry
->sha1
));
363 sha1write(f
, map
, mapsize
);
364 munmap(map
, mapsize
);
370 munmap(map
, mapsize
);
374 buf
= read_sha1_file(entry
->sha1
, type
, &size
);
376 die("unable to read %s", sha1_to_hex(entry
->sha1
));
377 if (size
!= entry
->size
)
378 die("object %s size inconsistency (%lu vs %lu)",
379 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
381 buf
= delta_against(buf
, size
, entry
);
382 size
= entry
->delta_size
;
383 obj_type
= OBJ_DELTA
;
386 * The object header is a byte of 'type' followed by zero or
387 * more bytes of length. For deltas, the 20 bytes of delta
390 hdrlen
= encode_header(obj_type
, size
, header
);
391 sha1write(f
, header
, hdrlen
);
394 sha1write(f
, entry
->delta
, 20);
397 datalen
= sha1write_compressed(f
, buf
, size
);
401 struct packed_git
*p
= entry
->in_pack
;
404 datalen
= find_packed_object_size(p
, entry
->in_pack_offset
);
405 buf
= (char *) p
->pack_base
+ entry
->in_pack_offset
;
407 if (revalidate_pack_entry(entry
, buf
, datalen
))
408 die("corrupt delta in pack %s", sha1_to_hex(entry
->sha1
));
409 sha1write(f
, buf
, datalen
);
411 hdrlen
= 0; /* not really */
412 if (obj_type
== OBJ_DELTA
)
416 if (obj_type
== OBJ_DELTA
)
419 return hdrlen
+ datalen
;
422 static unsigned long write_one(struct sha1file
*f
,
423 struct object_entry
*e
,
424 unsigned long offset
)
427 /* offset starts from header size and cannot be zero
428 * if it is written already.
432 offset
+= write_object(f
, e
);
433 /* if we are deltified, write out its base object. */
435 offset
= write_one(f
, e
->delta
, offset
);
439 static void write_pack_file(void)
443 unsigned long offset
;
444 struct pack_header hdr
;
445 unsigned last_percent
= 999;
449 f
= sha1fd(1, "<stdout>");
451 f
= sha1create("%s-%s.%s", base_name
,
452 sha1_to_hex(object_list_sha1
), "pack");
453 do_progress
= progress
;
456 fprintf(stderr
, "Writing %d objects.\n", nr_result
);
458 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
459 hdr
.hdr_version
= htonl(PACK_VERSION
);
460 hdr
.hdr_entries
= htonl(nr_result
);
461 sha1write(f
, &hdr
, sizeof(hdr
));
462 offset
= sizeof(hdr
);
465 for (i
= 0; i
< nr_objects
; i
++) {
466 offset
= write_one(f
, objects
+ i
, offset
);
468 unsigned percent
= written
* 100 / nr_result
;
469 if (progress_update
|| percent
!= last_percent
) {
470 fprintf(stderr
, "%4u%% (%u/%u) done\r",
471 percent
, written
, nr_result
);
473 last_percent
= percent
;
480 sha1close(f
, pack_file_sha1
, 1);
483 static void write_index_file(void)
486 struct sha1file
*f
= sha1create("%s-%s.%s", base_name
,
487 sha1_to_hex(object_list_sha1
), "idx");
488 struct object_entry
**list
= sorted_by_sha
;
489 struct object_entry
**last
= list
+ nr_result
;
490 unsigned int array
[256];
493 * Write the first-level table (the list is sorted,
494 * but we use a 256-entry lookup to be able to avoid
495 * having to do eight extra binary search iterations).
497 for (i
= 0; i
< 256; i
++) {
498 struct object_entry
**next
= list
;
499 while (next
< last
) {
500 struct object_entry
*entry
= *next
;
501 if (entry
->sha1
[0] != i
)
505 array
[i
] = htonl(next
- sorted_by_sha
);
508 sha1write(f
, array
, 256 * sizeof(int));
511 * Write the actual SHA1 entries..
513 list
= sorted_by_sha
;
514 for (i
= 0; i
< nr_result
; i
++) {
515 struct object_entry
*entry
= *list
++;
516 unsigned int offset
= htonl(entry
->offset
);
517 sha1write(f
, &offset
, 4);
518 sha1write(f
, entry
->sha1
, 20);
520 sha1write(f
, pack_file_sha1
, 20);
521 sha1close(f
, NULL
, 1);
524 static int locate_object_entry_hash(const unsigned char *sha1
)
528 memcpy(&ui
, sha1
, sizeof(unsigned int));
529 i
= ui
% object_ix_hashsz
;
530 while (0 < object_ix
[i
]) {
531 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].sha1
))
533 if (++i
== object_ix_hashsz
)
539 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
543 if (!object_ix_hashsz
)
546 i
= locate_object_entry_hash(sha1
);
548 return &objects
[object_ix
[i
]-1];
552 static void rehash_objects(void)
555 struct object_entry
*oe
;
557 object_ix_hashsz
= nr_objects
* 3;
558 if (object_ix_hashsz
< 1024)
559 object_ix_hashsz
= 1024;
560 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
561 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
562 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
563 int ix
= locate_object_entry_hash(oe
->sha1
);
567 object_ix
[ix
] = i
+ 1;
571 static unsigned name_hash(const char *name
)
577 * This effectively just creates a sortable number from the
578 * last sixteen non-whitespace characters. Last characters
579 * count "most", so things that end in ".c" sort together.
581 while ((c
= *name
++) != 0) {
584 hash
= (hash
>> 2) + (c
<< 24);
589 static int add_object_entry(const unsigned char *sha1
, unsigned hash
, int exclude
)
591 unsigned int idx
= nr_objects
;
592 struct object_entry
*entry
;
593 struct packed_git
*p
;
594 unsigned int found_offset
= 0;
595 struct packed_git
*found_pack
= NULL
;
599 for (p
= packed_git
; p
; p
= p
->next
) {
601 if (find_pack_entry_one(sha1
, &e
, p
)) {
604 if (local
&& !p
->pack_local
)
607 found_offset
= e
.offset
;
613 if ((entry
= locate_object_entry(sha1
)) != NULL
)
616 if (idx
>= nr_alloc
) {
617 unsigned int needed
= (idx
+ 1024) * 3 / 2;
618 objects
= xrealloc(objects
, needed
* sizeof(*entry
));
621 entry
= objects
+ idx
;
622 nr_objects
= idx
+ 1;
623 memset(entry
, 0, sizeof(*entry
));
624 hashcpy(entry
->sha1
, sha1
);
627 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
630 ix
= locate_object_entry_hash(entry
->sha1
);
632 die("internal error in object hashing.");
633 object_ix
[-1 - ix
] = idx
+ 1;
638 if (progress_update
) {
639 fprintf(stderr
, "Counting objects...%d\r", nr_objects
);
643 entry
->preferred_base
= 1;
646 entry
->in_pack
= found_pack
;
647 entry
->in_pack_offset
= found_offset
;
653 struct pbase_tree_cache
{
654 unsigned char sha1
[20];
658 unsigned long tree_size
;
661 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
662 static int pbase_tree_cache_ix(const unsigned char *sha1
)
664 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
666 static int pbase_tree_cache_ix_incr(int ix
)
668 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
671 static struct pbase_tree
{
672 struct pbase_tree
*next
;
673 /* This is a phony "cache" entry; we are not
674 * going to evict it nor find it through _get()
675 * mechanism -- this is for the toplevel node that
676 * would almost always change with any commit.
678 struct pbase_tree_cache pcache
;
681 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
683 struct pbase_tree_cache
*ent
, *nent
;
688 int my_ix
= pbase_tree_cache_ix(sha1
);
689 int available_ix
= -1;
691 /* pbase-tree-cache acts as a limited hashtable.
692 * your object will be found at your index or within a few
693 * slots after that slot if it is cached.
695 for (neigh
= 0; neigh
< 8; neigh
++) {
696 ent
= pbase_tree_cache
[my_ix
];
697 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
701 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
702 ((0 <= available_ix
) &&
703 (!ent
&& pbase_tree_cache
[available_ix
])))
704 available_ix
= my_ix
;
707 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
710 /* Did not find one. Either we got a bogus request or
711 * we need to read and perhaps cache.
713 data
= read_sha1_file(sha1
, type
, &size
);
716 if (strcmp(type
, tree_type
)) {
721 /* We need to either cache or return a throwaway copy */
723 if (available_ix
< 0)
726 ent
= pbase_tree_cache
[available_ix
];
727 my_ix
= available_ix
;
731 nent
= xmalloc(sizeof(*nent
));
732 nent
->temporary
= (available_ix
< 0);
735 /* evict and reuse */
736 free(ent
->tree_data
);
739 hashcpy(nent
->sha1
, sha1
);
740 nent
->tree_data
= data
;
741 nent
->tree_size
= size
;
743 if (!nent
->temporary
)
744 pbase_tree_cache
[my_ix
] = nent
;
748 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
750 if (!cache
->temporary
) {
754 free(cache
->tree_data
);
758 static int name_cmp_len(const char *name
)
761 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
766 static void add_pbase_object(struct tree_desc
*tree
,
769 const char *fullname
)
771 struct name_entry entry
;
773 while (tree_entry(tree
,&entry
)) {
777 if (entry
.pathlen
!= cmplen
||
778 memcmp(entry
.path
, name
, cmplen
) ||
779 !has_sha1_file(entry
.sha1
) ||
780 sha1_object_info(entry
.sha1
, type
, &size
))
782 if (name
[cmplen
] != '/') {
783 unsigned hash
= name_hash(fullname
);
784 add_object_entry(entry
.sha1
, hash
, 1);
787 if (!strcmp(type
, tree_type
)) {
788 struct tree_desc sub
;
789 struct pbase_tree_cache
*tree
;
790 const char *down
= name
+cmplen
+1;
791 int downlen
= name_cmp_len(down
);
793 tree
= pbase_tree_get(entry
.sha1
);
796 sub
.buf
= tree
->tree_data
;
797 sub
.size
= tree
->tree_size
;
799 add_pbase_object(&sub
, down
, downlen
, fullname
);
800 pbase_tree_put(tree
);
805 static unsigned *done_pbase_paths
;
806 static int done_pbase_paths_num
;
807 static int done_pbase_paths_alloc
;
808 static int done_pbase_path_pos(unsigned hash
)
811 int hi
= done_pbase_paths_num
;
813 int mi
= (hi
+ lo
) / 2;
814 if (done_pbase_paths
[mi
] == hash
)
816 if (done_pbase_paths
[mi
] < hash
)
824 static int check_pbase_path(unsigned hash
)
826 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
830 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
831 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
832 done_pbase_paths
= xrealloc(done_pbase_paths
,
833 done_pbase_paths_alloc
*
836 done_pbase_paths_num
++;
837 if (pos
< done_pbase_paths_num
)
838 memmove(done_pbase_paths
+ pos
+ 1,
839 done_pbase_paths
+ pos
,
840 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
841 done_pbase_paths
[pos
] = hash
;
845 static void add_preferred_base_object(const char *name
, unsigned hash
)
847 struct pbase_tree
*it
;
848 int cmplen
= name_cmp_len(name
);
850 if (check_pbase_path(hash
))
853 for (it
= pbase_tree
; it
; it
= it
->next
) {
855 hash
= name_hash("");
856 add_object_entry(it
->pcache
.sha1
, hash
, 1);
859 struct tree_desc tree
;
860 tree
.buf
= it
->pcache
.tree_data
;
861 tree
.size
= it
->pcache
.tree_size
;
862 add_pbase_object(&tree
, name
, cmplen
, name
);
867 static void add_preferred_base(unsigned char *sha1
)
869 struct pbase_tree
*it
;
872 unsigned char tree_sha1
[20];
874 if (window
<= num_preferred_base
++)
877 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
881 for (it
= pbase_tree
; it
; it
= it
->next
) {
882 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
888 it
= xcalloc(1, sizeof(*it
));
889 it
->next
= pbase_tree
;
892 hashcpy(it
->pcache
.sha1
, tree_sha1
);
893 it
->pcache
.tree_data
= data
;
894 it
->pcache
.tree_size
= size
;
897 static void check_object(struct object_entry
*entry
)
901 if (entry
->in_pack
&& !entry
->preferred_base
) {
902 unsigned char base
[20];
904 struct object_entry
*base_entry
;
906 /* We want in_pack_type even if we do not reuse delta.
907 * There is no point not reusing non-delta representations.
909 check_reuse_pack_delta(entry
->in_pack
,
910 entry
->in_pack_offset
,
912 &entry
->in_pack_type
);
914 /* Check if it is delta, and the base is also an object
915 * we are going to pack. If so we will reuse the existing
918 if (!no_reuse_delta
&&
919 entry
->in_pack_type
== OBJ_DELTA
&&
920 (base_entry
= locate_object_entry(base
)) &&
921 (!base_entry
->preferred_base
)) {
923 /* Depth value does not matter - find_deltas()
924 * will never consider reused delta as the
925 * base object to deltify other objects
926 * against, in order to avoid circular deltas.
929 /* uncompressed size of the delta data */
930 entry
->size
= entry
->delta_size
= size
;
931 entry
->delta
= base_entry
;
932 entry
->type
= OBJ_DELTA
;
934 entry
->delta_sibling
= base_entry
->delta_child
;
935 base_entry
->delta_child
= entry
;
939 /* Otherwise we would do the usual */
942 if (sha1_object_info(entry
->sha1
, type
, &entry
->size
))
943 die("unable to get type of object %s",
944 sha1_to_hex(entry
->sha1
));
946 if (!strcmp(type
, commit_type
)) {
947 entry
->type
= OBJ_COMMIT
;
948 } else if (!strcmp(type
, tree_type
)) {
949 entry
->type
= OBJ_TREE
;
950 } else if (!strcmp(type
, blob_type
)) {
951 entry
->type
= OBJ_BLOB
;
952 } else if (!strcmp(type
, tag_type
)) {
953 entry
->type
= OBJ_TAG
;
955 die("unable to pack object %s of type %s",
956 sha1_to_hex(entry
->sha1
), type
);
959 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
961 struct object_entry
*child
= me
->delta_child
;
964 unsigned int c
= check_delta_limit(child
, n
+ 1);
967 child
= child
->delta_sibling
;
972 static void get_object_details(void)
975 struct object_entry
*entry
;
978 for (i
= 0, entry
= objects
; i
< nr_objects
; i
++, entry
++)
981 if (nr_objects
== nr_result
) {
983 * Depth of objects that depend on the entry -- this
984 * is subtracted from depth-max to break too deep
985 * delta chain because of delta data reusing.
986 * However, we loosen this restriction when we know we
987 * are creating a thin pack -- it will have to be
988 * expanded on the other end anyway, so do not
989 * artificially cut the delta chain and let it go as
992 for (i
= 0, entry
= objects
; i
< nr_objects
; i
++, entry
++)
993 if (!entry
->delta
&& entry
->delta_child
)
995 check_delta_limit(entry
, 1);
999 typedef int (*entry_sort_t
)(const struct object_entry
*, const struct object_entry
*);
1001 static entry_sort_t current_sort
;
1003 static int sort_comparator(const void *_a
, const void *_b
)
1005 struct object_entry
*a
= *(struct object_entry
**)_a
;
1006 struct object_entry
*b
= *(struct object_entry
**)_b
;
1007 return current_sort(a
,b
);
1010 static struct object_entry
**create_sorted_list(entry_sort_t sort
)
1012 struct object_entry
**list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
1015 for (i
= 0; i
< nr_objects
; i
++)
1016 list
[i
] = objects
+ i
;
1017 current_sort
= sort
;
1018 qsort(list
, nr_objects
, sizeof(struct object_entry
*), sort_comparator
);
1022 static int sha1_sort(const struct object_entry
*a
, const struct object_entry
*b
)
1024 return hashcmp(a
->sha1
, b
->sha1
);
1027 static struct object_entry
**create_final_object_list(void)
1029 struct object_entry
**list
;
1032 for (i
= nr_result
= 0; i
< nr_objects
; i
++)
1033 if (!objects
[i
].preferred_base
)
1035 list
= xmalloc(nr_result
* sizeof(struct object_entry
*));
1036 for (i
= j
= 0; i
< nr_objects
; i
++) {
1037 if (!objects
[i
].preferred_base
)
1038 list
[j
++] = objects
+ i
;
1040 current_sort
= sha1_sort
;
1041 qsort(list
, nr_result
, sizeof(struct object_entry
*), sort_comparator
);
1045 static int type_size_sort(const struct object_entry
*a
, const struct object_entry
*b
)
1047 if (a
->type
< b
->type
)
1049 if (a
->type
> b
->type
)
1051 if (a
->hash
< b
->hash
)
1053 if (a
->hash
> b
->hash
)
1055 if (a
->preferred_base
< b
->preferred_base
)
1057 if (a
->preferred_base
> b
->preferred_base
)
1059 if (a
->size
< b
->size
)
1061 if (a
->size
> b
->size
)
1063 return a
< b
? -1 : (a
> b
);
1067 struct object_entry
*entry
;
1069 struct delta_index
*index
;
1073 * We search for deltas _backwards_ in a list sorted by type and
1074 * by size, so that we see progressively smaller and smaller files.
1075 * That's because we prefer deltas to be from the bigger file
1076 * to the smaller - deletes are potentially cheaper, but perhaps
1077 * more importantly, the bigger file is likely the more recent
1080 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1083 struct object_entry
*trg_entry
= trg
->entry
;
1084 struct object_entry
*src_entry
= src
->entry
;
1085 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1089 /* Don't bother doing diffs between different types */
1090 if (trg_entry
->type
!= src_entry
->type
)
1093 /* We do not compute delta to *create* objects we are not
1096 if (trg_entry
->preferred_base
)
1100 * We do not bother to try a delta that we discarded
1101 * on an earlier try, but only when reusing delta data.
1103 if (!no_reuse_delta
&& trg_entry
->in_pack
&&
1104 trg_entry
->in_pack
== src_entry
->in_pack
)
1108 * If the current object is at pack edge, take the depth the
1109 * objects that depend on the current object into account --
1110 * otherwise they would become too deep.
1112 if (trg_entry
->delta_child
) {
1113 if (max_depth
<= trg_entry
->delta_limit
)
1115 max_depth
-= trg_entry
->delta_limit
;
1117 if (src_entry
->depth
>= max_depth
)
1120 /* Now some size filtering heuristics. */
1121 trg_size
= trg_entry
->size
;
1122 max_size
= trg_size
/2 - 20;
1123 max_size
= max_size
* (max_depth
- src_entry
->depth
) / max_depth
;
1126 if (trg_entry
->delta
&& trg_entry
->delta_size
<= max_size
)
1127 max_size
= trg_entry
->delta_size
-1;
1128 src_size
= src_entry
->size
;
1129 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1130 if (sizediff
>= max_size
)
1133 /* Load data if not already done */
1135 trg
->data
= read_sha1_file(trg_entry
->sha1
, type
, &sz
);
1137 die("object %s inconsistent object length (%lu vs %lu)",
1138 sha1_to_hex(trg_entry
->sha1
), sz
, trg_size
);
1141 src
->data
= read_sha1_file(src_entry
->sha1
, type
, &sz
);
1143 die("object %s inconsistent object length (%lu vs %lu)",
1144 sha1_to_hex(src_entry
->sha1
), sz
, src_size
);
1147 src
->index
= create_delta_index(src
->data
, src_size
);
1149 die("out of memory");
1152 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1156 trg_entry
->delta
= src_entry
;
1157 trg_entry
->delta_size
= delta_size
;
1158 trg_entry
->depth
= src_entry
->depth
+ 1;
1163 static void progress_interval(int signum
)
1165 progress_update
= 1;
1168 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
1171 unsigned int array_size
= window
* sizeof(struct unpacked
);
1172 struct unpacked
*array
= xmalloc(array_size
);
1173 unsigned processed
= 0;
1174 unsigned last_percent
= 999;
1176 memset(array
, 0, array_size
);
1180 fprintf(stderr
, "Deltifying %d objects.\n", nr_result
);
1183 struct object_entry
*entry
= list
[i
];
1184 struct unpacked
*n
= array
+ idx
;
1187 if (!entry
->preferred_base
)
1191 unsigned percent
= processed
* 100 / nr_result
;
1192 if (percent
!= last_percent
|| progress_update
) {
1193 fprintf(stderr
, "%4u%% (%u/%u) done\r",
1194 percent
, processed
, nr_result
);
1195 progress_update
= 0;
1196 last_percent
= percent
;
1201 /* This happens if we decided to reuse existing
1202 * delta from a pack. "!no_reuse_delta &&" is implied.
1206 if (entry
->size
< 50)
1208 free_delta_index(n
->index
);
1216 unsigned int other_idx
= idx
+ j
;
1218 if (other_idx
>= window
)
1219 other_idx
-= window
;
1220 m
= array
+ other_idx
;
1223 if (try_delta(n
, m
, depth
) < 0)
1226 /* if we made n a delta, and if n is already at max
1227 * depth, leaving it in the window is pointless. we
1228 * should evict it first.
1230 if (entry
->delta
&& depth
<= entry
->depth
)
1239 fputc('\n', stderr
);
1241 for (i
= 0; i
< window
; ++i
) {
1242 free_delta_index(array
[i
].index
);
1243 free(array
[i
].data
);
1248 static void prepare_pack(int window
, int depth
)
1250 get_object_details();
1251 sorted_by_type
= create_sorted_list(type_size_sort
);
1252 if (window
&& depth
)
1253 find_deltas(sorted_by_type
, window
+1, depth
);
1256 static int reuse_cached_pack(unsigned char *sha1
)
1258 static const char cache
[] = "pack-cache/pack-%s.%s";
1259 char *cached_pack
, *cached_idx
;
1260 int ifd
, ofd
, ifd_ix
= -1;
1262 cached_pack
= git_path(cache
, sha1_to_hex(sha1
), "pack");
1263 ifd
= open(cached_pack
, O_RDONLY
);
1267 if (!pack_to_stdout
) {
1268 cached_idx
= git_path(cache
, sha1_to_hex(sha1
), "idx");
1269 ifd_ix
= open(cached_idx
, O_RDONLY
);
1277 fprintf(stderr
, "Reusing %d objects pack %s\n", nr_objects
,
1280 if (pack_to_stdout
) {
1281 if (copy_fd(ifd
, 1))
1286 char name
[PATH_MAX
];
1287 snprintf(name
, sizeof(name
),
1288 "%s-%s.%s", base_name
, sha1_to_hex(sha1
), "pack");
1289 ofd
= open(name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
1291 die("unable to open %s (%s)", name
, strerror(errno
));
1292 if (copy_fd(ifd
, ofd
))
1296 snprintf(name
, sizeof(name
),
1297 "%s-%s.%s", base_name
, sha1_to_hex(sha1
), "idx");
1298 ofd
= open(name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
1300 die("unable to open %s (%s)", name
, strerror(errno
));
1301 if (copy_fd(ifd_ix
, ofd
))
1304 puts(sha1_to_hex(sha1
));
1310 static void setup_progress_signal(void)
1312 struct sigaction sa
;
1315 memset(&sa
, 0, sizeof(sa
));
1316 sa
.sa_handler
= progress_interval
;
1317 sigemptyset(&sa
.sa_mask
);
1318 sa
.sa_flags
= SA_RESTART
;
1319 sigaction(SIGALRM
, &sa
, NULL
);
1321 v
.it_interval
.tv_sec
= 1;
1322 v
.it_interval
.tv_usec
= 0;
1323 v
.it_value
= v
.it_interval
;
1324 setitimer(ITIMER_REAL
, &v
, NULL
);
1327 static int git_pack_config(const char *k
, const char *v
)
1329 if(!strcmp(k
, "pack.window")) {
1330 window
= git_config_int(k
, v
);
1333 return git_default_config(k
, v
);
1336 static void read_object_list_from_stdin(void)
1338 char line
[40 + 1 + PATH_MAX
+ 2];
1339 unsigned char sha1
[20];
1343 if (!fgets(line
, sizeof(line
), stdin
)) {
1347 die("fgets returned NULL, not EOF, not error!");
1349 die("fgets: %s", strerror(errno
));
1353 if (line
[0] == '-') {
1354 if (get_sha1_hex(line
+1, sha1
))
1355 die("expected edge sha1, got garbage:\n %s",
1357 add_preferred_base(sha1
);
1360 if (get_sha1_hex(line
, sha1
))
1361 die("expected sha1, got garbage:\n %s", line
);
1363 hash
= name_hash(line
+41);
1364 add_preferred_base_object(line
+41, hash
);
1365 add_object_entry(sha1
, hash
, 0);
1369 static void show_commit(struct commit
*commit
)
1371 unsigned hash
= name_hash("");
1372 add_preferred_base_object("", hash
);
1373 add_object_entry(commit
->object
.sha1
, hash
, 0);
1376 static void show_object(struct object_array_entry
*p
)
1378 unsigned hash
= name_hash(p
->name
);
1379 add_preferred_base_object(p
->name
, hash
);
1380 add_object_entry(p
->item
->sha1
, hash
, 0);
1383 static void show_edge(struct commit
*commit
)
1385 add_preferred_base(commit
->object
.sha1
);
1388 static void get_object_list(int ac
, const char **av
)
1390 struct rev_info revs
;
1394 init_revisions(&revs
, NULL
);
1395 save_commit_buffer
= 0;
1396 track_object_refs
= 0;
1397 setup_revisions(ac
, av
, &revs
, NULL
);
1399 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
1400 int len
= strlen(line
);
1401 if (line
[len
- 1] == '\n')
1406 if (!strcmp(line
, "--not")) {
1407 flags
^= UNINTERESTING
;
1410 die("not a rev '%s'", line
);
1412 if (handle_revision_arg(line
, &revs
, flags
, 1))
1413 die("bad revision '%s'", line
);
1416 prepare_revision_walk(&revs
);
1417 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
1418 traverse_commit_list(&revs
, show_commit
, show_object
);
1421 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
1425 struct object_entry
**list
;
1426 int use_internal_rev_list
= 0;
1429 const char *rp_av
[64];
1432 rp_av
[0] = "pack-objects";
1433 rp_av
[1] = "--objects"; /* --thin will make it --objects-edge */
1436 git_config(git_pack_config
);
1438 progress
= isatty(2);
1439 for (i
= 1; i
< argc
; i
++) {
1440 const char *arg
= argv
[i
];
1445 if (!strcmp("--non-empty", arg
)) {
1449 if (!strcmp("--local", arg
)) {
1453 if (!strcmp("--progress", arg
)) {
1457 if (!strcmp("--incremental", arg
)) {
1461 if (!strncmp("--window=", arg
, 9)) {
1463 window
= strtoul(arg
+9, &end
, 0);
1464 if (!arg
[9] || *end
)
1468 if (!strncmp("--depth=", arg
, 8)) {
1470 depth
= strtoul(arg
+8, &end
, 0);
1471 if (!arg
[8] || *end
)
1475 if (!strcmp("--progress", arg
)) {
1479 if (!strcmp("-q", arg
)) {
1483 if (!strcmp("--no-reuse-delta", arg
)) {
1487 if (!strcmp("--stdout", arg
)) {
1491 if (!strcmp("--revs", arg
)) {
1492 use_internal_rev_list
= 1;
1495 if (!strcmp("--unpacked", arg
) ||
1496 !strncmp("--unpacked=", arg
, 11) ||
1497 !strcmp("--all", arg
)) {
1498 use_internal_rev_list
= 1;
1499 if (ARRAY_SIZE(rp_av
) - 1 <= rp_ac
)
1500 die("too many internal rev-list options");
1501 rp_av
[rp_ac
++] = arg
;
1504 if (!strcmp("--thin", arg
)) {
1505 use_internal_rev_list
= 1;
1507 rp_av
[1] = "--objects-edge";
1513 /* Traditionally "pack-objects [options] base extra" failed;
1514 * we would however want to take refs parameter that would
1515 * have been given to upstream rev-list ourselves, which means
1516 * we somehow want to say what the base name is. So the
1519 * pack-objects [options] base <refs...>
1521 * in other words, we would treat the first non-option as the
1522 * base_name and send everything else to the internal revision
1526 if (!pack_to_stdout
)
1527 base_name
= argv
[i
++];
1529 if (pack_to_stdout
!= !base_name
)
1532 if (!pack_to_stdout
&& thin
)
1533 die("--thin cannot be used to build an indexable pack.");
1535 prepare_packed_git();
1538 fprintf(stderr
, "Generating pack...\n");
1539 setup_progress_signal();
1542 if (!use_internal_rev_list
)
1543 read_object_list_from_stdin();
1545 rp_av
[rp_ac
] = NULL
;
1546 get_object_list(rp_ac
, rp_av
);
1550 fprintf(stderr
, "Done counting %d objects.\n", nr_objects
);
1551 sorted_by_sha
= create_final_object_list();
1552 if (non_empty
&& !nr_result
)
1556 list
= sorted_by_sha
;
1557 for (i
= 0; i
< nr_result
; i
++) {
1558 struct object_entry
*entry
= *list
++;
1559 SHA1_Update(&ctx
, entry
->sha1
, 20);
1561 SHA1_Final(object_list_sha1
, &ctx
);
1562 if (progress
&& (nr_objects
!= nr_result
))
1563 fprintf(stderr
, "Result has %d objects.\n", nr_result
);
1565 if (reuse_cached_pack(object_list_sha1
))
1569 prepare_pack(window
, depth
);
1570 if (progress
&& pack_to_stdout
) {
1571 /* the other end usually displays progress itself */
1572 struct itimerval v
= {{0,},};
1573 setitimer(ITIMER_REAL
, &v
, NULL
);
1574 signal(SIGALRM
, SIG_IGN
);
1575 progress_update
= 0;
1578 if (!pack_to_stdout
) {
1580 puts(sha1_to_hex(object_list_sha1
));
1584 fprintf(stderr
, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
1585 nr_result
, written
, written_delta
, reused
, reused_delta
);