13 static const char index_pack_usage
[] =
14 "git index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] [--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 (consumed_bytes
> 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
)
270 void *buf
= xmalloc(size
);
272 memset(&stream
, 0, sizeof(stream
));
273 stream
.next_out
= buf
;
274 stream
.avail_out
= size
;
275 stream
.next_in
= fill(1);
276 stream
.avail_in
= input_len
;
277 git_inflate_init(&stream
);
280 int ret
= git_inflate(&stream
, 0);
281 use(input_len
- stream
.avail_in
);
282 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
285 bad_object(offset
, "inflate returned %d", ret
);
286 stream
.next_in
= fill(1);
287 stream
.avail_in
= input_len
;
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 find_delta(const union delta_base
*base
)
399 int first
= 0, last
= nr_deltas
;
401 while (first
< last
) {
402 int next
= (first
+ last
) / 2;
403 struct delta_entry
*delta
= &deltas
[next
];
406 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
418 static void find_delta_children(const union delta_base
*base
,
419 int *first_index
, int *last_index
)
421 int first
= find_delta(base
);
423 int end
= nr_deltas
- 1;
430 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
432 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
434 *first_index
= first
;
438 static void sha1_object(const void *data
, unsigned long size
,
439 enum object_type type
, unsigned char *sha1
)
441 hash_sha1_file(data
, size
, typename(type
), sha1
);
442 if (has_sha1_file(sha1
)) {
444 enum object_type has_type
;
445 unsigned long has_size
;
446 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
448 die("cannot read existing object %s", sha1_to_hex(sha1
));
449 if (size
!= has_size
|| type
!= has_type
||
450 memcmp(data
, has_data
, size
) != 0)
451 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
455 if (type
== OBJ_BLOB
) {
456 struct blob
*blob
= lookup_blob(sha1
);
458 blob
->object
.flags
|= FLAG_CHECKED
;
460 die("invalid blob object %s", sha1_to_hex(sha1
));
464 void *buf
= (void *) data
;
467 * we do not need to free the memory here, as the
468 * buf is deleted by the caller.
470 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
472 die("invalid %s", typename(type
));
473 if (fsck_object(obj
, 1, fsck_error_function
))
474 die("Error in object");
475 if (fsck_walk(obj
, mark_link
, NULL
))
476 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
478 if (obj
->type
== OBJ_TREE
) {
479 struct tree
*item
= (struct tree
*) obj
;
482 if (obj
->type
== OBJ_COMMIT
) {
483 struct commit
*commit
= (struct commit
*) obj
;
484 commit
->buffer
= NULL
;
486 obj
->flags
|= FLAG_CHECKED
;
491 static void *get_base_data(struct base_data
*c
)
494 struct object_entry
*obj
= c
->obj
;
496 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
497 void *base
= get_base_data(c
->base
);
498 void *raw
= get_data_from_pack(obj
);
499 c
->data
= patch_delta(
505 bad_object(obj
->idx
.offset
, "failed to apply delta");
507 c
->data
= get_data_from_pack(obj
);
511 base_cache_used
+= c
->size
;
517 static void resolve_delta(struct object_entry
*delta_obj
,
518 struct base_data
*base
, struct base_data
*result
)
520 void *base_data
, *delta_data
;
522 delta_obj
->real_type
= base
->obj
->real_type
;
523 delta_data
= get_data_from_pack(delta_obj
);
524 base_data
= get_base_data(base
);
525 result
->obj
= delta_obj
;
526 result
->data
= patch_delta(base_data
, base
->size
,
527 delta_data
, delta_obj
->size
, &result
->size
);
530 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
531 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
532 delta_obj
->idx
.sha1
);
533 nr_resolved_deltas
++;
536 static void find_unresolved_deltas(struct base_data
*base
,
537 struct base_data
*prev_base
)
539 int i
, ref_first
, ref_last
, ofs_first
, ofs_last
;
542 * This is a recursive function. Those brackets should help reducing
543 * stack usage by limiting the scope of the delta_base union.
546 union delta_base base_spec
;
548 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
549 find_delta_children(&base_spec
, &ref_first
, &ref_last
);
551 memset(&base_spec
, 0, sizeof(base_spec
));
552 base_spec
.offset
= base
->obj
->idx
.offset
;
553 find_delta_children(&base_spec
, &ofs_first
, &ofs_last
);
556 if (ref_last
== -1 && ofs_last
== -1) {
561 link_base_data(prev_base
, base
);
563 for (i
= ref_first
; i
<= ref_last
; i
++) {
564 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
565 if (child
->real_type
== OBJ_REF_DELTA
) {
566 struct base_data result
;
567 resolve_delta(child
, base
, &result
);
568 if (i
== ref_last
&& ofs_last
== -1)
569 free_base_data(base
);
570 find_unresolved_deltas(&result
, base
);
574 for (i
= ofs_first
; i
<= ofs_last
; i
++) {
575 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
576 if (child
->real_type
== OBJ_OFS_DELTA
) {
577 struct base_data result
;
578 resolve_delta(child
, base
, &result
);
580 free_base_data(base
);
581 find_unresolved_deltas(&result
, base
);
585 unlink_base_data(base
);
588 static int compare_delta_entry(const void *a
, const void *b
)
590 const struct delta_entry
*delta_a
= a
;
591 const struct delta_entry
*delta_b
= b
;
592 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
595 /* Parse all objects and return the pack content SHA1 hash */
596 static void parse_pack_objects(unsigned char *sha1
)
599 struct delta_entry
*delta
= deltas
;
604 * - find locations of all objects;
605 * - calculate SHA1 of all non-delta objects;
606 * - remember base (SHA1 or offset) for all deltas.
609 progress
= start_progress(
610 from_stdin
? "Receiving objects" : "Indexing objects",
612 for (i
= 0; i
< nr_objects
; i
++) {
613 struct object_entry
*obj
= &objects
[i
];
614 void *data
= unpack_raw_entry(obj
, &delta
->base
);
615 obj
->real_type
= obj
->type
;
616 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
621 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
623 display_progress(progress
, i
+1);
625 objects
[i
].idx
.offset
= consumed_bytes
;
626 stop_progress(&progress
);
628 /* Check pack integrity */
630 git_SHA1_Final(sha1
, &input_ctx
);
631 if (hashcmp(fill(20), sha1
))
632 die("pack is corrupted (SHA1 mismatch)");
635 /* If input_fd is a file, we should have reached its end now. */
636 if (fstat(input_fd
, &st
))
637 die_errno("cannot fstat packfile");
638 if (S_ISREG(st
.st_mode
) &&
639 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
640 die("pack has junk at the end");
645 /* Sort deltas by base SHA1/offset for fast searching */
646 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
647 compare_delta_entry
);
651 * - for all non-delta objects, look if it is used as a base for
653 * - if used as a base, uncompress the object and apply all deltas,
654 * recursively checking if the resulting object is used as a base
655 * for some more deltas.
658 progress
= start_progress("Resolving deltas", nr_deltas
);
659 for (i
= 0; i
< nr_objects
; i
++) {
660 struct object_entry
*obj
= &objects
[i
];
661 struct base_data base_obj
;
663 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
666 base_obj
.data
= NULL
;
667 find_unresolved_deltas(&base_obj
, NULL
);
668 display_progress(progress
, nr_resolved_deltas
);
672 static int write_compressed(struct sha1file
*f
, void *in
, unsigned int size
)
675 unsigned long maxsize
;
678 memset(&stream
, 0, sizeof(stream
));
679 deflateInit(&stream
, zlib_compression_level
);
680 maxsize
= deflateBound(&stream
, size
);
681 out
= xmalloc(maxsize
);
685 stream
.avail_in
= size
;
686 stream
.next_out
= out
;
687 stream
.avail_out
= maxsize
;
688 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
691 size
= stream
.total_out
;
692 sha1write(f
, out
, size
);
697 static struct object_entry
*append_obj_to_pack(struct sha1file
*f
,
698 const unsigned char *sha1
, void *buf
,
699 unsigned long size
, enum object_type type
)
701 struct object_entry
*obj
= &objects
[nr_objects
++];
702 unsigned char header
[10];
703 unsigned long s
= size
;
705 unsigned char c
= (type
<< 4) | (s
& 15);
708 header
[n
++] = c
| 0x80;
714 sha1write(f
, header
, n
);
718 obj
[0].real_type
= type
;
719 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
720 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
721 obj
[0].idx
.crc32
= crc32_end(f
);
723 hashcpy(obj
->idx
.sha1
, sha1
);
727 static int delta_pos_compare(const void *_a
, const void *_b
)
729 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
730 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
731 return a
->obj_no
- b
->obj_no
;
734 static void fix_unresolved_deltas(struct sha1file
*f
, int nr_unresolved
)
736 struct delta_entry
**sorted_by_pos
;
740 * Since many unresolved deltas may well be themselves base objects
741 * for more unresolved deltas, we really want to include the
742 * smallest number of base objects that would cover as much delta
743 * as possible by picking the
744 * trunc deltas first, allowing for other deltas to resolve without
745 * additional base objects. Since most base objects are to be found
746 * before deltas depending on them, a good heuristic is to start
747 * resolving deltas in the same order as their position in the pack.
749 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
750 for (i
= 0; i
< nr_deltas
; i
++) {
751 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
753 sorted_by_pos
[n
++] = &deltas
[i
];
755 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
757 for (i
= 0; i
< n
; i
++) {
758 struct delta_entry
*d
= sorted_by_pos
[i
];
759 enum object_type type
;
760 struct base_data base_obj
;
762 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
764 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
768 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
769 base_obj
.size
, typename(type
)))
770 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
771 base_obj
.obj
= append_obj_to_pack(f
, d
->base
.sha1
,
772 base_obj
.data
, base_obj
.size
, type
);
773 find_unresolved_deltas(&base_obj
, NULL
);
774 display_progress(progress
, nr_resolved_deltas
);
779 static void final(const char *final_pack_name
, const char *curr_pack_name
,
780 const char *final_index_name
, const char *curr_index_name
,
781 const char *keep_name
, const char *keep_msg
,
784 const char *report
= "pack";
791 fsync_or_die(output_fd
, curr_pack_name
);
792 err
= close(output_fd
);
794 die_errno("error while closing pack file");
798 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
801 keep_fd
= odb_pack_keep(name
, sizeof(name
), sha1
);
803 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
807 die_errno("cannot write keep file '%s'",
810 if (keep_msg_len
> 0) {
811 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
812 write_or_die(keep_fd
, "\n", 1);
814 if (close(keep_fd
) != 0)
815 die_errno("cannot close written keep file '%s'",
821 if (final_pack_name
!= curr_pack_name
) {
822 if (!final_pack_name
) {
823 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
824 get_object_directory(), sha1_to_hex(sha1
));
825 final_pack_name
= name
;
827 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
828 die("cannot store pack file");
829 } else if (from_stdin
)
830 chmod(final_pack_name
, 0444);
832 if (final_index_name
!= curr_index_name
) {
833 if (!final_index_name
) {
834 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
835 get_object_directory(), sha1_to_hex(sha1
));
836 final_index_name
= name
;
838 if (move_temp_to_file(curr_index_name
, final_index_name
))
839 die("cannot store index file");
841 chmod(final_index_name
, 0444);
844 printf("%s\n", sha1_to_hex(sha1
));
847 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
848 report
, sha1_to_hex(sha1
));
849 write_or_die(1, buf
, len
);
852 * Let's just mimic git-unpack-objects here and write
853 * the last part of the input buffer to stdout.
856 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
865 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
867 if (!strcmp(k
, "pack.indexversion")) {
868 pack_idx_default_version
= git_config_int(k
, v
);
869 if (pack_idx_default_version
> 2)
870 die("bad pack.indexversion=%"PRIu32
,
871 pack_idx_default_version
);
874 return git_default_config(k
, v
, cb
);
877 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
879 int i
, fix_thin_pack
= 0;
880 const char *curr_pack
, *curr_index
;
881 const char *index_name
= NULL
, *pack_name
= NULL
;
882 const char *keep_name
= NULL
, *keep_msg
= NULL
;
883 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
884 struct pack_idx_entry
**idx_objects
;
885 unsigned char pack_sha1
[20];
887 if (argc
== 2 && !strcmp(argv
[1], "-h"))
888 usage(index_pack_usage
);
891 * We wish to read the repository's config file if any, and
892 * for that it is necessary to call setup_git_directory_gently().
893 * However if the cwd was inside .git/objects/pack/ then we need
894 * to go back there or all the pack name arguments will be wrong.
895 * And in that case we cannot rely on any prefix returned by
896 * setup_git_directory_gently() either.
899 char cwd
[PATH_MAX
+1];
902 if (!getcwd(cwd
, sizeof(cwd
)-1))
903 die("Unable to get current working directory");
904 setup_git_directory_gently(&nongit
);
905 git_config(git_index_pack_config
, NULL
);
907 die("Cannot come back to cwd");
910 for (i
= 1; i
< argc
; i
++) {
911 const char *arg
= argv
[i
];
914 if (!strcmp(arg
, "--stdin")) {
916 } else if (!strcmp(arg
, "--fix-thin")) {
918 } else if (!strcmp(arg
, "--strict")) {
920 } else if (!strcmp(arg
, "--keep")) {
922 } else if (!prefixcmp(arg
, "--keep=")) {
924 } else if (!prefixcmp(arg
, "--pack_header=")) {
925 struct pack_header
*hdr
;
928 hdr
= (struct pack_header
*)input_buffer
;
929 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
930 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
933 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
936 input_len
= sizeof(*hdr
);
937 } else if (!strcmp(arg
, "-v")) {
939 } else if (!strcmp(arg
, "-o")) {
940 if (index_name
|| (i
+1) >= argc
)
941 usage(index_pack_usage
);
942 index_name
= argv
[++i
];
943 } else if (!prefixcmp(arg
, "--index-version=")) {
945 pack_idx_default_version
= strtoul(arg
+ 16, &c
, 10);
946 if (pack_idx_default_version
> 2)
949 pack_idx_off32_limit
= strtoul(c
+1, &c
, 0);
950 if (*c
|| pack_idx_off32_limit
& 0x80000000)
953 usage(index_pack_usage
);
958 usage(index_pack_usage
);
962 if (!pack_name
&& !from_stdin
)
963 usage(index_pack_usage
);
964 if (fix_thin_pack
&& !from_stdin
)
965 die("--fix-thin cannot be used without --stdin");
966 if (!index_name
&& pack_name
) {
967 int len
= strlen(pack_name
);
968 if (!has_extension(pack_name
, ".pack"))
969 die("packfile name '%s' does not end with '.pack'",
971 index_name_buf
= xmalloc(len
);
972 memcpy(index_name_buf
, pack_name
, len
- 5);
973 strcpy(index_name_buf
+ len
- 5, ".idx");
974 index_name
= index_name_buf
;
976 if (keep_msg
&& !keep_name
&& pack_name
) {
977 int len
= strlen(pack_name
);
978 if (!has_extension(pack_name
, ".pack"))
979 die("packfile name '%s' does not end with '.pack'",
981 keep_name_buf
= xmalloc(len
);
982 memcpy(keep_name_buf
, pack_name
, len
- 5);
983 strcpy(keep_name_buf
+ len
- 5, ".keep");
984 keep_name
= keep_name_buf
;
987 curr_pack
= open_pack_file(pack_name
);
989 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
990 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
991 parse_pack_objects(pack_sha1
);
992 if (nr_deltas
== nr_resolved_deltas
) {
993 stop_progress(&progress
);
994 /* Flush remaining pack final 20-byte SHA1. */
999 unsigned char read_sha1
[20], tail_sha1
[20];
1001 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
1002 int nr_objects_initial
= nr_objects
;
1003 if (nr_unresolved
<= 0)
1004 die("confusion beyond insanity");
1005 objects
= xrealloc(objects
,
1006 (nr_objects
+ nr_unresolved
+ 1)
1007 * sizeof(*objects
));
1008 f
= sha1fd(output_fd
, curr_pack
);
1009 fix_unresolved_deltas(f
, nr_unresolved
);
1010 sprintf(msg
, "completed with %d local objects",
1011 nr_objects
- nr_objects_initial
);
1012 stop_progress_msg(&progress
, msg
);
1013 sha1close(f
, tail_sha1
, 0);
1014 hashcpy(read_sha1
, pack_sha1
);
1015 fixup_pack_header_footer(output_fd
, pack_sha1
,
1016 curr_pack
, nr_objects
,
1017 read_sha1
, consumed_bytes
-20);
1018 if (hashcmp(read_sha1
, tail_sha1
) != 0)
1019 die("Unexpected tail checksum for %s "
1020 "(disk corruption?)", curr_pack
);
1022 if (nr_deltas
!= nr_resolved_deltas
)
1023 die("pack has %d unresolved deltas",
1024 nr_deltas
- nr_resolved_deltas
);
1030 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
1031 for (i
= 0; i
< nr_objects
; i
++)
1032 idx_objects
[i
] = &objects
[i
].idx
;
1033 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, pack_sha1
);
1036 final(pack_name
, curr_pack
,
1037 index_name
, curr_index
,
1038 keep_name
, keep_msg
,
1041 free(index_name_buf
);
1042 free(keep_name_buf
);
1043 if (pack_name
== NULL
)
1044 free((void *) curr_pack
);
1045 if (index_name
== NULL
)
1046 free((void *) curr_index
);