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