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_EXTRAEDGES 0x45444745 /* "EDGE" */
42 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
43 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
44 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
45 #define MAX_NUM_CHUNKS 7
47 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
49 #define GRAPH_VERSION_1 0x1
50 #define GRAPH_VERSION GRAPH_VERSION_1
52 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
53 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
54 #define GRAPH_PARENT_NONE 0x70000000
56 #define GRAPH_LAST_EDGE 0x80000000
58 #define GRAPH_HEADER_SIZE 8
59 #define GRAPH_FANOUT_SIZE (4 * 256)
60 #define GRAPH_CHUNKLOOKUP_WIDTH 12
61 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
62 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
64 /* Remember to update object flag allocation in object.h */
65 #define REACHABLE (1u<<15)
67 /* Keep track of the order in which commits are added to our list. */
68 define_commit_slab(commit_pos, int);
69 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
71 static void set_commit_pos(struct repository *r, const struct object_id *oid)
73 static int32_t max_pos;
74 struct commit *commit = lookup_commit(r, oid);
77 return; /* should never happen, but be lenient */
79 *commit_pos_at(&commit_pos, commit) = max_pos++;
82 static int commit_pos_cmp(const void *va, const void *vb)
84 const struct commit *a = *(const struct commit **)va;
85 const struct commit *b = *(const struct commit **)vb;
86 return commit_pos_at(&commit_pos, a) -
87 commit_pos_at(&commit_pos, b);
90 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
91 static struct commit_graph_data_slab commit_graph_data_slab =
92 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
94 uint32_t commit_graph_position(const struct commit *c)
96 struct commit_graph_data *data =
97 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
99 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
102 uint32_t commit_graph_generation(const struct commit *c)
104 struct commit_graph_data *data =
105 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
108 return GENERATION_NUMBER_INFINITY;
109 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
110 return GENERATION_NUMBER_INFINITY;
112 return data->generation;
115 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
117 unsigned int i, nth_slab;
118 struct commit_graph_data *data =
119 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
124 nth_slab = c->index / commit_graph_data_slab.slab_size;
125 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
128 * commit-slab initializes elements with zero, overwrite this with
129 * COMMIT_NOT_FROM_GRAPH for graph_pos.
131 * We avoid initializing generation with checking if graph position
132 * is not COMMIT_NOT_FROM_GRAPH.
134 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
135 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
136 COMMIT_NOT_FROM_GRAPH;
142 static int commit_gen_cmp(const void *va, const void *vb)
144 const struct commit *a = *(const struct commit **)va;
145 const struct commit *b = *(const struct commit **)vb;
147 uint32_t generation_a = commit_graph_generation(a);
148 uint32_t generation_b = commit_graph_generation(b);
149 /* lower generation commits first */
150 if (generation_a < generation_b)
152 else if (generation_a > generation_b)
155 /* use date as a heuristic when generations are equal */
156 if (a->date < b->date)
158 else if (a->date > b->date)
163 char *get_commit_graph_filename(struct object_directory *obj_dir)
165 return xstrfmt("%s/info/commit-graph", obj_dir->path);
168 static char *get_split_graph_filename(struct object_directory *odb,
171 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
175 char *get_commit_graph_chain_filename(struct object_directory *odb)
177 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
180 static uint8_t oid_version(void)
182 switch (hash_algo_by_ptr(the_hash_algo)) {
185 case GIT_HASH_SHA256:
188 die(_("invalid hash version"));
192 static struct commit_graph *alloc_commit_graph(void)
194 struct commit_graph *g = xcalloc(1, sizeof(*g));
199 extern int read_replace_refs;
201 static int commit_graph_compatible(struct repository *r)
206 if (read_replace_refs) {
207 prepare_replace_object(r);
208 if (hashmap_get_size(&r->objects->replace_map->map)) {
209 warning(_("repository contains replace objects; "
210 "skipping commit-graph"));
215 prepare_commit_graft(r);
216 if (r->parsed_objects &&
217 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent)) {
218 warning(_("repository contains (deprecated) grafts; "
219 "skipping commit-graph"));
222 if (is_repository_shallow(r)) {
223 warning(_("repository is shallow; skipping commit-graph"));
230 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
232 *fd = git_open(graph_file);
235 if (fstat(*fd, st)) {
242 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
243 int fd, struct stat *st,
244 struct object_directory *odb)
248 struct commit_graph *ret;
250 graph_size = xsize_t(st->st_size);
252 if (graph_size < GRAPH_MIN_SIZE) {
254 error(_("commit-graph file is too small"));
257 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
259 ret = parse_commit_graph(r, graph_map, graph_size);
264 munmap(graph_map, graph_size);
269 static int verify_commit_graph_lite(struct commit_graph *g)
272 * Basic validation shared between parse_commit_graph()
273 * which'll be called every time the graph is used, and the
274 * much more expensive verify_commit_graph() used by
275 * "commit-graph verify".
277 * There should only be very basic checks here to ensure that
278 * we don't e.g. segfault in fill_commit_in_graph(), but
279 * because this is a very hot codepath nothing that e.g. loops
280 * over g->num_commits, or runs a checksum on the commit-graph
283 if (!g->chunk_oid_fanout) {
284 error("commit-graph is missing the OID Fanout chunk");
287 if (!g->chunk_oid_lookup) {
288 error("commit-graph is missing the OID Lookup chunk");
291 if (!g->chunk_commit_data) {
292 error("commit-graph is missing the Commit Data chunk");
299 struct commit_graph *parse_commit_graph(struct repository *r,
300 void *graph_map, size_t graph_size)
302 const unsigned char *data, *chunk_lookup;
304 struct commit_graph *graph;
305 uint64_t next_chunk_offset;
306 uint32_t graph_signature;
307 unsigned char graph_version, hash_version;
312 if (graph_size < GRAPH_MIN_SIZE)
315 data = (const unsigned char *)graph_map;
317 graph_signature = get_be32(data);
318 if (graph_signature != GRAPH_SIGNATURE) {
319 error(_("commit-graph signature %X does not match signature %X"),
320 graph_signature, GRAPH_SIGNATURE);
324 graph_version = *(unsigned char*)(data + 4);
325 if (graph_version != GRAPH_VERSION) {
326 error(_("commit-graph version %X does not match version %X"),
327 graph_version, GRAPH_VERSION);
331 hash_version = *(unsigned char*)(data + 5);
332 if (hash_version != oid_version()) {
333 error(_("commit-graph hash version %X does not match version %X"),
334 hash_version, oid_version());
338 prepare_repo_settings(r);
340 graph = alloc_commit_graph();
342 graph->hash_len = the_hash_algo->rawsz;
343 graph->num_chunks = *(unsigned char*)(data + 6);
344 graph->data = graph_map;
345 graph->data_len = graph_size;
347 if (graph_size < GRAPH_HEADER_SIZE +
348 (graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH +
349 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
350 error(_("commit-graph file is too small to hold %u chunks"),
356 chunk_lookup = data + 8;
357 next_chunk_offset = get_be64(chunk_lookup + 4);
358 for (i = 0; i < graph->num_chunks; i++) {
360 uint64_t chunk_offset = next_chunk_offset;
361 int chunk_repeated = 0;
363 chunk_id = get_be32(chunk_lookup + 0);
365 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
366 next_chunk_offset = get_be64(chunk_lookup + 4);
368 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
369 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
370 (uint32_t)chunk_offset);
371 goto free_and_return;
375 case GRAPH_CHUNKID_OIDFANOUT:
376 if (graph->chunk_oid_fanout)
379 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
382 case GRAPH_CHUNKID_OIDLOOKUP:
383 if (graph->chunk_oid_lookup)
386 graph->chunk_oid_lookup = data + chunk_offset;
387 graph->num_commits = (next_chunk_offset - chunk_offset)
392 case GRAPH_CHUNKID_DATA:
393 if (graph->chunk_commit_data)
396 graph->chunk_commit_data = data + chunk_offset;
399 case GRAPH_CHUNKID_EXTRAEDGES:
400 if (graph->chunk_extra_edges)
403 graph->chunk_extra_edges = data + chunk_offset;
406 case GRAPH_CHUNKID_BASE:
407 if (graph->chunk_base_graphs)
410 graph->chunk_base_graphs = data + chunk_offset;
413 case GRAPH_CHUNKID_BLOOMINDEXES:
414 if (graph->chunk_bloom_indexes)
416 else if (r->settings.commit_graph_read_changed_paths)
417 graph->chunk_bloom_indexes = data + chunk_offset;
420 case GRAPH_CHUNKID_BLOOMDATA:
421 if (graph->chunk_bloom_data)
423 else if (r->settings.commit_graph_read_changed_paths) {
424 uint32_t hash_version;
425 graph->chunk_bloom_data = data + chunk_offset;
426 hash_version = get_be32(data + chunk_offset);
428 if (hash_version != 1)
431 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
432 graph->bloom_filter_settings->hash_version = hash_version;
433 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
434 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
435 graph->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
440 if (chunk_repeated) {
441 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
442 goto free_and_return;
446 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
447 init_bloom_filters();
449 /* We need both the bloom chunks to exist together. Else ignore the data */
450 graph->chunk_bloom_indexes = NULL;
451 graph->chunk_bloom_data = NULL;
452 FREE_AND_NULL(graph->bloom_filter_settings);
455 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
457 if (verify_commit_graph_lite(graph))
458 goto free_and_return;
463 free(graph->bloom_filter_settings);
468 static struct commit_graph *load_commit_graph_one(struct repository *r,
469 const char *graph_file,
470 struct object_directory *odb)
475 struct commit_graph *g;
476 int open_ok = open_commit_graph(graph_file, &fd, &st);
481 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
484 g->filename = xstrdup(graph_file);
489 static struct commit_graph *load_commit_graph_v1(struct repository *r,
490 struct object_directory *odb)
492 char *graph_name = get_commit_graph_filename(odb);
493 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
499 static int add_graph_to_chain(struct commit_graph *g,
500 struct commit_graph *chain,
501 struct object_id *oids,
504 struct commit_graph *cur_g = chain;
506 if (n && !g->chunk_base_graphs) {
507 warning(_("commit-graph has no base graphs chunk"));
515 !oideq(&oids[n], &cur_g->oid) ||
516 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
517 warning(_("commit-graph chain does not match"));
521 cur_g = cur_g->base_graph;
524 g->base_graph = chain;
527 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
532 static struct commit_graph *load_commit_graph_chain(struct repository *r,
533 struct object_directory *odb)
535 struct commit_graph *graph_chain = NULL;
536 struct strbuf line = STRBUF_INIT;
538 struct object_id *oids;
539 int i = 0, valid = 1, count;
540 char *chain_name = get_commit_graph_chain_filename(odb);
544 fp = fopen(chain_name, "r");
545 stat_res = stat(chain_name, &st);
550 st.st_size <= the_hash_algo->hexsz)
553 count = st.st_size / (the_hash_algo->hexsz + 1);
554 oids = xcalloc(count, sizeof(struct object_id));
558 for (i = 0; i < count; i++) {
559 struct object_directory *odb;
561 if (strbuf_getline_lf(&line, fp) == EOF)
564 if (get_oid_hex(line.buf, &oids[i])) {
565 warning(_("invalid commit-graph chain: line '%s' not a hash"),
572 for (odb = r->objects->odb; odb; odb = odb->next) {
573 char *graph_name = get_split_graph_filename(odb, line.buf);
574 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
579 if (add_graph_to_chain(g, graph_chain, oids, i)) {
589 warning(_("unable to find all commit-graph files"));
596 strbuf_release(&line);
601 struct commit_graph *read_commit_graph_one(struct repository *r,
602 struct object_directory *odb)
604 struct commit_graph *g = load_commit_graph_v1(r, odb);
607 g = load_commit_graph_chain(r, odb);
612 static void prepare_commit_graph_one(struct repository *r,
613 struct object_directory *odb)
616 if (r->objects->commit_graph)
619 r->objects->commit_graph = read_commit_graph_one(r, odb);
623 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
625 * On the first invocation, this function attempts to load the commit
626 * graph if the_repository is configured to have one.
628 static int prepare_commit_graph(struct repository *r)
630 struct object_directory *odb;
633 * This must come before the "already attempted?" check below, because
634 * we want to disable even an already-loaded graph file.
636 if (r->commit_graph_disabled)
639 if (r->objects->commit_graph_attempted)
640 return !!r->objects->commit_graph;
641 r->objects->commit_graph_attempted = 1;
643 prepare_repo_settings(r);
645 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
646 r->settings.core_commit_graph != 1)
648 * This repository is not configured to use commit graphs, so
649 * do not load one. (But report commit_graph_attempted anyway
650 * so that commit graph loading is not attempted again for this
655 if (!commit_graph_compatible(r))
659 for (odb = r->objects->odb;
660 !r->objects->commit_graph && odb;
662 prepare_commit_graph_one(r, odb);
663 return !!r->objects->commit_graph;
666 int generation_numbers_enabled(struct repository *r)
668 uint32_t first_generation;
669 struct commit_graph *g;
670 if (!prepare_commit_graph(r))
673 g = r->objects->commit_graph;
678 first_generation = get_be32(g->chunk_commit_data +
679 g->hash_len + 8) >> 2;
681 return !!first_generation;
684 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
686 struct commit_graph *g = r->objects->commit_graph;
688 if (g->bloom_filter_settings)
689 return g->bloom_filter_settings;
695 static void close_commit_graph_one(struct commit_graph *g)
700 close_commit_graph_one(g->base_graph);
701 free_commit_graph(g);
704 void close_commit_graph(struct raw_object_store *o)
706 close_commit_graph_one(o->commit_graph);
707 o->commit_graph = NULL;
710 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
712 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
713 g->chunk_oid_lookup, g->hash_len, pos);
716 static void load_oid_from_graph(struct commit_graph *g,
718 struct object_id *oid)
722 while (g && pos < g->num_commits_in_base)
726 BUG("NULL commit-graph");
728 if (pos >= g->num_commits + g->num_commits_in_base)
729 die(_("invalid commit position. commit-graph is likely corrupt"));
731 lex_index = pos - g->num_commits_in_base;
733 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
736 static struct commit_list **insert_parent_or_die(struct repository *r,
737 struct commit_graph *g,
739 struct commit_list **pptr)
742 struct object_id oid;
744 if (pos >= g->num_commits + g->num_commits_in_base)
745 die("invalid parent position %"PRIu32, pos);
747 load_oid_from_graph(g, pos, &oid);
748 c = lookup_commit(r, &oid);
750 die(_("could not find commit %s"), oid_to_hex(&oid));
751 commit_graph_data_at(c)->graph_pos = pos;
752 return &commit_list_insert(c, pptr)->next;
755 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
757 const unsigned char *commit_data;
758 struct commit_graph_data *graph_data;
761 while (pos < g->num_commits_in_base)
764 lex_index = pos - g->num_commits_in_base;
765 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
767 graph_data = commit_graph_data_at(item);
768 graph_data->graph_pos = pos;
769 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
772 static inline void set_commit_tree(struct commit *c, struct tree *t)
777 static int fill_commit_in_graph(struct repository *r,
779 struct commit_graph *g, uint32_t pos)
782 uint32_t *parent_data_ptr;
783 uint64_t date_low, date_high;
784 struct commit_list **pptr;
785 struct commit_graph_data *graph_data;
786 const unsigned char *commit_data;
789 while (pos < g->num_commits_in_base)
792 if (pos >= g->num_commits + g->num_commits_in_base)
793 die(_("invalid commit position. commit-graph is likely corrupt"));
796 * Store the "full" position, but then use the
797 * "local" position for the rest of the calculation.
799 graph_data = commit_graph_data_at(item);
800 graph_data->graph_pos = pos;
801 lex_index = pos - g->num_commits_in_base;
803 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
805 item->object.parsed = 1;
807 set_commit_tree(item, NULL);
809 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
810 date_low = get_be32(commit_data + g->hash_len + 12);
811 item->date = (timestamp_t)((date_high << 32) | date_low);
813 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
815 pptr = &item->parents;
817 edge_value = get_be32(commit_data + g->hash_len);
818 if (edge_value == GRAPH_PARENT_NONE)
820 pptr = insert_parent_or_die(r, g, edge_value, pptr);
822 edge_value = get_be32(commit_data + g->hash_len + 4);
823 if (edge_value == GRAPH_PARENT_NONE)
825 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
826 pptr = insert_parent_or_die(r, g, edge_value, pptr);
830 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
831 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
833 edge_value = get_be32(parent_data_ptr);
834 pptr = insert_parent_or_die(r, g,
835 edge_value & GRAPH_EDGE_LAST_MASK,
838 } while (!(edge_value & GRAPH_LAST_EDGE));
843 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
845 uint32_t graph_pos = commit_graph_position(item);
846 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
850 struct commit_graph *cur_g = g;
853 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
854 cur_g = cur_g->base_graph;
857 *pos = lex_index + cur_g->num_commits_in_base;
865 static int parse_commit_in_graph_one(struct repository *r,
866 struct commit_graph *g,
871 if (item->object.parsed)
874 if (find_commit_in_graph(item, g, &pos))
875 return fill_commit_in_graph(r, item, g, pos);
880 int parse_commit_in_graph(struct repository *r, struct commit *item)
882 static int checked_env = 0;
885 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
886 die("dying as requested by the '%s' variable on commit-graph parse!",
887 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
890 if (!prepare_commit_graph(r))
892 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
895 void load_commit_graph_info(struct repository *r, struct commit *item)
898 if (!prepare_commit_graph(r))
900 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
901 fill_commit_graph_info(item, r->objects->commit_graph, pos);
904 static struct tree *load_tree_for_commit(struct repository *r,
905 struct commit_graph *g,
908 struct object_id oid;
909 const unsigned char *commit_data;
910 uint32_t graph_pos = commit_graph_position(c);
912 while (graph_pos < g->num_commits_in_base)
915 commit_data = g->chunk_commit_data +
916 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
918 hashcpy(oid.hash, commit_data);
919 set_commit_tree(c, lookup_tree(r, &oid));
921 return c->maybe_tree;
924 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
925 struct commit_graph *g,
926 const struct commit *c)
929 return c->maybe_tree;
930 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
931 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
933 return load_tree_for_commit(r, g, (struct commit *)c);
936 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
938 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
941 struct packed_commit_list {
942 struct commit **list;
947 struct write_commit_graph_context {
948 struct repository *r;
949 struct object_directory *odb;
951 struct oid_array oids;
952 struct packed_commit_list commits;
954 unsigned long approx_nr_objects;
955 struct progress *progress;
957 uint64_t progress_cnt;
959 char *base_graph_name;
960 int num_commit_graphs_before;
961 int num_commit_graphs_after;
962 char **commit_graph_filenames_before;
963 char **commit_graph_filenames_after;
964 char **commit_graph_hash_after;
965 uint32_t new_num_commits_in_base;
966 struct commit_graph *new_base_graph;
974 const struct commit_graph_opts *opts;
975 size_t total_bloom_filter_data_size;
976 const struct bloom_filter_settings *bloom_settings;
978 int count_bloom_filter_computed;
979 int count_bloom_filter_not_computed;
980 int count_bloom_filter_trunc_empty;
981 int count_bloom_filter_trunc_large;
984 static int write_graph_chunk_fanout(struct hashfile *f,
985 struct write_commit_graph_context *ctx)
988 struct commit **list = ctx->commits.list;
991 * Write the first-level table (the list is sorted,
992 * but we use a 256-entry lookup to be able to avoid
993 * having to do eight extra binary search iterations).
995 for (i = 0; i < 256; i++) {
996 while (count < ctx->commits.nr) {
997 if ((*list)->object.oid.hash[0] != i)
999 display_progress(ctx->progress, ++ctx->progress_cnt);
1004 hashwrite_be32(f, count);
1010 static int write_graph_chunk_oids(struct hashfile *f,
1011 struct write_commit_graph_context *ctx)
1013 struct commit **list = ctx->commits.list;
1015 for (count = 0; count < ctx->commits.nr; count++, list++) {
1016 display_progress(ctx->progress, ++ctx->progress_cnt);
1017 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1023 static const unsigned char *commit_to_sha1(size_t index, void *table)
1025 struct commit **commits = table;
1026 return commits[index]->object.oid.hash;
1029 static int write_graph_chunk_data(struct hashfile *f,
1030 struct write_commit_graph_context *ctx)
1032 struct commit **list = ctx->commits.list;
1033 struct commit **last = ctx->commits.list + ctx->commits.nr;
1034 uint32_t num_extra_edges = 0;
1036 while (list < last) {
1037 struct commit_list *parent;
1038 struct object_id *tree;
1040 uint32_t packedDate[2];
1041 display_progress(ctx->progress, ++ctx->progress_cnt);
1043 if (parse_commit_no_graph(*list))
1044 die(_("unable to parse commit %s"),
1045 oid_to_hex(&(*list)->object.oid));
1046 tree = get_commit_tree_oid(*list);
1047 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1049 parent = (*list)->parents;
1052 edge_value = GRAPH_PARENT_NONE;
1054 edge_value = sha1_pos(parent->item->object.oid.hash,
1059 if (edge_value >= 0)
1060 edge_value += ctx->new_num_commits_in_base;
1061 else if (ctx->new_base_graph) {
1063 if (find_commit_in_graph(parent->item,
1064 ctx->new_base_graph,
1070 BUG("missing parent %s for commit %s",
1071 oid_to_hex(&parent->item->object.oid),
1072 oid_to_hex(&(*list)->object.oid));
1075 hashwrite_be32(f, edge_value);
1078 parent = parent->next;
1081 edge_value = GRAPH_PARENT_NONE;
1082 else if (parent->next)
1083 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1085 edge_value = sha1_pos(parent->item->object.oid.hash,
1090 if (edge_value >= 0)
1091 edge_value += ctx->new_num_commits_in_base;
1092 else if (ctx->new_base_graph) {
1094 if (find_commit_in_graph(parent->item,
1095 ctx->new_base_graph,
1101 BUG("missing parent %s for commit %s",
1102 oid_to_hex(&parent->item->object.oid),
1103 oid_to_hex(&(*list)->object.oid));
1106 hashwrite_be32(f, edge_value);
1108 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1111 parent = parent->next;
1115 if (sizeof((*list)->date) > 4)
1116 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1120 packedDate[0] |= htonl(commit_graph_data_at(*list)->generation << 2);
1122 packedDate[1] = htonl((*list)->date);
1123 hashwrite(f, packedDate, 8);
1131 static int write_graph_chunk_extra_edges(struct hashfile *f,
1132 struct write_commit_graph_context *ctx)
1134 struct commit **list = ctx->commits.list;
1135 struct commit **last = ctx->commits.list + ctx->commits.nr;
1136 struct commit_list *parent;
1138 while (list < last) {
1139 int num_parents = 0;
1141 display_progress(ctx->progress, ++ctx->progress_cnt);
1143 for (parent = (*list)->parents; num_parents < 3 && parent;
1144 parent = parent->next)
1147 if (num_parents <= 2) {
1152 /* Since num_parents > 2, this initializer is safe. */
1153 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1154 int edge_value = sha1_pos(parent->item->object.oid.hash,
1159 if (edge_value >= 0)
1160 edge_value += ctx->new_num_commits_in_base;
1161 else if (ctx->new_base_graph) {
1163 if (find_commit_in_graph(parent->item,
1164 ctx->new_base_graph,
1170 BUG("missing parent %s for commit %s",
1171 oid_to_hex(&parent->item->object.oid),
1172 oid_to_hex(&(*list)->object.oid));
1173 else if (!parent->next)
1174 edge_value |= GRAPH_LAST_EDGE;
1176 hashwrite_be32(f, edge_value);
1185 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1186 struct write_commit_graph_context *ctx)
1188 struct commit **list = ctx->commits.list;
1189 struct commit **last = ctx->commits.list + ctx->commits.nr;
1190 uint32_t cur_pos = 0;
1192 while (list < last) {
1193 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1194 size_t len = filter ? filter->len : 0;
1196 display_progress(ctx->progress, ++ctx->progress_cnt);
1197 hashwrite_be32(f, cur_pos);
1204 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1206 struct json_writer jw = JSON_WRITER_INIT;
1208 jw_object_begin(&jw, 0);
1209 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1210 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1211 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1212 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1215 trace2_data_json("bloom", ctx->r, "settings", &jw);
1220 static int write_graph_chunk_bloom_data(struct hashfile *f,
1221 struct write_commit_graph_context *ctx)
1223 struct commit **list = ctx->commits.list;
1224 struct commit **last = ctx->commits.list + ctx->commits.nr;
1226 trace2_bloom_filter_settings(ctx);
1228 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1229 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1230 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1232 while (list < last) {
1233 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1234 size_t len = filter ? filter->len : 0;
1236 display_progress(ctx->progress, ++ctx->progress_cnt);
1238 hashwrite(f, filter->data, len * sizeof(unsigned char));
1245 static int add_packed_commits(const struct object_id *oid,
1246 struct packed_git *pack,
1250 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1251 enum object_type type;
1252 off_t offset = nth_packed_object_offset(pack, pos);
1253 struct object_info oi = OBJECT_INFO_INIT;
1256 display_progress(ctx->progress, ++ctx->progress_done);
1259 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1260 die(_("unable to get type of object %s"), oid_to_hex(oid));
1262 if (type != OBJ_COMMIT)
1265 oid_array_append(&ctx->oids, oid);
1266 set_commit_pos(ctx->r, oid);
1271 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1273 struct commit_list *parent;
1274 for (parent = commit->parents; parent; parent = parent->next) {
1275 if (!(parent->item->object.flags & REACHABLE)) {
1276 oid_array_append(&ctx->oids, &parent->item->object.oid);
1277 parent->item->object.flags |= REACHABLE;
1282 static void close_reachable(struct write_commit_graph_context *ctx)
1285 struct commit *commit;
1286 enum commit_graph_split_flags flags = ctx->opts ?
1287 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1289 if (ctx->report_progress)
1290 ctx->progress = start_delayed_progress(
1291 _("Loading known commits in commit graph"),
1293 for (i = 0; i < ctx->oids.nr; i++) {
1294 display_progress(ctx->progress, i + 1);
1295 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1297 commit->object.flags |= REACHABLE;
1299 stop_progress(&ctx->progress);
1302 * As this loop runs, ctx->oids.nr may grow, but not more
1303 * than the number of missing commits in the reachable
1306 if (ctx->report_progress)
1307 ctx->progress = start_delayed_progress(
1308 _("Expanding reachable commits in commit graph"),
1310 for (i = 0; i < ctx->oids.nr; i++) {
1311 display_progress(ctx->progress, i + 1);
1312 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1317 if ((!parse_commit(commit) &&
1318 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1319 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1320 add_missing_parents(ctx, commit);
1321 } else if (!parse_commit_no_graph(commit))
1322 add_missing_parents(ctx, commit);
1324 stop_progress(&ctx->progress);
1326 if (ctx->report_progress)
1327 ctx->progress = start_delayed_progress(
1328 _("Clearing commit marks in commit graph"),
1330 for (i = 0; i < ctx->oids.nr; i++) {
1331 display_progress(ctx->progress, i + 1);
1332 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1335 commit->object.flags &= ~REACHABLE;
1337 stop_progress(&ctx->progress);
1340 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1343 struct commit_list *list = NULL;
1345 if (ctx->report_progress)
1346 ctx->progress = start_delayed_progress(
1347 _("Computing commit graph generation numbers"),
1349 for (i = 0; i < ctx->commits.nr; i++) {
1350 uint32_t generation = commit_graph_data_at(ctx->commits.list[i])->generation;
1352 display_progress(ctx->progress, i + 1);
1353 if (generation != GENERATION_NUMBER_INFINITY &&
1354 generation != GENERATION_NUMBER_ZERO)
1357 commit_list_insert(ctx->commits.list[i], &list);
1359 struct commit *current = list->item;
1360 struct commit_list *parent;
1361 int all_parents_computed = 1;
1362 uint32_t max_generation = 0;
1364 for (parent = current->parents; parent; parent = parent->next) {
1365 generation = commit_graph_data_at(parent->item)->generation;
1367 if (generation == GENERATION_NUMBER_INFINITY ||
1368 generation == GENERATION_NUMBER_ZERO) {
1369 all_parents_computed = 0;
1370 commit_list_insert(parent->item, &list);
1372 } else if (generation > max_generation) {
1373 max_generation = generation;
1377 if (all_parents_computed) {
1378 struct commit_graph_data *data = commit_graph_data_at(current);
1380 data->generation = max_generation + 1;
1383 if (data->generation > GENERATION_NUMBER_MAX)
1384 data->generation = GENERATION_NUMBER_MAX;
1388 stop_progress(&ctx->progress);
1391 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1393 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1394 ctx->count_bloom_filter_computed);
1395 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1396 ctx->count_bloom_filter_not_computed);
1397 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1398 ctx->count_bloom_filter_trunc_empty);
1399 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1400 ctx->count_bloom_filter_trunc_large);
1403 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1406 struct progress *progress = NULL;
1407 struct commit **sorted_commits;
1408 int max_new_filters;
1410 init_bloom_filters();
1412 if (ctx->report_progress)
1413 progress = start_delayed_progress(
1414 _("Computing commit changed paths Bloom filters"),
1417 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1418 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1420 if (ctx->order_by_pack)
1421 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1423 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1425 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1426 ctx->opts->max_new_filters : ctx->commits.nr;
1428 for (i = 0; i < ctx->commits.nr; i++) {
1429 enum bloom_filter_computed computed = 0;
1430 struct commit *c = sorted_commits[i];
1431 struct bloom_filter *filter = get_or_compute_bloom_filter(
1434 ctx->count_bloom_filter_computed < max_new_filters,
1435 ctx->bloom_settings,
1437 if (computed & BLOOM_COMPUTED) {
1438 ctx->count_bloom_filter_computed++;
1439 if (computed & BLOOM_TRUNC_EMPTY)
1440 ctx->count_bloom_filter_trunc_empty++;
1441 if (computed & BLOOM_TRUNC_LARGE)
1442 ctx->count_bloom_filter_trunc_large++;
1443 } else if (computed & BLOOM_NOT_COMPUTED)
1444 ctx->count_bloom_filter_not_computed++;
1445 ctx->total_bloom_filter_data_size += filter
1446 ? sizeof(unsigned char) * filter->len : 0;
1447 display_progress(progress, i + 1);
1450 if (trace2_is_enabled())
1451 trace2_bloom_filter_write_statistics(ctx);
1453 free(sorted_commits);
1454 stop_progress(&progress);
1457 struct refs_cb_data {
1458 struct oidset *commits;
1459 struct progress *progress;
1462 static int add_ref_to_set(const char *refname,
1463 const struct object_id *oid,
1464 int flags, void *cb_data)
1466 struct object_id peeled;
1467 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1469 if (!peel_ref(refname, &peeled))
1471 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1472 oidset_insert(data->commits, oid);
1474 display_progress(data->progress, oidset_size(data->commits));
1479 int write_commit_graph_reachable(struct object_directory *odb,
1480 enum commit_graph_write_flags flags,
1481 const struct commit_graph_opts *opts)
1483 struct oidset commits = OIDSET_INIT;
1484 struct refs_cb_data data;
1487 memset(&data, 0, sizeof(data));
1488 data.commits = &commits;
1489 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1490 data.progress = start_delayed_progress(
1491 _("Collecting referenced commits"), 0);
1493 for_each_ref(add_ref_to_set, &data);
1495 stop_progress(&data.progress);
1497 result = write_commit_graph(odb, NULL, &commits,
1500 oidset_clear(&commits);
1504 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1505 struct string_list *pack_indexes)
1508 struct strbuf progress_title = STRBUF_INIT;
1509 struct strbuf packname = STRBUF_INIT;
1512 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1513 dirlen = packname.len;
1514 if (ctx->report_progress) {
1515 strbuf_addf(&progress_title,
1516 Q_("Finding commits for commit graph in %d pack",
1517 "Finding commits for commit graph in %d packs",
1520 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1521 ctx->progress_done = 0;
1523 for (i = 0; i < pack_indexes->nr; i++) {
1524 struct packed_git *p;
1525 strbuf_setlen(&packname, dirlen);
1526 strbuf_addstr(&packname, pack_indexes->items[i].string);
1527 p = add_packed_git(packname.buf, packname.len, 1);
1529 error(_("error adding pack %s"), packname.buf);
1532 if (open_pack_index(p)) {
1533 error(_("error opening index for %s"), packname.buf);
1536 for_each_object_in_pack(p, add_packed_commits, ctx,
1537 FOR_EACH_OBJECT_PACK_ORDER);
1542 stop_progress(&ctx->progress);
1543 strbuf_release(&progress_title);
1544 strbuf_release(&packname);
1549 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1550 struct oidset *commits)
1552 struct oidset_iter iter;
1553 struct object_id *oid;
1555 if (!oidset_size(commits))
1558 oidset_iter_init(commits, &iter);
1559 while ((oid = oidset_iter_next(&iter))) {
1560 oid_array_append(&ctx->oids, oid);
1566 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1568 if (ctx->report_progress)
1569 ctx->progress = start_delayed_progress(
1570 _("Finding commits for commit graph among packed objects"),
1571 ctx->approx_nr_objects);
1572 for_each_packed_object(add_packed_commits, ctx,
1573 FOR_EACH_OBJECT_PACK_ORDER);
1574 if (ctx->progress_done < ctx->approx_nr_objects)
1575 display_progress(ctx->progress, ctx->approx_nr_objects);
1576 stop_progress(&ctx->progress);
1579 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1582 enum commit_graph_split_flags flags = ctx->opts ?
1583 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1585 ctx->num_extra_edges = 0;
1586 if (ctx->report_progress)
1587 ctx->progress = start_delayed_progress(
1588 _("Finding extra edges in commit graph"),
1590 oid_array_sort(&ctx->oids);
1591 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1592 unsigned int num_parents;
1594 display_progress(ctx->progress, i + 1);
1596 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1597 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1599 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1600 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1603 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1604 parse_commit(ctx->commits.list[ctx->commits.nr]);
1606 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1608 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1609 if (num_parents > 2)
1610 ctx->num_extra_edges += num_parents - 1;
1614 stop_progress(&ctx->progress);
1617 static int write_graph_chunk_base_1(struct hashfile *f,
1618 struct commit_graph *g)
1625 num = write_graph_chunk_base_1(f, g->base_graph);
1626 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1630 static int write_graph_chunk_base(struct hashfile *f,
1631 struct write_commit_graph_context *ctx)
1633 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1635 if (num != ctx->num_commit_graphs_after - 1) {
1636 error(_("failed to write correct number of base graph ids"));
1643 typedef int (*chunk_write_fn)(struct hashfile *f,
1644 struct write_commit_graph_context *ctx);
1649 chunk_write_fn write_fn;
1652 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1657 struct lock_file lk = LOCK_INIT;
1658 struct chunk_info chunks[MAX_NUM_CHUNKS + 1];
1659 const unsigned hashsz = the_hash_algo->rawsz;
1660 struct strbuf progress_title = STRBUF_INIT;
1662 uint64_t chunk_offset;
1663 struct object_id file_hash;
1666 struct strbuf tmp_file = STRBUF_INIT;
1668 strbuf_addf(&tmp_file,
1669 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1671 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1673 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1676 if (safe_create_leading_directories(ctx->graph_name)) {
1677 UNLEAK(ctx->graph_name);
1678 error(_("unable to create leading directories of %s"),
1684 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1686 hold_lock_file_for_update_mode(&lk, lock_name,
1687 LOCK_DIE_ON_ERROR, 0444);
1689 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1691 error(_("unable to create temporary graph layer"));
1695 if (adjust_shared_perm(ctx->graph_name)) {
1696 error(_("unable to adjust shared permissions for '%s'"),
1701 f = hashfd(fd, ctx->graph_name);
1703 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1704 LOCK_DIE_ON_ERROR, 0444);
1705 fd = get_lock_file_fd(&lk);
1706 f = hashfd(fd, get_lock_file_path(&lk));
1709 chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
1710 chunks[0].size = GRAPH_FANOUT_SIZE;
1711 chunks[0].write_fn = write_graph_chunk_fanout;
1712 chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
1713 chunks[1].size = hashsz * ctx->commits.nr;
1714 chunks[1].write_fn = write_graph_chunk_oids;
1715 chunks[2].id = GRAPH_CHUNKID_DATA;
1716 chunks[2].size = (hashsz + 16) * ctx->commits.nr;
1717 chunks[2].write_fn = write_graph_chunk_data;
1718 if (ctx->num_extra_edges) {
1719 chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
1720 chunks[num_chunks].size = 4 * ctx->num_extra_edges;
1721 chunks[num_chunks].write_fn = write_graph_chunk_extra_edges;
1724 if (ctx->changed_paths) {
1725 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
1726 chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
1727 chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes;
1729 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
1730 chunks[num_chunks].size = sizeof(uint32_t) * 3
1731 + ctx->total_bloom_filter_data_size;
1732 chunks[num_chunks].write_fn = write_graph_chunk_bloom_data;
1735 if (ctx->num_commit_graphs_after > 1) {
1736 chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
1737 chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
1738 chunks[num_chunks].write_fn = write_graph_chunk_base;
1742 chunks[num_chunks].id = 0;
1743 chunks[num_chunks].size = 0;
1745 hashwrite_be32(f, GRAPH_SIGNATURE);
1747 hashwrite_u8(f, GRAPH_VERSION);
1748 hashwrite_u8(f, oid_version());
1749 hashwrite_u8(f, num_chunks);
1750 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1752 chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1753 for (i = 0; i <= num_chunks; i++) {
1754 uint32_t chunk_write[3];
1756 chunk_write[0] = htonl(chunks[i].id);
1757 chunk_write[1] = htonl(chunk_offset >> 32);
1758 chunk_write[2] = htonl(chunk_offset & 0xffffffff);
1759 hashwrite(f, chunk_write, 12);
1761 chunk_offset += chunks[i].size;
1764 if (ctx->report_progress) {
1765 strbuf_addf(&progress_title,
1766 Q_("Writing out commit graph in %d pass",
1767 "Writing out commit graph in %d passes",
1770 ctx->progress = start_delayed_progress(
1772 num_chunks * ctx->commits.nr);
1775 for (i = 0; i < num_chunks; i++) {
1776 uint64_t start_offset = f->total + f->offset;
1778 if (chunks[i].write_fn(f, ctx))
1781 if (f->total + f->offset != start_offset + chunks[i].size)
1782 BUG("expected to write %"PRId64" bytes to chunk %"PRIx32", but wrote %"PRId64" instead",
1783 chunks[i].size, chunks[i].id,
1784 f->total + f->offset - start_offset);
1787 stop_progress(&ctx->progress);
1788 strbuf_release(&progress_title);
1790 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1791 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1792 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1794 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1795 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1796 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1797 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1800 close_commit_graph(ctx->r->objects);
1801 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1804 FILE *chainf = fdopen_lock_file(&lk, "w");
1805 char *final_graph_name;
1811 error(_("unable to open commit-graph chain file"));
1815 if (ctx->base_graph_name) {
1817 int idx = ctx->num_commit_graphs_after - 1;
1818 if (ctx->num_commit_graphs_after > 1)
1821 dest = ctx->commit_graph_filenames_after[idx];
1823 if (strcmp(ctx->base_graph_name, dest)) {
1824 result = rename(ctx->base_graph_name, dest);
1827 error(_("failed to rename base commit-graph file"));
1832 char *graph_name = get_commit_graph_filename(ctx->odb);
1836 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1837 final_graph_name = get_split_graph_filename(ctx->odb,
1838 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1839 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1841 result = rename(ctx->graph_name, final_graph_name);
1843 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1844 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
1847 error(_("failed to rename temporary commit-graph file"));
1852 commit_lock_file(&lk);
1857 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1859 struct commit_graph *g;
1860 uint32_t num_commits;
1861 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1864 int max_commits = 0;
1868 max_commits = ctx->opts->max_commits;
1870 if (ctx->opts->size_multiple)
1871 size_mult = ctx->opts->size_multiple;
1873 flags = ctx->opts->split_flags;
1876 g = ctx->r->objects->commit_graph;
1877 num_commits = ctx->commits.nr;
1878 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1879 ctx->num_commit_graphs_after = 1;
1881 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1883 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1884 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
1885 while (g && (g->num_commits <= size_mult * num_commits ||
1886 (max_commits && num_commits > max_commits))) {
1887 if (g->odb != ctx->odb)
1890 num_commits += g->num_commits;
1893 ctx->num_commit_graphs_after--;
1897 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
1898 ctx->new_base_graph = g;
1899 else if (ctx->num_commit_graphs_after != 1)
1900 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1901 "should be 1 with --split=replace");
1903 if (ctx->num_commit_graphs_after == 2) {
1904 char *old_graph_name = get_commit_graph_filename(g->odb);
1906 if (!strcmp(g->filename, old_graph_name) &&
1907 g->odb != ctx->odb) {
1908 ctx->num_commit_graphs_after = 1;
1909 ctx->new_base_graph = NULL;
1912 free(old_graph_name);
1915 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1916 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1918 for (i = 0; i < ctx->num_commit_graphs_after &&
1919 i < ctx->num_commit_graphs_before; i++)
1920 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1922 i = ctx->num_commit_graphs_before - 1;
1923 g = ctx->r->objects->commit_graph;
1926 if (i < ctx->num_commit_graphs_after)
1927 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1934 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1935 struct commit_graph *g)
1938 uint32_t offset = g->num_commits_in_base;
1940 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1942 for (i = 0; i < g->num_commits; i++) {
1943 struct object_id oid;
1944 struct commit *result;
1946 display_progress(ctx->progress, i + 1);
1948 load_oid_from_graph(g, i + offset, &oid);
1950 /* only add commits if they still exist in the repo */
1951 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1954 ctx->commits.list[ctx->commits.nr] = result;
1960 static int commit_compare(const void *_a, const void *_b)
1962 const struct commit *a = *(const struct commit **)_a;
1963 const struct commit *b = *(const struct commit **)_b;
1964 return oidcmp(&a->object.oid, &b->object.oid);
1967 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1969 uint32_t i, dedup_i = 0;
1971 if (ctx->report_progress)
1972 ctx->progress = start_delayed_progress(
1973 _("Scanning merged commits"),
1976 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1978 ctx->num_extra_edges = 0;
1979 for (i = 0; i < ctx->commits.nr; i++) {
1980 display_progress(ctx->progress, i);
1982 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1983 &ctx->commits.list[i]->object.oid)) {
1985 * Silently ignore duplicates. These were likely
1986 * created due to a commit appearing in multiple
1987 * layers of the chain, which is unexpected but
1988 * not invalid. We should make sure there is a
1989 * unique copy in the new layer.
1992 unsigned int num_parents;
1994 ctx->commits.list[dedup_i] = ctx->commits.list[i];
1997 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1998 if (num_parents > 2)
1999 ctx->num_extra_edges += num_parents - 1;
2003 ctx->commits.nr = dedup_i;
2005 stop_progress(&ctx->progress);
2008 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2010 struct commit_graph *g = ctx->r->objects->commit_graph;
2011 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2013 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2014 current_graph_number--;
2016 if (ctx->report_progress)
2017 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2019 merge_commit_graph(ctx, g);
2020 stop_progress(&ctx->progress);
2026 ctx->new_base_graph = g;
2027 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2030 if (ctx->new_base_graph)
2031 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2033 sort_and_scan_merged_commits(ctx);
2036 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2039 time_t now = time(NULL);
2041 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2043 struct utimbuf updated_time;
2045 stat(ctx->commit_graph_filenames_before[i], &st);
2047 updated_time.actime = st.st_atime;
2048 updated_time.modtime = now;
2049 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2053 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2055 struct strbuf path = STRBUF_INIT;
2059 timestamp_t expire_time = time(NULL);
2061 if (ctx->opts && ctx->opts->expire_time)
2062 expire_time = ctx->opts->expire_time;
2064 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2065 unlink(chain_file_name);
2066 free(chain_file_name);
2067 ctx->num_commit_graphs_after = 0;
2070 strbuf_addstr(&path, ctx->odb->path);
2071 strbuf_addstr(&path, "/info/commit-graphs");
2072 dir = opendir(path.buf);
2077 strbuf_addch(&path, '/');
2078 dirnamelen = path.len;
2079 while ((de = readdir(dir)) != NULL) {
2081 uint32_t i, found = 0;
2083 strbuf_setlen(&path, dirnamelen);
2084 strbuf_addstr(&path, de->d_name);
2086 stat(path.buf, &st);
2088 if (st.st_mtime > expire_time)
2090 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2093 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2094 if (!strcmp(ctx->commit_graph_filenames_after[i],
2106 strbuf_release(&path);
2109 int write_commit_graph(struct object_directory *odb,
2110 struct string_list *pack_indexes,
2111 struct oidset *commits,
2112 enum commit_graph_write_flags flags,
2113 const struct commit_graph_opts *opts)
2115 struct write_commit_graph_context *ctx;
2119 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2121 prepare_repo_settings(the_repository);
2122 if (!the_repository->settings.core_commit_graph) {
2123 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2126 if (!commit_graph_compatible(the_repository))
2129 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
2130 ctx->r = the_repository;
2132 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2133 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2134 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2136 ctx->total_bloom_filter_data_size = 0;
2138 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2139 bloom_settings.bits_per_entry);
2140 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2141 bloom_settings.num_hashes);
2142 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2143 bloom_settings.max_changed_paths);
2144 ctx->bloom_settings = &bloom_settings;
2146 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2147 ctx->changed_paths = 1;
2148 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2149 struct commit_graph *g;
2150 prepare_commit_graph_one(ctx->r, ctx->odb);
2152 g = ctx->r->objects->commit_graph;
2154 /* We have changed-paths already. Keep them in the next graph */
2155 if (g && g->chunk_bloom_data) {
2156 ctx->changed_paths = 1;
2157 ctx->bloom_settings = g->bloom_filter_settings;
2162 struct commit_graph *g;
2163 prepare_commit_graph(ctx->r);
2165 g = ctx->r->objects->commit_graph;
2168 ctx->num_commit_graphs_before++;
2172 if (ctx->num_commit_graphs_before) {
2173 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2174 i = ctx->num_commit_graphs_before;
2175 g = ctx->r->objects->commit_graph;
2178 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2184 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2187 ctx->approx_nr_objects = approximate_object_count();
2190 prepare_commit_graph_one(ctx->r, ctx->odb);
2192 if (ctx->append && ctx->r->objects->commit_graph) {
2193 struct commit_graph *g = ctx->r->objects->commit_graph;
2194 for (i = 0; i < g->num_commits; i++) {
2195 struct object_id oid;
2196 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2197 oid_array_append(&ctx->oids, &oid);
2202 ctx->order_by_pack = 1;
2203 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2208 if ((res = fill_oids_from_commits(ctx, commits)))
2212 if (!pack_indexes && !commits) {
2213 ctx->order_by_pack = 1;
2214 fill_oids_from_all_packs(ctx);
2217 close_reachable(ctx);
2219 copy_oids_to_commits(ctx);
2221 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2222 error(_("too many commits to write graph"));
2227 if (!ctx->commits.nr && !replace)
2231 split_graph_merge_strategy(ctx);
2234 merge_commit_graphs(ctx);
2236 ctx->num_commit_graphs_after = 1;
2238 compute_generation_numbers(ctx);
2240 if (ctx->changed_paths)
2241 compute_bloom_filters(ctx);
2243 res = write_commit_graph_file(ctx);
2246 mark_commit_graphs(ctx);
2248 expire_commit_graphs(ctx);
2251 free(ctx->graph_name);
2252 free(ctx->commits.list);
2253 oid_array_clear(&ctx->oids);
2255 if (ctx->commit_graph_filenames_after) {
2256 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2257 free(ctx->commit_graph_filenames_after[i]);
2258 free(ctx->commit_graph_hash_after[i]);
2261 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2262 free(ctx->commit_graph_filenames_before[i]);
2264 free(ctx->commit_graph_filenames_after);
2265 free(ctx->commit_graph_filenames_before);
2266 free(ctx->commit_graph_hash_after);
2274 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2275 static int verify_commit_graph_error;
2277 static void graph_report(const char *fmt, ...)
2281 verify_commit_graph_error = 1;
2283 vfprintf(stderr, fmt, ap);
2284 fprintf(stderr, "\n");
2288 #define GENERATION_ZERO_EXISTS 1
2289 #define GENERATION_NUMBER_EXISTS 2
2291 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2293 uint32_t i, cur_fanout_pos = 0;
2294 struct object_id prev_oid, cur_oid, checksum;
2295 int generation_zero = 0;
2298 struct progress *progress = NULL;
2299 int local_error = 0;
2302 graph_report("no commit-graph file loaded");
2306 verify_commit_graph_error = verify_commit_graph_lite(g);
2307 if (verify_commit_graph_error)
2308 return verify_commit_graph_error;
2310 devnull = open("/dev/null", O_WRONLY);
2311 f = hashfd(devnull, NULL);
2312 hashwrite(f, g->data, g->data_len - g->hash_len);
2313 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2314 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2315 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2316 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2319 for (i = 0; i < g->num_commits; i++) {
2320 struct commit *graph_commit;
2322 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2324 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2325 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2326 oid_to_hex(&prev_oid),
2327 oid_to_hex(&cur_oid));
2329 oidcpy(&prev_oid, &cur_oid);
2331 while (cur_oid.hash[0] > cur_fanout_pos) {
2332 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2334 if (i != fanout_value)
2335 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2336 cur_fanout_pos, fanout_value, i);
2340 graph_commit = lookup_commit(r, &cur_oid);
2341 if (!parse_commit_in_graph_one(r, g, graph_commit))
2342 graph_report(_("failed to parse commit %s from commit-graph"),
2343 oid_to_hex(&cur_oid));
2346 while (cur_fanout_pos < 256) {
2347 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2349 if (g->num_commits != fanout_value)
2350 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2351 cur_fanout_pos, fanout_value, i);
2356 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2357 return verify_commit_graph_error;
2359 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2360 progress = start_progress(_("Verifying commits in commit graph"),
2363 for (i = 0; i < g->num_commits; i++) {
2364 struct commit *graph_commit, *odb_commit;
2365 struct commit_list *graph_parents, *odb_parents;
2366 uint32_t max_generation = 0;
2367 uint32_t generation;
2369 display_progress(progress, i + 1);
2370 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2372 graph_commit = lookup_commit(r, &cur_oid);
2373 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2374 if (parse_commit_internal(odb_commit, 0, 0)) {
2375 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2376 oid_to_hex(&cur_oid));
2380 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2381 get_commit_tree_oid(odb_commit)))
2382 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2383 oid_to_hex(&cur_oid),
2384 oid_to_hex(get_commit_tree_oid(graph_commit)),
2385 oid_to_hex(get_commit_tree_oid(odb_commit)));
2387 graph_parents = graph_commit->parents;
2388 odb_parents = odb_commit->parents;
2390 while (graph_parents) {
2391 if (odb_parents == NULL) {
2392 graph_report(_("commit-graph parent list for commit %s is too long"),
2393 oid_to_hex(&cur_oid));
2397 /* parse parent in case it is in a base graph */
2398 parse_commit_in_graph_one(r, g, graph_parents->item);
2400 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2401 graph_report(_("commit-graph parent for %s is %s != %s"),
2402 oid_to_hex(&cur_oid),
2403 oid_to_hex(&graph_parents->item->object.oid),
2404 oid_to_hex(&odb_parents->item->object.oid));
2406 generation = commit_graph_generation(graph_parents->item);
2407 if (generation > max_generation)
2408 max_generation = generation;
2410 graph_parents = graph_parents->next;
2411 odb_parents = odb_parents->next;
2414 if (odb_parents != NULL)
2415 graph_report(_("commit-graph parent list for commit %s terminates early"),
2416 oid_to_hex(&cur_oid));
2418 if (!commit_graph_generation(graph_commit)) {
2419 if (generation_zero == GENERATION_NUMBER_EXISTS)
2420 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2421 oid_to_hex(&cur_oid));
2422 generation_zero = GENERATION_ZERO_EXISTS;
2423 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2424 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2425 oid_to_hex(&cur_oid));
2427 if (generation_zero == GENERATION_ZERO_EXISTS)
2431 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2432 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2433 * extra logic in the following condition.
2435 if (max_generation == GENERATION_NUMBER_MAX)
2438 generation = commit_graph_generation(graph_commit);
2439 if (generation != max_generation + 1)
2440 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2441 oid_to_hex(&cur_oid),
2443 max_generation + 1);
2445 if (graph_commit->date != odb_commit->date)
2446 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2447 oid_to_hex(&cur_oid),
2451 stop_progress(&progress);
2453 local_error = verify_commit_graph_error;
2455 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2456 local_error |= verify_commit_graph(r, g->base_graph, flags);
2461 void free_commit_graph(struct commit_graph *g)
2466 munmap((void *)g->data, g->data_len);
2470 free(g->bloom_filter_settings);
2474 void disable_commit_graph(struct repository *r)
2476 r->commit_graph_disabled = 1;