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 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 static char *get_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)
185 static struct commit_graph *alloc_commit_graph(void)
187 struct commit_graph *g = xcalloc(1, sizeof(*g));
192 extern int read_replace_refs;
194 static int commit_graph_compatible(struct repository *r)
199 if (read_replace_refs) {
200 prepare_replace_object(r);
201 if (hashmap_get_size(&r->objects->replace_map->map))
205 prepare_commit_graft(r);
206 if (r->parsed_objects &&
207 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
209 if (is_repository_shallow(r))
215 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
217 *fd = git_open(graph_file);
220 if (fstat(*fd, st)) {
227 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
228 struct object_directory *odb)
232 struct commit_graph *ret;
234 graph_size = xsize_t(st->st_size);
236 if (graph_size < GRAPH_MIN_SIZE) {
238 error(_("commit-graph file is too small"));
241 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
243 ret = parse_commit_graph(graph_map, graph_size);
248 munmap(graph_map, graph_size);
253 static int verify_commit_graph_lite(struct commit_graph *g)
256 * Basic validation shared between parse_commit_graph()
257 * which'll be called every time the graph is used, and the
258 * much more expensive verify_commit_graph() used by
259 * "commit-graph verify".
261 * There should only be very basic checks here to ensure that
262 * we don't e.g. segfault in fill_commit_in_graph(), but
263 * because this is a very hot codepath nothing that e.g. loops
264 * over g->num_commits, or runs a checksum on the commit-graph
267 if (!g->chunk_oid_fanout) {
268 error("commit-graph is missing the OID Fanout chunk");
271 if (!g->chunk_oid_lookup) {
272 error("commit-graph is missing the OID Lookup chunk");
275 if (!g->chunk_commit_data) {
276 error("commit-graph is missing the Commit Data chunk");
283 struct commit_graph *parse_commit_graph(void *graph_map, size_t graph_size)
285 const unsigned char *data, *chunk_lookup;
287 struct commit_graph *graph;
288 uint64_t last_chunk_offset;
289 uint32_t last_chunk_id;
290 uint32_t graph_signature;
291 unsigned char graph_version, hash_version;
296 if (graph_size < GRAPH_MIN_SIZE)
299 data = (const unsigned char *)graph_map;
301 graph_signature = get_be32(data);
302 if (graph_signature != GRAPH_SIGNATURE) {
303 error(_("commit-graph signature %X does not match signature %X"),
304 graph_signature, GRAPH_SIGNATURE);
308 graph_version = *(unsigned char*)(data + 4);
309 if (graph_version != GRAPH_VERSION) {
310 error(_("commit-graph version %X does not match version %X"),
311 graph_version, GRAPH_VERSION);
315 hash_version = *(unsigned char*)(data + 5);
316 if (hash_version != oid_version()) {
317 error(_("commit-graph hash version %X does not match version %X"),
318 hash_version, oid_version());
322 graph = alloc_commit_graph();
324 graph->hash_len = the_hash_algo->rawsz;
325 graph->num_chunks = *(unsigned char*)(data + 6);
326 graph->data = graph_map;
327 graph->data_len = graph_size;
330 last_chunk_offset = 8;
331 chunk_lookup = data + 8;
332 for (i = 0; i < graph->num_chunks; i++) {
334 uint64_t chunk_offset;
335 int chunk_repeated = 0;
337 if (data + graph_size - chunk_lookup <
338 GRAPH_CHUNKLOOKUP_WIDTH) {
339 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
340 goto free_and_return;
343 chunk_id = get_be32(chunk_lookup + 0);
344 chunk_offset = get_be64(chunk_lookup + 4);
346 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
348 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
349 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
350 (uint32_t)chunk_offset);
351 goto free_and_return;
355 case GRAPH_CHUNKID_OIDFANOUT:
356 if (graph->chunk_oid_fanout)
359 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
362 case GRAPH_CHUNKID_OIDLOOKUP:
363 if (graph->chunk_oid_lookup)
366 graph->chunk_oid_lookup = data + chunk_offset;
369 case GRAPH_CHUNKID_DATA:
370 if (graph->chunk_commit_data)
373 graph->chunk_commit_data = data + chunk_offset;
376 case GRAPH_CHUNKID_EXTRAEDGES:
377 if (graph->chunk_extra_edges)
380 graph->chunk_extra_edges = data + chunk_offset;
383 case GRAPH_CHUNKID_BASE:
384 if (graph->chunk_base_graphs)
387 graph->chunk_base_graphs = data + chunk_offset;
390 case GRAPH_CHUNKID_BLOOMINDEXES:
391 if (graph->chunk_bloom_indexes)
394 graph->chunk_bloom_indexes = data + chunk_offset;
397 case GRAPH_CHUNKID_BLOOMDATA:
398 if (graph->chunk_bloom_data)
401 uint32_t hash_version;
402 graph->chunk_bloom_data = data + chunk_offset;
403 hash_version = get_be32(data + chunk_offset);
405 if (hash_version != 1)
408 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
409 graph->bloom_filter_settings->hash_version = hash_version;
410 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
411 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
416 if (chunk_repeated) {
417 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
418 goto free_and_return;
421 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
423 graph->num_commits = (chunk_offset - last_chunk_offset)
427 last_chunk_id = chunk_id;
428 last_chunk_offset = chunk_offset;
431 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
432 init_bloom_filters();
434 /* We need both the bloom chunks to exist together. Else ignore the data */
435 graph->chunk_bloom_indexes = NULL;
436 graph->chunk_bloom_data = NULL;
437 FREE_AND_NULL(graph->bloom_filter_settings);
440 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
442 if (verify_commit_graph_lite(graph))
443 goto free_and_return;
448 free(graph->bloom_filter_settings);
453 static struct commit_graph *load_commit_graph_one(const char *graph_file,
454 struct object_directory *odb)
459 struct commit_graph *g;
460 int open_ok = open_commit_graph(graph_file, &fd, &st);
465 g = load_commit_graph_one_fd_st(fd, &st, odb);
468 g->filename = xstrdup(graph_file);
473 static struct commit_graph *load_commit_graph_v1(struct repository *r,
474 struct object_directory *odb)
476 char *graph_name = get_commit_graph_filename(odb);
477 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
483 static int add_graph_to_chain(struct commit_graph *g,
484 struct commit_graph *chain,
485 struct object_id *oids,
488 struct commit_graph *cur_g = chain;
490 if (n && !g->chunk_base_graphs) {
491 warning(_("commit-graph has no base graphs chunk"));
499 !oideq(&oids[n], &cur_g->oid) ||
500 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
501 warning(_("commit-graph chain does not match"));
505 cur_g = cur_g->base_graph;
508 g->base_graph = chain;
511 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
516 static struct commit_graph *load_commit_graph_chain(struct repository *r,
517 struct object_directory *odb)
519 struct commit_graph *graph_chain = NULL;
520 struct strbuf line = STRBUF_INIT;
522 struct object_id *oids;
523 int i = 0, valid = 1, count;
524 char *chain_name = get_chain_filename(odb);
528 fp = fopen(chain_name, "r");
529 stat_res = stat(chain_name, &st);
534 st.st_size <= the_hash_algo->hexsz)
537 count = st.st_size / (the_hash_algo->hexsz + 1);
538 oids = xcalloc(count, sizeof(struct object_id));
542 for (i = 0; i < count; i++) {
543 struct object_directory *odb;
545 if (strbuf_getline_lf(&line, fp) == EOF)
548 if (get_oid_hex(line.buf, &oids[i])) {
549 warning(_("invalid commit-graph chain: line '%s' not a hash"),
556 for (odb = r->objects->odb; odb; odb = odb->next) {
557 char *graph_name = get_split_graph_filename(odb, line.buf);
558 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
563 if (add_graph_to_chain(g, graph_chain, oids, i)) {
573 warning(_("unable to find all commit-graph files"));
580 strbuf_release(&line);
585 struct commit_graph *read_commit_graph_one(struct repository *r,
586 struct object_directory *odb)
588 struct commit_graph *g = load_commit_graph_v1(r, odb);
591 g = load_commit_graph_chain(r, odb);
596 static void prepare_commit_graph_one(struct repository *r,
597 struct object_directory *odb)
600 if (r->objects->commit_graph)
603 r->objects->commit_graph = read_commit_graph_one(r, odb);
607 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
609 * On the first invocation, this function attempts to load the commit
610 * graph if the_repository is configured to have one.
612 static int prepare_commit_graph(struct repository *r)
614 struct object_directory *odb;
617 * This must come before the "already attempted?" check below, because
618 * we want to disable even an already-loaded graph file.
620 if (r->commit_graph_disabled)
623 if (r->objects->commit_graph_attempted)
624 return !!r->objects->commit_graph;
625 r->objects->commit_graph_attempted = 1;
627 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
628 die("dying as requested by the '%s' variable on commit-graph load!",
629 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
631 prepare_repo_settings(r);
633 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
634 r->settings.core_commit_graph != 1)
636 * This repository is not configured to use commit graphs, so
637 * do not load one. (But report commit_graph_attempted anyway
638 * so that commit graph loading is not attempted again for this
643 if (!commit_graph_compatible(r))
647 for (odb = r->objects->odb;
648 !r->objects->commit_graph && odb;
650 prepare_commit_graph_one(r, odb);
651 return !!r->objects->commit_graph;
654 int generation_numbers_enabled(struct repository *r)
656 uint32_t first_generation;
657 struct commit_graph *g;
658 if (!prepare_commit_graph(r))
661 g = r->objects->commit_graph;
666 first_generation = get_be32(g->chunk_commit_data +
667 g->hash_len + 8) >> 2;
669 return !!first_generation;
672 static void close_commit_graph_one(struct commit_graph *g)
677 close_commit_graph_one(g->base_graph);
678 free_commit_graph(g);
681 void close_commit_graph(struct raw_object_store *o)
683 close_commit_graph_one(o->commit_graph);
684 o->commit_graph = NULL;
687 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
689 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
690 g->chunk_oid_lookup, g->hash_len, pos);
693 static void load_oid_from_graph(struct commit_graph *g,
695 struct object_id *oid)
699 while (g && pos < g->num_commits_in_base)
703 BUG("NULL commit-graph");
705 if (pos >= g->num_commits + g->num_commits_in_base)
706 die(_("invalid commit position. commit-graph is likely corrupt"));
708 lex_index = pos - g->num_commits_in_base;
710 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
713 static struct commit_list **insert_parent_or_die(struct repository *r,
714 struct commit_graph *g,
716 struct commit_list **pptr)
719 struct object_id oid;
721 if (pos >= g->num_commits + g->num_commits_in_base)
722 die("invalid parent position %"PRIu32, pos);
724 load_oid_from_graph(g, pos, &oid);
725 c = lookup_commit(r, &oid);
727 die(_("could not find commit %s"), oid_to_hex(&oid));
728 commit_graph_data_at(c)->graph_pos = pos;
729 return &commit_list_insert(c, pptr)->next;
732 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
734 const unsigned char *commit_data;
735 struct commit_graph_data *graph_data;
738 while (pos < g->num_commits_in_base)
741 lex_index = pos - g->num_commits_in_base;
742 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
744 graph_data = commit_graph_data_at(item);
745 graph_data->graph_pos = pos;
746 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
749 static inline void set_commit_tree(struct commit *c, struct tree *t)
754 static int fill_commit_in_graph(struct repository *r,
756 struct commit_graph *g, uint32_t pos)
759 uint32_t *parent_data_ptr;
760 uint64_t date_low, date_high;
761 struct commit_list **pptr;
762 struct commit_graph_data *graph_data;
763 const unsigned char *commit_data;
766 while (pos < g->num_commits_in_base)
769 if (pos >= g->num_commits + g->num_commits_in_base)
770 die(_("invalid commit position. commit-graph is likely corrupt"));
773 * Store the "full" position, but then use the
774 * "local" position for the rest of the calculation.
776 graph_data = commit_graph_data_at(item);
777 graph_data->graph_pos = pos;
778 lex_index = pos - g->num_commits_in_base;
780 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
782 item->object.parsed = 1;
784 set_commit_tree(item, NULL);
786 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
787 date_low = get_be32(commit_data + g->hash_len + 12);
788 item->date = (timestamp_t)((date_high << 32) | date_low);
790 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
792 pptr = &item->parents;
794 edge_value = get_be32(commit_data + g->hash_len);
795 if (edge_value == GRAPH_PARENT_NONE)
797 pptr = insert_parent_or_die(r, g, edge_value, pptr);
799 edge_value = get_be32(commit_data + g->hash_len + 4);
800 if (edge_value == GRAPH_PARENT_NONE)
802 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
803 pptr = insert_parent_or_die(r, g, edge_value, pptr);
807 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
808 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
810 edge_value = get_be32(parent_data_ptr);
811 pptr = insert_parent_or_die(r, g,
812 edge_value & GRAPH_EDGE_LAST_MASK,
815 } while (!(edge_value & GRAPH_LAST_EDGE));
820 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
822 uint32_t graph_pos = commit_graph_position(item);
823 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
827 struct commit_graph *cur_g = g;
830 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
831 cur_g = cur_g->base_graph;
834 *pos = lex_index + cur_g->num_commits_in_base;
842 static int parse_commit_in_graph_one(struct repository *r,
843 struct commit_graph *g,
848 if (item->object.parsed)
851 if (find_commit_in_graph(item, g, &pos))
852 return fill_commit_in_graph(r, item, g, pos);
857 int parse_commit_in_graph(struct repository *r, struct commit *item)
859 if (!prepare_commit_graph(r))
861 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
864 void load_commit_graph_info(struct repository *r, struct commit *item)
867 if (!prepare_commit_graph(r))
869 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
870 fill_commit_graph_info(item, r->objects->commit_graph, pos);
873 static struct tree *load_tree_for_commit(struct repository *r,
874 struct commit_graph *g,
877 struct object_id oid;
878 const unsigned char *commit_data;
879 uint32_t graph_pos = commit_graph_position(c);
881 while (graph_pos < g->num_commits_in_base)
884 commit_data = g->chunk_commit_data +
885 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
887 hashcpy(oid.hash, commit_data);
888 set_commit_tree(c, lookup_tree(r, &oid));
890 return c->maybe_tree;
893 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
894 struct commit_graph *g,
895 const struct commit *c)
898 return c->maybe_tree;
899 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
900 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
902 return load_tree_for_commit(r, g, (struct commit *)c);
905 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
907 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
910 struct packed_commit_list {
911 struct commit **list;
916 struct packed_oid_list {
917 struct object_id *list;
922 struct write_commit_graph_context {
923 struct repository *r;
924 struct object_directory *odb;
926 struct packed_oid_list oids;
927 struct packed_commit_list commits;
929 unsigned long approx_nr_objects;
930 struct progress *progress;
932 uint64_t progress_cnt;
934 char *base_graph_name;
935 int num_commit_graphs_before;
936 int num_commit_graphs_after;
937 char **commit_graph_filenames_before;
938 char **commit_graph_filenames_after;
939 char **commit_graph_hash_after;
940 uint32_t new_num_commits_in_base;
941 struct commit_graph *new_base_graph;
949 const struct split_commit_graph_opts *split_opts;
950 size_t total_bloom_filter_data_size;
953 static void write_graph_chunk_fanout(struct hashfile *f,
954 struct write_commit_graph_context *ctx)
957 struct commit **list = ctx->commits.list;
960 * Write the first-level table (the list is sorted,
961 * but we use a 256-entry lookup to be able to avoid
962 * having to do eight extra binary search iterations).
964 for (i = 0; i < 256; i++) {
965 while (count < ctx->commits.nr) {
966 if ((*list)->object.oid.hash[0] != i)
968 display_progress(ctx->progress, ++ctx->progress_cnt);
973 hashwrite_be32(f, count);
977 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
978 struct write_commit_graph_context *ctx)
980 struct commit **list = ctx->commits.list;
982 for (count = 0; count < ctx->commits.nr; count++, list++) {
983 display_progress(ctx->progress, ++ctx->progress_cnt);
984 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
988 static const unsigned char *commit_to_sha1(size_t index, void *table)
990 struct commit **commits = table;
991 return commits[index]->object.oid.hash;
994 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
995 struct write_commit_graph_context *ctx)
997 struct commit **list = ctx->commits.list;
998 struct commit **last = ctx->commits.list + ctx->commits.nr;
999 uint32_t num_extra_edges = 0;
1001 while (list < last) {
1002 struct commit_list *parent;
1003 struct object_id *tree;
1005 uint32_t packedDate[2];
1006 display_progress(ctx->progress, ++ctx->progress_cnt);
1008 if (parse_commit_no_graph(*list))
1009 die(_("unable to parse commit %s"),
1010 oid_to_hex(&(*list)->object.oid));
1011 tree = get_commit_tree_oid(*list);
1012 hashwrite(f, tree->hash, hash_len);
1014 parent = (*list)->parents;
1017 edge_value = GRAPH_PARENT_NONE;
1019 edge_value = sha1_pos(parent->item->object.oid.hash,
1024 if (edge_value >= 0)
1025 edge_value += ctx->new_num_commits_in_base;
1026 else if (ctx->new_base_graph) {
1028 if (find_commit_in_graph(parent->item,
1029 ctx->new_base_graph,
1035 BUG("missing parent %s for commit %s",
1036 oid_to_hex(&parent->item->object.oid),
1037 oid_to_hex(&(*list)->object.oid));
1040 hashwrite_be32(f, edge_value);
1043 parent = parent->next;
1046 edge_value = GRAPH_PARENT_NONE;
1047 else if (parent->next)
1048 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1050 edge_value = sha1_pos(parent->item->object.oid.hash,
1055 if (edge_value >= 0)
1056 edge_value += ctx->new_num_commits_in_base;
1057 else if (ctx->new_base_graph) {
1059 if (find_commit_in_graph(parent->item,
1060 ctx->new_base_graph,
1066 BUG("missing parent %s for commit %s",
1067 oid_to_hex(&parent->item->object.oid),
1068 oid_to_hex(&(*list)->object.oid));
1071 hashwrite_be32(f, edge_value);
1073 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1076 parent = parent->next;
1080 if (sizeof((*list)->date) > 4)
1081 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1085 packedDate[0] |= htonl(commit_graph_data_at(*list)->generation << 2);
1087 packedDate[1] = htonl((*list)->date);
1088 hashwrite(f, packedDate, 8);
1094 static void write_graph_chunk_extra_edges(struct hashfile *f,
1095 struct write_commit_graph_context *ctx)
1097 struct commit **list = ctx->commits.list;
1098 struct commit **last = ctx->commits.list + ctx->commits.nr;
1099 struct commit_list *parent;
1101 while (list < last) {
1102 int num_parents = 0;
1104 display_progress(ctx->progress, ++ctx->progress_cnt);
1106 for (parent = (*list)->parents; num_parents < 3 && parent;
1107 parent = parent->next)
1110 if (num_parents <= 2) {
1115 /* Since num_parents > 2, this initializer is safe. */
1116 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1117 int edge_value = sha1_pos(parent->item->object.oid.hash,
1122 if (edge_value >= 0)
1123 edge_value += ctx->new_num_commits_in_base;
1124 else if (ctx->new_base_graph) {
1126 if (find_commit_in_graph(parent->item,
1127 ctx->new_base_graph,
1133 BUG("missing parent %s for commit %s",
1134 oid_to_hex(&parent->item->object.oid),
1135 oid_to_hex(&(*list)->object.oid));
1136 else if (!parent->next)
1137 edge_value |= GRAPH_LAST_EDGE;
1139 hashwrite_be32(f, edge_value);
1146 static void write_graph_chunk_bloom_indexes(struct hashfile *f,
1147 struct write_commit_graph_context *ctx)
1149 struct commit **list = ctx->commits.list;
1150 struct commit **last = ctx->commits.list + ctx->commits.nr;
1151 uint32_t cur_pos = 0;
1153 while (list < last) {
1154 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
1155 cur_pos += filter->len;
1156 display_progress(ctx->progress, ++ctx->progress_cnt);
1157 hashwrite_be32(f, cur_pos);
1162 static void write_graph_chunk_bloom_data(struct hashfile *f,
1163 struct write_commit_graph_context *ctx,
1164 const struct bloom_filter_settings *settings)
1166 struct commit **list = ctx->commits.list;
1167 struct commit **last = ctx->commits.list + ctx->commits.nr;
1169 hashwrite_be32(f, settings->hash_version);
1170 hashwrite_be32(f, settings->num_hashes);
1171 hashwrite_be32(f, settings->bits_per_entry);
1173 while (list < last) {
1174 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
1175 display_progress(ctx->progress, ++ctx->progress_cnt);
1176 hashwrite(f, filter->data, filter->len * sizeof(unsigned char));
1181 static int oid_compare(const void *_a, const void *_b)
1183 const struct object_id *a = (const struct object_id *)_a;
1184 const struct object_id *b = (const struct object_id *)_b;
1185 return oidcmp(a, b);
1188 static int add_packed_commits(const struct object_id *oid,
1189 struct packed_git *pack,
1193 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1194 enum object_type type;
1195 off_t offset = nth_packed_object_offset(pack, pos);
1196 struct object_info oi = OBJECT_INFO_INIT;
1199 display_progress(ctx->progress, ++ctx->progress_done);
1202 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1203 die(_("unable to get type of object %s"), oid_to_hex(oid));
1205 if (type != OBJ_COMMIT)
1208 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1209 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1212 set_commit_pos(ctx->r, oid);
1217 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1219 struct commit_list *parent;
1220 for (parent = commit->parents; parent; parent = parent->next) {
1221 if (!(parent->item->object.flags & REACHABLE)) {
1222 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1223 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1225 parent->item->object.flags |= REACHABLE;
1230 static void close_reachable(struct write_commit_graph_context *ctx)
1233 struct commit *commit;
1234 enum commit_graph_split_flags flags = ctx->split_opts ?
1235 ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1237 if (ctx->report_progress)
1238 ctx->progress = start_delayed_progress(
1239 _("Loading known commits in commit graph"),
1241 for (i = 0; i < ctx->oids.nr; i++) {
1242 display_progress(ctx->progress, i + 1);
1243 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1245 commit->object.flags |= REACHABLE;
1247 stop_progress(&ctx->progress);
1250 * As this loop runs, ctx->oids.nr may grow, but not more
1251 * than the number of missing commits in the reachable
1254 if (ctx->report_progress)
1255 ctx->progress = start_delayed_progress(
1256 _("Expanding reachable commits in commit graph"),
1258 for (i = 0; i < ctx->oids.nr; i++) {
1259 display_progress(ctx->progress, i + 1);
1260 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1265 if ((!parse_commit(commit) &&
1266 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1267 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1268 add_missing_parents(ctx, commit);
1269 } else if (!parse_commit_no_graph(commit))
1270 add_missing_parents(ctx, commit);
1272 stop_progress(&ctx->progress);
1274 if (ctx->report_progress)
1275 ctx->progress = start_delayed_progress(
1276 _("Clearing commit marks in commit graph"),
1278 for (i = 0; i < ctx->oids.nr; i++) {
1279 display_progress(ctx->progress, i + 1);
1280 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1283 commit->object.flags &= ~REACHABLE;
1285 stop_progress(&ctx->progress);
1288 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1291 struct commit_list *list = NULL;
1293 if (ctx->report_progress)
1294 ctx->progress = start_delayed_progress(
1295 _("Computing commit graph generation numbers"),
1297 for (i = 0; i < ctx->commits.nr; i++) {
1298 uint32_t generation = commit_graph_data_at(ctx->commits.list[i])->generation;
1300 display_progress(ctx->progress, i + 1);
1301 if (generation != GENERATION_NUMBER_INFINITY &&
1302 generation != GENERATION_NUMBER_ZERO)
1305 commit_list_insert(ctx->commits.list[i], &list);
1307 struct commit *current = list->item;
1308 struct commit_list *parent;
1309 int all_parents_computed = 1;
1310 uint32_t max_generation = 0;
1312 for (parent = current->parents; parent; parent = parent->next) {
1313 generation = commit_graph_data_at(parent->item)->generation;
1315 if (generation == GENERATION_NUMBER_INFINITY ||
1316 generation == GENERATION_NUMBER_ZERO) {
1317 all_parents_computed = 0;
1318 commit_list_insert(parent->item, &list);
1320 } else if (generation > max_generation) {
1321 max_generation = generation;
1325 if (all_parents_computed) {
1326 struct commit_graph_data *data = commit_graph_data_at(current);
1328 data->generation = max_generation + 1;
1331 if (data->generation > GENERATION_NUMBER_MAX)
1332 data->generation = GENERATION_NUMBER_MAX;
1336 stop_progress(&ctx->progress);
1339 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1342 struct progress *progress = NULL;
1343 struct commit **sorted_commits;
1345 init_bloom_filters();
1347 if (ctx->report_progress)
1348 progress = start_delayed_progress(
1349 _("Computing commit changed paths Bloom filters"),
1352 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1353 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1355 if (ctx->order_by_pack)
1356 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1358 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1360 for (i = 0; i < ctx->commits.nr; i++) {
1361 struct commit *c = sorted_commits[i];
1362 struct bloom_filter *filter = get_bloom_filter(ctx->r, c, 1);
1363 ctx->total_bloom_filter_data_size += sizeof(unsigned char) * filter->len;
1364 display_progress(progress, i + 1);
1367 free(sorted_commits);
1368 stop_progress(&progress);
1371 struct refs_cb_data {
1372 struct oidset *commits;
1373 struct progress *progress;
1376 static int add_ref_to_set(const char *refname,
1377 const struct object_id *oid,
1378 int flags, void *cb_data)
1380 struct object_id peeled;
1381 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1383 if (!peel_ref(refname, &peeled))
1385 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1386 oidset_insert(data->commits, oid);
1388 display_progress(data->progress, oidset_size(data->commits));
1393 int write_commit_graph_reachable(struct object_directory *odb,
1394 enum commit_graph_write_flags flags,
1395 const struct split_commit_graph_opts *split_opts)
1397 struct oidset commits = OIDSET_INIT;
1398 struct refs_cb_data data;
1401 memset(&data, 0, sizeof(data));
1402 data.commits = &commits;
1403 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1404 data.progress = start_delayed_progress(
1405 _("Collecting referenced commits"), 0);
1407 for_each_ref(add_ref_to_set, &data);
1409 stop_progress(&data.progress);
1411 result = write_commit_graph(odb, NULL, &commits,
1414 oidset_clear(&commits);
1418 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1419 struct string_list *pack_indexes)
1422 struct strbuf progress_title = STRBUF_INIT;
1423 struct strbuf packname = STRBUF_INIT;
1426 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1427 dirlen = packname.len;
1428 if (ctx->report_progress) {
1429 strbuf_addf(&progress_title,
1430 Q_("Finding commits for commit graph in %d pack",
1431 "Finding commits for commit graph in %d packs",
1434 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1435 ctx->progress_done = 0;
1437 for (i = 0; i < pack_indexes->nr; i++) {
1438 struct packed_git *p;
1439 strbuf_setlen(&packname, dirlen);
1440 strbuf_addstr(&packname, pack_indexes->items[i].string);
1441 p = add_packed_git(packname.buf, packname.len, 1);
1443 error(_("error adding pack %s"), packname.buf);
1446 if (open_pack_index(p)) {
1447 error(_("error opening index for %s"), packname.buf);
1450 for_each_object_in_pack(p, add_packed_commits, ctx,
1451 FOR_EACH_OBJECT_PACK_ORDER);
1456 stop_progress(&ctx->progress);
1457 strbuf_release(&progress_title);
1458 strbuf_release(&packname);
1463 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1464 struct oidset *commits)
1466 struct oidset_iter iter;
1467 struct object_id *oid;
1469 if (!oidset_size(commits))
1472 oidset_iter_init(commits, &iter);
1473 while ((oid = oidset_iter_next(&iter))) {
1474 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1475 oidcpy(&ctx->oids.list[ctx->oids.nr], oid);
1482 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1484 if (ctx->report_progress)
1485 ctx->progress = start_delayed_progress(
1486 _("Finding commits for commit graph among packed objects"),
1487 ctx->approx_nr_objects);
1488 for_each_packed_object(add_packed_commits, ctx,
1489 FOR_EACH_OBJECT_PACK_ORDER);
1490 if (ctx->progress_done < ctx->approx_nr_objects)
1491 display_progress(ctx->progress, ctx->approx_nr_objects);
1492 stop_progress(&ctx->progress);
1495 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1497 uint32_t i, count_distinct = 1;
1499 if (ctx->report_progress)
1500 ctx->progress = start_delayed_progress(
1501 _("Counting distinct commits in commit graph"),
1503 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1504 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1506 for (i = 1; i < ctx->oids.nr; i++) {
1507 display_progress(ctx->progress, i + 1);
1508 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1510 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1512 if (!c || commit_graph_position(c) != COMMIT_NOT_FROM_GRAPH)
1519 stop_progress(&ctx->progress);
1521 return count_distinct;
1524 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1527 enum commit_graph_split_flags flags = ctx->split_opts ?
1528 ctx->split_opts->flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1530 ctx->num_extra_edges = 0;
1531 if (ctx->report_progress)
1532 ctx->progress = start_delayed_progress(
1533 _("Finding extra edges in commit graph"),
1535 for (i = 0; i < ctx->oids.nr; i++) {
1536 unsigned int num_parents;
1538 display_progress(ctx->progress, i + 1);
1539 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1542 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1543 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1545 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1546 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1549 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1550 parse_commit(ctx->commits.list[ctx->commits.nr]);
1552 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1554 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1555 if (num_parents > 2)
1556 ctx->num_extra_edges += num_parents - 1;
1560 stop_progress(&ctx->progress);
1563 static int write_graph_chunk_base_1(struct hashfile *f,
1564 struct commit_graph *g)
1571 num = write_graph_chunk_base_1(f, g->base_graph);
1572 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1576 static int write_graph_chunk_base(struct hashfile *f,
1577 struct write_commit_graph_context *ctx)
1579 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1581 if (num != ctx->num_commit_graphs_after - 1) {
1582 error(_("failed to write correct number of base graph ids"));
1589 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1594 struct lock_file lk = LOCK_INIT;
1595 uint32_t chunk_ids[MAX_NUM_CHUNKS + 1];
1596 uint64_t chunk_offsets[MAX_NUM_CHUNKS + 1];
1597 const unsigned hashsz = the_hash_algo->rawsz;
1598 struct strbuf progress_title = STRBUF_INIT;
1600 struct object_id file_hash;
1601 const struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
1604 struct strbuf tmp_file = STRBUF_INIT;
1606 strbuf_addf(&tmp_file,
1607 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1609 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1611 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1614 if (safe_create_leading_directories(ctx->graph_name)) {
1615 UNLEAK(ctx->graph_name);
1616 error(_("unable to create leading directories of %s"),
1622 char *lock_name = get_chain_filename(ctx->odb);
1624 hold_lock_file_for_update_mode(&lk, lock_name,
1625 LOCK_DIE_ON_ERROR, 0444);
1627 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1629 error(_("unable to create temporary graph layer"));
1633 if (adjust_shared_perm(ctx->graph_name)) {
1634 error(_("unable to adjust shared permissions for '%s'"),
1639 f = hashfd(fd, ctx->graph_name);
1641 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1642 LOCK_DIE_ON_ERROR, 0444);
1643 fd = lk.tempfile->fd;
1644 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1647 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1648 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1649 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1650 if (ctx->num_extra_edges) {
1651 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1654 if (ctx->changed_paths) {
1655 chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMINDEXES;
1657 chunk_ids[num_chunks] = GRAPH_CHUNKID_BLOOMDATA;
1660 if (ctx->num_commit_graphs_after > 1) {
1661 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1665 chunk_ids[num_chunks] = 0;
1667 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1668 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1669 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1670 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1673 if (ctx->num_extra_edges) {
1674 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1675 4 * ctx->num_extra_edges;
1678 if (ctx->changed_paths) {
1679 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1680 sizeof(uint32_t) * ctx->commits.nr;
1683 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1684 sizeof(uint32_t) * 3 + ctx->total_bloom_filter_data_size;
1687 if (ctx->num_commit_graphs_after > 1) {
1688 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1689 hashsz * (ctx->num_commit_graphs_after - 1);
1693 hashwrite_be32(f, GRAPH_SIGNATURE);
1695 hashwrite_u8(f, GRAPH_VERSION);
1696 hashwrite_u8(f, oid_version());
1697 hashwrite_u8(f, num_chunks);
1698 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1700 for (i = 0; i <= num_chunks; i++) {
1701 uint32_t chunk_write[3];
1703 chunk_write[0] = htonl(chunk_ids[i]);
1704 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1705 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1706 hashwrite(f, chunk_write, 12);
1709 if (ctx->report_progress) {
1710 strbuf_addf(&progress_title,
1711 Q_("Writing out commit graph in %d pass",
1712 "Writing out commit graph in %d passes",
1715 ctx->progress = start_delayed_progress(
1717 num_chunks * ctx->commits.nr);
1719 write_graph_chunk_fanout(f, ctx);
1720 write_graph_chunk_oids(f, hashsz, ctx);
1721 write_graph_chunk_data(f, hashsz, ctx);
1722 if (ctx->num_extra_edges)
1723 write_graph_chunk_extra_edges(f, ctx);
1724 if (ctx->changed_paths) {
1725 write_graph_chunk_bloom_indexes(f, ctx);
1726 write_graph_chunk_bloom_data(f, ctx, &bloom_settings);
1728 if (ctx->num_commit_graphs_after > 1 &&
1729 write_graph_chunk_base(f, ctx)) {
1732 stop_progress(&ctx->progress);
1733 strbuf_release(&progress_title);
1735 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1736 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1737 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1739 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1740 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1741 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1742 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1745 close_commit_graph(ctx->r->objects);
1746 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1749 FILE *chainf = fdopen_lock_file(&lk, "w");
1750 char *final_graph_name;
1756 error(_("unable to open commit-graph chain file"));
1760 if (ctx->base_graph_name) {
1762 int idx = ctx->num_commit_graphs_after - 1;
1763 if (ctx->num_commit_graphs_after > 1)
1766 dest = ctx->commit_graph_filenames_after[idx];
1768 if (strcmp(ctx->base_graph_name, dest)) {
1769 result = rename(ctx->base_graph_name, dest);
1772 error(_("failed to rename base commit-graph file"));
1777 char *graph_name = get_commit_graph_filename(ctx->odb);
1781 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1782 final_graph_name = get_split_graph_filename(ctx->odb,
1783 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1784 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1786 result = rename(ctx->graph_name, final_graph_name);
1788 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1789 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1792 error(_("failed to rename temporary commit-graph file"));
1797 commit_lock_file(&lk);
1802 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1804 struct commit_graph *g;
1805 uint32_t num_commits;
1806 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1809 int max_commits = 0;
1812 if (ctx->split_opts) {
1813 max_commits = ctx->split_opts->max_commits;
1815 if (ctx->split_opts->size_multiple)
1816 size_mult = ctx->split_opts->size_multiple;
1818 flags = ctx->split_opts->flags;
1821 g = ctx->r->objects->commit_graph;
1822 num_commits = ctx->commits.nr;
1823 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1824 ctx->num_commit_graphs_after = 1;
1826 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1828 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1829 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
1830 while (g && (g->num_commits <= size_mult * num_commits ||
1831 (max_commits && num_commits > max_commits))) {
1832 if (g->odb != ctx->odb)
1835 num_commits += g->num_commits;
1838 ctx->num_commit_graphs_after--;
1842 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
1843 ctx->new_base_graph = g;
1844 else if (ctx->num_commit_graphs_after != 1)
1845 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1846 "should be 1 with --split=replace");
1848 if (ctx->num_commit_graphs_after == 2) {
1849 char *old_graph_name = get_commit_graph_filename(g->odb);
1851 if (!strcmp(g->filename, old_graph_name) &&
1852 g->odb != ctx->odb) {
1853 ctx->num_commit_graphs_after = 1;
1854 ctx->new_base_graph = NULL;
1857 free(old_graph_name);
1860 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1861 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1863 for (i = 0; i < ctx->num_commit_graphs_after &&
1864 i < ctx->num_commit_graphs_before; i++)
1865 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1867 i = ctx->num_commit_graphs_before - 1;
1868 g = ctx->r->objects->commit_graph;
1871 if (i < ctx->num_commit_graphs_after)
1872 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1879 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1880 struct commit_graph *g)
1883 uint32_t offset = g->num_commits_in_base;
1885 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1887 for (i = 0; i < g->num_commits; i++) {
1888 struct object_id oid;
1889 struct commit *result;
1891 display_progress(ctx->progress, i + 1);
1893 load_oid_from_graph(g, i + offset, &oid);
1895 /* only add commits if they still exist in the repo */
1896 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1899 ctx->commits.list[ctx->commits.nr] = result;
1905 static int commit_compare(const void *_a, const void *_b)
1907 const struct commit *a = *(const struct commit **)_a;
1908 const struct commit *b = *(const struct commit **)_b;
1909 return oidcmp(&a->object.oid, &b->object.oid);
1912 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1916 if (ctx->report_progress)
1917 ctx->progress = start_delayed_progress(
1918 _("Scanning merged commits"),
1921 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1923 ctx->num_extra_edges = 0;
1924 for (i = 0; i < ctx->commits.nr; i++) {
1925 display_progress(ctx->progress, i);
1927 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1928 &ctx->commits.list[i]->object.oid)) {
1929 die(_("unexpected duplicate commit id %s"),
1930 oid_to_hex(&ctx->commits.list[i]->object.oid));
1932 unsigned int num_parents;
1934 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1935 if (num_parents > 2)
1936 ctx->num_extra_edges += num_parents - 1;
1940 stop_progress(&ctx->progress);
1943 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1945 struct commit_graph *g = ctx->r->objects->commit_graph;
1946 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1948 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1949 current_graph_number--;
1951 if (ctx->report_progress)
1952 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
1954 merge_commit_graph(ctx, g);
1955 stop_progress(&ctx->progress);
1961 ctx->new_base_graph = g;
1962 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1965 if (ctx->new_base_graph)
1966 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1968 sort_and_scan_merged_commits(ctx);
1971 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1974 time_t now = time(NULL);
1976 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1978 struct utimbuf updated_time;
1980 stat(ctx->commit_graph_filenames_before[i], &st);
1982 updated_time.actime = st.st_atime;
1983 updated_time.modtime = now;
1984 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1988 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1990 struct strbuf path = STRBUF_INIT;
1994 timestamp_t expire_time = time(NULL);
1996 if (ctx->split_opts && ctx->split_opts->expire_time)
1997 expire_time = ctx->split_opts->expire_time;
1999 char *chain_file_name = get_chain_filename(ctx->odb);
2000 unlink(chain_file_name);
2001 free(chain_file_name);
2002 ctx->num_commit_graphs_after = 0;
2005 strbuf_addstr(&path, ctx->odb->path);
2006 strbuf_addstr(&path, "/info/commit-graphs");
2007 dir = opendir(path.buf);
2012 strbuf_addch(&path, '/');
2013 dirnamelen = path.len;
2014 while ((de = readdir(dir)) != NULL) {
2016 uint32_t i, found = 0;
2018 strbuf_setlen(&path, dirnamelen);
2019 strbuf_addstr(&path, de->d_name);
2021 stat(path.buf, &st);
2023 if (st.st_mtime > expire_time)
2025 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2028 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2029 if (!strcmp(ctx->commit_graph_filenames_after[i],
2041 strbuf_release(&path);
2044 int write_commit_graph(struct object_directory *odb,
2045 struct string_list *pack_indexes,
2046 struct oidset *commits,
2047 enum commit_graph_write_flags flags,
2048 const struct split_commit_graph_opts *split_opts)
2050 struct write_commit_graph_context *ctx;
2051 uint32_t i, count_distinct = 0;
2055 if (!commit_graph_compatible(the_repository))
2058 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
2059 ctx->r = the_repository;
2061 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2062 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2063 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2064 ctx->split_opts = split_opts;
2065 ctx->changed_paths = flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS ? 1 : 0;
2066 ctx->total_bloom_filter_data_size = 0;
2069 struct commit_graph *g;
2070 prepare_commit_graph(ctx->r);
2072 g = ctx->r->objects->commit_graph;
2075 ctx->num_commit_graphs_before++;
2079 if (ctx->num_commit_graphs_before) {
2080 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2081 i = ctx->num_commit_graphs_before;
2082 g = ctx->r->objects->commit_graph;
2085 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2090 if (ctx->split_opts)
2091 replace = ctx->split_opts->flags & COMMIT_GRAPH_SPLIT_REPLACE;
2094 ctx->approx_nr_objects = approximate_object_count();
2095 ctx->oids.alloc = ctx->approx_nr_objects / 32;
2097 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
2098 ctx->oids.alloc = split_opts->max_commits;
2101 prepare_commit_graph_one(ctx->r, ctx->odb);
2102 if (ctx->r->objects->commit_graph)
2103 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
2106 if (ctx->oids.alloc < 1024)
2107 ctx->oids.alloc = 1024;
2108 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
2110 if (ctx->append && ctx->r->objects->commit_graph) {
2111 struct commit_graph *g = ctx->r->objects->commit_graph;
2112 for (i = 0; i < g->num_commits; i++) {
2113 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
2114 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
2119 ctx->order_by_pack = 1;
2120 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2125 if ((res = fill_oids_from_commits(ctx, commits)))
2129 if (!pack_indexes && !commits) {
2130 ctx->order_by_pack = 1;
2131 fill_oids_from_all_packs(ctx);
2134 close_reachable(ctx);
2136 count_distinct = count_distinct_commits(ctx);
2138 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
2139 error(_("the commit graph format cannot write %d commits"), count_distinct);
2144 ctx->commits.alloc = count_distinct;
2145 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
2147 copy_oids_to_commits(ctx);
2149 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2150 error(_("too many commits to write graph"));
2155 if (!ctx->commits.nr && !replace)
2159 split_graph_merge_strategy(ctx);
2162 merge_commit_graphs(ctx);
2164 ctx->num_commit_graphs_after = 1;
2166 compute_generation_numbers(ctx);
2168 if (ctx->changed_paths)
2169 compute_bloom_filters(ctx);
2171 res = write_commit_graph_file(ctx);
2174 mark_commit_graphs(ctx);
2176 expire_commit_graphs(ctx);
2179 free(ctx->graph_name);
2180 free(ctx->commits.list);
2181 free(ctx->oids.list);
2183 if (ctx->commit_graph_filenames_after) {
2184 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2185 free(ctx->commit_graph_filenames_after[i]);
2186 free(ctx->commit_graph_hash_after[i]);
2189 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2190 free(ctx->commit_graph_filenames_before[i]);
2192 free(ctx->commit_graph_filenames_after);
2193 free(ctx->commit_graph_filenames_before);
2194 free(ctx->commit_graph_hash_after);
2202 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2203 static int verify_commit_graph_error;
2205 static void graph_report(const char *fmt, ...)
2209 verify_commit_graph_error = 1;
2211 vfprintf(stderr, fmt, ap);
2212 fprintf(stderr, "\n");
2216 #define GENERATION_ZERO_EXISTS 1
2217 #define GENERATION_NUMBER_EXISTS 2
2219 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2221 uint32_t i, cur_fanout_pos = 0;
2222 struct object_id prev_oid, cur_oid, checksum;
2223 int generation_zero = 0;
2226 struct progress *progress = NULL;
2227 int local_error = 0;
2230 graph_report("no commit-graph file loaded");
2234 verify_commit_graph_error = verify_commit_graph_lite(g);
2235 if (verify_commit_graph_error)
2236 return verify_commit_graph_error;
2238 devnull = open("/dev/null", O_WRONLY);
2239 f = hashfd(devnull, NULL);
2240 hashwrite(f, g->data, g->data_len - g->hash_len);
2241 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2242 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2243 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2244 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2247 for (i = 0; i < g->num_commits; i++) {
2248 struct commit *graph_commit;
2250 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2252 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2253 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2254 oid_to_hex(&prev_oid),
2255 oid_to_hex(&cur_oid));
2257 oidcpy(&prev_oid, &cur_oid);
2259 while (cur_oid.hash[0] > cur_fanout_pos) {
2260 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2262 if (i != fanout_value)
2263 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2264 cur_fanout_pos, fanout_value, i);
2268 graph_commit = lookup_commit(r, &cur_oid);
2269 if (!parse_commit_in_graph_one(r, g, graph_commit))
2270 graph_report(_("failed to parse commit %s from commit-graph"),
2271 oid_to_hex(&cur_oid));
2274 while (cur_fanout_pos < 256) {
2275 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2277 if (g->num_commits != fanout_value)
2278 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2279 cur_fanout_pos, fanout_value, i);
2284 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2285 return verify_commit_graph_error;
2287 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2288 progress = start_progress(_("Verifying commits in commit graph"),
2291 for (i = 0; i < g->num_commits; i++) {
2292 struct commit *graph_commit, *odb_commit;
2293 struct commit_list *graph_parents, *odb_parents;
2294 uint32_t max_generation = 0;
2295 uint32_t generation;
2297 display_progress(progress, i + 1);
2298 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2300 graph_commit = lookup_commit(r, &cur_oid);
2301 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2302 if (parse_commit_internal(odb_commit, 0, 0)) {
2303 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2304 oid_to_hex(&cur_oid));
2308 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2309 get_commit_tree_oid(odb_commit)))
2310 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2311 oid_to_hex(&cur_oid),
2312 oid_to_hex(get_commit_tree_oid(graph_commit)),
2313 oid_to_hex(get_commit_tree_oid(odb_commit)));
2315 graph_parents = graph_commit->parents;
2316 odb_parents = odb_commit->parents;
2318 while (graph_parents) {
2319 if (odb_parents == NULL) {
2320 graph_report(_("commit-graph parent list for commit %s is too long"),
2321 oid_to_hex(&cur_oid));
2325 /* parse parent in case it is in a base graph */
2326 parse_commit_in_graph_one(r, g, graph_parents->item);
2328 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2329 graph_report(_("commit-graph parent for %s is %s != %s"),
2330 oid_to_hex(&cur_oid),
2331 oid_to_hex(&graph_parents->item->object.oid),
2332 oid_to_hex(&odb_parents->item->object.oid));
2334 generation = commit_graph_generation(graph_parents->item);
2335 if (generation > max_generation)
2336 max_generation = generation;
2338 graph_parents = graph_parents->next;
2339 odb_parents = odb_parents->next;
2342 if (odb_parents != NULL)
2343 graph_report(_("commit-graph parent list for commit %s terminates early"),
2344 oid_to_hex(&cur_oid));
2346 if (!commit_graph_generation(graph_commit)) {
2347 if (generation_zero == GENERATION_NUMBER_EXISTS)
2348 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2349 oid_to_hex(&cur_oid));
2350 generation_zero = GENERATION_ZERO_EXISTS;
2351 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2352 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2353 oid_to_hex(&cur_oid));
2355 if (generation_zero == GENERATION_ZERO_EXISTS)
2359 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2360 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2361 * extra logic in the following condition.
2363 if (max_generation == GENERATION_NUMBER_MAX)
2366 generation = commit_graph_generation(graph_commit);
2367 if (generation != max_generation + 1)
2368 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2369 oid_to_hex(&cur_oid),
2371 max_generation + 1);
2373 if (graph_commit->date != odb_commit->date)
2374 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2375 oid_to_hex(&cur_oid),
2379 stop_progress(&progress);
2381 local_error = verify_commit_graph_error;
2383 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2384 local_error |= verify_commit_graph(r, g->base_graph, flags);
2389 void free_commit_graph(struct commit_graph *g)
2394 munmap((void *)g->data, g->data_len);
2398 free(g->bloom_filter_settings);
2402 void disable_commit_graph(struct repository *r)
2404 r->commit_graph_disabled = 1;