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