Merge branch 'ks/ref-filter-signature'
[alt-git.git] / commit-graph.c
blobf70afccada4bd3f197869941ff8d8f2a88fc3d90
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"
29 #include "wrapper.h"
31 void git_test_write_commit_graph_or_die(void)
33 int flags = 0;
34 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
35 return;
37 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
38 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
40 if (write_commit_graph_reachable(the_repository->objects->odb,
41 flags, NULL))
42 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
45 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
46 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
47 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
48 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
49 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
50 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
51 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
52 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
53 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
54 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
56 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
58 #define GRAPH_VERSION_1 0x1
59 #define GRAPH_VERSION GRAPH_VERSION_1
61 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
62 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
63 #define GRAPH_PARENT_NONE 0x70000000
65 #define GRAPH_LAST_EDGE 0x80000000
67 #define GRAPH_HEADER_SIZE 8
68 #define GRAPH_FANOUT_SIZE (4 * 256)
69 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
70 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
72 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
74 /* Remember to update object flag allocation in object.h */
75 #define REACHABLE (1u<<15)
77 define_commit_slab(topo_level_slab, uint32_t);
79 /* Keep track of the order in which commits are added to our list. */
80 define_commit_slab(commit_pos, int);
81 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
83 static void set_commit_pos(struct repository *r, const struct object_id *oid)
85 static int32_t max_pos;
86 struct commit *commit = lookup_commit(r, oid);
88 if (!commit)
89 return; /* should never happen, but be lenient */
91 *commit_pos_at(&commit_pos, commit) = max_pos++;
94 static int commit_pos_cmp(const void *va, const void *vb)
96 const struct commit *a = *(const struct commit **)va;
97 const struct commit *b = *(const struct commit **)vb;
98 return commit_pos_at(&commit_pos, a) -
99 commit_pos_at(&commit_pos, b);
102 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
103 static struct commit_graph_data_slab commit_graph_data_slab =
104 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
106 static int get_configured_generation_version(struct repository *r)
108 int version = 2;
109 repo_config_get_int(r, "commitgraph.generationversion", &version);
110 return version;
113 uint32_t commit_graph_position(const struct commit *c)
115 struct commit_graph_data *data =
116 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
118 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
121 timestamp_t commit_graph_generation(const struct commit *c)
123 struct commit_graph_data *data =
124 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
126 if (data && data->generation)
127 return data->generation;
129 return GENERATION_NUMBER_INFINITY;
132 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
134 unsigned int i, nth_slab;
135 struct commit_graph_data *data =
136 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
138 if (data)
139 return data;
141 nth_slab = c->index / commit_graph_data_slab.slab_size;
142 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
145 * commit-slab initializes elements with zero, overwrite this with
146 * COMMIT_NOT_FROM_GRAPH for graph_pos.
148 * We avoid initializing generation with checking if graph position
149 * is not COMMIT_NOT_FROM_GRAPH.
151 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
152 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
153 COMMIT_NOT_FROM_GRAPH;
156 return data;
160 * Should be used only while writing commit-graph as it compares
161 * generation value of commits by directly accessing commit-slab.
163 static int commit_gen_cmp(const void *va, const void *vb)
165 const struct commit *a = *(const struct commit **)va;
166 const struct commit *b = *(const struct commit **)vb;
168 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
169 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
170 /* lower generation commits first */
171 if (generation_a < generation_b)
172 return -1;
173 else if (generation_a > generation_b)
174 return 1;
176 /* use date as a heuristic when generations are equal */
177 if (a->date < b->date)
178 return -1;
179 else if (a->date > b->date)
180 return 1;
181 return 0;
184 char *get_commit_graph_filename(struct object_directory *obj_dir)
186 return xstrfmt("%s/info/commit-graph", obj_dir->path);
189 static char *get_split_graph_filename(struct object_directory *odb,
190 const char *oid_hex)
192 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
193 oid_hex);
196 char *get_commit_graph_chain_filename(struct object_directory *odb)
198 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
201 static struct commit_graph *alloc_commit_graph(void)
203 struct commit_graph *g = xcalloc(1, sizeof(*g));
205 return g;
208 static int commit_graph_compatible(struct repository *r)
210 if (!r->gitdir)
211 return 0;
213 if (replace_refs_enabled(r)) {
214 prepare_replace_object(r);
215 if (hashmap_get_size(&r->objects->replace_map->map))
216 return 0;
219 prepare_commit_graft(r);
220 if (r->parsed_objects &&
221 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
222 return 0;
223 if (is_repository_shallow(r))
224 return 0;
226 return 1;
229 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
231 *fd = git_open(graph_file);
232 if (*fd < 0)
233 return 0;
234 if (fstat(*fd, st)) {
235 close(*fd);
236 return 0;
238 return 1;
241 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
242 int fd, struct stat *st,
243 struct object_directory *odb)
245 void *graph_map;
246 size_t graph_size;
247 struct commit_graph *ret;
249 graph_size = xsize_t(st->st_size);
251 if (graph_size < GRAPH_MIN_SIZE) {
252 close(fd);
253 error(_("commit-graph file is too small"));
254 return NULL;
256 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
257 close(fd);
258 prepare_repo_settings(r);
259 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
261 if (ret)
262 ret->odb = odb;
263 else
264 munmap(graph_map, graph_size);
266 return ret;
269 static int verify_commit_graph_lite(struct commit_graph *g)
272 * Basic validation shared between parse_commit_graph()
273 * which'll be called every time the graph is used, and the
274 * much more expensive verify_commit_graph() used by
275 * "commit-graph verify".
277 * There should only be very basic checks here to ensure that
278 * we don't e.g. segfault in fill_commit_in_graph(), but
279 * because this is a very hot codepath nothing that e.g. loops
280 * over g->num_commits, or runs a checksum on the commit-graph
281 * itself.
283 if (!g->chunk_oid_fanout) {
284 error("commit-graph is missing the OID Fanout chunk");
285 return 1;
287 if (!g->chunk_oid_lookup) {
288 error("commit-graph is missing the OID Lookup chunk");
289 return 1;
291 if (!g->chunk_commit_data) {
292 error("commit-graph is missing the Commit Data chunk");
293 return 1;
296 return 0;
299 static int graph_read_oid_lookup(const unsigned char *chunk_start,
300 size_t chunk_size, void *data)
302 struct commit_graph *g = data;
303 g->chunk_oid_lookup = chunk_start;
304 g->num_commits = chunk_size / g->hash_len;
305 return 0;
308 static int graph_read_bloom_data(const unsigned char *chunk_start,
309 size_t chunk_size, void *data)
311 struct commit_graph *g = data;
312 uint32_t hash_version;
313 g->chunk_bloom_data = chunk_start;
314 hash_version = get_be32(chunk_start);
316 if (hash_version != 1)
317 return 0;
319 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
320 g->bloom_filter_settings->hash_version = hash_version;
321 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
322 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
323 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
325 return 0;
328 struct commit_graph *parse_commit_graph(struct repo_settings *s,
329 void *graph_map, size_t graph_size)
331 const unsigned char *data;
332 struct commit_graph *graph;
333 uint32_t graph_signature;
334 unsigned char graph_version, hash_version;
335 struct chunkfile *cf = NULL;
337 if (!graph_map)
338 return NULL;
340 if (graph_size < GRAPH_MIN_SIZE)
341 return NULL;
343 data = (const unsigned char *)graph_map;
345 graph_signature = get_be32(data);
346 if (graph_signature != GRAPH_SIGNATURE) {
347 error(_("commit-graph signature %X does not match signature %X"),
348 graph_signature, GRAPH_SIGNATURE);
349 return NULL;
352 graph_version = *(unsigned char*)(data + 4);
353 if (graph_version != GRAPH_VERSION) {
354 error(_("commit-graph version %X does not match version %X"),
355 graph_version, GRAPH_VERSION);
356 return NULL;
359 hash_version = *(unsigned char*)(data + 5);
360 if (hash_version != oid_version(the_hash_algo)) {
361 error(_("commit-graph hash version %X does not match version %X"),
362 hash_version, oid_version(the_hash_algo));
363 return NULL;
366 graph = alloc_commit_graph();
368 graph->hash_len = the_hash_algo->rawsz;
369 graph->num_chunks = *(unsigned char*)(data + 6);
370 graph->data = graph_map;
371 graph->data_len = graph_size;
373 if (graph_size < GRAPH_HEADER_SIZE +
374 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
375 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
376 error(_("commit-graph file is too small to hold %u chunks"),
377 graph->num_chunks);
378 free(graph);
379 return NULL;
382 cf = init_chunkfile(NULL);
384 if (read_table_of_contents(cf, graph->data, graph_size,
385 GRAPH_HEADER_SIZE, graph->num_chunks))
386 goto free_and_return;
388 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
389 (const unsigned char **)&graph->chunk_oid_fanout);
390 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
391 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
392 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
393 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
395 if (s->commit_graph_generation_version >= 2) {
396 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
397 &graph->chunk_generation_data);
398 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
399 &graph->chunk_generation_data_overflow);
401 if (graph->chunk_generation_data)
402 graph->read_generation_data = 1;
405 if (s->commit_graph_read_changed_paths) {
406 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
407 &graph->chunk_bloom_indexes);
408 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
409 graph_read_bloom_data, graph);
412 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
413 init_bloom_filters();
414 } else {
415 /* We need both the bloom chunks to exist together. Else ignore the data */
416 graph->chunk_bloom_indexes = NULL;
417 graph->chunk_bloom_data = NULL;
418 FREE_AND_NULL(graph->bloom_filter_settings);
421 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
423 if (verify_commit_graph_lite(graph))
424 goto free_and_return;
426 free_chunkfile(cf);
427 return graph;
429 free_and_return:
430 free_chunkfile(cf);
431 free(graph->bloom_filter_settings);
432 free(graph);
433 return NULL;
436 static struct commit_graph *load_commit_graph_one(struct repository *r,
437 const char *graph_file,
438 struct object_directory *odb)
441 struct stat st;
442 int fd;
443 struct commit_graph *g;
444 int open_ok = open_commit_graph(graph_file, &fd, &st);
446 if (!open_ok)
447 return NULL;
449 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
451 if (g)
452 g->filename = xstrdup(graph_file);
454 return g;
457 static struct commit_graph *load_commit_graph_v1(struct repository *r,
458 struct object_directory *odb)
460 char *graph_name = get_commit_graph_filename(odb);
461 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
462 free(graph_name);
464 return g;
467 static int add_graph_to_chain(struct commit_graph *g,
468 struct commit_graph *chain,
469 struct object_id *oids,
470 int n)
472 struct commit_graph *cur_g = chain;
474 if (n && !g->chunk_base_graphs) {
475 warning(_("commit-graph has no base graphs chunk"));
476 return 0;
479 while (n) {
480 n--;
482 if (!cur_g ||
483 !oideq(&oids[n], &cur_g->oid) ||
484 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
485 warning(_("commit-graph chain does not match"));
486 return 0;
489 cur_g = cur_g->base_graph;
492 g->base_graph = chain;
494 if (chain)
495 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
497 return 1;
500 static struct commit_graph *load_commit_graph_chain(struct repository *r,
501 struct object_directory *odb)
503 struct commit_graph *graph_chain = NULL;
504 struct strbuf line = STRBUF_INIT;
505 struct stat st;
506 struct object_id *oids;
507 int i = 0, valid = 1, count;
508 char *chain_name = get_commit_graph_chain_filename(odb);
509 FILE *fp;
510 int stat_res;
512 fp = fopen(chain_name, "r");
513 stat_res = stat(chain_name, &st);
514 free(chain_name);
516 if (!fp)
517 return NULL;
518 if (stat_res ||
519 st.st_size <= the_hash_algo->hexsz) {
520 fclose(fp);
521 return NULL;
524 count = st.st_size / (the_hash_algo->hexsz + 1);
525 CALLOC_ARRAY(oids, count);
527 prepare_alt_odb(r);
529 for (i = 0; i < count; i++) {
530 struct object_directory *odb;
532 if (strbuf_getline_lf(&line, fp) == EOF)
533 break;
535 if (get_oid_hex(line.buf, &oids[i])) {
536 warning(_("invalid commit-graph chain: line '%s' not a hash"),
537 line.buf);
538 valid = 0;
539 break;
542 valid = 0;
543 for (odb = r->objects->odb; odb; odb = odb->next) {
544 char *graph_name = get_split_graph_filename(odb, line.buf);
545 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
547 free(graph_name);
549 if (g) {
550 if (add_graph_to_chain(g, graph_chain, oids, i)) {
551 graph_chain = g;
552 valid = 1;
555 break;
559 if (!valid) {
560 warning(_("unable to find all commit-graph files"));
561 break;
565 free(oids);
566 fclose(fp);
567 strbuf_release(&line);
569 return graph_chain;
573 * returns 1 if and only if all graphs in the chain have
574 * corrected commit dates stored in the generation_data chunk.
576 static int validate_mixed_generation_chain(struct commit_graph *g)
578 int read_generation_data = 1;
579 struct commit_graph *p = g;
581 while (read_generation_data && p) {
582 read_generation_data = p->read_generation_data;
583 p = p->base_graph;
586 if (read_generation_data)
587 return 1;
589 while (g) {
590 g->read_generation_data = 0;
591 g = g->base_graph;
594 return 0;
597 struct commit_graph *read_commit_graph_one(struct repository *r,
598 struct object_directory *odb)
600 struct commit_graph *g = load_commit_graph_v1(r, odb);
602 if (!g)
603 g = load_commit_graph_chain(r, odb);
605 validate_mixed_generation_chain(g);
607 return g;
610 static void prepare_commit_graph_one(struct repository *r,
611 struct object_directory *odb)
614 if (r->objects->commit_graph)
615 return;
617 r->objects->commit_graph = read_commit_graph_one(r, odb);
621 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
623 * On the first invocation, this function attempts to load the commit
624 * graph if the_repository is configured to have one.
626 static int prepare_commit_graph(struct repository *r)
628 struct object_directory *odb;
631 * Early return if there is no git dir or if the commit graph is
632 * disabled.
634 * This must come before the "already attempted?" check below, because
635 * we want to disable even an already-loaded graph file.
637 if (!r->gitdir || r->commit_graph_disabled)
638 return 0;
640 if (r->objects->commit_graph_attempted)
641 return !!r->objects->commit_graph;
642 r->objects->commit_graph_attempted = 1;
644 prepare_repo_settings(r);
646 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
647 r->settings.core_commit_graph != 1)
649 * This repository is not configured to use commit graphs, so
650 * do not load one. (But report commit_graph_attempted anyway
651 * so that commit graph loading is not attempted again for this
652 * repository.)
654 return 0;
656 if (!commit_graph_compatible(r))
657 return 0;
659 prepare_alt_odb(r);
660 for (odb = r->objects->odb;
661 !r->objects->commit_graph && odb;
662 odb = odb->next)
663 prepare_commit_graph_one(r, odb);
664 return !!r->objects->commit_graph;
667 int generation_numbers_enabled(struct repository *r)
669 uint32_t first_generation;
670 struct commit_graph *g;
671 if (!prepare_commit_graph(r))
672 return 0;
674 g = r->objects->commit_graph;
676 if (!g->num_commits)
677 return 0;
679 first_generation = get_be32(g->chunk_commit_data +
680 g->hash_len + 8) >> 2;
682 return !!first_generation;
685 int corrected_commit_dates_enabled(struct repository *r)
687 struct commit_graph *g;
688 if (!prepare_commit_graph(r))
689 return 0;
691 g = r->objects->commit_graph;
693 if (!g->num_commits)
694 return 0;
696 return g->read_generation_data;
699 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
701 struct commit_graph *g = r->objects->commit_graph;
702 while (g) {
703 if (g->bloom_filter_settings)
704 return g->bloom_filter_settings;
705 g = g->base_graph;
707 return NULL;
710 static void close_commit_graph_one(struct commit_graph *g)
712 if (!g)
713 return;
715 clear_commit_graph_data_slab(&commit_graph_data_slab);
716 close_commit_graph_one(g->base_graph);
717 free_commit_graph(g);
720 void close_commit_graph(struct raw_object_store *o)
722 close_commit_graph_one(o->commit_graph);
723 o->commit_graph = NULL;
726 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
728 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
729 g->chunk_oid_lookup, g->hash_len, pos);
732 static void load_oid_from_graph(struct commit_graph *g,
733 uint32_t pos,
734 struct object_id *oid)
736 uint32_t lex_index;
738 while (g && pos < g->num_commits_in_base)
739 g = g->base_graph;
741 if (!g)
742 BUG("NULL commit-graph");
744 if (pos >= g->num_commits + g->num_commits_in_base)
745 die(_("invalid commit position. commit-graph is likely corrupt"));
747 lex_index = pos - g->num_commits_in_base;
749 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
752 static struct commit_list **insert_parent_or_die(struct repository *r,
753 struct commit_graph *g,
754 uint32_t pos,
755 struct commit_list **pptr)
757 struct commit *c;
758 struct object_id oid;
760 if (pos >= g->num_commits + g->num_commits_in_base)
761 die("invalid parent position %"PRIu32, pos);
763 load_oid_from_graph(g, pos, &oid);
764 c = lookup_commit(r, &oid);
765 if (!c)
766 die(_("could not find commit %s"), oid_to_hex(&oid));
767 commit_graph_data_at(c)->graph_pos = pos;
768 return &commit_list_insert(c, pptr)->next;
771 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
773 const unsigned char *commit_data;
774 struct commit_graph_data *graph_data;
775 uint32_t lex_index, offset_pos;
776 uint64_t date_high, date_low, offset;
778 while (pos < g->num_commits_in_base)
779 g = g->base_graph;
781 if (pos >= g->num_commits + g->num_commits_in_base)
782 die(_("invalid commit position. commit-graph is likely corrupt"));
784 lex_index = pos - g->num_commits_in_base;
785 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
787 graph_data = commit_graph_data_at(item);
788 graph_data->graph_pos = pos;
790 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
791 date_low = get_be32(commit_data + g->hash_len + 12);
792 item->date = (timestamp_t)((date_high << 32) | date_low);
794 if (g->read_generation_data) {
795 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
797 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
798 if (!g->chunk_generation_data_overflow)
799 die(_("commit-graph requires overflow generation data but has none"));
801 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
802 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
803 } else
804 graph_data->generation = item->date + offset;
805 } else
806 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
808 if (g->topo_levels)
809 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
812 static inline void set_commit_tree(struct commit *c, struct tree *t)
814 c->maybe_tree = t;
817 static int fill_commit_in_graph(struct repository *r,
818 struct commit *item,
819 struct commit_graph *g, uint32_t pos)
821 uint32_t edge_value;
822 uint32_t *parent_data_ptr;
823 struct commit_list **pptr;
824 const unsigned char *commit_data;
825 uint32_t lex_index;
827 while (pos < g->num_commits_in_base)
828 g = g->base_graph;
830 fill_commit_graph_info(item, g, pos);
832 lex_index = pos - g->num_commits_in_base;
833 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
835 item->object.parsed = 1;
837 set_commit_tree(item, NULL);
839 pptr = &item->parents;
841 edge_value = get_be32(commit_data + g->hash_len);
842 if (edge_value == GRAPH_PARENT_NONE)
843 return 1;
844 pptr = insert_parent_or_die(r, g, edge_value, pptr);
846 edge_value = get_be32(commit_data + g->hash_len + 4);
847 if (edge_value == GRAPH_PARENT_NONE)
848 return 1;
849 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
850 pptr = insert_parent_or_die(r, g, edge_value, pptr);
851 return 1;
854 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
855 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
856 do {
857 edge_value = get_be32(parent_data_ptr);
858 pptr = insert_parent_or_die(r, g,
859 edge_value & GRAPH_EDGE_LAST_MASK,
860 pptr);
861 parent_data_ptr++;
862 } while (!(edge_value & GRAPH_LAST_EDGE));
864 return 1;
867 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
869 struct commit_graph *cur_g = g;
870 uint32_t lex_index;
872 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
873 cur_g = cur_g->base_graph;
875 if (cur_g) {
876 *pos = lex_index + cur_g->num_commits_in_base;
877 return 1;
880 return 0;
883 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
885 uint32_t graph_pos = commit_graph_position(item);
886 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
887 *pos = graph_pos;
888 return 1;
889 } else {
890 return search_commit_pos_in_graph(&item->object.oid, g, pos);
894 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
895 uint32_t *pos)
897 if (!prepare_commit_graph(r))
898 return 0;
899 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
902 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
904 struct commit *commit;
905 uint32_t pos;
907 if (!prepare_commit_graph(repo))
908 return NULL;
909 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
910 return NULL;
911 if (!has_object(repo, id, 0))
912 return NULL;
914 commit = lookup_commit(repo, id);
915 if (!commit)
916 return NULL;
917 if (commit->object.parsed)
918 return commit;
920 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
921 return NULL;
923 return commit;
926 static int parse_commit_in_graph_one(struct repository *r,
927 struct commit_graph *g,
928 struct commit *item)
930 uint32_t pos;
932 if (item->object.parsed)
933 return 1;
935 if (find_commit_pos_in_graph(item, g, &pos))
936 return fill_commit_in_graph(r, item, g, pos);
938 return 0;
941 int parse_commit_in_graph(struct repository *r, struct commit *item)
943 static int checked_env = 0;
945 if (!checked_env &&
946 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
947 die("dying as requested by the '%s' variable on commit-graph parse!",
948 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
949 checked_env = 1;
951 if (!prepare_commit_graph(r))
952 return 0;
953 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
956 void load_commit_graph_info(struct repository *r, struct commit *item)
958 uint32_t pos;
959 if (repo_find_commit_pos_in_graph(r, item, &pos))
960 fill_commit_graph_info(item, r->objects->commit_graph, pos);
963 static struct tree *load_tree_for_commit(struct repository *r,
964 struct commit_graph *g,
965 struct commit *c)
967 struct object_id oid;
968 const unsigned char *commit_data;
969 uint32_t graph_pos = commit_graph_position(c);
971 while (graph_pos < g->num_commits_in_base)
972 g = g->base_graph;
974 commit_data = g->chunk_commit_data +
975 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
977 oidread(&oid, commit_data);
978 set_commit_tree(c, lookup_tree(r, &oid));
980 return c->maybe_tree;
983 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
984 struct commit_graph *g,
985 const struct commit *c)
987 if (c->maybe_tree)
988 return c->maybe_tree;
989 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
990 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
992 return load_tree_for_commit(r, g, (struct commit *)c);
995 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
997 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1000 struct packed_commit_list {
1001 struct commit **list;
1002 size_t nr;
1003 size_t alloc;
1006 struct write_commit_graph_context {
1007 struct repository *r;
1008 struct object_directory *odb;
1009 char *graph_name;
1010 struct oid_array oids;
1011 struct packed_commit_list commits;
1012 int num_extra_edges;
1013 int num_generation_data_overflows;
1014 unsigned long approx_nr_objects;
1015 struct progress *progress;
1016 int progress_done;
1017 uint64_t progress_cnt;
1019 char *base_graph_name;
1020 int num_commit_graphs_before;
1021 int num_commit_graphs_after;
1022 char **commit_graph_filenames_before;
1023 char **commit_graph_filenames_after;
1024 char **commit_graph_hash_after;
1025 uint32_t new_num_commits_in_base;
1026 struct commit_graph *new_base_graph;
1028 unsigned append:1,
1029 report_progress:1,
1030 split:1,
1031 changed_paths:1,
1032 order_by_pack:1,
1033 write_generation_data:1,
1034 trust_generation_numbers:1;
1036 struct topo_level_slab *topo_levels;
1037 const struct commit_graph_opts *opts;
1038 size_t total_bloom_filter_data_size;
1039 const struct bloom_filter_settings *bloom_settings;
1041 int count_bloom_filter_computed;
1042 int count_bloom_filter_not_computed;
1043 int count_bloom_filter_trunc_empty;
1044 int count_bloom_filter_trunc_large;
1047 static int write_graph_chunk_fanout(struct hashfile *f,
1048 void *data)
1050 struct write_commit_graph_context *ctx = data;
1051 int i, count = 0;
1052 struct commit **list = ctx->commits.list;
1055 * Write the first-level table (the list is sorted,
1056 * but we use a 256-entry lookup to be able to avoid
1057 * having to do eight extra binary search iterations).
1059 for (i = 0; i < 256; i++) {
1060 while (count < ctx->commits.nr) {
1061 if ((*list)->object.oid.hash[0] != i)
1062 break;
1063 display_progress(ctx->progress, ++ctx->progress_cnt);
1064 count++;
1065 list++;
1068 hashwrite_be32(f, count);
1071 return 0;
1074 static int write_graph_chunk_oids(struct hashfile *f,
1075 void *data)
1077 struct write_commit_graph_context *ctx = data;
1078 struct commit **list = ctx->commits.list;
1079 int count;
1080 for (count = 0; count < ctx->commits.nr; count++, list++) {
1081 display_progress(ctx->progress, ++ctx->progress_cnt);
1082 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1085 return 0;
1088 static const struct object_id *commit_to_oid(size_t index, const void *table)
1090 const struct commit * const *commits = table;
1091 return &commits[index]->object.oid;
1094 static int write_graph_chunk_data(struct hashfile *f,
1095 void *data)
1097 struct write_commit_graph_context *ctx = data;
1098 struct commit **list = ctx->commits.list;
1099 struct commit **last = ctx->commits.list + ctx->commits.nr;
1100 uint32_t num_extra_edges = 0;
1102 while (list < last) {
1103 struct commit_list *parent;
1104 struct object_id *tree;
1105 int edge_value;
1106 uint32_t packedDate[2];
1107 display_progress(ctx->progress, ++ctx->progress_cnt);
1109 if (repo_parse_commit_no_graph(ctx->r, *list))
1110 die(_("unable to parse commit %s"),
1111 oid_to_hex(&(*list)->object.oid));
1112 tree = get_commit_tree_oid(*list);
1113 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1115 parent = (*list)->parents;
1117 if (!parent)
1118 edge_value = GRAPH_PARENT_NONE;
1119 else {
1120 edge_value = oid_pos(&parent->item->object.oid,
1121 ctx->commits.list,
1122 ctx->commits.nr,
1123 commit_to_oid);
1125 if (edge_value >= 0)
1126 edge_value += ctx->new_num_commits_in_base;
1127 else if (ctx->new_base_graph) {
1128 uint32_t pos;
1129 if (find_commit_pos_in_graph(parent->item,
1130 ctx->new_base_graph,
1131 &pos))
1132 edge_value = pos;
1135 if (edge_value < 0)
1136 BUG("missing parent %s for commit %s",
1137 oid_to_hex(&parent->item->object.oid),
1138 oid_to_hex(&(*list)->object.oid));
1141 hashwrite_be32(f, edge_value);
1143 if (parent)
1144 parent = parent->next;
1146 if (!parent)
1147 edge_value = GRAPH_PARENT_NONE;
1148 else if (parent->next)
1149 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1150 else {
1151 edge_value = oid_pos(&parent->item->object.oid,
1152 ctx->commits.list,
1153 ctx->commits.nr,
1154 commit_to_oid);
1156 if (edge_value >= 0)
1157 edge_value += ctx->new_num_commits_in_base;
1158 else if (ctx->new_base_graph) {
1159 uint32_t pos;
1160 if (find_commit_pos_in_graph(parent->item,
1161 ctx->new_base_graph,
1162 &pos))
1163 edge_value = pos;
1166 if (edge_value < 0)
1167 BUG("missing parent %s for commit %s",
1168 oid_to_hex(&parent->item->object.oid),
1169 oid_to_hex(&(*list)->object.oid));
1172 hashwrite_be32(f, edge_value);
1174 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1175 do {
1176 num_extra_edges++;
1177 parent = parent->next;
1178 } while (parent);
1181 if (sizeof((*list)->date) > 4)
1182 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1183 else
1184 packedDate[0] = 0;
1186 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1188 packedDate[1] = htonl((*list)->date);
1189 hashwrite(f, packedDate, 8);
1191 list++;
1194 return 0;
1197 static int write_graph_chunk_generation_data(struct hashfile *f,
1198 void *data)
1200 struct write_commit_graph_context *ctx = data;
1201 int i, num_generation_data_overflows = 0;
1203 for (i = 0; i < ctx->commits.nr; i++) {
1204 struct commit *c = ctx->commits.list[i];
1205 timestamp_t offset;
1206 repo_parse_commit(ctx->r, c);
1207 offset = commit_graph_data_at(c)->generation - c->date;
1208 display_progress(ctx->progress, ++ctx->progress_cnt);
1210 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1211 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1212 num_generation_data_overflows++;
1215 hashwrite_be32(f, offset);
1218 return 0;
1221 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1222 void *data)
1224 struct write_commit_graph_context *ctx = data;
1225 int i;
1226 for (i = 0; i < ctx->commits.nr; i++) {
1227 struct commit *c = ctx->commits.list[i];
1228 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1229 display_progress(ctx->progress, ++ctx->progress_cnt);
1231 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1232 hashwrite_be32(f, offset >> 32);
1233 hashwrite_be32(f, (uint32_t) offset);
1237 return 0;
1240 static int write_graph_chunk_extra_edges(struct hashfile *f,
1241 void *data)
1243 struct write_commit_graph_context *ctx = data;
1244 struct commit **list = ctx->commits.list;
1245 struct commit **last = ctx->commits.list + ctx->commits.nr;
1246 struct commit_list *parent;
1248 while (list < last) {
1249 int num_parents = 0;
1251 display_progress(ctx->progress, ++ctx->progress_cnt);
1253 for (parent = (*list)->parents; num_parents < 3 && parent;
1254 parent = parent->next)
1255 num_parents++;
1257 if (num_parents <= 2) {
1258 list++;
1259 continue;
1262 /* Since num_parents > 2, this initializer is safe. */
1263 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1264 int edge_value = oid_pos(&parent->item->object.oid,
1265 ctx->commits.list,
1266 ctx->commits.nr,
1267 commit_to_oid);
1269 if (edge_value >= 0)
1270 edge_value += ctx->new_num_commits_in_base;
1271 else if (ctx->new_base_graph) {
1272 uint32_t pos;
1273 if (find_commit_pos_in_graph(parent->item,
1274 ctx->new_base_graph,
1275 &pos))
1276 edge_value = pos;
1279 if (edge_value < 0)
1280 BUG("missing parent %s for commit %s",
1281 oid_to_hex(&parent->item->object.oid),
1282 oid_to_hex(&(*list)->object.oid));
1283 else if (!parent->next)
1284 edge_value |= GRAPH_LAST_EDGE;
1286 hashwrite_be32(f, edge_value);
1289 list++;
1292 return 0;
1295 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1296 void *data)
1298 struct write_commit_graph_context *ctx = data;
1299 struct commit **list = ctx->commits.list;
1300 struct commit **last = ctx->commits.list + ctx->commits.nr;
1301 uint32_t cur_pos = 0;
1303 while (list < last) {
1304 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1305 size_t len = filter ? filter->len : 0;
1306 cur_pos += len;
1307 display_progress(ctx->progress, ++ctx->progress_cnt);
1308 hashwrite_be32(f, cur_pos);
1309 list++;
1312 return 0;
1315 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1317 struct json_writer jw = JSON_WRITER_INIT;
1319 jw_object_begin(&jw, 0);
1320 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1321 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1322 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1323 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1324 jw_end(&jw);
1326 trace2_data_json("bloom", ctx->r, "settings", &jw);
1328 jw_release(&jw);
1331 static int write_graph_chunk_bloom_data(struct hashfile *f,
1332 void *data)
1334 struct write_commit_graph_context *ctx = data;
1335 struct commit **list = ctx->commits.list;
1336 struct commit **last = ctx->commits.list + ctx->commits.nr;
1338 trace2_bloom_filter_settings(ctx);
1340 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1341 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1342 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1344 while (list < last) {
1345 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1346 size_t len = filter ? filter->len : 0;
1348 display_progress(ctx->progress, ++ctx->progress_cnt);
1349 if (len)
1350 hashwrite(f, filter->data, len * sizeof(unsigned char));
1351 list++;
1354 return 0;
1357 static int add_packed_commits(const struct object_id *oid,
1358 struct packed_git *pack,
1359 uint32_t pos,
1360 void *data)
1362 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1363 enum object_type type;
1364 off_t offset = nth_packed_object_offset(pack, pos);
1365 struct object_info oi = OBJECT_INFO_INIT;
1367 if (ctx->progress)
1368 display_progress(ctx->progress, ++ctx->progress_done);
1370 oi.typep = &type;
1371 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1372 die(_("unable to get type of object %s"), oid_to_hex(oid));
1374 if (type != OBJ_COMMIT)
1375 return 0;
1377 oid_array_append(&ctx->oids, oid);
1378 set_commit_pos(ctx->r, oid);
1380 return 0;
1383 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1385 struct commit_list *parent;
1386 for (parent = commit->parents; parent; parent = parent->next) {
1387 if (!(parent->item->object.flags & REACHABLE)) {
1388 oid_array_append(&ctx->oids, &parent->item->object.oid);
1389 parent->item->object.flags |= REACHABLE;
1394 static void close_reachable(struct write_commit_graph_context *ctx)
1396 int i;
1397 struct commit *commit;
1398 enum commit_graph_split_flags flags = ctx->opts ?
1399 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1401 if (ctx->report_progress)
1402 ctx->progress = start_delayed_progress(
1403 _("Loading known commits in commit graph"),
1404 ctx->oids.nr);
1405 for (i = 0; i < ctx->oids.nr; i++) {
1406 display_progress(ctx->progress, i + 1);
1407 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1408 if (commit)
1409 commit->object.flags |= REACHABLE;
1411 stop_progress(&ctx->progress);
1414 * As this loop runs, ctx->oids.nr may grow, but not more
1415 * than the number of missing commits in the reachable
1416 * closure.
1418 if (ctx->report_progress)
1419 ctx->progress = start_delayed_progress(
1420 _("Expanding reachable commits in commit graph"),
1422 for (i = 0; i < ctx->oids.nr; i++) {
1423 display_progress(ctx->progress, i + 1);
1424 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1426 if (!commit)
1427 continue;
1428 if (ctx->split) {
1429 if ((!repo_parse_commit(ctx->r, commit) &&
1430 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1431 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1432 add_missing_parents(ctx, commit);
1433 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1434 add_missing_parents(ctx, commit);
1436 stop_progress(&ctx->progress);
1438 if (ctx->report_progress)
1439 ctx->progress = start_delayed_progress(
1440 _("Clearing commit marks in commit graph"),
1441 ctx->oids.nr);
1442 for (i = 0; i < ctx->oids.nr; i++) {
1443 display_progress(ctx->progress, i + 1);
1444 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1446 if (commit)
1447 commit->object.flags &= ~REACHABLE;
1449 stop_progress(&ctx->progress);
1452 struct compute_generation_info {
1453 struct repository *r;
1454 struct packed_commit_list *commits;
1455 struct progress *progress;
1456 int progress_cnt;
1458 timestamp_t (*get_generation)(struct commit *c, void *data);
1459 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1460 void *data;
1463 static timestamp_t compute_generation_from_max(struct commit *c,
1464 timestamp_t max_gen,
1465 int generation_version)
1467 switch (generation_version) {
1468 case 1: /* topological levels */
1469 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1470 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1471 return max_gen + 1;
1473 case 2: /* corrected commit date */
1474 if (c->date && c->date > max_gen)
1475 max_gen = c->date - 1;
1476 return max_gen + 1;
1478 default:
1479 BUG("attempting unimplemented version");
1483 static void compute_reachable_generation_numbers(
1484 struct compute_generation_info *info,
1485 int generation_version)
1487 int i;
1488 struct commit_list *list = NULL;
1490 for (i = 0; i < info->commits->nr; i++) {
1491 struct commit *c = info->commits->list[i];
1492 timestamp_t gen;
1493 repo_parse_commit(info->r, c);
1494 gen = info->get_generation(c, info->data);
1495 display_progress(info->progress, info->progress_cnt + 1);
1497 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1498 continue;
1500 commit_list_insert(c, &list);
1501 while (list) {
1502 struct commit *current = list->item;
1503 struct commit_list *parent;
1504 int all_parents_computed = 1;
1505 uint32_t max_gen = 0;
1507 for (parent = current->parents; parent; parent = parent->next) {
1508 repo_parse_commit(info->r, parent->item);
1509 gen = info->get_generation(parent->item, info->data);
1511 if (gen == GENERATION_NUMBER_ZERO) {
1512 all_parents_computed = 0;
1513 commit_list_insert(parent->item, &list);
1514 break;
1517 if (gen > max_gen)
1518 max_gen = gen;
1521 if (all_parents_computed) {
1522 pop_commit(&list);
1523 gen = compute_generation_from_max(
1524 current, max_gen,
1525 generation_version);
1526 info->set_generation(current, gen, info->data);
1532 static timestamp_t get_topo_level(struct commit *c, void *data)
1534 struct write_commit_graph_context *ctx = data;
1535 return *topo_level_slab_at(ctx->topo_levels, c);
1538 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1540 struct write_commit_graph_context *ctx = data;
1541 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1544 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1546 struct compute_generation_info info = {
1547 .r = ctx->r,
1548 .commits = &ctx->commits,
1549 .get_generation = get_topo_level,
1550 .set_generation = set_topo_level,
1551 .data = ctx,
1554 if (ctx->report_progress)
1555 info.progress = ctx->progress
1556 = start_delayed_progress(
1557 _("Computing commit graph topological levels"),
1558 ctx->commits.nr);
1560 compute_reachable_generation_numbers(&info, 1);
1562 stop_progress(&ctx->progress);
1565 static timestamp_t get_generation_from_graph_data(struct commit *c, void *data)
1567 return commit_graph_data_at(c)->generation;
1570 static void set_generation_v2(struct commit *c, timestamp_t t, void *data)
1572 struct commit_graph_data *g = commit_graph_data_at(c);
1573 g->generation = t;
1576 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1578 int i;
1579 struct compute_generation_info info = {
1580 .r = ctx->r,
1581 .commits = &ctx->commits,
1582 .get_generation = get_generation_from_graph_data,
1583 .set_generation = set_generation_v2,
1584 .data = ctx,
1587 if (ctx->report_progress)
1588 info.progress = ctx->progress
1589 = start_delayed_progress(
1590 _("Computing commit graph generation numbers"),
1591 ctx->commits.nr);
1593 if (!ctx->trust_generation_numbers) {
1594 for (i = 0; i < ctx->commits.nr; i++) {
1595 struct commit *c = ctx->commits.list[i];
1596 repo_parse_commit(ctx->r, c);
1597 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1601 compute_reachable_generation_numbers(&info, 2);
1603 for (i = 0; i < ctx->commits.nr; i++) {
1604 struct commit *c = ctx->commits.list[i];
1605 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1606 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1607 ctx->num_generation_data_overflows++;
1609 stop_progress(&ctx->progress);
1612 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1613 void *data)
1615 commit_graph_data_at(c)->generation = t;
1619 * After this method, all commits reachable from those in the given
1620 * list will have non-zero, non-infinite generation numbers.
1622 void ensure_generations_valid(struct repository *r,
1623 struct commit **commits, size_t nr)
1625 int generation_version = get_configured_generation_version(r);
1626 struct packed_commit_list list = {
1627 .list = commits,
1628 .alloc = nr,
1629 .nr = nr,
1631 struct compute_generation_info info = {
1632 .r = r,
1633 .commits = &list,
1634 .get_generation = get_generation_from_graph_data,
1635 .set_generation = set_generation_in_graph_data,
1638 compute_reachable_generation_numbers(&info, generation_version);
1641 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1643 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1644 ctx->count_bloom_filter_computed);
1645 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1646 ctx->count_bloom_filter_not_computed);
1647 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1648 ctx->count_bloom_filter_trunc_empty);
1649 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1650 ctx->count_bloom_filter_trunc_large);
1653 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1655 int i;
1656 struct progress *progress = NULL;
1657 struct commit **sorted_commits;
1658 int max_new_filters;
1660 init_bloom_filters();
1662 if (ctx->report_progress)
1663 progress = start_delayed_progress(
1664 _("Computing commit changed paths Bloom filters"),
1665 ctx->commits.nr);
1667 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1669 if (ctx->order_by_pack)
1670 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1671 else
1672 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1674 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1675 ctx->opts->max_new_filters : ctx->commits.nr;
1677 for (i = 0; i < ctx->commits.nr; i++) {
1678 enum bloom_filter_computed computed = 0;
1679 struct commit *c = sorted_commits[i];
1680 struct bloom_filter *filter = get_or_compute_bloom_filter(
1681 ctx->r,
1683 ctx->count_bloom_filter_computed < max_new_filters,
1684 ctx->bloom_settings,
1685 &computed);
1686 if (computed & BLOOM_COMPUTED) {
1687 ctx->count_bloom_filter_computed++;
1688 if (computed & BLOOM_TRUNC_EMPTY)
1689 ctx->count_bloom_filter_trunc_empty++;
1690 if (computed & BLOOM_TRUNC_LARGE)
1691 ctx->count_bloom_filter_trunc_large++;
1692 } else if (computed & BLOOM_NOT_COMPUTED)
1693 ctx->count_bloom_filter_not_computed++;
1694 ctx->total_bloom_filter_data_size += filter
1695 ? sizeof(unsigned char) * filter->len : 0;
1696 display_progress(progress, i + 1);
1699 if (trace2_is_enabled())
1700 trace2_bloom_filter_write_statistics(ctx);
1702 free(sorted_commits);
1703 stop_progress(&progress);
1706 struct refs_cb_data {
1707 struct oidset *commits;
1708 struct progress *progress;
1711 static int add_ref_to_set(const char *refname UNUSED,
1712 const struct object_id *oid,
1713 int flags UNUSED, void *cb_data)
1715 struct object_id peeled;
1716 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1718 if (!peel_iterated_oid(oid, &peeled))
1719 oid = &peeled;
1720 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1721 oidset_insert(data->commits, oid);
1723 display_progress(data->progress, oidset_size(data->commits));
1725 return 0;
1728 int write_commit_graph_reachable(struct object_directory *odb,
1729 enum commit_graph_write_flags flags,
1730 const struct commit_graph_opts *opts)
1732 struct oidset commits = OIDSET_INIT;
1733 struct refs_cb_data data;
1734 int result;
1736 memset(&data, 0, sizeof(data));
1737 data.commits = &commits;
1738 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1739 data.progress = start_delayed_progress(
1740 _("Collecting referenced commits"), 0);
1742 for_each_ref(add_ref_to_set, &data);
1744 stop_progress(&data.progress);
1746 result = write_commit_graph(odb, NULL, &commits,
1747 flags, opts);
1749 oidset_clear(&commits);
1750 return result;
1753 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1754 const struct string_list *pack_indexes)
1756 uint32_t i;
1757 struct strbuf progress_title = STRBUF_INIT;
1758 struct strbuf packname = STRBUF_INIT;
1759 int dirlen;
1760 int ret = 0;
1762 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1763 dirlen = packname.len;
1764 if (ctx->report_progress) {
1765 strbuf_addf(&progress_title,
1766 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1767 "Finding commits for commit graph in %"PRIuMAX" packs",
1768 pack_indexes->nr),
1769 (uintmax_t)pack_indexes->nr);
1770 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1771 ctx->progress_done = 0;
1773 for (i = 0; i < pack_indexes->nr; i++) {
1774 struct packed_git *p;
1775 strbuf_setlen(&packname, dirlen);
1776 strbuf_addstr(&packname, pack_indexes->items[i].string);
1777 p = add_packed_git(packname.buf, packname.len, 1);
1778 if (!p) {
1779 ret = error(_("error adding pack %s"), packname.buf);
1780 goto cleanup;
1782 if (open_pack_index(p)) {
1783 ret = error(_("error opening index for %s"), packname.buf);
1784 goto cleanup;
1786 for_each_object_in_pack(p, add_packed_commits, ctx,
1787 FOR_EACH_OBJECT_PACK_ORDER);
1788 close_pack(p);
1789 free(p);
1792 cleanup:
1793 stop_progress(&ctx->progress);
1794 strbuf_release(&progress_title);
1795 strbuf_release(&packname);
1797 return ret;
1800 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1801 struct oidset *commits)
1803 struct oidset_iter iter;
1804 struct object_id *oid;
1806 if (!oidset_size(commits))
1807 return 0;
1809 oidset_iter_init(commits, &iter);
1810 while ((oid = oidset_iter_next(&iter))) {
1811 oid_array_append(&ctx->oids, oid);
1814 return 0;
1817 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1819 if (ctx->report_progress)
1820 ctx->progress = start_delayed_progress(
1821 _("Finding commits for commit graph among packed objects"),
1822 ctx->approx_nr_objects);
1823 for_each_packed_object(add_packed_commits, ctx,
1824 FOR_EACH_OBJECT_PACK_ORDER);
1825 if (ctx->progress_done < ctx->approx_nr_objects)
1826 display_progress(ctx->progress, ctx->approx_nr_objects);
1827 stop_progress(&ctx->progress);
1830 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1832 uint32_t i;
1833 enum commit_graph_split_flags flags = ctx->opts ?
1834 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1836 ctx->num_extra_edges = 0;
1837 if (ctx->report_progress)
1838 ctx->progress = start_delayed_progress(
1839 _("Finding extra edges in commit graph"),
1840 ctx->oids.nr);
1841 oid_array_sort(&ctx->oids);
1842 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1843 unsigned int num_parents;
1845 display_progress(ctx->progress, i + 1);
1847 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1848 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1850 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1851 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1852 continue;
1854 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1855 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1856 else
1857 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1859 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1860 if (num_parents > 2)
1861 ctx->num_extra_edges += num_parents - 1;
1863 ctx->commits.nr++;
1865 stop_progress(&ctx->progress);
1868 static int write_graph_chunk_base_1(struct hashfile *f,
1869 struct commit_graph *g)
1871 int num = 0;
1873 if (!g)
1874 return 0;
1876 num = write_graph_chunk_base_1(f, g->base_graph);
1877 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1878 return num + 1;
1881 static int write_graph_chunk_base(struct hashfile *f,
1882 void *data)
1884 struct write_commit_graph_context *ctx = data;
1885 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1887 if (num != ctx->num_commit_graphs_after - 1) {
1888 error(_("failed to write correct number of base graph ids"));
1889 return -1;
1892 return 0;
1895 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1897 uint32_t i;
1898 int fd;
1899 struct hashfile *f;
1900 struct lock_file lk = LOCK_INIT;
1901 const unsigned hashsz = the_hash_algo->rawsz;
1902 struct strbuf progress_title = STRBUF_INIT;
1903 struct chunkfile *cf;
1904 unsigned char file_hash[GIT_MAX_RAWSZ];
1906 if (ctx->split) {
1907 struct strbuf tmp_file = STRBUF_INIT;
1909 strbuf_addf(&tmp_file,
1910 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1911 ctx->odb->path);
1912 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1913 } else {
1914 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1917 if (safe_create_leading_directories(ctx->graph_name)) {
1918 UNLEAK(ctx->graph_name);
1919 error(_("unable to create leading directories of %s"),
1920 ctx->graph_name);
1921 return -1;
1924 if (ctx->split) {
1925 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1927 hold_lock_file_for_update_mode(&lk, lock_name,
1928 LOCK_DIE_ON_ERROR, 0444);
1929 free(lock_name);
1931 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1932 if (fd < 0) {
1933 error(_("unable to create temporary graph layer"));
1934 return -1;
1937 if (adjust_shared_perm(ctx->graph_name)) {
1938 error(_("unable to adjust shared permissions for '%s'"),
1939 ctx->graph_name);
1940 return -1;
1943 f = hashfd(fd, ctx->graph_name);
1944 } else {
1945 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1946 LOCK_DIE_ON_ERROR, 0444);
1947 fd = get_lock_file_fd(&lk);
1948 f = hashfd(fd, get_lock_file_path(&lk));
1951 cf = init_chunkfile(f);
1953 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1954 write_graph_chunk_fanout);
1955 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1956 write_graph_chunk_oids);
1957 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1958 write_graph_chunk_data);
1960 if (ctx->write_generation_data)
1961 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1962 sizeof(uint32_t) * ctx->commits.nr,
1963 write_graph_chunk_generation_data);
1964 if (ctx->num_generation_data_overflows)
1965 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1966 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1967 write_graph_chunk_generation_data_overflow);
1968 if (ctx->num_extra_edges)
1969 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1970 4 * ctx->num_extra_edges,
1971 write_graph_chunk_extra_edges);
1972 if (ctx->changed_paths) {
1973 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1974 sizeof(uint32_t) * ctx->commits.nr,
1975 write_graph_chunk_bloom_indexes);
1976 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1977 sizeof(uint32_t) * 3
1978 + ctx->total_bloom_filter_data_size,
1979 write_graph_chunk_bloom_data);
1981 if (ctx->num_commit_graphs_after > 1)
1982 add_chunk(cf, GRAPH_CHUNKID_BASE,
1983 hashsz * (ctx->num_commit_graphs_after - 1),
1984 write_graph_chunk_base);
1986 hashwrite_be32(f, GRAPH_SIGNATURE);
1988 hashwrite_u8(f, GRAPH_VERSION);
1989 hashwrite_u8(f, oid_version(the_hash_algo));
1990 hashwrite_u8(f, get_num_chunks(cf));
1991 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1993 if (ctx->report_progress) {
1994 strbuf_addf(&progress_title,
1995 Q_("Writing out commit graph in %d pass",
1996 "Writing out commit graph in %d passes",
1997 get_num_chunks(cf)),
1998 get_num_chunks(cf));
1999 ctx->progress = start_delayed_progress(
2000 progress_title.buf,
2001 get_num_chunks(cf) * ctx->commits.nr);
2004 write_chunkfile(cf, ctx);
2006 stop_progress(&ctx->progress);
2007 strbuf_release(&progress_title);
2009 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2010 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2011 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2013 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2014 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2015 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2016 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2019 close_commit_graph(ctx->r->objects);
2020 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2021 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2022 free_chunkfile(cf);
2024 if (ctx->split) {
2025 FILE *chainf = fdopen_lock_file(&lk, "w");
2026 char *final_graph_name;
2027 int result;
2029 close(fd);
2031 if (!chainf) {
2032 error(_("unable to open commit-graph chain file"));
2033 return -1;
2036 if (ctx->base_graph_name) {
2037 const char *dest;
2038 int idx = ctx->num_commit_graphs_after - 1;
2039 if (ctx->num_commit_graphs_after > 1)
2040 idx--;
2042 dest = ctx->commit_graph_filenames_after[idx];
2044 if (strcmp(ctx->base_graph_name, dest)) {
2045 result = rename(ctx->base_graph_name, dest);
2047 if (result) {
2048 error(_("failed to rename base commit-graph file"));
2049 return -1;
2052 } else {
2053 char *graph_name = get_commit_graph_filename(ctx->odb);
2054 unlink(graph_name);
2055 free(graph_name);
2058 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2059 final_graph_name = get_split_graph_filename(ctx->odb,
2060 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2061 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2063 result = rename(ctx->graph_name, final_graph_name);
2065 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2066 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2068 if (result) {
2069 error(_("failed to rename temporary commit-graph file"));
2070 return -1;
2074 commit_lock_file(&lk);
2076 return 0;
2079 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2081 struct commit_graph *g;
2082 uint32_t num_commits;
2083 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2084 uint32_t i;
2086 int max_commits = 0;
2087 int size_mult = 2;
2089 if (ctx->opts) {
2090 max_commits = ctx->opts->max_commits;
2092 if (ctx->opts->size_multiple)
2093 size_mult = ctx->opts->size_multiple;
2095 flags = ctx->opts->split_flags;
2098 g = ctx->r->objects->commit_graph;
2099 num_commits = ctx->commits.nr;
2100 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2101 ctx->num_commit_graphs_after = 1;
2102 else
2103 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2105 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2106 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2107 while (g && (g->num_commits <= size_mult * num_commits ||
2108 (max_commits && num_commits > max_commits))) {
2109 if (g->odb != ctx->odb)
2110 break;
2112 num_commits += g->num_commits;
2113 g = g->base_graph;
2115 ctx->num_commit_graphs_after--;
2119 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2120 ctx->new_base_graph = g;
2121 else if (ctx->num_commit_graphs_after != 1)
2122 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2123 "should be 1 with --split=replace");
2125 if (ctx->num_commit_graphs_after == 2) {
2126 char *old_graph_name = get_commit_graph_filename(g->odb);
2128 if (!strcmp(g->filename, old_graph_name) &&
2129 g->odb != ctx->odb) {
2130 ctx->num_commit_graphs_after = 1;
2131 ctx->new_base_graph = NULL;
2134 free(old_graph_name);
2137 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2138 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2140 for (i = 0; i < ctx->num_commit_graphs_after &&
2141 i < ctx->num_commit_graphs_before; i++)
2142 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2144 i = ctx->num_commit_graphs_before - 1;
2145 g = ctx->r->objects->commit_graph;
2147 while (g) {
2148 if (i < ctx->num_commit_graphs_after)
2149 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2152 * If the topmost remaining layer has generation data chunk, the
2153 * resultant layer also has generation data chunk.
2155 if (i == ctx->num_commit_graphs_after - 2)
2156 ctx->write_generation_data = !!g->chunk_generation_data;
2158 i--;
2159 g = g->base_graph;
2163 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2164 struct commit_graph *g)
2166 uint32_t i;
2167 uint32_t offset = g->num_commits_in_base;
2169 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2171 for (i = 0; i < g->num_commits; i++) {
2172 struct object_id oid;
2173 struct commit *result;
2175 display_progress(ctx->progress, i + 1);
2177 load_oid_from_graph(g, i + offset, &oid);
2179 /* only add commits if they still exist in the repo */
2180 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2182 if (result) {
2183 ctx->commits.list[ctx->commits.nr] = result;
2184 ctx->commits.nr++;
2189 static int commit_compare(const void *_a, const void *_b)
2191 const struct commit *a = *(const struct commit **)_a;
2192 const struct commit *b = *(const struct commit **)_b;
2193 return oidcmp(&a->object.oid, &b->object.oid);
2196 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2198 uint32_t i, dedup_i = 0;
2200 if (ctx->report_progress)
2201 ctx->progress = start_delayed_progress(
2202 _("Scanning merged commits"),
2203 ctx->commits.nr);
2205 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2207 ctx->num_extra_edges = 0;
2208 for (i = 0; i < ctx->commits.nr; i++) {
2209 display_progress(ctx->progress, i + 1);
2211 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2212 &ctx->commits.list[i]->object.oid)) {
2214 * Silently ignore duplicates. These were likely
2215 * created due to a commit appearing in multiple
2216 * layers of the chain, which is unexpected but
2217 * not invalid. We should make sure there is a
2218 * unique copy in the new layer.
2220 } else {
2221 unsigned int num_parents;
2223 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2224 dedup_i++;
2226 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2227 if (num_parents > 2)
2228 ctx->num_extra_edges += num_parents - 1;
2232 ctx->commits.nr = dedup_i;
2234 stop_progress(&ctx->progress);
2237 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2239 struct commit_graph *g = ctx->r->objects->commit_graph;
2240 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2242 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2243 current_graph_number--;
2245 if (ctx->report_progress)
2246 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2248 merge_commit_graph(ctx, g);
2249 stop_progress(&ctx->progress);
2251 g = g->base_graph;
2254 if (g) {
2255 ctx->new_base_graph = g;
2256 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2259 if (ctx->new_base_graph)
2260 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2262 sort_and_scan_merged_commits(ctx);
2265 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2267 uint32_t i;
2268 time_t now = time(NULL);
2270 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2271 struct stat st;
2272 struct utimbuf updated_time;
2274 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2275 continue;
2277 updated_time.actime = st.st_atime;
2278 updated_time.modtime = now;
2279 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2283 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2285 struct strbuf path = STRBUF_INIT;
2286 DIR *dir;
2287 struct dirent *de;
2288 size_t dirnamelen;
2289 timestamp_t expire_time = time(NULL);
2291 if (ctx->opts && ctx->opts->expire_time)
2292 expire_time = ctx->opts->expire_time;
2293 if (!ctx->split) {
2294 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2295 unlink(chain_file_name);
2296 free(chain_file_name);
2297 ctx->num_commit_graphs_after = 0;
2300 strbuf_addstr(&path, ctx->odb->path);
2301 strbuf_addstr(&path, "/info/commit-graphs");
2302 dir = opendir(path.buf);
2304 if (!dir)
2305 goto out;
2307 strbuf_addch(&path, '/');
2308 dirnamelen = path.len;
2309 while ((de = readdir(dir)) != NULL) {
2310 struct stat st;
2311 uint32_t i, found = 0;
2313 strbuf_setlen(&path, dirnamelen);
2314 strbuf_addstr(&path, de->d_name);
2316 if (stat(path.buf, &st) < 0)
2317 continue;
2319 if (st.st_mtime > expire_time)
2320 continue;
2321 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2322 continue;
2324 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2325 if (!strcmp(ctx->commit_graph_filenames_after[i],
2326 path.buf)) {
2327 found = 1;
2328 break;
2332 if (!found)
2333 unlink(path.buf);
2336 out:
2337 if(dir)
2338 closedir(dir);
2339 strbuf_release(&path);
2342 int write_commit_graph(struct object_directory *odb,
2343 const struct string_list *const pack_indexes,
2344 struct oidset *commits,
2345 enum commit_graph_write_flags flags,
2346 const struct commit_graph_opts *opts)
2348 struct repository *r = the_repository;
2349 struct write_commit_graph_context *ctx;
2350 uint32_t i;
2351 int res = 0;
2352 int replace = 0;
2353 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2354 struct topo_level_slab topo_levels;
2356 prepare_repo_settings(r);
2357 if (!r->settings.core_commit_graph) {
2358 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2359 return 0;
2361 if (!commit_graph_compatible(r))
2362 return 0;
2364 CALLOC_ARRAY(ctx, 1);
2365 ctx->r = r;
2366 ctx->odb = odb;
2367 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2368 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2369 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2370 ctx->opts = opts;
2371 ctx->total_bloom_filter_data_size = 0;
2372 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2373 ctx->num_generation_data_overflows = 0;
2375 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2376 bloom_settings.bits_per_entry);
2377 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2378 bloom_settings.num_hashes);
2379 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2380 bloom_settings.max_changed_paths);
2381 ctx->bloom_settings = &bloom_settings;
2383 init_topo_level_slab(&topo_levels);
2384 ctx->topo_levels = &topo_levels;
2386 prepare_commit_graph(ctx->r);
2387 if (ctx->r->objects->commit_graph) {
2388 struct commit_graph *g = ctx->r->objects->commit_graph;
2390 while (g) {
2391 g->topo_levels = &topo_levels;
2392 g = g->base_graph;
2396 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2397 ctx->changed_paths = 1;
2398 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2399 struct commit_graph *g;
2401 g = ctx->r->objects->commit_graph;
2403 /* We have changed-paths already. Keep them in the next graph */
2404 if (g && g->chunk_bloom_data) {
2405 ctx->changed_paths = 1;
2406 ctx->bloom_settings = g->bloom_filter_settings;
2410 if (ctx->split) {
2411 struct commit_graph *g = ctx->r->objects->commit_graph;
2413 while (g) {
2414 ctx->num_commit_graphs_before++;
2415 g = g->base_graph;
2418 if (ctx->num_commit_graphs_before) {
2419 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2420 i = ctx->num_commit_graphs_before;
2421 g = ctx->r->objects->commit_graph;
2423 while (g) {
2424 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2425 g = g->base_graph;
2429 if (ctx->opts)
2430 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2433 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2435 if (ctx->append && ctx->r->objects->commit_graph) {
2436 struct commit_graph *g = ctx->r->objects->commit_graph;
2437 for (i = 0; i < g->num_commits; i++) {
2438 struct object_id oid;
2439 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2440 oid_array_append(&ctx->oids, &oid);
2444 if (pack_indexes) {
2445 ctx->order_by_pack = 1;
2446 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2447 goto cleanup;
2450 if (commits) {
2451 if ((res = fill_oids_from_commits(ctx, commits)))
2452 goto cleanup;
2455 if (!pack_indexes && !commits) {
2456 ctx->order_by_pack = 1;
2457 fill_oids_from_all_packs(ctx);
2460 close_reachable(ctx);
2462 copy_oids_to_commits(ctx);
2464 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2465 error(_("too many commits to write graph"));
2466 res = -1;
2467 goto cleanup;
2470 if (!ctx->commits.nr && !replace)
2471 goto cleanup;
2473 if (ctx->split) {
2474 split_graph_merge_strategy(ctx);
2476 if (!replace)
2477 merge_commit_graphs(ctx);
2478 } else
2479 ctx->num_commit_graphs_after = 1;
2481 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2483 compute_topological_levels(ctx);
2484 if (ctx->write_generation_data)
2485 compute_generation_numbers(ctx);
2487 if (ctx->changed_paths)
2488 compute_bloom_filters(ctx);
2490 res = write_commit_graph_file(ctx);
2492 if (ctx->split)
2493 mark_commit_graphs(ctx);
2495 expire_commit_graphs(ctx);
2497 cleanup:
2498 free(ctx->graph_name);
2499 free(ctx->commits.list);
2500 oid_array_clear(&ctx->oids);
2501 clear_topo_level_slab(&topo_levels);
2503 if (ctx->commit_graph_filenames_after) {
2504 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2505 free(ctx->commit_graph_filenames_after[i]);
2506 free(ctx->commit_graph_hash_after[i]);
2509 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2510 free(ctx->commit_graph_filenames_before[i]);
2512 free(ctx->commit_graph_filenames_after);
2513 free(ctx->commit_graph_filenames_before);
2514 free(ctx->commit_graph_hash_after);
2517 free(ctx);
2519 return res;
2522 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2523 static int verify_commit_graph_error;
2525 __attribute__((format (printf, 1, 2)))
2526 static void graph_report(const char *fmt, ...)
2528 va_list ap;
2530 verify_commit_graph_error = 1;
2531 va_start(ap, fmt);
2532 vfprintf(stderr, fmt, ap);
2533 fprintf(stderr, "\n");
2534 va_end(ap);
2537 #define GENERATION_ZERO_EXISTS 1
2538 #define GENERATION_NUMBER_EXISTS 2
2540 static int commit_graph_checksum_valid(struct commit_graph *g)
2542 return hashfile_checksum_valid(g->data, g->data_len);
2545 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2547 uint32_t i, cur_fanout_pos = 0;
2548 struct object_id prev_oid, cur_oid;
2549 int generation_zero = 0;
2550 struct progress *progress = NULL;
2551 int local_error = 0;
2553 if (!g) {
2554 graph_report("no commit-graph file loaded");
2555 return 1;
2558 verify_commit_graph_error = verify_commit_graph_lite(g);
2559 if (verify_commit_graph_error)
2560 return verify_commit_graph_error;
2562 if (!commit_graph_checksum_valid(g)) {
2563 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2564 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2567 for (i = 0; i < g->num_commits; i++) {
2568 struct commit *graph_commit;
2570 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2572 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2573 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2574 oid_to_hex(&prev_oid),
2575 oid_to_hex(&cur_oid));
2577 oidcpy(&prev_oid, &cur_oid);
2579 while (cur_oid.hash[0] > cur_fanout_pos) {
2580 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2582 if (i != fanout_value)
2583 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2584 cur_fanout_pos, fanout_value, i);
2585 cur_fanout_pos++;
2588 graph_commit = lookup_commit(r, &cur_oid);
2589 if (!parse_commit_in_graph_one(r, g, graph_commit))
2590 graph_report(_("failed to parse commit %s from commit-graph"),
2591 oid_to_hex(&cur_oid));
2594 while (cur_fanout_pos < 256) {
2595 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2597 if (g->num_commits != fanout_value)
2598 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2599 cur_fanout_pos, fanout_value, i);
2601 cur_fanout_pos++;
2604 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2605 return verify_commit_graph_error;
2607 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2608 progress = start_progress(_("Verifying commits in commit graph"),
2609 g->num_commits);
2611 for (i = 0; i < g->num_commits; i++) {
2612 struct commit *graph_commit, *odb_commit;
2613 struct commit_list *graph_parents, *odb_parents;
2614 timestamp_t max_generation = 0;
2615 timestamp_t generation;
2617 display_progress(progress, i + 1);
2618 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2620 graph_commit = lookup_commit(r, &cur_oid);
2621 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2622 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2623 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2624 oid_to_hex(&cur_oid));
2625 continue;
2628 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2629 get_commit_tree_oid(odb_commit)))
2630 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2631 oid_to_hex(&cur_oid),
2632 oid_to_hex(get_commit_tree_oid(graph_commit)),
2633 oid_to_hex(get_commit_tree_oid(odb_commit)));
2635 graph_parents = graph_commit->parents;
2636 odb_parents = odb_commit->parents;
2638 while (graph_parents) {
2639 if (!odb_parents) {
2640 graph_report(_("commit-graph parent list for commit %s is too long"),
2641 oid_to_hex(&cur_oid));
2642 break;
2645 /* parse parent in case it is in a base graph */
2646 parse_commit_in_graph_one(r, g, graph_parents->item);
2648 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2649 graph_report(_("commit-graph parent for %s is %s != %s"),
2650 oid_to_hex(&cur_oid),
2651 oid_to_hex(&graph_parents->item->object.oid),
2652 oid_to_hex(&odb_parents->item->object.oid));
2654 generation = commit_graph_generation(graph_parents->item);
2655 if (generation > max_generation)
2656 max_generation = generation;
2658 graph_parents = graph_parents->next;
2659 odb_parents = odb_parents->next;
2662 if (odb_parents)
2663 graph_report(_("commit-graph parent list for commit %s terminates early"),
2664 oid_to_hex(&cur_oid));
2666 if (!commit_graph_generation(graph_commit)) {
2667 if (generation_zero == GENERATION_NUMBER_EXISTS)
2668 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2669 oid_to_hex(&cur_oid));
2670 generation_zero = GENERATION_ZERO_EXISTS;
2671 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2672 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2673 oid_to_hex(&cur_oid));
2675 if (generation_zero == GENERATION_ZERO_EXISTS)
2676 continue;
2679 * If we are using topological level and one of our parents has
2680 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2681 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2682 * in the following condition.
2684 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2685 max_generation--;
2687 generation = commit_graph_generation(graph_commit);
2688 if (generation < max_generation + 1)
2689 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2690 oid_to_hex(&cur_oid),
2691 generation,
2692 max_generation + 1);
2694 if (graph_commit->date != odb_commit->date)
2695 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2696 oid_to_hex(&cur_oid),
2697 graph_commit->date,
2698 odb_commit->date);
2700 stop_progress(&progress);
2702 local_error = verify_commit_graph_error;
2704 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2705 local_error |= verify_commit_graph(r, g->base_graph, flags);
2707 return local_error;
2710 void free_commit_graph(struct commit_graph *g)
2712 if (!g)
2713 return;
2714 if (g->data) {
2715 munmap((void *)g->data, g->data_len);
2716 g->data = NULL;
2718 free(g->filename);
2719 free(g->bloom_filter_settings);
2720 free(g);
2723 void disable_commit_graph(struct repository *r)
2725 r->commit_graph_disabled = 1;