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 #include "commit-slab.h"
22 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
23 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
24 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
25 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
26 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
27 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
28 #define MAX_NUM_CHUNKS 5
30 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
32 #define GRAPH_VERSION_1 0x1
33 #define GRAPH_VERSION GRAPH_VERSION_1
35 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
36 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
37 #define GRAPH_PARENT_NONE 0x70000000
39 #define GRAPH_LAST_EDGE 0x80000000
41 #define GRAPH_HEADER_SIZE 8
42 #define GRAPH_FANOUT_SIZE (4 * 256)
43 #define GRAPH_CHUNKLOOKUP_WIDTH 12
44 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
45 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
47 /* Remember to update object flag allocation in object.h */
48 #define REACHABLE (1u<<15)
50 /* Keep track of the order in which commits are added to our list. */
51 define_commit_slab(commit_pos, int);
52 static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
54 static void set_commit_pos(struct repository *r, const struct object_id *oid)
56 static int32_t max_pos;
57 struct commit *commit = lookup_commit(r, oid);
60 return; /* should never happen, but be lenient */
62 *commit_pos_at(&commit_pos, commit) = max_pos++;
65 static int commit_pos_cmp(const void *va, const void *vb)
67 const struct commit *a = *(const struct commit **)va;
68 const struct commit *b = *(const struct commit **)vb;
69 return commit_pos_at(&commit_pos, a) -
70 commit_pos_at(&commit_pos, b);
73 static int commit_gen_cmp(const void *va, const void *vb)
75 const struct commit *a = *(const struct commit **)va;
76 const struct commit *b = *(const struct commit **)vb;
78 /* lower generation commits first */
79 if (a->generation < b->generation)
81 else if (a->generation > b->generation)
84 /* use date as a heuristic when generations are equal */
85 if (a->date < b->date)
87 else if (a->date > b->date)
92 char *get_commit_graph_filename(struct object_directory *obj_dir)
94 return xstrfmt("%s/info/commit-graph", obj_dir->path);
97 static char *get_split_graph_filename(struct object_directory *odb,
100 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
104 static char *get_chain_filename(struct object_directory *odb)
106 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
109 static uint8_t oid_version(void)
114 static struct commit_graph *alloc_commit_graph(void)
116 struct commit_graph *g = xcalloc(1, sizeof(*g));
122 extern int read_replace_refs;
124 static int commit_graph_compatible(struct repository *r)
129 if (read_replace_refs) {
130 prepare_replace_object(r);
131 if (hashmap_get_size(&r->objects->replace_map->map))
135 prepare_commit_graft(r);
136 if (r->parsed_objects && r->parsed_objects->grafts_nr)
138 if (is_repository_shallow(r))
144 int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
146 *fd = git_open(graph_file);
149 if (fstat(*fd, st)) {
156 struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
157 struct object_directory *odb)
161 struct commit_graph *ret;
163 graph_size = xsize_t(st->st_size);
165 if (graph_size < GRAPH_MIN_SIZE) {
167 error(_("commit-graph file is too small"));
170 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
171 ret = parse_commit_graph(graph_map, fd, graph_size);
176 munmap(graph_map, graph_size);
183 static int verify_commit_graph_lite(struct commit_graph *g)
186 * Basic validation shared between parse_commit_graph()
187 * which'll be called every time the graph is used, and the
188 * much more expensive verify_commit_graph() used by
189 * "commit-graph verify".
191 * There should only be very basic checks here to ensure that
192 * we don't e.g. segfault in fill_commit_in_graph(), but
193 * because this is a very hot codepath nothing that e.g. loops
194 * over g->num_commits, or runs a checksum on the commit-graph
197 if (!g->chunk_oid_fanout) {
198 error("commit-graph is missing the OID Fanout chunk");
201 if (!g->chunk_oid_lookup) {
202 error("commit-graph is missing the OID Lookup chunk");
205 if (!g->chunk_commit_data) {
206 error("commit-graph is missing the Commit Data chunk");
213 struct commit_graph *parse_commit_graph(void *graph_map, int fd,
216 const unsigned char *data, *chunk_lookup;
218 struct commit_graph *graph;
219 uint64_t last_chunk_offset;
220 uint32_t last_chunk_id;
221 uint32_t graph_signature;
222 unsigned char graph_version, hash_version;
227 if (graph_size < GRAPH_MIN_SIZE)
230 data = (const unsigned char *)graph_map;
232 graph_signature = get_be32(data);
233 if (graph_signature != GRAPH_SIGNATURE) {
234 error(_("commit-graph signature %X does not match signature %X"),
235 graph_signature, GRAPH_SIGNATURE);
239 graph_version = *(unsigned char*)(data + 4);
240 if (graph_version != GRAPH_VERSION) {
241 error(_("commit-graph version %X does not match version %X"),
242 graph_version, GRAPH_VERSION);
246 hash_version = *(unsigned char*)(data + 5);
247 if (hash_version != oid_version()) {
248 error(_("commit-graph hash version %X does not match version %X"),
249 hash_version, oid_version());
253 graph = alloc_commit_graph();
255 graph->hash_len = the_hash_algo->rawsz;
256 graph->num_chunks = *(unsigned char*)(data + 6);
257 graph->graph_fd = fd;
258 graph->data = graph_map;
259 graph->data_len = graph_size;
262 last_chunk_offset = 8;
263 chunk_lookup = data + 8;
264 for (i = 0; i < graph->num_chunks; i++) {
266 uint64_t chunk_offset;
267 int chunk_repeated = 0;
269 if (data + graph_size - chunk_lookup <
270 GRAPH_CHUNKLOOKUP_WIDTH) {
271 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
276 chunk_id = get_be32(chunk_lookup + 0);
277 chunk_offset = get_be64(chunk_lookup + 4);
279 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
281 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
282 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
283 (uint32_t)chunk_offset);
289 case GRAPH_CHUNKID_OIDFANOUT:
290 if (graph->chunk_oid_fanout)
293 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
296 case GRAPH_CHUNKID_OIDLOOKUP:
297 if (graph->chunk_oid_lookup)
300 graph->chunk_oid_lookup = data + chunk_offset;
303 case GRAPH_CHUNKID_DATA:
304 if (graph->chunk_commit_data)
307 graph->chunk_commit_data = data + chunk_offset;
310 case GRAPH_CHUNKID_EXTRAEDGES:
311 if (graph->chunk_extra_edges)
314 graph->chunk_extra_edges = data + chunk_offset;
317 case GRAPH_CHUNKID_BASE:
318 if (graph->chunk_base_graphs)
321 graph->chunk_base_graphs = data + chunk_offset;
324 if (chunk_repeated) {
325 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
330 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
332 graph->num_commits = (chunk_offset - last_chunk_offset)
336 last_chunk_id = chunk_id;
337 last_chunk_offset = chunk_offset;
340 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
342 if (verify_commit_graph_lite(graph)) {
350 static struct commit_graph *load_commit_graph_one(const char *graph_file,
351 struct object_directory *odb)
356 struct commit_graph *g;
357 int open_ok = open_commit_graph(graph_file, &fd, &st);
362 g = load_commit_graph_one_fd_st(fd, &st, odb);
365 g->filename = xstrdup(graph_file);
370 static struct commit_graph *load_commit_graph_v1(struct repository *r,
371 struct object_directory *odb)
373 char *graph_name = get_commit_graph_filename(odb);
374 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
380 static int add_graph_to_chain(struct commit_graph *g,
381 struct commit_graph *chain,
382 struct object_id *oids,
385 struct commit_graph *cur_g = chain;
387 if (n && !g->chunk_base_graphs) {
388 warning(_("commit-graph has no base graphs chunk"));
396 !oideq(&oids[n], &cur_g->oid) ||
397 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
398 warning(_("commit-graph chain does not match"));
402 cur_g = cur_g->base_graph;
405 g->base_graph = chain;
408 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
413 static struct commit_graph *load_commit_graph_chain(struct repository *r,
414 struct object_directory *odb)
416 struct commit_graph *graph_chain = NULL;
417 struct strbuf line = STRBUF_INIT;
419 struct object_id *oids;
420 int i = 0, valid = 1, count;
421 char *chain_name = get_chain_filename(odb);
425 fp = fopen(chain_name, "r");
426 stat_res = stat(chain_name, &st);
431 st.st_size <= the_hash_algo->hexsz)
434 count = st.st_size / (the_hash_algo->hexsz + 1);
435 oids = xcalloc(count, sizeof(struct object_id));
439 for (i = 0; i < count; i++) {
440 struct object_directory *odb;
442 if (strbuf_getline_lf(&line, fp) == EOF)
445 if (get_oid_hex(line.buf, &oids[i])) {
446 warning(_("invalid commit-graph chain: line '%s' not a hash"),
453 for (odb = r->objects->odb; odb; odb = odb->next) {
454 char *graph_name = get_split_graph_filename(odb, line.buf);
455 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
460 if (add_graph_to_chain(g, graph_chain, oids, i)) {
470 warning(_("unable to find all commit-graph files"));
477 strbuf_release(&line);
482 struct commit_graph *read_commit_graph_one(struct repository *r,
483 struct object_directory *odb)
485 struct commit_graph *g = load_commit_graph_v1(r, odb);
488 g = load_commit_graph_chain(r, odb);
493 static void prepare_commit_graph_one(struct repository *r,
494 struct object_directory *odb)
497 if (r->objects->commit_graph)
500 r->objects->commit_graph = read_commit_graph_one(r, odb);
504 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
506 * On the first invocation, this function attempts to load the commit
507 * graph if the_repository is configured to have one.
509 static int prepare_commit_graph(struct repository *r)
511 struct object_directory *odb;
514 * This must come before the "already attempted?" check below, because
515 * we want to disable even an already-loaded graph file.
517 if (r->commit_graph_disabled)
520 if (r->objects->commit_graph_attempted)
521 return !!r->objects->commit_graph;
522 r->objects->commit_graph_attempted = 1;
524 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
525 die("dying as requested by the '%s' variable on commit-graph load!",
526 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
528 prepare_repo_settings(r);
530 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
531 r->settings.core_commit_graph != 1)
533 * This repository is not configured to use commit graphs, so
534 * do not load one. (But report commit_graph_attempted anyway
535 * so that commit graph loading is not attempted again for this
540 if (!commit_graph_compatible(r))
544 for (odb = r->objects->odb;
545 !r->objects->commit_graph && odb;
547 prepare_commit_graph_one(r, odb);
548 return !!r->objects->commit_graph;
551 int generation_numbers_enabled(struct repository *r)
553 uint32_t first_generation;
554 struct commit_graph *g;
555 if (!prepare_commit_graph(r))
558 g = r->objects->commit_graph;
563 first_generation = get_be32(g->chunk_commit_data +
564 g->hash_len + 8) >> 2;
566 return !!first_generation;
569 static void close_commit_graph_one(struct commit_graph *g)
574 close_commit_graph_one(g->base_graph);
575 free_commit_graph(g);
578 void close_commit_graph(struct raw_object_store *o)
580 close_commit_graph_one(o->commit_graph);
581 o->commit_graph = NULL;
584 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
586 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
587 g->chunk_oid_lookup, g->hash_len, pos);
590 static void load_oid_from_graph(struct commit_graph *g,
592 struct object_id *oid)
596 while (g && pos < g->num_commits_in_base)
600 BUG("NULL commit-graph");
602 if (pos >= g->num_commits + g->num_commits_in_base)
603 die(_("invalid commit position. commit-graph is likely corrupt"));
605 lex_index = pos - g->num_commits_in_base;
607 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
610 static struct commit_list **insert_parent_or_die(struct repository *r,
611 struct commit_graph *g,
613 struct commit_list **pptr)
616 struct object_id oid;
618 if (pos >= g->num_commits + g->num_commits_in_base)
619 die("invalid parent position %"PRIu32, pos);
621 load_oid_from_graph(g, pos, &oid);
622 c = lookup_commit(r, &oid);
624 die(_("could not find commit %s"), oid_to_hex(&oid));
626 return &commit_list_insert(c, pptr)->next;
629 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
631 const unsigned char *commit_data;
634 while (pos < g->num_commits_in_base)
637 lex_index = pos - g->num_commits_in_base;
638 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
639 item->graph_pos = pos;
640 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
643 static inline void set_commit_tree(struct commit *c, struct tree *t)
648 static int fill_commit_in_graph(struct repository *r,
650 struct commit_graph *g, uint32_t pos)
653 uint32_t *parent_data_ptr;
654 uint64_t date_low, date_high;
655 struct commit_list **pptr;
656 const unsigned char *commit_data;
659 while (pos < g->num_commits_in_base)
662 if (pos >= g->num_commits + g->num_commits_in_base)
663 die(_("invalid commit position. commit-graph is likely corrupt"));
666 * Store the "full" position, but then use the
667 * "local" position for the rest of the calculation.
669 item->graph_pos = pos;
670 lex_index = pos - g->num_commits_in_base;
672 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
674 item->object.parsed = 1;
676 set_commit_tree(item, NULL);
678 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
679 date_low = get_be32(commit_data + g->hash_len + 12);
680 item->date = (timestamp_t)((date_high << 32) | date_low);
682 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
684 pptr = &item->parents;
686 edge_value = get_be32(commit_data + g->hash_len);
687 if (edge_value == GRAPH_PARENT_NONE)
689 pptr = insert_parent_or_die(r, g, edge_value, pptr);
691 edge_value = get_be32(commit_data + g->hash_len + 4);
692 if (edge_value == GRAPH_PARENT_NONE)
694 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
695 pptr = insert_parent_or_die(r, g, edge_value, pptr);
699 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
700 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
702 edge_value = get_be32(parent_data_ptr);
703 pptr = insert_parent_or_die(r, g,
704 edge_value & GRAPH_EDGE_LAST_MASK,
707 } while (!(edge_value & GRAPH_LAST_EDGE));
712 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
714 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
715 *pos = item->graph_pos;
718 struct commit_graph *cur_g = g;
721 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
722 cur_g = cur_g->base_graph;
725 *pos = lex_index + cur_g->num_commits_in_base;
733 static int parse_commit_in_graph_one(struct repository *r,
734 struct commit_graph *g,
739 if (item->object.parsed)
742 if (find_commit_in_graph(item, g, &pos))
743 return fill_commit_in_graph(r, item, g, pos);
748 int parse_commit_in_graph(struct repository *r, struct commit *item)
750 if (!prepare_commit_graph(r))
752 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
755 void load_commit_graph_info(struct repository *r, struct commit *item)
758 if (!prepare_commit_graph(r))
760 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
761 fill_commit_graph_info(item, r->objects->commit_graph, pos);
764 static struct tree *load_tree_for_commit(struct repository *r,
765 struct commit_graph *g,
768 struct object_id oid;
769 const unsigned char *commit_data;
771 while (c->graph_pos < g->num_commits_in_base)
774 commit_data = g->chunk_commit_data +
775 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
777 hashcpy(oid.hash, commit_data);
778 set_commit_tree(c, lookup_tree(r, &oid));
780 return c->maybe_tree;
783 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
784 struct commit_graph *g,
785 const struct commit *c)
788 return c->maybe_tree;
789 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
790 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
792 return load_tree_for_commit(r, g, (struct commit *)c);
795 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
797 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
800 struct packed_commit_list {
801 struct commit **list;
806 struct packed_oid_list {
807 struct object_id *list;
812 struct write_commit_graph_context {
813 struct repository *r;
814 struct object_directory *odb;
816 struct packed_oid_list oids;
817 struct packed_commit_list commits;
819 unsigned long approx_nr_objects;
820 struct progress *progress;
822 uint64_t progress_cnt;
824 char *base_graph_name;
825 int num_commit_graphs_before;
826 int num_commit_graphs_after;
827 char **commit_graph_filenames_before;
828 char **commit_graph_filenames_after;
829 char **commit_graph_hash_after;
830 uint32_t new_num_commits_in_base;
831 struct commit_graph *new_base_graph;
840 const struct split_commit_graph_opts *split_opts;
841 size_t total_bloom_filter_data_size;
844 static void write_graph_chunk_fanout(struct hashfile *f,
845 struct write_commit_graph_context *ctx)
848 struct commit **list = ctx->commits.list;
851 * Write the first-level table (the list is sorted,
852 * but we use a 256-entry lookup to be able to avoid
853 * having to do eight extra binary search iterations).
855 for (i = 0; i < 256; i++) {
856 while (count < ctx->commits.nr) {
857 if ((*list)->object.oid.hash[0] != i)
859 display_progress(ctx->progress, ++ctx->progress_cnt);
864 hashwrite_be32(f, count);
868 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
869 struct write_commit_graph_context *ctx)
871 struct commit **list = ctx->commits.list;
873 for (count = 0; count < ctx->commits.nr; count++, list++) {
874 display_progress(ctx->progress, ++ctx->progress_cnt);
875 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
879 static const unsigned char *commit_to_sha1(size_t index, void *table)
881 struct commit **commits = table;
882 return commits[index]->object.oid.hash;
885 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
886 struct write_commit_graph_context *ctx)
888 struct commit **list = ctx->commits.list;
889 struct commit **last = ctx->commits.list + ctx->commits.nr;
890 uint32_t num_extra_edges = 0;
892 while (list < last) {
893 struct commit_list *parent;
894 struct object_id *tree;
896 uint32_t packedDate[2];
897 display_progress(ctx->progress, ++ctx->progress_cnt);
899 if (parse_commit_no_graph(*list))
900 die(_("unable to parse commit %s"),
901 oid_to_hex(&(*list)->object.oid));
902 tree = get_commit_tree_oid(*list);
903 hashwrite(f, tree->hash, hash_len);
905 parent = (*list)->parents;
908 edge_value = GRAPH_PARENT_NONE;
910 edge_value = sha1_pos(parent->item->object.oid.hash,
916 edge_value += ctx->new_num_commits_in_base;
919 if (find_commit_in_graph(parent->item,
926 BUG("missing parent %s for commit %s",
927 oid_to_hex(&parent->item->object.oid),
928 oid_to_hex(&(*list)->object.oid));
931 hashwrite_be32(f, edge_value);
934 parent = parent->next;
937 edge_value = GRAPH_PARENT_NONE;
938 else if (parent->next)
939 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
941 edge_value = sha1_pos(parent->item->object.oid.hash,
947 edge_value += ctx->new_num_commits_in_base;
950 if (find_commit_in_graph(parent->item,
957 BUG("missing parent %s for commit %s",
958 oid_to_hex(&parent->item->object.oid),
959 oid_to_hex(&(*list)->object.oid));
962 hashwrite_be32(f, edge_value);
964 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
967 parent = parent->next;
971 if (sizeof((*list)->date) > 4)
972 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
976 packedDate[0] |= htonl((*list)->generation << 2);
978 packedDate[1] = htonl((*list)->date);
979 hashwrite(f, packedDate, 8);
985 static void write_graph_chunk_extra_edges(struct hashfile *f,
986 struct write_commit_graph_context *ctx)
988 struct commit **list = ctx->commits.list;
989 struct commit **last = ctx->commits.list + ctx->commits.nr;
990 struct commit_list *parent;
992 while (list < last) {
995 display_progress(ctx->progress, ++ctx->progress_cnt);
997 for (parent = (*list)->parents; num_parents < 3 && parent;
998 parent = parent->next)
1001 if (num_parents <= 2) {
1006 /* Since num_parents > 2, this initializer is safe. */
1007 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1008 int edge_value = sha1_pos(parent->item->object.oid.hash,
1013 if (edge_value >= 0)
1014 edge_value += ctx->new_num_commits_in_base;
1017 if (find_commit_in_graph(parent->item,
1018 ctx->new_base_graph,
1024 BUG("missing parent %s for commit %s",
1025 oid_to_hex(&parent->item->object.oid),
1026 oid_to_hex(&(*list)->object.oid));
1027 else if (!parent->next)
1028 edge_value |= GRAPH_LAST_EDGE;
1030 hashwrite_be32(f, edge_value);
1037 static int oid_compare(const void *_a, const void *_b)
1039 const struct object_id *a = (const struct object_id *)_a;
1040 const struct object_id *b = (const struct object_id *)_b;
1041 return oidcmp(a, b);
1044 static int add_packed_commits(const struct object_id *oid,
1045 struct packed_git *pack,
1049 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
1050 enum object_type type;
1051 off_t offset = nth_packed_object_offset(pack, pos);
1052 struct object_info oi = OBJECT_INFO_INIT;
1055 display_progress(ctx->progress, ++ctx->progress_done);
1058 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
1059 die(_("unable to get type of object %s"), oid_to_hex(oid));
1061 if (type != OBJ_COMMIT)
1064 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1065 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1068 set_commit_pos(ctx->r, oid);
1073 static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1075 struct commit_list *parent;
1076 for (parent = commit->parents; parent; parent = parent->next) {
1077 if (!(parent->item->object.flags & REACHABLE)) {
1078 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1079 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1081 parent->item->object.flags |= REACHABLE;
1086 static void close_reachable(struct write_commit_graph_context *ctx)
1089 struct commit *commit;
1091 if (ctx->report_progress)
1092 ctx->progress = start_delayed_progress(
1093 _("Loading known commits in commit graph"),
1095 for (i = 0; i < ctx->oids.nr; i++) {
1096 display_progress(ctx->progress, i + 1);
1097 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1099 commit->object.flags |= REACHABLE;
1101 stop_progress(&ctx->progress);
1104 * As this loop runs, ctx->oids.nr may grow, but not more
1105 * than the number of missing commits in the reachable
1108 if (ctx->report_progress)
1109 ctx->progress = start_delayed_progress(
1110 _("Expanding reachable commits in commit graph"),
1112 for (i = 0; i < ctx->oids.nr; i++) {
1113 display_progress(ctx->progress, i + 1);
1114 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1119 if (!parse_commit(commit) &&
1120 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1121 add_missing_parents(ctx, commit);
1122 } else if (!parse_commit_no_graph(commit))
1123 add_missing_parents(ctx, commit);
1125 stop_progress(&ctx->progress);
1127 if (ctx->report_progress)
1128 ctx->progress = start_delayed_progress(
1129 _("Clearing commit marks in commit graph"),
1131 for (i = 0; i < ctx->oids.nr; i++) {
1132 display_progress(ctx->progress, i + 1);
1133 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1136 commit->object.flags &= ~REACHABLE;
1138 stop_progress(&ctx->progress);
1141 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1144 struct commit_list *list = NULL;
1146 if (ctx->report_progress)
1147 ctx->progress = start_delayed_progress(
1148 _("Computing commit graph generation numbers"),
1150 for (i = 0; i < ctx->commits.nr; i++) {
1151 display_progress(ctx->progress, i + 1);
1152 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1153 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
1156 commit_list_insert(ctx->commits.list[i], &list);
1158 struct commit *current = list->item;
1159 struct commit_list *parent;
1160 int all_parents_computed = 1;
1161 uint32_t max_generation = 0;
1163 for (parent = current->parents; parent; parent = parent->next) {
1164 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1165 parent->item->generation == GENERATION_NUMBER_ZERO) {
1166 all_parents_computed = 0;
1167 commit_list_insert(parent->item, &list);
1169 } else if (parent->item->generation > max_generation) {
1170 max_generation = parent->item->generation;
1174 if (all_parents_computed) {
1175 current->generation = max_generation + 1;
1178 if (current->generation > GENERATION_NUMBER_MAX)
1179 current->generation = GENERATION_NUMBER_MAX;
1183 stop_progress(&ctx->progress);
1186 static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1189 struct progress *progress = NULL;
1190 struct commit **sorted_commits;
1192 init_bloom_filters();
1194 if (ctx->report_progress)
1195 progress = start_delayed_progress(
1196 _("Computing commit changed paths Bloom filters"),
1199 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1200 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1202 if (ctx->order_by_pack)
1203 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1205 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
1207 for (i = 0; i < ctx->commits.nr; i++) {
1208 struct commit *c = sorted_commits[i];
1209 struct bloom_filter *filter = get_bloom_filter(ctx->r, c);
1210 ctx->total_bloom_filter_data_size += sizeof(unsigned char) * filter->len;
1211 display_progress(progress, i + 1);
1214 free(sorted_commits);
1215 stop_progress(&progress);
1218 static int add_ref_to_list(const char *refname,
1219 const struct object_id *oid,
1220 int flags, void *cb_data)
1222 struct string_list *list = (struct string_list *)cb_data;
1224 string_list_append(list, oid_to_hex(oid));
1228 int write_commit_graph_reachable(struct object_directory *odb,
1229 enum commit_graph_write_flags flags,
1230 const struct split_commit_graph_opts *split_opts)
1232 struct string_list list = STRING_LIST_INIT_DUP;
1235 for_each_ref(add_ref_to_list, &list);
1236 result = write_commit_graph(odb, NULL, &list,
1239 string_list_clear(&list, 0);
1243 static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1244 struct string_list *pack_indexes)
1247 struct strbuf progress_title = STRBUF_INIT;
1248 struct strbuf packname = STRBUF_INIT;
1251 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
1252 dirlen = packname.len;
1253 if (ctx->report_progress) {
1254 strbuf_addf(&progress_title,
1255 Q_("Finding commits for commit graph in %d pack",
1256 "Finding commits for commit graph in %d packs",
1259 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1260 ctx->progress_done = 0;
1262 for (i = 0; i < pack_indexes->nr; i++) {
1263 struct packed_git *p;
1264 strbuf_setlen(&packname, dirlen);
1265 strbuf_addstr(&packname, pack_indexes->items[i].string);
1266 p = add_packed_git(packname.buf, packname.len, 1);
1268 error(_("error adding pack %s"), packname.buf);
1271 if (open_pack_index(p)) {
1272 error(_("error opening index for %s"), packname.buf);
1275 for_each_object_in_pack(p, add_packed_commits, ctx,
1276 FOR_EACH_OBJECT_PACK_ORDER);
1281 stop_progress(&ctx->progress);
1282 strbuf_release(&progress_title);
1283 strbuf_release(&packname);
1288 static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1289 struct string_list *commit_hex)
1292 struct strbuf progress_title = STRBUF_INIT;
1294 if (ctx->report_progress) {
1295 strbuf_addf(&progress_title,
1296 Q_("Finding commits for commit graph from %d ref",
1297 "Finding commits for commit graph from %d refs",
1300 ctx->progress = start_delayed_progress(
1304 for (i = 0; i < commit_hex->nr; i++) {
1306 struct object_id oid;
1307 struct commit *result;
1309 display_progress(ctx->progress, i + 1);
1310 if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1311 (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
1312 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1313 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1315 } else if (ctx->check_oids) {
1316 error(_("invalid commit object id: %s"),
1317 commit_hex->items[i].string);
1321 stop_progress(&ctx->progress);
1322 strbuf_release(&progress_title);
1327 static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1329 if (ctx->report_progress)
1330 ctx->progress = start_delayed_progress(
1331 _("Finding commits for commit graph among packed objects"),
1332 ctx->approx_nr_objects);
1333 for_each_packed_object(add_packed_commits, ctx,
1334 FOR_EACH_OBJECT_PACK_ORDER);
1335 if (ctx->progress_done < ctx->approx_nr_objects)
1336 display_progress(ctx->progress, ctx->approx_nr_objects);
1337 stop_progress(&ctx->progress);
1340 static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1342 uint32_t i, count_distinct = 1;
1344 if (ctx->report_progress)
1345 ctx->progress = start_delayed_progress(
1346 _("Counting distinct commits in commit graph"),
1348 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1349 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1351 for (i = 1; i < ctx->oids.nr; i++) {
1352 display_progress(ctx->progress, i + 1);
1353 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1355 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1357 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1364 stop_progress(&ctx->progress);
1366 return count_distinct;
1369 static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1373 ctx->num_extra_edges = 0;
1374 if (ctx->report_progress)
1375 ctx->progress = start_delayed_progress(
1376 _("Finding extra edges in commit graph"),
1378 for (i = 0; i < ctx->oids.nr; i++) {
1379 unsigned int num_parents;
1381 display_progress(ctx->progress, i + 1);
1382 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1385 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1386 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1389 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1392 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1394 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
1395 if (num_parents > 2)
1396 ctx->num_extra_edges += num_parents - 1;
1400 stop_progress(&ctx->progress);
1403 static int write_graph_chunk_base_1(struct hashfile *f,
1404 struct commit_graph *g)
1411 num = write_graph_chunk_base_1(f, g->base_graph);
1412 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1416 static int write_graph_chunk_base(struct hashfile *f,
1417 struct write_commit_graph_context *ctx)
1419 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1421 if (num != ctx->num_commit_graphs_after - 1) {
1422 error(_("failed to write correct number of base graph ids"));
1429 static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1434 struct lock_file lk = LOCK_INIT;
1435 uint32_t chunk_ids[MAX_NUM_CHUNKS + 1];
1436 uint64_t chunk_offsets[MAX_NUM_CHUNKS + 1];
1437 const unsigned hashsz = the_hash_algo->rawsz;
1438 struct strbuf progress_title = STRBUF_INIT;
1440 struct object_id file_hash;
1443 struct strbuf tmp_file = STRBUF_INIT;
1445 strbuf_addf(&tmp_file,
1446 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1448 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1450 ctx->graph_name = get_commit_graph_filename(ctx->odb);
1453 if (safe_create_leading_directories(ctx->graph_name)) {
1454 UNLEAK(ctx->graph_name);
1455 error(_("unable to create leading directories of %s"),
1461 char *lock_name = get_chain_filename(ctx->odb);
1463 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1465 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1467 error(_("unable to create '%s'"), ctx->graph_name);
1471 f = hashfd(fd, ctx->graph_name);
1473 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1474 fd = lk.tempfile->fd;
1475 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1478 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1479 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1480 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1481 if (ctx->num_extra_edges) {
1482 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1485 if (ctx->num_commit_graphs_after > 1) {
1486 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1490 chunk_ids[num_chunks] = 0;
1492 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1493 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1494 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1495 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1498 if (ctx->num_extra_edges) {
1499 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1500 4 * ctx->num_extra_edges;
1503 if (ctx->num_commit_graphs_after > 1) {
1504 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1505 hashsz * (ctx->num_commit_graphs_after - 1);
1509 hashwrite_be32(f, GRAPH_SIGNATURE);
1511 hashwrite_u8(f, GRAPH_VERSION);
1512 hashwrite_u8(f, oid_version());
1513 hashwrite_u8(f, num_chunks);
1514 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1516 for (i = 0; i <= num_chunks; i++) {
1517 uint32_t chunk_write[3];
1519 chunk_write[0] = htonl(chunk_ids[i]);
1520 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1521 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1522 hashwrite(f, chunk_write, 12);
1525 if (ctx->report_progress) {
1526 strbuf_addf(&progress_title,
1527 Q_("Writing out commit graph in %d pass",
1528 "Writing out commit graph in %d passes",
1531 ctx->progress = start_delayed_progress(
1533 num_chunks * ctx->commits.nr);
1535 write_graph_chunk_fanout(f, ctx);
1536 write_graph_chunk_oids(f, hashsz, ctx);
1537 write_graph_chunk_data(f, hashsz, ctx);
1538 if (ctx->num_extra_edges)
1539 write_graph_chunk_extra_edges(f, ctx);
1540 if (ctx->num_commit_graphs_after > 1 &&
1541 write_graph_chunk_base(f, ctx)) {
1544 stop_progress(&ctx->progress);
1545 strbuf_release(&progress_title);
1547 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1548 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1549 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
1551 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1552 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1553 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1554 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1557 close_commit_graph(ctx->r->objects);
1558 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1561 FILE *chainf = fdopen_lock_file(&lk, "w");
1562 char *final_graph_name;
1568 error(_("unable to open commit-graph chain file"));
1572 if (ctx->base_graph_name) {
1573 const char *dest = ctx->commit_graph_filenames_after[
1574 ctx->num_commit_graphs_after - 2];
1576 if (strcmp(ctx->base_graph_name, dest)) {
1577 result = rename(ctx->base_graph_name, dest);
1580 error(_("failed to rename base commit-graph file"));
1585 char *graph_name = get_commit_graph_filename(ctx->odb);
1589 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1590 final_graph_name = get_split_graph_filename(ctx->odb,
1591 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1592 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1594 result = rename(ctx->graph_name, final_graph_name);
1596 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1597 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1600 error(_("failed to rename temporary commit-graph file"));
1605 commit_lock_file(&lk);
1610 static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1612 struct commit_graph *g;
1613 uint32_t num_commits;
1616 int max_commits = 0;
1619 if (ctx->split_opts) {
1620 max_commits = ctx->split_opts->max_commits;
1622 if (ctx->split_opts->size_multiple)
1623 size_mult = ctx->split_opts->size_multiple;
1626 g = ctx->r->objects->commit_graph;
1627 num_commits = ctx->commits.nr;
1628 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1630 while (g && (g->num_commits <= size_mult * num_commits ||
1631 (max_commits && num_commits > max_commits))) {
1632 if (g->odb != ctx->odb)
1635 num_commits += g->num_commits;
1638 ctx->num_commit_graphs_after--;
1641 ctx->new_base_graph = g;
1643 if (ctx->num_commit_graphs_after == 2) {
1644 char *old_graph_name = get_commit_graph_filename(g->odb);
1646 if (!strcmp(g->filename, old_graph_name) &&
1647 g->odb != ctx->odb) {
1648 ctx->num_commit_graphs_after = 1;
1649 ctx->new_base_graph = NULL;
1652 free(old_graph_name);
1655 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1656 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1658 for (i = 0; i < ctx->num_commit_graphs_after &&
1659 i < ctx->num_commit_graphs_before; i++)
1660 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1662 i = ctx->num_commit_graphs_before - 1;
1663 g = ctx->r->objects->commit_graph;
1666 if (i < ctx->num_commit_graphs_after)
1667 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1674 static void merge_commit_graph(struct write_commit_graph_context *ctx,
1675 struct commit_graph *g)
1678 uint32_t offset = g->num_commits_in_base;
1680 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1682 for (i = 0; i < g->num_commits; i++) {
1683 struct object_id oid;
1684 struct commit *result;
1686 display_progress(ctx->progress, i + 1);
1688 load_oid_from_graph(g, i + offset, &oid);
1690 /* only add commits if they still exist in the repo */
1691 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1694 ctx->commits.list[ctx->commits.nr] = result;
1700 static int commit_compare(const void *_a, const void *_b)
1702 const struct commit *a = *(const struct commit **)_a;
1703 const struct commit *b = *(const struct commit **)_b;
1704 return oidcmp(&a->object.oid, &b->object.oid);
1707 static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1711 if (ctx->report_progress)
1712 ctx->progress = start_delayed_progress(
1713 _("Scanning merged commits"),
1716 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1718 ctx->num_extra_edges = 0;
1719 for (i = 0; i < ctx->commits.nr; i++) {
1720 display_progress(ctx->progress, i);
1722 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1723 &ctx->commits.list[i]->object.oid)) {
1724 die(_("unexpected duplicate commit id %s"),
1725 oid_to_hex(&ctx->commits.list[i]->object.oid));
1727 unsigned int num_parents;
1729 num_parents = commit_list_count(ctx->commits.list[i]->parents);
1730 if (num_parents > 2)
1731 ctx->num_extra_edges += num_parents - 1;
1735 stop_progress(&ctx->progress);
1738 static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1740 struct commit_graph *g = ctx->r->objects->commit_graph;
1741 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1743 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1744 current_graph_number--;
1746 if (ctx->report_progress)
1747 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
1749 merge_commit_graph(ctx, g);
1750 stop_progress(&ctx->progress);
1756 ctx->new_base_graph = g;
1757 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1760 if (ctx->new_base_graph)
1761 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1763 sort_and_scan_merged_commits(ctx);
1766 static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1769 time_t now = time(NULL);
1771 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1773 struct utimbuf updated_time;
1775 stat(ctx->commit_graph_filenames_before[i], &st);
1777 updated_time.actime = st.st_atime;
1778 updated_time.modtime = now;
1779 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1783 static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1785 struct strbuf path = STRBUF_INIT;
1789 timestamp_t expire_time = time(NULL);
1791 if (ctx->split_opts && ctx->split_opts->expire_time)
1792 expire_time -= ctx->split_opts->expire_time;
1794 char *chain_file_name = get_chain_filename(ctx->odb);
1795 unlink(chain_file_name);
1796 free(chain_file_name);
1797 ctx->num_commit_graphs_after = 0;
1800 strbuf_addstr(&path, ctx->odb->path);
1801 strbuf_addstr(&path, "/info/commit-graphs");
1802 dir = opendir(path.buf);
1807 strbuf_addch(&path, '/');
1808 dirnamelen = path.len;
1809 while ((de = readdir(dir)) != NULL) {
1811 uint32_t i, found = 0;
1813 strbuf_setlen(&path, dirnamelen);
1814 strbuf_addstr(&path, de->d_name);
1816 stat(path.buf, &st);
1818 if (st.st_mtime > expire_time)
1820 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1823 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1824 if (!strcmp(ctx->commit_graph_filenames_after[i],
1836 strbuf_release(&path);
1839 int write_commit_graph(struct object_directory *odb,
1840 struct string_list *pack_indexes,
1841 struct string_list *commit_hex,
1842 enum commit_graph_write_flags flags,
1843 const struct split_commit_graph_opts *split_opts)
1845 struct write_commit_graph_context *ctx;
1846 uint32_t i, count_distinct = 0;
1849 if (!commit_graph_compatible(the_repository))
1852 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1853 ctx->r = the_repository;
1855 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1856 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1857 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
1858 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
1859 ctx->split_opts = split_opts;
1860 ctx->changed_paths = flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS ? 1 : 0;
1861 ctx->total_bloom_filter_data_size = 0;
1864 struct commit_graph *g;
1865 prepare_commit_graph(ctx->r);
1867 g = ctx->r->objects->commit_graph;
1870 ctx->num_commit_graphs_before++;
1874 if (ctx->num_commit_graphs_before) {
1875 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1876 i = ctx->num_commit_graphs_before;
1877 g = ctx->r->objects->commit_graph;
1880 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1886 ctx->approx_nr_objects = approximate_object_count();
1887 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1889 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1890 ctx->oids.alloc = split_opts->max_commits;
1893 prepare_commit_graph_one(ctx->r, ctx->odb);
1894 if (ctx->r->objects->commit_graph)
1895 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
1898 if (ctx->oids.alloc < 1024)
1899 ctx->oids.alloc = 1024;
1900 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1902 if (ctx->append && ctx->r->objects->commit_graph) {
1903 struct commit_graph *g = ctx->r->objects->commit_graph;
1904 for (i = 0; i < g->num_commits; i++) {
1905 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1906 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
1911 ctx->order_by_pack = 1;
1912 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1917 if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
1921 if (!pack_indexes && !commit_hex) {
1922 ctx->order_by_pack = 1;
1923 fill_oids_from_all_packs(ctx);
1926 close_reachable(ctx);
1928 count_distinct = count_distinct_commits(ctx);
1930 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1931 error(_("the commit graph format cannot write %d commits"), count_distinct);
1936 ctx->commits.alloc = count_distinct;
1937 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
1939 copy_oids_to_commits(ctx);
1941 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
1942 error(_("too many commits to write graph"));
1947 if (!ctx->commits.nr)
1951 split_graph_merge_strategy(ctx);
1953 merge_commit_graphs(ctx);
1955 ctx->num_commit_graphs_after = 1;
1957 compute_generation_numbers(ctx);
1959 if (ctx->changed_paths)
1960 compute_bloom_filters(ctx);
1962 res = write_commit_graph_file(ctx);
1965 mark_commit_graphs(ctx);
1967 expire_commit_graphs(ctx);
1970 free(ctx->graph_name);
1971 free(ctx->commits.list);
1972 free(ctx->oids.list);
1974 if (ctx->commit_graph_filenames_after) {
1975 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1976 free(ctx->commit_graph_filenames_after[i]);
1977 free(ctx->commit_graph_hash_after[i]);
1980 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1981 free(ctx->commit_graph_filenames_before[i]);
1983 free(ctx->commit_graph_filenames_after);
1984 free(ctx->commit_graph_filenames_before);
1985 free(ctx->commit_graph_hash_after);
1993 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1994 static int verify_commit_graph_error;
1996 static void graph_report(const char *fmt, ...)
2000 verify_commit_graph_error = 1;
2002 vfprintf(stderr, fmt, ap);
2003 fprintf(stderr, "\n");
2007 #define GENERATION_ZERO_EXISTS 1
2008 #define GENERATION_NUMBER_EXISTS 2
2010 int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
2012 uint32_t i, cur_fanout_pos = 0;
2013 struct object_id prev_oid, cur_oid, checksum;
2014 int generation_zero = 0;
2017 struct progress *progress = NULL;
2018 int local_error = 0;
2021 graph_report("no commit-graph file loaded");
2025 verify_commit_graph_error = verify_commit_graph_lite(g);
2026 if (verify_commit_graph_error)
2027 return verify_commit_graph_error;
2029 devnull = open("/dev/null", O_WRONLY);
2030 f = hashfd(devnull, NULL);
2031 hashwrite(f, g->data, g->data_len - g->hash_len);
2032 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
2033 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
2034 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2035 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2038 for (i = 0; i < g->num_commits; i++) {
2039 struct commit *graph_commit;
2041 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2043 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
2044 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2045 oid_to_hex(&prev_oid),
2046 oid_to_hex(&cur_oid));
2048 oidcpy(&prev_oid, &cur_oid);
2050 while (cur_oid.hash[0] > cur_fanout_pos) {
2051 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2053 if (i != fanout_value)
2054 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2055 cur_fanout_pos, fanout_value, i);
2059 graph_commit = lookup_commit(r, &cur_oid);
2060 if (!parse_commit_in_graph_one(r, g, graph_commit))
2061 graph_report(_("failed to parse commit %s from commit-graph"),
2062 oid_to_hex(&cur_oid));
2065 while (cur_fanout_pos < 256) {
2066 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2068 if (g->num_commits != fanout_value)
2069 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2070 cur_fanout_pos, fanout_value, i);
2075 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
2076 return verify_commit_graph_error;
2078 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2079 progress = start_progress(_("Verifying commits in commit graph"),
2082 for (i = 0; i < g->num_commits; i++) {
2083 struct commit *graph_commit, *odb_commit;
2084 struct commit_list *graph_parents, *odb_parents;
2085 uint32_t max_generation = 0;
2087 display_progress(progress, i + 1);
2088 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2090 graph_commit = lookup_commit(r, &cur_oid);
2091 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
2092 if (parse_commit_internal(odb_commit, 0, 0)) {
2093 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2094 oid_to_hex(&cur_oid));
2098 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2099 get_commit_tree_oid(odb_commit)))
2100 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2101 oid_to_hex(&cur_oid),
2102 oid_to_hex(get_commit_tree_oid(graph_commit)),
2103 oid_to_hex(get_commit_tree_oid(odb_commit)));
2105 graph_parents = graph_commit->parents;
2106 odb_parents = odb_commit->parents;
2108 while (graph_parents) {
2109 if (odb_parents == NULL) {
2110 graph_report(_("commit-graph parent list for commit %s is too long"),
2111 oid_to_hex(&cur_oid));
2115 /* parse parent in case it is in a base graph */
2116 parse_commit_in_graph_one(r, g, graph_parents->item);
2118 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2119 graph_report(_("commit-graph parent for %s is %s != %s"),
2120 oid_to_hex(&cur_oid),
2121 oid_to_hex(&graph_parents->item->object.oid),
2122 oid_to_hex(&odb_parents->item->object.oid));
2124 if (graph_parents->item->generation > max_generation)
2125 max_generation = graph_parents->item->generation;
2127 graph_parents = graph_parents->next;
2128 odb_parents = odb_parents->next;
2131 if (odb_parents != NULL)
2132 graph_report(_("commit-graph parent list for commit %s terminates early"),
2133 oid_to_hex(&cur_oid));
2135 if (!graph_commit->generation) {
2136 if (generation_zero == GENERATION_NUMBER_EXISTS)
2137 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2138 oid_to_hex(&cur_oid));
2139 generation_zero = GENERATION_ZERO_EXISTS;
2140 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2141 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2142 oid_to_hex(&cur_oid));
2144 if (generation_zero == GENERATION_ZERO_EXISTS)
2148 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2149 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2150 * extra logic in the following condition.
2152 if (max_generation == GENERATION_NUMBER_MAX)
2155 if (graph_commit->generation != max_generation + 1)
2156 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2157 oid_to_hex(&cur_oid),
2158 graph_commit->generation,
2159 max_generation + 1);
2161 if (graph_commit->date != odb_commit->date)
2162 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2163 oid_to_hex(&cur_oid),
2167 stop_progress(&progress);
2169 local_error = verify_commit_graph_error;
2171 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2172 local_error |= verify_commit_graph(r, g->base_graph, flags);
2177 void free_commit_graph(struct commit_graph *g)
2181 if (g->graph_fd >= 0) {
2182 munmap((void *)g->data, g->data_len);
2190 void disable_commit_graph(struct repository *r)
2192 r->commit_graph_disabled = 1;