4 #include "git-compat-util.h"
12 #include "sha1-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
18 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
19 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
20 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
21 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
22 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
24 #define GRAPH_DATA_WIDTH 36
26 #define GRAPH_VERSION_1 0x1
27 #define GRAPH_VERSION GRAPH_VERSION_1
29 #define GRAPH_OID_VERSION_SHA1 1
30 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
31 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
32 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
34 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
35 #define GRAPH_PARENT_MISSING 0x7fffffff
36 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
37 #define GRAPH_PARENT_NONE 0x70000000
39 #define GRAPH_LAST_EDGE 0x80000000
41 #define GRAPH_HEADER_SIZE 8
42 #define GRAPH_FANOUT_SIZE (4 * 256)
43 #define GRAPH_CHUNKLOOKUP_WIDTH 12
44 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
45 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
47 char *get_commit_graph_filename(const char *obj_dir)
49 return xstrfmt("%s/info/commit-graph", obj_dir);
52 static struct commit_graph *alloc_commit_graph(void)
54 struct commit_graph *g = xcalloc(1, sizeof(*g));
60 struct commit_graph *load_commit_graph_one(const char *graph_file)
63 const unsigned char *data, *chunk_lookup;
67 struct commit_graph *graph;
68 int fd = git_open(graph_file);
69 uint64_t last_chunk_offset;
70 uint32_t last_chunk_id;
71 uint32_t graph_signature;
72 unsigned char graph_version, hash_version;
80 graph_size = xsize_t(st.st_size);
82 if (graph_size < GRAPH_MIN_SIZE) {
84 die(_("graph file %s is too small"), graph_file);
86 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
87 data = (const unsigned char *)graph_map;
89 graph_signature = get_be32(data);
90 if (graph_signature != GRAPH_SIGNATURE) {
91 error(_("graph signature %X does not match signature %X"),
92 graph_signature, GRAPH_SIGNATURE);
96 graph_version = *(unsigned char*)(data + 4);
97 if (graph_version != GRAPH_VERSION) {
98 error(_("graph version %X does not match version %X"),
99 graph_version, GRAPH_VERSION);
103 hash_version = *(unsigned char*)(data + 5);
104 if (hash_version != GRAPH_OID_VERSION) {
105 error(_("hash version %X does not match version %X"),
106 hash_version, GRAPH_OID_VERSION);
110 graph = alloc_commit_graph();
112 graph->hash_len = GRAPH_OID_LEN;
113 graph->num_chunks = *(unsigned char*)(data + 6);
114 graph->graph_fd = fd;
115 graph->data = graph_map;
116 graph->data_len = graph_size;
119 last_chunk_offset = 8;
120 chunk_lookup = data + 8;
121 for (i = 0; i < graph->num_chunks; i++) {
122 uint32_t chunk_id = get_be32(chunk_lookup + 0);
123 uint64_t chunk_offset = get_be64(chunk_lookup + 4);
124 int chunk_repeated = 0;
126 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
128 if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
129 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
130 (uint32_t)chunk_offset);
135 case GRAPH_CHUNKID_OIDFANOUT:
136 if (graph->chunk_oid_fanout)
139 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
142 case GRAPH_CHUNKID_OIDLOOKUP:
143 if (graph->chunk_oid_lookup)
146 graph->chunk_oid_lookup = data + chunk_offset;
149 case GRAPH_CHUNKID_DATA:
150 if (graph->chunk_commit_data)
153 graph->chunk_commit_data = data + chunk_offset;
156 case GRAPH_CHUNKID_LARGEEDGES:
157 if (graph->chunk_large_edges)
160 graph->chunk_large_edges = data + chunk_offset;
164 if (chunk_repeated) {
165 error(_("chunk id %08x appears multiple times"), chunk_id);
169 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
171 graph->num_commits = (chunk_offset - last_chunk_offset)
175 last_chunk_id = chunk_id;
176 last_chunk_offset = chunk_offset;
182 munmap(graph_map, graph_size);
187 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
191 if (r->objects->commit_graph)
194 graph_name = get_commit_graph_filename(obj_dir);
195 r->objects->commit_graph =
196 load_commit_graph_one(graph_name);
198 FREE_AND_NULL(graph_name);
202 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
204 * On the first invocation, this function attemps to load the commit
205 * graph if the_repository is configured to have one.
207 static int prepare_commit_graph(struct repository *r)
209 struct alternate_object_database *alt;
213 if (r->objects->commit_graph_attempted)
214 return !!r->objects->commit_graph;
215 r->objects->commit_graph_attempted = 1;
217 if (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
220 * This repository is not configured to use commit graphs, so
221 * do not load one. (But report commit_graph_attempted anyway
222 * so that commit graph loading is not attempted again for this
227 obj_dir = r->objects->objectdir;
228 prepare_commit_graph_one(r, obj_dir);
230 for (alt = r->objects->alt_odb_list;
231 !r->objects->commit_graph && alt;
233 prepare_commit_graph_one(r, alt->path);
234 return !!r->objects->commit_graph;
237 static void close_commit_graph(void)
239 free_commit_graph(the_repository->objects->commit_graph);
240 the_repository->objects->commit_graph = NULL;
243 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
245 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
246 g->chunk_oid_lookup, g->hash_len, pos);
249 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
251 struct commit_list **pptr)
254 struct object_id oid;
256 if (pos >= g->num_commits)
257 die("invalid parent position %"PRIu64, pos);
259 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
260 c = lookup_commit(the_repository, &oid);
262 die(_("could not find commit %s"), oid_to_hex(&oid));
264 return &commit_list_insert(c, pptr)->next;
267 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
269 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
270 item->graph_pos = pos;
271 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
274 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
277 uint32_t *parent_data_ptr;
278 uint64_t date_low, date_high;
279 struct commit_list **pptr;
280 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
282 item->object.parsed = 1;
283 item->graph_pos = pos;
285 item->maybe_tree = NULL;
287 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
288 date_low = get_be32(commit_data + g->hash_len + 12);
289 item->date = (timestamp_t)((date_high << 32) | date_low);
291 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
293 pptr = &item->parents;
295 edge_value = get_be32(commit_data + g->hash_len);
296 if (edge_value == GRAPH_PARENT_NONE)
298 pptr = insert_parent_or_die(g, edge_value, pptr);
300 edge_value = get_be32(commit_data + g->hash_len + 4);
301 if (edge_value == GRAPH_PARENT_NONE)
303 if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
304 pptr = insert_parent_or_die(g, edge_value, pptr);
308 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
309 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
311 edge_value = get_be32(parent_data_ptr);
312 pptr = insert_parent_or_die(g,
313 edge_value & GRAPH_EDGE_LAST_MASK,
316 } while (!(edge_value & GRAPH_LAST_EDGE));
321 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
323 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
324 *pos = item->graph_pos;
327 return bsearch_graph(g, &(item->object.oid), pos);
331 static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item)
335 if (item->object.parsed)
338 if (find_commit_in_graph(item, g, &pos))
339 return fill_commit_in_graph(item, g, pos);
344 int parse_commit_in_graph(struct repository *r, struct commit *item)
346 if (!prepare_commit_graph(r))
348 return parse_commit_in_graph_one(r->objects->commit_graph, item);
351 void load_commit_graph_info(struct repository *r, struct commit *item)
354 if (!prepare_commit_graph(r))
356 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
357 fill_commit_graph_info(item, r->objects->commit_graph, pos);
360 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
362 struct object_id oid;
363 const unsigned char *commit_data = g->chunk_commit_data +
364 GRAPH_DATA_WIDTH * (c->graph_pos);
366 hashcpy(oid.hash, commit_data);
367 c->maybe_tree = lookup_tree(the_repository, &oid);
369 return c->maybe_tree;
372 static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
373 const struct commit *c)
376 return c->maybe_tree;
377 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
378 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
380 return load_tree_for_commit(g, (struct commit *)c);
383 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
385 return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
388 static void write_graph_chunk_fanout(struct hashfile *f,
389 struct commit **commits,
393 struct commit **list = commits;
396 * Write the first-level table (the list is sorted,
397 * but we use a 256-entry lookup to be able to avoid
398 * having to do eight extra binary search iterations).
400 for (i = 0; i < 256; i++) {
401 while (count < nr_commits) {
402 if ((*list)->object.oid.hash[0] != i)
408 hashwrite_be32(f, count);
412 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
413 struct commit **commits, int nr_commits)
415 struct commit **list = commits;
417 for (count = 0; count < nr_commits; count++, list++)
418 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
421 static const unsigned char *commit_to_sha1(size_t index, void *table)
423 struct commit **commits = table;
424 return commits[index]->object.oid.hash;
427 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
428 struct commit **commits, int nr_commits)
430 struct commit **list = commits;
431 struct commit **last = commits + nr_commits;
432 uint32_t num_extra_edges = 0;
434 while (list < last) {
435 struct commit_list *parent;
437 uint32_t packedDate[2];
440 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
442 parent = (*list)->parents;
445 edge_value = GRAPH_PARENT_NONE;
447 edge_value = sha1_pos(parent->item->object.oid.hash,
453 edge_value = GRAPH_PARENT_MISSING;
456 hashwrite_be32(f, edge_value);
459 parent = parent->next;
462 edge_value = GRAPH_PARENT_NONE;
463 else if (parent->next)
464 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
466 edge_value = sha1_pos(parent->item->object.oid.hash,
471 edge_value = GRAPH_PARENT_MISSING;
474 hashwrite_be32(f, edge_value);
476 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
479 parent = parent->next;
483 if (sizeof((*list)->date) > 4)
484 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
488 packedDate[0] |= htonl((*list)->generation << 2);
490 packedDate[1] = htonl((*list)->date);
491 hashwrite(f, packedDate, 8);
497 static void write_graph_chunk_large_edges(struct hashfile *f,
498 struct commit **commits,
501 struct commit **list = commits;
502 struct commit **last = commits + nr_commits;
503 struct commit_list *parent;
505 while (list < last) {
507 for (parent = (*list)->parents; num_parents < 3 && parent;
508 parent = parent->next)
511 if (num_parents <= 2) {
516 /* Since num_parents > 2, this initializer is safe. */
517 for (parent = (*list)->parents->next; parent; parent = parent->next) {
518 int edge_value = sha1_pos(parent->item->object.oid.hash,
524 edge_value = GRAPH_PARENT_MISSING;
525 else if (!parent->next)
526 edge_value |= GRAPH_LAST_EDGE;
528 hashwrite_be32(f, edge_value);
535 static int commit_compare(const void *_a, const void *_b)
537 const struct object_id *a = (const struct object_id *)_a;
538 const struct object_id *b = (const struct object_id *)_b;
542 struct packed_commit_list {
543 struct commit **list;
548 struct packed_oid_list {
549 struct object_id *list;
552 struct progress *progress;
556 static int add_packed_commits(const struct object_id *oid,
557 struct packed_git *pack,
561 struct packed_oid_list *list = (struct packed_oid_list*)data;
562 enum object_type type;
563 off_t offset = nth_packed_object_offset(pack, pos);
564 struct object_info oi = OBJECT_INFO_INIT;
567 display_progress(list->progress, ++list->progress_done);
570 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
571 die(_("unable to get type of object %s"), oid_to_hex(oid));
573 if (type != OBJ_COMMIT)
576 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
577 oidcpy(&(list->list[list->nr]), oid);
583 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
585 struct commit_list *parent;
586 for (parent = commit->parents; parent; parent = parent->next) {
587 if (!(parent->item->object.flags & UNINTERESTING)) {
588 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
589 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
591 parent->item->object.flags |= UNINTERESTING;
596 static void close_reachable(struct packed_oid_list *oids, int report_progress)
599 struct commit *commit;
600 struct progress *progress = NULL;
604 progress = start_delayed_progress(
605 _("Annotating commits in commit graph"), 0);
606 for (i = 0; i < oids->nr; i++) {
607 display_progress(progress, ++j);
608 commit = lookup_commit(the_repository, &oids->list[i]);
610 commit->object.flags |= UNINTERESTING;
614 * As this loop runs, oids->nr may grow, but not more
615 * than the number of missing commits in the reachable
618 for (i = 0; i < oids->nr; i++) {
619 display_progress(progress, ++j);
620 commit = lookup_commit(the_repository, &oids->list[i]);
622 if (commit && !parse_commit(commit))
623 add_missing_parents(oids, commit);
626 for (i = 0; i < oids->nr; i++) {
627 display_progress(progress, ++j);
628 commit = lookup_commit(the_repository, &oids->list[i]);
631 commit->object.flags &= ~UNINTERESTING;
633 stop_progress(&progress);
636 static void compute_generation_numbers(struct packed_commit_list* commits,
640 struct commit_list *list = NULL;
641 struct progress *progress = NULL;
644 progress = start_progress(
645 _("Computing commit graph generation numbers"),
647 for (i = 0; i < commits->nr; i++) {
648 display_progress(progress, i + 1);
649 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
650 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
653 commit_list_insert(commits->list[i], &list);
655 struct commit *current = list->item;
656 struct commit_list *parent;
657 int all_parents_computed = 1;
658 uint32_t max_generation = 0;
660 for (parent = current->parents; parent; parent = parent->next) {
661 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
662 parent->item->generation == GENERATION_NUMBER_ZERO) {
663 all_parents_computed = 0;
664 commit_list_insert(parent->item, &list);
666 } else if (parent->item->generation > max_generation) {
667 max_generation = parent->item->generation;
671 if (all_parents_computed) {
672 current->generation = max_generation + 1;
675 if (current->generation > GENERATION_NUMBER_MAX)
676 current->generation = GENERATION_NUMBER_MAX;
680 stop_progress(&progress);
683 static int add_ref_to_list(const char *refname,
684 const struct object_id *oid,
685 int flags, void *cb_data)
687 struct string_list *list = (struct string_list *)cb_data;
689 string_list_append(list, oid_to_hex(oid));
693 void write_commit_graph_reachable(const char *obj_dir, int append,
696 struct string_list list;
698 string_list_init(&list, 1);
699 for_each_ref(add_ref_to_list, &list);
700 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
703 void write_commit_graph(const char *obj_dir,
704 struct string_list *pack_indexes,
705 struct string_list *commit_hex,
706 int append, int report_progress)
708 struct packed_oid_list oids;
709 struct packed_commit_list commits;
711 uint32_t i, count_distinct = 0;
713 struct lock_file lk = LOCK_INIT;
714 uint32_t chunk_ids[5];
715 uint64_t chunk_offsets[5];
718 struct commit_list *parent;
719 struct progress *progress = NULL;
722 oids.alloc = approximate_object_count() / 4;
723 oids.progress = NULL;
724 oids.progress_done = 0;
727 prepare_commit_graph_one(the_repository, obj_dir);
728 if (the_repository->objects->commit_graph)
729 oids.alloc += the_repository->objects->commit_graph->num_commits;
732 if (oids.alloc < 1024)
734 ALLOC_ARRAY(oids.list, oids.alloc);
736 if (append && the_repository->objects->commit_graph) {
737 struct commit_graph *commit_graph =
738 the_repository->objects->commit_graph;
739 for (i = 0; i < commit_graph->num_commits; i++) {
740 const unsigned char *hash = commit_graph->chunk_oid_lookup +
741 commit_graph->hash_len * i;
742 hashcpy(oids.list[oids.nr++].hash, hash);
747 struct strbuf packname = STRBUF_INIT;
749 strbuf_addf(&packname, "%s/pack/", obj_dir);
750 dirlen = packname.len;
751 if (report_progress) {
752 oids.progress = start_delayed_progress(
753 _("Finding commits for commit graph"), 0);
754 oids.progress_done = 0;
756 for (i = 0; i < pack_indexes->nr; i++) {
757 struct packed_git *p;
758 strbuf_setlen(&packname, dirlen);
759 strbuf_addstr(&packname, pack_indexes->items[i].string);
760 p = add_packed_git(packname.buf, packname.len, 1);
762 die(_("error adding pack %s"), packname.buf);
763 if (open_pack_index(p))
764 die(_("error opening index for %s"), packname.buf);
765 for_each_object_in_pack(p, add_packed_commits, &oids, 0);
768 stop_progress(&oids.progress);
769 strbuf_release(&packname);
774 progress = start_delayed_progress(
775 _("Finding commits for commit graph"),
777 for (i = 0; i < commit_hex->nr; i++) {
779 struct object_id oid;
780 struct commit *result;
782 display_progress(progress, i + 1);
783 if (commit_hex->items[i].string &&
784 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
787 result = lookup_commit_reference_gently(the_repository, &oid, 1);
790 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
791 oidcpy(&oids.list[oids.nr], &(result->object.oid));
795 stop_progress(&progress);
798 if (!pack_indexes && !commit_hex) {
800 oids.progress = start_delayed_progress(
801 _("Finding commits for commit graph"), 0);
802 for_each_packed_object(add_packed_commits, &oids, 0);
803 stop_progress(&oids.progress);
806 close_reachable(&oids, report_progress);
808 QSORT(oids.list, oids.nr, commit_compare);
811 for (i = 1; i < oids.nr; i++) {
812 if (oidcmp(&oids.list[i-1], &oids.list[i]))
816 if (count_distinct >= GRAPH_PARENT_MISSING)
817 die(_("the commit graph format cannot write %d commits"), count_distinct);
820 commits.alloc = count_distinct;
821 ALLOC_ARRAY(commits.list, commits.alloc);
824 for (i = 0; i < oids.nr; i++) {
826 if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
829 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
830 parse_commit(commits.list[commits.nr]);
832 for (parent = commits.list[commits.nr]->parents;
833 parent; parent = parent->next)
837 num_extra_edges += num_parents - 1;
841 num_chunks = num_extra_edges ? 4 : 3;
843 if (commits.nr >= GRAPH_PARENT_MISSING)
844 die(_("too many commits to write graph"));
846 compute_generation_numbers(&commits, report_progress);
848 graph_name = get_commit_graph_filename(obj_dir);
849 if (safe_create_leading_directories(graph_name))
850 die_errno(_("unable to create leading directories of %s"),
853 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
854 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
856 hashwrite_be32(f, GRAPH_SIGNATURE);
858 hashwrite_u8(f, GRAPH_VERSION);
859 hashwrite_u8(f, GRAPH_OID_VERSION);
860 hashwrite_u8(f, num_chunks);
861 hashwrite_u8(f, 0); /* unused padding byte */
863 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
864 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
865 chunk_ids[2] = GRAPH_CHUNKID_DATA;
867 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
872 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
873 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
874 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
875 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
876 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
878 for (i = 0; i <= num_chunks; i++) {
879 uint32_t chunk_write[3];
881 chunk_write[0] = htonl(chunk_ids[i]);
882 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
883 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
884 hashwrite(f, chunk_write, 12);
887 write_graph_chunk_fanout(f, commits.list, commits.nr);
888 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
889 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
890 write_graph_chunk_large_edges(f, commits.list, commits.nr);
892 close_commit_graph();
893 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
894 commit_lock_file(&lk);
901 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
902 static int verify_commit_graph_error;
904 static void graph_report(const char *fmt, ...)
908 verify_commit_graph_error = 1;
910 vfprintf(stderr, fmt, ap);
911 fprintf(stderr, "\n");
915 #define GENERATION_ZERO_EXISTS 1
916 #define GENERATION_NUMBER_EXISTS 2
918 int verify_commit_graph(struct repository *r, struct commit_graph *g)
920 uint32_t i, cur_fanout_pos = 0;
921 struct object_id prev_oid, cur_oid, checksum;
922 int generation_zero = 0;
927 graph_report("no commit-graph file loaded");
931 verify_commit_graph_error = 0;
933 if (!g->chunk_oid_fanout)
934 graph_report("commit-graph is missing the OID Fanout chunk");
935 if (!g->chunk_oid_lookup)
936 graph_report("commit-graph is missing the OID Lookup chunk");
937 if (!g->chunk_commit_data)
938 graph_report("commit-graph is missing the Commit Data chunk");
940 if (verify_commit_graph_error)
941 return verify_commit_graph_error;
943 devnull = open("/dev/null", O_WRONLY);
944 f = hashfd(devnull, NULL);
945 hashwrite(f, g->data, g->data_len - g->hash_len);
946 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
947 if (hashcmp(checksum.hash, g->data + g->data_len - g->hash_len)) {
948 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
949 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
952 for (i = 0; i < g->num_commits; i++) {
953 struct commit *graph_commit;
955 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
957 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
958 graph_report("commit-graph has incorrect OID order: %s then %s",
959 oid_to_hex(&prev_oid),
960 oid_to_hex(&cur_oid));
962 oidcpy(&prev_oid, &cur_oid);
964 while (cur_oid.hash[0] > cur_fanout_pos) {
965 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
967 if (i != fanout_value)
968 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
969 cur_fanout_pos, fanout_value, i);
973 graph_commit = lookup_commit(r, &cur_oid);
974 if (!parse_commit_in_graph_one(g, graph_commit))
975 graph_report("failed to parse %s from commit-graph",
976 oid_to_hex(&cur_oid));
979 while (cur_fanout_pos < 256) {
980 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
982 if (g->num_commits != fanout_value)
983 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
984 cur_fanout_pos, fanout_value, i);
989 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
990 return verify_commit_graph_error;
992 for (i = 0; i < g->num_commits; i++) {
993 struct commit *graph_commit, *odb_commit;
994 struct commit_list *graph_parents, *odb_parents;
995 uint32_t max_generation = 0;
997 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
999 graph_commit = lookup_commit(r, &cur_oid);
1000 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1001 if (parse_commit_internal(odb_commit, 0, 0)) {
1002 graph_report("failed to parse %s from object database",
1003 oid_to_hex(&cur_oid));
1007 if (oidcmp(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
1008 get_commit_tree_oid(odb_commit)))
1009 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1010 oid_to_hex(&cur_oid),
1011 oid_to_hex(get_commit_tree_oid(graph_commit)),
1012 oid_to_hex(get_commit_tree_oid(odb_commit)));
1014 graph_parents = graph_commit->parents;
1015 odb_parents = odb_commit->parents;
1017 while (graph_parents) {
1018 if (odb_parents == NULL) {
1019 graph_report("commit-graph parent list for commit %s is too long",
1020 oid_to_hex(&cur_oid));
1024 if (oidcmp(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1025 graph_report("commit-graph parent for %s is %s != %s",
1026 oid_to_hex(&cur_oid),
1027 oid_to_hex(&graph_parents->item->object.oid),
1028 oid_to_hex(&odb_parents->item->object.oid));
1030 if (graph_parents->item->generation > max_generation)
1031 max_generation = graph_parents->item->generation;
1033 graph_parents = graph_parents->next;
1034 odb_parents = odb_parents->next;
1037 if (odb_parents != NULL)
1038 graph_report("commit-graph parent list for commit %s terminates early",
1039 oid_to_hex(&cur_oid));
1041 if (!graph_commit->generation) {
1042 if (generation_zero == GENERATION_NUMBER_EXISTS)
1043 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1044 oid_to_hex(&cur_oid));
1045 generation_zero = GENERATION_ZERO_EXISTS;
1046 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1047 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1048 oid_to_hex(&cur_oid));
1050 if (generation_zero == GENERATION_ZERO_EXISTS)
1054 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1055 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1056 * extra logic in the following condition.
1058 if (max_generation == GENERATION_NUMBER_MAX)
1061 if (graph_commit->generation != max_generation + 1)
1062 graph_report("commit-graph generation for commit %s is %u != %u",
1063 oid_to_hex(&cur_oid),
1064 graph_commit->generation,
1065 max_generation + 1);
1067 if (graph_commit->date != odb_commit->date)
1068 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1069 oid_to_hex(&cur_oid),
1074 return verify_commit_graph_error;
1077 void free_commit_graph(struct commit_graph *g)
1081 if (g->graph_fd >= 0) {
1082 munmap((void *)g->data, g->data_len);