1 #include "git-compat-util.h"
10 #include "object-file.h"
11 #include "object-store-ll.h"
12 #include "hash-lookup.h"
16 #include "run-command.h"
17 #include "repository.h"
18 #include "chunk-format.h"
20 #include "pack-bitmap.h"
23 #include "list-objects.h"
25 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
26 #define MIDX_VERSION 1
27 #define MIDX_BYTE_FILE_VERSION 4
28 #define MIDX_BYTE_HASH_VERSION 5
29 #define MIDX_BYTE_NUM_CHUNKS 6
30 #define MIDX_BYTE_NUM_PACKS 8
31 #define MIDX_HEADER_SIZE 12
32 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
34 #define MIDX_CHUNK_ALIGNMENT 4
35 #define MIDX_CHUNKID_PACKNAMES 0x504e414d /* "PNAM" */
36 #define MIDX_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
37 #define MIDX_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
38 #define MIDX_CHUNKID_OBJECTOFFSETS 0x4f4f4646 /* "OOFF" */
39 #define MIDX_CHUNKID_LARGEOFFSETS 0x4c4f4646 /* "LOFF" */
40 #define MIDX_CHUNKID_REVINDEX 0x52494458 /* "RIDX" */
41 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
42 #define MIDX_CHUNK_OFFSET_WIDTH (2 * sizeof(uint32_t))
43 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
44 #define MIDX_LARGE_OFFSET_NEEDED 0x80000000
46 #define PACK_EXPIRED UINT_MAX
48 const unsigned char *get_midx_checksum(struct multi_pack_index
*m
)
50 return m
->data
+ m
->data_len
- the_hash_algo
->rawsz
;
53 void get_midx_filename(struct strbuf
*out
, const char *object_dir
)
55 strbuf_addf(out
, "%s/pack/multi-pack-index", object_dir
);
58 void get_midx_rev_filename(struct strbuf
*out
, struct multi_pack_index
*m
)
60 get_midx_filename(out
, m
->object_dir
);
61 strbuf_addf(out
, "-%s.rev", hash_to_hex(get_midx_checksum(m
)));
64 static int midx_read_oid_fanout(const unsigned char *chunk_start
,
65 size_t chunk_size
, void *data
)
67 struct multi_pack_index
*m
= data
;
68 m
->chunk_oid_fanout
= (uint32_t *)chunk_start
;
70 if (chunk_size
!= 4 * 256) {
71 error(_("multi-pack-index OID fanout is of the wrong size"));
74 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
78 static int midx_read_oid_lookup(const unsigned char *chunk_start
,
79 size_t chunk_size
, void *data
)
81 struct multi_pack_index
*m
= data
;
82 m
->chunk_oid_lookup
= chunk_start
;
84 if (chunk_size
!= st_mult(m
->hash_len
, m
->num_objects
)) {
85 error(_("multi-pack-index OID lookup chunk is the wrong size"));
91 struct multi_pack_index
*load_multi_pack_index(const char *object_dir
, int local
)
93 struct multi_pack_index
*m
= NULL
;
97 void *midx_map
= NULL
;
98 uint32_t hash_version
;
99 struct strbuf midx_name
= STRBUF_INIT
;
101 const char *cur_pack_name
;
102 struct chunkfile
*cf
= NULL
;
104 get_midx_filename(&midx_name
, object_dir
);
106 fd
= git_open(midx_name
.buf
);
110 if (fstat(fd
, &st
)) {
111 error_errno(_("failed to read %s"), midx_name
.buf
);
115 midx_size
= xsize_t(st
.st_size
);
117 if (midx_size
< MIDX_MIN_SIZE
) {
118 error(_("multi-pack-index file %s is too small"), midx_name
.buf
);
122 strbuf_release(&midx_name
);
124 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
127 FLEX_ALLOC_STR(m
, object_dir
, object_dir
);
129 m
->data_len
= midx_size
;
132 m
->signature
= get_be32(m
->data
);
133 if (m
->signature
!= MIDX_SIGNATURE
)
134 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
135 m
->signature
, MIDX_SIGNATURE
);
137 m
->version
= m
->data
[MIDX_BYTE_FILE_VERSION
];
138 if (m
->version
!= MIDX_VERSION
)
139 die(_("multi-pack-index version %d not recognized"),
142 hash_version
= m
->data
[MIDX_BYTE_HASH_VERSION
];
143 if (hash_version
!= oid_version(the_hash_algo
)) {
144 error(_("multi-pack-index hash version %u does not match version %u"),
145 hash_version
, oid_version(the_hash_algo
));
148 m
->hash_len
= the_hash_algo
->rawsz
;
150 m
->num_chunks
= m
->data
[MIDX_BYTE_NUM_CHUNKS
];
152 m
->num_packs
= get_be32(m
->data
+ MIDX_BYTE_NUM_PACKS
);
154 cf
= init_chunkfile(NULL
);
156 if (read_table_of_contents(cf
, m
->data
, midx_size
,
157 MIDX_HEADER_SIZE
, m
->num_chunks
))
160 if (pair_chunk_unsafe(cf
, MIDX_CHUNKID_PACKNAMES
, &m
->chunk_pack_names
))
161 die(_("multi-pack-index required pack-name chunk missing or corrupted"));
162 if (read_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, midx_read_oid_fanout
, m
))
163 die(_("multi-pack-index required OID fanout chunk missing or corrupted"));
164 if (read_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
, midx_read_oid_lookup
, m
))
165 die(_("multi-pack-index required OID lookup chunk missing or corrupted"));
166 if (pair_chunk_unsafe(cf
, MIDX_CHUNKID_OBJECTOFFSETS
, &m
->chunk_object_offsets
))
167 die(_("multi-pack-index required object offsets chunk missing or corrupted"));
169 pair_chunk_unsafe(cf
, MIDX_CHUNKID_LARGEOFFSETS
, &m
->chunk_large_offsets
);
171 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
172 pair_chunk_unsafe(cf
, MIDX_CHUNKID_REVINDEX
, &m
->chunk_revindex
);
174 CALLOC_ARRAY(m
->pack_names
, m
->num_packs
);
175 CALLOC_ARRAY(m
->packs
, m
->num_packs
);
177 cur_pack_name
= (const char *)m
->chunk_pack_names
;
178 for (i
= 0; i
< m
->num_packs
; i
++) {
179 m
->pack_names
[i
] = cur_pack_name
;
181 cur_pack_name
+= strlen(cur_pack_name
) + 1;
183 if (i
&& strcmp(m
->pack_names
[i
], m
->pack_names
[i
- 1]) <= 0)
184 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
185 m
->pack_names
[i
- 1],
189 trace2_data_intmax("midx", the_repository
, "load/num_packs", m
->num_packs
);
190 trace2_data_intmax("midx", the_repository
, "load/num_objects", m
->num_objects
);
197 strbuf_release(&midx_name
);
200 munmap(midx_map
, midx_size
);
206 void close_midx(struct multi_pack_index
*m
)
215 munmap((unsigned char *)m
->data
, m
->data_len
);
217 for (i
= 0; i
< m
->num_packs
; i
++) {
219 m
->packs
[i
]->multi_pack_index
= 0;
221 FREE_AND_NULL(m
->packs
);
222 FREE_AND_NULL(m
->pack_names
);
226 int prepare_midx_pack(struct repository
*r
, struct multi_pack_index
*m
, uint32_t pack_int_id
)
228 struct strbuf pack_name
= STRBUF_INIT
;
229 struct packed_git
*p
;
231 if (pack_int_id
>= m
->num_packs
)
232 die(_("bad pack-int-id: %u (%u total packs)"),
233 pack_int_id
, m
->num_packs
);
235 if (m
->packs
[pack_int_id
])
238 strbuf_addf(&pack_name
, "%s/pack/%s", m
->object_dir
,
239 m
->pack_names
[pack_int_id
]);
241 p
= add_packed_git(pack_name
.buf
, pack_name
.len
, m
->local
);
242 strbuf_release(&pack_name
);
247 p
->multi_pack_index
= 1;
248 m
->packs
[pack_int_id
] = p
;
249 install_packed_git(r
, p
);
250 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
255 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
, uint32_t *result
)
257 return bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
, m
->chunk_oid_lookup
,
258 the_hash_algo
->rawsz
, result
);
261 struct object_id
*nth_midxed_object_oid(struct object_id
*oid
,
262 struct multi_pack_index
*m
,
265 if (n
>= m
->num_objects
)
268 oidread(oid
, m
->chunk_oid_lookup
+ st_mult(m
->hash_len
, n
));
272 off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
274 const unsigned char *offset_data
;
277 offset_data
= m
->chunk_object_offsets
+ (off_t
)pos
* MIDX_CHUNK_OFFSET_WIDTH
;
278 offset32
= get_be32(offset_data
+ sizeof(uint32_t));
280 if (m
->chunk_large_offsets
&& offset32
& MIDX_LARGE_OFFSET_NEEDED
) {
281 if (sizeof(off_t
) < sizeof(uint64_t))
282 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
284 offset32
^= MIDX_LARGE_OFFSET_NEEDED
;
285 return get_be64(m
->chunk_large_offsets
+
286 st_mult(sizeof(uint64_t), offset32
));
292 uint32_t nth_midxed_pack_int_id(struct multi_pack_index
*m
, uint32_t pos
)
294 return get_be32(m
->chunk_object_offsets
+
295 (off_t
)pos
* MIDX_CHUNK_OFFSET_WIDTH
);
298 int fill_midx_entry(struct repository
*r
,
299 const struct object_id
*oid
,
300 struct pack_entry
*e
,
301 struct multi_pack_index
*m
)
304 uint32_t pack_int_id
;
305 struct packed_git
*p
;
307 if (!bsearch_midx(oid
, m
, &pos
))
310 if (pos
>= m
->num_objects
)
313 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
315 if (prepare_midx_pack(r
, m
, pack_int_id
))
317 p
= m
->packs
[pack_int_id
];
320 * We are about to tell the caller where they can locate the
321 * requested object. We better make sure the packfile is
322 * still here and can be accessed before supplying that
323 * answer, as it may have been deleted since the MIDX was
326 if (!is_pack_valid(p
))
329 if (oidset_size(&p
->bad_objects
) &&
330 oidset_contains(&p
->bad_objects
, oid
))
333 e
->offset
= nth_midxed_offset(m
, pos
);
339 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
340 static int cmp_idx_or_pack_name(const char *idx_or_pack_name
,
341 const char *idx_name
)
343 /* Skip past any initial matching prefix. */
344 while (*idx_name
&& *idx_name
== *idx_or_pack_name
) {
350 * If we didn't match completely, we may have matched "pack-1234." and
351 * be left with "idx" and "pack" respectively, which is also OK. We do
352 * not have to check for "idx" and "idx", because that would have been
353 * a complete match (and in that case these strcmps will be false, but
354 * we'll correctly return 0 from the final strcmp() below.
356 * Technically this matches "fooidx" and "foopack", but we'd never have
357 * such names in the first place.
359 if (!strcmp(idx_name
, "idx") && !strcmp(idx_or_pack_name
, "pack"))
363 * This not only checks for a complete match, but also orders based on
364 * the first non-identical character, which means our ordering will
365 * match a raw strcmp(). That makes it OK to use this to binary search
366 * a naively-sorted list.
368 return strcmp(idx_or_pack_name
, idx_name
);
371 int midx_contains_pack(struct multi_pack_index
*m
, const char *idx_or_pack_name
)
373 uint32_t first
= 0, last
= m
->num_packs
;
375 while (first
< last
) {
376 uint32_t mid
= first
+ (last
- first
) / 2;
380 current
= m
->pack_names
[mid
];
381 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
394 int prepare_multi_pack_index_one(struct repository
*r
, const char *object_dir
, int local
)
396 struct multi_pack_index
*m
;
397 struct multi_pack_index
*m_search
;
399 prepare_repo_settings(r
);
400 if (!r
->settings
.core_multi_pack_index
)
403 for (m_search
= r
->objects
->multi_pack_index
; m_search
; m_search
= m_search
->next
)
404 if (!strcmp(object_dir
, m_search
->object_dir
))
407 m
= load_multi_pack_index(object_dir
, local
);
410 struct multi_pack_index
*mp
= r
->objects
->multi_pack_index
;
415 r
->objects
->multi_pack_index
= m
;
422 static size_t write_midx_header(struct hashfile
*f
,
423 unsigned char num_chunks
,
426 hashwrite_be32(f
, MIDX_SIGNATURE
);
427 hashwrite_u8(f
, MIDX_VERSION
);
428 hashwrite_u8(f
, oid_version(the_hash_algo
));
429 hashwrite_u8(f
, num_chunks
);
430 hashwrite_u8(f
, 0); /* unused */
431 hashwrite_be32(f
, num_packs
);
433 return MIDX_HEADER_SIZE
;
437 uint32_t orig_pack_int_id
;
439 struct packed_git
*p
;
440 unsigned expired
: 1;
443 static int pack_info_compare(const void *_a
, const void *_b
)
445 struct pack_info
*a
= (struct pack_info
*)_a
;
446 struct pack_info
*b
= (struct pack_info
*)_b
;
447 return strcmp(a
->pack_name
, b
->pack_name
);
450 static int idx_or_pack_name_cmp(const void *_va
, const void *_vb
)
452 const char *pack_name
= _va
;
453 const struct pack_info
*compar
= _vb
;
455 return cmp_idx_or_pack_name(pack_name
, compar
->pack_name
);
458 struct write_midx_context
{
459 struct pack_info
*info
;
462 struct multi_pack_index
*m
;
463 struct progress
*progress
;
464 unsigned pack_paths_checked
;
466 struct pack_midx_entry
*entries
;
470 uint32_t *pack_order
;
471 unsigned large_offsets_needed
:1;
472 uint32_t num_large_offsets
;
474 int preferred_pack_idx
;
476 struct string_list
*to_include
;
479 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
480 const char *file_name
, void *data
)
482 struct write_midx_context
*ctx
= data
;
484 if (ends_with(file_name
, ".idx")) {
485 display_progress(ctx
->progress
, ++ctx
->pack_paths_checked
);
487 * Note that at most one of ctx->m and ctx->to_include are set,
488 * so we are testing midx_contains_pack() and
489 * string_list_has_string() independently (guarded by the
490 * appropriate NULL checks).
492 * We could support passing to_include while reusing an existing
493 * MIDX, but don't currently since the reuse process drags
494 * forward all packs from an existing MIDX (without checking
495 * whether or not they appear in the to_include list).
497 * If we added support for that, these next two conditional
498 * should be performed independently (likely checking
499 * to_include before the existing MIDX).
501 if (ctx
->m
&& midx_contains_pack(ctx
->m
, file_name
))
503 else if (ctx
->to_include
&&
504 !string_list_has_string(ctx
->to_include
, file_name
))
507 ALLOC_GROW(ctx
->info
, ctx
->nr
+ 1, ctx
->alloc
);
509 ctx
->info
[ctx
->nr
].p
= add_packed_git(full_path
,
513 if (!ctx
->info
[ctx
->nr
].p
) {
514 warning(_("failed to add packfile '%s'"),
519 if (open_pack_index(ctx
->info
[ctx
->nr
].p
)) {
520 warning(_("failed to open pack-index '%s'"),
522 close_pack(ctx
->info
[ctx
->nr
].p
);
523 FREE_AND_NULL(ctx
->info
[ctx
->nr
].p
);
527 ctx
->info
[ctx
->nr
].pack_name
= xstrdup(file_name
);
528 ctx
->info
[ctx
->nr
].orig_pack_int_id
= ctx
->nr
;
529 ctx
->info
[ctx
->nr
].expired
= 0;
534 struct pack_midx_entry
{
535 struct object_id oid
;
536 uint32_t pack_int_id
;
539 unsigned preferred
: 1;
542 static int midx_oid_compare(const void *_a
, const void *_b
)
544 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
545 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
546 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
551 /* Sort objects in a preferred pack first when multiple copies exist. */
552 if (a
->preferred
> b
->preferred
)
554 if (a
->preferred
< b
->preferred
)
557 if (a
->pack_mtime
> b
->pack_mtime
)
559 else if (a
->pack_mtime
< b
->pack_mtime
)
562 return a
->pack_int_id
- b
->pack_int_id
;
565 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
566 struct pack_midx_entry
*e
,
569 if (pos
>= m
->num_objects
)
572 nth_midxed_object_oid(&e
->oid
, m
, pos
);
573 e
->pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
574 e
->offset
= nth_midxed_offset(m
, pos
);
576 /* consider objects in midx to be from "old" packs */
581 static void fill_pack_entry(uint32_t pack_int_id
,
582 struct packed_git
*p
,
584 struct pack_midx_entry
*entry
,
587 if (nth_packed_object_id(&entry
->oid
, p
, cur_object
) < 0)
588 die(_("failed to locate object %d in packfile"), cur_object
);
590 entry
->pack_int_id
= pack_int_id
;
591 entry
->pack_mtime
= p
->mtime
;
593 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
594 entry
->preferred
= !!preferred
;
598 struct pack_midx_entry
*entries
;
602 static void midx_fanout_grow(struct midx_fanout
*fanout
, size_t nr
)
605 BUG("negative growth in midx_fanout_grow() (%"PRIuMAX
" < %"PRIuMAX
")",
606 (uintmax_t)nr
, (uintmax_t)fanout
->nr
);
607 ALLOC_GROW(fanout
->entries
, nr
, fanout
->alloc
);
610 static void midx_fanout_sort(struct midx_fanout
*fanout
)
612 QSORT(fanout
->entries
, fanout
->nr
, midx_oid_compare
);
615 static void midx_fanout_add_midx_fanout(struct midx_fanout
*fanout
,
616 struct multi_pack_index
*m
,
620 uint32_t start
= 0, end
;
624 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
625 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
627 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
628 if ((preferred_pack
> -1) &&
629 (preferred_pack
== nth_midxed_pack_int_id(m
, cur_object
))) {
631 * Objects from preferred packs are added
637 midx_fanout_grow(fanout
, fanout
->nr
+ 1);
638 nth_midxed_pack_midx_entry(m
,
639 &fanout
->entries
[fanout
->nr
],
641 fanout
->entries
[fanout
->nr
].preferred
= 0;
646 static void midx_fanout_add_pack_fanout(struct midx_fanout
*fanout
,
647 struct pack_info
*info
,
652 struct packed_git
*pack
= info
[cur_pack
].p
;
653 uint32_t start
= 0, end
;
657 start
= get_pack_fanout(pack
, cur_fanout
- 1);
658 end
= get_pack_fanout(pack
, cur_fanout
);
660 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
661 midx_fanout_grow(fanout
, fanout
->nr
+ 1);
662 fill_pack_entry(cur_pack
,
665 &fanout
->entries
[fanout
->nr
],
672 * It is possible to artificially get into a state where there are many
673 * duplicate copies of objects. That can create high memory pressure if
674 * we are to create a list of all objects before de-duplication. To reduce
675 * this memory pressure without a significant performance drop, automatically
676 * group objects by the first byte of their object id. Use the IDX fanout
677 * tables to group the data, copy to a local array, then sort.
679 * Copy only the de-duplicated entries (selected by most-recent modified time
680 * of a packfile containing the object).
682 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
683 struct pack_info
*info
,
688 uint32_t cur_fanout
, cur_pack
, cur_object
;
689 size_t alloc_objects
, total_objects
= 0;
690 struct midx_fanout fanout
= { 0 };
691 struct pack_midx_entry
*deduplicated_entries
= NULL
;
692 uint32_t start_pack
= m
? m
->num_packs
: 0;
694 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
695 total_objects
= st_add(total_objects
,
696 info
[cur_pack
].p
->num_objects
);
699 * As we de-duplicate by fanout value, we expect the fanout
700 * slices to be evenly distributed, with some noise. Hence,
701 * allocate slightly more than one 256th.
703 alloc_objects
= fanout
.alloc
= total_objects
> 3200 ? total_objects
/ 200 : 16;
705 ALLOC_ARRAY(fanout
.entries
, fanout
.alloc
);
706 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
709 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
713 midx_fanout_add_midx_fanout(&fanout
, m
, cur_fanout
,
716 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
717 int preferred
= cur_pack
== preferred_pack
;
718 midx_fanout_add_pack_fanout(&fanout
,
720 preferred
, cur_fanout
);
723 if (-1 < preferred_pack
&& preferred_pack
< start_pack
)
724 midx_fanout_add_pack_fanout(&fanout
, info
,
728 midx_fanout_sort(&fanout
);
731 * The batch is now sorted by OID and then mtime (descending).
732 * Take only the first duplicate.
734 for (cur_object
= 0; cur_object
< fanout
.nr
; cur_object
++) {
735 if (cur_object
&& oideq(&fanout
.entries
[cur_object
- 1].oid
,
736 &fanout
.entries
[cur_object
].oid
))
739 ALLOC_GROW(deduplicated_entries
, st_add(*nr_objects
, 1),
741 memcpy(&deduplicated_entries
[*nr_objects
],
742 &fanout
.entries
[cur_object
],
743 sizeof(struct pack_midx_entry
));
748 free(fanout
.entries
);
749 return deduplicated_entries
;
752 static int write_midx_pack_names(struct hashfile
*f
, void *data
)
754 struct write_midx_context
*ctx
= data
;
756 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
759 for (i
= 0; i
< ctx
->nr
; i
++) {
762 if (ctx
->info
[i
].expired
)
765 if (i
&& strcmp(ctx
->info
[i
].pack_name
, ctx
->info
[i
- 1].pack_name
) <= 0)
766 BUG("incorrect pack-file order: %s before %s",
767 ctx
->info
[i
- 1].pack_name
,
768 ctx
->info
[i
].pack_name
);
770 writelen
= strlen(ctx
->info
[i
].pack_name
) + 1;
771 hashwrite(f
, ctx
->info
[i
].pack_name
, writelen
);
775 /* add padding to be aligned */
776 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
777 if (i
< MIDX_CHUNK_ALIGNMENT
) {
778 memset(padding
, 0, sizeof(padding
));
779 hashwrite(f
, padding
, i
);
785 static int write_midx_oid_fanout(struct hashfile
*f
,
788 struct write_midx_context
*ctx
= data
;
789 struct pack_midx_entry
*list
= ctx
->entries
;
790 struct pack_midx_entry
*last
= ctx
->entries
+ ctx
->entries_nr
;
795 * Write the first-level table (the list is sorted,
796 * but we use a 256-entry lookup to be able to avoid
797 * having to do eight extra binary search iterations).
799 for (i
= 0; i
< 256; i
++) {
800 struct pack_midx_entry
*next
= list
;
802 while (next
< last
&& next
->oid
.hash
[0] == i
) {
807 hashwrite_be32(f
, count
);
814 static int write_midx_oid_lookup(struct hashfile
*f
,
817 struct write_midx_context
*ctx
= data
;
818 unsigned char hash_len
= the_hash_algo
->rawsz
;
819 struct pack_midx_entry
*list
= ctx
->entries
;
822 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
823 struct pack_midx_entry
*obj
= list
++;
825 if (i
< ctx
->entries_nr
- 1) {
826 struct pack_midx_entry
*next
= list
;
827 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
828 BUG("OIDs not in order: %s >= %s",
829 oid_to_hex(&obj
->oid
),
830 oid_to_hex(&next
->oid
));
833 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
839 static int write_midx_object_offsets(struct hashfile
*f
,
842 struct write_midx_context
*ctx
= data
;
843 struct pack_midx_entry
*list
= ctx
->entries
;
844 uint32_t i
, nr_large_offset
= 0;
846 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
847 struct pack_midx_entry
*obj
= list
++;
849 if (ctx
->pack_perm
[obj
->pack_int_id
] == PACK_EXPIRED
)
850 BUG("object %s is in an expired pack with int-id %d",
851 oid_to_hex(&obj
->oid
),
854 hashwrite_be32(f
, ctx
->pack_perm
[obj
->pack_int_id
]);
856 if (ctx
->large_offsets_needed
&& obj
->offset
>> 31)
857 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
858 else if (!ctx
->large_offsets_needed
&& obj
->offset
>> 32)
859 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
860 oid_to_hex(&obj
->oid
),
863 hashwrite_be32(f
, (uint32_t)obj
->offset
);
869 static int write_midx_large_offsets(struct hashfile
*f
,
872 struct write_midx_context
*ctx
= data
;
873 struct pack_midx_entry
*list
= ctx
->entries
;
874 struct pack_midx_entry
*end
= ctx
->entries
+ ctx
->entries_nr
;
875 uint32_t nr_large_offset
= ctx
->num_large_offsets
;
877 while (nr_large_offset
) {
878 struct pack_midx_entry
*obj
;
882 BUG("too many large-offset objects");
885 offset
= obj
->offset
;
890 hashwrite_be64(f
, offset
);
898 static int write_midx_revindex(struct hashfile
*f
,
901 struct write_midx_context
*ctx
= data
;
904 for (i
= 0; i
< ctx
->entries_nr
; i
++)
905 hashwrite_be32(f
, ctx
->pack_order
[i
]);
910 struct midx_pack_order_data
{
916 static int midx_pack_order_cmp(const void *va
, const void *vb
)
918 const struct midx_pack_order_data
*a
= va
, *b
= vb
;
919 if (a
->pack
< b
->pack
)
921 else if (a
->pack
> b
->pack
)
923 else if (a
->offset
< b
->offset
)
925 else if (a
->offset
> b
->offset
)
931 static uint32_t *midx_pack_order(struct write_midx_context
*ctx
)
933 struct midx_pack_order_data
*data
;
934 uint32_t *pack_order
;
937 trace2_region_enter("midx", "midx_pack_order", the_repository
);
939 ALLOC_ARRAY(data
, ctx
->entries_nr
);
940 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
941 struct pack_midx_entry
*e
= &ctx
->entries
[i
];
943 data
[i
].pack
= ctx
->pack_perm
[e
->pack_int_id
];
945 data
[i
].pack
|= (1U << 31);
946 data
[i
].offset
= e
->offset
;
949 QSORT(data
, ctx
->entries_nr
, midx_pack_order_cmp
);
951 ALLOC_ARRAY(pack_order
, ctx
->entries_nr
);
952 for (i
= 0; i
< ctx
->entries_nr
; i
++)
953 pack_order
[i
] = data
[i
].nr
;
956 trace2_region_leave("midx", "midx_pack_order", the_repository
);
961 static void write_midx_reverse_index(char *midx_name
, unsigned char *midx_hash
,
962 struct write_midx_context
*ctx
)
964 struct strbuf buf
= STRBUF_INIT
;
965 const char *tmp_file
;
967 trace2_region_enter("midx", "write_midx_reverse_index", the_repository
);
969 strbuf_addf(&buf
, "%s-%s.rev", midx_name
, hash_to_hex(midx_hash
));
971 tmp_file
= write_rev_file_order(NULL
, ctx
->pack_order
, ctx
->entries_nr
,
972 midx_hash
, WRITE_REV
);
974 if (finalize_object_file(tmp_file
, buf
.buf
))
975 die(_("cannot store reverse index file"));
977 strbuf_release(&buf
);
979 trace2_region_leave("midx", "write_midx_reverse_index", the_repository
);
982 static void clear_midx_files_ext(const char *object_dir
, const char *ext
,
983 unsigned char *keep_hash
);
985 static int midx_checksum_valid(struct multi_pack_index
*m
)
987 return hashfile_checksum_valid(m
->data
, m
->data_len
);
990 static void prepare_midx_packing_data(struct packing_data
*pdata
,
991 struct write_midx_context
*ctx
)
995 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository
);
997 memset(pdata
, 0, sizeof(struct packing_data
));
998 prepare_packing_data(the_repository
, pdata
);
1000 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
1001 struct pack_midx_entry
*from
= &ctx
->entries
[ctx
->pack_order
[i
]];
1002 struct object_entry
*to
= packlist_alloc(pdata
, &from
->oid
);
1004 oe_set_in_pack(pdata
, to
,
1005 ctx
->info
[ctx
->pack_perm
[from
->pack_int_id
]].p
);
1008 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository
);
1011 static int add_ref_to_pending(const char *refname
,
1012 const struct object_id
*oid
,
1013 int flag
, void *cb_data
)
1015 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
1016 struct object_id peeled
;
1017 struct object
*object
;
1019 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
1020 warning("symbolic ref is dangling: %s", refname
);
1024 if (!peel_iterated_oid(oid
, &peeled
))
1027 object
= parse_object_or_die(oid
, refname
);
1028 if (object
->type
!= OBJ_COMMIT
)
1031 add_pending_object(revs
, object
, "");
1032 if (bitmap_is_preferred_refname(revs
->repo
, refname
))
1033 object
->flags
|= NEEDS_BITMAP
;
1037 struct bitmap_commit_cb
{
1038 struct commit
**commits
;
1039 size_t commits_nr
, commits_alloc
;
1041 struct write_midx_context
*ctx
;
1044 static const struct object_id
*bitmap_oid_access(size_t index
,
1045 const void *_entries
)
1047 const struct pack_midx_entry
*entries
= _entries
;
1048 return &entries
[index
].oid
;
1051 static void bitmap_show_commit(struct commit
*commit
, void *_data
)
1053 struct bitmap_commit_cb
*data
= _data
;
1054 int pos
= oid_pos(&commit
->object
.oid
, data
->ctx
->entries
,
1055 data
->ctx
->entries_nr
,
1060 ALLOC_GROW(data
->commits
, data
->commits_nr
+ 1, data
->commits_alloc
);
1061 data
->commits
[data
->commits_nr
++] = commit
;
1064 static int read_refs_snapshot(const char *refs_snapshot
,
1065 struct rev_info
*revs
)
1067 struct strbuf buf
= STRBUF_INIT
;
1068 struct object_id oid
;
1069 FILE *f
= xfopen(refs_snapshot
, "r");
1071 while (strbuf_getline(&buf
, f
) != EOF
) {
1072 struct object
*object
;
1074 char *hex
= buf
.buf
;
1075 const char *end
= NULL
;
1077 if (buf
.len
&& *buf
.buf
== '+') {
1082 if (parse_oid_hex(hex
, &oid
, &end
) < 0)
1083 die(_("could not parse line: %s"), buf
.buf
);
1085 die(_("malformed line: %s"), buf
.buf
);
1087 object
= parse_object_or_die(&oid
, NULL
);
1089 object
->flags
|= NEEDS_BITMAP
;
1091 add_pending_object(revs
, object
, "");
1095 strbuf_release(&buf
);
1099 static struct commit
**find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p
,
1100 const char *refs_snapshot
,
1101 struct write_midx_context
*ctx
)
1103 struct rev_info revs
;
1104 struct bitmap_commit_cb cb
= {0};
1106 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
1111 repo_init_revisions(the_repository
, &revs
, NULL
);
1112 if (refs_snapshot
) {
1113 read_refs_snapshot(refs_snapshot
, &revs
);
1115 setup_revisions(0, NULL
, &revs
, NULL
);
1116 for_each_ref(add_ref_to_pending
, &revs
);
1120 * Skipping promisor objects here is intentional, since it only excludes
1121 * them from the list of reachable commits that we want to select from
1122 * when computing the selection of MIDX'd commits to receive bitmaps.
1124 * Reachability bitmaps do require that their objects be closed under
1125 * reachability, but fetching any objects missing from promisors at this
1126 * point is too late. But, if one of those objects can be reached from
1127 * an another object that is included in the bitmap, then we will
1128 * complain later that we don't have reachability closure (and fail
1131 fetch_if_missing
= 0;
1132 revs
.exclude_promisor_objects
= 1;
1134 if (prepare_revision_walk(&revs
))
1135 die(_("revision walk setup failed"));
1137 traverse_commit_list(&revs
, bitmap_show_commit
, NULL
, &cb
);
1138 if (indexed_commits_nr_p
)
1139 *indexed_commits_nr_p
= cb
.commits_nr
;
1141 release_revisions(&revs
);
1143 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
1149 static int write_midx_bitmap(const char *midx_name
,
1150 const unsigned char *midx_hash
,
1151 struct packing_data
*pdata
,
1152 struct commit
**commits
,
1153 uint32_t commits_nr
,
1154 uint32_t *pack_order
,
1158 uint16_t options
= 0;
1159 struct pack_idx_entry
**index
;
1160 char *bitmap_name
= xstrfmt("%s-%s.bitmap", midx_name
,
1161 hash_to_hex(midx_hash
));
1163 trace2_region_enter("midx", "write_midx_bitmap", the_repository
);
1165 if (flags
& MIDX_WRITE_BITMAP_HASH_CACHE
)
1166 options
|= BITMAP_OPT_HASH_CACHE
;
1168 if (flags
& MIDX_WRITE_BITMAP_LOOKUP_TABLE
)
1169 options
|= BITMAP_OPT_LOOKUP_TABLE
;
1172 * Build the MIDX-order index based on pdata.objects (which is already
1173 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
1176 ALLOC_ARRAY(index
, pdata
->nr_objects
);
1177 for (i
= 0; i
< pdata
->nr_objects
; i
++)
1178 index
[i
] = &pdata
->objects
[i
].idx
;
1180 bitmap_writer_show_progress(flags
& MIDX_PROGRESS
);
1181 bitmap_writer_build_type_index(pdata
, index
, pdata
->nr_objects
);
1184 * bitmap_writer_finish expects objects in lex order, but pack_order
1185 * gives us exactly that. use it directly instead of re-sorting the
1188 * This changes the order of objects in 'index' between
1189 * bitmap_writer_build_type_index and bitmap_writer_finish.
1191 * The same re-ordering takes place in the single-pack bitmap code via
1192 * write_idx_file(), which is called by finish_tmp_packfile(), which
1193 * happens between bitmap_writer_build_type_index() and
1194 * bitmap_writer_finish().
1196 for (i
= 0; i
< pdata
->nr_objects
; i
++)
1197 index
[pack_order
[i
]] = &pdata
->objects
[i
].idx
;
1199 bitmap_writer_select_commits(commits
, commits_nr
, -1);
1200 ret
= bitmap_writer_build(pdata
);
1204 bitmap_writer_set_checksum(midx_hash
);
1205 bitmap_writer_finish(index
, pdata
->nr_objects
, bitmap_name
, options
);
1211 trace2_region_leave("midx", "write_midx_bitmap", the_repository
);
1216 static struct multi_pack_index
*lookup_multi_pack_index(struct repository
*r
,
1217 const char *object_dir
)
1219 struct multi_pack_index
*result
= NULL
;
1220 struct multi_pack_index
*cur
;
1221 char *obj_dir_real
= real_pathdup(object_dir
, 1);
1222 struct strbuf cur_path_real
= STRBUF_INIT
;
1224 /* Ensure the given object_dir is local, or a known alternate. */
1225 find_odb(r
, obj_dir_real
);
1227 for (cur
= get_multi_pack_index(r
); cur
; cur
= cur
->next
) {
1228 strbuf_realpath(&cur_path_real
, cur
->object_dir
, 1);
1229 if (!strcmp(obj_dir_real
, cur_path_real
.buf
)) {
1237 strbuf_release(&cur_path_real
);
1241 static int write_midx_internal(const char *object_dir
,
1242 struct string_list
*packs_to_include
,
1243 struct string_list
*packs_to_drop
,
1244 const char *preferred_pack_name
,
1245 const char *refs_snapshot
,
1248 struct strbuf midx_name
= STRBUF_INIT
;
1249 unsigned char midx_hash
[GIT_MAX_RAWSZ
];
1251 struct hashfile
*f
= NULL
;
1252 struct lock_file lk
;
1253 struct write_midx_context ctx
= { 0 };
1254 int pack_name_concat_len
= 0;
1255 int dropped_packs
= 0;
1257 struct chunkfile
*cf
;
1259 trace2_region_enter("midx", "write_midx_internal", the_repository
);
1261 get_midx_filename(&midx_name
, object_dir
);
1262 if (safe_create_leading_directories(midx_name
.buf
))
1263 die_errno(_("unable to create leading directories of %s"),
1266 if (!packs_to_include
) {
1268 * Only reference an existing MIDX when not filtering which
1269 * packs to include, since all packs and objects are copied
1270 * blindly from an existing MIDX if one is present.
1272 ctx
.m
= lookup_multi_pack_index(the_repository
, object_dir
);
1275 if (ctx
.m
&& !midx_checksum_valid(ctx
.m
)) {
1276 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1281 ctx
.alloc
= ctx
.m
? ctx
.m
->num_packs
: 16;
1283 ALLOC_ARRAY(ctx
.info
, ctx
.alloc
);
1286 for (i
= 0; i
< ctx
.m
->num_packs
; i
++) {
1287 ALLOC_GROW(ctx
.info
, ctx
.nr
+ 1, ctx
.alloc
);
1289 ctx
.info
[ctx
.nr
].orig_pack_int_id
= i
;
1290 ctx
.info
[ctx
.nr
].pack_name
= xstrdup(ctx
.m
->pack_names
[i
]);
1291 ctx
.info
[ctx
.nr
].p
= ctx
.m
->packs
[i
];
1292 ctx
.info
[ctx
.nr
].expired
= 0;
1294 if (flags
& MIDX_WRITE_REV_INDEX
) {
1296 * If generating a reverse index, need to have
1297 * packed_git's loaded to compare their
1298 * mtimes and object count.
1300 if (prepare_midx_pack(the_repository
, ctx
.m
, i
)) {
1301 error(_("could not load pack"));
1306 if (open_pack_index(ctx
.m
->packs
[i
]))
1307 die(_("could not open index for %s"),
1308 ctx
.m
->packs
[i
]->pack_name
);
1309 ctx
.info
[ctx
.nr
].p
= ctx
.m
->packs
[i
];
1316 ctx
.pack_paths_checked
= 0;
1317 if (flags
& MIDX_PROGRESS
)
1318 ctx
.progress
= start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
1320 ctx
.progress
= NULL
;
1322 ctx
.to_include
= packs_to_include
;
1324 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &ctx
);
1325 stop_progress(&ctx
.progress
);
1327 if ((ctx
.m
&& ctx
.nr
== ctx
.m
->num_packs
) &&
1328 !(packs_to_include
|| packs_to_drop
)) {
1329 struct bitmap_index
*bitmap_git
;
1331 int want_bitmap
= flags
& MIDX_WRITE_BITMAP
;
1333 bitmap_git
= prepare_midx_bitmap_git(ctx
.m
);
1334 bitmap_exists
= bitmap_git
&& bitmap_is_midx(bitmap_git
);
1335 free_bitmap_index(bitmap_git
);
1337 if (bitmap_exists
|| !want_bitmap
) {
1339 * The correct MIDX already exists, and so does a
1340 * corresponding bitmap (or one wasn't requested).
1343 clear_midx_files_ext(object_dir
, ".bitmap",
1349 if (preferred_pack_name
) {
1350 ctx
.preferred_pack_idx
= -1;
1352 for (i
= 0; i
< ctx
.nr
; i
++) {
1353 if (!cmp_idx_or_pack_name(preferred_pack_name
,
1354 ctx
.info
[i
].pack_name
)) {
1355 ctx
.preferred_pack_idx
= i
;
1360 if (ctx
.preferred_pack_idx
== -1)
1361 warning(_("unknown preferred pack: '%s'"),
1362 preferred_pack_name
);
1363 } else if (ctx
.nr
&&
1364 (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
))) {
1365 struct packed_git
*oldest
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1366 ctx
.preferred_pack_idx
= 0;
1368 if (packs_to_drop
&& packs_to_drop
->nr
)
1369 BUG("cannot write a MIDX bitmap during expiration");
1372 * set a preferred pack when writing a bitmap to ensure that
1373 * the pack from which the first object is selected in pseudo
1374 * pack-order has all of its objects selected from that pack
1375 * (and not another pack containing a duplicate)
1377 for (i
= 1; i
< ctx
.nr
; i
++) {
1378 struct packed_git
*p
= ctx
.info
[i
].p
;
1380 if (!oldest
->num_objects
|| p
->mtime
< oldest
->mtime
) {
1382 ctx
.preferred_pack_idx
= i
;
1386 if (!oldest
->num_objects
) {
1388 * If all packs are empty; unset the preferred index.
1389 * This is acceptable since there will be no duplicate
1390 * objects to resolve, so the preferred value doesn't
1393 ctx
.preferred_pack_idx
= -1;
1397 * otherwise don't mark any pack as preferred to avoid
1398 * interfering with expiration logic below
1400 ctx
.preferred_pack_idx
= -1;
1403 if (ctx
.preferred_pack_idx
> -1) {
1404 struct packed_git
*preferred
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1405 if (!preferred
->num_objects
) {
1406 error(_("cannot select preferred pack %s with no objects"),
1407 preferred
->pack_name
);
1413 ctx
.entries
= get_sorted_entries(ctx
.m
, ctx
.info
, ctx
.nr
, &ctx
.entries_nr
,
1414 ctx
.preferred_pack_idx
);
1416 ctx
.large_offsets_needed
= 0;
1417 for (i
= 0; i
< ctx
.entries_nr
; i
++) {
1418 if (ctx
.entries
[i
].offset
> 0x7fffffff)
1419 ctx
.num_large_offsets
++;
1420 if (ctx
.entries
[i
].offset
> 0xffffffff)
1421 ctx
.large_offsets_needed
= 1;
1424 QSORT(ctx
.info
, ctx
.nr
, pack_info_compare
);
1426 if (packs_to_drop
&& packs_to_drop
->nr
) {
1428 int missing_drops
= 0;
1430 for (i
= 0; i
< ctx
.nr
&& drop_index
< packs_to_drop
->nr
; i
++) {
1431 int cmp
= strcmp(ctx
.info
[i
].pack_name
,
1432 packs_to_drop
->items
[drop_index
].string
);
1436 ctx
.info
[i
].expired
= 1;
1437 } else if (cmp
> 0) {
1438 error(_("did not see pack-file %s to drop"),
1439 packs_to_drop
->items
[drop_index
].string
);
1444 ctx
.info
[i
].expired
= 0;
1448 if (missing_drops
) {
1455 * pack_perm stores a permutation between pack-int-ids from the
1456 * previous multi-pack-index to the new one we are writing:
1458 * pack_perm[old_id] = new_id
1460 ALLOC_ARRAY(ctx
.pack_perm
, ctx
.nr
);
1461 for (i
= 0; i
< ctx
.nr
; i
++) {
1462 if (ctx
.info
[i
].expired
) {
1464 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = PACK_EXPIRED
;
1466 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = i
- dropped_packs
;
1470 for (i
= 0; i
< ctx
.nr
; i
++) {
1471 if (!ctx
.info
[i
].expired
)
1472 pack_name_concat_len
+= strlen(ctx
.info
[i
].pack_name
) + 1;
1475 /* Check that the preferred pack wasn't expired (if given). */
1476 if (preferred_pack_name
) {
1477 struct pack_info
*preferred
= bsearch(preferred_pack_name
,
1480 idx_or_pack_name_cmp
);
1482 uint32_t perm
= ctx
.pack_perm
[preferred
->orig_pack_int_id
];
1483 if (perm
== PACK_EXPIRED
)
1484 warning(_("preferred pack '%s' is expired"),
1485 preferred_pack_name
);
1489 if (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
1490 pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
1491 (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
1493 hold_lock_file_for_update(&lk
, midx_name
.buf
, LOCK_DIE_ON_ERROR
);
1494 f
= hashfd(get_lock_file_fd(&lk
), get_lock_file_path(&lk
));
1496 if (ctx
.nr
- dropped_packs
== 0) {
1497 error(_("no pack files to index."));
1502 if (!ctx
.entries_nr
) {
1503 if (flags
& MIDX_WRITE_BITMAP
)
1504 warning(_("refusing to write multi-pack .bitmap without any objects"));
1505 flags
&= ~(MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
);
1508 cf
= init_chunkfile(f
);
1510 add_chunk(cf
, MIDX_CHUNKID_PACKNAMES
, pack_name_concat_len
,
1511 write_midx_pack_names
);
1512 add_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, MIDX_CHUNK_FANOUT_SIZE
,
1513 write_midx_oid_fanout
);
1514 add_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
,
1515 st_mult(ctx
.entries_nr
, the_hash_algo
->rawsz
),
1516 write_midx_oid_lookup
);
1517 add_chunk(cf
, MIDX_CHUNKID_OBJECTOFFSETS
,
1518 st_mult(ctx
.entries_nr
, MIDX_CHUNK_OFFSET_WIDTH
),
1519 write_midx_object_offsets
);
1521 if (ctx
.large_offsets_needed
)
1522 add_chunk(cf
, MIDX_CHUNKID_LARGEOFFSETS
,
1523 st_mult(ctx
.num_large_offsets
,
1524 MIDX_CHUNK_LARGE_OFFSET_WIDTH
),
1525 write_midx_large_offsets
);
1527 if (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
)) {
1528 ctx
.pack_order
= midx_pack_order(&ctx
);
1529 add_chunk(cf
, MIDX_CHUNKID_REVINDEX
,
1530 st_mult(ctx
.entries_nr
, sizeof(uint32_t)),
1531 write_midx_revindex
);
1534 write_midx_header(f
, get_num_chunks(cf
), ctx
.nr
- dropped_packs
);
1535 write_chunkfile(cf
, &ctx
);
1537 finalize_hashfile(f
, midx_hash
, FSYNC_COMPONENT_PACK_METADATA
,
1538 CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
1541 if (flags
& MIDX_WRITE_REV_INDEX
&&
1542 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1543 write_midx_reverse_index(midx_name
.buf
, midx_hash
, &ctx
);
1545 if (flags
& MIDX_WRITE_BITMAP
) {
1546 struct packing_data pdata
;
1547 struct commit
**commits
;
1548 uint32_t commits_nr
;
1550 if (!ctx
.entries_nr
)
1551 BUG("cannot write a bitmap without any objects");
1553 prepare_midx_packing_data(&pdata
, &ctx
);
1555 commits
= find_commits_for_midx_bitmap(&commits_nr
, refs_snapshot
, &ctx
);
1558 * The previous steps translated the information from
1559 * 'entries' into information suitable for constructing
1560 * bitmaps. We no longer need that array, so clear it to
1561 * reduce memory pressure.
1563 FREE_AND_NULL(ctx
.entries
);
1566 if (write_midx_bitmap(midx_name
.buf
, midx_hash
, &pdata
,
1567 commits
, commits_nr
, ctx
.pack_order
,
1569 error(_("could not write multi-pack bitmap"));
1575 * NOTE: Do not use ctx.entries beyond this point, since it might
1576 * have been freed in the previous if block.
1580 close_object_store(the_repository
->objects
);
1582 if (commit_lock_file(&lk
) < 0)
1583 die_errno(_("could not write multi-pack-index"));
1585 clear_midx_files_ext(object_dir
, ".bitmap", midx_hash
);
1586 clear_midx_files_ext(object_dir
, ".rev", midx_hash
);
1589 for (i
= 0; i
< ctx
.nr
; i
++) {
1590 if (ctx
.info
[i
].p
) {
1591 close_pack(ctx
.info
[i
].p
);
1592 free(ctx
.info
[i
].p
);
1594 free(ctx
.info
[i
].pack_name
);
1599 free(ctx
.pack_perm
);
1600 free(ctx
.pack_order
);
1601 strbuf_release(&midx_name
);
1603 trace2_region_leave("midx", "write_midx_internal", the_repository
);
1608 int write_midx_file(const char *object_dir
,
1609 const char *preferred_pack_name
,
1610 const char *refs_snapshot
,
1613 return write_midx_internal(object_dir
, NULL
, NULL
, preferred_pack_name
,
1614 refs_snapshot
, flags
);
1617 int write_midx_file_only(const char *object_dir
,
1618 struct string_list
*packs_to_include
,
1619 const char *preferred_pack_name
,
1620 const char *refs_snapshot
,
1623 return write_midx_internal(object_dir
, packs_to_include
, NULL
,
1624 preferred_pack_name
, refs_snapshot
, flags
);
1627 struct clear_midx_data
{
1632 static void clear_midx_file_ext(const char *full_path
, size_t full_path_len UNUSED
,
1633 const char *file_name
, void *_data
)
1635 struct clear_midx_data
*data
= _data
;
1637 if (!(starts_with(file_name
, "multi-pack-index-") &&
1638 ends_with(file_name
, data
->ext
)))
1640 if (data
->keep
&& !strcmp(data
->keep
, file_name
))
1643 if (unlink(full_path
))
1644 die_errno(_("failed to remove %s"), full_path
);
1647 static void clear_midx_files_ext(const char *object_dir
, const char *ext
,
1648 unsigned char *keep_hash
)
1650 struct clear_midx_data data
;
1651 memset(&data
, 0, sizeof(struct clear_midx_data
));
1654 data
.keep
= xstrfmt("multi-pack-index-%s%s",
1655 hash_to_hex(keep_hash
), ext
);
1658 for_each_file_in_pack_dir(object_dir
,
1659 clear_midx_file_ext
,
1665 void clear_midx_file(struct repository
*r
)
1667 struct strbuf midx
= STRBUF_INIT
;
1669 get_midx_filename(&midx
, r
->objects
->odb
->path
);
1671 if (r
->objects
&& r
->objects
->multi_pack_index
) {
1672 close_midx(r
->objects
->multi_pack_index
);
1673 r
->objects
->multi_pack_index
= NULL
;
1676 if (remove_path(midx
.buf
))
1677 die(_("failed to clear multi-pack-index at %s"), midx
.buf
);
1679 clear_midx_files_ext(r
->objects
->odb
->path
, ".bitmap", NULL
);
1680 clear_midx_files_ext(r
->objects
->odb
->path
, ".rev", NULL
);
1682 strbuf_release(&midx
);
1685 static int verify_midx_error
;
1687 __attribute__((format (printf
, 1, 2)))
1688 static void midx_report(const char *fmt
, ...)
1691 verify_midx_error
= 1;
1693 vfprintf(stderr
, fmt
, ap
);
1694 fprintf(stderr
, "\n");
1698 struct pair_pos_vs_id
1701 uint32_t pack_int_id
;
1704 static int compare_pair_pos_vs_id(const void *_a
, const void *_b
)
1706 struct pair_pos_vs_id
*a
= (struct pair_pos_vs_id
*)_a
;
1707 struct pair_pos_vs_id
*b
= (struct pair_pos_vs_id
*)_b
;
1709 return b
->pack_int_id
- a
->pack_int_id
;
1713 * Limit calls to display_progress() for performance reasons.
1714 * The interval here was arbitrarily chosen.
1716 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
1717 #define midx_display_sparse_progress(progress, n) \
1719 uint64_t _n = (n); \
1720 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1721 display_progress(progress, _n); \
1724 int verify_midx_file(struct repository
*r
, const char *object_dir
, unsigned flags
)
1726 struct pair_pos_vs_id
*pairs
= NULL
;
1728 struct progress
*progress
= NULL
;
1729 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1730 verify_midx_error
= 0;
1735 struct strbuf filename
= STRBUF_INIT
;
1737 get_midx_filename(&filename
, object_dir
);
1739 if (!stat(filename
.buf
, &sb
)) {
1740 error(_("multi-pack-index file exists, but failed to parse"));
1743 strbuf_release(&filename
);
1747 if (!midx_checksum_valid(m
))
1748 midx_report(_("incorrect checksum"));
1750 if (flags
& MIDX_PROGRESS
)
1751 progress
= start_delayed_progress(_("Looking for referenced packfiles"),
1753 for (i
= 0; i
< m
->num_packs
; i
++) {
1754 if (prepare_midx_pack(r
, m
, i
))
1755 midx_report("failed to load pack in position %d", i
);
1757 display_progress(progress
, i
+ 1);
1759 stop_progress(&progress
);
1761 for (i
= 0; i
< 255; i
++) {
1762 uint32_t oid_fanout1
= ntohl(m
->chunk_oid_fanout
[i
]);
1763 uint32_t oid_fanout2
= ntohl(m
->chunk_oid_fanout
[i
+ 1]);
1765 if (oid_fanout1
> oid_fanout2
)
1766 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32
" > %"PRIx32
" = fanout[%d]"),
1767 i
, oid_fanout1
, oid_fanout2
, i
+ 1);
1770 if (m
->num_objects
== 0) {
1771 midx_report(_("the midx contains no oid"));
1773 * Remaining tests assume that we have objects, so we can
1779 if (flags
& MIDX_PROGRESS
)
1780 progress
= start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1781 m
->num_objects
- 1);
1782 for (i
= 0; i
< m
->num_objects
- 1; i
++) {
1783 struct object_id oid1
, oid2
;
1785 nth_midxed_object_oid(&oid1
, m
, i
);
1786 nth_midxed_object_oid(&oid2
, m
, i
+ 1);
1788 if (oidcmp(&oid1
, &oid2
) >= 0)
1789 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1790 i
, oid_to_hex(&oid1
), oid_to_hex(&oid2
), i
+ 1);
1792 midx_display_sparse_progress(progress
, i
+ 1);
1794 stop_progress(&progress
);
1797 * Create an array mapping each object to its packfile id. Sort it
1798 * to group the objects by packfile. Use this permutation to visit
1799 * each of the objects and only require 1 packfile to be open at a
1802 ALLOC_ARRAY(pairs
, m
->num_objects
);
1803 for (i
= 0; i
< m
->num_objects
; i
++) {
1805 pairs
[i
].pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1808 if (flags
& MIDX_PROGRESS
)
1809 progress
= start_sparse_progress(_("Sorting objects by packfile"),
1811 display_progress(progress
, 0); /* TODO: Measure QSORT() progress */
1812 QSORT(pairs
, m
->num_objects
, compare_pair_pos_vs_id
);
1813 stop_progress(&progress
);
1815 if (flags
& MIDX_PROGRESS
)
1816 progress
= start_sparse_progress(_("Verifying object offsets"), m
->num_objects
);
1817 for (i
= 0; i
< m
->num_objects
; i
++) {
1818 struct object_id oid
;
1819 struct pack_entry e
;
1820 off_t m_offset
, p_offset
;
1822 if (i
> 0 && pairs
[i
-1].pack_int_id
!= pairs
[i
].pack_int_id
&&
1823 m
->packs
[pairs
[i
-1].pack_int_id
])
1825 close_pack_fd(m
->packs
[pairs
[i
-1].pack_int_id
]);
1826 close_pack_index(m
->packs
[pairs
[i
-1].pack_int_id
]);
1829 nth_midxed_object_oid(&oid
, m
, pairs
[i
].pos
);
1831 if (!fill_midx_entry(r
, &oid
, &e
, m
)) {
1832 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1833 pairs
[i
].pos
, oid_to_hex(&oid
));
1837 if (open_pack_index(e
.p
)) {
1838 midx_report(_("failed to load pack-index for packfile %s"),
1843 m_offset
= e
.offset
;
1844 p_offset
= find_pack_entry_one(oid
.hash
, e
.p
);
1846 if (m_offset
!= p_offset
)
1847 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64
" != %"PRIx64
),
1848 pairs
[i
].pos
, oid_to_hex(&oid
), m_offset
, p_offset
);
1850 midx_display_sparse_progress(progress
, i
+ 1);
1852 stop_progress(&progress
);
1858 return verify_midx_error
;
1861 int expire_midx_packs(struct repository
*r
, const char *object_dir
, unsigned flags
)
1863 uint32_t i
, *count
, result
= 0;
1864 struct string_list packs_to_drop
= STRING_LIST_INIT_DUP
;
1865 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
1866 struct progress
*progress
= NULL
;
1871 CALLOC_ARRAY(count
, m
->num_packs
);
1873 if (flags
& MIDX_PROGRESS
)
1874 progress
= start_delayed_progress(_("Counting referenced objects"),
1876 for (i
= 0; i
< m
->num_objects
; i
++) {
1877 int pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1878 count
[pack_int_id
]++;
1879 display_progress(progress
, i
+ 1);
1881 stop_progress(&progress
);
1883 if (flags
& MIDX_PROGRESS
)
1884 progress
= start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1886 for (i
= 0; i
< m
->num_packs
; i
++) {
1888 display_progress(progress
, i
+ 1);
1893 if (prepare_midx_pack(r
, m
, i
))
1896 if (m
->packs
[i
]->pack_keep
|| m
->packs
[i
]->is_cruft
)
1899 pack_name
= xstrdup(m
->packs
[i
]->pack_name
);
1900 close_pack(m
->packs
[i
]);
1902 string_list_insert(&packs_to_drop
, m
->pack_names
[i
]);
1903 unlink_pack_path(pack_name
, 0);
1906 stop_progress(&progress
);
1910 if (packs_to_drop
.nr
)
1911 result
= write_midx_internal(object_dir
, NULL
, &packs_to_drop
, NULL
, NULL
, flags
);
1913 string_list_clear(&packs_to_drop
, 0);
1918 struct repack_info
{
1920 uint32_t referenced_objects
;
1921 uint32_t pack_int_id
;
1924 static int compare_by_mtime(const void *a_
, const void *b_
)
1926 const struct repack_info
*a
, *b
;
1928 a
= (const struct repack_info
*)a_
;
1929 b
= (const struct repack_info
*)b_
;
1931 if (a
->mtime
< b
->mtime
)
1933 if (a
->mtime
> b
->mtime
)
1938 static int fill_included_packs_all(struct repository
*r
,
1939 struct multi_pack_index
*m
,
1940 unsigned char *include_pack
)
1942 uint32_t i
, count
= 0;
1943 int pack_kept_objects
= 0;
1945 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1947 for (i
= 0; i
< m
->num_packs
; i
++) {
1948 if (prepare_midx_pack(r
, m
, i
))
1950 if (!pack_kept_objects
&& m
->packs
[i
]->pack_keep
)
1952 if (m
->packs
[i
]->is_cruft
)
1955 include_pack
[i
] = 1;
1962 static int fill_included_packs_batch(struct repository
*r
,
1963 struct multi_pack_index
*m
,
1964 unsigned char *include_pack
,
1967 uint32_t i
, packs_to_repack
;
1969 struct repack_info
*pack_info
;
1970 int pack_kept_objects
= 0;
1972 CALLOC_ARRAY(pack_info
, m
->num_packs
);
1974 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1976 for (i
= 0; i
< m
->num_packs
; i
++) {
1977 pack_info
[i
].pack_int_id
= i
;
1979 if (prepare_midx_pack(r
, m
, i
))
1982 pack_info
[i
].mtime
= m
->packs
[i
]->mtime
;
1985 for (i
= 0; i
< m
->num_objects
; i
++) {
1986 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1987 pack_info
[pack_int_id
].referenced_objects
++;
1990 QSORT(pack_info
, m
->num_packs
, compare_by_mtime
);
1993 packs_to_repack
= 0;
1994 for (i
= 0; total_size
< batch_size
&& i
< m
->num_packs
; i
++) {
1995 int pack_int_id
= pack_info
[i
].pack_int_id
;
1996 struct packed_git
*p
= m
->packs
[pack_int_id
];
1997 size_t expected_size
;
2001 if (!pack_kept_objects
&& p
->pack_keep
)
2005 if (open_pack_index(p
) || !p
->num_objects
)
2008 expected_size
= st_mult(p
->pack_size
,
2009 pack_info
[i
].referenced_objects
);
2010 expected_size
/= p
->num_objects
;
2012 if (expected_size
>= batch_size
)
2016 total_size
+= expected_size
;
2017 include_pack
[pack_int_id
] = 1;
2022 if (packs_to_repack
< 2)
2028 int midx_repack(struct repository
*r
, const char *object_dir
, size_t batch_size
, unsigned flags
)
2032 unsigned char *include_pack
;
2033 struct child_process cmd
= CHILD_PROCESS_INIT
;
2035 struct strbuf base_name
= STRBUF_INIT
;
2036 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
2039 * When updating the default for these configuration
2040 * variables in builtin/repack.c, these must be adjusted
2043 int delta_base_offset
= 1;
2044 int use_delta_islands
= 0;
2049 CALLOC_ARRAY(include_pack
, m
->num_packs
);
2052 if (fill_included_packs_batch(r
, m
, include_pack
, batch_size
))
2054 } else if (fill_included_packs_all(r
, m
, include_pack
))
2057 repo_config_get_bool(r
, "repack.usedeltabaseoffset", &delta_base_offset
);
2058 repo_config_get_bool(r
, "repack.usedeltaislands", &use_delta_islands
);
2060 strvec_push(&cmd
.args
, "pack-objects");
2062 strbuf_addstr(&base_name
, object_dir
);
2063 strbuf_addstr(&base_name
, "/pack/pack");
2064 strvec_push(&cmd
.args
, base_name
.buf
);
2066 if (delta_base_offset
)
2067 strvec_push(&cmd
.args
, "--delta-base-offset");
2068 if (use_delta_islands
)
2069 strvec_push(&cmd
.args
, "--delta-islands");
2071 if (flags
& MIDX_PROGRESS
)
2072 strvec_push(&cmd
.args
, "--progress");
2074 strvec_push(&cmd
.args
, "-q");
2076 strbuf_release(&base_name
);
2079 cmd
.in
= cmd
.out
= -1;
2081 if (start_command(&cmd
)) {
2082 error(_("could not start pack-objects"));
2087 cmd_in
= xfdopen(cmd
.in
, "w");
2089 for (i
= 0; i
< m
->num_objects
; i
++) {
2090 struct object_id oid
;
2091 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
2093 if (!include_pack
[pack_int_id
])
2096 nth_midxed_object_oid(&oid
, m
, i
);
2097 fprintf(cmd_in
, "%s\n", oid_to_hex(&oid
));
2101 if (finish_command(&cmd
)) {
2102 error(_("could not finish pack-objects"));
2107 result
= write_midx_internal(object_dir
, NULL
, NULL
, NULL
, NULL
, flags
);