Coverage Report

Created: 2026-02-14 06:27

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