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 uint32_t crc32
; /* crc of raw pack data for this object */
26 off_t offset
; /* offset into the final pack file */
27 unsigned long size
; /* uncompressed size */
28 unsigned int hash
; /* name hint hash */
29 unsigned int depth
; /* delta depth */
30 struct packed_git
*in_pack
; /* already in pack */
32 struct object_entry
*delta
; /* delta base object */
33 struct object_entry
*delta_child
; /* deltified objects who bases me */
34 struct object_entry
*delta_sibling
; /* other deltified objects who
35 * uses the same base as me
37 unsigned long delta_size
; /* delta data size (uncompressed) */
38 enum object_type type
;
39 enum object_type in_pack_type
; /* could be delta */
40 unsigned char in_pack_header_size
;
41 unsigned char preferred_base
; /* we do not pack this, but is available
42 * to be used as the base objectto delta
48 * Objects we are going to pack are collected in objects array (dynamically
49 * expanded). nr_objects & nr_alloc controls this array. They are stored
50 * in the order we see -- typically rev-list --objects order that gives us
51 * nice "minimum seek" order.
53 * sorted-by-sha ans sorted-by-type are arrays of pointers that point at
54 * elements in the objects array. The former is used to build the pack
55 * index (lists object names in the ascending order to help offset lookup),
56 * and the latter is used to group similar things together by try_delta()
61 static int no_reuse_delta
;
63 static int incremental
;
64 static int allow_ofs_delta
;
66 static struct object_entry
*objects
;
67 static uint32_t nr_objects
, nr_alloc
, nr_result
;
68 static const char *pack_tmp_name
, *idx_tmp_name
;
69 static char tmpname
[PATH_MAX
];
70 static unsigned char pack_file_sha1
[20];
71 static int progress
= 1;
72 static volatile sig_atomic_t progress_update
;
73 static int window
= 10;
74 static int pack_to_stdout
;
75 static int num_preferred_base
;
78 * The object names in objects array are hashed with this hashtable,
79 * to help looking up the entry by object name. Binary search from
80 * sorted_by_sha is also possible but this was easier to code and faster.
81 * This hashtable is built after all the objects are seen.
83 static int *object_ix
;
84 static int object_ix_hashsz
;
87 * Pack index for existing packs give us easy access to the offsets into
88 * corresponding pack file where each object's data starts, but the entries
89 * do not store the size of the compressed representation (uncompressed
90 * size is easily available by examining the pack entry header). It is
91 * also rather expensive to find the sha1 for an object given its offset.
93 * We build a hashtable of existing packs (pack_revindex), and keep reverse
94 * index here -- pack index file is sorted by object name mapping to offset;
95 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
96 * ordered by offset, so if you know the offset of an object, next offset
97 * is where its packed representation ends and the index_nr can be used to
98 * get the object sha1 from the main index.
100 struct revindex_entry
{
104 struct pack_revindex
{
105 struct packed_git
*p
;
106 struct revindex_entry
*revindex
;
108 static struct pack_revindex
*pack_revindex
;
109 static int pack_revindex_hashsz
;
114 static uint32_t written
, written_delta
;
115 static uint32_t reused
, reused_delta
;
117 static int pack_revindex_ix(struct packed_git
*p
)
119 unsigned long ui
= (unsigned long)p
;
122 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
123 i
= (int)(ui
% pack_revindex_hashsz
);
124 while (pack_revindex
[i
].p
) {
125 if (pack_revindex
[i
].p
== p
)
127 if (++i
== pack_revindex_hashsz
)
133 static void prepare_pack_ix(void)
136 struct packed_git
*p
;
137 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
141 pack_revindex_hashsz
= num
* 11;
142 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
143 for (p
= packed_git
; p
; p
= p
->next
) {
144 num
= pack_revindex_ix(p
);
146 pack_revindex
[num
].p
= p
;
148 /* revindex elements are lazily initialized */
151 static int cmp_offset(const void *a_
, const void *b_
)
153 const struct revindex_entry
*a
= a_
;
154 const struct revindex_entry
*b
= b_
;
155 return (a
->offset
< b
->offset
) ? -1 : (a
->offset
> b
->offset
) ? 1 : 0;
159 * Ordered list of offsets of objects in the pack.
161 static void prepare_pack_revindex(struct pack_revindex
*rix
)
163 struct packed_git
*p
= rix
->p
;
164 int num_ent
= p
->num_objects
;
166 const char *index
= p
->index_data
;
168 rix
->revindex
= xmalloc(sizeof(*rix
->revindex
) * (num_ent
+ 1));
171 if (p
->index_version
> 1) {
172 const uint32_t *off_32
=
173 (uint32_t *)(index
+ 8 + p
->num_objects
* (20 + 4));
174 const uint32_t *off_64
= off_32
+ p
->num_objects
;
175 for (i
= 0; i
< num_ent
; i
++) {
176 uint32_t off
= ntohl(*off_32
++);
177 if (!(off
& 0x80000000)) {
178 rix
->revindex
[i
].offset
= off
;
180 rix
->revindex
[i
].offset
=
181 ((uint64_t)ntohl(*off_64
++)) << 32;
182 rix
->revindex
[i
].offset
|=
185 rix
->revindex
[i
].nr
= i
;
188 for (i
= 0; i
< num_ent
; i
++) {
189 uint32_t hl
= *((uint32_t *)(index
+ 24 * i
));
190 rix
->revindex
[i
].offset
= ntohl(hl
);
191 rix
->revindex
[i
].nr
= i
;
195 /* This knows the pack format -- the 20-byte trailer
196 * follows immediately after the last object data.
198 rix
->revindex
[num_ent
].offset
= p
->pack_size
- 20;
199 rix
->revindex
[num_ent
].nr
= -1;
200 qsort(rix
->revindex
, num_ent
, sizeof(*rix
->revindex
), cmp_offset
);
203 static struct revindex_entry
* find_packed_object(struct packed_git
*p
,
208 struct pack_revindex
*rix
;
209 struct revindex_entry
*revindex
;
210 num
= pack_revindex_ix(p
);
212 die("internal error: pack revindex uninitialized");
213 rix
= &pack_revindex
[num
];
215 prepare_pack_revindex(rix
);
216 revindex
= rix
->revindex
;
218 hi
= p
->num_objects
+ 1;
220 int mi
= (lo
+ hi
) / 2;
221 if (revindex
[mi
].offset
== ofs
) {
222 return revindex
+ mi
;
224 else if (ofs
< revindex
[mi
].offset
)
229 die("internal error: pack revindex corrupt");
232 static const unsigned char *find_packed_object_name(struct packed_git
*p
,
235 struct revindex_entry
*entry
= find_packed_object(p
, ofs
);
236 return nth_packed_object_sha1(p
, entry
->nr
);
239 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
241 unsigned long othersize
, delta_size
;
242 enum object_type type
;
243 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, &type
, &othersize
);
247 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
248 delta_buf
= diff_delta(otherbuf
, othersize
,
249 buf
, size
, &delta_size
, 0);
250 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
251 die("delta size changed");
258 * The per-object header is a pretty dense thing, which is
259 * - first byte: low four bits are "size", then three bits of "type",
260 * and the high bit is "size continues".
261 * - each byte afterwards: low seven bits are size continuation,
262 * with the high bit being "size continues"
264 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
269 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
270 die("bad type %d", type
);
272 c
= (type
<< 4) | (size
& 15);
285 * we are going to reuse the existing object data as is. make
286 * sure it is not corrupt.
288 static int check_pack_inflate(struct packed_git
*p
,
289 struct pack_window
**w_curs
,
292 unsigned long expect
)
295 unsigned char fakebuf
[4096], *in
;
298 memset(&stream
, 0, sizeof(stream
));
299 inflateInit(&stream
);
301 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
303 stream
.next_out
= fakebuf
;
304 stream
.avail_out
= sizeof(fakebuf
);
305 st
= inflate(&stream
, Z_FINISH
);
306 offset
+= stream
.next_in
- in
;
307 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
309 return (st
== Z_STREAM_END
&&
310 stream
.total_out
== expect
&&
311 stream
.total_in
== len
) ? 0 : -1;
314 static int check_pack_crc(struct packed_git
*p
, struct pack_window
**w_curs
,
315 off_t offset
, off_t len
, unsigned int nr
)
317 const uint32_t *index_crc
;
318 uint32_t data_crc
= crc32(0, Z_NULL
, 0);
322 void *data
= use_pack(p
, w_curs
, offset
, &avail
);
325 data_crc
= crc32(data_crc
, data
, avail
);
330 index_crc
= p
->index_data
;
331 index_crc
+= 2 + 256 + p
->num_objects
* (20/4) + nr
;
333 return data_crc
!= ntohl(*index_crc
);
336 static void copy_pack_data(struct sha1file
*f
,
337 struct packed_git
*p
,
338 struct pack_window
**w_curs
,
346 in
= use_pack(p
, w_curs
, offset
, &avail
);
348 avail
= (unsigned int)len
;
349 sha1write(f
, in
, avail
);
355 static int check_loose_inflate(unsigned char *data
, unsigned long len
, unsigned long expect
)
358 unsigned char fakebuf
[4096];
361 memset(&stream
, 0, sizeof(stream
));
362 stream
.next_in
= data
;
363 stream
.avail_in
= len
;
364 stream
.next_out
= fakebuf
;
365 stream
.avail_out
= sizeof(fakebuf
);
366 inflateInit(&stream
);
369 st
= inflate(&stream
, Z_FINISH
);
370 if (st
== Z_STREAM_END
|| st
== Z_OK
) {
371 st
= (stream
.total_out
== expect
&&
372 stream
.total_in
== len
) ? 0 : -1;
375 if (st
!= Z_BUF_ERROR
) {
379 stream
.next_out
= fakebuf
;
380 stream
.avail_out
= sizeof(fakebuf
);
386 static int revalidate_loose_object(struct object_entry
*entry
,
388 unsigned long mapsize
)
390 /* we already know this is a loose object with new type header. */
391 enum object_type type
;
392 unsigned long size
, used
;
397 used
= unpack_object_header_gently(map
, mapsize
, &type
, &size
);
402 return check_loose_inflate(map
, mapsize
, size
);
405 static unsigned long write_object(struct sha1file
*f
,
406 struct object_entry
*entry
)
409 enum object_type type
;
411 unsigned char header
[10];
414 enum object_type obj_type
;
420 obj_type
= entry
->type
;
421 if (! entry
->in_pack
)
422 to_reuse
= 0; /* can't reuse what we don't have */
423 else if (obj_type
== OBJ_REF_DELTA
|| obj_type
== OBJ_OFS_DELTA
)
424 to_reuse
= 1; /* check_object() decided it for us */
425 else if (obj_type
!= entry
->in_pack_type
)
426 to_reuse
= 0; /* pack has delta which is unusable */
427 else if (entry
->delta
)
428 to_reuse
= 0; /* we want to pack afresh */
430 to_reuse
= 1; /* we have it in-pack undeltified,
431 * and we do not need to deltify it.
434 if (!entry
->in_pack
&& !entry
->delta
) {
436 unsigned long mapsize
;
437 map
= map_sha1_file(entry
->sha1
, &mapsize
);
438 if (map
&& !legacy_loose_object(map
)) {
439 /* We can copy straight into the pack file */
440 if (revalidate_loose_object(entry
, map
, mapsize
))
441 die("corrupt loose object %s",
442 sha1_to_hex(entry
->sha1
));
443 sha1write(f
, map
, mapsize
);
444 munmap(map
, mapsize
);
450 munmap(map
, mapsize
);
454 buf
= read_sha1_file(entry
->sha1
, &type
, &size
);
456 die("unable to read %s", sha1_to_hex(entry
->sha1
));
457 if (size
!= entry
->size
)
458 die("object %s size inconsistency (%lu vs %lu)",
459 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
461 buf
= delta_against(buf
, size
, entry
);
462 size
= entry
->delta_size
;
463 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
464 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
467 * The object header is a byte of 'type' followed by zero or
468 * more bytes of length.
470 hdrlen
= encode_header(obj_type
, size
, header
);
471 sha1write(f
, header
, hdrlen
);
473 if (obj_type
== OBJ_OFS_DELTA
) {
475 * Deltas with relative base contain an additional
476 * encoding of the relative offset for the delta
477 * base from this object's position in the pack.
479 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
480 unsigned pos
= sizeof(header
) - 1;
481 header
[pos
] = ofs
& 127;
483 header
[--pos
] = 128 | (--ofs
& 127);
484 sha1write(f
, header
+ pos
, sizeof(header
) - pos
);
485 hdrlen
+= sizeof(header
) - pos
;
486 } else if (obj_type
== OBJ_REF_DELTA
) {
488 * Deltas with a base reference contain
489 * an additional 20 bytes for the base sha1.
491 sha1write(f
, entry
->delta
->sha1
, 20);
494 datalen
= sha1write_compressed(f
, buf
, size
);
498 struct packed_git
*p
= entry
->in_pack
;
499 struct pack_window
*w_curs
= NULL
;
500 struct revindex_entry
*revidx
;
504 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
505 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
508 hdrlen
= encode_header(obj_type
, entry
->size
, header
);
509 sha1write(f
, header
, hdrlen
);
510 if (obj_type
== OBJ_OFS_DELTA
) {
511 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
512 unsigned pos
= sizeof(header
) - 1;
513 header
[pos
] = ofs
& 127;
515 header
[--pos
] = 128 | (--ofs
& 127);
516 sha1write(f
, header
+ pos
, sizeof(header
) - pos
);
517 hdrlen
+= sizeof(header
) - pos
;
518 } else if (obj_type
== OBJ_REF_DELTA
) {
519 sha1write(f
, entry
->delta
->sha1
, 20);
523 offset
= entry
->in_pack_offset
;
524 revidx
= find_packed_object(p
, offset
);
525 datalen
= revidx
[1].offset
- offset
;
526 if (!pack_to_stdout
&& p
->index_version
> 1 &&
527 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
))
528 die("bad packed object CRC for %s", sha1_to_hex(entry
->sha1
));
529 offset
+= entry
->in_pack_header_size
;
530 datalen
-= entry
->in_pack_header_size
;
531 if (!pack_to_stdout
&& p
->index_version
== 1 &&
532 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
))
533 die("corrupt packed object for %s", sha1_to_hex(entry
->sha1
));
534 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
542 entry
->crc32
= crc32_end(f
);
543 return hdrlen
+ datalen
;
546 static off_t
write_one(struct sha1file
*f
,
547 struct object_entry
*e
,
552 /* offset is non zero if object is written already. */
553 if (e
->offset
|| e
->preferred_base
)
556 /* if we are deltified, write out base object first. */
558 offset
= write_one(f
, e
->delta
, offset
);
561 size
= write_object(f
, e
);
563 /* make sure off_t is sufficiently large not to wrap */
564 if (offset
> offset
+ size
)
565 die("pack too large for current definition of off_t");
566 return offset
+ size
;
569 static off_t
write_pack_file(void)
573 off_t offset
, last_obj_offset
= 0;
574 struct pack_header hdr
;
575 unsigned last_percent
= 999;
576 int do_progress
= progress
;
578 if (pack_to_stdout
) {
579 f
= sha1fd(1, "<stdout>");
583 snprintf(tmpname
, sizeof(tmpname
), "tmp_pack_XXXXXX");
584 fd
= mkstemp(tmpname
);
586 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
587 pack_tmp_name
= xstrdup(tmpname
);
588 f
= sha1fd(fd
, pack_tmp_name
);
592 fprintf(stderr
, "Writing %u objects.\n", nr_result
);
594 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
595 hdr
.hdr_version
= htonl(PACK_VERSION
);
596 hdr
.hdr_entries
= htonl(nr_result
);
597 sha1write(f
, &hdr
, sizeof(hdr
));
598 offset
= sizeof(hdr
);
601 for (i
= 0; i
< nr_objects
; i
++) {
602 last_obj_offset
= offset
;
603 offset
= write_one(f
, objects
+ i
, offset
);
605 unsigned percent
= written
* 100 / nr_result
;
606 if (progress_update
|| percent
!= last_percent
) {
607 fprintf(stderr
, "%4u%% (%u/%u) done\r",
608 percent
, written
, nr_result
);
610 last_percent
= percent
;
617 if (written
!= nr_result
)
618 die("wrote %u objects while expecting %u", written
, nr_result
);
619 sha1close(f
, pack_file_sha1
, 1);
621 return last_obj_offset
;
624 static int sha1_sort(const void *_a
, const void *_b
)
626 const struct object_entry
*a
= *(struct object_entry
**)_a
;
627 const struct object_entry
*b
= *(struct object_entry
**)_b
;
628 return hashcmp(a
->sha1
, b
->sha1
);
631 static uint32_t index_default_version
= 1;
632 static uint32_t index_off32_limit
= 0x7fffffff;
634 static void write_index_file(off_t last_obj_offset
, unsigned char *sha1
)
637 struct object_entry
**sorted_by_sha
, **list
, **last
;
639 uint32_t i
, index_version
;
643 snprintf(tmpname
, sizeof(tmpname
), "tmp_idx_XXXXXX");
644 fd
= mkstemp(tmpname
);
646 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
647 idx_tmp_name
= xstrdup(tmpname
);
648 f
= sha1fd(fd
, idx_tmp_name
);
653 xcalloc(nr_result
, sizeof(struct object_entry
*));
654 for (i
= 0; i
< nr_objects
; i
++)
655 if (!objects
[i
].preferred_base
)
656 sorted_by_sha
[j
++] = objects
+ i
;
658 die("listed %u objects while expecting %u", j
, nr_result
);
659 qsort(sorted_by_sha
, nr_result
, sizeof(*sorted_by_sha
), sha1_sort
);
660 list
= sorted_by_sha
;
661 last
= sorted_by_sha
+ nr_result
;
663 sorted_by_sha
= list
= last
= NULL
;
665 /* if last object's offset is >= 2^31 we should use index V2 */
666 index_version
= (last_obj_offset
>> 31) ? 2 : index_default_version
;
668 /* index versions 2 and above need a header */
669 if (index_version
>= 2) {
670 struct pack_idx_header hdr
;
671 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
672 hdr
.idx_version
= htonl(index_version
);
673 sha1write(f
, &hdr
, sizeof(hdr
));
677 * Write the first-level table (the list is sorted,
678 * but we use a 256-entry lookup to be able to avoid
679 * having to do eight extra binary search iterations).
681 for (i
= 0; i
< 256; i
++) {
682 struct object_entry
**next
= list
;
683 while (next
< last
) {
684 struct object_entry
*entry
= *next
;
685 if (entry
->sha1
[0] != i
)
689 array
[i
] = htonl(next
- sorted_by_sha
);
692 sha1write(f
, array
, 256 * 4);
694 /* Compute the SHA1 hash of sorted object names. */
697 /* Write the actual SHA1 entries. */
698 list
= sorted_by_sha
;
699 for (i
= 0; i
< nr_result
; i
++) {
700 struct object_entry
*entry
= *list
++;
701 if (index_version
< 2) {
702 uint32_t offset
= htonl(entry
->offset
);
703 sha1write(f
, &offset
, 4);
705 sha1write(f
, entry
->sha1
, 20);
706 SHA1_Update(&ctx
, entry
->sha1
, 20);
709 if (index_version
>= 2) {
710 unsigned int nr_large_offset
= 0;
712 /* write the crc32 table */
713 list
= sorted_by_sha
;
714 for (i
= 0; i
< nr_objects
; i
++) {
715 struct object_entry
*entry
= *list
++;
716 uint32_t crc32_val
= htonl(entry
->crc32
);
717 sha1write(f
, &crc32_val
, 4);
720 /* write the 32-bit offset table */
721 list
= sorted_by_sha
;
722 for (i
= 0; i
< nr_objects
; i
++) {
723 struct object_entry
*entry
= *list
++;
724 uint32_t offset
= (entry
->offset
<= index_off32_limit
) ?
725 entry
->offset
: (0x80000000 | nr_large_offset
++);
726 offset
= htonl(offset
);
727 sha1write(f
, &offset
, 4);
730 /* write the large offset table */
731 list
= sorted_by_sha
;
732 while (nr_large_offset
) {
733 struct object_entry
*entry
= *list
++;
734 uint64_t offset
= entry
->offset
;
735 if (offset
> index_off32_limit
) {
737 split
[0] = htonl(offset
>> 32);
738 split
[1] = htonl(offset
& 0xffffffff);
739 sha1write(f
, split
, 8);
745 sha1write(f
, pack_file_sha1
, 20);
746 sha1close(f
, NULL
, 1);
748 SHA1_Final(sha1
, &ctx
);
751 static int locate_object_entry_hash(const unsigned char *sha1
)
755 memcpy(&ui
, sha1
, sizeof(unsigned int));
756 i
= ui
% object_ix_hashsz
;
757 while (0 < object_ix
[i
]) {
758 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].sha1
))
760 if (++i
== object_ix_hashsz
)
766 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
770 if (!object_ix_hashsz
)
773 i
= locate_object_entry_hash(sha1
);
775 return &objects
[object_ix
[i
]-1];
779 static void rehash_objects(void)
782 struct object_entry
*oe
;
784 object_ix_hashsz
= nr_objects
* 3;
785 if (object_ix_hashsz
< 1024)
786 object_ix_hashsz
= 1024;
787 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
788 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
789 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
790 int ix
= locate_object_entry_hash(oe
->sha1
);
794 object_ix
[ix
] = i
+ 1;
798 static unsigned name_hash(const char *name
)
804 * This effectively just creates a sortable number from the
805 * last sixteen non-whitespace characters. Last characters
806 * count "most", so things that end in ".c" sort together.
808 while ((c
= *name
++) != 0) {
811 hash
= (hash
>> 2) + (c
<< 24);
816 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
817 unsigned hash
, int exclude
)
819 struct object_entry
*entry
;
820 struct packed_git
*p
, *found_pack
= NULL
;
821 off_t found_offset
= 0;
824 ix
= nr_objects
? locate_object_entry_hash(sha1
) : -1;
827 entry
= objects
+ object_ix
[ix
] - 1;
828 if (!entry
->preferred_base
)
830 entry
->preferred_base
= 1;
835 for (p
= packed_git
; p
; p
= p
->next
) {
836 off_t offset
= find_pack_entry_one(sha1
, p
);
839 found_offset
= offset
;
846 if (local
&& !p
->pack_local
)
851 if (nr_objects
>= nr_alloc
) {
852 nr_alloc
= (nr_alloc
+ 1024) * 3 / 2;
853 objects
= xrealloc(objects
, nr_alloc
* sizeof(*entry
));
856 entry
= objects
+ nr_objects
++;
857 memset(entry
, 0, sizeof(*entry
));
858 hashcpy(entry
->sha1
, sha1
);
863 entry
->preferred_base
= 1;
867 entry
->in_pack
= found_pack
;
868 entry
->in_pack_offset
= found_offset
;
871 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
874 object_ix
[-1 - ix
] = nr_objects
;
876 if (progress_update
) {
877 fprintf(stderr
, "Counting objects...%u\r", nr_objects
);
884 struct pbase_tree_cache
{
885 unsigned char sha1
[20];
889 unsigned long tree_size
;
892 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
893 static int pbase_tree_cache_ix(const unsigned char *sha1
)
895 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
897 static int pbase_tree_cache_ix_incr(int ix
)
899 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
902 static struct pbase_tree
{
903 struct pbase_tree
*next
;
904 /* This is a phony "cache" entry; we are not
905 * going to evict it nor find it through _get()
906 * mechanism -- this is for the toplevel node that
907 * would almost always change with any commit.
909 struct pbase_tree_cache pcache
;
912 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
914 struct pbase_tree_cache
*ent
, *nent
;
917 enum object_type type
;
919 int my_ix
= pbase_tree_cache_ix(sha1
);
920 int available_ix
= -1;
922 /* pbase-tree-cache acts as a limited hashtable.
923 * your object will be found at your index or within a few
924 * slots after that slot if it is cached.
926 for (neigh
= 0; neigh
< 8; neigh
++) {
927 ent
= pbase_tree_cache
[my_ix
];
928 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
932 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
933 ((0 <= available_ix
) &&
934 (!ent
&& pbase_tree_cache
[available_ix
])))
935 available_ix
= my_ix
;
938 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
941 /* Did not find one. Either we got a bogus request or
942 * we need to read and perhaps cache.
944 data
= read_sha1_file(sha1
, &type
, &size
);
947 if (type
!= OBJ_TREE
) {
952 /* We need to either cache or return a throwaway copy */
954 if (available_ix
< 0)
957 ent
= pbase_tree_cache
[available_ix
];
958 my_ix
= available_ix
;
962 nent
= xmalloc(sizeof(*nent
));
963 nent
->temporary
= (available_ix
< 0);
966 /* evict and reuse */
967 free(ent
->tree_data
);
970 hashcpy(nent
->sha1
, sha1
);
971 nent
->tree_data
= data
;
972 nent
->tree_size
= size
;
974 if (!nent
->temporary
)
975 pbase_tree_cache
[my_ix
] = nent
;
979 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
981 if (!cache
->temporary
) {
985 free(cache
->tree_data
);
989 static int name_cmp_len(const char *name
)
992 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
997 static void add_pbase_object(struct tree_desc
*tree
,
1000 const char *fullname
)
1002 struct name_entry entry
;
1005 while (tree_entry(tree
,&entry
)) {
1006 cmp
= tree_entry_len(entry
.path
, entry
.sha1
) != cmplen
? 1 :
1007 memcmp(name
, entry
.path
, cmplen
);
1012 if (name
[cmplen
] != '/') {
1013 unsigned hash
= name_hash(fullname
);
1014 add_object_entry(entry
.sha1
,
1015 S_ISDIR(entry
.mode
) ? OBJ_TREE
: OBJ_BLOB
,
1019 if (S_ISDIR(entry
.mode
)) {
1020 struct tree_desc sub
;
1021 struct pbase_tree_cache
*tree
;
1022 const char *down
= name
+cmplen
+1;
1023 int downlen
= name_cmp_len(down
);
1025 tree
= pbase_tree_get(entry
.sha1
);
1028 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1030 add_pbase_object(&sub
, down
, downlen
, fullname
);
1031 pbase_tree_put(tree
);
1036 static unsigned *done_pbase_paths
;
1037 static int done_pbase_paths_num
;
1038 static int done_pbase_paths_alloc
;
1039 static int done_pbase_path_pos(unsigned hash
)
1042 int hi
= done_pbase_paths_num
;
1044 int mi
= (hi
+ lo
) / 2;
1045 if (done_pbase_paths
[mi
] == hash
)
1047 if (done_pbase_paths
[mi
] < hash
)
1055 static int check_pbase_path(unsigned hash
)
1057 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1061 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1062 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1063 done_pbase_paths
= xrealloc(done_pbase_paths
,
1064 done_pbase_paths_alloc
*
1067 done_pbase_paths_num
++;
1068 if (pos
< done_pbase_paths_num
)
1069 memmove(done_pbase_paths
+ pos
+ 1,
1070 done_pbase_paths
+ pos
,
1071 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1072 done_pbase_paths
[pos
] = hash
;
1076 static void add_preferred_base_object(const char *name
, unsigned hash
)
1078 struct pbase_tree
*it
;
1081 if (!num_preferred_base
|| check_pbase_path(hash
))
1084 cmplen
= name_cmp_len(name
);
1085 for (it
= pbase_tree
; it
; it
= it
->next
) {
1087 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, 0, 1);
1090 struct tree_desc tree
;
1091 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1092 add_pbase_object(&tree
, name
, cmplen
, name
);
1097 static void add_preferred_base(unsigned char *sha1
)
1099 struct pbase_tree
*it
;
1102 unsigned char tree_sha1
[20];
1104 if (window
<= num_preferred_base
++)
1107 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1111 for (it
= pbase_tree
; it
; it
= it
->next
) {
1112 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1118 it
= xcalloc(1, sizeof(*it
));
1119 it
->next
= pbase_tree
;
1122 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1123 it
->pcache
.tree_data
= data
;
1124 it
->pcache
.tree_size
= size
;
1127 static void check_object(struct object_entry
*entry
)
1129 if (entry
->in_pack
) {
1130 struct packed_git
*p
= entry
->in_pack
;
1131 struct pack_window
*w_curs
= NULL
;
1132 const unsigned char *base_ref
= NULL
;
1133 struct object_entry
*base_entry
;
1134 unsigned long used
, used_0
;
1137 unsigned char *buf
, c
;
1139 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1142 * We want in_pack_type even if we do not reuse delta.
1143 * There is no point not reusing non-delta representations.
1145 used
= unpack_object_header_gently(buf
, avail
,
1146 &entry
->in_pack_type
,
1150 * Determine if this is a delta and if so whether we can
1151 * reuse it or not. Otherwise let's find out as cheaply as
1152 * possible what the actual type and size for this object is.
1154 switch (entry
->in_pack_type
) {
1156 /* Not a delta hence we've already got all we need. */
1157 entry
->type
= entry
->in_pack_type
;
1158 entry
->in_pack_header_size
= used
;
1159 unuse_pack(&w_curs
);
1162 if (!no_reuse_delta
&& !entry
->preferred_base
)
1163 base_ref
= use_pack(p
, &w_curs
,
1164 entry
->in_pack_offset
+ used
, NULL
);
1165 entry
->in_pack_header_size
= used
+ 20;
1168 buf
= use_pack(p
, &w_curs
,
1169 entry
->in_pack_offset
+ used
, NULL
);
1175 if (!ofs
|| MSB(ofs
, 7))
1176 die("delta base offset overflow in pack for %s",
1177 sha1_to_hex(entry
->sha1
));
1179 ofs
= (ofs
<< 7) + (c
& 127);
1181 if (ofs
>= entry
->in_pack_offset
)
1182 die("delta base offset out of bound for %s",
1183 sha1_to_hex(entry
->sha1
));
1184 ofs
= entry
->in_pack_offset
- ofs
;
1185 if (!no_reuse_delta
&& !entry
->preferred_base
)
1186 base_ref
= find_packed_object_name(p
, ofs
);
1187 entry
->in_pack_header_size
= used
+ used_0
;
1191 if (base_ref
&& (base_entry
= locate_object_entry(base_ref
))) {
1193 * If base_ref was set above that means we wish to
1194 * reuse delta data, and we even found that base
1195 * in the list of objects we want to pack. Goodie!
1197 * Depth value does not matter - find_deltas() will
1198 * never consider reused delta as the base object to
1199 * deltify other objects against, in order to avoid
1202 entry
->type
= entry
->in_pack_type
;
1203 entry
->delta
= base_entry
;
1204 entry
->delta_sibling
= base_entry
->delta_child
;
1205 base_entry
->delta_child
= entry
;
1206 unuse_pack(&w_curs
);
1212 * This must be a delta and we already know what the
1213 * final object type is. Let's extract the actual
1214 * object size from the delta header.
1216 entry
->size
= get_size_from_delta(p
, &w_curs
,
1217 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1218 unuse_pack(&w_curs
);
1223 * No choice but to fall back to the recursive delta walk
1224 * with sha1_object_info() to find about the object type
1227 unuse_pack(&w_curs
);
1230 entry
->type
= sha1_object_info(entry
->sha1
, &entry
->size
);
1231 if (entry
->type
< 0)
1232 die("unable to get type of object %s",
1233 sha1_to_hex(entry
->sha1
));
1236 static int pack_offset_sort(const void *_a
, const void *_b
)
1238 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1239 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1241 /* avoid filesystem trashing with loose objects */
1242 if (!a
->in_pack
&& !b
->in_pack
)
1243 return hashcmp(a
->sha1
, b
->sha1
);
1245 if (a
->in_pack
< b
->in_pack
)
1247 if (a
->in_pack
> b
->in_pack
)
1249 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1250 (a
->in_pack_offset
> b
->in_pack_offset
);
1253 static void get_object_details(void)
1256 struct object_entry
**sorted_by_offset
;
1258 sorted_by_offset
= xcalloc(nr_objects
, sizeof(struct object_entry
*));
1259 for (i
= 0; i
< nr_objects
; i
++)
1260 sorted_by_offset
[i
] = objects
+ i
;
1261 qsort(sorted_by_offset
, nr_objects
, sizeof(*sorted_by_offset
), pack_offset_sort
);
1264 for (i
= 0; i
< nr_objects
; i
++)
1265 check_object(sorted_by_offset
[i
]);
1266 free(sorted_by_offset
);
1269 static int type_size_sort(const void *_a
, const void *_b
)
1271 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1272 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1274 if (a
->type
< b
->type
)
1276 if (a
->type
> b
->type
)
1278 if (a
->hash
< b
->hash
)
1280 if (a
->hash
> b
->hash
)
1282 if (a
->preferred_base
< b
->preferred_base
)
1284 if (a
->preferred_base
> b
->preferred_base
)
1286 if (a
->size
< b
->size
)
1288 if (a
->size
> b
->size
)
1290 return a
> b
? -1 : (a
< b
); /* newest last */
1294 struct object_entry
*entry
;
1296 struct delta_index
*index
;
1300 * We search for deltas _backwards_ in a list sorted by type and
1301 * by size, so that we see progressively smaller and smaller files.
1302 * That's because we prefer deltas to be from the bigger file
1303 * to the smaller - deletes are potentially cheaper, but perhaps
1304 * more importantly, the bigger file is likely the more recent
1307 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1310 struct object_entry
*trg_entry
= trg
->entry
;
1311 struct object_entry
*src_entry
= src
->entry
;
1312 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1313 enum object_type type
;
1316 /* Don't bother doing diffs between different types */
1317 if (trg_entry
->type
!= src_entry
->type
)
1320 /* We do not compute delta to *create* objects we are not
1323 if (trg_entry
->preferred_base
)
1327 * We do not bother to try a delta that we discarded
1328 * on an earlier try, but only when reusing delta data.
1330 if (!no_reuse_delta
&& trg_entry
->in_pack
&&
1331 trg_entry
->in_pack
== src_entry
->in_pack
&&
1332 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1333 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1336 /* Let's not bust the allowed depth. */
1337 if (src_entry
->depth
>= max_depth
)
1340 /* Now some size filtering heuristics. */
1341 trg_size
= trg_entry
->size
;
1342 max_size
= trg_size
/2 - 20;
1343 max_size
= max_size
* (max_depth
- src_entry
->depth
) / max_depth
;
1346 if (trg_entry
->delta
&& trg_entry
->delta_size
<= max_size
)
1347 max_size
= trg_entry
->delta_size
-1;
1348 src_size
= src_entry
->size
;
1349 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1350 if (sizediff
>= max_size
)
1353 /* Load data if not already done */
1355 trg
->data
= read_sha1_file(trg_entry
->sha1
, &type
, &sz
);
1357 die("object %s inconsistent object length (%lu vs %lu)",
1358 sha1_to_hex(trg_entry
->sha1
), sz
, trg_size
);
1361 src
->data
= read_sha1_file(src_entry
->sha1
, &type
, &sz
);
1363 die("object %s inconsistent object length (%lu vs %lu)",
1364 sha1_to_hex(src_entry
->sha1
), sz
, src_size
);
1367 src
->index
= create_delta_index(src
->data
, src_size
);
1369 die("out of memory");
1372 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1376 trg_entry
->delta
= src_entry
;
1377 trg_entry
->delta_size
= delta_size
;
1378 trg_entry
->depth
= src_entry
->depth
+ 1;
1383 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1385 struct object_entry
*child
= me
->delta_child
;
1388 unsigned int c
= check_delta_limit(child
, n
+ 1);
1391 child
= child
->delta_sibling
;
1396 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
1398 uint32_t i
= nr_objects
, idx
= 0, processed
= 0;
1399 unsigned int array_size
= window
* sizeof(struct unpacked
);
1400 struct unpacked
*array
;
1401 unsigned last_percent
= 999;
1406 array
= xmalloc(array_size
);
1407 memset(array
, 0, array_size
);
1409 fprintf(stderr
, "Deltifying %u objects.\n", nr_result
);
1412 struct object_entry
*entry
= list
[--i
];
1413 struct unpacked
*n
= array
+ idx
;
1416 if (!entry
->preferred_base
)
1420 unsigned percent
= processed
* 100 / nr_result
;
1421 if (percent
!= last_percent
|| progress_update
) {
1422 fprintf(stderr
, "%4u%% (%u/%u) done\r",
1423 percent
, processed
, nr_result
);
1424 progress_update
= 0;
1425 last_percent
= percent
;
1430 /* This happens if we decided to reuse existing
1431 * delta from a pack. "!no_reuse_delta &&" is implied.
1435 if (entry
->size
< 50)
1437 free_delta_index(n
->index
);
1444 * If the current object is at pack edge, take the depth the
1445 * objects that depend on the current object into account
1446 * otherwise they would become too deep.
1449 if (entry
->delta_child
) {
1450 max_depth
-= check_delta_limit(entry
, 0);
1457 uint32_t other_idx
= idx
+ j
;
1459 if (other_idx
>= window
)
1460 other_idx
-= window
;
1461 m
= array
+ other_idx
;
1464 if (try_delta(n
, m
, max_depth
) < 0)
1468 /* if we made n a delta, and if n is already at max
1469 * depth, leaving it in the window is pointless. we
1470 * should evict it first.
1472 if (entry
->delta
&& depth
<= entry
->depth
)
1482 fputc('\n', stderr
);
1484 for (i
= 0; i
< window
; ++i
) {
1485 free_delta_index(array
[i
].index
);
1486 free(array
[i
].data
);
1491 static void prepare_pack(int window
, int depth
)
1493 struct object_entry
**delta_list
;
1496 get_object_details();
1498 if (!window
|| !depth
)
1501 delta_list
= xmalloc(nr_objects
* sizeof(*delta_list
));
1502 for (i
= 0; i
< nr_objects
; i
++)
1503 delta_list
[i
] = objects
+ i
;
1504 qsort(delta_list
, nr_objects
, sizeof(*delta_list
), type_size_sort
);
1505 find_deltas(delta_list
, window
+1, depth
);
1509 static void progress_interval(int signum
)
1511 progress_update
= 1;
1514 static void setup_progress_signal(void)
1516 struct sigaction sa
;
1519 memset(&sa
, 0, sizeof(sa
));
1520 sa
.sa_handler
= progress_interval
;
1521 sigemptyset(&sa
.sa_mask
);
1522 sa
.sa_flags
= SA_RESTART
;
1523 sigaction(SIGALRM
, &sa
, NULL
);
1525 v
.it_interval
.tv_sec
= 1;
1526 v
.it_interval
.tv_usec
= 0;
1527 v
.it_value
= v
.it_interval
;
1528 setitimer(ITIMER_REAL
, &v
, NULL
);
1531 static int git_pack_config(const char *k
, const char *v
)
1533 if(!strcmp(k
, "pack.window")) {
1534 window
= git_config_int(k
, v
);
1537 return git_default_config(k
, v
);
1540 static void read_object_list_from_stdin(void)
1542 char line
[40 + 1 + PATH_MAX
+ 2];
1543 unsigned char sha1
[20];
1547 if (!fgets(line
, sizeof(line
), stdin
)) {
1551 die("fgets returned NULL, not EOF, not error!");
1553 die("fgets: %s", strerror(errno
));
1557 if (line
[0] == '-') {
1558 if (get_sha1_hex(line
+1, sha1
))
1559 die("expected edge sha1, got garbage:\n %s",
1561 add_preferred_base(sha1
);
1564 if (get_sha1_hex(line
, sha1
))
1565 die("expected sha1, got garbage:\n %s", line
);
1567 hash
= name_hash(line
+41);
1568 add_preferred_base_object(line
+41, hash
);
1569 add_object_entry(sha1
, 0, hash
, 0);
1573 static void show_commit(struct commit
*commit
)
1575 add_object_entry(commit
->object
.sha1
, OBJ_COMMIT
, 0, 0);
1578 static void show_object(struct object_array_entry
*p
)
1580 unsigned hash
= name_hash(p
->name
);
1581 add_preferred_base_object(p
->name
, hash
);
1582 add_object_entry(p
->item
->sha1
, p
->item
->type
, hash
, 0);
1585 static void show_edge(struct commit
*commit
)
1587 add_preferred_base(commit
->object
.sha1
);
1590 static void get_object_list(int ac
, const char **av
)
1592 struct rev_info revs
;
1596 init_revisions(&revs
, NULL
);
1597 save_commit_buffer
= 0;
1598 track_object_refs
= 0;
1599 setup_revisions(ac
, av
, &revs
, NULL
);
1601 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
1602 int len
= strlen(line
);
1603 if (line
[len
- 1] == '\n')
1608 if (!strcmp(line
, "--not")) {
1609 flags
^= UNINTERESTING
;
1612 die("not a rev '%s'", line
);
1614 if (handle_revision_arg(line
, &revs
, flags
, 1))
1615 die("bad revision '%s'", line
);
1618 prepare_revision_walk(&revs
);
1619 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
1620 traverse_commit_list(&revs
, show_commit
, show_object
);
1623 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
1626 int use_internal_rev_list
= 0;
1629 off_t last_obj_offset
;
1630 const char *base_name
= NULL
;
1632 int rp_ac_alloc
= 64;
1635 rp_av
= xcalloc(rp_ac_alloc
, sizeof(*rp_av
));
1637 rp_av
[0] = "pack-objects";
1638 rp_av
[1] = "--objects"; /* --thin will make it --objects-edge */
1641 git_config(git_pack_config
);
1643 progress
= isatty(2);
1644 for (i
= 1; i
< argc
; i
++) {
1645 const char *arg
= argv
[i
];
1650 if (!strcmp("--non-empty", arg
)) {
1654 if (!strcmp("--local", arg
)) {
1658 if (!strcmp("--incremental", arg
)) {
1662 if (!prefixcmp(arg
, "--window=")) {
1664 window
= strtoul(arg
+9, &end
, 0);
1665 if (!arg
[9] || *end
)
1669 if (!prefixcmp(arg
, "--depth=")) {
1671 depth
= strtoul(arg
+8, &end
, 0);
1672 if (!arg
[8] || *end
)
1676 if (!strcmp("--progress", arg
)) {
1680 if (!strcmp("--all-progress", arg
)) {
1684 if (!strcmp("-q", arg
)) {
1688 if (!strcmp("--no-reuse-delta", arg
)) {
1692 if (!strcmp("--delta-base-offset", arg
)) {
1693 allow_ofs_delta
= 1;
1696 if (!strcmp("--stdout", arg
)) {
1700 if (!strcmp("--revs", arg
)) {
1701 use_internal_rev_list
= 1;
1704 if (!strcmp("--unpacked", arg
) ||
1705 !prefixcmp(arg
, "--unpacked=") ||
1706 !strcmp("--reflog", arg
) ||
1707 !strcmp("--all", arg
)) {
1708 use_internal_rev_list
= 1;
1709 if (rp_ac
>= rp_ac_alloc
- 1) {
1710 rp_ac_alloc
= alloc_nr(rp_ac_alloc
);
1711 rp_av
= xrealloc(rp_av
,
1712 rp_ac_alloc
* sizeof(*rp_av
));
1714 rp_av
[rp_ac
++] = arg
;
1717 if (!strcmp("--thin", arg
)) {
1718 use_internal_rev_list
= 1;
1720 rp_av
[1] = "--objects-edge";
1723 if (!prefixcmp(arg
, "--index-version=")) {
1725 index_default_version
= strtoul(arg
+ 16, &c
, 10);
1726 if (index_default_version
> 2)
1729 index_off32_limit
= strtoul(c
+1, &c
, 0);
1730 if (*c
|| index_off32_limit
& 0x80000000)
1737 /* Traditionally "pack-objects [options] base extra" failed;
1738 * we would however want to take refs parameter that would
1739 * have been given to upstream rev-list ourselves, which means
1740 * we somehow want to say what the base name is. So the
1743 * pack-objects [options] base <refs...>
1745 * in other words, we would treat the first non-option as the
1746 * base_name and send everything else to the internal revision
1750 if (!pack_to_stdout
)
1751 base_name
= argv
[i
++];
1753 if (pack_to_stdout
!= !base_name
)
1756 if (!pack_to_stdout
&& thin
)
1757 die("--thin cannot be used to build an indexable pack.");
1759 prepare_packed_git();
1762 fprintf(stderr
, "Generating pack...\n");
1763 setup_progress_signal();
1766 if (!use_internal_rev_list
)
1767 read_object_list_from_stdin();
1769 rp_av
[rp_ac
] = NULL
;
1770 get_object_list(rp_ac
, rp_av
);
1774 fprintf(stderr
, "Done counting %u objects.\n", nr_objects
);
1775 if (non_empty
&& !nr_result
)
1777 if (progress
&& (nr_objects
!= nr_result
))
1778 fprintf(stderr
, "Result has %u objects.\n", nr_result
);
1780 prepare_pack(window
, depth
);
1781 if (progress
== 1 && pack_to_stdout
) {
1782 /* the other end usually displays progress itself */
1783 struct itimerval v
= {{0,},};
1784 setitimer(ITIMER_REAL
, &v
, NULL
);
1785 signal(SIGALRM
, SIG_IGN
);
1786 progress_update
= 0;
1788 last_obj_offset
= write_pack_file();
1789 if (!pack_to_stdout
) {
1790 unsigned char object_list_sha1
[20];
1791 write_index_file(last_obj_offset
, object_list_sha1
);
1792 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.pack",
1793 base_name
, sha1_to_hex(object_list_sha1
));
1794 if (rename(pack_tmp_name
, tmpname
))
1795 die("unable to rename temporary pack file: %s",
1797 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.idx",
1798 base_name
, sha1_to_hex(object_list_sha1
));
1799 if (rename(idx_tmp_name
, tmpname
))
1800 die("unable to rename temporary index file: %s",
1802 puts(sha1_to_hex(object_list_sha1
));
1805 fprintf(stderr
, "Total %u (delta %u), reused %u (delta %u)\n",
1806 written
, written_delta
, reused
, reused_delta
);