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