12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
17 #include "replace-object.h"
20 #include "commit-slab.h"
22 #include "json-writer.h"
24 #include "chunk-format.h"
27 void git_test_write_commit_graph_or_die(void)
30 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0))
33 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
34 flags
= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
36 if (write_commit_graph_reachable(the_repository
->objects
->odb
,
38 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
41 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
42 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
43 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
44 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
45 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
46 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
47 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
48 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
49 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
50 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
52 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
54 #define GRAPH_VERSION_1 0x1
55 #define GRAPH_VERSION GRAPH_VERSION_1
57 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
58 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
59 #define GRAPH_PARENT_NONE 0x70000000
61 #define GRAPH_LAST_EDGE 0x80000000
63 #define GRAPH_HEADER_SIZE 8
64 #define GRAPH_FANOUT_SIZE (4 * 256)
65 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
66 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
68 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
70 /* Remember to update object flag allocation in object.h */
71 #define REACHABLE (1u<<15)
73 define_commit_slab(topo_level_slab
, uint32_t);
75 /* Keep track of the order in which commits are added to our list. */
76 define_commit_slab(commit_pos
, int);
77 static struct commit_pos commit_pos
= COMMIT_SLAB_INIT(1, commit_pos
);
79 static void set_commit_pos(struct repository
*r
, const struct object_id
*oid
)
81 static int32_t max_pos
;
82 struct commit
*commit
= lookup_commit(r
, oid
);
85 return; /* should never happen, but be lenient */
87 *commit_pos_at(&commit_pos
, commit
) = max_pos
++;
90 static int commit_pos_cmp(const void *va
, const void *vb
)
92 const struct commit
*a
= *(const struct commit
**)va
;
93 const struct commit
*b
= *(const struct commit
**)vb
;
94 return commit_pos_at(&commit_pos
, a
) -
95 commit_pos_at(&commit_pos
, b
);
98 define_commit_slab(commit_graph_data_slab
, struct commit_graph_data
);
99 static struct commit_graph_data_slab commit_graph_data_slab
=
100 COMMIT_SLAB_INIT(1, commit_graph_data_slab
);
102 static int get_configured_generation_version(struct repository
*r
)
105 repo_config_get_int(r
, "commitgraph.generationversion", &version
);
109 uint32_t commit_graph_position(const struct commit
*c
)
111 struct commit_graph_data
*data
=
112 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
114 return data
? data
->graph_pos
: COMMIT_NOT_FROM_GRAPH
;
117 timestamp_t
commit_graph_generation(const struct commit
*c
)
119 struct commit_graph_data
*data
=
120 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
122 if (data
&& data
->generation
)
123 return data
->generation
;
125 return GENERATION_NUMBER_INFINITY
;
128 static struct commit_graph_data
*commit_graph_data_at(const struct commit
*c
)
130 unsigned int i
, nth_slab
;
131 struct commit_graph_data
*data
=
132 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
137 nth_slab
= c
->index
/ commit_graph_data_slab
.slab_size
;
138 data
= commit_graph_data_slab_at(&commit_graph_data_slab
, c
);
141 * commit-slab initializes elements with zero, overwrite this with
142 * COMMIT_NOT_FROM_GRAPH for graph_pos.
144 * We avoid initializing generation with checking if graph position
145 * is not COMMIT_NOT_FROM_GRAPH.
147 for (i
= 0; i
< commit_graph_data_slab
.slab_size
; i
++) {
148 commit_graph_data_slab
.slab
[nth_slab
][i
].graph_pos
=
149 COMMIT_NOT_FROM_GRAPH
;
156 * Should be used only while writing commit-graph as it compares
157 * generation value of commits by directly accessing commit-slab.
159 static int commit_gen_cmp(const void *va
, const void *vb
)
161 const struct commit
*a
= *(const struct commit
**)va
;
162 const struct commit
*b
= *(const struct commit
**)vb
;
164 const timestamp_t generation_a
= commit_graph_data_at(a
)->generation
;
165 const timestamp_t generation_b
= commit_graph_data_at(b
)->generation
;
166 /* lower generation commits first */
167 if (generation_a
< generation_b
)
169 else if (generation_a
> generation_b
)
172 /* use date as a heuristic when generations are equal */
173 if (a
->date
< b
->date
)
175 else if (a
->date
> b
->date
)
180 char *get_commit_graph_filename(struct object_directory
*obj_dir
)
182 return xstrfmt("%s/info/commit-graph", obj_dir
->path
);
185 static char *get_split_graph_filename(struct object_directory
*odb
,
188 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb
->path
,
192 char *get_commit_graph_chain_filename(struct object_directory
*odb
)
194 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb
->path
);
197 static struct commit_graph
*alloc_commit_graph(void)
199 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
204 extern int read_replace_refs
;
206 static int commit_graph_compatible(struct repository
*r
)
211 if (read_replace_refs
) {
212 prepare_replace_object(r
);
213 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
217 prepare_commit_graft(r
);
218 if (r
->parsed_objects
&&
219 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
221 if (is_repository_shallow(r
))
227 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
229 *fd
= git_open(graph_file
);
232 if (fstat(*fd
, st
)) {
239 struct commit_graph
*load_commit_graph_one_fd_st(struct repository
*r
,
240 int fd
, struct stat
*st
,
241 struct object_directory
*odb
)
245 struct commit_graph
*ret
;
247 graph_size
= xsize_t(st
->st_size
);
249 if (graph_size
< GRAPH_MIN_SIZE
) {
251 error(_("commit-graph file is too small"));
254 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
256 prepare_repo_settings(r
);
257 ret
= parse_commit_graph(&r
->settings
, graph_map
, graph_size
);
262 munmap(graph_map
, graph_size
);
267 static int verify_commit_graph_lite(struct commit_graph
*g
)
270 * Basic validation shared between parse_commit_graph()
271 * which'll be called every time the graph is used, and the
272 * much more expensive verify_commit_graph() used by
273 * "commit-graph verify".
275 * There should only be very basic checks here to ensure that
276 * we don't e.g. segfault in fill_commit_in_graph(), but
277 * because this is a very hot codepath nothing that e.g. loops
278 * over g->num_commits, or runs a checksum on the commit-graph
281 if (!g
->chunk_oid_fanout
) {
282 error("commit-graph is missing the OID Fanout chunk");
285 if (!g
->chunk_oid_lookup
) {
286 error("commit-graph is missing the OID Lookup chunk");
289 if (!g
->chunk_commit_data
) {
290 error("commit-graph is missing the Commit Data chunk");
297 static int graph_read_oid_lookup(const unsigned char *chunk_start
,
298 size_t chunk_size
, void *data
)
300 struct commit_graph
*g
= data
;
301 g
->chunk_oid_lookup
= chunk_start
;
302 g
->num_commits
= chunk_size
/ g
->hash_len
;
306 static int graph_read_bloom_data(const unsigned char *chunk_start
,
307 size_t chunk_size
, void *data
)
309 struct commit_graph
*g
= data
;
310 uint32_t hash_version
;
311 g
->chunk_bloom_data
= chunk_start
;
312 hash_version
= get_be32(chunk_start
);
314 if (hash_version
!= 1)
317 g
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
318 g
->bloom_filter_settings
->hash_version
= hash_version
;
319 g
->bloom_filter_settings
->num_hashes
= get_be32(chunk_start
+ 4);
320 g
->bloom_filter_settings
->bits_per_entry
= get_be32(chunk_start
+ 8);
321 g
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
326 struct commit_graph
*parse_commit_graph(struct repo_settings
*s
,
327 void *graph_map
, size_t graph_size
)
329 const unsigned char *data
;
330 struct commit_graph
*graph
;
331 uint32_t graph_signature
;
332 unsigned char graph_version
, hash_version
;
333 struct chunkfile
*cf
= NULL
;
338 if (graph_size
< GRAPH_MIN_SIZE
)
341 data
= (const unsigned char *)graph_map
;
343 graph_signature
= get_be32(data
);
344 if (graph_signature
!= GRAPH_SIGNATURE
) {
345 error(_("commit-graph signature %X does not match signature %X"),
346 graph_signature
, GRAPH_SIGNATURE
);
350 graph_version
= *(unsigned char*)(data
+ 4);
351 if (graph_version
!= GRAPH_VERSION
) {
352 error(_("commit-graph version %X does not match version %X"),
353 graph_version
, GRAPH_VERSION
);
357 hash_version
= *(unsigned char*)(data
+ 5);
358 if (hash_version
!= oid_version(the_hash_algo
)) {
359 error(_("commit-graph hash version %X does not match version %X"),
360 hash_version
, oid_version(the_hash_algo
));
364 graph
= alloc_commit_graph();
366 graph
->hash_len
= the_hash_algo
->rawsz
;
367 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
368 graph
->data
= graph_map
;
369 graph
->data_len
= graph_size
;
371 if (graph_size
< GRAPH_HEADER_SIZE
+
372 (graph
->num_chunks
+ 1) * CHUNK_TOC_ENTRY_SIZE
+
373 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
374 error(_("commit-graph file is too small to hold %u chunks"),
380 cf
= init_chunkfile(NULL
);
382 if (read_table_of_contents(cf
, graph
->data
, graph_size
,
383 GRAPH_HEADER_SIZE
, graph
->num_chunks
))
384 goto free_and_return
;
386 pair_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
,
387 (const unsigned char **)&graph
->chunk_oid_fanout
);
388 read_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, graph_read_oid_lookup
, graph
);
389 pair_chunk(cf
, GRAPH_CHUNKID_DATA
, &graph
->chunk_commit_data
);
390 pair_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
, &graph
->chunk_extra_edges
);
391 pair_chunk(cf
, GRAPH_CHUNKID_BASE
, &graph
->chunk_base_graphs
);
393 if (s
->commit_graph_generation_version
>= 2) {
394 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
395 &graph
->chunk_generation_data
);
396 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
397 &graph
->chunk_generation_data_overflow
);
399 if (graph
->chunk_generation_data
)
400 graph
->read_generation_data
= 1;
403 if (s
->commit_graph_read_changed_paths
) {
404 pair_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
405 &graph
->chunk_bloom_indexes
);
406 read_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
407 graph_read_bloom_data
, graph
);
410 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
411 init_bloom_filters();
413 /* We need both the bloom chunks to exist together. Else ignore the data */
414 graph
->chunk_bloom_indexes
= NULL
;
415 graph
->chunk_bloom_data
= NULL
;
416 FREE_AND_NULL(graph
->bloom_filter_settings
);
419 oidread(&graph
->oid
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
421 if (verify_commit_graph_lite(graph
))
422 goto free_and_return
;
429 free(graph
->bloom_filter_settings
);
434 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
435 const char *graph_file
,
436 struct object_directory
*odb
)
441 struct commit_graph
*g
;
442 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
447 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
450 g
->filename
= xstrdup(graph_file
);
455 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
456 struct object_directory
*odb
)
458 char *graph_name
= get_commit_graph_filename(odb
);
459 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
465 static int add_graph_to_chain(struct commit_graph
*g
,
466 struct commit_graph
*chain
,
467 struct object_id
*oids
,
470 struct commit_graph
*cur_g
= chain
;
472 if (n
&& !g
->chunk_base_graphs
) {
473 warning(_("commit-graph has no base graphs chunk"));
481 !oideq(&oids
[n
], &cur_g
->oid
) ||
482 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
483 warning(_("commit-graph chain does not match"));
487 cur_g
= cur_g
->base_graph
;
490 g
->base_graph
= chain
;
493 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
498 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
499 struct object_directory
*odb
)
501 struct commit_graph
*graph_chain
= NULL
;
502 struct strbuf line
= STRBUF_INIT
;
504 struct object_id
*oids
;
505 int i
= 0, valid
= 1, count
;
506 char *chain_name
= get_commit_graph_chain_filename(odb
);
510 fp
= fopen(chain_name
, "r");
511 stat_res
= stat(chain_name
, &st
);
517 st
.st_size
<= the_hash_algo
->hexsz
) {
522 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
523 CALLOC_ARRAY(oids
, count
);
527 for (i
= 0; i
< count
; i
++) {
528 struct object_directory
*odb
;
530 if (strbuf_getline_lf(&line
, fp
) == EOF
)
533 if (get_oid_hex(line
.buf
, &oids
[i
])) {
534 warning(_("invalid commit-graph chain: line '%s' not a hash"),
541 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
542 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
543 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
548 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
558 warning(_("unable to find all commit-graph files"));
565 strbuf_release(&line
);
571 * returns 1 if and only if all graphs in the chain have
572 * corrected commit dates stored in the generation_data chunk.
574 static int validate_mixed_generation_chain(struct commit_graph
*g
)
576 int read_generation_data
= 1;
577 struct commit_graph
*p
= g
;
579 while (read_generation_data
&& p
) {
580 read_generation_data
= p
->read_generation_data
;
584 if (read_generation_data
)
588 g
->read_generation_data
= 0;
595 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
596 struct object_directory
*odb
)
598 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
601 g
= load_commit_graph_chain(r
, odb
);
603 validate_mixed_generation_chain(g
);
608 static void prepare_commit_graph_one(struct repository
*r
,
609 struct object_directory
*odb
)
612 if (r
->objects
->commit_graph
)
615 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
619 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
621 * On the first invocation, this function attempts to load the commit
622 * graph if the_repository is configured to have one.
624 static int prepare_commit_graph(struct repository
*r
)
626 struct object_directory
*odb
;
629 * Early return if there is no git dir or if the commit graph is
632 * This must come before the "already attempted?" check below, because
633 * we want to disable even an already-loaded graph file.
635 if (!r
->gitdir
|| r
->commit_graph_disabled
)
638 if (r
->objects
->commit_graph_attempted
)
639 return !!r
->objects
->commit_graph
;
640 r
->objects
->commit_graph_attempted
= 1;
642 prepare_repo_settings(r
);
644 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
645 r
->settings
.core_commit_graph
!= 1)
647 * This repository is not configured to use commit graphs, so
648 * do not load one. (But report commit_graph_attempted anyway
649 * so that commit graph loading is not attempted again for this
654 if (!commit_graph_compatible(r
))
658 for (odb
= r
->objects
->odb
;
659 !r
->objects
->commit_graph
&& odb
;
661 prepare_commit_graph_one(r
, odb
);
662 return !!r
->objects
->commit_graph
;
665 int generation_numbers_enabled(struct repository
*r
)
667 uint32_t first_generation
;
668 struct commit_graph
*g
;
669 if (!prepare_commit_graph(r
))
672 g
= r
->objects
->commit_graph
;
677 first_generation
= get_be32(g
->chunk_commit_data
+
678 g
->hash_len
+ 8) >> 2;
680 return !!first_generation
;
683 int corrected_commit_dates_enabled(struct repository
*r
)
685 struct commit_graph
*g
;
686 if (!prepare_commit_graph(r
))
689 g
= r
->objects
->commit_graph
;
694 return g
->read_generation_data
;
697 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
699 struct commit_graph
*g
= r
->objects
->commit_graph
;
701 if (g
->bloom_filter_settings
)
702 return g
->bloom_filter_settings
;
708 static void close_commit_graph_one(struct commit_graph
*g
)
713 clear_commit_graph_data_slab(&commit_graph_data_slab
);
714 close_commit_graph_one(g
->base_graph
);
715 free_commit_graph(g
);
718 void close_commit_graph(struct raw_object_store
*o
)
720 close_commit_graph_one(o
->commit_graph
);
721 o
->commit_graph
= NULL
;
724 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
726 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
727 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
730 static void load_oid_from_graph(struct commit_graph
*g
,
732 struct object_id
*oid
)
736 while (g
&& pos
< g
->num_commits_in_base
)
740 BUG("NULL commit-graph");
742 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
743 die(_("invalid commit position. commit-graph is likely corrupt"));
745 lex_index
= pos
- g
->num_commits_in_base
;
747 oidread(oid
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
750 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
751 struct commit_graph
*g
,
753 struct commit_list
**pptr
)
756 struct object_id oid
;
758 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
759 die("invalid parent position %"PRIu32
, pos
);
761 load_oid_from_graph(g
, pos
, &oid
);
762 c
= lookup_commit(r
, &oid
);
764 die(_("could not find commit %s"), oid_to_hex(&oid
));
765 commit_graph_data_at(c
)->graph_pos
= pos
;
766 return &commit_list_insert(c
, pptr
)->next
;
769 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
771 const unsigned char *commit_data
;
772 struct commit_graph_data
*graph_data
;
773 uint32_t lex_index
, offset_pos
;
774 uint64_t date_high
, date_low
, offset
;
776 while (pos
< g
->num_commits_in_base
)
779 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
780 die(_("invalid commit position. commit-graph is likely corrupt"));
782 lex_index
= pos
- g
->num_commits_in_base
;
783 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
785 graph_data
= commit_graph_data_at(item
);
786 graph_data
->graph_pos
= pos
;
788 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
789 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
790 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
792 if (g
->read_generation_data
) {
793 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ sizeof(uint32_t) * lex_index
);
795 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
796 if (!g
->chunk_generation_data_overflow
)
797 die(_("commit-graph requires overflow generation data but has none"));
799 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
800 graph_data
->generation
= item
->date
+ get_be64(g
->chunk_generation_data_overflow
+ 8 * offset_pos
);
802 graph_data
->generation
= item
->date
+ offset
;
804 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
807 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
810 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
815 static int fill_commit_in_graph(struct repository
*r
,
817 struct commit_graph
*g
, uint32_t pos
)
820 uint32_t *parent_data_ptr
;
821 struct commit_list
**pptr
;
822 const unsigned char *commit_data
;
825 while (pos
< g
->num_commits_in_base
)
828 fill_commit_graph_info(item
, g
, pos
);
830 lex_index
= pos
- g
->num_commits_in_base
;
831 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
833 item
->object
.parsed
= 1;
835 set_commit_tree(item
, NULL
);
837 pptr
= &item
->parents
;
839 edge_value
= get_be32(commit_data
+ g
->hash_len
);
840 if (edge_value
== GRAPH_PARENT_NONE
)
842 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
844 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
845 if (edge_value
== GRAPH_PARENT_NONE
)
847 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
848 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
852 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
853 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
855 edge_value
= get_be32(parent_data_ptr
);
856 pptr
= insert_parent_or_die(r
, g
,
857 edge_value
& GRAPH_EDGE_LAST_MASK
,
860 } while (!(edge_value
& GRAPH_LAST_EDGE
));
865 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
867 struct commit_graph
*cur_g
= g
;
870 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
871 cur_g
= cur_g
->base_graph
;
874 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
881 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
883 uint32_t graph_pos
= commit_graph_position(item
);
884 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
888 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
892 int repo_find_commit_pos_in_graph(struct repository
*r
, struct commit
*c
,
895 if (!prepare_commit_graph(r
))
897 return find_commit_pos_in_graph(c
, r
->objects
->commit_graph
, pos
);
900 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
902 struct commit
*commit
;
905 if (!prepare_commit_graph(repo
))
907 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
909 if (!has_object(repo
, id
, 0))
912 commit
= lookup_commit(repo
, id
);
915 if (commit
->object
.parsed
)
918 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
924 static int parse_commit_in_graph_one(struct repository
*r
,
925 struct commit_graph
*g
,
930 if (item
->object
.parsed
)
933 if (find_commit_pos_in_graph(item
, g
, &pos
))
934 return fill_commit_in_graph(r
, item
, g
, pos
);
939 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
941 static int checked_env
= 0;
944 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
945 die("dying as requested by the '%s' variable on commit-graph parse!",
946 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
949 if (!prepare_commit_graph(r
))
951 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
954 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
957 if (repo_find_commit_pos_in_graph(r
, item
, &pos
))
958 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
961 static struct tree
*load_tree_for_commit(struct repository
*r
,
962 struct commit_graph
*g
,
965 struct object_id oid
;
966 const unsigned char *commit_data
;
967 uint32_t graph_pos
= commit_graph_position(c
);
969 while (graph_pos
< g
->num_commits_in_base
)
972 commit_data
= g
->chunk_commit_data
+
973 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
975 oidread(&oid
, commit_data
);
976 set_commit_tree(c
, lookup_tree(r
, &oid
));
978 return c
->maybe_tree
;
981 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
982 struct commit_graph
*g
,
983 const struct commit
*c
)
986 return c
->maybe_tree
;
987 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
988 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
990 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
993 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
995 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
998 struct packed_commit_list
{
999 struct commit
**list
;
1004 struct write_commit_graph_context
{
1005 struct repository
*r
;
1006 struct object_directory
*odb
;
1008 struct oid_array oids
;
1009 struct packed_commit_list commits
;
1010 int num_extra_edges
;
1011 int num_generation_data_overflows
;
1012 unsigned long approx_nr_objects
;
1013 struct progress
*progress
;
1015 uint64_t progress_cnt
;
1017 char *base_graph_name
;
1018 int num_commit_graphs_before
;
1019 int num_commit_graphs_after
;
1020 char **commit_graph_filenames_before
;
1021 char **commit_graph_filenames_after
;
1022 char **commit_graph_hash_after
;
1023 uint32_t new_num_commits_in_base
;
1024 struct commit_graph
*new_base_graph
;
1031 write_generation_data
:1,
1032 trust_generation_numbers
:1;
1034 struct topo_level_slab
*topo_levels
;
1035 const struct commit_graph_opts
*opts
;
1036 size_t total_bloom_filter_data_size
;
1037 const struct bloom_filter_settings
*bloom_settings
;
1039 int count_bloom_filter_computed
;
1040 int count_bloom_filter_not_computed
;
1041 int count_bloom_filter_trunc_empty
;
1042 int count_bloom_filter_trunc_large
;
1045 static int write_graph_chunk_fanout(struct hashfile
*f
,
1048 struct write_commit_graph_context
*ctx
= data
;
1050 struct commit
**list
= ctx
->commits
.list
;
1053 * Write the first-level table (the list is sorted,
1054 * but we use a 256-entry lookup to be able to avoid
1055 * having to do eight extra binary search iterations).
1057 for (i
= 0; i
< 256; i
++) {
1058 while (count
< ctx
->commits
.nr
) {
1059 if ((*list
)->object
.oid
.hash
[0] != i
)
1061 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1066 hashwrite_be32(f
, count
);
1072 static int write_graph_chunk_oids(struct hashfile
*f
,
1075 struct write_commit_graph_context
*ctx
= data
;
1076 struct commit
**list
= ctx
->commits
.list
;
1078 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1079 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1080 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1086 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1088 const struct commit
* const *commits
= table
;
1089 return &commits
[index
]->object
.oid
;
1092 static int write_graph_chunk_data(struct hashfile
*f
,
1095 struct write_commit_graph_context
*ctx
= data
;
1096 struct commit
**list
= ctx
->commits
.list
;
1097 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1098 uint32_t num_extra_edges
= 0;
1100 while (list
< last
) {
1101 struct commit_list
*parent
;
1102 struct object_id
*tree
;
1104 uint32_t packedDate
[2];
1105 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1107 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1108 die(_("unable to parse commit %s"),
1109 oid_to_hex(&(*list
)->object
.oid
));
1110 tree
= get_commit_tree_oid(*list
);
1111 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1113 parent
= (*list
)->parents
;
1116 edge_value
= GRAPH_PARENT_NONE
;
1118 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1123 if (edge_value
>= 0)
1124 edge_value
+= ctx
->new_num_commits_in_base
;
1125 else if (ctx
->new_base_graph
) {
1127 if (find_commit_pos_in_graph(parent
->item
,
1128 ctx
->new_base_graph
,
1134 BUG("missing parent %s for commit %s",
1135 oid_to_hex(&parent
->item
->object
.oid
),
1136 oid_to_hex(&(*list
)->object
.oid
));
1139 hashwrite_be32(f
, edge_value
);
1142 parent
= parent
->next
;
1145 edge_value
= GRAPH_PARENT_NONE
;
1146 else if (parent
->next
)
1147 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1149 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1154 if (edge_value
>= 0)
1155 edge_value
+= ctx
->new_num_commits_in_base
;
1156 else if (ctx
->new_base_graph
) {
1158 if (find_commit_pos_in_graph(parent
->item
,
1159 ctx
->new_base_graph
,
1165 BUG("missing parent %s for commit %s",
1166 oid_to_hex(&parent
->item
->object
.oid
),
1167 oid_to_hex(&(*list
)->object
.oid
));
1170 hashwrite_be32(f
, edge_value
);
1172 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1175 parent
= parent
->next
;
1179 if (sizeof((*list
)->date
) > 4)
1180 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1184 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1186 packedDate
[1] = htonl((*list
)->date
);
1187 hashwrite(f
, packedDate
, 8);
1195 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1198 struct write_commit_graph_context
*ctx
= data
;
1199 int i
, num_generation_data_overflows
= 0;
1201 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1202 struct commit
*c
= ctx
->commits
.list
[i
];
1204 repo_parse_commit(ctx
->r
, c
);
1205 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1206 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1208 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1209 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1210 num_generation_data_overflows
++;
1213 hashwrite_be32(f
, offset
);
1219 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1222 struct write_commit_graph_context
*ctx
= data
;
1224 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1225 struct commit
*c
= ctx
->commits
.list
[i
];
1226 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1227 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1229 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1230 hashwrite_be32(f
, offset
>> 32);
1231 hashwrite_be32(f
, (uint32_t) offset
);
1238 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1241 struct write_commit_graph_context
*ctx
= data
;
1242 struct commit
**list
= ctx
->commits
.list
;
1243 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1244 struct commit_list
*parent
;
1246 while (list
< last
) {
1247 int num_parents
= 0;
1249 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1251 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1252 parent
= parent
->next
)
1255 if (num_parents
<= 2) {
1260 /* Since num_parents > 2, this initializer is safe. */
1261 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1262 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1267 if (edge_value
>= 0)
1268 edge_value
+= ctx
->new_num_commits_in_base
;
1269 else if (ctx
->new_base_graph
) {
1271 if (find_commit_pos_in_graph(parent
->item
,
1272 ctx
->new_base_graph
,
1278 BUG("missing parent %s for commit %s",
1279 oid_to_hex(&parent
->item
->object
.oid
),
1280 oid_to_hex(&(*list
)->object
.oid
));
1281 else if (!parent
->next
)
1282 edge_value
|= GRAPH_LAST_EDGE
;
1284 hashwrite_be32(f
, edge_value
);
1293 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1296 struct write_commit_graph_context
*ctx
= data
;
1297 struct commit
**list
= ctx
->commits
.list
;
1298 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1299 uint32_t cur_pos
= 0;
1301 while (list
< last
) {
1302 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1303 size_t len
= filter
? filter
->len
: 0;
1305 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1306 hashwrite_be32(f
, cur_pos
);
1313 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1315 struct json_writer jw
= JSON_WRITER_INIT
;
1317 jw_object_begin(&jw
, 0);
1318 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1319 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1320 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1321 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1324 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1329 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1332 struct write_commit_graph_context
*ctx
= data
;
1333 struct commit
**list
= ctx
->commits
.list
;
1334 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1336 trace2_bloom_filter_settings(ctx
);
1338 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1339 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1340 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1342 while (list
< last
) {
1343 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1344 size_t len
= filter
? filter
->len
: 0;
1346 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1348 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1355 static int add_packed_commits(const struct object_id
*oid
,
1356 struct packed_git
*pack
,
1360 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1361 enum object_type type
;
1362 off_t offset
= nth_packed_object_offset(pack
, pos
);
1363 struct object_info oi
= OBJECT_INFO_INIT
;
1366 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1369 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1370 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1372 if (type
!= OBJ_COMMIT
)
1375 oid_array_append(&ctx
->oids
, oid
);
1376 set_commit_pos(ctx
->r
, oid
);
1381 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1383 struct commit_list
*parent
;
1384 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1385 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1386 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1387 parent
->item
->object
.flags
|= REACHABLE
;
1392 static void close_reachable(struct write_commit_graph_context
*ctx
)
1395 struct commit
*commit
;
1396 enum commit_graph_split_flags flags
= ctx
->opts
?
1397 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1399 if (ctx
->report_progress
)
1400 ctx
->progress
= start_delayed_progress(
1401 _("Loading known commits in commit graph"),
1403 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1404 display_progress(ctx
->progress
, i
+ 1);
1405 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1407 commit
->object
.flags
|= REACHABLE
;
1409 stop_progress(&ctx
->progress
);
1412 * As this loop runs, ctx->oids.nr may grow, but not more
1413 * than the number of missing commits in the reachable
1416 if (ctx
->report_progress
)
1417 ctx
->progress
= start_delayed_progress(
1418 _("Expanding reachable commits in commit graph"),
1420 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1421 display_progress(ctx
->progress
, i
+ 1);
1422 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1427 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1428 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1429 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1430 add_missing_parents(ctx
, commit
);
1431 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1432 add_missing_parents(ctx
, commit
);
1434 stop_progress(&ctx
->progress
);
1436 if (ctx
->report_progress
)
1437 ctx
->progress
= start_delayed_progress(
1438 _("Clearing commit marks in commit graph"),
1440 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1441 display_progress(ctx
->progress
, i
+ 1);
1442 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1445 commit
->object
.flags
&= ~REACHABLE
;
1447 stop_progress(&ctx
->progress
);
1450 struct compute_generation_info
{
1451 struct repository
*r
;
1452 struct packed_commit_list
*commits
;
1453 struct progress
*progress
;
1456 timestamp_t (*get_generation
)(struct commit
*c
, void *data
);
1457 void (*set_generation
)(struct commit
*c
, timestamp_t gen
, void *data
);
1461 static timestamp_t
compute_generation_from_max(struct commit
*c
,
1462 timestamp_t max_gen
,
1463 int generation_version
)
1465 switch (generation_version
) {
1466 case 1: /* topological levels */
1467 if (max_gen
> GENERATION_NUMBER_V1_MAX
- 1)
1468 max_gen
= GENERATION_NUMBER_V1_MAX
- 1;
1471 case 2: /* corrected commit date */
1472 if (c
->date
&& c
->date
> max_gen
)
1473 max_gen
= c
->date
- 1;
1477 BUG("attempting unimplemented version");
1481 static void compute_reachable_generation_numbers(
1482 struct compute_generation_info
*info
,
1483 int generation_version
)
1486 struct commit_list
*list
= NULL
;
1488 for (i
= 0; i
< info
->commits
->nr
; i
++) {
1489 struct commit
*c
= info
->commits
->list
[i
];
1491 repo_parse_commit(info
->r
, c
);
1492 gen
= info
->get_generation(c
, info
->data
);
1493 display_progress(info
->progress
, info
->progress_cnt
+ 1);
1495 if (gen
!= GENERATION_NUMBER_ZERO
&& gen
!= GENERATION_NUMBER_INFINITY
)
1498 commit_list_insert(c
, &list
);
1500 struct commit
*current
= list
->item
;
1501 struct commit_list
*parent
;
1502 int all_parents_computed
= 1;
1503 uint32_t max_gen
= 0;
1505 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1506 repo_parse_commit(info
->r
, parent
->item
);
1507 gen
= info
->get_generation(parent
->item
, info
->data
);
1509 if (gen
== GENERATION_NUMBER_ZERO
) {
1510 all_parents_computed
= 0;
1511 commit_list_insert(parent
->item
, &list
);
1519 if (all_parents_computed
) {
1521 gen
= compute_generation_from_max(
1523 generation_version
);
1524 info
->set_generation(current
, gen
, info
->data
);
1530 static timestamp_t
get_topo_level(struct commit
*c
, void *data
)
1532 struct write_commit_graph_context
*ctx
= data
;
1533 return *topo_level_slab_at(ctx
->topo_levels
, c
);
1536 static void set_topo_level(struct commit
*c
, timestamp_t t
, void *data
)
1538 struct write_commit_graph_context
*ctx
= data
;
1539 *topo_level_slab_at(ctx
->topo_levels
, c
) = (uint32_t)t
;
1542 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1544 struct compute_generation_info info
= {
1546 .commits
= &ctx
->commits
,
1547 .get_generation
= get_topo_level
,
1548 .set_generation
= set_topo_level
,
1552 if (ctx
->report_progress
)
1553 info
.progress
= ctx
->progress
1554 = start_delayed_progress(
1555 _("Computing commit graph topological levels"),
1558 compute_reachable_generation_numbers(&info
, 1);
1560 stop_progress(&ctx
->progress
);
1563 static timestamp_t
get_generation_from_graph_data(struct commit
*c
, void *data
)
1565 return commit_graph_data_at(c
)->generation
;
1568 static void set_generation_v2(struct commit
*c
, timestamp_t t
, void *data
)
1570 struct commit_graph_data
*g
= commit_graph_data_at(c
);
1574 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1577 struct compute_generation_info info
= {
1579 .commits
= &ctx
->commits
,
1580 .get_generation
= get_generation_from_graph_data
,
1581 .set_generation
= set_generation_v2
,
1585 if (ctx
->report_progress
)
1586 info
.progress
= ctx
->progress
1587 = start_delayed_progress(
1588 _("Computing commit graph generation numbers"),
1591 if (!ctx
->trust_generation_numbers
) {
1592 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1593 struct commit
*c
= ctx
->commits
.list
[i
];
1594 repo_parse_commit(ctx
->r
, c
);
1595 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1599 compute_reachable_generation_numbers(&info
, 2);
1601 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1602 struct commit
*c
= ctx
->commits
.list
[i
];
1603 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1604 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1605 ctx
->num_generation_data_overflows
++;
1607 stop_progress(&ctx
->progress
);
1610 static void set_generation_in_graph_data(struct commit
*c
, timestamp_t t
,
1613 commit_graph_data_at(c
)->generation
= t
;
1617 * After this method, all commits reachable from those in the given
1618 * list will have non-zero, non-infinite generation numbers.
1620 void ensure_generations_valid(struct repository
*r
,
1621 struct commit
**commits
, size_t nr
)
1623 int generation_version
= get_configured_generation_version(r
);
1624 struct packed_commit_list list
= {
1629 struct compute_generation_info info
= {
1632 .get_generation
= get_generation_from_graph_data
,
1633 .set_generation
= set_generation_in_graph_data
,
1636 compute_reachable_generation_numbers(&info
, generation_version
);
1639 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1641 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1642 ctx
->count_bloom_filter_computed
);
1643 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1644 ctx
->count_bloom_filter_not_computed
);
1645 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1646 ctx
->count_bloom_filter_trunc_empty
);
1647 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1648 ctx
->count_bloom_filter_trunc_large
);
1651 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1654 struct progress
*progress
= NULL
;
1655 struct commit
**sorted_commits
;
1656 int max_new_filters
;
1658 init_bloom_filters();
1660 if (ctx
->report_progress
)
1661 progress
= start_delayed_progress(
1662 _("Computing commit changed paths Bloom filters"),
1665 DUP_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1667 if (ctx
->order_by_pack
)
1668 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1670 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1672 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1673 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1675 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1676 enum bloom_filter_computed computed
= 0;
1677 struct commit
*c
= sorted_commits
[i
];
1678 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1681 ctx
->count_bloom_filter_computed
< max_new_filters
,
1682 ctx
->bloom_settings
,
1684 if (computed
& BLOOM_COMPUTED
) {
1685 ctx
->count_bloom_filter_computed
++;
1686 if (computed
& BLOOM_TRUNC_EMPTY
)
1687 ctx
->count_bloom_filter_trunc_empty
++;
1688 if (computed
& BLOOM_TRUNC_LARGE
)
1689 ctx
->count_bloom_filter_trunc_large
++;
1690 } else if (computed
& BLOOM_NOT_COMPUTED
)
1691 ctx
->count_bloom_filter_not_computed
++;
1692 ctx
->total_bloom_filter_data_size
+= filter
1693 ? sizeof(unsigned char) * filter
->len
: 0;
1694 display_progress(progress
, i
+ 1);
1697 if (trace2_is_enabled())
1698 trace2_bloom_filter_write_statistics(ctx
);
1700 free(sorted_commits
);
1701 stop_progress(&progress
);
1704 struct refs_cb_data
{
1705 struct oidset
*commits
;
1706 struct progress
*progress
;
1709 static int add_ref_to_set(const char *refname UNUSED
,
1710 const struct object_id
*oid
,
1711 int flags UNUSED
, void *cb_data
)
1713 struct object_id peeled
;
1714 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1716 if (!peel_iterated_oid(oid
, &peeled
))
1718 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1719 oidset_insert(data
->commits
, oid
);
1721 display_progress(data
->progress
, oidset_size(data
->commits
));
1726 int write_commit_graph_reachable(struct object_directory
*odb
,
1727 enum commit_graph_write_flags flags
,
1728 const struct commit_graph_opts
*opts
)
1730 struct oidset commits
= OIDSET_INIT
;
1731 struct refs_cb_data data
;
1734 memset(&data
, 0, sizeof(data
));
1735 data
.commits
= &commits
;
1736 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1737 data
.progress
= start_delayed_progress(
1738 _("Collecting referenced commits"), 0);
1740 for_each_ref(add_ref_to_set
, &data
);
1742 stop_progress(&data
.progress
);
1744 result
= write_commit_graph(odb
, NULL
, &commits
,
1747 oidset_clear(&commits
);
1751 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1752 const struct string_list
*pack_indexes
)
1755 struct strbuf progress_title
= STRBUF_INIT
;
1756 struct strbuf packname
= STRBUF_INIT
;
1760 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1761 dirlen
= packname
.len
;
1762 if (ctx
->report_progress
) {
1763 strbuf_addf(&progress_title
,
1764 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1765 "Finding commits for commit graph in %"PRIuMAX
" packs",
1767 (uintmax_t)pack_indexes
->nr
);
1768 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1769 ctx
->progress_done
= 0;
1771 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1772 struct packed_git
*p
;
1773 strbuf_setlen(&packname
, dirlen
);
1774 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1775 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1777 ret
= error(_("error adding pack %s"), packname
.buf
);
1780 if (open_pack_index(p
)) {
1781 ret
= error(_("error opening index for %s"), packname
.buf
);
1784 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1785 FOR_EACH_OBJECT_PACK_ORDER
);
1791 stop_progress(&ctx
->progress
);
1792 strbuf_release(&progress_title
);
1793 strbuf_release(&packname
);
1798 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1799 struct oidset
*commits
)
1801 struct oidset_iter iter
;
1802 struct object_id
*oid
;
1804 if (!oidset_size(commits
))
1807 oidset_iter_init(commits
, &iter
);
1808 while ((oid
= oidset_iter_next(&iter
))) {
1809 oid_array_append(&ctx
->oids
, oid
);
1815 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1817 if (ctx
->report_progress
)
1818 ctx
->progress
= start_delayed_progress(
1819 _("Finding commits for commit graph among packed objects"),
1820 ctx
->approx_nr_objects
);
1821 for_each_packed_object(add_packed_commits
, ctx
,
1822 FOR_EACH_OBJECT_PACK_ORDER
);
1823 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1824 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1825 stop_progress(&ctx
->progress
);
1828 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1831 enum commit_graph_split_flags flags
= ctx
->opts
?
1832 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1834 ctx
->num_extra_edges
= 0;
1835 if (ctx
->report_progress
)
1836 ctx
->progress
= start_delayed_progress(
1837 _("Finding extra edges in commit graph"),
1839 oid_array_sort(&ctx
->oids
);
1840 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1841 unsigned int num_parents
;
1843 display_progress(ctx
->progress
, i
+ 1);
1845 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1846 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1848 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1849 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1852 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1853 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1855 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1857 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1858 if (num_parents
> 2)
1859 ctx
->num_extra_edges
+= num_parents
- 1;
1863 stop_progress(&ctx
->progress
);
1866 static int write_graph_chunk_base_1(struct hashfile
*f
,
1867 struct commit_graph
*g
)
1874 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1875 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1879 static int write_graph_chunk_base(struct hashfile
*f
,
1882 struct write_commit_graph_context
*ctx
= data
;
1883 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1885 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1886 error(_("failed to write correct number of base graph ids"));
1893 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1898 struct lock_file lk
= LOCK_INIT
;
1899 const unsigned hashsz
= the_hash_algo
->rawsz
;
1900 struct strbuf progress_title
= STRBUF_INIT
;
1901 struct chunkfile
*cf
;
1902 unsigned char file_hash
[GIT_MAX_RAWSZ
];
1905 struct strbuf tmp_file
= STRBUF_INIT
;
1907 strbuf_addf(&tmp_file
,
1908 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1910 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1912 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1915 if (safe_create_leading_directories(ctx
->graph_name
)) {
1916 UNLEAK(ctx
->graph_name
);
1917 error(_("unable to create leading directories of %s"),
1923 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1925 hold_lock_file_for_update_mode(&lk
, lock_name
,
1926 LOCK_DIE_ON_ERROR
, 0444);
1929 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1931 error(_("unable to create temporary graph layer"));
1935 if (adjust_shared_perm(ctx
->graph_name
)) {
1936 error(_("unable to adjust shared permissions for '%s'"),
1941 f
= hashfd(fd
, ctx
->graph_name
);
1943 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1944 LOCK_DIE_ON_ERROR
, 0444);
1945 fd
= get_lock_file_fd(&lk
);
1946 f
= hashfd(fd
, get_lock_file_path(&lk
));
1949 cf
= init_chunkfile(f
);
1951 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
1952 write_graph_chunk_fanout
);
1953 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, hashsz
* ctx
->commits
.nr
,
1954 write_graph_chunk_oids
);
1955 add_chunk(cf
, GRAPH_CHUNKID_DATA
, (hashsz
+ 16) * ctx
->commits
.nr
,
1956 write_graph_chunk_data
);
1958 if (ctx
->write_generation_data
)
1959 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
1960 sizeof(uint32_t) * ctx
->commits
.nr
,
1961 write_graph_chunk_generation_data
);
1962 if (ctx
->num_generation_data_overflows
)
1963 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
1964 sizeof(timestamp_t
) * ctx
->num_generation_data_overflows
,
1965 write_graph_chunk_generation_data_overflow
);
1966 if (ctx
->num_extra_edges
)
1967 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
1968 4 * ctx
->num_extra_edges
,
1969 write_graph_chunk_extra_edges
);
1970 if (ctx
->changed_paths
) {
1971 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
1972 sizeof(uint32_t) * ctx
->commits
.nr
,
1973 write_graph_chunk_bloom_indexes
);
1974 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
1975 sizeof(uint32_t) * 3
1976 + ctx
->total_bloom_filter_data_size
,
1977 write_graph_chunk_bloom_data
);
1979 if (ctx
->num_commit_graphs_after
> 1)
1980 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
1981 hashsz
* (ctx
->num_commit_graphs_after
- 1),
1982 write_graph_chunk_base
);
1984 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1986 hashwrite_u8(f
, GRAPH_VERSION
);
1987 hashwrite_u8(f
, oid_version(the_hash_algo
));
1988 hashwrite_u8(f
, get_num_chunks(cf
));
1989 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1991 if (ctx
->report_progress
) {
1992 strbuf_addf(&progress_title
,
1993 Q_("Writing out commit graph in %d pass",
1994 "Writing out commit graph in %d passes",
1995 get_num_chunks(cf
)),
1996 get_num_chunks(cf
));
1997 ctx
->progress
= start_delayed_progress(
1999 get_num_chunks(cf
) * ctx
->commits
.nr
);
2002 write_chunkfile(cf
, ctx
);
2004 stop_progress(&ctx
->progress
);
2005 strbuf_release(&progress_title
);
2007 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
2008 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
2009 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
2011 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
2012 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
2013 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
2014 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
2017 close_commit_graph(ctx
->r
->objects
);
2018 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
2019 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
2023 FILE *chainf
= fdopen_lock_file(&lk
, "w");
2024 char *final_graph_name
;
2030 error(_("unable to open commit-graph chain file"));
2034 if (ctx
->base_graph_name
) {
2036 int idx
= ctx
->num_commit_graphs_after
- 1;
2037 if (ctx
->num_commit_graphs_after
> 1)
2040 dest
= ctx
->commit_graph_filenames_after
[idx
];
2042 if (strcmp(ctx
->base_graph_name
, dest
)) {
2043 result
= rename(ctx
->base_graph_name
, dest
);
2046 error(_("failed to rename base commit-graph file"));
2051 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
2056 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
2057 final_graph_name
= get_split_graph_filename(ctx
->odb
,
2058 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
2059 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
2061 result
= rename(ctx
->graph_name
, final_graph_name
);
2063 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
2064 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
2067 error(_("failed to rename temporary commit-graph file"));
2072 commit_lock_file(&lk
);
2077 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2079 struct commit_graph
*g
;
2080 uint32_t num_commits
;
2081 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2084 int max_commits
= 0;
2088 max_commits
= ctx
->opts
->max_commits
;
2090 if (ctx
->opts
->size_multiple
)
2091 size_mult
= ctx
->opts
->size_multiple
;
2093 flags
= ctx
->opts
->split_flags
;
2096 g
= ctx
->r
->objects
->commit_graph
;
2097 num_commits
= ctx
->commits
.nr
;
2098 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2099 ctx
->num_commit_graphs_after
= 1;
2101 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2103 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2104 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2105 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
2106 (max_commits
&& num_commits
> max_commits
))) {
2107 if (g
->odb
!= ctx
->odb
)
2110 num_commits
+= g
->num_commits
;
2113 ctx
->num_commit_graphs_after
--;
2117 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2118 ctx
->new_base_graph
= g
;
2119 else if (ctx
->num_commit_graphs_after
!= 1)
2120 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2121 "should be 1 with --split=replace");
2123 if (ctx
->num_commit_graphs_after
== 2) {
2124 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2126 if (!strcmp(g
->filename
, old_graph_name
) &&
2127 g
->odb
!= ctx
->odb
) {
2128 ctx
->num_commit_graphs_after
= 1;
2129 ctx
->new_base_graph
= NULL
;
2132 free(old_graph_name
);
2135 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2136 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2138 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2139 i
< ctx
->num_commit_graphs_before
; i
++)
2140 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2142 i
= ctx
->num_commit_graphs_before
- 1;
2143 g
= ctx
->r
->objects
->commit_graph
;
2146 if (i
< ctx
->num_commit_graphs_after
)
2147 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2150 * If the topmost remaining layer has generation data chunk, the
2151 * resultant layer also has generation data chunk.
2153 if (i
== ctx
->num_commit_graphs_after
- 2)
2154 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2161 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2162 struct commit_graph
*g
)
2165 uint32_t offset
= g
->num_commits_in_base
;
2167 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2169 for (i
= 0; i
< g
->num_commits
; i
++) {
2170 struct object_id oid
;
2171 struct commit
*result
;
2173 display_progress(ctx
->progress
, i
+ 1);
2175 load_oid_from_graph(g
, i
+ offset
, &oid
);
2177 /* only add commits if they still exist in the repo */
2178 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2181 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2187 static int commit_compare(const void *_a
, const void *_b
)
2189 const struct commit
*a
= *(const struct commit
**)_a
;
2190 const struct commit
*b
= *(const struct commit
**)_b
;
2191 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2194 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2196 uint32_t i
, dedup_i
= 0;
2198 if (ctx
->report_progress
)
2199 ctx
->progress
= start_delayed_progress(
2200 _("Scanning merged commits"),
2203 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2205 ctx
->num_extra_edges
= 0;
2206 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2207 display_progress(ctx
->progress
, i
+ 1);
2209 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2210 &ctx
->commits
.list
[i
]->object
.oid
)) {
2212 * Silently ignore duplicates. These were likely
2213 * created due to a commit appearing in multiple
2214 * layers of the chain, which is unexpected but
2215 * not invalid. We should make sure there is a
2216 * unique copy in the new layer.
2219 unsigned int num_parents
;
2221 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2224 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2225 if (num_parents
> 2)
2226 ctx
->num_extra_edges
+= num_parents
- 1;
2230 ctx
->commits
.nr
= dedup_i
;
2232 stop_progress(&ctx
->progress
);
2235 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2237 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2238 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2240 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2241 current_graph_number
--;
2243 if (ctx
->report_progress
)
2244 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2246 merge_commit_graph(ctx
, g
);
2247 stop_progress(&ctx
->progress
);
2253 ctx
->new_base_graph
= g
;
2254 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2257 if (ctx
->new_base_graph
)
2258 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2260 sort_and_scan_merged_commits(ctx
);
2263 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2266 time_t now
= time(NULL
);
2268 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2270 struct utimbuf updated_time
;
2272 if (stat(ctx
->commit_graph_filenames_before
[i
], &st
) < 0)
2275 updated_time
.actime
= st
.st_atime
;
2276 updated_time
.modtime
= now
;
2277 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2281 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2283 struct strbuf path
= STRBUF_INIT
;
2287 timestamp_t expire_time
= time(NULL
);
2289 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2290 expire_time
= ctx
->opts
->expire_time
;
2292 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2293 unlink(chain_file_name
);
2294 free(chain_file_name
);
2295 ctx
->num_commit_graphs_after
= 0;
2298 strbuf_addstr(&path
, ctx
->odb
->path
);
2299 strbuf_addstr(&path
, "/info/commit-graphs");
2300 dir
= opendir(path
.buf
);
2305 strbuf_addch(&path
, '/');
2306 dirnamelen
= path
.len
;
2307 while ((de
= readdir(dir
)) != NULL
) {
2309 uint32_t i
, found
= 0;
2311 strbuf_setlen(&path
, dirnamelen
);
2312 strbuf_addstr(&path
, de
->d_name
);
2314 if (stat(path
.buf
, &st
) < 0)
2317 if (st
.st_mtime
> expire_time
)
2319 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2322 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2323 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2337 strbuf_release(&path
);
2340 int write_commit_graph(struct object_directory
*odb
,
2341 const struct string_list
*const pack_indexes
,
2342 struct oidset
*commits
,
2343 enum commit_graph_write_flags flags
,
2344 const struct commit_graph_opts
*opts
)
2346 struct repository
*r
= the_repository
;
2347 struct write_commit_graph_context
*ctx
;
2351 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2352 struct topo_level_slab topo_levels
;
2354 prepare_repo_settings(r
);
2355 if (!r
->settings
.core_commit_graph
) {
2356 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2359 if (!commit_graph_compatible(r
))
2362 CALLOC_ARRAY(ctx
, 1);
2365 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2366 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2367 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2369 ctx
->total_bloom_filter_data_size
= 0;
2370 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2371 ctx
->num_generation_data_overflows
= 0;
2373 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2374 bloom_settings
.bits_per_entry
);
2375 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2376 bloom_settings
.num_hashes
);
2377 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2378 bloom_settings
.max_changed_paths
);
2379 ctx
->bloom_settings
= &bloom_settings
;
2381 init_topo_level_slab(&topo_levels
);
2382 ctx
->topo_levels
= &topo_levels
;
2384 prepare_commit_graph(ctx
->r
);
2385 if (ctx
->r
->objects
->commit_graph
) {
2386 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2389 g
->topo_levels
= &topo_levels
;
2394 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2395 ctx
->changed_paths
= 1;
2396 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2397 struct commit_graph
*g
;
2399 g
= ctx
->r
->objects
->commit_graph
;
2401 /* We have changed-paths already. Keep them in the next graph */
2402 if (g
&& g
->chunk_bloom_data
) {
2403 ctx
->changed_paths
= 1;
2404 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2409 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2412 ctx
->num_commit_graphs_before
++;
2416 if (ctx
->num_commit_graphs_before
) {
2417 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2418 i
= ctx
->num_commit_graphs_before
;
2419 g
= ctx
->r
->objects
->commit_graph
;
2422 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2428 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2431 ctx
->approx_nr_objects
= repo_approximate_object_count(the_repository
);
2433 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2434 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2435 for (i
= 0; i
< g
->num_commits
; i
++) {
2436 struct object_id oid
;
2437 oidread(&oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2438 oid_array_append(&ctx
->oids
, &oid
);
2443 ctx
->order_by_pack
= 1;
2444 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2449 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2453 if (!pack_indexes
&& !commits
) {
2454 ctx
->order_by_pack
= 1;
2455 fill_oids_from_all_packs(ctx
);
2458 close_reachable(ctx
);
2460 copy_oids_to_commits(ctx
);
2462 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2463 error(_("too many commits to write graph"));
2468 if (!ctx
->commits
.nr
&& !replace
)
2472 split_graph_merge_strategy(ctx
);
2475 merge_commit_graphs(ctx
);
2477 ctx
->num_commit_graphs_after
= 1;
2479 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2481 compute_topological_levels(ctx
);
2482 if (ctx
->write_generation_data
)
2483 compute_generation_numbers(ctx
);
2485 if (ctx
->changed_paths
)
2486 compute_bloom_filters(ctx
);
2488 res
= write_commit_graph_file(ctx
);
2491 mark_commit_graphs(ctx
);
2493 expire_commit_graphs(ctx
);
2496 free(ctx
->graph_name
);
2497 free(ctx
->commits
.list
);
2498 oid_array_clear(&ctx
->oids
);
2499 clear_topo_level_slab(&topo_levels
);
2501 if (ctx
->commit_graph_filenames_after
) {
2502 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2503 free(ctx
->commit_graph_filenames_after
[i
]);
2504 free(ctx
->commit_graph_hash_after
[i
]);
2507 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2508 free(ctx
->commit_graph_filenames_before
[i
]);
2510 free(ctx
->commit_graph_filenames_after
);
2511 free(ctx
->commit_graph_filenames_before
);
2512 free(ctx
->commit_graph_hash_after
);
2520 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2521 static int verify_commit_graph_error
;
2523 __attribute__((format (printf
, 1, 2)))
2524 static void graph_report(const char *fmt
, ...)
2528 verify_commit_graph_error
= 1;
2530 vfprintf(stderr
, fmt
, ap
);
2531 fprintf(stderr
, "\n");
2535 #define GENERATION_ZERO_EXISTS 1
2536 #define GENERATION_NUMBER_EXISTS 2
2538 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2540 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2543 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2545 uint32_t i
, cur_fanout_pos
= 0;
2546 struct object_id prev_oid
, cur_oid
;
2547 int generation_zero
= 0;
2548 struct progress
*progress
= NULL
;
2549 int local_error
= 0;
2552 graph_report("no commit-graph file loaded");
2556 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2557 if (verify_commit_graph_error
)
2558 return verify_commit_graph_error
;
2560 if (!commit_graph_checksum_valid(g
)) {
2561 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2562 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2565 for (i
= 0; i
< g
->num_commits
; i
++) {
2566 struct commit
*graph_commit
;
2568 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2570 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2571 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2572 oid_to_hex(&prev_oid
),
2573 oid_to_hex(&cur_oid
));
2575 oidcpy(&prev_oid
, &cur_oid
);
2577 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2578 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2580 if (i
!= fanout_value
)
2581 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2582 cur_fanout_pos
, fanout_value
, i
);
2586 graph_commit
= lookup_commit(r
, &cur_oid
);
2587 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2588 graph_report(_("failed to parse commit %s from commit-graph"),
2589 oid_to_hex(&cur_oid
));
2592 while (cur_fanout_pos
< 256) {
2593 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2595 if (g
->num_commits
!= fanout_value
)
2596 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2597 cur_fanout_pos
, fanout_value
, i
);
2602 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2603 return verify_commit_graph_error
;
2605 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2606 progress
= start_progress(_("Verifying commits in commit graph"),
2609 for (i
= 0; i
< g
->num_commits
; i
++) {
2610 struct commit
*graph_commit
, *odb_commit
;
2611 struct commit_list
*graph_parents
, *odb_parents
;
2612 timestamp_t max_generation
= 0;
2613 timestamp_t generation
;
2615 display_progress(progress
, i
+ 1);
2616 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2618 graph_commit
= lookup_commit(r
, &cur_oid
);
2619 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2620 if (repo_parse_commit_internal(r
, odb_commit
, 0, 0)) {
2621 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2622 oid_to_hex(&cur_oid
));
2626 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2627 get_commit_tree_oid(odb_commit
)))
2628 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2629 oid_to_hex(&cur_oid
),
2630 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2631 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2633 graph_parents
= graph_commit
->parents
;
2634 odb_parents
= odb_commit
->parents
;
2636 while (graph_parents
) {
2638 graph_report(_("commit-graph parent list for commit %s is too long"),
2639 oid_to_hex(&cur_oid
));
2643 /* parse parent in case it is in a base graph */
2644 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2646 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2647 graph_report(_("commit-graph parent for %s is %s != %s"),
2648 oid_to_hex(&cur_oid
),
2649 oid_to_hex(&graph_parents
->item
->object
.oid
),
2650 oid_to_hex(&odb_parents
->item
->object
.oid
));
2652 generation
= commit_graph_generation(graph_parents
->item
);
2653 if (generation
> max_generation
)
2654 max_generation
= generation
;
2656 graph_parents
= graph_parents
->next
;
2657 odb_parents
= odb_parents
->next
;
2661 graph_report(_("commit-graph parent list for commit %s terminates early"),
2662 oid_to_hex(&cur_oid
));
2664 if (!commit_graph_generation(graph_commit
)) {
2665 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2666 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2667 oid_to_hex(&cur_oid
));
2668 generation_zero
= GENERATION_ZERO_EXISTS
;
2669 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2670 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2671 oid_to_hex(&cur_oid
));
2673 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2677 * If we are using topological level and one of our parents has
2678 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2679 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2680 * in the following condition.
2682 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2685 generation
= commit_graph_generation(graph_commit
);
2686 if (generation
< max_generation
+ 1)
2687 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2688 oid_to_hex(&cur_oid
),
2690 max_generation
+ 1);
2692 if (graph_commit
->date
!= odb_commit
->date
)
2693 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2694 oid_to_hex(&cur_oid
),
2698 stop_progress(&progress
);
2700 local_error
= verify_commit_graph_error
;
2702 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2703 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2708 void free_commit_graph(struct commit_graph
*g
)
2713 munmap((void *)g
->data
, g
->data_len
);
2717 free(g
->bloom_filter_settings
);
2721 void disable_commit_graph(struct repository
*r
)
2723 r
->commit_graph_disabled
= 1;