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
;
18 enum object_type type
;
19 enum object_type real_type
;
20 unsigned char sha1
[20];
24 unsigned char sha1
[20];
29 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
30 * to memcmp() only the first 20 bytes.
32 #define UNION_BASE_SZ 20
36 union delta_base base
;
40 static struct object_entry
*objects
;
41 static struct delta_entry
*deltas
;
42 static int nr_objects
;
44 static int nr_resolved_deltas
;
46 static int from_stdin
;
49 static volatile sig_atomic_t progress_update
;
51 static void progress_interval(int signum
)
56 static void setup_progress_signal(void)
61 memset(&sa
, 0, sizeof(sa
));
62 sa
.sa_handler
= progress_interval
;
63 sigemptyset(&sa
.sa_mask
);
64 sa
.sa_flags
= SA_RESTART
;
65 sigaction(SIGALRM
, &sa
, NULL
);
67 v
.it_interval
.tv_sec
= 1;
68 v
.it_interval
.tv_usec
= 0;
69 v
.it_value
= v
.it_interval
;
70 setitimer(ITIMER_REAL
, &v
, NULL
);
74 static unsigned display_progress(unsigned n
, unsigned total
, unsigned last_pc
)
76 unsigned percent
= n
* 100 / total
;
77 if (percent
!= last_pc
|| progress_update
) {
78 fprintf(stderr
, "%4u%% (%u/%u) done\r", percent
, n
, total
);
84 /* We always read in 4kB chunks. */
85 static unsigned char input_buffer
[4096];
86 static unsigned long input_offset
, input_len
, consumed_bytes
;
87 static SHA_CTX input_ctx
;
88 static int input_fd
, output_fd
, pack_fd
;
90 /* Discard current buffer used content. */
91 static void flush(void)
95 write_or_die(output_fd
, input_buffer
, input_offset
);
96 SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
97 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
103 * Make sure at least "min" bytes are available in the buffer, and
104 * return the pointer to the buffer.
106 static void *fill(int min
)
108 if (min
<= input_len
)
109 return input_buffer
+ input_offset
;
110 if (min
> sizeof(input_buffer
))
111 die("cannot fill %d bytes", min
);
114 int ret
= xread(input_fd
, input_buffer
+ input_len
,
115 sizeof(input_buffer
) - input_len
);
119 die("read error on input: %s", strerror(errno
));
122 } while (input_len
< min
);
126 static void use(int bytes
)
128 if (bytes
> input_len
)
129 die("used more bytes than were available");
131 input_offset
+= bytes
;
132 consumed_bytes
+= bytes
;
135 static const char *open_pack_file(const char *pack_name
)
140 static char tmpfile
[PATH_MAX
];
141 snprintf(tmpfile
, sizeof(tmpfile
),
142 "%s/pack_XXXXXX", get_object_directory());
143 output_fd
= mkstemp(tmpfile
);
144 pack_name
= xstrdup(tmpfile
);
146 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
148 die("unable to create %s: %s\n", pack_name
, strerror(errno
));
151 input_fd
= open(pack_name
, O_RDONLY
);
153 die("cannot open packfile '%s': %s",
154 pack_name
, strerror(errno
));
158 SHA1_Init(&input_ctx
);
162 static void parse_pack_header(void)
164 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
166 /* Header consistency check */
167 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
168 die("pack signature mismatch");
169 if (!pack_version_ok(hdr
->hdr_version
))
170 die("pack version %d unsupported", ntohl(hdr
->hdr_version
));
172 nr_objects
= ntohl(hdr
->hdr_entries
);
173 use(sizeof(struct pack_header
));
176 static void bad_object(unsigned long offset
, const char *format
,
177 ...) NORETURN
__attribute__((format (printf
, 2, 3)));
179 static void bad_object(unsigned long offset
, const char *format
, ...)
184 va_start(params
, format
);
185 vsnprintf(buf
, sizeof(buf
), format
, params
);
187 die("pack has bad object at offset %lu: %s", offset
, buf
);
190 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
193 void *buf
= xmalloc(size
);
195 memset(&stream
, 0, sizeof(stream
));
196 stream
.next_out
= buf
;
197 stream
.avail_out
= size
;
198 stream
.next_in
= fill(1);
199 stream
.avail_in
= input_len
;
200 inflateInit(&stream
);
203 int ret
= inflate(&stream
, 0);
204 use(input_len
- stream
.avail_in
);
205 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
208 bad_object(offset
, "inflate returned %d", ret
);
209 stream
.next_in
= fill(1);
210 stream
.avail_in
= input_len
;
216 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
219 unsigned long size
, base_offset
;
222 obj
->offset
= consumed_bytes
;
227 obj
->type
= (c
>> 4) & 7;
234 size
+= (c
& 0x7fUL
) << shift
;
241 hashcpy(delta_base
->sha1
, fill(20));
245 memset(delta_base
, 0, sizeof(*delta_base
));
249 base_offset
= c
& 127;
252 if (!base_offset
|| base_offset
& ~(~0UL >> 7))
253 bad_object(obj
->offset
, "offset value overflow for delta base object");
257 base_offset
= (base_offset
<< 7) + (c
& 127);
259 delta_base
->offset
= obj
->offset
- base_offset
;
260 if (delta_base
->offset
>= obj
->offset
)
261 bad_object(obj
->offset
, "delta base offset is out of bound");
269 bad_object(obj
->offset
, "unknown object type %d", obj
->type
);
271 obj
->hdr_size
= consumed_bytes
- obj
->offset
;
273 return unpack_entry_data(obj
->offset
, obj
->size
);
276 static void *get_data_from_pack(struct object_entry
*obj
)
278 unsigned long from
= obj
[0].offset
+ obj
[0].hdr_size
;
279 unsigned long len
= obj
[1].offset
- from
;
280 unsigned char *src
, *data
;
285 if (pread(pack_fd
, src
, len
, from
) != len
)
286 die("cannot pread pack file: %s", strerror(errno
));
287 data
= xmalloc(obj
->size
);
288 memset(&stream
, 0, sizeof(stream
));
289 stream
.next_out
= data
;
290 stream
.avail_out
= obj
->size
;
291 stream
.next_in
= src
;
292 stream
.avail_in
= len
;
293 inflateInit(&stream
);
294 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
296 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
297 die("serious inflate inconsistency");
302 static int find_delta(const union delta_base
*base
)
304 int first
= 0, last
= nr_deltas
;
306 while (first
< last
) {
307 int next
= (first
+ last
) / 2;
308 struct delta_entry
*delta
= &deltas
[next
];
311 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
323 static int find_delta_children(const union delta_base
*base
,
324 int *first_index
, int *last_index
)
326 int first
= find_delta(base
);
328 int end
= nr_deltas
- 1;
332 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
334 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
336 *first_index
= first
;
341 static void sha1_object(const void *data
, unsigned long size
,
342 enum object_type type
, unsigned char *sha1
)
347 const char *type_str
;
350 case OBJ_COMMIT
: type_str
= commit_type
; break;
351 case OBJ_TREE
: type_str
= tree_type
; break;
352 case OBJ_BLOB
: type_str
= blob_type
; break;
353 case OBJ_TAG
: type_str
= tag_type
; break;
355 die("bad type %d", type
);
358 header_size
= sprintf(header
, "%s %lu", type_str
, size
) + 1;
361 SHA1_Update(&ctx
, header
, header_size
);
362 SHA1_Update(&ctx
, data
, size
);
363 SHA1_Final(sha1
, &ctx
);
366 static void resolve_delta(struct object_entry
*delta_obj
, void *base_data
,
367 unsigned long base_size
, enum object_type type
)
370 unsigned long delta_size
;
372 unsigned long result_size
;
373 union delta_base delta_base
;
376 delta_obj
->real_type
= type
;
377 delta_data
= get_data_from_pack(delta_obj
);
378 delta_size
= delta_obj
->size
;
379 result
= patch_delta(base_data
, base_size
, delta_data
, delta_size
,
383 bad_object(delta_obj
->offset
, "failed to apply delta");
384 sha1_object(result
, result_size
, type
, delta_obj
->sha1
);
385 nr_resolved_deltas
++;
387 hashcpy(delta_base
.sha1
, delta_obj
->sha1
);
388 if (!find_delta_children(&delta_base
, &first
, &last
)) {
389 for (j
= first
; j
<= last
; j
++) {
390 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
391 if (child
->real_type
== OBJ_REF_DELTA
)
392 resolve_delta(child
, result
, result_size
, type
);
396 memset(&delta_base
, 0, sizeof(delta_base
));
397 delta_base
.offset
= delta_obj
->offset
;
398 if (!find_delta_children(&delta_base
, &first
, &last
)) {
399 for (j
= first
; j
<= last
; j
++) {
400 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
401 if (child
->real_type
== OBJ_OFS_DELTA
)
402 resolve_delta(child
, result
, result_size
, type
);
409 static int compare_delta_entry(const void *a
, const void *b
)
411 const struct delta_entry
*delta_a
= a
;
412 const struct delta_entry
*delta_b
= b
;
413 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
416 /* Parse all objects and return the pack content SHA1 hash */
417 static void parse_pack_objects(unsigned char *sha1
)
420 struct delta_entry
*delta
= deltas
;
426 * - find locations of all objects;
427 * - calculate SHA1 of all non-delta objects;
428 * - remember base (SHA1 or offset) for all deltas.
431 fprintf(stderr
, "Indexing %d objects.\n", nr_objects
);
432 for (i
= 0; i
< nr_objects
; i
++) {
433 struct object_entry
*obj
= &objects
[i
];
434 data
= unpack_raw_entry(obj
, &delta
->base
);
435 obj
->real_type
= obj
->type
;
436 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
441 sha1_object(data
, obj
->size
, obj
->type
, obj
->sha1
);
444 percent
= display_progress(i
+1, nr_objects
, percent
);
446 objects
[i
].offset
= consumed_bytes
;
450 /* Check pack integrity */
452 SHA1_Final(sha1
, &input_ctx
);
453 if (hashcmp(fill(20), sha1
))
454 die("pack is corrupted (SHA1 mismatch)");
457 /* If input_fd is a file, we should have reached its end now. */
458 if (fstat(input_fd
, &st
))
459 die("cannot fstat packfile: %s", strerror(errno
));
460 if (S_ISREG(st
.st_mode
) && st
.st_size
!= consumed_bytes
)
461 die("pack has junk at the end");
466 /* Sort deltas by base SHA1/offset for fast searching */
467 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
468 compare_delta_entry
);
472 * - for all non-delta objects, look if it is used as a base for
474 * - if used as a base, uncompress the object and apply all deltas,
475 * recursively checking if the resulting object is used as a base
476 * for some more deltas.
479 fprintf(stderr
, "Resolving %d deltas.\n", nr_deltas
);
480 for (i
= 0; i
< nr_objects
; i
++) {
481 struct object_entry
*obj
= &objects
[i
];
482 union delta_base base
;
483 int j
, ref
, ref_first
, ref_last
, ofs
, ofs_first
, ofs_last
;
485 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
487 hashcpy(base
.sha1
, obj
->sha1
);
488 ref
= !find_delta_children(&base
, &ref_first
, &ref_last
);
489 memset(&base
, 0, sizeof(base
));
490 base
.offset
= obj
->offset
;
491 ofs
= !find_delta_children(&base
, &ofs_first
, &ofs_last
);
494 data
= get_data_from_pack(obj
);
496 for (j
= ref_first
; j
<= ref_last
; j
++) {
497 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
498 if (child
->real_type
== OBJ_REF_DELTA
)
499 resolve_delta(child
, data
,
500 obj
->size
, obj
->type
);
503 for (j
= ofs_first
; j
<= ofs_last
; j
++) {
504 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
505 if (child
->real_type
== OBJ_OFS_DELTA
)
506 resolve_delta(child
, data
,
507 obj
->size
, obj
->type
);
511 percent
= display_progress(nr_resolved_deltas
,
514 if (verbose
&& nr_resolved_deltas
== nr_deltas
)
518 static int write_compressed(int fd
, void *in
, unsigned int size
)
521 unsigned long maxsize
;
524 memset(&stream
, 0, sizeof(stream
));
525 deflateInit(&stream
, zlib_compression_level
);
526 maxsize
= deflateBound(&stream
, size
);
527 out
= xmalloc(maxsize
);
531 stream
.avail_in
= size
;
532 stream
.next_out
= out
;
533 stream
.avail_out
= maxsize
;
534 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
537 size
= stream
.total_out
;
538 write_or_die(fd
, out
, size
);
543 static void append_obj_to_pack(void *buf
,
544 unsigned long size
, enum object_type type
)
546 struct object_entry
*obj
= &objects
[nr_objects
++];
547 unsigned char header
[10];
548 unsigned long s
= size
;
550 unsigned char c
= (type
<< 4) | (s
& 15);
553 header
[n
++] = c
| 0x80;
558 write_or_die(output_fd
, header
, n
);
559 obj
[1].offset
= obj
[0].offset
+ n
;
560 obj
[1].offset
+= write_compressed(output_fd
, buf
, size
);
561 sha1_object(buf
, size
, type
, obj
->sha1
);
564 static int delta_pos_compare(const void *_a
, const void *_b
)
566 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
567 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
568 return a
->obj_no
- b
->obj_no
;
571 static void fix_unresolved_deltas(int nr_unresolved
)
573 struct delta_entry
**sorted_by_pos
;
574 int i
, n
= 0, percent
= -1;
577 * Since many unresolved deltas may well be themselves base objects
578 * for more unresolved deltas, we really want to include the
579 * smallest number of base objects that would cover as much delta
580 * as possible by picking the
581 * trunc deltas first, allowing for other deltas to resolve without
582 * additional base objects. Since most base objects are to be found
583 * before deltas depending on them, a good heuristic is to start
584 * resolving deltas in the same order as their position in the pack.
586 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
587 for (i
= 0; i
< nr_deltas
; i
++) {
588 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
590 sorted_by_pos
[n
++] = &deltas
[i
];
592 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
594 for (i
= 0; i
< n
; i
++) {
595 struct delta_entry
*d
= sorted_by_pos
[i
];
599 enum object_type obj_type
;
602 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
604 data
= read_sha1_file(d
->base
.sha1
, type
, &size
);
607 if (!strcmp(type
, blob_type
)) obj_type
= OBJ_BLOB
;
608 else if (!strcmp(type
, tree_type
)) obj_type
= OBJ_TREE
;
609 else if (!strcmp(type
, commit_type
)) obj_type
= OBJ_COMMIT
;
610 else if (!strcmp(type
, tag_type
)) obj_type
= OBJ_TAG
;
611 else die("base object %s is of type '%s'",
612 sha1_to_hex(d
->base
.sha1
), type
);
614 find_delta_children(&d
->base
, &first
, &last
);
615 for (j
= first
; j
<= last
; j
++) {
616 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
617 if (child
->real_type
== OBJ_REF_DELTA
)
618 resolve_delta(child
, data
, size
, obj_type
);
621 append_obj_to_pack(data
, size
, obj_type
);
624 percent
= display_progress(nr_resolved_deltas
,
632 static void readjust_pack_header_and_sha1(unsigned char *sha1
)
634 struct pack_header hdr
;
638 /* Rewrite pack header with updated object number */
639 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
640 die("cannot seek back: %s", strerror(errno
));
641 if (read_in_full(output_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
642 die("cannot read pack header back: %s", strerror(errno
));
643 hdr
.hdr_entries
= htonl(nr_objects
);
644 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
645 die("cannot seek back: %s", strerror(errno
));
646 write_or_die(output_fd
, &hdr
, sizeof(hdr
));
647 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
648 die("cannot seek back: %s", strerror(errno
));
650 /* Recompute and store the new pack's SHA1 */
653 unsigned char *buf
[4096];
654 size
= xread(output_fd
, buf
, sizeof(buf
));
656 die("cannot read pack data back: %s", strerror(errno
));
657 SHA1_Update(&ctx
, buf
, size
);
659 SHA1_Final(sha1
, &ctx
);
660 write_or_die(output_fd
, sha1
, 20);
663 static int sha1_compare(const void *_a
, const void *_b
)
665 struct object_entry
*a
= *(struct object_entry
**)_a
;
666 struct object_entry
*b
= *(struct object_entry
**)_b
;
667 return hashcmp(a
->sha1
, b
->sha1
);
671 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
672 * the SHA1 hash of sorted object names.
674 static const char *write_index_file(const char *index_name
, unsigned char *sha1
)
677 struct object_entry
**sorted_by_sha
, **list
, **last
;
678 unsigned int array
[256];
684 xcalloc(nr_objects
, sizeof(struct object_entry
*));
685 list
= sorted_by_sha
;
686 last
= sorted_by_sha
+ nr_objects
;
687 for (i
= 0; i
< nr_objects
; ++i
)
688 sorted_by_sha
[i
] = &objects
[i
];
689 qsort(sorted_by_sha
, nr_objects
, sizeof(sorted_by_sha
[0]),
694 sorted_by_sha
= list
= last
= NULL
;
697 static char tmpfile
[PATH_MAX
];
698 snprintf(tmpfile
, sizeof(tmpfile
),
699 "%s/index_XXXXXX", get_object_directory());
700 fd
= mkstemp(tmpfile
);
701 index_name
= xstrdup(tmpfile
);
704 fd
= open(index_name
, O_CREAT
|O_EXCL
|O_WRONLY
, 0600);
707 die("unable to create %s: %s", index_name
, strerror(errno
));
708 f
= sha1fd(fd
, index_name
);
711 * Write the first-level table (the list is sorted,
712 * but we use a 256-entry lookup to be able to avoid
713 * having to do eight extra binary search iterations).
715 for (i
= 0; i
< 256; i
++) {
716 struct object_entry
**next
= list
;
717 while (next
< last
) {
718 struct object_entry
*obj
= *next
;
719 if (obj
->sha1
[0] != i
)
723 array
[i
] = htonl(next
- sorted_by_sha
);
726 sha1write(f
, array
, 256 * sizeof(int));
728 /* recompute the SHA1 hash of sorted object names.
729 * currently pack-objects does not do this, but that
734 * Write the actual SHA1 entries..
736 list
= sorted_by_sha
;
737 for (i
= 0; i
< nr_objects
; i
++) {
738 struct object_entry
*obj
= *list
++;
739 unsigned int offset
= htonl(obj
->offset
);
740 sha1write(f
, &offset
, 4);
741 sha1write(f
, obj
->sha1
, 20);
742 SHA1_Update(&ctx
, obj
->sha1
, 20);
744 sha1write(f
, sha1
, 20);
745 sha1close(f
, NULL
, 1);
747 SHA1_Final(sha1
, &ctx
);
751 static void final(const char *final_pack_name
, const char *curr_pack_name
,
752 const char *final_index_name
, const char *curr_index_name
,
753 const char *keep_name
, const char *keep_msg
,
756 char *report
= "pack";
763 err
= close(output_fd
);
765 die("error while closing pack file: %s", strerror(errno
));
766 chmod(curr_pack_name
, 0444);
770 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
772 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
773 get_object_directory(), sha1_to_hex(sha1
));
776 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
779 die("cannot write keep file");
781 if (keep_msg_len
> 0) {
782 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
783 write_or_die(keep_fd
, "\n", 1);
790 if (final_pack_name
!= curr_pack_name
) {
791 if (!final_pack_name
) {
792 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
793 get_object_directory(), sha1_to_hex(sha1
));
794 final_pack_name
= name
;
796 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
797 die("cannot store pack file");
800 chmod(curr_index_name
, 0444);
801 if (final_index_name
!= curr_index_name
) {
802 if (!final_index_name
) {
803 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
804 get_object_directory(), sha1_to_hex(sha1
));
805 final_index_name
= name
;
807 if (move_temp_to_file(curr_index_name
, final_index_name
))
808 die("cannot store index file");
812 printf("%s\n", sha1_to_hex(sha1
));
815 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
816 report
, sha1_to_hex(sha1
));
817 write_in_full(1, buf
, len
);
820 * Let's just mimic git-unpack-objects here and write
821 * the last part of the input buffer to stdout.
824 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
833 int main(int argc
, char **argv
)
835 int i
, fix_thin_pack
= 0;
836 const char *curr_pack
, *pack_name
= NULL
;
837 const char *curr_index
, *index_name
= NULL
;
838 const char *keep_name
= NULL
, *keep_msg
= NULL
;
839 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
840 unsigned char sha1
[20];
842 for (i
= 1; i
< argc
; i
++) {
843 const char *arg
= argv
[i
];
846 if (!strcmp(arg
, "--stdin")) {
848 } else if (!strcmp(arg
, "--fix-thin")) {
850 } else if (!strcmp(arg
, "--keep")) {
852 } else if (!strncmp(arg
, "--keep=", 7)) {
854 } else if (!strncmp(arg
, "--pack_header=", 14)) {
855 struct pack_header
*hdr
;
858 hdr
= (struct pack_header
*)input_buffer
;
859 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
860 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
863 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
866 input_len
= sizeof(*hdr
);
867 } else if (!strcmp(arg
, "-v")) {
869 } else if (!strcmp(arg
, "-o")) {
870 if (index_name
|| (i
+1) >= argc
)
871 usage(index_pack_usage
);
872 index_name
= argv
[++i
];
874 usage(index_pack_usage
);
879 usage(index_pack_usage
);
883 if (!pack_name
&& !from_stdin
)
884 usage(index_pack_usage
);
885 if (fix_thin_pack
&& !from_stdin
)
886 die("--fix-thin cannot be used without --stdin");
887 if (!index_name
&& pack_name
) {
888 int len
= strlen(pack_name
);
889 if (!has_extension(pack_name
, ".pack"))
890 die("packfile name '%s' does not end with '.pack'",
892 index_name_buf
= xmalloc(len
);
893 memcpy(index_name_buf
, pack_name
, len
- 5);
894 strcpy(index_name_buf
+ len
- 5, ".idx");
895 index_name
= index_name_buf
;
897 if (keep_msg
&& !keep_name
&& pack_name
) {
898 int len
= strlen(pack_name
);
899 if (!has_extension(pack_name
, ".pack"))
900 die("packfile name '%s' does not end with '.pack'",
902 keep_name_buf
= xmalloc(len
);
903 memcpy(keep_name_buf
, pack_name
, len
- 5);
904 strcpy(keep_name_buf
+ len
- 5, ".keep");
905 keep_name
= keep_name_buf
;
908 curr_pack
= open_pack_file(pack_name
);
910 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
911 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
913 setup_progress_signal();
914 parse_pack_objects(sha1
);
915 if (nr_deltas
!= nr_resolved_deltas
) {
917 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
918 int nr_objects_initial
= nr_objects
;
919 if (nr_unresolved
<= 0)
920 die("confusion beyond insanity");
921 objects
= xrealloc(objects
,
922 (nr_objects
+ nr_unresolved
+ 1)
924 fix_unresolved_deltas(nr_unresolved
);
926 fprintf(stderr
, "%d objects were added to complete this thin pack.\n",
927 nr_objects
- nr_objects_initial
);
928 readjust_pack_header_and_sha1(sha1
);
930 if (nr_deltas
!= nr_resolved_deltas
)
931 die("pack has %d unresolved deltas",
932 nr_deltas
- nr_resolved_deltas
);
934 /* Flush remaining pack final 20-byte SHA1. */
938 curr_index
= write_index_file(index_name
, sha1
);
939 final(pack_name
, curr_pack
,
940 index_name
, curr_index
,
944 free(index_name_buf
);