10 #include "csum-file.h"
11 #include "tree-walk.h"
14 #include "list-objects.h"
17 static const char pack_usage
[] = "\
18 git-pack-objects [{ -q | --progress | --all-progress }] [--max-pack-size=N] \n\
19 [--local] [--incremental] [--window=N] [--depth=N] \n\
20 [--no-reuse-delta] [--no-reuse-object] [--delta-base-offset] \n\
21 [--non-empty] [--revs [--unpacked | --all]*] [--reflog] \n\
22 [--stdout | base-name] [<ref-list | <object-list]";
25 unsigned char sha1
[20];
26 uint32_t crc32
; /* crc of raw pack data for this object */
27 off_t offset
; /* offset into the final pack file */
28 unsigned long size
; /* uncompressed size */
29 unsigned int hash
; /* name hint hash */
30 unsigned int depth
; /* delta depth */
31 struct packed_git
*in_pack
; /* already in pack */
33 struct object_entry
*delta
; /* delta base object */
34 struct object_entry
*delta_child
; /* deltified objects who bases me */
35 struct object_entry
*delta_sibling
; /* other deltified objects who
36 * uses the same base as me
38 unsigned long delta_size
; /* delta data size (uncompressed) */
39 enum object_type type
;
40 enum object_type in_pack_type
; /* could be delta */
41 unsigned char in_pack_header_size
;
42 unsigned char preferred_base
; /* we do not pack this, but is available
43 * to be used as the base objectto delta
49 * Objects we are going to pack are collected in objects array (dynamically
50 * expanded). nr_objects & nr_alloc controls this array. They are stored
51 * in the order we see -- typically rev-list --objects order that gives us
52 * nice "minimum seek" order.
54 static struct object_entry
*objects
;
55 static struct object_entry
**written_list
;
56 static uint32_t nr_objects
, nr_alloc
, nr_result
, nr_written
;
59 static int no_reuse_delta
, no_reuse_object
;
61 static int incremental
;
62 static int allow_ofs_delta
;
63 static const char *pack_tmp_name
, *idx_tmp_name
;
64 static char tmpname
[PATH_MAX
];
65 static const char *base_name
;
66 static unsigned char pack_file_sha1
[20];
67 static int progress
= 1;
68 static int window
= 10;
69 static uint32_t pack_size_limit
;
70 static int depth
= 50;
71 static int pack_to_stdout
;
72 static int num_preferred_base
;
73 static struct progress progress_state
;
74 static int pack_compression_level
= Z_DEFAULT_COMPRESSION
;
75 static int pack_compression_seen
;
78 * The object names in objects array are hashed with this hashtable,
79 * to help looking up the entry by object name.
80 * This hashtable is built after all the objects are seen.
82 static int *object_ix
;
83 static int object_ix_hashsz
;
86 * Pack index for existing packs give us easy access to the offsets into
87 * corresponding pack file where each object's data starts, but the entries
88 * do not store the size of the compressed representation (uncompressed
89 * size is easily available by examining the pack entry header). It is
90 * also rather expensive to find the sha1 for an object given its offset.
92 * We build a hashtable of existing packs (pack_revindex), and keep reverse
93 * index here -- pack index file is sorted by object name mapping to offset;
94 * this pack_revindex[].revindex array is a list of offset/index_nr pairs
95 * ordered by offset, so if you know the offset of an object, next offset
96 * is where its packed representation ends and the index_nr can be used to
97 * get the object sha1 from the main index.
99 struct revindex_entry
{
103 struct pack_revindex
{
104 struct packed_git
*p
;
105 struct revindex_entry
*revindex
;
107 static struct pack_revindex
*pack_revindex
;
108 static int pack_revindex_hashsz
;
113 static uint32_t written
, written_delta
;
114 static uint32_t reused
, reused_delta
;
116 static int pack_revindex_ix(struct packed_git
*p
)
118 unsigned long ui
= (unsigned long)p
;
121 ui
= ui
^ (ui
>> 16); /* defeat structure alignment */
122 i
= (int)(ui
% pack_revindex_hashsz
);
123 while (pack_revindex
[i
].p
) {
124 if (pack_revindex
[i
].p
== p
)
126 if (++i
== pack_revindex_hashsz
)
132 static void prepare_pack_ix(void)
135 struct packed_git
*p
;
136 for (num
= 0, p
= packed_git
; p
; p
= p
->next
)
140 pack_revindex_hashsz
= num
* 11;
141 pack_revindex
= xcalloc(sizeof(*pack_revindex
), pack_revindex_hashsz
);
142 for (p
= packed_git
; p
; p
= p
->next
) {
143 num
= pack_revindex_ix(p
);
145 pack_revindex
[num
].p
= p
;
147 /* revindex elements are lazily initialized */
150 static int cmp_offset(const void *a_
, const void *b_
)
152 const struct revindex_entry
*a
= a_
;
153 const struct revindex_entry
*b
= b_
;
154 return (a
->offset
< b
->offset
) ? -1 : (a
->offset
> b
->offset
) ? 1 : 0;
158 * Ordered list of offsets of objects in the pack.
160 static void prepare_pack_revindex(struct pack_revindex
*rix
)
162 struct packed_git
*p
= rix
->p
;
163 int num_ent
= p
->num_objects
;
165 const char *index
= p
->index_data
;
167 rix
->revindex
= xmalloc(sizeof(*rix
->revindex
) * (num_ent
+ 1));
170 if (p
->index_version
> 1) {
171 const uint32_t *off_32
=
172 (uint32_t *)(index
+ 8 + p
->num_objects
* (20 + 4));
173 const uint32_t *off_64
= off_32
+ p
->num_objects
;
174 for (i
= 0; i
< num_ent
; i
++) {
175 uint32_t off
= ntohl(*off_32
++);
176 if (!(off
& 0x80000000)) {
177 rix
->revindex
[i
].offset
= off
;
179 rix
->revindex
[i
].offset
=
180 ((uint64_t)ntohl(*off_64
++)) << 32;
181 rix
->revindex
[i
].offset
|=
184 rix
->revindex
[i
].nr
= i
;
187 for (i
= 0; i
< num_ent
; i
++) {
188 uint32_t hl
= *((uint32_t *)(index
+ 24 * i
));
189 rix
->revindex
[i
].offset
= ntohl(hl
);
190 rix
->revindex
[i
].nr
= i
;
194 /* This knows the pack format -- the 20-byte trailer
195 * follows immediately after the last object data.
197 rix
->revindex
[num_ent
].offset
= p
->pack_size
- 20;
198 rix
->revindex
[num_ent
].nr
= -1;
199 qsort(rix
->revindex
, num_ent
, sizeof(*rix
->revindex
), cmp_offset
);
202 static struct revindex_entry
* find_packed_object(struct packed_git
*p
,
207 struct pack_revindex
*rix
;
208 struct revindex_entry
*revindex
;
209 num
= pack_revindex_ix(p
);
211 die("internal error: pack revindex uninitialized");
212 rix
= &pack_revindex
[num
];
214 prepare_pack_revindex(rix
);
215 revindex
= rix
->revindex
;
217 hi
= p
->num_objects
+ 1;
219 int mi
= (lo
+ hi
) / 2;
220 if (revindex
[mi
].offset
== ofs
) {
221 return revindex
+ mi
;
223 else if (ofs
< revindex
[mi
].offset
)
228 die("internal error: pack revindex corrupt");
231 static const unsigned char *find_packed_object_name(struct packed_git
*p
,
234 struct revindex_entry
*entry
= find_packed_object(p
, ofs
);
235 return nth_packed_object_sha1(p
, entry
->nr
);
238 static void *delta_against(void *buf
, unsigned long size
, struct object_entry
*entry
)
240 unsigned long othersize
, delta_size
;
241 enum object_type type
;
242 void *otherbuf
= read_sha1_file(entry
->delta
->sha1
, &type
, &othersize
);
246 die("unable to read %s", sha1_to_hex(entry
->delta
->sha1
));
247 delta_buf
= diff_delta(otherbuf
, othersize
,
248 buf
, size
, &delta_size
, 0);
249 if (!delta_buf
|| delta_size
!= entry
->delta_size
)
250 die("delta size changed");
257 * The per-object header is a pretty dense thing, which is
258 * - first byte: low four bits are "size", then three bits of "type",
259 * and the high bit is "size continues".
260 * - each byte afterwards: low seven bits are size continuation,
261 * with the high bit being "size continues"
263 static int encode_header(enum object_type type
, unsigned long size
, unsigned char *hdr
)
268 if (type
< OBJ_COMMIT
|| type
> OBJ_REF_DELTA
)
269 die("bad type %d", type
);
271 c
= (type
<< 4) | (size
& 15);
284 * we are going to reuse the existing object data as is. make
285 * sure it is not corrupt.
287 static int check_pack_inflate(struct packed_git
*p
,
288 struct pack_window
**w_curs
,
291 unsigned long expect
)
294 unsigned char fakebuf
[4096], *in
;
297 memset(&stream
, 0, sizeof(stream
));
298 inflateInit(&stream
);
300 in
= use_pack(p
, w_curs
, offset
, &stream
.avail_in
);
302 stream
.next_out
= fakebuf
;
303 stream
.avail_out
= sizeof(fakebuf
);
304 st
= inflate(&stream
, Z_FINISH
);
305 offset
+= stream
.next_in
- in
;
306 } while (st
== Z_OK
|| st
== Z_BUF_ERROR
);
308 return (st
== Z_STREAM_END
&&
309 stream
.total_out
== expect
&&
310 stream
.total_in
== len
) ? 0 : -1;
313 static int check_pack_crc(struct packed_git
*p
, struct pack_window
**w_curs
,
314 off_t offset
, off_t len
, unsigned int nr
)
316 const uint32_t *index_crc
;
317 uint32_t data_crc
= crc32(0, Z_NULL
, 0);
321 void *data
= use_pack(p
, w_curs
, offset
, &avail
);
324 data_crc
= crc32(data_crc
, data
, avail
);
329 index_crc
= p
->index_data
;
330 index_crc
+= 2 + 256 + p
->num_objects
* (20/4) + nr
;
332 return data_crc
!= ntohl(*index_crc
);
335 static void copy_pack_data(struct sha1file
*f
,
336 struct packed_git
*p
,
337 struct pack_window
**w_curs
,
345 in
= use_pack(p
, w_curs
, offset
, &avail
);
347 avail
= (unsigned int)len
;
348 sha1write(f
, in
, avail
);
354 static unsigned long write_object(struct sha1file
*f
,
355 struct object_entry
*entry
,
359 enum object_type type
;
361 unsigned char header
[10];
362 unsigned char dheader
[10];
365 enum object_type obj_type
;
367 /* write limit if limited packsize and not first object */
368 unsigned long limit
= pack_size_limit
&& nr_written
?
369 pack_size_limit
- write_offset
: 0;
371 int usable_delta
= !entry
->delta
? 0 :
372 /* yes if unlimited packfile */
373 !pack_size_limit
? 1 :
374 /* no if base written to previous pack */
375 entry
->delta
->offset
== (off_t
)-1 ? 0 :
376 /* otherwise double-check written to this
377 * pack, like we do below
379 entry
->delta
->offset
? 1 : 0;
384 obj_type
= entry
->type
;
386 to_reuse
= 0; /* explicit */
387 else if (!entry
->in_pack
)
388 to_reuse
= 0; /* can't reuse what we don't have */
389 else if (obj_type
== OBJ_REF_DELTA
|| obj_type
== OBJ_OFS_DELTA
)
390 /* check_object() decided it for us ... */
391 to_reuse
= usable_delta
;
392 /* ... but pack split may override that */
393 else if (obj_type
!= entry
->in_pack_type
)
394 to_reuse
= 0; /* pack has delta which is unusable */
395 else if (entry
->delta
)
396 to_reuse
= 0; /* we want to pack afresh */
398 to_reuse
= 1; /* we have it in-pack undeltified,
399 * and we do not need to deltify it.
404 unsigned long maxsize
;
406 buf
= read_sha1_file(entry
->sha1
, &type
, &size
);
408 die("unable to read %s", sha1_to_hex(entry
->sha1
));
409 if (size
!= entry
->size
)
410 die("object %s size inconsistency (%lu vs %lu)",
411 sha1_to_hex(entry
->sha1
), size
, entry
->size
);
413 buf
= delta_against(buf
, size
, entry
);
414 size
= entry
->delta_size
;
415 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
416 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
419 * recover real object type in case
420 * check_object() wanted to re-use a delta,
421 * but we couldn't since base was in previous split pack
425 /* compress the data to store and put compressed length in datalen */
426 memset(&stream
, 0, sizeof(stream
));
427 deflateInit(&stream
, pack_compression_level
);
428 maxsize
= deflateBound(&stream
, size
);
429 out
= xmalloc(maxsize
);
431 stream
.next_in
= buf
;
432 stream
.avail_in
= size
;
433 stream
.next_out
= out
;
434 stream
.avail_out
= maxsize
;
435 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
438 datalen
= stream
.total_out
;
441 * The object header is a byte of 'type' followed by zero or
442 * more bytes of length.
444 hdrlen
= encode_header(obj_type
, size
, header
);
446 if (obj_type
== OBJ_OFS_DELTA
) {
448 * Deltas with relative base contain an additional
449 * encoding of the relative offset for the delta
450 * base from this object's position in the pack.
452 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
453 unsigned pos
= sizeof(dheader
) - 1;
454 dheader
[pos
] = ofs
& 127;
456 dheader
[--pos
] = 128 | (--ofs
& 127);
457 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
) {
462 sha1write(f
, header
, hdrlen
);
463 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
464 hdrlen
+= sizeof(dheader
) - pos
;
465 } else if (obj_type
== OBJ_REF_DELTA
) {
467 * Deltas with a base reference contain
468 * an additional 20 bytes for the base sha1.
470 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
) {
475 sha1write(f
, header
, hdrlen
);
476 sha1write(f
, entry
->delta
->sha1
, 20);
479 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
) {
484 sha1write(f
, header
, hdrlen
);
486 sha1write(f
, out
, datalen
);
491 struct packed_git
*p
= entry
->in_pack
;
492 struct pack_window
*w_curs
= NULL
;
493 struct revindex_entry
*revidx
;
497 obj_type
= (allow_ofs_delta
&& entry
->delta
->offset
) ?
498 OBJ_OFS_DELTA
: OBJ_REF_DELTA
;
501 hdrlen
= encode_header(obj_type
, entry
->size
, header
);
502 offset
= entry
->in_pack_offset
;
503 revidx
= find_packed_object(p
, offset
);
504 datalen
= revidx
[1].offset
- offset
;
505 if (!pack_to_stdout
&& p
->index_version
> 1 &&
506 check_pack_crc(p
, &w_curs
, offset
, datalen
, revidx
->nr
))
507 die("bad packed object CRC for %s", sha1_to_hex(entry
->sha1
));
508 offset
+= entry
->in_pack_header_size
;
509 datalen
-= entry
->in_pack_header_size
;
510 if (obj_type
== OBJ_OFS_DELTA
) {
511 off_t ofs
= entry
->offset
- entry
->delta
->offset
;
512 unsigned pos
= sizeof(dheader
) - 1;
513 dheader
[pos
] = ofs
& 127;
515 dheader
[--pos
] = 128 | (--ofs
& 127);
516 if (limit
&& hdrlen
+ sizeof(dheader
) - pos
+ datalen
+ 20 >= limit
)
518 sha1write(f
, header
, hdrlen
);
519 sha1write(f
, dheader
+ pos
, sizeof(dheader
) - pos
);
520 hdrlen
+= sizeof(dheader
) - pos
;
521 } else if (obj_type
== OBJ_REF_DELTA
) {
522 if (limit
&& hdrlen
+ 20 + datalen
+ 20 >= limit
)
524 sha1write(f
, header
, hdrlen
);
525 sha1write(f
, entry
->delta
->sha1
, 20);
528 if (limit
&& hdrlen
+ datalen
+ 20 >= limit
)
530 sha1write(f
, header
, hdrlen
);
533 if (!pack_to_stdout
&& p
->index_version
== 1 &&
534 check_pack_inflate(p
, &w_curs
, offset
, datalen
, entry
->size
))
535 die("corrupt packed object for %s", sha1_to_hex(entry
->sha1
));
536 copy_pack_data(f
, p
, &w_curs
, offset
, datalen
);
544 entry
->crc32
= crc32_end(f
);
545 return hdrlen
+ datalen
;
548 static off_t
write_one(struct sha1file
*f
,
549 struct object_entry
*e
,
554 /* offset is non zero if object is written already. */
555 if (e
->offset
|| e
->preferred_base
)
558 /* if we are deltified, write out base object first. */
560 offset
= write_one(f
, e
->delta
, offset
);
566 size
= write_object(f
, e
, offset
);
571 written_list
[nr_written
++] = e
;
573 /* make sure off_t is sufficiently large not to wrap */
574 if (offset
> offset
+ size
)
575 die("pack too large for current definition of off_t");
576 return offset
+ size
;
579 static int open_object_dir_tmp(const char *path
)
581 snprintf(tmpname
, sizeof(tmpname
), "%s/%s", get_object_directory(), path
);
582 return mkstemp(tmpname
);
585 /* forward declarations for write_pack_file */
586 static void write_index_file(off_t last_obj_offset
, unsigned char *sha1
);
587 static int adjust_perm(const char *path
, mode_t mode
);
589 static void write_pack_file(void)
593 off_t offset
, offset_one
, last_obj_offset
= 0;
594 struct pack_header hdr
;
595 int do_progress
= progress
>> pack_to_stdout
;
596 uint32_t nr_remaining
= nr_result
;
599 start_progress(&progress_state
, "Writing %u objects...", "", nr_result
);
600 written_list
= xmalloc(nr_objects
* sizeof(struct object_entry
*));
603 if (pack_to_stdout
) {
604 f
= sha1fd(1, "<stdout>");
606 int fd
= open_object_dir_tmp("tmp_pack_XXXXXX");
608 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
609 pack_tmp_name
= xstrdup(tmpname
);
610 f
= sha1fd(fd
, pack_tmp_name
);
613 hdr
.hdr_signature
= htonl(PACK_SIGNATURE
);
614 hdr
.hdr_version
= htonl(PACK_VERSION
);
615 hdr
.hdr_entries
= htonl(nr_remaining
);
616 sha1write(f
, &hdr
, sizeof(hdr
));
617 offset
= sizeof(hdr
);
619 for (; i
< nr_objects
; i
++) {
620 last_obj_offset
= offset
;
621 offset_one
= write_one(f
, objects
+ i
, offset
);
626 display_progress(&progress_state
, written
);
630 * Did we write the wrong # entries in the header?
631 * If so, rewrite it like in fast-import
633 if (pack_to_stdout
|| nr_written
== nr_remaining
) {
634 sha1close(f
, pack_file_sha1
, 1);
636 sha1close(f
, pack_file_sha1
, 0);
637 fixup_pack_header_footer(f
->fd
, pack_file_sha1
, pack_tmp_name
, nr_written
);
641 if (!pack_to_stdout
) {
642 unsigned char object_list_sha1
[20];
643 mode_t mode
= umask(0);
648 write_index_file(last_obj_offset
, object_list_sha1
);
649 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.pack",
650 base_name
, sha1_to_hex(object_list_sha1
));
651 if (adjust_perm(pack_tmp_name
, mode
))
652 die("unable to make temporary pack file readable: %s",
654 if (rename(pack_tmp_name
, tmpname
))
655 die("unable to rename temporary pack file: %s",
657 snprintf(tmpname
, sizeof(tmpname
), "%s-%s.idx",
658 base_name
, sha1_to_hex(object_list_sha1
));
659 if (adjust_perm(idx_tmp_name
, mode
))
660 die("unable to make temporary index file readable: %s",
662 if (rename(idx_tmp_name
, tmpname
))
663 die("unable to rename temporary index file: %s",
665 puts(sha1_to_hex(object_list_sha1
));
668 /* mark written objects as written to previous pack */
669 for (j
= 0; j
< nr_written
; j
++) {
670 written_list
[j
]->offset
= (off_t
)-1;
672 nr_remaining
-= nr_written
;
673 } while (nr_remaining
&& i
< nr_objects
);
677 stop_progress(&progress_state
);
678 if (written
!= nr_result
)
679 die("wrote %u objects while expecting %u", written
, nr_result
);
681 * We have scanned through [0 ... i). Since we have written
682 * the correct number of objects, the remaining [i ... nr_objects)
683 * items must be either already written (due to out-of-order delta base)
684 * or a preferred base. Count those which are neither and complain if any.
686 for (j
= 0; i
< nr_objects
; i
++) {
687 struct object_entry
*e
= objects
+ i
;
688 j
+= !e
->offset
&& !e
->preferred_base
;
691 die("wrote %u objects as expected but %u unwritten", written
, j
);
694 static int sha1_sort(const void *_a
, const void *_b
)
696 const struct object_entry
*a
= *(struct object_entry
**)_a
;
697 const struct object_entry
*b
= *(struct object_entry
**)_b
;
698 return hashcmp(a
->sha1
, b
->sha1
);
701 static uint32_t index_default_version
= 1;
702 static uint32_t index_off32_limit
= 0x7fffffff;
704 static void write_index_file(off_t last_obj_offset
, unsigned char *sha1
)
707 struct object_entry
**sorted_by_sha
, **list
, **last
;
709 uint32_t i
, index_version
;
712 int fd
= open_object_dir_tmp("tmp_idx_XXXXXX");
714 die("unable to create %s: %s\n", tmpname
, strerror(errno
));
715 idx_tmp_name
= xstrdup(tmpname
);
716 f
= sha1fd(fd
, idx_tmp_name
);
719 sorted_by_sha
= written_list
;
720 qsort(sorted_by_sha
, nr_written
, sizeof(*sorted_by_sha
), sha1_sort
);
721 list
= sorted_by_sha
;
722 last
= sorted_by_sha
+ nr_written
;
724 sorted_by_sha
= list
= last
= NULL
;
726 /* if last object's offset is >= 2^31 we should use index V2 */
727 index_version
= (last_obj_offset
>> 31) ? 2 : index_default_version
;
729 /* index versions 2 and above need a header */
730 if (index_version
>= 2) {
731 struct pack_idx_header hdr
;
732 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
733 hdr
.idx_version
= htonl(index_version
);
734 sha1write(f
, &hdr
, sizeof(hdr
));
738 * Write the first-level table (the list is sorted,
739 * but we use a 256-entry lookup to be able to avoid
740 * having to do eight extra binary search iterations).
742 for (i
= 0; i
< 256; i
++) {
743 struct object_entry
**next
= list
;
744 while (next
< last
) {
745 struct object_entry
*entry
= *next
;
746 if (entry
->sha1
[0] != i
)
750 array
[i
] = htonl(next
- sorted_by_sha
);
753 sha1write(f
, array
, 256 * 4);
755 /* Compute the SHA1 hash of sorted object names. */
758 /* Write the actual SHA1 entries. */
759 list
= sorted_by_sha
;
760 for (i
= 0; i
< nr_written
; i
++) {
761 struct object_entry
*entry
= *list
++;
762 if (index_version
< 2) {
763 uint32_t offset
= htonl(entry
->offset
);
764 sha1write(f
, &offset
, 4);
766 sha1write(f
, entry
->sha1
, 20);
767 SHA1_Update(&ctx
, entry
->sha1
, 20);
770 if (index_version
>= 2) {
771 unsigned int nr_large_offset
= 0;
773 /* write the crc32 table */
774 list
= sorted_by_sha
;
775 for (i
= 0; i
< nr_written
; i
++) {
776 struct object_entry
*entry
= *list
++;
777 uint32_t crc32_val
= htonl(entry
->crc32
);
778 sha1write(f
, &crc32_val
, 4);
781 /* write the 32-bit offset table */
782 list
= sorted_by_sha
;
783 for (i
= 0; i
< nr_written
; i
++) {
784 struct object_entry
*entry
= *list
++;
785 uint32_t offset
= (entry
->offset
<= index_off32_limit
) ?
786 entry
->offset
: (0x80000000 | nr_large_offset
++);
787 offset
= htonl(offset
);
788 sha1write(f
, &offset
, 4);
791 /* write the large offset table */
792 list
= sorted_by_sha
;
793 while (nr_large_offset
) {
794 struct object_entry
*entry
= *list
++;
795 uint64_t offset
= entry
->offset
;
796 if (offset
> index_off32_limit
) {
798 split
[0] = htonl(offset
>> 32);
799 split
[1] = htonl(offset
& 0xffffffff);
800 sha1write(f
, split
, 8);
806 sha1write(f
, pack_file_sha1
, 20);
807 sha1close(f
, NULL
, 1);
808 SHA1_Final(sha1
, &ctx
);
811 static int locate_object_entry_hash(const unsigned char *sha1
)
815 memcpy(&ui
, sha1
, sizeof(unsigned int));
816 i
= ui
% object_ix_hashsz
;
817 while (0 < object_ix
[i
]) {
818 if (!hashcmp(sha1
, objects
[object_ix
[i
] - 1].sha1
))
820 if (++i
== object_ix_hashsz
)
826 static struct object_entry
*locate_object_entry(const unsigned char *sha1
)
830 if (!object_ix_hashsz
)
833 i
= locate_object_entry_hash(sha1
);
835 return &objects
[object_ix
[i
]-1];
839 static void rehash_objects(void)
842 struct object_entry
*oe
;
844 object_ix_hashsz
= nr_objects
* 3;
845 if (object_ix_hashsz
< 1024)
846 object_ix_hashsz
= 1024;
847 object_ix
= xrealloc(object_ix
, sizeof(int) * object_ix_hashsz
);
848 memset(object_ix
, 0, sizeof(int) * object_ix_hashsz
);
849 for (i
= 0, oe
= objects
; i
< nr_objects
; i
++, oe
++) {
850 int ix
= locate_object_entry_hash(oe
->sha1
);
854 object_ix
[ix
] = i
+ 1;
858 static unsigned name_hash(const char *name
)
864 * This effectively just creates a sortable number from the
865 * last sixteen non-whitespace characters. Last characters
866 * count "most", so things that end in ".c" sort together.
868 while ((c
= *name
++) != 0) {
871 hash
= (hash
>> 2) + (c
<< 24);
876 static int add_object_entry(const unsigned char *sha1
, enum object_type type
,
877 unsigned hash
, int exclude
)
879 struct object_entry
*entry
;
880 struct packed_git
*p
, *found_pack
= NULL
;
881 off_t found_offset
= 0;
884 ix
= nr_objects
? locate_object_entry_hash(sha1
) : -1;
887 entry
= objects
+ object_ix
[ix
] - 1;
888 if (!entry
->preferred_base
)
890 entry
->preferred_base
= 1;
895 for (p
= packed_git
; p
; p
= p
->next
) {
896 off_t offset
= find_pack_entry_one(sha1
, p
);
899 found_offset
= offset
;
906 if (local
&& !p
->pack_local
)
911 if (nr_objects
>= nr_alloc
) {
912 nr_alloc
= (nr_alloc
+ 1024) * 3 / 2;
913 objects
= xrealloc(objects
, nr_alloc
* sizeof(*entry
));
916 entry
= objects
+ nr_objects
++;
917 memset(entry
, 0, sizeof(*entry
));
918 hashcpy(entry
->sha1
, sha1
);
923 entry
->preferred_base
= 1;
927 entry
->in_pack
= found_pack
;
928 entry
->in_pack_offset
= found_offset
;
931 if (object_ix_hashsz
* 3 <= nr_objects
* 4)
934 object_ix
[-1 - ix
] = nr_objects
;
937 display_progress(&progress_state
, nr_objects
);
942 struct pbase_tree_cache
{
943 unsigned char sha1
[20];
947 unsigned long tree_size
;
950 static struct pbase_tree_cache
*(pbase_tree_cache
[256]);
951 static int pbase_tree_cache_ix(const unsigned char *sha1
)
953 return sha1
[0] % ARRAY_SIZE(pbase_tree_cache
);
955 static int pbase_tree_cache_ix_incr(int ix
)
957 return (ix
+1) % ARRAY_SIZE(pbase_tree_cache
);
960 static struct pbase_tree
{
961 struct pbase_tree
*next
;
962 /* This is a phony "cache" entry; we are not
963 * going to evict it nor find it through _get()
964 * mechanism -- this is for the toplevel node that
965 * would almost always change with any commit.
967 struct pbase_tree_cache pcache
;
970 static struct pbase_tree_cache
*pbase_tree_get(const unsigned char *sha1
)
972 struct pbase_tree_cache
*ent
, *nent
;
975 enum object_type type
;
977 int my_ix
= pbase_tree_cache_ix(sha1
);
978 int available_ix
= -1;
980 /* pbase-tree-cache acts as a limited hashtable.
981 * your object will be found at your index or within a few
982 * slots after that slot if it is cached.
984 for (neigh
= 0; neigh
< 8; neigh
++) {
985 ent
= pbase_tree_cache
[my_ix
];
986 if (ent
&& !hashcmp(ent
->sha1
, sha1
)) {
990 else if (((available_ix
< 0) && (!ent
|| !ent
->ref
)) ||
991 ((0 <= available_ix
) &&
992 (!ent
&& pbase_tree_cache
[available_ix
])))
993 available_ix
= my_ix
;
996 my_ix
= pbase_tree_cache_ix_incr(my_ix
);
999 /* Did not find one. Either we got a bogus request or
1000 * we need to read and perhaps cache.
1002 data
= read_sha1_file(sha1
, &type
, &size
);
1005 if (type
!= OBJ_TREE
) {
1010 /* We need to either cache or return a throwaway copy */
1012 if (available_ix
< 0)
1015 ent
= pbase_tree_cache
[available_ix
];
1016 my_ix
= available_ix
;
1020 nent
= xmalloc(sizeof(*nent
));
1021 nent
->temporary
= (available_ix
< 0);
1024 /* evict and reuse */
1025 free(ent
->tree_data
);
1028 hashcpy(nent
->sha1
, sha1
);
1029 nent
->tree_data
= data
;
1030 nent
->tree_size
= size
;
1032 if (!nent
->temporary
)
1033 pbase_tree_cache
[my_ix
] = nent
;
1037 static void pbase_tree_put(struct pbase_tree_cache
*cache
)
1039 if (!cache
->temporary
) {
1043 free(cache
->tree_data
);
1047 static int name_cmp_len(const char *name
)
1050 for (i
= 0; name
[i
] && name
[i
] != '\n' && name
[i
] != '/'; i
++)
1055 static void add_pbase_object(struct tree_desc
*tree
,
1058 const char *fullname
)
1060 struct name_entry entry
;
1063 while (tree_entry(tree
,&entry
)) {
1064 cmp
= tree_entry_len(entry
.path
, entry
.sha1
) != cmplen
? 1 :
1065 memcmp(name
, entry
.path
, cmplen
);
1070 if (name
[cmplen
] != '/') {
1071 unsigned hash
= name_hash(fullname
);
1072 add_object_entry(entry
.sha1
,
1073 S_ISDIR(entry
.mode
) ? OBJ_TREE
: OBJ_BLOB
,
1077 if (S_ISDIR(entry
.mode
)) {
1078 struct tree_desc sub
;
1079 struct pbase_tree_cache
*tree
;
1080 const char *down
= name
+cmplen
+1;
1081 int downlen
= name_cmp_len(down
);
1083 tree
= pbase_tree_get(entry
.sha1
);
1086 init_tree_desc(&sub
, tree
->tree_data
, tree
->tree_size
);
1088 add_pbase_object(&sub
, down
, downlen
, fullname
);
1089 pbase_tree_put(tree
);
1094 static unsigned *done_pbase_paths
;
1095 static int done_pbase_paths_num
;
1096 static int done_pbase_paths_alloc
;
1097 static int done_pbase_path_pos(unsigned hash
)
1100 int hi
= done_pbase_paths_num
;
1102 int mi
= (hi
+ lo
) / 2;
1103 if (done_pbase_paths
[mi
] == hash
)
1105 if (done_pbase_paths
[mi
] < hash
)
1113 static int check_pbase_path(unsigned hash
)
1115 int pos
= (!done_pbase_paths
) ? -1 : done_pbase_path_pos(hash
);
1119 if (done_pbase_paths_alloc
<= done_pbase_paths_num
) {
1120 done_pbase_paths_alloc
= alloc_nr(done_pbase_paths_alloc
);
1121 done_pbase_paths
= xrealloc(done_pbase_paths
,
1122 done_pbase_paths_alloc
*
1125 done_pbase_paths_num
++;
1126 if (pos
< done_pbase_paths_num
)
1127 memmove(done_pbase_paths
+ pos
+ 1,
1128 done_pbase_paths
+ pos
,
1129 (done_pbase_paths_num
- pos
- 1) * sizeof(unsigned));
1130 done_pbase_paths
[pos
] = hash
;
1134 static void add_preferred_base_object(const char *name
, unsigned hash
)
1136 struct pbase_tree
*it
;
1139 if (!num_preferred_base
|| check_pbase_path(hash
))
1142 cmplen
= name_cmp_len(name
);
1143 for (it
= pbase_tree
; it
; it
= it
->next
) {
1145 add_object_entry(it
->pcache
.sha1
, OBJ_TREE
, 0, 1);
1148 struct tree_desc tree
;
1149 init_tree_desc(&tree
, it
->pcache
.tree_data
, it
->pcache
.tree_size
);
1150 add_pbase_object(&tree
, name
, cmplen
, name
);
1155 static void add_preferred_base(unsigned char *sha1
)
1157 struct pbase_tree
*it
;
1160 unsigned char tree_sha1
[20];
1162 if (window
<= num_preferred_base
++)
1165 data
= read_object_with_reference(sha1
, tree_type
, &size
, tree_sha1
);
1169 for (it
= pbase_tree
; it
; it
= it
->next
) {
1170 if (!hashcmp(it
->pcache
.sha1
, tree_sha1
)) {
1176 it
= xcalloc(1, sizeof(*it
));
1177 it
->next
= pbase_tree
;
1180 hashcpy(it
->pcache
.sha1
, tree_sha1
);
1181 it
->pcache
.tree_data
= data
;
1182 it
->pcache
.tree_size
= size
;
1185 static void check_object(struct object_entry
*entry
)
1187 if (entry
->in_pack
) {
1188 struct packed_git
*p
= entry
->in_pack
;
1189 struct pack_window
*w_curs
= NULL
;
1190 const unsigned char *base_ref
= NULL
;
1191 struct object_entry
*base_entry
;
1192 unsigned long used
, used_0
;
1195 unsigned char *buf
, c
;
1197 buf
= use_pack(p
, &w_curs
, entry
->in_pack_offset
, &avail
);
1200 * We want in_pack_type even if we do not reuse delta
1201 * since non-delta representations could still be reused.
1203 used
= unpack_object_header_gently(buf
, avail
,
1204 &entry
->in_pack_type
,
1208 * Determine if this is a delta and if so whether we can
1209 * reuse it or not. Otherwise let's find out as cheaply as
1210 * possible what the actual type and size for this object is.
1212 switch (entry
->in_pack_type
) {
1214 /* Not a delta hence we've already got all we need. */
1215 entry
->type
= entry
->in_pack_type
;
1216 entry
->in_pack_header_size
= used
;
1217 unuse_pack(&w_curs
);
1220 if (!no_reuse_delta
&& !entry
->preferred_base
)
1221 base_ref
= use_pack(p
, &w_curs
,
1222 entry
->in_pack_offset
+ used
, NULL
);
1223 entry
->in_pack_header_size
= used
+ 20;
1226 buf
= use_pack(p
, &w_curs
,
1227 entry
->in_pack_offset
+ used
, NULL
);
1233 if (!ofs
|| MSB(ofs
, 7))
1234 die("delta base offset overflow in pack for %s",
1235 sha1_to_hex(entry
->sha1
));
1237 ofs
= (ofs
<< 7) + (c
& 127);
1239 if (ofs
>= entry
->in_pack_offset
)
1240 die("delta base offset out of bound for %s",
1241 sha1_to_hex(entry
->sha1
));
1242 ofs
= entry
->in_pack_offset
- ofs
;
1243 if (!no_reuse_delta
&& !entry
->preferred_base
)
1244 base_ref
= find_packed_object_name(p
, ofs
);
1245 entry
->in_pack_header_size
= used
+ used_0
;
1249 if (base_ref
&& (base_entry
= locate_object_entry(base_ref
))) {
1251 * If base_ref was set above that means we wish to
1252 * reuse delta data, and we even found that base
1253 * in the list of objects we want to pack. Goodie!
1255 * Depth value does not matter - find_deltas() will
1256 * never consider reused delta as the base object to
1257 * deltify other objects against, in order to avoid
1260 entry
->type
= entry
->in_pack_type
;
1261 entry
->delta
= base_entry
;
1262 entry
->delta_sibling
= base_entry
->delta_child
;
1263 base_entry
->delta_child
= entry
;
1264 unuse_pack(&w_curs
);
1270 * This must be a delta and we already know what the
1271 * final object type is. Let's extract the actual
1272 * object size from the delta header.
1274 entry
->size
= get_size_from_delta(p
, &w_curs
,
1275 entry
->in_pack_offset
+ entry
->in_pack_header_size
);
1276 unuse_pack(&w_curs
);
1281 * No choice but to fall back to the recursive delta walk
1282 * with sha1_object_info() to find about the object type
1285 unuse_pack(&w_curs
);
1288 entry
->type
= sha1_object_info(entry
->sha1
, &entry
->size
);
1289 if (entry
->type
< 0)
1290 die("unable to get type of object %s",
1291 sha1_to_hex(entry
->sha1
));
1294 static int pack_offset_sort(const void *_a
, const void *_b
)
1296 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1297 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1299 /* avoid filesystem trashing with loose objects */
1300 if (!a
->in_pack
&& !b
->in_pack
)
1301 return hashcmp(a
->sha1
, b
->sha1
);
1303 if (a
->in_pack
< b
->in_pack
)
1305 if (a
->in_pack
> b
->in_pack
)
1307 return a
->in_pack_offset
< b
->in_pack_offset
? -1 :
1308 (a
->in_pack_offset
> b
->in_pack_offset
);
1311 static void get_object_details(void)
1314 struct object_entry
**sorted_by_offset
;
1316 sorted_by_offset
= xcalloc(nr_objects
, sizeof(struct object_entry
*));
1317 for (i
= 0; i
< nr_objects
; i
++)
1318 sorted_by_offset
[i
] = objects
+ i
;
1319 qsort(sorted_by_offset
, nr_objects
, sizeof(*sorted_by_offset
), pack_offset_sort
);
1322 for (i
= 0; i
< nr_objects
; i
++)
1323 check_object(sorted_by_offset
[i
]);
1324 free(sorted_by_offset
);
1327 static int type_size_sort(const void *_a
, const void *_b
)
1329 const struct object_entry
*a
= *(struct object_entry
**)_a
;
1330 const struct object_entry
*b
= *(struct object_entry
**)_b
;
1332 if (a
->type
< b
->type
)
1334 if (a
->type
> b
->type
)
1336 if (a
->hash
< b
->hash
)
1338 if (a
->hash
> b
->hash
)
1340 if (a
->preferred_base
< b
->preferred_base
)
1342 if (a
->preferred_base
> b
->preferred_base
)
1344 if (a
->size
< b
->size
)
1346 if (a
->size
> b
->size
)
1348 return a
> b
? -1 : (a
< b
); /* newest last */
1352 struct object_entry
*entry
;
1354 struct delta_index
*index
;
1358 * We search for deltas _backwards_ in a list sorted by type and
1359 * by size, so that we see progressively smaller and smaller files.
1360 * That's because we prefer deltas to be from the bigger file
1361 * to the smaller - deletes are potentially cheaper, but perhaps
1362 * more importantly, the bigger file is likely the more recent
1365 static int try_delta(struct unpacked
*trg
, struct unpacked
*src
,
1368 struct object_entry
*trg_entry
= trg
->entry
;
1369 struct object_entry
*src_entry
= src
->entry
;
1370 unsigned long trg_size
, src_size
, delta_size
, sizediff
, max_size
, sz
;
1371 enum object_type type
;
1374 /* Don't bother doing diffs between different types */
1375 if (trg_entry
->type
!= src_entry
->type
)
1378 /* We do not compute delta to *create* objects we are not
1381 if (trg_entry
->preferred_base
)
1385 * We do not bother to try a delta that we discarded
1386 * on an earlier try, but only when reusing delta data.
1388 if (!no_reuse_delta
&& trg_entry
->in_pack
&&
1389 trg_entry
->in_pack
== src_entry
->in_pack
&&
1390 trg_entry
->in_pack_type
!= OBJ_REF_DELTA
&&
1391 trg_entry
->in_pack_type
!= OBJ_OFS_DELTA
)
1394 /* Let's not bust the allowed depth. */
1395 if (src_entry
->depth
>= max_depth
)
1398 /* Now some size filtering heuristics. */
1399 trg_size
= trg_entry
->size
;
1400 max_size
= trg_size
/2 - 20;
1401 max_size
= max_size
* (max_depth
- src_entry
->depth
) / max_depth
;
1404 if (trg_entry
->delta
&& trg_entry
->delta_size
<= max_size
)
1405 max_size
= trg_entry
->delta_size
-1;
1406 src_size
= src_entry
->size
;
1407 sizediff
= src_size
< trg_size
? trg_size
- src_size
: 0;
1408 if (sizediff
>= max_size
)
1411 /* Load data if not already done */
1413 trg
->data
= read_sha1_file(trg_entry
->sha1
, &type
, &sz
);
1415 die("object %s inconsistent object length (%lu vs %lu)",
1416 sha1_to_hex(trg_entry
->sha1
), sz
, trg_size
);
1419 src
->data
= read_sha1_file(src_entry
->sha1
, &type
, &sz
);
1421 die("object %s inconsistent object length (%lu vs %lu)",
1422 sha1_to_hex(src_entry
->sha1
), sz
, src_size
);
1425 src
->index
= create_delta_index(src
->data
, src_size
);
1427 die("out of memory");
1430 delta_buf
= create_delta(src
->index
, trg
->data
, trg_size
, &delta_size
, max_size
);
1434 trg_entry
->delta
= src_entry
;
1435 trg_entry
->delta_size
= delta_size
;
1436 trg_entry
->depth
= src_entry
->depth
+ 1;
1441 static unsigned int check_delta_limit(struct object_entry
*me
, unsigned int n
)
1443 struct object_entry
*child
= me
->delta_child
;
1446 unsigned int c
= check_delta_limit(child
, n
+ 1);
1449 child
= child
->delta_sibling
;
1454 static void find_deltas(struct object_entry
**list
, int window
, int depth
)
1456 uint32_t i
= nr_objects
, idx
= 0, processed
= 0;
1457 unsigned int array_size
= window
* sizeof(struct unpacked
);
1458 struct unpacked
*array
;
1463 array
= xmalloc(array_size
);
1464 memset(array
, 0, array_size
);
1466 start_progress(&progress_state
, "Deltifying %u objects...", "", nr_result
);
1469 struct object_entry
*entry
= list
[--i
];
1470 struct unpacked
*n
= array
+ idx
;
1473 if (!entry
->preferred_base
)
1477 display_progress(&progress_state
, processed
);
1480 /* This happens if we decided to reuse existing
1481 * delta from a pack. "!no_reuse_delta &&" is implied.
1485 if (entry
->size
< 50)
1487 free_delta_index(n
->index
);
1494 * If the current object is at pack edge, take the depth the
1495 * objects that depend on the current object into account
1496 * otherwise they would become too deep.
1499 if (entry
->delta_child
) {
1500 max_depth
-= check_delta_limit(entry
, 0);
1507 uint32_t other_idx
= idx
+ j
;
1509 if (other_idx
>= window
)
1510 other_idx
-= window
;
1511 m
= array
+ other_idx
;
1514 if (try_delta(n
, m
, max_depth
) < 0)
1518 /* if we made n a delta, and if n is already at max
1519 * depth, leaving it in the window is pointless. we
1520 * should evict it first.
1522 if (entry
->delta
&& depth
<= entry
->depth
)
1532 stop_progress(&progress_state
);
1534 for (i
= 0; i
< window
; ++i
) {
1535 free_delta_index(array
[i
].index
);
1536 free(array
[i
].data
);
1541 static void prepare_pack(int window
, int depth
)
1543 struct object_entry
**delta_list
;
1546 get_object_details();
1548 if (!window
|| !depth
)
1551 delta_list
= xmalloc(nr_objects
* sizeof(*delta_list
));
1552 for (i
= 0; i
< nr_objects
; i
++)
1553 delta_list
[i
] = objects
+ i
;
1554 qsort(delta_list
, nr_objects
, sizeof(*delta_list
), type_size_sort
);
1555 find_deltas(delta_list
, window
+1, depth
);
1559 static int git_pack_config(const char *k
, const char *v
)
1561 if(!strcmp(k
, "pack.window")) {
1562 window
= git_config_int(k
, v
);
1565 if(!strcmp(k
, "pack.depth")) {
1566 depth
= git_config_int(k
, v
);
1569 if (!strcmp(k
, "pack.compression")) {
1570 int level
= git_config_int(k
, v
);
1572 level
= Z_DEFAULT_COMPRESSION
;
1573 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1574 die("bad pack compression level %d", level
);
1575 pack_compression_level
= level
;
1576 pack_compression_seen
= 1;
1579 return git_default_config(k
, v
);
1582 static void read_object_list_from_stdin(void)
1584 char line
[40 + 1 + PATH_MAX
+ 2];
1585 unsigned char sha1
[20];
1589 if (!fgets(line
, sizeof(line
), stdin
)) {
1593 die("fgets returned NULL, not EOF, not error!");
1595 die("fgets: %s", strerror(errno
));
1599 if (line
[0] == '-') {
1600 if (get_sha1_hex(line
+1, sha1
))
1601 die("expected edge sha1, got garbage:\n %s",
1603 add_preferred_base(sha1
);
1606 if (get_sha1_hex(line
, sha1
))
1607 die("expected sha1, got garbage:\n %s", line
);
1609 hash
= name_hash(line
+41);
1610 add_preferred_base_object(line
+41, hash
);
1611 add_object_entry(sha1
, 0, hash
, 0);
1615 static void show_commit(struct commit
*commit
)
1617 add_object_entry(commit
->object
.sha1
, OBJ_COMMIT
, 0, 0);
1620 static void show_object(struct object_array_entry
*p
)
1622 unsigned hash
= name_hash(p
->name
);
1623 add_preferred_base_object(p
->name
, hash
);
1624 add_object_entry(p
->item
->sha1
, p
->item
->type
, hash
, 0);
1627 static void show_edge(struct commit
*commit
)
1629 add_preferred_base(commit
->object
.sha1
);
1632 static void get_object_list(int ac
, const char **av
)
1634 struct rev_info revs
;
1638 init_revisions(&revs
, NULL
);
1639 save_commit_buffer
= 0;
1640 track_object_refs
= 0;
1641 setup_revisions(ac
, av
, &revs
, NULL
);
1643 while (fgets(line
, sizeof(line
), stdin
) != NULL
) {
1644 int len
= strlen(line
);
1645 if (line
[len
- 1] == '\n')
1650 if (!strcmp(line
, "--not")) {
1651 flags
^= UNINTERESTING
;
1654 die("not a rev '%s'", line
);
1656 if (handle_revision_arg(line
, &revs
, flags
, 1))
1657 die("bad revision '%s'", line
);
1660 prepare_revision_walk(&revs
);
1661 mark_edges_uninteresting(revs
.commits
, &revs
, show_edge
);
1662 traverse_commit_list(&revs
, show_commit
, show_object
);
1665 static int adjust_perm(const char *path
, mode_t mode
)
1667 if (chmod(path
, mode
))
1669 return adjust_shared_perm(path
);
1672 int cmd_pack_objects(int argc
, const char **argv
, const char *prefix
)
1674 int use_internal_rev_list
= 0;
1678 int rp_ac_alloc
= 64;
1681 rp_av
= xcalloc(rp_ac_alloc
, sizeof(*rp_av
));
1683 rp_av
[0] = "pack-objects";
1684 rp_av
[1] = "--objects"; /* --thin will make it --objects-edge */
1687 git_config(git_pack_config
);
1688 if (!pack_compression_seen
&& core_compression_seen
)
1689 pack_compression_level
= core_compression_level
;
1691 progress
= isatty(2);
1692 for (i
= 1; i
< argc
; i
++) {
1693 const char *arg
= argv
[i
];
1698 if (!strcmp("--non-empty", arg
)) {
1702 if (!strcmp("--local", arg
)) {
1706 if (!strcmp("--incremental", arg
)) {
1710 if (!prefixcmp(arg
, "--compression=")) {
1712 int level
= strtoul(arg
+14, &end
, 0);
1713 if (!arg
[14] || *end
)
1716 level
= Z_DEFAULT_COMPRESSION
;
1717 else if (level
< 0 || level
> Z_BEST_COMPRESSION
)
1718 die("bad pack compression level %d", level
);
1719 pack_compression_level
= level
;
1722 if (!prefixcmp(arg
, "--max-pack-size=")) {
1724 pack_size_limit
= strtoul(arg
+16, &end
, 0) * 1024 * 1024;
1725 if (!arg
[16] || *end
)
1729 if (!prefixcmp(arg
, "--window=")) {
1731 window
= strtoul(arg
+9, &end
, 0);
1732 if (!arg
[9] || *end
)
1736 if (!prefixcmp(arg
, "--depth=")) {
1738 depth
= strtoul(arg
+8, &end
, 0);
1739 if (!arg
[8] || *end
)
1743 if (!strcmp("--progress", arg
)) {
1747 if (!strcmp("--all-progress", arg
)) {
1751 if (!strcmp("-q", arg
)) {
1755 if (!strcmp("--no-reuse-delta", arg
)) {
1759 if (!strcmp("--no-reuse-object", arg
)) {
1760 no_reuse_object
= no_reuse_delta
= 1;
1763 if (!strcmp("--delta-base-offset", arg
)) {
1764 allow_ofs_delta
= 1;
1767 if (!strcmp("--stdout", arg
)) {
1771 if (!strcmp("--revs", arg
)) {
1772 use_internal_rev_list
= 1;
1775 if (!strcmp("--unpacked", arg
) ||
1776 !prefixcmp(arg
, "--unpacked=") ||
1777 !strcmp("--reflog", arg
) ||
1778 !strcmp("--all", arg
)) {
1779 use_internal_rev_list
= 1;
1780 if (rp_ac
>= rp_ac_alloc
- 1) {
1781 rp_ac_alloc
= alloc_nr(rp_ac_alloc
);
1782 rp_av
= xrealloc(rp_av
,
1783 rp_ac_alloc
* sizeof(*rp_av
));
1785 rp_av
[rp_ac
++] = arg
;
1788 if (!strcmp("--thin", arg
)) {
1789 use_internal_rev_list
= 1;
1791 rp_av
[1] = "--objects-edge";
1794 if (!prefixcmp(arg
, "--index-version=")) {
1796 index_default_version
= strtoul(arg
+ 16, &c
, 10);
1797 if (index_default_version
> 2)
1800 index_off32_limit
= strtoul(c
+1, &c
, 0);
1801 if (*c
|| index_off32_limit
& 0x80000000)
1808 /* Traditionally "pack-objects [options] base extra" failed;
1809 * we would however want to take refs parameter that would
1810 * have been given to upstream rev-list ourselves, which means
1811 * we somehow want to say what the base name is. So the
1814 * pack-objects [options] base <refs...>
1816 * in other words, we would treat the first non-option as the
1817 * base_name and send everything else to the internal revision
1821 if (!pack_to_stdout
)
1822 base_name
= argv
[i
++];
1824 if (pack_to_stdout
!= !base_name
)
1827 if (pack_to_stdout
&& pack_size_limit
)
1828 die("--max-pack-size cannot be used to build a pack for transfer.");
1830 if (!pack_to_stdout
&& thin
)
1831 die("--thin cannot be used to build an indexable pack.");
1833 prepare_packed_git();
1836 start_progress(&progress_state
, "Generating pack...",
1837 "Counting objects: ", 0);
1838 if (!use_internal_rev_list
)
1839 read_object_list_from_stdin();
1841 rp_av
[rp_ac
] = NULL
;
1842 get_object_list(rp_ac
, rp_av
);
1845 stop_progress(&progress_state
);
1846 fprintf(stderr
, "Done counting %u objects.\n", nr_objects
);
1849 if (non_empty
&& !nr_result
)
1851 if (progress
&& (nr_objects
!= nr_result
))
1852 fprintf(stderr
, "Result has %u objects.\n", nr_result
);
1854 prepare_pack(window
, depth
);
1857 fprintf(stderr
, "Total %u (delta %u), reused %u (delta %u)\n",
1858 written
, written_delta
, reused
, reused_delta
);