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