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
;
40 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
41 * to memcmp() only the first 20 bytes.
43 #define UNION_BASE_SZ 20
45 #define FLAG_LINK (1u<<20)
46 #define FLAG_CHECKED (1u<<21)
49 union delta_base base
;
53 static struct object_entry
*objects
;
54 static struct delta_entry
*deltas
;
55 static struct base_data
*base_cache
;
56 static size_t base_cache_used
;
57 static int nr_objects
;
59 static int nr_resolved_deltas
;
61 static int from_stdin
;
65 static struct progress
*progress
;
67 /* We always read in 4kB chunks. */
68 static unsigned char input_buffer
[4096];
69 static unsigned int input_offset
, input_len
;
70 static off_t consumed_bytes
;
71 static unsigned deepest_delta
;
72 static git_SHA_CTX input_ctx
;
73 static uint32_t input_crc32
;
74 static int input_fd
, output_fd
, pack_fd
;
76 static int mark_link(struct object
*obj
, int type
, void *data
)
81 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
82 die("object type mismatch at %s", sha1_to_hex(obj
->sha1
));
84 obj
->flags
|= FLAG_LINK
;
88 /* The content of each linked object must have been checked
89 or it must be already present in the object database */
90 static void check_object(struct object
*obj
)
95 if (!(obj
->flags
& FLAG_LINK
))
98 if (!(obj
->flags
& FLAG_CHECKED
)) {
100 int type
= sha1_object_info(obj
->sha1
, &size
);
101 if (type
!= obj
->type
|| type
<= 0)
102 die("object of unexpected type");
103 obj
->flags
|= FLAG_CHECKED
;
108 static void check_objects(void)
112 max
= get_max_object_index();
113 for (i
= 0; i
< max
; i
++)
114 check_object(get_indexed_object(i
));
118 /* Discard current buffer used content. */
119 static void flush(void)
123 write_or_die(output_fd
, input_buffer
, input_offset
);
124 git_SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
125 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
131 * Make sure at least "min" bytes are available in the buffer, and
132 * return the pointer to the buffer.
134 static void *fill(int min
)
136 if (min
<= input_len
)
137 return input_buffer
+ input_offset
;
138 if (min
> sizeof(input_buffer
))
139 die("cannot fill %d bytes", min
);
142 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
143 sizeof(input_buffer
) - input_len
);
147 die_errno("read error on input");
151 display_throughput(progress
, consumed_bytes
+ input_len
);
152 } while (input_len
< min
);
156 static void use(int bytes
)
158 if (bytes
> input_len
)
159 die("used more bytes than were available");
160 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
162 input_offset
+= bytes
;
164 /* make sure off_t is sufficiently large not to wrap */
165 if (signed_add_overflows(consumed_bytes
, bytes
))
166 die("pack too large for current definition of off_t");
167 consumed_bytes
+= bytes
;
170 static const char *open_pack_file(const char *pack_name
)
175 static char tmpfile
[PATH_MAX
];
176 output_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
177 "pack/tmp_pack_XXXXXX");
178 pack_name
= xstrdup(tmpfile
);
180 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
182 die_errno("unable to create '%s'", pack_name
);
185 input_fd
= open(pack_name
, O_RDONLY
);
187 die_errno("cannot open packfile '%s'", pack_name
);
191 git_SHA1_Init(&input_ctx
);
195 static void parse_pack_header(void)
197 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
199 /* Header consistency check */
200 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
201 die("pack signature mismatch");
202 if (!pack_version_ok(hdr
->hdr_version
))
203 die("pack version %"PRIu32
" unsupported",
204 ntohl(hdr
->hdr_version
));
206 nr_objects
= ntohl(hdr
->hdr_entries
);
207 use(sizeof(struct pack_header
));
210 static NORETURN
void bad_object(unsigned long offset
, const char *format
,
211 ...) __attribute__((format (printf
, 2, 3)));
213 static NORETURN
void bad_object(unsigned long offset
, const char *format
, ...)
218 va_start(params
, format
);
219 vsnprintf(buf
, sizeof(buf
), format
, params
);
221 die("pack has bad object at offset %lu: %s", offset
, buf
);
224 static void free_base_data(struct base_data
*c
)
229 base_cache_used
-= c
->size
;
233 static void prune_base_data(struct base_data
*retain
)
237 base_cache_used
> delta_base_cache_limit
&& b
;
239 if (b
->data
&& b
!= retain
)
244 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
254 base_cache_used
+= c
->size
;
258 static void unlink_base_data(struct base_data
*c
)
260 struct base_data
*base
= c
->base
;
268 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
272 void *buf
= xmalloc(size
);
274 memset(&stream
, 0, sizeof(stream
));
275 git_inflate_init(&stream
);
276 stream
.next_out
= buf
;
277 stream
.avail_out
= size
;
280 stream
.next_in
= fill(1);
281 stream
.avail_in
= input_len
;
282 status
= git_inflate(&stream
, 0);
283 use(input_len
- stream
.avail_in
);
284 } while (status
== Z_OK
);
285 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
286 bad_object(offset
, "inflate returned %d", status
);
287 git_inflate_end(&stream
);
291 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
294 unsigned long size
, c
;
299 obj
->idx
.offset
= consumed_bytes
;
300 input_crc32
= crc32(0, NULL
, 0);
305 obj
->type
= (c
>> 4) & 7;
312 size
+= (c
& 0x7f) << shift
;
319 hashcpy(delta_base
->sha1
, fill(20));
323 memset(delta_base
, 0, sizeof(*delta_base
));
327 base_offset
= c
& 127;
330 if (!base_offset
|| MSB(base_offset
, 7))
331 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
335 base_offset
= (base_offset
<< 7) + (c
& 127);
337 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
338 if (delta_base
->offset
<= 0 || delta_base
->offset
>= obj
->idx
.offset
)
339 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
347 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
349 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
351 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
352 obj
->idx
.crc32
= input_crc32
;
356 static void *get_data_from_pack(struct object_entry
*obj
)
358 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
359 unsigned long len
= obj
[1].idx
.offset
- from
;
360 unsigned char *data
, *inbuf
;
364 data
= xmalloc(obj
->size
);
365 inbuf
= xmalloc((len
< 64*1024) ? len
: 64*1024);
367 memset(&stream
, 0, sizeof(stream
));
368 git_inflate_init(&stream
);
369 stream
.next_out
= data
;
370 stream
.avail_out
= obj
->size
;
373 ssize_t n
= (len
< 64*1024) ? len
: 64*1024;
374 n
= pread(pack_fd
, inbuf
, n
, from
);
376 die_errno("cannot pread pack file");
378 die("premature end of pack file, %lu bytes missing", len
);
381 stream
.next_in
= inbuf
;
383 status
= git_inflate(&stream
, 0);
384 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
386 /* This has been inflated OK when first encountered, so... */
387 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
388 die("serious inflate inconsistency");
390 git_inflate_end(&stream
);
395 static int compare_delta_bases(const union delta_base
*base1
,
396 const union delta_base
*base2
,
397 enum object_type type1
,
398 enum object_type type2
)
400 int cmp
= type1
- type2
;
403 return memcmp(base1
, base2
, UNION_BASE_SZ
);
406 static int find_delta(const union delta_base
*base
, enum object_type type
)
408 int first
= 0, last
= nr_deltas
;
410 while (first
< last
) {
411 int next
= (first
+ last
) / 2;
412 struct delta_entry
*delta
= &deltas
[next
];
415 cmp
= compare_delta_bases(base
, &delta
->base
,
416 type
, objects
[delta
->obj_no
].type
);
428 static void find_delta_children(const union delta_base
*base
,
429 int *first_index
, int *last_index
,
430 enum object_type type
)
432 int first
= find_delta(base
, type
);
434 int end
= nr_deltas
- 1;
441 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
443 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
445 *first_index
= first
;
449 static void sha1_object(const void *data
, unsigned long size
,
450 enum object_type type
, unsigned char *sha1
)
452 hash_sha1_file(data
, size
, typename(type
), sha1
);
453 if (has_sha1_file(sha1
)) {
455 enum object_type has_type
;
456 unsigned long has_size
;
457 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
459 die("cannot read existing object %s", sha1_to_hex(sha1
));
460 if (size
!= has_size
|| type
!= has_type
||
461 memcmp(data
, has_data
, size
) != 0)
462 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
466 if (type
== OBJ_BLOB
) {
467 struct blob
*blob
= lookup_blob(sha1
);
469 blob
->object
.flags
|= FLAG_CHECKED
;
471 die("invalid blob object %s", sha1_to_hex(sha1
));
475 void *buf
= (void *) data
;
478 * we do not need to free the memory here, as the
479 * buf is deleted by the caller.
481 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
483 die("invalid %s", typename(type
));
484 if (fsck_object(obj
, 1, fsck_error_function
))
485 die("Error in object");
486 if (fsck_walk(obj
, mark_link
, NULL
))
487 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
489 if (obj
->type
== OBJ_TREE
) {
490 struct tree
*item
= (struct tree
*) obj
;
493 if (obj
->type
== OBJ_COMMIT
) {
494 struct commit
*commit
= (struct commit
*) obj
;
495 commit
->buffer
= NULL
;
497 obj
->flags
|= FLAG_CHECKED
;
502 static int is_delta_type(enum object_type type
)
504 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
507 static void *get_base_data(struct base_data
*c
)
510 struct object_entry
*obj
= c
->obj
;
512 if (is_delta_type(obj
->type
)) {
513 void *base
= get_base_data(c
->base
);
514 void *raw
= get_data_from_pack(obj
);
515 c
->data
= patch_delta(
521 bad_object(obj
->idx
.offset
, "failed to apply delta");
523 c
->data
= get_data_from_pack(obj
);
527 base_cache_used
+= c
->size
;
533 static void resolve_delta(struct object_entry
*delta_obj
,
534 struct base_data
*base
, struct base_data
*result
)
536 void *base_data
, *delta_data
;
538 delta_obj
->real_type
= base
->obj
->real_type
;
539 delta_obj
->delta_depth
= base
->obj
->delta_depth
+ 1;
540 if (deepest_delta
< delta_obj
->delta_depth
)
541 deepest_delta
= delta_obj
->delta_depth
;
542 delta_obj
->base_object_no
= base
->obj
- objects
;
543 delta_data
= get_data_from_pack(delta_obj
);
544 base_data
= get_base_data(base
);
545 result
->obj
= delta_obj
;
546 result
->data
= patch_delta(base_data
, base
->size
,
547 delta_data
, delta_obj
->size
, &result
->size
);
550 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
551 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
552 delta_obj
->idx
.sha1
);
553 nr_resolved_deltas
++;
556 static void find_unresolved_deltas(struct base_data
*base
,
557 struct base_data
*prev_base
)
559 int i
, ref_first
, ref_last
, ofs_first
, ofs_last
;
562 * This is a recursive function. Those brackets should help reducing
563 * stack usage by limiting the scope of the delta_base union.
566 union delta_base base_spec
;
568 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
569 find_delta_children(&base_spec
,
570 &ref_first
, &ref_last
, OBJ_REF_DELTA
);
572 memset(&base_spec
, 0, sizeof(base_spec
));
573 base_spec
.offset
= base
->obj
->idx
.offset
;
574 find_delta_children(&base_spec
,
575 &ofs_first
, &ofs_last
, OBJ_OFS_DELTA
);
578 if (ref_last
== -1 && ofs_last
== -1) {
583 link_base_data(prev_base
, base
);
585 for (i
= ref_first
; i
<= ref_last
; i
++) {
586 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
587 struct base_data result
;
589 assert(child
->real_type
== OBJ_REF_DELTA
);
590 resolve_delta(child
, base
, &result
);
591 if (i
== ref_last
&& ofs_last
== -1)
592 free_base_data(base
);
593 find_unresolved_deltas(&result
, base
);
596 for (i
= ofs_first
; i
<= ofs_last
; i
++) {
597 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
598 struct base_data result
;
600 assert(child
->real_type
== OBJ_OFS_DELTA
);
601 resolve_delta(child
, base
, &result
);
603 free_base_data(base
);
604 find_unresolved_deltas(&result
, base
);
607 unlink_base_data(base
);
610 static int compare_delta_entry(const void *a
, const void *b
)
612 const struct delta_entry
*delta_a
= a
;
613 const struct delta_entry
*delta_b
= b
;
615 /* group by type (ref vs ofs) and then by value (sha-1 or offset) */
616 return compare_delta_bases(&delta_a
->base
, &delta_b
->base
,
617 objects
[delta_a
->obj_no
].type
,
618 objects
[delta_b
->obj_no
].type
);
621 /* Parse all objects and return the pack content SHA1 hash */
622 static void parse_pack_objects(unsigned char *sha1
)
625 struct delta_entry
*delta
= deltas
;
630 * - find locations of all objects;
631 * - calculate SHA1 of all non-delta objects;
632 * - remember base (SHA1 or offset) for all deltas.
635 progress
= start_progress(
636 from_stdin
? "Receiving objects" : "Indexing objects",
638 for (i
= 0; i
< nr_objects
; i
++) {
639 struct object_entry
*obj
= &objects
[i
];
640 void *data
= unpack_raw_entry(obj
, &delta
->base
);
641 obj
->real_type
= obj
->type
;
642 if (is_delta_type(obj
->type
)) {
647 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
649 display_progress(progress
, i
+1);
651 objects
[i
].idx
.offset
= consumed_bytes
;
652 stop_progress(&progress
);
654 /* Check pack integrity */
656 git_SHA1_Final(sha1
, &input_ctx
);
657 if (hashcmp(fill(20), sha1
))
658 die("pack is corrupted (SHA1 mismatch)");
661 /* If input_fd is a file, we should have reached its end now. */
662 if (fstat(input_fd
, &st
))
663 die_errno("cannot fstat packfile");
664 if (S_ISREG(st
.st_mode
) &&
665 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
666 die("pack has junk at the end");
671 /* Sort deltas by base SHA1/offset for fast searching */
672 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
673 compare_delta_entry
);
677 * - for all non-delta objects, look if it is used as a base for
679 * - if used as a base, uncompress the object and apply all deltas,
680 * recursively checking if the resulting object is used as a base
681 * for some more deltas.
684 progress
= start_progress("Resolving deltas", nr_deltas
);
685 for (i
= 0; i
< nr_objects
; i
++) {
686 struct object_entry
*obj
= &objects
[i
];
687 struct base_data base_obj
;
689 if (is_delta_type(obj
->type
))
692 base_obj
.data
= NULL
;
693 find_unresolved_deltas(&base_obj
, NULL
);
694 display_progress(progress
, nr_resolved_deltas
);
698 static int write_compressed(struct sha1file
*f
, void *in
, unsigned int size
)
702 unsigned char outbuf
[4096];
704 memset(&stream
, 0, sizeof(stream
));
705 git_deflate_init(&stream
, zlib_compression_level
);
707 stream
.avail_in
= size
;
710 stream
.next_out
= outbuf
;
711 stream
.avail_out
= sizeof(outbuf
);
712 status
= git_deflate(&stream
, Z_FINISH
);
713 sha1write(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
714 } while (status
== Z_OK
);
716 if (status
!= Z_STREAM_END
)
717 die("unable to deflate appended object (%d)", status
);
718 size
= stream
.total_out
;
719 git_deflate_end(&stream
);
723 static struct object_entry
*append_obj_to_pack(struct sha1file
*f
,
724 const unsigned char *sha1
, void *buf
,
725 unsigned long size
, enum object_type type
)
727 struct object_entry
*obj
= &objects
[nr_objects
++];
728 unsigned char header
[10];
729 unsigned long s
= size
;
731 unsigned char c
= (type
<< 4) | (s
& 15);
734 header
[n
++] = c
| 0x80;
740 sha1write(f
, header
, n
);
744 obj
[0].real_type
= type
;
745 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
746 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
747 obj
[0].idx
.crc32
= crc32_end(f
);
749 hashcpy(obj
->idx
.sha1
, sha1
);
753 static int delta_pos_compare(const void *_a
, const void *_b
)
755 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
756 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
757 return a
->obj_no
- b
->obj_no
;
760 static void fix_unresolved_deltas(struct sha1file
*f
, int nr_unresolved
)
762 struct delta_entry
**sorted_by_pos
;
766 * Since many unresolved deltas may well be themselves base objects
767 * for more unresolved deltas, we really want to include the
768 * smallest number of base objects that would cover as much delta
769 * as possible by picking the
770 * trunc deltas first, allowing for other deltas to resolve without
771 * additional base objects. Since most base objects are to be found
772 * before deltas depending on them, a good heuristic is to start
773 * resolving deltas in the same order as their position in the pack.
775 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
776 for (i
= 0; i
< nr_deltas
; i
++) {
777 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
779 sorted_by_pos
[n
++] = &deltas
[i
];
781 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
783 for (i
= 0; i
< n
; i
++) {
784 struct delta_entry
*d
= sorted_by_pos
[i
];
785 enum object_type type
;
786 struct base_data base_obj
;
788 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
790 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
794 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
795 base_obj
.size
, typename(type
)))
796 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
797 base_obj
.obj
= append_obj_to_pack(f
, d
->base
.sha1
,
798 base_obj
.data
, base_obj
.size
, type
);
799 find_unresolved_deltas(&base_obj
, NULL
);
800 display_progress(progress
, nr_resolved_deltas
);
805 static void final(const char *final_pack_name
, const char *curr_pack_name
,
806 const char *final_index_name
, const char *curr_index_name
,
807 const char *keep_name
, const char *keep_msg
,
810 const char *report
= "pack";
817 fsync_or_die(output_fd
, curr_pack_name
);
818 err
= close(output_fd
);
820 die_errno("error while closing pack file");
824 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
827 keep_fd
= odb_pack_keep(name
, sizeof(name
), sha1
);
829 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
833 die_errno("cannot write keep file '%s'",
836 if (keep_msg_len
> 0) {
837 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
838 write_or_die(keep_fd
, "\n", 1);
840 if (close(keep_fd
) != 0)
841 die_errno("cannot close written keep file '%s'",
847 if (final_pack_name
!= curr_pack_name
) {
848 if (!final_pack_name
) {
849 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
850 get_object_directory(), sha1_to_hex(sha1
));
851 final_pack_name
= name
;
853 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
854 die("cannot store pack file");
855 } else if (from_stdin
)
856 chmod(final_pack_name
, 0444);
858 if (final_index_name
!= curr_index_name
) {
859 if (!final_index_name
) {
860 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
861 get_object_directory(), sha1_to_hex(sha1
));
862 final_index_name
= name
;
864 if (move_temp_to_file(curr_index_name
, final_index_name
))
865 die("cannot store index file");
867 chmod(final_index_name
, 0444);
870 printf("%s\n", sha1_to_hex(sha1
));
873 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
874 report
, sha1_to_hex(sha1
));
875 write_or_die(1, buf
, len
);
878 * Let's just mimic git-unpack-objects here and write
879 * the last part of the input buffer to stdout.
882 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
891 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
893 struct pack_idx_option
*opts
= cb
;
895 if (!strcmp(k
, "pack.indexversion")) {
896 opts
->version
= git_config_int(k
, v
);
897 if (opts
->version
> 2)
898 die("bad pack.indexversion=%"PRIu32
, opts
->version
);
901 return git_default_config(k
, v
, cb
);
904 static int cmp_uint32(const void *a_
, const void *b_
)
906 uint32_t a
= *((uint32_t *)a_
);
907 uint32_t b
= *((uint32_t *)b_
);
909 return (a
< b
) ? -1 : (a
!= b
);
912 static void read_v2_anomalous_offsets(struct packed_git
*p
,
913 struct pack_idx_option
*opts
)
915 const uint32_t *idx1
, *idx2
;
918 /* The address of the 4-byte offset table */
919 idx1
= (((const uint32_t *)p
->index_data
)
920 + 2 /* 8-byte header */
922 + 5 * p
->num_objects
/* 20-byte SHA-1 table */
923 + p
->num_objects
/* CRC32 table */
926 /* The address of the 8-byte offset table */
927 idx2
= idx1
+ p
->num_objects
;
929 for (i
= 0; i
< p
->num_objects
; i
++) {
930 uint32_t off
= ntohl(idx1
[i
]);
931 if (!(off
& 0x80000000))
933 off
= off
& 0x7fffffff;
937 * The real offset is ntohl(idx2[off * 2]) in high 4
938 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
939 * octets. But idx2[off * 2] is Zero!!!
941 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
942 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
945 if (1 < opts
->anomaly_nr
)
946 qsort(opts
->anomaly
, opts
->anomaly_nr
, sizeof(uint32_t), cmp_uint32
);
949 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
951 struct packed_git
*p
= add_packed_git(pack_name
, strlen(pack_name
), 1);
954 die("Cannot open existing pack file '%s'", pack_name
);
955 if (open_pack_index(p
))
956 die("Cannot open existing pack idx file for '%s'", pack_name
);
958 /* Read the attributes from the existing idx file */
959 opts
->version
= p
->index_version
;
961 if (opts
->version
== 2)
962 read_v2_anomalous_offsets(p
, opts
);
965 * Get rid of the idx file as we do not need it anymore.
966 * NEEDSWORK: extract this bit from free_pack_by_name() in
967 * sha1_file.c, perhaps? It shouldn't matter very much as we
968 * know we haven't installed this pack (hence we never have
969 * read anything from it).
975 static void show_pack_info(int stat_only
)
977 int i
, baseobjects
= nr_objects
- nr_deltas
;
978 unsigned long *chain_histogram
= NULL
;
981 chain_histogram
= xcalloc(deepest_delta
, sizeof(unsigned long));
983 for (i
= 0; i
< nr_objects
; i
++) {
984 struct object_entry
*obj
= &objects
[i
];
986 if (is_delta_type(obj
->type
))
987 chain_histogram
[obj
->delta_depth
- 1]++;
990 printf("%s %-6s %lu %lu %"PRIuMAX
,
991 sha1_to_hex(obj
->idx
.sha1
),
992 typename(obj
->real_type
), obj
->size
,
993 (unsigned long)(obj
[1].idx
.offset
- obj
->idx
.offset
),
994 (uintmax_t)obj
->idx
.offset
);
995 if (is_delta_type(obj
->type
)) {
996 struct object_entry
*bobj
= &objects
[obj
->base_object_no
];
997 printf(" %u %s", obj
->delta_depth
, sha1_to_hex(bobj
->idx
.sha1
));
1003 printf("non delta: %d object%s\n",
1004 baseobjects
, baseobjects
> 1 ? "s" : "");
1005 for (i
= 0; i
< deepest_delta
; i
++) {
1006 if (!chain_histogram
[i
])
1008 printf("chain length = %d: %lu object%s\n",
1011 chain_histogram
[i
] > 1 ? "s" : "");
1015 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
1017 int i
, fix_thin_pack
= 0, verify
= 0, stat_only
= 0, stat
= 0;
1018 const char *curr_pack
, *curr_index
;
1019 const char *index_name
= NULL
, *pack_name
= NULL
;
1020 const char *keep_name
= NULL
, *keep_msg
= NULL
;
1021 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
1022 struct pack_idx_entry
**idx_objects
;
1023 struct pack_idx_option opts
;
1024 unsigned char pack_sha1
[20];
1026 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1027 usage(index_pack_usage
);
1029 read_replace_refs
= 0;
1031 reset_pack_idx_option(&opts
);
1032 git_config(git_index_pack_config
, &opts
);
1033 if (prefix
&& chdir(prefix
))
1034 die("Cannot come back to cwd");
1036 for (i
= 1; i
< argc
; i
++) {
1037 const char *arg
= argv
[i
];
1040 if (!strcmp(arg
, "--stdin")) {
1042 } else if (!strcmp(arg
, "--fix-thin")) {
1044 } else if (!strcmp(arg
, "--strict")) {
1046 } else if (!strcmp(arg
, "--verify")) {
1048 } else if (!strcmp(arg
, "--verify-stat")) {
1051 } else if (!strcmp(arg
, "--verify-stat-only")) {
1055 } else if (!strcmp(arg
, "--keep")) {
1057 } else if (!prefixcmp(arg
, "--keep=")) {
1059 } else if (!prefixcmp(arg
, "--pack_header=")) {
1060 struct pack_header
*hdr
;
1063 hdr
= (struct pack_header
*)input_buffer
;
1064 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
1065 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
1068 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
1071 input_len
= sizeof(*hdr
);
1072 } else if (!strcmp(arg
, "-v")) {
1074 } else if (!strcmp(arg
, "-o")) {
1075 if (index_name
|| (i
+1) >= argc
)
1076 usage(index_pack_usage
);
1077 index_name
= argv
[++i
];
1078 } else if (!prefixcmp(arg
, "--index-version=")) {
1080 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1081 if (opts
.version
> 2)
1084 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1085 if (*c
|| opts
.off32_limit
& 0x80000000)
1088 usage(index_pack_usage
);
1093 usage(index_pack_usage
);
1097 if (!pack_name
&& !from_stdin
)
1098 usage(index_pack_usage
);
1099 if (fix_thin_pack
&& !from_stdin
)
1100 die("--fix-thin cannot be used without --stdin");
1101 if (!index_name
&& pack_name
) {
1102 int len
= strlen(pack_name
);
1103 if (!has_extension(pack_name
, ".pack"))
1104 die("packfile name '%s' does not end with '.pack'",
1106 index_name_buf
= xmalloc(len
);
1107 memcpy(index_name_buf
, pack_name
, len
- 5);
1108 strcpy(index_name_buf
+ len
- 5, ".idx");
1109 index_name
= index_name_buf
;
1111 if (keep_msg
&& !keep_name
&& pack_name
) {
1112 int len
= strlen(pack_name
);
1113 if (!has_extension(pack_name
, ".pack"))
1114 die("packfile name '%s' does not end with '.pack'",
1116 keep_name_buf
= xmalloc(len
);
1117 memcpy(keep_name_buf
, pack_name
, len
- 5);
1118 strcpy(keep_name_buf
+ len
- 5, ".keep");
1119 keep_name
= keep_name_buf
;
1123 die("--verify with no packfile name given");
1124 read_idx_option(&opts
, index_name
);
1125 opts
.flags
|= WRITE_IDX_VERIFY
| WRITE_IDX_STRICT
;
1128 opts
.flags
|= WRITE_IDX_STRICT
;
1130 curr_pack
= open_pack_file(pack_name
);
1131 parse_pack_header();
1132 objects
= xcalloc(nr_objects
+ 1, sizeof(struct object_entry
));
1133 deltas
= xcalloc(nr_objects
, sizeof(struct delta_entry
));
1134 parse_pack_objects(pack_sha1
);
1135 if (nr_deltas
== nr_resolved_deltas
) {
1136 stop_progress(&progress
);
1137 /* Flush remaining pack final 20-byte SHA1. */
1140 if (fix_thin_pack
) {
1142 unsigned char read_sha1
[20], tail_sha1
[20];
1144 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
1145 int nr_objects_initial
= nr_objects
;
1146 if (nr_unresolved
<= 0)
1147 die("confusion beyond insanity");
1148 objects
= xrealloc(objects
,
1149 (nr_objects
+ nr_unresolved
+ 1)
1150 * sizeof(*objects
));
1151 f
= sha1fd(output_fd
, curr_pack
);
1152 fix_unresolved_deltas(f
, nr_unresolved
);
1153 sprintf(msg
, "completed with %d local objects",
1154 nr_objects
- nr_objects_initial
);
1155 stop_progress_msg(&progress
, msg
);
1156 sha1close(f
, tail_sha1
, 0);
1157 hashcpy(read_sha1
, pack_sha1
);
1158 fixup_pack_header_footer(output_fd
, pack_sha1
,
1159 curr_pack
, nr_objects
,
1160 read_sha1
, consumed_bytes
-20);
1161 if (hashcmp(read_sha1
, tail_sha1
) != 0)
1162 die("Unexpected tail checksum for %s "
1163 "(disk corruption?)", curr_pack
);
1165 if (nr_deltas
!= nr_resolved_deltas
)
1166 die("pack has %d unresolved deltas",
1167 nr_deltas
- nr_resolved_deltas
);
1174 show_pack_info(stat_only
);
1176 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
1177 for (i
= 0; i
< nr_objects
; i
++)
1178 idx_objects
[i
] = &objects
[i
].idx
;
1179 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, &opts
, pack_sha1
);
1183 final(pack_name
, curr_pack
,
1184 index_name
, curr_index
,
1185 keep_name
, keep_msg
,
1190 free(index_name_buf
);
1191 free(keep_name_buf
);
1192 if (pack_name
== NULL
)
1193 free((void *) curr_pack
);
1194 if (index_name
== NULL
)
1195 free((void *) curr_index
);