1 #include "git-compat-util.h"
7 #include "object-file.h"
8 #include "hash-lookup.h"
12 #include "run-command.h"
13 #include "chunk-format.h"
14 #include "pack-bitmap.h"
17 #include "list-objects.h"
19 #define PACK_EXPIRED UINT_MAX
20 #define BITMAP_POS_UNKNOWN (~((uint32_t)0))
21 #define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
22 #define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
24 extern int midx_checksum_valid(struct multi_pack_index
*m
);
25 extern void clear_midx_files_ext(const char *object_dir
, const char *ext
,
26 unsigned char *keep_hash
);
27 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name
,
28 const char *idx_name
);
30 static size_t write_midx_header(struct hashfile
*f
,
31 unsigned char num_chunks
,
34 hashwrite_be32(f
, MIDX_SIGNATURE
);
35 hashwrite_u8(f
, MIDX_VERSION
);
36 hashwrite_u8(f
, oid_version(the_hash_algo
));
37 hashwrite_u8(f
, num_chunks
);
38 hashwrite_u8(f
, 0); /* unused */
39 hashwrite_be32(f
, num_packs
);
41 return MIDX_HEADER_SIZE
;
45 uint32_t orig_pack_int_id
;
55 static void fill_pack_info(struct pack_info
*info
,
56 struct packed_git
*p
, const char *pack_name
,
57 uint32_t orig_pack_int_id
)
59 memset(info
, 0, sizeof(struct pack_info
));
61 info
->orig_pack_int_id
= orig_pack_int_id
;
62 info
->pack_name
= xstrdup(pack_name
);
64 info
->bitmap_pos
= BITMAP_POS_UNKNOWN
;
67 static int pack_info_compare(const void *_a
, const void *_b
)
69 struct pack_info
*a
= (struct pack_info
*)_a
;
70 struct pack_info
*b
= (struct pack_info
*)_b
;
71 return strcmp(a
->pack_name
, b
->pack_name
);
74 static int idx_or_pack_name_cmp(const void *_va
, const void *_vb
)
76 const char *pack_name
= _va
;
77 const struct pack_info
*compar
= _vb
;
79 return cmp_idx_or_pack_name(pack_name
, compar
->pack_name
);
82 struct write_midx_context
{
83 struct pack_info
*info
;
86 struct multi_pack_index
*m
;
87 struct progress
*progress
;
88 unsigned pack_paths_checked
;
90 struct pack_midx_entry
*entries
;
95 unsigned large_offsets_needed
:1;
96 uint32_t num_large_offsets
;
98 int preferred_pack_idx
;
100 struct string_list
*to_include
;
103 static void add_pack_to_midx(const char *full_path
, size_t full_path_len
,
104 const char *file_name
, void *data
)
106 struct write_midx_context
*ctx
= data
;
107 struct packed_git
*p
;
109 if (ends_with(file_name
, ".idx")) {
110 display_progress(ctx
->progress
, ++ctx
->pack_paths_checked
);
112 * Note that at most one of ctx->m and ctx->to_include are set,
113 * so we are testing midx_contains_pack() and
114 * string_list_has_string() independently (guarded by the
115 * appropriate NULL checks).
117 * We could support passing to_include while reusing an existing
118 * MIDX, but don't currently since the reuse process drags
119 * forward all packs from an existing MIDX (without checking
120 * whether or not they appear in the to_include list).
122 * If we added support for that, these next two conditional
123 * should be performed independently (likely checking
124 * to_include before the existing MIDX).
126 if (ctx
->m
&& midx_contains_pack(ctx
->m
, file_name
))
128 else if (ctx
->to_include
&&
129 !string_list_has_string(ctx
->to_include
, file_name
))
132 ALLOC_GROW(ctx
->info
, ctx
->nr
+ 1, ctx
->alloc
);
134 p
= add_packed_git(full_path
, full_path_len
, 0);
136 warning(_("failed to add packfile '%s'"),
141 if (open_pack_index(p
)) {
142 warning(_("failed to open pack-index '%s'"),
149 fill_pack_info(&ctx
->info
[ctx
->nr
], p
, file_name
, ctx
->nr
);
154 struct pack_midx_entry
{
155 struct object_id oid
;
156 uint32_t pack_int_id
;
159 unsigned preferred
: 1;
162 static int midx_oid_compare(const void *_a
, const void *_b
)
164 const struct pack_midx_entry
*a
= (const struct pack_midx_entry
*)_a
;
165 const struct pack_midx_entry
*b
= (const struct pack_midx_entry
*)_b
;
166 int cmp
= oidcmp(&a
->oid
, &b
->oid
);
171 /* Sort objects in a preferred pack first when multiple copies exist. */
172 if (a
->preferred
> b
->preferred
)
174 if (a
->preferred
< b
->preferred
)
177 if (a
->pack_mtime
> b
->pack_mtime
)
179 else if (a
->pack_mtime
< b
->pack_mtime
)
182 return a
->pack_int_id
- b
->pack_int_id
;
185 static int nth_midxed_pack_midx_entry(struct multi_pack_index
*m
,
186 struct pack_midx_entry
*e
,
189 if (pos
>= m
->num_objects
)
192 nth_midxed_object_oid(&e
->oid
, m
, pos
);
193 e
->pack_int_id
= nth_midxed_pack_int_id(m
, pos
);
194 e
->offset
= nth_midxed_offset(m
, pos
);
196 /* consider objects in midx to be from "old" packs */
201 static void fill_pack_entry(uint32_t pack_int_id
,
202 struct packed_git
*p
,
204 struct pack_midx_entry
*entry
,
207 if (nth_packed_object_id(&entry
->oid
, p
, cur_object
) < 0)
208 die(_("failed to locate object %d in packfile"), cur_object
);
210 entry
->pack_int_id
= pack_int_id
;
211 entry
->pack_mtime
= p
->mtime
;
213 entry
->offset
= nth_packed_object_offset(p
, cur_object
);
214 entry
->preferred
= !!preferred
;
218 struct pack_midx_entry
*entries
;
222 static void midx_fanout_grow(struct midx_fanout
*fanout
, size_t nr
)
225 BUG("negative growth in midx_fanout_grow() (%"PRIuMAX
" < %"PRIuMAX
")",
226 (uintmax_t)nr
, (uintmax_t)fanout
->nr
);
227 ALLOC_GROW(fanout
->entries
, nr
, fanout
->alloc
);
230 static void midx_fanout_sort(struct midx_fanout
*fanout
)
232 QSORT(fanout
->entries
, fanout
->nr
, midx_oid_compare
);
235 static void midx_fanout_add_midx_fanout(struct midx_fanout
*fanout
,
236 struct multi_pack_index
*m
,
240 uint32_t start
= 0, end
;
244 start
= ntohl(m
->chunk_oid_fanout
[cur_fanout
- 1]);
245 end
= ntohl(m
->chunk_oid_fanout
[cur_fanout
]);
247 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
248 if ((preferred_pack
> -1) &&
249 (preferred_pack
== nth_midxed_pack_int_id(m
, cur_object
))) {
251 * Objects from preferred packs are added
257 midx_fanout_grow(fanout
, fanout
->nr
+ 1);
258 nth_midxed_pack_midx_entry(m
,
259 &fanout
->entries
[fanout
->nr
],
261 fanout
->entries
[fanout
->nr
].preferred
= 0;
266 static void midx_fanout_add_pack_fanout(struct midx_fanout
*fanout
,
267 struct pack_info
*info
,
272 struct packed_git
*pack
= info
[cur_pack
].p
;
273 uint32_t start
= 0, end
;
277 start
= get_pack_fanout(pack
, cur_fanout
- 1);
278 end
= get_pack_fanout(pack
, cur_fanout
);
280 for (cur_object
= start
; cur_object
< end
; cur_object
++) {
281 midx_fanout_grow(fanout
, fanout
->nr
+ 1);
282 fill_pack_entry(cur_pack
,
285 &fanout
->entries
[fanout
->nr
],
292 * It is possible to artificially get into a state where there are many
293 * duplicate copies of objects. That can create high memory pressure if
294 * we are to create a list of all objects before de-duplication. To reduce
295 * this memory pressure without a significant performance drop, automatically
296 * group objects by the first byte of their object id. Use the IDX fanout
297 * tables to group the data, copy to a local array, then sort.
299 * Copy only the de-duplicated entries (selected by most-recent modified time
300 * of a packfile containing the object).
302 static struct pack_midx_entry
*get_sorted_entries(struct multi_pack_index
*m
,
303 struct pack_info
*info
,
308 uint32_t cur_fanout
, cur_pack
, cur_object
;
309 size_t alloc_objects
, total_objects
= 0;
310 struct midx_fanout fanout
= { 0 };
311 struct pack_midx_entry
*deduplicated_entries
= NULL
;
312 uint32_t start_pack
= m
? m
->num_packs
: 0;
314 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++)
315 total_objects
= st_add(total_objects
,
316 info
[cur_pack
].p
->num_objects
);
319 * As we de-duplicate by fanout value, we expect the fanout
320 * slices to be evenly distributed, with some noise. Hence,
321 * allocate slightly more than one 256th.
323 alloc_objects
= fanout
.alloc
= total_objects
> 3200 ? total_objects
/ 200 : 16;
325 ALLOC_ARRAY(fanout
.entries
, fanout
.alloc
);
326 ALLOC_ARRAY(deduplicated_entries
, alloc_objects
);
329 for (cur_fanout
= 0; cur_fanout
< 256; cur_fanout
++) {
333 midx_fanout_add_midx_fanout(&fanout
, m
, cur_fanout
,
336 for (cur_pack
= start_pack
; cur_pack
< nr_packs
; cur_pack
++) {
337 int preferred
= cur_pack
== preferred_pack
;
338 midx_fanout_add_pack_fanout(&fanout
,
340 preferred
, cur_fanout
);
343 if (-1 < preferred_pack
&& preferred_pack
< start_pack
)
344 midx_fanout_add_pack_fanout(&fanout
, info
,
348 midx_fanout_sort(&fanout
);
351 * The batch is now sorted by OID and then mtime (descending).
352 * Take only the first duplicate.
354 for (cur_object
= 0; cur_object
< fanout
.nr
; cur_object
++) {
355 if (cur_object
&& oideq(&fanout
.entries
[cur_object
- 1].oid
,
356 &fanout
.entries
[cur_object
].oid
))
359 ALLOC_GROW(deduplicated_entries
, st_add(*nr_objects
, 1),
361 memcpy(&deduplicated_entries
[*nr_objects
],
362 &fanout
.entries
[cur_object
],
363 sizeof(struct pack_midx_entry
));
368 free(fanout
.entries
);
369 return deduplicated_entries
;
372 static int write_midx_pack_names(struct hashfile
*f
, void *data
)
374 struct write_midx_context
*ctx
= data
;
376 unsigned char padding
[MIDX_CHUNK_ALIGNMENT
];
379 for (i
= 0; i
< ctx
->nr
; i
++) {
382 if (ctx
->info
[i
].expired
)
385 if (i
&& strcmp(ctx
->info
[i
].pack_name
, ctx
->info
[i
- 1].pack_name
) <= 0)
386 BUG("incorrect pack-file order: %s before %s",
387 ctx
->info
[i
- 1].pack_name
,
388 ctx
->info
[i
].pack_name
);
390 writelen
= strlen(ctx
->info
[i
].pack_name
) + 1;
391 hashwrite(f
, ctx
->info
[i
].pack_name
, writelen
);
395 /* add padding to be aligned */
396 i
= MIDX_CHUNK_ALIGNMENT
- (written
% MIDX_CHUNK_ALIGNMENT
);
397 if (i
< MIDX_CHUNK_ALIGNMENT
) {
398 memset(padding
, 0, sizeof(padding
));
399 hashwrite(f
, padding
, i
);
405 static int write_midx_bitmapped_packs(struct hashfile
*f
, void *data
)
407 struct write_midx_context
*ctx
= data
;
410 for (i
= 0; i
< ctx
->nr
; i
++) {
411 struct pack_info
*pack
= &ctx
->info
[i
];
415 if (pack
->bitmap_pos
== BITMAP_POS_UNKNOWN
&& pack
->bitmap_nr
)
416 BUG("pack '%s' has no bitmap position, but has %d bitmapped object(s)",
417 pack
->pack_name
, pack
->bitmap_nr
);
419 hashwrite_be32(f
, pack
->bitmap_pos
);
420 hashwrite_be32(f
, pack
->bitmap_nr
);
425 static int write_midx_oid_fanout(struct hashfile
*f
,
428 struct write_midx_context
*ctx
= data
;
429 struct pack_midx_entry
*list
= ctx
->entries
;
430 struct pack_midx_entry
*last
= ctx
->entries
+ ctx
->entries_nr
;
435 * Write the first-level table (the list is sorted,
436 * but we use a 256-entry lookup to be able to avoid
437 * having to do eight extra binary search iterations).
439 for (i
= 0; i
< 256; i
++) {
440 struct pack_midx_entry
*next
= list
;
442 while (next
< last
&& next
->oid
.hash
[0] == i
) {
447 hashwrite_be32(f
, count
);
454 static int write_midx_oid_lookup(struct hashfile
*f
,
457 struct write_midx_context
*ctx
= data
;
458 unsigned char hash_len
= the_hash_algo
->rawsz
;
459 struct pack_midx_entry
*list
= ctx
->entries
;
462 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
463 struct pack_midx_entry
*obj
= list
++;
465 if (i
< ctx
->entries_nr
- 1) {
466 struct pack_midx_entry
*next
= list
;
467 if (oidcmp(&obj
->oid
, &next
->oid
) >= 0)
468 BUG("OIDs not in order: %s >= %s",
469 oid_to_hex(&obj
->oid
),
470 oid_to_hex(&next
->oid
));
473 hashwrite(f
, obj
->oid
.hash
, (int)hash_len
);
479 static int write_midx_object_offsets(struct hashfile
*f
,
482 struct write_midx_context
*ctx
= data
;
483 struct pack_midx_entry
*list
= ctx
->entries
;
484 uint32_t i
, nr_large_offset
= 0;
486 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
487 struct pack_midx_entry
*obj
= list
++;
489 if (ctx
->pack_perm
[obj
->pack_int_id
] == PACK_EXPIRED
)
490 BUG("object %s is in an expired pack with int-id %d",
491 oid_to_hex(&obj
->oid
),
494 hashwrite_be32(f
, ctx
->pack_perm
[obj
->pack_int_id
]);
496 if (ctx
->large_offsets_needed
&& obj
->offset
>> 31)
497 hashwrite_be32(f
, MIDX_LARGE_OFFSET_NEEDED
| nr_large_offset
++);
498 else if (!ctx
->large_offsets_needed
&& obj
->offset
>> 32)
499 BUG("object %s requires a large offset (%"PRIx64
") but the MIDX is not writing large offsets!",
500 oid_to_hex(&obj
->oid
),
503 hashwrite_be32(f
, (uint32_t)obj
->offset
);
509 static int write_midx_large_offsets(struct hashfile
*f
,
512 struct write_midx_context
*ctx
= data
;
513 struct pack_midx_entry
*list
= ctx
->entries
;
514 struct pack_midx_entry
*end
= ctx
->entries
+ ctx
->entries_nr
;
515 uint32_t nr_large_offset
= ctx
->num_large_offsets
;
517 while (nr_large_offset
) {
518 struct pack_midx_entry
*obj
;
522 BUG("too many large-offset objects");
525 offset
= obj
->offset
;
530 hashwrite_be64(f
, offset
);
538 static int write_midx_revindex(struct hashfile
*f
,
541 struct write_midx_context
*ctx
= data
;
544 for (i
= 0; i
< ctx
->entries_nr
; i
++)
545 hashwrite_be32(f
, ctx
->pack_order
[i
]);
550 struct midx_pack_order_data
{
556 static int midx_pack_order_cmp(const void *va
, const void *vb
)
558 const struct midx_pack_order_data
*a
= va
, *b
= vb
;
559 if (a
->pack
< b
->pack
)
561 else if (a
->pack
> b
->pack
)
563 else if (a
->offset
< b
->offset
)
565 else if (a
->offset
> b
->offset
)
571 static uint32_t *midx_pack_order(struct write_midx_context
*ctx
)
573 struct midx_pack_order_data
*data
;
574 uint32_t *pack_order
;
577 trace2_region_enter("midx", "midx_pack_order", the_repository
);
579 ALLOC_ARRAY(data
, ctx
->entries_nr
);
580 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
581 struct pack_midx_entry
*e
= &ctx
->entries
[i
];
583 data
[i
].pack
= ctx
->pack_perm
[e
->pack_int_id
];
585 data
[i
].pack
|= (1U << 31);
586 data
[i
].offset
= e
->offset
;
589 QSORT(data
, ctx
->entries_nr
, midx_pack_order_cmp
);
591 ALLOC_ARRAY(pack_order
, ctx
->entries_nr
);
592 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
593 struct pack_midx_entry
*e
= &ctx
->entries
[data
[i
].nr
];
594 struct pack_info
*pack
= &ctx
->info
[ctx
->pack_perm
[e
->pack_int_id
]];
595 if (pack
->bitmap_pos
== BITMAP_POS_UNKNOWN
)
596 pack
->bitmap_pos
= i
;
598 pack_order
[i
] = data
[i
].nr
;
600 for (i
= 0; i
< ctx
->nr
; i
++) {
601 struct pack_info
*pack
= &ctx
->info
[ctx
->pack_perm
[i
]];
602 if (pack
->bitmap_pos
== BITMAP_POS_UNKNOWN
)
603 pack
->bitmap_pos
= 0;
607 trace2_region_leave("midx", "midx_pack_order", the_repository
);
612 static void write_midx_reverse_index(char *midx_name
, unsigned char *midx_hash
,
613 struct write_midx_context
*ctx
)
615 struct strbuf buf
= STRBUF_INIT
;
616 const char *tmp_file
;
618 trace2_region_enter("midx", "write_midx_reverse_index", the_repository
);
620 strbuf_addf(&buf
, "%s-%s.rev", midx_name
, hash_to_hex(midx_hash
));
622 tmp_file
= write_rev_file_order(NULL
, ctx
->pack_order
, ctx
->entries_nr
,
623 midx_hash
, WRITE_REV
);
625 if (finalize_object_file(tmp_file
, buf
.buf
))
626 die(_("cannot store reverse index file"));
628 strbuf_release(&buf
);
630 trace2_region_leave("midx", "write_midx_reverse_index", the_repository
);
633 static void prepare_midx_packing_data(struct packing_data
*pdata
,
634 struct write_midx_context
*ctx
)
638 trace2_region_enter("midx", "prepare_midx_packing_data", the_repository
);
640 memset(pdata
, 0, sizeof(struct packing_data
));
641 prepare_packing_data(the_repository
, pdata
);
643 for (i
= 0; i
< ctx
->entries_nr
; i
++) {
644 struct pack_midx_entry
*from
= &ctx
->entries
[ctx
->pack_order
[i
]];
645 struct object_entry
*to
= packlist_alloc(pdata
, &from
->oid
);
647 oe_set_in_pack(pdata
, to
,
648 ctx
->info
[ctx
->pack_perm
[from
->pack_int_id
]].p
);
651 trace2_region_leave("midx", "prepare_midx_packing_data", the_repository
);
654 static int add_ref_to_pending(const char *refname
,
655 const struct object_id
*oid
,
656 int flag
, void *cb_data
)
658 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
659 struct object_id peeled
;
660 struct object
*object
;
662 if ((flag
& REF_ISSYMREF
) && (flag
& REF_ISBROKEN
)) {
663 warning("symbolic ref is dangling: %s", refname
);
667 if (!peel_iterated_oid(oid
, &peeled
))
670 object
= parse_object_or_die(oid
, refname
);
671 if (object
->type
!= OBJ_COMMIT
)
674 add_pending_object(revs
, object
, "");
675 if (bitmap_is_preferred_refname(revs
->repo
, refname
))
676 object
->flags
|= NEEDS_BITMAP
;
680 struct bitmap_commit_cb
{
681 struct commit
**commits
;
682 size_t commits_nr
, commits_alloc
;
684 struct write_midx_context
*ctx
;
687 static const struct object_id
*bitmap_oid_access(size_t index
,
688 const void *_entries
)
690 const struct pack_midx_entry
*entries
= _entries
;
691 return &entries
[index
].oid
;
694 static void bitmap_show_commit(struct commit
*commit
, void *_data
)
696 struct bitmap_commit_cb
*data
= _data
;
697 int pos
= oid_pos(&commit
->object
.oid
, data
->ctx
->entries
,
698 data
->ctx
->entries_nr
,
703 ALLOC_GROW(data
->commits
, data
->commits_nr
+ 1, data
->commits_alloc
);
704 data
->commits
[data
->commits_nr
++] = commit
;
707 static int read_refs_snapshot(const char *refs_snapshot
,
708 struct rev_info
*revs
)
710 struct strbuf buf
= STRBUF_INIT
;
711 struct object_id oid
;
712 FILE *f
= xfopen(refs_snapshot
, "r");
714 while (strbuf_getline(&buf
, f
) != EOF
) {
715 struct object
*object
;
718 const char *end
= NULL
;
720 if (buf
.len
&& *buf
.buf
== '+') {
725 if (parse_oid_hex(hex
, &oid
, &end
) < 0)
726 die(_("could not parse line: %s"), buf
.buf
);
728 die(_("malformed line: %s"), buf
.buf
);
730 object
= parse_object_or_die(&oid
, NULL
);
732 object
->flags
|= NEEDS_BITMAP
;
734 add_pending_object(revs
, object
, "");
738 strbuf_release(&buf
);
741 static struct commit
**find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p
,
742 const char *refs_snapshot
,
743 struct write_midx_context
*ctx
)
745 struct rev_info revs
;
746 struct bitmap_commit_cb cb
= {0};
748 trace2_region_enter("midx", "find_commits_for_midx_bitmap",
753 repo_init_revisions(the_repository
, &revs
, NULL
);
755 read_refs_snapshot(refs_snapshot
, &revs
);
757 setup_revisions(0, NULL
, &revs
, NULL
);
758 refs_for_each_ref(get_main_ref_store(the_repository
),
759 add_ref_to_pending
, &revs
);
763 * Skipping promisor objects here is intentional, since it only excludes
764 * them from the list of reachable commits that we want to select from
765 * when computing the selection of MIDX'd commits to receive bitmaps.
767 * Reachability bitmaps do require that their objects be closed under
768 * reachability, but fetching any objects missing from promisors at this
769 * point is too late. But, if one of those objects can be reached from
770 * an another object that is included in the bitmap, then we will
771 * complain later that we don't have reachability closure (and fail
774 fetch_if_missing
= 0;
775 revs
.exclude_promisor_objects
= 1;
777 if (prepare_revision_walk(&revs
))
778 die(_("revision walk setup failed"));
780 traverse_commit_list(&revs
, bitmap_show_commit
, NULL
, &cb
);
781 if (indexed_commits_nr_p
)
782 *indexed_commits_nr_p
= cb
.commits_nr
;
784 release_revisions(&revs
);
786 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
792 static int write_midx_bitmap(const char *midx_name
,
793 const unsigned char *midx_hash
,
794 struct packing_data
*pdata
,
795 struct commit
**commits
,
797 uint32_t *pack_order
,
801 uint16_t options
= 0;
802 struct pack_idx_entry
**index
;
803 char *bitmap_name
= xstrfmt("%s-%s.bitmap", midx_name
,
804 hash_to_hex(midx_hash
));
806 trace2_region_enter("midx", "write_midx_bitmap", the_repository
);
808 if (flags
& MIDX_WRITE_BITMAP_HASH_CACHE
)
809 options
|= BITMAP_OPT_HASH_CACHE
;
811 if (flags
& MIDX_WRITE_BITMAP_LOOKUP_TABLE
)
812 options
|= BITMAP_OPT_LOOKUP_TABLE
;
815 * Build the MIDX-order index based on pdata.objects (which is already
816 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
819 ALLOC_ARRAY(index
, pdata
->nr_objects
);
820 for (i
= 0; i
< pdata
->nr_objects
; i
++)
821 index
[i
] = &pdata
->objects
[i
].idx
;
823 bitmap_writer_show_progress(flags
& MIDX_PROGRESS
);
824 bitmap_writer_build_type_index(pdata
, index
, pdata
->nr_objects
);
827 * bitmap_writer_finish expects objects in lex order, but pack_order
828 * gives us exactly that. use it directly instead of re-sorting the
831 * This changes the order of objects in 'index' between
832 * bitmap_writer_build_type_index and bitmap_writer_finish.
834 * The same re-ordering takes place in the single-pack bitmap code via
835 * write_idx_file(), which is called by finish_tmp_packfile(), which
836 * happens between bitmap_writer_build_type_index() and
837 * bitmap_writer_finish().
839 for (i
= 0; i
< pdata
->nr_objects
; i
++)
840 index
[pack_order
[i
]] = &pdata
->objects
[i
].idx
;
842 bitmap_writer_select_commits(commits
, commits_nr
, -1);
843 ret
= bitmap_writer_build(pdata
);
847 bitmap_writer_set_checksum(midx_hash
);
848 bitmap_writer_finish(index
, pdata
->nr_objects
, bitmap_name
, options
);
854 trace2_region_leave("midx", "write_midx_bitmap", the_repository
);
859 static struct multi_pack_index
*lookup_multi_pack_index(struct repository
*r
,
860 const char *object_dir
)
862 struct multi_pack_index
*result
= NULL
;
863 struct multi_pack_index
*cur
;
864 char *obj_dir_real
= real_pathdup(object_dir
, 1);
865 struct strbuf cur_path_real
= STRBUF_INIT
;
867 /* Ensure the given object_dir is local, or a known alternate. */
868 find_odb(r
, obj_dir_real
);
870 for (cur
= get_multi_pack_index(r
); cur
; cur
= cur
->next
) {
871 strbuf_realpath(&cur_path_real
, cur
->object_dir
, 1);
872 if (!strcmp(obj_dir_real
, cur_path_real
.buf
)) {
880 strbuf_release(&cur_path_real
);
884 static int write_midx_internal(const char *object_dir
,
885 struct string_list
*packs_to_include
,
886 struct string_list
*packs_to_drop
,
887 const char *preferred_pack_name
,
888 const char *refs_snapshot
,
891 struct strbuf midx_name
= STRBUF_INIT
;
892 unsigned char midx_hash
[GIT_MAX_RAWSZ
];
894 struct hashfile
*f
= NULL
;
896 struct write_midx_context ctx
= { 0 };
897 int bitmapped_packs_concat_len
= 0;
898 int pack_name_concat_len
= 0;
899 int dropped_packs
= 0;
901 struct chunkfile
*cf
;
903 trace2_region_enter("midx", "write_midx_internal", the_repository
);
905 get_midx_filename(&midx_name
, object_dir
);
906 if (safe_create_leading_directories(midx_name
.buf
))
907 die_errno(_("unable to create leading directories of %s"),
910 if (!packs_to_include
) {
912 * Only reference an existing MIDX when not filtering which
913 * packs to include, since all packs and objects are copied
914 * blindly from an existing MIDX if one is present.
916 ctx
.m
= lookup_multi_pack_index(the_repository
, object_dir
);
919 if (ctx
.m
&& !midx_checksum_valid(ctx
.m
)) {
920 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
925 ctx
.alloc
= ctx
.m
? ctx
.m
->num_packs
: 16;
927 ALLOC_ARRAY(ctx
.info
, ctx
.alloc
);
930 for (i
= 0; i
< ctx
.m
->num_packs
; i
++) {
931 ALLOC_GROW(ctx
.info
, ctx
.nr
+ 1, ctx
.alloc
);
933 if (flags
& MIDX_WRITE_REV_INDEX
) {
935 * If generating a reverse index, need to have
936 * packed_git's loaded to compare their
937 * mtimes and object count.
939 if (prepare_midx_pack(the_repository
, ctx
.m
, i
)) {
940 error(_("could not load pack"));
945 if (open_pack_index(ctx
.m
->packs
[i
]))
946 die(_("could not open index for %s"),
947 ctx
.m
->packs
[i
]->pack_name
);
950 fill_pack_info(&ctx
.info
[ctx
.nr
++], ctx
.m
->packs
[i
],
951 ctx
.m
->pack_names
[i
], i
);
955 ctx
.pack_paths_checked
= 0;
956 if (flags
& MIDX_PROGRESS
)
957 ctx
.progress
= start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
961 ctx
.to_include
= packs_to_include
;
963 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &ctx
);
964 stop_progress(&ctx
.progress
);
966 if ((ctx
.m
&& ctx
.nr
== ctx
.m
->num_packs
) &&
967 !(packs_to_include
|| packs_to_drop
)) {
968 struct bitmap_index
*bitmap_git
;
970 int want_bitmap
= flags
& MIDX_WRITE_BITMAP
;
972 bitmap_git
= prepare_midx_bitmap_git(ctx
.m
);
973 bitmap_exists
= bitmap_git
&& bitmap_is_midx(bitmap_git
);
974 free_bitmap_index(bitmap_git
);
976 if (bitmap_exists
|| !want_bitmap
) {
978 * The correct MIDX already exists, and so does a
979 * corresponding bitmap (or one wasn't requested).
982 clear_midx_files_ext(object_dir
, ".bitmap",
988 if (preferred_pack_name
) {
989 ctx
.preferred_pack_idx
= -1;
991 for (i
= 0; i
< ctx
.nr
; i
++) {
992 if (!cmp_idx_or_pack_name(preferred_pack_name
,
993 ctx
.info
[i
].pack_name
)) {
994 ctx
.preferred_pack_idx
= i
;
999 if (ctx
.preferred_pack_idx
== -1)
1000 warning(_("unknown preferred pack: '%s'"),
1001 preferred_pack_name
);
1002 } else if (ctx
.nr
&&
1003 (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
))) {
1004 struct packed_git
*oldest
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1005 ctx
.preferred_pack_idx
= 0;
1007 if (packs_to_drop
&& packs_to_drop
->nr
)
1008 BUG("cannot write a MIDX bitmap during expiration");
1011 * set a preferred pack when writing a bitmap to ensure that
1012 * the pack from which the first object is selected in pseudo
1013 * pack-order has all of its objects selected from that pack
1014 * (and not another pack containing a duplicate)
1016 for (i
= 1; i
< ctx
.nr
; i
++) {
1017 struct packed_git
*p
= ctx
.info
[i
].p
;
1019 if (!oldest
->num_objects
|| p
->mtime
< oldest
->mtime
) {
1021 ctx
.preferred_pack_idx
= i
;
1025 if (!oldest
->num_objects
) {
1027 * If all packs are empty; unset the preferred index.
1028 * This is acceptable since there will be no duplicate
1029 * objects to resolve, so the preferred value doesn't
1032 ctx
.preferred_pack_idx
= -1;
1036 * otherwise don't mark any pack as preferred to avoid
1037 * interfering with expiration logic below
1039 ctx
.preferred_pack_idx
= -1;
1042 if (ctx
.preferred_pack_idx
> -1) {
1043 struct packed_git
*preferred
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1044 if (!preferred
->num_objects
) {
1045 error(_("cannot select preferred pack %s with no objects"),
1046 preferred
->pack_name
);
1052 ctx
.entries
= get_sorted_entries(ctx
.m
, ctx
.info
, ctx
.nr
, &ctx
.entries_nr
,
1053 ctx
.preferred_pack_idx
);
1055 ctx
.large_offsets_needed
= 0;
1056 for (i
= 0; i
< ctx
.entries_nr
; i
++) {
1057 if (ctx
.entries
[i
].offset
> 0x7fffffff)
1058 ctx
.num_large_offsets
++;
1059 if (ctx
.entries
[i
].offset
> 0xffffffff)
1060 ctx
.large_offsets_needed
= 1;
1063 QSORT(ctx
.info
, ctx
.nr
, pack_info_compare
);
1065 if (packs_to_drop
&& packs_to_drop
->nr
) {
1067 int missing_drops
= 0;
1069 for (i
= 0; i
< ctx
.nr
&& drop_index
< packs_to_drop
->nr
; i
++) {
1070 int cmp
= strcmp(ctx
.info
[i
].pack_name
,
1071 packs_to_drop
->items
[drop_index
].string
);
1075 ctx
.info
[i
].expired
= 1;
1076 } else if (cmp
> 0) {
1077 error(_("did not see pack-file %s to drop"),
1078 packs_to_drop
->items
[drop_index
].string
);
1083 ctx
.info
[i
].expired
= 0;
1087 if (missing_drops
) {
1094 * pack_perm stores a permutation between pack-int-ids from the
1095 * previous multi-pack-index to the new one we are writing:
1097 * pack_perm[old_id] = new_id
1099 ALLOC_ARRAY(ctx
.pack_perm
, ctx
.nr
);
1100 for (i
= 0; i
< ctx
.nr
; i
++) {
1101 if (ctx
.info
[i
].expired
) {
1103 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = PACK_EXPIRED
;
1105 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = i
- dropped_packs
;
1109 for (i
= 0; i
< ctx
.nr
; i
++) {
1110 if (ctx
.info
[i
].expired
)
1112 pack_name_concat_len
+= strlen(ctx
.info
[i
].pack_name
) + 1;
1113 bitmapped_packs_concat_len
+= 2 * sizeof(uint32_t);
1116 /* Check that the preferred pack wasn't expired (if given). */
1117 if (preferred_pack_name
) {
1118 struct pack_info
*preferred
= bsearch(preferred_pack_name
,
1121 idx_or_pack_name_cmp
);
1123 uint32_t perm
= ctx
.pack_perm
[preferred
->orig_pack_int_id
];
1124 if (perm
== PACK_EXPIRED
)
1125 warning(_("preferred pack '%s' is expired"),
1126 preferred_pack_name
);
1130 if (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
1131 pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
1132 (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
1134 hold_lock_file_for_update(&lk
, midx_name
.buf
, LOCK_DIE_ON_ERROR
);
1135 f
= hashfd(get_lock_file_fd(&lk
), get_lock_file_path(&lk
));
1137 if (ctx
.nr
- dropped_packs
== 0) {
1138 error(_("no pack files to index."));
1143 if (!ctx
.entries_nr
) {
1144 if (flags
& MIDX_WRITE_BITMAP
)
1145 warning(_("refusing to write multi-pack .bitmap without any objects"));
1146 flags
&= ~(MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
);
1149 cf
= init_chunkfile(f
);
1151 add_chunk(cf
, MIDX_CHUNKID_PACKNAMES
, pack_name_concat_len
,
1152 write_midx_pack_names
);
1153 add_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, MIDX_CHUNK_FANOUT_SIZE
,
1154 write_midx_oid_fanout
);
1155 add_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
,
1156 st_mult(ctx
.entries_nr
, the_hash_algo
->rawsz
),
1157 write_midx_oid_lookup
);
1158 add_chunk(cf
, MIDX_CHUNKID_OBJECTOFFSETS
,
1159 st_mult(ctx
.entries_nr
, MIDX_CHUNK_OFFSET_WIDTH
),
1160 write_midx_object_offsets
);
1162 if (ctx
.large_offsets_needed
)
1163 add_chunk(cf
, MIDX_CHUNKID_LARGEOFFSETS
,
1164 st_mult(ctx
.num_large_offsets
,
1165 MIDX_CHUNK_LARGE_OFFSET_WIDTH
),
1166 write_midx_large_offsets
);
1168 if (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
)) {
1169 ctx
.pack_order
= midx_pack_order(&ctx
);
1170 add_chunk(cf
, MIDX_CHUNKID_REVINDEX
,
1171 st_mult(ctx
.entries_nr
, sizeof(uint32_t)),
1172 write_midx_revindex
);
1173 add_chunk(cf
, MIDX_CHUNKID_BITMAPPEDPACKS
,
1174 bitmapped_packs_concat_len
,
1175 write_midx_bitmapped_packs
);
1178 write_midx_header(f
, get_num_chunks(cf
), ctx
.nr
- dropped_packs
);
1179 write_chunkfile(cf
, &ctx
);
1181 finalize_hashfile(f
, midx_hash
, FSYNC_COMPONENT_PACK_METADATA
,
1182 CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
1185 if (flags
& MIDX_WRITE_REV_INDEX
&&
1186 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1187 write_midx_reverse_index(midx_name
.buf
, midx_hash
, &ctx
);
1189 if (flags
& MIDX_WRITE_BITMAP
) {
1190 struct packing_data pdata
;
1191 struct commit
**commits
;
1192 uint32_t commits_nr
;
1194 if (!ctx
.entries_nr
)
1195 BUG("cannot write a bitmap without any objects");
1197 prepare_midx_packing_data(&pdata
, &ctx
);
1199 commits
= find_commits_for_midx_bitmap(&commits_nr
, refs_snapshot
, &ctx
);
1202 * The previous steps translated the information from
1203 * 'entries' into information suitable for constructing
1204 * bitmaps. We no longer need that array, so clear it to
1205 * reduce memory pressure.
1207 FREE_AND_NULL(ctx
.entries
);
1210 if (write_midx_bitmap(midx_name
.buf
, midx_hash
, &pdata
,
1211 commits
, commits_nr
, ctx
.pack_order
,
1213 error(_("could not write multi-pack bitmap"));
1215 clear_packing_data(&pdata
);
1220 clear_packing_data(&pdata
);
1224 * NOTE: Do not use ctx.entries beyond this point, since it might
1225 * have been freed in the previous if block.
1229 close_object_store(the_repository
->objects
);
1231 if (commit_lock_file(&lk
) < 0)
1232 die_errno(_("could not write multi-pack-index"));
1234 clear_midx_files_ext(object_dir
, ".bitmap", midx_hash
);
1235 clear_midx_files_ext(object_dir
, ".rev", midx_hash
);
1238 for (i
= 0; i
< ctx
.nr
; i
++) {
1239 if (ctx
.info
[i
].p
) {
1240 close_pack(ctx
.info
[i
].p
);
1241 free(ctx
.info
[i
].p
);
1243 free(ctx
.info
[i
].pack_name
);
1248 free(ctx
.pack_perm
);
1249 free(ctx
.pack_order
);
1250 strbuf_release(&midx_name
);
1252 trace2_region_leave("midx", "write_midx_internal", the_repository
);
1257 int write_midx_file(const char *object_dir
,
1258 const char *preferred_pack_name
,
1259 const char *refs_snapshot
,
1262 return write_midx_internal(object_dir
, NULL
, NULL
, preferred_pack_name
,
1263 refs_snapshot
, flags
);
1266 int write_midx_file_only(const char *object_dir
,
1267 struct string_list
*packs_to_include
,
1268 const char *preferred_pack_name
,
1269 const char *refs_snapshot
,
1272 return write_midx_internal(object_dir
, packs_to_include
, NULL
,
1273 preferred_pack_name
, refs_snapshot
, flags
);
1276 int expire_midx_packs(struct repository
*r
, const char *object_dir
, unsigned flags
)
1278 uint32_t i
, *count
, result
= 0;
1279 struct string_list packs_to_drop
= STRING_LIST_INIT_DUP
;
1280 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
1281 struct progress
*progress
= NULL
;
1286 CALLOC_ARRAY(count
, m
->num_packs
);
1288 if (flags
& MIDX_PROGRESS
)
1289 progress
= start_delayed_progress(_("Counting referenced objects"),
1291 for (i
= 0; i
< m
->num_objects
; i
++) {
1292 int pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1293 count
[pack_int_id
]++;
1294 display_progress(progress
, i
+ 1);
1296 stop_progress(&progress
);
1298 if (flags
& MIDX_PROGRESS
)
1299 progress
= start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1301 for (i
= 0; i
< m
->num_packs
; i
++) {
1303 display_progress(progress
, i
+ 1);
1308 if (prepare_midx_pack(r
, m
, i
))
1311 if (m
->packs
[i
]->pack_keep
|| m
->packs
[i
]->is_cruft
)
1314 pack_name
= xstrdup(m
->packs
[i
]->pack_name
);
1315 close_pack(m
->packs
[i
]);
1317 string_list_insert(&packs_to_drop
, m
->pack_names
[i
]);
1318 unlink_pack_path(pack_name
, 0);
1321 stop_progress(&progress
);
1325 if (packs_to_drop
.nr
)
1326 result
= write_midx_internal(object_dir
, NULL
, &packs_to_drop
, NULL
, NULL
, flags
);
1328 string_list_clear(&packs_to_drop
, 0);
1333 struct repack_info
{
1335 uint32_t referenced_objects
;
1336 uint32_t pack_int_id
;
1339 static int compare_by_mtime(const void *a_
, const void *b_
)
1341 const struct repack_info
*a
, *b
;
1343 a
= (const struct repack_info
*)a_
;
1344 b
= (const struct repack_info
*)b_
;
1346 if (a
->mtime
< b
->mtime
)
1348 if (a
->mtime
> b
->mtime
)
1353 static int want_included_pack(struct repository
*r
,
1354 struct multi_pack_index
*m
,
1355 int pack_kept_objects
,
1356 uint32_t pack_int_id
)
1358 struct packed_git
*p
;
1359 if (prepare_midx_pack(r
, m
, pack_int_id
))
1361 p
= m
->packs
[pack_int_id
];
1362 if (!pack_kept_objects
&& p
->pack_keep
)
1366 if (open_pack_index(p
) || !p
->num_objects
)
1371 static void fill_included_packs_all(struct repository
*r
,
1372 struct multi_pack_index
*m
,
1373 unsigned char *include_pack
)
1376 int pack_kept_objects
= 0;
1378 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1380 for (i
= 0; i
< m
->num_packs
; i
++) {
1381 if (!want_included_pack(r
, m
, pack_kept_objects
, i
))
1384 include_pack
[i
] = 1;
1388 static void fill_included_packs_batch(struct repository
*r
,
1389 struct multi_pack_index
*m
,
1390 unsigned char *include_pack
,
1395 struct repack_info
*pack_info
;
1396 int pack_kept_objects
= 0;
1398 CALLOC_ARRAY(pack_info
, m
->num_packs
);
1400 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1402 for (i
= 0; i
< m
->num_packs
; i
++) {
1403 pack_info
[i
].pack_int_id
= i
;
1405 if (prepare_midx_pack(r
, m
, i
))
1408 pack_info
[i
].mtime
= m
->packs
[i
]->mtime
;
1411 for (i
= 0; i
< m
->num_objects
; i
++) {
1412 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1413 pack_info
[pack_int_id
].referenced_objects
++;
1416 QSORT(pack_info
, m
->num_packs
, compare_by_mtime
);
1419 for (i
= 0; total_size
< batch_size
&& i
< m
->num_packs
; i
++) {
1420 int pack_int_id
= pack_info
[i
].pack_int_id
;
1421 struct packed_git
*p
= m
->packs
[pack_int_id
];
1422 size_t expected_size
;
1424 if (!want_included_pack(r
, m
, pack_kept_objects
, pack_int_id
))
1427 expected_size
= st_mult(p
->pack_size
,
1428 pack_info
[i
].referenced_objects
);
1429 expected_size
/= p
->num_objects
;
1431 if (expected_size
>= batch_size
)
1434 total_size
+= expected_size
;
1435 include_pack
[pack_int_id
] = 1;
1441 int midx_repack(struct repository
*r
, const char *object_dir
, size_t batch_size
, unsigned flags
)
1444 uint32_t i
, packs_to_repack
= 0;
1445 unsigned char *include_pack
;
1446 struct child_process cmd
= CHILD_PROCESS_INIT
;
1448 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
1451 * When updating the default for these configuration
1452 * variables in builtin/repack.c, these must be adjusted
1455 int delta_base_offset
= 1;
1456 int use_delta_islands
= 0;
1461 CALLOC_ARRAY(include_pack
, m
->num_packs
);
1464 fill_included_packs_batch(r
, m
, include_pack
, batch_size
);
1466 fill_included_packs_all(r
, m
, include_pack
);
1468 for (i
= 0; i
< m
->num_packs
; i
++) {
1469 if (include_pack
[i
])
1472 if (packs_to_repack
<= 1)
1475 repo_config_get_bool(r
, "repack.usedeltabaseoffset", &delta_base_offset
);
1476 repo_config_get_bool(r
, "repack.usedeltaislands", &use_delta_islands
);
1478 strvec_pushl(&cmd
.args
, "pack-objects", "--stdin-packs", "--non-empty",
1481 strvec_pushf(&cmd
.args
, "%s/pack/pack", object_dir
);
1483 if (delta_base_offset
)
1484 strvec_push(&cmd
.args
, "--delta-base-offset");
1485 if (use_delta_islands
)
1486 strvec_push(&cmd
.args
, "--delta-islands");
1488 if (flags
& MIDX_PROGRESS
)
1489 strvec_push(&cmd
.args
, "--progress");
1491 strvec_push(&cmd
.args
, "-q");
1494 cmd
.in
= cmd
.out
= -1;
1496 if (start_command(&cmd
)) {
1497 error(_("could not start pack-objects"));
1502 cmd_in
= xfdopen(cmd
.in
, "w");
1503 for (i
= 0; i
< m
->num_packs
; i
++) {
1504 struct packed_git
*p
= m
->packs
[i
];
1508 if (include_pack
[i
])
1509 fprintf(cmd_in
, "%s\n", pack_basename(p
));
1511 fprintf(cmd_in
, "^%s\n", pack_basename(p
));
1515 if (finish_command(&cmd
)) {
1516 error(_("could not finish pack-objects"));
1521 result
= write_midx_internal(object_dir
, NULL
, NULL
, NULL
, NULL
, flags
);