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];
35 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
36 * to memcmp() only the first 20 bytes.
38 #define UNION_BASE_SZ 20
40 #define FLAG_LINK (1u<<20)
41 #define FLAG_CHECKED (1u<<21)
45 union delta_base base
;
49 static struct object_entry
*objects
;
50 static struct delta_entry
*deltas
;
51 static int nr_objects
;
53 static int nr_resolved_deltas
;
55 static int from_stdin
;
59 static struct progress
*progress
;
61 /* We always read in 4kB chunks. */
62 static unsigned char input_buffer
[4096];
63 static unsigned int input_offset
, input_len
;
64 static off_t consumed_bytes
;
65 static SHA_CTX input_ctx
;
66 static uint32_t input_crc32
;
67 static int input_fd
, output_fd
, pack_fd
;
69 static int mark_link(struct object
*obj
, int type
, void *data
)
74 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
75 die("object type mismatch at %s", sha1_to_hex(obj
->sha1
));
77 obj
->flags
|= FLAG_LINK
;
81 /* The content of each linked object must have been checked
82 or it must be already present in the object database */
83 static void check_object(struct object
*obj
)
88 if (!(obj
->flags
& FLAG_LINK
))
91 if (!(obj
->flags
& FLAG_CHECKED
)) {
93 int type
= sha1_object_info(obj
->sha1
, &size
);
94 if (type
!= obj
->type
|| type
<= 0)
95 die("object of unexpected type");
96 obj
->flags
|= FLAG_CHECKED
;
101 static void check_objects(void)
105 max
= get_max_object_index();
106 for (i
= 0; i
< max
; i
++)
107 check_object(get_indexed_object(i
));
111 /* Discard current buffer used content. */
112 static void flush(void)
116 write_or_die(output_fd
, input_buffer
, input_offset
);
117 SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
118 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
124 * Make sure at least "min" bytes are available in the buffer, and
125 * return the pointer to the buffer.
127 static void *fill(int min
)
129 if (min
<= input_len
)
130 return input_buffer
+ input_offset
;
131 if (min
> sizeof(input_buffer
))
132 die("cannot fill %d bytes", min
);
135 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
136 sizeof(input_buffer
) - input_len
);
140 die("read error on input: %s", strerror(errno
));
144 display_throughput(progress
, consumed_bytes
+ input_len
);
145 } while (input_len
< min
);
149 static void use(int bytes
)
151 if (bytes
> input_len
)
152 die("used more bytes than were available");
153 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
155 input_offset
+= bytes
;
157 /* make sure off_t is sufficiently large not to wrap */
158 if (consumed_bytes
> consumed_bytes
+ bytes
)
159 die("pack too large for current definition of off_t");
160 consumed_bytes
+= bytes
;
163 static char *open_pack_file(char *pack_name
)
168 static char tmpfile
[PATH_MAX
];
169 snprintf(tmpfile
, sizeof(tmpfile
),
170 "%s/tmp_pack_XXXXXX", get_object_directory());
171 output_fd
= xmkstemp(tmpfile
);
172 pack_name
= xstrdup(tmpfile
);
174 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
176 die("unable to create %s: %s\n", pack_name
, strerror(errno
));
179 input_fd
= open(pack_name
, O_RDONLY
);
181 die("cannot open packfile '%s': %s",
182 pack_name
, strerror(errno
));
186 SHA1_Init(&input_ctx
);
190 static void parse_pack_header(void)
192 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
194 /* Header consistency check */
195 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
196 die("pack signature mismatch");
197 if (!pack_version_ok(hdr
->hdr_version
))
198 die("pack version %d unsupported", ntohl(hdr
->hdr_version
));
200 nr_objects
= ntohl(hdr
->hdr_entries
);
201 use(sizeof(struct pack_header
));
204 static void bad_object(unsigned long offset
, const char *format
,
205 ...) NORETURN
__attribute__((format (printf
, 2, 3)));
207 static void bad_object(unsigned long offset
, const char *format
, ...)
212 va_start(params
, format
);
213 vsnprintf(buf
, sizeof(buf
), format
, params
);
215 die("pack has bad object at offset %lu: %s", offset
, buf
);
218 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
221 void *buf
= xmalloc(size
);
223 memset(&stream
, 0, sizeof(stream
));
224 stream
.next_out
= buf
;
225 stream
.avail_out
= size
;
226 stream
.next_in
= fill(1);
227 stream
.avail_in
= input_len
;
228 inflateInit(&stream
);
231 int ret
= inflate(&stream
, 0);
232 use(input_len
- stream
.avail_in
);
233 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
236 bad_object(offset
, "inflate returned %d", ret
);
237 stream
.next_in
= fill(1);
238 stream
.avail_in
= input_len
;
244 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
252 obj
->idx
.offset
= consumed_bytes
;
253 input_crc32
= crc32(0, Z_NULL
, 0);
258 obj
->type
= (c
>> 4) & 7;
265 size
+= (c
& 0x7fUL
) << shift
;
272 hashcpy(delta_base
->sha1
, fill(20));
276 memset(delta_base
, 0, sizeof(*delta_base
));
280 base_offset
= c
& 127;
283 if (!base_offset
|| MSB(base_offset
, 7))
284 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
288 base_offset
= (base_offset
<< 7) + (c
& 127);
290 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
291 if (delta_base
->offset
>= obj
->idx
.offset
)
292 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
300 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
302 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
304 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
305 obj
->idx
.crc32
= input_crc32
;
309 static void *get_data_from_pack(struct object_entry
*obj
)
311 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
312 unsigned long len
= obj
[1].idx
.offset
- from
;
313 unsigned long rdy
= 0;
314 unsigned char *src
, *data
;
321 ssize_t n
= pread(pack_fd
, data
+ rdy
, len
- rdy
, from
+ rdy
);
323 die("cannot pread pack file: %s", strerror(errno
));
326 data
= xmalloc(obj
->size
);
327 memset(&stream
, 0, sizeof(stream
));
328 stream
.next_out
= data
;
329 stream
.avail_out
= obj
->size
;
330 stream
.next_in
= src
;
331 stream
.avail_in
= len
;
332 inflateInit(&stream
);
333 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
335 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
336 die("serious inflate inconsistency");
341 static int find_delta(const union delta_base
*base
)
343 int first
= 0, last
= nr_deltas
;
345 while (first
< last
) {
346 int next
= (first
+ last
) / 2;
347 struct delta_entry
*delta
= &deltas
[next
];
350 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
362 static int find_delta_children(const union delta_base
*base
,
363 int *first_index
, int *last_index
)
365 int first
= find_delta(base
);
367 int end
= nr_deltas
- 1;
371 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
373 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
375 *first_index
= first
;
380 static void sha1_object(const void *data
, unsigned long size
,
381 enum object_type type
, unsigned char *sha1
)
383 hash_sha1_file(data
, size
, typename(type
), sha1
);
384 if (has_sha1_file(sha1
)) {
386 enum object_type has_type
;
387 unsigned long has_size
;
388 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
390 die("cannot read existing object %s", sha1_to_hex(sha1
));
391 if (size
!= has_size
|| type
!= has_type
||
392 memcmp(data
, has_data
, size
) != 0)
393 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
397 if (type
== OBJ_BLOB
) {
398 struct blob
*blob
= lookup_blob(sha1
);
400 blob
->object
.flags
|= FLAG_CHECKED
;
402 die("invalid blob object %s", sha1_to_hex(sha1
));
406 void *buf
= (void *) data
;
409 * we do not need to free the memory here, as the
410 * buf is deleted by the caller.
412 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
414 die("invalid %s", typename(type
));
415 if (fsck_object(obj
, 1, fsck_error_function
))
416 die("Error in object");
417 if (fsck_walk(obj
, mark_link
, 0))
418 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
420 if (obj
->type
== OBJ_TREE
) {
421 struct tree
*item
= (struct tree
*) obj
;
424 if (obj
->type
== OBJ_COMMIT
) {
425 struct commit
*commit
= (struct commit
*) obj
;
426 commit
->buffer
= NULL
;
428 obj
->flags
|= FLAG_CHECKED
;
433 static void resolve_delta(struct object_entry
*delta_obj
,
434 struct base_data
*base_obj
, enum object_type type
)
437 unsigned long delta_size
;
438 union delta_base delta_base
;
440 struct base_data result
;
442 delta_obj
->real_type
= type
;
443 delta_data
= get_data_from_pack(delta_obj
);
444 delta_size
= delta_obj
->size
;
445 result
.data
= patch_delta(base_obj
->data
, base_obj
->size
,
446 delta_data
, delta_size
,
450 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
451 sha1_object(result
.data
, result
.size
, type
, delta_obj
->idx
.sha1
);
452 nr_resolved_deltas
++;
454 hashcpy(delta_base
.sha1
, delta_obj
->idx
.sha1
);
455 if (!find_delta_children(&delta_base
, &first
, &last
)) {
456 for (j
= first
; j
<= last
; j
++) {
457 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
458 if (child
->real_type
== OBJ_REF_DELTA
)
459 resolve_delta(child
, &result
, type
);
463 memset(&delta_base
, 0, sizeof(delta_base
));
464 delta_base
.offset
= delta_obj
->idx
.offset
;
465 if (!find_delta_children(&delta_base
, &first
, &last
)) {
466 for (j
= first
; j
<= last
; j
++) {
467 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
468 if (child
->real_type
== OBJ_OFS_DELTA
)
469 resolve_delta(child
, &result
, type
);
476 static int compare_delta_entry(const void *a
, const void *b
)
478 const struct delta_entry
*delta_a
= a
;
479 const struct delta_entry
*delta_b
= b
;
480 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
483 /* Parse all objects and return the pack content SHA1 hash */
484 static void parse_pack_objects(unsigned char *sha1
)
487 struct delta_entry
*delta
= deltas
;
492 * - find locations of all objects;
493 * - calculate SHA1 of all non-delta objects;
494 * - remember base (SHA1 or offset) for all deltas.
497 progress
= start_progress(
498 from_stdin
? "Receiving objects" : "Indexing objects",
500 for (i
= 0; i
< nr_objects
; i
++) {
501 struct object_entry
*obj
= &objects
[i
];
502 void *data
= unpack_raw_entry(obj
, &delta
->base
);
503 obj
->real_type
= obj
->type
;
504 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
509 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
511 display_progress(progress
, i
+1);
513 objects
[i
].idx
.offset
= consumed_bytes
;
514 stop_progress(&progress
);
516 /* Check pack integrity */
518 SHA1_Final(sha1
, &input_ctx
);
519 if (hashcmp(fill(20), sha1
))
520 die("pack is corrupted (SHA1 mismatch)");
523 /* If input_fd is a file, we should have reached its end now. */
524 if (fstat(input_fd
, &st
))
525 die("cannot fstat packfile: %s", strerror(errno
));
526 if (S_ISREG(st
.st_mode
) &&
527 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
528 die("pack has junk at the end");
533 /* Sort deltas by base SHA1/offset for fast searching */
534 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
535 compare_delta_entry
);
539 * - for all non-delta objects, look if it is used as a base for
541 * - if used as a base, uncompress the object and apply all deltas,
542 * recursively checking if the resulting object is used as a base
543 * for some more deltas.
546 progress
= start_progress("Resolving deltas", nr_deltas
);
547 for (i
= 0; i
< nr_objects
; i
++) {
548 struct object_entry
*obj
= &objects
[i
];
549 union delta_base base
;
550 int j
, ref
, ref_first
, ref_last
, ofs
, ofs_first
, ofs_last
;
551 struct base_data base_obj
;
553 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
555 hashcpy(base
.sha1
, obj
->idx
.sha1
);
556 ref
= !find_delta_children(&base
, &ref_first
, &ref_last
);
557 memset(&base
, 0, sizeof(base
));
558 base
.offset
= obj
->idx
.offset
;
559 ofs
= !find_delta_children(&base
, &ofs_first
, &ofs_last
);
562 base_obj
.data
= get_data_from_pack(obj
);
563 base_obj
.size
= obj
->size
;
566 for (j
= ref_first
; j
<= ref_last
; j
++) {
567 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
568 if (child
->real_type
== OBJ_REF_DELTA
)
569 resolve_delta(child
, &base_obj
, obj
->type
);
572 for (j
= ofs_first
; j
<= ofs_last
; j
++) {
573 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
574 if (child
->real_type
== OBJ_OFS_DELTA
)
575 resolve_delta(child
, &base_obj
, obj
->type
);
578 display_progress(progress
, nr_resolved_deltas
);
582 static int write_compressed(int fd
, void *in
, unsigned int size
, uint32_t *obj_crc
)
585 unsigned long maxsize
;
588 memset(&stream
, 0, sizeof(stream
));
589 deflateInit(&stream
, zlib_compression_level
);
590 maxsize
= deflateBound(&stream
, size
);
591 out
= xmalloc(maxsize
);
595 stream
.avail_in
= size
;
596 stream
.next_out
= out
;
597 stream
.avail_out
= maxsize
;
598 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
601 size
= stream
.total_out
;
602 write_or_die(fd
, out
, size
);
603 *obj_crc
= crc32(*obj_crc
, out
, size
);
608 static void append_obj_to_pack(const unsigned char *sha1
, void *buf
,
609 unsigned long size
, enum object_type type
)
611 struct object_entry
*obj
= &objects
[nr_objects
++];
612 unsigned char header
[10];
613 unsigned long s
= size
;
615 unsigned char c
= (type
<< 4) | (s
& 15);
618 header
[n
++] = c
| 0x80;
623 write_or_die(output_fd
, header
, n
);
624 obj
[0].idx
.crc32
= crc32(0, Z_NULL
, 0);
625 obj
[0].idx
.crc32
= crc32(obj
[0].idx
.crc32
, header
, n
);
626 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
627 obj
[1].idx
.offset
+= write_compressed(output_fd
, buf
, size
, &obj
[0].idx
.crc32
);
628 hashcpy(obj
->idx
.sha1
, sha1
);
631 static int delta_pos_compare(const void *_a
, const void *_b
)
633 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
634 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
635 return a
->obj_no
- b
->obj_no
;
638 static void fix_unresolved_deltas(int nr_unresolved
)
640 struct delta_entry
**sorted_by_pos
;
644 * Since many unresolved deltas may well be themselves base objects
645 * for more unresolved deltas, we really want to include the
646 * smallest number of base objects that would cover as much delta
647 * as possible by picking the
648 * trunc deltas first, allowing for other deltas to resolve without
649 * additional base objects. Since most base objects are to be found
650 * before deltas depending on them, a good heuristic is to start
651 * resolving deltas in the same order as their position in the pack.
653 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
654 for (i
= 0; i
< nr_deltas
; i
++) {
655 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
657 sorted_by_pos
[n
++] = &deltas
[i
];
659 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
661 for (i
= 0; i
< n
; i
++) {
662 struct delta_entry
*d
= sorted_by_pos
[i
];
663 enum object_type type
;
665 struct base_data base_obj
;
667 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
669 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
673 find_delta_children(&d
->base
, &first
, &last
);
674 for (j
= first
; j
<= last
; j
++) {
675 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
676 if (child
->real_type
== OBJ_REF_DELTA
)
677 resolve_delta(child
, &base_obj
, type
);
680 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
681 base_obj
.size
, typename(type
)))
682 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
683 append_obj_to_pack(d
->base
.sha1
, base_obj
.data
,
684 base_obj
.size
, type
);
686 display_progress(progress
, nr_resolved_deltas
);
691 static void final(const char *final_pack_name
, const char *curr_pack_name
,
692 const char *final_index_name
, const char *curr_index_name
,
693 const char *keep_name
, const char *keep_msg
,
696 const char *report
= "pack";
703 fsync_or_die(output_fd
, curr_pack_name
);
704 err
= close(output_fd
);
706 die("error while closing pack file: %s", strerror(errno
));
707 chmod(curr_pack_name
, 0444);
711 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
713 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
714 get_object_directory(), sha1_to_hex(sha1
));
717 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
720 die("cannot write keep file");
722 if (keep_msg_len
> 0) {
723 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
724 write_or_die(keep_fd
, "\n", 1);
726 if (close(keep_fd
) != 0)
727 die("cannot write keep file");
732 if (final_pack_name
!= curr_pack_name
) {
733 if (!final_pack_name
) {
734 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
735 get_object_directory(), sha1_to_hex(sha1
));
736 final_pack_name
= name
;
738 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
739 die("cannot store pack file");
742 chmod(curr_index_name
, 0444);
743 if (final_index_name
!= curr_index_name
) {
744 if (!final_index_name
) {
745 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
746 get_object_directory(), sha1_to_hex(sha1
));
747 final_index_name
= name
;
749 if (move_temp_to_file(curr_index_name
, final_index_name
))
750 die("cannot store index file");
754 printf("%s\n", sha1_to_hex(sha1
));
757 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
758 report
, sha1_to_hex(sha1
));
759 write_or_die(1, buf
, len
);
762 * Let's just mimic git-unpack-objects here and write
763 * the last part of the input buffer to stdout.
766 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
775 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
777 if (!strcmp(k
, "pack.indexversion")) {
778 pack_idx_default_version
= git_config_int(k
, v
);
779 if (pack_idx_default_version
> 2)
780 die("bad pack.indexversion=%d", pack_idx_default_version
);
783 return git_default_config(k
, v
, cb
);
786 int main(int argc
, char **argv
)
788 int i
, fix_thin_pack
= 0;
789 char *curr_pack
, *pack_name
= NULL
;
790 char *curr_index
, *index_name
= NULL
;
791 const char *keep_name
= NULL
, *keep_msg
= NULL
;
792 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
793 struct pack_idx_entry
**idx_objects
;
794 unsigned char sha1
[20];
796 git_config(git_index_pack_config
, NULL
);
798 for (i
= 1; i
< argc
; i
++) {
802 if (!strcmp(arg
, "--stdin")) {
804 } else if (!strcmp(arg
, "--fix-thin")) {
806 } else if (!strcmp(arg
, "--strict")) {
808 } else if (!strcmp(arg
, "--keep")) {
810 } else if (!prefixcmp(arg
, "--keep=")) {
812 } else if (!prefixcmp(arg
, "--pack_header=")) {
813 struct pack_header
*hdr
;
816 hdr
= (struct pack_header
*)input_buffer
;
817 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
818 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
821 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
824 input_len
= sizeof(*hdr
);
825 } else if (!strcmp(arg
, "-v")) {
827 } else if (!strcmp(arg
, "-o")) {
828 if (index_name
|| (i
+1) >= argc
)
829 usage(index_pack_usage
);
830 index_name
= argv
[++i
];
831 } else if (!prefixcmp(arg
, "--index-version=")) {
833 pack_idx_default_version
= strtoul(arg
+ 16, &c
, 10);
834 if (pack_idx_default_version
> 2)
837 pack_idx_off32_limit
= strtoul(c
+1, &c
, 0);
838 if (*c
|| pack_idx_off32_limit
& 0x80000000)
841 usage(index_pack_usage
);
846 usage(index_pack_usage
);
850 if (!pack_name
&& !from_stdin
)
851 usage(index_pack_usage
);
852 if (fix_thin_pack
&& !from_stdin
)
853 die("--fix-thin cannot be used without --stdin");
854 if (!index_name
&& pack_name
) {
855 int len
= strlen(pack_name
);
856 if (!has_extension(pack_name
, ".pack"))
857 die("packfile name '%s' does not end with '.pack'",
859 index_name_buf
= xmalloc(len
);
860 memcpy(index_name_buf
, pack_name
, len
- 5);
861 strcpy(index_name_buf
+ len
- 5, ".idx");
862 index_name
= index_name_buf
;
864 if (keep_msg
&& !keep_name
&& pack_name
) {
865 int len
= strlen(pack_name
);
866 if (!has_extension(pack_name
, ".pack"))
867 die("packfile name '%s' does not end with '.pack'",
869 keep_name_buf
= xmalloc(len
);
870 memcpy(keep_name_buf
, pack_name
, len
- 5);
871 strcpy(keep_name_buf
+ len
- 5, ".keep");
872 keep_name
= keep_name_buf
;
875 curr_pack
= open_pack_file(pack_name
);
877 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
878 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
879 parse_pack_objects(sha1
);
880 if (nr_deltas
== nr_resolved_deltas
) {
881 stop_progress(&progress
);
882 /* Flush remaining pack final 20-byte SHA1. */
887 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
888 int nr_objects_initial
= nr_objects
;
889 if (nr_unresolved
<= 0)
890 die("confusion beyond insanity");
891 objects
= xrealloc(objects
,
892 (nr_objects
+ nr_unresolved
+ 1)
894 fix_unresolved_deltas(nr_unresolved
);
895 sprintf(msg
, "completed with %d local objects",
896 nr_objects
- nr_objects_initial
);
897 stop_progress_msg(&progress
, msg
);
898 fixup_pack_header_footer(output_fd
, sha1
,
899 curr_pack
, nr_objects
);
901 if (nr_deltas
!= nr_resolved_deltas
)
902 die("pack has %d unresolved deltas",
903 nr_deltas
- nr_resolved_deltas
);
909 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
910 for (i
= 0; i
< nr_objects
; i
++)
911 idx_objects
[i
] = &objects
[i
].idx
;
912 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, sha1
);
915 final(pack_name
, curr_pack
,
916 index_name
, curr_index
,
920 free(index_name_buf
);
922 if (pack_name
== NULL
)
924 if (index_name
== NULL
)