4 #include "git-compat-util.h"
12 #include "sha1-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
17 #include "replace-object.h"
19 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
20 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
21 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
22 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
23 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
25 #define GRAPH_DATA_WIDTH 36
27 #define GRAPH_VERSION_1 0x1
28 #define GRAPH_VERSION GRAPH_VERSION_1
30 #define GRAPH_OID_VERSION_SHA1 1
31 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
32 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
33 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
35 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
36 #define GRAPH_PARENT_MISSING 0x7fffffff
37 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
38 #define GRAPH_PARENT_NONE 0x70000000
40 #define GRAPH_LAST_EDGE 0x80000000
42 #define GRAPH_HEADER_SIZE 8
43 #define GRAPH_FANOUT_SIZE (4 * 256)
44 #define GRAPH_CHUNKLOOKUP_WIDTH 12
45 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
46 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
48 char *get_commit_graph_filename(const char *obj_dir)
50 return xstrfmt("%s/info/commit-graph", obj_dir);
53 static struct commit_graph *alloc_commit_graph(void)
55 struct commit_graph *g = xcalloc(1, sizeof(*g));
61 extern int read_replace_refs;
63 static int commit_graph_compatible(struct repository *r)
65 if (read_replace_refs) {
66 prepare_replace_object(r);
67 if (hashmap_get_size(&r->objects->replace_map->map))
71 prepare_commit_graft(r);
72 if (r->parsed_objects && r->parsed_objects->grafts_nr)
74 if (is_repository_shallow(r))
80 struct commit_graph *load_commit_graph_one(const char *graph_file)
83 const unsigned char *data, *chunk_lookup;
87 struct commit_graph *graph;
88 int fd = git_open(graph_file);
89 uint64_t last_chunk_offset;
90 uint32_t last_chunk_id;
91 uint32_t graph_signature;
92 unsigned char graph_version, hash_version;
100 graph_size = xsize_t(st.st_size);
102 if (graph_size < GRAPH_MIN_SIZE) {
104 die("graph file %s is too small", graph_file);
106 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
107 data = (const unsigned char *)graph_map;
109 graph_signature = get_be32(data);
110 if (graph_signature != GRAPH_SIGNATURE) {
111 error("graph signature %X does not match signature %X",
112 graph_signature, GRAPH_SIGNATURE);
116 graph_version = *(unsigned char*)(data + 4);
117 if (graph_version != GRAPH_VERSION) {
118 error("graph version %X does not match version %X",
119 graph_version, GRAPH_VERSION);
123 hash_version = *(unsigned char*)(data + 5);
124 if (hash_version != GRAPH_OID_VERSION) {
125 error("hash version %X does not match version %X",
126 hash_version, GRAPH_OID_VERSION);
130 graph = alloc_commit_graph();
132 graph->hash_len = GRAPH_OID_LEN;
133 graph->num_chunks = *(unsigned char*)(data + 6);
134 graph->graph_fd = fd;
135 graph->data = graph_map;
136 graph->data_len = graph_size;
139 last_chunk_offset = 8;
140 chunk_lookup = data + 8;
141 for (i = 0; i < graph->num_chunks; i++) {
142 uint32_t chunk_id = get_be32(chunk_lookup + 0);
143 uint64_t chunk_offset = get_be64(chunk_lookup + 4);
144 int chunk_repeated = 0;
146 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
148 if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
149 error("improper chunk offset %08x%08x", (uint32_t)(chunk_offset >> 32),
150 (uint32_t)chunk_offset);
155 case GRAPH_CHUNKID_OIDFANOUT:
156 if (graph->chunk_oid_fanout)
159 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
162 case GRAPH_CHUNKID_OIDLOOKUP:
163 if (graph->chunk_oid_lookup)
166 graph->chunk_oid_lookup = data + chunk_offset;
169 case GRAPH_CHUNKID_DATA:
170 if (graph->chunk_commit_data)
173 graph->chunk_commit_data = data + chunk_offset;
176 case GRAPH_CHUNKID_LARGEEDGES:
177 if (graph->chunk_large_edges)
180 graph->chunk_large_edges = data + chunk_offset;
184 if (chunk_repeated) {
185 error("chunk id %08x appears multiple times", chunk_id);
189 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
191 graph->num_commits = (chunk_offset - last_chunk_offset)
195 last_chunk_id = chunk_id;
196 last_chunk_offset = chunk_offset;
202 munmap(graph_map, graph_size);
207 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
211 if (r->objects->commit_graph)
214 graph_name = get_commit_graph_filename(obj_dir);
215 r->objects->commit_graph =
216 load_commit_graph_one(graph_name);
218 FREE_AND_NULL(graph_name);
222 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
224 * On the first invocation, this function attemps to load the commit
225 * graph if the_repository is configured to have one.
227 static int prepare_commit_graph(struct repository *r)
229 struct alternate_object_database *alt;
233 if (r->objects->commit_graph_attempted)
234 return !!r->objects->commit_graph;
235 r->objects->commit_graph_attempted = 1;
237 if (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
240 * This repository is not configured to use commit graphs, so
241 * do not load one. (But report commit_graph_attempted anyway
242 * so that commit graph loading is not attempted again for this
247 if (!commit_graph_compatible(r))
250 obj_dir = r->objects->objectdir;
251 prepare_commit_graph_one(r, obj_dir);
253 for (alt = r->objects->alt_odb_list;
254 !r->objects->commit_graph && alt;
256 prepare_commit_graph_one(r, alt->path);
257 return !!r->objects->commit_graph;
260 static void close_commit_graph(void)
262 free_commit_graph(the_repository->objects->commit_graph);
263 the_repository->objects->commit_graph = NULL;
266 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
268 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
269 g->chunk_oid_lookup, g->hash_len, pos);
272 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
274 struct commit_list **pptr)
277 struct object_id oid;
279 if (pos >= g->num_commits)
280 die("invalid parent position %"PRIu64, pos);
282 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
283 c = lookup_commit(the_repository, &oid);
285 die("could not find commit %s", oid_to_hex(&oid));
287 return &commit_list_insert(c, pptr)->next;
290 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
292 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
293 item->graph_pos = pos;
294 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
297 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
300 uint32_t *parent_data_ptr;
301 uint64_t date_low, date_high;
302 struct commit_list **pptr;
303 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
305 item->object.parsed = 1;
306 item->graph_pos = pos;
308 item->maybe_tree = NULL;
310 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
311 date_low = get_be32(commit_data + g->hash_len + 12);
312 item->date = (timestamp_t)((date_high << 32) | date_low);
314 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
316 pptr = &item->parents;
318 edge_value = get_be32(commit_data + g->hash_len);
319 if (edge_value == GRAPH_PARENT_NONE)
321 pptr = insert_parent_or_die(g, edge_value, pptr);
323 edge_value = get_be32(commit_data + g->hash_len + 4);
324 if (edge_value == GRAPH_PARENT_NONE)
326 if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
327 pptr = insert_parent_or_die(g, edge_value, pptr);
331 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
332 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
334 edge_value = get_be32(parent_data_ptr);
335 pptr = insert_parent_or_die(g,
336 edge_value & GRAPH_EDGE_LAST_MASK,
339 } while (!(edge_value & GRAPH_LAST_EDGE));
344 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
346 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
347 *pos = item->graph_pos;
350 return bsearch_graph(g, &(item->object.oid), pos);
354 static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item)
358 if (item->object.parsed)
361 if (find_commit_in_graph(item, g, &pos))
362 return fill_commit_in_graph(item, g, pos);
367 int parse_commit_in_graph(struct repository *r, struct commit *item)
369 if (!prepare_commit_graph(r))
371 return parse_commit_in_graph_one(r->objects->commit_graph, item);
374 void load_commit_graph_info(struct repository *r, struct commit *item)
377 if (!prepare_commit_graph(r))
379 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
380 fill_commit_graph_info(item, r->objects->commit_graph, pos);
383 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
385 struct object_id oid;
386 const unsigned char *commit_data = g->chunk_commit_data +
387 GRAPH_DATA_WIDTH * (c->graph_pos);
389 hashcpy(oid.hash, commit_data);
390 c->maybe_tree = lookup_tree(the_repository, &oid);
392 return c->maybe_tree;
395 static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
396 const struct commit *c)
399 return c->maybe_tree;
400 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
401 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
403 return load_tree_for_commit(g, (struct commit *)c);
406 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
408 return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
411 static void write_graph_chunk_fanout(struct hashfile *f,
412 struct commit **commits,
416 struct commit **list = commits;
419 * Write the first-level table (the list is sorted,
420 * but we use a 256-entry lookup to be able to avoid
421 * having to do eight extra binary search iterations).
423 for (i = 0; i < 256; i++) {
424 while (count < nr_commits) {
425 if ((*list)->object.oid.hash[0] != i)
431 hashwrite_be32(f, count);
435 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
436 struct commit **commits, int nr_commits)
438 struct commit **list = commits;
440 for (count = 0; count < nr_commits; count++, list++)
441 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
444 static const unsigned char *commit_to_sha1(size_t index, void *table)
446 struct commit **commits = table;
447 return commits[index]->object.oid.hash;
450 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
451 struct commit **commits, int nr_commits)
453 struct commit **list = commits;
454 struct commit **last = commits + nr_commits;
455 uint32_t num_extra_edges = 0;
457 while (list < last) {
458 struct commit_list *parent;
460 uint32_t packedDate[2];
463 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
465 parent = (*list)->parents;
468 edge_value = GRAPH_PARENT_NONE;
470 edge_value = sha1_pos(parent->item->object.oid.hash,
476 edge_value = GRAPH_PARENT_MISSING;
479 hashwrite_be32(f, edge_value);
482 parent = parent->next;
485 edge_value = GRAPH_PARENT_NONE;
486 else if (parent->next)
487 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
489 edge_value = sha1_pos(parent->item->object.oid.hash,
494 edge_value = GRAPH_PARENT_MISSING;
497 hashwrite_be32(f, edge_value);
499 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
502 parent = parent->next;
506 if (sizeof((*list)->date) > 4)
507 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
511 packedDate[0] |= htonl((*list)->generation << 2);
513 packedDate[1] = htonl((*list)->date);
514 hashwrite(f, packedDate, 8);
520 static void write_graph_chunk_large_edges(struct hashfile *f,
521 struct commit **commits,
524 struct commit **list = commits;
525 struct commit **last = commits + nr_commits;
526 struct commit_list *parent;
528 while (list < last) {
530 for (parent = (*list)->parents; num_parents < 3 && parent;
531 parent = parent->next)
534 if (num_parents <= 2) {
539 /* Since num_parents > 2, this initializer is safe. */
540 for (parent = (*list)->parents->next; parent; parent = parent->next) {
541 int edge_value = sha1_pos(parent->item->object.oid.hash,
547 edge_value = GRAPH_PARENT_MISSING;
548 else if (!parent->next)
549 edge_value |= GRAPH_LAST_EDGE;
551 hashwrite_be32(f, edge_value);
558 static int commit_compare(const void *_a, const void *_b)
560 const struct object_id *a = (const struct object_id *)_a;
561 const struct object_id *b = (const struct object_id *)_b;
565 struct packed_commit_list {
566 struct commit **list;
571 struct packed_oid_list {
572 struct object_id *list;
577 static int add_packed_commits(const struct object_id *oid,
578 struct packed_git *pack,
582 struct packed_oid_list *list = (struct packed_oid_list*)data;
583 enum object_type type;
584 off_t offset = nth_packed_object_offset(pack, pos);
585 struct object_info oi = OBJECT_INFO_INIT;
588 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
589 die("unable to get type of object %s", oid_to_hex(oid));
591 if (type != OBJ_COMMIT)
594 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
595 oidcpy(&(list->list[list->nr]), oid);
601 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
603 struct commit_list *parent;
604 for (parent = commit->parents; parent; parent = parent->next) {
605 if (!(parent->item->object.flags & UNINTERESTING)) {
606 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
607 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
609 parent->item->object.flags |= UNINTERESTING;
614 static void close_reachable(struct packed_oid_list *oids)
617 struct commit *commit;
619 for (i = 0; i < oids->nr; i++) {
620 commit = lookup_commit(the_repository, &oids->list[i]);
622 commit->object.flags |= UNINTERESTING;
626 * As this loop runs, oids->nr may grow, but not more
627 * than the number of missing commits in the reachable
630 for (i = 0; i < oids->nr; i++) {
631 commit = lookup_commit(the_repository, &oids->list[i]);
633 if (commit && !parse_commit(commit))
634 add_missing_parents(oids, commit);
637 for (i = 0; i < oids->nr; i++) {
638 commit = lookup_commit(the_repository, &oids->list[i]);
641 commit->object.flags &= ~UNINTERESTING;
645 static void compute_generation_numbers(struct packed_commit_list* commits)
648 struct commit_list *list = NULL;
650 for (i = 0; i < commits->nr; i++) {
651 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
652 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
655 commit_list_insert(commits->list[i], &list);
657 struct commit *current = list->item;
658 struct commit_list *parent;
659 int all_parents_computed = 1;
660 uint32_t max_generation = 0;
662 for (parent = current->parents; parent; parent = parent->next) {
663 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
664 parent->item->generation == GENERATION_NUMBER_ZERO) {
665 all_parents_computed = 0;
666 commit_list_insert(parent->item, &list);
668 } else if (parent->item->generation > max_generation) {
669 max_generation = parent->item->generation;
673 if (all_parents_computed) {
674 current->generation = max_generation + 1;
677 if (current->generation > GENERATION_NUMBER_MAX)
678 current->generation = GENERATION_NUMBER_MAX;
684 static int add_ref_to_list(const char *refname,
685 const struct object_id *oid,
686 int flags, void *cb_data)
688 struct string_list *list = (struct string_list *)cb_data;
690 string_list_append(list, oid_to_hex(oid));
694 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);
703 void write_commit_graph(const char *obj_dir,
704 struct string_list *pack_indexes,
705 struct string_list *commit_hex,
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;
720 if (!commit_graph_compatible(the_repository))
724 oids.alloc = approximate_object_count() / 4;
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 for (i = 0; i < pack_indexes->nr; i++) {
752 struct packed_git *p;
753 strbuf_setlen(&packname, dirlen);
754 strbuf_addstr(&packname, pack_indexes->items[i].string);
755 p = add_packed_git(packname.buf, packname.len, 1);
757 die("error adding pack %s", packname.buf);
758 if (open_pack_index(p))
759 die("error opening index for %s", packname.buf);
760 for_each_object_in_pack(p, add_packed_commits, &oids);
763 strbuf_release(&packname);
767 for (i = 0; i < commit_hex->nr; i++) {
769 struct object_id oid;
770 struct commit *result;
772 if (commit_hex->items[i].string &&
773 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
776 result = lookup_commit_reference_gently(the_repository, &oid, 1);
779 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
780 oidcpy(&oids.list[oids.nr], &(result->object.oid));
786 if (!pack_indexes && !commit_hex)
787 for_each_packed_object(add_packed_commits, &oids, 0);
789 close_reachable(&oids);
791 QSORT(oids.list, oids.nr, commit_compare);
794 for (i = 1; i < oids.nr; i++) {
795 if (oidcmp(&oids.list[i-1], &oids.list[i]))
799 if (count_distinct >= GRAPH_PARENT_MISSING)
800 die(_("the commit graph format cannot write %d commits"), count_distinct);
803 commits.alloc = count_distinct;
804 ALLOC_ARRAY(commits.list, commits.alloc);
807 for (i = 0; i < oids.nr; i++) {
809 if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
812 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
813 parse_commit(commits.list[commits.nr]);
815 for (parent = commits.list[commits.nr]->parents;
816 parent; parent = parent->next)
820 num_extra_edges += num_parents - 1;
824 num_chunks = num_extra_edges ? 4 : 3;
826 if (commits.nr >= GRAPH_PARENT_MISSING)
827 die(_("too many commits to write graph"));
829 compute_generation_numbers(&commits);
831 graph_name = get_commit_graph_filename(obj_dir);
832 if (safe_create_leading_directories(graph_name))
833 die_errno(_("unable to create leading directories of %s"),
836 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
837 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
839 hashwrite_be32(f, GRAPH_SIGNATURE);
841 hashwrite_u8(f, GRAPH_VERSION);
842 hashwrite_u8(f, GRAPH_OID_VERSION);
843 hashwrite_u8(f, num_chunks);
844 hashwrite_u8(f, 0); /* unused padding byte */
846 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
847 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
848 chunk_ids[2] = GRAPH_CHUNKID_DATA;
850 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
855 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
856 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
857 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
858 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
859 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
861 for (i = 0; i <= num_chunks; i++) {
862 uint32_t chunk_write[3];
864 chunk_write[0] = htonl(chunk_ids[i]);
865 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
866 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
867 hashwrite(f, chunk_write, 12);
870 write_graph_chunk_fanout(f, commits.list, commits.nr);
871 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
872 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
873 write_graph_chunk_large_edges(f, commits.list, commits.nr);
875 close_commit_graph();
876 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
877 commit_lock_file(&lk);
884 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
885 static int verify_commit_graph_error;
887 static void graph_report(const char *fmt, ...)
891 verify_commit_graph_error = 1;
893 vfprintf(stderr, fmt, ap);
894 fprintf(stderr, "\n");
898 #define GENERATION_ZERO_EXISTS 1
899 #define GENERATION_NUMBER_EXISTS 2
901 int verify_commit_graph(struct repository *r, struct commit_graph *g)
903 uint32_t i, cur_fanout_pos = 0;
904 struct object_id prev_oid, cur_oid, checksum;
905 int generation_zero = 0;
910 graph_report("no commit-graph file loaded");
914 verify_commit_graph_error = 0;
916 if (!g->chunk_oid_fanout)
917 graph_report("commit-graph is missing the OID Fanout chunk");
918 if (!g->chunk_oid_lookup)
919 graph_report("commit-graph is missing the OID Lookup chunk");
920 if (!g->chunk_commit_data)
921 graph_report("commit-graph is missing the Commit Data chunk");
923 if (verify_commit_graph_error)
924 return verify_commit_graph_error;
926 devnull = open("/dev/null", O_WRONLY);
927 f = hashfd(devnull, NULL);
928 hashwrite(f, g->data, g->data_len - g->hash_len);
929 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
930 if (hashcmp(checksum.hash, g->data + g->data_len - g->hash_len)) {
931 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
932 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
935 for (i = 0; i < g->num_commits; i++) {
936 struct commit *graph_commit;
938 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
940 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
941 graph_report("commit-graph has incorrect OID order: %s then %s",
942 oid_to_hex(&prev_oid),
943 oid_to_hex(&cur_oid));
945 oidcpy(&prev_oid, &cur_oid);
947 while (cur_oid.hash[0] > cur_fanout_pos) {
948 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
950 if (i != fanout_value)
951 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
952 cur_fanout_pos, fanout_value, i);
956 graph_commit = lookup_commit(r, &cur_oid);
957 if (!parse_commit_in_graph_one(g, graph_commit))
958 graph_report("failed to parse %s from commit-graph",
959 oid_to_hex(&cur_oid));
962 while (cur_fanout_pos < 256) {
963 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
965 if (g->num_commits != fanout_value)
966 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
967 cur_fanout_pos, fanout_value, i);
972 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
973 return verify_commit_graph_error;
975 for (i = 0; i < g->num_commits; i++) {
976 struct commit *graph_commit, *odb_commit;
977 struct commit_list *graph_parents, *odb_parents;
978 uint32_t max_generation = 0;
980 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
982 graph_commit = lookup_commit(r, &cur_oid);
983 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
984 if (parse_commit_internal(odb_commit, 0, 0)) {
985 graph_report("failed to parse %s from object database",
986 oid_to_hex(&cur_oid));
990 if (oidcmp(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
991 get_commit_tree_oid(odb_commit)))
992 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
993 oid_to_hex(&cur_oid),
994 oid_to_hex(get_commit_tree_oid(graph_commit)),
995 oid_to_hex(get_commit_tree_oid(odb_commit)));
997 graph_parents = graph_commit->parents;
998 odb_parents = odb_commit->parents;
1000 while (graph_parents) {
1001 if (odb_parents == NULL) {
1002 graph_report("commit-graph parent list for commit %s is too long",
1003 oid_to_hex(&cur_oid));
1007 if (oidcmp(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1008 graph_report("commit-graph parent for %s is %s != %s",
1009 oid_to_hex(&cur_oid),
1010 oid_to_hex(&graph_parents->item->object.oid),
1011 oid_to_hex(&odb_parents->item->object.oid));
1013 if (graph_parents->item->generation > max_generation)
1014 max_generation = graph_parents->item->generation;
1016 graph_parents = graph_parents->next;
1017 odb_parents = odb_parents->next;
1020 if (odb_parents != NULL)
1021 graph_report("commit-graph parent list for commit %s terminates early",
1022 oid_to_hex(&cur_oid));
1024 if (!graph_commit->generation) {
1025 if (generation_zero == GENERATION_NUMBER_EXISTS)
1026 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1027 oid_to_hex(&cur_oid));
1028 generation_zero = GENERATION_ZERO_EXISTS;
1029 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1030 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1031 oid_to_hex(&cur_oid));
1033 if (generation_zero == GENERATION_ZERO_EXISTS)
1037 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1038 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1039 * extra logic in the following condition.
1041 if (max_generation == GENERATION_NUMBER_MAX)
1044 if (graph_commit->generation != max_generation + 1)
1045 graph_report("commit-graph generation for commit %s is %u != %u",
1046 oid_to_hex(&cur_oid),
1047 graph_commit->generation,
1048 max_generation + 1);
1050 if (graph_commit->date != odb_commit->date)
1051 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1052 oid_to_hex(&cur_oid),
1057 return verify_commit_graph_error;
1060 void free_commit_graph(struct commit_graph *g)
1064 if (g->graph_fd >= 0) {
1065 munmap((void *)g->data, g->data_len);