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