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