documentation: add some commas where they are helpful
[git.git] / commit-graph.c
blob1a56efcf69033dad73d56e448874c41a74ffda1e
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;
477 * returns 1 if and only if all graphs in the chain have
478 * corrected commit dates stored in the generation_data chunk.
480 static int validate_mixed_generation_chain(struct commit_graph *g)
482 int read_generation_data = 1;
483 struct commit_graph *p = g;
485 while (read_generation_data && p) {
486 read_generation_data = p->read_generation_data;
487 p = p->base_graph;
490 if (read_generation_data)
491 return 1;
493 while (g) {
494 g->read_generation_data = 0;
495 g = g->base_graph;
498 return 0;
501 static int add_graph_to_chain(struct commit_graph *g,
502 struct commit_graph *chain,
503 struct object_id *oids,
504 int n)
506 struct commit_graph *cur_g = chain;
508 if (n && !g->chunk_base_graphs) {
509 warning(_("commit-graph has no base graphs chunk"));
510 return 0;
513 while (n) {
514 n--;
516 if (!cur_g ||
517 !oideq(&oids[n], &cur_g->oid) ||
518 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
519 warning(_("commit-graph chain does not match"));
520 return 0;
523 cur_g = cur_g->base_graph;
526 g->base_graph = chain;
528 if (chain) {
529 if (unsigned_add_overflows(chain->num_commits,
530 chain->num_commits_in_base)) {
531 warning(_("commit count in base graph too high: %"PRIuMAX),
532 (uintmax_t)chain->num_commits_in_base);
533 return 0;
535 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
538 return 1;
541 int open_commit_graph_chain(const char *chain_file,
542 int *fd, struct stat *st)
544 *fd = git_open(chain_file);
545 if (*fd < 0)
546 return 0;
547 if (fstat(*fd, st)) {
548 close(*fd);
549 return 0;
551 if (st->st_size < the_hash_algo->hexsz) {
552 close(*fd);
553 if (!st->st_size) {
554 /* treat empty files the same as missing */
555 errno = ENOENT;
556 } else {
557 warning("commit-graph chain file too small");
558 errno = EINVAL;
560 return 0;
562 return 1;
565 struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
566 int fd, struct stat *st,
567 int *incomplete_chain)
569 struct commit_graph *graph_chain = NULL;
570 struct strbuf line = STRBUF_INIT;
571 struct object_id *oids;
572 int i = 0, valid = 1, count;
573 FILE *fp = xfdopen(fd, "r");
575 count = st->st_size / (the_hash_algo->hexsz + 1);
576 CALLOC_ARRAY(oids, count);
578 prepare_alt_odb(r);
580 for (i = 0; i < count; i++) {
581 struct object_directory *odb;
583 if (strbuf_getline_lf(&line, fp) == EOF)
584 break;
586 if (get_oid_hex(line.buf, &oids[i])) {
587 warning(_("invalid commit-graph chain: line '%s' not a hash"),
588 line.buf);
589 valid = 0;
590 break;
593 valid = 0;
594 for (odb = r->objects->odb; odb; odb = odb->next) {
595 char *graph_name = get_split_graph_filename(odb, line.buf);
596 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
598 free(graph_name);
600 if (g) {
601 if (add_graph_to_chain(g, graph_chain, oids, i)) {
602 graph_chain = g;
603 valid = 1;
606 break;
610 if (!valid) {
611 warning(_("unable to find all commit-graph files"));
612 break;
616 validate_mixed_generation_chain(graph_chain);
618 free(oids);
619 fclose(fp);
620 strbuf_release(&line);
622 *incomplete_chain = !valid;
623 return graph_chain;
626 static struct commit_graph *load_commit_graph_chain(struct repository *r,
627 struct object_directory *odb)
629 char *chain_file = get_commit_graph_chain_filename(odb);
630 struct stat st;
631 int fd;
632 struct commit_graph *g = NULL;
634 if (open_commit_graph_chain(chain_file, &fd, &st)) {
635 int incomplete;
636 /* ownership of fd is taken over by load function */
637 g = load_commit_graph_chain_fd_st(r, fd, &st, &incomplete);
640 free(chain_file);
641 return g;
644 struct commit_graph *read_commit_graph_one(struct repository *r,
645 struct object_directory *odb)
647 struct commit_graph *g = load_commit_graph_v1(r, odb);
649 if (!g)
650 g = load_commit_graph_chain(r, odb);
652 return g;
655 static void prepare_commit_graph_one(struct repository *r,
656 struct object_directory *odb)
659 if (r->objects->commit_graph)
660 return;
662 r->objects->commit_graph = read_commit_graph_one(r, odb);
666 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
668 * On the first invocation, this function attempts to load the commit
669 * graph if the_repository is configured to have one.
671 static int prepare_commit_graph(struct repository *r)
673 struct object_directory *odb;
676 * Early return if there is no git dir or if the commit graph is
677 * disabled.
679 * This must come before the "already attempted?" check below, because
680 * we want to disable even an already-loaded graph file.
682 if (!r->gitdir || r->commit_graph_disabled)
683 return 0;
685 if (r->objects->commit_graph_attempted)
686 return !!r->objects->commit_graph;
687 r->objects->commit_graph_attempted = 1;
689 prepare_repo_settings(r);
691 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
692 r->settings.core_commit_graph != 1)
694 * This repository is not configured to use commit graphs, so
695 * do not load one. (But report commit_graph_attempted anyway
696 * so that commit graph loading is not attempted again for this
697 * repository.)
699 return 0;
701 if (!commit_graph_compatible(r))
702 return 0;
704 prepare_alt_odb(r);
705 for (odb = r->objects->odb;
706 !r->objects->commit_graph && odb;
707 odb = odb->next)
708 prepare_commit_graph_one(r, odb);
709 return !!r->objects->commit_graph;
712 int generation_numbers_enabled(struct repository *r)
714 uint32_t first_generation;
715 struct commit_graph *g;
716 if (!prepare_commit_graph(r))
717 return 0;
719 g = r->objects->commit_graph;
721 if (!g->num_commits)
722 return 0;
724 first_generation = get_be32(g->chunk_commit_data +
725 g->hash_len + 8) >> 2;
727 return !!first_generation;
730 int corrected_commit_dates_enabled(struct repository *r)
732 struct commit_graph *g;
733 if (!prepare_commit_graph(r))
734 return 0;
736 g = r->objects->commit_graph;
738 if (!g->num_commits)
739 return 0;
741 return g->read_generation_data;
744 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
746 struct commit_graph *g = r->objects->commit_graph;
747 while (g) {
748 if (g->bloom_filter_settings)
749 return g->bloom_filter_settings;
750 g = g->base_graph;
752 return NULL;
755 static void close_commit_graph_one(struct commit_graph *g)
757 if (!g)
758 return;
760 clear_commit_graph_data_slab(&commit_graph_data_slab);
761 close_commit_graph_one(g->base_graph);
762 free_commit_graph(g);
765 void close_commit_graph(struct raw_object_store *o)
767 close_commit_graph_one(o->commit_graph);
768 o->commit_graph = NULL;
771 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
773 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
774 g->chunk_oid_lookup, g->hash_len, pos);
777 static void load_oid_from_graph(struct commit_graph *g,
778 uint32_t pos,
779 struct object_id *oid)
781 uint32_t lex_index;
783 while (g && pos < g->num_commits_in_base)
784 g = g->base_graph;
786 if (!g)
787 BUG("NULL commit-graph");
789 if (pos >= g->num_commits + g->num_commits_in_base)
790 die(_("invalid commit position. commit-graph is likely corrupt"));
792 lex_index = pos - g->num_commits_in_base;
794 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
797 static struct commit_list **insert_parent_or_die(struct repository *r,
798 struct commit_graph *g,
799 uint32_t pos,
800 struct commit_list **pptr)
802 struct commit *c;
803 struct object_id oid;
805 if (pos >= g->num_commits + g->num_commits_in_base)
806 die("invalid parent position %"PRIu32, pos);
808 load_oid_from_graph(g, pos, &oid);
809 c = lookup_commit(r, &oid);
810 if (!c)
811 die(_("could not find commit %s"), oid_to_hex(&oid));
812 commit_graph_data_at(c)->graph_pos = pos;
813 return &commit_list_insert(c, pptr)->next;
816 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
818 const unsigned char *commit_data;
819 struct commit_graph_data *graph_data;
820 uint32_t lex_index, offset_pos;
821 uint64_t date_high, date_low, offset;
823 while (pos < g->num_commits_in_base)
824 g = g->base_graph;
826 if (pos >= g->num_commits + g->num_commits_in_base)
827 die(_("invalid commit position. commit-graph is likely corrupt"));
829 lex_index = pos - g->num_commits_in_base;
830 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
832 graph_data = commit_graph_data_at(item);
833 graph_data->graph_pos = pos;
835 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
836 date_low = get_be32(commit_data + g->hash_len + 12);
837 item->date = (timestamp_t)((date_high << 32) | date_low);
839 if (g->read_generation_data) {
840 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
842 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
843 if (!g->chunk_generation_data_overflow)
844 die(_("commit-graph requires overflow generation data but has none"));
846 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
847 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + st_mult(8, offset_pos));
848 } else
849 graph_data->generation = item->date + offset;
850 } else
851 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
853 if (g->topo_levels)
854 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
857 static inline void set_commit_tree(struct commit *c, struct tree *t)
859 c->maybe_tree = t;
862 static int fill_commit_in_graph(struct repository *r,
863 struct commit *item,
864 struct commit_graph *g, uint32_t pos)
866 uint32_t edge_value;
867 uint32_t *parent_data_ptr;
868 struct commit_list **pptr;
869 const unsigned char *commit_data;
870 uint32_t lex_index;
872 while (pos < g->num_commits_in_base)
873 g = g->base_graph;
875 fill_commit_graph_info(item, g, pos);
877 lex_index = pos - g->num_commits_in_base;
878 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
880 item->object.parsed = 1;
882 set_commit_tree(item, NULL);
884 pptr = &item->parents;
886 edge_value = get_be32(commit_data + g->hash_len);
887 if (edge_value == GRAPH_PARENT_NONE)
888 return 1;
889 pptr = insert_parent_or_die(r, g, edge_value, pptr);
891 edge_value = get_be32(commit_data + g->hash_len + 4);
892 if (edge_value == GRAPH_PARENT_NONE)
893 return 1;
894 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
895 pptr = insert_parent_or_die(r, g, edge_value, pptr);
896 return 1;
899 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
900 st_mult(4, edge_value & GRAPH_EDGE_LAST_MASK));
901 do {
902 edge_value = get_be32(parent_data_ptr);
903 pptr = insert_parent_or_die(r, g,
904 edge_value & GRAPH_EDGE_LAST_MASK,
905 pptr);
906 parent_data_ptr++;
907 } while (!(edge_value & GRAPH_LAST_EDGE));
909 return 1;
912 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
914 struct commit_graph *cur_g = g;
915 uint32_t lex_index;
917 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
918 cur_g = cur_g->base_graph;
920 if (cur_g) {
921 *pos = lex_index + cur_g->num_commits_in_base;
922 return 1;
925 return 0;
928 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
930 uint32_t graph_pos = commit_graph_position(item);
931 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
932 *pos = graph_pos;
933 return 1;
934 } else {
935 return search_commit_pos_in_graph(&item->object.oid, g, pos);
939 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
940 uint32_t *pos)
942 if (!prepare_commit_graph(r))
943 return 0;
944 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
947 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
949 struct commit *commit;
950 uint32_t pos;
952 if (!prepare_commit_graph(repo))
953 return NULL;
954 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
955 return NULL;
956 if (!has_object(repo, id, 0))
957 return NULL;
959 commit = lookup_commit(repo, id);
960 if (!commit)
961 return NULL;
962 if (commit->object.parsed)
963 return commit;
965 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
966 return NULL;
968 return commit;
971 static int parse_commit_in_graph_one(struct repository *r,
972 struct commit_graph *g,
973 struct commit *item)
975 uint32_t pos;
977 if (item->object.parsed)
978 return 1;
980 if (find_commit_pos_in_graph(item, g, &pos))
981 return fill_commit_in_graph(r, item, g, pos);
983 return 0;
986 int parse_commit_in_graph(struct repository *r, struct commit *item)
988 static int checked_env = 0;
990 if (!checked_env &&
991 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
992 die("dying as requested by the '%s' variable on commit-graph parse!",
993 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
994 checked_env = 1;
996 if (!prepare_commit_graph(r))
997 return 0;
998 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
1001 void load_commit_graph_info(struct repository *r, struct commit *item)
1003 uint32_t pos;
1004 if (repo_find_commit_pos_in_graph(r, item, &pos))
1005 fill_commit_graph_info(item, r->objects->commit_graph, pos);
1008 static struct tree *load_tree_for_commit(struct repository *r,
1009 struct commit_graph *g,
1010 struct commit *c)
1012 struct object_id oid;
1013 const unsigned char *commit_data;
1014 uint32_t graph_pos = commit_graph_position(c);
1016 while (graph_pos < g->num_commits_in_base)
1017 g = g->base_graph;
1019 commit_data = g->chunk_commit_data +
1020 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
1022 oidread(&oid, commit_data);
1023 set_commit_tree(c, lookup_tree(r, &oid));
1025 return c->maybe_tree;
1028 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
1029 struct commit_graph *g,
1030 const struct commit *c)
1032 if (c->maybe_tree)
1033 return c->maybe_tree;
1034 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1035 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1037 return load_tree_for_commit(r, g, (struct commit *)c);
1040 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1042 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1045 struct packed_commit_list {
1046 struct commit **list;
1047 size_t nr;
1048 size_t alloc;
1051 struct write_commit_graph_context {
1052 struct repository *r;
1053 struct object_directory *odb;
1054 char *graph_name;
1055 struct oid_array oids;
1056 struct packed_commit_list commits;
1057 int num_extra_edges;
1058 int num_generation_data_overflows;
1059 unsigned long approx_nr_objects;
1060 struct progress *progress;
1061 int progress_done;
1062 uint64_t progress_cnt;
1064 char *base_graph_name;
1065 int num_commit_graphs_before;
1066 int num_commit_graphs_after;
1067 char **commit_graph_filenames_before;
1068 char **commit_graph_filenames_after;
1069 char **commit_graph_hash_after;
1070 uint32_t new_num_commits_in_base;
1071 struct commit_graph *new_base_graph;
1073 unsigned append:1,
1074 report_progress:1,
1075 split:1,
1076 changed_paths:1,
1077 order_by_pack:1,
1078 write_generation_data:1,
1079 trust_generation_numbers:1;
1081 struct topo_level_slab *topo_levels;
1082 const struct commit_graph_opts *opts;
1083 size_t total_bloom_filter_data_size;
1084 const struct bloom_filter_settings *bloom_settings;
1086 int count_bloom_filter_computed;
1087 int count_bloom_filter_not_computed;
1088 int count_bloom_filter_trunc_empty;
1089 int count_bloom_filter_trunc_large;
1092 static int write_graph_chunk_fanout(struct hashfile *f,
1093 void *data)
1095 struct write_commit_graph_context *ctx = data;
1096 int i, count = 0;
1097 struct commit **list = ctx->commits.list;
1100 * Write the first-level table (the list is sorted,
1101 * but we use a 256-entry lookup to be able to avoid
1102 * having to do eight extra binary search iterations).
1104 for (i = 0; i < 256; i++) {
1105 while (count < ctx->commits.nr) {
1106 if ((*list)->object.oid.hash[0] != i)
1107 break;
1108 display_progress(ctx->progress, ++ctx->progress_cnt);
1109 count++;
1110 list++;
1113 hashwrite_be32(f, count);
1116 return 0;
1119 static int write_graph_chunk_oids(struct hashfile *f,
1120 void *data)
1122 struct write_commit_graph_context *ctx = data;
1123 struct commit **list = ctx->commits.list;
1124 int count;
1125 for (count = 0; count < ctx->commits.nr; count++, list++) {
1126 display_progress(ctx->progress, ++ctx->progress_cnt);
1127 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1130 return 0;
1133 static const struct object_id *commit_to_oid(size_t index, const void *table)
1135 const struct commit * const *commits = table;
1136 return &commits[index]->object.oid;
1139 static int write_graph_chunk_data(struct hashfile *f,
1140 void *data)
1142 struct write_commit_graph_context *ctx = data;
1143 struct commit **list = ctx->commits.list;
1144 struct commit **last = ctx->commits.list + ctx->commits.nr;
1145 uint32_t num_extra_edges = 0;
1147 while (list < last) {
1148 struct commit_list *parent;
1149 struct object_id *tree;
1150 int edge_value;
1151 uint32_t packedDate[2];
1152 display_progress(ctx->progress, ++ctx->progress_cnt);
1154 if (repo_parse_commit_no_graph(ctx->r, *list))
1155 die(_("unable to parse commit %s"),
1156 oid_to_hex(&(*list)->object.oid));
1157 tree = get_commit_tree_oid(*list);
1158 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1160 parent = (*list)->parents;
1162 if (!parent)
1163 edge_value = GRAPH_PARENT_NONE;
1164 else {
1165 edge_value = oid_pos(&parent->item->object.oid,
1166 ctx->commits.list,
1167 ctx->commits.nr,
1168 commit_to_oid);
1170 if (edge_value >= 0)
1171 edge_value += ctx->new_num_commits_in_base;
1172 else if (ctx->new_base_graph) {
1173 uint32_t pos;
1174 if (find_commit_pos_in_graph(parent->item,
1175 ctx->new_base_graph,
1176 &pos))
1177 edge_value = pos;
1180 if (edge_value < 0)
1181 BUG("missing parent %s for commit %s",
1182 oid_to_hex(&parent->item->object.oid),
1183 oid_to_hex(&(*list)->object.oid));
1186 hashwrite_be32(f, edge_value);
1188 if (parent)
1189 parent = parent->next;
1191 if (!parent)
1192 edge_value = GRAPH_PARENT_NONE;
1193 else if (parent->next)
1194 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1195 else {
1196 edge_value = oid_pos(&parent->item->object.oid,
1197 ctx->commits.list,
1198 ctx->commits.nr,
1199 commit_to_oid);
1201 if (edge_value >= 0)
1202 edge_value += ctx->new_num_commits_in_base;
1203 else if (ctx->new_base_graph) {
1204 uint32_t pos;
1205 if (find_commit_pos_in_graph(parent->item,
1206 ctx->new_base_graph,
1207 &pos))
1208 edge_value = pos;
1211 if (edge_value < 0)
1212 BUG("missing parent %s for commit %s",
1213 oid_to_hex(&parent->item->object.oid),
1214 oid_to_hex(&(*list)->object.oid));
1217 hashwrite_be32(f, edge_value);
1219 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1220 do {
1221 num_extra_edges++;
1222 parent = parent->next;
1223 } while (parent);
1226 if (sizeof((*list)->date) > 4)
1227 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1228 else
1229 packedDate[0] = 0;
1231 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1233 packedDate[1] = htonl((*list)->date);
1234 hashwrite(f, packedDate, 8);
1236 list++;
1239 return 0;
1242 static int write_graph_chunk_generation_data(struct hashfile *f,
1243 void *data)
1245 struct write_commit_graph_context *ctx = data;
1246 int i, num_generation_data_overflows = 0;
1248 for (i = 0; i < ctx->commits.nr; i++) {
1249 struct commit *c = ctx->commits.list[i];
1250 timestamp_t offset;
1251 repo_parse_commit(ctx->r, c);
1252 offset = commit_graph_data_at(c)->generation - c->date;
1253 display_progress(ctx->progress, ++ctx->progress_cnt);
1255 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1256 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1257 num_generation_data_overflows++;
1260 hashwrite_be32(f, offset);
1263 return 0;
1266 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1267 void *data)
1269 struct write_commit_graph_context *ctx = data;
1270 int i;
1271 for (i = 0; i < ctx->commits.nr; i++) {
1272 struct commit *c = ctx->commits.list[i];
1273 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1274 display_progress(ctx->progress, ++ctx->progress_cnt);
1276 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1277 hashwrite_be32(f, offset >> 32);
1278 hashwrite_be32(f, (uint32_t) offset);
1282 return 0;
1285 static int write_graph_chunk_extra_edges(struct hashfile *f,
1286 void *data)
1288 struct write_commit_graph_context *ctx = data;
1289 struct commit **list = ctx->commits.list;
1290 struct commit **last = ctx->commits.list + ctx->commits.nr;
1291 struct commit_list *parent;
1293 while (list < last) {
1294 int num_parents = 0;
1296 display_progress(ctx->progress, ++ctx->progress_cnt);
1298 for (parent = (*list)->parents; num_parents < 3 && parent;
1299 parent = parent->next)
1300 num_parents++;
1302 if (num_parents <= 2) {
1303 list++;
1304 continue;
1307 /* Since num_parents > 2, this initializer is safe. */
1308 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1309 int edge_value = oid_pos(&parent->item->object.oid,
1310 ctx->commits.list,
1311 ctx->commits.nr,
1312 commit_to_oid);
1314 if (edge_value >= 0)
1315 edge_value += ctx->new_num_commits_in_base;
1316 else if (ctx->new_base_graph) {
1317 uint32_t pos;
1318 if (find_commit_pos_in_graph(parent->item,
1319 ctx->new_base_graph,
1320 &pos))
1321 edge_value = pos;
1324 if (edge_value < 0)
1325 BUG("missing parent %s for commit %s",
1326 oid_to_hex(&parent->item->object.oid),
1327 oid_to_hex(&(*list)->object.oid));
1328 else if (!parent->next)
1329 edge_value |= GRAPH_LAST_EDGE;
1331 hashwrite_be32(f, edge_value);
1334 list++;
1337 return 0;
1340 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1341 void *data)
1343 struct write_commit_graph_context *ctx = data;
1344 struct commit **list = ctx->commits.list;
1345 struct commit **last = ctx->commits.list + ctx->commits.nr;
1346 uint32_t cur_pos = 0;
1348 while (list < last) {
1349 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1350 size_t len = filter ? filter->len : 0;
1351 cur_pos += len;
1352 display_progress(ctx->progress, ++ctx->progress_cnt);
1353 hashwrite_be32(f, cur_pos);
1354 list++;
1357 return 0;
1360 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1362 struct json_writer jw = JSON_WRITER_INIT;
1364 jw_object_begin(&jw, 0);
1365 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1366 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1367 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1368 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1369 jw_end(&jw);
1371 trace2_data_json("bloom", ctx->r, "settings", &jw);
1373 jw_release(&jw);
1376 static int write_graph_chunk_bloom_data(struct hashfile *f,
1377 void *data)
1379 struct write_commit_graph_context *ctx = data;
1380 struct commit **list = ctx->commits.list;
1381 struct commit **last = ctx->commits.list + ctx->commits.nr;
1383 trace2_bloom_filter_settings(ctx);
1385 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1386 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1387 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1389 while (list < last) {
1390 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1391 size_t len = filter ? filter->len : 0;
1393 display_progress(ctx->progress, ++ctx->progress_cnt);
1394 if (len)
1395 hashwrite(f, filter->data, len * sizeof(unsigned char));
1396 list++;
1399 return 0;
1402 static int add_packed_commits(const struct object_id *oid,
1403 struct packed_git *pack,
1404 uint32_t pos,
1405 void *data)
1407 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1408 enum object_type type;
1409 off_t offset = nth_packed_object_offset(pack, pos);
1410 struct object_info oi = OBJECT_INFO_INIT;
1412 if (ctx->progress)
1413 display_progress(ctx->progress, ++ctx->progress_done);
1415 oi.typep = &type;
1416 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1417 die(_("unable to get type of object %s"), oid_to_hex(oid));
1419 if (type != OBJ_COMMIT)
1420 return 0;
1422 oid_array_append(&ctx->oids, oid);
1423 set_commit_pos(ctx->r, oid);
1425 return 0;
1428 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1430 struct commit_list *parent;
1431 for (parent = commit->parents; parent; parent = parent->next) {
1432 if (!(parent->item->object.flags & REACHABLE)) {
1433 oid_array_append(&ctx->oids, &parent->item->object.oid);
1434 parent->item->object.flags |= REACHABLE;
1439 static void close_reachable(struct write_commit_graph_context *ctx)
1441 int i;
1442 struct commit *commit;
1443 enum commit_graph_split_flags flags = ctx->opts ?
1444 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1446 if (ctx->report_progress)
1447 ctx->progress = start_delayed_progress(
1448 _("Loading known commits in commit graph"),
1449 ctx->oids.nr);
1450 for (i = 0; i < ctx->oids.nr; i++) {
1451 display_progress(ctx->progress, i + 1);
1452 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1453 if (commit)
1454 commit->object.flags |= REACHABLE;
1456 stop_progress(&ctx->progress);
1459 * As this loop runs, ctx->oids.nr may grow, but not more
1460 * than the number of missing commits in the reachable
1461 * closure.
1463 if (ctx->report_progress)
1464 ctx->progress = start_delayed_progress(
1465 _("Expanding reachable commits in commit graph"),
1467 for (i = 0; i < ctx->oids.nr; i++) {
1468 display_progress(ctx->progress, i + 1);
1469 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1471 if (!commit)
1472 continue;
1473 if (ctx->split) {
1474 if ((!repo_parse_commit(ctx->r, commit) &&
1475 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1476 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1477 add_missing_parents(ctx, commit);
1478 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1479 add_missing_parents(ctx, commit);
1481 stop_progress(&ctx->progress);
1483 if (ctx->report_progress)
1484 ctx->progress = start_delayed_progress(
1485 _("Clearing commit marks in commit graph"),
1486 ctx->oids.nr);
1487 for (i = 0; i < ctx->oids.nr; i++) {
1488 display_progress(ctx->progress, i + 1);
1489 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1491 if (commit)
1492 commit->object.flags &= ~REACHABLE;
1494 stop_progress(&ctx->progress);
1497 struct compute_generation_info {
1498 struct repository *r;
1499 struct packed_commit_list *commits;
1500 struct progress *progress;
1501 int progress_cnt;
1503 timestamp_t (*get_generation)(struct commit *c, void *data);
1504 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1505 void *data;
1508 static timestamp_t compute_generation_from_max(struct commit *c,
1509 timestamp_t max_gen,
1510 int generation_version)
1512 switch (generation_version) {
1513 case 1: /* topological levels */
1514 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1515 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1516 return max_gen + 1;
1518 case 2: /* corrected commit date */
1519 if (c->date && c->date > max_gen)
1520 max_gen = c->date - 1;
1521 return max_gen + 1;
1523 default:
1524 BUG("attempting unimplemented version");
1528 static void compute_reachable_generation_numbers(
1529 struct compute_generation_info *info,
1530 int generation_version)
1532 int i;
1533 struct commit_list *list = NULL;
1535 for (i = 0; i < info->commits->nr; i++) {
1536 struct commit *c = info->commits->list[i];
1537 timestamp_t gen;
1538 repo_parse_commit(info->r, c);
1539 gen = info->get_generation(c, info->data);
1540 display_progress(info->progress, info->progress_cnt + 1);
1542 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1543 continue;
1545 commit_list_insert(c, &list);
1546 while (list) {
1547 struct commit *current = list->item;
1548 struct commit_list *parent;
1549 int all_parents_computed = 1;
1550 uint32_t max_gen = 0;
1552 for (parent = current->parents; parent; parent = parent->next) {
1553 repo_parse_commit(info->r, parent->item);
1554 gen = info->get_generation(parent->item, info->data);
1556 if (gen == GENERATION_NUMBER_ZERO) {
1557 all_parents_computed = 0;
1558 commit_list_insert(parent->item, &list);
1559 break;
1562 if (gen > max_gen)
1563 max_gen = gen;
1566 if (all_parents_computed) {
1567 pop_commit(&list);
1568 gen = compute_generation_from_max(
1569 current, max_gen,
1570 generation_version);
1571 info->set_generation(current, gen, info->data);
1577 static timestamp_t get_topo_level(struct commit *c, void *data)
1579 struct write_commit_graph_context *ctx = data;
1580 return *topo_level_slab_at(ctx->topo_levels, c);
1583 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1585 struct write_commit_graph_context *ctx = data;
1586 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1589 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1591 struct compute_generation_info info = {
1592 .r = ctx->r,
1593 .commits = &ctx->commits,
1594 .get_generation = get_topo_level,
1595 .set_generation = set_topo_level,
1596 .data = ctx,
1599 if (ctx->report_progress)
1600 info.progress = ctx->progress
1601 = start_delayed_progress(
1602 _("Computing commit graph topological levels"),
1603 ctx->commits.nr);
1605 compute_reachable_generation_numbers(&info, 1);
1607 stop_progress(&ctx->progress);
1610 static timestamp_t get_generation_from_graph_data(struct commit *c,
1611 void *data UNUSED)
1613 return commit_graph_data_at(c)->generation;
1616 static void set_generation_v2(struct commit *c, timestamp_t t,
1617 void *data UNUSED)
1619 struct commit_graph_data *g = commit_graph_data_at(c);
1620 g->generation = t;
1623 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1625 int i;
1626 struct compute_generation_info info = {
1627 .r = ctx->r,
1628 .commits = &ctx->commits,
1629 .get_generation = get_generation_from_graph_data,
1630 .set_generation = set_generation_v2,
1633 if (ctx->report_progress)
1634 info.progress = ctx->progress
1635 = start_delayed_progress(
1636 _("Computing commit graph generation numbers"),
1637 ctx->commits.nr);
1639 if (!ctx->trust_generation_numbers) {
1640 for (i = 0; i < ctx->commits.nr; i++) {
1641 struct commit *c = ctx->commits.list[i];
1642 repo_parse_commit(ctx->r, c);
1643 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1647 compute_reachable_generation_numbers(&info, 2);
1649 for (i = 0; i < ctx->commits.nr; i++) {
1650 struct commit *c = ctx->commits.list[i];
1651 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1652 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1653 ctx->num_generation_data_overflows++;
1655 stop_progress(&ctx->progress);
1658 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1659 void *data UNUSED)
1661 commit_graph_data_at(c)->generation = t;
1665 * After this method, all commits reachable from those in the given
1666 * list will have non-zero, non-infinite generation numbers.
1668 void ensure_generations_valid(struct repository *r,
1669 struct commit **commits, size_t nr)
1671 int generation_version = get_configured_generation_version(r);
1672 struct packed_commit_list list = {
1673 .list = commits,
1674 .alloc = nr,
1675 .nr = nr,
1677 struct compute_generation_info info = {
1678 .r = r,
1679 .commits = &list,
1680 .get_generation = get_generation_from_graph_data,
1681 .set_generation = set_generation_in_graph_data,
1684 compute_reachable_generation_numbers(&info, generation_version);
1687 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1689 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1690 ctx->count_bloom_filter_computed);
1691 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1692 ctx->count_bloom_filter_not_computed);
1693 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1694 ctx->count_bloom_filter_trunc_empty);
1695 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1696 ctx->count_bloom_filter_trunc_large);
1699 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1701 int i;
1702 struct progress *progress = NULL;
1703 struct commit **sorted_commits;
1704 int max_new_filters;
1706 init_bloom_filters();
1708 if (ctx->report_progress)
1709 progress = start_delayed_progress(
1710 _("Computing commit changed paths Bloom filters"),
1711 ctx->commits.nr);
1713 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1715 if (ctx->order_by_pack)
1716 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1717 else
1718 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1720 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1721 ctx->opts->max_new_filters : ctx->commits.nr;
1723 for (i = 0; i < ctx->commits.nr; i++) {
1724 enum bloom_filter_computed computed = 0;
1725 struct commit *c = sorted_commits[i];
1726 struct bloom_filter *filter = get_or_compute_bloom_filter(
1727 ctx->r,
1729 ctx->count_bloom_filter_computed < max_new_filters,
1730 ctx->bloom_settings,
1731 &computed);
1732 if (computed & BLOOM_COMPUTED) {
1733 ctx->count_bloom_filter_computed++;
1734 if (computed & BLOOM_TRUNC_EMPTY)
1735 ctx->count_bloom_filter_trunc_empty++;
1736 if (computed & BLOOM_TRUNC_LARGE)
1737 ctx->count_bloom_filter_trunc_large++;
1738 } else if (computed & BLOOM_NOT_COMPUTED)
1739 ctx->count_bloom_filter_not_computed++;
1740 ctx->total_bloom_filter_data_size += filter
1741 ? sizeof(unsigned char) * filter->len : 0;
1742 display_progress(progress, i + 1);
1745 if (trace2_is_enabled())
1746 trace2_bloom_filter_write_statistics(ctx);
1748 free(sorted_commits);
1749 stop_progress(&progress);
1752 struct refs_cb_data {
1753 struct oidset *commits;
1754 struct progress *progress;
1757 static int add_ref_to_set(const char *refname UNUSED,
1758 const struct object_id *oid,
1759 int flags UNUSED, void *cb_data)
1761 struct object_id peeled;
1762 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1764 if (!peel_iterated_oid(oid, &peeled))
1765 oid = &peeled;
1766 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1767 oidset_insert(data->commits, oid);
1769 display_progress(data->progress, oidset_size(data->commits));
1771 return 0;
1774 int write_commit_graph_reachable(struct object_directory *odb,
1775 enum commit_graph_write_flags flags,
1776 const struct commit_graph_opts *opts)
1778 struct oidset commits = OIDSET_INIT;
1779 struct refs_cb_data data;
1780 int result;
1782 memset(&data, 0, sizeof(data));
1783 data.commits = &commits;
1784 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1785 data.progress = start_delayed_progress(
1786 _("Collecting referenced commits"), 0);
1788 for_each_ref(add_ref_to_set, &data);
1790 stop_progress(&data.progress);
1792 result = write_commit_graph(odb, NULL, &commits,
1793 flags, opts);
1795 oidset_clear(&commits);
1796 return result;
1799 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1800 const struct string_list *pack_indexes)
1802 uint32_t i;
1803 struct strbuf progress_title = STRBUF_INIT;
1804 struct strbuf packname = STRBUF_INIT;
1805 int dirlen;
1806 int ret = 0;
1808 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1809 dirlen = packname.len;
1810 if (ctx->report_progress) {
1811 strbuf_addf(&progress_title,
1812 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1813 "Finding commits for commit graph in %"PRIuMAX" packs",
1814 pack_indexes->nr),
1815 (uintmax_t)pack_indexes->nr);
1816 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1817 ctx->progress_done = 0;
1819 for (i = 0; i < pack_indexes->nr; i++) {
1820 struct packed_git *p;
1821 strbuf_setlen(&packname, dirlen);
1822 strbuf_addstr(&packname, pack_indexes->items[i].string);
1823 p = add_packed_git(packname.buf, packname.len, 1);
1824 if (!p) {
1825 ret = error(_("error adding pack %s"), packname.buf);
1826 goto cleanup;
1828 if (open_pack_index(p)) {
1829 ret = error(_("error opening index for %s"), packname.buf);
1830 goto cleanup;
1832 for_each_object_in_pack(p, add_packed_commits, ctx,
1833 FOR_EACH_OBJECT_PACK_ORDER);
1834 close_pack(p);
1835 free(p);
1838 cleanup:
1839 stop_progress(&ctx->progress);
1840 strbuf_release(&progress_title);
1841 strbuf_release(&packname);
1843 return ret;
1846 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1847 struct oidset *commits)
1849 struct oidset_iter iter;
1850 struct object_id *oid;
1852 if (!oidset_size(commits))
1853 return 0;
1855 oidset_iter_init(commits, &iter);
1856 while ((oid = oidset_iter_next(&iter))) {
1857 oid_array_append(&ctx->oids, oid);
1860 return 0;
1863 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1865 if (ctx->report_progress)
1866 ctx->progress = start_delayed_progress(
1867 _("Finding commits for commit graph among packed objects"),
1868 ctx->approx_nr_objects);
1869 for_each_packed_object(add_packed_commits, ctx,
1870 FOR_EACH_OBJECT_PACK_ORDER);
1871 if (ctx->progress_done < ctx->approx_nr_objects)
1872 display_progress(ctx->progress, ctx->approx_nr_objects);
1873 stop_progress(&ctx->progress);
1876 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1878 uint32_t i;
1879 enum commit_graph_split_flags flags = ctx->opts ?
1880 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1882 ctx->num_extra_edges = 0;
1883 if (ctx->report_progress)
1884 ctx->progress = start_delayed_progress(
1885 _("Finding extra edges in commit graph"),
1886 ctx->oids.nr);
1887 oid_array_sort(&ctx->oids);
1888 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1889 unsigned int num_parents;
1891 display_progress(ctx->progress, i + 1);
1893 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1894 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1896 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1897 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1898 continue;
1900 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1901 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1902 else
1903 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1905 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1906 if (num_parents > 2)
1907 ctx->num_extra_edges += num_parents - 1;
1909 ctx->commits.nr++;
1911 stop_progress(&ctx->progress);
1914 static int write_graph_chunk_base_1(struct hashfile *f,
1915 struct commit_graph *g)
1917 int num = 0;
1919 if (!g)
1920 return 0;
1922 num = write_graph_chunk_base_1(f, g->base_graph);
1923 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1924 return num + 1;
1927 static int write_graph_chunk_base(struct hashfile *f,
1928 void *data)
1930 struct write_commit_graph_context *ctx = data;
1931 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1933 if (num != ctx->num_commit_graphs_after - 1) {
1934 error(_("failed to write correct number of base graph ids"));
1935 return -1;
1938 return 0;
1941 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1943 uint32_t i;
1944 int fd;
1945 struct hashfile *f;
1946 struct lock_file lk = LOCK_INIT;
1947 const unsigned hashsz = the_hash_algo->rawsz;
1948 struct strbuf progress_title = STRBUF_INIT;
1949 struct chunkfile *cf;
1950 unsigned char file_hash[GIT_MAX_RAWSZ];
1952 if (ctx->split) {
1953 struct strbuf tmp_file = STRBUF_INIT;
1955 strbuf_addf(&tmp_file,
1956 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1957 ctx->odb->path);
1958 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1959 } else {
1960 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1963 if (safe_create_leading_directories(ctx->graph_name)) {
1964 UNLEAK(ctx->graph_name);
1965 error(_("unable to create leading directories of %s"),
1966 ctx->graph_name);
1967 return -1;
1970 if (ctx->split) {
1971 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1973 hold_lock_file_for_update_mode(&lk, lock_name,
1974 LOCK_DIE_ON_ERROR, 0444);
1975 free(lock_name);
1977 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1978 if (fd < 0) {
1979 error(_("unable to create temporary graph layer"));
1980 return -1;
1983 if (adjust_shared_perm(ctx->graph_name)) {
1984 error(_("unable to adjust shared permissions for '%s'"),
1985 ctx->graph_name);
1986 return -1;
1989 f = hashfd(fd, ctx->graph_name);
1990 } else {
1991 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1992 LOCK_DIE_ON_ERROR, 0444);
1993 fd = get_lock_file_fd(&lk);
1994 f = hashfd(fd, get_lock_file_path(&lk));
1997 cf = init_chunkfile(f);
1999 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2000 write_graph_chunk_fanout);
2001 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2002 write_graph_chunk_oids);
2003 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2004 write_graph_chunk_data);
2006 if (ctx->write_generation_data)
2007 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2008 st_mult(sizeof(uint32_t), ctx->commits.nr),
2009 write_graph_chunk_generation_data);
2010 if (ctx->num_generation_data_overflows)
2011 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2012 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2013 write_graph_chunk_generation_data_overflow);
2014 if (ctx->num_extra_edges)
2015 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2016 st_mult(4, ctx->num_extra_edges),
2017 write_graph_chunk_extra_edges);
2018 if (ctx->changed_paths) {
2019 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2020 st_mult(sizeof(uint32_t), ctx->commits.nr),
2021 write_graph_chunk_bloom_indexes);
2022 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2023 st_add(sizeof(uint32_t) * 3,
2024 ctx->total_bloom_filter_data_size),
2025 write_graph_chunk_bloom_data);
2027 if (ctx->num_commit_graphs_after > 1)
2028 add_chunk(cf, GRAPH_CHUNKID_BASE,
2029 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2030 write_graph_chunk_base);
2032 hashwrite_be32(f, GRAPH_SIGNATURE);
2034 hashwrite_u8(f, GRAPH_VERSION);
2035 hashwrite_u8(f, oid_version(the_hash_algo));
2036 hashwrite_u8(f, get_num_chunks(cf));
2037 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2039 if (ctx->report_progress) {
2040 strbuf_addf(&progress_title,
2041 Q_("Writing out commit graph in %d pass",
2042 "Writing out commit graph in %d passes",
2043 get_num_chunks(cf)),
2044 get_num_chunks(cf));
2045 ctx->progress = start_delayed_progress(
2046 progress_title.buf,
2047 st_mult(get_num_chunks(cf), ctx->commits.nr));
2050 write_chunkfile(cf, ctx);
2052 stop_progress(&ctx->progress);
2053 strbuf_release(&progress_title);
2055 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2056 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2057 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2059 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2060 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2061 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2062 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2065 close_commit_graph(ctx->r->objects);
2066 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2067 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2068 free_chunkfile(cf);
2070 if (ctx->split) {
2071 FILE *chainf = fdopen_lock_file(&lk, "w");
2072 char *final_graph_name;
2073 int result;
2075 close(fd);
2077 if (!chainf) {
2078 error(_("unable to open commit-graph chain file"));
2079 return -1;
2082 if (ctx->base_graph_name) {
2083 const char *dest;
2084 int idx = ctx->num_commit_graphs_after - 1;
2085 if (ctx->num_commit_graphs_after > 1)
2086 idx--;
2088 dest = ctx->commit_graph_filenames_after[idx];
2090 if (strcmp(ctx->base_graph_name, dest)) {
2091 result = rename(ctx->base_graph_name, dest);
2093 if (result) {
2094 error(_("failed to rename base commit-graph file"));
2095 return -1;
2098 } else {
2099 char *graph_name = get_commit_graph_filename(ctx->odb);
2100 unlink(graph_name);
2101 free(graph_name);
2104 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2105 final_graph_name = get_split_graph_filename(ctx->odb,
2106 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2107 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2109 result = rename(ctx->graph_name, final_graph_name);
2111 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2112 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2114 if (result) {
2115 error(_("failed to rename temporary commit-graph file"));
2116 return -1;
2120 commit_lock_file(&lk);
2122 return 0;
2125 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2127 struct commit_graph *g;
2128 uint32_t num_commits;
2129 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2130 uint32_t i;
2132 int max_commits = 0;
2133 int size_mult = 2;
2135 if (ctx->opts) {
2136 max_commits = ctx->opts->max_commits;
2138 if (ctx->opts->size_multiple)
2139 size_mult = ctx->opts->size_multiple;
2141 flags = ctx->opts->split_flags;
2144 g = ctx->r->objects->commit_graph;
2145 num_commits = ctx->commits.nr;
2146 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2147 ctx->num_commit_graphs_after = 1;
2148 else
2149 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2151 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2152 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2153 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2154 (max_commits && num_commits > max_commits))) {
2155 if (g->odb != ctx->odb)
2156 break;
2158 if (unsigned_add_overflows(num_commits, g->num_commits))
2159 die(_("cannot merge graphs with %"PRIuMAX", "
2160 "%"PRIuMAX" commits"),
2161 (uintmax_t)num_commits,
2162 (uintmax_t)g->num_commits);
2163 num_commits += g->num_commits;
2164 g = g->base_graph;
2166 ctx->num_commit_graphs_after--;
2170 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2171 ctx->new_base_graph = g;
2172 else if (ctx->num_commit_graphs_after != 1)
2173 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2174 "should be 1 with --split=replace");
2176 if (ctx->num_commit_graphs_after == 2) {
2177 char *old_graph_name = get_commit_graph_filename(g->odb);
2179 if (!strcmp(g->filename, old_graph_name) &&
2180 g->odb != ctx->odb) {
2181 ctx->num_commit_graphs_after = 1;
2182 ctx->new_base_graph = NULL;
2185 free(old_graph_name);
2188 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2189 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2191 for (i = 0; i < ctx->num_commit_graphs_after &&
2192 i < ctx->num_commit_graphs_before; i++)
2193 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2195 i = ctx->num_commit_graphs_before - 1;
2196 g = ctx->r->objects->commit_graph;
2198 while (g) {
2199 if (i < ctx->num_commit_graphs_after)
2200 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2203 * If the topmost remaining layer has generation data chunk, the
2204 * resultant layer also has generation data chunk.
2206 if (i == ctx->num_commit_graphs_after - 2)
2207 ctx->write_generation_data = !!g->chunk_generation_data;
2209 i--;
2210 g = g->base_graph;
2214 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2215 struct commit_graph *g)
2217 uint32_t i;
2218 uint32_t offset = g->num_commits_in_base;
2220 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2221 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2222 oid_to_hex(&g->oid),
2223 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2225 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2227 for (i = 0; i < g->num_commits; i++) {
2228 struct object_id oid;
2229 struct commit *result;
2231 display_progress(ctx->progress, i + 1);
2233 load_oid_from_graph(g, i + offset, &oid);
2235 /* only add commits if they still exist in the repo */
2236 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2238 if (result) {
2239 ctx->commits.list[ctx->commits.nr] = result;
2240 ctx->commits.nr++;
2245 static int commit_compare(const void *_a, const void *_b)
2247 const struct commit *a = *(const struct commit **)_a;
2248 const struct commit *b = *(const struct commit **)_b;
2249 return oidcmp(&a->object.oid, &b->object.oid);
2252 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2254 uint32_t i, dedup_i = 0;
2256 if (ctx->report_progress)
2257 ctx->progress = start_delayed_progress(
2258 _("Scanning merged commits"),
2259 ctx->commits.nr);
2261 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2263 ctx->num_extra_edges = 0;
2264 for (i = 0; i < ctx->commits.nr; i++) {
2265 display_progress(ctx->progress, i + 1);
2267 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2268 &ctx->commits.list[i]->object.oid)) {
2270 * Silently ignore duplicates. These were likely
2271 * created due to a commit appearing in multiple
2272 * layers of the chain, which is unexpected but
2273 * not invalid. We should make sure there is a
2274 * unique copy in the new layer.
2276 } else {
2277 unsigned int num_parents;
2279 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2280 dedup_i++;
2282 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2283 if (num_parents > 2)
2284 ctx->num_extra_edges += num_parents - 1;
2288 ctx->commits.nr = dedup_i;
2290 stop_progress(&ctx->progress);
2293 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2295 struct commit_graph *g = ctx->r->objects->commit_graph;
2296 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2298 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2299 current_graph_number--;
2301 if (ctx->report_progress)
2302 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2304 merge_commit_graph(ctx, g);
2305 stop_progress(&ctx->progress);
2307 g = g->base_graph;
2310 if (g) {
2311 ctx->new_base_graph = g;
2312 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2315 if (ctx->new_base_graph)
2316 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2318 sort_and_scan_merged_commits(ctx);
2321 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2323 uint32_t i;
2324 time_t now = time(NULL);
2326 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2327 struct stat st;
2328 struct utimbuf updated_time;
2330 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2331 continue;
2333 updated_time.actime = st.st_atime;
2334 updated_time.modtime = now;
2335 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2339 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2341 struct strbuf path = STRBUF_INIT;
2342 DIR *dir;
2343 struct dirent *de;
2344 size_t dirnamelen;
2345 timestamp_t expire_time = time(NULL);
2347 if (ctx->opts && ctx->opts->expire_time)
2348 expire_time = ctx->opts->expire_time;
2349 if (!ctx->split) {
2350 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2351 unlink(chain_file_name);
2352 free(chain_file_name);
2353 ctx->num_commit_graphs_after = 0;
2356 strbuf_addstr(&path, ctx->odb->path);
2357 strbuf_addstr(&path, "/info/commit-graphs");
2358 dir = opendir(path.buf);
2360 if (!dir)
2361 goto out;
2363 strbuf_addch(&path, '/');
2364 dirnamelen = path.len;
2365 while ((de = readdir(dir)) != NULL) {
2366 struct stat st;
2367 uint32_t i, found = 0;
2369 strbuf_setlen(&path, dirnamelen);
2370 strbuf_addstr(&path, de->d_name);
2372 if (stat(path.buf, &st) < 0)
2373 continue;
2375 if (st.st_mtime > expire_time)
2376 continue;
2377 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2378 continue;
2380 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2381 if (!strcmp(ctx->commit_graph_filenames_after[i],
2382 path.buf)) {
2383 found = 1;
2384 break;
2388 if (!found)
2389 unlink(path.buf);
2392 out:
2393 if(dir)
2394 closedir(dir);
2395 strbuf_release(&path);
2398 int write_commit_graph(struct object_directory *odb,
2399 const struct string_list *const pack_indexes,
2400 struct oidset *commits,
2401 enum commit_graph_write_flags flags,
2402 const struct commit_graph_opts *opts)
2404 struct repository *r = the_repository;
2405 struct write_commit_graph_context *ctx;
2406 uint32_t i;
2407 int res = 0;
2408 int replace = 0;
2409 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2410 struct topo_level_slab topo_levels;
2412 prepare_repo_settings(r);
2413 if (!r->settings.core_commit_graph) {
2414 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2415 return 0;
2417 if (!commit_graph_compatible(r))
2418 return 0;
2420 CALLOC_ARRAY(ctx, 1);
2421 ctx->r = r;
2422 ctx->odb = odb;
2423 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2424 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2425 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2426 ctx->opts = opts;
2427 ctx->total_bloom_filter_data_size = 0;
2428 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2429 ctx->num_generation_data_overflows = 0;
2431 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2432 bloom_settings.bits_per_entry);
2433 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2434 bloom_settings.num_hashes);
2435 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2436 bloom_settings.max_changed_paths);
2437 ctx->bloom_settings = &bloom_settings;
2439 init_topo_level_slab(&topo_levels);
2440 ctx->topo_levels = &topo_levels;
2442 prepare_commit_graph(ctx->r);
2443 if (ctx->r->objects->commit_graph) {
2444 struct commit_graph *g = ctx->r->objects->commit_graph;
2446 while (g) {
2447 g->topo_levels = &topo_levels;
2448 g = g->base_graph;
2452 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2453 ctx->changed_paths = 1;
2454 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2455 struct commit_graph *g;
2457 g = ctx->r->objects->commit_graph;
2459 /* We have changed-paths already. Keep them in the next graph */
2460 if (g && g->chunk_bloom_data) {
2461 ctx->changed_paths = 1;
2462 ctx->bloom_settings = g->bloom_filter_settings;
2466 if (ctx->split) {
2467 struct commit_graph *g = ctx->r->objects->commit_graph;
2469 while (g) {
2470 ctx->num_commit_graphs_before++;
2471 g = g->base_graph;
2474 if (ctx->num_commit_graphs_before) {
2475 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2476 i = ctx->num_commit_graphs_before;
2477 g = ctx->r->objects->commit_graph;
2479 while (g) {
2480 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2481 g = g->base_graph;
2485 if (ctx->opts)
2486 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2489 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2491 if (ctx->append && ctx->r->objects->commit_graph) {
2492 struct commit_graph *g = ctx->r->objects->commit_graph;
2493 for (i = 0; i < g->num_commits; i++) {
2494 struct object_id oid;
2495 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2496 oid_array_append(&ctx->oids, &oid);
2500 if (pack_indexes) {
2501 ctx->order_by_pack = 1;
2502 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2503 goto cleanup;
2506 if (commits) {
2507 if ((res = fill_oids_from_commits(ctx, commits)))
2508 goto cleanup;
2511 if (!pack_indexes && !commits) {
2512 ctx->order_by_pack = 1;
2513 fill_oids_from_all_packs(ctx);
2516 close_reachable(ctx);
2518 copy_oids_to_commits(ctx);
2520 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2521 error(_("too many commits to write graph"));
2522 res = -1;
2523 goto cleanup;
2526 if (!ctx->commits.nr && !replace)
2527 goto cleanup;
2529 if (ctx->split) {
2530 split_graph_merge_strategy(ctx);
2532 if (!replace)
2533 merge_commit_graphs(ctx);
2534 } else
2535 ctx->num_commit_graphs_after = 1;
2537 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2539 compute_topological_levels(ctx);
2540 if (ctx->write_generation_data)
2541 compute_generation_numbers(ctx);
2543 if (ctx->changed_paths)
2544 compute_bloom_filters(ctx);
2546 res = write_commit_graph_file(ctx);
2548 if (ctx->split)
2549 mark_commit_graphs(ctx);
2551 expire_commit_graphs(ctx);
2553 cleanup:
2554 free(ctx->graph_name);
2555 free(ctx->commits.list);
2556 oid_array_clear(&ctx->oids);
2557 clear_topo_level_slab(&topo_levels);
2559 if (ctx->commit_graph_filenames_after) {
2560 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2561 free(ctx->commit_graph_filenames_after[i]);
2562 free(ctx->commit_graph_hash_after[i]);
2565 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2566 free(ctx->commit_graph_filenames_before[i]);
2568 free(ctx->commit_graph_filenames_after);
2569 free(ctx->commit_graph_filenames_before);
2570 free(ctx->commit_graph_hash_after);
2573 free(ctx);
2575 return res;
2578 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2579 static int verify_commit_graph_error;
2581 __attribute__((format (printf, 1, 2)))
2582 static void graph_report(const char *fmt, ...)
2584 va_list ap;
2586 verify_commit_graph_error = 1;
2587 va_start(ap, fmt);
2588 vfprintf(stderr, fmt, ap);
2589 fprintf(stderr, "\n");
2590 va_end(ap);
2593 static int commit_graph_checksum_valid(struct commit_graph *g)
2595 return hashfile_checksum_valid(g->data, g->data_len);
2598 static int verify_one_commit_graph(struct repository *r,
2599 struct commit_graph *g,
2600 struct progress *progress,
2601 uint64_t *seen)
2603 uint32_t i, cur_fanout_pos = 0;
2604 struct object_id prev_oid, cur_oid;
2605 struct commit *seen_gen_zero = NULL;
2606 struct commit *seen_gen_non_zero = NULL;
2608 verify_commit_graph_error = verify_commit_graph_lite(g);
2609 if (verify_commit_graph_error)
2610 return verify_commit_graph_error;
2612 if (!commit_graph_checksum_valid(g)) {
2613 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2614 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2617 for (i = 0; i < g->num_commits; i++) {
2618 struct commit *graph_commit;
2620 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2622 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2623 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2624 oid_to_hex(&prev_oid),
2625 oid_to_hex(&cur_oid));
2627 oidcpy(&prev_oid, &cur_oid);
2629 while (cur_oid.hash[0] > cur_fanout_pos) {
2630 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2632 if (i != fanout_value)
2633 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2634 cur_fanout_pos, fanout_value, i);
2635 cur_fanout_pos++;
2638 graph_commit = lookup_commit(r, &cur_oid);
2639 if (!parse_commit_in_graph_one(r, g, graph_commit))
2640 graph_report(_("failed to parse commit %s from commit-graph"),
2641 oid_to_hex(&cur_oid));
2644 while (cur_fanout_pos < 256) {
2645 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2647 if (g->num_commits != fanout_value)
2648 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2649 cur_fanout_pos, fanout_value, i);
2651 cur_fanout_pos++;
2654 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2655 return verify_commit_graph_error;
2657 for (i = 0; i < g->num_commits; i++) {
2658 struct commit *graph_commit, *odb_commit;
2659 struct commit_list *graph_parents, *odb_parents;
2660 timestamp_t max_generation = 0;
2661 timestamp_t generation;
2663 display_progress(progress, ++(*seen));
2664 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2666 graph_commit = lookup_commit(r, &cur_oid);
2667 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2668 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2669 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2670 oid_to_hex(&cur_oid));
2671 continue;
2674 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2675 get_commit_tree_oid(odb_commit)))
2676 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2677 oid_to_hex(&cur_oid),
2678 oid_to_hex(get_commit_tree_oid(graph_commit)),
2679 oid_to_hex(get_commit_tree_oid(odb_commit)));
2681 graph_parents = graph_commit->parents;
2682 odb_parents = odb_commit->parents;
2684 while (graph_parents) {
2685 if (!odb_parents) {
2686 graph_report(_("commit-graph parent list for commit %s is too long"),
2687 oid_to_hex(&cur_oid));
2688 break;
2691 /* parse parent in case it is in a base graph */
2692 parse_commit_in_graph_one(r, g, graph_parents->item);
2694 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2695 graph_report(_("commit-graph parent for %s is %s != %s"),
2696 oid_to_hex(&cur_oid),
2697 oid_to_hex(&graph_parents->item->object.oid),
2698 oid_to_hex(&odb_parents->item->object.oid));
2700 generation = commit_graph_generation_from_graph(graph_parents->item);
2701 if (generation > max_generation)
2702 max_generation = generation;
2704 graph_parents = graph_parents->next;
2705 odb_parents = odb_parents->next;
2708 if (odb_parents)
2709 graph_report(_("commit-graph parent list for commit %s terminates early"),
2710 oid_to_hex(&cur_oid));
2712 if (commit_graph_generation_from_graph(graph_commit))
2713 seen_gen_non_zero = graph_commit;
2714 else
2715 seen_gen_zero = graph_commit;
2717 if (seen_gen_zero)
2718 continue;
2721 * If we are using topological level and one of our parents has
2722 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2723 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2724 * in the following condition.
2726 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2727 max_generation--;
2729 generation = commit_graph_generation(graph_commit);
2730 if (generation < max_generation + 1)
2731 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2732 oid_to_hex(&cur_oid),
2733 generation,
2734 max_generation + 1);
2736 if (graph_commit->date != odb_commit->date)
2737 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2738 oid_to_hex(&cur_oid),
2739 graph_commit->date,
2740 odb_commit->date);
2743 if (seen_gen_zero && seen_gen_non_zero)
2744 graph_report(_("commit-graph has both zero and non-zero "
2745 "generations (e.g., commits '%s' and '%s')"),
2746 oid_to_hex(&seen_gen_zero->object.oid),
2747 oid_to_hex(&seen_gen_non_zero->object.oid));
2749 return verify_commit_graph_error;
2752 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2754 struct progress *progress = NULL;
2755 int local_error = 0;
2756 uint64_t seen = 0;
2758 if (!g) {
2759 graph_report("no commit-graph file loaded");
2760 return 1;
2763 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2764 uint64_t total = g->num_commits;
2765 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2766 total += g->num_commits_in_base;
2768 progress = start_progress(_("Verifying commits in commit graph"),
2769 total);
2772 for (; g; g = g->base_graph) {
2773 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2774 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2775 break;
2778 stop_progress(&progress);
2780 return local_error;
2783 void free_commit_graph(struct commit_graph *g)
2785 if (!g)
2786 return;
2787 if (g->data) {
2788 munmap((void *)g->data, g->data_len);
2789 g->data = NULL;
2791 free(g->filename);
2792 free(g->bloom_filter_settings);
2793 free(g);
2796 void disable_commit_graph(struct repository *r)
2798 r->commit_graph_disabled = 1;