]>
git.ipfire.org Git - thirdparty/git.git/blob - index-pack.c
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 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 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 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("read error on input: %s", strerror(errno
));
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 (consumed_bytes
> consumed_bytes
+ bytes
)
163 die("pack too large for current definition of off_t");
164 consumed_bytes
+= bytes
;
167 static char *open_pack_file(char *pack_name
)
172 static char tmpfile
[PATH_MAX
];
173 snprintf(tmpfile
, sizeof(tmpfile
),
174 "%s/tmp_pack_XXXXXX", get_object_directory());
175 output_fd
= xmkstemp(tmpfile
);
176 pack_name
= xstrdup(tmpfile
);
178 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
180 die("unable to create %s: %s\n", pack_name
, strerror(errno
));
183 input_fd
= open(pack_name
, O_RDONLY
);
185 die("cannot open packfile '%s': %s",
186 pack_name
, strerror(errno
));
190 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 %d unsupported", ntohl(hdr
->hdr_version
));
204 nr_objects
= ntohl(hdr
->hdr_entries
);
205 use(sizeof(struct pack_header
));
208 static void bad_object(unsigned long offset
, const char *format
,
209 ...) NORETURN
__attribute__((format (printf
, 2, 3)));
211 static void bad_object(unsigned long offset
, const char *format
, ...)
216 va_start(params
, format
);
217 vsnprintf(buf
, sizeof(buf
), format
, params
);
219 die("pack has bad object at offset %lu: %s", offset
, buf
);
222 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
233 static void unlink_base_data(struct base_data
*c
)
235 struct base_data
*base
= c
->base
;
243 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
246 void *buf
= xmalloc(size
);
248 memset(&stream
, 0, sizeof(stream
));
249 stream
.next_out
= buf
;
250 stream
.avail_out
= size
;
251 stream
.next_in
= fill(1);
252 stream
.avail_in
= input_len
;
253 inflateInit(&stream
);
256 int ret
= inflate(&stream
, 0);
257 use(input_len
- stream
.avail_in
);
258 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
261 bad_object(offset
, "inflate returned %d", ret
);
262 stream
.next_in
= fill(1);
263 stream
.avail_in
= input_len
;
269 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
277 obj
->idx
.offset
= consumed_bytes
;
278 input_crc32
= crc32(0, Z_NULL
, 0);
283 obj
->type
= (c
>> 4) & 7;
290 size
+= (c
& 0x7fUL
) << shift
;
297 hashcpy(delta_base
->sha1
, fill(20));
301 memset(delta_base
, 0, sizeof(*delta_base
));
305 base_offset
= c
& 127;
308 if (!base_offset
|| MSB(base_offset
, 7))
309 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
313 base_offset
= (base_offset
<< 7) + (c
& 127);
315 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
316 if (delta_base
->offset
>= obj
->idx
.offset
)
317 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
325 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
327 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
329 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
330 obj
->idx
.crc32
= input_crc32
;
334 static void *get_data_from_pack(struct object_entry
*obj
)
336 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
337 unsigned long len
= obj
[1].idx
.offset
- from
;
338 unsigned long rdy
= 0;
339 unsigned char *src
, *data
;
346 ssize_t n
= pread(pack_fd
, data
+ rdy
, len
- rdy
, from
+ rdy
);
348 die("cannot pread pack file: %s", strerror(errno
));
351 data
= xmalloc(obj
->size
);
352 memset(&stream
, 0, sizeof(stream
));
353 stream
.next_out
= data
;
354 stream
.avail_out
= obj
->size
;
355 stream
.next_in
= src
;
356 stream
.avail_in
= len
;
357 inflateInit(&stream
);
358 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
360 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
361 die("serious inflate inconsistency");
366 static int find_delta(const union delta_base
*base
)
368 int first
= 0, last
= nr_deltas
;
370 while (first
< last
) {
371 int next
= (first
+ last
) / 2;
372 struct delta_entry
*delta
= &deltas
[next
];
375 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
387 static int find_delta_children(const union delta_base
*base
,
388 int *first_index
, int *last_index
)
390 int first
= find_delta(base
);
392 int end
= nr_deltas
- 1;
396 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
398 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
400 *first_index
= first
;
405 static void sha1_object(const void *data
, unsigned long size
,
406 enum object_type type
, unsigned char *sha1
)
408 hash_sha1_file(data
, size
, typename(type
), sha1
);
409 if (has_sha1_file(sha1
)) {
411 enum object_type has_type
;
412 unsigned long has_size
;
413 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
415 die("cannot read existing object %s", sha1_to_hex(sha1
));
416 if (size
!= has_size
|| type
!= has_type
||
417 memcmp(data
, has_data
, size
) != 0)
418 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
422 if (type
== OBJ_BLOB
) {
423 struct blob
*blob
= lookup_blob(sha1
);
425 blob
->object
.flags
|= FLAG_CHECKED
;
427 die("invalid blob object %s", sha1_to_hex(sha1
));
431 void *buf
= (void *) data
;
434 * we do not need to free the memory here, as the
435 * buf is deleted by the caller.
437 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
439 die("invalid %s", typename(type
));
440 if (fsck_object(obj
, 1, fsck_error_function
))
441 die("Error in object");
442 if (fsck_walk(obj
, mark_link
, 0))
443 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
445 if (obj
->type
== OBJ_TREE
) {
446 struct tree
*item
= (struct tree
*) obj
;
449 if (obj
->type
== OBJ_COMMIT
) {
450 struct commit
*commit
= (struct commit
*) obj
;
451 commit
->buffer
= NULL
;
453 obj
->flags
|= FLAG_CHECKED
;
458 static void resolve_delta(struct object_entry
*delta_obj
,
459 struct base_data
*base_obj
, enum object_type type
)
462 unsigned long delta_size
;
463 union delta_base delta_base
;
465 struct base_data result
;
467 delta_obj
->real_type
= type
;
468 delta_data
= get_data_from_pack(delta_obj
);
469 delta_size
= delta_obj
->size
;
470 result
.data
= patch_delta(base_obj
->data
, base_obj
->size
,
471 delta_data
, delta_size
,
475 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
476 sha1_object(result
.data
, result
.size
, type
, delta_obj
->idx
.sha1
);
477 nr_resolved_deltas
++;
479 result
.obj
= delta_obj
;
480 link_base_data(base_obj
, &result
);
482 hashcpy(delta_base
.sha1
, delta_obj
->idx
.sha1
);
483 if (!find_delta_children(&delta_base
, &first
, &last
)) {
484 for (j
= first
; j
<= last
; j
++) {
485 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
486 if (child
->real_type
== OBJ_REF_DELTA
)
487 resolve_delta(child
, &result
, type
);
491 memset(&delta_base
, 0, sizeof(delta_base
));
492 delta_base
.offset
= delta_obj
->idx
.offset
;
493 if (!find_delta_children(&delta_base
, &first
, &last
)) {
494 for (j
= first
; j
<= last
; j
++) {
495 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
496 if (child
->real_type
== OBJ_OFS_DELTA
)
497 resolve_delta(child
, &result
, type
);
501 unlink_base_data(&result
);
504 static int compare_delta_entry(const void *a
, const void *b
)
506 const struct delta_entry
*delta_a
= a
;
507 const struct delta_entry
*delta_b
= b
;
508 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
511 /* Parse all objects and return the pack content SHA1 hash */
512 static void parse_pack_objects(unsigned char *sha1
)
515 struct delta_entry
*delta
= deltas
;
520 * - find locations of all objects;
521 * - calculate SHA1 of all non-delta objects;
522 * - remember base (SHA1 or offset) for all deltas.
525 progress
= start_progress(
526 from_stdin
? "Receiving objects" : "Indexing objects",
528 for (i
= 0; i
< nr_objects
; i
++) {
529 struct object_entry
*obj
= &objects
[i
];
530 void *data
= unpack_raw_entry(obj
, &delta
->base
);
531 obj
->real_type
= obj
->type
;
532 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
537 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
539 display_progress(progress
, i
+1);
541 objects
[i
].idx
.offset
= consumed_bytes
;
542 stop_progress(&progress
);
544 /* Check pack integrity */
546 SHA1_Final(sha1
, &input_ctx
);
547 if (hashcmp(fill(20), sha1
))
548 die("pack is corrupted (SHA1 mismatch)");
551 /* If input_fd is a file, we should have reached its end now. */
552 if (fstat(input_fd
, &st
))
553 die("cannot fstat packfile: %s", strerror(errno
));
554 if (S_ISREG(st
.st_mode
) &&
555 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
556 die("pack has junk at the end");
561 /* Sort deltas by base SHA1/offset for fast searching */
562 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
563 compare_delta_entry
);
567 * - for all non-delta objects, look if it is used as a base for
569 * - if used as a base, uncompress the object and apply all deltas,
570 * recursively checking if the resulting object is used as a base
571 * for some more deltas.
574 progress
= start_progress("Resolving deltas", nr_deltas
);
575 for (i
= 0; i
< nr_objects
; i
++) {
576 struct object_entry
*obj
= &objects
[i
];
577 union delta_base base
;
578 int j
, ref
, ref_first
, ref_last
, ofs
, ofs_first
, ofs_last
;
579 struct base_data base_obj
;
581 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
583 hashcpy(base
.sha1
, obj
->idx
.sha1
);
584 ref
= !find_delta_children(&base
, &ref_first
, &ref_last
);
585 memset(&base
, 0, sizeof(base
));
586 base
.offset
= obj
->idx
.offset
;
587 ofs
= !find_delta_children(&base
, &ofs_first
, &ofs_last
);
590 base_obj
.data
= get_data_from_pack(obj
);
591 base_obj
.size
= obj
->size
;
593 link_base_data(NULL
, &base_obj
);
596 for (j
= ref_first
; j
<= ref_last
; j
++) {
597 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
598 if (child
->real_type
== OBJ_REF_DELTA
)
599 resolve_delta(child
, &base_obj
, obj
->type
);
602 for (j
= ofs_first
; j
<= ofs_last
; j
++) {
603 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
604 if (child
->real_type
== OBJ_OFS_DELTA
)
605 resolve_delta(child
, &base_obj
, obj
->type
);
607 unlink_base_data(&base_obj
);
608 display_progress(progress
, nr_resolved_deltas
);
612 static int write_compressed(int fd
, void *in
, unsigned int size
, uint32_t *obj_crc
)
615 unsigned long maxsize
;
618 memset(&stream
, 0, sizeof(stream
));
619 deflateInit(&stream
, zlib_compression_level
);
620 maxsize
= deflateBound(&stream
, size
);
621 out
= xmalloc(maxsize
);
625 stream
.avail_in
= size
;
626 stream
.next_out
= out
;
627 stream
.avail_out
= maxsize
;
628 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
631 size
= stream
.total_out
;
632 write_or_die(fd
, out
, size
);
633 *obj_crc
= crc32(*obj_crc
, out
, size
);
638 static struct object_entry
*append_obj_to_pack(
639 const unsigned char *sha1
, void *buf
,
640 unsigned long size
, enum object_type type
)
642 struct object_entry
*obj
= &objects
[nr_objects
++];
643 unsigned char header
[10];
644 unsigned long s
= size
;
646 unsigned char c
= (type
<< 4) | (s
& 15);
649 header
[n
++] = c
| 0x80;
654 write_or_die(output_fd
, header
, n
);
655 obj
[0].idx
.crc32
= crc32(0, Z_NULL
, 0);
656 obj
[0].idx
.crc32
= crc32(obj
[0].idx
.crc32
, header
, n
);
657 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
658 obj
[1].idx
.offset
+= write_compressed(output_fd
, buf
, size
, &obj
[0].idx
.crc32
);
659 hashcpy(obj
->idx
.sha1
, sha1
);
663 static int delta_pos_compare(const void *_a
, const void *_b
)
665 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
666 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
667 return a
->obj_no
- b
->obj_no
;
670 static void fix_unresolved_deltas(int nr_unresolved
)
672 struct delta_entry
**sorted_by_pos
;
676 * Since many unresolved deltas may well be themselves base objects
677 * for more unresolved deltas, we really want to include the
678 * smallest number of base objects that would cover as much delta
679 * as possible by picking the
680 * trunc deltas first, allowing for other deltas to resolve without
681 * additional base objects. Since most base objects are to be found
682 * before deltas depending on them, a good heuristic is to start
683 * resolving deltas in the same order as their position in the pack.
685 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
686 for (i
= 0; i
< nr_deltas
; i
++) {
687 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
689 sorted_by_pos
[n
++] = &deltas
[i
];
691 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
693 for (i
= 0; i
< n
; i
++) {
694 struct delta_entry
*d
= sorted_by_pos
[i
];
695 enum object_type type
;
697 struct base_data base_obj
;
699 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
701 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
705 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
706 base_obj
.size
, typename(type
)))
707 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
708 base_obj
.obj
= append_obj_to_pack(d
->base
.sha1
, base_obj
.data
,
709 base_obj
.size
, type
);
710 link_base_data(NULL
, &base_obj
);
712 find_delta_children(&d
->base
, &first
, &last
);
713 for (j
= first
; j
<= last
; j
++) {
714 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
715 if (child
->real_type
== OBJ_REF_DELTA
)
716 resolve_delta(child
, &base_obj
, type
);
719 unlink_base_data(&base_obj
);
720 display_progress(progress
, nr_resolved_deltas
);
725 static void final(const char *final_pack_name
, const char *curr_pack_name
,
726 const char *final_index_name
, const char *curr_index_name
,
727 const char *keep_name
, const char *keep_msg
,
730 const char *report
= "pack";
737 fsync_or_die(output_fd
, curr_pack_name
);
738 err
= close(output_fd
);
740 die("error while closing pack file: %s", strerror(errno
));
741 chmod(curr_pack_name
, 0444);
745 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
747 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
748 get_object_directory(), sha1_to_hex(sha1
));
751 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
754 die("cannot write keep file");
756 if (keep_msg_len
> 0) {
757 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
758 write_or_die(keep_fd
, "\n", 1);
760 if (close(keep_fd
) != 0)
761 die("cannot write keep file");
766 if (final_pack_name
!= curr_pack_name
) {
767 if (!final_pack_name
) {
768 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
769 get_object_directory(), sha1_to_hex(sha1
));
770 final_pack_name
= name
;
772 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
773 die("cannot store pack file");
776 chmod(curr_index_name
, 0444);
777 if (final_index_name
!= curr_index_name
) {
778 if (!final_index_name
) {
779 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
780 get_object_directory(), sha1_to_hex(sha1
));
781 final_index_name
= name
;
783 if (move_temp_to_file(curr_index_name
, final_index_name
))
784 die("cannot store index file");
788 printf("%s\n", sha1_to_hex(sha1
));
791 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
792 report
, sha1_to_hex(sha1
));
793 write_or_die(1, buf
, len
);
796 * Let's just mimic git-unpack-objects here and write
797 * the last part of the input buffer to stdout.
800 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
809 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
811 if (!strcmp(k
, "pack.indexversion")) {
812 pack_idx_default_version
= git_config_int(k
, v
);
813 if (pack_idx_default_version
> 2)
814 die("bad pack.indexversion=%d", pack_idx_default_version
);
817 return git_default_config(k
, v
, cb
);
820 int main(int argc
, char **argv
)
822 int i
, fix_thin_pack
= 0;
823 char *curr_pack
, *pack_name
= NULL
;
824 char *curr_index
, *index_name
= NULL
;
825 const char *keep_name
= NULL
, *keep_msg
= NULL
;
826 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
827 struct pack_idx_entry
**idx_objects
;
828 unsigned char sha1
[20];
830 git_config(git_index_pack_config
, NULL
);
832 for (i
= 1; i
< argc
; i
++) {
836 if (!strcmp(arg
, "--stdin")) {
838 } else if (!strcmp(arg
, "--fix-thin")) {
840 } else if (!strcmp(arg
, "--strict")) {
842 } else if (!strcmp(arg
, "--keep")) {
844 } else if (!prefixcmp(arg
, "--keep=")) {
846 } else if (!prefixcmp(arg
, "--pack_header=")) {
847 struct pack_header
*hdr
;
850 hdr
= (struct pack_header
*)input_buffer
;
851 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
852 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
855 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
858 input_len
= sizeof(*hdr
);
859 } else if (!strcmp(arg
, "-v")) {
861 } else if (!strcmp(arg
, "-o")) {
862 if (index_name
|| (i
+1) >= argc
)
863 usage(index_pack_usage
);
864 index_name
= argv
[++i
];
865 } else if (!prefixcmp(arg
, "--index-version=")) {
867 pack_idx_default_version
= strtoul(arg
+ 16, &c
, 10);
868 if (pack_idx_default_version
> 2)
871 pack_idx_off32_limit
= strtoul(c
+1, &c
, 0);
872 if (*c
|| pack_idx_off32_limit
& 0x80000000)
875 usage(index_pack_usage
);
880 usage(index_pack_usage
);
884 if (!pack_name
&& !from_stdin
)
885 usage(index_pack_usage
);
886 if (fix_thin_pack
&& !from_stdin
)
887 die("--fix-thin cannot be used without --stdin");
888 if (!index_name
&& pack_name
) {
889 int len
= strlen(pack_name
);
890 if (!has_extension(pack_name
, ".pack"))
891 die("packfile name '%s' does not end with '.pack'",
893 index_name_buf
= xmalloc(len
);
894 memcpy(index_name_buf
, pack_name
, len
- 5);
895 strcpy(index_name_buf
+ len
- 5, ".idx");
896 index_name
= index_name_buf
;
898 if (keep_msg
&& !keep_name
&& pack_name
) {
899 int len
= strlen(pack_name
);
900 if (!has_extension(pack_name
, ".pack"))
901 die("packfile name '%s' does not end with '.pack'",
903 keep_name_buf
= xmalloc(len
);
904 memcpy(keep_name_buf
, pack_name
, len
- 5);
905 strcpy(keep_name_buf
+ len
- 5, ".keep");
906 keep_name
= keep_name_buf
;
909 curr_pack
= open_pack_file(pack_name
);
911 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
912 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
913 parse_pack_objects(sha1
);
914 if (nr_deltas
== nr_resolved_deltas
) {
915 stop_progress(&progress
);
916 /* Flush remaining pack final 20-byte SHA1. */
921 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
922 int nr_objects_initial
= nr_objects
;
923 if (nr_unresolved
<= 0)
924 die("confusion beyond insanity");
925 objects
= xrealloc(objects
,
926 (nr_objects
+ nr_unresolved
+ 1)
928 fix_unresolved_deltas(nr_unresolved
);
929 sprintf(msg
, "completed with %d local objects",
930 nr_objects
- nr_objects_initial
);
931 stop_progress_msg(&progress
, msg
);
932 fixup_pack_header_footer(output_fd
, sha1
,
933 curr_pack
, nr_objects
);
935 if (nr_deltas
!= nr_resolved_deltas
)
936 die("pack has %d unresolved deltas",
937 nr_deltas
- nr_resolved_deltas
);
943 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
944 for (i
= 0; i
< nr_objects
; i
++)
945 idx_objects
[i
] = &objects
[i
].idx
;
946 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, sha1
);
949 final(pack_name
, curr_pack
,
950 index_name
, curr_index
,
954 free(index_name_buf
);
956 if (pack_name
== NULL
)
958 if (index_name
== NULL
)