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