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