12 static const char index_pack_usage
[] =
13 "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)
48 union delta_base base
;
52 static struct object_entry
*objects
;
53 static struct delta_entry
*deltas
;
54 static struct base_data
*base_cache
;
55 static size_t base_cache_used
;
56 static int nr_objects
;
58 static int nr_resolved_deltas
;
60 static int from_stdin
;
64 static struct progress
*progress
;
66 /* We always read in 4kB chunks. */
67 static unsigned char input_buffer
[4096];
68 static unsigned int input_offset
, input_len
;
69 static off_t consumed_bytes
;
70 static git_SHA_CTX input_ctx
;
71 static uint32_t input_crc32
;
72 static int input_fd
, output_fd
, pack_fd
;
74 static int mark_link(struct object
*obj
, int type
, void *data
)
79 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
80 die("object type mismatch at %s", sha1_to_hex(obj
->sha1
));
82 obj
->flags
|= FLAG_LINK
;
86 /* The content of each linked object must have been checked
87 or it must be already present in the object database */
88 static void check_object(struct object
*obj
)
93 if (!(obj
->flags
& FLAG_LINK
))
96 if (!(obj
->flags
& FLAG_CHECKED
)) {
98 int type
= sha1_object_info(obj
->sha1
, &size
);
99 if (type
!= obj
->type
|| type
<= 0)
100 die("object of unexpected type");
101 obj
->flags
|= FLAG_CHECKED
;
106 static void check_objects(void)
110 max
= get_max_object_index();
111 for (i
= 0; i
< max
; i
++)
112 check_object(get_indexed_object(i
));
116 /* Discard current buffer used content. */
117 static void flush(void)
121 write_or_die(output_fd
, input_buffer
, input_offset
);
122 git_SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
123 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
129 * Make sure at least "min" bytes are available in the buffer, and
130 * return the pointer to the buffer.
132 static void *fill(int min
)
134 if (min
<= input_len
)
135 return input_buffer
+ input_offset
;
136 if (min
> sizeof(input_buffer
))
137 die("cannot fill %d bytes", min
);
140 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
141 sizeof(input_buffer
) - input_len
);
145 die("read error on input: %s", strerror(errno
));
149 display_throughput(progress
, consumed_bytes
+ input_len
);
150 } while (input_len
< min
);
154 static void use(int bytes
)
156 if (bytes
> input_len
)
157 die("used more bytes than were available");
158 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
160 input_offset
+= bytes
;
162 /* make sure off_t is sufficiently large not to wrap */
163 if (consumed_bytes
> consumed_bytes
+ bytes
)
164 die("pack too large for current definition of off_t");
165 consumed_bytes
+= bytes
;
168 static char *open_pack_file(char *pack_name
)
173 static char tmpfile
[PATH_MAX
];
174 snprintf(tmpfile
, sizeof(tmpfile
),
175 "%s/pack/tmp_pack_XXXXXX", get_object_directory());
176 output_fd
= xmkstemp(tmpfile
);
177 pack_name
= xstrdup(tmpfile
);
179 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
181 die("unable to create %s: %s\n", pack_name
, strerror(errno
));
184 input_fd
= open(pack_name
, O_RDONLY
);
186 die("cannot open packfile '%s': %s",
187 pack_name
, strerror(errno
));
191 git_SHA1_Init(&input_ctx
);
195 static void parse_pack_header(void)
197 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
199 /* Header consistency check */
200 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
201 die("pack signature mismatch");
202 if (!pack_version_ok(hdr
->hdr_version
))
203 die("pack version %"PRIu32
" unsupported",
204 ntohl(hdr
->hdr_version
));
206 nr_objects
= ntohl(hdr
->hdr_entries
);
207 use(sizeof(struct pack_header
));
210 static void bad_object(unsigned long offset
, const char *format
,
211 ...) NORETURN
__attribute__((format (printf
, 2, 3)));
213 static void bad_object(unsigned long offset
, const char *format
, ...)
218 va_start(params
, format
);
219 vsnprintf(buf
, sizeof(buf
), format
, params
);
221 die("pack has bad object at offset %lu: %s", offset
, buf
);
224 static void free_base_data(struct base_data
*c
)
229 base_cache_used
-= c
->size
;
233 static void prune_base_data(struct base_data
*retain
)
235 struct base_data
*b
= base_cache
;
237 base_cache_used
> delta_base_cache_limit
&& b
;
239 if (b
->data
&& b
!= retain
)
244 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
254 base_cache_used
+= c
->size
;
258 static void unlink_base_data(struct base_data
*c
)
260 struct base_data
*base
= c
->base
;
268 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
271 void *buf
= xmalloc(size
);
273 memset(&stream
, 0, sizeof(stream
));
274 stream
.next_out
= buf
;
275 stream
.avail_out
= size
;
276 stream
.next_in
= fill(1);
277 stream
.avail_in
= input_len
;
278 inflateInit(&stream
);
281 int ret
= inflate(&stream
, 0);
282 use(input_len
- stream
.avail_in
);
283 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
286 bad_object(offset
, "inflate returned %d", ret
);
287 stream
.next_in
= fill(1);
288 stream
.avail_in
= input_len
;
294 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
302 obj
->idx
.offset
= consumed_bytes
;
303 input_crc32
= crc32(0, Z_NULL
, 0);
308 obj
->type
= (c
>> 4) & 7;
315 size
+= (c
& 0x7fUL
) << shift
;
322 hashcpy(delta_base
->sha1
, fill(20));
326 memset(delta_base
, 0, sizeof(*delta_base
));
330 base_offset
= c
& 127;
333 if (!base_offset
|| MSB(base_offset
, 7))
334 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
338 base_offset
= (base_offset
<< 7) + (c
& 127);
340 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
341 if (delta_base
->offset
>= obj
->idx
.offset
)
342 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
350 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
352 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
354 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
355 obj
->idx
.crc32
= input_crc32
;
359 static void *get_data_from_pack(struct object_entry
*obj
)
361 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
362 unsigned long len
= obj
[1].idx
.offset
- from
;
363 unsigned long rdy
= 0;
364 unsigned char *src
, *data
;
371 ssize_t n
= pread(pack_fd
, data
+ rdy
, len
- rdy
, from
+ rdy
);
373 die("cannot pread pack file: %s", strerror(errno
));
375 die("premature end of pack file, %lu bytes missing",
379 data
= xmalloc(obj
->size
);
380 memset(&stream
, 0, sizeof(stream
));
381 stream
.next_out
= data
;
382 stream
.avail_out
= obj
->size
;
383 stream
.next_in
= src
;
384 stream
.avail_in
= len
;
385 inflateInit(&stream
);
386 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
388 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
389 die("serious inflate inconsistency");
394 static int find_delta(const union delta_base
*base
)
396 int first
= 0, last
= nr_deltas
;
398 while (first
< last
) {
399 int next
= (first
+ last
) / 2;
400 struct delta_entry
*delta
= &deltas
[next
];
403 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
415 static void find_delta_children(const union delta_base
*base
,
416 int *first_index
, int *last_index
)
418 int first
= find_delta(base
);
420 int end
= nr_deltas
- 1;
427 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
429 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
431 *first_index
= first
;
435 static void sha1_object(const void *data
, unsigned long size
,
436 enum object_type type
, unsigned char *sha1
)
438 hash_sha1_file(data
, size
, typename(type
), sha1
);
439 if (has_sha1_file(sha1
)) {
441 enum object_type has_type
;
442 unsigned long has_size
;
443 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
445 die("cannot read existing object %s", sha1_to_hex(sha1
));
446 if (size
!= has_size
|| type
!= has_type
||
447 memcmp(data
, has_data
, size
) != 0)
448 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
452 if (type
== OBJ_BLOB
) {
453 struct blob
*blob
= lookup_blob(sha1
);
455 blob
->object
.flags
|= FLAG_CHECKED
;
457 die("invalid blob object %s", sha1_to_hex(sha1
));
461 void *buf
= (void *) data
;
464 * we do not need to free the memory here, as the
465 * buf is deleted by the caller.
467 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
469 die("invalid %s", typename(type
));
470 if (fsck_object(obj
, 1, fsck_error_function
))
471 die("Error in object");
472 if (fsck_walk(obj
, mark_link
, 0))
473 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
475 if (obj
->type
== OBJ_TREE
) {
476 struct tree
*item
= (struct tree
*) obj
;
479 if (obj
->type
== OBJ_COMMIT
) {
480 struct commit
*commit
= (struct commit
*) obj
;
481 commit
->buffer
= NULL
;
483 obj
->flags
|= FLAG_CHECKED
;
488 static void *get_base_data(struct base_data
*c
)
491 struct object_entry
*obj
= c
->obj
;
493 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
494 void *base
= get_base_data(c
->base
);
495 void *raw
= get_data_from_pack(obj
);
496 c
->data
= patch_delta(
502 bad_object(obj
->idx
.offset
, "failed to apply delta");
504 c
->data
= get_data_from_pack(obj
);
508 base_cache_used
+= c
->size
;
514 static void resolve_delta(struct object_entry
*delta_obj
,
515 struct base_data
*base
, struct base_data
*result
)
517 void *base_data
, *delta_data
;
519 delta_obj
->real_type
= base
->obj
->real_type
;
520 delta_data
= get_data_from_pack(delta_obj
);
521 base_data
= get_base_data(base
);
522 result
->obj
= delta_obj
;
523 result
->data
= patch_delta(base_data
, base
->size
,
524 delta_data
, delta_obj
->size
, &result
->size
);
527 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
528 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
529 delta_obj
->idx
.sha1
);
530 nr_resolved_deltas
++;
533 static void find_unresolved_deltas(struct base_data
*base
,
534 struct base_data
*prev_base
)
536 int i
, ref_first
, ref_last
, ofs_first
, ofs_last
;
539 * This is a recursive function. Those brackets should help reducing
540 * stack usage by limiting the scope of the delta_base union.
543 union delta_base base_spec
;
545 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
546 find_delta_children(&base_spec
, &ref_first
, &ref_last
);
548 memset(&base_spec
, 0, sizeof(base_spec
));
549 base_spec
.offset
= base
->obj
->idx
.offset
;
550 find_delta_children(&base_spec
, &ofs_first
, &ofs_last
);
553 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("cannot fstat packfile: %s", strerror(errno
));
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
)
670 unsigned long maxsize
;
673 memset(&stream
, 0, sizeof(stream
));
674 deflateInit(&stream
, zlib_compression_level
);
675 maxsize
= deflateBound(&stream
, size
);
676 out
= xmalloc(maxsize
);
680 stream
.avail_in
= size
;
681 stream
.next_out
= out
;
682 stream
.avail_out
= maxsize
;
683 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
686 size
= stream
.total_out
;
687 sha1write(f
, out
, size
);
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("error while closing pack file: %s", strerror(errno
));
790 chmod(curr_pack_name
, 0444);
794 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
796 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
797 get_object_directory(), sha1_to_hex(sha1
));
800 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
803 die("cannot write keep file");
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("cannot write keep file");
815 if (final_pack_name
!= curr_pack_name
) {
816 if (!final_pack_name
) {
817 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
818 get_object_directory(), sha1_to_hex(sha1
));
819 final_pack_name
= name
;
821 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
822 die("cannot store pack file");
825 chmod(curr_index_name
, 0444);
826 if (final_index_name
!= curr_index_name
) {
827 if (!final_index_name
) {
828 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
829 get_object_directory(), sha1_to_hex(sha1
));
830 final_index_name
= name
;
832 if (move_temp_to_file(curr_index_name
, final_index_name
))
833 die("cannot store index file");
837 printf("%s\n", sha1_to_hex(sha1
));
840 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
841 report
, sha1_to_hex(sha1
));
842 write_or_die(1, buf
, len
);
845 * Let's just mimic git-unpack-objects here and write
846 * the last part of the input buffer to stdout.
849 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
858 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
860 if (!strcmp(k
, "pack.indexversion")) {
861 pack_idx_default_version
= git_config_int(k
, v
);
862 if (pack_idx_default_version
> 2)
863 die("bad pack.indexversion=%"PRIu32
,
864 pack_idx_default_version
);
867 return git_default_config(k
, v
, cb
);
870 int main(int argc
, char **argv
)
872 int i
, fix_thin_pack
= 0;
873 char *curr_pack
, *pack_name
= NULL
;
874 char *curr_index
, *index_name
= NULL
;
875 const char *keep_name
= NULL
, *keep_msg
= NULL
;
876 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
877 struct pack_idx_entry
**idx_objects
;
878 unsigned char pack_sha1
[20];
881 setup_git_directory_gently(&nongit
);
882 git_config(git_index_pack_config
, NULL
);
884 for (i
= 1; i
< argc
; i
++) {
888 if (!strcmp(arg
, "--stdin")) {
890 } else if (!strcmp(arg
, "--fix-thin")) {
892 } else if (!strcmp(arg
, "--strict")) {
894 } else if (!strcmp(arg
, "--keep")) {
896 } else if (!prefixcmp(arg
, "--keep=")) {
898 } else if (!prefixcmp(arg
, "--pack_header=")) {
899 struct pack_header
*hdr
;
902 hdr
= (struct pack_header
*)input_buffer
;
903 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
904 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
907 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
910 input_len
= sizeof(*hdr
);
911 } else if (!strcmp(arg
, "-v")) {
913 } else if (!strcmp(arg
, "-o")) {
914 if (index_name
|| (i
+1) >= argc
)
915 usage(index_pack_usage
);
916 index_name
= argv
[++i
];
917 } else if (!prefixcmp(arg
, "--index-version=")) {
919 pack_idx_default_version
= strtoul(arg
+ 16, &c
, 10);
920 if (pack_idx_default_version
> 2)
923 pack_idx_off32_limit
= strtoul(c
+1, &c
, 0);
924 if (*c
|| pack_idx_off32_limit
& 0x80000000)
927 usage(index_pack_usage
);
932 usage(index_pack_usage
);
936 if (!pack_name
&& !from_stdin
)
937 usage(index_pack_usage
);
938 if (fix_thin_pack
&& !from_stdin
)
939 die("--fix-thin cannot be used without --stdin");
940 if (!index_name
&& pack_name
) {
941 int len
= strlen(pack_name
);
942 if (!has_extension(pack_name
, ".pack"))
943 die("packfile name '%s' does not end with '.pack'",
945 index_name_buf
= xmalloc(len
);
946 memcpy(index_name_buf
, pack_name
, len
- 5);
947 strcpy(index_name_buf
+ len
- 5, ".idx");
948 index_name
= index_name_buf
;
950 if (keep_msg
&& !keep_name
&& pack_name
) {
951 int len
= strlen(pack_name
);
952 if (!has_extension(pack_name
, ".pack"))
953 die("packfile name '%s' does not end with '.pack'",
955 keep_name_buf
= xmalloc(len
);
956 memcpy(keep_name_buf
, pack_name
, len
- 5);
957 strcpy(keep_name_buf
+ len
- 5, ".keep");
958 keep_name
= keep_name_buf
;
961 curr_pack
= open_pack_file(pack_name
);
963 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
964 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
965 parse_pack_objects(pack_sha1
);
966 if (nr_deltas
== nr_resolved_deltas
) {
967 stop_progress(&progress
);
968 /* Flush remaining pack final 20-byte SHA1. */
973 unsigned char read_sha1
[20], tail_sha1
[20];
975 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
976 int nr_objects_initial
= nr_objects
;
977 if (nr_unresolved
<= 0)
978 die("confusion beyond insanity");
979 objects
= xrealloc(objects
,
980 (nr_objects
+ nr_unresolved
+ 1)
982 f
= sha1fd(output_fd
, curr_pack
);
983 fix_unresolved_deltas(f
, nr_unresolved
);
984 sprintf(msg
, "completed with %d local objects",
985 nr_objects
- nr_objects_initial
);
986 stop_progress_msg(&progress
, msg
);
987 sha1close(f
, tail_sha1
, 0);
988 hashcpy(read_sha1
, pack_sha1
);
989 fixup_pack_header_footer(output_fd
, pack_sha1
,
990 curr_pack
, nr_objects
,
991 read_sha1
, consumed_bytes
-20);
992 if (hashcmp(read_sha1
, tail_sha1
) != 0)
993 die("Unexpected tail checksum for %s "
994 "(disk corruption?)", curr_pack
);
996 if (nr_deltas
!= nr_resolved_deltas
)
997 die("pack has %d unresolved deltas",
998 nr_deltas
- nr_resolved_deltas
);
1004 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
1005 for (i
= 0; i
< nr_objects
; i
++)
1006 idx_objects
[i
] = &objects
[i
].idx
;
1007 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, pack_sha1
);
1010 final(pack_name
, curr_pack
,
1011 index_name
, curr_index
,
1012 keep_name
, keep_msg
,
1015 free(index_name_buf
);
1016 free(keep_name_buf
);
1017 if (pack_name
== NULL
)
1019 if (index_name
== NULL
)