Merge branch 'tb/pack-revindex-on-disk'
[git/debian.git] / commit-graph.c
blobca025ce8eb561181fb450e607c50cf3d9dc448cb
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 0x47444154 /* "GDAT" */
43 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f56 /* "GDOV" */
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 uint32_t commit_graph_position(const struct commit *c)
101 struct commit_graph_data *data =
102 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
104 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
107 timestamp_t commit_graph_generation(const struct commit *c)
109 struct commit_graph_data *data =
110 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
112 if (!data)
113 return GENERATION_NUMBER_INFINITY;
114 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
115 return GENERATION_NUMBER_INFINITY;
117 return data->generation;
120 static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
122 unsigned int i, nth_slab;
123 struct commit_graph_data *data =
124 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
126 if (data)
127 return data;
129 nth_slab = c->index / commit_graph_data_slab.slab_size;
130 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
133 * commit-slab initializes elements with zero, overwrite this with
134 * COMMIT_NOT_FROM_GRAPH for graph_pos.
136 * We avoid initializing generation with checking if graph position
137 * is not COMMIT_NOT_FROM_GRAPH.
139 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
140 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
141 COMMIT_NOT_FROM_GRAPH;
144 return data;
148 * Should be used only while writing commit-graph as it compares
149 * generation value of commits by directly accessing commit-slab.
151 static int commit_gen_cmp(const void *va, const void *vb)
153 const struct commit *a = *(const struct commit **)va;
154 const struct commit *b = *(const struct commit **)vb;
156 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
157 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
158 /* lower generation commits first */
159 if (generation_a < generation_b)
160 return -1;
161 else if (generation_a > generation_b)
162 return 1;
164 /* use date as a heuristic when generations are equal */
165 if (a->date < b->date)
166 return -1;
167 else if (a->date > b->date)
168 return 1;
169 return 0;
172 char *get_commit_graph_filename(struct object_directory *obj_dir)
174 return xstrfmt("%s/info/commit-graph", obj_dir->path);
177 static char *get_split_graph_filename(struct object_directory *odb,
178 const char *oid_hex)
180 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
181 oid_hex);
184 char *get_commit_graph_chain_filename(struct object_directory *odb)
186 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
189 static uint8_t oid_version(void)
191 switch (hash_algo_by_ptr(the_hash_algo)) {
192 case GIT_HASH_SHA1:
193 return 1;
194 case GIT_HASH_SHA256:
195 return 2;
196 default:
197 die(_("invalid hash version"));
201 static struct commit_graph *alloc_commit_graph(void)
203 struct commit_graph *g = xcalloc(1, sizeof(*g));
205 return g;
208 extern int read_replace_refs;
210 static int commit_graph_compatible(struct repository *r)
212 if (!r->gitdir)
213 return 0;
215 if (read_replace_refs) {
216 prepare_replace_object(r);
217 if (hashmap_get_size(&r->objects->replace_map->map))
218 return 0;
221 prepare_commit_graft(r);
222 if (r->parsed_objects &&
223 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
224 return 0;
225 if (is_repository_shallow(r))
226 return 0;
228 return 1;
231 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
233 *fd = git_open(graph_file);
234 if (*fd < 0)
235 return 0;
236 if (fstat(*fd, st)) {
237 close(*fd);
238 return 0;
240 return 1;
243 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
244 int fd, struct stat *st,
245 struct object_directory *odb)
247 void *graph_map;
248 size_t graph_size;
249 struct commit_graph *ret;
251 graph_size = xsize_t(st->st_size);
253 if (graph_size < GRAPH_MIN_SIZE) {
254 close(fd);
255 error(_("commit-graph file is too small"));
256 return NULL;
258 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
259 close(fd);
260 ret = parse_commit_graph(r, graph_map, graph_size);
262 if (ret)
263 ret->odb = odb;
264 else
265 munmap(graph_map, graph_size);
267 return ret;
270 static int verify_commit_graph_lite(struct commit_graph *g)
273 * Basic validation shared between parse_commit_graph()
274 * which'll be called every time the graph is used, and the
275 * much more expensive verify_commit_graph() used by
276 * "commit-graph verify".
278 * There should only be very basic checks here to ensure that
279 * we don't e.g. segfault in fill_commit_in_graph(), but
280 * because this is a very hot codepath nothing that e.g. loops
281 * over g->num_commits, or runs a checksum on the commit-graph
282 * itself.
284 if (!g->chunk_oid_fanout) {
285 error("commit-graph is missing the OID Fanout chunk");
286 return 1;
288 if (!g->chunk_oid_lookup) {
289 error("commit-graph is missing the OID Lookup chunk");
290 return 1;
292 if (!g->chunk_commit_data) {
293 error("commit-graph is missing the Commit Data chunk");
294 return 1;
297 return 0;
300 static int graph_read_oid_lookup(const unsigned char *chunk_start,
301 size_t chunk_size, void *data)
303 struct commit_graph *g = data;
304 g->chunk_oid_lookup = chunk_start;
305 g->num_commits = chunk_size / g->hash_len;
306 return 0;
309 static int graph_read_bloom_data(const unsigned char *chunk_start,
310 size_t chunk_size, void *data)
312 struct commit_graph *g = data;
313 uint32_t hash_version;
314 g->chunk_bloom_data = chunk_start;
315 hash_version = get_be32(chunk_start);
317 if (hash_version != 1)
318 return 0;
320 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
321 g->bloom_filter_settings->hash_version = hash_version;
322 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
323 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
324 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
326 return 0;
329 struct commit_graph *parse_commit_graph(struct repository *r,
330 void *graph_map, size_t graph_size)
332 const unsigned char *data;
333 struct commit_graph *graph;
334 uint32_t graph_signature;
335 unsigned char graph_version, hash_version;
336 struct chunkfile *cf = NULL;
338 if (!graph_map)
339 return NULL;
341 if (graph_size < GRAPH_MIN_SIZE)
342 return NULL;
344 data = (const unsigned char *)graph_map;
346 graph_signature = get_be32(data);
347 if (graph_signature != GRAPH_SIGNATURE) {
348 error(_("commit-graph signature %X does not match signature %X"),
349 graph_signature, GRAPH_SIGNATURE);
350 return NULL;
353 graph_version = *(unsigned char*)(data + 4);
354 if (graph_version != GRAPH_VERSION) {
355 error(_("commit-graph version %X does not match version %X"),
356 graph_version, GRAPH_VERSION);
357 return NULL;
360 hash_version = *(unsigned char*)(data + 5);
361 if (hash_version != oid_version()) {
362 error(_("commit-graph hash version %X does not match version %X"),
363 hash_version, oid_version());
364 return NULL;
367 prepare_repo_settings(r);
369 graph = alloc_commit_graph();
371 graph->hash_len = the_hash_algo->rawsz;
372 graph->num_chunks = *(unsigned char*)(data + 6);
373 graph->data = graph_map;
374 graph->data_len = graph_size;
376 if (graph_size < GRAPH_HEADER_SIZE +
377 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
378 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
379 error(_("commit-graph file is too small to hold %u chunks"),
380 graph->num_chunks);
381 free(graph);
382 return NULL;
385 cf = init_chunkfile(NULL);
387 if (read_table_of_contents(cf, graph->data, graph_size,
388 GRAPH_HEADER_SIZE, graph->num_chunks))
389 goto free_and_return;
391 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
392 (const unsigned char **)&graph->chunk_oid_fanout);
393 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
394 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
395 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
396 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
397 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
398 &graph->chunk_generation_data);
399 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
400 &graph->chunk_generation_data_overflow);
402 if (r->settings.commit_graph_read_changed_paths) {
403 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
404 &graph->chunk_bloom_indexes);
405 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
406 graph_read_bloom_data, graph);
409 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
410 init_bloom_filters();
411 } else {
412 /* We need both the bloom chunks to exist together. Else ignore the data */
413 graph->chunk_bloom_indexes = NULL;
414 graph->chunk_bloom_data = NULL;
415 FREE_AND_NULL(graph->bloom_filter_settings);
418 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
420 if (verify_commit_graph_lite(graph))
421 goto free_and_return;
423 free_chunkfile(cf);
424 return graph;
426 free_and_return:
427 free_chunkfile(cf);
428 free(graph->bloom_filter_settings);
429 free(graph);
430 return NULL;
433 static struct commit_graph *load_commit_graph_one(struct repository *r,
434 const char *graph_file,
435 struct object_directory *odb)
438 struct stat st;
439 int fd;
440 struct commit_graph *g;
441 int open_ok = open_commit_graph(graph_file, &fd, &st);
443 if (!open_ok)
444 return NULL;
446 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
448 if (g)
449 g->filename = xstrdup(graph_file);
451 return g;
454 static struct commit_graph *load_commit_graph_v1(struct repository *r,
455 struct object_directory *odb)
457 char *graph_name = get_commit_graph_filename(odb);
458 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
459 free(graph_name);
461 return g;
464 static int add_graph_to_chain(struct commit_graph *g,
465 struct commit_graph *chain,
466 struct object_id *oids,
467 int n)
469 struct commit_graph *cur_g = chain;
471 if (n && !g->chunk_base_graphs) {
472 warning(_("commit-graph has no base graphs chunk"));
473 return 0;
476 while (n) {
477 n--;
479 if (!cur_g ||
480 !oideq(&oids[n], &cur_g->oid) ||
481 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
482 warning(_("commit-graph chain does not match"));
483 return 0;
486 cur_g = cur_g->base_graph;
489 g->base_graph = chain;
491 if (chain)
492 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
494 return 1;
497 static struct commit_graph *load_commit_graph_chain(struct repository *r,
498 struct object_directory *odb)
500 struct commit_graph *graph_chain = NULL;
501 struct strbuf line = STRBUF_INIT;
502 struct stat st;
503 struct object_id *oids;
504 int i = 0, valid = 1, count;
505 char *chain_name = get_commit_graph_chain_filename(odb);
506 FILE *fp;
507 int stat_res;
509 fp = fopen(chain_name, "r");
510 stat_res = stat(chain_name, &st);
511 free(chain_name);
513 if (!fp ||
514 stat_res ||
515 st.st_size <= the_hash_algo->hexsz)
516 return NULL;
518 count = st.st_size / (the_hash_algo->hexsz + 1);
519 oids = xcalloc(count, sizeof(struct object_id));
521 prepare_alt_odb(r);
523 for (i = 0; i < count; i++) {
524 struct object_directory *odb;
526 if (strbuf_getline_lf(&line, fp) == EOF)
527 break;
529 if (get_oid_hex(line.buf, &oids[i])) {
530 warning(_("invalid commit-graph chain: line '%s' not a hash"),
531 line.buf);
532 valid = 0;
533 break;
536 valid = 0;
537 for (odb = r->objects->odb; odb; odb = odb->next) {
538 char *graph_name = get_split_graph_filename(odb, line.buf);
539 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
541 free(graph_name);
543 if (g) {
544 if (add_graph_to_chain(g, graph_chain, oids, i)) {
545 graph_chain = g;
546 valid = 1;
549 break;
553 if (!valid) {
554 warning(_("unable to find all commit-graph files"));
555 break;
559 free(oids);
560 fclose(fp);
561 strbuf_release(&line);
563 return graph_chain;
567 * returns 1 if and only if all graphs in the chain have
568 * corrected commit dates stored in the generation_data chunk.
570 static int validate_mixed_generation_chain(struct commit_graph *g)
572 int read_generation_data = 1;
573 struct commit_graph *p = g;
575 while (read_generation_data && p) {
576 read_generation_data = p->read_generation_data;
577 p = p->base_graph;
580 if (read_generation_data)
581 return 1;
583 while (g) {
584 g->read_generation_data = 0;
585 g = g->base_graph;
588 return 0;
591 struct commit_graph *read_commit_graph_one(struct repository *r,
592 struct object_directory *odb)
594 struct commit_graph *g = load_commit_graph_v1(r, odb);
596 if (!g)
597 g = load_commit_graph_chain(r, odb);
599 validate_mixed_generation_chain(g);
601 return g;
604 static void prepare_commit_graph_one(struct repository *r,
605 struct object_directory *odb)
608 if (r->objects->commit_graph)
609 return;
611 r->objects->commit_graph = read_commit_graph_one(r, odb);
615 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
617 * On the first invocation, this function attempts to load the commit
618 * graph if the_repository is configured to have one.
620 static int prepare_commit_graph(struct repository *r)
622 struct object_directory *odb;
625 * This must come before the "already attempted?" check below, because
626 * we want to disable even an already-loaded graph file.
628 if (r->commit_graph_disabled)
629 return 0;
631 if (r->objects->commit_graph_attempted)
632 return !!r->objects->commit_graph;
633 r->objects->commit_graph_attempted = 1;
635 prepare_repo_settings(r);
637 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
638 r->settings.core_commit_graph != 1)
640 * This repository is not configured to use commit graphs, so
641 * do not load one. (But report commit_graph_attempted anyway
642 * so that commit graph loading is not attempted again for this
643 * repository.)
645 return 0;
647 if (!commit_graph_compatible(r))
648 return 0;
650 prepare_alt_odb(r);
651 for (odb = r->objects->odb;
652 !r->objects->commit_graph && odb;
653 odb = odb->next)
654 prepare_commit_graph_one(r, odb);
655 return !!r->objects->commit_graph;
658 int generation_numbers_enabled(struct repository *r)
660 uint32_t first_generation;
661 struct commit_graph *g;
662 if (!prepare_commit_graph(r))
663 return 0;
665 g = r->objects->commit_graph;
667 if (!g->num_commits)
668 return 0;
670 first_generation = get_be32(g->chunk_commit_data +
671 g->hash_len + 8) >> 2;
673 return !!first_generation;
676 int corrected_commit_dates_enabled(struct repository *r)
678 struct commit_graph *g;
679 if (!prepare_commit_graph(r))
680 return 0;
682 g = r->objects->commit_graph;
684 if (!g->num_commits)
685 return 0;
687 return g->read_generation_data;
690 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
692 struct commit_graph *g = r->objects->commit_graph;
693 while (g) {
694 if (g->bloom_filter_settings)
695 return g->bloom_filter_settings;
696 g = g->base_graph;
698 return NULL;
701 static void close_commit_graph_one(struct commit_graph *g)
703 if (!g)
704 return;
706 close_commit_graph_one(g->base_graph);
707 free_commit_graph(g);
710 void close_commit_graph(struct raw_object_store *o)
712 close_commit_graph_one(o->commit_graph);
713 o->commit_graph = NULL;
716 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
718 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
719 g->chunk_oid_lookup, g->hash_len, pos);
722 static void load_oid_from_graph(struct commit_graph *g,
723 uint32_t pos,
724 struct object_id *oid)
726 uint32_t lex_index;
728 while (g && pos < g->num_commits_in_base)
729 g = g->base_graph;
731 if (!g)
732 BUG("NULL commit-graph");
734 if (pos >= g->num_commits + g->num_commits_in_base)
735 die(_("invalid commit position. commit-graph is likely corrupt"));
737 lex_index = pos - g->num_commits_in_base;
739 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
742 static struct commit_list **insert_parent_or_die(struct repository *r,
743 struct commit_graph *g,
744 uint32_t pos,
745 struct commit_list **pptr)
747 struct commit *c;
748 struct object_id oid;
750 if (pos >= g->num_commits + g->num_commits_in_base)
751 die("invalid parent position %"PRIu32, pos);
753 load_oid_from_graph(g, pos, &oid);
754 c = lookup_commit(r, &oid);
755 if (!c)
756 die(_("could not find commit %s"), oid_to_hex(&oid));
757 commit_graph_data_at(c)->graph_pos = pos;
758 return &commit_list_insert(c, pptr)->next;
761 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
763 const unsigned char *commit_data;
764 struct commit_graph_data *graph_data;
765 uint32_t lex_index, offset_pos;
766 uint64_t date_high, date_low, offset;
768 while (pos < g->num_commits_in_base)
769 g = g->base_graph;
771 if (pos >= g->num_commits + g->num_commits_in_base)
772 die(_("invalid commit position. commit-graph is likely corrupt"));
774 lex_index = pos - g->num_commits_in_base;
775 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
777 graph_data = commit_graph_data_at(item);
778 graph_data->graph_pos = pos;
780 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
781 date_low = get_be32(commit_data + g->hash_len + 12);
782 item->date = (timestamp_t)((date_high << 32) | date_low);
784 if (g->read_generation_data) {
785 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
787 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
788 if (!g->chunk_generation_data_overflow)
789 die(_("commit-graph requires overflow generation data but has none"));
791 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
792 graph_data->generation = get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
793 } else
794 graph_data->generation = item->date + offset;
795 } else
796 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
798 if (g->topo_levels)
799 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
802 static inline void set_commit_tree(struct commit *c, struct tree *t)
804 c->maybe_tree = t;
807 static int fill_commit_in_graph(struct repository *r,
808 struct commit *item,
809 struct commit_graph *g, uint32_t pos)
811 uint32_t edge_value;
812 uint32_t *parent_data_ptr;
813 struct commit_list **pptr;
814 const unsigned char *commit_data;
815 uint32_t lex_index;
817 while (pos < g->num_commits_in_base)
818 g = g->base_graph;
820 fill_commit_graph_info(item, g, pos);
822 lex_index = pos - g->num_commits_in_base;
823 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
825 item->object.parsed = 1;
827 set_commit_tree(item, NULL);
829 pptr = &item->parents;
831 edge_value = get_be32(commit_data + g->hash_len);
832 if (edge_value == GRAPH_PARENT_NONE)
833 return 1;
834 pptr = insert_parent_or_die(r, g, edge_value, pptr);
836 edge_value = get_be32(commit_data + g->hash_len + 4);
837 if (edge_value == GRAPH_PARENT_NONE)
838 return 1;
839 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
840 pptr = insert_parent_or_die(r, g, edge_value, pptr);
841 return 1;
844 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
845 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
846 do {
847 edge_value = get_be32(parent_data_ptr);
848 pptr = insert_parent_or_die(r, g,
849 edge_value & GRAPH_EDGE_LAST_MASK,
850 pptr);
851 parent_data_ptr++;
852 } while (!(edge_value & GRAPH_LAST_EDGE));
854 return 1;
857 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
859 uint32_t graph_pos = commit_graph_position(item);
860 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
861 *pos = graph_pos;
862 return 1;
863 } else {
864 struct commit_graph *cur_g = g;
865 uint32_t lex_index;
867 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
868 cur_g = cur_g->base_graph;
870 if (cur_g) {
871 *pos = lex_index + cur_g->num_commits_in_base;
872 return 1;
875 return 0;
879 static int parse_commit_in_graph_one(struct repository *r,
880 struct commit_graph *g,
881 struct commit *item)
883 uint32_t pos;
885 if (item->object.parsed)
886 return 1;
888 if (find_commit_in_graph(item, g, &pos))
889 return fill_commit_in_graph(r, item, g, pos);
891 return 0;
894 int parse_commit_in_graph(struct repository *r, struct commit *item)
896 static int checked_env = 0;
898 if (!checked_env &&
899 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
900 die("dying as requested by the '%s' variable on commit-graph parse!",
901 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
902 checked_env = 1;
904 if (!prepare_commit_graph(r))
905 return 0;
906 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
909 void load_commit_graph_info(struct repository *r, struct commit *item)
911 uint32_t pos;
912 if (!prepare_commit_graph(r))
913 return;
914 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
915 fill_commit_graph_info(item, r->objects->commit_graph, pos);
918 static struct tree *load_tree_for_commit(struct repository *r,
919 struct commit_graph *g,
920 struct commit *c)
922 struct object_id oid;
923 const unsigned char *commit_data;
924 uint32_t graph_pos = commit_graph_position(c);
926 while (graph_pos < g->num_commits_in_base)
927 g = g->base_graph;
929 commit_data = g->chunk_commit_data +
930 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
932 hashcpy(oid.hash, commit_data);
933 set_commit_tree(c, lookup_tree(r, &oid));
935 return c->maybe_tree;
938 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
939 struct commit_graph *g,
940 const struct commit *c)
942 if (c->maybe_tree)
943 return c->maybe_tree;
944 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
945 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
947 return load_tree_for_commit(r, g, (struct commit *)c);
950 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
952 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
955 struct packed_commit_list {
956 struct commit **list;
957 size_t nr;
958 size_t alloc;
961 struct write_commit_graph_context {
962 struct repository *r;
963 struct object_directory *odb;
964 char *graph_name;
965 struct oid_array oids;
966 struct packed_commit_list commits;
967 int num_extra_edges;
968 int num_generation_data_overflows;
969 unsigned long approx_nr_objects;
970 struct progress *progress;
971 int progress_done;
972 uint64_t progress_cnt;
974 char *base_graph_name;
975 int num_commit_graphs_before;
976 int num_commit_graphs_after;
977 char **commit_graph_filenames_before;
978 char **commit_graph_filenames_after;
979 char **commit_graph_hash_after;
980 uint32_t new_num_commits_in_base;
981 struct commit_graph *new_base_graph;
983 unsigned append:1,
984 report_progress:1,
985 split:1,
986 changed_paths:1,
987 order_by_pack:1,
988 write_generation_data:1,
989 trust_generation_numbers:1;
991 struct topo_level_slab *topo_levels;
992 const struct commit_graph_opts *opts;
993 size_t total_bloom_filter_data_size;
994 const struct bloom_filter_settings *bloom_settings;
996 int count_bloom_filter_computed;
997 int count_bloom_filter_not_computed;
998 int count_bloom_filter_trunc_empty;
999 int count_bloom_filter_trunc_large;
1002 static int write_graph_chunk_fanout(struct hashfile *f,
1003 void *data)
1005 struct write_commit_graph_context *ctx = data;
1006 int i, count = 0;
1007 struct commit **list = ctx->commits.list;
1010 * Write the first-level table (the list is sorted,
1011 * but we use a 256-entry lookup to be able to avoid
1012 * having to do eight extra binary search iterations).
1014 for (i = 0; i < 256; i++) {
1015 while (count < ctx->commits.nr) {
1016 if ((*list)->object.oid.hash[0] != i)
1017 break;
1018 display_progress(ctx->progress, ++ctx->progress_cnt);
1019 count++;
1020 list++;
1023 hashwrite_be32(f, count);
1026 return 0;
1029 static int write_graph_chunk_oids(struct hashfile *f,
1030 void *data)
1032 struct write_commit_graph_context *ctx = data;
1033 struct commit **list = ctx->commits.list;
1034 int count;
1035 for (count = 0; count < ctx->commits.nr; count++, list++) {
1036 display_progress(ctx->progress, ++ctx->progress_cnt);
1037 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1040 return 0;
1043 static const struct object_id *commit_to_oid(size_t index, const void *table)
1045 const struct commit * const *commits = table;
1046 return &commits[index]->object.oid;
1049 static int write_graph_chunk_data(struct hashfile *f,
1050 void *data)
1052 struct write_commit_graph_context *ctx = data;
1053 struct commit **list = ctx->commits.list;
1054 struct commit **last = ctx->commits.list + ctx->commits.nr;
1055 uint32_t num_extra_edges = 0;
1057 while (list < last) {
1058 struct commit_list *parent;
1059 struct object_id *tree;
1060 int edge_value;
1061 uint32_t packedDate[2];
1062 display_progress(ctx->progress, ++ctx->progress_cnt);
1064 if (repo_parse_commit_no_graph(ctx->r, *list))
1065 die(_("unable to parse commit %s"),
1066 oid_to_hex(&(*list)->object.oid));
1067 tree = get_commit_tree_oid(*list);
1068 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1070 parent = (*list)->parents;
1072 if (!parent)
1073 edge_value = GRAPH_PARENT_NONE;
1074 else {
1075 edge_value = oid_pos(&parent->item->object.oid,
1076 ctx->commits.list,
1077 ctx->commits.nr,
1078 commit_to_oid);
1080 if (edge_value >= 0)
1081 edge_value += ctx->new_num_commits_in_base;
1082 else if (ctx->new_base_graph) {
1083 uint32_t pos;
1084 if (find_commit_in_graph(parent->item,
1085 ctx->new_base_graph,
1086 &pos))
1087 edge_value = pos;
1090 if (edge_value < 0)
1091 BUG("missing parent %s for commit %s",
1092 oid_to_hex(&parent->item->object.oid),
1093 oid_to_hex(&(*list)->object.oid));
1096 hashwrite_be32(f, edge_value);
1098 if (parent)
1099 parent = parent->next;
1101 if (!parent)
1102 edge_value = GRAPH_PARENT_NONE;
1103 else if (parent->next)
1104 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1105 else {
1106 edge_value = oid_pos(&parent->item->object.oid,
1107 ctx->commits.list,
1108 ctx->commits.nr,
1109 commit_to_oid);
1111 if (edge_value >= 0)
1112 edge_value += ctx->new_num_commits_in_base;
1113 else if (ctx->new_base_graph) {
1114 uint32_t pos;
1115 if (find_commit_in_graph(parent->item,
1116 ctx->new_base_graph,
1117 &pos))
1118 edge_value = pos;
1121 if (edge_value < 0)
1122 BUG("missing parent %s for commit %s",
1123 oid_to_hex(&parent->item->object.oid),
1124 oid_to_hex(&(*list)->object.oid));
1127 hashwrite_be32(f, edge_value);
1129 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1130 do {
1131 num_extra_edges++;
1132 parent = parent->next;
1133 } while (parent);
1136 if (sizeof((*list)->date) > 4)
1137 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1138 else
1139 packedDate[0] = 0;
1141 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1143 packedDate[1] = htonl((*list)->date);
1144 hashwrite(f, packedDate, 8);
1146 list++;
1149 return 0;
1152 static int write_graph_chunk_generation_data(struct hashfile *f,
1153 void *data)
1155 struct write_commit_graph_context *ctx = data;
1156 int i, num_generation_data_overflows = 0;
1158 for (i = 0; i < ctx->commits.nr; i++) {
1159 struct commit *c = ctx->commits.list[i];
1160 timestamp_t offset;
1161 repo_parse_commit(ctx->r, c);
1162 offset = commit_graph_data_at(c)->generation - c->date;
1163 display_progress(ctx->progress, ++ctx->progress_cnt);
1165 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1166 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1167 num_generation_data_overflows++;
1170 hashwrite_be32(f, offset);
1173 return 0;
1176 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1177 void *data)
1179 struct write_commit_graph_context *ctx = data;
1180 int i;
1181 for (i = 0; i < ctx->commits.nr; i++) {
1182 struct commit *c = ctx->commits.list[i];
1183 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1184 display_progress(ctx->progress, ++ctx->progress_cnt);
1186 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1187 hashwrite_be32(f, offset >> 32);
1188 hashwrite_be32(f, (uint32_t) offset);
1192 return 0;
1195 static int write_graph_chunk_extra_edges(struct hashfile *f,
1196 void *data)
1198 struct write_commit_graph_context *ctx = data;
1199 struct commit **list = ctx->commits.list;
1200 struct commit **last = ctx->commits.list + ctx->commits.nr;
1201 struct commit_list *parent;
1203 while (list < last) {
1204 int num_parents = 0;
1206 display_progress(ctx->progress, ++ctx->progress_cnt);
1208 for (parent = (*list)->parents; num_parents < 3 && parent;
1209 parent = parent->next)
1210 num_parents++;
1212 if (num_parents <= 2) {
1213 list++;
1214 continue;
1217 /* Since num_parents > 2, this initializer is safe. */
1218 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1219 int edge_value = oid_pos(&parent->item->object.oid,
1220 ctx->commits.list,
1221 ctx->commits.nr,
1222 commit_to_oid);
1224 if (edge_value >= 0)
1225 edge_value += ctx->new_num_commits_in_base;
1226 else if (ctx->new_base_graph) {
1227 uint32_t pos;
1228 if (find_commit_in_graph(parent->item,
1229 ctx->new_base_graph,
1230 &pos))
1231 edge_value = pos;
1234 if (edge_value < 0)
1235 BUG("missing parent %s for commit %s",
1236 oid_to_hex(&parent->item->object.oid),
1237 oid_to_hex(&(*list)->object.oid));
1238 else if (!parent->next)
1239 edge_value |= GRAPH_LAST_EDGE;
1241 hashwrite_be32(f, edge_value);
1244 list++;
1247 return 0;
1250 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1251 void *data)
1253 struct write_commit_graph_context *ctx = data;
1254 struct commit **list = ctx->commits.list;
1255 struct commit **last = ctx->commits.list + ctx->commits.nr;
1256 uint32_t cur_pos = 0;
1258 while (list < last) {
1259 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1260 size_t len = filter ? filter->len : 0;
1261 cur_pos += len;
1262 display_progress(ctx->progress, ++ctx->progress_cnt);
1263 hashwrite_be32(f, cur_pos);
1264 list++;
1267 return 0;
1270 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1272 struct json_writer jw = JSON_WRITER_INIT;
1274 jw_object_begin(&jw, 0);
1275 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1276 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1277 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1278 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1279 jw_end(&jw);
1281 trace2_data_json("bloom", ctx->r, "settings", &jw);
1283 jw_release(&jw);
1286 static int write_graph_chunk_bloom_data(struct hashfile *f,
1287 void *data)
1289 struct write_commit_graph_context *ctx = data;
1290 struct commit **list = ctx->commits.list;
1291 struct commit **last = ctx->commits.list + ctx->commits.nr;
1293 trace2_bloom_filter_settings(ctx);
1295 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1296 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1297 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1299 while (list < last) {
1300 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1301 size_t len = filter ? filter->len : 0;
1303 display_progress(ctx->progress, ++ctx->progress_cnt);
1304 if (len)
1305 hashwrite(f, filter->data, len * sizeof(unsigned char));
1306 list++;
1309 return 0;
1312 static int add_packed_commits(const struct object_id *oid,
1313 struct packed_git *pack,
1314 uint32_t pos,
1315 void *data)
1317 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1318 enum object_type type;
1319 off_t offset = nth_packed_object_offset(pack, pos);
1320 struct object_info oi = OBJECT_INFO_INIT;
1322 if (ctx->progress)
1323 display_progress(ctx->progress, ++ctx->progress_done);
1325 oi.typep = &type;
1326 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1327 die(_("unable to get type of object %s"), oid_to_hex(oid));
1329 if (type != OBJ_COMMIT)
1330 return 0;
1332 oid_array_append(&ctx->oids, oid);
1333 set_commit_pos(ctx->r, oid);
1335 return 0;
1338 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1340 struct commit_list *parent;
1341 for (parent = commit->parents; parent; parent = parent->next) {
1342 if (!(parent->item->object.flags & REACHABLE)) {
1343 oid_array_append(&ctx->oids, &parent->item->object.oid);
1344 parent->item->object.flags |= REACHABLE;
1349 static void close_reachable(struct write_commit_graph_context *ctx)
1351 int i;
1352 struct commit *commit;
1353 enum commit_graph_split_flags flags = ctx->opts ?
1354 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1356 if (ctx->report_progress)
1357 ctx->progress = start_delayed_progress(
1358 _("Loading known commits in commit graph"),
1359 ctx->oids.nr);
1360 for (i = 0; i < ctx->oids.nr; i++) {
1361 display_progress(ctx->progress, i + 1);
1362 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1363 if (commit)
1364 commit->object.flags |= REACHABLE;
1366 stop_progress(&ctx->progress);
1369 * As this loop runs, ctx->oids.nr may grow, but not more
1370 * than the number of missing commits in the reachable
1371 * closure.
1373 if (ctx->report_progress)
1374 ctx->progress = start_delayed_progress(
1375 _("Expanding reachable commits in commit graph"),
1377 for (i = 0; i < ctx->oids.nr; i++) {
1378 display_progress(ctx->progress, i + 1);
1379 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1381 if (!commit)
1382 continue;
1383 if (ctx->split) {
1384 if ((!repo_parse_commit(ctx->r, commit) &&
1385 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1386 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1387 add_missing_parents(ctx, commit);
1388 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1389 add_missing_parents(ctx, commit);
1391 stop_progress(&ctx->progress);
1393 if (ctx->report_progress)
1394 ctx->progress = start_delayed_progress(
1395 _("Clearing commit marks in commit graph"),
1396 ctx->oids.nr);
1397 for (i = 0; i < ctx->oids.nr; i++) {
1398 display_progress(ctx->progress, i + 1);
1399 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1401 if (commit)
1402 commit->object.flags &= ~REACHABLE;
1404 stop_progress(&ctx->progress);
1407 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1409 int i;
1410 struct commit_list *list = NULL;
1412 if (ctx->report_progress)
1413 ctx->progress = start_delayed_progress(
1414 _("Computing commit graph topological levels"),
1415 ctx->commits.nr);
1416 for (i = 0; i < ctx->commits.nr; i++) {
1417 struct commit *c = ctx->commits.list[i];
1418 uint32_t level;
1420 repo_parse_commit(ctx->r, c);
1421 level = *topo_level_slab_at(ctx->topo_levels, c);
1423 display_progress(ctx->progress, i + 1);
1424 if (level != GENERATION_NUMBER_ZERO)
1425 continue;
1427 commit_list_insert(c, &list);
1428 while (list) {
1429 struct commit *current = list->item;
1430 struct commit_list *parent;
1431 int all_parents_computed = 1;
1432 uint32_t max_level = 0;
1434 for (parent = current->parents; parent; parent = parent->next) {
1435 repo_parse_commit(ctx->r, parent->item);
1436 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1438 if (level == GENERATION_NUMBER_ZERO) {
1439 all_parents_computed = 0;
1440 commit_list_insert(parent->item, &list);
1441 break;
1444 if (level > max_level)
1445 max_level = level;
1448 if (all_parents_computed) {
1449 pop_commit(&list);
1451 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1452 max_level = GENERATION_NUMBER_V1_MAX - 1;
1453 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1457 stop_progress(&ctx->progress);
1460 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1462 int i;
1463 struct commit_list *list = NULL;
1465 if (ctx->report_progress)
1466 ctx->progress = start_delayed_progress(
1467 _("Computing commit graph generation numbers"),
1468 ctx->commits.nr);
1470 if (!ctx->trust_generation_numbers) {
1471 for (i = 0; i < ctx->commits.nr; i++) {
1472 struct commit *c = ctx->commits.list[i];
1473 repo_parse_commit(ctx->r, c);
1474 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1478 for (i = 0; i < ctx->commits.nr; i++) {
1479 struct commit *c = ctx->commits.list[i];
1480 timestamp_t corrected_commit_date;
1482 repo_parse_commit(ctx->r, c);
1483 corrected_commit_date = commit_graph_data_at(c)->generation;
1485 display_progress(ctx->progress, i + 1);
1486 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1487 continue;
1489 commit_list_insert(c, &list);
1490 while (list) {
1491 struct commit *current = list->item;
1492 struct commit_list *parent;
1493 int all_parents_computed = 1;
1494 timestamp_t max_corrected_commit_date = 0;
1496 for (parent = current->parents; parent; parent = parent->next) {
1497 repo_parse_commit(ctx->r, parent->item);
1498 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1500 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1501 all_parents_computed = 0;
1502 commit_list_insert(parent->item, &list);
1503 break;
1506 if (corrected_commit_date > max_corrected_commit_date)
1507 max_corrected_commit_date = corrected_commit_date;
1510 if (all_parents_computed) {
1511 pop_commit(&list);
1513 if (current->date && current->date > max_corrected_commit_date)
1514 max_corrected_commit_date = current->date - 1;
1515 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1517 if (commit_graph_data_at(current)->generation - current->date > GENERATION_NUMBER_V2_OFFSET_MAX)
1518 ctx->num_generation_data_overflows++;
1522 stop_progress(&ctx->progress);
1525 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1527 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1528 ctx->count_bloom_filter_computed);
1529 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1530 ctx->count_bloom_filter_not_computed);
1531 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1532 ctx->count_bloom_filter_trunc_empty);
1533 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1534 ctx->count_bloom_filter_trunc_large);
1537 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1539 int i;
1540 struct progress *progress = NULL;
1541 struct commit **sorted_commits;
1542 int max_new_filters;
1544 init_bloom_filters();
1546 if (ctx->report_progress)
1547 progress = start_delayed_progress(
1548 _("Computing commit changed paths Bloom filters"),
1549 ctx->commits.nr);
1551 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1552 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1554 if (ctx->order_by_pack)
1555 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1556 else
1557 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1559 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1560 ctx->opts->max_new_filters : ctx->commits.nr;
1562 for (i = 0; i < ctx->commits.nr; i++) {
1563 enum bloom_filter_computed computed = 0;
1564 struct commit *c = sorted_commits[i];
1565 struct bloom_filter *filter = get_or_compute_bloom_filter(
1566 ctx->r,
1568 ctx->count_bloom_filter_computed < max_new_filters,
1569 ctx->bloom_settings,
1570 &computed);
1571 if (computed & BLOOM_COMPUTED) {
1572 ctx->count_bloom_filter_computed++;
1573 if (computed & BLOOM_TRUNC_EMPTY)
1574 ctx->count_bloom_filter_trunc_empty++;
1575 if (computed & BLOOM_TRUNC_LARGE)
1576 ctx->count_bloom_filter_trunc_large++;
1577 } else if (computed & BLOOM_NOT_COMPUTED)
1578 ctx->count_bloom_filter_not_computed++;
1579 ctx->total_bloom_filter_data_size += filter
1580 ? sizeof(unsigned char) * filter->len : 0;
1581 display_progress(progress, i + 1);
1584 if (trace2_is_enabled())
1585 trace2_bloom_filter_write_statistics(ctx);
1587 free(sorted_commits);
1588 stop_progress(&progress);
1591 struct refs_cb_data {
1592 struct oidset *commits;
1593 struct progress *progress;
1596 static int add_ref_to_set(const char *refname,
1597 const struct object_id *oid,
1598 int flags, void *cb_data)
1600 struct object_id peeled;
1601 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1603 if (!peel_iterated_oid(oid, &peeled))
1604 oid = &peeled;
1605 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1606 oidset_insert(data->commits, oid);
1608 display_progress(data->progress, oidset_size(data->commits));
1610 return 0;
1613 int write_commit_graph_reachable(struct object_directory *odb,
1614 enum commit_graph_write_flags flags,
1615 const struct commit_graph_opts *opts)
1617 struct oidset commits = OIDSET_INIT;
1618 struct refs_cb_data data;
1619 int result;
1621 memset(&data, 0, sizeof(data));
1622 data.commits = &commits;
1623 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1624 data.progress = start_delayed_progress(
1625 _("Collecting referenced commits"), 0);
1627 for_each_ref(add_ref_to_set, &data);
1629 stop_progress(&data.progress);
1631 result = write_commit_graph(odb, NULL, &commits,
1632 flags, opts);
1634 oidset_clear(&commits);
1635 return result;
1638 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1639 struct string_list *pack_indexes)
1641 uint32_t i;
1642 struct strbuf progress_title = STRBUF_INIT;
1643 struct strbuf packname = STRBUF_INIT;
1644 int dirlen;
1646 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1647 dirlen = packname.len;
1648 if (ctx->report_progress) {
1649 strbuf_addf(&progress_title,
1650 Q_("Finding commits for commit graph in %d pack",
1651 "Finding commits for commit graph in %d packs",
1652 pack_indexes->nr),
1653 pack_indexes->nr);
1654 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1655 ctx->progress_done = 0;
1657 for (i = 0; i < pack_indexes->nr; i++) {
1658 struct packed_git *p;
1659 strbuf_setlen(&packname, dirlen);
1660 strbuf_addstr(&packname, pack_indexes->items[i].string);
1661 p = add_packed_git(packname.buf, packname.len, 1);
1662 if (!p) {
1663 error(_("error adding pack %s"), packname.buf);
1664 return -1;
1666 if (open_pack_index(p)) {
1667 error(_("error opening index for %s"), packname.buf);
1668 return -1;
1670 for_each_object_in_pack(p, add_packed_commits, ctx,
1671 FOR_EACH_OBJECT_PACK_ORDER);
1672 close_pack(p);
1673 free(p);
1676 stop_progress(&ctx->progress);
1677 strbuf_release(&progress_title);
1678 strbuf_release(&packname);
1680 return 0;
1683 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1684 struct oidset *commits)
1686 struct oidset_iter iter;
1687 struct object_id *oid;
1689 if (!oidset_size(commits))
1690 return 0;
1692 oidset_iter_init(commits, &iter);
1693 while ((oid = oidset_iter_next(&iter))) {
1694 oid_array_append(&ctx->oids, oid);
1697 return 0;
1700 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1702 if (ctx->report_progress)
1703 ctx->progress = start_delayed_progress(
1704 _("Finding commits for commit graph among packed objects"),
1705 ctx->approx_nr_objects);
1706 for_each_packed_object(add_packed_commits, ctx,
1707 FOR_EACH_OBJECT_PACK_ORDER);
1708 if (ctx->progress_done < ctx->approx_nr_objects)
1709 display_progress(ctx->progress, ctx->approx_nr_objects);
1710 stop_progress(&ctx->progress);
1713 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1715 uint32_t i;
1716 enum commit_graph_split_flags flags = ctx->opts ?
1717 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1719 ctx->num_extra_edges = 0;
1720 if (ctx->report_progress)
1721 ctx->progress = start_delayed_progress(
1722 _("Finding extra edges in commit graph"),
1723 ctx->oids.nr);
1724 oid_array_sort(&ctx->oids);
1725 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1726 unsigned int num_parents;
1728 display_progress(ctx->progress, i + 1);
1730 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1731 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1733 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1734 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1735 continue;
1737 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1738 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1739 else
1740 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1742 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1743 if (num_parents > 2)
1744 ctx->num_extra_edges += num_parents - 1;
1746 ctx->commits.nr++;
1748 stop_progress(&ctx->progress);
1751 static int write_graph_chunk_base_1(struct hashfile *f,
1752 struct commit_graph *g)
1754 int num = 0;
1756 if (!g)
1757 return 0;
1759 num = write_graph_chunk_base_1(f, g->base_graph);
1760 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1761 return num + 1;
1764 static int write_graph_chunk_base(struct hashfile *f,
1765 void *data)
1767 struct write_commit_graph_context *ctx = data;
1768 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1770 if (num != ctx->num_commit_graphs_after - 1) {
1771 error(_("failed to write correct number of base graph ids"));
1772 return -1;
1775 return 0;
1778 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1780 uint32_t i;
1781 int fd;
1782 struct hashfile *f;
1783 struct lock_file lk = LOCK_INIT;
1784 const unsigned hashsz = the_hash_algo->rawsz;
1785 struct strbuf progress_title = STRBUF_INIT;
1786 struct object_id file_hash;
1787 struct chunkfile *cf;
1789 if (ctx->split) {
1790 struct strbuf tmp_file = STRBUF_INIT;
1792 strbuf_addf(&tmp_file,
1793 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1794 ctx->odb->path);
1795 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1796 } else {
1797 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1800 if (safe_create_leading_directories(ctx->graph_name)) {
1801 UNLEAK(ctx->graph_name);
1802 error(_("unable to create leading directories of %s"),
1803 ctx->graph_name);
1804 return -1;
1807 if (ctx->split) {
1808 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1810 hold_lock_file_for_update_mode(&lk, lock_name,
1811 LOCK_DIE_ON_ERROR, 0444);
1813 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1814 if (fd < 0) {
1815 error(_("unable to create temporary graph layer"));
1816 return -1;
1819 if (adjust_shared_perm(ctx->graph_name)) {
1820 error(_("unable to adjust shared permissions for '%s'"),
1821 ctx->graph_name);
1822 return -1;
1825 f = hashfd(fd, ctx->graph_name);
1826 } else {
1827 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1828 LOCK_DIE_ON_ERROR, 0444);
1829 fd = get_lock_file_fd(&lk);
1830 f = hashfd(fd, get_lock_file_path(&lk));
1833 cf = init_chunkfile(f);
1835 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1836 write_graph_chunk_fanout);
1837 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1838 write_graph_chunk_oids);
1839 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1840 write_graph_chunk_data);
1842 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_NO_GDAT, 0))
1843 ctx->write_generation_data = 0;
1844 if (ctx->write_generation_data)
1845 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1846 sizeof(uint32_t) * ctx->commits.nr,
1847 write_graph_chunk_generation_data);
1848 if (ctx->num_generation_data_overflows)
1849 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1850 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1851 write_graph_chunk_generation_data_overflow);
1852 if (ctx->num_extra_edges)
1853 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1854 4 * ctx->num_extra_edges,
1855 write_graph_chunk_extra_edges);
1856 if (ctx->changed_paths) {
1857 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1858 sizeof(uint32_t) * ctx->commits.nr,
1859 write_graph_chunk_bloom_indexes);
1860 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1861 sizeof(uint32_t) * 3
1862 + ctx->total_bloom_filter_data_size,
1863 write_graph_chunk_bloom_data);
1865 if (ctx->num_commit_graphs_after > 1)
1866 add_chunk(cf, GRAPH_CHUNKID_BASE,
1867 hashsz * (ctx->num_commit_graphs_after - 1),
1868 write_graph_chunk_base);
1870 hashwrite_be32(f, GRAPH_SIGNATURE);
1872 hashwrite_u8(f, GRAPH_VERSION);
1873 hashwrite_u8(f, oid_version());
1874 hashwrite_u8(f, get_num_chunks(cf));
1875 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1877 if (ctx->report_progress) {
1878 strbuf_addf(&progress_title,
1879 Q_("Writing out commit graph in %d pass",
1880 "Writing out commit graph in %d passes",
1881 get_num_chunks(cf)),
1882 get_num_chunks(cf));
1883 ctx->progress = start_delayed_progress(
1884 progress_title.buf,
1885 get_num_chunks(cf) * ctx->commits.nr);
1888 write_chunkfile(cf, ctx);
1890 stop_progress(&ctx->progress);
1891 strbuf_release(&progress_title);
1893 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1894 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1895 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1897 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1898 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1899 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1900 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1903 close_commit_graph(ctx->r->objects);
1904 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1905 free_chunkfile(cf);
1907 if (ctx->split) {
1908 FILE *chainf = fdopen_lock_file(&lk, "w");
1909 char *final_graph_name;
1910 int result;
1912 close(fd);
1914 if (!chainf) {
1915 error(_("unable to open commit-graph chain file"));
1916 return -1;
1919 if (ctx->base_graph_name) {
1920 const char *dest;
1921 int idx = ctx->num_commit_graphs_after - 1;
1922 if (ctx->num_commit_graphs_after > 1)
1923 idx--;
1925 dest = ctx->commit_graph_filenames_after[idx];
1927 if (strcmp(ctx->base_graph_name, dest)) {
1928 result = rename(ctx->base_graph_name, dest);
1930 if (result) {
1931 error(_("failed to rename base commit-graph file"));
1932 return -1;
1935 } else {
1936 char *graph_name = get_commit_graph_filename(ctx->odb);
1937 unlink(graph_name);
1940 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1941 final_graph_name = get_split_graph_filename(ctx->odb,
1942 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1943 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1945 result = rename(ctx->graph_name, final_graph_name);
1947 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1948 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
1950 if (result) {
1951 error(_("failed to rename temporary commit-graph file"));
1952 return -1;
1956 commit_lock_file(&lk);
1958 return 0;
1961 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1963 struct commit_graph *g;
1964 uint32_t num_commits;
1965 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1966 uint32_t i;
1968 int max_commits = 0;
1969 int size_mult = 2;
1971 if (ctx->opts) {
1972 max_commits = ctx->opts->max_commits;
1974 if (ctx->opts->size_multiple)
1975 size_mult = ctx->opts->size_multiple;
1977 flags = ctx->opts->split_flags;
1980 g = ctx->r->objects->commit_graph;
1981 num_commits = ctx->commits.nr;
1982 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1983 ctx->num_commit_graphs_after = 1;
1984 else
1985 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1987 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1988 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
1989 while (g && (g->num_commits <= size_mult * num_commits ||
1990 (max_commits && num_commits > max_commits))) {
1991 if (g->odb != ctx->odb)
1992 break;
1994 num_commits += g->num_commits;
1995 g = g->base_graph;
1997 ctx->num_commit_graphs_after--;
2001 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2002 ctx->new_base_graph = g;
2003 else if (ctx->num_commit_graphs_after != 1)
2004 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2005 "should be 1 with --split=replace");
2007 if (ctx->num_commit_graphs_after == 2) {
2008 char *old_graph_name = get_commit_graph_filename(g->odb);
2010 if (!strcmp(g->filename, old_graph_name) &&
2011 g->odb != ctx->odb) {
2012 ctx->num_commit_graphs_after = 1;
2013 ctx->new_base_graph = NULL;
2016 free(old_graph_name);
2019 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2020 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2022 for (i = 0; i < ctx->num_commit_graphs_after &&
2023 i < ctx->num_commit_graphs_before; i++)
2024 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2026 i = ctx->num_commit_graphs_before - 1;
2027 g = ctx->r->objects->commit_graph;
2029 while (g) {
2030 if (i < ctx->num_commit_graphs_after)
2031 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2034 * If the topmost remaining layer has generation data chunk, the
2035 * resultant layer also has generation data chunk.
2037 if (i == ctx->num_commit_graphs_after - 2)
2038 ctx->write_generation_data = !!g->chunk_generation_data;
2040 i--;
2041 g = g->base_graph;
2045 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2046 struct commit_graph *g)
2048 uint32_t i;
2049 uint32_t offset = g->num_commits_in_base;
2051 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2053 for (i = 0; i < g->num_commits; i++) {
2054 struct object_id oid;
2055 struct commit *result;
2057 display_progress(ctx->progress, i + 1);
2059 load_oid_from_graph(g, i + offset, &oid);
2061 /* only add commits if they still exist in the repo */
2062 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2064 if (result) {
2065 ctx->commits.list[ctx->commits.nr] = result;
2066 ctx->commits.nr++;
2071 static int commit_compare(const void *_a, const void *_b)
2073 const struct commit *a = *(const struct commit **)_a;
2074 const struct commit *b = *(const struct commit **)_b;
2075 return oidcmp(&a->object.oid, &b->object.oid);
2078 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2080 uint32_t i, dedup_i = 0;
2082 if (ctx->report_progress)
2083 ctx->progress = start_delayed_progress(
2084 _("Scanning merged commits"),
2085 ctx->commits.nr);
2087 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2089 ctx->num_extra_edges = 0;
2090 for (i = 0; i < ctx->commits.nr; i++) {
2091 display_progress(ctx->progress, i);
2093 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2094 &ctx->commits.list[i]->object.oid)) {
2096 * Silently ignore duplicates. These were likely
2097 * created due to a commit appearing in multiple
2098 * layers of the chain, which is unexpected but
2099 * not invalid. We should make sure there is a
2100 * unique copy in the new layer.
2102 } else {
2103 unsigned int num_parents;
2105 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2106 dedup_i++;
2108 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2109 if (num_parents > 2)
2110 ctx->num_extra_edges += num_parents - 1;
2114 ctx->commits.nr = dedup_i;
2116 stop_progress(&ctx->progress);
2119 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2121 struct commit_graph *g = ctx->r->objects->commit_graph;
2122 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2124 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2125 current_graph_number--;
2127 if (ctx->report_progress)
2128 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2130 merge_commit_graph(ctx, g);
2131 stop_progress(&ctx->progress);
2133 g = g->base_graph;
2136 if (g) {
2137 ctx->new_base_graph = g;
2138 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2141 if (ctx->new_base_graph)
2142 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2144 sort_and_scan_merged_commits(ctx);
2147 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2149 uint32_t i;
2150 time_t now = time(NULL);
2152 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2153 struct stat st;
2154 struct utimbuf updated_time;
2156 stat(ctx->commit_graph_filenames_before[i], &st);
2158 updated_time.actime = st.st_atime;
2159 updated_time.modtime = now;
2160 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2164 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2166 struct strbuf path = STRBUF_INIT;
2167 DIR *dir;
2168 struct dirent *de;
2169 size_t dirnamelen;
2170 timestamp_t expire_time = time(NULL);
2172 if (ctx->opts && ctx->opts->expire_time)
2173 expire_time = ctx->opts->expire_time;
2174 if (!ctx->split) {
2175 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2176 unlink(chain_file_name);
2177 free(chain_file_name);
2178 ctx->num_commit_graphs_after = 0;
2181 strbuf_addstr(&path, ctx->odb->path);
2182 strbuf_addstr(&path, "/info/commit-graphs");
2183 dir = opendir(path.buf);
2185 if (!dir)
2186 goto out;
2188 strbuf_addch(&path, '/');
2189 dirnamelen = path.len;
2190 while ((de = readdir(dir)) != NULL) {
2191 struct stat st;
2192 uint32_t i, found = 0;
2194 strbuf_setlen(&path, dirnamelen);
2195 strbuf_addstr(&path, de->d_name);
2197 stat(path.buf, &st);
2199 if (st.st_mtime > expire_time)
2200 continue;
2201 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2202 continue;
2204 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2205 if (!strcmp(ctx->commit_graph_filenames_after[i],
2206 path.buf)) {
2207 found = 1;
2208 break;
2212 if (!found)
2213 unlink(path.buf);
2216 out:
2217 strbuf_release(&path);
2220 int write_commit_graph(struct object_directory *odb,
2221 struct string_list *pack_indexes,
2222 struct oidset *commits,
2223 enum commit_graph_write_flags flags,
2224 const struct commit_graph_opts *opts)
2226 struct write_commit_graph_context *ctx;
2227 uint32_t i;
2228 int res = 0;
2229 int replace = 0;
2230 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2231 struct topo_level_slab topo_levels;
2233 prepare_repo_settings(the_repository);
2234 if (!the_repository->settings.core_commit_graph) {
2235 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2236 return 0;
2238 if (!commit_graph_compatible(the_repository))
2239 return 0;
2241 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
2242 ctx->r = the_repository;
2243 ctx->odb = odb;
2244 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2245 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2246 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2247 ctx->opts = opts;
2248 ctx->total_bloom_filter_data_size = 0;
2249 ctx->write_generation_data = 1;
2250 ctx->num_generation_data_overflows = 0;
2252 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2253 bloom_settings.bits_per_entry);
2254 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2255 bloom_settings.num_hashes);
2256 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2257 bloom_settings.max_changed_paths);
2258 ctx->bloom_settings = &bloom_settings;
2260 init_topo_level_slab(&topo_levels);
2261 ctx->topo_levels = &topo_levels;
2263 prepare_commit_graph(ctx->r);
2264 if (ctx->r->objects->commit_graph) {
2265 struct commit_graph *g = ctx->r->objects->commit_graph;
2267 while (g) {
2268 g->topo_levels = &topo_levels;
2269 g = g->base_graph;
2273 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2274 ctx->changed_paths = 1;
2275 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2276 struct commit_graph *g;
2278 g = ctx->r->objects->commit_graph;
2280 /* We have changed-paths already. Keep them in the next graph */
2281 if (g && g->chunk_bloom_data) {
2282 ctx->changed_paths = 1;
2283 ctx->bloom_settings = g->bloom_filter_settings;
2287 if (ctx->split) {
2288 struct commit_graph *g = ctx->r->objects->commit_graph;
2290 while (g) {
2291 ctx->num_commit_graphs_before++;
2292 g = g->base_graph;
2295 if (ctx->num_commit_graphs_before) {
2296 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2297 i = ctx->num_commit_graphs_before;
2298 g = ctx->r->objects->commit_graph;
2300 while (g) {
2301 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2302 g = g->base_graph;
2306 if (ctx->opts)
2307 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2310 ctx->approx_nr_objects = approximate_object_count();
2312 if (ctx->append && ctx->r->objects->commit_graph) {
2313 struct commit_graph *g = ctx->r->objects->commit_graph;
2314 for (i = 0; i < g->num_commits; i++) {
2315 struct object_id oid;
2316 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2317 oid_array_append(&ctx->oids, &oid);
2321 if (pack_indexes) {
2322 ctx->order_by_pack = 1;
2323 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2324 goto cleanup;
2327 if (commits) {
2328 if ((res = fill_oids_from_commits(ctx, commits)))
2329 goto cleanup;
2332 if (!pack_indexes && !commits) {
2333 ctx->order_by_pack = 1;
2334 fill_oids_from_all_packs(ctx);
2337 close_reachable(ctx);
2339 copy_oids_to_commits(ctx);
2341 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2342 error(_("too many commits to write graph"));
2343 res = -1;
2344 goto cleanup;
2347 if (!ctx->commits.nr && !replace)
2348 goto cleanup;
2350 if (ctx->split) {
2351 split_graph_merge_strategy(ctx);
2353 if (!replace)
2354 merge_commit_graphs(ctx);
2355 } else
2356 ctx->num_commit_graphs_after = 1;
2358 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2360 compute_topological_levels(ctx);
2361 if (ctx->write_generation_data)
2362 compute_generation_numbers(ctx);
2364 if (ctx->changed_paths)
2365 compute_bloom_filters(ctx);
2367 res = write_commit_graph_file(ctx);
2369 if (ctx->split)
2370 mark_commit_graphs(ctx);
2372 expire_commit_graphs(ctx);
2374 cleanup:
2375 free(ctx->graph_name);
2376 free(ctx->commits.list);
2377 oid_array_clear(&ctx->oids);
2378 clear_topo_level_slab(&topo_levels);
2380 if (ctx->commit_graph_filenames_after) {
2381 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2382 free(ctx->commit_graph_filenames_after[i]);
2383 free(ctx->commit_graph_hash_after[i]);
2386 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2387 free(ctx->commit_graph_filenames_before[i]);
2389 free(ctx->commit_graph_filenames_after);
2390 free(ctx->commit_graph_filenames_before);
2391 free(ctx->commit_graph_hash_after);
2394 free(ctx);
2396 return res;
2399 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2400 static int verify_commit_graph_error;
2402 static void graph_report(const char *fmt, ...)
2404 va_list ap;
2406 verify_commit_graph_error = 1;
2407 va_start(ap, fmt);
2408 vfprintf(stderr, fmt, ap);
2409 fprintf(stderr, "\n");
2410 va_end(ap);
2413 #define GENERATION_ZERO_EXISTS 1
2414 #define GENERATION_NUMBER_EXISTS 2
2416 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2418 uint32_t i, cur_fanout_pos = 0;
2419 struct object_id prev_oid, cur_oid, checksum;
2420 int generation_zero = 0;
2421 struct hashfile *f;
2422 int devnull;
2423 struct progress *progress = NULL;
2424 int local_error = 0;
2426 if (!g) {
2427 graph_report("no commit-graph file loaded");
2428 return 1;
2431 verify_commit_graph_error = verify_commit_graph_lite(g);
2432 if (verify_commit_graph_error)
2433 return verify_commit_graph_error;
2435 devnull = open("/dev/null", O_WRONLY);
2436 f = hashfd(devnull, NULL);
2437 hashwrite(f, g->data, g->data_len - g->hash_len);
2438 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2439 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2440 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2441 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2444 for (i = 0; i < g->num_commits; i++) {
2445 struct commit *graph_commit;
2447 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2449 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2450 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2451 oid_to_hex(&prev_oid),
2452 oid_to_hex(&cur_oid));
2454 oidcpy(&prev_oid, &cur_oid);
2456 while (cur_oid.hash[0] > cur_fanout_pos) {
2457 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2459 if (i != fanout_value)
2460 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2461 cur_fanout_pos, fanout_value, i);
2462 cur_fanout_pos++;
2465 graph_commit = lookup_commit(r, &cur_oid);
2466 if (!parse_commit_in_graph_one(r, g, graph_commit))
2467 graph_report(_("failed to parse commit %s from commit-graph"),
2468 oid_to_hex(&cur_oid));
2471 while (cur_fanout_pos < 256) {
2472 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2474 if (g->num_commits != fanout_value)
2475 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2476 cur_fanout_pos, fanout_value, i);
2478 cur_fanout_pos++;
2481 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2482 return verify_commit_graph_error;
2484 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2485 progress = start_progress(_("Verifying commits in commit graph"),
2486 g->num_commits);
2488 for (i = 0; i < g->num_commits; i++) {
2489 struct commit *graph_commit, *odb_commit;
2490 struct commit_list *graph_parents, *odb_parents;
2491 timestamp_t max_generation = 0;
2492 timestamp_t generation;
2494 display_progress(progress, i + 1);
2495 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2497 graph_commit = lookup_commit(r, &cur_oid);
2498 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2499 if (parse_commit_internal(odb_commit, 0, 0)) {
2500 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2501 oid_to_hex(&cur_oid));
2502 continue;
2505 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2506 get_commit_tree_oid(odb_commit)))
2507 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2508 oid_to_hex(&cur_oid),
2509 oid_to_hex(get_commit_tree_oid(graph_commit)),
2510 oid_to_hex(get_commit_tree_oid(odb_commit)));
2512 graph_parents = graph_commit->parents;
2513 odb_parents = odb_commit->parents;
2515 while (graph_parents) {
2516 if (odb_parents == NULL) {
2517 graph_report(_("commit-graph parent list for commit %s is too long"),
2518 oid_to_hex(&cur_oid));
2519 break;
2522 /* parse parent in case it is in a base graph */
2523 parse_commit_in_graph_one(r, g, graph_parents->item);
2525 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2526 graph_report(_("commit-graph parent for %s is %s != %s"),
2527 oid_to_hex(&cur_oid),
2528 oid_to_hex(&graph_parents->item->object.oid),
2529 oid_to_hex(&odb_parents->item->object.oid));
2531 generation = commit_graph_generation(graph_parents->item);
2532 if (generation > max_generation)
2533 max_generation = generation;
2535 graph_parents = graph_parents->next;
2536 odb_parents = odb_parents->next;
2539 if (odb_parents != NULL)
2540 graph_report(_("commit-graph parent list for commit %s terminates early"),
2541 oid_to_hex(&cur_oid));
2543 if (!commit_graph_generation(graph_commit)) {
2544 if (generation_zero == GENERATION_NUMBER_EXISTS)
2545 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2546 oid_to_hex(&cur_oid));
2547 generation_zero = GENERATION_ZERO_EXISTS;
2548 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2549 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2550 oid_to_hex(&cur_oid));
2552 if (generation_zero == GENERATION_ZERO_EXISTS)
2553 continue;
2556 * If we are using topological level and one of our parents has
2557 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2558 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2559 * in the following condition.
2561 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2562 max_generation--;
2564 generation = commit_graph_generation(graph_commit);
2565 if (generation < max_generation + 1)
2566 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2567 oid_to_hex(&cur_oid),
2568 generation,
2569 max_generation + 1);
2571 if (graph_commit->date != odb_commit->date)
2572 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2573 oid_to_hex(&cur_oid),
2574 graph_commit->date,
2575 odb_commit->date);
2577 stop_progress(&progress);
2579 local_error = verify_commit_graph_error;
2581 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2582 local_error |= verify_commit_graph(r, g->base_graph, flags);
2584 return local_error;
2587 void free_commit_graph(struct commit_graph *g)
2589 if (!g)
2590 return;
2591 if (g->data) {
2592 munmap((void *)g->data, g->data_len);
2593 g->data = NULL;
2595 free(g->filename);
2596 free(g->bloom_filter_settings);
2597 free(g);
2600 void disable_commit_graph(struct repository *r)
2602 r->commit_graph_disabled = 1;