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 char *get_split_graph_filename(const char *obj_dir,
51 return xstrfmt("%s/info/commit-graphs/graph-%s.graph",
56 static char *get_chain_filename(const char *obj_dir)
58 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
61 static uint8_t oid_version(void)
66 static struct commit_graph *alloc_commit_graph(void)
68 struct commit_graph *g = xcalloc(1, sizeof(*g));
74 extern int read_replace_refs;
76 static int commit_graph_compatible(struct repository *r)
81 if (read_replace_refs) {
82 prepare_replace_object(r);
83 if (hashmap_get_size(&r->objects->replace_map->map))
87 prepare_commit_graft(r);
88 if (r->parsed_objects && r->parsed_objects->grafts_nr)
90 if (is_repository_shallow(r))
96 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
98 *fd = git_open(graph_file);
101 if (fstat(*fd, st)) {
108 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
112 struct commit_graph *ret;
114 graph_size = xsize_t(st->st_size);
116 if (graph_size < GRAPH_MIN_SIZE) {
118 error(_("commit-graph file is too small"));
121 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
122 ret = parse_commit_graph(graph_map, fd, graph_size);
125 munmap(graph_map, graph_size);
132 static int verify_commit_graph_lite(struct commit_graph *g)
135 * Basic validation shared between parse_commit_graph()
136 * which'll be called every time the graph is used, and the
137 * much more expensive verify_commit_graph() used by
138 * "commit-graph verify".
140 * There should only be very basic checks here to ensure that
141 * we don't e.g. segfault in fill_commit_in_graph(), but
142 * because this is a very hot codepath nothing that e.g. loops
143 * over g->num_commits, or runs a checksum on the commit-graph
146 if (!g->chunk_oid_fanout) {
147 error("commit-graph is missing the OID Fanout chunk");
150 if (!g->chunk_oid_lookup) {
151 error("commit-graph is missing the OID Lookup chunk");
154 if (!g->chunk_commit_data) {
155 error("commit-graph is missing the Commit Data chunk");
162 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
165 const unsigned char *data, *chunk_lookup;
167 struct commit_graph *graph;
168 uint64_t last_chunk_offset;
169 uint32_t last_chunk_id;
170 uint32_t graph_signature;
171 unsigned char graph_version, hash_version;
176 if (graph_size < GRAPH_MIN_SIZE)
179 data = (const unsigned char *)graph_map;
181 graph_signature = get_be32(data);
182 if (graph_signature != GRAPH_SIGNATURE) {
183 error(_("commit-graph signature %X does not match signature %X"),
184 graph_signature, GRAPH_SIGNATURE);
188 graph_version = *(unsigned char*)(data + 4);
189 if (graph_version != GRAPH_VERSION) {
190 error(_("commit-graph version %X does not match version %X"),
191 graph_version, GRAPH_VERSION);
195 hash_version = *(unsigned char*)(data + 5);
196 if (hash_version != oid_version()) {
197 error(_("commit-graph hash version %X does not match version %X"),
198 hash_version, oid_version());
202 graph = alloc_commit_graph();
204 graph->hash_len = the_hash_algo->rawsz;
205 graph->num_chunks = *(unsigned char*)(data + 6);
206 graph->graph_fd = fd;
207 graph->data = graph_map;
208 graph->data_len = graph_size;
211 last_chunk_offset = 8;
212 chunk_lookup = data + 8;
213 for (i = 0; i < graph->num_chunks; i++) {
215 uint64_t chunk_offset;
216 int chunk_repeated = 0;
218 if (data + graph_size - chunk_lookup <
219 GRAPH_CHUNKLOOKUP_WIDTH) {
220 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
225 chunk_id = get_be32(chunk_lookup + 0);
226 chunk_offset = get_be64(chunk_lookup + 4);
228 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
230 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
231 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
232 (uint32_t)chunk_offset);
238 case GRAPH_CHUNKID_OIDFANOUT:
239 if (graph->chunk_oid_fanout)
242 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
245 case GRAPH_CHUNKID_OIDLOOKUP:
246 if (graph->chunk_oid_lookup)
249 graph->chunk_oid_lookup = data + chunk_offset;
252 case GRAPH_CHUNKID_DATA:
253 if (graph->chunk_commit_data)
256 graph->chunk_commit_data = data + chunk_offset;
259 case GRAPH_CHUNKID_EXTRAEDGES:
260 if (graph->chunk_extra_edges)
263 graph->chunk_extra_edges = data + chunk_offset;
267 if (chunk_repeated) {
268 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
273 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
275 graph->num_commits = (chunk_offset - last_chunk_offset)
279 last_chunk_id = chunk_id;
280 last_chunk_offset = chunk_offset;
283 if (verify_commit_graph_lite(graph))
289 static struct commit_graph *load_commit_graph_one(const char *graph_file)
294 int open_ok = open_commit_graph(graph_file, &fd, &st);
299 return load_commit_graph_one_fd_st(fd, &st);
302 static struct commit_graph *load_commit_graph_v1(struct repository *r, const char *obj_dir)
304 char *graph_name = get_commit_graph_filename(obj_dir);
305 struct commit_graph *g = load_commit_graph_one(graph_name);
311 static int add_graph_to_chain(struct commit_graph *g,
312 struct commit_graph *chain,
313 struct object_id *oids,
316 struct commit_graph *cur_g = chain;
320 cur_g = cur_g->base_graph;
323 g->base_graph = chain;
326 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
331 static struct commit_graph *load_commit_graph_chain(struct repository *r, const char *obj_dir)
333 struct commit_graph *graph_chain = NULL;
334 struct strbuf line = STRBUF_INIT;
336 struct object_id *oids;
337 int i = 0, valid = 1, count;
338 char *chain_name = get_chain_filename(obj_dir);
342 fp = fopen(chain_name, "r");
343 stat_res = stat(chain_name, &st);
348 st.st_size <= the_hash_algo->hexsz)
351 count = st.st_size / (the_hash_algo->hexsz + 1);
352 oids = xcalloc(count, sizeof(struct object_id));
354 for (i = 0; i < count && valid; i++) {
356 struct commit_graph *g;
358 if (strbuf_getline_lf(&line, fp) == EOF)
361 if (get_oid_hex(line.buf, &oids[i])) {
362 warning(_("invalid commit-graph chain: line '%s' not a hash"),
368 graph_name = get_split_graph_filename(obj_dir, line.buf);
369 g = load_commit_graph_one(graph_name);
372 if (g && add_graph_to_chain(g, graph_chain, oids, i))
384 static struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
386 struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
389 g = load_commit_graph_chain(r, obj_dir);
394 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
397 if (r->objects->commit_graph)
400 r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
404 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
406 * On the first invocation, this function attemps to load the commit
407 * graph if the_repository is configured to have one.
409 static int prepare_commit_graph(struct repository *r)
411 struct object_directory *odb;
414 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
415 die("dying as requested by the '%s' variable on commit-graph load!",
416 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
418 if (r->objects->commit_graph_attempted)
419 return !!r->objects->commit_graph;
420 r->objects->commit_graph_attempted = 1;
422 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
423 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
426 * This repository is not configured to use commit graphs, so
427 * do not load one. (But report commit_graph_attempted anyway
428 * so that commit graph loading is not attempted again for this
433 if (!commit_graph_compatible(r))
437 for (odb = r->objects->odb;
438 !r->objects->commit_graph && odb;
440 prepare_commit_graph_one(r, odb->path);
441 return !!r->objects->commit_graph;
444 int generation_numbers_enabled(struct repository *r)
446 uint32_t first_generation;
447 struct commit_graph *g;
448 if (!prepare_commit_graph(r))
451 g = r->objects->commit_graph;
456 first_generation = get_be32(g->chunk_commit_data +
457 g->hash_len + 8) >> 2;
459 return !!first_generation;
462 static void close_commit_graph_one(struct commit_graph *g)
467 close_commit_graph_one(g->base_graph);
468 free_commit_graph(g);
471 void close_commit_graph(struct raw_object_store *o)
473 close_commit_graph_one(o->commit_graph);
474 o->commit_graph = NULL;
477 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
479 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
480 g->chunk_oid_lookup, g->hash_len, pos);
483 static void load_oid_from_graph(struct commit_graph *g,
485 struct object_id *oid)
489 while (g && pos < g->num_commits_in_base)
493 BUG("NULL commit-graph");
495 if (pos >= g->num_commits + g->num_commits_in_base)
496 die(_("invalid commit position. commit-graph is likely corrupt"));
498 lex_index = pos - g->num_commits_in_base;
500 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
503 static struct commit_list **insert_parent_or_die(struct repository *r,
504 struct commit_graph *g,
506 struct commit_list **pptr)
509 struct object_id oid;
511 if (pos >= g->num_commits + g->num_commits_in_base)
512 die("invalid parent position %"PRIu32, pos);
514 load_oid_from_graph(g, pos, &oid);
515 c = lookup_commit(r, &oid);
517 die(_("could not find commit %s"), oid_to_hex(&oid));
519 return &commit_list_insert(c, pptr)->next;
522 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
524 const unsigned char *commit_data;
527 while (pos < g->num_commits_in_base)
530 lex_index = pos - g->num_commits_in_base;
531 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
532 item->graph_pos = pos;
533 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
536 static int fill_commit_in_graph(struct repository *r,
538 struct commit_graph *g, uint32_t pos)
541 uint32_t *parent_data_ptr;
542 uint64_t date_low, date_high;
543 struct commit_list **pptr;
544 const unsigned char *commit_data;
547 while (pos < g->num_commits_in_base)
550 if (pos >= g->num_commits + g->num_commits_in_base)
551 die(_("invalid commit position. commit-graph is likely corrupt"));
554 * Store the "full" position, but then use the
555 * "local" position for the rest of the calculation.
557 item->graph_pos = pos;
558 lex_index = pos - g->num_commits_in_base;
560 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
562 item->object.parsed = 1;
564 item->maybe_tree = NULL;
566 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
567 date_low = get_be32(commit_data + g->hash_len + 12);
568 item->date = (timestamp_t)((date_high << 32) | date_low);
570 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
572 pptr = &item->parents;
574 edge_value = get_be32(commit_data + g->hash_len);
575 if (edge_value == GRAPH_PARENT_NONE)
577 pptr = insert_parent_or_die(r, g, edge_value, pptr);
579 edge_value = get_be32(commit_data + g->hash_len + 4);
580 if (edge_value == GRAPH_PARENT_NONE)
582 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
583 pptr = insert_parent_or_die(r, g, edge_value, pptr);
587 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
588 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
590 edge_value = get_be32(parent_data_ptr);
591 pptr = insert_parent_or_die(r, g,
592 edge_value & GRAPH_EDGE_LAST_MASK,
595 } while (!(edge_value & GRAPH_LAST_EDGE));
600 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
602 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
603 *pos = item->graph_pos;
606 struct commit_graph *cur_g = g;
609 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
610 cur_g = cur_g->base_graph;
613 *pos = lex_index + cur_g->num_commits_in_base;
621 static int parse_commit_in_graph_one(struct repository *r,
622 struct commit_graph *g,
627 if (item->object.parsed)
630 if (find_commit_in_graph(item, g, &pos))
631 return fill_commit_in_graph(r, item, g, pos);
636 int parse_commit_in_graph(struct repository *r, struct commit *item)
638 if (!prepare_commit_graph(r))
640 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
643 void load_commit_graph_info(struct repository *r, struct commit *item)
646 if (!prepare_commit_graph(r))
648 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
649 fill_commit_graph_info(item, r->objects->commit_graph, pos);
652 static struct tree *load_tree_for_commit(struct repository *r,
653 struct commit_graph *g,
656 struct object_id oid;
657 const unsigned char *commit_data;
659 while (c->graph_pos < g->num_commits_in_base)
662 commit_data = g->chunk_commit_data +
663 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
665 hashcpy(oid.hash, commit_data);
666 c->maybe_tree = lookup_tree(r, &oid);
668 return c->maybe_tree;
671 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
672 struct commit_graph *g,
673 const struct commit *c)
676 return c->maybe_tree;
677 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
678 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
680 return load_tree_for_commit(r, g, (struct commit *)c);
683 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
685 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
688 struct packed_commit_list {
689 struct commit **list;
694 struct packed_oid_list {
695 struct object_id *list;
700 struct write_commit_graph_context {
701 struct repository *r;
704 struct packed_oid_list oids;
705 struct packed_commit_list commits;
707 unsigned long approx_nr_objects;
708 struct progress *progress;
710 uint64_t progress_cnt;
715 static void write_graph_chunk_fanout(struct hashfile *f,
716 struct write_commit_graph_context *ctx)
719 struct commit **list = ctx->commits.list;
722 * Write the first-level table (the list is sorted,
723 * but we use a 256-entry lookup to be able to avoid
724 * having to do eight extra binary search iterations).
726 for (i = 0; i < 256; i++) {
727 while (count < ctx->commits.nr) {
728 if ((*list)->object.oid.hash[0] != i)
730 display_progress(ctx->progress, ++ctx->progress_cnt);
735 hashwrite_be32(f, count);
739 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
740 struct write_commit_graph_context *ctx)
742 struct commit **list = ctx->commits.list;
744 for (count = 0; count < ctx->commits.nr; count++, list++) {
745 display_progress(ctx->progress, ++ctx->progress_cnt);
746 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
750 static const unsigned char *commit_to_sha1(size_t index, void *table)
752 struct commit **commits = table;
753 return commits[index]->object.oid.hash;
756 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
757 struct write_commit_graph_context *ctx)
759 struct commit **list = ctx->commits.list;
760 struct commit **last = ctx->commits.list + ctx->commits.nr;
761 uint32_t num_extra_edges = 0;
763 while (list < last) {
764 struct commit_list *parent;
766 uint32_t packedDate[2];
767 display_progress(ctx->progress, ++ctx->progress_cnt);
769 parse_commit_no_graph(*list);
770 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
772 parent = (*list)->parents;
775 edge_value = GRAPH_PARENT_NONE;
777 edge_value = sha1_pos(parent->item->object.oid.hash,
783 BUG("missing parent %s for commit %s",
784 oid_to_hex(&parent->item->object.oid),
785 oid_to_hex(&(*list)->object.oid));
788 hashwrite_be32(f, edge_value);
791 parent = parent->next;
794 edge_value = GRAPH_PARENT_NONE;
795 else if (parent->next)
796 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
798 edge_value = sha1_pos(parent->item->object.oid.hash,
803 BUG("missing parent %s for commit %s",
804 oid_to_hex(&parent->item->object.oid),
805 oid_to_hex(&(*list)->object.oid));
808 hashwrite_be32(f, edge_value);
810 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
813 parent = parent->next;
817 if (sizeof((*list)->date) > 4)
818 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
822 packedDate[0] |= htonl((*list)->generation << 2);
824 packedDate[1] = htonl((*list)->date);
825 hashwrite(f, packedDate, 8);
831 static void write_graph_chunk_extra_edges(struct hashfile *f,
832 struct write_commit_graph_context *ctx)
834 struct commit **list = ctx->commits.list;
835 struct commit **last = ctx->commits.list + ctx->commits.nr;
836 struct commit_list *parent;
838 while (list < last) {
841 display_progress(ctx->progress, ++ctx->progress_cnt);
843 for (parent = (*list)->parents; num_parents < 3 && parent;
844 parent = parent->next)
847 if (num_parents <= 2) {
852 /* Since num_parents > 2, this initializer is safe. */
853 for (parent = (*list)->parents->next; parent; parent = parent->next) {
854 int edge_value = sha1_pos(parent->item->object.oid.hash,
860 BUG("missing parent %s for commit %s",
861 oid_to_hex(&parent->item->object.oid),
862 oid_to_hex(&(*list)->object.oid));
863 else if (!parent->next)
864 edge_value |= GRAPH_LAST_EDGE;
866 hashwrite_be32(f, edge_value);
873 static int oid_compare(const void *_a, const void *_b)
875 const struct object_id *a = (const struct object_id *)_a;
876 const struct object_id *b = (const struct object_id *)_b;
880 static int add_packed_commits(const struct object_id *oid,
881 struct packed_git *pack,
885 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
886 enum object_type type;
887 off_t offset = nth_packed_object_offset(pack, pos);
888 struct object_info oi = OBJECT_INFO_INIT;
891 display_progress(ctx->progress, ++ctx->progress_done);
894 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
895 die(_("unable to get type of object %s"), oid_to_hex(oid));
897 if (type != OBJ_COMMIT)
900 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
901 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
907 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
909 struct commit_list *parent;
910 for (parent = commit->parents; parent; parent = parent->next) {
911 if (!(parent->item->object.flags & UNINTERESTING)) {
912 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
913 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
915 parent->item->object.flags |= UNINTERESTING;
920 static void close_reachable(struct write_commit_graph_context *ctx)
923 struct commit *commit;
925 if (ctx->report_progress)
926 ctx->progress = start_delayed_progress(
927 _("Loading known commits in commit graph"),
929 for (i = 0; i < ctx->oids.nr; i++) {
930 display_progress(ctx->progress, i + 1);
931 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
933 commit->object.flags |= UNINTERESTING;
935 stop_progress(&ctx->progress);
938 * As this loop runs, ctx->oids.nr may grow, but not more
939 * than the number of missing commits in the reachable
942 if (ctx->report_progress)
943 ctx->progress = start_delayed_progress(
944 _("Expanding reachable commits in commit graph"),
946 for (i = 0; i < ctx->oids.nr; i++) {
947 display_progress(ctx->progress, i + 1);
948 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
950 if (commit && !parse_commit_no_graph(commit))
951 add_missing_parents(ctx, commit);
953 stop_progress(&ctx->progress);
955 if (ctx->report_progress)
956 ctx->progress = start_delayed_progress(
957 _("Clearing commit marks in commit graph"),
959 for (i = 0; i < ctx->oids.nr; i++) {
960 display_progress(ctx->progress, i + 1);
961 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
964 commit->object.flags &= ~UNINTERESTING;
966 stop_progress(&ctx->progress);
969 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
972 struct commit_list *list = NULL;
974 if (ctx->report_progress)
975 ctx->progress = start_progress(
976 _("Computing commit graph generation numbers"),
978 for (i = 0; i < ctx->commits.nr; i++) {
979 display_progress(ctx->progress, i + 1);
980 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
981 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
984 commit_list_insert(ctx->commits.list[i], &list);
986 struct commit *current = list->item;
987 struct commit_list *parent;
988 int all_parents_computed = 1;
989 uint32_t max_generation = 0;
991 for (parent = current->parents; parent; parent = parent->next) {
992 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
993 parent->item->generation == GENERATION_NUMBER_ZERO) {
994 all_parents_computed = 0;
995 commit_list_insert(parent->item, &list);
997 } else if (parent->item->generation > max_generation) {
998 max_generation = parent->item->generation;
1002 if (all_parents_computed) {
1003 current->generation = max_generation + 1;
1006 if (current->generation > GENERATION_NUMBER_MAX)
1007 current->generation = GENERATION_NUMBER_MAX;
1011 stop_progress(&ctx->progress);
1014 static int add_ref_to_list(const char *refname,
1015 const struct object_id *oid,
1016 int flags, void *cb_data)
1018 struct string_list *list = (struct string_list *)cb_data;
1020 string_list_append(list, oid_to_hex(oid));
1024 int write_commit_graph_reachable(const char *obj_dir, unsigned int flags)
1026 struct string_list list = STRING_LIST_INIT_DUP;
1029 for_each_ref(add_ref_to_list, &list);
1030 result = write_commit_graph(obj_dir, NULL, &list,
1033 string_list_clear(&list, 0);
1037 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1038 struct string_list *pack_indexes)
1041 struct strbuf progress_title = STRBUF_INIT;
1042 struct strbuf packname = STRBUF_INIT;
1045 strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
1046 dirlen = packname.len;
1047 if (ctx->report_progress) {
1048 strbuf_addf(&progress_title,
1049 Q_("Finding commits for commit graph in %d pack",
1050 "Finding commits for commit graph in %d packs",
1053 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1054 ctx->progress_done = 0;
1056 for (i = 0; i < pack_indexes->nr; i++) {
1057 struct packed_git *p;
1058 strbuf_setlen(&packname, dirlen);
1059 strbuf_addstr(&packname, pack_indexes->items[i].string);
1060 p = add_packed_git(packname.buf, packname.len, 1);
1062 error(_("error adding pack %s"), packname.buf);
1065 if (open_pack_index(p)) {
1066 error(_("error opening index for %s"), packname.buf);
1069 for_each_object_in_pack(p, add_packed_commits, ctx,
1070 FOR_EACH_OBJECT_PACK_ORDER);
1075 stop_progress(&ctx->progress);
1076 strbuf_reset(&progress_title);
1077 strbuf_release(&packname);
1082 static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1083 struct string_list *commit_hex)
1086 struct strbuf progress_title = STRBUF_INIT;
1088 if (ctx->report_progress) {
1089 strbuf_addf(&progress_title,
1090 Q_("Finding commits for commit graph from %d ref",
1091 "Finding commits for commit graph from %d refs",
1094 ctx->progress = start_delayed_progress(
1098 for (i = 0; i < commit_hex->nr; i++) {
1100 struct object_id oid;
1101 struct commit *result;
1103 display_progress(ctx->progress, i + 1);
1104 if (commit_hex->items[i].string &&
1105 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
1108 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1111 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1112 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1116 stop_progress(&ctx->progress);
1117 strbuf_release(&progress_title);
1120 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1122 if (ctx->report_progress)
1123 ctx->progress = start_delayed_progress(
1124 _("Finding commits for commit graph among packed objects"),
1125 ctx->approx_nr_objects);
1126 for_each_packed_object(add_packed_commits, ctx,
1127 FOR_EACH_OBJECT_PACK_ORDER);
1128 if (ctx->progress_done < ctx->approx_nr_objects)
1129 display_progress(ctx->progress, ctx->approx_nr_objects);
1130 stop_progress(&ctx->progress);
1133 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1135 uint32_t i, count_distinct = 1;
1137 if (ctx->report_progress)
1138 ctx->progress = start_delayed_progress(
1139 _("Counting distinct commits in commit graph"),
1141 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1142 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1144 for (i = 1; i < ctx->oids.nr; i++) {
1145 display_progress(ctx->progress, i + 1);
1146 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1149 stop_progress(&ctx->progress);
1151 return count_distinct;
1154 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1157 struct commit_list *parent;
1159 ctx->num_extra_edges = 0;
1160 if (ctx->report_progress)
1161 ctx->progress = start_delayed_progress(
1162 _("Finding extra edges in commit graph"),
1164 for (i = 0; i < ctx->oids.nr; i++) {
1165 int num_parents = 0;
1166 display_progress(ctx->progress, i + 1);
1167 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1170 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1171 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1173 for (parent = ctx->commits.list[ctx->commits.nr]->parents;
1174 parent; parent = parent->next)
1177 if (num_parents > 2)
1178 ctx->num_extra_edges += num_parents - 1;
1182 stop_progress(&ctx->progress);
1185 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1189 struct lock_file lk = LOCK_INIT;
1190 uint32_t chunk_ids[5];
1191 uint64_t chunk_offsets[5];
1192 const unsigned hashsz = the_hash_algo->rawsz;
1193 struct strbuf progress_title = STRBUF_INIT;
1194 int num_chunks = ctx->num_extra_edges ? 4 : 3;
1196 ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1197 if (safe_create_leading_directories(ctx->graph_name)) {
1198 UNLEAK(ctx->graph_name);
1199 error(_("unable to create leading directories of %s"),
1204 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1205 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1207 hashwrite_be32(f, GRAPH_SIGNATURE);
1209 hashwrite_u8(f, GRAPH_VERSION);
1210 hashwrite_u8(f, oid_version());
1211 hashwrite_u8(f, num_chunks);
1212 hashwrite_u8(f, 0); /* unused padding byte */
1214 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1215 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1216 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1217 if (ctx->num_extra_edges)
1218 chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
1223 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1224 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1225 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1226 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1227 chunk_offsets[4] = chunk_offsets[3] + 4 * ctx->num_extra_edges;
1229 for (i = 0; i <= num_chunks; i++) {
1230 uint32_t chunk_write[3];
1232 chunk_write[0] = htonl(chunk_ids[i]);
1233 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1234 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1235 hashwrite(f, chunk_write, 12);
1238 if (ctx->report_progress) {
1239 strbuf_addf(&progress_title,
1240 Q_("Writing out commit graph in %d pass",
1241 "Writing out commit graph in %d passes",
1244 ctx->progress = start_delayed_progress(
1246 num_chunks * ctx->commits.nr);
1248 write_graph_chunk_fanout(f, ctx);
1249 write_graph_chunk_oids(f, hashsz, ctx);
1250 write_graph_chunk_data(f, hashsz, ctx);
1251 if (ctx->num_extra_edges)
1252 write_graph_chunk_extra_edges(f, ctx);
1253 stop_progress(&ctx->progress);
1254 strbuf_release(&progress_title);
1256 close_commit_graph(ctx->r->objects);
1257 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1258 commit_lock_file(&lk);
1263 int write_commit_graph(const char *obj_dir,
1264 struct string_list *pack_indexes,
1265 struct string_list *commit_hex,
1268 struct write_commit_graph_context *ctx;
1269 uint32_t i, count_distinct = 0;
1272 if (!commit_graph_compatible(the_repository))
1275 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1276 ctx->r = the_repository;
1277 ctx->obj_dir = obj_dir;
1278 ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
1279 ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
1281 ctx->approx_nr_objects = approximate_object_count();
1282 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1285 prepare_commit_graph_one(ctx->r, ctx->obj_dir);
1286 if (ctx->r->objects->commit_graph)
1287 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
1290 if (ctx->oids.alloc < 1024)
1291 ctx->oids.alloc = 1024;
1292 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1294 if (ctx->append && ctx->r->objects->commit_graph) {
1295 struct commit_graph *g = ctx->r->objects->commit_graph;
1296 for (i = 0; i < g->num_commits; i++) {
1297 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1298 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
1303 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1308 fill_oids_from_commit_hex(ctx, commit_hex);
1310 if (!pack_indexes && !commit_hex)
1311 fill_oids_from_all_packs(ctx);
1313 close_reachable(ctx);
1315 count_distinct = count_distinct_commits(ctx);
1317 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1318 error(_("the commit graph format cannot write %d commits"), count_distinct);
1323 ctx->commits.alloc = count_distinct;
1324 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
1326 copy_oids_to_commits(ctx);
1328 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
1329 error(_("too many commits to write graph"));
1334 compute_generation_numbers(ctx);
1336 res = write_commit_graph_file(ctx);
1339 free(ctx->graph_name);
1340 free(ctx->commits.list);
1341 free(ctx->oids.list);
1347 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1348 static int verify_commit_graph_error;
1350 static void graph_report(const char *fmt, ...)
1354 verify_commit_graph_error = 1;
1356 vfprintf(stderr, fmt, ap);
1357 fprintf(stderr, "\n");
1361 #define GENERATION_ZERO_EXISTS 1
1362 #define GENERATION_NUMBER_EXISTS 2
1364 int verify_commit_graph(struct repository *r, struct commit_graph *g)
1366 uint32_t i, cur_fanout_pos = 0;
1367 struct object_id prev_oid, cur_oid, checksum;
1368 int generation_zero = 0;
1371 struct progress *progress = NULL;
1374 graph_report("no commit-graph file loaded");
1378 verify_commit_graph_error = verify_commit_graph_lite(g);
1379 if (verify_commit_graph_error)
1380 return verify_commit_graph_error;
1382 devnull = open("/dev/null", O_WRONLY);
1383 f = hashfd(devnull, NULL);
1384 hashwrite(f, g->data, g->data_len - g->hash_len);
1385 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1386 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1387 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1388 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1391 for (i = 0; i < g->num_commits; i++) {
1392 struct commit *graph_commit;
1394 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1396 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1397 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1398 oid_to_hex(&prev_oid),
1399 oid_to_hex(&cur_oid));
1401 oidcpy(&prev_oid, &cur_oid);
1403 while (cur_oid.hash[0] > cur_fanout_pos) {
1404 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1406 if (i != fanout_value)
1407 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1408 cur_fanout_pos, fanout_value, i);
1412 graph_commit = lookup_commit(r, &cur_oid);
1413 if (!parse_commit_in_graph_one(r, g, graph_commit))
1414 graph_report(_("failed to parse commit %s from commit-graph"),
1415 oid_to_hex(&cur_oid));
1418 while (cur_fanout_pos < 256) {
1419 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1421 if (g->num_commits != fanout_value)
1422 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1423 cur_fanout_pos, fanout_value, i);
1428 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1429 return verify_commit_graph_error;
1431 progress = start_progress(_("Verifying commits in commit graph"),
1433 for (i = 0; i < g->num_commits; i++) {
1434 struct commit *graph_commit, *odb_commit;
1435 struct commit_list *graph_parents, *odb_parents;
1436 uint32_t max_generation = 0;
1438 display_progress(progress, i + 1);
1439 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1441 graph_commit = lookup_commit(r, &cur_oid);
1442 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1443 if (parse_commit_internal(odb_commit, 0, 0)) {
1444 graph_report(_("failed to parse commit %s from object database for commit-graph"),
1445 oid_to_hex(&cur_oid));
1449 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
1450 get_commit_tree_oid(odb_commit)))
1451 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
1452 oid_to_hex(&cur_oid),
1453 oid_to_hex(get_commit_tree_oid(graph_commit)),
1454 oid_to_hex(get_commit_tree_oid(odb_commit)));
1456 graph_parents = graph_commit->parents;
1457 odb_parents = odb_commit->parents;
1459 while (graph_parents) {
1460 if (odb_parents == NULL) {
1461 graph_report(_("commit-graph parent list for commit %s is too long"),
1462 oid_to_hex(&cur_oid));
1466 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1467 graph_report(_("commit-graph parent for %s is %s != %s"),
1468 oid_to_hex(&cur_oid),
1469 oid_to_hex(&graph_parents->item->object.oid),
1470 oid_to_hex(&odb_parents->item->object.oid));
1472 if (graph_parents->item->generation > max_generation)
1473 max_generation = graph_parents->item->generation;
1475 graph_parents = graph_parents->next;
1476 odb_parents = odb_parents->next;
1479 if (odb_parents != NULL)
1480 graph_report(_("commit-graph parent list for commit %s terminates early"),
1481 oid_to_hex(&cur_oid));
1483 if (!graph_commit->generation) {
1484 if (generation_zero == GENERATION_NUMBER_EXISTS)
1485 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
1486 oid_to_hex(&cur_oid));
1487 generation_zero = GENERATION_ZERO_EXISTS;
1488 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1489 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
1490 oid_to_hex(&cur_oid));
1492 if (generation_zero == GENERATION_ZERO_EXISTS)
1496 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1497 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1498 * extra logic in the following condition.
1500 if (max_generation == GENERATION_NUMBER_MAX)
1503 if (graph_commit->generation != max_generation + 1)
1504 graph_report(_("commit-graph generation for commit %s is %u != %u"),
1505 oid_to_hex(&cur_oid),
1506 graph_commit->generation,
1507 max_generation + 1);
1509 if (graph_commit->date != odb_commit->date)
1510 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
1511 oid_to_hex(&cur_oid),
1515 stop_progress(&progress);
1517 return verify_commit_graph_error;
1520 void free_commit_graph(struct commit_graph *g)
1524 if (g->graph_fd >= 0) {
1525 munmap((void *)g->data, g->data_len);