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