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