Coverage Report

Created: 2026-11-12 06:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/commit-graph.c
Line
Count
Source
1
#define DISABLE_SIGN_COMPARE_WARNINGS
2
3
#include "git-compat-util.h"
4
#include "abspath.h"
5
#include "config.h"
6
#include "csum-file.h"
7
#include "environment.h"
8
#include "gettext.h"
9
#include "hex.h"
10
#include "lockfile.h"
11
#include "packfile.h"
12
#include "commit.h"
13
#include "object.h"
14
#include "refs.h"
15
#include "hash-lookup.h"
16
#include "commit-graph.h"
17
#include "odb.h"
18
#include "odb/source-files.h"
19
#include "oid-array.h"
20
#include "path.h"
21
#include "alloc.h"
22
#include "hashmap.h"
23
#include "replace-object.h"
24
#include "progress.h"
25
#include "bloom.h"
26
#include "commit-slab.h"
27
#include "shallow.h"
28
#include "json-writer.h"
29
#include "trace2.h"
30
#include "tree.h"
31
#include "chunk-format.h"
32
33
void git_test_write_commit_graph_or_die(struct repository *repo)
34
0
{
35
0
  int flags = 0;
36
0
  if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
37
0
    return;
38
39
0
  if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
40
0
    flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
41
42
0
  if (write_commit_graph_reachable(repo, repo->objects->source->path, flags, NULL))
43
0
    die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
44
0
}
45
46
0
#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
47
0
#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
48
0
#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
49
0
#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
50
0
#define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
51
0
#define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
52
0
#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
53
0
#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
54
0
#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
55
0
#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
56
57
0
#define GRAPH_VERSION_1 0x1
58
0
#define GRAPH_VERSION GRAPH_VERSION_1
59
60
0
#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61
0
#define GRAPH_EDGE_LAST_MASK 0x7fffffff
62
0
#define GRAPH_PARENT_NONE 0x70000000
63
64
0
#define GRAPH_LAST_EDGE 0x80000000
65
66
0
#define GRAPH_HEADER_SIZE 8
67
0
#define GRAPH_FANOUT_SIZE (4 * 256)
68
69
0
#define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
70
71
/* Remember to update object flag allocation in object.h */
72
0
#define REACHABLE       (1u<<15)
73
74
0
define_commit_slab(topo_level_slab, uint32_t);
Unexecuted instantiation: commit-graph.c:topo_level_slab_at_peek
Unexecuted instantiation: commit-graph.c:init_topo_level_slab_with_stride
Unexecuted instantiation: commit-graph.c:clear_topo_level_slab
75
0
76
0
/* Keep track of the order in which commits are added to our list. */
77
0
define_commit_slab(commit_pos, int);
78
0
static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
79
0
80
0
static size_t graph_data_width(const struct git_hash_algo *algop)
81
0
{
82
0
  return algop->rawsz + 16;
83
0
}
84
85
static size_t graph_min_size(const struct git_hash_algo *algop)
86
0
{
87
0
  return GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE + GRAPH_FANOUT_SIZE + algop->rawsz;
88
0
}
89
90
static void set_commit_pos(struct repository *r, const struct object_id *oid)
91
0
{
92
0
  static int32_t max_pos;
93
0
  struct commit *commit = lookup_commit(r, oid);
94
95
0
  if (!commit)
96
0
    return; /* should never happen, but be lenient */
97
98
0
  *commit_pos_at(&commit_pos, commit) = max_pos++;
99
0
}
100
101
static int commit_pos_cmp(const void *va, const void *vb)
102
0
{
103
0
  const struct commit *a = *(const struct commit **)va;
104
0
  const struct commit *b = *(const struct commit **)vb;
105
0
  return commit_pos_at(&commit_pos, a) -
106
0
         commit_pos_at(&commit_pos, b);
107
0
}
108
109
0
define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
Unexecuted instantiation: commit-graph.c:commit_graph_data_slab_at_peek
Unexecuted instantiation: commit-graph.c:clear_commit_graph_data_slab
110
0
static struct commit_graph_data_slab commit_graph_data_slab =
111
0
  COMMIT_SLAB_INIT(1, commit_graph_data_slab);
112
0
113
0
static int get_configured_generation_version(struct repository *r)
114
0
{
115
0
  int version = 2;
116
0
  repo_config_get_int(r, "commitgraph.generationversion", &version);
117
0
  return version;
118
0
}
119
120
uint32_t commit_graph_position(const struct commit *c)
121
0
{
122
0
  struct commit_graph_data *data =
123
0
    commit_graph_data_slab_peek(&commit_graph_data_slab, c);
124
125
0
  return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
126
0
}
127
128
timestamp_t commit_graph_generation(const struct commit *c)
129
0
{
130
0
  struct commit_graph_data *data =
131
0
    commit_graph_data_slab_peek(&commit_graph_data_slab, c);
132
133
0
  if (data && data->generation)
134
0
    return data->generation;
135
136
0
  return GENERATION_NUMBER_INFINITY;
137
0
}
138
139
static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
140
0
{
141
0
  struct commit_graph_data *data =
142
0
    commit_graph_data_slab_peek(&commit_graph_data_slab, c);
143
144
0
  if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
145
0
    return GENERATION_NUMBER_INFINITY;
146
0
  return data->generation;
147
0
}
148
149
static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
150
0
{
151
0
  unsigned int i, nth_slab;
152
0
  struct commit_graph_data *data =
153
0
    commit_graph_data_slab_peek(&commit_graph_data_slab, c);
154
155
0
  if (data)
156
0
    return data;
157
158
0
  nth_slab = c->index / commit_graph_data_slab.slab_size;
159
0
  data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
160
161
  /*
162
   * commit-slab initializes elements with zero, overwrite this with
163
   * COMMIT_NOT_FROM_GRAPH for graph_pos.
164
   *
165
   * We avoid initializing generation with checking if graph position
166
   * is not COMMIT_NOT_FROM_GRAPH.
167
   */
168
0
  for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
169
0
    commit_graph_data_slab.slab[nth_slab][i].graph_pos =
170
0
      COMMIT_NOT_FROM_GRAPH;
171
0
  }
172
173
0
  return data;
174
0
}
175
176
/*
177
 * Should be used only while writing commit-graph as it compares
178
 * generation value of commits by directly accessing commit-slab.
179
 */
180
static int commit_gen_cmp(const void *va, const void *vb)
181
0
{
182
0
  const struct commit *a = *(const struct commit **)va;
183
0
  const struct commit *b = *(const struct commit **)vb;
184
185
0
  const timestamp_t generation_a = commit_graph_data_at(a)->generation;
186
0
  const timestamp_t generation_b = commit_graph_data_at(b)->generation;
187
  /* lower generation commits first */
188
0
  if (generation_a < generation_b)
189
0
    return -1;
190
0
  else if (generation_a > generation_b)
191
0
    return 1;
192
193
  /* use date as a heuristic when generations are equal */
194
0
  if (a->date < b->date)
195
0
    return -1;
196
0
  else if (a->date > b->date)
197
0
    return 1;
198
0
  return 0;
199
0
}
200
201
char *get_commit_graph_filename(const char *dir)
202
0
{
203
0
  return xstrfmt("%s/info/commit-graph", dir);
204
0
}
205
206
static char *get_split_graph_filename(const char *dir,
207
              const char *oid_hex)
208
0
{
209
0
  return xstrfmt("%s/info/commit-graphs/graph-%s.graph", dir,
210
0
           oid_hex);
211
0
}
212
213
char *get_commit_graph_chain_filename(const char *dir)
214
0
{
215
0
  return xstrfmt("%s/info/commit-graphs/commit-graph-chain", dir);
216
0
}
217
218
static struct commit_graph *alloc_commit_graph(void)
219
0
{
220
0
  struct commit_graph *g = xcalloc(1, sizeof(*g));
221
222
0
  return g;
223
0
}
224
225
static int commit_graph_compatible(struct repository *r)
226
0
{
227
0
  if (!r->gitdir)
228
0
    return 0;
229
230
0
  if (replace_refs_enabled(r)) {
231
0
    prepare_replace_object(r);
232
0
    if (oidmap_get_size(&r->objects->replace_map))
233
0
      return 0;
234
0
  }
235
236
0
  prepare_commit_graft(r);
237
0
  if (r->parsed_objects &&
238
0
      (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
239
0
    return 0;
240
0
  if (is_repository_shallow(r))
241
0
    return 0;
242
243
0
  return 1;
244
0
}
245
246
int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
247
0
{
248
0
  *fd = git_open(graph_file);
249
0
  if (*fd < 0)
250
0
    return 0;
251
0
  if (fstat(*fd, st)) {
252
0
    close(*fd);
253
0
    return 0;
254
0
  }
255
0
  return 1;
256
0
}
257
258
struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo,
259
             const char *dir,
260
             int fd, struct stat *st)
261
0
{
262
0
  void *graph_map;
263
0
  size_t graph_size;
264
0
  struct commit_graph *ret;
265
266
0
  graph_size = xsize_t(st->st_size);
267
268
0
  if (graph_size < graph_min_size(repo->hash_algo)) {
269
0
    close(fd);
270
0
    error(_("commit-graph file is too small"));
271
0
    return NULL;
272
0
  }
273
0
  graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
274
0
  close(fd);
275
276
0
  ret = parse_commit_graph(repo, graph_map, graph_size);
277
0
  if (ret)
278
0
    ret->dir = absolute_pathdup(dir);
279
0
  else
280
0
    munmap(graph_map, graph_size);
281
282
0
  return ret;
283
0
}
284
285
static int graph_read_oid_fanout(const unsigned char *chunk_start,
286
         size_t chunk_size, void *data)
287
0
{
288
0
  struct commit_graph *g = data;
289
0
  int i;
290
291
0
  if (chunk_size != 256 * sizeof(uint32_t))
292
0
    return error(_("commit-graph oid fanout chunk is wrong size"));
293
0
  g->chunk_oid_fanout = (const uint32_t *)chunk_start;
294
0
  g->num_commits = ntohl(g->chunk_oid_fanout[255]);
295
296
0
  for (i = 0; i < 255; i++) {
297
0
    uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
298
0
    uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
299
300
0
    if (oid_fanout1 > oid_fanout2) {
301
0
      error(_("commit-graph fanout values out of order"));
302
0
      return 1;
303
0
    }
304
0
  }
305
306
0
  return 0;
307
0
}
308
309
static int graph_read_oid_lookup(const unsigned char *chunk_start,
310
         size_t chunk_size, void *data)
311
0
{
312
0
  struct commit_graph *g = data;
313
0
  g->chunk_oid_lookup = chunk_start;
314
0
  if (chunk_size / g->hash_algo->rawsz != g->num_commits)
315
0
    return error(_("commit-graph OID lookup chunk is the wrong size"));
316
0
  return 0;
317
0
}
318
319
static int graph_read_commit_data(const unsigned char *chunk_start,
320
          size_t chunk_size, void *data)
321
0
{
322
0
  struct commit_graph *g = data;
323
0
  if (chunk_size / graph_data_width(g->hash_algo) != g->num_commits)
324
0
    return error(_("commit-graph commit data chunk is wrong size"));
325
0
  g->chunk_commit_data = chunk_start;
326
0
  return 0;
327
0
}
328
329
static int graph_read_generation_data(const unsigned char *chunk_start,
330
              size_t chunk_size, void *data)
331
0
{
332
0
  struct commit_graph *g = data;
333
0
  if (chunk_size / sizeof(uint32_t) != g->num_commits)
334
0
    return error(_("commit-graph generations chunk is wrong size"));
335
0
  g->chunk_generation_data = chunk_start;
336
0
  return 0;
337
0
}
338
339
static int graph_read_bloom_index(const unsigned char *chunk_start,
340
          size_t chunk_size, void *data)
341
0
{
342
0
  struct commit_graph *g = data;
343
0
  if (chunk_size / 4 != g->num_commits) {
344
0
    warning(_("commit-graph changed-path index chunk is too small"));
345
0
    return -1;
346
0
  }
347
0
  g->chunk_bloom_indexes = chunk_start;
348
0
  return 0;
349
0
}
350
351
static int graph_read_bloom_data(const unsigned char *chunk_start,
352
          size_t chunk_size, void *data)
353
0
{
354
0
  struct commit_graph *g = data;
355
356
0
  if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
357
0
    warning(_("ignoring too-small changed-path chunk"
358
0
      " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file"),
359
0
      (uintmax_t)chunk_size,
360
0
      (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
361
0
    return -1;
362
0
  }
363
364
0
  g->chunk_bloom_data = chunk_start;
365
0
  g->chunk_bloom_data_size = chunk_size;
366
367
0
  g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
368
0
  g->bloom_filter_settings->hash_version = get_be32(chunk_start);
369
0
  g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
370
0
  g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
371
0
  g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
372
373
0
  return 0;
374
0
}
375
376
struct commit_graph *parse_commit_graph(struct repository *r,
377
          void *graph_map, size_t graph_size)
378
0
{
379
0
  const unsigned char *data;
380
0
  struct commit_graph *graph;
381
0
  uint32_t graph_signature;
382
0
  unsigned char graph_version, hash_version;
383
0
  struct chunkfile *cf = NULL;
384
385
0
  if (!graph_map)
386
0
    return NULL;
387
388
0
  if (graph_size < graph_min_size(r->hash_algo))
389
0
    return NULL;
390
391
0
  data = (const unsigned char *)graph_map;
392
393
0
  graph_signature = get_be32(data);
394
0
  if (graph_signature != GRAPH_SIGNATURE) {
395
0
    error(_("commit-graph signature %X does not match signature %X"),
396
0
          graph_signature, GRAPH_SIGNATURE);
397
0
    return NULL;
398
0
  }
399
400
0
  graph_version = *(unsigned char*)(data + 4);
401
0
  if (graph_version != GRAPH_VERSION) {
402
0
    error(_("commit-graph version %X does not match version %X"),
403
0
          graph_version, GRAPH_VERSION);
404
0
    return NULL;
405
0
  }
406
407
0
  hash_version = *(unsigned char*)(data + 5);
408
0
  if (hash_version != oid_version(r->hash_algo)) {
409
0
    error(_("commit-graph hash version %X does not match version %X"),
410
0
          hash_version, oid_version(r->hash_algo));
411
0
    return NULL;
412
0
  }
413
414
0
  graph = alloc_commit_graph();
415
416
0
  graph->repo = r;
417
0
  graph->hash_algo = r->hash_algo;
418
0
  graph->num_chunks = *(unsigned char*)(data + 6);
419
0
  graph->data = graph_map;
420
0
  graph->data_len = graph_size;
421
422
0
  if (graph_size < GRAPH_HEADER_SIZE +
423
0
       (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
424
0
       GRAPH_FANOUT_SIZE + r->hash_algo->rawsz) {
425
0
    error(_("commit-graph file is too small to hold %u chunks"),
426
0
          graph->num_chunks);
427
0
    free(graph);
428
0
    return NULL;
429
0
  }
430
431
0
  cf = init_chunkfile(NULL);
432
433
0
  if (read_table_of_contents(cf, graph->data, graph_size,
434
0
           GRAPH_HEADER_SIZE, graph->num_chunks, 1))
435
0
    goto free_and_return;
436
437
0
  if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
438
0
    error(_("commit-graph required OID fanout chunk missing or corrupted"));
439
0
    goto free_and_return;
440
0
  }
441
0
  if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
442
0
    error(_("commit-graph required OID lookup chunk missing or corrupted"));
443
0
    goto free_and_return;
444
0
  }
445
0
  if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
446
0
    error(_("commit-graph required commit data chunk missing or corrupted"));
447
0
    goto free_and_return;
448
0
  }
449
450
0
  pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
451
0
       &graph->chunk_extra_edges_size);
452
0
  pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
453
0
       &graph->chunk_base_graphs_size);
454
455
0
  prepare_repo_settings(r);
456
457
0
  if (r->settings.commit_graph_generation_version >= 2) {
458
0
    read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
459
0
         graph_read_generation_data, graph);
460
0
    pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
461
0
         &graph->chunk_generation_data_overflow,
462
0
         &graph->chunk_generation_data_overflow_size);
463
464
0
    if (graph->chunk_generation_data)
465
0
      graph->read_generation_data = 1;
466
0
  }
467
468
0
  if (r->settings.commit_graph_changed_paths_version) {
469
0
    read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
470
0
         graph_read_bloom_index, graph);
471
0
    read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
472
0
         graph_read_bloom_data, graph);
473
0
  }
474
475
0
  if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
476
0
    init_bloom_filters();
477
0
  } else {
478
    /* We need both the bloom chunks to exist together. Else ignore the data */
479
0
    graph->chunk_bloom_indexes = NULL;
480
0
    graph->chunk_bloom_data = NULL;
481
0
    FREE_AND_NULL(graph->bloom_filter_settings);
482
0
  }
483
484
0
  oidread(&graph->oid, graph->data + graph->data_len - graph->hash_algo->rawsz,
485
0
    r->hash_algo);
486
487
0
  free_chunkfile(cf);
488
0
  return graph;
489
490
0
free_and_return:
491
0
  free_chunkfile(cf);
492
0
  free(graph->bloom_filter_settings);
493
0
  free(graph);
494
0
  return NULL;
495
0
}
496
497
static struct commit_graph *load_commit_graph_one(struct repository *repo,
498
              const char *dir,
499
              const char *graph_file)
500
0
{
501
0
  struct stat st;
502
0
  int fd;
503
0
  struct commit_graph *g;
504
0
  int open_ok = open_commit_graph(graph_file, &fd, &st);
505
506
0
  if (!open_ok)
507
0
    return NULL;
508
509
0
  g = load_commit_graph_one_fd_st(repo, dir, fd, &st);
510
0
  if (g)
511
0
    g->filename = absolute_pathdup(graph_file);
512
513
0
  return g;
514
0
}
515
516
static struct commit_graph *load_commit_graph_v1(struct repository *repo,
517
             const char *dir)
518
0
{
519
0
  char *graph_name = get_commit_graph_filename(dir);
520
0
  struct commit_graph *g = load_commit_graph_one(repo, dir, graph_name);
521
0
  free(graph_name);
522
523
0
  return g;
524
0
}
525
526
/*
527
 * returns 1 if and only if all graphs in the chain have
528
 * corrected commit dates stored in the generation_data chunk.
529
 */
530
static int validate_mixed_generation_chain(struct commit_graph *g)
531
0
{
532
0
  int read_generation_data = 1;
533
0
  struct commit_graph *p = g;
534
535
0
  while (read_generation_data && p) {
536
0
    read_generation_data = p->read_generation_data;
537
0
    p = p->base_graph;
538
0
  }
539
540
0
  if (read_generation_data)
541
0
    return 1;
542
543
0
  while (g) {
544
0
    g->read_generation_data = 0;
545
0
    g = g->base_graph;
546
0
  }
547
548
0
  return 0;
549
0
}
550
551
static void validate_mixed_bloom_settings(struct commit_graph *g)
552
0
{
553
0
  struct bloom_filter_settings *settings = NULL;
554
0
  for (; g; g = g->base_graph) {
555
0
    if (!g->bloom_filter_settings)
556
0
      continue;
557
0
    if (!settings) {
558
0
      settings = g->bloom_filter_settings;
559
0
      continue;
560
0
    }
561
562
0
    if (g->bloom_filter_settings->bits_per_entry != settings->bits_per_entry ||
563
0
        g->bloom_filter_settings->num_hashes != settings->num_hashes ||
564
0
        g->bloom_filter_settings->hash_version != settings->hash_version) {
565
0
      g->chunk_bloom_indexes = NULL;
566
0
      g->chunk_bloom_data = NULL;
567
0
      FREE_AND_NULL(g->bloom_filter_settings);
568
569
0
      warning(_("disabling Bloom filters for commit-graph "
570
0
          "layer '%s' due to incompatible settings"),
571
0
        oid_to_hex(&g->oid));
572
0
    }
573
0
  }
574
0
}
575
576
static int add_graph_to_chain(struct commit_graph *g,
577
            struct commit_graph *chain,
578
            struct object_id *oids,
579
            int n)
580
0
{
581
0
  struct commit_graph *cur_g = chain;
582
583
0
  if (n && !g->chunk_base_graphs) {
584
0
    warning(_("commit-graph has no base graphs chunk"));
585
0
    return 0;
586
0
  }
587
588
0
  if (g->chunk_base_graphs_size / g->hash_algo->rawsz < n) {
589
0
    warning(_("commit-graph base graphs chunk is too small"));
590
0
    return 0;
591
0
  }
592
593
0
  while (n) {
594
0
    n--;
595
596
0
    if (!cur_g ||
597
0
        !oideq(&oids[n], &cur_g->oid) ||
598
0
        !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n),
599
0
          g->hash_algo)) {
600
0
      warning(_("commit-graph chain does not match"));
601
0
      return 0;
602
0
    }
603
604
0
    cur_g = cur_g->base_graph;
605
0
  }
606
607
0
  if (chain) {
608
0
    if (unsigned_add_overflows(chain->num_commits,
609
0
             chain->num_commits_in_base)) {
610
0
      warning(_("commit count in base graph too high: %"PRIuMAX),
611
0
        (uintmax_t)chain->num_commits_in_base);
612
0
      return 0;
613
0
    }
614
0
    g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
615
0
  }
616
617
0
  g->base_graph = chain;
618
619
0
  return 1;
620
0
}
621
622
int open_commit_graph_chain(const char *chain_file,
623
          int *fd, struct stat *st,
624
          const struct git_hash_algo *hash_algo)
625
0
{
626
0
  *fd = git_open(chain_file);
627
0
  if (*fd < 0)
628
0
    return 0;
629
0
  if (fstat(*fd, st)) {
630
0
    close(*fd);
631
0
    return 0;
632
0
  }
633
0
  if (st->st_size < hash_algo->hexsz) {
634
0
    close(*fd);
635
0
    if (!st->st_size) {
636
      /* treat empty files the same as missing */
637
0
      errno = ENOENT;
638
0
    } else {
639
0
      warning(_("commit-graph chain file too small"));
640
0
      errno = EINVAL;
641
0
    }
642
0
    return 0;
643
0
  }
644
0
  return 1;
645
0
}
646
647
struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
648
               int fd, struct stat *st,
649
               int *incomplete_chain)
650
0
{
651
0
  struct commit_graph *graph_chain = NULL;
652
0
  struct strbuf line = STRBUF_INIT;
653
0
  struct object_id *oids;
654
0
  int i = 0, valid = 1, count;
655
0
  FILE *fp = xfdopen(fd, "r");
656
657
0
  count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
658
0
  CALLOC_ARRAY(oids, count);
659
660
0
  for (i = 0; i < count; i++) {
661
0
    struct odb_source_files *files;
662
663
0
    if (strbuf_getline_lf(&line, fp) == EOF)
664
0
      break;
665
666
0
    if (get_oid_hex_algop(line.buf, &oids[i], odb->repo->hash_algo)) {
667
0
      warning(_("invalid commit-graph chain: line '%s' not a hash"),
668
0
        line.buf);
669
0
      valid = 0;
670
0
      break;
671
0
    }
672
673
0
    valid = 0;
674
675
0
    files = odb_source_files_downcast(odb->source);
676
0
    for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
677
0
      char *graph_name = get_split_graph_filename(dir->abspath, line.buf);
678
0
      struct commit_graph *g = load_commit_graph_one(odb->repo, dir->abspath, graph_name);
679
680
0
      free(graph_name);
681
682
0
      if (g) {
683
0
        if (add_graph_to_chain(g, graph_chain, oids, i)) {
684
0
          graph_chain = g;
685
0
          valid = 1;
686
0
        } else {
687
0
          free_commit_graph(g);
688
0
        }
689
690
0
        break;
691
0
      }
692
0
    }
693
694
0
    if (!valid) {
695
0
      warning(_("unable to find all commit-graph files"));
696
0
      break;
697
0
    }
698
0
  }
699
700
0
  validate_mixed_generation_chain(graph_chain);
701
0
  validate_mixed_bloom_settings(graph_chain);
702
703
0
  free(oids);
704
0
  fclose(fp);
705
0
  strbuf_release(&line);
706
707
0
  *incomplete_chain = !valid;
708
0
  return graph_chain;
709
0
}
710
711
static struct commit_graph *load_commit_graph_chain(struct repository *repo,
712
                const char *dir)
713
0
{
714
0
  char *chain_file = get_commit_graph_chain_filename(dir);
715
0
  struct stat st;
716
0
  int fd;
717
0
  struct commit_graph *g = NULL;
718
719
0
  if (open_commit_graph_chain(chain_file, &fd, &st, repo->hash_algo)) {
720
0
    int incomplete;
721
    /* ownership of fd is taken over by load function */
722
0
    g = load_commit_graph_chain_fd_st(repo->objects, fd, &st, &incomplete);
723
0
  }
724
725
0
  free(chain_file);
726
0
  return g;
727
0
}
728
729
struct commit_graph *read_commit_graph_one(struct repository *repo,
730
             const char *dir)
731
0
{
732
0
  struct commit_graph *g = load_commit_graph_v1(repo, dir);
733
734
0
  if (!g)
735
0
    g = load_commit_graph_chain(repo, dir);
736
737
0
  return g;
738
0
}
739
740
/*
741
 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
742
 *
743
 * On the first invocation, this function attempts to load the commit
744
 * graph if the repository is configured to have one.
745
 */
746
struct commit_graph *prepare_commit_graph(struct repository *r)
747
0
{
748
0
  struct odb_source_files *files;
749
750
  /*
751
   * Early return if there is no object database or if the commit graph is
752
   * disabled.
753
   *
754
   * This must come before the "already attempted?" check below, because
755
   * we want to disable even an already-loaded graph file.
756
   */
757
0
  if (!r->objects || r->commit_graph_disabled)
758
0
    return NULL;
759
760
0
  if (r->objects->commit_graph_attempted)
761
0
    return r->objects->commit_graph;
762
0
  r->objects->commit_graph_attempted = 1;
763
764
0
  prepare_repo_settings(r);
765
766
0
  if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
767
0
      r->settings.core_commit_graph != 1)
768
    /*
769
     * This repository is not configured to use commit graphs, so
770
     * do not load one. (But report commit_graph_attempted anyway
771
     * so that commit graph loading is not attempted again for this
772
     * repository.)
773
     */
774
0
    return NULL;
775
776
0
  if (!commit_graph_compatible(r))
777
0
    return NULL;
778
779
0
  files = odb_source_files_downcast(r->objects->source);
780
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
781
0
    r->objects->commit_graph = read_commit_graph_one(r, dir->abspath);
782
0
    if (r->objects->commit_graph)
783
0
      break;
784
0
  }
785
786
0
  return r->objects->commit_graph;
787
0
}
788
789
int generation_numbers_enabled(struct repository *r)
790
0
{
791
0
  uint32_t first_generation;
792
0
  struct commit_graph *g;
793
794
0
  g = prepare_commit_graph(r);
795
0
  if (!g || !g->num_commits)
796
0
         return 0;
797
798
0
  first_generation = get_be32(g->chunk_commit_data +
799
0
            g->hash_algo->rawsz + 8) >> 2;
800
801
0
  return !!first_generation;
802
0
}
803
804
int corrected_commit_dates_enabled(struct repository *r)
805
0
{
806
0
  struct commit_graph *g;
807
808
0
  g = prepare_commit_graph(r);
809
0
  if (!g || !g->num_commits)
810
0
    return 0;
811
812
0
  return g->read_generation_data;
813
0
}
814
815
struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
816
0
{
817
0
  struct commit_graph *g;
818
819
0
  if (!prepare_commit_graph(r))
820
0
         return NULL;
821
822
0
  g = r->objects->commit_graph;
823
0
  while (g) {
824
0
    if (g->bloom_filter_settings)
825
0
      return g->bloom_filter_settings;
826
0
    g = g->base_graph;
827
0
  }
828
0
  return NULL;
829
0
}
830
831
void close_commit_graph(struct object_database *o)
832
0
{
833
0
  if (!o->commit_graph)
834
0
    return;
835
836
0
  clear_commit_graph_data_slab(&commit_graph_data_slab);
837
0
  deinit_bloom_filters();
838
0
  free_commit_graph(o->commit_graph);
839
0
  o->commit_graph = NULL;
840
0
}
841
842
static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
843
0
{
844
0
  return bsearch_hash(oid->hash, g->chunk_oid_fanout,
845
0
          g->chunk_oid_lookup, g->hash_algo->rawsz, pos);
846
0
}
847
848
static void load_oid_from_graph(struct commit_graph *g,
849
        uint32_t pos,
850
        struct object_id *oid)
851
0
{
852
0
  uint32_t lex_index;
853
854
0
  while (g && pos < g->num_commits_in_base)
855
0
    g = g->base_graph;
856
857
0
  if (!g)
858
0
    BUG("NULL commit-graph");
859
860
0
  if (pos >= g->num_commits + g->num_commits_in_base)
861
0
    die(_("invalid commit position. commit-graph is likely corrupt"));
862
863
0
  lex_index = pos - g->num_commits_in_base;
864
865
0
  oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, lex_index),
866
0
    g->hash_algo);
867
0
}
868
869
static struct commit_list **insert_parent_or_die(struct commit_graph *g,
870
             uint32_t pos,
871
             struct commit_list **pptr)
872
0
{
873
0
  struct commit *c;
874
0
  struct object_id oid;
875
876
0
  if (pos >= g->num_commits + g->num_commits_in_base)
877
0
    die("invalid parent position %"PRIu32, pos);
878
879
0
  load_oid_from_graph(g, pos, &oid);
880
0
  c = lookup_commit(g->repo, &oid);
881
0
  if (!c)
882
0
    die(_("could not find commit %s"), oid_to_hex(&oid));
883
0
  commit_graph_data_at(c)->graph_pos = pos;
884
0
  return &commit_list_insert(c, pptr)->next;
885
0
}
886
887
static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
888
0
{
889
0
  const unsigned char *commit_data;
890
0
  struct commit_graph_data *graph_data;
891
0
  uint32_t lex_index, offset_pos;
892
0
  uint64_t date_high, date_low, offset;
893
894
0
  while (pos < g->num_commits_in_base)
895
0
    g = g->base_graph;
896
897
0
  if (pos >= g->num_commits + g->num_commits_in_base)
898
0
    die(_("invalid commit position. commit-graph is likely corrupt"));
899
900
0
  lex_index = pos - g->num_commits_in_base;
901
0
  commit_data = g->chunk_commit_data + st_mult(graph_data_width(g->hash_algo), lex_index);
902
903
0
  graph_data = commit_graph_data_at(item);
904
0
  graph_data->graph_pos = pos;
905
906
0
  date_high = get_be32(commit_data + g->hash_algo->rawsz + 8) & 0x3;
907
0
  date_low = get_be32(commit_data + g->hash_algo->rawsz + 12);
908
0
  item->date = (timestamp_t)((date_high << 32) | date_low);
909
910
0
  if (g->read_generation_data) {
911
0
    offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
912
913
0
    if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
914
0
      if (!g->chunk_generation_data_overflow)
915
0
        die(_("commit-graph requires overflow generation data but has none"));
916
917
0
      offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
918
0
      if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
919
0
        die(_("commit-graph overflow generation data is too small"));
920
0
      graph_data->generation = item->date +
921
0
        get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
922
0
    } else
923
0
      graph_data->generation = item->date + offset;
924
0
  } else
925
0
    graph_data->generation = get_be32(commit_data + g->hash_algo->rawsz + 8) >> 2;
926
927
0
  if (g->topo_levels)
928
0
    *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_algo->rawsz + 8) >> 2;
929
0
}
930
931
static inline void set_commit_tree(struct commit *c, struct tree *t)
932
0
{
933
0
  c->maybe_tree = t;
934
0
}
935
936
static int fill_commit_in_graph(struct commit *item,
937
        struct commit_graph *g, uint32_t pos)
938
0
{
939
0
  uint32_t edge_value;
940
0
  uint32_t parent_data_pos;
941
0
  struct commit_list **pptr;
942
0
  const unsigned char *commit_data;
943
0
  uint32_t lex_index;
944
945
0
  while (pos < g->num_commits_in_base)
946
0
    g = g->base_graph;
947
948
0
  fill_commit_graph_info(item, g, pos);
949
950
0
  lex_index = pos - g->num_commits_in_base;
951
0
  commit_data = g->chunk_commit_data + st_mult(g->hash_algo->rawsz + 16, lex_index);
952
953
0
  item->object.parsed = 1;
954
955
0
  set_commit_tree(item, NULL);
956
957
0
  pptr = &item->parents;
958
959
0
  edge_value = get_be32(commit_data + g->hash_algo->rawsz);
960
0
  if (edge_value == GRAPH_PARENT_NONE)
961
0
    return 1;
962
0
  pptr = insert_parent_or_die(g, edge_value, pptr);
963
964
0
  edge_value = get_be32(commit_data + g->hash_algo->rawsz + 4);
965
0
  if (edge_value == GRAPH_PARENT_NONE)
966
0
    return 1;
967
0
  if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
968
0
    pptr = insert_parent_or_die(g, edge_value, pptr);
969
0
    return 1;
970
0
  }
971
972
0
  parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
973
0
  do {
974
0
    if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
975
0
      error(_("commit-graph extra-edges pointer out of bounds"));
976
0
      commit_list_free(item->parents);
977
0
      item->parents = NULL;
978
0
      item->object.parsed = 0;
979
0
      return 0;
980
0
    }
981
0
    edge_value = get_be32(g->chunk_extra_edges +
982
0
              sizeof(uint32_t) * parent_data_pos);
983
0
    pptr = insert_parent_or_die(g,
984
0
              edge_value & GRAPH_EDGE_LAST_MASK,
985
0
              pptr);
986
0
    parent_data_pos++;
987
0
  } while (!(edge_value & GRAPH_LAST_EDGE));
988
989
0
  return 1;
990
0
}
991
992
static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
993
0
{
994
0
  struct commit_graph *cur_g = g;
995
0
  uint32_t lex_index;
996
997
0
  while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
998
0
    cur_g = cur_g->base_graph;
999
1000
0
  if (cur_g) {
1001
0
    *pos = lex_index + cur_g->num_commits_in_base;
1002
0
    return 1;
1003
0
  }
1004
1005
0
  return 0;
1006
0
}
1007
1008
static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
1009
0
{
1010
0
  uint32_t graph_pos = commit_graph_position(item);
1011
0
  if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
1012
0
    *pos = graph_pos;
1013
0
    return 1;
1014
0
  } else {
1015
0
    return search_commit_pos_in_graph(&item->object.oid, g, pos);
1016
0
  }
1017
0
}
1018
1019
struct commit_graph *repo_find_commit_pos_in_graph(struct repository *r,
1020
               struct commit *c,
1021
               uint32_t *pos)
1022
0
{
1023
0
  struct commit_graph *g = prepare_commit_graph(r);
1024
0
  if (!g)
1025
0
    return NULL;
1026
0
  if (!find_commit_pos_in_graph(c, g, pos))
1027
0
    return NULL;
1028
0
  return g;
1029
0
}
1030
1031
struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1032
0
{
1033
0
  static int commit_graph_paranoia = -1;
1034
0
  struct commit_graph *g;
1035
0
  struct commit *commit;
1036
0
  uint32_t pos;
1037
1038
0
  if (commit_graph_paranoia == -1)
1039
0
    commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
1040
1041
0
  g = prepare_commit_graph(repo);
1042
0
  if (!g)
1043
0
    return NULL;
1044
0
  if (!search_commit_pos_in_graph(id, g, &pos))
1045
0
    return NULL;
1046
0
  if (commit_graph_paranoia && !odb_has_object(repo->objects, id, 0))
1047
0
    return NULL;
1048
1049
0
  commit = lookup_commit(repo, id);
1050
0
  if (!commit)
1051
0
    return NULL;
1052
0
  if (commit->object.parsed)
1053
0
    return commit;
1054
1055
0
  if (!fill_commit_in_graph(commit, g, pos))
1056
0
    return NULL;
1057
1058
0
  return commit;
1059
0
}
1060
1061
static int parse_commit_in_graph_one(struct commit_graph *g,
1062
             struct commit *item)
1063
0
{
1064
0
  uint32_t pos;
1065
1066
0
  if (item->object.parsed)
1067
0
    return 1;
1068
1069
0
  if (find_commit_pos_in_graph(item, g, &pos))
1070
0
    return fill_commit_in_graph(item, g, pos);
1071
1072
0
  return 0;
1073
0
}
1074
1075
int parse_commit_in_graph(struct repository *r, struct commit *item)
1076
0
{
1077
0
  static int checked_env = 0;
1078
0
  struct commit_graph *g;
1079
1080
0
  if (!checked_env &&
1081
0
      git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1082
0
    die("dying as requested by the '%s' variable on commit-graph parse!",
1083
0
        GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1084
0
  checked_env = 1;
1085
1086
0
  g = prepare_commit_graph(r);
1087
0
  if (!g)
1088
0
    return 0;
1089
0
  return parse_commit_in_graph_one(g, item);
1090
0
}
1091
1092
void load_commit_graph_info(struct repository *r, struct commit *item)
1093
0
{
1094
0
  struct commit_graph *g;
1095
0
  uint32_t pos;
1096
1097
0
  g = repo_find_commit_pos_in_graph(r, item, &pos);
1098
0
  if (g)
1099
0
    fill_commit_graph_info(item, g, pos);
1100
0
}
1101
1102
static struct tree *load_tree_for_commit(struct commit_graph *g,
1103
           struct commit *c)
1104
0
{
1105
0
  struct object_id oid;
1106
0
  const unsigned char *commit_data;
1107
0
  uint32_t graph_pos = commit_graph_position(c);
1108
1109
0
  while (graph_pos < g->num_commits_in_base)
1110
0
    g = g->base_graph;
1111
1112
0
  commit_data = g->chunk_commit_data +
1113
0
      st_mult(graph_data_width(g->hash_algo),
1114
0
        graph_pos - g->num_commits_in_base);
1115
1116
0
  oidread(&oid, commit_data, g->hash_algo);
1117
0
  set_commit_tree(c, lookup_tree(g->repo, &oid));
1118
1119
0
  return c->maybe_tree;
1120
0
}
1121
1122
static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
1123
             const struct commit *c)
1124
0
{
1125
0
  if (c->maybe_tree)
1126
0
    return c->maybe_tree;
1127
0
  if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1128
0
    BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1129
1130
0
  return load_tree_for_commit(g, (struct commit *)c);
1131
0
}
1132
1133
struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1134
0
{
1135
0
  return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
1136
0
}
1137
1138
struct write_commit_graph_context {
1139
  struct repository *r;
1140
  char *dir;
1141
  char *graph_name;
1142
  struct oid_array oids;
1143
  struct commit_stack commits;
1144
  int num_extra_edges;
1145
  int num_generation_data_overflows;
1146
  unsigned long approx_nr_objects;
1147
  struct progress *progress;
1148
  int progress_done;
1149
  uint64_t progress_cnt;
1150
1151
  char *base_graph_name;
1152
  int num_commit_graphs_before;
1153
  int num_commit_graphs_after;
1154
  char **commit_graph_filenames_before;
1155
  char **commit_graph_filenames_after;
1156
  char **commit_graph_hash_after;
1157
  uint32_t new_num_commits_in_base;
1158
  struct commit_graph *new_base_graph;
1159
1160
  unsigned append:1,
1161
     report_progress:1,
1162
     split:1,
1163
     changed_paths:1,
1164
     order_by_pack:1,
1165
     write_generation_data:1,
1166
     trust_generation_numbers:1;
1167
1168
  struct topo_level_slab *topo_levels;
1169
  const struct commit_graph_opts *opts;
1170
  size_t total_bloom_filter_data_size;
1171
  const struct bloom_filter_settings *bloom_settings;
1172
1173
  int count_bloom_filter_computed;
1174
  int count_bloom_filter_not_computed;
1175
  int count_bloom_filter_trunc_empty;
1176
  int count_bloom_filter_trunc_large;
1177
  int count_bloom_filter_upgraded;
1178
};
1179
1180
static int write_graph_chunk_fanout(struct hashfile *f,
1181
            void *data)
1182
0
{
1183
0
  struct write_commit_graph_context *ctx = data;
1184
0
  int i, count = 0;
1185
0
  struct commit **list = ctx->commits.items;
1186
1187
  /*
1188
   * Write the first-level table (the list is sorted,
1189
   * but we use a 256-entry lookup to be able to avoid
1190
   * having to do eight extra binary search iterations).
1191
   */
1192
0
  for (i = 0; i < 256; i++) {
1193
0
    while (count < ctx->commits.nr) {
1194
0
      if ((*list)->object.oid.hash[0] != i)
1195
0
        break;
1196
0
      display_progress(ctx->progress, ++ctx->progress_cnt);
1197
0
      count++;
1198
0
      list++;
1199
0
    }
1200
1201
0
    hashwrite_be32(f, count);
1202
0
  }
1203
1204
0
  return 0;
1205
0
}
1206
1207
static int write_graph_chunk_oids(struct hashfile *f,
1208
          void *data)
1209
0
{
1210
0
  struct write_commit_graph_context *ctx = data;
1211
0
  struct commit **list = ctx->commits.items;
1212
0
  int count;
1213
0
  for (count = 0; count < ctx->commits.nr; count++, list++) {
1214
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1215
0
    hashwrite(f, (*list)->object.oid.hash, f->algop->rawsz);
1216
0
  }
1217
1218
0
  return 0;
1219
0
}
1220
1221
static const struct object_id *commit_to_oid(size_t index, const void *table)
1222
0
{
1223
0
  const struct commit * const *commits = table;
1224
0
  return &commits[index]->object.oid;
1225
0
}
1226
1227
static int write_graph_chunk_data(struct hashfile *f,
1228
          void *data)
1229
0
{
1230
0
  struct write_commit_graph_context *ctx = data;
1231
0
  struct commit **list = ctx->commits.items;
1232
0
  struct commit **last = ctx->commits.items + ctx->commits.nr;
1233
0
  uint32_t num_extra_edges = 0;
1234
1235
0
  while (list < last) {
1236
0
    struct commit_list *parent;
1237
0
    struct object_id *tree;
1238
0
    int edge_value;
1239
0
    uint32_t packedDate[2];
1240
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1241
1242
0
    if (repo_parse_commit_no_graph(ctx->r, *list))
1243
0
      die(_("unable to parse commit %s"),
1244
0
        oid_to_hex(&(*list)->object.oid));
1245
0
    tree = get_commit_tree_oid(*list);
1246
0
    hashwrite(f, tree->hash, ctx->r->hash_algo->rawsz);
1247
1248
0
    parent = (*list)->parents;
1249
1250
0
    if (!parent)
1251
0
      edge_value = GRAPH_PARENT_NONE;
1252
0
    else {
1253
0
      edge_value = oid_pos(&parent->item->object.oid,
1254
0
               ctx->commits.items,
1255
0
               ctx->commits.nr,
1256
0
               commit_to_oid);
1257
1258
0
      if (edge_value >= 0)
1259
0
        edge_value += ctx->new_num_commits_in_base;
1260
0
      else if (ctx->new_base_graph) {
1261
0
        uint32_t pos;
1262
0
        if (find_commit_pos_in_graph(parent->item,
1263
0
                   ctx->new_base_graph,
1264
0
                   &pos))
1265
0
          edge_value = pos;
1266
0
      }
1267
1268
0
      if (edge_value < 0)
1269
0
        BUG("missing parent %s for commit %s",
1270
0
            oid_to_hex(&parent->item->object.oid),
1271
0
            oid_to_hex(&(*list)->object.oid));
1272
0
    }
1273
1274
0
    hashwrite_be32(f, edge_value);
1275
1276
0
    if (parent)
1277
0
      parent = parent->next;
1278
1279
0
    if (!parent)
1280
0
      edge_value = GRAPH_PARENT_NONE;
1281
0
    else if (parent->next)
1282
0
      edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1283
0
    else {
1284
0
      edge_value = oid_pos(&parent->item->object.oid,
1285
0
               ctx->commits.items,
1286
0
               ctx->commits.nr,
1287
0
               commit_to_oid);
1288
1289
0
      if (edge_value >= 0)
1290
0
        edge_value += ctx->new_num_commits_in_base;
1291
0
      else if (ctx->new_base_graph) {
1292
0
        uint32_t pos;
1293
0
        if (find_commit_pos_in_graph(parent->item,
1294
0
                   ctx->new_base_graph,
1295
0
                   &pos))
1296
0
          edge_value = pos;
1297
0
      }
1298
1299
0
      if (edge_value < 0)
1300
0
        BUG("missing parent %s for commit %s",
1301
0
            oid_to_hex(&parent->item->object.oid),
1302
0
            oid_to_hex(&(*list)->object.oid));
1303
0
    }
1304
1305
0
    hashwrite_be32(f, edge_value);
1306
1307
0
    if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1308
0
      do {
1309
0
        num_extra_edges++;
1310
0
        parent = parent->next;
1311
0
      } while (parent);
1312
0
    }
1313
1314
0
    if (sizeof((*list)->date) > 4)
1315
0
      packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1316
0
    else
1317
0
      packedDate[0] = 0;
1318
1319
0
    packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1320
1321
0
    packedDate[1] = htonl((*list)->date);
1322
0
    hashwrite(f, packedDate, 8);
1323
1324
0
    list++;
1325
0
  }
1326
1327
0
  return 0;
1328
0
}
1329
1330
/*
1331
 * Compute the generation offset between the commit date and its generation.
1332
 * This is what's ultimately stored as generation number in the commit graph.
1333
 *
1334
 * Note that the computation of the commit date is more involved than you might
1335
 * think. Instead of using the full commit date, we're in fact masking bits so
1336
 * that only the 34 lowest bits are considered. This results from the fact that
1337
 * commit graphs themselves only ever store 34 bits of the commit date
1338
 * themselves.
1339
 *
1340
 * This means that if we have a commit date that exceeds 34 bits we'll end up
1341
 * in situations where depending on whether the commit has been parsed from the
1342
 * object database or the commit graph we'll have different dates, where the
1343
 * ones parsed from the object database would have full 64 bit precision.
1344
 *
1345
 * But ultimately, we only ever want the offset to be relative to what we
1346
 * actually end up storing on disk, and hence we have to mask all the other
1347
 * bits.
1348
 */
1349
static timestamp_t compute_generation_offset(struct commit *c)
1350
0
{
1351
0
  timestamp_t masked_date;
1352
1353
0
  if (sizeof(timestamp_t) > 4)
1354
0
    masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
1355
0
  else
1356
0
    masked_date = c->date;
1357
1358
0
  return commit_graph_data_at(c)->generation - masked_date;
1359
0
}
1360
1361
static int write_graph_chunk_generation_data(struct hashfile *f,
1362
               void *data)
1363
0
{
1364
0
  struct write_commit_graph_context *ctx = data;
1365
0
  int i, num_generation_data_overflows = 0;
1366
1367
0
  for (i = 0; i < ctx->commits.nr; i++) {
1368
0
    struct commit *c = ctx->commits.items[i];
1369
0
    timestamp_t offset;
1370
0
    repo_parse_commit(ctx->r, c);
1371
0
    offset = compute_generation_offset(c);
1372
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1373
1374
0
    if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1375
0
      offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1376
0
      num_generation_data_overflows++;
1377
0
    }
1378
1379
0
    hashwrite_be32(f, offset);
1380
0
  }
1381
1382
0
  return 0;
1383
0
}
1384
1385
static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1386
                  void *data)
1387
0
{
1388
0
  struct write_commit_graph_context *ctx = data;
1389
0
  int i;
1390
0
  for (i = 0; i < ctx->commits.nr; i++) {
1391
0
    struct commit *c = ctx->commits.items[i];
1392
0
    timestamp_t offset = compute_generation_offset(c);
1393
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1394
1395
0
    if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1396
0
      hashwrite_be32(f, offset >> 32);
1397
0
      hashwrite_be32(f, (uint32_t) offset);
1398
0
    }
1399
0
  }
1400
1401
0
  return 0;
1402
0
}
1403
1404
static int write_graph_chunk_extra_edges(struct hashfile *f,
1405
           void *data)
1406
0
{
1407
0
  struct write_commit_graph_context *ctx = data;
1408
0
  struct commit **list = ctx->commits.items;
1409
0
  struct commit **last = ctx->commits.items + ctx->commits.nr;
1410
0
  struct commit_list *parent;
1411
1412
0
  while (list < last) {
1413
0
    int num_parents = 0;
1414
1415
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1416
1417
0
    for (parent = (*list)->parents; num_parents < 3 && parent;
1418
0
         parent = parent->next)
1419
0
      num_parents++;
1420
1421
0
    if (num_parents <= 2) {
1422
0
      list++;
1423
0
      continue;
1424
0
    }
1425
1426
    /* Since num_parents > 2, this initializer is safe. */
1427
0
    for (parent = (*list)->parents->next; parent; parent = parent->next) {
1428
0
      int edge_value = oid_pos(&parent->item->object.oid,
1429
0
             ctx->commits.items,
1430
0
             ctx->commits.nr,
1431
0
             commit_to_oid);
1432
1433
0
      if (edge_value >= 0)
1434
0
        edge_value += ctx->new_num_commits_in_base;
1435
0
      else if (ctx->new_base_graph) {
1436
0
        uint32_t pos;
1437
0
        if (find_commit_pos_in_graph(parent->item,
1438
0
                   ctx->new_base_graph,
1439
0
                   &pos))
1440
0
          edge_value = pos;
1441
0
      }
1442
1443
0
      if (edge_value < 0)
1444
0
        BUG("missing parent %s for commit %s",
1445
0
            oid_to_hex(&parent->item->object.oid),
1446
0
            oid_to_hex(&(*list)->object.oid));
1447
0
      else if (!parent->next)
1448
0
        edge_value |= GRAPH_LAST_EDGE;
1449
1450
0
      hashwrite_be32(f, edge_value);
1451
0
    }
1452
1453
0
    list++;
1454
0
  }
1455
1456
0
  return 0;
1457
0
}
1458
1459
static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1460
             void *data)
1461
0
{
1462
0
  struct write_commit_graph_context *ctx = data;
1463
0
  struct commit **list = ctx->commits.items;
1464
0
  struct commit **last = ctx->commits.items + ctx->commits.nr;
1465
0
  uint32_t cur_pos = 0;
1466
1467
0
  while (list < last) {
1468
0
    struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1469
0
    size_t len = filter ? filter->len : 0;
1470
0
    cur_pos += len;
1471
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1472
0
    hashwrite_be32(f, cur_pos);
1473
0
    list++;
1474
0
  }
1475
1476
0
  return 0;
1477
0
}
1478
1479
static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1480
0
{
1481
0
  struct json_writer jw = JSON_WRITER_INIT;
1482
1483
0
  jw_object_begin(&jw, 0);
1484
0
  jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1485
0
  jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1486
0
  jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1487
0
  jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1488
0
  jw_end(&jw);
1489
1490
0
  trace2_data_json("bloom", ctx->r, "settings", &jw);
1491
1492
0
  jw_release(&jw);
1493
0
}
1494
1495
static int write_graph_chunk_bloom_data(struct hashfile *f,
1496
          void *data)
1497
0
{
1498
0
  struct write_commit_graph_context *ctx = data;
1499
0
  struct commit **list = ctx->commits.items;
1500
0
  struct commit **last = ctx->commits.items + ctx->commits.nr;
1501
1502
0
  trace2_bloom_filter_settings(ctx);
1503
1504
0
  hashwrite_be32(f, ctx->bloom_settings->hash_version);
1505
0
  hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1506
0
  hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1507
1508
0
  while (list < last) {
1509
0
    struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1510
0
    size_t len = filter ? filter->len : 0;
1511
1512
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1513
0
    if (len)
1514
0
      hashwrite(f, filter->data, len * sizeof(unsigned char));
1515
0
    list++;
1516
0
  }
1517
1518
0
  return 0;
1519
0
}
1520
1521
static int add_packed_commits_oi(const struct object_id *oid,
1522
         struct object_info *oi,
1523
         void *data)
1524
0
{
1525
0
  struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1526
1527
0
  if (ctx->progress)
1528
0
    display_progress(ctx->progress, ++ctx->progress_done);
1529
1530
0
  if (*oi->typep != OBJ_COMMIT)
1531
0
    return 0;
1532
1533
0
  oid_array_append(&ctx->oids, oid);
1534
0
  set_commit_pos(ctx->r, oid);
1535
1536
0
  return 0;
1537
0
}
1538
1539
static int add_packed_commits(const struct object_id *oid,
1540
            struct packed_git *pack,
1541
            uint32_t pos,
1542
            void *data)
1543
0
{
1544
0
  enum object_type type;
1545
0
  off_t offset = nth_packed_object_offset(pack, pos);
1546
0
  struct object_info oi = OBJECT_INFO_INIT;
1547
1548
0
  oi.typep = &type;
1549
0
  if (packed_object_info(NULL, pack, offset, &oi) < 0)
1550
0
    die(_("unable to get type of object %s"), oid_to_hex(oid));
1551
1552
0
  return add_packed_commits_oi(oid, &oi, data);
1553
0
}
1554
1555
static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1556
0
{
1557
0
  struct commit_list *parent;
1558
0
  for (parent = commit->parents; parent; parent = parent->next) {
1559
0
    if (!(parent->item->object.flags & REACHABLE)) {
1560
0
      oid_array_append(&ctx->oids, &parent->item->object.oid);
1561
0
      parent->item->object.flags |= REACHABLE;
1562
0
    }
1563
0
  }
1564
0
}
1565
1566
static void close_reachable(struct write_commit_graph_context *ctx)
1567
0
{
1568
0
  int i;
1569
0
  struct commit *commit;
1570
0
  enum commit_graph_split_flags flags = ctx->opts ?
1571
0
    ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1572
1573
0
  if (ctx->report_progress)
1574
0
    ctx->progress = start_delayed_progress(
1575
0
          ctx->r,
1576
0
          _("Loading known commits in commit graph"),
1577
0
          ctx->oids.nr);
1578
0
  for (i = 0; i < ctx->oids.nr; i++) {
1579
0
    display_progress(ctx->progress, i + 1);
1580
0
    commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1581
0
    if (commit)
1582
0
      commit->object.flags |= REACHABLE;
1583
0
  }
1584
0
  stop_progress(&ctx->progress);
1585
1586
  /*
1587
   * As this loop runs, ctx->oids.nr may grow, but not more
1588
   * than the number of missing commits in the reachable
1589
   * closure.
1590
   */
1591
0
  if (ctx->report_progress)
1592
0
    ctx->progress = start_delayed_progress(
1593
0
          ctx->r,
1594
0
          _("Expanding reachable commits in commit graph"),
1595
0
          0);
1596
0
  for (i = 0; i < ctx->oids.nr; i++) {
1597
0
    display_progress(ctx->progress, i + 1);
1598
0
    commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1599
1600
0
    if (!commit)
1601
0
      continue;
1602
0
    if (ctx->split) {
1603
0
      if ((!repo_parse_commit(ctx->r, commit) &&
1604
0
           commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1605
0
          flags == COMMIT_GRAPH_SPLIT_REPLACE)
1606
0
        add_missing_parents(ctx, commit);
1607
0
    } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1608
0
      add_missing_parents(ctx, commit);
1609
0
  }
1610
0
  stop_progress(&ctx->progress);
1611
1612
0
  if (ctx->report_progress)
1613
0
    ctx->progress = start_delayed_progress(
1614
0
          ctx->r,
1615
0
          _("Clearing commit marks in commit graph"),
1616
0
          ctx->oids.nr);
1617
0
  for (i = 0; i < ctx->oids.nr; i++) {
1618
0
    display_progress(ctx->progress, i + 1);
1619
0
    commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1620
1621
0
    if (commit)
1622
0
      commit->object.flags &= ~REACHABLE;
1623
0
  }
1624
0
  stop_progress(&ctx->progress);
1625
0
}
1626
1627
struct compute_generation_info {
1628
  struct repository *r;
1629
  struct commit_stack *commits;
1630
  struct progress *progress;
1631
  int progress_cnt;
1632
1633
  timestamp_t (*get_generation)(struct commit *c, void *data);
1634
  void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1635
  void *data;
1636
};
1637
1638
static timestamp_t compute_generation_from_max(struct commit *c,
1639
                 timestamp_t max_gen,
1640
                 int generation_version)
1641
0
{
1642
0
  switch (generation_version) {
1643
0
  case 1: /* topological levels */
1644
0
    if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1645
0
      max_gen = GENERATION_NUMBER_V1_MAX - 1;
1646
0
    return max_gen + 1;
1647
1648
0
  case 2: /* corrected commit date */
1649
0
    if (c->date && c->date > max_gen)
1650
0
      max_gen = c->date - 1;
1651
0
    return max_gen + 1;
1652
1653
0
  default:
1654
0
    BUG("attempting unimplemented version");
1655
0
  }
1656
0
}
1657
1658
static void compute_reachable_generation_numbers(
1659
      struct compute_generation_info *info,
1660
      int generation_version)
1661
0
{
1662
0
  int i;
1663
0
  struct commit_list *list = NULL;
1664
0
  intmax_t steps = 0;
1665
1666
0
  for (i = 0; i < info->commits->nr; i++) {
1667
0
    struct commit *c = info->commits->items[i];
1668
0
    timestamp_t gen;
1669
0
    repo_parse_commit(info->r, c);
1670
0
    gen = info->get_generation(c, info->data);
1671
0
    display_progress(info->progress, ++info->progress_cnt);
1672
1673
0
    if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1674
0
      continue;
1675
1676
0
    commit_list_insert(c, &list);
1677
0
    while (list) {
1678
0
      struct commit *current = list->item;
1679
0
      struct commit_list *parent;
1680
0
      int all_parents_computed = 1;
1681
0
      timestamp_t max_gen = 0;
1682
1683
0
      steps++;
1684
0
      for (parent = current->parents; parent; parent = parent->next) {
1685
0
        repo_parse_commit(info->r, parent->item);
1686
0
        gen = info->get_generation(parent->item, info->data);
1687
1688
0
        if (gen == GENERATION_NUMBER_ZERO) {
1689
0
          all_parents_computed = 0;
1690
0
          commit_list_insert(parent->item, &list);
1691
0
          break;
1692
0
        }
1693
1694
0
        if (gen > max_gen)
1695
0
          max_gen = gen;
1696
0
      }
1697
1698
0
      if (all_parents_computed) {
1699
0
        pop_commit(&list);
1700
0
        gen = compute_generation_from_max(
1701
0
            current, max_gen,
1702
0
            generation_version);
1703
0
        info->set_generation(current, gen, info->data);
1704
0
      }
1705
0
    }
1706
0
  }
1707
1708
0
  trace2_data_intmax("commit-graph", info->r,
1709
0
         "generation-dfs-steps", steps);
1710
0
}
1711
1712
static timestamp_t get_topo_level(struct commit *c, void *data)
1713
0
{
1714
0
  struct write_commit_graph_context *ctx = data;
1715
0
  return *topo_level_slab_at(ctx->topo_levels, c);
1716
0
}
1717
1718
static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1719
0
{
1720
0
  struct write_commit_graph_context *ctx = data;
1721
0
  *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1722
0
}
1723
1724
static void compute_topological_levels(struct write_commit_graph_context *ctx)
1725
0
{
1726
0
  struct compute_generation_info info = {
1727
0
    .r = ctx->r,
1728
0
    .commits = &ctx->commits,
1729
0
    .get_generation = get_topo_level,
1730
0
    .set_generation = set_topo_level,
1731
0
    .data = ctx,
1732
0
  };
1733
1734
0
  if (ctx->report_progress)
1735
0
    info.progress = ctx->progress
1736
0
            = start_delayed_progress(
1737
0
          ctx->r,
1738
0
          _("Computing commit graph topological levels"),
1739
0
          ctx->commits.nr);
1740
1741
0
  compute_reachable_generation_numbers(&info, 1);
1742
1743
0
  stop_progress(&ctx->progress);
1744
0
}
1745
1746
static timestamp_t get_generation_from_graph_data(struct commit *c,
1747
              void *data UNUSED)
1748
0
{
1749
0
  return commit_graph_data_at(c)->generation;
1750
0
}
1751
1752
static void set_generation_v2(struct commit *c, timestamp_t t,
1753
            void *data UNUSED)
1754
0
{
1755
0
  struct commit_graph_data *g = commit_graph_data_at(c);
1756
0
  g->generation = t;
1757
0
}
1758
1759
static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1760
0
{
1761
0
  int i;
1762
0
  struct compute_generation_info info = {
1763
0
    .r = ctx->r,
1764
0
    .commits = &ctx->commits,
1765
0
    .get_generation = get_generation_from_graph_data,
1766
0
    .set_generation = set_generation_v2,
1767
0
  };
1768
1769
0
  if (ctx->report_progress)
1770
0
    info.progress = ctx->progress
1771
0
            = start_delayed_progress(
1772
0
          ctx->r,
1773
0
          _("Computing commit graph generation numbers"),
1774
0
          ctx->commits.nr);
1775
1776
0
  if (!ctx->trust_generation_numbers) {
1777
0
    for (i = 0; i < ctx->commits.nr; i++) {
1778
0
      struct commit *c = ctx->commits.items[i];
1779
0
      repo_parse_commit(ctx->r, c);
1780
0
      commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1781
0
    }
1782
0
  }
1783
1784
0
  compute_reachable_generation_numbers(&info, 2);
1785
1786
0
  for (i = 0; i < ctx->commits.nr; i++) {
1787
0
    struct commit *c = ctx->commits.items[i];
1788
0
    timestamp_t offset = compute_generation_offset(c);
1789
0
    if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1790
0
      ctx->num_generation_data_overflows++;
1791
0
  }
1792
0
  stop_progress(&ctx->progress);
1793
0
}
1794
1795
static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1796
           void *data UNUSED)
1797
0
{
1798
0
  commit_graph_data_at(c)->generation = t;
1799
0
}
1800
1801
/*
1802
 * After this method, all commits reachable from those in the given
1803
 * list will have non-zero, non-infinite generation numbers.
1804
 */
1805
void ensure_generations_valid(struct repository *r,
1806
            struct commit **commits, size_t nr)
1807
0
{
1808
0
  int generation_version = get_configured_generation_version(r);
1809
0
  struct commit_stack list = {
1810
0
    .items = commits,
1811
0
    .alloc = nr,
1812
0
    .nr = nr,
1813
0
  };
1814
0
  struct compute_generation_info info = {
1815
0
    .r = r,
1816
0
    .commits = &list,
1817
0
    .get_generation = get_generation_from_graph_data,
1818
0
    .set_generation = set_generation_in_graph_data,
1819
0
  };
1820
1821
0
  compute_reachable_generation_numbers(&info, generation_version);
1822
0
}
1823
1824
static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1825
0
{
1826
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1827
0
         ctx->count_bloom_filter_computed);
1828
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1829
0
         ctx->count_bloom_filter_not_computed);
1830
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1831
0
         ctx->count_bloom_filter_trunc_empty);
1832
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1833
0
         ctx->count_bloom_filter_trunc_large);
1834
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-upgraded",
1835
0
         ctx->count_bloom_filter_upgraded);
1836
0
}
1837
1838
static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1839
0
{
1840
0
  int i;
1841
0
  struct progress *progress = NULL;
1842
0
  struct commit **sorted_commits;
1843
0
  int max_new_filters;
1844
1845
0
  init_bloom_filters();
1846
1847
0
  if (ctx->report_progress)
1848
0
    progress = start_delayed_progress(
1849
0
      ctx->r,
1850
0
      _("Computing commit changed paths Bloom filters"),
1851
0
      ctx->commits.nr);
1852
1853
0
  DUP_ARRAY(sorted_commits, ctx->commits.items, ctx->commits.nr);
1854
1855
0
  if (ctx->order_by_pack)
1856
0
    QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1857
0
  else
1858
0
    QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1859
1860
0
  max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1861
0
    ctx->opts->max_new_filters : ctx->commits.nr;
1862
1863
0
  for (i = 0; i < ctx->commits.nr; i++) {
1864
0
    enum bloom_filter_computed computed = 0;
1865
0
    struct commit *c = sorted_commits[i];
1866
0
    struct bloom_filter *filter = get_or_compute_bloom_filter(
1867
0
      ctx->r,
1868
0
      c,
1869
0
      ctx->count_bloom_filter_computed < max_new_filters,
1870
0
      ctx->bloom_settings,
1871
0
      &computed);
1872
0
    if (computed & BLOOM_COMPUTED) {
1873
0
      ctx->count_bloom_filter_computed++;
1874
0
      if (computed & BLOOM_TRUNC_EMPTY)
1875
0
        ctx->count_bloom_filter_trunc_empty++;
1876
0
      if (computed & BLOOM_TRUNC_LARGE)
1877
0
        ctx->count_bloom_filter_trunc_large++;
1878
0
    } else if (computed & BLOOM_UPGRADED) {
1879
0
      ctx->count_bloom_filter_upgraded++;
1880
0
    } else if (computed & BLOOM_NOT_COMPUTED)
1881
0
      ctx->count_bloom_filter_not_computed++;
1882
0
    ctx->total_bloom_filter_data_size += filter
1883
0
      ? sizeof(unsigned char) * filter->len : 0;
1884
0
    display_progress(progress, i + 1);
1885
0
  }
1886
1887
0
  if (trace2_is_enabled())
1888
0
    trace2_bloom_filter_write_statistics(ctx);
1889
1890
0
  free(sorted_commits);
1891
0
  stop_progress(&progress);
1892
0
}
1893
1894
struct refs_cb_data {
1895
  struct repository *repo;
1896
  struct oidset *commits;
1897
  struct progress *progress;
1898
};
1899
1900
static int add_ref_to_set(const struct reference *ref, void *cb_data)
1901
0
{
1902
0
  const struct object_id *maybe_peeled = ref->oid;
1903
0
  struct object_id peeled;
1904
0
  struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1905
1906
0
  if (!reference_get_peeled_oid(data->repo, ref, &peeled))
1907
0
    maybe_peeled = &peeled;
1908
0
  if (odb_read_object_info(data->repo->objects, maybe_peeled, NULL) == OBJ_COMMIT)
1909
0
    oidset_insert(data->commits, maybe_peeled);
1910
1911
0
  display_progress(data->progress, oidset_size(data->commits));
1912
1913
0
  return 0;
1914
0
}
1915
1916
int write_commit_graph_reachable(struct repository *repo,
1917
         const char *dir,
1918
         enum commit_graph_write_flags flags,
1919
         const struct commit_graph_opts *opts)
1920
0
{
1921
0
  struct oidset commits = OIDSET_INIT;
1922
0
  struct refs_cb_data data;
1923
0
  int result;
1924
1925
0
  memset(&data, 0, sizeof(data));
1926
0
  data.repo = repo;
1927
0
  data.commits = &commits;
1928
1929
0
  if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1930
0
    data.progress = start_delayed_progress(
1931
0
      repo,
1932
0
      _("Collecting referenced commits"), 0);
1933
1934
0
  refs_for_each_ref(get_main_ref_store(repo), add_ref_to_set,
1935
0
        &data);
1936
1937
0
  stop_progress(&data.progress);
1938
1939
0
  result = write_commit_graph(repo, dir, NULL, &commits,
1940
0
            flags, opts);
1941
1942
0
  oidset_clear(&commits);
1943
0
  return result;
1944
0
}
1945
1946
static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1947
        const struct string_list *pack_indexes)
1948
0
{
1949
0
  uint32_t i;
1950
0
  struct strbuf progress_title = STRBUF_INIT;
1951
0
  int ret = 0;
1952
1953
0
  if (ctx->report_progress) {
1954
0
    strbuf_addf(&progress_title,
1955
0
          Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1956
0
             "Finding commits for commit graph in %"PRIuMAX" packs",
1957
0
             pack_indexes->nr),
1958
0
          (uintmax_t)pack_indexes->nr);
1959
0
    ctx->progress = start_delayed_progress(ctx->r,
1960
0
                   progress_title.buf, 0);
1961
0
    ctx->progress_done = 0;
1962
0
  }
1963
0
  for (i = 0; i < pack_indexes->nr; i++) {
1964
0
    struct packed_git *p;
1965
1966
0
    p = add_packed_git(ctx->r, pack_indexes->items[i].string,
1967
0
           strlen(pack_indexes->items[i].string), 1);
1968
0
    if (!p) {
1969
0
      ret = error(_("error adding pack %s"), pack_indexes->items[i].string);
1970
0
      goto cleanup;
1971
0
    }
1972
0
    if (open_pack_index(p)) {
1973
0
      ret = error(_("error opening index for %s"), pack_indexes->items[i].string);
1974
0
      close_pack(p);
1975
0
      free(p);
1976
0
      goto cleanup;
1977
0
    }
1978
0
    for_each_object_in_pack(p, add_packed_commits, ctx,
1979
0
          ODB_FOR_EACH_OBJECT_PACK_ORDER);
1980
0
    close_pack(p);
1981
0
    free(p);
1982
0
  }
1983
1984
0
cleanup:
1985
0
  stop_progress(&ctx->progress);
1986
0
  strbuf_release(&progress_title);
1987
1988
0
  return ret;
1989
0
}
1990
1991
static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1992
          struct oidset *commits)
1993
0
{
1994
0
  struct oidset_iter iter;
1995
0
  struct object_id *oid;
1996
1997
0
  if (!oidset_size(commits))
1998
0
    return 0;
1999
2000
0
  oidset_iter_init(commits, &iter);
2001
0
  while ((oid = oidset_iter_next(&iter))) {
2002
0
    oid_array_append(&ctx->oids, oid);
2003
0
  }
2004
2005
0
  return 0;
2006
0
}
2007
2008
static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
2009
0
{
2010
0
  struct odb_source_files *files;
2011
0
  enum object_type type;
2012
0
  struct odb_for_each_object_options opts = {
2013
0
    .flags = ODB_FOR_EACH_OBJECT_PACK_ORDER,
2014
0
  };
2015
0
  struct object_info oi = {
2016
0
    .typep = &type,
2017
0
  };
2018
2019
0
  if (ctx->report_progress)
2020
0
    ctx->progress = start_delayed_progress(
2021
0
      ctx->r,
2022
0
      _("Finding commits for commit graph among packed objects"),
2023
0
      ctx->approx_nr_objects);
2024
2025
0
  files = odb_source_files_downcast(ctx->r->objects->source);
2026
0
  for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
2027
0
    odb_source_for_each_object(&dir->packed->base, &oi, add_packed_commits_oi,
2028
0
             ctx, &opts);
2029
0
  }
2030
2031
0
  if (ctx->progress_done < ctx->approx_nr_objects)
2032
0
    display_progress(ctx->progress, ctx->approx_nr_objects);
2033
0
  stop_progress(&ctx->progress);
2034
0
}
2035
2036
static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
2037
0
{
2038
0
  uint32_t i;
2039
0
  enum commit_graph_split_flags flags = ctx->opts ?
2040
0
    ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2041
2042
0
  ctx->num_extra_edges = 0;
2043
0
  if (ctx->report_progress)
2044
0
    ctx->progress = start_delayed_progress(
2045
0
      ctx->r,
2046
0
      _("Finding extra edges in commit graph"),
2047
0
      ctx->oids.nr);
2048
0
  oid_array_sort(&ctx->oids);
2049
0
  for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
2050
0
    unsigned int num_parents;
2051
0
    struct commit *commit;
2052
2053
0
    display_progress(ctx->progress, i + 1);
2054
2055
0
    commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
2056
2057
0
    if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
2058
0
        commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
2059
0
      continue;
2060
2061
0
    if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
2062
0
      repo_parse_commit(ctx->r, commit);
2063
0
    else
2064
0
      repo_parse_commit_no_graph(ctx->r, commit);
2065
2066
0
    num_parents = commit_list_count(commit->parents);
2067
0
    if (num_parents > 2)
2068
0
      ctx->num_extra_edges += num_parents - 1;
2069
2070
0
    commit_stack_push(&ctx->commits, commit);
2071
0
  }
2072
0
  stop_progress(&ctx->progress);
2073
0
}
2074
2075
static int write_graph_chunk_base_1(struct hashfile *f,
2076
            struct commit_graph *g)
2077
0
{
2078
0
  int num = 0;
2079
2080
0
  if (!g)
2081
0
    return 0;
2082
2083
0
  num = write_graph_chunk_base_1(f, g->base_graph);
2084
0
  hashwrite(f, g->oid.hash, g->hash_algo->rawsz);
2085
0
  return num + 1;
2086
0
}
2087
2088
static int write_graph_chunk_base(struct hashfile *f,
2089
            void *data)
2090
0
{
2091
0
  struct write_commit_graph_context *ctx = data;
2092
0
  int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
2093
2094
0
  if (num != ctx->num_commit_graphs_after - 1) {
2095
0
    error(_("failed to write correct number of base graph ids"));
2096
0
    return -1;
2097
0
  }
2098
2099
0
  return 0;
2100
0
}
2101
2102
static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2103
0
{
2104
0
  uint32_t i;
2105
0
  struct hashfile *f;
2106
0
  struct tempfile *graph_layer; /* when ctx->split is non-zero */
2107
0
  struct lock_file lk = LOCK_INIT;
2108
0
  const unsigned hashsz = ctx->r->hash_algo->rawsz;
2109
0
  struct strbuf progress_title = STRBUF_INIT;
2110
0
  struct chunkfile *cf;
2111
0
  unsigned char file_hash[GIT_MAX_RAWSZ];
2112
2113
0
  if (ctx->split) {
2114
0
    struct strbuf tmp_file = STRBUF_INIT;
2115
2116
0
    strbuf_addf(&tmp_file,
2117
0
          "%s/info/commit-graphs/tmp_graph_XXXXXX",
2118
0
          ctx->dir);
2119
0
    ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2120
0
  } else {
2121
0
    ctx->graph_name = get_commit_graph_filename(ctx->dir);
2122
0
  }
2123
2124
0
  if (safe_create_leading_directories(ctx->r, ctx->graph_name)) {
2125
0
    error(_("unable to create leading directories of %s"),
2126
0
      ctx->graph_name);
2127
0
    return -1;
2128
0
  }
2129
2130
0
  if (ctx->split) {
2131
0
    char *lock_name = get_commit_graph_chain_filename(ctx->dir);
2132
2133
0
    repo_hold_lock_file_for_update_mode(ctx->r, &lk, lock_name,
2134
0
                LOCK_DIE_ON_ERROR, 0444);
2135
0
    free(lock_name);
2136
2137
0
    graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
2138
0
    if (!graph_layer) {
2139
0
      error(_("unable to create temporary graph layer"));
2140
0
      return -1;
2141
0
    }
2142
2143
0
    if (adjust_shared_perm(ctx->r, get_tempfile_path(graph_layer))) {
2144
0
      error(_("unable to adjust shared permissions for '%s'"),
2145
0
            get_tempfile_path(graph_layer));
2146
0
      return -1;
2147
0
    }
2148
2149
0
    f = hashfd(ctx->r->hash_algo,
2150
0
         get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
2151
0
  } else {
2152
0
    repo_hold_lock_file_for_update_mode(ctx->r, &lk,
2153
0
                ctx->graph_name,
2154
0
                LOCK_DIE_ON_ERROR, 0444);
2155
0
    f = hashfd(ctx->r->hash_algo,
2156
0
         get_lock_file_fd(&lk), get_lock_file_path(&lk));
2157
0
  }
2158
2159
0
  cf = init_chunkfile(f);
2160
2161
0
  add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2162
0
      write_graph_chunk_fanout);
2163
0
  add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2164
0
      write_graph_chunk_oids);
2165
0
  add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2166
0
      write_graph_chunk_data);
2167
2168
0
  if (ctx->write_generation_data)
2169
0
    add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2170
0
        st_mult(sizeof(uint32_t), ctx->commits.nr),
2171
0
        write_graph_chunk_generation_data);
2172
0
  if (ctx->num_generation_data_overflows)
2173
0
    add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2174
0
        st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2175
0
        write_graph_chunk_generation_data_overflow);
2176
0
  if (ctx->num_extra_edges)
2177
0
    add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2178
0
        st_mult(4, ctx->num_extra_edges),
2179
0
        write_graph_chunk_extra_edges);
2180
0
  if (ctx->changed_paths) {
2181
0
    add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2182
0
        st_mult(sizeof(uint32_t), ctx->commits.nr),
2183
0
        write_graph_chunk_bloom_indexes);
2184
0
    add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2185
0
        st_add(sizeof(uint32_t) * 3,
2186
0
         ctx->total_bloom_filter_data_size),
2187
0
        write_graph_chunk_bloom_data);
2188
0
  }
2189
0
  if (ctx->num_commit_graphs_after > 1)
2190
0
    add_chunk(cf, GRAPH_CHUNKID_BASE,
2191
0
        st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2192
0
        write_graph_chunk_base);
2193
2194
0
  hashwrite_be32(f, GRAPH_SIGNATURE);
2195
2196
0
  hashwrite_u8(f, GRAPH_VERSION);
2197
0
  hashwrite_u8(f, oid_version(ctx->r->hash_algo));
2198
0
  hashwrite_u8(f, get_num_chunks(cf));
2199
0
  hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2200
2201
0
  if (ctx->report_progress) {
2202
0
    strbuf_addf(&progress_title,
2203
0
          Q_("Writing out commit graph in %d pass",
2204
0
             "Writing out commit graph in %d passes",
2205
0
             get_num_chunks(cf)),
2206
0
          get_num_chunks(cf));
2207
0
    ctx->progress = start_delayed_progress(
2208
0
      ctx->r,
2209
0
      progress_title.buf,
2210
0
      st_mult(get_num_chunks(cf), ctx->commits.nr));
2211
0
  }
2212
2213
0
  write_chunkfile(cf, ctx);
2214
2215
0
  stop_progress(&ctx->progress);
2216
0
  strbuf_release(&progress_title);
2217
2218
0
  if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2219
0
    char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2220
0
    char *new_base_name = get_split_graph_filename(ctx->new_base_graph->dir, new_base_hash);
2221
2222
0
    free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2223
0
    free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2224
0
    ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2225
0
    ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2226
0
  }
2227
2228
0
  close_commit_graph(ctx->r->objects);
2229
0
  finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2230
0
        CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2231
0
  free_chunkfile(cf);
2232
2233
0
  if (ctx->split) {
2234
0
    FILE *chainf = fdopen_lock_file(&lk, "w");
2235
0
    char *final_graph_name;
2236
0
    int result;
2237
2238
0
    if (!chainf) {
2239
0
      error(_("unable to open commit-graph chain file"));
2240
0
      return -1;
2241
0
    }
2242
2243
0
    if (ctx->base_graph_name) {
2244
0
      const char *dest;
2245
0
      int idx = ctx->num_commit_graphs_after - 1;
2246
0
      if (ctx->num_commit_graphs_after > 1)
2247
0
        idx--;
2248
2249
0
      dest = ctx->commit_graph_filenames_after[idx];
2250
2251
0
      if (strcmp(ctx->base_graph_name, dest)) {
2252
0
        result = rename(ctx->base_graph_name, dest);
2253
2254
0
        if (result) {
2255
0
          error(_("failed to rename base commit-graph file"));
2256
0
          return -1;
2257
0
        }
2258
0
      }
2259
0
    } else {
2260
0
      char *graph_name = get_commit_graph_filename(ctx->dir);
2261
0
      unlink(graph_name);
2262
0
      free(graph_name);
2263
0
    }
2264
2265
0
    free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2266
0
    ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] =
2267
0
      xstrdup(hash_to_hex_algop(file_hash, ctx->r->hash_algo));
2268
0
    final_graph_name = get_split_graph_filename(ctx->dir,
2269
0
          ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2270
0
    free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2271
0
    ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2272
2273
0
    result = rename_tempfile(&graph_layer, final_graph_name);
2274
2275
0
    for (i = 0; i < ctx->num_commit_graphs_after; i++)
2276
0
      fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2277
2278
0
    if (result) {
2279
0
      error(_("failed to rename temporary commit-graph file"));
2280
0
      return -1;
2281
0
    }
2282
0
  }
2283
2284
0
  commit_lock_file(&lk);
2285
2286
0
  return 0;
2287
0
}
2288
2289
static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
2290
               struct commit_graph *graph_to_merge)
2291
0
{
2292
0
  struct commit_graph *g;
2293
0
  uint32_t num_commits;
2294
0
  enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2295
0
  uint32_t i;
2296
2297
0
  int max_commits = 0;
2298
0
  int size_mult = 2;
2299
2300
0
  if (ctx->opts) {
2301
0
    max_commits = ctx->opts->max_commits;
2302
2303
0
    if (ctx->opts->size_multiple)
2304
0
      size_mult = ctx->opts->size_multiple;
2305
2306
0
    flags = ctx->opts->split_flags;
2307
0
  }
2308
2309
0
  g = graph_to_merge;
2310
0
  num_commits = ctx->commits.nr;
2311
0
  if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2312
0
    ctx->num_commit_graphs_after = 1;
2313
0
  else
2314
0
    ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2315
2316
0
  if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2317
0
      flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2318
0
    while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2319
0
          (max_commits && num_commits > max_commits))) {
2320
0
      if (strcmp(g->dir, ctx->dir))
2321
0
        break;
2322
2323
0
      if (unsigned_add_overflows(num_commits, g->num_commits))
2324
0
        die(_("cannot merge graphs with %"PRIuMAX", "
2325
0
              "%"PRIuMAX" commits"),
2326
0
            (uintmax_t)num_commits,
2327
0
            (uintmax_t)g->num_commits);
2328
0
      num_commits += g->num_commits;
2329
0
      g = g->base_graph;
2330
2331
0
      ctx->num_commit_graphs_after--;
2332
0
    }
2333
0
  }
2334
2335
0
  if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2336
0
    ctx->new_base_graph = g;
2337
0
  else if (ctx->num_commit_graphs_after != 1)
2338
0
    BUG("split_graph_merge_strategy: num_commit_graphs_after "
2339
0
        "should be 1 with --split=replace");
2340
2341
0
  if (ctx->num_commit_graphs_after == 2) {
2342
0
    char *old_graph_name = get_commit_graph_filename(g->dir);
2343
2344
0
    if (!strcmp(g->filename, old_graph_name) &&
2345
0
        strcmp(g->dir, ctx->dir)) {
2346
0
      ctx->num_commit_graphs_after = 1;
2347
0
      ctx->new_base_graph = NULL;
2348
0
    }
2349
2350
0
    free(old_graph_name);
2351
0
  }
2352
2353
0
  CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2354
0
  CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2355
2356
0
  for (i = 0; i < ctx->num_commit_graphs_after &&
2357
0
        i < ctx->num_commit_graphs_before; i++)
2358
0
    ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2359
2360
0
  i = ctx->num_commit_graphs_before - 1;
2361
0
  g = graph_to_merge;
2362
2363
0
  while (g) {
2364
0
    if (i < ctx->num_commit_graphs_after)
2365
0
      ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2366
2367
    /*
2368
     * If the topmost remaining layer has generation data chunk, the
2369
     * resultant layer also has generation data chunk.
2370
     */
2371
0
    if (i == ctx->num_commit_graphs_after - 2)
2372
0
      ctx->write_generation_data = !!g->chunk_generation_data;
2373
2374
0
    i--;
2375
0
    g = g->base_graph;
2376
0
  }
2377
0
}
2378
2379
static void merge_commit_graph(struct write_commit_graph_context *ctx,
2380
             struct commit_graph *g)
2381
0
{
2382
0
  uint32_t i;
2383
0
  uint32_t offset = g->num_commits_in_base;
2384
2385
0
  if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2386
0
    die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2387
0
        oid_to_hex(&g->oid),
2388
0
        (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2389
2390
0
  commit_stack_grow(&ctx->commits, g->num_commits);
2391
2392
0
  for (i = 0; i < g->num_commits; i++) {
2393
0
    struct object_id oid;
2394
0
    struct commit *result;
2395
2396
0
    display_progress(ctx->progress, i + 1);
2397
2398
0
    load_oid_from_graph(g, i + offset, &oid);
2399
2400
    /* only add commits if they still exist in the repo */
2401
0
    result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2402
2403
0
    if (result)
2404
0
      commit_stack_push(&ctx->commits, result);
2405
0
  }
2406
0
}
2407
2408
static int commit_compare(const void *_a, const void *_b)
2409
0
{
2410
0
  const struct commit *a = *(const struct commit **)_a;
2411
0
  const struct commit *b = *(const struct commit **)_b;
2412
0
  return oidcmp(&a->object.oid, &b->object.oid);
2413
0
}
2414
2415
static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2416
0
{
2417
0
  uint32_t i, dedup_i = 0;
2418
2419
0
  if (ctx->report_progress)
2420
0
    ctx->progress = start_delayed_progress(
2421
0
          ctx->r,
2422
0
          _("Scanning merged commits"),
2423
0
          ctx->commits.nr);
2424
2425
0
  QSORT(ctx->commits.items, ctx->commits.nr, commit_compare);
2426
2427
0
  ctx->num_extra_edges = 0;
2428
0
  for (i = 0; i < ctx->commits.nr; i++) {
2429
0
    display_progress(ctx->progress, i + 1);
2430
2431
0
    if (i && oideq(&ctx->commits.items[i - 1]->object.oid,
2432
0
        &ctx->commits.items[i]->object.oid)) {
2433
      /*
2434
       * Silently ignore duplicates. These were likely
2435
       * created due to a commit appearing in multiple
2436
       * layers of the chain, which is unexpected but
2437
       * not invalid. We should make sure there is a
2438
       * unique copy in the new layer.
2439
       */
2440
0
    } else {
2441
0
      unsigned int num_parents;
2442
2443
0
      ctx->commits.items[dedup_i] = ctx->commits.items[i];
2444
0
      dedup_i++;
2445
2446
0
      num_parents = commit_list_count(ctx->commits.items[i]->parents);
2447
0
      if (num_parents > 2)
2448
0
        ctx->num_extra_edges += num_parents - 1;
2449
0
    }
2450
0
  }
2451
2452
0
  ctx->commits.nr = dedup_i;
2453
2454
0
  stop_progress(&ctx->progress);
2455
0
}
2456
2457
static void merge_commit_graphs(struct write_commit_graph_context *ctx,
2458
        struct commit_graph *g)
2459
0
{
2460
0
  uint32_t current_graph_number = ctx->num_commit_graphs_before;
2461
2462
0
  while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2463
0
    current_graph_number--;
2464
2465
0
    if (ctx->report_progress)
2466
0
      ctx->progress = start_delayed_progress(ctx->r,
2467
0
                     _("Merging commit-graph"), 0);
2468
2469
0
    merge_commit_graph(ctx, g);
2470
0
    stop_progress(&ctx->progress);
2471
2472
0
    g = g->base_graph;
2473
0
  }
2474
2475
0
  if (g) {
2476
0
    ctx->new_base_graph = g;
2477
0
    ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2478
0
  }
2479
2480
0
  if (ctx->new_base_graph)
2481
0
    ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2482
2483
0
  sort_and_scan_merged_commits(ctx);
2484
0
}
2485
2486
static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2487
0
{
2488
0
  uint32_t i;
2489
0
  time_t now = time(NULL);
2490
2491
0
  for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2492
0
    struct stat st;
2493
0
    struct utimbuf updated_time;
2494
2495
0
    if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2496
0
      continue;
2497
2498
0
    updated_time.actime = st.st_atime;
2499
0
    updated_time.modtime = now;
2500
0
    utime(ctx->commit_graph_filenames_before[i], &updated_time);
2501
0
  }
2502
0
}
2503
2504
static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2505
0
{
2506
0
  struct strbuf path = STRBUF_INIT;
2507
0
  DIR *dir;
2508
0
  struct dirent *de;
2509
0
  size_t dirnamelen;
2510
0
  timestamp_t expire_time = time(NULL);
2511
2512
0
  if (ctx->opts && ctx->opts->expire_time)
2513
0
    expire_time = ctx->opts->expire_time;
2514
0
  if (!ctx->split) {
2515
0
    char *chain_file_name = get_commit_graph_chain_filename(ctx->dir);
2516
0
    unlink(chain_file_name);
2517
0
    free(chain_file_name);
2518
0
    ctx->num_commit_graphs_after = 0;
2519
0
  }
2520
2521
0
  strbuf_addstr(&path, ctx->dir);
2522
0
  strbuf_addstr(&path, "/info/commit-graphs");
2523
0
  dir = opendir(path.buf);
2524
2525
0
  if (!dir)
2526
0
    goto out;
2527
2528
0
  strbuf_addch(&path, '/');
2529
0
  dirnamelen = path.len;
2530
0
  while ((de = readdir(dir)) != NULL) {
2531
0
    struct stat st;
2532
0
    uint32_t i, found = 0;
2533
2534
0
    strbuf_setlen(&path, dirnamelen);
2535
0
    strbuf_addstr(&path, de->d_name);
2536
2537
0
    if (stat(path.buf, &st) < 0)
2538
0
      continue;
2539
2540
0
    if (st.st_mtime > expire_time)
2541
0
      continue;
2542
0
    if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2543
0
      continue;
2544
2545
0
    for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2546
0
      if (!strcmp(ctx->commit_graph_filenames_after[i],
2547
0
            path.buf)) {
2548
0
        found = 1;
2549
0
        break;
2550
0
      }
2551
0
    }
2552
2553
0
    if (!found)
2554
0
      unlink(path.buf);
2555
0
  }
2556
2557
0
out:
2558
0
  if(dir)
2559
0
    closedir(dir);
2560
0
  strbuf_release(&path);
2561
0
}
2562
2563
int write_commit_graph(struct repository *r,
2564
           const char *dir,
2565
           const struct string_list *const pack_indexes,
2566
           struct oidset *commits,
2567
           enum commit_graph_write_flags flags,
2568
           const struct commit_graph_opts *opts)
2569
0
{
2570
0
  struct write_commit_graph_context ctx = {
2571
0
    .r = r,
2572
0
    .append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0,
2573
0
    .report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0,
2574
0
    .split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0,
2575
0
    .opts = opts,
2576
0
    .total_bloom_filter_data_size = 0,
2577
0
    .write_generation_data = (get_configured_generation_version(r) == 2),
2578
0
    .num_generation_data_overflows = 0,
2579
0
  };
2580
0
  uint32_t i;
2581
0
  int res = 0;
2582
0
  int replace = 0;
2583
0
  struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2584
0
  struct topo_level_slab topo_levels;
2585
0
  struct commit_graph *g;
2586
2587
0
  prepare_repo_settings(r);
2588
0
  if (!r->settings.core_commit_graph) {
2589
0
    warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2590
0
    return 0;
2591
0
  }
2592
0
  if (!commit_graph_compatible(r))
2593
0
    return 0;
2594
0
  if (r->settings.commit_graph_changed_paths_version < -1
2595
0
      || r->settings.commit_graph_changed_paths_version > 2) {
2596
0
    warning(_("attempting to write a commit-graph, but "
2597
0
        "'commitGraph.changedPathsVersion' (%d) is not supported"),
2598
0
      r->settings.commit_graph_changed_paths_version);
2599
0
    return 0;
2600
0
  }
2601
2602
0
  ctx.dir = absolute_pathdup(dir);
2603
2604
0
  bloom_settings.hash_version = r->settings.commit_graph_changed_paths_version;
2605
0
  bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2606
0
                  bloom_settings.bits_per_entry);
2607
0
  bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2608
0
              bloom_settings.num_hashes);
2609
0
  bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2610
0
               bloom_settings.max_changed_paths);
2611
0
  ctx.bloom_settings = &bloom_settings;
2612
2613
0
  init_topo_level_slab(&topo_levels);
2614
0
  ctx.topo_levels = &topo_levels;
2615
2616
0
  g = prepare_commit_graph(ctx.r);
2617
0
  for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2618
0
    chain->topo_levels = &topo_levels;
2619
2620
0
  if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2621
0
    ctx.changed_paths = 1;
2622
0
  if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2623
    /* We have changed-paths already. Keep them in the next graph */
2624
0
    if (g && g->bloom_filter_settings) {
2625
0
      ctx.changed_paths = 1;
2626
2627
      /* don't propagate the hash_version unless unspecified */
2628
0
      if (bloom_settings.hash_version == -1)
2629
0
        bloom_settings.hash_version = g->bloom_filter_settings->hash_version;
2630
0
      bloom_settings.bits_per_entry = g->bloom_filter_settings->bits_per_entry;
2631
0
      bloom_settings.num_hashes = g->bloom_filter_settings->num_hashes;
2632
0
      bloom_settings.max_changed_paths = g->bloom_filter_settings->max_changed_paths;
2633
0
    }
2634
0
  }
2635
2636
0
  bloom_settings.hash_version = bloom_settings.hash_version == 2 ? 2 : 1;
2637
2638
0
  if (ctx.split) {
2639
0
    for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2640
0
      ctx.num_commit_graphs_before++;
2641
2642
0
    if (ctx.num_commit_graphs_before) {
2643
0
      ALLOC_ARRAY(ctx.commit_graph_filenames_before, ctx.num_commit_graphs_before);
2644
0
      i = ctx.num_commit_graphs_before;
2645
2646
0
      for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2647
0
        ctx.commit_graph_filenames_before[--i] = xstrdup(chain->filename);
2648
0
    }
2649
2650
0
    if (ctx.opts)
2651
0
      replace = ctx.opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2652
0
  }
2653
2654
0
  if (odb_count_objects(r->objects, ODB_COUNT_OBJECTS_APPROXIMATE, &ctx.approx_nr_objects) < 0)
2655
0
    ctx.approx_nr_objects = 0;
2656
2657
0
  if (ctx.append && g) {
2658
0
    for (i = 0; i < g->num_commits; i++) {
2659
0
      struct object_id oid;
2660
0
      oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2661
0
        r->hash_algo);
2662
0
      oid_array_append(&ctx.oids, &oid);
2663
0
    }
2664
0
  }
2665
2666
0
  if (pack_indexes) {
2667
0
    ctx.order_by_pack = 1;
2668
0
    if ((res = fill_oids_from_packs(&ctx, pack_indexes)))
2669
0
      goto cleanup;
2670
0
  }
2671
2672
0
  if (commits) {
2673
0
    if ((res = fill_oids_from_commits(&ctx, commits)))
2674
0
      goto cleanup;
2675
0
  }
2676
2677
0
  if (!pack_indexes && !commits) {
2678
0
    ctx.order_by_pack = 1;
2679
0
    fill_oids_from_all_packs(&ctx);
2680
0
  }
2681
2682
0
  close_reachable(&ctx);
2683
2684
0
  copy_oids_to_commits(&ctx);
2685
2686
0
  if (ctx.commits.nr >= GRAPH_EDGE_LAST_MASK) {
2687
0
    error(_("too many commits to write graph"));
2688
0
    res = -1;
2689
0
    goto cleanup;
2690
0
  }
2691
2692
0
  if (!ctx.commits.nr && !replace)
2693
0
    goto cleanup;
2694
2695
0
  if (ctx.split) {
2696
0
    split_graph_merge_strategy(&ctx, g);
2697
2698
0
    if (!replace)
2699
0
      merge_commit_graphs(&ctx, g);
2700
0
  } else {
2701
0
    ctx.num_commit_graphs_after = 1;
2702
0
  }
2703
2704
0
  ctx.trust_generation_numbers = validate_mixed_generation_chain(g);
2705
2706
0
  compute_topological_levels(&ctx);
2707
0
  if (ctx.write_generation_data)
2708
0
    compute_generation_numbers(&ctx);
2709
2710
0
  if (ctx.changed_paths)
2711
0
    compute_bloom_filters(&ctx);
2712
2713
0
  res = write_commit_graph_file(&ctx);
2714
2715
0
  if (ctx.changed_paths)
2716
0
    deinit_bloom_filters();
2717
2718
0
  if (ctx.split)
2719
0
    mark_commit_graphs(&ctx);
2720
2721
0
  expire_commit_graphs(&ctx);
2722
2723
0
cleanup:
2724
0
  free(ctx.graph_name);
2725
0
  free(ctx.base_graph_name);
2726
0
  free(ctx.dir);
2727
0
  commit_stack_clear(&ctx.commits);
2728
0
  oid_array_clear(&ctx.oids);
2729
0
  clear_topo_level_slab(&topo_levels);
2730
2731
0
  if (ctx.r->objects->commit_graph) {
2732
0
    struct commit_graph *g = ctx.r->objects->commit_graph;
2733
2734
0
    while (g) {
2735
0
      g->topo_levels = NULL;
2736
0
      g = g->base_graph;
2737
0
    }
2738
0
  }
2739
2740
0
  for (i = 0; i < ctx.num_commit_graphs_before; i++)
2741
0
    free(ctx.commit_graph_filenames_before[i]);
2742
0
  free(ctx.commit_graph_filenames_before);
2743
2744
0
  for (i = 0; i < ctx.num_commit_graphs_after; i++) {
2745
0
    free(ctx.commit_graph_filenames_after[i]);
2746
0
    free(ctx.commit_graph_hash_after[i]);
2747
0
  }
2748
0
  free(ctx.commit_graph_filenames_after);
2749
0
  free(ctx.commit_graph_hash_after);
2750
2751
0
  return res;
2752
0
}
2753
2754
0
#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2755
static int verify_commit_graph_error;
2756
2757
__attribute__((format (printf, 1, 2)))
2758
static void graph_report(const char *fmt, ...)
2759
0
{
2760
0
  va_list ap;
2761
2762
0
  verify_commit_graph_error = 1;
2763
0
  va_start(ap, fmt);
2764
0
  vfprintf(stderr, fmt, ap);
2765
0
  fprintf(stderr, "\n");
2766
0
  va_end(ap);
2767
0
}
2768
2769
static int commit_graph_checksum_valid(struct commit_graph *g)
2770
0
{
2771
0
  return hashfile_checksum_valid(g->hash_algo,
2772
0
               g->data, g->data_len);
2773
0
}
2774
2775
static int verify_one_commit_graph(struct commit_graph *g,
2776
           struct progress *progress,
2777
           uint64_t *seen)
2778
0
{
2779
0
  struct repository *r = g->repo;
2780
0
  uint32_t i, cur_fanout_pos = 0;
2781
0
  struct object_id prev_oid, cur_oid;
2782
0
  struct commit *seen_gen_zero = NULL;
2783
0
  struct commit *seen_gen_non_zero = NULL;
2784
2785
0
  if (!commit_graph_checksum_valid(g)) {
2786
0
    graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2787
0
    verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2788
0
  }
2789
2790
0
  for (i = 0; i < g->num_commits; i++) {
2791
0
    struct commit *graph_commit;
2792
2793
0
    oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2794
0
      g->hash_algo);
2795
2796
0
    if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2797
0
      graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2798
0
             oid_to_hex(&prev_oid),
2799
0
             oid_to_hex(&cur_oid));
2800
2801
0
    oidcpy(&prev_oid, &cur_oid);
2802
2803
0
    while (cur_oid.hash[0] > cur_fanout_pos) {
2804
0
      uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2805
2806
0
      if (i != fanout_value)
2807
0
        graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2808
0
               cur_fanout_pos, fanout_value, i);
2809
0
      cur_fanout_pos++;
2810
0
    }
2811
2812
0
    graph_commit = lookup_commit(r, &cur_oid);
2813
0
    if (!parse_commit_in_graph_one(g, graph_commit))
2814
0
      graph_report(_("failed to parse commit %s from commit-graph"),
2815
0
             oid_to_hex(&cur_oid));
2816
0
  }
2817
2818
0
  while (cur_fanout_pos < 256) {
2819
0
    uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2820
2821
0
    if (g->num_commits != fanout_value)
2822
0
      graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2823
0
             cur_fanout_pos, fanout_value, i);
2824
2825
0
    cur_fanout_pos++;
2826
0
  }
2827
2828
0
  if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2829
0
    return verify_commit_graph_error;
2830
2831
0
  for (i = 0; i < g->num_commits; i++) {
2832
0
    struct commit *graph_commit, *odb_commit;
2833
0
    struct commit_list *graph_parents, *odb_parents;
2834
0
    timestamp_t max_generation = 0;
2835
0
    timestamp_t generation;
2836
2837
0
    display_progress(progress, ++(*seen));
2838
0
    oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2839
0
      g->hash_algo);
2840
2841
0
    graph_commit = lookup_commit(r, &cur_oid);
2842
0
    odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2843
0
    if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2844
0
      graph_report(_("failed to parse commit %s from object database for commit-graph"),
2845
0
             oid_to_hex(&cur_oid));
2846
0
      continue;
2847
0
    }
2848
2849
0
    if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
2850
0
         get_commit_tree_oid(odb_commit)))
2851
0
      graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2852
0
             oid_to_hex(&cur_oid),
2853
0
             oid_to_hex(get_commit_tree_oid(graph_commit)),
2854
0
             oid_to_hex(get_commit_tree_oid(odb_commit)));
2855
2856
0
    graph_parents = graph_commit->parents;
2857
0
    odb_parents = odb_commit->parents;
2858
2859
0
    while (graph_parents) {
2860
0
      if (!odb_parents) {
2861
0
        graph_report(_("commit-graph parent list for commit %s is too long"),
2862
0
               oid_to_hex(&cur_oid));
2863
0
        break;
2864
0
      }
2865
2866
      /* parse parent in case it is in a base graph */
2867
0
      parse_commit_in_graph_one(g, graph_parents->item);
2868
2869
0
      if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2870
0
        graph_report(_("commit-graph parent for %s is %s != %s"),
2871
0
               oid_to_hex(&cur_oid),
2872
0
               oid_to_hex(&graph_parents->item->object.oid),
2873
0
               oid_to_hex(&odb_parents->item->object.oid));
2874
2875
0
      generation = commit_graph_generation_from_graph(graph_parents->item);
2876
0
      if (generation > max_generation)
2877
0
        max_generation = generation;
2878
2879
0
      graph_parents = graph_parents->next;
2880
0
      odb_parents = odb_parents->next;
2881
0
    }
2882
2883
0
    if (odb_parents)
2884
0
      graph_report(_("commit-graph parent list for commit %s terminates early"),
2885
0
             oid_to_hex(&cur_oid));
2886
2887
0
    if (commit_graph_generation_from_graph(graph_commit))
2888
0
      seen_gen_non_zero = graph_commit;
2889
0
    else
2890
0
      seen_gen_zero = graph_commit;
2891
2892
0
    if (seen_gen_zero)
2893
0
      continue;
2894
2895
    /*
2896
     * If we are using topological level and one of our parents has
2897
     * generation GENERATION_NUMBER_V1_MAX, then our generation is
2898
     * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2899
     * in the following condition.
2900
     */
2901
0
    if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2902
0
      max_generation--;
2903
2904
0
    generation = commit_graph_generation(graph_commit);
2905
0
    if (generation < max_generation + 1)
2906
0
      graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2907
0
             oid_to_hex(&cur_oid),
2908
0
             generation,
2909
0
             max_generation + 1);
2910
2911
0
    if (graph_commit->date != odb_commit->date)
2912
0
      graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2913
0
             oid_to_hex(&cur_oid),
2914
0
             graph_commit->date,
2915
0
             odb_commit->date);
2916
0
  }
2917
2918
0
  if (seen_gen_zero && seen_gen_non_zero)
2919
0
    graph_report(_("commit-graph has both zero and non-zero "
2920
0
             "generations (e.g., commits '%s' and '%s')"),
2921
0
           oid_to_hex(&seen_gen_zero->object.oid),
2922
0
           oid_to_hex(&seen_gen_non_zero->object.oid));
2923
2924
0
  return verify_commit_graph_error;
2925
0
}
2926
2927
int verify_commit_graph(struct commit_graph *g, int flags)
2928
0
{
2929
0
  struct progress *progress = NULL;
2930
0
  int local_error = 0;
2931
0
  uint64_t seen = 0;
2932
2933
0
  if (!g) {
2934
0
    graph_report("no commit-graph file loaded");
2935
0
    return 1;
2936
0
  }
2937
2938
0
  if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2939
0
    uint64_t total = g->num_commits;
2940
0
    if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2941
0
      total += g->num_commits_in_base;
2942
2943
0
    progress = start_progress(g->repo,
2944
0
            _("Verifying commits in commit graph"),
2945
0
            total);
2946
0
  }
2947
2948
0
  for (; g; g = g->base_graph) {
2949
0
    local_error |= verify_one_commit_graph(g, progress, &seen);
2950
0
    if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2951
0
      break;
2952
0
  }
2953
2954
0
  stop_progress(&progress);
2955
2956
0
  return local_error;
2957
0
}
2958
2959
void free_commit_graph(struct commit_graph *g)
2960
0
{
2961
0
  while (g) {
2962
0
    struct commit_graph *next = g->base_graph;
2963
2964
0
    if (g->data)
2965
0
      munmap((void *)g->data, g->data_len);
2966
0
    free(g->dir);
2967
0
    free(g->filename);
2968
0
    free(g->bloom_filter_settings);
2969
0
    free(g);
2970
2971
0
    g = next;
2972
0
  }
2973
0
}
2974
2975
void disable_commit_graph(struct repository *r)
2976
0
{
2977
0
  r->commit_graph_disabled = 1;
2978
0
}