Sync with 'maint'
[git/gitster.git] / pack-bitmap-write.c
blob49758e2525f01afc4c31a97a8128e5d92f7f7939
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "object-store-ll.h"
8 #include "commit.h"
9 #include "diff.h"
10 #include "revision.h"
11 #include "progress.h"
12 #include "pack.h"
13 #include "pack-bitmap.h"
14 #include "hash-lookup.h"
15 #include "pack-objects.h"
16 #include "path.h"
17 #include "commit-reach.h"
18 #include "prio-queue.h"
19 #include "trace2.h"
20 #include "tree.h"
21 #include "tree-walk.h"
22 #include "pseudo-merge.h"
23 #include "oid-array.h"
24 #include "config.h"
25 #include "alloc.h"
26 #include "refs.h"
27 #include "strmap.h"
29 struct bitmapped_commit {
30 struct commit *commit;
31 struct ewah_bitmap *bitmap;
32 struct ewah_bitmap *write_as;
33 int flags;
34 int xor_offset;
35 uint32_t commit_pos;
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));
48 if (writer->bitmaps)
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)
61 if (!idx)
62 return;
63 free(idx->pseudo_merge);
64 free(idx);
67 static void pseudo_merge_group_release_cb(void *payload, const char *name UNUSED)
69 pseudo_merge_group_release(payload);
70 free(payload);
73 void bitmap_writer_free(struct bitmap_writer *writer)
75 uint32_t i;
76 struct pseudo_merge_commit_idx *idx;
78 if (!writer)
79 return;
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)
114 uint32_t i;
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)) {
129 case OBJ_COMMIT:
130 case OBJ_TREE:
131 case OBJ_BLOB:
132 case OBJ_TAG:
133 real_type = oe_type(entry);
134 break;
136 default:
137 real_type = oid_object_info(writer->to_pack->repo,
138 &entry->idx.oid, NULL);
139 break;
142 switch (real_type) {
143 case OBJ_COMMIT:
144 ewah_set(writer->commits, i);
145 break;
147 case OBJ_TREE:
148 ewah_set(writer->trees, i);
149 break;
151 case OBJ_BLOB:
152 ewah_set(writer->blobs, i);
153 break;
155 case OBJ_TAG:
156 ewah_set(writer->tags, i);
157 break;
159 default:
160 die("Missing type information for %s (%d/%d)",
161 oid_to_hex(&entry->idx.oid), real_type,
162 oe_type(entry));
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);
185 if (!pseudo_merge) {
186 int hash_ret;
187 khiter_t hash_pos = kh_put_oid_map(writer->bitmaps,
188 commit->object.oid,
189 &hash_ret);
191 if (!hash_ret)
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);
211 if (!entry) {
212 if (found)
213 *found = 0;
214 warning("Failed to write bitmap index. Packfile doesn't have full closure "
215 "(object %s is missing)", oid_to_hex(oid));
216 return 0;
219 if (found)
220 *found = 1;
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;
228 int i, next = 0;
230 while (next < writer->selected_nr) {
231 struct bitmapped_commit *stored = &writer->selected[next];
232 int best_offset = 0;
233 struct ewah_bitmap *best_bitmap = stored->bitmap;
234 struct ewah_bitmap *test_xor;
236 if (stored->pseudo_merge)
237 goto next;
239 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
240 int curr = next - i;
242 if (curr < 0)
243 break;
244 if (writer->selected[curr].pseudo_merge)
245 continue;
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;
255 best_offset = i;
256 } else {
257 ewah_pool_free(test_xor);
261 next:
262 stored->xor_offset = best_offset;
263 stored->write_as = best_bitmap;
265 next++;
269 struct bb_commit {
270 struct commit_list *reverse_edges;
271 struct bitmap *commit_mask;
272 struct bitmap *bitmap;
273 unsigned selected:1,
274 maximal:1,
275 pseudo_merge:1;
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 {
289 struct bb_data data;
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);
309 revs.topo_order = 1;
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);
316 ent->selected = 1;
317 ent->maximal = 1;
318 ent->pseudo_merge = bc->pseudo_merge;
319 ent->idx = i;
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)
347 continue;
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);
360 goto next;
363 if (c_ent->maximal) {
364 num_maximal++;
365 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
366 bb->commits[bb->commits_nr++] = commit;
369 if (p) {
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();
375 c_not_p = 1;
376 p_not_c = 0;
377 } else {
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);
382 if (!c_not_p)
383 continue;
385 bitmap_or(p_ent->commit_mask, c_ent->commit_mask);
387 if (p_not_c)
388 p_ent->maximal = 1;
389 else {
390 p_ent->maximal = 0;
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);
397 } else {
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);
407 next:
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);
429 free(bb->commits);
430 bb->commits_nr = bb->commits_alloc = 0;
433 static int fill_bitmap_tree(struct bitmap_writer *writer,
434 struct bitmap *bitmap,
435 struct tree *tree)
437 int found;
438 uint32_t pos;
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);
447 if (!found)
448 return -1;
449 if (bitmap_get(bitmap, pos))
450 return 0;
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)) {
460 case OBJ_TREE:
461 if (fill_bitmap_tree(writer, bitmap,
462 lookup_tree(the_repository, &entry.oid)) < 0)
463 return -1;
464 break;
465 case OBJ_BLOB:
466 pos = find_object_pos(writer, &entry.oid, &found);
467 if (!found)
468 return -1;
469 bitmap_set(bitmap, pos);
470 break;
471 default:
472 /* Gitlink, etc; not reachable */
473 break;
477 free_tree_buffer(tree);
478 return 0;
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)
492 int found;
493 uint32_t pos;
494 if (!ent->bitmap)
495 ent->bitmap = bitmap_new();
497 prio_queue_put(queue, commit);
499 while (queue->nr) {
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);
509 else
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++;
521 else
522 reused_bitmaps_nr++;
523 continue;
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);
534 if (!found)
535 return -1;
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,
543 &found);
544 if (!found)
545 return -1;
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)
556 return -1;
558 return 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];
565 khiter_t hash_pos;
567 stored->bitmap = bitmap_to_ewah(ent->bitmap);
569 if (ent->pseudo_merge)
570 return;
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;
583 size_t i;
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;
588 uint32_t *mapping;
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",
595 the_repository);
597 old_bitmap = prepare_bitmap_git(writer->to_pack->repo);
598 if (old_bitmap)
599 mapping = create_bitmap_mapping(old_bitmap, writer->to_pack);
600 else
601 mapping = NULL;
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;
608 int reused = 0;
610 if (fill_bitmap_commit(writer, ent, commit, &queue, &tree_queue,
611 old_bitmap, mapping) < 0) {
612 closed = 0;
613 break;
616 if (ent->selected) {
617 store_selected(writer, ent, commit);
618 nr_stored++;
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);
628 else if (reused)
629 child_ent->bitmap = bitmap_dup(ent->bitmap);
630 else {
631 child_ent->bitmap = ent->bitmap;
632 reused = 1;
635 if (!reused)
636 bitmap_free(ent->bitmap);
637 ent->bitmap = NULL;
639 clear_prio_queue(&queue);
640 clear_prio_queue(&tree_queue);
641 bitmap_builder_clear(&bb);
642 free_bitmap_index(old_bitmap);
643 free(mapping);
645 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
646 the_repository);
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);
655 if (closed)
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)
674 return 0;
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);
708 return;
711 if (writer->show_progress)
712 writer->progress = start_progress("Selecting bitmap commits", 0);
714 for (;;) {
715 struct commit *chosen = NULL;
717 next = next_commit_index(i);
719 if (i + next >= indexed_commits_nr)
720 break;
722 if (next == 0) {
723 chosen = indexed_commits[i];
724 } else {
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) {
731 chosen = cm;
732 break;
735 if (cm->parents && cm->parents->next)
736 chosen = cm;
740 bitmap_writer_push_commit(writer, chosen, 0);
742 i += next + 1;
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);
756 return 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)
777 int i;
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));
785 if (offsets)
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,
797 struct hashfile *f)
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);
805 size_t i, j = 0;
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,
822 NULL));
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),
840 sizeof(uint64_t)));
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))
849 continue;
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++) {
857 int hash_pos;
858 struct pseudo_merge_commit_idx *c;
860 hash_pos = kh_get_oid_map(writer->pseudo_merge_commits,
861 commits.oid[i]);
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],
869 NULL));
870 if (c->nr == 1)
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,
877 sizeof(uint32_t),
878 st_mult(c->nr, sizeof(uint64_t)));
879 } else
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++) {
886 int hash_pos;
887 struct pseudo_merge_commit_idx *c;
889 hash_pos = kh_get_oid_map(writer->pseudo_merge_commits,
890 commits.oid[i]);
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);
896 if (c->nr == 1)
897 continue;
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)
928 return -1;
929 else if (a->commit_pos > b->commit_pos)
930 return 1;
932 return 0;
935 static void write_lookup_table(struct bitmap_writer *writer, struct hashfile *f,
936 off_t *offsets)
938 uint32_t i;
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++)
945 table[i] = 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;
964 uint32_t xor_row;
966 if (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];
979 } else {
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);
989 free(table);
990 free(table_inv);
993 static void write_hash_cache(struct hashfile *f,
994 struct pack_idx_entry **index,
995 uint32_t index_nr)
997 uint32_t i;
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,
1014 uint16_t options)
1016 static uint16_t default_version = 1;
1017 static uint16_t flags = BITMAP_OPT_FULL_DAG;
1018 struct strbuf tmp_file = STRBUF_INIT;
1019 struct hashfile *f;
1020 off_t *offsets = NULL;
1021 uint32_t i;
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,
1051 oid_access);
1053 if (commit_pos < 0)
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);
1079 free(offsets);