8 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";
11 unsigned char sha1
[20];
12 unsigned long size
; /* uncompressed size */
13 unsigned long offset
; /* offset into the final pack file;
14 * nonzero if already written.
16 unsigned int depth
; /* delta depth */
17 unsigned int delta_limit
; /* base adjustment for in-pack delta */
18 unsigned int hash
; /* name hint hash */
19 enum object_type type
;
20 enum object_type in_pack_type
; /* could be delta */
21 unsigned long delta_size
; /* delta data size (uncompressed) */
22 struct object_entry
*delta
; /* delta base object */
23 struct packed_git
*in_pack
; /* already in pack */
24 unsigned int in_pack_offset
;
25 struct object_entry
*delta_child
; /* delitified objects who bases me */
26 struct object_entry
*delta_sibling
; /* other deltified objects who
27 * uses the same base as me
32 * Objects we are going to pack are colected in objects array (dynamically
33 * expanded). nr_objects & nr_alloc controls this array. They are stored
34 * in the order we see -- typically rev-list --objects order that gives us
35 * nice "minimum seek" order.
37 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
38 * elements in the objects array. The former is used to build the pack
39 * index (lists object names in the ascending order to help offset lookup),
40 * and the latter is used to group similar things together by try_delta()
44 static unsigned char object_list_sha1
[20];
45 static int non_empty
= 0;
46 static int no_reuse_delta
= 0;
48 static int incremental
= 0;
49 static struct object_entry
**sorted_by_sha
, **sorted_by_type
;
50 static struct object_entry
*objects
= NULL
;
51 static int nr_objects
= 0, nr_alloc
= 0;
52 static const char *base_name
;
53 static unsigned char pack_file_sha1
[20];
54 static int progress
= 1;
57 * The object names in objects array are hashed with this hashtable,
58 * to help looking up the entry by object name. Binary search from
59 * sorted_by_sha is also possible but this was easier to code and faster.
60 * This hashtable is built after all the objects are seen.
62 static int *object_ix
= NULL
;
63 static int object_ix_hashsz
= 0;
66 * Pack index for existing packs give us easy access to the offsets into
67 * corresponding pack file where each object's data starts, but the entries
68 * do not store the size of the compressed representation (uncompressed
69 * size is easily available by examining the pack entry header). We build
70 * a hashtable of existing packs (pack_revindex), and keep reverse index
71 * here -- pack index file is sorted by object name mapping to offset; this
72 * pack_revindex[].revindex array is an ordered list of offsets, so if you
73 * know the offset of an object, next offset is where its packed
74 * representation ends.
76 struct pack_revindex
{
78 unsigned long *revindex
;
79 } *pack_revindex
= NULL
;
80 static int pack_revindex_hashsz
= 0;
85 static int written
= 0;
86 static int written_delta
= 0;
87 static int reused
= 0;
88 static int reused_delta
= 0;
90 static int pack_revindex_ix(struct packed_git
*p
)
92 unsigned int ui
= (unsigned int) p
;
95 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
96 i
= (int)(ui
% pack_revindex_hashsz
);
97 while (pack_revindex
[i
].p
) {
98 if (pack_revindex
[i
].p
== p
)
100 if (++i
== pack_revindex_hashsz
)
106 static void prepare_pack_ix(void)
109 struct packed_git
*p
;
110 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
114 pack_revindex_hashsz
= num
* 11;
115 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
116 for (p
= packed_git
; p
; p
= p
->next
) {
117 num
= pack_revindex_ix(p
);
119 pack_revindex
[num
].p
= p
;
121 /* revindex elements are lazily initialized */
124 static int cmp_offset(const void *a_
, const void *b_
)
126 unsigned long a
= *(unsigned long *) a_
;
127 unsigned long b
= *(unsigned long *) b_
;
137 * Ordered list of offsets of objects in the pack.
139 static void prepare_pack_revindex(struct pack_revindex
*rix
)
141 struct packed_git
*p
= rix
->p
;
142 int num_ent
= num_packed_objects(p
);
144 void *index
= p
->index_base
+ 256;
146 rix
->revindex
= xmalloc(sizeof(unsigned long) * (num_ent
+ 1));
147 for (i
= 0; i
< num_ent
; i
++) {
148 long hl
= *((long *)(index
+ 24 * i
));
149 rix
->revindex
[i
] = ntohl(hl
);
151 /* This knows the pack format -- the 20-byte trailer
152 * follows immediately after the last object data.
154 rix
->revindex
[num_ent
] = p
->pack_size
- 20;
155 qsort(rix
->revindex
, num_ent
, sizeof(unsigned long), cmp_offset
);
158 static unsigned long find_packed_object_size(struct packed_git
*p
,
163 struct pack_revindex
*rix
;
164 unsigned long *revindex
;
165 num
= pack_revindex_ix(p
);
167 die("internal error: pack revindex uninitialized");
168 rix
= &pack_revindex
[num
];
170 prepare_pack_revindex(rix
);
171 revindex
= rix
->revindex
;
173 hi
= num_packed_objects(p
) + 1;
175 int mi
= (lo
+ hi
) / 2;
176 if (revindex
[mi
] == ofs
) {
177 return revindex
[mi
+1] - ofs
;
179 else if (ofs
< revindex
[mi
])
184 die("internal error: pack revindex corrupt");
187 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
189 unsigned long othersize
, delta_size
;
191 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, type
, &othersize
);
195 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
196 delta_buf
= diff_delta(otherbuf
, othersize
,
197 buf
, size
, &delta_size
, 0);
198 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
199 die("delta size changed");
206 * The per-object header is a pretty dense thing, which is
207 * - first byte: low four bits are "size", then three bits of "type",
208 * and the high bit is "size continues".
209 * - each byte afterwards: low seven bits are size continuation,
210 * with the high bit being "size continues"
212 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
217 if (type
< OBJ_COMMIT
|| type
> OBJ_DELTA
)
218 die("bad type %d", type
);
220 c
= (type
<< 4) | (size
& 15);
232 static unsigned long write_object(struct sha1file
*f
, struct object_entry
*entry
)
237 unsigned char header
[10];
238 unsigned hdrlen
, datalen
;
239 enum object_type obj_type
;
242 obj_type
= entry
->type
;
243 if (! entry
->in_pack
)
244 to_reuse
= 0; /* can't reuse what we don't have */
245 else if (obj_type
== OBJ_DELTA
)
246 to_reuse
= 1; /* check_object() decided it for us */
247 else if (obj_type
!= entry
->in_pack_type
)
248 to_reuse
= 0; /* pack has delta which is unusable */
249 else if (entry
->delta
)
250 to_reuse
= 0; /* we want to pack afresh */
252 to_reuse
= 1; /* we have it in-pack undeltified,
253 * and we do not need to deltify it.
257 buf
= read_sha1_file(entry
->sha1
, type
, &size
);
259 die("unable to read %s", sha1_to_hex(entry
->sha1
));
260 if (size
!= entry
->size
)
261 die("object %s size inconsistency (%lu vs %lu)",
262 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
264 buf
= delta_against(buf
, size
, entry
);
265 size
= entry
->delta_size
;
266 obj_type
= OBJ_DELTA
;
269 * The object header is a byte of 'type' followed by zero or
270 * more bytes of length. For deltas, the 20 bytes of delta
273 hdrlen
= encode_header(obj_type
, size
, header
);
274 sha1write(f
, header
, hdrlen
);
277 sha1write(f
, entry
->delta
, 20);
280 datalen
= sha1write_compressed(f
, buf
, size
);
284 struct packed_git
*p
= entry
->in_pack
;
287 datalen
= find_packed_object_size(p
, entry
->in_pack_offset
);
288 buf
= p
->pack_base
+ entry
->in_pack_offset
;
289 sha1write(f
, buf
, datalen
);
291 hdrlen
= 0; /* not really */
292 if (obj_type
== OBJ_DELTA
)
296 if (obj_type
== OBJ_DELTA
)
299 return hdrlen
+ datalen
;
302 static unsigned long write_one(struct sha1file
*f
,
303 struct object_entry
*e
,
304 unsigned long offset
)
307 /* offset starts from header size and cannot be zero
308 * if it is written already.
312 offset
+= write_object(f
, e
);
313 /* if we are deltified, write out its base object. */
315 offset
= write_one(f
, e
->delta
, offset
);
319 static void write_pack_file(void)
323 unsigned long offset
;
324 struct pack_header hdr
;
327 f
= sha1fd(1, "<stdout>");
329 f
= sha1create("%s-%s.%s", base_name
, sha1_to_hex(object_list_sha1
), "pack");
330 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
331 hdr
.hdr_version
= htonl(PACK_VERSION
);
332 hdr
.hdr_entries
= htonl(nr_objects
);
333 sha1write(f
, &hdr
, sizeof(hdr
));
334 offset
= sizeof(hdr
);
335 for (i
= 0; i
< nr_objects
; i
++)
336 offset
= write_one(f
, objects
+ i
, offset
);
338 sha1close(f
, pack_file_sha1
, 1);
341 static void write_index_file(void)
344 struct sha1file
*f
= sha1create("%s-%s.%s", base_name
, sha1_to_hex(object_list_sha1
), "idx");
345 struct object_entry
**list
= sorted_by_sha
;
346 struct object_entry
**last
= list
+ nr_objects
;
347 unsigned int array
[256];
350 * Write the first-level table (the list is sorted,
351 * but we use a 256-entry lookup to be able to avoid
352 * having to do eight extra binary search iterations).
354 for (i
= 0; i
< 256; i
++) {
355 struct object_entry
**next
= list
;
356 while (next
< last
) {
357 struct object_entry
*entry
= *next
;
358 if (entry
->sha1
[0] != i
)
362 array
[i
] = htonl(next
- sorted_by_sha
);
365 sha1write(f
, array
, 256 * sizeof(int));
368 * Write the actual SHA1 entries..
370 list
= sorted_by_sha
;
371 for (i
= 0; i
< nr_objects
; i
++) {
372 struct object_entry
*entry
= *list
++;
373 unsigned int offset
= htonl(entry
->offset
);
374 sha1write(f
, &offset
, 4);
375 sha1write(f
, entry
->sha1
, 20);
377 sha1write(f
, pack_file_sha1
, 20);
378 sha1close(f
, NULL
, 1);
381 static int add_object_entry(unsigned char *sha1
, unsigned int hash
)
383 unsigned int idx
= nr_objects
;
384 struct object_entry
*entry
;
385 struct packed_git
*p
;
386 unsigned int found_offset
= 0;
387 struct packed_git
*found_pack
= NULL
;
389 for (p
= packed_git
; p
; p
= p
->next
) {
391 if (find_pack_entry_one(sha1
, &e
, p
)) {
394 if (local
&& !p
->pack_local
)
397 found_offset
= e
.offset
;
403 if (idx
>= nr_alloc
) {
404 unsigned int needed
= (idx
+ 1024) * 3 / 2;
405 objects
= xrealloc(objects
, needed
* sizeof(*entry
));
408 entry
= objects
+ idx
;
409 memset(entry
, 0, sizeof(*entry
));
410 memcpy(entry
->sha1
, sha1
, 20);
413 entry
->in_pack
= found_pack
;
414 entry
->in_pack_offset
= found_offset
;
420 static int locate_object_entry_hash(unsigned char *sha1
)
424 memcpy(&ui
, sha1
, sizeof(unsigned int));
425 i
= ui
% object_ix_hashsz
;
426 while (0 < object_ix
[i
]) {
427 if (!memcmp(sha1
, objects
[object_ix
[i
]-1].sha1
, 20))
429 if (++i
== object_ix_hashsz
)
435 static struct object_entry
*locate_object_entry(unsigned char *sha1
)
437 int i
= locate_object_entry_hash(sha1
);
439 return &objects
[object_ix
[i
]-1];
443 static void check_object(struct object_entry
*entry
)
447 if (entry
->in_pack
) {
448 unsigned char base
[20];
450 struct object_entry
*base_entry
;
452 /* We want in_pack_type even if we do not reuse delta.
453 * There is no point not reusing non-delta representations.
455 check_reuse_pack_delta(entry
->in_pack
,
456 entry
->in_pack_offset
,
458 &entry
->in_pack_type
);
460 /* Check if it is delta, and the base is also an object
461 * we are going to pack. If so we will reuse the existing
464 if (!no_reuse_delta
&&
465 entry
->in_pack_type
== OBJ_DELTA
&&
466 (base_entry
= locate_object_entry(base
))) {
468 /* Depth value does not matter - find_deltas()
469 * will never consider reused delta as the
470 * base object to deltify other objects
471 * against, in order to avoid circular deltas.
474 /* uncompressed size of the delta data */
475 entry
->size
= entry
->delta_size
= size
;
476 entry
->delta
= base_entry
;
477 entry
->type
= OBJ_DELTA
;
479 entry
->delta_sibling
= base_entry
->delta_child
;
480 base_entry
->delta_child
= entry
;
484 /* Otherwise we would do the usual */
487 if (sha1_object_info(entry
->sha1
, type
, &entry
->size
))
488 die("unable to get type of object %s",
489 sha1_to_hex(entry
->sha1
));
491 if (!strcmp(type
, "commit")) {
492 entry
->type
= OBJ_COMMIT
;
493 } else if (!strcmp(type
, "tree")) {
494 entry
->type
= OBJ_TREE
;
495 } else if (!strcmp(type
, "blob")) {
496 entry
->type
= OBJ_BLOB
;
497 } else if (!strcmp(type
, "tag")) {
498 entry
->type
= OBJ_TAG
;
500 die("unable to pack object %s of type %s",
501 sha1_to_hex(entry
->sha1
), type
);
504 static void hash_objects(void)
507 struct object_entry
*oe
;
509 object_ix_hashsz
= nr_objects
* 2;
510 object_ix
= xcalloc(sizeof(int), object_ix_hashsz
);
511 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
512 int ix
= locate_object_entry_hash(oe
->sha1
);
514 error("the same object '%s' added twice",
515 sha1_to_hex(oe
->sha1
));
519 object_ix
[ix
] = i
+ 1;
523 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
525 struct object_entry
*child
= me
->delta_child
;
528 unsigned int c
= check_delta_limit(child
, n
+ 1);
531 child
= child
->delta_sibling
;
536 static void get_object_details(void)
539 struct object_entry
*entry
;
543 for (i
= 0, entry
= objects
; i
< nr_objects
; i
++, entry
++)
545 for (i
= 0, entry
= objects
; i
< nr_objects
; i
++, entry
++)
546 if (!entry
->delta
&& entry
->delta_child
)
548 check_delta_limit(entry
, 1);
551 typedef int (*entry_sort_t
)(const struct object_entry
*, const struct object_entry
*);
553 static entry_sort_t current_sort
;
555 static int sort_comparator(const void *_a
, const void *_b
)
557 struct object_entry
*a
= *(struct object_entry
**)_a
;
558 struct object_entry
*b
= *(struct object_entry
**)_b
;
559 return current_sort(a
,b
);
562 static struct object_entry
**create_sorted_list(entry_sort_t sort
)
564 struct object_entry
**list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
567 for (i
= 0; i
< nr_objects
; i
++)
568 list
[i
] = objects
+ i
;
570 qsort(list
, nr_objects
, sizeof(struct object_entry
*), sort_comparator
);
574 static int sha1_sort(const struct object_entry
*a
, const struct object_entry
*b
)
576 return memcmp(a
->sha1
, b
->sha1
, 20);
579 static int type_size_sort(const struct object_entry
*a
, const struct object_entry
*b
)
581 if (a
->type
< b
->type
)
583 if (a
->type
> b
->type
)
585 if (a
->hash
< b
->hash
)
587 if (a
->hash
> b
->hash
)
589 if (a
->size
< b
->size
)
591 if (a
->size
> b
->size
)
593 return a
< b
? -1 : (a
> b
);
597 struct object_entry
*entry
;
602 * We search for deltas _backwards_ in a list sorted by type and
603 * by size, so that we see progressively smaller and smaller files.
604 * That's because we prefer deltas to be from the bigger file
605 * to the smaller - deletes are potentially cheaper, but perhaps
606 * more importantly, the bigger file is likely the more recent
609 static int try_delta(struct unpacked
*cur
, struct unpacked
*old
, unsigned max_depth
)
611 struct object_entry
*cur_entry
= cur
->entry
;
612 struct object_entry
*old_entry
= old
->entry
;
613 unsigned long size
, oldsize
, delta_size
, sizediff
;
617 /* Don't bother doing diffs between different types */
618 if (cur_entry
->type
!= old_entry
->type
)
621 /* If the current object is at edge, take the depth the objects
622 * that depend on the current object into account -- otherwise
623 * they would become too deep.
625 if (cur_entry
->delta_child
) {
626 if (max_depth
<= cur_entry
->delta_limit
)
628 max_depth
-= cur_entry
->delta_limit
;
631 size
= cur_entry
->size
;
632 oldsize
= old_entry
->size
;
633 sizediff
= oldsize
> size
? oldsize
- size
: size
- oldsize
;
637 if (old_entry
->depth
>= max_depth
)
643 * We always delta from the bigger to the smaller, since that's
644 * more space-efficient (deletes don't have to say _what_ they
647 max_size
= size
/ 2 - 20;
648 if (cur_entry
->delta
)
649 max_size
= cur_entry
->delta_size
-1;
650 if (sizediff
>= max_size
)
652 delta_buf
= diff_delta(old
->data
, oldsize
,
653 cur
->data
, size
, &delta_size
, max_size
);
656 cur_entry
->delta
= old_entry
;
657 cur_entry
->delta_size
= delta_size
;
658 cur_entry
->depth
= old_entry
->depth
+ 1;
663 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
666 unsigned int array_size
= window
* sizeof(struct unpacked
);
667 struct unpacked
*array
= xmalloc(array_size
);
670 memset(array
, 0, array_size
);
673 eye_candy
= i
- (nr_objects
/ 20);
676 struct object_entry
*entry
= list
[i
];
677 struct unpacked
*n
= array
+ idx
;
682 if (progress
&& i
<= eye_candy
) {
683 eye_candy
-= nr_objects
/ 20;
688 /* This happens if we decided to reuse existing
689 * delta from a pack. "!no_reuse_delta &&" is implied.
695 n
->data
= read_sha1_file(entry
->sha1
, type
, &size
);
696 if (size
!= entry
->size
)
697 die("object %s inconsistent object length (%lu vs %lu)", sha1_to_hex(entry
->sha1
), size
, entry
->size
);
701 unsigned int other_idx
= idx
+ j
;
703 if (other_idx
>= window
)
705 m
= array
+ other_idx
;
708 if (try_delta(n
, m
, depth
) < 0)
716 for (i
= 0; i
< window
; ++i
)
721 static void prepare_pack(int window
, int depth
)
724 fprintf(stderr
, "Packing %d objects", nr_objects
);
725 get_object_details();
729 sorted_by_type
= create_sorted_list(type_size_sort
);
731 find_deltas(sorted_by_type
, window
+1, depth
);
737 static int reuse_cached_pack(unsigned char *sha1
, int pack_to_stdout
)
739 static const char cache
[] = "pack-cache/pack-%s.%s";
740 char *cached_pack
, *cached_idx
;
741 int ifd
, ofd
, ifd_ix
= -1;
743 cached_pack
= git_path(cache
, sha1_to_hex(sha1
), "pack");
744 ifd
= open(cached_pack
, O_RDONLY
);
748 if (!pack_to_stdout
) {
749 cached_idx
= git_path(cache
, sha1_to_hex(sha1
), "idx");
750 ifd_ix
= open(cached_idx
, O_RDONLY
);
758 fprintf(stderr
, "Reusing %d objects pack %s\n", nr_objects
,
761 if (pack_to_stdout
) {
768 snprintf(name
, sizeof(name
),
769 "%s-%s.%s", base_name
, sha1_to_hex(sha1
), "pack");
770 ofd
= open(name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
772 die("unable to open %s (%s)", name
, strerror(errno
));
773 if (copy_fd(ifd
, ofd
))
777 snprintf(name
, sizeof(name
),
778 "%s-%s.%s", base_name
, sha1_to_hex(sha1
), "idx");
779 ofd
= open(name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
781 die("unable to open %s (%s)", name
, strerror(errno
));
782 if (copy_fd(ifd_ix
, ofd
))
785 puts(sha1_to_hex(sha1
));
791 int main(int argc
, char **argv
)
794 char line
[PATH_MAX
+ 20];
795 int window
= 10, depth
= 10, pack_to_stdout
= 0;
796 struct object_entry
**list
;
798 struct timeval prev_tv
;
800 int eye_candy_incr
= 500;
803 setup_git_directory();
805 for (i
= 1; i
< argc
; i
++) {
806 const char *arg
= argv
[i
];
809 if (!strcmp("--non-empty", arg
)) {
813 if (!strcmp("--local", arg
)) {
817 if (!strcmp("--incremental", arg
)) {
821 if (!strncmp("--window=", arg
, 9)) {
823 window
= strtoul(arg
+9, &end
, 0);
828 if (!strncmp("--depth=", arg
, 8)) {
830 depth
= strtoul(arg
+8, &end
, 0);
835 if (!strcmp("-q", arg
)) {
839 if (!strcmp("--no-reuse-delta", arg
)) {
843 if (!strcmp("--stdout", arg
)) {
854 if (pack_to_stdout
!= !base_name
)
857 prepare_packed_git();
859 fprintf(stderr
, "Generating pack...\n");
860 gettimeofday(&prev_tv
, NULL
);
862 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
865 unsigned char sha1
[20];
867 if (progress
&& (eye_candy
<= nr_objects
)) {
868 fprintf(stderr
, "Counting objects...%d\r", nr_objects
);
869 if (eye_candy
&& (50 <= eye_candy_incr
)) {
872 gettimeofday(&tv
, NULL
);
873 time_diff
= (tv
.tv_sec
- prev_tv
.tv_sec
);
875 time_diff
+= (tv
.tv_usec
- prev_tv
.tv_usec
);
876 if ((1 << 9) < time_diff
)
877 eye_candy_incr
+= 50;
878 else if (50 < eye_candy_incr
)
879 eye_candy_incr
-= 50;
881 eye_candy
+= eye_candy_incr
;
883 if (get_sha1_hex(line
, sha1
))
884 die("expected sha1, got garbage:\n %s", line
);
888 unsigned char c
= *p
++;
891 hash
= hash
* 11 + c
;
893 add_object_entry(sha1
, hash
);
896 fprintf(stderr
, "Done counting %d objects.\n", nr_objects
);
897 if (non_empty
&& !nr_objects
)
900 sorted_by_sha
= create_sorted_list(sha1_sort
);
902 list
= sorted_by_sha
;
903 for (i
= 0; i
< nr_objects
; i
++) {
904 struct object_entry
*entry
= *list
++;
905 SHA1_Update(&ctx
, entry
->sha1
, 20);
907 SHA1_Final(object_list_sha1
, &ctx
);
909 if (reuse_cached_pack(object_list_sha1
, pack_to_stdout
))
912 prepare_pack(window
, depth
);
913 if (!pack_to_stdout
) {
915 puts(sha1_to_hex(object_list_sha1
));
919 fprintf(stderr
, "Total %d, written %d (delta %d), reused %d (delta %d)\n",
920 nr_objects
, written
, written_delta
, reused
, reused_delta
);