]> git.ipfire.org Git - thirdparty/git.git/blob - pack-bitmap-write.c
Merge branch 'as/doc-markup-fix'
[thirdparty/git.git] / pack-bitmap-write.c
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "hex.h"
4 #include "object-store.h"
5 #include "commit.h"
6 #include "tag.h"
7 #include "diff.h"
8 #include "revision.h"
9 #include "list-objects.h"
10 #include "progress.h"
11 #include "pack-revindex.h"
12 #include "pack.h"
13 #include "pack-bitmap.h"
14 #include "hash-lookup.h"
15 #include "pack-objects.h"
16 #include "commit-reach.h"
17 #include "prio-queue.h"
18
19 struct bitmapped_commit {
20 struct commit *commit;
21 struct ewah_bitmap *bitmap;
22 struct ewah_bitmap *write_as;
23 int flags;
24 int xor_offset;
25 uint32_t commit_pos;
26 };
27
28 struct bitmap_writer {
29 struct ewah_bitmap *commits;
30 struct ewah_bitmap *trees;
31 struct ewah_bitmap *blobs;
32 struct ewah_bitmap *tags;
33
34 kh_oid_map_t *bitmaps;
35 struct packing_data *to_pack;
36
37 struct bitmapped_commit *selected;
38 unsigned int selected_nr, selected_alloc;
39
40 struct progress *progress;
41 int show_progress;
42 unsigned char pack_checksum[GIT_MAX_RAWSZ];
43 };
44
45 static struct bitmap_writer writer;
46
47 void bitmap_writer_show_progress(int show)
48 {
49 writer.show_progress = show;
50 }
51
52 /**
53 * Build the initial type index for the packfile or multi-pack-index
54 */
55 void bitmap_writer_build_type_index(struct packing_data *to_pack,
56 struct pack_idx_entry **index,
57 uint32_t index_nr)
58 {
59 uint32_t i;
60
61 writer.commits = ewah_new();
62 writer.trees = ewah_new();
63 writer.blobs = ewah_new();
64 writer.tags = ewah_new();
65 ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
66
67 for (i = 0; i < index_nr; ++i) {
68 struct object_entry *entry = (struct object_entry *)index[i];
69 enum object_type real_type;
70
71 oe_set_in_pack_pos(to_pack, entry, i);
72
73 switch (oe_type(entry)) {
74 case OBJ_COMMIT:
75 case OBJ_TREE:
76 case OBJ_BLOB:
77 case OBJ_TAG:
78 real_type = oe_type(entry);
79 break;
80
81 default:
82 real_type = oid_object_info(to_pack->repo,
83 &entry->idx.oid, NULL);
84 break;
85 }
86
87 switch (real_type) {
88 case OBJ_COMMIT:
89 ewah_set(writer.commits, i);
90 break;
91
92 case OBJ_TREE:
93 ewah_set(writer.trees, i);
94 break;
95
96 case OBJ_BLOB:
97 ewah_set(writer.blobs, i);
98 break;
99
100 case OBJ_TAG:
101 ewah_set(writer.tags, i);
102 break;
103
104 default:
105 die("Missing type information for %s (%d/%d)",
106 oid_to_hex(&entry->idx.oid), real_type,
107 oe_type(entry));
108 }
109 }
110 }
111
112 /**
113 * Compute the actual bitmaps
114 */
115
116 static inline void push_bitmapped_commit(struct commit *commit)
117 {
118 if (writer.selected_nr >= writer.selected_alloc) {
119 writer.selected_alloc = (writer.selected_alloc + 32) * 2;
120 REALLOC_ARRAY(writer.selected, writer.selected_alloc);
121 }
122
123 writer.selected[writer.selected_nr].commit = commit;
124 writer.selected[writer.selected_nr].bitmap = NULL;
125 writer.selected[writer.selected_nr].flags = 0;
126
127 writer.selected_nr++;
128 }
129
130 static uint32_t find_object_pos(const struct object_id *oid, int *found)
131 {
132 struct object_entry *entry = packlist_find(writer.to_pack, oid);
133
134 if (!entry) {
135 if (found)
136 *found = 0;
137 warning("Failed to write bitmap index. Packfile doesn't have full closure "
138 "(object %s is missing)", oid_to_hex(oid));
139 return 0;
140 }
141
142 if (found)
143 *found = 1;
144 return oe_in_pack_pos(writer.to_pack, entry);
145 }
146
147 static void compute_xor_offsets(void)
148 {
149 static const int MAX_XOR_OFFSET_SEARCH = 10;
150
151 int i, next = 0;
152
153 while (next < writer.selected_nr) {
154 struct bitmapped_commit *stored = &writer.selected[next];
155
156 int best_offset = 0;
157 struct ewah_bitmap *best_bitmap = stored->bitmap;
158 struct ewah_bitmap *test_xor;
159
160 for (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {
161 int curr = next - i;
162
163 if (curr < 0)
164 break;
165
166 test_xor = ewah_pool_new();
167 ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
168
169 if (test_xor->buffer_size < best_bitmap->buffer_size) {
170 if (best_bitmap != stored->bitmap)
171 ewah_pool_free(best_bitmap);
172
173 best_bitmap = test_xor;
174 best_offset = i;
175 } else {
176 ewah_pool_free(test_xor);
177 }
178 }
179
180 stored->xor_offset = best_offset;
181 stored->write_as = best_bitmap;
182
183 next++;
184 }
185 }
186
187 struct bb_commit {
188 struct commit_list *reverse_edges;
189 struct bitmap *commit_mask;
190 struct bitmap *bitmap;
191 unsigned selected:1,
192 maximal:1;
193 unsigned idx; /* within selected array */
194 };
195
196 define_commit_slab(bb_data, struct bb_commit);
197
198 struct bitmap_builder {
199 struct bb_data data;
200 struct commit **commits;
201 size_t commits_nr, commits_alloc;
202 };
203
204 static void bitmap_builder_init(struct bitmap_builder *bb,
205 struct bitmap_writer *writer,
206 struct bitmap_index *old_bitmap)
207 {
208 struct rev_info revs;
209 struct commit *commit;
210 struct commit_list *reusable = NULL;
211 struct commit_list *r;
212 unsigned int i, num_maximal = 0;
213
214 memset(bb, 0, sizeof(*bb));
215 init_bb_data(&bb->data);
216
217 reset_revision_walk();
218 repo_init_revisions(writer->to_pack->repo, &revs, NULL);
219 revs.topo_order = 1;
220 revs.first_parent_only = 1;
221
222 for (i = 0; i < writer->selected_nr; i++) {
223 struct commit *c = writer->selected[i].commit;
224 struct bb_commit *ent = bb_data_at(&bb->data, c);
225
226 ent->selected = 1;
227 ent->maximal = 1;
228 ent->idx = i;
229
230 ent->commit_mask = bitmap_new();
231 bitmap_set(ent->commit_mask, i);
232
233 add_pending_object(&revs, &c->object, "");
234 }
235
236 if (prepare_revision_walk(&revs))
237 die("revision walk setup failed");
238
239 while ((commit = get_revision(&revs))) {
240 struct commit_list *p = commit->parents;
241 struct bb_commit *c_ent;
242
243 parse_commit_or_die(commit);
244
245 c_ent = bb_data_at(&bb->data, commit);
246
247 /*
248 * If there is no commit_mask, there is no reason to iterate
249 * over this commit; it is not selected (if it were, it would
250 * not have a blank commit mask) and all its children have
251 * existing bitmaps (see the comment starting with "This commit
252 * has an existing bitmap" below), so it does not contribute
253 * anything to the final bitmap file or its descendants.
254 */
255 if (!c_ent->commit_mask)
256 continue;
257
258 if (old_bitmap && bitmap_for_commit(old_bitmap, commit)) {
259 /*
260 * This commit has an existing bitmap, so we can
261 * get its bits immediately without an object
262 * walk. That is, it is reusable as-is and there is no
263 * need to continue walking beyond it.
264 *
265 * Mark it as such and add it to bb->commits separately
266 * to avoid allocating a position in the commit mask.
267 */
268 commit_list_insert(commit, &reusable);
269 goto next;
270 }
271
272 if (c_ent->maximal) {
273 num_maximal++;
274 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
275 bb->commits[bb->commits_nr++] = commit;
276 }
277
278 if (p) {
279 struct bb_commit *p_ent = bb_data_at(&bb->data, p->item);
280 int c_not_p, p_not_c;
281
282 if (!p_ent->commit_mask) {
283 p_ent->commit_mask = bitmap_new();
284 c_not_p = 1;
285 p_not_c = 0;
286 } else {
287 c_not_p = bitmap_is_subset(c_ent->commit_mask, p_ent->commit_mask);
288 p_not_c = bitmap_is_subset(p_ent->commit_mask, c_ent->commit_mask);
289 }
290
291 if (!c_not_p)
292 continue;
293
294 bitmap_or(p_ent->commit_mask, c_ent->commit_mask);
295
296 if (p_not_c)
297 p_ent->maximal = 1;
298 else {
299 p_ent->maximal = 0;
300 free_commit_list(p_ent->reverse_edges);
301 p_ent->reverse_edges = NULL;
302 }
303
304 if (c_ent->maximal) {
305 commit_list_insert(commit, &p_ent->reverse_edges);
306 } else {
307 struct commit_list *cc = c_ent->reverse_edges;
308
309 for (; cc; cc = cc->next) {
310 if (!commit_list_contains(cc->item, p_ent->reverse_edges))
311 commit_list_insert(cc->item, &p_ent->reverse_edges);
312 }
313 }
314 }
315
316 next:
317 bitmap_free(c_ent->commit_mask);
318 c_ent->commit_mask = NULL;
319 }
320
321 for (r = reusable; r; r = r->next) {
322 ALLOC_GROW(bb->commits, bb->commits_nr + 1, bb->commits_alloc);
323 bb->commits[bb->commits_nr++] = r->item;
324 }
325
326 trace2_data_intmax("pack-bitmap-write", the_repository,
327 "num_selected_commits", writer->selected_nr);
328 trace2_data_intmax("pack-bitmap-write", the_repository,
329 "num_maximal_commits", num_maximal);
330
331 release_revisions(&revs);
332 free_commit_list(reusable);
333 }
334
335 static void bitmap_builder_clear(struct bitmap_builder *bb)
336 {
337 clear_bb_data(&bb->data);
338 free(bb->commits);
339 bb->commits_nr = bb->commits_alloc = 0;
340 }
341
342 static int fill_bitmap_tree(struct bitmap *bitmap,
343 struct tree *tree)
344 {
345 int found;
346 uint32_t pos;
347 struct tree_desc desc;
348 struct name_entry entry;
349
350 /*
351 * If our bit is already set, then there is nothing to do. Both this
352 * tree and all of its children will be set.
353 */
354 pos = find_object_pos(&tree->object.oid, &found);
355 if (!found)
356 return -1;
357 if (bitmap_get(bitmap, pos))
358 return 0;
359 bitmap_set(bitmap, pos);
360
361 if (parse_tree(tree) < 0)
362 die("unable to load tree object %s",
363 oid_to_hex(&tree->object.oid));
364 init_tree_desc(&desc, tree->buffer, tree->size);
365
366 while (tree_entry(&desc, &entry)) {
367 switch (object_type(entry.mode)) {
368 case OBJ_TREE:
369 if (fill_bitmap_tree(bitmap,
370 lookup_tree(the_repository, &entry.oid)) < 0)
371 return -1;
372 break;
373 case OBJ_BLOB:
374 pos = find_object_pos(&entry.oid, &found);
375 if (!found)
376 return -1;
377 bitmap_set(bitmap, pos);
378 break;
379 default:
380 /* Gitlink, etc; not reachable */
381 break;
382 }
383 }
384
385 free_tree_buffer(tree);
386 return 0;
387 }
388
389 static int reused_bitmaps_nr;
390
391 static int fill_bitmap_commit(struct bb_commit *ent,
392 struct commit *commit,
393 struct prio_queue *queue,
394 struct prio_queue *tree_queue,
395 struct bitmap_index *old_bitmap,
396 const uint32_t *mapping)
397 {
398 int found;
399 uint32_t pos;
400 if (!ent->bitmap)
401 ent->bitmap = bitmap_new();
402
403 prio_queue_put(queue, commit);
404
405 while (queue->nr) {
406 struct commit_list *p;
407 struct commit *c = prio_queue_get(queue);
408
409 if (old_bitmap && mapping) {
410 struct ewah_bitmap *old = bitmap_for_commit(old_bitmap, c);
411 /*
412 * If this commit has an old bitmap, then translate that
413 * bitmap and add its bits to this one. No need to walk
414 * parents or the tree for this commit.
415 */
416 if (old && !rebuild_bitmap(mapping, old, ent->bitmap)) {
417 reused_bitmaps_nr++;
418 continue;
419 }
420 }
421
422 /*
423 * Mark ourselves and queue our tree. The commit
424 * walk ensures we cover all parents.
425 */
426 pos = find_object_pos(&c->object.oid, &found);
427 if (!found)
428 return -1;
429 bitmap_set(ent->bitmap, pos);
430 prio_queue_put(tree_queue, get_commit_tree(c));
431
432 for (p = c->parents; p; p = p->next) {
433 pos = find_object_pos(&p->item->object.oid, &found);
434 if (!found)
435 return -1;
436 if (!bitmap_get(ent->bitmap, pos)) {
437 bitmap_set(ent->bitmap, pos);
438 prio_queue_put(queue, p->item);
439 }
440 }
441 }
442
443 while (tree_queue->nr) {
444 if (fill_bitmap_tree(ent->bitmap,
445 prio_queue_get(tree_queue)) < 0)
446 return -1;
447 }
448 return 0;
449 }
450
451 static void store_selected(struct bb_commit *ent, struct commit *commit)
452 {
453 struct bitmapped_commit *stored = &writer.selected[ent->idx];
454 khiter_t hash_pos;
455 int hash_ret;
456
457 stored->bitmap = bitmap_to_ewah(ent->bitmap);
458
459 hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret);
460 if (hash_ret == 0)
461 die("Duplicate entry when writing index: %s",
462 oid_to_hex(&commit->object.oid));
463 kh_value(writer.bitmaps, hash_pos) = stored;
464 }
465
466 int bitmap_writer_build(struct packing_data *to_pack)
467 {
468 struct bitmap_builder bb;
469 size_t i;
470 int nr_stored = 0; /* for progress */
471 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
472 struct prio_queue tree_queue = { NULL };
473 struct bitmap_index *old_bitmap;
474 uint32_t *mapping;
475 int closed = 1; /* until proven otherwise */
476
477 writer.bitmaps = kh_init_oid_map();
478 writer.to_pack = to_pack;
479
480 if (writer.show_progress)
481 writer.progress = start_progress("Building bitmaps", writer.selected_nr);
482 trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
483 the_repository);
484
485 old_bitmap = prepare_bitmap_git(to_pack->repo);
486 if (old_bitmap)
487 mapping = create_bitmap_mapping(old_bitmap, to_pack);
488 else
489 mapping = NULL;
490
491 bitmap_builder_init(&bb, &writer, old_bitmap);
492 for (i = bb.commits_nr; i > 0; i--) {
493 struct commit *commit = bb.commits[i-1];
494 struct bb_commit *ent = bb_data_at(&bb.data, commit);
495 struct commit *child;
496 int reused = 0;
497
498 if (fill_bitmap_commit(ent, commit, &queue, &tree_queue,
499 old_bitmap, mapping) < 0) {
500 closed = 0;
501 break;
502 }
503
504 if (ent->selected) {
505 store_selected(ent, commit);
506 nr_stored++;
507 display_progress(writer.progress, nr_stored);
508 }
509
510 while ((child = pop_commit(&ent->reverse_edges))) {
511 struct bb_commit *child_ent =
512 bb_data_at(&bb.data, child);
513
514 if (child_ent->bitmap)
515 bitmap_or(child_ent->bitmap, ent->bitmap);
516 else if (reused)
517 child_ent->bitmap = bitmap_dup(ent->bitmap);
518 else {
519 child_ent->bitmap = ent->bitmap;
520 reused = 1;
521 }
522 }
523 if (!reused)
524 bitmap_free(ent->bitmap);
525 ent->bitmap = NULL;
526 }
527 clear_prio_queue(&queue);
528 clear_prio_queue(&tree_queue);
529 bitmap_builder_clear(&bb);
530 free_bitmap_index(old_bitmap);
531 free(mapping);
532
533 trace2_region_leave("pack-bitmap-write", "building_bitmaps_total",
534 the_repository);
535 trace2_data_intmax("pack-bitmap-write", the_repository,
536 "building_bitmaps_reused", reused_bitmaps_nr);
537
538 stop_progress(&writer.progress);
539
540 if (closed)
541 compute_xor_offsets();
542 return closed ? 0 : -1;
543 }
544
545 /**
546 * Select the commits that will be bitmapped
547 */
548 static inline unsigned int next_commit_index(unsigned int idx)
549 {
550 static const unsigned int MIN_COMMITS = 100;
551 static const unsigned int MAX_COMMITS = 5000;
552
553 static const unsigned int MUST_REGION = 100;
554 static const unsigned int MIN_REGION = 20000;
555
556 unsigned int offset, next;
557
558 if (idx <= MUST_REGION)
559 return 0;
560
561 if (idx <= MIN_REGION) {
562 offset = idx - MUST_REGION;
563 return (offset < MIN_COMMITS) ? offset : MIN_COMMITS;
564 }
565
566 offset = idx - MIN_REGION;
567 next = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;
568
569 return (next > MIN_COMMITS) ? next : MIN_COMMITS;
570 }
571
572 static int date_compare(const void *_a, const void *_b)
573 {
574 struct commit *a = *(struct commit **)_a;
575 struct commit *b = *(struct commit **)_b;
576 return (long)b->date - (long)a->date;
577 }
578
579 void bitmap_writer_select_commits(struct commit **indexed_commits,
580 unsigned int indexed_commits_nr,
581 int max_bitmaps)
582 {
583 unsigned int i = 0, j, next;
584
585 QSORT(indexed_commits, indexed_commits_nr, date_compare);
586
587 if (indexed_commits_nr < 100) {
588 for (i = 0; i < indexed_commits_nr; ++i)
589 push_bitmapped_commit(indexed_commits[i]);
590 return;
591 }
592
593 if (writer.show_progress)
594 writer.progress = start_progress("Selecting bitmap commits", 0);
595
596 for (;;) {
597 struct commit *chosen = NULL;
598
599 next = next_commit_index(i);
600
601 if (i + next >= indexed_commits_nr)
602 break;
603
604 if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
605 writer.selected_nr = max_bitmaps;
606 break;
607 }
608
609 if (next == 0) {
610 chosen = indexed_commits[i];
611 } else {
612 chosen = indexed_commits[i + next];
613
614 for (j = 0; j <= next; ++j) {
615 struct commit *cm = indexed_commits[i + j];
616
617 if ((cm->object.flags & NEEDS_BITMAP) != 0) {
618 chosen = cm;
619 break;
620 }
621
622 if (cm->parents && cm->parents->next)
623 chosen = cm;
624 }
625 }
626
627 push_bitmapped_commit(chosen);
628
629 i += next + 1;
630 display_progress(writer.progress, i);
631 }
632
633 stop_progress(&writer.progress);
634 }
635
636
637 static int hashwrite_ewah_helper(void *f, const void *buf, size_t len)
638 {
639 /* hashwrite will die on error */
640 hashwrite(f, buf, len);
641 return len;
642 }
643
644 /**
645 * Write the bitmap index to disk
646 */
647 static inline void dump_bitmap(struct hashfile *f, struct ewah_bitmap *bitmap)
648 {
649 if (ewah_serialize_to(bitmap, hashwrite_ewah_helper, f) < 0)
650 die("Failed to write bitmap index");
651 }
652
653 static const struct object_id *oid_access(size_t pos, const void *table)
654 {
655 const struct pack_idx_entry * const *index = table;
656 return &index[pos]->oid;
657 }
658
659 static void write_selected_commits_v1(struct hashfile *f,
660 uint32_t *commit_positions,
661 off_t *offsets)
662 {
663 int i;
664
665 for (i = 0; i < writer.selected_nr; ++i) {
666 struct bitmapped_commit *stored = &writer.selected[i];
667
668 if (offsets)
669 offsets[i] = hashfile_total(f);
670
671 hashwrite_be32(f, commit_positions[i]);
672 hashwrite_u8(f, stored->xor_offset);
673 hashwrite_u8(f, stored->flags);
674
675 dump_bitmap(f, stored->write_as);
676 }
677 }
678
679 static int table_cmp(const void *_va, const void *_vb, void *_data)
680 {
681 uint32_t *commit_positions = _data;
682 uint32_t a = commit_positions[*(uint32_t *)_va];
683 uint32_t b = commit_positions[*(uint32_t *)_vb];
684
685 if (a > b)
686 return 1;
687 else if (a < b)
688 return -1;
689
690 return 0;
691 }
692
693 static void write_lookup_table(struct hashfile *f,
694 uint32_t *commit_positions,
695 off_t *offsets)
696 {
697 uint32_t i;
698 uint32_t *table, *table_inv;
699
700 ALLOC_ARRAY(table, writer.selected_nr);
701 ALLOC_ARRAY(table_inv, writer.selected_nr);
702
703 for (i = 0; i < writer.selected_nr; i++)
704 table[i] = i;
705
706 /*
707 * At the end of this sort table[j] = i means that the i'th
708 * bitmap corresponds to j'th bitmapped commit (among the selected
709 * commits) in lex order of OIDs.
710 */
711 QSORT_S(table, writer.selected_nr, table_cmp, commit_positions);
712
713 /* table_inv helps us discover that relationship (i'th bitmap
714 * to j'th commit by j = table_inv[i])
715 */
716 for (i = 0; i < writer.selected_nr; i++)
717 table_inv[table[i]] = i;
718
719 trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository);
720 for (i = 0; i < writer.selected_nr; i++) {
721 struct bitmapped_commit *selected = &writer.selected[table[i]];
722 uint32_t xor_offset = selected->xor_offset;
723 uint32_t xor_row;
724
725 if (xor_offset) {
726 /*
727 * xor_index stores the index (in the bitmap entries)
728 * of the corresponding xor bitmap. But we need to convert
729 * this index into lookup table's index. So, table_inv[xor_index]
730 * gives us the index position w.r.t. the lookup table.
731 *
732 * If "k = table[i] - xor_offset" then the xor base is the k'th
733 * bitmap. `table_inv[k]` gives us the position of that bitmap
734 * in the lookup table.
735 */
736 uint32_t xor_index = table[i] - xor_offset;
737 xor_row = table_inv[xor_index];
738 } else {
739 xor_row = 0xffffffff;
740 }
741
742 hashwrite_be32(f, commit_positions[table[i]]);
743 hashwrite_be64(f, (uint64_t)offsets[table[i]]);
744 hashwrite_be32(f, xor_row);
745 }
746 trace2_region_leave("pack-bitmap-write", "writing_lookup_table", the_repository);
747
748 free(table);
749 free(table_inv);
750 }
751
752 static void write_hash_cache(struct hashfile *f,
753 struct pack_idx_entry **index,
754 uint32_t index_nr)
755 {
756 uint32_t i;
757
758 for (i = 0; i < index_nr; ++i) {
759 struct object_entry *entry = (struct object_entry *)index[i];
760 hashwrite_be32(f, entry->hash);
761 }
762 }
763
764 void bitmap_writer_set_checksum(const unsigned char *sha1)
765 {
766 hashcpy(writer.pack_checksum, sha1);
767 }
768
769 void bitmap_writer_finish(struct pack_idx_entry **index,
770 uint32_t index_nr,
771 const char *filename,
772 uint16_t options)
773 {
774 static uint16_t default_version = 1;
775 static uint16_t flags = BITMAP_OPT_FULL_DAG;
776 struct strbuf tmp_file = STRBUF_INIT;
777 struct hashfile *f;
778 uint32_t *commit_positions = NULL;
779 off_t *offsets = NULL;
780 uint32_t i;
781
782 struct bitmap_disk_header header;
783
784 int fd = odb_mkstemp(&tmp_file, "pack/tmp_bitmap_XXXXXX");
785
786 f = hashfd(fd, tmp_file.buf);
787
788 memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
789 header.version = htons(default_version);
790 header.options = htons(flags | options);
791 header.entry_count = htonl(writer.selected_nr);
792 hashcpy(header.checksum, writer.pack_checksum);
793
794 hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
795 dump_bitmap(f, writer.commits);
796 dump_bitmap(f, writer.trees);
797 dump_bitmap(f, writer.blobs);
798 dump_bitmap(f, writer.tags);
799
800 if (options & BITMAP_OPT_LOOKUP_TABLE)
801 CALLOC_ARRAY(offsets, index_nr);
802
803 ALLOC_ARRAY(commit_positions, writer.selected_nr);
804
805 for (i = 0; i < writer.selected_nr; i++) {
806 struct bitmapped_commit *stored = &writer.selected[i];
807 int commit_pos = oid_pos(&stored->commit->object.oid, index, index_nr, oid_access);
808
809 if (commit_pos < 0)
810 BUG(_("trying to write commit not in index"));
811
812 commit_positions[i] = commit_pos;
813 }
814
815 write_selected_commits_v1(f, commit_positions, offsets);
816
817 if (options & BITMAP_OPT_LOOKUP_TABLE)
818 write_lookup_table(f, commit_positions, offsets);
819
820 if (options & BITMAP_OPT_HASH_CACHE)
821 write_hash_cache(f, index, index_nr);
822
823 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA,
824 CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE);
825
826 if (adjust_shared_perm(tmp_file.buf))
827 die_errno("unable to make temporary bitmap file readable");
828
829 if (rename(tmp_file.buf, filename))
830 die_errno("unable to rename temporary bitmap file to '%s'", filename);
831
832 strbuf_release(&tmp_file);
833 free(commit_positions);
834 free(offsets);
835 }