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