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 static struct object_entry
*objects
;
54 static uint32_t nr_objects
, nr_alloc
, nr_result
;
57 static int no_reuse_delta
;
59 static int incremental
;
60 static int allow_ofs_delta
;
61 static const char *pack_tmp_name
, *idx_tmp_name
;
62 static char tmpname
[PATH_MAX
];
63 static unsigned char pack_file_sha1
[20];
64 static int progress
= 1;
65 static volatile sig_atomic_t progress_update
;
66 static int window
= 10;
67 static int pack_to_stdout
;
68 static int num_preferred_base
;
71 * The object names in objects array are hashed with this hashtable,
72 * to help looking up the entry by object name.
73 * This hashtable is built after all the objects are seen.
75 static int *object_ix
;
76 static int object_ix_hashsz
;
79 * Pack index for existing packs give us easy access to the offsets into
80 * corresponding pack file where each object's data starts, but the entries
81 * do not store the size of the compressed representation (uncompressed
82 * size is easily available by examining the pack entry header). It is
83 * also rather expensive to find the sha1 for an object given its offset.
85 * We build a hashtable of existing packs (pack_revindex), and keep reverse
86 * index here -- pack index file is sorted by object name mapping to offset;
87 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
88 * ordered by offset, so if you know the offset of an object, next offset
89 * is where its packed representation ends and the index_nr can be used to
90 * get the object sha1 from the main index.
92 struct revindex_entry
{
96 struct pack_revindex
{
98 struct revindex_entry
*revindex
;
100 static struct pack_revindex
*pack_revindex
;
101 static int pack_revindex_hashsz
;
106 static uint32_t written
, written_delta
;
107 static uint32_t reused
, reused_delta
;
109 static int pack_revindex_ix(struct packed_git
*p
)
111 unsigned long ui
= (unsigned long)p
;
114 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
115 i
= (int)(ui
% pack_revindex_hashsz
);
116 while (pack_revindex
[i
].p
) {
117 if (pack_revindex
[i
].p
== p
)
119 if (++i
== pack_revindex_hashsz
)
125 static void prepare_pack_ix(void)
128 struct packed_git
*p
;
129 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
133 pack_revindex_hashsz
= num
* 11;
134 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
135 for (p
= packed_git
; p
; p
= p
->next
) {
136 num
= pack_revindex_ix(p
);
138 pack_revindex
[num
].p
= p
;
140 /* revindex elements are lazily initialized */
143 static int cmp_offset(const void *a_
, const void *b_
)
145 const struct revindex_entry
*a
= a_
;
146 const struct revindex_entry
*b
= b_
;
147 return (a
->offset
< b
->offset
) ? -1 : (a
->offset
> b
->offset
) ? 1 : 0;
151 * Ordered list of offsets of objects in the pack.
153 static void prepare_pack_revindex(struct pack_revindex
*rix
)
155 struct packed_git
*p
= rix
->p
;
156 int num_ent
= p
->num_objects
;
158 const char *index
= p
->index_data
;
160 rix
->revindex
= xmalloc(sizeof(*rix
->revindex
) * (num_ent
+ 1));
163 if (p
->index_version
> 1) {
164 const uint32_t *off_32
=
165 (uint32_t *)(index
+ 8 + p
->num_objects
* (20 + 4));
166 const uint32_t *off_64
= off_32
+ p
->num_objects
;
167 for (i
= 0; i
< num_ent
; i
++) {
168 uint32_t off
= ntohl(*off_32
++);
169 if (!(off
& 0x80000000)) {
170 rix
->revindex
[i
].offset
= off
;
172 rix
->revindex
[i
].offset
=
173 ((uint64_t)ntohl(*off_64
++)) << 32;
174 rix
->revindex
[i
].offset
|=
177 rix
->revindex
[i
].nr
= i
;
180 for (i
= 0; i
< num_ent
; i
++) {
181 uint32_t hl
= *((uint32_t *)(index
+ 24 * i
));
182 rix
->revindex
[i
].offset
= ntohl(hl
);
183 rix
->revindex
[i
].nr
= i
;
187 /* This knows the pack format -- the 20-byte trailer
188 * follows immediately after the last object data.
190 rix
->revindex
[num_ent
].offset
= p
->pack_size
- 20;
191 rix
->revindex
[num_ent
].nr
= -1;
192 qsort(rix
->revindex
, num_ent
, sizeof(*rix
->revindex
), cmp_offset
);
195 static struct revindex_entry
* find_packed_object(struct packed_git
*p
,
200 struct pack_revindex
*rix
;
201 struct revindex_entry
*revindex
;
202 num
= pack_revindex_ix(p
);
204 die("internal error: pack revindex uninitialized");
205 rix
= &pack_revindex
[num
];
207 prepare_pack_revindex(rix
);
208 revindex
= rix
->revindex
;
210 hi
= p
->num_objects
+ 1;
212 int mi
= (lo
+ hi
) / 2;
213 if (revindex
[mi
].offset
== ofs
) {
214 return revindex
+ mi
;
216 else if (ofs
< revindex
[mi
].offset
)
221 die("internal error: pack revindex corrupt");
224 static const unsigned char *find_packed_object_name(struct packed_git
*p
,
227 struct revindex_entry
*entry
= find_packed_object(p
, ofs
);
228 return nth_packed_object_sha1(p
, entry
->nr
);
231 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
233 unsigned long othersize
, delta_size
;
234 enum object_type type
;
235 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, &type
, &othersize
);
239 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
240 delta_buf
= diff_delta(otherbuf
, othersize
,
241 buf
, size
, &delta_size
, 0);
242 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
243 die("delta size changed");
250 * The per-object header is a pretty dense thing, which is
251 * - first byte: low four bits are "size", then three bits of "type",
252 * and the high bit is "size continues".
253 * - each byte afterwards: low seven bits are size continuation,
254 * with the high bit being "size continues"
256 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
261 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
262 die("bad type %d", type
);
264 c
= (type
<< 4) | (size
& 15);
277 * we are going to reuse the existing object data as is. make
278 * sure it is not corrupt.
280 static int check_pack_inflate(struct packed_git
*p
,
281 struct pack_window
**w_curs
,
284 unsigned long expect
)
287 unsigned char fakebuf
[4096], *in
;
290 memset(&stream
, 0, sizeof(stream
));
291 inflateInit(&stream
);
293 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
295 stream
.next_out
= fakebuf
;
296 stream
.avail_out
= sizeof(fakebuf
);
297 st
= inflate(&stream
, Z_FINISH
);
298 offset
+= stream
.next_in
- in
;
299 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
301 return (st
== Z_STREAM_END
&&
302 stream
.total_out
== expect
&&
303 stream
.total_in
== len
) ? 0 : -1;
306 static int check_pack_crc(struct packed_git
*p
, struct pack_window
**w_curs
,
307 off_t offset
, off_t len
, unsigned int nr
)
309 const uint32_t *index_crc
;
310 uint32_t data_crc
= crc32(0, Z_NULL
, 0);
314 void *data
= use_pack(p
, w_curs
, offset
, &avail
);
317 data_crc
= crc32(data_crc
, data
, avail
);
322 index_crc
= p
->index_data
;
323 index_crc
+= 2 + 256 + p
->num_objects
* (20/4) + nr
;
325 return data_crc
!= ntohl(*index_crc
);
328 static void copy_pack_data(struct sha1file
*f
,
329 struct packed_git
*p
,
330 struct pack_window
**w_curs
,
338 in
= use_pack(p
, w_curs
, offset
, &avail
);
340 avail
= (unsigned int)len
;
341 sha1write(f
, in
, avail
);
347 static int check_loose_inflate(unsigned char *data
, unsigned long len
, unsigned long expect
)
350 unsigned char fakebuf
[4096];
353 memset(&stream
, 0, sizeof(stream
));
354 stream
.next_in
= data
;
355 stream
.avail_in
= len
;
356 stream
.next_out
= fakebuf
;
357 stream
.avail_out
= sizeof(fakebuf
);
358 inflateInit(&stream
);
361 st
= inflate(&stream
, Z_FINISH
);
362 if (st
== Z_STREAM_END
|| st
== Z_OK
) {
363 st
= (stream
.total_out
== expect
&&
364 stream
.total_in
== len
) ? 0 : -1;
367 if (st
!= Z_BUF_ERROR
) {
371 stream
.next_out
= fakebuf
;
372 stream
.avail_out
= sizeof(fakebuf
);
378 static int revalidate_loose_object(struct object_entry
*entry
,
380 unsigned long mapsize
)
382 /* we already know this is a loose object with new type header. */
383 enum object_type type
;
384 unsigned long size
, used
;
389 used
= unpack_object_header_gently(map
, mapsize
, &type
, &size
);
394 return check_loose_inflate(map
, mapsize
, size
);
397 static unsigned long write_object(struct sha1file
*f
,
398 struct object_entry
*entry
)
401 enum object_type type
;
403 unsigned char header
[10];
406 enum object_type obj_type
;
412 obj_type
= entry
->type
;
413 if (! entry
->in_pack
)
414 to_reuse
= 0; /* can't reuse what we don't have */
415 else if (obj_type
== OBJ_REF_DELTA
|| obj_type
== OBJ_OFS_DELTA
)
416 to_reuse
= 1; /* check_object() decided it for us */
417 else if (obj_type
!= entry
->in_pack_type
)
418 to_reuse
= 0; /* pack has delta which is unusable */
419 else if (entry
->delta
)
420 to_reuse
= 0; /* we want to pack afresh */
422 to_reuse
= 1; /* we have it in-pack undeltified,
423 * and we do not need to deltify it.
426 if (!entry
->in_pack
&& !entry
->delta
) {
428 unsigned long mapsize
;
429 map
= map_sha1_file(entry
->sha1
, &mapsize
);
430 if (map
&& !legacy_loose_object(map
)) {
431 /* We can copy straight into the pack file */
432 if (revalidate_loose_object(entry
, map
, mapsize
))
433 die("corrupt loose object %s",
434 sha1_to_hex(entry
->sha1
));
435 sha1write(f
, map
, mapsize
);
436 munmap(map
, mapsize
);
442 munmap(map
, mapsize
);
446 buf
= read_sha1_file(entry
->sha1
, &type
, &size
);
448 die("unable to read %s", sha1_to_hex(entry
->sha1
));
449 if (size
!= entry
->size
)
450 die("object %s size inconsistency (%lu vs %lu)",
451 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
453 buf
= delta_against(buf
, size
, entry
);
454 size
= entry
->delta_size
;
455 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
456 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
459 * The object header is a byte of 'type' followed by zero or
460 * more bytes of length.
462 hdrlen
= encode_header(obj_type
, size
, header
);
463 sha1write(f
, header
, hdrlen
);
465 if (obj_type
== OBJ_OFS_DELTA
) {
467 * Deltas with relative base contain an additional
468 * encoding of the relative offset for the delta
469 * base from this object's position in the pack.
471 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
472 unsigned pos
= sizeof(header
) - 1;
473 header
[pos
] = ofs
& 127;
475 header
[--pos
] = 128 | (--ofs
& 127);
476 sha1write(f
, header
+ pos
, sizeof(header
) - pos
);
477 hdrlen
+= sizeof(header
) - pos
;
478 } else if (obj_type
== OBJ_REF_DELTA
) {
480 * Deltas with a base reference contain
481 * an additional 20 bytes for the base sha1.
483 sha1write(f
, entry
->delta
->sha1
, 20);
486 datalen
= sha1write_compressed(f
, buf
, size
);
490 struct packed_git
*p
= entry
->in_pack
;
491 struct pack_window
*w_curs
= NULL
;
492 struct revindex_entry
*revidx
;
496 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
497 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
500 hdrlen
= encode_header(obj_type
, entry
->size
, header
);
501 sha1write(f
, header
, hdrlen
);
502 if (obj_type
== OBJ_OFS_DELTA
) {
503 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
504 unsigned pos
= sizeof(header
) - 1;
505 header
[pos
] = ofs
& 127;
507 header
[--pos
] = 128 | (--ofs
& 127);
508 sha1write(f
, header
+ pos
, sizeof(header
) - pos
);
509 hdrlen
+= sizeof(header
) - pos
;
510 } else if (obj_type
== OBJ_REF_DELTA
) {
511 sha1write(f
, entry
->delta
->sha1
, 20);
515 offset
= entry
->in_pack_offset
;
516 revidx
= find_packed_object(p
, offset
);
517 datalen
= revidx
[1].offset
- offset
;
518 if (!pack_to_stdout
&& p
->index_version
> 1 &&
519 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
))
520 die("bad packed object CRC for %s", sha1_to_hex(entry
->sha1
));
521 offset
+= entry
->in_pack_header_size
;
522 datalen
-= entry
->in_pack_header_size
;
523 if (!pack_to_stdout
&& p
->index_version
== 1 &&
524 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
))
525 die("corrupt packed object for %s", sha1_to_hex(entry
->sha1
));
526 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
534 entry
->crc32
= crc32_end(f
);
535 return hdrlen
+ datalen
;
538 static off_t
write_one(struct sha1file
*f
,
539 struct object_entry
*e
,
544 /* offset is non zero if object is written already. */
545 if (e
->offset
|| e
->preferred_base
)
548 /* if we are deltified, write out base object first. */
550 offset
= write_one(f
, e
->delta
, offset
);
553 size
= write_object(f
, e
);
555 /* make sure off_t is sufficiently large not to wrap */
556 if (offset
> offset
+ size
)
557 die("pack too large for current definition of off_t");
558 return offset
+ size
;
561 static off_t
write_pack_file(void)
565 off_t offset
, last_obj_offset
= 0;
566 struct pack_header hdr
;
567 unsigned last_percent
= 999;
568 int do_progress
= progress
;
570 if (pack_to_stdout
) {
571 f
= sha1fd(1, "<stdout>");
575 snprintf(tmpname
, sizeof(tmpname
), "tmp_pack_XXXXXX");
576 fd
= mkstemp(tmpname
);
578 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
579 pack_tmp_name
= xstrdup(tmpname
);
580 f
= sha1fd(fd
, pack_tmp_name
);
584 fprintf(stderr
, "Writing %u objects.\n", nr_result
);
586 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
587 hdr
.hdr_version
= htonl(PACK_VERSION
);
588 hdr
.hdr_entries
= htonl(nr_result
);
589 sha1write(f
, &hdr
, sizeof(hdr
));
590 offset
= sizeof(hdr
);
593 for (i
= 0; i
< nr_objects
; i
++) {
594 last_obj_offset
= offset
;
595 offset
= write_one(f
, objects
+ i
, offset
);
597 unsigned percent
= written
* 100 / nr_result
;
598 if (progress_update
|| percent
!= last_percent
) {
599 fprintf(stderr
, "%4u%% (%u/%u) done\r",
600 percent
, written
, nr_result
);
602 last_percent
= percent
;
609 if (written
!= nr_result
)
610 die("wrote %u objects while expecting %u", written
, nr_result
);
611 sha1close(f
, pack_file_sha1
, 1);
613 return last_obj_offset
;
616 static int sha1_sort(const void *_a
, const void *_b
)
618 const struct object_entry
*a
= *(struct object_entry
**)_a
;
619 const struct object_entry
*b
= *(struct object_entry
**)_b
;
620 return hashcmp(a
->sha1
, b
->sha1
);
623 static uint32_t index_default_version
= 1;
624 static uint32_t index_off32_limit
= 0x7fffffff;
626 static void write_index_file(off_t last_obj_offset
, unsigned char *sha1
)
629 struct object_entry
**sorted_by_sha
, **list
, **last
;
631 uint32_t i
, index_version
;
635 snprintf(tmpname
, sizeof(tmpname
), "tmp_idx_XXXXXX");
636 fd
= mkstemp(tmpname
);
638 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
639 idx_tmp_name
= xstrdup(tmpname
);
640 f
= sha1fd(fd
, idx_tmp_name
);
645 xcalloc(nr_result
, sizeof(struct object_entry
*));
646 for (i
= 0; i
< nr_objects
; i
++)
647 if (!objects
[i
].preferred_base
)
648 sorted_by_sha
[j
++] = objects
+ i
;
650 die("listed %u objects while expecting %u", j
, nr_result
);
651 qsort(sorted_by_sha
, nr_result
, sizeof(*sorted_by_sha
), sha1_sort
);
652 list
= sorted_by_sha
;
653 last
= sorted_by_sha
+ nr_result
;
655 sorted_by_sha
= list
= last
= NULL
;
657 /* if last object's offset is >= 2^31 we should use index V2 */
658 index_version
= (last_obj_offset
>> 31) ? 2 : index_default_version
;
660 /* index versions 2 and above need a header */
661 if (index_version
>= 2) {
662 struct pack_idx_header hdr
;
663 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
664 hdr
.idx_version
= htonl(index_version
);
665 sha1write(f
, &hdr
, sizeof(hdr
));
669 * Write the first-level table (the list is sorted,
670 * but we use a 256-entry lookup to be able to avoid
671 * having to do eight extra binary search iterations).
673 for (i
= 0; i
< 256; i
++) {
674 struct object_entry
**next
= list
;
675 while (next
< last
) {
676 struct object_entry
*entry
= *next
;
677 if (entry
->sha1
[0] != i
)
681 array
[i
] = htonl(next
- sorted_by_sha
);
684 sha1write(f
, array
, 256 * 4);
686 /* Compute the SHA1 hash of sorted object names. */
689 /* Write the actual SHA1 entries. */
690 list
= sorted_by_sha
;
691 for (i
= 0; i
< nr_result
; i
++) {
692 struct object_entry
*entry
= *list
++;
693 if (index_version
< 2) {
694 uint32_t offset
= htonl(entry
->offset
);
695 sha1write(f
, &offset
, 4);
697 sha1write(f
, entry
->sha1
, 20);
698 SHA1_Update(&ctx
, entry
->sha1
, 20);
701 if (index_version
>= 2) {
702 unsigned int nr_large_offset
= 0;
704 /* write the crc32 table */
705 list
= sorted_by_sha
;
706 for (i
= 0; i
< nr_objects
; i
++) {
707 struct object_entry
*entry
= *list
++;
708 uint32_t crc32_val
= htonl(entry
->crc32
);
709 sha1write(f
, &crc32_val
, 4);
712 /* write the 32-bit offset table */
713 list
= sorted_by_sha
;
714 for (i
= 0; i
< nr_objects
; i
++) {
715 struct object_entry
*entry
= *list
++;
716 uint32_t offset
= (entry
->offset
<= index_off32_limit
) ?
717 entry
->offset
: (0x80000000 | nr_large_offset
++);
718 offset
= htonl(offset
);
719 sha1write(f
, &offset
, 4);
722 /* write the large offset table */
723 list
= sorted_by_sha
;
724 while (nr_large_offset
) {
725 struct object_entry
*entry
= *list
++;
726 uint64_t offset
= entry
->offset
;
727 if (offset
> index_off32_limit
) {
729 split
[0] = htonl(offset
>> 32);
730 split
[1] = htonl(offset
& 0xffffffff);
731 sha1write(f
, split
, 8);
737 sha1write(f
, pack_file_sha1
, 20);
738 sha1close(f
, NULL
, 1);
740 SHA1_Final(sha1
, &ctx
);
743 static int locate_object_entry_hash(const unsigned char *sha1
)
747 memcpy(&ui
, sha1
, sizeof(unsigned int));
748 i
= ui
% object_ix_hashsz
;
749 while (0 < object_ix
[i
]) {
750 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].sha1
))
752 if (++i
== object_ix_hashsz
)
758 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
762 if (!object_ix_hashsz
)
765 i
= locate_object_entry_hash(sha1
);
767 return &objects
[object_ix
[i
]-1];
771 static void rehash_objects(void)
774 struct object_entry
*oe
;
776 object_ix_hashsz
= nr_objects
* 3;
777 if (object_ix_hashsz
< 1024)
778 object_ix_hashsz
= 1024;
779 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
780 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
781 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
782 int ix
= locate_object_entry_hash(oe
->sha1
);
786 object_ix
[ix
] = i
+ 1;
790 static unsigned name_hash(const char *name
)
796 * This effectively just creates a sortable number from the
797 * last sixteen non-whitespace characters. Last characters
798 * count "most", so things that end in ".c" sort together.
800 while ((c
= *name
++) != 0) {
803 hash
= (hash
>> 2) + (c
<< 24);
808 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
809 unsigned hash
, int exclude
)
811 struct object_entry
*entry
;
812 struct packed_git
*p
, *found_pack
= NULL
;
813 off_t found_offset
= 0;
816 ix
= nr_objects
? locate_object_entry_hash(sha1
) : -1;
819 entry
= objects
+ object_ix
[ix
] - 1;
820 if (!entry
->preferred_base
)
822 entry
->preferred_base
= 1;
827 for (p
= packed_git
; p
; p
= p
->next
) {
828 off_t offset
= find_pack_entry_one(sha1
, p
);
831 found_offset
= offset
;
838 if (local
&& !p
->pack_local
)
843 if (nr_objects
>= nr_alloc
) {
844 nr_alloc
= (nr_alloc
+ 1024) * 3 / 2;
845 objects
= xrealloc(objects
, nr_alloc
* sizeof(*entry
));
848 entry
= objects
+ nr_objects
++;
849 memset(entry
, 0, sizeof(*entry
));
850 hashcpy(entry
->sha1
, sha1
);
855 entry
->preferred_base
= 1;
859 entry
->in_pack
= found_pack
;
860 entry
->in_pack_offset
= found_offset
;
863 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
866 object_ix
[-1 - ix
] = nr_objects
;
868 if (progress_update
) {
869 fprintf(stderr
, "Counting objects...%u\r", nr_objects
);
876 struct pbase_tree_cache
{
877 unsigned char sha1
[20];
881 unsigned long tree_size
;
884 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
885 static int pbase_tree_cache_ix(const unsigned char *sha1
)
887 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
889 static int pbase_tree_cache_ix_incr(int ix
)
891 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
894 static struct pbase_tree
{
895 struct pbase_tree
*next
;
896 /* This is a phony "cache" entry; we are not
897 * going to evict it nor find it through _get()
898 * mechanism -- this is for the toplevel node that
899 * would almost always change with any commit.
901 struct pbase_tree_cache pcache
;
904 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
906 struct pbase_tree_cache
*ent
, *nent
;
909 enum object_type type
;
911 int my_ix
= pbase_tree_cache_ix(sha1
);
912 int available_ix
= -1;
914 /* pbase-tree-cache acts as a limited hashtable.
915 * your object will be found at your index or within a few
916 * slots after that slot if it is cached.
918 for (neigh
= 0; neigh
< 8; neigh
++) {
919 ent
= pbase_tree_cache
[my_ix
];
920 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
924 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
925 ((0 <= available_ix
) &&
926 (!ent
&& pbase_tree_cache
[available_ix
])))
927 available_ix
= my_ix
;
930 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
933 /* Did not find one. Either we got a bogus request or
934 * we need to read and perhaps cache.
936 data
= read_sha1_file(sha1
, &type
, &size
);
939 if (type
!= OBJ_TREE
) {
944 /* We need to either cache or return a throwaway copy */
946 if (available_ix
< 0)
949 ent
= pbase_tree_cache
[available_ix
];
950 my_ix
= available_ix
;
954 nent
= xmalloc(sizeof(*nent
));
955 nent
->temporary
= (available_ix
< 0);
958 /* evict and reuse */
959 free(ent
->tree_data
);
962 hashcpy(nent
->sha1
, sha1
);
963 nent
->tree_data
= data
;
964 nent
->tree_size
= size
;
966 if (!nent
->temporary
)
967 pbase_tree_cache
[my_ix
] = nent
;
971 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
973 if (!cache
->temporary
) {
977 free(cache
->tree_data
);
981 static int name_cmp_len(const char *name
)
984 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
989 static void add_pbase_object(struct tree_desc
*tree
,
992 const char *fullname
)
994 struct name_entry entry
;
997 while (tree_entry(tree
,&entry
)) {
998 cmp
= tree_entry_len(entry
.path
, entry
.sha1
) != cmplen
? 1 :
999 memcmp(name
, entry
.path
, cmplen
);
1004 if (name
[cmplen
] != '/') {
1005 unsigned hash
= name_hash(fullname
);
1006 add_object_entry(entry
.sha1
,
1007 S_ISDIR(entry
.mode
) ? OBJ_TREE
: OBJ_BLOB
,
1011 if (S_ISDIR(entry
.mode
)) {
1012 struct tree_desc sub
;
1013 struct pbase_tree_cache
*tree
;
1014 const char *down
= name
+cmplen
+1;
1015 int downlen
= name_cmp_len(down
);
1017 tree
= pbase_tree_get(entry
.sha1
);
1020 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1022 add_pbase_object(&sub
, down
, downlen
, fullname
);
1023 pbase_tree_put(tree
);
1028 static unsigned *done_pbase_paths
;
1029 static int done_pbase_paths_num
;
1030 static int done_pbase_paths_alloc
;
1031 static int done_pbase_path_pos(unsigned hash
)
1034 int hi
= done_pbase_paths_num
;
1036 int mi
= (hi
+ lo
) / 2;
1037 if (done_pbase_paths
[mi
] == hash
)
1039 if (done_pbase_paths
[mi
] < hash
)
1047 static int check_pbase_path(unsigned hash
)
1049 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1053 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1054 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1055 done_pbase_paths
= xrealloc(done_pbase_paths
,
1056 done_pbase_paths_alloc
*
1059 done_pbase_paths_num
++;
1060 if (pos
< done_pbase_paths_num
)
1061 memmove(done_pbase_paths
+ pos
+ 1,
1062 done_pbase_paths
+ pos
,
1063 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1064 done_pbase_paths
[pos
] = hash
;
1068 static void add_preferred_base_object(const char *name
, unsigned hash
)
1070 struct pbase_tree
*it
;
1073 if (!num_preferred_base
|| check_pbase_path(hash
))
1076 cmplen
= name_cmp_len(name
);
1077 for (it
= pbase_tree
; it
; it
= it
->next
) {
1079 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, 0, 1);
1082 struct tree_desc tree
;
1083 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1084 add_pbase_object(&tree
, name
, cmplen
, name
);
1089 static void add_preferred_base(unsigned char *sha1
)
1091 struct pbase_tree
*it
;
1094 unsigned char tree_sha1
[20];
1096 if (window
<= num_preferred_base
++)
1099 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1103 for (it
= pbase_tree
; it
; it
= it
->next
) {
1104 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1110 it
= xcalloc(1, sizeof(*it
));
1111 it
->next
= pbase_tree
;
1114 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1115 it
->pcache
.tree_data
= data
;
1116 it
->pcache
.tree_size
= size
;
1119 static void check_object(struct object_entry
*entry
)
1121 if (entry
->in_pack
) {
1122 struct packed_git
*p
= entry
->in_pack
;
1123 struct pack_window
*w_curs
= NULL
;
1124 const unsigned char *base_ref
= NULL
;
1125 struct object_entry
*base_entry
;
1126 unsigned long used
, used_0
;
1129 unsigned char *buf
, c
;
1131 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1134 * We want in_pack_type even if we do not reuse delta.
1135 * There is no point not reusing non-delta representations.
1137 used
= unpack_object_header_gently(buf
, avail
,
1138 &entry
->in_pack_type
,
1142 * Determine if this is a delta and if so whether we can
1143 * reuse it or not. Otherwise let's find out as cheaply as
1144 * possible what the actual type and size for this object is.
1146 switch (entry
->in_pack_type
) {
1148 /* Not a delta hence we've already got all we need. */
1149 entry
->type
= entry
->in_pack_type
;
1150 entry
->in_pack_header_size
= used
;
1151 unuse_pack(&w_curs
);
1154 if (!no_reuse_delta
&& !entry
->preferred_base
)
1155 base_ref
= use_pack(p
, &w_curs
,
1156 entry
->in_pack_offset
+ used
, NULL
);
1157 entry
->in_pack_header_size
= used
+ 20;
1160 buf
= use_pack(p
, &w_curs
,
1161 entry
->in_pack_offset
+ used
, NULL
);
1167 if (!ofs
|| MSB(ofs
, 7))
1168 die("delta base offset overflow in pack for %s",
1169 sha1_to_hex(entry
->sha1
));
1171 ofs
= (ofs
<< 7) + (c
& 127);
1173 if (ofs
>= entry
->in_pack_offset
)
1174 die("delta base offset out of bound for %s",
1175 sha1_to_hex(entry
->sha1
));
1176 ofs
= entry
->in_pack_offset
- ofs
;
1177 if (!no_reuse_delta
&& !entry
->preferred_base
)
1178 base_ref
= find_packed_object_name(p
, ofs
);
1179 entry
->in_pack_header_size
= used
+ used_0
;
1183 if (base_ref
&& (base_entry
= locate_object_entry(base_ref
))) {
1185 * If base_ref was set above that means we wish to
1186 * reuse delta data, and we even found that base
1187 * in the list of objects we want to pack. Goodie!
1189 * Depth value does not matter - find_deltas() will
1190 * never consider reused delta as the base object to
1191 * deltify other objects against, in order to avoid
1194 entry
->type
= entry
->in_pack_type
;
1195 entry
->delta
= base_entry
;
1196 entry
->delta_sibling
= base_entry
->delta_child
;
1197 base_entry
->delta_child
= entry
;
1198 unuse_pack(&w_curs
);
1204 * This must be a delta and we already know what the
1205 * final object type is. Let's extract the actual
1206 * object size from the delta header.
1208 entry
->size
= get_size_from_delta(p
, &w_curs
,
1209 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1210 unuse_pack(&w_curs
);
1215 * No choice but to fall back to the recursive delta walk
1216 * with sha1_object_info() to find about the object type
1219 unuse_pack(&w_curs
);
1222 entry
->type
= sha1_object_info(entry
->sha1
, &entry
->size
);
1223 if (entry
->type
< 0)
1224 die("unable to get type of object %s",
1225 sha1_to_hex(entry
->sha1
));
1228 static int pack_offset_sort(const void *_a
, const void *_b
)
1230 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1231 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1233 /* avoid filesystem trashing with loose objects */
1234 if (!a
->in_pack
&& !b
->in_pack
)
1235 return hashcmp(a
->sha1
, b
->sha1
);
1237 if (a
->in_pack
< b
->in_pack
)
1239 if (a
->in_pack
> b
->in_pack
)
1241 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1242 (a
->in_pack_offset
> b
->in_pack_offset
);
1245 static void get_object_details(void)
1248 struct object_entry
**sorted_by_offset
;
1250 sorted_by_offset
= xcalloc(nr_objects
, sizeof(struct object_entry
*));
1251 for (i
= 0; i
< nr_objects
; i
++)
1252 sorted_by_offset
[i
] = objects
+ i
;
1253 qsort(sorted_by_offset
, nr_objects
, sizeof(*sorted_by_offset
), pack_offset_sort
);
1256 for (i
= 0; i
< nr_objects
; i
++)
1257 check_object(sorted_by_offset
[i
]);
1258 free(sorted_by_offset
);
1261 static int type_size_sort(const void *_a
, const void *_b
)
1263 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1264 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1266 if (a
->type
< b
->type
)
1268 if (a
->type
> b
->type
)
1270 if (a
->hash
< b
->hash
)
1272 if (a
->hash
> b
->hash
)
1274 if (a
->preferred_base
< b
->preferred_base
)
1276 if (a
->preferred_base
> b
->preferred_base
)
1278 if (a
->size
< b
->size
)
1280 if (a
->size
> b
->size
)
1282 return a
> b
? -1 : (a
< b
); /* newest last */
1286 struct object_entry
*entry
;
1288 struct delta_index
*index
;
1292 * We search for deltas _backwards_ in a list sorted by type and
1293 * by size, so that we see progressively smaller and smaller files.
1294 * That's because we prefer deltas to be from the bigger file
1295 * to the smaller - deletes are potentially cheaper, but perhaps
1296 * more importantly, the bigger file is likely the more recent
1299 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1302 struct object_entry
*trg_entry
= trg
->entry
;
1303 struct object_entry
*src_entry
= src
->entry
;
1304 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1305 enum object_type type
;
1308 /* Don't bother doing diffs between different types */
1309 if (trg_entry
->type
!= src_entry
->type
)
1312 /* We do not compute delta to *create* objects we are not
1315 if (trg_entry
->preferred_base
)
1319 * We do not bother to try a delta that we discarded
1320 * on an earlier try, but only when reusing delta data.
1322 if (!no_reuse_delta
&& trg_entry
->in_pack
&&
1323 trg_entry
->in_pack
== src_entry
->in_pack
&&
1324 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1325 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1328 /* Let's not bust the allowed depth. */
1329 if (src_entry
->depth
>= max_depth
)
1332 /* Now some size filtering heuristics. */
1333 trg_size
= trg_entry
->size
;
1334 max_size
= trg_size
/2 - 20;
1335 max_size
= max_size
* (max_depth
- src_entry
->depth
) / max_depth
;
1338 if (trg_entry
->delta
&& trg_entry
->delta_size
<= max_size
)
1339 max_size
= trg_entry
->delta_size
-1;
1340 src_size
= src_entry
->size
;
1341 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1342 if (sizediff
>= max_size
)
1345 /* Load data if not already done */
1347 trg
->data
= read_sha1_file(trg_entry
->sha1
, &type
, &sz
);
1349 die("object %s inconsistent object length (%lu vs %lu)",
1350 sha1_to_hex(trg_entry
->sha1
), sz
, trg_size
);
1353 src
->data
= read_sha1_file(src_entry
->sha1
, &type
, &sz
);
1355 die("object %s inconsistent object length (%lu vs %lu)",
1356 sha1_to_hex(src_entry
->sha1
), sz
, src_size
);
1359 src
->index
= create_delta_index(src
->data
, src_size
);
1361 die("out of memory");
1364 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1368 trg_entry
->delta
= src_entry
;
1369 trg_entry
->delta_size
= delta_size
;
1370 trg_entry
->depth
= src_entry
->depth
+ 1;
1375 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1377 struct object_entry
*child
= me
->delta_child
;
1380 unsigned int c
= check_delta_limit(child
, n
+ 1);
1383 child
= child
->delta_sibling
;
1388 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
1390 uint32_t i
= nr_objects
, idx
= 0, processed
= 0;
1391 unsigned int array_size
= window
* sizeof(struct unpacked
);
1392 struct unpacked
*array
;
1393 unsigned last_percent
= 999;
1398 array
= xmalloc(array_size
);
1399 memset(array
, 0, array_size
);
1401 fprintf(stderr
, "Deltifying %u objects.\n", nr_result
);
1404 struct object_entry
*entry
= list
[--i
];
1405 struct unpacked
*n
= array
+ idx
;
1408 if (!entry
->preferred_base
)
1412 unsigned percent
= processed
* 100 / nr_result
;
1413 if (percent
!= last_percent
|| progress_update
) {
1414 fprintf(stderr
, "%4u%% (%u/%u) done\r",
1415 percent
, processed
, nr_result
);
1416 progress_update
= 0;
1417 last_percent
= percent
;
1422 /* This happens if we decided to reuse existing
1423 * delta from a pack. "!no_reuse_delta &&" is implied.
1427 if (entry
->size
< 50)
1429 free_delta_index(n
->index
);
1436 * If the current object is at pack edge, take the depth the
1437 * objects that depend on the current object into account
1438 * otherwise they would become too deep.
1441 if (entry
->delta_child
) {
1442 max_depth
-= check_delta_limit(entry
, 0);
1449 uint32_t other_idx
= idx
+ j
;
1451 if (other_idx
>= window
)
1452 other_idx
-= window
;
1453 m
= array
+ other_idx
;
1456 if (try_delta(n
, m
, max_depth
) < 0)
1460 /* if we made n a delta, and if n is already at max
1461 * depth, leaving it in the window is pointless. we
1462 * should evict it first.
1464 if (entry
->delta
&& depth
<= entry
->depth
)
1474 fputc('\n', stderr
);
1476 for (i
= 0; i
< window
; ++i
) {
1477 free_delta_index(array
[i
].index
);
1478 free(array
[i
].data
);
1483 static void prepare_pack(int window
, int depth
)
1485 struct object_entry
**delta_list
;
1488 get_object_details();
1490 if (!window
|| !depth
)
1493 delta_list
= xmalloc(nr_objects
* sizeof(*delta_list
));
1494 for (i
= 0; i
< nr_objects
; i
++)
1495 delta_list
[i
] = objects
+ i
;
1496 qsort(delta_list
, nr_objects
, sizeof(*delta_list
), type_size_sort
);
1497 find_deltas(delta_list
, window
+1, depth
);
1501 static void progress_interval(int signum
)
1503 progress_update
= 1;
1506 static void setup_progress_signal(void)
1508 struct sigaction sa
;
1511 memset(&sa
, 0, sizeof(sa
));
1512 sa
.sa_handler
= progress_interval
;
1513 sigemptyset(&sa
.sa_mask
);
1514 sa
.sa_flags
= SA_RESTART
;
1515 sigaction(SIGALRM
, &sa
, NULL
);
1517 v
.it_interval
.tv_sec
= 1;
1518 v
.it_interval
.tv_usec
= 0;
1519 v
.it_value
= v
.it_interval
;
1520 setitimer(ITIMER_REAL
, &v
, NULL
);
1523 static int git_pack_config(const char *k
, const char *v
)
1525 if(!strcmp(k
, "pack.window")) {
1526 window
= git_config_int(k
, v
);
1529 return git_default_config(k
, v
);
1532 static void read_object_list_from_stdin(void)
1534 char line
[40 + 1 + PATH_MAX
+ 2];
1535 unsigned char sha1
[20];
1539 if (!fgets(line
, sizeof(line
), stdin
)) {
1543 die("fgets returned NULL, not EOF, not error!");
1545 die("fgets: %s", strerror(errno
));
1549 if (line
[0] == '-') {
1550 if (get_sha1_hex(line
+1, sha1
))
1551 die("expected edge sha1, got garbage:\n %s",
1553 add_preferred_base(sha1
);
1556 if (get_sha1_hex(line
, sha1
))
1557 die("expected sha1, got garbage:\n %s", line
);
1559 hash
= name_hash(line
+41);
1560 add_preferred_base_object(line
+41, hash
);
1561 add_object_entry(sha1
, 0, hash
, 0);
1565 static void show_commit(struct commit
*commit
)
1567 add_object_entry(commit
->object
.sha1
, OBJ_COMMIT
, 0, 0);
1570 static void show_object(struct object_array_entry
*p
)
1572 unsigned hash
= name_hash(p
->name
);
1573 add_preferred_base_object(p
->name
, hash
);
1574 add_object_entry(p
->item
->sha1
, p
->item
->type
, hash
, 0);
1577 static void show_edge(struct commit
*commit
)
1579 add_preferred_base(commit
->object
.sha1
);
1582 static void get_object_list(int ac
, const char **av
)
1584 struct rev_info revs
;
1588 init_revisions(&revs
, NULL
);
1589 save_commit_buffer
= 0;
1590 track_object_refs
= 0;
1591 setup_revisions(ac
, av
, &revs
, NULL
);
1593 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
1594 int len
= strlen(line
);
1595 if (line
[len
- 1] == '\n')
1600 if (!strcmp(line
, "--not")) {
1601 flags
^= UNINTERESTING
;
1604 die("not a rev '%s'", line
);
1606 if (handle_revision_arg(line
, &revs
, flags
, 1))
1607 die("bad revision '%s'", line
);
1610 prepare_revision_walk(&revs
);
1611 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
1612 traverse_commit_list(&revs
, show_commit
, show_object
);
1615 static int adjust_perm(const char *path
, mode_t mode
)
1617 if (chmod(path
, mode
))
1619 return adjust_shared_perm(path
);
1622 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
1625 int use_internal_rev_list
= 0;
1628 off_t last_obj_offset
;
1629 const char *base_name
= NULL
;
1631 int rp_ac_alloc
= 64;
1634 rp_av
= xcalloc(rp_ac_alloc
, sizeof(*rp_av
));
1636 rp_av
[0] = "pack-objects";
1637 rp_av
[1] = "--objects"; /* --thin will make it --objects-edge */
1640 git_config(git_pack_config
);
1642 progress
= isatty(2);
1643 for (i
= 1; i
< argc
; i
++) {
1644 const char *arg
= argv
[i
];
1649 if (!strcmp("--non-empty", arg
)) {
1653 if (!strcmp("--local", arg
)) {
1657 if (!strcmp("--incremental", arg
)) {
1661 if (!prefixcmp(arg
, "--window=")) {
1663 window
= strtoul(arg
+9, &end
, 0);
1664 if (!arg
[9] || *end
)
1668 if (!prefixcmp(arg
, "--depth=")) {
1670 depth
= strtoul(arg
+8, &end
, 0);
1671 if (!arg
[8] || *end
)
1675 if (!strcmp("--progress", arg
)) {
1679 if (!strcmp("--all-progress", arg
)) {
1683 if (!strcmp("-q", arg
)) {
1687 if (!strcmp("--no-reuse-delta", arg
)) {
1691 if (!strcmp("--delta-base-offset", arg
)) {
1692 allow_ofs_delta
= 1;
1695 if (!strcmp("--stdout", arg
)) {
1699 if (!strcmp("--revs", arg
)) {
1700 use_internal_rev_list
= 1;
1703 if (!strcmp("--unpacked", arg
) ||
1704 !prefixcmp(arg
, "--unpacked=") ||
1705 !strcmp("--reflog", arg
) ||
1706 !strcmp("--all", arg
)) {
1707 use_internal_rev_list
= 1;
1708 if (rp_ac
>= rp_ac_alloc
- 1) {
1709 rp_ac_alloc
= alloc_nr(rp_ac_alloc
);
1710 rp_av
= xrealloc(rp_av
,
1711 rp_ac_alloc
* sizeof(*rp_av
));
1713 rp_av
[rp_ac
++] = arg
;
1716 if (!strcmp("--thin", arg
)) {
1717 use_internal_rev_list
= 1;
1719 rp_av
[1] = "--objects-edge";
1722 if (!prefixcmp(arg
, "--index-version=")) {
1724 index_default_version
= strtoul(arg
+ 16, &c
, 10);
1725 if (index_default_version
> 2)
1728 index_off32_limit
= strtoul(c
+1, &c
, 0);
1729 if (*c
|| index_off32_limit
& 0x80000000)
1736 /* Traditionally "pack-objects [options] base extra" failed;
1737 * we would however want to take refs parameter that would
1738 * have been given to upstream rev-list ourselves, which means
1739 * we somehow want to say what the base name is. So the
1742 * pack-objects [options] base <refs...>
1744 * in other words, we would treat the first non-option as the
1745 * base_name and send everything else to the internal revision
1749 if (!pack_to_stdout
)
1750 base_name
= argv
[i
++];
1752 if (pack_to_stdout
!= !base_name
)
1755 if (!pack_to_stdout
&& thin
)
1756 die("--thin cannot be used to build an indexable pack.");
1758 prepare_packed_git();
1761 fprintf(stderr
, "Generating pack...\n");
1762 setup_progress_signal();
1765 if (!use_internal_rev_list
)
1766 read_object_list_from_stdin();
1768 rp_av
[rp_ac
] = NULL
;
1769 get_object_list(rp_ac
, rp_av
);
1773 fprintf(stderr
, "Done counting %u objects.\n", nr_objects
);
1774 if (non_empty
&& !nr_result
)
1776 if (progress
&& (nr_objects
!= nr_result
))
1777 fprintf(stderr
, "Result has %u objects.\n", nr_result
);
1779 prepare_pack(window
, depth
);
1780 if (progress
== 1 && pack_to_stdout
) {
1781 /* the other end usually displays progress itself */
1782 struct itimerval v
= {{0,},};
1783 setitimer(ITIMER_REAL
, &v
, NULL
);
1784 signal(SIGALRM
, SIG_IGN
);
1785 progress_update
= 0;
1787 last_obj_offset
= write_pack_file();
1788 if (!pack_to_stdout
) {
1789 unsigned char object_list_sha1
[20];
1790 mode_t mode
= umask(0);
1793 mode
= 0444 & ~mode
;
1795 write_index_file(last_obj_offset
, object_list_sha1
);
1796 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.pack",
1797 base_name
, sha1_to_hex(object_list_sha1
));
1798 if (adjust_perm(pack_tmp_name
, mode
))
1799 die("unable to make temporary pack file readable: %s",
1801 if (rename(pack_tmp_name
, tmpname
))
1802 die("unable to rename temporary pack file: %s",
1804 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.idx",
1805 base_name
, sha1_to_hex(object_list_sha1
));
1806 if (adjust_perm(idx_tmp_name
, mode
))
1807 die("unable to make temporary index file readable: %s",
1809 if (rename(idx_tmp_name
, tmpname
))
1810 die("unable to rename temporary index file: %s",
1812 puts(sha1_to_hex(object_list_sha1
));
1815 fprintf(stderr
, "Total %u (delta %u), reused %u (delta %u)\n",
1816 written
, written_delta
, reused
, reused_delta
);