1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
7 #include "environment.h"
11 #include "csum-file.h"
19 #include "streaming.h"
20 #include "thread-utils.h"
22 #include "pack-revindex.h"
23 #include "object-file.h"
25 #include "oid-array.h"
28 #include "replace-object.h"
29 #include "tree-walk.h"
30 #include "promisor-remote.h"
31 #include "run-command.h"
35 static const char index_pack_usage
[] =
36 "git index-pack [-v] [-o <index-file>] [--keep | --keep=<msg>] [--[no-]rev-index] [--verify] [--strict[=<msg-id>=<severity>...]] [--fsck-objects[=<msg-id>=<severity>...]] (<pack-file> | --stdin [--fix-thin] [<pack-file>])";
39 struct pack_idx_entry idx
;
41 unsigned char hdr_size
;
43 signed char real_type
;
52 /* Initialized by make_base(). */
53 struct base_data
*base
;
54 struct object_entry
*obj
;
55 int ref_first
, ref_last
;
56 int ofs_first
, ofs_last
;
58 * Threads should increment retain_data if they are about to call
59 * patch_delta() using this struct's data as a base, and decrement this
60 * when they are done. While retain_data is nonzero, this struct's data
61 * will not be freed even if the delta base cache limit is exceeded.
65 * The number of direct children that have not been fully processed
66 * (entered work_head, entered done_head, left done_head). When this
67 * number reaches zero, this struct base_data can be freed.
69 int children_remaining
;
71 /* Not initialized by make_base(). */
72 struct list_head list
;
78 * Stack of struct base_data that have unprocessed children.
79 * threaded_second_pass() uses this as a source of work (the other being the
82 * Guarded by work_mutex.
84 static LIST_HEAD(work_head
);
87 * Stack of struct base_data that have children, all of whom have been
88 * processed or are being processed, and at least one child is being processed.
89 * These struct base_data must be kept around until the last child is
92 * Guarded by work_mutex.
94 static LIST_HEAD(done_head
);
97 * All threads share one delta base cache.
99 * base_cache_used is guarded by work_mutex, and base_cache_limit is read-only
102 static size_t base_cache_used
;
103 static size_t base_cache_limit
;
105 struct thread_local_data
{
110 /* Remember to update object flag allocation in object.h */
111 #define FLAG_LINK (1u<<20)
112 #define FLAG_CHECKED (1u<<21)
114 struct ofs_delta_entry
{
119 struct ref_delta_entry
{
120 struct object_id oid
;
124 static struct object_entry
*objects
;
125 static struct object_stat
*obj_stat
;
126 static struct ofs_delta_entry
*ofs_deltas
;
127 static struct ref_delta_entry
*ref_deltas
;
128 static struct thread_local_data nothread_data
;
129 static int nr_objects
;
130 static int nr_ofs_deltas
;
131 static int nr_ref_deltas
;
132 static int ref_deltas_alloc
;
133 static int nr_resolved_deltas
;
134 static int nr_threads
;
136 static int from_stdin
;
138 static int do_fsck_object
;
139 static struct fsck_options fsck_options
= FSCK_OPTIONS_MISSING_GITMODULES
;
141 static const char *progress_title
;
142 static int show_resolving_progress
;
143 static int show_stat
;
144 static int check_self_contained_and_connected
;
146 static struct progress
*progress
;
148 /* We always read in 4kB chunks. */
149 static unsigned char input_buffer
[4096];
150 static unsigned int input_offset
, input_len
;
151 static off_t consumed_bytes
;
152 static off_t max_input_size
;
153 static unsigned deepest_delta
;
154 static struct git_hash_ctx input_ctx
;
155 static uint32_t input_crc32
;
156 static int input_fd
, output_fd
;
157 static const char *curr_pack
;
160 * outgoing_links is guarded by read_mutex, and record_outgoing_links is
161 * read-only in a thread.
163 static struct oidset outgoing_links
= OIDSET_INIT
;
164 static int record_outgoing_links
;
166 static struct thread_local_data
*thread_data
;
167 static int nr_dispatched
;
168 static int threads_active
;
170 static pthread_mutex_t read_mutex
;
171 #define read_lock() lock_mutex(&read_mutex)
172 #define read_unlock() unlock_mutex(&read_mutex)
174 static pthread_mutex_t counter_mutex
;
175 #define counter_lock() lock_mutex(&counter_mutex)
176 #define counter_unlock() unlock_mutex(&counter_mutex)
178 static pthread_mutex_t work_mutex
;
179 #define work_lock() lock_mutex(&work_mutex)
180 #define work_unlock() unlock_mutex(&work_mutex)
182 static pthread_mutex_t deepest_delta_mutex
;
183 #define deepest_delta_lock() lock_mutex(&deepest_delta_mutex)
184 #define deepest_delta_unlock() unlock_mutex(&deepest_delta_mutex)
186 static pthread_key_t key
;
188 static inline void lock_mutex(pthread_mutex_t
*mutex
)
191 pthread_mutex_lock(mutex
);
194 static inline void unlock_mutex(pthread_mutex_t
*mutex
)
197 pthread_mutex_unlock(mutex
);
201 * Mutex and conditional variable can't be statically-initialized on Windows.
203 static void init_thread(void)
206 init_recursive_mutex(&read_mutex
);
207 pthread_mutex_init(&counter_mutex
, NULL
);
208 pthread_mutex_init(&work_mutex
, NULL
);
210 pthread_mutex_init(&deepest_delta_mutex
, NULL
);
211 pthread_key_create(&key
, NULL
);
212 CALLOC_ARRAY(thread_data
, nr_threads
);
213 for (i
= 0; i
< nr_threads
; i
++) {
214 thread_data
[i
].pack_fd
= xopen(curr_pack
, O_RDONLY
);
220 static void cleanup_thread(void)
226 pthread_mutex_destroy(&read_mutex
);
227 pthread_mutex_destroy(&counter_mutex
);
228 pthread_mutex_destroy(&work_mutex
);
230 pthread_mutex_destroy(&deepest_delta_mutex
);
231 for (i
= 0; i
< nr_threads
; i
++)
232 close(thread_data
[i
].pack_fd
);
233 pthread_key_delete(key
);
237 static int mark_link(struct object
*obj
, enum object_type type
,
239 struct fsck_options
*options UNUSED
)
244 if (type
!= OBJ_ANY
&& obj
->type
!= type
)
245 die(_("object type mismatch at %s"), oid_to_hex(&obj
->oid
));
247 obj
->flags
|= FLAG_LINK
;
251 /* The content of each linked object must have been checked
252 or it must be already present in the object database */
253 static unsigned check_object(struct object
*obj
)
258 if (!(obj
->flags
& FLAG_LINK
))
261 if (!(obj
->flags
& FLAG_CHECKED
)) {
263 int type
= odb_read_object_info(the_repository
->objects
,
266 die(_("did not receive expected object %s"),
267 oid_to_hex(&obj
->oid
));
268 if (type
!= obj
->type
)
269 die(_("object %s: expected type %s, found %s"),
270 oid_to_hex(&obj
->oid
),
271 type_name(obj
->type
), type_name(type
));
272 obj
->flags
|= FLAG_CHECKED
;
279 static unsigned check_objects(void)
281 unsigned i
, max
, foreign_nr
= 0;
283 max
= get_max_object_index(the_repository
);
286 progress
= start_delayed_progress(the_repository
,
287 _("Checking objects"), max
);
289 for (i
= 0; i
< max
; i
++) {
290 foreign_nr
+= check_object(get_indexed_object(the_repository
, i
));
291 display_progress(progress
, i
+ 1);
294 stop_progress(&progress
);
299 /* Discard current buffer used content. */
300 static void flush(void)
304 write_or_die(output_fd
, input_buffer
, input_offset
);
305 git_hash_update(&input_ctx
, input_buffer
, input_offset
);
306 memmove(input_buffer
, input_buffer
+ input_offset
, input_len
);
312 * Make sure at least "min" bytes are available in the buffer, and
313 * return the pointer to the buffer.
315 static void *fill(int min
)
317 if (min
<= input_len
)
318 return input_buffer
+ input_offset
;
319 if (min
> sizeof(input_buffer
))
320 die(Q_("cannot fill %d byte",
321 "cannot fill %d bytes",
326 ssize_t ret
= xread(input_fd
, input_buffer
+ input_len
,
327 sizeof(input_buffer
) - input_len
);
331 die_errno(_("read error on input"));
335 display_throughput(progress
, consumed_bytes
+ input_len
);
336 } while (input_len
< min
);
340 static void use(int bytes
)
342 if (bytes
> input_len
)
343 die(_("used more bytes than were available"));
344 input_crc32
= crc32(input_crc32
, input_buffer
+ input_offset
, bytes
);
346 input_offset
+= bytes
;
348 /* make sure off_t is sufficiently large not to wrap */
349 if (signed_add_overflows(consumed_bytes
, bytes
))
350 die(_("pack too large for current definition of off_t"));
351 consumed_bytes
+= bytes
;
352 if (max_input_size
&& consumed_bytes
> max_input_size
) {
353 struct strbuf size_limit
= STRBUF_INIT
;
354 strbuf_humanise_bytes(&size_limit
, max_input_size
);
355 die(_("pack exceeds maximum allowed size (%s)"),
360 static const char *open_pack_file(const char *pack_name
)
365 struct strbuf tmp_file
= STRBUF_INIT
;
366 output_fd
= odb_mkstemp(the_repository
->objects
, &tmp_file
,
367 "pack/tmp_pack_XXXXXX");
368 pack_name
= strbuf_detach(&tmp_file
, NULL
);
370 output_fd
= xopen(pack_name
, O_CREAT
|O_EXCL
|O_RDWR
, 0600);
372 nothread_data
.pack_fd
= output_fd
;
374 input_fd
= xopen(pack_name
, O_RDONLY
);
376 nothread_data
.pack_fd
= input_fd
;
378 the_hash_algo
->init_fn(&input_ctx
);
382 static void parse_pack_header(void)
384 unsigned char *hdr
= fill(sizeof(struct pack_header
));
386 /* Header consistency check */
387 if (get_be32(hdr
) != PACK_SIGNATURE
)
388 die(_("pack signature mismatch"));
390 if (!pack_version_ok_native(get_be32(hdr
)))
391 die(_("pack version %"PRIu32
" unsupported"),
395 nr_objects
= get_be32(hdr
);
396 use(sizeof(struct pack_header
));
399 __attribute__((format (printf
, 2, 3)))
400 static NORETURN
void bad_object(off_t offset
, const char *format
, ...)
405 va_start(params
, format
);
406 vsnprintf(buf
, sizeof(buf
), format
, params
);
408 die(_("pack has bad object at offset %"PRIuMAX
": %s"),
409 (uintmax_t)offset
, buf
);
412 static inline struct thread_local_data
*get_thread_data(void)
416 return pthread_getspecific(key
);
417 assert(!threads_active
&&
418 "This should only be reached when all threads are gone");
420 return ¬hread_data
;
423 static void set_thread_data(struct thread_local_data
*data
)
426 pthread_setspecific(key
, data
);
429 static void free_base_data(struct base_data
*c
)
432 FREE_AND_NULL(c
->data
);
433 base_cache_used
-= c
->size
;
437 static void prune_base_data(struct base_data
*retain
)
439 struct list_head
*pos
;
441 if (base_cache_used
<= base_cache_limit
)
444 list_for_each_prev(pos
, &done_head
) {
445 struct base_data
*b
= list_entry(pos
, struct base_data
, list
);
446 if (b
->retain_data
|| b
== retain
)
450 if (base_cache_used
<= base_cache_limit
)
455 list_for_each_prev(pos
, &work_head
) {
456 struct base_data
*b
= list_entry(pos
, struct base_data
, list
);
457 if (b
->retain_data
|| b
== retain
)
461 if (base_cache_used
<= base_cache_limit
)
467 static int is_delta_type(enum object_type type
)
469 return (type
== OBJ_REF_DELTA
|| type
== OBJ_OFS_DELTA
);
472 static void *unpack_entry_data(off_t offset
, unsigned long size
,
473 enum object_type type
, struct object_id
*oid
)
475 static char fixed_buf
[8192];
479 struct git_hash_ctx c
;
483 if (!is_delta_type(type
)) {
484 hdrlen
= format_object_header(hdr
, sizeof(hdr
), type
, size
);
485 the_hash_algo
->init_fn(&c
);
486 git_hash_update(&c
, hdr
, hdrlen
);
489 if (type
== OBJ_BLOB
&&
490 size
> repo_settings_get_big_file_threshold(the_repository
))
493 buf
= xmallocz(size
);
495 memset(&stream
, 0, sizeof(stream
));
496 git_inflate_init(&stream
);
497 stream
.next_out
= buf
;
498 stream
.avail_out
= buf
== fixed_buf
? sizeof(fixed_buf
) : size
;
501 unsigned char *last_out
= stream
.next_out
;
502 stream
.next_in
= fill(1);
503 stream
.avail_in
= input_len
;
504 status
= git_inflate(&stream
, 0);
505 use(input_len
- stream
.avail_in
);
507 git_hash_update(&c
, last_out
, stream
.next_out
- last_out
);
508 if (buf
== fixed_buf
) {
509 stream
.next_out
= buf
;
510 stream
.avail_out
= sizeof(fixed_buf
);
512 } while (status
== Z_OK
);
513 if (stream
.total_out
!= size
|| status
!= Z_STREAM_END
)
514 bad_object(offset
, _("inflate returned %d"), status
);
515 git_inflate_end(&stream
);
517 git_hash_final_oid(oid
, &c
);
518 return buf
== fixed_buf
? NULL
: buf
;
521 static void *unpack_raw_entry(struct object_entry
*obj
,
523 struct object_id
*ref_oid
,
524 struct object_id
*oid
)
527 unsigned long size
, c
;
532 obj
->idx
.offset
= consumed_bytes
;
533 input_crc32
= crc32(0, NULL
, 0);
538 obj
->type
= (c
>> 4) & 7;
545 size
+= (c
& 0x7f) << shift
;
552 oidread(ref_oid
, fill(the_hash_algo
->rawsz
),
553 the_repository
->hash_algo
);
554 use(the_hash_algo
->rawsz
);
560 base_offset
= c
& 127;
563 if (!base_offset
|| MSB(base_offset
, 7))
564 bad_object(obj
->idx
.offset
, _("offset value overflow for delta base object"));
568 base_offset
= (base_offset
<< 7) + (c
& 127);
570 *ofs_offset
= obj
->idx
.offset
- base_offset
;
571 if (*ofs_offset
<= 0 || *ofs_offset
>= obj
->idx
.offset
)
572 bad_object(obj
->idx
.offset
, _("delta base offset is out of bound"));
580 bad_object(obj
->idx
.offset
, _("unknown object type %d"), obj
->type
);
582 obj
->hdr_size
= consumed_bytes
- obj
->idx
.offset
;
584 data
= unpack_entry_data(obj
->idx
.offset
, obj
->size
, obj
->type
, oid
);
585 obj
->idx
.crc32
= input_crc32
;
589 static void *unpack_data(struct object_entry
*obj
,
590 int (*consume
)(const unsigned char *, unsigned long, void *),
593 off_t from
= obj
[0].idx
.offset
+ obj
[0].hdr_size
;
594 off_t len
= obj
[1].idx
.offset
- from
;
595 unsigned char *data
, *inbuf
;
599 data
= xmallocz(consume
? 64*1024 : obj
->size
);
600 inbuf
= xmalloc((len
< 64*1024) ? (int)len
: 64*1024);
602 memset(&stream
, 0, sizeof(stream
));
603 git_inflate_init(&stream
);
604 stream
.next_out
= data
;
605 stream
.avail_out
= consume
? 64*1024 : obj
->size
;
608 ssize_t n
= (len
< 64*1024) ? (ssize_t
)len
: 64*1024;
609 n
= xpread(get_thread_data()->pack_fd
, inbuf
, n
, from
);
611 die_errno(_("cannot pread pack file"));
613 die(Q_("premature end of pack file, %"PRIuMAX
" byte missing",
614 "premature end of pack file, %"PRIuMAX
" bytes missing",
619 stream
.next_in
= inbuf
;
622 status
= git_inflate(&stream
, 0);
625 status
= git_inflate(&stream
, 0);
626 if (consume(data
, stream
.next_out
- data
, cb_data
)) {
631 stream
.next_out
= data
;
632 stream
.avail_out
= 64*1024;
633 } while (status
== Z_OK
&& stream
.avail_in
);
635 } while (len
&& status
== Z_OK
&& !stream
.avail_in
);
637 /* This has been inflated OK when first encountered, so... */
638 if (status
!= Z_STREAM_END
|| stream
.total_out
!= obj
->size
)
639 die(_("serious inflate inconsistency"));
641 git_inflate_end(&stream
);
649 static void *get_data_from_pack(struct object_entry
*obj
)
651 return unpack_data(obj
, NULL
, NULL
);
654 static int compare_ofs_delta_bases(off_t offset1
, off_t offset2
,
655 enum object_type type1
,
656 enum object_type type2
)
658 int cmp
= type1
- type2
;
661 return offset1
< offset2
? -1 :
662 offset1
> offset2
? 1 :
666 static int find_ofs_delta(const off_t offset
)
668 int first
= 0, last
= nr_ofs_deltas
;
670 while (first
< last
) {
671 int next
= first
+ (last
- first
) / 2;
672 struct ofs_delta_entry
*delta
= &ofs_deltas
[next
];
675 cmp
= compare_ofs_delta_bases(offset
, delta
->offset
,
677 objects
[delta
->obj_no
].type
);
689 static void find_ofs_delta_children(off_t offset
,
690 int *first_index
, int *last_index
)
692 int first
= find_ofs_delta(offset
);
694 int end
= nr_ofs_deltas
- 1;
701 while (first
> 0 && ofs_deltas
[first
- 1].offset
== offset
)
703 while (last
< end
&& ofs_deltas
[last
+ 1].offset
== offset
)
705 *first_index
= first
;
709 static int compare_ref_delta_bases(const struct object_id
*oid1
,
710 const struct object_id
*oid2
,
711 enum object_type type1
,
712 enum object_type type2
)
714 int cmp
= type1
- type2
;
717 return oidcmp(oid1
, oid2
);
720 static int find_ref_delta(const struct object_id
*oid
)
722 int first
= 0, last
= nr_ref_deltas
;
724 while (first
< last
) {
725 int next
= first
+ (last
- first
) / 2;
726 struct ref_delta_entry
*delta
= &ref_deltas
[next
];
729 cmp
= compare_ref_delta_bases(oid
, &delta
->oid
,
731 objects
[delta
->obj_no
].type
);
743 static void find_ref_delta_children(const struct object_id
*oid
,
744 int *first_index
, int *last_index
)
746 int first
= find_ref_delta(oid
);
748 int end
= nr_ref_deltas
- 1;
755 while (first
> 0 && oideq(&ref_deltas
[first
- 1].oid
, oid
))
757 while (last
< end
&& oideq(&ref_deltas
[last
+ 1].oid
, oid
))
759 *first_index
= first
;
763 struct compare_data
{
764 struct object_entry
*entry
;
765 struct git_istream
*st
;
767 unsigned long buf_size
;
770 static int compare_objects(const unsigned char *buf
, unsigned long size
,
773 struct compare_data
*data
= cb_data
;
775 if (data
->buf_size
< size
) {
777 data
->buf
= xmalloc(size
);
778 data
->buf_size
= size
;
782 ssize_t len
= read_istream(data
->st
, data
->buf
, size
);
784 die(_("SHA1 COLLISION FOUND WITH %s !"),
785 oid_to_hex(&data
->entry
->idx
.oid
));
787 die(_("unable to read %s"),
788 oid_to_hex(&data
->entry
->idx
.oid
));
789 if (memcmp(buf
, data
->buf
, len
))
790 die(_("SHA1 COLLISION FOUND WITH %s !"),
791 oid_to_hex(&data
->entry
->idx
.oid
));
798 static int check_collison(struct object_entry
*entry
)
800 struct compare_data data
;
801 enum object_type type
;
804 if (entry
->size
<= repo_settings_get_big_file_threshold(the_repository
) ||
805 entry
->type
!= OBJ_BLOB
)
808 memset(&data
, 0, sizeof(data
));
810 data
.st
= open_istream(the_repository
, &entry
->idx
.oid
, &type
, &size
,
814 if (size
!= entry
->size
|| type
!= entry
->type
)
815 die(_("SHA1 COLLISION FOUND WITH %s !"),
816 oid_to_hex(&entry
->idx
.oid
));
817 unpack_data(entry
, compare_objects
, &data
);
818 close_istream(data
.st
);
823 static void record_outgoing_link(const struct object_id
*oid
)
825 oidset_insert(&outgoing_links
, oid
);
828 static void maybe_record_name_entry(const struct name_entry
*entry
)
831 * Checking only trees here results in a significantly faster packfile
832 * indexing, but the drawback is that if the packfile to be indexed
833 * references a local blob only directly (that is, never through a
834 * local tree), that local blob is in danger of being garbage
835 * collected. Such a situation may arise if we push local commits,
836 * including one with a change to a blob in the root tree, and then the
837 * server incorporates them into its main branch through a "rebase" or
838 * "squash" merge strategy, and then we fetch the new main branch from
841 * This situation has not been observed yet - we have only noticed
842 * missing commits, not missing trees or blobs. (In fact, if it were
843 * believed that only missing commits are problematic, one could argue
844 * that we should also exclude trees during the outgoing link check;
845 * but it is safer to include them.)
847 * Due to the rarity of the situation (it has not been observed to
848 * happen in real life), and because the "penalty" in such a situation
849 * is merely to refetch the missing blob when it's needed (and this
850 * happens only once - when refetched, the blob goes into a promisor
851 * pack, so it won't be GC-ed, the tradeoff seems worth it.
853 if (S_ISDIR(entry
->mode
))
854 record_outgoing_link(&entry
->oid
);
857 static void do_record_outgoing_links(struct object
*obj
)
859 if (obj
->type
== OBJ_TREE
) {
860 struct tree
*tree
= (struct tree
*)obj
;
861 struct tree_desc desc
;
862 struct name_entry entry
;
863 if (init_tree_desc_gently(&desc
, &tree
->object
.oid
,
864 tree
->buffer
, tree
->size
, 0))
866 * Error messages are given when packs are
867 * verified, so do not print any here.
870 while (tree_entry_gently(&desc
, &entry
))
871 maybe_record_name_entry(&entry
);
872 } else if (obj
->type
== OBJ_COMMIT
) {
873 struct commit
*commit
= (struct commit
*) obj
;
874 struct commit_list
*parents
= commit
->parents
;
876 record_outgoing_link(get_commit_tree_oid(commit
));
877 for (; parents
; parents
= parents
->next
)
878 record_outgoing_link(&parents
->item
->object
.oid
);
879 } else if (obj
->type
== OBJ_TAG
) {
880 struct tag
*tag
= (struct tag
*) obj
;
881 record_outgoing_link(get_tagged_oid(tag
));
885 static void sha1_object(const void *data
, struct object_entry
*obj_entry
,
886 unsigned long size
, enum object_type type
,
887 const struct object_id
*oid
)
889 void *new_data
= NULL
;
890 int collision_test_needed
= 0;
892 assert(data
|| obj_entry
);
894 if (startup_info
->have_repository
) {
896 collision_test_needed
= odb_has_object(the_repository
->objects
, oid
,
897 HAS_OBJECT_FETCH_PROMISOR
);
901 if (collision_test_needed
&& !data
) {
903 if (!check_collison(obj_entry
))
904 collision_test_needed
= 0;
907 if (collision_test_needed
) {
909 enum object_type has_type
;
910 unsigned long has_size
;
912 has_type
= odb_read_object_info(the_repository
->objects
, oid
, &has_size
);
914 die(_("cannot read existing object info %s"), oid_to_hex(oid
));
915 if (has_type
!= type
|| has_size
!= size
)
916 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid
));
917 has_data
= odb_read_object(the_repository
->objects
, oid
,
918 &has_type
, &has_size
);
921 data
= new_data
= get_data_from_pack(obj_entry
);
923 die(_("cannot read existing object %s"), oid_to_hex(oid
));
924 if (size
!= has_size
|| type
!= has_type
||
925 memcmp(data
, has_data
, size
) != 0)
926 die(_("SHA1 COLLISION FOUND WITH %s !"), oid_to_hex(oid
));
930 if (strict
|| do_fsck_object
|| record_outgoing_links
) {
932 if (type
== OBJ_BLOB
) {
933 struct blob
*blob
= lookup_blob(the_repository
, oid
);
935 blob
->object
.flags
|= FLAG_CHECKED
;
937 die(_("invalid blob object %s"), oid_to_hex(oid
));
938 if (do_fsck_object
&&
939 fsck_object(&blob
->object
, (void *)data
, size
, &fsck_options
))
940 die(_("fsck error in packed object"));
944 void *buf
= (void *) data
;
946 assert(data
&& "data can only be NULL for large _blobs_");
949 * we do not need to free the memory here, as the
950 * buf is deleted by the caller.
952 obj
= parse_object_buffer(the_repository
, oid
, type
,
956 die(_("invalid %s"), type_name(type
));
957 if (do_fsck_object
&&
958 fsck_object(obj
, buf
, size
, &fsck_options
))
959 die(_("fsck error in packed object"));
960 if (strict
&& fsck_walk(obj
, NULL
, &fsck_options
))
961 die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj
->oid
));
962 if (record_outgoing_links
)
963 do_record_outgoing_links(obj
);
965 if (obj
->type
== OBJ_TREE
) {
966 struct tree
*item
= (struct tree
*) obj
;
970 if (obj
->type
== OBJ_COMMIT
) {
971 struct commit
*commit
= (struct commit
*) obj
;
972 if (detach_commit_buffer(commit
, NULL
) != data
)
973 BUG("parse_object_buffer transmogrified our buffer");
975 obj
->flags
|= FLAG_CHECKED
;
984 * Ensure that this node has been reconstructed and return its contents.
986 * In the typical and best case, this node would already be reconstructed
987 * (through the invocation to resolve_delta() in threaded_second_pass()) and it
988 * would not be pruned. However, if pruning of this node was necessary due to
989 * reaching delta_base_cache_limit, this function will find the closest
990 * ancestor with reconstructed data that has not been pruned (or if there is
991 * none, the ultimate base object), and reconstruct each node in the delta
992 * chain in order to generate the reconstructed data for this node.
994 static void *get_base_data(struct base_data
*c
)
997 struct object_entry
*obj
= c
->obj
;
998 struct base_data
**delta
= NULL
;
999 int delta_nr
= 0, delta_alloc
= 0;
1001 while (is_delta_type(c
->obj
->type
) && !c
->data
) {
1002 ALLOC_GROW(delta
, delta_nr
+ 1, delta_alloc
);
1003 delta
[delta_nr
++] = c
;
1007 c
->data
= get_data_from_pack(obj
);
1008 c
->size
= obj
->size
;
1009 base_cache_used
+= c
->size
;
1012 for (; delta_nr
> 0; delta_nr
--) {
1014 c
= delta
[delta_nr
- 1];
1016 base
= get_base_data(c
->base
);
1017 raw
= get_data_from_pack(obj
);
1018 c
->data
= patch_delta(
1019 base
, c
->base
->size
,
1024 bad_object(obj
->idx
.offset
, _("failed to apply delta"));
1025 base_cache_used
+= c
->size
;
1033 static struct base_data
*make_base(struct object_entry
*obj
,
1034 struct base_data
*parent
)
1036 struct base_data
*base
= xcalloc(1, sizeof(struct base_data
));
1037 base
->base
= parent
;
1039 find_ref_delta_children(&obj
->idx
.oid
,
1040 &base
->ref_first
, &base
->ref_last
);
1041 find_ofs_delta_children(obj
->idx
.offset
,
1042 &base
->ofs_first
, &base
->ofs_last
);
1043 base
->children_remaining
= base
->ref_last
- base
->ref_first
+
1044 base
->ofs_last
- base
->ofs_first
+ 2;
1048 static struct base_data
*resolve_delta(struct object_entry
*delta_obj
,
1049 struct base_data
*base
)
1051 void *delta_data
, *result_data
;
1052 struct base_data
*result
;
1053 unsigned long result_size
;
1056 int i
= delta_obj
- objects
;
1057 int j
= base
->obj
- objects
;
1058 obj_stat
[i
].delta_depth
= obj_stat
[j
].delta_depth
+ 1;
1059 deepest_delta_lock();
1060 if (deepest_delta
< obj_stat
[i
].delta_depth
)
1061 deepest_delta
= obj_stat
[i
].delta_depth
;
1062 deepest_delta_unlock();
1063 obj_stat
[i
].base_object_no
= j
;
1065 delta_data
= get_data_from_pack(delta_obj
);
1067 result_data
= patch_delta(base
->data
, base
->size
,
1068 delta_data
, delta_obj
->size
, &result_size
);
1071 bad_object(delta_obj
->idx
.offset
, _("failed to apply delta"));
1072 hash_object_file(the_hash_algo
, result_data
, result_size
,
1073 delta_obj
->real_type
, &delta_obj
->idx
.oid
);
1074 sha1_object(result_data
, NULL
, result_size
, delta_obj
->real_type
,
1075 &delta_obj
->idx
.oid
);
1077 result
= make_base(delta_obj
, base
);
1078 result
->data
= result_data
;
1079 result
->size
= result_size
;
1082 nr_resolved_deltas
++;
1088 static int compare_ofs_delta_entry(const void *a
, const void *b
)
1090 const struct ofs_delta_entry
*delta_a
= a
;
1091 const struct ofs_delta_entry
*delta_b
= b
;
1093 return delta_a
->offset
< delta_b
->offset
? -1 :
1094 delta_a
->offset
> delta_b
->offset
? 1 :
1098 static int compare_ref_delta_entry(const void *a
, const void *b
)
1100 const struct ref_delta_entry
*delta_a
= a
;
1101 const struct ref_delta_entry
*delta_b
= b
;
1103 return oidcmp(&delta_a
->oid
, &delta_b
->oid
);
1106 static void *threaded_second_pass(void *data
)
1109 set_thread_data(data
);
1111 struct base_data
*parent
= NULL
;
1112 struct object_entry
*child_obj
= NULL
;
1113 struct base_data
*child
= NULL
;
1116 display_progress(progress
, nr_resolved_deltas
);
1120 if (list_empty(&work_head
)) {
1122 * Take an object from the object array.
1124 while (nr_dispatched
< nr_objects
&&
1125 is_delta_type(objects
[nr_dispatched
].type
))
1127 if (nr_dispatched
>= nr_objects
) {
1131 child_obj
= &objects
[nr_dispatched
++];
1134 * Peek at the top of the stack, and take a child from
1137 parent
= list_first_entry(&work_head
, struct base_data
,
1140 while (parent
->ref_first
<= parent
->ref_last
) {
1141 int offset
= ref_deltas
[parent
->ref_first
++].obj_no
;
1142 child_obj
= objects
+ offset
;
1143 if (child_obj
->real_type
!= OBJ_REF_DELTA
) {
1147 child_obj
->real_type
= parent
->obj
->real_type
;
1151 if (!child_obj
&& parent
->ofs_first
<= parent
->ofs_last
) {
1152 child_obj
= objects
+
1153 ofs_deltas
[parent
->ofs_first
++].obj_no
;
1154 assert(child_obj
->real_type
== OBJ_OFS_DELTA
);
1155 child_obj
->real_type
= parent
->obj
->real_type
;
1158 if (parent
->ref_first
> parent
->ref_last
&&
1159 parent
->ofs_first
> parent
->ofs_last
) {
1161 * This parent has run out of children, so move
1164 list_del(&parent
->list
);
1165 list_add(&parent
->list
, &done_head
);
1169 * Ensure that the parent has data, since we will need
1172 * NEEDSWORK: If parent data needs to be reloaded, this
1173 * prolongs the time that the current thread spends in
1174 * the mutex. A mitigating factor is that parent data
1175 * needs to be reloaded only if the delta base cache
1176 * limit is exceeded, so in the typical case, this does
1179 get_base_data(parent
);
1180 parent
->retain_data
++;
1186 child
= resolve_delta(child_obj
, parent
);
1187 if (!child
->children_remaining
)
1188 FREE_AND_NULL(child
->data
);
1190 child
= make_base(child_obj
, NULL
);
1191 if (child
->children_remaining
) {
1193 * Since this child has its own delta children,
1194 * we will need this data in the future.
1195 * Inflate now so that future iterations will
1196 * have access to this object's data while
1197 * outside the work mutex.
1199 child
->data
= get_data_from_pack(child_obj
);
1200 child
->size
= child_obj
->size
;
1207 parent
->retain_data
--;
1209 if (child
&& child
->data
) {
1211 * This child has its own children, so add it to
1214 list_add(&child
->list
, &work_head
);
1215 base_cache_used
+= child
->size
;
1216 prune_base_data(NULL
);
1217 free_base_data(child
);
1220 * This child does not have its own children. It may be
1221 * the last descendant of its ancestors; free those
1224 struct base_data
*p
= parent
;
1227 struct base_data
*next_p
;
1229 p
->children_remaining
--;
1230 if (p
->children_remaining
)
1240 FREE_AND_NULL(child
);
1249 * - find locations of all objects;
1250 * - calculate SHA1 of all non-delta objects;
1251 * - remember base (SHA1 or offset) for all deltas.
1253 static void parse_pack_objects(unsigned char *hash
)
1255 int i
, nr_delays
= 0;
1256 struct ofs_delta_entry
*ofs_delta
= ofs_deltas
;
1257 struct object_id ref_delta_oid
;
1259 struct git_hash_ctx tmp_ctx
;
1262 progress
= start_progress(
1264 progress_title
? progress_title
:
1265 from_stdin
? _("Receiving objects") : _("Indexing objects"),
1267 for (i
= 0; i
< nr_objects
; i
++) {
1268 struct object_entry
*obj
= &objects
[i
];
1269 void *data
= unpack_raw_entry(obj
, &ofs_delta
->offset
,
1272 obj
->real_type
= obj
->type
;
1273 if (obj
->type
== OBJ_OFS_DELTA
) {
1275 ofs_delta
->obj_no
= i
;
1277 } else if (obj
->type
== OBJ_REF_DELTA
) {
1278 ALLOC_GROW(ref_deltas
, nr_ref_deltas
+ 1, ref_deltas_alloc
);
1279 oidcpy(&ref_deltas
[nr_ref_deltas
].oid
, &ref_delta_oid
);
1280 ref_deltas
[nr_ref_deltas
].obj_no
= i
;
1283 /* large blobs, check later */
1284 obj
->real_type
= OBJ_BAD
;
1287 sha1_object(data
, NULL
, obj
->size
, obj
->type
,
1290 display_progress(progress
, i
+1);
1292 objects
[i
].idx
.offset
= consumed_bytes
;
1293 stop_progress(&progress
);
1295 /* Check pack integrity */
1297 the_hash_algo
->init_fn(&tmp_ctx
);
1298 git_hash_clone(&tmp_ctx
, &input_ctx
);
1299 git_hash_final(hash
, &tmp_ctx
);
1300 if (!hasheq(fill(the_hash_algo
->rawsz
), hash
, the_repository
->hash_algo
))
1301 die(_("pack is corrupted (SHA1 mismatch)"));
1302 use(the_hash_algo
->rawsz
);
1304 /* If input_fd is a file, we should have reached its end now. */
1305 if (fstat(input_fd
, &st
))
1306 die_errno(_("cannot fstat packfile"));
1307 if (S_ISREG(st
.st_mode
) &&
1308 lseek(input_fd
, 0, SEEK_CUR
) - input_len
!= st
.st_size
)
1309 die(_("pack has junk at the end"));
1311 for (i
= 0; i
< nr_objects
; i
++) {
1312 struct object_entry
*obj
= &objects
[i
];
1313 if (obj
->real_type
!= OBJ_BAD
)
1315 obj
->real_type
= obj
->type
;
1316 sha1_object(NULL
, obj
, obj
->size
, obj
->type
,
1321 die(_("confusion beyond insanity in parse_pack_objects()"));
1326 * - for all non-delta objects, look if it is used as a base for
1328 * - if used as a base, uncompress the object and apply all deltas,
1329 * recursively checking if the resulting object is used as a base
1330 * for some more deltas.
1332 static void resolve_deltas(struct pack_idx_option
*opts
)
1336 if (!nr_ofs_deltas
&& !nr_ref_deltas
)
1339 /* Sort deltas by base SHA1/offset for fast searching */
1340 QSORT(ofs_deltas
, nr_ofs_deltas
, compare_ofs_delta_entry
);
1341 QSORT(ref_deltas
, nr_ref_deltas
, compare_ref_delta_entry
);
1343 if (verbose
|| show_resolving_progress
)
1344 progress
= start_progress(the_repository
,
1345 _("Resolving deltas"),
1346 nr_ref_deltas
+ nr_ofs_deltas
);
1349 base_cache_limit
= opts
->delta_base_cache_limit
* nr_threads
;
1350 if (nr_threads
> 1 || getenv("GIT_FORCE_THREADS")) {
1352 for (i
= 0; i
< nr_threads
; i
++) {
1353 int ret
= pthread_create(&thread_data
[i
].thread
, NULL
,
1354 threaded_second_pass
, thread_data
+ i
);
1356 die(_("unable to create thread: %s"),
1359 for (i
= 0; i
< nr_threads
; i
++)
1360 pthread_join(thread_data
[i
].thread
, NULL
);
1364 threaded_second_pass(¬hread_data
);
1369 * - append objects to convert thin pack to full pack if required
1370 * - write the final pack hash
1372 static void fix_unresolved_deltas(struct hashfile
*f
);
1373 static void conclude_pack(int fix_thin_pack
, const char *curr_pack
, unsigned char *pack_hash
)
1375 if (nr_ref_deltas
+ nr_ofs_deltas
== nr_resolved_deltas
) {
1376 stop_progress(&progress
);
1377 /* Flush remaining pack final hash. */
1382 if (fix_thin_pack
) {
1384 unsigned char read_hash
[GIT_MAX_RAWSZ
], tail_hash
[GIT_MAX_RAWSZ
];
1385 struct strbuf msg
= STRBUF_INIT
;
1386 int nr_unresolved
= nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
;
1387 int nr_objects_initial
= nr_objects
;
1388 if (nr_unresolved
<= 0)
1389 die(_("confusion beyond insanity"));
1390 REALLOC_ARRAY(objects
, nr_objects
+ nr_unresolved
+ 1);
1391 memset(objects
+ nr_objects
+ 1, 0,
1392 nr_unresolved
* sizeof(*objects
));
1393 f
= hashfd(the_repository
->hash_algo
, output_fd
, curr_pack
);
1394 fix_unresolved_deltas(f
);
1395 strbuf_addf(&msg
, Q_("completed with %d local object",
1396 "completed with %d local objects",
1397 nr_objects
- nr_objects_initial
),
1398 nr_objects
- nr_objects_initial
);
1399 stop_progress_msg(&progress
, msg
.buf
);
1400 strbuf_release(&msg
);
1401 finalize_hashfile(f
, tail_hash
, FSYNC_COMPONENT_PACK
, 0);
1402 hashcpy(read_hash
, pack_hash
, the_repository
->hash_algo
);
1403 fixup_pack_header_footer(the_hash_algo
, output_fd
, pack_hash
,
1404 curr_pack
, nr_objects
,
1405 read_hash
, consumed_bytes
-the_hash_algo
->rawsz
);
1406 if (!hasheq(read_hash
, tail_hash
, the_repository
->hash_algo
))
1407 die(_("Unexpected tail checksum for %s "
1408 "(disk corruption?)"), curr_pack
);
1410 if (nr_ofs_deltas
+ nr_ref_deltas
!= nr_resolved_deltas
)
1411 die(Q_("pack has %d unresolved delta",
1412 "pack has %d unresolved deltas",
1413 nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
),
1414 nr_ofs_deltas
+ nr_ref_deltas
- nr_resolved_deltas
);
1417 static int write_compressed(struct hashfile
*f
, void *in
, unsigned int size
)
1421 unsigned char outbuf
[4096];
1423 git_deflate_init(&stream
, zlib_compression_level
);
1424 stream
.next_in
= in
;
1425 stream
.avail_in
= size
;
1428 stream
.next_out
= outbuf
;
1429 stream
.avail_out
= sizeof(outbuf
);
1430 status
= git_deflate(&stream
, Z_FINISH
);
1431 hashwrite(f
, outbuf
, sizeof(outbuf
) - stream
.avail_out
);
1432 } while (status
== Z_OK
);
1434 if (status
!= Z_STREAM_END
)
1435 die(_("unable to deflate appended object (%d)"), status
);
1436 size
= stream
.total_out
;
1437 git_deflate_end(&stream
);
1441 static struct object_entry
*append_obj_to_pack(struct hashfile
*f
,
1442 const unsigned char *sha1
, void *buf
,
1443 unsigned long size
, enum object_type type
)
1445 struct object_entry
*obj
= &objects
[nr_objects
++];
1446 unsigned char header
[10];
1447 unsigned long s
= size
;
1449 unsigned char c
= (type
<< 4) | (s
& 15);
1452 header
[n
++] = c
| 0x80;
1458 hashwrite(f
, header
, n
);
1460 obj
[0].hdr_size
= n
;
1462 obj
[0].real_type
= type
;
1463 obj
[1].idx
.offset
= obj
[0].idx
.offset
+ n
;
1464 obj
[1].idx
.offset
+= write_compressed(f
, buf
, size
);
1465 obj
[0].idx
.crc32
= crc32_end(f
);
1467 oidread(&obj
->idx
.oid
, sha1
, the_repository
->hash_algo
);
1471 static int delta_pos_compare(const void *_a
, const void *_b
)
1473 struct ref_delta_entry
*a
= *(struct ref_delta_entry
**)_a
;
1474 struct ref_delta_entry
*b
= *(struct ref_delta_entry
**)_b
;
1475 return a
->obj_no
- b
->obj_no
;
1478 static void fix_unresolved_deltas(struct hashfile
*f
)
1480 struct ref_delta_entry
**sorted_by_pos
;
1484 * Since many unresolved deltas may well be themselves base objects
1485 * for more unresolved deltas, we really want to include the
1486 * smallest number of base objects that would cover as much delta
1487 * as possible by picking the
1488 * trunc deltas first, allowing for other deltas to resolve without
1489 * additional base objects. Since most base objects are to be found
1490 * before deltas depending on them, a good heuristic is to start
1491 * resolving deltas in the same order as their position in the pack.
1493 ALLOC_ARRAY(sorted_by_pos
, nr_ref_deltas
);
1494 for (i
= 0; i
< nr_ref_deltas
; i
++)
1495 sorted_by_pos
[i
] = &ref_deltas
[i
];
1496 QSORT(sorted_by_pos
, nr_ref_deltas
, delta_pos_compare
);
1498 if (repo_has_promisor_remote(the_repository
)) {
1500 * Prefetch the delta bases.
1502 struct oid_array to_fetch
= OID_ARRAY_INIT
;
1503 for (i
= 0; i
< nr_ref_deltas
; i
++) {
1504 struct ref_delta_entry
*d
= sorted_by_pos
[i
];
1505 if (!odb_read_object_info_extended(the_repository
->objects
,
1507 OBJECT_INFO_FOR_PREFETCH
))
1509 oid_array_append(&to_fetch
, &d
->oid
);
1511 promisor_remote_get_direct(the_repository
,
1512 to_fetch
.oid
, to_fetch
.nr
);
1513 oid_array_clear(&to_fetch
);
1516 for (i
= 0; i
< nr_ref_deltas
; i
++) {
1517 struct ref_delta_entry
*d
= sorted_by_pos
[i
];
1518 enum object_type type
;
1522 if (objects
[d
->obj_no
].real_type
!= OBJ_REF_DELTA
)
1524 data
= odb_read_object(the_repository
->objects
, &d
->oid
,
1529 if (check_object_signature(the_repository
, &d
->oid
, data
, size
,
1531 die(_("local object %s is corrupt"), oid_to_hex(&d
->oid
));
1534 * Add this as an object to the objects array and call
1535 * threaded_second_pass() (which will pick up the added
1538 append_obj_to_pack(f
, d
->oid
.hash
, data
, size
, type
);
1540 threaded_second_pass(NULL
);
1542 display_progress(progress
, nr_resolved_deltas
);
1544 free(sorted_by_pos
);
1547 static const char *derive_filename(const char *pack_name
, const char *strip
,
1548 const char *suffix
, struct strbuf
*buf
)
1551 if (!strip_suffix(pack_name
, strip
, &len
) || !len
||
1552 pack_name
[len
- 1] != '.')
1553 die(_("packfile name '%s' does not end with '.%s'"),
1555 strbuf_add(buf
, pack_name
, len
);
1556 strbuf_addstr(buf
, suffix
);
1560 static void write_special_file(const char *suffix
, const char *msg
,
1561 const char *pack_name
, const unsigned char *hash
,
1562 const char **report
)
1564 struct strbuf name_buf
= STRBUF_INIT
;
1565 const char *filename
;
1567 int msg_len
= strlen(msg
);
1570 filename
= derive_filename(pack_name
, "pack", suffix
, &name_buf
);
1572 filename
= odb_pack_name(the_repository
, &name_buf
, hash
, suffix
);
1574 fd
= safe_create_file_with_leading_directories(the_repository
, filename
);
1576 if (errno
!= EEXIST
)
1577 die_errno(_("cannot write %s file '%s'"),
1581 write_or_die(fd
, msg
, msg_len
);
1582 write_or_die(fd
, "\n", 1);
1585 die_errno(_("cannot close written %s file '%s'"),
1590 strbuf_release(&name_buf
);
1593 static void rename_tmp_packfile(const char **final_name
,
1594 const char *curr_name
,
1595 struct strbuf
*name
, unsigned char *hash
,
1596 const char *ext
, int make_read_only_if_same
)
1598 if (!*final_name
|| strcmp(*final_name
, curr_name
)) {
1600 *final_name
= odb_pack_name(the_repository
, name
, hash
, ext
);
1601 if (finalize_object_file(the_repository
, curr_name
, *final_name
))
1602 die(_("unable to rename temporary '*.%s' file to '%s'"),
1604 } else if (make_read_only_if_same
) {
1605 chmod(*final_name
, 0444);
1609 static void final(const char *final_pack_name
, const char *curr_pack_name
,
1610 const char *final_index_name
, const char *curr_index_name
,
1611 const char *final_rev_index_name
, const char *curr_rev_index_name
,
1612 const char *keep_msg
, const char *promisor_msg
,
1613 unsigned char *hash
)
1615 const char *report
= "pack";
1616 struct strbuf pack_name
= STRBUF_INIT
;
1617 struct strbuf index_name
= STRBUF_INIT
;
1618 struct strbuf rev_index_name
= STRBUF_INIT
;
1623 fsync_component_or_die(FSYNC_COMPONENT_PACK
, output_fd
, curr_pack_name
);
1624 if (close(output_fd
))
1625 die_errno(_("error while closing pack file"));
1629 write_special_file("keep", keep_msg
, final_pack_name
, hash
,
1632 write_special_file("promisor", promisor_msg
, final_pack_name
,
1635 rename_tmp_packfile(&final_pack_name
, curr_pack_name
, &pack_name
,
1636 hash
, "pack", from_stdin
);
1637 if (curr_rev_index_name
)
1638 rename_tmp_packfile(&final_rev_index_name
, curr_rev_index_name
,
1639 &rev_index_name
, hash
, "rev", 1);
1640 rename_tmp_packfile(&final_index_name
, curr_index_name
, &index_name
,
1644 packfile_store_load_pack(the_repository
->objects
->packfiles
,
1645 final_index_name
, 0);
1648 printf("%s\n", hash_to_hex(hash
));
1650 struct strbuf buf
= STRBUF_INIT
;
1652 strbuf_addf(&buf
, "%s\t%s\n", report
, hash_to_hex(hash
));
1653 write_or_die(1, buf
.buf
, buf
.len
);
1654 strbuf_release(&buf
);
1656 /* Write the last part of the buffer to stdout */
1657 write_in_full(1, input_buffer
+ input_offset
, input_len
);
1660 strbuf_release(&rev_index_name
);
1661 strbuf_release(&index_name
);
1662 strbuf_release(&pack_name
);
1665 static int git_index_pack_config(const char *k
, const char *v
,
1666 const struct config_context
*ctx
, void *cb
)
1668 struct pack_idx_option
*opts
= cb
;
1670 if (!strcmp(k
, "pack.indexversion")) {
1671 opts
->version
= git_config_int(k
, v
, ctx
->kvi
);
1672 if (opts
->version
> 2)
1673 die(_("bad pack.indexVersion=%"PRIu32
), opts
->version
);
1676 if (!strcmp(k
, "pack.threads")) {
1677 nr_threads
= git_config_int(k
, v
, ctx
->kvi
);
1679 die(_("invalid number of threads specified (%d)"),
1681 if (!HAVE_THREADS
&& nr_threads
!= 1) {
1682 warning(_("no threads support, ignoring %s"), k
);
1687 if (!strcmp(k
, "pack.writereverseindex")) {
1688 if (git_config_bool(k
, v
))
1689 opts
->flags
|= WRITE_REV
;
1691 opts
->flags
&= ~WRITE_REV
;
1693 if (!strcmp(k
, "core.deltabasecachelimit")) {
1694 opts
->delta_base_cache_limit
= git_config_ulong(k
, v
, ctx
->kvi
);
1697 return git_default_config(k
, v
, ctx
, cb
);
1700 static int cmp_uint32(const void *a_
, const void *b_
)
1702 uint32_t a
= *((uint32_t *)a_
);
1703 uint32_t b
= *((uint32_t *)b_
);
1705 return (a
< b
) ? -1 : (a
!= b
);
1708 static void read_v2_anomalous_offsets(struct packed_git
*p
,
1709 struct pack_idx_option
*opts
)
1711 const uint32_t *idx1
, *idx2
;
1714 /* The address of the 4-byte offset table */
1715 idx1
= (((const uint32_t *)((const uint8_t *)p
->index_data
+ p
->crc_offset
))
1716 + (size_t)p
->num_objects
/* CRC32 table */
1719 /* The address of the 8-byte offset table */
1720 idx2
= idx1
+ p
->num_objects
;
1722 for (i
= 0; i
< p
->num_objects
; i
++) {
1723 uint32_t off
= ntohl(idx1
[i
]);
1724 if (!(off
& 0x80000000))
1726 off
= off
& 0x7fffffff;
1727 check_pack_index_ptr(p
, &idx2
[off
* 2]);
1731 * The real offset is ntohl(idx2[off * 2]) in high 4
1732 * octets, and ntohl(idx2[off * 2 + 1]) in low 4
1733 * octets. But idx2[off * 2] is Zero!!!
1735 ALLOC_GROW(opts
->anomaly
, opts
->anomaly_nr
+ 1, opts
->anomaly_alloc
);
1736 opts
->anomaly
[opts
->anomaly_nr
++] = ntohl(idx2
[off
* 2 + 1]);
1739 QSORT(opts
->anomaly
, opts
->anomaly_nr
, cmp_uint32
);
1742 static void read_idx_option(struct pack_idx_option
*opts
, const char *pack_name
)
1744 struct packed_git
*p
= add_packed_git(the_repository
, pack_name
,
1745 strlen(pack_name
), 1);
1748 die(_("Cannot open existing pack file '%s'"), pack_name
);
1749 if (open_pack_index(p
))
1750 die(_("Cannot open existing pack idx file for '%s'"), pack_name
);
1752 /* Read the attributes from the existing idx file */
1753 opts
->version
= p
->index_version
;
1755 if (opts
->version
== 2)
1756 read_v2_anomalous_offsets(p
, opts
);
1759 * Get rid of the idx file as we do not need it anymore.
1760 * NEEDSWORK: extract this bit from free_pack_by_name() in
1761 * object-file.c, perhaps? It shouldn't matter very much as we
1762 * know we haven't installed this pack (hence we never have
1763 * read anything from it).
1765 close_pack_index(p
);
1769 static void show_pack_info(int stat_only
)
1771 int i
, baseobjects
= nr_objects
- nr_ref_deltas
- nr_ofs_deltas
;
1772 unsigned long *chain_histogram
= NULL
;
1775 CALLOC_ARRAY(chain_histogram
, deepest_delta
);
1777 for (i
= 0; i
< nr_objects
; i
++) {
1778 struct object_entry
*obj
= &objects
[i
];
1780 if (is_delta_type(obj
->type
))
1781 chain_histogram
[obj_stat
[i
].delta_depth
- 1]++;
1784 printf("%s %-6s %"PRIuMAX
" %"PRIuMAX
" %"PRIuMAX
,
1785 oid_to_hex(&obj
->idx
.oid
),
1786 type_name(obj
->real_type
), (uintmax_t)obj
->size
,
1787 (uintmax_t)(obj
[1].idx
.offset
- obj
->idx
.offset
),
1788 (uintmax_t)obj
->idx
.offset
);
1789 if (is_delta_type(obj
->type
)) {
1790 struct object_entry
*bobj
= &objects
[obj_stat
[i
].base_object_no
];
1791 printf(" %u %s", obj_stat
[i
].delta_depth
,
1792 oid_to_hex(&bobj
->idx
.oid
));
1798 printf_ln(Q_("non delta: %d object",
1799 "non delta: %d objects",
1802 for (i
= 0; i
< deepest_delta
; i
++) {
1803 if (!chain_histogram
[i
])
1805 printf_ln(Q_("chain length = %d: %lu object",
1806 "chain length = %d: %lu objects",
1807 chain_histogram
[i
]),
1809 chain_histogram
[i
]);
1811 free(chain_histogram
);
1814 static void repack_local_links(void)
1816 struct child_process cmd
= CHILD_PROCESS_INIT
;
1818 struct strbuf line
= STRBUF_INIT
;
1819 struct oidset_iter iter
;
1820 struct object_id
*oid
;
1821 char *base_name
= NULL
;
1823 if (!oidset_size(&outgoing_links
))
1826 oidset_iter_init(&outgoing_links
, &iter
);
1827 while ((oid
= oidset_iter_next(&iter
))) {
1828 struct object_info info
= OBJECT_INFO_INIT
;
1829 if (odb_read_object_info_extended(the_repository
->objects
, oid
, &info
, 0))
1830 /* Missing; assume it is a promisor object */
1832 if (info
.whence
== OI_PACKED
&& info
.u
.packed
.pack
->pack_promisor
)
1836 base_name
= mkpathdup(
1838 repo_get_object_directory(the_repository
));
1839 strvec_push(&cmd
.args
, "pack-objects");
1840 strvec_push(&cmd
.args
,
1841 "--exclude-promisor-objects-best-effort");
1842 strvec_push(&cmd
.args
, base_name
);
1846 if (start_command(&cmd
))
1847 die(_("could not start pack-objects to repack local links"));
1850 if (write_in_full(cmd
.in
, oid_to_hex(oid
), the_hash_algo
->hexsz
) < 0 ||
1851 write_in_full(cmd
.in
, "\n", 1) < 0)
1852 die(_("failed to feed local object to pack-objects"));
1860 out
= xfdopen(cmd
.out
, "r");
1861 while (strbuf_getline_lf(&line
, out
) != EOF
) {
1862 unsigned char binary
[GIT_MAX_RAWSZ
];
1863 if (line
.len
!= the_hash_algo
->hexsz
||
1864 !hex_to_bytes(binary
, line
.buf
, line
.len
))
1865 die(_("index-pack: Expecting full hex object ID lines only from pack-objects."));
1868 * pack-objects creates the .pack and .idx files, but not the
1869 * .promisor file. Create the .promisor file, which is empty.
1871 write_special_file("promisor", "", NULL
, binary
, NULL
);
1875 if (finish_command(&cmd
))
1876 die(_("could not finish pack-objects to repack local links"));
1877 strbuf_release(&line
);
1881 int cmd_index_pack(int argc
,
1884 struct repository
*repo UNUSED
)
1886 int i
, fix_thin_pack
= 0, verify
= 0, stat_only
= 0, rev_index
;
1887 const char *curr_index
;
1888 char *curr_rev_index
= NULL
;
1889 const char *index_name
= NULL
, *pack_name
= NULL
, *rev_index_name
= NULL
;
1890 const char *keep_msg
= NULL
;
1891 const char *promisor_msg
= NULL
;
1892 struct strbuf index_name_buf
= STRBUF_INIT
;
1893 struct strbuf rev_index_name_buf
= STRBUF_INIT
;
1894 struct pack_idx_entry
**idx_objects
;
1895 struct pack_idx_option opts
;
1896 unsigned char pack_hash
[GIT_MAX_RAWSZ
];
1897 unsigned foreign_nr
= 1; /* zero is a "good" value, assume bad */
1898 int report_end_of_input
= 0;
1902 * index-pack never needs to fetch missing objects except when
1903 * REF_DELTA bases are missing (which are explicitly handled). It only
1904 * accesses the repo to do hash collision checks and to check which
1905 * REF_DELTA bases need to be fetched.
1907 fetch_if_missing
= 0;
1909 show_usage_if_asked(argc
, argv
, index_pack_usage
);
1911 disable_replace_refs();
1912 fsck_options
.walk
= mark_link
;
1914 reset_pack_idx_option(&opts
);
1915 opts
.flags
|= WRITE_REV
;
1916 repo_config(the_repository
, git_index_pack_config
, &opts
);
1917 if (prefix
&& chdir(prefix
))
1918 die(_("Cannot come back to cwd"));
1920 if (git_env_bool(GIT_TEST_NO_WRITE_REV_INDEX
, 0))
1923 rev_index
= !!(opts
.flags
& (WRITE_REV_VERIFY
| WRITE_REV
));
1925 for (i
= 1; i
< argc
; i
++) {
1926 const char *arg
= argv
[i
];
1929 if (!strcmp(arg
, "--stdin")) {
1931 } else if (!strcmp(arg
, "--fix-thin")) {
1933 } else if (skip_to_optional_arg(arg
, "--strict", &arg
)) {
1936 fsck_set_msg_types(&fsck_options
, arg
);
1937 } else if (!strcmp(arg
, "--check-self-contained-and-connected")) {
1939 check_self_contained_and_connected
= 1;
1940 } else if (skip_to_optional_arg(arg
, "--fsck-objects", &arg
)) {
1942 fsck_set_msg_types(&fsck_options
, arg
);
1943 } else if (!strcmp(arg
, "--verify")) {
1945 } else if (!strcmp(arg
, "--verify-stat")) {
1948 } else if (!strcmp(arg
, "--verify-stat-only")) {
1952 } else if (skip_to_optional_arg(arg
, "--keep", &keep_msg
)) {
1953 ; /* nothing to do */
1954 } else if (skip_to_optional_arg(arg
, "--promisor", &promisor_msg
)) {
1955 record_outgoing_links
= 1;
1956 } else if (starts_with(arg
, "--threads=")) {
1958 nr_threads
= strtoul(arg
+10, &end
, 0);
1959 if (!arg
[10] || *end
|| nr_threads
< 0)
1960 usage(index_pack_usage
);
1961 if (!HAVE_THREADS
&& nr_threads
!= 1) {
1962 warning(_("no threads support, ignoring %s"), arg
);
1965 } else if (skip_prefix(arg
, "--pack_header=", &arg
)) {
1966 if (parse_pack_header_option(arg
,
1969 die(_("bad --pack_header: %s"), arg
);
1970 } else if (!strcmp(arg
, "-v")) {
1972 } else if (!strcmp(arg
, "--progress-title")) {
1973 if (progress_title
|| (i
+1) >= argc
)
1974 usage(index_pack_usage
);
1975 progress_title
= argv
[++i
];
1976 } else if (!strcmp(arg
, "--show-resolving-progress")) {
1977 show_resolving_progress
= 1;
1978 } else if (!strcmp(arg
, "--report-end-of-input")) {
1979 report_end_of_input
= 1;
1980 } else if (!strcmp(arg
, "-o")) {
1981 if (index_name
|| (i
+1) >= argc
)
1982 usage(index_pack_usage
);
1983 index_name
= argv
[++i
];
1984 } else if (starts_with(arg
, "--index-version=")) {
1986 opts
.version
= strtoul(arg
+ 16, &c
, 10);
1987 if (opts
.version
> 2)
1988 die(_("bad %s"), arg
);
1990 opts
.off32_limit
= strtoul(c
+1, &c
, 0);
1991 if (*c
|| opts
.off32_limit
& 0x80000000)
1992 die(_("bad %s"), arg
);
1993 } else if (skip_prefix(arg
, "--max-input-size=", &arg
)) {
1994 max_input_size
= strtoumax(arg
, NULL
, 10);
1995 } else if (skip_prefix(arg
, "--object-format=", &arg
)) {
1996 hash_algo
= hash_algo_by_name(arg
);
1997 if (hash_algo
== GIT_HASH_UNKNOWN
)
1998 die(_("unknown hash algorithm '%s'"), arg
);
1999 repo_set_hash_algo(the_repository
, hash_algo
);
2000 } else if (!strcmp(arg
, "--rev-index")) {
2002 } else if (!strcmp(arg
, "--no-rev-index")) {
2005 usage(index_pack_usage
);
2010 usage(index_pack_usage
);
2014 if (!pack_name
&& !from_stdin
)
2015 usage(index_pack_usage
);
2016 if (fix_thin_pack
&& !from_stdin
)
2017 die(_("the option '%s' requires '%s'"), "--fix-thin", "--stdin");
2018 if (promisor_msg
&& pack_name
)
2019 die(_("--promisor cannot be used with a pack name"));
2020 if (from_stdin
&& !startup_info
->have_repository
)
2021 die(_("--stdin requires a git repository"));
2022 if (from_stdin
&& hash_algo
)
2023 die(_("options '%s' and '%s' cannot be used together"), "--object-format", "--stdin");
2024 if (!index_name
&& pack_name
)
2025 index_name
= derive_filename(pack_name
, "pack", "idx", &index_name_buf
);
2028 * Packfiles and indices do not carry enough information to be able to
2029 * identify their object hash. So when we are neither in a repository
2030 * nor has the user told us which object hash to use we have no other
2031 * choice but to guess the object hash.
2033 if (!the_repository
->hash_algo
)
2034 repo_set_hash_algo(the_repository
, GIT_HASH_DEFAULT
);
2036 opts
.flags
&= ~(WRITE_REV
| WRITE_REV_VERIFY
);
2038 opts
.flags
|= verify
? WRITE_REV_VERIFY
: WRITE_REV
;
2040 rev_index_name
= derive_filename(index_name
,
2042 &rev_index_name_buf
);
2047 die(_("--verify with no packfile name given"));
2048 read_idx_option(&opts
, index_name
);
2049 opts
.flags
|= WRITE_IDX_VERIFY
| WRITE_IDX_STRICT
;
2052 opts
.flags
|= WRITE_IDX_STRICT
;
2054 if (HAVE_THREADS
&& !nr_threads
) {
2055 nr_threads
= online_cpus();
2057 * Experiments show that going above 20 threads doesn't help,
2058 * no matter how many cores you have. Below that, we tend to
2059 * max at half the number of online_cpus(), presumably because
2060 * half of those are hyperthreads rather than full cores. We'll
2061 * never reduce the level below "3", though, to match a
2062 * historical value that nobody complained about.
2065 ; /* too few cores to consider capping */
2066 else if (nr_threads
< 6)
2067 nr_threads
= 3; /* historic cap */
2068 else if (nr_threads
< 40)
2071 nr_threads
= 20; /* hard cap */
2074 curr_pack
= open_pack_file(pack_name
);
2075 parse_pack_header();
2076 CALLOC_ARRAY(objects
, st_add(nr_objects
, 1));
2078 CALLOC_ARRAY(obj_stat
, st_add(nr_objects
, 1));
2079 CALLOC_ARRAY(ofs_deltas
, nr_objects
);
2080 parse_pack_objects(pack_hash
);
2081 if (report_end_of_input
)
2082 write_in_full(2, "\0", 1);
2083 resolve_deltas(&opts
);
2084 conclude_pack(fix_thin_pack
, curr_pack
, pack_hash
);
2088 foreign_nr
= check_objects();
2091 show_pack_info(stat_only
);
2093 ALLOC_ARRAY(idx_objects
, nr_objects
);
2094 for (i
= 0; i
< nr_objects
; i
++)
2095 idx_objects
[i
] = &objects
[i
].idx
;
2096 curr_index
= write_idx_file(the_repository
, index_name
, idx_objects
,
2097 nr_objects
, &opts
, pack_hash
);
2099 curr_rev_index
= write_rev_file(the_repository
, rev_index_name
,
2100 idx_objects
, nr_objects
,
2101 pack_hash
, opts
.flags
);
2105 final(pack_name
, curr_pack
,
2106 index_name
, curr_index
,
2107 rev_index_name
, curr_rev_index
,
2108 keep_msg
, promisor_msg
,
2113 if (do_fsck_object
&& fsck_finish(&fsck_options
))
2114 die(_("fsck error in pack objects"));
2118 strbuf_release(&index_name_buf
);
2119 strbuf_release(&rev_index_name_buf
);
2121 free((void *) curr_pack
);
2123 free((void *) curr_index
);
2124 free(curr_rev_index
);
2126 repack_local_links();
2129 * Let the caller know this pack is not self contained
2131 if (check_self_contained_and_connected
&& foreign_nr
)