1 #include "git-compat-util.h"
10 #include "sha1-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
15 #include "replace-object.h"
18 #include "commit-slab.h"
20 void git_test_write_commit_graph_or_die(void)
23 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
26 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
27 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
29 if (write_commit_graph_reachable(the_repository->objects->odb,
31 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
34 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
35 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
36 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
37 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
38 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
39 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
40 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
41 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
42 #define MAX_NUM_CHUNKS 7
44 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
46 #define GRAPH_VERSION_1 0x1
47 #define GRAPH_VERSION GRAPH_VERSION_1
49 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
50 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
51 #define GRAPH_PARENT_NONE 0x70000000
53 #define GRAPH_LAST_EDGE 0x80000000
55 #define GRAPH_HEADER_SIZE 8
56 #define GRAPH_FANOUT_SIZE (4 * 256)
57 #define GRAPH_CHUNKLOOKUP_WIDTH 12
58 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
59 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
61 /* Remember to update object flag allocation in object.h */
62 #define REACHABLE (1u<<15)
64 /* Keep track of the order in which commits are added to our list. */
65 define_commit_slab(commit_pos, int);
66 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
68 static void set_commit_pos(struct repository *r, const struct object_id *oid)
70 static int32_t max_pos;
71 struct commit *commit = lookup_commit(r, oid);
74 return; /* should never happen, but be lenient */
76 *commit_pos_at(&commit_pos, commit) = max_pos++;
79 static int commit_pos_cmp(const void *va, const void *vb)
81 const struct commit *a = *(const struct commit **)va;
82 const struct commit *b = *(const struct commit **)vb;
83 return commit_pos_at(&commit_pos, a) -
84 commit_pos_at(&commit_pos, b);
87 static int commit_gen_cmp(const void *va, const void *vb)
89 const struct commit *a = *(const struct commit **)va;
90 const struct commit *b = *(const struct commit **)vb;
92 /* lower generation commits first */
93 if (a->generation < b->generation)
95 else if (a->generation > b->generation)
98 /* use date as a heuristic when generations are equal */
99 if (a->date < b->date)
101 else if (a->date > b->date)
106 char *get_commit_graph_filename(struct object_directory *obj_dir)
108 return xstrfmt("%s/info/commit-graph", obj_dir->path);
111 static char *get_split_graph_filename(struct object_directory *odb,
114 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
118 static char *get_chain_filename(struct object_directory *odb)
120 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
123 static uint8_t oid_version(void)
128 static struct commit_graph *alloc_commit_graph(void)
130 struct commit_graph *g = xcalloc(1, sizeof(*g));
136 extern int read_replace_refs;
138 static int commit_graph_compatible(struct repository *r)
143 if (read_replace_refs) {
144 prepare_replace_object(r);
145 if (hashmap_get_size(&r->objects->replace_map->map))
149 prepare_commit_graft(r);
150 if (r->parsed_objects && r->parsed_objects->grafts_nr)
152 if (is_repository_shallow(r))
158 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
160 *fd = git_open(graph_file);
163 if (fstat(*fd, st)) {
170 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
171 struct object_directory *odb)
175 struct commit_graph *ret;
177 graph_size = xsize_t(st->st_size);
179 if (graph_size < GRAPH_MIN_SIZE) {
181 error(_("commit-graph file is too small"));
184 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
185 ret = parse_commit_graph(graph_map, fd, graph_size);
190 munmap(graph_map, graph_size);
197 static int verify_commit_graph_lite(struct commit_graph *g)
200 * Basic validation shared between parse_commit_graph()
201 * which'll be called every time the graph is used, and the
202 * much more expensive verify_commit_graph() used by
203 * "commit-graph verify".
205 * There should only be very basic checks here to ensure that
206 * we don't e.g. segfault in fill_commit_in_graph(), but
207 * because this is a very hot codepath nothing that e.g. loops
208 * over g->num_commits, or runs a checksum on the commit-graph
211 if (!g->chunk_oid_fanout) {
212 error("commit-graph is missing the OID Fanout chunk");
215 if (!g->chunk_oid_lookup) {
216 error("commit-graph is missing the OID Lookup chunk");
219 if (!g->chunk_commit_data) {
220 error("commit-graph is missing the Commit Data chunk");
227 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
230 const unsigned char *data, *chunk_lookup;
232 struct commit_graph *graph;
233 uint64_t next_chunk_offset;
234 uint32_t graph_signature;
235 unsigned char graph_version, hash_version;
240 if (graph_size < GRAPH_MIN_SIZE)
243 data = (const unsigned char *)graph_map;
245 graph_signature = get_be32(data);
246 if (graph_signature != GRAPH_SIGNATURE) {
247 error(_("commit-graph signature %X does not match signature %X"),
248 graph_signature, GRAPH_SIGNATURE);
252 graph_version = *(unsigned char*)(data + 4);
253 if (graph_version != GRAPH_VERSION) {
254 error(_("commit-graph version %X does not match version %X"),
255 graph_version, GRAPH_VERSION);
259 hash_version = *(unsigned char*)(data + 5);
260 if (hash_version != oid_version()) {
261 error(_("commit-graph hash version %X does not match version %X"),
262 hash_version, oid_version());
266 graph = alloc_commit_graph();
268 graph->hash_len = the_hash_algo->rawsz;
269 graph->num_chunks = *(unsigned char*)(data + 6);
270 graph->graph_fd = fd;
271 graph->data = graph_map;
272 graph->data_len = graph_size;
274 if (graph_size < GRAPH_HEADER_SIZE +
275 (graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH +
276 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
277 error(_("commit-graph file is too small to hold %u chunks"),
283 chunk_lookup = data + 8;
284 next_chunk_offset = get_be64(chunk_lookup + 4);
285 for (i = 0; i < graph->num_chunks; i++) {
287 uint64_t chunk_offset = next_chunk_offset;
288 int chunk_repeated = 0;
290 chunk_id = get_be32(chunk_lookup + 0);
292 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
293 next_chunk_offset = get_be64(chunk_lookup + 4);
295 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
296 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
297 (uint32_t)chunk_offset);
303 case GRAPH_CHUNKID_OIDFANOUT:
304 if (graph->chunk_oid_fanout)
307 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
310 case GRAPH_CHUNKID_OIDLOOKUP:
311 if (graph->chunk_oid_lookup)
314 graph->chunk_oid_lookup = data + chunk_offset;
315 graph->num_commits = (next_chunk_offset - chunk_offset)
320 case GRAPH_CHUNKID_DATA:
321 if (graph->chunk_commit_data)
324 graph->chunk_commit_data = data + chunk_offset;
327 case GRAPH_CHUNKID_EXTRAEDGES:
328 if (graph->chunk_extra_edges)
331 graph->chunk_extra_edges = data + chunk_offset;
334 case GRAPH_CHUNKID_BASE:
335 if (graph->chunk_base_graphs)
338 graph->chunk_base_graphs = data + chunk_offset;
341 case GRAPH_CHUNKID_BLOOMINDEXES:
342 if (graph->chunk_bloom_indexes)
345 graph->chunk_bloom_indexes = data + chunk_offset;
348 case GRAPH_CHUNKID_BLOOMDATA:
349 if (graph->chunk_bloom_data)
352 uint32_t hash_version;
353 graph->chunk_bloom_data = data + chunk_offset;
354 hash_version = get_be32(data + chunk_offset);
356 if (hash_version != 1)
359 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
360 graph->bloom_filter_settings->hash_version = hash_version;
361 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
362 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
367 if (chunk_repeated) {
368 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
374 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
375 init_bloom_filters();
377 /* We need both the bloom chunks to exist together. Else ignore the data */
378 graph->chunk_bloom_indexes = NULL;
379 graph->chunk_bloom_data = NULL;
380 graph->bloom_filter_settings = NULL;
383 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
385 if (verify_commit_graph_lite(graph)) {
393 static struct commit_graph *load_commit_graph_one(const char *graph_file,
394 struct object_directory *odb)
399 struct commit_graph *g;
400 int open_ok = open_commit_graph(graph_file, &fd, &st);
405 g = load_commit_graph_one_fd_st(fd, &st, odb);
408 g->filename = xstrdup(graph_file);
413 static struct commit_graph *load_commit_graph_v1(struct repository *r,
414 struct object_directory *odb)
416 char *graph_name = get_commit_graph_filename(odb);
417 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
423 static int add_graph_to_chain(struct commit_graph *g,
424 struct commit_graph *chain,
425 struct object_id *oids,
428 struct commit_graph *cur_g = chain;
430 if (n && !g->chunk_base_graphs) {
431 warning(_("commit-graph has no base graphs chunk"));
439 !oideq(&oids[n], &cur_g->oid) ||
440 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
441 warning(_("commit-graph chain does not match"));
445 cur_g = cur_g->base_graph;
448 g->base_graph = chain;
451 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
456 static struct commit_graph *load_commit_graph_chain(struct repository *r,
457 struct object_directory *odb)
459 struct commit_graph *graph_chain = NULL;
460 struct strbuf line = STRBUF_INIT;
462 struct object_id *oids;
463 int i = 0, valid = 1, count;
464 char *chain_name = get_chain_filename(odb);
468 fp = fopen(chain_name, "r");
469 stat_res = stat(chain_name, &st);
474 st.st_size <= the_hash_algo->hexsz)
477 count = st.st_size / (the_hash_algo->hexsz + 1);
478 oids = xcalloc(count, sizeof(struct object_id));
482 for (i = 0; i < count; i++) {
483 struct object_directory *odb;
485 if (strbuf_getline_lf(&line, fp) == EOF)
488 if (get_oid_hex(line.buf, &oids[i])) {
489 warning(_("invalid commit-graph chain: line '%s' not a hash"),
496 for (odb = r->objects->odb; odb; odb = odb->next) {
497 char *graph_name = get_split_graph_filename(odb, line.buf);
498 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
503 if (add_graph_to_chain(g, graph_chain, oids, i)) {
513 warning(_("unable to find all commit-graph files"));
520 strbuf_release(&line);
525 struct commit_graph *read_commit_graph_one(struct repository *r,
526 struct object_directory *odb)
528 struct commit_graph *g = load_commit_graph_v1(r, odb);
531 g = load_commit_graph_chain(r, odb);
536 static void prepare_commit_graph_one(struct repository *r,
537 struct object_directory *odb)
540 if (r->objects->commit_graph)
543 r->objects->commit_graph = read_commit_graph_one(r, odb);
547 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
549 * On the first invocation, this function attempts to load the commit
550 * graph if the_repository is configured to have one.
552 static int prepare_commit_graph(struct repository *r)
554 struct object_directory *odb;
557 * This must come before the "already attempted?" check below, because
558 * we want to disable even an already-loaded graph file.
560 if (r->commit_graph_disabled)
563 if (r->objects->commit_graph_attempted)
564 return !!r->objects->commit_graph;
565 r->objects->commit_graph_attempted = 1;
567 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
568 die("dying as requested by the '%s' variable on commit-graph load!",
569 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
571 prepare_repo_settings(r);
573 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
574 r->settings.core_commit_graph != 1)
576 * This repository is not configured to use commit graphs, so
577 * do not load one. (But report commit_graph_attempted anyway
578 * so that commit graph loading is not attempted again for this
583 if (!commit_graph_compatible(r))
587 for (odb = r->objects->odb;
588 !r->objects->commit_graph && odb;
590 prepare_commit_graph_one(r, odb);
591 return !!r->objects->commit_graph;
594 int generation_numbers_enabled(struct repository *r)
596 uint32_t first_generation;
597 struct commit_graph *g;
598 if (!prepare_commit_graph(r))
601 g = r->objects->commit_graph;
606 first_generation = get_be32(g->chunk_commit_data +
607 g->hash_len + 8) >> 2;
609 return !!first_generation;
612 static void close_commit_graph_one(struct commit_graph *g)
617 close_commit_graph_one(g->base_graph);
618 free_commit_graph(g);
621 void close_commit_graph(struct raw_object_store *o)
623 close_commit_graph_one(o->commit_graph);
624 o->commit_graph = NULL;
627 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
629 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
630 g->chunk_oid_lookup, g->hash_len, pos);
633 static void load_oid_from_graph(struct commit_graph *g,
635 struct object_id *oid)
639 while (g && pos < g->num_commits_in_base)
643 BUG("NULL commit-graph");
645 if (pos >= g->num_commits + g->num_commits_in_base)
646 die(_("invalid commit position. commit-graph is likely corrupt"));
648 lex_index = pos - g->num_commits_in_base;
650 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
653 static struct commit_list **insert_parent_or_die(struct repository *r,
654 struct commit_graph *g,
656 struct commit_list **pptr)
659 struct object_id oid;
661 if (pos >= g->num_commits + g->num_commits_in_base)
662 die("invalid parent position %"PRIu32, pos);
664 load_oid_from_graph(g, pos, &oid);
665 c = lookup_commit(r, &oid);
667 die(_("could not find commit %s"), oid_to_hex(&oid));
669 return &commit_list_insert(c, pptr)->next;
672 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
674 const unsigned char *commit_data;
677 while (pos < g->num_commits_in_base)
680 lex_index = pos - g->num_commits_in_base;
681 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
682 item->graph_pos = pos;
683 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
686 static inline void set_commit_tree(struct commit *c, struct tree *t)
691 static int fill_commit_in_graph(struct repository *r,
693 struct commit_graph *g, uint32_t pos)
696 uint32_t *parent_data_ptr;
697 uint64_t date_low, date_high;
698 struct commit_list **pptr;
699 const unsigned char *commit_data;
702 while (pos < g->num_commits_in_base)
705 if (pos >= g->num_commits + g->num_commits_in_base)
706 die(_("invalid commit position. commit-graph is likely corrupt"));
709 * Store the "full" position, but then use the
710 * "local" position for the rest of the calculation.
712 item->graph_pos = pos;
713 lex_index = pos - g->num_commits_in_base;
715 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
717 item->object.parsed = 1;
719 set_commit_tree(item, NULL);
721 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
722 date_low = get_be32(commit_data + g->hash_len + 12);
723 item->date = (timestamp_t)((date_high << 32) | date_low);
725 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
727 pptr = &item->parents;
729 edge_value = get_be32(commit_data + g->hash_len);
730 if (edge_value == GRAPH_PARENT_NONE)
732 pptr = insert_parent_or_die(r, g, edge_value, pptr);
734 edge_value = get_be32(commit_data + g->hash_len + 4);
735 if (edge_value == GRAPH_PARENT_NONE)
737 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
738 pptr = insert_parent_or_die(r, g, edge_value, pptr);
742 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
743 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
745 edge_value = get_be32(parent_data_ptr);
746 pptr = insert_parent_or_die(r, g,
747 edge_value & GRAPH_EDGE_LAST_MASK,
750 } while (!(edge_value & GRAPH_LAST_EDGE));
755 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
757 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
758 *pos = item->graph_pos;
761 struct commit_graph *cur_g = g;
764 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
765 cur_g = cur_g->base_graph;
768 *pos = lex_index + cur_g->num_commits_in_base;
776 static int parse_commit_in_graph_one(struct repository *r,
777 struct commit_graph *g,
782 if (item->object.parsed)
785 if (find_commit_in_graph(item, g, &pos))
786 return fill_commit_in_graph(r, item, g, pos);
791 int parse_commit_in_graph(struct repository *r, struct commit *item)
793 if (!prepare_commit_graph(r))
795 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
798 void load_commit_graph_info(struct repository *r, struct commit *item)
801 if (!prepare_commit_graph(r))
803 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
804 fill_commit_graph_info(item, r->objects->commit_graph, pos);
807 static struct tree *load_tree_for_commit(struct repository *r,
808 struct commit_graph *g,
811 struct object_id oid;
812 const unsigned char *commit_data;
814 while (c->graph_pos < g->num_commits_in_base)
817 commit_data = g->chunk_commit_data +
818 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
820 hashcpy(oid.hash, commit_data);
821 set_commit_tree(c, lookup_tree(r, &oid));
823 return c->maybe_tree;
826 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
827 struct commit_graph *g,
828 const struct commit *c)
831 return c->maybe_tree;
832 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
833 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
835 return load_tree_for_commit(r, g, (struct commit *)c);
838 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
840 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
843 struct packed_commit_list {
844 struct commit **list;
849 struct packed_oid_list {
850 struct object_id *list;
855 struct write_commit_graph_context {
856 struct repository *r;
857 struct object_directory *odb;
859 struct packed_oid_list oids;
860 struct packed_commit_list commits;
862 unsigned long approx_nr_objects;
863 struct progress *progress;
865 uint64_t progress_cnt;
867 char *base_graph_name;
868 int num_commit_graphs_before;
869 int num_commit_graphs_after;
870 char **commit_graph_filenames_before;
871 char **commit_graph_filenames_after;
872 char **commit_graph_hash_after;
873 uint32_t new_num_commits_in_base;
874 struct commit_graph *new_base_graph;
883 const struct split_commit_graph_opts *split_opts;
884 size_t total_bloom_filter_data_size;
887 static void write_graph_chunk_fanout(struct hashfile *f,
888 struct write_commit_graph_context *ctx)
891 struct commit **list = ctx->commits.list;
894 * Write the first-level table (the list is sorted,
895 * but we use a 256-entry lookup to be able to avoid
896 * having to do eight extra binary search iterations).
898 for (i = 0; i < 256; i++) {
899 while (count < ctx->commits.nr) {
900 if ((*list)->object.oid.hash[0] != i)
902 display_progress(ctx->progress, ++ctx->progress_cnt);
907 hashwrite_be32(f, count);
911 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
912 struct write_commit_graph_context *ctx)
914 struct commit **list = ctx->commits.list;
916 for (count = 0; count < ctx->commits.nr; count++, list++) {
917 display_progress(ctx->progress, ++ctx->progress_cnt);
918 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
922 static const unsigned char *commit_to_sha1(size_t index, void *table)
924 struct commit **commits = table;
925 return commits[index]->object.oid.hash;
928 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
929 struct write_commit_graph_context *ctx)
931 struct commit **list = ctx->commits.list;
932 struct commit **last = ctx->commits.list + ctx->commits.nr;
933 uint32_t num_extra_edges = 0;
935 while (list < last) {
936 struct commit_list *parent;
937 struct object_id *tree;
939 uint32_t packedDate[2];
940 display_progress(ctx->progress, ++ctx->progress_cnt);
942 if (parse_commit_no_graph(*list))
943 die(_("unable to parse commit %s"),
944 oid_to_hex(&(*list)->object.oid));
945 tree = get_commit_tree_oid(*list);
946 hashwrite(f, tree->hash, hash_len);
948 parent = (*list)->parents;
951 edge_value = GRAPH_PARENT_NONE;
953 edge_value = sha1_pos(parent->item->object.oid.hash,
959 edge_value += ctx->new_num_commits_in_base;
962 if (find_commit_in_graph(parent->item,
969 BUG("missing parent %s for commit %s",
970 oid_to_hex(&parent->item->object.oid),
971 oid_to_hex(&(*list)->object.oid));
974 hashwrite_be32(f, edge_value);
977 parent = parent->next;
980 edge_value = GRAPH_PARENT_NONE;
981 else if (parent->next)
982 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
984 edge_value = sha1_pos(parent->item->object.oid.hash,
990 edge_value += ctx->new_num_commits_in_base;
993 if (find_commit_in_graph(parent->item,
1000 BUG("missing parent %s for commit %s",
1001 oid_to_hex(&parent->item->object.oid),
1002 oid_to_hex(&(*list)->object.oid));
1005 hashwrite_be32(f, edge_value);
1007 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1010 parent = parent->next;
1014 if (sizeof((*list)->date) > 4)
1015 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1019 packedDate[0] |= htonl((*list)->generation << 2);
1021 packedDate[1] = htonl((*list)->date);
1022 hashwrite(f, packedDate, 8);
1028 static void write_graph_chunk_extra_edges(struct hashfile *f,
1029 struct write_commit_graph_context *ctx)
1031 struct commit **list = ctx->commits.list;
1032 struct commit **last = ctx->commits.list + ctx->commits.nr;
1033 struct commit_list *parent;
1035 while (list < last) {
1036 int num_parents = 0;
1038 display_progress(ctx->progress, ++ctx->progress_cnt);
1040 for (parent = (*list)->parents; num_parents < 3 && parent;
1041 parent = parent->next)
1044 if (num_parents <= 2) {
1049 /* Since num_parents > 2, this initializer is safe. */
1050 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1051 int edge_value = sha1_pos(parent->item->object.oid.hash,
1056 if (edge_value >= 0)
1057 edge_value += ctx->new_num_commits_in_base;
1060 if (find_commit_in_graph(parent->item,
1061 ctx->new_base_graph,
1067 BUG("missing parent %s for commit %s",
1068 oid_to_hex(&parent->item->object.oid),
1069 oid_to_hex(&(*list)->object.oid));
1070 else if (!parent->next)
1071 edge_value |= GRAPH_LAST_EDGE;
1073 hashwrite_be32(f, edge_value);
1080 static void write_graph_chunk_bloom_indexes(struct hashfile *f,
1081 struct write_commit_graph_context *ctx)
1083 struct commit **list = ctx->commits.list;
1084 struct commit **last = ctx->commits.list + ctx->commits.nr;
1085 uint32_t cur_pos = 0;
1086 struct progress *progress = NULL;
1089 if (ctx->report_progress)
1090 progress = start_delayed_progress(
1091 _("Writing changed paths Bloom filters index"),
1094 while (list < last) {
1095 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
1096 cur_pos += filter->len;
1097 display_progress(progress, ++i);
1098 hashwrite_be32(f, cur_pos);
1102 stop_progress(&progress);
1105 static void write_graph_chunk_bloom_data(struct hashfile *f,
1106 struct write_commit_graph_context *ctx,
1107 const struct bloom_filter_settings *settings)
1109 struct commit **list = ctx->commits.list;
1110 struct commit **last = ctx->commits.list + ctx->commits.nr;
1111 struct progress *progress = NULL;
1114 if (ctx->report_progress)
1115 progress = start_delayed_progress(
1116 _("Writing changed paths Bloom filters data"),
1119 hashwrite_be32(f, settings->hash_version);
1120 hashwrite_be32(f, settings->num_hashes);
1121 hashwrite_be32(f, settings->bits_per_entry);
1123 while (list < last) {
1124 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list, 0);
1125 display_progress(progress, ++i);
1126 hashwrite(f, filter->data, filter->len * sizeof(unsigned char));
1130 stop_progress(&progress);
1133 static int oid_compare(const void *_a, const void *_b)
1135 const struct object_id *a = (const struct object_id *)_a;
1136 const struct object_id *b = (const struct object_id *)_b;
1137 return oidcmp(a, b);
1140 static int add_packed_commits(const struct object_id *oid,
1141 struct packed_git *pack,
1145 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1146 enum object_type type;
1147 off_t offset = nth_packed_object_offset(pack, pos);
1148 struct object_info oi = OBJECT_INFO_INIT;
1151 display_progress(ctx->progress, ++ctx->progress_done);
1154 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1155 die(_("unable to get type of object %s"), oid_to_hex(oid));
1157 if (type != OBJ_COMMIT)
1160 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1161 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1164 set_commit_pos(ctx->r, oid);
1169 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1171 struct commit_list *parent;
1172 for (parent = commit->parents; parent; parent = parent->next) {
1173 if (!(parent->item->object.flags & REACHABLE)) {
1174 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1175 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1177 parent->item->object.flags |= REACHABLE;
1182 static void close_reachable(struct write_commit_graph_context *ctx)
1185 struct commit *commit;
1187 if (ctx->report_progress)
1188 ctx->progress = start_delayed_progress(
1189 _("Loading known commits in commit graph"),
1191 for (i = 0; i < ctx->oids.nr; i++) {
1192 display_progress(ctx->progress, i + 1);
1193 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1195 commit->object.flags |= REACHABLE;
1197 stop_progress(&ctx->progress);
1200 * As this loop runs, ctx->oids.nr may grow, but not more
1201 * than the number of missing commits in the reachable
1204 if (ctx->report_progress)
1205 ctx->progress = start_delayed_progress(
1206 _("Expanding reachable commits in commit graph"),
1208 for (i = 0; i < ctx->oids.nr; i++) {
1209 display_progress(ctx->progress, i + 1);
1210 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1215 if (!parse_commit(commit) &&
1216 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1217 add_missing_parents(ctx, commit);
1218 } else if (!parse_commit_no_graph(commit))
1219 add_missing_parents(ctx, commit);
1221 stop_progress(&ctx->progress);
1223 if (ctx->report_progress)
1224 ctx->progress = start_delayed_progress(
1225 _("Clearing commit marks in commit graph"),
1227 for (i = 0; i < ctx->oids.nr; i++) {
1228 display_progress(ctx->progress, i + 1);
1229 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1232 commit->object.flags &= ~REACHABLE;
1234 stop_progress(&ctx->progress);
1237 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1240 struct commit_list *list = NULL;
1242 if (ctx->report_progress)
1243 ctx->progress = start_delayed_progress(
1244 _("Computing commit graph generation numbers"),
1246 for (i = 0; i < ctx->commits.nr; i++) {
1247 display_progress(ctx->progress, i + 1);
1248 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1249 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
1252 commit_list_insert(ctx->commits.list[i], &list);
1254 struct commit *current = list->item;
1255 struct commit_list *parent;
1256 int all_parents_computed = 1;
1257 uint32_t max_generation = 0;
1259 for (parent = current->parents; parent; parent = parent->next) {
1260 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1261 parent->item->generation == GENERATION_NUMBER_ZERO) {
1262 all_parents_computed = 0;
1263 commit_list_insert(parent->item, &list);
1265 } else if (parent->item->generation > max_generation) {
1266 max_generation = parent->item->generation;
1270 if (all_parents_computed) {
1271 current->generation = max_generation + 1;
1274 if (current->generation > GENERATION_NUMBER_MAX)
1275 current->generation = GENERATION_NUMBER_MAX;
1279 stop_progress(&ctx->progress);
1282 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1285 struct progress *progress = NULL;
1286 struct commit **sorted_commits;
1288 init_bloom_filters();
1290 if (ctx->report_progress)
1291 progress = start_delayed_progress(
1292 _("Computing commit changed paths Bloom filters"),
1295 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1296 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1298 if (ctx->order_by_pack)
1299 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1301 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1303 for (i = 0; i < ctx->commits.nr; i++) {
1304 struct commit *c = sorted_commits[i];
1305 struct bloom_filter *filter = get_bloom_filter(ctx->r, c, 1);
1306 ctx->total_bloom_filter_data_size += sizeof(unsigned char) * filter->len;
1307 display_progress(progress, i + 1);
1310 free(sorted_commits);
1311 stop_progress(&progress);
1314 static int add_ref_to_list(const char *refname,
1315 const struct object_id *oid,
1316 int flags, void *cb_data)
1318 struct string_list *list = (struct string_list *)cb_data;
1320 string_list_append(list, oid_to_hex(oid));
1324 int write_commit_graph_reachable(struct object_directory *odb,
1325 enum commit_graph_write_flags flags,
1326 const struct split_commit_graph_opts *split_opts)
1328 struct string_list list = STRING_LIST_INIT_DUP;
1331 for_each_ref(add_ref_to_list, &list);
1332 result = write_commit_graph(odb, NULL, &list,
1335 string_list_clear(&list, 0);
1339 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1340 struct string_list *pack_indexes)
1343 struct strbuf progress_title = STRBUF_INIT;
1344 struct strbuf packname = STRBUF_INIT;
1347 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1348 dirlen = packname.len;
1349 if (ctx->report_progress) {
1350 strbuf_addf(&progress_title,
1351 Q_("Finding commits for commit graph in %d pack",
1352 "Finding commits for commit graph in %d packs",
1355 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1356 ctx->progress_done = 0;
1358 for (i = 0; i < pack_indexes->nr; i++) {
1359 struct packed_git *p;
1360 strbuf_setlen(&packname, dirlen);
1361 strbuf_addstr(&packname, pack_indexes->items[i].string);
1362 p = add_packed_git(packname.buf, packname.len, 1);
1364 error(_("error adding pack %s"), packname.buf);
1367 if (open_pack_index(p)) {
1368 error(_("error opening index for %s"), packname.buf);
1371 for_each_object_in_pack(p, add_packed_commits, ctx,
1372 FOR_EACH_OBJECT_PACK_ORDER);
1377 stop_progress(&ctx->progress);
1378 strbuf_release(&progress_title);
1379 strbuf_release(&packname);
1384 static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1385 struct string_list *commit_hex)
1388 struct strbuf progress_title = STRBUF_INIT;
1390 if (ctx->report_progress) {
1391 strbuf_addf(&progress_title,
1392 Q_("Finding commits for commit graph from %d ref",
1393 "Finding commits for commit graph from %d refs",
1396 ctx->progress = start_delayed_progress(
1400 for (i = 0; i < commit_hex->nr; i++) {
1402 struct object_id oid;
1403 struct commit *result;
1405 display_progress(ctx->progress, i + 1);
1406 if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1407 (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
1408 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1409 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1411 } else if (ctx->check_oids) {
1412 error(_("invalid commit object id: %s"),
1413 commit_hex->items[i].string);
1417 stop_progress(&ctx->progress);
1418 strbuf_release(&progress_title);
1423 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1425 if (ctx->report_progress)
1426 ctx->progress = start_delayed_progress(
1427 _("Finding commits for commit graph among packed objects"),
1428 ctx->approx_nr_objects);
1429 for_each_packed_object(add_packed_commits, ctx,
1430 FOR_EACH_OBJECT_PACK_ORDER);
1431 if (ctx->progress_done < ctx->approx_nr_objects)
1432 display_progress(ctx->progress, ctx->approx_nr_objects);
1433 stop_progress(&ctx->progress);
1436 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1438 uint32_t i, count_distinct = 1;
1440 if (ctx->report_progress)
1441 ctx->progress = start_delayed_progress(
1442 _("Counting distinct commits in commit graph"),
1444 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1445 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1447 for (i = 1; i < ctx->oids.nr; i++) {
1448 display_progress(ctx->progress, i + 1);
1449 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1451 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1453 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1460 stop_progress(&ctx->progress);
1462 return count_distinct;
1465 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1469 ctx->num_extra_edges = 0;
1470 if (ctx->report_progress)
1471 ctx->progress = start_delayed_progress(
1472 _("Finding extra edges in commit graph"),
1474 for (i = 0; i < ctx->oids.nr; i++) {
1475 unsigned int num_parents;
1477 display_progress(ctx->progress, i + 1);
1478 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1481 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1482 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1485 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1488 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1490 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1491 if (num_parents > 2)
1492 ctx->num_extra_edges += num_parents - 1;
1496 stop_progress(&ctx->progress);
1499 static int write_graph_chunk_base_1(struct hashfile *f,
1500 struct commit_graph *g)
1507 num = write_graph_chunk_base_1(f, g->base_graph);
1508 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1512 static int write_graph_chunk_base(struct hashfile *f,
1513 struct write_commit_graph_context *ctx)
1515 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1517 if (num != ctx->num_commit_graphs_after - 1) {
1518 error(_("failed to write correct number of base graph ids"));
1530 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1535 struct lock_file lk = LOCK_INIT;
1536 struct chunk_info chunks[MAX_NUM_CHUNKS + 1];
1537 const unsigned hashsz = the_hash_algo->rawsz;
1538 struct strbuf progress_title = STRBUF_INIT;
1540 uint64_t chunk_offset;
1541 struct object_id file_hash;
1542 const struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
1545 struct strbuf tmp_file = STRBUF_INIT;
1547 strbuf_addf(&tmp_file,
1548 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1550 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1552 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1555 if (safe_create_leading_directories(ctx->graph_name)) {
1556 UNLEAK(ctx->graph_name);
1557 error(_("unable to create leading directories of %s"),
1563 char *lock_name = get_chain_filename(ctx->odb);
1565 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1567 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1569 error(_("unable to create '%s'"), ctx->graph_name);
1573 f = hashfd(fd, ctx->graph_name);
1575 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1576 fd = lk.tempfile->fd;
1577 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1580 chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
1581 chunks[0].size = GRAPH_FANOUT_SIZE;
1582 chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
1583 chunks[1].size = hashsz * ctx->commits.nr;
1584 chunks[2].id = GRAPH_CHUNKID_DATA;
1585 chunks[2].size = (hashsz + 16) * ctx->commits.nr;
1586 if (ctx->num_extra_edges) {
1587 chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
1588 chunks[num_chunks].size = 4 * ctx->num_extra_edges;
1591 if (ctx->changed_paths) {
1592 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
1593 chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
1595 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
1596 chunks[num_chunks].size = sizeof(uint32_t) * 3
1597 + ctx->total_bloom_filter_data_size;
1600 if (ctx->num_commit_graphs_after > 1) {
1601 chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
1602 chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
1606 chunks[num_chunks].id = 0;
1607 chunks[num_chunks].size = 0;
1609 hashwrite_be32(f, GRAPH_SIGNATURE);
1611 hashwrite_u8(f, GRAPH_VERSION);
1612 hashwrite_u8(f, oid_version());
1613 hashwrite_u8(f, num_chunks);
1614 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1616 chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1617 for (i = 0; i <= num_chunks; i++) {
1618 uint32_t chunk_write[3];
1620 chunk_write[0] = htonl(chunks[i].id);
1621 chunk_write[1] = htonl(chunk_offset >> 32);
1622 chunk_write[2] = htonl(chunk_offset & 0xffffffff);
1623 hashwrite(f, chunk_write, 12);
1625 chunk_offset += chunks[i].size;
1628 if (ctx->report_progress) {
1629 strbuf_addf(&progress_title,
1630 Q_("Writing out commit graph in %d pass",
1631 "Writing out commit graph in %d passes",
1634 ctx->progress = start_delayed_progress(
1636 num_chunks * ctx->commits.nr);
1638 write_graph_chunk_fanout(f, ctx);
1639 write_graph_chunk_oids(f, hashsz, ctx);
1640 write_graph_chunk_data(f, hashsz, ctx);
1641 if (ctx->num_extra_edges)
1642 write_graph_chunk_extra_edges(f, ctx);
1643 if (ctx->changed_paths) {
1644 write_graph_chunk_bloom_indexes(f, ctx);
1645 write_graph_chunk_bloom_data(f, ctx, &bloom_settings);
1647 if (ctx->num_commit_graphs_after > 1 &&
1648 write_graph_chunk_base(f, ctx)) {
1651 stop_progress(&ctx->progress);
1652 strbuf_release(&progress_title);
1654 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1655 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1656 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1658 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1659 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1660 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1661 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1664 close_commit_graph(ctx->r->objects);
1665 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1668 FILE *chainf = fdopen_lock_file(&lk, "w");
1669 char *final_graph_name;
1675 error(_("unable to open commit-graph chain file"));
1679 if (ctx->base_graph_name) {
1680 const char *dest = ctx->commit_graph_filenames_after[
1681 ctx->num_commit_graphs_after - 2];
1683 if (strcmp(ctx->base_graph_name, dest)) {
1684 result = rename(ctx->base_graph_name, dest);
1687 error(_("failed to rename base commit-graph file"));
1692 char *graph_name = get_commit_graph_filename(ctx->odb);
1696 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1697 final_graph_name = get_split_graph_filename(ctx->odb,
1698 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1699 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1701 result = rename(ctx->graph_name, final_graph_name);
1703 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1704 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1707 error(_("failed to rename temporary commit-graph file"));
1712 commit_lock_file(&lk);
1717 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1719 struct commit_graph *g;
1720 uint32_t num_commits;
1723 int max_commits = 0;
1726 if (ctx->split_opts) {
1727 max_commits = ctx->split_opts->max_commits;
1729 if (ctx->split_opts->size_multiple)
1730 size_mult = ctx->split_opts->size_multiple;
1733 g = ctx->r->objects->commit_graph;
1734 num_commits = ctx->commits.nr;
1735 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1737 while (g && (g->num_commits <= size_mult * num_commits ||
1738 (max_commits && num_commits > max_commits))) {
1739 if (g->odb != ctx->odb)
1742 num_commits += g->num_commits;
1745 ctx->num_commit_graphs_after--;
1748 ctx->new_base_graph = g;
1750 if (ctx->num_commit_graphs_after == 2) {
1751 char *old_graph_name = get_commit_graph_filename(g->odb);
1753 if (!strcmp(g->filename, old_graph_name) &&
1754 g->odb != ctx->odb) {
1755 ctx->num_commit_graphs_after = 1;
1756 ctx->new_base_graph = NULL;
1759 free(old_graph_name);
1762 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1763 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1765 for (i = 0; i < ctx->num_commit_graphs_after &&
1766 i < ctx->num_commit_graphs_before; i++)
1767 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1769 i = ctx->num_commit_graphs_before - 1;
1770 g = ctx->r->objects->commit_graph;
1773 if (i < ctx->num_commit_graphs_after)
1774 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1781 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1782 struct commit_graph *g)
1785 uint32_t offset = g->num_commits_in_base;
1787 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1789 for (i = 0; i < g->num_commits; i++) {
1790 struct object_id oid;
1791 struct commit *result;
1793 display_progress(ctx->progress, i + 1);
1795 load_oid_from_graph(g, i + offset, &oid);
1797 /* only add commits if they still exist in the repo */
1798 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1801 ctx->commits.list[ctx->commits.nr] = result;
1807 static int commit_compare(const void *_a, const void *_b)
1809 const struct commit *a = *(const struct commit **)_a;
1810 const struct commit *b = *(const struct commit **)_b;
1811 return oidcmp(&a->object.oid, &b->object.oid);
1814 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1818 if (ctx->report_progress)
1819 ctx->progress = start_delayed_progress(
1820 _("Scanning merged commits"),
1823 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1825 ctx->num_extra_edges = 0;
1826 for (i = 0; i < ctx->commits.nr; i++) {
1827 display_progress(ctx->progress, i);
1829 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1830 &ctx->commits.list[i]->object.oid)) {
1831 die(_("unexpected duplicate commit id %s"),
1832 oid_to_hex(&ctx->commits.list[i]->object.oid));
1834 unsigned int num_parents;
1836 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1837 if (num_parents > 2)
1838 ctx->num_extra_edges += num_parents - 1;
1842 stop_progress(&ctx->progress);
1845 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1847 struct commit_graph *g = ctx->r->objects->commit_graph;
1848 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1850 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1851 current_graph_number--;
1853 if (ctx->report_progress)
1854 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
1856 merge_commit_graph(ctx, g);
1857 stop_progress(&ctx->progress);
1863 ctx->new_base_graph = g;
1864 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1867 if (ctx->new_base_graph)
1868 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1870 sort_and_scan_merged_commits(ctx);
1873 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1876 time_t now = time(NULL);
1878 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1880 struct utimbuf updated_time;
1882 stat(ctx->commit_graph_filenames_before[i], &st);
1884 updated_time.actime = st.st_atime;
1885 updated_time.modtime = now;
1886 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1890 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1892 struct strbuf path = STRBUF_INIT;
1896 timestamp_t expire_time = time(NULL);
1898 if (ctx->split_opts && ctx->split_opts->expire_time)
1899 expire_time -= ctx->split_opts->expire_time;
1901 char *chain_file_name = get_chain_filename(ctx->odb);
1902 unlink(chain_file_name);
1903 free(chain_file_name);
1904 ctx->num_commit_graphs_after = 0;
1907 strbuf_addstr(&path, ctx->odb->path);
1908 strbuf_addstr(&path, "/info/commit-graphs");
1909 dir = opendir(path.buf);
1914 strbuf_addch(&path, '/');
1915 dirnamelen = path.len;
1916 while ((de = readdir(dir)) != NULL) {
1918 uint32_t i, found = 0;
1920 strbuf_setlen(&path, dirnamelen);
1921 strbuf_addstr(&path, de->d_name);
1923 stat(path.buf, &st);
1925 if (st.st_mtime > expire_time)
1927 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1930 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1931 if (!strcmp(ctx->commit_graph_filenames_after[i],
1943 strbuf_release(&path);
1946 int write_commit_graph(struct object_directory *odb,
1947 struct string_list *pack_indexes,
1948 struct string_list *commit_hex,
1949 enum commit_graph_write_flags flags,
1950 const struct split_commit_graph_opts *split_opts)
1952 struct write_commit_graph_context *ctx;
1953 uint32_t i, count_distinct = 0;
1956 if (!commit_graph_compatible(the_repository))
1959 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1960 ctx->r = the_repository;
1962 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1963 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1964 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
1965 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
1966 ctx->split_opts = split_opts;
1967 ctx->changed_paths = flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS ? 1 : 0;
1968 ctx->total_bloom_filter_data_size = 0;
1971 struct commit_graph *g;
1972 prepare_commit_graph(ctx->r);
1974 g = ctx->r->objects->commit_graph;
1977 ctx->num_commit_graphs_before++;
1981 if (ctx->num_commit_graphs_before) {
1982 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1983 i = ctx->num_commit_graphs_before;
1984 g = ctx->r->objects->commit_graph;
1987 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1993 ctx->approx_nr_objects = approximate_object_count();
1994 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1996 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1997 ctx->oids.alloc = split_opts->max_commits;
2000 prepare_commit_graph_one(ctx->r, ctx->odb);
2001 if (ctx->r->objects->commit_graph)
2002 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
2005 if (ctx->oids.alloc < 1024)
2006 ctx->oids.alloc = 1024;
2007 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
2009 if (ctx->append && ctx->r->objects->commit_graph) {
2010 struct commit_graph *g = ctx->r->objects->commit_graph;
2011 for (i = 0; i < g->num_commits; i++) {
2012 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
2013 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
2018 ctx->order_by_pack = 1;
2019 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2024 if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
2028 if (!pack_indexes && !commit_hex) {
2029 ctx->order_by_pack = 1;
2030 fill_oids_from_all_packs(ctx);
2033 close_reachable(ctx);
2035 count_distinct = count_distinct_commits(ctx);
2037 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
2038 error(_("the commit graph format cannot write %d commits"), count_distinct);
2043 ctx->commits.alloc = count_distinct;
2044 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
2046 copy_oids_to_commits(ctx);
2048 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2049 error(_("too many commits to write graph"));
2054 if (!ctx->commits.nr)
2058 split_graph_merge_strategy(ctx);
2060 merge_commit_graphs(ctx);
2062 ctx->num_commit_graphs_after = 1;
2064 compute_generation_numbers(ctx);
2066 if (ctx->changed_paths)
2067 compute_bloom_filters(ctx);
2069 res = write_commit_graph_file(ctx);
2072 mark_commit_graphs(ctx);
2074 expire_commit_graphs(ctx);
2077 free(ctx->graph_name);
2078 free(ctx->commits.list);
2079 free(ctx->oids.list);
2081 if (ctx->commit_graph_filenames_after) {
2082 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2083 free(ctx->commit_graph_filenames_after[i]);
2084 free(ctx->commit_graph_hash_after[i]);
2087 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2088 free(ctx->commit_graph_filenames_before[i]);
2090 free(ctx->commit_graph_filenames_after);
2091 free(ctx->commit_graph_filenames_before);
2092 free(ctx->commit_graph_hash_after);
2100 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2101 static int verify_commit_graph_error;
2103 static void graph_report(const char *fmt, ...)
2107 verify_commit_graph_error = 1;
2109 vfprintf(stderr, fmt, ap);
2110 fprintf(stderr, "\n");
2114 #define GENERATION_ZERO_EXISTS 1
2115 #define GENERATION_NUMBER_EXISTS 2
2117 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2119 uint32_t i, cur_fanout_pos = 0;
2120 struct object_id prev_oid, cur_oid, checksum;
2121 int generation_zero = 0;
2124 struct progress *progress = NULL;
2125 int local_error = 0;
2128 graph_report("no commit-graph file loaded");
2132 verify_commit_graph_error = verify_commit_graph_lite(g);
2133 if (verify_commit_graph_error)
2134 return verify_commit_graph_error;
2136 devnull = open("/dev/null", O_WRONLY);
2137 f = hashfd(devnull, NULL);
2138 hashwrite(f, g->data, g->data_len - g->hash_len);
2139 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2140 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2141 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2142 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2145 for (i = 0; i < g->num_commits; i++) {
2146 struct commit *graph_commit;
2148 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2150 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2151 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2152 oid_to_hex(&prev_oid),
2153 oid_to_hex(&cur_oid));
2155 oidcpy(&prev_oid, &cur_oid);
2157 while (cur_oid.hash[0] > cur_fanout_pos) {
2158 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2160 if (i != fanout_value)
2161 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2162 cur_fanout_pos, fanout_value, i);
2166 graph_commit = lookup_commit(r, &cur_oid);
2167 if (!parse_commit_in_graph_one(r, g, graph_commit))
2168 graph_report(_("failed to parse commit %s from commit-graph"),
2169 oid_to_hex(&cur_oid));
2172 while (cur_fanout_pos < 256) {
2173 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2175 if (g->num_commits != fanout_value)
2176 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2177 cur_fanout_pos, fanout_value, i);
2182 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2183 return verify_commit_graph_error;
2185 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2186 progress = start_progress(_("Verifying commits in commit graph"),
2189 for (i = 0; i < g->num_commits; i++) {
2190 struct commit *graph_commit, *odb_commit;
2191 struct commit_list *graph_parents, *odb_parents;
2192 uint32_t max_generation = 0;
2194 display_progress(progress, i + 1);
2195 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2197 graph_commit = lookup_commit(r, &cur_oid);
2198 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2199 if (parse_commit_internal(odb_commit, 0, 0)) {
2200 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2201 oid_to_hex(&cur_oid));
2205 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2206 get_commit_tree_oid(odb_commit)))
2207 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2208 oid_to_hex(&cur_oid),
2209 oid_to_hex(get_commit_tree_oid(graph_commit)),
2210 oid_to_hex(get_commit_tree_oid(odb_commit)));
2212 graph_parents = graph_commit->parents;
2213 odb_parents = odb_commit->parents;
2215 while (graph_parents) {
2216 if (odb_parents == NULL) {
2217 graph_report(_("commit-graph parent list for commit %s is too long"),
2218 oid_to_hex(&cur_oid));
2222 /* parse parent in case it is in a base graph */
2223 parse_commit_in_graph_one(r, g, graph_parents->item);
2225 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2226 graph_report(_("commit-graph parent for %s is %s != %s"),
2227 oid_to_hex(&cur_oid),
2228 oid_to_hex(&graph_parents->item->object.oid),
2229 oid_to_hex(&odb_parents->item->object.oid));
2231 if (graph_parents->item->generation > max_generation)
2232 max_generation = graph_parents->item->generation;
2234 graph_parents = graph_parents->next;
2235 odb_parents = odb_parents->next;
2238 if (odb_parents != NULL)
2239 graph_report(_("commit-graph parent list for commit %s terminates early"),
2240 oid_to_hex(&cur_oid));
2242 if (!graph_commit->generation) {
2243 if (generation_zero == GENERATION_NUMBER_EXISTS)
2244 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2245 oid_to_hex(&cur_oid));
2246 generation_zero = GENERATION_ZERO_EXISTS;
2247 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2248 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2249 oid_to_hex(&cur_oid));
2251 if (generation_zero == GENERATION_ZERO_EXISTS)
2255 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2256 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2257 * extra logic in the following condition.
2259 if (max_generation == GENERATION_NUMBER_MAX)
2262 if (graph_commit->generation != max_generation + 1)
2263 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2264 oid_to_hex(&cur_oid),
2265 graph_commit->generation,
2266 max_generation + 1);
2268 if (graph_commit->date != odb_commit->date)
2269 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2270 oid_to_hex(&cur_oid),
2274 stop_progress(&progress);
2276 local_error = verify_commit_graph_error;
2278 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2279 local_error |= verify_commit_graph(r, g->base_graph, flags);
2284 void free_commit_graph(struct commit_graph *g)
2288 if (g->graph_fd >= 0) {
2289 munmap((void *)g->data, g->data_len);
2294 free(g->bloom_filter_settings);
2298 void disable_commit_graph(struct repository *r)
2300 r->commit_graph_disabled = 1;