Merge branch 'jc/am-parseopt-fix'
[git.git] / commit-graph.c
blob0aa1640d15aca59b90617677afd30ef7c8d247c1
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "hex.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-file.h"
15 #include "object-store-ll.h"
16 #include "oid-array.h"
17 #include "path.h"
18 #include "alloc.h"
19 #include "hashmap.h"
20 #include "replace-object.h"
21 #include "progress.h"
22 #include "bloom.h"
23 #include "commit-slab.h"
24 #include "shallow.h"
25 #include "json-writer.h"
26 #include "trace2.h"
27 #include "tree.h"
28 #include "chunk-format.h"
30 void git_test_write_commit_graph_or_die(void)
32 int flags = 0;
33 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
34 return;
36 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
37 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
39 if (write_commit_graph_reachable(the_repository->objects->odb,
40 flags, NULL))
41 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
44 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
45 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
46 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
47 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
48 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
49 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
50 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
51 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
52 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
53 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
55 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
57 #define GRAPH_VERSION_1 0x1
58 #define GRAPH_VERSION GRAPH_VERSION_1
60 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
62 #define GRAPH_PARENT_NONE 0x70000000
64 #define GRAPH_LAST_EDGE 0x80000000
66 #define GRAPH_HEADER_SIZE 8
67 #define GRAPH_FANOUT_SIZE (4 * 256)
68 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
69 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
71 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
73 /* Remember to update object flag allocation in object.h */
74 #define REACHABLE (1u<<15)
76 define_commit_slab(topo_level_slab, uint32_t);
78 /* Keep track of the order in which commits are added to our list. */
79 define_commit_slab(commit_pos, int);
80 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
82 static void set_commit_pos(struct repository *r, const struct object_id *oid)
84 static int32_t max_pos;
85 struct commit *commit = lookup_commit(r, oid);
87 if (!commit)
88 return; /* should never happen, but be lenient */
90 *commit_pos_at(&commit_pos, commit) = max_pos++;
93 static int commit_pos_cmp(const void *va, const void *vb)
95 const struct commit *a = *(const struct commit **)va;
96 const struct commit *b = *(const struct commit **)vb;
97 return commit_pos_at(&commit_pos, a) -
98 commit_pos_at(&commit_pos, b);
101 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
102 static struct commit_graph_data_slab commit_graph_data_slab =
103 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
105 static int get_configured_generation_version(struct repository *r)
107 int version = 2;
108 repo_config_get_int(r, "commitgraph.generationversion", &version);
109 return version;
112 uint32_t commit_graph_position(const struct commit *c)
114 struct commit_graph_data *data =
115 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
117 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
120 timestamp_t commit_graph_generation(const struct commit *c)
122 struct commit_graph_data *data =
123 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
125 if (data && data->generation)
126 return data->generation;
128 return GENERATION_NUMBER_INFINITY;
131 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
133 unsigned int i, nth_slab;
134 struct commit_graph_data *data =
135 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
137 if (data)
138 return data;
140 nth_slab = c->index / commit_graph_data_slab.slab_size;
141 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
144 * commit-slab initializes elements with zero, overwrite this with
145 * COMMIT_NOT_FROM_GRAPH for graph_pos.
147 * We avoid initializing generation with checking if graph position
148 * is not COMMIT_NOT_FROM_GRAPH.
150 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
151 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
152 COMMIT_NOT_FROM_GRAPH;
155 return data;
159 * Should be used only while writing commit-graph as it compares
160 * generation value of commits by directly accessing commit-slab.
162 static int commit_gen_cmp(const void *va, const void *vb)
164 const struct commit *a = *(const struct commit **)va;
165 const struct commit *b = *(const struct commit **)vb;
167 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
168 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
169 /* lower generation commits first */
170 if (generation_a < generation_b)
171 return -1;
172 else if (generation_a > generation_b)
173 return 1;
175 /* use date as a heuristic when generations are equal */
176 if (a->date < b->date)
177 return -1;
178 else if (a->date > b->date)
179 return 1;
180 return 0;
183 char *get_commit_graph_filename(struct object_directory *obj_dir)
185 return xstrfmt("%s/info/commit-graph", obj_dir->path);
188 static char *get_split_graph_filename(struct object_directory *odb,
189 const char *oid_hex)
191 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
192 oid_hex);
195 char *get_commit_graph_chain_filename(struct object_directory *odb)
197 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
200 static struct commit_graph *alloc_commit_graph(void)
202 struct commit_graph *g = xcalloc(1, sizeof(*g));
204 return g;
207 static int commit_graph_compatible(struct repository *r)
209 if (!r->gitdir)
210 return 0;
212 if (replace_refs_enabled(r)) {
213 prepare_replace_object(r);
214 if (hashmap_get_size(&r->objects->replace_map->map))
215 return 0;
218 prepare_commit_graft(r);
219 if (r->parsed_objects &&
220 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
221 return 0;
222 if (is_repository_shallow(r))
223 return 0;
225 return 1;
228 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
230 *fd = git_open(graph_file);
231 if (*fd < 0)
232 return 0;
233 if (fstat(*fd, st)) {
234 close(*fd);
235 return 0;
237 return 1;
240 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
241 int fd, struct stat *st,
242 struct object_directory *odb)
244 void *graph_map;
245 size_t graph_size;
246 struct commit_graph *ret;
248 graph_size = xsize_t(st->st_size);
250 if (graph_size < GRAPH_MIN_SIZE) {
251 close(fd);
252 error(_("commit-graph file is too small"));
253 return NULL;
255 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
256 close(fd);
257 prepare_repo_settings(r);
258 ret = parse_commit_graph(&r->settings, graph_map, graph_size);
260 if (ret)
261 ret->odb = odb;
262 else
263 munmap(graph_map, graph_size);
265 return ret;
268 static int verify_commit_graph_lite(struct commit_graph *g)
271 * Basic validation shared between parse_commit_graph()
272 * which'll be called every time the graph is used, and the
273 * much more expensive verify_commit_graph() used by
274 * "commit-graph verify".
276 * There should only be very basic checks here to ensure that
277 * we don't e.g. segfault in fill_commit_in_graph(), but
278 * because this is a very hot codepath nothing that e.g. loops
279 * over g->num_commits, or runs a checksum on the commit-graph
280 * itself.
282 if (!g->chunk_oid_fanout) {
283 error("commit-graph is missing the OID Fanout chunk");
284 return 1;
286 if (!g->chunk_oid_lookup) {
287 error("commit-graph is missing the OID Lookup chunk");
288 return 1;
290 if (!g->chunk_commit_data) {
291 error("commit-graph is missing the Commit Data chunk");
292 return 1;
295 return 0;
298 static int graph_read_oid_lookup(const unsigned char *chunk_start,
299 size_t chunk_size, void *data)
301 struct commit_graph *g = data;
302 g->chunk_oid_lookup = chunk_start;
303 g->num_commits = chunk_size / g->hash_len;
304 return 0;
307 static int graph_read_bloom_data(const unsigned char *chunk_start,
308 size_t chunk_size, void *data)
310 struct commit_graph *g = data;
311 uint32_t hash_version;
312 g->chunk_bloom_data = chunk_start;
313 hash_version = get_be32(chunk_start);
315 if (hash_version != 1)
316 return 0;
318 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
319 g->bloom_filter_settings->hash_version = hash_version;
320 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
321 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
322 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
324 return 0;
327 struct commit_graph *parse_commit_graph(struct repo_settings *s,
328 void *graph_map, size_t graph_size)
330 const unsigned char *data;
331 struct commit_graph *graph;
332 uint32_t graph_signature;
333 unsigned char graph_version, hash_version;
334 struct chunkfile *cf = NULL;
336 if (!graph_map)
337 return NULL;
339 if (graph_size < GRAPH_MIN_SIZE)
340 return NULL;
342 data = (const unsigned char *)graph_map;
344 graph_signature = get_be32(data);
345 if (graph_signature != GRAPH_SIGNATURE) {
346 error(_("commit-graph signature %X does not match signature %X"),
347 graph_signature, GRAPH_SIGNATURE);
348 return NULL;
351 graph_version = *(unsigned char*)(data + 4);
352 if (graph_version != GRAPH_VERSION) {
353 error(_("commit-graph version %X does not match version %X"),
354 graph_version, GRAPH_VERSION);
355 return NULL;
358 hash_version = *(unsigned char*)(data + 5);
359 if (hash_version != oid_version(the_hash_algo)) {
360 error(_("commit-graph hash version %X does not match version %X"),
361 hash_version, oid_version(the_hash_algo));
362 return NULL;
365 graph = alloc_commit_graph();
367 graph->hash_len = the_hash_algo->rawsz;
368 graph->num_chunks = *(unsigned char*)(data + 6);
369 graph->data = graph_map;
370 graph->data_len = graph_size;
372 if (graph_size < GRAPH_HEADER_SIZE +
373 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
374 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
375 error(_("commit-graph file is too small to hold %u chunks"),
376 graph->num_chunks);
377 free(graph);
378 return NULL;
381 cf = init_chunkfile(NULL);
383 if (read_table_of_contents(cf, graph->data, graph_size,
384 GRAPH_HEADER_SIZE, graph->num_chunks))
385 goto free_and_return;
387 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
388 (const unsigned char **)&graph->chunk_oid_fanout);
389 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
390 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
391 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
392 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
394 if (s->commit_graph_generation_version >= 2) {
395 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
396 &graph->chunk_generation_data);
397 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
398 &graph->chunk_generation_data_overflow);
400 if (graph->chunk_generation_data)
401 graph->read_generation_data = 1;
404 if (s->commit_graph_read_changed_paths) {
405 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
406 &graph->chunk_bloom_indexes);
407 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
408 graph_read_bloom_data, graph);
411 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
412 init_bloom_filters();
413 } else {
414 /* We need both the bloom chunks to exist together. Else ignore the data */
415 graph->chunk_bloom_indexes = NULL;
416 graph->chunk_bloom_data = NULL;
417 FREE_AND_NULL(graph->bloom_filter_settings);
420 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
422 if (verify_commit_graph_lite(graph))
423 goto free_and_return;
425 free_chunkfile(cf);
426 return graph;
428 free_and_return:
429 free_chunkfile(cf);
430 free(graph->bloom_filter_settings);
431 free(graph);
432 return NULL;
435 static struct commit_graph *load_commit_graph_one(struct repository *r,
436 const char *graph_file,
437 struct object_directory *odb)
440 struct stat st;
441 int fd;
442 struct commit_graph *g;
443 int open_ok = open_commit_graph(graph_file, &fd, &st);
445 if (!open_ok)
446 return NULL;
448 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
450 if (g)
451 g->filename = xstrdup(graph_file);
453 return g;
456 static struct commit_graph *load_commit_graph_v1(struct repository *r,
457 struct object_directory *odb)
459 char *graph_name = get_commit_graph_filename(odb);
460 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
461 free(graph_name);
463 return g;
466 static int add_graph_to_chain(struct commit_graph *g,
467 struct commit_graph *chain,
468 struct object_id *oids,
469 int n)
471 struct commit_graph *cur_g = chain;
473 if (n && !g->chunk_base_graphs) {
474 warning(_("commit-graph has no base graphs chunk"));
475 return 0;
478 while (n) {
479 n--;
481 if (!cur_g ||
482 !oideq(&oids[n], &cur_g->oid) ||
483 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) {
484 warning(_("commit-graph chain does not match"));
485 return 0;
488 cur_g = cur_g->base_graph;
491 g->base_graph = chain;
493 if (chain) {
494 if (unsigned_add_overflows(chain->num_commits,
495 chain->num_commits_in_base)) {
496 warning(_("commit count in base graph too high: %"PRIuMAX),
497 (uintmax_t)chain->num_commits_in_base);
498 return 0;
500 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
503 return 1;
506 static struct commit_graph *load_commit_graph_chain(struct repository *r,
507 struct object_directory *odb)
509 struct commit_graph *graph_chain = NULL;
510 struct strbuf line = STRBUF_INIT;
511 struct stat st;
512 struct object_id *oids;
513 int i = 0, valid = 1, count;
514 char *chain_name = get_commit_graph_chain_filename(odb);
515 FILE *fp;
516 int stat_res;
518 fp = fopen(chain_name, "r");
519 stat_res = stat(chain_name, &st);
520 free(chain_name);
522 if (!fp)
523 return NULL;
524 if (stat_res ||
525 st.st_size <= the_hash_algo->hexsz) {
526 fclose(fp);
527 return NULL;
530 count = st.st_size / (the_hash_algo->hexsz + 1);
531 CALLOC_ARRAY(oids, count);
533 prepare_alt_odb(r);
535 for (i = 0; i < count; i++) {
536 struct object_directory *odb;
538 if (strbuf_getline_lf(&line, fp) == EOF)
539 break;
541 if (get_oid_hex(line.buf, &oids[i])) {
542 warning(_("invalid commit-graph chain: line '%s' not a hash"),
543 line.buf);
544 valid = 0;
545 break;
548 valid = 0;
549 for (odb = r->objects->odb; odb; odb = odb->next) {
550 char *graph_name = get_split_graph_filename(odb, line.buf);
551 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
553 free(graph_name);
555 if (g) {
556 if (add_graph_to_chain(g, graph_chain, oids, i)) {
557 graph_chain = g;
558 valid = 1;
561 break;
565 if (!valid) {
566 warning(_("unable to find all commit-graph files"));
567 break;
571 free(oids);
572 fclose(fp);
573 strbuf_release(&line);
575 return graph_chain;
579 * returns 1 if and only if all graphs in the chain have
580 * corrected commit dates stored in the generation_data chunk.
582 static int validate_mixed_generation_chain(struct commit_graph *g)
584 int read_generation_data = 1;
585 struct commit_graph *p = g;
587 while (read_generation_data && p) {
588 read_generation_data = p->read_generation_data;
589 p = p->base_graph;
592 if (read_generation_data)
593 return 1;
595 while (g) {
596 g->read_generation_data = 0;
597 g = g->base_graph;
600 return 0;
603 struct commit_graph *read_commit_graph_one(struct repository *r,
604 struct object_directory *odb)
606 struct commit_graph *g = load_commit_graph_v1(r, odb);
608 if (!g)
609 g = load_commit_graph_chain(r, odb);
611 validate_mixed_generation_chain(g);
613 return g;
616 static void prepare_commit_graph_one(struct repository *r,
617 struct object_directory *odb)
620 if (r->objects->commit_graph)
621 return;
623 r->objects->commit_graph = read_commit_graph_one(r, odb);
627 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
629 * On the first invocation, this function attempts to load the commit
630 * graph if the_repository is configured to have one.
632 static int prepare_commit_graph(struct repository *r)
634 struct object_directory *odb;
637 * Early return if there is no git dir or if the commit graph is
638 * disabled.
640 * This must come before the "already attempted?" check below, because
641 * we want to disable even an already-loaded graph file.
643 if (!r->gitdir || r->commit_graph_disabled)
644 return 0;
646 if (r->objects->commit_graph_attempted)
647 return !!r->objects->commit_graph;
648 r->objects->commit_graph_attempted = 1;
650 prepare_repo_settings(r);
652 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
653 r->settings.core_commit_graph != 1)
655 * This repository is not configured to use commit graphs, so
656 * do not load one. (But report commit_graph_attempted anyway
657 * so that commit graph loading is not attempted again for this
658 * repository.)
660 return 0;
662 if (!commit_graph_compatible(r))
663 return 0;
665 prepare_alt_odb(r);
666 for (odb = r->objects->odb;
667 !r->objects->commit_graph && odb;
668 odb = odb->next)
669 prepare_commit_graph_one(r, odb);
670 return !!r->objects->commit_graph;
673 int generation_numbers_enabled(struct repository *r)
675 uint32_t first_generation;
676 struct commit_graph *g;
677 if (!prepare_commit_graph(r))
678 return 0;
680 g = r->objects->commit_graph;
682 if (!g->num_commits)
683 return 0;
685 first_generation = get_be32(g->chunk_commit_data +
686 g->hash_len + 8) >> 2;
688 return !!first_generation;
691 int corrected_commit_dates_enabled(struct repository *r)
693 struct commit_graph *g;
694 if (!prepare_commit_graph(r))
695 return 0;
697 g = r->objects->commit_graph;
699 if (!g->num_commits)
700 return 0;
702 return g->read_generation_data;
705 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
707 struct commit_graph *g = r->objects->commit_graph;
708 while (g) {
709 if (g->bloom_filter_settings)
710 return g->bloom_filter_settings;
711 g = g->base_graph;
713 return NULL;
716 static void close_commit_graph_one(struct commit_graph *g)
718 if (!g)
719 return;
721 clear_commit_graph_data_slab(&commit_graph_data_slab);
722 close_commit_graph_one(g->base_graph);
723 free_commit_graph(g);
726 void close_commit_graph(struct raw_object_store *o)
728 close_commit_graph_one(o->commit_graph);
729 o->commit_graph = NULL;
732 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
734 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
735 g->chunk_oid_lookup, g->hash_len, pos);
738 static void load_oid_from_graph(struct commit_graph *g,
739 uint32_t pos,
740 struct object_id *oid)
742 uint32_t lex_index;
744 while (g && pos < g->num_commits_in_base)
745 g = g->base_graph;
747 if (!g)
748 BUG("NULL commit-graph");
750 if (pos >= g->num_commits + g->num_commits_in_base)
751 die(_("invalid commit position. commit-graph is likely corrupt"));
753 lex_index = pos - g->num_commits_in_base;
755 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index));
758 static struct commit_list **insert_parent_or_die(struct repository *r,
759 struct commit_graph *g,
760 uint32_t pos,
761 struct commit_list **pptr)
763 struct commit *c;
764 struct object_id oid;
766 if (pos >= g->num_commits + g->num_commits_in_base)
767 die("invalid parent position %"PRIu32, pos);
769 load_oid_from_graph(g, pos, &oid);
770 c = lookup_commit(r, &oid);
771 if (!c)
772 die(_("could not find commit %s"), oid_to_hex(&oid));
773 commit_graph_data_at(c)->graph_pos = pos;
774 return &commit_list_insert(c, pptr)->next;
777 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
779 const unsigned char *commit_data;
780 struct commit_graph_data *graph_data;
781 uint32_t lex_index, offset_pos;
782 uint64_t date_high, date_low, offset;
784 while (pos < g->num_commits_in_base)
785 g = g->base_graph;
787 if (pos >= g->num_commits + g->num_commits_in_base)
788 die(_("invalid commit position. commit-graph is likely corrupt"));
790 lex_index = pos - g->num_commits_in_base;
791 commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index);
793 graph_data = commit_graph_data_at(item);
794 graph_data->graph_pos = pos;
796 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
797 date_low = get_be32(commit_data + g->hash_len + 12);
798 item->date = (timestamp_t)((date_high << 32) | date_low);
800 if (g->read_generation_data) {
801 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
803 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
804 if (!g->chunk_generation_data_overflow)
805 die(_("commit-graph requires overflow generation data but has none"));
807 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
808 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + st_mult(8, offset_pos));
809 } else
810 graph_data->generation = item->date + offset;
811 } else
812 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
814 if (g->topo_levels)
815 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
818 static inline void set_commit_tree(struct commit *c, struct tree *t)
820 c->maybe_tree = t;
823 static int fill_commit_in_graph(struct repository *r,
824 struct commit *item,
825 struct commit_graph *g, uint32_t pos)
827 uint32_t edge_value;
828 uint32_t *parent_data_ptr;
829 struct commit_list **pptr;
830 const unsigned char *commit_data;
831 uint32_t lex_index;
833 while (pos < g->num_commits_in_base)
834 g = g->base_graph;
836 fill_commit_graph_info(item, g, pos);
838 lex_index = pos - g->num_commits_in_base;
839 commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index);
841 item->object.parsed = 1;
843 set_commit_tree(item, NULL);
845 pptr = &item->parents;
847 edge_value = get_be32(commit_data + g->hash_len);
848 if (edge_value == GRAPH_PARENT_NONE)
849 return 1;
850 pptr = insert_parent_or_die(r, g, edge_value, pptr);
852 edge_value = get_be32(commit_data + g->hash_len + 4);
853 if (edge_value == GRAPH_PARENT_NONE)
854 return 1;
855 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
856 pptr = insert_parent_or_die(r, g, edge_value, pptr);
857 return 1;
860 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
861 st_mult(4, edge_value & GRAPH_EDGE_LAST_MASK));
862 do {
863 edge_value = get_be32(parent_data_ptr);
864 pptr = insert_parent_or_die(r, g,
865 edge_value & GRAPH_EDGE_LAST_MASK,
866 pptr);
867 parent_data_ptr++;
868 } while (!(edge_value & GRAPH_LAST_EDGE));
870 return 1;
873 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
875 struct commit_graph *cur_g = g;
876 uint32_t lex_index;
878 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
879 cur_g = cur_g->base_graph;
881 if (cur_g) {
882 *pos = lex_index + cur_g->num_commits_in_base;
883 return 1;
886 return 0;
889 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
891 uint32_t graph_pos = commit_graph_position(item);
892 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
893 *pos = graph_pos;
894 return 1;
895 } else {
896 return search_commit_pos_in_graph(&item->object.oid, g, pos);
900 int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
901 uint32_t *pos)
903 if (!prepare_commit_graph(r))
904 return 0;
905 return find_commit_pos_in_graph(c, r->objects->commit_graph, pos);
908 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
910 struct commit *commit;
911 uint32_t pos;
913 if (!prepare_commit_graph(repo))
914 return NULL;
915 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
916 return NULL;
917 if (!has_object(repo, id, 0))
918 return NULL;
920 commit = lookup_commit(repo, id);
921 if (!commit)
922 return NULL;
923 if (commit->object.parsed)
924 return commit;
926 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
927 return NULL;
929 return commit;
932 static int parse_commit_in_graph_one(struct repository *r,
933 struct commit_graph *g,
934 struct commit *item)
936 uint32_t pos;
938 if (item->object.parsed)
939 return 1;
941 if (find_commit_pos_in_graph(item, g, &pos))
942 return fill_commit_in_graph(r, item, g, pos);
944 return 0;
947 int parse_commit_in_graph(struct repository *r, struct commit *item)
949 static int checked_env = 0;
951 if (!checked_env &&
952 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
953 die("dying as requested by the '%s' variable on commit-graph parse!",
954 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
955 checked_env = 1;
957 if (!prepare_commit_graph(r))
958 return 0;
959 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
962 void load_commit_graph_info(struct repository *r, struct commit *item)
964 uint32_t pos;
965 if (repo_find_commit_pos_in_graph(r, item, &pos))
966 fill_commit_graph_info(item, r->objects->commit_graph, pos);
969 static struct tree *load_tree_for_commit(struct repository *r,
970 struct commit_graph *g,
971 struct commit *c)
973 struct object_id oid;
974 const unsigned char *commit_data;
975 uint32_t graph_pos = commit_graph_position(c);
977 while (graph_pos < g->num_commits_in_base)
978 g = g->base_graph;
980 commit_data = g->chunk_commit_data +
981 st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base);
983 oidread(&oid, commit_data);
984 set_commit_tree(c, lookup_tree(r, &oid));
986 return c->maybe_tree;
989 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
990 struct commit_graph *g,
991 const struct commit *c)
993 if (c->maybe_tree)
994 return c->maybe_tree;
995 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
996 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
998 return load_tree_for_commit(r, g, (struct commit *)c);
1001 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1003 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1006 struct packed_commit_list {
1007 struct commit **list;
1008 size_t nr;
1009 size_t alloc;
1012 struct write_commit_graph_context {
1013 struct repository *r;
1014 struct object_directory *odb;
1015 char *graph_name;
1016 struct oid_array oids;
1017 struct packed_commit_list commits;
1018 int num_extra_edges;
1019 int num_generation_data_overflows;
1020 unsigned long approx_nr_objects;
1021 struct progress *progress;
1022 int progress_done;
1023 uint64_t progress_cnt;
1025 char *base_graph_name;
1026 int num_commit_graphs_before;
1027 int num_commit_graphs_after;
1028 char **commit_graph_filenames_before;
1029 char **commit_graph_filenames_after;
1030 char **commit_graph_hash_after;
1031 uint32_t new_num_commits_in_base;
1032 struct commit_graph *new_base_graph;
1034 unsigned append:1,
1035 report_progress:1,
1036 split:1,
1037 changed_paths:1,
1038 order_by_pack:1,
1039 write_generation_data:1,
1040 trust_generation_numbers:1;
1042 struct topo_level_slab *topo_levels;
1043 const struct commit_graph_opts *opts;
1044 size_t total_bloom_filter_data_size;
1045 const struct bloom_filter_settings *bloom_settings;
1047 int count_bloom_filter_computed;
1048 int count_bloom_filter_not_computed;
1049 int count_bloom_filter_trunc_empty;
1050 int count_bloom_filter_trunc_large;
1053 static int write_graph_chunk_fanout(struct hashfile *f,
1054 void *data)
1056 struct write_commit_graph_context *ctx = data;
1057 int i, count = 0;
1058 struct commit **list = ctx->commits.list;
1061 * Write the first-level table (the list is sorted,
1062 * but we use a 256-entry lookup to be able to avoid
1063 * having to do eight extra binary search iterations).
1065 for (i = 0; i < 256; i++) {
1066 while (count < ctx->commits.nr) {
1067 if ((*list)->object.oid.hash[0] != i)
1068 break;
1069 display_progress(ctx->progress, ++ctx->progress_cnt);
1070 count++;
1071 list++;
1074 hashwrite_be32(f, count);
1077 return 0;
1080 static int write_graph_chunk_oids(struct hashfile *f,
1081 void *data)
1083 struct write_commit_graph_context *ctx = data;
1084 struct commit **list = ctx->commits.list;
1085 int count;
1086 for (count = 0; count < ctx->commits.nr; count++, list++) {
1087 display_progress(ctx->progress, ++ctx->progress_cnt);
1088 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1091 return 0;
1094 static const struct object_id *commit_to_oid(size_t index, const void *table)
1096 const struct commit * const *commits = table;
1097 return &commits[index]->object.oid;
1100 static int write_graph_chunk_data(struct hashfile *f,
1101 void *data)
1103 struct write_commit_graph_context *ctx = data;
1104 struct commit **list = ctx->commits.list;
1105 struct commit **last = ctx->commits.list + ctx->commits.nr;
1106 uint32_t num_extra_edges = 0;
1108 while (list < last) {
1109 struct commit_list *parent;
1110 struct object_id *tree;
1111 int edge_value;
1112 uint32_t packedDate[2];
1113 display_progress(ctx->progress, ++ctx->progress_cnt);
1115 if (repo_parse_commit_no_graph(ctx->r, *list))
1116 die(_("unable to parse commit %s"),
1117 oid_to_hex(&(*list)->object.oid));
1118 tree = get_commit_tree_oid(*list);
1119 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1121 parent = (*list)->parents;
1123 if (!parent)
1124 edge_value = GRAPH_PARENT_NONE;
1125 else {
1126 edge_value = oid_pos(&parent->item->object.oid,
1127 ctx->commits.list,
1128 ctx->commits.nr,
1129 commit_to_oid);
1131 if (edge_value >= 0)
1132 edge_value += ctx->new_num_commits_in_base;
1133 else if (ctx->new_base_graph) {
1134 uint32_t pos;
1135 if (find_commit_pos_in_graph(parent->item,
1136 ctx->new_base_graph,
1137 &pos))
1138 edge_value = pos;
1141 if (edge_value < 0)
1142 BUG("missing parent %s for commit %s",
1143 oid_to_hex(&parent->item->object.oid),
1144 oid_to_hex(&(*list)->object.oid));
1147 hashwrite_be32(f, edge_value);
1149 if (parent)
1150 parent = parent->next;
1152 if (!parent)
1153 edge_value = GRAPH_PARENT_NONE;
1154 else if (parent->next)
1155 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1156 else {
1157 edge_value = oid_pos(&parent->item->object.oid,
1158 ctx->commits.list,
1159 ctx->commits.nr,
1160 commit_to_oid);
1162 if (edge_value >= 0)
1163 edge_value += ctx->new_num_commits_in_base;
1164 else if (ctx->new_base_graph) {
1165 uint32_t pos;
1166 if (find_commit_pos_in_graph(parent->item,
1167 ctx->new_base_graph,
1168 &pos))
1169 edge_value = pos;
1172 if (edge_value < 0)
1173 BUG("missing parent %s for commit %s",
1174 oid_to_hex(&parent->item->object.oid),
1175 oid_to_hex(&(*list)->object.oid));
1178 hashwrite_be32(f, edge_value);
1180 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1181 do {
1182 num_extra_edges++;
1183 parent = parent->next;
1184 } while (parent);
1187 if (sizeof((*list)->date) > 4)
1188 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1189 else
1190 packedDate[0] = 0;
1192 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1194 packedDate[1] = htonl((*list)->date);
1195 hashwrite(f, packedDate, 8);
1197 list++;
1200 return 0;
1203 static int write_graph_chunk_generation_data(struct hashfile *f,
1204 void *data)
1206 struct write_commit_graph_context *ctx = data;
1207 int i, num_generation_data_overflows = 0;
1209 for (i = 0; i < ctx->commits.nr; i++) {
1210 struct commit *c = ctx->commits.list[i];
1211 timestamp_t offset;
1212 repo_parse_commit(ctx->r, c);
1213 offset = commit_graph_data_at(c)->generation - c->date;
1214 display_progress(ctx->progress, ++ctx->progress_cnt);
1216 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1217 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1218 num_generation_data_overflows++;
1221 hashwrite_be32(f, offset);
1224 return 0;
1227 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1228 void *data)
1230 struct write_commit_graph_context *ctx = data;
1231 int i;
1232 for (i = 0; i < ctx->commits.nr; i++) {
1233 struct commit *c = ctx->commits.list[i];
1234 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1235 display_progress(ctx->progress, ++ctx->progress_cnt);
1237 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1238 hashwrite_be32(f, offset >> 32);
1239 hashwrite_be32(f, (uint32_t) offset);
1243 return 0;
1246 static int write_graph_chunk_extra_edges(struct hashfile *f,
1247 void *data)
1249 struct write_commit_graph_context *ctx = data;
1250 struct commit **list = ctx->commits.list;
1251 struct commit **last = ctx->commits.list + ctx->commits.nr;
1252 struct commit_list *parent;
1254 while (list < last) {
1255 int num_parents = 0;
1257 display_progress(ctx->progress, ++ctx->progress_cnt);
1259 for (parent = (*list)->parents; num_parents < 3 && parent;
1260 parent = parent->next)
1261 num_parents++;
1263 if (num_parents <= 2) {
1264 list++;
1265 continue;
1268 /* Since num_parents > 2, this initializer is safe. */
1269 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1270 int edge_value = oid_pos(&parent->item->object.oid,
1271 ctx->commits.list,
1272 ctx->commits.nr,
1273 commit_to_oid);
1275 if (edge_value >= 0)
1276 edge_value += ctx->new_num_commits_in_base;
1277 else if (ctx->new_base_graph) {
1278 uint32_t pos;
1279 if (find_commit_pos_in_graph(parent->item,
1280 ctx->new_base_graph,
1281 &pos))
1282 edge_value = pos;
1285 if (edge_value < 0)
1286 BUG("missing parent %s for commit %s",
1287 oid_to_hex(&parent->item->object.oid),
1288 oid_to_hex(&(*list)->object.oid));
1289 else if (!parent->next)
1290 edge_value |= GRAPH_LAST_EDGE;
1292 hashwrite_be32(f, edge_value);
1295 list++;
1298 return 0;
1301 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1302 void *data)
1304 struct write_commit_graph_context *ctx = data;
1305 struct commit **list = ctx->commits.list;
1306 struct commit **last = ctx->commits.list + ctx->commits.nr;
1307 uint32_t cur_pos = 0;
1309 while (list < last) {
1310 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1311 size_t len = filter ? filter->len : 0;
1312 cur_pos += len;
1313 display_progress(ctx->progress, ++ctx->progress_cnt);
1314 hashwrite_be32(f, cur_pos);
1315 list++;
1318 return 0;
1321 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1323 struct json_writer jw = JSON_WRITER_INIT;
1325 jw_object_begin(&jw, 0);
1326 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1327 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1328 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1329 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1330 jw_end(&jw);
1332 trace2_data_json("bloom", ctx->r, "settings", &jw);
1334 jw_release(&jw);
1337 static int write_graph_chunk_bloom_data(struct hashfile *f,
1338 void *data)
1340 struct write_commit_graph_context *ctx = data;
1341 struct commit **list = ctx->commits.list;
1342 struct commit **last = ctx->commits.list + ctx->commits.nr;
1344 trace2_bloom_filter_settings(ctx);
1346 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1347 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1348 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1350 while (list < last) {
1351 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1352 size_t len = filter ? filter->len : 0;
1354 display_progress(ctx->progress, ++ctx->progress_cnt);
1355 if (len)
1356 hashwrite(f, filter->data, len * sizeof(unsigned char));
1357 list++;
1360 return 0;
1363 static int add_packed_commits(const struct object_id *oid,
1364 struct packed_git *pack,
1365 uint32_t pos,
1366 void *data)
1368 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1369 enum object_type type;
1370 off_t offset = nth_packed_object_offset(pack, pos);
1371 struct object_info oi = OBJECT_INFO_INIT;
1373 if (ctx->progress)
1374 display_progress(ctx->progress, ++ctx->progress_done);
1376 oi.typep = &type;
1377 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1378 die(_("unable to get type of object %s"), oid_to_hex(oid));
1380 if (type != OBJ_COMMIT)
1381 return 0;
1383 oid_array_append(&ctx->oids, oid);
1384 set_commit_pos(ctx->r, oid);
1386 return 0;
1389 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1391 struct commit_list *parent;
1392 for (parent = commit->parents; parent; parent = parent->next) {
1393 if (!(parent->item->object.flags & REACHABLE)) {
1394 oid_array_append(&ctx->oids, &parent->item->object.oid);
1395 parent->item->object.flags |= REACHABLE;
1400 static void close_reachable(struct write_commit_graph_context *ctx)
1402 int i;
1403 struct commit *commit;
1404 enum commit_graph_split_flags flags = ctx->opts ?
1405 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1407 if (ctx->report_progress)
1408 ctx->progress = start_delayed_progress(
1409 _("Loading known commits in commit graph"),
1410 ctx->oids.nr);
1411 for (i = 0; i < ctx->oids.nr; i++) {
1412 display_progress(ctx->progress, i + 1);
1413 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1414 if (commit)
1415 commit->object.flags |= REACHABLE;
1417 stop_progress(&ctx->progress);
1420 * As this loop runs, ctx->oids.nr may grow, but not more
1421 * than the number of missing commits in the reachable
1422 * closure.
1424 if (ctx->report_progress)
1425 ctx->progress = start_delayed_progress(
1426 _("Expanding reachable commits in commit graph"),
1428 for (i = 0; i < ctx->oids.nr; i++) {
1429 display_progress(ctx->progress, i + 1);
1430 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1432 if (!commit)
1433 continue;
1434 if (ctx->split) {
1435 if ((!repo_parse_commit(ctx->r, commit) &&
1436 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1437 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1438 add_missing_parents(ctx, commit);
1439 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1440 add_missing_parents(ctx, commit);
1442 stop_progress(&ctx->progress);
1444 if (ctx->report_progress)
1445 ctx->progress = start_delayed_progress(
1446 _("Clearing commit marks in commit graph"),
1447 ctx->oids.nr);
1448 for (i = 0; i < ctx->oids.nr; i++) {
1449 display_progress(ctx->progress, i + 1);
1450 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1452 if (commit)
1453 commit->object.flags &= ~REACHABLE;
1455 stop_progress(&ctx->progress);
1458 struct compute_generation_info {
1459 struct repository *r;
1460 struct packed_commit_list *commits;
1461 struct progress *progress;
1462 int progress_cnt;
1464 timestamp_t (*get_generation)(struct commit *c, void *data);
1465 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1466 void *data;
1469 static timestamp_t compute_generation_from_max(struct commit *c,
1470 timestamp_t max_gen,
1471 int generation_version)
1473 switch (generation_version) {
1474 case 1: /* topological levels */
1475 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1476 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1477 return max_gen + 1;
1479 case 2: /* corrected commit date */
1480 if (c->date && c->date > max_gen)
1481 max_gen = c->date - 1;
1482 return max_gen + 1;
1484 default:
1485 BUG("attempting unimplemented version");
1489 static void compute_reachable_generation_numbers(
1490 struct compute_generation_info *info,
1491 int generation_version)
1493 int i;
1494 struct commit_list *list = NULL;
1496 for (i = 0; i < info->commits->nr; i++) {
1497 struct commit *c = info->commits->list[i];
1498 timestamp_t gen;
1499 repo_parse_commit(info->r, c);
1500 gen = info->get_generation(c, info->data);
1501 display_progress(info->progress, info->progress_cnt + 1);
1503 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1504 continue;
1506 commit_list_insert(c, &list);
1507 while (list) {
1508 struct commit *current = list->item;
1509 struct commit_list *parent;
1510 int all_parents_computed = 1;
1511 uint32_t max_gen = 0;
1513 for (parent = current->parents; parent; parent = parent->next) {
1514 repo_parse_commit(info->r, parent->item);
1515 gen = info->get_generation(parent->item, info->data);
1517 if (gen == GENERATION_NUMBER_ZERO) {
1518 all_parents_computed = 0;
1519 commit_list_insert(parent->item, &list);
1520 break;
1523 if (gen > max_gen)
1524 max_gen = gen;
1527 if (all_parents_computed) {
1528 pop_commit(&list);
1529 gen = compute_generation_from_max(
1530 current, max_gen,
1531 generation_version);
1532 info->set_generation(current, gen, info->data);
1538 static timestamp_t get_topo_level(struct commit *c, void *data)
1540 struct write_commit_graph_context *ctx = data;
1541 return *topo_level_slab_at(ctx->topo_levels, c);
1544 static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1546 struct write_commit_graph_context *ctx = data;
1547 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1550 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1552 struct compute_generation_info info = {
1553 .r = ctx->r,
1554 .commits = &ctx->commits,
1555 .get_generation = get_topo_level,
1556 .set_generation = set_topo_level,
1557 .data = ctx,
1560 if (ctx->report_progress)
1561 info.progress = ctx->progress
1562 = start_delayed_progress(
1563 _("Computing commit graph topological levels"),
1564 ctx->commits.nr);
1566 compute_reachable_generation_numbers(&info, 1);
1568 stop_progress(&ctx->progress);
1571 static timestamp_t get_generation_from_graph_data(struct commit *c, void *data)
1573 return commit_graph_data_at(c)->generation;
1576 static void set_generation_v2(struct commit *c, timestamp_t t, void *data)
1578 struct commit_graph_data *g = commit_graph_data_at(c);
1579 g->generation = t;
1582 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1584 int i;
1585 struct compute_generation_info info = {
1586 .r = ctx->r,
1587 .commits = &ctx->commits,
1588 .get_generation = get_generation_from_graph_data,
1589 .set_generation = set_generation_v2,
1590 .data = ctx,
1593 if (ctx->report_progress)
1594 info.progress = ctx->progress
1595 = start_delayed_progress(
1596 _("Computing commit graph generation numbers"),
1597 ctx->commits.nr);
1599 if (!ctx->trust_generation_numbers) {
1600 for (i = 0; i < ctx->commits.nr; i++) {
1601 struct commit *c = ctx->commits.list[i];
1602 repo_parse_commit(ctx->r, c);
1603 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1607 compute_reachable_generation_numbers(&info, 2);
1609 for (i = 0; i < ctx->commits.nr; i++) {
1610 struct commit *c = ctx->commits.list[i];
1611 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1612 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1613 ctx->num_generation_data_overflows++;
1615 stop_progress(&ctx->progress);
1618 static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1619 void *data)
1621 commit_graph_data_at(c)->generation = t;
1625 * After this method, all commits reachable from those in the given
1626 * list will have non-zero, non-infinite generation numbers.
1628 void ensure_generations_valid(struct repository *r,
1629 struct commit **commits, size_t nr)
1631 int generation_version = get_configured_generation_version(r);
1632 struct packed_commit_list list = {
1633 .list = commits,
1634 .alloc = nr,
1635 .nr = nr,
1637 struct compute_generation_info info = {
1638 .r = r,
1639 .commits = &list,
1640 .get_generation = get_generation_from_graph_data,
1641 .set_generation = set_generation_in_graph_data,
1644 compute_reachable_generation_numbers(&info, generation_version);
1647 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1649 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1650 ctx->count_bloom_filter_computed);
1651 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1652 ctx->count_bloom_filter_not_computed);
1653 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1654 ctx->count_bloom_filter_trunc_empty);
1655 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1656 ctx->count_bloom_filter_trunc_large);
1659 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1661 int i;
1662 struct progress *progress = NULL;
1663 struct commit **sorted_commits;
1664 int max_new_filters;
1666 init_bloom_filters();
1668 if (ctx->report_progress)
1669 progress = start_delayed_progress(
1670 _("Computing commit changed paths Bloom filters"),
1671 ctx->commits.nr);
1673 DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1675 if (ctx->order_by_pack)
1676 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1677 else
1678 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1680 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1681 ctx->opts->max_new_filters : ctx->commits.nr;
1683 for (i = 0; i < ctx->commits.nr; i++) {
1684 enum bloom_filter_computed computed = 0;
1685 struct commit *c = sorted_commits[i];
1686 struct bloom_filter *filter = get_or_compute_bloom_filter(
1687 ctx->r,
1689 ctx->count_bloom_filter_computed < max_new_filters,
1690 ctx->bloom_settings,
1691 &computed);
1692 if (computed & BLOOM_COMPUTED) {
1693 ctx->count_bloom_filter_computed++;
1694 if (computed & BLOOM_TRUNC_EMPTY)
1695 ctx->count_bloom_filter_trunc_empty++;
1696 if (computed & BLOOM_TRUNC_LARGE)
1697 ctx->count_bloom_filter_trunc_large++;
1698 } else if (computed & BLOOM_NOT_COMPUTED)
1699 ctx->count_bloom_filter_not_computed++;
1700 ctx->total_bloom_filter_data_size += filter
1701 ? sizeof(unsigned char) * filter->len : 0;
1702 display_progress(progress, i + 1);
1705 if (trace2_is_enabled())
1706 trace2_bloom_filter_write_statistics(ctx);
1708 free(sorted_commits);
1709 stop_progress(&progress);
1712 struct refs_cb_data {
1713 struct oidset *commits;
1714 struct progress *progress;
1717 static int add_ref_to_set(const char *refname UNUSED,
1718 const struct object_id *oid,
1719 int flags UNUSED, void *cb_data)
1721 struct object_id peeled;
1722 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1724 if (!peel_iterated_oid(oid, &peeled))
1725 oid = &peeled;
1726 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1727 oidset_insert(data->commits, oid);
1729 display_progress(data->progress, oidset_size(data->commits));
1731 return 0;
1734 int write_commit_graph_reachable(struct object_directory *odb,
1735 enum commit_graph_write_flags flags,
1736 const struct commit_graph_opts *opts)
1738 struct oidset commits = OIDSET_INIT;
1739 struct refs_cb_data data;
1740 int result;
1742 memset(&data, 0, sizeof(data));
1743 data.commits = &commits;
1744 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1745 data.progress = start_delayed_progress(
1746 _("Collecting referenced commits"), 0);
1748 for_each_ref(add_ref_to_set, &data);
1750 stop_progress(&data.progress);
1752 result = write_commit_graph(odb, NULL, &commits,
1753 flags, opts);
1755 oidset_clear(&commits);
1756 return result;
1759 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1760 const struct string_list *pack_indexes)
1762 uint32_t i;
1763 struct strbuf progress_title = STRBUF_INIT;
1764 struct strbuf packname = STRBUF_INIT;
1765 int dirlen;
1766 int ret = 0;
1768 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1769 dirlen = packname.len;
1770 if (ctx->report_progress) {
1771 strbuf_addf(&progress_title,
1772 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1773 "Finding commits for commit graph in %"PRIuMAX" packs",
1774 pack_indexes->nr),
1775 (uintmax_t)pack_indexes->nr);
1776 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1777 ctx->progress_done = 0;
1779 for (i = 0; i < pack_indexes->nr; i++) {
1780 struct packed_git *p;
1781 strbuf_setlen(&packname, dirlen);
1782 strbuf_addstr(&packname, pack_indexes->items[i].string);
1783 p = add_packed_git(packname.buf, packname.len, 1);
1784 if (!p) {
1785 ret = error(_("error adding pack %s"), packname.buf);
1786 goto cleanup;
1788 if (open_pack_index(p)) {
1789 ret = error(_("error opening index for %s"), packname.buf);
1790 goto cleanup;
1792 for_each_object_in_pack(p, add_packed_commits, ctx,
1793 FOR_EACH_OBJECT_PACK_ORDER);
1794 close_pack(p);
1795 free(p);
1798 cleanup:
1799 stop_progress(&ctx->progress);
1800 strbuf_release(&progress_title);
1801 strbuf_release(&packname);
1803 return ret;
1806 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1807 struct oidset *commits)
1809 struct oidset_iter iter;
1810 struct object_id *oid;
1812 if (!oidset_size(commits))
1813 return 0;
1815 oidset_iter_init(commits, &iter);
1816 while ((oid = oidset_iter_next(&iter))) {
1817 oid_array_append(&ctx->oids, oid);
1820 return 0;
1823 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1825 if (ctx->report_progress)
1826 ctx->progress = start_delayed_progress(
1827 _("Finding commits for commit graph among packed objects"),
1828 ctx->approx_nr_objects);
1829 for_each_packed_object(add_packed_commits, ctx,
1830 FOR_EACH_OBJECT_PACK_ORDER);
1831 if (ctx->progress_done < ctx->approx_nr_objects)
1832 display_progress(ctx->progress, ctx->approx_nr_objects);
1833 stop_progress(&ctx->progress);
1836 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1838 uint32_t i;
1839 enum commit_graph_split_flags flags = ctx->opts ?
1840 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1842 ctx->num_extra_edges = 0;
1843 if (ctx->report_progress)
1844 ctx->progress = start_delayed_progress(
1845 _("Finding extra edges in commit graph"),
1846 ctx->oids.nr);
1847 oid_array_sort(&ctx->oids);
1848 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1849 unsigned int num_parents;
1851 display_progress(ctx->progress, i + 1);
1853 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1854 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1856 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1857 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1858 continue;
1860 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1861 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1862 else
1863 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1865 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1866 if (num_parents > 2)
1867 ctx->num_extra_edges += num_parents - 1;
1869 ctx->commits.nr++;
1871 stop_progress(&ctx->progress);
1874 static int write_graph_chunk_base_1(struct hashfile *f,
1875 struct commit_graph *g)
1877 int num = 0;
1879 if (!g)
1880 return 0;
1882 num = write_graph_chunk_base_1(f, g->base_graph);
1883 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1884 return num + 1;
1887 static int write_graph_chunk_base(struct hashfile *f,
1888 void *data)
1890 struct write_commit_graph_context *ctx = data;
1891 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1893 if (num != ctx->num_commit_graphs_after - 1) {
1894 error(_("failed to write correct number of base graph ids"));
1895 return -1;
1898 return 0;
1901 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1903 uint32_t i;
1904 int fd;
1905 struct hashfile *f;
1906 struct lock_file lk = LOCK_INIT;
1907 const unsigned hashsz = the_hash_algo->rawsz;
1908 struct strbuf progress_title = STRBUF_INIT;
1909 struct chunkfile *cf;
1910 unsigned char file_hash[GIT_MAX_RAWSZ];
1912 if (ctx->split) {
1913 struct strbuf tmp_file = STRBUF_INIT;
1915 strbuf_addf(&tmp_file,
1916 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1917 ctx->odb->path);
1918 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1919 } else {
1920 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1923 if (safe_create_leading_directories(ctx->graph_name)) {
1924 UNLEAK(ctx->graph_name);
1925 error(_("unable to create leading directories of %s"),
1926 ctx->graph_name);
1927 return -1;
1930 if (ctx->split) {
1931 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1933 hold_lock_file_for_update_mode(&lk, lock_name,
1934 LOCK_DIE_ON_ERROR, 0444);
1935 free(lock_name);
1937 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1938 if (fd < 0) {
1939 error(_("unable to create temporary graph layer"));
1940 return -1;
1943 if (adjust_shared_perm(ctx->graph_name)) {
1944 error(_("unable to adjust shared permissions for '%s'"),
1945 ctx->graph_name);
1946 return -1;
1949 f = hashfd(fd, ctx->graph_name);
1950 } else {
1951 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1952 LOCK_DIE_ON_ERROR, 0444);
1953 fd = get_lock_file_fd(&lk);
1954 f = hashfd(fd, get_lock_file_path(&lk));
1957 cf = init_chunkfile(f);
1959 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1960 write_graph_chunk_fanout);
1961 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
1962 write_graph_chunk_oids);
1963 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
1964 write_graph_chunk_data);
1966 if (ctx->write_generation_data)
1967 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1968 st_mult(sizeof(uint32_t), ctx->commits.nr),
1969 write_graph_chunk_generation_data);
1970 if (ctx->num_generation_data_overflows)
1971 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1972 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
1973 write_graph_chunk_generation_data_overflow);
1974 if (ctx->num_extra_edges)
1975 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1976 st_mult(4, ctx->num_extra_edges),
1977 write_graph_chunk_extra_edges);
1978 if (ctx->changed_paths) {
1979 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1980 st_mult(sizeof(uint32_t), ctx->commits.nr),
1981 write_graph_chunk_bloom_indexes);
1982 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1983 st_add(sizeof(uint32_t) * 3,
1984 ctx->total_bloom_filter_data_size),
1985 write_graph_chunk_bloom_data);
1987 if (ctx->num_commit_graphs_after > 1)
1988 add_chunk(cf, GRAPH_CHUNKID_BASE,
1989 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
1990 write_graph_chunk_base);
1992 hashwrite_be32(f, GRAPH_SIGNATURE);
1994 hashwrite_u8(f, GRAPH_VERSION);
1995 hashwrite_u8(f, oid_version(the_hash_algo));
1996 hashwrite_u8(f, get_num_chunks(cf));
1997 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1999 if (ctx->report_progress) {
2000 strbuf_addf(&progress_title,
2001 Q_("Writing out commit graph in %d pass",
2002 "Writing out commit graph in %d passes",
2003 get_num_chunks(cf)),
2004 get_num_chunks(cf));
2005 ctx->progress = start_delayed_progress(
2006 progress_title.buf,
2007 st_mult(get_num_chunks(cf), ctx->commits.nr));
2010 write_chunkfile(cf, ctx);
2012 stop_progress(&ctx->progress);
2013 strbuf_release(&progress_title);
2015 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2016 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2017 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
2019 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2020 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2021 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2022 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2025 close_commit_graph(ctx->r->objects);
2026 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2027 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2028 free_chunkfile(cf);
2030 if (ctx->split) {
2031 FILE *chainf = fdopen_lock_file(&lk, "w");
2032 char *final_graph_name;
2033 int result;
2035 close(fd);
2037 if (!chainf) {
2038 error(_("unable to open commit-graph chain file"));
2039 return -1;
2042 if (ctx->base_graph_name) {
2043 const char *dest;
2044 int idx = ctx->num_commit_graphs_after - 1;
2045 if (ctx->num_commit_graphs_after > 1)
2046 idx--;
2048 dest = ctx->commit_graph_filenames_after[idx];
2050 if (strcmp(ctx->base_graph_name, dest)) {
2051 result = rename(ctx->base_graph_name, dest);
2053 if (result) {
2054 error(_("failed to rename base commit-graph file"));
2055 return -1;
2058 } else {
2059 char *graph_name = get_commit_graph_filename(ctx->odb);
2060 unlink(graph_name);
2061 free(graph_name);
2064 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
2065 final_graph_name = get_split_graph_filename(ctx->odb,
2066 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2067 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2069 result = rename(ctx->graph_name, final_graph_name);
2071 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2072 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2074 if (result) {
2075 error(_("failed to rename temporary commit-graph file"));
2076 return -1;
2080 commit_lock_file(&lk);
2082 return 0;
2085 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2087 struct commit_graph *g;
2088 uint32_t num_commits;
2089 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2090 uint32_t i;
2092 int max_commits = 0;
2093 int size_mult = 2;
2095 if (ctx->opts) {
2096 max_commits = ctx->opts->max_commits;
2098 if (ctx->opts->size_multiple)
2099 size_mult = ctx->opts->size_multiple;
2101 flags = ctx->opts->split_flags;
2104 g = ctx->r->objects->commit_graph;
2105 num_commits = ctx->commits.nr;
2106 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2107 ctx->num_commit_graphs_after = 1;
2108 else
2109 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2111 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2112 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2113 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2114 (max_commits && num_commits > max_commits))) {
2115 if (g->odb != ctx->odb)
2116 break;
2118 if (unsigned_add_overflows(num_commits, g->num_commits))
2119 die(_("cannot merge graphs with %"PRIuMAX", "
2120 "%"PRIuMAX" commits"),
2121 (uintmax_t)num_commits,
2122 (uintmax_t)g->num_commits);
2123 num_commits += g->num_commits;
2124 g = g->base_graph;
2126 ctx->num_commit_graphs_after--;
2130 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2131 ctx->new_base_graph = g;
2132 else if (ctx->num_commit_graphs_after != 1)
2133 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2134 "should be 1 with --split=replace");
2136 if (ctx->num_commit_graphs_after == 2) {
2137 char *old_graph_name = get_commit_graph_filename(g->odb);
2139 if (!strcmp(g->filename, old_graph_name) &&
2140 g->odb != ctx->odb) {
2141 ctx->num_commit_graphs_after = 1;
2142 ctx->new_base_graph = NULL;
2145 free(old_graph_name);
2148 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2149 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2151 for (i = 0; i < ctx->num_commit_graphs_after &&
2152 i < ctx->num_commit_graphs_before; i++)
2153 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2155 i = ctx->num_commit_graphs_before - 1;
2156 g = ctx->r->objects->commit_graph;
2158 while (g) {
2159 if (i < ctx->num_commit_graphs_after)
2160 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2163 * If the topmost remaining layer has generation data chunk, the
2164 * resultant layer also has generation data chunk.
2166 if (i == ctx->num_commit_graphs_after - 2)
2167 ctx->write_generation_data = !!g->chunk_generation_data;
2169 i--;
2170 g = g->base_graph;
2174 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2175 struct commit_graph *g)
2177 uint32_t i;
2178 uint32_t offset = g->num_commits_in_base;
2180 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2181 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2182 oid_to_hex(&g->oid),
2183 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2185 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2187 for (i = 0; i < g->num_commits; i++) {
2188 struct object_id oid;
2189 struct commit *result;
2191 display_progress(ctx->progress, i + 1);
2193 load_oid_from_graph(g, i + offset, &oid);
2195 /* only add commits if they still exist in the repo */
2196 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2198 if (result) {
2199 ctx->commits.list[ctx->commits.nr] = result;
2200 ctx->commits.nr++;
2205 static int commit_compare(const void *_a, const void *_b)
2207 const struct commit *a = *(const struct commit **)_a;
2208 const struct commit *b = *(const struct commit **)_b;
2209 return oidcmp(&a->object.oid, &b->object.oid);
2212 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2214 uint32_t i, dedup_i = 0;
2216 if (ctx->report_progress)
2217 ctx->progress = start_delayed_progress(
2218 _("Scanning merged commits"),
2219 ctx->commits.nr);
2221 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2223 ctx->num_extra_edges = 0;
2224 for (i = 0; i < ctx->commits.nr; i++) {
2225 display_progress(ctx->progress, i + 1);
2227 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2228 &ctx->commits.list[i]->object.oid)) {
2230 * Silently ignore duplicates. These were likely
2231 * created due to a commit appearing in multiple
2232 * layers of the chain, which is unexpected but
2233 * not invalid. We should make sure there is a
2234 * unique copy in the new layer.
2236 } else {
2237 unsigned int num_parents;
2239 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2240 dedup_i++;
2242 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2243 if (num_parents > 2)
2244 ctx->num_extra_edges += num_parents - 1;
2248 ctx->commits.nr = dedup_i;
2250 stop_progress(&ctx->progress);
2253 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2255 struct commit_graph *g = ctx->r->objects->commit_graph;
2256 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2258 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2259 current_graph_number--;
2261 if (ctx->report_progress)
2262 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2264 merge_commit_graph(ctx, g);
2265 stop_progress(&ctx->progress);
2267 g = g->base_graph;
2270 if (g) {
2271 ctx->new_base_graph = g;
2272 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2275 if (ctx->new_base_graph)
2276 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2278 sort_and_scan_merged_commits(ctx);
2281 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2283 uint32_t i;
2284 time_t now = time(NULL);
2286 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2287 struct stat st;
2288 struct utimbuf updated_time;
2290 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2291 continue;
2293 updated_time.actime = st.st_atime;
2294 updated_time.modtime = now;
2295 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2299 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2301 struct strbuf path = STRBUF_INIT;
2302 DIR *dir;
2303 struct dirent *de;
2304 size_t dirnamelen;
2305 timestamp_t expire_time = time(NULL);
2307 if (ctx->opts && ctx->opts->expire_time)
2308 expire_time = ctx->opts->expire_time;
2309 if (!ctx->split) {
2310 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2311 unlink(chain_file_name);
2312 free(chain_file_name);
2313 ctx->num_commit_graphs_after = 0;
2316 strbuf_addstr(&path, ctx->odb->path);
2317 strbuf_addstr(&path, "/info/commit-graphs");
2318 dir = opendir(path.buf);
2320 if (!dir)
2321 goto out;
2323 strbuf_addch(&path, '/');
2324 dirnamelen = path.len;
2325 while ((de = readdir(dir)) != NULL) {
2326 struct stat st;
2327 uint32_t i, found = 0;
2329 strbuf_setlen(&path, dirnamelen);
2330 strbuf_addstr(&path, de->d_name);
2332 if (stat(path.buf, &st) < 0)
2333 continue;
2335 if (st.st_mtime > expire_time)
2336 continue;
2337 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2338 continue;
2340 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2341 if (!strcmp(ctx->commit_graph_filenames_after[i],
2342 path.buf)) {
2343 found = 1;
2344 break;
2348 if (!found)
2349 unlink(path.buf);
2352 out:
2353 if(dir)
2354 closedir(dir);
2355 strbuf_release(&path);
2358 int write_commit_graph(struct object_directory *odb,
2359 const struct string_list *const pack_indexes,
2360 struct oidset *commits,
2361 enum commit_graph_write_flags flags,
2362 const struct commit_graph_opts *opts)
2364 struct repository *r = the_repository;
2365 struct write_commit_graph_context *ctx;
2366 uint32_t i;
2367 int res = 0;
2368 int replace = 0;
2369 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2370 struct topo_level_slab topo_levels;
2372 prepare_repo_settings(r);
2373 if (!r->settings.core_commit_graph) {
2374 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2375 return 0;
2377 if (!commit_graph_compatible(r))
2378 return 0;
2380 CALLOC_ARRAY(ctx, 1);
2381 ctx->r = r;
2382 ctx->odb = odb;
2383 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2384 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2385 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2386 ctx->opts = opts;
2387 ctx->total_bloom_filter_data_size = 0;
2388 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2389 ctx->num_generation_data_overflows = 0;
2391 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2392 bloom_settings.bits_per_entry);
2393 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2394 bloom_settings.num_hashes);
2395 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2396 bloom_settings.max_changed_paths);
2397 ctx->bloom_settings = &bloom_settings;
2399 init_topo_level_slab(&topo_levels);
2400 ctx->topo_levels = &topo_levels;
2402 prepare_commit_graph(ctx->r);
2403 if (ctx->r->objects->commit_graph) {
2404 struct commit_graph *g = ctx->r->objects->commit_graph;
2406 while (g) {
2407 g->topo_levels = &topo_levels;
2408 g = g->base_graph;
2412 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2413 ctx->changed_paths = 1;
2414 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2415 struct commit_graph *g;
2417 g = ctx->r->objects->commit_graph;
2419 /* We have changed-paths already. Keep them in the next graph */
2420 if (g && g->chunk_bloom_data) {
2421 ctx->changed_paths = 1;
2422 ctx->bloom_settings = g->bloom_filter_settings;
2426 if (ctx->split) {
2427 struct commit_graph *g = ctx->r->objects->commit_graph;
2429 while (g) {
2430 ctx->num_commit_graphs_before++;
2431 g = g->base_graph;
2434 if (ctx->num_commit_graphs_before) {
2435 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2436 i = ctx->num_commit_graphs_before;
2437 g = ctx->r->objects->commit_graph;
2439 while (g) {
2440 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2441 g = g->base_graph;
2445 if (ctx->opts)
2446 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2449 ctx->approx_nr_objects = repo_approximate_object_count(the_repository);
2451 if (ctx->append && ctx->r->objects->commit_graph) {
2452 struct commit_graph *g = ctx->r->objects->commit_graph;
2453 for (i = 0; i < g->num_commits; i++) {
2454 struct object_id oid;
2455 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2456 oid_array_append(&ctx->oids, &oid);
2460 if (pack_indexes) {
2461 ctx->order_by_pack = 1;
2462 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2463 goto cleanup;
2466 if (commits) {
2467 if ((res = fill_oids_from_commits(ctx, commits)))
2468 goto cleanup;
2471 if (!pack_indexes && !commits) {
2472 ctx->order_by_pack = 1;
2473 fill_oids_from_all_packs(ctx);
2476 close_reachable(ctx);
2478 copy_oids_to_commits(ctx);
2480 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2481 error(_("too many commits to write graph"));
2482 res = -1;
2483 goto cleanup;
2486 if (!ctx->commits.nr && !replace)
2487 goto cleanup;
2489 if (ctx->split) {
2490 split_graph_merge_strategy(ctx);
2492 if (!replace)
2493 merge_commit_graphs(ctx);
2494 } else
2495 ctx->num_commit_graphs_after = 1;
2497 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2499 compute_topological_levels(ctx);
2500 if (ctx->write_generation_data)
2501 compute_generation_numbers(ctx);
2503 if (ctx->changed_paths)
2504 compute_bloom_filters(ctx);
2506 res = write_commit_graph_file(ctx);
2508 if (ctx->split)
2509 mark_commit_graphs(ctx);
2511 expire_commit_graphs(ctx);
2513 cleanup:
2514 free(ctx->graph_name);
2515 free(ctx->commits.list);
2516 oid_array_clear(&ctx->oids);
2517 clear_topo_level_slab(&topo_levels);
2519 if (ctx->commit_graph_filenames_after) {
2520 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2521 free(ctx->commit_graph_filenames_after[i]);
2522 free(ctx->commit_graph_hash_after[i]);
2525 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2526 free(ctx->commit_graph_filenames_before[i]);
2528 free(ctx->commit_graph_filenames_after);
2529 free(ctx->commit_graph_filenames_before);
2530 free(ctx->commit_graph_hash_after);
2533 free(ctx);
2535 return res;
2538 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2539 static int verify_commit_graph_error;
2541 __attribute__((format (printf, 1, 2)))
2542 static void graph_report(const char *fmt, ...)
2544 va_list ap;
2546 verify_commit_graph_error = 1;
2547 va_start(ap, fmt);
2548 vfprintf(stderr, fmt, ap);
2549 fprintf(stderr, "\n");
2550 va_end(ap);
2553 #define GENERATION_ZERO_EXISTS 1
2554 #define GENERATION_NUMBER_EXISTS 2
2556 static int commit_graph_checksum_valid(struct commit_graph *g)
2558 return hashfile_checksum_valid(g->data, g->data_len);
2561 static int verify_one_commit_graph(struct repository *r,
2562 struct commit_graph *g,
2563 struct progress *progress,
2564 uint64_t *seen)
2566 uint32_t i, cur_fanout_pos = 0;
2567 struct object_id prev_oid, cur_oid;
2568 int generation_zero = 0;
2570 verify_commit_graph_error = verify_commit_graph_lite(g);
2571 if (verify_commit_graph_error)
2572 return verify_commit_graph_error;
2574 if (!commit_graph_checksum_valid(g)) {
2575 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2576 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2579 for (i = 0; i < g->num_commits; i++) {
2580 struct commit *graph_commit;
2582 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2584 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2585 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2586 oid_to_hex(&prev_oid),
2587 oid_to_hex(&cur_oid));
2589 oidcpy(&prev_oid, &cur_oid);
2591 while (cur_oid.hash[0] > cur_fanout_pos) {
2592 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2594 if (i != fanout_value)
2595 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2596 cur_fanout_pos, fanout_value, i);
2597 cur_fanout_pos++;
2600 graph_commit = lookup_commit(r, &cur_oid);
2601 if (!parse_commit_in_graph_one(r, g, graph_commit))
2602 graph_report(_("failed to parse commit %s from commit-graph"),
2603 oid_to_hex(&cur_oid));
2606 while (cur_fanout_pos < 256) {
2607 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2609 if (g->num_commits != fanout_value)
2610 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2611 cur_fanout_pos, fanout_value, i);
2613 cur_fanout_pos++;
2616 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2617 return verify_commit_graph_error;
2619 for (i = 0; i < g->num_commits; i++) {
2620 struct commit *graph_commit, *odb_commit;
2621 struct commit_list *graph_parents, *odb_parents;
2622 timestamp_t max_generation = 0;
2623 timestamp_t generation;
2625 display_progress(progress, ++(*seen));
2626 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_len, i));
2628 graph_commit = lookup_commit(r, &cur_oid);
2629 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2630 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2631 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2632 oid_to_hex(&cur_oid));
2633 continue;
2636 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2637 get_commit_tree_oid(odb_commit)))
2638 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2639 oid_to_hex(&cur_oid),
2640 oid_to_hex(get_commit_tree_oid(graph_commit)),
2641 oid_to_hex(get_commit_tree_oid(odb_commit)));
2643 graph_parents = graph_commit->parents;
2644 odb_parents = odb_commit->parents;
2646 while (graph_parents) {
2647 if (!odb_parents) {
2648 graph_report(_("commit-graph parent list for commit %s is too long"),
2649 oid_to_hex(&cur_oid));
2650 break;
2653 /* parse parent in case it is in a base graph */
2654 parse_commit_in_graph_one(r, g, graph_parents->item);
2656 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2657 graph_report(_("commit-graph parent for %s is %s != %s"),
2658 oid_to_hex(&cur_oid),
2659 oid_to_hex(&graph_parents->item->object.oid),
2660 oid_to_hex(&odb_parents->item->object.oid));
2662 generation = commit_graph_generation(graph_parents->item);
2663 if (generation > max_generation)
2664 max_generation = generation;
2666 graph_parents = graph_parents->next;
2667 odb_parents = odb_parents->next;
2670 if (odb_parents)
2671 graph_report(_("commit-graph parent list for commit %s terminates early"),
2672 oid_to_hex(&cur_oid));
2674 if (!commit_graph_generation(graph_commit)) {
2675 if (generation_zero == GENERATION_NUMBER_EXISTS)
2676 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2677 oid_to_hex(&cur_oid));
2678 generation_zero = GENERATION_ZERO_EXISTS;
2679 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2680 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2681 oid_to_hex(&cur_oid));
2683 if (generation_zero == GENERATION_ZERO_EXISTS)
2684 continue;
2687 * If we are using topological level and one of our parents has
2688 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2689 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2690 * in the following condition.
2692 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2693 max_generation--;
2695 generation = commit_graph_generation(graph_commit);
2696 if (generation < max_generation + 1)
2697 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2698 oid_to_hex(&cur_oid),
2699 generation,
2700 max_generation + 1);
2702 if (graph_commit->date != odb_commit->date)
2703 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2704 oid_to_hex(&cur_oid),
2705 graph_commit->date,
2706 odb_commit->date);
2709 return verify_commit_graph_error;
2712 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2714 struct progress *progress = NULL;
2715 int local_error = 0;
2716 uint64_t seen = 0;
2718 if (!g) {
2719 graph_report("no commit-graph file loaded");
2720 return 1;
2723 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2724 uint64_t total = g->num_commits;
2725 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2726 total += g->num_commits_in_base;
2728 progress = start_progress(_("Verifying commits in commit graph"),
2729 total);
2732 for (; g; g = g->base_graph) {
2733 local_error |= verify_one_commit_graph(r, g, progress, &seen);
2734 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2735 break;
2738 stop_progress(&progress);
2740 return local_error;
2743 void free_commit_graph(struct commit_graph *g)
2745 if (!g)
2746 return;
2747 if (g->data) {
2748 munmap((void *)g->data, g->data_len);
2749 g->data = NULL;
2751 free(g->filename);
2752 free(g->bloom_filter_settings);
2753 free(g);
2756 void disable_commit_graph(struct repository *r)
2758 r->commit_graph_disabled = 1;