10 #include "tree-walk.h"
14 static const char pack_usage
[] = "git-pack-objects [-q] [--no-reuse-delta] [--non-empty] [--local] [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list";
17 unsigned char sha1
[20];
18 unsigned long size
; /* uncompressed size */
19 unsigned long offset
; /* offset into the final pack file;
20 * nonzero if already written.
22 unsigned int depth
; /* delta depth */
23 unsigned int delta_limit
; /* base adjustment for in-pack delta */
24 unsigned int hash
; /* name hint hash */
25 enum object_type type
;
26 enum object_type in_pack_type
; /* could be delta */
27 unsigned long delta_size
; /* delta data size (uncompressed) */
28 struct object_entry
*delta
; /* delta base object */
29 struct packed_git
*in_pack
; /* already in pack */
30 unsigned int in_pack_offset
;
31 struct object_entry
*delta_child
; /* delitified objects who bases me */
32 struct object_entry
*delta_sibling
; /* other deltified objects who
33 * uses the same base as me
35 int preferred_base
; /* we do not pack this, but is encouraged to
36 * be used as the base objectto delta huge
42 * Objects we are going to pack are colected in objects array (dynamically
43 * expanded). nr_objects & nr_alloc controls this array. They are stored
44 * in the order we see -- typically rev-list --objects order that gives us
45 * nice "minimum seek" order.
47 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
48 * elements in the objects array. The former is used to build the pack
49 * index (lists object names in the ascending order to help offset lookup),
50 * and the latter is used to group similar things together by try_delta()
54 static unsigned char object_list_sha1
[20];
55 static int non_empty
= 0;
56 static int no_reuse_delta
= 0;
58 static int incremental
= 0;
59 static struct object_entry
**sorted_by_sha
, **sorted_by_type
;
60 static struct object_entry
*objects
= NULL
;
61 static int nr_objects
= 0, nr_alloc
= 0, nr_result
= 0;
62 static const char *base_name
;
63 static unsigned char pack_file_sha1
[20];
64 static int progress
= 1;
65 static volatile sig_atomic_t progress_update
= 0;
68 * The object names in objects array are hashed with this hashtable,
69 * to help looking up the entry by object name. Binary search from
70 * sorted_by_sha is also possible but this was easier to code and faster.
71 * This hashtable is built after all the objects are seen.
73 static int *object_ix
= NULL
;
74 static int object_ix_hashsz
= 0;
77 * Pack index for existing packs give us easy access to the offsets into
78 * corresponding pack file where each object's data starts, but the entries
79 * do not store the size of the compressed representation (uncompressed
80 * size is easily available by examining the pack entry header). We build
81 * a hashtable of existing packs (pack_revindex), and keep reverse index
82 * here -- pack index file is sorted by object name mapping to offset; this
83 * pack_revindex[].revindex array is an ordered list of offsets, so if you
84 * know the offset of an object, next offset is where its packed
85 * representation ends.
87 struct pack_revindex
{
89 unsigned long *revindex
;
90 } *pack_revindex
= NULL
;
91 static int pack_revindex_hashsz
= 0;
96 static int written
= 0;
97 static int written_delta
= 0;
98 static int reused
= 0;
99 static int reused_delta
= 0;
101 static int pack_revindex_ix(struct packed_git
*p
)
103 unsigned long ui
= (unsigned long)p
;
106 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
107 i
= (int)(ui
% pack_revindex_hashsz
);
108 while (pack_revindex
[i
].p
) {
109 if (pack_revindex
[i
].p
== p
)
111 if (++i
== pack_revindex_hashsz
)
117 static void prepare_pack_ix(void)
120 struct packed_git
*p
;
121 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
125 pack_revindex_hashsz
= num
* 11;
126 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
127 for (p
= packed_git
; p
; p
= p
->next
) {
128 num
= pack_revindex_ix(p
);
130 pack_revindex
[num
].p
= p
;
132 /* revindex elements are lazily initialized */
135 static int cmp_offset(const void *a_
, const void *b_
)
137 unsigned long a
= *(unsigned long *) a_
;
138 unsigned long b
= *(unsigned long *) b_
;
148 * Ordered list of offsets of objects in the pack.
150 static void prepare_pack_revindex(struct pack_revindex
*rix
)
152 struct packed_git
*p
= rix
->p
;
153 int num_ent
= num_packed_objects(p
);
155 void *index
= p
->index_base
+ 256;
157 rix
->revindex
= xmalloc(sizeof(unsigned long) * (num_ent
+ 1));
158 for (i
= 0; i
< num_ent
; i
++) {
159 unsigned int hl
= *((unsigned int *)(index
+ 24 * i
));
160 rix
->revindex
[i
] = ntohl(hl
);
162 /* This knows the pack format -- the 20-byte trailer
163 * follows immediately after the last object data.
165 rix
->revindex
[num_ent
] = p
->pack_size
- 20;
166 qsort(rix
->revindex
, num_ent
, sizeof(unsigned long), cmp_offset
);
169 static unsigned long find_packed_object_size(struct packed_git
*p
,
174 struct pack_revindex
*rix
;
175 unsigned long *revindex
;
176 num
= pack_revindex_ix(p
);
178 die("internal error: pack revindex uninitialized");
179 rix
= &pack_revindex
[num
];
181 prepare_pack_revindex(rix
);
182 revindex
= rix
->revindex
;
184 hi
= num_packed_objects(p
) + 1;
186 int mi
= (lo
+ hi
) / 2;
187 if (revindex
[mi
] == ofs
) {
188 return revindex
[mi
+1] - ofs
;
190 else if (ofs
< revindex
[mi
])
195 die("internal error: pack revindex corrupt");
198 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
200 unsigned long othersize
, delta_size
;
202 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, type
, &othersize
);
206 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
207 delta_buf
= diff_delta(otherbuf
, othersize
,
208 buf
, size
, &delta_size
, 0);
209 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
210 die("delta size changed");
217 * The per-object header is a pretty dense thing, which is
218 * - first byte: low four bits are "size", then three bits of "type",
219 * and the high bit is "size continues".
220 * - each byte afterwards: low seven bits are size continuation,
221 * with the high bit being "size continues"
223 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
228 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
229 die("bad type %d", type
);
231 c
= (type
<< 4) | (size
& 15);
243 static unsigned long write_object(struct sha1file
*f
,
244 struct object_entry
*entry
)
249 unsigned char header
[10];
250 unsigned hdrlen
, datalen
;
251 enum object_type obj_type
;
254 if (entry
->preferred_base
)
257 obj_type
= entry
->type
;
258 if (! entry
->in_pack
)
259 to_reuse
= 0; /* can't reuse what we don't have */
260 else if (obj_type
== OBJ_DELTA
)
261 to_reuse
= 1; /* check_object() decided it for us */
262 else if (obj_type
!= entry
->in_pack_type
)
263 to_reuse
= 0; /* pack has delta which is unusable */
264 else if (entry
->delta
)
265 to_reuse
= 0; /* we want to pack afresh */
267 to_reuse
= 1; /* we have it in-pack undeltified,
268 * and we do not need to deltify it.
272 buf
= read_sha1_file(entry
->sha1
, type
, &size
);
274 die("unable to read %s", sha1_to_hex(entry
->sha1
));
275 if (size
!= entry
->size
)
276 die("object %s size inconsistency (%lu vs %lu)",
277 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
279 buf
= delta_against(buf
, size
, entry
);
280 size
= entry
->delta_size
;
281 obj_type
= OBJ_DELTA
;
284 * The object header is a byte of 'type' followed by zero or
285 * more bytes of length. For deltas, the 20 bytes of delta
288 hdrlen
= encode_header(obj_type
, size
, header
);
289 sha1write(f
, header
, hdrlen
);
292 sha1write(f
, entry
->delta
, 20);
295 datalen
= sha1write_compressed(f
, buf
, size
);
299 struct packed_git
*p
= entry
->in_pack
;
302 datalen
= find_packed_object_size(p
, entry
->in_pack_offset
);
303 buf
= p
->pack_base
+ entry
->in_pack_offset
;
304 sha1write(f
, buf
, datalen
);
306 hdrlen
= 0; /* not really */
307 if (obj_type
== OBJ_DELTA
)
311 if (obj_type
== OBJ_DELTA
)
314 return hdrlen
+ datalen
;
317 static unsigned long write_one(struct sha1file
*f
,
318 struct object_entry
*e
,
319 unsigned long offset
)
322 /* offset starts from header size and cannot be zero
323 * if it is written already.
327 offset
+= write_object(f
, e
);
328 /* if we are deltified, write out its base object. */
330 offset
= write_one(f
, e
->delta
, offset
);
334 static void write_pack_file(void)
338 unsigned long offset
;
339 struct pack_header hdr
;
340 unsigned last_percent
= 999;
344 f
= sha1fd(1, "<stdout>");
346 f
= sha1create("%s-%s.%s", base_name
,
347 sha1_to_hex(object_list_sha1
), "pack");
348 do_progress
= progress
;
351 fprintf(stderr
, "Writing %d objects.\n", nr_result
);
353 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
354 hdr
.hdr_version
= htonl(PACK_VERSION
);
355 hdr
.hdr_entries
= htonl(nr_result
);
356 sha1write(f
, &hdr
, sizeof(hdr
));
357 offset
= sizeof(hdr
);
360 for (i
= 0; i
< nr_objects
; i
++) {
361 offset
= write_one(f
, objects
+ i
, offset
);
363 unsigned percent
= written
* 100 / nr_result
;
364 if (progress_update
|| percent
!= last_percent
) {
365 fprintf(stderr
, "%4u%% (%u/%u) done\r",
366 percent
, written
, nr_result
);
368 last_percent
= percent
;
375 sha1close(f
, pack_file_sha1
, 1);
378 static void write_index_file(void)
381 struct sha1file
*f
= sha1create("%s-%s.%s", base_name
,
382 sha1_to_hex(object_list_sha1
), "idx");
383 struct object_entry
**list
= sorted_by_sha
;
384 struct object_entry
**last
= list
+ nr_result
;
385 unsigned int array
[256];
388 * Write the first-level table (the list is sorted,
389 * but we use a 256-entry lookup to be able to avoid
390 * having to do eight extra binary search iterations).
392 for (i
= 0; i
< 256; i
++) {
393 struct object_entry
**next
= list
;
394 while (next
< last
) {
395 struct object_entry
*entry
= *next
;
396 if (entry
->sha1
[0] != i
)
400 array
[i
] = htonl(next
- sorted_by_sha
);
403 sha1write(f
, array
, 256 * sizeof(int));
406 * Write the actual SHA1 entries..
408 list
= sorted_by_sha
;
409 for (i
= 0; i
< nr_result
; i
++) {
410 struct object_entry
*entry
= *list
++;
411 unsigned int offset
= htonl(entry
->offset
);
412 sha1write(f
, &offset
, 4);
413 sha1write(f
, entry
->sha1
, 20);
415 sha1write(f
, pack_file_sha1
, 20);
416 sha1close(f
, NULL
, 1);
419 static int locate_object_entry_hash(const unsigned char *sha1
)
423 memcpy(&ui
, sha1
, sizeof(unsigned int));
424 i
= ui
% object_ix_hashsz
;
425 while (0 < object_ix
[i
]) {
426 if (!memcmp(sha1
, objects
[object_ix
[i
]-1].sha1
, 20))
428 if (++i
== object_ix_hashsz
)
434 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
438 if (!object_ix_hashsz
)
441 i
= locate_object_entry_hash(sha1
);
443 return &objects
[object_ix
[i
]-1];
447 static void rehash_objects(void)
450 struct object_entry
*oe
;
452 object_ix_hashsz
= nr_objects
* 3;
453 if (object_ix_hashsz
< 1024)
454 object_ix_hashsz
= 1024;
455 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
456 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
457 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
458 int ix
= locate_object_entry_hash(oe
->sha1
);
462 object_ix
[ix
] = i
+ 1;
467 struct name_path
*up
;
474 static unsigned name_hash(struct name_path
*path
, const char *name
)
476 struct name_path
*p
= path
;
477 const char *n
= name
+ strlen(name
);
478 unsigned hash
= 0, name_hash
= 0, name_done
= 0;
480 if (n
!= name
&& n
[-1] == '\n')
482 while (name
<= --n
) {
483 unsigned char c
= *n
;
484 if (c
== '/' && !name_done
) {
489 hash
= hash
* 11 + c
;
495 for (p
= path
; p
; p
= p
->up
) {
496 hash
= hash
* 11 + '/';
497 n
= p
->elem
+ p
->len
;
498 while (p
->elem
<= --n
) {
499 unsigned char c
= *n
;
500 hash
= hash
* 11 + c
;
504 * Make sure "Makefile" and "t/Makefile" are hashed separately
507 hash
= (name_hash
<<DIRBITS
) | (hash
& ((1U<<DIRBITS
)-1));
511 static int add_object_entry(const unsigned char *sha1
, unsigned hash
, int exclude
)
513 unsigned int idx
= nr_objects
;
514 struct object_entry
*entry
;
515 struct packed_git
*p
;
516 unsigned int found_offset
= 0;
517 struct packed_git
*found_pack
= NULL
;
521 for (p
= packed_git
; p
; p
= p
->next
) {
523 if (find_pack_entry_one(sha1
, &e
, p
)) {
526 if (local
&& !p
->pack_local
)
529 found_offset
= e
.offset
;
535 if ((entry
= locate_object_entry(sha1
)) != NULL
)
538 if (idx
>= nr_alloc
) {
539 unsigned int needed
= (idx
+ 1024) * 3 / 2;
540 objects
= xrealloc(objects
, needed
* sizeof(*entry
));
543 entry
= objects
+ idx
;
544 nr_objects
= idx
+ 1;
545 memset(entry
, 0, sizeof(*entry
));
546 memcpy(entry
->sha1
, sha1
, 20);
549 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
552 ix
= locate_object_entry_hash(entry
->sha1
);
554 die("internal error in object hashing.");
555 object_ix
[-1 - ix
] = idx
+ 1;
560 if (progress_update
) {
561 fprintf(stderr
, "Counting objects...%d\r", nr_objects
);
565 entry
->preferred_base
= 1;
568 entry
->in_pack
= found_pack
;
569 entry
->in_pack_offset
= found_offset
;
575 struct pbase_tree_cache
{
576 unsigned char sha1
[20];
580 unsigned long tree_size
;
583 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
584 static int pbase_tree_cache_ix(const unsigned char *sha1
)
586 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
588 static int pbase_tree_cache_ix_incr(int ix
)
590 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
593 static struct pbase_tree
{
594 struct pbase_tree
*next
;
595 /* This is a phony "cache" entry; we are not
596 * going to evict it nor find it through _get()
597 * mechanism -- this is for the toplevel node that
598 * would almost always change with any commit.
600 struct pbase_tree_cache pcache
;
603 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
605 struct pbase_tree_cache
*ent
, *nent
;
610 int my_ix
= pbase_tree_cache_ix(sha1
);
611 int available_ix
= -1;
613 /* pbase-tree-cache acts as a limited hashtable.
614 * your object will be found at your index or within a few
615 * slots after that slot if it is cached.
617 for (neigh
= 0; neigh
< 8; neigh
++) {
618 ent
= pbase_tree_cache
[my_ix
];
619 if (ent
&& !memcmp(ent
->sha1
, sha1
, 20)) {
623 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
624 ((0 <= available_ix
) &&
625 (!ent
&& pbase_tree_cache
[available_ix
])))
626 available_ix
= my_ix
;
629 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
632 /* Did not find one. Either we got a bogus request or
633 * we need to read and perhaps cache.
635 data
= read_sha1_file(sha1
, type
, &size
);
638 if (strcmp(type
, tree_type
)) {
643 /* We need to either cache or return a throwaway copy */
645 if (available_ix
< 0)
648 ent
= pbase_tree_cache
[available_ix
];
649 my_ix
= available_ix
;
653 nent
= xmalloc(sizeof(*nent
));
654 nent
->temporary
= (available_ix
< 0);
657 /* evict and reuse */
658 free(ent
->tree_data
);
661 memcpy(nent
->sha1
, sha1
, 20);
662 nent
->tree_data
= data
;
663 nent
->tree_size
= size
;
665 if (!nent
->temporary
)
666 pbase_tree_cache
[my_ix
] = nent
;
670 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
672 if (!cache
->temporary
) {
676 free(cache
->tree_data
);
680 static int name_cmp_len(const char *name
)
683 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
688 static void add_pbase_object(struct tree_desc
*tree
,
689 struct name_path
*up
,
693 struct name_entry entry
;
695 while (tree_entry(tree
,&entry
)) {
699 if (entry
.pathlen
!= cmplen
||
700 memcmp(entry
.path
, name
, cmplen
) ||
701 !has_sha1_file(entry
.sha1
) ||
702 sha1_object_info(entry
.sha1
, type
, &size
))
704 if (name
[cmplen
] != '/') {
705 unsigned hash
= name_hash(up
, name
);
706 add_object_entry(entry
.sha1
, hash
, 1);
709 if (!strcmp(type
, tree_type
)) {
710 struct tree_desc sub
;
712 struct pbase_tree_cache
*tree
;
713 const char *down
= name
+cmplen
+1;
714 int downlen
= name_cmp_len(down
);
716 tree
= pbase_tree_get(entry
.sha1
);
719 sub
.buf
= tree
->tree_data
;
720 sub
.size
= tree
->tree_size
;
723 me
.elem
= entry
.path
;
724 me
.len
= entry
.pathlen
;
725 add_pbase_object(&sub
, &me
, down
, downlen
);
726 pbase_tree_put(tree
);
731 static unsigned *done_pbase_paths
;
732 static int done_pbase_paths_num
;
733 static int done_pbase_paths_alloc
;
734 static int done_pbase_path_pos(unsigned hash
)
737 int hi
= done_pbase_paths_num
;
739 int mi
= (hi
+ lo
) / 2;
740 if (done_pbase_paths
[mi
] == hash
)
742 if (done_pbase_paths
[mi
] < hash
)
750 static int check_pbase_path(unsigned hash
)
752 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
756 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
757 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
758 done_pbase_paths
= xrealloc(done_pbase_paths
,
759 done_pbase_paths_alloc
*
762 done_pbase_paths_num
++;
763 if (pos
< done_pbase_paths_num
)
764 memmove(done_pbase_paths
+ pos
+ 1,
765 done_pbase_paths
+ pos
,
766 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
767 done_pbase_paths
[pos
] = hash
;
771 static void add_preferred_base_object(char *name
, unsigned hash
)
773 struct pbase_tree
*it
;
774 int cmplen
= name_cmp_len(name
);
776 if (check_pbase_path(hash
))
779 for (it
= pbase_tree
; it
; it
= it
->next
) {
781 hash
= name_hash(NULL
, "");
782 add_object_entry(it
->pcache
.sha1
, hash
, 1);
785 struct tree_desc tree
;
786 tree
.buf
= it
->pcache
.tree_data
;
787 tree
.size
= it
->pcache
.tree_size
;
788 add_pbase_object(&tree
, NULL
, name
, cmplen
);
793 static void add_preferred_base(unsigned char *sha1
)
795 struct pbase_tree
*it
;
798 unsigned char tree_sha1
[20];
800 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
804 for (it
= pbase_tree
; it
; it
= it
->next
) {
805 if (!memcmp(it
->pcache
.sha1
, tree_sha1
, 20)) {
811 it
= xcalloc(1, sizeof(*it
));
812 it
->next
= pbase_tree
;
815 memcpy(it
->pcache
.sha1
, tree_sha1
, 20);
816 it
->pcache
.tree_data
= data
;
817 it
->pcache
.tree_size
= size
;
820 static void check_object(struct object_entry
*entry
)
824 if (entry
->in_pack
&& !entry
->preferred_base
) {
825 unsigned char base
[20];
827 struct object_entry
*base_entry
;
829 /* We want in_pack_type even if we do not reuse delta.
830 * There is no point not reusing non-delta representations.
832 check_reuse_pack_delta(entry
->in_pack
,
833 entry
->in_pack_offset
,
835 &entry
->in_pack_type
);
837 /* Check if it is delta, and the base is also an object
838 * we are going to pack. If so we will reuse the existing
841 if (!no_reuse_delta
&&
842 entry
->in_pack_type
== OBJ_DELTA
&&
843 (base_entry
= locate_object_entry(base
)) &&
844 (!base_entry
->preferred_base
)) {
846 /* Depth value does not matter - find_deltas()
847 * will never consider reused delta as the
848 * base object to deltify other objects
849 * against, in order to avoid circular deltas.
852 /* uncompressed size of the delta data */
853 entry
->size
= entry
->delta_size
= size
;
854 entry
->delta
= base_entry
;
855 entry
->type
= OBJ_DELTA
;
857 entry
->delta_sibling
= base_entry
->delta_child
;
858 base_entry
->delta_child
= entry
;
862 /* Otherwise we would do the usual */
865 if (sha1_object_info(entry
->sha1
, type
, &entry
->size
))
866 die("unable to get type of object %s",
867 sha1_to_hex(entry
->sha1
));
869 if (!strcmp(type
, commit_type
)) {
870 entry
->type
= OBJ_COMMIT
;
871 } else if (!strcmp(type
, tree_type
)) {
872 entry
->type
= OBJ_TREE
;
873 } else if (!strcmp(type
, blob_type
)) {
874 entry
->type
= OBJ_BLOB
;
875 } else if (!strcmp(type
, tag_type
)) {
876 entry
->type
= OBJ_TAG
;
878 die("unable to pack object %s of type %s",
879 sha1_to_hex(entry
->sha1
), type
);
882 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
884 struct object_entry
*child
= me
->delta_child
;
887 unsigned int c
= check_delta_limit(child
, n
+ 1);
890 child
= child
->delta_sibling
;
895 static void get_object_details(void)
898 struct object_entry
*entry
;
901 for (i
= 0, entry
= objects
; i
< nr_objects
; i
++, entry
++)
904 if (nr_objects
== nr_result
) {
906 * Depth of objects that depend on the entry -- this
907 * is subtracted from depth-max to break too deep
908 * delta chain because of delta data reusing.
909 * However, we loosen this restriction when we know we
910 * are creating a thin pack -- it will have to be
911 * expanded on the other end anyway, so do not
912 * artificially cut the delta chain and let it go as
915 for (i
= 0, entry
= objects
; i
< nr_objects
; i
++, entry
++)
916 if (!entry
->delta
&& entry
->delta_child
)
918 check_delta_limit(entry
, 1);
922 typedef int (*entry_sort_t
)(const struct object_entry
*, const struct object_entry
*);
924 static entry_sort_t current_sort
;
926 static int sort_comparator(const void *_a
, const void *_b
)
928 struct object_entry
*a
= *(struct object_entry
**)_a
;
929 struct object_entry
*b
= *(struct object_entry
**)_b
;
930 return current_sort(a
,b
);
933 static struct object_entry
**create_sorted_list(entry_sort_t sort
)
935 struct object_entry
**list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
938 for (i
= 0; i
< nr_objects
; i
++)
939 list
[i
] = objects
+ i
;
941 qsort(list
, nr_objects
, sizeof(struct object_entry
*), sort_comparator
);
945 static int sha1_sort(const struct object_entry
*a
, const struct object_entry
*b
)
947 return memcmp(a
->sha1
, b
->sha1
, 20);
950 static struct object_entry
**create_final_object_list(void)
952 struct object_entry
**list
;
955 for (i
= nr_result
= 0; i
< nr_objects
; i
++)
956 if (!objects
[i
].preferred_base
)
958 list
= xmalloc(nr_result
* sizeof(struct object_entry
*));
959 for (i
= j
= 0; i
< nr_objects
; i
++) {
960 if (!objects
[i
].preferred_base
)
961 list
[j
++] = objects
+ i
;
963 current_sort
= sha1_sort
;
964 qsort(list
, nr_result
, sizeof(struct object_entry
*), sort_comparator
);
968 static int type_size_sort(const struct object_entry
*a
, const struct object_entry
*b
)
970 if (a
->type
< b
->type
)
972 if (a
->type
> b
->type
)
974 if (a
->hash
< b
->hash
)
976 if (a
->hash
> b
->hash
)
978 if (a
->preferred_base
< b
->preferred_base
)
980 if (a
->preferred_base
> b
->preferred_base
)
982 if (a
->size
< b
->size
)
984 if (a
->size
> b
->size
)
986 return a
< b
? -1 : (a
> b
);
990 struct object_entry
*entry
;
992 struct delta_index
*index
;
996 * We search for deltas _backwards_ in a list sorted by type and
997 * by size, so that we see progressively smaller and smaller files.
998 * That's because we prefer deltas to be from the bigger file
999 * to the smaller - deletes are potentially cheaper, but perhaps
1000 * more importantly, the bigger file is likely the more recent
1003 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1004 struct delta_index
*src_index
, unsigned max_depth
)
1006 struct object_entry
*trg_entry
= trg
->entry
;
1007 struct object_entry
*src_entry
= src
->entry
;
1008 unsigned long size
, src_size
, delta_size
, sizediff
, max_size
;
1011 /* Don't bother doing diffs between different types */
1012 if (trg_entry
->type
!= src_entry
->type
)
1015 /* We do not compute delta to *create* objects we are not
1018 if (trg_entry
->preferred_base
)
1022 * If the current object is at pack edge, take the depth the
1023 * objects that depend on the current object into account --
1024 * otherwise they would become too deep.
1026 if (trg_entry
->delta_child
) {
1027 if (max_depth
<= trg_entry
->delta_limit
)
1029 max_depth
-= trg_entry
->delta_limit
;
1031 if (src_entry
->depth
>= max_depth
)
1034 /* Now some size filtering heuristics. */
1035 size
= trg_entry
->size
;
1036 max_size
= size
/2 - 20;
1037 max_size
= max_size
* (max_depth
- src_entry
->depth
) / max_depth
;
1040 if (trg_entry
->delta
&& trg_entry
->delta_size
<= max_size
)
1041 max_size
= trg_entry
->delta_size
-1;
1042 src_size
= src_entry
->size
;
1043 sizediff
= src_size
< size
? size
- src_size
: 0;
1044 if (sizediff
>= max_size
)
1047 delta_buf
= create_delta(src_index
, trg
->data
, size
, &delta_size
, max_size
);
1051 trg_entry
->delta
= src_entry
;
1052 trg_entry
->delta_size
= delta_size
;
1053 trg_entry
->depth
= src_entry
->depth
+ 1;
1058 static void progress_interval(int signum
)
1060 progress_update
= 1;
1063 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
1066 unsigned int array_size
= window
* sizeof(struct unpacked
);
1067 struct unpacked
*array
= xmalloc(array_size
);
1068 unsigned processed
= 0;
1069 unsigned last_percent
= 999;
1071 memset(array
, 0, array_size
);
1075 fprintf(stderr
, "Deltifying %d objects.\n", nr_result
);
1078 struct object_entry
*entry
= list
[i
];
1079 struct unpacked
*n
= array
+ idx
;
1084 if (!entry
->preferred_base
)
1088 unsigned percent
= processed
* 100 / nr_result
;
1089 if (percent
!= last_percent
|| progress_update
) {
1090 fprintf(stderr
, "%4u%% (%u/%u) done\r",
1091 percent
, processed
, nr_result
);
1092 progress_update
= 0;
1093 last_percent
= percent
;
1098 /* This happens if we decided to reuse existing
1099 * delta from a pack. "!no_reuse_delta &&" is implied.
1103 if (entry
->size
< 50)
1105 free_delta_index(n
->index
);
1109 n
->data
= read_sha1_file(entry
->sha1
, type
, &size
);
1110 if (size
!= entry
->size
)
1111 die("object %s inconsistent object length (%lu vs %lu)",
1112 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
1116 unsigned int other_idx
= idx
+ j
;
1118 if (other_idx
>= window
)
1119 other_idx
-= window
;
1120 m
= array
+ other_idx
;
1123 if (try_delta(n
, m
, m
->index
, depth
) < 0)
1126 /* if we made n a delta, and if n is already at max
1127 * depth, leaving it in the window is pointless. we
1128 * should evict it first.
1130 if (entry
->delta
&& depth
<= entry
->depth
)
1133 n
->index
= create_delta_index(n
->data
, size
);
1135 die("out of memory");
1143 fputc('\n', stderr
);
1145 for (i
= 0; i
< window
; ++i
) {
1146 free_delta_index(array
[i
].index
);
1147 free(array
[i
].data
);
1152 static void prepare_pack(int window
, int depth
)
1154 get_object_details();
1155 sorted_by_type
= create_sorted_list(type_size_sort
);
1156 if (window
&& depth
)
1157 find_deltas(sorted_by_type
, window
+1, depth
);
1160 static int reuse_cached_pack(unsigned char *sha1
, int pack_to_stdout
)
1162 static const char cache
[] = "pack-cache/pack-%s.%s";
1163 char *cached_pack
, *cached_idx
;
1164 int ifd
, ofd
, ifd_ix
= -1;
1166 cached_pack
= git_path(cache
, sha1_to_hex(sha1
), "pack");
1167 ifd
= open(cached_pack
, O_RDONLY
);
1171 if (!pack_to_stdout
) {
1172 cached_idx
= git_path(cache
, sha1_to_hex(sha1
), "idx");
1173 ifd_ix
= open(cached_idx
, O_RDONLY
);
1181 fprintf(stderr
, "Reusing %d objects pack %s\n", nr_objects
,
1184 if (pack_to_stdout
) {
1185 if (copy_fd(ifd
, 1))
1190 char name
[PATH_MAX
];
1191 snprintf(name
, sizeof(name
),
1192 "%s-%s.%s", base_name
, sha1_to_hex(sha1
), "pack");
1193 ofd
= open(name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
1195 die("unable to open %s (%s)", name
, strerror(errno
));
1196 if (copy_fd(ifd
, ofd
))
1200 snprintf(name
, sizeof(name
),
1201 "%s-%s.%s", base_name
, sha1_to_hex(sha1
), "idx");
1202 ofd
= open(name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
1204 die("unable to open %s (%s)", name
, strerror(errno
));
1205 if (copy_fd(ifd_ix
, ofd
))
1208 puts(sha1_to_hex(sha1
));
1214 static void setup_progress_signal(void)
1216 struct sigaction sa
;
1219 memset(&sa
, 0, sizeof(sa
));
1220 sa
.sa_handler
= progress_interval
;
1221 sigemptyset(&sa
.sa_mask
);
1222 sa
.sa_flags
= SA_RESTART
;
1223 sigaction(SIGALRM
, &sa
, NULL
);
1225 v
.it_interval
.tv_sec
= 1;
1226 v
.it_interval
.tv_usec
= 0;
1227 v
.it_value
= v
.it_interval
;
1228 setitimer(ITIMER_REAL
, &v
, NULL
);
1231 int main(int argc
, char **argv
)
1234 char line
[40 + 1 + PATH_MAX
+ 2];
1235 int window
= 10, depth
= 10, pack_to_stdout
= 0;
1236 struct object_entry
**list
;
1237 int num_preferred_base
= 0;
1240 setup_git_directory();
1242 progress
= isatty(2);
1243 for (i
= 1; i
< argc
; i
++) {
1244 const char *arg
= argv
[i
];
1247 if (!strcmp("--non-empty", arg
)) {
1251 if (!strcmp("--local", arg
)) {
1255 if (!strcmp("--incremental", arg
)) {
1259 if (!strncmp("--window=", arg
, 9)) {
1261 window
= strtoul(arg
+9, &end
, 0);
1262 if (!arg
[9] || *end
)
1266 if (!strncmp("--depth=", arg
, 8)) {
1268 depth
= strtoul(arg
+8, &end
, 0);
1269 if (!arg
[8] || *end
)
1273 if (!strcmp("--progress", arg
)) {
1277 if (!strcmp("-q", arg
)) {
1281 if (!strcmp("--no-reuse-delta", arg
)) {
1285 if (!strcmp("--stdout", arg
)) {
1296 if (pack_to_stdout
!= !base_name
)
1299 prepare_packed_git();
1302 fprintf(stderr
, "Generating pack...\n");
1303 setup_progress_signal();
1307 unsigned char sha1
[20];
1310 if (!fgets(line
, sizeof(line
), stdin
)) {
1314 die("fgets returned NULL, not EOF, not error!");
1316 die("fgets: %s", strerror(errno
));
1321 if (line
[0] == '-') {
1322 if (get_sha1_hex(line
+1, sha1
))
1323 die("expected edge sha1, got garbage:\n %s",
1325 if (num_preferred_base
++ < window
)
1326 add_preferred_base(sha1
);
1329 if (get_sha1_hex(line
, sha1
))
1330 die("expected sha1, got garbage:\n %s", line
);
1331 hash
= name_hash(NULL
, line
+41);
1332 add_preferred_base_object(line
+41, hash
);
1333 add_object_entry(sha1
, hash
, 0);
1336 fprintf(stderr
, "Done counting %d objects.\n", nr_objects
);
1337 sorted_by_sha
= create_final_object_list();
1338 if (non_empty
&& !nr_result
)
1342 list
= sorted_by_sha
;
1343 for (i
= 0; i
< nr_result
; i
++) {
1344 struct object_entry
*entry
= *list
++;
1345 SHA1_Update(&ctx
, entry
->sha1
, 20);
1347 SHA1_Final(object_list_sha1
, &ctx
);
1348 if (progress
&& (nr_objects
!= nr_result
))
1349 fprintf(stderr
, "Result has %d objects.\n", nr_result
);
1351 if (reuse_cached_pack(object_list_sha1
, pack_to_stdout
))
1355 prepare_pack(window
, depth
);
1356 if (progress
&& pack_to_stdout
) {
1357 /* the other end usually displays progress itself */
1358 struct itimerval v
= {{0,},};
1359 setitimer(ITIMER_REAL
, &v
, NULL
);
1360 signal(SIGALRM
, SIG_IGN
);
1361 progress_update
= 0;
1364 if (!pack_to_stdout
) {
1366 puts(sha1_to_hex(object_list_sha1
));
1370 fprintf(stderr
, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
1371 nr_result
, written
, written_delta
, reused
, reused_delta
);