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" */
25 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
27 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
29 #define GRAPH_VERSION_1 0x1
30 #define GRAPH_VERSION GRAPH_VERSION_1
32 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
33 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
34 #define GRAPH_PARENT_NONE 0x70000000
36 #define GRAPH_LAST_EDGE 0x80000000
38 #define GRAPH_HEADER_SIZE 8
39 #define GRAPH_FANOUT_SIZE (4 * 256)
40 #define GRAPH_CHUNKLOOKUP_WIDTH 12
41 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
42 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
44 /* Remember to update object flag allocation in object.h */
45 #define REACHABLE (1u<<15)
47 char *get_commit_graph_filename(const char *obj_dir)
49 char *filename = xstrfmt("%s/info/commit-graph", obj_dir);
50 char *normalized = xmalloc(strlen(filename) + 1);
51 normalize_path_copy(normalized, filename);
56 static char *get_split_graph_filename(const char *obj_dir,
59 char *filename = xstrfmt("%s/info/commit-graphs/graph-%s.graph",
62 char *normalized = xmalloc(strlen(filename) + 1);
63 normalize_path_copy(normalized, filename);
68 static char *get_chain_filename(const char *obj_dir)
70 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
73 static uint8_t oid_version(void)
78 static struct commit_graph *alloc_commit_graph(void)
80 struct commit_graph *g = xcalloc(1, sizeof(*g));
86 extern int read_replace_refs;
88 static int commit_graph_compatible(struct repository *r)
93 if (read_replace_refs) {
94 prepare_replace_object(r);
95 if (hashmap_get_size(&r->objects->replace_map->map))
99 prepare_commit_graft(r);
100 if (r->parsed_objects && r->parsed_objects->grafts_nr)
102 if (is_repository_shallow(r))
108 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
110 *fd = git_open(graph_file);
113 if (fstat(*fd, st)) {
120 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
124 struct commit_graph *ret;
126 graph_size = xsize_t(st->st_size);
128 if (graph_size < GRAPH_MIN_SIZE) {
130 error(_("commit-graph file is too small"));
133 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
134 ret = parse_commit_graph(graph_map, fd, graph_size);
137 munmap(graph_map, graph_size);
144 static int verify_commit_graph_lite(struct commit_graph *g)
147 * Basic validation shared between parse_commit_graph()
148 * which'll be called every time the graph is used, and the
149 * much more expensive verify_commit_graph() used by
150 * "commit-graph verify".
152 * There should only be very basic checks here to ensure that
153 * we don't e.g. segfault in fill_commit_in_graph(), but
154 * because this is a very hot codepath nothing that e.g. loops
155 * over g->num_commits, or runs a checksum on the commit-graph
158 if (!g->chunk_oid_fanout) {
159 error("commit-graph is missing the OID Fanout chunk");
162 if (!g->chunk_oid_lookup) {
163 error("commit-graph is missing the OID Lookup chunk");
166 if (!g->chunk_commit_data) {
167 error("commit-graph is missing the Commit Data chunk");
174 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
177 const unsigned char *data, *chunk_lookup;
179 struct commit_graph *graph;
180 uint64_t last_chunk_offset;
181 uint32_t last_chunk_id;
182 uint32_t graph_signature;
183 unsigned char graph_version, hash_version;
188 if (graph_size < GRAPH_MIN_SIZE)
191 data = (const unsigned char *)graph_map;
193 graph_signature = get_be32(data);
194 if (graph_signature != GRAPH_SIGNATURE) {
195 error(_("commit-graph signature %X does not match signature %X"),
196 graph_signature, GRAPH_SIGNATURE);
200 graph_version = *(unsigned char*)(data + 4);
201 if (graph_version != GRAPH_VERSION) {
202 error(_("commit-graph version %X does not match version %X"),
203 graph_version, GRAPH_VERSION);
207 hash_version = *(unsigned char*)(data + 5);
208 if (hash_version != oid_version()) {
209 error(_("commit-graph hash version %X does not match version %X"),
210 hash_version, oid_version());
214 graph = alloc_commit_graph();
216 graph->hash_len = the_hash_algo->rawsz;
217 graph->num_chunks = *(unsigned char*)(data + 6);
218 graph->graph_fd = fd;
219 graph->data = graph_map;
220 graph->data_len = graph_size;
223 last_chunk_offset = 8;
224 chunk_lookup = data + 8;
225 for (i = 0; i < graph->num_chunks; i++) {
227 uint64_t chunk_offset;
228 int chunk_repeated = 0;
230 if (data + graph_size - chunk_lookup <
231 GRAPH_CHUNKLOOKUP_WIDTH) {
232 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
237 chunk_id = get_be32(chunk_lookup + 0);
238 chunk_offset = get_be64(chunk_lookup + 4);
240 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
242 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
243 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
244 (uint32_t)chunk_offset);
250 case GRAPH_CHUNKID_OIDFANOUT:
251 if (graph->chunk_oid_fanout)
254 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
257 case GRAPH_CHUNKID_OIDLOOKUP:
258 if (graph->chunk_oid_lookup)
261 graph->chunk_oid_lookup = data + chunk_offset;
264 case GRAPH_CHUNKID_DATA:
265 if (graph->chunk_commit_data)
268 graph->chunk_commit_data = data + chunk_offset;
271 case GRAPH_CHUNKID_EXTRAEDGES:
272 if (graph->chunk_extra_edges)
275 graph->chunk_extra_edges = data + chunk_offset;
278 case GRAPH_CHUNKID_BASE:
279 if (graph->chunk_base_graphs)
282 graph->chunk_base_graphs = data + chunk_offset;
285 if (chunk_repeated) {
286 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
291 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
293 graph->num_commits = (chunk_offset - last_chunk_offset)
297 last_chunk_id = chunk_id;
298 last_chunk_offset = chunk_offset;
301 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
303 if (verify_commit_graph_lite(graph)) {
311 static struct commit_graph *load_commit_graph_one(const char *graph_file)
316 struct commit_graph *g;
317 int open_ok = open_commit_graph(graph_file, &fd, &st);
322 g = load_commit_graph_one_fd_st(fd, &st);
325 g->filename = xstrdup(graph_file);
330 static struct commit_graph *load_commit_graph_v1(struct repository *r,
331 struct object_directory *odb)
333 char *graph_name = get_commit_graph_filename(odb->path);
334 struct commit_graph *g = load_commit_graph_one(graph_name);
343 static int add_graph_to_chain(struct commit_graph *g,
344 struct commit_graph *chain,
345 struct object_id *oids,
348 struct commit_graph *cur_g = chain;
350 if (n && !g->chunk_base_graphs) {
351 warning(_("commit-graph has no base graphs chunk"));
359 !oideq(&oids[n], &cur_g->oid) ||
360 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
361 warning(_("commit-graph chain does not match"));
365 cur_g = cur_g->base_graph;
368 g->base_graph = chain;
371 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
376 static struct commit_graph *load_commit_graph_chain(struct repository *r,
377 struct object_directory *odb)
379 struct commit_graph *graph_chain = NULL;
380 struct strbuf line = STRBUF_INIT;
382 struct object_id *oids;
383 int i = 0, valid = 1, count;
384 char *chain_name = get_chain_filename(odb->path);
388 fp = fopen(chain_name, "r");
389 stat_res = stat(chain_name, &st);
394 st.st_size <= the_hash_algo->hexsz)
397 count = st.st_size / (the_hash_algo->hexsz + 1);
398 oids = xcalloc(count, sizeof(struct object_id));
402 for (i = 0; i < count; i++) {
403 struct object_directory *odb;
405 if (strbuf_getline_lf(&line, fp) == EOF)
408 if (get_oid_hex(line.buf, &oids[i])) {
409 warning(_("invalid commit-graph chain: line '%s' not a hash"),
416 for (odb = r->objects->odb; odb; odb = odb->next) {
417 char *graph_name = get_split_graph_filename(odb->path, line.buf);
418 struct commit_graph *g = load_commit_graph_one(graph_name);
425 if (add_graph_to_chain(g, graph_chain, oids, i)) {
435 warning(_("unable to find all commit-graph files"));
442 strbuf_release(&line);
447 struct commit_graph *read_commit_graph_one(struct repository *r,
448 struct object_directory *odb)
450 struct commit_graph *g = load_commit_graph_v1(r, odb);
453 g = load_commit_graph_chain(r, odb);
458 static void prepare_commit_graph_one(struct repository *r,
459 struct object_directory *odb)
462 if (r->objects->commit_graph)
465 r->objects->commit_graph = read_commit_graph_one(r, odb);
469 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
471 * On the first invocation, this function attempts to load the commit
472 * graph if the_repository is configured to have one.
474 static int prepare_commit_graph(struct repository *r)
476 struct object_directory *odb;
479 * This must come before the "already attempted?" check below, because
480 * we want to disable even an already-loaded graph file.
482 if (r->commit_graph_disabled)
485 if (r->objects->commit_graph_attempted)
486 return !!r->objects->commit_graph;
487 r->objects->commit_graph_attempted = 1;
489 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
490 die("dying as requested by the '%s' variable on commit-graph load!",
491 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
493 prepare_repo_settings(r);
495 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
496 r->settings.core_commit_graph != 1)
498 * This repository is not configured to use commit graphs, so
499 * do not load one. (But report commit_graph_attempted anyway
500 * so that commit graph loading is not attempted again for this
505 if (!commit_graph_compatible(r))
509 for (odb = r->objects->odb;
510 !r->objects->commit_graph && odb;
512 prepare_commit_graph_one(r, odb);
513 return !!r->objects->commit_graph;
516 int generation_numbers_enabled(struct repository *r)
518 uint32_t first_generation;
519 struct commit_graph *g;
520 if (!prepare_commit_graph(r))
523 g = r->objects->commit_graph;
528 first_generation = get_be32(g->chunk_commit_data +
529 g->hash_len + 8) >> 2;
531 return !!first_generation;
534 static void close_commit_graph_one(struct commit_graph *g)
539 close_commit_graph_one(g->base_graph);
540 free_commit_graph(g);
543 void close_commit_graph(struct raw_object_store *o)
545 close_commit_graph_one(o->commit_graph);
546 o->commit_graph = NULL;
549 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
551 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
552 g->chunk_oid_lookup, g->hash_len, pos);
555 static void load_oid_from_graph(struct commit_graph *g,
557 struct object_id *oid)
561 while (g && pos < g->num_commits_in_base)
565 BUG("NULL commit-graph");
567 if (pos >= g->num_commits + g->num_commits_in_base)
568 die(_("invalid commit position. commit-graph is likely corrupt"));
570 lex_index = pos - g->num_commits_in_base;
572 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
575 static struct commit_list **insert_parent_or_die(struct repository *r,
576 struct commit_graph *g,
578 struct commit_list **pptr)
581 struct object_id oid;
583 if (pos >= g->num_commits + g->num_commits_in_base)
584 die("invalid parent position %"PRIu32, pos);
586 load_oid_from_graph(g, pos, &oid);
587 c = lookup_commit(r, &oid);
589 die(_("could not find commit %s"), oid_to_hex(&oid));
591 return &commit_list_insert(c, pptr)->next;
594 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
596 const unsigned char *commit_data;
599 while (pos < g->num_commits_in_base)
602 lex_index = pos - g->num_commits_in_base;
603 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
604 item->graph_pos = pos;
605 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
608 static inline void set_commit_tree(struct commit *c, struct tree *t)
613 static int fill_commit_in_graph(struct repository *r,
615 struct commit_graph *g, uint32_t pos)
618 uint32_t *parent_data_ptr;
619 uint64_t date_low, date_high;
620 struct commit_list **pptr;
621 const unsigned char *commit_data;
624 while (pos < g->num_commits_in_base)
627 if (pos >= g->num_commits + g->num_commits_in_base)
628 die(_("invalid commit position. commit-graph is likely corrupt"));
631 * Store the "full" position, but then use the
632 * "local" position for the rest of the calculation.
634 item->graph_pos = pos;
635 lex_index = pos - g->num_commits_in_base;
637 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
639 item->object.parsed = 1;
641 set_commit_tree(item, NULL);
643 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
644 date_low = get_be32(commit_data + g->hash_len + 12);
645 item->date = (timestamp_t)((date_high << 32) | date_low);
647 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
649 pptr = &item->parents;
651 edge_value = get_be32(commit_data + g->hash_len);
652 if (edge_value == GRAPH_PARENT_NONE)
654 pptr = insert_parent_or_die(r, g, edge_value, pptr);
656 edge_value = get_be32(commit_data + g->hash_len + 4);
657 if (edge_value == GRAPH_PARENT_NONE)
659 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
660 pptr = insert_parent_or_die(r, g, edge_value, pptr);
664 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
665 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
667 edge_value = get_be32(parent_data_ptr);
668 pptr = insert_parent_or_die(r, g,
669 edge_value & GRAPH_EDGE_LAST_MASK,
672 } while (!(edge_value & GRAPH_LAST_EDGE));
677 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
679 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
680 *pos = item->graph_pos;
683 struct commit_graph *cur_g = g;
686 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
687 cur_g = cur_g->base_graph;
690 *pos = lex_index + cur_g->num_commits_in_base;
698 static int parse_commit_in_graph_one(struct repository *r,
699 struct commit_graph *g,
704 if (item->object.parsed)
707 if (find_commit_in_graph(item, g, &pos))
708 return fill_commit_in_graph(r, item, g, pos);
713 int parse_commit_in_graph(struct repository *r, struct commit *item)
715 if (!prepare_commit_graph(r))
717 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
720 void load_commit_graph_info(struct repository *r, struct commit *item)
723 if (!prepare_commit_graph(r))
725 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
726 fill_commit_graph_info(item, r->objects->commit_graph, pos);
729 static struct tree *load_tree_for_commit(struct repository *r,
730 struct commit_graph *g,
733 struct object_id oid;
734 const unsigned char *commit_data;
736 while (c->graph_pos < g->num_commits_in_base)
739 commit_data = g->chunk_commit_data +
740 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
742 hashcpy(oid.hash, commit_data);
743 set_commit_tree(c, lookup_tree(r, &oid));
745 return c->maybe_tree;
748 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
749 struct commit_graph *g,
750 const struct commit *c)
753 return c->maybe_tree;
754 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
755 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
757 return load_tree_for_commit(r, g, (struct commit *)c);
760 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
762 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
765 struct packed_commit_list {
766 struct commit **list;
771 struct packed_oid_list {
772 struct object_id *list;
777 struct write_commit_graph_context {
778 struct repository *r;
779 struct object_directory *odb;
781 struct packed_oid_list oids;
782 struct packed_commit_list commits;
784 unsigned long approx_nr_objects;
785 struct progress *progress;
787 uint64_t progress_cnt;
789 char *base_graph_name;
790 int num_commit_graphs_before;
791 int num_commit_graphs_after;
792 char **commit_graph_filenames_before;
793 char **commit_graph_filenames_after;
794 char **commit_graph_hash_after;
795 uint32_t new_num_commits_in_base;
796 struct commit_graph *new_base_graph;
803 const struct split_commit_graph_opts *split_opts;
806 static void write_graph_chunk_fanout(struct hashfile *f,
807 struct write_commit_graph_context *ctx)
810 struct commit **list = ctx->commits.list;
813 * Write the first-level table (the list is sorted,
814 * but we use a 256-entry lookup to be able to avoid
815 * having to do eight extra binary search iterations).
817 for (i = 0; i < 256; i++) {
818 while (count < ctx->commits.nr) {
819 if ((*list)->object.oid.hash[0] != i)
821 display_progress(ctx->progress, ++ctx->progress_cnt);
826 hashwrite_be32(f, count);
830 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
831 struct write_commit_graph_context *ctx)
833 struct commit **list = ctx->commits.list;
835 for (count = 0; count < ctx->commits.nr; count++, list++) {
836 display_progress(ctx->progress, ++ctx->progress_cnt);
837 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
841 static const unsigned char *commit_to_sha1(size_t index, void *table)
843 struct commit **commits = table;
844 return commits[index]->object.oid.hash;
847 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
848 struct write_commit_graph_context *ctx)
850 struct commit **list = ctx->commits.list;
851 struct commit **last = ctx->commits.list + ctx->commits.nr;
852 uint32_t num_extra_edges = 0;
854 while (list < last) {
855 struct commit_list *parent;
856 struct object_id *tree;
858 uint32_t packedDate[2];
859 display_progress(ctx->progress, ++ctx->progress_cnt);
861 if (parse_commit_no_graph(*list))
862 die(_("unable to parse commit %s"),
863 oid_to_hex(&(*list)->object.oid));
864 tree = get_commit_tree_oid(*list);
865 hashwrite(f, tree->hash, hash_len);
867 parent = (*list)->parents;
870 edge_value = GRAPH_PARENT_NONE;
872 edge_value = sha1_pos(parent->item->object.oid.hash,
878 edge_value += ctx->new_num_commits_in_base;
881 if (find_commit_in_graph(parent->item,
888 BUG("missing parent %s for commit %s",
889 oid_to_hex(&parent->item->object.oid),
890 oid_to_hex(&(*list)->object.oid));
893 hashwrite_be32(f, edge_value);
896 parent = parent->next;
899 edge_value = GRAPH_PARENT_NONE;
900 else if (parent->next)
901 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
903 edge_value = sha1_pos(parent->item->object.oid.hash,
909 edge_value += ctx->new_num_commits_in_base;
912 if (find_commit_in_graph(parent->item,
919 BUG("missing parent %s for commit %s",
920 oid_to_hex(&parent->item->object.oid),
921 oid_to_hex(&(*list)->object.oid));
924 hashwrite_be32(f, edge_value);
926 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
929 parent = parent->next;
933 if (sizeof((*list)->date) > 4)
934 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
938 packedDate[0] |= htonl((*list)->generation << 2);
940 packedDate[1] = htonl((*list)->date);
941 hashwrite(f, packedDate, 8);
947 static void write_graph_chunk_extra_edges(struct hashfile *f,
948 struct write_commit_graph_context *ctx)
950 struct commit **list = ctx->commits.list;
951 struct commit **last = ctx->commits.list + ctx->commits.nr;
952 struct commit_list *parent;
954 while (list < last) {
957 display_progress(ctx->progress, ++ctx->progress_cnt);
959 for (parent = (*list)->parents; num_parents < 3 && parent;
960 parent = parent->next)
963 if (num_parents <= 2) {
968 /* Since num_parents > 2, this initializer is safe. */
969 for (parent = (*list)->parents->next; parent; parent = parent->next) {
970 int edge_value = sha1_pos(parent->item->object.oid.hash,
976 edge_value += ctx->new_num_commits_in_base;
979 if (find_commit_in_graph(parent->item,
986 BUG("missing parent %s for commit %s",
987 oid_to_hex(&parent->item->object.oid),
988 oid_to_hex(&(*list)->object.oid));
989 else if (!parent->next)
990 edge_value |= GRAPH_LAST_EDGE;
992 hashwrite_be32(f, edge_value);
999 static int oid_compare(const void *_a, const void *_b)
1001 const struct object_id *a = (const struct object_id *)_a;
1002 const struct object_id *b = (const struct object_id *)_b;
1003 return oidcmp(a, b);
1006 static int add_packed_commits(const struct object_id *oid,
1007 struct packed_git *pack,
1011 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1012 enum object_type type;
1013 off_t offset = nth_packed_object_offset(pack, pos);
1014 struct object_info oi = OBJECT_INFO_INIT;
1017 display_progress(ctx->progress, ++ctx->progress_done);
1020 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1021 die(_("unable to get type of object %s"), oid_to_hex(oid));
1023 if (type != OBJ_COMMIT)
1026 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1027 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1033 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1035 struct commit_list *parent;
1036 for (parent = commit->parents; parent; parent = parent->next) {
1037 if (!(parent->item->object.flags & REACHABLE)) {
1038 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1039 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1041 parent->item->object.flags |= REACHABLE;
1046 static void close_reachable(struct write_commit_graph_context *ctx)
1049 struct commit *commit;
1051 if (ctx->report_progress)
1052 ctx->progress = start_delayed_progress(
1053 _("Loading known commits in commit graph"),
1055 for (i = 0; i < ctx->oids.nr; i++) {
1056 display_progress(ctx->progress, i + 1);
1057 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1059 commit->object.flags |= REACHABLE;
1061 stop_progress(&ctx->progress);
1064 * As this loop runs, ctx->oids.nr may grow, but not more
1065 * than the number of missing commits in the reachable
1068 if (ctx->report_progress)
1069 ctx->progress = start_delayed_progress(
1070 _("Expanding reachable commits in commit graph"),
1072 for (i = 0; i < ctx->oids.nr; i++) {
1073 display_progress(ctx->progress, i + 1);
1074 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1079 if (!parse_commit(commit) &&
1080 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1081 add_missing_parents(ctx, commit);
1082 } else if (!parse_commit_no_graph(commit))
1083 add_missing_parents(ctx, commit);
1085 stop_progress(&ctx->progress);
1087 if (ctx->report_progress)
1088 ctx->progress = start_delayed_progress(
1089 _("Clearing commit marks in commit graph"),
1091 for (i = 0; i < ctx->oids.nr; i++) {
1092 display_progress(ctx->progress, i + 1);
1093 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1096 commit->object.flags &= ~REACHABLE;
1098 stop_progress(&ctx->progress);
1101 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1104 struct commit_list *list = NULL;
1106 if (ctx->report_progress)
1107 ctx->progress = start_delayed_progress(
1108 _("Computing commit graph generation numbers"),
1110 for (i = 0; i < ctx->commits.nr; i++) {
1111 display_progress(ctx->progress, i + 1);
1112 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1113 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
1116 commit_list_insert(ctx->commits.list[i], &list);
1118 struct commit *current = list->item;
1119 struct commit_list *parent;
1120 int all_parents_computed = 1;
1121 uint32_t max_generation = 0;
1123 for (parent = current->parents; parent; parent = parent->next) {
1124 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1125 parent->item->generation == GENERATION_NUMBER_ZERO) {
1126 all_parents_computed = 0;
1127 commit_list_insert(parent->item, &list);
1129 } else if (parent->item->generation > max_generation) {
1130 max_generation = parent->item->generation;
1134 if (all_parents_computed) {
1135 current->generation = max_generation + 1;
1138 if (current->generation > GENERATION_NUMBER_MAX)
1139 current->generation = GENERATION_NUMBER_MAX;
1143 stop_progress(&ctx->progress);
1146 static int add_ref_to_list(const char *refname,
1147 const struct object_id *oid,
1148 int flags, void *cb_data)
1150 struct string_list *list = (struct string_list *)cb_data;
1152 string_list_append(list, oid_to_hex(oid));
1156 int write_commit_graph_reachable(struct object_directory *odb,
1157 enum commit_graph_write_flags flags,
1158 const struct split_commit_graph_opts *split_opts)
1160 struct string_list list = STRING_LIST_INIT_DUP;
1163 for_each_ref(add_ref_to_list, &list);
1164 result = write_commit_graph(odb, NULL, &list,
1167 string_list_clear(&list, 0);
1171 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1172 struct string_list *pack_indexes)
1175 struct strbuf progress_title = STRBUF_INIT;
1176 struct strbuf packname = STRBUF_INIT;
1179 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1180 dirlen = packname.len;
1181 if (ctx->report_progress) {
1182 strbuf_addf(&progress_title,
1183 Q_("Finding commits for commit graph in %d pack",
1184 "Finding commits for commit graph in %d packs",
1187 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1188 ctx->progress_done = 0;
1190 for (i = 0; i < pack_indexes->nr; i++) {
1191 struct packed_git *p;
1192 strbuf_setlen(&packname, dirlen);
1193 strbuf_addstr(&packname, pack_indexes->items[i].string);
1194 p = add_packed_git(packname.buf, packname.len, 1);
1196 error(_("error adding pack %s"), packname.buf);
1199 if (open_pack_index(p)) {
1200 error(_("error opening index for %s"), packname.buf);
1203 for_each_object_in_pack(p, add_packed_commits, ctx,
1204 FOR_EACH_OBJECT_PACK_ORDER);
1209 stop_progress(&ctx->progress);
1210 strbuf_release(&progress_title);
1211 strbuf_release(&packname);
1216 static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1217 struct string_list *commit_hex)
1220 struct strbuf progress_title = STRBUF_INIT;
1222 if (ctx->report_progress) {
1223 strbuf_addf(&progress_title,
1224 Q_("Finding commits for commit graph from %d ref",
1225 "Finding commits for commit graph from %d refs",
1228 ctx->progress = start_delayed_progress(
1232 for (i = 0; i < commit_hex->nr; i++) {
1234 struct object_id oid;
1235 struct commit *result;
1237 display_progress(ctx->progress, i + 1);
1238 if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1239 (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
1240 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1241 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1243 } else if (ctx->check_oids) {
1244 error(_("invalid commit object id: %s"),
1245 commit_hex->items[i].string);
1249 stop_progress(&ctx->progress);
1250 strbuf_release(&progress_title);
1255 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1257 if (ctx->report_progress)
1258 ctx->progress = start_delayed_progress(
1259 _("Finding commits for commit graph among packed objects"),
1260 ctx->approx_nr_objects);
1261 for_each_packed_object(add_packed_commits, ctx,
1262 FOR_EACH_OBJECT_PACK_ORDER);
1263 if (ctx->progress_done < ctx->approx_nr_objects)
1264 display_progress(ctx->progress, ctx->approx_nr_objects);
1265 stop_progress(&ctx->progress);
1268 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1270 uint32_t i, count_distinct = 1;
1272 if (ctx->report_progress)
1273 ctx->progress = start_delayed_progress(
1274 _("Counting distinct commits in commit graph"),
1276 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1277 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1279 for (i = 1; i < ctx->oids.nr; i++) {
1280 display_progress(ctx->progress, i + 1);
1281 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1283 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1285 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1292 stop_progress(&ctx->progress);
1294 return count_distinct;
1297 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1301 ctx->num_extra_edges = 0;
1302 if (ctx->report_progress)
1303 ctx->progress = start_delayed_progress(
1304 _("Finding extra edges in commit graph"),
1306 for (i = 0; i < ctx->oids.nr; i++) {
1307 unsigned int num_parents;
1309 display_progress(ctx->progress, i + 1);
1310 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1313 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1314 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1317 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1320 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1322 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1323 if (num_parents > 2)
1324 ctx->num_extra_edges += num_parents - 1;
1328 stop_progress(&ctx->progress);
1331 static int write_graph_chunk_base_1(struct hashfile *f,
1332 struct commit_graph *g)
1339 num = write_graph_chunk_base_1(f, g->base_graph);
1340 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1344 static int write_graph_chunk_base(struct hashfile *f,
1345 struct write_commit_graph_context *ctx)
1347 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1349 if (num != ctx->num_commit_graphs_after - 1) {
1350 error(_("failed to write correct number of base graph ids"));
1357 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1362 struct lock_file lk = LOCK_INIT;
1363 uint32_t chunk_ids[6];
1364 uint64_t chunk_offsets[6];
1365 const unsigned hashsz = the_hash_algo->rawsz;
1366 struct strbuf progress_title = STRBUF_INIT;
1368 struct object_id file_hash;
1371 struct strbuf tmp_file = STRBUF_INIT;
1373 strbuf_addf(&tmp_file,
1374 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1376 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1378 ctx->graph_name = get_commit_graph_filename(ctx->odb->path);
1381 if (safe_create_leading_directories(ctx->graph_name)) {
1382 UNLEAK(ctx->graph_name);
1383 error(_("unable to create leading directories of %s"),
1389 char *lock_name = get_chain_filename(ctx->odb->path);
1391 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1393 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1395 error(_("unable to create '%s'"), ctx->graph_name);
1399 f = hashfd(fd, ctx->graph_name);
1401 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1402 fd = lk.tempfile->fd;
1403 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1406 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1407 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1408 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1409 if (ctx->num_extra_edges) {
1410 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1413 if (ctx->num_commit_graphs_after > 1) {
1414 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1418 chunk_ids[num_chunks] = 0;
1420 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1421 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1422 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1423 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1426 if (ctx->num_extra_edges) {
1427 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1428 4 * ctx->num_extra_edges;
1431 if (ctx->num_commit_graphs_after > 1) {
1432 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1433 hashsz * (ctx->num_commit_graphs_after - 1);
1437 hashwrite_be32(f, GRAPH_SIGNATURE);
1439 hashwrite_u8(f, GRAPH_VERSION);
1440 hashwrite_u8(f, oid_version());
1441 hashwrite_u8(f, num_chunks);
1442 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1444 for (i = 0; i <= num_chunks; i++) {
1445 uint32_t chunk_write[3];
1447 chunk_write[0] = htonl(chunk_ids[i]);
1448 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1449 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1450 hashwrite(f, chunk_write, 12);
1453 if (ctx->report_progress) {
1454 strbuf_addf(&progress_title,
1455 Q_("Writing out commit graph in %d pass",
1456 "Writing out commit graph in %d passes",
1459 ctx->progress = start_delayed_progress(
1461 num_chunks * ctx->commits.nr);
1463 write_graph_chunk_fanout(f, ctx);
1464 write_graph_chunk_oids(f, hashsz, ctx);
1465 write_graph_chunk_data(f, hashsz, ctx);
1466 if (ctx->num_extra_edges)
1467 write_graph_chunk_extra_edges(f, ctx);
1468 if (ctx->num_commit_graphs_after > 1 &&
1469 write_graph_chunk_base(f, ctx)) {
1472 stop_progress(&ctx->progress);
1473 strbuf_release(&progress_title);
1475 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1476 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1477 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb->path, new_base_hash);
1479 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1480 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1481 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1482 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1485 close_commit_graph(ctx->r->objects);
1486 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1489 FILE *chainf = fdopen_lock_file(&lk, "w");
1490 char *final_graph_name;
1496 error(_("unable to open commit-graph chain file"));
1500 if (ctx->base_graph_name) {
1501 const char *dest = ctx->commit_graph_filenames_after[
1502 ctx->num_commit_graphs_after - 2];
1504 if (strcmp(ctx->base_graph_name, dest)) {
1505 result = rename(ctx->base_graph_name, dest);
1508 error(_("failed to rename base commit-graph file"));
1513 char *graph_name = get_commit_graph_filename(ctx->odb->path);
1517 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1518 final_graph_name = get_split_graph_filename(ctx->odb->path,
1519 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1520 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1522 result = rename(ctx->graph_name, final_graph_name);
1524 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1525 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1528 error(_("failed to rename temporary commit-graph file"));
1533 commit_lock_file(&lk);
1538 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1540 struct commit_graph *g;
1541 uint32_t num_commits;
1544 int max_commits = 0;
1547 if (ctx->split_opts) {
1548 max_commits = ctx->split_opts->max_commits;
1550 if (ctx->split_opts->size_multiple)
1551 size_mult = ctx->split_opts->size_multiple;
1554 g = ctx->r->objects->commit_graph;
1555 num_commits = ctx->commits.nr;
1556 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1558 while (g && (g->num_commits <= size_mult * num_commits ||
1559 (max_commits && num_commits > max_commits))) {
1560 if (strcmp(g->odb->path, ctx->odb->path))
1563 num_commits += g->num_commits;
1566 ctx->num_commit_graphs_after--;
1569 ctx->new_base_graph = g;
1571 if (ctx->num_commit_graphs_after == 2) {
1572 char *old_graph_name = get_commit_graph_filename(g->odb->path);
1574 if (!strcmp(g->filename, old_graph_name) &&
1575 strcmp(g->odb->path, ctx->odb->path)) {
1576 ctx->num_commit_graphs_after = 1;
1577 ctx->new_base_graph = NULL;
1580 free(old_graph_name);
1583 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1584 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1586 for (i = 0; i < ctx->num_commit_graphs_after &&
1587 i < ctx->num_commit_graphs_before; i++)
1588 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1590 i = ctx->num_commit_graphs_before - 1;
1591 g = ctx->r->objects->commit_graph;
1594 if (i < ctx->num_commit_graphs_after)
1595 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1602 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1603 struct commit_graph *g)
1606 uint32_t offset = g->num_commits_in_base;
1608 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1610 for (i = 0; i < g->num_commits; i++) {
1611 struct object_id oid;
1612 struct commit *result;
1614 display_progress(ctx->progress, i + 1);
1616 load_oid_from_graph(g, i + offset, &oid);
1618 /* only add commits if they still exist in the repo */
1619 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1622 ctx->commits.list[ctx->commits.nr] = result;
1628 static int commit_compare(const void *_a, const void *_b)
1630 const struct commit *a = *(const struct commit **)_a;
1631 const struct commit *b = *(const struct commit **)_b;
1632 return oidcmp(&a->object.oid, &b->object.oid);
1635 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1639 if (ctx->report_progress)
1640 ctx->progress = start_delayed_progress(
1641 _("Scanning merged commits"),
1644 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1646 ctx->num_extra_edges = 0;
1647 for (i = 0; i < ctx->commits.nr; i++) {
1648 display_progress(ctx->progress, i);
1650 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1651 &ctx->commits.list[i]->object.oid)) {
1652 die(_("unexpected duplicate commit id %s"),
1653 oid_to_hex(&ctx->commits.list[i]->object.oid));
1655 unsigned int num_parents;
1657 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1658 if (num_parents > 2)
1659 ctx->num_extra_edges += num_parents - 1;
1663 stop_progress(&ctx->progress);
1666 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1668 struct commit_graph *g = ctx->r->objects->commit_graph;
1669 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1670 struct strbuf progress_title = STRBUF_INIT;
1672 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1673 current_graph_number--;
1675 if (ctx->report_progress) {
1676 strbuf_addstr(&progress_title, _("Merging commit-graph"));
1677 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1680 merge_commit_graph(ctx, g);
1681 stop_progress(&ctx->progress);
1682 strbuf_release(&progress_title);
1688 ctx->new_base_graph = g;
1689 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1692 if (ctx->new_base_graph)
1693 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1695 sort_and_scan_merged_commits(ctx);
1698 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1701 time_t now = time(NULL);
1703 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1705 struct utimbuf updated_time;
1707 stat(ctx->commit_graph_filenames_before[i], &st);
1709 updated_time.actime = st.st_atime;
1710 updated_time.modtime = now;
1711 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1715 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1717 struct strbuf path = STRBUF_INIT;
1721 timestamp_t expire_time = time(NULL);
1723 if (ctx->split_opts && ctx->split_opts->expire_time)
1724 expire_time -= ctx->split_opts->expire_time;
1726 char *chain_file_name = get_chain_filename(ctx->odb->path);
1727 unlink(chain_file_name);
1728 free(chain_file_name);
1729 ctx->num_commit_graphs_after = 0;
1732 strbuf_addstr(&path, ctx->odb->path);
1733 strbuf_addstr(&path, "/info/commit-graphs");
1734 dir = opendir(path.buf);
1739 strbuf_addch(&path, '/');
1740 dirnamelen = path.len;
1741 while ((de = readdir(dir)) != NULL) {
1743 uint32_t i, found = 0;
1745 strbuf_setlen(&path, dirnamelen);
1746 strbuf_addstr(&path, de->d_name);
1748 stat(path.buf, &st);
1750 if (st.st_mtime > expire_time)
1752 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1755 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1756 if (!strcmp(ctx->commit_graph_filenames_after[i],
1768 strbuf_release(&path);
1771 int write_commit_graph(struct object_directory *odb,
1772 struct string_list *pack_indexes,
1773 struct string_list *commit_hex,
1774 enum commit_graph_write_flags flags,
1775 const struct split_commit_graph_opts *split_opts)
1777 struct write_commit_graph_context *ctx;
1778 uint32_t i, count_distinct = 0;
1781 if (!commit_graph_compatible(the_repository))
1784 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1785 ctx->r = the_repository;
1787 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1788 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1789 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
1790 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
1791 ctx->split_opts = split_opts;
1794 struct commit_graph *g;
1795 prepare_commit_graph(ctx->r);
1797 g = ctx->r->objects->commit_graph;
1800 ctx->num_commit_graphs_before++;
1804 if (ctx->num_commit_graphs_before) {
1805 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1806 i = ctx->num_commit_graphs_before;
1807 g = ctx->r->objects->commit_graph;
1810 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1816 ctx->approx_nr_objects = approximate_object_count();
1817 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1819 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1820 ctx->oids.alloc = split_opts->max_commits;
1823 prepare_commit_graph_one(ctx->r, ctx->odb);
1824 if (ctx->r->objects->commit_graph)
1825 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
1828 if (ctx->oids.alloc < 1024)
1829 ctx->oids.alloc = 1024;
1830 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1832 if (ctx->append && ctx->r->objects->commit_graph) {
1833 struct commit_graph *g = ctx->r->objects->commit_graph;
1834 for (i = 0; i < g->num_commits; i++) {
1835 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1836 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
1841 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1846 if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
1850 if (!pack_indexes && !commit_hex)
1851 fill_oids_from_all_packs(ctx);
1853 close_reachable(ctx);
1855 count_distinct = count_distinct_commits(ctx);
1857 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1858 error(_("the commit graph format cannot write %d commits"), count_distinct);
1863 ctx->commits.alloc = count_distinct;
1864 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
1866 copy_oids_to_commits(ctx);
1868 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
1869 error(_("too many commits to write graph"));
1874 if (!ctx->commits.nr)
1878 split_graph_merge_strategy(ctx);
1880 merge_commit_graphs(ctx);
1882 ctx->num_commit_graphs_after = 1;
1884 compute_generation_numbers(ctx);
1886 res = write_commit_graph_file(ctx);
1889 mark_commit_graphs(ctx);
1891 expire_commit_graphs(ctx);
1894 free(ctx->graph_name);
1895 free(ctx->commits.list);
1896 free(ctx->oids.list);
1898 if (ctx->commit_graph_filenames_after) {
1899 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1900 free(ctx->commit_graph_filenames_after[i]);
1901 free(ctx->commit_graph_hash_after[i]);
1904 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1905 free(ctx->commit_graph_filenames_before[i]);
1907 free(ctx->commit_graph_filenames_after);
1908 free(ctx->commit_graph_filenames_before);
1909 free(ctx->commit_graph_hash_after);
1917 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1918 static int verify_commit_graph_error;
1920 static void graph_report(const char *fmt, ...)
1924 verify_commit_graph_error = 1;
1926 vfprintf(stderr, fmt, ap);
1927 fprintf(stderr, "\n");
1931 #define GENERATION_ZERO_EXISTS 1
1932 #define GENERATION_NUMBER_EXISTS 2
1934 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
1936 uint32_t i, cur_fanout_pos = 0;
1937 struct object_id prev_oid, cur_oid, checksum;
1938 int generation_zero = 0;
1941 struct progress *progress = NULL;
1942 int local_error = 0;
1945 graph_report("no commit-graph file loaded");
1949 verify_commit_graph_error = verify_commit_graph_lite(g);
1950 if (verify_commit_graph_error)
1951 return verify_commit_graph_error;
1953 devnull = open("/dev/null", O_WRONLY);
1954 f = hashfd(devnull, NULL);
1955 hashwrite(f, g->data, g->data_len - g->hash_len);
1956 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1957 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1958 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1959 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1962 for (i = 0; i < g->num_commits; i++) {
1963 struct commit *graph_commit;
1965 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1967 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1968 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1969 oid_to_hex(&prev_oid),
1970 oid_to_hex(&cur_oid));
1972 oidcpy(&prev_oid, &cur_oid);
1974 while (cur_oid.hash[0] > cur_fanout_pos) {
1975 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1977 if (i != fanout_value)
1978 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1979 cur_fanout_pos, fanout_value, i);
1983 graph_commit = lookup_commit(r, &cur_oid);
1984 if (!parse_commit_in_graph_one(r, g, graph_commit))
1985 graph_report(_("failed to parse commit %s from commit-graph"),
1986 oid_to_hex(&cur_oid));
1989 while (cur_fanout_pos < 256) {
1990 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1992 if (g->num_commits != fanout_value)
1993 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1994 cur_fanout_pos, fanout_value, i);
1999 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2000 return verify_commit_graph_error;
2002 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2003 progress = start_progress(_("Verifying commits in commit graph"),
2006 for (i = 0; i < g->num_commits; i++) {
2007 struct commit *graph_commit, *odb_commit;
2008 struct commit_list *graph_parents, *odb_parents;
2009 uint32_t max_generation = 0;
2011 display_progress(progress, i + 1);
2012 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2014 graph_commit = lookup_commit(r, &cur_oid);
2015 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2016 if (parse_commit_internal(odb_commit, 0, 0)) {
2017 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2018 oid_to_hex(&cur_oid));
2022 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2023 get_commit_tree_oid(odb_commit)))
2024 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2025 oid_to_hex(&cur_oid),
2026 oid_to_hex(get_commit_tree_oid(graph_commit)),
2027 oid_to_hex(get_commit_tree_oid(odb_commit)));
2029 graph_parents = graph_commit->parents;
2030 odb_parents = odb_commit->parents;
2032 while (graph_parents) {
2033 if (odb_parents == NULL) {
2034 graph_report(_("commit-graph parent list for commit %s is too long"),
2035 oid_to_hex(&cur_oid));
2039 /* parse parent in case it is in a base graph */
2040 parse_commit_in_graph_one(r, g, graph_parents->item);
2042 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2043 graph_report(_("commit-graph parent for %s is %s != %s"),
2044 oid_to_hex(&cur_oid),
2045 oid_to_hex(&graph_parents->item->object.oid),
2046 oid_to_hex(&odb_parents->item->object.oid));
2048 if (graph_parents->item->generation > max_generation)
2049 max_generation = graph_parents->item->generation;
2051 graph_parents = graph_parents->next;
2052 odb_parents = odb_parents->next;
2055 if (odb_parents != NULL)
2056 graph_report(_("commit-graph parent list for commit %s terminates early"),
2057 oid_to_hex(&cur_oid));
2059 if (!graph_commit->generation) {
2060 if (generation_zero == GENERATION_NUMBER_EXISTS)
2061 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2062 oid_to_hex(&cur_oid));
2063 generation_zero = GENERATION_ZERO_EXISTS;
2064 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2065 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2066 oid_to_hex(&cur_oid));
2068 if (generation_zero == GENERATION_ZERO_EXISTS)
2072 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2073 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2074 * extra logic in the following condition.
2076 if (max_generation == GENERATION_NUMBER_MAX)
2079 if (graph_commit->generation != max_generation + 1)
2080 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2081 oid_to_hex(&cur_oid),
2082 graph_commit->generation,
2083 max_generation + 1);
2085 if (graph_commit->date != odb_commit->date)
2086 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2087 oid_to_hex(&cur_oid),
2091 stop_progress(&progress);
2093 local_error = verify_commit_graph_error;
2095 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2096 local_error |= verify_commit_graph(r, g->base_graph, flags);
2101 void free_commit_graph(struct commit_graph *g)
2105 if (g->graph_fd >= 0) {
2106 munmap((void *)g->data, g->data_len);
2114 void disable_commit_graph(struct repository *r)
2116 r->commit_graph_disabled = 1;