Coverage Report

Created: 2024-09-08 06:24

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