11 static const char index_pack_usage
[] =
12 "git-index-pack [-v] [-o <index-file>] [{ ---keep | --keep=<msg> }] { <pack-file> | --stdin [--fix-thin] [<pack-file>] }";
18 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 struct progress progress
;
53 /* We always read in 4kB chunks. */
54 static unsigned char input_buffer
[4096];
55 static unsigned int input_offset
, input_len
;
56 static off_t consumed_bytes
;
57 static SHA_CTX input_ctx
;
58 static uint32_t input_crc32
;
59 static int input_fd
, output_fd
, pack_fd
;
61 /* Discard current buffer used content. */
62 static void flush(void)
66 write_or_die(output_fd
, input_buffer
, input_offset
);
67 SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
68 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
74 * Make sure at least "min" bytes are available in the buffer, and
75 * return the pointer to the buffer.
77 static void *fill(int min
)
80 return input_buffer
+ input_offset
;
81 if (min
> sizeof(input_buffer
))
82 die("cannot fill %d bytes", min
);
85 int ret
= xread(input_fd
, input_buffer
+ input_len
,
86 sizeof(input_buffer
) - input_len
);
90 die("read error on input: %s", strerror(errno
));
93 } while (input_len
< min
);
97 static void use(int bytes
)
99 if (bytes
> input_len
)
100 die("used more bytes than were available");
101 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
103 input_offset
+= bytes
;
105 /* make sure off_t is sufficiently large not to wrap */
106 if (consumed_bytes
> consumed_bytes
+ bytes
)
107 die("pack too large for current definition of off_t");
108 consumed_bytes
+= bytes
;
111 static const char *open_pack_file(const char *pack_name
)
116 static char tmpfile
[PATH_MAX
];
117 snprintf(tmpfile
, sizeof(tmpfile
),
118 "%s/tmp_pack_XXXXXX", get_object_directory());
119 output_fd
= mkstemp(tmpfile
);
120 pack_name
= xstrdup(tmpfile
);
122 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
124 die("unable to create %s: %s\n", pack_name
, strerror(errno
));
127 input_fd
= open(pack_name
, O_RDONLY
);
129 die("cannot open packfile '%s': %s",
130 pack_name
, strerror(errno
));
134 SHA1_Init(&input_ctx
);
138 static void parse_pack_header(void)
140 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
142 /* Header consistency check */
143 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
144 die("pack signature mismatch");
145 if (!pack_version_ok(hdr
->hdr_version
))
146 die("pack version %d unsupported", ntohl(hdr
->hdr_version
));
148 nr_objects
= ntohl(hdr
->hdr_entries
);
149 use(sizeof(struct pack_header
));
152 static void bad_object(unsigned long offset
, const char *format
,
153 ...) NORETURN
__attribute__((format (printf
, 2, 3)));
155 static void bad_object(unsigned long offset
, const char *format
, ...)
160 va_start(params
, format
);
161 vsnprintf(buf
, sizeof(buf
), format
, params
);
163 die("pack has bad object at offset %lu: %s", offset
, buf
);
166 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
169 void *buf
= xmalloc(size
);
171 memset(&stream
, 0, sizeof(stream
));
172 stream
.next_out
= buf
;
173 stream
.avail_out
= size
;
174 stream
.next_in
= fill(1);
175 stream
.avail_in
= input_len
;
176 inflateInit(&stream
);
179 int ret
= inflate(&stream
, 0);
180 use(input_len
- stream
.avail_in
);
181 if (stream
.total_out
== size
&& ret
== Z_STREAM_END
)
184 bad_object(offset
, "inflate returned %d", ret
);
185 stream
.next_in
= fill(1);
186 stream
.avail_in
= input_len
;
192 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
200 obj
->offset
= consumed_bytes
;
201 input_crc32
= crc32(0, Z_NULL
, 0);
206 obj
->type
= (c
>> 4) & 7;
213 size
+= (c
& 0x7fUL
) << shift
;
220 hashcpy(delta_base
->sha1
, fill(20));
224 memset(delta_base
, 0, sizeof(*delta_base
));
228 base_offset
= c
& 127;
231 if (!base_offset
|| MSB(base_offset
, 7))
232 bad_object(obj
->offset
, "offset value overflow for delta base object");
236 base_offset
= (base_offset
<< 7) + (c
& 127);
238 delta_base
->offset
= obj
->offset
- base_offset
;
239 if (delta_base
->offset
>= obj
->offset
)
240 bad_object(obj
->offset
, "delta base offset is out of bound");
248 bad_object(obj
->offset
, "unknown object type %d", obj
->type
);
250 obj
->hdr_size
= consumed_bytes
- obj
->offset
;
252 data
= unpack_entry_data(obj
->offset
, obj
->size
);
253 obj
->crc32
= input_crc32
;
257 static void *get_data_from_pack(struct object_entry
*obj
)
259 unsigned long from
= obj
[0].offset
+ obj
[0].hdr_size
;
260 unsigned long len
= obj
[1].offset
- from
;
261 unsigned long rdy
= 0;
262 unsigned char *src
, *data
;
269 ssize_t n
= pread(pack_fd
, data
+ rdy
, len
- rdy
, from
+ rdy
);
271 die("cannot pread pack file: %s", strerror(errno
));
274 data
= xmalloc(obj
->size
);
275 memset(&stream
, 0, sizeof(stream
));
276 stream
.next_out
= data
;
277 stream
.avail_out
= obj
->size
;
278 stream
.next_in
= src
;
279 stream
.avail_in
= len
;
280 inflateInit(&stream
);
281 while ((st
= inflate(&stream
, Z_FINISH
)) == Z_OK
);
283 if (st
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
284 die("serious inflate inconsistency");
289 static int find_delta(const union delta_base
*base
)
291 int first
= 0, last
= nr_deltas
;
293 while (first
< last
) {
294 int next
= (first
+ last
) / 2;
295 struct delta_entry
*delta
= &deltas
[next
];
298 cmp
= memcmp(base
, &delta
->base
, UNION_BASE_SZ
);
310 static int find_delta_children(const union delta_base
*base
,
311 int *first_index
, int *last_index
)
313 int first
= find_delta(base
);
315 int end
= nr_deltas
- 1;
319 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
321 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
323 *first_index
= first
;
328 static void sha1_object(const void *data
, unsigned long size
,
329 enum object_type type
, unsigned char *sha1
)
331 hash_sha1_file(data
, size
, typename(type
), sha1
);
332 if (has_sha1_file(sha1
)) {
334 enum object_type has_type
;
335 unsigned long has_size
;
336 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
338 die("cannot read existing object %s", sha1_to_hex(sha1
));
339 if (size
!= has_size
|| type
!= has_type
||
340 memcmp(data
, has_data
, size
) != 0)
341 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
346 static void resolve_delta(struct object_entry
*delta_obj
, void *base_data
,
347 unsigned long base_size
, enum object_type type
)
350 unsigned long delta_size
;
352 unsigned long result_size
;
353 union delta_base delta_base
;
356 delta_obj
->real_type
= type
;
357 delta_data
= get_data_from_pack(delta_obj
);
358 delta_size
= delta_obj
->size
;
359 result
= patch_delta(base_data
, base_size
, delta_data
, delta_size
,
363 bad_object(delta_obj
->offset
, "failed to apply delta");
364 sha1_object(result
, result_size
, type
, delta_obj
->sha1
);
365 nr_resolved_deltas
++;
367 hashcpy(delta_base
.sha1
, delta_obj
->sha1
);
368 if (!find_delta_children(&delta_base
, &first
, &last
)) {
369 for (j
= first
; j
<= last
; j
++) {
370 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
371 if (child
->real_type
== OBJ_REF_DELTA
)
372 resolve_delta(child
, result
, result_size
, type
);
376 memset(&delta_base
, 0, sizeof(delta_base
));
377 delta_base
.offset
= delta_obj
->offset
;
378 if (!find_delta_children(&delta_base
, &first
, &last
)) {
379 for (j
= first
; j
<= last
; j
++) {
380 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
381 if (child
->real_type
== OBJ_OFS_DELTA
)
382 resolve_delta(child
, result
, result_size
, type
);
389 static int compare_delta_entry(const void *a
, const void *b
)
391 const struct delta_entry
*delta_a
= a
;
392 const struct delta_entry
*delta_b
= b
;
393 return memcmp(&delta_a
->base
, &delta_b
->base
, UNION_BASE_SZ
);
396 /* Parse all objects and return the pack content SHA1 hash */
397 static void parse_pack_objects(unsigned char *sha1
)
400 struct delta_entry
*delta
= deltas
;
406 * - find locations of all objects;
407 * - calculate SHA1 of all non-delta objects;
408 * - remember base (SHA1 or offset) for all deltas.
411 fprintf(stderr
, "Indexing %d objects.\n", nr_objects
);
412 start_progress(&progress
, "", nr_objects
);
414 for (i
= 0; i
< nr_objects
; i
++) {
415 struct object_entry
*obj
= &objects
[i
];
416 data
= unpack_raw_entry(obj
, &delta
->base
);
417 obj
->real_type
= obj
->type
;
418 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
) {
423 sha1_object(data
, obj
->size
, obj
->type
, obj
->sha1
);
426 display_progress(&progress
, i
+1);
428 objects
[i
].offset
= consumed_bytes
;
430 stop_progress(&progress
);
432 /* Check pack integrity */
434 SHA1_Final(sha1
, &input_ctx
);
435 if (hashcmp(fill(20), sha1
))
436 die("pack is corrupted (SHA1 mismatch)");
439 /* If input_fd is a file, we should have reached its end now. */
440 if (fstat(input_fd
, &st
))
441 die("cannot fstat packfile: %s", strerror(errno
));
442 if (S_ISREG(st
.st_mode
) &&
443 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
444 die("pack has junk at the end");
449 /* Sort deltas by base SHA1/offset for fast searching */
450 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
451 compare_delta_entry
);
455 * - for all non-delta objects, look if it is used as a base for
457 * - if used as a base, uncompress the object and apply all deltas,
458 * recursively checking if the resulting object is used as a base
459 * for some more deltas.
462 fprintf(stderr
, "Resolving %d deltas.\n", nr_deltas
);
463 start_progress(&progress
, "", nr_deltas
);
465 for (i
= 0; i
< nr_objects
; i
++) {
466 struct object_entry
*obj
= &objects
[i
];
467 union delta_base base
;
468 int j
, ref
, ref_first
, ref_last
, ofs
, ofs_first
, ofs_last
;
470 if (obj
->type
== OBJ_REF_DELTA
|| obj
->type
== OBJ_OFS_DELTA
)
472 hashcpy(base
.sha1
, obj
->sha1
);
473 ref
= !find_delta_children(&base
, &ref_first
, &ref_last
);
474 memset(&base
, 0, sizeof(base
));
475 base
.offset
= obj
->offset
;
476 ofs
= !find_delta_children(&base
, &ofs_first
, &ofs_last
);
479 data
= get_data_from_pack(obj
);
481 for (j
= ref_first
; j
<= ref_last
; j
++) {
482 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
483 if (child
->real_type
== OBJ_REF_DELTA
)
484 resolve_delta(child
, data
,
485 obj
->size
, obj
->type
);
488 for (j
= ofs_first
; j
<= ofs_last
; j
++) {
489 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
490 if (child
->real_type
== OBJ_OFS_DELTA
)
491 resolve_delta(child
, data
,
492 obj
->size
, obj
->type
);
496 display_progress(&progress
, nr_resolved_deltas
);
500 static int write_compressed(int fd
, void *in
, unsigned int size
, uint32_t *obj_crc
)
503 unsigned long maxsize
;
506 memset(&stream
, 0, sizeof(stream
));
507 deflateInit(&stream
, zlib_compression_level
);
508 maxsize
= deflateBound(&stream
, size
);
509 out
= xmalloc(maxsize
);
513 stream
.avail_in
= size
;
514 stream
.next_out
= out
;
515 stream
.avail_out
= maxsize
;
516 while (deflate(&stream
, Z_FINISH
) == Z_OK
);
519 size
= stream
.total_out
;
520 write_or_die(fd
, out
, size
);
521 *obj_crc
= crc32(*obj_crc
, out
, size
);
526 static void append_obj_to_pack(const unsigned char *sha1
, void *buf
,
527 unsigned long size
, enum object_type type
)
529 struct object_entry
*obj
= &objects
[nr_objects
++];
530 unsigned char header
[10];
531 unsigned long s
= size
;
533 unsigned char c
= (type
<< 4) | (s
& 15);
536 header
[n
++] = c
| 0x80;
541 write_or_die(output_fd
, header
, n
);
542 obj
[0].crc32
= crc32(0, Z_NULL
, 0);
543 obj
[0].crc32
= crc32(obj
[0].crc32
, header
, n
);
544 obj
[1].offset
= obj
[0].offset
+ n
;
545 obj
[1].offset
+= write_compressed(output_fd
, buf
, size
, &obj
[0].crc32
);
546 hashcpy(obj
->sha1
, sha1
);
549 static int delta_pos_compare(const void *_a
, const void *_b
)
551 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
552 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
553 return a
->obj_no
- b
->obj_no
;
556 static void fix_unresolved_deltas(int nr_unresolved
)
558 struct delta_entry
**sorted_by_pos
;
562 * Since many unresolved deltas may well be themselves base objects
563 * for more unresolved deltas, we really want to include the
564 * smallest number of base objects that would cover as much delta
565 * as possible by picking the
566 * trunc deltas first, allowing for other deltas to resolve without
567 * additional base objects. Since most base objects are to be found
568 * before deltas depending on them, a good heuristic is to start
569 * resolving deltas in the same order as their position in the pack.
571 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
572 for (i
= 0; i
< nr_deltas
; i
++) {
573 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
575 sorted_by_pos
[n
++] = &deltas
[i
];
577 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
579 for (i
= 0; i
< n
; i
++) {
580 struct delta_entry
*d
= sorted_by_pos
[i
];
583 enum object_type type
;
586 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
588 data
= read_sha1_file(d
->base
.sha1
, &type
, &size
);
592 find_delta_children(&d
->base
, &first
, &last
);
593 for (j
= first
; j
<= last
; j
++) {
594 struct object_entry
*child
= objects
+ deltas
[j
].obj_no
;
595 if (child
->real_type
== OBJ_REF_DELTA
)
596 resolve_delta(child
, data
, size
, type
);
599 if (check_sha1_signature(d
->base
.sha1
, data
, size
, typename(type
)))
600 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
601 append_obj_to_pack(d
->base
.sha1
, data
, size
, type
);
604 display_progress(&progress
, nr_resolved_deltas
);
609 static void readjust_pack_header_and_sha1(unsigned char *sha1
)
611 struct pack_header hdr
;
615 /* Rewrite pack header with updated object number */
616 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
617 die("cannot seek back: %s", strerror(errno
));
618 if (read_in_full(output_fd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
619 die("cannot read pack header back: %s", strerror(errno
));
620 hdr
.hdr_entries
= htonl(nr_objects
);
621 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
622 die("cannot seek back: %s", strerror(errno
));
623 write_or_die(output_fd
, &hdr
, sizeof(hdr
));
624 if (lseek(output_fd
, 0, SEEK_SET
) != 0)
625 die("cannot seek back: %s", strerror(errno
));
627 /* Recompute and store the new pack's SHA1 */
630 unsigned char *buf
[4096];
631 size
= xread(output_fd
, buf
, sizeof(buf
));
633 die("cannot read pack data back: %s", strerror(errno
));
634 SHA1_Update(&ctx
, buf
, size
);
636 SHA1_Final(sha1
, &ctx
);
637 write_or_die(output_fd
, sha1
, 20);
640 static uint32_t index_default_version
= 1;
641 static uint32_t index_off32_limit
= 0x7fffffff;
643 static int sha1_compare(const void *_a
, const void *_b
)
645 struct object_entry
*a
= *(struct object_entry
**)_a
;
646 struct object_entry
*b
= *(struct object_entry
**)_b
;
647 return hashcmp(a
->sha1
, b
->sha1
);
651 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
652 * the SHA1 hash of sorted object names.
654 static const char *write_index_file(const char *index_name
, unsigned char *sha1
)
657 struct object_entry
**sorted_by_sha
, **list
, **last
;
661 uint32_t index_version
;
665 xcalloc(nr_objects
, sizeof(struct object_entry
*));
666 list
= sorted_by_sha
;
667 last
= sorted_by_sha
+ nr_objects
;
668 for (i
= 0; i
< nr_objects
; ++i
)
669 sorted_by_sha
[i
] = &objects
[i
];
670 qsort(sorted_by_sha
, nr_objects
, sizeof(sorted_by_sha
[0]),
674 sorted_by_sha
= list
= last
= NULL
;
677 static char tmpfile
[PATH_MAX
];
678 snprintf(tmpfile
, sizeof(tmpfile
),
679 "%s/tmp_idx_XXXXXX", get_object_directory());
680 fd
= mkstemp(tmpfile
);
681 index_name
= xstrdup(tmpfile
);
684 fd
= open(index_name
, O_CREAT
|O_EXCL
|O_WRONLY
, 0600);
687 die("unable to create %s: %s", index_name
, strerror(errno
));
688 f
= sha1fd(fd
, index_name
);
690 /* if last object's offset is >= 2^31 we should use index V2 */
691 index_version
= (objects
[nr_objects
-1].offset
>> 31) ? 2 : index_default_version
;
693 /* index versions 2 and above need a header */
694 if (index_version
>= 2) {
695 struct pack_idx_header hdr
;
696 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
697 hdr
.idx_version
= htonl(index_version
);
698 sha1write(f
, &hdr
, sizeof(hdr
));
702 * Write the first-level table (the list is sorted,
703 * but we use a 256-entry lookup to be able to avoid
704 * having to do eight extra binary search iterations).
706 for (i
= 0; i
< 256; i
++) {
707 struct object_entry
**next
= list
;
708 while (next
< last
) {
709 struct object_entry
*obj
= *next
;
710 if (obj
->sha1
[0] != i
)
714 array
[i
] = htonl(next
- sorted_by_sha
);
717 sha1write(f
, array
, 256 * 4);
719 /* compute the SHA1 hash of sorted object names. */
723 * Write the actual SHA1 entries..
725 list
= sorted_by_sha
;
726 for (i
= 0; i
< nr_objects
; i
++) {
727 struct object_entry
*obj
= *list
++;
728 if (index_version
< 2) {
729 uint32_t offset
= htonl(obj
->offset
);
730 sha1write(f
, &offset
, 4);
732 sha1write(f
, obj
->sha1
, 20);
733 SHA1_Update(&ctx
, obj
->sha1
, 20);
736 if (index_version
>= 2) {
737 unsigned int nr_large_offset
= 0;
739 /* write the crc32 table */
740 list
= sorted_by_sha
;
741 for (i
= 0; i
< nr_objects
; i
++) {
742 struct object_entry
*obj
= *list
++;
743 uint32_t crc32_val
= htonl(obj
->crc32
);
744 sha1write(f
, &crc32_val
, 4);
747 /* write the 32-bit offset table */
748 list
= sorted_by_sha
;
749 for (i
= 0; i
< nr_objects
; i
++) {
750 struct object_entry
*obj
= *list
++;
751 uint32_t offset
= (obj
->offset
<= index_off32_limit
) ?
752 obj
->offset
: (0x80000000 | nr_large_offset
++);
753 offset
= htonl(offset
);
754 sha1write(f
, &offset
, 4);
757 /* write the large offset table */
758 list
= sorted_by_sha
;
759 while (nr_large_offset
) {
760 struct object_entry
*obj
= *list
++;
761 uint64_t offset
= obj
->offset
;
762 if (offset
> index_off32_limit
) {
764 split
[0] = htonl(offset
>> 32);
765 split
[1] = htonl(offset
& 0xffffffff);
766 sha1write(f
, split
, 8);
772 sha1write(f
, sha1
, 20);
773 sha1close(f
, NULL
, 1);
775 SHA1_Final(sha1
, &ctx
);
779 static void final(const char *final_pack_name
, const char *curr_pack_name
,
780 const char *final_index_name
, const char *curr_index_name
,
781 const char *keep_name
, const char *keep_msg
,
784 const char *report
= "pack";
791 err
= close(output_fd
);
793 die("error while closing pack file: %s", strerror(errno
));
794 chmod(curr_pack_name
, 0444);
798 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
800 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.keep",
801 get_object_directory(), sha1_to_hex(sha1
));
804 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
807 die("cannot write keep file");
809 if (keep_msg_len
> 0) {
810 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
811 write_or_die(keep_fd
, "\n", 1);
818 if (final_pack_name
!= curr_pack_name
) {
819 if (!final_pack_name
) {
820 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
821 get_object_directory(), sha1_to_hex(sha1
));
822 final_pack_name
= name
;
824 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
825 die("cannot store pack file");
828 chmod(curr_index_name
, 0444);
829 if (final_index_name
!= curr_index_name
) {
830 if (!final_index_name
) {
831 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
832 get_object_directory(), sha1_to_hex(sha1
));
833 final_index_name
= name
;
835 if (move_temp_to_file(curr_index_name
, final_index_name
))
836 die("cannot store index file");
840 printf("%s\n", sha1_to_hex(sha1
));
843 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
844 report
, sha1_to_hex(sha1
));
845 write_or_die(1, buf
, len
);
848 * Let's just mimic git-unpack-objects here and write
849 * the last part of the input buffer to stdout.
852 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
861 int main(int argc
, char **argv
)
863 int i
, fix_thin_pack
= 0;
864 const char *curr_pack
, *pack_name
= NULL
;
865 const char *curr_index
, *index_name
= NULL
;
866 const char *keep_name
= NULL
, *keep_msg
= NULL
;
867 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
868 unsigned char sha1
[20];
870 for (i
= 1; i
< argc
; i
++) {
871 const char *arg
= argv
[i
];
874 if (!strcmp(arg
, "--stdin")) {
876 } else if (!strcmp(arg
, "--fix-thin")) {
878 } else if (!strcmp(arg
, "--keep")) {
880 } else if (!prefixcmp(arg
, "--keep=")) {
882 } else if (!prefixcmp(arg
, "--pack_header=")) {
883 struct pack_header
*hdr
;
886 hdr
= (struct pack_header
*)input_buffer
;
887 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
888 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
891 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
894 input_len
= sizeof(*hdr
);
895 } else if (!strcmp(arg
, "-v")) {
897 } else if (!strcmp(arg
, "-o")) {
898 if (index_name
|| (i
+1) >= argc
)
899 usage(index_pack_usage
);
900 index_name
= argv
[++i
];
901 } else if (!prefixcmp(arg
, "--index-version=")) {
903 index_default_version
= strtoul(arg
+ 16, &c
, 10);
904 if (index_default_version
> 2)
907 index_off32_limit
= strtoul(c
+1, &c
, 0);
908 if (*c
|| index_off32_limit
& 0x80000000)
911 usage(index_pack_usage
);
916 usage(index_pack_usage
);
920 if (!pack_name
&& !from_stdin
)
921 usage(index_pack_usage
);
922 if (fix_thin_pack
&& !from_stdin
)
923 die("--fix-thin cannot be used without --stdin");
924 if (!index_name
&& pack_name
) {
925 int len
= strlen(pack_name
);
926 if (!has_extension(pack_name
, ".pack"))
927 die("packfile name '%s' does not end with '.pack'",
929 index_name_buf
= xmalloc(len
);
930 memcpy(index_name_buf
, pack_name
, len
- 5);
931 strcpy(index_name_buf
+ len
- 5, ".idx");
932 index_name
= index_name_buf
;
934 if (keep_msg
&& !keep_name
&& pack_name
) {
935 int len
= strlen(pack_name
);
936 if (!has_extension(pack_name
, ".pack"))
937 die("packfile name '%s' does not end with '.pack'",
939 keep_name_buf
= xmalloc(len
);
940 memcpy(keep_name_buf
, pack_name
, len
- 5);
941 strcpy(keep_name_buf
+ len
- 5, ".keep");
942 keep_name
= keep_name_buf
;
945 curr_pack
= open_pack_file(pack_name
);
947 objects
= xmalloc((nr_objects
+ 1) * sizeof(struct object_entry
));
948 deltas
= xmalloc(nr_objects
* sizeof(struct delta_entry
));
949 parse_pack_objects(sha1
);
950 if (nr_deltas
== nr_resolved_deltas
) {
952 stop_progress(&progress
);
953 /* Flush remaining pack final 20-byte SHA1. */
957 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
958 int nr_objects_initial
= nr_objects
;
959 if (nr_unresolved
<= 0)
960 die("confusion beyond insanity");
961 objects
= xrealloc(objects
,
962 (nr_objects
+ nr_unresolved
+ 1)
964 fix_unresolved_deltas(nr_unresolved
);
966 stop_progress(&progress
);
967 fprintf(stderr
, "%d objects were added to complete this thin pack.\n",
968 nr_objects
- nr_objects_initial
);
970 readjust_pack_header_and_sha1(sha1
);
972 if (nr_deltas
!= nr_resolved_deltas
)
973 die("pack has %d unresolved deltas",
974 nr_deltas
- nr_resolved_deltas
);
977 curr_index
= write_index_file(index_name
, sha1
);
978 final(pack_name
, curr_pack
,
979 index_name
, curr_index
,
983 free(index_name_buf
);