5 #include "environment.h"
18 #include "streaming.h"
19 #include "thread-utils.h"
21 #include "pack-revindex.h"
22 #include "object-file.h"
23 #include "object-store-ll.h"
24 #include "oid-array.h"
25 #include "replace-object.h"
26 #include "promisor-remote.h"
30 static const char index_pack_usage
[] =
31 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--[no-]rev-index] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
34 struct pack_idx_entry idx
;
36 unsigned char hdr_size
;
38 signed char real_type
;
47 /* Initialized by make_base(). */
48 struct base_data
*base
;
49 struct object_entry
*obj
;
50 int ref_first
, ref_last
;
51 int ofs_first
, ofs_last
;
53 * Threads should increment retain_data if they are about to call
54 * patch_delta() using this struct's data as a base, and decrement this
55 * when they are done. While retain_data is nonzero, this struct's data
56 * will not be freed even if the delta base cache limit is exceeded.
60 * The number of direct children that have not been fully processed
61 * (entered work_head, entered done_head, left done_head). When this
62 * number reaches zero, this struct base_data can be freed.
64 int children_remaining
;
66 /* Not initialized by make_base(). */
67 struct list_head list
;
73 * Stack of struct base_data that have unprocessed children.
74 * threaded_second_pass() uses this as a source of work (the other being the
77 * Guarded by work_mutex.
79 static LIST_HEAD(work_head
);
82 * Stack of struct base_data that have children, all of whom have been
83 * processed or are being processed, and at least one child is being processed.
84 * These struct base_data must be kept around until the last child is
87 * Guarded by work_mutex.
89 static LIST_HEAD(done_head
);
92 * All threads share one delta base cache.
94 * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only
97 static size_t base_cache_used
;
98 static size_t base_cache_limit
;
100 struct thread_local
{
105 /* Remember to update object flag allocation in object.h */
106 #define FLAG_LINK (1u<<20)
107 #define FLAG_CHECKED (1u<<21)
109 struct ofs_delta_entry
{
114 struct ref_delta_entry
{
115 struct object_id oid
;
119 static struct object_entry
*objects
;
120 static struct object_stat
*obj_stat
;
121 static struct ofs_delta_entry
*ofs_deltas
;
122 static struct ref_delta_entry
*ref_deltas
;
123 static struct thread_local nothread_data
;
124 static int nr_objects
;
125 static int nr_ofs_deltas
;
126 static int nr_ref_deltas
;
127 static int ref_deltas_alloc
;
128 static int nr_resolved_deltas
;
129 static int nr_threads
;
131 static int from_stdin
;
133 static int do_fsck_object
;
134 static struct fsck_options fsck_options
= FSCK_OPTIONS_MISSING_GITMODULES
;
136 static const char *progress_title
;
137 static int show_resolving_progress
;
138 static int show_stat
;
139 static int check_self_contained_and_connected
;
141 static struct progress
*progress
;
143 /* We always read in 4kB chunks. */
144 static unsigned char input_buffer
[4096];
145 static unsigned int input_offset
, input_len
;
146 static off_t consumed_bytes
;
147 static off_t max_input_size
;
148 static unsigned deepest_delta
;
149 static git_hash_ctx input_ctx
;
150 static uint32_t input_crc32
;
151 static int input_fd
, output_fd
;
152 static const char *curr_pack
;
154 static struct thread_local
*thread_data
;
155 static int nr_dispatched
;
156 static int threads_active
;
158 static pthread_mutex_t read_mutex
;
159 #define read_lock() lock_mutex(&read_mutex)
160 #define read_unlock() unlock_mutex(&read_mutex)
162 static pthread_mutex_t counter_mutex
;
163 #define counter_lock() lock_mutex(&counter_mutex)
164 #define counter_unlock() unlock_mutex(&counter_mutex)
166 static pthread_mutex_t work_mutex
;
167 #define work_lock() lock_mutex(&work_mutex)
168 #define work_unlock() unlock_mutex(&work_mutex)
170 static pthread_mutex_t deepest_delta_mutex
;
171 #define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
172 #define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
174 static pthread_key_t key
;
176 static inline void lock_mutex(pthread_mutex_t
*mutex
)
179 pthread_mutex_lock(mutex
);
182 static inline void unlock_mutex(pthread_mutex_t
*mutex
)
185 pthread_mutex_unlock(mutex
);
189 * Mutex and conditional variable can't be statically-initialized on Windows.
191 static void init_thread(void)
194 init_recursive_mutex(&read_mutex
);
195 pthread_mutex_init(&counter_mutex
, NULL
);
196 pthread_mutex_init(&work_mutex
, NULL
);
198 pthread_mutex_init(&deepest_delta_mutex
, NULL
);
199 pthread_key_create(&key
, NULL
);
200 CALLOC_ARRAY(thread_data
, nr_threads
);
201 for (i
= 0; i
< nr_threads
; i
++) {
202 thread_data
[i
].pack_fd
= xopen(curr_pack
, O_RDONLY
);
208 static void cleanup_thread(void)
214 pthread_mutex_destroy(&read_mutex
);
215 pthread_mutex_destroy(&counter_mutex
);
216 pthread_mutex_destroy(&work_mutex
);
218 pthread_mutex_destroy(&deepest_delta_mutex
);
219 for (i
= 0; i
< nr_threads
; i
++)
220 close(thread_data
[i
].pack_fd
);
221 pthread_key_delete(key
);
225 static int mark_link(struct object
*obj
, enum object_type type
,
226 void *data
, struct fsck_options
*options
)
231 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
232 die(_("object type mismatch at %s"), oid_to_hex(&obj
->oid
));
234 obj
->flags
|= FLAG_LINK
;
238 /* The content of each linked object must have been checked
239 or it must be already present in the object database */
240 static unsigned check_object(struct object
*obj
)
245 if (!(obj
->flags
& FLAG_LINK
))
248 if (!(obj
->flags
& FLAG_CHECKED
)) {
250 int type
= oid_object_info(the_repository
, &obj
->oid
, &size
);
252 die(_("did not receive expected object %s"),
253 oid_to_hex(&obj
->oid
));
254 if (type
!= obj
->type
)
255 die(_("object %s: expected type %s, found %s"),
256 oid_to_hex(&obj
->oid
),
257 type_name(obj
->type
), type_name(type
));
258 obj
->flags
|= FLAG_CHECKED
;
265 static unsigned check_objects(void)
267 unsigned i
, max
, foreign_nr
= 0;
269 max
= get_max_object_index();
272 progress
= start_delayed_progress(_("Checking objects"), max
);
274 for (i
= 0; i
< max
; i
++) {
275 foreign_nr
+= check_object(get_indexed_object(i
));
276 display_progress(progress
, i
+ 1);
279 stop_progress(&progress
);
284 /* Discard current buffer used content. */
285 static void flush(void)
289 write_or_die(output_fd
, input_buffer
, input_offset
);
290 the_hash_algo
->update_fn(&input_ctx
, input_buffer
, input_offset
);
291 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
297 * Make sure at least "min" bytes are available in the buffer, and
298 * return the pointer to the buffer.
300 static void *fill(int min
)
302 if (min
<= input_len
)
303 return input_buffer
+ input_offset
;
304 if (min
> sizeof(input_buffer
))
305 die(Q_("cannot fill %d byte",
306 "cannot fill %d bytes",
311 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
312 sizeof(input_buffer
) - input_len
);
316 die_errno(_("read error on input"));
320 display_throughput(progress
, consumed_bytes
+ input_len
);
321 } while (input_len
< min
);
325 static void use(int bytes
)
327 if (bytes
> input_len
)
328 die(_("used more bytes than were available"));
329 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
331 input_offset
+= bytes
;
333 /* make sure off_t is sufficiently large not to wrap */
334 if (signed_add_overflows(consumed_bytes
, bytes
))
335 die(_("pack too large for current definition of off_t"));
336 consumed_bytes
+= bytes
;
337 if (max_input_size
&& consumed_bytes
> max_input_size
) {
338 struct strbuf size_limit
= STRBUF_INIT
;
339 strbuf_humanise_bytes(&size_limit
, max_input_size
);
340 die(_("pack exceeds maximum allowed size (%s)"),
345 static const char *open_pack_file(const char *pack_name
)
350 struct strbuf tmp_file
= STRBUF_INIT
;
351 output_fd
= odb_mkstemp(&tmp_file
,
352 "pack/tmp_pack_XXXXXX");
353 pack_name
= strbuf_detach(&tmp_file
, NULL
);
355 output_fd
= xopen(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
357 nothread_data
.pack_fd
= output_fd
;
359 input_fd
= xopen(pack_name
, O_RDONLY
);
361 nothread_data
.pack_fd
= input_fd
;
363 the_hash_algo
->init_fn(&input_ctx
);
367 static void parse_pack_header(void)
369 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
371 /* Header consistency check */
372 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
373 die(_("pack signature mismatch"));
374 if (!pack_version_ok(hdr
->hdr_version
))
375 die(_("pack version %"PRIu32
" unsupported"),
376 ntohl(hdr
->hdr_version
));
378 nr_objects
= ntohl(hdr
->hdr_entries
);
379 use(sizeof(struct pack_header
));
382 __attribute__((format (printf
, 2, 3)))
383 static NORETURN
void bad_object(off_t offset
, const char *format
, ...)
388 va_start(params
, format
);
389 vsnprintf(buf
, sizeof(buf
), format
, params
);
391 die(_("pack has bad object at offset %"PRIuMAX
": %s"),
392 (uintmax_t)offset
, buf
);
395 static inline struct thread_local
*get_thread_data(void)
399 return pthread_getspecific(key
);
400 assert(!threads_active
&&
401 "This should only be reached when all threads are gone");
403 return ¬hread_data
;
406 static void set_thread_data(struct thread_local
*data
)
409 pthread_setspecific(key
, data
);
412 static void free_base_data(struct base_data
*c
)
415 FREE_AND_NULL(c
->data
);
416 base_cache_used
-= c
->size
;
420 static void prune_base_data(struct base_data
*retain
)
422 struct list_head
*pos
;
424 if (base_cache_used
<= base_cache_limit
)
427 list_for_each_prev(pos
, &done_head
) {
428 struct base_data
*b
= list_entry(pos
, struct base_data
, list
);
429 if (b
->retain_data
|| b
== retain
)
433 if (base_cache_used
<= base_cache_limit
)
438 list_for_each_prev(pos
, &work_head
) {
439 struct base_data
*b
= list_entry(pos
, struct base_data
, list
);
440 if (b
->retain_data
|| b
== retain
)
444 if (base_cache_used
<= base_cache_limit
)
450 static int is_delta_type(enum object_type type
)
452 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
455 static void *unpack_entry_data(off_t offset
, unsigned long size
,
456 enum object_type type
, struct object_id
*oid
)
458 static char fixed_buf
[8192];
466 if (!is_delta_type(type
)) {
467 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, size
);
468 the_hash_algo
->init_fn(&c
);
469 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
472 if (type
== OBJ_BLOB
&& size
> big_file_threshold
)
475 buf
= xmallocz(size
);
477 memset(&stream
, 0, sizeof(stream
));
478 git_inflate_init(&stream
);
479 stream
.next_out
= buf
;
480 stream
.avail_out
= buf
== fixed_buf
? sizeof(fixed_buf
) : size
;
483 unsigned char *last_out
= stream
.next_out
;
484 stream
.next_in
= fill(1);
485 stream
.avail_in
= input_len
;
486 status
= git_inflate(&stream
, 0);
487 use(input_len
- stream
.avail_in
);
489 the_hash_algo
->update_fn(&c
, last_out
, stream
.next_out
- last_out
);
490 if (buf
== fixed_buf
) {
491 stream
.next_out
= buf
;
492 stream
.avail_out
= sizeof(fixed_buf
);
494 } while (status
== Z_OK
);
495 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
496 bad_object(offset
, _("inflate returned %d"), status
);
497 git_inflate_end(&stream
);
499 the_hash_algo
->final_oid_fn(oid
, &c
);
500 return buf
== fixed_buf
? NULL
: buf
;
503 static void *unpack_raw_entry(struct object_entry
*obj
,
505 struct object_id
*ref_oid
,
506 struct object_id
*oid
)
509 unsigned long size
, c
;
514 obj
->idx
.offset
= consumed_bytes
;
515 input_crc32
= crc32(0, NULL
, 0);
520 obj
->type
= (c
>> 4) & 7;
527 size
+= (c
& 0x7f) << shift
;
534 oidread(ref_oid
, fill(the_hash_algo
->rawsz
));
535 use(the_hash_algo
->rawsz
);
541 base_offset
= c
& 127;
544 if (!base_offset
|| MSB(base_offset
, 7))
545 bad_object(obj
->idx
.offset
, _("offset value overflow for delta base object"));
549 base_offset
= (base_offset
<< 7) + (c
& 127);
551 *ofs_offset
= obj
->idx
.offset
- base_offset
;
552 if (*ofs_offset
<= 0 || *ofs_offset
>= obj
->idx
.offset
)
553 bad_object(obj
->idx
.offset
, _("delta base offset is out of bound"));
561 bad_object(obj
->idx
.offset
, _("unknown object type %d"), obj
->type
);
563 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
565 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
, obj
->type
, oid
);
566 obj
->idx
.crc32
= input_crc32
;
570 static void *unpack_data(struct object_entry
*obj
,
571 int (*consume
)(const unsigned char *, unsigned long, void *),
574 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
575 off_t len
= obj
[1].idx
.offset
- from
;
576 unsigned char *data
, *inbuf
;
580 data
= xmallocz(consume
? 64*1024 : obj
->size
);
581 inbuf
= xmalloc((len
< 64*1024) ? (int)len
: 64*1024);
583 memset(&stream
, 0, sizeof(stream
));
584 git_inflate_init(&stream
);
585 stream
.next_out
= data
;
586 stream
.avail_out
= consume
? 64*1024 : obj
->size
;
589 ssize_t n
= (len
< 64*1024) ? (ssize_t
)len
: 64*1024;
590 n
= xpread(get_thread_data()->pack_fd
, inbuf
, n
, from
);
592 die_errno(_("cannot pread pack file"));
594 die(Q_("premature end of pack file, %"PRIuMAX
" byte missing",
595 "premature end of pack file, %"PRIuMAX
" bytes missing",
600 stream
.next_in
= inbuf
;
603 status
= git_inflate(&stream
, 0);
606 status
= git_inflate(&stream
, 0);
607 if (consume(data
, stream
.next_out
- data
, cb_data
)) {
612 stream
.next_out
= data
;
613 stream
.avail_out
= 64*1024;
614 } while (status
== Z_OK
&& stream
.avail_in
);
616 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
618 /* This has been inflated OK when first encountered, so... */
619 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
620 die(_("serious inflate inconsistency"));
622 git_inflate_end(&stream
);
630 static void *get_data_from_pack(struct object_entry
*obj
)
632 return unpack_data(obj
, NULL
, NULL
);
635 static int compare_ofs_delta_bases(off_t offset1
, off_t offset2
,
636 enum object_type type1
,
637 enum object_type type2
)
639 int cmp
= type1
- type2
;
642 return offset1
< offset2
? -1 :
643 offset1
> offset2
? 1 :
647 static int find_ofs_delta(const off_t offset
)
649 int first
= 0, last
= nr_ofs_deltas
;
651 while (first
< last
) {
652 int next
= first
+ (last
- first
) / 2;
653 struct ofs_delta_entry
*delta
= &ofs_deltas
[next
];
656 cmp
= compare_ofs_delta_bases(offset
, delta
->offset
,
658 objects
[delta
->obj_no
].type
);
670 static void find_ofs_delta_children(off_t offset
,
671 int *first_index
, int *last_index
)
673 int first
= find_ofs_delta(offset
);
675 int end
= nr_ofs_deltas
- 1;
682 while (first
> 0 && ofs_deltas
[first
- 1].offset
== offset
)
684 while (last
< end
&& ofs_deltas
[last
+ 1].offset
== offset
)
686 *first_index
= first
;
690 static int compare_ref_delta_bases(const struct object_id
*oid1
,
691 const struct object_id
*oid2
,
692 enum object_type type1
,
693 enum object_type type2
)
695 int cmp
= type1
- type2
;
698 return oidcmp(oid1
, oid2
);
701 static int find_ref_delta(const struct object_id
*oid
)
703 int first
= 0, last
= nr_ref_deltas
;
705 while (first
< last
) {
706 int next
= first
+ (last
- first
) / 2;
707 struct ref_delta_entry
*delta
= &ref_deltas
[next
];
710 cmp
= compare_ref_delta_bases(oid
, &delta
->oid
,
712 objects
[delta
->obj_no
].type
);
724 static void find_ref_delta_children(const struct object_id
*oid
,
725 int *first_index
, int *last_index
)
727 int first
= find_ref_delta(oid
);
729 int end
= nr_ref_deltas
- 1;
736 while (first
> 0 && oideq(&ref_deltas
[first
- 1].oid
, oid
))
738 while (last
< end
&& oideq(&ref_deltas
[last
+ 1].oid
, oid
))
740 *first_index
= first
;
744 struct compare_data
{
745 struct object_entry
*entry
;
746 struct git_istream
*st
;
748 unsigned long buf_size
;
751 static int compare_objects(const unsigned char *buf
, unsigned long size
,
754 struct compare_data
*data
= cb_data
;
756 if (data
->buf_size
< size
) {
758 data
->buf
= xmalloc(size
);
759 data
->buf_size
= size
;
763 ssize_t len
= read_istream(data
->st
, data
->buf
, size
);
765 die(_("SHA1 COLLISION FOUND WITH %s !"),
766 oid_to_hex(&data
->entry
->idx
.oid
));
768 die(_("unable to read %s"),
769 oid_to_hex(&data
->entry
->idx
.oid
));
770 if (memcmp(buf
, data
->buf
, len
))
771 die(_("SHA1 COLLISION FOUND WITH %s !"),
772 oid_to_hex(&data
->entry
->idx
.oid
));
779 static int check_collison(struct object_entry
*entry
)
781 struct compare_data data
;
782 enum object_type type
;
785 if (entry
->size
<= big_file_threshold
|| entry
->type
!= OBJ_BLOB
)
788 memset(&data
, 0, sizeof(data
));
790 data
.st
= open_istream(the_repository
, &entry
->idx
.oid
, &type
, &size
,
794 if (size
!= entry
->size
|| type
!= entry
->type
)
795 die(_("SHA1 COLLISION FOUND WITH %s !"),
796 oid_to_hex(&entry
->idx
.oid
));
797 unpack_data(entry
, compare_objects
, &data
);
798 close_istream(data
.st
);
803 static void sha1_object(const void *data
, struct object_entry
*obj_entry
,
804 unsigned long size
, enum object_type type
,
805 const struct object_id
*oid
)
807 void *new_data
= NULL
;
808 int collision_test_needed
= 0;
810 assert(data
|| obj_entry
);
812 if (startup_info
->have_repository
) {
814 collision_test_needed
=
815 repo_has_object_file_with_flags(the_repository
, oid
,
820 if (collision_test_needed
&& !data
) {
822 if (!check_collison(obj_entry
))
823 collision_test_needed
= 0;
826 if (collision_test_needed
) {
828 enum object_type has_type
;
829 unsigned long has_size
;
831 has_type
= oid_object_info(the_repository
, oid
, &has_size
);
833 die(_("cannot read existing object info %s"), oid_to_hex(oid
));
834 if (has_type
!= type
|| has_size
!= size
)
835 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid
));
836 has_data
= repo_read_object_file(the_repository
, oid
,
837 &has_type
, &has_size
);
840 data
= new_data
= get_data_from_pack(obj_entry
);
842 die(_("cannot read existing object %s"), oid_to_hex(oid
));
843 if (size
!= has_size
|| type
!= has_type
||
844 memcmp(data
, has_data
, size
) != 0)
845 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid
));
849 if (strict
|| do_fsck_object
) {
851 if (type
== OBJ_BLOB
) {
852 struct blob
*blob
= lookup_blob(the_repository
, oid
);
854 blob
->object
.flags
|= FLAG_CHECKED
;
856 die(_("invalid blob object %s"), oid_to_hex(oid
));
857 if (do_fsck_object
&&
858 fsck_object(&blob
->object
, (void *)data
, size
, &fsck_options
))
859 die(_("fsck error in packed object"));
863 void *buf
= (void *) data
;
865 assert(data
&& "data can only be NULL for large _blobs_");
868 * we do not need to free the memory here, as the
869 * buf is deleted by the caller.
871 obj
= parse_object_buffer(the_repository
, oid
, type
,
875 die(_("invalid %s"), type_name(type
));
876 if (do_fsck_object
&&
877 fsck_object(obj
, buf
, size
, &fsck_options
))
878 die(_("fsck error in packed object"));
879 if (strict
&& fsck_walk(obj
, NULL
, &fsck_options
))
880 die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj
->oid
));
882 if (obj
->type
== OBJ_TREE
) {
883 struct tree
*item
= (struct tree
*) obj
;
887 if (obj
->type
== OBJ_COMMIT
) {
888 struct commit
*commit
= (struct commit
*) obj
;
889 if (detach_commit_buffer(commit
, NULL
) != data
)
890 BUG("parse_object_buffer transmogrified our buffer");
892 obj
->flags
|= FLAG_CHECKED
;
901 * Ensure that this node has been reconstructed and return its contents.
903 * In the typical and best case, this node would already be reconstructed
904 * (through the invocation to resolve_delta() in threaded_second_pass()) and it
905 * would not be pruned. However, if pruning of this node was necessary due to
906 * reaching delta_base_cache_limit, this function will find the closest
907 * ancestor with reconstructed data that has not been pruned (or if there is
908 * none, the ultimate base object), and reconstruct each node in the delta
909 * chain in order to generate the reconstructed data for this node.
911 static void *get_base_data(struct base_data
*c
)
914 struct object_entry
*obj
= c
->obj
;
915 struct base_data
**delta
= NULL
;
916 int delta_nr
= 0, delta_alloc
= 0;
918 while (is_delta_type(c
->obj
->type
) && !c
->data
) {
919 ALLOC_GROW(delta
, delta_nr
+ 1, delta_alloc
);
920 delta
[delta_nr
++] = c
;
924 c
->data
= get_data_from_pack(obj
);
926 base_cache_used
+= c
->size
;
929 for (; delta_nr
> 0; delta_nr
--) {
931 c
= delta
[delta_nr
- 1];
933 base
= get_base_data(c
->base
);
934 raw
= get_data_from_pack(obj
);
935 c
->data
= patch_delta(
941 bad_object(obj
->idx
.offset
, _("failed to apply delta"));
942 base_cache_used
+= c
->size
;
950 static struct base_data
*make_base(struct object_entry
*obj
,
951 struct base_data
*parent
)
953 struct base_data
*base
= xcalloc(1, sizeof(struct base_data
));
956 find_ref_delta_children(&obj
->idx
.oid
,
957 &base
->ref_first
, &base
->ref_last
);
958 find_ofs_delta_children(obj
->idx
.offset
,
959 &base
->ofs_first
, &base
->ofs_last
);
960 base
->children_remaining
= base
->ref_last
- base
->ref_first
+
961 base
->ofs_last
- base
->ofs_first
+ 2;
965 static struct base_data
*resolve_delta(struct object_entry
*delta_obj
,
966 struct base_data
*base
)
968 void *delta_data
, *result_data
;
969 struct base_data
*result
;
970 unsigned long result_size
;
973 int i
= delta_obj
- objects
;
974 int j
= base
->obj
- objects
;
975 obj_stat
[i
].delta_depth
= obj_stat
[j
].delta_depth
+ 1;
976 deepest_delta_lock();
977 if (deepest_delta
< obj_stat
[i
].delta_depth
)
978 deepest_delta
= obj_stat
[i
].delta_depth
;
979 deepest_delta_unlock();
980 obj_stat
[i
].base_object_no
= j
;
982 delta_data
= get_data_from_pack(delta_obj
);
984 result_data
= patch_delta(base
->data
, base
->size
,
985 delta_data
, delta_obj
->size
, &result_size
);
988 bad_object(delta_obj
->idx
.offset
, _("failed to apply delta"));
989 hash_object_file(the_hash_algo
, result_data
, result_size
,
990 delta_obj
->real_type
, &delta_obj
->idx
.oid
);
991 sha1_object(result_data
, NULL
, result_size
, delta_obj
->real_type
,
992 &delta_obj
->idx
.oid
);
994 result
= make_base(delta_obj
, base
);
995 result
->data
= result_data
;
996 result
->size
= result_size
;
999 nr_resolved_deltas
++;
1005 static int compare_ofs_delta_entry(const void *a
, const void *b
)
1007 const struct ofs_delta_entry
*delta_a
= a
;
1008 const struct ofs_delta_entry
*delta_b
= b
;
1010 return delta_a
->offset
< delta_b
->offset
? -1 :
1011 delta_a
->offset
> delta_b
->offset
? 1 :
1015 static int compare_ref_delta_entry(const void *a
, const void *b
)
1017 const struct ref_delta_entry
*delta_a
= a
;
1018 const struct ref_delta_entry
*delta_b
= b
;
1020 return oidcmp(&delta_a
->oid
, &delta_b
->oid
);
1023 static void *threaded_second_pass(void *data
)
1026 set_thread_data(data
);
1028 struct base_data
*parent
= NULL
;
1029 struct object_entry
*child_obj
;
1030 struct base_data
*child
;
1033 display_progress(progress
, nr_resolved_deltas
);
1037 if (list_empty(&work_head
)) {
1039 * Take an object from the object array.
1041 while (nr_dispatched
< nr_objects
&&
1042 is_delta_type(objects
[nr_dispatched
].type
))
1044 if (nr_dispatched
>= nr_objects
) {
1048 child_obj
= &objects
[nr_dispatched
++];
1051 * Peek at the top of the stack, and take a child from
1054 parent
= list_first_entry(&work_head
, struct base_data
,
1057 if (parent
->ref_first
<= parent
->ref_last
) {
1058 int offset
= ref_deltas
[parent
->ref_first
++].obj_no
;
1059 child_obj
= objects
+ offset
;
1060 if (child_obj
->real_type
!= OBJ_REF_DELTA
)
1061 die("REF_DELTA at offset %"PRIuMAX
" already resolved (duplicate base %s?)",
1062 (uintmax_t) child_obj
->idx
.offset
,
1063 oid_to_hex(&parent
->obj
->idx
.oid
));
1064 child_obj
->real_type
= parent
->obj
->real_type
;
1066 child_obj
= objects
+
1067 ofs_deltas
[parent
->ofs_first
++].obj_no
;
1068 assert(child_obj
->real_type
== OBJ_OFS_DELTA
);
1069 child_obj
->real_type
= parent
->obj
->real_type
;
1072 if (parent
->ref_first
> parent
->ref_last
&&
1073 parent
->ofs_first
> parent
->ofs_last
) {
1075 * This parent has run out of children, so move
1078 list_del(&parent
->list
);
1079 list_add(&parent
->list
, &done_head
);
1083 * Ensure that the parent has data, since we will need
1086 * NEEDSWORK: If parent data needs to be reloaded, this
1087 * prolongs the time that the current thread spends in
1088 * the mutex. A mitigating factor is that parent data
1089 * needs to be reloaded only if the delta base cache
1090 * limit is exceeded, so in the typical case, this does
1093 get_base_data(parent
);
1094 parent
->retain_data
++;
1099 child
= resolve_delta(child_obj
, parent
);
1100 if (!child
->children_remaining
)
1101 FREE_AND_NULL(child
->data
);
1103 child
= make_base(child_obj
, NULL
);
1104 if (child
->children_remaining
) {
1106 * Since this child has its own delta children,
1107 * we will need this data in the future.
1108 * Inflate now so that future iterations will
1109 * have access to this object's data while
1110 * outside the work mutex.
1112 child
->data
= get_data_from_pack(child_obj
);
1113 child
->size
= child_obj
->size
;
1119 parent
->retain_data
--;
1122 * This child has its own children, so add it to
1125 list_add(&child
->list
, &work_head
);
1126 base_cache_used
+= child
->size
;
1127 prune_base_data(NULL
);
1128 free_base_data(child
);
1131 * This child does not have its own children. It may be
1132 * the last descendant of its ancestors; free those
1135 struct base_data
*p
= parent
;
1138 struct base_data
*next_p
;
1140 p
->children_remaining
--;
1141 if (p
->children_remaining
)
1151 FREE_AND_NULL(child
);
1160 * - find locations of all objects;
1161 * - calculate SHA1 of all non-delta objects;
1162 * - remember base (SHA1 or offset) for all deltas.
1164 static void parse_pack_objects(unsigned char *hash
)
1166 int i
, nr_delays
= 0;
1167 struct ofs_delta_entry
*ofs_delta
= ofs_deltas
;
1168 struct object_id ref_delta_oid
;
1172 progress
= start_progress(
1173 progress_title
? progress_title
:
1174 from_stdin
? _("Receiving objects") : _("Indexing objects"),
1176 for (i
= 0; i
< nr_objects
; i
++) {
1177 struct object_entry
*obj
= &objects
[i
];
1178 void *data
= unpack_raw_entry(obj
, &ofs_delta
->offset
,
1181 obj
->real_type
= obj
->type
;
1182 if (obj
->type
== OBJ_OFS_DELTA
) {
1184 ofs_delta
->obj_no
= i
;
1186 } else if (obj
->type
== OBJ_REF_DELTA
) {
1187 ALLOC_GROW(ref_deltas
, nr_ref_deltas
+ 1, ref_deltas_alloc
);
1188 oidcpy(&ref_deltas
[nr_ref_deltas
].oid
, &ref_delta_oid
);
1189 ref_deltas
[nr_ref_deltas
].obj_no
= i
;
1192 /* large blobs, check later */
1193 obj
->real_type
= OBJ_BAD
;
1196 sha1_object(data
, NULL
, obj
->size
, obj
->type
,
1199 display_progress(progress
, i
+1);
1201 objects
[i
].idx
.offset
= consumed_bytes
;
1202 stop_progress(&progress
);
1204 /* Check pack integrity */
1206 the_hash_algo
->final_fn(hash
, &input_ctx
);
1207 if (!hasheq(fill(the_hash_algo
->rawsz
), hash
))
1208 die(_("pack is corrupted (SHA1 mismatch)"));
1209 use(the_hash_algo
->rawsz
);
1211 /* If input_fd is a file, we should have reached its end now. */
1212 if (fstat(input_fd
, &st
))
1213 die_errno(_("cannot fstat packfile"));
1214 if (S_ISREG(st
.st_mode
) &&
1215 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
1216 die(_("pack has junk at the end"));
1218 for (i
= 0; i
< nr_objects
; i
++) {
1219 struct object_entry
*obj
= &objects
[i
];
1220 if (obj
->real_type
!= OBJ_BAD
)
1222 obj
->real_type
= obj
->type
;
1223 sha1_object(NULL
, obj
, obj
->size
, obj
->type
,
1228 die(_("confusion beyond insanity in parse_pack_objects()"));
1233 * - for all non-delta objects, look if it is used as a base for
1235 * - if used as a base, uncompress the object and apply all deltas,
1236 * recursively checking if the resulting object is used as a base
1237 * for some more deltas.
1239 static void resolve_deltas(void)
1243 if (!nr_ofs_deltas
&& !nr_ref_deltas
)
1246 /* Sort deltas by base SHA1/offset for fast searching */
1247 QSORT(ofs_deltas
, nr_ofs_deltas
, compare_ofs_delta_entry
);
1248 QSORT(ref_deltas
, nr_ref_deltas
, compare_ref_delta_entry
);
1250 if (verbose
|| show_resolving_progress
)
1251 progress
= start_progress(_("Resolving deltas"),
1252 nr_ref_deltas
+ nr_ofs_deltas
);
1255 base_cache_limit
= delta_base_cache_limit
* nr_threads
;
1256 if (nr_threads
> 1 || getenv("GIT_FORCE_THREADS")) {
1258 for (i
= 0; i
< nr_threads
; i
++) {
1259 int ret
= pthread_create(&thread_data
[i
].thread
, NULL
,
1260 threaded_second_pass
, thread_data
+ i
);
1262 die(_("unable to create thread: %s"),
1265 for (i
= 0; i
< nr_threads
; i
++)
1266 pthread_join(thread_data
[i
].thread
, NULL
);
1270 threaded_second_pass(¬hread_data
);
1275 * - append objects to convert thin pack to full pack if required
1276 * - write the final pack hash
1278 static void fix_unresolved_deltas(struct hashfile
*f
);
1279 static void conclude_pack(int fix_thin_pack
, const char *curr_pack
, unsigned char *pack_hash
)
1281 if (nr_ref_deltas
+ nr_ofs_deltas
== nr_resolved_deltas
) {
1282 stop_progress(&progress
);
1283 /* Flush remaining pack final hash. */
1288 if (fix_thin_pack
) {
1290 unsigned char read_hash
[GIT_MAX_RAWSZ
], tail_hash
[GIT_MAX_RAWSZ
];
1291 struct strbuf msg
= STRBUF_INIT
;
1292 int nr_unresolved
= nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
;
1293 int nr_objects_initial
= nr_objects
;
1294 if (nr_unresolved
<= 0)
1295 die(_("confusion beyond insanity"));
1296 REALLOC_ARRAY(objects
, nr_objects
+ nr_unresolved
+ 1);
1297 memset(objects
+ nr_objects
+ 1, 0,
1298 nr_unresolved
* sizeof(*objects
));
1299 f
= hashfd(output_fd
, curr_pack
);
1300 fix_unresolved_deltas(f
);
1301 strbuf_addf(&msg
, Q_("completed with %d local object",
1302 "completed with %d local objects",
1303 nr_objects
- nr_objects_initial
),
1304 nr_objects
- nr_objects_initial
);
1305 stop_progress_msg(&progress
, msg
.buf
);
1306 strbuf_release(&msg
);
1307 finalize_hashfile(f
, tail_hash
, FSYNC_COMPONENT_PACK
, 0);
1308 hashcpy(read_hash
, pack_hash
);
1309 fixup_pack_header_footer(output_fd
, pack_hash
,
1310 curr_pack
, nr_objects
,
1311 read_hash
, consumed_bytes
-the_hash_algo
->rawsz
);
1312 if (!hasheq(read_hash
, tail_hash
))
1313 die(_("Unexpected tail checksum for %s "
1314 "(disk corruption?)"), curr_pack
);
1316 if (nr_ofs_deltas
+ nr_ref_deltas
!= nr_resolved_deltas
)
1317 die(Q_("pack has %d unresolved delta",
1318 "pack has %d unresolved deltas",
1319 nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
),
1320 nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
);
1323 static int write_compressed(struct hashfile
*f
, void *in
, unsigned int size
)
1327 unsigned char outbuf
[4096];
1329 git_deflate_init(&stream
, zlib_compression_level
);
1330 stream
.next_in
= in
;
1331 stream
.avail_in
= size
;
1334 stream
.next_out
= outbuf
;
1335 stream
.avail_out
= sizeof(outbuf
);
1336 status
= git_deflate(&stream
, Z_FINISH
);
1337 hashwrite(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
1338 } while (status
== Z_OK
);
1340 if (status
!= Z_STREAM_END
)
1341 die(_("unable to deflate appended object (%d)"), status
);
1342 size
= stream
.total_out
;
1343 git_deflate_end(&stream
);
1347 static struct object_entry
*append_obj_to_pack(struct hashfile
*f
,
1348 const unsigned char *sha1
, void *buf
,
1349 unsigned long size
, enum object_type type
)
1351 struct object_entry
*obj
= &objects
[nr_objects
++];
1352 unsigned char header
[10];
1353 unsigned long s
= size
;
1355 unsigned char c
= (type
<< 4) | (s
& 15);
1358 header
[n
++] = c
| 0x80;
1364 hashwrite(f
, header
, n
);
1366 obj
[0].hdr_size
= n
;
1368 obj
[0].real_type
= type
;
1369 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
1370 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
1371 obj
[0].idx
.crc32
= crc32_end(f
);
1373 oidread(&obj
->idx
.oid
, sha1
);
1377 static int delta_pos_compare(const void *_a
, const void *_b
)
1379 struct ref_delta_entry
*a
= *(struct ref_delta_entry
**)_a
;
1380 struct ref_delta_entry
*b
= *(struct ref_delta_entry
**)_b
;
1381 return a
->obj_no
- b
->obj_no
;
1384 static void fix_unresolved_deltas(struct hashfile
*f
)
1386 struct ref_delta_entry
**sorted_by_pos
;
1390 * Since many unresolved deltas may well be themselves base objects
1391 * for more unresolved deltas, we really want to include the
1392 * smallest number of base objects that would cover as much delta
1393 * as possible by picking the
1394 * trunc deltas first, allowing for other deltas to resolve without
1395 * additional base objects. Since most base objects are to be found
1396 * before deltas depending on them, a good heuristic is to start
1397 * resolving deltas in the same order as their position in the pack.
1399 ALLOC_ARRAY(sorted_by_pos
, nr_ref_deltas
);
1400 for (i
= 0; i
< nr_ref_deltas
; i
++)
1401 sorted_by_pos
[i
] = &ref_deltas
[i
];
1402 QSORT(sorted_by_pos
, nr_ref_deltas
, delta_pos_compare
);
1404 if (repo_has_promisor_remote(the_repository
)) {
1406 * Prefetch the delta bases.
1408 struct oid_array to_fetch
= OID_ARRAY_INIT
;
1409 for (i
= 0; i
< nr_ref_deltas
; i
++) {
1410 struct ref_delta_entry
*d
= sorted_by_pos
[i
];
1411 if (!oid_object_info_extended(the_repository
, &d
->oid
,
1413 OBJECT_INFO_FOR_PREFETCH
))
1415 oid_array_append(&to_fetch
, &d
->oid
);
1417 promisor_remote_get_direct(the_repository
,
1418 to_fetch
.oid
, to_fetch
.nr
);
1419 oid_array_clear(&to_fetch
);
1422 for (i
= 0; i
< nr_ref_deltas
; i
++) {
1423 struct ref_delta_entry
*d
= sorted_by_pos
[i
];
1424 enum object_type type
;
1428 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
1430 data
= repo_read_object_file(the_repository
, &d
->oid
, &type
,
1435 if (check_object_signature(the_repository
, &d
->oid
, data
, size
,
1437 die(_("local object %s is corrupt"), oid_to_hex(&d
->oid
));
1440 * Add this as an object to the objects array and call
1441 * threaded_second_pass() (which will pick up the added
1444 append_obj_to_pack(f
, d
->oid
.hash
, data
, size
, type
);
1446 threaded_second_pass(NULL
);
1448 display_progress(progress
, nr_resolved_deltas
);
1450 free(sorted_by_pos
);
1453 static const char *derive_filename(const char *pack_name
, const char *strip
,
1454 const char *suffix
, struct strbuf
*buf
)
1457 if (!strip_suffix(pack_name
, strip
, &len
) || !len
||
1458 pack_name
[len
- 1] != '.')
1459 die(_("packfile name '%s' does not end with '.%s'"),
1461 strbuf_add(buf
, pack_name
, len
);
1462 strbuf_addstr(buf
, suffix
);
1466 static void write_special_file(const char *suffix
, const char *msg
,
1467 const char *pack_name
, const unsigned char *hash
,
1468 const char **report
)
1470 struct strbuf name_buf
= STRBUF_INIT
;
1471 const char *filename
;
1473 int msg_len
= strlen(msg
);
1476 filename
= derive_filename(pack_name
, "pack", suffix
, &name_buf
);
1478 filename
= odb_pack_name(&name_buf
, hash
, suffix
);
1480 fd
= odb_pack_keep(filename
);
1482 if (errno
!= EEXIST
)
1483 die_errno(_("cannot write %s file '%s'"),
1487 write_or_die(fd
, msg
, msg_len
);
1488 write_or_die(fd
, "\n", 1);
1491 die_errno(_("cannot close written %s file '%s'"),
1496 strbuf_release(&name_buf
);
1499 static void rename_tmp_packfile(const char **final_name
,
1500 const char *curr_name
,
1501 struct strbuf
*name
, unsigned char *hash
,
1502 const char *ext
, int make_read_only_if_same
)
1504 if (*final_name
!= curr_name
) {
1506 *final_name
= odb_pack_name(name
, hash
, ext
);
1507 if (finalize_object_file(curr_name
, *final_name
))
1508 die(_("unable to rename temporary '*.%s' file to '%s'"),
1510 } else if (make_read_only_if_same
) {
1511 chmod(*final_name
, 0444);
1515 static void final(const char *final_pack_name
, const char *curr_pack_name
,
1516 const char *final_index_name
, const char *curr_index_name
,
1517 const char *final_rev_index_name
, const char *curr_rev_index_name
,
1518 const char *keep_msg
, const char *promisor_msg
,
1519 unsigned char *hash
)
1521 const char *report
= "pack";
1522 struct strbuf pack_name
= STRBUF_INIT
;
1523 struct strbuf index_name
= STRBUF_INIT
;
1524 struct strbuf rev_index_name
= STRBUF_INIT
;
1530 fsync_component_or_die(FSYNC_COMPONENT_PACK
, output_fd
, curr_pack_name
);
1531 err
= close(output_fd
);
1533 die_errno(_("error while closing pack file"));
1537 write_special_file("keep", keep_msg
, final_pack_name
, hash
,
1540 write_special_file("promisor", promisor_msg
, final_pack_name
,
1543 rename_tmp_packfile(&final_pack_name
, curr_pack_name
, &pack_name
,
1544 hash
, "pack", from_stdin
);
1545 if (curr_rev_index_name
)
1546 rename_tmp_packfile(&final_rev_index_name
, curr_rev_index_name
,
1547 &rev_index_name
, hash
, "rev", 1);
1548 rename_tmp_packfile(&final_index_name
, curr_index_name
, &index_name
,
1551 if (do_fsck_object
) {
1552 struct packed_git
*p
;
1553 p
= add_packed_git(final_index_name
, strlen(final_index_name
), 0);
1555 install_packed_git(the_repository
, p
);
1559 printf("%s\n", hash_to_hex(hash
));
1561 struct strbuf buf
= STRBUF_INIT
;
1563 strbuf_addf(&buf
, "%s\t%s\n", report
, hash_to_hex(hash
));
1564 write_or_die(1, buf
.buf
, buf
.len
);
1565 strbuf_release(&buf
);
1568 * Let's just mimic git-unpack-objects here and write
1569 * the last part of the input buffer to stdout.
1572 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
1576 input_offset
+= err
;
1580 strbuf_release(&rev_index_name
);
1581 strbuf_release(&index_name
);
1582 strbuf_release(&pack_name
);
1585 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
1587 struct pack_idx_option
*opts
= cb
;
1589 if (!strcmp(k
, "pack.indexversion")) {
1590 opts
->version
= git_config_int(k
, v
);
1591 if (opts
->version
> 2)
1592 die(_("bad pack.indexVersion=%"PRIu32
), opts
->version
);
1595 if (!strcmp(k
, "pack.threads")) {
1596 nr_threads
= git_config_int(k
, v
);
1598 die(_("invalid number of threads specified (%d)"),
1600 if (!HAVE_THREADS
&& nr_threads
!= 1) {
1601 warning(_("no threads support, ignoring %s"), k
);
1606 if (!strcmp(k
, "pack.writereverseindex")) {
1607 if (git_config_bool(k
, v
))
1608 opts
->flags
|= WRITE_REV
;
1610 opts
->flags
&= ~WRITE_REV
;
1612 return git_default_config(k
, v
, cb
);
1615 static int cmp_uint32(const void *a_
, const void *b_
)
1617 uint32_t a
= *((uint32_t *)a_
);
1618 uint32_t b
= *((uint32_t *)b_
);
1620 return (a
< b
) ? -1 : (a
!= b
);
1623 static void read_v2_anomalous_offsets(struct packed_git
*p
,
1624 struct pack_idx_option
*opts
)
1626 const uint32_t *idx1
, *idx2
;
1629 /* The address of the 4-byte offset table */
1630 idx1
= (((const uint32_t *)((const uint8_t *)p
->index_data
+ p
->crc_offset
))
1631 + (size_t)p
->num_objects
/* CRC32 table */
1634 /* The address of the 8-byte offset table */
1635 idx2
= idx1
+ p
->num_objects
;
1637 for (i
= 0; i
< p
->num_objects
; i
++) {
1638 uint32_t off
= ntohl(idx1
[i
]);
1639 if (!(off
& 0x80000000))
1641 off
= off
& 0x7fffffff;
1642 check_pack_index_ptr(p
, &idx2
[off
* 2]);
1646 * The real offset is ntohl(idx2[off * 2]) in high 4
1647 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1648 * octets. But idx2[off * 2] is Zero!!!
1650 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
1651 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
1654 QSORT(opts
->anomaly
, opts
->anomaly_nr
, cmp_uint32
);
1657 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
1659 struct packed_git
*p
= add_packed_git(pack_name
, strlen(pack_name
), 1);
1662 die(_("Cannot open existing pack file '%s'"), pack_name
);
1663 if (open_pack_index(p
))
1664 die(_("Cannot open existing pack idx file for '%s'"), pack_name
);
1666 /* Read the attributes from the existing idx file */
1667 opts
->version
= p
->index_version
;
1669 if (opts
->version
== 2)
1670 read_v2_anomalous_offsets(p
, opts
);
1673 * Get rid of the idx file as we do not need it anymore.
1674 * NEEDSWORK: extract this bit from free_pack_by_name() in
1675 * object-file.c, perhaps? It shouldn't matter very much as we
1676 * know we haven't installed this pack (hence we never have
1677 * read anything from it).
1679 close_pack_index(p
);
1683 static void show_pack_info(int stat_only
)
1685 int i
, baseobjects
= nr_objects
- nr_ref_deltas
- nr_ofs_deltas
;
1686 unsigned long *chain_histogram
= NULL
;
1689 CALLOC_ARRAY(chain_histogram
, deepest_delta
);
1691 for (i
= 0; i
< nr_objects
; i
++) {
1692 struct object_entry
*obj
= &objects
[i
];
1694 if (is_delta_type(obj
->type
))
1695 chain_histogram
[obj_stat
[i
].delta_depth
- 1]++;
1698 printf("%s %-6s %"PRIuMAX
" %"PRIuMAX
" %"PRIuMAX
,
1699 oid_to_hex(&obj
->idx
.oid
),
1700 type_name(obj
->real_type
), (uintmax_t)obj
->size
,
1701 (uintmax_t)(obj
[1].idx
.offset
- obj
->idx
.offset
),
1702 (uintmax_t)obj
->idx
.offset
);
1703 if (is_delta_type(obj
->type
)) {
1704 struct object_entry
*bobj
= &objects
[obj_stat
[i
].base_object_no
];
1705 printf(" %u %s", obj_stat
[i
].delta_depth
,
1706 oid_to_hex(&bobj
->idx
.oid
));
1712 printf_ln(Q_("non delta: %d object",
1713 "non delta: %d objects",
1716 for (i
= 0; i
< deepest_delta
; i
++) {
1717 if (!chain_histogram
[i
])
1719 printf_ln(Q_("chain length = %d: %lu object",
1720 "chain length = %d: %lu objects",
1721 chain_histogram
[i
]),
1723 chain_histogram
[i
]);
1725 free(chain_histogram
);
1728 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
1730 int i
, fix_thin_pack
= 0, verify
= 0, stat_only
= 0, rev_index
;
1731 const char *curr_index
;
1732 const char *curr_rev_index
= NULL
;
1733 const char *index_name
= NULL
, *pack_name
= NULL
, *rev_index_name
= NULL
;
1734 const char *keep_msg
= NULL
;
1735 const char *promisor_msg
= NULL
;
1736 struct strbuf index_name_buf
= STRBUF_INIT
;
1737 struct strbuf rev_index_name_buf
= STRBUF_INIT
;
1738 struct pack_idx_entry
**idx_objects
;
1739 struct pack_idx_option opts
;
1740 unsigned char pack_hash
[GIT_MAX_RAWSZ
];
1741 unsigned foreign_nr
= 1; /* zero is a "good" value, assume bad */
1742 int report_end_of_input
= 0;
1746 * index-pack never needs to fetch missing objects except when
1747 * REF_DELTA bases are missing (which are explicitly handled). It only
1748 * accesses the repo to do hash collision checks and to check which
1749 * REF_DELTA bases need to be fetched.
1751 fetch_if_missing
= 0;
1753 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1754 usage(index_pack_usage
);
1756 disable_replace_refs();
1757 fsck_options
.walk
= mark_link
;
1759 reset_pack_idx_option(&opts
);
1760 opts
.flags
|= WRITE_REV
;
1761 git_config(git_index_pack_config
, &opts
);
1762 if (prefix
&& chdir(prefix
))
1763 die(_("Cannot come back to cwd"));
1765 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX
, 0))
1768 rev_index
= !!(opts
.flags
& (WRITE_REV_VERIFY
| WRITE_REV
));
1770 for (i
= 1; i
< argc
; i
++) {
1771 const char *arg
= argv
[i
];
1774 if (!strcmp(arg
, "--stdin")) {
1776 } else if (!strcmp(arg
, "--fix-thin")) {
1778 } else if (skip_to_optional_arg(arg
, "--strict", &arg
)) {
1781 fsck_set_msg_types(&fsck_options
, arg
);
1782 } else if (!strcmp(arg
, "--check-self-contained-and-connected")) {
1784 check_self_contained_and_connected
= 1;
1785 } else if (!strcmp(arg
, "--fsck-objects")) {
1787 } else if (!strcmp(arg
, "--verify")) {
1789 } else if (!strcmp(arg
, "--verify-stat")) {
1792 } else if (!strcmp(arg
, "--verify-stat-only")) {
1796 } else if (skip_to_optional_arg(arg
, "--keep", &keep_msg
)) {
1797 ; /* nothing to do */
1798 } else if (skip_to_optional_arg(arg
, "--promisor", &promisor_msg
)) {
1799 ; /* already parsed */
1800 } else if (starts_with(arg
, "--threads=")) {
1802 nr_threads
= strtoul(arg
+10, &end
, 0);
1803 if (!arg
[10] || *end
|| nr_threads
< 0)
1804 usage(index_pack_usage
);
1805 if (!HAVE_THREADS
&& nr_threads
!= 1) {
1806 warning(_("no threads support, ignoring %s"), arg
);
1809 } else if (starts_with(arg
, "--pack_header=")) {
1810 struct pack_header
*hdr
;
1813 hdr
= (struct pack_header
*)input_buffer
;
1814 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
1815 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
1817 die(_("bad %s"), arg
);
1818 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
1820 die(_("bad %s"), arg
);
1821 input_len
= sizeof(*hdr
);
1822 } else if (!strcmp(arg
, "-v")) {
1824 } else if (!strcmp(arg
, "--progress-title")) {
1825 if (progress_title
|| (i
+1) >= argc
)
1826 usage(index_pack_usage
);
1827 progress_title
= argv
[++i
];
1828 } else if (!strcmp(arg
, "--show-resolving-progress")) {
1829 show_resolving_progress
= 1;
1830 } else if (!strcmp(arg
, "--report-end-of-input")) {
1831 report_end_of_input
= 1;
1832 } else if (!strcmp(arg
, "-o")) {
1833 if (index_name
|| (i
+1) >= argc
)
1834 usage(index_pack_usage
);
1835 index_name
= argv
[++i
];
1836 } else if (starts_with(arg
, "--index-version=")) {
1838 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1839 if (opts
.version
> 2)
1840 die(_("bad %s"), arg
);
1842 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1843 if (*c
|| opts
.off32_limit
& 0x80000000)
1844 die(_("bad %s"), arg
);
1845 } else if (skip_prefix(arg
, "--max-input-size=", &arg
)) {
1846 max_input_size
= strtoumax(arg
, NULL
, 10);
1847 } else if (skip_prefix(arg
, "--object-format=", &arg
)) {
1848 hash_algo
= hash_algo_by_name(arg
);
1849 if (hash_algo
== GIT_HASH_UNKNOWN
)
1850 die(_("unknown hash algorithm '%s'"), arg
);
1851 repo_set_hash_algo(the_repository
, hash_algo
);
1852 } else if (!strcmp(arg
, "--rev-index")) {
1854 } else if (!strcmp(arg
, "--no-rev-index")) {
1857 usage(index_pack_usage
);
1862 usage(index_pack_usage
);
1866 if (!pack_name
&& !from_stdin
)
1867 usage(index_pack_usage
);
1868 if (fix_thin_pack
&& !from_stdin
)
1869 die(_("the option '%s' requires '%s'"), "--fix-thin", "--stdin");
1870 if (from_stdin
&& !startup_info
->have_repository
)
1871 die(_("--stdin requires a git repository"));
1872 if (from_stdin
&& hash_algo
)
1873 die(_("options '%s' and '%s' cannot be used together"), "--object-format", "--stdin");
1874 if (!index_name
&& pack_name
)
1875 index_name
= derive_filename(pack_name
, "pack", "idx", &index_name_buf
);
1877 opts
.flags
&= ~(WRITE_REV
| WRITE_REV_VERIFY
);
1879 opts
.flags
|= verify
? WRITE_REV_VERIFY
: WRITE_REV
;
1881 rev_index_name
= derive_filename(index_name
,
1883 &rev_index_name_buf
);
1888 die(_("--verify with no packfile name given"));
1889 read_idx_option(&opts
, index_name
);
1890 opts
.flags
|= WRITE_IDX_VERIFY
| WRITE_IDX_STRICT
;
1893 opts
.flags
|= WRITE_IDX_STRICT
;
1895 if (HAVE_THREADS
&& !nr_threads
) {
1896 nr_threads
= online_cpus();
1898 * Experiments show that going above 20 threads doesn't help,
1899 * no matter how many cores you have. Below that, we tend to
1900 * max at half the number of online_cpus(), presumably because
1901 * half of those are hyperthreads rather than full cores. We'll
1902 * never reduce the level below "3", though, to match a
1903 * historical value that nobody complained about.
1906 ; /* too few cores to consider capping */
1907 else if (nr_threads
< 6)
1908 nr_threads
= 3; /* historic cap */
1909 else if (nr_threads
< 40)
1912 nr_threads
= 20; /* hard cap */
1915 curr_pack
= open_pack_file(pack_name
);
1916 parse_pack_header();
1917 CALLOC_ARRAY(objects
, st_add(nr_objects
, 1));
1919 CALLOC_ARRAY(obj_stat
, st_add(nr_objects
, 1));
1920 CALLOC_ARRAY(ofs_deltas
, nr_objects
);
1921 parse_pack_objects(pack_hash
);
1922 if (report_end_of_input
)
1923 write_in_full(2, "\0", 1);
1925 conclude_pack(fix_thin_pack
, curr_pack
, pack_hash
);
1929 foreign_nr
= check_objects();
1932 show_pack_info(stat_only
);
1934 ALLOC_ARRAY(idx_objects
, nr_objects
);
1935 for (i
= 0; i
< nr_objects
; i
++)
1936 idx_objects
[i
] = &objects
[i
].idx
;
1937 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, &opts
, pack_hash
);
1939 curr_rev_index
= write_rev_file(rev_index_name
, idx_objects
,
1940 nr_objects
, pack_hash
,
1945 final(pack_name
, curr_pack
,
1946 index_name
, curr_index
,
1947 rev_index_name
, curr_rev_index
,
1948 keep_msg
, promisor_msg
,
1953 if (do_fsck_object
&& fsck_finish(&fsck_options
))
1954 die(_("fsck error in pack objects"));
1958 strbuf_release(&index_name_buf
);
1959 strbuf_release(&rev_index_name_buf
);
1961 free((void *) curr_pack
);
1963 free((void *) curr_index
);
1964 if (!rev_index_name
)
1965 free((void *) curr_rev_index
);
1968 * Let the caller know this pack is not self contained
1970 if (check_self_contained_and_connected
&& foreign_nr
)