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 (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
218 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
221 * This repository is not configured to use commit graphs, so
222 * do not load one. (But report commit_graph_attempted anyway
223 * so that commit graph loading is not attempted again for this
228 obj_dir = r->objects->objectdir;
229 prepare_commit_graph_one(r, obj_dir);
231 for (alt = r->objects->alt_odb_list;
232 !r->objects->commit_graph && alt;
234 prepare_commit_graph_one(r, alt->path);
235 return !!r->objects->commit_graph;
238 int generation_numbers_enabled(struct repository *r)
240 uint32_t first_generation;
241 struct commit_graph *g;
242 if (!prepare_commit_graph(r))
245 g = r->objects->commit_graph;
250 first_generation = get_be32(g->chunk_commit_data +
251 g->hash_len + 8) >> 2;
253 return !!first_generation;
256 static void close_commit_graph(void)
258 free_commit_graph(the_repository->objects->commit_graph);
259 the_repository->objects->commit_graph = NULL;
262 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
264 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
265 g->chunk_oid_lookup, g->hash_len, pos);
268 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
270 struct commit_list **pptr)
273 struct object_id oid;
275 if (pos >= g->num_commits)
276 die("invalid parent position %"PRIu64, pos);
278 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
279 c = lookup_commit(the_repository, &oid);
281 die(_("could not find commit %s"), oid_to_hex(&oid));
283 return &commit_list_insert(c, pptr)->next;
286 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
288 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
289 item->graph_pos = pos;
290 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
293 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
296 uint32_t *parent_data_ptr;
297 uint64_t date_low, date_high;
298 struct commit_list **pptr;
299 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
301 item->object.parsed = 1;
302 item->graph_pos = pos;
304 item->maybe_tree = NULL;
306 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
307 date_low = get_be32(commit_data + g->hash_len + 12);
308 item->date = (timestamp_t)((date_high << 32) | date_low);
310 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
312 pptr = &item->parents;
314 edge_value = get_be32(commit_data + g->hash_len);
315 if (edge_value == GRAPH_PARENT_NONE)
317 pptr = insert_parent_or_die(g, edge_value, pptr);
319 edge_value = get_be32(commit_data + g->hash_len + 4);
320 if (edge_value == GRAPH_PARENT_NONE)
322 if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
323 pptr = insert_parent_or_die(g, edge_value, pptr);
327 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
328 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
330 edge_value = get_be32(parent_data_ptr);
331 pptr = insert_parent_or_die(g,
332 edge_value & GRAPH_EDGE_LAST_MASK,
335 } while (!(edge_value & GRAPH_LAST_EDGE));
340 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
342 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
343 *pos = item->graph_pos;
346 return bsearch_graph(g, &(item->object.oid), pos);
350 static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item)
354 if (item->object.parsed)
357 if (find_commit_in_graph(item, g, &pos))
358 return fill_commit_in_graph(item, g, pos);
363 int parse_commit_in_graph(struct repository *r, struct commit *item)
365 if (!prepare_commit_graph(r))
367 return parse_commit_in_graph_one(r->objects->commit_graph, item);
370 void load_commit_graph_info(struct repository *r, struct commit *item)
373 if (!prepare_commit_graph(r))
375 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
376 fill_commit_graph_info(item, r->objects->commit_graph, pos);
379 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
381 struct object_id oid;
382 const unsigned char *commit_data = g->chunk_commit_data +
383 GRAPH_DATA_WIDTH * (c->graph_pos);
385 hashcpy(oid.hash, commit_data);
386 c->maybe_tree = lookup_tree(the_repository, &oid);
388 return c->maybe_tree;
391 static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
392 const struct commit *c)
395 return c->maybe_tree;
396 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
397 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
399 return load_tree_for_commit(g, (struct commit *)c);
402 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
404 return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
407 static void write_graph_chunk_fanout(struct hashfile *f,
408 struct commit **commits,
412 struct commit **list = commits;
415 * Write the first-level table (the list is sorted,
416 * but we use a 256-entry lookup to be able to avoid
417 * having to do eight extra binary search iterations).
419 for (i = 0; i < 256; i++) {
420 while (count < nr_commits) {
421 if ((*list)->object.oid.hash[0] != i)
427 hashwrite_be32(f, count);
431 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
432 struct commit **commits, int nr_commits)
434 struct commit **list = commits;
436 for (count = 0; count < nr_commits; count++, list++)
437 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
440 static const unsigned char *commit_to_sha1(size_t index, void *table)
442 struct commit **commits = table;
443 return commits[index]->object.oid.hash;
446 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
447 struct commit **commits, int nr_commits)
449 struct commit **list = commits;
450 struct commit **last = commits + nr_commits;
451 uint32_t num_extra_edges = 0;
453 while (list < last) {
454 struct commit_list *parent;
456 uint32_t packedDate[2];
459 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
461 parent = (*list)->parents;
464 edge_value = GRAPH_PARENT_NONE;
466 edge_value = sha1_pos(parent->item->object.oid.hash,
472 edge_value = GRAPH_PARENT_MISSING;
475 hashwrite_be32(f, edge_value);
478 parent = parent->next;
481 edge_value = GRAPH_PARENT_NONE;
482 else if (parent->next)
483 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
485 edge_value = sha1_pos(parent->item->object.oid.hash,
490 edge_value = GRAPH_PARENT_MISSING;
493 hashwrite_be32(f, edge_value);
495 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
498 parent = parent->next;
502 if (sizeof((*list)->date) > 4)
503 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
507 packedDate[0] |= htonl((*list)->generation << 2);
509 packedDate[1] = htonl((*list)->date);
510 hashwrite(f, packedDate, 8);
516 static void write_graph_chunk_large_edges(struct hashfile *f,
517 struct commit **commits,
520 struct commit **list = commits;
521 struct commit **last = commits + nr_commits;
522 struct commit_list *parent;
524 while (list < last) {
526 for (parent = (*list)->parents; num_parents < 3 && parent;
527 parent = parent->next)
530 if (num_parents <= 2) {
535 /* Since num_parents > 2, this initializer is safe. */
536 for (parent = (*list)->parents->next; parent; parent = parent->next) {
537 int edge_value = sha1_pos(parent->item->object.oid.hash,
543 edge_value = GRAPH_PARENT_MISSING;
544 else if (!parent->next)
545 edge_value |= GRAPH_LAST_EDGE;
547 hashwrite_be32(f, edge_value);
554 static int commit_compare(const void *_a, const void *_b)
556 const struct object_id *a = (const struct object_id *)_a;
557 const struct object_id *b = (const struct object_id *)_b;
561 struct packed_commit_list {
562 struct commit **list;
567 struct packed_oid_list {
568 struct object_id *list;
571 struct progress *progress;
575 static int add_packed_commits(const struct object_id *oid,
576 struct packed_git *pack,
580 struct packed_oid_list *list = (struct packed_oid_list*)data;
581 enum object_type type;
582 off_t offset = nth_packed_object_offset(pack, pos);
583 struct object_info oi = OBJECT_INFO_INIT;
586 display_progress(list->progress, ++list->progress_done);
589 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
590 die(_("unable to get type of object %s"), oid_to_hex(oid));
592 if (type != OBJ_COMMIT)
595 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
596 oidcpy(&(list->list[list->nr]), oid);
602 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
604 struct commit_list *parent;
605 for (parent = commit->parents; parent; parent = parent->next) {
606 if (!(parent->item->object.flags & UNINTERESTING)) {
607 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
608 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
610 parent->item->object.flags |= UNINTERESTING;
615 static void close_reachable(struct packed_oid_list *oids, int report_progress)
618 struct commit *commit;
619 struct progress *progress = NULL;
623 progress = start_delayed_progress(
624 _("Annotating commits in commit graph"), 0);
625 for (i = 0; i < oids->nr; i++) {
626 display_progress(progress, ++j);
627 commit = lookup_commit(the_repository, &oids->list[i]);
629 commit->object.flags |= UNINTERESTING;
633 * As this loop runs, oids->nr may grow, but not more
634 * than the number of missing commits in the reachable
637 for (i = 0; i < oids->nr; i++) {
638 display_progress(progress, ++j);
639 commit = lookup_commit(the_repository, &oids->list[i]);
641 if (commit && !parse_commit(commit))
642 add_missing_parents(oids, commit);
645 for (i = 0; i < oids->nr; i++) {
646 display_progress(progress, ++j);
647 commit = lookup_commit(the_repository, &oids->list[i]);
650 commit->object.flags &= ~UNINTERESTING;
652 stop_progress(&progress);
655 static void compute_generation_numbers(struct packed_commit_list* commits,
659 struct commit_list *list = NULL;
660 struct progress *progress = NULL;
663 progress = start_progress(
664 _("Computing commit graph generation numbers"),
666 for (i = 0; i < commits->nr; i++) {
667 display_progress(progress, i + 1);
668 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
669 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
672 commit_list_insert(commits->list[i], &list);
674 struct commit *current = list->item;
675 struct commit_list *parent;
676 int all_parents_computed = 1;
677 uint32_t max_generation = 0;
679 for (parent = current->parents; parent; parent = parent->next) {
680 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
681 parent->item->generation == GENERATION_NUMBER_ZERO) {
682 all_parents_computed = 0;
683 commit_list_insert(parent->item, &list);
685 } else if (parent->item->generation > max_generation) {
686 max_generation = parent->item->generation;
690 if (all_parents_computed) {
691 current->generation = max_generation + 1;
694 if (current->generation > GENERATION_NUMBER_MAX)
695 current->generation = GENERATION_NUMBER_MAX;
699 stop_progress(&progress);
702 static int add_ref_to_list(const char *refname,
703 const struct object_id *oid,
704 int flags, void *cb_data)
706 struct string_list *list = (struct string_list *)cb_data;
708 string_list_append(list, oid_to_hex(oid));
712 void write_commit_graph_reachable(const char *obj_dir, int append,
715 struct string_list list;
717 string_list_init(&list, 1);
718 for_each_ref(add_ref_to_list, &list);
719 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
722 void write_commit_graph(const char *obj_dir,
723 struct string_list *pack_indexes,
724 struct string_list *commit_hex,
725 int append, int report_progress)
727 struct packed_oid_list oids;
728 struct packed_commit_list commits;
730 uint32_t i, count_distinct = 0;
732 struct lock_file lk = LOCK_INIT;
733 uint32_t chunk_ids[5];
734 uint64_t chunk_offsets[5];
737 struct commit_list *parent;
738 struct progress *progress = NULL;
741 oids.alloc = approximate_object_count() / 4;
742 oids.progress = NULL;
743 oids.progress_done = 0;
746 prepare_commit_graph_one(the_repository, obj_dir);
747 if (the_repository->objects->commit_graph)
748 oids.alloc += the_repository->objects->commit_graph->num_commits;
751 if (oids.alloc < 1024)
753 ALLOC_ARRAY(oids.list, oids.alloc);
755 if (append && the_repository->objects->commit_graph) {
756 struct commit_graph *commit_graph =
757 the_repository->objects->commit_graph;
758 for (i = 0; i < commit_graph->num_commits; i++) {
759 const unsigned char *hash = commit_graph->chunk_oid_lookup +
760 commit_graph->hash_len * i;
761 hashcpy(oids.list[oids.nr++].hash, hash);
766 struct strbuf packname = STRBUF_INIT;
768 strbuf_addf(&packname, "%s/pack/", obj_dir);
769 dirlen = packname.len;
770 if (report_progress) {
771 oids.progress = start_delayed_progress(
772 _("Finding commits for commit graph"), 0);
773 oids.progress_done = 0;
775 for (i = 0; i < pack_indexes->nr; i++) {
776 struct packed_git *p;
777 strbuf_setlen(&packname, dirlen);
778 strbuf_addstr(&packname, pack_indexes->items[i].string);
779 p = add_packed_git(packname.buf, packname.len, 1);
781 die(_("error adding pack %s"), packname.buf);
782 if (open_pack_index(p))
783 die(_("error opening index for %s"), packname.buf);
784 for_each_object_in_pack(p, add_packed_commits, &oids, 0);
787 stop_progress(&oids.progress);
788 strbuf_release(&packname);
793 progress = start_delayed_progress(
794 _("Finding commits for commit graph"),
796 for (i = 0; i < commit_hex->nr; i++) {
798 struct object_id oid;
799 struct commit *result;
801 display_progress(progress, i + 1);
802 if (commit_hex->items[i].string &&
803 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
806 result = lookup_commit_reference_gently(the_repository, &oid, 1);
809 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
810 oidcpy(&oids.list[oids.nr], &(result->object.oid));
814 stop_progress(&progress);
817 if (!pack_indexes && !commit_hex) {
819 oids.progress = start_delayed_progress(
820 _("Finding commits for commit graph"), 0);
821 for_each_packed_object(add_packed_commits, &oids, 0);
822 stop_progress(&oids.progress);
825 close_reachable(&oids, report_progress);
827 QSORT(oids.list, oids.nr, commit_compare);
830 for (i = 1; i < oids.nr; i++) {
831 if (!oideq(&oids.list[i - 1], &oids.list[i]))
835 if (count_distinct >= GRAPH_PARENT_MISSING)
836 die(_("the commit graph format cannot write %d commits"), count_distinct);
839 commits.alloc = count_distinct;
840 ALLOC_ARRAY(commits.list, commits.alloc);
843 for (i = 0; i < oids.nr; i++) {
845 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
848 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
849 parse_commit(commits.list[commits.nr]);
851 for (parent = commits.list[commits.nr]->parents;
852 parent; parent = parent->next)
856 num_extra_edges += num_parents - 1;
860 num_chunks = num_extra_edges ? 4 : 3;
862 if (commits.nr >= GRAPH_PARENT_MISSING)
863 die(_("too many commits to write graph"));
865 compute_generation_numbers(&commits, report_progress);
867 graph_name = get_commit_graph_filename(obj_dir);
868 if (safe_create_leading_directories(graph_name))
869 die_errno(_("unable to create leading directories of %s"),
872 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
873 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
875 hashwrite_be32(f, GRAPH_SIGNATURE);
877 hashwrite_u8(f, GRAPH_VERSION);
878 hashwrite_u8(f, GRAPH_OID_VERSION);
879 hashwrite_u8(f, num_chunks);
880 hashwrite_u8(f, 0); /* unused padding byte */
882 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
883 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
884 chunk_ids[2] = GRAPH_CHUNKID_DATA;
886 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
891 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
892 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
893 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
894 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
895 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
897 for (i = 0; i <= num_chunks; i++) {
898 uint32_t chunk_write[3];
900 chunk_write[0] = htonl(chunk_ids[i]);
901 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
902 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
903 hashwrite(f, chunk_write, 12);
906 write_graph_chunk_fanout(f, commits.list, commits.nr);
907 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
908 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
909 write_graph_chunk_large_edges(f, commits.list, commits.nr);
911 close_commit_graph();
912 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
913 commit_lock_file(&lk);
920 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
921 static int verify_commit_graph_error;
923 static void graph_report(const char *fmt, ...)
927 verify_commit_graph_error = 1;
929 vfprintf(stderr, fmt, ap);
930 fprintf(stderr, "\n");
934 #define GENERATION_ZERO_EXISTS 1
935 #define GENERATION_NUMBER_EXISTS 2
937 int verify_commit_graph(struct repository *r, struct commit_graph *g)
939 uint32_t i, cur_fanout_pos = 0;
940 struct object_id prev_oid, cur_oid, checksum;
941 int generation_zero = 0;
944 struct progress *progress = NULL;
947 graph_report("no commit-graph file loaded");
951 verify_commit_graph_error = 0;
953 if (!g->chunk_oid_fanout)
954 graph_report("commit-graph is missing the OID Fanout chunk");
955 if (!g->chunk_oid_lookup)
956 graph_report("commit-graph is missing the OID Lookup chunk");
957 if (!g->chunk_commit_data)
958 graph_report("commit-graph is missing the Commit Data chunk");
960 if (verify_commit_graph_error)
961 return verify_commit_graph_error;
963 devnull = open("/dev/null", O_WRONLY);
964 f = hashfd(devnull, NULL);
965 hashwrite(f, g->data, g->data_len - g->hash_len);
966 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
967 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
968 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
969 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
972 for (i = 0; i < g->num_commits; i++) {
973 struct commit *graph_commit;
975 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
977 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
978 graph_report("commit-graph has incorrect OID order: %s then %s",
979 oid_to_hex(&prev_oid),
980 oid_to_hex(&cur_oid));
982 oidcpy(&prev_oid, &cur_oid);
984 while (cur_oid.hash[0] > cur_fanout_pos) {
985 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
987 if (i != fanout_value)
988 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
989 cur_fanout_pos, fanout_value, i);
993 graph_commit = lookup_commit(r, &cur_oid);
994 if (!parse_commit_in_graph_one(g, graph_commit))
995 graph_report("failed to parse %s from commit-graph",
996 oid_to_hex(&cur_oid));
999 while (cur_fanout_pos < 256) {
1000 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1002 if (g->num_commits != fanout_value)
1003 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1004 cur_fanout_pos, fanout_value, i);
1009 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1010 return verify_commit_graph_error;
1012 progress = start_progress(_("Verifying commits in commit graph"),
1014 for (i = 0; i < g->num_commits; i++) {
1015 struct commit *graph_commit, *odb_commit;
1016 struct commit_list *graph_parents, *odb_parents;
1017 uint32_t max_generation = 0;
1019 display_progress(progress, i + 1);
1020 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1022 graph_commit = lookup_commit(r, &cur_oid);
1023 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1024 if (parse_commit_internal(odb_commit, 0, 0)) {
1025 graph_report("failed to parse %s from object database",
1026 oid_to_hex(&cur_oid));
1030 if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
1031 get_commit_tree_oid(odb_commit)))
1032 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1033 oid_to_hex(&cur_oid),
1034 oid_to_hex(get_commit_tree_oid(graph_commit)),
1035 oid_to_hex(get_commit_tree_oid(odb_commit)));
1037 graph_parents = graph_commit->parents;
1038 odb_parents = odb_commit->parents;
1040 while (graph_parents) {
1041 if (odb_parents == NULL) {
1042 graph_report("commit-graph parent list for commit %s is too long",
1043 oid_to_hex(&cur_oid));
1047 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1048 graph_report("commit-graph parent for %s is %s != %s",
1049 oid_to_hex(&cur_oid),
1050 oid_to_hex(&graph_parents->item->object.oid),
1051 oid_to_hex(&odb_parents->item->object.oid));
1053 if (graph_parents->item->generation > max_generation)
1054 max_generation = graph_parents->item->generation;
1056 graph_parents = graph_parents->next;
1057 odb_parents = odb_parents->next;
1060 if (odb_parents != NULL)
1061 graph_report("commit-graph parent list for commit %s terminates early",
1062 oid_to_hex(&cur_oid));
1064 if (!graph_commit->generation) {
1065 if (generation_zero == GENERATION_NUMBER_EXISTS)
1066 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1067 oid_to_hex(&cur_oid));
1068 generation_zero = GENERATION_ZERO_EXISTS;
1069 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1070 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1071 oid_to_hex(&cur_oid));
1073 if (generation_zero == GENERATION_ZERO_EXISTS)
1077 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1078 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1079 * extra logic in the following condition.
1081 if (max_generation == GENERATION_NUMBER_MAX)
1084 if (graph_commit->generation != max_generation + 1)
1085 graph_report("commit-graph generation for commit %s is %u != %u",
1086 oid_to_hex(&cur_oid),
1087 graph_commit->generation,
1088 max_generation + 1);
1090 if (graph_commit->date != odb_commit->date)
1091 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1092 oid_to_hex(&cur_oid),
1096 stop_progress(&progress);
1098 return verify_commit_graph_error;
1101 void free_commit_graph(struct commit_graph *g)
1105 if (g->graph_fd >= 0) {
1106 munmap((void *)g->data, g->data_len);