Coverage Report

Created: 2025-12-31 07:02

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
59
#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
45
26
#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
46
12
#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
47
6
#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
54
#define GRAPH_VERSION_1 0x1
56
54
#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
166
#define GRAPH_HEADER_SIZE 8
65
113
#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
6
{
80
6
  return algop->rawsz + 16;
81
6
}
82
83
static size_t graph_min_size(const struct git_hash_algo *algop)
84
59
{
85
59
  return GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE + GRAPH_FANOUT_SIZE + algop->rawsz;
86
59
}
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
54
{
218
54
  struct commit_graph *g = xcalloc(1, sizeof(*g));
219
220
54
  return g;
221
54
}
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
25
{
285
25
  struct commit_graph *g = data;
286
25
  int i;
287
288
25
  if (chunk_size != 256 * sizeof(uint32_t))
289
10
    return error(_("commit-graph oid fanout chunk is wrong size"));
290
15
  g->chunk_oid_fanout = (const uint32_t *)chunk_start;
291
15
  g->num_commits = ntohl(g->chunk_oid_fanout[255]);
292
293
3.18k
  for (i = 0; i < 255; i++) {
294
3.17k
    uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
295
3.17k
    uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
296
297
3.17k
    if (oid_fanout1 > oid_fanout2) {
298
3
      error(_("commit-graph fanout values out of order"));
299
3
      return 1;
300
3
    }
301
3.17k
  }
302
303
12
  return 0;
304
15
}
305
306
static int graph_read_oid_lookup(const unsigned char *chunk_start,
307
         size_t chunk_size, void *data)
308
12
{
309
12
  struct commit_graph *g = data;
310
12
  g->chunk_oid_lookup = chunk_start;
311
12
  if (chunk_size / g->hash_algo->rawsz != g->num_commits)
312
6
    return error(_("commit-graph OID lookup chunk is the wrong size"));
313
6
  return 0;
314
12
}
315
316
static int graph_read_commit_data(const unsigned char *chunk_start,
317
          size_t chunk_size, void *data)
318
6
{
319
6
  struct commit_graph *g = data;
320
6
  if (chunk_size / graph_data_width(g->hash_algo) != g->num_commits)
321
6
    return error(_("commit-graph commit data chunk is wrong size"));
322
0
  g->chunk_commit_data = chunk_start;
323
0
  return 0;
324
6
}
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
59
{
376
59
  const unsigned char *data;
377
59
  struct commit_graph *graph;
378
59
  uint32_t graph_signature;
379
59
  unsigned char graph_version, hash_version;
380
59
  struct chunkfile *cf = NULL;
381
382
59
  if (!graph_map)
383
0
    return NULL;
384
385
59
  if (graph_size < graph_min_size(r->hash_algo))
386
0
    return NULL;
387
388
59
  data = (const unsigned char *)graph_map;
389
390
59
  graph_signature = get_be32(data);
391
59
  if (graph_signature != GRAPH_SIGNATURE) {
392
5
    error(_("commit-graph signature %X does not match signature %X"),
393
5
          graph_signature, GRAPH_SIGNATURE);
394
5
    return NULL;
395
5
  }
396
397
54
  graph_version = *(unsigned char*)(data + 4);
398
54
  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
54
  hash_version = *(unsigned char*)(data + 5);
405
54
  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
54
  graph = alloc_commit_graph();
412
413
54
  graph->hash_algo = r->hash_algo;
414
54
  graph->num_chunks = *(unsigned char*)(data + 6);
415
54
  graph->data = graph_map;
416
54
  graph->data_len = graph_size;
417
418
54
  if (graph_size < GRAPH_HEADER_SIZE +
419
54
       (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
420
54
       GRAPH_FANOUT_SIZE + r->hash_algo->rawsz) {
421
1
    error(_("commit-graph file is too small to hold %u chunks"),
422
1
          graph->num_chunks);
423
1
    free(graph);
424
1
    return NULL;
425
1
  }
426
427
53
  cf = init_chunkfile(NULL);
428
429
53
  if (read_table_of_contents(cf, graph->data, graph_size,
430
53
           GRAPH_HEADER_SIZE, graph->num_chunks, 1))
431
27
    goto free_and_return;
432
433
26
  if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
434
14
    error(_("commit-graph required OID fanout chunk missing or corrupted"));
435
14
    goto free_and_return;
436
14
  }
437
12
  if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
438
6
    error(_("commit-graph required OID lookup chunk missing or corrupted"));
439
6
    goto free_and_return;
440
6
  }
441
6
  if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
442
6
    error(_("commit-graph required commit data chunk missing or corrupted"));
443
6
    goto free_and_return;
444
6
  }
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
53
free_and_return:
487
53
  free_chunkfile(cf);
488
53
  free(graph->bloom_filter_settings);
489
53
  free(graph);
490
53
  return NULL;
491
6
}
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
      free_commit_list(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 packed_commit_list {
1131
  struct commit **list;
1132
  size_t nr;
1133
  size_t alloc;
1134
};
1135
1136
struct write_commit_graph_context {
1137
  struct repository *r;
1138
  struct odb_source *odb_source;
1139
  char *graph_name;
1140
  struct oid_array oids;
1141
  struct packed_commit_list commits;
1142
  int num_extra_edges;
1143
  int num_generation_data_overflows;
1144
  unsigned long approx_nr_objects;
1145
  struct progress *progress;
1146
  int progress_done;
1147
  uint64_t progress_cnt;
1148
1149
  char *base_graph_name;
1150
  int num_commit_graphs_before;
1151
  int num_commit_graphs_after;
1152
  char **commit_graph_filenames_before;
1153
  char **commit_graph_filenames_after;
1154
  char **commit_graph_hash_after;
1155
  uint32_t new_num_commits_in_base;
1156
  struct commit_graph *new_base_graph;
1157
1158
  unsigned append:1,
1159
     report_progress:1,
1160
     split:1,
1161
     changed_paths:1,
1162
     order_by_pack:1,
1163
     write_generation_data:1,
1164
     trust_generation_numbers:1;
1165
1166
  struct topo_level_slab *topo_levels;
1167
  const struct commit_graph_opts *opts;
1168
  size_t total_bloom_filter_data_size;
1169
  const struct bloom_filter_settings *bloom_settings;
1170
1171
  int count_bloom_filter_computed;
1172
  int count_bloom_filter_not_computed;
1173
  int count_bloom_filter_trunc_empty;
1174
  int count_bloom_filter_trunc_large;
1175
  int count_bloom_filter_upgraded;
1176
};
1177
1178
static int write_graph_chunk_fanout(struct hashfile *f,
1179
            void *data)
1180
0
{
1181
0
  struct write_commit_graph_context *ctx = data;
1182
0
  int i, count = 0;
1183
0
  struct commit **list = ctx->commits.list;
1184
1185
  /*
1186
   * Write the first-level table (the list is sorted,
1187
   * but we use a 256-entry lookup to be able to avoid
1188
   * having to do eight extra binary search iterations).
1189
   */
1190
0
  for (i = 0; i < 256; i++) {
1191
0
    while (count < ctx->commits.nr) {
1192
0
      if ((*list)->object.oid.hash[0] != i)
1193
0
        break;
1194
0
      display_progress(ctx->progress, ++ctx->progress_cnt);
1195
0
      count++;
1196
0
      list++;
1197
0
    }
1198
1199
0
    hashwrite_be32(f, count);
1200
0
  }
1201
1202
0
  return 0;
1203
0
}
1204
1205
static int write_graph_chunk_oids(struct hashfile *f,
1206
          void *data)
1207
0
{
1208
0
  struct write_commit_graph_context *ctx = data;
1209
0
  struct commit **list = ctx->commits.list;
1210
0
  int count;
1211
0
  for (count = 0; count < ctx->commits.nr; count++, list++) {
1212
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1213
0
    hashwrite(f, (*list)->object.oid.hash, f->algop->rawsz);
1214
0
  }
1215
1216
0
  return 0;
1217
0
}
1218
1219
static const struct object_id *commit_to_oid(size_t index, const void *table)
1220
0
{
1221
0
  const struct commit * const *commits = table;
1222
0
  return &commits[index]->object.oid;
1223
0
}
1224
1225
static int write_graph_chunk_data(struct hashfile *f,
1226
          void *data)
1227
0
{
1228
0
  struct write_commit_graph_context *ctx = data;
1229
0
  struct commit **list = ctx->commits.list;
1230
0
  struct commit **last = ctx->commits.list + ctx->commits.nr;
1231
0
  uint32_t num_extra_edges = 0;
1232
1233
0
  while (list < last) {
1234
0
    struct commit_list *parent;
1235
0
    struct object_id *tree;
1236
0
    int edge_value;
1237
0
    uint32_t packedDate[2];
1238
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1239
1240
0
    if (repo_parse_commit_no_graph(ctx->r, *list))
1241
0
      die(_("unable to parse commit %s"),
1242
0
        oid_to_hex(&(*list)->object.oid));
1243
0
    tree = get_commit_tree_oid(*list);
1244
0
    hashwrite(f, tree->hash, ctx->r->hash_algo->rawsz);
1245
1246
0
    parent = (*list)->parents;
1247
1248
0
    if (!parent)
1249
0
      edge_value = GRAPH_PARENT_NONE;
1250
0
    else {
1251
0
      edge_value = oid_pos(&parent->item->object.oid,
1252
0
               ctx->commits.list,
1253
0
               ctx->commits.nr,
1254
0
               commit_to_oid);
1255
1256
0
      if (edge_value >= 0)
1257
0
        edge_value += ctx->new_num_commits_in_base;
1258
0
      else if (ctx->new_base_graph) {
1259
0
        uint32_t pos;
1260
0
        if (find_commit_pos_in_graph(parent->item,
1261
0
                   ctx->new_base_graph,
1262
0
                   &pos))
1263
0
          edge_value = pos;
1264
0
      }
1265
1266
0
      if (edge_value < 0)
1267
0
        BUG("missing parent %s for commit %s",
1268
0
            oid_to_hex(&parent->item->object.oid),
1269
0
            oid_to_hex(&(*list)->object.oid));
1270
0
    }
1271
1272
0
    hashwrite_be32(f, edge_value);
1273
1274
0
    if (parent)
1275
0
      parent = parent->next;
1276
1277
0
    if (!parent)
1278
0
      edge_value = GRAPH_PARENT_NONE;
1279
0
    else if (parent->next)
1280
0
      edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1281
0
    else {
1282
0
      edge_value = oid_pos(&parent->item->object.oid,
1283
0
               ctx->commits.list,
1284
0
               ctx->commits.nr,
1285
0
               commit_to_oid);
1286
1287
0
      if (edge_value >= 0)
1288
0
        edge_value += ctx->new_num_commits_in_base;
1289
0
      else if (ctx->new_base_graph) {
1290
0
        uint32_t pos;
1291
0
        if (find_commit_pos_in_graph(parent->item,
1292
0
                   ctx->new_base_graph,
1293
0
                   &pos))
1294
0
          edge_value = pos;
1295
0
      }
1296
1297
0
      if (edge_value < 0)
1298
0
        BUG("missing parent %s for commit %s",
1299
0
            oid_to_hex(&parent->item->object.oid),
1300
0
            oid_to_hex(&(*list)->object.oid));
1301
0
    }
1302
1303
0
    hashwrite_be32(f, edge_value);
1304
1305
0
    if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1306
0
      do {
1307
0
        num_extra_edges++;
1308
0
        parent = parent->next;
1309
0
      } while (parent);
1310
0
    }
1311
1312
0
    if (sizeof((*list)->date) > 4)
1313
0
      packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1314
0
    else
1315
0
      packedDate[0] = 0;
1316
1317
0
    packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1318
1319
0
    packedDate[1] = htonl((*list)->date);
1320
0
    hashwrite(f, packedDate, 8);
1321
1322
0
    list++;
1323
0
  }
1324
1325
0
  return 0;
1326
0
}
1327
1328
static int write_graph_chunk_generation_data(struct hashfile *f,
1329
               void *data)
1330
0
{
1331
0
  struct write_commit_graph_context *ctx = data;
1332
0
  int i, num_generation_data_overflows = 0;
1333
1334
0
  for (i = 0; i < ctx->commits.nr; i++) {
1335
0
    struct commit *c = ctx->commits.list[i];
1336
0
    timestamp_t offset;
1337
0
    repo_parse_commit(ctx->r, c);
1338
0
    offset = commit_graph_data_at(c)->generation - c->date;
1339
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1340
1341
0
    if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1342
0
      offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1343
0
      num_generation_data_overflows++;
1344
0
    }
1345
1346
0
    hashwrite_be32(f, offset);
1347
0
  }
1348
1349
0
  return 0;
1350
0
}
1351
1352
static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1353
                  void *data)
1354
0
{
1355
0
  struct write_commit_graph_context *ctx = data;
1356
0
  int i;
1357
0
  for (i = 0; i < ctx->commits.nr; i++) {
1358
0
    struct commit *c = ctx->commits.list[i];
1359
0
    timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1360
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1361
1362
0
    if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1363
0
      hashwrite_be32(f, offset >> 32);
1364
0
      hashwrite_be32(f, (uint32_t) offset);
1365
0
    }
1366
0
  }
1367
1368
0
  return 0;
1369
0
}
1370
1371
static int write_graph_chunk_extra_edges(struct hashfile *f,
1372
           void *data)
1373
0
{
1374
0
  struct write_commit_graph_context *ctx = data;
1375
0
  struct commit **list = ctx->commits.list;
1376
0
  struct commit **last = ctx->commits.list + ctx->commits.nr;
1377
0
  struct commit_list *parent;
1378
1379
0
  while (list < last) {
1380
0
    int num_parents = 0;
1381
1382
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1383
1384
0
    for (parent = (*list)->parents; num_parents < 3 && parent;
1385
0
         parent = parent->next)
1386
0
      num_parents++;
1387
1388
0
    if (num_parents <= 2) {
1389
0
      list++;
1390
0
      continue;
1391
0
    }
1392
1393
    /* Since num_parents > 2, this initializer is safe. */
1394
0
    for (parent = (*list)->parents->next; parent; parent = parent->next) {
1395
0
      int edge_value = oid_pos(&parent->item->object.oid,
1396
0
             ctx->commits.list,
1397
0
             ctx->commits.nr,
1398
0
             commit_to_oid);
1399
1400
0
      if (edge_value >= 0)
1401
0
        edge_value += ctx->new_num_commits_in_base;
1402
0
      else if (ctx->new_base_graph) {
1403
0
        uint32_t pos;
1404
0
        if (find_commit_pos_in_graph(parent->item,
1405
0
                   ctx->new_base_graph,
1406
0
                   &pos))
1407
0
          edge_value = pos;
1408
0
      }
1409
1410
0
      if (edge_value < 0)
1411
0
        BUG("missing parent %s for commit %s",
1412
0
            oid_to_hex(&parent->item->object.oid),
1413
0
            oid_to_hex(&(*list)->object.oid));
1414
0
      else if (!parent->next)
1415
0
        edge_value |= GRAPH_LAST_EDGE;
1416
1417
0
      hashwrite_be32(f, edge_value);
1418
0
    }
1419
1420
0
    list++;
1421
0
  }
1422
1423
0
  return 0;
1424
0
}
1425
1426
static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1427
             void *data)
1428
0
{
1429
0
  struct write_commit_graph_context *ctx = data;
1430
0
  struct commit **list = ctx->commits.list;
1431
0
  struct commit **last = ctx->commits.list + ctx->commits.nr;
1432
0
  uint32_t cur_pos = 0;
1433
1434
0
  while (list < last) {
1435
0
    struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1436
0
    size_t len = filter ? filter->len : 0;
1437
0
    cur_pos += len;
1438
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1439
0
    hashwrite_be32(f, cur_pos);
1440
0
    list++;
1441
0
  }
1442
1443
0
  return 0;
1444
0
}
1445
1446
static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1447
0
{
1448
0
  struct json_writer jw = JSON_WRITER_INIT;
1449
1450
0
  jw_object_begin(&jw, 0);
1451
0
  jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1452
0
  jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1453
0
  jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1454
0
  jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1455
0
  jw_end(&jw);
1456
1457
0
  trace2_data_json("bloom", ctx->r, "settings", &jw);
1458
1459
0
  jw_release(&jw);
1460
0
}
1461
1462
static int write_graph_chunk_bloom_data(struct hashfile *f,
1463
          void *data)
1464
0
{
1465
0
  struct write_commit_graph_context *ctx = data;
1466
0
  struct commit **list = ctx->commits.list;
1467
0
  struct commit **last = ctx->commits.list + ctx->commits.nr;
1468
1469
0
  trace2_bloom_filter_settings(ctx);
1470
1471
0
  hashwrite_be32(f, ctx->bloom_settings->hash_version);
1472
0
  hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1473
0
  hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1474
1475
0
  while (list < last) {
1476
0
    struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1477
0
    size_t len = filter ? filter->len : 0;
1478
1479
0
    display_progress(ctx->progress, ++ctx->progress_cnt);
1480
0
    if (len)
1481
0
      hashwrite(f, filter->data, len * sizeof(unsigned char));
1482
0
    list++;
1483
0
  }
1484
1485
0
  return 0;
1486
0
}
1487
1488
static int add_packed_commits(const struct object_id *oid,
1489
            struct packed_git *pack,
1490
            uint32_t pos,
1491
            void *data)
1492
0
{
1493
0
  struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1494
0
  enum object_type type;
1495
0
  off_t offset = nth_packed_object_offset(pack, pos);
1496
0
  struct object_info oi = OBJECT_INFO_INIT;
1497
1498
0
  if (ctx->progress)
1499
0
    display_progress(ctx->progress, ++ctx->progress_done);
1500
1501
0
  oi.typep = &type;
1502
0
  if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1503
0
    die(_("unable to get type of object %s"), oid_to_hex(oid));
1504
1505
0
  if (type != OBJ_COMMIT)
1506
0
    return 0;
1507
1508
0
  oid_array_append(&ctx->oids, oid);
1509
0
  set_commit_pos(ctx->r, oid);
1510
1511
0
  return 0;
1512
0
}
1513
1514
static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1515
0
{
1516
0
  struct commit_list *parent;
1517
0
  for (parent = commit->parents; parent; parent = parent->next) {
1518
0
    if (!(parent->item->object.flags & REACHABLE)) {
1519
0
      oid_array_append(&ctx->oids, &parent->item->object.oid);
1520
0
      parent->item->object.flags |= REACHABLE;
1521
0
    }
1522
0
  }
1523
0
}
1524
1525
static void close_reachable(struct write_commit_graph_context *ctx)
1526
0
{
1527
0
  int i;
1528
0
  struct commit *commit;
1529
0
  enum commit_graph_split_flags flags = ctx->opts ?
1530
0
    ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1531
1532
0
  if (ctx->report_progress)
1533
0
    ctx->progress = start_delayed_progress(
1534
0
          ctx->r,
1535
0
          _("Loading known commits in commit graph"),
1536
0
          ctx->oids.nr);
1537
0
  for (i = 0; i < ctx->oids.nr; i++) {
1538
0
    display_progress(ctx->progress, i + 1);
1539
0
    commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1540
0
    if (commit)
1541
0
      commit->object.flags |= REACHABLE;
1542
0
  }
1543
0
  stop_progress(&ctx->progress);
1544
1545
  /*
1546
   * As this loop runs, ctx->oids.nr may grow, but not more
1547
   * than the number of missing commits in the reachable
1548
   * closure.
1549
   */
1550
0
  if (ctx->report_progress)
1551
0
    ctx->progress = start_delayed_progress(
1552
0
          ctx->r,
1553
0
          _("Expanding reachable commits in commit graph"),
1554
0
          0);
1555
0
  for (i = 0; i < ctx->oids.nr; i++) {
1556
0
    display_progress(ctx->progress, i + 1);
1557
0
    commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1558
1559
0
    if (!commit)
1560
0
      continue;
1561
0
    if (ctx->split) {
1562
0
      if ((!repo_parse_commit(ctx->r, commit) &&
1563
0
           commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1564
0
          flags == COMMIT_GRAPH_SPLIT_REPLACE)
1565
0
        add_missing_parents(ctx, commit);
1566
0
    } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1567
0
      add_missing_parents(ctx, commit);
1568
0
  }
1569
0
  stop_progress(&ctx->progress);
1570
1571
0
  if (ctx->report_progress)
1572
0
    ctx->progress = start_delayed_progress(
1573
0
          ctx->r,
1574
0
          _("Clearing commit marks in commit graph"),
1575
0
          ctx->oids.nr);
1576
0
  for (i = 0; i < ctx->oids.nr; i++) {
1577
0
    display_progress(ctx->progress, i + 1);
1578
0
    commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1579
1580
0
    if (commit)
1581
0
      commit->object.flags &= ~REACHABLE;
1582
0
  }
1583
0
  stop_progress(&ctx->progress);
1584
0
}
1585
1586
struct compute_generation_info {
1587
  struct repository *r;
1588
  struct packed_commit_list *commits;
1589
  struct progress *progress;
1590
  int progress_cnt;
1591
1592
  timestamp_t (*get_generation)(struct commit *c, void *data);
1593
  void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1594
  void *data;
1595
};
1596
1597
static timestamp_t compute_generation_from_max(struct commit *c,
1598
                 timestamp_t max_gen,
1599
                 int generation_version)
1600
0
{
1601
0
  switch (generation_version) {
1602
0
  case 1: /* topological levels */
1603
0
    if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1604
0
      max_gen = GENERATION_NUMBER_V1_MAX - 1;
1605
0
    return max_gen + 1;
1606
1607
0
  case 2: /* corrected commit date */
1608
0
    if (c->date && c->date > max_gen)
1609
0
      max_gen = c->date - 1;
1610
0
    return max_gen + 1;
1611
1612
0
  default:
1613
0
    BUG("attempting unimplemented version");
1614
0
  }
1615
0
}
1616
1617
static void compute_reachable_generation_numbers(
1618
      struct compute_generation_info *info,
1619
      int generation_version)
1620
0
{
1621
0
  int i;
1622
0
  struct commit_list *list = NULL;
1623
1624
0
  for (i = 0; i < info->commits->nr; i++) {
1625
0
    struct commit *c = info->commits->list[i];
1626
0
    timestamp_t gen;
1627
0
    repo_parse_commit(info->r, c);
1628
0
    gen = info->get_generation(c, info->data);
1629
0
    display_progress(info->progress, ++info->progress_cnt);
1630
1631
0
    if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1632
0
      continue;
1633
1634
0
    commit_list_insert(c, &list);
1635
0
    while (list) {
1636
0
      struct commit *current = list->item;
1637
0
      struct commit_list *parent;
1638
0
      int all_parents_computed = 1;
1639
0
      uint32_t max_gen = 0;
1640
1641
0
      for (parent = current->parents; parent; parent = parent->next) {
1642
0
        repo_parse_commit(info->r, parent->item);
1643
0
        gen = info->get_generation(parent->item, info->data);
1644
1645
0
        if (gen == GENERATION_NUMBER_ZERO) {
1646
0
          all_parents_computed = 0;
1647
0
          commit_list_insert(parent->item, &list);
1648
0
          break;
1649
0
        }
1650
1651
0
        if (gen > max_gen)
1652
0
          max_gen = gen;
1653
0
      }
1654
1655
0
      if (all_parents_computed) {
1656
0
        pop_commit(&list);
1657
0
        gen = compute_generation_from_max(
1658
0
            current, max_gen,
1659
0
            generation_version);
1660
0
        info->set_generation(current, gen, info->data);
1661
0
      }
1662
0
    }
1663
0
  }
1664
0
}
1665
1666
static timestamp_t get_topo_level(struct commit *c, void *data)
1667
0
{
1668
0
  struct write_commit_graph_context *ctx = data;
1669
0
  return *topo_level_slab_at(ctx->topo_levels, c);
1670
0
}
1671
1672
static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1673
0
{
1674
0
  struct write_commit_graph_context *ctx = data;
1675
0
  *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1676
0
}
1677
1678
static void compute_topological_levels(struct write_commit_graph_context *ctx)
1679
0
{
1680
0
  struct compute_generation_info info = {
1681
0
    .r = ctx->r,
1682
0
    .commits = &ctx->commits,
1683
0
    .get_generation = get_topo_level,
1684
0
    .set_generation = set_topo_level,
1685
0
    .data = ctx,
1686
0
  };
1687
1688
0
  if (ctx->report_progress)
1689
0
    info.progress = ctx->progress
1690
0
            = start_delayed_progress(
1691
0
          ctx->r,
1692
0
          _("Computing commit graph topological levels"),
1693
0
          ctx->commits.nr);
1694
1695
0
  compute_reachable_generation_numbers(&info, 1);
1696
1697
0
  stop_progress(&ctx->progress);
1698
0
}
1699
1700
static timestamp_t get_generation_from_graph_data(struct commit *c,
1701
              void *data UNUSED)
1702
0
{
1703
0
  return commit_graph_data_at(c)->generation;
1704
0
}
1705
1706
static void set_generation_v2(struct commit *c, timestamp_t t,
1707
            void *data UNUSED)
1708
0
{
1709
0
  struct commit_graph_data *g = commit_graph_data_at(c);
1710
0
  g->generation = t;
1711
0
}
1712
1713
static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1714
0
{
1715
0
  int i;
1716
0
  struct compute_generation_info info = {
1717
0
    .r = ctx->r,
1718
0
    .commits = &ctx->commits,
1719
0
    .get_generation = get_generation_from_graph_data,
1720
0
    .set_generation = set_generation_v2,
1721
0
  };
1722
1723
0
  if (ctx->report_progress)
1724
0
    info.progress = ctx->progress
1725
0
            = start_delayed_progress(
1726
0
          ctx->r,
1727
0
          _("Computing commit graph generation numbers"),
1728
0
          ctx->commits.nr);
1729
1730
0
  if (!ctx->trust_generation_numbers) {
1731
0
    for (i = 0; i < ctx->commits.nr; i++) {
1732
0
      struct commit *c = ctx->commits.list[i];
1733
0
      repo_parse_commit(ctx->r, c);
1734
0
      commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1735
0
    }
1736
0
  }
1737
1738
0
  compute_reachable_generation_numbers(&info, 2);
1739
1740
0
  for (i = 0; i < ctx->commits.nr; i++) {
1741
0
    struct commit *c = ctx->commits.list[i];
1742
0
    timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1743
0
    if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1744
0
      ctx->num_generation_data_overflows++;
1745
0
  }
1746
0
  stop_progress(&ctx->progress);
1747
0
}
1748
1749
static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1750
           void *data UNUSED)
1751
0
{
1752
0
  commit_graph_data_at(c)->generation = t;
1753
0
}
1754
1755
/*
1756
 * After this method, all commits reachable from those in the given
1757
 * list will have non-zero, non-infinite generation numbers.
1758
 */
1759
void ensure_generations_valid(struct repository *r,
1760
            struct commit **commits, size_t nr)
1761
0
{
1762
0
  int generation_version = get_configured_generation_version(r);
1763
0
  struct packed_commit_list list = {
1764
0
    .list = commits,
1765
0
    .alloc = nr,
1766
0
    .nr = nr,
1767
0
  };
1768
0
  struct compute_generation_info info = {
1769
0
    .r = r,
1770
0
    .commits = &list,
1771
0
    .get_generation = get_generation_from_graph_data,
1772
0
    .set_generation = set_generation_in_graph_data,
1773
0
  };
1774
1775
0
  compute_reachable_generation_numbers(&info, generation_version);
1776
0
}
1777
1778
static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1779
0
{
1780
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1781
0
         ctx->count_bloom_filter_computed);
1782
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1783
0
         ctx->count_bloom_filter_not_computed);
1784
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1785
0
         ctx->count_bloom_filter_trunc_empty);
1786
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1787
0
         ctx->count_bloom_filter_trunc_large);
1788
0
  trace2_data_intmax("commit-graph", ctx->r, "filter-upgraded",
1789
0
         ctx->count_bloom_filter_upgraded);
1790
0
}
1791
1792
static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1793
0
{
1794
0
  int i;
1795
0
  struct progress *progress = NULL;
1796
0
  struct commit **sorted_commits;
1797
0
  int max_new_filters;
1798
1799
0
  init_bloom_filters();
1800
1801
0
  if (ctx->report_progress)
1802
0
    progress = start_delayed_progress(
1803
0
      ctx->r,
1804
0
      _("Computing commit changed paths Bloom filters"),
1805
0
      ctx->commits.nr);
1806
1807
0
  DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1808
1809
0
  if (ctx->order_by_pack)
1810
0
    QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1811
0
  else
1812
0
    QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1813
1814
0
  max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1815
0
    ctx->opts->max_new_filters : ctx->commits.nr;
1816
1817
0
  for (i = 0; i < ctx->commits.nr; i++) {
1818
0
    enum bloom_filter_computed computed = 0;
1819
0
    struct commit *c = sorted_commits[i];
1820
0
    struct bloom_filter *filter = get_or_compute_bloom_filter(
1821
0
      ctx->r,
1822
0
      c,
1823
0
      ctx->count_bloom_filter_computed < max_new_filters,
1824
0
      ctx->bloom_settings,
1825
0
      &computed);
1826
0
    if (computed & BLOOM_COMPUTED) {
1827
0
      ctx->count_bloom_filter_computed++;
1828
0
      if (computed & BLOOM_TRUNC_EMPTY)
1829
0
        ctx->count_bloom_filter_trunc_empty++;
1830
0
      if (computed & BLOOM_TRUNC_LARGE)
1831
0
        ctx->count_bloom_filter_trunc_large++;
1832
0
    } else if (computed & BLOOM_UPGRADED) {
1833
0
      ctx->count_bloom_filter_upgraded++;
1834
0
    } else if (computed & BLOOM_NOT_COMPUTED)
1835
0
      ctx->count_bloom_filter_not_computed++;
1836
0
    ctx->total_bloom_filter_data_size += filter
1837
0
      ? sizeof(unsigned char) * filter->len : 0;
1838
0
    display_progress(progress, i + 1);
1839
0
  }
1840
1841
0
  if (trace2_is_enabled())
1842
0
    trace2_bloom_filter_write_statistics(ctx);
1843
1844
0
  free(sorted_commits);
1845
0
  stop_progress(&progress);
1846
0
}
1847
1848
struct refs_cb_data {
1849
  struct repository *repo;
1850
  struct oidset *commits;
1851
  struct progress *progress;
1852
};
1853
1854
static int add_ref_to_set(const struct reference *ref, void *cb_data)
1855
0
{
1856
0
  const struct object_id *maybe_peeled = ref->oid;
1857
0
  struct object_id peeled;
1858
0
  struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1859
1860
0
  if (!reference_get_peeled_oid(data->repo, ref, &peeled))
1861
0
    maybe_peeled = &peeled;
1862
0
  if (odb_read_object_info(data->repo->objects, maybe_peeled, NULL) == OBJ_COMMIT)
1863
0
    oidset_insert(data->commits, maybe_peeled);
1864
1865
0
  display_progress(data->progress, oidset_size(data->commits));
1866
1867
0
  return 0;
1868
0
}
1869
1870
int write_commit_graph_reachable(struct odb_source *source,
1871
         enum commit_graph_write_flags flags,
1872
         const struct commit_graph_opts *opts)
1873
0
{
1874
0
  struct oidset commits = OIDSET_INIT;
1875
0
  struct refs_cb_data data;
1876
0
  int result;
1877
1878
0
  memset(&data, 0, sizeof(data));
1879
0
  data.repo = source->odb->repo;
1880
0
  data.commits = &commits;
1881
1882
0
  if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1883
0
    data.progress = start_delayed_progress(
1884
0
      source->odb->repo,
1885
0
      _("Collecting referenced commits"), 0);
1886
1887
0
  refs_for_each_ref(get_main_ref_store(source->odb->repo), add_ref_to_set,
1888
0
        &data);
1889
1890
0
  stop_progress(&data.progress);
1891
1892
0
  result = write_commit_graph(source, NULL, &commits,
1893
0
            flags, opts);
1894
1895
0
  oidset_clear(&commits);
1896
0
  return result;
1897
0
}
1898
1899
static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1900
        const struct string_list *pack_indexes)
1901
0
{
1902
0
  uint32_t i;
1903
0
  struct strbuf progress_title = STRBUF_INIT;
1904
0
  struct strbuf packname = STRBUF_INIT;
1905
0
  int dirlen;
1906
0
  int ret = 0;
1907
1908
0
  strbuf_addf(&packname, "%s/pack/", ctx->odb_source->path);
1909
0
  dirlen = packname.len;
1910
0
  if (ctx->report_progress) {
1911
0
    strbuf_addf(&progress_title,
1912
0
          Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1913
0
             "Finding commits for commit graph in %"PRIuMAX" packs",
1914
0
             pack_indexes->nr),
1915
0
          (uintmax_t)pack_indexes->nr);
1916
0
    ctx->progress = start_delayed_progress(ctx->r,
1917
0
                   progress_title.buf, 0);
1918
0
    ctx->progress_done = 0;
1919
0
  }
1920
0
  for (i = 0; i < pack_indexes->nr; i++) {
1921
0
    struct packed_git *p;
1922
0
    strbuf_setlen(&packname, dirlen);
1923
0
    strbuf_addstr(&packname, pack_indexes->items[i].string);
1924
0
    p = add_packed_git(ctx->r, packname.buf, packname.len, 1);
1925
0
    if (!p) {
1926
0
      ret = error(_("error adding pack %s"), packname.buf);
1927
0
      goto cleanup;
1928
0
    }
1929
0
    if (open_pack_index(p)) {
1930
0
      ret = error(_("error opening index for %s"), packname.buf);
1931
0
      close_pack(p);
1932
0
      free(p);
1933
0
      goto cleanup;
1934
0
    }
1935
0
    for_each_object_in_pack(p, add_packed_commits, ctx,
1936
0
          FOR_EACH_OBJECT_PACK_ORDER);
1937
0
    close_pack(p);
1938
0
    free(p);
1939
0
  }
1940
1941
0
cleanup:
1942
0
  stop_progress(&ctx->progress);
1943
0
  strbuf_release(&progress_title);
1944
0
  strbuf_release(&packname);
1945
1946
0
  return ret;
1947
0
}
1948
1949
static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1950
          struct oidset *commits)
1951
0
{
1952
0
  struct oidset_iter iter;
1953
0
  struct object_id *oid;
1954
1955
0
  if (!oidset_size(commits))
1956
0
    return 0;
1957
1958
0
  oidset_iter_init(commits, &iter);
1959
0
  while ((oid = oidset_iter_next(&iter))) {
1960
0
    oid_array_append(&ctx->oids, oid);
1961
0
  }
1962
1963
0
  return 0;
1964
0
}
1965
1966
static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1967
0
{
1968
0
  if (ctx->report_progress)
1969
0
    ctx->progress = start_delayed_progress(
1970
0
      ctx->r,
1971
0
      _("Finding commits for commit graph among packed objects"),
1972
0
      ctx->approx_nr_objects);
1973
0
  for_each_packed_object(ctx->r, add_packed_commits, ctx,
1974
0
             FOR_EACH_OBJECT_PACK_ORDER);
1975
0
  if (ctx->progress_done < ctx->approx_nr_objects)
1976
0
    display_progress(ctx->progress, ctx->approx_nr_objects);
1977
0
  stop_progress(&ctx->progress);
1978
0
}
1979
1980
static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1981
0
{
1982
0
  uint32_t i;
1983
0
  enum commit_graph_split_flags flags = ctx->opts ?
1984
0
    ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1985
1986
0
  ctx->num_extra_edges = 0;
1987
0
  if (ctx->report_progress)
1988
0
    ctx->progress = start_delayed_progress(
1989
0
      ctx->r,
1990
0
      _("Finding extra edges in commit graph"),
1991
0
      ctx->oids.nr);
1992
0
  oid_array_sort(&ctx->oids);
1993
0
  for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1994
0
    unsigned int num_parents;
1995
1996
0
    display_progress(ctx->progress, i + 1);
1997
1998
0
    ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1999
0
    ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
2000
2001
0
    if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
2002
0
        commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
2003
0
      continue;
2004
2005
0
    if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
2006
0
      repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
2007
0
    else
2008
0
      repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
2009
2010
0
    num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
2011
0
    if (num_parents > 2)
2012
0
      ctx->num_extra_edges += num_parents - 1;
2013
2014
0
    ctx->commits.nr++;
2015
0
  }
2016
0
  stop_progress(&ctx->progress);
2017
0
}
2018
2019
static int write_graph_chunk_base_1(struct hashfile *f,
2020
            struct commit_graph *g)
2021
0
{
2022
0
  int num = 0;
2023
2024
0
  if (!g)
2025
0
    return 0;
2026
2027
0
  num = write_graph_chunk_base_1(f, g->base_graph);
2028
0
  hashwrite(f, g->oid.hash, g->hash_algo->rawsz);
2029
0
  return num + 1;
2030
0
}
2031
2032
static int write_graph_chunk_base(struct hashfile *f,
2033
            void *data)
2034
0
{
2035
0
  struct write_commit_graph_context *ctx = data;
2036
0
  int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
2037
2038
0
  if (num != ctx->num_commit_graphs_after - 1) {
2039
0
    error(_("failed to write correct number of base graph ids"));
2040
0
    return -1;
2041
0
  }
2042
2043
0
  return 0;
2044
0
}
2045
2046
static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2047
0
{
2048
0
  uint32_t i;
2049
0
  struct hashfile *f;
2050
0
  struct tempfile *graph_layer; /* when ctx->split is non-zero */
2051
0
  struct lock_file lk = LOCK_INIT;
2052
0
  const unsigned hashsz = ctx->r->hash_algo->rawsz;
2053
0
  struct strbuf progress_title = STRBUF_INIT;
2054
0
  struct chunkfile *cf;
2055
0
  unsigned char file_hash[GIT_MAX_RAWSZ];
2056
2057
0
  if (ctx->split) {
2058
0
    struct strbuf tmp_file = STRBUF_INIT;
2059
2060
0
    strbuf_addf(&tmp_file,
2061
0
          "%s/info/commit-graphs/tmp_graph_XXXXXX",
2062
0
          ctx->odb_source->path);
2063
0
    ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2064
0
  } else {
2065
0
    ctx->graph_name = get_commit_graph_filename(ctx->odb_source);
2066
0
  }
2067
2068
0
  if (safe_create_leading_directories(ctx->r, ctx->graph_name)) {
2069
0
    error(_("unable to create leading directories of %s"),
2070
0
      ctx->graph_name);
2071
0
    return -1;
2072
0
  }
2073
2074
0
  if (ctx->split) {
2075
0
    char *lock_name = get_commit_graph_chain_filename(ctx->odb_source);
2076
2077
0
    hold_lock_file_for_update_mode(&lk, lock_name,
2078
0
                 LOCK_DIE_ON_ERROR, 0444);
2079
0
    free(lock_name);
2080
2081
0
    graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
2082
0
    if (!graph_layer) {
2083
0
      error(_("unable to create temporary graph layer"));
2084
0
      return -1;
2085
0
    }
2086
2087
0
    if (adjust_shared_perm(ctx->r, get_tempfile_path(graph_layer))) {
2088
0
      error(_("unable to adjust shared permissions for '%s'"),
2089
0
            get_tempfile_path(graph_layer));
2090
0
      return -1;
2091
0
    }
2092
2093
0
    f = hashfd(ctx->r->hash_algo,
2094
0
         get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
2095
0
  } else {
2096
0
    hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2097
0
                 LOCK_DIE_ON_ERROR, 0444);
2098
0
    f = hashfd(ctx->r->hash_algo,
2099
0
         get_lock_file_fd(&lk), get_lock_file_path(&lk));
2100
0
  }
2101
2102
0
  cf = init_chunkfile(f);
2103
2104
0
  add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2105
0
      write_graph_chunk_fanout);
2106
0
  add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2107
0
      write_graph_chunk_oids);
2108
0
  add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2109
0
      write_graph_chunk_data);
2110
2111
0
  if (ctx->write_generation_data)
2112
0
    add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2113
0
        st_mult(sizeof(uint32_t), ctx->commits.nr),
2114
0
        write_graph_chunk_generation_data);
2115
0
  if (ctx->num_generation_data_overflows)
2116
0
    add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2117
0
        st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2118
0
        write_graph_chunk_generation_data_overflow);
2119
0
  if (ctx->num_extra_edges)
2120
0
    add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2121
0
        st_mult(4, ctx->num_extra_edges),
2122
0
        write_graph_chunk_extra_edges);
2123
0
  if (ctx->changed_paths) {
2124
0
    add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2125
0
        st_mult(sizeof(uint32_t), ctx->commits.nr),
2126
0
        write_graph_chunk_bloom_indexes);
2127
0
    add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2128
0
        st_add(sizeof(uint32_t) * 3,
2129
0
         ctx->total_bloom_filter_data_size),
2130
0
        write_graph_chunk_bloom_data);
2131
0
  }
2132
0
  if (ctx->num_commit_graphs_after > 1)
2133
0
    add_chunk(cf, GRAPH_CHUNKID_BASE,
2134
0
        st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2135
0
        write_graph_chunk_base);
2136
2137
0
  hashwrite_be32(f, GRAPH_SIGNATURE);
2138
2139
0
  hashwrite_u8(f, GRAPH_VERSION);
2140
0
  hashwrite_u8(f, oid_version(ctx->r->hash_algo));
2141
0
  hashwrite_u8(f, get_num_chunks(cf));
2142
0
  hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2143
2144
0
  if (ctx->report_progress) {
2145
0
    strbuf_addf(&progress_title,
2146
0
          Q_("Writing out commit graph in %d pass",
2147
0
             "Writing out commit graph in %d passes",
2148
0
             get_num_chunks(cf)),
2149
0
          get_num_chunks(cf));
2150
0
    ctx->progress = start_delayed_progress(
2151
0
      ctx->r,
2152
0
      progress_title.buf,
2153
0
      st_mult(get_num_chunks(cf), ctx->commits.nr));
2154
0
  }
2155
2156
0
  write_chunkfile(cf, ctx);
2157
2158
0
  stop_progress(&ctx->progress);
2159
0
  strbuf_release(&progress_title);
2160
2161
0
  if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2162
0
    char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2163
0
    char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb_source, new_base_hash);
2164
2165
0
    free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2166
0
    free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2167
0
    ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2168
0
    ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2169
0
  }
2170
2171
0
  close_commit_graph(ctx->r->objects);
2172
0
  finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2173
0
        CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2174
0
  free_chunkfile(cf);
2175
2176
0
  if (ctx->split) {
2177
0
    FILE *chainf = fdopen_lock_file(&lk, "w");
2178
0
    char *final_graph_name;
2179
0
    int result;
2180
2181
0
    if (!chainf) {
2182
0
      error(_("unable to open commit-graph chain file"));
2183
0
      return -1;
2184
0
    }
2185
2186
0
    if (ctx->base_graph_name) {
2187
0
      const char *dest;
2188
0
      int idx = ctx->num_commit_graphs_after - 1;
2189
0
      if (ctx->num_commit_graphs_after > 1)
2190
0
        idx--;
2191
2192
0
      dest = ctx->commit_graph_filenames_after[idx];
2193
2194
0
      if (strcmp(ctx->base_graph_name, dest)) {
2195
0
        result = rename(ctx->base_graph_name, dest);
2196
2197
0
        if (result) {
2198
0
          error(_("failed to rename base commit-graph file"));
2199
0
          return -1;
2200
0
        }
2201
0
      }
2202
0
    } else {
2203
0
      char *graph_name = get_commit_graph_filename(ctx->odb_source);
2204
0
      unlink(graph_name);
2205
0
      free(graph_name);
2206
0
    }
2207
2208
0
    free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2209
0
    ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] =
2210
0
      xstrdup(hash_to_hex_algop(file_hash, ctx->r->hash_algo));
2211
0
    final_graph_name = get_split_graph_filename(ctx->odb_source,
2212
0
          ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2213
0
    free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2214
0
    ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2215
2216
0
    result = rename_tempfile(&graph_layer, final_graph_name);
2217
2218
0
    for (i = 0; i < ctx->num_commit_graphs_after; i++)
2219
0
      fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2220
2221
0
    if (result) {
2222
0
      error(_("failed to rename temporary commit-graph file"));
2223
0
      return -1;
2224
0
    }
2225
0
  }
2226
2227
0
  commit_lock_file(&lk);
2228
2229
0
  return 0;
2230
0
}
2231
2232
static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
2233
               struct commit_graph *graph_to_merge)
2234
0
{
2235
0
  struct commit_graph *g;
2236
0
  uint32_t num_commits;
2237
0
  enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2238
0
  uint32_t i;
2239
2240
0
  int max_commits = 0;
2241
0
  int size_mult = 2;
2242
2243
0
  if (ctx->opts) {
2244
0
    max_commits = ctx->opts->max_commits;
2245
2246
0
    if (ctx->opts->size_multiple)
2247
0
      size_mult = ctx->opts->size_multiple;
2248
2249
0
    flags = ctx->opts->split_flags;
2250
0
  }
2251
2252
0
  g = graph_to_merge;
2253
0
  num_commits = ctx->commits.nr;
2254
0
  if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2255
0
    ctx->num_commit_graphs_after = 1;
2256
0
  else
2257
0
    ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2258
2259
0
  if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2260
0
      flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2261
0
    while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2262
0
          (max_commits && num_commits > max_commits))) {
2263
0
      if (g->odb_source != ctx->odb_source)
2264
0
        break;
2265
2266
0
      if (unsigned_add_overflows(num_commits, g->num_commits))
2267
0
        die(_("cannot merge graphs with %"PRIuMAX", "
2268
0
              "%"PRIuMAX" commits"),
2269
0
            (uintmax_t)num_commits,
2270
0
            (uintmax_t)g->num_commits);
2271
0
      num_commits += g->num_commits;
2272
0
      g = g->base_graph;
2273
2274
0
      ctx->num_commit_graphs_after--;
2275
0
    }
2276
0
  }
2277
2278
0
  if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2279
0
    ctx->new_base_graph = g;
2280
0
  else if (ctx->num_commit_graphs_after != 1)
2281
0
    BUG("split_graph_merge_strategy: num_commit_graphs_after "
2282
0
        "should be 1 with --split=replace");
2283
2284
0
  if (ctx->num_commit_graphs_after == 2) {
2285
0
    char *old_graph_name = get_commit_graph_filename(g->odb_source);
2286
2287
0
    if (!strcmp(g->filename, old_graph_name) &&
2288
0
        g->odb_source != ctx->odb_source) {
2289
0
      ctx->num_commit_graphs_after = 1;
2290
0
      ctx->new_base_graph = NULL;
2291
0
    }
2292
2293
0
    free(old_graph_name);
2294
0
  }
2295
2296
0
  CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2297
0
  CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2298
2299
0
  for (i = 0; i < ctx->num_commit_graphs_after &&
2300
0
        i < ctx->num_commit_graphs_before; i++)
2301
0
    ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2302
2303
0
  i = ctx->num_commit_graphs_before - 1;
2304
0
  g = graph_to_merge;
2305
2306
0
  while (g) {
2307
0
    if (i < ctx->num_commit_graphs_after)
2308
0
      ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2309
2310
    /*
2311
     * If the topmost remaining layer has generation data chunk, the
2312
     * resultant layer also has generation data chunk.
2313
     */
2314
0
    if (i == ctx->num_commit_graphs_after - 2)
2315
0
      ctx->write_generation_data = !!g->chunk_generation_data;
2316
2317
0
    i--;
2318
0
    g = g->base_graph;
2319
0
  }
2320
0
}
2321
2322
static void merge_commit_graph(struct write_commit_graph_context *ctx,
2323
             struct commit_graph *g)
2324
0
{
2325
0
  uint32_t i;
2326
0
  uint32_t offset = g->num_commits_in_base;
2327
2328
0
  if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2329
0
    die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2330
0
        oid_to_hex(&g->oid),
2331
0
        (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2332
2333
0
  ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2334
2335
0
  for (i = 0; i < g->num_commits; i++) {
2336
0
    struct object_id oid;
2337
0
    struct commit *result;
2338
2339
0
    display_progress(ctx->progress, i + 1);
2340
2341
0
    load_oid_from_graph(g, i + offset, &oid);
2342
2343
    /* only add commits if they still exist in the repo */
2344
0
    result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2345
2346
0
    if (result) {
2347
0
      ctx->commits.list[ctx->commits.nr] = result;
2348
0
      ctx->commits.nr++;
2349
0
    }
2350
0
  }
2351
0
}
2352
2353
static int commit_compare(const void *_a, const void *_b)
2354
0
{
2355
0
  const struct commit *a = *(const struct commit **)_a;
2356
0
  const struct commit *b = *(const struct commit **)_b;
2357
0
  return oidcmp(&a->object.oid, &b->object.oid);
2358
0
}
2359
2360
static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2361
0
{
2362
0
  uint32_t i, dedup_i = 0;
2363
2364
0
  if (ctx->report_progress)
2365
0
    ctx->progress = start_delayed_progress(
2366
0
          ctx->r,
2367
0
          _("Scanning merged commits"),
2368
0
          ctx->commits.nr);
2369
2370
0
  QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2371
2372
0
  ctx->num_extra_edges = 0;
2373
0
  for (i = 0; i < ctx->commits.nr; i++) {
2374
0
    display_progress(ctx->progress, i + 1);
2375
2376
0
    if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2377
0
        &ctx->commits.list[i]->object.oid)) {
2378
      /*
2379
       * Silently ignore duplicates. These were likely
2380
       * created due to a commit appearing in multiple
2381
       * layers of the chain, which is unexpected but
2382
       * not invalid. We should make sure there is a
2383
       * unique copy in the new layer.
2384
       */
2385
0
    } else {
2386
0
      unsigned int num_parents;
2387
2388
0
      ctx->commits.list[dedup_i] = ctx->commits.list[i];
2389
0
      dedup_i++;
2390
2391
0
      num_parents = commit_list_count(ctx->commits.list[i]->parents);
2392
0
      if (num_parents > 2)
2393
0
        ctx->num_extra_edges += num_parents - 1;
2394
0
    }
2395
0
  }
2396
2397
0
  ctx->commits.nr = dedup_i;
2398
2399
0
  stop_progress(&ctx->progress);
2400
0
}
2401
2402
static void merge_commit_graphs(struct write_commit_graph_context *ctx,
2403
        struct commit_graph *g)
2404
0
{
2405
0
  uint32_t current_graph_number = ctx->num_commit_graphs_before;
2406
2407
0
  while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2408
0
    current_graph_number--;
2409
2410
0
    if (ctx->report_progress)
2411
0
      ctx->progress = start_delayed_progress(ctx->r,
2412
0
                     _("Merging commit-graph"), 0);
2413
2414
0
    merge_commit_graph(ctx, g);
2415
0
    stop_progress(&ctx->progress);
2416
2417
0
    g = g->base_graph;
2418
0
  }
2419
2420
0
  if (g) {
2421
0
    ctx->new_base_graph = g;
2422
0
    ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2423
0
  }
2424
2425
0
  if (ctx->new_base_graph)
2426
0
    ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2427
2428
0
  sort_and_scan_merged_commits(ctx);
2429
0
}
2430
2431
static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2432
0
{
2433
0
  uint32_t i;
2434
0
  time_t now = time(NULL);
2435
2436
0
  for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2437
0
    struct stat st;
2438
0
    struct utimbuf updated_time;
2439
2440
0
    if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2441
0
      continue;
2442
2443
0
    updated_time.actime = st.st_atime;
2444
0
    updated_time.modtime = now;
2445
0
    utime(ctx->commit_graph_filenames_before[i], &updated_time);
2446
0
  }
2447
0
}
2448
2449
static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2450
0
{
2451
0
  struct strbuf path = STRBUF_INIT;
2452
0
  DIR *dir;
2453
0
  struct dirent *de;
2454
0
  size_t dirnamelen;
2455
0
  timestamp_t expire_time = time(NULL);
2456
2457
0
  if (ctx->opts && ctx->opts->expire_time)
2458
0
    expire_time = ctx->opts->expire_time;
2459
0
  if (!ctx->split) {
2460
0
    char *chain_file_name = get_commit_graph_chain_filename(ctx->odb_source);
2461
0
    unlink(chain_file_name);
2462
0
    free(chain_file_name);
2463
0
    ctx->num_commit_graphs_after = 0;
2464
0
  }
2465
2466
0
  strbuf_addstr(&path, ctx->odb_source->path);
2467
0
  strbuf_addstr(&path, "/info/commit-graphs");
2468
0
  dir = opendir(path.buf);
2469
2470
0
  if (!dir)
2471
0
    goto out;
2472
2473
0
  strbuf_addch(&path, '/');
2474
0
  dirnamelen = path.len;
2475
0
  while ((de = readdir(dir)) != NULL) {
2476
0
    struct stat st;
2477
0
    uint32_t i, found = 0;
2478
2479
0
    strbuf_setlen(&path, dirnamelen);
2480
0
    strbuf_addstr(&path, de->d_name);
2481
2482
0
    if (stat(path.buf, &st) < 0)
2483
0
      continue;
2484
2485
0
    if (st.st_mtime > expire_time)
2486
0
      continue;
2487
0
    if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2488
0
      continue;
2489
2490
0
    for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2491
0
      if (!strcmp(ctx->commit_graph_filenames_after[i],
2492
0
            path.buf)) {
2493
0
        found = 1;
2494
0
        break;
2495
0
      }
2496
0
    }
2497
2498
0
    if (!found)
2499
0
      unlink(path.buf);
2500
0
  }
2501
2502
0
out:
2503
0
  if(dir)
2504
0
    closedir(dir);
2505
0
  strbuf_release(&path);
2506
0
}
2507
2508
int write_commit_graph(struct odb_source *source,
2509
           const struct string_list *const pack_indexes,
2510
           struct oidset *commits,
2511
           enum commit_graph_write_flags flags,
2512
           const struct commit_graph_opts *opts)
2513
0
{
2514
0
  struct repository *r = source->odb->repo;
2515
0
  struct write_commit_graph_context ctx = {
2516
0
    .r = r,
2517
0
    .odb_source = source,
2518
0
    .append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0,
2519
0
    .report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0,
2520
0
    .split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0,
2521
0
    .opts = opts,
2522
0
    .total_bloom_filter_data_size = 0,
2523
0
    .write_generation_data = (get_configured_generation_version(r) == 2),
2524
0
    .num_generation_data_overflows = 0,
2525
0
  };
2526
0
  uint32_t i;
2527
0
  int res = 0;
2528
0
  int replace = 0;
2529
0
  struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2530
0
  struct topo_level_slab topo_levels;
2531
0
  struct commit_graph *g;
2532
2533
0
  prepare_repo_settings(r);
2534
0
  if (!r->settings.core_commit_graph) {
2535
0
    warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2536
0
    return 0;
2537
0
  }
2538
0
  if (!commit_graph_compatible(r))
2539
0
    return 0;
2540
0
  if (r->settings.commit_graph_changed_paths_version < -1
2541
0
      || r->settings.commit_graph_changed_paths_version > 2) {
2542
0
    warning(_("attempting to write a commit-graph, but "
2543
0
        "'commitGraph.changedPathsVersion' (%d) is not supported"),
2544
0
      r->settings.commit_graph_changed_paths_version);
2545
0
    return 0;
2546
0
  }
2547
2548
0
  bloom_settings.hash_version = r->settings.commit_graph_changed_paths_version;
2549
0
  bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2550
0
                  bloom_settings.bits_per_entry);
2551
0
  bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2552
0
              bloom_settings.num_hashes);
2553
0
  bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2554
0
               bloom_settings.max_changed_paths);
2555
0
  ctx.bloom_settings = &bloom_settings;
2556
2557
0
  init_topo_level_slab(&topo_levels);
2558
0
  ctx.topo_levels = &topo_levels;
2559
2560
0
  g = prepare_commit_graph(ctx.r);
2561
0
  for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2562
0
    g->topo_levels = &topo_levels;
2563
2564
0
  if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2565
0
    ctx.changed_paths = 1;
2566
0
  if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2567
    /* We have changed-paths already. Keep them in the next graph */
2568
0
    if (g && g->bloom_filter_settings) {
2569
0
      ctx.changed_paths = 1;
2570
2571
      /* don't propagate the hash_version unless unspecified */
2572
0
      if (bloom_settings.hash_version == -1)
2573
0
        bloom_settings.hash_version = g->bloom_filter_settings->hash_version;
2574
0
      bloom_settings.bits_per_entry = g->bloom_filter_settings->bits_per_entry;
2575
0
      bloom_settings.num_hashes = g->bloom_filter_settings->num_hashes;
2576
0
      bloom_settings.max_changed_paths = g->bloom_filter_settings->max_changed_paths;
2577
0
    }
2578
0
  }
2579
2580
0
  bloom_settings.hash_version = bloom_settings.hash_version == 2 ? 2 : 1;
2581
2582
0
  if (ctx.split) {
2583
0
    for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2584
0
      ctx.num_commit_graphs_before++;
2585
2586
0
    if (ctx.num_commit_graphs_before) {
2587
0
      ALLOC_ARRAY(ctx.commit_graph_filenames_before, ctx.num_commit_graphs_before);
2588
0
      i = ctx.num_commit_graphs_before;
2589
2590
0
      for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2591
0
        ctx.commit_graph_filenames_before[--i] = xstrdup(chain->filename);
2592
0
    }
2593
2594
0
    if (ctx.opts)
2595
0
      replace = ctx.opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2596
0
  }
2597
2598
0
  ctx.approx_nr_objects = repo_approximate_object_count(r);
2599
2600
0
  if (ctx.append && g) {
2601
0
    for (i = 0; i < g->num_commits; i++) {
2602
0
      struct object_id oid;
2603
0
      oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2604
0
        r->hash_algo);
2605
0
      oid_array_append(&ctx.oids, &oid);
2606
0
    }
2607
0
  }
2608
2609
0
  if (pack_indexes) {
2610
0
    ctx.order_by_pack = 1;
2611
0
    if ((res = fill_oids_from_packs(&ctx, pack_indexes)))
2612
0
      goto cleanup;
2613
0
  }
2614
2615
0
  if (commits) {
2616
0
    if ((res = fill_oids_from_commits(&ctx, commits)))
2617
0
      goto cleanup;
2618
0
  }
2619
2620
0
  if (!pack_indexes && !commits) {
2621
0
    ctx.order_by_pack = 1;
2622
0
    fill_oids_from_all_packs(&ctx);
2623
0
  }
2624
2625
0
  close_reachable(&ctx);
2626
2627
0
  copy_oids_to_commits(&ctx);
2628
2629
0
  if (ctx.commits.nr >= GRAPH_EDGE_LAST_MASK) {
2630
0
    error(_("too many commits to write graph"));
2631
0
    res = -1;
2632
0
    goto cleanup;
2633
0
  }
2634
2635
0
  if (!ctx.commits.nr && !replace)
2636
0
    goto cleanup;
2637
2638
0
  if (ctx.split) {
2639
0
    split_graph_merge_strategy(&ctx, g);
2640
2641
0
    if (!replace)
2642
0
      merge_commit_graphs(&ctx, g);
2643
0
  } else {
2644
0
    ctx.num_commit_graphs_after = 1;
2645
0
  }
2646
2647
0
  ctx.trust_generation_numbers = validate_mixed_generation_chain(g);
2648
2649
0
  compute_topological_levels(&ctx);
2650
0
  if (ctx.write_generation_data)
2651
0
    compute_generation_numbers(&ctx);
2652
2653
0
  if (ctx.changed_paths)
2654
0
    compute_bloom_filters(&ctx);
2655
2656
0
  res = write_commit_graph_file(&ctx);
2657
2658
0
  if (ctx.changed_paths)
2659
0
    deinit_bloom_filters();
2660
2661
0
  if (ctx.split)
2662
0
    mark_commit_graphs(&ctx);
2663
2664
0
  expire_commit_graphs(&ctx);
2665
2666
0
cleanup:
2667
0
  free(ctx.graph_name);
2668
0
  free(ctx.base_graph_name);
2669
0
  free(ctx.commits.list);
2670
0
  oid_array_clear(&ctx.oids);
2671
0
  clear_topo_level_slab(&topo_levels);
2672
2673
0
  if (ctx.r->objects->commit_graph) {
2674
0
    struct commit_graph *g = ctx.r->objects->commit_graph;
2675
2676
0
    while (g) {
2677
0
      g->topo_levels = NULL;
2678
0
      g = g->base_graph;
2679
0
    }
2680
0
  }
2681
2682
0
  for (i = 0; i < ctx.num_commit_graphs_before; i++)
2683
0
    free(ctx.commit_graph_filenames_before[i]);
2684
0
  free(ctx.commit_graph_filenames_before);
2685
2686
0
  for (i = 0; i < ctx.num_commit_graphs_after; i++) {
2687
0
    free(ctx.commit_graph_filenames_after[i]);
2688
0
    free(ctx.commit_graph_hash_after[i]);
2689
0
  }
2690
0
  free(ctx.commit_graph_filenames_after);
2691
0
  free(ctx.commit_graph_hash_after);
2692
2693
0
  return res;
2694
0
}
2695
2696
0
#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2697
static int verify_commit_graph_error;
2698
2699
__attribute__((format (printf, 1, 2)))
2700
static void graph_report(const char *fmt, ...)
2701
0
{
2702
0
  va_list ap;
2703
2704
0
  verify_commit_graph_error = 1;
2705
0
  va_start(ap, fmt);
2706
0
  vfprintf(stderr, fmt, ap);
2707
0
  fprintf(stderr, "\n");
2708
0
  va_end(ap);
2709
0
}
2710
2711
static int commit_graph_checksum_valid(struct commit_graph *g)
2712
0
{
2713
0
  return hashfile_checksum_valid(g->hash_algo,
2714
0
               g->data, g->data_len);
2715
0
}
2716
2717
static int verify_one_commit_graph(struct commit_graph *g,
2718
           struct progress *progress,
2719
           uint64_t *seen)
2720
0
{
2721
0
  struct repository *r = g->odb_source->odb->repo;
2722
0
  uint32_t i, cur_fanout_pos = 0;
2723
0
  struct object_id prev_oid, cur_oid;
2724
0
  struct commit *seen_gen_zero = NULL;
2725
0
  struct commit *seen_gen_non_zero = NULL;
2726
2727
0
  if (!commit_graph_checksum_valid(g)) {
2728
0
    graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2729
0
    verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2730
0
  }
2731
2732
0
  for (i = 0; i < g->num_commits; i++) {
2733
0
    struct commit *graph_commit;
2734
2735
0
    oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2736
0
      g->hash_algo);
2737
2738
0
    if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2739
0
      graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2740
0
             oid_to_hex(&prev_oid),
2741
0
             oid_to_hex(&cur_oid));
2742
2743
0
    oidcpy(&prev_oid, &cur_oid);
2744
2745
0
    while (cur_oid.hash[0] > cur_fanout_pos) {
2746
0
      uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2747
2748
0
      if (i != fanout_value)
2749
0
        graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2750
0
               cur_fanout_pos, fanout_value, i);
2751
0
      cur_fanout_pos++;
2752
0
    }
2753
2754
0
    graph_commit = lookup_commit(r, &cur_oid);
2755
0
    if (!parse_commit_in_graph_one(g, graph_commit))
2756
0
      graph_report(_("failed to parse commit %s from commit-graph"),
2757
0
             oid_to_hex(&cur_oid));
2758
0
  }
2759
2760
0
  while (cur_fanout_pos < 256) {
2761
0
    uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2762
2763
0
    if (g->num_commits != fanout_value)
2764
0
      graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2765
0
             cur_fanout_pos, fanout_value, i);
2766
2767
0
    cur_fanout_pos++;
2768
0
  }
2769
2770
0
  if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2771
0
    return verify_commit_graph_error;
2772
2773
0
  for (i = 0; i < g->num_commits; i++) {
2774
0
    struct commit *graph_commit, *odb_commit;
2775
0
    struct commit_list *graph_parents, *odb_parents;
2776
0
    timestamp_t max_generation = 0;
2777
0
    timestamp_t generation;
2778
2779
0
    display_progress(progress, ++(*seen));
2780
0
    oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2781
0
      g->hash_algo);
2782
2783
0
    graph_commit = lookup_commit(r, &cur_oid);
2784
0
    odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2785
0
    if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2786
0
      graph_report(_("failed to parse commit %s from object database for commit-graph"),
2787
0
             oid_to_hex(&cur_oid));
2788
0
      continue;
2789
0
    }
2790
2791
0
    if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
2792
0
         get_commit_tree_oid(odb_commit)))
2793
0
      graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2794
0
             oid_to_hex(&cur_oid),
2795
0
             oid_to_hex(get_commit_tree_oid(graph_commit)),
2796
0
             oid_to_hex(get_commit_tree_oid(odb_commit)));
2797
2798
0
    graph_parents = graph_commit->parents;
2799
0
    odb_parents = odb_commit->parents;
2800
2801
0
    while (graph_parents) {
2802
0
      if (!odb_parents) {
2803
0
        graph_report(_("commit-graph parent list for commit %s is too long"),
2804
0
               oid_to_hex(&cur_oid));
2805
0
        break;
2806
0
      }
2807
2808
      /* parse parent in case it is in a base graph */
2809
0
      parse_commit_in_graph_one(g, graph_parents->item);
2810
2811
0
      if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2812
0
        graph_report(_("commit-graph parent for %s is %s != %s"),
2813
0
               oid_to_hex(&cur_oid),
2814
0
               oid_to_hex(&graph_parents->item->object.oid),
2815
0
               oid_to_hex(&odb_parents->item->object.oid));
2816
2817
0
      generation = commit_graph_generation_from_graph(graph_parents->item);
2818
0
      if (generation > max_generation)
2819
0
        max_generation = generation;
2820
2821
0
      graph_parents = graph_parents->next;
2822
0
      odb_parents = odb_parents->next;
2823
0
    }
2824
2825
0
    if (odb_parents)
2826
0
      graph_report(_("commit-graph parent list for commit %s terminates early"),
2827
0
             oid_to_hex(&cur_oid));
2828
2829
0
    if (commit_graph_generation_from_graph(graph_commit))
2830
0
      seen_gen_non_zero = graph_commit;
2831
0
    else
2832
0
      seen_gen_zero = graph_commit;
2833
2834
0
    if (seen_gen_zero)
2835
0
      continue;
2836
2837
    /*
2838
     * If we are using topological level and one of our parents has
2839
     * generation GENERATION_NUMBER_V1_MAX, then our generation is
2840
     * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2841
     * in the following condition.
2842
     */
2843
0
    if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2844
0
      max_generation--;
2845
2846
0
    generation = commit_graph_generation(graph_commit);
2847
0
    if (generation < max_generation + 1)
2848
0
      graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2849
0
             oid_to_hex(&cur_oid),
2850
0
             generation,
2851
0
             max_generation + 1);
2852
2853
0
    if (graph_commit->date != odb_commit->date)
2854
0
      graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2855
0
             oid_to_hex(&cur_oid),
2856
0
             graph_commit->date,
2857
0
             odb_commit->date);
2858
0
  }
2859
2860
0
  if (seen_gen_zero && seen_gen_non_zero)
2861
0
    graph_report(_("commit-graph has both zero and non-zero "
2862
0
             "generations (e.g., commits '%s' and '%s')"),
2863
0
           oid_to_hex(&seen_gen_zero->object.oid),
2864
0
           oid_to_hex(&seen_gen_non_zero->object.oid));
2865
2866
0
  return verify_commit_graph_error;
2867
0
}
2868
2869
int verify_commit_graph(struct commit_graph *g, int flags)
2870
0
{
2871
0
  struct progress *progress = NULL;
2872
0
  int local_error = 0;
2873
0
  uint64_t seen = 0;
2874
2875
0
  if (!g) {
2876
0
    graph_report("no commit-graph file loaded");
2877
0
    return 1;
2878
0
  }
2879
2880
0
  if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2881
0
    uint64_t total = g->num_commits;
2882
0
    if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2883
0
      total += g->num_commits_in_base;
2884
2885
0
    progress = start_progress(g->odb_source->odb->repo,
2886
0
            _("Verifying commits in commit graph"),
2887
0
            total);
2888
0
  }
2889
2890
0
  for (; g; g = g->base_graph) {
2891
0
    local_error |= verify_one_commit_graph(g, progress, &seen);
2892
0
    if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2893
0
      break;
2894
0
  }
2895
2896
0
  stop_progress(&progress);
2897
2898
0
  return local_error;
2899
0
}
2900
2901
void free_commit_graph(struct commit_graph *g)
2902
59
{
2903
59
  while (g) {
2904
0
    struct commit_graph *next = g->base_graph;
2905
2906
0
    if (g->data)
2907
0
      munmap((void *)g->data, g->data_len);
2908
0
    free(g->filename);
2909
0
    free(g->bloom_filter_settings);
2910
0
    free(g);
2911
2912
0
    g = next;
2913
0
  }
2914
59
}
2915
2916
void disable_commit_graph(struct repository *r)
2917
0
{
2918
0
  r->commit_graph_disabled = 1;
2919
0
}