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