4 #include "git-compat-util.h"
12 #include "sha1-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
17 #include "replace-object.h"
20 #include "commit-slab.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 /* lower generation commits first */
148 if (a->generation < b->generation)
150 else if (a->generation > b->generation)
153 /* use date as a heuristic when generations are equal */
154 if (a->date < b->date)
156 else if (a->date > b->date)
161 char *get_commit_graph_filename(struct object_directory *obj_dir)
163 return xstrfmt("%s/info/commit-graph", obj_dir->path);
166 static char *get_split_graph_filename(struct object_directory *odb,
169 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
173 static char *get_chain_filename(struct object_directory *odb)
175 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
178 static uint8_t oid_version(void)
183 static struct commit_graph *alloc_commit_graph(void)
185 struct commit_graph *g = xcalloc(1, sizeof(*g));
190 extern int read_replace_refs;
192 static int commit_graph_compatible(struct repository *r)
197 if (read_replace_refs) {
198 prepare_replace_object(r);
199 if (hashmap_get_size(&r->objects->replace_map->map))
203 prepare_commit_graft(r);
204 if (r->parsed_objects && r->parsed_objects->grafts_nr)
206 if (is_repository_shallow(r))
212 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
214 *fd = git_open(graph_file);
217 if (fstat(*fd, st)) {
224 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
225 struct object_directory *odb)
229 struct commit_graph *ret;
231 graph_size = xsize_t(st->st_size);
233 if (graph_size < GRAPH_MIN_SIZE) {
235 error(_("commit-graph file is too small"));
238 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
240 ret = parse_commit_graph(graph_map, graph_size);
245 munmap(graph_map, graph_size);
250 static int verify_commit_graph_lite(struct commit_graph *g)
253 * Basic validation shared between parse_commit_graph()
254 * which'll be called every time the graph is used, and the
255 * much more expensive verify_commit_graph() used by
256 * "commit-graph verify".
258 * There should only be very basic checks here to ensure that
259 * we don't e.g. segfault in fill_commit_in_graph(), but
260 * because this is a very hot codepath nothing that e.g. loops
261 * over g->num_commits, or runs a checksum on the commit-graph
264 if (!g->chunk_oid_fanout) {
265 error("commit-graph is missing the OID Fanout chunk");
268 if (!g->chunk_oid_lookup) {
269 error("commit-graph is missing the OID Lookup chunk");
272 if (!g->chunk_commit_data) {
273 error("commit-graph is missing the Commit Data chunk");
280 struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size)
282 const unsigned char *data, *chunk_lookup;
284 struct commit_graph *graph;
285 uint64_t last_chunk_offset;
286 uint32_t last_chunk_id;
287 uint32_t graph_signature;
288 unsigned char graph_version, hash_version;
293 if (graph_size < GRAPH_MIN_SIZE)
296 data = (const unsigned char *)graph_map;
298 graph_signature = get_be32(data);
299 if (graph_signature != GRAPH_SIGNATURE) {
300 error(_("commit-graph signature %X does not match signature %X"),
301 graph_signature, GRAPH_SIGNATURE);
305 graph_version = *(unsigned char*)(data + 4);
306 if (graph_version != GRAPH_VERSION) {
307 error(_("commit-graph version %X does not match version %X"),
308 graph_version, GRAPH_VERSION);
312 hash_version = *(unsigned char*)(data + 5);
313 if (hash_version != oid_version()) {
314 error(_("commit-graph hash version %X does not match version %X"),
315 hash_version, oid_version());
319 graph = alloc_commit_graph();
321 graph->hash_len = the_hash_algo->rawsz;
322 graph->num_chunks = *(unsigned char*)(data + 6);
323 graph->data = graph_map;
324 graph->data_len = graph_size;
327 last_chunk_offset = 8;
328 chunk_lookup = data + 8;
329 for (i = 0; i < graph->num_chunks; i++) {
331 uint64_t chunk_offset;
332 int chunk_repeated = 0;
334 if (data + graph_size - chunk_lookup <
335 GRAPH_CHUNKLOOKUP_WIDTH) {
336 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
337 goto free_and_return;
340 chunk_id = get_be32(chunk_lookup + 0);
341 chunk_offset = get_be64(chunk_lookup + 4);
343 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
345 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
346 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
347 (uint32_t)chunk_offset);
348 goto free_and_return;
352 case GRAPH_CHUNKID_OIDFANOUT:
353 if (graph->chunk_oid_fanout)
356 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
359 case GRAPH_CHUNKID_OIDLOOKUP:
360 if (graph->chunk_oid_lookup)
363 graph->chunk_oid_lookup = data + chunk_offset;
366 case GRAPH_CHUNKID_DATA:
367 if (graph->chunk_commit_data)
370 graph->chunk_commit_data = data + chunk_offset;
373 case GRAPH_CHUNKID_EXTRAEDGES:
374 if (graph->chunk_extra_edges)
377 graph->chunk_extra_edges = data + chunk_offset;
380 case GRAPH_CHUNKID_BASE:
381 if (graph->chunk_base_graphs)
384 graph->chunk_base_graphs = data + chunk_offset;
387 case GRAPH_CHUNKID_BLOOMINDEXES:
388 if (graph->chunk_bloom_indexes)
391 graph->chunk_bloom_indexes = data + chunk_offset;
394 case GRAPH_CHUNKID_BLOOMDATA:
395 if (graph->chunk_bloom_data)
398 uint32_t hash_version;
399 graph->chunk_bloom_data = data + chunk_offset;
400 hash_version = get_be32(data + chunk_offset);
402 if (hash_version != 1)
405 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
406 graph->bloom_filter_settings->hash_version = hash_version;
407 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
408 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
413 if (chunk_repeated) {
414 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
415 goto free_and_return;
418 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
420 graph->num_commits = (chunk_offset - last_chunk_offset)
424 last_chunk_id = chunk_id;
425 last_chunk_offset = chunk_offset;
428 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
429 init_bloom_filters();
431 /* We need both the bloom chunks to exist together. Else ignore the data */
432 graph->chunk_bloom_indexes = NULL;
433 graph->chunk_bloom_data = NULL;
434 FREE_AND_NULL(graph->bloom_filter_settings);
437 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
439 if (verify_commit_graph_lite(graph))
440 goto free_and_return;
445 free(graph->bloom_filter_settings);
450 static struct commit_graph *load_commit_graph_one(const char *graph_file,
451 struct object_directory *odb)
456 struct commit_graph *g;
457 int open_ok = open_commit_graph(graph_file, &fd, &st);
462 g = load_commit_graph_one_fd_st(fd, &st, odb);
465 g->filename = xstrdup(graph_file);
470 static struct commit_graph *load_commit_graph_v1(struct repository *r,
471 struct object_directory *odb)
473 char *graph_name = get_commit_graph_filename(odb);
474 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
480 static int add_graph_to_chain(struct commit_graph *g,
481 struct commit_graph *chain,
482 struct object_id *oids,
485 struct commit_graph *cur_g = chain;
487 if (n && !g->chunk_base_graphs) {
488 warning(_("commit-graph has no base graphs chunk"));
496 !oideq(&oids[n], &cur_g->oid) ||
497 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
498 warning(_("commit-graph chain does not match"));
502 cur_g = cur_g->base_graph;
505 g->base_graph = chain;
508 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
513 static struct commit_graph *load_commit_graph_chain(struct repository *r,
514 struct object_directory *odb)
516 struct commit_graph *graph_chain = NULL;
517 struct strbuf line = STRBUF_INIT;
519 struct object_id *oids;
520 int i = 0, valid = 1, count;
521 char *chain_name = get_chain_filename(odb);
525 fp = fopen(chain_name, "r");
526 stat_res = stat(chain_name, &st);
531 st.st_size <= the_hash_algo->hexsz)
534 count = st.st_size / (the_hash_algo->hexsz + 1);
535 oids = xcalloc(count, sizeof(struct object_id));
539 for (i = 0; i < count; i++) {
540 struct object_directory *odb;
542 if (strbuf_getline_lf(&line, fp) == EOF)
545 if (get_oid_hex(line.buf, &oids[i])) {
546 warning(_("invalid commit-graph chain: line '%s' not a hash"),
553 for (odb = r->objects->odb; odb; odb = odb->next) {
554 char *graph_name = get_split_graph_filename(odb, line.buf);
555 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
560 if (add_graph_to_chain(g, graph_chain, oids, i)) {
570 warning(_("unable to find all commit-graph files"));
577 strbuf_release(&line);
582 struct commit_graph *read_commit_graph_one(struct repository *r,
583 struct object_directory *odb)
585 struct commit_graph *g = load_commit_graph_v1(r, odb);
588 g = load_commit_graph_chain(r, odb);
593 static void prepare_commit_graph_one(struct repository *r,
594 struct object_directory *odb)
597 if (r->objects->commit_graph)
600 r->objects->commit_graph = read_commit_graph_one(r, odb);
604 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
606 * On the first invocation, this function attempts to load the commit
607 * graph if the_repository is configured to have one.
609 static int prepare_commit_graph(struct repository *r)
611 struct object_directory *odb;
614 * This must come before the "already attempted?" check below, because
615 * we want to disable even an already-loaded graph file.
617 if (r->commit_graph_disabled)
620 if (r->objects->commit_graph_attempted)
621 return !!r->objects->commit_graph;
622 r->objects->commit_graph_attempted = 1;
624 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
625 die("dying as requested by the '%s' variable on commit-graph load!",
626 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
628 prepare_repo_settings(r);
630 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
631 r->settings.core_commit_graph != 1)
633 * This repository is not configured to use commit graphs, so
634 * do not load one. (But report commit_graph_attempted anyway
635 * so that commit graph loading is not attempted again for this
640 if (!commit_graph_compatible(r))
644 for (odb = r->objects->odb;
645 !r->objects->commit_graph && odb;
647 prepare_commit_graph_one(r, odb);
648 return !!r->objects->commit_graph;
651 int generation_numbers_enabled(struct repository *r)
653 uint32_t first_generation;
654 struct commit_graph *g;
655 if (!prepare_commit_graph(r))
658 g = r->objects->commit_graph;
663 first_generation = get_be32(g->chunk_commit_data +
664 g->hash_len + 8) >> 2;
666 return !!first_generation;
669 static void close_commit_graph_one(struct commit_graph *g)
674 close_commit_graph_one(g->base_graph);
675 free_commit_graph(g);
678 void close_commit_graph(struct raw_object_store *o)
680 close_commit_graph_one(o->commit_graph);
681 o->commit_graph = NULL;
684 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
686 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
687 g->chunk_oid_lookup, g->hash_len, pos);
690 static void load_oid_from_graph(struct commit_graph *g,
692 struct object_id *oid)
696 while (g && pos < g->num_commits_in_base)
700 BUG("NULL commit-graph");
702 if (pos >= g->num_commits + g->num_commits_in_base)
703 die(_("invalid commit position. commit-graph is likely corrupt"));
705 lex_index = pos - g->num_commits_in_base;
707 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
710 static struct commit_list **insert_parent_or_die(struct repository *r,
711 struct commit_graph *g,
713 struct commit_list **pptr)
716 struct object_id oid;
718 if (pos >= g->num_commits + g->num_commits_in_base)
719 die("invalid parent position %"PRIu32, pos);
721 load_oid_from_graph(g, pos, &oid);
722 c = lookup_commit(r, &oid);
724 die(_("could not find commit %s"), oid_to_hex(&oid));
726 return &commit_list_insert(c, pptr)->next;
729 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
731 const unsigned char *commit_data;
734 while (pos < g->num_commits_in_base)
737 lex_index = pos - g->num_commits_in_base;
738 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
739 item->graph_pos = pos;
740 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
743 static inline void set_commit_tree(struct commit *c, struct tree *t)
748 static int fill_commit_in_graph(struct repository *r,
750 struct commit_graph *g, uint32_t pos)
753 uint32_t *parent_data_ptr;
754 uint64_t date_low, date_high;
755 struct commit_list **pptr;
756 const unsigned char *commit_data;
759 while (pos < g->num_commits_in_base)
762 if (pos >= g->num_commits + g->num_commits_in_base)
763 die(_("invalid commit position. commit-graph is likely corrupt"));
766 * Store the "full" position, but then use the
767 * "local" position for the rest of the calculation.
769 item->graph_pos = pos;
770 lex_index = pos - g->num_commits_in_base;
772 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
774 item->object.parsed = 1;
776 set_commit_tree(item, NULL);
778 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
779 date_low = get_be32(commit_data + g->hash_len + 12);
780 item->date = (timestamp_t)((date_high << 32) | date_low);
782 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
784 pptr = &item->parents;
786 edge_value = get_be32(commit_data + g->hash_len);
787 if (edge_value == GRAPH_PARENT_NONE)
789 pptr = insert_parent_or_die(r, g, edge_value, pptr);
791 edge_value = get_be32(commit_data + g->hash_len + 4);
792 if (edge_value == GRAPH_PARENT_NONE)
794 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
795 pptr = insert_parent_or_die(r, g, edge_value, pptr);
799 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
800 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
802 edge_value = get_be32(parent_data_ptr);
803 pptr = insert_parent_or_die(r, g,
804 edge_value & GRAPH_EDGE_LAST_MASK,
807 } while (!(edge_value & GRAPH_LAST_EDGE));
812 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
814 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
815 *pos = item->graph_pos;
818 struct commit_graph *cur_g = g;
821 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
822 cur_g = cur_g->base_graph;
825 *pos = lex_index + cur_g->num_commits_in_base;
833 static int parse_commit_in_graph_one(struct repository *r,
834 struct commit_graph *g,
839 if (item->object.parsed)
842 if (find_commit_in_graph(item, g, &pos))
843 return fill_commit_in_graph(r, item, g, pos);
848 int parse_commit_in_graph(struct repository *r, struct commit *item)
850 if (!prepare_commit_graph(r))
852 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
855 void load_commit_graph_info(struct repository *r, struct commit *item)
858 if (!prepare_commit_graph(r))
860 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
861 fill_commit_graph_info(item, r->objects->commit_graph, pos);
864 static struct tree *load_tree_for_commit(struct repository *r,
865 struct commit_graph *g,
868 struct object_id oid;
869 const unsigned char *commit_data;
871 while (c->graph_pos < g->num_commits_in_base)
874 commit_data = g->chunk_commit_data +
875 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
877 hashcpy(oid.hash, commit_data);
878 set_commit_tree(c, lookup_tree(r, &oid));
880 return c->maybe_tree;
883 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
884 struct commit_graph *g,
885 const struct commit *c)
888 return c->maybe_tree;
889 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
890 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
892 return load_tree_for_commit(r, g, (struct commit *)c);
895 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
897 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
900 struct packed_commit_list {
901 struct commit **list;
906 struct packed_oid_list {
907 struct object_id *list;
912 struct write_commit_graph_context {
913 struct repository *r;
914 struct object_directory *odb;
916 struct packed_oid_list oids;
917 struct packed_commit_list commits;
919 unsigned long approx_nr_objects;
920 struct progress *progress;
922 uint64_t progress_cnt;
924 char *base_graph_name;
925 int num_commit_graphs_before;
926 int num_commit_graphs_after;
927 char **commit_graph_filenames_before;
928 char **commit_graph_filenames_after;
929 char **commit_graph_hash_after;
930 uint32_t new_num_commits_in_base;
931 struct commit_graph *new_base_graph;
939 const struct split_commit_graph_opts *split_opts;
940 size_t total_bloom_filter_data_size;
943 static void write_graph_chunk_fanout(struct hashfile *f,
944 struct write_commit_graph_context *ctx)
947 struct commit **list = ctx->commits.list;
950 * Write the first-level table (the list is sorted,
951 * but we use a 256-entry lookup to be able to avoid
952 * having to do eight extra binary search iterations).
954 for (i = 0; i < 256; i++) {
955 while (count < ctx->commits.nr) {
956 if ((*list)->object.oid.hash[0] != i)
958 display_progress(ctx->progress, ++ctx->progress_cnt);
963 hashwrite_be32(f, count);
967 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
968 struct write_commit_graph_context *ctx)
970 struct commit **list = ctx->commits.list;
972 for (count = 0; count < ctx->commits.nr; count++, list++) {
973 display_progress(ctx->progress, ++ctx->progress_cnt);
974 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
978 static const unsigned char *commit_to_sha1(size_t index, void *table)
980 struct commit **commits = table;
981 return commits[index]->object.oid.hash;
984 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
985 struct write_commit_graph_context *ctx)
987 struct commit **list = ctx->commits.list;
988 struct commit **last = ctx->commits.list + ctx->commits.nr;
989 uint32_t num_extra_edges = 0;
991 while (list < last) {
992 struct commit_list *parent;
993 struct object_id *tree;
995 uint32_t packedDate[2];
996 display_progress(ctx->progress, ++ctx->progress_cnt);
998 if (parse_commit_no_graph(*list))
999 die(_("unable to parse commit %s"),
1000 oid_to_hex(&(*list)->object.oid));
1001 tree = get_commit_tree_oid(*list);
1002 hashwrite(f, tree->hash, hash_len);
1004 parent = (*list)->parents;
1007 edge_value = GRAPH_PARENT_NONE;
1009 edge_value = sha1_pos(parent->item->object.oid.hash,
1014 if (edge_value >= 0)
1015 edge_value += ctx->new_num_commits_in_base;
1016 else if (ctx->new_base_graph) {
1018 if (find_commit_in_graph(parent->item,
1019 ctx->new_base_graph,
1025 BUG("missing parent %s for commit %s",
1026 oid_to_hex(&parent->item->object.oid),
1027 oid_to_hex(&(*list)->object.oid));
1030 hashwrite_be32(f, edge_value);
1033 parent = parent->next;
1036 edge_value = GRAPH_PARENT_NONE;
1037 else if (parent->next)
1038 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1040 edge_value = sha1_pos(parent->item->object.oid.hash,
1045 if (edge_value >= 0)
1046 edge_value += ctx->new_num_commits_in_base;
1047 else if (ctx->new_base_graph) {
1049 if (find_commit_in_graph(parent->item,
1050 ctx->new_base_graph,
1056 BUG("missing parent %s for commit %s",
1057 oid_to_hex(&parent->item->object.oid),
1058 oid_to_hex(&(*list)->object.oid));
1061 hashwrite_be32(f, edge_value);
1063 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1066 parent = parent->next;
1070 if (sizeof((*list)->date) > 4)
1071 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1075 packedDate[0] |= htonl(commit_graph_data_at(*list)->generation << 2);
1077 packedDate[1] = htonl((*list)->date);
1078 hashwrite(f, packedDate, 8);
1084 static void write_graph_chunk_extra_edges(struct hashfile *f,
1085 struct write_commit_graph_context *ctx)
1087 struct commit **list = ctx->commits.list;
1088 struct commit **last = ctx->commits.list + ctx->commits.nr;
1089 struct commit_list *parent;
1091 while (list < last) {
1092 int num_parents = 0;
1094 display_progress(ctx->progress, ++ctx->progress_cnt);
1096 for (parent = (*list)->parents; num_parents < 3 && parent;
1097 parent = parent->next)
1100 if (num_parents <= 2) {
1105 /* Since num_parents > 2, this initializer is safe. */
1106 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1107 int edge_value = sha1_pos(parent->item->object.oid.hash,
1112 if (edge_value >= 0)
1113 edge_value += ctx->new_num_commits_in_base;
1114 else if (ctx->new_base_graph) {
1116 if (find_commit_in_graph(parent->item,
1117 ctx->new_base_graph,
1123 BUG("missing parent %s for commit %s",
1124 oid_to_hex(&parent->item->object.oid),
1125 oid_to_hex(&(*list)->object.oid));
1126 else if (!parent->next)
1127 edge_value |= GRAPH_LAST_EDGE;
1129 hashwrite_be32(f, edge_value);
1136 static void write_graph_chunk_bloom_indexes(struct hashfile *f,
1137 struct write_commit_graph_context *ctx)
1139 struct commit **list = ctx->commits.list;
1140 struct commit **last = ctx->commits.list + ctx->commits.nr;
1141 uint32_t cur_pos = 0;
1142 struct progress *progress = NULL;
1145 if (ctx->report_progress)
1146 progress = start_delayed_progress(
1147 _("Writing changed paths Bloom filters index"),
1150 while (list < last) {
1151 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
1152 cur_pos += filter->len;
1153 display_progress(progress, ++i);
1154 hashwrite_be32(f, cur_pos);
1158 stop_progress(&progress);
1161 static void write_graph_chunk_bloom_data(struct hashfile *f,
1162 struct write_commit_graph_context *ctx,
1163 const struct bloom_filter_settings *settings)
1165 struct commit **list = ctx->commits.list;
1166 struct commit **last = ctx->commits.list + ctx->commits.nr;
1167 struct progress *progress = NULL;
1170 if (ctx->report_progress)
1171 progress = start_delayed_progress(
1172 _("Writing changed paths Bloom filters data"),
1175 hashwrite_be32(f, settings->hash_version);
1176 hashwrite_be32(f, settings->num_hashes);
1177 hashwrite_be32(f, settings->bits_per_entry);
1179 while (list < last) {
1180 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
1181 display_progress(progress, ++i);
1182 hashwrite(f, filter->data, filter->len * sizeof(unsigned char));
1186 stop_progress(&progress);
1189 static int oid_compare(const void *_a, const void *_b)
1191 const struct object_id *a = (const struct object_id *)_a;
1192 const struct object_id *b = (const struct object_id *)_b;
1193 return oidcmp(a, b);
1196 static int add_packed_commits(const struct object_id *oid,
1197 struct packed_git *pack,
1201 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1202 enum object_type type;
1203 off_t offset = nth_packed_object_offset(pack, pos);
1204 struct object_info oi = OBJECT_INFO_INIT;
1207 display_progress(ctx->progress, ++ctx->progress_done);
1210 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1211 die(_("unable to get type of object %s"), oid_to_hex(oid));
1213 if (type != OBJ_COMMIT)
1216 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1217 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1220 set_commit_pos(ctx->r, oid);
1225 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1227 struct commit_list *parent;
1228 for (parent = commit->parents; parent; parent = parent->next) {
1229 if (!(parent->item->object.flags & REACHABLE)) {
1230 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1231 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1233 parent->item->object.flags |= REACHABLE;
1238 static void close_reachable(struct write_commit_graph_context *ctx)
1241 struct commit *commit;
1242 enum commit_graph_split_flags flags = ctx->split_opts ?
1243 ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1245 if (ctx->report_progress)
1246 ctx->progress = start_delayed_progress(
1247 _("Loading known commits in commit graph"),
1249 for (i = 0; i < ctx->oids.nr; i++) {
1250 display_progress(ctx->progress, i + 1);
1251 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1253 commit->object.flags |= REACHABLE;
1255 stop_progress(&ctx->progress);
1258 * As this loop runs, ctx->oids.nr may grow, but not more
1259 * than the number of missing commits in the reachable
1262 if (ctx->report_progress)
1263 ctx->progress = start_delayed_progress(
1264 _("Expanding reachable commits in commit graph"),
1266 for (i = 0; i < ctx->oids.nr; i++) {
1267 display_progress(ctx->progress, i + 1);
1268 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1273 if ((!parse_commit(commit) &&
1274 commit->graph_pos == COMMIT_NOT_FROM_GRAPH) ||
1275 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1276 add_missing_parents(ctx, commit);
1277 } else if (!parse_commit_no_graph(commit))
1278 add_missing_parents(ctx, commit);
1280 stop_progress(&ctx->progress);
1282 if (ctx->report_progress)
1283 ctx->progress = start_delayed_progress(
1284 _("Clearing commit marks in commit graph"),
1286 for (i = 0; i < ctx->oids.nr; i++) {
1287 display_progress(ctx->progress, i + 1);
1288 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1291 commit->object.flags &= ~REACHABLE;
1293 stop_progress(&ctx->progress);
1296 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1299 struct commit_list *list = NULL;
1301 if (ctx->report_progress)
1302 ctx->progress = start_delayed_progress(
1303 _("Computing commit graph generation numbers"),
1305 for (i = 0; i < ctx->commits.nr; i++) {
1306 uint32_t generation = commit_graph_data_at(ctx->commits.list[i])->generation;
1308 display_progress(ctx->progress, i + 1);
1309 if (generation != GENERATION_NUMBER_INFINITY &&
1310 generation != GENERATION_NUMBER_ZERO)
1313 commit_list_insert(ctx->commits.list[i], &list);
1315 struct commit *current = list->item;
1316 struct commit_list *parent;
1317 int all_parents_computed = 1;
1318 uint32_t max_generation = 0;
1320 for (parent = current->parents; parent; parent = parent->next) {
1321 generation = commit_graph_data_at(parent->item)->generation;
1323 if (generation == GENERATION_NUMBER_INFINITY ||
1324 generation == GENERATION_NUMBER_ZERO) {
1325 all_parents_computed = 0;
1326 commit_list_insert(parent->item, &list);
1328 } else if (generation > max_generation) {
1329 max_generation = generation;
1333 if (all_parents_computed) {
1334 struct commit_graph_data *data = commit_graph_data_at(current);
1336 data->generation = max_generation + 1;
1339 if (data->generation > GENERATION_NUMBER_MAX)
1340 data->generation = GENERATION_NUMBER_MAX;
1344 stop_progress(&ctx->progress);
1347 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1350 struct progress *progress = NULL;
1351 struct commit **sorted_commits;
1353 init_bloom_filters();
1355 if (ctx->report_progress)
1356 progress = start_delayed_progress(
1357 _("Computing commit changed paths Bloom filters"),
1360 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1361 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1363 if (ctx->order_by_pack)
1364 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1366 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1368 for (i = 0; i < ctx->commits.nr; i++) {
1369 struct commit *c = sorted_commits[i];
1370 struct bloom_filter *filter = get_bloom_filter(ctx->r, c, 1);
1371 ctx->total_bloom_filter_data_size += sizeof(unsigned char) * filter->len;
1372 display_progress(progress, i + 1);
1375 free(sorted_commits);
1376 stop_progress(&progress);
1379 struct refs_cb_data {
1380 struct oidset *commits;
1381 struct progress *progress;
1384 static int add_ref_to_set(const char *refname,
1385 const struct object_id *oid,
1386 int flags, void *cb_data)
1388 struct object_id peeled;
1389 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1391 if (!peel_ref(refname, &peeled))
1393 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1394 oidset_insert(data->commits, oid);
1396 display_progress(data->progress, oidset_size(data->commits));
1401 int write_commit_graph_reachable(struct object_directory *odb,
1402 enum commit_graph_write_flags flags,
1403 const struct split_commit_graph_opts *split_opts)
1405 struct oidset commits = OIDSET_INIT;
1406 struct refs_cb_data data;
1409 memset(&data, 0, sizeof(data));
1410 data.commits = &commits;
1411 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1412 data.progress = start_delayed_progress(
1413 _("Collecting referenced commits"), 0);
1415 for_each_ref(add_ref_to_set, &data);
1416 result = write_commit_graph(odb, NULL, &commits,
1419 oidset_clear(&commits);
1421 stop_progress(&data.progress);
1425 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1426 struct string_list *pack_indexes)
1429 struct strbuf progress_title = STRBUF_INIT;
1430 struct strbuf packname = STRBUF_INIT;
1433 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1434 dirlen = packname.len;
1435 if (ctx->report_progress) {
1436 strbuf_addf(&progress_title,
1437 Q_("Finding commits for commit graph in %d pack",
1438 "Finding commits for commit graph in %d packs",
1441 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1442 ctx->progress_done = 0;
1444 for (i = 0; i < pack_indexes->nr; i++) {
1445 struct packed_git *p;
1446 strbuf_setlen(&packname, dirlen);
1447 strbuf_addstr(&packname, pack_indexes->items[i].string);
1448 p = add_packed_git(packname.buf, packname.len, 1);
1450 error(_("error adding pack %s"), packname.buf);
1453 if (open_pack_index(p)) {
1454 error(_("error opening index for %s"), packname.buf);
1457 for_each_object_in_pack(p, add_packed_commits, ctx,
1458 FOR_EACH_OBJECT_PACK_ORDER);
1463 stop_progress(&ctx->progress);
1464 strbuf_release(&progress_title);
1465 strbuf_release(&packname);
1470 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1471 struct oidset *commits)
1473 struct oidset_iter iter;
1474 struct object_id *oid;
1476 if (!oidset_size(commits))
1479 oidset_iter_init(commits, &iter);
1480 while ((oid = oidset_iter_next(&iter))) {
1481 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1482 oidcpy(&ctx->oids.list[ctx->oids.nr], oid);
1489 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1491 if (ctx->report_progress)
1492 ctx->progress = start_delayed_progress(
1493 _("Finding commits for commit graph among packed objects"),
1494 ctx->approx_nr_objects);
1495 for_each_packed_object(add_packed_commits, ctx,
1496 FOR_EACH_OBJECT_PACK_ORDER);
1497 if (ctx->progress_done < ctx->approx_nr_objects)
1498 display_progress(ctx->progress, ctx->approx_nr_objects);
1499 stop_progress(&ctx->progress);
1502 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1504 uint32_t i, count_distinct = 1;
1506 if (ctx->report_progress)
1507 ctx->progress = start_delayed_progress(
1508 _("Counting distinct commits in commit graph"),
1510 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1511 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1513 for (i = 1; i < ctx->oids.nr; i++) {
1514 display_progress(ctx->progress, i + 1);
1515 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1517 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1519 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1526 stop_progress(&ctx->progress);
1528 return count_distinct;
1531 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1534 enum commit_graph_split_flags flags = ctx->split_opts ?
1535 ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1537 ctx->num_extra_edges = 0;
1538 if (ctx->report_progress)
1539 ctx->progress = start_delayed_progress(
1540 _("Finding extra edges in commit graph"),
1542 for (i = 0; i < ctx->oids.nr; i++) {
1543 unsigned int num_parents;
1545 display_progress(ctx->progress, i + 1);
1546 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1549 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1550 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1552 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1553 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1556 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1557 parse_commit(ctx->commits.list[ctx->commits.nr]);
1559 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1561 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1562 if (num_parents > 2)
1563 ctx->num_extra_edges += num_parents - 1;
1567 stop_progress(&ctx->progress);
1570 static int write_graph_chunk_base_1(struct hashfile *f,
1571 struct commit_graph *g)
1578 num = write_graph_chunk_base_1(f, g->base_graph);
1579 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1583 static int write_graph_chunk_base(struct hashfile *f,
1584 struct write_commit_graph_context *ctx)
1586 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1588 if (num != ctx->num_commit_graphs_after - 1) {
1589 error(_("failed to write correct number of base graph ids"));
1596 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1601 struct lock_file lk = LOCK_INIT;
1602 uint32_t chunk_ids[MAX_NUM_CHUNKS + 1];
1603 uint64_t chunk_offsets[MAX_NUM_CHUNKS + 1];
1604 const unsigned hashsz = the_hash_algo->rawsz;
1605 struct strbuf progress_title = STRBUF_INIT;
1607 struct object_id file_hash;
1608 const struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
1611 struct strbuf tmp_file = STRBUF_INIT;
1613 strbuf_addf(&tmp_file,
1614 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1616 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1618 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1621 if (safe_create_leading_directories(ctx->graph_name)) {
1622 UNLEAK(ctx->graph_name);
1623 error(_("unable to create leading directories of %s"),
1629 char *lock_name = get_chain_filename(ctx->odb);
1631 hold_lock_file_for_update_mode(&lk, lock_name,
1632 LOCK_DIE_ON_ERROR, 0444);
1634 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1636 error(_("unable to create temporary graph layer"));
1640 if (adjust_shared_perm(ctx->graph_name)) {
1641 error(_("unable to adjust shared permissions for '%s'"),
1646 f = hashfd(fd, ctx->graph_name);
1648 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1649 LOCK_DIE_ON_ERROR, 0444);
1650 fd = lk.tempfile->fd;
1651 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1654 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1655 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1656 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1657 if (ctx->num_extra_edges) {
1658 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1661 if (ctx->changed_paths) {
1662 chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMINDEXES;
1664 chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMDATA;
1667 if (ctx->num_commit_graphs_after > 1) {
1668 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1672 chunk_ids[num_chunks] = 0;
1674 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1675 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1676 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1677 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1680 if (ctx->num_extra_edges) {
1681 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1682 4 * ctx->num_extra_edges;
1685 if (ctx->changed_paths) {
1686 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1687 sizeof(uint32_t) * ctx->commits.nr;
1690 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1691 sizeof(uint32_t) * 3 + ctx->total_bloom_filter_data_size;
1694 if (ctx->num_commit_graphs_after > 1) {
1695 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1696 hashsz * (ctx->num_commit_graphs_after - 1);
1700 hashwrite_be32(f, GRAPH_SIGNATURE);
1702 hashwrite_u8(f, GRAPH_VERSION);
1703 hashwrite_u8(f, oid_version());
1704 hashwrite_u8(f, num_chunks);
1705 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1707 for (i = 0; i <= num_chunks; i++) {
1708 uint32_t chunk_write[3];
1710 chunk_write[0] = htonl(chunk_ids[i]);
1711 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1712 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1713 hashwrite(f, chunk_write, 12);
1716 if (ctx->report_progress) {
1717 strbuf_addf(&progress_title,
1718 Q_("Writing out commit graph in %d pass",
1719 "Writing out commit graph in %d passes",
1722 ctx->progress = start_delayed_progress(
1724 num_chunks * ctx->commits.nr);
1726 write_graph_chunk_fanout(f, ctx);
1727 write_graph_chunk_oids(f, hashsz, ctx);
1728 write_graph_chunk_data(f, hashsz, ctx);
1729 if (ctx->num_extra_edges)
1730 write_graph_chunk_extra_edges(f, ctx);
1731 if (ctx->changed_paths) {
1732 write_graph_chunk_bloom_indexes(f, ctx);
1733 write_graph_chunk_bloom_data(f, ctx, &bloom_settings);
1735 if (ctx->num_commit_graphs_after > 1 &&
1736 write_graph_chunk_base(f, ctx)) {
1739 stop_progress(&ctx->progress);
1740 strbuf_release(&progress_title);
1742 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1743 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1744 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1746 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1747 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1748 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1749 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1752 close_commit_graph(ctx->r->objects);
1753 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1756 FILE *chainf = fdopen_lock_file(&lk, "w");
1757 char *final_graph_name;
1763 error(_("unable to open commit-graph chain file"));
1767 if (ctx->base_graph_name) {
1769 int idx = ctx->num_commit_graphs_after - 1;
1770 if (ctx->num_commit_graphs_after > 1)
1773 dest = ctx->commit_graph_filenames_after[idx];
1775 if (strcmp(ctx->base_graph_name, dest)) {
1776 result = rename(ctx->base_graph_name, dest);
1779 error(_("failed to rename base commit-graph file"));
1784 char *graph_name = get_commit_graph_filename(ctx->odb);
1788 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1789 final_graph_name = get_split_graph_filename(ctx->odb,
1790 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1791 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1793 result = rename(ctx->graph_name, final_graph_name);
1795 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1796 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1799 error(_("failed to rename temporary commit-graph file"));
1804 commit_lock_file(&lk);
1809 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1811 struct commit_graph *g;
1812 uint32_t num_commits;
1813 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1816 int max_commits = 0;
1819 if (ctx->split_opts) {
1820 max_commits = ctx->split_opts->max_commits;
1822 if (ctx->split_opts->size_multiple)
1823 size_mult = ctx->split_opts->size_multiple;
1825 flags = ctx->split_opts->flags;
1828 g = ctx->r->objects->commit_graph;
1829 num_commits = ctx->commits.nr;
1830 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1831 ctx->num_commit_graphs_after = 1;
1833 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1835 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1836 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
1837 while (g && (g->num_commits <= size_mult * num_commits ||
1838 (max_commits && num_commits > max_commits))) {
1839 if (g->odb != ctx->odb)
1842 num_commits += g->num_commits;
1845 ctx->num_commit_graphs_after--;
1849 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
1850 ctx->new_base_graph = g;
1851 else if (ctx->num_commit_graphs_after != 1)
1852 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1853 "should be 1 with --split=replace");
1855 if (ctx->num_commit_graphs_after == 2) {
1856 char *old_graph_name = get_commit_graph_filename(g->odb);
1858 if (!strcmp(g->filename, old_graph_name) &&
1859 g->odb != ctx->odb) {
1860 ctx->num_commit_graphs_after = 1;
1861 ctx->new_base_graph = NULL;
1864 free(old_graph_name);
1867 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1868 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1870 for (i = 0; i < ctx->num_commit_graphs_after &&
1871 i < ctx->num_commit_graphs_before; i++)
1872 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1874 i = ctx->num_commit_graphs_before - 1;
1875 g = ctx->r->objects->commit_graph;
1878 if (i < ctx->num_commit_graphs_after)
1879 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1886 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1887 struct commit_graph *g)
1890 uint32_t offset = g->num_commits_in_base;
1892 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1894 for (i = 0; i < g->num_commits; i++) {
1895 struct object_id oid;
1896 struct commit *result;
1898 display_progress(ctx->progress, i + 1);
1900 load_oid_from_graph(g, i + offset, &oid);
1902 /* only add commits if they still exist in the repo */
1903 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1906 ctx->commits.list[ctx->commits.nr] = result;
1912 static int commit_compare(const void *_a, const void *_b)
1914 const struct commit *a = *(const struct commit **)_a;
1915 const struct commit *b = *(const struct commit **)_b;
1916 return oidcmp(&a->object.oid, &b->object.oid);
1919 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1923 if (ctx->report_progress)
1924 ctx->progress = start_delayed_progress(
1925 _("Scanning merged commits"),
1928 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1930 ctx->num_extra_edges = 0;
1931 for (i = 0; i < ctx->commits.nr; i++) {
1932 display_progress(ctx->progress, i);
1934 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1935 &ctx->commits.list[i]->object.oid)) {
1936 die(_("unexpected duplicate commit id %s"),
1937 oid_to_hex(&ctx->commits.list[i]->object.oid));
1939 unsigned int num_parents;
1941 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1942 if (num_parents > 2)
1943 ctx->num_extra_edges += num_parents - 1;
1947 stop_progress(&ctx->progress);
1950 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1952 struct commit_graph *g = ctx->r->objects->commit_graph;
1953 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1955 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1956 current_graph_number--;
1958 if (ctx->report_progress)
1959 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
1961 merge_commit_graph(ctx, g);
1962 stop_progress(&ctx->progress);
1968 ctx->new_base_graph = g;
1969 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1972 if (ctx->new_base_graph)
1973 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1975 sort_and_scan_merged_commits(ctx);
1978 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1981 time_t now = time(NULL);
1983 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1985 struct utimbuf updated_time;
1987 stat(ctx->commit_graph_filenames_before[i], &st);
1989 updated_time.actime = st.st_atime;
1990 updated_time.modtime = now;
1991 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1995 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1997 struct strbuf path = STRBUF_INIT;
2001 timestamp_t expire_time = time(NULL);
2003 if (ctx->split_opts && ctx->split_opts->expire_time)
2004 expire_time = ctx->split_opts->expire_time;
2006 char *chain_file_name = get_chain_filename(ctx->odb);
2007 unlink(chain_file_name);
2008 free(chain_file_name);
2009 ctx->num_commit_graphs_after = 0;
2012 strbuf_addstr(&path, ctx->odb->path);
2013 strbuf_addstr(&path, "/info/commit-graphs");
2014 dir = opendir(path.buf);
2019 strbuf_addch(&path, '/');
2020 dirnamelen = path.len;
2021 while ((de = readdir(dir)) != NULL) {
2023 uint32_t i, found = 0;
2025 strbuf_setlen(&path, dirnamelen);
2026 strbuf_addstr(&path, de->d_name);
2028 stat(path.buf, &st);
2030 if (st.st_mtime > expire_time)
2032 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2035 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2036 if (!strcmp(ctx->commit_graph_filenames_after[i],
2048 strbuf_release(&path);
2051 int write_commit_graph(struct object_directory *odb,
2052 struct string_list *pack_indexes,
2053 struct oidset *commits,
2054 enum commit_graph_write_flags flags,
2055 const struct split_commit_graph_opts *split_opts)
2057 struct write_commit_graph_context *ctx;
2058 uint32_t i, count_distinct = 0;
2062 if (!commit_graph_compatible(the_repository))
2065 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
2066 ctx->r = the_repository;
2068 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2069 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2070 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2071 ctx->split_opts = split_opts;
2072 ctx->changed_paths = flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS ? 1 : 0;
2073 ctx->total_bloom_filter_data_size = 0;
2076 struct commit_graph *g;
2077 prepare_commit_graph(ctx->r);
2079 g = ctx->r->objects->commit_graph;
2082 ctx->num_commit_graphs_before++;
2086 if (ctx->num_commit_graphs_before) {
2087 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2088 i = ctx->num_commit_graphs_before;
2089 g = ctx->r->objects->commit_graph;
2092 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2097 if (ctx->split_opts)
2098 replace = ctx->split_opts->flags & COMMIT_GRAPH_SPLIT_REPLACE;
2101 ctx->approx_nr_objects = approximate_object_count();
2102 ctx->oids.alloc = ctx->approx_nr_objects / 32;
2104 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
2105 ctx->oids.alloc = split_opts->max_commits;
2108 prepare_commit_graph_one(ctx->r, ctx->odb);
2109 if (ctx->r->objects->commit_graph)
2110 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
2113 if (ctx->oids.alloc < 1024)
2114 ctx->oids.alloc = 1024;
2115 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
2117 if (ctx->append && ctx->r->objects->commit_graph) {
2118 struct commit_graph *g = ctx->r->objects->commit_graph;
2119 for (i = 0; i < g->num_commits; i++) {
2120 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
2121 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
2126 ctx->order_by_pack = 1;
2127 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2132 if ((res = fill_oids_from_commits(ctx, commits)))
2136 if (!pack_indexes && !commits) {
2137 ctx->order_by_pack = 1;
2138 fill_oids_from_all_packs(ctx);
2141 close_reachable(ctx);
2143 count_distinct = count_distinct_commits(ctx);
2145 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
2146 error(_("the commit graph format cannot write %d commits"), count_distinct);
2151 ctx->commits.alloc = count_distinct;
2152 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
2154 copy_oids_to_commits(ctx);
2156 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2157 error(_("too many commits to write graph"));
2162 if (!ctx->commits.nr && !replace)
2166 split_graph_merge_strategy(ctx);
2169 merge_commit_graphs(ctx);
2171 ctx->num_commit_graphs_after = 1;
2173 compute_generation_numbers(ctx);
2175 if (ctx->changed_paths)
2176 compute_bloom_filters(ctx);
2178 res = write_commit_graph_file(ctx);
2181 mark_commit_graphs(ctx);
2183 expire_commit_graphs(ctx);
2186 free(ctx->graph_name);
2187 free(ctx->commits.list);
2188 free(ctx->oids.list);
2190 if (ctx->commit_graph_filenames_after) {
2191 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2192 free(ctx->commit_graph_filenames_after[i]);
2193 free(ctx->commit_graph_hash_after[i]);
2196 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2197 free(ctx->commit_graph_filenames_before[i]);
2199 free(ctx->commit_graph_filenames_after);
2200 free(ctx->commit_graph_filenames_before);
2201 free(ctx->commit_graph_hash_after);
2209 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2210 static int verify_commit_graph_error;
2212 static void graph_report(const char *fmt, ...)
2216 verify_commit_graph_error = 1;
2218 vfprintf(stderr, fmt, ap);
2219 fprintf(stderr, "\n");
2223 #define GENERATION_ZERO_EXISTS 1
2224 #define GENERATION_NUMBER_EXISTS 2
2226 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2228 uint32_t i, cur_fanout_pos = 0;
2229 struct object_id prev_oid, cur_oid, checksum;
2230 int generation_zero = 0;
2233 struct progress *progress = NULL;
2234 int local_error = 0;
2237 graph_report("no commit-graph file loaded");
2241 verify_commit_graph_error = verify_commit_graph_lite(g);
2242 if (verify_commit_graph_error)
2243 return verify_commit_graph_error;
2245 devnull = open("/dev/null", O_WRONLY);
2246 f = hashfd(devnull, NULL);
2247 hashwrite(f, g->data, g->data_len - g->hash_len);
2248 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2249 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2250 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2251 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2254 for (i = 0; i < g->num_commits; i++) {
2255 struct commit *graph_commit;
2257 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2259 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2260 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2261 oid_to_hex(&prev_oid),
2262 oid_to_hex(&cur_oid));
2264 oidcpy(&prev_oid, &cur_oid);
2266 while (cur_oid.hash[0] > cur_fanout_pos) {
2267 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2269 if (i != fanout_value)
2270 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2271 cur_fanout_pos, fanout_value, i);
2275 graph_commit = lookup_commit(r, &cur_oid);
2276 if (!parse_commit_in_graph_one(r, g, graph_commit))
2277 graph_report(_("failed to parse commit %s from commit-graph"),
2278 oid_to_hex(&cur_oid));
2281 while (cur_fanout_pos < 256) {
2282 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2284 if (g->num_commits != fanout_value)
2285 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2286 cur_fanout_pos, fanout_value, i);
2291 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2292 return verify_commit_graph_error;
2294 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2295 progress = start_progress(_("Verifying commits in commit graph"),
2298 for (i = 0; i < g->num_commits; i++) {
2299 struct commit *graph_commit, *odb_commit;
2300 struct commit_list *graph_parents, *odb_parents;
2301 uint32_t max_generation = 0;
2303 display_progress(progress, i + 1);
2304 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2306 graph_commit = lookup_commit(r, &cur_oid);
2307 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2308 if (parse_commit_internal(odb_commit, 0, 0)) {
2309 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2310 oid_to_hex(&cur_oid));
2314 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2315 get_commit_tree_oid(odb_commit)))
2316 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2317 oid_to_hex(&cur_oid),
2318 oid_to_hex(get_commit_tree_oid(graph_commit)),
2319 oid_to_hex(get_commit_tree_oid(odb_commit)));
2321 graph_parents = graph_commit->parents;
2322 odb_parents = odb_commit->parents;
2324 while (graph_parents) {
2325 if (odb_parents == NULL) {
2326 graph_report(_("commit-graph parent list for commit %s is too long"),
2327 oid_to_hex(&cur_oid));
2331 /* parse parent in case it is in a base graph */
2332 parse_commit_in_graph_one(r, g, graph_parents->item);
2334 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2335 graph_report(_("commit-graph parent for %s is %s != %s"),
2336 oid_to_hex(&cur_oid),
2337 oid_to_hex(&graph_parents->item->object.oid),
2338 oid_to_hex(&odb_parents->item->object.oid));
2340 if (graph_parents->item->generation > max_generation)
2341 max_generation = graph_parents->item->generation;
2343 graph_parents = graph_parents->next;
2344 odb_parents = odb_parents->next;
2347 if (odb_parents != NULL)
2348 graph_report(_("commit-graph parent list for commit %s terminates early"),
2349 oid_to_hex(&cur_oid));
2351 if (!graph_commit->generation) {
2352 if (generation_zero == GENERATION_NUMBER_EXISTS)
2353 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2354 oid_to_hex(&cur_oid));
2355 generation_zero = GENERATION_ZERO_EXISTS;
2356 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2357 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2358 oid_to_hex(&cur_oid));
2360 if (generation_zero == GENERATION_ZERO_EXISTS)
2364 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2365 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2366 * extra logic in the following condition.
2368 if (max_generation == GENERATION_NUMBER_MAX)
2371 if (graph_commit->generation != max_generation + 1)
2372 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2373 oid_to_hex(&cur_oid),
2374 graph_commit->generation,
2375 max_generation + 1);
2377 if (graph_commit->date != odb_commit->date)
2378 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2379 oid_to_hex(&cur_oid),
2383 stop_progress(&progress);
2385 local_error = verify_commit_graph_error;
2387 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2388 local_error |= verify_commit_graph(r, g->base_graph, flags);
2393 void free_commit_graph(struct commit_graph *g)
2398 munmap((void *)g->data, g->data_len);
2402 free(g->bloom_filter_settings);
2406 void disable_commit_graph(struct repository *r)
2408 r->commit_graph_disabled = 1;