13 static const char index_pack_usage
[] =
14 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--verify] [--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
;
27 unsigned char sha1
[20];
32 struct base_data
*base
;
33 struct base_data
*child
;
34 struct object_entry
*obj
;
37 int ref_first
, ref_last
;
38 int ofs_first
, ofs_last
;
42 * Even if sizeof(union delta_base) == 24 on 64-bit archs, we really want
43 * to memcmp() only the first 20 bytes.
45 #define UNION_BASE_SZ 20
47 #define FLAG_LINK (1u<<20)
48 #define FLAG_CHECKED (1u<<21)
51 union delta_base base
;
55 static struct object_entry
*objects
;
56 static struct delta_entry
*deltas
;
57 static struct base_data
*base_cache
;
58 static size_t base_cache_used
;
59 static int nr_objects
;
61 static int nr_resolved_deltas
;
63 static int from_stdin
;
67 static struct progress
*progress
;
69 /* We always read in 4kB chunks. */
70 static unsigned char input_buffer
[4096];
71 static unsigned int input_offset
, input_len
;
72 static off_t consumed_bytes
;
73 static unsigned deepest_delta
;
74 static git_SHA_CTX input_ctx
;
75 static uint32_t input_crc32
;
76 static int input_fd
, output_fd
, pack_fd
;
78 static int mark_link(struct object
*obj
, int type
, void *data
)
83 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
84 die("object type mismatch at %s", sha1_to_hex(obj
->sha1
));
86 obj
->flags
|= FLAG_LINK
;
90 /* The content of each linked object must have been checked
91 or it must be already present in the object database */
92 static void check_object(struct object
*obj
)
97 if (!(obj
->flags
& FLAG_LINK
))
100 if (!(obj
->flags
& FLAG_CHECKED
)) {
102 int type
= sha1_object_info(obj
->sha1
, &size
);
103 if (type
!= obj
->type
|| type
<= 0)
104 die("object of unexpected type");
105 obj
->flags
|= FLAG_CHECKED
;
110 static void check_objects(void)
114 max
= get_max_object_index();
115 for (i
= 0; i
< max
; i
++)
116 check_object(get_indexed_object(i
));
120 /* Discard current buffer used content. */
121 static void flush(void)
125 write_or_die(output_fd
, input_buffer
, input_offset
);
126 git_SHA1_Update(&input_ctx
, input_buffer
, input_offset
);
127 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
133 * Make sure at least "min" bytes are available in the buffer, and
134 * return the pointer to the buffer.
136 static void *fill(int min
)
138 if (min
<= input_len
)
139 return input_buffer
+ input_offset
;
140 if (min
> sizeof(input_buffer
))
141 die("cannot fill %d bytes", min
);
144 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
145 sizeof(input_buffer
) - input_len
);
149 die_errno("read error on input");
153 display_throughput(progress
, consumed_bytes
+ input_len
);
154 } while (input_len
< min
);
158 static void use(int bytes
)
160 if (bytes
> input_len
)
161 die("used more bytes than were available");
162 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
164 input_offset
+= bytes
;
166 /* make sure off_t is sufficiently large not to wrap */
167 if (signed_add_overflows(consumed_bytes
, bytes
))
168 die("pack too large for current definition of off_t");
169 consumed_bytes
+= bytes
;
172 static const char *open_pack_file(const char *pack_name
)
177 static char tmp_file
[PATH_MAX
];
178 output_fd
= odb_mkstemp(tmp_file
, sizeof(tmp_file
),
179 "pack/tmp_pack_XXXXXX");
180 pack_name
= xstrdup(tmp_file
);
182 output_fd
= open(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
184 die_errno("unable to create '%s'", pack_name
);
187 input_fd
= open(pack_name
, O_RDONLY
);
189 die_errno("cannot open packfile '%s'", pack_name
);
193 git_SHA1_Init(&input_ctx
);
197 static void parse_pack_header(void)
199 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
201 /* Header consistency check */
202 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
203 die("pack signature mismatch");
204 if (!pack_version_ok(hdr
->hdr_version
))
205 die("pack version %"PRIu32
" unsupported",
206 ntohl(hdr
->hdr_version
));
208 nr_objects
= ntohl(hdr
->hdr_entries
);
209 use(sizeof(struct pack_header
));
212 static NORETURN
void bad_object(unsigned long offset
, const char *format
,
213 ...) __attribute__((format (printf
, 2, 3)));
215 static NORETURN
void bad_object(unsigned long offset
, const char *format
, ...)
220 va_start(params
, format
);
221 vsnprintf(buf
, sizeof(buf
), format
, params
);
223 die("pack has bad object at offset %lu: %s", offset
, buf
);
226 static struct base_data
*alloc_base_data(void)
228 struct base_data
*base
= xmalloc(sizeof(struct base_data
));
229 memset(base
, 0, sizeof(*base
));
235 static void free_base_data(struct base_data
*c
)
240 base_cache_used
-= c
->size
;
244 static void prune_base_data(struct base_data
*retain
)
248 base_cache_used
> delta_base_cache_limit
&& b
;
250 if (b
->data
&& b
!= retain
)
255 static void link_base_data(struct base_data
*base
, struct base_data
*c
)
265 base_cache_used
+= c
->size
;
269 static void unlink_base_data(struct base_data
*c
)
271 struct base_data
*base
= c
->base
;
279 static void *unpack_entry_data(unsigned long offset
, unsigned long size
)
283 void *buf
= xmalloc(size
);
285 memset(&stream
, 0, sizeof(stream
));
286 git_inflate_init(&stream
);
287 stream
.next_out
= buf
;
288 stream
.avail_out
= size
;
291 stream
.next_in
= fill(1);
292 stream
.avail_in
= input_len
;
293 status
= git_inflate(&stream
, 0);
294 use(input_len
- stream
.avail_in
);
295 } while (status
== Z_OK
);
296 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
297 bad_object(offset
, "inflate returned %d", status
);
298 git_inflate_end(&stream
);
302 static void *unpack_raw_entry(struct object_entry
*obj
, union delta_base
*delta_base
)
305 unsigned long size
, c
;
310 obj
->idx
.offset
= consumed_bytes
;
311 input_crc32
= crc32(0, NULL
, 0);
316 obj
->type
= (c
>> 4) & 7;
323 size
+= (c
& 0x7f) << shift
;
330 hashcpy(delta_base
->sha1
, fill(20));
334 memset(delta_base
, 0, sizeof(*delta_base
));
338 base_offset
= c
& 127;
341 if (!base_offset
|| MSB(base_offset
, 7))
342 bad_object(obj
->idx
.offset
, "offset value overflow for delta base object");
346 base_offset
= (base_offset
<< 7) + (c
& 127);
348 delta_base
->offset
= obj
->idx
.offset
- base_offset
;
349 if (delta_base
->offset
<= 0 || delta_base
->offset
>= obj
->idx
.offset
)
350 bad_object(obj
->idx
.offset
, "delta base offset is out of bound");
358 bad_object(obj
->idx
.offset
, "unknown object type %d", obj
->type
);
360 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
362 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
);
363 obj
->idx
.crc32
= input_crc32
;
367 static void *get_data_from_pack(struct object_entry
*obj
)
369 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
370 unsigned long len
= obj
[1].idx
.offset
- from
;
371 unsigned char *data
, *inbuf
;
375 data
= xmalloc(obj
->size
);
376 inbuf
= xmalloc((len
< 64*1024) ? len
: 64*1024);
378 memset(&stream
, 0, sizeof(stream
));
379 git_inflate_init(&stream
);
380 stream
.next_out
= data
;
381 stream
.avail_out
= obj
->size
;
384 ssize_t n
= (len
< 64*1024) ? len
: 64*1024;
385 n
= pread(pack_fd
, inbuf
, n
, from
);
387 die_errno("cannot pread pack file");
389 die("premature end of pack file, %lu bytes missing", len
);
392 stream
.next_in
= inbuf
;
394 status
= git_inflate(&stream
, 0);
395 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
397 /* This has been inflated OK when first encountered, so... */
398 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
399 die("serious inflate inconsistency");
401 git_inflate_end(&stream
);
406 static int compare_delta_bases(const union delta_base
*base1
,
407 const union delta_base
*base2
,
408 enum object_type type1
,
409 enum object_type type2
)
411 int cmp
= type1
- type2
;
414 return memcmp(base1
, base2
, UNION_BASE_SZ
);
417 static int find_delta(const union delta_base
*base
, enum object_type type
)
419 int first
= 0, last
= nr_deltas
;
421 while (first
< last
) {
422 int next
= (first
+ last
) / 2;
423 struct delta_entry
*delta
= &deltas
[next
];
426 cmp
= compare_delta_bases(base
, &delta
->base
,
427 type
, objects
[delta
->obj_no
].type
);
439 static void find_delta_children(const union delta_base
*base
,
440 int *first_index
, int *last_index
,
441 enum object_type type
)
443 int first
= find_delta(base
, type
);
445 int end
= nr_deltas
- 1;
452 while (first
> 0 && !memcmp(&deltas
[first
- 1].base
, base
, UNION_BASE_SZ
))
454 while (last
< end
&& !memcmp(&deltas
[last
+ 1].base
, base
, UNION_BASE_SZ
))
456 *first_index
= first
;
460 static void sha1_object(const void *data
, unsigned long size
,
461 enum object_type type
, unsigned char *sha1
)
463 hash_sha1_file(data
, size
, typename(type
), sha1
);
464 if (has_sha1_file(sha1
)) {
466 enum object_type has_type
;
467 unsigned long has_size
;
468 has_data
= read_sha1_file(sha1
, &has_type
, &has_size
);
470 die("cannot read existing object %s", sha1_to_hex(sha1
));
471 if (size
!= has_size
|| type
!= has_type
||
472 memcmp(data
, has_data
, size
) != 0)
473 die("SHA1 COLLISION FOUND WITH %s !", sha1_to_hex(sha1
));
477 if (type
== OBJ_BLOB
) {
478 struct blob
*blob
= lookup_blob(sha1
);
480 blob
->object
.flags
|= FLAG_CHECKED
;
482 die("invalid blob object %s", sha1_to_hex(sha1
));
486 void *buf
= (void *) data
;
489 * we do not need to free the memory here, as the
490 * buf is deleted by the caller.
492 obj
= parse_object_buffer(sha1
, type
, size
, buf
, &eaten
);
494 die("invalid %s", typename(type
));
495 if (fsck_object(obj
, 1, fsck_error_function
))
496 die("Error in object");
497 if (fsck_walk(obj
, mark_link
, NULL
))
498 die("Not all child objects of %s are reachable", sha1_to_hex(obj
->sha1
));
500 if (obj
->type
== OBJ_TREE
) {
501 struct tree
*item
= (struct tree
*) obj
;
504 if (obj
->type
== OBJ_COMMIT
) {
505 struct commit
*commit
= (struct commit
*) obj
;
506 commit
->buffer
= NULL
;
508 obj
->flags
|= FLAG_CHECKED
;
513 static int is_delta_type(enum object_type type
)
515 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
519 * This function is part of find_unresolved_deltas(). There are two
520 * walkers going in the opposite ways.
522 * The first one in find_unresolved_deltas() traverses down from
523 * parent node to children, deflating nodes along the way. However,
524 * memory for deflated nodes is limited by delta_base_cache_limit, so
525 * at some point parent node's deflated content may be freed.
527 * The second walker is this function, which goes from current node up
528 * to top parent if necessary to deflate the node. In normal
529 * situation, its parent node would be already deflated, so it just
530 * needs to apply delta.
532 * In the worst case scenario, parent node is no longer deflated because
533 * we're running out of delta_base_cache_limit; we need to re-deflate
534 * parents, possibly up to the top base.
536 * All deflated objects here are subject to be freed if we exceed
537 * delta_base_cache_limit, just like in find_unresolved_deltas(), we
538 * just need to make sure the last node is not freed.
540 static void *get_base_data(struct base_data
*c
)
543 struct object_entry
*obj
= c
->obj
;
544 struct base_data
**delta
= NULL
;
545 int delta_nr
= 0, delta_alloc
= 0;
547 while (is_delta_type(c
->obj
->type
) && !c
->data
) {
548 ALLOC_GROW(delta
, delta_nr
+ 1, delta_alloc
);
549 delta
[delta_nr
++] = c
;
553 c
->data
= get_data_from_pack(obj
);
555 base_cache_used
+= c
->size
;
558 for (; delta_nr
> 0; delta_nr
--) {
560 c
= delta
[delta_nr
- 1];
562 base
= get_base_data(c
->base
);
563 raw
= get_data_from_pack(obj
);
564 c
->data
= patch_delta(
570 bad_object(obj
->idx
.offset
, "failed to apply delta");
571 base_cache_used
+= c
->size
;
579 static void resolve_delta(struct object_entry
*delta_obj
,
580 struct base_data
*base
, struct base_data
*result
)
582 void *base_data
, *delta_data
;
584 delta_obj
->real_type
= base
->obj
->real_type
;
585 delta_obj
->delta_depth
= base
->obj
->delta_depth
+ 1;
586 if (deepest_delta
< delta_obj
->delta_depth
)
587 deepest_delta
= delta_obj
->delta_depth
;
588 delta_obj
->base_object_no
= base
->obj
- objects
;
589 delta_data
= get_data_from_pack(delta_obj
);
590 base_data
= get_base_data(base
);
591 result
->obj
= delta_obj
;
592 result
->data
= patch_delta(base_data
, base
->size
,
593 delta_data
, delta_obj
->size
, &result
->size
);
596 bad_object(delta_obj
->idx
.offset
, "failed to apply delta");
597 sha1_object(result
->data
, result
->size
, delta_obj
->real_type
,
598 delta_obj
->idx
.sha1
);
599 nr_resolved_deltas
++;
602 static struct base_data
*find_unresolved_deltas_1(struct base_data
*base
,
603 struct base_data
*prev_base
)
605 if (base
->ref_last
== -1 && base
->ofs_last
== -1) {
606 union delta_base base_spec
;
608 hashcpy(base_spec
.sha1
, base
->obj
->idx
.sha1
);
609 find_delta_children(&base_spec
,
610 &base
->ref_first
, &base
->ref_last
, OBJ_REF_DELTA
);
612 memset(&base_spec
, 0, sizeof(base_spec
));
613 base_spec
.offset
= base
->obj
->idx
.offset
;
614 find_delta_children(&base_spec
,
615 &base
->ofs_first
, &base
->ofs_last
, OBJ_OFS_DELTA
);
617 if (base
->ref_last
== -1 && base
->ofs_last
== -1) {
622 link_base_data(prev_base
, base
);
625 if (base
->ref_first
<= base
->ref_last
) {
626 struct object_entry
*child
= objects
+ deltas
[base
->ref_first
].obj_no
;
627 struct base_data
*result
= alloc_base_data();
629 assert(child
->real_type
== OBJ_REF_DELTA
);
630 resolve_delta(child
, base
, result
);
631 if (base
->ref_first
== base
->ref_last
&& base
->ofs_last
== -1)
632 free_base_data(base
);
638 if (base
->ofs_first
<= base
->ofs_last
) {
639 struct object_entry
*child
= objects
+ deltas
[base
->ofs_first
].obj_no
;
640 struct base_data
*result
= alloc_base_data();
642 assert(child
->real_type
== OBJ_OFS_DELTA
);
643 resolve_delta(child
, base
, result
);
644 if (base
->ofs_first
== base
->ofs_last
)
645 free_base_data(base
);
651 unlink_base_data(base
);
655 static void find_unresolved_deltas(struct base_data
*base
)
657 struct base_data
*new_base
, *prev_base
= NULL
;
659 new_base
= find_unresolved_deltas_1(base
, prev_base
);
669 prev_base
= base
->base
;
674 static int compare_delta_entry(const void *a
, const void *b
)
676 const struct delta_entry
*delta_a
= a
;
677 const struct delta_entry
*delta_b
= b
;
679 /* group by type (ref vs ofs) and then by value (sha-1 or offset) */
680 return compare_delta_bases(&delta_a
->base
, &delta_b
->base
,
681 objects
[delta_a
->obj_no
].type
,
682 objects
[delta_b
->obj_no
].type
);
685 /* Parse all objects and return the pack content SHA1 hash */
686 static void parse_pack_objects(unsigned char *sha1
)
689 struct delta_entry
*delta
= deltas
;
694 * - find locations of all objects;
695 * - calculate SHA1 of all non-delta objects;
696 * - remember base (SHA1 or offset) for all deltas.
699 progress
= start_progress(
700 from_stdin
? "Receiving objects" : "Indexing objects",
702 for (i
= 0; i
< nr_objects
; i
++) {
703 struct object_entry
*obj
= &objects
[i
];
704 void *data
= unpack_raw_entry(obj
, &delta
->base
);
705 obj
->real_type
= obj
->type
;
706 if (is_delta_type(obj
->type
)) {
711 sha1_object(data
, obj
->size
, obj
->type
, obj
->idx
.sha1
);
713 display_progress(progress
, i
+1);
715 objects
[i
].idx
.offset
= consumed_bytes
;
716 stop_progress(&progress
);
718 /* Check pack integrity */
720 git_SHA1_Final(sha1
, &input_ctx
);
721 if (hashcmp(fill(20), sha1
))
722 die("pack is corrupted (SHA1 mismatch)");
725 /* If input_fd is a file, we should have reached its end now. */
726 if (fstat(input_fd
, &st
))
727 die_errno("cannot fstat packfile");
728 if (S_ISREG(st
.st_mode
) &&
729 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
730 die("pack has junk at the end");
735 /* Sort deltas by base SHA1/offset for fast searching */
736 qsort(deltas
, nr_deltas
, sizeof(struct delta_entry
),
737 compare_delta_entry
);
741 * - for all non-delta objects, look if it is used as a base for
743 * - if used as a base, uncompress the object and apply all deltas,
744 * recursively checking if the resulting object is used as a base
745 * for some more deltas.
748 progress
= start_progress("Resolving deltas", nr_deltas
);
749 for (i
= 0; i
< nr_objects
; i
++) {
750 struct object_entry
*obj
= &objects
[i
];
751 struct base_data
*base_obj
= alloc_base_data();
753 if (is_delta_type(obj
->type
))
756 base_obj
->data
= NULL
;
757 find_unresolved_deltas(base_obj
);
758 display_progress(progress
, nr_resolved_deltas
);
762 static int write_compressed(struct sha1file
*f
, void *in
, unsigned int size
)
766 unsigned char outbuf
[4096];
768 memset(&stream
, 0, sizeof(stream
));
769 git_deflate_init(&stream
, zlib_compression_level
);
771 stream
.avail_in
= size
;
774 stream
.next_out
= outbuf
;
775 stream
.avail_out
= sizeof(outbuf
);
776 status
= git_deflate(&stream
, Z_FINISH
);
777 sha1write(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
778 } while (status
== Z_OK
);
780 if (status
!= Z_STREAM_END
)
781 die("unable to deflate appended object (%d)", status
);
782 size
= stream
.total_out
;
783 git_deflate_end(&stream
);
787 static struct object_entry
*append_obj_to_pack(struct sha1file
*f
,
788 const unsigned char *sha1
, void *buf
,
789 unsigned long size
, enum object_type type
)
791 struct object_entry
*obj
= &objects
[nr_objects
++];
792 unsigned char header
[10];
793 unsigned long s
= size
;
795 unsigned char c
= (type
<< 4) | (s
& 15);
798 header
[n
++] = c
| 0x80;
804 sha1write(f
, header
, n
);
808 obj
[0].real_type
= type
;
809 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
810 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
811 obj
[0].idx
.crc32
= crc32_end(f
);
813 hashcpy(obj
->idx
.sha1
, sha1
);
817 static int delta_pos_compare(const void *_a
, const void *_b
)
819 struct delta_entry
*a
= *(struct delta_entry
**)_a
;
820 struct delta_entry
*b
= *(struct delta_entry
**)_b
;
821 return a
->obj_no
- b
->obj_no
;
824 static void fix_unresolved_deltas(struct sha1file
*f
, int nr_unresolved
)
826 struct delta_entry
**sorted_by_pos
;
830 * Since many unresolved deltas may well be themselves base objects
831 * for more unresolved deltas, we really want to include the
832 * smallest number of base objects that would cover as much delta
833 * as possible by picking the
834 * trunc deltas first, allowing for other deltas to resolve without
835 * additional base objects. Since most base objects are to be found
836 * before deltas depending on them, a good heuristic is to start
837 * resolving deltas in the same order as their position in the pack.
839 sorted_by_pos
= xmalloc(nr_unresolved
* sizeof(*sorted_by_pos
));
840 for (i
= 0; i
< nr_deltas
; i
++) {
841 if (objects
[deltas
[i
].obj_no
].real_type
!= OBJ_REF_DELTA
)
843 sorted_by_pos
[n
++] = &deltas
[i
];
845 qsort(sorted_by_pos
, n
, sizeof(*sorted_by_pos
), delta_pos_compare
);
847 for (i
= 0; i
< n
; i
++) {
848 struct delta_entry
*d
= sorted_by_pos
[i
];
849 enum object_type type
;
850 struct base_data
*base_obj
= alloc_base_data();
852 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
854 base_obj
->data
= read_sha1_file(d
->base
.sha1
, &type
, &base_obj
->size
);
858 if (check_sha1_signature(d
->base
.sha1
, base_obj
->data
,
859 base_obj
->size
, typename(type
)))
860 die("local object %s is corrupt", sha1_to_hex(d
->base
.sha1
));
861 base_obj
->obj
= append_obj_to_pack(f
, d
->base
.sha1
,
862 base_obj
->data
, base_obj
->size
, type
);
863 find_unresolved_deltas(base_obj
);
864 display_progress(progress
, nr_resolved_deltas
);
869 static void final(const char *final_pack_name
, const char *curr_pack_name
,
870 const char *final_index_name
, const char *curr_index_name
,
871 const char *keep_name
, const char *keep_msg
,
874 const char *report
= "pack";
881 fsync_or_die(output_fd
, curr_pack_name
);
882 err
= close(output_fd
);
884 die_errno("error while closing pack file");
888 int keep_fd
, keep_msg_len
= strlen(keep_msg
);
891 keep_fd
= odb_pack_keep(name
, sizeof(name
), sha1
);
893 keep_fd
= open(keep_name
, O_RDWR
|O_CREAT
|O_EXCL
, 0600);
897 die_errno("cannot write keep file '%s'",
900 if (keep_msg_len
> 0) {
901 write_or_die(keep_fd
, keep_msg
, keep_msg_len
);
902 write_or_die(keep_fd
, "\n", 1);
904 if (close(keep_fd
) != 0)
905 die_errno("cannot close written keep file '%s'",
911 if (final_pack_name
!= curr_pack_name
) {
912 if (!final_pack_name
) {
913 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.pack",
914 get_object_directory(), sha1_to_hex(sha1
));
915 final_pack_name
= name
;
917 if (move_temp_to_file(curr_pack_name
, final_pack_name
))
918 die("cannot store pack file");
919 } else if (from_stdin
)
920 chmod(final_pack_name
, 0444);
922 if (final_index_name
!= curr_index_name
) {
923 if (!final_index_name
) {
924 snprintf(name
, sizeof(name
), "%s/pack/pack-%s.idx",
925 get_object_directory(), sha1_to_hex(sha1
));
926 final_index_name
= name
;
928 if (move_temp_to_file(curr_index_name
, final_index_name
))
929 die("cannot store index file");
931 chmod(final_index_name
, 0444);
934 printf("%s\n", sha1_to_hex(sha1
));
937 int len
= snprintf(buf
, sizeof(buf
), "%s\t%s\n",
938 report
, sha1_to_hex(sha1
));
939 write_or_die(1, buf
, len
);
942 * Let's just mimic git-unpack-objects here and write
943 * the last part of the input buffer to stdout.
946 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
955 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
957 struct pack_idx_option
*opts
= cb
;
959 if (!strcmp(k
, "pack.indexversion")) {
960 opts
->version
= git_config_int(k
, v
);
961 if (opts
->version
> 2)
962 die("bad pack.indexversion=%"PRIu32
, opts
->version
);
965 return git_default_config(k
, v
, cb
);
968 static int cmp_uint32(const void *a_
, const void *b_
)
970 uint32_t a
= *((uint32_t *)a_
);
971 uint32_t b
= *((uint32_t *)b_
);
973 return (a
< b
) ? -1 : (a
!= b
);
976 static void read_v2_anomalous_offsets(struct packed_git
*p
,
977 struct pack_idx_option
*opts
)
979 const uint32_t *idx1
, *idx2
;
982 /* The address of the 4-byte offset table */
983 idx1
= (((const uint32_t *)p
->index_data
)
984 + 2 /* 8-byte header */
986 + 5 * p
->num_objects
/* 20-byte SHA-1 table */
987 + p
->num_objects
/* CRC32 table */
990 /* The address of the 8-byte offset table */
991 idx2
= idx1
+ p
->num_objects
;
993 for (i
= 0; i
< p
->num_objects
; i
++) {
994 uint32_t off
= ntohl(idx1
[i
]);
995 if (!(off
& 0x80000000))
997 off
= off
& 0x7fffffff;
1001 * The real offset is ntohl(idx2[off * 2]) in high 4
1002 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1003 * octets. But idx2[off * 2] is Zero!!!
1005 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
1006 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
1009 if (1 < opts
->anomaly_nr
)
1010 qsort(opts
->anomaly
, opts
->anomaly_nr
, sizeof(uint32_t), cmp_uint32
);
1013 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
1015 struct packed_git
*p
= add_packed_git(pack_name
, strlen(pack_name
), 1);
1018 die("Cannot open existing pack file '%s'", pack_name
);
1019 if (open_pack_index(p
))
1020 die("Cannot open existing pack idx file for '%s'", pack_name
);
1022 /* Read the attributes from the existing idx file */
1023 opts
->version
= p
->index_version
;
1025 if (opts
->version
== 2)
1026 read_v2_anomalous_offsets(p
, opts
);
1029 * Get rid of the idx file as we do not need it anymore.
1030 * NEEDSWORK: extract this bit from free_pack_by_name() in
1031 * sha1_file.c, perhaps? It shouldn't matter very much as we
1032 * know we haven't installed this pack (hence we never have
1033 * read anything from it).
1035 close_pack_index(p
);
1039 static void show_pack_info(int stat_only
)
1041 int i
, baseobjects
= nr_objects
- nr_deltas
;
1042 unsigned long *chain_histogram
= NULL
;
1045 chain_histogram
= xcalloc(deepest_delta
, sizeof(unsigned long));
1047 for (i
= 0; i
< nr_objects
; i
++) {
1048 struct object_entry
*obj
= &objects
[i
];
1050 if (is_delta_type(obj
->type
))
1051 chain_histogram
[obj
->delta_depth
- 1]++;
1054 printf("%s %-6s %lu %lu %"PRIuMAX
,
1055 sha1_to_hex(obj
->idx
.sha1
),
1056 typename(obj
->real_type
), obj
->size
,
1057 (unsigned long)(obj
[1].idx
.offset
- obj
->idx
.offset
),
1058 (uintmax_t)obj
->idx
.offset
);
1059 if (is_delta_type(obj
->type
)) {
1060 struct object_entry
*bobj
= &objects
[obj
->base_object_no
];
1061 printf(" %u %s", obj
->delta_depth
, sha1_to_hex(bobj
->idx
.sha1
));
1067 printf("non delta: %d object%s\n",
1068 baseobjects
, baseobjects
> 1 ? "s" : "");
1069 for (i
= 0; i
< deepest_delta
; i
++) {
1070 if (!chain_histogram
[i
])
1072 printf("chain length = %d: %lu object%s\n",
1075 chain_histogram
[i
] > 1 ? "s" : "");
1079 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
1081 int i
, fix_thin_pack
= 0, verify
= 0, stat_only
= 0, stat
= 0;
1082 const char *curr_pack
, *curr_index
;
1083 const char *index_name
= NULL
, *pack_name
= NULL
;
1084 const char *keep_name
= NULL
, *keep_msg
= NULL
;
1085 char *index_name_buf
= NULL
, *keep_name_buf
= NULL
;
1086 struct pack_idx_entry
**idx_objects
;
1087 struct pack_idx_option opts
;
1088 unsigned char pack_sha1
[20];
1090 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1091 usage(index_pack_usage
);
1093 read_replace_refs
= 0;
1095 reset_pack_idx_option(&opts
);
1096 git_config(git_index_pack_config
, &opts
);
1097 if (prefix
&& chdir(prefix
))
1098 die("Cannot come back to cwd");
1100 for (i
= 1; i
< argc
; i
++) {
1101 const char *arg
= argv
[i
];
1104 if (!strcmp(arg
, "--stdin")) {
1106 } else if (!strcmp(arg
, "--fix-thin")) {
1108 } else if (!strcmp(arg
, "--strict")) {
1110 } else if (!strcmp(arg
, "--verify")) {
1112 } else if (!strcmp(arg
, "--verify-stat")) {
1115 } else if (!strcmp(arg
, "--verify-stat-only")) {
1119 } else if (!strcmp(arg
, "--keep")) {
1121 } else if (!prefixcmp(arg
, "--keep=")) {
1123 } else if (!prefixcmp(arg
, "--pack_header=")) {
1124 struct pack_header
*hdr
;
1127 hdr
= (struct pack_header
*)input_buffer
;
1128 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
1129 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
1132 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
1135 input_len
= sizeof(*hdr
);
1136 } else if (!strcmp(arg
, "-v")) {
1138 } else if (!strcmp(arg
, "-o")) {
1139 if (index_name
|| (i
+1) >= argc
)
1140 usage(index_pack_usage
);
1141 index_name
= argv
[++i
];
1142 } else if (!prefixcmp(arg
, "--index-version=")) {
1144 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1145 if (opts
.version
> 2)
1148 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1149 if (*c
|| opts
.off32_limit
& 0x80000000)
1152 usage(index_pack_usage
);
1157 usage(index_pack_usage
);
1161 if (!pack_name
&& !from_stdin
)
1162 usage(index_pack_usage
);
1163 if (fix_thin_pack
&& !from_stdin
)
1164 die("--fix-thin cannot be used without --stdin");
1165 if (!index_name
&& pack_name
) {
1166 int len
= strlen(pack_name
);
1167 if (!has_extension(pack_name
, ".pack"))
1168 die("packfile name '%s' does not end with '.pack'",
1170 index_name_buf
= xmalloc(len
);
1171 memcpy(index_name_buf
, pack_name
, len
- 5);
1172 strcpy(index_name_buf
+ len
- 5, ".idx");
1173 index_name
= index_name_buf
;
1175 if (keep_msg
&& !keep_name
&& pack_name
) {
1176 int len
= strlen(pack_name
);
1177 if (!has_extension(pack_name
, ".pack"))
1178 die("packfile name '%s' does not end with '.pack'",
1180 keep_name_buf
= xmalloc(len
);
1181 memcpy(keep_name_buf
, pack_name
, len
- 5);
1182 strcpy(keep_name_buf
+ len
- 5, ".keep");
1183 keep_name
= keep_name_buf
;
1187 die("--verify with no packfile name given");
1188 read_idx_option(&opts
, index_name
);
1189 opts
.flags
|= WRITE_IDX_VERIFY
| WRITE_IDX_STRICT
;
1192 opts
.flags
|= WRITE_IDX_STRICT
;
1194 curr_pack
= open_pack_file(pack_name
);
1195 parse_pack_header();
1196 objects
= xcalloc(nr_objects
+ 1, sizeof(struct object_entry
));
1197 deltas
= xcalloc(nr_objects
, sizeof(struct delta_entry
));
1198 parse_pack_objects(pack_sha1
);
1199 if (nr_deltas
== nr_resolved_deltas
) {
1200 stop_progress(&progress
);
1201 /* Flush remaining pack final 20-byte SHA1. */
1204 if (fix_thin_pack
) {
1206 unsigned char read_sha1
[20], tail_sha1
[20];
1208 int nr_unresolved
= nr_deltas
- nr_resolved_deltas
;
1209 int nr_objects_initial
= nr_objects
;
1210 if (nr_unresolved
<= 0)
1211 die("confusion beyond insanity");
1212 objects
= xrealloc(objects
,
1213 (nr_objects
+ nr_unresolved
+ 1)
1214 * sizeof(*objects
));
1215 f
= sha1fd(output_fd
, curr_pack
);
1216 fix_unresolved_deltas(f
, nr_unresolved
);
1217 sprintf(msg
, "completed with %d local objects",
1218 nr_objects
- nr_objects_initial
);
1219 stop_progress_msg(&progress
, msg
);
1220 sha1close(f
, tail_sha1
, 0);
1221 hashcpy(read_sha1
, pack_sha1
);
1222 fixup_pack_header_footer(output_fd
, pack_sha1
,
1223 curr_pack
, nr_objects
,
1224 read_sha1
, consumed_bytes
-20);
1225 if (hashcmp(read_sha1
, tail_sha1
) != 0)
1226 die("Unexpected tail checksum for %s "
1227 "(disk corruption?)", curr_pack
);
1229 if (nr_deltas
!= nr_resolved_deltas
)
1230 die("pack has %d unresolved deltas",
1231 nr_deltas
- nr_resolved_deltas
);
1238 show_pack_info(stat_only
);
1240 idx_objects
= xmalloc((nr_objects
) * sizeof(struct pack_idx_entry
*));
1241 for (i
= 0; i
< nr_objects
; i
++)
1242 idx_objects
[i
] = &objects
[i
].idx
;
1243 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, &opts
, pack_sha1
);
1247 final(pack_name
, curr_pack
,
1248 index_name
, curr_index
,
1249 keep_name
, keep_msg
,
1254 free(index_name_buf
);
1255 free(keep_name_buf
);
1256 if (pack_name
== NULL
)
1257 free((void *) curr_pack
);
1258 if (index_name
== NULL
)
1259 free((void *) curr_index
);