Coverage Report

Created: 2026-12-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/pack-bitmap.c
Line
Count
Source
1
#define DISABLE_SIGN_COMPARE_WARNINGS
2
3
#include "git-compat-util.h"
4
#include "commit.h"
5
#include "gettext.h"
6
#include "hex.h"
7
#include "strbuf.h"
8
#include "tag.h"
9
#include "diff.h"
10
#include "revision.h"
11
#include "progress.h"
12
#include "list-objects.h"
13
#include "pack.h"
14
#include "pack-bitmap.h"
15
#include "pack-revindex.h"
16
#include "pack-objects.h"
17
#include "packfile.h"
18
#include "repository.h"
19
#include "trace2.h"
20
#include "odb.h"
21
#include "list-objects-filter-options.h"
22
#include "midx.h"
23
#include "config.h"
24
#include "pseudo-merge.h"
25
26
/*
27
 * An entry on the bitmap index, representing the bitmap for a given
28
 * commit.
29
 */
30
struct stored_bitmap {
31
  struct object_id oid;
32
  struct ewah_bitmap *root;
33
  struct stored_bitmap *xor;
34
  size_t map_pos;
35
  int flags;
36
};
37
38
/*
39
 * The active bitmap index for a repository. By design, repositories only have
40
 * a single bitmap index available (the index for the biggest packfile in
41
 * the repository), since bitmap indexes need full closure.
42
 *
43
 * If there is more than one bitmap index available (e.g. because of alternates),
44
 * the active bitmap index is the largest one.
45
 */
46
struct bitmap_index {
47
  /*
48
   * The pack or multi-pack index (MIDX) that this bitmap index belongs
49
   * to.
50
   *
51
   * Exactly one of these must be non-NULL; this specifies the object
52
   * order used to interpret this bitmap.
53
   */
54
  struct packed_git *pack;
55
  struct multi_pack_index *midx;
56
57
  /*
58
   * If using a multi-pack index chain, 'base' points to the
59
   * bitmap index corresponding to this bitmap's midx->base_midx.
60
   *
61
   * base_nr indicates how many layers precede this one, and is
62
   * zero when base is NULL.
63
   */
64
  struct bitmap_index *base;
65
  uint32_t base_nr;
66
67
  /* mmapped buffer of the whole bitmap index */
68
  unsigned char *map;
69
  size_t map_size; /* size of the mmaped buffer */
70
  size_t map_pos; /* current position when loading the index */
71
72
  /*
73
   * Type indexes.
74
   *
75
   * Each bitmap marks which objects in the packfile  are of the given
76
   * type. This provides type information when yielding the objects from
77
   * the packfile during a walk, which allows for better delta bases.
78
   */
79
  struct ewah_bitmap *commits;
80
  struct ewah_bitmap *trees;
81
  struct ewah_bitmap *blobs;
82
  struct ewah_bitmap *tags;
83
84
  /*
85
   * Type index arrays when this bitmap is associated with an
86
   * incremental multi-pack index chain.
87
   *
88
   * If n is the number of unique layers in the MIDX chain, then
89
   * commits_all[n-1] is this structs 'commits' field,
90
   * commits_all[n-2] is the commits field of this bitmap's
91
   * 'base', and so on.
92
   *
93
   * When associated either with a non-incremental MIDX or a
94
   * single packfile, these arrays each contain a single element.
95
   */
96
  struct ewah_bitmap **commits_all;
97
  struct ewah_bitmap **trees_all;
98
  struct ewah_bitmap **blobs_all;
99
  struct ewah_bitmap **tags_all;
100
101
  /* Map from object ID -> `stored_bitmap` for all the bitmapped commits */
102
  kh_oid_map_t *bitmaps;
103
104
  /* Number of bitmapped commits */
105
  uint32_t entry_count;
106
107
  /* If not NULL, this is a name-hash cache pointing into map. */
108
  uint32_t *hashes;
109
110
  /* The checksum of the packfile or MIDX; points into map. */
111
  const unsigned char *checksum;
112
113
  /*
114
   * If not NULL, this point into the commit table extension
115
   * (within the memory mapped region `map`).
116
   */
117
  unsigned char *table_lookup;
118
119
  /* This contains the pseudo-merge cache within 'map' (if found). */
120
  struct pseudo_merge_map pseudo_merges;
121
122
  /*
123
   * Extended index.
124
   *
125
   * When trying to perform bitmap operations with objects that are not
126
   * packed in `pack`, these objects are added to this "fake index" and
127
   * are assumed to appear at the end of the packfile for all operations
128
   */
129
  struct eindex {
130
    struct object **objects;
131
    uint32_t *hashes;
132
    uint32_t count, alloc;
133
    kh_oid_pos_t *positions;
134
  } ext_index;
135
136
  /* Bitmap result of the last performed walk */
137
  struct bitmap *result;
138
139
  /* "have" bitmap from the last performed walk */
140
  struct bitmap *haves;
141
142
  /* Version of the bitmap index */
143
  unsigned int version;
144
};
145
146
static int pseudo_merges_satisfied_nr;
147
static int pseudo_merges_cascades_nr;
148
static int existing_bitmaps_hits_nr;
149
static int existing_bitmaps_misses_nr;
150
static int roots_with_bitmaps_nr;
151
static int roots_without_bitmaps_nr;
152
153
static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)
154
0
{
155
0
  struct ewah_bitmap *parent;
156
0
  struct ewah_bitmap *composed;
157
158
0
  if (!st->xor)
159
0
    return st->root;
160
161
0
  composed = ewah_pool_new();
162
0
  parent = lookup_stored_bitmap(st->xor);
163
0
  ewah_xor(st->root, parent, composed);
164
165
0
  ewah_pool_free(st->root);
166
0
  st->root = composed;
167
0
  st->xor = NULL;
168
169
0
  return composed;
170
0
}
171
172
struct ewah_bitmap *read_bitmap(const unsigned char *map,
173
        size_t map_size, size_t *map_pos)
174
0
{
175
0
  struct ewah_bitmap *b = ewah_pool_new();
176
177
0
  ssize_t bitmap_size = ewah_read_mmap(b, map + *map_pos,
178
0
               map_size - *map_pos);
179
180
0
  if (bitmap_size < 0) {
181
0
    error(_("failed to load bitmap index (corrupted?)"));
182
0
    ewah_pool_free(b);
183
0
    return NULL;
184
0
  }
185
186
0
  *map_pos += bitmap_size;
187
188
0
  return b;
189
0
}
190
191
/*
192
 * Read a bitmap from the current read position on the mmaped
193
 * index, and increase the read position accordingly
194
 */
195
static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)
196
0
{
197
0
  return read_bitmap(index->map, index->map_size, &index->map_pos);
198
0
}
199
200
static uint32_t bitmap_num_objects_total(struct bitmap_index *index)
201
0
{
202
0
  if (index->midx) {
203
0
    struct multi_pack_index *m = index->midx;
204
0
    return m->num_objects + m->num_objects_in_base;
205
0
  }
206
0
  return index->pack->num_objects;
207
0
}
208
209
static uint32_t bitmap_num_objects(struct bitmap_index *index)
210
0
{
211
0
  if (index->midx)
212
0
    return index->midx->num_objects;
213
0
  return index->pack->num_objects;
214
0
}
215
216
static uint32_t bitmap_name_hash(struct bitmap_index *index, uint32_t pos)
217
0
{
218
0
  if (bitmap_is_midx(index)) {
219
0
    while (index && pos < index->midx->num_objects_in_base) {
220
0
      ASSERT(bitmap_is_midx(index));
221
0
      index = index->base;
222
0
    }
223
224
0
    if (!index)
225
0
      BUG("NULL base bitmap for object position: %"PRIu32, pos);
226
227
0
    pos -= index->midx->num_objects_in_base;
228
0
    if (pos >= index->midx->num_objects)
229
0
      BUG("out-of-bounds midx bitmap object at %"PRIu32, pos);
230
0
  }
231
232
0
  if (!index->hashes)
233
0
    return 0;
234
235
0
  return get_be32(index->hashes + pos);
236
0
}
237
238
static struct repository *bitmap_repo(struct bitmap_index *bitmap_git)
239
0
{
240
0
  if (bitmap_is_midx(bitmap_git))
241
0
    return bitmap_git->midx->source->base.odb->repo;
242
0
  return bitmap_git->pack->repo;
243
0
}
244
245
static int load_bitmap_header(struct bitmap_index *index)
246
0
{
247
0
  struct bitmap_disk_header *header = (void *)index->map;
248
0
  const struct git_hash_algo *hash_algo = bitmap_repo(index)->hash_algo;
249
250
0
  size_t header_size = sizeof(*header) - GIT_MAX_RAWSZ + hash_algo->rawsz;
251
252
0
  if (index->map_size < header_size + hash_algo->rawsz)
253
0
    return error(_("corrupted bitmap index (too small)"));
254
255
0
  if (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)
256
0
    return error(_("corrupted bitmap index file (wrong header)"));
257
258
0
  index->version = ntohs(header->version);
259
0
  if (index->version != 1)
260
0
    return error(_("unsupported version '%d' for bitmap index file"), index->version);
261
262
  /* Parse known bitmap format options */
263
0
  {
264
0
    uint32_t flags = ntohs(header->options);
265
0
    size_t cache_size = st_mult(bitmap_num_objects(index), sizeof(uint32_t));
266
0
    unsigned char *index_end = index->map + index->map_size - hash_algo->rawsz;
267
268
0
    if ((flags & BITMAP_OPT_FULL_DAG) == 0)
269
0
      BUG("unsupported options for bitmap index file "
270
0
        "(Git requires BITMAP_OPT_FULL_DAG)");
271
272
0
    if (flags & BITMAP_OPT_HASH_CACHE) {
273
0
      if (cache_size > index_end - index->map - header_size)
274
0
        return error(_("corrupted bitmap index file (too short to fit hash cache)"));
275
0
      index->hashes = (void *)(index_end - cache_size);
276
0
      index_end -= cache_size;
277
0
    }
278
279
0
    if (flags & BITMAP_OPT_LOOKUP_TABLE) {
280
0
      size_t table_size = st_mult(ntohl(header->entry_count),
281
0
                BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
282
0
      if (table_size > index_end - index->map - header_size)
283
0
        return error(_("corrupted bitmap index file (too short to fit lookup table)"));
284
0
      if (git_env_bool("GIT_TEST_READ_COMMIT_TABLE", 1))
285
0
        index->table_lookup = (void *)(index_end - table_size);
286
0
      index_end -= table_size;
287
0
    }
288
289
0
    if (flags & BITMAP_OPT_PSEUDO_MERGES) {
290
0
      unsigned char *pseudo_merge_ofs;
291
0
      size_t table_size;
292
0
      uint32_t i;
293
294
0
      if (sizeof(table_size) > index_end - index->map - header_size)
295
0
        return error(_("corrupted bitmap index file (too short to fit pseudo-merge table header)"));
296
297
0
      table_size = get_be64(index_end - 8);
298
0
      if (table_size > index_end - index->map - header_size)
299
0
        return error(_("corrupted bitmap index file (too short to fit pseudo-merge table)"));
300
301
0
      if (git_env_bool("GIT_TEST_USE_PSEUDO_MERGES", 1)) {
302
0
        const unsigned char *ext = (index_end - table_size);
303
304
0
        index->pseudo_merges.map = index->map;
305
0
        index->pseudo_merges.map_size = index->map_size;
306
0
        index->pseudo_merges.commits = ext + get_be64(index_end - 16);
307
0
        index->pseudo_merges.commits_nr = get_be32(index_end - 20);
308
0
        index->pseudo_merges.nr = get_be32(index_end - 24);
309
310
0
        if (st_add(st_mult(index->pseudo_merges.nr,
311
0
               sizeof(uint64_t)),
312
0
             24) > table_size)
313
0
          return error(_("corrupted bitmap index file, pseudo-merge table too short"));
314
315
0
        CALLOC_ARRAY(index->pseudo_merges.v,
316
0
               index->pseudo_merges.nr);
317
318
0
        pseudo_merge_ofs = index_end - 24 -
319
0
          (index->pseudo_merges.nr * sizeof(uint64_t));
320
0
        for (i = 0; i < index->pseudo_merges.nr; i++) {
321
0
          index->pseudo_merges.v[i].at = get_be64(pseudo_merge_ofs);
322
0
          pseudo_merge_ofs += sizeof(uint64_t);
323
0
        }
324
0
      }
325
326
0
      index_end -= table_size;
327
0
    }
328
0
  }
329
330
0
  index->entry_count = ntohl(header->entry_count);
331
0
  index->checksum = header->checksum;
332
0
  index->map_pos += header_size;
333
0
  return 0;
334
0
}
335
336
static struct stored_bitmap *store_bitmap(struct bitmap_index *index,
337
            struct ewah_bitmap *root,
338
            const struct object_id *oid,
339
            struct stored_bitmap *xor_with,
340
            int flags, size_t map_pos)
341
0
{
342
0
  struct stored_bitmap *stored;
343
0
  khiter_t hash_pos;
344
0
  int ret;
345
346
0
  stored = xmalloc(sizeof(struct stored_bitmap));
347
0
  stored->map_pos = map_pos;
348
0
  stored->root = root;
349
0
  stored->xor = xor_with;
350
0
  stored->flags = flags;
351
0
  oidcpy(&stored->oid, oid);
352
353
0
  hash_pos = kh_put_oid_map(index->bitmaps, stored->oid, &ret);
354
355
  /*
356
   * A 0 return code means the insertion succeeded with no changes,
357
   * because the SHA1 already existed on the map. This is bad, there
358
   * shouldn't be duplicated commits in the index.
359
   */
360
0
  if (ret == 0) {
361
0
    error(_("duplicate entry in bitmap index: '%s'"), oid_to_hex(oid));
362
0
    return NULL;
363
0
  }
364
365
0
  kh_value(index->bitmaps, hash_pos) = stored;
366
0
  return stored;
367
0
}
368
369
static inline uint32_t read_be32(const unsigned char *buffer, size_t *pos)
370
0
{
371
0
  uint32_t result = get_be32(buffer + *pos);
372
0
  (*pos) += sizeof(result);
373
0
  return result;
374
0
}
375
376
static inline uint8_t read_u8(const unsigned char *buffer, size_t *pos)
377
0
{
378
0
  return buffer[(*pos)++];
379
0
}
380
381
0
#define MAX_XOR_OFFSET 160
382
383
static int nth_bitmap_object_oid(struct bitmap_index *index,
384
         struct object_id *oid,
385
         uint32_t n)
386
0
{
387
0
  if (index->midx)
388
0
    return nth_midxed_object_oid(oid, index->midx, n) ? 0 : -1;
389
0
  return nth_packed_object_id(oid, index->pack, n);
390
0
}
391
392
static int load_bitmap_entries_v1(struct bitmap_index *index)
393
0
{
394
0
  uint32_t i;
395
0
  struct stored_bitmap *recent_bitmaps[MAX_XOR_OFFSET] = { NULL };
396
397
0
  for (i = 0; i < index->entry_count; ++i) {
398
0
    int xor_offset, flags;
399
0
    struct ewah_bitmap *bitmap = NULL;
400
0
    struct stored_bitmap *xor_bitmap = NULL;
401
0
    uint32_t commit_idx_pos;
402
0
    struct object_id oid;
403
0
    size_t entry_map_pos;
404
405
0
    if (index->map_size - index->map_pos < 6)
406
0
      return error(_("corrupt ewah bitmap: truncated header for entry %d"), i);
407
408
0
    entry_map_pos = index->map_pos;
409
0
    commit_idx_pos = read_be32(index->map, &index->map_pos);
410
0
    xor_offset = read_u8(index->map, &index->map_pos);
411
0
    flags = read_u8(index->map, &index->map_pos);
412
413
0
    if (nth_bitmap_object_oid(index, &oid, commit_idx_pos) < 0)
414
0
      return error(_("corrupt ewah bitmap: commit index %u out of range"),
415
0
             (unsigned)commit_idx_pos);
416
417
0
    if (xor_offset > MAX_XOR_OFFSET || xor_offset > i)
418
0
      return error(_("corrupted bitmap pack index"));
419
420
0
    if (xor_offset > 0) {
421
0
      xor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];
422
423
0
      if (!xor_bitmap)
424
0
        return error(_("invalid XOR offset in bitmap pack index"));
425
0
    }
426
427
0
    bitmap = read_bitmap_1(index);
428
0
    if (!bitmap)
429
0
      return -1;
430
431
0
    recent_bitmaps[i % MAX_XOR_OFFSET] =
432
0
      store_bitmap(index, bitmap, &oid, xor_bitmap, flags,
433
0
             entry_map_pos);
434
0
  }
435
436
0
  return 0;
437
0
}
438
439
char *midx_bitmap_filename(struct multi_pack_index *midx)
440
0
{
441
0
  struct strbuf buf = STRBUF_INIT;
442
0
  if (midx->has_chain)
443
0
    get_split_midx_filename_ext(midx->source, &buf,
444
0
              midx_get_checksum_hash(midx),
445
0
              MIDX_EXT_BITMAP);
446
0
  else
447
0
    get_midx_filename_ext(midx->source, &buf,
448
0
              midx_get_checksum_hash(midx),
449
0
              MIDX_EXT_BITMAP);
450
451
0
  return strbuf_detach(&buf, NULL);
452
0
}
453
454
char *pack_bitmap_filename(struct packed_git *p)
455
0
{
456
0
  size_t len;
457
458
0
  if (!strip_suffix(p->pack_name, ".pack", &len))
459
0
    BUG("pack_name does not end in .pack");
460
0
  return xstrfmt("%.*s.bitmap", (int)len, p->pack_name);
461
0
}
462
463
static int open_midx_bitmap(struct bitmap_index *bitmap_git,
464
          struct multi_pack_index *midx)
465
0
{
466
0
  struct stat st;
467
0
  char *bitmap_name = midx_bitmap_filename(midx);
468
0
  int fd = git_open(bitmap_name);
469
0
  uint32_t i;
470
471
0
  if (fd < 0) {
472
0
    if (errno != ENOENT)
473
0
      warning_errno("cannot open '%s'", bitmap_name);
474
0
    free(bitmap_name);
475
0
    return -1;
476
0
  }
477
0
  free(bitmap_name);
478
479
0
  if (fstat(fd, &st)) {
480
0
    error_errno(_("cannot fstat bitmap file"));
481
0
    close(fd);
482
0
    return -1;
483
0
  }
484
485
0
  if (bitmap_git->pack || bitmap_git->midx) {
486
0
    struct strbuf buf = STRBUF_INIT;
487
0
    get_midx_filename(midx->source, &buf);
488
0
    trace2_data_string("bitmap", bitmap_repo(bitmap_git),
489
0
           "ignoring extra midx bitmap file", buf.buf);
490
0
    close(fd);
491
0
    strbuf_release(&buf);
492
0
    return -1;
493
0
  }
494
495
0
  bitmap_git->midx = midx;
496
0
  bitmap_git->map_size = xsize_t(st.st_size);
497
0
  bitmap_git->map_pos = 0;
498
0
  bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ,
499
0
        MAP_PRIVATE, fd, 0);
500
0
  close(fd);
501
502
0
  if (load_bitmap_header(bitmap_git) < 0)
503
0
    goto cleanup;
504
505
0
  if (!hasheq(midx_get_checksum_hash(bitmap_git->midx), bitmap_git->checksum,
506
0
        bitmap_repo(bitmap_git)->hash_algo)) {
507
0
    error(_("checksum doesn't match in MIDX and bitmap"));
508
0
    goto cleanup;
509
0
  }
510
511
0
  if (load_midx_revindex(bitmap_git->midx)) {
512
0
    warning(_("multi-pack bitmap is missing required reverse index"));
513
0
    goto cleanup;
514
0
  }
515
516
0
  for (i = 0; i < bitmap_git->midx->num_packs + bitmap_git->midx->num_packs_in_base; i++) {
517
0
    if (prepare_midx_pack(bitmap_git->midx, i)) {
518
0
      warning(_("could not open pack %s"),
519
0
        bitmap_git->midx->pack_names[i]);
520
0
      goto cleanup;
521
0
    }
522
0
  }
523
524
0
  if (midx->base_midx) {
525
0
    bitmap_git->base = prepare_midx_bitmap_git(midx->base_midx);
526
0
    if (!bitmap_git->base) {
527
0
      warning(_("could not open bitmap for base MIDX"));
528
0
      goto cleanup;
529
0
    }
530
0
    bitmap_git->base_nr = bitmap_git->base->base_nr + 1;
531
0
  } else {
532
0
    bitmap_git->base_nr = 0;
533
0
  }
534
535
0
  return 0;
536
537
0
cleanup:
538
0
  munmap(bitmap_git->map, bitmap_git->map_size);
539
0
  bitmap_git->map_size = 0;
540
0
  bitmap_git->map_pos = 0;
541
0
  bitmap_git->map = NULL;
542
0
  bitmap_git->midx = NULL;
543
0
  return -1;
544
0
}
545
546
static int open_pack_bitmap(struct bitmap_index *bitmap_git, struct packed_git *packfile)
547
0
{
548
0
  int fd;
549
0
  struct stat st;
550
0
  char *bitmap_name;
551
552
0
  bitmap_name = pack_bitmap_filename(packfile);
553
0
  fd = git_open(bitmap_name);
554
555
0
  if (fd < 0) {
556
0
    if (errno != ENOENT)
557
0
      warning_errno("cannot open '%s'", bitmap_name);
558
0
    free(bitmap_name);
559
0
    return -1;
560
0
  }
561
0
  free(bitmap_name);
562
563
0
  if (fstat(fd, &st)) {
564
0
    error_errno(_("cannot fstat bitmap file"));
565
0
    close(fd);
566
0
    return -1;
567
0
  }
568
569
0
  if (bitmap_git->pack || bitmap_git->midx) {
570
0
    trace2_data_string("bitmap", bitmap_repo(bitmap_git),
571
0
           "ignoring extra bitmap file",
572
0
           packfile->pack_name);
573
0
    close(fd);
574
0
    return -1;
575
0
  }
576
577
0
  if (!is_pack_valid(packfile)) {
578
0
    close(fd);
579
0
    return -1;
580
0
  }
581
582
0
  bitmap_git->pack = packfile;
583
0
  bitmap_git->map_size = xsize_t(st.st_size);
584
0
  bitmap_git->map = xmmap(NULL, bitmap_git->map_size, PROT_READ, MAP_PRIVATE, fd, 0);
585
0
  bitmap_git->map_pos = 0;
586
0
  bitmap_git->base_nr = 0;
587
0
  close(fd);
588
589
0
  if (load_bitmap_header(bitmap_git) < 0) {
590
0
    munmap(bitmap_git->map, bitmap_git->map_size);
591
0
    bitmap_git->map = NULL;
592
0
    bitmap_git->map_size = 0;
593
0
    bitmap_git->map_pos = 0;
594
0
    bitmap_git->pack = NULL;
595
0
    return -1;
596
0
  }
597
598
0
  trace2_data_string("bitmap", bitmap_repo(bitmap_git),
599
0
         "opened bitmap file", packfile->pack_name);
600
0
  return 0;
601
0
}
602
603
static int load_reverse_index(struct repository *r, struct bitmap_index *bitmap_git)
604
0
{
605
0
  if (bitmap_is_midx(bitmap_git)) {
606
0
    struct multi_pack_index *m;
607
608
    /*
609
     * The multi-pack-index's .rev file is already loaded via
610
     * open_pack_bitmap().
611
     *
612
     * But we still need to open the individual pack .rev files,
613
     * since we will need to make use of them in pack-objects.
614
     */
615
0
    for (m = bitmap_git->midx; m; m = m->base_midx) {
616
0
      uint32_t i;
617
0
      int ret;
618
619
0
      for (i = 0; i < m->num_packs; i++) {
620
0
        ret = load_pack_revindex(r, m->packs[i]);
621
0
        if (ret)
622
0
          return ret;
623
0
      }
624
0
    }
625
0
    return 0;
626
0
  }
627
0
  return load_pack_revindex(r, bitmap_git->pack);
628
0
}
629
630
static void load_all_type_bitmaps(struct bitmap_index *bitmap_git)
631
0
{
632
0
  struct bitmap_index *curr = bitmap_git;
633
0
  size_t i = bitmap_git->base_nr;
634
635
0
  ALLOC_ARRAY(bitmap_git->commits_all, bitmap_git->base_nr + 1);
636
0
  ALLOC_ARRAY(bitmap_git->trees_all, bitmap_git->base_nr + 1);
637
0
  ALLOC_ARRAY(bitmap_git->blobs_all, bitmap_git->base_nr + 1);
638
0
  ALLOC_ARRAY(bitmap_git->tags_all, bitmap_git->base_nr + 1);
639
640
0
  while (curr) {
641
0
    bitmap_git->commits_all[i] = curr->commits;
642
0
    bitmap_git->trees_all[i] = curr->trees;
643
0
    bitmap_git->blobs_all[i] = curr->blobs;
644
0
    bitmap_git->tags_all[i] = curr->tags;
645
646
0
    curr = curr->base;
647
0
    if (curr && !i)
648
0
      BUG("unexpected number of bitmap layers, expected %"PRIu32,
649
0
          bitmap_git->base_nr + 1);
650
0
    i -= 1;
651
0
  }
652
0
}
653
654
static int load_bitmap(struct repository *r, struct bitmap_index *bitmap_git,
655
           int recursing)
656
0
{
657
0
  assert(bitmap_git->map);
658
659
0
  bitmap_git->bitmaps = kh_init_oid_map();
660
0
  bitmap_git->ext_index.positions = kh_init_oid_pos();
661
662
0
  if (load_reverse_index(r, bitmap_git))
663
0
    return -1;
664
665
0
  if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
666
0
    !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
667
0
    !(bitmap_git->blobs = read_bitmap_1(bitmap_git)) ||
668
0
    !(bitmap_git->tags = read_bitmap_1(bitmap_git)))
669
0
    return -1;
670
671
0
  if (!bitmap_git->table_lookup && load_bitmap_entries_v1(bitmap_git) < 0)
672
0
    return -1;
673
674
0
  if (bitmap_git->base) {
675
0
    if (!bitmap_is_midx(bitmap_git))
676
0
      BUG("non-MIDX bitmap has non-NULL base bitmap index");
677
0
    if (load_bitmap(r, bitmap_git->base, 1) < 0)
678
0
      return -1;
679
0
  }
680
681
0
  if (!recursing)
682
0
    load_all_type_bitmaps(bitmap_git);
683
684
0
  return 0;
685
0
}
686
687
static int open_bitmap_for_source(struct odb_source_packed *source,
688
          struct bitmap_index *bitmap_git)
689
0
{
690
0
  struct multi_pack_index *midx = get_multi_pack_index(source);
691
0
  struct packfile_list_entry *e;
692
0
  bool found = false;
693
694
0
  if (midx && !open_midx_bitmap(bitmap_git, midx))
695
0
    found = true;
696
697
0
  for (e = packfile_store_get_packs(source); e; e = e->next) {
698
    /*
699
     * When tracing is enabled we want to keep looking to report
700
     * duplicates even if we have already found a bitmap.
701
     */
702
0
    if (found && !trace2_is_enabled())
703
0
      break;
704
705
0
    if (!open_pack_bitmap(bitmap_git, e->pack))
706
0
      found = true;
707
0
  }
708
709
0
  return found ? 0 : -1;
710
0
}
711
712
static int open_bitmap(struct repository *r,
713
           struct bitmap_index *bitmap_git)
714
0
{
715
0
  struct odb_source_files *files = odb_source_files_downcast(r->objects->source);
716
0
  bool found = false;
717
718
0
  assert(!bitmap_git->map);
719
720
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
721
0
    if (!open_bitmap_for_source(dir->packed, bitmap_git))
722
0
      found = true;
723
724
    /*
725
     * The only reason to keep looking after having found a bitmap
726
     * is to report duplicates.
727
     */
728
0
    if (found && !trace2_is_enabled())
729
0
      break;
730
0
  }
731
732
0
  return found ? 0 : -1;
733
0
}
734
735
struct bitmap_index *prepare_bitmap_git(struct repository *r)
736
0
{
737
0
  struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
738
739
0
  if (!open_bitmap(r, bitmap_git) && !load_bitmap(r, bitmap_git, 0))
740
0
    return bitmap_git;
741
742
0
  free_bitmap_index(bitmap_git);
743
0
  return NULL;
744
0
}
745
746
struct bitmap_index *prepare_midx_bitmap_git(struct multi_pack_index *midx)
747
0
{
748
0
  struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
749
750
0
  if (!open_midx_bitmap(bitmap_git, midx))
751
0
    return bitmap_git;
752
753
0
  free_bitmap_index(bitmap_git);
754
0
  return NULL;
755
0
}
756
757
struct bitmap_index *prepare_bitmap_git_for_source(struct odb_source_packed *source)
758
0
{
759
0
  struct bitmap_index *bitmap_git = xcalloc(1, sizeof(*bitmap_git));
760
761
0
  if (!open_bitmap_for_source(source, bitmap_git) &&
762
0
      !load_bitmap(source->base.odb->repo, bitmap_git, 0))
763
0
    return bitmap_git;
764
765
0
  free_bitmap_index(bitmap_git);
766
0
  return NULL;
767
0
}
768
769
int bitmap_index_contains_pack(struct bitmap_index *bitmap, struct packed_git *pack)
770
0
{
771
0
  for (; bitmap; bitmap = bitmap->base) {
772
0
    if (bitmap_is_midx(bitmap)) {
773
0
      for (size_t i = 0; i < bitmap->midx->num_packs; i++)
774
0
        if (bitmap->midx->packs[i] == pack)
775
0
          return 1;
776
0
    } else if (bitmap->pack == pack) {
777
0
      return 1;
778
0
    }
779
0
  }
780
781
0
  return 0;
782
0
}
783
784
struct include_data {
785
  struct bitmap_index *bitmap_git;
786
  struct bitmap *base;
787
  struct bitmap *seen;
788
};
789
790
struct bitmap_lookup_table_triplet {
791
  uint32_t commit_pos;
792
  uint64_t offset;
793
  uint32_t xor_row;
794
};
795
796
struct bitmap_lookup_table_xor_item {
797
  struct object_id oid;
798
  uint64_t offset;
799
};
800
801
/*
802
 * Given a `triplet` struct pointer and pointer `p`, this
803
 * function reads the triplet beginning at `p` into the struct.
804
 * Note that this function assumes that there is enough memory
805
 * left for filling the `triplet` struct from `p`.
806
 */
807
static int bitmap_lookup_table_get_triplet_by_pointer(struct bitmap_lookup_table_triplet *triplet,
808
                  const unsigned char *p)
809
0
{
810
0
  if (!triplet)
811
0
    return -1;
812
813
0
  triplet->commit_pos = get_be32(p);
814
0
  p += sizeof(uint32_t);
815
0
  triplet->offset = get_be64(p);
816
0
  p += sizeof(uint64_t);
817
0
  triplet->xor_row = get_be32(p);
818
0
  return 0;
819
0
}
820
821
/*
822
 * This function gets the raw triplet from `row`'th row in the
823
 * lookup table and fills that data to the `triplet`.
824
 */
825
static int bitmap_lookup_table_get_triplet(struct bitmap_index *bitmap_git,
826
             uint32_t pos,
827
             struct bitmap_lookup_table_triplet *triplet)
828
0
{
829
0
  unsigned char *p = NULL;
830
0
  if (pos >= bitmap_git->entry_count)
831
0
    return error(_("corrupt bitmap lookup table: triplet position out of index"));
832
833
0
  p = bitmap_git->table_lookup + st_mult(pos, BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH);
834
835
0
  return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
836
0
}
837
838
/*
839
 * Searches for a matching triplet. `commit_pos` is a pointer
840
 * to the wanted commit position value. `table_entry` points to
841
 * a triplet in lookup table. The first 4 bytes of each
842
 * triplet (pointed by `table_entry`) are compared with `*commit_pos`.
843
 */
844
static int triplet_cmp(const void *commit_pos, const void *table_entry)
845
0
{
846
847
0
  uint32_t a = *(uint32_t *)commit_pos;
848
0
  uint32_t b = get_be32(table_entry);
849
0
  if (a > b)
850
0
    return 1;
851
0
  else if (a < b)
852
0
    return -1;
853
854
0
  return 0;
855
0
}
856
857
static uint32_t bitmap_bsearch_pos(struct bitmap_index *bitmap_git,
858
          struct object_id *oid,
859
          uint32_t *result)
860
0
{
861
0
  int found;
862
863
0
  if (bitmap_is_midx(bitmap_git))
864
0
    found = bsearch_midx(oid, bitmap_git->midx, result);
865
0
  else
866
0
    found = bsearch_pack(oid, bitmap_git->pack, result);
867
868
0
  return found;
869
0
}
870
871
/*
872
 * `bsearch_triplet_by_pos` function searches for the raw triplet
873
 * having commit position same as `commit_pos` and fills `triplet`
874
 * object from the raw triplet. Returns 1 on success and 0 on
875
 * failure.
876
 */
877
static int bitmap_bsearch_triplet_by_pos(uint32_t commit_pos,
878
          struct bitmap_index *bitmap_git,
879
          struct bitmap_lookup_table_triplet *triplet)
880
0
{
881
0
  unsigned char *p = bsearch(&commit_pos, bitmap_git->table_lookup, bitmap_git->entry_count,
882
0
           BITMAP_LOOKUP_TABLE_TRIPLET_WIDTH, triplet_cmp);
883
884
0
  if (!p)
885
0
    return -1;
886
887
0
  return bitmap_lookup_table_get_triplet_by_pointer(triplet, p);
888
0
}
889
890
static struct stored_bitmap *lazy_bitmap_for_commit(struct bitmap_index *bitmap_git,
891
                struct commit *commit)
892
0
{
893
0
  uint32_t commit_pos, xor_row;
894
0
  uint64_t offset;
895
0
  int flags;
896
0
  struct bitmap_lookup_table_triplet triplet;
897
0
  struct object_id *oid = &commit->object.oid;
898
0
  struct ewah_bitmap *bitmap;
899
0
  struct stored_bitmap *xor_bitmap = NULL;
900
0
  const int bitmap_header_size = 6;
901
0
  static struct bitmap_lookup_table_xor_item *xor_items = NULL;
902
0
  static size_t xor_items_nr = 0, xor_items_alloc = 0;
903
0
  static int is_corrupt = 0;
904
0
  int xor_flags;
905
0
  khiter_t hash_pos;
906
0
  struct bitmap_lookup_table_xor_item *xor_item;
907
0
  size_t entry_map_pos;
908
909
0
  if (is_corrupt)
910
0
    return NULL;
911
912
0
  if (!bitmap_bsearch_pos(bitmap_git, oid, &commit_pos))
913
0
    return NULL;
914
915
0
  if (bitmap_bsearch_triplet_by_pos(commit_pos, bitmap_git, &triplet) < 0)
916
0
    return NULL;
917
918
0
  xor_items_nr = 0;
919
0
  offset = triplet.offset;
920
0
  xor_row = triplet.xor_row;
921
922
0
  while (xor_row != 0xffffffff) {
923
0
    ALLOC_GROW(xor_items, xor_items_nr + 1, xor_items_alloc);
924
925
0
    if (xor_items_nr + 1 >= bitmap_git->entry_count) {
926
0
      error(_("corrupt bitmap lookup table: xor chain exceeds entry count"));
927
0
      goto corrupt;
928
0
    }
929
930
0
    if (bitmap_lookup_table_get_triplet(bitmap_git, xor_row, &triplet) < 0)
931
0
      goto corrupt;
932
933
0
    xor_item = &xor_items[xor_items_nr];
934
0
    xor_item->offset = triplet.offset;
935
936
0
    if (nth_bitmap_object_oid(bitmap_git, &xor_item->oid, triplet.commit_pos) < 0) {
937
0
      error(_("corrupt bitmap lookup table: commit index %u out of range"),
938
0
        triplet.commit_pos);
939
0
      goto corrupt;
940
0
    }
941
942
0
    hash_pos = kh_get_oid_map(bitmap_git->bitmaps, xor_item->oid);
943
944
    /*
945
     * If desired bitmap is already stored, we don't need
946
     * to iterate further. Because we know that bitmaps
947
     * that are needed to be parsed to parse this bitmap
948
     * has already been stored. So, assign this stored bitmap
949
     * to the xor_bitmap.
950
     */
951
0
    if (hash_pos < kh_end(bitmap_git->bitmaps) &&
952
0
      (xor_bitmap = kh_value(bitmap_git->bitmaps, hash_pos)))
953
0
      break;
954
0
    xor_items_nr++;
955
0
    xor_row = triplet.xor_row;
956
0
  }
957
958
0
  while (xor_items_nr) {
959
0
    xor_item = &xor_items[xor_items_nr - 1];
960
0
    bitmap_git->map_pos = xor_item->offset;
961
0
    if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
962
0
      error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
963
0
        oid_to_hex(&xor_item->oid));
964
0
      goto corrupt;
965
0
    }
966
967
0
    entry_map_pos = bitmap_git->map_pos;
968
0
    bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
969
0
    xor_flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
970
0
    bitmap = read_bitmap_1(bitmap_git);
971
972
0
    if (!bitmap)
973
0
      goto corrupt;
974
975
0
    xor_bitmap = store_bitmap(bitmap_git, bitmap, &xor_item->oid,
976
0
            xor_bitmap, xor_flags, entry_map_pos);
977
0
    xor_items_nr--;
978
0
  }
979
980
0
  bitmap_git->map_pos = offset;
981
0
  if (bitmap_git->map_size - bitmap_git->map_pos < bitmap_header_size) {
982
0
    error(_("corrupt ewah bitmap: truncated header for bitmap of commit \"%s\""),
983
0
      oid_to_hex(oid));
984
0
    goto corrupt;
985
0
  }
986
987
  /*
988
   * Don't bother reading the commit's index position or its xor
989
   * offset:
990
   *
991
   *   - The commit's index position is irrelevant to us, since
992
   *     load_bitmap_entries_v1 only uses it to learn the object
993
   *     id which is used to compute the hashmap's key. We already
994
   *     have an object id, so no need to look it up again.
995
   *
996
   *   - The xor_offset is unusable for us, since it specifies how
997
   *     many entries previous to ours we should look at. This
998
   *     makes sense when reading the bitmaps sequentially (as in
999
   *     load_bitmap_entries_v1()), since we can keep track of
1000
   *     each bitmap as we read them.
1001
   *
1002
   *     But it can't work for us, since the bitmap's don't have a
1003
   *     fixed size. So we learn the position of the xor'd bitmap
1004
   *     from the commit table (and resolve it to a bitmap in the
1005
   *     above if-statement).
1006
   *
1007
   * Instead, we can skip ahead and immediately read the flags and
1008
   * ewah bitmap.
1009
   */
1010
0
  entry_map_pos = bitmap_git->map_pos;
1011
0
  bitmap_git->map_pos += sizeof(uint32_t) + sizeof(uint8_t);
1012
0
  flags = read_u8(bitmap_git->map, &bitmap_git->map_pos);
1013
0
  bitmap = read_bitmap_1(bitmap_git);
1014
1015
0
  if (!bitmap)
1016
0
    goto corrupt;
1017
1018
0
  return store_bitmap(bitmap_git, bitmap, oid, xor_bitmap, flags,
1019
0
          entry_map_pos);
1020
1021
0
corrupt:
1022
0
  free(xor_items);
1023
0
  is_corrupt = 1;
1024
0
  return NULL;
1025
0
}
1026
1027
static struct ewah_bitmap *find_bitmap_for_commit(struct bitmap_index *bitmap_git,
1028
              struct commit *commit,
1029
              struct bitmap_index **found)
1030
0
{
1031
0
  khiter_t hash_pos;
1032
0
  if (!bitmap_git)
1033
0
    return NULL;
1034
1035
0
  hash_pos = kh_get_oid_map(bitmap_git->bitmaps, commit->object.oid);
1036
0
  if (hash_pos >= kh_end(bitmap_git->bitmaps)) {
1037
0
    struct stored_bitmap *bitmap = NULL;
1038
0
    if (!bitmap_git->table_lookup)
1039
0
      return find_bitmap_for_commit(bitmap_git->base, commit,
1040
0
                  found);
1041
1042
    /* this is a fairly hot codepath - no trace2_region please */
1043
    /* NEEDSWORK: cache misses aren't recorded */
1044
0
    bitmap = lazy_bitmap_for_commit(bitmap_git, commit);
1045
0
    if (!bitmap)
1046
0
      return find_bitmap_for_commit(bitmap_git->base, commit,
1047
0
                  found);
1048
0
    if (found)
1049
0
      *found = bitmap_git;
1050
0
    return lookup_stored_bitmap(bitmap);
1051
0
  }
1052
0
  if (found)
1053
0
    *found = bitmap_git;
1054
0
  return lookup_stored_bitmap(kh_value(bitmap_git->bitmaps, hash_pos));
1055
0
}
1056
1057
struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git,
1058
              struct commit *commit)
1059
0
{
1060
0
  return find_bitmap_for_commit(bitmap_git, commit, NULL);
1061
0
}
1062
1063
static inline int bitmap_position_extended(struct bitmap_index *bitmap_git,
1064
             const struct object_id *oid)
1065
0
{
1066
0
  kh_oid_pos_t *positions = bitmap_git->ext_index.positions;
1067
0
  khiter_t pos = kh_get_oid_pos(positions, *oid);
1068
1069
0
  if (pos < kh_end(positions)) {
1070
0
    int bitmap_pos = kh_value(positions, pos);
1071
0
    return bitmap_pos + bitmap_num_objects_total(bitmap_git);
1072
0
  }
1073
1074
0
  return -1;
1075
0
}
1076
1077
static inline int bitmap_position_packfile(struct bitmap_index *bitmap_git,
1078
             const struct object_id *oid)
1079
0
{
1080
0
  uint32_t pos;
1081
0
  off_t offset = find_pack_entry_one(oid, bitmap_git->pack);
1082
0
  if (!offset)
1083
0
    return -1;
1084
1085
0
  if (offset_to_pack_pos(bitmap_git->pack, offset, &pos) < 0)
1086
0
    return -1;
1087
0
  return pos;
1088
0
}
1089
1090
static int bitmap_position_midx(struct bitmap_index *bitmap_git,
1091
        const struct object_id *oid)
1092
0
{
1093
0
  uint32_t want, got;
1094
0
  if (!bsearch_midx(oid, bitmap_git->midx, &want))
1095
0
    return -1;
1096
1097
0
  if (midx_to_pack_pos(bitmap_git->midx, want, &got) < 0)
1098
0
    return -1;
1099
0
  return got;
1100
0
}
1101
1102
static int bitmap_position(struct bitmap_index *bitmap_git,
1103
         const struct object_id *oid)
1104
0
{
1105
0
  int pos;
1106
0
  if (bitmap_is_midx(bitmap_git))
1107
0
    pos = bitmap_position_midx(bitmap_git, oid);
1108
0
  else
1109
0
    pos = bitmap_position_packfile(bitmap_git, oid);
1110
0
  return (pos >= 0) ? pos : bitmap_position_extended(bitmap_git, oid);
1111
0
}
1112
1113
static int ext_index_add_object(struct bitmap_index *bitmap_git,
1114
        struct object *object, const char *name)
1115
0
{
1116
0
  struct eindex *eindex = &bitmap_git->ext_index;
1117
1118
0
  khiter_t hash_pos;
1119
0
  int hash_ret;
1120
0
  int bitmap_pos;
1121
1122
0
  hash_pos = kh_put_oid_pos(eindex->positions, object->oid, &hash_ret);
1123
0
  if (hash_ret > 0) {
1124
0
    if (eindex->count >= eindex->alloc) {
1125
0
      eindex->alloc = (eindex->alloc + 16) * 3 / 2;
1126
0
      REALLOC_ARRAY(eindex->objects, eindex->alloc);
1127
0
      REALLOC_ARRAY(eindex->hashes, eindex->alloc);
1128
0
    }
1129
1130
0
    bitmap_pos = eindex->count;
1131
0
    eindex->objects[eindex->count] = object;
1132
0
    eindex->hashes[eindex->count] = pack_name_hash(name);
1133
0
    kh_value(eindex->positions, hash_pos) = bitmap_pos;
1134
0
    eindex->count++;
1135
0
  } else {
1136
0
    bitmap_pos = kh_value(eindex->positions, hash_pos);
1137
0
  }
1138
1139
0
  return bitmap_pos + bitmap_num_objects_total(bitmap_git);
1140
0
}
1141
1142
struct bitmap_show_data {
1143
  struct bitmap_index *bitmap_git;
1144
  struct bitmap *base;
1145
};
1146
1147
static void show_object(struct object *object, const char *name, void *data_)
1148
0
{
1149
0
  struct bitmap_show_data *data = data_;
1150
0
  int bitmap_pos;
1151
1152
0
  bitmap_pos = bitmap_position(data->bitmap_git, &object->oid);
1153
1154
0
  if (bitmap_pos < 0)
1155
0
    bitmap_pos = ext_index_add_object(data->bitmap_git, object,
1156
0
              name);
1157
1158
0
  bitmap_set(data->base, bitmap_pos);
1159
0
}
1160
1161
static void show_commit(struct commit *commit UNUSED,
1162
      void *data UNUSED)
1163
0
{
1164
0
}
1165
1166
static unsigned apply_pseudo_merges_for_commit_1(struct bitmap_index *bitmap_git,
1167
             struct bitmap *result,
1168
             struct commit *commit,
1169
             uint32_t commit_pos)
1170
0
{
1171
0
  struct bitmap_index *curr = bitmap_git;
1172
0
  int ret = 0;
1173
1174
0
  while (curr) {
1175
0
    ret += apply_pseudo_merges_for_commit(&curr->pseudo_merges,
1176
0
                  result, commit,
1177
0
                  commit_pos);
1178
0
    curr = curr->base;
1179
0
  }
1180
1181
0
  if (ret)
1182
0
    pseudo_merges_satisfied_nr += ret;
1183
1184
0
  return ret;
1185
0
}
1186
1187
static int add_to_include_set(struct bitmap_index *bitmap_git,
1188
            struct include_data *data,
1189
            struct commit *commit,
1190
            int bitmap_pos)
1191
0
{
1192
0
  struct ewah_bitmap *partial;
1193
1194
0
  if (data->seen && bitmap_get(data->seen, bitmap_pos))
1195
0
    return 0;
1196
1197
0
  if (bitmap_get(data->base, bitmap_pos))
1198
0
    return 0;
1199
1200
0
  partial = bitmap_for_commit(bitmap_git, commit);
1201
0
  if (partial) {
1202
0
    existing_bitmaps_hits_nr++;
1203
1204
0
    bitmap_or_ewah(data->base, partial);
1205
0
    return 0;
1206
0
  }
1207
1208
0
  existing_bitmaps_misses_nr++;
1209
1210
0
  bitmap_set(data->base, bitmap_pos);
1211
0
  if (apply_pseudo_merges_for_commit_1(bitmap_git, data->base, commit,
1212
0
               bitmap_pos))
1213
0
    return 0;
1214
1215
0
  return 1;
1216
0
}
1217
1218
static int should_include(struct commit *commit, void *_data)
1219
0
{
1220
0
  struct include_data *data = _data;
1221
0
  int bitmap_pos;
1222
1223
0
  bitmap_pos = bitmap_position(data->bitmap_git, &commit->object.oid);
1224
0
  if (bitmap_pos < 0)
1225
0
    bitmap_pos = ext_index_add_object(data->bitmap_git,
1226
0
              (struct object *)commit,
1227
0
              NULL);
1228
1229
0
  if (!add_to_include_set(data->bitmap_git, data, commit, bitmap_pos)) {
1230
0
    struct commit_list *parent = commit->parents;
1231
1232
0
    while (parent) {
1233
0
      parent->item->object.flags |= SEEN;
1234
0
      parent = parent->next;
1235
0
    }
1236
1237
0
    return 0;
1238
0
  }
1239
1240
0
  return 1;
1241
0
}
1242
1243
static int should_include_obj(struct object *obj, void *_data)
1244
0
{
1245
0
  struct include_data *data = _data;
1246
0
  int bitmap_pos;
1247
1248
0
  bitmap_pos = bitmap_position(data->bitmap_git, &obj->oid);
1249
0
  if (bitmap_pos < 0)
1250
0
    return 1;
1251
0
  if ((data->seen && bitmap_get(data->seen, bitmap_pos)) ||
1252
0
       bitmap_get(data->base, bitmap_pos)) {
1253
0
    obj->flags |= SEEN;
1254
0
    return 0;
1255
0
  }
1256
0
  return 1;
1257
0
}
1258
1259
static int add_commit_to_bitmap(struct bitmap_index *bitmap_git,
1260
        struct bitmap **base,
1261
        struct commit *commit)
1262
0
{
1263
0
  struct ewah_bitmap *or_with = bitmap_for_commit(bitmap_git, commit);
1264
1265
0
  if (!or_with) {
1266
0
    existing_bitmaps_misses_nr++;
1267
0
    return 0;
1268
0
  }
1269
1270
0
  existing_bitmaps_hits_nr++;
1271
1272
0
  if (!*base)
1273
0
    *base = ewah_to_bitmap(or_with);
1274
0
  else
1275
0
    bitmap_or_ewah(*base, or_with);
1276
1277
0
  return 1;
1278
0
}
1279
1280
static struct bitmap *fill_in_bitmap(struct bitmap_index *bitmap_git,
1281
             struct rev_info *revs,
1282
             struct bitmap *base,
1283
             struct bitmap *seen)
1284
0
{
1285
0
  struct include_data incdata;
1286
0
  struct bitmap_show_data show_data;
1287
1288
0
  if (!base)
1289
0
    base = bitmap_new();
1290
1291
0
  incdata.bitmap_git = bitmap_git;
1292
0
  incdata.base = base;
1293
0
  incdata.seen = seen;
1294
1295
0
  revs->include_check = should_include;
1296
0
  revs->include_check_obj = should_include_obj;
1297
0
  revs->include_check_data = &incdata;
1298
1299
0
  if (prepare_revision_walk(revs))
1300
0
    die(_("revision walk setup failed"));
1301
1302
0
  show_data.bitmap_git = bitmap_git;
1303
0
  show_data.base = base;
1304
1305
0
  traverse_commit_list(revs, show_commit, show_object, &show_data);
1306
1307
0
  revs->include_check = NULL;
1308
0
  revs->include_check_obj = NULL;
1309
0
  revs->include_check_data = NULL;
1310
1311
0
  return base;
1312
0
}
1313
1314
struct bitmap_boundary_cb {
1315
  struct bitmap_index *bitmap_git;
1316
  struct bitmap *base;
1317
1318
  struct object_array boundary;
1319
};
1320
1321
static void show_boundary_commit(struct commit *commit, void *_data)
1322
0
{
1323
0
  struct bitmap_boundary_cb *data = _data;
1324
1325
0
  if (commit->object.flags & BOUNDARY)
1326
0
    add_object_array(&commit->object, "", &data->boundary);
1327
1328
0
  if (commit->object.flags & UNINTERESTING) {
1329
0
    if (bitmap_walk_contains(data->bitmap_git, data->base,
1330
0
           &commit->object.oid))
1331
0
      return;
1332
1333
0
    add_commit_to_bitmap(data->bitmap_git, &data->base, commit);
1334
0
  }
1335
0
}
1336
1337
static void show_boundary_object(struct object *object UNUSED,
1338
         const char *name UNUSED,
1339
         void *data UNUSED)
1340
0
{
1341
0
  BUG("should not be called");
1342
0
}
1343
1344
static unsigned cascade_pseudo_merges_1(struct bitmap_index *bitmap_git,
1345
          struct bitmap *result,
1346
          struct bitmap *roots)
1347
0
{
1348
0
  int ret = cascade_pseudo_merges(&bitmap_git->pseudo_merges,
1349
0
          result, roots);
1350
0
  if (ret) {
1351
0
    pseudo_merges_cascades_nr++;
1352
0
    pseudo_merges_satisfied_nr += ret;
1353
0
  }
1354
1355
0
  return ret;
1356
0
}
1357
1358
static struct bitmap *find_boundary_objects(struct bitmap_index *bitmap_git,
1359
              struct rev_info *revs,
1360
              struct object_list *roots)
1361
0
{
1362
0
  struct bitmap_boundary_cb cb;
1363
0
  struct object_list *root;
1364
0
  struct repository *repo;
1365
0
  unsigned int i;
1366
0
  unsigned int tmp_blobs, tmp_trees, tmp_tags;
1367
0
  int any_missing = 0;
1368
0
  int existing_bitmaps = 0;
1369
1370
0
  cb.bitmap_git = bitmap_git;
1371
0
  cb.base = bitmap_new();
1372
0
  object_array_init(&cb.boundary);
1373
1374
0
  repo = bitmap_repo(bitmap_git);
1375
1376
0
  revs->ignore_missing_links = 1;
1377
1378
0
  if (bitmap_git->pseudo_merges.nr) {
1379
0
    struct bitmap *roots_bitmap = bitmap_new();
1380
0
    struct object_list *objects = NULL;
1381
1382
0
    for (objects = roots; objects; objects = objects->next) {
1383
0
      struct object *object = objects->item;
1384
0
      int pos;
1385
1386
0
      pos = bitmap_position(bitmap_git, &object->oid);
1387
0
      if (pos < 0)
1388
0
        continue;
1389
1390
0
      bitmap_set(roots_bitmap, pos);
1391
0
    }
1392
1393
0
    cascade_pseudo_merges_1(bitmap_git, cb.base, roots_bitmap);
1394
0
    bitmap_free(roots_bitmap);
1395
0
  }
1396
1397
  /*
1398
   * OR in any existing reachability bitmaps among `roots` into
1399
   * `cb.base`.
1400
   */
1401
0
  for (root = roots; root; root = root->next) {
1402
0
    struct object *object = root->item;
1403
0
    if (object->type != OBJ_COMMIT ||
1404
0
        bitmap_walk_contains(bitmap_git, cb.base, &object->oid))
1405
0
      continue;
1406
1407
0
    if (add_commit_to_bitmap(bitmap_git, &cb.base,
1408
0
           (struct commit *)object)) {
1409
0
      existing_bitmaps = 1;
1410
0
      continue;
1411
0
    }
1412
1413
0
    any_missing = 1;
1414
0
  }
1415
1416
0
  if (!any_missing)
1417
0
    goto cleanup;
1418
1419
0
  if (existing_bitmaps)
1420
0
    cascade_pseudo_merges_1(bitmap_git, cb.base, NULL);
1421
1422
0
  tmp_blobs = revs->blob_objects;
1423
0
  tmp_trees = revs->tree_objects;
1424
0
  tmp_tags = revs->tag_objects;
1425
0
  revs->blob_objects = 0;
1426
0
  revs->tree_objects = 0;
1427
0
  revs->tag_objects = 0;
1428
1429
  /*
1430
   * We didn't have complete coverage of the roots. First setup a
1431
   * revision walk to (a) OR in any bitmaps that are UNINTERESTING
1432
   * between the tips and boundary, and (b) record the boundary.
1433
   */
1434
0
  trace2_region_enter("pack-bitmap", "boundary-prepare", repo);
1435
0
  if (prepare_revision_walk(revs))
1436
0
    die("revision walk setup failed");
1437
0
  trace2_region_leave("pack-bitmap", "boundary-prepare", repo);
1438
1439
0
  trace2_region_enter("pack-bitmap", "boundary-traverse", repo);
1440
0
  revs->boundary = 1;
1441
0
  traverse_commit_list_filtered(revs,
1442
0
              show_boundary_commit,
1443
0
              show_boundary_object,
1444
0
              &cb, NULL);
1445
0
  revs->boundary = 0;
1446
0
  trace2_region_leave("pack-bitmap", "boundary-traverse", repo);
1447
1448
0
  revs->blob_objects = tmp_blobs;
1449
0
  revs->tree_objects = tmp_trees;
1450
0
  revs->tag_objects = tmp_tags;
1451
1452
0
  reset_revision_walk();
1453
0
  clear_object_flags(repo, UNINTERESTING);
1454
1455
  /*
1456
   * Then add the boundary commit(s) as fill-in traversal tips.
1457
   */
1458
0
  trace2_region_enter("pack-bitmap", "boundary-fill-in", repo);
1459
0
  for (i = 0; i < cb.boundary.nr; i++) {
1460
0
    struct object *obj = cb.boundary.objects[i].item;
1461
0
    if (bitmap_walk_contains(bitmap_git, cb.base, &obj->oid))
1462
0
      obj->flags |= SEEN;
1463
0
    else
1464
0
      add_pending_object(revs, obj, "");
1465
0
  }
1466
0
  if (revs->pending.nr)
1467
0
    cb.base = fill_in_bitmap(bitmap_git, revs, cb.base, NULL);
1468
0
  trace2_region_leave("pack-bitmap", "boundary-fill-in", repo);
1469
1470
0
cleanup:
1471
0
  object_array_clear(&cb.boundary);
1472
0
  revs->ignore_missing_links = 0;
1473
1474
0
  return cb.base;
1475
0
}
1476
1477
struct ewah_bitmap *pseudo_merge_bitmap_for_commit(struct bitmap_index *bitmap_git,
1478
               struct commit *commit)
1479
0
{
1480
0
  struct commit_list *p;
1481
0
  struct bitmap *parents;
1482
0
  struct pseudo_merge *match = NULL;
1483
1484
0
  if (!bitmap_git->pseudo_merges.nr)
1485
0
    return NULL;
1486
1487
0
  parents = bitmap_new();
1488
1489
0
  for (p = commit->parents; p; p = p->next) {
1490
0
    int pos = bitmap_position(bitmap_git, &p->item->object.oid);
1491
0
    if (pos < 0 || pos >= bitmap_num_objects(bitmap_git))
1492
0
      goto done;
1493
1494
    /*
1495
     * Use bitmap-relative positions instead of offsetting
1496
     * by bitmap_git->num_objects_in_base because we use
1497
     * this to find a match in pseudo_merge_for_parents(),
1498
     * and pseudo-merge groups cannot span multiple bitmap
1499
     * layers.
1500
     */
1501
0
    bitmap_set(parents, pos);
1502
0
  }
1503
1504
0
  match = pseudo_merge_for_parents(&bitmap_git->pseudo_merges, parents);
1505
1506
0
done:
1507
0
  bitmap_free(parents);
1508
0
  if (match)
1509
0
    return pseudo_merge_bitmap(&bitmap_git->pseudo_merges, match);
1510
1511
0
  return NULL;
1512
0
}
1513
1514
static void unsatisfy_all_pseudo_merges(struct bitmap_index *bitmap_git)
1515
0
{
1516
0
  uint32_t i;
1517
0
  for (i = 0; i < bitmap_git->pseudo_merges.nr; i++)
1518
0
    bitmap_git->pseudo_merges.v[i].satisfied = 0;
1519
0
}
1520
1521
static struct bitmap *find_objects(struct bitmap_index *bitmap_git,
1522
           struct rev_info *revs,
1523
           struct object_list *roots,
1524
           struct bitmap *seen)
1525
0
{
1526
0
  struct bitmap *base = NULL;
1527
0
  int needs_walk = 0;
1528
0
  unsigned existing_bitmaps = 0;
1529
1530
0
  struct object_list *not_mapped = NULL;
1531
1532
0
  unsatisfy_all_pseudo_merges(bitmap_git);
1533
1534
0
  if (bitmap_git->pseudo_merges.nr) {
1535
0
    struct bitmap *roots_bitmap = bitmap_new();
1536
0
    struct object_list *objects = NULL;
1537
1538
0
    for (objects = roots; objects; objects = objects->next) {
1539
0
      struct object *object = objects->item;
1540
0
      int pos;
1541
1542
0
      pos = bitmap_position(bitmap_git, &object->oid);
1543
0
      if (pos < 0)
1544
0
        continue;
1545
1546
0
      bitmap_set(roots_bitmap, pos);
1547
0
    }
1548
1549
0
    base = bitmap_new();
1550
0
    cascade_pseudo_merges_1(bitmap_git, base, roots_bitmap);
1551
0
    bitmap_free(roots_bitmap);
1552
0
  }
1553
1554
  /*
1555
   * Go through all the roots for the walk. The ones that have bitmaps
1556
   * on the bitmap index will be `or`ed together to form an initial
1557
   * global reachability analysis.
1558
   *
1559
   * The ones without bitmaps in the index will be stored in the
1560
   * `not_mapped_list` for further processing.
1561
   */
1562
0
  while (roots) {
1563
0
    struct object *object = roots->item;
1564
1565
0
    roots = roots->next;
1566
1567
0
    if (base) {
1568
0
      int pos = bitmap_position(bitmap_git, &object->oid);
1569
0
      if (pos >= 0 && bitmap_get(base, pos)) {
1570
0
        object->flags |= SEEN;
1571
0
        continue;
1572
0
      }
1573
0
    }
1574
1575
0
    if (object->type == OBJ_COMMIT &&
1576
0
        add_commit_to_bitmap(bitmap_git, &base, (struct commit *)object)) {
1577
0
      object->flags |= SEEN;
1578
0
      existing_bitmaps = 1;
1579
0
      continue;
1580
0
    }
1581
1582
0
    object_list_insert(object, &not_mapped);
1583
0
  }
1584
1585
  /*
1586
   * Best case scenario: We found bitmaps for all the roots,
1587
   * so the resulting `or` bitmap has the full reachability analysis
1588
   */
1589
0
  if (!not_mapped)
1590
0
    return base;
1591
1592
0
  roots = not_mapped;
1593
1594
0
  if (existing_bitmaps)
1595
0
    cascade_pseudo_merges_1(bitmap_git, base, NULL);
1596
1597
  /*
1598
   * Let's iterate through all the roots that don't have bitmaps to
1599
   * check if we can determine them to be reachable from the existing
1600
   * global bitmap.
1601
   *
1602
   * If we cannot find them in the existing global bitmap, we'll need
1603
   * to push them to an actual walk and run it until we can confirm
1604
   * they are reachable
1605
   */
1606
0
  while (roots) {
1607
0
    struct object *object = roots->item;
1608
0
    int pos;
1609
1610
0
    roots = roots->next;
1611
0
    pos = bitmap_position(bitmap_git, &object->oid);
1612
1613
0
    if (pos < 0 || base == NULL || !bitmap_get(base, pos)) {
1614
0
      object->flags &= ~UNINTERESTING;
1615
0
      add_pending_object(revs, object, "");
1616
0
      needs_walk = 1;
1617
1618
0
      roots_without_bitmaps_nr++;
1619
0
    } else {
1620
0
      object->flags |= SEEN;
1621
1622
0
      roots_with_bitmaps_nr++;
1623
0
    }
1624
0
  }
1625
1626
0
  if (needs_walk) {
1627
    /*
1628
     * This fill-in traversal may walk over some objects
1629
     * again, since we have already traversed in order to
1630
     * find the boundary.
1631
     *
1632
     * But this extra walk should be extremely cheap, since
1633
     * all commit objects are loaded into memory, and
1634
     * because we skip walking to parents that are
1635
     * UNINTERESTING, since it will be marked in the haves
1636
     * bitmap already (or it has an on-disk bitmap, since
1637
     * OR-ing it in covers all of its ancestors).
1638
     */
1639
0
    base = fill_in_bitmap(bitmap_git, revs, base, seen);
1640
0
  }
1641
1642
0
  object_list_free(&not_mapped);
1643
1644
0
  return base;
1645
0
}
1646
1647
static void show_extended_objects(struct bitmap_index *bitmap_git,
1648
          struct rev_info *revs,
1649
          show_reachable_fn show_reach)
1650
0
{
1651
0
  struct bitmap *objects = bitmap_git->result;
1652
0
  struct eindex *eindex = &bitmap_git->ext_index;
1653
0
  uint32_t i;
1654
1655
0
  for (i = 0; i < eindex->count; ++i) {
1656
0
    struct object *obj;
1657
1658
0
    if (!bitmap_get(objects,
1659
0
        st_add(bitmap_num_objects_total(bitmap_git),
1660
0
               i)))
1661
0
      continue;
1662
1663
0
    obj = eindex->objects[i];
1664
0
    if ((obj->type == OBJ_BLOB && !revs->blob_objects) ||
1665
0
        (obj->type == OBJ_TREE && !revs->tree_objects) ||
1666
0
        (obj->type == OBJ_TAG && !revs->tag_objects))
1667
0
      continue;
1668
1669
0
    show_reach(&obj->oid, obj->type, 0, eindex->hashes[i], NULL, 0, NULL);
1670
0
  }
1671
0
}
1672
1673
static void init_type_iterator(struct ewah_or_iterator *it,
1674
             struct bitmap_index *bitmap_git,
1675
             enum object_type type)
1676
0
{
1677
0
  switch (type) {
1678
0
  case OBJ_COMMIT:
1679
0
    ewah_or_iterator_init(it, bitmap_git->commits_all,
1680
0
              bitmap_git->base_nr + 1);
1681
0
    break;
1682
1683
0
  case OBJ_TREE:
1684
0
    ewah_or_iterator_init(it, bitmap_git->trees_all,
1685
0
              bitmap_git->base_nr + 1);
1686
0
    break;
1687
1688
0
  case OBJ_BLOB:
1689
0
    ewah_or_iterator_init(it, bitmap_git->blobs_all,
1690
0
              bitmap_git->base_nr + 1);
1691
0
    break;
1692
1693
0
  case OBJ_TAG:
1694
0
    ewah_or_iterator_init(it, bitmap_git->tags_all,
1695
0
              bitmap_git->base_nr + 1);
1696
0
    break;
1697
1698
0
  default:
1699
0
    BUG("object type %d not stored by bitmap type index", type);
1700
0
    break;
1701
0
  }
1702
0
}
1703
1704
static int show_objects_for_type(
1705
  struct bitmap_index *bitmap_git,
1706
  struct bitmap *objects,
1707
  enum object_type object_type,
1708
  show_reachable_fn show_reach,
1709
  void *payload)
1710
0
{
1711
0
  size_t i = 0;
1712
0
  uint32_t offset;
1713
0
  int ret;
1714
1715
0
  struct ewah_or_iterator it;
1716
0
  eword_t filter;
1717
1718
0
  init_type_iterator(&it, bitmap_git, object_type);
1719
1720
0
  for (i = 0; i < objects->word_alloc &&
1721
0
      ewah_or_iterator_next(&filter, &it); i++) {
1722
0
    eword_t word = objects->words[i] & filter;
1723
0
    size_t pos = (i * BITS_IN_EWORD);
1724
1725
0
    if (!word)
1726
0
      continue;
1727
1728
0
    for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
1729
0
      struct packed_git *pack;
1730
0
      struct object_id oid;
1731
0
      uint32_t hash = 0, index_pos;
1732
0
      off_t ofs;
1733
1734
0
      if ((word >> offset) == 0)
1735
0
        break;
1736
1737
0
      offset += ewah_bit_ctz64(word >> offset);
1738
1739
0
      if (bitmap_is_midx(bitmap_git)) {
1740
0
        struct multi_pack_index *m = bitmap_git->midx;
1741
0
        uint32_t pack_id;
1742
1743
0
        index_pos = pack_pos_to_midx(m, pos + offset);
1744
0
        ofs = nth_midxed_offset(m, index_pos);
1745
0
        nth_midxed_object_oid(&oid, m, index_pos);
1746
1747
0
        pack_id = nth_midxed_pack_int_id(m, index_pos);
1748
0
        pack = nth_midxed_pack(bitmap_git->midx, pack_id);
1749
0
      } else {
1750
0
        index_pos = pack_pos_to_index(bitmap_git->pack, pos + offset);
1751
0
        ofs = pack_pos_to_offset(bitmap_git->pack, pos + offset);
1752
0
        nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
1753
1754
0
        pack = bitmap_git->pack;
1755
0
      }
1756
1757
0
      hash = bitmap_name_hash(bitmap_git, index_pos);
1758
1759
0
      ret = show_reach(&oid, object_type, 0, hash, pack, ofs, payload);
1760
0
      if (ret)
1761
0
        goto out;
1762
0
    }
1763
0
  }
1764
1765
0
  ret = 0;
1766
1767
0
out:
1768
0
  ewah_or_iterator_release(&it);
1769
0
  return ret;
1770
0
}
1771
1772
static int in_bitmapped_pack(struct bitmap_index *bitmap_git,
1773
           struct object_list *roots)
1774
0
{
1775
0
  while (roots) {
1776
0
    struct object *object = roots->item;
1777
0
    roots = roots->next;
1778
1779
0
    if (bitmap_is_midx(bitmap_git)) {
1780
0
      if (bsearch_midx(&object->oid, bitmap_git->midx, NULL))
1781
0
        return 1;
1782
0
    } else {
1783
0
      if (find_pack_entry_one(&object->oid, bitmap_git->pack) > 0)
1784
0
        return 1;
1785
0
    }
1786
0
  }
1787
1788
0
  return 0;
1789
0
}
1790
1791
static struct bitmap *find_tip_objects(struct bitmap_index *bitmap_git,
1792
               struct object_list *tip_objects,
1793
               enum object_type type)
1794
0
{
1795
0
  struct bitmap *result = bitmap_new();
1796
0
  struct object_list *p;
1797
1798
0
  for (p = tip_objects; p; p = p->next) {
1799
0
    int pos;
1800
1801
0
    if (p->item->type != type)
1802
0
      continue;
1803
1804
0
    pos = bitmap_position(bitmap_git, &p->item->oid);
1805
0
    if (pos < 0)
1806
0
      continue;
1807
1808
0
    bitmap_set(result, pos);
1809
0
  }
1810
1811
0
  return result;
1812
0
}
1813
1814
static void filter_bitmap_exclude_type(struct bitmap_index *bitmap_git,
1815
               struct object_list *tip_objects,
1816
               struct bitmap *to_filter,
1817
               enum object_type type)
1818
0
{
1819
0
  struct eindex *eindex = &bitmap_git->ext_index;
1820
0
  struct bitmap *tips;
1821
0
  struct ewah_or_iterator it;
1822
0
  eword_t mask;
1823
0
  uint32_t i;
1824
1825
  /*
1826
   * The non-bitmap version of this filter never removes
1827
   * objects which the other side specifically asked for,
1828
   * so we must match that behavior.
1829
   */
1830
0
  tips = find_tip_objects(bitmap_git, tip_objects, type);
1831
1832
  /*
1833
   * We can use the type-level bitmap for 'type' to work in whole
1834
   * words for the objects that are actually in the bitmapped
1835
   * packfile.
1836
   */
1837
0
  for (i = 0, init_type_iterator(&it, bitmap_git, type);
1838
0
       i < to_filter->word_alloc && ewah_or_iterator_next(&mask, &it);
1839
0
       i++) {
1840
0
    if (i < tips->word_alloc)
1841
0
      mask &= ~tips->words[i];
1842
0
    to_filter->words[i] &= ~mask;
1843
0
  }
1844
1845
  /*
1846
   * Clear any objects that weren't in the packfile (and so would
1847
   * not have been caught by the loop above. We'll have to check
1848
   * them individually.
1849
   */
1850
0
  for (i = 0; i < eindex->count; i++) {
1851
0
    size_t pos = st_add(i, bitmap_num_objects_total(bitmap_git));
1852
0
    if (eindex->objects[i]->type == type &&
1853
0
        bitmap_get(to_filter, pos) &&
1854
0
        !bitmap_get(tips, pos))
1855
0
      bitmap_unset(to_filter, pos);
1856
0
  }
1857
1858
0
  ewah_or_iterator_release(&it);
1859
0
  bitmap_free(tips);
1860
0
}
1861
1862
static void filter_bitmap_blob_none(struct bitmap_index *bitmap_git,
1863
            struct object_list *tip_objects,
1864
            struct bitmap *to_filter)
1865
0
{
1866
0
  filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1867
0
           OBJ_BLOB);
1868
0
}
1869
1870
static unsigned long get_size_by_pos(struct bitmap_index *bitmap_git,
1871
             uint32_t pos)
1872
0
{
1873
0
  size_t size;
1874
0
  struct object_info oi = OBJECT_INFO_INIT;
1875
1876
0
  oi.sizep = &size;
1877
1878
0
  if (pos < bitmap_num_objects_total(bitmap_git)) {
1879
0
    struct packed_git *pack;
1880
0
    off_t ofs;
1881
1882
0
    if (bitmap_is_midx(bitmap_git)) {
1883
0
      uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, pos);
1884
0
      uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
1885
1886
0
      pack = nth_midxed_pack(bitmap_git->midx, pack_id);
1887
0
      ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
1888
0
    } else {
1889
0
      pack = bitmap_git->pack;
1890
0
      ofs = pack_pos_to_offset(pack, pos);
1891
0
    }
1892
1893
0
    if (packed_object_info(NULL, pack, ofs, &oi) < 0) {
1894
0
      struct object_id oid;
1895
0
      nth_bitmap_object_oid(bitmap_git, &oid,
1896
0
                pack_pos_to_index(pack, pos));
1897
0
      die(_("unable to get size of %s"), oid_to_hex(&oid));
1898
0
    }
1899
0
  } else {
1900
0
    size_t eindex_pos = pos - bitmap_num_objects_total(bitmap_git);
1901
0
    struct eindex *eindex = &bitmap_git->ext_index;
1902
0
    struct object *obj = eindex->objects[eindex_pos];
1903
0
    if (odb_read_object_info_extended(bitmap_repo(bitmap_git)->objects, &obj->oid,
1904
0
              &oi, 0) < 0)
1905
0
      die(_("unable to get size of %s"), oid_to_hex(&obj->oid));
1906
0
  }
1907
1908
0
  return cast_size_t_to_ulong(size);
1909
0
}
1910
1911
static void filter_bitmap_blob_limit(struct bitmap_index *bitmap_git,
1912
             struct object_list *tip_objects,
1913
             struct bitmap *to_filter,
1914
             unsigned long limit)
1915
0
{
1916
0
  struct eindex *eindex = &bitmap_git->ext_index;
1917
0
  struct bitmap *tips;
1918
0
  struct ewah_or_iterator it;
1919
0
  eword_t mask;
1920
0
  uint32_t i;
1921
1922
0
  tips = find_tip_objects(bitmap_git, tip_objects, OBJ_BLOB);
1923
1924
0
  for (i = 0, init_type_iterator(&it, bitmap_git, OBJ_BLOB);
1925
0
       i < to_filter->word_alloc && ewah_or_iterator_next(&mask, &it);
1926
0
       i++) {
1927
0
    eword_t word = to_filter->words[i] & mask;
1928
0
    unsigned offset;
1929
1930
0
    for (offset = 0; offset < BITS_IN_EWORD; offset++) {
1931
0
      uint32_t pos;
1932
1933
0
      if ((word >> offset) == 0)
1934
0
        break;
1935
0
      offset += ewah_bit_ctz64(word >> offset);
1936
0
      pos = i * BITS_IN_EWORD + offset;
1937
1938
0
      if (!bitmap_get(tips, pos) &&
1939
0
          get_size_by_pos(bitmap_git, pos) >= limit)
1940
0
        bitmap_unset(to_filter, pos);
1941
0
    }
1942
0
  }
1943
1944
0
  for (i = 0; i < eindex->count; i++) {
1945
0
    size_t pos = st_add(i, bitmap_num_objects(bitmap_git));
1946
0
    if (eindex->objects[i]->type == OBJ_BLOB &&
1947
0
        bitmap_get(to_filter, pos) &&
1948
0
        !bitmap_get(tips, pos) &&
1949
0
        get_size_by_pos(bitmap_git, pos) >= limit)
1950
0
      bitmap_unset(to_filter, pos);
1951
0
  }
1952
1953
0
  ewah_or_iterator_release(&it);
1954
0
  bitmap_free(tips);
1955
0
}
1956
1957
static void filter_bitmap_tree_depth(struct bitmap_index *bitmap_git,
1958
             struct object_list *tip_objects,
1959
             struct bitmap *to_filter,
1960
             unsigned long limit)
1961
0
{
1962
0
  if (limit)
1963
0
    BUG("filter_bitmap_tree_depth given non-zero limit");
1964
1965
0
  filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1966
0
           OBJ_TREE);
1967
0
  filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter,
1968
0
           OBJ_BLOB);
1969
0
}
1970
1971
static void filter_bitmap_object_type(struct bitmap_index *bitmap_git,
1972
              struct object_list *tip_objects,
1973
              struct bitmap *to_filter,
1974
              enum object_type object_type)
1975
0
{
1976
0
  if (object_type < OBJ_COMMIT || object_type > OBJ_TAG)
1977
0
    BUG("filter_bitmap_object_type given invalid object");
1978
1979
0
  if (object_type != OBJ_TAG)
1980
0
    filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TAG);
1981
0
  if (object_type != OBJ_COMMIT)
1982
0
    filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_COMMIT);
1983
0
  if (object_type != OBJ_TREE)
1984
0
    filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_TREE);
1985
0
  if (object_type != OBJ_BLOB)
1986
0
    filter_bitmap_exclude_type(bitmap_git, tip_objects, to_filter, OBJ_BLOB);
1987
0
}
1988
1989
static int filter_bitmap(struct bitmap_index *bitmap_git,
1990
       struct object_list *tip_objects,
1991
       struct bitmap *to_filter,
1992
       const struct list_objects_filter_options *filter)
1993
0
{
1994
0
  if (!filter || filter->choice == LOFC_DISABLED)
1995
0
    return 0;
1996
1997
0
  if (filter->choice == LOFC_BLOB_NONE) {
1998
0
    if (bitmap_git)
1999
0
      filter_bitmap_blob_none(bitmap_git, tip_objects,
2000
0
            to_filter);
2001
0
    return 0;
2002
0
  }
2003
2004
0
  if (filter->choice == LOFC_BLOB_LIMIT) {
2005
0
    if (bitmap_git)
2006
0
      filter_bitmap_blob_limit(bitmap_git, tip_objects,
2007
0
             to_filter,
2008
0
             filter->blob_limit_value);
2009
0
    return 0;
2010
0
  }
2011
2012
0
  if (filter->choice == LOFC_TREE_DEPTH &&
2013
0
      filter->tree_exclude_depth == 0) {
2014
0
    if (bitmap_git)
2015
0
      filter_bitmap_tree_depth(bitmap_git, tip_objects,
2016
0
             to_filter,
2017
0
             filter->tree_exclude_depth);
2018
0
    return 0;
2019
0
  }
2020
2021
0
  if (filter->choice == LOFC_OBJECT_TYPE) {
2022
0
    if (bitmap_git)
2023
0
      filter_bitmap_object_type(bitmap_git, tip_objects,
2024
0
              to_filter,
2025
0
              filter->object_type);
2026
0
    return 0;
2027
0
  }
2028
2029
0
  if (filter->choice == LOFC_COMBINE) {
2030
0
    int i;
2031
0
    for (i = 0; i < filter->sub_nr; i++) {
2032
0
      if (filter_bitmap(bitmap_git, tip_objects, to_filter,
2033
0
            &filter->sub[i]) < 0)
2034
0
        return -1;
2035
0
    }
2036
0
    return 0;
2037
0
  }
2038
2039
  /* filter choice not handled */
2040
0
  return -1;
2041
0
}
2042
2043
bool can_filter_bitmap(const struct list_objects_filter_options *filter)
2044
0
{
2045
0
  return !filter_bitmap(NULL, NULL, NULL, filter);
2046
0
}
2047
2048
static void filter_packed_objects_from_bitmap(struct bitmap_index *bitmap_git,
2049
                struct bitmap *result)
2050
0
{
2051
0
  struct eindex *eindex = &bitmap_git->ext_index;
2052
0
  uint32_t objects_nr;
2053
0
  size_t i, pos;
2054
2055
0
  objects_nr = bitmap_num_objects_total(bitmap_git);
2056
0
  pos = objects_nr / BITS_IN_EWORD;
2057
2058
0
  if (pos > result->word_alloc)
2059
0
    pos = result->word_alloc;
2060
2061
0
  memset(result->words, 0x00, sizeof(eword_t) * pos);
2062
0
  for (i = pos * BITS_IN_EWORD; i < objects_nr; i++)
2063
0
    bitmap_unset(result, i);
2064
2065
0
  for (i = 0; i < eindex->count; ++i) {
2066
0
    if (has_object_pack(bitmap_repo(bitmap_git),
2067
0
            &eindex->objects[i]->oid))
2068
0
      bitmap_unset(result, objects_nr + i);
2069
0
  }
2070
0
}
2071
2072
int for_each_bitmapped_object(struct bitmap_index *bitmap_git,
2073
            const struct list_objects_filter_options *filter,
2074
            show_reachable_fn show_reach,
2075
            void *payload)
2076
0
{
2077
0
  const enum object_type types[] = {
2078
0
    OBJ_COMMIT,
2079
0
    OBJ_TREE,
2080
0
    OBJ_BLOB,
2081
0
    OBJ_TAG,
2082
0
  };
2083
0
  struct bitmap *filtered_bitmap = NULL;
2084
0
  uint32_t objects_nr;
2085
0
  size_t full_word_count;
2086
0
  int ret;
2087
2088
0
  if (!can_filter_bitmap(filter)) {
2089
0
    ret = -1;
2090
0
    goto out;
2091
0
  }
2092
2093
0
  objects_nr = bitmap_num_objects(bitmap_git);
2094
0
  full_word_count = objects_nr / BITS_IN_EWORD;
2095
2096
  /* We start from the all-1 bitmap and then filter down from there. */
2097
0
  filtered_bitmap = bitmap_word_alloc(full_word_count + !!(objects_nr % BITS_IN_EWORD));
2098
0
  memset(filtered_bitmap->words, 0xff, full_word_count * sizeof(*filtered_bitmap->words));
2099
0
  for (size_t i = full_word_count * BITS_IN_EWORD; i < objects_nr; i++)
2100
0
    bitmap_set(filtered_bitmap, i);
2101
2102
0
  if (filter_bitmap(bitmap_git, NULL, filtered_bitmap, filter) < 0) {
2103
0
    ret = -1;
2104
0
    goto out;
2105
0
  }
2106
2107
0
  for (size_t i = 0; i < ARRAY_SIZE(types); i++) {
2108
0
    ret = show_objects_for_type(bitmap_git, filtered_bitmap,
2109
0
              types[i], show_reach, payload);
2110
0
    if (ret)
2111
0
      goto out;
2112
0
  }
2113
2114
0
  ret = 0;
2115
0
out:
2116
0
  bitmap_free(filtered_bitmap);
2117
0
  return ret;
2118
0
}
2119
2120
struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
2121
           int filter_provided_objects)
2122
0
{
2123
0
  unsigned int i;
2124
0
  int use_boundary_traversal;
2125
2126
0
  struct object_list *wants = NULL;
2127
0
  struct object_list *haves = NULL;
2128
2129
0
  struct bitmap *wants_bitmap = NULL;
2130
0
  struct bitmap *haves_bitmap = NULL;
2131
2132
0
  struct bitmap_index *bitmap_git;
2133
0
  struct repository *repo;
2134
2135
  /*
2136
   * We can't do pathspec limiting with bitmaps, because we don't know
2137
   * which commits are associated with which object changes (let alone
2138
   * even which objects are associated with which paths).
2139
   */
2140
0
  if (revs->prune)
2141
0
    return NULL;
2142
2143
0
  if (!can_filter_bitmap(&revs->filter))
2144
0
    return NULL;
2145
2146
  /* try to open a bitmapped pack, but don't parse it yet
2147
   * because we may not need to use it */
2148
0
  CALLOC_ARRAY(bitmap_git, 1);
2149
0
  if (open_bitmap(revs->repo, bitmap_git) < 0)
2150
0
    goto cleanup;
2151
2152
0
  for (i = 0; i < revs->pending.nr; ++i) {
2153
0
    struct object *object = revs->pending.objects[i].item;
2154
2155
0
    if (object->type == OBJ_NONE)
2156
0
      parse_object_or_die(revs->repo, &object->oid, NULL);
2157
2158
0
    while (object->type == OBJ_TAG) {
2159
0
      struct tag *tag = (struct tag *) object;
2160
2161
0
      if (object->flags & UNINTERESTING)
2162
0
        object_list_insert(object, &haves);
2163
0
      else
2164
0
        object_list_insert(object, &wants);
2165
2166
0
      object = parse_object_or_die(revs->repo, get_tagged_oid(tag), NULL);
2167
0
      object->flags |= (tag->object.flags & UNINTERESTING);
2168
0
    }
2169
2170
0
    if (object->flags & UNINTERESTING)
2171
0
      object_list_insert(object, &haves);
2172
0
    else
2173
0
      object_list_insert(object, &wants);
2174
0
  }
2175
2176
0
  use_boundary_traversal = git_env_bool(GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL, -1);
2177
0
  if (use_boundary_traversal < 0) {
2178
0
    prepare_repo_settings(revs->repo);
2179
0
    use_boundary_traversal = revs->repo->settings.pack_use_bitmap_boundary_traversal;
2180
0
  }
2181
2182
0
  if (!use_boundary_traversal) {
2183
    /*
2184
     * if we have a HAVES list, but none of those haves is contained
2185
     * in the packfile that has a bitmap, we don't have anything to
2186
     * optimize here
2187
     */
2188
0
    if (haves && !in_bitmapped_pack(bitmap_git, haves))
2189
0
      goto cleanup;
2190
0
  }
2191
2192
  /* if we don't want anything, we're done here */
2193
0
  if (!wants)
2194
0
    goto cleanup;
2195
2196
  /*
2197
   * now we're going to use bitmaps, so load the actual bitmap entries
2198
   * from disk. this is the point of no return; after this the rev_list
2199
   * becomes invalidated and we must perform the revwalk through bitmaps
2200
   */
2201
0
  if (load_bitmap(revs->repo, bitmap_git, 0) < 0)
2202
0
    goto cleanup;
2203
2204
0
  if (!use_boundary_traversal)
2205
0
    object_array_clear(&revs->pending);
2206
2207
0
  repo = bitmap_repo(bitmap_git);
2208
2209
0
  if (haves) {
2210
0
    if (use_boundary_traversal) {
2211
0
      trace2_region_enter("pack-bitmap", "haves/boundary", repo);
2212
0
      haves_bitmap = find_boundary_objects(bitmap_git, revs, haves);
2213
0
      trace2_region_leave("pack-bitmap", "haves/boundary", repo);
2214
0
    } else {
2215
0
      trace2_region_enter("pack-bitmap", "haves/classic", repo);
2216
0
      revs->ignore_missing_links = 1;
2217
0
      haves_bitmap = find_objects(bitmap_git, revs, haves, NULL);
2218
0
      reset_revision_walk();
2219
0
      revs->ignore_missing_links = 0;
2220
0
      trace2_region_leave("pack-bitmap", "haves/classic", repo);
2221
0
    }
2222
2223
0
    if (!haves_bitmap)
2224
0
      BUG("failed to perform bitmap walk");
2225
0
  }
2226
2227
0
  if (use_boundary_traversal) {
2228
0
    object_array_clear(&revs->pending);
2229
0
    reset_revision_walk();
2230
0
  }
2231
2232
0
  wants_bitmap = find_objects(bitmap_git, revs, wants, haves_bitmap);
2233
2234
0
  if (!wants_bitmap)
2235
0
    BUG("failed to perform bitmap walk");
2236
2237
0
  if (haves_bitmap)
2238
0
    bitmap_and_not(wants_bitmap, haves_bitmap);
2239
2240
0
  filter_bitmap(bitmap_git,
2241
0
          (revs->filter.choice && filter_provided_objects) ? NULL : wants,
2242
0
          wants_bitmap,
2243
0
          &revs->filter);
2244
2245
0
  if (revs->unpacked)
2246
0
    filter_packed_objects_from_bitmap(bitmap_git, wants_bitmap);
2247
2248
0
  bitmap_git->result = wants_bitmap;
2249
0
  bitmap_git->haves = haves_bitmap;
2250
2251
0
  object_list_free(&wants);
2252
0
  object_list_free(&haves);
2253
2254
0
  trace2_data_intmax("bitmap", repo, "pseudo_merges_satisfied",
2255
0
         pseudo_merges_satisfied_nr);
2256
0
  trace2_data_intmax("bitmap", repo, "pseudo_merges_cascades",
2257
0
         pseudo_merges_cascades_nr);
2258
0
  trace2_data_intmax("bitmap", repo, "bitmap/hits",
2259
0
         existing_bitmaps_hits_nr);
2260
0
  trace2_data_intmax("bitmap", repo, "bitmap/misses",
2261
0
         existing_bitmaps_misses_nr);
2262
0
  trace2_data_intmax("bitmap", repo, "bitmap/roots_with_bitmap",
2263
0
         roots_with_bitmaps_nr);
2264
0
  trace2_data_intmax("bitmap", repo, "bitmap/roots_without_bitmap",
2265
0
         roots_without_bitmaps_nr);
2266
2267
0
  return bitmap_git;
2268
2269
0
cleanup:
2270
0
  free_bitmap_index(bitmap_git);
2271
0
  object_list_free(&wants);
2272
0
  object_list_free(&haves);
2273
0
  return NULL;
2274
0
}
2275
2276
/*
2277
 * -1 means "stop trying further objects"; 0 means we may or may not have
2278
 * reused, but you can keep feeding bits.
2279
 */
2280
static int try_partial_reuse(struct bitmap_index *bitmap_git,
2281
           struct bitmapped_pack *pack,
2282
           size_t bitmap_pos,
2283
           uint32_t pack_pos,
2284
           off_t offset,
2285
           struct bitmap *reuse,
2286
           struct pack_window **w_curs)
2287
0
{
2288
0
  off_t delta_obj_offset;
2289
0
  enum object_type type;
2290
0
  size_t size;
2291
2292
0
  if (pack_pos >= pack->p->num_objects)
2293
0
    return -1; /* not actually in the pack */
2294
2295
0
  delta_obj_offset = offset;
2296
0
  type = unpack_object_header(pack->p, w_curs, &offset, &size);
2297
0
  if (type < 0)
2298
0
    return -1; /* broken packfile, punt */
2299
2300
0
  if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
2301
0
    off_t base_offset;
2302
0
    uint32_t base_pos;
2303
0
    uint32_t base_bitmap_pos;
2304
2305
    /*
2306
     * Find the position of the base object so we can look it up
2307
     * in our bitmaps. If we can't come up with an offset, or if
2308
     * that offset is not in the revidx, the pack is corrupt.
2309
     * There's nothing we can do, so just punt on this object,
2310
     * and the normal slow path will complain about it in
2311
     * more detail.
2312
     */
2313
0
    base_offset = get_delta_base(pack->p, w_curs, &offset, type,
2314
0
               delta_obj_offset);
2315
0
    if (!base_offset)
2316
0
      return 0;
2317
2318
0
    offset_to_pack_pos(pack->p, base_offset, &base_pos);
2319
2320
0
    if (bitmap_is_midx(bitmap_git)) {
2321
      /*
2322
       * Cross-pack deltas are rejected for now, but could
2323
       * theoretically be supported in the future.
2324
       *
2325
       * We would need to ensure that we're sending both
2326
       * halves of the delta/base pair, regardless of whether
2327
       * or not the two cross a pack boundary. If they do,
2328
       * then we must convert the delta to an REF_DELTA to
2329
       * refer back to the base in the other pack.
2330
       * */
2331
0
      if (midx_pair_to_pack_pos(bitmap_git->midx,
2332
0
              pack->pack_int_id,
2333
0
              base_offset,
2334
0
              &base_bitmap_pos) < 0) {
2335
0
        return 0;
2336
0
      }
2337
0
    } else {
2338
0
      if (offset_to_pack_pos(pack->p, base_offset,
2339
0
                 &base_pos) < 0)
2340
0
        return 0;
2341
      /*
2342
       * We assume delta dependencies always point backwards.
2343
       * This lets us do a single pass, and is basically
2344
       * always true due to the way OFS_DELTAs work. You would
2345
       * not typically find REF_DELTA in a bitmapped pack,
2346
       * since we only bitmap packs we write fresh, and
2347
       * OFS_DELTA is the default). But let's double check to
2348
       * make sure the pack wasn't written with odd
2349
       * parameters.
2350
       */
2351
0
      if (base_pos >= pack_pos)
2352
0
        return 0;
2353
0
      base_bitmap_pos = pack->bitmap_pos + base_pos;
2354
0
    }
2355
2356
    /*
2357
     * And finally, if we're not sending the base as part of our
2358
     * reuse chunk, then don't send this object either. The base
2359
     * would come after us, along with other objects not
2360
     * necessarily in the pack, which means we'd need to convert
2361
     * to REF_DELTA on the fly. Better to just let the normal
2362
     * object_entry code path handle it.
2363
     */
2364
0
    if (!bitmap_get(reuse, base_bitmap_pos))
2365
0
      return 0;
2366
0
  }
2367
2368
  /*
2369
   * If we got here, then the object is OK to reuse. Mark it.
2370
   */
2371
0
  bitmap_set(reuse, bitmap_pos);
2372
0
  return 0;
2373
0
}
2374
2375
static void reuse_partial_packfile_from_bitmap_1(struct bitmap_index *bitmap_git,
2376
             struct bitmapped_pack *pack,
2377
             struct bitmap *reuse)
2378
0
{
2379
0
  struct bitmap *result = bitmap_git->result;
2380
0
  struct pack_window *w_curs = NULL;
2381
0
  size_t pos = pack->bitmap_pos / BITS_IN_EWORD;
2382
2383
0
  if (!pack->bitmap_pos) {
2384
    /*
2385
     * If we're processing the first (in the case of a MIDX, the
2386
     * preferred pack) or the only (in the case of single-pack
2387
     * bitmaps) pack, then we can reuse whole words at a time.
2388
     *
2389
     * This is because we know that any deltas in this range *must*
2390
     * have their bases chosen from the same pack, since:
2391
     *
2392
     * - In the single pack case, there is no other pack to choose
2393
     *   them from.
2394
     *
2395
     * - In the MIDX case, the first pack is the preferred pack, so
2396
     *   all ties are broken in favor of that pack (i.e. the one
2397
     *   we're currently processing). So any duplicate bases will be
2398
     *   resolved in favor of the pack we're processing.
2399
     */
2400
0
    while (pos < result->word_alloc &&
2401
0
           pos < pack->bitmap_nr / BITS_IN_EWORD &&
2402
0
           result->words[pos] == (eword_t)~0)
2403
0
      pos++;
2404
0
    memset(reuse->words, 0xFF, pos * sizeof(eword_t));
2405
0
  }
2406
2407
0
  for (; pos < result->word_alloc; pos++) {
2408
0
    eword_t word = result->words[pos];
2409
0
    size_t offset;
2410
2411
0
    for (offset = 0; offset < BITS_IN_EWORD; offset++) {
2412
0
      size_t bit_pos;
2413
0
      uint32_t pack_pos;
2414
0
      off_t ofs;
2415
2416
0
      if (word >> offset == 0)
2417
0
        break;
2418
2419
0
      offset += ewah_bit_ctz64(word >> offset);
2420
2421
0
      bit_pos = pos * BITS_IN_EWORD + offset;
2422
0
      if (bit_pos < pack->bitmap_pos)
2423
0
        continue;
2424
0
      if (bit_pos >= pack->bitmap_pos + pack->bitmap_nr)
2425
0
        goto done;
2426
2427
0
      if (bitmap_is_midx(bitmap_git)) {
2428
0
        uint32_t midx_pos;
2429
2430
0
        midx_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2431
0
        ofs = nth_midxed_offset(bitmap_git->midx, midx_pos);
2432
2433
0
        if (offset_to_pack_pos(pack->p, ofs, &pack_pos) < 0)
2434
0
          BUG("could not find object in pack %s "
2435
0
              "at offset %"PRIuMAX" in MIDX",
2436
0
              pack_basename(pack->p), (uintmax_t)ofs);
2437
0
      } else {
2438
0
        pack_pos = cast_size_t_to_uint32_t(st_sub(bit_pos, pack->bitmap_pos));
2439
0
        if (pack_pos >= pack->p->num_objects)
2440
0
          BUG("advanced beyond the end of pack %s (%"PRIuMAX" > %"PRIu32")",
2441
0
              pack_basename(pack->p), (uintmax_t)pack_pos,
2442
0
              pack->p->num_objects);
2443
2444
0
        ofs = pack_pos_to_offset(pack->p, pack_pos);
2445
0
      }
2446
2447
0
      if (try_partial_reuse(bitmap_git, pack, bit_pos,
2448
0
                pack_pos, ofs, reuse, &w_curs) < 0) {
2449
        /*
2450
         * try_partial_reuse indicated we couldn't reuse
2451
         * any bits, so there is no point in trying more
2452
         * bits in the current word, or any other words
2453
         * in result.
2454
         *
2455
         * Jump out of both loops to avoid future
2456
         * unnecessary calls to try_partial_reuse.
2457
         */
2458
0
        goto done;
2459
0
      }
2460
0
    }
2461
0
  }
2462
2463
0
done:
2464
0
  unuse_pack(&w_curs);
2465
0
}
2466
2467
static int bitmapped_pack_cmp(const void *va, const void *vb)
2468
0
{
2469
0
  const struct bitmapped_pack *a = va;
2470
0
  const struct bitmapped_pack *b = vb;
2471
2472
0
  if (a->bitmap_pos < b->bitmap_pos)
2473
0
    return -1;
2474
0
  if (a->bitmap_pos > b->bitmap_pos)
2475
0
    return 1;
2476
0
  return 0;
2477
0
}
2478
2479
void reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
2480
          struct bitmapped_pack **packs_out,
2481
          size_t *packs_nr_out,
2482
          struct bitmap **reuse_out,
2483
          int multi_pack_reuse)
2484
0
{
2485
0
  struct repository *r = bitmap_repo(bitmap_git);
2486
0
  struct bitmapped_pack *packs = NULL;
2487
0
  struct bitmap *result = bitmap_git->result;
2488
0
  struct bitmap *reuse;
2489
0
  size_t i;
2490
0
  size_t packs_nr = 0, packs_alloc = 0;
2491
0
  size_t word_alloc;
2492
0
  uint32_t objects_nr = 0;
2493
2494
0
  assert(result);
2495
2496
0
  load_reverse_index(r, bitmap_git);
2497
2498
0
  if (!bitmap_is_midx(bitmap_git) || !bitmap_git->midx->chunk_bitmapped_packs)
2499
0
    multi_pack_reuse = 0;
2500
2501
0
  if (multi_pack_reuse) {
2502
0
    struct multi_pack_index *m = bitmap_git->midx;
2503
0
    for (i = 0; i < m->num_packs + m->num_packs_in_base; i++) {
2504
0
      struct bitmapped_pack pack;
2505
0
      if (nth_bitmapped_pack(bitmap_git->midx, &pack, i) < 0) {
2506
0
        warning(_("unable to load pack: '%s', disabling pack-reuse"),
2507
0
          bitmap_git->midx->pack_names[i]);
2508
0
        free(packs);
2509
0
        return;
2510
0
      }
2511
2512
0
      if (!pack.bitmap_nr)
2513
0
        continue;
2514
2515
0
      if (is_pack_valid(pack.p)) {
2516
0
        ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2517
0
        memcpy(&packs[packs_nr++], &pack, sizeof(pack));
2518
0
      }
2519
2520
0
      objects_nr += pack.p->num_objects;
2521
0
    }
2522
2523
0
    QSORT(packs, packs_nr, bitmapped_pack_cmp);
2524
0
  } else {
2525
0
    struct packed_git *pack;
2526
0
    uint32_t pack_int_id;
2527
2528
0
    if (bitmap_is_midx(bitmap_git)) {
2529
0
      struct multi_pack_index *m = bitmap_git->midx;
2530
0
      uint32_t preferred_pack_pos;
2531
2532
0
      while (m->base_midx)
2533
0
        m = m->base_midx;
2534
2535
0
      if (midx_preferred_pack(m, &preferred_pack_pos) < 0) {
2536
0
        warning(_("unable to compute preferred pack, disabling pack-reuse"));
2537
0
        return;
2538
0
      }
2539
2540
0
      pack = nth_midxed_pack(m, preferred_pack_pos);
2541
0
      pack_int_id = preferred_pack_pos;
2542
0
    } else {
2543
0
      pack = bitmap_git->pack;
2544
      /*
2545
       * Any value for 'pack_int_id' will do here. When we
2546
       * process the pack via try_partial_reuse(), we won't
2547
       * use the `pack_int_id` field since we have a non-MIDX
2548
       * bitmap.
2549
       *
2550
       * Use '-1' as a sentinel value to make it clear
2551
       * that we do not expect to read this field.
2552
       */
2553
0
      pack_int_id = -1;
2554
0
    }
2555
2556
0
    if (is_pack_valid(pack)) {
2557
0
      ALLOC_GROW(packs, packs_nr + 1, packs_alloc);
2558
0
      packs[packs_nr].p = pack;
2559
0
      packs[packs_nr].pack_int_id = pack_int_id;
2560
0
      packs[packs_nr].bitmap_nr = pack->num_objects;
2561
0
      packs[packs_nr].bitmap_pos = 0;
2562
0
      packs[packs_nr].from_midx = bitmap_git->midx;
2563
0
      packs_nr++;
2564
0
    }
2565
2566
0
    objects_nr = pack->num_objects;
2567
0
  }
2568
2569
0
  if (!packs_nr)
2570
0
    return;
2571
2572
0
  word_alloc = objects_nr / BITS_IN_EWORD;
2573
0
  if (objects_nr % BITS_IN_EWORD)
2574
0
    word_alloc++;
2575
0
  reuse = bitmap_word_alloc(word_alloc);
2576
2577
0
  for (i = 0; i < packs_nr; i++)
2578
0
    reuse_partial_packfile_from_bitmap_1(bitmap_git, &packs[i], reuse);
2579
2580
0
  if (bitmap_is_empty(reuse)) {
2581
0
    free(packs);
2582
0
    bitmap_free(reuse);
2583
0
    return;
2584
0
  }
2585
2586
  /*
2587
   * Drop any reused objects from the result, since they will not
2588
   * need to be handled separately.
2589
   */
2590
0
  bitmap_and_not(result, reuse);
2591
0
  *packs_out = packs;
2592
0
  *packs_nr_out = packs_nr;
2593
0
  *reuse_out = reuse;
2594
0
}
2595
2596
int bitmap_walk_contains(struct bitmap_index *bitmap_git,
2597
       struct bitmap *bitmap, const struct object_id *oid)
2598
0
{
2599
0
  int idx;
2600
2601
0
  if (!bitmap)
2602
0
    return 0;
2603
2604
0
  idx = bitmap_position(bitmap_git, oid);
2605
0
  return idx >= 0 && bitmap_get(bitmap, idx);
2606
0
}
2607
2608
void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
2609
         struct rev_info *revs,
2610
         show_reachable_fn show_reachable)
2611
0
{
2612
0
  assert(bitmap_git->result);
2613
2614
0
  show_objects_for_type(bitmap_git, bitmap_git->result,
2615
0
            OBJ_COMMIT, show_reachable, NULL);
2616
0
  if (revs->tree_objects)
2617
0
    show_objects_for_type(bitmap_git, bitmap_git->result,
2618
0
              OBJ_TREE, show_reachable, NULL);
2619
0
  if (revs->blob_objects)
2620
0
    show_objects_for_type(bitmap_git, bitmap_git->result,
2621
0
              OBJ_BLOB, show_reachable, NULL);
2622
0
  if (revs->tag_objects)
2623
0
    show_objects_for_type(bitmap_git, bitmap_git->result,
2624
0
              OBJ_TAG, show_reachable, NULL);
2625
2626
0
  show_extended_objects(bitmap_git, revs, show_reachable);
2627
0
}
2628
2629
static uint32_t count_object_type(struct bitmap_index *bitmap_git,
2630
          enum object_type type)
2631
0
{
2632
0
  struct bitmap *objects = bitmap_git->result;
2633
0
  struct eindex *eindex = &bitmap_git->ext_index;
2634
2635
0
  uint32_t i = 0, count = 0;
2636
0
  struct ewah_or_iterator it;
2637
0
  eword_t filter;
2638
2639
0
  init_type_iterator(&it, bitmap_git, type);
2640
2641
0
  while (i < objects->word_alloc && ewah_or_iterator_next(&filter, &it)) {
2642
0
    eword_t word = objects->words[i++] & filter;
2643
0
    count += ewah_bit_popcount64(word);
2644
0
  }
2645
2646
0
  for (i = 0; i < eindex->count; ++i) {
2647
0
    if (eindex->objects[i]->type == type &&
2648
0
        bitmap_get(objects,
2649
0
             st_add(bitmap_num_objects_total(bitmap_git), i)))
2650
0
      count++;
2651
0
  }
2652
2653
0
  ewah_or_iterator_release(&it);
2654
2655
0
  return count;
2656
0
}
2657
2658
void count_bitmap_commit_list(struct bitmap_index *bitmap_git,
2659
            uint32_t *commits, uint32_t *trees,
2660
            uint32_t *blobs, uint32_t *tags)
2661
0
{
2662
0
  assert(bitmap_git->result);
2663
2664
0
  if (commits)
2665
0
    *commits = count_object_type(bitmap_git, OBJ_COMMIT);
2666
2667
0
  if (trees)
2668
0
    *trees = count_object_type(bitmap_git, OBJ_TREE);
2669
2670
0
  if (blobs)
2671
0
    *blobs = count_object_type(bitmap_git, OBJ_BLOB);
2672
2673
0
  if (tags)
2674
0
    *tags = count_object_type(bitmap_git, OBJ_TAG);
2675
0
}
2676
2677
struct bitmap_test_data {
2678
  struct bitmap_index *bitmap_git;
2679
  struct bitmap *base;
2680
  struct bitmap *commits;
2681
  struct bitmap *trees;
2682
  struct bitmap *blobs;
2683
  struct bitmap *tags;
2684
  struct progress *prg;
2685
  size_t seen;
2686
2687
  struct bitmap_test_data *base_tdata;
2688
};
2689
2690
static void test_bitmap_type(struct bitmap_test_data *tdata,
2691
           struct object *obj, int pos)
2692
0
{
2693
0
  enum object_type bitmap_type = OBJ_NONE;
2694
0
  int bitmaps_nr = 0;
2695
2696
0
  if (bitmap_is_midx(tdata->bitmap_git)) {
2697
0
    while (pos < tdata->bitmap_git->midx->num_objects_in_base)
2698
0
      tdata = tdata->base_tdata;
2699
0
  }
2700
2701
0
  if (bitmap_get(tdata->commits, pos)) {
2702
0
    bitmap_type = OBJ_COMMIT;
2703
0
    bitmaps_nr++;
2704
0
  }
2705
0
  if (bitmap_get(tdata->trees, pos)) {
2706
0
    bitmap_type = OBJ_TREE;
2707
0
    bitmaps_nr++;
2708
0
  }
2709
0
  if (bitmap_get(tdata->blobs, pos)) {
2710
0
    bitmap_type = OBJ_BLOB;
2711
0
    bitmaps_nr++;
2712
0
  }
2713
0
  if (bitmap_get(tdata->tags, pos)) {
2714
0
    bitmap_type = OBJ_TAG;
2715
0
    bitmaps_nr++;
2716
0
  }
2717
2718
0
  if (bitmap_type == OBJ_NONE)
2719
0
    die(_("object '%s' not found in type bitmaps"),
2720
0
        oid_to_hex(&obj->oid));
2721
2722
0
  if (bitmaps_nr > 1)
2723
0
    die(_("object '%s' does not have a unique type"),
2724
0
        oid_to_hex(&obj->oid));
2725
2726
0
  if (bitmap_type != obj->type)
2727
0
    die(_("object '%s': real type '%s', expected: '%s'"),
2728
0
        oid_to_hex(&obj->oid),
2729
0
        type_name(obj->type),
2730
0
        type_name(bitmap_type));
2731
0
}
2732
2733
static void test_show_object(struct object *object,
2734
           const char *name UNUSED,
2735
           void *data)
2736
0
{
2737
0
  struct bitmap_test_data *tdata = data;
2738
0
  int bitmap_pos;
2739
2740
0
  bitmap_pos = bitmap_position(tdata->bitmap_git, &object->oid);
2741
0
  if (bitmap_pos < 0)
2742
0
    die(_("object not in bitmap: '%s'"), oid_to_hex(&object->oid));
2743
0
  test_bitmap_type(tdata, object, bitmap_pos);
2744
2745
0
  bitmap_set(tdata->base, bitmap_pos);
2746
0
  display_progress(tdata->prg, ++tdata->seen);
2747
0
}
2748
2749
static void test_show_commit(struct commit *commit, void *data)
2750
0
{
2751
0
  struct bitmap_test_data *tdata = data;
2752
0
  int bitmap_pos;
2753
2754
0
  bitmap_pos = bitmap_position(tdata->bitmap_git,
2755
0
             &commit->object.oid);
2756
0
  if (bitmap_pos < 0)
2757
0
    die(_("object not in bitmap: '%s'"), oid_to_hex(&commit->object.oid));
2758
0
  test_bitmap_type(tdata, &commit->object, bitmap_pos);
2759
2760
0
  bitmap_set(tdata->base, bitmap_pos);
2761
0
  display_progress(tdata->prg, ++tdata->seen);
2762
0
}
2763
2764
static uint32_t bitmap_total_entry_count(struct bitmap_index *bitmap_git)
2765
0
{
2766
0
  uint32_t total = 0;
2767
0
  do {
2768
0
    total = st_add(total, bitmap_git->entry_count);
2769
0
    bitmap_git = bitmap_git->base;
2770
0
  } while (bitmap_git);
2771
2772
0
  return total;
2773
0
}
2774
2775
static void bitmap_test_data_prepare(struct bitmap_test_data *tdata,
2776
             struct bitmap_index *bitmap_git)
2777
0
{
2778
0
  memset(tdata, 0, sizeof(struct bitmap_test_data));
2779
2780
0
  tdata->bitmap_git = bitmap_git;
2781
0
  tdata->base = bitmap_new();
2782
0
  tdata->commits = ewah_to_bitmap(bitmap_git->commits);
2783
0
  tdata->trees = ewah_to_bitmap(bitmap_git->trees);
2784
0
  tdata->blobs = ewah_to_bitmap(bitmap_git->blobs);
2785
0
  tdata->tags = ewah_to_bitmap(bitmap_git->tags);
2786
2787
0
  if (bitmap_git->base) {
2788
0
    tdata->base_tdata = xmalloc(sizeof(struct bitmap_test_data));
2789
0
    bitmap_test_data_prepare(tdata->base_tdata, bitmap_git->base);
2790
0
  }
2791
0
}
2792
2793
static void bitmap_test_data_release(struct bitmap_test_data *tdata)
2794
0
{
2795
0
  if (!tdata)
2796
0
    return;
2797
2798
0
  bitmap_test_data_release(tdata->base_tdata);
2799
0
  free(tdata->base_tdata);
2800
2801
0
  bitmap_free(tdata->base);
2802
0
  bitmap_free(tdata->commits);
2803
0
  bitmap_free(tdata->trees);
2804
0
  bitmap_free(tdata->blobs);
2805
0
  bitmap_free(tdata->tags);
2806
0
}
2807
2808
void test_bitmap_walk(struct rev_info *revs)
2809
0
{
2810
0
  struct object *root;
2811
0
  struct bitmap *result = NULL;
2812
0
  size_t result_popcnt;
2813
0
  struct bitmap_test_data tdata;
2814
0
  struct bitmap_index *bitmap_git, *found;
2815
0
  struct ewah_bitmap *bm;
2816
2817
0
  if (!(bitmap_git = prepare_bitmap_git(revs->repo)))
2818
0
    die(_("failed to load bitmap indexes"));
2819
2820
0
  if (revs->pending.nr != 1)
2821
0
    die(_("you must specify exactly one commit to test"));
2822
2823
0
  fprintf_ln(stderr, "Bitmap v%d test (%d entries%s, %d total)",
2824
0
    bitmap_git->version,
2825
0
    bitmap_git->entry_count,
2826
0
    bitmap_git->table_lookup ? "" : " loaded",
2827
0
    bitmap_total_entry_count(bitmap_git));
2828
2829
0
  root = revs->pending.objects[0].item;
2830
0
  bm = find_bitmap_for_commit(bitmap_git, (struct commit *)root, &found);
2831
2832
0
  if (bm) {
2833
0
    fprintf_ln(stderr, "Found bitmap for '%s'. %d bits / %08x checksum",
2834
0
      oid_to_hex(&root->oid),
2835
0
      (int)bm->bit_size, ewah_checksum(bm));
2836
2837
0
    if (bitmap_is_midx(found))
2838
0
      fprintf_ln(stderr, "Located via MIDX '%s'.",
2839
0
           midx_get_checksum_hex(found->midx));
2840
0
    else
2841
0
      fprintf_ln(stderr, "Located via pack '%s'.",
2842
0
           hash_to_hex_algop(found->pack->hash,
2843
0
                 revs->repo->hash_algo));
2844
2845
0
    result = ewah_to_bitmap(bm);
2846
0
  }
2847
2848
0
  if (!result)
2849
0
    die(_("commit '%s' doesn't have an indexed bitmap"), oid_to_hex(&root->oid));
2850
2851
0
  revs->tag_objects = 1;
2852
0
  revs->tree_objects = 1;
2853
0
  revs->blob_objects = 1;
2854
2855
0
  result_popcnt = bitmap_popcount(result);
2856
2857
0
  if (prepare_revision_walk(revs))
2858
0
    die(_("revision walk setup failed"));
2859
2860
0
  bitmap_test_data_prepare(&tdata, bitmap_git);
2861
0
  tdata.prg = start_progress(revs->repo,
2862
0
           "Verifying bitmap entries",
2863
0
           result_popcnt);
2864
2865
0
  traverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);
2866
2867
0
  stop_progress(&tdata.prg);
2868
2869
0
  if (bitmap_equals(result, tdata.base))
2870
0
    fprintf_ln(stderr, "OK!");
2871
0
  else
2872
0
    die(_("mismatch in bitmap results"));
2873
2874
0
  bitmap_free(result);
2875
0
  bitmap_test_data_release(&tdata);
2876
0
  free_bitmap_index(bitmap_git);
2877
0
}
2878
2879
int test_bitmap_commits(struct repository *r)
2880
0
{
2881
0
  struct object_id oid;
2882
0
  MAYBE_UNUSED void *value;
2883
0
  struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2884
2885
0
  if (!bitmap_git)
2886
0
    die(_("failed to load bitmap indexes"));
2887
2888
  /*
2889
   * Since this function needs to print the bitmapped
2890
   * commits, bypass the commit lookup table (if one exists)
2891
   * by forcing the bitmap to eagerly load its entries.
2892
   */
2893
0
  if (bitmap_git->table_lookup) {
2894
0
    if (load_bitmap_entries_v1(bitmap_git) < 0)
2895
0
      die(_("failed to load bitmap indexes"));
2896
0
  }
2897
2898
0
  kh_foreach(bitmap_git->bitmaps, oid, value, {
2899
0
    printf_ln("%s", oid_to_hex(&oid));
2900
0
  });
2901
2902
0
  free_bitmap_index(bitmap_git);
2903
2904
0
  return 0;
2905
0
}
2906
2907
int test_bitmap_commits_with_offset(struct repository *r)
2908
0
{
2909
0
  struct object_id oid;
2910
0
  struct stored_bitmap *stored;
2911
0
  struct bitmap_index *bitmap_git;
2912
0
  size_t commit_idx_pos_map_pos, xor_offset_map_pos, flag_map_pos,
2913
0
    ewah_bitmap_map_pos;
2914
2915
0
  bitmap_git = prepare_bitmap_git(r);
2916
0
  if (!bitmap_git)
2917
0
    die(_("failed to load bitmap indexes"));
2918
2919
  /*
2920
   * Since this function needs to know the position of each individual
2921
   * bitmap, bypass the commit lookup table (if one exists) by forcing
2922
   * the bitmap to eagerly load its entries.
2923
   */
2924
0
  if (bitmap_git->table_lookup) {
2925
0
    if (load_bitmap_entries_v1(bitmap_git) < 0)
2926
0
      die(_("failed to load bitmap indexes"));
2927
0
  }
2928
2929
0
  kh_foreach (bitmap_git->bitmaps, oid, stored, {
2930
0
    commit_idx_pos_map_pos = stored->map_pos;
2931
0
    xor_offset_map_pos = stored->map_pos + sizeof(uint32_t);
2932
0
    flag_map_pos = xor_offset_map_pos + sizeof(uint8_t);
2933
0
    ewah_bitmap_map_pos = flag_map_pos + sizeof(uint8_t);
2934
2935
0
    printf_ln("%s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
2936
0
        oid_to_hex(&oid),
2937
0
        (uintmax_t)commit_idx_pos_map_pos,
2938
0
        (uintmax_t)xor_offset_map_pos,
2939
0
        (uintmax_t)flag_map_pos,
2940
0
        (uintmax_t)ewah_bitmap_map_pos);
2941
0
  })
2942
0
    ;
2943
2944
0
  free_bitmap_index(bitmap_git);
2945
2946
0
  return 0;
2947
0
}
2948
2949
int test_bitmap_hashes(struct repository *r)
2950
0
{
2951
0
  struct bitmap_index *bitmap_git = prepare_bitmap_git(r);
2952
0
  struct object_id oid;
2953
0
  uint32_t i, index_pos;
2954
2955
0
  if (!bitmap_git || !bitmap_git->hashes)
2956
0
    goto cleanup;
2957
2958
0
  for (i = 0; i < bitmap_num_objects(bitmap_git); i++) {
2959
0
    if (bitmap_is_midx(bitmap_git))
2960
0
      index_pos = pack_pos_to_midx(bitmap_git->midx, i);
2961
0
    else
2962
0
      index_pos = pack_pos_to_index(bitmap_git->pack, i);
2963
2964
0
    nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
2965
2966
0
    printf_ln("%s %"PRIu32"",
2967
0
           oid_to_hex(&oid), get_be32(bitmap_git->hashes + index_pos));
2968
0
  }
2969
2970
0
cleanup:
2971
0
  free_bitmap_index(bitmap_git);
2972
2973
0
  return 0;
2974
0
}
2975
2976
static void bit_pos_to_object_id(struct bitmap_index *bitmap_git,
2977
         uint32_t bit_pos,
2978
         struct object_id *oid)
2979
0
{
2980
0
  uint32_t index_pos;
2981
2982
0
  if (bitmap_is_midx(bitmap_git))
2983
0
    index_pos = pack_pos_to_midx(bitmap_git->midx, bit_pos);
2984
0
  else
2985
0
    index_pos = pack_pos_to_index(bitmap_git->pack, bit_pos);
2986
2987
0
  nth_bitmap_object_oid(bitmap_git, oid, index_pos);
2988
0
}
2989
2990
int test_bitmap_pseudo_merges(struct repository *r)
2991
0
{
2992
0
  struct bitmap_index *bitmap_git;
2993
0
  uint32_t i;
2994
2995
0
  bitmap_git = prepare_bitmap_git(r);
2996
0
  if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
2997
0
    goto cleanup;
2998
2999
0
  for (i = 0; i < bitmap_git->pseudo_merges.nr; i++) {
3000
0
    struct pseudo_merge *merge;
3001
0
    struct ewah_bitmap *commits_bitmap, *merge_bitmap;
3002
3003
0
    merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3004
0
           &bitmap_git->pseudo_merges.v[i]);
3005
0
    commits_bitmap = merge->commits;
3006
0
    merge_bitmap = pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
3007
0
               merge);
3008
3009
0
    printf("at=%"PRIuMAX", commits=%"PRIuMAX", objects=%"PRIuMAX"\n",
3010
0
           (uintmax_t)merge->at,
3011
0
           (uintmax_t)ewah_bitmap_popcount(commits_bitmap),
3012
0
           (uintmax_t)ewah_bitmap_popcount(merge_bitmap));
3013
0
  }
3014
3015
0
cleanup:
3016
0
  free_bitmap_index(bitmap_git);
3017
0
  return 0;
3018
0
}
3019
3020
static void dump_ewah_object_ids(struct bitmap_index *bitmap_git,
3021
         struct ewah_bitmap *bitmap)
3022
3023
0
{
3024
0
  struct ewah_iterator it;
3025
0
  eword_t word;
3026
0
  uint32_t pos = 0;
3027
3028
0
  ewah_iterator_init(&it, bitmap);
3029
3030
0
  while (ewah_iterator_next(&word, &it)) {
3031
0
    struct object_id oid;
3032
0
    uint32_t offset;
3033
3034
0
    for (offset = 0; offset < BITS_IN_EWORD; offset++) {
3035
0
      if (!(word >> offset))
3036
0
        break;
3037
3038
0
      offset += ewah_bit_ctz64(word >> offset);
3039
3040
0
      bit_pos_to_object_id(bitmap_git, pos + offset, &oid);
3041
0
      printf("%s\n", oid_to_hex(&oid));
3042
0
    }
3043
0
    pos += BITS_IN_EWORD;
3044
0
  }
3045
0
}
3046
3047
int test_bitmap_pseudo_merge_commits(struct repository *r, uint32_t n)
3048
0
{
3049
0
  struct bitmap_index *bitmap_git;
3050
0
  struct pseudo_merge *merge;
3051
0
  int ret = 0;
3052
3053
0
  bitmap_git = prepare_bitmap_git(r);
3054
0
  if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3055
0
    goto cleanup;
3056
3057
0
  if (n >= bitmap_git->pseudo_merges.nr) {
3058
0
    ret = error(_("pseudo-merge index out of range "
3059
0
            "(%"PRIu32" >= %"PRIuMAX")"),
3060
0
          n, (uintmax_t)bitmap_git->pseudo_merges.nr);
3061
0
    goto cleanup;
3062
0
  }
3063
3064
0
  merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3065
0
         &bitmap_git->pseudo_merges.v[n]);
3066
0
  dump_ewah_object_ids(bitmap_git, merge->commits);
3067
3068
0
cleanup:
3069
0
  free_bitmap_index(bitmap_git);
3070
0
  return ret;
3071
0
}
3072
3073
int test_bitmap_pseudo_merge_objects(struct repository *r, uint32_t n)
3074
0
{
3075
0
  struct bitmap_index *bitmap_git;
3076
0
  struct pseudo_merge *merge;
3077
0
  int ret = 0;
3078
3079
0
  bitmap_git = prepare_bitmap_git(r);
3080
0
  if (!bitmap_git || !bitmap_git->pseudo_merges.nr)
3081
0
    goto cleanup;
3082
3083
0
  if (n >= bitmap_git->pseudo_merges.nr) {
3084
0
    ret = error(_("pseudo-merge index out of range "
3085
0
            "(%"PRIu32" >= %"PRIuMAX")"),
3086
0
          n, (uintmax_t)bitmap_git->pseudo_merges.nr);
3087
0
    goto cleanup;
3088
0
  }
3089
3090
0
  merge = use_pseudo_merge(&bitmap_git->pseudo_merges,
3091
0
         &bitmap_git->pseudo_merges.v[n]);
3092
3093
0
  dump_ewah_object_ids(bitmap_git,
3094
0
           pseudo_merge_bitmap(&bitmap_git->pseudo_merges,
3095
0
             merge));
3096
3097
0
cleanup:
3098
0
  free_bitmap_index(bitmap_git);
3099
0
  return ret;
3100
0
}
3101
3102
int rebuild_bitmap(const uint32_t *reposition,
3103
       struct ewah_bitmap *source,
3104
       struct bitmap *dest)
3105
0
{
3106
0
  uint32_t pos = 0;
3107
0
  struct ewah_iterator it;
3108
0
  eword_t word;
3109
3110
0
  ewah_iterator_init(&it, source);
3111
3112
0
  while (ewah_iterator_next(&word, &it)) {
3113
0
    uint32_t offset, bit_pos;
3114
3115
0
    for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
3116
0
      if ((word >> offset) == 0)
3117
0
        break;
3118
3119
0
      offset += ewah_bit_ctz64(word >> offset);
3120
3121
0
      bit_pos = reposition[pos + offset];
3122
0
      if (bit_pos > 0)
3123
0
        bitmap_set(dest, bit_pos - 1);
3124
0
      else /* can't reuse, we don't have the object */
3125
0
        return -1;
3126
0
    }
3127
3128
0
    pos += BITS_IN_EWORD;
3129
0
  }
3130
0
  return 0;
3131
0
}
3132
3133
uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git,
3134
        struct packing_data *mapping)
3135
0
{
3136
0
  struct repository *r = bitmap_repo(bitmap_git);
3137
0
  uint32_t i, num_objects;
3138
0
  uint32_t *reposition;
3139
3140
0
  if (!bitmap_is_midx(bitmap_git))
3141
0
    load_reverse_index(r, bitmap_git);
3142
0
  else if (load_midx_revindex(bitmap_git->midx))
3143
0
    BUG("rebuild_existing_bitmaps: missing required rev-cache "
3144
0
        "extension");
3145
3146
0
  num_objects = bitmap_num_objects_total(bitmap_git);
3147
0
  CALLOC_ARRAY(reposition, num_objects);
3148
3149
0
  for (i = 0; i < num_objects; ++i) {
3150
0
    struct object_id oid;
3151
0
    struct object_entry *oe;
3152
0
    uint32_t index_pos;
3153
3154
0
    if (bitmap_is_midx(bitmap_git))
3155
0
      index_pos = pack_pos_to_midx(bitmap_git->midx, i);
3156
0
    else
3157
0
      index_pos = pack_pos_to_index(bitmap_git->pack, i);
3158
0
    nth_bitmap_object_oid(bitmap_git, &oid, index_pos);
3159
0
    oe = packlist_find(mapping, &oid);
3160
3161
0
    if (oe) {
3162
0
      reposition[i] = oe_in_pack_pos(mapping, oe) + 1;
3163
0
      if (!oe->hash)
3164
0
        oe->hash = bitmap_name_hash(bitmap_git, index_pos);
3165
0
    }
3166
0
  }
3167
3168
0
  return reposition;
3169
0
}
3170
3171
void free_bitmap_index(struct bitmap_index *b)
3172
0
{
3173
0
  if (!b)
3174
0
    return;
3175
3176
0
  if (b->map)
3177
0
    munmap(b->map, b->map_size);
3178
0
  ewah_pool_free(b->commits);
3179
0
  ewah_pool_free(b->trees);
3180
0
  ewah_pool_free(b->blobs);
3181
0
  ewah_pool_free(b->tags);
3182
0
  free(b->commits_all);
3183
0
  free(b->trees_all);
3184
0
  free(b->blobs_all);
3185
0
  free(b->tags_all);
3186
0
  if (b->bitmaps) {
3187
0
    struct stored_bitmap *sb;
3188
0
    kh_foreach_value(b->bitmaps, sb, {
3189
0
      ewah_pool_free(sb->root);
3190
0
      free(sb);
3191
0
    });
3192
0
  }
3193
0
  kh_destroy_oid_map(b->bitmaps);
3194
0
  free(b->ext_index.objects);
3195
0
  free(b->ext_index.hashes);
3196
0
  kh_destroy_oid_pos(b->ext_index.positions);
3197
0
  bitmap_free(b->result);
3198
0
  bitmap_free(b->haves);
3199
0
  if (bitmap_is_midx(b)) {
3200
    /*
3201
     * Multi-pack bitmaps need to have resources associated with
3202
     * their on-disk reverse indexes unmapped so that stale .rev and
3203
     * .bitmap files can be removed.
3204
     *
3205
     * Unlike pack-based bitmaps, multi-pack bitmaps can be read and
3206
     * written in the same 'git multi-pack-index write --bitmap'
3207
     * process. Close resources so they can be removed safely on
3208
     * platforms like Windows.
3209
     */
3210
0
    close_midx_revindex(b->midx);
3211
0
  }
3212
0
  free_pseudo_merge_map(&b->pseudo_merges);
3213
0
  free_bitmap_index(b->base);
3214
0
  free(b);
3215
0
}
3216
3217
int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
3218
            const struct object_id *oid)
3219
0
{
3220
0
  return bitmap_git &&
3221
0
    bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
3222
0
}
3223
3224
static off_t get_disk_usage_for_type(struct bitmap_index *bitmap_git,
3225
             enum object_type object_type)
3226
0
{
3227
0
  struct bitmap *result = bitmap_git->result;
3228
0
  off_t total = 0;
3229
0
  struct ewah_or_iterator it;
3230
0
  eword_t filter;
3231
0
  size_t i;
3232
3233
0
  init_type_iterator(&it, bitmap_git, object_type);
3234
0
  for (i = 0; i < result->word_alloc &&
3235
0
      ewah_or_iterator_next(&filter, &it); i++) {
3236
0
    eword_t word = result->words[i] & filter;
3237
0
    size_t base = (i * BITS_IN_EWORD);
3238
0
    unsigned offset;
3239
3240
0
    if (!word)
3241
0
      continue;
3242
3243
0
    for (offset = 0; offset < BITS_IN_EWORD; offset++) {
3244
0
      if ((word >> offset) == 0)
3245
0
        break;
3246
3247
0
      offset += ewah_bit_ctz64(word >> offset);
3248
3249
0
      if (bitmap_is_midx(bitmap_git)) {
3250
0
        uint32_t pack_pos;
3251
0
        uint32_t midx_pos = pack_pos_to_midx(bitmap_git->midx, base + offset);
3252
0
        off_t offset = nth_midxed_offset(bitmap_git->midx, midx_pos);
3253
3254
0
        uint32_t pack_id = nth_midxed_pack_int_id(bitmap_git->midx, midx_pos);
3255
0
        struct packed_git *pack = nth_midxed_pack(bitmap_git->midx, pack_id);
3256
3257
0
        if (offset_to_pack_pos(pack, offset, &pack_pos) < 0) {
3258
0
          struct object_id oid;
3259
0
          nth_midxed_object_oid(&oid, bitmap_git->midx, midx_pos);
3260
3261
0
          die(_("could not find '%s' in pack '%s' at offset %"PRIuMAX),
3262
0
              oid_to_hex(&oid),
3263
0
              pack->pack_name,
3264
0
              (uintmax_t)offset);
3265
0
        }
3266
3267
0
        total += pack_pos_to_offset(pack, pack_pos + 1) - offset;
3268
0
      } else {
3269
0
        size_t pos = base + offset;
3270
0
        total += pack_pos_to_offset(bitmap_git->pack, pos + 1) -
3271
0
           pack_pos_to_offset(bitmap_git->pack, pos);
3272
0
      }
3273
0
    }
3274
0
  }
3275
3276
0
  ewah_or_iterator_release(&it);
3277
3278
0
  return total;
3279
0
}
3280
3281
static off_t get_disk_usage_for_extended(struct bitmap_index *bitmap_git)
3282
0
{
3283
0
  struct bitmap *result = bitmap_git->result;
3284
0
  struct eindex *eindex = &bitmap_git->ext_index;
3285
0
  off_t total = 0;
3286
0
  struct object_info oi = OBJECT_INFO_INIT;
3287
0
  off_t object_size;
3288
0
  size_t i;
3289
3290
0
  oi.disk_sizep = &object_size;
3291
3292
0
  for (i = 0; i < eindex->count; i++) {
3293
0
    struct object *obj = eindex->objects[i];
3294
3295
0
    if (!bitmap_get(result,
3296
0
        st_add(bitmap_num_objects_total(bitmap_git),
3297
0
               i)))
3298
0
      continue;
3299
3300
0
    if (odb_read_object_info_extended(bitmap_repo(bitmap_git)->objects,
3301
0
              &obj->oid, &oi, 0) < 0)
3302
0
      die(_("unable to get disk usage of '%s'"),
3303
0
          oid_to_hex(&obj->oid));
3304
3305
0
    total += object_size;
3306
0
  }
3307
0
  return total;
3308
0
}
3309
3310
off_t get_disk_usage_from_bitmap(struct bitmap_index *bitmap_git,
3311
         struct rev_info *revs)
3312
0
{
3313
0
  off_t total = 0;
3314
3315
0
  total += get_disk_usage_for_type(bitmap_git, OBJ_COMMIT);
3316
0
  if (revs->tree_objects)
3317
0
    total += get_disk_usage_for_type(bitmap_git, OBJ_TREE);
3318
0
  if (revs->blob_objects)
3319
0
    total += get_disk_usage_for_type(bitmap_git, OBJ_BLOB);
3320
0
  if (revs->tag_objects)
3321
0
    total += get_disk_usage_for_type(bitmap_git, OBJ_TAG);
3322
3323
0
  total += get_disk_usage_for_extended(bitmap_git);
3324
3325
0
  return total;
3326
0
}
3327
3328
int bitmap_is_midx(struct bitmap_index *bitmap_git)
3329
0
{
3330
0
  return !!bitmap_git->midx;
3331
0
}
3332
3333
static const struct string_list *bitmap_preferred_tips(struct repository *r)
3334
0
{
3335
0
  const struct string_list *dest;
3336
3337
0
  if (!repo_config_get_string_multi(r, "pack.preferbitmaptips", &dest))
3338
0
    return dest;
3339
0
  return NULL;
3340
0
}
3341
3342
void for_each_preferred_bitmap_tip(struct repository *repo,
3343
           refs_for_each_cb cb, void *cb_data)
3344
0
{
3345
0
  struct refs_for_each_ref_options opts = { 0 };
3346
0
  struct string_list_item *item;
3347
0
  const struct string_list *preferred_tips;
3348
0
  struct strbuf buf = STRBUF_INIT;
3349
3350
0
  preferred_tips = bitmap_preferred_tips(repo);
3351
0
  if (!preferred_tips)
3352
0
    return;
3353
3354
0
  for_each_string_list_item(item, preferred_tips) {
3355
0
    opts.prefix = item->string;
3356
3357
0
    if (!ends_with(opts.prefix, "/")) {
3358
0
      strbuf_reset(&buf);
3359
0
      strbuf_addf(&buf, "%s/", opts.prefix);
3360
0
      opts.prefix = buf.buf;
3361
0
    }
3362
3363
0
    refs_for_each_ref_ext(get_main_ref_store(repo),
3364
0
              cb, cb_data, &opts);
3365
0
  }
3366
3367
0
  strbuf_release(&buf);
3368
0
}
3369
3370
int bitmap_is_preferred_refname(struct repository *r, const char *refname)
3371
0
{
3372
0
  const struct string_list *preferred_tips = bitmap_preferred_tips(r);
3373
0
  struct string_list_item *item;
3374
3375
0
  if (!preferred_tips)
3376
0
    return 0;
3377
3378
0
  for_each_string_list_item(item, preferred_tips) {
3379
0
    if (starts_with(refname, item->string))
3380
0
      return 1;
3381
0
  }
3382
3383
0
  return 0;
3384
0
}
3385
3386
static int verify_bitmap_file(const struct git_hash_algo *algop,
3387
            const char *name)
3388
0
{
3389
0
  struct stat st;
3390
0
  unsigned char *data;
3391
0
  int fd = git_open(name);
3392
0
  int res = 0;
3393
3394
  /* It is OK to not have the file. */
3395
0
  if (fd < 0 || fstat(fd, &st)) {
3396
0
    if (fd >= 0)
3397
0
      close(fd);
3398
0
    return 0;
3399
0
  }
3400
3401
0
  data = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
3402
0
  close(fd);
3403
0
  if (!hashfile_checksum_valid(algop, data, st.st_size))
3404
0
    res = error(_("bitmap file '%s' has invalid checksum"),
3405
0
          name);
3406
3407
0
  munmap(data, st.st_size);
3408
0
  return res;
3409
0
}
3410
3411
int verify_bitmap_files(struct odb_source_packed *source)
3412
0
{
3413
0
  struct packfile_list_entry *e;
3414
0
  struct multi_pack_index *m;
3415
0
  int res = 0;
3416
3417
0
  m = get_multi_pack_index(source);
3418
0
  if (m) {
3419
0
    char *midx_bitmap_name = midx_bitmap_filename(m);
3420
0
    res |= verify_bitmap_file(source->base.odb->repo->hash_algo, midx_bitmap_name);
3421
0
    free(midx_bitmap_name);
3422
0
  }
3423
3424
0
  for (e = packfile_store_get_packs(source); e; e = e->next) {
3425
0
    char *pack_bitmap_name = pack_bitmap_filename(e->pack);
3426
0
    res |= verify_bitmap_file(source->base.odb->repo->hash_algo, pack_bitmap_name);
3427
0
    free(pack_bitmap_name);
3428
0
  }
3429
3430
0
  return res;
3431
0
}