]> git.ipfire.org Git - thirdparty/git.git/blame - commit-graph.c
Merge branch 'rs/mem-pool-improvements'
[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{
ac6d45d1 809 clear_commit_graph_data_slab(&commit_graph_data_slab);
09a75abb 810 free_commit_graph(o->commit_graph);
c3a3a964 811 o->commit_graph = NULL;
177722b3
DS
812}
813
809ea28f 814static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
177722b3
DS
815{
816 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
817 g->chunk_oid_lookup, g->hash_len, pos);
818}
819
d4f4d60f
DS
820static void load_oid_from_graph(struct commit_graph *g,
821 uint32_t pos,
822 struct object_id *oid)
823{
824 uint32_t lex_index;
825
826 while (g && pos < g->num_commits_in_base)
827 g = g->base_graph;
828
829 if (!g)
830 BUG("NULL commit-graph");
831
832 if (pos >= g->num_commits + g->num_commits_in_base)
833 die(_("invalid commit position. commit-graph is likely corrupt"));
834
835 lex_index = pos - g->num_commits_in_base;
836
0bd8f30a 837 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
d4f4d60f
DS
838}
839
4f542b7a
SB
840static struct commit_list **insert_parent_or_die(struct repository *r,
841 struct commit_graph *g,
d4f4d60f 842 uint32_t pos,
177722b3
DS
843 struct commit_list **pptr)
844{
845 struct commit *c;
846 struct object_id oid;
96af91d4 847
d4f4d60f
DS
848 if (pos >= g->num_commits + g->num_commits_in_base)
849 die("invalid parent position %"PRIu32, pos);
53614b13 850
d4f4d60f 851 load_oid_from_graph(g, pos, &oid);
4f542b7a 852 c = lookup_commit(r, &oid);
177722b3 853 if (!c)
4f5b532d 854 die(_("could not find commit %s"), oid_to_hex(&oid));
c49c82aa 855 commit_graph_data_at(c)->graph_pos = pos;
177722b3
DS
856 return &commit_list_insert(c, pptr)->next;
857}
858
e2838d85
DS
859static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
860{
d4f4d60f 861 const unsigned char *commit_data;
c752ad09 862 struct commit_graph_data *graph_data;
e8b63005
AK
863 uint32_t lex_index, offset_pos;
864 uint64_t date_high, date_low, offset;
d4f4d60f
DS
865
866 while (pos < g->num_commits_in_base)
867 g = g->base_graph;
868
f90fca63
AK
869 if (pos >= g->num_commits + g->num_commits_in_base)
870 die(_("invalid commit position. commit-graph is likely corrupt"));
871
d4f4d60f 872 lex_index = pos - g->num_commits_in_base;
2740ed1c 873 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
c752ad09
AK
874
875 graph_data = commit_graph_data_at(item);
876 graph_data->graph_pos = pos;
f90fca63
AK
877
878 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
879 date_low = get_be32(commit_data + g->hash_len + 12);
880 item->date = (timestamp_t)((date_high << 32) | date_low);
881
1fdc383c 882 if (g->read_generation_data) {
2740ed1c 883 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
e8b63005
AK
884
885 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
886 if (!g->chunk_generation_data_overflow)
887 die(_("commit-graph requires overflow generation data but has none"));
888
889 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
ee6a7924
JK
890 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
891 die(_("commit-graph overflow generation data is too small"));
892 graph_data->generation = item->date +
893 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
e8b63005
AK
894 } else
895 graph_data->generation = item->date + offset;
896 } else
897 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
72a2bfca
AK
898
899 if (g->topo_levels)
900 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
e2838d85
DS
901}
902
a133c40b
NTND
903static inline void set_commit_tree(struct commit *c, struct tree *t)
904{
905 c->maybe_tree = t;
906}
907
4f542b7a
SB
908static int fill_commit_in_graph(struct repository *r,
909 struct commit *item,
910 struct commit_graph *g, uint32_t pos)
177722b3 911{
177722b3 912 uint32_t edge_value;
9622610e 913 uint32_t parent_data_pos;
177722b3 914 struct commit_list **pptr;
d4f4d60f
DS
915 const unsigned char *commit_data;
916 uint32_t lex_index;
177722b3 917
d4f4d60f
DS
918 while (pos < g->num_commits_in_base)
919 g = g->base_graph;
920
f90fca63 921 fill_commit_graph_info(item, g, pos);
d4f4d60f 922
d4f4d60f 923 lex_index = pos - g->num_commits_in_base;
50a71c29 924 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
d4f4d60f
DS
925
926 item->object.parsed = 1;
177722b3 927
a133c40b 928 set_commit_tree(item, NULL);
177722b3 929
177722b3
DS
930 pptr = &item->parents;
931
932 edge_value = get_be32(commit_data + g->hash_len);
933 if (edge_value == GRAPH_PARENT_NONE)
934 return 1;
4f542b7a 935 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
936
937 edge_value = get_be32(commit_data + g->hash_len + 4);
938 if (edge_value == GRAPH_PARENT_NONE)
939 return 1;
5af7417b 940 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
4f542b7a 941 pptr = insert_parent_or_die(r, g, edge_value, pptr);
177722b3
DS
942 return 1;
943 }
944
9622610e 945 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
177722b3 946 do {
9622610e 947 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
e0203916 948 error(_("commit-graph extra-edges pointer out of bounds"));
9622610e
JK
949 free_commit_list(item->parents);
950 item->parents = NULL;
951 item->object.parsed = 0;
952 return 0;
953 }
954 edge_value = get_be32(g->chunk_extra_edges +
955 sizeof(uint32_t) * parent_data_pos);
4f542b7a 956 pptr = insert_parent_or_die(r, g,
177722b3
DS
957 edge_value & GRAPH_EDGE_LAST_MASK,
958 pptr);
9622610e 959 parent_data_pos++;
177722b3
DS
960 } while (!(edge_value & GRAPH_LAST_EDGE));
961
962 return 1;
963}
964
809ea28f
PS
965static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
966{
967 struct commit_graph *cur_g = g;
968 uint32_t lex_index;
969
970 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
971 cur_g = cur_g->base_graph;
972
973 if (cur_g) {
974 *pos = lex_index + cur_g->num_commits_in_base;
975 return 1;
976 }
977
978 return 0;
979}
980
981static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
e2838d85 982{
c752ad09
AK
983 uint32_t graph_pos = commit_graph_position(item);
984 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
985 *pos = graph_pos;
e2838d85
DS
986 return 1;
987 } else {
809ea28f 988 return search_commit_pos_in_graph(&item->object.oid, g, pos);
e2838d85
DS
989 }
990}
991
7805360b
TB
992int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
993 uint32_t *pos)
994{
995 if (!prepare_commit_graph(r))
996 return 0;
997 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
998}
999
f559d6d4
PS
1000struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1001{
e04838ea 1002 static int commit_graph_paranoia = -1;
f559d6d4
PS
1003 struct commit *commit;
1004 uint32_t pos;
1005
e04838ea 1006 if (commit_graph_paranoia == -1)
b1df3b38 1007 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
e04838ea 1008
d6045294 1009 if (!prepare_commit_graph(repo))
f559d6d4
PS
1010 return NULL;
1011 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1012 return NULL;
e04838ea 1013 if (commit_graph_paranoia && !has_object(repo, id, 0))
f559d6d4
PS
1014 return NULL;
1015
1016 commit = lookup_commit(repo, id);
1017 if (!commit)
1018 return NULL;
1019 if (commit->object.parsed)
1020 return commit;
1021
1022 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1023 return NULL;
1024
1025 return commit;
1026}
1027
4f542b7a
SB
1028static int parse_commit_in_graph_one(struct repository *r,
1029 struct commit_graph *g,
1030 struct commit *item)
177722b3 1031{
e2838d85
DS
1032 uint32_t pos;
1033
177722b3
DS
1034 if (item->object.parsed)
1035 return 1;
ee797053 1036
809ea28f 1037 if (find_commit_pos_in_graph(item, g, &pos))
4f542b7a 1038 return fill_commit_in_graph(r, item, g, pos);
ee797053
DS
1039
1040 return 0;
1041}
1042
dade47c0 1043int parse_commit_in_graph(struct repository *r, struct commit *item)
ee797053 1044{
7b671f8c
DS
1045 static int checked_env = 0;
1046
1047 if (!checked_env &&
1048 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1049 die("dying as requested by the '%s' variable on commit-graph parse!",
1050 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1051 checked_env = 1;
1052
dade47c0 1053 if (!prepare_commit_graph(r))
ee797053 1054 return 0;
4f542b7a 1055 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
177722b3
DS
1056}
1057
dade47c0 1058void load_commit_graph_info(struct repository *r, struct commit *item)
e2838d85
DS
1059{
1060 uint32_t pos;
7805360b 1061 if (repo_find_commit_pos_in_graph(r, item, &pos))
dade47c0 1062 fill_commit_graph_info(item, r->objects->commit_graph, pos);
e2838d85
DS
1063}
1064
4f542b7a
SB
1065static struct tree *load_tree_for_commit(struct repository *r,
1066 struct commit_graph *g,
1067 struct commit *c)
7b8a21db
DS
1068{
1069 struct object_id oid;
d4f4d60f 1070 const unsigned char *commit_data;
c752ad09 1071 uint32_t graph_pos = commit_graph_position(c);
d4f4d60f 1072
c752ad09 1073 while (graph_pos < g->num_commits_in_base)
d4f4d60f
DS
1074 g = g->base_graph;
1075
1076 commit_data = g->chunk_commit_data +
51c31a64 1077 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
7b8a21db 1078
92e2cab9 1079 oidread(&oid, commit_data);
a133c40b 1080 set_commit_tree(c, lookup_tree(r, &oid));
7b8a21db
DS
1081
1082 return c->maybe_tree;
1083}
1084
4f542b7a
SB
1085static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1086 struct commit_graph *g,
0cbef8f8 1087 const struct commit *c)
7b8a21db
DS
1088{
1089 if (c->maybe_tree)
1090 return c->maybe_tree;
c49c82aa 1091 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
0cbef8f8
DS
1092 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1093
4f542b7a 1094 return load_tree_for_commit(r, g, (struct commit *)c);
0cbef8f8 1095}
7b8a21db 1096
dade47c0 1097struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
0cbef8f8 1098{
4f542b7a 1099 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
7b8a21db
DS
1100}
1101
c9905bea
DS
1102struct packed_commit_list {
1103 struct commit **list;
3361390c
JK
1104 size_t nr;
1105 size_t alloc;
c9905bea
DS
1106};
1107
c9905bea
DS
1108struct write_commit_graph_context {
1109 struct repository *r;
0bd52e27 1110 struct object_directory *odb;
c9905bea 1111 char *graph_name;
a5f1c448 1112 struct oid_array oids;
c9905bea
DS
1113 struct packed_commit_list commits;
1114 int num_extra_edges;
e8b63005 1115 int num_generation_data_overflows;
c9905bea
DS
1116 unsigned long approx_nr_objects;
1117 struct progress *progress;
1118 int progress_done;
1119 uint64_t progress_cnt;
6c622f9f
DS
1120
1121 char *base_graph_name;
1122 int num_commit_graphs_before;
1123 int num_commit_graphs_after;
1124 char **commit_graph_filenames_before;
1125 char **commit_graph_filenames_after;
1126 char **commit_graph_hash_after;
1127 uint32_t new_num_commits_in_base;
1128 struct commit_graph *new_base_graph;
1129
c9905bea 1130 unsigned append:1,
6c622f9f 1131 report_progress:1,
7c5c9b9c 1132 split:1,
3d112755 1133 changed_paths:1,
e8b63005 1134 order_by_pack:1,
fde55b09
DS
1135 write_generation_data:1,
1136 trust_generation_numbers:1;
c2bc6e6a 1137
72a2bfca 1138 struct topo_level_slab *topo_levels;
98bb7961 1139 const struct commit_graph_opts *opts;
f97b9325 1140 size_t total_bloom_filter_data_size;
98037f2b 1141 const struct bloom_filter_settings *bloom_settings;
312cff52
TB
1142
1143 int count_bloom_filter_computed;
1144 int count_bloom_filter_not_computed;
59f0d507 1145 int count_bloom_filter_trunc_empty;
312cff52 1146 int count_bloom_filter_trunc_large;
c9905bea
DS
1147};
1148
9bab081d 1149static int write_graph_chunk_fanout(struct hashfile *f,
eb907191 1150 void *data)
08fd81c9 1151{
eb907191 1152 struct write_commit_graph_context *ctx = data;
08fd81c9 1153 int i, count = 0;
c9905bea 1154 struct commit **list = ctx->commits.list;
08fd81c9
DS
1155
1156 /*
1157 * Write the first-level table (the list is sorted,
1158 * but we use a 256-entry lookup to be able to avoid
1159 * having to do eight extra binary search iterations).
1160 */
1161 for (i = 0; i < 256; i++) {
c9905bea 1162 while (count < ctx->commits.nr) {
08fd81c9
DS
1163 if ((*list)->object.oid.hash[0] != i)
1164 break;
c9905bea 1165 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9
DS
1166 count++;
1167 list++;
1168 }
1169
1170 hashwrite_be32(f, count);
1171 }
9bab081d
SG
1172
1173 return 0;
08fd81c9
DS
1174}
1175
9bab081d 1176static int write_graph_chunk_oids(struct hashfile *f,
eb907191 1177 void *data)
08fd81c9 1178{
eb907191 1179 struct write_commit_graph_context *ctx = data;
c9905bea 1180 struct commit **list = ctx->commits.list;
08fd81c9 1181 int count;
c9905bea
DS
1182 for (count = 0; count < ctx->commits.nr; count++, list++) {
1183 display_progress(ctx->progress, ++ctx->progress_cnt);
9bab081d 1184 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
53035c4f 1185 }
9bab081d
SG
1186
1187 return 0;
08fd81c9
DS
1188}
1189
8380dcd7 1190static const struct object_id *commit_to_oid(size_t index, const void *table)
08fd81c9 1191{
8380dcd7 1192 const struct commit * const *commits = table;
45ee13b9 1193 return &commits[index]->object.oid;
08fd81c9
DS
1194}
1195
9bab081d 1196static int write_graph_chunk_data(struct hashfile *f,
eb907191 1197 void *data)
08fd81c9 1198{
eb907191 1199 struct write_commit_graph_context *ctx = data;
c9905bea
DS
1200 struct commit **list = ctx->commits.list;
1201 struct commit **last = ctx->commits.list + ctx->commits.nr;
08fd81c9
DS
1202 uint32_t num_extra_edges = 0;
1203
1204 while (list < last) {
1205 struct commit_list *parent;
806278de 1206 struct object_id *tree;
08fd81c9
DS
1207 int edge_value;
1208 uint32_t packedDate[2];
c9905bea 1209 display_progress(ctx->progress, ++ctx->progress_cnt);
08fd81c9 1210
c4cc0831 1211 if (repo_parse_commit_no_graph(ctx->r, *list))
16749b8d
TB
1212 die(_("unable to parse commit %s"),
1213 oid_to_hex(&(*list)->object.oid));
806278de 1214 tree = get_commit_tree_oid(*list);
9bab081d 1215 hashwrite(f, tree->hash, the_hash_algo->rawsz);
08fd81c9
DS
1216
1217 parent = (*list)->parents;
1218
1219 if (!parent)
1220 edge_value = GRAPH_PARENT_NONE;
1221 else {
45ee13b9
JK
1222 edge_value = oid_pos(&parent->item->object.oid,
1223 ctx->commits.list,
1224 ctx->commits.nr,
1225 commit_to_oid);
08fd81c9 1226
6c622f9f
DS
1227 if (edge_value >= 0)
1228 edge_value += ctx->new_num_commits_in_base;
8a6ac287 1229 else if (ctx->new_base_graph) {
6c622f9f 1230 uint32_t pos;
809ea28f
PS
1231 if (find_commit_pos_in_graph(parent->item,
1232 ctx->new_base_graph,
1233 &pos))
6c622f9f
DS
1234 edge_value = pos;
1235 }
1236
08fd81c9 1237 if (edge_value < 0)
cce99cd8
DS
1238 BUG("missing parent %s for commit %s",
1239 oid_to_hex(&parent->item->object.oid),
1240 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
1241 }
1242
1243 hashwrite_be32(f, edge_value);
1244
1245 if (parent)
1246 parent = parent->next;
1247
1248 if (!parent)
1249 edge_value = GRAPH_PARENT_NONE;
1250 else if (parent->next)
5af7417b 1251 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
08fd81c9 1252 else {
45ee13b9
JK
1253 edge_value = oid_pos(&parent->item->object.oid,
1254 ctx->commits.list,
1255 ctx->commits.nr,
1256 commit_to_oid);
6c622f9f
DS
1257
1258 if (edge_value >= 0)
1259 edge_value += ctx->new_num_commits_in_base;
8a6ac287 1260 else if (ctx->new_base_graph) {
6c622f9f 1261 uint32_t pos;
809ea28f
PS
1262 if (find_commit_pos_in_graph(parent->item,
1263 ctx->new_base_graph,
1264 &pos))
6c622f9f
DS
1265 edge_value = pos;
1266 }
1267
08fd81c9 1268 if (edge_value < 0)
cce99cd8
DS
1269 BUG("missing parent %s for commit %s",
1270 oid_to_hex(&parent->item->object.oid),
1271 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
1272 }
1273
1274 hashwrite_be32(f, edge_value);
1275
5af7417b 1276 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
08fd81c9
DS
1277 do {
1278 num_extra_edges++;
1279 parent = parent->next;
1280 } while (parent);
1281 }
1282
1283 if (sizeof((*list)->date) > 4)
1284 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1285 else
1286 packedDate[0] = 0;
1287
72a2bfca 1288 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
3258c663 1289
08fd81c9
DS
1290 packedDate[1] = htonl((*list)->date);
1291 hashwrite(f, packedDate, 8);
1292
1293 list++;
1294 }
9bab081d
SG
1295
1296 return 0;
08fd81c9
DS
1297}
1298
e8b63005 1299static int write_graph_chunk_generation_data(struct hashfile *f,
eb907191 1300 void *data)
e8b63005 1301{
eb907191 1302 struct write_commit_graph_context *ctx = data;
e8b63005
AK
1303 int i, num_generation_data_overflows = 0;
1304
1305 for (i = 0; i < ctx->commits.nr; i++) {
1306 struct commit *c = ctx->commits.list[i];
90cb1c47
DS
1307 timestamp_t offset;
1308 repo_parse_commit(ctx->r, c);
1309 offset = commit_graph_data_at(c)->generation - c->date;
e8b63005
AK
1310 display_progress(ctx->progress, ++ctx->progress_cnt);
1311
1312 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1313 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1314 num_generation_data_overflows++;
1315 }
1316
1317 hashwrite_be32(f, offset);
1318 }
1319
1320 return 0;
1321}
1322
1323static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
eb907191 1324 void *data)
e8b63005 1325{
eb907191 1326 struct write_commit_graph_context *ctx = data;
e8b63005
AK
1327 int i;
1328 for (i = 0; i < ctx->commits.nr; i++) {
1329 struct commit *c = ctx->commits.list[i];
1330 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1331 display_progress(ctx->progress, ++ctx->progress_cnt);
1332
1333 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1334 hashwrite_be32(f, offset >> 32);
1335 hashwrite_be32(f, (uint32_t) offset);
1336 }
1337 }
1338
1339 return 0;
1340}
1341
9bab081d 1342static int write_graph_chunk_extra_edges(struct hashfile *f,
eb907191 1343 void *data)
08fd81c9 1344{
eb907191 1345 struct write_commit_graph_context *ctx = data;
c9905bea
DS
1346 struct commit **list = ctx->commits.list;
1347 struct commit **last = ctx->commits.list + ctx->commits.nr;
08fd81c9
DS
1348 struct commit_list *parent;
1349
1350 while (list < last) {
1351 int num_parents = 0;
53035c4f 1352
c9905bea 1353 display_progress(ctx->progress, ++ctx->progress_cnt);
53035c4f 1354
08fd81c9
DS
1355 for (parent = (*list)->parents; num_parents < 3 && parent;
1356 parent = parent->next)
1357 num_parents++;
1358
1359 if (num_parents <= 2) {
1360 list++;
1361 continue;
1362 }
1363
1364 /* Since num_parents > 2, this initializer is safe. */
1365 for (parent = (*list)->parents->next; parent; parent = parent->next) {
45ee13b9
JK
1366 int edge_value = oid_pos(&parent->item->object.oid,
1367 ctx->commits.list,
1368 ctx->commits.nr,
1369 commit_to_oid);
08fd81c9 1370
6c622f9f
DS
1371 if (edge_value >= 0)
1372 edge_value += ctx->new_num_commits_in_base;
8a6ac287 1373 else if (ctx->new_base_graph) {
6c622f9f 1374 uint32_t pos;
809ea28f
PS
1375 if (find_commit_pos_in_graph(parent->item,
1376 ctx->new_base_graph,
1377 &pos))
6c622f9f
DS
1378 edge_value = pos;
1379 }
1380
08fd81c9 1381 if (edge_value < 0)
cce99cd8
DS
1382 BUG("missing parent %s for commit %s",
1383 oid_to_hex(&parent->item->object.oid),
1384 oid_to_hex(&(*list)->object.oid));
08fd81c9
DS
1385 else if (!parent->next)
1386 edge_value |= GRAPH_LAST_EDGE;
1387
1388 hashwrite_be32(f, edge_value);
1389 }
1390
1391 list++;
1392 }
9bab081d
SG
1393
1394 return 0;
08fd81c9
DS
1395}
1396
9bab081d 1397static int write_graph_chunk_bloom_indexes(struct hashfile *f,
eb907191 1398 void *data)
76ffbca7 1399{
eb907191 1400 struct write_commit_graph_context *ctx = data;
76ffbca7
GS
1401 struct commit **list = ctx->commits.list;
1402 struct commit **last = ctx->commits.list + ctx->commits.nr;
1403 uint32_t cur_pos = 0;
76ffbca7
GS
1404
1405 while (list < last) {
312cff52 1406 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
94919742
DS
1407 size_t len = filter ? filter->len : 0;
1408 cur_pos += len;
150cd3b6 1409 display_progress(ctx->progress, ++ctx->progress_cnt);
76ffbca7
GS
1410 hashwrite_be32(f, cur_pos);
1411 list++;
1412 }
1413
9bab081d 1414 return 0;
76ffbca7
GS
1415}
1416
0087a87b
DS
1417static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1418{
1419 struct json_writer jw = JSON_WRITER_INIT;
1420
1421 jw_object_begin(&jw, 0);
1422 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1423 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1424 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
97ffa4fa 1425 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
0087a87b
DS
1426 jw_end(&jw);
1427
1428 trace2_data_json("bloom", ctx->r, "settings", &jw);
1429
1430 jw_release(&jw);
76ffbca7
GS
1431}
1432
9bab081d 1433static int write_graph_chunk_bloom_data(struct hashfile *f,
eb907191 1434 void *data)
76ffbca7 1435{
eb907191 1436 struct write_commit_graph_context *ctx = data;
76ffbca7
GS
1437 struct commit **list = ctx->commits.list;
1438 struct commit **last = ctx->commits.list + ctx->commits.nr;
76ffbca7 1439
0087a87b
DS
1440 trace2_bloom_filter_settings(ctx);
1441
98037f2b
DS
1442 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1443 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1444 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
76ffbca7
GS
1445
1446 while (list < last) {
312cff52 1447 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
94919742 1448 size_t len = filter ? filter->len : 0;
94919742 1449
150cd3b6 1450 display_progress(ctx->progress, ++ctx->progress_cnt);
94919742
DS
1451 if (len)
1452 hashwrite(f, filter->data, len * sizeof(unsigned char));
76ffbca7
GS
1453 list++;
1454 }
1455
9bab081d 1456 return 0;
76ffbca7
GS
1457}
1458
08fd81c9
DS
1459static int add_packed_commits(const struct object_id *oid,
1460 struct packed_git *pack,
1461 uint32_t pos,
1462 void *data)
1463{
c9905bea 1464 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
08fd81c9
DS
1465 enum object_type type;
1466 off_t offset = nth_packed_object_offset(pack, pos);
1467 struct object_info oi = OBJECT_INFO_INIT;
1468
c9905bea
DS
1469 if (ctx->progress)
1470 display_progress(ctx->progress, ++ctx->progress_done);
7b0f2292 1471
08fd81c9 1472 oi.typep = &type;
c9905bea 1473 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
4f5b532d 1474 die(_("unable to get type of object %s"), oid_to_hex(oid));
08fd81c9
DS
1475
1476 if (type != OBJ_COMMIT)
1477 return 0;
1478
a5f1c448 1479 oid_array_append(&ctx->oids, oid);
d21ee7d1
JK
1480 set_commit_pos(ctx->r, oid);
1481
08fd81c9
DS
1482 return 0;
1483}
1484
c9905bea 1485static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
4f2542b4
DS
1486{
1487 struct commit_list *parent;
1488 for (parent = commit->parents; parent; parent = parent->next) {
cb99a34e 1489 if (!(parent->item->object.flags & REACHABLE)) {
a5f1c448 1490 oid_array_append(&ctx->oids, &parent->item->object.oid);
cb99a34e 1491 parent->item->object.flags |= REACHABLE;
4f2542b4
DS
1492 }
1493 }
1494}
1495
c9905bea 1496static void close_reachable(struct write_commit_graph_context *ctx)
4f2542b4 1497{
49bbc57a 1498 int i;
4f2542b4 1499 struct commit *commit;
98bb7961
TB
1500 enum commit_graph_split_flags flags = ctx->opts ?
1501 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
4f2542b4 1502
c9905bea
DS
1503 if (ctx->report_progress)
1504 ctx->progress = start_delayed_progress(
1505 _("Loading known commits in commit graph"),
1506 ctx->oids.nr);
1507 for (i = 0; i < ctx->oids.nr; i++) {
1508 display_progress(ctx->progress, i + 1);
a5f1c448 1509 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
4f2542b4 1510 if (commit)
cb99a34e 1511 commit->object.flags |= REACHABLE;
4f2542b4 1512 }
c9905bea 1513 stop_progress(&ctx->progress);
4f2542b4
DS
1514
1515 /*
c9905bea 1516 * As this loop runs, ctx->oids.nr may grow, but not more
4f2542b4
DS
1517 * than the number of missing commits in the reachable
1518 * closure.
1519 */
c9905bea
DS
1520 if (ctx->report_progress)
1521 ctx->progress = start_delayed_progress(
1522 _("Expanding reachable commits in commit graph"),
67fa6aac 1523 0);
c9905bea
DS
1524 for (i = 0; i < ctx->oids.nr; i++) {
1525 display_progress(ctx->progress, i + 1);
a5f1c448 1526 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
4f2542b4 1527
6c622f9f
DS
1528 if (!commit)
1529 continue;
1530 if (ctx->split) {
c4cc0831 1531 if ((!repo_parse_commit(ctx->r, commit) &&
c49c82aa 1532 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
8a6ac287 1533 flags == COMMIT_GRAPH_SPLIT_REPLACE)
6c622f9f 1534 add_missing_parents(ctx, commit);
c4cc0831 1535 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
c9905bea 1536 add_missing_parents(ctx, commit);
4f2542b4 1537 }
c9905bea 1538 stop_progress(&ctx->progress);
4f2542b4 1539
c9905bea
DS
1540 if (ctx->report_progress)
1541 ctx->progress = start_delayed_progress(
1542 _("Clearing commit marks in commit graph"),
1543 ctx->oids.nr);
1544 for (i = 0; i < ctx->oids.nr; i++) {
1545 display_progress(ctx->progress, i + 1);
a5f1c448 1546 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
4f2542b4
DS
1547
1548 if (commit)
cb99a34e 1549 commit->object.flags &= ~REACHABLE;
4f2542b4 1550 }
c9905bea 1551 stop_progress(&ctx->progress);
4f2542b4
DS
1552}
1553
368d19b0
DS
1554struct compute_generation_info {
1555 struct repository *r;
1556 struct packed_commit_list *commits;
1557 struct progress *progress;
1558 int progress_cnt;
1559
1560 timestamp_t (*get_generation)(struct commit *c, void *data);
1561 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1562 void *data;
1563};
1564
1565static timestamp_t compute_generation_from_max(struct commit *c,
1566 timestamp_t max_gen,
1567 int generation_version)
1568{
1569 switch (generation_version) {
1570 case 1: /* topological levels */
1571 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1572 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1573 return max_gen + 1;
1574
1575 case 2: /* corrected commit date */
1576 if (c->date && c->date > max_gen)
1577 max_gen = c->date - 1;
1578 return max_gen + 1;
1579
1580 default:
1581 BUG("attempting unimplemented version");
1582 }
1583}
1584
1585static void compute_reachable_generation_numbers(
1586 struct compute_generation_info *info,
1587 int generation_version)
3258c663
DS
1588{
1589 int i;
1590 struct commit_list *list = NULL;
1591
368d19b0
DS
1592 for (i = 0; i < info->commits->nr; i++) {
1593 struct commit *c = info->commits->list[i];
1594 timestamp_t gen;
1595 repo_parse_commit(info->r, c);
1596 gen = info->get_generation(c, info->data);
1597 display_progress(info->progress, info->progress_cnt + 1);
4844812b 1598
368d19b0 1599 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
3258c663
DS
1600 continue;
1601
90cb1c47 1602 commit_list_insert(c, &list);
3258c663
DS
1603 while (list) {
1604 struct commit *current = list->item;
1605 struct commit_list *parent;
1606 int all_parents_computed = 1;
368d19b0 1607 uint32_t max_gen = 0;
3258c663
DS
1608
1609 for (parent = current->parents; parent; parent = parent->next) {
368d19b0
DS
1610 repo_parse_commit(info->r, parent->item);
1611 gen = info->get_generation(parent->item, info->data);
4844812b 1612
368d19b0 1613 if (gen == GENERATION_NUMBER_ZERO) {
3258c663
DS
1614 all_parents_computed = 0;
1615 commit_list_insert(parent->item, &list);
1616 break;
3258c663 1617 }
c1a09119 1618
368d19b0
DS
1619 if (gen > max_gen)
1620 max_gen = gen;
3258c663
DS
1621 }
1622
1623 if (all_parents_computed) {
3258c663 1624 pop_commit(&list);
368d19b0
DS
1625 gen = compute_generation_from_max(
1626 current, max_gen,
1627 generation_version);
1628 info->set_generation(current, gen, info->data);
9c2c0a82
DS
1629 }
1630 }
1631 }
368d19b0
DS
1632}
1633
1634static timestamp_t get_topo_level(struct commit *c, void *data)
1635{
1636 struct write_commit_graph_context *ctx = data;
1637 return *topo_level_slab_at(ctx->topo_levels, c);
1638}
1639
1640static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1641{
1642 struct write_commit_graph_context *ctx = data;
1643 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1644}
1645
1646static void compute_topological_levels(struct write_commit_graph_context *ctx)
1647{
1648 struct compute_generation_info info = {
1649 .r = ctx->r,
1650 .commits = &ctx->commits,
1651 .get_generation = get_topo_level,
1652 .set_generation = set_topo_level,
1653 .data = ctx,
1654 };
1655
1656 if (ctx->report_progress)
1657 info.progress = ctx->progress
1658 = start_delayed_progress(
1659 _("Computing commit graph topological levels"),
1660 ctx->commits.nr);
1661
1662 compute_reachable_generation_numbers(&info, 1);
1663
9c2c0a82
DS
1664 stop_progress(&ctx->progress);
1665}
1666
e1cba404
JK
1667static timestamp_t get_generation_from_graph_data(struct commit *c,
1668 void *data UNUSED)
80c928d9
DS
1669{
1670 return commit_graph_data_at(c)->generation;
1671}
1672
e1cba404
JK
1673static void set_generation_v2(struct commit *c, timestamp_t t,
1674 void *data UNUSED)
80c928d9
DS
1675{
1676 struct commit_graph_data *g = commit_graph_data_at(c);
d3af1c19 1677 g->generation = t;
80c928d9
DS
1678}
1679
9c2c0a82
DS
1680static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1681{
1682 int i;
80c928d9
DS
1683 struct compute_generation_info info = {
1684 .r = ctx->r,
1685 .commits = &ctx->commits,
1686 .get_generation = get_generation_from_graph_data,
1687 .set_generation = set_generation_v2,
80c928d9 1688 };
9c2c0a82
DS
1689
1690 if (ctx->report_progress)
80c928d9
DS
1691 info.progress = ctx->progress
1692 = start_delayed_progress(
9c2c0a82
DS
1693 _("Computing commit graph generation numbers"),
1694 ctx->commits.nr);
fde55b09
DS
1695
1696 if (!ctx->trust_generation_numbers) {
1697 for (i = 0; i < ctx->commits.nr; i++) {
1698 struct commit *c = ctx->commits.list[i];
1699 repo_parse_commit(ctx->r, c);
1700 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1701 }
1702 }
1703
80c928d9 1704 compute_reachable_generation_numbers(&info, 2);
75979d94
DS
1705
1706 for (i = 0; i < ctx->commits.nr; i++) {
1707 struct commit *c = ctx->commits.list[i];
1708 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1709 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1710 ctx->num_generation_data_overflows++;
1711 }
c9905bea 1712 stop_progress(&ctx->progress);
3258c663
DS
1713}
1714
c08645b3 1715static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
e1cba404 1716 void *data UNUSED)
c08645b3
TB
1717{
1718 commit_graph_data_at(c)->generation = t;
1719}
1720
1721/*
1722 * After this method, all commits reachable from those in the given
1723 * list will have non-zero, non-infinite generation numbers.
1724 */
1725void ensure_generations_valid(struct repository *r,
1726 struct commit **commits, size_t nr)
1727{
1728 int generation_version = get_configured_generation_version(r);
1729 struct packed_commit_list list = {
1730 .list = commits,
1731 .alloc = nr,
1732 .nr = nr,
1733 };
1734 struct compute_generation_info info = {
1735 .r = r,
1736 .commits = &list,
1737 .get_generation = get_generation_from_graph_data,
1738 .set_generation = set_generation_in_graph_data,
1739 };
1740
1741 compute_reachable_generation_numbers(&info, generation_version);
1742}
1743
312cff52
TB
1744static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1745{
1746 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1747 ctx->count_bloom_filter_computed);
1748 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1749 ctx->count_bloom_filter_not_computed);
59f0d507
TB
1750 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1751 ctx->count_bloom_filter_trunc_empty);
312cff52
TB
1752 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1753 ctx->count_bloom_filter_trunc_large);
1754}
1755
f97b9325
GS
1756static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1757{
1758 int i;
1759 struct progress *progress = NULL;
d21ee7d1 1760 struct commit **sorted_commits;
809e0327 1761 int max_new_filters;
f97b9325
GS
1762
1763 init_bloom_filters();
1764
1765 if (ctx->report_progress)
1766 progress = start_delayed_progress(
1767 _("Computing commit changed paths Bloom filters"),
1768 ctx->commits.nr);
1769
6e578410 1770 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
3d112755
GS
1771
1772 if (ctx->order_by_pack)
1773 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1774 else
1775 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
d21ee7d1 1776
809e0327
TB
1777 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1778 ctx->opts->max_new_filters : ctx->commits.nr;
1779
f97b9325 1780 for (i = 0; i < ctx->commits.nr; i++) {
312cff52 1781 enum bloom_filter_computed computed = 0;
d21ee7d1 1782 struct commit *c = sorted_commits[i];
312cff52
TB
1783 struct bloom_filter *filter = get_or_compute_bloom_filter(
1784 ctx->r,
1785 c,
809e0327 1786 ctx->count_bloom_filter_computed < max_new_filters,
9a7a9ed1 1787 ctx->bloom_settings,
312cff52
TB
1788 &computed);
1789 if (computed & BLOOM_COMPUTED) {
1790 ctx->count_bloom_filter_computed++;
59f0d507
TB
1791 if (computed & BLOOM_TRUNC_EMPTY)
1792 ctx->count_bloom_filter_trunc_empty++;
312cff52
TB
1793 if (computed & BLOOM_TRUNC_LARGE)
1794 ctx->count_bloom_filter_trunc_large++;
1795 } else if (computed & BLOOM_NOT_COMPUTED)
1796 ctx->count_bloom_filter_not_computed++;
809e0327
TB
1797 ctx->total_bloom_filter_data_size += filter
1798 ? sizeof(unsigned char) * filter->len : 0;
f97b9325
GS
1799 display_progress(progress, i + 1);
1800 }
1801
312cff52
TB
1802 if (trace2_is_enabled())
1803 trace2_bloom_filter_write_statistics(ctx);
1804
d21ee7d1 1805 free(sorted_commits);
f97b9325
GS
1806 stop_progress(&progress);
1807}
1808
1fe10844
TB
1809struct refs_cb_data {
1810 struct oidset *commits;
d335ce8f 1811 struct progress *progress;
1fe10844
TB
1812};
1813
5cf88fd8 1814static int add_ref_to_set(const char *refname UNUSED,
6830c360 1815 const struct object_id *oid,
5cf88fd8 1816 int flags UNUSED, void *cb_data)
59fb8770 1817{
630cd519 1818 struct object_id peeled;
1fe10844 1819 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
59fb8770 1820
36a31792 1821 if (!peel_iterated_oid(oid, &peeled))
630cd519
TB
1822 oid = &peeled;
1823 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1824 oidset_insert(data->commits, oid);
d335ce8f
TB
1825
1826 display_progress(data->progress, oidset_size(data->commits));
59fb8770 1827
59fb8770
DS
1828 return 0;
1829}
1830
0bd52e27 1831int write_commit_graph_reachable(struct object_directory *odb,
39d88318 1832 enum commit_graph_write_flags flags,
98bb7961 1833 const struct commit_graph_opts *opts)
59fb8770 1834{
6830c360 1835 struct oidset commits = OIDSET_INIT;
1fe10844 1836 struct refs_cb_data data;
e103f727 1837 int result;
59fb8770 1838
1fe10844
TB
1839 memset(&data, 0, sizeof(data));
1840 data.commits = &commits;
d335ce8f
TB
1841 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1842 data.progress = start_delayed_progress(
1843 _("Collecting referenced commits"), 0);
1fe10844
TB
1844
1845 for_each_ref(add_ref_to_set, &data);
6f9d5f2f
SG
1846
1847 stop_progress(&data.progress);
1848
6830c360 1849 result = write_commit_graph(odb, NULL, &commits,
98bb7961 1850 flags, opts);
f4dbdfc4 1851
6830c360 1852 oidset_clear(&commits);
e103f727 1853 return result;
59fb8770
DS
1854}
1855
ef5b83f2 1856static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
4a047908 1857 const struct string_list *pack_indexes)
08fd81c9 1858{
ef5b83f2 1859 uint32_t i;
28944739 1860 struct strbuf progress_title = STRBUF_INIT;
ef5b83f2
DS
1861 struct strbuf packname = STRBUF_INIT;
1862 int dirlen;
51a94d8f 1863 int ret = 0;
08fd81c9 1864
0bd52e27 1865 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
ef5b83f2
DS
1866 dirlen = packname.len;
1867 if (ctx->report_progress) {
1868 strbuf_addf(&progress_title,
99d60545
ÆAB
1869 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1870 "Finding commits for commit graph in %"PRIuMAX" packs",
ef5b83f2 1871 pack_indexes->nr),
99d60545 1872 (uintmax_t)pack_indexes->nr);
ef5b83f2
DS
1873 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1874 ctx->progress_done = 0;
7547b95b 1875 }
ef5b83f2
DS
1876 for (i = 0; i < pack_indexes->nr; i++) {
1877 struct packed_git *p;
1878 strbuf_setlen(&packname, dirlen);
1879 strbuf_addstr(&packname, pack_indexes->items[i].string);
1880 p = add_packed_git(packname.buf, packname.len, 1);
1881 if (!p) {
51a94d8f
ÆAB
1882 ret = error(_("error adding pack %s"), packname.buf);
1883 goto cleanup;
7547b95b 1884 }
ef5b83f2 1885 if (open_pack_index(p)) {
51a94d8f
ÆAB
1886 ret = error(_("error opening index for %s"), packname.buf);
1887 goto cleanup;
ef5b83f2
DS
1888 }
1889 for_each_object_in_pack(p, add_packed_commits, ctx,
1890 FOR_EACH_OBJECT_PACK_ORDER);
1891 close_pack(p);
1892 free(p);
7547b95b
DS
1893 }
1894
51a94d8f 1895cleanup:
ef5b83f2 1896 stop_progress(&ctx->progress);
0aa6bce7 1897 strbuf_release(&progress_title);
ef5b83f2
DS
1898 strbuf_release(&packname);
1899
51a94d8f 1900 return ret;
ef5b83f2
DS
1901}
1902
6830c360
TB
1903static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1904 struct oidset *commits)
4c9efe85 1905{
6830c360
TB
1906 struct oidset_iter iter;
1907 struct object_id *oid;
1908
1909 if (!oidset_size(commits))
1910 return 0;
4c9efe85 1911
6830c360
TB
1912 oidset_iter_init(commits, &iter);
1913 while ((oid = oidset_iter_next(&iter))) {
a5f1c448 1914 oid_array_append(&ctx->oids, oid);
3d5df01b 1915 }
6830c360 1916
7c5c9b9c 1917 return 0;
4c9efe85 1918}
3d5df01b 1919
b2c83060
DS
1920static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1921{
1922 if (ctx->report_progress)
1923 ctx->progress = start_delayed_progress(
1924 _("Finding commits for commit graph among packed objects"),
1925 ctx->approx_nr_objects);
1926 for_each_packed_object(add_packed_commits, ctx,
1927 FOR_EACH_OBJECT_PACK_ORDER);
1928 if (ctx->progress_done < ctx->approx_nr_objects)
1929 display_progress(ctx->progress, ctx->approx_nr_objects);
1930 stop_progress(&ctx->progress);
1931}
049d51a2 1932
f998d542
DS
1933static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1934{
1935 uint32_t i;
98bb7961
TB
1936 enum commit_graph_split_flags flags = ctx->opts ?
1937 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
08fd81c9 1938
f998d542
DS
1939 ctx->num_extra_edges = 0;
1940 if (ctx->report_progress)
1941 ctx->progress = start_delayed_progress(
890226cc 1942 _("Finding extra edges in commit graph"),
f998d542 1943 ctx->oids.nr);
a5f1c448
JK
1944 oid_array_sort(&ctx->oids);
1945 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
689a146c
RS
1946 unsigned int num_parents;
1947
f998d542 1948 display_progress(ctx->progress, i + 1);
08fd81c9 1949
6c622f9f 1950 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
a5f1c448 1951 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
6c622f9f 1952
8a6ac287 1953 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
c49c82aa 1954 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
6c622f9f
DS
1955 continue;
1956
8a6ac287 1957 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
c4cc0831 1958 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
8a6ac287 1959 else
c4cc0831 1960 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
08fd81c9 1961
689a146c 1962 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
08fd81c9 1963 if (num_parents > 2)
f998d542 1964 ctx->num_extra_edges += num_parents - 1;
08fd81c9 1965
f998d542 1966 ctx->commits.nr++;
08fd81c9 1967 }
f998d542
DS
1968 stop_progress(&ctx->progress);
1969}
08fd81c9 1970
6c622f9f
DS
1971static int write_graph_chunk_base_1(struct hashfile *f,
1972 struct commit_graph *g)
1973{
1974 int num = 0;
1975
1976 if (!g)
1977 return 0;
1978
1979 num = write_graph_chunk_base_1(f, g->base_graph);
1980 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1981 return num + 1;
1982}
1983
1984static int write_graph_chunk_base(struct hashfile *f,
eb907191 1985 void *data)
6c622f9f 1986{
eb907191 1987 struct write_commit_graph_context *ctx = data;
6c622f9f
DS
1988 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1989
1990 if (num != ctx->num_commit_graphs_after - 1) {
1991 error(_("failed to write correct number of base graph ids"));
1992 return -1;
1993 }
1994
1995 return 0;
1996}
1997
238def57 1998static int write_commit_graph_file(struct write_commit_graph_context *ctx)
08fd81c9 1999{
238def57 2000 uint32_t i;
6c622f9f 2001 int fd;
08fd81c9 2002 struct hashfile *f;
08fd81c9 2003 struct lock_file lk = LOCK_INIT;
c1665998 2004 const unsigned hashsz = the_hash_algo->rawsz;
28944739 2005 struct strbuf progress_title = STRBUF_INIT;
47410aa8 2006 struct chunkfile *cf;
72871b13 2007 unsigned char file_hash[GIT_MAX_RAWSZ];
6c622f9f
DS
2008
2009 if (ctx->split) {
2010 struct strbuf tmp_file = STRBUF_INIT;
2011
2012 strbuf_addf(&tmp_file,
2013 "%s/info/commit-graphs/tmp_graph_XXXXXX",
0bd52e27 2014 ctx->odb->path);
6c622f9f
DS
2015 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2016 } else {
ad2dd5bb 2017 ctx->graph_name = get_commit_graph_filename(ctx->odb);
6c622f9f 2018 }
238def57 2019
238def57
DS
2020 if (safe_create_leading_directories(ctx->graph_name)) {
2021 UNLEAK(ctx->graph_name);
2022 error(_("unable to create leading directories of %s"),
2023 ctx->graph_name);
2024 return -1;
f4dbdfc4 2025 }
08fd81c9 2026
6c622f9f 2027 if (ctx->split) {
663b2b1b 2028 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
08fd81c9 2029
45a4365c
TB
2030 hold_lock_file_for_update_mode(&lk, lock_name,
2031 LOCK_DIE_ON_ERROR, 0444);
ef3fe214 2032 free(lock_name);
08fd81c9 2033
6c622f9f
DS
2034 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2035 if (fd < 0) {
a2d57e22 2036 error(_("unable to create temporary graph layer"));
6c622f9f
DS
2037 return -1;
2038 }
2039
f4d62847
TB
2040 if (adjust_shared_perm(ctx->graph_name)) {
2041 error(_("unable to adjust shared permissions for '%s'"),
2042 ctx->graph_name);
2043 return -1;
2044 }
2045
6c622f9f
DS
2046 f = hashfd(fd, ctx->graph_name);
2047 } else {
1f9becae
TB
2048 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2049 LOCK_DIE_ON_ERROR, 0444);
a52cdce9
2050 fd = get_lock_file_fd(&lk);
2051 f = hashfd(fd, get_lock_file_path(&lk));
6c622f9f 2052 }
08fd81c9 2053
47410aa8
DS
2054 cf = init_chunkfile(f);
2055
2056 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2057 write_graph_chunk_fanout);
48f3f8cf 2058 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
47410aa8 2059 write_graph_chunk_oids);
48f3f8cf 2060 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
47410aa8 2061 write_graph_chunk_data);
e8b63005 2062
47410aa8
DS
2063 if (ctx->write_generation_data)
2064 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
48f3f8cf 2065 st_mult(sizeof(uint32_t), ctx->commits.nr),
47410aa8
DS
2066 write_graph_chunk_generation_data);
2067 if (ctx->num_generation_data_overflows)
2068 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
48f3f8cf 2069 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
47410aa8
DS
2070 write_graph_chunk_generation_data_overflow);
2071 if (ctx->num_extra_edges)
2072 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
48f3f8cf 2073 st_mult(4, ctx->num_extra_edges),
47410aa8 2074 write_graph_chunk_extra_edges);
76ffbca7 2075 if (ctx->changed_paths) {
47410aa8 2076 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
48f3f8cf 2077 st_mult(sizeof(uint32_t), ctx->commits.nr),
47410aa8
DS
2078 write_graph_chunk_bloom_indexes);
2079 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
48f3f8cf
TB
2080 st_add(sizeof(uint32_t) * 3,
2081 ctx->total_bloom_filter_data_size),
47410aa8 2082 write_graph_chunk_bloom_data);
76ffbca7 2083 }
47410aa8
DS
2084 if (ctx->num_commit_graphs_after > 1)
2085 add_chunk(cf, GRAPH_CHUNKID_BASE,
48f3f8cf 2086 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
47410aa8 2087 write_graph_chunk_base);
144354b0
DS
2088
2089 hashwrite_be32(f, GRAPH_SIGNATURE);
2090
2091 hashwrite_u8(f, GRAPH_VERSION);
d9fef9d9 2092 hashwrite_u8(f, oid_version(the_hash_algo));
47410aa8 2093 hashwrite_u8(f, get_num_chunks(cf));
6c622f9f 2094 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
08fd81c9 2095
238def57 2096 if (ctx->report_progress) {
28944739
ÆAB
2097 strbuf_addf(&progress_title,
2098 Q_("Writing out commit graph in %d pass",
2099 "Writing out commit graph in %d passes",
c4ff24bb
TB
2100 get_num_chunks(cf)),
2101 get_num_chunks(cf));
238def57 2102 ctx->progress = start_delayed_progress(
28944739 2103 progress_title.buf,
48f3f8cf 2104 st_mult(get_num_chunks(cf), ctx->commits.nr));
28944739 2105 }
17e6275f 2106
47410aa8 2107 write_chunkfile(cf, ctx);
17e6275f 2108
238def57 2109 stop_progress(&ctx->progress);
28944739 2110 strbuf_release(&progress_title);
08fd81c9 2111
6c622f9f
DS
2112 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2113 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
ad2dd5bb 2114 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
6c622f9f
DS
2115
2116 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2117 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2118 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2119 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2120 }
2121
c3a3a964 2122 close_commit_graph(ctx->r->objects);
020406ea
NS
2123 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2124 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
47410aa8 2125 free_chunkfile(cf);
6c622f9f
DS
2126
2127 if (ctx->split) {
2128 FILE *chainf = fdopen_lock_file(&lk, "w");
2129 char *final_graph_name;
2130 int result;
2131
2132 close(fd);
2133
2134 if (!chainf) {
2135 error(_("unable to open commit-graph chain file"));
2136 return -1;
2137 }
2138
2139 if (ctx->base_graph_name) {
8a6ac287
TB
2140 const char *dest;
2141 int idx = ctx->num_commit_graphs_after - 1;
2142 if (ctx->num_commit_graphs_after > 1)
2143 idx--;
2144
2145 dest = ctx->commit_graph_filenames_after[idx];
6c622f9f 2146
135a7123
DS
2147 if (strcmp(ctx->base_graph_name, dest)) {
2148 result = rename(ctx->base_graph_name, dest);
2149
2150 if (result) {
2151 error(_("failed to rename base commit-graph file"));
2152 return -1;
2153 }
6c622f9f
DS
2154 }
2155 } else {
ad2dd5bb 2156 char *graph_name = get_commit_graph_filename(ctx->odb);
6c622f9f 2157 unlink(graph_name);
ef3fe214 2158 free(graph_name);
6c622f9f
DS
2159 }
2160
274bfa7f 2161 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
72871b13 2162 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
ad2dd5bb 2163 final_graph_name = get_split_graph_filename(ctx->odb,
6c622f9f 2164 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
274bfa7f 2165 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
6c622f9f
DS
2166 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2167
2168 result = rename(ctx->graph_name, final_graph_name);
2169
2170 for (i = 0; i < ctx->num_commit_graphs_after; i++)
a52cdce9 2171 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
6c622f9f
DS
2172
2173 if (result) {
2174 error(_("failed to rename temporary commit-graph file"));
2175 return -1;
2176 }
2177 }
2178
08fd81c9
DS
2179 commit_lock_file(&lk);
2180
238def57
DS
2181 return 0;
2182}
2183
1771be90
DS
2184static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2185{
8da02ce6
AH
2186 struct commit_graph *g;
2187 uint32_t num_commits;
fdbde82f 2188 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1771be90
DS
2189 uint32_t i;
2190
c2bc6e6a
DS
2191 int max_commits = 0;
2192 int size_mult = 2;
2193
98bb7961
TB
2194 if (ctx->opts) {
2195 max_commits = ctx->opts->max_commits;
63020f17 2196
98bb7961
TB
2197 if (ctx->opts->size_multiple)
2198 size_mult = ctx->opts->size_multiple;
fdbde82f 2199
98bb7961 2200 flags = ctx->opts->split_flags;
c2bc6e6a
DS
2201 }
2202
1771be90 2203 g = ctx->r->objects->commit_graph;
8da02ce6 2204 num_commits = ctx->commits.nr;
8a6ac287
TB
2205 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2206 ctx->num_commit_graphs_after = 1;
2207 else
2208 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1771be90 2209
8a6ac287
TB
2210 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2211 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
19565d09 2212 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
fdbde82f
TB
2213 (max_commits && num_commits > max_commits))) {
2214 if (g->odb != ctx->odb)
2215 break;
c523035c 2216
19565d09
TB
2217 if (unsigned_add_overflows(num_commits, g->num_commits))
2218 die(_("cannot merge graphs with %"PRIuMAX", "
2219 "%"PRIuMAX" commits"),
2220 (uintmax_t)num_commits,
2221 (uintmax_t)g->num_commits);
fdbde82f
TB
2222 num_commits += g->num_commits;
2223 g = g->base_graph;
1771be90 2224
fdbde82f
TB
2225 ctx->num_commit_graphs_after--;
2226 }
1771be90
DS
2227 }
2228
8a6ac287
TB
2229 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2230 ctx->new_base_graph = g;
2231 else if (ctx->num_commit_graphs_after != 1)
2232 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2233 "should be 1 with --split=replace");
1771be90 2234
c523035c 2235 if (ctx->num_commit_graphs_after == 2) {
ad2dd5bb 2236 char *old_graph_name = get_commit_graph_filename(g->odb);
c523035c
DS
2237
2238 if (!strcmp(g->filename, old_graph_name) &&
ad2dd5bb 2239 g->odb != ctx->odb) {
c523035c
DS
2240 ctx->num_commit_graphs_after = 1;
2241 ctx->new_base_graph = NULL;
2242 }
2243
2244 free(old_graph_name);
2245 }
2246
b78a556a
TB
2247 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2248 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1771be90
DS
2249
2250 for (i = 0; i < ctx->num_commit_graphs_after &&
2251 i < ctx->num_commit_graphs_before; i++)
2252 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2253
2254 i = ctx->num_commit_graphs_before - 1;
2255 g = ctx->r->objects->commit_graph;
2256
2257 while (g) {
2258 if (i < ctx->num_commit_graphs_after)
2259 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2260
1fdc383c
AK
2261 /*
2262 * If the topmost remaining layer has generation data chunk, the
2263 * resultant layer also has generation data chunk.
2264 */
2265 if (i == ctx->num_commit_graphs_after - 2)
2266 ctx->write_generation_data = !!g->chunk_generation_data;
2267
1771be90
DS
2268 i--;
2269 g = g->base_graph;
2270 }
2271}
2272
2273static void merge_commit_graph(struct write_commit_graph_context *ctx,
2274 struct commit_graph *g)
2275{
2276 uint32_t i;
2277 uint32_t offset = g->num_commits_in_base;
2278
d76e0a74
TB
2279 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2280 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2281 oid_to_hex(&g->oid),
2282 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2283
1771be90
DS
2284 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2285
2286 for (i = 0; i < g->num_commits; i++) {
2287 struct object_id oid;
2288 struct commit *result;
2289
2290 display_progress(ctx->progress, i + 1);
2291
2292 load_oid_from_graph(g, i + offset, &oid);
2293
2294 /* only add commits if they still exist in the repo */
2295 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2296
2297 if (result) {
2298 ctx->commits.list[ctx->commits.nr] = result;
2299 ctx->commits.nr++;
2300 }
2301 }
2302}
2303
2304static int commit_compare(const void *_a, const void *_b)
2305{
2306 const struct commit *a = *(const struct commit **)_a;
2307 const struct commit *b = *(const struct commit **)_b;
2308 return oidcmp(&a->object.oid, &b->object.oid);
2309}
2310
2311static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2312{
150f1157 2313 uint32_t i, dedup_i = 0;
1771be90
DS
2314
2315 if (ctx->report_progress)
2316 ctx->progress = start_delayed_progress(
2317 _("Scanning merged commits"),
2318 ctx->commits.nr);
2319
2320 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2321
2322 ctx->num_extra_edges = 0;
2323 for (i = 0; i < ctx->commits.nr; i++) {
40112249 2324 display_progress(ctx->progress, i + 1);
1771be90
DS
2325
2326 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2327 &ctx->commits.list[i]->object.oid)) {
150f1157
DS
2328 /*
2329 * Silently ignore duplicates. These were likely
2330 * created due to a commit appearing in multiple
2331 * layers of the chain, which is unexpected but
2332 * not invalid. We should make sure there is a
2333 * unique copy in the new layer.
2334 */
1771be90 2335 } else {
689a146c 2336 unsigned int num_parents;
1771be90 2337
150f1157
DS
2338 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2339 dedup_i++;
2340
689a146c 2341 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1771be90 2342 if (num_parents > 2)
a35bea40 2343 ctx->num_extra_edges += num_parents - 1;
1771be90
DS
2344 }
2345 }
2346
150f1157
DS
2347 ctx->commits.nr = dedup_i;
2348
1771be90
DS
2349 stop_progress(&ctx->progress);
2350}
2351
2352static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2353{
2354 struct commit_graph *g = ctx->r->objects->commit_graph;
2355 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1771be90
DS
2356
2357 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2358 current_graph_number--;
2359
d68ce906
RS
2360 if (ctx->report_progress)
2361 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
1771be90
DS
2362
2363 merge_commit_graph(ctx, g);
2364 stop_progress(&ctx->progress);
1771be90
DS
2365
2366 g = g->base_graph;
2367 }
2368
2369 if (g) {
2370 ctx->new_base_graph = g;
2371 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2372 }
2373
2374 if (ctx->new_base_graph)
2375 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2376
2377 sort_and_scan_merged_commits(ctx);
2378}
2379
8d84097f
DS
2380static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2381{
2382 uint32_t i;
2383 time_t now = time(NULL);
2384
2385 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2386 struct stat st;
2387 struct utimbuf updated_time;
2388
7c898554
ÆAB
2389 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2390 continue;
8d84097f
DS
2391
2392 updated_time.actime = st.st_atime;
2393 updated_time.modtime = now;
2394 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2395 }
2396}
2397
2398static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2399{
2400 struct strbuf path = STRBUF_INIT;
2401 DIR *dir;
2402 struct dirent *de;
2403 size_t dirnamelen;
c2bc6e6a
DS
2404 timestamp_t expire_time = time(NULL);
2405
98bb7961
TB
2406 if (ctx->opts && ctx->opts->expire_time)
2407 expire_time = ctx->opts->expire_time;
ba41112a 2408 if (!ctx->split) {
663b2b1b 2409 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
ba41112a
DS
2410 unlink(chain_file_name);
2411 free(chain_file_name);
2412 ctx->num_commit_graphs_after = 0;
2413 }
8d84097f 2414
0bd52e27 2415 strbuf_addstr(&path, ctx->odb->path);
8d84097f
DS
2416 strbuf_addstr(&path, "/info/commit-graphs");
2417 dir = opendir(path.buf);
2418
0aa6bce7
RS
2419 if (!dir)
2420 goto out;
8d84097f
DS
2421
2422 strbuf_addch(&path, '/');
2423 dirnamelen = path.len;
2424 while ((de = readdir(dir)) != NULL) {
2425 struct stat st;
2426 uint32_t i, found = 0;
2427
2428 strbuf_setlen(&path, dirnamelen);
2429 strbuf_addstr(&path, de->d_name);
2430
7c898554
ÆAB
2431 if (stat(path.buf, &st) < 0)
2432 continue;
8d84097f
DS
2433
2434 if (st.st_mtime > expire_time)
2435 continue;
2436 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2437 continue;
2438
2439 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2440 if (!strcmp(ctx->commit_graph_filenames_after[i],
2441 path.buf)) {
2442 found = 1;
2443 break;
2444 }
2445 }
2446
2447 if (!found)
2448 unlink(path.buf);
8d84097f 2449 }
0aa6bce7
RS
2450
2451out:
12f1ae53
ML
2452 if(dir)
2453 closedir(dir);
0aa6bce7 2454 strbuf_release(&path);
8d84097f
DS
2455}
2456
0bd52e27 2457int write_commit_graph(struct object_directory *odb,
4a047908 2458 const struct string_list *const pack_indexes,
6830c360 2459 struct oidset *commits,
39d88318 2460 enum commit_graph_write_flags flags,
98bb7961 2461 const struct commit_graph_opts *opts)
238def57 2462{
c7ef8fe6 2463 struct repository *r = the_repository;
238def57 2464 struct write_commit_graph_context *ctx;
1cbdbf3b 2465 uint32_t i;
e103f727 2466 int res = 0;
8a6ac287 2467 int replace = 0;
9a7a9ed1 2468 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
72a2bfca 2469 struct topo_level_slab topo_levels;
08fd81c9 2470
c7ef8fe6
DS
2471 prepare_repo_settings(r);
2472 if (!r->settings.core_commit_graph) {
85102ac7
DS
2473 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2474 return 0;
2475 }
c7ef8fe6 2476 if (!commit_graph_compatible(r))
e103f727 2477 return 0;
d6538246 2478
ca56dadb 2479 CALLOC_ARRAY(ctx, 1);
c7ef8fe6 2480 ctx->r = r;
0bd52e27 2481 ctx->odb = odb;
39d88318
SG
2482 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2483 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2484 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
98bb7961 2485 ctx->opts = opts;
f97b9325 2486 ctx->total_bloom_filter_data_size = 0;
702110aa 2487 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
e8b63005 2488 ctx->num_generation_data_overflows = 0;
6c622f9f 2489
9a7a9ed1
TB
2490 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2491 bloom_settings.bits_per_entry);
2492 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2493 bloom_settings.num_hashes);
2494 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2495 bloom_settings.max_changed_paths);
2496 ctx->bloom_settings = &bloom_settings;
2497
72a2bfca
AK
2498 init_topo_level_slab(&topo_levels);
2499 ctx->topo_levels = &topo_levels;
2500
bc50d6c9 2501 prepare_commit_graph(ctx->r);
72a2bfca
AK
2502 if (ctx->r->objects->commit_graph) {
2503 struct commit_graph *g = ctx->r->objects->commit_graph;
2504
2505 while (g) {
2506 g->topo_levels = &topo_levels;
2507 g = g->base_graph;
2508 }
2509 }
2510
0087a87b
DS
2511 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2512 ctx->changed_paths = 1;
2513 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2514 struct commit_graph *g;
0087a87b
DS
2515
2516 g = ctx->r->objects->commit_graph;
2517
2518 /* We have changed-paths already. Keep them in the next graph */
2519 if (g && g->chunk_bloom_data) {
2520 ctx->changed_paths = 1;
2521 ctx->bloom_settings = g->bloom_filter_settings;
2522 }
2523 }
2524
6c622f9f 2525 if (ctx->split) {
bc50d6c9 2526 struct commit_graph *g = ctx->r->objects->commit_graph;
6c622f9f
DS
2527
2528 while (g) {
2529 ctx->num_commit_graphs_before++;
2530 g = g->base_graph;
2531 }
2532
2533 if (ctx->num_commit_graphs_before) {
2534 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2535 i = ctx->num_commit_graphs_before;
2536 g = ctx->r->objects->commit_graph;
2537
2538 while (g) {
2539 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2540 g = g->base_graph;
2541 }
2542 }
8a6ac287 2543
98bb7961
TB
2544 if (ctx->opts)
2545 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
6c622f9f 2546 }
c9905bea 2547
afe27c88 2548 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
c9905bea 2549
c9905bea
DS
2550 if (ctx->append && ctx->r->objects->commit_graph) {
2551 struct commit_graph *g = ctx->r->objects->commit_graph;
2552 for (i = 0; i < g->num_commits; i++) {
a5f1c448 2553 struct object_id oid;
588af1bf 2554 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
a5f1c448 2555 oid_array_append(&ctx->oids, &oid);
7547b95b
DS
2556 }
2557 }
2558
049d51a2 2559 if (pack_indexes) {
3d112755 2560 ctx->order_by_pack = 1;
ef5b83f2
DS
2561 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2562 goto cleanup;
3d5df01b
DS
2563 }
2564
6830c360
TB
2565 if (commits) {
2566 if ((res = fill_oids_from_commits(ctx, commits)))
7c5c9b9c
SG
2567 goto cleanup;
2568 }
3d5df01b 2569
9b6606f4 2570 if (!pack_indexes && !commits) {
3d112755 2571 ctx->order_by_pack = 1;
b2c83060 2572 fill_oids_from_all_packs(ctx);
3d112755 2573 }
049d51a2 2574
c9905bea 2575 close_reachable(ctx);
08fd81c9 2576
f998d542 2577 copy_oids_to_commits(ctx);
08fd81c9 2578
c9905bea 2579 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
e103f727
DS
2580 error(_("too many commits to write graph"));
2581 res = -1;
2582 goto cleanup;
2583 }
08fd81c9 2584
8a6ac287 2585 if (!ctx->commits.nr && !replace)
6c622f9f
DS
2586 goto cleanup;
2587
1771be90
DS
2588 if (ctx->split) {
2589 split_graph_merge_strategy(ctx);
2590
8a6ac287
TB
2591 if (!replace)
2592 merge_commit_graphs(ctx);
1771be90 2593 } else
6c622f9f
DS
2594 ctx->num_commit_graphs_after = 1;
2595
fde55b09 2596 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
1fdc383c 2597
9c2c0a82
DS
2598 compute_topological_levels(ctx);
2599 if (ctx->write_generation_data)
2600 compute_generation_numbers(ctx);
08fd81c9 2601
f97b9325
GS
2602 if (ctx->changed_paths)
2603 compute_bloom_filters(ctx);
2604
238def57 2605 res = write_commit_graph_file(ctx);
08fd81c9 2606
ba41112a 2607 if (ctx->split)
8d84097f 2608 mark_commit_graphs(ctx);
ba41112a
DS
2609
2610 expire_commit_graphs(ctx);
8d84097f 2611
e103f727 2612cleanup:
238def57 2613 free(ctx->graph_name);
d9c84c6d 2614 free(ctx->base_graph_name);
c9905bea 2615 free(ctx->commits.list);
a5f1c448 2616 oid_array_clear(&ctx->oids);
bf4bb9f9 2617 clear_topo_level_slab(&topo_levels);
6c622f9f
DS
2618
2619 if (ctx->commit_graph_filenames_after) {
2620 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2621 free(ctx->commit_graph_filenames_after[i]);
2622 free(ctx->commit_graph_hash_after[i]);
2623 }
2624
2625 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2626 free(ctx->commit_graph_filenames_before[i]);
2627
2628 free(ctx->commit_graph_filenames_after);
2629 free(ctx->commit_graph_filenames_before);
2630 free(ctx->commit_graph_hash_after);
2631 }
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}