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_EXTRAEDGES 0x45444745 /* "EDGE" */
26 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
28 #define GRAPH_VERSION_1 0x1
29 #define GRAPH_VERSION GRAPH_VERSION_1
31 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
32 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
33 #define GRAPH_PARENT_NONE 0x70000000
35 #define GRAPH_LAST_EDGE 0x80000000
37 #define GRAPH_HEADER_SIZE 8
38 #define GRAPH_FANOUT_SIZE (4 * 256)
39 #define GRAPH_CHUNKLOOKUP_WIDTH 12
40 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
41 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
43 char *get_commit_graph_filename(const char *obj_dir)
45 return xstrfmt("%s/info/commit-graph", obj_dir);
48 static uint8_t oid_version(void)
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)
68 if (read_replace_refs) {
69 prepare_replace_object(r);
70 if (hashmap_get_size(&r->objects->replace_map->map))
74 prepare_commit_graft(r);
75 if (r->parsed_objects && r->parsed_objects->grafts_nr)
77 if (is_repository_shallow(r))
83 struct commit_graph *load_commit_graph_one(const char *graph_file)
88 struct commit_graph *ret;
89 int fd = git_open(graph_file);
97 graph_size = xsize_t(st.st_size);
99 if (graph_size < GRAPH_MIN_SIZE) {
101 die(_("graph file %s is too small"), graph_file);
103 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
104 ret = parse_commit_graph(graph_map, fd, graph_size);
107 munmap(graph_map, graph_size);
115 static int verify_commit_graph_lite(struct commit_graph *g)
118 * Basic validation shared between parse_commit_graph()
119 * which'll be called every time the graph is used, and the
120 * much more expensive verify_commit_graph() used by
121 * "commit-graph verify".
123 * There should only be very basic checks here to ensure that
124 * we don't e.g. segfault in fill_commit_in_graph(), but
125 * because this is a very hot codepath nothing that e.g. loops
126 * over g->num_commits, or runs a checksum on the commit-graph
129 if (!g->chunk_oid_fanout) {
130 error("commit-graph is missing the OID Fanout chunk");
133 if (!g->chunk_oid_lookup) {
134 error("commit-graph is missing the OID Lookup chunk");
137 if (!g->chunk_commit_data) {
138 error("commit-graph is missing the Commit Data chunk");
145 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
148 const unsigned char *data, *chunk_lookup;
150 struct commit_graph *graph;
151 uint64_t last_chunk_offset;
152 uint32_t last_chunk_id;
153 uint32_t graph_signature;
154 unsigned char graph_version, hash_version;
159 if (graph_size < GRAPH_MIN_SIZE)
162 data = (const unsigned char *)graph_map;
164 graph_signature = get_be32(data);
165 if (graph_signature != GRAPH_SIGNATURE) {
166 error(_("graph signature %X does not match signature %X"),
167 graph_signature, GRAPH_SIGNATURE);
171 graph_version = *(unsigned char*)(data + 4);
172 if (graph_version != GRAPH_VERSION) {
173 error(_("graph version %X does not match version %X"),
174 graph_version, GRAPH_VERSION);
178 hash_version = *(unsigned char*)(data + 5);
179 if (hash_version != oid_version()) {
180 error(_("hash version %X does not match version %X"),
181 hash_version, oid_version());
185 graph = alloc_commit_graph();
187 graph->hash_len = the_hash_algo->rawsz;
188 graph->num_chunks = *(unsigned char*)(data + 6);
189 graph->graph_fd = fd;
190 graph->data = graph_map;
191 graph->data_len = graph_size;
194 last_chunk_offset = 8;
195 chunk_lookup = data + 8;
196 for (i = 0; i < graph->num_chunks; i++) {
198 uint64_t chunk_offset;
199 int chunk_repeated = 0;
201 if (data + graph_size - chunk_lookup <
202 GRAPH_CHUNKLOOKUP_WIDTH) {
203 error(_("chunk lookup table entry missing; graph file may be incomplete"));
208 chunk_id = get_be32(chunk_lookup + 0);
209 chunk_offset = get_be64(chunk_lookup + 4);
211 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
213 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
214 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
215 (uint32_t)chunk_offset);
221 case GRAPH_CHUNKID_OIDFANOUT:
222 if (graph->chunk_oid_fanout)
225 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
228 case GRAPH_CHUNKID_OIDLOOKUP:
229 if (graph->chunk_oid_lookup)
232 graph->chunk_oid_lookup = data + chunk_offset;
235 case GRAPH_CHUNKID_DATA:
236 if (graph->chunk_commit_data)
239 graph->chunk_commit_data = data + chunk_offset;
242 case GRAPH_CHUNKID_EXTRAEDGES:
243 if (graph->chunk_extra_edges)
246 graph->chunk_extra_edges = data + chunk_offset;
250 if (chunk_repeated) {
251 error(_("chunk id %08x appears multiple times"), chunk_id);
256 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
258 graph->num_commits = (chunk_offset - last_chunk_offset)
262 last_chunk_id = chunk_id;
263 last_chunk_offset = chunk_offset;
266 if (verify_commit_graph_lite(graph))
272 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
276 if (r->objects->commit_graph)
279 graph_name = get_commit_graph_filename(obj_dir);
280 r->objects->commit_graph =
281 load_commit_graph_one(graph_name);
283 FREE_AND_NULL(graph_name);
287 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
289 * On the first invocation, this function attemps to load the commit
290 * graph if the_repository is configured to have one.
292 static int prepare_commit_graph(struct repository *r)
294 struct object_directory *odb;
297 if (r->objects->commit_graph_attempted)
298 return !!r->objects->commit_graph;
299 r->objects->commit_graph_attempted = 1;
301 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
302 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
305 * This repository is not configured to use commit graphs, so
306 * do not load one. (But report commit_graph_attempted anyway
307 * so that commit graph loading is not attempted again for this
312 if (!commit_graph_compatible(r))
316 for (odb = r->objects->odb;
317 !r->objects->commit_graph && odb;
319 prepare_commit_graph_one(r, odb->path);
320 return !!r->objects->commit_graph;
323 int generation_numbers_enabled(struct repository *r)
325 uint32_t first_generation;
326 struct commit_graph *g;
327 if (!prepare_commit_graph(r))
330 g = r->objects->commit_graph;
335 first_generation = get_be32(g->chunk_commit_data +
336 g->hash_len + 8) >> 2;
338 return !!first_generation;
341 void close_commit_graph(struct repository *r)
343 free_commit_graph(r->objects->commit_graph);
344 r->objects->commit_graph = NULL;
347 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
349 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
350 g->chunk_oid_lookup, g->hash_len, pos);
353 static struct commit_list **insert_parent_or_die(struct repository *r,
354 struct commit_graph *g,
356 struct commit_list **pptr)
359 struct object_id oid;
361 if (pos >= g->num_commits)
362 die("invalid parent position %"PRIu64, pos);
364 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
365 c = lookup_commit(r, &oid);
367 die(_("could not find commit %s"), oid_to_hex(&oid));
369 return &commit_list_insert(c, pptr)->next;
372 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
374 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
375 item->graph_pos = pos;
376 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
379 static int fill_commit_in_graph(struct repository *r,
381 struct commit_graph *g, uint32_t pos)
384 uint32_t *parent_data_ptr;
385 uint64_t date_low, date_high;
386 struct commit_list **pptr;
387 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
389 item->object.parsed = 1;
390 item->graph_pos = pos;
392 item->maybe_tree = NULL;
394 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
395 date_low = get_be32(commit_data + g->hash_len + 12);
396 item->date = (timestamp_t)((date_high << 32) | date_low);
398 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
400 pptr = &item->parents;
402 edge_value = get_be32(commit_data + g->hash_len);
403 if (edge_value == GRAPH_PARENT_NONE)
405 pptr = insert_parent_or_die(r, g, edge_value, pptr);
407 edge_value = get_be32(commit_data + g->hash_len + 4);
408 if (edge_value == GRAPH_PARENT_NONE)
410 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
411 pptr = insert_parent_or_die(r, g, edge_value, pptr);
415 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
416 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
418 edge_value = get_be32(parent_data_ptr);
419 pptr = insert_parent_or_die(r, g,
420 edge_value & GRAPH_EDGE_LAST_MASK,
423 } while (!(edge_value & GRAPH_LAST_EDGE));
428 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
430 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
431 *pos = item->graph_pos;
434 return bsearch_graph(g, &(item->object.oid), pos);
438 static int parse_commit_in_graph_one(struct repository *r,
439 struct commit_graph *g,
444 if (item->object.parsed)
447 if (find_commit_in_graph(item, g, &pos))
448 return fill_commit_in_graph(r, item, g, pos);
453 int parse_commit_in_graph(struct repository *r, struct commit *item)
455 if (!prepare_commit_graph(r))
457 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
460 void load_commit_graph_info(struct repository *r, struct commit *item)
463 if (!prepare_commit_graph(r))
465 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
466 fill_commit_graph_info(item, r->objects->commit_graph, pos);
469 static struct tree *load_tree_for_commit(struct repository *r,
470 struct commit_graph *g,
473 struct object_id oid;
474 const unsigned char *commit_data = g->chunk_commit_data +
475 GRAPH_DATA_WIDTH * (c->graph_pos);
477 hashcpy(oid.hash, commit_data);
478 c->maybe_tree = lookup_tree(r, &oid);
480 return c->maybe_tree;
483 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
484 struct commit_graph *g,
485 const struct commit *c)
488 return c->maybe_tree;
489 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
490 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
492 return load_tree_for_commit(r, g, (struct commit *)c);
495 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
497 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
500 static void write_graph_chunk_fanout(struct hashfile *f,
501 struct commit **commits,
503 struct progress *progress,
504 uint64_t *progress_cnt)
507 struct commit **list = commits;
510 * Write the first-level table (the list is sorted,
511 * but we use a 256-entry lookup to be able to avoid
512 * having to do eight extra binary search iterations).
514 for (i = 0; i < 256; i++) {
515 while (count < nr_commits) {
516 if ((*list)->object.oid.hash[0] != i)
518 display_progress(progress, ++*progress_cnt);
523 hashwrite_be32(f, count);
527 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
528 struct commit **commits, int nr_commits,
529 struct progress *progress,
530 uint64_t *progress_cnt)
532 struct commit **list = commits;
534 for (count = 0; count < nr_commits; count++, list++) {
535 display_progress(progress, ++*progress_cnt);
536 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
540 static const unsigned char *commit_to_sha1(size_t index, void *table)
542 struct commit **commits = table;
543 return commits[index]->object.oid.hash;
546 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
547 struct commit **commits, int nr_commits,
548 struct progress *progress,
549 uint64_t *progress_cnt)
551 struct commit **list = commits;
552 struct commit **last = commits + nr_commits;
553 uint32_t num_extra_edges = 0;
555 while (list < last) {
556 struct commit_list *parent;
558 uint32_t packedDate[2];
559 display_progress(progress, ++*progress_cnt);
562 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
564 parent = (*list)->parents;
567 edge_value = GRAPH_PARENT_NONE;
569 edge_value = sha1_pos(parent->item->object.oid.hash,
575 BUG("missing parent %s for commit %s",
576 oid_to_hex(&parent->item->object.oid),
577 oid_to_hex(&(*list)->object.oid));
580 hashwrite_be32(f, edge_value);
583 parent = parent->next;
586 edge_value = GRAPH_PARENT_NONE;
587 else if (parent->next)
588 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
590 edge_value = sha1_pos(parent->item->object.oid.hash,
595 BUG("missing parent %s for commit %s",
596 oid_to_hex(&parent->item->object.oid),
597 oid_to_hex(&(*list)->object.oid));
600 hashwrite_be32(f, edge_value);
602 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
605 parent = parent->next;
609 if (sizeof((*list)->date) > 4)
610 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
614 packedDate[0] |= htonl((*list)->generation << 2);
616 packedDate[1] = htonl((*list)->date);
617 hashwrite(f, packedDate, 8);
623 static void write_graph_chunk_extra_edges(struct hashfile *f,
624 struct commit **commits,
626 struct progress *progress,
627 uint64_t *progress_cnt)
629 struct commit **list = commits;
630 struct commit **last = commits + nr_commits;
631 struct commit_list *parent;
633 while (list < last) {
636 display_progress(progress, ++*progress_cnt);
638 for (parent = (*list)->parents; num_parents < 3 && parent;
639 parent = parent->next)
642 if (num_parents <= 2) {
647 /* Since num_parents > 2, this initializer is safe. */
648 for (parent = (*list)->parents->next; parent; parent = parent->next) {
649 int edge_value = sha1_pos(parent->item->object.oid.hash,
655 BUG("missing parent %s for commit %s",
656 oid_to_hex(&parent->item->object.oid),
657 oid_to_hex(&(*list)->object.oid));
658 else if (!parent->next)
659 edge_value |= GRAPH_LAST_EDGE;
661 hashwrite_be32(f, edge_value);
668 static int commit_compare(const void *_a, const void *_b)
670 const struct object_id *a = (const struct object_id *)_a;
671 const struct object_id *b = (const struct object_id *)_b;
675 struct packed_commit_list {
676 struct commit **list;
681 struct packed_oid_list {
682 struct object_id *list;
685 struct progress *progress;
689 static int add_packed_commits(const struct object_id *oid,
690 struct packed_git *pack,
694 struct packed_oid_list *list = (struct packed_oid_list*)data;
695 enum object_type type;
696 off_t offset = nth_packed_object_offset(pack, pos);
697 struct object_info oi = OBJECT_INFO_INIT;
700 display_progress(list->progress, ++list->progress_done);
703 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
704 die(_("unable to get type of object %s"), oid_to_hex(oid));
706 if (type != OBJ_COMMIT)
709 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
710 oidcpy(&(list->list[list->nr]), oid);
716 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
718 struct commit_list *parent;
719 for (parent = commit->parents; parent; parent = parent->next) {
720 if (!(parent->item->object.flags & UNINTERESTING)) {
721 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
722 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
724 parent->item->object.flags |= UNINTERESTING;
729 static void close_reachable(struct packed_oid_list *oids, int report_progress)
732 struct commit *commit;
733 struct progress *progress = NULL;
736 progress = start_delayed_progress(
737 _("Loading known commits in commit graph"), oids->nr);
738 for (i = 0; i < oids->nr; i++) {
739 display_progress(progress, i + 1);
740 commit = lookup_commit(the_repository, &oids->list[i]);
742 commit->object.flags |= UNINTERESTING;
744 stop_progress(&progress);
747 * As this loop runs, oids->nr may grow, but not more
748 * than the number of missing commits in the reachable
752 progress = start_delayed_progress(
753 _("Expanding reachable commits in commit graph"), oids->nr);
754 for (i = 0; i < oids->nr; i++) {
755 display_progress(progress, i + 1);
756 commit = lookup_commit(the_repository, &oids->list[i]);
758 if (commit && !parse_commit(commit))
759 add_missing_parents(oids, commit);
761 stop_progress(&progress);
764 progress = start_delayed_progress(
765 _("Clearing commit marks in commit graph"), oids->nr);
766 for (i = 0; i < oids->nr; i++) {
767 display_progress(progress, i + 1);
768 commit = lookup_commit(the_repository, &oids->list[i]);
771 commit->object.flags &= ~UNINTERESTING;
773 stop_progress(&progress);
776 static void compute_generation_numbers(struct packed_commit_list* commits,
780 struct commit_list *list = NULL;
781 struct progress *progress = NULL;
784 progress = start_progress(
785 _("Computing commit graph generation numbers"),
787 for (i = 0; i < commits->nr; i++) {
788 display_progress(progress, i + 1);
789 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
790 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
793 commit_list_insert(commits->list[i], &list);
795 struct commit *current = list->item;
796 struct commit_list *parent;
797 int all_parents_computed = 1;
798 uint32_t max_generation = 0;
800 for (parent = current->parents; parent; parent = parent->next) {
801 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
802 parent->item->generation == GENERATION_NUMBER_ZERO) {
803 all_parents_computed = 0;
804 commit_list_insert(parent->item, &list);
806 } else if (parent->item->generation > max_generation) {
807 max_generation = parent->item->generation;
811 if (all_parents_computed) {
812 current->generation = max_generation + 1;
815 if (current->generation > GENERATION_NUMBER_MAX)
816 current->generation = GENERATION_NUMBER_MAX;
820 stop_progress(&progress);
823 static int add_ref_to_list(const char *refname,
824 const struct object_id *oid,
825 int flags, void *cb_data)
827 struct string_list *list = (struct string_list *)cb_data;
829 string_list_append(list, oid_to_hex(oid));
833 void write_commit_graph_reachable(const char *obj_dir, int append,
836 struct string_list list = STRING_LIST_INIT_DUP;
838 for_each_ref(add_ref_to_list, &list);
839 write_commit_graph(obj_dir, NULL, &list, append, report_progress);
841 string_list_clear(&list, 0);
844 void write_commit_graph(const char *obj_dir,
845 struct string_list *pack_indexes,
846 struct string_list *commit_hex,
847 int append, int report_progress)
849 struct packed_oid_list oids;
850 struct packed_commit_list commits;
852 uint32_t i, count_distinct = 0;
854 struct lock_file lk = LOCK_INIT;
855 uint32_t chunk_ids[5];
856 uint64_t chunk_offsets[5];
859 struct commit_list *parent;
860 struct progress *progress = NULL;
861 const unsigned hashsz = the_hash_algo->rawsz;
862 uint64_t progress_cnt = 0;
863 struct strbuf progress_title = STRBUF_INIT;
864 unsigned long approx_nr_objects;
866 if (!commit_graph_compatible(the_repository))
870 approx_nr_objects = approximate_object_count();
871 oids.alloc = approx_nr_objects / 32;
872 oids.progress = NULL;
873 oids.progress_done = 0;
876 prepare_commit_graph_one(the_repository, obj_dir);
877 if (the_repository->objects->commit_graph)
878 oids.alloc += the_repository->objects->commit_graph->num_commits;
881 if (oids.alloc < 1024)
883 ALLOC_ARRAY(oids.list, oids.alloc);
885 if (append && the_repository->objects->commit_graph) {
886 struct commit_graph *commit_graph =
887 the_repository->objects->commit_graph;
888 for (i = 0; i < commit_graph->num_commits; i++) {
889 const unsigned char *hash = commit_graph->chunk_oid_lookup +
890 commit_graph->hash_len * i;
891 hashcpy(oids.list[oids.nr++].hash, hash);
896 struct strbuf packname = STRBUF_INIT;
898 strbuf_addf(&packname, "%s/pack/", obj_dir);
899 dirlen = packname.len;
900 if (report_progress) {
901 strbuf_addf(&progress_title,
902 Q_("Finding commits for commit graph in %d pack",
903 "Finding commits for commit graph in %d packs",
906 oids.progress = start_delayed_progress(progress_title.buf, 0);
907 oids.progress_done = 0;
909 for (i = 0; i < pack_indexes->nr; i++) {
910 struct packed_git *p;
911 strbuf_setlen(&packname, dirlen);
912 strbuf_addstr(&packname, pack_indexes->items[i].string);
913 p = add_packed_git(packname.buf, packname.len, 1);
915 die(_("error adding pack %s"), packname.buf);
916 if (open_pack_index(p))
917 die(_("error opening index for %s"), packname.buf);
918 for_each_object_in_pack(p, add_packed_commits, &oids,
919 FOR_EACH_OBJECT_PACK_ORDER);
923 stop_progress(&oids.progress);
924 strbuf_reset(&progress_title);
925 strbuf_release(&packname);
929 if (report_progress) {
930 strbuf_addf(&progress_title,
931 Q_("Finding commits for commit graph from %d ref",
932 "Finding commits for commit graph from %d refs",
935 progress = start_delayed_progress(progress_title.buf,
938 for (i = 0; i < commit_hex->nr; i++) {
940 struct object_id oid;
941 struct commit *result;
943 display_progress(progress, i + 1);
944 if (commit_hex->items[i].string &&
945 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
948 result = lookup_commit_reference_gently(the_repository, &oid, 1);
951 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
952 oidcpy(&oids.list[oids.nr], &(result->object.oid));
956 stop_progress(&progress);
957 strbuf_reset(&progress_title);
960 if (!pack_indexes && !commit_hex) {
962 oids.progress = start_delayed_progress(
963 _("Finding commits for commit graph among packed objects"),
965 for_each_packed_object(add_packed_commits, &oids,
966 FOR_EACH_OBJECT_PACK_ORDER);
967 if (oids.progress_done < approx_nr_objects)
968 display_progress(oids.progress, approx_nr_objects);
969 stop_progress(&oids.progress);
972 close_reachable(&oids, report_progress);
975 progress = start_delayed_progress(
976 _("Counting distinct commits in commit graph"),
978 display_progress(progress, 0); /* TODO: Measure QSORT() progress */
979 QSORT(oids.list, oids.nr, commit_compare);
981 for (i = 1; i < oids.nr; i++) {
982 display_progress(progress, i + 1);
983 if (!oideq(&oids.list[i - 1], &oids.list[i]))
986 stop_progress(&progress);
988 if (count_distinct >= GRAPH_EDGE_LAST_MASK)
989 die(_("the commit graph format cannot write %d commits"), count_distinct);
992 commits.alloc = count_distinct;
993 ALLOC_ARRAY(commits.list, commits.alloc);
997 progress = start_delayed_progress(
998 _("Finding extra edges in commit graph"),
1000 for (i = 0; i < oids.nr; i++) {
1001 int num_parents = 0;
1002 display_progress(progress, i + 1);
1003 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
1006 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
1007 parse_commit(commits.list[commits.nr]);
1009 for (parent = commits.list[commits.nr]->parents;
1010 parent; parent = parent->next)
1013 if (num_parents > 2)
1014 num_extra_edges += num_parents - 1;
1018 num_chunks = num_extra_edges ? 4 : 3;
1019 stop_progress(&progress);
1021 if (commits.nr >= GRAPH_EDGE_LAST_MASK)
1022 die(_("too many commits to write graph"));
1024 compute_generation_numbers(&commits, report_progress);
1026 graph_name = get_commit_graph_filename(obj_dir);
1027 if (safe_create_leading_directories(graph_name)) {
1029 die_errno(_("unable to create leading directories of %s"),
1033 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
1034 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1036 hashwrite_be32(f, GRAPH_SIGNATURE);
1038 hashwrite_u8(f, GRAPH_VERSION);
1039 hashwrite_u8(f, oid_version());
1040 hashwrite_u8(f, num_chunks);
1041 hashwrite_u8(f, 0); /* unused padding byte */
1043 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1044 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1045 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1046 if (num_extra_edges)
1047 chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
1052 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1053 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1054 chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr;
1055 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr;
1056 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
1058 for (i = 0; i <= num_chunks; i++) {
1059 uint32_t chunk_write[3];
1061 chunk_write[0] = htonl(chunk_ids[i]);
1062 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1063 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1064 hashwrite(f, chunk_write, 12);
1067 if (report_progress) {
1068 strbuf_addf(&progress_title,
1069 Q_("Writing out commit graph in %d pass",
1070 "Writing out commit graph in %d passes",
1073 progress = start_delayed_progress(
1075 num_chunks * commits.nr);
1077 write_graph_chunk_fanout(f, commits.list, commits.nr, progress, &progress_cnt);
1078 write_graph_chunk_oids(f, hashsz, commits.list, commits.nr, progress, &progress_cnt);
1079 write_graph_chunk_data(f, hashsz, commits.list, commits.nr, progress, &progress_cnt);
1080 if (num_extra_edges)
1081 write_graph_chunk_extra_edges(f, commits.list, commits.nr, progress, &progress_cnt);
1082 stop_progress(&progress);
1083 strbuf_release(&progress_title);
1085 close_commit_graph(the_repository);
1086 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1087 commit_lock_file(&lk);
1094 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1095 static int verify_commit_graph_error;
1097 static void graph_report(const char *fmt, ...)
1101 verify_commit_graph_error = 1;
1103 vfprintf(stderr, fmt, ap);
1104 fprintf(stderr, "\n");
1108 #define GENERATION_ZERO_EXISTS 1
1109 #define GENERATION_NUMBER_EXISTS 2
1111 int verify_commit_graph(struct repository *r, struct commit_graph *g)
1113 uint32_t i, cur_fanout_pos = 0;
1114 struct object_id prev_oid, cur_oid, checksum;
1115 int generation_zero = 0;
1118 struct progress *progress = NULL;
1121 graph_report("no commit-graph file loaded");
1125 verify_commit_graph_error = verify_commit_graph_lite(g);
1126 if (verify_commit_graph_error)
1127 return verify_commit_graph_error;
1129 devnull = open("/dev/null", O_WRONLY);
1130 f = hashfd(devnull, NULL);
1131 hashwrite(f, g->data, g->data_len - g->hash_len);
1132 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1133 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1134 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1135 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1138 for (i = 0; i < g->num_commits; i++) {
1139 struct commit *graph_commit;
1141 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1143 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1144 graph_report("commit-graph has incorrect OID order: %s then %s",
1145 oid_to_hex(&prev_oid),
1146 oid_to_hex(&cur_oid));
1148 oidcpy(&prev_oid, &cur_oid);
1150 while (cur_oid.hash[0] > cur_fanout_pos) {
1151 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1153 if (i != fanout_value)
1154 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1155 cur_fanout_pos, fanout_value, i);
1159 graph_commit = lookup_commit(r, &cur_oid);
1160 if (!parse_commit_in_graph_one(r, g, graph_commit))
1161 graph_report("failed to parse %s from commit-graph",
1162 oid_to_hex(&cur_oid));
1165 while (cur_fanout_pos < 256) {
1166 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1168 if (g->num_commits != fanout_value)
1169 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1170 cur_fanout_pos, fanout_value, i);
1175 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1176 return verify_commit_graph_error;
1178 progress = start_progress(_("Verifying commits in commit graph"),
1180 for (i = 0; i < g->num_commits; i++) {
1181 struct commit *graph_commit, *odb_commit;
1182 struct commit_list *graph_parents, *odb_parents;
1183 uint32_t max_generation = 0;
1185 display_progress(progress, i + 1);
1186 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1188 graph_commit = lookup_commit(r, &cur_oid);
1189 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1190 if (parse_commit_internal(odb_commit, 0, 0)) {
1191 graph_report("failed to parse %s from object database",
1192 oid_to_hex(&cur_oid));
1196 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
1197 get_commit_tree_oid(odb_commit)))
1198 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1199 oid_to_hex(&cur_oid),
1200 oid_to_hex(get_commit_tree_oid(graph_commit)),
1201 oid_to_hex(get_commit_tree_oid(odb_commit)));
1203 graph_parents = graph_commit->parents;
1204 odb_parents = odb_commit->parents;
1206 while (graph_parents) {
1207 if (odb_parents == NULL) {
1208 graph_report("commit-graph parent list for commit %s is too long",
1209 oid_to_hex(&cur_oid));
1213 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1214 graph_report("commit-graph parent for %s is %s != %s",
1215 oid_to_hex(&cur_oid),
1216 oid_to_hex(&graph_parents->item->object.oid),
1217 oid_to_hex(&odb_parents->item->object.oid));
1219 if (graph_parents->item->generation > max_generation)
1220 max_generation = graph_parents->item->generation;
1222 graph_parents = graph_parents->next;
1223 odb_parents = odb_parents->next;
1226 if (odb_parents != NULL)
1227 graph_report("commit-graph parent list for commit %s terminates early",
1228 oid_to_hex(&cur_oid));
1230 if (!graph_commit->generation) {
1231 if (generation_zero == GENERATION_NUMBER_EXISTS)
1232 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1233 oid_to_hex(&cur_oid));
1234 generation_zero = GENERATION_ZERO_EXISTS;
1235 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1236 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1237 oid_to_hex(&cur_oid));
1239 if (generation_zero == GENERATION_ZERO_EXISTS)
1243 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1244 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1245 * extra logic in the following condition.
1247 if (max_generation == GENERATION_NUMBER_MAX)
1250 if (graph_commit->generation != max_generation + 1)
1251 graph_report("commit-graph generation for commit %s is %u != %u",
1252 oid_to_hex(&cur_oid),
1253 graph_commit->generation,
1254 max_generation + 1);
1256 if (graph_commit->date != odb_commit->date)
1257 graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1258 oid_to_hex(&cur_oid),
1262 stop_progress(&progress);
1264 return verify_commit_graph_error;
1267 void free_commit_graph(struct commit_graph *g)
1271 if (g->graph_fd >= 0) {
1272 munmap((void *)g->data, g->data_len);