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>])";
17 struct pack_idx_entry idx
;
19 unsigned int hdr_size
;
20 enum object_type type
;
21 enum object_type real_type
;
25 unsigned char sha1
[20];
30 struct base_data
*base
;
31 struct base_data
*child
;
32 struct object_entry
*obj
;
38 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
39 * to memcmp() only the first 20 bytes.
41 #define UNION_BASE_SZ 20
43 #define FLAG_LINK (1u<<20)
44 #define FLAG_CHECKED (1u<<21)
47 union delta_base base
;
51 static struct object_entry
*objects
;
52 static struct delta_entry
*deltas
;
53 static struct base_data
*base_cache
;
54 static size_t base_cache_used
;
55 static int nr_objects
;
57 static int nr_resolved_deltas
;
59 static int from_stdin
;
63 static struct progress
*progress
;
65 /* We always read in 4kB chunks. */
66 static unsigned char input_buffer
[4096];
67 static unsigned int input_offset
, input_len
;
68 static off_t consumed_bytes
;
69 static git_SHA_CTX input_ctx
;
70 static uint32_t input_crc32
;
71 static int input_fd
, output_fd
, pack_fd
;
73 static int mark_link(struct object
*obj
, int type
, void *data
)
78 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
79 die("object type mismatch at %s", sha1_to_hex(obj
->sha1
));
81 obj
->flags
|= FLAG_LINK
;
85 /* The content of each linked object must have been checked
86 or it must be already present in the object database */
87 static void check_object(struct object
*obj
)
92 if (!(obj
->flags
& FLAG_LINK
))
95 if (!(obj
->flags
& FLAG_CHECKED
)) {
97 int type
= sha1_object_info(obj
->sha1
, &size
);
98 if (type
!= obj
->type
|| type
<= 0)
99 die("object of unexpected type");
100 obj
->flags
|= FLAG_CHECKED
;
105 static void check_objects(void)
109 max
= get_max_object_index();
110 for (i
= 0; i
< max
; i
++)
111 check_object(get_indexed_object(i
));
115 /* Discard current buffer used content. */
116 static void flush(void)
120 write_or_die(output_fd
, input_buffer
, input_offset
);
121 git_SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
122 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
128 * Make sure at least "min" bytes are available in the buffer, and
129 * return the pointer to the buffer.
131 static void *fill(int min
)
133 if (min
<= input_len
)
134 return input_buffer
+ input_offset
;
135 if (min
> sizeof(input_buffer
))
136 die("cannot fill %d bytes", min
);
139 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
140 sizeof(input_buffer
) - input_len
);
144 die_errno("read error on input");
148 display_throughput(progress
, consumed_bytes
+ input_len
);
149 } while (input_len
< min
);
153 static void use(int bytes
)
155 if (bytes
> input_len
)
156 die("used more bytes than were available");
157 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
159 input_offset
+= bytes
;
161 /* make sure off_t is sufficiently large not to wrap */
162 if (signed_add_overflows(consumed_bytes
, bytes
))
163 die("pack too large for current definition of off_t");
164 consumed_bytes
+= bytes
;
167 static const char *open_pack_file(const char *pack_name
)
172 static char tmpfile
[PATH_MAX
];
173 output_fd
= odb_mkstemp(tmpfile
, sizeof(tmpfile
),
174 "pack/tmp_pack_XXXXXX");
175 pack_name
= xstrdup(tmpfile
);
177 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
179 die_errno("unable to create '%s'", pack_name
);
182 input_fd
= open(pack_name
, O_RDONLY
);
184 die_errno("cannot open packfile '%s'", pack_name
);
188 git_SHA1_Init(&input_ctx
);
192 static void parse_pack_header(void)
194 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
196 /* Header consistency check */
197 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
198 die("pack signature mismatch");
199 if (!pack_version_ok(hdr
->hdr_version
))
200 die("pack version %"PRIu32
" unsupported",
201 ntohl(hdr
->hdr_version
));
203 nr_objects
= ntohl(hdr
->hdr_entries
);
204 use(sizeof(struct pack_header
));
207 static NORETURN
void bad_object(unsigned long offset
, const char *format
,
208 ...) __attribute__((format (printf
, 2, 3)));
210 static NORETURN
void bad_object(unsigned long offset
, const char *format
, ...)
215 va_start(params
, format
);
216 vsnprintf(buf
, sizeof(buf
), format
, params
);
218 die("pack has bad object at offset %lu: %s", offset
, buf
);
221 static void free_base_data(struct base_data
*c
)
226 base_cache_used
-= c
->size
;
230 static void prune_base_data(struct base_data
*retain
)
234 base_cache_used
> delta_base_cache_limit
&& b
;
236 if (b
->data
&& b
!= retain
)
241 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
251 base_cache_used
+= c
->size
;
255 static void unlink_base_data(struct base_data
*c
)
257 struct base_data
*base
= c
->base
;
265 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
269 void *buf
= xmalloc(size
);
271 memset(&stream
, 0, sizeof(stream
));
272 git_inflate_init(&stream
);
273 stream
.next_out
= buf
;
274 stream
.avail_out
= size
;
277 stream
.next_in
= fill(1);
278 stream
.avail_in
= input_len
;
279 status
= git_inflate(&stream
, 0);
280 use(input_len
- stream
.avail_in
);
281 } while (status
== Z_OK
);
282 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
283 bad_object(offset
, "inflate returned %d", status
);
284 git_inflate_end(&stream
);
288 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
291 unsigned long size
, c
;
296 obj
->idx
.offset
= consumed_bytes
;
297 input_crc32
= crc32(0, NULL
, 0);
302 obj
->type
= (c
>> 4) & 7;
309 size
+= (c
& 0x7f) << shift
;
316 hashcpy(delta_base
->sha1
, fill(20));
320 memset(delta_base
, 0, sizeof(*delta_base
));
324 base_offset
= c
& 127;
327 if (!base_offset
|| MSB(base_offset
, 7))
328 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
332 base_offset
= (base_offset
<< 7) + (c
& 127);
334 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
335 if (delta_base
->offset
<= 0 || delta_base
->offset
>= obj
->idx
.offset
)
336 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
344 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
346 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
348 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
349 obj
->idx
.crc32
= input_crc32
;
353 static void *get_data_from_pack(struct object_entry
*obj
)
355 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
356 unsigned long len
= obj
[1].idx
.offset
- from
;
357 unsigned char *data
, *inbuf
;
361 data
= xmalloc(obj
->size
);
362 inbuf
= xmalloc((len
< 64*1024) ? len
: 64*1024);
364 memset(&stream
, 0, sizeof(stream
));
365 git_inflate_init(&stream
);
366 stream
.next_out
= data
;
367 stream
.avail_out
= obj
->size
;
370 ssize_t n
= (len
< 64*1024) ? len
: 64*1024;
371 n
= pread(pack_fd
, inbuf
, n
, from
);
373 die_errno("cannot pread pack file");
375 die("premature end of pack file, %lu bytes missing", len
);
378 stream
.next_in
= inbuf
;
380 status
= git_inflate(&stream
, 0);
381 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
383 /* This has been inflated OK when first encountered, so... */
384 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
385 die("serious inflate inconsistency");
387 git_inflate_end(&stream
);
392 static int find_delta(const union delta_base
*base
)
394 int first
= 0, last
= nr_deltas
;
396 while (first
< last
) {
397 int next
= (first
+ last
) / 2;
398 struct delta_entry
*delta
= &deltas
[next
];
401 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
413 static void find_delta_children(const union delta_base
*base
,
414 int *first_index
, int *last_index
)
416 int first
= find_delta(base
);
418 int end
= nr_deltas
- 1;
425 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
427 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
429 *first_index
= first
;
433 static void sha1_object(const void *data
, unsigned long size
,
434 enum object_type type
, unsigned char *sha1
)
436 hash_sha1_file(data
, size
, typename(type
), sha1
);
437 if (has_sha1_file(sha1
)) {
439 enum object_type has_type
;
440 unsigned long has_size
;
441 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
443 die("cannot read existing object %s", sha1_to_hex(sha1
));
444 if (size
!= has_size
|| type
!= has_type
||
445 memcmp(data
, has_data
, size
) != 0)
446 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
450 if (type
== OBJ_BLOB
) {
451 struct blob
*blob
= lookup_blob(sha1
);
453 blob
->object
.flags
|= FLAG_CHECKED
;
455 die("invalid blob object %s", sha1_to_hex(sha1
));
459 void *buf
= (void *) data
;
462 * we do not need to free the memory here, as the
463 * buf is deleted by the caller.
465 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
467 die("invalid %s", typename(type
));
468 if (fsck_object(obj
, 1, fsck_error_function
))
469 die("Error in object");
470 if (fsck_walk(obj
, mark_link
, NULL
))
471 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
473 if (obj
->type
== OBJ_TREE
) {
474 struct tree
*item
= (struct tree
*) obj
;
477 if (obj
->type
== OBJ_COMMIT
) {
478 struct commit
*commit
= (struct commit
*) obj
;
479 commit
->buffer
= NULL
;
481 obj
->flags
|= FLAG_CHECKED
;
486 static void *get_base_data(struct base_data
*c
)
489 struct object_entry
*obj
= c
->obj
;
491 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
492 void *base
= get_base_data(c
->base
);
493 void *raw
= get_data_from_pack(obj
);
494 c
->data
= patch_delta(
500 bad_object(obj
->idx
.offset
, "failed to apply delta");
502 c
->data
= get_data_from_pack(obj
);
506 base_cache_used
+= c
->size
;
512 static void resolve_delta(struct object_entry
*delta_obj
,
513 struct base_data
*base
, struct base_data
*result
)
515 void *base_data
, *delta_data
;
517 delta_obj
->real_type
= base
->obj
->real_type
;
518 delta_data
= get_data_from_pack(delta_obj
);
519 base_data
= get_base_data(base
);
520 result
->obj
= delta_obj
;
521 result
->data
= patch_delta(base_data
, base
->size
,
522 delta_data
, delta_obj
->size
, &result
->size
);
525 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
526 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
527 delta_obj
->idx
.sha1
);
528 nr_resolved_deltas
++;
531 static void find_unresolved_deltas(struct base_data
*base
,
532 struct base_data
*prev_base
)
534 int i
, ref_first
, ref_last
, ofs_first
, ofs_last
;
537 * This is a recursive function. Those brackets should help reducing
538 * stack usage by limiting the scope of the delta_base union.
541 union delta_base base_spec
;
543 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
544 find_delta_children(&base_spec
, &ref_first
, &ref_last
);
546 memset(&base_spec
, 0, sizeof(base_spec
));
547 base_spec
.offset
= base
->obj
->idx
.offset
;
548 find_delta_children(&base_spec
, &ofs_first
, &ofs_last
);
551 if (ref_last
== -1 && ofs_last
== -1) {
556 link_base_data(prev_base
, base
);
558 for (i
= ref_first
; i
<= ref_last
; i
++) {
559 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
560 if (child
->real_type
== OBJ_REF_DELTA
) {
561 struct base_data result
;
562 resolve_delta(child
, base
, &result
);
563 if (i
== ref_last
&& ofs_last
== -1)
564 free_base_data(base
);
565 find_unresolved_deltas(&result
, base
);
569 for (i
= ofs_first
; i
<= ofs_last
; i
++) {
570 struct object_entry
*child
= objects
+ deltas
[i
].obj_no
;
571 if (child
->real_type
== OBJ_OFS_DELTA
) {
572 struct base_data result
;
573 resolve_delta(child
, base
, &result
);
575 free_base_data(base
);
576 find_unresolved_deltas(&result
, base
);
580 unlink_base_data(base
);
583 static int compare_delta_entry(const void *a
, const void *b
)
585 const struct delta_entry
*delta_a
= a
;
586 const struct delta_entry
*delta_b
= b
;
587 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
590 /* Parse all objects and return the pack content SHA1 hash */
591 static void parse_pack_objects(unsigned char *sha1
)
594 struct delta_entry
*delta
= deltas
;
599 * - find locations of all objects;
600 * - calculate SHA1 of all non-delta objects;
601 * - remember base (SHA1 or offset) for all deltas.
604 progress
= start_progress(
605 from_stdin
? "Receiving objects" : "Indexing objects",
607 for (i
= 0; i
< nr_objects
; i
++) {
608 struct object_entry
*obj
= &objects
[i
];
609 void *data
= unpack_raw_entry(obj
, &delta
->base
);
610 obj
->real_type
= obj
->type
;
611 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
616 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
618 display_progress(progress
, i
+1);
620 objects
[i
].idx
.offset
= consumed_bytes
;
621 stop_progress(&progress
);
623 /* Check pack integrity */
625 git_SHA1_Final(sha1
, &input_ctx
);
626 if (hashcmp(fill(20), sha1
))
627 die("pack is corrupted (SHA1 mismatch)");
630 /* If input_fd is a file, we should have reached its end now. */
631 if (fstat(input_fd
, &st
))
632 die_errno("cannot fstat packfile");
633 if (S_ISREG(st
.st_mode
) &&
634 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
635 die("pack has junk at the end");
640 /* Sort deltas by base SHA1/offset for fast searching */
641 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
642 compare_delta_entry
);
646 * - for all non-delta objects, look if it is used as a base for
648 * - if used as a base, uncompress the object and apply all deltas,
649 * recursively checking if the resulting object is used as a base
650 * for some more deltas.
653 progress
= start_progress("Resolving deltas", nr_deltas
);
654 for (i
= 0; i
< nr_objects
; i
++) {
655 struct object_entry
*obj
= &objects
[i
];
656 struct base_data base_obj
;
658 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
661 base_obj
.data
= NULL
;
662 find_unresolved_deltas(&base_obj
, NULL
);
663 display_progress(progress
, nr_resolved_deltas
);
667 static int write_compressed(struct sha1file
*f
, void *in
, unsigned int size
)
671 unsigned char outbuf
[4096];
673 memset(&stream
, 0, sizeof(stream
));
674 deflateInit(&stream
, zlib_compression_level
);
676 stream
.avail_in
= size
;
679 stream
.next_out
= outbuf
;
680 stream
.avail_out
= sizeof(outbuf
);
681 status
= deflate(&stream
, Z_FINISH
);
682 sha1write(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
683 } while (status
== Z_OK
);
685 if (status
!= Z_STREAM_END
)
686 die("unable to deflate appended object (%d)", status
);
687 size
= stream
.total_out
;
692 static struct object_entry
*append_obj_to_pack(struct sha1file
*f
,
693 const unsigned char *sha1
, void *buf
,
694 unsigned long size
, enum object_type type
)
696 struct object_entry
*obj
= &objects
[nr_objects
++];
697 unsigned char header
[10];
698 unsigned long s
= size
;
700 unsigned char c
= (type
<< 4) | (s
& 15);
703 header
[n
++] = c
| 0x80;
709 sha1write(f
, header
, n
);
713 obj
[0].real_type
= type
;
714 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
715 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
716 obj
[0].idx
.crc32
= crc32_end(f
);
718 hashcpy(obj
->idx
.sha1
, sha1
);
722 static int delta_pos_compare(const void *_a
, const void *_b
)
724 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
725 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
726 return a
->obj_no
- b
->obj_no
;
729 static void fix_unresolved_deltas(struct sha1file
*f
, int nr_unresolved
)
731 struct delta_entry
**sorted_by_pos
;
735 * Since many unresolved deltas may well be themselves base objects
736 * for more unresolved deltas, we really want to include the
737 * smallest number of base objects that would cover as much delta
738 * as possible by picking the
739 * trunc deltas first, allowing for other deltas to resolve without
740 * additional base objects. Since most base objects are to be found
741 * before deltas depending on them, a good heuristic is to start
742 * resolving deltas in the same order as their position in the pack.
744 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
745 for (i
= 0; i
< nr_deltas
; i
++) {
746 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
748 sorted_by_pos
[n
++] = &deltas
[i
];
750 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
752 for (i
= 0; i
< n
; i
++) {
753 struct delta_entry
*d
= sorted_by_pos
[i
];
754 enum object_type type
;
755 struct base_data base_obj
;
757 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
759 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
763 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
764 base_obj
.size
, typename(type
)))
765 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
766 base_obj
.obj
= append_obj_to_pack(f
, d
->base
.sha1
,
767 base_obj
.data
, base_obj
.size
, type
);
768 find_unresolved_deltas(&base_obj
, NULL
);
769 display_progress(progress
, nr_resolved_deltas
);
774 static void final(const char *final_pack_name
, const char *curr_pack_name
,
775 const char *final_index_name
, const char *curr_index_name
,
776 const char *keep_name
, const char *keep_msg
,
779 const char *report
= "pack";
786 fsync_or_die(output_fd
, curr_pack_name
);
787 err
= close(output_fd
);
789 die_errno("error while closing pack file");
793 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
796 keep_fd
= odb_pack_keep(name
, sizeof(name
), sha1
);
798 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
802 die_errno("cannot write keep file '%s'",
805 if (keep_msg_len
> 0) {
806 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
807 write_or_die(keep_fd
, "\n", 1);
809 if (close(keep_fd
) != 0)
810 die_errno("cannot close written keep file '%s'",
816 if (final_pack_name
!= curr_pack_name
) {
817 if (!final_pack_name
) {
818 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
819 get_object_directory(), sha1_to_hex(sha1
));
820 final_pack_name
= name
;
822 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
823 die("cannot store pack file");
824 } else if (from_stdin
)
825 chmod(final_pack_name
, 0444);
827 if (final_index_name
!= curr_index_name
) {
828 if (!final_index_name
) {
829 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
830 get_object_directory(), sha1_to_hex(sha1
));
831 final_index_name
= name
;
833 if (move_temp_to_file(curr_index_name
, final_index_name
))
834 die("cannot store index file");
836 chmod(final_index_name
, 0444);
839 printf("%s\n", sha1_to_hex(sha1
));
842 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
843 report
, sha1_to_hex(sha1
));
844 write_or_die(1, buf
, len
);
847 * Let's just mimic git-unpack-objects here and write
848 * the last part of the input buffer to stdout.
851 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
860 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
862 if (!strcmp(k
, "pack.indexversion")) {
863 pack_idx_default_version
= git_config_int(k
, v
);
864 if (pack_idx_default_version
> 2)
865 die("bad pack.indexversion=%"PRIu32
,
866 pack_idx_default_version
);
869 return git_default_config(k
, v
, cb
);
872 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
874 int i
, fix_thin_pack
= 0;
875 const char *curr_pack
, *curr_index
;
876 const char *index_name
= NULL
, *pack_name
= NULL
;
877 const char *keep_name
= NULL
, *keep_msg
= NULL
;
878 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
879 struct pack_idx_entry
**idx_objects
;
880 unsigned char pack_sha1
[20];
882 if (argc
== 2 && !strcmp(argv
[1], "-h"))
883 usage(index_pack_usage
);
885 read_replace_refs
= 0;
887 git_config(git_index_pack_config
, NULL
);
888 if (prefix
&& chdir(prefix
))
889 die("Cannot come back to cwd");
891 for (i
= 1; i
< argc
; i
++) {
892 const char *arg
= argv
[i
];
895 if (!strcmp(arg
, "--stdin")) {
897 } else if (!strcmp(arg
, "--fix-thin")) {
899 } else if (!strcmp(arg
, "--strict")) {
901 } else if (!strcmp(arg
, "--keep")) {
903 } else if (!prefixcmp(arg
, "--keep=")) {
905 } else if (!prefixcmp(arg
, "--pack_header=")) {
906 struct pack_header
*hdr
;
909 hdr
= (struct pack_header
*)input_buffer
;
910 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
911 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
914 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
917 input_len
= sizeof(*hdr
);
918 } else if (!strcmp(arg
, "-v")) {
920 } else if (!strcmp(arg
, "-o")) {
921 if (index_name
|| (i
+1) >= argc
)
922 usage(index_pack_usage
);
923 index_name
= argv
[++i
];
924 } else if (!prefixcmp(arg
, "--index-version=")) {
926 pack_idx_default_version
= strtoul(arg
+ 16, &c
, 10);
927 if (pack_idx_default_version
> 2)
930 pack_idx_off32_limit
= strtoul(c
+1, &c
, 0);
931 if (*c
|| pack_idx_off32_limit
& 0x80000000)
934 usage(index_pack_usage
);
939 usage(index_pack_usage
);
943 if (!pack_name
&& !from_stdin
)
944 usage(index_pack_usage
);
945 if (fix_thin_pack
&& !from_stdin
)
946 die("--fix-thin cannot be used without --stdin");
947 if (!index_name
&& pack_name
) {
948 int len
= strlen(pack_name
);
949 if (!has_extension(pack_name
, ".pack"))
950 die("packfile name '%s' does not end with '.pack'",
952 index_name_buf
= xmalloc(len
);
953 memcpy(index_name_buf
, pack_name
, len
- 5);
954 strcpy(index_name_buf
+ len
- 5, ".idx");
955 index_name
= index_name_buf
;
957 if (keep_msg
&& !keep_name
&& pack_name
) {
958 int len
= strlen(pack_name
);
959 if (!has_extension(pack_name
, ".pack"))
960 die("packfile name '%s' does not end with '.pack'",
962 keep_name_buf
= xmalloc(len
);
963 memcpy(keep_name_buf
, pack_name
, len
- 5);
964 strcpy(keep_name_buf
+ len
- 5, ".keep");
965 keep_name
= keep_name_buf
;
968 curr_pack
= open_pack_file(pack_name
);
970 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
971 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
972 parse_pack_objects(pack_sha1
);
973 if (nr_deltas
== nr_resolved_deltas
) {
974 stop_progress(&progress
);
975 /* Flush remaining pack final 20-byte SHA1. */
980 unsigned char read_sha1
[20], tail_sha1
[20];
982 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
983 int nr_objects_initial
= nr_objects
;
984 if (nr_unresolved
<= 0)
985 die("confusion beyond insanity");
986 objects
= xrealloc(objects
,
987 (nr_objects
+ nr_unresolved
+ 1)
989 f
= sha1fd(output_fd
, curr_pack
);
990 fix_unresolved_deltas(f
, nr_unresolved
);
991 sprintf(msg
, "completed with %d local objects",
992 nr_objects
- nr_objects_initial
);
993 stop_progress_msg(&progress
, msg
);
994 sha1close(f
, tail_sha1
, 0);
995 hashcpy(read_sha1
, pack_sha1
);
996 fixup_pack_header_footer(output_fd
, pack_sha1
,
997 curr_pack
, nr_objects
,
998 read_sha1
, consumed_bytes
-20);
999 if (hashcmp(read_sha1
, tail_sha1
) != 0)
1000 die("Unexpected tail checksum for %s "
1001 "(disk corruption?)", curr_pack
);
1003 if (nr_deltas
!= nr_resolved_deltas
)
1004 die("pack has %d unresolved deltas",
1005 nr_deltas
- nr_resolved_deltas
);
1011 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
1012 for (i
= 0; i
< nr_objects
; i
++)
1013 idx_objects
[i
] = &objects
[i
].idx
;
1014 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, pack_sha1
);
1017 final(pack_name
, curr_pack
,
1018 index_name
, curr_index
,
1019 keep_name
, keep_msg
,
1022 free(index_name_buf
);
1023 free(keep_name_buf
);
1024 if (pack_name
== NULL
)
1025 free((void *) curr_pack
);
1026 if (index_name
== NULL
)
1027 free((void *) curr_index
);