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