12 static const char index_pack_usage
[] =
13 "git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
19 unsigned int hdr_size
;
20 enum object_type type
;
21 enum object_type real_type
;
22 unsigned char sha1
[20];
26 unsigned char sha1
[20];
31 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
32 * to memcmp() only the first 20 bytes.
34 #define UNION_BASE_SZ 20
38 union delta_base base
;
42 static struct object_entry
*objects
;
43 static struct delta_entry
*deltas
;
44 static int nr_objects
;
46 static int nr_resolved_deltas
;
48 static int from_stdin
;
51 static volatile sig_atomic_t progress_update
;
53 static void progress_interval(int signum
)
58 static void setup_progress_signal(void)
63 memset(&sa
, 0, sizeof(sa
));
64 sa
.sa_handler
= progress_interval
;
65 sigemptyset(&sa
.sa_mask
);
66 sa
.sa_flags
= SA_RESTART
;
67 sigaction(SIGALRM
, &sa
, NULL
);
69 v
.it_interval
.tv_sec
= 1;
70 v
.it_interval
.tv_usec
= 0;
71 v
.it_value
= v
.it_interval
;
72 setitimer(ITIMER_REAL
, &v
, NULL
);
76 static unsigned display_progress(unsigned n
, unsigned total
, unsigned last_pc
)
78 unsigned percent
= n
* 100 / total
;
79 if (percent
!= last_pc
|| progress_update
) {
80 fprintf(stderr
, "%4u%% (%u/%u) done\r", percent
, n
, total
);
86 /* We always read in 4kB chunks. */
87 static unsigned char input_buffer
[4096];
88 static unsigned long input_offset
, input_len
, consumed_bytes
;
89 static SHA_CTX input_ctx
;
90 static int input_fd
, output_fd
, mmap_fd
;
92 /* Discard current buffer used content. */
93 static void flush(void)
97 write_or_die(output_fd
, input_buffer
, input_offset
);
98 SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
99 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
105 * Make sure at least "min" bytes are available in the buffer, and
106 * return the pointer to the buffer.
108 static void *fill(int min
)
110 if (min
<= input_len
)
111 return input_buffer
+ input_offset
;
112 if (min
> sizeof(input_buffer
))
113 die("cannot fill %d bytes", min
);
116 int ret
= xread(input_fd
, input_buffer
+ input_len
,
117 sizeof(input_buffer
) - input_len
);
121 die("read error on input: %s", strerror(errno
));
124 } while (input_len
< min
);
128 static void use(int bytes
)
130 if (bytes
> input_len
)
131 die("used more bytes than were available");
133 input_offset
+= bytes
;
134 consumed_bytes
+= bytes
;
137 static const char *open_pack_file(const char *pack_name
)
142 static char tmpfile
[PATH_MAX
];
143 snprintf(tmpfile
, sizeof(tmpfile
),
144 "%s/pack_XXXXXX", get_object_directory());
145 output_fd
= mkstemp(tmpfile
);
146 pack_name
= xstrdup(tmpfile
);
148 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
150 die("unable to create %s: %s\n", pack_name
, strerror(errno
));
153 input_fd
= open(pack_name
, O_RDONLY
);
155 die("cannot open packfile '%s': %s",
156 pack_name
, strerror(errno
));
160 SHA1_Init(&input_ctx
);
164 static void parse_pack_header(void)
166 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
168 /* Header consistency check */
169 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
170 die("pack signature mismatch");
171 if (!pack_version_ok(hdr
->hdr_version
))
172 die("pack version %d unsupported", ntohl(hdr
->hdr_version
));
174 nr_objects
= ntohl(hdr
->hdr_entries
);
175 use(sizeof(struct pack_header
));
178 static void bad_object(unsigned long offset
, const char *format
,
179 ...) NORETURN
__attribute__((format (printf
, 2, 3)));
181 static void bad_object(unsigned long offset
, const char *format
, ...)
186 va_start(params
, format
);
187 vsnprintf(buf
, sizeof(buf
), format
, params
);
189 die("pack has bad object at offset %lu: %s", offset
, buf
);
192 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
195 void *buf
= xmalloc(size
);
197 memset(&stream
, 0, sizeof(stream
));
198 stream
.next_out
= buf
;
199 stream
.avail_out
= size
;
200 stream
.next_in
= fill(1);
201 stream
.avail_in
= input_len
;
202 inflateInit(&stream
);
205 int ret
= inflate(&stream
, 0);
206 use(input_len
- stream
.avail_in
);
207 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
210 bad_object(offset
, "inflate returned %d", ret
);
211 stream
.next_in
= fill(1);
212 stream
.avail_in
= input_len
;
218 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
221 unsigned long size
, base_offset
;
224 obj
->offset
= consumed_bytes
;
229 obj
->type
= (c
>> 4) & 7;
236 size
+= (c
& 0x7fUL
) << shift
;
243 hashcpy(delta_base
->sha1
, fill(20));
247 memset(delta_base
, 0, sizeof(*delta_base
));
251 base_offset
= c
& 127;
254 if (!base_offset
|| base_offset
& ~(~0UL >> 7))
255 bad_object(obj
->offset
, "offset value overflow for delta base object");
259 base_offset
= (base_offset
<< 7) + (c
& 127);
261 delta_base
->offset
= obj
->offset
- base_offset
;
262 if (delta_base
->offset
>= obj
->offset
)
263 bad_object(obj
->offset
, "delta base offset is out of bound");
271 bad_object(obj
->offset
, "bad object type %d", obj
->type
);
273 obj
->hdr_size
= consumed_bytes
- obj
->offset
;
275 return unpack_entry_data(obj
->offset
, obj
->size
);
278 static void *get_data_from_pack(struct object_entry
*obj
)
280 unsigned long from
= obj
[0].offset
+ obj
[0].hdr_size
;
281 unsigned long len
= obj
[1].offset
- from
;
282 unsigned pg_offset
= from
% getpagesize();
283 unsigned char *map
, *data
;
287 map
= mmap(NULL
, len
+ pg_offset
, PROT_READ
, MAP_PRIVATE
,
288 mmap_fd
, from
- pg_offset
);
289 if (map
== MAP_FAILED
)
290 die("cannot mmap pack file: %s", strerror(errno
));
291 data
= xmalloc(obj
->size
);
292 memset(&stream
, 0, sizeof(stream
));
293 stream
.next_out
= data
;
294 stream
.avail_out
= obj
->size
;
295 stream
.next_in
= map
+ pg_offset
;
296 stream
.avail_in
= len
;
297 inflateInit(&stream
);
298 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
300 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
301 die("serious inflate inconsistency");
302 munmap(map
, len
+ pg_offset
);
306 static int find_delta(const union delta_base
*base
)
308 int first
= 0, last
= nr_deltas
;
310 while (first
< last
) {
311 int next
= (first
+ last
) / 2;
312 struct delta_entry
*delta
= &deltas
[next
];
315 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
327 static int find_delta_children(const union delta_base
*base
,
328 int *first_index
, int *last_index
)
330 int first
= find_delta(base
);
332 int end
= nr_deltas
- 1;
336 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
338 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
340 *first_index
= first
;
345 static void sha1_object(const void *data
, unsigned long size
,
346 enum object_type type
, unsigned char *sha1
)
351 const char *type_str
;
354 case OBJ_COMMIT
: type_str
= commit_type
; break;
355 case OBJ_TREE
: type_str
= tree_type
; break;
356 case OBJ_BLOB
: type_str
= blob_type
; break;
357 case OBJ_TAG
: type_str
= tag_type
; break;
359 die("bad type %d", type
);
362 header_size
= sprintf(header
, "%s %lu", type_str
, size
) + 1;
365 SHA1_Update(&ctx
, header
, header_size
);
366 SHA1_Update(&ctx
, data
, size
);
367 SHA1_Final(sha1
, &ctx
);
370 static void resolve_delta(struct object_entry
*delta_obj
, void *base_data
,
371 unsigned long base_size
, enum object_type type
)
374 unsigned long delta_size
;
376 unsigned long result_size
;
377 union delta_base delta_base
;
380 delta_obj
->real_type
= type
;
381 delta_data
= get_data_from_pack(delta_obj
);
382 delta_size
= delta_obj
->size
;
383 result
= patch_delta(base_data
, base_size
, delta_data
, delta_size
,
387 bad_object(delta_obj
->offset
, "failed to apply delta");
388 sha1_object(result
, result_size
, type
, delta_obj
->sha1
);
389 nr_resolved_deltas
++;
391 hashcpy(delta_base
.sha1
, delta_obj
->sha1
);
392 if (!find_delta_children(&delta_base
, &first
, &last
)) {
393 for (j
= first
; j
<= last
; j
++) {
394 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
395 if (child
->real_type
== OBJ_REF_DELTA
)
396 resolve_delta(child
, result
, result_size
, type
);
400 memset(&delta_base
, 0, sizeof(delta_base
));
401 delta_base
.offset
= delta_obj
->offset
;
402 if (!find_delta_children(&delta_base
, &first
, &last
)) {
403 for (j
= first
; j
<= last
; j
++) {
404 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
405 if (child
->real_type
== OBJ_OFS_DELTA
)
406 resolve_delta(child
, result
, result_size
, type
);
413 static int compare_delta_entry(const void *a
, const void *b
)
415 const struct delta_entry
*delta_a
= a
;
416 const struct delta_entry
*delta_b
= b
;
417 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
420 /* Parse all objects and return the pack content SHA1 hash */
421 static void parse_pack_objects(unsigned char *sha1
)
424 struct delta_entry
*delta
= deltas
;
430 * - find locations of all objects;
431 * - calculate SHA1 of all non-delta objects;
432 * - remember base (SHA1 or offset) for all deltas.
435 fprintf(stderr
, "Indexing %d objects.\n", nr_objects
);
436 for (i
= 0; i
< nr_objects
; i
++) {
437 struct object_entry
*obj
= &objects
[i
];
438 data
= unpack_raw_entry(obj
, &delta
->base
);
439 obj
->real_type
= obj
->type
;
440 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
445 sha1_object(data
, obj
->size
, obj
->type
, obj
->sha1
);
448 percent
= display_progress(i
+1, nr_objects
, percent
);
450 objects
[i
].offset
= consumed_bytes
;
454 /* Check pack integrity */
456 SHA1_Final(sha1
, &input_ctx
);
457 if (hashcmp(fill(20), sha1
))
458 die("pack is corrupted (SHA1 mismatch)");
461 /* If input_fd is a file, we should have reached its end now. */
462 if (fstat(input_fd
, &st
))
463 die("cannot fstat packfile: %s", strerror(errno
));
464 if (S_ISREG(st
.st_mode
) && st
.st_size
!= consumed_bytes
)
465 die("pack has junk at the end");
470 /* Sort deltas by base SHA1/offset for fast searching */
471 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
472 compare_delta_entry
);
476 * - for all non-delta objects, look if it is used as a base for
478 * - if used as a base, uncompress the object and apply all deltas,
479 * recursively checking if the resulting object is used as a base
480 * for some more deltas.
483 fprintf(stderr
, "Resolving %d deltas.\n", nr_deltas
);
484 for (i
= 0; i
< nr_objects
; i
++) {
485 struct object_entry
*obj
= &objects
[i
];
486 union delta_base base
;
487 int j
, ref
, ref_first
, ref_last
, ofs
, ofs_first
, ofs_last
;
489 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
491 hashcpy(base
.sha1
, obj
->sha1
);
492 ref
= !find_delta_children(&base
, &ref_first
, &ref_last
);
493 memset(&base
, 0, sizeof(base
));
494 base
.offset
= obj
->offset
;
495 ofs
= !find_delta_children(&base
, &ofs_first
, &ofs_last
);
498 data
= get_data_from_pack(obj
);
500 for (j
= ref_first
; j
<= ref_last
; j
++) {
501 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
502 if (child
->real_type
== OBJ_REF_DELTA
)
503 resolve_delta(child
, data
,
504 obj
->size
, obj
->type
);
507 for (j
= ofs_first
; j
<= ofs_last
; j
++) {
508 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
509 if (child
->real_type
== OBJ_OFS_DELTA
)
510 resolve_delta(child
, data
,
511 obj
->size
, obj
->type
);
515 percent
= display_progress(nr_resolved_deltas
,
518 if (verbose
&& nr_resolved_deltas
== nr_deltas
)
522 static int write_compressed(int fd
, void *in
, unsigned int size
)
525 unsigned long maxsize
;
528 memset(&stream
, 0, sizeof(stream
));
529 deflateInit(&stream
, zlib_compression_level
);
530 maxsize
= deflateBound(&stream
, size
);
531 out
= xmalloc(maxsize
);
535 stream
.avail_in
= size
;
536 stream
.next_out
= out
;
537 stream
.avail_out
= maxsize
;
538 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
541 size
= stream
.total_out
;
542 write_or_die(fd
, out
, size
);
547 static void append_obj_to_pack(void *buf
,
548 unsigned long size
, enum object_type type
)
550 struct object_entry
*obj
= &objects
[nr_objects
++];
551 unsigned char header
[10];
552 unsigned long s
= size
;
554 unsigned char c
= (type
<< 4) | (s
& 15);
557 header
[n
++] = c
| 0x80;
562 write_or_die(output_fd
, header
, n
);
563 obj
[1].offset
= obj
[0].offset
+ n
;
564 obj
[1].offset
+= write_compressed(output_fd
, buf
, size
);
565 sha1_object(buf
, size
, type
, obj
->sha1
);
568 static int delta_pos_compare(const void *_a
, const void *_b
)
570 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
571 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
572 return a
->obj_no
- b
->obj_no
;
575 static void fix_unresolved_deltas(int nr_unresolved
)
577 struct delta_entry
**sorted_by_pos
;
578 int i
, n
= 0, percent
= -1;
581 * Since many unresolved deltas may well be themselves base objects
582 * for more unresolved deltas, we really want to include the
583 * smallest number of base objects that would cover as much delta
584 * as possible by picking the
585 * trunc deltas first, allowing for other deltas to resolve without
586 * additional base objects. Since most base objects are to be found
587 * before deltas depending on them, a good heuristic is to start
588 * resolving deltas in the same order as their position in the pack.
590 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
591 for (i
= 0; i
< nr_deltas
; i
++) {
592 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
594 sorted_by_pos
[n
++] = &deltas
[i
];
596 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
598 for (i
= 0; i
< n
; i
++) {
599 struct delta_entry
*d
= sorted_by_pos
[i
];
603 enum object_type obj_type
;
606 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
608 data
= read_sha1_file(d
->base
.sha1
, type
, &size
);
611 if (!strcmp(type
, blob_type
)) obj_type
= OBJ_BLOB
;
612 else if (!strcmp(type
, tree_type
)) obj_type
= OBJ_TREE
;
613 else if (!strcmp(type
, commit_type
)) obj_type
= OBJ_COMMIT
;
614 else if (!strcmp(type
, tag_type
)) obj_type
= OBJ_TAG
;
615 else die("base object %s is of type '%s'",
616 sha1_to_hex(d
->base
.sha1
), type
);
618 find_delta_children(&d
->base
, &first
, &last
);
619 for (j
= first
; j
<= last
; j
++) {
620 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
621 if (child
->real_type
== OBJ_REF_DELTA
)
622 resolve_delta(child
, data
, size
, obj_type
);
625 append_obj_to_pack(data
, size
, obj_type
);
628 percent
= display_progress(nr_resolved_deltas
,
636 static void readjust_pack_header_and_sha1(unsigned char *sha1
)
638 struct pack_header hdr
;
642 /* Rewrite pack header with updated object number */
643 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
644 die("cannot seek back: %s", strerror(errno
));
645 if (xread(output_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
646 die("cannot read pack header back: %s", strerror(errno
));
647 hdr
.hdr_entries
= htonl(nr_objects
);
648 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
649 die("cannot seek back: %s", strerror(errno
));
650 write_or_die(output_fd
, &hdr
, sizeof(hdr
));
651 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
652 die("cannot seek back: %s", strerror(errno
));
654 /* Recompute and store the new pack's SHA1 */
657 unsigned char *buf
[4096];
658 size
= xread(output_fd
, buf
, sizeof(buf
));
660 die("cannot read pack data back: %s", strerror(errno
));
661 SHA1_Update(&ctx
, buf
, size
);
663 SHA1_Final(sha1
, &ctx
);
664 write_or_die(output_fd
, sha1
, 20);
667 static int sha1_compare(const void *_a
, const void *_b
)
669 struct object_entry
*a
= *(struct object_entry
**)_a
;
670 struct object_entry
*b
= *(struct object_entry
**)_b
;
671 return hashcmp(a
->sha1
, b
->sha1
);
675 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
676 * the SHA1 hash of sorted object names.
678 static const char *write_index_file(const char *index_name
, unsigned char *sha1
)
681 struct object_entry
**sorted_by_sha
, **list
, **last
;
682 unsigned int array
[256];
688 xcalloc(nr_objects
, sizeof(struct object_entry
*));
689 list
= sorted_by_sha
;
690 last
= sorted_by_sha
+ nr_objects
;
691 for (i
= 0; i
< nr_objects
; ++i
)
692 sorted_by_sha
[i
] = &objects
[i
];
693 qsort(sorted_by_sha
, nr_objects
, sizeof(sorted_by_sha
[0]),
698 sorted_by_sha
= list
= last
= NULL
;
701 static char tmpfile
[PATH_MAX
];
702 snprintf(tmpfile
, sizeof(tmpfile
),
703 "%s/index_XXXXXX", get_object_directory());
704 fd
= mkstemp(tmpfile
);
705 index_name
= xstrdup(tmpfile
);
708 fd
= open(index_name
, O_CREAT
|O_EXCL
|O_WRONLY
, 0600);
711 die("unable to create %s: %s", index_name
, strerror(errno
));
712 f
= sha1fd(fd
, index_name
);
715 * Write the first-level table (the list is sorted,
716 * but we use a 256-entry lookup to be able to avoid
717 * having to do eight extra binary search iterations).
719 for (i
= 0; i
< 256; i
++) {
720 struct object_entry
**next
= list
;
721 while (next
< last
) {
722 struct object_entry
*obj
= *next
;
723 if (obj
->sha1
[0] != i
)
727 array
[i
] = htonl(next
- sorted_by_sha
);
730 sha1write(f
, array
, 256 * sizeof(int));
732 /* recompute the SHA1 hash of sorted object names.
733 * currently pack-objects does not do this, but that
738 * Write the actual SHA1 entries..
740 list
= sorted_by_sha
;
741 for (i
= 0; i
< nr_objects
; i
++) {
742 struct object_entry
*obj
= *list
++;
743 unsigned int offset
= htonl(obj
->offset
);
744 sha1write(f
, &offset
, 4);
745 sha1write(f
, obj
->sha1
, 20);
746 SHA1_Update(&ctx
, obj
->sha1
, 20);
748 sha1write(f
, sha1
, 20);
749 sha1close(f
, NULL
, 1);
751 SHA1_Final(sha1
, &ctx
);
755 static void final(const char *final_pack_name
, const char *curr_pack_name
,
756 const char *final_index_name
, const char *curr_index_name
,
757 const char *keep_name
, const char *keep_msg
,
760 char *report
= "pack";
767 err
= close(output_fd
);
769 die("error while closing pack file: %s", strerror(errno
));
770 chmod(curr_pack_name
, 0444);
774 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
776 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
777 get_object_directory(), sha1_to_hex(sha1
));
780 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
783 die("cannot write keep file");
785 if (keep_msg_len
> 0) {
786 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
787 write_or_die(keep_fd
, "\n", 1);
794 if (final_pack_name
!= curr_pack_name
) {
795 if (!final_pack_name
) {
796 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
797 get_object_directory(), sha1_to_hex(sha1
));
798 final_pack_name
= name
;
800 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
801 die("cannot store pack file");
804 chmod(curr_index_name
, 0444);
805 if (final_index_name
!= curr_index_name
) {
806 if (!final_index_name
) {
807 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
808 get_object_directory(), sha1_to_hex(sha1
));
809 final_index_name
= name
;
811 if (move_temp_to_file(curr_index_name
, final_index_name
))
812 die("cannot store index file");
816 printf("%s\n", sha1_to_hex(sha1
));
819 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
820 report
, sha1_to_hex(sha1
));
824 * Let's just mimic git-unpack-objects here and write
825 * the last part of the input buffer to stdout.
828 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
837 int main(int argc
, char **argv
)
839 int i
, fix_thin_pack
= 0;
840 const char *curr_pack
, *pack_name
= NULL
;
841 const char *curr_index
, *index_name
= NULL
;
842 const char *keep_name
= NULL
, *keep_msg
= NULL
;
843 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
844 unsigned char sha1
[20];
846 for (i
= 1; i
< argc
; i
++) {
847 const char *arg
= argv
[i
];
850 if (!strcmp(arg
, "--stdin")) {
852 } else if (!strcmp(arg
, "--fix-thin")) {
854 } else if (!strcmp(arg
, "--keep")) {
856 } else if (!strncmp(arg
, "--keep=", 7)) {
858 } else if (!strncmp(arg
, "--pack_header=", 14)) {
859 struct pack_header
*hdr
;
862 hdr
= (struct pack_header
*)input_buffer
;
863 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
864 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
867 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
870 input_len
= sizeof(*hdr
);
871 } else if (!strcmp(arg
, "-v")) {
873 } else if (!strcmp(arg
, "-o")) {
874 if (index_name
|| (i
+1) >= argc
)
875 usage(index_pack_usage
);
876 index_name
= argv
[++i
];
878 usage(index_pack_usage
);
883 usage(index_pack_usage
);
887 if (!pack_name
&& !from_stdin
)
888 usage(index_pack_usage
);
889 if (fix_thin_pack
&& !from_stdin
)
890 die("--fix-thin cannot be used without --stdin");
891 if (!index_name
&& pack_name
) {
892 int len
= strlen(pack_name
);
893 if (!has_extension(pack_name
, ".pack"))
894 die("packfile name '%s' does not end with '.pack'",
896 index_name_buf
= xmalloc(len
);
897 memcpy(index_name_buf
, pack_name
, len
- 5);
898 strcpy(index_name_buf
+ len
- 5, ".idx");
899 index_name
= index_name_buf
;
901 if (keep_msg
&& !keep_name
&& pack_name
) {
902 int len
= strlen(pack_name
);
903 if (!has_extension(pack_name
, ".pack"))
904 die("packfile name '%s' does not end with '.pack'",
906 keep_name_buf
= xmalloc(len
);
907 memcpy(keep_name_buf
, pack_name
, len
- 5);
908 strcpy(keep_name_buf
+ len
- 5, ".keep");
909 keep_name
= keep_name_buf
;
912 curr_pack
= open_pack_file(pack_name
);
914 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
915 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
917 setup_progress_signal();
918 parse_pack_objects(sha1
);
919 if (nr_deltas
!= nr_resolved_deltas
) {
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
);
930 fprintf(stderr
, "%d objects were added to complete this thin pack.\n",
931 nr_objects
- nr_objects_initial
);
932 readjust_pack_header_and_sha1(sha1
);
934 if (nr_deltas
!= nr_resolved_deltas
)
935 die("pack has %d unresolved deltas",
936 nr_deltas
- nr_resolved_deltas
);
938 /* Flush remaining pack final 20-byte SHA1. */
942 curr_index
= write_index_file(index_name
, sha1
);
943 final(pack_name
, curr_pack
,
944 index_name
, curr_index
,
948 free(index_name_buf
);