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