Merge branch 'jk/unused-post-2.40'
[git.git] / commit-graph.c
blob8fce79b39b7286adf8ab54416d978e9e3df69cd5
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "hex.h"
4 #include "lockfile.h"
5 #include "pack.h"
6 #include "packfile.h"
7 #include "commit.h"
8 #include "object.h"
9 #include "refs.h"
10 #include "revision.h"
11 #include "hash-lookup.h"
12 #include "commit-graph.h"
13 #include "object-store.h"
14 #include "alloc.h"
15 #include "hashmap.h"
16 #include "replace-object.h"
17 #include "progress.h"
18 #include "bloom.h"
19 #include "commit-slab.h"
20 #include "shallow.h"
21 #include "json-writer.h"
22 #include "trace2.h"
23 #include "chunk-format.h"
25 void git_test_write_commit_graph_or_die(void)
27 int flags = 0;
28 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
29 return;
31 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
32 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
34 if (write_commit_graph_reachable(the_repository->objects->odb,
35 flags, NULL))
36 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
39 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
40 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
41 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
42 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
43 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
44 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
45 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
46 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
47 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
48 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
50 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
52 #define GRAPH_VERSION_1 0x1
53 #define GRAPH_VERSION GRAPH_VERSION_1
55 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
56 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
57 #define GRAPH_PARENT_NONE 0x70000000
59 #define GRAPH_LAST_EDGE 0x80000000
61 #define GRAPH_HEADER_SIZE 8
62 #define GRAPH_FANOUT_SIZE (4 * 256)
63 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
64 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
66 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
68 /* Remember to update object flag allocation in object.h */
69 #define REACHABLE (1u<<15)
71 define_commit_slab(topo_level_slab, uint32_t);
73 /* Keep track of the order in which commits are added to our list. */
74 define_commit_slab(commit_pos, int);
75 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
77 static void set_commit_pos(struct repository *r, const struct object_id *oid)
79 static int32_t max_pos;
80 struct commit *commit = lookup_commit(r, oid);
82 if (!commit)
83 return; /* should never happen, but be lenient */
85 *commit_pos_at(&commit_pos, commit) = max_pos++;
88 static int commit_pos_cmp(const void *va, const void *vb)
90 const struct commit *a = *(const struct commit **)va;
91 const struct commit *b = *(const struct commit **)vb;
92 return commit_pos_at(&commit_pos, a) -
93 commit_pos_at(&commit_pos, b);
96 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
97 static struct commit_graph_data_slab commit_graph_data_slab =
98 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
100 static int get_configured_generation_version(struct repository *r)
102 int version = 2;
103 repo_config_get_int(r, "commitgraph.generationversion", &version);
104 return version;
107 uint32_t commit_graph_position(const struct commit *c)
109 struct commit_graph_data *data =
110 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
112 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
115 timestamp_t commit_graph_generation(const struct commit *c)
117 struct commit_graph_data *data =
118 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
120 if (data && data->generation)
121 return data->generation;
123 return GENERATION_NUMBER_INFINITY;
126 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
128 unsigned int i, nth_slab;
129 struct commit_graph_data *data =
130 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
132 if (data)
133 return data;
135 nth_slab = c->index / commit_graph_data_slab.slab_size;
136 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
139 * commit-slab initializes elements with zero, overwrite this with
140 * COMMIT_NOT_FROM_GRAPH for graph_pos.
142 * We avoid initializing generation with checking if graph position
143 * is not COMMIT_NOT_FROM_GRAPH.
145 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
146 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
147 COMMIT_NOT_FROM_GRAPH;
150 return data;
154 * Should be used only while writing commit-graph as it compares
155 * generation value of commits by directly accessing commit-slab.
157 static int commit_gen_cmp(const void *va, const void *vb)
159 const struct commit *a = *(const struct commit **)va;
160 const struct commit *b = *(const struct commit **)vb;
162 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
163 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
164 /* lower generation commits first */
165 if (generation_a < generation_b)
166 return -1;
167 else if (generation_a > generation_b)
168 return 1;
170 /* use date as a heuristic when generations are equal */
171 if (a->date < b->date)
172 return -1;
173 else if (a->date > b->date)
174 return 1;
175 return 0;
178 char *get_commit_graph_filename(struct object_directory *obj_dir)
180 return xstrfmt("%s/info/commit-graph", obj_dir->path);
183 static char *get_split_graph_filename(struct object_directory *odb,
184 const char *oid_hex)
186 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
187 oid_hex);
190 char *get_commit_graph_chain_filename(struct object_directory *odb)
192 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
195 static struct commit_graph *alloc_commit_graph(void)
197 struct commit_graph *g = xcalloc(1, sizeof(*g));
199 return g;
202 extern int read_replace_refs;
204 static int commit_graph_compatible(struct repository *r)
206 if (!r->gitdir)
207 return 0;
209 if (read_replace_refs) {
210 prepare_replace_object(r);
211 if (hashmap_get_size(&r->objects->replace_map->map))
212 return 0;
215 prepare_commit_graft(r);
216 if (r->parsed_objects &&
217 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
218 return 0;
219 if (is_repository_shallow(r))
220 return 0;
222 return 1;
225 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
227 *fd = git_open(graph_file);
228 if (*fd < 0)
229 return 0;
230 if (fstat(*fd, st)) {
231 close(*fd);
232 return 0;
234 return 1;
237 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
238 int fd, struct stat *st,
239 struct object_directory *odb)
241 void *graph_map;
242 size_t graph_size;
243 struct commit_graph *ret;
245 graph_size = xsize_t(st->st_size);
247 if (graph_size < GRAPH_MIN_SIZE) {
248 close(fd);
249 error(_("commit-graph file is too small"));
250 return NULL;
252 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
253 close(fd);
254 prepare_repo_settings(r);
255 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
257 if (ret)
258 ret->odb = odb;
259 else
260 munmap(graph_map, graph_size);
262 return ret;
265 static int verify_commit_graph_lite(struct commit_graph *g)
268 * Basic validation shared between parse_commit_graph()
269 * which'll be called every time the graph is used, and the
270 * much more expensive verify_commit_graph() used by
271 * "commit-graph verify".
273 * There should only be very basic checks here to ensure that
274 * we don't e.g. segfault in fill_commit_in_graph(), but
275 * because this is a very hot codepath nothing that e.g. loops
276 * over g->num_commits, or runs a checksum on the commit-graph
277 * itself.
279 if (!g->chunk_oid_fanout) {
280 error("commit-graph is missing the OID Fanout chunk");
281 return 1;
283 if (!g->chunk_oid_lookup) {
284 error("commit-graph is missing the OID Lookup chunk");
285 return 1;
287 if (!g->chunk_commit_data) {
288 error("commit-graph is missing the Commit Data chunk");
289 return 1;
292 return 0;
295 static int graph_read_oid_lookup(const unsigned char *chunk_start,
296 size_t chunk_size, void *data)
298 struct commit_graph *g = data;
299 g->chunk_oid_lookup = chunk_start;
300 g->num_commits = chunk_size / g->hash_len;
301 return 0;
304 static int graph_read_bloom_data(const unsigned char *chunk_start,
305 size_t chunk_size, void *data)
307 struct commit_graph *g = data;
308 uint32_t hash_version;
309 g->chunk_bloom_data = chunk_start;
310 hash_version = get_be32(chunk_start);
312 if (hash_version != 1)
313 return 0;
315 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
316 g->bloom_filter_settings->hash_version = hash_version;
317 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
318 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
319 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
321 return 0;
324 struct commit_graph *parse_commit_graph(struct repo_settings *s,
325 void *graph_map, size_t graph_size)
327 const unsigned char *data;
328 struct commit_graph *graph;
329 uint32_t graph_signature;
330 unsigned char graph_version, hash_version;
331 struct chunkfile *cf = NULL;
333 if (!graph_map)
334 return NULL;
336 if (graph_size < GRAPH_MIN_SIZE)
337 return NULL;
339 data = (const unsigned char *)graph_map;
341 graph_signature = get_be32(data);
342 if (graph_signature != GRAPH_SIGNATURE) {
343 error(_("commit-graph signature %X does not match signature %X"),
344 graph_signature, GRAPH_SIGNATURE);
345 return NULL;
348 graph_version = *(unsigned char*)(data + 4);
349 if (graph_version != GRAPH_VERSION) {
350 error(_("commit-graph version %X does not match version %X"),
351 graph_version, GRAPH_VERSION);
352 return NULL;
355 hash_version = *(unsigned char*)(data + 5);
356 if (hash_version != oid_version(the_hash_algo)) {
357 error(_("commit-graph hash version %X does not match version %X"),
358 hash_version, oid_version(the_hash_algo));
359 return NULL;
362 graph = alloc_commit_graph();
364 graph->hash_len = the_hash_algo->rawsz;
365 graph->num_chunks = *(unsigned char*)(data + 6);
366 graph->data = graph_map;
367 graph->data_len = graph_size;
369 if (graph_size < GRAPH_HEADER_SIZE +
370 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
371 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
372 error(_("commit-graph file is too small to hold %u chunks"),
373 graph->num_chunks);
374 free(graph);
375 return NULL;
378 cf = init_chunkfile(NULL);
380 if (read_table_of_contents(cf, graph->data, graph_size,
381 GRAPH_HEADER_SIZE, graph->num_chunks))
382 goto free_and_return;
384 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
385 (const unsigned char **)&graph->chunk_oid_fanout);
386 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
387 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
388 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
389 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
391 if (s->commit_graph_generation_version >= 2) {
392 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
393 &graph->chunk_generation_data);
394 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
395 &graph->chunk_generation_data_overflow);
397 if (graph->chunk_generation_data)
398 graph->read_generation_data = 1;
401 if (s->commit_graph_read_changed_paths) {
402 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
403 &graph->chunk_bloom_indexes);
404 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
405 graph_read_bloom_data, graph);
408 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
409 init_bloom_filters();
410 } else {
411 /* We need both the bloom chunks to exist together. Else ignore the data */
412 graph->chunk_bloom_indexes = NULL;
413 graph->chunk_bloom_data = NULL;
414 FREE_AND_NULL(graph->bloom_filter_settings);
417 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
419 if (verify_commit_graph_lite(graph))
420 goto free_and_return;
422 free_chunkfile(cf);
423 return graph;
425 free_and_return:
426 free_chunkfile(cf);
427 free(graph->bloom_filter_settings);
428 free(graph);
429 return NULL;
432 static struct commit_graph *load_commit_graph_one(struct repository *r,
433 const char *graph_file,
434 struct object_directory *odb)
437 struct stat st;
438 int fd;
439 struct commit_graph *g;
440 int open_ok = open_commit_graph(graph_file, &fd, &st);
442 if (!open_ok)
443 return NULL;
445 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
447 if (g)
448 g->filename = xstrdup(graph_file);
450 return g;
453 static struct commit_graph *load_commit_graph_v1(struct repository *r,
454 struct object_directory *odb)
456 char *graph_name = get_commit_graph_filename(odb);
457 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
458 free(graph_name);
460 return g;
463 static int add_graph_to_chain(struct commit_graph *g,
464 struct commit_graph *chain,
465 struct object_id *oids,
466 int n)
468 struct commit_graph *cur_g = chain;
470 if (n && !g->chunk_base_graphs) {
471 warning(_("commit-graph has no base graphs chunk"));
472 return 0;
475 while (n) {
476 n--;
478 if (!cur_g ||
479 !oideq(&oids[n], &cur_g->oid) ||
480 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
481 warning(_("commit-graph chain does not match"));
482 return 0;
485 cur_g = cur_g->base_graph;
488 g->base_graph = chain;
490 if (chain)
491 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
493 return 1;
496 static struct commit_graph *load_commit_graph_chain(struct repository *r,
497 struct object_directory *odb)
499 struct commit_graph *graph_chain = NULL;
500 struct strbuf line = STRBUF_INIT;
501 struct stat st;
502 struct object_id *oids;
503 int i = 0, valid = 1, count;
504 char *chain_name = get_commit_graph_chain_filename(odb);
505 FILE *fp;
506 int stat_res;
508 fp = fopen(chain_name, "r");
509 stat_res = stat(chain_name, &st);
510 free(chain_name);
512 if (!fp)
513 return NULL;
514 if (stat_res ||
515 st.st_size <= the_hash_algo->hexsz) {
516 fclose(fp);
517 return NULL;
520 count = st.st_size / (the_hash_algo->hexsz + 1);
521 CALLOC_ARRAY(oids, count);
523 prepare_alt_odb(r);
525 for (i = 0; i < count; i++) {
526 struct object_directory *odb;
528 if (strbuf_getline_lf(&line, fp) == EOF)
529 break;
531 if (get_oid_hex(line.buf, &oids[i])) {
532 warning(_("invalid commit-graph chain: line '%s' not a hash"),
533 line.buf);
534 valid = 0;
535 break;
538 valid = 0;
539 for (odb = r->objects->odb; odb; odb = odb->next) {
540 char *graph_name = get_split_graph_filename(odb, line.buf);
541 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
543 free(graph_name);
545 if (g) {
546 if (add_graph_to_chain(g, graph_chain, oids, i)) {
547 graph_chain = g;
548 valid = 1;
551 break;
555 if (!valid) {
556 warning(_("unable to find all commit-graph files"));
557 break;
561 free(oids);
562 fclose(fp);
563 strbuf_release(&line);
565 return graph_chain;
569 * returns 1 if and only if all graphs in the chain have
570 * corrected commit dates stored in the generation_data chunk.
572 static int validate_mixed_generation_chain(struct commit_graph *g)
574 int read_generation_data = 1;
575 struct commit_graph *p = g;
577 while (read_generation_data && p) {
578 read_generation_data = p->read_generation_data;
579 p = p->base_graph;
582 if (read_generation_data)
583 return 1;
585 while (g) {
586 g->read_generation_data = 0;
587 g = g->base_graph;
590 return 0;
593 struct commit_graph *read_commit_graph_one(struct repository *r,
594 struct object_directory *odb)
596 struct commit_graph *g = load_commit_graph_v1(r, odb);
598 if (!g)
599 g = load_commit_graph_chain(r, odb);
601 validate_mixed_generation_chain(g);
603 return g;
606 static void prepare_commit_graph_one(struct repository *r,
607 struct object_directory *odb)
610 if (r->objects->commit_graph)
611 return;
613 r->objects->commit_graph = read_commit_graph_one(r, odb);
617 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
619 * On the first invocation, this function attempts to load the commit
620 * graph if the_repository is configured to have one.
622 static int prepare_commit_graph(struct repository *r)
624 struct object_directory *odb;
627 * Early return if there is no git dir or if the commit graph is
628 * disabled.
630 * This must come before the "already attempted?" check below, because
631 * we want to disable even an already-loaded graph file.
633 if (!r->gitdir || r->commit_graph_disabled)
634 return 0;
636 if (r->objects->commit_graph_attempted)
637 return !!r->objects->commit_graph;
638 r->objects->commit_graph_attempted = 1;
640 prepare_repo_settings(r);
642 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
643 r->settings.core_commit_graph != 1)
645 * This repository is not configured to use commit graphs, so
646 * do not load one. (But report commit_graph_attempted anyway
647 * so that commit graph loading is not attempted again for this
648 * repository.)
650 return 0;
652 if (!commit_graph_compatible(r))
653 return 0;
655 prepare_alt_odb(r);
656 for (odb = r->objects->odb;
657 !r->objects->commit_graph && odb;
658 odb = odb->next)
659 prepare_commit_graph_one(r, odb);
660 return !!r->objects->commit_graph;
663 int generation_numbers_enabled(struct repository *r)
665 uint32_t first_generation;
666 struct commit_graph *g;
667 if (!prepare_commit_graph(r))
668 return 0;
670 g = r->objects->commit_graph;
672 if (!g->num_commits)
673 return 0;
675 first_generation = get_be32(g->chunk_commit_data +
676 g->hash_len + 8) >> 2;
678 return !!first_generation;
681 int corrected_commit_dates_enabled(struct repository *r)
683 struct commit_graph *g;
684 if (!prepare_commit_graph(r))
685 return 0;
687 g = r->objects->commit_graph;
689 if (!g->num_commits)
690 return 0;
692 return g->read_generation_data;
695 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
697 struct commit_graph *g = r->objects->commit_graph;
698 while (g) {
699 if (g->bloom_filter_settings)
700 return g->bloom_filter_settings;
701 g = g->base_graph;
703 return NULL;
706 static void close_commit_graph_one(struct commit_graph *g)
708 if (!g)
709 return;
711 clear_commit_graph_data_slab(&commit_graph_data_slab);
712 close_commit_graph_one(g->base_graph);
713 free_commit_graph(g);
716 void close_commit_graph(struct raw_object_store *o)
718 close_commit_graph_one(o->commit_graph);
719 o->commit_graph = NULL;
722 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
724 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
725 g->chunk_oid_lookup, g->hash_len, pos);
728 static void load_oid_from_graph(struct commit_graph *g,
729 uint32_t pos,
730 struct object_id *oid)
732 uint32_t lex_index;
734 while (g && pos < g->num_commits_in_base)
735 g = g->base_graph;
737 if (!g)
738 BUG("NULL commit-graph");
740 if (pos >= g->num_commits + g->num_commits_in_base)
741 die(_("invalid commit position. commit-graph is likely corrupt"));
743 lex_index = pos - g->num_commits_in_base;
745 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
748 static struct commit_list **insert_parent_or_die(struct repository *r,
749 struct commit_graph *g,
750 uint32_t pos,
751 struct commit_list **pptr)
753 struct commit *c;
754 struct object_id oid;
756 if (pos >= g->num_commits + g->num_commits_in_base)
757 die("invalid parent position %"PRIu32, pos);
759 load_oid_from_graph(g, pos, &oid);
760 c = lookup_commit(r, &oid);
761 if (!c)
762 die(_("could not find commit %s"), oid_to_hex(&oid));
763 commit_graph_data_at(c)->graph_pos = pos;
764 return &commit_list_insert(c, pptr)->next;
767 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
769 const unsigned char *commit_data;
770 struct commit_graph_data *graph_data;
771 uint32_t lex_index, offset_pos;
772 uint64_t date_high, date_low, offset;
774 while (pos < g->num_commits_in_base)
775 g = g->base_graph;
777 if (pos >= g->num_commits + g->num_commits_in_base)
778 die(_("invalid commit position. commit-graph is likely corrupt"));
780 lex_index = pos - g->num_commits_in_base;
781 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
783 graph_data = commit_graph_data_at(item);
784 graph_data->graph_pos = pos;
786 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
787 date_low = get_be32(commit_data + g->hash_len + 12);
788 item->date = (timestamp_t)((date_high << 32) | date_low);
790 if (g->read_generation_data) {
791 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
793 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
794 if (!g->chunk_generation_data_overflow)
795 die(_("commit-graph requires overflow generation data but has none"));
797 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
798 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
799 } else
800 graph_data->generation = item->date + offset;
801 } else
802 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
804 if (g->topo_levels)
805 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
808 static inline void set_commit_tree(struct commit *c, struct tree *t)
810 c->maybe_tree = t;
813 static int fill_commit_in_graph(struct repository *r,
814 struct commit *item,
815 struct commit_graph *g, uint32_t pos)
817 uint32_t edge_value;
818 uint32_t *parent_data_ptr;
819 struct commit_list **pptr;
820 const unsigned char *commit_data;
821 uint32_t lex_index;
823 while (pos < g->num_commits_in_base)
824 g = g->base_graph;
826 fill_commit_graph_info(item, g, pos);
828 lex_index = pos - g->num_commits_in_base;
829 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
831 item->object.parsed = 1;
833 set_commit_tree(item, NULL);
835 pptr = &item->parents;
837 edge_value = get_be32(commit_data + g->hash_len);
838 if (edge_value == GRAPH_PARENT_NONE)
839 return 1;
840 pptr = insert_parent_or_die(r, g, edge_value, pptr);
842 edge_value = get_be32(commit_data + g->hash_len + 4);
843 if (edge_value == GRAPH_PARENT_NONE)
844 return 1;
845 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
846 pptr = insert_parent_or_die(r, g, edge_value, pptr);
847 return 1;
850 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
851 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
852 do {
853 edge_value = get_be32(parent_data_ptr);
854 pptr = insert_parent_or_die(r, g,
855 edge_value & GRAPH_EDGE_LAST_MASK,
856 pptr);
857 parent_data_ptr++;
858 } while (!(edge_value & GRAPH_LAST_EDGE));
860 return 1;
863 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
865 struct commit_graph *cur_g = g;
866 uint32_t lex_index;
868 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
869 cur_g = cur_g->base_graph;
871 if (cur_g) {
872 *pos = lex_index + cur_g->num_commits_in_base;
873 return 1;
876 return 0;
879 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
881 uint32_t graph_pos = commit_graph_position(item);
882 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
883 *pos = graph_pos;
884 return 1;
885 } else {
886 return search_commit_pos_in_graph(&item->object.oid, g, pos);
890 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
891 uint32_t *pos)
893 if (!prepare_commit_graph(r))
894 return 0;
895 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
898 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
900 struct commit *commit;
901 uint32_t pos;
903 if (!prepare_commit_graph(repo))
904 return NULL;
905 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
906 return NULL;
907 if (!has_object(repo, id, 0))
908 return NULL;
910 commit = lookup_commit(repo, id);
911 if (!commit)
912 return NULL;
913 if (commit->object.parsed)
914 return commit;
916 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
917 return NULL;
919 return commit;
922 static int parse_commit_in_graph_one(struct repository *r,
923 struct commit_graph *g,
924 struct commit *item)
926 uint32_t pos;
928 if (item->object.parsed)
929 return 1;
931 if (find_commit_pos_in_graph(item, g, &pos))
932 return fill_commit_in_graph(r, item, g, pos);
934 return 0;
937 int parse_commit_in_graph(struct repository *r, struct commit *item)
939 static int checked_env = 0;
941 if (!checked_env &&
942 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
943 die("dying as requested by the '%s' variable on commit-graph parse!",
944 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
945 checked_env = 1;
947 if (!prepare_commit_graph(r))
948 return 0;
949 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
952 void load_commit_graph_info(struct repository *r, struct commit *item)
954 uint32_t pos;
955 if (repo_find_commit_pos_in_graph(r, item, &pos))
956 fill_commit_graph_info(item, r->objects->commit_graph, pos);
959 static struct tree *load_tree_for_commit(struct repository *r,
960 struct commit_graph *g,
961 struct commit *c)
963 struct object_id oid;
964 const unsigned char *commit_data;
965 uint32_t graph_pos = commit_graph_position(c);
967 while (graph_pos < g->num_commits_in_base)
968 g = g->base_graph;
970 commit_data = g->chunk_commit_data +
971 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
973 oidread(&oid, commit_data);
974 set_commit_tree(c, lookup_tree(r, &oid));
976 return c->maybe_tree;
979 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
980 struct commit_graph *g,
981 const struct commit *c)
983 if (c->maybe_tree)
984 return c->maybe_tree;
985 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
986 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
988 return load_tree_for_commit(r, g, (struct commit *)c);
991 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
993 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
996 struct packed_commit_list {
997 struct commit **list;
998 size_t nr;
999 size_t alloc;
1002 struct write_commit_graph_context {
1003 struct repository *r;
1004 struct object_directory *odb;
1005 char *graph_name;
1006 struct oid_array oids;
1007 struct packed_commit_list commits;
1008 int num_extra_edges;
1009 int num_generation_data_overflows;
1010 unsigned long approx_nr_objects;
1011 struct progress *progress;
1012 int progress_done;
1013 uint64_t progress_cnt;
1015 char *base_graph_name;
1016 int num_commit_graphs_before;
1017 int num_commit_graphs_after;
1018 char **commit_graph_filenames_before;
1019 char **commit_graph_filenames_after;
1020 char **commit_graph_hash_after;
1021 uint32_t new_num_commits_in_base;
1022 struct commit_graph *new_base_graph;
1024 unsigned append:1,
1025 report_progress:1,
1026 split:1,
1027 changed_paths:1,
1028 order_by_pack:1,
1029 write_generation_data:1,
1030 trust_generation_numbers:1;
1032 struct topo_level_slab *topo_levels;
1033 const struct commit_graph_opts *opts;
1034 size_t total_bloom_filter_data_size;
1035 const struct bloom_filter_settings *bloom_settings;
1037 int count_bloom_filter_computed;
1038 int count_bloom_filter_not_computed;
1039 int count_bloom_filter_trunc_empty;
1040 int count_bloom_filter_trunc_large;
1043 static int write_graph_chunk_fanout(struct hashfile *f,
1044 void *data)
1046 struct write_commit_graph_context *ctx = data;
1047 int i, count = 0;
1048 struct commit **list = ctx->commits.list;
1051 * Write the first-level table (the list is sorted,
1052 * but we use a 256-entry lookup to be able to avoid
1053 * having to do eight extra binary search iterations).
1055 for (i = 0; i < 256; i++) {
1056 while (count < ctx->commits.nr) {
1057 if ((*list)->object.oid.hash[0] != i)
1058 break;
1059 display_progress(ctx->progress, ++ctx->progress_cnt);
1060 count++;
1061 list++;
1064 hashwrite_be32(f, count);
1067 return 0;
1070 static int write_graph_chunk_oids(struct hashfile *f,
1071 void *data)
1073 struct write_commit_graph_context *ctx = data;
1074 struct commit **list = ctx->commits.list;
1075 int count;
1076 for (count = 0; count < ctx->commits.nr; count++, list++) {
1077 display_progress(ctx->progress, ++ctx->progress_cnt);
1078 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1081 return 0;
1084 static const struct object_id *commit_to_oid(size_t index, const void *table)
1086 const struct commit * const *commits = table;
1087 return &commits[index]->object.oid;
1090 static int write_graph_chunk_data(struct hashfile *f,
1091 void *data)
1093 struct write_commit_graph_context *ctx = data;
1094 struct commit **list = ctx->commits.list;
1095 struct commit **last = ctx->commits.list + ctx->commits.nr;
1096 uint32_t num_extra_edges = 0;
1098 while (list < last) {
1099 struct commit_list *parent;
1100 struct object_id *tree;
1101 int edge_value;
1102 uint32_t packedDate[2];
1103 display_progress(ctx->progress, ++ctx->progress_cnt);
1105 if (repo_parse_commit_no_graph(ctx->r, *list))
1106 die(_("unable to parse commit %s"),
1107 oid_to_hex(&(*list)->object.oid));
1108 tree = get_commit_tree_oid(*list);
1109 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1111 parent = (*list)->parents;
1113 if (!parent)
1114 edge_value = GRAPH_PARENT_NONE;
1115 else {
1116 edge_value = oid_pos(&parent->item->object.oid,
1117 ctx->commits.list,
1118 ctx->commits.nr,
1119 commit_to_oid);
1121 if (edge_value >= 0)
1122 edge_value += ctx->new_num_commits_in_base;
1123 else if (ctx->new_base_graph) {
1124 uint32_t pos;
1125 if (find_commit_pos_in_graph(parent->item,
1126 ctx->new_base_graph,
1127 &pos))
1128 edge_value = pos;
1131 if (edge_value < 0)
1132 BUG("missing parent %s for commit %s",
1133 oid_to_hex(&parent->item->object.oid),
1134 oid_to_hex(&(*list)->object.oid));
1137 hashwrite_be32(f, edge_value);
1139 if (parent)
1140 parent = parent->next;
1142 if (!parent)
1143 edge_value = GRAPH_PARENT_NONE;
1144 else if (parent->next)
1145 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1146 else {
1147 edge_value = oid_pos(&parent->item->object.oid,
1148 ctx->commits.list,
1149 ctx->commits.nr,
1150 commit_to_oid);
1152 if (edge_value >= 0)
1153 edge_value += ctx->new_num_commits_in_base;
1154 else if (ctx->new_base_graph) {
1155 uint32_t pos;
1156 if (find_commit_pos_in_graph(parent->item,
1157 ctx->new_base_graph,
1158 &pos))
1159 edge_value = pos;
1162 if (edge_value < 0)
1163 BUG("missing parent %s for commit %s",
1164 oid_to_hex(&parent->item->object.oid),
1165 oid_to_hex(&(*list)->object.oid));
1168 hashwrite_be32(f, edge_value);
1170 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1171 do {
1172 num_extra_edges++;
1173 parent = parent->next;
1174 } while (parent);
1177 if (sizeof((*list)->date) > 4)
1178 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1179 else
1180 packedDate[0] = 0;
1182 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1184 packedDate[1] = htonl((*list)->date);
1185 hashwrite(f, packedDate, 8);
1187 list++;
1190 return 0;
1193 static int write_graph_chunk_generation_data(struct hashfile *f,
1194 void *data)
1196 struct write_commit_graph_context *ctx = data;
1197 int i, num_generation_data_overflows = 0;
1199 for (i = 0; i < ctx->commits.nr; i++) {
1200 struct commit *c = ctx->commits.list[i];
1201 timestamp_t offset;
1202 repo_parse_commit(ctx->r, c);
1203 offset = commit_graph_data_at(c)->generation - c->date;
1204 display_progress(ctx->progress, ++ctx->progress_cnt);
1206 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1207 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1208 num_generation_data_overflows++;
1211 hashwrite_be32(f, offset);
1214 return 0;
1217 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1218 void *data)
1220 struct write_commit_graph_context *ctx = data;
1221 int i;
1222 for (i = 0; i < ctx->commits.nr; i++) {
1223 struct commit *c = ctx->commits.list[i];
1224 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1225 display_progress(ctx->progress, ++ctx->progress_cnt);
1227 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1228 hashwrite_be32(f, offset >> 32);
1229 hashwrite_be32(f, (uint32_t) offset);
1233 return 0;
1236 static int write_graph_chunk_extra_edges(struct hashfile *f,
1237 void *data)
1239 struct write_commit_graph_context *ctx = data;
1240 struct commit **list = ctx->commits.list;
1241 struct commit **last = ctx->commits.list + ctx->commits.nr;
1242 struct commit_list *parent;
1244 while (list < last) {
1245 int num_parents = 0;
1247 display_progress(ctx->progress, ++ctx->progress_cnt);
1249 for (parent = (*list)->parents; num_parents < 3 && parent;
1250 parent = parent->next)
1251 num_parents++;
1253 if (num_parents <= 2) {
1254 list++;
1255 continue;
1258 /* Since num_parents > 2, this initializer is safe. */
1259 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1260 int edge_value = oid_pos(&parent->item->object.oid,
1261 ctx->commits.list,
1262 ctx->commits.nr,
1263 commit_to_oid);
1265 if (edge_value >= 0)
1266 edge_value += ctx->new_num_commits_in_base;
1267 else if (ctx->new_base_graph) {
1268 uint32_t pos;
1269 if (find_commit_pos_in_graph(parent->item,
1270 ctx->new_base_graph,
1271 &pos))
1272 edge_value = pos;
1275 if (edge_value < 0)
1276 BUG("missing parent %s for commit %s",
1277 oid_to_hex(&parent->item->object.oid),
1278 oid_to_hex(&(*list)->object.oid));
1279 else if (!parent->next)
1280 edge_value |= GRAPH_LAST_EDGE;
1282 hashwrite_be32(f, edge_value);
1285 list++;
1288 return 0;
1291 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1292 void *data)
1294 struct write_commit_graph_context *ctx = data;
1295 struct commit **list = ctx->commits.list;
1296 struct commit **last = ctx->commits.list + ctx->commits.nr;
1297 uint32_t cur_pos = 0;
1299 while (list < last) {
1300 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1301 size_t len = filter ? filter->len : 0;
1302 cur_pos += len;
1303 display_progress(ctx->progress, ++ctx->progress_cnt);
1304 hashwrite_be32(f, cur_pos);
1305 list++;
1308 return 0;
1311 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1313 struct json_writer jw = JSON_WRITER_INIT;
1315 jw_object_begin(&jw, 0);
1316 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1317 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1318 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1319 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1320 jw_end(&jw);
1322 trace2_data_json("bloom", ctx->r, "settings", &jw);
1324 jw_release(&jw);
1327 static int write_graph_chunk_bloom_data(struct hashfile *f,
1328 void *data)
1330 struct write_commit_graph_context *ctx = data;
1331 struct commit **list = ctx->commits.list;
1332 struct commit **last = ctx->commits.list + ctx->commits.nr;
1334 trace2_bloom_filter_settings(ctx);
1336 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1337 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1338 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1340 while (list < last) {
1341 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1342 size_t len = filter ? filter->len : 0;
1344 display_progress(ctx->progress, ++ctx->progress_cnt);
1345 if (len)
1346 hashwrite(f, filter->data, len * sizeof(unsigned char));
1347 list++;
1350 return 0;
1353 static int add_packed_commits(const struct object_id *oid,
1354 struct packed_git *pack,
1355 uint32_t pos,
1356 void *data)
1358 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1359 enum object_type type;
1360 off_t offset = nth_packed_object_offset(pack, pos);
1361 struct object_info oi = OBJECT_INFO_INIT;
1363 if (ctx->progress)
1364 display_progress(ctx->progress, ++ctx->progress_done);
1366 oi.typep = &type;
1367 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1368 die(_("unable to get type of object %s"), oid_to_hex(oid));
1370 if (type != OBJ_COMMIT)
1371 return 0;
1373 oid_array_append(&ctx->oids, oid);
1374 set_commit_pos(ctx->r, oid);
1376 return 0;
1379 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1381 struct commit_list *parent;
1382 for (parent = commit->parents; parent; parent = parent->next) {
1383 if (!(parent->item->object.flags & REACHABLE)) {
1384 oid_array_append(&ctx->oids, &parent->item->object.oid);
1385 parent->item->object.flags |= REACHABLE;
1390 static void close_reachable(struct write_commit_graph_context *ctx)
1392 int i;
1393 struct commit *commit;
1394 enum commit_graph_split_flags flags = ctx->opts ?
1395 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1397 if (ctx->report_progress)
1398 ctx->progress = start_delayed_progress(
1399 _("Loading known commits in commit graph"),
1400 ctx->oids.nr);
1401 for (i = 0; i < ctx->oids.nr; i++) {
1402 display_progress(ctx->progress, i + 1);
1403 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1404 if (commit)
1405 commit->object.flags |= REACHABLE;
1407 stop_progress(&ctx->progress);
1410 * As this loop runs, ctx->oids.nr may grow, but not more
1411 * than the number of missing commits in the reachable
1412 * closure.
1414 if (ctx->report_progress)
1415 ctx->progress = start_delayed_progress(
1416 _("Expanding reachable commits in commit graph"),
1418 for (i = 0; i < ctx->oids.nr; i++) {
1419 display_progress(ctx->progress, i + 1);
1420 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1422 if (!commit)
1423 continue;
1424 if (ctx->split) {
1425 if ((!repo_parse_commit(ctx->r, commit) &&
1426 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1427 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1428 add_missing_parents(ctx, commit);
1429 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1430 add_missing_parents(ctx, commit);
1432 stop_progress(&ctx->progress);
1434 if (ctx->report_progress)
1435 ctx->progress = start_delayed_progress(
1436 _("Clearing commit marks in commit graph"),
1437 ctx->oids.nr);
1438 for (i = 0; i < ctx->oids.nr; i++) {
1439 display_progress(ctx->progress, i + 1);
1440 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1442 if (commit)
1443 commit->object.flags &= ~REACHABLE;
1445 stop_progress(&ctx->progress);
1448 struct compute_generation_info {
1449 struct repository *r;
1450 struct packed_commit_list *commits;
1451 struct progress *progress;
1452 int progress_cnt;
1454 timestamp_t (*get_generation)(struct commit *c, void *data);
1455 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1456 void *data;
1459 static timestamp_t compute_generation_from_max(struct commit *c,
1460 timestamp_t max_gen,
1461 int generation_version)
1463 switch (generation_version) {
1464 case 1: /* topological levels */
1465 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1466 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1467 return max_gen + 1;
1469 case 2: /* corrected commit date */
1470 if (c->date && c->date > max_gen)
1471 max_gen = c->date - 1;
1472 return max_gen + 1;
1474 default:
1475 BUG("attempting unimplemented version");
1479 static void compute_reachable_generation_numbers(
1480 struct compute_generation_info *info,
1481 int generation_version)
1483 int i;
1484 struct commit_list *list = NULL;
1486 for (i = 0; i < info->commits->nr; i++) {
1487 struct commit *c = info->commits->list[i];
1488 timestamp_t gen;
1489 repo_parse_commit(info->r, c);
1490 gen = info->get_generation(c, info->data);
1491 display_progress(info->progress, info->progress_cnt + 1);
1493 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1494 continue;
1496 commit_list_insert(c, &list);
1497 while (list) {
1498 struct commit *current = list->item;
1499 struct commit_list *parent;
1500 int all_parents_computed = 1;
1501 uint32_t max_gen = 0;
1503 for (parent = current->parents; parent; parent = parent->next) {
1504 repo_parse_commit(info->r, parent->item);
1505 gen = info->get_generation(parent->item, info->data);
1507 if (gen == GENERATION_NUMBER_ZERO) {
1508 all_parents_computed = 0;
1509 commit_list_insert(parent->item, &list);
1510 break;
1513 if (gen > max_gen)
1514 max_gen = gen;
1517 if (all_parents_computed) {
1518 pop_commit(&list);
1519 gen = compute_generation_from_max(
1520 current, max_gen,
1521 generation_version);
1522 info->set_generation(current, gen, info->data);
1528 static timestamp_t get_topo_level(struct commit *c, void *data)
1530 struct write_commit_graph_context *ctx = data;
1531 return *topo_level_slab_at(ctx->topo_levels, c);
1534 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1536 struct write_commit_graph_context *ctx = data;
1537 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1540 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1542 struct compute_generation_info info = {
1543 .r = ctx->r,
1544 .commits = &ctx->commits,
1545 .get_generation = get_topo_level,
1546 .set_generation = set_topo_level,
1547 .data = ctx,
1550 if (ctx->report_progress)
1551 info.progress = ctx->progress
1552 = start_delayed_progress(
1553 _("Computing commit graph topological levels"),
1554 ctx->commits.nr);
1556 compute_reachable_generation_numbers(&info, 1);
1558 stop_progress(&ctx->progress);
1561 static timestamp_t get_generation_from_graph_data(struct commit *c, void *data)
1563 return commit_graph_data_at(c)->generation;
1566 static void set_generation_v2(struct commit *c, timestamp_t t, void *data)
1568 struct commit_graph_data *g = commit_graph_data_at(c);
1569 g->generation = t;
1572 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1574 int i;
1575 struct compute_generation_info info = {
1576 .r = ctx->r,
1577 .commits = &ctx->commits,
1578 .get_generation = get_generation_from_graph_data,
1579 .set_generation = set_generation_v2,
1580 .data = ctx,
1583 if (ctx->report_progress)
1584 info.progress = ctx->progress
1585 = start_delayed_progress(
1586 _("Computing commit graph generation numbers"),
1587 ctx->commits.nr);
1589 if (!ctx->trust_generation_numbers) {
1590 for (i = 0; i < ctx->commits.nr; i++) {
1591 struct commit *c = ctx->commits.list[i];
1592 repo_parse_commit(ctx->r, c);
1593 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1597 compute_reachable_generation_numbers(&info, 2);
1599 for (i = 0; i < ctx->commits.nr; i++) {
1600 struct commit *c = ctx->commits.list[i];
1601 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1602 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1603 ctx->num_generation_data_overflows++;
1605 stop_progress(&ctx->progress);
1608 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1609 void *data)
1611 commit_graph_data_at(c)->generation = t;
1615 * After this method, all commits reachable from those in the given
1616 * list will have non-zero, non-infinite generation numbers.
1618 void ensure_generations_valid(struct repository *r,
1619 struct commit **commits, size_t nr)
1621 int generation_version = get_configured_generation_version(r);
1622 struct packed_commit_list list = {
1623 .list = commits,
1624 .alloc = nr,
1625 .nr = nr,
1627 struct compute_generation_info info = {
1628 .r = r,
1629 .commits = &list,
1630 .get_generation = get_generation_from_graph_data,
1631 .set_generation = set_generation_in_graph_data,
1634 compute_reachable_generation_numbers(&info, generation_version);
1637 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1639 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1640 ctx->count_bloom_filter_computed);
1641 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1642 ctx->count_bloom_filter_not_computed);
1643 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1644 ctx->count_bloom_filter_trunc_empty);
1645 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1646 ctx->count_bloom_filter_trunc_large);
1649 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1651 int i;
1652 struct progress *progress = NULL;
1653 struct commit **sorted_commits;
1654 int max_new_filters;
1656 init_bloom_filters();
1658 if (ctx->report_progress)
1659 progress = start_delayed_progress(
1660 _("Computing commit changed paths Bloom filters"),
1661 ctx->commits.nr);
1663 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1665 if (ctx->order_by_pack)
1666 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1667 else
1668 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1670 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1671 ctx->opts->max_new_filters : ctx->commits.nr;
1673 for (i = 0; i < ctx->commits.nr; i++) {
1674 enum bloom_filter_computed computed = 0;
1675 struct commit *c = sorted_commits[i];
1676 struct bloom_filter *filter = get_or_compute_bloom_filter(
1677 ctx->r,
1679 ctx->count_bloom_filter_computed < max_new_filters,
1680 ctx->bloom_settings,
1681 &computed);
1682 if (computed & BLOOM_COMPUTED) {
1683 ctx->count_bloom_filter_computed++;
1684 if (computed & BLOOM_TRUNC_EMPTY)
1685 ctx->count_bloom_filter_trunc_empty++;
1686 if (computed & BLOOM_TRUNC_LARGE)
1687 ctx->count_bloom_filter_trunc_large++;
1688 } else if (computed & BLOOM_NOT_COMPUTED)
1689 ctx->count_bloom_filter_not_computed++;
1690 ctx->total_bloom_filter_data_size += filter
1691 ? sizeof(unsigned char) * filter->len : 0;
1692 display_progress(progress, i + 1);
1695 if (trace2_is_enabled())
1696 trace2_bloom_filter_write_statistics(ctx);
1698 free(sorted_commits);
1699 stop_progress(&progress);
1702 struct refs_cb_data {
1703 struct oidset *commits;
1704 struct progress *progress;
1707 static int add_ref_to_set(const char *refname UNUSED,
1708 const struct object_id *oid,
1709 int flags UNUSED, void *cb_data)
1711 struct object_id peeled;
1712 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1714 if (!peel_iterated_oid(oid, &peeled))
1715 oid = &peeled;
1716 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1717 oidset_insert(data->commits, oid);
1719 display_progress(data->progress, oidset_size(data->commits));
1721 return 0;
1724 int write_commit_graph_reachable(struct object_directory *odb,
1725 enum commit_graph_write_flags flags,
1726 const struct commit_graph_opts *opts)
1728 struct oidset commits = OIDSET_INIT;
1729 struct refs_cb_data data;
1730 int result;
1732 memset(&data, 0, sizeof(data));
1733 data.commits = &commits;
1734 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1735 data.progress = start_delayed_progress(
1736 _("Collecting referenced commits"), 0);
1738 for_each_ref(add_ref_to_set, &data);
1740 stop_progress(&data.progress);
1742 result = write_commit_graph(odb, NULL, &commits,
1743 flags, opts);
1745 oidset_clear(&commits);
1746 return result;
1749 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1750 const struct string_list *pack_indexes)
1752 uint32_t i;
1753 struct strbuf progress_title = STRBUF_INIT;
1754 struct strbuf packname = STRBUF_INIT;
1755 int dirlen;
1756 int ret = 0;
1758 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1759 dirlen = packname.len;
1760 if (ctx->report_progress) {
1761 strbuf_addf(&progress_title,
1762 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1763 "Finding commits for commit graph in %"PRIuMAX" packs",
1764 pack_indexes->nr),
1765 (uintmax_t)pack_indexes->nr);
1766 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1767 ctx->progress_done = 0;
1769 for (i = 0; i < pack_indexes->nr; i++) {
1770 struct packed_git *p;
1771 strbuf_setlen(&packname, dirlen);
1772 strbuf_addstr(&packname, pack_indexes->items[i].string);
1773 p = add_packed_git(packname.buf, packname.len, 1);
1774 if (!p) {
1775 ret = error(_("error adding pack %s"), packname.buf);
1776 goto cleanup;
1778 if (open_pack_index(p)) {
1779 ret = error(_("error opening index for %s"), packname.buf);
1780 goto cleanup;
1782 for_each_object_in_pack(p, add_packed_commits, ctx,
1783 FOR_EACH_OBJECT_PACK_ORDER);
1784 close_pack(p);
1785 free(p);
1788 cleanup:
1789 stop_progress(&ctx->progress);
1790 strbuf_release(&progress_title);
1791 strbuf_release(&packname);
1793 return ret;
1796 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1797 struct oidset *commits)
1799 struct oidset_iter iter;
1800 struct object_id *oid;
1802 if (!oidset_size(commits))
1803 return 0;
1805 oidset_iter_init(commits, &iter);
1806 while ((oid = oidset_iter_next(&iter))) {
1807 oid_array_append(&ctx->oids, oid);
1810 return 0;
1813 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1815 if (ctx->report_progress)
1816 ctx->progress = start_delayed_progress(
1817 _("Finding commits for commit graph among packed objects"),
1818 ctx->approx_nr_objects);
1819 for_each_packed_object(add_packed_commits, ctx,
1820 FOR_EACH_OBJECT_PACK_ORDER);
1821 if (ctx->progress_done < ctx->approx_nr_objects)
1822 display_progress(ctx->progress, ctx->approx_nr_objects);
1823 stop_progress(&ctx->progress);
1826 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1828 uint32_t i;
1829 enum commit_graph_split_flags flags = ctx->opts ?
1830 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1832 ctx->num_extra_edges = 0;
1833 if (ctx->report_progress)
1834 ctx->progress = start_delayed_progress(
1835 _("Finding extra edges in commit graph"),
1836 ctx->oids.nr);
1837 oid_array_sort(&ctx->oids);
1838 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1839 unsigned int num_parents;
1841 display_progress(ctx->progress, i + 1);
1843 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1844 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1846 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1847 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1848 continue;
1850 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1851 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1852 else
1853 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1855 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1856 if (num_parents > 2)
1857 ctx->num_extra_edges += num_parents - 1;
1859 ctx->commits.nr++;
1861 stop_progress(&ctx->progress);
1864 static int write_graph_chunk_base_1(struct hashfile *f,
1865 struct commit_graph *g)
1867 int num = 0;
1869 if (!g)
1870 return 0;
1872 num = write_graph_chunk_base_1(f, g->base_graph);
1873 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1874 return num + 1;
1877 static int write_graph_chunk_base(struct hashfile *f,
1878 void *data)
1880 struct write_commit_graph_context *ctx = data;
1881 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1883 if (num != ctx->num_commit_graphs_after - 1) {
1884 error(_("failed to write correct number of base graph ids"));
1885 return -1;
1888 return 0;
1891 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1893 uint32_t i;
1894 int fd;
1895 struct hashfile *f;
1896 struct lock_file lk = LOCK_INIT;
1897 const unsigned hashsz = the_hash_algo->rawsz;
1898 struct strbuf progress_title = STRBUF_INIT;
1899 struct chunkfile *cf;
1900 unsigned char file_hash[GIT_MAX_RAWSZ];
1902 if (ctx->split) {
1903 struct strbuf tmp_file = STRBUF_INIT;
1905 strbuf_addf(&tmp_file,
1906 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1907 ctx->odb->path);
1908 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1909 } else {
1910 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1913 if (safe_create_leading_directories(ctx->graph_name)) {
1914 UNLEAK(ctx->graph_name);
1915 error(_("unable to create leading directories of %s"),
1916 ctx->graph_name);
1917 return -1;
1920 if (ctx->split) {
1921 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1923 hold_lock_file_for_update_mode(&lk, lock_name,
1924 LOCK_DIE_ON_ERROR, 0444);
1925 free(lock_name);
1927 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1928 if (fd < 0) {
1929 error(_("unable to create temporary graph layer"));
1930 return -1;
1933 if (adjust_shared_perm(ctx->graph_name)) {
1934 error(_("unable to adjust shared permissions for '%s'"),
1935 ctx->graph_name);
1936 return -1;
1939 f = hashfd(fd, ctx->graph_name);
1940 } else {
1941 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1942 LOCK_DIE_ON_ERROR, 0444);
1943 fd = get_lock_file_fd(&lk);
1944 f = hashfd(fd, get_lock_file_path(&lk));
1947 cf = init_chunkfile(f);
1949 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1950 write_graph_chunk_fanout);
1951 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1952 write_graph_chunk_oids);
1953 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1954 write_graph_chunk_data);
1956 if (ctx->write_generation_data)
1957 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1958 sizeof(uint32_t) * ctx->commits.nr,
1959 write_graph_chunk_generation_data);
1960 if (ctx->num_generation_data_overflows)
1961 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1962 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1963 write_graph_chunk_generation_data_overflow);
1964 if (ctx->num_extra_edges)
1965 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1966 4 * ctx->num_extra_edges,
1967 write_graph_chunk_extra_edges);
1968 if (ctx->changed_paths) {
1969 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1970 sizeof(uint32_t) * ctx->commits.nr,
1971 write_graph_chunk_bloom_indexes);
1972 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1973 sizeof(uint32_t) * 3
1974 + ctx->total_bloom_filter_data_size,
1975 write_graph_chunk_bloom_data);
1977 if (ctx->num_commit_graphs_after > 1)
1978 add_chunk(cf, GRAPH_CHUNKID_BASE,
1979 hashsz * (ctx->num_commit_graphs_after - 1),
1980 write_graph_chunk_base);
1982 hashwrite_be32(f, GRAPH_SIGNATURE);
1984 hashwrite_u8(f, GRAPH_VERSION);
1985 hashwrite_u8(f, oid_version(the_hash_algo));
1986 hashwrite_u8(f, get_num_chunks(cf));
1987 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1989 if (ctx->report_progress) {
1990 strbuf_addf(&progress_title,
1991 Q_("Writing out commit graph in %d pass",
1992 "Writing out commit graph in %d passes",
1993 get_num_chunks(cf)),
1994 get_num_chunks(cf));
1995 ctx->progress = start_delayed_progress(
1996 progress_title.buf,
1997 get_num_chunks(cf) * ctx->commits.nr);
2000 write_chunkfile(cf, ctx);
2002 stop_progress(&ctx->progress);
2003 strbuf_release(&progress_title);
2005 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2006 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2007 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2009 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2010 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2011 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2012 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2015 close_commit_graph(ctx->r->objects);
2016 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2017 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2018 free_chunkfile(cf);
2020 if (ctx->split) {
2021 FILE *chainf = fdopen_lock_file(&lk, "w");
2022 char *final_graph_name;
2023 int result;
2025 close(fd);
2027 if (!chainf) {
2028 error(_("unable to open commit-graph chain file"));
2029 return -1;
2032 if (ctx->base_graph_name) {
2033 const char *dest;
2034 int idx = ctx->num_commit_graphs_after - 1;
2035 if (ctx->num_commit_graphs_after > 1)
2036 idx--;
2038 dest = ctx->commit_graph_filenames_after[idx];
2040 if (strcmp(ctx->base_graph_name, dest)) {
2041 result = rename(ctx->base_graph_name, dest);
2043 if (result) {
2044 error(_("failed to rename base commit-graph file"));
2045 return -1;
2048 } else {
2049 char *graph_name = get_commit_graph_filename(ctx->odb);
2050 unlink(graph_name);
2051 free(graph_name);
2054 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2055 final_graph_name = get_split_graph_filename(ctx->odb,
2056 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2057 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2059 result = rename(ctx->graph_name, final_graph_name);
2061 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2062 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2064 if (result) {
2065 error(_("failed to rename temporary commit-graph file"));
2066 return -1;
2070 commit_lock_file(&lk);
2072 return 0;
2075 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2077 struct commit_graph *g;
2078 uint32_t num_commits;
2079 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2080 uint32_t i;
2082 int max_commits = 0;
2083 int size_mult = 2;
2085 if (ctx->opts) {
2086 max_commits = ctx->opts->max_commits;
2088 if (ctx->opts->size_multiple)
2089 size_mult = ctx->opts->size_multiple;
2091 flags = ctx->opts->split_flags;
2094 g = ctx->r->objects->commit_graph;
2095 num_commits = ctx->commits.nr;
2096 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2097 ctx->num_commit_graphs_after = 1;
2098 else
2099 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2101 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2102 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2103 while (g && (g->num_commits <= size_mult * num_commits ||
2104 (max_commits && num_commits > max_commits))) {
2105 if (g->odb != ctx->odb)
2106 break;
2108 num_commits += g->num_commits;
2109 g = g->base_graph;
2111 ctx->num_commit_graphs_after--;
2115 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2116 ctx->new_base_graph = g;
2117 else if (ctx->num_commit_graphs_after != 1)
2118 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2119 "should be 1 with --split=replace");
2121 if (ctx->num_commit_graphs_after == 2) {
2122 char *old_graph_name = get_commit_graph_filename(g->odb);
2124 if (!strcmp(g->filename, old_graph_name) &&
2125 g->odb != ctx->odb) {
2126 ctx->num_commit_graphs_after = 1;
2127 ctx->new_base_graph = NULL;
2130 free(old_graph_name);
2133 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2134 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2136 for (i = 0; i < ctx->num_commit_graphs_after &&
2137 i < ctx->num_commit_graphs_before; i++)
2138 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2140 i = ctx->num_commit_graphs_before - 1;
2141 g = ctx->r->objects->commit_graph;
2143 while (g) {
2144 if (i < ctx->num_commit_graphs_after)
2145 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2148 * If the topmost remaining layer has generation data chunk, the
2149 * resultant layer also has generation data chunk.
2151 if (i == ctx->num_commit_graphs_after - 2)
2152 ctx->write_generation_data = !!g->chunk_generation_data;
2154 i--;
2155 g = g->base_graph;
2159 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2160 struct commit_graph *g)
2162 uint32_t i;
2163 uint32_t offset = g->num_commits_in_base;
2165 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2167 for (i = 0; i < g->num_commits; i++) {
2168 struct object_id oid;
2169 struct commit *result;
2171 display_progress(ctx->progress, i + 1);
2173 load_oid_from_graph(g, i + offset, &oid);
2175 /* only add commits if they still exist in the repo */
2176 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2178 if (result) {
2179 ctx->commits.list[ctx->commits.nr] = result;
2180 ctx->commits.nr++;
2185 static int commit_compare(const void *_a, const void *_b)
2187 const struct commit *a = *(const struct commit **)_a;
2188 const struct commit *b = *(const struct commit **)_b;
2189 return oidcmp(&a->object.oid, &b->object.oid);
2192 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2194 uint32_t i, dedup_i = 0;
2196 if (ctx->report_progress)
2197 ctx->progress = start_delayed_progress(
2198 _("Scanning merged commits"),
2199 ctx->commits.nr);
2201 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2203 ctx->num_extra_edges = 0;
2204 for (i = 0; i < ctx->commits.nr; i++) {
2205 display_progress(ctx->progress, i + 1);
2207 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2208 &ctx->commits.list[i]->object.oid)) {
2210 * Silently ignore duplicates. These were likely
2211 * created due to a commit appearing in multiple
2212 * layers of the chain, which is unexpected but
2213 * not invalid. We should make sure there is a
2214 * unique copy in the new layer.
2216 } else {
2217 unsigned int num_parents;
2219 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2220 dedup_i++;
2222 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2223 if (num_parents > 2)
2224 ctx->num_extra_edges += num_parents - 1;
2228 ctx->commits.nr = dedup_i;
2230 stop_progress(&ctx->progress);
2233 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2235 struct commit_graph *g = ctx->r->objects->commit_graph;
2236 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2238 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2239 current_graph_number--;
2241 if (ctx->report_progress)
2242 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2244 merge_commit_graph(ctx, g);
2245 stop_progress(&ctx->progress);
2247 g = g->base_graph;
2250 if (g) {
2251 ctx->new_base_graph = g;
2252 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2255 if (ctx->new_base_graph)
2256 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2258 sort_and_scan_merged_commits(ctx);
2261 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2263 uint32_t i;
2264 time_t now = time(NULL);
2266 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2267 struct stat st;
2268 struct utimbuf updated_time;
2270 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2271 continue;
2273 updated_time.actime = st.st_atime;
2274 updated_time.modtime = now;
2275 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2279 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2281 struct strbuf path = STRBUF_INIT;
2282 DIR *dir;
2283 struct dirent *de;
2284 size_t dirnamelen;
2285 timestamp_t expire_time = time(NULL);
2287 if (ctx->opts && ctx->opts->expire_time)
2288 expire_time = ctx->opts->expire_time;
2289 if (!ctx->split) {
2290 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2291 unlink(chain_file_name);
2292 free(chain_file_name);
2293 ctx->num_commit_graphs_after = 0;
2296 strbuf_addstr(&path, ctx->odb->path);
2297 strbuf_addstr(&path, "/info/commit-graphs");
2298 dir = opendir(path.buf);
2300 if (!dir)
2301 goto out;
2303 strbuf_addch(&path, '/');
2304 dirnamelen = path.len;
2305 while ((de = readdir(dir)) != NULL) {
2306 struct stat st;
2307 uint32_t i, found = 0;
2309 strbuf_setlen(&path, dirnamelen);
2310 strbuf_addstr(&path, de->d_name);
2312 if (stat(path.buf, &st) < 0)
2313 continue;
2315 if (st.st_mtime > expire_time)
2316 continue;
2317 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2318 continue;
2320 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2321 if (!strcmp(ctx->commit_graph_filenames_after[i],
2322 path.buf)) {
2323 found = 1;
2324 break;
2328 if (!found)
2329 unlink(path.buf);
2332 out:
2333 if(dir)
2334 closedir(dir);
2335 strbuf_release(&path);
2338 int write_commit_graph(struct object_directory *odb,
2339 const struct string_list *const pack_indexes,
2340 struct oidset *commits,
2341 enum commit_graph_write_flags flags,
2342 const struct commit_graph_opts *opts)
2344 struct repository *r = the_repository;
2345 struct write_commit_graph_context *ctx;
2346 uint32_t i;
2347 int res = 0;
2348 int replace = 0;
2349 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2350 struct topo_level_slab topo_levels;
2352 prepare_repo_settings(r);
2353 if (!r->settings.core_commit_graph) {
2354 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2355 return 0;
2357 if (!commit_graph_compatible(r))
2358 return 0;
2360 CALLOC_ARRAY(ctx, 1);
2361 ctx->r = r;
2362 ctx->odb = odb;
2363 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2364 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2365 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2366 ctx->opts = opts;
2367 ctx->total_bloom_filter_data_size = 0;
2368 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2369 ctx->num_generation_data_overflows = 0;
2371 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2372 bloom_settings.bits_per_entry);
2373 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2374 bloom_settings.num_hashes);
2375 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2376 bloom_settings.max_changed_paths);
2377 ctx->bloom_settings = &bloom_settings;
2379 init_topo_level_slab(&topo_levels);
2380 ctx->topo_levels = &topo_levels;
2382 prepare_commit_graph(ctx->r);
2383 if (ctx->r->objects->commit_graph) {
2384 struct commit_graph *g = ctx->r->objects->commit_graph;
2386 while (g) {
2387 g->topo_levels = &topo_levels;
2388 g = g->base_graph;
2392 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2393 ctx->changed_paths = 1;
2394 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2395 struct commit_graph *g;
2397 g = ctx->r->objects->commit_graph;
2399 /* We have changed-paths already. Keep them in the next graph */
2400 if (g && g->chunk_bloom_data) {
2401 ctx->changed_paths = 1;
2402 ctx->bloom_settings = g->bloom_filter_settings;
2406 if (ctx->split) {
2407 struct commit_graph *g = ctx->r->objects->commit_graph;
2409 while (g) {
2410 ctx->num_commit_graphs_before++;
2411 g = g->base_graph;
2414 if (ctx->num_commit_graphs_before) {
2415 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2416 i = ctx->num_commit_graphs_before;
2417 g = ctx->r->objects->commit_graph;
2419 while (g) {
2420 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2421 g = g->base_graph;
2425 if (ctx->opts)
2426 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2429 ctx->approx_nr_objects = approximate_object_count();
2431 if (ctx->append && ctx->r->objects->commit_graph) {
2432 struct commit_graph *g = ctx->r->objects->commit_graph;
2433 for (i = 0; i < g->num_commits; i++) {
2434 struct object_id oid;
2435 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2436 oid_array_append(&ctx->oids, &oid);
2440 if (pack_indexes) {
2441 ctx->order_by_pack = 1;
2442 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2443 goto cleanup;
2446 if (commits) {
2447 if ((res = fill_oids_from_commits(ctx, commits)))
2448 goto cleanup;
2451 if (!pack_indexes && !commits) {
2452 ctx->order_by_pack = 1;
2453 fill_oids_from_all_packs(ctx);
2456 close_reachable(ctx);
2458 copy_oids_to_commits(ctx);
2460 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2461 error(_("too many commits to write graph"));
2462 res = -1;
2463 goto cleanup;
2466 if (!ctx->commits.nr && !replace)
2467 goto cleanup;
2469 if (ctx->split) {
2470 split_graph_merge_strategy(ctx);
2472 if (!replace)
2473 merge_commit_graphs(ctx);
2474 } else
2475 ctx->num_commit_graphs_after = 1;
2477 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2479 compute_topological_levels(ctx);
2480 if (ctx->write_generation_data)
2481 compute_generation_numbers(ctx);
2483 if (ctx->changed_paths)
2484 compute_bloom_filters(ctx);
2486 res = write_commit_graph_file(ctx);
2488 if (ctx->split)
2489 mark_commit_graphs(ctx);
2491 expire_commit_graphs(ctx);
2493 cleanup:
2494 free(ctx->graph_name);
2495 free(ctx->commits.list);
2496 oid_array_clear(&ctx->oids);
2497 clear_topo_level_slab(&topo_levels);
2499 if (ctx->commit_graph_filenames_after) {
2500 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2501 free(ctx->commit_graph_filenames_after[i]);
2502 free(ctx->commit_graph_hash_after[i]);
2505 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2506 free(ctx->commit_graph_filenames_before[i]);
2508 free(ctx->commit_graph_filenames_after);
2509 free(ctx->commit_graph_filenames_before);
2510 free(ctx->commit_graph_hash_after);
2513 free(ctx);
2515 return res;
2518 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2519 static int verify_commit_graph_error;
2521 __attribute__((format (printf, 1, 2)))
2522 static void graph_report(const char *fmt, ...)
2524 va_list ap;
2526 verify_commit_graph_error = 1;
2527 va_start(ap, fmt);
2528 vfprintf(stderr, fmt, ap);
2529 fprintf(stderr, "\n");
2530 va_end(ap);
2533 #define GENERATION_ZERO_EXISTS 1
2534 #define GENERATION_NUMBER_EXISTS 2
2536 static int commit_graph_checksum_valid(struct commit_graph *g)
2538 return hashfile_checksum_valid(g->data, g->data_len);
2541 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2543 uint32_t i, cur_fanout_pos = 0;
2544 struct object_id prev_oid, cur_oid;
2545 int generation_zero = 0;
2546 struct progress *progress = NULL;
2547 int local_error = 0;
2549 if (!g) {
2550 graph_report("no commit-graph file loaded");
2551 return 1;
2554 verify_commit_graph_error = verify_commit_graph_lite(g);
2555 if (verify_commit_graph_error)
2556 return verify_commit_graph_error;
2558 if (!commit_graph_checksum_valid(g)) {
2559 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2560 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2563 for (i = 0; i < g->num_commits; i++) {
2564 struct commit *graph_commit;
2566 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2568 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2569 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2570 oid_to_hex(&prev_oid),
2571 oid_to_hex(&cur_oid));
2573 oidcpy(&prev_oid, &cur_oid);
2575 while (cur_oid.hash[0] > cur_fanout_pos) {
2576 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2578 if (i != fanout_value)
2579 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2580 cur_fanout_pos, fanout_value, i);
2581 cur_fanout_pos++;
2584 graph_commit = lookup_commit(r, &cur_oid);
2585 if (!parse_commit_in_graph_one(r, g, graph_commit))
2586 graph_report(_("failed to parse commit %s from commit-graph"),
2587 oid_to_hex(&cur_oid));
2590 while (cur_fanout_pos < 256) {
2591 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2593 if (g->num_commits != fanout_value)
2594 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2595 cur_fanout_pos, fanout_value, i);
2597 cur_fanout_pos++;
2600 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2601 return verify_commit_graph_error;
2603 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2604 progress = start_progress(_("Verifying commits in commit graph"),
2605 g->num_commits);
2607 for (i = 0; i < g->num_commits; i++) {
2608 struct commit *graph_commit, *odb_commit;
2609 struct commit_list *graph_parents, *odb_parents;
2610 timestamp_t max_generation = 0;
2611 timestamp_t generation;
2613 display_progress(progress, i + 1);
2614 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2616 graph_commit = lookup_commit(r, &cur_oid);
2617 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2618 if (parse_commit_internal(odb_commit, 0, 0)) {
2619 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2620 oid_to_hex(&cur_oid));
2621 continue;
2624 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2625 get_commit_tree_oid(odb_commit)))
2626 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2627 oid_to_hex(&cur_oid),
2628 oid_to_hex(get_commit_tree_oid(graph_commit)),
2629 oid_to_hex(get_commit_tree_oid(odb_commit)));
2631 graph_parents = graph_commit->parents;
2632 odb_parents = odb_commit->parents;
2634 while (graph_parents) {
2635 if (!odb_parents) {
2636 graph_report(_("commit-graph parent list for commit %s is too long"),
2637 oid_to_hex(&cur_oid));
2638 break;
2641 /* parse parent in case it is in a base graph */
2642 parse_commit_in_graph_one(r, g, graph_parents->item);
2644 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2645 graph_report(_("commit-graph parent for %s is %s != %s"),
2646 oid_to_hex(&cur_oid),
2647 oid_to_hex(&graph_parents->item->object.oid),
2648 oid_to_hex(&odb_parents->item->object.oid));
2650 generation = commit_graph_generation(graph_parents->item);
2651 if (generation > max_generation)
2652 max_generation = generation;
2654 graph_parents = graph_parents->next;
2655 odb_parents = odb_parents->next;
2658 if (odb_parents)
2659 graph_report(_("commit-graph parent list for commit %s terminates early"),
2660 oid_to_hex(&cur_oid));
2662 if (!commit_graph_generation(graph_commit)) {
2663 if (generation_zero == GENERATION_NUMBER_EXISTS)
2664 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2665 oid_to_hex(&cur_oid));
2666 generation_zero = GENERATION_ZERO_EXISTS;
2667 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2668 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2669 oid_to_hex(&cur_oid));
2671 if (generation_zero == GENERATION_ZERO_EXISTS)
2672 continue;
2675 * If we are using topological level and one of our parents has
2676 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2677 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2678 * in the following condition.
2680 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2681 max_generation--;
2683 generation = commit_graph_generation(graph_commit);
2684 if (generation < max_generation + 1)
2685 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2686 oid_to_hex(&cur_oid),
2687 generation,
2688 max_generation + 1);
2690 if (graph_commit->date != odb_commit->date)
2691 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2692 oid_to_hex(&cur_oid),
2693 graph_commit->date,
2694 odb_commit->date);
2696 stop_progress(&progress);
2698 local_error = verify_commit_graph_error;
2700 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2701 local_error |= verify_commit_graph(r, g->base_graph, flags);
2703 return local_error;
2706 void free_commit_graph(struct commit_graph *g)
2708 if (!g)
2709 return;
2710 if (g->data) {
2711 munmap((void *)g->data, g->data_len);
2712 g->data = NULL;
2714 free(g->filename);
2715 free(g->bloom_filter_settings);
2716 free(g);
2719 void disable_commit_graph(struct repository *r)
2721 r->commit_graph_disabled = 1;