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"
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_LARGEEDGES 0x45444745 /* "EDGE" */
26 #define GRAPH_DATA_WIDTH 36
28 #define GRAPH_VERSION_1 0x1
29 #define GRAPH_VERSION GRAPH_VERSION_1
31 #define GRAPH_OID_VERSION_SHA1 1
32 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
33 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
34 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
36 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
37 #define GRAPH_PARENT_MISSING 0x7fffffff
38 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
39 #define GRAPH_PARENT_NONE 0x70000000
41 #define GRAPH_LAST_EDGE 0x80000000
43 #define GRAPH_HEADER_SIZE 8
44 #define GRAPH_FANOUT_SIZE (4 * 256)
45 #define GRAPH_CHUNKLOOKUP_WIDTH 12
46 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
47 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
49 char *get_commit_graph_filename(const char *obj_dir)
51 return xstrfmt("%s/info/commit-graph", obj_dir);
54 static struct commit_graph *alloc_commit_graph(void)
56 struct commit_graph *g = xcalloc(1, sizeof(*g));
62 extern int read_replace_refs;
64 static int commit_graph_compatible(struct repository *r)
69 if (read_replace_refs) {
70 prepare_replace_object(r);
71 if (hashmap_get_size(&r->objects->replace_map->map))
75 prepare_commit_graft(r);
76 if (r->parsed_objects && r->parsed_objects->grafts_nr)
78 if (is_repository_shallow(r))
84 struct commit_graph *load_commit_graph_one(const char *graph_file)
89 struct commit_graph *ret;
90 int fd = git_open(graph_file);
98 graph_size = xsize_t(st.st_size);
100 if (graph_size < GRAPH_MIN_SIZE) {
102 die(_("graph file %s is too small"), graph_file);
104 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
105 ret = parse_commit_graph(graph_map, fd, graph_size);
108 munmap(graph_map, graph_size);
116 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
119 const unsigned char *data, *chunk_lookup;
121 struct commit_graph *graph;
122 uint64_t last_chunk_offset;
123 uint32_t last_chunk_id;
124 uint32_t graph_signature;
125 unsigned char graph_version, hash_version;
130 if (graph_size < GRAPH_MIN_SIZE)
133 data = (const unsigned char *)graph_map;
135 graph_signature = get_be32(data);
136 if (graph_signature != GRAPH_SIGNATURE) {
137 error(_("graph signature %X does not match signature %X"),
138 graph_signature, GRAPH_SIGNATURE);
142 graph_version = *(unsigned char*)(data + 4);
143 if (graph_version != GRAPH_VERSION) {
144 error(_("graph version %X does not match version %X"),
145 graph_version, GRAPH_VERSION);
149 hash_version = *(unsigned char*)(data + 5);
150 if (hash_version != GRAPH_OID_VERSION) {
151 error(_("hash version %X does not match version %X"),
152 hash_version, GRAPH_OID_VERSION);
156 graph = alloc_commit_graph();
158 graph->hash_len = GRAPH_OID_LEN;
159 graph->num_chunks = *(unsigned char*)(data + 6);
160 graph->graph_fd = fd;
161 graph->data = graph_map;
162 graph->data_len = graph_size;
165 last_chunk_offset = 8;
166 chunk_lookup = data + 8;
167 for (i = 0; i < graph->num_chunks; i++) {
168 uint32_t chunk_id = get_be32(chunk_lookup + 0);
169 uint64_t chunk_offset = get_be64(chunk_lookup + 4);
170 int chunk_repeated = 0;
172 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
174 if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
175 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
176 (uint32_t)chunk_offset);
182 case GRAPH_CHUNKID_OIDFANOUT:
183 if (graph->chunk_oid_fanout)
186 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
189 case GRAPH_CHUNKID_OIDLOOKUP:
190 if (graph->chunk_oid_lookup)
193 graph->chunk_oid_lookup = data + chunk_offset;
196 case GRAPH_CHUNKID_DATA:
197 if (graph->chunk_commit_data)
200 graph->chunk_commit_data = data + chunk_offset;
203 case GRAPH_CHUNKID_LARGEEDGES:
204 if (graph->chunk_large_edges)
207 graph->chunk_large_edges = data + chunk_offset;
211 if (chunk_repeated) {
212 error(_("chunk id %08x appears multiple times"), chunk_id);
217 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
219 graph->num_commits = (chunk_offset - last_chunk_offset)
223 last_chunk_id = chunk_id;
224 last_chunk_offset = chunk_offset;
230 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
234 if (r->objects->commit_graph)
237 graph_name = get_commit_graph_filename(obj_dir);
238 r->objects->commit_graph =
239 load_commit_graph_one(graph_name);
241 FREE_AND_NULL(graph_name);
245 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
247 * On the first invocation, this function attemps to load the commit
248 * graph if the_repository is configured to have one.
250 static int prepare_commit_graph(struct repository *r)
252 struct object_directory *odb;
255 if (r->objects->commit_graph_attempted)
256 return !!r->objects->commit_graph;
257 r->objects->commit_graph_attempted = 1;
259 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
260 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
263 * This repository is not configured to use commit graphs, so
264 * do not load one. (But report commit_graph_attempted anyway
265 * so that commit graph loading is not attempted again for this
270 if (!commit_graph_compatible(r))
274 for (odb = r->objects->odb;
275 !r->objects->commit_graph && odb;
277 prepare_commit_graph_one(r, odb->path);
278 return !!r->objects->commit_graph;
281 int generation_numbers_enabled(struct repository *r)
283 uint32_t first_generation;
284 struct commit_graph *g;
285 if (!prepare_commit_graph(r))
288 g = r->objects->commit_graph;
293 first_generation = get_be32(g->chunk_commit_data +
294 g->hash_len + 8) >> 2;
296 return !!first_generation;
299 void close_commit_graph(struct repository *r)
301 free_commit_graph(r->objects->commit_graph);
302 r->objects->commit_graph = NULL;
305 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
307 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
308 g->chunk_oid_lookup, g->hash_len, pos);
311 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
313 struct commit_list **pptr)
316 struct object_id oid;
318 if (pos >= g->num_commits)
319 die("invalid parent position %"PRIu64, pos);
321 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
322 c = lookup_commit(the_repository, &oid);
324 die(_("could not find commit %s"), oid_to_hex(&oid));
326 return &commit_list_insert(c, pptr)->next;
329 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
331 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
332 item->graph_pos = pos;
333 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
336 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
339 uint32_t *parent_data_ptr;
340 uint64_t date_low, date_high;
341 struct commit_list **pptr;
342 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
344 item->object.parsed = 1;
345 item->graph_pos = pos;
347 item->maybe_tree = NULL;
349 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
350 date_low = get_be32(commit_data + g->hash_len + 12);
351 item->date = (timestamp_t)((date_high << 32) | date_low);
353 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
355 pptr = &item->parents;
357 edge_value = get_be32(commit_data + g->hash_len);
358 if (edge_value == GRAPH_PARENT_NONE)
360 pptr = insert_parent_or_die(g, edge_value, pptr);
362 edge_value = get_be32(commit_data + g->hash_len + 4);
363 if (edge_value == GRAPH_PARENT_NONE)
365 if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
366 pptr = insert_parent_or_die(g, edge_value, pptr);
370 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
371 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
373 edge_value = get_be32(parent_data_ptr);
374 pptr = insert_parent_or_die(g,
375 edge_value & GRAPH_EDGE_LAST_MASK,
378 } while (!(edge_value & GRAPH_LAST_EDGE));
383 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
385 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
386 *pos = item->graph_pos;
389 return bsearch_graph(g, &(item->object.oid), pos);
393 static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item)
397 if (item->object.parsed)
400 if (find_commit_in_graph(item, g, &pos))
401 return fill_commit_in_graph(item, g, pos);
406 int parse_commit_in_graph(struct repository *r, struct commit *item)
408 if (!prepare_commit_graph(r))
410 return parse_commit_in_graph_one(r->objects->commit_graph, item);
413 void load_commit_graph_info(struct repository *r, struct commit *item)
416 if (!prepare_commit_graph(r))
418 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
419 fill_commit_graph_info(item, r->objects->commit_graph, pos);
422 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
424 struct object_id oid;
425 const unsigned char *commit_data = g->chunk_commit_data +
426 GRAPH_DATA_WIDTH * (c->graph_pos);
428 hashcpy(oid.hash, commit_data);
429 c->maybe_tree = lookup_tree(the_repository, &oid);
431 return c->maybe_tree;
434 static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
435 const struct commit *c)
438 return c->maybe_tree;
439 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
440 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
442 return load_tree_for_commit(g, (struct commit *)c);
445 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
447 return get_commit_tree_in_graph_one(r->objects->commit_graph, c);
450 static void write_graph_chunk_fanout(struct hashfile *f,
451 struct commit **commits,
455 struct commit **list = commits;
458 * Write the first-level table (the list is sorted,
459 * but we use a 256-entry lookup to be able to avoid
460 * having to do eight extra binary search iterations).
462 for (i = 0; i < 256; i++) {
463 while (count < nr_commits) {
464 if ((*list)->object.oid.hash[0] != i)
470 hashwrite_be32(f, count);
474 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
475 struct commit **commits, int nr_commits)
477 struct commit **list = commits;
479 for (count = 0; count < nr_commits; count++, list++)
480 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
483 static const unsigned char *commit_to_sha1(size_t index, void *table)
485 struct commit **commits = table;
486 return commits[index]->object.oid.hash;
489 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
490 struct commit **commits, int nr_commits)
492 struct commit **list = commits;
493 struct commit **last = commits + nr_commits;
494 uint32_t num_extra_edges = 0;
496 while (list < last) {
497 struct commit_list *parent;
499 uint32_t packedDate[2];
502 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
504 parent = (*list)->parents;
507 edge_value = GRAPH_PARENT_NONE;
509 edge_value = sha1_pos(parent->item->object.oid.hash,
515 edge_value = GRAPH_PARENT_MISSING;
518 hashwrite_be32(f, edge_value);
521 parent = parent->next;
524 edge_value = GRAPH_PARENT_NONE;
525 else if (parent->next)
526 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
528 edge_value = sha1_pos(parent->item->object.oid.hash,
533 edge_value = GRAPH_PARENT_MISSING;
536 hashwrite_be32(f, edge_value);
538 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
541 parent = parent->next;
545 if (sizeof((*list)->date) > 4)
546 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
550 packedDate[0] |= htonl((*list)->generation << 2);
552 packedDate[1] = htonl((*list)->date);
553 hashwrite(f, packedDate, 8);
559 static void write_graph_chunk_large_edges(struct hashfile *f,
560 struct commit **commits,
563 struct commit **list = commits;
564 struct commit **last = commits + nr_commits;
565 struct commit_list *parent;
567 while (list < last) {
569 for (parent = (*list)->parents; num_parents < 3 && parent;
570 parent = parent->next)
573 if (num_parents <= 2) {
578 /* Since num_parents > 2, this initializer is safe. */
579 for (parent = (*list)->parents->next; parent; parent = parent->next) {
580 int edge_value = sha1_pos(parent->item->object.oid.hash,
586 edge_value = GRAPH_PARENT_MISSING;
587 else if (!parent->next)
588 edge_value |= GRAPH_LAST_EDGE;
590 hashwrite_be32(f, edge_value);
597 static int commit_compare(const void *_a, const void *_b)
599 const struct object_id *a = (const struct object_id *)_a;
600 const struct object_id *b = (const struct object_id *)_b;
604 struct packed_commit_list {
605 struct commit **list;
610 struct packed_oid_list {
611 struct object_id *list;
614 struct progress *progress;
618 static int add_packed_commits(const struct object_id *oid,
619 struct packed_git *pack,
623 struct packed_oid_list *list = (struct packed_oid_list*)data;
624 enum object_type type;
625 off_t offset = nth_packed_object_offset(pack, pos);
626 struct object_info oi = OBJECT_INFO_INIT;
629 display_progress(list->progress, ++list->progress_done);
632 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
633 die(_("unable to get type of object %s"), oid_to_hex(oid));
635 if (type != OBJ_COMMIT)
638 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
639 oidcpy(&(list->list[list->nr]), oid);
645 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
647 struct commit_list *parent;
648 for (parent = commit->parents; parent; parent = parent->next) {
649 if (!(parent->item->object.flags & UNINTERESTING)) {
650 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
651 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
653 parent->item->object.flags |= UNINTERESTING;
658 static void close_reachable(struct packed_oid_list *oids, int report_progress)
661 struct commit *commit;
662 struct progress *progress = NULL;
665 progress = start_delayed_progress(
666 _("Loading known commits in commit graph"), j = 0);
667 for (i = 0; i < oids->nr; i++) {
668 display_progress(progress, ++j);
669 commit = lookup_commit(the_repository, &oids->list[i]);
671 commit->object.flags |= UNINTERESTING;
673 stop_progress(&progress);
676 * As this loop runs, oids->nr may grow, but not more
677 * than the number of missing commits in the reachable
681 progress = start_delayed_progress(
682 _("Expanding reachable commits in commit graph"), j = 0);
683 for (i = 0; i < oids->nr; i++) {
684 display_progress(progress, ++j);
685 commit = lookup_commit(the_repository, &oids->list[i]);
687 if (commit && !parse_commit(commit))
688 add_missing_parents(oids, commit);
690 stop_progress(&progress);
693 progress = start_delayed_progress(
694 _("Clearing commit marks in commit graph"), j = 0);
695 for (i = 0; i < oids->nr; i++) {
696 display_progress(progress, ++j);
697 commit = lookup_commit(the_repository, &oids->list[i]);
700 commit->object.flags &= ~UNINTERESTING;
702 stop_progress(&progress);
705 static void compute_generation_numbers(struct packed_commit_list* commits,
709 struct commit_list *list = NULL;
710 struct progress *progress = NULL;
713 progress = start_progress(
714 _("Computing commit graph generation numbers"),
716 for (i = 0; i < commits->nr; i++) {
717 display_progress(progress, i + 1);
718 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
719 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
722 commit_list_insert(commits->list[i], &list);
724 struct commit *current = list->item;
725 struct commit_list *parent;
726 int all_parents_computed = 1;
727 uint32_t max_generation = 0;
729 for (parent = current->parents; parent; parent = parent->next) {
730 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
731 parent->item->generation == GENERATION_NUMBER_ZERO) {
732 all_parents_computed = 0;
733 commit_list_insert(parent->item, &list);
735 } else if (parent->item->generation > max_generation) {
736 max_generation = parent->item->generation;
740 if (all_parents_computed) {
741 current->generation = max_generation + 1;
744 if (current->generation > GENERATION_NUMBER_MAX)
745 current->generation = GENERATION_NUMBER_MAX;
749 stop_progress(&progress);
752 static int add_ref_to_list(const char *refname,
753 const struct object_id *oid,
754 int flags, void *cb_data)
756 struct string_list *list = (struct string_list *)cb_data;
758 string_list_append(list, oid_to_hex(oid));
762 void write_commit_graph_reachable(const char *obj_dir, int append,
765 struct string_list list = STRING_LIST_INIT_DUP;
767 for_each_ref(add_ref_to_list, &list);
768 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
770 string_list_clear(&list, 0);
773 void write_commit_graph(const char *obj_dir,
774 struct string_list *pack_indexes,
775 struct string_list *commit_hex,
776 int append, int report_progress)
778 struct packed_oid_list oids;
779 struct packed_commit_list commits;
781 uint32_t i, count_distinct = 0;
783 struct lock_file lk = LOCK_INIT;
784 uint32_t chunk_ids[5];
785 uint64_t chunk_offsets[5];
788 struct commit_list *parent;
789 struct progress *progress = NULL;
791 if (!commit_graph_compatible(the_repository))
795 oids.alloc = approximate_object_count() / 32;
796 oids.progress = NULL;
797 oids.progress_done = 0;
800 prepare_commit_graph_one(the_repository, obj_dir);
801 if (the_repository->objects->commit_graph)
802 oids.alloc += the_repository->objects->commit_graph->num_commits;
805 if (oids.alloc < 1024)
807 ALLOC_ARRAY(oids.list, oids.alloc);
809 if (append && the_repository->objects->commit_graph) {
810 struct commit_graph *commit_graph =
811 the_repository->objects->commit_graph;
812 for (i = 0; i < commit_graph->num_commits; i++) {
813 const unsigned char *hash = commit_graph->chunk_oid_lookup +
814 commit_graph->hash_len * i;
815 hashcpy(oids.list[oids.nr++].hash, hash);
820 struct strbuf packname = STRBUF_INIT;
822 strbuf_addf(&packname, "%s/pack/", obj_dir);
823 dirlen = packname.len;
824 if (report_progress) {
825 oids.progress = start_delayed_progress(
826 _("Finding commits for commit graph"), 0);
827 oids.progress_done = 0;
829 for (i = 0; i < pack_indexes->nr; i++) {
830 struct packed_git *p;
831 strbuf_setlen(&packname, dirlen);
832 strbuf_addstr(&packname, pack_indexes->items[i].string);
833 p = add_packed_git(packname.buf, packname.len, 1);
835 die(_("error adding pack %s"), packname.buf);
836 if (open_pack_index(p))
837 die(_("error opening index for %s"), packname.buf);
838 for_each_object_in_pack(p, add_packed_commits, &oids, 0);
842 stop_progress(&oids.progress);
843 strbuf_release(&packname);
848 progress = start_delayed_progress(
849 _("Finding commits for commit graph"),
851 for (i = 0; i < commit_hex->nr; i++) {
853 struct object_id oid;
854 struct commit *result;
856 display_progress(progress, i + 1);
857 if (commit_hex->items[i].string &&
858 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
861 result = lookup_commit_reference_gently(the_repository, &oid, 1);
864 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
865 oidcpy(&oids.list[oids.nr], &(result->object.oid));
869 stop_progress(&progress);
872 if (!pack_indexes && !commit_hex) {
874 oids.progress = start_delayed_progress(
875 _("Finding commits for commit graph"), 0);
876 for_each_packed_object(add_packed_commits, &oids, 0);
877 stop_progress(&oids.progress);
880 close_reachable(&oids, report_progress);
882 QSORT(oids.list, oids.nr, commit_compare);
885 for (i = 1; i < oids.nr; i++) {
886 if (!oideq(&oids.list[i - 1], &oids.list[i]))
890 if (count_distinct >= GRAPH_PARENT_MISSING)
891 die(_("the commit graph format cannot write %d commits"), count_distinct);
894 commits.alloc = count_distinct;
895 ALLOC_ARRAY(commits.list, commits.alloc);
898 for (i = 0; i < oids.nr; i++) {
900 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
903 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
904 parse_commit(commits.list[commits.nr]);
906 for (parent = commits.list[commits.nr]->parents;
907 parent; parent = parent->next)
911 num_extra_edges += num_parents - 1;
915 num_chunks = num_extra_edges ? 4 : 3;
917 if (commits.nr >= GRAPH_PARENT_MISSING)
918 die(_("too many commits to write graph"));
920 compute_generation_numbers(&commits, report_progress);
922 graph_name = get_commit_graph_filename(obj_dir);
923 if (safe_create_leading_directories(graph_name)) {
925 die_errno(_("unable to create leading directories of %s"),
929 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
930 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
932 hashwrite_be32(f, GRAPH_SIGNATURE);
934 hashwrite_u8(f, GRAPH_VERSION);
935 hashwrite_u8(f, GRAPH_OID_VERSION);
936 hashwrite_u8(f, num_chunks);
937 hashwrite_u8(f, 0); /* unused padding byte */
939 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
940 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
941 chunk_ids[2] = GRAPH_CHUNKID_DATA;
943 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
948 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
949 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
950 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
951 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
952 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
954 for (i = 0; i <= num_chunks; i++) {
955 uint32_t chunk_write[3];
957 chunk_write[0] = htonl(chunk_ids[i]);
958 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
959 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
960 hashwrite(f, chunk_write, 12);
963 write_graph_chunk_fanout(f, commits.list, commits.nr);
964 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
965 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
966 write_graph_chunk_large_edges(f, commits.list, commits.nr);
968 close_commit_graph(the_repository);
969 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
970 commit_lock_file(&lk);
977 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
978 static int verify_commit_graph_error;
980 static void graph_report(const char *fmt, ...)
984 verify_commit_graph_error = 1;
986 vfprintf(stderr, fmt, ap);
987 fprintf(stderr, "\n");
991 #define GENERATION_ZERO_EXISTS 1
992 #define GENERATION_NUMBER_EXISTS 2
994 int verify_commit_graph(struct repository *r, struct commit_graph *g)
996 uint32_t i, cur_fanout_pos = 0;
997 struct object_id prev_oid, cur_oid, checksum;
998 int generation_zero = 0;
1001 struct progress *progress = NULL;
1004 graph_report("no commit-graph file loaded");
1008 verify_commit_graph_error = 0;
1010 if (!g->chunk_oid_fanout)
1011 graph_report("commit-graph is missing the OID Fanout chunk");
1012 if (!g->chunk_oid_lookup)
1013 graph_report("commit-graph is missing the OID Lookup chunk");
1014 if (!g->chunk_commit_data)
1015 graph_report("commit-graph is missing the Commit Data chunk");
1017 if (verify_commit_graph_error)
1018 return verify_commit_graph_error;
1020 devnull = open("/dev/null", O_WRONLY);
1021 f = hashfd(devnull, NULL);
1022 hashwrite(f, g->data, g->data_len - g->hash_len);
1023 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1024 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1025 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1026 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1029 for (i = 0; i < g->num_commits; i++) {
1030 struct commit *graph_commit;
1032 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1034 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1035 graph_report("commit-graph has incorrect OID order: %s then %s",
1036 oid_to_hex(&prev_oid),
1037 oid_to_hex(&cur_oid));
1039 oidcpy(&prev_oid, &cur_oid);
1041 while (cur_oid.hash[0] > cur_fanout_pos) {
1042 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1044 if (i != fanout_value)
1045 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1046 cur_fanout_pos, fanout_value, i);
1050 graph_commit = lookup_commit(r, &cur_oid);
1051 if (!parse_commit_in_graph_one(g, graph_commit))
1052 graph_report("failed to parse %s from commit-graph",
1053 oid_to_hex(&cur_oid));
1056 while (cur_fanout_pos < 256) {
1057 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1059 if (g->num_commits != fanout_value)
1060 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1061 cur_fanout_pos, fanout_value, i);
1066 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1067 return verify_commit_graph_error;
1069 progress = start_progress(_("Verifying commits in commit graph"),
1071 for (i = 0; i < g->num_commits; i++) {
1072 struct commit *graph_commit, *odb_commit;
1073 struct commit_list *graph_parents, *odb_parents;
1074 uint32_t max_generation = 0;
1076 display_progress(progress, i + 1);
1077 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1079 graph_commit = lookup_commit(r, &cur_oid);
1080 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1081 if (parse_commit_internal(odb_commit, 0, 0)) {
1082 graph_report("failed to parse %s from object database",
1083 oid_to_hex(&cur_oid));
1087 if (!oideq(&get_commit_tree_in_graph_one(g, graph_commit)->object.oid,
1088 get_commit_tree_oid(odb_commit)))
1089 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1090 oid_to_hex(&cur_oid),
1091 oid_to_hex(get_commit_tree_oid(graph_commit)),
1092 oid_to_hex(get_commit_tree_oid(odb_commit)));
1094 graph_parents = graph_commit->parents;
1095 odb_parents = odb_commit->parents;
1097 while (graph_parents) {
1098 if (odb_parents == NULL) {
1099 graph_report("commit-graph parent list for commit %s is too long",
1100 oid_to_hex(&cur_oid));
1104 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1105 graph_report("commit-graph parent for %s is %s != %s",
1106 oid_to_hex(&cur_oid),
1107 oid_to_hex(&graph_parents->item->object.oid),
1108 oid_to_hex(&odb_parents->item->object.oid));
1110 if (graph_parents->item->generation > max_generation)
1111 max_generation = graph_parents->item->generation;
1113 graph_parents = graph_parents->next;
1114 odb_parents = odb_parents->next;
1117 if (odb_parents != NULL)
1118 graph_report("commit-graph parent list for commit %s terminates early",
1119 oid_to_hex(&cur_oid));
1121 if (!graph_commit->generation) {
1122 if (generation_zero == GENERATION_NUMBER_EXISTS)
1123 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1124 oid_to_hex(&cur_oid));
1125 generation_zero = GENERATION_ZERO_EXISTS;
1126 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1127 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1128 oid_to_hex(&cur_oid));
1130 if (generation_zero == GENERATION_ZERO_EXISTS)
1134 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1135 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1136 * extra logic in the following condition.
1138 if (max_generation == GENERATION_NUMBER_MAX)
1141 if (graph_commit->generation != max_generation + 1)
1142 graph_report("commit-graph generation for commit %s is %u != %u",
1143 oid_to_hex(&cur_oid),
1144 graph_commit->generation,
1145 max_generation + 1);
1147 if (graph_commit->date != odb_commit->date)
1148 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1149 oid_to_hex(&cur_oid),
1153 stop_progress(&progress);
1155 return verify_commit_graph_error;
1158 void free_commit_graph(struct commit_graph *g)
1162 if (g->graph_fd >= 0) {
1163 munmap((void *)g->data, g->data_len);