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 for_each_ref(add_ref_to_pending
, &revs
);
762 * Skipping promisor objects here is intentional, since it only excludes
763 * them from the list of reachable commits that we want to select from
764 * when computing the selection of MIDX'd commits to receive bitmaps.
766 * Reachability bitmaps do require that their objects be closed under
767 * reachability, but fetching any objects missing from promisors at this
768 * point is too late. But, if one of those objects can be reached from
769 * an another object that is included in the bitmap, then we will
770 * complain later that we don't have reachability closure (and fail
773 fetch_if_missing
= 0;
774 revs
.exclude_promisor_objects
= 1;
776 if (prepare_revision_walk(&revs
))
777 die(_("revision walk setup failed"));
779 traverse_commit_list(&revs
, bitmap_show_commit
, NULL
, &cb
);
780 if (indexed_commits_nr_p
)
781 *indexed_commits_nr_p
= cb
.commits_nr
;
783 release_revisions(&revs
);
785 trace2_region_leave("midx", "find_commits_for_midx_bitmap",
791 static int write_midx_bitmap(const char *midx_name
,
792 const unsigned char *midx_hash
,
793 struct packing_data
*pdata
,
794 struct commit
**commits
,
796 uint32_t *pack_order
,
800 uint16_t options
= 0;
801 struct pack_idx_entry
**index
;
802 char *bitmap_name
= xstrfmt("%s-%s.bitmap", midx_name
,
803 hash_to_hex(midx_hash
));
805 trace2_region_enter("midx", "write_midx_bitmap", the_repository
);
807 if (flags
& MIDX_WRITE_BITMAP_HASH_CACHE
)
808 options
|= BITMAP_OPT_HASH_CACHE
;
810 if (flags
& MIDX_WRITE_BITMAP_LOOKUP_TABLE
)
811 options
|= BITMAP_OPT_LOOKUP_TABLE
;
814 * Build the MIDX-order index based on pdata.objects (which is already
815 * in MIDX order; c.f., 'midx_pack_order_cmp()' for the definition of
818 ALLOC_ARRAY(index
, pdata
->nr_objects
);
819 for (i
= 0; i
< pdata
->nr_objects
; i
++)
820 index
[i
] = &pdata
->objects
[i
].idx
;
822 bitmap_writer_show_progress(flags
& MIDX_PROGRESS
);
823 bitmap_writer_build_type_index(pdata
, index
, pdata
->nr_objects
);
826 * bitmap_writer_finish expects objects in lex order, but pack_order
827 * gives us exactly that. use it directly instead of re-sorting the
830 * This changes the order of objects in 'index' between
831 * bitmap_writer_build_type_index and bitmap_writer_finish.
833 * The same re-ordering takes place in the single-pack bitmap code via
834 * write_idx_file(), which is called by finish_tmp_packfile(), which
835 * happens between bitmap_writer_build_type_index() and
836 * bitmap_writer_finish().
838 for (i
= 0; i
< pdata
->nr_objects
; i
++)
839 index
[pack_order
[i
]] = &pdata
->objects
[i
].idx
;
841 bitmap_writer_select_commits(commits
, commits_nr
, -1);
842 ret
= bitmap_writer_build(pdata
);
846 bitmap_writer_set_checksum(midx_hash
);
847 bitmap_writer_finish(index
, pdata
->nr_objects
, bitmap_name
, options
);
853 trace2_region_leave("midx", "write_midx_bitmap", the_repository
);
858 static struct multi_pack_index
*lookup_multi_pack_index(struct repository
*r
,
859 const char *object_dir
)
861 struct multi_pack_index
*result
= NULL
;
862 struct multi_pack_index
*cur
;
863 char *obj_dir_real
= real_pathdup(object_dir
, 1);
864 struct strbuf cur_path_real
= STRBUF_INIT
;
866 /* Ensure the given object_dir is local, or a known alternate. */
867 find_odb(r
, obj_dir_real
);
869 for (cur
= get_multi_pack_index(r
); cur
; cur
= cur
->next
) {
870 strbuf_realpath(&cur_path_real
, cur
->object_dir
, 1);
871 if (!strcmp(obj_dir_real
, cur_path_real
.buf
)) {
879 strbuf_release(&cur_path_real
);
883 static int write_midx_internal(const char *object_dir
,
884 struct string_list
*packs_to_include
,
885 struct string_list
*packs_to_drop
,
886 const char *preferred_pack_name
,
887 const char *refs_snapshot
,
890 struct strbuf midx_name
= STRBUF_INIT
;
891 unsigned char midx_hash
[GIT_MAX_RAWSZ
];
893 struct hashfile
*f
= NULL
;
895 struct write_midx_context ctx
= { 0 };
896 int bitmapped_packs_concat_len
= 0;
897 int pack_name_concat_len
= 0;
898 int dropped_packs
= 0;
900 struct chunkfile
*cf
;
902 trace2_region_enter("midx", "write_midx_internal", the_repository
);
904 get_midx_filename(&midx_name
, object_dir
);
905 if (safe_create_leading_directories(midx_name
.buf
))
906 die_errno(_("unable to create leading directories of %s"),
909 if (!packs_to_include
) {
911 * Only reference an existing MIDX when not filtering which
912 * packs to include, since all packs and objects are copied
913 * blindly from an existing MIDX if one is present.
915 ctx
.m
= lookup_multi_pack_index(the_repository
, object_dir
);
918 if (ctx
.m
&& !midx_checksum_valid(ctx
.m
)) {
919 warning(_("ignoring existing multi-pack-index; checksum mismatch"));
924 ctx
.alloc
= ctx
.m
? ctx
.m
->num_packs
: 16;
926 ALLOC_ARRAY(ctx
.info
, ctx
.alloc
);
929 for (i
= 0; i
< ctx
.m
->num_packs
; i
++) {
930 ALLOC_GROW(ctx
.info
, ctx
.nr
+ 1, ctx
.alloc
);
932 if (flags
& MIDX_WRITE_REV_INDEX
) {
934 * If generating a reverse index, need to have
935 * packed_git's loaded to compare their
936 * mtimes and object count.
938 if (prepare_midx_pack(the_repository
, ctx
.m
, i
)) {
939 error(_("could not load pack"));
944 if (open_pack_index(ctx
.m
->packs
[i
]))
945 die(_("could not open index for %s"),
946 ctx
.m
->packs
[i
]->pack_name
);
949 fill_pack_info(&ctx
.info
[ctx
.nr
++], ctx
.m
->packs
[i
],
950 ctx
.m
->pack_names
[i
], i
);
954 ctx
.pack_paths_checked
= 0;
955 if (flags
& MIDX_PROGRESS
)
956 ctx
.progress
= start_delayed_progress(_("Adding packfiles to multi-pack-index"), 0);
960 ctx
.to_include
= packs_to_include
;
962 for_each_file_in_pack_dir(object_dir
, add_pack_to_midx
, &ctx
);
963 stop_progress(&ctx
.progress
);
965 if ((ctx
.m
&& ctx
.nr
== ctx
.m
->num_packs
) &&
966 !(packs_to_include
|| packs_to_drop
)) {
967 struct bitmap_index
*bitmap_git
;
969 int want_bitmap
= flags
& MIDX_WRITE_BITMAP
;
971 bitmap_git
= prepare_midx_bitmap_git(ctx
.m
);
972 bitmap_exists
= bitmap_git
&& bitmap_is_midx(bitmap_git
);
973 free_bitmap_index(bitmap_git
);
975 if (bitmap_exists
|| !want_bitmap
) {
977 * The correct MIDX already exists, and so does a
978 * corresponding bitmap (or one wasn't requested).
981 clear_midx_files_ext(object_dir
, ".bitmap",
987 if (preferred_pack_name
) {
988 ctx
.preferred_pack_idx
= -1;
990 for (i
= 0; i
< ctx
.nr
; i
++) {
991 if (!cmp_idx_or_pack_name(preferred_pack_name
,
992 ctx
.info
[i
].pack_name
)) {
993 ctx
.preferred_pack_idx
= i
;
998 if (ctx
.preferred_pack_idx
== -1)
999 warning(_("unknown preferred pack: '%s'"),
1000 preferred_pack_name
);
1001 } else if (ctx
.nr
&&
1002 (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
))) {
1003 struct packed_git
*oldest
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1004 ctx
.preferred_pack_idx
= 0;
1006 if (packs_to_drop
&& packs_to_drop
->nr
)
1007 BUG("cannot write a MIDX bitmap during expiration");
1010 * set a preferred pack when writing a bitmap to ensure that
1011 * the pack from which the first object is selected in pseudo
1012 * pack-order has all of its objects selected from that pack
1013 * (and not another pack containing a duplicate)
1015 for (i
= 1; i
< ctx
.nr
; i
++) {
1016 struct packed_git
*p
= ctx
.info
[i
].p
;
1018 if (!oldest
->num_objects
|| p
->mtime
< oldest
->mtime
) {
1020 ctx
.preferred_pack_idx
= i
;
1024 if (!oldest
->num_objects
) {
1026 * If all packs are empty; unset the preferred index.
1027 * This is acceptable since there will be no duplicate
1028 * objects to resolve, so the preferred value doesn't
1031 ctx
.preferred_pack_idx
= -1;
1035 * otherwise don't mark any pack as preferred to avoid
1036 * interfering with expiration logic below
1038 ctx
.preferred_pack_idx
= -1;
1041 if (ctx
.preferred_pack_idx
> -1) {
1042 struct packed_git
*preferred
= ctx
.info
[ctx
.preferred_pack_idx
].p
;
1043 if (!preferred
->num_objects
) {
1044 error(_("cannot select preferred pack %s with no objects"),
1045 preferred
->pack_name
);
1051 ctx
.entries
= get_sorted_entries(ctx
.m
, ctx
.info
, ctx
.nr
, &ctx
.entries_nr
,
1052 ctx
.preferred_pack_idx
);
1054 ctx
.large_offsets_needed
= 0;
1055 for (i
= 0; i
< ctx
.entries_nr
; i
++) {
1056 if (ctx
.entries
[i
].offset
> 0x7fffffff)
1057 ctx
.num_large_offsets
++;
1058 if (ctx
.entries
[i
].offset
> 0xffffffff)
1059 ctx
.large_offsets_needed
= 1;
1062 QSORT(ctx
.info
, ctx
.nr
, pack_info_compare
);
1064 if (packs_to_drop
&& packs_to_drop
->nr
) {
1066 int missing_drops
= 0;
1068 for (i
= 0; i
< ctx
.nr
&& drop_index
< packs_to_drop
->nr
; i
++) {
1069 int cmp
= strcmp(ctx
.info
[i
].pack_name
,
1070 packs_to_drop
->items
[drop_index
].string
);
1074 ctx
.info
[i
].expired
= 1;
1075 } else if (cmp
> 0) {
1076 error(_("did not see pack-file %s to drop"),
1077 packs_to_drop
->items
[drop_index
].string
);
1082 ctx
.info
[i
].expired
= 0;
1086 if (missing_drops
) {
1093 * pack_perm stores a permutation between pack-int-ids from the
1094 * previous multi-pack-index to the new one we are writing:
1096 * pack_perm[old_id] = new_id
1098 ALLOC_ARRAY(ctx
.pack_perm
, ctx
.nr
);
1099 for (i
= 0; i
< ctx
.nr
; i
++) {
1100 if (ctx
.info
[i
].expired
) {
1102 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = PACK_EXPIRED
;
1104 ctx
.pack_perm
[ctx
.info
[i
].orig_pack_int_id
] = i
- dropped_packs
;
1108 for (i
= 0; i
< ctx
.nr
; i
++) {
1109 if (ctx
.info
[i
].expired
)
1111 pack_name_concat_len
+= strlen(ctx
.info
[i
].pack_name
) + 1;
1112 bitmapped_packs_concat_len
+= 2 * sizeof(uint32_t);
1115 /* Check that the preferred pack wasn't expired (if given). */
1116 if (preferred_pack_name
) {
1117 struct pack_info
*preferred
= bsearch(preferred_pack_name
,
1120 idx_or_pack_name_cmp
);
1122 uint32_t perm
= ctx
.pack_perm
[preferred
->orig_pack_int_id
];
1123 if (perm
== PACK_EXPIRED
)
1124 warning(_("preferred pack '%s' is expired"),
1125 preferred_pack_name
);
1129 if (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
)
1130 pack_name_concat_len
+= MIDX_CHUNK_ALIGNMENT
-
1131 (pack_name_concat_len
% MIDX_CHUNK_ALIGNMENT
);
1133 hold_lock_file_for_update(&lk
, midx_name
.buf
, LOCK_DIE_ON_ERROR
);
1134 f
= hashfd(get_lock_file_fd(&lk
), get_lock_file_path(&lk
));
1136 if (ctx
.nr
- dropped_packs
== 0) {
1137 error(_("no pack files to index."));
1142 if (!ctx
.entries_nr
) {
1143 if (flags
& MIDX_WRITE_BITMAP
)
1144 warning(_("refusing to write multi-pack .bitmap without any objects"));
1145 flags
&= ~(MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
);
1148 cf
= init_chunkfile(f
);
1150 add_chunk(cf
, MIDX_CHUNKID_PACKNAMES
, pack_name_concat_len
,
1151 write_midx_pack_names
);
1152 add_chunk(cf
, MIDX_CHUNKID_OIDFANOUT
, MIDX_CHUNK_FANOUT_SIZE
,
1153 write_midx_oid_fanout
);
1154 add_chunk(cf
, MIDX_CHUNKID_OIDLOOKUP
,
1155 st_mult(ctx
.entries_nr
, the_hash_algo
->rawsz
),
1156 write_midx_oid_lookup
);
1157 add_chunk(cf
, MIDX_CHUNKID_OBJECTOFFSETS
,
1158 st_mult(ctx
.entries_nr
, MIDX_CHUNK_OFFSET_WIDTH
),
1159 write_midx_object_offsets
);
1161 if (ctx
.large_offsets_needed
)
1162 add_chunk(cf
, MIDX_CHUNKID_LARGEOFFSETS
,
1163 st_mult(ctx
.num_large_offsets
,
1164 MIDX_CHUNK_LARGE_OFFSET_WIDTH
),
1165 write_midx_large_offsets
);
1167 if (flags
& (MIDX_WRITE_REV_INDEX
| MIDX_WRITE_BITMAP
)) {
1168 ctx
.pack_order
= midx_pack_order(&ctx
);
1169 add_chunk(cf
, MIDX_CHUNKID_REVINDEX
,
1170 st_mult(ctx
.entries_nr
, sizeof(uint32_t)),
1171 write_midx_revindex
);
1172 add_chunk(cf
, MIDX_CHUNKID_BITMAPPEDPACKS
,
1173 bitmapped_packs_concat_len
,
1174 write_midx_bitmapped_packs
);
1177 write_midx_header(f
, get_num_chunks(cf
), ctx
.nr
- dropped_packs
);
1178 write_chunkfile(cf
, &ctx
);
1180 finalize_hashfile(f
, midx_hash
, FSYNC_COMPONENT_PACK_METADATA
,
1181 CSUM_FSYNC
| CSUM_HASH_IN_STREAM
);
1184 if (flags
& MIDX_WRITE_REV_INDEX
&&
1185 git_env_bool("GIT_TEST_MIDX_WRITE_REV", 0))
1186 write_midx_reverse_index(midx_name
.buf
, midx_hash
, &ctx
);
1188 if (flags
& MIDX_WRITE_BITMAP
) {
1189 struct packing_data pdata
;
1190 struct commit
**commits
;
1191 uint32_t commits_nr
;
1193 if (!ctx
.entries_nr
)
1194 BUG("cannot write a bitmap without any objects");
1196 prepare_midx_packing_data(&pdata
, &ctx
);
1198 commits
= find_commits_for_midx_bitmap(&commits_nr
, refs_snapshot
, &ctx
);
1201 * The previous steps translated the information from
1202 * 'entries' into information suitable for constructing
1203 * bitmaps. We no longer need that array, so clear it to
1204 * reduce memory pressure.
1206 FREE_AND_NULL(ctx
.entries
);
1209 if (write_midx_bitmap(midx_name
.buf
, midx_hash
, &pdata
,
1210 commits
, commits_nr
, ctx
.pack_order
,
1212 error(_("could not write multi-pack bitmap"));
1214 clear_packing_data(&pdata
);
1219 clear_packing_data(&pdata
);
1223 * NOTE: Do not use ctx.entries beyond this point, since it might
1224 * have been freed in the previous if block.
1228 close_object_store(the_repository
->objects
);
1230 if (commit_lock_file(&lk
) < 0)
1231 die_errno(_("could not write multi-pack-index"));
1233 clear_midx_files_ext(object_dir
, ".bitmap", midx_hash
);
1234 clear_midx_files_ext(object_dir
, ".rev", midx_hash
);
1237 for (i
= 0; i
< ctx
.nr
; i
++) {
1238 if (ctx
.info
[i
].p
) {
1239 close_pack(ctx
.info
[i
].p
);
1240 free(ctx
.info
[i
].p
);
1242 free(ctx
.info
[i
].pack_name
);
1247 free(ctx
.pack_perm
);
1248 free(ctx
.pack_order
);
1249 strbuf_release(&midx_name
);
1251 trace2_region_leave("midx", "write_midx_internal", the_repository
);
1256 int write_midx_file(const char *object_dir
,
1257 const char *preferred_pack_name
,
1258 const char *refs_snapshot
,
1261 return write_midx_internal(object_dir
, NULL
, NULL
, preferred_pack_name
,
1262 refs_snapshot
, flags
);
1265 int write_midx_file_only(const char *object_dir
,
1266 struct string_list
*packs_to_include
,
1267 const char *preferred_pack_name
,
1268 const char *refs_snapshot
,
1271 return write_midx_internal(object_dir
, packs_to_include
, NULL
,
1272 preferred_pack_name
, refs_snapshot
, flags
);
1275 int expire_midx_packs(struct repository
*r
, const char *object_dir
, unsigned flags
)
1277 uint32_t i
, *count
, result
= 0;
1278 struct string_list packs_to_drop
= STRING_LIST_INIT_DUP
;
1279 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
1280 struct progress
*progress
= NULL
;
1285 CALLOC_ARRAY(count
, m
->num_packs
);
1287 if (flags
& MIDX_PROGRESS
)
1288 progress
= start_delayed_progress(_("Counting referenced objects"),
1290 for (i
= 0; i
< m
->num_objects
; i
++) {
1291 int pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1292 count
[pack_int_id
]++;
1293 display_progress(progress
, i
+ 1);
1295 stop_progress(&progress
);
1297 if (flags
& MIDX_PROGRESS
)
1298 progress
= start_delayed_progress(_("Finding and deleting unreferenced packfiles"),
1300 for (i
= 0; i
< m
->num_packs
; i
++) {
1302 display_progress(progress
, i
+ 1);
1307 if (prepare_midx_pack(r
, m
, i
))
1310 if (m
->packs
[i
]->pack_keep
|| m
->packs
[i
]->is_cruft
)
1313 pack_name
= xstrdup(m
->packs
[i
]->pack_name
);
1314 close_pack(m
->packs
[i
]);
1316 string_list_insert(&packs_to_drop
, m
->pack_names
[i
]);
1317 unlink_pack_path(pack_name
, 0);
1320 stop_progress(&progress
);
1324 if (packs_to_drop
.nr
)
1325 result
= write_midx_internal(object_dir
, NULL
, &packs_to_drop
, NULL
, NULL
, flags
);
1327 string_list_clear(&packs_to_drop
, 0);
1332 struct repack_info
{
1334 uint32_t referenced_objects
;
1335 uint32_t pack_int_id
;
1338 static int compare_by_mtime(const void *a_
, const void *b_
)
1340 const struct repack_info
*a
, *b
;
1342 a
= (const struct repack_info
*)a_
;
1343 b
= (const struct repack_info
*)b_
;
1345 if (a
->mtime
< b
->mtime
)
1347 if (a
->mtime
> b
->mtime
)
1352 static int want_included_pack(struct repository
*r
,
1353 struct multi_pack_index
*m
,
1354 int pack_kept_objects
,
1355 uint32_t pack_int_id
)
1357 struct packed_git
*p
;
1358 if (prepare_midx_pack(r
, m
, pack_int_id
))
1360 p
= m
->packs
[pack_int_id
];
1361 if (!pack_kept_objects
&& p
->pack_keep
)
1365 if (open_pack_index(p
) || !p
->num_objects
)
1370 static void fill_included_packs_all(struct repository
*r
,
1371 struct multi_pack_index
*m
,
1372 unsigned char *include_pack
)
1375 int pack_kept_objects
= 0;
1377 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1379 for (i
= 0; i
< m
->num_packs
; i
++) {
1380 if (!want_included_pack(r
, m
, pack_kept_objects
, i
))
1383 include_pack
[i
] = 1;
1387 static void fill_included_packs_batch(struct repository
*r
,
1388 struct multi_pack_index
*m
,
1389 unsigned char *include_pack
,
1394 struct repack_info
*pack_info
;
1395 int pack_kept_objects
= 0;
1397 CALLOC_ARRAY(pack_info
, m
->num_packs
);
1399 repo_config_get_bool(r
, "repack.packkeptobjects", &pack_kept_objects
);
1401 for (i
= 0; i
< m
->num_packs
; i
++) {
1402 pack_info
[i
].pack_int_id
= i
;
1404 if (prepare_midx_pack(r
, m
, i
))
1407 pack_info
[i
].mtime
= m
->packs
[i
]->mtime
;
1410 for (i
= 0; i
< m
->num_objects
; i
++) {
1411 uint32_t pack_int_id
= nth_midxed_pack_int_id(m
, i
);
1412 pack_info
[pack_int_id
].referenced_objects
++;
1415 QSORT(pack_info
, m
->num_packs
, compare_by_mtime
);
1418 for (i
= 0; total_size
< batch_size
&& i
< m
->num_packs
; i
++) {
1419 int pack_int_id
= pack_info
[i
].pack_int_id
;
1420 struct packed_git
*p
= m
->packs
[pack_int_id
];
1421 size_t expected_size
;
1423 if (!want_included_pack(r
, m
, pack_kept_objects
, pack_int_id
))
1426 expected_size
= st_mult(p
->pack_size
,
1427 pack_info
[i
].referenced_objects
);
1428 expected_size
/= p
->num_objects
;
1430 if (expected_size
>= batch_size
)
1433 total_size
+= expected_size
;
1434 include_pack
[pack_int_id
] = 1;
1440 int midx_repack(struct repository
*r
, const char *object_dir
, size_t batch_size
, unsigned flags
)
1443 uint32_t i
, packs_to_repack
= 0;
1444 unsigned char *include_pack
;
1445 struct child_process cmd
= CHILD_PROCESS_INIT
;
1447 struct multi_pack_index
*m
= lookup_multi_pack_index(r
, object_dir
);
1450 * When updating the default for these configuration
1451 * variables in builtin/repack.c, these must be adjusted
1454 int delta_base_offset
= 1;
1455 int use_delta_islands
= 0;
1460 CALLOC_ARRAY(include_pack
, m
->num_packs
);
1463 fill_included_packs_batch(r
, m
, include_pack
, batch_size
);
1465 fill_included_packs_all(r
, m
, include_pack
);
1467 for (i
= 0; i
< m
->num_packs
; i
++) {
1468 if (include_pack
[i
])
1471 if (packs_to_repack
<= 1)
1474 repo_config_get_bool(r
, "repack.usedeltabaseoffset", &delta_base_offset
);
1475 repo_config_get_bool(r
, "repack.usedeltaislands", &use_delta_islands
);
1477 strvec_pushl(&cmd
.args
, "pack-objects", "--stdin-packs", "--non-empty",
1480 strvec_pushf(&cmd
.args
, "%s/pack/pack", object_dir
);
1482 if (delta_base_offset
)
1483 strvec_push(&cmd
.args
, "--delta-base-offset");
1484 if (use_delta_islands
)
1485 strvec_push(&cmd
.args
, "--delta-islands");
1487 if (flags
& MIDX_PROGRESS
)
1488 strvec_push(&cmd
.args
, "--progress");
1490 strvec_push(&cmd
.args
, "-q");
1493 cmd
.in
= cmd
.out
= -1;
1495 if (start_command(&cmd
)) {
1496 error(_("could not start pack-objects"));
1501 cmd_in
= xfdopen(cmd
.in
, "w");
1502 for (i
= 0; i
< m
->num_packs
; i
++) {
1503 struct packed_git
*p
= m
->packs
[i
];
1507 if (include_pack
[i
])
1508 fprintf(cmd_in
, "%s\n", pack_basename(p
));
1510 fprintf(cmd_in
, "^%s\n", pack_basename(p
));
1514 if (finish_command(&cmd
)) {
1515 error(_("could not finish pack-objects"));
1520 result
= write_midx_internal(object_dir
, NULL
, NULL
, NULL
, NULL
, flags
);