]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/index-pack.c
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>])";
18 struct pack_idx_entry idx
;
20 unsigned int hdr_size
;
21 enum object_type type
;
22 enum object_type real_type
;
28 unsigned char sha1
[20];
33 struct base_data
*base
;
34 struct base_data
*child
;
35 struct object_entry
*obj
;
41 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
42 * to memcmp() only the first 20 bytes.
44 #define UNION_BASE_SZ 20
46 #define FLAG_LINK (1u<<20)
47 #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("cannot fill %d bytes", min
);
144 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
145 sizeof(input_buffer
) - input_len
);
149 die_errno("read error on input");
153 display_throughput(progress
, consumed_bytes
+ input_len
);
154 } while (input_len
< min
);
158 static void use(int bytes
)
160 if (bytes
> input_len
)
161 die("used more bytes than were available");
162 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
164 input_offset
+= bytes
;
166 /* make sure off_t is sufficiently large not to wrap */
167 if (signed_add_overflows(consumed_bytes
, bytes
))
168 die("pack too large for current definition of off_t");
169 consumed_bytes
+= bytes
;
172 static const char *open_pack_file(const char *pack_name
)
177 static char tmpfile
[PATH_MAX
];
178 output_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
179 "pack/tmp_pack_XXXXXX");
180 pack_name
= xstrdup(tmpfile
);
182 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
184 die_errno("unable to create '%s'", pack_name
);
187 input_fd
= open(pack_name
, O_RDONLY
);
189 die_errno("cannot open packfile '%s'", pack_name
);
193 git_SHA1_Init(&input_ctx
);
197 static void parse_pack_header(void)
199 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
201 /* Header consistency check */
202 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
203 die("pack signature mismatch");
204 if (!pack_version_ok(hdr
->hdr_version
))
205 die("pack version %"PRIu32
" unsupported",
206 ntohl(hdr
->hdr_version
));
208 nr_objects
= ntohl(hdr
->hdr_entries
);
209 use(sizeof(struct pack_header
));
212 static NORETURN
void bad_object(unsigned long offset
, const char *format
,
213 ...) __attribute__((format (printf
, 2, 3)));
215 static void bad_object(unsigned long offset
, const char *format
, ...)
220 va_start(params
, format
);
221 vsnprintf(buf
, sizeof(buf
), format
, params
);
223 die("pack has bad object at offset %lu: %s", offset
, buf
);
226 static void free_base_data(struct base_data
*c
)
231 base_cache_used
-= c
->size
;
235 static void prune_base_data(struct base_data
*retain
)
239 base_cache_used
> delta_base_cache_limit
&& b
;
241 if (b
->data
&& b
!= retain
)
246 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
256 base_cache_used
+= c
->size
;
260 static void unlink_base_data(struct base_data
*c
)
262 struct base_data
*base
= c
->base
;
270 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
274 void *buf
= xmalloc(size
);
276 memset(&stream
, 0, sizeof(stream
));
277 git_inflate_init(&stream
);
278 stream
.next_out
= buf
;
279 stream
.avail_out
= size
;
282 stream
.next_in
= fill(1);
283 stream
.avail_in
= input_len
;
284 status
= git_inflate(&stream
, 0);
285 use(input_len
- stream
.avail_in
);
286 } while (status
== Z_OK
);
287 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
288 bad_object(offset
, "inflate returned %d", status
);
289 git_inflate_end(&stream
);
293 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
296 unsigned long size
, c
;
301 obj
->idx
.offset
= consumed_bytes
;
302 input_crc32
= crc32(0, Z_NULL
, 0);
307 obj
->type
= (c
>> 4) & 7;
314 size
+= (c
& 0x7f) << shift
;
321 hashcpy(delta_base
->sha1
, fill(20));
325 memset(delta_base
, 0, sizeof(*delta_base
));
329 base_offset
= c
& 127;
332 if (!base_offset
|| MSB(base_offset
, 7))
333 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
337 base_offset
= (base_offset
<< 7) + (c
& 127);
339 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
340 if (delta_base
->offset
<= 0 || delta_base
->offset
>= obj
->idx
.offset
)
341 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
349 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
351 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
353 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
354 obj
->idx
.crc32
= input_crc32
;
358 static void *get_data_from_pack(struct object_entry
*obj
)
360 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
361 unsigned long len
= obj
[1].idx
.offset
- from
;
362 unsigned char *data
, *inbuf
;
366 data
= xmalloc(obj
->size
);
367 inbuf
= xmalloc((len
< 64*1024) ? len
: 64*1024);
369 memset(&stream
, 0, sizeof(stream
));
370 git_inflate_init(&stream
);
371 stream
.next_out
= data
;
372 stream
.avail_out
= obj
->size
;
375 ssize_t n
= (len
< 64*1024) ? len
: 64*1024;
376 n
= pread(pack_fd
, inbuf
, n
, from
);
378 die_errno("cannot pread pack file");
380 die("premature end of pack file, %lu bytes missing", len
);
383 stream
.next_in
= inbuf
;
385 status
= git_inflate(&stream
, 0);
386 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
388 /* This has been inflated OK when first encountered, so... */
389 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
390 die("serious inflate inconsistency");
392 git_inflate_end(&stream
);
397 static int compare_delta_bases(const union delta_base
*base1
,
398 const union delta_base
*base2
,
399 enum object_type type1
,
400 enum object_type type2
)
402 int cmp
= type1
- type2
;
405 return memcmp(base1
, base2
, UNION_BASE_SZ
);
408 static int find_delta(const union delta_base
*base
, enum object_type type
)
410 int first
= 0, last
= nr_deltas
;
412 while (first
< last
) {
413 int next
= (first
+ last
) / 2;
414 struct delta_entry
*delta
= &deltas
[next
];
417 cmp
= compare_delta_bases(base
, &delta
->base
,
418 type
, objects
[delta
->obj_no
].type
);
430 static void find_delta_children(const union delta_base
*base
,
431 int *first_index
, int *last_index
,
432 enum object_type type
)
434 int first
= find_delta(base
, type
);
436 int end
= nr_deltas
- 1;
443 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
445 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
447 *first_index
= first
;
451 static void sha1_object(const void *data
, unsigned long size
,
452 enum object_type type
, unsigned char *sha1
)
454 hash_sha1_file(data
, size
, typename(type
), sha1
);
455 if (has_sha1_file(sha1
)) {
457 enum object_type has_type
;
458 unsigned long has_size
;
459 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
461 die("cannot read existing object %s", sha1_to_hex(sha1
));
462 if (size
!= has_size
|| type
!= has_type
||
463 memcmp(data
, has_data
, size
) != 0)
464 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
468 if (type
== OBJ_BLOB
) {
469 struct blob
*blob
= lookup_blob(sha1
);
471 blob
->object
.flags
|= FLAG_CHECKED
;
473 die("invalid blob object %s", sha1_to_hex(sha1
));
477 void *buf
= (void *) data
;
480 * we do not need to free the memory here, as the
481 * buf is deleted by the caller.
483 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
485 die("invalid %s", typename(type
));
486 if (fsck_object(obj
, 1, fsck_error_function
))
487 die("Error in object");
488 if (fsck_walk(obj
, mark_link
, NULL
))
489 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
491 if (obj
->type
== OBJ_TREE
) {
492 struct tree
*item
= (struct tree
*) obj
;
495 if (obj
->type
== OBJ_COMMIT
) {
496 struct commit
*commit
= (struct commit
*) obj
;
497 commit
->buffer
= NULL
;
499 obj
->flags
|= FLAG_CHECKED
;
504 static int is_delta_type(enum object_type type
)
506 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
509 static void *get_base_data(struct base_data
*c
)
512 struct object_entry
*obj
= c
->obj
;
514 if (is_delta_type(obj
->type
)) {
515 void *base
= get_base_data(c
->base
);
516 void *raw
= get_data_from_pack(obj
);
517 c
->data
= patch_delta(
523 bad_object(obj
->idx
.offset
, "failed to apply delta");
525 c
->data
= get_data_from_pack(obj
);
529 base_cache_used
+= c
->size
;
535 static void resolve_delta(struct object_entry
*delta_obj
,
536 struct base_data
*base
, struct base_data
*result
)
538 void *base_data
, *delta_data
;
540 delta_obj
->real_type
= base
->obj
->real_type
;
541 delta_obj
->delta_depth
= base
->obj
->delta_depth
+ 1;
542 if (deepest_delta
< delta_obj
->delta_depth
)
543 deepest_delta
= delta_obj
->delta_depth
;
544 delta_obj
->base_object_no
= base
->obj
- objects
;
545 delta_data
= get_data_from_pack(delta_obj
);
546 base_data
= get_base_data(base
);
547 result
->obj
= delta_obj
;
548 result
->data
= patch_delta(base_data
, base
->size
,
549 delta_data
, delta_obj
->size
, &result
->size
);
552 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
553 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
554 delta_obj
->idx
.sha1
);
555 nr_resolved_deltas
++;
558 static void find_unresolved_deltas(struct base_data
*base
,
559 struct base_data
*prev_base
)
561 int i
, ref_first
, ref_last
, ofs_first
, ofs_last
;
564 * This is a recursive function. Those brackets should help reducing
565 * stack usage by limiting the scope of the delta_base union.
568 union delta_base base_spec
;
570 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
571 find_delta_children(&base_spec
,
572 &ref_first
, &ref_last
, OBJ_REF_DELTA
);
574 memset(&base_spec
, 0, sizeof(base_spec
));
575 base_spec
.offset
= base
->obj
->idx
.offset
;
576 find_delta_children(&base_spec
,
577 &ofs_first
, &ofs_last
, OBJ_OFS_DELTA
);
580 if (ref_last
== -1 && ofs_last
== -1) {
585 link_base_data(prev_base
, base
);
587 for (i
= ref_first
; i
<= ref_last
; i
++) {
588 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
589 struct base_data result
;
591 assert(child
->real_type
== OBJ_REF_DELTA
);
592 resolve_delta(child
, base
, &result
);
593 if (i
== ref_last
&& ofs_last
== -1)
594 free_base_data(base
);
595 find_unresolved_deltas(&result
, base
);
598 for (i
= ofs_first
; i
<= ofs_last
; i
++) {
599 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
600 struct base_data result
;
602 assert(child
->real_type
== OBJ_OFS_DELTA
);
603 resolve_delta(child
, base
, &result
);
605 free_base_data(base
);
606 find_unresolved_deltas(&result
, base
);
609 unlink_base_data(base
);
612 static int compare_delta_entry(const void *a
, const void *b
)
614 const struct delta_entry
*delta_a
= a
;
615 const struct delta_entry
*delta_b
= b
;
617 /* group by type (ref vs ofs) and then by value (sha-1 or offset) */
618 return compare_delta_bases(&delta_a
->base
, &delta_b
->base
,
619 objects
[delta_a
->obj_no
].type
,
620 objects
[delta_b
->obj_no
].type
);
623 /* Parse all objects and return the pack content SHA1 hash */
624 static void parse_pack_objects(unsigned char *sha1
)
627 struct delta_entry
*delta
= deltas
;
632 * - find locations of all objects;
633 * - calculate SHA1 of all non-delta objects;
634 * - remember base (SHA1 or offset) for all deltas.
637 progress
= start_progress(
638 from_stdin
? "Receiving objects" : "Indexing objects",
640 for (i
= 0; i
< nr_objects
; i
++) {
641 struct object_entry
*obj
= &objects
[i
];
642 void *data
= unpack_raw_entry(obj
, &delta
->base
);
643 obj
->real_type
= obj
->type
;
644 if (is_delta_type(obj
->type
)) {
649 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
651 display_progress(progress
, i
+1);
653 objects
[i
].idx
.offset
= consumed_bytes
;
654 stop_progress(&progress
);
656 /* Check pack integrity */
658 git_SHA1_Final(sha1
, &input_ctx
);
659 if (hashcmp(fill(20), sha1
))
660 die("pack is corrupted (SHA1 mismatch)");
663 /* If input_fd is a file, we should have reached its end now. */
664 if (fstat(input_fd
, &st
))
665 die_errno("cannot fstat packfile");
666 if (S_ISREG(st
.st_mode
) &&
667 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
668 die("pack has junk at the end");
673 /* Sort deltas by base SHA1/offset for fast searching */
674 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
675 compare_delta_entry
);
679 * - for all non-delta objects, look if it is used as a base for
681 * - if used as a base, uncompress the object and apply all deltas,
682 * recursively checking if the resulting object is used as a base
683 * for some more deltas.
686 progress
= start_progress("Resolving deltas", nr_deltas
);
687 for (i
= 0; i
< nr_objects
; i
++) {
688 struct object_entry
*obj
= &objects
[i
];
689 struct base_data base_obj
;
691 if (is_delta_type(obj
->type
))
694 base_obj
.data
= NULL
;
695 find_unresolved_deltas(&base_obj
, NULL
);
696 display_progress(progress
, nr_resolved_deltas
);
700 static int write_compressed(struct sha1file
*f
, void *in
, unsigned int size
)
704 unsigned char outbuf
[4096];
706 memset(&stream
, 0, sizeof(stream
));
707 deflateInit(&stream
, zlib_compression_level
);
709 stream
.avail_in
= size
;
712 stream
.next_out
= outbuf
;
713 stream
.avail_out
= sizeof(outbuf
);
714 status
= deflate(&stream
, Z_FINISH
);
715 sha1write(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
716 } while (status
== Z_OK
);
718 if (status
!= Z_STREAM_END
)
719 die("unable to deflate appended object (%d)", status
);
720 size
= stream
.total_out
;
725 static struct object_entry
*append_obj_to_pack(struct sha1file
*f
,
726 const unsigned char *sha1
, void *buf
,
727 unsigned long size
, enum object_type type
)
729 struct object_entry
*obj
= &objects
[nr_objects
++];
730 unsigned char header
[10];
731 unsigned long s
= size
;
733 unsigned char c
= (type
<< 4) | (s
& 15);
736 header
[n
++] = c
| 0x80;
742 sha1write(f
, header
, n
);
746 obj
[0].real_type
= type
;
747 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
748 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
749 obj
[0].idx
.crc32
= crc32_end(f
);
751 hashcpy(obj
->idx
.sha1
, sha1
);
755 static int delta_pos_compare(const void *_a
, const void *_b
)
757 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
758 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
759 return a
->obj_no
- b
->obj_no
;
762 static void fix_unresolved_deltas(struct sha1file
*f
, int nr_unresolved
)
764 struct delta_entry
**sorted_by_pos
;
768 * Since many unresolved deltas may well be themselves base objects
769 * for more unresolved deltas, we really want to include the
770 * smallest number of base objects that would cover as much delta
771 * as possible by picking the
772 * trunc deltas first, allowing for other deltas to resolve without
773 * additional base objects. Since most base objects are to be found
774 * before deltas depending on them, a good heuristic is to start
775 * resolving deltas in the same order as their position in the pack.
777 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
778 for (i
= 0; i
< nr_deltas
; i
++) {
779 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
781 sorted_by_pos
[n
++] = &deltas
[i
];
783 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
785 for (i
= 0; i
< n
; i
++) {
786 struct delta_entry
*d
= sorted_by_pos
[i
];
787 enum object_type type
;
788 struct base_data base_obj
;
790 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
792 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
796 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
797 base_obj
.size
, typename(type
)))
798 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
799 base_obj
.obj
= append_obj_to_pack(f
, d
->base
.sha1
,
800 base_obj
.data
, base_obj
.size
, type
);
801 find_unresolved_deltas(&base_obj
, NULL
);
802 display_progress(progress
, nr_resolved_deltas
);
807 static void final(const char *final_pack_name
, const char *curr_pack_name
,
808 const char *final_index_name
, const char *curr_index_name
,
809 const char *keep_name
, const char *keep_msg
,
812 const char *report
= "pack";
819 fsync_or_die(output_fd
, curr_pack_name
);
820 err
= close(output_fd
);
822 die_errno("error while closing pack file");
826 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
829 keep_fd
= odb_pack_keep(name
, sizeof(name
), sha1
);
831 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
835 die_errno("cannot write keep file '%s'",
838 if (keep_msg_len
> 0) {
839 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
840 write_or_die(keep_fd
, "\n", 1);
842 if (close(keep_fd
) != 0)
843 die_errno("cannot close written keep file '%s'",
849 if (final_pack_name
!= curr_pack_name
) {
850 if (!final_pack_name
) {
851 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
852 get_object_directory(), sha1_to_hex(sha1
));
853 final_pack_name
= name
;
855 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
856 die("cannot store pack file");
857 } else if (from_stdin
)
858 chmod(final_pack_name
, 0444);
860 if (final_index_name
!= curr_index_name
) {
861 if (!final_index_name
) {
862 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
863 get_object_directory(), sha1_to_hex(sha1
));
864 final_index_name
= name
;
866 if (move_temp_to_file(curr_index_name
, final_index_name
))
867 die("cannot store index file");
869 chmod(final_index_name
, 0444);
872 printf("%s\n", sha1_to_hex(sha1
));
875 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
876 report
, sha1_to_hex(sha1
));
877 write_or_die(1, buf
, len
);
880 * Let's just mimic git-unpack-objects here and write
881 * the last part of the input buffer to stdout.
884 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
893 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
895 struct pack_idx_option
*opts
= cb
;
897 if (!strcmp(k
, "pack.indexversion")) {
898 opts
->version
= git_config_int(k
, v
);
899 if (opts
->version
> 2)
900 die("bad pack.indexversion=%"PRIu32
, opts
->version
);
903 return git_default_config(k
, v
, cb
);
906 static int cmp_uint32(const void *a_
, const void *b_
)
908 uint32_t a
= *((uint32_t *)a_
);
909 uint32_t b
= *((uint32_t *)b_
);
911 return (a
< b
) ? -1 : (a
!= b
);
914 static void read_v2_anomalous_offsets(struct packed_git
*p
,
915 struct pack_idx_option
*opts
)
917 const uint32_t *idx1
, *idx2
;
920 /* The address of the 4-byte offset table */
921 idx1
= (((const uint32_t *)p
->index_data
)
922 + 2 /* 8-byte header */
924 + 5 * p
->num_objects
/* 20-byte SHA-1 table */
925 + p
->num_objects
/* CRC32 table */
928 /* The address of the 8-byte offset table */
929 idx2
= idx1
+ p
->num_objects
;
931 for (i
= 0; i
< p
->num_objects
; i
++) {
932 uint32_t off
= ntohl(idx1
[i
]);
933 if (!(off
& 0x80000000))
935 off
= off
& 0x7fffffff;
939 * The real offset is ntohl(idx2[off * 2]) in high 4
940 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
941 * octets. But idx2[off * 2] is Zero!!!
943 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
944 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
947 if (1 < opts
->anomaly_nr
)
948 qsort(opts
->anomaly
, opts
->anomaly_nr
, sizeof(uint32_t), cmp_uint32
);
951 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
953 struct packed_git
*p
= add_packed_git(pack_name
, strlen(pack_name
), 1);
956 die("Cannot open existing pack file '%s'", pack_name
);
957 if (open_pack_index(p
))
958 die("Cannot open existing pack idx file for '%s'", pack_name
);
960 /* Read the attributes from the existing idx file */
961 opts
->version
= p
->index_version
;
963 if (opts
->version
== 2)
964 read_v2_anomalous_offsets(p
, opts
);
967 * Get rid of the idx file as we do not need it anymore.
968 * NEEDSWORK: extract this bit from free_pack_by_name() in
969 * sha1_file.c, perhaps? It shouldn't matter very much as we
970 * know we haven't installed this pack (hence we never have
971 * read anything from it).
977 static void show_pack_info(int stat_only
)
979 int i
, baseobjects
= nr_objects
- nr_deltas
;
980 unsigned long *chain_histogram
= NULL
;
983 chain_histogram
= xcalloc(deepest_delta
, sizeof(unsigned long));
985 for (i
= 0; i
< nr_objects
; i
++) {
986 struct object_entry
*obj
= &objects
[i
];
988 if (is_delta_type(obj
->type
))
989 chain_histogram
[obj
->delta_depth
- 1]++;
992 printf("%s %-6s %lu %lu %"PRIuMAX
,
993 sha1_to_hex(obj
->idx
.sha1
),
994 typename(obj
->real_type
), obj
->size
,
995 (unsigned long)(obj
[1].idx
.offset
- obj
->idx
.offset
),
996 (uintmax_t)obj
->idx
.offset
);
997 if (is_delta_type(obj
->type
)) {
998 struct object_entry
*bobj
= &objects
[obj
->base_object_no
];
999 printf(" %u %s", obj
->delta_depth
, sha1_to_hex(bobj
->idx
.sha1
));
1005 printf("non delta: %d object%s\n",
1006 baseobjects
, baseobjects
> 1 ? "s" : "");
1007 for (i
= 0; i
< deepest_delta
; i
++) {
1008 if (!chain_histogram
[i
])
1010 printf("chain length = %d: %lu object%s\n",
1013 chain_histogram
[i
] > 1 ? "s" : "");
1017 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
1019 int i
, fix_thin_pack
= 0, verify
= 0, stat_only
= 0, stat
= 0;
1020 const char *curr_pack
, *curr_index
;
1021 const char *index_name
= NULL
, *pack_name
= NULL
;
1022 const char *keep_name
= NULL
, *keep_msg
= NULL
;
1023 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
1024 struct pack_idx_entry
**idx_objects
;
1025 struct pack_idx_option opts
;
1026 unsigned char pack_sha1
[20];
1028 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1029 usage(index_pack_usage
);
1031 read_replace_refs
= 0;
1033 reset_pack_idx_option(&opts
);
1034 git_config(git_index_pack_config
, &opts
);
1035 if (prefix
&& chdir(prefix
))
1036 die("Cannot come back to cwd");
1038 for (i
= 1; i
< argc
; i
++) {
1039 const char *arg
= argv
[i
];
1042 if (!strcmp(arg
, "--stdin")) {
1044 } else if (!strcmp(arg
, "--fix-thin")) {
1046 } else if (!strcmp(arg
, "--strict")) {
1048 } else if (!strcmp(arg
, "--verify")) {
1050 } else if (!strcmp(arg
, "--verify-stat")) {
1053 } else if (!strcmp(arg
, "--verify-stat-only")) {
1057 } else if (!strcmp(arg
, "--keep")) {
1059 } else if (!prefixcmp(arg
, "--keep=")) {
1061 } else if (!prefixcmp(arg
, "--pack_header=")) {
1062 struct pack_header
*hdr
;
1065 hdr
= (struct pack_header
*)input_buffer
;
1066 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
1067 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
1070 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
1073 input_len
= sizeof(*hdr
);
1074 } else if (!strcmp(arg
, "-v")) {
1076 } else if (!strcmp(arg
, "-o")) {
1077 if (index_name
|| (i
+1) >= argc
)
1078 usage(index_pack_usage
);
1079 index_name
= argv
[++i
];
1080 } else if (!prefixcmp(arg
, "--index-version=")) {
1082 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1083 if (opts
.version
> 2)
1086 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1087 if (*c
|| opts
.off32_limit
& 0x80000000)
1090 usage(index_pack_usage
);
1095 usage(index_pack_usage
);
1099 if (!pack_name
&& !from_stdin
)
1100 usage(index_pack_usage
);
1101 if (fix_thin_pack
&& !from_stdin
)
1102 die("--fix-thin cannot be used without --stdin");
1103 if (!index_name
&& pack_name
) {
1104 int len
= strlen(pack_name
);
1105 if (!has_extension(pack_name
, ".pack"))
1106 die("packfile name '%s' does not end with '.pack'",
1108 index_name_buf
= xmalloc(len
);
1109 memcpy(index_name_buf
, pack_name
, len
- 5);
1110 strcpy(index_name_buf
+ len
- 5, ".idx");
1111 index_name
= index_name_buf
;
1113 if (keep_msg
&& !keep_name
&& pack_name
) {
1114 int len
= strlen(pack_name
);
1115 if (!has_extension(pack_name
, ".pack"))
1116 die("packfile name '%s' does not end with '.pack'",
1118 keep_name_buf
= xmalloc(len
);
1119 memcpy(keep_name_buf
, pack_name
, len
- 5);
1120 strcpy(keep_name_buf
+ len
- 5, ".keep");
1121 keep_name
= keep_name_buf
;
1125 die("--verify with no packfile name given");
1126 read_idx_option(&opts
, index_name
);
1127 opts
.flags
|= WRITE_IDX_VERIFY
;
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
);