13 static const char index_pack_usage
[] =
14 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
17 struct pack_idx_entry idx
;
19 unsigned int hdr_size
;
20 enum object_type type
;
21 enum object_type real_type
;
27 unsigned char sha1
[20];
32 struct base_data
*base
;
33 struct base_data
*child
;
34 struct object_entry
*obj
;
37 int ref_first
, ref_last
;
38 int ofs_first
, ofs_last
;
42 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
43 * to memcmp() only the first 20 bytes.
45 #define UNION_BASE_SZ 20
47 #define FLAG_LINK (1u<<20)
48 #define FLAG_CHECKED (1u<<21)
51 union delta_base base
;
55 static struct object_entry
*objects
;
56 static struct delta_entry
*deltas
;
57 static struct base_data
*base_cache
;
58 static size_t base_cache_used
;
59 static int nr_objects
;
61 static int nr_resolved_deltas
;
63 static int from_stdin
;
67 static struct progress
*progress
;
69 /* We always read in 4kB chunks. */
70 static unsigned char input_buffer
[4096];
71 static unsigned int input_offset
, input_len
;
72 static off_t consumed_bytes
;
73 static unsigned deepest_delta
;
74 static git_SHA_CTX input_ctx
;
75 static uint32_t input_crc32
;
76 static int input_fd
, output_fd
, pack_fd
;
78 static int mark_link(struct object
*obj
, int type
, void *data
)
83 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
84 die(_("object type mismatch at %s"), sha1_to_hex(obj
->sha1
));
86 obj
->flags
|= FLAG_LINK
;
90 /* The content of each linked object must have been checked
91 or it must be already present in the object database */
92 static void check_object(struct object
*obj
)
97 if (!(obj
->flags
& FLAG_LINK
))
100 if (!(obj
->flags
& FLAG_CHECKED
)) {
102 int type
= sha1_object_info(obj
->sha1
, &size
);
103 if (type
!= obj
->type
|| type
<= 0)
104 die(_("object of unexpected type"));
105 obj
->flags
|= FLAG_CHECKED
;
110 static void check_objects(void)
114 max
= get_max_object_index();
115 for (i
= 0; i
< max
; i
++)
116 check_object(get_indexed_object(i
));
120 /* Discard current buffer used content. */
121 static void flush(void)
125 write_or_die(output_fd
, input_buffer
, input_offset
);
126 git_SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
127 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
133 * Make sure at least "min" bytes are available in the buffer, and
134 * return the pointer to the buffer.
136 static void *fill(int min
)
138 if (min
<= input_len
)
139 return input_buffer
+ input_offset
;
140 if (min
> sizeof(input_buffer
))
141 die(Q_("cannot fill %d byte",
142 "cannot fill %d bytes",
147 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
148 sizeof(input_buffer
) - input_len
);
152 die_errno(_("read error on input"));
156 display_throughput(progress
, consumed_bytes
+ input_len
);
157 } while (input_len
< min
);
161 static void use(int bytes
)
163 if (bytes
> input_len
)
164 die(_("used more bytes than were available"));
165 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
167 input_offset
+= bytes
;
169 /* make sure off_t is sufficiently large not to wrap */
170 if (signed_add_overflows(consumed_bytes
, bytes
))
171 die(_("pack too large for current definition of off_t"));
172 consumed_bytes
+= bytes
;
175 static const char *open_pack_file(const char *pack_name
)
180 static char tmp_file
[PATH_MAX
];
181 output_fd
= odb_mkstemp(tmp_file
, sizeof(tmp_file
),
182 "pack/tmp_pack_XXXXXX");
183 pack_name
= xstrdup(tmp_file
);
185 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
187 die_errno(_("unable to create '%s'"), pack_name
);
190 input_fd
= open(pack_name
, O_RDONLY
);
192 die_errno(_("cannot open packfile '%s'"), pack_name
);
196 git_SHA1_Init(&input_ctx
);
200 static void parse_pack_header(void)
202 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
204 /* Header consistency check */
205 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
206 die(_("pack signature mismatch"));
207 if (!pack_version_ok(hdr
->hdr_version
))
208 die("pack version %"PRIu32
" unsupported",
209 ntohl(hdr
->hdr_version
));
211 nr_objects
= ntohl(hdr
->hdr_entries
);
212 use(sizeof(struct pack_header
));
215 static NORETURN
void bad_object(unsigned long offset
, const char *format
,
216 ...) __attribute__((format (printf
, 2, 3)));
218 static NORETURN
void bad_object(unsigned long offset
, const char *format
, ...)
223 va_start(params
, format
);
224 vsnprintf(buf
, sizeof(buf
), format
, params
);
226 die(_("pack has bad object at offset %lu: %s"), offset
, buf
);
229 static struct base_data
*alloc_base_data(void)
231 struct base_data
*base
= xmalloc(sizeof(struct base_data
));
232 memset(base
, 0, sizeof(*base
));
238 static void free_base_data(struct base_data
*c
)
243 base_cache_used
-= c
->size
;
247 static void prune_base_data(struct base_data
*retain
)
251 base_cache_used
> delta_base_cache_limit
&& b
;
253 if (b
->data
&& b
!= retain
)
258 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
268 base_cache_used
+= c
->size
;
272 static void unlink_base_data(struct base_data
*c
)
274 struct base_data
*base
= c
->base
;
282 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
286 void *buf
= xmalloc(size
);
288 memset(&stream
, 0, sizeof(stream
));
289 git_inflate_init(&stream
);
290 stream
.next_out
= buf
;
291 stream
.avail_out
= size
;
294 stream
.next_in
= fill(1);
295 stream
.avail_in
= input_len
;
296 status
= git_inflate(&stream
, 0);
297 use(input_len
- stream
.avail_in
);
298 } while (status
== Z_OK
);
299 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
300 bad_object(offset
, _("inflate returned %d"), status
);
301 git_inflate_end(&stream
);
305 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
308 unsigned long size
, c
;
313 obj
->idx
.offset
= consumed_bytes
;
314 input_crc32
= crc32(0, NULL
, 0);
319 obj
->type
= (c
>> 4) & 7;
326 size
+= (c
& 0x7f) << shift
;
333 hashcpy(delta_base
->sha1
, fill(20));
337 memset(delta_base
, 0, sizeof(*delta_base
));
341 base_offset
= c
& 127;
344 if (!base_offset
|| MSB(base_offset
, 7))
345 bad_object(obj
->idx
.offset
, _("offset value overflow for delta base object"));
349 base_offset
= (base_offset
<< 7) + (c
& 127);
351 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
352 if (delta_base
->offset
<= 0 || delta_base
->offset
>= obj
->idx
.offset
)
353 bad_object(obj
->idx
.offset
, _("delta base offset is out of bound"));
361 bad_object(obj
->idx
.offset
, _("unknown object type %d"), obj
->type
);
363 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
365 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
366 obj
->idx
.crc32
= input_crc32
;
370 static void *get_data_from_pack(struct object_entry
*obj
)
372 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
373 unsigned long len
= obj
[1].idx
.offset
- from
;
374 unsigned char *data
, *inbuf
;
378 data
= xmalloc(obj
->size
);
379 inbuf
= xmalloc((len
< 64*1024) ? len
: 64*1024);
381 memset(&stream
, 0, sizeof(stream
));
382 git_inflate_init(&stream
);
383 stream
.next_out
= data
;
384 stream
.avail_out
= obj
->size
;
387 ssize_t n
= (len
< 64*1024) ? len
: 64*1024;
388 n
= pread(pack_fd
, inbuf
, n
, from
);
390 die_errno(_("cannot pread pack file"));
392 die(Q_("premature end of pack file, %lu byte missing",
393 "premature end of pack file, %lu bytes missing",
398 stream
.next_in
= inbuf
;
400 status
= git_inflate(&stream
, 0);
401 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
403 /* This has been inflated OK when first encountered, so... */
404 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
405 die(_("serious inflate inconsistency"));
407 git_inflate_end(&stream
);
412 static int compare_delta_bases(const union delta_base
*base1
,
413 const union delta_base
*base2
,
414 enum object_type type1
,
415 enum object_type type2
)
417 int cmp
= type1
- type2
;
420 return memcmp(base1
, base2
, UNION_BASE_SZ
);
423 static int find_delta(const union delta_base
*base
, enum object_type type
)
425 int first
= 0, last
= nr_deltas
;
427 while (first
< last
) {
428 int next
= (first
+ last
) / 2;
429 struct delta_entry
*delta
= &deltas
[next
];
432 cmp
= compare_delta_bases(base
, &delta
->base
,
433 type
, objects
[delta
->obj_no
].type
);
445 static void find_delta_children(const union delta_base
*base
,
446 int *first_index
, int *last_index
,
447 enum object_type type
)
449 int first
= find_delta(base
, type
);
451 int end
= nr_deltas
- 1;
458 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
460 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
462 *first_index
= first
;
466 static void sha1_object(const void *data
, unsigned long size
,
467 enum object_type type
, unsigned char *sha1
)
469 hash_sha1_file(data
, size
, typename(type
), sha1
);
470 if (has_sha1_file(sha1
)) {
472 enum object_type has_type
;
473 unsigned long has_size
;
474 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
476 die(_("cannot read existing object %s"), sha1_to_hex(sha1
));
477 if (size
!= has_size
|| type
!= has_type
||
478 memcmp(data
, has_data
, size
) != 0)
479 die(_("SHA1 COLLISION FOUND WITH %s !"), sha1_to_hex(sha1
));
483 if (type
== OBJ_BLOB
) {
484 struct blob
*blob
= lookup_blob(sha1
);
486 blob
->object
.flags
|= FLAG_CHECKED
;
488 die(_("invalid blob object %s"), sha1_to_hex(sha1
));
492 void *buf
= (void *) data
;
495 * we do not need to free the memory here, as the
496 * buf is deleted by the caller.
498 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
500 die(_("invalid %s"), typename(type
));
501 if (fsck_object(obj
, 1, fsck_error_function
))
502 die(_("Error in object"));
503 if (fsck_walk(obj
, mark_link
, NULL
))
504 die(_("Not all child objects of %s are reachable"), sha1_to_hex(obj
->sha1
));
506 if (obj
->type
== OBJ_TREE
) {
507 struct tree
*item
= (struct tree
*) obj
;
510 if (obj
->type
== OBJ_COMMIT
) {
511 struct commit
*commit
= (struct commit
*) obj
;
512 commit
->buffer
= NULL
;
514 obj
->flags
|= FLAG_CHECKED
;
519 static int is_delta_type(enum object_type type
)
521 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
525 * This function is part of find_unresolved_deltas(). There are two
526 * walkers going in the opposite ways.
528 * The first one in find_unresolved_deltas() traverses down from
529 * parent node to children, deflating nodes along the way. However,
530 * memory for deflated nodes is limited by delta_base_cache_limit, so
531 * at some point parent node's deflated content may be freed.
533 * The second walker is this function, which goes from current node up
534 * to top parent if necessary to deflate the node. In normal
535 * situation, its parent node would be already deflated, so it just
536 * needs to apply delta.
538 * In the worst case scenario, parent node is no longer deflated because
539 * we're running out of delta_base_cache_limit; we need to re-deflate
540 * parents, possibly up to the top base.
542 * All deflated objects here are subject to be freed if we exceed
543 * delta_base_cache_limit, just like in find_unresolved_deltas(), we
544 * just need to make sure the last node is not freed.
546 static void *get_base_data(struct base_data
*c
)
549 struct object_entry
*obj
= c
->obj
;
550 struct base_data
**delta
= NULL
;
551 int delta_nr
= 0, delta_alloc
= 0;
553 while (is_delta_type(c
->obj
->type
) && !c
->data
) {
554 ALLOC_GROW(delta
, delta_nr
+ 1, delta_alloc
);
555 delta
[delta_nr
++] = c
;
559 c
->data
= get_data_from_pack(obj
);
561 base_cache_used
+= c
->size
;
564 for (; delta_nr
> 0; delta_nr
--) {
566 c
= delta
[delta_nr
- 1];
568 base
= get_base_data(c
->base
);
569 raw
= get_data_from_pack(obj
);
570 c
->data
= patch_delta(
576 bad_object(obj
->idx
.offset
, _("failed to apply delta"));
577 base_cache_used
+= c
->size
;
585 static void resolve_delta(struct object_entry
*delta_obj
,
586 struct base_data
*base
, struct base_data
*result
)
588 void *base_data
, *delta_data
;
590 delta_obj
->real_type
= base
->obj
->real_type
;
591 delta_obj
->delta_depth
= base
->obj
->delta_depth
+ 1;
592 if (deepest_delta
< delta_obj
->delta_depth
)
593 deepest_delta
= delta_obj
->delta_depth
;
594 delta_obj
->base_object_no
= base
->obj
- objects
;
595 delta_data
= get_data_from_pack(delta_obj
);
596 base_data
= get_base_data(base
);
597 result
->obj
= delta_obj
;
598 result
->data
= patch_delta(base_data
, base
->size
,
599 delta_data
, delta_obj
->size
, &result
->size
);
602 bad_object(delta_obj
->idx
.offset
, _("failed to apply delta"));
603 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
604 delta_obj
->idx
.sha1
);
605 nr_resolved_deltas
++;
608 static struct base_data
*find_unresolved_deltas_1(struct base_data
*base
,
609 struct base_data
*prev_base
)
611 if (base
->ref_last
== -1 && base
->ofs_last
== -1) {
612 union delta_base base_spec
;
614 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
615 find_delta_children(&base_spec
,
616 &base
->ref_first
, &base
->ref_last
, OBJ_REF_DELTA
);
618 memset(&base_spec
, 0, sizeof(base_spec
));
619 base_spec
.offset
= base
->obj
->idx
.offset
;
620 find_delta_children(&base_spec
,
621 &base
->ofs_first
, &base
->ofs_last
, OBJ_OFS_DELTA
);
623 if (base
->ref_last
== -1 && base
->ofs_last
== -1) {
628 link_base_data(prev_base
, base
);
631 if (base
->ref_first
<= base
->ref_last
) {
632 struct object_entry
*child
= objects
+ deltas
[base
->ref_first
].obj_no
;
633 struct base_data
*result
= alloc_base_data();
635 assert(child
->real_type
== OBJ_REF_DELTA
);
636 resolve_delta(child
, base
, result
);
637 if (base
->ref_first
== base
->ref_last
&& base
->ofs_last
== -1)
638 free_base_data(base
);
644 if (base
->ofs_first
<= base
->ofs_last
) {
645 struct object_entry
*child
= objects
+ deltas
[base
->ofs_first
].obj_no
;
646 struct base_data
*result
= alloc_base_data();
648 assert(child
->real_type
== OBJ_OFS_DELTA
);
649 resolve_delta(child
, base
, result
);
650 if (base
->ofs_first
== base
->ofs_last
)
651 free_base_data(base
);
657 unlink_base_data(base
);
661 static void find_unresolved_deltas(struct base_data
*base
)
663 struct base_data
*new_base
, *prev_base
= NULL
;
665 new_base
= find_unresolved_deltas_1(base
, prev_base
);
675 prev_base
= base
->base
;
680 static int compare_delta_entry(const void *a
, const void *b
)
682 const struct delta_entry
*delta_a
= a
;
683 const struct delta_entry
*delta_b
= b
;
685 /* group by type (ref vs ofs) and then by value (sha-1 or offset) */
686 return compare_delta_bases(&delta_a
->base
, &delta_b
->base
,
687 objects
[delta_a
->obj_no
].type
,
688 objects
[delta_b
->obj_no
].type
);
691 /* Parse all objects and return the pack content SHA1 hash */
692 static void parse_pack_objects(unsigned char *sha1
)
695 struct delta_entry
*delta
= deltas
;
700 * - find locations of all objects;
701 * - calculate SHA1 of all non-delta objects;
702 * - remember base (SHA1 or offset) for all deltas.
705 progress
= start_progress(
706 from_stdin
? _("Receiving objects") : _("Indexing objects"),
708 for (i
= 0; i
< nr_objects
; i
++) {
709 struct object_entry
*obj
= &objects
[i
];
710 void *data
= unpack_raw_entry(obj
, &delta
->base
);
711 obj
->real_type
= obj
->type
;
712 if (is_delta_type(obj
->type
)) {
717 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
719 display_progress(progress
, i
+1);
721 objects
[i
].idx
.offset
= consumed_bytes
;
722 stop_progress(&progress
);
724 /* Check pack integrity */
726 git_SHA1_Final(sha1
, &input_ctx
);
727 if (hashcmp(fill(20), sha1
))
728 die(_("pack is corrupted (SHA1 mismatch)"));
731 /* If input_fd is a file, we should have reached its end now. */
732 if (fstat(input_fd
, &st
))
733 die_errno(_("cannot fstat packfile"));
734 if (S_ISREG(st
.st_mode
) &&
735 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
736 die(_("pack has junk at the end"));
741 /* Sort deltas by base SHA1/offset for fast searching */
742 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
743 compare_delta_entry
);
747 * - for all non-delta objects, look if it is used as a base for
749 * - if used as a base, uncompress the object and apply all deltas,
750 * recursively checking if the resulting object is used as a base
751 * for some more deltas.
754 progress
= start_progress(_("Resolving deltas"), nr_deltas
);
755 for (i
= 0; i
< nr_objects
; i
++) {
756 struct object_entry
*obj
= &objects
[i
];
757 struct base_data
*base_obj
= alloc_base_data();
759 if (is_delta_type(obj
->type
))
762 base_obj
->data
= NULL
;
763 find_unresolved_deltas(base_obj
);
764 display_progress(progress
, nr_resolved_deltas
);
768 static int write_compressed(struct sha1file
*f
, void *in
, unsigned int size
)
772 unsigned char outbuf
[4096];
774 memset(&stream
, 0, sizeof(stream
));
775 git_deflate_init(&stream
, zlib_compression_level
);
777 stream
.avail_in
= size
;
780 stream
.next_out
= outbuf
;
781 stream
.avail_out
= sizeof(outbuf
);
782 status
= git_deflate(&stream
, Z_FINISH
);
783 sha1write(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
784 } while (status
== Z_OK
);
786 if (status
!= Z_STREAM_END
)
787 die(_("unable to deflate appended object (%d)"), status
);
788 size
= stream
.total_out
;
789 git_deflate_end(&stream
);
793 static struct object_entry
*append_obj_to_pack(struct sha1file
*f
,
794 const unsigned char *sha1
, void *buf
,
795 unsigned long size
, enum object_type type
)
797 struct object_entry
*obj
= &objects
[nr_objects
++];
798 unsigned char header
[10];
799 unsigned long s
= size
;
801 unsigned char c
= (type
<< 4) | (s
& 15);
804 header
[n
++] = c
| 0x80;
810 sha1write(f
, header
, n
);
814 obj
[0].real_type
= type
;
815 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
816 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
817 obj
[0].idx
.crc32
= crc32_end(f
);
819 hashcpy(obj
->idx
.sha1
, sha1
);
823 static int delta_pos_compare(const void *_a
, const void *_b
)
825 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
826 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
827 return a
->obj_no
- b
->obj_no
;
830 static void fix_unresolved_deltas(struct sha1file
*f
, int nr_unresolved
)
832 struct delta_entry
**sorted_by_pos
;
836 * Since many unresolved deltas may well be themselves base objects
837 * for more unresolved deltas, we really want to include the
838 * smallest number of base objects that would cover as much delta
839 * as possible by picking the
840 * trunc deltas first, allowing for other deltas to resolve without
841 * additional base objects. Since most base objects are to be found
842 * before deltas depending on them, a good heuristic is to start
843 * resolving deltas in the same order as their position in the pack.
845 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
846 for (i
= 0; i
< nr_deltas
; i
++) {
847 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
849 sorted_by_pos
[n
++] = &deltas
[i
];
851 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
853 for (i
= 0; i
< n
; i
++) {
854 struct delta_entry
*d
= sorted_by_pos
[i
];
855 enum object_type type
;
856 struct base_data
*base_obj
= alloc_base_data();
858 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
860 base_obj
->data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
->size
);
864 if (check_sha1_signature(d
->base
.sha1
, base_obj
->data
,
865 base_obj
->size
, typename(type
)))
866 die(_("local object %s is corrupt"), sha1_to_hex(d
->base
.sha1
));
867 base_obj
->obj
= append_obj_to_pack(f
, d
->base
.sha1
,
868 base_obj
->data
, base_obj
->size
, type
);
869 find_unresolved_deltas(base_obj
);
870 display_progress(progress
, nr_resolved_deltas
);
875 static void final(const char *final_pack_name
, const char *curr_pack_name
,
876 const char *final_index_name
, const char *curr_index_name
,
877 const char *keep_name
, const char *keep_msg
,
880 const char *report
= "pack";
887 fsync_or_die(output_fd
, curr_pack_name
);
888 err
= close(output_fd
);
890 die_errno(_("error while closing pack file"));
894 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
897 keep_fd
= odb_pack_keep(name
, sizeof(name
), sha1
);
899 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
903 die_errno(_("cannot write keep file '%s'"),
906 if (keep_msg_len
> 0) {
907 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
908 write_or_die(keep_fd
, "\n", 1);
910 if (close(keep_fd
) != 0)
911 die_errno(_("cannot close written keep file '%s'"),
917 if (final_pack_name
!= curr_pack_name
) {
918 if (!final_pack_name
) {
919 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
920 get_object_directory(), sha1_to_hex(sha1
));
921 final_pack_name
= name
;
923 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
924 die(_("cannot store pack file"));
925 } else if (from_stdin
)
926 chmod(final_pack_name
, 0444);
928 if (final_index_name
!= curr_index_name
) {
929 if (!final_index_name
) {
930 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
931 get_object_directory(), sha1_to_hex(sha1
));
932 final_index_name
= name
;
934 if (move_temp_to_file(curr_index_name
, final_index_name
))
935 die(_("cannot store index file"));
937 chmod(final_index_name
, 0444);
940 printf("%s\n", sha1_to_hex(sha1
));
943 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
944 report
, sha1_to_hex(sha1
));
945 write_or_die(1, buf
, len
);
948 * Let's just mimic git-unpack-objects here and write
949 * the last part of the input buffer to stdout.
952 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
961 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
963 struct pack_idx_option
*opts
= cb
;
965 if (!strcmp(k
, "pack.indexversion")) {
966 opts
->version
= git_config_int(k
, v
);
967 if (opts
->version
> 2)
968 die("bad pack.indexversion=%"PRIu32
, opts
->version
);
971 return git_default_config(k
, v
, cb
);
974 static int cmp_uint32(const void *a_
, const void *b_
)
976 uint32_t a
= *((uint32_t *)a_
);
977 uint32_t b
= *((uint32_t *)b_
);
979 return (a
< b
) ? -1 : (a
!= b
);
982 static void read_v2_anomalous_offsets(struct packed_git
*p
,
983 struct pack_idx_option
*opts
)
985 const uint32_t *idx1
, *idx2
;
988 /* The address of the 4-byte offset table */
989 idx1
= (((const uint32_t *)p
->index_data
)
990 + 2 /* 8-byte header */
992 + 5 * p
->num_objects
/* 20-byte SHA-1 table */
993 + p
->num_objects
/* CRC32 table */
996 /* The address of the 8-byte offset table */
997 idx2
= idx1
+ p
->num_objects
;
999 for (i
= 0; i
< p
->num_objects
; i
++) {
1000 uint32_t off
= ntohl(idx1
[i
]);
1001 if (!(off
& 0x80000000))
1003 off
= off
& 0x7fffffff;
1007 * The real offset is ntohl(idx2[off * 2]) in high 4
1008 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1009 * octets. But idx2[off * 2] is Zero!!!
1011 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
1012 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
1015 if (1 < opts
->anomaly_nr
)
1016 qsort(opts
->anomaly
, opts
->anomaly_nr
, sizeof(uint32_t), cmp_uint32
);
1019 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
1021 struct packed_git
*p
= add_packed_git(pack_name
, strlen(pack_name
), 1);
1024 die(_("Cannot open existing pack file '%s'"), pack_name
);
1025 if (open_pack_index(p
))
1026 die(_("Cannot open existing pack idx file for '%s'"), pack_name
);
1028 /* Read the attributes from the existing idx file */
1029 opts
->version
= p
->index_version
;
1031 if (opts
->version
== 2)
1032 read_v2_anomalous_offsets(p
, opts
);
1035 * Get rid of the idx file as we do not need it anymore.
1036 * NEEDSWORK: extract this bit from free_pack_by_name() in
1037 * sha1_file.c, perhaps? It shouldn't matter very much as we
1038 * know we haven't installed this pack (hence we never have
1039 * read anything from it).
1041 close_pack_index(p
);
1045 static void show_pack_info(int stat_only
)
1047 int i
, baseobjects
= nr_objects
- nr_deltas
;
1048 unsigned long *chain_histogram
= NULL
;
1051 chain_histogram
= xcalloc(deepest_delta
, sizeof(unsigned long));
1053 for (i
= 0; i
< nr_objects
; i
++) {
1054 struct object_entry
*obj
= &objects
[i
];
1056 if (is_delta_type(obj
->type
))
1057 chain_histogram
[obj
->delta_depth
- 1]++;
1060 printf("%s %-6s %lu %lu %"PRIuMAX
,
1061 sha1_to_hex(obj
->idx
.sha1
),
1062 typename(obj
->real_type
), obj
->size
,
1063 (unsigned long)(obj
[1].idx
.offset
- obj
->idx
.offset
),
1064 (uintmax_t)obj
->idx
.offset
);
1065 if (is_delta_type(obj
->type
)) {
1066 struct object_entry
*bobj
= &objects
[obj
->base_object_no
];
1067 printf(" %u %s", obj
->delta_depth
, sha1_to_hex(bobj
->idx
.sha1
));
1073 printf_ln(Q_("non delta: %d object",
1074 "non delta: %d objects",
1077 for (i
= 0; i
< deepest_delta
; i
++) {
1078 if (!chain_histogram
[i
])
1080 printf_ln(Q_("chain length = %d: %lu object",
1081 "chain length = %d: %lu objects",
1082 chain_histogram
[i
]),
1084 chain_histogram
[i
]);
1088 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
1090 int i
, fix_thin_pack
= 0, verify
= 0, stat_only
= 0, stat
= 0;
1091 const char *curr_pack
, *curr_index
;
1092 const char *index_name
= NULL
, *pack_name
= NULL
;
1093 const char *keep_name
= NULL
, *keep_msg
= NULL
;
1094 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
1095 struct pack_idx_entry
**idx_objects
;
1096 struct pack_idx_option opts
;
1097 unsigned char pack_sha1
[20];
1099 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1100 usage(index_pack_usage
);
1102 read_replace_refs
= 0;
1104 reset_pack_idx_option(&opts
);
1105 git_config(git_index_pack_config
, &opts
);
1106 if (prefix
&& chdir(prefix
))
1107 die(_("Cannot come back to cwd"));
1109 for (i
= 1; i
< argc
; i
++) {
1110 const char *arg
= argv
[i
];
1113 if (!strcmp(arg
, "--stdin")) {
1115 } else if (!strcmp(arg
, "--fix-thin")) {
1117 } else if (!strcmp(arg
, "--strict")) {
1119 } else if (!strcmp(arg
, "--verify")) {
1121 } else if (!strcmp(arg
, "--verify-stat")) {
1124 } else if (!strcmp(arg
, "--verify-stat-only")) {
1128 } else if (!strcmp(arg
, "--keep")) {
1130 } else if (!prefixcmp(arg
, "--keep=")) {
1132 } else if (!prefixcmp(arg
, "--pack_header=")) {
1133 struct pack_header
*hdr
;
1136 hdr
= (struct pack_header
*)input_buffer
;
1137 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
1138 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
1140 die(_("bad %s"), arg
);
1141 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
1143 die(_("bad %s"), arg
);
1144 input_len
= sizeof(*hdr
);
1145 } else if (!strcmp(arg
, "-v")) {
1147 } else if (!strcmp(arg
, "-o")) {
1148 if (index_name
|| (i
+1) >= argc
)
1149 usage(index_pack_usage
);
1150 index_name
= argv
[++i
];
1151 } else if (!prefixcmp(arg
, "--index-version=")) {
1153 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1154 if (opts
.version
> 2)
1155 die(_("bad %s"), arg
);
1157 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1158 if (*c
|| opts
.off32_limit
& 0x80000000)
1159 die(_("bad %s"), arg
);
1161 usage(index_pack_usage
);
1166 usage(index_pack_usage
);
1170 if (!pack_name
&& !from_stdin
)
1171 usage(index_pack_usage
);
1172 if (fix_thin_pack
&& !from_stdin
)
1173 die(_("--fix-thin cannot be used without --stdin"));
1174 if (!index_name
&& pack_name
) {
1175 int len
= strlen(pack_name
);
1176 if (!has_extension(pack_name
, ".pack"))
1177 die(_("packfile name '%s' does not end with '.pack'"),
1179 index_name_buf
= xmalloc(len
);
1180 memcpy(index_name_buf
, pack_name
, len
- 5);
1181 strcpy(index_name_buf
+ len
- 5, ".idx");
1182 index_name
= index_name_buf
;
1184 if (keep_msg
&& !keep_name
&& pack_name
) {
1185 int len
= strlen(pack_name
);
1186 if (!has_extension(pack_name
, ".pack"))
1187 die(_("packfile name '%s' does not end with '.pack'"),
1189 keep_name_buf
= xmalloc(len
);
1190 memcpy(keep_name_buf
, pack_name
, len
- 5);
1191 strcpy(keep_name_buf
+ len
- 5, ".keep");
1192 keep_name
= keep_name_buf
;
1196 die(_("--verify with no packfile name given"));
1197 read_idx_option(&opts
, index_name
);
1198 opts
.flags
|= WRITE_IDX_VERIFY
| WRITE_IDX_STRICT
;
1201 opts
.flags
|= WRITE_IDX_STRICT
;
1203 curr_pack
= open_pack_file(pack_name
);
1204 parse_pack_header();
1205 objects
= xcalloc(nr_objects
+ 1, sizeof(struct object_entry
));
1206 deltas
= xcalloc(nr_objects
, sizeof(struct delta_entry
));
1207 parse_pack_objects(pack_sha1
);
1208 if (nr_deltas
== nr_resolved_deltas
) {
1209 stop_progress(&progress
);
1210 /* Flush remaining pack final 20-byte SHA1. */
1213 if (fix_thin_pack
) {
1215 unsigned char read_sha1
[20], tail_sha1
[20];
1217 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
1218 int nr_objects_initial
= nr_objects
;
1219 if (nr_unresolved
<= 0)
1220 die(_("confusion beyond insanity"));
1221 objects
= xrealloc(objects
,
1222 (nr_objects
+ nr_unresolved
+ 1)
1223 * sizeof(*objects
));
1224 f
= sha1fd(output_fd
, curr_pack
);
1225 fix_unresolved_deltas(f
, nr_unresolved
);
1226 sprintf(msg
, "completed with %d local objects",
1227 nr_objects
- nr_objects_initial
);
1228 stop_progress_msg(&progress
, msg
);
1229 sha1close(f
, tail_sha1
, 0);
1230 hashcpy(read_sha1
, pack_sha1
);
1231 fixup_pack_header_footer(output_fd
, pack_sha1
,
1232 curr_pack
, nr_objects
,
1233 read_sha1
, consumed_bytes
-20);
1234 if (hashcmp(read_sha1
, tail_sha1
) != 0)
1235 die("Unexpected tail checksum for %s "
1236 "(disk corruption?)", curr_pack
);
1238 if (nr_deltas
!= nr_resolved_deltas
)
1239 die(Q_("pack has %d unresolved delta",
1240 "pack has %d unresolved deltas",
1241 nr_deltas
- nr_resolved_deltas
),
1242 nr_deltas
- nr_resolved_deltas
);
1249 show_pack_info(stat_only
);
1251 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
1252 for (i
= 0; i
< nr_objects
; i
++)
1253 idx_objects
[i
] = &objects
[i
].idx
;
1254 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, &opts
, pack_sha1
);
1258 final(pack_name
, curr_pack
,
1259 index_name
, curr_index
,
1260 keep_name
, keep_msg
,
1265 free(index_name_buf
);
1266 free(keep_name_buf
);
1267 if (pack_name
== NULL
)
1268 free((void *) curr_pack
);
1269 if (index_name
== NULL
)
1270 free((void *) curr_index
);