]> git.ipfire.org Git - thirdparty/git.git/blob - commit-graph.c
commit-graph: turn a group of write-related macro flags into an enum
[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
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
438 return graph_chain;
439 }
440
441 struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
442 {
443 struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
444
445 if (!g)
446 g = load_commit_graph_chain(r, obj_dir);
447
448 return g;
449 }
450
451 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
452 {
453
454 if (r->objects->commit_graph)
455 return;
456
457 r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
458 }
459
460 /*
461 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
462 *
463 * On the first invocation, this function attemps to load the commit
464 * graph if the_repository is configured to have one.
465 */
466 static int prepare_commit_graph(struct repository *r)
467 {
468 struct object_directory *odb;
469 int config_value;
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 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
480 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
481 !config_value))
482 /*
483 * This repository is not configured to use commit graphs, so
484 * do not load one. (But report commit_graph_attempted anyway
485 * so that commit graph loading is not attempted again for this
486 * repository.)
487 */
488 return 0;
489
490 if (!commit_graph_compatible(r))
491 return 0;
492
493 prepare_alt_odb(r);
494 for (odb = r->objects->odb;
495 !r->objects->commit_graph && odb;
496 odb = odb->next)
497 prepare_commit_graph_one(r, odb->path);
498 return !!r->objects->commit_graph;
499 }
500
501 int generation_numbers_enabled(struct repository *r)
502 {
503 uint32_t first_generation;
504 struct commit_graph *g;
505 if (!prepare_commit_graph(r))
506 return 0;
507
508 g = r->objects->commit_graph;
509
510 if (!g->num_commits)
511 return 0;
512
513 first_generation = get_be32(g->chunk_commit_data +
514 g->hash_len + 8) >> 2;
515
516 return !!first_generation;
517 }
518
519 static void close_commit_graph_one(struct commit_graph *g)
520 {
521 if (!g)
522 return;
523
524 close_commit_graph_one(g->base_graph);
525 free_commit_graph(g);
526 }
527
528 void close_commit_graph(struct raw_object_store *o)
529 {
530 close_commit_graph_one(o->commit_graph);
531 o->commit_graph = NULL;
532 }
533
534 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
535 {
536 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
537 g->chunk_oid_lookup, g->hash_len, pos);
538 }
539
540 static void load_oid_from_graph(struct commit_graph *g,
541 uint32_t pos,
542 struct object_id *oid)
543 {
544 uint32_t lex_index;
545
546 while (g && pos < g->num_commits_in_base)
547 g = g->base_graph;
548
549 if (!g)
550 BUG("NULL commit-graph");
551
552 if (pos >= g->num_commits + g->num_commits_in_base)
553 die(_("invalid commit position. commit-graph is likely corrupt"));
554
555 lex_index = pos - g->num_commits_in_base;
556
557 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
558 }
559
560 static struct commit_list **insert_parent_or_die(struct repository *r,
561 struct commit_graph *g,
562 uint32_t pos,
563 struct commit_list **pptr)
564 {
565 struct commit *c;
566 struct object_id oid;
567
568 if (pos >= g->num_commits + g->num_commits_in_base)
569 die("invalid parent position %"PRIu32, pos);
570
571 load_oid_from_graph(g, pos, &oid);
572 c = lookup_commit(r, &oid);
573 if (!c)
574 die(_("could not find commit %s"), oid_to_hex(&oid));
575 c->graph_pos = pos;
576 return &commit_list_insert(c, pptr)->next;
577 }
578
579 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
580 {
581 const unsigned char *commit_data;
582 uint32_t lex_index;
583
584 while (pos < g->num_commits_in_base)
585 g = g->base_graph;
586
587 lex_index = pos - g->num_commits_in_base;
588 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
589 item->graph_pos = pos;
590 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
591 }
592
593 static inline void set_commit_tree(struct commit *c, struct tree *t)
594 {
595 c->maybe_tree = t;
596 }
597
598 static int fill_commit_in_graph(struct repository *r,
599 struct commit *item,
600 struct commit_graph *g, uint32_t pos)
601 {
602 uint32_t edge_value;
603 uint32_t *parent_data_ptr;
604 uint64_t date_low, date_high;
605 struct commit_list **pptr;
606 const unsigned char *commit_data;
607 uint32_t lex_index;
608
609 while (pos < g->num_commits_in_base)
610 g = g->base_graph;
611
612 if (pos >= g->num_commits + g->num_commits_in_base)
613 die(_("invalid commit position. commit-graph is likely corrupt"));
614
615 /*
616 * Store the "full" position, but then use the
617 * "local" position for the rest of the calculation.
618 */
619 item->graph_pos = pos;
620 lex_index = pos - g->num_commits_in_base;
621
622 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
623
624 item->object.parsed = 1;
625
626 set_commit_tree(item, NULL);
627
628 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
629 date_low = get_be32(commit_data + g->hash_len + 12);
630 item->date = (timestamp_t)((date_high << 32) | date_low);
631
632 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
633
634 pptr = &item->parents;
635
636 edge_value = get_be32(commit_data + g->hash_len);
637 if (edge_value == GRAPH_PARENT_NONE)
638 return 1;
639 pptr = insert_parent_or_die(r, g, edge_value, pptr);
640
641 edge_value = get_be32(commit_data + g->hash_len + 4);
642 if (edge_value == GRAPH_PARENT_NONE)
643 return 1;
644 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
645 pptr = insert_parent_or_die(r, g, edge_value, pptr);
646 return 1;
647 }
648
649 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
650 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
651 do {
652 edge_value = get_be32(parent_data_ptr);
653 pptr = insert_parent_or_die(r, g,
654 edge_value & GRAPH_EDGE_LAST_MASK,
655 pptr);
656 parent_data_ptr++;
657 } while (!(edge_value & GRAPH_LAST_EDGE));
658
659 return 1;
660 }
661
662 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
663 {
664 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
665 *pos = item->graph_pos;
666 return 1;
667 } else {
668 struct commit_graph *cur_g = g;
669 uint32_t lex_index;
670
671 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
672 cur_g = cur_g->base_graph;
673
674 if (cur_g) {
675 *pos = lex_index + cur_g->num_commits_in_base;
676 return 1;
677 }
678
679 return 0;
680 }
681 }
682
683 static int parse_commit_in_graph_one(struct repository *r,
684 struct commit_graph *g,
685 struct commit *item)
686 {
687 uint32_t pos;
688
689 if (item->object.parsed)
690 return 1;
691
692 if (find_commit_in_graph(item, g, &pos))
693 return fill_commit_in_graph(r, item, g, pos);
694
695 return 0;
696 }
697
698 int parse_commit_in_graph(struct repository *r, struct commit *item)
699 {
700 if (!prepare_commit_graph(r))
701 return 0;
702 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
703 }
704
705 void load_commit_graph_info(struct repository *r, struct commit *item)
706 {
707 uint32_t pos;
708 if (!prepare_commit_graph(r))
709 return;
710 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
711 fill_commit_graph_info(item, r->objects->commit_graph, pos);
712 }
713
714 static struct tree *load_tree_for_commit(struct repository *r,
715 struct commit_graph *g,
716 struct commit *c)
717 {
718 struct object_id oid;
719 const unsigned char *commit_data;
720
721 while (c->graph_pos < g->num_commits_in_base)
722 g = g->base_graph;
723
724 commit_data = g->chunk_commit_data +
725 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
726
727 hashcpy(oid.hash, commit_data);
728 set_commit_tree(c, lookup_tree(r, &oid));
729
730 return c->maybe_tree;
731 }
732
733 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
734 struct commit_graph *g,
735 const struct commit *c)
736 {
737 if (c->maybe_tree)
738 return c->maybe_tree;
739 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
740 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
741
742 return load_tree_for_commit(r, g, (struct commit *)c);
743 }
744
745 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
746 {
747 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
748 }
749
750 struct packed_commit_list {
751 struct commit **list;
752 int nr;
753 int alloc;
754 };
755
756 struct packed_oid_list {
757 struct object_id *list;
758 int nr;
759 int alloc;
760 };
761
762 struct write_commit_graph_context {
763 struct repository *r;
764 char *obj_dir;
765 char *graph_name;
766 struct packed_oid_list oids;
767 struct packed_commit_list commits;
768 int num_extra_edges;
769 unsigned long approx_nr_objects;
770 struct progress *progress;
771 int progress_done;
772 uint64_t progress_cnt;
773
774 char *base_graph_name;
775 int num_commit_graphs_before;
776 int num_commit_graphs_after;
777 char **commit_graph_filenames_before;
778 char **commit_graph_filenames_after;
779 char **commit_graph_hash_after;
780 uint32_t new_num_commits_in_base;
781 struct commit_graph *new_base_graph;
782
783 unsigned append:1,
784 report_progress:1,
785 split:1;
786
787 const struct split_commit_graph_opts *split_opts;
788 };
789
790 static void write_graph_chunk_fanout(struct hashfile *f,
791 struct write_commit_graph_context *ctx)
792 {
793 int i, count = 0;
794 struct commit **list = ctx->commits.list;
795
796 /*
797 * Write the first-level table (the list is sorted,
798 * but we use a 256-entry lookup to be able to avoid
799 * having to do eight extra binary search iterations).
800 */
801 for (i = 0; i < 256; i++) {
802 while (count < ctx->commits.nr) {
803 if ((*list)->object.oid.hash[0] != i)
804 break;
805 display_progress(ctx->progress, ++ctx->progress_cnt);
806 count++;
807 list++;
808 }
809
810 hashwrite_be32(f, count);
811 }
812 }
813
814 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
815 struct write_commit_graph_context *ctx)
816 {
817 struct commit **list = ctx->commits.list;
818 int count;
819 for (count = 0; count < ctx->commits.nr; count++, list++) {
820 display_progress(ctx->progress, ++ctx->progress_cnt);
821 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
822 }
823 }
824
825 static const unsigned char *commit_to_sha1(size_t index, void *table)
826 {
827 struct commit **commits = table;
828 return commits[index]->object.oid.hash;
829 }
830
831 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
832 struct write_commit_graph_context *ctx)
833 {
834 struct commit **list = ctx->commits.list;
835 struct commit **last = ctx->commits.list + ctx->commits.nr;
836 uint32_t num_extra_edges = 0;
837
838 while (list < last) {
839 struct commit_list *parent;
840 int edge_value;
841 uint32_t packedDate[2];
842 display_progress(ctx->progress, ++ctx->progress_cnt);
843
844 parse_commit_no_graph(*list);
845 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
846
847 parent = (*list)->parents;
848
849 if (!parent)
850 edge_value = GRAPH_PARENT_NONE;
851 else {
852 edge_value = sha1_pos(parent->item->object.oid.hash,
853 ctx->commits.list,
854 ctx->commits.nr,
855 commit_to_sha1);
856
857 if (edge_value >= 0)
858 edge_value += ctx->new_num_commits_in_base;
859 else {
860 uint32_t pos;
861 if (find_commit_in_graph(parent->item,
862 ctx->new_base_graph,
863 &pos))
864 edge_value = pos;
865 }
866
867 if (edge_value < 0)
868 BUG("missing parent %s for commit %s",
869 oid_to_hex(&parent->item->object.oid),
870 oid_to_hex(&(*list)->object.oid));
871 }
872
873 hashwrite_be32(f, edge_value);
874
875 if (parent)
876 parent = parent->next;
877
878 if (!parent)
879 edge_value = GRAPH_PARENT_NONE;
880 else if (parent->next)
881 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
882 else {
883 edge_value = sha1_pos(parent->item->object.oid.hash,
884 ctx->commits.list,
885 ctx->commits.nr,
886 commit_to_sha1);
887
888 if (edge_value >= 0)
889 edge_value += ctx->new_num_commits_in_base;
890 else {
891 uint32_t pos;
892 if (find_commit_in_graph(parent->item,
893 ctx->new_base_graph,
894 &pos))
895 edge_value = pos;
896 }
897
898 if (edge_value < 0)
899 BUG("missing parent %s for commit %s",
900 oid_to_hex(&parent->item->object.oid),
901 oid_to_hex(&(*list)->object.oid));
902 }
903
904 hashwrite_be32(f, edge_value);
905
906 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
907 do {
908 num_extra_edges++;
909 parent = parent->next;
910 } while (parent);
911 }
912
913 if (sizeof((*list)->date) > 4)
914 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
915 else
916 packedDate[0] = 0;
917
918 packedDate[0] |= htonl((*list)->generation << 2);
919
920 packedDate[1] = htonl((*list)->date);
921 hashwrite(f, packedDate, 8);
922
923 list++;
924 }
925 }
926
927 static void write_graph_chunk_extra_edges(struct hashfile *f,
928 struct write_commit_graph_context *ctx)
929 {
930 struct commit **list = ctx->commits.list;
931 struct commit **last = ctx->commits.list + ctx->commits.nr;
932 struct commit_list *parent;
933
934 while (list < last) {
935 int num_parents = 0;
936
937 display_progress(ctx->progress, ++ctx->progress_cnt);
938
939 for (parent = (*list)->parents; num_parents < 3 && parent;
940 parent = parent->next)
941 num_parents++;
942
943 if (num_parents <= 2) {
944 list++;
945 continue;
946 }
947
948 /* Since num_parents > 2, this initializer is safe. */
949 for (parent = (*list)->parents->next; parent; parent = parent->next) {
950 int edge_value = sha1_pos(parent->item->object.oid.hash,
951 ctx->commits.list,
952 ctx->commits.nr,
953 commit_to_sha1);
954
955 if (edge_value >= 0)
956 edge_value += ctx->new_num_commits_in_base;
957 else {
958 uint32_t pos;
959 if (find_commit_in_graph(parent->item,
960 ctx->new_base_graph,
961 &pos))
962 edge_value = pos;
963 }
964
965 if (edge_value < 0)
966 BUG("missing parent %s for commit %s",
967 oid_to_hex(&parent->item->object.oid),
968 oid_to_hex(&(*list)->object.oid));
969 else if (!parent->next)
970 edge_value |= GRAPH_LAST_EDGE;
971
972 hashwrite_be32(f, edge_value);
973 }
974
975 list++;
976 }
977 }
978
979 static int oid_compare(const void *_a, const void *_b)
980 {
981 const struct object_id *a = (const struct object_id *)_a;
982 const struct object_id *b = (const struct object_id *)_b;
983 return oidcmp(a, b);
984 }
985
986 static int add_packed_commits(const struct object_id *oid,
987 struct packed_git *pack,
988 uint32_t pos,
989 void *data)
990 {
991 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
992 enum object_type type;
993 off_t offset = nth_packed_object_offset(pack, pos);
994 struct object_info oi = OBJECT_INFO_INIT;
995
996 if (ctx->progress)
997 display_progress(ctx->progress, ++ctx->progress_done);
998
999 oi.typep = &type;
1000 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1001 die(_("unable to get type of object %s"), oid_to_hex(oid));
1002
1003 if (type != OBJ_COMMIT)
1004 return 0;
1005
1006 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1007 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1008 ctx->oids.nr++;
1009
1010 return 0;
1011 }
1012
1013 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1014 {
1015 struct commit_list *parent;
1016 for (parent = commit->parents; parent; parent = parent->next) {
1017 if (!(parent->item->object.flags & UNINTERESTING)) {
1018 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1019 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1020 ctx->oids.nr++;
1021 parent->item->object.flags |= UNINTERESTING;
1022 }
1023 }
1024 }
1025
1026 static void close_reachable(struct write_commit_graph_context *ctx)
1027 {
1028 int i;
1029 struct commit *commit;
1030
1031 if (ctx->report_progress)
1032 ctx->progress = start_delayed_progress(
1033 _("Loading known commits in commit graph"),
1034 ctx->oids.nr);
1035 for (i = 0; i < ctx->oids.nr; i++) {
1036 display_progress(ctx->progress, i + 1);
1037 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1038 if (commit)
1039 commit->object.flags |= UNINTERESTING;
1040 }
1041 stop_progress(&ctx->progress);
1042
1043 /*
1044 * As this loop runs, ctx->oids.nr may grow, but not more
1045 * than the number of missing commits in the reachable
1046 * closure.
1047 */
1048 if (ctx->report_progress)
1049 ctx->progress = start_delayed_progress(
1050 _("Expanding reachable commits in commit graph"),
1051 ctx->oids.nr);
1052 for (i = 0; i < ctx->oids.nr; i++) {
1053 display_progress(ctx->progress, i + 1);
1054 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1055
1056 if (!commit)
1057 continue;
1058 if (ctx->split) {
1059 if (!parse_commit(commit) &&
1060 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1061 add_missing_parents(ctx, commit);
1062 } else if (!parse_commit_no_graph(commit))
1063 add_missing_parents(ctx, commit);
1064 }
1065 stop_progress(&ctx->progress);
1066
1067 if (ctx->report_progress)
1068 ctx->progress = start_delayed_progress(
1069 _("Clearing commit marks in commit graph"),
1070 ctx->oids.nr);
1071 for (i = 0; i < ctx->oids.nr; i++) {
1072 display_progress(ctx->progress, i + 1);
1073 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1074
1075 if (commit)
1076 commit->object.flags &= ~UNINTERESTING;
1077 }
1078 stop_progress(&ctx->progress);
1079 }
1080
1081 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1082 {
1083 int i;
1084 struct commit_list *list = NULL;
1085
1086 if (ctx->report_progress)
1087 ctx->progress = start_progress(
1088 _("Computing commit graph generation numbers"),
1089 ctx->commits.nr);
1090 for (i = 0; i < ctx->commits.nr; i++) {
1091 display_progress(ctx->progress, i + 1);
1092 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1093 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
1094 continue;
1095
1096 commit_list_insert(ctx->commits.list[i], &list);
1097 while (list) {
1098 struct commit *current = list->item;
1099 struct commit_list *parent;
1100 int all_parents_computed = 1;
1101 uint32_t max_generation = 0;
1102
1103 for (parent = current->parents; parent; parent = parent->next) {
1104 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1105 parent->item->generation == GENERATION_NUMBER_ZERO) {
1106 all_parents_computed = 0;
1107 commit_list_insert(parent->item, &list);
1108 break;
1109 } else if (parent->item->generation > max_generation) {
1110 max_generation = parent->item->generation;
1111 }
1112 }
1113
1114 if (all_parents_computed) {
1115 current->generation = max_generation + 1;
1116 pop_commit(&list);
1117
1118 if (current->generation > GENERATION_NUMBER_MAX)
1119 current->generation = GENERATION_NUMBER_MAX;
1120 }
1121 }
1122 }
1123 stop_progress(&ctx->progress);
1124 }
1125
1126 static int add_ref_to_list(const char *refname,
1127 const struct object_id *oid,
1128 int flags, void *cb_data)
1129 {
1130 struct string_list *list = (struct string_list *)cb_data;
1131
1132 string_list_append(list, oid_to_hex(oid));
1133 return 0;
1134 }
1135
1136 int write_commit_graph_reachable(const char *obj_dir,
1137 enum commit_graph_write_flags flags,
1138 const struct split_commit_graph_opts *split_opts)
1139 {
1140 struct string_list list = STRING_LIST_INIT_DUP;
1141 int result;
1142
1143 for_each_ref(add_ref_to_list, &list);
1144 result = write_commit_graph(obj_dir, NULL, &list,
1145 flags, split_opts);
1146
1147 string_list_clear(&list, 0);
1148 return result;
1149 }
1150
1151 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1152 struct string_list *pack_indexes)
1153 {
1154 uint32_t i;
1155 struct strbuf progress_title = STRBUF_INIT;
1156 struct strbuf packname = STRBUF_INIT;
1157 int dirlen;
1158
1159 strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
1160 dirlen = packname.len;
1161 if (ctx->report_progress) {
1162 strbuf_addf(&progress_title,
1163 Q_("Finding commits for commit graph in %d pack",
1164 "Finding commits for commit graph in %d packs",
1165 pack_indexes->nr),
1166 pack_indexes->nr);
1167 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1168 ctx->progress_done = 0;
1169 }
1170 for (i = 0; i < pack_indexes->nr; i++) {
1171 struct packed_git *p;
1172 strbuf_setlen(&packname, dirlen);
1173 strbuf_addstr(&packname, pack_indexes->items[i].string);
1174 p = add_packed_git(packname.buf, packname.len, 1);
1175 if (!p) {
1176 error(_("error adding pack %s"), packname.buf);
1177 return -1;
1178 }
1179 if (open_pack_index(p)) {
1180 error(_("error opening index for %s"), packname.buf);
1181 return -1;
1182 }
1183 for_each_object_in_pack(p, add_packed_commits, ctx,
1184 FOR_EACH_OBJECT_PACK_ORDER);
1185 close_pack(p);
1186 free(p);
1187 }
1188
1189 stop_progress(&ctx->progress);
1190 strbuf_reset(&progress_title);
1191 strbuf_release(&packname);
1192
1193 return 0;
1194 }
1195
1196 static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1197 struct string_list *commit_hex)
1198 {
1199 uint32_t i;
1200 struct strbuf progress_title = STRBUF_INIT;
1201
1202 if (ctx->report_progress) {
1203 strbuf_addf(&progress_title,
1204 Q_("Finding commits for commit graph from %d ref",
1205 "Finding commits for commit graph from %d refs",
1206 commit_hex->nr),
1207 commit_hex->nr);
1208 ctx->progress = start_delayed_progress(
1209 progress_title.buf,
1210 commit_hex->nr);
1211 }
1212 for (i = 0; i < commit_hex->nr; i++) {
1213 const char *end;
1214 struct object_id oid;
1215 struct commit *result;
1216
1217 display_progress(ctx->progress, i + 1);
1218 if (commit_hex->items[i].string &&
1219 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
1220 continue;
1221
1222 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1223
1224 if (result) {
1225 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1226 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1227 ctx->oids.nr++;
1228 }
1229 }
1230 stop_progress(&ctx->progress);
1231 strbuf_release(&progress_title);
1232 }
1233
1234 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1235 {
1236 if (ctx->report_progress)
1237 ctx->progress = start_delayed_progress(
1238 _("Finding commits for commit graph among packed objects"),
1239 ctx->approx_nr_objects);
1240 for_each_packed_object(add_packed_commits, ctx,
1241 FOR_EACH_OBJECT_PACK_ORDER);
1242 if (ctx->progress_done < ctx->approx_nr_objects)
1243 display_progress(ctx->progress, ctx->approx_nr_objects);
1244 stop_progress(&ctx->progress);
1245 }
1246
1247 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1248 {
1249 uint32_t i, count_distinct = 1;
1250
1251 if (ctx->report_progress)
1252 ctx->progress = start_delayed_progress(
1253 _("Counting distinct commits in commit graph"),
1254 ctx->oids.nr);
1255 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1256 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1257
1258 for (i = 1; i < ctx->oids.nr; i++) {
1259 display_progress(ctx->progress, i + 1);
1260 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1261 if (ctx->split) {
1262 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1263
1264 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1265 continue;
1266 }
1267
1268 count_distinct++;
1269 }
1270 }
1271 stop_progress(&ctx->progress);
1272
1273 return count_distinct;
1274 }
1275
1276 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1277 {
1278 uint32_t i;
1279 struct commit_list *parent;
1280
1281 ctx->num_extra_edges = 0;
1282 if (ctx->report_progress)
1283 ctx->progress = start_delayed_progress(
1284 _("Finding extra edges in commit graph"),
1285 ctx->oids.nr);
1286 for (i = 0; i < ctx->oids.nr; i++) {
1287 int num_parents = 0;
1288 display_progress(ctx->progress, i + 1);
1289 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1290 continue;
1291
1292 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1293 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1294
1295 if (ctx->split &&
1296 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1297 continue;
1298
1299 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1300
1301 for (parent = ctx->commits.list[ctx->commits.nr]->parents;
1302 parent; parent = parent->next)
1303 num_parents++;
1304
1305 if (num_parents > 2)
1306 ctx->num_extra_edges += num_parents - 1;
1307
1308 ctx->commits.nr++;
1309 }
1310 stop_progress(&ctx->progress);
1311 }
1312
1313 static int write_graph_chunk_base_1(struct hashfile *f,
1314 struct commit_graph *g)
1315 {
1316 int num = 0;
1317
1318 if (!g)
1319 return 0;
1320
1321 num = write_graph_chunk_base_1(f, g->base_graph);
1322 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1323 return num + 1;
1324 }
1325
1326 static int write_graph_chunk_base(struct hashfile *f,
1327 struct write_commit_graph_context *ctx)
1328 {
1329 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1330
1331 if (num != ctx->num_commit_graphs_after - 1) {
1332 error(_("failed to write correct number of base graph ids"));
1333 return -1;
1334 }
1335
1336 return 0;
1337 }
1338
1339 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1340 {
1341 uint32_t i;
1342 int fd;
1343 struct hashfile *f;
1344 struct lock_file lk = LOCK_INIT;
1345 uint32_t chunk_ids[6];
1346 uint64_t chunk_offsets[6];
1347 const unsigned hashsz = the_hash_algo->rawsz;
1348 struct strbuf progress_title = STRBUF_INIT;
1349 int num_chunks = 3;
1350 struct object_id file_hash;
1351
1352 if (ctx->split) {
1353 struct strbuf tmp_file = STRBUF_INIT;
1354
1355 strbuf_addf(&tmp_file,
1356 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1357 ctx->obj_dir);
1358 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1359 } else {
1360 ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1361 }
1362
1363 if (safe_create_leading_directories(ctx->graph_name)) {
1364 UNLEAK(ctx->graph_name);
1365 error(_("unable to create leading directories of %s"),
1366 ctx->graph_name);
1367 return -1;
1368 }
1369
1370 if (ctx->split) {
1371 char *lock_name = get_chain_filename(ctx->obj_dir);
1372
1373 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1374
1375 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1376 if (fd < 0) {
1377 error(_("unable to create '%s'"), ctx->graph_name);
1378 return -1;
1379 }
1380
1381 f = hashfd(fd, ctx->graph_name);
1382 } else {
1383 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1384 fd = lk.tempfile->fd;
1385 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1386 }
1387
1388 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1389 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1390 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1391 if (ctx->num_extra_edges) {
1392 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1393 num_chunks++;
1394 }
1395 if (ctx->num_commit_graphs_after > 1) {
1396 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1397 num_chunks++;
1398 }
1399
1400 chunk_ids[num_chunks] = 0;
1401
1402 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1403 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1404 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1405 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1406
1407 num_chunks = 3;
1408 if (ctx->num_extra_edges) {
1409 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1410 4 * ctx->num_extra_edges;
1411 num_chunks++;
1412 }
1413 if (ctx->num_commit_graphs_after > 1) {
1414 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1415 hashsz * (ctx->num_commit_graphs_after - 1);
1416 num_chunks++;
1417 }
1418
1419 hashwrite_be32(f, GRAPH_SIGNATURE);
1420
1421 hashwrite_u8(f, GRAPH_VERSION);
1422 hashwrite_u8(f, oid_version());
1423 hashwrite_u8(f, num_chunks);
1424 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1425
1426 for (i = 0; i <= num_chunks; i++) {
1427 uint32_t chunk_write[3];
1428
1429 chunk_write[0] = htonl(chunk_ids[i]);
1430 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1431 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1432 hashwrite(f, chunk_write, 12);
1433 }
1434
1435 if (ctx->report_progress) {
1436 strbuf_addf(&progress_title,
1437 Q_("Writing out commit graph in %d pass",
1438 "Writing out commit graph in %d passes",
1439 num_chunks),
1440 num_chunks);
1441 ctx->progress = start_delayed_progress(
1442 progress_title.buf,
1443 num_chunks * ctx->commits.nr);
1444 }
1445 write_graph_chunk_fanout(f, ctx);
1446 write_graph_chunk_oids(f, hashsz, ctx);
1447 write_graph_chunk_data(f, hashsz, ctx);
1448 if (ctx->num_extra_edges)
1449 write_graph_chunk_extra_edges(f, ctx);
1450 if (ctx->num_commit_graphs_after > 1 &&
1451 write_graph_chunk_base(f, ctx)) {
1452 return -1;
1453 }
1454 stop_progress(&ctx->progress);
1455 strbuf_release(&progress_title);
1456
1457 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1458 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1459 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
1460
1461 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1462 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1463 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1464 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1465 }
1466
1467 close_commit_graph(ctx->r->objects);
1468 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1469
1470 if (ctx->split) {
1471 FILE *chainf = fdopen_lock_file(&lk, "w");
1472 char *final_graph_name;
1473 int result;
1474
1475 close(fd);
1476
1477 if (!chainf) {
1478 error(_("unable to open commit-graph chain file"));
1479 return -1;
1480 }
1481
1482 if (ctx->base_graph_name) {
1483 const char *dest = ctx->commit_graph_filenames_after[
1484 ctx->num_commit_graphs_after - 2];
1485
1486 if (strcmp(ctx->base_graph_name, dest)) {
1487 result = rename(ctx->base_graph_name, dest);
1488
1489 if (result) {
1490 error(_("failed to rename base commit-graph file"));
1491 return -1;
1492 }
1493 }
1494 } else {
1495 char *graph_name = get_commit_graph_filename(ctx->obj_dir);
1496 unlink(graph_name);
1497 }
1498
1499 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1500 final_graph_name = get_split_graph_filename(ctx->obj_dir,
1501 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1502 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1503
1504 result = rename(ctx->graph_name, final_graph_name);
1505
1506 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1507 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1508
1509 if (result) {
1510 error(_("failed to rename temporary commit-graph file"));
1511 return -1;
1512 }
1513 }
1514
1515 commit_lock_file(&lk);
1516
1517 return 0;
1518 }
1519
1520 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1521 {
1522 struct commit_graph *g = ctx->r->objects->commit_graph;
1523 uint32_t num_commits = ctx->commits.nr;
1524 uint32_t i;
1525
1526 int max_commits = 0;
1527 int size_mult = 2;
1528
1529 if (ctx->split_opts) {
1530 max_commits = ctx->split_opts->max_commits;
1531 size_mult = ctx->split_opts->size_multiple;
1532 }
1533
1534 g = ctx->r->objects->commit_graph;
1535 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1536
1537 while (g && (g->num_commits <= size_mult * num_commits ||
1538 (max_commits && num_commits > max_commits))) {
1539 if (strcmp(g->obj_dir, ctx->obj_dir))
1540 break;
1541
1542 num_commits += g->num_commits;
1543 g = g->base_graph;
1544
1545 ctx->num_commit_graphs_after--;
1546 }
1547
1548 ctx->new_base_graph = g;
1549
1550 if (ctx->num_commit_graphs_after == 2) {
1551 char *old_graph_name = get_commit_graph_filename(g->obj_dir);
1552
1553 if (!strcmp(g->filename, old_graph_name) &&
1554 strcmp(g->obj_dir, ctx->obj_dir)) {
1555 ctx->num_commit_graphs_after = 1;
1556 ctx->new_base_graph = NULL;
1557 }
1558
1559 free(old_graph_name);
1560 }
1561
1562 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1563 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1564
1565 for (i = 0; i < ctx->num_commit_graphs_after &&
1566 i < ctx->num_commit_graphs_before; i++)
1567 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1568
1569 i = ctx->num_commit_graphs_before - 1;
1570 g = ctx->r->objects->commit_graph;
1571
1572 while (g) {
1573 if (i < ctx->num_commit_graphs_after)
1574 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1575
1576 i--;
1577 g = g->base_graph;
1578 }
1579 }
1580
1581 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1582 struct commit_graph *g)
1583 {
1584 uint32_t i;
1585 uint32_t offset = g->num_commits_in_base;
1586
1587 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1588
1589 for (i = 0; i < g->num_commits; i++) {
1590 struct object_id oid;
1591 struct commit *result;
1592
1593 display_progress(ctx->progress, i + 1);
1594
1595 load_oid_from_graph(g, i + offset, &oid);
1596
1597 /* only add commits if they still exist in the repo */
1598 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1599
1600 if (result) {
1601 ctx->commits.list[ctx->commits.nr] = result;
1602 ctx->commits.nr++;
1603 }
1604 }
1605 }
1606
1607 static int commit_compare(const void *_a, const void *_b)
1608 {
1609 const struct commit *a = *(const struct commit **)_a;
1610 const struct commit *b = *(const struct commit **)_b;
1611 return oidcmp(&a->object.oid, &b->object.oid);
1612 }
1613
1614 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1615 {
1616 uint32_t i, num_parents;
1617 struct commit_list *parent;
1618
1619 if (ctx->report_progress)
1620 ctx->progress = start_delayed_progress(
1621 _("Scanning merged commits"),
1622 ctx->commits.nr);
1623
1624 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1625
1626 ctx->num_extra_edges = 0;
1627 for (i = 0; i < ctx->commits.nr; i++) {
1628 display_progress(ctx->progress, i);
1629
1630 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1631 &ctx->commits.list[i]->object.oid)) {
1632 die(_("unexpected duplicate commit id %s"),
1633 oid_to_hex(&ctx->commits.list[i]->object.oid));
1634 } else {
1635 num_parents = 0;
1636 for (parent = ctx->commits.list[i]->parents; parent; parent = parent->next)
1637 num_parents++;
1638
1639 if (num_parents > 2)
1640 ctx->num_extra_edges += num_parents - 2;
1641 }
1642 }
1643
1644 stop_progress(&ctx->progress);
1645 }
1646
1647 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1648 {
1649 struct commit_graph *g = ctx->r->objects->commit_graph;
1650 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1651 struct strbuf progress_title = STRBUF_INIT;
1652
1653 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1654 current_graph_number--;
1655
1656 if (ctx->report_progress) {
1657 strbuf_addstr(&progress_title, _("Merging commit-graph"));
1658 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1659 }
1660
1661 merge_commit_graph(ctx, g);
1662 stop_progress(&ctx->progress);
1663 strbuf_release(&progress_title);
1664
1665 g = g->base_graph;
1666 }
1667
1668 if (g) {
1669 ctx->new_base_graph = g;
1670 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1671 }
1672
1673 if (ctx->new_base_graph)
1674 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1675
1676 sort_and_scan_merged_commits(ctx);
1677 }
1678
1679 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1680 {
1681 uint32_t i;
1682 time_t now = time(NULL);
1683
1684 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1685 struct stat st;
1686 struct utimbuf updated_time;
1687
1688 stat(ctx->commit_graph_filenames_before[i], &st);
1689
1690 updated_time.actime = st.st_atime;
1691 updated_time.modtime = now;
1692 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1693 }
1694 }
1695
1696 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1697 {
1698 struct strbuf path = STRBUF_INIT;
1699 DIR *dir;
1700 struct dirent *de;
1701 size_t dirnamelen;
1702 timestamp_t expire_time = time(NULL);
1703
1704 if (ctx->split_opts && ctx->split_opts->expire_time)
1705 expire_time -= ctx->split_opts->expire_time;
1706 if (!ctx->split) {
1707 char *chain_file_name = get_chain_filename(ctx->obj_dir);
1708 unlink(chain_file_name);
1709 free(chain_file_name);
1710 ctx->num_commit_graphs_after = 0;
1711 }
1712
1713 strbuf_addstr(&path, ctx->obj_dir);
1714 strbuf_addstr(&path, "/info/commit-graphs");
1715 dir = opendir(path.buf);
1716
1717 if (!dir) {
1718 strbuf_release(&path);
1719 return;
1720 }
1721
1722 strbuf_addch(&path, '/');
1723 dirnamelen = path.len;
1724 while ((de = readdir(dir)) != NULL) {
1725 struct stat st;
1726 uint32_t i, found = 0;
1727
1728 strbuf_setlen(&path, dirnamelen);
1729 strbuf_addstr(&path, de->d_name);
1730
1731 stat(path.buf, &st);
1732
1733 if (st.st_mtime > expire_time)
1734 continue;
1735 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1736 continue;
1737
1738 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1739 if (!strcmp(ctx->commit_graph_filenames_after[i],
1740 path.buf)) {
1741 found = 1;
1742 break;
1743 }
1744 }
1745
1746 if (!found)
1747 unlink(path.buf);
1748 }
1749 }
1750
1751 int write_commit_graph(const char *obj_dir,
1752 struct string_list *pack_indexes,
1753 struct string_list *commit_hex,
1754 enum commit_graph_write_flags flags,
1755 const struct split_commit_graph_opts *split_opts)
1756 {
1757 struct write_commit_graph_context *ctx;
1758 uint32_t i, count_distinct = 0;
1759 size_t len;
1760 int res = 0;
1761
1762 if (!commit_graph_compatible(the_repository))
1763 return 0;
1764
1765 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1766 ctx->r = the_repository;
1767
1768 /* normalize object dir with no trailing slash */
1769 ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
1770 normalize_path_copy(ctx->obj_dir, obj_dir);
1771 len = strlen(ctx->obj_dir);
1772 if (len && ctx->obj_dir[len - 1] == '/')
1773 ctx->obj_dir[len - 1] = 0;
1774
1775 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1776 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1777 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
1778 ctx->split_opts = split_opts;
1779
1780 if (ctx->split) {
1781 struct commit_graph *g;
1782 prepare_commit_graph(ctx->r);
1783
1784 g = ctx->r->objects->commit_graph;
1785
1786 while (g) {
1787 ctx->num_commit_graphs_before++;
1788 g = g->base_graph;
1789 }
1790
1791 if (ctx->num_commit_graphs_before) {
1792 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1793 i = ctx->num_commit_graphs_before;
1794 g = ctx->r->objects->commit_graph;
1795
1796 while (g) {
1797 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1798 g = g->base_graph;
1799 }
1800 }
1801 }
1802
1803 ctx->approx_nr_objects = approximate_object_count();
1804 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1805
1806 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1807 ctx->oids.alloc = split_opts->max_commits;
1808
1809 if (ctx->append) {
1810 prepare_commit_graph_one(ctx->r, ctx->obj_dir);
1811 if (ctx->r->objects->commit_graph)
1812 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
1813 }
1814
1815 if (ctx->oids.alloc < 1024)
1816 ctx->oids.alloc = 1024;
1817 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1818
1819 if (ctx->append && ctx->r->objects->commit_graph) {
1820 struct commit_graph *g = ctx->r->objects->commit_graph;
1821 for (i = 0; i < g->num_commits; i++) {
1822 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1823 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
1824 }
1825 }
1826
1827 if (pack_indexes) {
1828 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1829 goto cleanup;
1830 }
1831
1832 if (commit_hex)
1833 fill_oids_from_commit_hex(ctx, commit_hex);
1834
1835 if (!pack_indexes && !commit_hex)
1836 fill_oids_from_all_packs(ctx);
1837
1838 close_reachable(ctx);
1839
1840 count_distinct = count_distinct_commits(ctx);
1841
1842 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1843 error(_("the commit graph format cannot write %d commits"), count_distinct);
1844 res = -1;
1845 goto cleanup;
1846 }
1847
1848 ctx->commits.alloc = count_distinct;
1849 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
1850
1851 copy_oids_to_commits(ctx);
1852
1853 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
1854 error(_("too many commits to write graph"));
1855 res = -1;
1856 goto cleanup;
1857 }
1858
1859 if (!ctx->commits.nr)
1860 goto cleanup;
1861
1862 if (ctx->split) {
1863 split_graph_merge_strategy(ctx);
1864
1865 merge_commit_graphs(ctx);
1866 } else
1867 ctx->num_commit_graphs_after = 1;
1868
1869 compute_generation_numbers(ctx);
1870
1871 res = write_commit_graph_file(ctx);
1872
1873 if (ctx->split)
1874 mark_commit_graphs(ctx);
1875
1876 expire_commit_graphs(ctx);
1877
1878 cleanup:
1879 free(ctx->graph_name);
1880 free(ctx->commits.list);
1881 free(ctx->oids.list);
1882 free(ctx->obj_dir);
1883
1884 if (ctx->commit_graph_filenames_after) {
1885 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1886 free(ctx->commit_graph_filenames_after[i]);
1887 free(ctx->commit_graph_hash_after[i]);
1888 }
1889
1890 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1891 free(ctx->commit_graph_filenames_before[i]);
1892
1893 free(ctx->commit_graph_filenames_after);
1894 free(ctx->commit_graph_filenames_before);
1895 free(ctx->commit_graph_hash_after);
1896 }
1897
1898 free(ctx);
1899
1900 return res;
1901 }
1902
1903 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1904 static int verify_commit_graph_error;
1905
1906 static void graph_report(const char *fmt, ...)
1907 {
1908 va_list ap;
1909
1910 verify_commit_graph_error = 1;
1911 va_start(ap, fmt);
1912 vfprintf(stderr, fmt, ap);
1913 fprintf(stderr, "\n");
1914 va_end(ap);
1915 }
1916
1917 #define GENERATION_ZERO_EXISTS 1
1918 #define GENERATION_NUMBER_EXISTS 2
1919
1920 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
1921 {
1922 uint32_t i, cur_fanout_pos = 0;
1923 struct object_id prev_oid, cur_oid, checksum;
1924 int generation_zero = 0;
1925 struct hashfile *f;
1926 int devnull;
1927 struct progress *progress = NULL;
1928 int local_error = 0;
1929
1930 if (!g) {
1931 graph_report("no commit-graph file loaded");
1932 return 1;
1933 }
1934
1935 verify_commit_graph_error = verify_commit_graph_lite(g);
1936 if (verify_commit_graph_error)
1937 return verify_commit_graph_error;
1938
1939 devnull = open("/dev/null", O_WRONLY);
1940 f = hashfd(devnull, NULL);
1941 hashwrite(f, g->data, g->data_len - g->hash_len);
1942 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1943 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1944 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1945 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1946 }
1947
1948 for (i = 0; i < g->num_commits; i++) {
1949 struct commit *graph_commit;
1950
1951 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1952
1953 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1954 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1955 oid_to_hex(&prev_oid),
1956 oid_to_hex(&cur_oid));
1957
1958 oidcpy(&prev_oid, &cur_oid);
1959
1960 while (cur_oid.hash[0] > cur_fanout_pos) {
1961 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1962
1963 if (i != fanout_value)
1964 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1965 cur_fanout_pos, fanout_value, i);
1966 cur_fanout_pos++;
1967 }
1968
1969 graph_commit = lookup_commit(r, &cur_oid);
1970 if (!parse_commit_in_graph_one(r, g, graph_commit))
1971 graph_report(_("failed to parse commit %s from commit-graph"),
1972 oid_to_hex(&cur_oid));
1973 }
1974
1975 while (cur_fanout_pos < 256) {
1976 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1977
1978 if (g->num_commits != fanout_value)
1979 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1980 cur_fanout_pos, fanout_value, i);
1981
1982 cur_fanout_pos++;
1983 }
1984
1985 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1986 return verify_commit_graph_error;
1987
1988 progress = start_progress(_("Verifying commits in commit graph"),
1989 g->num_commits);
1990 for (i = 0; i < g->num_commits; i++) {
1991 struct commit *graph_commit, *odb_commit;
1992 struct commit_list *graph_parents, *odb_parents;
1993 uint32_t max_generation = 0;
1994
1995 display_progress(progress, i + 1);
1996 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1997
1998 graph_commit = lookup_commit(r, &cur_oid);
1999 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2000 if (parse_commit_internal(odb_commit, 0, 0)) {
2001 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2002 oid_to_hex(&cur_oid));
2003 continue;
2004 }
2005
2006 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2007 get_commit_tree_oid(odb_commit)))
2008 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2009 oid_to_hex(&cur_oid),
2010 oid_to_hex(get_commit_tree_oid(graph_commit)),
2011 oid_to_hex(get_commit_tree_oid(odb_commit)));
2012
2013 graph_parents = graph_commit->parents;
2014 odb_parents = odb_commit->parents;
2015
2016 while (graph_parents) {
2017 if (odb_parents == NULL) {
2018 graph_report(_("commit-graph parent list for commit %s is too long"),
2019 oid_to_hex(&cur_oid));
2020 break;
2021 }
2022
2023 /* parse parent in case it is in a base graph */
2024 parse_commit_in_graph_one(r, g, graph_parents->item);
2025
2026 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2027 graph_report(_("commit-graph parent for %s is %s != %s"),
2028 oid_to_hex(&cur_oid),
2029 oid_to_hex(&graph_parents->item->object.oid),
2030 oid_to_hex(&odb_parents->item->object.oid));
2031
2032 if (graph_parents->item->generation > max_generation)
2033 max_generation = graph_parents->item->generation;
2034
2035 graph_parents = graph_parents->next;
2036 odb_parents = odb_parents->next;
2037 }
2038
2039 if (odb_parents != NULL)
2040 graph_report(_("commit-graph parent list for commit %s terminates early"),
2041 oid_to_hex(&cur_oid));
2042
2043 if (!graph_commit->generation) {
2044 if (generation_zero == GENERATION_NUMBER_EXISTS)
2045 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2046 oid_to_hex(&cur_oid));
2047 generation_zero = GENERATION_ZERO_EXISTS;
2048 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2049 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2050 oid_to_hex(&cur_oid));
2051
2052 if (generation_zero == GENERATION_ZERO_EXISTS)
2053 continue;
2054
2055 /*
2056 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2057 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2058 * extra logic in the following condition.
2059 */
2060 if (max_generation == GENERATION_NUMBER_MAX)
2061 max_generation--;
2062
2063 if (graph_commit->generation != max_generation + 1)
2064 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2065 oid_to_hex(&cur_oid),
2066 graph_commit->generation,
2067 max_generation + 1);
2068
2069 if (graph_commit->date != odb_commit->date)
2070 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2071 oid_to_hex(&cur_oid),
2072 graph_commit->date,
2073 odb_commit->date);
2074 }
2075 stop_progress(&progress);
2076
2077 local_error = verify_commit_graph_error;
2078
2079 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2080 local_error |= verify_commit_graph(r, g->base_graph, flags);
2081
2082 return local_error;
2083 }
2084
2085 void free_commit_graph(struct commit_graph *g)
2086 {
2087 if (!g)
2088 return;
2089 if (g->graph_fd >= 0) {
2090 munmap((void *)g->data, g->data_len);
2091 g->data = NULL;
2092 close(g->graph_fd);
2093 }
2094 free(g->filename);
2095 free(g);
2096 }