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
;
26 unsigned char sha1
[20];
31 struct base_data
*base
;
32 struct base_data
*child
;
33 struct object_entry
*obj
;
39 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
40 * to memcmp() only the first 20 bytes.
42 #define UNION_BASE_SZ 20
44 #define FLAG_LINK (1u<<20)
45 #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 git_SHA_CTX input_ctx
;
72 static uint32_t input_crc32
;
73 static int input_fd
, output_fd
, pack_fd
;
75 static int mark_link(struct object
*obj
, int type
, void *data
)
80 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
81 die("object type mismatch at %s", sha1_to_hex(obj
->sha1
));
83 obj
->flags
|= FLAG_LINK
;
87 /* The content of each linked object must have been checked
88 or it must be already present in the object database */
89 static void check_object(struct object
*obj
)
94 if (!(obj
->flags
& FLAG_LINK
))
97 if (!(obj
->flags
& FLAG_CHECKED
)) {
99 int type
= sha1_object_info(obj
->sha1
, &size
);
100 if (type
!= obj
->type
|| type
<= 0)
101 die("object of unexpected type");
102 obj
->flags
|= FLAG_CHECKED
;
107 static void check_objects(void)
111 max
= get_max_object_index();
112 for (i
= 0; i
< max
; i
++)
113 check_object(get_indexed_object(i
));
117 /* Discard current buffer used content. */
118 static void flush(void)
122 write_or_die(output_fd
, input_buffer
, input_offset
);
123 git_SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
124 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
130 * Make sure at least "min" bytes are available in the buffer, and
131 * return the pointer to the buffer.
133 static void *fill(int min
)
135 if (min
<= input_len
)
136 return input_buffer
+ input_offset
;
137 if (min
> sizeof(input_buffer
))
138 die("cannot fill %d bytes", min
);
141 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
142 sizeof(input_buffer
) - input_len
);
146 die_errno("read error on input");
150 display_throughput(progress
, consumed_bytes
+ input_len
);
151 } while (input_len
< min
);
155 static void use(int bytes
)
157 if (bytes
> input_len
)
158 die("used more bytes than were available");
159 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
161 input_offset
+= bytes
;
163 /* make sure off_t is sufficiently large not to wrap */
164 if (signed_add_overflows(consumed_bytes
, bytes
))
165 die("pack too large for current definition of off_t");
166 consumed_bytes
+= bytes
;
169 static const char *open_pack_file(const char *pack_name
)
174 static char tmpfile
[PATH_MAX
];
175 output_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
176 "pack/tmp_pack_XXXXXX");
177 pack_name
= xstrdup(tmpfile
);
179 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
181 die_errno("unable to create '%s'", pack_name
);
184 input_fd
= open(pack_name
, O_RDONLY
);
186 die_errno("cannot open packfile '%s'", pack_name
);
190 git_SHA1_Init(&input_ctx
);
194 static void parse_pack_header(void)
196 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
198 /* Header consistency check */
199 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
200 die("pack signature mismatch");
201 if (!pack_version_ok(hdr
->hdr_version
))
202 die("pack version %"PRIu32
" unsupported",
203 ntohl(hdr
->hdr_version
));
205 nr_objects
= ntohl(hdr
->hdr_entries
);
206 use(sizeof(struct pack_header
));
209 static NORETURN
void bad_object(unsigned long offset
, const char *format
,
210 ...) __attribute__((format (printf
, 2, 3)));
212 static void bad_object(unsigned long offset
, const char *format
, ...)
217 va_start(params
, format
);
218 vsnprintf(buf
, sizeof(buf
), format
, params
);
220 die("pack has bad object at offset %lu: %s", offset
, buf
);
223 static void free_base_data(struct base_data
*c
)
228 base_cache_used
-= c
->size
;
232 static void prune_base_data(struct base_data
*retain
)
236 base_cache_used
> delta_base_cache_limit
&& b
;
238 if (b
->data
&& b
!= retain
)
243 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
253 base_cache_used
+= c
->size
;
257 static void unlink_base_data(struct base_data
*c
)
259 struct base_data
*base
= c
->base
;
267 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
271 void *buf
= xmalloc(size
);
273 memset(&stream
, 0, sizeof(stream
));
274 git_inflate_init(&stream
);
275 stream
.next_out
= buf
;
276 stream
.avail_out
= size
;
279 stream
.next_in
= fill(1);
280 stream
.avail_in
= input_len
;
281 status
= git_inflate(&stream
, 0);
282 use(input_len
- stream
.avail_in
);
283 } while (status
== Z_OK
);
284 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
285 bad_object(offset
, "inflate returned %d", status
);
286 git_inflate_end(&stream
);
290 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
293 unsigned long size
, c
;
298 obj
->idx
.offset
= consumed_bytes
;
299 input_crc32
= crc32(0, Z_NULL
, 0);
304 obj
->type
= (c
>> 4) & 7;
311 size
+= (c
& 0x7f) << shift
;
318 hashcpy(delta_base
->sha1
, fill(20));
322 memset(delta_base
, 0, sizeof(*delta_base
));
326 base_offset
= c
& 127;
329 if (!base_offset
|| MSB(base_offset
, 7))
330 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
334 base_offset
= (base_offset
<< 7) + (c
& 127);
336 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
337 if (delta_base
->offset
<= 0 || delta_base
->offset
>= obj
->idx
.offset
)
338 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
346 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
348 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
350 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
351 obj
->idx
.crc32
= input_crc32
;
355 static void *get_data_from_pack(struct object_entry
*obj
)
357 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
358 unsigned long len
= obj
[1].idx
.offset
- from
;
359 unsigned char *data
, *inbuf
;
363 data
= xmalloc(obj
->size
);
364 inbuf
= xmalloc((len
< 64*1024) ? len
: 64*1024);
366 memset(&stream
, 0, sizeof(stream
));
367 git_inflate_init(&stream
);
368 stream
.next_out
= data
;
369 stream
.avail_out
= obj
->size
;
372 ssize_t n
= (len
< 64*1024) ? len
: 64*1024;
373 n
= pread(pack_fd
, inbuf
, n
, from
);
375 die_errno("cannot pread pack file");
377 die("premature end of pack file, %lu bytes missing", len
);
380 stream
.next_in
= inbuf
;
382 status
= git_inflate(&stream
, 0);
383 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
385 /* This has been inflated OK when first encountered, so... */
386 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
387 die("serious inflate inconsistency");
389 git_inflate_end(&stream
);
394 static int compare_delta_bases(const union delta_base
*base1
,
395 const union delta_base
*base2
,
396 enum object_type type1
,
397 enum object_type type2
)
399 int cmp
= type1
- type2
;
402 return memcmp(base1
, base2
, UNION_BASE_SZ
);
405 static int find_delta(const union delta_base
*base
, enum object_type type
)
407 int first
= 0, last
= nr_deltas
;
409 while (first
< last
) {
410 int next
= (first
+ last
) / 2;
411 struct delta_entry
*delta
= &deltas
[next
];
414 cmp
= compare_delta_bases(base
, &delta
->base
,
415 type
, objects
[delta
->obj_no
].type
);
427 static void find_delta_children(const union delta_base
*base
,
428 int *first_index
, int *last_index
,
429 enum object_type type
)
431 int first
= find_delta(base
, type
);
433 int end
= nr_deltas
- 1;
440 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
442 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
444 *first_index
= first
;
448 static void sha1_object(const void *data
, unsigned long size
,
449 enum object_type type
, unsigned char *sha1
)
451 hash_sha1_file(data
, size
, typename(type
), sha1
);
452 if (has_sha1_file(sha1
)) {
454 enum object_type has_type
;
455 unsigned long has_size
;
456 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
458 die("cannot read existing object %s", sha1_to_hex(sha1
));
459 if (size
!= has_size
|| type
!= has_type
||
460 memcmp(data
, has_data
, size
) != 0)
461 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
465 if (type
== OBJ_BLOB
) {
466 struct blob
*blob
= lookup_blob(sha1
);
468 blob
->object
.flags
|= FLAG_CHECKED
;
470 die("invalid blob object %s", sha1_to_hex(sha1
));
474 void *buf
= (void *) data
;
477 * we do not need to free the memory here, as the
478 * buf is deleted by the caller.
480 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
482 die("invalid %s", typename(type
));
483 if (fsck_object(obj
, 1, fsck_error_function
))
484 die("Error in object");
485 if (fsck_walk(obj
, mark_link
, NULL
))
486 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
488 if (obj
->type
== OBJ_TREE
) {
489 struct tree
*item
= (struct tree
*) obj
;
492 if (obj
->type
== OBJ_COMMIT
) {
493 struct commit
*commit
= (struct commit
*) obj
;
494 commit
->buffer
= NULL
;
496 obj
->flags
|= FLAG_CHECKED
;
501 static int is_delta_type(enum object_type type
)
503 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
506 static void *get_base_data(struct base_data
*c
)
509 struct object_entry
*obj
= c
->obj
;
511 if (is_delta_type(obj
->type
)) {
512 void *base
= get_base_data(c
->base
);
513 void *raw
= get_data_from_pack(obj
);
514 c
->data
= patch_delta(
520 bad_object(obj
->idx
.offset
, "failed to apply delta");
522 c
->data
= get_data_from_pack(obj
);
526 base_cache_used
+= c
->size
;
532 static void resolve_delta(struct object_entry
*delta_obj
,
533 struct base_data
*base
, struct base_data
*result
)
535 void *base_data
, *delta_data
;
537 delta_obj
->real_type
= base
->obj
->real_type
;
538 delta_data
= get_data_from_pack(delta_obj
);
539 base_data
= get_base_data(base
);
540 result
->obj
= delta_obj
;
541 result
->data
= patch_delta(base_data
, base
->size
,
542 delta_data
, delta_obj
->size
, &result
->size
);
545 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
546 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
547 delta_obj
->idx
.sha1
);
548 nr_resolved_deltas
++;
551 static void find_unresolved_deltas(struct base_data
*base
,
552 struct base_data
*prev_base
)
554 int i
, ref_first
, ref_last
, ofs_first
, ofs_last
;
557 * This is a recursive function. Those brackets should help reducing
558 * stack usage by limiting the scope of the delta_base union.
561 union delta_base base_spec
;
563 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
564 find_delta_children(&base_spec
,
565 &ref_first
, &ref_last
, OBJ_REF_DELTA
);
567 memset(&base_spec
, 0, sizeof(base_spec
));
568 base_spec
.offset
= base
->obj
->idx
.offset
;
569 find_delta_children(&base_spec
,
570 &ofs_first
, &ofs_last
, OBJ_OFS_DELTA
);
573 if (ref_last
== -1 && ofs_last
== -1) {
578 link_base_data(prev_base
, base
);
580 for (i
= ref_first
; i
<= ref_last
; i
++) {
581 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
582 struct base_data result
;
584 assert(child
->real_type
== OBJ_REF_DELTA
);
585 resolve_delta(child
, base
, &result
);
586 if (i
== ref_last
&& ofs_last
== -1)
587 free_base_data(base
);
588 find_unresolved_deltas(&result
, base
);
591 for (i
= ofs_first
; i
<= ofs_last
; i
++) {
592 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
593 struct base_data result
;
595 assert(child
->real_type
== OBJ_OFS_DELTA
);
596 resolve_delta(child
, base
, &result
);
598 free_base_data(base
);
599 find_unresolved_deltas(&result
, base
);
602 unlink_base_data(base
);
605 static int compare_delta_entry(const void *a
, const void *b
)
607 const struct delta_entry
*delta_a
= a
;
608 const struct delta_entry
*delta_b
= b
;
610 /* group by type (ref vs ofs) and then by value (sha-1 or offset) */
611 return compare_delta_bases(&delta_a
->base
, &delta_b
->base
,
612 objects
[delta_a
->obj_no
].type
,
613 objects
[delta_b
->obj_no
].type
);
616 /* Parse all objects and return the pack content SHA1 hash */
617 static void parse_pack_objects(unsigned char *sha1
)
620 struct delta_entry
*delta
= deltas
;
625 * - find locations of all objects;
626 * - calculate SHA1 of all non-delta objects;
627 * - remember base (SHA1 or offset) for all deltas.
630 progress
= start_progress(
631 from_stdin
? "Receiving objects" : "Indexing objects",
633 for (i
= 0; i
< nr_objects
; i
++) {
634 struct object_entry
*obj
= &objects
[i
];
635 void *data
= unpack_raw_entry(obj
, &delta
->base
);
636 obj
->real_type
= obj
->type
;
637 if (is_delta_type(obj
->type
)) {
642 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
644 display_progress(progress
, i
+1);
646 objects
[i
].idx
.offset
= consumed_bytes
;
647 stop_progress(&progress
);
649 /* Check pack integrity */
651 git_SHA1_Final(sha1
, &input_ctx
);
652 if (hashcmp(fill(20), sha1
))
653 die("pack is corrupted (SHA1 mismatch)");
656 /* If input_fd is a file, we should have reached its end now. */
657 if (fstat(input_fd
, &st
))
658 die_errno("cannot fstat packfile");
659 if (S_ISREG(st
.st_mode
) &&
660 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
661 die("pack has junk at the end");
666 /* Sort deltas by base SHA1/offset for fast searching */
667 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
668 compare_delta_entry
);
672 * - for all non-delta objects, look if it is used as a base for
674 * - if used as a base, uncompress the object and apply all deltas,
675 * recursively checking if the resulting object is used as a base
676 * for some more deltas.
679 progress
= start_progress("Resolving deltas", nr_deltas
);
680 for (i
= 0; i
< nr_objects
; i
++) {
681 struct object_entry
*obj
= &objects
[i
];
682 struct base_data base_obj
;
684 if (is_delta_type(obj
->type
))
687 base_obj
.data
= NULL
;
688 find_unresolved_deltas(&base_obj
, NULL
);
689 display_progress(progress
, nr_resolved_deltas
);
693 static int write_compressed(struct sha1file
*f
, void *in
, unsigned int size
)
697 unsigned char outbuf
[4096];
699 memset(&stream
, 0, sizeof(stream
));
700 deflateInit(&stream
, zlib_compression_level
);
702 stream
.avail_in
= size
;
705 stream
.next_out
= outbuf
;
706 stream
.avail_out
= sizeof(outbuf
);
707 status
= deflate(&stream
, Z_FINISH
);
708 sha1write(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
709 } while (status
== Z_OK
);
711 if (status
!= Z_STREAM_END
)
712 die("unable to deflate appended object (%d)", status
);
713 size
= stream
.total_out
;
718 static struct object_entry
*append_obj_to_pack(struct sha1file
*f
,
719 const unsigned char *sha1
, void *buf
,
720 unsigned long size
, enum object_type type
)
722 struct object_entry
*obj
= &objects
[nr_objects
++];
723 unsigned char header
[10];
724 unsigned long s
= size
;
726 unsigned char c
= (type
<< 4) | (s
& 15);
729 header
[n
++] = c
| 0x80;
735 sha1write(f
, header
, n
);
739 obj
[0].real_type
= type
;
740 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
741 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
742 obj
[0].idx
.crc32
= crc32_end(f
);
744 hashcpy(obj
->idx
.sha1
, sha1
);
748 static int delta_pos_compare(const void *_a
, const void *_b
)
750 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
751 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
752 return a
->obj_no
- b
->obj_no
;
755 static void fix_unresolved_deltas(struct sha1file
*f
, int nr_unresolved
)
757 struct delta_entry
**sorted_by_pos
;
761 * Since many unresolved deltas may well be themselves base objects
762 * for more unresolved deltas, we really want to include the
763 * smallest number of base objects that would cover as much delta
764 * as possible by picking the
765 * trunc deltas first, allowing for other deltas to resolve without
766 * additional base objects. Since most base objects are to be found
767 * before deltas depending on them, a good heuristic is to start
768 * resolving deltas in the same order as their position in the pack.
770 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
771 for (i
= 0; i
< nr_deltas
; i
++) {
772 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
774 sorted_by_pos
[n
++] = &deltas
[i
];
776 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
778 for (i
= 0; i
< n
; i
++) {
779 struct delta_entry
*d
= sorted_by_pos
[i
];
780 enum object_type type
;
781 struct base_data base_obj
;
783 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
785 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
789 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
790 base_obj
.size
, typename(type
)))
791 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
792 base_obj
.obj
= append_obj_to_pack(f
, d
->base
.sha1
,
793 base_obj
.data
, base_obj
.size
, type
);
794 find_unresolved_deltas(&base_obj
, NULL
);
795 display_progress(progress
, nr_resolved_deltas
);
800 static void final(const char *final_pack_name
, const char *curr_pack_name
,
801 const char *final_index_name
, const char *curr_index_name
,
802 const char *keep_name
, const char *keep_msg
,
805 const char *report
= "pack";
812 fsync_or_die(output_fd
, curr_pack_name
);
813 err
= close(output_fd
);
815 die_errno("error while closing pack file");
819 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
822 keep_fd
= odb_pack_keep(name
, sizeof(name
), sha1
);
824 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
828 die_errno("cannot write keep file '%s'",
831 if (keep_msg_len
> 0) {
832 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
833 write_or_die(keep_fd
, "\n", 1);
835 if (close(keep_fd
) != 0)
836 die_errno("cannot close written keep file '%s'",
842 if (final_pack_name
!= curr_pack_name
) {
843 if (!final_pack_name
) {
844 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
845 get_object_directory(), sha1_to_hex(sha1
));
846 final_pack_name
= name
;
848 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
849 die("cannot store pack file");
850 } else if (from_stdin
)
851 chmod(final_pack_name
, 0444);
853 if (final_index_name
!= curr_index_name
) {
854 if (!final_index_name
) {
855 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
856 get_object_directory(), sha1_to_hex(sha1
));
857 final_index_name
= name
;
859 if (move_temp_to_file(curr_index_name
, final_index_name
))
860 die("cannot store index file");
862 chmod(final_index_name
, 0444);
865 printf("%s\n", sha1_to_hex(sha1
));
868 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
869 report
, sha1_to_hex(sha1
));
870 write_or_die(1, buf
, len
);
873 * Let's just mimic git-unpack-objects here and write
874 * the last part of the input buffer to stdout.
877 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
886 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
888 struct pack_idx_option
*opts
= cb
;
890 if (!strcmp(k
, "pack.indexversion")) {
891 opts
->version
= git_config_int(k
, v
);
892 if (opts
->version
> 2)
893 die("bad pack.indexversion=%"PRIu32
, opts
->version
);
896 return git_default_config(k
, v
, cb
);
899 static int cmp_uint32(const void *a_
, const void *b_
)
901 uint32_t a
= *((uint32_t *)a_
);
902 uint32_t b
= *((uint32_t *)b_
);
904 return (a
< b
) ? -1 : (a
!= b
);
907 static void read_v2_anomalous_offsets(struct packed_git
*p
,
908 struct pack_idx_option
*opts
)
910 const uint32_t *idx1
, *idx2
;
913 /* The address of the 4-byte offset table */
914 idx1
= (((const uint32_t *)p
->index_data
)
915 + 2 /* 8-byte header */
917 + 5 * p
->num_objects
/* 20-byte SHA-1 table */
918 + p
->num_objects
/* CRC32 table */
921 /* The address of the 8-byte offset table */
922 idx2
= idx1
+ p
->num_objects
;
924 for (i
= 0; i
< p
->num_objects
; i
++) {
925 uint32_t off
= ntohl(idx1
[i
]);
926 if (!(off
& 0x80000000))
928 off
= off
& 0x7fffffff;
932 * The real offset is ntohl(idx2[off * 2]) in high 4
933 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
934 * octets. But idx2[off * 2] is Zero!!!
936 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
937 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
940 if (1 < opts
->anomaly_nr
)
941 qsort(opts
->anomaly
, opts
->anomaly_nr
, sizeof(uint32_t), cmp_uint32
);
944 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
946 struct packed_git
*p
= add_packed_git(pack_name
, strlen(pack_name
), 1);
949 die("Cannot open existing pack file '%s'", pack_name
);
950 if (open_pack_index(p
))
951 die("Cannot open existing pack idx file for '%s'", pack_name
);
953 /* Read the attributes from the existing idx file */
954 opts
->version
= p
->index_version
;
956 if (opts
->version
== 2)
957 read_v2_anomalous_offsets(p
, opts
);
960 * Get rid of the idx file as we do not need it anymore.
961 * NEEDSWORK: extract this bit from free_pack_by_name() in
962 * sha1_file.c, perhaps? It shouldn't matter very much as we
963 * know we haven't installed this pack (hence we never have
964 * read anything from it).
970 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
972 int i
, fix_thin_pack
= 0, verify
= 0;
973 const char *curr_pack
, *curr_index
;
974 const char *index_name
= NULL
, *pack_name
= NULL
;
975 const char *keep_name
= NULL
, *keep_msg
= NULL
;
976 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
977 struct pack_idx_entry
**idx_objects
;
978 struct pack_idx_option opts
;
979 unsigned char pack_sha1
[20];
981 if (argc
== 2 && !strcmp(argv
[1], "-h"))
982 usage(index_pack_usage
);
984 read_replace_refs
= 0;
986 reset_pack_idx_option(&opts
);
987 git_config(git_index_pack_config
, &opts
);
988 if (prefix
&& chdir(prefix
))
989 die("Cannot come back to cwd");
991 for (i
= 1; i
< argc
; i
++) {
992 const char *arg
= argv
[i
];
995 if (!strcmp(arg
, "--stdin")) {
997 } else if (!strcmp(arg
, "--fix-thin")) {
999 } else if (!strcmp(arg
, "--strict")) {
1001 } else if (!strcmp(arg
, "--verify")) {
1003 } else if (!strcmp(arg
, "--keep")) {
1005 } else if (!prefixcmp(arg
, "--keep=")) {
1007 } else if (!prefixcmp(arg
, "--pack_header=")) {
1008 struct pack_header
*hdr
;
1011 hdr
= (struct pack_header
*)input_buffer
;
1012 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
1013 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
1016 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
1019 input_len
= sizeof(*hdr
);
1020 } else if (!strcmp(arg
, "-v")) {
1022 } else if (!strcmp(arg
, "-o")) {
1023 if (index_name
|| (i
+1) >= argc
)
1024 usage(index_pack_usage
);
1025 index_name
= argv
[++i
];
1026 } else if (!prefixcmp(arg
, "--index-version=")) {
1028 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1029 if (opts
.version
> 2)
1032 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1033 if (*c
|| opts
.off32_limit
& 0x80000000)
1036 usage(index_pack_usage
);
1041 usage(index_pack_usage
);
1045 if (!pack_name
&& !from_stdin
)
1046 usage(index_pack_usage
);
1047 if (fix_thin_pack
&& !from_stdin
)
1048 die("--fix-thin cannot be used without --stdin");
1049 if (!index_name
&& pack_name
) {
1050 int len
= strlen(pack_name
);
1051 if (!has_extension(pack_name
, ".pack"))
1052 die("packfile name '%s' does not end with '.pack'",
1054 index_name_buf
= xmalloc(len
);
1055 memcpy(index_name_buf
, pack_name
, len
- 5);
1056 strcpy(index_name_buf
+ len
- 5, ".idx");
1057 index_name
= index_name_buf
;
1059 if (keep_msg
&& !keep_name
&& pack_name
) {
1060 int len
= strlen(pack_name
);
1061 if (!has_extension(pack_name
, ".pack"))
1062 die("packfile name '%s' does not end with '.pack'",
1064 keep_name_buf
= xmalloc(len
);
1065 memcpy(keep_name_buf
, pack_name
, len
- 5);
1066 strcpy(keep_name_buf
+ len
- 5, ".keep");
1067 keep_name
= keep_name_buf
;
1071 die("--verify with no packfile name given");
1072 read_idx_option(&opts
, index_name
);
1073 opts
.flags
|= WRITE_IDX_VERIFY
;
1076 curr_pack
= open_pack_file(pack_name
);
1077 parse_pack_header();
1078 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
1079 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
1080 parse_pack_objects(pack_sha1
);
1081 if (nr_deltas
== nr_resolved_deltas
) {
1082 stop_progress(&progress
);
1083 /* Flush remaining pack final 20-byte SHA1. */
1086 if (fix_thin_pack
) {
1088 unsigned char read_sha1
[20], tail_sha1
[20];
1090 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
1091 int nr_objects_initial
= nr_objects
;
1092 if (nr_unresolved
<= 0)
1093 die("confusion beyond insanity");
1094 objects
= xrealloc(objects
,
1095 (nr_objects
+ nr_unresolved
+ 1)
1096 * sizeof(*objects
));
1097 f
= sha1fd(output_fd
, curr_pack
);
1098 fix_unresolved_deltas(f
, nr_unresolved
);
1099 sprintf(msg
, "completed with %d local objects",
1100 nr_objects
- nr_objects_initial
);
1101 stop_progress_msg(&progress
, msg
);
1102 sha1close(f
, tail_sha1
, 0);
1103 hashcpy(read_sha1
, pack_sha1
);
1104 fixup_pack_header_footer(output_fd
, pack_sha1
,
1105 curr_pack
, nr_objects
,
1106 read_sha1
, consumed_bytes
-20);
1107 if (hashcmp(read_sha1
, tail_sha1
) != 0)
1108 die("Unexpected tail checksum for %s "
1109 "(disk corruption?)", curr_pack
);
1111 if (nr_deltas
!= nr_resolved_deltas
)
1112 die("pack has %d unresolved deltas",
1113 nr_deltas
- nr_resolved_deltas
);
1119 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
1120 for (i
= 0; i
< nr_objects
; i
++)
1121 idx_objects
[i
] = &objects
[i
].idx
;
1122 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, &opts
, pack_sha1
);
1126 final(pack_name
, curr_pack
,
1127 index_name
, curr_index
,
1128 keep_name
, keep_msg
,
1133 free(index_name_buf
);
1134 free(keep_name_buf
);
1135 if (pack_name
== NULL
)
1136 free((void *) curr_pack
);
1137 if (index_name
== NULL
)
1138 free((void *) curr_index
);