1 #include "git-compat-util.h"
10 #include "hash-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
15 #include "replace-object.h"
18 #include "commit-slab.h"
20 #include "json-writer.h"
22 #include "chunk-format.h"
24 void git_test_write_commit_graph_or_die(void)
27 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
30 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
31 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
33 if (write_commit_graph_reachable(the_repository->objects->odb,
35 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
38 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
39 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
40 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
41 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
42 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444154 /* "GDAT" */
43 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f56 /* "GDOV" */
44 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
45 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
46 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
47 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
49 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
51 #define GRAPH_VERSION_1 0x1
52 #define GRAPH_VERSION GRAPH_VERSION_1
54 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
55 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
56 #define GRAPH_PARENT_NONE 0x70000000
58 #define GRAPH_LAST_EDGE 0x80000000
60 #define GRAPH_HEADER_SIZE 8
61 #define GRAPH_FANOUT_SIZE (4 * 256)
62 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
63 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
65 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
67 /* Remember to update object flag allocation in object.h */
68 #define REACHABLE (1u<<15)
70 define_commit_slab(topo_level_slab, uint32_t);
72 /* Keep track of the order in which commits are added to our list. */
73 define_commit_slab(commit_pos, int);
74 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
76 static void set_commit_pos(struct repository *r, const struct object_id *oid)
78 static int32_t max_pos;
79 struct commit *commit = lookup_commit(r, oid);
82 return; /* should never happen, but be lenient */
84 *commit_pos_at(&commit_pos, commit) = max_pos++;
87 static int commit_pos_cmp(const void *va, const void *vb)
89 const struct commit *a = *(const struct commit **)va;
90 const struct commit *b = *(const struct commit **)vb;
91 return commit_pos_at(&commit_pos, a) -
92 commit_pos_at(&commit_pos, b);
95 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
96 static struct commit_graph_data_slab commit_graph_data_slab =
97 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
99 uint32_t commit_graph_position(const struct commit *c)
101 struct commit_graph_data *data =
102 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
104 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
107 timestamp_t commit_graph_generation(const struct commit *c)
109 struct commit_graph_data *data =
110 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
113 return GENERATION_NUMBER_INFINITY;
114 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
115 return GENERATION_NUMBER_INFINITY;
117 return data->generation;
120 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
122 unsigned int i, nth_slab;
123 struct commit_graph_data *data =
124 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
129 nth_slab = c->index / commit_graph_data_slab.slab_size;
130 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
133 * commit-slab initializes elements with zero, overwrite this with
134 * COMMIT_NOT_FROM_GRAPH for graph_pos.
136 * We avoid initializing generation with checking if graph position
137 * is not COMMIT_NOT_FROM_GRAPH.
139 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
140 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
141 COMMIT_NOT_FROM_GRAPH;
148 * Should be used only while writing commit-graph as it compares
149 * generation value of commits by directly accessing commit-slab.
151 static int commit_gen_cmp(const void *va, const void *vb)
153 const struct commit *a = *(const struct commit **)va;
154 const struct commit *b = *(const struct commit **)vb;
156 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
157 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
158 /* lower generation commits first */
159 if (generation_a < generation_b)
161 else if (generation_a > generation_b)
164 /* use date as a heuristic when generations are equal */
165 if (a->date < b->date)
167 else if (a->date > b->date)
172 char *get_commit_graph_filename(struct object_directory *obj_dir)
174 return xstrfmt("%s/info/commit-graph", obj_dir->path);
177 static char *get_split_graph_filename(struct object_directory *odb,
180 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
184 char *get_commit_graph_chain_filename(struct object_directory *odb)
186 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
189 static uint8_t oid_version(void)
191 switch (hash_algo_by_ptr(the_hash_algo)) {
194 case GIT_HASH_SHA256:
197 die(_("invalid hash version"));
201 static struct commit_graph *alloc_commit_graph(void)
203 struct commit_graph *g = xcalloc(1, sizeof(*g));
208 extern int read_replace_refs;
210 static int commit_graph_compatible(struct repository *r)
215 if (read_replace_refs) {
216 prepare_replace_object(r);
217 if (hashmap_get_size(&r->objects->replace_map->map))
221 prepare_commit_graft(r);
222 if (r->parsed_objects &&
223 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
225 if (is_repository_shallow(r))
231 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
233 *fd = git_open(graph_file);
236 if (fstat(*fd, st)) {
243 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
244 int fd, struct stat *st,
245 struct object_directory *odb)
249 struct commit_graph *ret;
251 graph_size = xsize_t(st->st_size);
253 if (graph_size < GRAPH_MIN_SIZE) {
255 error(_("commit-graph file is too small"));
258 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
260 ret = parse_commit_graph(r, graph_map, graph_size);
265 munmap(graph_map, graph_size);
270 static int verify_commit_graph_lite(struct commit_graph *g)
273 * Basic validation shared between parse_commit_graph()
274 * which'll be called every time the graph is used, and the
275 * much more expensive verify_commit_graph() used by
276 * "commit-graph verify".
278 * There should only be very basic checks here to ensure that
279 * we don't e.g. segfault in fill_commit_in_graph(), but
280 * because this is a very hot codepath nothing that e.g. loops
281 * over g->num_commits, or runs a checksum on the commit-graph
284 if (!g->chunk_oid_fanout) {
285 error("commit-graph is missing the OID Fanout chunk");
288 if (!g->chunk_oid_lookup) {
289 error("commit-graph is missing the OID Lookup chunk");
292 if (!g->chunk_commit_data) {
293 error("commit-graph is missing the Commit Data chunk");
300 static int graph_read_oid_lookup(const unsigned char *chunk_start,
301 size_t chunk_size, void *data)
303 struct commit_graph *g = data;
304 g->chunk_oid_lookup = chunk_start;
305 g->num_commits = chunk_size / g->hash_len;
309 static int graph_read_bloom_data(const unsigned char *chunk_start,
310 size_t chunk_size, void *data)
312 struct commit_graph *g = data;
313 uint32_t hash_version;
314 g->chunk_bloom_data = chunk_start;
315 hash_version = get_be32(chunk_start);
317 if (hash_version != 1)
320 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
321 g->bloom_filter_settings->hash_version = hash_version;
322 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
323 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
324 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
329 struct commit_graph *parse_commit_graph(struct repository *r,
330 void *graph_map, size_t graph_size)
332 const unsigned char *data;
333 struct commit_graph *graph;
334 uint32_t graph_signature;
335 unsigned char graph_version, hash_version;
336 struct chunkfile *cf = NULL;
341 if (graph_size < GRAPH_MIN_SIZE)
344 data = (const unsigned char *)graph_map;
346 graph_signature = get_be32(data);
347 if (graph_signature != GRAPH_SIGNATURE) {
348 error(_("commit-graph signature %X does not match signature %X"),
349 graph_signature, GRAPH_SIGNATURE);
353 graph_version = *(unsigned char*)(data + 4);
354 if (graph_version != GRAPH_VERSION) {
355 error(_("commit-graph version %X does not match version %X"),
356 graph_version, GRAPH_VERSION);
360 hash_version = *(unsigned char*)(data + 5);
361 if (hash_version != oid_version()) {
362 error(_("commit-graph hash version %X does not match version %X"),
363 hash_version, oid_version());
367 prepare_repo_settings(r);
369 graph = alloc_commit_graph();
371 graph->hash_len = the_hash_algo->rawsz;
372 graph->num_chunks = *(unsigned char*)(data + 6);
373 graph->data = graph_map;
374 graph->data_len = graph_size;
376 if (graph_size < GRAPH_HEADER_SIZE +
377 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
378 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
379 error(_("commit-graph file is too small to hold %u chunks"),
385 cf = init_chunkfile(NULL);
387 if (read_table_of_contents(cf, graph->data, graph_size,
388 GRAPH_HEADER_SIZE, graph->num_chunks))
389 goto free_and_return;
391 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
392 (const unsigned char **)&graph->chunk_oid_fanout);
393 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
394 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
395 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
396 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
397 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
398 &graph->chunk_generation_data);
399 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
400 &graph->chunk_generation_data_overflow);
402 if (r->settings.commit_graph_read_changed_paths) {
403 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
404 &graph->chunk_bloom_indexes);
405 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
406 graph_read_bloom_data, graph);
409 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
410 init_bloom_filters();
412 /* We need both the bloom chunks to exist together. Else ignore the data */
413 graph->chunk_bloom_indexes = NULL;
414 graph->chunk_bloom_data = NULL;
415 FREE_AND_NULL(graph->bloom_filter_settings);
418 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
420 if (verify_commit_graph_lite(graph))
421 goto free_and_return;
428 free(graph->bloom_filter_settings);
433 static struct commit_graph *load_commit_graph_one(struct repository *r,
434 const char *graph_file,
435 struct object_directory *odb)
440 struct commit_graph *g;
441 int open_ok = open_commit_graph(graph_file, &fd, &st);
446 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
449 g->filename = xstrdup(graph_file);
454 static struct commit_graph *load_commit_graph_v1(struct repository *r,
455 struct object_directory *odb)
457 char *graph_name = get_commit_graph_filename(odb);
458 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
464 static int add_graph_to_chain(struct commit_graph *g,
465 struct commit_graph *chain,
466 struct object_id *oids,
469 struct commit_graph *cur_g = chain;
471 if (n && !g->chunk_base_graphs) {
472 warning(_("commit-graph has no base graphs chunk"));
480 !oideq(&oids[n], &cur_g->oid) ||
481 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
482 warning(_("commit-graph chain does not match"));
486 cur_g = cur_g->base_graph;
489 g->base_graph = chain;
492 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
497 static struct commit_graph *load_commit_graph_chain(struct repository *r,
498 struct object_directory *odb)
500 struct commit_graph *graph_chain = NULL;
501 struct strbuf line = STRBUF_INIT;
503 struct object_id *oids;
504 int i = 0, valid = 1, count;
505 char *chain_name = get_commit_graph_chain_filename(odb);
509 fp = fopen(chain_name, "r");
510 stat_res = stat(chain_name, &st);
515 st.st_size <= the_hash_algo->hexsz)
518 count = st.st_size / (the_hash_algo->hexsz + 1);
519 CALLOC_ARRAY(oids, count);
523 for (i = 0; i < count; i++) {
524 struct object_directory *odb;
526 if (strbuf_getline_lf(&line, fp) == EOF)
529 if (get_oid_hex(line.buf, &oids[i])) {
530 warning(_("invalid commit-graph chain: line '%s' not a hash"),
537 for (odb = r->objects->odb; odb; odb = odb->next) {
538 char *graph_name = get_split_graph_filename(odb, line.buf);
539 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
544 if (add_graph_to_chain(g, graph_chain, oids, i)) {
554 warning(_("unable to find all commit-graph files"));
561 strbuf_release(&line);
567 * returns 1 if and only if all graphs in the chain have
568 * corrected commit dates stored in the generation_data chunk.
570 static int validate_mixed_generation_chain(struct commit_graph *g)
572 int read_generation_data = 1;
573 struct commit_graph *p = g;
575 while (read_generation_data && p) {
576 read_generation_data = p->read_generation_data;
580 if (read_generation_data)
584 g->read_generation_data = 0;
591 struct commit_graph *read_commit_graph_one(struct repository *r,
592 struct object_directory *odb)
594 struct commit_graph *g = load_commit_graph_v1(r, odb);
597 g = load_commit_graph_chain(r, odb);
599 validate_mixed_generation_chain(g);
604 static void prepare_commit_graph_one(struct repository *r,
605 struct object_directory *odb)
608 if (r->objects->commit_graph)
611 r->objects->commit_graph = read_commit_graph_one(r, odb);
615 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
617 * On the first invocation, this function attempts to load the commit
618 * graph if the_repository is configured to have one.
620 static int prepare_commit_graph(struct repository *r)
622 struct object_directory *odb;
625 * This must come before the "already attempted?" check below, because
626 * we want to disable even an already-loaded graph file.
628 if (r->commit_graph_disabled)
631 if (r->objects->commit_graph_attempted)
632 return !!r->objects->commit_graph;
633 r->objects->commit_graph_attempted = 1;
635 prepare_repo_settings(r);
637 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
638 r->settings.core_commit_graph != 1)
640 * This repository is not configured to use commit graphs, so
641 * do not load one. (But report commit_graph_attempted anyway
642 * so that commit graph loading is not attempted again for this
647 if (!commit_graph_compatible(r))
651 for (odb = r->objects->odb;
652 !r->objects->commit_graph && odb;
654 prepare_commit_graph_one(r, odb);
655 return !!r->objects->commit_graph;
658 int generation_numbers_enabled(struct repository *r)
660 uint32_t first_generation;
661 struct commit_graph *g;
662 if (!prepare_commit_graph(r))
665 g = r->objects->commit_graph;
670 first_generation = get_be32(g->chunk_commit_data +
671 g->hash_len + 8) >> 2;
673 return !!first_generation;
676 int corrected_commit_dates_enabled(struct repository *r)
678 struct commit_graph *g;
679 if (!prepare_commit_graph(r))
682 g = r->objects->commit_graph;
687 return g->read_generation_data;
690 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
692 struct commit_graph *g = r->objects->commit_graph;
694 if (g->bloom_filter_settings)
695 return g->bloom_filter_settings;
701 static void close_commit_graph_one(struct commit_graph *g)
706 close_commit_graph_one(g->base_graph);
707 free_commit_graph(g);
710 void close_commit_graph(struct raw_object_store *o)
712 close_commit_graph_one(o->commit_graph);
713 o->commit_graph = NULL;
716 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
718 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
719 g->chunk_oid_lookup, g->hash_len, pos);
722 static void load_oid_from_graph(struct commit_graph *g,
724 struct object_id *oid)
728 while (g && pos < g->num_commits_in_base)
732 BUG("NULL commit-graph");
734 if (pos >= g->num_commits + g->num_commits_in_base)
735 die(_("invalid commit position. commit-graph is likely corrupt"));
737 lex_index = pos - g->num_commits_in_base;
739 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
742 static struct commit_list **insert_parent_or_die(struct repository *r,
743 struct commit_graph *g,
745 struct commit_list **pptr)
748 struct object_id oid;
750 if (pos >= g->num_commits + g->num_commits_in_base)
751 die("invalid parent position %"PRIu32, pos);
753 load_oid_from_graph(g, pos, &oid);
754 c = lookup_commit(r, &oid);
756 die(_("could not find commit %s"), oid_to_hex(&oid));
757 commit_graph_data_at(c)->graph_pos = pos;
758 return &commit_list_insert(c, pptr)->next;
761 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
763 const unsigned char *commit_data;
764 struct commit_graph_data *graph_data;
765 uint32_t lex_index, offset_pos;
766 uint64_t date_high, date_low, offset;
768 while (pos < g->num_commits_in_base)
771 if (pos >= g->num_commits + g->num_commits_in_base)
772 die(_("invalid commit position. commit-graph is likely corrupt"));
774 lex_index = pos - g->num_commits_in_base;
775 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
777 graph_data = commit_graph_data_at(item);
778 graph_data->graph_pos = pos;
780 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
781 date_low = get_be32(commit_data + g->hash_len + 12);
782 item->date = (timestamp_t)((date_high << 32) | date_low);
784 if (g->read_generation_data) {
785 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
787 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
788 if (!g->chunk_generation_data_overflow)
789 die(_("commit-graph requires overflow generation data but has none"));
791 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
792 graph_data->generation = get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
794 graph_data->generation = item->date + offset;
796 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
799 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
802 static inline void set_commit_tree(struct commit *c, struct tree *t)
807 static int fill_commit_in_graph(struct repository *r,
809 struct commit_graph *g, uint32_t pos)
812 uint32_t *parent_data_ptr;
813 struct commit_list **pptr;
814 const unsigned char *commit_data;
817 while (pos < g->num_commits_in_base)
820 fill_commit_graph_info(item, g, pos);
822 lex_index = pos - g->num_commits_in_base;
823 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
825 item->object.parsed = 1;
827 set_commit_tree(item, NULL);
829 pptr = &item->parents;
831 edge_value = get_be32(commit_data + g->hash_len);
832 if (edge_value == GRAPH_PARENT_NONE)
834 pptr = insert_parent_or_die(r, g, edge_value, pptr);
836 edge_value = get_be32(commit_data + g->hash_len + 4);
837 if (edge_value == GRAPH_PARENT_NONE)
839 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
840 pptr = insert_parent_or_die(r, g, edge_value, pptr);
844 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
845 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
847 edge_value = get_be32(parent_data_ptr);
848 pptr = insert_parent_or_die(r, g,
849 edge_value & GRAPH_EDGE_LAST_MASK,
852 } while (!(edge_value & GRAPH_LAST_EDGE));
857 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
859 uint32_t graph_pos = commit_graph_position(item);
860 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
864 struct commit_graph *cur_g = g;
867 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
868 cur_g = cur_g->base_graph;
871 *pos = lex_index + cur_g->num_commits_in_base;
879 static int parse_commit_in_graph_one(struct repository *r,
880 struct commit_graph *g,
885 if (item->object.parsed)
888 if (find_commit_in_graph(item, g, &pos))
889 return fill_commit_in_graph(r, item, g, pos);
894 int parse_commit_in_graph(struct repository *r, struct commit *item)
896 static int checked_env = 0;
899 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
900 die("dying as requested by the '%s' variable on commit-graph parse!",
901 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
904 if (!prepare_commit_graph(r))
906 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
909 void load_commit_graph_info(struct repository *r, struct commit *item)
912 if (!prepare_commit_graph(r))
914 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
915 fill_commit_graph_info(item, r->objects->commit_graph, pos);
918 static struct tree *load_tree_for_commit(struct repository *r,
919 struct commit_graph *g,
922 struct object_id oid;
923 const unsigned char *commit_data;
924 uint32_t graph_pos = commit_graph_position(c);
926 while (graph_pos < g->num_commits_in_base)
929 commit_data = g->chunk_commit_data +
930 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
932 hashcpy(oid.hash, commit_data);
933 set_commit_tree(c, lookup_tree(r, &oid));
935 return c->maybe_tree;
938 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
939 struct commit_graph *g,
940 const struct commit *c)
943 return c->maybe_tree;
944 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
945 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
947 return load_tree_for_commit(r, g, (struct commit *)c);
950 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
952 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
955 struct packed_commit_list {
956 struct commit **list;
961 struct write_commit_graph_context {
962 struct repository *r;
963 struct object_directory *odb;
965 struct oid_array oids;
966 struct packed_commit_list commits;
968 int num_generation_data_overflows;
969 unsigned long approx_nr_objects;
970 struct progress *progress;
972 uint64_t progress_cnt;
974 char *base_graph_name;
975 int num_commit_graphs_before;
976 int num_commit_graphs_after;
977 char **commit_graph_filenames_before;
978 char **commit_graph_filenames_after;
979 char **commit_graph_hash_after;
980 uint32_t new_num_commits_in_base;
981 struct commit_graph *new_base_graph;
988 write_generation_data:1,
989 trust_generation_numbers:1;
991 struct topo_level_slab *topo_levels;
992 const struct commit_graph_opts *opts;
993 size_t total_bloom_filter_data_size;
994 const struct bloom_filter_settings *bloom_settings;
996 int count_bloom_filter_computed;
997 int count_bloom_filter_not_computed;
998 int count_bloom_filter_trunc_empty;
999 int count_bloom_filter_trunc_large;
1002 static int write_graph_chunk_fanout(struct hashfile *f,
1005 struct write_commit_graph_context *ctx = data;
1007 struct commit **list = ctx->commits.list;
1010 * Write the first-level table (the list is sorted,
1011 * but we use a 256-entry lookup to be able to avoid
1012 * having to do eight extra binary search iterations).
1014 for (i = 0; i < 256; i++) {
1015 while (count < ctx->commits.nr) {
1016 if ((*list)->object.oid.hash[0] != i)
1018 display_progress(ctx->progress, ++ctx->progress_cnt);
1023 hashwrite_be32(f, count);
1029 static int write_graph_chunk_oids(struct hashfile *f,
1032 struct write_commit_graph_context *ctx = data;
1033 struct commit **list = ctx->commits.list;
1035 for (count = 0; count < ctx->commits.nr; count++, list++) {
1036 display_progress(ctx->progress, ++ctx->progress_cnt);
1037 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1043 static const struct object_id *commit_to_oid(size_t index, const void *table)
1045 const struct commit * const *commits = table;
1046 return &commits[index]->object.oid;
1049 static int write_graph_chunk_data(struct hashfile *f,
1052 struct write_commit_graph_context *ctx = data;
1053 struct commit **list = ctx->commits.list;
1054 struct commit **last = ctx->commits.list + ctx->commits.nr;
1055 uint32_t num_extra_edges = 0;
1057 while (list < last) {
1058 struct commit_list *parent;
1059 struct object_id *tree;
1061 uint32_t packedDate[2];
1062 display_progress(ctx->progress, ++ctx->progress_cnt);
1064 if (repo_parse_commit_no_graph(ctx->r, *list))
1065 die(_("unable to parse commit %s"),
1066 oid_to_hex(&(*list)->object.oid));
1067 tree = get_commit_tree_oid(*list);
1068 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1070 parent = (*list)->parents;
1073 edge_value = GRAPH_PARENT_NONE;
1075 edge_value = oid_pos(&parent->item->object.oid,
1080 if (edge_value >= 0)
1081 edge_value += ctx->new_num_commits_in_base;
1082 else if (ctx->new_base_graph) {
1084 if (find_commit_in_graph(parent->item,
1085 ctx->new_base_graph,
1091 BUG("missing parent %s for commit %s",
1092 oid_to_hex(&parent->item->object.oid),
1093 oid_to_hex(&(*list)->object.oid));
1096 hashwrite_be32(f, edge_value);
1099 parent = parent->next;
1102 edge_value = GRAPH_PARENT_NONE;
1103 else if (parent->next)
1104 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1106 edge_value = oid_pos(&parent->item->object.oid,
1111 if (edge_value >= 0)
1112 edge_value += ctx->new_num_commits_in_base;
1113 else if (ctx->new_base_graph) {
1115 if (find_commit_in_graph(parent->item,
1116 ctx->new_base_graph,
1122 BUG("missing parent %s for commit %s",
1123 oid_to_hex(&parent->item->object.oid),
1124 oid_to_hex(&(*list)->object.oid));
1127 hashwrite_be32(f, edge_value);
1129 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1132 parent = parent->next;
1136 if (sizeof((*list)->date) > 4)
1137 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1141 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1143 packedDate[1] = htonl((*list)->date);
1144 hashwrite(f, packedDate, 8);
1152 static int write_graph_chunk_generation_data(struct hashfile *f,
1155 struct write_commit_graph_context *ctx = data;
1156 int i, num_generation_data_overflows = 0;
1158 for (i = 0; i < ctx->commits.nr; i++) {
1159 struct commit *c = ctx->commits.list[i];
1161 repo_parse_commit(ctx->r, c);
1162 offset = commit_graph_data_at(c)->generation - c->date;
1163 display_progress(ctx->progress, ++ctx->progress_cnt);
1165 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1166 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1167 num_generation_data_overflows++;
1170 hashwrite_be32(f, offset);
1176 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1179 struct write_commit_graph_context *ctx = data;
1181 for (i = 0; i < ctx->commits.nr; i++) {
1182 struct commit *c = ctx->commits.list[i];
1183 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1184 display_progress(ctx->progress, ++ctx->progress_cnt);
1186 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1187 hashwrite_be32(f, offset >> 32);
1188 hashwrite_be32(f, (uint32_t) offset);
1195 static int write_graph_chunk_extra_edges(struct hashfile *f,
1198 struct write_commit_graph_context *ctx = data;
1199 struct commit **list = ctx->commits.list;
1200 struct commit **last = ctx->commits.list + ctx->commits.nr;
1201 struct commit_list *parent;
1203 while (list < last) {
1204 int num_parents = 0;
1206 display_progress(ctx->progress, ++ctx->progress_cnt);
1208 for (parent = (*list)->parents; num_parents < 3 && parent;
1209 parent = parent->next)
1212 if (num_parents <= 2) {
1217 /* Since num_parents > 2, this initializer is safe. */
1218 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1219 int edge_value = oid_pos(&parent->item->object.oid,
1224 if (edge_value >= 0)
1225 edge_value += ctx->new_num_commits_in_base;
1226 else if (ctx->new_base_graph) {
1228 if (find_commit_in_graph(parent->item,
1229 ctx->new_base_graph,
1235 BUG("missing parent %s for commit %s",
1236 oid_to_hex(&parent->item->object.oid),
1237 oid_to_hex(&(*list)->object.oid));
1238 else if (!parent->next)
1239 edge_value |= GRAPH_LAST_EDGE;
1241 hashwrite_be32(f, edge_value);
1250 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1253 struct write_commit_graph_context *ctx = data;
1254 struct commit **list = ctx->commits.list;
1255 struct commit **last = ctx->commits.list + ctx->commits.nr;
1256 uint32_t cur_pos = 0;
1258 while (list < last) {
1259 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1260 size_t len = filter ? filter->len : 0;
1262 display_progress(ctx->progress, ++ctx->progress_cnt);
1263 hashwrite_be32(f, cur_pos);
1270 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1272 struct json_writer jw = JSON_WRITER_INIT;
1274 jw_object_begin(&jw, 0);
1275 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1276 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1277 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1278 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1281 trace2_data_json("bloom", ctx->r, "settings", &jw);
1286 static int write_graph_chunk_bloom_data(struct hashfile *f,
1289 struct write_commit_graph_context *ctx = data;
1290 struct commit **list = ctx->commits.list;
1291 struct commit **last = ctx->commits.list + ctx->commits.nr;
1293 trace2_bloom_filter_settings(ctx);
1295 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1296 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1297 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1299 while (list < last) {
1300 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1301 size_t len = filter ? filter->len : 0;
1303 display_progress(ctx->progress, ++ctx->progress_cnt);
1305 hashwrite(f, filter->data, len * sizeof(unsigned char));
1312 static int add_packed_commits(const struct object_id *oid,
1313 struct packed_git *pack,
1317 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1318 enum object_type type;
1319 off_t offset = nth_packed_object_offset(pack, pos);
1320 struct object_info oi = OBJECT_INFO_INIT;
1323 display_progress(ctx->progress, ++ctx->progress_done);
1326 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1327 die(_("unable to get type of object %s"), oid_to_hex(oid));
1329 if (type != OBJ_COMMIT)
1332 oid_array_append(&ctx->oids, oid);
1333 set_commit_pos(ctx->r, oid);
1338 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1340 struct commit_list *parent;
1341 for (parent = commit->parents; parent; parent = parent->next) {
1342 if (!(parent->item->object.flags & REACHABLE)) {
1343 oid_array_append(&ctx->oids, &parent->item->object.oid);
1344 parent->item->object.flags |= REACHABLE;
1349 static void close_reachable(struct write_commit_graph_context *ctx)
1352 struct commit *commit;
1353 enum commit_graph_split_flags flags = ctx->opts ?
1354 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1356 if (ctx->report_progress)
1357 ctx->progress = start_delayed_progress(
1358 _("Loading known commits in commit graph"),
1360 for (i = 0; i < ctx->oids.nr; i++) {
1361 display_progress(ctx->progress, i + 1);
1362 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1364 commit->object.flags |= REACHABLE;
1366 stop_progress(&ctx->progress);
1369 * As this loop runs, ctx->oids.nr may grow, but not more
1370 * than the number of missing commits in the reachable
1373 if (ctx->report_progress)
1374 ctx->progress = start_delayed_progress(
1375 _("Expanding reachable commits in commit graph"),
1377 for (i = 0; i < ctx->oids.nr; i++) {
1378 display_progress(ctx->progress, i + 1);
1379 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1384 if ((!repo_parse_commit(ctx->r, commit) &&
1385 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1386 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1387 add_missing_parents(ctx, commit);
1388 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1389 add_missing_parents(ctx, commit);
1391 stop_progress(&ctx->progress);
1393 if (ctx->report_progress)
1394 ctx->progress = start_delayed_progress(
1395 _("Clearing commit marks in commit graph"),
1397 for (i = 0; i < ctx->oids.nr; i++) {
1398 display_progress(ctx->progress, i + 1);
1399 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1402 commit->object.flags &= ~REACHABLE;
1404 stop_progress(&ctx->progress);
1407 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1410 struct commit_list *list = NULL;
1412 if (ctx->report_progress)
1413 ctx->progress = start_delayed_progress(
1414 _("Computing commit graph topological levels"),
1416 for (i = 0; i < ctx->commits.nr; i++) {
1417 struct commit *c = ctx->commits.list[i];
1420 repo_parse_commit(ctx->r, c);
1421 level = *topo_level_slab_at(ctx->topo_levels, c);
1423 display_progress(ctx->progress, i + 1);
1424 if (level != GENERATION_NUMBER_ZERO)
1427 commit_list_insert(c, &list);
1429 struct commit *current = list->item;
1430 struct commit_list *parent;
1431 int all_parents_computed = 1;
1432 uint32_t max_level = 0;
1434 for (parent = current->parents; parent; parent = parent->next) {
1435 repo_parse_commit(ctx->r, parent->item);
1436 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1438 if (level == GENERATION_NUMBER_ZERO) {
1439 all_parents_computed = 0;
1440 commit_list_insert(parent->item, &list);
1444 if (level > max_level)
1448 if (all_parents_computed) {
1451 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1452 max_level = GENERATION_NUMBER_V1_MAX - 1;
1453 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1457 stop_progress(&ctx->progress);
1460 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1463 struct commit_list *list = NULL;
1465 if (ctx->report_progress)
1466 ctx->progress = start_delayed_progress(
1467 _("Computing commit graph generation numbers"),
1470 if (!ctx->trust_generation_numbers) {
1471 for (i = 0; i < ctx->commits.nr; i++) {
1472 struct commit *c = ctx->commits.list[i];
1473 repo_parse_commit(ctx->r, c);
1474 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1478 for (i = 0; i < ctx->commits.nr; i++) {
1479 struct commit *c = ctx->commits.list[i];
1480 timestamp_t corrected_commit_date;
1482 repo_parse_commit(ctx->r, c);
1483 corrected_commit_date = commit_graph_data_at(c)->generation;
1485 display_progress(ctx->progress, i + 1);
1486 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1489 commit_list_insert(c, &list);
1491 struct commit *current = list->item;
1492 struct commit_list *parent;
1493 int all_parents_computed = 1;
1494 timestamp_t max_corrected_commit_date = 0;
1496 for (parent = current->parents; parent; parent = parent->next) {
1497 repo_parse_commit(ctx->r, parent->item);
1498 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1500 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1501 all_parents_computed = 0;
1502 commit_list_insert(parent->item, &list);
1506 if (corrected_commit_date > max_corrected_commit_date)
1507 max_corrected_commit_date = corrected_commit_date;
1510 if (all_parents_computed) {
1513 if (current->date && current->date > max_corrected_commit_date)
1514 max_corrected_commit_date = current->date - 1;
1515 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1517 if (commit_graph_data_at(current)->generation - current->date > GENERATION_NUMBER_V2_OFFSET_MAX)
1518 ctx->num_generation_data_overflows++;
1522 stop_progress(&ctx->progress);
1525 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1527 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1528 ctx->count_bloom_filter_computed);
1529 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1530 ctx->count_bloom_filter_not_computed);
1531 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1532 ctx->count_bloom_filter_trunc_empty);
1533 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1534 ctx->count_bloom_filter_trunc_large);
1537 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1540 struct progress *progress = NULL;
1541 struct commit **sorted_commits;
1542 int max_new_filters;
1544 init_bloom_filters();
1546 if (ctx->report_progress)
1547 progress = start_delayed_progress(
1548 _("Computing commit changed paths Bloom filters"),
1551 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1552 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1554 if (ctx->order_by_pack)
1555 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1557 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1559 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1560 ctx->opts->max_new_filters : ctx->commits.nr;
1562 for (i = 0; i < ctx->commits.nr; i++) {
1563 enum bloom_filter_computed computed = 0;
1564 struct commit *c = sorted_commits[i];
1565 struct bloom_filter *filter = get_or_compute_bloom_filter(
1568 ctx->count_bloom_filter_computed < max_new_filters,
1569 ctx->bloom_settings,
1571 if (computed & BLOOM_COMPUTED) {
1572 ctx->count_bloom_filter_computed++;
1573 if (computed & BLOOM_TRUNC_EMPTY)
1574 ctx->count_bloom_filter_trunc_empty++;
1575 if (computed & BLOOM_TRUNC_LARGE)
1576 ctx->count_bloom_filter_trunc_large++;
1577 } else if (computed & BLOOM_NOT_COMPUTED)
1578 ctx->count_bloom_filter_not_computed++;
1579 ctx->total_bloom_filter_data_size += filter
1580 ? sizeof(unsigned char) * filter->len : 0;
1581 display_progress(progress, i + 1);
1584 if (trace2_is_enabled())
1585 trace2_bloom_filter_write_statistics(ctx);
1587 free(sorted_commits);
1588 stop_progress(&progress);
1591 struct refs_cb_data {
1592 struct oidset *commits;
1593 struct progress *progress;
1596 static int add_ref_to_set(const char *refname,
1597 const struct object_id *oid,
1598 int flags, void *cb_data)
1600 struct object_id peeled;
1601 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1603 if (!peel_iterated_oid(oid, &peeled))
1605 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1606 oidset_insert(data->commits, oid);
1608 display_progress(data->progress, oidset_size(data->commits));
1613 int write_commit_graph_reachable(struct object_directory *odb,
1614 enum commit_graph_write_flags flags,
1615 const struct commit_graph_opts *opts)
1617 struct oidset commits = OIDSET_INIT;
1618 struct refs_cb_data data;
1621 memset(&data, 0, sizeof(data));
1622 data.commits = &commits;
1623 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1624 data.progress = start_delayed_progress(
1625 _("Collecting referenced commits"), 0);
1627 for_each_ref(add_ref_to_set, &data);
1629 stop_progress(&data.progress);
1631 result = write_commit_graph(odb, NULL, &commits,
1634 oidset_clear(&commits);
1638 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1639 struct string_list *pack_indexes)
1642 struct strbuf progress_title = STRBUF_INIT;
1643 struct strbuf packname = STRBUF_INIT;
1646 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1647 dirlen = packname.len;
1648 if (ctx->report_progress) {
1649 strbuf_addf(&progress_title,
1650 Q_("Finding commits for commit graph in %d pack",
1651 "Finding commits for commit graph in %d packs",
1654 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1655 ctx->progress_done = 0;
1657 for (i = 0; i < pack_indexes->nr; i++) {
1658 struct packed_git *p;
1659 strbuf_setlen(&packname, dirlen);
1660 strbuf_addstr(&packname, pack_indexes->items[i].string);
1661 p = add_packed_git(packname.buf, packname.len, 1);
1663 error(_("error adding pack %s"), packname.buf);
1666 if (open_pack_index(p)) {
1667 error(_("error opening index for %s"), packname.buf);
1670 for_each_object_in_pack(p, add_packed_commits, ctx,
1671 FOR_EACH_OBJECT_PACK_ORDER);
1676 stop_progress(&ctx->progress);
1677 strbuf_release(&progress_title);
1678 strbuf_release(&packname);
1683 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1684 struct oidset *commits)
1686 struct oidset_iter iter;
1687 struct object_id *oid;
1689 if (!oidset_size(commits))
1692 oidset_iter_init(commits, &iter);
1693 while ((oid = oidset_iter_next(&iter))) {
1694 oid_array_append(&ctx->oids, oid);
1700 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1702 if (ctx->report_progress)
1703 ctx->progress = start_delayed_progress(
1704 _("Finding commits for commit graph among packed objects"),
1705 ctx->approx_nr_objects);
1706 for_each_packed_object(add_packed_commits, ctx,
1707 FOR_EACH_OBJECT_PACK_ORDER);
1708 if (ctx->progress_done < ctx->approx_nr_objects)
1709 display_progress(ctx->progress, ctx->approx_nr_objects);
1710 stop_progress(&ctx->progress);
1713 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1716 enum commit_graph_split_flags flags = ctx->opts ?
1717 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1719 ctx->num_extra_edges = 0;
1720 if (ctx->report_progress)
1721 ctx->progress = start_delayed_progress(
1722 _("Finding extra edges in commit graph"),
1724 oid_array_sort(&ctx->oids);
1725 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1726 unsigned int num_parents;
1728 display_progress(ctx->progress, i + 1);
1730 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1731 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1733 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1734 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1737 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1738 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1740 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1742 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1743 if (num_parents > 2)
1744 ctx->num_extra_edges += num_parents - 1;
1748 stop_progress(&ctx->progress);
1751 static int write_graph_chunk_base_1(struct hashfile *f,
1752 struct commit_graph *g)
1759 num = write_graph_chunk_base_1(f, g->base_graph);
1760 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1764 static int write_graph_chunk_base(struct hashfile *f,
1767 struct write_commit_graph_context *ctx = data;
1768 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1770 if (num != ctx->num_commit_graphs_after - 1) {
1771 error(_("failed to write correct number of base graph ids"));
1778 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1783 struct lock_file lk = LOCK_INIT;
1784 const unsigned hashsz = the_hash_algo->rawsz;
1785 struct strbuf progress_title = STRBUF_INIT;
1786 struct object_id file_hash;
1787 struct chunkfile *cf;
1790 struct strbuf tmp_file = STRBUF_INIT;
1792 strbuf_addf(&tmp_file,
1793 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1795 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1797 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1800 if (safe_create_leading_directories(ctx->graph_name)) {
1801 UNLEAK(ctx->graph_name);
1802 error(_("unable to create leading directories of %s"),
1808 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1810 hold_lock_file_for_update_mode(&lk, lock_name,
1811 LOCK_DIE_ON_ERROR, 0444);
1813 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1815 error(_("unable to create temporary graph layer"));
1819 if (adjust_shared_perm(ctx->graph_name)) {
1820 error(_("unable to adjust shared permissions for '%s'"),
1825 f = hashfd(fd, ctx->graph_name);
1827 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1828 LOCK_DIE_ON_ERROR, 0444);
1829 fd = get_lock_file_fd(&lk);
1830 f = hashfd(fd, get_lock_file_path(&lk));
1833 cf = init_chunkfile(f);
1835 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1836 write_graph_chunk_fanout);
1837 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1838 write_graph_chunk_oids);
1839 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1840 write_graph_chunk_data);
1842 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_NO_GDAT, 0))
1843 ctx->write_generation_data = 0;
1844 if (ctx->write_generation_data)
1845 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1846 sizeof(uint32_t) * ctx->commits.nr,
1847 write_graph_chunk_generation_data);
1848 if (ctx->num_generation_data_overflows)
1849 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1850 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1851 write_graph_chunk_generation_data_overflow);
1852 if (ctx->num_extra_edges)
1853 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1854 4 * ctx->num_extra_edges,
1855 write_graph_chunk_extra_edges);
1856 if (ctx->changed_paths) {
1857 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1858 sizeof(uint32_t) * ctx->commits.nr,
1859 write_graph_chunk_bloom_indexes);
1860 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1861 sizeof(uint32_t) * 3
1862 + ctx->total_bloom_filter_data_size,
1863 write_graph_chunk_bloom_data);
1865 if (ctx->num_commit_graphs_after > 1)
1866 add_chunk(cf, GRAPH_CHUNKID_BASE,
1867 hashsz * (ctx->num_commit_graphs_after - 1),
1868 write_graph_chunk_base);
1870 hashwrite_be32(f, GRAPH_SIGNATURE);
1872 hashwrite_u8(f, GRAPH_VERSION);
1873 hashwrite_u8(f, oid_version());
1874 hashwrite_u8(f, get_num_chunks(cf));
1875 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1877 if (ctx->report_progress) {
1878 strbuf_addf(&progress_title,
1879 Q_("Writing out commit graph in %d pass",
1880 "Writing out commit graph in %d passes",
1881 get_num_chunks(cf)),
1882 get_num_chunks(cf));
1883 ctx->progress = start_delayed_progress(
1885 get_num_chunks(cf) * ctx->commits.nr);
1888 write_chunkfile(cf, ctx);
1890 stop_progress(&ctx->progress);
1891 strbuf_release(&progress_title);
1893 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1894 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1895 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1897 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1898 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1899 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1900 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1903 close_commit_graph(ctx->r->objects);
1904 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1908 FILE *chainf = fdopen_lock_file(&lk, "w");
1909 char *final_graph_name;
1915 error(_("unable to open commit-graph chain file"));
1919 if (ctx->base_graph_name) {
1921 int idx = ctx->num_commit_graphs_after - 1;
1922 if (ctx->num_commit_graphs_after > 1)
1925 dest = ctx->commit_graph_filenames_after[idx];
1927 if (strcmp(ctx->base_graph_name, dest)) {
1928 result = rename(ctx->base_graph_name, dest);
1931 error(_("failed to rename base commit-graph file"));
1936 char *graph_name = get_commit_graph_filename(ctx->odb);
1940 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1941 final_graph_name = get_split_graph_filename(ctx->odb,
1942 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1943 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1945 result = rename(ctx->graph_name, final_graph_name);
1947 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1948 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
1951 error(_("failed to rename temporary commit-graph file"));
1956 commit_lock_file(&lk);
1961 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1963 struct commit_graph *g;
1964 uint32_t num_commits;
1965 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1968 int max_commits = 0;
1972 max_commits = ctx->opts->max_commits;
1974 if (ctx->opts->size_multiple)
1975 size_mult = ctx->opts->size_multiple;
1977 flags = ctx->opts->split_flags;
1980 g = ctx->r->objects->commit_graph;
1981 num_commits = ctx->commits.nr;
1982 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1983 ctx->num_commit_graphs_after = 1;
1985 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1987 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1988 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
1989 while (g && (g->num_commits <= size_mult * num_commits ||
1990 (max_commits && num_commits > max_commits))) {
1991 if (g->odb != ctx->odb)
1994 num_commits += g->num_commits;
1997 ctx->num_commit_graphs_after--;
2001 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2002 ctx->new_base_graph = g;
2003 else if (ctx->num_commit_graphs_after != 1)
2004 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2005 "should be 1 with --split=replace");
2007 if (ctx->num_commit_graphs_after == 2) {
2008 char *old_graph_name = get_commit_graph_filename(g->odb);
2010 if (!strcmp(g->filename, old_graph_name) &&
2011 g->odb != ctx->odb) {
2012 ctx->num_commit_graphs_after = 1;
2013 ctx->new_base_graph = NULL;
2016 free(old_graph_name);
2019 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2020 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2022 for (i = 0; i < ctx->num_commit_graphs_after &&
2023 i < ctx->num_commit_graphs_before; i++)
2024 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2026 i = ctx->num_commit_graphs_before - 1;
2027 g = ctx->r->objects->commit_graph;
2030 if (i < ctx->num_commit_graphs_after)
2031 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2034 * If the topmost remaining layer has generation data chunk, the
2035 * resultant layer also has generation data chunk.
2037 if (i == ctx->num_commit_graphs_after - 2)
2038 ctx->write_generation_data = !!g->chunk_generation_data;
2045 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2046 struct commit_graph *g)
2049 uint32_t offset = g->num_commits_in_base;
2051 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2053 for (i = 0; i < g->num_commits; i++) {
2054 struct object_id oid;
2055 struct commit *result;
2057 display_progress(ctx->progress, i + 1);
2059 load_oid_from_graph(g, i + offset, &oid);
2061 /* only add commits if they still exist in the repo */
2062 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2065 ctx->commits.list[ctx->commits.nr] = result;
2071 static int commit_compare(const void *_a, const void *_b)
2073 const struct commit *a = *(const struct commit **)_a;
2074 const struct commit *b = *(const struct commit **)_b;
2075 return oidcmp(&a->object.oid, &b->object.oid);
2078 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2080 uint32_t i, dedup_i = 0;
2082 if (ctx->report_progress)
2083 ctx->progress = start_delayed_progress(
2084 _("Scanning merged commits"),
2087 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2089 ctx->num_extra_edges = 0;
2090 for (i = 0; i < ctx->commits.nr; i++) {
2091 display_progress(ctx->progress, i);
2093 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2094 &ctx->commits.list[i]->object.oid)) {
2096 * Silently ignore duplicates. These were likely
2097 * created due to a commit appearing in multiple
2098 * layers of the chain, which is unexpected but
2099 * not invalid. We should make sure there is a
2100 * unique copy in the new layer.
2103 unsigned int num_parents;
2105 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2108 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2109 if (num_parents > 2)
2110 ctx->num_extra_edges += num_parents - 1;
2114 ctx->commits.nr = dedup_i;
2116 stop_progress(&ctx->progress);
2119 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2121 struct commit_graph *g = ctx->r->objects->commit_graph;
2122 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2124 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2125 current_graph_number--;
2127 if (ctx->report_progress)
2128 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2130 merge_commit_graph(ctx, g);
2131 stop_progress(&ctx->progress);
2137 ctx->new_base_graph = g;
2138 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2141 if (ctx->new_base_graph)
2142 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2144 sort_and_scan_merged_commits(ctx);
2147 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2150 time_t now = time(NULL);
2152 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2154 struct utimbuf updated_time;
2156 stat(ctx->commit_graph_filenames_before[i], &st);
2158 updated_time.actime = st.st_atime;
2159 updated_time.modtime = now;
2160 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2164 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2166 struct strbuf path = STRBUF_INIT;
2170 timestamp_t expire_time = time(NULL);
2172 if (ctx->opts && ctx->opts->expire_time)
2173 expire_time = ctx->opts->expire_time;
2175 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2176 unlink(chain_file_name);
2177 free(chain_file_name);
2178 ctx->num_commit_graphs_after = 0;
2181 strbuf_addstr(&path, ctx->odb->path);
2182 strbuf_addstr(&path, "/info/commit-graphs");
2183 dir = opendir(path.buf);
2188 strbuf_addch(&path, '/');
2189 dirnamelen = path.len;
2190 while ((de = readdir(dir)) != NULL) {
2192 uint32_t i, found = 0;
2194 strbuf_setlen(&path, dirnamelen);
2195 strbuf_addstr(&path, de->d_name);
2197 stat(path.buf, &st);
2199 if (st.st_mtime > expire_time)
2201 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2204 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2205 if (!strcmp(ctx->commit_graph_filenames_after[i],
2217 strbuf_release(&path);
2220 int write_commit_graph(struct object_directory *odb,
2221 struct string_list *pack_indexes,
2222 struct oidset *commits,
2223 enum commit_graph_write_flags flags,
2224 const struct commit_graph_opts *opts)
2226 struct write_commit_graph_context *ctx;
2230 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2231 struct topo_level_slab topo_levels;
2233 prepare_repo_settings(the_repository);
2234 if (!the_repository->settings.core_commit_graph) {
2235 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2238 if (!commit_graph_compatible(the_repository))
2241 CALLOC_ARRAY(ctx, 1);
2242 ctx->r = the_repository;
2244 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2245 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2246 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2248 ctx->total_bloom_filter_data_size = 0;
2249 ctx->write_generation_data = 1;
2250 ctx->num_generation_data_overflows = 0;
2252 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2253 bloom_settings.bits_per_entry);
2254 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2255 bloom_settings.num_hashes);
2256 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2257 bloom_settings.max_changed_paths);
2258 ctx->bloom_settings = &bloom_settings;
2260 init_topo_level_slab(&topo_levels);
2261 ctx->topo_levels = &topo_levels;
2263 prepare_commit_graph(ctx->r);
2264 if (ctx->r->objects->commit_graph) {
2265 struct commit_graph *g = ctx->r->objects->commit_graph;
2268 g->topo_levels = &topo_levels;
2273 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2274 ctx->changed_paths = 1;
2275 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2276 struct commit_graph *g;
2278 g = ctx->r->objects->commit_graph;
2280 /* We have changed-paths already. Keep them in the next graph */
2281 if (g && g->chunk_bloom_data) {
2282 ctx->changed_paths = 1;
2283 ctx->bloom_settings = g->bloom_filter_settings;
2288 struct commit_graph *g = ctx->r->objects->commit_graph;
2291 ctx->num_commit_graphs_before++;
2295 if (ctx->num_commit_graphs_before) {
2296 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2297 i = ctx->num_commit_graphs_before;
2298 g = ctx->r->objects->commit_graph;
2301 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2307 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2310 ctx->approx_nr_objects = approximate_object_count();
2312 if (ctx->append && ctx->r->objects->commit_graph) {
2313 struct commit_graph *g = ctx->r->objects->commit_graph;
2314 for (i = 0; i < g->num_commits; i++) {
2315 struct object_id oid;
2316 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2317 oid_array_append(&ctx->oids, &oid);
2322 ctx->order_by_pack = 1;
2323 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2328 if ((res = fill_oids_from_commits(ctx, commits)))
2332 if (!pack_indexes && !commits) {
2333 ctx->order_by_pack = 1;
2334 fill_oids_from_all_packs(ctx);
2337 close_reachable(ctx);
2339 copy_oids_to_commits(ctx);
2341 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2342 error(_("too many commits to write graph"));
2347 if (!ctx->commits.nr && !replace)
2351 split_graph_merge_strategy(ctx);
2354 merge_commit_graphs(ctx);
2356 ctx->num_commit_graphs_after = 1;
2358 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2360 compute_topological_levels(ctx);
2361 if (ctx->write_generation_data)
2362 compute_generation_numbers(ctx);
2364 if (ctx->changed_paths)
2365 compute_bloom_filters(ctx);
2367 res = write_commit_graph_file(ctx);
2370 mark_commit_graphs(ctx);
2372 expire_commit_graphs(ctx);
2375 free(ctx->graph_name);
2376 free(ctx->commits.list);
2377 oid_array_clear(&ctx->oids);
2378 clear_topo_level_slab(&topo_levels);
2380 if (ctx->commit_graph_filenames_after) {
2381 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2382 free(ctx->commit_graph_filenames_after[i]);
2383 free(ctx->commit_graph_hash_after[i]);
2386 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2387 free(ctx->commit_graph_filenames_before[i]);
2389 free(ctx->commit_graph_filenames_after);
2390 free(ctx->commit_graph_filenames_before);
2391 free(ctx->commit_graph_hash_after);
2399 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2400 static int verify_commit_graph_error;
2402 static void graph_report(const char *fmt, ...)
2406 verify_commit_graph_error = 1;
2408 vfprintf(stderr, fmt, ap);
2409 fprintf(stderr, "\n");
2413 #define GENERATION_ZERO_EXISTS 1
2414 #define GENERATION_NUMBER_EXISTS 2
2416 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2418 uint32_t i, cur_fanout_pos = 0;
2419 struct object_id prev_oid, cur_oid, checksum;
2420 int generation_zero = 0;
2423 struct progress *progress = NULL;
2424 int local_error = 0;
2427 graph_report("no commit-graph file loaded");
2431 verify_commit_graph_error = verify_commit_graph_lite(g);
2432 if (verify_commit_graph_error)
2433 return verify_commit_graph_error;
2435 devnull = open("/dev/null", O_WRONLY);
2436 f = hashfd(devnull, NULL);
2437 hashwrite(f, g->data, g->data_len - g->hash_len);
2438 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2439 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2440 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2441 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2444 for (i = 0; i < g->num_commits; i++) {
2445 struct commit *graph_commit;
2447 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2449 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2450 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2451 oid_to_hex(&prev_oid),
2452 oid_to_hex(&cur_oid));
2454 oidcpy(&prev_oid, &cur_oid);
2456 while (cur_oid.hash[0] > cur_fanout_pos) {
2457 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2459 if (i != fanout_value)
2460 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2461 cur_fanout_pos, fanout_value, i);
2465 graph_commit = lookup_commit(r, &cur_oid);
2466 if (!parse_commit_in_graph_one(r, g, graph_commit))
2467 graph_report(_("failed to parse commit %s from commit-graph"),
2468 oid_to_hex(&cur_oid));
2471 while (cur_fanout_pos < 256) {
2472 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2474 if (g->num_commits != fanout_value)
2475 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2476 cur_fanout_pos, fanout_value, i);
2481 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2482 return verify_commit_graph_error;
2484 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2485 progress = start_progress(_("Verifying commits in commit graph"),
2488 for (i = 0; i < g->num_commits; i++) {
2489 struct commit *graph_commit, *odb_commit;
2490 struct commit_list *graph_parents, *odb_parents;
2491 timestamp_t max_generation = 0;
2492 timestamp_t generation;
2494 display_progress(progress, i + 1);
2495 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2497 graph_commit = lookup_commit(r, &cur_oid);
2498 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2499 if (parse_commit_internal(odb_commit, 0, 0)) {
2500 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2501 oid_to_hex(&cur_oid));
2505 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2506 get_commit_tree_oid(odb_commit)))
2507 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2508 oid_to_hex(&cur_oid),
2509 oid_to_hex(get_commit_tree_oid(graph_commit)),
2510 oid_to_hex(get_commit_tree_oid(odb_commit)));
2512 graph_parents = graph_commit->parents;
2513 odb_parents = odb_commit->parents;
2515 while (graph_parents) {
2516 if (odb_parents == NULL) {
2517 graph_report(_("commit-graph parent list for commit %s is too long"),
2518 oid_to_hex(&cur_oid));
2522 /* parse parent in case it is in a base graph */
2523 parse_commit_in_graph_one(r, g, graph_parents->item);
2525 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2526 graph_report(_("commit-graph parent for %s is %s != %s"),
2527 oid_to_hex(&cur_oid),
2528 oid_to_hex(&graph_parents->item->object.oid),
2529 oid_to_hex(&odb_parents->item->object.oid));
2531 generation = commit_graph_generation(graph_parents->item);
2532 if (generation > max_generation)
2533 max_generation = generation;
2535 graph_parents = graph_parents->next;
2536 odb_parents = odb_parents->next;
2539 if (odb_parents != NULL)
2540 graph_report(_("commit-graph parent list for commit %s terminates early"),
2541 oid_to_hex(&cur_oid));
2543 if (!commit_graph_generation(graph_commit)) {
2544 if (generation_zero == GENERATION_NUMBER_EXISTS)
2545 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2546 oid_to_hex(&cur_oid));
2547 generation_zero = GENERATION_ZERO_EXISTS;
2548 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2549 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2550 oid_to_hex(&cur_oid));
2552 if (generation_zero == GENERATION_ZERO_EXISTS)
2556 * If we are using topological level and one of our parents has
2557 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2558 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2559 * in the following condition.
2561 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2564 generation = commit_graph_generation(graph_commit);
2565 if (generation < max_generation + 1)
2566 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2567 oid_to_hex(&cur_oid),
2569 max_generation + 1);
2571 if (graph_commit->date != odb_commit->date)
2572 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2573 oid_to_hex(&cur_oid),
2577 stop_progress(&progress);
2579 local_error = verify_commit_graph_error;
2581 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2582 local_error |= verify_commit_graph(r, g->base_graph, flags);
2587 void free_commit_graph(struct commit_graph *g)
2592 munmap((void *)g->data, g->data_len);
2596 free(g->bloom_filter_settings);
2600 void disable_commit_graph(struct repository *r)
2602 r->commit_graph_disabled = 1;