1 #include "git-compat-util.h"
12 #include "hash-lookup.h"
13 #include "commit-graph.h"
14 #include "object-file.h"
15 #include "object-store-ll.h"
16 #include "oid-array.h"
20 #include "replace-object.h"
23 #include "commit-slab.h"
25 #include "json-writer.h"
28 #include "chunk-format.h"
30 void git_test_write_commit_graph_or_die(void)
33 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0))
36 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
37 flags
= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
39 if (write_commit_graph_reachable(the_repository
->objects
->odb
,
41 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
44 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
45 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
46 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
47 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
48 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
49 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
50 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
51 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
52 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
53 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
55 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
57 #define GRAPH_VERSION_1 0x1
58 #define GRAPH_VERSION GRAPH_VERSION_1
60 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
61 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
62 #define GRAPH_PARENT_NONE 0x70000000
64 #define GRAPH_LAST_EDGE 0x80000000
66 #define GRAPH_HEADER_SIZE 8
67 #define GRAPH_FANOUT_SIZE (4 * 256)
68 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
69 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
71 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
73 /* Remember to update object flag allocation in object.h */
74 #define REACHABLE (1u<<15)
76 define_commit_slab(topo_level_slab
, uint32_t);
78 /* Keep track of the order in which commits are added to our list. */
79 define_commit_slab(commit_pos
, int);
80 static struct commit_pos commit_pos
= COMMIT_SLAB_INIT(1, commit_pos
);
82 static void set_commit_pos(struct repository
*r
, const struct object_id
*oid
)
84 static int32_t max_pos
;
85 struct commit
*commit
= lookup_commit(r
, oid
);
88 return; /* should never happen, but be lenient */
90 *commit_pos_at(&commit_pos
, commit
) = max_pos
++;
93 static int commit_pos_cmp(const void *va
, const void *vb
)
95 const struct commit
*a
= *(const struct commit
**)va
;
96 const struct commit
*b
= *(const struct commit
**)vb
;
97 return commit_pos_at(&commit_pos
, a
) -
98 commit_pos_at(&commit_pos
, b
);
101 define_commit_slab(commit_graph_data_slab
, struct commit_graph_data
);
102 static struct commit_graph_data_slab commit_graph_data_slab
=
103 COMMIT_SLAB_INIT(1, commit_graph_data_slab
);
105 static int get_configured_generation_version(struct repository
*r
)
108 repo_config_get_int(r
, "commitgraph.generationversion", &version
);
112 uint32_t commit_graph_position(const struct commit
*c
)
114 struct commit_graph_data
*data
=
115 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
117 return data
? data
->graph_pos
: COMMIT_NOT_FROM_GRAPH
;
120 timestamp_t
commit_graph_generation(const struct commit
*c
)
122 struct commit_graph_data
*data
=
123 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
125 if (data
&& data
->generation
)
126 return data
->generation
;
128 return GENERATION_NUMBER_INFINITY
;
131 static struct commit_graph_data
*commit_graph_data_at(const struct commit
*c
)
133 unsigned int i
, nth_slab
;
134 struct commit_graph_data
*data
=
135 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
140 nth_slab
= c
->index
/ commit_graph_data_slab
.slab_size
;
141 data
= commit_graph_data_slab_at(&commit_graph_data_slab
, c
);
144 * commit-slab initializes elements with zero, overwrite this with
145 * COMMIT_NOT_FROM_GRAPH for graph_pos.
147 * We avoid initializing generation with checking if graph position
148 * is not COMMIT_NOT_FROM_GRAPH.
150 for (i
= 0; i
< commit_graph_data_slab
.slab_size
; i
++) {
151 commit_graph_data_slab
.slab
[nth_slab
][i
].graph_pos
=
152 COMMIT_NOT_FROM_GRAPH
;
159 * Should be used only while writing commit-graph as it compares
160 * generation value of commits by directly accessing commit-slab.
162 static int commit_gen_cmp(const void *va
, const void *vb
)
164 const struct commit
*a
= *(const struct commit
**)va
;
165 const struct commit
*b
= *(const struct commit
**)vb
;
167 const timestamp_t generation_a
= commit_graph_data_at(a
)->generation
;
168 const timestamp_t generation_b
= commit_graph_data_at(b
)->generation
;
169 /* lower generation commits first */
170 if (generation_a
< generation_b
)
172 else if (generation_a
> generation_b
)
175 /* use date as a heuristic when generations are equal */
176 if (a
->date
< b
->date
)
178 else if (a
->date
> b
->date
)
183 char *get_commit_graph_filename(struct object_directory
*obj_dir
)
185 return xstrfmt("%s/info/commit-graph", obj_dir
->path
);
188 static char *get_split_graph_filename(struct object_directory
*odb
,
191 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb
->path
,
195 char *get_commit_graph_chain_filename(struct object_directory
*odb
)
197 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb
->path
);
200 static struct commit_graph
*alloc_commit_graph(void)
202 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
207 static int commit_graph_compatible(struct repository
*r
)
212 if (replace_refs_enabled(r
)) {
213 prepare_replace_object(r
);
214 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
218 prepare_commit_graft(r
);
219 if (r
->parsed_objects
&&
220 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
222 if (is_repository_shallow(r
))
228 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
230 *fd
= git_open(graph_file
);
233 if (fstat(*fd
, st
)) {
240 struct commit_graph
*load_commit_graph_one_fd_st(struct repository
*r
,
241 int fd
, struct stat
*st
,
242 struct object_directory
*odb
)
246 struct commit_graph
*ret
;
248 graph_size
= xsize_t(st
->st_size
);
250 if (graph_size
< GRAPH_MIN_SIZE
) {
252 error(_("commit-graph file is too small"));
255 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
257 prepare_repo_settings(r
);
258 ret
= parse_commit_graph(&r
->settings
, graph_map
, graph_size
);
263 munmap(graph_map
, graph_size
);
268 static int verify_commit_graph_lite(struct commit_graph
*g
)
271 * Basic validation shared between parse_commit_graph()
272 * which'll be called every time the graph is used, and the
273 * much more expensive verify_commit_graph() used by
274 * "commit-graph verify".
276 * There should only be very basic checks here to ensure that
277 * we don't e.g. segfault in fill_commit_in_graph(), but
278 * because this is a very hot codepath nothing that e.g. loops
279 * over g->num_commits, or runs a checksum on the commit-graph
282 if (!g
->chunk_oid_fanout
) {
283 error("commit-graph is missing the OID Fanout chunk");
286 if (!g
->chunk_oid_lookup
) {
287 error("commit-graph is missing the OID Lookup chunk");
290 if (!g
->chunk_commit_data
) {
291 error("commit-graph is missing the Commit Data chunk");
298 static int graph_read_oid_lookup(const unsigned char *chunk_start
,
299 size_t chunk_size
, void *data
)
301 struct commit_graph
*g
= data
;
302 g
->chunk_oid_lookup
= chunk_start
;
303 g
->num_commits
= chunk_size
/ g
->hash_len
;
307 static int graph_read_bloom_data(const unsigned char *chunk_start
,
308 size_t chunk_size
, void *data
)
310 struct commit_graph
*g
= data
;
311 uint32_t hash_version
;
312 g
->chunk_bloom_data
= chunk_start
;
313 hash_version
= get_be32(chunk_start
);
315 if (hash_version
!= 1)
318 g
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
319 g
->bloom_filter_settings
->hash_version
= hash_version
;
320 g
->bloom_filter_settings
->num_hashes
= get_be32(chunk_start
+ 4);
321 g
->bloom_filter_settings
->bits_per_entry
= get_be32(chunk_start
+ 8);
322 g
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
327 struct commit_graph
*parse_commit_graph(struct repo_settings
*s
,
328 void *graph_map
, size_t graph_size
)
330 const unsigned char *data
;
331 struct commit_graph
*graph
;
332 uint32_t graph_signature
;
333 unsigned char graph_version
, hash_version
;
334 struct chunkfile
*cf
= NULL
;
339 if (graph_size
< GRAPH_MIN_SIZE
)
342 data
= (const unsigned char *)graph_map
;
344 graph_signature
= get_be32(data
);
345 if (graph_signature
!= GRAPH_SIGNATURE
) {
346 error(_("commit-graph signature %X does not match signature %X"),
347 graph_signature
, GRAPH_SIGNATURE
);
351 graph_version
= *(unsigned char*)(data
+ 4);
352 if (graph_version
!= GRAPH_VERSION
) {
353 error(_("commit-graph version %X does not match version %X"),
354 graph_version
, GRAPH_VERSION
);
358 hash_version
= *(unsigned char*)(data
+ 5);
359 if (hash_version
!= oid_version(the_hash_algo
)) {
360 error(_("commit-graph hash version %X does not match version %X"),
361 hash_version
, oid_version(the_hash_algo
));
365 graph
= alloc_commit_graph();
367 graph
->hash_len
= the_hash_algo
->rawsz
;
368 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
369 graph
->data
= graph_map
;
370 graph
->data_len
= graph_size
;
372 if (graph_size
< GRAPH_HEADER_SIZE
+
373 (graph
->num_chunks
+ 1) * CHUNK_TOC_ENTRY_SIZE
+
374 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
375 error(_("commit-graph file is too small to hold %u chunks"),
381 cf
= init_chunkfile(NULL
);
383 if (read_table_of_contents(cf
, graph
->data
, graph_size
,
384 GRAPH_HEADER_SIZE
, graph
->num_chunks
))
385 goto free_and_return
;
387 pair_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
,
388 (const unsigned char **)&graph
->chunk_oid_fanout
);
389 read_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, graph_read_oid_lookup
, graph
);
390 pair_chunk(cf
, GRAPH_CHUNKID_DATA
, &graph
->chunk_commit_data
);
391 pair_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
, &graph
->chunk_extra_edges
);
392 pair_chunk(cf
, GRAPH_CHUNKID_BASE
, &graph
->chunk_base_graphs
);
394 if (s
->commit_graph_generation_version
>= 2) {
395 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
396 &graph
->chunk_generation_data
);
397 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
398 &graph
->chunk_generation_data_overflow
);
400 if (graph
->chunk_generation_data
)
401 graph
->read_generation_data
= 1;
404 if (s
->commit_graph_read_changed_paths
) {
405 pair_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
406 &graph
->chunk_bloom_indexes
);
407 read_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
408 graph_read_bloom_data
, graph
);
411 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
412 init_bloom_filters();
414 /* We need both the bloom chunks to exist together. Else ignore the data */
415 graph
->chunk_bloom_indexes
= NULL
;
416 graph
->chunk_bloom_data
= NULL
;
417 FREE_AND_NULL(graph
->bloom_filter_settings
);
420 oidread(&graph
->oid
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
422 if (verify_commit_graph_lite(graph
))
423 goto free_and_return
;
430 free(graph
->bloom_filter_settings
);
435 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
436 const char *graph_file
,
437 struct object_directory
*odb
)
442 struct commit_graph
*g
;
443 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
448 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
451 g
->filename
= xstrdup(graph_file
);
456 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
457 struct object_directory
*odb
)
459 char *graph_name
= get_commit_graph_filename(odb
);
460 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
466 static int add_graph_to_chain(struct commit_graph
*g
,
467 struct commit_graph
*chain
,
468 struct object_id
*oids
,
471 struct commit_graph
*cur_g
= chain
;
473 if (n
&& !g
->chunk_base_graphs
) {
474 warning(_("commit-graph has no base graphs chunk"));
482 !oideq(&oids
[n
], &cur_g
->oid
) ||
483 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
484 warning(_("commit-graph chain does not match"));
488 cur_g
= cur_g
->base_graph
;
491 g
->base_graph
= chain
;
494 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
499 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
500 struct object_directory
*odb
)
502 struct commit_graph
*graph_chain
= NULL
;
503 struct strbuf line
= STRBUF_INIT
;
505 struct object_id
*oids
;
506 int i
= 0, valid
= 1, count
;
507 char *chain_name
= get_commit_graph_chain_filename(odb
);
511 fp
= fopen(chain_name
, "r");
512 stat_res
= stat(chain_name
, &st
);
518 st
.st_size
<= the_hash_algo
->hexsz
) {
523 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
524 CALLOC_ARRAY(oids
, count
);
528 for (i
= 0; i
< count
; i
++) {
529 struct object_directory
*odb
;
531 if (strbuf_getline_lf(&line
, fp
) == EOF
)
534 if (get_oid_hex(line
.buf
, &oids
[i
])) {
535 warning(_("invalid commit-graph chain: line '%s' not a hash"),
542 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
543 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
544 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
549 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
559 warning(_("unable to find all commit-graph files"));
566 strbuf_release(&line
);
572 * returns 1 if and only if all graphs in the chain have
573 * corrected commit dates stored in the generation_data chunk.
575 static int validate_mixed_generation_chain(struct commit_graph
*g
)
577 int read_generation_data
= 1;
578 struct commit_graph
*p
= g
;
580 while (read_generation_data
&& p
) {
581 read_generation_data
= p
->read_generation_data
;
585 if (read_generation_data
)
589 g
->read_generation_data
= 0;
596 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
597 struct object_directory
*odb
)
599 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
602 g
= load_commit_graph_chain(r
, odb
);
604 validate_mixed_generation_chain(g
);
609 static void prepare_commit_graph_one(struct repository
*r
,
610 struct object_directory
*odb
)
613 if (r
->objects
->commit_graph
)
616 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
620 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
622 * On the first invocation, this function attempts to load the commit
623 * graph if the_repository is configured to have one.
625 static int prepare_commit_graph(struct repository
*r
)
627 struct object_directory
*odb
;
630 * Early return if there is no git dir or if the commit graph is
633 * This must come before the "already attempted?" check below, because
634 * we want to disable even an already-loaded graph file.
636 if (!r
->gitdir
|| r
->commit_graph_disabled
)
639 if (r
->objects
->commit_graph_attempted
)
640 return !!r
->objects
->commit_graph
;
641 r
->objects
->commit_graph_attempted
= 1;
643 prepare_repo_settings(r
);
645 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
646 r
->settings
.core_commit_graph
!= 1)
648 * This repository is not configured to use commit graphs, so
649 * do not load one. (But report commit_graph_attempted anyway
650 * so that commit graph loading is not attempted again for this
655 if (!commit_graph_compatible(r
))
659 for (odb
= r
->objects
->odb
;
660 !r
->objects
->commit_graph
&& odb
;
662 prepare_commit_graph_one(r
, odb
);
663 return !!r
->objects
->commit_graph
;
666 int generation_numbers_enabled(struct repository
*r
)
668 uint32_t first_generation
;
669 struct commit_graph
*g
;
670 if (!prepare_commit_graph(r
))
673 g
= r
->objects
->commit_graph
;
678 first_generation
= get_be32(g
->chunk_commit_data
+
679 g
->hash_len
+ 8) >> 2;
681 return !!first_generation
;
684 int corrected_commit_dates_enabled(struct repository
*r
)
686 struct commit_graph
*g
;
687 if (!prepare_commit_graph(r
))
690 g
= r
->objects
->commit_graph
;
695 return g
->read_generation_data
;
698 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
700 struct commit_graph
*g
= r
->objects
->commit_graph
;
702 if (g
->bloom_filter_settings
)
703 return g
->bloom_filter_settings
;
709 static void close_commit_graph_one(struct commit_graph
*g
)
714 clear_commit_graph_data_slab(&commit_graph_data_slab
);
715 close_commit_graph_one(g
->base_graph
);
716 free_commit_graph(g
);
719 void close_commit_graph(struct raw_object_store
*o
)
721 close_commit_graph_one(o
->commit_graph
);
722 o
->commit_graph
= NULL
;
725 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
727 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
728 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
731 static void load_oid_from_graph(struct commit_graph
*g
,
733 struct object_id
*oid
)
737 while (g
&& pos
< g
->num_commits_in_base
)
741 BUG("NULL commit-graph");
743 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
744 die(_("invalid commit position. commit-graph is likely corrupt"));
746 lex_index
= pos
- g
->num_commits_in_base
;
748 oidread(oid
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
751 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
752 struct commit_graph
*g
,
754 struct commit_list
**pptr
)
757 struct object_id oid
;
759 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
760 die("invalid parent position %"PRIu32
, pos
);
762 load_oid_from_graph(g
, pos
, &oid
);
763 c
= lookup_commit(r
, &oid
);
765 die(_("could not find commit %s"), oid_to_hex(&oid
));
766 commit_graph_data_at(c
)->graph_pos
= pos
;
767 return &commit_list_insert(c
, pptr
)->next
;
770 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
772 const unsigned char *commit_data
;
773 struct commit_graph_data
*graph_data
;
774 uint32_t lex_index
, offset_pos
;
775 uint64_t date_high
, date_low
, offset
;
777 while (pos
< g
->num_commits_in_base
)
780 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
781 die(_("invalid commit position. commit-graph is likely corrupt"));
783 lex_index
= pos
- g
->num_commits_in_base
;
784 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
786 graph_data
= commit_graph_data_at(item
);
787 graph_data
->graph_pos
= pos
;
789 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
790 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
791 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
793 if (g
->read_generation_data
) {
794 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ sizeof(uint32_t) * lex_index
);
796 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
797 if (!g
->chunk_generation_data_overflow
)
798 die(_("commit-graph requires overflow generation data but has none"));
800 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
801 graph_data
->generation
= item
->date
+ get_be64(g
->chunk_generation_data_overflow
+ 8 * offset_pos
);
803 graph_data
->generation
= item
->date
+ offset
;
805 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
808 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
811 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
816 static int fill_commit_in_graph(struct repository
*r
,
818 struct commit_graph
*g
, uint32_t pos
)
821 uint32_t *parent_data_ptr
;
822 struct commit_list
**pptr
;
823 const unsigned char *commit_data
;
826 while (pos
< g
->num_commits_in_base
)
829 fill_commit_graph_info(item
, g
, pos
);
831 lex_index
= pos
- g
->num_commits_in_base
;
832 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
834 item
->object
.parsed
= 1;
836 set_commit_tree(item
, NULL
);
838 pptr
= &item
->parents
;
840 edge_value
= get_be32(commit_data
+ g
->hash_len
);
841 if (edge_value
== GRAPH_PARENT_NONE
)
843 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
845 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
846 if (edge_value
== GRAPH_PARENT_NONE
)
848 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
849 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
853 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
854 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
856 edge_value
= get_be32(parent_data_ptr
);
857 pptr
= insert_parent_or_die(r
, g
,
858 edge_value
& GRAPH_EDGE_LAST_MASK
,
861 } while (!(edge_value
& GRAPH_LAST_EDGE
));
866 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
868 struct commit_graph
*cur_g
= g
;
871 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
872 cur_g
= cur_g
->base_graph
;
875 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
882 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
884 uint32_t graph_pos
= commit_graph_position(item
);
885 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
889 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
893 int repo_find_commit_pos_in_graph(struct repository
*r
, struct commit
*c
,
896 if (!prepare_commit_graph(r
))
898 return find_commit_pos_in_graph(c
, r
->objects
->commit_graph
, pos
);
901 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
903 struct commit
*commit
;
906 if (!prepare_commit_graph(repo
))
908 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
910 if (!has_object(repo
, id
, 0))
913 commit
= lookup_commit(repo
, id
);
916 if (commit
->object
.parsed
)
919 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
925 static int parse_commit_in_graph_one(struct repository
*r
,
926 struct commit_graph
*g
,
931 if (item
->object
.parsed
)
934 if (find_commit_pos_in_graph(item
, g
, &pos
))
935 return fill_commit_in_graph(r
, item
, g
, pos
);
940 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
942 static int checked_env
= 0;
945 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
946 die("dying as requested by the '%s' variable on commit-graph parse!",
947 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
950 if (!prepare_commit_graph(r
))
952 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
955 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
958 if (repo_find_commit_pos_in_graph(r
, item
, &pos
))
959 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
962 static struct tree
*load_tree_for_commit(struct repository
*r
,
963 struct commit_graph
*g
,
966 struct object_id oid
;
967 const unsigned char *commit_data
;
968 uint32_t graph_pos
= commit_graph_position(c
);
970 while (graph_pos
< g
->num_commits_in_base
)
973 commit_data
= g
->chunk_commit_data
+
974 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
976 oidread(&oid
, commit_data
);
977 set_commit_tree(c
, lookup_tree(r
, &oid
));
979 return c
->maybe_tree
;
982 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
983 struct commit_graph
*g
,
984 const struct commit
*c
)
987 return c
->maybe_tree
;
988 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
989 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
991 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
994 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
996 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
999 struct packed_commit_list
{
1000 struct commit
**list
;
1005 struct write_commit_graph_context
{
1006 struct repository
*r
;
1007 struct object_directory
*odb
;
1009 struct oid_array oids
;
1010 struct packed_commit_list commits
;
1011 int num_extra_edges
;
1012 int num_generation_data_overflows
;
1013 unsigned long approx_nr_objects
;
1014 struct progress
*progress
;
1016 uint64_t progress_cnt
;
1018 char *base_graph_name
;
1019 int num_commit_graphs_before
;
1020 int num_commit_graphs_after
;
1021 char **commit_graph_filenames_before
;
1022 char **commit_graph_filenames_after
;
1023 char **commit_graph_hash_after
;
1024 uint32_t new_num_commits_in_base
;
1025 struct commit_graph
*new_base_graph
;
1032 write_generation_data
:1,
1033 trust_generation_numbers
:1;
1035 struct topo_level_slab
*topo_levels
;
1036 const struct commit_graph_opts
*opts
;
1037 size_t total_bloom_filter_data_size
;
1038 const struct bloom_filter_settings
*bloom_settings
;
1040 int count_bloom_filter_computed
;
1041 int count_bloom_filter_not_computed
;
1042 int count_bloom_filter_trunc_empty
;
1043 int count_bloom_filter_trunc_large
;
1046 static int write_graph_chunk_fanout(struct hashfile
*f
,
1049 struct write_commit_graph_context
*ctx
= data
;
1051 struct commit
**list
= ctx
->commits
.list
;
1054 * Write the first-level table (the list is sorted,
1055 * but we use a 256-entry lookup to be able to avoid
1056 * having to do eight extra binary search iterations).
1058 for (i
= 0; i
< 256; i
++) {
1059 while (count
< ctx
->commits
.nr
) {
1060 if ((*list
)->object
.oid
.hash
[0] != i
)
1062 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1067 hashwrite_be32(f
, count
);
1073 static int write_graph_chunk_oids(struct hashfile
*f
,
1076 struct write_commit_graph_context
*ctx
= data
;
1077 struct commit
**list
= ctx
->commits
.list
;
1079 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1080 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1081 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1087 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1089 const struct commit
* const *commits
= table
;
1090 return &commits
[index
]->object
.oid
;
1093 static int write_graph_chunk_data(struct hashfile
*f
,
1096 struct write_commit_graph_context
*ctx
= data
;
1097 struct commit
**list
= ctx
->commits
.list
;
1098 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1099 uint32_t num_extra_edges
= 0;
1101 while (list
< last
) {
1102 struct commit_list
*parent
;
1103 struct object_id
*tree
;
1105 uint32_t packedDate
[2];
1106 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1108 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1109 die(_("unable to parse commit %s"),
1110 oid_to_hex(&(*list
)->object
.oid
));
1111 tree
= get_commit_tree_oid(*list
);
1112 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1114 parent
= (*list
)->parents
;
1117 edge_value
= GRAPH_PARENT_NONE
;
1119 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1124 if (edge_value
>= 0)
1125 edge_value
+= ctx
->new_num_commits_in_base
;
1126 else if (ctx
->new_base_graph
) {
1128 if (find_commit_pos_in_graph(parent
->item
,
1129 ctx
->new_base_graph
,
1135 BUG("missing parent %s for commit %s",
1136 oid_to_hex(&parent
->item
->object
.oid
),
1137 oid_to_hex(&(*list
)->object
.oid
));
1140 hashwrite_be32(f
, edge_value
);
1143 parent
= parent
->next
;
1146 edge_value
= GRAPH_PARENT_NONE
;
1147 else if (parent
->next
)
1148 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1150 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1155 if (edge_value
>= 0)
1156 edge_value
+= ctx
->new_num_commits_in_base
;
1157 else if (ctx
->new_base_graph
) {
1159 if (find_commit_pos_in_graph(parent
->item
,
1160 ctx
->new_base_graph
,
1166 BUG("missing parent %s for commit %s",
1167 oid_to_hex(&parent
->item
->object
.oid
),
1168 oid_to_hex(&(*list
)->object
.oid
));
1171 hashwrite_be32(f
, edge_value
);
1173 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1176 parent
= parent
->next
;
1180 if (sizeof((*list
)->date
) > 4)
1181 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1185 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1187 packedDate
[1] = htonl((*list
)->date
);
1188 hashwrite(f
, packedDate
, 8);
1196 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1199 struct write_commit_graph_context
*ctx
= data
;
1200 int i
, num_generation_data_overflows
= 0;
1202 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1203 struct commit
*c
= ctx
->commits
.list
[i
];
1205 repo_parse_commit(ctx
->r
, c
);
1206 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1207 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1209 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1210 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1211 num_generation_data_overflows
++;
1214 hashwrite_be32(f
, offset
);
1220 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1223 struct write_commit_graph_context
*ctx
= data
;
1225 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1226 struct commit
*c
= ctx
->commits
.list
[i
];
1227 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1228 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1230 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1231 hashwrite_be32(f
, offset
>> 32);
1232 hashwrite_be32(f
, (uint32_t) offset
);
1239 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1242 struct write_commit_graph_context
*ctx
= data
;
1243 struct commit
**list
= ctx
->commits
.list
;
1244 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1245 struct commit_list
*parent
;
1247 while (list
< last
) {
1248 int num_parents
= 0;
1250 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1252 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1253 parent
= parent
->next
)
1256 if (num_parents
<= 2) {
1261 /* Since num_parents > 2, this initializer is safe. */
1262 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1263 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1268 if (edge_value
>= 0)
1269 edge_value
+= ctx
->new_num_commits_in_base
;
1270 else if (ctx
->new_base_graph
) {
1272 if (find_commit_pos_in_graph(parent
->item
,
1273 ctx
->new_base_graph
,
1279 BUG("missing parent %s for commit %s",
1280 oid_to_hex(&parent
->item
->object
.oid
),
1281 oid_to_hex(&(*list
)->object
.oid
));
1282 else if (!parent
->next
)
1283 edge_value
|= GRAPH_LAST_EDGE
;
1285 hashwrite_be32(f
, edge_value
);
1294 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1297 struct write_commit_graph_context
*ctx
= data
;
1298 struct commit
**list
= ctx
->commits
.list
;
1299 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1300 uint32_t cur_pos
= 0;
1302 while (list
< last
) {
1303 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1304 size_t len
= filter
? filter
->len
: 0;
1306 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1307 hashwrite_be32(f
, cur_pos
);
1314 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1316 struct json_writer jw
= JSON_WRITER_INIT
;
1318 jw_object_begin(&jw
, 0);
1319 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1320 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1321 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1322 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1325 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1330 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1333 struct write_commit_graph_context
*ctx
= data
;
1334 struct commit
**list
= ctx
->commits
.list
;
1335 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1337 trace2_bloom_filter_settings(ctx
);
1339 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1340 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1341 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1343 while (list
< last
) {
1344 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1345 size_t len
= filter
? filter
->len
: 0;
1347 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1349 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1356 static int add_packed_commits(const struct object_id
*oid
,
1357 struct packed_git
*pack
,
1361 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1362 enum object_type type
;
1363 off_t offset
= nth_packed_object_offset(pack
, pos
);
1364 struct object_info oi
= OBJECT_INFO_INIT
;
1367 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1370 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1371 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1373 if (type
!= OBJ_COMMIT
)
1376 oid_array_append(&ctx
->oids
, oid
);
1377 set_commit_pos(ctx
->r
, oid
);
1382 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1384 struct commit_list
*parent
;
1385 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1386 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1387 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1388 parent
->item
->object
.flags
|= REACHABLE
;
1393 static void close_reachable(struct write_commit_graph_context
*ctx
)
1396 struct commit
*commit
;
1397 enum commit_graph_split_flags flags
= ctx
->opts
?
1398 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1400 if (ctx
->report_progress
)
1401 ctx
->progress
= start_delayed_progress(
1402 _("Loading known commits in commit graph"),
1404 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1405 display_progress(ctx
->progress
, i
+ 1);
1406 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1408 commit
->object
.flags
|= REACHABLE
;
1410 stop_progress(&ctx
->progress
);
1413 * As this loop runs, ctx->oids.nr may grow, but not more
1414 * than the number of missing commits in the reachable
1417 if (ctx
->report_progress
)
1418 ctx
->progress
= start_delayed_progress(
1419 _("Expanding reachable commits in commit graph"),
1421 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1422 display_progress(ctx
->progress
, i
+ 1);
1423 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1428 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1429 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1430 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1431 add_missing_parents(ctx
, commit
);
1432 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1433 add_missing_parents(ctx
, commit
);
1435 stop_progress(&ctx
->progress
);
1437 if (ctx
->report_progress
)
1438 ctx
->progress
= start_delayed_progress(
1439 _("Clearing commit marks in commit graph"),
1441 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1442 display_progress(ctx
->progress
, i
+ 1);
1443 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1446 commit
->object
.flags
&= ~REACHABLE
;
1448 stop_progress(&ctx
->progress
);
1451 struct compute_generation_info
{
1452 struct repository
*r
;
1453 struct packed_commit_list
*commits
;
1454 struct progress
*progress
;
1457 timestamp_t (*get_generation
)(struct commit
*c
, void *data
);
1458 void (*set_generation
)(struct commit
*c
, timestamp_t gen
, void *data
);
1462 static timestamp_t
compute_generation_from_max(struct commit
*c
,
1463 timestamp_t max_gen
,
1464 int generation_version
)
1466 switch (generation_version
) {
1467 case 1: /* topological levels */
1468 if (max_gen
> GENERATION_NUMBER_V1_MAX
- 1)
1469 max_gen
= GENERATION_NUMBER_V1_MAX
- 1;
1472 case 2: /* corrected commit date */
1473 if (c
->date
&& c
->date
> max_gen
)
1474 max_gen
= c
->date
- 1;
1478 BUG("attempting unimplemented version");
1482 static void compute_reachable_generation_numbers(
1483 struct compute_generation_info
*info
,
1484 int generation_version
)
1487 struct commit_list
*list
= NULL
;
1489 for (i
= 0; i
< info
->commits
->nr
; i
++) {
1490 struct commit
*c
= info
->commits
->list
[i
];
1492 repo_parse_commit(info
->r
, c
);
1493 gen
= info
->get_generation(c
, info
->data
);
1494 display_progress(info
->progress
, info
->progress_cnt
+ 1);
1496 if (gen
!= GENERATION_NUMBER_ZERO
&& gen
!= GENERATION_NUMBER_INFINITY
)
1499 commit_list_insert(c
, &list
);
1501 struct commit
*current
= list
->item
;
1502 struct commit_list
*parent
;
1503 int all_parents_computed
= 1;
1504 uint32_t max_gen
= 0;
1506 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1507 repo_parse_commit(info
->r
, parent
->item
);
1508 gen
= info
->get_generation(parent
->item
, info
->data
);
1510 if (gen
== GENERATION_NUMBER_ZERO
) {
1511 all_parents_computed
= 0;
1512 commit_list_insert(parent
->item
, &list
);
1520 if (all_parents_computed
) {
1522 gen
= compute_generation_from_max(
1524 generation_version
);
1525 info
->set_generation(current
, gen
, info
->data
);
1531 static timestamp_t
get_topo_level(struct commit
*c
, void *data
)
1533 struct write_commit_graph_context
*ctx
= data
;
1534 return *topo_level_slab_at(ctx
->topo_levels
, c
);
1537 static void set_topo_level(struct commit
*c
, timestamp_t t
, void *data
)
1539 struct write_commit_graph_context
*ctx
= data
;
1540 *topo_level_slab_at(ctx
->topo_levels
, c
) = (uint32_t)t
;
1543 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1545 struct compute_generation_info info
= {
1547 .commits
= &ctx
->commits
,
1548 .get_generation
= get_topo_level
,
1549 .set_generation
= set_topo_level
,
1553 if (ctx
->report_progress
)
1554 info
.progress
= ctx
->progress
1555 = start_delayed_progress(
1556 _("Computing commit graph topological levels"),
1559 compute_reachable_generation_numbers(&info
, 1);
1561 stop_progress(&ctx
->progress
);
1564 static timestamp_t
get_generation_from_graph_data(struct commit
*c
, void *data
)
1566 return commit_graph_data_at(c
)->generation
;
1569 static void set_generation_v2(struct commit
*c
, timestamp_t t
, void *data
)
1571 struct commit_graph_data
*g
= commit_graph_data_at(c
);
1575 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1578 struct compute_generation_info info
= {
1580 .commits
= &ctx
->commits
,
1581 .get_generation
= get_generation_from_graph_data
,
1582 .set_generation
= set_generation_v2
,
1586 if (ctx
->report_progress
)
1587 info
.progress
= ctx
->progress
1588 = start_delayed_progress(
1589 _("Computing commit graph generation numbers"),
1592 if (!ctx
->trust_generation_numbers
) {
1593 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1594 struct commit
*c
= ctx
->commits
.list
[i
];
1595 repo_parse_commit(ctx
->r
, c
);
1596 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1600 compute_reachable_generation_numbers(&info
, 2);
1602 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1603 struct commit
*c
= ctx
->commits
.list
[i
];
1604 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1605 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1606 ctx
->num_generation_data_overflows
++;
1608 stop_progress(&ctx
->progress
);
1611 static void set_generation_in_graph_data(struct commit
*c
, timestamp_t t
,
1614 commit_graph_data_at(c
)->generation
= t
;
1618 * After this method, all commits reachable from those in the given
1619 * list will have non-zero, non-infinite generation numbers.
1621 void ensure_generations_valid(struct repository
*r
,
1622 struct commit
**commits
, size_t nr
)
1624 int generation_version
= get_configured_generation_version(r
);
1625 struct packed_commit_list list
= {
1630 struct compute_generation_info info
= {
1633 .get_generation
= get_generation_from_graph_data
,
1634 .set_generation
= set_generation_in_graph_data
,
1637 compute_reachable_generation_numbers(&info
, generation_version
);
1640 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1642 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1643 ctx
->count_bloom_filter_computed
);
1644 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1645 ctx
->count_bloom_filter_not_computed
);
1646 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1647 ctx
->count_bloom_filter_trunc_empty
);
1648 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1649 ctx
->count_bloom_filter_trunc_large
);
1652 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1655 struct progress
*progress
= NULL
;
1656 struct commit
**sorted_commits
;
1657 int max_new_filters
;
1659 init_bloom_filters();
1661 if (ctx
->report_progress
)
1662 progress
= start_delayed_progress(
1663 _("Computing commit changed paths Bloom filters"),
1666 DUP_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1668 if (ctx
->order_by_pack
)
1669 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1671 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1673 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1674 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1676 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1677 enum bloom_filter_computed computed
= 0;
1678 struct commit
*c
= sorted_commits
[i
];
1679 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1682 ctx
->count_bloom_filter_computed
< max_new_filters
,
1683 ctx
->bloom_settings
,
1685 if (computed
& BLOOM_COMPUTED
) {
1686 ctx
->count_bloom_filter_computed
++;
1687 if (computed
& BLOOM_TRUNC_EMPTY
)
1688 ctx
->count_bloom_filter_trunc_empty
++;
1689 if (computed
& BLOOM_TRUNC_LARGE
)
1690 ctx
->count_bloom_filter_trunc_large
++;
1691 } else if (computed
& BLOOM_NOT_COMPUTED
)
1692 ctx
->count_bloom_filter_not_computed
++;
1693 ctx
->total_bloom_filter_data_size
+= filter
1694 ? sizeof(unsigned char) * filter
->len
: 0;
1695 display_progress(progress
, i
+ 1);
1698 if (trace2_is_enabled())
1699 trace2_bloom_filter_write_statistics(ctx
);
1701 free(sorted_commits
);
1702 stop_progress(&progress
);
1705 struct refs_cb_data
{
1706 struct oidset
*commits
;
1707 struct progress
*progress
;
1710 static int add_ref_to_set(const char *refname UNUSED
,
1711 const struct object_id
*oid
,
1712 int flags UNUSED
, void *cb_data
)
1714 struct object_id peeled
;
1715 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1717 if (!peel_iterated_oid(oid
, &peeled
))
1719 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1720 oidset_insert(data
->commits
, oid
);
1722 display_progress(data
->progress
, oidset_size(data
->commits
));
1727 int write_commit_graph_reachable(struct object_directory
*odb
,
1728 enum commit_graph_write_flags flags
,
1729 const struct commit_graph_opts
*opts
)
1731 struct oidset commits
= OIDSET_INIT
;
1732 struct refs_cb_data data
;
1735 memset(&data
, 0, sizeof(data
));
1736 data
.commits
= &commits
;
1737 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1738 data
.progress
= start_delayed_progress(
1739 _("Collecting referenced commits"), 0);
1741 for_each_ref(add_ref_to_set
, &data
);
1743 stop_progress(&data
.progress
);
1745 result
= write_commit_graph(odb
, NULL
, &commits
,
1748 oidset_clear(&commits
);
1752 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1753 const struct string_list
*pack_indexes
)
1756 struct strbuf progress_title
= STRBUF_INIT
;
1757 struct strbuf packname
= STRBUF_INIT
;
1761 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1762 dirlen
= packname
.len
;
1763 if (ctx
->report_progress
) {
1764 strbuf_addf(&progress_title
,
1765 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1766 "Finding commits for commit graph in %"PRIuMAX
" packs",
1768 (uintmax_t)pack_indexes
->nr
);
1769 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1770 ctx
->progress_done
= 0;
1772 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1773 struct packed_git
*p
;
1774 strbuf_setlen(&packname
, dirlen
);
1775 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1776 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1778 ret
= error(_("error adding pack %s"), packname
.buf
);
1781 if (open_pack_index(p
)) {
1782 ret
= error(_("error opening index for %s"), packname
.buf
);
1785 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1786 FOR_EACH_OBJECT_PACK_ORDER
);
1792 stop_progress(&ctx
->progress
);
1793 strbuf_release(&progress_title
);
1794 strbuf_release(&packname
);
1799 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1800 struct oidset
*commits
)
1802 struct oidset_iter iter
;
1803 struct object_id
*oid
;
1805 if (!oidset_size(commits
))
1808 oidset_iter_init(commits
, &iter
);
1809 while ((oid
= oidset_iter_next(&iter
))) {
1810 oid_array_append(&ctx
->oids
, oid
);
1816 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1818 if (ctx
->report_progress
)
1819 ctx
->progress
= start_delayed_progress(
1820 _("Finding commits for commit graph among packed objects"),
1821 ctx
->approx_nr_objects
);
1822 for_each_packed_object(add_packed_commits
, ctx
,
1823 FOR_EACH_OBJECT_PACK_ORDER
);
1824 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1825 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1826 stop_progress(&ctx
->progress
);
1829 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1832 enum commit_graph_split_flags flags
= ctx
->opts
?
1833 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1835 ctx
->num_extra_edges
= 0;
1836 if (ctx
->report_progress
)
1837 ctx
->progress
= start_delayed_progress(
1838 _("Finding extra edges in commit graph"),
1840 oid_array_sort(&ctx
->oids
);
1841 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1842 unsigned int num_parents
;
1844 display_progress(ctx
->progress
, i
+ 1);
1846 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1847 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1849 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1850 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1853 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1854 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1856 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1858 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1859 if (num_parents
> 2)
1860 ctx
->num_extra_edges
+= num_parents
- 1;
1864 stop_progress(&ctx
->progress
);
1867 static int write_graph_chunk_base_1(struct hashfile
*f
,
1868 struct commit_graph
*g
)
1875 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1876 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1880 static int write_graph_chunk_base(struct hashfile
*f
,
1883 struct write_commit_graph_context
*ctx
= data
;
1884 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1886 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1887 error(_("failed to write correct number of base graph ids"));
1894 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1899 struct lock_file lk
= LOCK_INIT
;
1900 const unsigned hashsz
= the_hash_algo
->rawsz
;
1901 struct strbuf progress_title
= STRBUF_INIT
;
1902 struct chunkfile
*cf
;
1903 unsigned char file_hash
[GIT_MAX_RAWSZ
];
1906 struct strbuf tmp_file
= STRBUF_INIT
;
1908 strbuf_addf(&tmp_file
,
1909 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1911 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1913 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1916 if (safe_create_leading_directories(ctx
->graph_name
)) {
1917 UNLEAK(ctx
->graph_name
);
1918 error(_("unable to create leading directories of %s"),
1924 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1926 hold_lock_file_for_update_mode(&lk
, lock_name
,
1927 LOCK_DIE_ON_ERROR
, 0444);
1930 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1932 error(_("unable to create temporary graph layer"));
1936 if (adjust_shared_perm(ctx
->graph_name
)) {
1937 error(_("unable to adjust shared permissions for '%s'"),
1942 f
= hashfd(fd
, ctx
->graph_name
);
1944 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1945 LOCK_DIE_ON_ERROR
, 0444);
1946 fd
= get_lock_file_fd(&lk
);
1947 f
= hashfd(fd
, get_lock_file_path(&lk
));
1950 cf
= init_chunkfile(f
);
1952 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
1953 write_graph_chunk_fanout
);
1954 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, hashsz
* ctx
->commits
.nr
,
1955 write_graph_chunk_oids
);
1956 add_chunk(cf
, GRAPH_CHUNKID_DATA
, (hashsz
+ 16) * ctx
->commits
.nr
,
1957 write_graph_chunk_data
);
1959 if (ctx
->write_generation_data
)
1960 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
1961 sizeof(uint32_t) * ctx
->commits
.nr
,
1962 write_graph_chunk_generation_data
);
1963 if (ctx
->num_generation_data_overflows
)
1964 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
1965 sizeof(timestamp_t
) * ctx
->num_generation_data_overflows
,
1966 write_graph_chunk_generation_data_overflow
);
1967 if (ctx
->num_extra_edges
)
1968 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
1969 4 * ctx
->num_extra_edges
,
1970 write_graph_chunk_extra_edges
);
1971 if (ctx
->changed_paths
) {
1972 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
1973 sizeof(uint32_t) * ctx
->commits
.nr
,
1974 write_graph_chunk_bloom_indexes
);
1975 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
1976 sizeof(uint32_t) * 3
1977 + ctx
->total_bloom_filter_data_size
,
1978 write_graph_chunk_bloom_data
);
1980 if (ctx
->num_commit_graphs_after
> 1)
1981 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
1982 hashsz
* (ctx
->num_commit_graphs_after
- 1),
1983 write_graph_chunk_base
);
1985 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1987 hashwrite_u8(f
, GRAPH_VERSION
);
1988 hashwrite_u8(f
, oid_version(the_hash_algo
));
1989 hashwrite_u8(f
, get_num_chunks(cf
));
1990 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1992 if (ctx
->report_progress
) {
1993 strbuf_addf(&progress_title
,
1994 Q_("Writing out commit graph in %d pass",
1995 "Writing out commit graph in %d passes",
1996 get_num_chunks(cf
)),
1997 get_num_chunks(cf
));
1998 ctx
->progress
= start_delayed_progress(
2000 get_num_chunks(cf
) * ctx
->commits
.nr
);
2003 write_chunkfile(cf
, ctx
);
2005 stop_progress(&ctx
->progress
);
2006 strbuf_release(&progress_title
);
2008 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
2009 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
2010 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
2012 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
2013 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
2014 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
2015 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
2018 close_commit_graph(ctx
->r
->objects
);
2019 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
2020 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
2024 FILE *chainf
= fdopen_lock_file(&lk
, "w");
2025 char *final_graph_name
;
2031 error(_("unable to open commit-graph chain file"));
2035 if (ctx
->base_graph_name
) {
2037 int idx
= ctx
->num_commit_graphs_after
- 1;
2038 if (ctx
->num_commit_graphs_after
> 1)
2041 dest
= ctx
->commit_graph_filenames_after
[idx
];
2043 if (strcmp(ctx
->base_graph_name
, dest
)) {
2044 result
= rename(ctx
->base_graph_name
, dest
);
2047 error(_("failed to rename base commit-graph file"));
2052 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
2057 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
2058 final_graph_name
= get_split_graph_filename(ctx
->odb
,
2059 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
2060 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
2062 result
= rename(ctx
->graph_name
, final_graph_name
);
2064 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
2065 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
2068 error(_("failed to rename temporary commit-graph file"));
2073 commit_lock_file(&lk
);
2078 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2080 struct commit_graph
*g
;
2081 uint32_t num_commits
;
2082 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2085 int max_commits
= 0;
2089 max_commits
= ctx
->opts
->max_commits
;
2091 if (ctx
->opts
->size_multiple
)
2092 size_mult
= ctx
->opts
->size_multiple
;
2094 flags
= ctx
->opts
->split_flags
;
2097 g
= ctx
->r
->objects
->commit_graph
;
2098 num_commits
= ctx
->commits
.nr
;
2099 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2100 ctx
->num_commit_graphs_after
= 1;
2102 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2104 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2105 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2106 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
2107 (max_commits
&& num_commits
> max_commits
))) {
2108 if (g
->odb
!= ctx
->odb
)
2111 num_commits
+= g
->num_commits
;
2114 ctx
->num_commit_graphs_after
--;
2118 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2119 ctx
->new_base_graph
= g
;
2120 else if (ctx
->num_commit_graphs_after
!= 1)
2121 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2122 "should be 1 with --split=replace");
2124 if (ctx
->num_commit_graphs_after
== 2) {
2125 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2127 if (!strcmp(g
->filename
, old_graph_name
) &&
2128 g
->odb
!= ctx
->odb
) {
2129 ctx
->num_commit_graphs_after
= 1;
2130 ctx
->new_base_graph
= NULL
;
2133 free(old_graph_name
);
2136 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2137 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2139 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2140 i
< ctx
->num_commit_graphs_before
; i
++)
2141 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2143 i
= ctx
->num_commit_graphs_before
- 1;
2144 g
= ctx
->r
->objects
->commit_graph
;
2147 if (i
< ctx
->num_commit_graphs_after
)
2148 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2151 * If the topmost remaining layer has generation data chunk, the
2152 * resultant layer also has generation data chunk.
2154 if (i
== ctx
->num_commit_graphs_after
- 2)
2155 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2162 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2163 struct commit_graph
*g
)
2166 uint32_t offset
= g
->num_commits_in_base
;
2168 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2170 for (i
= 0; i
< g
->num_commits
; i
++) {
2171 struct object_id oid
;
2172 struct commit
*result
;
2174 display_progress(ctx
->progress
, i
+ 1);
2176 load_oid_from_graph(g
, i
+ offset
, &oid
);
2178 /* only add commits if they still exist in the repo */
2179 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2182 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2188 static int commit_compare(const void *_a
, const void *_b
)
2190 const struct commit
*a
= *(const struct commit
**)_a
;
2191 const struct commit
*b
= *(const struct commit
**)_b
;
2192 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2195 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2197 uint32_t i
, dedup_i
= 0;
2199 if (ctx
->report_progress
)
2200 ctx
->progress
= start_delayed_progress(
2201 _("Scanning merged commits"),
2204 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2206 ctx
->num_extra_edges
= 0;
2207 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2208 display_progress(ctx
->progress
, i
+ 1);
2210 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2211 &ctx
->commits
.list
[i
]->object
.oid
)) {
2213 * Silently ignore duplicates. These were likely
2214 * created due to a commit appearing in multiple
2215 * layers of the chain, which is unexpected but
2216 * not invalid. We should make sure there is a
2217 * unique copy in the new layer.
2220 unsigned int num_parents
;
2222 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2225 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2226 if (num_parents
> 2)
2227 ctx
->num_extra_edges
+= num_parents
- 1;
2231 ctx
->commits
.nr
= dedup_i
;
2233 stop_progress(&ctx
->progress
);
2236 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2238 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2239 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2241 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2242 current_graph_number
--;
2244 if (ctx
->report_progress
)
2245 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2247 merge_commit_graph(ctx
, g
);
2248 stop_progress(&ctx
->progress
);
2254 ctx
->new_base_graph
= g
;
2255 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2258 if (ctx
->new_base_graph
)
2259 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2261 sort_and_scan_merged_commits(ctx
);
2264 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2267 time_t now
= time(NULL
);
2269 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2271 struct utimbuf updated_time
;
2273 if (stat(ctx
->commit_graph_filenames_before
[i
], &st
) < 0)
2276 updated_time
.actime
= st
.st_atime
;
2277 updated_time
.modtime
= now
;
2278 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2282 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2284 struct strbuf path
= STRBUF_INIT
;
2288 timestamp_t expire_time
= time(NULL
);
2290 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2291 expire_time
= ctx
->opts
->expire_time
;
2293 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2294 unlink(chain_file_name
);
2295 free(chain_file_name
);
2296 ctx
->num_commit_graphs_after
= 0;
2299 strbuf_addstr(&path
, ctx
->odb
->path
);
2300 strbuf_addstr(&path
, "/info/commit-graphs");
2301 dir
= opendir(path
.buf
);
2306 strbuf_addch(&path
, '/');
2307 dirnamelen
= path
.len
;
2308 while ((de
= readdir(dir
)) != NULL
) {
2310 uint32_t i
, found
= 0;
2312 strbuf_setlen(&path
, dirnamelen
);
2313 strbuf_addstr(&path
, de
->d_name
);
2315 if (stat(path
.buf
, &st
) < 0)
2318 if (st
.st_mtime
> expire_time
)
2320 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2323 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2324 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2338 strbuf_release(&path
);
2341 int write_commit_graph(struct object_directory
*odb
,
2342 const struct string_list
*const pack_indexes
,
2343 struct oidset
*commits
,
2344 enum commit_graph_write_flags flags
,
2345 const struct commit_graph_opts
*opts
)
2347 struct repository
*r
= the_repository
;
2348 struct write_commit_graph_context
*ctx
;
2352 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2353 struct topo_level_slab topo_levels
;
2355 prepare_repo_settings(r
);
2356 if (!r
->settings
.core_commit_graph
) {
2357 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2360 if (!commit_graph_compatible(r
))
2363 CALLOC_ARRAY(ctx
, 1);
2366 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2367 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2368 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2370 ctx
->total_bloom_filter_data_size
= 0;
2371 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2372 ctx
->num_generation_data_overflows
= 0;
2374 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2375 bloom_settings
.bits_per_entry
);
2376 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2377 bloom_settings
.num_hashes
);
2378 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2379 bloom_settings
.max_changed_paths
);
2380 ctx
->bloom_settings
= &bloom_settings
;
2382 init_topo_level_slab(&topo_levels
);
2383 ctx
->topo_levels
= &topo_levels
;
2385 prepare_commit_graph(ctx
->r
);
2386 if (ctx
->r
->objects
->commit_graph
) {
2387 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2390 g
->topo_levels
= &topo_levels
;
2395 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2396 ctx
->changed_paths
= 1;
2397 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2398 struct commit_graph
*g
;
2400 g
= ctx
->r
->objects
->commit_graph
;
2402 /* We have changed-paths already. Keep them in the next graph */
2403 if (g
&& g
->chunk_bloom_data
) {
2404 ctx
->changed_paths
= 1;
2405 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2410 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2413 ctx
->num_commit_graphs_before
++;
2417 if (ctx
->num_commit_graphs_before
) {
2418 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2419 i
= ctx
->num_commit_graphs_before
;
2420 g
= ctx
->r
->objects
->commit_graph
;
2423 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2429 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2432 ctx
->approx_nr_objects
= repo_approximate_object_count(the_repository
);
2434 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2435 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2436 for (i
= 0; i
< g
->num_commits
; i
++) {
2437 struct object_id oid
;
2438 oidread(&oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2439 oid_array_append(&ctx
->oids
, &oid
);
2444 ctx
->order_by_pack
= 1;
2445 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2450 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2454 if (!pack_indexes
&& !commits
) {
2455 ctx
->order_by_pack
= 1;
2456 fill_oids_from_all_packs(ctx
);
2459 close_reachable(ctx
);
2461 copy_oids_to_commits(ctx
);
2463 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2464 error(_("too many commits to write graph"));
2469 if (!ctx
->commits
.nr
&& !replace
)
2473 split_graph_merge_strategy(ctx
);
2476 merge_commit_graphs(ctx
);
2478 ctx
->num_commit_graphs_after
= 1;
2480 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2482 compute_topological_levels(ctx
);
2483 if (ctx
->write_generation_data
)
2484 compute_generation_numbers(ctx
);
2486 if (ctx
->changed_paths
)
2487 compute_bloom_filters(ctx
);
2489 res
= write_commit_graph_file(ctx
);
2492 mark_commit_graphs(ctx
);
2494 expire_commit_graphs(ctx
);
2497 free(ctx
->graph_name
);
2498 free(ctx
->commits
.list
);
2499 oid_array_clear(&ctx
->oids
);
2500 clear_topo_level_slab(&topo_levels
);
2502 if (ctx
->commit_graph_filenames_after
) {
2503 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2504 free(ctx
->commit_graph_filenames_after
[i
]);
2505 free(ctx
->commit_graph_hash_after
[i
]);
2508 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2509 free(ctx
->commit_graph_filenames_before
[i
]);
2511 free(ctx
->commit_graph_filenames_after
);
2512 free(ctx
->commit_graph_filenames_before
);
2513 free(ctx
->commit_graph_hash_after
);
2521 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2522 static int verify_commit_graph_error
;
2524 __attribute__((format (printf
, 1, 2)))
2525 static void graph_report(const char *fmt
, ...)
2529 verify_commit_graph_error
= 1;
2531 vfprintf(stderr
, fmt
, ap
);
2532 fprintf(stderr
, "\n");
2536 #define GENERATION_ZERO_EXISTS 1
2537 #define GENERATION_NUMBER_EXISTS 2
2539 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2541 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2544 static int verify_one_commit_graph(struct repository
*r
,
2545 struct commit_graph
*g
,
2546 struct progress
*progress
,
2549 uint32_t i
, cur_fanout_pos
= 0;
2550 struct object_id prev_oid
, cur_oid
;
2551 int generation_zero
= 0;
2553 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2554 if (verify_commit_graph_error
)
2555 return verify_commit_graph_error
;
2557 if (!commit_graph_checksum_valid(g
)) {
2558 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2559 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2562 for (i
= 0; i
< g
->num_commits
; i
++) {
2563 struct commit
*graph_commit
;
2565 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2567 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2568 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2569 oid_to_hex(&prev_oid
),
2570 oid_to_hex(&cur_oid
));
2572 oidcpy(&prev_oid
, &cur_oid
);
2574 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2575 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2577 if (i
!= fanout_value
)
2578 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2579 cur_fanout_pos
, fanout_value
, i
);
2583 graph_commit
= lookup_commit(r
, &cur_oid
);
2584 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2585 graph_report(_("failed to parse commit %s from commit-graph"),
2586 oid_to_hex(&cur_oid
));
2589 while (cur_fanout_pos
< 256) {
2590 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2592 if (g
->num_commits
!= fanout_value
)
2593 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2594 cur_fanout_pos
, fanout_value
, i
);
2599 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2600 return verify_commit_graph_error
;
2602 for (i
= 0; i
< g
->num_commits
; i
++) {
2603 struct commit
*graph_commit
, *odb_commit
;
2604 struct commit_list
*graph_parents
, *odb_parents
;
2605 timestamp_t max_generation
= 0;
2606 timestamp_t generation
;
2608 display_progress(progress
, ++(*seen
));
2609 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2611 graph_commit
= lookup_commit(r
, &cur_oid
);
2612 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2613 if (repo_parse_commit_internal(r
, odb_commit
, 0, 0)) {
2614 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2615 oid_to_hex(&cur_oid
));
2619 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2620 get_commit_tree_oid(odb_commit
)))
2621 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2622 oid_to_hex(&cur_oid
),
2623 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2624 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2626 graph_parents
= graph_commit
->parents
;
2627 odb_parents
= odb_commit
->parents
;
2629 while (graph_parents
) {
2631 graph_report(_("commit-graph parent list for commit %s is too long"),
2632 oid_to_hex(&cur_oid
));
2636 /* parse parent in case it is in a base graph */
2637 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2639 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2640 graph_report(_("commit-graph parent for %s is %s != %s"),
2641 oid_to_hex(&cur_oid
),
2642 oid_to_hex(&graph_parents
->item
->object
.oid
),
2643 oid_to_hex(&odb_parents
->item
->object
.oid
));
2645 generation
= commit_graph_generation(graph_parents
->item
);
2646 if (generation
> max_generation
)
2647 max_generation
= generation
;
2649 graph_parents
= graph_parents
->next
;
2650 odb_parents
= odb_parents
->next
;
2654 graph_report(_("commit-graph parent list for commit %s terminates early"),
2655 oid_to_hex(&cur_oid
));
2657 if (!commit_graph_generation(graph_commit
)) {
2658 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2659 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2660 oid_to_hex(&cur_oid
));
2661 generation_zero
= GENERATION_ZERO_EXISTS
;
2662 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2663 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2664 oid_to_hex(&cur_oid
));
2666 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2670 * If we are using topological level and one of our parents has
2671 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2672 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2673 * in the following condition.
2675 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2678 generation
= commit_graph_generation(graph_commit
);
2679 if (generation
< max_generation
+ 1)
2680 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2681 oid_to_hex(&cur_oid
),
2683 max_generation
+ 1);
2685 if (graph_commit
->date
!= odb_commit
->date
)
2686 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2687 oid_to_hex(&cur_oid
),
2692 return verify_commit_graph_error
;
2695 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2697 struct progress
*progress
= NULL
;
2698 int local_error
= 0;
2702 graph_report("no commit-graph file loaded");
2706 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
) {
2707 uint64_t total
= g
->num_commits
;
2708 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
))
2709 total
+= g
->num_commits_in_base
;
2711 progress
= start_progress(_("Verifying commits in commit graph"),
2715 for (; g
; g
= g
->base_graph
) {
2716 local_error
|= verify_one_commit_graph(r
, g
, progress
, &seen
);
2717 if (flags
& COMMIT_GRAPH_VERIFY_SHALLOW
)
2721 stop_progress(&progress
);
2726 void free_commit_graph(struct commit_graph
*g
)
2731 munmap((void *)g
->data
, g
->data_len
);
2735 free(g
->bloom_filter_settings
);
2739 void disable_commit_graph(struct repository
*r
)
2741 r
->commit_graph_disabled
= 1;