interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / commit-graph.c

2978 lines · 80879 bytes Raw Blame History
1#define DISABLE_SIGN_COMPARE_WARNINGS
2
3#include "git-compat-util.h"
4#include "abspath.h"
5#include "config.h"
6#include "csum-file.h"
7#include "environment.h"
8#include "gettext.h"
9#include "hex.h"
10#include "lockfile.h"
11#include "packfile.h"
12#include "commit.h"
13#include "object.h"
14#include "refs.h"
15#include "hash-lookup.h"
16#include "commit-graph.h"
17#include "odb.h"
18#include "odb/source-files.h"
19#include "oid-array.h"
20#include "path.h"
21#include "alloc.h"
22#include "hashmap.h"
23#include "replace-object.h"
24#include "progress.h"
25#include "bloom.h"
26#include "commit-slab.h"
27#include "shallow.h"
28#include "json-writer.h"
29#include "trace2.h"
30#include "tree.h"
31#include "chunk-format.h"
32
33void git_test_write_commit_graph_or_die(struct repository *repo)
34{
35 int flags = 0;
36 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
37 return;
38
39 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
40 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
41
42 if (write_commit_graph_reachable(repo, repo->objects->source->path, flags, NULL))
43 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
44}
45
46#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
47#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
48#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
49#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
50#define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
51#define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
52#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
53#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
54#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
55#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
56
57#define GRAPH_VERSION_1 0x1
58#define GRAPH_VERSION GRAPH_VERSION_1
59
60#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61#define GRAPH_EDGE_LAST_MASK 0x7fffffff
62#define GRAPH_PARENT_NONE 0x70000000
63
64#define GRAPH_LAST_EDGE 0x80000000
65
66#define GRAPH_HEADER_SIZE 8
67#define GRAPH_FANOUT_SIZE (4 * 256)
68
69#define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
70
71/* Remember to update object flag allocation in object.h */
72#define REACHABLE (1u<<15)
73
74define_commit_slab(topo_level_slab, uint32_t);
75
76/* Keep track of the order in which commits are added to our list. */
77define_commit_slab(commit_pos, int);
78static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
79
80static size_t graph_data_width(const struct git_hash_algo *algop)
81{
82 return algop->rawsz + 16;
83}
84
85static size_t graph_min_size(const struct git_hash_algo *algop)
86{
87 return GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE + GRAPH_FANOUT_SIZE + algop->rawsz;
88}
89
90static void set_commit_pos(struct repository *r, const struct object_id *oid)
91{
92 static int32_t max_pos;
93 struct commit *commit = lookup_commit(r, oid);
94
95 if (!commit)
96 return; /* should never happen, but be lenient */
97
98 *commit_pos_at(&commit_pos, commit) = max_pos++;
99}
100
101static int commit_pos_cmp(const void *va, const void *vb)
102{
103 const struct commit *a = *(const struct commit **)va;
104 const struct commit *b = *(const struct commit **)vb;
105 return commit_pos_at(&commit_pos, a) -
106 commit_pos_at(&commit_pos, b);
107}
108
109define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
110static struct commit_graph_data_slab commit_graph_data_slab =
111 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
112
113static int get_configured_generation_version(struct repository *r)
114{
115 int version = 2;
116 repo_config_get_int(r, "commitgraph.generationversion", &version);
117 return version;
118}
119
120uint32_t commit_graph_position(const struct commit *c)
121{
122 struct commit_graph_data *data =
123 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
124
125 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
126}
127
128timestamp_t commit_graph_generation(const struct commit *c)
129{
130 struct commit_graph_data *data =
131 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
132
133 if (data && data->generation)
134 return data->generation;
135
136 return GENERATION_NUMBER_INFINITY;
137}
138
139static timestamp_t commit_graph_generation_from_graph(const struct commit *c)
140{
141 struct commit_graph_data *data =
142 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
143
144 if (!data || data->graph_pos == COMMIT_NOT_FROM_GRAPH)
145 return GENERATION_NUMBER_INFINITY;
146 return data->generation;
147}
148
149static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
150{
151 unsigned int i, nth_slab;
152 struct commit_graph_data *data =
153 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
154
155 if (data)
156 return data;
157
158 nth_slab = c->index / commit_graph_data_slab.slab_size;
159 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
160
161 /*
162 * commit-slab initializes elements with zero, overwrite this with
163 * COMMIT_NOT_FROM_GRAPH for graph_pos.
164 *
165 * We avoid initializing generation with checking if graph position
166 * is not COMMIT_NOT_FROM_GRAPH.
167 */
168 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
169 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
170 COMMIT_NOT_FROM_GRAPH;
171 }
172
173 return data;
174}
175
176/*
177 * Should be used only while writing commit-graph as it compares
178 * generation value of commits by directly accessing commit-slab.
179 */
180static int commit_gen_cmp(const void *va, const void *vb)
181{
182 const struct commit *a = *(const struct commit **)va;
183 const struct commit *b = *(const struct commit **)vb;
184
185 const timestamp_t generation_a = commit_graph_data_at(a)->generation;
186 const timestamp_t generation_b = commit_graph_data_at(b)->generation;
187 /* lower generation commits first */
188 if (generation_a < generation_b)
189 return -1;
190 else if (generation_a > generation_b)
191 return 1;
192
193 /* use date as a heuristic when generations are equal */
194 if (a->date < b->date)
195 return -1;
196 else if (a->date > b->date)
197 return 1;
198 return 0;
199}
200
201char *get_commit_graph_filename(const char *dir)
202{
203 return xstrfmt("%s/info/commit-graph", dir);
204}
205
206static char *get_split_graph_filename(const char *dir,
207 const char *oid_hex)
208{
209 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", dir,
210 oid_hex);
211}
212
213char *get_commit_graph_chain_filename(const char *dir)
214{
215 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", dir);
216}
217
218static struct commit_graph *alloc_commit_graph(void)
219{
220 struct commit_graph *g = xcalloc(1, sizeof(*g));
221
222 return g;
223}
224
225static int commit_graph_compatible(struct repository *r)
226{
227 if (!r->gitdir)
228 return 0;
229
230 if (replace_refs_enabled(r)) {
231 prepare_replace_object(r);
232 if (oidmap_get_size(&r->objects->replace_map))
233 return 0;
234 }
235
236 prepare_commit_graft(r);
237 if (r->parsed_objects &&
238 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
239 return 0;
240 if (is_repository_shallow(r))
241 return 0;
242
243 return 1;
244}
245
246int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
247{
248 *fd = git_open(graph_file);
249 if (*fd < 0)
250 return 0;
251 if (fstat(*fd, st)) {
252 close(*fd);
253 return 0;
254 }
255 return 1;
256}
257
258struct commit_graph *load_commit_graph_one_fd_st(struct repository *repo,
259 const char *dir,
260 int fd, struct stat *st)
261{
262 void *graph_map;
263 size_t graph_size;
264 struct commit_graph *ret;
265
266 graph_size = xsize_t(st->st_size);
267
268 if (graph_size < graph_min_size(repo->hash_algo)) {
269 close(fd);
270 error(_("commit-graph file is too small"));
271 return NULL;
272 }
273 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
274 close(fd);
275
276 ret = parse_commit_graph(repo, graph_map, graph_size);
277 if (ret)
278 ret->dir = absolute_pathdup(dir);
279 else
280 munmap(graph_map, graph_size);
281
282 return ret;
283}
284
285static int graph_read_oid_fanout(const unsigned char *chunk_start,
286 size_t chunk_size, void *data)
287{
288 struct commit_graph *g = data;
289 int i;
290
291 if (chunk_size != 256 * sizeof(uint32_t))
292 return error(_("commit-graph oid fanout chunk is wrong size"));
293 g->chunk_oid_fanout = (const uint32_t *)chunk_start;
294 g->num_commits = ntohl(g->chunk_oid_fanout[255]);
295
296 for (i = 0; i < 255; i++) {
297 uint32_t oid_fanout1 = ntohl(g->chunk_oid_fanout[i]);
298 uint32_t oid_fanout2 = ntohl(g->chunk_oid_fanout[i + 1]);
299
300 if (oid_fanout1 > oid_fanout2) {
301 error(_("commit-graph fanout values out of order"));
302 return 1;
303 }
304 }
305
306 return 0;
307}
308
309static int graph_read_oid_lookup(const unsigned char *chunk_start,
310 size_t chunk_size, void *data)
311{
312 struct commit_graph *g = data;
313 g->chunk_oid_lookup = chunk_start;
314 if (chunk_size / g->hash_algo->rawsz != g->num_commits)
315 return error(_("commit-graph OID lookup chunk is the wrong size"));
316 return 0;
317}
318
319static int graph_read_commit_data(const unsigned char *chunk_start,
320 size_t chunk_size, void *data)
321{
322 struct commit_graph *g = data;
323 if (chunk_size / graph_data_width(g->hash_algo) != g->num_commits)
324 return error(_("commit-graph commit data chunk is wrong size"));
325 g->chunk_commit_data = chunk_start;
326 return 0;
327}
328
329static int graph_read_generation_data(const unsigned char *chunk_start,
330 size_t chunk_size, void *data)
331{
332 struct commit_graph *g = data;
333 if (chunk_size / sizeof(uint32_t) != g->num_commits)
334 return error(_("commit-graph generations chunk is wrong size"));
335 g->chunk_generation_data = chunk_start;
336 return 0;
337}
338
339static int graph_read_bloom_index(const unsigned char *chunk_start,
340 size_t chunk_size, void *data)
341{
342 struct commit_graph *g = data;
343 if (chunk_size / 4 != g->num_commits) {
344 warning(_("commit-graph changed-path index chunk is too small"));
345 return -1;
346 }
347 g->chunk_bloom_indexes = chunk_start;
348 return 0;
349}
350
351static int graph_read_bloom_data(const unsigned char *chunk_start,
352 size_t chunk_size, void *data)
353{
354 struct commit_graph *g = data;
355
356 if (chunk_size < BLOOMDATA_CHUNK_HEADER_SIZE) {
357 warning(_("ignoring too-small changed-path chunk"
358 " (%"PRIuMAX" < %"PRIuMAX") in commit-graph file"),
359 (uintmax_t)chunk_size,
360 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE);
361 return -1;
362 }
363
364 g->chunk_bloom_data = chunk_start;
365 g->chunk_bloom_data_size = chunk_size;
366
367 g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
368 g->bloom_filter_settings->hash_version = get_be32(chunk_start);
369 g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4);
370 g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8);
371 g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
372
373 return 0;
374}
375
376struct commit_graph *parse_commit_graph(struct repository *r,
377 void *graph_map, size_t graph_size)
378{
379 const unsigned char *data;
380 struct commit_graph *graph;
381 uint32_t graph_signature;
382 unsigned char graph_version, hash_version;
383 struct chunkfile *cf = NULL;
384
385 if (!graph_map)
386 return NULL;
387
388 if (graph_size < graph_min_size(r->hash_algo))
389 return NULL;
390
391 data = (const unsigned char *)graph_map;
392
393 graph_signature = get_be32(data);
394 if (graph_signature != GRAPH_SIGNATURE) {
395 error(_("commit-graph signature %X does not match signature %X"),
396 graph_signature, GRAPH_SIGNATURE);
397 return NULL;
398 }
399
400 graph_version = *(unsigned char*)(data + 4);
401 if (graph_version != GRAPH_VERSION) {
402 error(_("commit-graph version %X does not match version %X"),
403 graph_version, GRAPH_VERSION);
404 return NULL;
405 }
406
407 hash_version = *(unsigned char*)(data + 5);
408 if (hash_version != oid_version(r->hash_algo)) {
409 error(_("commit-graph hash version %X does not match version %X"),
410 hash_version, oid_version(r->hash_algo));
411 return NULL;
412 }
413
414 graph = alloc_commit_graph();
415
416 graph->repo = r;
417 graph->hash_algo = r->hash_algo;
418 graph->num_chunks = *(unsigned char*)(data + 6);
419 graph->data = graph_map;
420 graph->data_len = graph_size;
421
422 if (graph_size < GRAPH_HEADER_SIZE +
423 (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE +
424 GRAPH_FANOUT_SIZE + r->hash_algo->rawsz) {
425 error(_("commit-graph file is too small to hold %u chunks"),
426 graph->num_chunks);
427 free(graph);
428 return NULL;
429 }
430
431 cf = init_chunkfile(NULL);
432
433 if (read_table_of_contents(cf, graph->data, graph_size,
434 GRAPH_HEADER_SIZE, graph->num_chunks, 1))
435 goto free_and_return;
436
437 if (read_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, graph_read_oid_fanout, graph)) {
438 error(_("commit-graph required OID fanout chunk missing or corrupted"));
439 goto free_and_return;
440 }
441 if (read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph)) {
442 error(_("commit-graph required OID lookup chunk missing or corrupted"));
443 goto free_and_return;
444 }
445 if (read_chunk(cf, GRAPH_CHUNKID_DATA, graph_read_commit_data, graph)) {
446 error(_("commit-graph required commit data chunk missing or corrupted"));
447 goto free_and_return;
448 }
449
450 pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges,
451 &graph->chunk_extra_edges_size);
452 pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs,
453 &graph->chunk_base_graphs_size);
454
455 prepare_repo_settings(r);
456
457 if (r->settings.commit_graph_generation_version >= 2) {
458 read_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
459 graph_read_generation_data, graph);
460 pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
461 &graph->chunk_generation_data_overflow,
462 &graph->chunk_generation_data_overflow_size);
463
464 if (graph->chunk_generation_data)
465 graph->read_generation_data = 1;
466 }
467
468 if (r->settings.commit_graph_changed_paths_version) {
469 read_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
470 graph_read_bloom_index, graph);
471 read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
472 graph_read_bloom_data, graph);
473 }
474
475 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
476 init_bloom_filters();
477 } else {
478 /* We need both the bloom chunks to exist together. Else ignore the data */
479 graph->chunk_bloom_indexes = NULL;
480 graph->chunk_bloom_data = NULL;
481 FREE_AND_NULL(graph->bloom_filter_settings);
482 }
483
484 oidread(&graph->oid, graph->data + graph->data_len - graph->hash_algo->rawsz,
485 r->hash_algo);
486
487 free_chunkfile(cf);
488 return graph;
489
490free_and_return:
491 free_chunkfile(cf);
492 free(graph->bloom_filter_settings);
493 free(graph);
494 return NULL;
495}
496
497static struct commit_graph *load_commit_graph_one(struct repository *repo,
498 const char *dir,
499 const char *graph_file)
500{
501 struct stat st;
502 int fd;
503 struct commit_graph *g;
504 int open_ok = open_commit_graph(graph_file, &fd, &st);
505
506 if (!open_ok)
507 return NULL;
508
509 g = load_commit_graph_one_fd_st(repo, dir, fd, &st);
510 if (g)
511 g->filename = absolute_pathdup(graph_file);
512
513 return g;
514}
515
516static struct commit_graph *load_commit_graph_v1(struct repository *repo,
517 const char *dir)
518{
519 char *graph_name = get_commit_graph_filename(dir);
520 struct commit_graph *g = load_commit_graph_one(repo, dir, graph_name);
521 free(graph_name);
522
523 return g;
524}
525
526/*
527 * returns 1 if and only if all graphs in the chain have
528 * corrected commit dates stored in the generation_data chunk.
529 */
530static int validate_mixed_generation_chain(struct commit_graph *g)
531{
532 int read_generation_data = 1;
533 struct commit_graph *p = g;
534
535 while (read_generation_data && p) {
536 read_generation_data = p->read_generation_data;
537 p = p->base_graph;
538 }
539
540 if (read_generation_data)
541 return 1;
542
543 while (g) {
544 g->read_generation_data = 0;
545 g = g->base_graph;
546 }
547
548 return 0;
549}
550
551static void validate_mixed_bloom_settings(struct commit_graph *g)
552{
553 struct bloom_filter_settings *settings = NULL;
554 for (; g; g = g->base_graph) {
555 if (!g->bloom_filter_settings)
556 continue;
557 if (!settings) {
558 settings = g->bloom_filter_settings;
559 continue;
560 }
561
562 if (g->bloom_filter_settings->bits_per_entry != settings->bits_per_entry ||
563 g->bloom_filter_settings->num_hashes != settings->num_hashes ||
564 g->bloom_filter_settings->hash_version != settings->hash_version) {
565 g->chunk_bloom_indexes = NULL;
566 g->chunk_bloom_data = NULL;
567 FREE_AND_NULL(g->bloom_filter_settings);
568
569 warning(_("disabling Bloom filters for commit-graph "
570 "layer '%s' due to incompatible settings"),
571 oid_to_hex(&g->oid));
572 }
573 }
574}
575
576static int add_graph_to_chain(struct commit_graph *g,
577 struct commit_graph *chain,
578 struct object_id *oids,
579 int n)
580{
581 struct commit_graph *cur_g = chain;
582
583 if (n && !g->chunk_base_graphs) {
584 warning(_("commit-graph has no base graphs chunk"));
585 return 0;
586 }
587
588 if (g->chunk_base_graphs_size / g->hash_algo->rawsz < n) {
589 warning(_("commit-graph base graphs chunk is too small"));
590 return 0;
591 }
592
593 while (n) {
594 n--;
595
596 if (!cur_g ||
597 !oideq(&oids[n], &cur_g->oid) ||
598 !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_algo->rawsz, n),
599 g->hash_algo)) {
600 warning(_("commit-graph chain does not match"));
601 return 0;
602 }
603
604 cur_g = cur_g->base_graph;
605 }
606
607 if (chain) {
608 if (unsigned_add_overflows(chain->num_commits,
609 chain->num_commits_in_base)) {
610 warning(_("commit count in base graph too high: %"PRIuMAX),
611 (uintmax_t)chain->num_commits_in_base);
612 return 0;
613 }
614 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
615 }
616
617 g->base_graph = chain;
618
619 return 1;
620}
621
622int open_commit_graph_chain(const char *chain_file,
623 int *fd, struct stat *st,
624 const struct git_hash_algo *hash_algo)
625{
626 *fd = git_open(chain_file);
627 if (*fd < 0)
628 return 0;
629 if (fstat(*fd, st)) {
630 close(*fd);
631 return 0;
632 }
633 if (st->st_size < hash_algo->hexsz) {
634 close(*fd);
635 if (!st->st_size) {
636 /* treat empty files the same as missing */
637 errno = ENOENT;
638 } else {
639 warning(_("commit-graph chain file too small"));
640 errno = EINVAL;
641 }
642 return 0;
643 }
644 return 1;
645}
646
647struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
648 int fd, struct stat *st,
649 int *incomplete_chain)
650{
651 struct commit_graph *graph_chain = NULL;
652 struct strbuf line = STRBUF_INIT;
653 struct object_id *oids;
654 int i = 0, valid = 1, count;
655 FILE *fp = xfdopen(fd, "r");
656
657 count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
658 CALLOC_ARRAY(oids, count);
659
660 for (i = 0; i < count; i++) {
661 struct odb_source_files *files;
662
663 if (strbuf_getline_lf(&line, fp) == EOF)
664 break;
665
666 if (get_oid_hex_algop(line.buf, &oids[i], odb->repo->hash_algo)) {
667 warning(_("invalid commit-graph chain: line '%s' not a hash"),
668 line.buf);
669 valid = 0;
670 break;
671 }
672
673 valid = 0;
674
675 files = odb_source_files_downcast(odb->source);
676 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
677 char *graph_name = get_split_graph_filename(dir->abspath, line.buf);
678 struct commit_graph *g = load_commit_graph_one(odb->repo, dir->abspath, graph_name);
679
680 free(graph_name);
681
682 if (g) {
683 if (add_graph_to_chain(g, graph_chain, oids, i)) {
684 graph_chain = g;
685 valid = 1;
686 } else {
687 free_commit_graph(g);
688 }
689
690 break;
691 }
692 }
693
694 if (!valid) {
695 warning(_("unable to find all commit-graph files"));
696 break;
697 }
698 }
699
700 validate_mixed_generation_chain(graph_chain);
701 validate_mixed_bloom_settings(graph_chain);
702
703 free(oids);
704 fclose(fp);
705 strbuf_release(&line);
706
707 *incomplete_chain = !valid;
708 return graph_chain;
709}
710
711static struct commit_graph *load_commit_graph_chain(struct repository *repo,
712 const char *dir)
713{
714 char *chain_file = get_commit_graph_chain_filename(dir);
715 struct stat st;
716 int fd;
717 struct commit_graph *g = NULL;
718
719 if (open_commit_graph_chain(chain_file, &fd, &st, repo->hash_algo)) {
720 int incomplete;
721 /* ownership of fd is taken over by load function */
722 g = load_commit_graph_chain_fd_st(repo->objects, fd, &st, &incomplete);
723 }
724
725 free(chain_file);
726 return g;
727}
728
729struct commit_graph *read_commit_graph_one(struct repository *repo,
730 const char *dir)
731{
732 struct commit_graph *g = load_commit_graph_v1(repo, dir);
733
734 if (!g)
735 g = load_commit_graph_chain(repo, dir);
736
737 return g;
738}
739
740/*
741 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
742 *
743 * On the first invocation, this function attempts to load the commit
744 * graph if the repository is configured to have one.
745 */
746struct commit_graph *prepare_commit_graph(struct repository *r)
747{
748 struct odb_source_files *files;
749
750 /*
751 * Early return if there is no object database or if the commit graph is
752 * disabled.
753 *
754 * This must come before the "already attempted?" check below, because
755 * we want to disable even an already-loaded graph file.
756 */
757 if (!r->objects || r->commit_graph_disabled)
758 return NULL;
759
760 if (r->objects->commit_graph_attempted)
761 return r->objects->commit_graph;
762 r->objects->commit_graph_attempted = 1;
763
764 prepare_repo_settings(r);
765
766 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
767 r->settings.core_commit_graph != 1)
768 /*
769 * This repository is not configured to use commit graphs, so
770 * do not load one. (But report commit_graph_attempted anyway
771 * so that commit graph loading is not attempted again for this
772 * repository.)
773 */
774 return NULL;
775
776 if (!commit_graph_compatible(r))
777 return NULL;
778
779 files = odb_source_files_downcast(r->objects->source);
780 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
781 r->objects->commit_graph = read_commit_graph_one(r, dir->abspath);
782 if (r->objects->commit_graph)
783 break;
784 }
785
786 return r->objects->commit_graph;
787}
788
789int generation_numbers_enabled(struct repository *r)
790{
791 uint32_t first_generation;
792 struct commit_graph *g;
793
794 g = prepare_commit_graph(r);
795 if (!g || !g->num_commits)
796 return 0;
797
798 first_generation = get_be32(g->chunk_commit_data +
799 g->hash_algo->rawsz + 8) >> 2;
800
801 return !!first_generation;
802}
803
804int corrected_commit_dates_enabled(struct repository *r)
805{
806 struct commit_graph *g;
807
808 g = prepare_commit_graph(r);
809 if (!g || !g->num_commits)
810 return 0;
811
812 return g->read_generation_data;
813}
814
815struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
816{
817 struct commit_graph *g;
818
819 if (!prepare_commit_graph(r))
820 return NULL;
821
822 g = r->objects->commit_graph;
823 while (g) {
824 if (g->bloom_filter_settings)
825 return g->bloom_filter_settings;
826 g = g->base_graph;
827 }
828 return NULL;
829}
830
831void close_commit_graph(struct object_database *o)
832{
833 if (!o->commit_graph)
834 return;
835
836 clear_commit_graph_data_slab(&commit_graph_data_slab);
837 deinit_bloom_filters();
838 free_commit_graph(o->commit_graph);
839 o->commit_graph = NULL;
840}
841
842static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos)
843{
844 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
845 g->chunk_oid_lookup, g->hash_algo->rawsz, pos);
846}
847
848static void load_oid_from_graph(struct commit_graph *g,
849 uint32_t pos,
850 struct object_id *oid)
851{
852 uint32_t lex_index;
853
854 while (g && pos < g->num_commits_in_base)
855 g = g->base_graph;
856
857 if (!g)
858 BUG("NULL commit-graph");
859
860 if (pos >= g->num_commits + g->num_commits_in_base)
861 die(_("invalid commit position. commit-graph is likely corrupt"));
862
863 lex_index = pos - g->num_commits_in_base;
864
865 oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, lex_index),
866 g->hash_algo);
867}
868
869static struct commit_list **insert_parent_or_die(struct commit_graph *g,
870 uint32_t pos,
871 struct commit_list **pptr)
872{
873 struct commit *c;
874 struct object_id oid;
875
876 if (pos >= g->num_commits + g->num_commits_in_base)
877 die("invalid parent position %"PRIu32, pos);
878
879 load_oid_from_graph(g, pos, &oid);
880 c = lookup_commit(g->repo, &oid);
881 if (!c)
882 die(_("could not find commit %s"), oid_to_hex(&oid));
883 commit_graph_data_at(c)->graph_pos = pos;
884 return &commit_list_insert(c, pptr)->next;
885}
886
887static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
888{
889 const unsigned char *commit_data;
890 struct commit_graph_data *graph_data;
891 uint32_t lex_index, offset_pos;
892 uint64_t date_high, date_low, offset;
893
894 while (pos < g->num_commits_in_base)
895 g = g->base_graph;
896
897 if (pos >= g->num_commits + g->num_commits_in_base)
898 die(_("invalid commit position. commit-graph is likely corrupt"));
899
900 lex_index = pos - g->num_commits_in_base;
901 commit_data = g->chunk_commit_data + st_mult(graph_data_width(g->hash_algo), lex_index);
902
903 graph_data = commit_graph_data_at(item);
904 graph_data->graph_pos = pos;
905
906 date_high = get_be32(commit_data + g->hash_algo->rawsz + 8) & 0x3;
907 date_low = get_be32(commit_data + g->hash_algo->rawsz + 12);
908 item->date = (timestamp_t)((date_high << 32) | date_low);
909
910 if (g->read_generation_data) {
911 offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index));
912
913 if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) {
914 if (!g->chunk_generation_data_overflow)
915 die(_("commit-graph requires overflow generation data but has none"));
916
917 offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW;
918 if (g->chunk_generation_data_overflow_size / sizeof(uint64_t) <= offset_pos)
919 die(_("commit-graph overflow generation data is too small"));
920 graph_data->generation = item->date +
921 get_be64(g->chunk_generation_data_overflow + sizeof(uint64_t) * offset_pos);
922 } else
923 graph_data->generation = item->date + offset;
924 } else
925 graph_data->generation = get_be32(commit_data + g->hash_algo->rawsz + 8) >> 2;
926
927 if (g->topo_levels)
928 *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_algo->rawsz + 8) >> 2;
929}
930
931static inline void set_commit_tree(struct commit *c, struct tree *t)
932{
933 c->maybe_tree = t;
934}
935
936static int fill_commit_in_graph(struct commit *item,
937 struct commit_graph *g, uint32_t pos)
938{
939 uint32_t edge_value;
940 uint32_t parent_data_pos;
941 struct commit_list **pptr;
942 const unsigned char *commit_data;
943 uint32_t lex_index;
944
945 while (pos < g->num_commits_in_base)
946 g = g->base_graph;
947
948 fill_commit_graph_info(item, g, pos);
949
950 lex_index = pos - g->num_commits_in_base;
951 commit_data = g->chunk_commit_data + st_mult(g->hash_algo->rawsz + 16, lex_index);
952
953 item->object.parsed = 1;
954
955 set_commit_tree(item, NULL);
956
957 pptr = &item->parents;
958
959 edge_value = get_be32(commit_data + g->hash_algo->rawsz);
960 if (edge_value == GRAPH_PARENT_NONE)
961 return 1;
962 pptr = insert_parent_or_die(g, edge_value, pptr);
963
964 edge_value = get_be32(commit_data + g->hash_algo->rawsz + 4);
965 if (edge_value == GRAPH_PARENT_NONE)
966 return 1;
967 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
968 pptr = insert_parent_or_die(g, edge_value, pptr);
969 return 1;
970 }
971
972 parent_data_pos = edge_value & GRAPH_EDGE_LAST_MASK;
973 do {
974 if (g->chunk_extra_edges_size / sizeof(uint32_t) <= parent_data_pos) {
975 error(_("commit-graph extra-edges pointer out of bounds"));
976 commit_list_free(item->parents);
977 item->parents = NULL;
978 item->object.parsed = 0;
979 return 0;
980 }
981 edge_value = get_be32(g->chunk_extra_edges +
982 sizeof(uint32_t) * parent_data_pos);
983 pptr = insert_parent_or_die(g,
984 edge_value & GRAPH_EDGE_LAST_MASK,
985 pptr);
986 parent_data_pos++;
987 } while (!(edge_value & GRAPH_LAST_EDGE));
988
989 return 1;
990}
991
992static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos)
993{
994 struct commit_graph *cur_g = g;
995 uint32_t lex_index;
996
997 while (cur_g && !bsearch_graph(cur_g, id, &lex_index))
998 cur_g = cur_g->base_graph;
999
1000 if (cur_g) {
1001 *pos = lex_index + cur_g->num_commits_in_base;
1002 return 1;
1003 }
1004
1005 return 0;
1006}
1007
1008static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
1009{
1010 uint32_t graph_pos = commit_graph_position(item);
1011 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
1012 *pos = graph_pos;
1013 return 1;
1014 } else {
1015 return search_commit_pos_in_graph(&item->object.oid, g, pos);
1016 }
1017}
1018
1019struct commit_graph *repo_find_commit_pos_in_graph(struct repository *r,
1020 struct commit *c,
1021 uint32_t *pos)
1022{
1023 struct commit_graph *g = prepare_commit_graph(r);
1024 if (!g)
1025 return NULL;
1026 if (!find_commit_pos_in_graph(c, g, pos))
1027 return NULL;
1028 return g;
1029}
1030
1031struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id)
1032{
1033 static int commit_graph_paranoia = -1;
1034 struct commit_graph *g;
1035 struct commit *commit;
1036 uint32_t pos;
1037
1038 if (commit_graph_paranoia == -1)
1039 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
1040
1041 g = prepare_commit_graph(repo);
1042 if (!g)
1043 return NULL;
1044 if (!search_commit_pos_in_graph(id, g, &pos))
1045 return NULL;
1046 if (commit_graph_paranoia && !odb_has_object(repo->objects, id, 0))
1047 return NULL;
1048
1049 commit = lookup_commit(repo, id);
1050 if (!commit)
1051 return NULL;
1052 if (commit->object.parsed)
1053 return commit;
1054
1055 if (!fill_commit_in_graph(commit, g, pos))
1056 return NULL;
1057
1058 return commit;
1059}
1060
1061static int parse_commit_in_graph_one(struct commit_graph *g,
1062 struct commit *item)
1063{
1064 uint32_t pos;
1065
1066 if (item->object.parsed)
1067 return 1;
1068
1069 if (find_commit_pos_in_graph(item, g, &pos))
1070 return fill_commit_in_graph(item, g, pos);
1071
1072 return 0;
1073}
1074
1075int parse_commit_in_graph(struct repository *r, struct commit *item)
1076{
1077 static int checked_env = 0;
1078 struct commit_graph *g;
1079
1080 if (!checked_env &&
1081 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
1082 die("dying as requested by the '%s' variable on commit-graph parse!",
1083 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
1084 checked_env = 1;
1085
1086 g = prepare_commit_graph(r);
1087 if (!g)
1088 return 0;
1089 return parse_commit_in_graph_one(g, item);
1090}
1091
1092void load_commit_graph_info(struct repository *r, struct commit *item)
1093{
1094 struct commit_graph *g;
1095 uint32_t pos;
1096
1097 g = repo_find_commit_pos_in_graph(r, item, &pos);
1098 if (g)
1099 fill_commit_graph_info(item, g, pos);
1100}
1101
1102static struct tree *load_tree_for_commit(struct commit_graph *g,
1103 struct commit *c)
1104{
1105 struct object_id oid;
1106 const unsigned char *commit_data;
1107 uint32_t graph_pos = commit_graph_position(c);
1108
1109 while (graph_pos < g->num_commits_in_base)
1110 g = g->base_graph;
1111
1112 commit_data = g->chunk_commit_data +
1113 st_mult(graph_data_width(g->hash_algo),
1114 graph_pos - g->num_commits_in_base);
1115
1116 oidread(&oid, commit_data, g->hash_algo);
1117 set_commit_tree(c, lookup_tree(g->repo, &oid));
1118
1119 return c->maybe_tree;
1120}
1121
1122static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
1123 const struct commit *c)
1124{
1125 if (c->maybe_tree)
1126 return c->maybe_tree;
1127 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
1128 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1129
1130 return load_tree_for_commit(g, (struct commit *)c);
1131}
1132
1133struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
1134{
1135 return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
1136}
1137
1138struct write_commit_graph_context {
1139 struct repository *r;
1140 char *dir;
1141 char *graph_name;
1142 struct oid_array oids;
1143 struct commit_stack commits;
1144 int num_extra_edges;
1145 int num_generation_data_overflows;
1146 unsigned long approx_nr_objects;
1147 struct progress *progress;
1148 int progress_done;
1149 uint64_t progress_cnt;
1150
1151 char *base_graph_name;
1152 int num_commit_graphs_before;
1153 int num_commit_graphs_after;
1154 char **commit_graph_filenames_before;
1155 char **commit_graph_filenames_after;
1156 char **commit_graph_hash_after;
1157 uint32_t new_num_commits_in_base;
1158 struct commit_graph *new_base_graph;
1159
1160 unsigned append:1,
1161 report_progress:1,
1162 split:1,
1163 changed_paths:1,
1164 order_by_pack:1,
1165 write_generation_data:1,
1166 trust_generation_numbers:1;
1167
1168 struct topo_level_slab *topo_levels;
1169 const struct commit_graph_opts *opts;
1170 size_t total_bloom_filter_data_size;
1171 const struct bloom_filter_settings *bloom_settings;
1172
1173 int count_bloom_filter_computed;
1174 int count_bloom_filter_not_computed;
1175 int count_bloom_filter_trunc_empty;
1176 int count_bloom_filter_trunc_large;
1177 int count_bloom_filter_upgraded;
1178};
1179
1180static int write_graph_chunk_fanout(struct hashfile *f,
1181 void *data)
1182{
1183 struct write_commit_graph_context *ctx = data;
1184 int i, count = 0;
1185 struct commit **list = ctx->commits.items;
1186
1187 /*
1188 * Write the first-level table (the list is sorted,
1189 * but we use a 256-entry lookup to be able to avoid
1190 * having to do eight extra binary search iterations).
1191 */
1192 for (i = 0; i < 256; i++) {
1193 while (count < ctx->commits.nr) {
1194 if ((*list)->object.oid.hash[0] != i)
1195 break;
1196 display_progress(ctx->progress, ++ctx->progress_cnt);
1197 count++;
1198 list++;
1199 }
1200
1201 hashwrite_be32(f, count);
1202 }
1203
1204 return 0;
1205}
1206
1207static int write_graph_chunk_oids(struct hashfile *f,
1208 void *data)
1209{
1210 struct write_commit_graph_context *ctx = data;
1211 struct commit **list = ctx->commits.items;
1212 int count;
1213 for (count = 0; count < ctx->commits.nr; count++, list++) {
1214 display_progress(ctx->progress, ++ctx->progress_cnt);
1215 hashwrite(f, (*list)->object.oid.hash, f->algop->rawsz);
1216 }
1217
1218 return 0;
1219}
1220
1221static const struct object_id *commit_to_oid(size_t index, const void *table)
1222{
1223 const struct commit * const *commits = table;
1224 return &commits[index]->object.oid;
1225}
1226
1227static int write_graph_chunk_data(struct hashfile *f,
1228 void *data)
1229{
1230 struct write_commit_graph_context *ctx = data;
1231 struct commit **list = ctx->commits.items;
1232 struct commit **last = ctx->commits.items + ctx->commits.nr;
1233 uint32_t num_extra_edges = 0;
1234
1235 while (list < last) {
1236 struct commit_list *parent;
1237 struct object_id *tree;
1238 int edge_value;
1239 uint32_t packedDate[2];
1240 display_progress(ctx->progress, ++ctx->progress_cnt);
1241
1242 if (repo_parse_commit_no_graph(ctx->r, *list))
1243 die(_("unable to parse commit %s"),
1244 oid_to_hex(&(*list)->object.oid));
1245 tree = get_commit_tree_oid(*list);
1246 hashwrite(f, tree->hash, ctx->r->hash_algo->rawsz);
1247
1248 parent = (*list)->parents;
1249
1250 if (!parent)
1251 edge_value = GRAPH_PARENT_NONE;
1252 else {
1253 edge_value = oid_pos(&parent->item->object.oid,
1254 ctx->commits.items,
1255 ctx->commits.nr,
1256 commit_to_oid);
1257
1258 if (edge_value >= 0)
1259 edge_value += ctx->new_num_commits_in_base;
1260 else if (ctx->new_base_graph) {
1261 uint32_t pos;
1262 if (find_commit_pos_in_graph(parent->item,
1263 ctx->new_base_graph,
1264 &pos))
1265 edge_value = pos;
1266 }
1267
1268 if (edge_value < 0)
1269 BUG("missing parent %s for commit %s",
1270 oid_to_hex(&parent->item->object.oid),
1271 oid_to_hex(&(*list)->object.oid));
1272 }
1273
1274 hashwrite_be32(f, edge_value);
1275
1276 if (parent)
1277 parent = parent->next;
1278
1279 if (!parent)
1280 edge_value = GRAPH_PARENT_NONE;
1281 else if (parent->next)
1282 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
1283 else {
1284 edge_value = oid_pos(&parent->item->object.oid,
1285 ctx->commits.items,
1286 ctx->commits.nr,
1287 commit_to_oid);
1288
1289 if (edge_value >= 0)
1290 edge_value += ctx->new_num_commits_in_base;
1291 else if (ctx->new_base_graph) {
1292 uint32_t pos;
1293 if (find_commit_pos_in_graph(parent->item,
1294 ctx->new_base_graph,
1295 &pos))
1296 edge_value = pos;
1297 }
1298
1299 if (edge_value < 0)
1300 BUG("missing parent %s for commit %s",
1301 oid_to_hex(&parent->item->object.oid),
1302 oid_to_hex(&(*list)->object.oid));
1303 }
1304
1305 hashwrite_be32(f, edge_value);
1306
1307 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
1308 do {
1309 num_extra_edges++;
1310 parent = parent->next;
1311 } while (parent);
1312 }
1313
1314 if (sizeof((*list)->date) > 4)
1315 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1316 else
1317 packedDate[0] = 0;
1318
1319 packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2);
1320
1321 packedDate[1] = htonl((*list)->date);
1322 hashwrite(f, packedDate, 8);
1323
1324 list++;
1325 }
1326
1327 return 0;
1328}
1329
1330/*
1331 * Compute the generation offset between the commit date and its generation.
1332 * This is what's ultimately stored as generation number in the commit graph.
1333 *
1334 * Note that the computation of the commit date is more involved than you might
1335 * think. Instead of using the full commit date, we're in fact masking bits so
1336 * that only the 34 lowest bits are considered. This results from the fact that
1337 * commit graphs themselves only ever store 34 bits of the commit date
1338 * themselves.
1339 *
1340 * This means that if we have a commit date that exceeds 34 bits we'll end up
1341 * in situations where depending on whether the commit has been parsed from the
1342 * object database or the commit graph we'll have different dates, where the
1343 * ones parsed from the object database would have full 64 bit precision.
1344 *
1345 * But ultimately, we only ever want the offset to be relative to what we
1346 * actually end up storing on disk, and hence we have to mask all the other
1347 * bits.
1348 */
1349static timestamp_t compute_generation_offset(struct commit *c)
1350{
1351 timestamp_t masked_date;
1352
1353 if (sizeof(timestamp_t) > 4)
1354 masked_date = c->date & (((timestamp_t) 1 << 34) - 1);
1355 else
1356 masked_date = c->date;
1357
1358 return commit_graph_data_at(c)->generation - masked_date;
1359}
1360
1361static int write_graph_chunk_generation_data(struct hashfile *f,
1362 void *data)
1363{
1364 struct write_commit_graph_context *ctx = data;
1365 int i, num_generation_data_overflows = 0;
1366
1367 for (i = 0; i < ctx->commits.nr; i++) {
1368 struct commit *c = ctx->commits.items[i];
1369 timestamp_t offset;
1370 repo_parse_commit(ctx->r, c);
1371 offset = compute_generation_offset(c);
1372 display_progress(ctx->progress, ++ctx->progress_cnt);
1373
1374 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1375 offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows;
1376 num_generation_data_overflows++;
1377 }
1378
1379 hashwrite_be32(f, offset);
1380 }
1381
1382 return 0;
1383}
1384
1385static int write_graph_chunk_generation_data_overflow(struct hashfile *f,
1386 void *data)
1387{
1388 struct write_commit_graph_context *ctx = data;
1389 int i;
1390 for (i = 0; i < ctx->commits.nr; i++) {
1391 struct commit *c = ctx->commits.items[i];
1392 timestamp_t offset = compute_generation_offset(c);
1393 display_progress(ctx->progress, ++ctx->progress_cnt);
1394
1395 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) {
1396 hashwrite_be32(f, offset >> 32);
1397 hashwrite_be32(f, (uint32_t) offset);
1398 }
1399 }
1400
1401 return 0;
1402}
1403
1404static int write_graph_chunk_extra_edges(struct hashfile *f,
1405 void *data)
1406{
1407 struct write_commit_graph_context *ctx = data;
1408 struct commit **list = ctx->commits.items;
1409 struct commit **last = ctx->commits.items + ctx->commits.nr;
1410 struct commit_list *parent;
1411
1412 while (list < last) {
1413 int num_parents = 0;
1414
1415 display_progress(ctx->progress, ++ctx->progress_cnt);
1416
1417 for (parent = (*list)->parents; num_parents < 3 && parent;
1418 parent = parent->next)
1419 num_parents++;
1420
1421 if (num_parents <= 2) {
1422 list++;
1423 continue;
1424 }
1425
1426 /* Since num_parents > 2, this initializer is safe. */
1427 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1428 int edge_value = oid_pos(&parent->item->object.oid,
1429 ctx->commits.items,
1430 ctx->commits.nr,
1431 commit_to_oid);
1432
1433 if (edge_value >= 0)
1434 edge_value += ctx->new_num_commits_in_base;
1435 else if (ctx->new_base_graph) {
1436 uint32_t pos;
1437 if (find_commit_pos_in_graph(parent->item,
1438 ctx->new_base_graph,
1439 &pos))
1440 edge_value = pos;
1441 }
1442
1443 if (edge_value < 0)
1444 BUG("missing parent %s for commit %s",
1445 oid_to_hex(&parent->item->object.oid),
1446 oid_to_hex(&(*list)->object.oid));
1447 else if (!parent->next)
1448 edge_value |= GRAPH_LAST_EDGE;
1449
1450 hashwrite_be32(f, edge_value);
1451 }
1452
1453 list++;
1454 }
1455
1456 return 0;
1457}
1458
1459static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1460 void *data)
1461{
1462 struct write_commit_graph_context *ctx = data;
1463 struct commit **list = ctx->commits.items;
1464 struct commit **last = ctx->commits.items + ctx->commits.nr;
1465 uint32_t cur_pos = 0;
1466
1467 while (list < last) {
1468 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1469 size_t len = filter ? filter->len : 0;
1470 cur_pos += len;
1471 display_progress(ctx->progress, ++ctx->progress_cnt);
1472 hashwrite_be32(f, cur_pos);
1473 list++;
1474 }
1475
1476 return 0;
1477}
1478
1479static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1480{
1481 struct json_writer jw = JSON_WRITER_INIT;
1482
1483 jw_object_begin(&jw, 0);
1484 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1485 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1486 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
1487 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
1488 jw_end(&jw);
1489
1490 trace2_data_json("bloom", ctx->r, "settings", &jw);
1491
1492 jw_release(&jw);
1493}
1494
1495static int write_graph_chunk_bloom_data(struct hashfile *f,
1496 void *data)
1497{
1498 struct write_commit_graph_context *ctx = data;
1499 struct commit **list = ctx->commits.items;
1500 struct commit **last = ctx->commits.items + ctx->commits.nr;
1501
1502 trace2_bloom_filter_settings(ctx);
1503
1504 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1505 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1506 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
1507
1508 while (list < last) {
1509 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
1510 size_t len = filter ? filter->len : 0;
1511
1512 display_progress(ctx->progress, ++ctx->progress_cnt);
1513 if (len)
1514 hashwrite(f, filter->data, len * sizeof(unsigned char));
1515 list++;
1516 }
1517
1518 return 0;
1519}
1520
1521static int add_packed_commits_oi(const struct object_id *oid,
1522 struct object_info *oi,
1523 void *data)
1524{
1525 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1526
1527 if (ctx->progress)
1528 display_progress(ctx->progress, ++ctx->progress_done);
1529
1530 if (*oi->typep != OBJ_COMMIT)
1531 return 0;
1532
1533 oid_array_append(&ctx->oids, oid);
1534 set_commit_pos(ctx->r, oid);
1535
1536 return 0;
1537}
1538
1539static int add_packed_commits(const struct object_id *oid,
1540 struct packed_git *pack,
1541 uint32_t pos,
1542 void *data)
1543{
1544 enum object_type type;
1545 off_t offset = nth_packed_object_offset(pack, pos);
1546 struct object_info oi = OBJECT_INFO_INIT;
1547
1548 oi.typep = &type;
1549 if (packed_object_info(NULL, pack, offset, &oi) < 0)
1550 die(_("unable to get type of object %s"), oid_to_hex(oid));
1551
1552 return add_packed_commits_oi(oid, &oi, data);
1553}
1554
1555static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1556{
1557 struct commit_list *parent;
1558 for (parent = commit->parents; parent; parent = parent->next) {
1559 if (!(parent->item->object.flags & REACHABLE)) {
1560 oid_array_append(&ctx->oids, &parent->item->object.oid);
1561 parent->item->object.flags |= REACHABLE;
1562 }
1563 }
1564}
1565
1566static void close_reachable(struct write_commit_graph_context *ctx)
1567{
1568 int i;
1569 struct commit *commit;
1570 enum commit_graph_split_flags flags = ctx->opts ?
1571 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
1572
1573 if (ctx->report_progress)
1574 ctx->progress = start_delayed_progress(
1575 ctx->r,
1576 _("Loading known commits in commit graph"),
1577 ctx->oids.nr);
1578 for (i = 0; i < ctx->oids.nr; i++) {
1579 display_progress(ctx->progress, i + 1);
1580 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1581 if (commit)
1582 commit->object.flags |= REACHABLE;
1583 }
1584 stop_progress(&ctx->progress);
1585
1586 /*
1587 * As this loop runs, ctx->oids.nr may grow, but not more
1588 * than the number of missing commits in the reachable
1589 * closure.
1590 */
1591 if (ctx->report_progress)
1592 ctx->progress = start_delayed_progress(
1593 ctx->r,
1594 _("Expanding reachable commits in commit graph"),
1595 0);
1596 for (i = 0; i < ctx->oids.nr; i++) {
1597 display_progress(ctx->progress, i + 1);
1598 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1599
1600 if (!commit)
1601 continue;
1602 if (ctx->split) {
1603 if ((!repo_parse_commit(ctx->r, commit) &&
1604 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
1605 flags == COMMIT_GRAPH_SPLIT_REPLACE)
1606 add_missing_parents(ctx, commit);
1607 } else if (!repo_parse_commit_no_graph(ctx->r, commit))
1608 add_missing_parents(ctx, commit);
1609 }
1610 stop_progress(&ctx->progress);
1611
1612 if (ctx->report_progress)
1613 ctx->progress = start_delayed_progress(
1614 ctx->r,
1615 _("Clearing commit marks in commit graph"),
1616 ctx->oids.nr);
1617 for (i = 0; i < ctx->oids.nr; i++) {
1618 display_progress(ctx->progress, i + 1);
1619 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
1620
1621 if (commit)
1622 commit->object.flags &= ~REACHABLE;
1623 }
1624 stop_progress(&ctx->progress);
1625}
1626
1627struct compute_generation_info {
1628 struct repository *r;
1629 struct commit_stack *commits;
1630 struct progress *progress;
1631 int progress_cnt;
1632
1633 timestamp_t (*get_generation)(struct commit *c, void *data);
1634 void (*set_generation)(struct commit *c, timestamp_t gen, void *data);
1635 void *data;
1636};
1637
1638static timestamp_t compute_generation_from_max(struct commit *c,
1639 timestamp_t max_gen,
1640 int generation_version)
1641{
1642 switch (generation_version) {
1643 case 1: /* topological levels */
1644 if (max_gen > GENERATION_NUMBER_V1_MAX - 1)
1645 max_gen = GENERATION_NUMBER_V1_MAX - 1;
1646 return max_gen + 1;
1647
1648 case 2: /* corrected commit date */
1649 if (c->date && c->date > max_gen)
1650 max_gen = c->date - 1;
1651 return max_gen + 1;
1652
1653 default:
1654 BUG("attempting unimplemented version");
1655 }
1656}
1657
1658static void compute_reachable_generation_numbers(
1659 struct compute_generation_info *info,
1660 int generation_version)
1661{
1662 int i;
1663 struct commit_list *list = NULL;
1664 intmax_t steps = 0;
1665
1666 for (i = 0; i < info->commits->nr; i++) {
1667 struct commit *c = info->commits->items[i];
1668 timestamp_t gen;
1669 repo_parse_commit(info->r, c);
1670 gen = info->get_generation(c, info->data);
1671 display_progress(info->progress, ++info->progress_cnt);
1672
1673 if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY)
1674 continue;
1675
1676 commit_list_insert(c, &list);
1677 while (list) {
1678 struct commit *current = list->item;
1679 struct commit_list *parent;
1680 int all_parents_computed = 1;
1681 timestamp_t max_gen = 0;
1682
1683 steps++;
1684 for (parent = current->parents; parent; parent = parent->next) {
1685 repo_parse_commit(info->r, parent->item);
1686 gen = info->get_generation(parent->item, info->data);
1687
1688 if (gen == GENERATION_NUMBER_ZERO) {
1689 all_parents_computed = 0;
1690 commit_list_insert(parent->item, &list);
1691 break;
1692 }
1693
1694 if (gen > max_gen)
1695 max_gen = gen;
1696 }
1697
1698 if (all_parents_computed) {
1699 pop_commit(&list);
1700 gen = compute_generation_from_max(
1701 current, max_gen,
1702 generation_version);
1703 info->set_generation(current, gen, info->data);
1704 }
1705 }
1706 }
1707
1708 trace2_data_intmax("commit-graph", info->r,
1709 "generation-dfs-steps", steps);
1710}
1711
1712static timestamp_t get_topo_level(struct commit *c, void *data)
1713{
1714 struct write_commit_graph_context *ctx = data;
1715 return *topo_level_slab_at(ctx->topo_levels, c);
1716}
1717
1718static void set_topo_level(struct commit *c, timestamp_t t, void *data)
1719{
1720 struct write_commit_graph_context *ctx = data;
1721 *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t;
1722}
1723
1724static void compute_topological_levels(struct write_commit_graph_context *ctx)
1725{
1726 struct compute_generation_info info = {
1727 .r = ctx->r,
1728 .commits = &ctx->commits,
1729 .get_generation = get_topo_level,
1730 .set_generation = set_topo_level,
1731 .data = ctx,
1732 };
1733
1734 if (ctx->report_progress)
1735 info.progress = ctx->progress
1736 = start_delayed_progress(
1737 ctx->r,
1738 _("Computing commit graph topological levels"),
1739 ctx->commits.nr);
1740
1741 compute_reachable_generation_numbers(&info, 1);
1742
1743 stop_progress(&ctx->progress);
1744}
1745
1746static timestamp_t get_generation_from_graph_data(struct commit *c,
1747 void *data UNUSED)
1748{
1749 return commit_graph_data_at(c)->generation;
1750}
1751
1752static void set_generation_v2(struct commit *c, timestamp_t t,
1753 void *data UNUSED)
1754{
1755 struct commit_graph_data *g = commit_graph_data_at(c);
1756 g->generation = t;
1757}
1758
1759static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1760{
1761 int i;
1762 struct compute_generation_info info = {
1763 .r = ctx->r,
1764 .commits = &ctx->commits,
1765 .get_generation = get_generation_from_graph_data,
1766 .set_generation = set_generation_v2,
1767 };
1768
1769 if (ctx->report_progress)
1770 info.progress = ctx->progress
1771 = start_delayed_progress(
1772 ctx->r,
1773 _("Computing commit graph generation numbers"),
1774 ctx->commits.nr);
1775
1776 if (!ctx->trust_generation_numbers) {
1777 for (i = 0; i < ctx->commits.nr; i++) {
1778 struct commit *c = ctx->commits.items[i];
1779 repo_parse_commit(ctx->r, c);
1780 commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO;
1781 }
1782 }
1783
1784 compute_reachable_generation_numbers(&info, 2);
1785
1786 for (i = 0; i < ctx->commits.nr; i++) {
1787 struct commit *c = ctx->commits.items[i];
1788 timestamp_t offset = compute_generation_offset(c);
1789 if (offset > GENERATION_NUMBER_V2_OFFSET_MAX)
1790 ctx->num_generation_data_overflows++;
1791 }
1792 stop_progress(&ctx->progress);
1793}
1794
1795static void set_generation_in_graph_data(struct commit *c, timestamp_t t,
1796 void *data UNUSED)
1797{
1798 commit_graph_data_at(c)->generation = t;
1799}
1800
1801/*
1802 * After this method, all commits reachable from those in the given
1803 * list will have non-zero, non-infinite generation numbers.
1804 */
1805void ensure_generations_valid(struct repository *r,
1806 struct commit **commits, size_t nr)
1807{
1808 int generation_version = get_configured_generation_version(r);
1809 struct commit_stack list = {
1810 .items = commits,
1811 .alloc = nr,
1812 .nr = nr,
1813 };
1814 struct compute_generation_info info = {
1815 .r = r,
1816 .commits = &list,
1817 .get_generation = get_generation_from_graph_data,
1818 .set_generation = set_generation_in_graph_data,
1819 };
1820
1821 compute_reachable_generation_numbers(&info, generation_version);
1822}
1823
1824static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1825{
1826 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1827 ctx->count_bloom_filter_computed);
1828 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1829 ctx->count_bloom_filter_not_computed);
1830 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1831 ctx->count_bloom_filter_trunc_empty);
1832 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1833 ctx->count_bloom_filter_trunc_large);
1834 trace2_data_intmax("commit-graph", ctx->r, "filter-upgraded",
1835 ctx->count_bloom_filter_upgraded);
1836}
1837
1838static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1839{
1840 int i;
1841 struct progress *progress = NULL;
1842 struct commit **sorted_commits;
1843 int max_new_filters;
1844
1845 init_bloom_filters();
1846
1847 if (ctx->report_progress)
1848 progress = start_delayed_progress(
1849 ctx->r,
1850 _("Computing commit changed paths Bloom filters"),
1851 ctx->commits.nr);
1852
1853 DUP_ARRAY(sorted_commits, ctx->commits.items, ctx->commits.nr);
1854
1855 if (ctx->order_by_pack)
1856 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1857 else
1858 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1859
1860 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1861 ctx->opts->max_new_filters : ctx->commits.nr;
1862
1863 for (i = 0; i < ctx->commits.nr; i++) {
1864 enum bloom_filter_computed computed = 0;
1865 struct commit *c = sorted_commits[i];
1866 struct bloom_filter *filter = get_or_compute_bloom_filter(
1867 ctx->r,
1868 c,
1869 ctx->count_bloom_filter_computed < max_new_filters,
1870 ctx->bloom_settings,
1871 &computed);
1872 if (computed & BLOOM_COMPUTED) {
1873 ctx->count_bloom_filter_computed++;
1874 if (computed & BLOOM_TRUNC_EMPTY)
1875 ctx->count_bloom_filter_trunc_empty++;
1876 if (computed & BLOOM_TRUNC_LARGE)
1877 ctx->count_bloom_filter_trunc_large++;
1878 } else if (computed & BLOOM_UPGRADED) {
1879 ctx->count_bloom_filter_upgraded++;
1880 } else if (computed & BLOOM_NOT_COMPUTED)
1881 ctx->count_bloom_filter_not_computed++;
1882 ctx->total_bloom_filter_data_size += filter
1883 ? sizeof(unsigned char) * filter->len : 0;
1884 display_progress(progress, i + 1);
1885 }
1886
1887 if (trace2_is_enabled())
1888 trace2_bloom_filter_write_statistics(ctx);
1889
1890 free(sorted_commits);
1891 stop_progress(&progress);
1892}
1893
1894struct refs_cb_data {
1895 struct repository *repo;
1896 struct oidset *commits;
1897 struct progress *progress;
1898};
1899
1900static int add_ref_to_set(const struct reference *ref, void *cb_data)
1901{
1902 const struct object_id *maybe_peeled = ref->oid;
1903 struct object_id peeled;
1904 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
1905
1906 if (!reference_get_peeled_oid(data->repo, ref, &peeled))
1907 maybe_peeled = &peeled;
1908 if (odb_read_object_info(data->repo->objects, maybe_peeled, NULL) == OBJ_COMMIT)
1909 oidset_insert(data->commits, maybe_peeled);
1910
1911 display_progress(data->progress, oidset_size(data->commits));
1912
1913 return 0;
1914}
1915
1916int write_commit_graph_reachable(struct repository *repo,
1917 const char *dir,
1918 enum commit_graph_write_flags flags,
1919 const struct commit_graph_opts *opts)
1920{
1921 struct oidset commits = OIDSET_INIT;
1922 struct refs_cb_data data;
1923 int result;
1924
1925 memset(&data, 0, sizeof(data));
1926 data.repo = repo;
1927 data.commits = &commits;
1928
1929 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1930 data.progress = start_delayed_progress(
1931 repo,
1932 _("Collecting referenced commits"), 0);
1933
1934 refs_for_each_ref(get_main_ref_store(repo), add_ref_to_set,
1935 &data);
1936
1937 stop_progress(&data.progress);
1938
1939 result = write_commit_graph(repo, dir, NULL, &commits,
1940 flags, opts);
1941
1942 oidset_clear(&commits);
1943 return result;
1944}
1945
1946static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1947 const struct string_list *pack_indexes)
1948{
1949 uint32_t i;
1950 struct strbuf progress_title = STRBUF_INIT;
1951 int ret = 0;
1952
1953 if (ctx->report_progress) {
1954 strbuf_addf(&progress_title,
1955 Q_("Finding commits for commit graph in %"PRIuMAX" pack",
1956 "Finding commits for commit graph in %"PRIuMAX" packs",
1957 pack_indexes->nr),
1958 (uintmax_t)pack_indexes->nr);
1959 ctx->progress = start_delayed_progress(ctx->r,
1960 progress_title.buf, 0);
1961 ctx->progress_done = 0;
1962 }
1963 for (i = 0; i < pack_indexes->nr; i++) {
1964 struct packed_git *p;
1965
1966 p = add_packed_git(ctx->r, pack_indexes->items[i].string,
1967 strlen(pack_indexes->items[i].string), 1);
1968 if (!p) {
1969 ret = error(_("error adding pack %s"), pack_indexes->items[i].string);
1970 goto cleanup;
1971 }
1972 if (open_pack_index(p)) {
1973 ret = error(_("error opening index for %s"), pack_indexes->items[i].string);
1974 close_pack(p);
1975 free(p);
1976 goto cleanup;
1977 }
1978 for_each_object_in_pack(p, add_packed_commits, ctx,
1979 ODB_FOR_EACH_OBJECT_PACK_ORDER);
1980 close_pack(p);
1981 free(p);
1982 }
1983
1984cleanup:
1985 stop_progress(&ctx->progress);
1986 strbuf_release(&progress_title);
1987
1988 return ret;
1989}
1990
1991static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1992 struct oidset *commits)
1993{
1994 struct oidset_iter iter;
1995 struct object_id *oid;
1996
1997 if (!oidset_size(commits))
1998 return 0;
1999
2000 oidset_iter_init(commits, &iter);
2001 while ((oid = oidset_iter_next(&iter))) {
2002 oid_array_append(&ctx->oids, oid);
2003 }
2004
2005 return 0;
2006}
2007
2008static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
2009{
2010 struct odb_source_files *files;
2011 enum object_type type;
2012 struct odb_for_each_object_options opts = {
2013 .flags = ODB_FOR_EACH_OBJECT_PACK_ORDER,
2014 };
2015 struct object_info oi = {
2016 .typep = &type,
2017 };
2018
2019 if (ctx->report_progress)
2020 ctx->progress = start_delayed_progress(
2021 ctx->r,
2022 _("Finding commits for commit graph among packed objects"),
2023 ctx->approx_nr_objects);
2024
2025 files = odb_source_files_downcast(ctx->r->objects->source);
2026 for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) {
2027 odb_source_for_each_object(&dir->packed->base, &oi, add_packed_commits_oi,
2028 ctx, &opts);
2029 }
2030
2031 if (ctx->progress_done < ctx->approx_nr_objects)
2032 display_progress(ctx->progress, ctx->approx_nr_objects);
2033 stop_progress(&ctx->progress);
2034}
2035
2036static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
2037{
2038 uint32_t i;
2039 enum commit_graph_split_flags flags = ctx->opts ?
2040 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2041
2042 ctx->num_extra_edges = 0;
2043 if (ctx->report_progress)
2044 ctx->progress = start_delayed_progress(
2045 ctx->r,
2046 _("Finding extra edges in commit graph"),
2047 ctx->oids.nr);
2048 oid_array_sort(&ctx->oids);
2049 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
2050 unsigned int num_parents;
2051 struct commit *commit;
2052
2053 display_progress(ctx->progress, i + 1);
2054
2055 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
2056
2057 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
2058 commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
2059 continue;
2060
2061 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
2062 repo_parse_commit(ctx->r, commit);
2063 else
2064 repo_parse_commit_no_graph(ctx->r, commit);
2065
2066 num_parents = commit_list_count(commit->parents);
2067 if (num_parents > 2)
2068 ctx->num_extra_edges += num_parents - 1;
2069
2070 commit_stack_push(&ctx->commits, commit);
2071 }
2072 stop_progress(&ctx->progress);
2073}
2074
2075static int write_graph_chunk_base_1(struct hashfile *f,
2076 struct commit_graph *g)
2077{
2078 int num = 0;
2079
2080 if (!g)
2081 return 0;
2082
2083 num = write_graph_chunk_base_1(f, g->base_graph);
2084 hashwrite(f, g->oid.hash, g->hash_algo->rawsz);
2085 return num + 1;
2086}
2087
2088static int write_graph_chunk_base(struct hashfile *f,
2089 void *data)
2090{
2091 struct write_commit_graph_context *ctx = data;
2092 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
2093
2094 if (num != ctx->num_commit_graphs_after - 1) {
2095 error(_("failed to write correct number of base graph ids"));
2096 return -1;
2097 }
2098
2099 return 0;
2100}
2101
2102static int write_commit_graph_file(struct write_commit_graph_context *ctx)
2103{
2104 uint32_t i;
2105 struct hashfile *f;
2106 struct tempfile *graph_layer; /* when ctx->split is non-zero */
2107 struct lock_file lk = LOCK_INIT;
2108 const unsigned hashsz = ctx->r->hash_algo->rawsz;
2109 struct strbuf progress_title = STRBUF_INIT;
2110 struct chunkfile *cf;
2111 unsigned char file_hash[GIT_MAX_RAWSZ];
2112
2113 if (ctx->split) {
2114 struct strbuf tmp_file = STRBUF_INIT;
2115
2116 strbuf_addf(&tmp_file,
2117 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2118 ctx->dir);
2119 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
2120 } else {
2121 ctx->graph_name = get_commit_graph_filename(ctx->dir);
2122 }
2123
2124 if (safe_create_leading_directories(ctx->r, ctx->graph_name)) {
2125 error(_("unable to create leading directories of %s"),
2126 ctx->graph_name);
2127 return -1;
2128 }
2129
2130 if (ctx->split) {
2131 char *lock_name = get_commit_graph_chain_filename(ctx->dir);
2132
2133 repo_hold_lock_file_for_update_mode(ctx->r, &lk, lock_name,
2134 LOCK_DIE_ON_ERROR, 0444);
2135 free(lock_name);
2136
2137 graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
2138 if (!graph_layer) {
2139 error(_("unable to create temporary graph layer"));
2140 return -1;
2141 }
2142
2143 if (adjust_shared_perm(ctx->r, get_tempfile_path(graph_layer))) {
2144 error(_("unable to adjust shared permissions for '%s'"),
2145 get_tempfile_path(graph_layer));
2146 return -1;
2147 }
2148
2149 f = hashfd(ctx->r->hash_algo,
2150 get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
2151 } else {
2152 repo_hold_lock_file_for_update_mode(ctx->r, &lk,
2153 ctx->graph_name,
2154 LOCK_DIE_ON_ERROR, 0444);
2155 f = hashfd(ctx->r->hash_algo,
2156 get_lock_file_fd(&lk), get_lock_file_path(&lk));
2157 }
2158
2159 cf = init_chunkfile(f);
2160
2161 add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE,
2162 write_graph_chunk_fanout);
2163 add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr),
2164 write_graph_chunk_oids);
2165 add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr),
2166 write_graph_chunk_data);
2167
2168 if (ctx->write_generation_data)
2169 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA,
2170 st_mult(sizeof(uint32_t), ctx->commits.nr),
2171 write_graph_chunk_generation_data);
2172 if (ctx->num_generation_data_overflows)
2173 add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW,
2174 st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows),
2175 write_graph_chunk_generation_data_overflow);
2176 if (ctx->num_extra_edges)
2177 add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES,
2178 st_mult(4, ctx->num_extra_edges),
2179 write_graph_chunk_extra_edges);
2180 if (ctx->changed_paths) {
2181 add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES,
2182 st_mult(sizeof(uint32_t), ctx->commits.nr),
2183 write_graph_chunk_bloom_indexes);
2184 add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA,
2185 st_add(sizeof(uint32_t) * 3,
2186 ctx->total_bloom_filter_data_size),
2187 write_graph_chunk_bloom_data);
2188 }
2189 if (ctx->num_commit_graphs_after > 1)
2190 add_chunk(cf, GRAPH_CHUNKID_BASE,
2191 st_mult(hashsz, ctx->num_commit_graphs_after - 1),
2192 write_graph_chunk_base);
2193
2194 hashwrite_be32(f, GRAPH_SIGNATURE);
2195
2196 hashwrite_u8(f, GRAPH_VERSION);
2197 hashwrite_u8(f, oid_version(ctx->r->hash_algo));
2198 hashwrite_u8(f, get_num_chunks(cf));
2199 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
2200
2201 if (ctx->report_progress) {
2202 strbuf_addf(&progress_title,
2203 Q_("Writing out commit graph in %d pass",
2204 "Writing out commit graph in %d passes",
2205 get_num_chunks(cf)),
2206 get_num_chunks(cf));
2207 ctx->progress = start_delayed_progress(
2208 ctx->r,
2209 progress_title.buf,
2210 st_mult(get_num_chunks(cf), ctx->commits.nr));
2211 }
2212
2213 write_chunkfile(cf, ctx);
2214
2215 stop_progress(&ctx->progress);
2216 strbuf_release(&progress_title);
2217
2218 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
2219 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
2220 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->dir, new_base_hash);
2221
2222 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
2223 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
2224 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
2225 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
2226 }
2227
2228 close_commit_graph(ctx->r->objects);
2229 finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH,
2230 CSUM_HASH_IN_STREAM | CSUM_FSYNC);
2231 free_chunkfile(cf);
2232
2233 if (ctx->split) {
2234 FILE *chainf = fdopen_lock_file(&lk, "w");
2235 char *final_graph_name;
2236 int result;
2237
2238 if (!chainf) {
2239 error(_("unable to open commit-graph chain file"));
2240 return -1;
2241 }
2242
2243 if (ctx->base_graph_name) {
2244 const char *dest;
2245 int idx = ctx->num_commit_graphs_after - 1;
2246 if (ctx->num_commit_graphs_after > 1)
2247 idx--;
2248
2249 dest = ctx->commit_graph_filenames_after[idx];
2250
2251 if (strcmp(ctx->base_graph_name, dest)) {
2252 result = rename(ctx->base_graph_name, dest);
2253
2254 if (result) {
2255 error(_("failed to rename base commit-graph file"));
2256 return -1;
2257 }
2258 }
2259 } else {
2260 char *graph_name = get_commit_graph_filename(ctx->dir);
2261 unlink(graph_name);
2262 free(graph_name);
2263 }
2264
2265 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2266 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] =
2267 xstrdup(hash_to_hex_algop(file_hash, ctx->r->hash_algo));
2268 final_graph_name = get_split_graph_filename(ctx->dir,
2269 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
2270 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
2271 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
2272
2273 result = rename_tempfile(&graph_layer, final_graph_name);
2274
2275 for (i = 0; i < ctx->num_commit_graphs_after; i++)
2276 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
2277
2278 if (result) {
2279 error(_("failed to rename temporary commit-graph file"));
2280 return -1;
2281 }
2282 }
2283
2284 commit_lock_file(&lk);
2285
2286 return 0;
2287}
2288
2289static void split_graph_merge_strategy(struct write_commit_graph_context *ctx,
2290 struct commit_graph *graph_to_merge)
2291{
2292 struct commit_graph *g;
2293 uint32_t num_commits;
2294 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
2295 uint32_t i;
2296
2297 int max_commits = 0;
2298 int size_mult = 2;
2299
2300 if (ctx->opts) {
2301 max_commits = ctx->opts->max_commits;
2302
2303 if (ctx->opts->size_multiple)
2304 size_mult = ctx->opts->size_multiple;
2305
2306 flags = ctx->opts->split_flags;
2307 }
2308
2309 g = graph_to_merge;
2310 num_commits = ctx->commits.nr;
2311 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
2312 ctx->num_commit_graphs_after = 1;
2313 else
2314 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
2315
2316 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
2317 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
2318 while (g && (g->num_commits <= st_mult(size_mult, num_commits) ||
2319 (max_commits && num_commits > max_commits))) {
2320 if (strcmp(g->dir, ctx->dir))
2321 break;
2322
2323 if (unsigned_add_overflows(num_commits, g->num_commits))
2324 die(_("cannot merge graphs with %"PRIuMAX", "
2325 "%"PRIuMAX" commits"),
2326 (uintmax_t)num_commits,
2327 (uintmax_t)g->num_commits);
2328 num_commits += g->num_commits;
2329 g = g->base_graph;
2330
2331 ctx->num_commit_graphs_after--;
2332 }
2333 }
2334
2335 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
2336 ctx->new_base_graph = g;
2337 else if (ctx->num_commit_graphs_after != 1)
2338 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2339 "should be 1 with --split=replace");
2340
2341 if (ctx->num_commit_graphs_after == 2) {
2342 char *old_graph_name = get_commit_graph_filename(g->dir);
2343
2344 if (!strcmp(g->filename, old_graph_name) &&
2345 strcmp(g->dir, ctx->dir)) {
2346 ctx->num_commit_graphs_after = 1;
2347 ctx->new_base_graph = NULL;
2348 }
2349
2350 free(old_graph_name);
2351 }
2352
2353 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
2354 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
2355
2356 for (i = 0; i < ctx->num_commit_graphs_after &&
2357 i < ctx->num_commit_graphs_before; i++)
2358 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
2359
2360 i = ctx->num_commit_graphs_before - 1;
2361 g = graph_to_merge;
2362
2363 while (g) {
2364 if (i < ctx->num_commit_graphs_after)
2365 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
2366
2367 /*
2368 * If the topmost remaining layer has generation data chunk, the
2369 * resultant layer also has generation data chunk.
2370 */
2371 if (i == ctx->num_commit_graphs_after - 2)
2372 ctx->write_generation_data = !!g->chunk_generation_data;
2373
2374 i--;
2375 g = g->base_graph;
2376 }
2377}
2378
2379static void merge_commit_graph(struct write_commit_graph_context *ctx,
2380 struct commit_graph *g)
2381{
2382 uint32_t i;
2383 uint32_t offset = g->num_commits_in_base;
2384
2385 if (unsigned_add_overflows(ctx->commits.nr, g->num_commits))
2386 die(_("cannot merge graph %s, too many commits: %"PRIuMAX),
2387 oid_to_hex(&g->oid),
2388 (uintmax_t)st_add(ctx->commits.nr, g->num_commits));
2389
2390 commit_stack_grow(&ctx->commits, g->num_commits);
2391
2392 for (i = 0; i < g->num_commits; i++) {
2393 struct object_id oid;
2394 struct commit *result;
2395
2396 display_progress(ctx->progress, i + 1);
2397
2398 load_oid_from_graph(g, i + offset, &oid);
2399
2400 /* only add commits if they still exist in the repo */
2401 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
2402
2403 if (result)
2404 commit_stack_push(&ctx->commits, result);
2405 }
2406}
2407
2408static int commit_compare(const void *_a, const void *_b)
2409{
2410 const struct commit *a = *(const struct commit **)_a;
2411 const struct commit *b = *(const struct commit **)_b;
2412 return oidcmp(&a->object.oid, &b->object.oid);
2413}
2414
2415static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2416{
2417 uint32_t i, dedup_i = 0;
2418
2419 if (ctx->report_progress)
2420 ctx->progress = start_delayed_progress(
2421 ctx->r,
2422 _("Scanning merged commits"),
2423 ctx->commits.nr);
2424
2425 QSORT(ctx->commits.items, ctx->commits.nr, commit_compare);
2426
2427 ctx->num_extra_edges = 0;
2428 for (i = 0; i < ctx->commits.nr; i++) {
2429 display_progress(ctx->progress, i + 1);
2430
2431 if (i && oideq(&ctx->commits.items[i - 1]->object.oid,
2432 &ctx->commits.items[i]->object.oid)) {
2433 /*
2434 * Silently ignore duplicates. These were likely
2435 * created due to a commit appearing in multiple
2436 * layers of the chain, which is unexpected but
2437 * not invalid. We should make sure there is a
2438 * unique copy in the new layer.
2439 */
2440 } else {
2441 unsigned int num_parents;
2442
2443 ctx->commits.items[dedup_i] = ctx->commits.items[i];
2444 dedup_i++;
2445
2446 num_parents = commit_list_count(ctx->commits.items[i]->parents);
2447 if (num_parents > 2)
2448 ctx->num_extra_edges += num_parents - 1;
2449 }
2450 }
2451
2452 ctx->commits.nr = dedup_i;
2453
2454 stop_progress(&ctx->progress);
2455}
2456
2457static void merge_commit_graphs(struct write_commit_graph_context *ctx,
2458 struct commit_graph *g)
2459{
2460 uint32_t current_graph_number = ctx->num_commit_graphs_before;
2461
2462 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2463 current_graph_number--;
2464
2465 if (ctx->report_progress)
2466 ctx->progress = start_delayed_progress(ctx->r,
2467 _("Merging commit-graph"), 0);
2468
2469 merge_commit_graph(ctx, g);
2470 stop_progress(&ctx->progress);
2471
2472 g = g->base_graph;
2473 }
2474
2475 if (g) {
2476 ctx->new_base_graph = g;
2477 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2478 }
2479
2480 if (ctx->new_base_graph)
2481 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2482
2483 sort_and_scan_merged_commits(ctx);
2484}
2485
2486static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2487{
2488 uint32_t i;
2489 time_t now = time(NULL);
2490
2491 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2492 struct stat st;
2493 struct utimbuf updated_time;
2494
2495 if (stat(ctx->commit_graph_filenames_before[i], &st) < 0)
2496 continue;
2497
2498 updated_time.actime = st.st_atime;
2499 updated_time.modtime = now;
2500 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2501 }
2502}
2503
2504static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2505{
2506 struct strbuf path = STRBUF_INIT;
2507 DIR *dir;
2508 struct dirent *de;
2509 size_t dirnamelen;
2510 timestamp_t expire_time = time(NULL);
2511
2512 if (ctx->opts && ctx->opts->expire_time)
2513 expire_time = ctx->opts->expire_time;
2514 if (!ctx->split) {
2515 char *chain_file_name = get_commit_graph_chain_filename(ctx->dir);
2516 unlink(chain_file_name);
2517 free(chain_file_name);
2518 ctx->num_commit_graphs_after = 0;
2519 }
2520
2521 strbuf_addstr(&path, ctx->dir);
2522 strbuf_addstr(&path, "/info/commit-graphs");
2523 dir = opendir(path.buf);
2524
2525 if (!dir)
2526 goto out;
2527
2528 strbuf_addch(&path, '/');
2529 dirnamelen = path.len;
2530 while ((de = readdir(dir)) != NULL) {
2531 struct stat st;
2532 uint32_t i, found = 0;
2533
2534 strbuf_setlen(&path, dirnamelen);
2535 strbuf_addstr(&path, de->d_name);
2536
2537 if (stat(path.buf, &st) < 0)
2538 continue;
2539
2540 if (st.st_mtime > expire_time)
2541 continue;
2542 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2543 continue;
2544
2545 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2546 if (!strcmp(ctx->commit_graph_filenames_after[i],
2547 path.buf)) {
2548 found = 1;
2549 break;
2550 }
2551 }
2552
2553 if (!found)
2554 unlink(path.buf);
2555 }
2556
2557out:
2558 if(dir)
2559 closedir(dir);
2560 strbuf_release(&path);
2561}
2562
2563int write_commit_graph(struct repository *r,
2564 const char *dir,
2565 const struct string_list *const pack_indexes,
2566 struct oidset *commits,
2567 enum commit_graph_write_flags flags,
2568 const struct commit_graph_opts *opts)
2569{
2570 struct write_commit_graph_context ctx = {
2571 .r = r,
2572 .append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0,
2573 .report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0,
2574 .split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0,
2575 .opts = opts,
2576 .total_bloom_filter_data_size = 0,
2577 .write_generation_data = (get_configured_generation_version(r) == 2),
2578 .num_generation_data_overflows = 0,
2579 };
2580 uint32_t i;
2581 int res = 0;
2582 int replace = 0;
2583 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
2584 struct topo_level_slab topo_levels;
2585 struct commit_graph *g;
2586
2587 prepare_repo_settings(r);
2588 if (!r->settings.core_commit_graph) {
2589 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2590 return 0;
2591 }
2592 if (!commit_graph_compatible(r))
2593 return 0;
2594 if (r->settings.commit_graph_changed_paths_version < -1
2595 || r->settings.commit_graph_changed_paths_version > 2) {
2596 warning(_("attempting to write a commit-graph, but "
2597 "'commitGraph.changedPathsVersion' (%d) is not supported"),
2598 r->settings.commit_graph_changed_paths_version);
2599 return 0;
2600 }
2601
2602 ctx.dir = absolute_pathdup(dir);
2603
2604 bloom_settings.hash_version = r->settings.commit_graph_changed_paths_version;
2605 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2606 bloom_settings.bits_per_entry);
2607 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2608 bloom_settings.num_hashes);
2609 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2610 bloom_settings.max_changed_paths);
2611 ctx.bloom_settings = &bloom_settings;
2612
2613 init_topo_level_slab(&topo_levels);
2614 ctx.topo_levels = &topo_levels;
2615
2616 g = prepare_commit_graph(ctx.r);
2617 for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2618 chain->topo_levels = &topo_levels;
2619
2620 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2621 ctx.changed_paths = 1;
2622 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2623 /* We have changed-paths already. Keep them in the next graph */
2624 if (g && g->bloom_filter_settings) {
2625 ctx.changed_paths = 1;
2626
2627 /* don't propagate the hash_version unless unspecified */
2628 if (bloom_settings.hash_version == -1)
2629 bloom_settings.hash_version = g->bloom_filter_settings->hash_version;
2630 bloom_settings.bits_per_entry = g->bloom_filter_settings->bits_per_entry;
2631 bloom_settings.num_hashes = g->bloom_filter_settings->num_hashes;
2632 bloom_settings.max_changed_paths = g->bloom_filter_settings->max_changed_paths;
2633 }
2634 }
2635
2636 bloom_settings.hash_version = bloom_settings.hash_version == 2 ? 2 : 1;
2637
2638 if (ctx.split) {
2639 for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2640 ctx.num_commit_graphs_before++;
2641
2642 if (ctx.num_commit_graphs_before) {
2643 ALLOC_ARRAY(ctx.commit_graph_filenames_before, ctx.num_commit_graphs_before);
2644 i = ctx.num_commit_graphs_before;
2645
2646 for (struct commit_graph *chain = g; chain; chain = chain->base_graph)
2647 ctx.commit_graph_filenames_before[--i] = xstrdup(chain->filename);
2648 }
2649
2650 if (ctx.opts)
2651 replace = ctx.opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
2652 }
2653
2654 if (odb_count_objects(r->objects, ODB_COUNT_OBJECTS_APPROXIMATE, &ctx.approx_nr_objects) < 0)
2655 ctx.approx_nr_objects = 0;
2656
2657 if (ctx.append && g) {
2658 for (i = 0; i < g->num_commits; i++) {
2659 struct object_id oid;
2660 oidread(&oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2661 r->hash_algo);
2662 oid_array_append(&ctx.oids, &oid);
2663 }
2664 }
2665
2666 if (pack_indexes) {
2667 ctx.order_by_pack = 1;
2668 if ((res = fill_oids_from_packs(&ctx, pack_indexes)))
2669 goto cleanup;
2670 }
2671
2672 if (commits) {
2673 if ((res = fill_oids_from_commits(&ctx, commits)))
2674 goto cleanup;
2675 }
2676
2677 if (!pack_indexes && !commits) {
2678 ctx.order_by_pack = 1;
2679 fill_oids_from_all_packs(&ctx);
2680 }
2681
2682 close_reachable(&ctx);
2683
2684 copy_oids_to_commits(&ctx);
2685
2686 if (ctx.commits.nr >= GRAPH_EDGE_LAST_MASK) {
2687 error(_("too many commits to write graph"));
2688 res = -1;
2689 goto cleanup;
2690 }
2691
2692 if (!ctx.commits.nr && !replace)
2693 goto cleanup;
2694
2695 if (ctx.split) {
2696 split_graph_merge_strategy(&ctx, g);
2697
2698 if (!replace)
2699 merge_commit_graphs(&ctx, g);
2700 } else {
2701 ctx.num_commit_graphs_after = 1;
2702 }
2703
2704 ctx.trust_generation_numbers = validate_mixed_generation_chain(g);
2705
2706 compute_topological_levels(&ctx);
2707 if (ctx.write_generation_data)
2708 compute_generation_numbers(&ctx);
2709
2710 if (ctx.changed_paths)
2711 compute_bloom_filters(&ctx);
2712
2713 res = write_commit_graph_file(&ctx);
2714
2715 if (ctx.changed_paths)
2716 deinit_bloom_filters();
2717
2718 if (ctx.split)
2719 mark_commit_graphs(&ctx);
2720
2721 expire_commit_graphs(&ctx);
2722
2723cleanup:
2724 free(ctx.graph_name);
2725 free(ctx.base_graph_name);
2726 free(ctx.dir);
2727 commit_stack_clear(&ctx.commits);
2728 oid_array_clear(&ctx.oids);
2729 clear_topo_level_slab(&topo_levels);
2730
2731 if (ctx.r->objects->commit_graph) {
2732 struct commit_graph *g = ctx.r->objects->commit_graph;
2733
2734 while (g) {
2735 g->topo_levels = NULL;
2736 g = g->base_graph;
2737 }
2738 }
2739
2740 for (i = 0; i < ctx.num_commit_graphs_before; i++)
2741 free(ctx.commit_graph_filenames_before[i]);
2742 free(ctx.commit_graph_filenames_before);
2743
2744 for (i = 0; i < ctx.num_commit_graphs_after; i++) {
2745 free(ctx.commit_graph_filenames_after[i]);
2746 free(ctx.commit_graph_hash_after[i]);
2747 }
2748 free(ctx.commit_graph_filenames_after);
2749 free(ctx.commit_graph_hash_after);
2750
2751 return res;
2752}
2753
2754#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2755static int verify_commit_graph_error;
2756
2757__attribute__((format (printf, 1, 2)))
2758static void graph_report(const char *fmt, ...)
2759{
2760 va_list ap;
2761
2762 verify_commit_graph_error = 1;
2763 va_start(ap, fmt);
2764 vfprintf(stderr, fmt, ap);
2765 fprintf(stderr, "\n");
2766 va_end(ap);
2767}
2768
2769static int commit_graph_checksum_valid(struct commit_graph *g)
2770{
2771 return hashfile_checksum_valid(g->hash_algo,
2772 g->data, g->data_len);
2773}
2774
2775static int verify_one_commit_graph(struct commit_graph *g,
2776 struct progress *progress,
2777 uint64_t *seen)
2778{
2779 struct repository *r = g->repo;
2780 uint32_t i, cur_fanout_pos = 0;
2781 struct object_id prev_oid, cur_oid;
2782 struct commit *seen_gen_zero = NULL;
2783 struct commit *seen_gen_non_zero = NULL;
2784
2785 if (!commit_graph_checksum_valid(g)) {
2786 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2787 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2788 }
2789
2790 for (i = 0; i < g->num_commits; i++) {
2791 struct commit *graph_commit;
2792
2793 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2794 g->hash_algo);
2795
2796 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2797 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2798 oid_to_hex(&prev_oid),
2799 oid_to_hex(&cur_oid));
2800
2801 oidcpy(&prev_oid, &cur_oid);
2802
2803 while (cur_oid.hash[0] > cur_fanout_pos) {
2804 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2805
2806 if (i != fanout_value)
2807 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2808 cur_fanout_pos, fanout_value, i);
2809 cur_fanout_pos++;
2810 }
2811
2812 graph_commit = lookup_commit(r, &cur_oid);
2813 if (!parse_commit_in_graph_one(g, graph_commit))
2814 graph_report(_("failed to parse commit %s from commit-graph"),
2815 oid_to_hex(&cur_oid));
2816 }
2817
2818 while (cur_fanout_pos < 256) {
2819 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2820
2821 if (g->num_commits != fanout_value)
2822 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2823 cur_fanout_pos, fanout_value, i);
2824
2825 cur_fanout_pos++;
2826 }
2827
2828 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2829 return verify_commit_graph_error;
2830
2831 for (i = 0; i < g->num_commits; i++) {
2832 struct commit *graph_commit, *odb_commit;
2833 struct commit_list *graph_parents, *odb_parents;
2834 timestamp_t max_generation = 0;
2835 timestamp_t generation;
2836
2837 display_progress(progress, ++(*seen));
2838 oidread(&cur_oid, g->chunk_oid_lookup + st_mult(g->hash_algo->rawsz, i),
2839 g->hash_algo);
2840
2841 graph_commit = lookup_commit(r, &cur_oid);
2842 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2843 if (repo_parse_commit_internal(r, odb_commit, 0, 0)) {
2844 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2845 oid_to_hex(&cur_oid));
2846 continue;
2847 }
2848
2849 if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
2850 get_commit_tree_oid(odb_commit)))
2851 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2852 oid_to_hex(&cur_oid),
2853 oid_to_hex(get_commit_tree_oid(graph_commit)),
2854 oid_to_hex(get_commit_tree_oid(odb_commit)));
2855
2856 graph_parents = graph_commit->parents;
2857 odb_parents = odb_commit->parents;
2858
2859 while (graph_parents) {
2860 if (!odb_parents) {
2861 graph_report(_("commit-graph parent list for commit %s is too long"),
2862 oid_to_hex(&cur_oid));
2863 break;
2864 }
2865
2866 /* parse parent in case it is in a base graph */
2867 parse_commit_in_graph_one(g, graph_parents->item);
2868
2869 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2870 graph_report(_("commit-graph parent for %s is %s != %s"),
2871 oid_to_hex(&cur_oid),
2872 oid_to_hex(&graph_parents->item->object.oid),
2873 oid_to_hex(&odb_parents->item->object.oid));
2874
2875 generation = commit_graph_generation_from_graph(graph_parents->item);
2876 if (generation > max_generation)
2877 max_generation = generation;
2878
2879 graph_parents = graph_parents->next;
2880 odb_parents = odb_parents->next;
2881 }
2882
2883 if (odb_parents)
2884 graph_report(_("commit-graph parent list for commit %s terminates early"),
2885 oid_to_hex(&cur_oid));
2886
2887 if (commit_graph_generation_from_graph(graph_commit))
2888 seen_gen_non_zero = graph_commit;
2889 else
2890 seen_gen_zero = graph_commit;
2891
2892 if (seen_gen_zero)
2893 continue;
2894
2895 /*
2896 * If we are using topological level and one of our parents has
2897 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2898 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2899 * in the following condition.
2900 */
2901 if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX)
2902 max_generation--;
2903
2904 generation = commit_graph_generation(graph_commit);
2905 if (generation < max_generation + 1)
2906 graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime),
2907 oid_to_hex(&cur_oid),
2908 generation,
2909 max_generation + 1);
2910
2911 if (graph_commit->date != odb_commit->date)
2912 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2913 oid_to_hex(&cur_oid),
2914 graph_commit->date,
2915 odb_commit->date);
2916 }
2917
2918 if (seen_gen_zero && seen_gen_non_zero)
2919 graph_report(_("commit-graph has both zero and non-zero "
2920 "generations (e.g., commits '%s' and '%s')"),
2921 oid_to_hex(&seen_gen_zero->object.oid),
2922 oid_to_hex(&seen_gen_non_zero->object.oid));
2923
2924 return verify_commit_graph_error;
2925}
2926
2927int verify_commit_graph(struct commit_graph *g, int flags)
2928{
2929 struct progress *progress = NULL;
2930 int local_error = 0;
2931 uint64_t seen = 0;
2932
2933 if (!g) {
2934 graph_report("no commit-graph file loaded");
2935 return 1;
2936 }
2937
2938 if (flags & COMMIT_GRAPH_WRITE_PROGRESS) {
2939 uint64_t total = g->num_commits;
2940 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW))
2941 total += g->num_commits_in_base;
2942
2943 progress = start_progress(g->repo,
2944 _("Verifying commits in commit graph"),
2945 total);
2946 }
2947
2948 for (; g; g = g->base_graph) {
2949 local_error |= verify_one_commit_graph(g, progress, &seen);
2950 if (flags & COMMIT_GRAPH_VERIFY_SHALLOW)
2951 break;
2952 }
2953
2954 stop_progress(&progress);
2955
2956 return local_error;
2957}
2958
2959void free_commit_graph(struct commit_graph *g)
2960{
2961 while (g) {
2962 struct commit_graph *next = g->base_graph;
2963
2964 if (g->data)
2965 munmap((void *)g->data, g->data_len);
2966 free(g->dir);
2967 free(g->filename);
2968 free(g->bloom_filter_settings);
2969 free(g);
2970
2971 g = next;
2972 }
2973}
2974
2975void disable_commit_graph(struct repository *r)
2976{
2977 r->commit_graph_disabled = 1;
2978}