commit-graph: factor out chain opening function
[git.git] / commit-graph.c
blob12cdd9af8e86b0067e1288062d94683a066db72a
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-file.h"
15 #include "object-store-ll.h"
16 #include "oid-array.h"
17 #include "path.h"
18 #include "alloc.h"
19 #include "hashmap.h"
20 #include "replace-object.h"
21 #include "progress.h"
22 #include "bloom.h"
23 #include "commit-slab.h"
24 #include "shallow.h"
25 #include "json-writer.h"
26 #include "trace2.h"
27 #include "tree.h"
28 #include "chunk-format.h"
30 void git_test_write_commit_graph_or_die(void)
32 int flags = 0;
33 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
34 return;
36 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
37 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
39 if (write_commit_graph_reachable(the_repository->objects->odb,
40 flags, NULL))
41 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
44 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
45 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
46 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
47 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
48 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
49 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
50 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
51 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
52 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
53 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
55 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
57 #define GRAPH_VERSION_1 0x1
58 #define GRAPH_VERSION GRAPH_VERSION_1
60 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
62 #define GRAPH_PARENT_NONE 0x70000000
64 #define GRAPH_LAST_EDGE 0x80000000
66 #define GRAPH_HEADER_SIZE 8
67 #define GRAPH_FANOUT_SIZE (4 * 256)
68 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
69 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
71 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
73 /* Remember to update object flag allocation in object.h */
74 #define REACHABLE (1u<<15)
76 define_commit_slab(topo_level_slab, uint32_t);
78 /* Keep track of the order in which commits are added to our list. */
79 define_commit_slab(commit_pos, int);
80 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
82 static void set_commit_pos(struct repository *r, const struct object_id *oid)
84 static int32_t max_pos;
85 struct commit *commit = lookup_commit(r, oid);
87 if (!commit)
88 return; /* should never happen, but be lenient */
90 *commit_pos_at(&commit_pos, commit) = max_pos++;
93 static int commit_pos_cmp(const void *va, const void *vb)
95 const struct commit *a = *(const struct commit **)va;
96 const struct commit *b = *(const struct commit **)vb;
97 return commit_pos_at(&commit_pos, a) -
98 commit_pos_at(&commit_pos, b);
101 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
102 static struct commit_graph_data_slab commit_graph_data_slab =
103 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
105 static int get_configured_generation_version(struct repository *r)
107 int version = 2;
108 repo_config_get_int(r, "commitgraph.generationversion", &version);
109 return version;
112 uint32_t commit_graph_position(const struct commit *c)
114 struct commit_graph_data *data =
115 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
117 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
120 timestamp_t commit_graph_generation(const struct commit *c)
122 struct commit_graph_data *data =
123 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
125 if (data && data->generation)
126 return data->generation;
128 return GENERATION_NUMBER_INFINITY;
131 static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
133 struct commit_graph_data *data =
134 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
136 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
137 return GENERATION_NUMBER_INFINITY;
138 return data->generation;
141 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
143 unsigned int i, nth_slab;
144 struct commit_graph_data *data =
145 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
147 if (data)
148 return data;
150 nth_slab = c->index / commit_graph_data_slab.slab_size;
151 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
154 * commit-slab initializes elements with zero, overwrite this with
155 * COMMIT_NOT_FROM_GRAPH for graph_pos.
157 * We avoid initializing generation with checking if graph position
158 * is not COMMIT_NOT_FROM_GRAPH.
160 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
161 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
162 COMMIT_NOT_FROM_GRAPH;
165 return data;
169 * Should be used only while writing commit-graph as it compares
170 * generation value of commits by directly accessing commit-slab.
172 static int commit_gen_cmp(const void *va, const void *vb)
174 const struct commit *a = *(const struct commit **)va;
175 const struct commit *b = *(const struct commit **)vb;
177 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
178 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
179 /* lower generation commits first */
180 if (generation_a < generation_b)
181 return -1;
182 else if (generation_a > generation_b)
183 return 1;
185 /* use date as a heuristic when generations are equal */
186 if (a->date < b->date)
187 return -1;
188 else if (a->date > b->date)
189 return 1;
190 return 0;
193 char *get_commit_graph_filename(struct object_directory *obj_dir)
195 return xstrfmt("%s/info/commit-graph", obj_dir->path);
198 static char *get_split_graph_filename(struct object_directory *odb,
199 const char *oid_hex)
201 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
202 oid_hex);
205 char *get_commit_graph_chain_filename(struct object_directory *odb)
207 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
210 static struct commit_graph *alloc_commit_graph(void)
212 struct commit_graph *g = xcalloc(1, sizeof(*g));
214 return g;
217 static int commit_graph_compatible(struct repository *r)
219 if (!r->gitdir)
220 return 0;
222 if (replace_refs_enabled(r)) {
223 prepare_replace_object(r);
224 if (hashmap_get_size(&r->objects->replace_map->map))
225 return 0;
228 prepare_commit_graft(r);
229 if (r->parsed_objects &&
230 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
231 return 0;
232 if (is_repository_shallow(r))
233 return 0;
235 return 1;
238 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
240 *fd = git_open(graph_file);
241 if (*fd < 0)
242 return 0;
243 if (fstat(*fd, st)) {
244 close(*fd);
245 return 0;
247 return 1;
250 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251 int fd, struct stat *st,
252 struct object_directory *odb)
254 void *graph_map;
255 size_t graph_size;
256 struct commit_graph *ret;
258 graph_size = xsize_t(st->st_size);
260 if (graph_size < GRAPH_MIN_SIZE) {
261 close(fd);
262 error(_("commit-graph file is too small"));
263 return NULL;
265 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 close(fd);
267 prepare_repo_settings(r);
268 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
270 if (ret)
271 ret->odb = odb;
272 else
273 munmap(graph_map, graph_size);
275 return ret;
278 static int verify_commit_graph_lite(struct commit_graph *g)
281 * Basic validation shared between parse_commit_graph()
282 * which'll be called every time the graph is used, and the
283 * much more expensive verify_commit_graph() used by
284 * "commit-graph verify".
286 * There should only be very basic checks here to ensure that
287 * we don't e.g. segfault in fill_commit_in_graph(), but
288 * because this is a very hot codepath nothing that e.g. loops
289 * over g->num_commits, or runs a checksum on the commit-graph
290 * itself.
292 if (!g->chunk_oid_fanout) {
293 error("commit-graph is missing the OID Fanout chunk");
294 return 1;
296 if (!g->chunk_oid_lookup) {
297 error("commit-graph is missing the OID Lookup chunk");
298 return 1;
300 if (!g->chunk_commit_data) {
301 error("commit-graph is missing the Commit Data chunk");
302 return 1;
305 return 0;
308 static int graph_read_oid_lookup(const unsigned char *chunk_start,
309 size_t chunk_size, void *data)
311 struct commit_graph *g = data;
312 g->chunk_oid_lookup = chunk_start;
313 g->num_commits = chunk_size / g->hash_len;
314 return 0;
317 static int graph_read_bloom_data(const unsigned char *chunk_start,
318 size_t chunk_size, void *data)
320 struct commit_graph *g = data;
321 uint32_t hash_version;
322 g->chunk_bloom_data = chunk_start;
323 hash_version = get_be32(chunk_start);
325 if (hash_version != 1)
326 return 0;
328 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
329 g->bloom_filter_settings->hash_version = hash_version;
330 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
331 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
332 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
334 return 0;
337 struct commit_graph *parse_commit_graph(struct repo_settings *s,
338 void *graph_map, size_t graph_size)
340 const unsigned char *data;
341 struct commit_graph *graph;
342 uint32_t graph_signature;
343 unsigned char graph_version, hash_version;
344 struct chunkfile *cf = NULL;
346 if (!graph_map)
347 return NULL;
349 if (graph_size < GRAPH_MIN_SIZE)
350 return NULL;
352 data = (const unsigned char *)graph_map;
354 graph_signature = get_be32(data);
355 if (graph_signature != GRAPH_SIGNATURE) {
356 error(_("commit-graph signature %X does not match signature %X"),
357 graph_signature, GRAPH_SIGNATURE);
358 return NULL;
361 graph_version = *(unsigned char*)(data + 4);
362 if (graph_version != GRAPH_VERSION) {
363 error(_("commit-graph version %X does not match version %X"),
364 graph_version, GRAPH_VERSION);
365 return NULL;
368 hash_version = *(unsigned char*)(data + 5);
369 if (hash_version != oid_version(the_hash_algo)) {
370 error(_("commit-graph hash version %X does not match version %X"),
371 hash_version, oid_version(the_hash_algo));
372 return NULL;
375 graph = alloc_commit_graph();
377 graph->hash_len = the_hash_algo->rawsz;
378 graph->num_chunks = *(unsigned char*)(data + 6);
379 graph->data = graph_map;
380 graph->data_len = graph_size;
382 if (graph_size < GRAPH_HEADER_SIZE +
383 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
384 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
385 error(_("commit-graph file is too small to hold %u chunks"),
386 graph->num_chunks);
387 free(graph);
388 return NULL;
391 cf = init_chunkfile(NULL);
393 if (read_table_of_contents(cf, graph->data, graph_size,
394 GRAPH_HEADER_SIZE, graph->num_chunks))
395 goto free_and_return;
397 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
398 (const unsigned char **)&graph->chunk_oid_fanout);
399 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
400 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
401 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
402 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
404 if (s->commit_graph_generation_version >= 2) {
405 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
406 &graph->chunk_generation_data);
407 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
408 &graph->chunk_generation_data_overflow);
410 if (graph->chunk_generation_data)
411 graph->read_generation_data = 1;
414 if (s->commit_graph_read_changed_paths) {
415 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
416 &graph->chunk_bloom_indexes);
417 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
418 graph_read_bloom_data, graph);
421 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
422 init_bloom_filters();
423 } else {
424 /* We need both the bloom chunks to exist together. Else ignore the data */
425 graph->chunk_bloom_indexes = NULL;
426 graph->chunk_bloom_data = NULL;
427 FREE_AND_NULL(graph->bloom_filter_settings);
430 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
432 if (verify_commit_graph_lite(graph))
433 goto free_and_return;
435 free_chunkfile(cf);
436 return graph;
438 free_and_return:
439 free_chunkfile(cf);
440 free(graph->bloom_filter_settings);
441 free(graph);
442 return NULL;
445 static struct commit_graph *load_commit_graph_one(struct repository *r,
446 const char *graph_file,
447 struct object_directory *odb)
450 struct stat st;
451 int fd;
452 struct commit_graph *g;
453 int open_ok = open_commit_graph(graph_file, &fd, &st);
455 if (!open_ok)
456 return NULL;
458 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
460 if (g)
461 g->filename = xstrdup(graph_file);
463 return g;
466 static struct commit_graph *load_commit_graph_v1(struct repository *r,
467 struct object_directory *odb)
469 char *graph_name = get_commit_graph_filename(odb);
470 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
471 free(graph_name);
473 return g;
476 static int add_graph_to_chain(struct commit_graph *g,
477 struct commit_graph *chain,
478 struct object_id *oids,
479 int n)
481 struct commit_graph *cur_g = chain;
483 if (n && !g->chunk_base_graphs) {
484 warning(_("commit-graph has no base graphs chunk"));
485 return 0;
488 while (n) {
489 n--;
491 if (!cur_g ||
492 !oideq(&oids[n], &cur_g->oid) ||
493 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
494 warning(_("commit-graph chain does not match"));
495 return 0;
498 cur_g = cur_g->base_graph;
501 g->base_graph = chain;
503 if (chain) {
504 if (unsigned_add_overflows(chain->num_commits,
505 chain->num_commits_in_base)) {
506 warning(_("commit count in base graph too high: %"PRIuMAX),
507 (uintmax_t)chain->num_commits_in_base);
508 return 0;
510 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
513 return 1;
516 int open_commit_graph_chain(const char *chain_file,
517 int *fd, struct stat *st)
519 *fd = git_open(chain_file);
520 if (*fd < 0)
521 return 0;
522 if (fstat(*fd, st)) {
523 close(*fd);
524 return 0;
526 if (st->st_size <= the_hash_algo->hexsz) {
527 close(*fd);
528 errno = ENOENT;
529 return 0;
531 return 1;
534 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
535 int fd, struct stat *st)
537 struct commit_graph *graph_chain = NULL;
538 struct strbuf line = STRBUF_INIT;
539 struct object_id *oids;
540 int i = 0, valid = 1, count;
541 FILE *fp = xfdopen(fd, "r");
543 count = st->st_size / (the_hash_algo->hexsz + 1);
544 CALLOC_ARRAY(oids, count);
546 prepare_alt_odb(r);
548 for (i = 0; i < count; i++) {
549 struct object_directory *odb;
551 if (strbuf_getline_lf(&line, fp) == EOF)
552 break;
554 if (get_oid_hex(line.buf, &oids[i])) {
555 warning(_("invalid commit-graph chain: line '%s' not a hash"),
556 line.buf);
557 valid = 0;
558 break;
561 valid = 0;
562 for (odb = r->objects->odb; odb; odb = odb->next) {
563 char *graph_name = get_split_graph_filename(odb, line.buf);
564 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
566 free(graph_name);
568 if (g) {
569 if (add_graph_to_chain(g, graph_chain, oids, i)) {
570 graph_chain = g;
571 valid = 1;
574 break;
578 if (!valid) {
579 warning(_("unable to find all commit-graph files"));
580 break;
584 free(oids);
585 fclose(fp);
586 strbuf_release(&line);
588 return graph_chain;
591 static struct commit_graph *load_commit_graph_chain(struct repository *r,
592 struct object_directory *odb)
594 char *chain_file = get_commit_graph_chain_filename(odb);
595 struct stat st;
596 int fd;
597 struct commit_graph *g = NULL;
599 if (open_commit_graph_chain(chain_file, &fd, &st)) {
600 /* ownership of fd is taken over by load function */
601 g = load_commit_graph_chain_fd_st(r, fd, &st);
604 free(chain_file);
605 return g;
609 * returns 1 if and only if all graphs in the chain have
610 * corrected commit dates stored in the generation_data chunk.
612 static int validate_mixed_generation_chain(struct commit_graph *g)
614 int read_generation_data = 1;
615 struct commit_graph *p = g;
617 while (read_generation_data && p) {
618 read_generation_data = p->read_generation_data;
619 p = p->base_graph;
622 if (read_generation_data)
623 return 1;
625 while (g) {
626 g->read_generation_data = 0;
627 g = g->base_graph;
630 return 0;
633 struct commit_graph *read_commit_graph_one(struct repository *r,
634 struct object_directory *odb)
636 struct commit_graph *g = load_commit_graph_v1(r, odb);
638 if (!g)
639 g = load_commit_graph_chain(r, odb);
641 validate_mixed_generation_chain(g);
643 return g;
646 static void prepare_commit_graph_one(struct repository *r,
647 struct object_directory *odb)
650 if (r->objects->commit_graph)
651 return;
653 r->objects->commit_graph = read_commit_graph_one(r, odb);
657 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
659 * On the first invocation, this function attempts to load the commit
660 * graph if the_repository is configured to have one.
662 static int prepare_commit_graph(struct repository *r)
664 struct object_directory *odb;
667 * Early return if there is no git dir or if the commit graph is
668 * disabled.
670 * This must come before the "already attempted?" check below, because
671 * we want to disable even an already-loaded graph file.
673 if (!r->gitdir || r->commit_graph_disabled)
674 return 0;
676 if (r->objects->commit_graph_attempted)
677 return !!r->objects->commit_graph;
678 r->objects->commit_graph_attempted = 1;
680 prepare_repo_settings(r);
682 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
683 r->settings.core_commit_graph != 1)
685 * This repository is not configured to use commit graphs, so
686 * do not load one. (But report commit_graph_attempted anyway
687 * so that commit graph loading is not attempted again for this
688 * repository.)
690 return 0;
692 if (!commit_graph_compatible(r))
693 return 0;
695 prepare_alt_odb(r);
696 for (odb = r->objects->odb;
697 !r->objects->commit_graph && odb;
698 odb = odb->next)
699 prepare_commit_graph_one(r, odb);
700 return !!r->objects->commit_graph;
703 int generation_numbers_enabled(struct repository *r)
705 uint32_t first_generation;
706 struct commit_graph *g;
707 if (!prepare_commit_graph(r))
708 return 0;
710 g = r->objects->commit_graph;
712 if (!g->num_commits)
713 return 0;
715 first_generation = get_be32(g->chunk_commit_data +
716 g->hash_len + 8) >> 2;
718 return !!first_generation;
721 int corrected_commit_dates_enabled(struct repository *r)
723 struct commit_graph *g;
724 if (!prepare_commit_graph(r))
725 return 0;
727 g = r->objects->commit_graph;
729 if (!g->num_commits)
730 return 0;
732 return g->read_generation_data;
735 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
737 struct commit_graph *g = r->objects->commit_graph;
738 while (g) {
739 if (g->bloom_filter_settings)
740 return g->bloom_filter_settings;
741 g = g->base_graph;
743 return NULL;
746 static void close_commit_graph_one(struct commit_graph *g)
748 if (!g)
749 return;
751 clear_commit_graph_data_slab(&commit_graph_data_slab);
752 close_commit_graph_one(g->base_graph);
753 free_commit_graph(g);
756 void close_commit_graph(struct raw_object_store *o)
758 close_commit_graph_one(o->commit_graph);
759 o->commit_graph = NULL;
762 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
764 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
765 g->chunk_oid_lookup, g->hash_len, pos);
768 static void load_oid_from_graph(struct commit_graph *g,
769 uint32_t pos,
770 struct object_id *oid)
772 uint32_t lex_index;
774 while (g && pos < g->num_commits_in_base)
775 g = g->base_graph;
777 if (!g)
778 BUG("NULL commit-graph");
780 if (pos >= g->num_commits + g->num_commits_in_base)
781 die(_("invalid commit position. commit-graph is likely corrupt"));
783 lex_index = pos - g->num_commits_in_base;
785 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
788 static struct commit_list **insert_parent_or_die(struct repository *r,
789 struct commit_graph *g,
790 uint32_t pos,
791 struct commit_list **pptr)
793 struct commit *c;
794 struct object_id oid;
796 if (pos >= g->num_commits + g->num_commits_in_base)
797 die("invalid parent position %"PRIu32, pos);
799 load_oid_from_graph(g, pos, &oid);
800 c = lookup_commit(r, &oid);
801 if (!c)
802 die(_("could not find commit %s"), oid_to_hex(&oid));
803 commit_graph_data_at(c)->graph_pos = pos;
804 return &commit_list_insert(c, pptr)->next;
807 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
809 const unsigned char *commit_data;
810 struct commit_graph_data *graph_data;
811 uint32_t lex_index, offset_pos;
812 uint64_t date_high, date_low, offset;
814 while (pos < g->num_commits_in_base)
815 g = g->base_graph;
817 if (pos >= g->num_commits + g->num_commits_in_base)
818 die(_("invalid commit position. commit-graph is likely corrupt"));
820 lex_index = pos - g->num_commits_in_base;
821 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
823 graph_data = commit_graph_data_at(item);
824 graph_data->graph_pos = pos;
826 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
827 date_low = get_be32(commit_data + g->hash_len + 12);
828 item->date = (timestamp_t)((date_high << 32) | date_low);
830 if (g->read_generation_data) {
831 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
833 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
834 if (!g->chunk_generation_data_overflow)
835 die(_("commit-graph requires overflow generation data but has none"));
837 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
838 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + st_mult(8, offset_pos));
839 } else
840 graph_data->generation = item->date + offset;
841 } else
842 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
844 if (g->topo_levels)
845 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
848 static inline void set_commit_tree(struct commit *c, struct tree *t)
850 c->maybe_tree = t;
853 static int fill_commit_in_graph(struct repository *r,
854 struct commit *item,
855 struct commit_graph *g, uint32_t pos)
857 uint32_t edge_value;
858 uint32_t *parent_data_ptr;
859 struct commit_list **pptr;
860 const unsigned char *commit_data;
861 uint32_t lex_index;
863 while (pos < g->num_commits_in_base)
864 g = g->base_graph;
866 fill_commit_graph_info(item, g, pos);
868 lex_index = pos - g->num_commits_in_base;
869 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
871 item->object.parsed = 1;
873 set_commit_tree(item, NULL);
875 pptr = &item->parents;
877 edge_value = get_be32(commit_data + g->hash_len);
878 if (edge_value == GRAPH_PARENT_NONE)
879 return 1;
880 pptr = insert_parent_or_die(r, g, edge_value, pptr);
882 edge_value = get_be32(commit_data + g->hash_len + 4);
883 if (edge_value == GRAPH_PARENT_NONE)
884 return 1;
885 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
886 pptr = insert_parent_or_die(r, g, edge_value, pptr);
887 return 1;
890 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
891 st_mult(4, edge_value & GRAPH_EDGE_LAST_MASK));
892 do {
893 edge_value = get_be32(parent_data_ptr);
894 pptr = insert_parent_or_die(r, g,
895 edge_value & GRAPH_EDGE_LAST_MASK,
896 pptr);
897 parent_data_ptr++;
898 } while (!(edge_value & GRAPH_LAST_EDGE));
900 return 1;
903 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
905 struct commit_graph *cur_g = g;
906 uint32_t lex_index;
908 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
909 cur_g = cur_g->base_graph;
911 if (cur_g) {
912 *pos = lex_index + cur_g->num_commits_in_base;
913 return 1;
916 return 0;
919 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
921 uint32_t graph_pos = commit_graph_position(item);
922 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
923 *pos = graph_pos;
924 return 1;
925 } else {
926 return search_commit_pos_in_graph(&item->object.oid, g, pos);
930 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
931 uint32_t *pos)
933 if (!prepare_commit_graph(r))
934 return 0;
935 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
938 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
940 struct commit *commit;
941 uint32_t pos;
943 if (!prepare_commit_graph(repo))
944 return NULL;
945 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
946 return NULL;
947 if (!has_object(repo, id, 0))
948 return NULL;
950 commit = lookup_commit(repo, id);
951 if (!commit)
952 return NULL;
953 if (commit->object.parsed)
954 return commit;
956 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
957 return NULL;
959 return commit;
962 static int parse_commit_in_graph_one(struct repository *r,
963 struct commit_graph *g,
964 struct commit *item)
966 uint32_t pos;
968 if (item->object.parsed)
969 return 1;
971 if (find_commit_pos_in_graph(item, g, &pos))
972 return fill_commit_in_graph(r, item, g, pos);
974 return 0;
977 int parse_commit_in_graph(struct repository *r, struct commit *item)
979 static int checked_env = 0;
981 if (!checked_env &&
982 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
983 die("dying as requested by the '%s' variable on commit-graph parse!",
984 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
985 checked_env = 1;
987 if (!prepare_commit_graph(r))
988 return 0;
989 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
992 void load_commit_graph_info(struct repository *r, struct commit *item)
994 uint32_t pos;
995 if (repo_find_commit_pos_in_graph(r, item, &pos))
996 fill_commit_graph_info(item, r->objects->commit_graph, pos);
999 static struct tree *load_tree_for_commit(struct repository *r,
1000 struct commit_graph *g,
1001 struct commit *c)
1003 struct object_id oid;
1004 const unsigned char *commit_data;
1005 uint32_t graph_pos = commit_graph_position(c);
1007 while (graph_pos < g->num_commits_in_base)
1008 g = g->base_graph;
1010 commit_data = g->chunk_commit_data +
1011 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1013 oidread(&oid, commit_data);
1014 set_commit_tree(c, lookup_tree(r, &oid));
1016 return c->maybe_tree;
1019 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1020 struct commit_graph *g,
1021 const struct commit *c)
1023 if (c->maybe_tree)
1024 return c->maybe_tree;
1025 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1026 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1028 return load_tree_for_commit(r, g, (struct commit *)c);
1031 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1033 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1036 struct packed_commit_list {
1037 struct commit **list;
1038 size_t nr;
1039 size_t alloc;
1042 struct write_commit_graph_context {
1043 struct repository *r;
1044 struct object_directory *odb;
1045 char *graph_name;
1046 struct oid_array oids;
1047 struct packed_commit_list commits;
1048 int num_extra_edges;
1049 int num_generation_data_overflows;
1050 unsigned long approx_nr_objects;
1051 struct progress *progress;
1052 int progress_done;
1053 uint64_t progress_cnt;
1055 char *base_graph_name;
1056 int num_commit_graphs_before;
1057 int num_commit_graphs_after;
1058 char **commit_graph_filenames_before;
1059 char **commit_graph_filenames_after;
1060 char **commit_graph_hash_after;
1061 uint32_t new_num_commits_in_base;
1062 struct commit_graph *new_base_graph;
1064 unsigned append:1,
1065 report_progress:1,
1066 split:1,
1067 changed_paths:1,
1068 order_by_pack:1,
1069 write_generation_data:1,
1070 trust_generation_numbers:1;
1072 struct topo_level_slab *topo_levels;
1073 const struct commit_graph_opts *opts;
1074 size_t total_bloom_filter_data_size;
1075 const struct bloom_filter_settings *bloom_settings;
1077 int count_bloom_filter_computed;
1078 int count_bloom_filter_not_computed;
1079 int count_bloom_filter_trunc_empty;
1080 int count_bloom_filter_trunc_large;
1083 static int write_graph_chunk_fanout(struct hashfile *f,
1084 void *data)
1086 struct write_commit_graph_context *ctx = data;
1087 int i, count = 0;
1088 struct commit **list = ctx->commits.list;
1091 * Write the first-level table (the list is sorted,
1092 * but we use a 256-entry lookup to be able to avoid
1093 * having to do eight extra binary search iterations).
1095 for (i = 0; i < 256; i++) {
1096 while (count < ctx->commits.nr) {
1097 if ((*list)->object.oid.hash[0] != i)
1098 break;
1099 display_progress(ctx->progress, ++ctx->progress_cnt);
1100 count++;
1101 list++;
1104 hashwrite_be32(f, count);
1107 return 0;
1110 static int write_graph_chunk_oids(struct hashfile *f,
1111 void *data)
1113 struct write_commit_graph_context *ctx = data;
1114 struct commit **list = ctx->commits.list;
1115 int count;
1116 for (count = 0; count < ctx->commits.nr; count++, list++) {
1117 display_progress(ctx->progress, ++ctx->progress_cnt);
1118 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1121 return 0;
1124 static const struct object_id *commit_to_oid(size_t index, const void *table)
1126 const struct commit * const *commits = table;
1127 return &commits[index]->object.oid;
1130 static int write_graph_chunk_data(struct hashfile *f,
1131 void *data)
1133 struct write_commit_graph_context *ctx = data;
1134 struct commit **list = ctx->commits.list;
1135 struct commit **last = ctx->commits.list + ctx->commits.nr;
1136 uint32_t num_extra_edges = 0;
1138 while (list < last) {
1139 struct commit_list *parent;
1140 struct object_id *tree;
1141 int edge_value;
1142 uint32_t packedDate[2];
1143 display_progress(ctx->progress, ++ctx->progress_cnt);
1145 if (repo_parse_commit_no_graph(ctx->r, *list))
1146 die(_("unable to parse commit %s"),
1147 oid_to_hex(&(*list)->object.oid));
1148 tree = get_commit_tree_oid(*list);
1149 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1151 parent = (*list)->parents;
1153 if (!parent)
1154 edge_value = GRAPH_PARENT_NONE;
1155 else {
1156 edge_value = oid_pos(&parent->item->object.oid,
1157 ctx->commits.list,
1158 ctx->commits.nr,
1159 commit_to_oid);
1161 if (edge_value >= 0)
1162 edge_value += ctx->new_num_commits_in_base;
1163 else if (ctx->new_base_graph) {
1164 uint32_t pos;
1165 if (find_commit_pos_in_graph(parent->item,
1166 ctx->new_base_graph,
1167 &pos))
1168 edge_value = pos;
1171 if (edge_value < 0)
1172 BUG("missing parent %s for commit %s",
1173 oid_to_hex(&parent->item->object.oid),
1174 oid_to_hex(&(*list)->object.oid));
1177 hashwrite_be32(f, edge_value);
1179 if (parent)
1180 parent = parent->next;
1182 if (!parent)
1183 edge_value = GRAPH_PARENT_NONE;
1184 else if (parent->next)
1185 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1186 else {
1187 edge_value = oid_pos(&parent->item->object.oid,
1188 ctx->commits.list,
1189 ctx->commits.nr,
1190 commit_to_oid);
1192 if (edge_value >= 0)
1193 edge_value += ctx->new_num_commits_in_base;
1194 else if (ctx->new_base_graph) {
1195 uint32_t pos;
1196 if (find_commit_pos_in_graph(parent->item,
1197 ctx->new_base_graph,
1198 &pos))
1199 edge_value = pos;
1202 if (edge_value < 0)
1203 BUG("missing parent %s for commit %s",
1204 oid_to_hex(&parent->item->object.oid),
1205 oid_to_hex(&(*list)->object.oid));
1208 hashwrite_be32(f, edge_value);
1210 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1211 do {
1212 num_extra_edges++;
1213 parent = parent->next;
1214 } while (parent);
1217 if (sizeof((*list)->date) > 4)
1218 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1219 else
1220 packedDate[0] = 0;
1222 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1224 packedDate[1] = htonl((*list)->date);
1225 hashwrite(f, packedDate, 8);
1227 list++;
1230 return 0;
1233 static int write_graph_chunk_generation_data(struct hashfile *f,
1234 void *data)
1236 struct write_commit_graph_context *ctx = data;
1237 int i, num_generation_data_overflows = 0;
1239 for (i = 0; i < ctx->commits.nr; i++) {
1240 struct commit *c = ctx->commits.list[i];
1241 timestamp_t offset;
1242 repo_parse_commit(ctx->r, c);
1243 offset = commit_graph_data_at(c)->generation - c->date;
1244 display_progress(ctx->progress, ++ctx->progress_cnt);
1246 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1247 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1248 num_generation_data_overflows++;
1251 hashwrite_be32(f, offset);
1254 return 0;
1257 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1258 void *data)
1260 struct write_commit_graph_context *ctx = data;
1261 int i;
1262 for (i = 0; i < ctx->commits.nr; i++) {
1263 struct commit *c = ctx->commits.list[i];
1264 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1265 display_progress(ctx->progress, ++ctx->progress_cnt);
1267 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1268 hashwrite_be32(f, offset >> 32);
1269 hashwrite_be32(f, (uint32_t) offset);
1273 return 0;
1276 static int write_graph_chunk_extra_edges(struct hashfile *f,
1277 void *data)
1279 struct write_commit_graph_context *ctx = data;
1280 struct commit **list = ctx->commits.list;
1281 struct commit **last = ctx->commits.list + ctx->commits.nr;
1282 struct commit_list *parent;
1284 while (list < last) {
1285 int num_parents = 0;
1287 display_progress(ctx->progress, ++ctx->progress_cnt);
1289 for (parent = (*list)->parents; num_parents < 3 && parent;
1290 parent = parent->next)
1291 num_parents++;
1293 if (num_parents <= 2) {
1294 list++;
1295 continue;
1298 /* Since num_parents > 2, this initializer is safe. */
1299 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1300 int edge_value = oid_pos(&parent->item->object.oid,
1301 ctx->commits.list,
1302 ctx->commits.nr,
1303 commit_to_oid);
1305 if (edge_value >= 0)
1306 edge_value += ctx->new_num_commits_in_base;
1307 else if (ctx->new_base_graph) {
1308 uint32_t pos;
1309 if (find_commit_pos_in_graph(parent->item,
1310 ctx->new_base_graph,
1311 &pos))
1312 edge_value = pos;
1315 if (edge_value < 0)
1316 BUG("missing parent %s for commit %s",
1317 oid_to_hex(&parent->item->object.oid),
1318 oid_to_hex(&(*list)->object.oid));
1319 else if (!parent->next)
1320 edge_value |= GRAPH_LAST_EDGE;
1322 hashwrite_be32(f, edge_value);
1325 list++;
1328 return 0;
1331 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1332 void *data)
1334 struct write_commit_graph_context *ctx = data;
1335 struct commit **list = ctx->commits.list;
1336 struct commit **last = ctx->commits.list + ctx->commits.nr;
1337 uint32_t cur_pos = 0;
1339 while (list < last) {
1340 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1341 size_t len = filter ? filter->len : 0;
1342 cur_pos += len;
1343 display_progress(ctx->progress, ++ctx->progress_cnt);
1344 hashwrite_be32(f, cur_pos);
1345 list++;
1348 return 0;
1351 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1353 struct json_writer jw = JSON_WRITER_INIT;
1355 jw_object_begin(&jw, 0);
1356 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1357 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1358 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1359 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1360 jw_end(&jw);
1362 trace2_data_json("bloom", ctx->r, "settings", &jw);
1364 jw_release(&jw);
1367 static int write_graph_chunk_bloom_data(struct hashfile *f,
1368 void *data)
1370 struct write_commit_graph_context *ctx = data;
1371 struct commit **list = ctx->commits.list;
1372 struct commit **last = ctx->commits.list + ctx->commits.nr;
1374 trace2_bloom_filter_settings(ctx);
1376 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1377 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1378 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1380 while (list < last) {
1381 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1382 size_t len = filter ? filter->len : 0;
1384 display_progress(ctx->progress, ++ctx->progress_cnt);
1385 if (len)
1386 hashwrite(f, filter->data, len * sizeof(unsigned char));
1387 list++;
1390 return 0;
1393 static int add_packed_commits(const struct object_id *oid,
1394 struct packed_git *pack,
1395 uint32_t pos,
1396 void *data)
1398 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1399 enum object_type type;
1400 off_t offset = nth_packed_object_offset(pack, pos);
1401 struct object_info oi = OBJECT_INFO_INIT;
1403 if (ctx->progress)
1404 display_progress(ctx->progress, ++ctx->progress_done);
1406 oi.typep = &type;
1407 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1408 die(_("unable to get type of object %s"), oid_to_hex(oid));
1410 if (type != OBJ_COMMIT)
1411 return 0;
1413 oid_array_append(&ctx->oids, oid);
1414 set_commit_pos(ctx->r, oid);
1416 return 0;
1419 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1421 struct commit_list *parent;
1422 for (parent = commit->parents; parent; parent = parent->next) {
1423 if (!(parent->item->object.flags & REACHABLE)) {
1424 oid_array_append(&ctx->oids, &parent->item->object.oid);
1425 parent->item->object.flags |= REACHABLE;
1430 static void close_reachable(struct write_commit_graph_context *ctx)
1432 int i;
1433 struct commit *commit;
1434 enum commit_graph_split_flags flags = ctx->opts ?
1435 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1437 if (ctx->report_progress)
1438 ctx->progress = start_delayed_progress(
1439 _("Loading known commits in commit graph"),
1440 ctx->oids.nr);
1441 for (i = 0; i < ctx->oids.nr; i++) {
1442 display_progress(ctx->progress, i + 1);
1443 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1444 if (commit)
1445 commit->object.flags |= REACHABLE;
1447 stop_progress(&ctx->progress);
1450 * As this loop runs, ctx->oids.nr may grow, but not more
1451 * than the number of missing commits in the reachable
1452 * closure.
1454 if (ctx->report_progress)
1455 ctx->progress = start_delayed_progress(
1456 _("Expanding reachable commits in commit graph"),
1458 for (i = 0; i < ctx->oids.nr; i++) {
1459 display_progress(ctx->progress, i + 1);
1460 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1462 if (!commit)
1463 continue;
1464 if (ctx->split) {
1465 if ((!repo_parse_commit(ctx->r, commit) &&
1466 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1467 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1468 add_missing_parents(ctx, commit);
1469 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1470 add_missing_parents(ctx, commit);
1472 stop_progress(&ctx->progress);
1474 if (ctx->report_progress)
1475 ctx->progress = start_delayed_progress(
1476 _("Clearing commit marks in commit graph"),
1477 ctx->oids.nr);
1478 for (i = 0; i < ctx->oids.nr; i++) {
1479 display_progress(ctx->progress, i + 1);
1480 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1482 if (commit)
1483 commit->object.flags &= ~REACHABLE;
1485 stop_progress(&ctx->progress);
1488 struct compute_generation_info {
1489 struct repository *r;
1490 struct packed_commit_list *commits;
1491 struct progress *progress;
1492 int progress_cnt;
1494 timestamp_t (*get_generation)(struct commit *c, void *data);
1495 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1496 void *data;
1499 static timestamp_t compute_generation_from_max(struct commit *c,
1500 timestamp_t max_gen,
1501 int generation_version)
1503 switch (generation_version) {
1504 case 1: /* topological levels */
1505 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1506 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1507 return max_gen + 1;
1509 case 2: /* corrected commit date */
1510 if (c->date && c->date > max_gen)
1511 max_gen = c->date - 1;
1512 return max_gen + 1;
1514 default:
1515 BUG("attempting unimplemented version");
1519 static void compute_reachable_generation_numbers(
1520 struct compute_generation_info *info,
1521 int generation_version)
1523 int i;
1524 struct commit_list *list = NULL;
1526 for (i = 0; i < info->commits->nr; i++) {
1527 struct commit *c = info->commits->list[i];
1528 timestamp_t gen;
1529 repo_parse_commit(info->r, c);
1530 gen = info->get_generation(c, info->data);
1531 display_progress(info->progress, info->progress_cnt + 1);
1533 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1534 continue;
1536 commit_list_insert(c, &list);
1537 while (list) {
1538 struct commit *current = list->item;
1539 struct commit_list *parent;
1540 int all_parents_computed = 1;
1541 uint32_t max_gen = 0;
1543 for (parent = current->parents; parent; parent = parent->next) {
1544 repo_parse_commit(info->r, parent->item);
1545 gen = info->get_generation(parent->item, info->data);
1547 if (gen == GENERATION_NUMBER_ZERO) {
1548 all_parents_computed = 0;
1549 commit_list_insert(parent->item, &list);
1550 break;
1553 if (gen > max_gen)
1554 max_gen = gen;
1557 if (all_parents_computed) {
1558 pop_commit(&list);
1559 gen = compute_generation_from_max(
1560 current, max_gen,
1561 generation_version);
1562 info->set_generation(current, gen, info->data);
1568 static timestamp_t get_topo_level(struct commit *c, void *data)
1570 struct write_commit_graph_context *ctx = data;
1571 return *topo_level_slab_at(ctx->topo_levels, c);
1574 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1576 struct write_commit_graph_context *ctx = data;
1577 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1580 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1582 struct compute_generation_info info = {
1583 .r = ctx->r,
1584 .commits = &ctx->commits,
1585 .get_generation = get_topo_level,
1586 .set_generation = set_topo_level,
1587 .data = ctx,
1590 if (ctx->report_progress)
1591 info.progress = ctx->progress
1592 = start_delayed_progress(
1593 _("Computing commit graph topological levels"),
1594 ctx->commits.nr);
1596 compute_reachable_generation_numbers(&info, 1);
1598 stop_progress(&ctx->progress);
1601 static timestamp_t get_generation_from_graph_data(struct commit *c,
1602 void *data UNUSED)
1604 return commit_graph_data_at(c)->generation;
1607 static void set_generation_v2(struct commit *c, timestamp_t t,
1608 void *data UNUSED)
1610 struct commit_graph_data *g = commit_graph_data_at(c);
1611 g->generation = t;
1614 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1616 int i;
1617 struct compute_generation_info info = {
1618 .r = ctx->r,
1619 .commits = &ctx->commits,
1620 .get_generation = get_generation_from_graph_data,
1621 .set_generation = set_generation_v2,
1624 if (ctx->report_progress)
1625 info.progress = ctx->progress
1626 = start_delayed_progress(
1627 _("Computing commit graph generation numbers"),
1628 ctx->commits.nr);
1630 if (!ctx->trust_generation_numbers) {
1631 for (i = 0; i < ctx->commits.nr; i++) {
1632 struct commit *c = ctx->commits.list[i];
1633 repo_parse_commit(ctx->r, c);
1634 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1638 compute_reachable_generation_numbers(&info, 2);
1640 for (i = 0; i < ctx->commits.nr; i++) {
1641 struct commit *c = ctx->commits.list[i];
1642 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1643 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1644 ctx->num_generation_data_overflows++;
1646 stop_progress(&ctx->progress);
1649 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1650 void *data UNUSED)
1652 commit_graph_data_at(c)->generation = t;
1656 * After this method, all commits reachable from those in the given
1657 * list will have non-zero, non-infinite generation numbers.
1659 void ensure_generations_valid(struct repository *r,
1660 struct commit **commits, size_t nr)
1662 int generation_version = get_configured_generation_version(r);
1663 struct packed_commit_list list = {
1664 .list = commits,
1665 .alloc = nr,
1666 .nr = nr,
1668 struct compute_generation_info info = {
1669 .r = r,
1670 .commits = &list,
1671 .get_generation = get_generation_from_graph_data,
1672 .set_generation = set_generation_in_graph_data,
1675 compute_reachable_generation_numbers(&info, generation_version);
1678 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1680 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1681 ctx->count_bloom_filter_computed);
1682 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1683 ctx->count_bloom_filter_not_computed);
1684 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1685 ctx->count_bloom_filter_trunc_empty);
1686 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1687 ctx->count_bloom_filter_trunc_large);
1690 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1692 int i;
1693 struct progress *progress = NULL;
1694 struct commit **sorted_commits;
1695 int max_new_filters;
1697 init_bloom_filters();
1699 if (ctx->report_progress)
1700 progress = start_delayed_progress(
1701 _("Computing commit changed paths Bloom filters"),
1702 ctx->commits.nr);
1704 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1706 if (ctx->order_by_pack)
1707 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1708 else
1709 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1711 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1712 ctx->opts->max_new_filters : ctx->commits.nr;
1714 for (i = 0; i < ctx->commits.nr; i++) {
1715 enum bloom_filter_computed computed = 0;
1716 struct commit *c = sorted_commits[i];
1717 struct bloom_filter *filter = get_or_compute_bloom_filter(
1718 ctx->r,
1720 ctx->count_bloom_filter_computed < max_new_filters,
1721 ctx->bloom_settings,
1722 &computed);
1723 if (computed & BLOOM_COMPUTED) {
1724 ctx->count_bloom_filter_computed++;
1725 if (computed & BLOOM_TRUNC_EMPTY)
1726 ctx->count_bloom_filter_trunc_empty++;
1727 if (computed & BLOOM_TRUNC_LARGE)
1728 ctx->count_bloom_filter_trunc_large++;
1729 } else if (computed & BLOOM_NOT_COMPUTED)
1730 ctx->count_bloom_filter_not_computed++;
1731 ctx->total_bloom_filter_data_size += filter
1732 ? sizeof(unsigned char) * filter->len : 0;
1733 display_progress(progress, i + 1);
1736 if (trace2_is_enabled())
1737 trace2_bloom_filter_write_statistics(ctx);
1739 free(sorted_commits);
1740 stop_progress(&progress);
1743 struct refs_cb_data {
1744 struct oidset *commits;
1745 struct progress *progress;
1748 static int add_ref_to_set(const char *refname UNUSED,
1749 const struct object_id *oid,
1750 int flags UNUSED, void *cb_data)
1752 struct object_id peeled;
1753 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1755 if (!peel_iterated_oid(oid, &peeled))
1756 oid = &peeled;
1757 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1758 oidset_insert(data->commits, oid);
1760 display_progress(data->progress, oidset_size(data->commits));
1762 return 0;
1765 int write_commit_graph_reachable(struct object_directory *odb,
1766 enum commit_graph_write_flags flags,
1767 const struct commit_graph_opts *opts)
1769 struct oidset commits = OIDSET_INIT;
1770 struct refs_cb_data data;
1771 int result;
1773 memset(&data, 0, sizeof(data));
1774 data.commits = &commits;
1775 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1776 data.progress = start_delayed_progress(
1777 _("Collecting referenced commits"), 0);
1779 for_each_ref(add_ref_to_set, &data);
1781 stop_progress(&data.progress);
1783 result = write_commit_graph(odb, NULL, &commits,
1784 flags, opts);
1786 oidset_clear(&commits);
1787 return result;
1790 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1791 const struct string_list *pack_indexes)
1793 uint32_t i;
1794 struct strbuf progress_title = STRBUF_INIT;
1795 struct strbuf packname = STRBUF_INIT;
1796 int dirlen;
1797 int ret = 0;
1799 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1800 dirlen = packname.len;
1801 if (ctx->report_progress) {
1802 strbuf_addf(&progress_title,
1803 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1804 "Finding commits for commit graph in %"PRIuMAX" packs",
1805 pack_indexes->nr),
1806 (uintmax_t)pack_indexes->nr);
1807 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1808 ctx->progress_done = 0;
1810 for (i = 0; i < pack_indexes->nr; i++) {
1811 struct packed_git *p;
1812 strbuf_setlen(&packname, dirlen);
1813 strbuf_addstr(&packname, pack_indexes->items[i].string);
1814 p = add_packed_git(packname.buf, packname.len, 1);
1815 if (!p) {
1816 ret = error(_("error adding pack %s"), packname.buf);
1817 goto cleanup;
1819 if (open_pack_index(p)) {
1820 ret = error(_("error opening index for %s"), packname.buf);
1821 goto cleanup;
1823 for_each_object_in_pack(p, add_packed_commits, ctx,
1824 FOR_EACH_OBJECT_PACK_ORDER);
1825 close_pack(p);
1826 free(p);
1829 cleanup:
1830 stop_progress(&ctx->progress);
1831 strbuf_release(&progress_title);
1832 strbuf_release(&packname);
1834 return ret;
1837 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1838 struct oidset *commits)
1840 struct oidset_iter iter;
1841 struct object_id *oid;
1843 if (!oidset_size(commits))
1844 return 0;
1846 oidset_iter_init(commits, &iter);
1847 while ((oid = oidset_iter_next(&iter))) {
1848 oid_array_append(&ctx->oids, oid);
1851 return 0;
1854 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1856 if (ctx->report_progress)
1857 ctx->progress = start_delayed_progress(
1858 _("Finding commits for commit graph among packed objects"),
1859 ctx->approx_nr_objects);
1860 for_each_packed_object(add_packed_commits, ctx,
1861 FOR_EACH_OBJECT_PACK_ORDER);
1862 if (ctx->progress_done < ctx->approx_nr_objects)
1863 display_progress(ctx->progress, ctx->approx_nr_objects);
1864 stop_progress(&ctx->progress);
1867 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1869 uint32_t i;
1870 enum commit_graph_split_flags flags = ctx->opts ?
1871 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1873 ctx->num_extra_edges = 0;
1874 if (ctx->report_progress)
1875 ctx->progress = start_delayed_progress(
1876 _("Finding extra edges in commit graph"),
1877 ctx->oids.nr);
1878 oid_array_sort(&ctx->oids);
1879 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1880 unsigned int num_parents;
1882 display_progress(ctx->progress, i + 1);
1884 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1885 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1887 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1888 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1889 continue;
1891 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1892 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1893 else
1894 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1896 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1897 if (num_parents > 2)
1898 ctx->num_extra_edges += num_parents - 1;
1900 ctx->commits.nr++;
1902 stop_progress(&ctx->progress);
1905 static int write_graph_chunk_base_1(struct hashfile *f,
1906 struct commit_graph *g)
1908 int num = 0;
1910 if (!g)
1911 return 0;
1913 num = write_graph_chunk_base_1(f, g->base_graph);
1914 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1915 return num + 1;
1918 static int write_graph_chunk_base(struct hashfile *f,
1919 void *data)
1921 struct write_commit_graph_context *ctx = data;
1922 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1924 if (num != ctx->num_commit_graphs_after - 1) {
1925 error(_("failed to write correct number of base graph ids"));
1926 return -1;
1929 return 0;
1932 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1934 uint32_t i;
1935 int fd;
1936 struct hashfile *f;
1937 struct lock_file lk = LOCK_INIT;
1938 const unsigned hashsz = the_hash_algo->rawsz;
1939 struct strbuf progress_title = STRBUF_INIT;
1940 struct chunkfile *cf;
1941 unsigned char file_hash[GIT_MAX_RAWSZ];
1943 if (ctx->split) {
1944 struct strbuf tmp_file = STRBUF_INIT;
1946 strbuf_addf(&tmp_file,
1947 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1948 ctx->odb->path);
1949 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1950 } else {
1951 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1954 if (safe_create_leading_directories(ctx->graph_name)) {
1955 UNLEAK(ctx->graph_name);
1956 error(_("unable to create leading directories of %s"),
1957 ctx->graph_name);
1958 return -1;
1961 if (ctx->split) {
1962 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1964 hold_lock_file_for_update_mode(&lk, lock_name,
1965 LOCK_DIE_ON_ERROR, 0444);
1966 free(lock_name);
1968 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1969 if (fd < 0) {
1970 error(_("unable to create temporary graph layer"));
1971 return -1;
1974 if (adjust_shared_perm(ctx->graph_name)) {
1975 error(_("unable to adjust shared permissions for '%s'"),
1976 ctx->graph_name);
1977 return -1;
1980 f = hashfd(fd, ctx->graph_name);
1981 } else {
1982 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1983 LOCK_DIE_ON_ERROR, 0444);
1984 fd = get_lock_file_fd(&lk);
1985 f = hashfd(fd, get_lock_file_path(&lk));
1988 cf = init_chunkfile(f);
1990 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1991 write_graph_chunk_fanout);
1992 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
1993 write_graph_chunk_oids);
1994 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
1995 write_graph_chunk_data);
1997 if (ctx->write_generation_data)
1998 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1999 st_mult(sizeof(uint32_t), ctx->commits.nr),
2000 write_graph_chunk_generation_data);
2001 if (ctx->num_generation_data_overflows)
2002 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2003 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2004 write_graph_chunk_generation_data_overflow);
2005 if (ctx->num_extra_edges)
2006 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2007 st_mult(4, ctx->num_extra_edges),
2008 write_graph_chunk_extra_edges);
2009 if (ctx->changed_paths) {
2010 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2011 st_mult(sizeof(uint32_t), ctx->commits.nr),
2012 write_graph_chunk_bloom_indexes);
2013 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2014 st_add(sizeof(uint32_t) * 3,
2015 ctx->total_bloom_filter_data_size),
2016 write_graph_chunk_bloom_data);
2018 if (ctx->num_commit_graphs_after > 1)
2019 add_chunk(cf, GRAPH_CHUNKID_BASE,
2020 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2021 write_graph_chunk_base);
2023 hashwrite_be32(f, GRAPH_SIGNATURE);
2025 hashwrite_u8(f, GRAPH_VERSION);
2026 hashwrite_u8(f, oid_version(the_hash_algo));
2027 hashwrite_u8(f, get_num_chunks(cf));
2028 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2030 if (ctx->report_progress) {
2031 strbuf_addf(&progress_title,
2032 Q_("Writing out commit graph in %d pass",
2033 "Writing out commit graph in %d passes",
2034 get_num_chunks(cf)),
2035 get_num_chunks(cf));
2036 ctx->progress = start_delayed_progress(
2037 progress_title.buf,
2038 st_mult(get_num_chunks(cf), ctx->commits.nr));
2041 write_chunkfile(cf, ctx);
2043 stop_progress(&ctx->progress);
2044 strbuf_release(&progress_title);
2046 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2047 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2048 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2050 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2051 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2052 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2053 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2056 close_commit_graph(ctx->r->objects);
2057 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2058 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2059 free_chunkfile(cf);
2061 if (ctx->split) {
2062 FILE *chainf = fdopen_lock_file(&lk, "w");
2063 char *final_graph_name;
2064 int result;
2066 close(fd);
2068 if (!chainf) {
2069 error(_("unable to open commit-graph chain file"));
2070 return -1;
2073 if (ctx->base_graph_name) {
2074 const char *dest;
2075 int idx = ctx->num_commit_graphs_after - 1;
2076 if (ctx->num_commit_graphs_after > 1)
2077 idx--;
2079 dest = ctx->commit_graph_filenames_after[idx];
2081 if (strcmp(ctx->base_graph_name, dest)) {
2082 result = rename(ctx->base_graph_name, dest);
2084 if (result) {
2085 error(_("failed to rename base commit-graph file"));
2086 return -1;
2089 } else {
2090 char *graph_name = get_commit_graph_filename(ctx->odb);
2091 unlink(graph_name);
2092 free(graph_name);
2095 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2096 final_graph_name = get_split_graph_filename(ctx->odb,
2097 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2098 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2100 result = rename(ctx->graph_name, final_graph_name);
2102 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2103 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2105 if (result) {
2106 error(_("failed to rename temporary commit-graph file"));
2107 return -1;
2111 commit_lock_file(&lk);
2113 return 0;
2116 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2118 struct commit_graph *g;
2119 uint32_t num_commits;
2120 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2121 uint32_t i;
2123 int max_commits = 0;
2124 int size_mult = 2;
2126 if (ctx->opts) {
2127 max_commits = ctx->opts->max_commits;
2129 if (ctx->opts->size_multiple)
2130 size_mult = ctx->opts->size_multiple;
2132 flags = ctx->opts->split_flags;
2135 g = ctx->r->objects->commit_graph;
2136 num_commits = ctx->commits.nr;
2137 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2138 ctx->num_commit_graphs_after = 1;
2139 else
2140 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2142 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2143 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2144 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2145 (max_commits && num_commits > max_commits))) {
2146 if (g->odb != ctx->odb)
2147 break;
2149 if (unsigned_add_overflows(num_commits, g->num_commits))
2150 die(_("cannot merge graphs with %"PRIuMAX", "
2151 "%"PRIuMAX" commits"),
2152 (uintmax_t)num_commits,
2153 (uintmax_t)g->num_commits);
2154 num_commits += g->num_commits;
2155 g = g->base_graph;
2157 ctx->num_commit_graphs_after--;
2161 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2162 ctx->new_base_graph = g;
2163 else if (ctx->num_commit_graphs_after != 1)
2164 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2165 "should be 1 with --split=replace");
2167 if (ctx->num_commit_graphs_after == 2) {
2168 char *old_graph_name = get_commit_graph_filename(g->odb);
2170 if (!strcmp(g->filename, old_graph_name) &&
2171 g->odb != ctx->odb) {
2172 ctx->num_commit_graphs_after = 1;
2173 ctx->new_base_graph = NULL;
2176 free(old_graph_name);
2179 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2180 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2182 for (i = 0; i < ctx->num_commit_graphs_after &&
2183 i < ctx->num_commit_graphs_before; i++)
2184 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2186 i = ctx->num_commit_graphs_before - 1;
2187 g = ctx->r->objects->commit_graph;
2189 while (g) {
2190 if (i < ctx->num_commit_graphs_after)
2191 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2194 * If the topmost remaining layer has generation data chunk, the
2195 * resultant layer also has generation data chunk.
2197 if (i == ctx->num_commit_graphs_after - 2)
2198 ctx->write_generation_data = !!g->chunk_generation_data;
2200 i--;
2201 g = g->base_graph;
2205 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2206 struct commit_graph *g)
2208 uint32_t i;
2209 uint32_t offset = g->num_commits_in_base;
2211 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2212 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2213 oid_to_hex(&g->oid),
2214 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2216 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2218 for (i = 0; i < g->num_commits; i++) {
2219 struct object_id oid;
2220 struct commit *result;
2222 display_progress(ctx->progress, i + 1);
2224 load_oid_from_graph(g, i + offset, &oid);
2226 /* only add commits if they still exist in the repo */
2227 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2229 if (result) {
2230 ctx->commits.list[ctx->commits.nr] = result;
2231 ctx->commits.nr++;
2236 static int commit_compare(const void *_a, const void *_b)
2238 const struct commit *a = *(const struct commit **)_a;
2239 const struct commit *b = *(const struct commit **)_b;
2240 return oidcmp(&a->object.oid, &b->object.oid);
2243 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2245 uint32_t i, dedup_i = 0;
2247 if (ctx->report_progress)
2248 ctx->progress = start_delayed_progress(
2249 _("Scanning merged commits"),
2250 ctx->commits.nr);
2252 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2254 ctx->num_extra_edges = 0;
2255 for (i = 0; i < ctx->commits.nr; i++) {
2256 display_progress(ctx->progress, i + 1);
2258 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2259 &ctx->commits.list[i]->object.oid)) {
2261 * Silently ignore duplicates. These were likely
2262 * created due to a commit appearing in multiple
2263 * layers of the chain, which is unexpected but
2264 * not invalid. We should make sure there is a
2265 * unique copy in the new layer.
2267 } else {
2268 unsigned int num_parents;
2270 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2271 dedup_i++;
2273 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2274 if (num_parents > 2)
2275 ctx->num_extra_edges += num_parents - 1;
2279 ctx->commits.nr = dedup_i;
2281 stop_progress(&ctx->progress);
2284 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2286 struct commit_graph *g = ctx->r->objects->commit_graph;
2287 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2289 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2290 current_graph_number--;
2292 if (ctx->report_progress)
2293 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2295 merge_commit_graph(ctx, g);
2296 stop_progress(&ctx->progress);
2298 g = g->base_graph;
2301 if (g) {
2302 ctx->new_base_graph = g;
2303 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2306 if (ctx->new_base_graph)
2307 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2309 sort_and_scan_merged_commits(ctx);
2312 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2314 uint32_t i;
2315 time_t now = time(NULL);
2317 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2318 struct stat st;
2319 struct utimbuf updated_time;
2321 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2322 continue;
2324 updated_time.actime = st.st_atime;
2325 updated_time.modtime = now;
2326 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2330 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2332 struct strbuf path = STRBUF_INIT;
2333 DIR *dir;
2334 struct dirent *de;
2335 size_t dirnamelen;
2336 timestamp_t expire_time = time(NULL);
2338 if (ctx->opts && ctx->opts->expire_time)
2339 expire_time = ctx->opts->expire_time;
2340 if (!ctx->split) {
2341 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2342 unlink(chain_file_name);
2343 free(chain_file_name);
2344 ctx->num_commit_graphs_after = 0;
2347 strbuf_addstr(&path, ctx->odb->path);
2348 strbuf_addstr(&path, "/info/commit-graphs");
2349 dir = opendir(path.buf);
2351 if (!dir)
2352 goto out;
2354 strbuf_addch(&path, '/');
2355 dirnamelen = path.len;
2356 while ((de = readdir(dir)) != NULL) {
2357 struct stat st;
2358 uint32_t i, found = 0;
2360 strbuf_setlen(&path, dirnamelen);
2361 strbuf_addstr(&path, de->d_name);
2363 if (stat(path.buf, &st) < 0)
2364 continue;
2366 if (st.st_mtime > expire_time)
2367 continue;
2368 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2369 continue;
2371 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2372 if (!strcmp(ctx->commit_graph_filenames_after[i],
2373 path.buf)) {
2374 found = 1;
2375 break;
2379 if (!found)
2380 unlink(path.buf);
2383 out:
2384 if(dir)
2385 closedir(dir);
2386 strbuf_release(&path);
2389 int write_commit_graph(struct object_directory *odb,
2390 const struct string_list *const pack_indexes,
2391 struct oidset *commits,
2392 enum commit_graph_write_flags flags,
2393 const struct commit_graph_opts *opts)
2395 struct repository *r = the_repository;
2396 struct write_commit_graph_context *ctx;
2397 uint32_t i;
2398 int res = 0;
2399 int replace = 0;
2400 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2401 struct topo_level_slab topo_levels;
2403 prepare_repo_settings(r);
2404 if (!r->settings.core_commit_graph) {
2405 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2406 return 0;
2408 if (!commit_graph_compatible(r))
2409 return 0;
2411 CALLOC_ARRAY(ctx, 1);
2412 ctx->r = r;
2413 ctx->odb = odb;
2414 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2415 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2416 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2417 ctx->opts = opts;
2418 ctx->total_bloom_filter_data_size = 0;
2419 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2420 ctx->num_generation_data_overflows = 0;
2422 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2423 bloom_settings.bits_per_entry);
2424 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2425 bloom_settings.num_hashes);
2426 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2427 bloom_settings.max_changed_paths);
2428 ctx->bloom_settings = &bloom_settings;
2430 init_topo_level_slab(&topo_levels);
2431 ctx->topo_levels = &topo_levels;
2433 prepare_commit_graph(ctx->r);
2434 if (ctx->r->objects->commit_graph) {
2435 struct commit_graph *g = ctx->r->objects->commit_graph;
2437 while (g) {
2438 g->topo_levels = &topo_levels;
2439 g = g->base_graph;
2443 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2444 ctx->changed_paths = 1;
2445 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2446 struct commit_graph *g;
2448 g = ctx->r->objects->commit_graph;
2450 /* We have changed-paths already. Keep them in the next graph */
2451 if (g && g->chunk_bloom_data) {
2452 ctx->changed_paths = 1;
2453 ctx->bloom_settings = g->bloom_filter_settings;
2457 if (ctx->split) {
2458 struct commit_graph *g = ctx->r->objects->commit_graph;
2460 while (g) {
2461 ctx->num_commit_graphs_before++;
2462 g = g->base_graph;
2465 if (ctx->num_commit_graphs_before) {
2466 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2467 i = ctx->num_commit_graphs_before;
2468 g = ctx->r->objects->commit_graph;
2470 while (g) {
2471 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2472 g = g->base_graph;
2476 if (ctx->opts)
2477 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2480 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2482 if (ctx->append && ctx->r->objects->commit_graph) {
2483 struct commit_graph *g = ctx->r->objects->commit_graph;
2484 for (i = 0; i < g->num_commits; i++) {
2485 struct object_id oid;
2486 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2487 oid_array_append(&ctx->oids, &oid);
2491 if (pack_indexes) {
2492 ctx->order_by_pack = 1;
2493 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2494 goto cleanup;
2497 if (commits) {
2498 if ((res = fill_oids_from_commits(ctx, commits)))
2499 goto cleanup;
2502 if (!pack_indexes && !commits) {
2503 ctx->order_by_pack = 1;
2504 fill_oids_from_all_packs(ctx);
2507 close_reachable(ctx);
2509 copy_oids_to_commits(ctx);
2511 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2512 error(_("too many commits to write graph"));
2513 res = -1;
2514 goto cleanup;
2517 if (!ctx->commits.nr && !replace)
2518 goto cleanup;
2520 if (ctx->split) {
2521 split_graph_merge_strategy(ctx);
2523 if (!replace)
2524 merge_commit_graphs(ctx);
2525 } else
2526 ctx->num_commit_graphs_after = 1;
2528 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2530 compute_topological_levels(ctx);
2531 if (ctx->write_generation_data)
2532 compute_generation_numbers(ctx);
2534 if (ctx->changed_paths)
2535 compute_bloom_filters(ctx);
2537 res = write_commit_graph_file(ctx);
2539 if (ctx->split)
2540 mark_commit_graphs(ctx);
2542 expire_commit_graphs(ctx);
2544 cleanup:
2545 free(ctx->graph_name);
2546 free(ctx->commits.list);
2547 oid_array_clear(&ctx->oids);
2548 clear_topo_level_slab(&topo_levels);
2550 if (ctx->commit_graph_filenames_after) {
2551 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2552 free(ctx->commit_graph_filenames_after[i]);
2553 free(ctx->commit_graph_hash_after[i]);
2556 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2557 free(ctx->commit_graph_filenames_before[i]);
2559 free(ctx->commit_graph_filenames_after);
2560 free(ctx->commit_graph_filenames_before);
2561 free(ctx->commit_graph_hash_after);
2564 free(ctx);
2566 return res;
2569 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2570 static int verify_commit_graph_error;
2572 __attribute__((format (printf, 1, 2)))
2573 static void graph_report(const char *fmt, ...)
2575 va_list ap;
2577 verify_commit_graph_error = 1;
2578 va_start(ap, fmt);
2579 vfprintf(stderr, fmt, ap);
2580 fprintf(stderr, "\n");
2581 va_end(ap);
2584 static int commit_graph_checksum_valid(struct commit_graph *g)
2586 return hashfile_checksum_valid(g->data, g->data_len);
2589 static int verify_one_commit_graph(struct repository *r,
2590 struct commit_graph *g,
2591 struct progress *progress,
2592 uint64_t *seen)
2594 uint32_t i, cur_fanout_pos = 0;
2595 struct object_id prev_oid, cur_oid;
2596 struct commit *seen_gen_zero = NULL;
2597 struct commit *seen_gen_non_zero = NULL;
2599 verify_commit_graph_error = verify_commit_graph_lite(g);
2600 if (verify_commit_graph_error)
2601 return verify_commit_graph_error;
2603 if (!commit_graph_checksum_valid(g)) {
2604 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2605 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2608 for (i = 0; i < g->num_commits; i++) {
2609 struct commit *graph_commit;
2611 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2613 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2614 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2615 oid_to_hex(&prev_oid),
2616 oid_to_hex(&cur_oid));
2618 oidcpy(&prev_oid, &cur_oid);
2620 while (cur_oid.hash[0] > cur_fanout_pos) {
2621 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2623 if (i != fanout_value)
2624 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2625 cur_fanout_pos, fanout_value, i);
2626 cur_fanout_pos++;
2629 graph_commit = lookup_commit(r, &cur_oid);
2630 if (!parse_commit_in_graph_one(r, g, graph_commit))
2631 graph_report(_("failed to parse commit %s from commit-graph"),
2632 oid_to_hex(&cur_oid));
2635 while (cur_fanout_pos < 256) {
2636 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2638 if (g->num_commits != fanout_value)
2639 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2640 cur_fanout_pos, fanout_value, i);
2642 cur_fanout_pos++;
2645 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2646 return verify_commit_graph_error;
2648 for (i = 0; i < g->num_commits; i++) {
2649 struct commit *graph_commit, *odb_commit;
2650 struct commit_list *graph_parents, *odb_parents;
2651 timestamp_t max_generation = 0;
2652 timestamp_t generation;
2654 display_progress(progress, ++(*seen));
2655 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2657 graph_commit = lookup_commit(r, &cur_oid);
2658 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2659 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2660 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2661 oid_to_hex(&cur_oid));
2662 continue;
2665 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2666 get_commit_tree_oid(odb_commit)))
2667 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2668 oid_to_hex(&cur_oid),
2669 oid_to_hex(get_commit_tree_oid(graph_commit)),
2670 oid_to_hex(get_commit_tree_oid(odb_commit)));
2672 graph_parents = graph_commit->parents;
2673 odb_parents = odb_commit->parents;
2675 while (graph_parents) {
2676 if (!odb_parents) {
2677 graph_report(_("commit-graph parent list for commit %s is too long"),
2678 oid_to_hex(&cur_oid));
2679 break;
2682 /* parse parent in case it is in a base graph */
2683 parse_commit_in_graph_one(r, g, graph_parents->item);
2685 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2686 graph_report(_("commit-graph parent for %s is %s != %s"),
2687 oid_to_hex(&cur_oid),
2688 oid_to_hex(&graph_parents->item->object.oid),
2689 oid_to_hex(&odb_parents->item->object.oid));
2691 generation = commit_graph_generation_from_graph(graph_parents->item);
2692 if (generation > max_generation)
2693 max_generation = generation;
2695 graph_parents = graph_parents->next;
2696 odb_parents = odb_parents->next;
2699 if (odb_parents)
2700 graph_report(_("commit-graph parent list for commit %s terminates early"),
2701 oid_to_hex(&cur_oid));
2703 if (commit_graph_generation_from_graph(graph_commit))
2704 seen_gen_non_zero = graph_commit;
2705 else
2706 seen_gen_zero = graph_commit;
2708 if (seen_gen_zero)
2709 continue;
2712 * If we are using topological level and one of our parents has
2713 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2714 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2715 * in the following condition.
2717 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2718 max_generation--;
2720 generation = commit_graph_generation(graph_commit);
2721 if (generation < max_generation + 1)
2722 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2723 oid_to_hex(&cur_oid),
2724 generation,
2725 max_generation + 1);
2727 if (graph_commit->date != odb_commit->date)
2728 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2729 oid_to_hex(&cur_oid),
2730 graph_commit->date,
2731 odb_commit->date);
2734 if (seen_gen_zero && seen_gen_non_zero)
2735 graph_report(_("commit-graph has both zero and non-zero "
2736 "generations (e.g., commits '%s' and '%s')"),
2737 oid_to_hex(&seen_gen_zero->object.oid),
2738 oid_to_hex(&seen_gen_non_zero->object.oid));
2740 return verify_commit_graph_error;
2743 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2745 struct progress *progress = NULL;
2746 int local_error = 0;
2747 uint64_t seen = 0;
2749 if (!g) {
2750 graph_report("no commit-graph file loaded");
2751 return 1;
2754 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2755 uint64_t total = g->num_commits;
2756 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2757 total += g->num_commits_in_base;
2759 progress = start_progress(_("Verifying commits in commit graph"),
2760 total);
2763 for (; g; g = g->base_graph) {
2764 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2765 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2766 break;
2769 stop_progress(&progress);
2771 return local_error;
2774 void free_commit_graph(struct commit_graph *g)
2776 if (!g)
2777 return;
2778 if (g->data) {
2779 munmap((void *)g->data, g->data_len);
2780 g->data = NULL;
2782 free(g->filename);
2783 free(g->bloom_filter_settings);
2784 free(g);
2787 void disable_commit_graph(struct repository *r)
2789 r->commit_graph_disabled = 1;