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