10 static const char index_pack_usage
[] =
11 "git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
17 unsigned int hdr_size
;
19 enum object_type type
;
20 enum object_type real_type
;
21 unsigned char sha1
[20];
25 unsigned char sha1
[20];
30 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
31 * to memcmp() only the first 20 bytes.
33 #define UNION_BASE_SZ 20
37 union delta_base base
;
41 static struct object_entry
*objects
;
42 static struct delta_entry
*deltas
;
43 static int nr_objects
;
45 static int nr_resolved_deltas
;
47 static int from_stdin
;
50 static volatile sig_atomic_t progress_update
;
52 static void progress_interval(int signum
)
57 static void setup_progress_signal(void)
62 memset(&sa
, 0, sizeof(sa
));
63 sa
.sa_handler
= progress_interval
;
64 sigemptyset(&sa
.sa_mask
);
65 sa
.sa_flags
= SA_RESTART
;
66 sigaction(SIGALRM
, &sa
, NULL
);
68 v
.it_interval
.tv_sec
= 1;
69 v
.it_interval
.tv_usec
= 0;
70 v
.it_value
= v
.it_interval
;
71 setitimer(ITIMER_REAL
, &v
, NULL
);
75 static unsigned display_progress(unsigned n
, unsigned total
, unsigned last_pc
)
77 unsigned percent
= n
* 100 / total
;
78 if (percent
!= last_pc
|| progress_update
) {
79 fprintf(stderr
, "%4u%% (%u/%u) done\r", percent
, n
, total
);
85 /* We always read in 4kB chunks. */
86 static unsigned char input_buffer
[4096];
87 static unsigned int input_offset
, input_len
;
88 static off_t consumed_bytes
;
89 static SHA_CTX input_ctx
;
90 static uint32_t input_crc32
;
91 static int input_fd
, output_fd
, pack_fd
;
93 /* Discard current buffer used content. */
94 static void flush(void)
98 write_or_die(output_fd
, input_buffer
, input_offset
);
99 SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
100 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
106 * Make sure at least "min" bytes are available in the buffer, and
107 * return the pointer to the buffer.
109 static void *fill(int min
)
111 if (min
<= input_len
)
112 return input_buffer
+ input_offset
;
113 if (min
> sizeof(input_buffer
))
114 die("cannot fill %d bytes", min
);
117 int ret
= xread(input_fd
, input_buffer
+ input_len
,
118 sizeof(input_buffer
) - input_len
);
122 die("read error on input: %s", strerror(errno
));
125 } while (input_len
< min
);
129 static void use(int bytes
)
131 if (bytes
> input_len
)
132 die("used more bytes than were available");
133 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
135 input_offset
+= bytes
;
137 /* make sure off_t is sufficiently large not to wrap */
138 if (consumed_bytes
> consumed_bytes
+ bytes
)
139 die("pack too large for current definition of off_t");
140 consumed_bytes
+= bytes
;
143 static const char *open_pack_file(const char *pack_name
)
148 static char tmpfile
[PATH_MAX
];
149 snprintf(tmpfile
, sizeof(tmpfile
),
150 "%s/tmp_pack_XXXXXX", get_object_directory());
151 output_fd
= mkstemp(tmpfile
);
152 pack_name
= xstrdup(tmpfile
);
154 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
156 die("unable to create %s: %s\n", pack_name
, strerror(errno
));
159 input_fd
= open(pack_name
, O_RDONLY
);
161 die("cannot open packfile '%s': %s",
162 pack_name
, strerror(errno
));
166 SHA1_Init(&input_ctx
);
170 static void parse_pack_header(void)
172 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
174 /* Header consistency check */
175 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
176 die("pack signature mismatch");
177 if (!pack_version_ok(hdr
->hdr_version
))
178 die("pack version %d unsupported", ntohl(hdr
->hdr_version
));
180 nr_objects
= ntohl(hdr
->hdr_entries
);
181 use(sizeof(struct pack_header
));
184 static void bad_object(unsigned long offset
, const char *format
,
185 ...) NORETURN
__attribute__((format (printf
, 2, 3)));
187 static void bad_object(unsigned long offset
, const char *format
, ...)
192 va_start(params
, format
);
193 vsnprintf(buf
, sizeof(buf
), format
, params
);
195 die("pack has bad object at offset %lu: %s", offset
, buf
);
198 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
201 void *buf
= xmalloc(size
);
203 memset(&stream
, 0, sizeof(stream
));
204 stream
.next_out
= buf
;
205 stream
.avail_out
= size
;
206 stream
.next_in
= fill(1);
207 stream
.avail_in
= input_len
;
208 inflateInit(&stream
);
211 int ret
= inflate(&stream
, 0);
212 use(input_len
- stream
.avail_in
);
213 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
216 bad_object(offset
, "inflate returned %d", ret
);
217 stream
.next_in
= fill(1);
218 stream
.avail_in
= input_len
;
224 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
232 obj
->offset
= consumed_bytes
;
233 input_crc32
= crc32(0, Z_NULL
, 0);
238 obj
->type
= (c
>> 4) & 7;
245 size
+= (c
& 0x7fUL
) << shift
;
252 hashcpy(delta_base
->sha1
, fill(20));
256 memset(delta_base
, 0, sizeof(*delta_base
));
260 base_offset
= c
& 127;
263 if (!base_offset
|| MSB(base_offset
, 7))
264 bad_object(obj
->offset
, "offset value overflow for delta base object");
268 base_offset
= (base_offset
<< 7) + (c
& 127);
270 delta_base
->offset
= obj
->offset
- base_offset
;
271 if (delta_base
->offset
>= obj
->offset
)
272 bad_object(obj
->offset
, "delta base offset is out of bound");
280 bad_object(obj
->offset
, "unknown object type %d", obj
->type
);
282 obj
->hdr_size
= consumed_bytes
- obj
->offset
;
284 data
= unpack_entry_data(obj
->offset
, obj
->size
);
285 obj
->crc32
= input_crc32
;
289 static void *get_data_from_pack(struct object_entry
*obj
)
291 unsigned long from
= obj
[0].offset
+ obj
[0].hdr_size
;
292 unsigned long len
= obj
[1].offset
- from
;
293 unsigned long rdy
= 0;
294 unsigned char *src
, *data
;
301 ssize_t n
= pread(pack_fd
, data
+ rdy
, len
- rdy
, from
+ rdy
);
303 die("cannot pread pack file: %s", strerror(errno
));
306 data
= xmalloc(obj
->size
);
307 memset(&stream
, 0, sizeof(stream
));
308 stream
.next_out
= data
;
309 stream
.avail_out
= obj
->size
;
310 stream
.next_in
= src
;
311 stream
.avail_in
= len
;
312 inflateInit(&stream
);
313 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
315 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
316 die("serious inflate inconsistency");
321 static int find_delta(const union delta_base
*base
)
323 int first
= 0, last
= nr_deltas
;
325 while (first
< last
) {
326 int next
= (first
+ last
) / 2;
327 struct delta_entry
*delta
= &deltas
[next
];
330 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
342 static int find_delta_children(const union delta_base
*base
,
343 int *first_index
, int *last_index
)
345 int first
= find_delta(base
);
347 int end
= nr_deltas
- 1;
351 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
353 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
355 *first_index
= first
;
360 static void sha1_object(const void *data
, unsigned long size
,
361 enum object_type type
, unsigned char *sha1
)
363 hash_sha1_file(data
, size
, typename(type
), sha1
);
364 if (has_sha1_file(sha1
)) {
366 enum object_type has_type
;
367 unsigned long has_size
;
368 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
370 die("cannot read existing object %s", sha1_to_hex(sha1
));
371 if (size
!= has_size
|| type
!= has_type
||
372 memcmp(data
, has_data
, size
) != 0)
373 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
378 static void resolve_delta(struct object_entry
*delta_obj
, void *base_data
,
379 unsigned long base_size
, enum object_type type
)
382 unsigned long delta_size
;
384 unsigned long result_size
;
385 union delta_base delta_base
;
388 delta_obj
->real_type
= type
;
389 delta_data
= get_data_from_pack(delta_obj
);
390 delta_size
= delta_obj
->size
;
391 result
= patch_delta(base_data
, base_size
, delta_data
, delta_size
,
395 bad_object(delta_obj
->offset
, "failed to apply delta");
396 sha1_object(result
, result_size
, type
, delta_obj
->sha1
);
397 nr_resolved_deltas
++;
399 hashcpy(delta_base
.sha1
, delta_obj
->sha1
);
400 if (!find_delta_children(&delta_base
, &first
, &last
)) {
401 for (j
= first
; j
<= last
; j
++) {
402 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
403 if (child
->real_type
== OBJ_REF_DELTA
)
404 resolve_delta(child
, result
, result_size
, type
);
408 memset(&delta_base
, 0, sizeof(delta_base
));
409 delta_base
.offset
= delta_obj
->offset
;
410 if (!find_delta_children(&delta_base
, &first
, &last
)) {
411 for (j
= first
; j
<= last
; j
++) {
412 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
413 if (child
->real_type
== OBJ_OFS_DELTA
)
414 resolve_delta(child
, result
, result_size
, type
);
421 static int compare_delta_entry(const void *a
, const void *b
)
423 const struct delta_entry
*delta_a
= a
;
424 const struct delta_entry
*delta_b
= b
;
425 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
428 /* Parse all objects and return the pack content SHA1 hash */
429 static void parse_pack_objects(unsigned char *sha1
)
432 struct delta_entry
*delta
= deltas
;
438 * - find locations of all objects;
439 * - calculate SHA1 of all non-delta objects;
440 * - remember base (SHA1 or offset) for all deltas.
443 fprintf(stderr
, "Indexing %d objects.\n", nr_objects
);
444 for (i
= 0; i
< nr_objects
; i
++) {
445 struct object_entry
*obj
= &objects
[i
];
446 data
= unpack_raw_entry(obj
, &delta
->base
);
447 obj
->real_type
= obj
->type
;
448 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
453 sha1_object(data
, obj
->size
, obj
->type
, obj
->sha1
);
456 percent
= display_progress(i
+1, nr_objects
, percent
);
458 objects
[i
].offset
= consumed_bytes
;
462 /* Check pack integrity */
464 SHA1_Final(sha1
, &input_ctx
);
465 if (hashcmp(fill(20), sha1
))
466 die("pack is corrupted (SHA1 mismatch)");
469 /* If input_fd is a file, we should have reached its end now. */
470 if (fstat(input_fd
, &st
))
471 die("cannot fstat packfile: %s", strerror(errno
));
472 if (S_ISREG(st
.st_mode
) &&
473 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
474 die("pack has junk at the end");
479 /* Sort deltas by base SHA1/offset for fast searching */
480 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
481 compare_delta_entry
);
485 * - for all non-delta objects, look if it is used as a base for
487 * - if used as a base, uncompress the object and apply all deltas,
488 * recursively checking if the resulting object is used as a base
489 * for some more deltas.
492 fprintf(stderr
, "Resolving %d deltas.\n", nr_deltas
);
493 for (i
= 0; i
< nr_objects
; i
++) {
494 struct object_entry
*obj
= &objects
[i
];
495 union delta_base base
;
496 int j
, ref
, ref_first
, ref_last
, ofs
, ofs_first
, ofs_last
;
498 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
500 hashcpy(base
.sha1
, obj
->sha1
);
501 ref
= !find_delta_children(&base
, &ref_first
, &ref_last
);
502 memset(&base
, 0, sizeof(base
));
503 base
.offset
= obj
->offset
;
504 ofs
= !find_delta_children(&base
, &ofs_first
, &ofs_last
);
507 data
= get_data_from_pack(obj
);
509 for (j
= ref_first
; j
<= ref_last
; j
++) {
510 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
511 if (child
->real_type
== OBJ_REF_DELTA
)
512 resolve_delta(child
, data
,
513 obj
->size
, obj
->type
);
516 for (j
= ofs_first
; j
<= ofs_last
; j
++) {
517 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
518 if (child
->real_type
== OBJ_OFS_DELTA
)
519 resolve_delta(child
, data
,
520 obj
->size
, obj
->type
);
524 percent
= display_progress(nr_resolved_deltas
,
527 if (verbose
&& nr_resolved_deltas
== nr_deltas
)
531 static int write_compressed(int fd
, void *in
, unsigned int size
, uint32_t *obj_crc
)
534 unsigned long maxsize
;
537 memset(&stream
, 0, sizeof(stream
));
538 deflateInit(&stream
, zlib_compression_level
);
539 maxsize
= deflateBound(&stream
, size
);
540 out
= xmalloc(maxsize
);
544 stream
.avail_in
= size
;
545 stream
.next_out
= out
;
546 stream
.avail_out
= maxsize
;
547 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
550 size
= stream
.total_out
;
551 write_or_die(fd
, out
, size
);
552 *obj_crc
= crc32(*obj_crc
, out
, size
);
557 static void append_obj_to_pack(const unsigned char *sha1
, void *buf
,
558 unsigned long size
, enum object_type type
)
560 struct object_entry
*obj
= &objects
[nr_objects
++];
561 unsigned char header
[10];
562 unsigned long s
= size
;
564 unsigned char c
= (type
<< 4) | (s
& 15);
567 header
[n
++] = c
| 0x80;
572 write_or_die(output_fd
, header
, n
);
573 obj
[0].crc32
= crc32(0, Z_NULL
, 0);
574 obj
[0].crc32
= crc32(obj
[0].crc32
, header
, n
);
575 obj
[1].offset
= obj
[0].offset
+ n
;
576 obj
[1].offset
+= write_compressed(output_fd
, buf
, size
, &obj
[0].crc32
);
577 hashcpy(obj
->sha1
, sha1
);
580 static int delta_pos_compare(const void *_a
, const void *_b
)
582 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
583 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
584 return a
->obj_no
- b
->obj_no
;
587 static void fix_unresolved_deltas(int nr_unresolved
)
589 struct delta_entry
**sorted_by_pos
;
590 int i
, n
= 0, percent
= -1;
593 * Since many unresolved deltas may well be themselves base objects
594 * for more unresolved deltas, we really want to include the
595 * smallest number of base objects that would cover as much delta
596 * as possible by picking the
597 * trunc deltas first, allowing for other deltas to resolve without
598 * additional base objects. Since most base objects are to be found
599 * before deltas depending on them, a good heuristic is to start
600 * resolving deltas in the same order as their position in the pack.
602 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
603 for (i
= 0; i
< nr_deltas
; i
++) {
604 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
606 sorted_by_pos
[n
++] = &deltas
[i
];
608 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
610 for (i
= 0; i
< n
; i
++) {
611 struct delta_entry
*d
= sorted_by_pos
[i
];
614 enum object_type type
;
617 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
619 data
= read_sha1_file(d
->base
.sha1
, &type
, &size
);
623 find_delta_children(&d
->base
, &first
, &last
);
624 for (j
= first
; j
<= last
; j
++) {
625 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
626 if (child
->real_type
== OBJ_REF_DELTA
)
627 resolve_delta(child
, data
, size
, type
);
630 if (check_sha1_signature(d
->base
.sha1
, data
, size
, typename(type
)))
631 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
632 append_obj_to_pack(d
->base
.sha1
, data
, size
, type
);
635 percent
= display_progress(nr_resolved_deltas
,
643 static void readjust_pack_header_and_sha1(unsigned char *sha1
)
645 struct pack_header hdr
;
649 /* Rewrite pack header with updated object number */
650 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
651 die("cannot seek back: %s", strerror(errno
));
652 if (read_in_full(output_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
653 die("cannot read pack header back: %s", strerror(errno
));
654 hdr
.hdr_entries
= htonl(nr_objects
);
655 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
656 die("cannot seek back: %s", strerror(errno
));
657 write_or_die(output_fd
, &hdr
, sizeof(hdr
));
658 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
659 die("cannot seek back: %s", strerror(errno
));
661 /* Recompute and store the new pack's SHA1 */
664 unsigned char *buf
[4096];
665 size
= xread(output_fd
, buf
, sizeof(buf
));
667 die("cannot read pack data back: %s", strerror(errno
));
668 SHA1_Update(&ctx
, buf
, size
);
670 SHA1_Final(sha1
, &ctx
);
671 write_or_die(output_fd
, sha1
, 20);
674 static int sha1_compare(const void *_a
, const void *_b
)
676 struct object_entry
*a
= *(struct object_entry
**)_a
;
677 struct object_entry
*b
= *(struct object_entry
**)_b
;
678 return hashcmp(a
->sha1
, b
->sha1
);
682 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
683 * the SHA1 hash of sorted object names.
685 static const char *write_index_file(const char *index_name
, unsigned char *sha1
)
688 struct object_entry
**sorted_by_sha
, **list
, **last
;
689 unsigned int array
[256];
695 xcalloc(nr_objects
, sizeof(struct object_entry
*));
696 list
= sorted_by_sha
;
697 last
= sorted_by_sha
+ nr_objects
;
698 for (i
= 0; i
< nr_objects
; ++i
)
699 sorted_by_sha
[i
] = &objects
[i
];
700 qsort(sorted_by_sha
, nr_objects
, sizeof(sorted_by_sha
[0]),
705 sorted_by_sha
= list
= last
= NULL
;
708 static char tmpfile
[PATH_MAX
];
709 snprintf(tmpfile
, sizeof(tmpfile
),
710 "%s/tmp_idx_XXXXXX", get_object_directory());
711 fd
= mkstemp(tmpfile
);
712 index_name
= xstrdup(tmpfile
);
715 fd
= open(index_name
, O_CREAT
|O_EXCL
|O_WRONLY
, 0600);
718 die("unable to create %s: %s", index_name
, strerror(errno
));
719 f
= sha1fd(fd
, index_name
);
722 * Write the first-level table (the list is sorted,
723 * but we use a 256-entry lookup to be able to avoid
724 * having to do eight extra binary search iterations).
726 for (i
= 0; i
< 256; i
++) {
727 struct object_entry
**next
= list
;
728 while (next
< last
) {
729 struct object_entry
*obj
= *next
;
730 if (obj
->sha1
[0] != i
)
734 array
[i
] = htonl(next
- sorted_by_sha
);
737 sha1write(f
, array
, 256 * sizeof(int));
739 /* recompute the SHA1 hash of sorted object names.
740 * currently pack-objects does not do this, but that
745 * Write the actual SHA1 entries..
747 list
= sorted_by_sha
;
748 for (i
= 0; i
< nr_objects
; i
++) {
749 struct object_entry
*obj
= *list
++;
750 unsigned int offset
= htonl(obj
->offset
);
751 sha1write(f
, &offset
, 4);
752 sha1write(f
, obj
->sha1
, 20);
753 SHA1_Update(&ctx
, obj
->sha1
, 20);
755 sha1write(f
, sha1
, 20);
756 sha1close(f
, NULL
, 1);
758 SHA1_Final(sha1
, &ctx
);
762 static void final(const char *final_pack_name
, const char *curr_pack_name
,
763 const char *final_index_name
, const char *curr_index_name
,
764 const char *keep_name
, const char *keep_msg
,
767 const char *report
= "pack";
774 err
= close(output_fd
);
776 die("error while closing pack file: %s", strerror(errno
));
777 chmod(curr_pack_name
, 0444);
781 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
783 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
784 get_object_directory(), sha1_to_hex(sha1
));
787 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
790 die("cannot write keep file");
792 if (keep_msg_len
> 0) {
793 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
794 write_or_die(keep_fd
, "\n", 1);
801 if (final_pack_name
!= curr_pack_name
) {
802 if (!final_pack_name
) {
803 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
804 get_object_directory(), sha1_to_hex(sha1
));
805 final_pack_name
= name
;
807 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
808 die("cannot store pack file");
811 chmod(curr_index_name
, 0444);
812 if (final_index_name
!= curr_index_name
) {
813 if (!final_index_name
) {
814 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
815 get_object_directory(), sha1_to_hex(sha1
));
816 final_index_name
= name
;
818 if (move_temp_to_file(curr_index_name
, final_index_name
))
819 die("cannot store index file");
823 printf("%s\n", sha1_to_hex(sha1
));
826 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
827 report
, sha1_to_hex(sha1
));
828 write_or_die(1, buf
, len
);
831 * Let's just mimic git-unpack-objects here and write
832 * the last part of the input buffer to stdout.
835 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
844 int main(int argc
, char **argv
)
846 int i
, fix_thin_pack
= 0;
847 const char *curr_pack
, *pack_name
= NULL
;
848 const char *curr_index
, *index_name
= NULL
;
849 const char *keep_name
= NULL
, *keep_msg
= NULL
;
850 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
851 unsigned char sha1
[20];
853 for (i
= 1; i
< argc
; i
++) {
854 const char *arg
= argv
[i
];
857 if (!strcmp(arg
, "--stdin")) {
859 } else if (!strcmp(arg
, "--fix-thin")) {
861 } else if (!strcmp(arg
, "--keep")) {
863 } else if (!prefixcmp(arg
, "--keep=")) {
865 } else if (!prefixcmp(arg
, "--pack_header=")) {
866 struct pack_header
*hdr
;
869 hdr
= (struct pack_header
*)input_buffer
;
870 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
871 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
874 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
877 input_len
= sizeof(*hdr
);
878 } else if (!strcmp(arg
, "-v")) {
880 } else if (!strcmp(arg
, "-o")) {
881 if (index_name
|| (i
+1) >= argc
)
882 usage(index_pack_usage
);
883 index_name
= argv
[++i
];
885 usage(index_pack_usage
);
890 usage(index_pack_usage
);
894 if (!pack_name
&& !from_stdin
)
895 usage(index_pack_usage
);
896 if (fix_thin_pack
&& !from_stdin
)
897 die("--fix-thin cannot be used without --stdin");
898 if (!index_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 index_name_buf
= xmalloc(len
);
904 memcpy(index_name_buf
, pack_name
, len
- 5);
905 strcpy(index_name_buf
+ len
- 5, ".idx");
906 index_name
= index_name_buf
;
908 if (keep_msg
&& !keep_name
&& pack_name
) {
909 int len
= strlen(pack_name
);
910 if (!has_extension(pack_name
, ".pack"))
911 die("packfile name '%s' does not end with '.pack'",
913 keep_name_buf
= xmalloc(len
);
914 memcpy(keep_name_buf
, pack_name
, len
- 5);
915 strcpy(keep_name_buf
+ len
- 5, ".keep");
916 keep_name
= keep_name_buf
;
919 curr_pack
= open_pack_file(pack_name
);
921 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
922 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
924 setup_progress_signal();
925 parse_pack_objects(sha1
);
926 if (nr_deltas
!= nr_resolved_deltas
) {
928 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
929 int nr_objects_initial
= nr_objects
;
930 if (nr_unresolved
<= 0)
931 die("confusion beyond insanity");
932 objects
= xrealloc(objects
,
933 (nr_objects
+ nr_unresolved
+ 1)
935 fix_unresolved_deltas(nr_unresolved
);
937 fprintf(stderr
, "%d objects were added to complete this thin pack.\n",
938 nr_objects
- nr_objects_initial
);
939 readjust_pack_header_and_sha1(sha1
);
941 if (nr_deltas
!= nr_resolved_deltas
)
942 die("pack has %d unresolved deltas",
943 nr_deltas
- nr_resolved_deltas
);
945 /* Flush remaining pack final 20-byte SHA1. */
949 curr_index
= write_index_file(index_name
, sha1
);
950 final(pack_name
, curr_pack
,
951 index_name
, curr_index
,
955 free(index_name_buf
);