1 #include "git-compat-util.h"
11 #include "object-store.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"));
77 struct multi_pack_index
*load_multi_pack_index(const char *object_dir
, int local
)
79 struct multi_pack_index
*m
= NULL
;
83 void *midx_map
= NULL
;
84 uint32_t hash_version
;
85 struct strbuf midx_name
= STRBUF_INIT
;
87 const char *cur_pack_name
;
88 struct chunkfile
*cf
= NULL
;
90 get_midx_filename(&midx_name
, object_dir
);
92 fd
= git_open(midx_name
.buf
);
97 error_errno(_("failed to read %s"), midx_name
.buf
);
101 midx_size
= xsize_t(st
.st_size
);
103 if (midx_size
< MIDX_MIN_SIZE
) {
104 error(_("multi-pack-index file %s is too small"), midx_name
.buf
);
108 strbuf_release(&midx_name
);
110 midx_map
= xmmap(NULL
, midx_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
113 FLEX_ALLOC_STR(m
, object_dir
, object_dir
);
115 m
->data_len
= midx_size
;
118 m
->signature
= get_be32(m
->data
);
119 if (m
->signature
!= MIDX_SIGNATURE
)
120 die(_("multi-pack-index signature 0x%08x does not match signature 0x%08x"),
121 m
->signature
, MIDX_SIGNATURE
);
123 m
->version
= m
->data
[MIDX_BYTE_FILE_VERSION
];
124 if (m
->version
!= MIDX_VERSION
)
125 die(_("multi-pack-index version %d not recognized"),
128 hash_version
= m
->data
[MIDX_BYTE_HASH_VERSION
];
129 if (hash_version
!= oid_version(the_hash_algo
)) {
130 error(_("multi-pack-index hash version %u does not match version %u"),
131 hash_version
, oid_version(the_hash_algo
));
134 m
->hash_len
= the_hash_algo
->rawsz
;
136 m
->num_chunks
= m
->data
[MIDX_BYTE_NUM_CHUNKS
];
138 m
->num_packs
= get_be32(m
->data
+ MIDX_BYTE_NUM_PACKS
);
140 cf
= init_chunkfile(NULL
);
142 if (read_table_of_contents(cf
, m
->data
, midx_size
,
143 MIDX_HEADER_SIZE
, m
->num_chunks
))
146 if (pair_chunk(cf
, MIDX_CHUNKID_PACKNAMES
, &m
->chunk_pack_names
) == CHUNK_NOT_FOUND
)
147 die(_("multi-pack-index missing required pack-name chunk"));
148 if (read_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, midx_read_oid_fanout
, m
) == CHUNK_NOT_FOUND
)
149 die(_("multi-pack-index missing required OID fanout chunk"));
150 if (pair_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
, &m
->chunk_oid_lookup
) == CHUNK_NOT_FOUND
)
151 die(_("multi-pack-index missing required OID lookup chunk"));
152 if (pair_chunk(cf
, MIDX_CHUNKID_OBJECTOFFSETS
, &m
->chunk_object_offsets
) == CHUNK_NOT_FOUND
)
153 die(_("multi-pack-index missing required object offsets chunk"));
155 pair_chunk(cf
, MIDX_CHUNKID_LARGEOFFSETS
, &m
->chunk_large_offsets
);
157 if (git_env_bool("GIT_TEST_MIDX_READ_RIDX", 1))
158 pair_chunk(cf
, MIDX_CHUNKID_REVINDEX
, &m
->chunk_revindex
);
160 m
->num_objects
= ntohl(m
->chunk_oid_fanout
[255]);
162 CALLOC_ARRAY(m
->pack_names
, m
->num_packs
);
163 CALLOC_ARRAY(m
->packs
, m
->num_packs
);
165 cur_pack_name
= (const char *)m
->chunk_pack_names
;
166 for (i
= 0; i
< m
->num_packs
; i
++) {
167 m
->pack_names
[i
] = cur_pack_name
;
169 cur_pack_name
+= strlen(cur_pack_name
) + 1;
171 if (i
&& strcmp(m
->pack_names
[i
], m
->pack_names
[i
- 1]) <= 0)
172 die(_("multi-pack-index pack names out of order: '%s' before '%s'"),
173 m
->pack_names
[i
- 1],
177 trace2_data_intmax("midx", the_repository
, "load/num_packs", m
->num_packs
);
178 trace2_data_intmax("midx", the_repository
, "load/num_objects", m
->num_objects
);
185 strbuf_release(&midx_name
);
188 munmap(midx_map
, midx_size
);
194 void close_midx(struct multi_pack_index
*m
)
203 munmap((unsigned char *)m
->data
, m
->data_len
);
205 for (i
= 0; i
< m
->num_packs
; i
++) {
207 m
->packs
[i
]->multi_pack_index
= 0;
209 FREE_AND_NULL(m
->packs
);
210 FREE_AND_NULL(m
->pack_names
);
214 int prepare_midx_pack(struct repository
*r
, struct multi_pack_index
*m
, uint32_t pack_int_id
)
216 struct strbuf pack_name
= STRBUF_INIT
;
217 struct packed_git
*p
;
219 if (pack_int_id
>= m
->num_packs
)
220 die(_("bad pack-int-id: %u (%u total packs)"),
221 pack_int_id
, m
->num_packs
);
223 if (m
->packs
[pack_int_id
])
226 strbuf_addf(&pack_name
, "%s/pack/%s", m
->object_dir
,
227 m
->pack_names
[pack_int_id
]);
229 p
= add_packed_git(pack_name
.buf
, pack_name
.len
, m
->local
);
230 strbuf_release(&pack_name
);
235 p
->multi_pack_index
= 1;
236 m
->packs
[pack_int_id
] = p
;
237 install_packed_git(r
, p
);
238 list_add_tail(&p
->mru
, &r
->objects
->packed_git_mru
);
243 int bsearch_midx(const struct object_id
*oid
, struct multi_pack_index
*m
, uint32_t *result
)
245 return bsearch_hash(oid
->hash
, m
->chunk_oid_fanout
, m
->chunk_oid_lookup
,
246 the_hash_algo
->rawsz
, result
);
249 struct object_id
*nth_midxed_object_oid(struct object_id
*oid
,
250 struct multi_pack_index
*m
,
253 if (n
>= m
->num_objects
)
256 oidread(oid
, m
->chunk_oid_lookup
+ m
->hash_len
* n
);
260 off_t
nth_midxed_offset(struct multi_pack_index
*m
, uint32_t pos
)
262 const unsigned char *offset_data
;
265 offset_data
= m
->chunk_object_offsets
+ (off_t
)pos
* MIDX_CHUNK_OFFSET_WIDTH
;
266 offset32
= get_be32(offset_data
+ sizeof(uint32_t));
268 if (m
->chunk_large_offsets
&& offset32
& MIDX_LARGE_OFFSET_NEEDED
) {
269 if (sizeof(off_t
) < sizeof(uint64_t))
270 die(_("multi-pack-index stores a 64-bit offset, but off_t is too small"));
272 offset32
^= MIDX_LARGE_OFFSET_NEEDED
;
273 return get_be64(m
->chunk_large_offsets
+ sizeof(uint64_t) * offset32
);
279 uint32_t nth_midxed_pack_int_id(struct multi_pack_index
*m
, uint32_t pos
)
281 return get_be32(m
->chunk_object_offsets
+
282 (off_t
)pos
* MIDX_CHUNK_OFFSET_WIDTH
);
285 int fill_midx_entry(struct repository
*r
,
286 const struct object_id
*oid
,
287 struct pack_entry
*e
,
288 struct multi_pack_index
*m
)
291 uint32_t pack_int_id
;
292 struct packed_git
*p
;
294 if (!bsearch_midx(oid
, m
, &pos
))
297 if (pos
>= m
->num_objects
)
300 pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
302 if (prepare_midx_pack(r
, m
, pack_int_id
))
304 p
= m
->packs
[pack_int_id
];
307 * We are about to tell the caller where they can locate the
308 * requested object. We better make sure the packfile is
309 * still here and can be accessed before supplying that
310 * answer, as it may have been deleted since the MIDX was
313 if (!is_pack_valid(p
))
316 if (oidset_size(&p
->bad_objects
) &&
317 oidset_contains(&p
->bad_objects
, oid
))
320 e
->offset
= nth_midxed_offset(m
, pos
);
326 /* Match "foo.idx" against either "foo.pack" _or_ "foo.idx". */
327 static int cmp_idx_or_pack_name(const char *idx_or_pack_name
,
328 const char *idx_name
)
330 /* Skip past any initial matching prefix. */
331 while (*idx_name
&& *idx_name
== *idx_or_pack_name
) {
337 * If we didn't match completely, we may have matched "pack-1234." and
338 * be left with "idx" and "pack" respectively, which is also OK. We do
339 * not have to check for "idx" and "idx", because that would have been
340 * a complete match (and in that case these strcmps will be false, but
341 * we'll correctly return 0 from the final strcmp() below.
343 * Technically this matches "fooidx" and "foopack", but we'd never have
344 * such names in the first place.
346 if (!strcmp(idx_name
, "idx") && !strcmp(idx_or_pack_name
, "pack"))
350 * This not only checks for a complete match, but also orders based on
351 * the first non-identical character, which means our ordering will
352 * match a raw strcmp(). That makes it OK to use this to binary search
353 * a naively-sorted list.
355 return strcmp(idx_or_pack_name
, idx_name
);
358 int midx_contains_pack(struct multi_pack_index
*m
, const char *idx_or_pack_name
)
360 uint32_t first
= 0, last
= m
->num_packs
;
362 while (first
< last
) {
363 uint32_t mid
= first
+ (last
- first
) / 2;
367 current
= m
->pack_names
[mid
];
368 cmp
= cmp_idx_or_pack_name(idx_or_pack_name
, current
);
381 int prepare_multi_pack_index_one(struct repository
*r
, const char *object_dir
, int local
)
383 struct multi_pack_index
*m
;
384 struct multi_pack_index
*m_search
;
386 prepare_repo_settings(r
);
387 if (!r
->settings
.core_multi_pack_index
)
390 for (m_search
= r
->objects
->multi_pack_index
; m_search
; m_search
= m_search
->next
)
391 if (!strcmp(object_dir
, m_search
->object_dir
))
394 m
= load_multi_pack_index(object_dir
, local
);
397 struct multi_pack_index
*mp
= r
->objects
->multi_pack_index
;
402 r
->objects
->multi_pack_index
= m
;
409 static size_t write_midx_header(struct hashfile
*f
,
410 unsigned char num_chunks
,
413 hashwrite_be32(f
, MIDX_SIGNATURE
);
414 hashwrite_u8(f
, MIDX_VERSION
);
415 hashwrite_u8(f
, oid_version(the_hash_algo
));
416 hashwrite_u8(f
, num_chunks
);
417 hashwrite_u8(f
, 0); /* unused */
418 hashwrite_be32(f
, num_packs
);
420 return MIDX_HEADER_SIZE
;
424 uint32_t orig_pack_int_id
;
426 struct packed_git
*p
;
427 unsigned expired
: 1;
430 static int pack_info_compare(const void *_a
, const void *_b
)
432 struct pack_info
*a
= (struct pack_info
*)_a
;
433 struct pack_info
*b
= (struct pack_info
*)_b
;
434 return strcmp(a
->pack_name
, b
->pack_name
);
437 static int idx_or_pack_name_cmp(const void *_va
, const void *_vb
)
439 const char *pack_name
= _va
;
440 const struct pack_info
*compar
= _vb
;
442 return cmp_idx_or_pack_name(pack_name
, compar
->pack_name
);
445 struct write_midx_context
{
446 struct pack_info
*info
;
449 struct multi_pack_index
*m
;
450 struct progress
*progress
;
451 unsigned pack_paths_checked
;
453 struct pack_midx_entry
*entries
;
457 uint32_t *pack_order
;
458 unsigned large_offsets_needed
:1;
459 uint32_t num_large_offsets
;
461 int preferred_pack_idx
;
463 struct string_list
*to_include
;
466 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
467 const char *file_name
, void *data
)
469 struct write_midx_context
*ctx
= data
;
471 if (ends_with(file_name
, ".idx")) {
472 display_progress(ctx
->progress
, ++ctx
->pack_paths_checked
);
474 * Note that at most one of ctx->m and ctx->to_include are set,
475 * so we are testing midx_contains_pack() and
476 * string_list_has_string() independently (guarded by the
477 * appropriate NULL checks).
479 * We could support passing to_include while reusing an existing
480 * MIDX, but don't currently since the reuse process drags
481 * forward all packs from an existing MIDX (without checking
482 * whether or not they appear in the to_include list).
484 * If we added support for that, these next two conditional
485 * should be performed independently (likely checking
486 * to_include before the existing MIDX).
488 if (ctx
->m
&& midx_contains_pack(ctx
->m
, file_name
))
490 else if (ctx
->to_include
&&
491 !string_list_has_string(ctx
->to_include
, file_name
))
494 ALLOC_GROW(ctx
->info
, ctx
->nr
+ 1, ctx
->alloc
);
496 ctx
->info
[ctx
->nr
].p
= add_packed_git(full_path
,
500 if (!ctx
->info
[ctx
->nr
].p
) {
501 warning(_("failed to add packfile '%s'"),
506 if (open_pack_index(ctx
->info
[ctx
->nr
].p
)) {
507 warning(_("failed to open pack-index '%s'"),
509 close_pack(ctx
->info
[ctx
->nr
].p
);
510 FREE_AND_NULL(ctx
->info
[ctx
->nr
].p
);
514 ctx
->info
[ctx
->nr
].pack_name
= xstrdup(file_name
);
515 ctx
->info
[ctx
->nr
].orig_pack_int_id
= ctx
->nr
;
516 ctx
->info
[ctx
->nr
].expired
= 0;
521 struct pack_midx_entry
{
522 struct object_id oid
;
523 uint32_t pack_int_id
;
526 unsigned preferred
: 1;
529 static int midx_oid_compare(const void *_a
, const void *_b
)
531 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
532 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
533 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
538 /* Sort objects in a preferred pack first when multiple copies exist. */
539 if (a
->preferred
> b
->preferred
)
541 if (a
->preferred
< b
->preferred
)
544 if (a
->pack_mtime
> b
->pack_mtime
)
546 else if (a
->pack_mtime
< b
->pack_mtime
)
549 return a
->pack_int_id
- b
->pack_int_id
;
552 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
553 struct pack_midx_entry
*e
,
556 if (pos
>= m
->num_objects
)
559 nth_midxed_object_oid(&e
->oid
, m
, pos
);
560 e
->pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
561 e
->offset
= nth_midxed_offset(m
, pos
);
563 /* consider objects in midx to be from "old" packs */
568 static void fill_pack_entry(uint32_t pack_int_id
,
569 struct packed_git
*p
,
571 struct pack_midx_entry
*entry
,
574 if (nth_packed_object_id(&entry
->oid
, p
, cur_object
) < 0)
575 die(_("failed to locate object %d in packfile"), cur_object
);
577 entry
->pack_int_id
= pack_int_id
;
578 entry
->pack_mtime
= p
->mtime
;
580 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
581 entry
->preferred
= !!preferred
;
585 struct pack_midx_entry
*entries
;
590 static void midx_fanout_grow(struct midx_fanout
*fanout
, uint32_t nr
)
592 ALLOC_GROW(fanout
->entries
, nr
, fanout
->alloc
);
595 static void midx_fanout_sort(struct midx_fanout
*fanout
)
597 QSORT(fanout
->entries
, fanout
->nr
, midx_oid_compare
);
600 static void midx_fanout_add_midx_fanout(struct midx_fanout
*fanout
,
601 struct multi_pack_index
*m
,
605 uint32_t start
= 0, end
;
609 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
610 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
612 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
613 if ((preferred_pack
> -1) &&
614 (preferred_pack
== nth_midxed_pack_int_id(m
, cur_object
))) {
616 * Objects from preferred packs are added
622 midx_fanout_grow(fanout
, fanout
->nr
+ 1);
623 nth_midxed_pack_midx_entry(m
,
624 &fanout
->entries
[fanout
->nr
],
626 fanout
->entries
[fanout
->nr
].preferred
= 0;
631 static void midx_fanout_add_pack_fanout(struct midx_fanout
*fanout
,
632 struct pack_info
*info
,
637 struct packed_git
*pack
= info
[cur_pack
].p
;
638 uint32_t start
= 0, end
;
642 start
= get_pack_fanout(pack
, cur_fanout
- 1);
643 end
= get_pack_fanout(pack
, cur_fanout
);
645 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
646 midx_fanout_grow(fanout
, fanout
->nr
+ 1);
647 fill_pack_entry(cur_pack
,
650 &fanout
->entries
[fanout
->nr
],
657 * It is possible to artificially get into a state where there are many
658 * duplicate copies of objects. That can create high memory pressure if
659 * we are to create a list of all objects before de-duplication. To reduce
660 * this memory pressure without a significant performance drop, automatically
661 * group objects by the first byte of their object id. Use the IDX fanout
662 * tables to group the data, copy to a local array, then sort.
664 * Copy only the de-duplicated entries (selected by most-recent modified time
665 * of a packfile containing the object).
667 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
668 struct pack_info
*info
,
670 uint32_t *nr_objects
,
673 uint32_t cur_fanout
, cur_pack
, cur_object
;
674 uint32_t alloc_objects
, total_objects
= 0;
675 struct midx_fanout fanout
= { 0 };
676 struct pack_midx_entry
*deduplicated_entries
= NULL
;
677 uint32_t start_pack
= m
? m
->num_packs
: 0;
679 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
680 total_objects
+= info
[cur_pack
].p
->num_objects
;
683 * As we de-duplicate by fanout value, we expect the fanout
684 * slices to be evenly distributed, with some noise. Hence,
685 * allocate slightly more than one 256th.
687 alloc_objects
= fanout
.alloc
= total_objects
> 3200 ? total_objects
/ 200 : 16;
689 ALLOC_ARRAY(fanout
.entries
, fanout
.alloc
);
690 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
693 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
697 midx_fanout_add_midx_fanout(&fanout
, m
, cur_fanout
,
700 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
701 int preferred
= cur_pack
== preferred_pack
;
702 midx_fanout_add_pack_fanout(&fanout
,
704 preferred
, cur_fanout
);
707 if (-1 < preferred_pack
&& preferred_pack
< start_pack
)
708 midx_fanout_add_pack_fanout(&fanout
, info
,
712 midx_fanout_sort(&fanout
);
715 * The batch is now sorted by OID and then mtime (descending).
716 * Take only the first duplicate.
718 for (cur_object
= 0; cur_object
< fanout
.nr
; cur_object
++) {
719 if (cur_object
&& oideq(&fanout
.entries
[cur_object
- 1].oid
,
720 &fanout
.entries
[cur_object
].oid
))
723 ALLOC_GROW(deduplicated_entries
, *nr_objects
+ 1, alloc_objects
);
724 memcpy(&deduplicated_entries
[*nr_objects
],
725 &fanout
.entries
[cur_object
],
726 sizeof(struct pack_midx_entry
));
731 free(fanout
.entries
);
732 return deduplicated_entries
;
735 static int write_midx_pack_names(struct hashfile
*f
, void *data
)
737 struct write_midx_context
*ctx
= data
;
739 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
742 for (i
= 0; i
< ctx
->nr
; i
++) {
745 if (ctx
->info
[i
].expired
)
748 if (i
&& strcmp(ctx
->info
[i
].pack_name
, ctx
->info
[i
- 1].pack_name
) <= 0)
749 BUG("incorrect pack-file order: %s before %s",
750 ctx
->info
[i
- 1].pack_name
,
751 ctx
->info
[i
].pack_name
);
753 writelen
= strlen(ctx
->info
[i
].pack_name
) + 1;
754 hashwrite(f
, ctx
->info
[i
].pack_name
, writelen
);
758 /* add padding to be aligned */
759 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
760 if (i
< MIDX_CHUNK_ALIGNMENT
) {
761 memset(padding
, 0, sizeof(padding
));
762 hashwrite(f
, padding
, i
);
768 static int write_midx_oid_fanout(struct hashfile
*f
,
771 struct write_midx_context
*ctx
= data
;
772 struct pack_midx_entry
*list
= ctx
->entries
;
773 struct pack_midx_entry
*last
= ctx
->entries
+ ctx
->entries_nr
;
778 * Write the first-level table (the list is sorted,
779 * but we use a 256-entry lookup to be able to avoid
780 * having to do eight extra binary search iterations).
782 for (i
= 0; i
< 256; i
++) {
783 struct pack_midx_entry
*next
= list
;
785 while (next
< last
&& next
->oid
.hash
[0] == i
) {
790 hashwrite_be32(f
, count
);
797 static int write_midx_oid_lookup(struct hashfile
*f
,
800 struct write_midx_context
*ctx
= data
;
801 unsigned char hash_len
= the_hash_algo
->rawsz
;
802 struct pack_midx_entry
*list
= ctx
->entries
;
805 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
806 struct pack_midx_entry
*obj
= list
++;
808 if (i
< ctx
->entries_nr
- 1) {
809 struct pack_midx_entry
*next
= list
;
810 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
811 BUG("OIDs not in order: %s >= %s",
812 oid_to_hex(&obj
->oid
),
813 oid_to_hex(&next
->oid
));
816 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
822 static int write_midx_object_offsets(struct hashfile
*f
,
825 struct write_midx_context
*ctx
= data
;
826 struct pack_midx_entry
*list
= ctx
->entries
;
827 uint32_t i
, nr_large_offset
= 0;
829 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
830 struct pack_midx_entry
*obj
= list
++;
832 if (ctx
->pack_perm
[obj
->pack_int_id
] == PACK_EXPIRED
)
833 BUG("object %s is in an expired pack with int-id %d",
834 oid_to_hex(&obj
->oid
),
837 hashwrite_be32(f
, ctx
->pack_perm
[obj
->pack_int_id
]);
839 if (ctx
->large_offsets_needed
&& obj
->offset
>> 31)
840 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
841 else if (!ctx
->large_offsets_needed
&& obj
->offset
>> 32)
842 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
843 oid_to_hex(&obj
->oid
),
846 hashwrite_be32(f
, (uint32_t)obj
->offset
);
852 static int write_midx_large_offsets(struct hashfile
*f
,
855 struct write_midx_context
*ctx
= data
;
856 struct pack_midx_entry
*list
= ctx
->entries
;
857 struct pack_midx_entry
*end
= ctx
->entries
+ ctx
->entries_nr
;
858 uint32_t nr_large_offset
= ctx
->num_large_offsets
;
860 while (nr_large_offset
) {
861 struct pack_midx_entry
*obj
;
865 BUG("too many large-offset objects");
868 offset
= obj
->offset
;
873 hashwrite_be64(f
, offset
);
881 static int write_midx_revindex(struct hashfile
*f
,
884 struct write_midx_context
*ctx
= data
;
887 for (i
= 0; i
< ctx
->entries_nr
; i
++)
888 hashwrite_be32(f
, ctx
->pack_order
[i
]);
893 struct midx_pack_order_data
{
899 static int midx_pack_order_cmp(const void *va
, const void *vb
)
901 const struct midx_pack_order_data
*a
= va
, *b
= vb
;
902 if (a
->pack
< b
->pack
)
904 else if (a
->pack
> b
->pack
)
906 else if (a
->offset
< b
->offset
)
908 else if (a
->offset
> b
->offset
)
914 static uint32_t *midx_pack_order(struct write_midx_context
*ctx
)
916 struct midx_pack_order_data
*data
;
917 uint32_t *pack_order
;
920 trace2_region_enter("midx", "midx_pack_order", the_repository
);
922 ALLOC_ARRAY(data
, ctx
->entries_nr
);
923 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
924 struct pack_midx_entry
*e
= &ctx
->entries
[i
];
926 data
[i
].pack
= ctx
->pack_perm
[e
->pack_int_id
];
928 data
[i
].pack
|= (1U << 31);
929 data
[i
].offset
= e
->offset
;
932 QSORT(data
, ctx
->entries_nr
, midx_pack_order_cmp
);
934 ALLOC_ARRAY(pack_order
, ctx
->entries_nr
);
935 for (i
= 0; i
< ctx
->entries_nr
; i
++)
936 pack_order
[i
] = data
[i
].nr
;
939 trace2_region_leave("midx", "midx_pack_order", the_repository
);
944 static void write_midx_reverse_index(char *midx_name
, unsigned char *midx_hash
,
945 struct write_midx_context
*ctx
)
947 struct strbuf buf
= STRBUF_INIT
;
948 const char *tmp_file
;
950 trace2_region_enter("midx", "write_midx_reverse_index", the_repository
);
952 strbuf_addf(&buf
, "%s-%s.rev", midx_name
, hash_to_hex(midx_hash
));
954 tmp_file
= write_rev_file_order(NULL
, ctx
->pack_order
, ctx
->entries_nr
,
955 midx_hash
, WRITE_REV
);
957 if (finalize_object_file(tmp_file
, buf
.buf
))
958 die(_("cannot store reverse index file"));
960 strbuf_release(&buf
);
962 trace2_region_leave("midx", "write_midx_reverse_index", the_repository
);
965 static void clear_midx_files_ext(const char *object_dir
, const char *ext
,
966 unsigned char *keep_hash
);
968 static int midx_checksum_valid(struct multi_pack_index
*m
)
970 return hashfile_checksum_valid(m
->data
, m
->data_len
);
973 static void prepare_midx_packing_data(struct packing_data
*pdata
,
974 struct write_midx_context
*ctx
)
978 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository
);
980 memset(pdata
, 0, sizeof(struct packing_data
));
981 prepare_packing_data(the_repository
, pdata
);
983 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
984 struct pack_midx_entry
*from
= &ctx
->entries
[ctx
->pack_order
[i
]];
985 struct object_entry
*to
= packlist_alloc(pdata
, &from
->oid
);
987 oe_set_in_pack(pdata
, to
,
988 ctx
->info
[ctx
->pack_perm
[from
->pack_int_id
]].p
);
991 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository
);
994 static int add_ref_to_pending(const char *refname
,
995 const struct object_id
*oid
,
996 int flag
, void *cb_data
)
998 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
999 struct object_id peeled
;
1000 struct object
*object
;
1002 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
1003 warning("symbolic ref is dangling: %s", refname
);
1007 if (!peel_iterated_oid(oid
, &peeled
))
1010 object
= parse_object_or_die(oid
, refname
);
1011 if (object
->type
!= OBJ_COMMIT
)
1014 add_pending_object(revs
, object
, "");
1015 if (bitmap_is_preferred_refname(revs
->repo
, refname
))
1016 object
->flags
|= NEEDS_BITMAP
;
1020 struct bitmap_commit_cb
{
1021 struct commit
**commits
;
1022 size_t commits_nr
, commits_alloc
;
1024 struct write_midx_context
*ctx
;
1027 static const struct object_id
*bitmap_oid_access(size_t index
,
1028 const void *_entries
)
1030 const struct pack_midx_entry
*entries
= _entries
;
1031 return &entries
[index
].oid
;
1034 static void bitmap_show_commit(struct commit
*commit
, void *_data
)
1036 struct bitmap_commit_cb
*data
= _data
;
1037 int pos
= oid_pos(&commit
->object
.oid
, data
->ctx
->entries
,
1038 data
->ctx
->entries_nr
,
1043 ALLOC_GROW(data
->commits
, data
->commits_nr
+ 1, data
->commits_alloc
);
1044 data
->commits
[data
->commits_nr
++] = commit
;
1047 static int read_refs_snapshot(const char *refs_snapshot
,
1048 struct rev_info
*revs
)
1050 struct strbuf buf
= STRBUF_INIT
;
1051 struct object_id oid
;
1052 FILE *f
= xfopen(refs_snapshot
, "r");
1054 while (strbuf_getline(&buf
, f
) != EOF
) {
1055 struct object
*object
;
1057 char *hex
= buf
.buf
;
1058 const char *end
= NULL
;
1060 if (buf
.len
&& *buf
.buf
== '+') {
1065 if (parse_oid_hex(hex
, &oid
, &end
) < 0)
1066 die(_("could not parse line: %s"), buf
.buf
);
1068 die(_("malformed line: %s"), buf
.buf
);
1070 object
= parse_object_or_die(&oid
, NULL
);
1072 object
->flags
|= NEEDS_BITMAP
;
1074 add_pending_object(revs
, object
, "");
1078 strbuf_release(&buf
);
1082 static struct commit
**find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p
,
1083 const char *refs_snapshot
,
1084 struct write_midx_context
*ctx
)
1086 struct rev_info revs
;
1087 struct bitmap_commit_cb cb
= {0};
1089 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
1094 repo_init_revisions(the_repository
, &revs
, NULL
);
1095 if (refs_snapshot
) {
1096 read_refs_snapshot(refs_snapshot
, &revs
);
1098 setup_revisions(0, NULL
, &revs
, NULL
);
1099 for_each_ref(add_ref_to_pending
, &revs
);
1103 * Skipping promisor objects here is intentional, since it only excludes
1104 * them from the list of reachable commits that we want to select from
1105 * when computing the selection of MIDX'd commits to receive bitmaps.
1107 * Reachability bitmaps do require that their objects be closed under
1108 * reachability, but fetching any objects missing from promisors at this
1109 * point is too late. But, if one of those objects can be reached from
1110 * an another object that is included in the bitmap, then we will
1111 * complain later that we don't have reachability closure (and fail
1114 fetch_if_missing
= 0;
1115 revs
.exclude_promisor_objects
= 1;
1117 if (prepare_revision_walk(&revs
))
1118 die(_("revision walk setup failed"));
1120 traverse_commit_list(&revs
, bitmap_show_commit
, NULL
, &cb
);
1121 if (indexed_commits_nr_p
)
1122 *indexed_commits_nr_p
= cb
.commits_nr
;
1124 release_revisions(&revs
);
1126 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
1132 static int write_midx_bitmap(const char *midx_name
,
1133 const unsigned char *midx_hash
,
1134 struct packing_data
*pdata
,
1135 struct commit
**commits
,
1136 uint32_t commits_nr
,
1137 uint32_t *pack_order
,
1141 uint16_t options
= 0;
1142 struct pack_idx_entry
**index
;
1143 char *bitmap_name
= xstrfmt("%s-%s.bitmap", midx_name
,
1144 hash_to_hex(midx_hash
));
1146 trace2_region_enter("midx", "write_midx_bitmap", the_repository
);
1148 if (flags
& MIDX_WRITE_BITMAP_HASH_CACHE
)
1149 options
|= BITMAP_OPT_HASH_CACHE
;
1151 if (flags
& MIDX_WRITE_BITMAP_LOOKUP_TABLE
)
1152 options
|= BITMAP_OPT_LOOKUP_TABLE
;
1155 * Build the MIDX-order index based on pdata.objects (which is already
1156 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
1159 ALLOC_ARRAY(index
, pdata
->nr_objects
);
1160 for (i
= 0; i
< pdata
->nr_objects
; i
++)
1161 index
[i
] = &pdata
->objects
[i
].idx
;
1163 bitmap_writer_show_progress(flags
& MIDX_PROGRESS
);
1164 bitmap_writer_build_type_index(pdata
, index
, pdata
->nr_objects
);
1167 * bitmap_writer_finish expects objects in lex order, but pack_order
1168 * gives us exactly that. use it directly instead of re-sorting the
1171 * This changes the order of objects in 'index' between
1172 * bitmap_writer_build_type_index and bitmap_writer_finish.
1174 * The same re-ordering takes place in the single-pack bitmap code via
1175 * write_idx_file(), which is called by finish_tmp_packfile(), which
1176 * happens between bitmap_writer_build_type_index() and
1177 * bitmap_writer_finish().
1179 for (i
= 0; i
< pdata
->nr_objects
; i
++)
1180 index
[pack_order
[i
]] = &pdata
->objects
[i
].idx
;
1182 bitmap_writer_select_commits(commits
, commits_nr
, -1);
1183 ret
= bitmap_writer_build(pdata
);
1187 bitmap_writer_set_checksum(midx_hash
);
1188 bitmap_writer_finish(index
, pdata
->nr_objects
, bitmap_name
, options
);
1194 trace2_region_leave("midx", "write_midx_bitmap", the_repository
);
1199 static struct multi_pack_index
*lookup_multi_pack_index(struct repository
*r
,
1200 const char *object_dir
)
1202 struct multi_pack_index
*result
= NULL
;
1203 struct multi_pack_index
*cur
;
1204 char *obj_dir_real
= real_pathdup(object_dir
, 1);
1205 struct strbuf cur_path_real
= STRBUF_INIT
;
1207 /* Ensure the given object_dir is local, or a known alternate. */
1208 find_odb(r
, obj_dir_real
);
1210 for (cur
= get_multi_pack_index(r
); cur
; cur
= cur
->next
) {
1211 strbuf_realpath(&cur_path_real
, cur
->object_dir
, 1);
1212 if (!strcmp(obj_dir_real
, cur_path_real
.buf
)) {
1220 strbuf_release(&cur_path_real
);
1224 static int write_midx_internal(const char *object_dir
,
1225 struct string_list
*packs_to_include
,
1226 struct string_list
*packs_to_drop
,
1227 const char *preferred_pack_name
,
1228 const char *refs_snapshot
,
1231 struct strbuf midx_name
= STRBUF_INIT
;
1232 unsigned char midx_hash
[GIT_MAX_RAWSZ
];
1234 struct hashfile
*f
= NULL
;
1235 struct lock_file lk
;
1236 struct write_midx_context ctx
= { 0 };
1237 int pack_name_concat_len
= 0;
1238 int dropped_packs
= 0;
1240 struct chunkfile
*cf
;
1242 trace2_region_enter("midx", "write_midx_internal", the_repository
);
1244 get_midx_filename(&midx_name
, object_dir
);
1245 if (safe_create_leading_directories(midx_name
.buf
))
1246 die_errno(_("unable to create leading directories of %s"),
1249 if (!packs_to_include
) {
1251 * Only reference an existing MIDX when not filtering which
1252 * packs to include, since all packs and objects are copied
1253 * blindly from an existing MIDX if one is present.
1255 ctx
.m
= lookup_multi_pack_index(the_repository
, object_dir
);
1258 if (ctx
.m
&& !midx_checksum_valid(ctx
.m
)) {
1259 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
1264 ctx
.alloc
= ctx
.m
? ctx
.m
->num_packs
: 16;
1266 ALLOC_ARRAY(ctx
.info
, ctx
.alloc
);
1269 for (i
= 0; i
< ctx
.m
->num_packs
; i
++) {
1270 ALLOC_GROW(ctx
.info
, ctx
.nr
+ 1, ctx
.alloc
);
1272 ctx
.info
[ctx
.nr
].orig_pack_int_id
= i
;
1273 ctx
.info
[ctx
.nr
].pack_name
= xstrdup(ctx
.m
->pack_names
[i
]);
1274 ctx
.info
[ctx
.nr
].p
= ctx
.m
->packs
[i
];
1275 ctx
.info
[ctx
.nr
].expired
= 0;
1277 if (flags
& MIDX_WRITE_REV_INDEX
) {
1279 * If generating a reverse index, need to have
1280 * packed_git's loaded to compare their
1281 * mtimes and object count.
1283 if (prepare_midx_pack(the_repository
, ctx
.m
, i
)) {
1284 error(_("could not load pack"));
1289 if (open_pack_index(ctx
.m
->packs
[i
]))
1290 die(_("could not open index for %s"),
1291 ctx
.m
->packs
[i
]->pack_name
);
1292 ctx
.info
[ctx
.nr
].p
= ctx
.m
->packs
[i
];
1299 ctx
.pack_paths_checked
= 0;
1300 if (flags
& MIDX_PROGRESS
)
1301 ctx
.progress
= start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
1303 ctx
.progress
= NULL
;
1305 ctx
.to_include
= packs_to_include
;
1307 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &ctx
);
1308 stop_progress(&ctx
.progress
);
1310 if ((ctx
.m
&& ctx
.nr
== ctx
.m
->num_packs
) &&
1311 !(packs_to_include
|| packs_to_drop
)) {
1312 struct bitmap_index
*bitmap_git
;
1314 int want_bitmap
= flags
& MIDX_WRITE_BITMAP
;
1316 bitmap_git
= prepare_midx_bitmap_git(ctx
.m
);
1317 bitmap_exists
= bitmap_git
&& bitmap_is_midx(bitmap_git
);
1318 free_bitmap_index(bitmap_git
);
1320 if (bitmap_exists
|| !want_bitmap
) {
1322 * The correct MIDX already exists, and so does a
1323 * corresponding bitmap (or one wasn't requested).
1326 clear_midx_files_ext(object_dir
, ".bitmap",
1332 if (preferred_pack_name
) {
1334 for (i
= 0; i
< ctx
.nr
; i
++) {
1335 if (!cmp_idx_or_pack_name(preferred_pack_name
,
1336 ctx
.info
[i
].pack_name
)) {
1337 ctx
.preferred_pack_idx
= i
;
1344 warning(_("unknown preferred pack: '%s'"),
1345 preferred_pack_name
);
1346 } else if (ctx
.nr
&&
1347 (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
))) {
1348 struct packed_git
*oldest
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1349 ctx
.preferred_pack_idx
= 0;
1351 if (packs_to_drop
&& packs_to_drop
->nr
)
1352 BUG("cannot write a MIDX bitmap during expiration");
1355 * set a preferred pack when writing a bitmap to ensure that
1356 * the pack from which the first object is selected in pseudo
1357 * pack-order has all of its objects selected from that pack
1358 * (and not another pack containing a duplicate)
1360 for (i
= 1; i
< ctx
.nr
; i
++) {
1361 struct packed_git
*p
= ctx
.info
[i
].p
;
1363 if (!oldest
->num_objects
|| p
->mtime
< oldest
->mtime
) {
1365 ctx
.preferred_pack_idx
= i
;
1369 if (!oldest
->num_objects
) {
1371 * If all packs are empty; unset the preferred index.
1372 * This is acceptable since there will be no duplicate
1373 * objects to resolve, so the preferred value doesn't
1376 ctx
.preferred_pack_idx
= -1;
1380 * otherwise don't mark any pack as preferred to avoid
1381 * interfering with expiration logic below
1383 ctx
.preferred_pack_idx
= -1;
1386 if (ctx
.preferred_pack_idx
> -1) {
1387 struct packed_git
*preferred
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1388 if (!preferred
->num_objects
) {
1389 error(_("cannot select preferred pack %s with no objects"),
1390 preferred
->pack_name
);
1396 ctx
.entries
= get_sorted_entries(ctx
.m
, ctx
.info
, ctx
.nr
, &ctx
.entries_nr
,
1397 ctx
.preferred_pack_idx
);
1399 ctx
.large_offsets_needed
= 0;
1400 for (i
= 0; i
< ctx
.entries_nr
; i
++) {
1401 if (ctx
.entries
[i
].offset
> 0x7fffffff)
1402 ctx
.num_large_offsets
++;
1403 if (ctx
.entries
[i
].offset
> 0xffffffff)
1404 ctx
.large_offsets_needed
= 1;
1407 QSORT(ctx
.info
, ctx
.nr
, pack_info_compare
);
1409 if (packs_to_drop
&& packs_to_drop
->nr
) {
1411 int missing_drops
= 0;
1413 for (i
= 0; i
< ctx
.nr
&& drop_index
< packs_to_drop
->nr
; i
++) {
1414 int cmp
= strcmp(ctx
.info
[i
].pack_name
,
1415 packs_to_drop
->items
[drop_index
].string
);
1419 ctx
.info
[i
].expired
= 1;
1420 } else if (cmp
> 0) {
1421 error(_("did not see pack-file %s to drop"),
1422 packs_to_drop
->items
[drop_index
].string
);
1427 ctx
.info
[i
].expired
= 0;
1431 if (missing_drops
) {
1438 * pack_perm stores a permutation between pack-int-ids from the
1439 * previous multi-pack-index to the new one we are writing:
1441 * pack_perm[old_id] = new_id
1443 ALLOC_ARRAY(ctx
.pack_perm
, ctx
.nr
);
1444 for (i
= 0; i
< ctx
.nr
; i
++) {
1445 if (ctx
.info
[i
].expired
) {
1447 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = PACK_EXPIRED
;
1449 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = i
- dropped_packs
;
1453 for (i
= 0; i
< ctx
.nr
; i
++) {
1454 if (!ctx
.info
[i
].expired
)
1455 pack_name_concat_len
+= strlen(ctx
.info
[i
].pack_name
) + 1;
1458 /* Check that the preferred pack wasn't expired (if given). */
1459 if (preferred_pack_name
) {
1460 struct pack_info
*preferred
= bsearch(preferred_pack_name
,
1463 idx_or_pack_name_cmp
);
1465 uint32_t perm
= ctx
.pack_perm
[preferred
->orig_pack_int_id
];
1466 if (perm
== PACK_EXPIRED
)
1467 warning(_("preferred pack '%s' is expired"),
1468 preferred_pack_name
);
1472 if (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
1473 pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
1474 (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
1476 hold_lock_file_for_update(&lk
, midx_name
.buf
, LOCK_DIE_ON_ERROR
);
1477 f
= hashfd(get_lock_file_fd(&lk
), get_lock_file_path(&lk
));
1479 if (ctx
.nr
- dropped_packs
== 0) {
1480 error(_("no pack files to index."));
1485 if (!ctx
.entries_nr
) {
1486 if (flags
& MIDX_WRITE_BITMAP
)
1487 warning(_("refusing to write multi-pack .bitmap without any objects"));
1488 flags
&= ~(MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
);
1491 cf
= init_chunkfile(f
);
1493 add_chunk(cf
, MIDX_CHUNKID_PACKNAMES
, pack_name_concat_len
,
1494 write_midx_pack_names
);
1495 add_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, MIDX_CHUNK_FANOUT_SIZE
,
1496 write_midx_oid_fanout
);
1497 add_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
,
1498 (size_t)ctx
.entries_nr
* the_hash_algo
->rawsz
,
1499 write_midx_oid_lookup
);
1500 add_chunk(cf
, MIDX_CHUNKID_OBJECTOFFSETS
,
1501 (size_t)ctx
.entries_nr
* MIDX_CHUNK_OFFSET_WIDTH
,
1502 write_midx_object_offsets
);
1504 if (ctx
.large_offsets_needed
)
1505 add_chunk(cf
, MIDX_CHUNKID_LARGEOFFSETS
,
1506 (size_t)ctx
.num_large_offsets
* MIDX_CHUNK_LARGE_OFFSET_WIDTH
,
1507 write_midx_large_offsets
);
1509 if (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
)) {
1510 ctx
.pack_order
= midx_pack_order(&ctx
);
1511 add_chunk(cf
, MIDX_CHUNKID_REVINDEX
,
1512 ctx
.entries_nr
* sizeof(uint32_t),
1513 write_midx_revindex
);
1516 write_midx_header(f
, get_num_chunks(cf
), ctx
.nr
- dropped_packs
);
1517 write_chunkfile(cf
, &ctx
);
1519 finalize_hashfile(f
, midx_hash
, FSYNC_COMPONENT_PACK_METADATA
,
1520 CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
1523 if (flags
& MIDX_WRITE_REV_INDEX
&&
1524 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1525 write_midx_reverse_index(midx_name
.buf
, midx_hash
, &ctx
);
1527 if (flags
& MIDX_WRITE_BITMAP
) {
1528 struct packing_data pdata
;
1529 struct commit
**commits
;
1530 uint32_t commits_nr
;
1532 if (!ctx
.entries_nr
)
1533 BUG("cannot write a bitmap without any objects");
1535 prepare_midx_packing_data(&pdata
, &ctx
);
1537 commits
= find_commits_for_midx_bitmap(&commits_nr
, refs_snapshot
, &ctx
);
1540 * The previous steps translated the information from
1541 * 'entries' into information suitable for constructing
1542 * bitmaps. We no longer need that array, so clear it to
1543 * reduce memory pressure.
1545 FREE_AND_NULL(ctx
.entries
);
1548 if (write_midx_bitmap(midx_name
.buf
, midx_hash
, &pdata
,
1549 commits
, commits_nr
, ctx
.pack_order
,
1551 error(_("could not write multi-pack bitmap"));
1557 * NOTE: Do not use ctx.entries beyond this point, since it might
1558 * have been freed in the previous if block.
1562 close_object_store(the_repository
->objects
);
1564 if (commit_lock_file(&lk
) < 0)
1565 die_errno(_("could not write multi-pack-index"));
1567 clear_midx_files_ext(object_dir
, ".bitmap", midx_hash
);
1568 clear_midx_files_ext(object_dir
, ".rev", midx_hash
);
1571 for (i
= 0; i
< ctx
.nr
; i
++) {
1572 if (ctx
.info
[i
].p
) {
1573 close_pack(ctx
.info
[i
].p
);
1574 free(ctx
.info
[i
].p
);
1576 free(ctx
.info
[i
].pack_name
);
1581 free(ctx
.pack_perm
);
1582 free(ctx
.pack_order
);
1583 strbuf_release(&midx_name
);
1585 trace2_region_leave("midx", "write_midx_internal", the_repository
);
1590 int write_midx_file(const char *object_dir
,
1591 const char *preferred_pack_name
,
1592 const char *refs_snapshot
,
1595 return write_midx_internal(object_dir
, NULL
, NULL
, preferred_pack_name
,
1596 refs_snapshot
, flags
);
1599 int write_midx_file_only(const char *object_dir
,
1600 struct string_list
*packs_to_include
,
1601 const char *preferred_pack_name
,
1602 const char *refs_snapshot
,
1605 return write_midx_internal(object_dir
, packs_to_include
, NULL
,
1606 preferred_pack_name
, refs_snapshot
, flags
);
1609 struct clear_midx_data
{
1614 static void clear_midx_file_ext(const char *full_path
, size_t full_path_len UNUSED
,
1615 const char *file_name
, void *_data
)
1617 struct clear_midx_data
*data
= _data
;
1619 if (!(starts_with(file_name
, "multi-pack-index-") &&
1620 ends_with(file_name
, data
->ext
)))
1622 if (data
->keep
&& !strcmp(data
->keep
, file_name
))
1625 if (unlink(full_path
))
1626 die_errno(_("failed to remove %s"), full_path
);
1629 static void clear_midx_files_ext(const char *object_dir
, const char *ext
,
1630 unsigned char *keep_hash
)
1632 struct clear_midx_data data
;
1633 memset(&data
, 0, sizeof(struct clear_midx_data
));
1636 data
.keep
= xstrfmt("multi-pack-index-%s%s",
1637 hash_to_hex(keep_hash
), ext
);
1640 for_each_file_in_pack_dir(object_dir
,
1641 clear_midx_file_ext
,
1647 void clear_midx_file(struct repository
*r
)
1649 struct strbuf midx
= STRBUF_INIT
;
1651 get_midx_filename(&midx
, r
->objects
->odb
->path
);
1653 if (r
->objects
&& r
->objects
->multi_pack_index
) {
1654 close_midx(r
->objects
->multi_pack_index
);
1655 r
->objects
->multi_pack_index
= NULL
;
1658 if (remove_path(midx
.buf
))
1659 die(_("failed to clear multi-pack-index at %s"), midx
.buf
);
1661 clear_midx_files_ext(r
->objects
->odb
->path
, ".bitmap", NULL
);
1662 clear_midx_files_ext(r
->objects
->odb
->path
, ".rev", NULL
);
1664 strbuf_release(&midx
);
1667 static int verify_midx_error
;
1669 __attribute__((format (printf
, 1, 2)))
1670 static void midx_report(const char *fmt
, ...)
1673 verify_midx_error
= 1;
1675 vfprintf(stderr
, fmt
, ap
);
1676 fprintf(stderr
, "\n");
1680 struct pair_pos_vs_id
1683 uint32_t pack_int_id
;
1686 static int compare_pair_pos_vs_id(const void *_a
, const void *_b
)
1688 struct pair_pos_vs_id
*a
= (struct pair_pos_vs_id
*)_a
;
1689 struct pair_pos_vs_id
*b
= (struct pair_pos_vs_id
*)_b
;
1691 return b
->pack_int_id
- a
->pack_int_id
;
1695 * Limit calls to display_progress() for performance reasons.
1696 * The interval here was arbitrarily chosen.
1698 #define SPARSE_PROGRESS_INTERVAL (1 << 12)
1699 #define midx_display_sparse_progress(progress, n) \
1701 uint64_t _n = (n); \
1702 if ((_n & (SPARSE_PROGRESS_INTERVAL - 1)) == 0) \
1703 display_progress(progress, _n); \
1706 int verify_midx_file(struct repository
*r
, const char *object_dir
, unsigned flags
)
1708 struct pair_pos_vs_id
*pairs
= NULL
;
1710 struct progress
*progress
= NULL
;
1711 struct multi_pack_index
*m
= load_multi_pack_index(object_dir
, 1);
1712 verify_midx_error
= 0;
1717 struct strbuf filename
= STRBUF_INIT
;
1719 get_midx_filename(&filename
, object_dir
);
1721 if (!stat(filename
.buf
, &sb
)) {
1722 error(_("multi-pack-index file exists, but failed to parse"));
1725 strbuf_release(&filename
);
1729 if (!midx_checksum_valid(m
))
1730 midx_report(_("incorrect checksum"));
1732 if (flags
& MIDX_PROGRESS
)
1733 progress
= start_delayed_progress(_("Looking for referenced packfiles"),
1735 for (i
= 0; i
< m
->num_packs
; i
++) {
1736 if (prepare_midx_pack(r
, m
, i
))
1737 midx_report("failed to load pack in position %d", i
);
1739 display_progress(progress
, i
+ 1);
1741 stop_progress(&progress
);
1743 for (i
= 0; i
< 255; i
++) {
1744 uint32_t oid_fanout1
= ntohl(m
->chunk_oid_fanout
[i
]);
1745 uint32_t oid_fanout2
= ntohl(m
->chunk_oid_fanout
[i
+ 1]);
1747 if (oid_fanout1
> oid_fanout2
)
1748 midx_report(_("oid fanout out of order: fanout[%d] = %"PRIx32
" > %"PRIx32
" = fanout[%d]"),
1749 i
, oid_fanout1
, oid_fanout2
, i
+ 1);
1752 if (m
->num_objects
== 0) {
1753 midx_report(_("the midx contains no oid"));
1755 * Remaining tests assume that we have objects, so we can
1761 if (flags
& MIDX_PROGRESS
)
1762 progress
= start_sparse_progress(_("Verifying OID order in multi-pack-index"),
1763 m
->num_objects
- 1);
1764 for (i
= 0; i
< m
->num_objects
- 1; i
++) {
1765 struct object_id oid1
, oid2
;
1767 nth_midxed_object_oid(&oid1
, m
, i
);
1768 nth_midxed_object_oid(&oid2
, m
, i
+ 1);
1770 if (oidcmp(&oid1
, &oid2
) >= 0)
1771 midx_report(_("oid lookup out of order: oid[%d] = %s >= %s = oid[%d]"),
1772 i
, oid_to_hex(&oid1
), oid_to_hex(&oid2
), i
+ 1);
1774 midx_display_sparse_progress(progress
, i
+ 1);
1776 stop_progress(&progress
);
1779 * Create an array mapping each object to its packfile id. Sort it
1780 * to group the objects by packfile. Use this permutation to visit
1781 * each of the objects and only require 1 packfile to be open at a
1784 ALLOC_ARRAY(pairs
, m
->num_objects
);
1785 for (i
= 0; i
< m
->num_objects
; i
++) {
1787 pairs
[i
].pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1790 if (flags
& MIDX_PROGRESS
)
1791 progress
= start_sparse_progress(_("Sorting objects by packfile"),
1793 display_progress(progress
, 0); /* TODO: Measure QSORT() progress */
1794 QSORT(pairs
, m
->num_objects
, compare_pair_pos_vs_id
);
1795 stop_progress(&progress
);
1797 if (flags
& MIDX_PROGRESS
)
1798 progress
= start_sparse_progress(_("Verifying object offsets"), m
->num_objects
);
1799 for (i
= 0; i
< m
->num_objects
; i
++) {
1800 struct object_id oid
;
1801 struct pack_entry e
;
1802 off_t m_offset
, p_offset
;
1804 if (i
> 0 && pairs
[i
-1].pack_int_id
!= pairs
[i
].pack_int_id
&&
1805 m
->packs
[pairs
[i
-1].pack_int_id
])
1807 close_pack_fd(m
->packs
[pairs
[i
-1].pack_int_id
]);
1808 close_pack_index(m
->packs
[pairs
[i
-1].pack_int_id
]);
1811 nth_midxed_object_oid(&oid
, m
, pairs
[i
].pos
);
1813 if (!fill_midx_entry(r
, &oid
, &e
, m
)) {
1814 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1815 pairs
[i
].pos
, oid_to_hex(&oid
));
1819 if (open_pack_index(e
.p
)) {
1820 midx_report(_("failed to load pack-index for packfile %s"),
1825 m_offset
= e
.offset
;
1826 p_offset
= find_pack_entry_one(oid
.hash
, e
.p
);
1828 if (m_offset
!= p_offset
)
1829 midx_report(_("incorrect object offset for oid[%d] = %s: %"PRIx64
" != %"PRIx64
),
1830 pairs
[i
].pos
, oid_to_hex(&oid
), m_offset
, p_offset
);
1832 midx_display_sparse_progress(progress
, i
+ 1);
1834 stop_progress(&progress
);
1840 return verify_midx_error
;
1843 int expire_midx_packs(struct repository
*r
, const char *object_dir
, unsigned flags
)
1845 uint32_t i
, *count
, result
= 0;
1846 struct string_list packs_to_drop
= STRING_LIST_INIT_DUP
;
1847 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
1848 struct progress
*progress
= NULL
;
1853 CALLOC_ARRAY(count
, m
->num_packs
);
1855 if (flags
& MIDX_PROGRESS
)
1856 progress
= start_delayed_progress(_("Counting referenced objects"),
1858 for (i
= 0; i
< m
->num_objects
; i
++) {
1859 int pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1860 count
[pack_int_id
]++;
1861 display_progress(progress
, i
+ 1);
1863 stop_progress(&progress
);
1865 if (flags
& MIDX_PROGRESS
)
1866 progress
= start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1868 for (i
= 0; i
< m
->num_packs
; i
++) {
1870 display_progress(progress
, i
+ 1);
1875 if (prepare_midx_pack(r
, m
, i
))
1878 if (m
->packs
[i
]->pack_keep
|| m
->packs
[i
]->is_cruft
)
1881 pack_name
= xstrdup(m
->packs
[i
]->pack_name
);
1882 close_pack(m
->packs
[i
]);
1884 string_list_insert(&packs_to_drop
, m
->pack_names
[i
]);
1885 unlink_pack_path(pack_name
, 0);
1888 stop_progress(&progress
);
1892 if (packs_to_drop
.nr
)
1893 result
= write_midx_internal(object_dir
, NULL
, &packs_to_drop
, NULL
, NULL
, flags
);
1895 string_list_clear(&packs_to_drop
, 0);
1900 struct repack_info
{
1902 uint32_t referenced_objects
;
1903 uint32_t pack_int_id
;
1906 static int compare_by_mtime(const void *a_
, const void *b_
)
1908 const struct repack_info
*a
, *b
;
1910 a
= (const struct repack_info
*)a_
;
1911 b
= (const struct repack_info
*)b_
;
1913 if (a
->mtime
< b
->mtime
)
1915 if (a
->mtime
> b
->mtime
)
1920 static int fill_included_packs_all(struct repository
*r
,
1921 struct multi_pack_index
*m
,
1922 unsigned char *include_pack
)
1924 uint32_t i
, count
= 0;
1925 int pack_kept_objects
= 0;
1927 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1929 for (i
= 0; i
< m
->num_packs
; i
++) {
1930 if (prepare_midx_pack(r
, m
, i
))
1932 if (!pack_kept_objects
&& m
->packs
[i
]->pack_keep
)
1934 if (m
->packs
[i
]->is_cruft
)
1937 include_pack
[i
] = 1;
1944 static int fill_included_packs_batch(struct repository
*r
,
1945 struct multi_pack_index
*m
,
1946 unsigned char *include_pack
,
1949 uint32_t i
, packs_to_repack
;
1951 struct repack_info
*pack_info
;
1952 int pack_kept_objects
= 0;
1954 CALLOC_ARRAY(pack_info
, m
->num_packs
);
1956 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1958 for (i
= 0; i
< m
->num_packs
; i
++) {
1959 pack_info
[i
].pack_int_id
= i
;
1961 if (prepare_midx_pack(r
, m
, i
))
1964 pack_info
[i
].mtime
= m
->packs
[i
]->mtime
;
1967 for (i
= 0; i
< m
->num_objects
; i
++) {
1968 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1969 pack_info
[pack_int_id
].referenced_objects
++;
1972 QSORT(pack_info
, m
->num_packs
, compare_by_mtime
);
1975 packs_to_repack
= 0;
1976 for (i
= 0; total_size
< batch_size
&& i
< m
->num_packs
; i
++) {
1977 int pack_int_id
= pack_info
[i
].pack_int_id
;
1978 struct packed_git
*p
= m
->packs
[pack_int_id
];
1979 size_t expected_size
;
1983 if (!pack_kept_objects
&& p
->pack_keep
)
1987 if (open_pack_index(p
) || !p
->num_objects
)
1990 expected_size
= (size_t)(p
->pack_size
1991 * pack_info
[i
].referenced_objects
);
1992 expected_size
/= p
->num_objects
;
1994 if (expected_size
>= batch_size
)
1998 total_size
+= expected_size
;
1999 include_pack
[pack_int_id
] = 1;
2004 if (packs_to_repack
< 2)
2010 int midx_repack(struct repository
*r
, const char *object_dir
, size_t batch_size
, unsigned flags
)
2014 unsigned char *include_pack
;
2015 struct child_process cmd
= CHILD_PROCESS_INIT
;
2017 struct strbuf base_name
= STRBUF_INIT
;
2018 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
2021 * When updating the default for these configuration
2022 * variables in builtin/repack.c, these must be adjusted
2025 int delta_base_offset
= 1;
2026 int use_delta_islands
= 0;
2031 CALLOC_ARRAY(include_pack
, m
->num_packs
);
2034 if (fill_included_packs_batch(r
, m
, include_pack
, batch_size
))
2036 } else if (fill_included_packs_all(r
, m
, include_pack
))
2039 repo_config_get_bool(r
, "repack.usedeltabaseoffset", &delta_base_offset
);
2040 repo_config_get_bool(r
, "repack.usedeltaislands", &use_delta_islands
);
2042 strvec_push(&cmd
.args
, "pack-objects");
2044 strbuf_addstr(&base_name
, object_dir
);
2045 strbuf_addstr(&base_name
, "/pack/pack");
2046 strvec_push(&cmd
.args
, base_name
.buf
);
2048 if (delta_base_offset
)
2049 strvec_push(&cmd
.args
, "--delta-base-offset");
2050 if (use_delta_islands
)
2051 strvec_push(&cmd
.args
, "--delta-islands");
2053 if (flags
& MIDX_PROGRESS
)
2054 strvec_push(&cmd
.args
, "--progress");
2056 strvec_push(&cmd
.args
, "-q");
2058 strbuf_release(&base_name
);
2061 cmd
.in
= cmd
.out
= -1;
2063 if (start_command(&cmd
)) {
2064 error(_("could not start pack-objects"));
2069 cmd_in
= xfdopen(cmd
.in
, "w");
2071 for (i
= 0; i
< m
->num_objects
; i
++) {
2072 struct object_id oid
;
2073 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
2075 if (!include_pack
[pack_int_id
])
2078 nth_midxed_object_oid(&oid
, m
, i
);
2079 fprintf(cmd_in
, "%s\n", oid_to_hex(&oid
));
2083 if (finish_command(&cmd
)) {
2084 error(_("could not finish pack-objects"));
2089 result
= write_midx_internal(object_dir
, NULL
, NULL
, NULL
, NULL
, flags
);