-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack-bitmap.c
2690 lines (2207 loc) · 68.9 KB
/
pack-bitmap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "git-compat-util.h"
#include "commit.h"
#include "gettext.h"
#include "hex.h"
#include "strbuf.h"
#include "tag.h"
#include "diff.h"
#include "revision.h"
#include "progress.h"
#include "list-objects.h"
#include "pack.h"
#include "pack-bitmap.h"
#include "pack-revindex.h"
#include "pack-objects.h"
#include "packfile.h"
#include "repository.h"
#include "trace2.h"
#include "object-file.h"
#include "object-store-ll.h"
#include "list-objects-filter-options.h"
#include "midx.h"
#include "config.h"
/*
* An entry on the bitmap index, representing the bitmap for a given
* commit.
*/
struct stored_bitmap {
struct object_id oid;
struct ewah_bitmap *root;
struct stored_bitmap *xor;
int flags;
};
/*
* The active bitmap index for a repository. By design, repositories only have
* a single bitmap index available (the index for the biggest packfile in
* the repository), since bitmap indexes need full closure.
*
* If there is more than one bitmap index available (e.g. because of alternates),
* the active bitmap index is the largest one.
*/
struct bitmap_index {
/*
* The pack or multi-pack index (MIDX) that this bitmap index belongs
* to.
*
* Exactly one of these must be non-NULL; this specifies the object
* order used to interpret this bitmap.
*/
struct packed_git *pack;
struct multi_pack_index *midx;
/* mmapped buffer of the whole bitmap index */
unsigned char *map;
size_t map_size; /* size of the mmaped buffer */
size_t map_pos; /* current position when loading the index */
/*
* Type indexes.
*
* Each bitmap marks which objects in the packfile are of the given
* type. This provides type information when yielding the objects from
* the packfile during a walk, which allows for better delta bases.
*/
struct ewah_bitmap *commits;
struct ewah_bitmap *trees;
struct ewah_bitmap *blobs;
struct ewah_bitmap *tags;
/* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
kh_oid_map_t *bitmaps;
/* Number of bitmapped commits */
uint32_t entry_count;
/* If not NULL, this is a name-hash cache pointing into map. */
uint32_t *hashes;
/* The checksum of the packfile or MIDX; points into map. */
const unsigned char *checksum;
/*
* If not NULL, this point into the commit table extension
* (within the memory mapped region `map`).
*/
unsigned char *table_lookup;
/*
* Extended index.
*
* When trying to perform bitmap operations with objects that are not
* packed in `pack`, these objects are added to this "fake index" and
* are assumed to appear at the end of the packfile for all operations
*/
struct eindex {
struct object **objects;
uint32_t *hashes;
uint32_t count, alloc;
kh_oid_pos_t *positions;
} ext_index;
/* Bitmap result of the last performed walk */
struct bitmap *result;
/* "have" bitmap from the last performed walk */
struct bitmap *haves;
/* Version of the bitmap index */
unsigned int version;
};
static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
{
struct ewah_bitmap *parent;
struct ewah_bitmap *composed;
if (!st->xor)
return st->root;
composed = ewah_pool_new();
parent = lookup_stored_bitmap(st->xor);
ewah_xor(st->root, parent, composed);
ewah_pool_free(st->root);
st->root = composed;
st->xor = NULL;
return composed;
}
/*
* Read a bitmap from the current read position on the mmaped
* index, and increase the read position accordingly
*/
static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
{
struct ewah_bitmap *b = ewah_pool_new();
ssize_t bitmap_size = ewah_read_mmap(b,
index->map + index->map_pos,
index->map_size - index->map_pos);
if (bitmap_size < 0) {
error(_("failed to load bitmap index (corrupted?)"));
ewah_pool_free(b);
return NULL;
}
index->map_pos += bitmap_size;
return b;
}
static uint32_t bitmap_num_objects(struct bitmap_index *index)
{
if (index->midx)
return index->midx->num_objects;
return index->pack->num_objects;
}
static int load_bitmap_header(struct bitmap_index *index)
{
struct bitmap_disk_header *header = (void *)index->map;
size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz;
if (index->map_size < header_size + the_hash_algo->rawsz)
return error(_("corrupted bitmap index (too small)"));
if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
return error(_("corrupted bitmap index file (wrong header)"));
index->version = ntohs(header->version);
if (index->version != 1)
return error(_("unsupported version '%d' for bitmap index file"), index->version);
/* Parse known bitmap format options */
{
uint32_t flags = ntohs(header->options);
size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
unsigned char *index_end = index->map + index->map_size - the_hash_algo->rawsz;
if ((flags & BITMAP_OPT_FULL_DAG) == 0)
BUG("unsupported options for bitmap index file "
"(Git requires BITMAP_OPT_FULL_DAG)");
if (flags & BITMAP_OPT_HASH_CACHE) {
if (cache_size > index_end - index->map - header_size)
return error(_("corrupted bitmap index file (too short to fit hash cache)"));
index->hashes = (void *)(index_end - cache_size);
index_end -= cache_size;
}
if (flags & BITMAP_OPT_LOOKUP_TABLE) {
size_t table_size = st_mult(ntohl(header->entry_count),
BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
if (table_size > index_end - index->map - header_size)
return error(_("corrupted bitmap index file (too short to fit lookup table)"));
if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
index->table_lookup = (void *)(index_end - table_size);
index_end -= table_size;
}
}
index->entry_count = ntohl(header->entry_count);
index->checksum = header->checksum;
index->map_pos += header_size;
return 0;
}
static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
struct ewah_bitmap *root,
const struct object_id *oid,
struct stored_bitmap *xor_with,
int flags)
{
struct stored_bitmap *stored;
khiter_t hash_pos;
int ret;
stored = xmalloc(sizeof(struct stored_bitmap));
stored->root = root;
stored->xor = xor_with;
stored->flags = flags;
oidcpy(&stored->oid, oid);
hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
/*
* A 0 return code means the insertion succeeded with no changes,
* because the SHA1 already existed on the map. This is bad, there
* shouldn't be duplicated commits in the index.
*/
if (ret == 0) {
error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
return NULL;
}
kh_value(index->bitmaps, hash_pos) = stored;
return stored;
}
static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
{
uint32_t result = get_be32(buffer + *pos);
(*pos) += sizeof(result);
return result;
}
static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
{
return buffer[(*pos)++];
}
#define MAX_XOR_OFFSET 160
static int nth_bitmap_object_oid(struct bitmap_index *index,
struct object_id *oid,
uint32_t n)
{
if (index->midx)
return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
return nth_packed_object_id(oid, index->pack, n);
}
static int load_bitmap_entries_v1(struct bitmap_index *index)
{
uint32_t i;
struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
for (i = 0; i < index->entry_count; ++i) {
int xor_offset, flags;
struct ewah_bitmap *bitmap = NULL;
struct stored_bitmap *xor_bitmap = NULL;
uint32_t commit_idx_pos;
struct object_id oid;
if (index->map_size - index->map_pos < 6)
return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
commit_idx_pos = read_be32(index->map, &index->map_pos);
xor_offset = read_u8(index->map, &index->map_pos);
flags = read_u8(index->map, &index->map_pos);
if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
return error(_("corrupt ewah bitmap: commit index %u out of range"),
(unsigned)commit_idx_pos);
bitmap = read_bitmap_1(index);
if (!bitmap)
return -1;
if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
return error(_("corrupted bitmap pack index"));
if (xor_offset > 0) {
xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
if (!xor_bitmap)
return error(_("invalid XOR offset in bitmap pack index"));
}
recent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(
index, bitmap, &oid, xor_bitmap, flags);
}
return 0;
}
char *midx_bitmap_filename(struct multi_pack_index *midx)
{
struct strbuf buf = STRBUF_INIT;
get_midx_filename(&buf, midx->object_dir);
strbuf_addf(&buf, "-%s.bitmap", hash_to_hex(get_midx_checksum(midx)));
return strbuf_detach(&buf, NULL);
}
char *pack_bitmap_filename(struct packed_git *p)
{
size_t len;
if (!strip_suffix(p->pack_name, ".pack", &len))
BUG("pack_name does not end in .pack");
return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
}
static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
struct multi_pack_index *midx)
{
struct stat st;
char *bitmap_name = midx_bitmap_filename(midx);
int fd = git_open(bitmap_name);
uint32_t i, preferred_pack;
struct packed_git *preferred;
if (fd < 0) {
if (errno != ENOENT)
warning_errno("cannot open '%s'", bitmap_name);
free(bitmap_name);
return -1;
}
free(bitmap_name);
if (fstat(fd, &st)) {
error_errno(_("cannot fstat bitmap file"));
close(fd);
return -1;
}
if (bitmap_git->pack || bitmap_git->midx) {
struct strbuf buf = STRBUF_INIT;
get_midx_filename(&buf, midx->object_dir);
trace2_data_string("bitmap", the_repository,
"ignoring extra midx bitmap file", buf.buf);
close(fd);
strbuf_release(&buf);
return -1;
}
bitmap_git->midx = midx;
bitmap_git->map_size = xsize_t(st.st_size);
bitmap_git->map_pos = 0;
bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
MAP_PRIVATE, fd, 0);
close(fd);
if (load_bitmap_header(bitmap_git) < 0)
goto cleanup;
if (!hasheq(get_midx_checksum(bitmap_git->midx), bitmap_git->checksum)) {
error(_("checksum doesn't match in MIDX and bitmap"));
goto cleanup;
}
if (load_midx_revindex(bitmap_git->midx)) {
warning(_("multi-pack bitmap is missing required reverse index"));
goto cleanup;
}
for (i = 0; i < bitmap_git->midx->num_packs; i++) {
if (prepare_midx_pack(the_repository, bitmap_git->midx, i)) {
warning(_("could not open pack %s"),
bitmap_git->midx->pack_names[i]);
goto cleanup;
}
}
if (midx_preferred_pack(bitmap_git->midx, &preferred_pack) < 0) {
warning(_("could not determine MIDX preferred pack"));
goto cleanup;
}
preferred = bitmap_git->midx->packs[preferred_pack];
if (!is_pack_valid(preferred)) {
warning(_("preferred pack (%s) is invalid"),
preferred->pack_name);
goto cleanup;
}
return 0;
cleanup:
munmap(bitmap_git->map, bitmap_git->map_size);
bitmap_git->map_size = 0;
bitmap_git->map_pos = 0;
bitmap_git->map = NULL;
bitmap_git->midx = NULL;
return -1;
}
static int open_pack_bitmap_1(struct bitmap_index *bitmap_git, struct packed_git *packfile)
{
int fd;
struct stat st;
char *bitmap_name;
bitmap_name = pack_bitmap_filename(packfile);
fd = git_open(bitmap_name);
if (fd < 0) {
if (errno != ENOENT)
warning_errno("cannot open '%s'", bitmap_name);
free(bitmap_name);
return -1;
}
free(bitmap_name);
if (fstat(fd, &st)) {
error_errno(_("cannot fstat bitmap file"));
close(fd);
return -1;
}
if (bitmap_git->pack || bitmap_git->midx) {
trace2_data_string("bitmap", the_repository,
"ignoring extra bitmap file", packfile->pack_name);
close(fd);
return -1;
}
if (!is_pack_valid(packfile)) {
close(fd);
return -1;
}
bitmap_git->pack = packfile;
bitmap_git->map_size = xsize_t(st.st_size);
bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
bitmap_git->map_pos = 0;
close(fd);
if (load_bitmap_header(bitmap_git) < 0) {
munmap(bitmap_git->map, bitmap_git->map_size);
bitmap_git->map = NULL;
bitmap_git->map_size = 0;
bitmap_git->map_pos = 0;
bitmap_git->pack = NULL;
return -1;
}
trace2_data_string("bitmap", the_repository, "opened bitmap file",
packfile->pack_name);
return 0;
}
static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
{
if (bitmap_is_midx(bitmap_git)) {
uint32_t i;
int ret;
/*
* The multi-pack-index's .rev file is already loaded via
* open_pack_bitmap_1().
*
* But we still need to open the individual pack .rev files,
* since we will need to make use of them in pack-objects.
*/
for (i = 0; i < bitmap_git->midx->num_packs; i++) {
ret = load_pack_revindex(r, bitmap_git->midx->packs[i]);
if (ret)
return ret;
}
return 0;
}
return load_pack_revindex(r, bitmap_git->pack);
}
static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git)
{
assert(bitmap_git->map);
bitmap_git->bitmaps = kh_init_oid_map();
bitmap_git->ext_index.positions = kh_init_oid_pos();
if (load_reverse_index(r, bitmap_git))
goto failed;
if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
!(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
!(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
!(bitmap_git->tags = read_bitmap_1(bitmap_git)))
goto failed;
if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
goto failed;
return 0;
failed:
munmap(bitmap_git->map, bitmap_git->map_size);
bitmap_git->map = NULL;
bitmap_git->map_size = 0;
kh_destroy_oid_map(bitmap_git->bitmaps);
bitmap_git->bitmaps = NULL;
kh_destroy_oid_pos(bitmap_git->ext_index.positions);
bitmap_git->ext_index.positions = NULL;
return -1;
}
static int open_pack_bitmap(struct repository *r,
struct bitmap_index *bitmap_git)
{
struct packed_git *p;
int ret = -1;
for (p = get_all_packs(r); p; p = p->next) {
if (open_pack_bitmap_1(bitmap_git, p) == 0) {
ret = 0;
/*
* The only reason to keep looking is to report
* duplicates.
*/
if (!trace2_is_enabled())
break;
}
}
return ret;
}
static int open_midx_bitmap(struct repository *r,
struct bitmap_index *bitmap_git)
{
int ret = -1;
struct multi_pack_index *midx;
assert(!bitmap_git->map);
for (midx = get_multi_pack_index(r); midx; midx = midx->next) {
if (!open_midx_bitmap_1(bitmap_git, midx))
ret = 0;
}
return ret;
}
static int open_bitmap(struct repository *r,
struct bitmap_index *bitmap_git)
{
int found;
assert(!bitmap_git->map);
found = !open_midx_bitmap(r, bitmap_git);
/*
* these will all be skipped if we opened a midx bitmap; but run it
* anyway if tracing is enabled to report the duplicates
*/
if (!found || trace2_is_enabled())
found |= !open_pack_bitmap(r, bitmap_git);
return found ? 0 : -1;
}
struct bitmap_index *prepare_bitmap_git(struct repository *r)
{
struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git))
return bitmap_git;
free_bitmap_index(bitmap_git);
return NULL;
}
struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
{
struct repository *r = the_repository;
struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
if (!open_midx_bitmap_1(bitmap_git, midx) && !load_bitmap(r, bitmap_git))
return bitmap_git;
free_bitmap_index(bitmap_git);
return NULL;
}
struct include_data {
struct bitmap_index *bitmap_git;
struct bitmap *base;
struct bitmap *seen;
};
struct bitmap_lookup_table_triplet {
uint32_t commit_pos;
uint64_t offset;
uint32_t xor_row;
};
struct bitmap_lookup_table_xor_item {
struct object_id oid;
uint64_t offset;
};
/*
* Given a `triplet` struct pointer and pointer `p`, this
* function reads the triplet beginning at `p` into the struct.
* Note that this function assumes that there is enough memory
* left for filling the `triplet` struct from `p`.
*/
static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
const unsigned char *p)
{
if (!triplet)
return -1;
triplet->commit_pos = get_be32(p);
p += sizeof(uint32_t);
triplet->offset = get_be64(p);
p += sizeof(uint64_t);
triplet->xor_row = get_be32(p);
return 0;
}
/*
* This function gets the raw triplet from `row`'th row in the
* lookup table and fills that data to the `triplet`.
*/
static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
uint32_t pos,
struct bitmap_lookup_table_triplet *triplet)
{
unsigned char *p = NULL;
if (pos >= bitmap_git->entry_count)
return error(_("corrupt bitmap lookup table: triplet position out of index"));
p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
}
/*
* Searches for a matching triplet. `commit_pos` is a pointer
* to the wanted commit position value. `table_entry` points to
* a triplet in lookup table. The first 4 bytes of each
* triplet (pointed by `table_entry`) are compared with `*commit_pos`.
*/
static int triplet_cmp(const void *commit_pos, const void *table_entry)
{
uint32_t a = *(uint32_t *)commit_pos;
uint32_t b = get_be32(table_entry);
if (a > b)
return 1;
else if (a < b)
return -1;
return 0;
}
static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
struct object_id *oid,
uint32_t *result)
{
int found;
if (bitmap_is_midx(bitmap_git))
found = bsearch_midx(oid, bitmap_git->midx, result);
else
found = bsearch_pack(oid, bitmap_git->pack, result);
return found;
}
/*
* `bsearch_triplet_by_pos` function searches for the raw triplet
* having commit position same as `commit_pos` and fills `triplet`
* object from the raw triplet. Returns 1 on success and 0 on
* failure.
*/
static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
struct bitmap_index *bitmap_git,
struct bitmap_lookup_table_triplet *triplet)
{
unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
if (!p)
return -1;
return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
}
static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
struct commit *commit)
{
uint32_t commit_pos, xor_row;
uint64_t offset;
int flags;
struct bitmap_lookup_table_triplet triplet;
struct object_id *oid = &commit->object.oid;
struct ewah_bitmap *bitmap;
struct stored_bitmap *xor_bitmap = NULL;
const int bitmap_header_size = 6;
static struct bitmap_lookup_table_xor_item *xor_items = NULL;
static size_t xor_items_nr = 0, xor_items_alloc = 0;
static int is_corrupt = 0;
int xor_flags;
khiter_t hash_pos;
struct bitmap_lookup_table_xor_item *xor_item;
if (is_corrupt)
return NULL;
if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
return NULL;
if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
return NULL;
xor_items_nr = 0;
offset = triplet.offset;
xor_row = triplet.xor_row;
while (xor_row != 0xffffffff) {
ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
if (xor_items_nr + 1 >= bitmap_git->entry_count) {
error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
goto corrupt;
}
if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
goto corrupt;
xor_item = &xor_items[xor_items_nr];
xor_item->offset = triplet.offset;
if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
error(_("corrupt bitmap lookup table: commit index %u out of range"),
triplet.commit_pos);
goto corrupt;
}
hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
/*
* If desired bitmap is already stored, we don't need
* to iterate further. Because we know that bitmaps
* that are needed to be parsed to parse this bitmap
* has already been stored. So, assign this stored bitmap
* to the xor_bitmap.
*/
if (hash_pos < kh_end(bitmap_git->bitmaps) &&
(xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
break;
xor_items_nr++;
xor_row = triplet.xor_row;
}
while (xor_items_nr) {
xor_item = &xor_items[xor_items_nr - 1];
bitmap_git->map_pos = xor_item->offset;
if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
oid_to_hex(&xor_item->oid));
goto corrupt;
}
bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
bitmap = read_bitmap_1(bitmap_git);
if (!bitmap)
goto corrupt;
xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid, xor_bitmap, xor_flags);
xor_items_nr--;
}
bitmap_git->map_pos = offset;
if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
oid_to_hex(oid));
goto corrupt;
}
/*
* Don't bother reading the commit's index position or its xor
* offset:
*
* - The commit's index position is irrelevant to us, since
* load_bitmap_entries_v1 only uses it to learn the object
* id which is used to compute the hashmap's key. We already
* have an object id, so no need to look it up again.
*
* - The xor_offset is unusable for us, since it specifies how
* many entries previous to ours we should look at. This
* makes sense when reading the bitmaps sequentially (as in
* load_bitmap_entries_v1()), since we can keep track of
* each bitmap as we read them.
*
* But it can't work for us, since the bitmap's don't have a
* fixed size. So we learn the position of the xor'd bitmap
* from the commit table (and resolve it to a bitmap in the
* above if-statement).
*
* Instead, we can skip ahead and immediately read the flags and
* ewah bitmap.
*/
bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
bitmap = read_bitmap_1(bitmap_git);
if (!bitmap)
goto corrupt;
return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags);
corrupt:
free(xor_items);
is_corrupt = 1;
return NULL;
}
struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
struct commit *commit)
{
khiter_t hash_pos = kh_get_oid_map(bitmap_git->bitmaps,
commit->object.oid);
if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
struct stored_bitmap *bitmap = NULL;
if (!bitmap_git->table_lookup)
return NULL;
/* this is a fairly hot codepath - no trace2_region please */
/* NEEDSWORK: cache misses aren't recorded */
bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
if (!bitmap)
return NULL;
return lookup_stored_bitmap(bitmap);
}
return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
}
static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
khiter_t pos = kh_get_oid_pos(positions, *oid);
if (pos < kh_end(positions)) {
int bitmap_pos = kh_value(positions, pos);
return bitmap_pos + bitmap_num_objects(bitmap_git);
}
return -1;
}
static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
uint32_t pos;
off_t offset = find_pack_entry_one(oid->hash, bitmap_git->pack);
if (!offset)
return -1;
if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
return -1;
return pos;
}
static int bitmap_position_midx(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
uint32_t want, got;
if (!bsearch_midx(oid, bitmap_git->midx, &want))
return -1;
if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
return -1;
return got;
}
static int bitmap_position(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
int pos;
if (bitmap_is_midx(bitmap_git))
pos = bitmap_position_midx(bitmap_git, oid);
else
pos = bitmap_position_packfile(bitmap_git, oid);
return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
}
static int ext_index_add_object(struct bitmap_index *bitmap_git,
struct object *object, const char *name)
{
struct eindex *eindex = &bitmap_git->ext_index;
khiter_t hash_pos;
int hash_ret;
int bitmap_pos;
hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
if (hash_ret > 0) {
if (eindex->count >= eindex->alloc) {
eindex->alloc = (eindex->alloc + 16) * 3 / 2;
REALLOC_ARRAY(eindex->objects, eindex->alloc);
REALLOC_ARRAY(eindex->hashes, eindex->alloc);
}
bitmap_pos = eindex->count;
eindex->objects[eindex->count] = object;
eindex->hashes[eindex->count] = pack_name_hash(name);
kh_value(eindex->positions, hash_pos) = bitmap_pos;
eindex->count++;
} else {
bitmap_pos = kh_value(eindex->positions, hash_pos);
}
return bitmap_pos + bitmap_num_objects(bitmap_git);
}
struct bitmap_show_data {
struct bitmap_index *bitmap_git;
struct bitmap *base;
};
static void show_object(struct object *object, const char *name, void *data_)
{
struct bitmap_show_data *data = data_;
int bitmap_pos;
bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
if (bitmap_pos < 0)
bitmap_pos = ext_index_add_object(data->bitmap_git, object,
name);
bitmap_set(data->base, bitmap_pos);
}
static void show_commit(struct commit *commit UNUSED,
void *data UNUSED)
{
}
static int add_to_include_set(struct bitmap_index *bitmap_git,
struct include_data *data,
struct commit *commit,
int bitmap_pos)
{
struct ewah_bitmap *partial;
if (data->seen && bitmap_get(data->seen, bitmap_pos))
return 0;
if (bitmap_get(data->base, bitmap_pos))
return 0;
partial = bitmap_for_commit(bitmap_git, commit);
if (partial) {
bitmap_or_ewah(data->base, partial);
return 0;
}
bitmap_set(data->base, bitmap_pos);
return 1;
}
static int should_include(struct commit *commit, void *_data)
{
struct include_data *data = _data;
int bitmap_pos;
bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
if (bitmap_pos < 0)
bitmap_pos = ext_index_add_object(data->bitmap_git,
(struct object *)commit,
NULL);
if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
struct commit_list *parent = commit->parents;