Merge branch 'fz/po-zh_CN' of github.com:fangyi-zhou/git-po
[alt-git.git] / commit-graph.c
blob92d450333664d4298eb9cbcda332fe857fd22458
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 struct commit_graph *alloc_commit_graph(void)
198 struct commit_graph *g = xcalloc(1, sizeof(*g));
200 return g;
203 extern int read_replace_refs;
205 static int commit_graph_compatible(struct repository *r)
207 if (!r->gitdir)
208 return 0;
210 if (read_replace_refs) {
211 prepare_replace_object(r);
212 if (hashmap_get_size(&r->objects->replace_map->map))
213 return 0;
216 prepare_commit_graft(r);
217 if (r->parsed_objects &&
218 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
219 return 0;
220 if (is_repository_shallow(r))
221 return 0;
223 return 1;
226 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
228 *fd = git_open(graph_file);
229 if (*fd < 0)
230 return 0;
231 if (fstat(*fd, st)) {
232 close(*fd);
233 return 0;
235 return 1;
238 struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
239 int fd, struct stat *st,
240 struct object_directory *odb)
242 void *graph_map;
243 size_t graph_size;
244 struct commit_graph *ret;
246 graph_size = xsize_t(st->st_size);
248 if (graph_size < GRAPH_MIN_SIZE) {
249 close(fd);
250 error(_("commit-graph file is too small"));
251 return NULL;
253 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
254 close(fd);
255 ret = parse_commit_graph(r, graph_map, graph_size);
257 if (ret)
258 ret->odb = odb;
259 else
260 munmap(graph_map, graph_size);
262 return ret;
265 static int verify_commit_graph_lite(struct commit_graph *g)
268 * Basic validation shared between parse_commit_graph()
269 * which'll be called every time the graph is used, and the
270 * much more expensive verify_commit_graph() used by
271 * "commit-graph verify".
273 * There should only be very basic checks here to ensure that
274 * we don't e.g. segfault in fill_commit_in_graph(), but
275 * because this is a very hot codepath nothing that e.g. loops
276 * over g->num_commits, or runs a checksum on the commit-graph
277 * itself.
279 if (!g->chunk_oid_fanout) {
280 error("commit-graph is missing the OID Fanout chunk");
281 return 1;
283 if (!g->chunk_oid_lookup) {
284 error("commit-graph is missing the OID Lookup chunk");
285 return 1;
287 if (!g->chunk_commit_data) {
288 error("commit-graph is missing the Commit Data chunk");
289 return 1;
292 return 0;
295 static int graph_read_oid_lookup(const unsigned char *chunk_start,
296 size_t chunk_size, void *data)
298 struct commit_graph *g = data;
299 g->chunk_oid_lookup = chunk_start;
300 g->num_commits = chunk_size / g->hash_len;
301 return 0;
304 static int graph_read_bloom_data(const unsigned char *chunk_start,
305 size_t chunk_size, void *data)
307 struct commit_graph *g = data;
308 uint32_t hash_version;
309 g->chunk_bloom_data = chunk_start;
310 hash_version = get_be32(chunk_start);
312 if (hash_version != 1)
313 return 0;
315 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
316 g->bloom_filter_settings->hash_version = hash_version;
317 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
318 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
319 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
321 return 0;
324 struct commit_graph *parse_commit_graph(struct repository *r,
325 void *graph_map, size_t graph_size)
327 const unsigned char *data;
328 struct commit_graph *graph;
329 uint32_t graph_signature;
330 unsigned char graph_version, hash_version;
331 struct chunkfile *cf = NULL;
333 if (!graph_map)
334 return NULL;
336 if (graph_size < GRAPH_MIN_SIZE)
337 return NULL;
339 data = (const unsigned char *)graph_map;
341 graph_signature = get_be32(data);
342 if (graph_signature != GRAPH_SIGNATURE) {
343 error(_("commit-graph signature %X does not match signature %X"),
344 graph_signature, GRAPH_SIGNATURE);
345 return NULL;
348 graph_version = *(unsigned char*)(data + 4);
349 if (graph_version != GRAPH_VERSION) {
350 error(_("commit-graph version %X does not match version %X"),
351 graph_version, GRAPH_VERSION);
352 return NULL;
355 hash_version = *(unsigned char*)(data + 5);
356 if (hash_version != oid_version(the_hash_algo)) {
357 error(_("commit-graph hash version %X does not match version %X"),
358 hash_version, oid_version(the_hash_algo));
359 return NULL;
362 prepare_repo_settings(r);
364 graph = alloc_commit_graph();
366 graph->hash_len = the_hash_algo->rawsz;
367 graph->num_chunks = *(unsigned char*)(data + 6);
368 graph->data = graph_map;
369 graph->data_len = graph_size;
371 if (graph_size < GRAPH_HEADER_SIZE +
372 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
373 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
374 error(_("commit-graph file is too small to hold %u chunks"),
375 graph->num_chunks);
376 free(graph);
377 return NULL;
380 cf = init_chunkfile(NULL);
382 if (read_table_of_contents(cf, graph->data, graph_size,
383 GRAPH_HEADER_SIZE, graph->num_chunks))
384 goto free_and_return;
386 pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT,
387 (const unsigned char **)&graph->chunk_oid_fanout);
388 read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph);
389 pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data);
390 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges);
391 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs);
393 if (get_configured_generation_version(r) >= 2) {
394 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
395 &graph->chunk_generation_data);
396 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
397 &graph->chunk_generation_data_overflow);
399 if (graph->chunk_generation_data)
400 graph->read_generation_data = 1;
403 if (r->settings.commit_graph_read_changed_paths) {
404 pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
405 &graph->chunk_bloom_indexes);
406 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
407 graph_read_bloom_data, graph);
410 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
411 init_bloom_filters();
412 } else {
413 /* We need both the bloom chunks to exist together. Else ignore the data */
414 graph->chunk_bloom_indexes = NULL;
415 graph->chunk_bloom_data = NULL;
416 FREE_AND_NULL(graph->bloom_filter_settings);
419 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len);
421 if (verify_commit_graph_lite(graph))
422 goto free_and_return;
424 free_chunkfile(cf);
425 return graph;
427 free_and_return:
428 free_chunkfile(cf);
429 free(graph->bloom_filter_settings);
430 free(graph);
431 return NULL;
434 static struct commit_graph *load_commit_graph_one(struct repository *r,
435 const char *graph_file,
436 struct object_directory *odb)
439 struct stat st;
440 int fd;
441 struct commit_graph *g;
442 int open_ok = open_commit_graph(graph_file, &fd, &st);
444 if (!open_ok)
445 return NULL;
447 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
449 if (g)
450 g->filename = xstrdup(graph_file);
452 return g;
455 static struct commit_graph *load_commit_graph_v1(struct repository *r,
456 struct object_directory *odb)
458 char *graph_name = get_commit_graph_filename(odb);
459 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
460 free(graph_name);
462 return g;
465 static int add_graph_to_chain(struct commit_graph *g,
466 struct commit_graph *chain,
467 struct object_id *oids,
468 int n)
470 struct commit_graph *cur_g = chain;
472 if (n && !g->chunk_base_graphs) {
473 warning(_("commit-graph has no base graphs chunk"));
474 return 0;
477 while (n) {
478 n--;
480 if (!cur_g ||
481 !oideq(&oids[n], &cur_g->oid) ||
482 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
483 warning(_("commit-graph chain does not match"));
484 return 0;
487 cur_g = cur_g->base_graph;
490 g->base_graph = chain;
492 if (chain)
493 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
495 return 1;
498 static struct commit_graph *load_commit_graph_chain(struct repository *r,
499 struct object_directory *odb)
501 struct commit_graph *graph_chain = NULL;
502 struct strbuf line = STRBUF_INIT;
503 struct stat st;
504 struct object_id *oids;
505 int i = 0, valid = 1, count;
506 char *chain_name = get_commit_graph_chain_filename(odb);
507 FILE *fp;
508 int stat_res;
510 fp = fopen(chain_name, "r");
511 stat_res = stat(chain_name, &st);
512 free(chain_name);
514 if (!fp)
515 return NULL;
516 if (stat_res ||
517 st.st_size <= the_hash_algo->hexsz) {
518 fclose(fp);
519 return NULL;
522 count = st.st_size / (the_hash_algo->hexsz + 1);
523 CALLOC_ARRAY(oids, count);
525 prepare_alt_odb(r);
527 for (i = 0; i < count; i++) {
528 struct object_directory *odb;
530 if (strbuf_getline_lf(&line, fp) == EOF)
531 break;
533 if (get_oid_hex(line.buf, &oids[i])) {
534 warning(_("invalid commit-graph chain: line '%s' not a hash"),
535 line.buf);
536 valid = 0;
537 break;
540 valid = 0;
541 for (odb = r->objects->odb; odb; odb = odb->next) {
542 char *graph_name = get_split_graph_filename(odb, line.buf);
543 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
545 free(graph_name);
547 if (g) {
548 if (add_graph_to_chain(g, graph_chain, oids, i)) {
549 graph_chain = g;
550 valid = 1;
553 break;
557 if (!valid) {
558 warning(_("unable to find all commit-graph files"));
559 break;
563 free(oids);
564 fclose(fp);
565 strbuf_release(&line);
567 return graph_chain;
571 * returns 1 if and only if all graphs in the chain have
572 * corrected commit dates stored in the generation_data chunk.
574 static int validate_mixed_generation_chain(struct commit_graph *g)
576 int read_generation_data = 1;
577 struct commit_graph *p = g;
579 while (read_generation_data && p) {
580 read_generation_data = p->read_generation_data;
581 p = p->base_graph;
584 if (read_generation_data)
585 return 1;
587 while (g) {
588 g->read_generation_data = 0;
589 g = g->base_graph;
592 return 0;
595 struct commit_graph *read_commit_graph_one(struct repository *r,
596 struct object_directory *odb)
598 struct commit_graph *g = load_commit_graph_v1(r, odb);
600 if (!g)
601 g = load_commit_graph_chain(r, odb);
603 validate_mixed_generation_chain(g);
605 return g;
608 static void prepare_commit_graph_one(struct repository *r,
609 struct object_directory *odb)
612 if (r->objects->commit_graph)
613 return;
615 r->objects->commit_graph = read_commit_graph_one(r, odb);
619 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
621 * On the first invocation, this function attempts to load the commit
622 * graph if the_repository is configured to have one.
624 static int prepare_commit_graph(struct repository *r)
626 struct object_directory *odb;
629 * Early return if there is no git dir or if the commit graph is
630 * disabled.
632 * This must come before the "already attempted?" check below, because
633 * we want to disable even an already-loaded graph file.
635 if (!r->gitdir || r->commit_graph_disabled)
636 return 0;
638 if (r->objects->commit_graph_attempted)
639 return !!r->objects->commit_graph;
640 r->objects->commit_graph_attempted = 1;
642 prepare_repo_settings(r);
644 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
645 r->settings.core_commit_graph != 1)
647 * This repository is not configured to use commit graphs, so
648 * do not load one. (But report commit_graph_attempted anyway
649 * so that commit graph loading is not attempted again for this
650 * repository.)
652 return 0;
654 if (!commit_graph_compatible(r))
655 return 0;
657 prepare_alt_odb(r);
658 for (odb = r->objects->odb;
659 !r->objects->commit_graph && odb;
660 odb = odb->next)
661 prepare_commit_graph_one(r, odb);
662 return !!r->objects->commit_graph;
665 int generation_numbers_enabled(struct repository *r)
667 uint32_t first_generation;
668 struct commit_graph *g;
669 if (!prepare_commit_graph(r))
670 return 0;
672 g = r->objects->commit_graph;
674 if (!g->num_commits)
675 return 0;
677 first_generation = get_be32(g->chunk_commit_data +
678 g->hash_len + 8) >> 2;
680 return !!first_generation;
683 int corrected_commit_dates_enabled(struct repository *r)
685 struct commit_graph *g;
686 if (!prepare_commit_graph(r))
687 return 0;
689 g = r->objects->commit_graph;
691 if (!g->num_commits)
692 return 0;
694 return g->read_generation_data;
697 struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
699 struct commit_graph *g = r->objects->commit_graph;
700 while (g) {
701 if (g->bloom_filter_settings)
702 return g->bloom_filter_settings;
703 g = g->base_graph;
705 return NULL;
708 static void close_commit_graph_one(struct commit_graph *g)
710 if (!g)
711 return;
713 clear_commit_graph_data_slab(&commit_graph_data_slab);
714 close_commit_graph_one(g->base_graph);
715 free_commit_graph(g);
718 void close_commit_graph(struct raw_object_store *o)
720 close_commit_graph_one(o->commit_graph);
721 o->commit_graph = NULL;
724 static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
726 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
727 g->chunk_oid_lookup, g->hash_len, pos);
730 static void load_oid_from_graph(struct commit_graph *g,
731 uint32_t pos,
732 struct object_id *oid)
734 uint32_t lex_index;
736 while (g && pos < g->num_commits_in_base)
737 g = g->base_graph;
739 if (!g)
740 BUG("NULL commit-graph");
742 if (pos >= g->num_commits + g->num_commits_in_base)
743 die(_("invalid commit position. commit-graph is likely corrupt"));
745 lex_index = pos - g->num_commits_in_base;
747 oidread(oid, g->chunk_oid_lookup + g->hash_len * lex_index);
750 static struct commit_list **insert_parent_or_die(struct repository *r,
751 struct commit_graph *g,
752 uint32_t pos,
753 struct commit_list **pptr)
755 struct commit *c;
756 struct object_id oid;
758 if (pos >= g->num_commits + g->num_commits_in_base)
759 die("invalid parent position %"PRIu32, pos);
761 load_oid_from_graph(g, pos, &oid);
762 c = lookup_commit(r, &oid);
763 if (!c)
764 die(_("could not find commit %s"), oid_to_hex(&oid));
765 commit_graph_data_at(c)->graph_pos = pos;
766 return &commit_list_insert(c, pptr)->next;
769 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
771 const unsigned char *commit_data;
772 struct commit_graph_data *graph_data;
773 uint32_t lex_index, offset_pos;
774 uint64_t date_high, date_low, offset;
776 while (pos < g->num_commits_in_base)
777 g = g->base_graph;
779 if (pos >= g->num_commits + g->num_commits_in_base)
780 die(_("invalid commit position. commit-graph is likely corrupt"));
782 lex_index = pos - g->num_commits_in_base;
783 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
785 graph_data = commit_graph_data_at(item);
786 graph_data->graph_pos = pos;
788 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
789 date_low = get_be32(commit_data + g->hash_len + 12);
790 item->date = (timestamp_t)((date_high << 32) | date_low);
792 if (g->read_generation_data) {
793 offset = (timestamp_t)get_be32(g->chunk_generation_data + sizeof(uint32_t) * lex_index);
795 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
796 if (!g->chunk_generation_data_overflow)
797 die(_("commit-graph requires overflow generation data but has none"));
799 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
800 graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + 8 * offset_pos);
801 } else
802 graph_data->generation = item->date + offset;
803 } else
804 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
806 if (g->topo_levels)
807 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2;
810 static inline void set_commit_tree(struct commit *c, struct tree *t)
812 c->maybe_tree = t;
815 static int fill_commit_in_graph(struct repository *r,
816 struct commit *item,
817 struct commit_graph *g, uint32_t pos)
819 uint32_t edge_value;
820 uint32_t *parent_data_ptr;
821 struct commit_list **pptr;
822 const unsigned char *commit_data;
823 uint32_t lex_index;
825 while (pos < g->num_commits_in_base)
826 g = g->base_graph;
828 fill_commit_graph_info(item, g, pos);
830 lex_index = pos - g->num_commits_in_base;
831 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
833 item->object.parsed = 1;
835 set_commit_tree(item, NULL);
837 pptr = &item->parents;
839 edge_value = get_be32(commit_data + g->hash_len);
840 if (edge_value == GRAPH_PARENT_NONE)
841 return 1;
842 pptr = insert_parent_or_die(r, g, edge_value, pptr);
844 edge_value = get_be32(commit_data + g->hash_len + 4);
845 if (edge_value == GRAPH_PARENT_NONE)
846 return 1;
847 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
848 pptr = insert_parent_or_die(r, g, edge_value, pptr);
849 return 1;
852 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
853 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
854 do {
855 edge_value = get_be32(parent_data_ptr);
856 pptr = insert_parent_or_die(r, g,
857 edge_value & GRAPH_EDGE_LAST_MASK,
858 pptr);
859 parent_data_ptr++;
860 } while (!(edge_value & GRAPH_LAST_EDGE));
862 return 1;
865 static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
867 struct commit_graph *cur_g = g;
868 uint32_t lex_index;
870 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
871 cur_g = cur_g->base_graph;
873 if (cur_g) {
874 *pos = lex_index + cur_g->num_commits_in_base;
875 return 1;
878 return 0;
881 static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
883 uint32_t graph_pos = commit_graph_position(item);
884 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
885 *pos = graph_pos;
886 return 1;
887 } else {
888 return search_commit_pos_in_graph(&item->object.oid, g, pos);
892 struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
894 struct commit *commit;
895 uint32_t pos;
897 if (!repo->objects->commit_graph)
898 return NULL;
899 if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos))
900 return NULL;
901 if (!repo_has_object_file(repo, id))
902 return NULL;
904 commit = lookup_commit(repo, id);
905 if (!commit)
906 return NULL;
907 if (commit->object.parsed)
908 return commit;
910 if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos))
911 return NULL;
913 return commit;
916 static int parse_commit_in_graph_one(struct repository *r,
917 struct commit_graph *g,
918 struct commit *item)
920 uint32_t pos;
922 if (item->object.parsed)
923 return 1;
925 if (find_commit_pos_in_graph(item, g, &pos))
926 return fill_commit_in_graph(r, item, g, pos);
928 return 0;
931 int parse_commit_in_graph(struct repository *r, struct commit *item)
933 static int checked_env = 0;
935 if (!checked_env &&
936 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
937 die("dying as requested by the '%s' variable on commit-graph parse!",
938 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
939 checked_env = 1;
941 if (!prepare_commit_graph(r))
942 return 0;
943 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
946 void load_commit_graph_info(struct repository *r, struct commit *item)
948 uint32_t pos;
949 if (!prepare_commit_graph(r))
950 return;
951 if (find_commit_pos_in_graph(item, r->objects->commit_graph, &pos))
952 fill_commit_graph_info(item, r->objects->commit_graph, pos);
955 static struct tree *load_tree_for_commit(struct repository *r,
956 struct commit_graph *g,
957 struct commit *c)
959 struct object_id oid;
960 const unsigned char *commit_data;
961 uint32_t graph_pos = commit_graph_position(c);
963 while (graph_pos < g->num_commits_in_base)
964 g = g->base_graph;
966 commit_data = g->chunk_commit_data +
967 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
969 oidread(&oid, commit_data);
970 set_commit_tree(c, lookup_tree(r, &oid));
972 return c->maybe_tree;
975 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
976 struct commit_graph *g,
977 const struct commit *c)
979 if (c->maybe_tree)
980 return c->maybe_tree;
981 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
982 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
984 return load_tree_for_commit(r, g, (struct commit *)c);
987 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
989 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
992 struct packed_commit_list {
993 struct commit **list;
994 size_t nr;
995 size_t alloc;
998 struct write_commit_graph_context {
999 struct repository *r;
1000 struct object_directory *odb;
1001 char *graph_name;
1002 struct oid_array oids;
1003 struct packed_commit_list commits;
1004 int num_extra_edges;
1005 int num_generation_data_overflows;
1006 unsigned long approx_nr_objects;
1007 struct progress *progress;
1008 int progress_done;
1009 uint64_t progress_cnt;
1011 char *base_graph_name;
1012 int num_commit_graphs_before;
1013 int num_commit_graphs_after;
1014 char **commit_graph_filenames_before;
1015 char **commit_graph_filenames_after;
1016 char **commit_graph_hash_after;
1017 uint32_t new_num_commits_in_base;
1018 struct commit_graph *new_base_graph;
1020 unsigned append:1,
1021 report_progress:1,
1022 split:1,
1023 changed_paths:1,
1024 order_by_pack:1,
1025 write_generation_data:1,
1026 trust_generation_numbers:1;
1028 struct topo_level_slab *topo_levels;
1029 const struct commit_graph_opts *opts;
1030 size_t total_bloom_filter_data_size;
1031 const struct bloom_filter_settings *bloom_settings;
1033 int count_bloom_filter_computed;
1034 int count_bloom_filter_not_computed;
1035 int count_bloom_filter_trunc_empty;
1036 int count_bloom_filter_trunc_large;
1039 static int write_graph_chunk_fanout(struct hashfile *f,
1040 void *data)
1042 struct write_commit_graph_context *ctx = data;
1043 int i, count = 0;
1044 struct commit **list = ctx->commits.list;
1047 * Write the first-level table (the list is sorted,
1048 * but we use a 256-entry lookup to be able to avoid
1049 * having to do eight extra binary search iterations).
1051 for (i = 0; i < 256; i++) {
1052 while (count < ctx->commits.nr) {
1053 if ((*list)->object.oid.hash[0] != i)
1054 break;
1055 display_progress(ctx->progress, ++ctx->progress_cnt);
1056 count++;
1057 list++;
1060 hashwrite_be32(f, count);
1063 return 0;
1066 static int write_graph_chunk_oids(struct hashfile *f,
1067 void *data)
1069 struct write_commit_graph_context *ctx = data;
1070 struct commit **list = ctx->commits.list;
1071 int count;
1072 for (count = 0; count < ctx->commits.nr; count++, list++) {
1073 display_progress(ctx->progress, ++ctx->progress_cnt);
1074 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
1077 return 0;
1080 static const struct object_id *commit_to_oid(size_t index, const void *table)
1082 const struct commit * const *commits = table;
1083 return &commits[index]->object.oid;
1086 static int write_graph_chunk_data(struct hashfile *f,
1087 void *data)
1089 struct write_commit_graph_context *ctx = data;
1090 struct commit **list = ctx->commits.list;
1091 struct commit **last = ctx->commits.list + ctx->commits.nr;
1092 uint32_t num_extra_edges = 0;
1094 while (list < last) {
1095 struct commit_list *parent;
1096 struct object_id *tree;
1097 int edge_value;
1098 uint32_t packedDate[2];
1099 display_progress(ctx->progress, ++ctx->progress_cnt);
1101 if (repo_parse_commit_no_graph(ctx->r, *list))
1102 die(_("unable to parse commit %s"),
1103 oid_to_hex(&(*list)->object.oid));
1104 tree = get_commit_tree_oid(*list);
1105 hashwrite(f, tree->hash, the_hash_algo->rawsz);
1107 parent = (*list)->parents;
1109 if (!parent)
1110 edge_value = GRAPH_PARENT_NONE;
1111 else {
1112 edge_value = oid_pos(&parent->item->object.oid,
1113 ctx->commits.list,
1114 ctx->commits.nr,
1115 commit_to_oid);
1117 if (edge_value >= 0)
1118 edge_value += ctx->new_num_commits_in_base;
1119 else if (ctx->new_base_graph) {
1120 uint32_t pos;
1121 if (find_commit_pos_in_graph(parent->item,
1122 ctx->new_base_graph,
1123 &pos))
1124 edge_value = pos;
1127 if (edge_value < 0)
1128 BUG("missing parent %s for commit %s",
1129 oid_to_hex(&parent->item->object.oid),
1130 oid_to_hex(&(*list)->object.oid));
1133 hashwrite_be32(f, edge_value);
1135 if (parent)
1136 parent = parent->next;
1138 if (!parent)
1139 edge_value = GRAPH_PARENT_NONE;
1140 else if (parent->next)
1141 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1142 else {
1143 edge_value = oid_pos(&parent->item->object.oid,
1144 ctx->commits.list,
1145 ctx->commits.nr,
1146 commit_to_oid);
1148 if (edge_value >= 0)
1149 edge_value += ctx->new_num_commits_in_base;
1150 else if (ctx->new_base_graph) {
1151 uint32_t pos;
1152 if (find_commit_pos_in_graph(parent->item,
1153 ctx->new_base_graph,
1154 &pos))
1155 edge_value = pos;
1158 if (edge_value < 0)
1159 BUG("missing parent %s for commit %s",
1160 oid_to_hex(&parent->item->object.oid),
1161 oid_to_hex(&(*list)->object.oid));
1164 hashwrite_be32(f, edge_value);
1166 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1167 do {
1168 num_extra_edges++;
1169 parent = parent->next;
1170 } while (parent);
1173 if (sizeof((*list)->date) > 4)
1174 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1175 else
1176 packedDate[0] = 0;
1178 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1180 packedDate[1] = htonl((*list)->date);
1181 hashwrite(f, packedDate, 8);
1183 list++;
1186 return 0;
1189 static int write_graph_chunk_generation_data(struct hashfile *f,
1190 void *data)
1192 struct write_commit_graph_context *ctx = data;
1193 int i, num_generation_data_overflows = 0;
1195 for (i = 0; i < ctx->commits.nr; i++) {
1196 struct commit *c = ctx->commits.list[i];
1197 timestamp_t offset;
1198 repo_parse_commit(ctx->r, c);
1199 offset = commit_graph_data_at(c)->generation - c->date;
1200 display_progress(ctx->progress, ++ctx->progress_cnt);
1202 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1203 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1204 num_generation_data_overflows++;
1207 hashwrite_be32(f, offset);
1210 return 0;
1213 static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1214 void *data)
1216 struct write_commit_graph_context *ctx = data;
1217 int i;
1218 for (i = 0; i < ctx->commits.nr; i++) {
1219 struct commit *c = ctx->commits.list[i];
1220 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1221 display_progress(ctx->progress, ++ctx->progress_cnt);
1223 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1224 hashwrite_be32(f, offset >> 32);
1225 hashwrite_be32(f, (uint32_t) offset);
1229 return 0;
1232 static int write_graph_chunk_extra_edges(struct hashfile *f,
1233 void *data)
1235 struct write_commit_graph_context *ctx = data;
1236 struct commit **list = ctx->commits.list;
1237 struct commit **last = ctx->commits.list + ctx->commits.nr;
1238 struct commit_list *parent;
1240 while (list < last) {
1241 int num_parents = 0;
1243 display_progress(ctx->progress, ++ctx->progress_cnt);
1245 for (parent = (*list)->parents; num_parents < 3 && parent;
1246 parent = parent->next)
1247 num_parents++;
1249 if (num_parents <= 2) {
1250 list++;
1251 continue;
1254 /* Since num_parents > 2, this initializer is safe. */
1255 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1256 int edge_value = oid_pos(&parent->item->object.oid,
1257 ctx->commits.list,
1258 ctx->commits.nr,
1259 commit_to_oid);
1261 if (edge_value >= 0)
1262 edge_value += ctx->new_num_commits_in_base;
1263 else if (ctx->new_base_graph) {
1264 uint32_t pos;
1265 if (find_commit_pos_in_graph(parent->item,
1266 ctx->new_base_graph,
1267 &pos))
1268 edge_value = pos;
1271 if (edge_value < 0)
1272 BUG("missing parent %s for commit %s",
1273 oid_to_hex(&parent->item->object.oid),
1274 oid_to_hex(&(*list)->object.oid));
1275 else if (!parent->next)
1276 edge_value |= GRAPH_LAST_EDGE;
1278 hashwrite_be32(f, edge_value);
1281 list++;
1284 return 0;
1287 static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1288 void *data)
1290 struct write_commit_graph_context *ctx = data;
1291 struct commit **list = ctx->commits.list;
1292 struct commit **last = ctx->commits.list + ctx->commits.nr;
1293 uint32_t cur_pos = 0;
1295 while (list < last) {
1296 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1297 size_t len = filter ? filter->len : 0;
1298 cur_pos += len;
1299 display_progress(ctx->progress, ++ctx->progress_cnt);
1300 hashwrite_be32(f, cur_pos);
1301 list++;
1304 return 0;
1307 static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1309 struct json_writer jw = JSON_WRITER_INIT;
1311 jw_object_begin(&jw, 0);
1312 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1313 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1314 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1315 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1316 jw_end(&jw);
1318 trace2_data_json("bloom", ctx->r, "settings", &jw);
1320 jw_release(&jw);
1323 static int write_graph_chunk_bloom_data(struct hashfile *f,
1324 void *data)
1326 struct write_commit_graph_context *ctx = data;
1327 struct commit **list = ctx->commits.list;
1328 struct commit **last = ctx->commits.list + ctx->commits.nr;
1330 trace2_bloom_filter_settings(ctx);
1332 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1333 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1334 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1336 while (list < last) {
1337 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1338 size_t len = filter ? filter->len : 0;
1340 display_progress(ctx->progress, ++ctx->progress_cnt);
1341 if (len)
1342 hashwrite(f, filter->data, len * sizeof(unsigned char));
1343 list++;
1346 return 0;
1349 static int add_packed_commits(const struct object_id *oid,
1350 struct packed_git *pack,
1351 uint32_t pos,
1352 void *data)
1354 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1355 enum object_type type;
1356 off_t offset = nth_packed_object_offset(pack, pos);
1357 struct object_info oi = OBJECT_INFO_INIT;
1359 if (ctx->progress)
1360 display_progress(ctx->progress, ++ctx->progress_done);
1362 oi.typep = &type;
1363 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1364 die(_("unable to get type of object %s"), oid_to_hex(oid));
1366 if (type != OBJ_COMMIT)
1367 return 0;
1369 oid_array_append(&ctx->oids, oid);
1370 set_commit_pos(ctx->r, oid);
1372 return 0;
1375 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1377 struct commit_list *parent;
1378 for (parent = commit->parents; parent; parent = parent->next) {
1379 if (!(parent->item->object.flags & REACHABLE)) {
1380 oid_array_append(&ctx->oids, &parent->item->object.oid);
1381 parent->item->object.flags |= REACHABLE;
1386 static void close_reachable(struct write_commit_graph_context *ctx)
1388 int i;
1389 struct commit *commit;
1390 enum commit_graph_split_flags flags = ctx->opts ?
1391 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1393 if (ctx->report_progress)
1394 ctx->progress = start_delayed_progress(
1395 _("Loading known commits 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]);
1400 if (commit)
1401 commit->object.flags |= REACHABLE;
1403 stop_progress(&ctx->progress);
1406 * As this loop runs, ctx->oids.nr may grow, but not more
1407 * than the number of missing commits in the reachable
1408 * closure.
1410 if (ctx->report_progress)
1411 ctx->progress = start_delayed_progress(
1412 _("Expanding reachable commits in commit graph"),
1414 for (i = 0; i < ctx->oids.nr; i++) {
1415 display_progress(ctx->progress, i + 1);
1416 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1418 if (!commit)
1419 continue;
1420 if (ctx->split) {
1421 if ((!repo_parse_commit(ctx->r, commit) &&
1422 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1423 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1424 add_missing_parents(ctx, commit);
1425 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1426 add_missing_parents(ctx, commit);
1428 stop_progress(&ctx->progress);
1430 if (ctx->report_progress)
1431 ctx->progress = start_delayed_progress(
1432 _("Clearing commit marks in commit graph"),
1433 ctx->oids.nr);
1434 for (i = 0; i < ctx->oids.nr; i++) {
1435 display_progress(ctx->progress, i + 1);
1436 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1438 if (commit)
1439 commit->object.flags &= ~REACHABLE;
1441 stop_progress(&ctx->progress);
1444 static void compute_topological_levels(struct write_commit_graph_context *ctx)
1446 int i;
1447 struct commit_list *list = NULL;
1449 if (ctx->report_progress)
1450 ctx->progress = start_delayed_progress(
1451 _("Computing commit graph topological levels"),
1452 ctx->commits.nr);
1453 for (i = 0; i < ctx->commits.nr; i++) {
1454 struct commit *c = ctx->commits.list[i];
1455 uint32_t level;
1457 repo_parse_commit(ctx->r, c);
1458 level = *topo_level_slab_at(ctx->topo_levels, c);
1460 display_progress(ctx->progress, i + 1);
1461 if (level != GENERATION_NUMBER_ZERO)
1462 continue;
1464 commit_list_insert(c, &list);
1465 while (list) {
1466 struct commit *current = list->item;
1467 struct commit_list *parent;
1468 int all_parents_computed = 1;
1469 uint32_t max_level = 0;
1471 for (parent = current->parents; parent; parent = parent->next) {
1472 repo_parse_commit(ctx->r, parent->item);
1473 level = *topo_level_slab_at(ctx->topo_levels, parent->item);
1475 if (level == GENERATION_NUMBER_ZERO) {
1476 all_parents_computed = 0;
1477 commit_list_insert(parent->item, &list);
1478 break;
1481 if (level > max_level)
1482 max_level = level;
1485 if (all_parents_computed) {
1486 pop_commit(&list);
1488 if (max_level > GENERATION_NUMBER_V1_MAX - 1)
1489 max_level = GENERATION_NUMBER_V1_MAX - 1;
1490 *topo_level_slab_at(ctx->topo_levels, current) = max_level + 1;
1494 stop_progress(&ctx->progress);
1497 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1499 int i;
1500 struct commit_list *list = NULL;
1502 if (ctx->report_progress)
1503 ctx->progress = start_delayed_progress(
1504 _("Computing commit graph generation numbers"),
1505 ctx->commits.nr);
1507 if (!ctx->trust_generation_numbers) {
1508 for (i = 0; i < ctx->commits.nr; i++) {
1509 struct commit *c = ctx->commits.list[i];
1510 repo_parse_commit(ctx->r, c);
1511 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1515 for (i = 0; i < ctx->commits.nr; i++) {
1516 struct commit *c = ctx->commits.list[i];
1517 timestamp_t corrected_commit_date;
1519 repo_parse_commit(ctx->r, c);
1520 corrected_commit_date = commit_graph_data_at(c)->generation;
1522 display_progress(ctx->progress, i + 1);
1523 if (corrected_commit_date != GENERATION_NUMBER_ZERO)
1524 continue;
1526 commit_list_insert(c, &list);
1527 while (list) {
1528 struct commit *current = list->item;
1529 struct commit_list *parent;
1530 int all_parents_computed = 1;
1531 timestamp_t max_corrected_commit_date = 0;
1533 for (parent = current->parents; parent; parent = parent->next) {
1534 repo_parse_commit(ctx->r, parent->item);
1535 corrected_commit_date = commit_graph_data_at(parent->item)->generation;
1537 if (corrected_commit_date == GENERATION_NUMBER_ZERO) {
1538 all_parents_computed = 0;
1539 commit_list_insert(parent->item, &list);
1540 break;
1543 if (corrected_commit_date > max_corrected_commit_date)
1544 max_corrected_commit_date = corrected_commit_date;
1547 if (all_parents_computed) {
1548 pop_commit(&list);
1550 if (current->date && current->date > max_corrected_commit_date)
1551 max_corrected_commit_date = current->date - 1;
1552 commit_graph_data_at(current)->generation = max_corrected_commit_date + 1;
1557 for (i = 0; i < ctx->commits.nr; i++) {
1558 struct commit *c = ctx->commits.list[i];
1559 timestamp_t offset = commit_graph_data_at(c)->generation - c->date;
1560 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1561 ctx->num_generation_data_overflows++;
1563 stop_progress(&ctx->progress);
1566 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1568 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1569 ctx->count_bloom_filter_computed);
1570 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1571 ctx->count_bloom_filter_not_computed);
1572 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1573 ctx->count_bloom_filter_trunc_empty);
1574 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1575 ctx->count_bloom_filter_trunc_large);
1578 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1580 int i;
1581 struct progress *progress = NULL;
1582 struct commit **sorted_commits;
1583 int max_new_filters;
1585 init_bloom_filters();
1587 if (ctx->report_progress)
1588 progress = start_delayed_progress(
1589 _("Computing commit changed paths Bloom filters"),
1590 ctx->commits.nr);
1592 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1593 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1595 if (ctx->order_by_pack)
1596 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1597 else
1598 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1600 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1601 ctx->opts->max_new_filters : ctx->commits.nr;
1603 for (i = 0; i < ctx->commits.nr; i++) {
1604 enum bloom_filter_computed computed = 0;
1605 struct commit *c = sorted_commits[i];
1606 struct bloom_filter *filter = get_or_compute_bloom_filter(
1607 ctx->r,
1609 ctx->count_bloom_filter_computed < max_new_filters,
1610 ctx->bloom_settings,
1611 &computed);
1612 if (computed & BLOOM_COMPUTED) {
1613 ctx->count_bloom_filter_computed++;
1614 if (computed & BLOOM_TRUNC_EMPTY)
1615 ctx->count_bloom_filter_trunc_empty++;
1616 if (computed & BLOOM_TRUNC_LARGE)
1617 ctx->count_bloom_filter_trunc_large++;
1618 } else if (computed & BLOOM_NOT_COMPUTED)
1619 ctx->count_bloom_filter_not_computed++;
1620 ctx->total_bloom_filter_data_size += filter
1621 ? sizeof(unsigned char) * filter->len : 0;
1622 display_progress(progress, i + 1);
1625 if (trace2_is_enabled())
1626 trace2_bloom_filter_write_statistics(ctx);
1628 free(sorted_commits);
1629 stop_progress(&progress);
1632 struct refs_cb_data {
1633 struct oidset *commits;
1634 struct progress *progress;
1637 static int add_ref_to_set(const char *refname,
1638 const struct object_id *oid,
1639 int flags, void *cb_data)
1641 struct object_id peeled;
1642 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1644 if (!peel_iterated_oid(oid, &peeled))
1645 oid = &peeled;
1646 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1647 oidset_insert(data->commits, oid);
1649 display_progress(data->progress, oidset_size(data->commits));
1651 return 0;
1654 int write_commit_graph_reachable(struct object_directory *odb,
1655 enum commit_graph_write_flags flags,
1656 const struct commit_graph_opts *opts)
1658 struct oidset commits = OIDSET_INIT;
1659 struct refs_cb_data data;
1660 int result;
1662 memset(&data, 0, sizeof(data));
1663 data.commits = &commits;
1664 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1665 data.progress = start_delayed_progress(
1666 _("Collecting referenced commits"), 0);
1668 for_each_ref(add_ref_to_set, &data);
1670 stop_progress(&data.progress);
1672 result = write_commit_graph(odb, NULL, &commits,
1673 flags, opts);
1675 oidset_clear(&commits);
1676 return result;
1679 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1680 const struct string_list *pack_indexes)
1682 uint32_t i;
1683 struct strbuf progress_title = STRBUF_INIT;
1684 struct strbuf packname = STRBUF_INIT;
1685 int dirlen;
1686 int ret = 0;
1688 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1689 dirlen = packname.len;
1690 if (ctx->report_progress) {
1691 strbuf_addf(&progress_title,
1692 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1693 "Finding commits for commit graph in %"PRIuMAX" packs",
1694 pack_indexes->nr),
1695 (uintmax_t)pack_indexes->nr);
1696 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1697 ctx->progress_done = 0;
1699 for (i = 0; i < pack_indexes->nr; i++) {
1700 struct packed_git *p;
1701 strbuf_setlen(&packname, dirlen);
1702 strbuf_addstr(&packname, pack_indexes->items[i].string);
1703 p = add_packed_git(packname.buf, packname.len, 1);
1704 if (!p) {
1705 ret = error(_("error adding pack %s"), packname.buf);
1706 goto cleanup;
1708 if (open_pack_index(p)) {
1709 ret = error(_("error opening index for %s"), packname.buf);
1710 goto cleanup;
1712 for_each_object_in_pack(p, add_packed_commits, ctx,
1713 FOR_EACH_OBJECT_PACK_ORDER);
1714 close_pack(p);
1715 free(p);
1718 cleanup:
1719 stop_progress(&ctx->progress);
1720 strbuf_release(&progress_title);
1721 strbuf_release(&packname);
1723 return ret;
1726 static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1727 struct oidset *commits)
1729 struct oidset_iter iter;
1730 struct object_id *oid;
1732 if (!oidset_size(commits))
1733 return 0;
1735 oidset_iter_init(commits, &iter);
1736 while ((oid = oidset_iter_next(&iter))) {
1737 oid_array_append(&ctx->oids, oid);
1740 return 0;
1743 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1745 if (ctx->report_progress)
1746 ctx->progress = start_delayed_progress(
1747 _("Finding commits for commit graph among packed objects"),
1748 ctx->approx_nr_objects);
1749 for_each_packed_object(add_packed_commits, ctx,
1750 FOR_EACH_OBJECT_PACK_ORDER);
1751 if (ctx->progress_done < ctx->approx_nr_objects)
1752 display_progress(ctx->progress, ctx->approx_nr_objects);
1753 stop_progress(&ctx->progress);
1756 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1758 uint32_t i;
1759 enum commit_graph_split_flags flags = ctx->opts ?
1760 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1762 ctx->num_extra_edges = 0;
1763 if (ctx->report_progress)
1764 ctx->progress = start_delayed_progress(
1765 _("Finding extra edges in commit graph"),
1766 ctx->oids.nr);
1767 oid_array_sort(&ctx->oids);
1768 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
1769 unsigned int num_parents;
1771 display_progress(ctx->progress, i + 1);
1773 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1774 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1776 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
1777 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
1778 continue;
1780 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1781 repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]);
1782 else
1783 repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]);
1785 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1786 if (num_parents > 2)
1787 ctx->num_extra_edges += num_parents - 1;
1789 ctx->commits.nr++;
1791 stop_progress(&ctx->progress);
1794 static int write_graph_chunk_base_1(struct hashfile *f,
1795 struct commit_graph *g)
1797 int num = 0;
1799 if (!g)
1800 return 0;
1802 num = write_graph_chunk_base_1(f, g->base_graph);
1803 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1804 return num + 1;
1807 static int write_graph_chunk_base(struct hashfile *f,
1808 void *data)
1810 struct write_commit_graph_context *ctx = data;
1811 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1813 if (num != ctx->num_commit_graphs_after - 1) {
1814 error(_("failed to write correct number of base graph ids"));
1815 return -1;
1818 return 0;
1821 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1823 uint32_t i;
1824 int fd;
1825 struct hashfile *f;
1826 struct lock_file lk = LOCK_INIT;
1827 const unsigned hashsz = the_hash_algo->rawsz;
1828 struct strbuf progress_title = STRBUF_INIT;
1829 struct chunkfile *cf;
1830 unsigned char file_hash[GIT_MAX_RAWSZ];
1832 if (ctx->split) {
1833 struct strbuf tmp_file = STRBUF_INIT;
1835 strbuf_addf(&tmp_file,
1836 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1837 ctx->odb->path);
1838 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1839 } else {
1840 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1843 if (safe_create_leading_directories(ctx->graph_name)) {
1844 UNLEAK(ctx->graph_name);
1845 error(_("unable to create leading directories of %s"),
1846 ctx->graph_name);
1847 return -1;
1850 if (ctx->split) {
1851 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
1853 hold_lock_file_for_update_mode(&lk, lock_name,
1854 LOCK_DIE_ON_ERROR, 0444);
1855 free(lock_name);
1857 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1858 if (fd < 0) {
1859 error(_("unable to create temporary graph layer"));
1860 return -1;
1863 if (adjust_shared_perm(ctx->graph_name)) {
1864 error(_("unable to adjust shared permissions for '%s'"),
1865 ctx->graph_name);
1866 return -1;
1869 f = hashfd(fd, ctx->graph_name);
1870 } else {
1871 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1872 LOCK_DIE_ON_ERROR, 0444);
1873 fd = get_lock_file_fd(&lk);
1874 f = hashfd(fd, get_lock_file_path(&lk));
1877 cf = init_chunkfile(f);
1879 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
1880 write_graph_chunk_fanout);
1881 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, hashsz * ctx->commits.nr,
1882 write_graph_chunk_oids);
1883 add_chunk(cf, GRAPH_CHUNKID_DATA, (hashsz + 16) * ctx->commits.nr,
1884 write_graph_chunk_data);
1886 if (ctx->write_generation_data)
1887 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
1888 sizeof(uint32_t) * ctx->commits.nr,
1889 write_graph_chunk_generation_data);
1890 if (ctx->num_generation_data_overflows)
1891 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
1892 sizeof(timestamp_t) * ctx->num_generation_data_overflows,
1893 write_graph_chunk_generation_data_overflow);
1894 if (ctx->num_extra_edges)
1895 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
1896 4 * ctx->num_extra_edges,
1897 write_graph_chunk_extra_edges);
1898 if (ctx->changed_paths) {
1899 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
1900 sizeof(uint32_t) * ctx->commits.nr,
1901 write_graph_chunk_bloom_indexes);
1902 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
1903 sizeof(uint32_t) * 3
1904 + ctx->total_bloom_filter_data_size,
1905 write_graph_chunk_bloom_data);
1907 if (ctx->num_commit_graphs_after > 1)
1908 add_chunk(cf, GRAPH_CHUNKID_BASE,
1909 hashsz * (ctx->num_commit_graphs_after - 1),
1910 write_graph_chunk_base);
1912 hashwrite_be32(f, GRAPH_SIGNATURE);
1914 hashwrite_u8(f, GRAPH_VERSION);
1915 hashwrite_u8(f, oid_version(the_hash_algo));
1916 hashwrite_u8(f, get_num_chunks(cf));
1917 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1919 if (ctx->report_progress) {
1920 strbuf_addf(&progress_title,
1921 Q_("Writing out commit graph in %d pass",
1922 "Writing out commit graph in %d passes",
1923 get_num_chunks(cf)),
1924 get_num_chunks(cf));
1925 ctx->progress = start_delayed_progress(
1926 progress_title.buf,
1927 get_num_chunks(cf) * ctx->commits.nr);
1930 write_chunkfile(cf, ctx);
1932 stop_progress(&ctx->progress);
1933 strbuf_release(&progress_title);
1935 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1936 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1937 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1939 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1940 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1941 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1942 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1945 close_commit_graph(ctx->r->objects);
1946 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
1947 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1948 free_chunkfile(cf);
1950 if (ctx->split) {
1951 FILE *chainf = fdopen_lock_file(&lk, "w");
1952 char *final_graph_name;
1953 int result;
1955 close(fd);
1957 if (!chainf) {
1958 error(_("unable to open commit-graph chain file"));
1959 return -1;
1962 if (ctx->base_graph_name) {
1963 const char *dest;
1964 int idx = ctx->num_commit_graphs_after - 1;
1965 if (ctx->num_commit_graphs_after > 1)
1966 idx--;
1968 dest = ctx->commit_graph_filenames_after[idx];
1970 if (strcmp(ctx->base_graph_name, dest)) {
1971 result = rename(ctx->base_graph_name, dest);
1973 if (result) {
1974 error(_("failed to rename base commit-graph file"));
1975 return -1;
1978 } else {
1979 char *graph_name = get_commit_graph_filename(ctx->odb);
1980 unlink(graph_name);
1981 free(graph_name);
1984 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash));
1985 final_graph_name = get_split_graph_filename(ctx->odb,
1986 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1987 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1989 result = rename(ctx->graph_name, final_graph_name);
1991 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1992 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
1994 if (result) {
1995 error(_("failed to rename temporary commit-graph file"));
1996 return -1;
2000 commit_lock_file(&lk);
2002 return 0;
2005 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
2007 struct commit_graph *g;
2008 uint32_t num_commits;
2009 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2010 uint32_t i;
2012 int max_commits = 0;
2013 int size_mult = 2;
2015 if (ctx->opts) {
2016 max_commits = ctx->opts->max_commits;
2018 if (ctx->opts->size_multiple)
2019 size_mult = ctx->opts->size_multiple;
2021 flags = ctx->opts->split_flags;
2024 g = ctx->r->objects->commit_graph;
2025 num_commits = ctx->commits.nr;
2026 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2027 ctx->num_commit_graphs_after = 1;
2028 else
2029 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2031 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2032 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2033 while (g && (g->num_commits <= size_mult * num_commits ||
2034 (max_commits && num_commits > max_commits))) {
2035 if (g->odb != ctx->odb)
2036 break;
2038 num_commits += g->num_commits;
2039 g = g->base_graph;
2041 ctx->num_commit_graphs_after--;
2045 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2046 ctx->new_base_graph = g;
2047 else if (ctx->num_commit_graphs_after != 1)
2048 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2049 "should be 1 with --split=replace");
2051 if (ctx->num_commit_graphs_after == 2) {
2052 char *old_graph_name = get_commit_graph_filename(g->odb);
2054 if (!strcmp(g->filename, old_graph_name) &&
2055 g->odb != ctx->odb) {
2056 ctx->num_commit_graphs_after = 1;
2057 ctx->new_base_graph = NULL;
2060 free(old_graph_name);
2063 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2064 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2066 for (i = 0; i < ctx->num_commit_graphs_after &&
2067 i < ctx->num_commit_graphs_before; i++)
2068 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2070 i = ctx->num_commit_graphs_before - 1;
2071 g = ctx->r->objects->commit_graph;
2073 while (g) {
2074 if (i < ctx->num_commit_graphs_after)
2075 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2078 * If the topmost remaining layer has generation data chunk, the
2079 * resultant layer also has generation data chunk.
2081 if (i == ctx->num_commit_graphs_after - 2)
2082 ctx->write_generation_data = !!g->chunk_generation_data;
2084 i--;
2085 g = g->base_graph;
2089 static void merge_commit_graph(struct write_commit_graph_context *ctx,
2090 struct commit_graph *g)
2092 uint32_t i;
2093 uint32_t offset = g->num_commits_in_base;
2095 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
2097 for (i = 0; i < g->num_commits; i++) {
2098 struct object_id oid;
2099 struct commit *result;
2101 display_progress(ctx->progress, i + 1);
2103 load_oid_from_graph(g, i + offset, &oid);
2105 /* only add commits if they still exist in the repo */
2106 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2108 if (result) {
2109 ctx->commits.list[ctx->commits.nr] = result;
2110 ctx->commits.nr++;
2115 static int commit_compare(const void *_a, const void *_b)
2117 const struct commit *a = *(const struct commit **)_a;
2118 const struct commit *b = *(const struct commit **)_b;
2119 return oidcmp(&a->object.oid, &b->object.oid);
2122 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2124 uint32_t i, dedup_i = 0;
2126 if (ctx->report_progress)
2127 ctx->progress = start_delayed_progress(
2128 _("Scanning merged commits"),
2129 ctx->commits.nr);
2131 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2133 ctx->num_extra_edges = 0;
2134 for (i = 0; i < ctx->commits.nr; i++) {
2135 display_progress(ctx->progress, i + 1);
2137 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2138 &ctx->commits.list[i]->object.oid)) {
2140 * Silently ignore duplicates. These were likely
2141 * created due to a commit appearing in multiple
2142 * layers of the chain, which is unexpected but
2143 * not invalid. We should make sure there is a
2144 * unique copy in the new layer.
2146 } else {
2147 unsigned int num_parents;
2149 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2150 dedup_i++;
2152 num_parents = commit_list_count(ctx->commits.list[i]->parents);
2153 if (num_parents > 2)
2154 ctx->num_extra_edges += num_parents - 1;
2158 ctx->commits.nr = dedup_i;
2160 stop_progress(&ctx->progress);
2163 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2165 struct commit_graph *g = ctx->r->objects->commit_graph;
2166 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2168 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2169 current_graph_number--;
2171 if (ctx->report_progress)
2172 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
2174 merge_commit_graph(ctx, g);
2175 stop_progress(&ctx->progress);
2177 g = g->base_graph;
2180 if (g) {
2181 ctx->new_base_graph = g;
2182 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2185 if (ctx->new_base_graph)
2186 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2188 sort_and_scan_merged_commits(ctx);
2191 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2193 uint32_t i;
2194 time_t now = time(NULL);
2196 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2197 struct stat st;
2198 struct utimbuf updated_time;
2200 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2201 continue;
2203 updated_time.actime = st.st_atime;
2204 updated_time.modtime = now;
2205 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2209 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2211 struct strbuf path = STRBUF_INIT;
2212 DIR *dir;
2213 struct dirent *de;
2214 size_t dirnamelen;
2215 timestamp_t expire_time = time(NULL);
2217 if (ctx->opts && ctx->opts->expire_time)
2218 expire_time = ctx->opts->expire_time;
2219 if (!ctx->split) {
2220 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
2221 unlink(chain_file_name);
2222 free(chain_file_name);
2223 ctx->num_commit_graphs_after = 0;
2226 strbuf_addstr(&path, ctx->odb->path);
2227 strbuf_addstr(&path, "/info/commit-graphs");
2228 dir = opendir(path.buf);
2230 if (!dir)
2231 goto out;
2233 strbuf_addch(&path, '/');
2234 dirnamelen = path.len;
2235 while ((de = readdir(dir)) != NULL) {
2236 struct stat st;
2237 uint32_t i, found = 0;
2239 strbuf_setlen(&path, dirnamelen);
2240 strbuf_addstr(&path, de->d_name);
2242 if (stat(path.buf, &st) < 0)
2243 continue;
2245 if (st.st_mtime > expire_time)
2246 continue;
2247 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2248 continue;
2250 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2251 if (!strcmp(ctx->commit_graph_filenames_after[i],
2252 path.buf)) {
2253 found = 1;
2254 break;
2258 if (!found)
2259 unlink(path.buf);
2262 out:
2263 strbuf_release(&path);
2266 int write_commit_graph(struct object_directory *odb,
2267 const struct string_list *const pack_indexes,
2268 struct oidset *commits,
2269 enum commit_graph_write_flags flags,
2270 const struct commit_graph_opts *opts)
2272 struct repository *r = the_repository;
2273 struct write_commit_graph_context *ctx;
2274 uint32_t i;
2275 int res = 0;
2276 int replace = 0;
2277 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2278 struct topo_level_slab topo_levels;
2280 prepare_repo_settings(r);
2281 if (!r->settings.core_commit_graph) {
2282 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2283 return 0;
2285 if (!commit_graph_compatible(r))
2286 return 0;
2288 CALLOC_ARRAY(ctx, 1);
2289 ctx->r = r;
2290 ctx->odb = odb;
2291 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2292 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2293 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
2294 ctx->opts = opts;
2295 ctx->total_bloom_filter_data_size = 0;
2296 ctx->write_generation_data = (get_configured_generation_version(r) == 2);
2297 ctx->num_generation_data_overflows = 0;
2299 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2300 bloom_settings.bits_per_entry);
2301 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2302 bloom_settings.num_hashes);
2303 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2304 bloom_settings.max_changed_paths);
2305 ctx->bloom_settings = &bloom_settings;
2307 init_topo_level_slab(&topo_levels);
2308 ctx->topo_levels = &topo_levels;
2310 prepare_commit_graph(ctx->r);
2311 if (ctx->r->objects->commit_graph) {
2312 struct commit_graph *g = ctx->r->objects->commit_graph;
2314 while (g) {
2315 g->topo_levels = &topo_levels;
2316 g = g->base_graph;
2320 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2321 ctx->changed_paths = 1;
2322 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2323 struct commit_graph *g;
2325 g = ctx->r->objects->commit_graph;
2327 /* We have changed-paths already. Keep them in the next graph */
2328 if (g && g->chunk_bloom_data) {
2329 ctx->changed_paths = 1;
2330 ctx->bloom_settings = g->bloom_filter_settings;
2334 if (ctx->split) {
2335 struct commit_graph *g = ctx->r->objects->commit_graph;
2337 while (g) {
2338 ctx->num_commit_graphs_before++;
2339 g = g->base_graph;
2342 if (ctx->num_commit_graphs_before) {
2343 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2344 i = ctx->num_commit_graphs_before;
2345 g = ctx->r->objects->commit_graph;
2347 while (g) {
2348 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2349 g = g->base_graph;
2353 if (ctx->opts)
2354 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2357 ctx->approx_nr_objects = approximate_object_count();
2359 if (ctx->append && ctx->r->objects->commit_graph) {
2360 struct commit_graph *g = ctx->r->objects->commit_graph;
2361 for (i = 0; i < g->num_commits; i++) {
2362 struct object_id oid;
2363 oidread(&oid, g->chunk_oid_lookup + g->hash_len * i);
2364 oid_array_append(&ctx->oids, &oid);
2368 if (pack_indexes) {
2369 ctx->order_by_pack = 1;
2370 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2371 goto cleanup;
2374 if (commits) {
2375 if ((res = fill_oids_from_commits(ctx, commits)))
2376 goto cleanup;
2379 if (!pack_indexes && !commits) {
2380 ctx->order_by_pack = 1;
2381 fill_oids_from_all_packs(ctx);
2384 close_reachable(ctx);
2386 copy_oids_to_commits(ctx);
2388 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
2389 error(_("too many commits to write graph"));
2390 res = -1;
2391 goto cleanup;
2394 if (!ctx->commits.nr && !replace)
2395 goto cleanup;
2397 if (ctx->split) {
2398 split_graph_merge_strategy(ctx);
2400 if (!replace)
2401 merge_commit_graphs(ctx);
2402 } else
2403 ctx->num_commit_graphs_after = 1;
2405 ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph);
2407 compute_topological_levels(ctx);
2408 if (ctx->write_generation_data)
2409 compute_generation_numbers(ctx);
2411 if (ctx->changed_paths)
2412 compute_bloom_filters(ctx);
2414 res = write_commit_graph_file(ctx);
2416 if (ctx->split)
2417 mark_commit_graphs(ctx);
2419 expire_commit_graphs(ctx);
2421 cleanup:
2422 free(ctx->graph_name);
2423 free(ctx->commits.list);
2424 oid_array_clear(&ctx->oids);
2425 clear_topo_level_slab(&topo_levels);
2427 if (ctx->commit_graph_filenames_after) {
2428 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2429 free(ctx->commit_graph_filenames_after[i]);
2430 free(ctx->commit_graph_hash_after[i]);
2433 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2434 free(ctx->commit_graph_filenames_before[i]);
2436 free(ctx->commit_graph_filenames_after);
2437 free(ctx->commit_graph_filenames_before);
2438 free(ctx->commit_graph_hash_after);
2441 free(ctx);
2443 return res;
2446 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2447 static int verify_commit_graph_error;
2449 __attribute__((format (printf, 1, 2)))
2450 static void graph_report(const char *fmt, ...)
2452 va_list ap;
2454 verify_commit_graph_error = 1;
2455 va_start(ap, fmt);
2456 vfprintf(stderr, fmt, ap);
2457 fprintf(stderr, "\n");
2458 va_end(ap);
2461 #define GENERATION_ZERO_EXISTS 1
2462 #define GENERATION_NUMBER_EXISTS 2
2464 static int commit_graph_checksum_valid(struct commit_graph *g)
2466 return hashfile_checksum_valid(g->data, g->data_len);
2469 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2471 uint32_t i, cur_fanout_pos = 0;
2472 struct object_id prev_oid, cur_oid;
2473 int generation_zero = 0;
2474 struct progress *progress = NULL;
2475 int local_error = 0;
2477 if (!g) {
2478 graph_report("no commit-graph file loaded");
2479 return 1;
2482 verify_commit_graph_error = verify_commit_graph_lite(g);
2483 if (verify_commit_graph_error)
2484 return verify_commit_graph_error;
2486 if (!commit_graph_checksum_valid(g)) {
2487 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2488 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2491 for (i = 0; i < g->num_commits; i++) {
2492 struct commit *graph_commit;
2494 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2496 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2497 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2498 oid_to_hex(&prev_oid),
2499 oid_to_hex(&cur_oid));
2501 oidcpy(&prev_oid, &cur_oid);
2503 while (cur_oid.hash[0] > cur_fanout_pos) {
2504 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2506 if (i != fanout_value)
2507 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2508 cur_fanout_pos, fanout_value, i);
2509 cur_fanout_pos++;
2512 graph_commit = lookup_commit(r, &cur_oid);
2513 if (!parse_commit_in_graph_one(r, g, graph_commit))
2514 graph_report(_("failed to parse commit %s from commit-graph"),
2515 oid_to_hex(&cur_oid));
2518 while (cur_fanout_pos < 256) {
2519 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2521 if (g->num_commits != fanout_value)
2522 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2523 cur_fanout_pos, fanout_value, i);
2525 cur_fanout_pos++;
2528 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2529 return verify_commit_graph_error;
2531 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2532 progress = start_progress(_("Verifying commits in commit graph"),
2533 g->num_commits);
2535 for (i = 0; i < g->num_commits; i++) {
2536 struct commit *graph_commit, *odb_commit;
2537 struct commit_list *graph_parents, *odb_parents;
2538 timestamp_t max_generation = 0;
2539 timestamp_t generation;
2541 display_progress(progress, i + 1);
2542 oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i);
2544 graph_commit = lookup_commit(r, &cur_oid);
2545 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2546 if (parse_commit_internal(odb_commit, 0, 0)) {
2547 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2548 oid_to_hex(&cur_oid));
2549 continue;
2552 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2553 get_commit_tree_oid(odb_commit)))
2554 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2555 oid_to_hex(&cur_oid),
2556 oid_to_hex(get_commit_tree_oid(graph_commit)),
2557 oid_to_hex(get_commit_tree_oid(odb_commit)));
2559 graph_parents = graph_commit->parents;
2560 odb_parents = odb_commit->parents;
2562 while (graph_parents) {
2563 if (!odb_parents) {
2564 graph_report(_("commit-graph parent list for commit %s is too long"),
2565 oid_to_hex(&cur_oid));
2566 break;
2569 /* parse parent in case it is in a base graph */
2570 parse_commit_in_graph_one(r, g, graph_parents->item);
2572 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2573 graph_report(_("commit-graph parent for %s is %s != %s"),
2574 oid_to_hex(&cur_oid),
2575 oid_to_hex(&graph_parents->item->object.oid),
2576 oid_to_hex(&odb_parents->item->object.oid));
2578 generation = commit_graph_generation(graph_parents->item);
2579 if (generation > max_generation)
2580 max_generation = generation;
2582 graph_parents = graph_parents->next;
2583 odb_parents = odb_parents->next;
2586 if (odb_parents)
2587 graph_report(_("commit-graph parent list for commit %s terminates early"),
2588 oid_to_hex(&cur_oid));
2590 if (!commit_graph_generation(graph_commit)) {
2591 if (generation_zero == GENERATION_NUMBER_EXISTS)
2592 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2593 oid_to_hex(&cur_oid));
2594 generation_zero = GENERATION_ZERO_EXISTS;
2595 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2596 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2597 oid_to_hex(&cur_oid));
2599 if (generation_zero == GENERATION_ZERO_EXISTS)
2600 continue;
2603 * If we are using topological level and one of our parents has
2604 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2605 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2606 * in the following condition.
2608 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2609 max_generation--;
2611 generation = commit_graph_generation(graph_commit);
2612 if (generation < max_generation + 1)
2613 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2614 oid_to_hex(&cur_oid),
2615 generation,
2616 max_generation + 1);
2618 if (graph_commit->date != odb_commit->date)
2619 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2620 oid_to_hex(&cur_oid),
2621 graph_commit->date,
2622 odb_commit->date);
2624 stop_progress(&progress);
2626 local_error = verify_commit_graph_error;
2628 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2629 local_error |= verify_commit_graph(r, g->base_graph, flags);
2631 return local_error;
2634 void free_commit_graph(struct commit_graph *g)
2636 if (!g)
2637 return;
2638 if (g->data) {
2639 munmap((void *)g->data, g->data_len);
2640 g->data = NULL;
2642 free(g->filename);
2643 free(g->bloom_filter_settings);
2644 free(g);
2647 void disable_commit_graph(struct repository *r)
2649 r->commit_graph_disabled = 1;