Merge branch 'ab/misc-cleanup'
[git/debian.git] / commit-graph.c
blob441b36016ba796c13665603b4c523c9baa52c12d
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "lockfile.h"
4 #include "pack.h"
5 #include "packfile.h"
6 #include "commit.h"
7 #include "object.h"
8 #include "refs.h"
9 #include "revision.h"
10 #include "hash-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
13 #include "alloc.h"
14 #include "hashmap.h"
15 #include "replace-object.h"
16 #include "progress.h"
17 #include "bloom.h"
18 #include "commit-slab.h"
19 #include "shallow.h"
20 #include "json-writer.h"
21 #include "trace2.h"
22 #include "chunk-format.h"
24 void git_test_write_commit_graph_or_die(void)
26 int flags = 0;
27 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
28 return;
30 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
31 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
33 if (write_commit_graph_reachable(the_repository->objects->odb,
34 flags, NULL))
35 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
38 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
39 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
40 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
41 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
42 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
43 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
44 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
45 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
46 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
47 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
49 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
51 #define GRAPH_VERSION_1 0x1
52 #define GRAPH_VERSION GRAPH_VERSION_1
54 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
55 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
56 #define GRAPH_PARENT_NONE 0x70000000
58 #define GRAPH_LAST_EDGE 0x80000000
60 #define GRAPH_HEADER_SIZE 8
61 #define GRAPH_FANOUT_SIZE (4 * 256)
62 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
63 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
65 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
67 /* Remember to update object flag allocation in object.h */
68 #define REACHABLE (1u<<15)
70 define_commit_slab(topo_level_slab, uint32_t);
72 /* Keep track of the order in which commits are added to our list. */
73 define_commit_slab(commit_pos, int);
74 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
76 static void set_commit_pos(struct repository *r, const struct object_id *oid)
78 static int32_t max_pos;
79 struct commit *commit = lookup_commit(r, oid);
81 if (!commit)
82 return; /* should never happen, but be lenient */
84 *commit_pos_at(&commit_pos, commit) = max_pos++;
87 static int commit_pos_cmp(const void *va, const void *vb)
89 const struct commit *a = *(const struct commit **)va;
90 const struct commit *b = *(const struct commit **)vb;
91 return commit_pos_at(&commit_pos, a) -
92 commit_pos_at(&commit_pos, b);
95 define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
96 static struct commit_graph_data_slab commit_graph_data_slab =
97 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
99 static int get_configured_generation_version(struct repository *r)
101 int version = 2;
102 repo_config_get_int(r, "commitgraph.generationversion", &version);
103 return version;
106 uint32_t commit_graph_position(const struct commit *c)
108 struct commit_graph_data *data =
109 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
111 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
114 timestamp_t commit_graph_generation(const struct commit *c)
116 struct commit_graph_data *data =
117 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
119 if (!data)
120 return GENERATION_NUMBER_INFINITY;
121 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
122 return GENERATION_NUMBER_INFINITY;
124 return data->generation;
127 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
129 unsigned int i, nth_slab;
130 struct commit_graph_data *data =
131 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
133 if (data)
134 return data;
136 nth_slab = c->index / commit_graph_data_slab.slab_size;
137 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
140 * commit-slab initializes elements with zero, overwrite this with
141 * COMMIT_NOT_FROM_GRAPH for graph_pos.
143 * We avoid initializing generation with checking if graph position
144 * is not COMMIT_NOT_FROM_GRAPH.
146 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
147 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
148 COMMIT_NOT_FROM_GRAPH;
151 return data;
155 * Should be used only while writing commit-graph as it compares
156 * generation value of commits by directly accessing commit-slab.
158 static int commit_gen_cmp(const void *va, const void *vb)
160 const struct commit *a = *(const struct commit **)va;
161 const struct commit *b = *(const struct commit **)vb;
163 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
164 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
165 /* lower generation commits first */
166 if (generation_a < generation_b)
167 return -1;
168 else if (generation_a > generation_b)
169 return 1;
171 /* use date as a heuristic when generations are equal */
172 if (a->date < b->date)
173 return -1;
174 else if (a->date > b->date)
175 return 1;
176 return 0;
179 char *get_commit_graph_filename(struct object_directory *obj_dir)
181 return xstrfmt("%s/info/commit-graph", obj_dir->path);
184 static char *get_split_graph_filename(struct object_directory *odb,
185 const char *oid_hex)
187 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
188 oid_hex);
191 char *get_commit_graph_chain_filename(struct object_directory *odb)
193 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
196 static uint8_t oid_version(void)
198 switch (hash_algo_by_ptr(the_hash_algo)) {
199 case GIT_HASH_SHA1:
200 return 1;
201 case GIT_HASH_SHA256:
202 return 2;
203 default:
204 die(_("invalid hash version"));
208 static struct commit_graph *alloc_commit_graph(void)
210 struct commit_graph *g = xcalloc(1, sizeof(*g));
212 return g;
215 extern int read_replace_refs;
217 static int commit_graph_compatible(struct repository *r)
219 if (!r->gitdir)
220 return 0;
222 if (read_replace_refs) {
223 prepare_replace_object(r);
224 if (hashmap_get_size(&r->objects->replace_map->map))
225 return 0;
228 prepare_commit_graft(r);
229 if (r->parsed_objects &&
230 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
231 return 0;
232 if (is_repository_shallow(r))
233 return 0;
235 return 1;
238 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
240 *fd = git_open(graph_file);
241 if (*fd < 0)
242 return 0;
243 if (fstat(*fd, st)) {
244 close(*fd);
245 return 0;
247 return 1;
250 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
251 int fd, struct stat *st,
252 struct object_directory *odb)
254 void *graph_map;
255 size_t graph_size;
256 struct commit_graph *ret;
258 graph_size = xsize_t(st->st_size);
260 if (graph_size < GRAPH_MIN_SIZE) {
261 close(fd);
262 error(_("commit-graph file is too small"));
263 return NULL;
265 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
266 close(fd);
267 ret = parse_commit_graph(r, graph_map, graph_size);
269 if (ret)
270 ret->odb = odb;
271 else
272 munmap(graph_map, graph_size);
274 return ret;
277 static int verify_commit_graph_lite(struct commit_graph *g)
280 * Basic validation shared between parse_commit_graph()
281 * which'll be called every time the graph is used, and the
282 * much more expensive verify_commit_graph() used by
283 * "commit-graph verify".
285 * There should only be very basic checks here to ensure that
286 * we don't e.g. segfault in fill_commit_in_graph(), but
287 * because this is a very hot codepath nothing that e.g. loops
288 * over g->num_commits, or runs a checksum on the commit-graph
289 * itself.
291 if (!g->chunk_oid_fanout) {
292 error("commit-graph is missing the OID Fanout chunk");
293 return 1;
295 if (!g->chunk_oid_lookup) {
296 error("commit-graph is missing the OID Lookup chunk");
297 return 1;
299 if (!g->chunk_commit_data) {
300 error("commit-graph is missing the Commit Data chunk");
301 return 1;
304 return 0;
307 static int graph_read_oid_lookup(const unsigned char *chunk_start,
308 size_t chunk_size, void *data)
310 struct commit_graph *g = data;
311 g->chunk_oid_lookup = chunk_start;
312 g->num_commits = chunk_size / g->hash_len;
313 return 0;
316 static int graph_read_bloom_data(const unsigned char *chunk_start,
317 size_t chunk_size, void *data)
319 struct commit_graph *g = data;
320 uint32_t hash_version;
321 g->chunk_bloom_data = chunk_start;
322 hash_version = get_be32(chunk_start);
324 if (hash_version != 1)
325 return 0;
327 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
328 g->bloom_filter_settings->hash_version = hash_version;
329 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
330 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
331 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
333 return 0;
336 struct commit_graph *parse_commit_graph(struct repository *r,
337 void *graph_map, size_t graph_size)
339 const unsigned char *data;
340 struct commit_graph *graph;
341 uint32_t graph_signature;
342 unsigned char graph_version, hash_version;
343 struct chunkfile *cf = NULL;
345 if (!graph_map)
346 return NULL;
348 if (graph_size < GRAPH_MIN_SIZE)
349 return NULL;
351 data = (const unsigned char *)graph_map;
353 graph_signature = get_be32(data);
354 if (graph_signature != GRAPH_SIGNATURE) {
355 error(_("commit-graph signature %X does not match signature %X"),
356 graph_signature, GRAPH_SIGNATURE);
357 return NULL;
360 graph_version = *(unsigned char*)(data + 4);
361 if (graph_version != GRAPH_VERSION) {
362 error(_("commit-graph version %X does not match version %X"),
363 graph_version, GRAPH_VERSION);
364 return NULL;
367 hash_version = *(unsigned char*)(data + 5);
368 if (hash_version != oid_version()) {
369 error(_("commit-graph hash version %X does not match version %X"),
370 hash_version, oid_version());
371 return NULL;
374 prepare_repo_settings(r);
376 graph = alloc_commit_graph();
378 graph->hash_len = the_hash_algo->rawsz;
379 graph->num_chunks = *(unsigned char*)(data + 6);
380 graph->data = graph_map;
381 graph->data_len = graph_size;
383 if (graph_size < GRAPH_HEADER_SIZE +
384 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
385 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
386 error(_("commit-graph file is too small to hold %u chunks"),
387 graph->num_chunks);
388 free(graph);
389 return NULL;
392 cf = init_chunkfile(NULL);
394 if (read_table_of_contents(cf, graph->data, graph_size,
395 GRAPH_HEADER_SIZE, graph->num_chunks))
396 goto free_and_return;
398 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
399 (const unsigned char **)&graph->chunk_oid_fanout);
400 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
401 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
402 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
403 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
405 if (get_configured_generation_version(r) >= 2) {
406 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
407 &graph->chunk_generation_data);
408 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
409 &graph->chunk_generation_data_overflow);
411 if (graph->chunk_generation_data)
412 graph->read_generation_data = 1;
415 if (r->settings.commit_graph_read_changed_paths) {
416 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
417 &graph->chunk_bloom_indexes);
418 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
419 graph_read_bloom_data, graph);
422 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
423 init_bloom_filters();
424 } else {
425 /* We need both the bloom chunks to exist together. Else ignore the data */
426 graph->chunk_bloom_indexes = NULL;
427 graph->chunk_bloom_data = NULL;
428 FREE_AND_NULL(graph->bloom_filter_settings);
431 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
433 if (verify_commit_graph_lite(graph))
434 goto free_and_return;
436 free_chunkfile(cf);
437 return graph;
439 free_and_return:
440 free_chunkfile(cf);
441 free(graph->bloom_filter_settings);
442 free(graph);
443 return NULL;
446 static struct commit_graph *load_commit_graph_one(struct repository *r,
447 const char *graph_file,
448 struct object_directory *odb)
451 struct stat st;
452 int fd;
453 struct commit_graph *g;
454 int open_ok = open_commit_graph(graph_file, &fd, &st);
456 if (!open_ok)
457 return NULL;
459 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
461 if (g)
462 g->filename = xstrdup(graph_file);
464 return g;
467 static struct commit_graph *load_commit_graph_v1(struct repository *r,
468 struct object_directory *odb)
470 char *graph_name = get_commit_graph_filename(odb);
471 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
472 free(graph_name);
474 return g;
477 static int add_graph_to_chain(struct commit_graph *g,
478 struct commit_graph *chain,
479 struct object_id *oids,
480 int n)
482 struct commit_graph *cur_g = chain;
484 if (n && !g->chunk_base_graphs) {
485 warning(_("commit-graph has no base graphs chunk"));
486 return 0;
489 while (n) {
490 n--;
492 if (!cur_g ||
493 !oideq(&oids[n], &cur_g->oid) ||
494 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
495 warning(_("commit-graph chain does not match"));
496 return 0;
499 cur_g = cur_g->base_graph;
502 g->base_graph = chain;
504 if (chain)
505 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
507 return 1;
510 static struct commit_graph *load_commit_graph_chain(struct repository *r,
511 struct object_directory *odb)
513 struct commit_graph *graph_chain = NULL;
514 struct strbuf line = STRBUF_INIT;
515 struct stat st;
516 struct object_id *oids;
517 int i = 0, valid = 1, count;
518 char *chain_name = get_commit_graph_chain_filename(odb);
519 FILE *fp;
520 int stat_res;
522 fp = fopen(chain_name, "r");
523 stat_res = stat(chain_name, &st);
524 free(chain_name);
526 if (!fp ||
527 stat_res ||
528 st.st_size <= the_hash_algo->hexsz)
529 return NULL;
531 count = st.st_size / (the_hash_algo->hexsz + 1);
532 CALLOC_ARRAY(oids, count);
534 prepare_alt_odb(r);
536 for (i = 0; i < count; i++) {
537 struct object_directory *odb;
539 if (strbuf_getline_lf(&line, fp) == EOF)
540 break;
542 if (get_oid_hex(line.buf, &oids[i])) {
543 warning(_("invalid commit-graph chain: line '%s' not a hash"),
544 line.buf);
545 valid = 0;
546 break;
549 valid = 0;
550 for (odb = r->objects->odb; odb; odb = odb->next) {
551 char *graph_name = get_split_graph_filename(odb, line.buf);
552 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
554 free(graph_name);
556 if (g) {
557 if (add_graph_to_chain(g, graph_chain, oids, i)) {
558 graph_chain = g;
559 valid = 1;
562 break;
566 if (!valid) {
567 warning(_("unable to find all commit-graph files"));
568 break;
572 free(oids);
573 fclose(fp);
574 strbuf_release(&line);
576 return graph_chain;
580 * returns 1 if and only if all graphs in the chain have
581 * corrected commit dates stored in the generation_data chunk.
583 static int validate_mixed_generation_chain(struct commit_graph *g)
585 int read_generation_data = 1;
586 struct commit_graph *p = g;
588 while (read_generation_data && p) {
589 read_generation_data = p->read_generation_data;
590 p = p->base_graph;
593 if (read_generation_data)
594 return 1;
596 while (g) {
597 g->read_generation_data = 0;
598 g = g->base_graph;
601 return 0;
604 struct commit_graph *read_commit_graph_one(struct repository *r,
605 struct object_directory *odb)
607 struct commit_graph *g = load_commit_graph_v1(r, odb);
609 if (!g)
610 g = load_commit_graph_chain(r, odb);
612 validate_mixed_generation_chain(g);
614 return g;
617 static void prepare_commit_graph_one(struct repository *r,
618 struct object_directory *odb)
621 if (r->objects->commit_graph)
622 return;
624 r->objects->commit_graph = read_commit_graph_one(r, odb);
628 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
630 * On the first invocation, this function attempts to load the commit
631 * graph if the_repository is configured to have one.
633 static int prepare_commit_graph(struct repository *r)
635 struct object_directory *odb;
638 * Early return if there is no git dir or if the commit graph is
639 * disabled.
641 * This must come before the "already attempted?" check below, because
642 * we want to disable even an already-loaded graph file.
644 if (!r->gitdir || r->commit_graph_disabled)
645 return 0;
647 if (r->objects->commit_graph_attempted)
648 return !!r->objects->commit_graph;
649 r->objects->commit_graph_attempted = 1;
651 prepare_repo_settings(r);
653 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
654 r->settings.core_commit_graph != 1)
656 * This repository is not configured to use commit graphs, so
657 * do not load one. (But report commit_graph_attempted anyway
658 * so that commit graph loading is not attempted again for this
659 * repository.)
661 return 0;
663 if (!commit_graph_compatible(r))
664 return 0;
666 prepare_alt_odb(r);
667 for (odb = r->objects->odb;
668 !r->objects->commit_graph && odb;
669 odb = odb->next)
670 prepare_commit_graph_one(r, odb);
671 return !!r->objects->commit_graph;
674 int generation_numbers_enabled(struct repository *r)
676 uint32_t first_generation;
677 struct commit_graph *g;
678 if (!prepare_commit_graph(r))
679 return 0;
681 g = r->objects->commit_graph;
683 if (!g->num_commits)
684 return 0;
686 first_generation = get_be32(g->chunk_commit_data +
687 g->hash_len + 8) >> 2;
689 return !!first_generation;
692 int corrected_commit_dates_enabled(struct repository *r)
694 struct commit_graph *g;
695 if (!prepare_commit_graph(r))
696 return 0;
698 g = r->objects->commit_graph;
700 if (!g->num_commits)
701 return 0;
703 return g->read_generation_data;
706 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
708 struct commit_graph *g = r->objects->commit_graph;
709 while (g) {
710 if (g->bloom_filter_settings)
711 return g->bloom_filter_settings;
712 g = g->base_graph;
714 return NULL;
717 static void close_commit_graph_one(struct commit_graph *g)
719 if (!g)
720 return;
722 clear_commit_graph_data_slab(&commit_graph_data_slab);
723 close_commit_graph_one(g->base_graph);
724 free_commit_graph(g);
727 void close_commit_graph(struct raw_object_store *o)
729 close_commit_graph_one(o->commit_graph);
730 o->commit_graph = NULL;
733 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
735 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
736 g->chunk_oid_lookup, g->hash_len, pos);
739 static void load_oid_from_graph(struct commit_graph *g,
740 uint32_t pos,
741 struct object_id *oid)
743 uint32_t lex_index;
745 while (g && pos < g->num_commits_in_base)
746 g = g->base_graph;
748 if (!g)
749 BUG("NULL commit-graph");
751 if (pos >= g->num_commits + g->num_commits_in_base)
752 die(_("invalid commit position. commit-graph is likely corrupt"));
754 lex_index = pos - g->num_commits_in_base;
756 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
759 static struct commit_list **insert_parent_or_die(struct repository *r,
760 struct commit_graph *g,
761 uint32_t pos,
762 struct commit_list **pptr)
764 struct commit *c;
765 struct object_id oid;
767 if (pos >= g->num_commits + g->num_commits_in_base)
768 die("invalid parent position %"PRIu32, pos);
770 load_oid_from_graph(g, pos, &oid);
771 c = lookup_commit(r, &oid);
772 if (!c)
773 die(_("could not find commit %s"), oid_to_hex(&oid));
774 commit_graph_data_at(c)->graph_pos = pos;
775 return &commit_list_insert(c, pptr)->next;
778 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
780 const unsigned char *commit_data;
781 struct commit_graph_data *graph_data;
782 uint32_t lex_index, offset_pos;
783 uint64_t date_high, date_low, offset;
785 while (pos < g->num_commits_in_base)
786 g = g->base_graph;
788 if (pos >= g->num_commits + g->num_commits_in_base)
789 die(_("invalid commit position. commit-graph is likely corrupt"));
791 lex_index = pos - g->num_commits_in_base;
792 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
794 graph_data = commit_graph_data_at(item);
795 graph_data->graph_pos = pos;
797 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
798 date_low = get_be32(commit_data + g->hash_len + 12);
799 item->date = (timestamp_t)((date_high << 32) | date_low);
801 if (g->read_generation_data) {
802 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
804 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
805 if (!g->chunk_generation_data_overflow)
806 die(_("commit-graph requires overflow generation data but has none"));
808 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
809 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
810 } else
811 graph_data->generation = item->date + offset;
812 } else
813 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
815 if (g->topo_levels)
816 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
819 static inline void set_commit_tree(struct commit *c, struct tree *t)
821 c->maybe_tree = t;
824 static int fill_commit_in_graph(struct repository *r,
825 struct commit *item,
826 struct commit_graph *g, uint32_t pos)
828 uint32_t edge_value;
829 uint32_t *parent_data_ptr;
830 struct commit_list **pptr;
831 const unsigned char *commit_data;
832 uint32_t lex_index;
834 while (pos < g->num_commits_in_base)
835 g = g->base_graph;
837 fill_commit_graph_info(item, g, pos);
839 lex_index = pos - g->num_commits_in_base;
840 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
842 item->object.parsed = 1;
844 set_commit_tree(item, NULL);
846 pptr = &item->parents;
848 edge_value = get_be32(commit_data + g->hash_len);
849 if (edge_value == GRAPH_PARENT_NONE)
850 return 1;
851 pptr = insert_parent_or_die(r, g, edge_value, pptr);
853 edge_value = get_be32(commit_data + g->hash_len + 4);
854 if (edge_value == GRAPH_PARENT_NONE)
855 return 1;
856 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
857 pptr = insert_parent_or_die(r, g, edge_value, pptr);
858 return 1;
861 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
862 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
863 do {
864 edge_value = get_be32(parent_data_ptr);
865 pptr = insert_parent_or_die(r, g,
866 edge_value & GRAPH_EDGE_LAST_MASK,
867 pptr);
868 parent_data_ptr++;
869 } while (!(edge_value & GRAPH_LAST_EDGE));
871 return 1;
874 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
876 struct commit_graph *cur_g = g;
877 uint32_t lex_index;
879 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
880 cur_g = cur_g->base_graph;
882 if (cur_g) {
883 *pos = lex_index + cur_g->num_commits_in_base;
884 return 1;
887 return 0;
890 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
892 uint32_t graph_pos = commit_graph_position(item);
893 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
894 *pos = graph_pos;
895 return 1;
896 } else {
897 return search_commit_pos_in_graph(&item->object.oid, g, pos);
901 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
903 struct commit *commit;
904 uint32_t pos;
906 if (!repo->objects->commit_graph)
907 return NULL;
908 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
909 return NULL;
910 if (!repo_has_object_file(repo, id))
911 return NULL;
913 commit = lookup_commit(repo, id);
914 if (!commit)
915 return NULL;
916 if (commit->object.parsed)
917 return commit;
919 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
920 return NULL;
922 return commit;
925 static int parse_commit_in_graph_one(struct repository *r,
926 struct commit_graph *g,
927 struct commit *item)
929 uint32_t pos;
931 if (item->object.parsed)
932 return 1;
934 if (find_commit_pos_in_graph(item, g, &pos))
935 return fill_commit_in_graph(r, item, g, pos);
937 return 0;
940 int parse_commit_in_graph(struct repository *r, struct commit *item)
942 static int checked_env = 0;
944 if (!checked_env &&
945 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
946 die("dying as requested by the '%s' variable on commit-graph parse!",
947 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
948 checked_env = 1;
950 if (!prepare_commit_graph(r))
951 return 0;
952 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
955 void load_commit_graph_info(struct repository *r, struct commit *item)
957 uint32_t pos;
958 if (!prepare_commit_graph(r))
959 return;
960 if (find_commit_pos_in_graph(item, r->objects->commit_graph, &pos))
961 fill_commit_graph_info(item, r->objects->commit_graph, pos);
964 static struct tree *load_tree_for_commit(struct repository *r,
965 struct commit_graph *g,
966 struct commit *c)
968 struct object_id oid;
969 const unsigned char *commit_data;
970 uint32_t graph_pos = commit_graph_position(c);
972 while (graph_pos < g->num_commits_in_base)
973 g = g->base_graph;
975 commit_data = g->chunk_commit_data +
976 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
978 oidread(&oid, commit_data);
979 set_commit_tree(c, lookup_tree(r, &oid));
981 return c->maybe_tree;
984 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
985 struct commit_graph *g,
986 const struct commit *c)
988 if (c->maybe_tree)
989 return c->maybe_tree;
990 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
991 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
993 return load_tree_for_commit(r, g, (struct commit *)c);
996 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
998 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
1001 struct packed_commit_list {
1002 struct commit **list;
1003 size_t nr;
1004 size_t alloc;
1007 struct write_commit_graph_context {
1008 struct repository *r;
1009 struct object_directory *odb;
1010 char *graph_name;
1011 struct oid_array oids;
1012 struct packed_commit_list commits;
1013 int num_extra_edges;
1014 int num_generation_data_overflows;
1015 unsigned long approx_nr_objects;
1016 struct progress *progress;
1017 int progress_done;
1018 uint64_t progress_cnt;
1020 char *base_graph_name;
1021 int num_commit_graphs_before;
1022 int num_commit_graphs_after;
1023 char **commit_graph_filenames_before;
1024 char **commit_graph_filenames_after;
1025 char **commit_graph_hash_after;
1026 uint32_t new_num_commits_in_base;
1027 struct commit_graph *new_base_graph;
1029 unsigned append:1,
1030 report_progress:1,
1031 split:1,
1032 changed_paths:1,
1033 order_by_pack:1,
1034 write_generation_data:1,
1035 trust_generation_numbers:1;
1037 struct topo_level_slab *topo_levels;
1038 const struct commit_graph_opts *opts;
1039 size_t total_bloom_filter_data_size;
1040 const struct bloom_filter_settings *bloom_settings;
1042 int count_bloom_filter_computed;
1043 int count_bloom_filter_not_computed;
1044 int count_bloom_filter_trunc_empty;
1045 int count_bloom_filter_trunc_large;
1048 static int write_graph_chunk_fanout(struct hashfile *f,
1049 void *data)
1051 struct write_commit_graph_context *ctx = data;
1052 int i, count = 0;
1053 struct commit **list = ctx->commits.list;
1056 * Write the first-level table (the list is sorted,
1057 * but we use a 256-entry lookup to be able to avoid
1058 * having to do eight extra binary search iterations).
1060 for (i = 0; i < 256; i++) {
1061 while (count < ctx->commits.nr) {
1062 if ((*list)->object.oid.hash[0] != i)
1063 break;
1064 display_progress(ctx->progress, ++ctx->progress_cnt);
1065 count++;
1066 list++;
1069 hashwrite_be32(f, count);
1072 return 0;
1075 static int write_graph_chunk_oids(struct hashfile *f,
1076 void *data)
1078 struct write_commit_graph_context *ctx = data;
1079 struct commit **list = ctx->commits.list;
1080 int count;
1081 for (count = 0; count < ctx->commits.nr; count++, list++) {
1082 display_progress(ctx->progress, ++ctx->progress_cnt);
1083 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1086 return 0;
1089 static const struct object_id *commit_to_oid(size_t index, const void *table)
1091 const struct commit * const *commits = table;
1092 return &commits[index]->object.oid;
1095 static int write_graph_chunk_data(struct hashfile *f,
1096 void *data)
1098 struct write_commit_graph_context *ctx = data;
1099 struct commit **list = ctx->commits.list;
1100 struct commit **last = ctx->commits.list + ctx->commits.nr;
1101 uint32_t num_extra_edges = 0;
1103 while (list < last) {
1104 struct commit_list *parent;
1105 struct object_id *tree;
1106 int edge_value;
1107 uint32_t packedDate[2];
1108 display_progress(ctx->progress, ++ctx->progress_cnt);
1110 if (repo_parse_commit_no_graph(ctx->r, *list))
1111 die(_("unable to parse commit %s"),
1112 oid_to_hex(&(*list)->object.oid));
1113 tree = get_commit_tree_oid(*list);
1114 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1116 parent = (*list)->parents;
1118 if (!parent)
1119 edge_value = GRAPH_PARENT_NONE;
1120 else {
1121 edge_value = oid_pos(&parent->item->object.oid,
1122 ctx->commits.list,
1123 ctx->commits.nr,
1124 commit_to_oid);
1126 if (edge_value >= 0)
1127 edge_value += ctx->new_num_commits_in_base;
1128 else if (ctx->new_base_graph) {
1129 uint32_t pos;
1130 if (find_commit_pos_in_graph(parent->item,
1131 ctx->new_base_graph,
1132 &pos))
1133 edge_value = pos;
1136 if (edge_value < 0)
1137 BUG("missing parent %s for commit %s",
1138 oid_to_hex(&parent->item->object.oid),
1139 oid_to_hex(&(*list)->object.oid));
1142 hashwrite_be32(f, edge_value);
1144 if (parent)
1145 parent = parent->next;
1147 if (!parent)
1148 edge_value = GRAPH_PARENT_NONE;
1149 else if (parent->next)
1150 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1151 else {
1152 edge_value = oid_pos(&parent->item->object.oid,
1153 ctx->commits.list,
1154 ctx->commits.nr,
1155 commit_to_oid);
1157 if (edge_value >= 0)
1158 edge_value += ctx->new_num_commits_in_base;
1159 else if (ctx->new_base_graph) {
1160 uint32_t pos;
1161 if (find_commit_pos_in_graph(parent->item,
1162 ctx->new_base_graph,
1163 &pos))
1164 edge_value = pos;
1167 if (edge_value < 0)
1168 BUG("missing parent %s for commit %s",
1169 oid_to_hex(&parent->item->object.oid),
1170 oid_to_hex(&(*list)->object.oid));
1173 hashwrite_be32(f, edge_value);
1175 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1176 do {
1177 num_extra_edges++;
1178 parent = parent->next;
1179 } while (parent);
1182 if (sizeof((*list)->date) > 4)
1183 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1184 else
1185 packedDate[0] = 0;
1187 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1189 packedDate[1] = htonl((*list)->date);
1190 hashwrite(f, packedDate, 8);
1192 list++;
1195 return 0;
1198 static int write_graph_chunk_generation_data(struct hashfile *f,
1199 void *data)
1201 struct write_commit_graph_context *ctx = data;
1202 int i, num_generation_data_overflows = 0;
1204 for (i = 0; i < ctx->commits.nr; i++) {
1205 struct commit *c = ctx->commits.list[i];
1206 timestamp_t offset;
1207 repo_parse_commit(ctx->r, c);
1208 offset = commit_graph_data_at(c)->generation - c->date;
1209 display_progress(ctx->progress, ++ctx->progress_cnt);
1211 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1212 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1213 num_generation_data_overflows++;
1216 hashwrite_be32(f, offset);
1219 return 0;
1222 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1223 void *data)
1225 struct write_commit_graph_context *ctx = data;
1226 int i;
1227 for (i = 0; i < ctx->commits.nr; i++) {
1228 struct commit *c = ctx->commits.list[i];
1229 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1230 display_progress(ctx->progress, ++ctx->progress_cnt);
1232 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1233 hashwrite_be32(f, offset >> 32);
1234 hashwrite_be32(f, (uint32_t) offset);
1238 return 0;
1241 static int write_graph_chunk_extra_edges(struct hashfile *f,
1242 void *data)
1244 struct write_commit_graph_context *ctx = data;
1245 struct commit **list = ctx->commits.list;
1246 struct commit **last = ctx->commits.list + ctx->commits.nr;
1247 struct commit_list *parent;
1249 while (list < last) {
1250 int num_parents = 0;
1252 display_progress(ctx->progress, ++ctx->progress_cnt);
1254 for (parent = (*list)->parents; num_parents < 3 && parent;
1255 parent = parent->next)
1256 num_parents++;
1258 if (num_parents <= 2) {
1259 list++;
1260 continue;
1263 /* Since num_parents > 2, this initializer is safe. */
1264 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1265 int edge_value = oid_pos(&parent->item->object.oid,
1266 ctx->commits.list,
1267 ctx->commits.nr,
1268 commit_to_oid);
1270 if (edge_value >= 0)
1271 edge_value += ctx->new_num_commits_in_base;
1272 else if (ctx->new_base_graph) {
1273 uint32_t pos;
1274 if (find_commit_pos_in_graph(parent->item,
1275 ctx->new_base_graph,
1276 &pos))
1277 edge_value = pos;
1280 if (edge_value < 0)
1281 BUG("missing parent %s for commit %s",
1282 oid_to_hex(&parent->item->object.oid),
1283 oid_to_hex(&(*list)->object.oid));
1284 else if (!parent->next)
1285 edge_value |= GRAPH_LAST_EDGE;
1287 hashwrite_be32(f, edge_value);
1290 list++;
1293 return 0;
1296 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1297 void *data)
1299 struct write_commit_graph_context *ctx = data;
1300 struct commit **list = ctx->commits.list;
1301 struct commit **last = ctx->commits.list + ctx->commits.nr;
1302 uint32_t cur_pos = 0;
1304 while (list < last) {
1305 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1306 size_t len = filter ? filter->len : 0;
1307 cur_pos += len;
1308 display_progress(ctx->progress, ++ctx->progress_cnt);
1309 hashwrite_be32(f, cur_pos);
1310 list++;
1313 return 0;
1316 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1318 struct json_writer jw = JSON_WRITER_INIT;
1320 jw_object_begin(&jw, 0);
1321 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1322 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1323 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1324 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1325 jw_end(&jw);
1327 trace2_data_json("bloom", ctx->r, "settings", &jw);
1329 jw_release(&jw);
1332 static int write_graph_chunk_bloom_data(struct hashfile *f,
1333 void *data)
1335 struct write_commit_graph_context *ctx = data;
1336 struct commit **list = ctx->commits.list;
1337 struct commit **last = ctx->commits.list + ctx->commits.nr;
1339 trace2_bloom_filter_settings(ctx);
1341 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1342 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1343 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1345 while (list < last) {
1346 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1347 size_t len = filter ? filter->len : 0;
1349 display_progress(ctx->progress, ++ctx->progress_cnt);
1350 if (len)
1351 hashwrite(f, filter->data, len * sizeof(unsigned char));
1352 list++;
1355 return 0;
1358 static int add_packed_commits(const struct object_id *oid,
1359 struct packed_git *pack,
1360 uint32_t pos,
1361 void *data)
1363 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1364 enum object_type type;
1365 off_t offset = nth_packed_object_offset(pack, pos);
1366 struct object_info oi = OBJECT_INFO_INIT;
1368 if (ctx->progress)
1369 display_progress(ctx->progress, ++ctx->progress_done);
1371 oi.typep = &type;
1372 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1373 die(_("unable to get type of object %s"), oid_to_hex(oid));
1375 if (type != OBJ_COMMIT)
1376 return 0;
1378 oid_array_append(&ctx->oids, oid);
1379 set_commit_pos(ctx->r, oid);
1381 return 0;
1384 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1386 struct commit_list *parent;
1387 for (parent = commit->parents; parent; parent = parent->next) {
1388 if (!(parent->item->object.flags & REACHABLE)) {
1389 oid_array_append(&ctx->oids, &parent->item->object.oid);
1390 parent->item->object.flags |= REACHABLE;
1395 static void close_reachable(struct write_commit_graph_context *ctx)
1397 int i;
1398 struct commit *commit;
1399 enum commit_graph_split_flags flags = ctx->opts ?
1400 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1402 if (ctx->report_progress)
1403 ctx->progress = start_delayed_progress(
1404 _("Loading known commits in commit graph"),
1405 ctx->oids.nr);
1406 for (i = 0; i < ctx->oids.nr; i++) {
1407 display_progress(ctx->progress, i + 1);
1408 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1409 if (commit)
1410 commit->object.flags |= REACHABLE;
1412 stop_progress(&ctx->progress);
1415 * As this loop runs, ctx->oids.nr may grow, but not more
1416 * than the number of missing commits in the reachable
1417 * closure.
1419 if (ctx->report_progress)
1420 ctx->progress = start_delayed_progress(
1421 _("Expanding reachable commits in commit graph"),
1423 for (i = 0; i < ctx->oids.nr; i++) {
1424 display_progress(ctx->progress, i + 1);
1425 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1427 if (!commit)
1428 continue;
1429 if (ctx->split) {
1430 if ((!repo_parse_commit(ctx->r, commit) &&
1431 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1432 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1433 add_missing_parents(ctx, commit);
1434 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1435 add_missing_parents(ctx, commit);
1437 stop_progress(&ctx->progress);
1439 if (ctx->report_progress)
1440 ctx->progress = start_delayed_progress(
1441 _("Clearing commit marks in commit graph"),
1442 ctx->oids.nr);
1443 for (i = 0; i < ctx->oids.nr; i++) {
1444 display_progress(ctx->progress, i + 1);
1445 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1447 if (commit)
1448 commit->object.flags &= ~REACHABLE;
1450 stop_progress(&ctx->progress);
1453 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1455 int i;
1456 struct commit_list *list = NULL;
1458 if (ctx->report_progress)
1459 ctx->progress = start_delayed_progress(
1460 _("Computing commit graph topological levels"),
1461 ctx->commits.nr);
1462 for (i = 0; i < ctx->commits.nr; i++) {
1463 struct commit *c = ctx->commits.list[i];
1464 uint32_t level;
1466 repo_parse_commit(ctx->r, c);
1467 level = *topo_level_slab_at(ctx->topo_levels, c);
1469 display_progress(ctx->progress, i + 1);
1470 if (level != GENERATION_NUMBER_ZERO)
1471 continue;
1473 commit_list_insert(c, &list);
1474 while (list) {
1475 struct commit *current = list->item;
1476 struct commit_list *parent;
1477 int all_parents_computed = 1;
1478 uint32_t max_level = 0;
1480 for (parent = current->parents; parent; parent = parent->next) {
1481 repo_parse_commit(ctx->r, parent->item);
1482 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1484 if (level == GENERATION_NUMBER_ZERO) {
1485 all_parents_computed = 0;
1486 commit_list_insert(parent->item, &list);
1487 break;
1490 if (level > max_level)
1491 max_level = level;
1494 if (all_parents_computed) {
1495 pop_commit(&list);
1497 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1498 max_level = GENERATION_NUMBER_V1_MAX - 1;
1499 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1503 stop_progress(&ctx->progress);
1506 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1508 int i;
1509 struct commit_list *list = NULL;
1511 if (ctx->report_progress)
1512 ctx->progress = start_delayed_progress(
1513 _("Computing commit graph generation numbers"),
1514 ctx->commits.nr);
1516 if (!ctx->trust_generation_numbers) {
1517 for (i = 0; i < ctx->commits.nr; i++) {
1518 struct commit *c = ctx->commits.list[i];
1519 repo_parse_commit(ctx->r, c);
1520 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1524 for (i = 0; i < ctx->commits.nr; i++) {
1525 struct commit *c = ctx->commits.list[i];
1526 timestamp_t corrected_commit_date;
1528 repo_parse_commit(ctx->r, c);
1529 corrected_commit_date = commit_graph_data_at(c)->generation;
1531 display_progress(ctx->progress, i + 1);
1532 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1533 continue;
1535 commit_list_insert(c, &list);
1536 while (list) {
1537 struct commit *current = list->item;
1538 struct commit_list *parent;
1539 int all_parents_computed = 1;
1540 timestamp_t max_corrected_commit_date = 0;
1542 for (parent = current->parents; parent; parent = parent->next) {
1543 repo_parse_commit(ctx->r, parent->item);
1544 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1546 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1547 all_parents_computed = 0;
1548 commit_list_insert(parent->item, &list);
1549 break;
1552 if (corrected_commit_date > max_corrected_commit_date)
1553 max_corrected_commit_date = corrected_commit_date;
1556 if (all_parents_computed) {
1557 pop_commit(&list);
1559 if (current->date && current->date > max_corrected_commit_date)
1560 max_corrected_commit_date = current->date - 1;
1561 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1566 for (i = 0; i < ctx->commits.nr; i++) {
1567 struct commit *c = ctx->commits.list[i];
1568 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1569 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1570 ctx->num_generation_data_overflows++;
1572 stop_progress(&ctx->progress);
1575 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1577 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1578 ctx->count_bloom_filter_computed);
1579 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1580 ctx->count_bloom_filter_not_computed);
1581 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1582 ctx->count_bloom_filter_trunc_empty);
1583 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1584 ctx->count_bloom_filter_trunc_large);
1587 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1589 int i;
1590 struct progress *progress = NULL;
1591 struct commit **sorted_commits;
1592 int max_new_filters;
1594 init_bloom_filters();
1596 if (ctx->report_progress)
1597 progress = start_delayed_progress(
1598 _("Computing commit changed paths Bloom filters"),
1599 ctx->commits.nr);
1601 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1602 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1604 if (ctx->order_by_pack)
1605 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1606 else
1607 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1609 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1610 ctx->opts->max_new_filters : ctx->commits.nr;
1612 for (i = 0; i < ctx->commits.nr; i++) {
1613 enum bloom_filter_computed computed = 0;
1614 struct commit *c = sorted_commits[i];
1615 struct bloom_filter *filter = get_or_compute_bloom_filter(
1616 ctx->r,
1618 ctx->count_bloom_filter_computed < max_new_filters,
1619 ctx->bloom_settings,
1620 &computed);
1621 if (computed & BLOOM_COMPUTED) {
1622 ctx->count_bloom_filter_computed++;
1623 if (computed & BLOOM_TRUNC_EMPTY)
1624 ctx->count_bloom_filter_trunc_empty++;
1625 if (computed & BLOOM_TRUNC_LARGE)
1626 ctx->count_bloom_filter_trunc_large++;
1627 } else if (computed & BLOOM_NOT_COMPUTED)
1628 ctx->count_bloom_filter_not_computed++;
1629 ctx->total_bloom_filter_data_size += filter
1630 ? sizeof(unsigned char) * filter->len : 0;
1631 display_progress(progress, i + 1);
1634 if (trace2_is_enabled())
1635 trace2_bloom_filter_write_statistics(ctx);
1637 free(sorted_commits);
1638 stop_progress(&progress);
1641 struct refs_cb_data {
1642 struct oidset *commits;
1643 struct progress *progress;
1646 static int add_ref_to_set(const char *refname,
1647 const struct object_id *oid,
1648 int flags, void *cb_data)
1650 struct object_id peeled;
1651 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1653 if (!peel_iterated_oid(oid, &peeled))
1654 oid = &peeled;
1655 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1656 oidset_insert(data->commits, oid);
1658 display_progress(data->progress, oidset_size(data->commits));
1660 return 0;
1663 int write_commit_graph_reachable(struct object_directory *odb,
1664 enum commit_graph_write_flags flags,
1665 const struct commit_graph_opts *opts)
1667 struct oidset commits = OIDSET_INIT;
1668 struct refs_cb_data data;
1669 int result;
1671 memset(&data, 0, sizeof(data));
1672 data.commits = &commits;
1673 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1674 data.progress = start_delayed_progress(
1675 _("Collecting referenced commits"), 0);
1677 for_each_ref(add_ref_to_set, &data);
1679 stop_progress(&data.progress);
1681 result = write_commit_graph(odb, NULL, &commits,
1682 flags, opts);
1684 oidset_clear(&commits);
1685 return result;
1688 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1689 const struct string_list *pack_indexes)
1691 uint32_t i;
1692 struct strbuf progress_title = STRBUF_INIT;
1693 struct strbuf packname = STRBUF_INIT;
1694 int dirlen;
1695 int ret = 0;
1697 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1698 dirlen = packname.len;
1699 if (ctx->report_progress) {
1700 strbuf_addf(&progress_title,
1701 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1702 "Finding commits for commit graph in %"PRIuMAX" packs",
1703 pack_indexes->nr),
1704 (uintmax_t)pack_indexes->nr);
1705 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1706 ctx->progress_done = 0;
1708 for (i = 0; i < pack_indexes->nr; i++) {
1709 struct packed_git *p;
1710 strbuf_setlen(&packname, dirlen);
1711 strbuf_addstr(&packname, pack_indexes->items[i].string);
1712 p = add_packed_git(packname.buf, packname.len, 1);
1713 if (!p) {
1714 ret = error(_("error adding pack %s"), packname.buf);
1715 goto cleanup;
1717 if (open_pack_index(p)) {
1718 ret = error(_("error opening index for %s"), packname.buf);
1719 goto cleanup;
1721 for_each_object_in_pack(p, add_packed_commits, ctx,
1722 FOR_EACH_OBJECT_PACK_ORDER);
1723 close_pack(p);
1724 free(p);
1727 cleanup:
1728 stop_progress(&ctx->progress);
1729 strbuf_release(&progress_title);
1730 strbuf_release(&packname);
1732 return ret;
1735 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1736 struct oidset *commits)
1738 struct oidset_iter iter;
1739 struct object_id *oid;
1741 if (!oidset_size(commits))
1742 return 0;
1744 oidset_iter_init(commits, &iter);
1745 while ((oid = oidset_iter_next(&iter))) {
1746 oid_array_append(&ctx->oids, oid);
1749 return 0;
1752 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1754 if (ctx->report_progress)
1755 ctx->progress = start_delayed_progress(
1756 _("Finding commits for commit graph among packed objects"),
1757 ctx->approx_nr_objects);
1758 for_each_packed_object(add_packed_commits, ctx,
1759 FOR_EACH_OBJECT_PACK_ORDER);
1760 if (ctx->progress_done < ctx->approx_nr_objects)
1761 display_progress(ctx->progress, ctx->approx_nr_objects);
1762 stop_progress(&ctx->progress);
1765 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1767 uint32_t i;
1768 enum commit_graph_split_flags flags = ctx->opts ?
1769 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1771 ctx->num_extra_edges = 0;
1772 if (ctx->report_progress)
1773 ctx->progress = start_delayed_progress(
1774 _("Finding extra edges in commit graph"),
1775 ctx->oids.nr);
1776 oid_array_sort(&ctx->oids);
1777 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1778 unsigned int num_parents;
1780 display_progress(ctx->progress, i + 1);
1782 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1783 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1785 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1786 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1787 continue;
1789 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1790 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1791 else
1792 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1794 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1795 if (num_parents > 2)
1796 ctx->num_extra_edges += num_parents - 1;
1798 ctx->commits.nr++;
1800 stop_progress(&ctx->progress);
1803 static int write_graph_chunk_base_1(struct hashfile *f,
1804 struct commit_graph *g)
1806 int num = 0;
1808 if (!g)
1809 return 0;
1811 num = write_graph_chunk_base_1(f, g->base_graph);
1812 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1813 return num + 1;
1816 static int write_graph_chunk_base(struct hashfile *f,
1817 void *data)
1819 struct write_commit_graph_context *ctx = data;
1820 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1822 if (num != ctx->num_commit_graphs_after - 1) {
1823 error(_("failed to write correct number of base graph ids"));
1824 return -1;
1827 return 0;
1830 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1832 uint32_t i;
1833 int fd;
1834 struct hashfile *f;
1835 struct lock_file lk = LOCK_INIT;
1836 const unsigned hashsz = the_hash_algo->rawsz;
1837 struct strbuf progress_title = STRBUF_INIT;
1838 struct chunkfile *cf;
1839 unsigned char file_hash[GIT_MAX_RAWSZ];
1841 if (ctx->split) {
1842 struct strbuf tmp_file = STRBUF_INIT;
1844 strbuf_addf(&tmp_file,
1845 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1846 ctx->odb->path);
1847 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1848 } else {
1849 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1852 if (safe_create_leading_directories(ctx->graph_name)) {
1853 UNLEAK(ctx->graph_name);
1854 error(_("unable to create leading directories of %s"),
1855 ctx->graph_name);
1856 return -1;
1859 if (ctx->split) {
1860 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1862 hold_lock_file_for_update_mode(&lk, lock_name,
1863 LOCK_DIE_ON_ERROR, 0444);
1864 free(lock_name);
1866 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1867 if (fd < 0) {
1868 error(_("unable to create temporary graph layer"));
1869 return -1;
1872 if (adjust_shared_perm(ctx->graph_name)) {
1873 error(_("unable to adjust shared permissions for '%s'"),
1874 ctx->graph_name);
1875 return -1;
1878 f = hashfd(fd, ctx->graph_name);
1879 } else {
1880 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1881 LOCK_DIE_ON_ERROR, 0444);
1882 fd = get_lock_file_fd(&lk);
1883 f = hashfd(fd, get_lock_file_path(&lk));
1886 cf = init_chunkfile(f);
1888 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1889 write_graph_chunk_fanout);
1890 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1891 write_graph_chunk_oids);
1892 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1893 write_graph_chunk_data);
1895 if (ctx->write_generation_data)
1896 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1897 sizeof(uint32_t) * ctx->commits.nr,
1898 write_graph_chunk_generation_data);
1899 if (ctx->num_generation_data_overflows)
1900 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1901 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1902 write_graph_chunk_generation_data_overflow);
1903 if (ctx->num_extra_edges)
1904 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1905 4 * ctx->num_extra_edges,
1906 write_graph_chunk_extra_edges);
1907 if (ctx->changed_paths) {
1908 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1909 sizeof(uint32_t) * ctx->commits.nr,
1910 write_graph_chunk_bloom_indexes);
1911 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1912 sizeof(uint32_t) * 3
1913 + ctx->total_bloom_filter_data_size,
1914 write_graph_chunk_bloom_data);
1916 if (ctx->num_commit_graphs_after > 1)
1917 add_chunk(cf, GRAPH_CHUNKID_BASE,
1918 hashsz * (ctx->num_commit_graphs_after - 1),
1919 write_graph_chunk_base);
1921 hashwrite_be32(f, GRAPH_SIGNATURE);
1923 hashwrite_u8(f, GRAPH_VERSION);
1924 hashwrite_u8(f, oid_version());
1925 hashwrite_u8(f, get_num_chunks(cf));
1926 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1928 if (ctx->report_progress) {
1929 strbuf_addf(&progress_title,
1930 Q_("Writing out commit graph in %d pass",
1931 "Writing out commit graph in %d passes",
1932 get_num_chunks(cf)),
1933 get_num_chunks(cf));
1934 ctx->progress = start_delayed_progress(
1935 progress_title.buf,
1936 get_num_chunks(cf) * ctx->commits.nr);
1939 write_chunkfile(cf, ctx);
1941 stop_progress(&ctx->progress);
1942 strbuf_release(&progress_title);
1944 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1945 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1946 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1948 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1949 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1950 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1951 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1954 close_commit_graph(ctx->r->objects);
1955 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1956 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1957 free_chunkfile(cf);
1959 if (ctx->split) {
1960 FILE *chainf = fdopen_lock_file(&lk, "w");
1961 char *final_graph_name;
1962 int result;
1964 close(fd);
1966 if (!chainf) {
1967 error(_("unable to open commit-graph chain file"));
1968 return -1;
1971 if (ctx->base_graph_name) {
1972 const char *dest;
1973 int idx = ctx->num_commit_graphs_after - 1;
1974 if (ctx->num_commit_graphs_after > 1)
1975 idx--;
1977 dest = ctx->commit_graph_filenames_after[idx];
1979 if (strcmp(ctx->base_graph_name, dest)) {
1980 result = rename(ctx->base_graph_name, dest);
1982 if (result) {
1983 error(_("failed to rename base commit-graph file"));
1984 return -1;
1987 } else {
1988 char *graph_name = get_commit_graph_filename(ctx->odb);
1989 unlink(graph_name);
1990 free(graph_name);
1993 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1994 final_graph_name = get_split_graph_filename(ctx->odb,
1995 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1996 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1998 result = rename(ctx->graph_name, final_graph_name);
2000 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2001 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2003 if (result) {
2004 error(_("failed to rename temporary commit-graph file"));
2005 return -1;
2009 commit_lock_file(&lk);
2011 return 0;
2014 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2016 struct commit_graph *g;
2017 uint32_t num_commits;
2018 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2019 uint32_t i;
2021 int max_commits = 0;
2022 int size_mult = 2;
2024 if (ctx->opts) {
2025 max_commits = ctx->opts->max_commits;
2027 if (ctx->opts->size_multiple)
2028 size_mult = ctx->opts->size_multiple;
2030 flags = ctx->opts->split_flags;
2033 g = ctx->r->objects->commit_graph;
2034 num_commits = ctx->commits.nr;
2035 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2036 ctx->num_commit_graphs_after = 1;
2037 else
2038 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2040 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2041 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2042 while (g && (g->num_commits <= size_mult * num_commits ||
2043 (max_commits && num_commits > max_commits))) {
2044 if (g->odb != ctx->odb)
2045 break;
2047 num_commits += g->num_commits;
2048 g = g->base_graph;
2050 ctx->num_commit_graphs_after--;
2054 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2055 ctx->new_base_graph = g;
2056 else if (ctx->num_commit_graphs_after != 1)
2057 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2058 "should be 1 with --split=replace");
2060 if (ctx->num_commit_graphs_after == 2) {
2061 char *old_graph_name = get_commit_graph_filename(g->odb);
2063 if (!strcmp(g->filename, old_graph_name) &&
2064 g->odb != ctx->odb) {
2065 ctx->num_commit_graphs_after = 1;
2066 ctx->new_base_graph = NULL;
2069 free(old_graph_name);
2072 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2073 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2075 for (i = 0; i < ctx->num_commit_graphs_after &&
2076 i < ctx->num_commit_graphs_before; i++)
2077 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2079 i = ctx->num_commit_graphs_before - 1;
2080 g = ctx->r->objects->commit_graph;
2082 while (g) {
2083 if (i < ctx->num_commit_graphs_after)
2084 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2087 * If the topmost remaining layer has generation data chunk, the
2088 * resultant layer also has generation data chunk.
2090 if (i == ctx->num_commit_graphs_after - 2)
2091 ctx->write_generation_data = !!g->chunk_generation_data;
2093 i--;
2094 g = g->base_graph;
2098 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2099 struct commit_graph *g)
2101 uint32_t i;
2102 uint32_t offset = g->num_commits_in_base;
2104 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2106 for (i = 0; i < g->num_commits; i++) {
2107 struct object_id oid;
2108 struct commit *result;
2110 display_progress(ctx->progress, i + 1);
2112 load_oid_from_graph(g, i + offset, &oid);
2114 /* only add commits if they still exist in the repo */
2115 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2117 if (result) {
2118 ctx->commits.list[ctx->commits.nr] = result;
2119 ctx->commits.nr++;
2124 static int commit_compare(const void *_a, const void *_b)
2126 const struct commit *a = *(const struct commit **)_a;
2127 const struct commit *b = *(const struct commit **)_b;
2128 return oidcmp(&a->object.oid, &b->object.oid);
2131 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2133 uint32_t i, dedup_i = 0;
2135 if (ctx->report_progress)
2136 ctx->progress = start_delayed_progress(
2137 _("Scanning merged commits"),
2138 ctx->commits.nr);
2140 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2142 ctx->num_extra_edges = 0;
2143 for (i = 0; i < ctx->commits.nr; i++) {
2144 display_progress(ctx->progress, i + 1);
2146 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2147 &ctx->commits.list[i]->object.oid)) {
2149 * Silently ignore duplicates. These were likely
2150 * created due to a commit appearing in multiple
2151 * layers of the chain, which is unexpected but
2152 * not invalid. We should make sure there is a
2153 * unique copy in the new layer.
2155 } else {
2156 unsigned int num_parents;
2158 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2159 dedup_i++;
2161 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2162 if (num_parents > 2)
2163 ctx->num_extra_edges += num_parents - 1;
2167 ctx->commits.nr = dedup_i;
2169 stop_progress(&ctx->progress);
2172 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2174 struct commit_graph *g = ctx->r->objects->commit_graph;
2175 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2177 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2178 current_graph_number--;
2180 if (ctx->report_progress)
2181 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2183 merge_commit_graph(ctx, g);
2184 stop_progress(&ctx->progress);
2186 g = g->base_graph;
2189 if (g) {
2190 ctx->new_base_graph = g;
2191 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2194 if (ctx->new_base_graph)
2195 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2197 sort_and_scan_merged_commits(ctx);
2200 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2202 uint32_t i;
2203 time_t now = time(NULL);
2205 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2206 struct stat st;
2207 struct utimbuf updated_time;
2209 stat(ctx->commit_graph_filenames_before[i], &st);
2211 updated_time.actime = st.st_atime;
2212 updated_time.modtime = now;
2213 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2217 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2219 struct strbuf path = STRBUF_INIT;
2220 DIR *dir;
2221 struct dirent *de;
2222 size_t dirnamelen;
2223 timestamp_t expire_time = time(NULL);
2225 if (ctx->opts && ctx->opts->expire_time)
2226 expire_time = ctx->opts->expire_time;
2227 if (!ctx->split) {
2228 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2229 unlink(chain_file_name);
2230 free(chain_file_name);
2231 ctx->num_commit_graphs_after = 0;
2234 strbuf_addstr(&path, ctx->odb->path);
2235 strbuf_addstr(&path, "/info/commit-graphs");
2236 dir = opendir(path.buf);
2238 if (!dir)
2239 goto out;
2241 strbuf_addch(&path, '/');
2242 dirnamelen = path.len;
2243 while ((de = readdir(dir)) != NULL) {
2244 struct stat st;
2245 uint32_t i, found = 0;
2247 strbuf_setlen(&path, dirnamelen);
2248 strbuf_addstr(&path, de->d_name);
2250 stat(path.buf, &st);
2252 if (st.st_mtime > expire_time)
2253 continue;
2254 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2255 continue;
2257 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2258 if (!strcmp(ctx->commit_graph_filenames_after[i],
2259 path.buf)) {
2260 found = 1;
2261 break;
2265 if (!found)
2266 unlink(path.buf);
2269 out:
2270 strbuf_release(&path);
2273 int write_commit_graph(struct object_directory *odb,
2274 const struct string_list *const pack_indexes,
2275 struct oidset *commits,
2276 enum commit_graph_write_flags flags,
2277 const struct commit_graph_opts *opts)
2279 struct repository *r = the_repository;
2280 struct write_commit_graph_context *ctx;
2281 uint32_t i;
2282 int res = 0;
2283 int replace = 0;
2284 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2285 struct topo_level_slab topo_levels;
2287 prepare_repo_settings(r);
2288 if (!r->settings.core_commit_graph) {
2289 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2290 return 0;
2292 if (!commit_graph_compatible(r))
2293 return 0;
2295 CALLOC_ARRAY(ctx, 1);
2296 ctx->r = r;
2297 ctx->odb = odb;
2298 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2299 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2300 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2301 ctx->opts = opts;
2302 ctx->total_bloom_filter_data_size = 0;
2303 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2304 ctx->num_generation_data_overflows = 0;
2306 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2307 bloom_settings.bits_per_entry);
2308 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2309 bloom_settings.num_hashes);
2310 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2311 bloom_settings.max_changed_paths);
2312 ctx->bloom_settings = &bloom_settings;
2314 init_topo_level_slab(&topo_levels);
2315 ctx->topo_levels = &topo_levels;
2317 prepare_commit_graph(ctx->r);
2318 if (ctx->r->objects->commit_graph) {
2319 struct commit_graph *g = ctx->r->objects->commit_graph;
2321 while (g) {
2322 g->topo_levels = &topo_levels;
2323 g = g->base_graph;
2327 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2328 ctx->changed_paths = 1;
2329 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2330 struct commit_graph *g;
2332 g = ctx->r->objects->commit_graph;
2334 /* We have changed-paths already. Keep them in the next graph */
2335 if (g && g->chunk_bloom_data) {
2336 ctx->changed_paths = 1;
2337 ctx->bloom_settings = g->bloom_filter_settings;
2341 if (ctx->split) {
2342 struct commit_graph *g = ctx->r->objects->commit_graph;
2344 while (g) {
2345 ctx->num_commit_graphs_before++;
2346 g = g->base_graph;
2349 if (ctx->num_commit_graphs_before) {
2350 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2351 i = ctx->num_commit_graphs_before;
2352 g = ctx->r->objects->commit_graph;
2354 while (g) {
2355 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2356 g = g->base_graph;
2360 if (ctx->opts)
2361 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2364 ctx->approx_nr_objects = approximate_object_count();
2366 if (ctx->append && ctx->r->objects->commit_graph) {
2367 struct commit_graph *g = ctx->r->objects->commit_graph;
2368 for (i = 0; i < g->num_commits; i++) {
2369 struct object_id oid;
2370 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2371 oid_array_append(&ctx->oids, &oid);
2375 if (pack_indexes) {
2376 ctx->order_by_pack = 1;
2377 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2378 goto cleanup;
2381 if (commits) {
2382 if ((res = fill_oids_from_commits(ctx, commits)))
2383 goto cleanup;
2386 if (!pack_indexes && !commits) {
2387 ctx->order_by_pack = 1;
2388 fill_oids_from_all_packs(ctx);
2391 close_reachable(ctx);
2393 copy_oids_to_commits(ctx);
2395 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2396 error(_("too many commits to write graph"));
2397 res = -1;
2398 goto cleanup;
2401 if (!ctx->commits.nr && !replace)
2402 goto cleanup;
2404 if (ctx->split) {
2405 split_graph_merge_strategy(ctx);
2407 if (!replace)
2408 merge_commit_graphs(ctx);
2409 } else
2410 ctx->num_commit_graphs_after = 1;
2412 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2414 compute_topological_levels(ctx);
2415 if (ctx->write_generation_data)
2416 compute_generation_numbers(ctx);
2418 if (ctx->changed_paths)
2419 compute_bloom_filters(ctx);
2421 res = write_commit_graph_file(ctx);
2423 if (ctx->split)
2424 mark_commit_graphs(ctx);
2426 expire_commit_graphs(ctx);
2428 cleanup:
2429 free(ctx->graph_name);
2430 free(ctx->commits.list);
2431 oid_array_clear(&ctx->oids);
2432 clear_topo_level_slab(&topo_levels);
2434 if (ctx->commit_graph_filenames_after) {
2435 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2436 free(ctx->commit_graph_filenames_after[i]);
2437 free(ctx->commit_graph_hash_after[i]);
2440 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2441 free(ctx->commit_graph_filenames_before[i]);
2443 free(ctx->commit_graph_filenames_after);
2444 free(ctx->commit_graph_filenames_before);
2445 free(ctx->commit_graph_hash_after);
2448 free(ctx);
2450 return res;
2453 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2454 static int verify_commit_graph_error;
2456 __attribute__((format (printf, 1, 2)))
2457 static void graph_report(const char *fmt, ...)
2459 va_list ap;
2461 verify_commit_graph_error = 1;
2462 va_start(ap, fmt);
2463 vfprintf(stderr, fmt, ap);
2464 fprintf(stderr, "\n");
2465 va_end(ap);
2468 #define GENERATION_ZERO_EXISTS 1
2469 #define GENERATION_NUMBER_EXISTS 2
2471 static int commit_graph_checksum_valid(struct commit_graph *g)
2473 return hashfile_checksum_valid(g->data, g->data_len);
2476 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2478 uint32_t i, cur_fanout_pos = 0;
2479 struct object_id prev_oid, cur_oid;
2480 int generation_zero = 0;
2481 struct progress *progress = NULL;
2482 int local_error = 0;
2484 if (!g) {
2485 graph_report("no commit-graph file loaded");
2486 return 1;
2489 verify_commit_graph_error = verify_commit_graph_lite(g);
2490 if (verify_commit_graph_error)
2491 return verify_commit_graph_error;
2493 if (!commit_graph_checksum_valid(g)) {
2494 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2495 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2498 for (i = 0; i < g->num_commits; i++) {
2499 struct commit *graph_commit;
2501 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2503 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2504 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2505 oid_to_hex(&prev_oid),
2506 oid_to_hex(&cur_oid));
2508 oidcpy(&prev_oid, &cur_oid);
2510 while (cur_oid.hash[0] > cur_fanout_pos) {
2511 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2513 if (i != fanout_value)
2514 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2515 cur_fanout_pos, fanout_value, i);
2516 cur_fanout_pos++;
2519 graph_commit = lookup_commit(r, &cur_oid);
2520 if (!parse_commit_in_graph_one(r, g, graph_commit))
2521 graph_report(_("failed to parse commit %s from commit-graph"),
2522 oid_to_hex(&cur_oid));
2525 while (cur_fanout_pos < 256) {
2526 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2528 if (g->num_commits != fanout_value)
2529 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2530 cur_fanout_pos, fanout_value, i);
2532 cur_fanout_pos++;
2535 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2536 return verify_commit_graph_error;
2538 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2539 progress = start_progress(_("Verifying commits in commit graph"),
2540 g->num_commits);
2542 for (i = 0; i < g->num_commits; i++) {
2543 struct commit *graph_commit, *odb_commit;
2544 struct commit_list *graph_parents, *odb_parents;
2545 timestamp_t max_generation = 0;
2546 timestamp_t generation;
2548 display_progress(progress, i + 1);
2549 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2551 graph_commit = lookup_commit(r, &cur_oid);
2552 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2553 if (parse_commit_internal(odb_commit, 0, 0)) {
2554 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2555 oid_to_hex(&cur_oid));
2556 continue;
2559 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2560 get_commit_tree_oid(odb_commit)))
2561 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2562 oid_to_hex(&cur_oid),
2563 oid_to_hex(get_commit_tree_oid(graph_commit)),
2564 oid_to_hex(get_commit_tree_oid(odb_commit)));
2566 graph_parents = graph_commit->parents;
2567 odb_parents = odb_commit->parents;
2569 while (graph_parents) {
2570 if (odb_parents == NULL) {
2571 graph_report(_("commit-graph parent list for commit %s is too long"),
2572 oid_to_hex(&cur_oid));
2573 break;
2576 /* parse parent in case it is in a base graph */
2577 parse_commit_in_graph_one(r, g, graph_parents->item);
2579 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2580 graph_report(_("commit-graph parent for %s is %s != %s"),
2581 oid_to_hex(&cur_oid),
2582 oid_to_hex(&graph_parents->item->object.oid),
2583 oid_to_hex(&odb_parents->item->object.oid));
2585 generation = commit_graph_generation(graph_parents->item);
2586 if (generation > max_generation)
2587 max_generation = generation;
2589 graph_parents = graph_parents->next;
2590 odb_parents = odb_parents->next;
2593 if (odb_parents != NULL)
2594 graph_report(_("commit-graph parent list for commit %s terminates early"),
2595 oid_to_hex(&cur_oid));
2597 if (!commit_graph_generation(graph_commit)) {
2598 if (generation_zero == GENERATION_NUMBER_EXISTS)
2599 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2600 oid_to_hex(&cur_oid));
2601 generation_zero = GENERATION_ZERO_EXISTS;
2602 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2603 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2604 oid_to_hex(&cur_oid));
2606 if (generation_zero == GENERATION_ZERO_EXISTS)
2607 continue;
2610 * If we are using topological level and one of our parents has
2611 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2612 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2613 * in the following condition.
2615 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2616 max_generation--;
2618 generation = commit_graph_generation(graph_commit);
2619 if (generation < max_generation + 1)
2620 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2621 oid_to_hex(&cur_oid),
2622 generation,
2623 max_generation + 1);
2625 if (graph_commit->date != odb_commit->date)
2626 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2627 oid_to_hex(&cur_oid),
2628 graph_commit->date,
2629 odb_commit->date);
2631 stop_progress(&progress);
2633 local_error = verify_commit_graph_error;
2635 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2636 local_error |= verify_commit_graph(r, g->base_graph, flags);
2638 return local_error;
2641 void free_commit_graph(struct commit_graph *g)
2643 if (!g)
2644 return;
2645 if (g->data) {
2646 munmap((void *)g->data, g->data_len);
2647 g->data = NULL;
2649 free(g->filename);
2650 free(g->bloom_filter_settings);
2651 free(g);
2654 void disable_commit_graph(struct repository *r)
2656 r->commit_graph_disabled = 1;