5 #include "environment.h"
17 #include "streaming.h"
18 #include "thread-utils.h"
20 #include "pack-revindex.h"
21 #include "object-file.h"
22 #include "object-store.h"
23 #include "oid-array.h"
24 #include "replace-object.h"
25 #include "promisor-remote.h"
29 static const char index_pack_usage
[] =
30 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--[no-]rev-index] [--verify] [--strict] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
33 struct pack_idx_entry idx
;
35 unsigned char hdr_size
;
37 signed char real_type
;
46 /* Initialized by make_base(). */
47 struct base_data
*base
;
48 struct object_entry
*obj
;
49 int ref_first
, ref_last
;
50 int ofs_first
, ofs_last
;
52 * Threads should increment retain_data if they are about to call
53 * patch_delta() using this struct's data as a base, and decrement this
54 * when they are done. While retain_data is nonzero, this struct's data
55 * will not be freed even if the delta base cache limit is exceeded.
59 * The number of direct children that have not been fully processed
60 * (entered work_head, entered done_head, left done_head). When this
61 * number reaches zero, this struct base_data can be freed.
63 int children_remaining
;
65 /* Not initialized by make_base(). */
66 struct list_head list
;
72 * Stack of struct base_data that have unprocessed children.
73 * threaded_second_pass() uses this as a source of work (the other being the
76 * Guarded by work_mutex.
78 static LIST_HEAD(work_head
);
81 * Stack of struct base_data that have children, all of whom have been
82 * processed or are being processed, and at least one child is being processed.
83 * These struct base_data must be kept around until the last child is
86 * Guarded by work_mutex.
88 static LIST_HEAD(done_head
);
91 * All threads share one delta base cache.
93 * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only
96 static size_t base_cache_used
;
97 static size_t base_cache_limit
;
104 /* Remember to update object flag allocation in object.h */
105 #define FLAG_LINK (1u<<20)
106 #define FLAG_CHECKED (1u<<21)
108 struct ofs_delta_entry
{
113 struct ref_delta_entry
{
114 struct object_id oid
;
118 static struct object_entry
*objects
;
119 static struct object_stat
*obj_stat
;
120 static struct ofs_delta_entry
*ofs_deltas
;
121 static struct ref_delta_entry
*ref_deltas
;
122 static struct thread_local nothread_data
;
123 static int nr_objects
;
124 static int nr_ofs_deltas
;
125 static int nr_ref_deltas
;
126 static int ref_deltas_alloc
;
127 static int nr_resolved_deltas
;
128 static int nr_threads
;
130 static int from_stdin
;
132 static int do_fsck_object
;
133 static struct fsck_options fsck_options
= FSCK_OPTIONS_MISSING_GITMODULES
;
135 static const char *progress_title
;
136 static int show_resolving_progress
;
137 static int show_stat
;
138 static int check_self_contained_and_connected
;
140 static struct progress
*progress
;
142 /* We always read in 4kB chunks. */
143 static unsigned char input_buffer
[4096];
144 static unsigned int input_offset
, input_len
;
145 static off_t consumed_bytes
;
146 static off_t max_input_size
;
147 static unsigned deepest_delta
;
148 static git_hash_ctx input_ctx
;
149 static uint32_t input_crc32
;
150 static int input_fd
, output_fd
;
151 static const char *curr_pack
;
153 static struct thread_local
*thread_data
;
154 static int nr_dispatched
;
155 static int threads_active
;
157 static pthread_mutex_t read_mutex
;
158 #define read_lock() lock_mutex(&read_mutex)
159 #define read_unlock() unlock_mutex(&read_mutex)
161 static pthread_mutex_t counter_mutex
;
162 #define counter_lock() lock_mutex(&counter_mutex)
163 #define counter_unlock() unlock_mutex(&counter_mutex)
165 static pthread_mutex_t work_mutex
;
166 #define work_lock() lock_mutex(&work_mutex)
167 #define work_unlock() unlock_mutex(&work_mutex)
169 static pthread_mutex_t deepest_delta_mutex
;
170 #define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
171 #define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
173 static pthread_key_t key
;
175 static inline void lock_mutex(pthread_mutex_t
*mutex
)
178 pthread_mutex_lock(mutex
);
181 static inline void unlock_mutex(pthread_mutex_t
*mutex
)
184 pthread_mutex_unlock(mutex
);
188 * Mutex and conditional variable can't be statically-initialized on Windows.
190 static void init_thread(void)
193 init_recursive_mutex(&read_mutex
);
194 pthread_mutex_init(&counter_mutex
, NULL
);
195 pthread_mutex_init(&work_mutex
, NULL
);
197 pthread_mutex_init(&deepest_delta_mutex
, NULL
);
198 pthread_key_create(&key
, NULL
);
199 CALLOC_ARRAY(thread_data
, nr_threads
);
200 for (i
= 0; i
< nr_threads
; i
++) {
201 thread_data
[i
].pack_fd
= xopen(curr_pack
, O_RDONLY
);
207 static void cleanup_thread(void)
213 pthread_mutex_destroy(&read_mutex
);
214 pthread_mutex_destroy(&counter_mutex
);
215 pthread_mutex_destroy(&work_mutex
);
217 pthread_mutex_destroy(&deepest_delta_mutex
);
218 for (i
= 0; i
< nr_threads
; i
++)
219 close(thread_data
[i
].pack_fd
);
220 pthread_key_delete(key
);
224 static int mark_link(struct object
*obj
, enum object_type type
,
225 void *data
, struct fsck_options
*options
)
230 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
231 die(_("object type mismatch at %s"), oid_to_hex(&obj
->oid
));
233 obj
->flags
|= FLAG_LINK
;
237 /* The content of each linked object must have been checked
238 or it must be already present in the object database */
239 static unsigned check_object(struct object
*obj
)
244 if (!(obj
->flags
& FLAG_LINK
))
247 if (!(obj
->flags
& FLAG_CHECKED
)) {
249 int type
= oid_object_info(the_repository
, &obj
->oid
, &size
);
251 die(_("did not receive expected object %s"),
252 oid_to_hex(&obj
->oid
));
253 if (type
!= obj
->type
)
254 die(_("object %s: expected type %s, found %s"),
255 oid_to_hex(&obj
->oid
),
256 type_name(obj
->type
), type_name(type
));
257 obj
->flags
|= FLAG_CHECKED
;
264 static unsigned check_objects(void)
266 unsigned i
, max
, foreign_nr
= 0;
268 max
= get_max_object_index();
271 progress
= start_delayed_progress(_("Checking objects"), max
);
273 for (i
= 0; i
< max
; i
++) {
274 foreign_nr
+= check_object(get_indexed_object(i
));
275 display_progress(progress
, i
+ 1);
278 stop_progress(&progress
);
283 /* Discard current buffer used content. */
284 static void flush(void)
288 write_or_die(output_fd
, input_buffer
, input_offset
);
289 the_hash_algo
->update_fn(&input_ctx
, input_buffer
, input_offset
);
290 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
296 * Make sure at least "min" bytes are available in the buffer, and
297 * return the pointer to the buffer.
299 static void *fill(int min
)
301 if (min
<= input_len
)
302 return input_buffer
+ input_offset
;
303 if (min
> sizeof(input_buffer
))
304 die(Q_("cannot fill %d byte",
305 "cannot fill %d bytes",
310 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
311 sizeof(input_buffer
) - input_len
);
315 die_errno(_("read error on input"));
319 display_throughput(progress
, consumed_bytes
+ input_len
);
320 } while (input_len
< min
);
324 static void use(int bytes
)
326 if (bytes
> input_len
)
327 die(_("used more bytes than were available"));
328 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
330 input_offset
+= bytes
;
332 /* make sure off_t is sufficiently large not to wrap */
333 if (signed_add_overflows(consumed_bytes
, bytes
))
334 die(_("pack too large for current definition of off_t"));
335 consumed_bytes
+= bytes
;
336 if (max_input_size
&& consumed_bytes
> max_input_size
) {
337 struct strbuf size_limit
= STRBUF_INIT
;
338 strbuf_humanise_bytes(&size_limit
, max_input_size
);
339 die(_("pack exceeds maximum allowed size (%s)"),
344 static const char *open_pack_file(const char *pack_name
)
349 struct strbuf tmp_file
= STRBUF_INIT
;
350 output_fd
= odb_mkstemp(&tmp_file
,
351 "pack/tmp_pack_XXXXXX");
352 pack_name
= strbuf_detach(&tmp_file
, NULL
);
354 output_fd
= xopen(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
356 nothread_data
.pack_fd
= output_fd
;
358 input_fd
= xopen(pack_name
, O_RDONLY
);
360 nothread_data
.pack_fd
= input_fd
;
362 the_hash_algo
->init_fn(&input_ctx
);
366 static void parse_pack_header(void)
368 struct pack_header
*hdr
= fill(sizeof(struct pack_header
));
370 /* Header consistency check */
371 if (hdr
->hdr_signature
!= htonl(PACK_SIGNATURE
))
372 die(_("pack signature mismatch"));
373 if (!pack_version_ok(hdr
->hdr_version
))
374 die(_("pack version %"PRIu32
" unsupported"),
375 ntohl(hdr
->hdr_version
));
377 nr_objects
= ntohl(hdr
->hdr_entries
);
378 use(sizeof(struct pack_header
));
381 __attribute__((format (printf
, 2, 3)))
382 static NORETURN
void bad_object(off_t offset
, const char *format
, ...)
387 va_start(params
, format
);
388 vsnprintf(buf
, sizeof(buf
), format
, params
);
390 die(_("pack has bad object at offset %"PRIuMAX
": %s"),
391 (uintmax_t)offset
, buf
);
394 static inline struct thread_local
*get_thread_data(void)
398 return pthread_getspecific(key
);
399 assert(!threads_active
&&
400 "This should only be reached when all threads are gone");
402 return ¬hread_data
;
405 static void set_thread_data(struct thread_local
*data
)
408 pthread_setspecific(key
, data
);
411 static void free_base_data(struct base_data
*c
)
414 FREE_AND_NULL(c
->data
);
415 base_cache_used
-= c
->size
;
419 static void prune_base_data(struct base_data
*retain
)
421 struct list_head
*pos
;
423 if (base_cache_used
<= base_cache_limit
)
426 list_for_each_prev(pos
, &done_head
) {
427 struct base_data
*b
= list_entry(pos
, struct base_data
, list
);
428 if (b
->retain_data
|| b
== retain
)
432 if (base_cache_used
<= base_cache_limit
)
437 list_for_each_prev(pos
, &work_head
) {
438 struct base_data
*b
= list_entry(pos
, struct base_data
, list
);
439 if (b
->retain_data
|| b
== retain
)
443 if (base_cache_used
<= base_cache_limit
)
449 static int is_delta_type(enum object_type type
)
451 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
454 static void *unpack_entry_data(off_t offset
, unsigned long size
,
455 enum object_type type
, struct object_id
*oid
)
457 static char fixed_buf
[8192];
465 if (!is_delta_type(type
)) {
466 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, size
);
467 the_hash_algo
->init_fn(&c
);
468 the_hash_algo
->update_fn(&c
, hdr
, hdrlen
);
471 if (type
== OBJ_BLOB
&& size
> big_file_threshold
)
474 buf
= xmallocz(size
);
476 memset(&stream
, 0, sizeof(stream
));
477 git_inflate_init(&stream
);
478 stream
.next_out
= buf
;
479 stream
.avail_out
= buf
== fixed_buf
? sizeof(fixed_buf
) : size
;
482 unsigned char *last_out
= stream
.next_out
;
483 stream
.next_in
= fill(1);
484 stream
.avail_in
= input_len
;
485 status
= git_inflate(&stream
, 0);
486 use(input_len
- stream
.avail_in
);
488 the_hash_algo
->update_fn(&c
, last_out
, stream
.next_out
- last_out
);
489 if (buf
== fixed_buf
) {
490 stream
.next_out
= buf
;
491 stream
.avail_out
= sizeof(fixed_buf
);
493 } while (status
== Z_OK
);
494 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
495 bad_object(offset
, _("inflate returned %d"), status
);
496 git_inflate_end(&stream
);
498 the_hash_algo
->final_oid_fn(oid
, &c
);
499 return buf
== fixed_buf
? NULL
: buf
;
502 static void *unpack_raw_entry(struct object_entry
*obj
,
504 struct object_id
*ref_oid
,
505 struct object_id
*oid
)
508 unsigned long size
, c
;
513 obj
->idx
.offset
= consumed_bytes
;
514 input_crc32
= crc32(0, NULL
, 0);
519 obj
->type
= (c
>> 4) & 7;
526 size
+= (c
& 0x7f) << shift
;
533 oidread(ref_oid
, fill(the_hash_algo
->rawsz
));
534 use(the_hash_algo
->rawsz
);
540 base_offset
= c
& 127;
543 if (!base_offset
|| MSB(base_offset
, 7))
544 bad_object(obj
->idx
.offset
, _("offset value overflow for delta base object"));
548 base_offset
= (base_offset
<< 7) + (c
& 127);
550 *ofs_offset
= obj
->idx
.offset
- base_offset
;
551 if (*ofs_offset
<= 0 || *ofs_offset
>= obj
->idx
.offset
)
552 bad_object(obj
->idx
.offset
, _("delta base offset is out of bound"));
560 bad_object(obj
->idx
.offset
, _("unknown object type %d"), obj
->type
);
562 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
564 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
, obj
->type
, oid
);
565 obj
->idx
.crc32
= input_crc32
;
569 static void *unpack_data(struct object_entry
*obj
,
570 int (*consume
)(const unsigned char *, unsigned long, void *),
573 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
574 off_t len
= obj
[1].idx
.offset
- from
;
575 unsigned char *data
, *inbuf
;
579 data
= xmallocz(consume
? 64*1024 : obj
->size
);
580 inbuf
= xmalloc((len
< 64*1024) ? (int)len
: 64*1024);
582 memset(&stream
, 0, sizeof(stream
));
583 git_inflate_init(&stream
);
584 stream
.next_out
= data
;
585 stream
.avail_out
= consume
? 64*1024 : obj
->size
;
588 ssize_t n
= (len
< 64*1024) ? (ssize_t
)len
: 64*1024;
589 n
= xpread(get_thread_data()->pack_fd
, inbuf
, n
, from
);
591 die_errno(_("cannot pread pack file"));
593 die(Q_("premature end of pack file, %"PRIuMAX
" byte missing",
594 "premature end of pack file, %"PRIuMAX
" bytes missing",
599 stream
.next_in
= inbuf
;
602 status
= git_inflate(&stream
, 0);
605 status
= git_inflate(&stream
, 0);
606 if (consume(data
, stream
.next_out
- data
, cb_data
)) {
611 stream
.next_out
= data
;
612 stream
.avail_out
= 64*1024;
613 } while (status
== Z_OK
&& stream
.avail_in
);
615 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
617 /* This has been inflated OK when first encountered, so... */
618 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
619 die(_("serious inflate inconsistency"));
621 git_inflate_end(&stream
);
629 static void *get_data_from_pack(struct object_entry
*obj
)
631 return unpack_data(obj
, NULL
, NULL
);
634 static int compare_ofs_delta_bases(off_t offset1
, off_t offset2
,
635 enum object_type type1
,
636 enum object_type type2
)
638 int cmp
= type1
- type2
;
641 return offset1
< offset2
? -1 :
642 offset1
> offset2
? 1 :
646 static int find_ofs_delta(const off_t offset
)
648 int first
= 0, last
= nr_ofs_deltas
;
650 while (first
< last
) {
651 int next
= first
+ (last
- first
) / 2;
652 struct ofs_delta_entry
*delta
= &ofs_deltas
[next
];
655 cmp
= compare_ofs_delta_bases(offset
, delta
->offset
,
657 objects
[delta
->obj_no
].type
);
669 static void find_ofs_delta_children(off_t offset
,
670 int *first_index
, int *last_index
)
672 int first
= find_ofs_delta(offset
);
674 int end
= nr_ofs_deltas
- 1;
681 while (first
> 0 && ofs_deltas
[first
- 1].offset
== offset
)
683 while (last
< end
&& ofs_deltas
[last
+ 1].offset
== offset
)
685 *first_index
= first
;
689 static int compare_ref_delta_bases(const struct object_id
*oid1
,
690 const struct object_id
*oid2
,
691 enum object_type type1
,
692 enum object_type type2
)
694 int cmp
= type1
- type2
;
697 return oidcmp(oid1
, oid2
);
700 static int find_ref_delta(const struct object_id
*oid
)
702 int first
= 0, last
= nr_ref_deltas
;
704 while (first
< last
) {
705 int next
= first
+ (last
- first
) / 2;
706 struct ref_delta_entry
*delta
= &ref_deltas
[next
];
709 cmp
= compare_ref_delta_bases(oid
, &delta
->oid
,
711 objects
[delta
->obj_no
].type
);
723 static void find_ref_delta_children(const struct object_id
*oid
,
724 int *first_index
, int *last_index
)
726 int first
= find_ref_delta(oid
);
728 int end
= nr_ref_deltas
- 1;
735 while (first
> 0 && oideq(&ref_deltas
[first
- 1].oid
, oid
))
737 while (last
< end
&& oideq(&ref_deltas
[last
+ 1].oid
, oid
))
739 *first_index
= first
;
743 struct compare_data
{
744 struct object_entry
*entry
;
745 struct git_istream
*st
;
747 unsigned long buf_size
;
750 static int compare_objects(const unsigned char *buf
, unsigned long size
,
753 struct compare_data
*data
= cb_data
;
755 if (data
->buf_size
< size
) {
757 data
->buf
= xmalloc(size
);
758 data
->buf_size
= size
;
762 ssize_t len
= read_istream(data
->st
, data
->buf
, size
);
764 die(_("SHA1 COLLISION FOUND WITH %s !"),
765 oid_to_hex(&data
->entry
->idx
.oid
));
767 die(_("unable to read %s"),
768 oid_to_hex(&data
->entry
->idx
.oid
));
769 if (memcmp(buf
, data
->buf
, len
))
770 die(_("SHA1 COLLISION FOUND WITH %s !"),
771 oid_to_hex(&data
->entry
->idx
.oid
));
778 static int check_collison(struct object_entry
*entry
)
780 struct compare_data data
;
781 enum object_type type
;
784 if (entry
->size
<= big_file_threshold
|| entry
->type
!= OBJ_BLOB
)
787 memset(&data
, 0, sizeof(data
));
789 data
.st
= open_istream(the_repository
, &entry
->idx
.oid
, &type
, &size
,
793 if (size
!= entry
->size
|| type
!= entry
->type
)
794 die(_("SHA1 COLLISION FOUND WITH %s !"),
795 oid_to_hex(&entry
->idx
.oid
));
796 unpack_data(entry
, compare_objects
, &data
);
797 close_istream(data
.st
);
802 static void sha1_object(const void *data
, struct object_entry
*obj_entry
,
803 unsigned long size
, enum object_type type
,
804 const struct object_id
*oid
)
806 void *new_data
= NULL
;
807 int collision_test_needed
= 0;
809 assert(data
|| obj_entry
);
811 if (startup_info
->have_repository
) {
813 collision_test_needed
=
814 repo_has_object_file_with_flags(the_repository
, oid
,
819 if (collision_test_needed
&& !data
) {
821 if (!check_collison(obj_entry
))
822 collision_test_needed
= 0;
825 if (collision_test_needed
) {
827 enum object_type has_type
;
828 unsigned long has_size
;
830 has_type
= oid_object_info(the_repository
, oid
, &has_size
);
832 die(_("cannot read existing object info %s"), oid_to_hex(oid
));
833 if (has_type
!= type
|| has_size
!= size
)
834 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid
));
835 has_data
= repo_read_object_file(the_repository
, oid
,
836 &has_type
, &has_size
);
839 data
= new_data
= get_data_from_pack(obj_entry
);
841 die(_("cannot read existing object %s"), oid_to_hex(oid
));
842 if (size
!= has_size
|| type
!= has_type
||
843 memcmp(data
, has_data
, size
) != 0)
844 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid
));
848 if (strict
|| do_fsck_object
) {
850 if (type
== OBJ_BLOB
) {
851 struct blob
*blob
= lookup_blob(the_repository
, oid
);
853 blob
->object
.flags
|= FLAG_CHECKED
;
855 die(_("invalid blob object %s"), oid_to_hex(oid
));
856 if (do_fsck_object
&&
857 fsck_object(&blob
->object
, (void *)data
, size
, &fsck_options
))
858 die(_("fsck error in packed object"));
862 void *buf
= (void *) data
;
864 assert(data
&& "data can only be NULL for large _blobs_");
867 * we do not need to free the memory here, as the
868 * buf is deleted by the caller.
870 obj
= parse_object_buffer(the_repository
, oid
, type
,
874 die(_("invalid %s"), type_name(type
));
875 if (do_fsck_object
&&
876 fsck_object(obj
, buf
, size
, &fsck_options
))
877 die(_("fsck error in packed object"));
878 if (strict
&& fsck_walk(obj
, NULL
, &fsck_options
))
879 die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj
->oid
));
881 if (obj
->type
== OBJ_TREE
) {
882 struct tree
*item
= (struct tree
*) obj
;
886 if (obj
->type
== OBJ_COMMIT
) {
887 struct commit
*commit
= (struct commit
*) obj
;
888 if (detach_commit_buffer(commit
, NULL
) != data
)
889 BUG("parse_object_buffer transmogrified our buffer");
891 obj
->flags
|= FLAG_CHECKED
;
900 * Ensure that this node has been reconstructed and return its contents.
902 * In the typical and best case, this node would already be reconstructed
903 * (through the invocation to resolve_delta() in threaded_second_pass()) and it
904 * would not be pruned. However, if pruning of this node was necessary due to
905 * reaching delta_base_cache_limit, this function will find the closest
906 * ancestor with reconstructed data that has not been pruned (or if there is
907 * none, the ultimate base object), and reconstruct each node in the delta
908 * chain in order to generate the reconstructed data for this node.
910 static void *get_base_data(struct base_data
*c
)
913 struct object_entry
*obj
= c
->obj
;
914 struct base_data
**delta
= NULL
;
915 int delta_nr
= 0, delta_alloc
= 0;
917 while (is_delta_type(c
->obj
->type
) && !c
->data
) {
918 ALLOC_GROW(delta
, delta_nr
+ 1, delta_alloc
);
919 delta
[delta_nr
++] = c
;
923 c
->data
= get_data_from_pack(obj
);
925 base_cache_used
+= c
->size
;
928 for (; delta_nr
> 0; delta_nr
--) {
930 c
= delta
[delta_nr
- 1];
932 base
= get_base_data(c
->base
);
933 raw
= get_data_from_pack(obj
);
934 c
->data
= patch_delta(
940 bad_object(obj
->idx
.offset
, _("failed to apply delta"));
941 base_cache_used
+= c
->size
;
949 static struct base_data
*make_base(struct object_entry
*obj
,
950 struct base_data
*parent
)
952 struct base_data
*base
= xcalloc(1, sizeof(struct base_data
));
955 find_ref_delta_children(&obj
->idx
.oid
,
956 &base
->ref_first
, &base
->ref_last
);
957 find_ofs_delta_children(obj
->idx
.offset
,
958 &base
->ofs_first
, &base
->ofs_last
);
959 base
->children_remaining
= base
->ref_last
- base
->ref_first
+
960 base
->ofs_last
- base
->ofs_first
+ 2;
964 static struct base_data
*resolve_delta(struct object_entry
*delta_obj
,
965 struct base_data
*base
)
967 void *delta_data
, *result_data
;
968 struct base_data
*result
;
969 unsigned long result_size
;
972 int i
= delta_obj
- objects
;
973 int j
= base
->obj
- objects
;
974 obj_stat
[i
].delta_depth
= obj_stat
[j
].delta_depth
+ 1;
975 deepest_delta_lock();
976 if (deepest_delta
< obj_stat
[i
].delta_depth
)
977 deepest_delta
= obj_stat
[i
].delta_depth
;
978 deepest_delta_unlock();
979 obj_stat
[i
].base_object_no
= j
;
981 delta_data
= get_data_from_pack(delta_obj
);
983 result_data
= patch_delta(base
->data
, base
->size
,
984 delta_data
, delta_obj
->size
, &result_size
);
987 bad_object(delta_obj
->idx
.offset
, _("failed to apply delta"));
988 hash_object_file(the_hash_algo
, result_data
, result_size
,
989 delta_obj
->real_type
, &delta_obj
->idx
.oid
);
990 sha1_object(result_data
, NULL
, result_size
, delta_obj
->real_type
,
991 &delta_obj
->idx
.oid
);
993 result
= make_base(delta_obj
, base
);
994 result
->data
= result_data
;
995 result
->size
= result_size
;
998 nr_resolved_deltas
++;
1004 static int compare_ofs_delta_entry(const void *a
, const void *b
)
1006 const struct ofs_delta_entry
*delta_a
= a
;
1007 const struct ofs_delta_entry
*delta_b
= b
;
1009 return delta_a
->offset
< delta_b
->offset
? -1 :
1010 delta_a
->offset
> delta_b
->offset
? 1 :
1014 static int compare_ref_delta_entry(const void *a
, const void *b
)
1016 const struct ref_delta_entry
*delta_a
= a
;
1017 const struct ref_delta_entry
*delta_b
= b
;
1019 return oidcmp(&delta_a
->oid
, &delta_b
->oid
);
1022 static void *threaded_second_pass(void *data
)
1025 set_thread_data(data
);
1027 struct base_data
*parent
= NULL
;
1028 struct object_entry
*child_obj
;
1029 struct base_data
*child
;
1032 display_progress(progress
, nr_resolved_deltas
);
1036 if (list_empty(&work_head
)) {
1038 * Take an object from the object array.
1040 while (nr_dispatched
< nr_objects
&&
1041 is_delta_type(objects
[nr_dispatched
].type
))
1043 if (nr_dispatched
>= nr_objects
) {
1047 child_obj
= &objects
[nr_dispatched
++];
1050 * Peek at the top of the stack, and take a child from
1053 parent
= list_first_entry(&work_head
, struct base_data
,
1056 if (parent
->ref_first
<= parent
->ref_last
) {
1057 int offset
= ref_deltas
[parent
->ref_first
++].obj_no
;
1058 child_obj
= objects
+ offset
;
1059 if (child_obj
->real_type
!= OBJ_REF_DELTA
)
1060 die("REF_DELTA at offset %"PRIuMAX
" already resolved (duplicate base %s?)",
1061 (uintmax_t) child_obj
->idx
.offset
,
1062 oid_to_hex(&parent
->obj
->idx
.oid
));
1063 child_obj
->real_type
= parent
->obj
->real_type
;
1065 child_obj
= objects
+
1066 ofs_deltas
[parent
->ofs_first
++].obj_no
;
1067 assert(child_obj
->real_type
== OBJ_OFS_DELTA
);
1068 child_obj
->real_type
= parent
->obj
->real_type
;
1071 if (parent
->ref_first
> parent
->ref_last
&&
1072 parent
->ofs_first
> parent
->ofs_last
) {
1074 * This parent has run out of children, so move
1077 list_del(&parent
->list
);
1078 list_add(&parent
->list
, &done_head
);
1082 * Ensure that the parent has data, since we will need
1085 * NEEDSWORK: If parent data needs to be reloaded, this
1086 * prolongs the time that the current thread spends in
1087 * the mutex. A mitigating factor is that parent data
1088 * needs to be reloaded only if the delta base cache
1089 * limit is exceeded, so in the typical case, this does
1092 get_base_data(parent
);
1093 parent
->retain_data
++;
1098 child
= resolve_delta(child_obj
, parent
);
1099 if (!child
->children_remaining
)
1100 FREE_AND_NULL(child
->data
);
1102 child
= make_base(child_obj
, NULL
);
1103 if (child
->children_remaining
) {
1105 * Since this child has its own delta children,
1106 * we will need this data in the future.
1107 * Inflate now so that future iterations will
1108 * have access to this object's data while
1109 * outside the work mutex.
1111 child
->data
= get_data_from_pack(child_obj
);
1112 child
->size
= child_obj
->size
;
1118 parent
->retain_data
--;
1121 * This child has its own children, so add it to
1124 list_add(&child
->list
, &work_head
);
1125 base_cache_used
+= child
->size
;
1126 prune_base_data(NULL
);
1127 free_base_data(child
);
1130 * This child does not have its own children. It may be
1131 * the last descendant of its ancestors; free those
1134 struct base_data
*p
= parent
;
1137 struct base_data
*next_p
;
1139 p
->children_remaining
--;
1140 if (p
->children_remaining
)
1150 FREE_AND_NULL(child
);
1159 * - find locations of all objects;
1160 * - calculate SHA1 of all non-delta objects;
1161 * - remember base (SHA1 or offset) for all deltas.
1163 static void parse_pack_objects(unsigned char *hash
)
1165 int i
, nr_delays
= 0;
1166 struct ofs_delta_entry
*ofs_delta
= ofs_deltas
;
1167 struct object_id ref_delta_oid
;
1171 progress
= start_progress(
1172 progress_title
? progress_title
:
1173 from_stdin
? _("Receiving objects") : _("Indexing objects"),
1175 for (i
= 0; i
< nr_objects
; i
++) {
1176 struct object_entry
*obj
= &objects
[i
];
1177 void *data
= unpack_raw_entry(obj
, &ofs_delta
->offset
,
1180 obj
->real_type
= obj
->type
;
1181 if (obj
->type
== OBJ_OFS_DELTA
) {
1183 ofs_delta
->obj_no
= i
;
1185 } else if (obj
->type
== OBJ_REF_DELTA
) {
1186 ALLOC_GROW(ref_deltas
, nr_ref_deltas
+ 1, ref_deltas_alloc
);
1187 oidcpy(&ref_deltas
[nr_ref_deltas
].oid
, &ref_delta_oid
);
1188 ref_deltas
[nr_ref_deltas
].obj_no
= i
;
1191 /* large blobs, check later */
1192 obj
->real_type
= OBJ_BAD
;
1195 sha1_object(data
, NULL
, obj
->size
, obj
->type
,
1198 display_progress(progress
, i
+1);
1200 objects
[i
].idx
.offset
= consumed_bytes
;
1201 stop_progress(&progress
);
1203 /* Check pack integrity */
1205 the_hash_algo
->final_fn(hash
, &input_ctx
);
1206 if (!hasheq(fill(the_hash_algo
->rawsz
), hash
))
1207 die(_("pack is corrupted (SHA1 mismatch)"));
1208 use(the_hash_algo
->rawsz
);
1210 /* If input_fd is a file, we should have reached its end now. */
1211 if (fstat(input_fd
, &st
))
1212 die_errno(_("cannot fstat packfile"));
1213 if (S_ISREG(st
.st_mode
) &&
1214 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
1215 die(_("pack has junk at the end"));
1217 for (i
= 0; i
< nr_objects
; i
++) {
1218 struct object_entry
*obj
= &objects
[i
];
1219 if (obj
->real_type
!= OBJ_BAD
)
1221 obj
->real_type
= obj
->type
;
1222 sha1_object(NULL
, obj
, obj
->size
, obj
->type
,
1227 die(_("confusion beyond insanity in parse_pack_objects()"));
1232 * - for all non-delta objects, look if it is used as a base for
1234 * - if used as a base, uncompress the object and apply all deltas,
1235 * recursively checking if the resulting object is used as a base
1236 * for some more deltas.
1238 static void resolve_deltas(void)
1242 if (!nr_ofs_deltas
&& !nr_ref_deltas
)
1245 /* Sort deltas by base SHA1/offset for fast searching */
1246 QSORT(ofs_deltas
, nr_ofs_deltas
, compare_ofs_delta_entry
);
1247 QSORT(ref_deltas
, nr_ref_deltas
, compare_ref_delta_entry
);
1249 if (verbose
|| show_resolving_progress
)
1250 progress
= start_progress(_("Resolving deltas"),
1251 nr_ref_deltas
+ nr_ofs_deltas
);
1254 base_cache_limit
= delta_base_cache_limit
* nr_threads
;
1255 if (nr_threads
> 1 || getenv("GIT_FORCE_THREADS")) {
1257 for (i
= 0; i
< nr_threads
; i
++) {
1258 int ret
= pthread_create(&thread_data
[i
].thread
, NULL
,
1259 threaded_second_pass
, thread_data
+ i
);
1261 die(_("unable to create thread: %s"),
1264 for (i
= 0; i
< nr_threads
; i
++)
1265 pthread_join(thread_data
[i
].thread
, NULL
);
1269 threaded_second_pass(¬hread_data
);
1274 * - append objects to convert thin pack to full pack if required
1275 * - write the final pack hash
1277 static void fix_unresolved_deltas(struct hashfile
*f
);
1278 static void conclude_pack(int fix_thin_pack
, const char *curr_pack
, unsigned char *pack_hash
)
1280 if (nr_ref_deltas
+ nr_ofs_deltas
== nr_resolved_deltas
) {
1281 stop_progress(&progress
);
1282 /* Flush remaining pack final hash. */
1287 if (fix_thin_pack
) {
1289 unsigned char read_hash
[GIT_MAX_RAWSZ
], tail_hash
[GIT_MAX_RAWSZ
];
1290 struct strbuf msg
= STRBUF_INIT
;
1291 int nr_unresolved
= nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
;
1292 int nr_objects_initial
= nr_objects
;
1293 if (nr_unresolved
<= 0)
1294 die(_("confusion beyond insanity"));
1295 REALLOC_ARRAY(objects
, nr_objects
+ nr_unresolved
+ 1);
1296 memset(objects
+ nr_objects
+ 1, 0,
1297 nr_unresolved
* sizeof(*objects
));
1298 f
= hashfd(output_fd
, curr_pack
);
1299 fix_unresolved_deltas(f
);
1300 strbuf_addf(&msg
, Q_("completed with %d local object",
1301 "completed with %d local objects",
1302 nr_objects
- nr_objects_initial
),
1303 nr_objects
- nr_objects_initial
);
1304 stop_progress_msg(&progress
, msg
.buf
);
1305 strbuf_release(&msg
);
1306 finalize_hashfile(f
, tail_hash
, FSYNC_COMPONENT_PACK
, 0);
1307 hashcpy(read_hash
, pack_hash
);
1308 fixup_pack_header_footer(output_fd
, pack_hash
,
1309 curr_pack
, nr_objects
,
1310 read_hash
, consumed_bytes
-the_hash_algo
->rawsz
);
1311 if (!hasheq(read_hash
, tail_hash
))
1312 die(_("Unexpected tail checksum for %s "
1313 "(disk corruption?)"), curr_pack
);
1315 if (nr_ofs_deltas
+ nr_ref_deltas
!= nr_resolved_deltas
)
1316 die(Q_("pack has %d unresolved delta",
1317 "pack has %d unresolved deltas",
1318 nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
),
1319 nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
);
1322 static int write_compressed(struct hashfile
*f
, void *in
, unsigned int size
)
1326 unsigned char outbuf
[4096];
1328 git_deflate_init(&stream
, zlib_compression_level
);
1329 stream
.next_in
= in
;
1330 stream
.avail_in
= size
;
1333 stream
.next_out
= outbuf
;
1334 stream
.avail_out
= sizeof(outbuf
);
1335 status
= git_deflate(&stream
, Z_FINISH
);
1336 hashwrite(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
1337 } while (status
== Z_OK
);
1339 if (status
!= Z_STREAM_END
)
1340 die(_("unable to deflate appended object (%d)"), status
);
1341 size
= stream
.total_out
;
1342 git_deflate_end(&stream
);
1346 static struct object_entry
*append_obj_to_pack(struct hashfile
*f
,
1347 const unsigned char *sha1
, void *buf
,
1348 unsigned long size
, enum object_type type
)
1350 struct object_entry
*obj
= &objects
[nr_objects
++];
1351 unsigned char header
[10];
1352 unsigned long s
= size
;
1354 unsigned char c
= (type
<< 4) | (s
& 15);
1357 header
[n
++] = c
| 0x80;
1363 hashwrite(f
, header
, n
);
1365 obj
[0].hdr_size
= n
;
1367 obj
[0].real_type
= type
;
1368 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
1369 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
1370 obj
[0].idx
.crc32
= crc32_end(f
);
1372 oidread(&obj
->idx
.oid
, sha1
);
1376 static int delta_pos_compare(const void *_a
, const void *_b
)
1378 struct ref_delta_entry
*a
= *(struct ref_delta_entry
**)_a
;
1379 struct ref_delta_entry
*b
= *(struct ref_delta_entry
**)_b
;
1380 return a
->obj_no
- b
->obj_no
;
1383 static void fix_unresolved_deltas(struct hashfile
*f
)
1385 struct ref_delta_entry
**sorted_by_pos
;
1389 * Since many unresolved deltas may well be themselves base objects
1390 * for more unresolved deltas, we really want to include the
1391 * smallest number of base objects that would cover as much delta
1392 * as possible by picking the
1393 * trunc deltas first, allowing for other deltas to resolve without
1394 * additional base objects. Since most base objects are to be found
1395 * before deltas depending on them, a good heuristic is to start
1396 * resolving deltas in the same order as their position in the pack.
1398 ALLOC_ARRAY(sorted_by_pos
, nr_ref_deltas
);
1399 for (i
= 0; i
< nr_ref_deltas
; i
++)
1400 sorted_by_pos
[i
] = &ref_deltas
[i
];
1401 QSORT(sorted_by_pos
, nr_ref_deltas
, delta_pos_compare
);
1403 if (repo_has_promisor_remote(the_repository
)) {
1405 * Prefetch the delta bases.
1407 struct oid_array to_fetch
= OID_ARRAY_INIT
;
1408 for (i
= 0; i
< nr_ref_deltas
; i
++) {
1409 struct ref_delta_entry
*d
= sorted_by_pos
[i
];
1410 if (!oid_object_info_extended(the_repository
, &d
->oid
,
1412 OBJECT_INFO_FOR_PREFETCH
))
1414 oid_array_append(&to_fetch
, &d
->oid
);
1416 promisor_remote_get_direct(the_repository
,
1417 to_fetch
.oid
, to_fetch
.nr
);
1418 oid_array_clear(&to_fetch
);
1421 for (i
= 0; i
< nr_ref_deltas
; i
++) {
1422 struct ref_delta_entry
*d
= sorted_by_pos
[i
];
1423 enum object_type type
;
1427 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
1429 data
= repo_read_object_file(the_repository
, &d
->oid
, &type
,
1434 if (check_object_signature(the_repository
, &d
->oid
, data
, size
,
1436 die(_("local object %s is corrupt"), oid_to_hex(&d
->oid
));
1439 * Add this as an object to the objects array and call
1440 * threaded_second_pass() (which will pick up the added
1443 append_obj_to_pack(f
, d
->oid
.hash
, data
, size
, type
);
1445 threaded_second_pass(NULL
);
1447 display_progress(progress
, nr_resolved_deltas
);
1449 free(sorted_by_pos
);
1452 static const char *derive_filename(const char *pack_name
, const char *strip
,
1453 const char *suffix
, struct strbuf
*buf
)
1456 if (!strip_suffix(pack_name
, strip
, &len
) || !len
||
1457 pack_name
[len
- 1] != '.')
1458 die(_("packfile name '%s' does not end with '.%s'"),
1460 strbuf_add(buf
, pack_name
, len
);
1461 strbuf_addstr(buf
, suffix
);
1465 static void write_special_file(const char *suffix
, const char *msg
,
1466 const char *pack_name
, const unsigned char *hash
,
1467 const char **report
)
1469 struct strbuf name_buf
= STRBUF_INIT
;
1470 const char *filename
;
1472 int msg_len
= strlen(msg
);
1475 filename
= derive_filename(pack_name
, "pack", suffix
, &name_buf
);
1477 filename
= odb_pack_name(&name_buf
, hash
, suffix
);
1479 fd
= odb_pack_keep(filename
);
1481 if (errno
!= EEXIST
)
1482 die_errno(_("cannot write %s file '%s'"),
1486 write_or_die(fd
, msg
, msg_len
);
1487 write_or_die(fd
, "\n", 1);
1490 die_errno(_("cannot close written %s file '%s'"),
1495 strbuf_release(&name_buf
);
1498 static void rename_tmp_packfile(const char **final_name
,
1499 const char *curr_name
,
1500 struct strbuf
*name
, unsigned char *hash
,
1501 const char *ext
, int make_read_only_if_same
)
1503 if (*final_name
!= curr_name
) {
1505 *final_name
= odb_pack_name(name
, hash
, ext
);
1506 if (finalize_object_file(curr_name
, *final_name
))
1507 die(_("unable to rename temporary '*.%s' file to '%s'"),
1509 } else if (make_read_only_if_same
) {
1510 chmod(*final_name
, 0444);
1514 static void final(const char *final_pack_name
, const char *curr_pack_name
,
1515 const char *final_index_name
, const char *curr_index_name
,
1516 const char *final_rev_index_name
, const char *curr_rev_index_name
,
1517 const char *keep_msg
, const char *promisor_msg
,
1518 unsigned char *hash
)
1520 const char *report
= "pack";
1521 struct strbuf pack_name
= STRBUF_INIT
;
1522 struct strbuf index_name
= STRBUF_INIT
;
1523 struct strbuf rev_index_name
= STRBUF_INIT
;
1529 fsync_component_or_die(FSYNC_COMPONENT_PACK
, output_fd
, curr_pack_name
);
1530 err
= close(output_fd
);
1532 die_errno(_("error while closing pack file"));
1536 write_special_file("keep", keep_msg
, final_pack_name
, hash
,
1539 write_special_file("promisor", promisor_msg
, final_pack_name
,
1542 rename_tmp_packfile(&final_pack_name
, curr_pack_name
, &pack_name
,
1543 hash
, "pack", from_stdin
);
1544 if (curr_rev_index_name
)
1545 rename_tmp_packfile(&final_rev_index_name
, curr_rev_index_name
,
1546 &rev_index_name
, hash
, "rev", 1);
1547 rename_tmp_packfile(&final_index_name
, curr_index_name
, &index_name
,
1550 if (do_fsck_object
) {
1551 struct packed_git
*p
;
1552 p
= add_packed_git(final_index_name
, strlen(final_index_name
), 0);
1554 install_packed_git(the_repository
, p
);
1558 printf("%s\n", hash_to_hex(hash
));
1560 struct strbuf buf
= STRBUF_INIT
;
1562 strbuf_addf(&buf
, "%s\t%s\n", report
, hash_to_hex(hash
));
1563 write_or_die(1, buf
.buf
, buf
.len
);
1564 strbuf_release(&buf
);
1567 * Let's just mimic git-unpack-objects here and write
1568 * the last part of the input buffer to stdout.
1571 err
= xwrite(1, input_buffer
+ input_offset
, input_len
);
1575 input_offset
+= err
;
1579 strbuf_release(&rev_index_name
);
1580 strbuf_release(&index_name
);
1581 strbuf_release(&pack_name
);
1584 static int git_index_pack_config(const char *k
, const char *v
, void *cb
)
1586 struct pack_idx_option
*opts
= cb
;
1588 if (!strcmp(k
, "pack.indexversion")) {
1589 opts
->version
= git_config_int(k
, v
);
1590 if (opts
->version
> 2)
1591 die(_("bad pack.indexVersion=%"PRIu32
), opts
->version
);
1594 if (!strcmp(k
, "pack.threads")) {
1595 nr_threads
= git_config_int(k
, v
);
1597 die(_("invalid number of threads specified (%d)"),
1599 if (!HAVE_THREADS
&& nr_threads
!= 1) {
1600 warning(_("no threads support, ignoring %s"), k
);
1605 if (!strcmp(k
, "pack.writereverseindex")) {
1606 if (git_config_bool(k
, v
))
1607 opts
->flags
|= WRITE_REV
;
1609 opts
->flags
&= ~WRITE_REV
;
1611 return git_default_config(k
, v
, cb
);
1614 static int cmp_uint32(const void *a_
, const void *b_
)
1616 uint32_t a
= *((uint32_t *)a_
);
1617 uint32_t b
= *((uint32_t *)b_
);
1619 return (a
< b
) ? -1 : (a
!= b
);
1622 static void read_v2_anomalous_offsets(struct packed_git
*p
,
1623 struct pack_idx_option
*opts
)
1625 const uint32_t *idx1
, *idx2
;
1628 /* The address of the 4-byte offset table */
1629 idx1
= (((const uint32_t *)((const uint8_t *)p
->index_data
+ p
->crc_offset
))
1630 + (size_t)p
->num_objects
/* CRC32 table */
1633 /* The address of the 8-byte offset table */
1634 idx2
= idx1
+ p
->num_objects
;
1636 for (i
= 0; i
< p
->num_objects
; i
++) {
1637 uint32_t off
= ntohl(idx1
[i
]);
1638 if (!(off
& 0x80000000))
1640 off
= off
& 0x7fffffff;
1641 check_pack_index_ptr(p
, &idx2
[off
* 2]);
1645 * The real offset is ntohl(idx2[off * 2]) in high 4
1646 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1647 * octets. But idx2[off * 2] is Zero!!!
1649 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
1650 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
1653 QSORT(opts
->anomaly
, opts
->anomaly_nr
, cmp_uint32
);
1656 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
1658 struct packed_git
*p
= add_packed_git(pack_name
, strlen(pack_name
), 1);
1661 die(_("Cannot open existing pack file '%s'"), pack_name
);
1662 if (open_pack_index(p
))
1663 die(_("Cannot open existing pack idx file for '%s'"), pack_name
);
1665 /* Read the attributes from the existing idx file */
1666 opts
->version
= p
->index_version
;
1668 if (opts
->version
== 2)
1669 read_v2_anomalous_offsets(p
, opts
);
1672 * Get rid of the idx file as we do not need it anymore.
1673 * NEEDSWORK: extract this bit from free_pack_by_name() in
1674 * object-file.c, perhaps? It shouldn't matter very much as we
1675 * know we haven't installed this pack (hence we never have
1676 * read anything from it).
1678 close_pack_index(p
);
1682 static void show_pack_info(int stat_only
)
1684 int i
, baseobjects
= nr_objects
- nr_ref_deltas
- nr_ofs_deltas
;
1685 unsigned long *chain_histogram
= NULL
;
1688 CALLOC_ARRAY(chain_histogram
, deepest_delta
);
1690 for (i
= 0; i
< nr_objects
; i
++) {
1691 struct object_entry
*obj
= &objects
[i
];
1693 if (is_delta_type(obj
->type
))
1694 chain_histogram
[obj_stat
[i
].delta_depth
- 1]++;
1697 printf("%s %-6s %"PRIuMAX
" %"PRIuMAX
" %"PRIuMAX
,
1698 oid_to_hex(&obj
->idx
.oid
),
1699 type_name(obj
->real_type
), (uintmax_t)obj
->size
,
1700 (uintmax_t)(obj
[1].idx
.offset
- obj
->idx
.offset
),
1701 (uintmax_t)obj
->idx
.offset
);
1702 if (is_delta_type(obj
->type
)) {
1703 struct object_entry
*bobj
= &objects
[obj_stat
[i
].base_object_no
];
1704 printf(" %u %s", obj_stat
[i
].delta_depth
,
1705 oid_to_hex(&bobj
->idx
.oid
));
1711 printf_ln(Q_("non delta: %d object",
1712 "non delta: %d objects",
1715 for (i
= 0; i
< deepest_delta
; i
++) {
1716 if (!chain_histogram
[i
])
1718 printf_ln(Q_("chain length = %d: %lu object",
1719 "chain length = %d: %lu objects",
1720 chain_histogram
[i
]),
1722 chain_histogram
[i
]);
1724 free(chain_histogram
);
1727 int cmd_index_pack(int argc
, const char **argv
, const char *prefix
)
1729 int i
, fix_thin_pack
= 0, verify
= 0, stat_only
= 0, rev_index
;
1730 const char *curr_index
;
1731 const char *curr_rev_index
= NULL
;
1732 const char *index_name
= NULL
, *pack_name
= NULL
, *rev_index_name
= NULL
;
1733 const char *keep_msg
= NULL
;
1734 const char *promisor_msg
= NULL
;
1735 struct strbuf index_name_buf
= STRBUF_INIT
;
1736 struct strbuf rev_index_name_buf
= STRBUF_INIT
;
1737 struct pack_idx_entry
**idx_objects
;
1738 struct pack_idx_option opts
;
1739 unsigned char pack_hash
[GIT_MAX_RAWSZ
];
1740 unsigned foreign_nr
= 1; /* zero is a "good" value, assume bad */
1741 int report_end_of_input
= 0;
1745 * index-pack never needs to fetch missing objects except when
1746 * REF_DELTA bases are missing (which are explicitly handled). It only
1747 * accesses the repo to do hash collision checks and to check which
1748 * REF_DELTA bases need to be fetched.
1750 fetch_if_missing
= 0;
1752 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1753 usage(index_pack_usage
);
1755 disable_replace_refs();
1756 fsck_options
.walk
= mark_link
;
1758 reset_pack_idx_option(&opts
);
1759 opts
.flags
|= WRITE_REV
;
1760 git_config(git_index_pack_config
, &opts
);
1761 if (prefix
&& chdir(prefix
))
1762 die(_("Cannot come back to cwd"));
1764 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX
, 0))
1767 rev_index
= !!(opts
.flags
& (WRITE_REV_VERIFY
| WRITE_REV
));
1769 for (i
= 1; i
< argc
; i
++) {
1770 const char *arg
= argv
[i
];
1773 if (!strcmp(arg
, "--stdin")) {
1775 } else if (!strcmp(arg
, "--fix-thin")) {
1777 } else if (skip_to_optional_arg(arg
, "--strict", &arg
)) {
1780 fsck_set_msg_types(&fsck_options
, arg
);
1781 } else if (!strcmp(arg
, "--check-self-contained-and-connected")) {
1783 check_self_contained_and_connected
= 1;
1784 } else if (!strcmp(arg
, "--fsck-objects")) {
1786 } else if (!strcmp(arg
, "--verify")) {
1788 } else if (!strcmp(arg
, "--verify-stat")) {
1791 } else if (!strcmp(arg
, "--verify-stat-only")) {
1795 } else if (skip_to_optional_arg(arg
, "--keep", &keep_msg
)) {
1796 ; /* nothing to do */
1797 } else if (skip_to_optional_arg(arg
, "--promisor", &promisor_msg
)) {
1798 ; /* already parsed */
1799 } else if (starts_with(arg
, "--threads=")) {
1801 nr_threads
= strtoul(arg
+10, &end
, 0);
1802 if (!arg
[10] || *end
|| nr_threads
< 0)
1803 usage(index_pack_usage
);
1804 if (!HAVE_THREADS
&& nr_threads
!= 1) {
1805 warning(_("no threads support, ignoring %s"), arg
);
1808 } else if (starts_with(arg
, "--pack_header=")) {
1809 struct pack_header
*hdr
;
1812 hdr
= (struct pack_header
*)input_buffer
;
1813 hdr
->hdr_signature
= htonl(PACK_SIGNATURE
);
1814 hdr
->hdr_version
= htonl(strtoul(arg
+ 14, &c
, 10));
1816 die(_("bad %s"), arg
);
1817 hdr
->hdr_entries
= htonl(strtoul(c
+ 1, &c
, 10));
1819 die(_("bad %s"), arg
);
1820 input_len
= sizeof(*hdr
);
1821 } else if (!strcmp(arg
, "-v")) {
1823 } else if (!strcmp(arg
, "--progress-title")) {
1824 if (progress_title
|| (i
+1) >= argc
)
1825 usage(index_pack_usage
);
1826 progress_title
= argv
[++i
];
1827 } else if (!strcmp(arg
, "--show-resolving-progress")) {
1828 show_resolving_progress
= 1;
1829 } else if (!strcmp(arg
, "--report-end-of-input")) {
1830 report_end_of_input
= 1;
1831 } else if (!strcmp(arg
, "-o")) {
1832 if (index_name
|| (i
+1) >= argc
)
1833 usage(index_pack_usage
);
1834 index_name
= argv
[++i
];
1835 } else if (starts_with(arg
, "--index-version=")) {
1837 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1838 if (opts
.version
> 2)
1839 die(_("bad %s"), arg
);
1841 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1842 if (*c
|| opts
.off32_limit
& 0x80000000)
1843 die(_("bad %s"), arg
);
1844 } else if (skip_prefix(arg
, "--max-input-size=", &arg
)) {
1845 max_input_size
= strtoumax(arg
, NULL
, 10);
1846 } else if (skip_prefix(arg
, "--object-format=", &arg
)) {
1847 hash_algo
= hash_algo_by_name(arg
);
1848 if (hash_algo
== GIT_HASH_UNKNOWN
)
1849 die(_("unknown hash algorithm '%s'"), arg
);
1850 repo_set_hash_algo(the_repository
, hash_algo
);
1851 } else if (!strcmp(arg
, "--rev-index")) {
1853 } else if (!strcmp(arg
, "--no-rev-index")) {
1856 usage(index_pack_usage
);
1861 usage(index_pack_usage
);
1865 if (!pack_name
&& !from_stdin
)
1866 usage(index_pack_usage
);
1867 if (fix_thin_pack
&& !from_stdin
)
1868 die(_("the option '%s' requires '%s'"), "--fix-thin", "--stdin");
1869 if (from_stdin
&& !startup_info
->have_repository
)
1870 die(_("--stdin requires a git repository"));
1871 if (from_stdin
&& hash_algo
)
1872 die(_("options '%s' and '%s' cannot be used together"), "--object-format", "--stdin");
1873 if (!index_name
&& pack_name
)
1874 index_name
= derive_filename(pack_name
, "pack", "idx", &index_name_buf
);
1876 opts
.flags
&= ~(WRITE_REV
| WRITE_REV_VERIFY
);
1878 opts
.flags
|= verify
? WRITE_REV_VERIFY
: WRITE_REV
;
1880 rev_index_name
= derive_filename(index_name
,
1882 &rev_index_name_buf
);
1887 die(_("--verify with no packfile name given"));
1888 read_idx_option(&opts
, index_name
);
1889 opts
.flags
|= WRITE_IDX_VERIFY
| WRITE_IDX_STRICT
;
1892 opts
.flags
|= WRITE_IDX_STRICT
;
1894 if (HAVE_THREADS
&& !nr_threads
) {
1895 nr_threads
= online_cpus();
1897 * Experiments show that going above 20 threads doesn't help,
1898 * no matter how many cores you have. Below that, we tend to
1899 * max at half the number of online_cpus(), presumably because
1900 * half of those are hyperthreads rather than full cores. We'll
1901 * never reduce the level below "3", though, to match a
1902 * historical value that nobody complained about.
1905 ; /* too few cores to consider capping */
1906 else if (nr_threads
< 6)
1907 nr_threads
= 3; /* historic cap */
1908 else if (nr_threads
< 40)
1911 nr_threads
= 20; /* hard cap */
1914 curr_pack
= open_pack_file(pack_name
);
1915 parse_pack_header();
1916 CALLOC_ARRAY(objects
, st_add(nr_objects
, 1));
1918 CALLOC_ARRAY(obj_stat
, st_add(nr_objects
, 1));
1919 CALLOC_ARRAY(ofs_deltas
, nr_objects
);
1920 parse_pack_objects(pack_hash
);
1921 if (report_end_of_input
)
1922 write_in_full(2, "\0", 1);
1924 conclude_pack(fix_thin_pack
, curr_pack
, pack_hash
);
1928 foreign_nr
= check_objects();
1931 show_pack_info(stat_only
);
1933 ALLOC_ARRAY(idx_objects
, nr_objects
);
1934 for (i
= 0; i
< nr_objects
; i
++)
1935 idx_objects
[i
] = &objects
[i
].idx
;
1936 curr_index
= write_idx_file(index_name
, idx_objects
, nr_objects
, &opts
, pack_hash
);
1938 curr_rev_index
= write_rev_file(rev_index_name
, idx_objects
,
1939 nr_objects
, pack_hash
,
1944 final(pack_name
, curr_pack
,
1945 index_name
, curr_index
,
1946 rev_index_name
, curr_rev_index
,
1947 keep_msg
, promisor_msg
,
1952 if (do_fsck_object
&& fsck_finish(&fsck_options
))
1953 die(_("fsck error in pack objects"));
1957 strbuf_release(&index_name_buf
);
1958 strbuf_release(&rev_index_name_buf
);
1960 free((void *) curr_pack
);
1962 free((void *) curr_index
);
1963 if (!rev_index_name
)
1964 free((void *) curr_rev_index
);
1967 * Let the caller know this pack is not self contained
1969 if (check_self_contained_and_connected
&& foreign_nr
)