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