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 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 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/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 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 %d unsupported", ntohl(hdr
->hdr_version
));
205 nr_objects
= ntohl(hdr
->hdr_entries
);
206 use(sizeof(struct pack_header
));
209 static void bad_object(unsigned long offset
, const char *format
,
210 ...) NORETURN
__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 prune_base_data(struct base_data
*retain
)
225 struct base_data
*b
= base_cache
;
227 base_cache_used
> delta_base_cache_limit
&& b
;
229 if (b
->data
&& b
!= retain
) {
232 base_cache_used
-= b
->size
;
237 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
246 base_cache_used
+= c
->size
;
250 static void unlink_base_data(struct base_data
*c
)
252 struct base_data
*base
= c
->base
;
259 base_cache_used
-= c
->size
;
263 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
266 void *buf
= xmalloc(size
);
268 memset(&stream
, 0, sizeof(stream
));
269 stream
.next_out
= buf
;
270 stream
.avail_out
= size
;
271 stream
.next_in
= fill(1);
272 stream
.avail_in
= input_len
;
273 inflateInit(&stream
);
276 int ret
= inflate(&stream
, 0);
277 use(input_len
- stream
.avail_in
);
278 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
281 bad_object(offset
, "inflate returned %d", ret
);
282 stream
.next_in
= fill(1);
283 stream
.avail_in
= input_len
;
289 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
297 obj
->idx
.offset
= consumed_bytes
;
298 input_crc32
= crc32(0, Z_NULL
, 0);
303 obj
->type
= (c
>> 4) & 7;
310 size
+= (c
& 0x7fUL
) << shift
;
317 hashcpy(delta_base
->sha1
, fill(20));
321 memset(delta_base
, 0, sizeof(*delta_base
));
325 base_offset
= c
& 127;
328 if (!base_offset
|| MSB(base_offset
, 7))
329 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
333 base_offset
= (base_offset
<< 7) + (c
& 127);
335 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
336 if (delta_base
->offset
>= obj
->idx
.offset
)
337 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
345 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
347 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
349 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
350 obj
->idx
.crc32
= input_crc32
;
354 static void *get_data_from_pack(struct object_entry
*obj
)
356 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
357 unsigned long len
= obj
[1].idx
.offset
- from
;
358 unsigned long rdy
= 0;
359 unsigned char *src
, *data
;
366 ssize_t n
= pread(pack_fd
, data
+ rdy
, len
- rdy
, from
+ rdy
);
368 die("cannot pread pack file: %s", strerror(errno
));
371 data
= xmalloc(obj
->size
);
372 memset(&stream
, 0, sizeof(stream
));
373 stream
.next_out
= data
;
374 stream
.avail_out
= obj
->size
;
375 stream
.next_in
= src
;
376 stream
.avail_in
= len
;
377 inflateInit(&stream
);
378 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
380 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
381 die("serious inflate inconsistency");
386 static int find_delta(const union delta_base
*base
)
388 int first
= 0, last
= nr_deltas
;
390 while (first
< last
) {
391 int next
= (first
+ last
) / 2;
392 struct delta_entry
*delta
= &deltas
[next
];
395 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
407 static int find_delta_children(const union delta_base
*base
,
408 int *first_index
, int *last_index
)
410 int first
= find_delta(base
);
412 int end
= nr_deltas
- 1;
416 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
418 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
420 *first_index
= first
;
425 static void sha1_object(const void *data
, unsigned long size
,
426 enum object_type type
, unsigned char *sha1
)
428 hash_sha1_file(data
, size
, typename(type
), sha1
);
429 if (has_sha1_file(sha1
)) {
431 enum object_type has_type
;
432 unsigned long has_size
;
433 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
435 die("cannot read existing object %s", sha1_to_hex(sha1
));
436 if (size
!= has_size
|| type
!= has_type
||
437 memcmp(data
, has_data
, size
) != 0)
438 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
442 if (type
== OBJ_BLOB
) {
443 struct blob
*blob
= lookup_blob(sha1
);
445 blob
->object
.flags
|= FLAG_CHECKED
;
447 die("invalid blob object %s", sha1_to_hex(sha1
));
451 void *buf
= (void *) data
;
454 * we do not need to free the memory here, as the
455 * buf is deleted by the caller.
457 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
459 die("invalid %s", typename(type
));
460 if (fsck_object(obj
, 1, fsck_error_function
))
461 die("Error in object");
462 if (fsck_walk(obj
, mark_link
, 0))
463 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
465 if (obj
->type
== OBJ_TREE
) {
466 struct tree
*item
= (struct tree
*) obj
;
469 if (obj
->type
== OBJ_COMMIT
) {
470 struct commit
*commit
= (struct commit
*) obj
;
471 commit
->buffer
= NULL
;
473 obj
->flags
|= FLAG_CHECKED
;
478 static void *get_base_data(struct base_data
*c
)
481 struct object_entry
*obj
= c
->obj
;
483 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
484 void *base
= get_base_data(c
->base
);
485 void *raw
= get_data_from_pack(obj
);
486 c
->data
= patch_delta(
492 bad_object(obj
->idx
.offset
, "failed to apply delta");
494 c
->data
= get_data_from_pack(obj
);
496 base_cache_used
+= c
->size
;
502 static void resolve_delta(struct object_entry
*delta_obj
,
503 struct base_data
*base_obj
, enum object_type type
)
506 unsigned long delta_size
;
507 union delta_base delta_base
;
509 struct base_data result
;
511 delta_obj
->real_type
= type
;
512 delta_data
= get_data_from_pack(delta_obj
);
513 delta_size
= delta_obj
->size
;
514 result
.data
= patch_delta(get_base_data(base_obj
), base_obj
->size
,
515 delta_data
, delta_size
,
519 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
520 sha1_object(result
.data
, result
.size
, type
, delta_obj
->idx
.sha1
);
521 nr_resolved_deltas
++;
523 result
.obj
= delta_obj
;
524 link_base_data(base_obj
, &result
);
526 hashcpy(delta_base
.sha1
, delta_obj
->idx
.sha1
);
527 if (!find_delta_children(&delta_base
, &first
, &last
)) {
528 for (j
= first
; j
<= last
; j
++) {
529 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
530 if (child
->real_type
== OBJ_REF_DELTA
)
531 resolve_delta(child
, &result
, type
);
535 memset(&delta_base
, 0, sizeof(delta_base
));
536 delta_base
.offset
= delta_obj
->idx
.offset
;
537 if (!find_delta_children(&delta_base
, &first
, &last
)) {
538 for (j
= first
; j
<= last
; j
++) {
539 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
540 if (child
->real_type
== OBJ_OFS_DELTA
)
541 resolve_delta(child
, &result
, type
);
545 unlink_base_data(&result
);
548 static int compare_delta_entry(const void *a
, const void *b
)
550 const struct delta_entry
*delta_a
= a
;
551 const struct delta_entry
*delta_b
= b
;
552 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
555 /* Parse all objects and return the pack content SHA1 hash */
556 static void parse_pack_objects(unsigned char *sha1
)
559 struct delta_entry
*delta
= deltas
;
564 * - find locations of all objects;
565 * - calculate SHA1 of all non-delta objects;
566 * - remember base (SHA1 or offset) for all deltas.
569 progress
= start_progress(
570 from_stdin
? "Receiving objects" : "Indexing objects",
572 for (i
= 0; i
< nr_objects
; i
++) {
573 struct object_entry
*obj
= &objects
[i
];
574 void *data
= unpack_raw_entry(obj
, &delta
->base
);
575 obj
->real_type
= obj
->type
;
576 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
581 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
583 display_progress(progress
, i
+1);
585 objects
[i
].idx
.offset
= consumed_bytes
;
586 stop_progress(&progress
);
588 /* Check pack integrity */
590 SHA1_Final(sha1
, &input_ctx
);
591 if (hashcmp(fill(20), sha1
))
592 die("pack is corrupted (SHA1 mismatch)");
595 /* If input_fd is a file, we should have reached its end now. */
596 if (fstat(input_fd
, &st
))
597 die("cannot fstat packfile: %s", strerror(errno
));
598 if (S_ISREG(st
.st_mode
) &&
599 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
600 die("pack has junk at the end");
605 /* Sort deltas by base SHA1/offset for fast searching */
606 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
607 compare_delta_entry
);
611 * - for all non-delta objects, look if it is used as a base for
613 * - if used as a base, uncompress the object and apply all deltas,
614 * recursively checking if the resulting object is used as a base
615 * for some more deltas.
618 progress
= start_progress("Resolving deltas", nr_deltas
);
619 for (i
= 0; i
< nr_objects
; i
++) {
620 struct object_entry
*obj
= &objects
[i
];
621 union delta_base base
;
622 int j
, ref
, ref_first
, ref_last
, ofs
, ofs_first
, ofs_last
;
623 struct base_data base_obj
;
625 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
627 hashcpy(base
.sha1
, obj
->idx
.sha1
);
628 ref
= !find_delta_children(&base
, &ref_first
, &ref_last
);
629 memset(&base
, 0, sizeof(base
));
630 base
.offset
= obj
->idx
.offset
;
631 ofs
= !find_delta_children(&base
, &ofs_first
, &ofs_last
);
634 base_obj
.data
= get_data_from_pack(obj
);
635 base_obj
.size
= obj
->size
;
637 link_base_data(NULL
, &base_obj
);
640 for (j
= ref_first
; j
<= ref_last
; j
++) {
641 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
642 if (child
->real_type
== OBJ_REF_DELTA
)
643 resolve_delta(child
, &base_obj
, obj
->type
);
646 for (j
= ofs_first
; j
<= ofs_last
; j
++) {
647 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
648 if (child
->real_type
== OBJ_OFS_DELTA
)
649 resolve_delta(child
, &base_obj
, obj
->type
);
651 unlink_base_data(&base_obj
);
652 display_progress(progress
, nr_resolved_deltas
);
656 static int write_compressed(int fd
, void *in
, unsigned int size
, uint32_t *obj_crc
)
659 unsigned long maxsize
;
662 memset(&stream
, 0, sizeof(stream
));
663 deflateInit(&stream
, zlib_compression_level
);
664 maxsize
= deflateBound(&stream
, size
);
665 out
= xmalloc(maxsize
);
669 stream
.avail_in
= size
;
670 stream
.next_out
= out
;
671 stream
.avail_out
= maxsize
;
672 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
675 size
= stream
.total_out
;
676 write_or_die(fd
, out
, size
);
677 *obj_crc
= crc32(*obj_crc
, out
, size
);
682 static struct object_entry
*append_obj_to_pack(
683 const unsigned char *sha1
, void *buf
,
684 unsigned long size
, enum object_type type
)
686 struct object_entry
*obj
= &objects
[nr_objects
++];
687 unsigned char header
[10];
688 unsigned long s
= size
;
690 unsigned char c
= (type
<< 4) | (s
& 15);
693 header
[n
++] = c
| 0x80;
698 write_or_die(output_fd
, header
, n
);
699 obj
[0].idx
.crc32
= crc32(0, Z_NULL
, 0);
700 obj
[0].idx
.crc32
= crc32(obj
[0].idx
.crc32
, header
, n
);
704 obj
[0].real_type
= type
;
705 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
706 obj
[1].idx
.offset
+= write_compressed(output_fd
, buf
, size
, &obj
[0].idx
.crc32
);
707 hashcpy(obj
->idx
.sha1
, sha1
);
711 static int delta_pos_compare(const void *_a
, const void *_b
)
713 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
714 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
715 return a
->obj_no
- b
->obj_no
;
718 static void fix_unresolved_deltas(int nr_unresolved
)
720 struct delta_entry
**sorted_by_pos
;
724 * Since many unresolved deltas may well be themselves base objects
725 * for more unresolved deltas, we really want to include the
726 * smallest number of base objects that would cover as much delta
727 * as possible by picking the
728 * trunc deltas first, allowing for other deltas to resolve without
729 * additional base objects. Since most base objects are to be found
730 * before deltas depending on them, a good heuristic is to start
731 * resolving deltas in the same order as their position in the pack.
733 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
734 for (i
= 0; i
< nr_deltas
; i
++) {
735 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
737 sorted_by_pos
[n
++] = &deltas
[i
];
739 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
741 for (i
= 0; i
< n
; i
++) {
742 struct delta_entry
*d
= sorted_by_pos
[i
];
743 enum object_type type
;
745 struct base_data base_obj
;
747 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
749 base_obj
.data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
.size
);
753 if (check_sha1_signature(d
->base
.sha1
, base_obj
.data
,
754 base_obj
.size
, typename(type
)))
755 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
756 base_obj
.obj
= append_obj_to_pack(d
->base
.sha1
, base_obj
.data
,
757 base_obj
.size
, type
);
758 link_base_data(NULL
, &base_obj
);
760 find_delta_children(&d
->base
, &first
, &last
);
761 for (j
= first
; j
<= last
; j
++) {
762 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
763 if (child
->real_type
== OBJ_REF_DELTA
)
764 resolve_delta(child
, &base_obj
, type
);
767 unlink_base_data(&base_obj
);
768 display_progress(progress
, nr_resolved_deltas
);
773 static void final(const char *final_pack_name
, const char *curr_pack_name
,
774 const char *final_index_name
, const char *curr_index_name
,
775 const char *keep_name
, const char *keep_msg
,
778 const char *report
= "pack";
785 fsync_or_die(output_fd
, curr_pack_name
);
786 err
= close(output_fd
);
788 die("error while closing pack file: %s", strerror(errno
));
789 chmod(curr_pack_name
, 0444);
793 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
795 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
796 get_object_directory(), sha1_to_hex(sha1
));
799 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
802 die("cannot write keep file");
804 if (keep_msg_len
> 0) {
805 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
806 write_or_die(keep_fd
, "\n", 1);
808 if (close(keep_fd
) != 0)
809 die("cannot write keep file");
814 if (final_pack_name
!= curr_pack_name
) {
815 if (!final_pack_name
) {
816 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
817 get_object_directory(), sha1_to_hex(sha1
));
818 final_pack_name
= name
;
820 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
821 die("cannot store pack file");
824 chmod(curr_index_name
, 0444);
825 if (final_index_name
!= curr_index_name
) {
826 if (!final_index_name
) {
827 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
828 get_object_directory(), sha1_to_hex(sha1
));
829 final_index_name
= name
;
831 if (move_temp_to_file(curr_index_name
, final_index_name
))
832 die("cannot store index file");
836 printf("%s\n", sha1_to_hex(sha1
));
839 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
840 report
, sha1_to_hex(sha1
));
841 write_or_die(1, buf
, len
);
844 * Let's just mimic git-unpack-objects here and write
845 * the last part of the input buffer to stdout.
848 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
857 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
859 if (!strcmp(k
, "pack.indexversion")) {
860 pack_idx_default_version
= git_config_int(k
, v
);
861 if (pack_idx_default_version
> 2)
862 die("bad pack.indexversion=%d", pack_idx_default_version
);
865 return git_default_config(k
, v
, cb
);
868 int main(int argc
, char **argv
)
870 int i
, fix_thin_pack
= 0;
871 char *curr_pack
, *pack_name
= NULL
;
872 char *curr_index
, *index_name
= NULL
;
873 const char *keep_name
= NULL
, *keep_msg
= NULL
;
874 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
875 struct pack_idx_entry
**idx_objects
;
876 unsigned char sha1
[20];
878 git_config(git_index_pack_config
, NULL
);
880 for (i
= 1; i
< argc
; i
++) {
884 if (!strcmp(arg
, "--stdin")) {
886 } else if (!strcmp(arg
, "--fix-thin")) {
888 } else if (!strcmp(arg
, "--strict")) {
890 } else if (!strcmp(arg
, "--keep")) {
892 } else if (!prefixcmp(arg
, "--keep=")) {
894 } else if (!prefixcmp(arg
, "--pack_header=")) {
895 struct pack_header
*hdr
;
898 hdr
= (struct pack_header
*)input_buffer
;
899 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
900 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
903 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
906 input_len
= sizeof(*hdr
);
907 } else if (!strcmp(arg
, "-v")) {
909 } else if (!strcmp(arg
, "-o")) {
910 if (index_name
|| (i
+1) >= argc
)
911 usage(index_pack_usage
);
912 index_name
= argv
[++i
];
913 } else if (!prefixcmp(arg
, "--index-version=")) {
915 pack_idx_default_version
= strtoul(arg
+ 16, &c
, 10);
916 if (pack_idx_default_version
> 2)
919 pack_idx_off32_limit
= strtoul(c
+1, &c
, 0);
920 if (*c
|| pack_idx_off32_limit
& 0x80000000)
923 usage(index_pack_usage
);
928 usage(index_pack_usage
);
932 if (!pack_name
&& !from_stdin
)
933 usage(index_pack_usage
);
934 if (fix_thin_pack
&& !from_stdin
)
935 die("--fix-thin cannot be used without --stdin");
936 if (!index_name
&& pack_name
) {
937 int len
= strlen(pack_name
);
938 if (!has_extension(pack_name
, ".pack"))
939 die("packfile name '%s' does not end with '.pack'",
941 index_name_buf
= xmalloc(len
);
942 memcpy(index_name_buf
, pack_name
, len
- 5);
943 strcpy(index_name_buf
+ len
- 5, ".idx");
944 index_name
= index_name_buf
;
946 if (keep_msg
&& !keep_name
&& pack_name
) {
947 int len
= strlen(pack_name
);
948 if (!has_extension(pack_name
, ".pack"))
949 die("packfile name '%s' does not end with '.pack'",
951 keep_name_buf
= xmalloc(len
);
952 memcpy(keep_name_buf
, pack_name
, len
- 5);
953 strcpy(keep_name_buf
+ len
- 5, ".keep");
954 keep_name
= keep_name_buf
;
957 curr_pack
= open_pack_file(pack_name
);
959 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
960 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
961 parse_pack_objects(sha1
);
962 if (nr_deltas
== nr_resolved_deltas
) {
963 stop_progress(&progress
);
964 /* Flush remaining pack final 20-byte SHA1. */
969 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
970 int nr_objects_initial
= nr_objects
;
971 if (nr_unresolved
<= 0)
972 die("confusion beyond insanity");
973 objects
= xrealloc(objects
,
974 (nr_objects
+ nr_unresolved
+ 1)
976 fix_unresolved_deltas(nr_unresolved
);
977 sprintf(msg
, "completed with %d local objects",
978 nr_objects
- nr_objects_initial
);
979 stop_progress_msg(&progress
, msg
);
980 fixup_pack_header_footer(output_fd
, sha1
,
981 curr_pack
, nr_objects
);
983 if (nr_deltas
!= nr_resolved_deltas
)
984 die("pack has %d unresolved deltas",
985 nr_deltas
- nr_resolved_deltas
);
991 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
992 for (i
= 0; i
< nr_objects
; i
++)
993 idx_objects
[i
] = &objects
[i
].idx
;
994 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, sha1
);
997 final(pack_name
, curr_pack
,
998 index_name
, curr_index
,
1002 free(index_name_buf
);
1003 free(keep_name_buf
);
1004 if (pack_name
== NULL
)
1006 if (index_name
== NULL
)