submodule-config.h: remove unnecessary include
[git/gitster.git] / commit-graph.c
blob5bfee53e87b75cac3e8326909476cdc16e9517e6
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "hash-lookup.h"
12 #include "commit-graph.h"
13 #include "object-file.h"
14 #include "object-store-ll.h"
15 #include "oid-array.h"
16 #include "path.h"
17 #include "alloc.h"
18 #include "hashmap.h"
19 #include "replace-object.h"
20 #include "progress.h"
21 #include "bloom.h"
22 #include "commit-slab.h"
23 #include "shallow.h"
24 #include "json-writer.h"
25 #include "trace2.h"
26 #include "tree.h"
27 #include "chunk-format.h"
29 void git_test_write_commit_graph_or_die(void)
31 int flags = 0;
32 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
33 return;
35 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
36 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
38 if (write_commit_graph_reachable(the_repository->objects->odb,
39 flags, NULL))
40 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
43 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
44 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
45 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
46 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
47 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
48 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
49 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
50 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
51 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
52 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
54 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
56 #define GRAPH_VERSION_1 0x1
57 #define GRAPH_VERSION GRAPH_VERSION_1
59 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
60 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
61 #define GRAPH_PARENT_NONE 0x70000000
63 #define GRAPH_LAST_EDGE 0x80000000
65 #define GRAPH_HEADER_SIZE 8
66 #define GRAPH_FANOUT_SIZE (4 * 256)
67 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
68 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
70 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
72 /* Remember to update object flag allocation in object.h */
73 #define REACHABLE (1u<<15)
75 define_commit_slab(topo_level_slab, uint32_t);
77 /* Keep track of the order in which commits are added to our list. */
78 define_commit_slab(commit_pos, int);
79 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
81 static void set_commit_pos(struct repository *r, const struct object_id *oid)
83 static int32_t max_pos;
84 struct commit *commit = lookup_commit(r, oid);
86 if (!commit)
87 return; /* should never happen, but be lenient */
89 *commit_pos_at(&commit_pos, commit) = max_pos++;
92 static int commit_pos_cmp(const void *va, const void *vb)
94 const struct commit *a = *(const struct commit **)va;
95 const struct commit *b = *(const struct commit **)vb;
96 return commit_pos_at(&commit_pos, a) -
97 commit_pos_at(&commit_pos, b);
100 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
101 static struct commit_graph_data_slab commit_graph_data_slab =
102 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
104 static int get_configured_generation_version(struct repository *r)
106 int version = 2;
107 repo_config_get_int(r, "commitgraph.generationversion", &version);
108 return version;
111 uint32_t commit_graph_position(const struct commit *c)
113 struct commit_graph_data *data =
114 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
116 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
119 timestamp_t commit_graph_generation(const struct commit *c)
121 struct commit_graph_data *data =
122 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
124 if (data && data->generation)
125 return data->generation;
127 return GENERATION_NUMBER_INFINITY;
130 static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
132 struct commit_graph_data *data =
133 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
135 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
136 return GENERATION_NUMBER_INFINITY;
137 return data->generation;
140 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
142 unsigned int i, nth_slab;
143 struct commit_graph_data *data =
144 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
146 if (data)
147 return data;
149 nth_slab = c->index / commit_graph_data_slab.slab_size;
150 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
153 * commit-slab initializes elements with zero, overwrite this with
154 * COMMIT_NOT_FROM_GRAPH for graph_pos.
156 * We avoid initializing generation with checking if graph position
157 * is not COMMIT_NOT_FROM_GRAPH.
159 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
160 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
161 COMMIT_NOT_FROM_GRAPH;
164 return data;
168 * Should be used only while writing commit-graph as it compares
169 * generation value of commits by directly accessing commit-slab.
171 static int commit_gen_cmp(const void *va, const void *vb)
173 const struct commit *a = *(const struct commit **)va;
174 const struct commit *b = *(const struct commit **)vb;
176 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
177 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
178 /* lower generation commits first */
179 if (generation_a < generation_b)
180 return -1;
181 else if (generation_a > generation_b)
182 return 1;
184 /* use date as a heuristic when generations are equal */
185 if (a->date < b->date)
186 return -1;
187 else if (a->date > b->date)
188 return 1;
189 return 0;
192 char *get_commit_graph_filename(struct object_directory *obj_dir)
194 return xstrfmt("%s/info/commit-graph", obj_dir->path);
197 static char *get_split_graph_filename(struct object_directory *odb,
198 const char *oid_hex)
200 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
201 oid_hex);
204 char *get_commit_graph_chain_filename(struct object_directory *odb)
206 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
209 static struct commit_graph *alloc_commit_graph(void)
211 struct commit_graph *g = xcalloc(1, sizeof(*g));
213 return g;
216 static int commit_graph_compatible(struct repository *r)
218 if (!r->gitdir)
219 return 0;
221 if (replace_refs_enabled(r)) {
222 prepare_replace_object(r);
223 if (hashmap_get_size(&r->objects->replace_map->map))
224 return 0;
227 prepare_commit_graft(r);
228 if (r->parsed_objects &&
229 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
230 return 0;
231 if (is_repository_shallow(r))
232 return 0;
234 return 1;
237 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
239 *fd = git_open(graph_file);
240 if (*fd < 0)
241 return 0;
242 if (fstat(*fd, st)) {
243 close(*fd);
244 return 0;
246 return 1;
249 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
250 int fd, struct stat *st,
251 struct object_directory *odb)
253 void *graph_map;
254 size_t graph_size;
255 struct commit_graph *ret;
257 graph_size = xsize_t(st->st_size);
259 if (graph_size < GRAPH_MIN_SIZE) {
260 close(fd);
261 error(_("commit-graph file is too small"));
262 return NULL;
264 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
265 close(fd);
266 prepare_repo_settings(r);
267 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
269 if (ret)
270 ret->odb = odb;
271 else
272 munmap(graph_map, graph_size);
274 return ret;
277 static int verify_commit_graph_lite(struct commit_graph *g)
279 int i;
282 * Basic validation shared between parse_commit_graph()
283 * which'll be called every time the graph is used, and the
284 * much more expensive verify_commit_graph() used by
285 * "commit-graph verify".
287 * There should only be very basic checks here to ensure that
288 * we don't e.g. segfault in fill_commit_in_graph(), but
289 * because this is a very hot codepath nothing that e.g. loops
290 * over g->num_commits, or runs a checksum on the commit-graph
291 * itself.
293 if (!g->chunk_oid_fanout) {
294 error("commit-graph is missing the OID Fanout chunk");
295 return 1;
297 if (!g->chunk_oid_lookup) {
298 error("commit-graph is missing the OID Lookup chunk");
299 return 1;
301 if (!g->chunk_commit_data) {
302 error("commit-graph is missing the Commit Data chunk");
303 return 1;
306 for (i = 0; i < 255; i++) {
307 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
308 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
310 if (oid_fanout1 > oid_fanout2) {
311 error("commit-graph fanout values out of order");
312 return 1;
315 if (ntohl(g->chunk_oid_fanout[255]) != g->num_commits) {
316 error("commit-graph oid table and fanout disagree on size");
317 return 1;
320 return 0;
323 static int graph_read_oid_fanout(const unsigned char *chunk_start,
324 size_t chunk_size, void *data)
326 struct commit_graph *g = data;
327 if (chunk_size != 256 * sizeof(uint32_t))
328 return error("commit-graph oid fanout chunk is wrong size");
329 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
330 return 0;
333 static int graph_read_oid_lookup(const unsigned char *chunk_start,
334 size_t chunk_size, void *data)
336 struct commit_graph *g = data;
337 g->chunk_oid_lookup = chunk_start;
338 g->num_commits = chunk_size / g->hash_len;
339 return 0;
342 static int graph_read_commit_data(const unsigned char *chunk_start,
343 size_t chunk_size, void *data)
345 struct commit_graph *g = data;
346 if (chunk_size != g->num_commits * GRAPH_DATA_WIDTH)
347 return error("commit-graph commit data chunk is wrong size");
348 g->chunk_commit_data = chunk_start;
349 return 0;
352 static int graph_read_generation_data(const unsigned char *chunk_start,
353 size_t chunk_size, void *data)
355 struct commit_graph *g = data;
356 if (chunk_size != g->num_commits * sizeof(uint32_t))
357 return error("commit-graph generations chunk is wrong size");
358 g->chunk_generation_data = chunk_start;
359 return 0;
362 static int graph_read_bloom_index(const unsigned char *chunk_start,
363 size_t chunk_size, void *data)
365 struct commit_graph *g = data;
366 if (chunk_size != g->num_commits * 4) {
367 warning("commit-graph changed-path index chunk is too small");
368 return -1;
370 g->chunk_bloom_indexes = chunk_start;
371 return 0;
374 static int graph_read_bloom_data(const unsigned char *chunk_start,
375 size_t chunk_size, void *data)
377 struct commit_graph *g = data;
378 uint32_t hash_version;
380 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
381 warning("ignoring too-small changed-path chunk"
382 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file",
383 (uintmax_t)chunk_size,
384 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
385 return -1;
388 g->chunk_bloom_data = chunk_start;
389 g->chunk_bloom_data_size = chunk_size;
390 hash_version = get_be32(chunk_start);
392 if (hash_version != 1)
393 return 0;
395 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
396 g->bloom_filter_settings->hash_version = hash_version;
397 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
398 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
399 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
401 return 0;
404 struct commit_graph *parse_commit_graph(struct repo_settings *s,
405 void *graph_map, size_t graph_size)
407 const unsigned char *data;
408 struct commit_graph *graph;
409 uint32_t graph_signature;
410 unsigned char graph_version, hash_version;
411 struct chunkfile *cf = NULL;
413 if (!graph_map)
414 return NULL;
416 if (graph_size < GRAPH_MIN_SIZE)
417 return NULL;
419 data = (const unsigned char *)graph_map;
421 graph_signature = get_be32(data);
422 if (graph_signature != GRAPH_SIGNATURE) {
423 error(_("commit-graph signature %X does not match signature %X"),
424 graph_signature, GRAPH_SIGNATURE);
425 return NULL;
428 graph_version = *(unsigned char*)(data + 4);
429 if (graph_version != GRAPH_VERSION) {
430 error(_("commit-graph version %X does not match version %X"),
431 graph_version, GRAPH_VERSION);
432 return NULL;
435 hash_version = *(unsigned char*)(data + 5);
436 if (hash_version != oid_version(the_hash_algo)) {
437 error(_("commit-graph hash version %X does not match version %X"),
438 hash_version, oid_version(the_hash_algo));
439 return NULL;
442 graph = alloc_commit_graph();
444 graph->hash_len = the_hash_algo->rawsz;
445 graph->num_chunks = *(unsigned char*)(data + 6);
446 graph->data = graph_map;
447 graph->data_len = graph_size;
449 if (graph_size < GRAPH_HEADER_SIZE +
450 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
451 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
452 error(_("commit-graph file is too small to hold %u chunks"),
453 graph->num_chunks);
454 free(graph);
455 return NULL;
458 cf = init_chunkfile(NULL);
460 if (read_table_of_contents(cf, graph->data, graph_size,
461 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
462 goto free_and_return;
464 read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph);
465 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
466 read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph);
467 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
468 &graph->chunk_extra_edges_size);
469 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
470 &graph->chunk_base_graphs_size);
472 if (s->commit_graph_generation_version >= 2) {
473 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
474 graph_read_generation_data, graph);
475 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
476 &graph->chunk_generation_data_overflow,
477 &graph->chunk_generation_data_overflow_size);
479 if (graph->chunk_generation_data)
480 graph->read_generation_data = 1;
483 if (s->commit_graph_read_changed_paths) {
484 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
485 graph_read_bloom_index, graph);
486 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
487 graph_read_bloom_data, graph);
490 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
491 init_bloom_filters();
492 } else {
493 /* We need both the bloom chunks to exist together. Else ignore the data */
494 graph->chunk_bloom_indexes = NULL;
495 graph->chunk_bloom_data = NULL;
496 FREE_AND_NULL(graph->bloom_filter_settings);
499 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
501 if (verify_commit_graph_lite(graph))
502 goto free_and_return;
504 free_chunkfile(cf);
505 return graph;
507 free_and_return:
508 free_chunkfile(cf);
509 free(graph->bloom_filter_settings);
510 free(graph);
511 return NULL;
514 static struct commit_graph *load_commit_graph_one(struct repository *r,
515 const char *graph_file,
516 struct object_directory *odb)
519 struct stat st;
520 int fd;
521 struct commit_graph *g;
522 int open_ok = open_commit_graph(graph_file, &fd, &st);
524 if (!open_ok)
525 return NULL;
527 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
529 if (g)
530 g->filename = xstrdup(graph_file);
532 return g;
535 static struct commit_graph *load_commit_graph_v1(struct repository *r,
536 struct object_directory *odb)
538 char *graph_name = get_commit_graph_filename(odb);
539 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
540 free(graph_name);
542 return g;
546 * returns 1 if and only if all graphs in the chain have
547 * corrected commit dates stored in the generation_data chunk.
549 static int validate_mixed_generation_chain(struct commit_graph *g)
551 int read_generation_data = 1;
552 struct commit_graph *p = g;
554 while (read_generation_data && p) {
555 read_generation_data = p->read_generation_data;
556 p = p->base_graph;
559 if (read_generation_data)
560 return 1;
562 while (g) {
563 g->read_generation_data = 0;
564 g = g->base_graph;
567 return 0;
570 static int add_graph_to_chain(struct commit_graph *g,
571 struct commit_graph *chain,
572 struct object_id *oids,
573 int n)
575 struct commit_graph *cur_g = chain;
577 if (n && !g->chunk_base_graphs) {
578 warning(_("commit-graph has no base graphs chunk"));
579 return 0;
582 if (g->chunk_base_graphs_size / g->hash_len < n) {
583 warning(_("commit-graph base graphs chunk is too small"));
584 return 0;
587 while (n) {
588 n--;
590 if (!cur_g ||
591 !oideq(&oids[n], &cur_g->oid) ||
592 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
593 warning(_("commit-graph chain does not match"));
594 return 0;
597 cur_g = cur_g->base_graph;
600 if (chain) {
601 if (unsigned_add_overflows(chain->num_commits,
602 chain->num_commits_in_base)) {
603 warning(_("commit count in base graph too high: %"PRIuMAX),
604 (uintmax_t)chain->num_commits_in_base);
605 return 0;
607 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
610 g->base_graph = chain;
612 return 1;
615 int open_commit_graph_chain(const char *chain_file,
616 int *fd, struct stat *st)
618 *fd = git_open(chain_file);
619 if (*fd < 0)
620 return 0;
621 if (fstat(*fd, st)) {
622 close(*fd);
623 return 0;
625 if (st->st_size < the_hash_algo->hexsz) {
626 close(*fd);
627 if (!st->st_size) {
628 /* treat empty files the same as missing */
629 errno = ENOENT;
630 } else {
631 warning("commit-graph chain file too small");
632 errno = EINVAL;
634 return 0;
636 return 1;
639 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
640 int fd, struct stat *st,
641 int *incomplete_chain)
643 struct commit_graph *graph_chain = NULL;
644 struct strbuf line = STRBUF_INIT;
645 struct object_id *oids;
646 int i = 0, valid = 1, count;
647 FILE *fp = xfdopen(fd, "r");
649 count = st->st_size / (the_hash_algo->hexsz + 1);
650 CALLOC_ARRAY(oids, count);
652 prepare_alt_odb(r);
654 for (i = 0; i < count; i++) {
655 struct object_directory *odb;
657 if (strbuf_getline_lf(&line, fp) == EOF)
658 break;
660 if (get_oid_hex(line.buf, &oids[i])) {
661 warning(_("invalid commit-graph chain: line '%s' not a hash"),
662 line.buf);
663 valid = 0;
664 break;
667 valid = 0;
668 for (odb = r->objects->odb; odb; odb = odb->next) {
669 char *graph_name = get_split_graph_filename(odb, line.buf);
670 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
672 free(graph_name);
674 if (g) {
675 if (add_graph_to_chain(g, graph_chain, oids, i)) {
676 graph_chain = g;
677 valid = 1;
678 } else {
679 free_commit_graph(g);
682 break;
686 if (!valid) {
687 warning(_("unable to find all commit-graph files"));
688 break;
692 validate_mixed_generation_chain(graph_chain);
694 free(oids);
695 fclose(fp);
696 strbuf_release(&line);
698 *incomplete_chain = !valid;
699 return graph_chain;
702 static struct commit_graph *load_commit_graph_chain(struct repository *r,
703 struct object_directory *odb)
705 char *chain_file = get_commit_graph_chain_filename(odb);
706 struct stat st;
707 int fd;
708 struct commit_graph *g = NULL;
710 if (open_commit_graph_chain(chain_file, &fd, &st)) {
711 int incomplete;
712 /* ownership of fd is taken over by load function */
713 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
716 free(chain_file);
717 return g;
720 struct commit_graph *read_commit_graph_one(struct repository *r,
721 struct object_directory *odb)
723 struct commit_graph *g = load_commit_graph_v1(r, odb);
725 if (!g)
726 g = load_commit_graph_chain(r, odb);
728 return g;
731 static void prepare_commit_graph_one(struct repository *r,
732 struct object_directory *odb)
735 if (r->objects->commit_graph)
736 return;
738 r->objects->commit_graph = read_commit_graph_one(r, odb);
742 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
744 * On the first invocation, this function attempts to load the commit
745 * graph if the_repository is configured to have one.
747 static int prepare_commit_graph(struct repository *r)
749 struct object_directory *odb;
752 * Early return if there is no git dir or if the commit graph is
753 * disabled.
755 * This must come before the "already attempted?" check below, because
756 * we want to disable even an already-loaded graph file.
758 if (!r->gitdir || r->commit_graph_disabled)
759 return 0;
761 if (r->objects->commit_graph_attempted)
762 return !!r->objects->commit_graph;
763 r->objects->commit_graph_attempted = 1;
765 prepare_repo_settings(r);
767 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
768 r->settings.core_commit_graph != 1)
770 * This repository is not configured to use commit graphs, so
771 * do not load one. (But report commit_graph_attempted anyway
772 * so that commit graph loading is not attempted again for this
773 * repository.)
775 return 0;
777 if (!commit_graph_compatible(r))
778 return 0;
780 prepare_alt_odb(r);
781 for (odb = r->objects->odb;
782 !r->objects->commit_graph && odb;
783 odb = odb->next)
784 prepare_commit_graph_one(r, odb);
785 return !!r->objects->commit_graph;
788 int generation_numbers_enabled(struct repository *r)
790 uint32_t first_generation;
791 struct commit_graph *g;
792 if (!prepare_commit_graph(r))
793 return 0;
795 g = r->objects->commit_graph;
797 if (!g->num_commits)
798 return 0;
800 first_generation = get_be32(g->chunk_commit_data +
801 g->hash_len + 8) >> 2;
803 return !!first_generation;
806 int corrected_commit_dates_enabled(struct repository *r)
808 struct commit_graph *g;
809 if (!prepare_commit_graph(r))
810 return 0;
812 g = r->objects->commit_graph;
814 if (!g->num_commits)
815 return 0;
817 return g->read_generation_data;
820 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
822 struct commit_graph *g = r->objects->commit_graph;
823 while (g) {
824 if (g->bloom_filter_settings)
825 return g->bloom_filter_settings;
826 g = g->base_graph;
828 return NULL;
831 void close_commit_graph(struct raw_object_store *o)
833 clear_commit_graph_data_slab(&commit_graph_data_slab);
834 free_commit_graph(o->commit_graph);
835 o->commit_graph = NULL;
838 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
840 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
841 g->chunk_oid_lookup, g->hash_len, pos);
844 static void load_oid_from_graph(struct commit_graph *g,
845 uint32_t pos,
846 struct object_id *oid)
848 uint32_t lex_index;
850 while (g && pos < g->num_commits_in_base)
851 g = g->base_graph;
853 if (!g)
854 BUG("NULL commit-graph");
856 if (pos >= g->num_commits + g->num_commits_in_base)
857 die(_("invalid commit position. commit-graph is likely corrupt"));
859 lex_index = pos - g->num_commits_in_base;
861 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
864 static struct commit_list **insert_parent_or_die(struct repository *r,
865 struct commit_graph *g,
866 uint32_t pos,
867 struct commit_list **pptr)
869 struct commit *c;
870 struct object_id oid;
872 if (pos >= g->num_commits + g->num_commits_in_base)
873 die("invalid parent position %"PRIu32, pos);
875 load_oid_from_graph(g, pos, &oid);
876 c = lookup_commit(r, &oid);
877 if (!c)
878 die(_("could not find commit %s"), oid_to_hex(&oid));
879 commit_graph_data_at(c)->graph_pos = pos;
880 return &commit_list_insert(c, pptr)->next;
883 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
885 const unsigned char *commit_data;
886 struct commit_graph_data *graph_data;
887 uint32_t lex_index, offset_pos;
888 uint64_t date_high, date_low, offset;
890 while (pos < g->num_commits_in_base)
891 g = g->base_graph;
893 if (pos >= g->num_commits + g->num_commits_in_base)
894 die(_("invalid commit position. commit-graph is likely corrupt"));
896 lex_index = pos - g->num_commits_in_base;
897 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
899 graph_data = commit_graph_data_at(item);
900 graph_data->graph_pos = pos;
902 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
903 date_low = get_be32(commit_data + g->hash_len + 12);
904 item->date = (timestamp_t)((date_high << 32) | date_low);
906 if (g->read_generation_data) {
907 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
909 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
910 if (!g->chunk_generation_data_overflow)
911 die(_("commit-graph requires overflow generation data but has none"));
913 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
914 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
915 die(_("commit-graph overflow generation data is too small"));
916 graph_data->generation = item->date +
917 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
918 } else
919 graph_data->generation = item->date + offset;
920 } else
921 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
923 if (g->topo_levels)
924 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
927 static inline void set_commit_tree(struct commit *c, struct tree *t)
929 c->maybe_tree = t;
932 static int fill_commit_in_graph(struct repository *r,
933 struct commit *item,
934 struct commit_graph *g, uint32_t pos)
936 uint32_t edge_value;
937 uint32_t parent_data_pos;
938 struct commit_list **pptr;
939 const unsigned char *commit_data;
940 uint32_t lex_index;
942 while (pos < g->num_commits_in_base)
943 g = g->base_graph;
945 fill_commit_graph_info(item, g, pos);
947 lex_index = pos - g->num_commits_in_base;
948 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
950 item->object.parsed = 1;
952 set_commit_tree(item, NULL);
954 pptr = &item->parents;
956 edge_value = get_be32(commit_data + g->hash_len);
957 if (edge_value == GRAPH_PARENT_NONE)
958 return 1;
959 pptr = insert_parent_or_die(r, g, edge_value, pptr);
961 edge_value = get_be32(commit_data + g->hash_len + 4);
962 if (edge_value == GRAPH_PARENT_NONE)
963 return 1;
964 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
965 pptr = insert_parent_or_die(r, g, edge_value, pptr);
966 return 1;
969 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
970 do {
971 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
972 error("commit-graph extra-edges pointer out of bounds");
973 free_commit_list(item->parents);
974 item->parents = NULL;
975 item->object.parsed = 0;
976 return 0;
978 edge_value = get_be32(g->chunk_extra_edges +
979 sizeof(uint32_t) * parent_data_pos);
980 pptr = insert_parent_or_die(r, g,
981 edge_value & GRAPH_EDGE_LAST_MASK,
982 pptr);
983 parent_data_pos++;
984 } while (!(edge_value & GRAPH_LAST_EDGE));
986 return 1;
989 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
991 struct commit_graph *cur_g = g;
992 uint32_t lex_index;
994 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
995 cur_g = cur_g->base_graph;
997 if (cur_g) {
998 *pos = lex_index + cur_g->num_commits_in_base;
999 return 1;
1002 return 0;
1005 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
1007 uint32_t graph_pos = commit_graph_position(item);
1008 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
1009 *pos = graph_pos;
1010 return 1;
1011 } else {
1012 return search_commit_pos_in_graph(&item->object.oid, g, pos);
1016 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
1017 uint32_t *pos)
1019 if (!prepare_commit_graph(r))
1020 return 0;
1021 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
1024 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1026 static int commit_graph_paranoia = -1;
1027 struct commit *commit;
1028 uint32_t pos;
1030 if (commit_graph_paranoia == -1)
1031 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 1);
1033 if (!prepare_commit_graph(repo))
1034 return NULL;
1035 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
1036 return NULL;
1037 if (commit_graph_paranoia && !has_object(repo, id, 0))
1038 return NULL;
1040 commit = lookup_commit(repo, id);
1041 if (!commit)
1042 return NULL;
1043 if (commit->object.parsed)
1044 return commit;
1046 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
1047 return NULL;
1049 return commit;
1052 static int parse_commit_in_graph_one(struct repository *r,
1053 struct commit_graph *g,
1054 struct commit *item)
1056 uint32_t pos;
1058 if (item->object.parsed)
1059 return 1;
1061 if (find_commit_pos_in_graph(item, g, &pos))
1062 return fill_commit_in_graph(r, item, g, pos);
1064 return 0;
1067 int parse_commit_in_graph(struct repository *r, struct commit *item)
1069 static int checked_env = 0;
1071 if (!checked_env &&
1072 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1073 die("dying as requested by the '%s' variable on commit-graph parse!",
1074 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1075 checked_env = 1;
1077 if (!prepare_commit_graph(r))
1078 return 0;
1079 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1082 void load_commit_graph_info(struct repository *r, struct commit *item)
1084 uint32_t pos;
1085 if (repo_find_commit_pos_in_graph(r, item, &pos))
1086 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1089 static struct tree *load_tree_for_commit(struct repository *r,
1090 struct commit_graph *g,
1091 struct commit *c)
1093 struct object_id oid;
1094 const unsigned char *commit_data;
1095 uint32_t graph_pos = commit_graph_position(c);
1097 while (graph_pos < g->num_commits_in_base)
1098 g = g->base_graph;
1100 commit_data = g->chunk_commit_data +
1101 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1103 oidread(&oid, commit_data);
1104 set_commit_tree(c, lookup_tree(r, &oid));
1106 return c->maybe_tree;
1109 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1110 struct commit_graph *g,
1111 const struct commit *c)
1113 if (c->maybe_tree)
1114 return c->maybe_tree;
1115 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1116 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1118 return load_tree_for_commit(r, g, (struct commit *)c);
1121 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1123 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1126 struct packed_commit_list {
1127 struct commit **list;
1128 size_t nr;
1129 size_t alloc;
1132 struct write_commit_graph_context {
1133 struct repository *r;
1134 struct object_directory *odb;
1135 char *graph_name;
1136 struct oid_array oids;
1137 struct packed_commit_list commits;
1138 int num_extra_edges;
1139 int num_generation_data_overflows;
1140 unsigned long approx_nr_objects;
1141 struct progress *progress;
1142 int progress_done;
1143 uint64_t progress_cnt;
1145 char *base_graph_name;
1146 int num_commit_graphs_before;
1147 int num_commit_graphs_after;
1148 char **commit_graph_filenames_before;
1149 char **commit_graph_filenames_after;
1150 char **commit_graph_hash_after;
1151 uint32_t new_num_commits_in_base;
1152 struct commit_graph *new_base_graph;
1154 unsigned append:1,
1155 report_progress:1,
1156 split:1,
1157 changed_paths:1,
1158 order_by_pack:1,
1159 write_generation_data:1,
1160 trust_generation_numbers:1;
1162 struct topo_level_slab *topo_levels;
1163 const struct commit_graph_opts *opts;
1164 size_t total_bloom_filter_data_size;
1165 const struct bloom_filter_settings *bloom_settings;
1167 int count_bloom_filter_computed;
1168 int count_bloom_filter_not_computed;
1169 int count_bloom_filter_trunc_empty;
1170 int count_bloom_filter_trunc_large;
1173 static int write_graph_chunk_fanout(struct hashfile *f,
1174 void *data)
1176 struct write_commit_graph_context *ctx = data;
1177 int i, count = 0;
1178 struct commit **list = ctx->commits.list;
1181 * Write the first-level table (the list is sorted,
1182 * but we use a 256-entry lookup to be able to avoid
1183 * having to do eight extra binary search iterations).
1185 for (i = 0; i < 256; i++) {
1186 while (count < ctx->commits.nr) {
1187 if ((*list)->object.oid.hash[0] != i)
1188 break;
1189 display_progress(ctx->progress, ++ctx->progress_cnt);
1190 count++;
1191 list++;
1194 hashwrite_be32(f, count);
1197 return 0;
1200 static int write_graph_chunk_oids(struct hashfile *f,
1201 void *data)
1203 struct write_commit_graph_context *ctx = data;
1204 struct commit **list = ctx->commits.list;
1205 int count;
1206 for (count = 0; count < ctx->commits.nr; count++, list++) {
1207 display_progress(ctx->progress, ++ctx->progress_cnt);
1208 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1211 return 0;
1214 static const struct object_id *commit_to_oid(size_t index, const void *table)
1216 const struct commit * const *commits = table;
1217 return &commits[index]->object.oid;
1220 static int write_graph_chunk_data(struct hashfile *f,
1221 void *data)
1223 struct write_commit_graph_context *ctx = data;
1224 struct commit **list = ctx->commits.list;
1225 struct commit **last = ctx->commits.list + ctx->commits.nr;
1226 uint32_t num_extra_edges = 0;
1228 while (list < last) {
1229 struct commit_list *parent;
1230 struct object_id *tree;
1231 int edge_value;
1232 uint32_t packedDate[2];
1233 display_progress(ctx->progress, ++ctx->progress_cnt);
1235 if (repo_parse_commit_no_graph(ctx->r, *list))
1236 die(_("unable to parse commit %s"),
1237 oid_to_hex(&(*list)->object.oid));
1238 tree = get_commit_tree_oid(*list);
1239 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1241 parent = (*list)->parents;
1243 if (!parent)
1244 edge_value = GRAPH_PARENT_NONE;
1245 else {
1246 edge_value = oid_pos(&parent->item->object.oid,
1247 ctx->commits.list,
1248 ctx->commits.nr,
1249 commit_to_oid);
1251 if (edge_value >= 0)
1252 edge_value += ctx->new_num_commits_in_base;
1253 else if (ctx->new_base_graph) {
1254 uint32_t pos;
1255 if (find_commit_pos_in_graph(parent->item,
1256 ctx->new_base_graph,
1257 &pos))
1258 edge_value = pos;
1261 if (edge_value < 0)
1262 BUG("missing parent %s for commit %s",
1263 oid_to_hex(&parent->item->object.oid),
1264 oid_to_hex(&(*list)->object.oid));
1267 hashwrite_be32(f, edge_value);
1269 if (parent)
1270 parent = parent->next;
1272 if (!parent)
1273 edge_value = GRAPH_PARENT_NONE;
1274 else if (parent->next)
1275 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1276 else {
1277 edge_value = oid_pos(&parent->item->object.oid,
1278 ctx->commits.list,
1279 ctx->commits.nr,
1280 commit_to_oid);
1282 if (edge_value >= 0)
1283 edge_value += ctx->new_num_commits_in_base;
1284 else if (ctx->new_base_graph) {
1285 uint32_t pos;
1286 if (find_commit_pos_in_graph(parent->item,
1287 ctx->new_base_graph,
1288 &pos))
1289 edge_value = pos;
1292 if (edge_value < 0)
1293 BUG("missing parent %s for commit %s",
1294 oid_to_hex(&parent->item->object.oid),
1295 oid_to_hex(&(*list)->object.oid));
1298 hashwrite_be32(f, edge_value);
1300 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1301 do {
1302 num_extra_edges++;
1303 parent = parent->next;
1304 } while (parent);
1307 if (sizeof((*list)->date) > 4)
1308 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1309 else
1310 packedDate[0] = 0;
1312 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1314 packedDate[1] = htonl((*list)->date);
1315 hashwrite(f, packedDate, 8);
1317 list++;
1320 return 0;
1323 static int write_graph_chunk_generation_data(struct hashfile *f,
1324 void *data)
1326 struct write_commit_graph_context *ctx = data;
1327 int i, num_generation_data_overflows = 0;
1329 for (i = 0; i < ctx->commits.nr; i++) {
1330 struct commit *c = ctx->commits.list[i];
1331 timestamp_t offset;
1332 repo_parse_commit(ctx->r, c);
1333 offset = commit_graph_data_at(c)->generation - c->date;
1334 display_progress(ctx->progress, ++ctx->progress_cnt);
1336 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1337 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1338 num_generation_data_overflows++;
1341 hashwrite_be32(f, offset);
1344 return 0;
1347 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1348 void *data)
1350 struct write_commit_graph_context *ctx = data;
1351 int i;
1352 for (i = 0; i < ctx->commits.nr; i++) {
1353 struct commit *c = ctx->commits.list[i];
1354 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1355 display_progress(ctx->progress, ++ctx->progress_cnt);
1357 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1358 hashwrite_be32(f, offset >> 32);
1359 hashwrite_be32(f, (uint32_t) offset);
1363 return 0;
1366 static int write_graph_chunk_extra_edges(struct hashfile *f,
1367 void *data)
1369 struct write_commit_graph_context *ctx = data;
1370 struct commit **list = ctx->commits.list;
1371 struct commit **last = ctx->commits.list + ctx->commits.nr;
1372 struct commit_list *parent;
1374 while (list < last) {
1375 int num_parents = 0;
1377 display_progress(ctx->progress, ++ctx->progress_cnt);
1379 for (parent = (*list)->parents; num_parents < 3 && parent;
1380 parent = parent->next)
1381 num_parents++;
1383 if (num_parents <= 2) {
1384 list++;
1385 continue;
1388 /* Since num_parents > 2, this initializer is safe. */
1389 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1390 int edge_value = oid_pos(&parent->item->object.oid,
1391 ctx->commits.list,
1392 ctx->commits.nr,
1393 commit_to_oid);
1395 if (edge_value >= 0)
1396 edge_value += ctx->new_num_commits_in_base;
1397 else if (ctx->new_base_graph) {
1398 uint32_t pos;
1399 if (find_commit_pos_in_graph(parent->item,
1400 ctx->new_base_graph,
1401 &pos))
1402 edge_value = pos;
1405 if (edge_value < 0)
1406 BUG("missing parent %s for commit %s",
1407 oid_to_hex(&parent->item->object.oid),
1408 oid_to_hex(&(*list)->object.oid));
1409 else if (!parent->next)
1410 edge_value |= GRAPH_LAST_EDGE;
1412 hashwrite_be32(f, edge_value);
1415 list++;
1418 return 0;
1421 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1422 void *data)
1424 struct write_commit_graph_context *ctx = data;
1425 struct commit **list = ctx->commits.list;
1426 struct commit **last = ctx->commits.list + ctx->commits.nr;
1427 uint32_t cur_pos = 0;
1429 while (list < last) {
1430 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1431 size_t len = filter ? filter->len : 0;
1432 cur_pos += len;
1433 display_progress(ctx->progress, ++ctx->progress_cnt);
1434 hashwrite_be32(f, cur_pos);
1435 list++;
1438 return 0;
1441 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1443 struct json_writer jw = JSON_WRITER_INIT;
1445 jw_object_begin(&jw, 0);
1446 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1447 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1448 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1449 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1450 jw_end(&jw);
1452 trace2_data_json("bloom", ctx->r, "settings", &jw);
1454 jw_release(&jw);
1457 static int write_graph_chunk_bloom_data(struct hashfile *f,
1458 void *data)
1460 struct write_commit_graph_context *ctx = data;
1461 struct commit **list = ctx->commits.list;
1462 struct commit **last = ctx->commits.list + ctx->commits.nr;
1464 trace2_bloom_filter_settings(ctx);
1466 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1467 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1468 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1470 while (list < last) {
1471 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1472 size_t len = filter ? filter->len : 0;
1474 display_progress(ctx->progress, ++ctx->progress_cnt);
1475 if (len)
1476 hashwrite(f, filter->data, len * sizeof(unsigned char));
1477 list++;
1480 return 0;
1483 static int add_packed_commits(const struct object_id *oid,
1484 struct packed_git *pack,
1485 uint32_t pos,
1486 void *data)
1488 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1489 enum object_type type;
1490 off_t offset = nth_packed_object_offset(pack, pos);
1491 struct object_info oi = OBJECT_INFO_INIT;
1493 if (ctx->progress)
1494 display_progress(ctx->progress, ++ctx->progress_done);
1496 oi.typep = &type;
1497 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1498 die(_("unable to get type of object %s"), oid_to_hex(oid));
1500 if (type != OBJ_COMMIT)
1501 return 0;
1503 oid_array_append(&ctx->oids, oid);
1504 set_commit_pos(ctx->r, oid);
1506 return 0;
1509 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1511 struct commit_list *parent;
1512 for (parent = commit->parents; parent; parent = parent->next) {
1513 if (!(parent->item->object.flags & REACHABLE)) {
1514 oid_array_append(&ctx->oids, &parent->item->object.oid);
1515 parent->item->object.flags |= REACHABLE;
1520 static void close_reachable(struct write_commit_graph_context *ctx)
1522 int i;
1523 struct commit *commit;
1524 enum commit_graph_split_flags flags = ctx->opts ?
1525 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1527 if (ctx->report_progress)
1528 ctx->progress = start_delayed_progress(
1529 _("Loading known commits in commit graph"),
1530 ctx->oids.nr);
1531 for (i = 0; i < ctx->oids.nr; i++) {
1532 display_progress(ctx->progress, i + 1);
1533 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1534 if (commit)
1535 commit->object.flags |= REACHABLE;
1537 stop_progress(&ctx->progress);
1540 * As this loop runs, ctx->oids.nr may grow, but not more
1541 * than the number of missing commits in the reachable
1542 * closure.
1544 if (ctx->report_progress)
1545 ctx->progress = start_delayed_progress(
1546 _("Expanding reachable commits in commit graph"),
1548 for (i = 0; i < ctx->oids.nr; i++) {
1549 display_progress(ctx->progress, i + 1);
1550 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1552 if (!commit)
1553 continue;
1554 if (ctx->split) {
1555 if ((!repo_parse_commit(ctx->r, commit) &&
1556 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1557 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1558 add_missing_parents(ctx, commit);
1559 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1560 add_missing_parents(ctx, commit);
1562 stop_progress(&ctx->progress);
1564 if (ctx->report_progress)
1565 ctx->progress = start_delayed_progress(
1566 _("Clearing commit marks in commit graph"),
1567 ctx->oids.nr);
1568 for (i = 0; i < ctx->oids.nr; i++) {
1569 display_progress(ctx->progress, i + 1);
1570 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1572 if (commit)
1573 commit->object.flags &= ~REACHABLE;
1575 stop_progress(&ctx->progress);
1578 struct compute_generation_info {
1579 struct repository *r;
1580 struct packed_commit_list *commits;
1581 struct progress *progress;
1582 int progress_cnt;
1584 timestamp_t (*get_generation)(struct commit *c, void *data);
1585 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1586 void *data;
1589 static timestamp_t compute_generation_from_max(struct commit *c,
1590 timestamp_t max_gen,
1591 int generation_version)
1593 switch (generation_version) {
1594 case 1: /* topological levels */
1595 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1596 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1597 return max_gen + 1;
1599 case 2: /* corrected commit date */
1600 if (c->date && c->date > max_gen)
1601 max_gen = c->date - 1;
1602 return max_gen + 1;
1604 default:
1605 BUG("attempting unimplemented version");
1609 static void compute_reachable_generation_numbers(
1610 struct compute_generation_info *info,
1611 int generation_version)
1613 int i;
1614 struct commit_list *list = NULL;
1616 for (i = 0; i < info->commits->nr; i++) {
1617 struct commit *c = info->commits->list[i];
1618 timestamp_t gen;
1619 repo_parse_commit(info->r, c);
1620 gen = info->get_generation(c, info->data);
1621 display_progress(info->progress, info->progress_cnt + 1);
1623 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1624 continue;
1626 commit_list_insert(c, &list);
1627 while (list) {
1628 struct commit *current = list->item;
1629 struct commit_list *parent;
1630 int all_parents_computed = 1;
1631 uint32_t max_gen = 0;
1633 for (parent = current->parents; parent; parent = parent->next) {
1634 repo_parse_commit(info->r, parent->item);
1635 gen = info->get_generation(parent->item, info->data);
1637 if (gen == GENERATION_NUMBER_ZERO) {
1638 all_parents_computed = 0;
1639 commit_list_insert(parent->item, &list);
1640 break;
1643 if (gen > max_gen)
1644 max_gen = gen;
1647 if (all_parents_computed) {
1648 pop_commit(&list);
1649 gen = compute_generation_from_max(
1650 current, max_gen,
1651 generation_version);
1652 info->set_generation(current, gen, info->data);
1658 static timestamp_t get_topo_level(struct commit *c, void *data)
1660 struct write_commit_graph_context *ctx = data;
1661 return *topo_level_slab_at(ctx->topo_levels, c);
1664 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1666 struct write_commit_graph_context *ctx = data;
1667 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1670 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1672 struct compute_generation_info info = {
1673 .r = ctx->r,
1674 .commits = &ctx->commits,
1675 .get_generation = get_topo_level,
1676 .set_generation = set_topo_level,
1677 .data = ctx,
1680 if (ctx->report_progress)
1681 info.progress = ctx->progress
1682 = start_delayed_progress(
1683 _("Computing commit graph topological levels"),
1684 ctx->commits.nr);
1686 compute_reachable_generation_numbers(&info, 1);
1688 stop_progress(&ctx->progress);
1691 static timestamp_t get_generation_from_graph_data(struct commit *c,
1692 void *data UNUSED)
1694 return commit_graph_data_at(c)->generation;
1697 static void set_generation_v2(struct commit *c, timestamp_t t,
1698 void *data UNUSED)
1700 struct commit_graph_data *g = commit_graph_data_at(c);
1701 g->generation = t;
1704 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1706 int i;
1707 struct compute_generation_info info = {
1708 .r = ctx->r,
1709 .commits = &ctx->commits,
1710 .get_generation = get_generation_from_graph_data,
1711 .set_generation = set_generation_v2,
1714 if (ctx->report_progress)
1715 info.progress = ctx->progress
1716 = start_delayed_progress(
1717 _("Computing commit graph generation numbers"),
1718 ctx->commits.nr);
1720 if (!ctx->trust_generation_numbers) {
1721 for (i = 0; i < ctx->commits.nr; i++) {
1722 struct commit *c = ctx->commits.list[i];
1723 repo_parse_commit(ctx->r, c);
1724 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1728 compute_reachable_generation_numbers(&info, 2);
1730 for (i = 0; i < ctx->commits.nr; i++) {
1731 struct commit *c = ctx->commits.list[i];
1732 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1733 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1734 ctx->num_generation_data_overflows++;
1736 stop_progress(&ctx->progress);
1739 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1740 void *data UNUSED)
1742 commit_graph_data_at(c)->generation = t;
1746 * After this method, all commits reachable from those in the given
1747 * list will have non-zero, non-infinite generation numbers.
1749 void ensure_generations_valid(struct repository *r,
1750 struct commit **commits, size_t nr)
1752 int generation_version = get_configured_generation_version(r);
1753 struct packed_commit_list list = {
1754 .list = commits,
1755 .alloc = nr,
1756 .nr = nr,
1758 struct compute_generation_info info = {
1759 .r = r,
1760 .commits = &list,
1761 .get_generation = get_generation_from_graph_data,
1762 .set_generation = set_generation_in_graph_data,
1765 compute_reachable_generation_numbers(&info, generation_version);
1768 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1770 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1771 ctx->count_bloom_filter_computed);
1772 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1773 ctx->count_bloom_filter_not_computed);
1774 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1775 ctx->count_bloom_filter_trunc_empty);
1776 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1777 ctx->count_bloom_filter_trunc_large);
1780 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1782 int i;
1783 struct progress *progress = NULL;
1784 struct commit **sorted_commits;
1785 int max_new_filters;
1787 init_bloom_filters();
1789 if (ctx->report_progress)
1790 progress = start_delayed_progress(
1791 _("Computing commit changed paths Bloom filters"),
1792 ctx->commits.nr);
1794 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1796 if (ctx->order_by_pack)
1797 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1798 else
1799 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1801 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1802 ctx->opts->max_new_filters : ctx->commits.nr;
1804 for (i = 0; i < ctx->commits.nr; i++) {
1805 enum bloom_filter_computed computed = 0;
1806 struct commit *c = sorted_commits[i];
1807 struct bloom_filter *filter = get_or_compute_bloom_filter(
1808 ctx->r,
1810 ctx->count_bloom_filter_computed < max_new_filters,
1811 ctx->bloom_settings,
1812 &computed);
1813 if (computed & BLOOM_COMPUTED) {
1814 ctx->count_bloom_filter_computed++;
1815 if (computed & BLOOM_TRUNC_EMPTY)
1816 ctx->count_bloom_filter_trunc_empty++;
1817 if (computed & BLOOM_TRUNC_LARGE)
1818 ctx->count_bloom_filter_trunc_large++;
1819 } else if (computed & BLOOM_NOT_COMPUTED)
1820 ctx->count_bloom_filter_not_computed++;
1821 ctx->total_bloom_filter_data_size += filter
1822 ? sizeof(unsigned char) * filter->len : 0;
1823 display_progress(progress, i + 1);
1826 if (trace2_is_enabled())
1827 trace2_bloom_filter_write_statistics(ctx);
1829 free(sorted_commits);
1830 stop_progress(&progress);
1833 struct refs_cb_data {
1834 struct oidset *commits;
1835 struct progress *progress;
1838 static int add_ref_to_set(const char *refname UNUSED,
1839 const struct object_id *oid,
1840 int flags UNUSED, void *cb_data)
1842 struct object_id peeled;
1843 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1845 if (!peel_iterated_oid(oid, &peeled))
1846 oid = &peeled;
1847 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1848 oidset_insert(data->commits, oid);
1850 display_progress(data->progress, oidset_size(data->commits));
1852 return 0;
1855 int write_commit_graph_reachable(struct object_directory *odb,
1856 enum commit_graph_write_flags flags,
1857 const struct commit_graph_opts *opts)
1859 struct oidset commits = OIDSET_INIT;
1860 struct refs_cb_data data;
1861 int result;
1863 memset(&data, 0, sizeof(data));
1864 data.commits = &commits;
1865 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1866 data.progress = start_delayed_progress(
1867 _("Collecting referenced commits"), 0);
1869 for_each_ref(add_ref_to_set, &data);
1871 stop_progress(&data.progress);
1873 result = write_commit_graph(odb, NULL, &commits,
1874 flags, opts);
1876 oidset_clear(&commits);
1877 return result;
1880 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1881 const struct string_list *pack_indexes)
1883 uint32_t i;
1884 struct strbuf progress_title = STRBUF_INIT;
1885 struct strbuf packname = STRBUF_INIT;
1886 int dirlen;
1887 int ret = 0;
1889 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1890 dirlen = packname.len;
1891 if (ctx->report_progress) {
1892 strbuf_addf(&progress_title,
1893 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1894 "Finding commits for commit graph in %"PRIuMAX" packs",
1895 pack_indexes->nr),
1896 (uintmax_t)pack_indexes->nr);
1897 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1898 ctx->progress_done = 0;
1900 for (i = 0; i < pack_indexes->nr; i++) {
1901 struct packed_git *p;
1902 strbuf_setlen(&packname, dirlen);
1903 strbuf_addstr(&packname, pack_indexes->items[i].string);
1904 p = add_packed_git(packname.buf, packname.len, 1);
1905 if (!p) {
1906 ret = error(_("error adding pack %s"), packname.buf);
1907 goto cleanup;
1909 if (open_pack_index(p)) {
1910 ret = error(_("error opening index for %s"), packname.buf);
1911 goto cleanup;
1913 for_each_object_in_pack(p, add_packed_commits, ctx,
1914 FOR_EACH_OBJECT_PACK_ORDER);
1915 close_pack(p);
1916 free(p);
1919 cleanup:
1920 stop_progress(&ctx->progress);
1921 strbuf_release(&progress_title);
1922 strbuf_release(&packname);
1924 return ret;
1927 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1928 struct oidset *commits)
1930 struct oidset_iter iter;
1931 struct object_id *oid;
1933 if (!oidset_size(commits))
1934 return 0;
1936 oidset_iter_init(commits, &iter);
1937 while ((oid = oidset_iter_next(&iter))) {
1938 oid_array_append(&ctx->oids, oid);
1941 return 0;
1944 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1946 if (ctx->report_progress)
1947 ctx->progress = start_delayed_progress(
1948 _("Finding commits for commit graph among packed objects"),
1949 ctx->approx_nr_objects);
1950 for_each_packed_object(add_packed_commits, ctx,
1951 FOR_EACH_OBJECT_PACK_ORDER);
1952 if (ctx->progress_done < ctx->approx_nr_objects)
1953 display_progress(ctx->progress, ctx->approx_nr_objects);
1954 stop_progress(&ctx->progress);
1957 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1959 uint32_t i;
1960 enum commit_graph_split_flags flags = ctx->opts ?
1961 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1963 ctx->num_extra_edges = 0;
1964 if (ctx->report_progress)
1965 ctx->progress = start_delayed_progress(
1966 _("Finding extra edges in commit graph"),
1967 ctx->oids.nr);
1968 oid_array_sort(&ctx->oids);
1969 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1970 unsigned int num_parents;
1972 display_progress(ctx->progress, i + 1);
1974 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1975 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1977 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1978 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1979 continue;
1981 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1982 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1983 else
1984 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1986 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1987 if (num_parents > 2)
1988 ctx->num_extra_edges += num_parents - 1;
1990 ctx->commits.nr++;
1992 stop_progress(&ctx->progress);
1995 static int write_graph_chunk_base_1(struct hashfile *f,
1996 struct commit_graph *g)
1998 int num = 0;
2000 if (!g)
2001 return 0;
2003 num = write_graph_chunk_base_1(f, g->base_graph);
2004 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
2005 return num + 1;
2008 static int write_graph_chunk_base(struct hashfile *f,
2009 void *data)
2011 struct write_commit_graph_context *ctx = data;
2012 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
2014 if (num != ctx->num_commit_graphs_after - 1) {
2015 error(_("failed to write correct number of base graph ids"));
2016 return -1;
2019 return 0;
2022 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2024 uint32_t i;
2025 int fd;
2026 struct hashfile *f;
2027 struct lock_file lk = LOCK_INIT;
2028 const unsigned hashsz = the_hash_algo->rawsz;
2029 struct strbuf progress_title = STRBUF_INIT;
2030 struct chunkfile *cf;
2031 unsigned char file_hash[GIT_MAX_RAWSZ];
2033 if (ctx->split) {
2034 struct strbuf tmp_file = STRBUF_INIT;
2036 strbuf_addf(&tmp_file,
2037 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2038 ctx->odb->path);
2039 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2040 } else {
2041 ctx->graph_name = get_commit_graph_filename(ctx->odb);
2044 if (safe_create_leading_directories(ctx->graph_name)) {
2045 UNLEAK(ctx->graph_name);
2046 error(_("unable to create leading directories of %s"),
2047 ctx->graph_name);
2048 return -1;
2051 if (ctx->split) {
2052 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
2054 hold_lock_file_for_update_mode(&lk, lock_name,
2055 LOCK_DIE_ON_ERROR, 0444);
2056 free(lock_name);
2058 fd = git_mkstemp_mode(ctx->graph_name, 0444);
2059 if (fd < 0) {
2060 error(_("unable to create temporary graph layer"));
2061 return -1;
2064 if (adjust_shared_perm(ctx->graph_name)) {
2065 error(_("unable to adjust shared permissions for '%s'"),
2066 ctx->graph_name);
2067 return -1;
2070 f = hashfd(fd, ctx->graph_name);
2071 } else {
2072 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
2073 LOCK_DIE_ON_ERROR, 0444);
2074 fd = get_lock_file_fd(&lk);
2075 f = hashfd(fd, get_lock_file_path(&lk));
2078 cf = init_chunkfile(f);
2080 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2081 write_graph_chunk_fanout);
2082 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2083 write_graph_chunk_oids);
2084 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2085 write_graph_chunk_data);
2087 if (ctx->write_generation_data)
2088 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2089 st_mult(sizeof(uint32_t), ctx->commits.nr),
2090 write_graph_chunk_generation_data);
2091 if (ctx->num_generation_data_overflows)
2092 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2093 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2094 write_graph_chunk_generation_data_overflow);
2095 if (ctx->num_extra_edges)
2096 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2097 st_mult(4, ctx->num_extra_edges),
2098 write_graph_chunk_extra_edges);
2099 if (ctx->changed_paths) {
2100 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2101 st_mult(sizeof(uint32_t), ctx->commits.nr),
2102 write_graph_chunk_bloom_indexes);
2103 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2104 st_add(sizeof(uint32_t) * 3,
2105 ctx->total_bloom_filter_data_size),
2106 write_graph_chunk_bloom_data);
2108 if (ctx->num_commit_graphs_after > 1)
2109 add_chunk(cf, GRAPH_CHUNKID_BASE,
2110 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2111 write_graph_chunk_base);
2113 hashwrite_be32(f, GRAPH_SIGNATURE);
2115 hashwrite_u8(f, GRAPH_VERSION);
2116 hashwrite_u8(f, oid_version(the_hash_algo));
2117 hashwrite_u8(f, get_num_chunks(cf));
2118 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2120 if (ctx->report_progress) {
2121 strbuf_addf(&progress_title,
2122 Q_("Writing out commit graph in %d pass",
2123 "Writing out commit graph in %d passes",
2124 get_num_chunks(cf)),
2125 get_num_chunks(cf));
2126 ctx->progress = start_delayed_progress(
2127 progress_title.buf,
2128 st_mult(get_num_chunks(cf), ctx->commits.nr));
2131 write_chunkfile(cf, ctx);
2133 stop_progress(&ctx->progress);
2134 strbuf_release(&progress_title);
2136 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2137 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2138 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2140 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2141 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2142 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2143 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2146 close_commit_graph(ctx->r->objects);
2147 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2148 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2149 free_chunkfile(cf);
2151 if (ctx->split) {
2152 FILE *chainf = fdopen_lock_file(&lk, "w");
2153 char *final_graph_name;
2154 int result;
2156 close(fd);
2158 if (!chainf) {
2159 error(_("unable to open commit-graph chain file"));
2160 return -1;
2163 if (ctx->base_graph_name) {
2164 const char *dest;
2165 int idx = ctx->num_commit_graphs_after - 1;
2166 if (ctx->num_commit_graphs_after > 1)
2167 idx--;
2169 dest = ctx->commit_graph_filenames_after[idx];
2171 if (strcmp(ctx->base_graph_name, dest)) {
2172 result = rename(ctx->base_graph_name, dest);
2174 if (result) {
2175 error(_("failed to rename base commit-graph file"));
2176 return -1;
2179 } else {
2180 char *graph_name = get_commit_graph_filename(ctx->odb);
2181 unlink(graph_name);
2182 free(graph_name);
2185 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2186 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2187 final_graph_name = get_split_graph_filename(ctx->odb,
2188 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2189 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2190 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2192 result = rename(ctx->graph_name, final_graph_name);
2194 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2195 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2197 if (result) {
2198 error(_("failed to rename temporary commit-graph file"));
2199 return -1;
2203 commit_lock_file(&lk);
2205 return 0;
2208 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2210 struct commit_graph *g;
2211 uint32_t num_commits;
2212 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2213 uint32_t i;
2215 int max_commits = 0;
2216 int size_mult = 2;
2218 if (ctx->opts) {
2219 max_commits = ctx->opts->max_commits;
2221 if (ctx->opts->size_multiple)
2222 size_mult = ctx->opts->size_multiple;
2224 flags = ctx->opts->split_flags;
2227 g = ctx->r->objects->commit_graph;
2228 num_commits = ctx->commits.nr;
2229 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2230 ctx->num_commit_graphs_after = 1;
2231 else
2232 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2234 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2235 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2236 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2237 (max_commits && num_commits > max_commits))) {
2238 if (g->odb != ctx->odb)
2239 break;
2241 if (unsigned_add_overflows(num_commits, g->num_commits))
2242 die(_("cannot merge graphs with %"PRIuMAX", "
2243 "%"PRIuMAX" commits"),
2244 (uintmax_t)num_commits,
2245 (uintmax_t)g->num_commits);
2246 num_commits += g->num_commits;
2247 g = g->base_graph;
2249 ctx->num_commit_graphs_after--;
2253 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2254 ctx->new_base_graph = g;
2255 else if (ctx->num_commit_graphs_after != 1)
2256 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2257 "should be 1 with --split=replace");
2259 if (ctx->num_commit_graphs_after == 2) {
2260 char *old_graph_name = get_commit_graph_filename(g->odb);
2262 if (!strcmp(g->filename, old_graph_name) &&
2263 g->odb != ctx->odb) {
2264 ctx->num_commit_graphs_after = 1;
2265 ctx->new_base_graph = NULL;
2268 free(old_graph_name);
2271 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2272 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2274 for (i = 0; i < ctx->num_commit_graphs_after &&
2275 i < ctx->num_commit_graphs_before; i++)
2276 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2278 i = ctx->num_commit_graphs_before - 1;
2279 g = ctx->r->objects->commit_graph;
2281 while (g) {
2282 if (i < ctx->num_commit_graphs_after)
2283 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2286 * If the topmost remaining layer has generation data chunk, the
2287 * resultant layer also has generation data chunk.
2289 if (i == ctx->num_commit_graphs_after - 2)
2290 ctx->write_generation_data = !!g->chunk_generation_data;
2292 i--;
2293 g = g->base_graph;
2297 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2298 struct commit_graph *g)
2300 uint32_t i;
2301 uint32_t offset = g->num_commits_in_base;
2303 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2304 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2305 oid_to_hex(&g->oid),
2306 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2308 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2310 for (i = 0; i < g->num_commits; i++) {
2311 struct object_id oid;
2312 struct commit *result;
2314 display_progress(ctx->progress, i + 1);
2316 load_oid_from_graph(g, i + offset, &oid);
2318 /* only add commits if they still exist in the repo */
2319 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2321 if (result) {
2322 ctx->commits.list[ctx->commits.nr] = result;
2323 ctx->commits.nr++;
2328 static int commit_compare(const void *_a, const void *_b)
2330 const struct commit *a = *(const struct commit **)_a;
2331 const struct commit *b = *(const struct commit **)_b;
2332 return oidcmp(&a->object.oid, &b->object.oid);
2335 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2337 uint32_t i, dedup_i = 0;
2339 if (ctx->report_progress)
2340 ctx->progress = start_delayed_progress(
2341 _("Scanning merged commits"),
2342 ctx->commits.nr);
2344 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2346 ctx->num_extra_edges = 0;
2347 for (i = 0; i < ctx->commits.nr; i++) {
2348 display_progress(ctx->progress, i + 1);
2350 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2351 &ctx->commits.list[i]->object.oid)) {
2353 * Silently ignore duplicates. These were likely
2354 * created due to a commit appearing in multiple
2355 * layers of the chain, which is unexpected but
2356 * not invalid. We should make sure there is a
2357 * unique copy in the new layer.
2359 } else {
2360 unsigned int num_parents;
2362 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2363 dedup_i++;
2365 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2366 if (num_parents > 2)
2367 ctx->num_extra_edges += num_parents - 1;
2371 ctx->commits.nr = dedup_i;
2373 stop_progress(&ctx->progress);
2376 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2378 struct commit_graph *g = ctx->r->objects->commit_graph;
2379 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2381 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2382 current_graph_number--;
2384 if (ctx->report_progress)
2385 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2387 merge_commit_graph(ctx, g);
2388 stop_progress(&ctx->progress);
2390 g = g->base_graph;
2393 if (g) {
2394 ctx->new_base_graph = g;
2395 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2398 if (ctx->new_base_graph)
2399 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2401 sort_and_scan_merged_commits(ctx);
2404 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2406 uint32_t i;
2407 time_t now = time(NULL);
2409 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2410 struct stat st;
2411 struct utimbuf updated_time;
2413 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2414 continue;
2416 updated_time.actime = st.st_atime;
2417 updated_time.modtime = now;
2418 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2422 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2424 struct strbuf path = STRBUF_INIT;
2425 DIR *dir;
2426 struct dirent *de;
2427 size_t dirnamelen;
2428 timestamp_t expire_time = time(NULL);
2430 if (ctx->opts && ctx->opts->expire_time)
2431 expire_time = ctx->opts->expire_time;
2432 if (!ctx->split) {
2433 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2434 unlink(chain_file_name);
2435 free(chain_file_name);
2436 ctx->num_commit_graphs_after = 0;
2439 strbuf_addstr(&path, ctx->odb->path);
2440 strbuf_addstr(&path, "/info/commit-graphs");
2441 dir = opendir(path.buf);
2443 if (!dir)
2444 goto out;
2446 strbuf_addch(&path, '/');
2447 dirnamelen = path.len;
2448 while ((de = readdir(dir)) != NULL) {
2449 struct stat st;
2450 uint32_t i, found = 0;
2452 strbuf_setlen(&path, dirnamelen);
2453 strbuf_addstr(&path, de->d_name);
2455 if (stat(path.buf, &st) < 0)
2456 continue;
2458 if (st.st_mtime > expire_time)
2459 continue;
2460 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2461 continue;
2463 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2464 if (!strcmp(ctx->commit_graph_filenames_after[i],
2465 path.buf)) {
2466 found = 1;
2467 break;
2471 if (!found)
2472 unlink(path.buf);
2475 out:
2476 if(dir)
2477 closedir(dir);
2478 strbuf_release(&path);
2481 int write_commit_graph(struct object_directory *odb,
2482 const struct string_list *const pack_indexes,
2483 struct oidset *commits,
2484 enum commit_graph_write_flags flags,
2485 const struct commit_graph_opts *opts)
2487 struct repository *r = the_repository;
2488 struct write_commit_graph_context *ctx;
2489 uint32_t i;
2490 int res = 0;
2491 int replace = 0;
2492 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2493 struct topo_level_slab topo_levels;
2495 prepare_repo_settings(r);
2496 if (!r->settings.core_commit_graph) {
2497 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2498 return 0;
2500 if (!commit_graph_compatible(r))
2501 return 0;
2503 CALLOC_ARRAY(ctx, 1);
2504 ctx->r = r;
2505 ctx->odb = odb;
2506 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2507 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2508 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2509 ctx->opts = opts;
2510 ctx->total_bloom_filter_data_size = 0;
2511 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2512 ctx->num_generation_data_overflows = 0;
2514 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2515 bloom_settings.bits_per_entry);
2516 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2517 bloom_settings.num_hashes);
2518 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2519 bloom_settings.max_changed_paths);
2520 ctx->bloom_settings = &bloom_settings;
2522 init_topo_level_slab(&topo_levels);
2523 ctx->topo_levels = &topo_levels;
2525 prepare_commit_graph(ctx->r);
2526 if (ctx->r->objects->commit_graph) {
2527 struct commit_graph *g = ctx->r->objects->commit_graph;
2529 while (g) {
2530 g->topo_levels = &topo_levels;
2531 g = g->base_graph;
2535 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2536 ctx->changed_paths = 1;
2537 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2538 struct commit_graph *g;
2540 g = ctx->r->objects->commit_graph;
2542 /* We have changed-paths already. Keep them in the next graph */
2543 if (g && g->chunk_bloom_data) {
2544 ctx->changed_paths = 1;
2545 ctx->bloom_settings = g->bloom_filter_settings;
2549 if (ctx->split) {
2550 struct commit_graph *g = ctx->r->objects->commit_graph;
2552 while (g) {
2553 ctx->num_commit_graphs_before++;
2554 g = g->base_graph;
2557 if (ctx->num_commit_graphs_before) {
2558 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2559 i = ctx->num_commit_graphs_before;
2560 g = ctx->r->objects->commit_graph;
2562 while (g) {
2563 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2564 g = g->base_graph;
2568 if (ctx->opts)
2569 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2572 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2574 if (ctx->append && ctx->r->objects->commit_graph) {
2575 struct commit_graph *g = ctx->r->objects->commit_graph;
2576 for (i = 0; i < g->num_commits; i++) {
2577 struct object_id oid;
2578 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2579 oid_array_append(&ctx->oids, &oid);
2583 if (pack_indexes) {
2584 ctx->order_by_pack = 1;
2585 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2586 goto cleanup;
2589 if (commits) {
2590 if ((res = fill_oids_from_commits(ctx, commits)))
2591 goto cleanup;
2594 if (!pack_indexes && !commits) {
2595 ctx->order_by_pack = 1;
2596 fill_oids_from_all_packs(ctx);
2599 close_reachable(ctx);
2601 copy_oids_to_commits(ctx);
2603 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2604 error(_("too many commits to write graph"));
2605 res = -1;
2606 goto cleanup;
2609 if (!ctx->commits.nr && !replace)
2610 goto cleanup;
2612 if (ctx->split) {
2613 split_graph_merge_strategy(ctx);
2615 if (!replace)
2616 merge_commit_graphs(ctx);
2617 } else
2618 ctx->num_commit_graphs_after = 1;
2620 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2622 compute_topological_levels(ctx);
2623 if (ctx->write_generation_data)
2624 compute_generation_numbers(ctx);
2626 if (ctx->changed_paths)
2627 compute_bloom_filters(ctx);
2629 res = write_commit_graph_file(ctx);
2631 if (ctx->split)
2632 mark_commit_graphs(ctx);
2634 expire_commit_graphs(ctx);
2636 cleanup:
2637 free(ctx->graph_name);
2638 free(ctx->base_graph_name);
2639 free(ctx->commits.list);
2640 oid_array_clear(&ctx->oids);
2641 clear_topo_level_slab(&topo_levels);
2643 if (ctx->commit_graph_filenames_after) {
2644 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2645 free(ctx->commit_graph_filenames_after[i]);
2646 free(ctx->commit_graph_hash_after[i]);
2649 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2650 free(ctx->commit_graph_filenames_before[i]);
2652 free(ctx->commit_graph_filenames_after);
2653 free(ctx->commit_graph_filenames_before);
2654 free(ctx->commit_graph_hash_after);
2657 free(ctx);
2659 return res;
2662 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2663 static int verify_commit_graph_error;
2665 __attribute__((format (printf, 1, 2)))
2666 static void graph_report(const char *fmt, ...)
2668 va_list ap;
2670 verify_commit_graph_error = 1;
2671 va_start(ap, fmt);
2672 vfprintf(stderr, fmt, ap);
2673 fprintf(stderr, "\n");
2674 va_end(ap);
2677 static int commit_graph_checksum_valid(struct commit_graph *g)
2679 return hashfile_checksum_valid(g->data, g->data_len);
2682 static int verify_one_commit_graph(struct repository *r,
2683 struct commit_graph *g,
2684 struct progress *progress,
2685 uint64_t *seen)
2687 uint32_t i, cur_fanout_pos = 0;
2688 struct object_id prev_oid, cur_oid;
2689 struct commit *seen_gen_zero = NULL;
2690 struct commit *seen_gen_non_zero = NULL;
2692 verify_commit_graph_error = verify_commit_graph_lite(g);
2693 if (verify_commit_graph_error)
2694 return verify_commit_graph_error;
2696 if (!commit_graph_checksum_valid(g)) {
2697 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2698 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2701 for (i = 0; i < g->num_commits; i++) {
2702 struct commit *graph_commit;
2704 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2706 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2707 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2708 oid_to_hex(&prev_oid),
2709 oid_to_hex(&cur_oid));
2711 oidcpy(&prev_oid, &cur_oid);
2713 while (cur_oid.hash[0] > cur_fanout_pos) {
2714 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2716 if (i != fanout_value)
2717 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2718 cur_fanout_pos, fanout_value, i);
2719 cur_fanout_pos++;
2722 graph_commit = lookup_commit(r, &cur_oid);
2723 if (!parse_commit_in_graph_one(r, g, graph_commit))
2724 graph_report(_("failed to parse commit %s from commit-graph"),
2725 oid_to_hex(&cur_oid));
2728 while (cur_fanout_pos < 256) {
2729 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2731 if (g->num_commits != fanout_value)
2732 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2733 cur_fanout_pos, fanout_value, i);
2735 cur_fanout_pos++;
2738 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2739 return verify_commit_graph_error;
2741 for (i = 0; i < g->num_commits; i++) {
2742 struct commit *graph_commit, *odb_commit;
2743 struct commit_list *graph_parents, *odb_parents;
2744 timestamp_t max_generation = 0;
2745 timestamp_t generation;
2747 display_progress(progress, ++(*seen));
2748 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2750 graph_commit = lookup_commit(r, &cur_oid);
2751 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2752 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2753 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2754 oid_to_hex(&cur_oid));
2755 continue;
2758 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2759 get_commit_tree_oid(odb_commit)))
2760 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2761 oid_to_hex(&cur_oid),
2762 oid_to_hex(get_commit_tree_oid(graph_commit)),
2763 oid_to_hex(get_commit_tree_oid(odb_commit)));
2765 graph_parents = graph_commit->parents;
2766 odb_parents = odb_commit->parents;
2768 while (graph_parents) {
2769 if (!odb_parents) {
2770 graph_report(_("commit-graph parent list for commit %s is too long"),
2771 oid_to_hex(&cur_oid));
2772 break;
2775 /* parse parent in case it is in a base graph */
2776 parse_commit_in_graph_one(r, g, graph_parents->item);
2778 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2779 graph_report(_("commit-graph parent for %s is %s != %s"),
2780 oid_to_hex(&cur_oid),
2781 oid_to_hex(&graph_parents->item->object.oid),
2782 oid_to_hex(&odb_parents->item->object.oid));
2784 generation = commit_graph_generation_from_graph(graph_parents->item);
2785 if (generation > max_generation)
2786 max_generation = generation;
2788 graph_parents = graph_parents->next;
2789 odb_parents = odb_parents->next;
2792 if (odb_parents)
2793 graph_report(_("commit-graph parent list for commit %s terminates early"),
2794 oid_to_hex(&cur_oid));
2796 if (commit_graph_generation_from_graph(graph_commit))
2797 seen_gen_non_zero = graph_commit;
2798 else
2799 seen_gen_zero = graph_commit;
2801 if (seen_gen_zero)
2802 continue;
2805 * If we are using topological level and one of our parents has
2806 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2807 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2808 * in the following condition.
2810 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2811 max_generation--;
2813 generation = commit_graph_generation(graph_commit);
2814 if (generation < max_generation + 1)
2815 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2816 oid_to_hex(&cur_oid),
2817 generation,
2818 max_generation + 1);
2820 if (graph_commit->date != odb_commit->date)
2821 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2822 oid_to_hex(&cur_oid),
2823 graph_commit->date,
2824 odb_commit->date);
2827 if (seen_gen_zero && seen_gen_non_zero)
2828 graph_report(_("commit-graph has both zero and non-zero "
2829 "generations (e.g., commits '%s' and '%s')"),
2830 oid_to_hex(&seen_gen_zero->object.oid),
2831 oid_to_hex(&seen_gen_non_zero->object.oid));
2833 return verify_commit_graph_error;
2836 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2838 struct progress *progress = NULL;
2839 int local_error = 0;
2840 uint64_t seen = 0;
2842 if (!g) {
2843 graph_report("no commit-graph file loaded");
2844 return 1;
2847 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2848 uint64_t total = g->num_commits;
2849 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2850 total += g->num_commits_in_base;
2852 progress = start_progress(_("Verifying commits in commit graph"),
2853 total);
2856 for (; g; g = g->base_graph) {
2857 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2858 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2859 break;
2862 stop_progress(&progress);
2864 return local_error;
2867 void free_commit_graph(struct commit_graph *g)
2869 while (g) {
2870 struct commit_graph *next = g->base_graph;
2872 if (g->data)
2873 munmap((void *)g->data, g->data_len);
2874 free(g->filename);
2875 free(g->bloom_filter_settings);
2876 free(g);
2878 g = next;
2882 void disable_commit_graph(struct repository *r)
2884 r->commit_graph_disabled = 1;