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 char *get_commit_graph_filename(const char *obj_dir)
46 return xstrfmt("%s/info/commit-graph", obj_dir);
49 static char *get_split_graph_filename(const char *obj_dir,
52 return xstrfmt("%s/info/commit-graphs/graph-%s.graph",
57 static char *get_chain_filename(const char *obj_dir)
59 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
62 static uint8_t oid_version(void)
67 static struct commit_graph *alloc_commit_graph(void)
69 struct commit_graph *g = xcalloc(1, sizeof(*g));
75 extern int read_replace_refs;
77 static int commit_graph_compatible(struct repository *r)
82 if (read_replace_refs) {
83 prepare_replace_object(r);
84 if (hashmap_get_size(&r->objects->replace_map->map))
88 prepare_commit_graft(r);
89 if (r->parsed_objects && r->parsed_objects->grafts_nr)
91 if (is_repository_shallow(r))
97 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
99 *fd = git_open(graph_file);
102 if (fstat(*fd, st)) {
109 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
113 struct commit_graph *ret;
115 graph_size = xsize_t(st->st_size);
117 if (graph_size < GRAPH_MIN_SIZE) {
119 error(_("commit-graph file is too small"));
122 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
123 ret = parse_commit_graph(graph_map, fd, graph_size);
126 munmap(graph_map, graph_size);
133 static int verify_commit_graph_lite(struct commit_graph *g)
136 * Basic validation shared between parse_commit_graph()
137 * which'll be called every time the graph is used, and the
138 * much more expensive verify_commit_graph() used by
139 * "commit-graph verify".
141 * There should only be very basic checks here to ensure that
142 * we don't e.g. segfault in fill_commit_in_graph(), but
143 * because this is a very hot codepath nothing that e.g. loops
144 * over g->num_commits, or runs a checksum on the commit-graph
147 if (!g->chunk_oid_fanout) {
148 error("commit-graph is missing the OID Fanout chunk");
151 if (!g->chunk_oid_lookup) {
152 error("commit-graph is missing the OID Lookup chunk");
155 if (!g->chunk_commit_data) {
156 error("commit-graph is missing the Commit Data chunk");
163 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
166 const unsigned char *data, *chunk_lookup;
168 struct commit_graph *graph;
169 uint64_t last_chunk_offset;
170 uint32_t last_chunk_id;
171 uint32_t graph_signature;
172 unsigned char graph_version, hash_version;
177 if (graph_size < GRAPH_MIN_SIZE)
180 data = (const unsigned char *)graph_map;
182 graph_signature = get_be32(data);
183 if (graph_signature != GRAPH_SIGNATURE) {
184 error(_("commit-graph signature %X does not match signature %X"),
185 graph_signature, GRAPH_SIGNATURE);
189 graph_version = *(unsigned char*)(data + 4);
190 if (graph_version != GRAPH_VERSION) {
191 error(_("commit-graph version %X does not match version %X"),
192 graph_version, GRAPH_VERSION);
196 hash_version = *(unsigned char*)(data + 5);
197 if (hash_version != oid_version()) {
198 error(_("commit-graph hash version %X does not match version %X"),
199 hash_version, oid_version());
203 graph = alloc_commit_graph();
205 graph->hash_len = the_hash_algo->rawsz;
206 graph->num_chunks = *(unsigned char*)(data + 6);
207 graph->graph_fd = fd;
208 graph->data = graph_map;
209 graph->data_len = graph_size;
212 last_chunk_offset = 8;
213 chunk_lookup = data + 8;
214 for (i = 0; i < graph->num_chunks; i++) {
216 uint64_t chunk_offset;
217 int chunk_repeated = 0;
219 if (data + graph_size - chunk_lookup <
220 GRAPH_CHUNKLOOKUP_WIDTH) {
221 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
226 chunk_id = get_be32(chunk_lookup + 0);
227 chunk_offset = get_be64(chunk_lookup + 4);
229 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
231 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
232 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
233 (uint32_t)chunk_offset);
239 case GRAPH_CHUNKID_OIDFANOUT:
240 if (graph->chunk_oid_fanout)
243 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
246 case GRAPH_CHUNKID_OIDLOOKUP:
247 if (graph->chunk_oid_lookup)
250 graph->chunk_oid_lookup = data + chunk_offset;
253 case GRAPH_CHUNKID_DATA:
254 if (graph->chunk_commit_data)
257 graph->chunk_commit_data = data + chunk_offset;
260 case GRAPH_CHUNKID_EXTRAEDGES:
261 if (graph->chunk_extra_edges)
264 graph->chunk_extra_edges = data + chunk_offset;
267 case GRAPH_CHUNKID_BASE:
268 if (graph->chunk_base_graphs)
271 graph->chunk_base_graphs = data + chunk_offset;
274 if (chunk_repeated) {
275 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
280 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
282 graph->num_commits = (chunk_offset - last_chunk_offset)
286 last_chunk_id = chunk_id;
287 last_chunk_offset = chunk_offset;
290 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
292 if (verify_commit_graph_lite(graph))
298 static struct commit_graph *load_commit_graph_one(const char *graph_file)
303 int open_ok = open_commit_graph(graph_file, &fd, &st);
308 return load_commit_graph_one_fd_st(fd, &st);
311 static struct commit_graph *load_commit_graph_v1(struct repository *r, const char *obj_dir)
313 char *graph_name = get_commit_graph_filename(obj_dir);
314 struct commit_graph *g = load_commit_graph_one(graph_name);
320 static int add_graph_to_chain(struct commit_graph *g,
321 struct commit_graph *chain,
322 struct object_id *oids,
325 struct commit_graph *cur_g = chain;
327 if (n && !g->chunk_base_graphs) {
328 warning(_("commit-graph has no base graphs chunk"));
336 !oideq(&oids[n], &cur_g->oid) ||
337 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
338 warning(_("commit-graph chain does not match"));
342 cur_g = cur_g->base_graph;
345 g->base_graph = chain;
348 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
353 static struct commit_graph *load_commit_graph_chain(struct repository *r, const char *obj_dir)
355 struct commit_graph *graph_chain = NULL;
356 struct strbuf line = STRBUF_INIT;
358 struct object_id *oids;
359 int i = 0, valid = 1, count;
360 char *chain_name = get_chain_filename(obj_dir);
364 fp = fopen(chain_name, "r");
365 stat_res = stat(chain_name, &st);
370 st.st_size <= the_hash_algo->hexsz)
373 count = st.st_size / (the_hash_algo->hexsz + 1);
374 oids = xcalloc(count, sizeof(struct object_id));
376 for (i = 0; i < count && valid; i++) {
378 struct commit_graph *g;
380 if (strbuf_getline_lf(&line, fp) == EOF)
383 if (get_oid_hex(line.buf, &oids[i])) {
384 warning(_("invalid commit-graph chain: line '%s' not a hash"),
390 graph_name = get_split_graph_filename(obj_dir, line.buf);
391 g = load_commit_graph_one(graph_name);
394 if (g && add_graph_to_chain(g, graph_chain, oids, i))
406 static struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
408 struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
411 g = load_commit_graph_chain(r, obj_dir);
416 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
419 if (r->objects->commit_graph)
422 r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
426 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
428 * On the first invocation, this function attemps to load the commit
429 * graph if the_repository is configured to have one.
431 static int prepare_commit_graph(struct repository *r)
433 struct object_directory *odb;
436 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
437 die("dying as requested by the '%s' variable on commit-graph load!",
438 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
440 if (r->objects->commit_graph_attempted)
441 return !!r->objects->commit_graph;
442 r->objects->commit_graph_attempted = 1;
444 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
445 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
448 * This repository is not configured to use commit graphs, so
449 * do not load one. (But report commit_graph_attempted anyway
450 * so that commit graph loading is not attempted again for this
455 if (!commit_graph_compatible(r))
459 for (odb = r->objects->odb;
460 !r->objects->commit_graph && odb;
462 prepare_commit_graph_one(r, odb->path);
463 return !!r->objects->commit_graph;
466 int generation_numbers_enabled(struct repository *r)
468 uint32_t first_generation;
469 struct commit_graph *g;
470 if (!prepare_commit_graph(r))
473 g = r->objects->commit_graph;
478 first_generation = get_be32(g->chunk_commit_data +
479 g->hash_len + 8) >> 2;
481 return !!first_generation;
484 static void close_commit_graph_one(struct commit_graph *g)
489 close_commit_graph_one(g->base_graph);
490 free_commit_graph(g);
493 void close_commit_graph(struct raw_object_store *o)
495 close_commit_graph_one(o->commit_graph);
496 o->commit_graph = NULL;
499 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
501 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
502 g->chunk_oid_lookup, g->hash_len, pos);
505 static void load_oid_from_graph(struct commit_graph *g,
507 struct object_id *oid)
511 while (g && pos < g->num_commits_in_base)
515 BUG("NULL commit-graph");
517 if (pos >= g->num_commits + g->num_commits_in_base)
518 die(_("invalid commit position. commit-graph is likely corrupt"));
520 lex_index = pos - g->num_commits_in_base;
522 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
525 static struct commit_list **insert_parent_or_die(struct repository *r,
526 struct commit_graph *g,
528 struct commit_list **pptr)
531 struct object_id oid;
533 if (pos >= g->num_commits + g->num_commits_in_base)
534 die("invalid parent position %"PRIu32, pos);
536 load_oid_from_graph(g, pos, &oid);
537 c = lookup_commit(r, &oid);
539 die(_("could not find commit %s"), oid_to_hex(&oid));
541 return &commit_list_insert(c, pptr)->next;
544 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
546 const unsigned char *commit_data;
549 while (pos < g->num_commits_in_base)
552 lex_index = pos - g->num_commits_in_base;
553 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
554 item->graph_pos = pos;
555 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
558 static int fill_commit_in_graph(struct repository *r,
560 struct commit_graph *g, uint32_t pos)
563 uint32_t *parent_data_ptr;
564 uint64_t date_low, date_high;
565 struct commit_list **pptr;
566 const unsigned char *commit_data;
569 while (pos < g->num_commits_in_base)
572 if (pos >= g->num_commits + g->num_commits_in_base)
573 die(_("invalid commit position. commit-graph is likely corrupt"));
576 * Store the "full" position, but then use the
577 * "local" position for the rest of the calculation.
579 item->graph_pos = pos;
580 lex_index = pos - g->num_commits_in_base;
582 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
584 item->object.parsed = 1;
586 item->maybe_tree = NULL;
588 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
589 date_low = get_be32(commit_data + g->hash_len + 12);
590 item->date = (timestamp_t)((date_high << 32) | date_low);
592 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
594 pptr = &item->parents;
596 edge_value = get_be32(commit_data + g->hash_len);
597 if (edge_value == GRAPH_PARENT_NONE)
599 pptr = insert_parent_or_die(r, g, edge_value, pptr);
601 edge_value = get_be32(commit_data + g->hash_len + 4);
602 if (edge_value == GRAPH_PARENT_NONE)
604 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
605 pptr = insert_parent_or_die(r, g, edge_value, pptr);
609 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
610 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
612 edge_value = get_be32(parent_data_ptr);
613 pptr = insert_parent_or_die(r, g,
614 edge_value & GRAPH_EDGE_LAST_MASK,
617 } while (!(edge_value & GRAPH_LAST_EDGE));
622 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
624 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
625 *pos = item->graph_pos;
628 struct commit_graph *cur_g = g;
631 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
632 cur_g = cur_g->base_graph;
635 *pos = lex_index + cur_g->num_commits_in_base;
643 static int parse_commit_in_graph_one(struct repository *r,
644 struct commit_graph *g,
649 if (item->object.parsed)
652 if (find_commit_in_graph(item, g, &pos))
653 return fill_commit_in_graph(r, item, g, pos);
658 int parse_commit_in_graph(struct repository *r, struct commit *item)
660 if (!prepare_commit_graph(r))
662 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
665 void load_commit_graph_info(struct repository *r, struct commit *item)
668 if (!prepare_commit_graph(r))
670 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
671 fill_commit_graph_info(item, r->objects->commit_graph, pos);
674 static struct tree *load_tree_for_commit(struct repository *r,
675 struct commit_graph *g,
678 struct object_id oid;
679 const unsigned char *commit_data;
681 while (c->graph_pos < g->num_commits_in_base)
684 commit_data = g->chunk_commit_data +
685 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
687 hashcpy(oid.hash, commit_data);
688 c->maybe_tree = lookup_tree(r, &oid);
690 return c->maybe_tree;
693 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
694 struct commit_graph *g,
695 const struct commit *c)
698 return c->maybe_tree;
699 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
700 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
702 return load_tree_for_commit(r, g, (struct commit *)c);
705 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
707 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
710 struct packed_commit_list {
711 struct commit **list;
716 struct packed_oid_list {
717 struct object_id *list;
722 struct write_commit_graph_context {
723 struct repository *r;
726 struct packed_oid_list oids;
727 struct packed_commit_list commits;
729 unsigned long approx_nr_objects;
730 struct progress *progress;
732 uint64_t progress_cnt;
737 static void write_graph_chunk_fanout(struct hashfile *f,
738 struct write_commit_graph_context *ctx)
741 struct commit **list = ctx->commits.list;
744 * Write the first-level table (the list is sorted,
745 * but we use a 256-entry lookup to be able to avoid
746 * having to do eight extra binary search iterations).
748 for (i = 0; i < 256; i++) {
749 while (count < ctx->commits.nr) {
750 if ((*list)->object.oid.hash[0] != i)
752 display_progress(ctx->progress, ++ctx->progress_cnt);
757 hashwrite_be32(f, count);
761 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
762 struct write_commit_graph_context *ctx)
764 struct commit **list = ctx->commits.list;
766 for (count = 0; count < ctx->commits.nr; count++, list++) {
767 display_progress(ctx->progress, ++ctx->progress_cnt);
768 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
772 static const unsigned char *commit_to_sha1(size_t index, void *table)
774 struct commit **commits = table;
775 return commits[index]->object.oid.hash;
778 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
779 struct write_commit_graph_context *ctx)
781 struct commit **list = ctx->commits.list;
782 struct commit **last = ctx->commits.list + ctx->commits.nr;
783 uint32_t num_extra_edges = 0;
785 while (list < last) {
786 struct commit_list *parent;
788 uint32_t packedDate[2];
789 display_progress(ctx->progress, ++ctx->progress_cnt);
791 parse_commit_no_graph(*list);
792 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
794 parent = (*list)->parents;
797 edge_value = GRAPH_PARENT_NONE;
799 edge_value = sha1_pos(parent->item->object.oid.hash,
805 BUG("missing parent %s for commit %s",
806 oid_to_hex(&parent->item->object.oid),
807 oid_to_hex(&(*list)->object.oid));
810 hashwrite_be32(f, edge_value);
813 parent = parent->next;
816 edge_value = GRAPH_PARENT_NONE;
817 else if (parent->next)
818 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
820 edge_value = sha1_pos(parent->item->object.oid.hash,
825 BUG("missing parent %s for commit %s",
826 oid_to_hex(&parent->item->object.oid),
827 oid_to_hex(&(*list)->object.oid));
830 hashwrite_be32(f, edge_value);
832 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
835 parent = parent->next;
839 if (sizeof((*list)->date) > 4)
840 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
844 packedDate[0] |= htonl((*list)->generation << 2);
846 packedDate[1] = htonl((*list)->date);
847 hashwrite(f, packedDate, 8);
853 static void write_graph_chunk_extra_edges(struct hashfile *f,
854 struct write_commit_graph_context *ctx)
856 struct commit **list = ctx->commits.list;
857 struct commit **last = ctx->commits.list + ctx->commits.nr;
858 struct commit_list *parent;
860 while (list < last) {
863 display_progress(ctx->progress, ++ctx->progress_cnt);
865 for (parent = (*list)->parents; num_parents < 3 && parent;
866 parent = parent->next)
869 if (num_parents <= 2) {
874 /* Since num_parents > 2, this initializer is safe. */
875 for (parent = (*list)->parents->next; parent; parent = parent->next) {
876 int edge_value = sha1_pos(parent->item->object.oid.hash,
882 BUG("missing parent %s for commit %s",
883 oid_to_hex(&parent->item->object.oid),
884 oid_to_hex(&(*list)->object.oid));
885 else if (!parent->next)
886 edge_value |= GRAPH_LAST_EDGE;
888 hashwrite_be32(f, edge_value);
895 static int oid_compare(const void *_a, const void *_b)
897 const struct object_id *a = (const struct object_id *)_a;
898 const struct object_id *b = (const struct object_id *)_b;
902 static int add_packed_commits(const struct object_id *oid,
903 struct packed_git *pack,
907 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
908 enum object_type type;
909 off_t offset = nth_packed_object_offset(pack, pos);
910 struct object_info oi = OBJECT_INFO_INIT;
913 display_progress(ctx->progress, ++ctx->progress_done);
916 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
917 die(_("unable to get type of object %s"), oid_to_hex(oid));
919 if (type != OBJ_COMMIT)
922 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
923 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
929 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
931 struct commit_list *parent;
932 for (parent = commit->parents; parent; parent = parent->next) {
933 if (!(parent->item->object.flags & UNINTERESTING)) {
934 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
935 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
937 parent->item->object.flags |= UNINTERESTING;
942 static void close_reachable(struct write_commit_graph_context *ctx)
945 struct commit *commit;
947 if (ctx->report_progress)
948 ctx->progress = start_delayed_progress(
949 _("Loading known commits in commit graph"),
951 for (i = 0; i < ctx->oids.nr; i++) {
952 display_progress(ctx->progress, i + 1);
953 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
955 commit->object.flags |= UNINTERESTING;
957 stop_progress(&ctx->progress);
960 * As this loop runs, ctx->oids.nr may grow, but not more
961 * than the number of missing commits in the reachable
964 if (ctx->report_progress)
965 ctx->progress = start_delayed_progress(
966 _("Expanding reachable commits in commit graph"),
968 for (i = 0; i < ctx->oids.nr; i++) {
969 display_progress(ctx->progress, i + 1);
970 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
972 if (commit && !parse_commit_no_graph(commit))
973 add_missing_parents(ctx, commit);
975 stop_progress(&ctx->progress);
977 if (ctx->report_progress)
978 ctx->progress = start_delayed_progress(
979 _("Clearing commit marks in commit graph"),
981 for (i = 0; i < ctx->oids.nr; i++) {
982 display_progress(ctx->progress, i + 1);
983 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
986 commit->object.flags &= ~UNINTERESTING;
988 stop_progress(&ctx->progress);
991 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
994 struct commit_list *list = NULL;
996 if (ctx->report_progress)
997 ctx->progress = start_progress(
998 _("Computing commit graph generation numbers"),
1000 for (i = 0; i < ctx->commits.nr; i++) {
1001 display_progress(ctx->progress, i + 1);
1002 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1003 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
1006 commit_list_insert(ctx->commits.list[i], &list);
1008 struct commit *current = list->item;
1009 struct commit_list *parent;
1010 int all_parents_computed = 1;
1011 uint32_t max_generation = 0;
1013 for (parent = current->parents; parent; parent = parent->next) {
1014 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1015 parent->item->generation == GENERATION_NUMBER_ZERO) {
1016 all_parents_computed = 0;
1017 commit_list_insert(parent->item, &list);
1019 } else if (parent->item->generation > max_generation) {
1020 max_generation = parent->item->generation;
1024 if (all_parents_computed) {
1025 current->generation = max_generation + 1;
1028 if (current->generation > GENERATION_NUMBER_MAX)
1029 current->generation = GENERATION_NUMBER_MAX;
1033 stop_progress(&ctx->progress);
1036 static int add_ref_to_list(const char *refname,
1037 const struct object_id *oid,
1038 int flags, void *cb_data)
1040 struct string_list *list = (struct string_list *)cb_data;
1042 string_list_append(list, oid_to_hex(oid));
1046 int write_commit_graph_reachable(const char *obj_dir, unsigned int flags)
1048 struct string_list list = STRING_LIST_INIT_DUP;
1051 for_each_ref(add_ref_to_list, &list);
1052 result = write_commit_graph(obj_dir, NULL, &list,
1055 string_list_clear(&list, 0);
1059 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1060 struct string_list *pack_indexes)
1063 struct strbuf progress_title = STRBUF_INIT;
1064 struct strbuf packname = STRBUF_INIT;
1067 strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
1068 dirlen = packname.len;
1069 if (ctx->report_progress) {
1070 strbuf_addf(&progress_title,
1071 Q_("Finding commits for commit graph in %d pack",
1072 "Finding commits for commit graph in %d packs",
1075 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1076 ctx->progress_done = 0;
1078 for (i = 0; i < pack_indexes->nr; i++) {
1079 struct packed_git *p;
1080 strbuf_setlen(&packname, dirlen);
1081 strbuf_addstr(&packname, pack_indexes->items[i].string);
1082 p = add_packed_git(packname.buf, packname.len, 1);
1084 error(_("error adding pack %s"), packname.buf);
1087 if (open_pack_index(p)) {
1088 error(_("error opening index for %s"), packname.buf);
1091 for_each_object_in_pack(p, add_packed_commits, ctx,
1092 FOR_EACH_OBJECT_PACK_ORDER);
1097 stop_progress(&ctx->progress);
1098 strbuf_reset(&progress_title);
1099 strbuf_release(&packname);
1104 static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1105 struct string_list *commit_hex)
1108 struct strbuf progress_title = STRBUF_INIT;
1110 if (ctx->report_progress) {
1111 strbuf_addf(&progress_title,
1112 Q_("Finding commits for commit graph from %d ref",
1113 "Finding commits for commit graph from %d refs",
1116 ctx->progress = start_delayed_progress(
1120 for (i = 0; i < commit_hex->nr; i++) {
1122 struct object_id oid;
1123 struct commit *result;
1125 display_progress(ctx->progress, i + 1);
1126 if (commit_hex->items[i].string &&
1127 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
1130 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1133 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1134 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1138 stop_progress(&ctx->progress);
1139 strbuf_release(&progress_title);
1142 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1144 if (ctx->report_progress)
1145 ctx->progress = start_delayed_progress(
1146 _("Finding commits for commit graph among packed objects"),
1147 ctx->approx_nr_objects);
1148 for_each_packed_object(add_packed_commits, ctx,
1149 FOR_EACH_OBJECT_PACK_ORDER);
1150 if (ctx->progress_done < ctx->approx_nr_objects)
1151 display_progress(ctx->progress, ctx->approx_nr_objects);
1152 stop_progress(&ctx->progress);
1155 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1157 uint32_t i, count_distinct = 1;
1159 if (ctx->report_progress)
1160 ctx->progress = start_delayed_progress(
1161 _("Counting distinct commits in commit graph"),
1163 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1164 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1166 for (i = 1; i < ctx->oids.nr; i++) {
1167 display_progress(ctx->progress, i + 1);
1168 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1171 stop_progress(&ctx->progress);
1173 return count_distinct;
1176 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1179 struct commit_list *parent;
1181 ctx->num_extra_edges = 0;
1182 if (ctx->report_progress)
1183 ctx->progress = start_delayed_progress(
1184 _("Finding extra edges in commit graph"),
1186 for (i = 0; i < ctx->oids.nr; i++) {
1187 int num_parents = 0;
1188 display_progress(ctx->progress, i + 1);
1189 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1192 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1193 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1195 for (parent = ctx->commits.list[ctx->commits.nr]->parents;
1196 parent; parent = parent->next)
1199 if (num_parents > 2)
1200 ctx->num_extra_edges += num_parents - 1;
1204 stop_progress(&ctx->progress);
1207 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1211 struct lock_file lk = LOCK_INIT;
1212 uint32_t chunk_ids[5];
1213 uint64_t chunk_offsets[5];
1214 const unsigned hashsz = the_hash_algo->rawsz;
1215 struct strbuf progress_title = STRBUF_INIT;
1218 ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1219 if (safe_create_leading_directories(ctx->graph_name)) {
1220 UNLEAK(ctx->graph_name);
1221 error(_("unable to create leading directories of %s"),
1226 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1227 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1229 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1230 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1231 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1232 if (ctx->num_extra_edges) {
1233 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1237 chunk_ids[num_chunks] = 0;
1239 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1240 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1241 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1242 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1245 if (ctx->num_extra_edges) {
1246 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1247 4 * ctx->num_extra_edges;
1251 hashwrite_be32(f, GRAPH_SIGNATURE);
1253 hashwrite_u8(f, GRAPH_VERSION);
1254 hashwrite_u8(f, oid_version());
1255 hashwrite_u8(f, num_chunks);
1258 for (i = 0; i <= num_chunks; i++) {
1259 uint32_t chunk_write[3];
1261 chunk_write[0] = htonl(chunk_ids[i]);
1262 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1263 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1264 hashwrite(f, chunk_write, 12);
1267 if (ctx->report_progress) {
1268 strbuf_addf(&progress_title,
1269 Q_("Writing out commit graph in %d pass",
1270 "Writing out commit graph in %d passes",
1273 ctx->progress = start_delayed_progress(
1275 num_chunks * ctx->commits.nr);
1277 write_graph_chunk_fanout(f, ctx);
1278 write_graph_chunk_oids(f, hashsz, ctx);
1279 write_graph_chunk_data(f, hashsz, ctx);
1280 if (ctx->num_extra_edges)
1281 write_graph_chunk_extra_edges(f, ctx);
1282 stop_progress(&ctx->progress);
1283 strbuf_release(&progress_title);
1285 close_commit_graph(ctx->r->objects);
1286 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1287 commit_lock_file(&lk);
1292 int write_commit_graph(const char *obj_dir,
1293 struct string_list *pack_indexes,
1294 struct string_list *commit_hex,
1297 struct write_commit_graph_context *ctx;
1298 uint32_t i, count_distinct = 0;
1301 if (!commit_graph_compatible(the_repository))
1304 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1305 ctx->r = the_repository;
1306 ctx->obj_dir = obj_dir;
1307 ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
1308 ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
1310 ctx->approx_nr_objects = approximate_object_count();
1311 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1314 prepare_commit_graph_one(ctx->r, ctx->obj_dir);
1315 if (ctx->r->objects->commit_graph)
1316 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
1319 if (ctx->oids.alloc < 1024)
1320 ctx->oids.alloc = 1024;
1321 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1323 if (ctx->append && ctx->r->objects->commit_graph) {
1324 struct commit_graph *g = ctx->r->objects->commit_graph;
1325 for (i = 0; i < g->num_commits; i++) {
1326 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1327 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
1332 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1337 fill_oids_from_commit_hex(ctx, commit_hex);
1339 if (!pack_indexes && !commit_hex)
1340 fill_oids_from_all_packs(ctx);
1342 close_reachable(ctx);
1344 count_distinct = count_distinct_commits(ctx);
1346 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1347 error(_("the commit graph format cannot write %d commits"), count_distinct);
1352 ctx->commits.alloc = count_distinct;
1353 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
1355 copy_oids_to_commits(ctx);
1357 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
1358 error(_("too many commits to write graph"));
1363 compute_generation_numbers(ctx);
1365 res = write_commit_graph_file(ctx);
1368 free(ctx->graph_name);
1369 free(ctx->commits.list);
1370 free(ctx->oids.list);
1376 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1377 static int verify_commit_graph_error;
1379 static void graph_report(const char *fmt, ...)
1383 verify_commit_graph_error = 1;
1385 vfprintf(stderr, fmt, ap);
1386 fprintf(stderr, "\n");
1390 #define GENERATION_ZERO_EXISTS 1
1391 #define GENERATION_NUMBER_EXISTS 2
1393 int verify_commit_graph(struct repository *r, struct commit_graph *g)
1395 uint32_t i, cur_fanout_pos = 0;
1396 struct object_id prev_oid, cur_oid, checksum;
1397 int generation_zero = 0;
1400 struct progress *progress = NULL;
1403 graph_report("no commit-graph file loaded");
1407 verify_commit_graph_error = verify_commit_graph_lite(g);
1408 if (verify_commit_graph_error)
1409 return verify_commit_graph_error;
1411 devnull = open("/dev/null", O_WRONLY);
1412 f = hashfd(devnull, NULL);
1413 hashwrite(f, g->data, g->data_len - g->hash_len);
1414 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1415 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1416 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1417 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1420 for (i = 0; i < g->num_commits; i++) {
1421 struct commit *graph_commit;
1423 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1425 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1426 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1427 oid_to_hex(&prev_oid),
1428 oid_to_hex(&cur_oid));
1430 oidcpy(&prev_oid, &cur_oid);
1432 while (cur_oid.hash[0] > cur_fanout_pos) {
1433 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1435 if (i != fanout_value)
1436 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1437 cur_fanout_pos, fanout_value, i);
1441 graph_commit = lookup_commit(r, &cur_oid);
1442 if (!parse_commit_in_graph_one(r, g, graph_commit))
1443 graph_report(_("failed to parse commit %s from commit-graph"),
1444 oid_to_hex(&cur_oid));
1447 while (cur_fanout_pos < 256) {
1448 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1450 if (g->num_commits != fanout_value)
1451 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1452 cur_fanout_pos, fanout_value, i);
1457 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1458 return verify_commit_graph_error;
1460 progress = start_progress(_("Verifying commits in commit graph"),
1462 for (i = 0; i < g->num_commits; i++) {
1463 struct commit *graph_commit, *odb_commit;
1464 struct commit_list *graph_parents, *odb_parents;
1465 uint32_t max_generation = 0;
1467 display_progress(progress, i + 1);
1468 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1470 graph_commit = lookup_commit(r, &cur_oid);
1471 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1472 if (parse_commit_internal(odb_commit, 0, 0)) {
1473 graph_report(_("failed to parse commit %s from object database for commit-graph"),
1474 oid_to_hex(&cur_oid));
1478 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
1479 get_commit_tree_oid(odb_commit)))
1480 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
1481 oid_to_hex(&cur_oid),
1482 oid_to_hex(get_commit_tree_oid(graph_commit)),
1483 oid_to_hex(get_commit_tree_oid(odb_commit)));
1485 graph_parents = graph_commit->parents;
1486 odb_parents = odb_commit->parents;
1488 while (graph_parents) {
1489 if (odb_parents == NULL) {
1490 graph_report(_("commit-graph parent list for commit %s is too long"),
1491 oid_to_hex(&cur_oid));
1495 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1496 graph_report(_("commit-graph parent for %s is %s != %s"),
1497 oid_to_hex(&cur_oid),
1498 oid_to_hex(&graph_parents->item->object.oid),
1499 oid_to_hex(&odb_parents->item->object.oid));
1501 if (graph_parents->item->generation > max_generation)
1502 max_generation = graph_parents->item->generation;
1504 graph_parents = graph_parents->next;
1505 odb_parents = odb_parents->next;
1508 if (odb_parents != NULL)
1509 graph_report(_("commit-graph parent list for commit %s terminates early"),
1510 oid_to_hex(&cur_oid));
1512 if (!graph_commit->generation) {
1513 if (generation_zero == GENERATION_NUMBER_EXISTS)
1514 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
1515 oid_to_hex(&cur_oid));
1516 generation_zero = GENERATION_ZERO_EXISTS;
1517 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1518 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
1519 oid_to_hex(&cur_oid));
1521 if (generation_zero == GENERATION_ZERO_EXISTS)
1525 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1526 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1527 * extra logic in the following condition.
1529 if (max_generation == GENERATION_NUMBER_MAX)
1532 if (graph_commit->generation != max_generation + 1)
1533 graph_report(_("commit-graph generation for commit %s is %u != %u"),
1534 oid_to_hex(&cur_oid),
1535 graph_commit->generation,
1536 max_generation + 1);
1538 if (graph_commit->date != odb_commit->date)
1539 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
1540 oid_to_hex(&cur_oid),
1544 stop_progress(&progress);
1546 return verify_commit_graph_error;
1549 void free_commit_graph(struct commit_graph *g)
1553 if (g->graph_fd >= 0) {
1554 munmap((void *)g->data, g->data_len);