1 #include "git-compat-util.h"
10 #include "sha1-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"
23 void git_test_write_commit_graph_or_die(void)
26 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
29 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
30 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
32 if (write_commit_graph_reachable(the_repository->objects->odb,
34 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
37 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
38 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
39 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
40 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
41 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444154 /* "GDAT" */
42 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f56 /* "GDOV" */
43 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
44 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
45 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
46 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
47 #define MAX_NUM_CHUNKS 9
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_CHUNKLOOKUP_WIDTH 12
63 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
64 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
66 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
68 /* Remember to update object flag allocation in object.h */
69 #define REACHABLE (1u<<15)
71 define_commit_slab(topo_level_slab, uint32_t);
73 /* Keep track of the order in which commits are added to our list. */
74 define_commit_slab(commit_pos, int);
75 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
77 static void set_commit_pos(struct repository *r, const struct object_id *oid)
79 static int32_t max_pos;
80 struct commit *commit = lookup_commit(r, oid);
83 return; /* should never happen, but be lenient */
85 *commit_pos_at(&commit_pos, commit) = max_pos++;
88 static int commit_pos_cmp(const void *va, const void *vb)
90 const struct commit *a = *(const struct commit **)va;
91 const struct commit *b = *(const struct commit **)vb;
92 return commit_pos_at(&commit_pos, a) -
93 commit_pos_at(&commit_pos, b);
96 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
97 static struct commit_graph_data_slab commit_graph_data_slab =
98 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
100 uint32_t commit_graph_position(const struct commit *c)
102 struct commit_graph_data *data =
103 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
105 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
108 timestamp_t commit_graph_generation(const struct commit *c)
110 struct commit_graph_data *data =
111 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
114 return GENERATION_NUMBER_INFINITY;
115 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
116 return GENERATION_NUMBER_INFINITY;
118 return data->generation;
121 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
123 unsigned int i, nth_slab;
124 struct commit_graph_data *data =
125 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
130 nth_slab = c->index / commit_graph_data_slab.slab_size;
131 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
134 * commit-slab initializes elements with zero, overwrite this with
135 * COMMIT_NOT_FROM_GRAPH for graph_pos.
137 * We avoid initializing generation with checking if graph position
138 * is not COMMIT_NOT_FROM_GRAPH.
140 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
141 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
142 COMMIT_NOT_FROM_GRAPH;
149 * Should be used only while writing commit-graph as it compares
150 * generation value of commits by directly accessing commit-slab.
152 static int commit_gen_cmp(const void *va, const void *vb)
154 const struct commit *a = *(const struct commit **)va;
155 const struct commit *b = *(const struct commit **)vb;
157 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
158 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
159 /* lower generation commits first */
160 if (generation_a < generation_b)
162 else if (generation_a > generation_b)
165 /* use date as a heuristic when generations are equal */
166 if (a->date < b->date)
168 else if (a->date > b->date)
173 char *get_commit_graph_filename(struct object_directory *obj_dir)
175 return xstrfmt("%s/info/commit-graph", obj_dir->path);
178 static char *get_split_graph_filename(struct object_directory *odb,
181 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
185 char *get_commit_graph_chain_filename(struct object_directory *odb)
187 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
190 static uint8_t oid_version(void)
192 switch (hash_algo_by_ptr(the_hash_algo)) {
195 case GIT_HASH_SHA256:
198 die(_("invalid hash version"));
202 static struct commit_graph *alloc_commit_graph(void)
204 struct commit_graph *g = xcalloc(1, sizeof(*g));
209 extern int read_replace_refs;
211 static int commit_graph_compatible(struct repository *r)
216 if (read_replace_refs) {
217 prepare_replace_object(r);
218 if (hashmap_get_size(&r->objects->replace_map->map))
222 prepare_commit_graft(r);
223 if (r->parsed_objects &&
224 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
226 if (is_repository_shallow(r))
232 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
234 *fd = git_open(graph_file);
237 if (fstat(*fd, st)) {
244 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
245 int fd, struct stat *st,
246 struct object_directory *odb)
250 struct commit_graph *ret;
252 graph_size = xsize_t(st->st_size);
254 if (graph_size < GRAPH_MIN_SIZE) {
256 error(_("commit-graph file is too small"));
259 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
261 ret = parse_commit_graph(r, graph_map, graph_size);
266 munmap(graph_map, graph_size);
271 static int verify_commit_graph_lite(struct commit_graph *g)
274 * Basic validation shared between parse_commit_graph()
275 * which'll be called every time the graph is used, and the
276 * much more expensive verify_commit_graph() used by
277 * "commit-graph verify".
279 * There should only be very basic checks here to ensure that
280 * we don't e.g. segfault in fill_commit_in_graph(), but
281 * because this is a very hot codepath nothing that e.g. loops
282 * over g->num_commits, or runs a checksum on the commit-graph
285 if (!g->chunk_oid_fanout) {
286 error("commit-graph is missing the OID Fanout chunk");
289 if (!g->chunk_oid_lookup) {
290 error("commit-graph is missing the OID Lookup chunk");
293 if (!g->chunk_commit_data) {
294 error("commit-graph is missing the Commit Data chunk");
301 struct commit_graph *parse_commit_graph(struct repository *r,
302 void *graph_map, size_t graph_size)
304 const unsigned char *data, *chunk_lookup;
306 struct commit_graph *graph;
307 uint64_t next_chunk_offset;
308 uint32_t graph_signature;
309 unsigned char graph_version, hash_version;
314 if (graph_size < GRAPH_MIN_SIZE)
317 data = (const unsigned char *)graph_map;
319 graph_signature = get_be32(data);
320 if (graph_signature != GRAPH_SIGNATURE) {
321 error(_("commit-graph signature %X does not match signature %X"),
322 graph_signature, GRAPH_SIGNATURE);
326 graph_version = *(unsigned char*)(data + 4);
327 if (graph_version != GRAPH_VERSION) {
328 error(_("commit-graph version %X does not match version %X"),
329 graph_version, GRAPH_VERSION);
333 hash_version = *(unsigned char*)(data + 5);
334 if (hash_version != oid_version()) {
335 error(_("commit-graph hash version %X does not match version %X"),
336 hash_version, oid_version());
340 prepare_repo_settings(r);
342 graph = alloc_commit_graph();
344 graph->hash_len = the_hash_algo->rawsz;
345 graph->num_chunks = *(unsigned char*)(data + 6);
346 graph->data = graph_map;
347 graph->data_len = graph_size;
349 if (graph_size < GRAPH_HEADER_SIZE +
350 (graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH +
351 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
352 error(_("commit-graph file is too small to hold %u chunks"),
358 chunk_lookup = data + 8;
359 next_chunk_offset = get_be64(chunk_lookup + 4);
360 for (i = 0; i < graph->num_chunks; i++) {
362 uint64_t chunk_offset = next_chunk_offset;
363 int chunk_repeated = 0;
365 chunk_id = get_be32(chunk_lookup + 0);
367 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
368 next_chunk_offset = get_be64(chunk_lookup + 4);
370 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
371 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
372 (uint32_t)chunk_offset);
373 goto free_and_return;
377 case GRAPH_CHUNKID_OIDFANOUT:
378 if (graph->chunk_oid_fanout)
381 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
384 case GRAPH_CHUNKID_OIDLOOKUP:
385 if (graph->chunk_oid_lookup)
388 graph->chunk_oid_lookup = data + chunk_offset;
389 graph->num_commits = (next_chunk_offset - chunk_offset)
394 case GRAPH_CHUNKID_DATA:
395 if (graph->chunk_commit_data)
398 graph->chunk_commit_data = data + chunk_offset;
401 case GRAPH_CHUNKID_GENERATION_DATA:
402 if (graph->chunk_generation_data)
405 graph->chunk_generation_data = data + chunk_offset;
408 case GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW:
409 if (graph->chunk_generation_data_overflow)
412 graph->chunk_generation_data_overflow = data + chunk_offset;
415 case GRAPH_CHUNKID_EXTRAEDGES:
416 if (graph->chunk_extra_edges)
419 graph->chunk_extra_edges = data + chunk_offset;
422 case GRAPH_CHUNKID_BASE:
423 if (graph->chunk_base_graphs)
426 graph->chunk_base_graphs = data + chunk_offset;
429 case GRAPH_CHUNKID_BLOOMINDEXES:
430 if (graph->chunk_bloom_indexes)
432 else if (r->settings.commit_graph_read_changed_paths)
433 graph->chunk_bloom_indexes = data + chunk_offset;
436 case GRAPH_CHUNKID_BLOOMDATA:
437 if (graph->chunk_bloom_data)
439 else if (r->settings.commit_graph_read_changed_paths) {
440 uint32_t hash_version;
441 graph->chunk_bloom_data = data + chunk_offset;
442 hash_version = get_be32(data + chunk_offset);
444 if (hash_version != 1)
447 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
448 graph->bloom_filter_settings->hash_version = hash_version;
449 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
450 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
451 graph->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
456 if (chunk_repeated) {
457 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
458 goto free_and_return;
462 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
463 init_bloom_filters();
465 /* We need both the bloom chunks to exist together. Else ignore the data */
466 graph->chunk_bloom_indexes = NULL;
467 graph->chunk_bloom_data = NULL;
468 FREE_AND_NULL(graph->bloom_filter_settings);
471 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
473 if (verify_commit_graph_lite(graph))
474 goto free_and_return;
479 free(graph->bloom_filter_settings);
484 static struct commit_graph *load_commit_graph_one(struct repository *r,
485 const char *graph_file,
486 struct object_directory *odb)
491 struct commit_graph *g;
492 int open_ok = open_commit_graph(graph_file, &fd, &st);
497 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
500 g->filename = xstrdup(graph_file);
505 static struct commit_graph *load_commit_graph_v1(struct repository *r,
506 struct object_directory *odb)
508 char *graph_name = get_commit_graph_filename(odb);
509 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
515 static int add_graph_to_chain(struct commit_graph *g,
516 struct commit_graph *chain,
517 struct object_id *oids,
520 struct commit_graph *cur_g = chain;
522 if (n && !g->chunk_base_graphs) {
523 warning(_("commit-graph has no base graphs chunk"));
531 !oideq(&oids[n], &cur_g->oid) ||
532 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
533 warning(_("commit-graph chain does not match"));
537 cur_g = cur_g->base_graph;
540 g->base_graph = chain;
543 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
548 static struct commit_graph *load_commit_graph_chain(struct repository *r,
549 struct object_directory *odb)
551 struct commit_graph *graph_chain = NULL;
552 struct strbuf line = STRBUF_INIT;
554 struct object_id *oids;
555 int i = 0, valid = 1, count;
556 char *chain_name = get_commit_graph_chain_filename(odb);
560 fp = fopen(chain_name, "r");
561 stat_res = stat(chain_name, &st);
566 st.st_size <= the_hash_algo->hexsz)
569 count = st.st_size / (the_hash_algo->hexsz + 1);
570 oids = xcalloc(count, sizeof(struct object_id));
574 for (i = 0; i < count; i++) {
575 struct object_directory *odb;
577 if (strbuf_getline_lf(&line, fp) == EOF)
580 if (get_oid_hex(line.buf, &oids[i])) {
581 warning(_("invalid commit-graph chain: line '%s' not a hash"),
588 for (odb = r->objects->odb; odb; odb = odb->next) {
589 char *graph_name = get_split_graph_filename(odb, line.buf);
590 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
595 if (add_graph_to_chain(g, graph_chain, oids, i)) {
605 warning(_("unable to find all commit-graph files"));
612 strbuf_release(&line);
617 static void validate_mixed_generation_chain(struct commit_graph *g)
619 int read_generation_data;
624 read_generation_data = !!g->chunk_generation_data;
627 g->read_generation_data = read_generation_data;
632 struct commit_graph *read_commit_graph_one(struct repository *r,
633 struct object_directory *odb)
635 struct commit_graph *g = load_commit_graph_v1(r, odb);
638 g = load_commit_graph_chain(r, odb);
640 validate_mixed_generation_chain(g);
645 static void prepare_commit_graph_one(struct repository *r,
646 struct object_directory *odb)
649 if (r->objects->commit_graph)
652 r->objects->commit_graph = read_commit_graph_one(r, odb);
656 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
658 * On the first invocation, this function attempts to load the commit
659 * graph if the_repository is configured to have one.
661 static int prepare_commit_graph(struct repository *r)
663 struct object_directory *odb;
666 * This must come before the "already attempted?" check below, because
667 * we want to disable even an already-loaded graph file.
669 if (r->commit_graph_disabled)
672 if (r->objects->commit_graph_attempted)
673 return !!r->objects->commit_graph;
674 r->objects->commit_graph_attempted = 1;
676 prepare_repo_settings(r);
678 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
679 r->settings.core_commit_graph != 1)
681 * This repository is not configured to use commit graphs, so
682 * do not load one. (But report commit_graph_attempted anyway
683 * so that commit graph loading is not attempted again for this
688 if (!commit_graph_compatible(r))
692 for (odb = r->objects->odb;
693 !r->objects->commit_graph && odb;
695 prepare_commit_graph_one(r, odb);
696 return !!r->objects->commit_graph;
699 int generation_numbers_enabled(struct repository *r)
701 uint32_t first_generation;
702 struct commit_graph *g;
703 if (!prepare_commit_graph(r))
706 g = r->objects->commit_graph;
711 first_generation = get_be32(g->chunk_commit_data +
712 g->hash_len + 8) >> 2;
714 return !!first_generation;
717 int corrected_commit_dates_enabled(struct repository *r)
719 struct commit_graph *g;
720 if (!prepare_commit_graph(r))
723 g = r->objects->commit_graph;
728 return g->read_generation_data;
731 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
733 struct commit_graph *g = r->objects->commit_graph;
735 if (g->bloom_filter_settings)
736 return g->bloom_filter_settings;
742 static void close_commit_graph_one(struct commit_graph *g)
747 close_commit_graph_one(g->base_graph);
748 free_commit_graph(g);
751 void close_commit_graph(struct raw_object_store *o)
753 close_commit_graph_one(o->commit_graph);
754 o->commit_graph = NULL;
757 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
759 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
760 g->chunk_oid_lookup, g->hash_len, pos);
763 static void load_oid_from_graph(struct commit_graph *g,
765 struct object_id *oid)
769 while (g && pos < g->num_commits_in_base)
773 BUG("NULL commit-graph");
775 if (pos >= g->num_commits + g->num_commits_in_base)
776 die(_("invalid commit position. commit-graph is likely corrupt"));
778 lex_index = pos - g->num_commits_in_base;
780 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
783 static struct commit_list **insert_parent_or_die(struct repository *r,
784 struct commit_graph *g,
786 struct commit_list **pptr)
789 struct object_id oid;
791 if (pos >= g->num_commits + g->num_commits_in_base)
792 die("invalid parent position %"PRIu32, pos);
794 load_oid_from_graph(g, pos, &oid);
795 c = lookup_commit(r, &oid);
797 die(_("could not find commit %s"), oid_to_hex(&oid));
798 commit_graph_data_at(c)->graph_pos = pos;
799 return &commit_list_insert(c, pptr)->next;
802 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
804 const unsigned char *commit_data;
805 struct commit_graph_data *graph_data;
806 uint32_t lex_index, offset_pos;
807 uint64_t date_high, date_low, offset;
809 while (pos < g->num_commits_in_base)
812 if (pos >= g->num_commits + g->num_commits_in_base)
813 die(_("invalid commit position. commit-graph is likely corrupt"));
815 lex_index = pos - g->num_commits_in_base;
816 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
818 graph_data = commit_graph_data_at(item);
819 graph_data->graph_pos = pos;
821 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
822 date_low = get_be32(commit_data + g->hash_len + 12);
823 item->date = (timestamp_t)((date_high << 32) | date_low);
825 if (g->read_generation_data) {
826 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
828 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
829 if (!g->chunk_generation_data_overflow)
830 die(_("commit-graph requires overflow generation data but has none"));
832 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
833 graph_data->generation = get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
835 graph_data->generation = item->date + offset;
837 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
840 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
843 static inline void set_commit_tree(struct commit *c, struct tree *t)
848 static int fill_commit_in_graph(struct repository *r,
850 struct commit_graph *g, uint32_t pos)
853 uint32_t *parent_data_ptr;
854 struct commit_list **pptr;
855 const unsigned char *commit_data;
858 while (pos < g->num_commits_in_base)
861 fill_commit_graph_info(item, g, pos);
863 lex_index = pos - g->num_commits_in_base;
864 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
866 item->object.parsed = 1;
868 set_commit_tree(item, NULL);
870 pptr = &item->parents;
872 edge_value = get_be32(commit_data + g->hash_len);
873 if (edge_value == GRAPH_PARENT_NONE)
875 pptr = insert_parent_or_die(r, g, edge_value, pptr);
877 edge_value = get_be32(commit_data + g->hash_len + 4);
878 if (edge_value == GRAPH_PARENT_NONE)
880 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
881 pptr = insert_parent_or_die(r, g, edge_value, pptr);
885 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
886 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
888 edge_value = get_be32(parent_data_ptr);
889 pptr = insert_parent_or_die(r, g,
890 edge_value & GRAPH_EDGE_LAST_MASK,
893 } while (!(edge_value & GRAPH_LAST_EDGE));
898 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
900 uint32_t graph_pos = commit_graph_position(item);
901 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
905 struct commit_graph *cur_g = g;
908 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
909 cur_g = cur_g->base_graph;
912 *pos = lex_index + cur_g->num_commits_in_base;
920 static int parse_commit_in_graph_one(struct repository *r,
921 struct commit_graph *g,
926 if (item->object.parsed)
929 if (find_commit_in_graph(item, g, &pos))
930 return fill_commit_in_graph(r, item, g, pos);
935 int parse_commit_in_graph(struct repository *r, struct commit *item)
937 static int checked_env = 0;
940 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
941 die("dying as requested by the '%s' variable on commit-graph parse!",
942 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
945 if (!prepare_commit_graph(r))
947 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
950 void load_commit_graph_info(struct repository *r, struct commit *item)
953 if (!prepare_commit_graph(r))
955 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
956 fill_commit_graph_info(item, r->objects->commit_graph, pos);
959 static struct tree *load_tree_for_commit(struct repository *r,
960 struct commit_graph *g,
963 struct object_id oid;
964 const unsigned char *commit_data;
965 uint32_t graph_pos = commit_graph_position(c);
967 while (graph_pos < g->num_commits_in_base)
970 commit_data = g->chunk_commit_data +
971 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
973 hashcpy(oid.hash, commit_data);
974 set_commit_tree(c, lookup_tree(r, &oid));
976 return c->maybe_tree;
979 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
980 struct commit_graph *g,
981 const struct commit *c)
984 return c->maybe_tree;
985 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
986 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
988 return load_tree_for_commit(r, g, (struct commit *)c);
991 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
993 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
996 struct packed_commit_list {
997 struct commit **list;
1002 struct write_commit_graph_context {
1003 struct repository *r;
1004 struct object_directory *odb;
1006 struct oid_array oids;
1007 struct packed_commit_list commits;
1008 int num_extra_edges;
1009 int num_generation_data_overflows;
1010 unsigned long approx_nr_objects;
1011 struct progress *progress;
1013 uint64_t progress_cnt;
1015 char *base_graph_name;
1016 int num_commit_graphs_before;
1017 int num_commit_graphs_after;
1018 char **commit_graph_filenames_before;
1019 char **commit_graph_filenames_after;
1020 char **commit_graph_hash_after;
1021 uint32_t new_num_commits_in_base;
1022 struct commit_graph *new_base_graph;
1029 write_generation_data:1;
1031 struct topo_level_slab *topo_levels;
1032 const struct commit_graph_opts *opts;
1033 size_t total_bloom_filter_data_size;
1034 const struct bloom_filter_settings *bloom_settings;
1036 int count_bloom_filter_computed;
1037 int count_bloom_filter_not_computed;
1038 int count_bloom_filter_trunc_empty;
1039 int count_bloom_filter_trunc_large;
1042 static int write_graph_chunk_fanout(struct hashfile *f,
1043 struct write_commit_graph_context *ctx)
1046 struct commit **list = ctx->commits.list;
1049 * Write the first-level table (the list is sorted,
1050 * but we use a 256-entry lookup to be able to avoid
1051 * having to do eight extra binary search iterations).
1053 for (i = 0; i < 256; i++) {
1054 while (count < ctx->commits.nr) {
1055 if ((*list)->object.oid.hash[0] != i)
1057 display_progress(ctx->progress, ++ctx->progress_cnt);
1062 hashwrite_be32(f, count);
1068 static int write_graph_chunk_oids(struct hashfile *f,
1069 struct write_commit_graph_context *ctx)
1071 struct commit **list = ctx->commits.list;
1073 for (count = 0; count < ctx->commits.nr; count++, list++) {
1074 display_progress(ctx->progress, ++ctx->progress_cnt);
1075 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1081 static const unsigned char *commit_to_sha1(size_t index, void *table)
1083 struct commit **commits = table;
1084 return commits[index]->object.oid.hash;
1087 static int write_graph_chunk_data(struct hashfile *f,
1088 struct write_commit_graph_context *ctx)
1090 struct commit **list = ctx->commits.list;
1091 struct commit **last = ctx->commits.list + ctx->commits.nr;
1092 uint32_t num_extra_edges = 0;
1094 while (list < last) {
1095 struct commit_list *parent;
1096 struct object_id *tree;
1098 uint32_t packedDate[2];
1099 display_progress(ctx->progress, ++ctx->progress_cnt);
1101 if (repo_parse_commit_no_graph(ctx->r, *list))
1102 die(_("unable to parse commit %s"),
1103 oid_to_hex(&(*list)->object.oid));
1104 tree = get_commit_tree_oid(*list);
1105 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1107 parent = (*list)->parents;
1110 edge_value = GRAPH_PARENT_NONE;
1112 edge_value = sha1_pos(parent->item->object.oid.hash,
1117 if (edge_value >= 0)
1118 edge_value += ctx->new_num_commits_in_base;
1119 else if (ctx->new_base_graph) {
1121 if (find_commit_in_graph(parent->item,
1122 ctx->new_base_graph,
1128 BUG("missing parent %s for commit %s",
1129 oid_to_hex(&parent->item->object.oid),
1130 oid_to_hex(&(*list)->object.oid));
1133 hashwrite_be32(f, edge_value);
1136 parent = parent->next;
1139 edge_value = GRAPH_PARENT_NONE;
1140 else if (parent->next)
1141 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1143 edge_value = sha1_pos(parent->item->object.oid.hash,
1148 if (edge_value >= 0)
1149 edge_value += ctx->new_num_commits_in_base;
1150 else if (ctx->new_base_graph) {
1152 if (find_commit_in_graph(parent->item,
1153 ctx->new_base_graph,
1159 BUG("missing parent %s for commit %s",
1160 oid_to_hex(&parent->item->object.oid),
1161 oid_to_hex(&(*list)->object.oid));
1164 hashwrite_be32(f, edge_value);
1166 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1169 parent = parent->next;
1173 if (sizeof((*list)->date) > 4)
1174 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1178 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1180 packedDate[1] = htonl((*list)->date);
1181 hashwrite(f, packedDate, 8);
1189 static int write_graph_chunk_generation_data(struct hashfile *f,
1190 struct write_commit_graph_context *ctx)
1192 int i, num_generation_data_overflows = 0;
1194 for (i = 0; i < ctx->commits.nr; i++) {
1195 struct commit *c = ctx->commits.list[i];
1197 repo_parse_commit(ctx->r, c);
1198 offset = commit_graph_data_at(c)->generation - c->date;
1199 display_progress(ctx->progress, ++ctx->progress_cnt);
1201 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1202 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1203 num_generation_data_overflows++;
1206 hashwrite_be32(f, offset);
1212 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1213 struct write_commit_graph_context *ctx)
1216 for (i = 0; i < ctx->commits.nr; i++) {
1217 struct commit *c = ctx->commits.list[i];
1218 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1219 display_progress(ctx->progress, ++ctx->progress_cnt);
1221 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1222 hashwrite_be32(f, offset >> 32);
1223 hashwrite_be32(f, (uint32_t) offset);
1230 static int write_graph_chunk_extra_edges(struct hashfile *f,
1231 struct write_commit_graph_context *ctx)
1233 struct commit **list = ctx->commits.list;
1234 struct commit **last = ctx->commits.list + ctx->commits.nr;
1235 struct commit_list *parent;
1237 while (list < last) {
1238 int num_parents = 0;
1240 display_progress(ctx->progress, ++ctx->progress_cnt);
1242 for (parent = (*list)->parents; num_parents < 3 && parent;
1243 parent = parent->next)
1246 if (num_parents <= 2) {
1251 /* Since num_parents > 2, this initializer is safe. */
1252 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1253 int edge_value = sha1_pos(parent->item->object.oid.hash,
1258 if (edge_value >= 0)
1259 edge_value += ctx->new_num_commits_in_base;
1260 else if (ctx->new_base_graph) {
1262 if (find_commit_in_graph(parent->item,
1263 ctx->new_base_graph,
1269 BUG("missing parent %s for commit %s",
1270 oid_to_hex(&parent->item->object.oid),
1271 oid_to_hex(&(*list)->object.oid));
1272 else if (!parent->next)
1273 edge_value |= GRAPH_LAST_EDGE;
1275 hashwrite_be32(f, edge_value);
1284 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1285 struct write_commit_graph_context *ctx)
1287 struct commit **list = ctx->commits.list;
1288 struct commit **last = ctx->commits.list + ctx->commits.nr;
1289 uint32_t cur_pos = 0;
1291 while (list < last) {
1292 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1293 size_t len = filter ? filter->len : 0;
1295 display_progress(ctx->progress, ++ctx->progress_cnt);
1296 hashwrite_be32(f, cur_pos);
1303 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1305 struct json_writer jw = JSON_WRITER_INIT;
1307 jw_object_begin(&jw, 0);
1308 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1309 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1310 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1311 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1314 trace2_data_json("bloom", ctx->r, "settings", &jw);
1319 static int write_graph_chunk_bloom_data(struct hashfile *f,
1320 struct write_commit_graph_context *ctx)
1322 struct commit **list = ctx->commits.list;
1323 struct commit **last = ctx->commits.list + ctx->commits.nr;
1325 trace2_bloom_filter_settings(ctx);
1327 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1328 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1329 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1331 while (list < last) {
1332 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1333 size_t len = filter ? filter->len : 0;
1335 display_progress(ctx->progress, ++ctx->progress_cnt);
1337 hashwrite(f, filter->data, len * sizeof(unsigned char));
1344 static int add_packed_commits(const struct object_id *oid,
1345 struct packed_git *pack,
1349 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1350 enum object_type type;
1351 off_t offset = nth_packed_object_offset(pack, pos);
1352 struct object_info oi = OBJECT_INFO_INIT;
1355 display_progress(ctx->progress, ++ctx->progress_done);
1358 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1359 die(_("unable to get type of object %s"), oid_to_hex(oid));
1361 if (type != OBJ_COMMIT)
1364 oid_array_append(&ctx->oids, oid);
1365 set_commit_pos(ctx->r, oid);
1370 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1372 struct commit_list *parent;
1373 for (parent = commit->parents; parent; parent = parent->next) {
1374 if (!(parent->item->object.flags & REACHABLE)) {
1375 oid_array_append(&ctx->oids, &parent->item->object.oid);
1376 parent->item->object.flags |= REACHABLE;
1381 static void close_reachable(struct write_commit_graph_context *ctx)
1384 struct commit *commit;
1385 enum commit_graph_split_flags flags = ctx->opts ?
1386 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1388 if (ctx->report_progress)
1389 ctx->progress = start_delayed_progress(
1390 _("Loading known commits in commit graph"),
1392 for (i = 0; i < ctx->oids.nr; i++) {
1393 display_progress(ctx->progress, i + 1);
1394 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1396 commit->object.flags |= REACHABLE;
1398 stop_progress(&ctx->progress);
1401 * As this loop runs, ctx->oids.nr may grow, but not more
1402 * than the number of missing commits in the reachable
1405 if (ctx->report_progress)
1406 ctx->progress = start_delayed_progress(
1407 _("Expanding reachable commits in commit graph"),
1409 for (i = 0; i < ctx->oids.nr; i++) {
1410 display_progress(ctx->progress, i + 1);
1411 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1416 if ((!repo_parse_commit(ctx->r, commit) &&
1417 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1418 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1419 add_missing_parents(ctx, commit);
1420 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1421 add_missing_parents(ctx, commit);
1423 stop_progress(&ctx->progress);
1425 if (ctx->report_progress)
1426 ctx->progress = start_delayed_progress(
1427 _("Clearing commit marks in commit graph"),
1429 for (i = 0; i < ctx->oids.nr; i++) {
1430 display_progress(ctx->progress, i + 1);
1431 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1434 commit->object.flags &= ~REACHABLE;
1436 stop_progress(&ctx->progress);
1439 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1442 struct commit_list *list = NULL;
1444 if (ctx->report_progress)
1445 ctx->progress = start_delayed_progress(
1446 _("Computing commit graph generation numbers"),
1448 for (i = 0; i < ctx->commits.nr; i++) {
1449 struct commit *c = ctx->commits.list[i];
1451 timestamp_t corrected_commit_date;
1453 repo_parse_commit(ctx->r, c);
1454 level = *topo_level_slab_at(ctx->topo_levels, c);
1455 corrected_commit_date = commit_graph_data_at(c)->generation;
1457 display_progress(ctx->progress, i + 1);
1458 if (level != GENERATION_NUMBER_ZERO &&
1459 corrected_commit_date != GENERATION_NUMBER_ZERO)
1462 commit_list_insert(c, &list);
1464 struct commit *current = list->item;
1465 struct commit_list *parent;
1466 int all_parents_computed = 1;
1467 uint32_t max_level = 0;
1468 timestamp_t max_corrected_commit_date = 0;
1470 for (parent = current->parents; parent; parent = parent->next) {
1471 repo_parse_commit(ctx->r, parent->item);
1472 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1473 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1475 if (level == GENERATION_NUMBER_ZERO ||
1476 corrected_commit_date == GENERATION_NUMBER_ZERO) {
1477 all_parents_computed = 0;
1478 commit_list_insert(parent->item, &list);
1482 if (level > max_level)
1485 if (corrected_commit_date > max_corrected_commit_date)
1486 max_corrected_commit_date = corrected_commit_date;
1489 if (all_parents_computed) {
1492 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1493 max_level = GENERATION_NUMBER_V1_MAX - 1;
1494 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1496 if (current->date && current->date > max_corrected_commit_date)
1497 max_corrected_commit_date = current->date - 1;
1498 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1500 if (commit_graph_data_at(current)->generation - current->date > GENERATION_NUMBER_V2_OFFSET_MAX)
1501 ctx->num_generation_data_overflows++;
1505 stop_progress(&ctx->progress);
1508 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1510 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1511 ctx->count_bloom_filter_computed);
1512 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1513 ctx->count_bloom_filter_not_computed);
1514 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1515 ctx->count_bloom_filter_trunc_empty);
1516 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1517 ctx->count_bloom_filter_trunc_large);
1520 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1523 struct progress *progress = NULL;
1524 struct commit **sorted_commits;
1525 int max_new_filters;
1527 init_bloom_filters();
1529 if (ctx->report_progress)
1530 progress = start_delayed_progress(
1531 _("Computing commit changed paths Bloom filters"),
1534 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1535 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1537 if (ctx->order_by_pack)
1538 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1540 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1542 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1543 ctx->opts->max_new_filters : ctx->commits.nr;
1545 for (i = 0; i < ctx->commits.nr; i++) {
1546 enum bloom_filter_computed computed = 0;
1547 struct commit *c = sorted_commits[i];
1548 struct bloom_filter *filter = get_or_compute_bloom_filter(
1551 ctx->count_bloom_filter_computed < max_new_filters,
1552 ctx->bloom_settings,
1554 if (computed & BLOOM_COMPUTED) {
1555 ctx->count_bloom_filter_computed++;
1556 if (computed & BLOOM_TRUNC_EMPTY)
1557 ctx->count_bloom_filter_trunc_empty++;
1558 if (computed & BLOOM_TRUNC_LARGE)
1559 ctx->count_bloom_filter_trunc_large++;
1560 } else if (computed & BLOOM_NOT_COMPUTED)
1561 ctx->count_bloom_filter_not_computed++;
1562 ctx->total_bloom_filter_data_size += filter
1563 ? sizeof(unsigned char) * filter->len : 0;
1564 display_progress(progress, i + 1);
1567 if (trace2_is_enabled())
1568 trace2_bloom_filter_write_statistics(ctx);
1570 free(sorted_commits);
1571 stop_progress(&progress);
1574 struct refs_cb_data {
1575 struct oidset *commits;
1576 struct progress *progress;
1579 static int add_ref_to_set(const char *refname,
1580 const struct object_id *oid,
1581 int flags, void *cb_data)
1583 struct object_id peeled;
1584 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1586 if (!peel_ref(refname, &peeled))
1588 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1589 oidset_insert(data->commits, oid);
1591 display_progress(data->progress, oidset_size(data->commits));
1596 int write_commit_graph_reachable(struct object_directory *odb,
1597 enum commit_graph_write_flags flags,
1598 const struct commit_graph_opts *opts)
1600 struct oidset commits = OIDSET_INIT;
1601 struct refs_cb_data data;
1604 memset(&data, 0, sizeof(data));
1605 data.commits = &commits;
1606 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1607 data.progress = start_delayed_progress(
1608 _("Collecting referenced commits"), 0);
1610 for_each_ref(add_ref_to_set, &data);
1612 stop_progress(&data.progress);
1614 result = write_commit_graph(odb, NULL, &commits,
1617 oidset_clear(&commits);
1621 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1622 struct string_list *pack_indexes)
1625 struct strbuf progress_title = STRBUF_INIT;
1626 struct strbuf packname = STRBUF_INIT;
1629 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1630 dirlen = packname.len;
1631 if (ctx->report_progress) {
1632 strbuf_addf(&progress_title,
1633 Q_("Finding commits for commit graph in %d pack",
1634 "Finding commits for commit graph in %d packs",
1637 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1638 ctx->progress_done = 0;
1640 for (i = 0; i < pack_indexes->nr; i++) {
1641 struct packed_git *p;
1642 strbuf_setlen(&packname, dirlen);
1643 strbuf_addstr(&packname, pack_indexes->items[i].string);
1644 p = add_packed_git(packname.buf, packname.len, 1);
1646 error(_("error adding pack %s"), packname.buf);
1649 if (open_pack_index(p)) {
1650 error(_("error opening index for %s"), packname.buf);
1653 for_each_object_in_pack(p, add_packed_commits, ctx,
1654 FOR_EACH_OBJECT_PACK_ORDER);
1659 stop_progress(&ctx->progress);
1660 strbuf_release(&progress_title);
1661 strbuf_release(&packname);
1666 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1667 struct oidset *commits)
1669 struct oidset_iter iter;
1670 struct object_id *oid;
1672 if (!oidset_size(commits))
1675 oidset_iter_init(commits, &iter);
1676 while ((oid = oidset_iter_next(&iter))) {
1677 oid_array_append(&ctx->oids, oid);
1683 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1685 if (ctx->report_progress)
1686 ctx->progress = start_delayed_progress(
1687 _("Finding commits for commit graph among packed objects"),
1688 ctx->approx_nr_objects);
1689 for_each_packed_object(add_packed_commits, ctx,
1690 FOR_EACH_OBJECT_PACK_ORDER);
1691 if (ctx->progress_done < ctx->approx_nr_objects)
1692 display_progress(ctx->progress, ctx->approx_nr_objects);
1693 stop_progress(&ctx->progress);
1696 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1699 enum commit_graph_split_flags flags = ctx->opts ?
1700 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1702 ctx->num_extra_edges = 0;
1703 if (ctx->report_progress)
1704 ctx->progress = start_delayed_progress(
1705 _("Finding extra edges in commit graph"),
1707 oid_array_sort(&ctx->oids);
1708 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1709 unsigned int num_parents;
1711 display_progress(ctx->progress, i + 1);
1713 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1714 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1716 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1717 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1720 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1721 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1723 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1725 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1726 if (num_parents > 2)
1727 ctx->num_extra_edges += num_parents - 1;
1731 stop_progress(&ctx->progress);
1734 static int write_graph_chunk_base_1(struct hashfile *f,
1735 struct commit_graph *g)
1742 num = write_graph_chunk_base_1(f, g->base_graph);
1743 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1747 static int write_graph_chunk_base(struct hashfile *f,
1748 struct write_commit_graph_context *ctx)
1750 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1752 if (num != ctx->num_commit_graphs_after - 1) {
1753 error(_("failed to write correct number of base graph ids"));
1760 typedef int (*chunk_write_fn)(struct hashfile *f,
1761 struct write_commit_graph_context *ctx);
1766 chunk_write_fn write_fn;
1769 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1774 struct lock_file lk = LOCK_INIT;
1775 struct chunk_info chunks[MAX_NUM_CHUNKS + 1];
1776 const unsigned hashsz = the_hash_algo->rawsz;
1777 struct strbuf progress_title = STRBUF_INIT;
1779 uint64_t chunk_offset;
1780 struct object_id file_hash;
1783 struct strbuf tmp_file = STRBUF_INIT;
1785 strbuf_addf(&tmp_file,
1786 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1788 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1790 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1793 if (safe_create_leading_directories(ctx->graph_name)) {
1794 UNLEAK(ctx->graph_name);
1795 error(_("unable to create leading directories of %s"),
1801 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1803 hold_lock_file_for_update_mode(&lk, lock_name,
1804 LOCK_DIE_ON_ERROR, 0444);
1806 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1808 error(_("unable to create temporary graph layer"));
1812 if (adjust_shared_perm(ctx->graph_name)) {
1813 error(_("unable to adjust shared permissions for '%s'"),
1818 f = hashfd(fd, ctx->graph_name);
1820 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1821 LOCK_DIE_ON_ERROR, 0444);
1822 fd = lk.tempfile->fd;
1823 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1826 chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
1827 chunks[0].size = GRAPH_FANOUT_SIZE;
1828 chunks[0].write_fn = write_graph_chunk_fanout;
1829 chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
1830 chunks[1].size = hashsz * ctx->commits.nr;
1831 chunks[1].write_fn = write_graph_chunk_oids;
1832 chunks[2].id = GRAPH_CHUNKID_DATA;
1833 chunks[2].size = (hashsz + 16) * ctx->commits.nr;
1834 chunks[2].write_fn = write_graph_chunk_data;
1836 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_NO_GDAT, 0))
1837 ctx->write_generation_data = 0;
1838 if (ctx->write_generation_data) {
1839 chunks[num_chunks].id = GRAPH_CHUNKID_GENERATION_DATA;
1840 chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
1841 chunks[num_chunks].write_fn = write_graph_chunk_generation_data;
1844 if (ctx->num_generation_data_overflows) {
1845 chunks[num_chunks].id = GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW;
1846 chunks[num_chunks].size = sizeof(timestamp_t) * ctx->num_generation_data_overflows;
1847 chunks[num_chunks].write_fn = write_graph_chunk_generation_data_overflow;
1850 if (ctx->num_extra_edges) {
1851 chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
1852 chunks[num_chunks].size = 4 * ctx->num_extra_edges;
1853 chunks[num_chunks].write_fn = write_graph_chunk_extra_edges;
1856 if (ctx->changed_paths) {
1857 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
1858 chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
1859 chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes;
1861 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
1862 chunks[num_chunks].size = sizeof(uint32_t) * 3
1863 + ctx->total_bloom_filter_data_size;
1864 chunks[num_chunks].write_fn = write_graph_chunk_bloom_data;
1867 if (ctx->num_commit_graphs_after > 1) {
1868 chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
1869 chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
1870 chunks[num_chunks].write_fn = write_graph_chunk_base;
1874 chunks[num_chunks].id = 0;
1875 chunks[num_chunks].size = 0;
1877 hashwrite_be32(f, GRAPH_SIGNATURE);
1879 hashwrite_u8(f, GRAPH_VERSION);
1880 hashwrite_u8(f, oid_version());
1881 hashwrite_u8(f, num_chunks);
1882 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1884 chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1885 for (i = 0; i <= num_chunks; i++) {
1886 uint32_t chunk_write[3];
1888 chunk_write[0] = htonl(chunks[i].id);
1889 chunk_write[1] = htonl(chunk_offset >> 32);
1890 chunk_write[2] = htonl(chunk_offset & 0xffffffff);
1891 hashwrite(f, chunk_write, 12);
1893 chunk_offset += chunks[i].size;
1896 if (ctx->report_progress) {
1897 strbuf_addf(&progress_title,
1898 Q_("Writing out commit graph in %d pass",
1899 "Writing out commit graph in %d passes",
1902 ctx->progress = start_delayed_progress(
1904 num_chunks * ctx->commits.nr);
1907 for (i = 0; i < num_chunks; i++) {
1908 uint64_t start_offset = f->total + f->offset;
1910 if (chunks[i].write_fn(f, ctx))
1913 if (f->total + f->offset != start_offset + chunks[i].size)
1914 BUG("expected to write %"PRId64" bytes to chunk %"PRIx32", but wrote %"PRId64" instead",
1915 chunks[i].size, chunks[i].id,
1916 f->total + f->offset - start_offset);
1919 stop_progress(&ctx->progress);
1920 strbuf_release(&progress_title);
1922 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1923 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1924 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1926 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1927 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1928 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1929 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1932 close_commit_graph(ctx->r->objects);
1933 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1936 FILE *chainf = fdopen_lock_file(&lk, "w");
1937 char *final_graph_name;
1943 error(_("unable to open commit-graph chain file"));
1947 if (ctx->base_graph_name) {
1949 int idx = ctx->num_commit_graphs_after - 1;
1950 if (ctx->num_commit_graphs_after > 1)
1953 dest = ctx->commit_graph_filenames_after[idx];
1955 if (strcmp(ctx->base_graph_name, dest)) {
1956 result = rename(ctx->base_graph_name, dest);
1959 error(_("failed to rename base commit-graph file"));
1964 char *graph_name = get_commit_graph_filename(ctx->odb);
1968 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1969 final_graph_name = get_split_graph_filename(ctx->odb,
1970 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1971 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1973 result = rename(ctx->graph_name, final_graph_name);
1975 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1976 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1979 error(_("failed to rename temporary commit-graph file"));
1984 commit_lock_file(&lk);
1989 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1991 struct commit_graph *g;
1992 uint32_t num_commits;
1993 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1996 int max_commits = 0;
2000 max_commits = ctx->opts->max_commits;
2002 if (ctx->opts->size_multiple)
2003 size_mult = ctx->opts->size_multiple;
2005 flags = ctx->opts->split_flags;
2008 g = ctx->r->objects->commit_graph;
2009 num_commits = ctx->commits.nr;
2010 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2011 ctx->num_commit_graphs_after = 1;
2013 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2015 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2016 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2017 while (g && (g->num_commits <= size_mult * num_commits ||
2018 (max_commits && num_commits > max_commits))) {
2019 if (g->odb != ctx->odb)
2022 num_commits += g->num_commits;
2025 ctx->num_commit_graphs_after--;
2029 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2030 ctx->new_base_graph = g;
2031 else if (ctx->num_commit_graphs_after != 1)
2032 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2033 "should be 1 with --split=replace");
2035 if (ctx->num_commit_graphs_after == 2) {
2036 char *old_graph_name = get_commit_graph_filename(g->odb);
2038 if (!strcmp(g->filename, old_graph_name) &&
2039 g->odb != ctx->odb) {
2040 ctx->num_commit_graphs_after = 1;
2041 ctx->new_base_graph = NULL;
2044 free(old_graph_name);
2047 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2048 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2050 for (i = 0; i < ctx->num_commit_graphs_after &&
2051 i < ctx->num_commit_graphs_before; i++)
2052 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2054 i = ctx->num_commit_graphs_before - 1;
2055 g = ctx->r->objects->commit_graph;
2058 if (i < ctx->num_commit_graphs_after)
2059 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2062 * If the topmost remaining layer has generation data chunk, the
2063 * resultant layer also has generation data chunk.
2065 if (i == ctx->num_commit_graphs_after - 2)
2066 ctx->write_generation_data = !!g->chunk_generation_data;
2073 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2074 struct commit_graph *g)
2077 uint32_t offset = g->num_commits_in_base;
2079 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2081 for (i = 0; i < g->num_commits; i++) {
2082 struct object_id oid;
2083 struct commit *result;
2085 display_progress(ctx->progress, i + 1);
2087 load_oid_from_graph(g, i + offset, &oid);
2089 /* only add commits if they still exist in the repo */
2090 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2093 ctx->commits.list[ctx->commits.nr] = result;
2099 static int commit_compare(const void *_a, const void *_b)
2101 const struct commit *a = *(const struct commit **)_a;
2102 const struct commit *b = *(const struct commit **)_b;
2103 return oidcmp(&a->object.oid, &b->object.oid);
2106 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2108 uint32_t i, dedup_i = 0;
2110 if (ctx->report_progress)
2111 ctx->progress = start_delayed_progress(
2112 _("Scanning merged commits"),
2115 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2117 ctx->num_extra_edges = 0;
2118 for (i = 0; i < ctx->commits.nr; i++) {
2119 display_progress(ctx->progress, i);
2121 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2122 &ctx->commits.list[i]->object.oid)) {
2124 * Silently ignore duplicates. These were likely
2125 * created due to a commit appearing in multiple
2126 * layers of the chain, which is unexpected but
2127 * not invalid. We should make sure there is a
2128 * unique copy in the new layer.
2131 unsigned int num_parents;
2133 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2136 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2137 if (num_parents > 2)
2138 ctx->num_extra_edges += num_parents - 1;
2142 ctx->commits.nr = dedup_i;
2144 stop_progress(&ctx->progress);
2147 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2149 struct commit_graph *g = ctx->r->objects->commit_graph;
2150 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2152 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2153 current_graph_number--;
2155 if (ctx->report_progress)
2156 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2158 merge_commit_graph(ctx, g);
2159 stop_progress(&ctx->progress);
2165 ctx->new_base_graph = g;
2166 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2169 if (ctx->new_base_graph)
2170 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2172 sort_and_scan_merged_commits(ctx);
2175 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2178 time_t now = time(NULL);
2180 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2182 struct utimbuf updated_time;
2184 stat(ctx->commit_graph_filenames_before[i], &st);
2186 updated_time.actime = st.st_atime;
2187 updated_time.modtime = now;
2188 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2192 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2194 struct strbuf path = STRBUF_INIT;
2198 timestamp_t expire_time = time(NULL);
2200 if (ctx->opts && ctx->opts->expire_time)
2201 expire_time = ctx->opts->expire_time;
2203 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2204 unlink(chain_file_name);
2205 free(chain_file_name);
2206 ctx->num_commit_graphs_after = 0;
2209 strbuf_addstr(&path, ctx->odb->path);
2210 strbuf_addstr(&path, "/info/commit-graphs");
2211 dir = opendir(path.buf);
2216 strbuf_addch(&path, '/');
2217 dirnamelen = path.len;
2218 while ((de = readdir(dir)) != NULL) {
2220 uint32_t i, found = 0;
2222 strbuf_setlen(&path, dirnamelen);
2223 strbuf_addstr(&path, de->d_name);
2225 stat(path.buf, &st);
2227 if (st.st_mtime > expire_time)
2229 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2232 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2233 if (!strcmp(ctx->commit_graph_filenames_after[i],
2245 strbuf_release(&path);
2248 int write_commit_graph(struct object_directory *odb,
2249 struct string_list *pack_indexes,
2250 struct oidset *commits,
2251 enum commit_graph_write_flags flags,
2252 const struct commit_graph_opts *opts)
2254 struct write_commit_graph_context *ctx;
2258 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2259 struct topo_level_slab topo_levels;
2261 prepare_repo_settings(the_repository);
2262 if (!the_repository->settings.core_commit_graph) {
2263 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2266 if (!commit_graph_compatible(the_repository))
2269 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
2270 ctx->r = the_repository;
2272 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2273 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2274 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2276 ctx->total_bloom_filter_data_size = 0;
2277 ctx->write_generation_data = 1;
2278 ctx->num_generation_data_overflows = 0;
2280 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2281 bloom_settings.bits_per_entry);
2282 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2283 bloom_settings.num_hashes);
2284 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2285 bloom_settings.max_changed_paths);
2286 ctx->bloom_settings = &bloom_settings;
2288 init_topo_level_slab(&topo_levels);
2289 ctx->topo_levels = &topo_levels;
2291 if (ctx->r->objects->commit_graph) {
2292 struct commit_graph *g = ctx->r->objects->commit_graph;
2295 g->topo_levels = &topo_levels;
2300 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2301 ctx->changed_paths = 1;
2302 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2303 struct commit_graph *g;
2304 prepare_commit_graph_one(ctx->r, ctx->odb);
2306 g = ctx->r->objects->commit_graph;
2308 /* We have changed-paths already. Keep them in the next graph */
2309 if (g && g->chunk_bloom_data) {
2310 ctx->changed_paths = 1;
2311 ctx->bloom_settings = g->bloom_filter_settings;
2316 struct commit_graph *g;
2317 prepare_commit_graph(ctx->r);
2319 g = ctx->r->objects->commit_graph;
2322 ctx->num_commit_graphs_before++;
2326 if (ctx->num_commit_graphs_before) {
2327 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2328 i = ctx->num_commit_graphs_before;
2329 g = ctx->r->objects->commit_graph;
2332 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2338 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2341 ctx->approx_nr_objects = approximate_object_count();
2344 prepare_commit_graph_one(ctx->r, ctx->odb);
2346 if (ctx->append && ctx->r->objects->commit_graph) {
2347 struct commit_graph *g = ctx->r->objects->commit_graph;
2348 for (i = 0; i < g->num_commits; i++) {
2349 struct object_id oid;
2350 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2351 oid_array_append(&ctx->oids, &oid);
2356 ctx->order_by_pack = 1;
2357 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2362 if ((res = fill_oids_from_commits(ctx, commits)))
2366 if (!pack_indexes && !commits) {
2367 ctx->order_by_pack = 1;
2368 fill_oids_from_all_packs(ctx);
2371 close_reachable(ctx);
2373 copy_oids_to_commits(ctx);
2375 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2376 error(_("too many commits to write graph"));
2381 if (!ctx->commits.nr && !replace)
2385 split_graph_merge_strategy(ctx);
2388 merge_commit_graphs(ctx);
2390 ctx->num_commit_graphs_after = 1;
2392 validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2394 compute_generation_numbers(ctx);
2396 if (ctx->changed_paths)
2397 compute_bloom_filters(ctx);
2399 res = write_commit_graph_file(ctx);
2402 mark_commit_graphs(ctx);
2404 expire_commit_graphs(ctx);
2407 free(ctx->graph_name);
2408 free(ctx->commits.list);
2409 oid_array_clear(&ctx->oids);
2411 if (ctx->commit_graph_filenames_after) {
2412 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2413 free(ctx->commit_graph_filenames_after[i]);
2414 free(ctx->commit_graph_hash_after[i]);
2417 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2418 free(ctx->commit_graph_filenames_before[i]);
2420 free(ctx->commit_graph_filenames_after);
2421 free(ctx->commit_graph_filenames_before);
2422 free(ctx->commit_graph_hash_after);
2430 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2431 static int verify_commit_graph_error;
2433 static void graph_report(const char *fmt, ...)
2437 verify_commit_graph_error = 1;
2439 vfprintf(stderr, fmt, ap);
2440 fprintf(stderr, "\n");
2444 #define GENERATION_ZERO_EXISTS 1
2445 #define GENERATION_NUMBER_EXISTS 2
2447 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2449 uint32_t i, cur_fanout_pos = 0;
2450 struct object_id prev_oid, cur_oid, checksum;
2451 int generation_zero = 0;
2454 struct progress *progress = NULL;
2455 int local_error = 0;
2458 graph_report("no commit-graph file loaded");
2462 verify_commit_graph_error = verify_commit_graph_lite(g);
2463 if (verify_commit_graph_error)
2464 return verify_commit_graph_error;
2466 devnull = open("/dev/null", O_WRONLY);
2467 f = hashfd(devnull, NULL);
2468 hashwrite(f, g->data, g->data_len - g->hash_len);
2469 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2470 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2471 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2472 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2475 for (i = 0; i < g->num_commits; i++) {
2476 struct commit *graph_commit;
2478 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2480 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2481 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2482 oid_to_hex(&prev_oid),
2483 oid_to_hex(&cur_oid));
2485 oidcpy(&prev_oid, &cur_oid);
2487 while (cur_oid.hash[0] > cur_fanout_pos) {
2488 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2490 if (i != fanout_value)
2491 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2492 cur_fanout_pos, fanout_value, i);
2496 graph_commit = lookup_commit(r, &cur_oid);
2497 if (!parse_commit_in_graph_one(r, g, graph_commit))
2498 graph_report(_("failed to parse commit %s from commit-graph"),
2499 oid_to_hex(&cur_oid));
2502 while (cur_fanout_pos < 256) {
2503 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2505 if (g->num_commits != fanout_value)
2506 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2507 cur_fanout_pos, fanout_value, i);
2512 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2513 return verify_commit_graph_error;
2515 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2516 progress = start_progress(_("Verifying commits in commit graph"),
2519 for (i = 0; i < g->num_commits; i++) {
2520 struct commit *graph_commit, *odb_commit;
2521 struct commit_list *graph_parents, *odb_parents;
2522 timestamp_t max_generation = 0;
2523 timestamp_t generation;
2525 display_progress(progress, i + 1);
2526 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2528 graph_commit = lookup_commit(r, &cur_oid);
2529 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2530 if (parse_commit_internal(odb_commit, 0, 0)) {
2531 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2532 oid_to_hex(&cur_oid));
2536 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2537 get_commit_tree_oid(odb_commit)))
2538 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2539 oid_to_hex(&cur_oid),
2540 oid_to_hex(get_commit_tree_oid(graph_commit)),
2541 oid_to_hex(get_commit_tree_oid(odb_commit)));
2543 graph_parents = graph_commit->parents;
2544 odb_parents = odb_commit->parents;
2546 while (graph_parents) {
2547 if (odb_parents == NULL) {
2548 graph_report(_("commit-graph parent list for commit %s is too long"),
2549 oid_to_hex(&cur_oid));
2553 /* parse parent in case it is in a base graph */
2554 parse_commit_in_graph_one(r, g, graph_parents->item);
2556 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2557 graph_report(_("commit-graph parent for %s is %s != %s"),
2558 oid_to_hex(&cur_oid),
2559 oid_to_hex(&graph_parents->item->object.oid),
2560 oid_to_hex(&odb_parents->item->object.oid));
2562 generation = commit_graph_generation(graph_parents->item);
2563 if (generation > max_generation)
2564 max_generation = generation;
2566 graph_parents = graph_parents->next;
2567 odb_parents = odb_parents->next;
2570 if (odb_parents != NULL)
2571 graph_report(_("commit-graph parent list for commit %s terminates early"),
2572 oid_to_hex(&cur_oid));
2574 if (!commit_graph_generation(graph_commit)) {
2575 if (generation_zero == GENERATION_NUMBER_EXISTS)
2576 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2577 oid_to_hex(&cur_oid));
2578 generation_zero = GENERATION_ZERO_EXISTS;
2579 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2580 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2581 oid_to_hex(&cur_oid));
2583 if (generation_zero == GENERATION_ZERO_EXISTS)
2587 * If we are using topological level and one of our parents has
2588 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2589 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2590 * in the following condition.
2592 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2595 generation = commit_graph_generation(graph_commit);
2596 if (generation < max_generation + 1)
2597 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2598 oid_to_hex(&cur_oid),
2600 max_generation + 1);
2602 if (graph_commit->date != odb_commit->date)
2603 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2604 oid_to_hex(&cur_oid),
2608 stop_progress(&progress);
2610 local_error = verify_commit_graph_error;
2612 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2613 local_error |= verify_commit_graph(r, g->base_graph, flags);
2618 void free_commit_graph(struct commit_graph *g)
2623 munmap((void *)g->data, g->data_len);
2627 free(g->bloom_filter_settings);
2631 void disable_commit_graph(struct repository *r)
2633 r->commit_graph_disabled = 1;