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