1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
7 #include "object-store-ll.h"
13 #include "pack-bitmap.h"
14 #include "hash-lookup.h"
15 #include "pack-objects.h"
17 #include "commit-reach.h"
18 #include "prio-queue.h"
21 #include "tree-walk.h"
22 #include "pseudo-merge.h"
23 #include "oid-array.h"
29 struct bitmapped_commit
{
30 struct commit
*commit
;
31 struct ewah_bitmap
*bitmap
;
32 struct ewah_bitmap
*write_as
;
36 unsigned pseudo_merge
: 1;
39 static inline int bitmap_writer_nr_selected_commits(struct bitmap_writer
*writer
)
41 return writer
->selected_nr
- writer
->pseudo_merges_nr
;
44 void bitmap_writer_init(struct bitmap_writer
*writer
, struct repository
*r
,
45 struct packing_data
*pdata
)
47 memset(writer
, 0, sizeof(struct bitmap_writer
));
49 BUG("bitmap writer already initialized");
50 writer
->bitmaps
= kh_init_oid_map();
51 writer
->pseudo_merge_commits
= kh_init_oid_map();
52 writer
->to_pack
= pdata
;
54 string_list_init_dup(&writer
->pseudo_merge_groups
);
56 load_pseudo_merges_from_config(r
, &writer
->pseudo_merge_groups
);
59 static void free_pseudo_merge_commit_idx(struct pseudo_merge_commit_idx
*idx
)
63 free(idx
->pseudo_merge
);
67 static void pseudo_merge_group_release_cb(void *payload
, const char *name UNUSED
)
69 pseudo_merge_group_release(payload
);
73 void bitmap_writer_free(struct bitmap_writer
*writer
)
76 struct pseudo_merge_commit_idx
*idx
;
81 ewah_free(writer
->commits
);
82 ewah_free(writer
->trees
);
83 ewah_free(writer
->blobs
);
84 ewah_free(writer
->tags
);
86 kh_destroy_oid_map(writer
->bitmaps
);
88 kh_foreach_value(writer
->pseudo_merge_commits
, idx
,
89 free_pseudo_merge_commit_idx(idx
));
90 kh_destroy_oid_map(writer
->pseudo_merge_commits
);
91 string_list_clear_func(&writer
->pseudo_merge_groups
,
92 pseudo_merge_group_release_cb
);
94 for (i
= 0; i
< writer
->selected_nr
; i
++) {
95 struct bitmapped_commit
*bc
= &writer
->selected
[i
];
96 if (bc
->write_as
!= bc
->bitmap
)
97 ewah_free(bc
->write_as
);
98 ewah_free(bc
->bitmap
);
100 free(writer
->selected
);
103 void bitmap_writer_show_progress(struct bitmap_writer
*writer
, int show
)
105 writer
->show_progress
= show
;
109 * Build the initial type index for the packfile or multi-pack-index
111 void bitmap_writer_build_type_index(struct bitmap_writer
*writer
,
112 struct pack_idx_entry
**index
)
116 writer
->commits
= ewah_new();
117 writer
->trees
= ewah_new();
118 writer
->blobs
= ewah_new();
119 writer
->tags
= ewah_new();
120 ALLOC_ARRAY(writer
->to_pack
->in_pack_pos
, writer
->to_pack
->nr_objects
);
122 for (i
= 0; i
< writer
->to_pack
->nr_objects
; ++i
) {
123 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
124 enum object_type real_type
;
126 oe_set_in_pack_pos(writer
->to_pack
, entry
, i
);
128 switch (oe_type(entry
)) {
133 real_type
= oe_type(entry
);
137 real_type
= oid_object_info(writer
->to_pack
->repo
,
138 &entry
->idx
.oid
, NULL
);
144 ewah_set(writer
->commits
, i
);
148 ewah_set(writer
->trees
, i
);
152 ewah_set(writer
->blobs
, i
);
156 ewah_set(writer
->tags
, i
);
160 die("Missing type information for %s (%d/%d)",
161 oid_to_hex(&entry
->idx
.oid
), real_type
,
167 int bitmap_writer_has_bitmapped_object_id(struct bitmap_writer
*writer
,
168 const struct object_id
*oid
)
170 return kh_get_oid_map(writer
->bitmaps
, *oid
) != kh_end(writer
->bitmaps
);
174 * Compute the actual bitmaps
177 void bitmap_writer_push_commit(struct bitmap_writer
*writer
,
178 struct commit
*commit
, unsigned pseudo_merge
)
180 if (writer
->selected_nr
>= writer
->selected_alloc
) {
181 writer
->selected_alloc
= (writer
->selected_alloc
+ 32) * 2;
182 REALLOC_ARRAY(writer
->selected
, writer
->selected_alloc
);
187 khiter_t hash_pos
= kh_put_oid_map(writer
->bitmaps
,
192 die(_("duplicate entry when writing bitmap index: %s"),
193 oid_to_hex(&commit
->object
.oid
));
194 kh_value(writer
->bitmaps
, hash_pos
) = NULL
;
197 writer
->selected
[writer
->selected_nr
].commit
= commit
;
198 writer
->selected
[writer
->selected_nr
].bitmap
= NULL
;
199 writer
->selected
[writer
->selected_nr
].write_as
= NULL
;
200 writer
->selected
[writer
->selected_nr
].flags
= 0;
201 writer
->selected
[writer
->selected_nr
].pseudo_merge
= pseudo_merge
;
203 writer
->selected_nr
++;
206 static uint32_t find_object_pos(struct bitmap_writer
*writer
,
207 const struct object_id
*oid
, int *found
)
209 struct object_entry
*entry
= packlist_find(writer
->to_pack
, oid
);
214 warning("Failed to write bitmap index. Packfile doesn't have full closure "
215 "(object %s is missing)", oid_to_hex(oid
));
221 return oe_in_pack_pos(writer
->to_pack
, entry
);
224 static void compute_xor_offsets(struct bitmap_writer
*writer
)
226 static const int MAX_XOR_OFFSET_SEARCH
= 10;
230 while (next
< writer
->selected_nr
) {
231 struct bitmapped_commit
*stored
= &writer
->selected
[next
];
233 struct ewah_bitmap
*best_bitmap
= stored
->bitmap
;
234 struct ewah_bitmap
*test_xor
;
236 if (stored
->pseudo_merge
)
239 for (i
= 1; i
<= MAX_XOR_OFFSET_SEARCH
; ++i
) {
244 if (writer
->selected
[curr
].pseudo_merge
)
247 test_xor
= ewah_pool_new();
248 ewah_xor(writer
->selected
[curr
].bitmap
, stored
->bitmap
, test_xor
);
250 if (test_xor
->buffer_size
< best_bitmap
->buffer_size
) {
251 if (best_bitmap
!= stored
->bitmap
)
252 ewah_pool_free(best_bitmap
);
254 best_bitmap
= test_xor
;
257 ewah_pool_free(test_xor
);
262 stored
->xor_offset
= best_offset
;
263 stored
->write_as
= best_bitmap
;
270 struct commit_list
*reverse_edges
;
271 struct bitmap
*commit_mask
;
272 struct bitmap
*bitmap
;
276 unsigned idx
; /* within selected array */
279 static void clear_bb_commit(struct bb_commit
*commit
)
281 free_commit_list(commit
->reverse_edges
);
282 bitmap_free(commit
->commit_mask
);
283 bitmap_free(commit
->bitmap
);
286 define_commit_slab(bb_data
, struct bb_commit
);
288 struct bitmap_builder
{
290 struct commit
**commits
;
291 size_t commits_nr
, commits_alloc
;
294 static void bitmap_builder_init(struct bitmap_builder
*bb
,
295 struct bitmap_writer
*writer
,
296 struct bitmap_index
*old_bitmap
)
298 struct rev_info revs
;
299 struct commit
*commit
;
300 struct commit_list
*reusable
= NULL
;
301 struct commit_list
*r
;
302 unsigned int i
, num_maximal
= 0;
304 memset(bb
, 0, sizeof(*bb
));
305 init_bb_data(&bb
->data
);
307 reset_revision_walk();
308 repo_init_revisions(writer
->to_pack
->repo
, &revs
, NULL
);
310 revs
.first_parent_only
= 1;
312 for (i
= 0; i
< writer
->selected_nr
; i
++) {
313 struct bitmapped_commit
*bc
= &writer
->selected
[i
];
314 struct bb_commit
*ent
= bb_data_at(&bb
->data
, bc
->commit
);
318 ent
->pseudo_merge
= bc
->pseudo_merge
;
321 ent
->commit_mask
= bitmap_new();
322 bitmap_set(ent
->commit_mask
, i
);
324 add_pending_object(&revs
, &bc
->commit
->object
, "");
327 if (prepare_revision_walk(&revs
))
328 die("revision walk setup failed");
330 while ((commit
= get_revision(&revs
))) {
331 struct commit_list
*p
= commit
->parents
;
332 struct bb_commit
*c_ent
;
334 parse_commit_or_die(commit
);
336 c_ent
= bb_data_at(&bb
->data
, commit
);
339 * If there is no commit_mask, there is no reason to iterate
340 * over this commit; it is not selected (if it were, it would
341 * not have a blank commit mask) and all its children have
342 * existing bitmaps (see the comment starting with "This commit
343 * has an existing bitmap" below), so it does not contribute
344 * anything to the final bitmap file or its descendants.
346 if (!c_ent
->commit_mask
)
349 if (old_bitmap
&& bitmap_for_commit(old_bitmap
, commit
)) {
351 * This commit has an existing bitmap, so we can
352 * get its bits immediately without an object
353 * walk. That is, it is reusable as-is and there is no
354 * need to continue walking beyond it.
356 * Mark it as such and add it to bb->commits separately
357 * to avoid allocating a position in the commit mask.
359 commit_list_insert(commit
, &reusable
);
363 if (c_ent
->maximal
) {
365 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
366 bb
->commits
[bb
->commits_nr
++] = commit
;
370 struct bb_commit
*p_ent
= bb_data_at(&bb
->data
, p
->item
);
371 int c_not_p
, p_not_c
;
373 if (!p_ent
->commit_mask
) {
374 p_ent
->commit_mask
= bitmap_new();
378 c_not_p
= bitmap_is_subset(c_ent
->commit_mask
, p_ent
->commit_mask
);
379 p_not_c
= bitmap_is_subset(p_ent
->commit_mask
, c_ent
->commit_mask
);
385 bitmap_or(p_ent
->commit_mask
, c_ent
->commit_mask
);
391 free_commit_list(p_ent
->reverse_edges
);
392 p_ent
->reverse_edges
= NULL
;
395 if (c_ent
->maximal
) {
396 commit_list_insert(commit
, &p_ent
->reverse_edges
);
398 struct commit_list
*cc
= c_ent
->reverse_edges
;
400 for (; cc
; cc
= cc
->next
) {
401 if (!commit_list_contains(cc
->item
, p_ent
->reverse_edges
))
402 commit_list_insert(cc
->item
, &p_ent
->reverse_edges
);
408 bitmap_free(c_ent
->commit_mask
);
409 c_ent
->commit_mask
= NULL
;
412 for (r
= reusable
; r
; r
= r
->next
) {
413 ALLOC_GROW(bb
->commits
, bb
->commits_nr
+ 1, bb
->commits_alloc
);
414 bb
->commits
[bb
->commits_nr
++] = r
->item
;
417 trace2_data_intmax("pack-bitmap-write", the_repository
,
418 "num_selected_commits", writer
->selected_nr
);
419 trace2_data_intmax("pack-bitmap-write", the_repository
,
420 "num_maximal_commits", num_maximal
);
422 release_revisions(&revs
);
423 free_commit_list(reusable
);
426 static void bitmap_builder_clear(struct bitmap_builder
*bb
)
428 deep_clear_bb_data(&bb
->data
, clear_bb_commit
);
430 bb
->commits_nr
= bb
->commits_alloc
= 0;
433 static int fill_bitmap_tree(struct bitmap_writer
*writer
,
434 struct bitmap
*bitmap
,
439 struct tree_desc desc
;
440 struct name_entry entry
;
443 * If our bit is already set, then there is nothing to do. Both this
444 * tree and all of its children will be set.
446 pos
= find_object_pos(writer
, &tree
->object
.oid
, &found
);
449 if (bitmap_get(bitmap
, pos
))
451 bitmap_set(bitmap
, pos
);
453 if (parse_tree(tree
) < 0)
454 die("unable to load tree object %s",
455 oid_to_hex(&tree
->object
.oid
));
456 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
458 while (tree_entry(&desc
, &entry
)) {
459 switch (object_type(entry
.mode
)) {
461 if (fill_bitmap_tree(writer
, bitmap
,
462 lookup_tree(the_repository
, &entry
.oid
)) < 0)
466 pos
= find_object_pos(writer
, &entry
.oid
, &found
);
469 bitmap_set(bitmap
, pos
);
472 /* Gitlink, etc; not reachable */
477 free_tree_buffer(tree
);
481 static int reused_bitmaps_nr
;
482 static int reused_pseudo_merge_bitmaps_nr
;
484 static int fill_bitmap_commit(struct bitmap_writer
*writer
,
485 struct bb_commit
*ent
,
486 struct commit
*commit
,
487 struct prio_queue
*queue
,
488 struct prio_queue
*tree_queue
,
489 struct bitmap_index
*old_bitmap
,
490 const uint32_t *mapping
)
495 ent
->bitmap
= bitmap_new();
497 prio_queue_put(queue
, commit
);
500 struct commit_list
*p
;
501 struct commit
*c
= prio_queue_get(queue
);
503 if (old_bitmap
&& mapping
) {
504 struct ewah_bitmap
*old
;
505 struct bitmap
*remapped
= bitmap_new();
507 if (commit
->object
.flags
& BITMAP_PSEUDO_MERGE
)
508 old
= pseudo_merge_bitmap_for_commit(old_bitmap
, c
);
510 old
= bitmap_for_commit(old_bitmap
, c
);
512 * If this commit has an old bitmap, then translate that
513 * bitmap and add its bits to this one. No need to walk
514 * parents or the tree for this commit.
516 if (old
&& !rebuild_bitmap(mapping
, old
, remapped
)) {
517 bitmap_or(ent
->bitmap
, remapped
);
518 bitmap_free(remapped
);
519 if (commit
->object
.flags
& BITMAP_PSEUDO_MERGE
)
520 reused_pseudo_merge_bitmaps_nr
++;
525 bitmap_free(remapped
);
529 * Mark ourselves and queue our tree. The commit
530 * walk ensures we cover all parents.
532 if (!(c
->object
.flags
& BITMAP_PSEUDO_MERGE
)) {
533 pos
= find_object_pos(writer
, &c
->object
.oid
, &found
);
536 bitmap_set(ent
->bitmap
, pos
);
537 prio_queue_put(tree_queue
,
538 repo_get_commit_tree(the_repository
, c
));
541 for (p
= c
->parents
; p
; p
= p
->next
) {
542 pos
= find_object_pos(writer
, &p
->item
->object
.oid
,
546 if (!bitmap_get(ent
->bitmap
, pos
)) {
547 bitmap_set(ent
->bitmap
, pos
);
548 prio_queue_put(queue
, p
->item
);
553 while (tree_queue
->nr
) {
554 if (fill_bitmap_tree(writer
, ent
->bitmap
,
555 prio_queue_get(tree_queue
)) < 0)
561 static void store_selected(struct bitmap_writer
*writer
,
562 struct bb_commit
*ent
, struct commit
*commit
)
564 struct bitmapped_commit
*stored
= &writer
->selected
[ent
->idx
];
567 stored
->bitmap
= bitmap_to_ewah(ent
->bitmap
);
569 if (ent
->pseudo_merge
)
572 hash_pos
= kh_get_oid_map(writer
->bitmaps
, commit
->object
.oid
);
573 if (hash_pos
== kh_end(writer
->bitmaps
))
574 die(_("attempted to store non-selected commit: '%s'"),
575 oid_to_hex(&commit
->object
.oid
));
577 kh_value(writer
->bitmaps
, hash_pos
) = stored
;
580 int bitmap_writer_build(struct bitmap_writer
*writer
)
582 struct bitmap_builder bb
;
584 int nr_stored
= 0; /* for progress */
585 struct prio_queue queue
= { compare_commits_by_gen_then_commit_date
};
586 struct prio_queue tree_queue
= { NULL
};
587 struct bitmap_index
*old_bitmap
;
589 int closed
= 1; /* until proven otherwise */
591 if (writer
->show_progress
)
592 writer
->progress
= start_progress("Building bitmaps",
593 writer
->selected_nr
);
594 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
597 old_bitmap
= prepare_bitmap_git(writer
->to_pack
->repo
);
599 mapping
= create_bitmap_mapping(old_bitmap
, writer
->to_pack
);
603 bitmap_builder_init(&bb
, writer
, old_bitmap
);
604 for (i
= bb
.commits_nr
; i
> 0; i
--) {
605 struct commit
*commit
= bb
.commits
[i
-1];
606 struct bb_commit
*ent
= bb_data_at(&bb
.data
, commit
);
607 struct commit
*child
;
610 if (fill_bitmap_commit(writer
, ent
, commit
, &queue
, &tree_queue
,
611 old_bitmap
, mapping
) < 0) {
617 store_selected(writer
, ent
, commit
);
619 display_progress(writer
->progress
, nr_stored
);
622 while ((child
= pop_commit(&ent
->reverse_edges
))) {
623 struct bb_commit
*child_ent
=
624 bb_data_at(&bb
.data
, child
);
626 if (child_ent
->bitmap
)
627 bitmap_or(child_ent
->bitmap
, ent
->bitmap
);
629 child_ent
->bitmap
= bitmap_dup(ent
->bitmap
);
631 child_ent
->bitmap
= ent
->bitmap
;
636 bitmap_free(ent
->bitmap
);
639 clear_prio_queue(&queue
);
640 clear_prio_queue(&tree_queue
);
641 bitmap_builder_clear(&bb
);
642 free_bitmap_index(old_bitmap
);
645 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
647 trace2_data_intmax("pack-bitmap-write", the_repository
,
648 "building_bitmaps_reused", reused_bitmaps_nr
);
649 trace2_data_intmax("pack-bitmap-write", the_repository
,
650 "building_bitmaps_pseudo_merge_reused",
651 reused_pseudo_merge_bitmaps_nr
);
653 stop_progress(&writer
->progress
);
656 compute_xor_offsets(writer
);
657 return closed
? 0 : -1;
661 * Select the commits that will be bitmapped
663 static inline unsigned int next_commit_index(unsigned int idx
)
665 static const unsigned int MIN_COMMITS
= 100;
666 static const unsigned int MAX_COMMITS
= 5000;
668 static const unsigned int MUST_REGION
= 100;
669 static const unsigned int MIN_REGION
= 20000;
671 unsigned int offset
, next
;
673 if (idx
<= MUST_REGION
)
676 if (idx
<= MIN_REGION
) {
677 offset
= idx
- MUST_REGION
;
678 return (offset
< MIN_COMMITS
) ? offset
: MIN_COMMITS
;
681 offset
= idx
- MIN_REGION
;
682 next
= (offset
< MAX_COMMITS
) ? offset
: MAX_COMMITS
;
684 return (next
> MIN_COMMITS
) ? next
: MIN_COMMITS
;
687 static int date_compare(const void *_a
, const void *_b
)
689 struct commit
*a
= *(struct commit
**)_a
;
690 struct commit
*b
= *(struct commit
**)_b
;
691 return (long)b
->date
- (long)a
->date
;
694 void bitmap_writer_select_commits(struct bitmap_writer
*writer
,
695 struct commit
**indexed_commits
,
696 unsigned int indexed_commits_nr
)
698 unsigned int i
= 0, j
, next
;
700 QSORT(indexed_commits
, indexed_commits_nr
, date_compare
);
702 if (indexed_commits_nr
< 100) {
703 for (i
= 0; i
< indexed_commits_nr
; ++i
)
704 bitmap_writer_push_commit(writer
, indexed_commits
[i
], 0);
706 select_pseudo_merges(writer
);
711 if (writer
->show_progress
)
712 writer
->progress
= start_progress("Selecting bitmap commits", 0);
715 struct commit
*chosen
= NULL
;
717 next
= next_commit_index(i
);
719 if (i
+ next
>= indexed_commits_nr
)
723 chosen
= indexed_commits
[i
];
725 chosen
= indexed_commits
[i
+ next
];
727 for (j
= 0; j
<= next
; ++j
) {
728 struct commit
*cm
= indexed_commits
[i
+ j
];
730 if ((cm
->object
.flags
& NEEDS_BITMAP
) != 0) {
735 if (cm
->parents
&& cm
->parents
->next
)
740 bitmap_writer_push_commit(writer
, chosen
, 0);
743 display_progress(writer
->progress
, i
);
746 stop_progress(&writer
->progress
);
748 select_pseudo_merges(writer
);
752 static int hashwrite_ewah_helper(void *f
, const void *buf
, size_t len
)
754 /* hashwrite will die on error */
755 hashwrite(f
, buf
, len
);
760 * Write the bitmap index to disk
762 static inline void dump_bitmap(struct hashfile
*f
, struct ewah_bitmap
*bitmap
)
764 if (ewah_serialize_to(bitmap
, hashwrite_ewah_helper
, f
) < 0)
765 die("Failed to write bitmap index");
768 static const struct object_id
*oid_access(size_t pos
, const void *table
)
770 const struct pack_idx_entry
* const *index
= table
;
771 return &index
[pos
]->oid
;
774 static void write_selected_commits_v1(struct bitmap_writer
*writer
,
775 struct hashfile
*f
, off_t
*offsets
)
779 for (i
= 0; i
< bitmap_writer_nr_selected_commits(writer
); ++i
) {
780 struct bitmapped_commit
*stored
= &writer
->selected
[i
];
781 if (stored
->pseudo_merge
)
782 BUG("unexpected pseudo-merge among selected: %s",
783 oid_to_hex(&stored
->commit
->object
.oid
));
786 offsets
[i
] = hashfile_total(f
);
788 hashwrite_be32(f
, stored
->commit_pos
);
789 hashwrite_u8(f
, stored
->xor_offset
);
790 hashwrite_u8(f
, stored
->flags
);
792 dump_bitmap(f
, stored
->write_as
);
796 static void write_pseudo_merges(struct bitmap_writer
*writer
,
799 struct oid_array commits
= OID_ARRAY_INIT
;
800 struct bitmap
**commits_bitmap
= NULL
;
801 off_t
*pseudo_merge_ofs
= NULL
;
802 off_t start
, table_start
, next_ext
;
804 uint32_t base
= bitmap_writer_nr_selected_commits(writer
);
807 CALLOC_ARRAY(commits_bitmap
, writer
->pseudo_merges_nr
);
808 CALLOC_ARRAY(pseudo_merge_ofs
, writer
->pseudo_merges_nr
);
810 for (i
= 0; i
< writer
->pseudo_merges_nr
; i
++) {
811 struct bitmapped_commit
*merge
= &writer
->selected
[base
+ i
];
812 struct commit_list
*p
;
814 if (!merge
->pseudo_merge
)
815 BUG("found non-pseudo merge commit at %"PRIuMAX
, (uintmax_t)i
);
817 commits_bitmap
[i
] = bitmap_new();
819 for (p
= merge
->commit
->parents
; p
; p
= p
->next
)
820 bitmap_set(commits_bitmap
[i
],
821 find_object_pos(writer
, &p
->item
->object
.oid
,
825 start
= hashfile_total(f
);
827 for (i
= 0; i
< writer
->pseudo_merges_nr
; i
++) {
828 struct ewah_bitmap
*commits_ewah
= bitmap_to_ewah(commits_bitmap
[i
]);
830 pseudo_merge_ofs
[i
] = hashfile_total(f
);
832 dump_bitmap(f
, commits_ewah
);
833 dump_bitmap(f
, writer
->selected
[base
+i
].write_as
);
835 ewah_free(commits_ewah
);
838 next_ext
= st_add(hashfile_total(f
),
839 st_mult(kh_size(writer
->pseudo_merge_commits
),
842 table_start
= hashfile_total(f
);
844 commits
.alloc
= kh_size(writer
->pseudo_merge_commits
);
845 CALLOC_ARRAY(commits
.oid
, commits
.alloc
);
847 for (i
= kh_begin(writer
->pseudo_merge_commits
); i
!= kh_end(writer
->pseudo_merge_commits
); i
++) {
848 if (!kh_exist(writer
->pseudo_merge_commits
, i
))
850 oid_array_append(&commits
, &kh_key(writer
->pseudo_merge_commits
, i
));
853 oid_array_sort(&commits
);
855 /* write lookup table (non-extended) */
856 for (i
= 0; i
< commits
.nr
; i
++) {
858 struct pseudo_merge_commit_idx
*c
;
860 hash_pos
= kh_get_oid_map(writer
->pseudo_merge_commits
,
862 if (hash_pos
== kh_end(writer
->pseudo_merge_commits
))
863 BUG("could not find pseudo-merge commit %s",
864 oid_to_hex(&commits
.oid
[i
]));
866 c
= kh_value(writer
->pseudo_merge_commits
, hash_pos
);
868 hashwrite_be32(f
, find_object_pos(writer
, &commits
.oid
[i
],
871 hashwrite_be64(f
, pseudo_merge_ofs
[c
->pseudo_merge
[0]]);
872 else if (c
->nr
> 1) {
873 if (next_ext
& ((uint64_t)1<<63))
874 die(_("too many pseudo-merges"));
875 hashwrite_be64(f
, next_ext
| ((uint64_t)1<<63));
876 next_ext
= st_add3(next_ext
,
878 st_mult(c
->nr
, sizeof(uint64_t)));
880 BUG("expected commit '%s' to have at least one "
881 "pseudo-merge", oid_to_hex(&commits
.oid
[i
]));
884 /* write lookup table (extended) */
885 for (i
= 0; i
< commits
.nr
; i
++) {
887 struct pseudo_merge_commit_idx
*c
;
889 hash_pos
= kh_get_oid_map(writer
->pseudo_merge_commits
,
891 if (hash_pos
== kh_end(writer
->pseudo_merge_commits
))
892 BUG("could not find pseudo-merge commit %s",
893 oid_to_hex(&commits
.oid
[i
]));
895 c
= kh_value(writer
->pseudo_merge_commits
, hash_pos
);
899 hashwrite_be32(f
, c
->nr
);
900 for (j
= 0; j
< c
->nr
; j
++)
901 hashwrite_be64(f
, pseudo_merge_ofs
[c
->pseudo_merge
[j
]]);
904 /* write positions for all pseudo merges */
905 for (i
= 0; i
< writer
->pseudo_merges_nr
; i
++)
906 hashwrite_be64(f
, pseudo_merge_ofs
[i
]);
908 hashwrite_be32(f
, writer
->pseudo_merges_nr
);
909 hashwrite_be32(f
, kh_size(writer
->pseudo_merge_commits
));
910 hashwrite_be64(f
, table_start
- start
);
911 hashwrite_be64(f
, hashfile_total(f
) - start
+ sizeof(uint64_t));
913 for (i
= 0; i
< writer
->pseudo_merges_nr
; i
++)
914 bitmap_free(commits_bitmap
[i
]);
916 oid_array_clear(&commits
);
917 free(pseudo_merge_ofs
);
918 free(commits_bitmap
);
921 static int table_cmp(const void *_va
, const void *_vb
, void *_data
)
923 struct bitmap_writer
*writer
= _data
;
924 struct bitmapped_commit
*a
= &writer
->selected
[*(uint32_t *)_va
];
925 struct bitmapped_commit
*b
= &writer
->selected
[*(uint32_t *)_vb
];
927 if (a
->commit_pos
< b
->commit_pos
)
929 else if (a
->commit_pos
> b
->commit_pos
)
935 static void write_lookup_table(struct bitmap_writer
*writer
, struct hashfile
*f
,
939 uint32_t *table
, *table_inv
;
941 ALLOC_ARRAY(table
, bitmap_writer_nr_selected_commits(writer
));
942 ALLOC_ARRAY(table_inv
, bitmap_writer_nr_selected_commits(writer
));
944 for (i
= 0; i
< bitmap_writer_nr_selected_commits(writer
); i
++)
948 * At the end of this sort table[j] = i means that the i'th
949 * bitmap corresponds to j'th bitmapped commit (among the selected
950 * commits) in lex order of OIDs.
952 QSORT_S(table
, bitmap_writer_nr_selected_commits(writer
), table_cmp
, writer
);
954 /* table_inv helps us discover that relationship (i'th bitmap
955 * to j'th commit by j = table_inv[i])
957 for (i
= 0; i
< bitmap_writer_nr_selected_commits(writer
); i
++)
958 table_inv
[table
[i
]] = i
;
960 trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository
);
961 for (i
= 0; i
< bitmap_writer_nr_selected_commits(writer
); i
++) {
962 struct bitmapped_commit
*selected
= &writer
->selected
[table
[i
]];
963 uint32_t xor_offset
= selected
->xor_offset
;
968 * xor_index stores the index (in the bitmap entries)
969 * of the corresponding xor bitmap. But we need to convert
970 * this index into lookup table's index. So, table_inv[xor_index]
971 * gives us the index position w.r.t. the lookup table.
973 * If "k = table[i] - xor_offset" then the xor base is the k'th
974 * bitmap. `table_inv[k]` gives us the position of that bitmap
975 * in the lookup table.
977 uint32_t xor_index
= table
[i
] - xor_offset
;
978 xor_row
= table_inv
[xor_index
];
980 xor_row
= 0xffffffff;
983 hashwrite_be32(f
, writer
->selected
[table
[i
]].commit_pos
);
984 hashwrite_be64(f
, (uint64_t)offsets
[table
[i
]]);
985 hashwrite_be32(f
, xor_row
);
987 trace2_region_leave("pack-bitmap-write", "writing_lookup_table", the_repository
);
993 static void write_hash_cache(struct hashfile
*f
,
994 struct pack_idx_entry
**index
,
999 for (i
= 0; i
< index_nr
; ++i
) {
1000 struct object_entry
*entry
= (struct object_entry
*)index
[i
];
1001 hashwrite_be32(f
, entry
->hash
);
1005 void bitmap_writer_set_checksum(struct bitmap_writer
*writer
,
1006 const unsigned char *sha1
)
1008 hashcpy(writer
->pack_checksum
, sha1
, the_repository
->hash_algo
);
1011 void bitmap_writer_finish(struct bitmap_writer
*writer
,
1012 struct pack_idx_entry
**index
,
1013 const char *filename
,
1016 static uint16_t default_version
= 1;
1017 static uint16_t flags
= BITMAP_OPT_FULL_DAG
;
1018 struct strbuf tmp_file
= STRBUF_INIT
;
1020 off_t
*offsets
= NULL
;
1023 struct bitmap_disk_header header
;
1025 int fd
= odb_mkstemp(&tmp_file
, "pack/tmp_bitmap_XXXXXX");
1027 if (writer
->pseudo_merges_nr
)
1028 options
|= BITMAP_OPT_PSEUDO_MERGES
;
1030 f
= hashfd(fd
, tmp_file
.buf
);
1032 memcpy(header
.magic
, BITMAP_IDX_SIGNATURE
, sizeof(BITMAP_IDX_SIGNATURE
));
1033 header
.version
= htons(default_version
);
1034 header
.options
= htons(flags
| options
);
1035 header
.entry_count
= htonl(bitmap_writer_nr_selected_commits(writer
));
1036 hashcpy(header
.checksum
, writer
->pack_checksum
, the_repository
->hash_algo
);
1038 hashwrite(f
, &header
, sizeof(header
) - GIT_MAX_RAWSZ
+ the_hash_algo
->rawsz
);
1039 dump_bitmap(f
, writer
->commits
);
1040 dump_bitmap(f
, writer
->trees
);
1041 dump_bitmap(f
, writer
->blobs
);
1042 dump_bitmap(f
, writer
->tags
);
1044 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
1045 CALLOC_ARRAY(offsets
, writer
->to_pack
->nr_objects
);
1047 for (i
= 0; i
< bitmap_writer_nr_selected_commits(writer
); i
++) {
1048 struct bitmapped_commit
*stored
= &writer
->selected
[i
];
1049 int commit_pos
= oid_pos(&stored
->commit
->object
.oid
, index
,
1050 writer
->to_pack
->nr_objects
,
1054 BUG(_("trying to write commit not in index"));
1055 stored
->commit_pos
= commit_pos
;
1058 write_selected_commits_v1(writer
, f
, offsets
);
1060 if (options
& BITMAP_OPT_PSEUDO_MERGES
)
1061 write_pseudo_merges(writer
, f
);
1063 if (options
& BITMAP_OPT_LOOKUP_TABLE
)
1064 write_lookup_table(writer
, f
, offsets
);
1066 if (options
& BITMAP_OPT_HASH_CACHE
)
1067 write_hash_cache(f
, index
, writer
->to_pack
->nr_objects
);
1069 finalize_hashfile(f
, NULL
, FSYNC_COMPONENT_PACK_METADATA
,
1070 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
| CSUM_CLOSE
);
1072 if (adjust_shared_perm(tmp_file
.buf
))
1073 die_errno("unable to make temporary bitmap file readable");
1075 if (rename(tmp_file
.buf
, filename
))
1076 die_errno("unable to rename temporary bitmap file to '%s'", filename
);
1078 strbuf_release(&tmp_file
);