1 #include "git-compat-util.h"
10 #include "hash-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
15 #include "replace-object.h"
18 #include "commit-slab.h"
20 #include "json-writer.h"
22 #include "chunk-format.h"
24 void git_test_write_commit_graph_or_die(void)
27 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0))
30 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
31 flags
= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
33 if (write_commit_graph_reachable(the_repository
->objects
->odb
,
35 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
38 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
39 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
40 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
41 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
42 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
43 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
44 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
45 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
46 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
47 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
49 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
51 #define GRAPH_VERSION_1 0x1
52 #define GRAPH_VERSION GRAPH_VERSION_1
54 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
55 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
56 #define GRAPH_PARENT_NONE 0x70000000
58 #define GRAPH_LAST_EDGE 0x80000000
60 #define GRAPH_HEADER_SIZE 8
61 #define GRAPH_FANOUT_SIZE (4 * 256)
62 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
63 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
65 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
67 /* Remember to update object flag allocation in object.h */
68 #define REACHABLE (1u<<15)
70 define_commit_slab(topo_level_slab
, uint32_t);
72 /* Keep track of the order in which commits are added to our list. */
73 define_commit_slab(commit_pos
, int);
74 static struct commit_pos commit_pos
= COMMIT_SLAB_INIT(1, commit_pos
);
76 static void set_commit_pos(struct repository
*r
, const struct object_id
*oid
)
78 static int32_t max_pos
;
79 struct commit
*commit
= lookup_commit(r
, oid
);
82 return; /* should never happen, but be lenient */
84 *commit_pos_at(&commit_pos
, commit
) = max_pos
++;
87 static int commit_pos_cmp(const void *va
, const void *vb
)
89 const struct commit
*a
= *(const struct commit
**)va
;
90 const struct commit
*b
= *(const struct commit
**)vb
;
91 return commit_pos_at(&commit_pos
, a
) -
92 commit_pos_at(&commit_pos
, b
);
95 define_commit_slab(commit_graph_data_slab
, struct commit_graph_data
);
96 static struct commit_graph_data_slab commit_graph_data_slab
=
97 COMMIT_SLAB_INIT(1, commit_graph_data_slab
);
99 static int get_configured_generation_version(struct repository
*r
)
102 repo_config_get_int(r
, "commitgraph.generationversion", &version
);
106 uint32_t commit_graph_position(const struct commit
*c
)
108 struct commit_graph_data
*data
=
109 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
111 return data
? data
->graph_pos
: COMMIT_NOT_FROM_GRAPH
;
114 timestamp_t
commit_graph_generation(const struct commit
*c
)
116 struct commit_graph_data
*data
=
117 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
120 return GENERATION_NUMBER_INFINITY
;
121 else if (data
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
122 return GENERATION_NUMBER_INFINITY
;
124 return data
->generation
;
127 static struct commit_graph_data
*commit_graph_data_at(const struct commit
*c
)
129 unsigned int i
, nth_slab
;
130 struct commit_graph_data
*data
=
131 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
136 nth_slab
= c
->index
/ commit_graph_data_slab
.slab_size
;
137 data
= commit_graph_data_slab_at(&commit_graph_data_slab
, c
);
140 * commit-slab initializes elements with zero, overwrite this with
141 * COMMIT_NOT_FROM_GRAPH for graph_pos.
143 * We avoid initializing generation with checking if graph position
144 * is not COMMIT_NOT_FROM_GRAPH.
146 for (i
= 0; i
< commit_graph_data_slab
.slab_size
; i
++) {
147 commit_graph_data_slab
.slab
[nth_slab
][i
].graph_pos
=
148 COMMIT_NOT_FROM_GRAPH
;
155 * Should be used only while writing commit-graph as it compares
156 * generation value of commits by directly accessing commit-slab.
158 static int commit_gen_cmp(const void *va
, const void *vb
)
160 const struct commit
*a
= *(const struct commit
**)va
;
161 const struct commit
*b
= *(const struct commit
**)vb
;
163 const timestamp_t generation_a
= commit_graph_data_at(a
)->generation
;
164 const timestamp_t generation_b
= commit_graph_data_at(b
)->generation
;
165 /* lower generation commits first */
166 if (generation_a
< generation_b
)
168 else if (generation_a
> generation_b
)
171 /* use date as a heuristic when generations are equal */
172 if (a
->date
< b
->date
)
174 else if (a
->date
> b
->date
)
179 char *get_commit_graph_filename(struct object_directory
*obj_dir
)
181 return xstrfmt("%s/info/commit-graph", obj_dir
->path
);
184 static char *get_split_graph_filename(struct object_directory
*odb
,
187 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb
->path
,
191 char *get_commit_graph_chain_filename(struct object_directory
*odb
)
193 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb
->path
);
196 static uint8_t oid_version(void)
198 switch (hash_algo_by_ptr(the_hash_algo
)) {
201 case GIT_HASH_SHA256
:
204 die(_("invalid hash version"));
208 static struct commit_graph
*alloc_commit_graph(void)
210 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
215 extern int read_replace_refs
;
217 static int commit_graph_compatible(struct repository
*r
)
222 if (read_replace_refs
) {
223 prepare_replace_object(r
);
224 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
228 prepare_commit_graft(r
);
229 if (r
->parsed_objects
&&
230 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
232 if (is_repository_shallow(r
))
238 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
240 *fd
= git_open(graph_file
);
243 if (fstat(*fd
, st
)) {
250 struct commit_graph
*load_commit_graph_one_fd_st(struct repository
*r
,
251 int fd
, struct stat
*st
,
252 struct object_directory
*odb
)
256 struct commit_graph
*ret
;
258 graph_size
= xsize_t(st
->st_size
);
260 if (graph_size
< GRAPH_MIN_SIZE
) {
262 error(_("commit-graph file is too small"));
265 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
267 ret
= parse_commit_graph(r
, graph_map
, graph_size
);
272 munmap(graph_map
, graph_size
);
277 static int verify_commit_graph_lite(struct commit_graph
*g
)
280 * Basic validation shared between parse_commit_graph()
281 * which'll be called every time the graph is used, and the
282 * much more expensive verify_commit_graph() used by
283 * "commit-graph verify".
285 * There should only be very basic checks here to ensure that
286 * we don't e.g. segfault in fill_commit_in_graph(), but
287 * because this is a very hot codepath nothing that e.g. loops
288 * over g->num_commits, or runs a checksum on the commit-graph
291 if (!g
->chunk_oid_fanout
) {
292 error("commit-graph is missing the OID Fanout chunk");
295 if (!g
->chunk_oid_lookup
) {
296 error("commit-graph is missing the OID Lookup chunk");
299 if (!g
->chunk_commit_data
) {
300 error("commit-graph is missing the Commit Data chunk");
307 static int graph_read_oid_lookup(const unsigned char *chunk_start
,
308 size_t chunk_size
, void *data
)
310 struct commit_graph
*g
= data
;
311 g
->chunk_oid_lookup
= chunk_start
;
312 g
->num_commits
= chunk_size
/ g
->hash_len
;
316 static int graph_read_bloom_data(const unsigned char *chunk_start
,
317 size_t chunk_size
, void *data
)
319 struct commit_graph
*g
= data
;
320 uint32_t hash_version
;
321 g
->chunk_bloom_data
= chunk_start
;
322 hash_version
= get_be32(chunk_start
);
324 if (hash_version
!= 1)
327 g
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
328 g
->bloom_filter_settings
->hash_version
= hash_version
;
329 g
->bloom_filter_settings
->num_hashes
= get_be32(chunk_start
+ 4);
330 g
->bloom_filter_settings
->bits_per_entry
= get_be32(chunk_start
+ 8);
331 g
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
336 struct commit_graph
*parse_commit_graph(struct repository
*r
,
337 void *graph_map
, size_t graph_size
)
339 const unsigned char *data
;
340 struct commit_graph
*graph
;
341 uint32_t graph_signature
;
342 unsigned char graph_version
, hash_version
;
343 struct chunkfile
*cf
= NULL
;
348 if (graph_size
< GRAPH_MIN_SIZE
)
351 data
= (const unsigned char *)graph_map
;
353 graph_signature
= get_be32(data
);
354 if (graph_signature
!= GRAPH_SIGNATURE
) {
355 error(_("commit-graph signature %X does not match signature %X"),
356 graph_signature
, GRAPH_SIGNATURE
);
360 graph_version
= *(unsigned char*)(data
+ 4);
361 if (graph_version
!= GRAPH_VERSION
) {
362 error(_("commit-graph version %X does not match version %X"),
363 graph_version
, GRAPH_VERSION
);
367 hash_version
= *(unsigned char*)(data
+ 5);
368 if (hash_version
!= oid_version()) {
369 error(_("commit-graph hash version %X does not match version %X"),
370 hash_version
, oid_version());
374 prepare_repo_settings(r
);
376 graph
= alloc_commit_graph();
378 graph
->hash_len
= the_hash_algo
->rawsz
;
379 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
380 graph
->data
= graph_map
;
381 graph
->data_len
= graph_size
;
383 if (graph_size
< GRAPH_HEADER_SIZE
+
384 (graph
->num_chunks
+ 1) * CHUNK_TOC_ENTRY_SIZE
+
385 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
386 error(_("commit-graph file is too small to hold %u chunks"),
392 cf
= init_chunkfile(NULL
);
394 if (read_table_of_contents(cf
, graph
->data
, graph_size
,
395 GRAPH_HEADER_SIZE
, graph
->num_chunks
))
396 goto free_and_return
;
398 pair_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
,
399 (const unsigned char **)&graph
->chunk_oid_fanout
);
400 read_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, graph_read_oid_lookup
, graph
);
401 pair_chunk(cf
, GRAPH_CHUNKID_DATA
, &graph
->chunk_commit_data
);
402 pair_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
, &graph
->chunk_extra_edges
);
403 pair_chunk(cf
, GRAPH_CHUNKID_BASE
, &graph
->chunk_base_graphs
);
405 if (get_configured_generation_version(r
) >= 2) {
406 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
407 &graph
->chunk_generation_data
);
408 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
409 &graph
->chunk_generation_data_overflow
);
411 if (graph
->chunk_generation_data
)
412 graph
->read_generation_data
= 1;
415 if (r
->settings
.commit_graph_read_changed_paths
) {
416 pair_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
417 &graph
->chunk_bloom_indexes
);
418 read_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
419 graph_read_bloom_data
, graph
);
422 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
423 init_bloom_filters();
425 /* We need both the bloom chunks to exist together. Else ignore the data */
426 graph
->chunk_bloom_indexes
= NULL
;
427 graph
->chunk_bloom_data
= NULL
;
428 FREE_AND_NULL(graph
->bloom_filter_settings
);
431 oidread(&graph
->oid
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
433 if (verify_commit_graph_lite(graph
))
434 goto free_and_return
;
441 free(graph
->bloom_filter_settings
);
446 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
447 const char *graph_file
,
448 struct object_directory
*odb
)
453 struct commit_graph
*g
;
454 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
459 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
462 g
->filename
= xstrdup(graph_file
);
467 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
468 struct object_directory
*odb
)
470 char *graph_name
= get_commit_graph_filename(odb
);
471 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
477 static int add_graph_to_chain(struct commit_graph
*g
,
478 struct commit_graph
*chain
,
479 struct object_id
*oids
,
482 struct commit_graph
*cur_g
= chain
;
484 if (n
&& !g
->chunk_base_graphs
) {
485 warning(_("commit-graph has no base graphs chunk"));
493 !oideq(&oids
[n
], &cur_g
->oid
) ||
494 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
495 warning(_("commit-graph chain does not match"));
499 cur_g
= cur_g
->base_graph
;
502 g
->base_graph
= chain
;
505 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
510 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
511 struct object_directory
*odb
)
513 struct commit_graph
*graph_chain
= NULL
;
514 struct strbuf line
= STRBUF_INIT
;
516 struct object_id
*oids
;
517 int i
= 0, valid
= 1, count
;
518 char *chain_name
= get_commit_graph_chain_filename(odb
);
522 fp
= fopen(chain_name
, "r");
523 stat_res
= stat(chain_name
, &st
);
529 st
.st_size
<= the_hash_algo
->hexsz
) {
534 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
535 CALLOC_ARRAY(oids
, count
);
539 for (i
= 0; i
< count
; i
++) {
540 struct object_directory
*odb
;
542 if (strbuf_getline_lf(&line
, fp
) == EOF
)
545 if (get_oid_hex(line
.buf
, &oids
[i
])) {
546 warning(_("invalid commit-graph chain: line '%s' not a hash"),
553 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
554 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
555 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
560 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
570 warning(_("unable to find all commit-graph files"));
577 strbuf_release(&line
);
583 * returns 1 if and only if all graphs in the chain have
584 * corrected commit dates stored in the generation_data chunk.
586 static int validate_mixed_generation_chain(struct commit_graph
*g
)
588 int read_generation_data
= 1;
589 struct commit_graph
*p
= g
;
591 while (read_generation_data
&& p
) {
592 read_generation_data
= p
->read_generation_data
;
596 if (read_generation_data
)
600 g
->read_generation_data
= 0;
607 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
608 struct object_directory
*odb
)
610 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
613 g
= load_commit_graph_chain(r
, odb
);
615 validate_mixed_generation_chain(g
);
620 static void prepare_commit_graph_one(struct repository
*r
,
621 struct object_directory
*odb
)
624 if (r
->objects
->commit_graph
)
627 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
631 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
633 * On the first invocation, this function attempts to load the commit
634 * graph if the_repository is configured to have one.
636 static int prepare_commit_graph(struct repository
*r
)
638 struct object_directory
*odb
;
641 * Early return if there is no git dir or if the commit graph is
644 * This must come before the "already attempted?" check below, because
645 * we want to disable even an already-loaded graph file.
647 if (!r
->gitdir
|| r
->commit_graph_disabled
)
650 if (r
->objects
->commit_graph_attempted
)
651 return !!r
->objects
->commit_graph
;
652 r
->objects
->commit_graph_attempted
= 1;
654 prepare_repo_settings(r
);
656 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
657 r
->settings
.core_commit_graph
!= 1)
659 * This repository is not configured to use commit graphs, so
660 * do not load one. (But report commit_graph_attempted anyway
661 * so that commit graph loading is not attempted again for this
666 if (!commit_graph_compatible(r
))
670 for (odb
= r
->objects
->odb
;
671 !r
->objects
->commit_graph
&& odb
;
673 prepare_commit_graph_one(r
, odb
);
674 return !!r
->objects
->commit_graph
;
677 int generation_numbers_enabled(struct repository
*r
)
679 uint32_t first_generation
;
680 struct commit_graph
*g
;
681 if (!prepare_commit_graph(r
))
684 g
= r
->objects
->commit_graph
;
689 first_generation
= get_be32(g
->chunk_commit_data
+
690 g
->hash_len
+ 8) >> 2;
692 return !!first_generation
;
695 int corrected_commit_dates_enabled(struct repository
*r
)
697 struct commit_graph
*g
;
698 if (!prepare_commit_graph(r
))
701 g
= r
->objects
->commit_graph
;
706 return g
->read_generation_data
;
709 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
711 struct commit_graph
*g
= r
->objects
->commit_graph
;
713 if (g
->bloom_filter_settings
)
714 return g
->bloom_filter_settings
;
720 static void close_commit_graph_one(struct commit_graph
*g
)
725 clear_commit_graph_data_slab(&commit_graph_data_slab
);
726 close_commit_graph_one(g
->base_graph
);
727 free_commit_graph(g
);
730 void close_commit_graph(struct raw_object_store
*o
)
732 close_commit_graph_one(o
->commit_graph
);
733 o
->commit_graph
= NULL
;
736 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
738 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
739 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
742 static void load_oid_from_graph(struct commit_graph
*g
,
744 struct object_id
*oid
)
748 while (g
&& pos
< g
->num_commits_in_base
)
752 BUG("NULL commit-graph");
754 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
755 die(_("invalid commit position. commit-graph is likely corrupt"));
757 lex_index
= pos
- g
->num_commits_in_base
;
759 oidread(oid
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
762 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
763 struct commit_graph
*g
,
765 struct commit_list
**pptr
)
768 struct object_id oid
;
770 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
771 die("invalid parent position %"PRIu32
, pos
);
773 load_oid_from_graph(g
, pos
, &oid
);
774 c
= lookup_commit(r
, &oid
);
776 die(_("could not find commit %s"), oid_to_hex(&oid
));
777 commit_graph_data_at(c
)->graph_pos
= pos
;
778 return &commit_list_insert(c
, pptr
)->next
;
781 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
783 const unsigned char *commit_data
;
784 struct commit_graph_data
*graph_data
;
785 uint32_t lex_index
, offset_pos
;
786 uint64_t date_high
, date_low
, offset
;
788 while (pos
< g
->num_commits_in_base
)
791 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
792 die(_("invalid commit position. commit-graph is likely corrupt"));
794 lex_index
= pos
- g
->num_commits_in_base
;
795 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
797 graph_data
= commit_graph_data_at(item
);
798 graph_data
->graph_pos
= pos
;
800 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
801 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
802 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
804 if (g
->read_generation_data
) {
805 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ sizeof(uint32_t) * lex_index
);
807 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
808 if (!g
->chunk_generation_data_overflow
)
809 die(_("commit-graph requires overflow generation data but has none"));
811 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
812 graph_data
->generation
= item
->date
+ get_be64(g
->chunk_generation_data_overflow
+ 8 * offset_pos
);
814 graph_data
->generation
= item
->date
+ offset
;
816 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
819 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
822 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
827 static int fill_commit_in_graph(struct repository
*r
,
829 struct commit_graph
*g
, uint32_t pos
)
832 uint32_t *parent_data_ptr
;
833 struct commit_list
**pptr
;
834 const unsigned char *commit_data
;
837 while (pos
< g
->num_commits_in_base
)
840 fill_commit_graph_info(item
, g
, pos
);
842 lex_index
= pos
- g
->num_commits_in_base
;
843 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
845 item
->object
.parsed
= 1;
847 set_commit_tree(item
, NULL
);
849 pptr
= &item
->parents
;
851 edge_value
= get_be32(commit_data
+ g
->hash_len
);
852 if (edge_value
== GRAPH_PARENT_NONE
)
854 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
856 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
857 if (edge_value
== GRAPH_PARENT_NONE
)
859 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
860 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
864 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
865 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
867 edge_value
= get_be32(parent_data_ptr
);
868 pptr
= insert_parent_or_die(r
, g
,
869 edge_value
& GRAPH_EDGE_LAST_MASK
,
872 } while (!(edge_value
& GRAPH_LAST_EDGE
));
877 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
879 struct commit_graph
*cur_g
= g
;
882 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
883 cur_g
= cur_g
->base_graph
;
886 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
893 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
895 uint32_t graph_pos
= commit_graph_position(item
);
896 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
900 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
904 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
906 struct commit
*commit
;
909 if (!repo
->objects
->commit_graph
)
911 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
913 if (!repo_has_object_file(repo
, id
))
916 commit
= lookup_commit(repo
, id
);
919 if (commit
->object
.parsed
)
922 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
928 static int parse_commit_in_graph_one(struct repository
*r
,
929 struct commit_graph
*g
,
934 if (item
->object
.parsed
)
937 if (find_commit_pos_in_graph(item
, g
, &pos
))
938 return fill_commit_in_graph(r
, item
, g
, pos
);
943 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
945 static int checked_env
= 0;
948 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
949 die("dying as requested by the '%s' variable on commit-graph parse!",
950 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
953 if (!prepare_commit_graph(r
))
955 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
958 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
961 if (!prepare_commit_graph(r
))
963 if (find_commit_pos_in_graph(item
, r
->objects
->commit_graph
, &pos
))
964 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
967 static struct tree
*load_tree_for_commit(struct repository
*r
,
968 struct commit_graph
*g
,
971 struct object_id oid
;
972 const unsigned char *commit_data
;
973 uint32_t graph_pos
= commit_graph_position(c
);
975 while (graph_pos
< g
->num_commits_in_base
)
978 commit_data
= g
->chunk_commit_data
+
979 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
981 oidread(&oid
, commit_data
);
982 set_commit_tree(c
, lookup_tree(r
, &oid
));
984 return c
->maybe_tree
;
987 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
988 struct commit_graph
*g
,
989 const struct commit
*c
)
992 return c
->maybe_tree
;
993 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
994 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
996 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
999 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
1001 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
1004 struct packed_commit_list
{
1005 struct commit
**list
;
1010 struct write_commit_graph_context
{
1011 struct repository
*r
;
1012 struct object_directory
*odb
;
1014 struct oid_array oids
;
1015 struct packed_commit_list commits
;
1016 int num_extra_edges
;
1017 int num_generation_data_overflows
;
1018 unsigned long approx_nr_objects
;
1019 struct progress
*progress
;
1021 uint64_t progress_cnt
;
1023 char *base_graph_name
;
1024 int num_commit_graphs_before
;
1025 int num_commit_graphs_after
;
1026 char **commit_graph_filenames_before
;
1027 char **commit_graph_filenames_after
;
1028 char **commit_graph_hash_after
;
1029 uint32_t new_num_commits_in_base
;
1030 struct commit_graph
*new_base_graph
;
1037 write_generation_data
:1,
1038 trust_generation_numbers
:1;
1040 struct topo_level_slab
*topo_levels
;
1041 const struct commit_graph_opts
*opts
;
1042 size_t total_bloom_filter_data_size
;
1043 const struct bloom_filter_settings
*bloom_settings
;
1045 int count_bloom_filter_computed
;
1046 int count_bloom_filter_not_computed
;
1047 int count_bloom_filter_trunc_empty
;
1048 int count_bloom_filter_trunc_large
;
1051 static int write_graph_chunk_fanout(struct hashfile
*f
,
1054 struct write_commit_graph_context
*ctx
= data
;
1056 struct commit
**list
= ctx
->commits
.list
;
1059 * Write the first-level table (the list is sorted,
1060 * but we use a 256-entry lookup to be able to avoid
1061 * having to do eight extra binary search iterations).
1063 for (i
= 0; i
< 256; i
++) {
1064 while (count
< ctx
->commits
.nr
) {
1065 if ((*list
)->object
.oid
.hash
[0] != i
)
1067 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1072 hashwrite_be32(f
, count
);
1078 static int write_graph_chunk_oids(struct hashfile
*f
,
1081 struct write_commit_graph_context
*ctx
= data
;
1082 struct commit
**list
= ctx
->commits
.list
;
1084 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1085 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1086 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1092 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1094 const struct commit
* const *commits
= table
;
1095 return &commits
[index
]->object
.oid
;
1098 static int write_graph_chunk_data(struct hashfile
*f
,
1101 struct write_commit_graph_context
*ctx
= data
;
1102 struct commit
**list
= ctx
->commits
.list
;
1103 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1104 uint32_t num_extra_edges
= 0;
1106 while (list
< last
) {
1107 struct commit_list
*parent
;
1108 struct object_id
*tree
;
1110 uint32_t packedDate
[2];
1111 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1113 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1114 die(_("unable to parse commit %s"),
1115 oid_to_hex(&(*list
)->object
.oid
));
1116 tree
= get_commit_tree_oid(*list
);
1117 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1119 parent
= (*list
)->parents
;
1122 edge_value
= GRAPH_PARENT_NONE
;
1124 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1129 if (edge_value
>= 0)
1130 edge_value
+= ctx
->new_num_commits_in_base
;
1131 else if (ctx
->new_base_graph
) {
1133 if (find_commit_pos_in_graph(parent
->item
,
1134 ctx
->new_base_graph
,
1140 BUG("missing parent %s for commit %s",
1141 oid_to_hex(&parent
->item
->object
.oid
),
1142 oid_to_hex(&(*list
)->object
.oid
));
1145 hashwrite_be32(f
, edge_value
);
1148 parent
= parent
->next
;
1151 edge_value
= GRAPH_PARENT_NONE
;
1152 else if (parent
->next
)
1153 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1155 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1160 if (edge_value
>= 0)
1161 edge_value
+= ctx
->new_num_commits_in_base
;
1162 else if (ctx
->new_base_graph
) {
1164 if (find_commit_pos_in_graph(parent
->item
,
1165 ctx
->new_base_graph
,
1171 BUG("missing parent %s for commit %s",
1172 oid_to_hex(&parent
->item
->object
.oid
),
1173 oid_to_hex(&(*list
)->object
.oid
));
1176 hashwrite_be32(f
, edge_value
);
1178 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1181 parent
= parent
->next
;
1185 if (sizeof((*list
)->date
) > 4)
1186 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1190 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1192 packedDate
[1] = htonl((*list
)->date
);
1193 hashwrite(f
, packedDate
, 8);
1201 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1204 struct write_commit_graph_context
*ctx
= data
;
1205 int i
, num_generation_data_overflows
= 0;
1207 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1208 struct commit
*c
= ctx
->commits
.list
[i
];
1210 repo_parse_commit(ctx
->r
, c
);
1211 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1212 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1214 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1215 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1216 num_generation_data_overflows
++;
1219 hashwrite_be32(f
, offset
);
1225 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1228 struct write_commit_graph_context
*ctx
= data
;
1230 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1231 struct commit
*c
= ctx
->commits
.list
[i
];
1232 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1233 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1235 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1236 hashwrite_be32(f
, offset
>> 32);
1237 hashwrite_be32(f
, (uint32_t) offset
);
1244 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1247 struct write_commit_graph_context
*ctx
= data
;
1248 struct commit
**list
= ctx
->commits
.list
;
1249 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1250 struct commit_list
*parent
;
1252 while (list
< last
) {
1253 int num_parents
= 0;
1255 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1257 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1258 parent
= parent
->next
)
1261 if (num_parents
<= 2) {
1266 /* Since num_parents > 2, this initializer is safe. */
1267 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1268 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1273 if (edge_value
>= 0)
1274 edge_value
+= ctx
->new_num_commits_in_base
;
1275 else if (ctx
->new_base_graph
) {
1277 if (find_commit_pos_in_graph(parent
->item
,
1278 ctx
->new_base_graph
,
1284 BUG("missing parent %s for commit %s",
1285 oid_to_hex(&parent
->item
->object
.oid
),
1286 oid_to_hex(&(*list
)->object
.oid
));
1287 else if (!parent
->next
)
1288 edge_value
|= GRAPH_LAST_EDGE
;
1290 hashwrite_be32(f
, edge_value
);
1299 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1302 struct write_commit_graph_context
*ctx
= data
;
1303 struct commit
**list
= ctx
->commits
.list
;
1304 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1305 uint32_t cur_pos
= 0;
1307 while (list
< last
) {
1308 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1309 size_t len
= filter
? filter
->len
: 0;
1311 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1312 hashwrite_be32(f
, cur_pos
);
1319 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1321 struct json_writer jw
= JSON_WRITER_INIT
;
1323 jw_object_begin(&jw
, 0);
1324 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1325 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1326 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1327 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1330 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1335 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1338 struct write_commit_graph_context
*ctx
= data
;
1339 struct commit
**list
= ctx
->commits
.list
;
1340 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1342 trace2_bloom_filter_settings(ctx
);
1344 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1345 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1346 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1348 while (list
< last
) {
1349 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1350 size_t len
= filter
? filter
->len
: 0;
1352 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1354 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1361 static int add_packed_commits(const struct object_id
*oid
,
1362 struct packed_git
*pack
,
1366 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1367 enum object_type type
;
1368 off_t offset
= nth_packed_object_offset(pack
, pos
);
1369 struct object_info oi
= OBJECT_INFO_INIT
;
1372 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1375 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1376 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1378 if (type
!= OBJ_COMMIT
)
1381 oid_array_append(&ctx
->oids
, oid
);
1382 set_commit_pos(ctx
->r
, oid
);
1387 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1389 struct commit_list
*parent
;
1390 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1391 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1392 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1393 parent
->item
->object
.flags
|= REACHABLE
;
1398 static void close_reachable(struct write_commit_graph_context
*ctx
)
1401 struct commit
*commit
;
1402 enum commit_graph_split_flags flags
= ctx
->opts
?
1403 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1405 if (ctx
->report_progress
)
1406 ctx
->progress
= start_delayed_progress(
1407 _("Loading known commits in commit graph"),
1409 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1410 display_progress(ctx
->progress
, i
+ 1);
1411 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1413 commit
->object
.flags
|= REACHABLE
;
1415 stop_progress(&ctx
->progress
);
1418 * As this loop runs, ctx->oids.nr may grow, but not more
1419 * than the number of missing commits in the reachable
1422 if (ctx
->report_progress
)
1423 ctx
->progress
= start_delayed_progress(
1424 _("Expanding reachable commits in commit graph"),
1426 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1427 display_progress(ctx
->progress
, i
+ 1);
1428 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1433 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1434 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1435 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1436 add_missing_parents(ctx
, commit
);
1437 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1438 add_missing_parents(ctx
, commit
);
1440 stop_progress(&ctx
->progress
);
1442 if (ctx
->report_progress
)
1443 ctx
->progress
= start_delayed_progress(
1444 _("Clearing commit marks in commit graph"),
1446 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1447 display_progress(ctx
->progress
, i
+ 1);
1448 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1451 commit
->object
.flags
&= ~REACHABLE
;
1453 stop_progress(&ctx
->progress
);
1456 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1459 struct commit_list
*list
= NULL
;
1461 if (ctx
->report_progress
)
1462 ctx
->progress
= start_delayed_progress(
1463 _("Computing commit graph topological levels"),
1465 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1466 struct commit
*c
= ctx
->commits
.list
[i
];
1469 repo_parse_commit(ctx
->r
, c
);
1470 level
= *topo_level_slab_at(ctx
->topo_levels
, c
);
1472 display_progress(ctx
->progress
, i
+ 1);
1473 if (level
!= GENERATION_NUMBER_ZERO
)
1476 commit_list_insert(c
, &list
);
1478 struct commit
*current
= list
->item
;
1479 struct commit_list
*parent
;
1480 int all_parents_computed
= 1;
1481 uint32_t max_level
= 0;
1483 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1484 repo_parse_commit(ctx
->r
, parent
->item
);
1485 level
= *topo_level_slab_at(ctx
->topo_levels
, parent
->item
);
1487 if (level
== GENERATION_NUMBER_ZERO
) {
1488 all_parents_computed
= 0;
1489 commit_list_insert(parent
->item
, &list
);
1493 if (level
> max_level
)
1497 if (all_parents_computed
) {
1500 if (max_level
> GENERATION_NUMBER_V1_MAX
- 1)
1501 max_level
= GENERATION_NUMBER_V1_MAX
- 1;
1502 *topo_level_slab_at(ctx
->topo_levels
, current
) = max_level
+ 1;
1506 stop_progress(&ctx
->progress
);
1509 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1512 struct commit_list
*list
= NULL
;
1514 if (ctx
->report_progress
)
1515 ctx
->progress
= start_delayed_progress(
1516 _("Computing commit graph generation numbers"),
1519 if (!ctx
->trust_generation_numbers
) {
1520 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1521 struct commit
*c
= ctx
->commits
.list
[i
];
1522 repo_parse_commit(ctx
->r
, c
);
1523 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1527 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1528 struct commit
*c
= ctx
->commits
.list
[i
];
1529 timestamp_t corrected_commit_date
;
1531 repo_parse_commit(ctx
->r
, c
);
1532 corrected_commit_date
= commit_graph_data_at(c
)->generation
;
1534 display_progress(ctx
->progress
, i
+ 1);
1535 if (corrected_commit_date
!= GENERATION_NUMBER_ZERO
)
1538 commit_list_insert(c
, &list
);
1540 struct commit
*current
= list
->item
;
1541 struct commit_list
*parent
;
1542 int all_parents_computed
= 1;
1543 timestamp_t max_corrected_commit_date
= 0;
1545 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1546 repo_parse_commit(ctx
->r
, parent
->item
);
1547 corrected_commit_date
= commit_graph_data_at(parent
->item
)->generation
;
1549 if (corrected_commit_date
== GENERATION_NUMBER_ZERO
) {
1550 all_parents_computed
= 0;
1551 commit_list_insert(parent
->item
, &list
);
1555 if (corrected_commit_date
> max_corrected_commit_date
)
1556 max_corrected_commit_date
= corrected_commit_date
;
1559 if (all_parents_computed
) {
1562 if (current
->date
&& current
->date
> max_corrected_commit_date
)
1563 max_corrected_commit_date
= current
->date
- 1;
1564 commit_graph_data_at(current
)->generation
= max_corrected_commit_date
+ 1;
1569 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1570 struct commit
*c
= ctx
->commits
.list
[i
];
1571 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1572 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1573 ctx
->num_generation_data_overflows
++;
1575 stop_progress(&ctx
->progress
);
1578 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1580 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1581 ctx
->count_bloom_filter_computed
);
1582 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1583 ctx
->count_bloom_filter_not_computed
);
1584 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1585 ctx
->count_bloom_filter_trunc_empty
);
1586 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1587 ctx
->count_bloom_filter_trunc_large
);
1590 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1593 struct progress
*progress
= NULL
;
1594 struct commit
**sorted_commits
;
1595 int max_new_filters
;
1597 init_bloom_filters();
1599 if (ctx
->report_progress
)
1600 progress
= start_delayed_progress(
1601 _("Computing commit changed paths Bloom filters"),
1604 ALLOC_ARRAY(sorted_commits
, ctx
->commits
.nr
);
1605 COPY_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1607 if (ctx
->order_by_pack
)
1608 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1610 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1612 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1613 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1615 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1616 enum bloom_filter_computed computed
= 0;
1617 struct commit
*c
= sorted_commits
[i
];
1618 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1621 ctx
->count_bloom_filter_computed
< max_new_filters
,
1622 ctx
->bloom_settings
,
1624 if (computed
& BLOOM_COMPUTED
) {
1625 ctx
->count_bloom_filter_computed
++;
1626 if (computed
& BLOOM_TRUNC_EMPTY
)
1627 ctx
->count_bloom_filter_trunc_empty
++;
1628 if (computed
& BLOOM_TRUNC_LARGE
)
1629 ctx
->count_bloom_filter_trunc_large
++;
1630 } else if (computed
& BLOOM_NOT_COMPUTED
)
1631 ctx
->count_bloom_filter_not_computed
++;
1632 ctx
->total_bloom_filter_data_size
+= filter
1633 ? sizeof(unsigned char) * filter
->len
: 0;
1634 display_progress(progress
, i
+ 1);
1637 if (trace2_is_enabled())
1638 trace2_bloom_filter_write_statistics(ctx
);
1640 free(sorted_commits
);
1641 stop_progress(&progress
);
1644 struct refs_cb_data
{
1645 struct oidset
*commits
;
1646 struct progress
*progress
;
1649 static int add_ref_to_set(const char *refname
,
1650 const struct object_id
*oid
,
1651 int flags
, void *cb_data
)
1653 struct object_id peeled
;
1654 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1656 if (!peel_iterated_oid(oid
, &peeled
))
1658 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1659 oidset_insert(data
->commits
, oid
);
1661 display_progress(data
->progress
, oidset_size(data
->commits
));
1666 int write_commit_graph_reachable(struct object_directory
*odb
,
1667 enum commit_graph_write_flags flags
,
1668 const struct commit_graph_opts
*opts
)
1670 struct oidset commits
= OIDSET_INIT
;
1671 struct refs_cb_data data
;
1674 memset(&data
, 0, sizeof(data
));
1675 data
.commits
= &commits
;
1676 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1677 data
.progress
= start_delayed_progress(
1678 _("Collecting referenced commits"), 0);
1680 for_each_ref(add_ref_to_set
, &data
);
1682 stop_progress(&data
.progress
);
1684 result
= write_commit_graph(odb
, NULL
, &commits
,
1687 oidset_clear(&commits
);
1691 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1692 const struct string_list
*pack_indexes
)
1695 struct strbuf progress_title
= STRBUF_INIT
;
1696 struct strbuf packname
= STRBUF_INIT
;
1700 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1701 dirlen
= packname
.len
;
1702 if (ctx
->report_progress
) {
1703 strbuf_addf(&progress_title
,
1704 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1705 "Finding commits for commit graph in %"PRIuMAX
" packs",
1707 (uintmax_t)pack_indexes
->nr
);
1708 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1709 ctx
->progress_done
= 0;
1711 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1712 struct packed_git
*p
;
1713 strbuf_setlen(&packname
, dirlen
);
1714 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1715 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1717 ret
= error(_("error adding pack %s"), packname
.buf
);
1720 if (open_pack_index(p
)) {
1721 ret
= error(_("error opening index for %s"), packname
.buf
);
1724 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1725 FOR_EACH_OBJECT_PACK_ORDER
);
1731 stop_progress(&ctx
->progress
);
1732 strbuf_release(&progress_title
);
1733 strbuf_release(&packname
);
1738 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1739 struct oidset
*commits
)
1741 struct oidset_iter iter
;
1742 struct object_id
*oid
;
1744 if (!oidset_size(commits
))
1747 oidset_iter_init(commits
, &iter
);
1748 while ((oid
= oidset_iter_next(&iter
))) {
1749 oid_array_append(&ctx
->oids
, oid
);
1755 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1757 if (ctx
->report_progress
)
1758 ctx
->progress
= start_delayed_progress(
1759 _("Finding commits for commit graph among packed objects"),
1760 ctx
->approx_nr_objects
);
1761 for_each_packed_object(add_packed_commits
, ctx
,
1762 FOR_EACH_OBJECT_PACK_ORDER
);
1763 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1764 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1765 stop_progress(&ctx
->progress
);
1768 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1771 enum commit_graph_split_flags flags
= ctx
->opts
?
1772 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1774 ctx
->num_extra_edges
= 0;
1775 if (ctx
->report_progress
)
1776 ctx
->progress
= start_delayed_progress(
1777 _("Finding extra edges in commit graph"),
1779 oid_array_sort(&ctx
->oids
);
1780 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1781 unsigned int num_parents
;
1783 display_progress(ctx
->progress
, i
+ 1);
1785 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1786 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1788 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1789 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1792 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1793 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1795 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1797 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1798 if (num_parents
> 2)
1799 ctx
->num_extra_edges
+= num_parents
- 1;
1803 stop_progress(&ctx
->progress
);
1806 static int write_graph_chunk_base_1(struct hashfile
*f
,
1807 struct commit_graph
*g
)
1814 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1815 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1819 static int write_graph_chunk_base(struct hashfile
*f
,
1822 struct write_commit_graph_context
*ctx
= data
;
1823 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1825 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1826 error(_("failed to write correct number of base graph ids"));
1833 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1838 struct lock_file lk
= LOCK_INIT
;
1839 const unsigned hashsz
= the_hash_algo
->rawsz
;
1840 struct strbuf progress_title
= STRBUF_INIT
;
1841 struct chunkfile
*cf
;
1842 unsigned char file_hash
[GIT_MAX_RAWSZ
];
1845 struct strbuf tmp_file
= STRBUF_INIT
;
1847 strbuf_addf(&tmp_file
,
1848 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1850 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1852 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1855 if (safe_create_leading_directories(ctx
->graph_name
)) {
1856 UNLEAK(ctx
->graph_name
);
1857 error(_("unable to create leading directories of %s"),
1863 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1865 hold_lock_file_for_update_mode(&lk
, lock_name
,
1866 LOCK_DIE_ON_ERROR
, 0444);
1869 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1871 error(_("unable to create temporary graph layer"));
1875 if (adjust_shared_perm(ctx
->graph_name
)) {
1876 error(_("unable to adjust shared permissions for '%s'"),
1881 f
= hashfd(fd
, ctx
->graph_name
);
1883 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1884 LOCK_DIE_ON_ERROR
, 0444);
1885 fd
= get_lock_file_fd(&lk
);
1886 f
= hashfd(fd
, get_lock_file_path(&lk
));
1889 cf
= init_chunkfile(f
);
1891 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
1892 write_graph_chunk_fanout
);
1893 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, hashsz
* ctx
->commits
.nr
,
1894 write_graph_chunk_oids
);
1895 add_chunk(cf
, GRAPH_CHUNKID_DATA
, (hashsz
+ 16) * ctx
->commits
.nr
,
1896 write_graph_chunk_data
);
1898 if (ctx
->write_generation_data
)
1899 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
1900 sizeof(uint32_t) * ctx
->commits
.nr
,
1901 write_graph_chunk_generation_data
);
1902 if (ctx
->num_generation_data_overflows
)
1903 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
1904 sizeof(timestamp_t
) * ctx
->num_generation_data_overflows
,
1905 write_graph_chunk_generation_data_overflow
);
1906 if (ctx
->num_extra_edges
)
1907 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
1908 4 * ctx
->num_extra_edges
,
1909 write_graph_chunk_extra_edges
);
1910 if (ctx
->changed_paths
) {
1911 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
1912 sizeof(uint32_t) * ctx
->commits
.nr
,
1913 write_graph_chunk_bloom_indexes
);
1914 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
1915 sizeof(uint32_t) * 3
1916 + ctx
->total_bloom_filter_data_size
,
1917 write_graph_chunk_bloom_data
);
1919 if (ctx
->num_commit_graphs_after
> 1)
1920 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
1921 hashsz
* (ctx
->num_commit_graphs_after
- 1),
1922 write_graph_chunk_base
);
1924 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1926 hashwrite_u8(f
, GRAPH_VERSION
);
1927 hashwrite_u8(f
, oid_version());
1928 hashwrite_u8(f
, get_num_chunks(cf
));
1929 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1931 if (ctx
->report_progress
) {
1932 strbuf_addf(&progress_title
,
1933 Q_("Writing out commit graph in %d pass",
1934 "Writing out commit graph in %d passes",
1935 get_num_chunks(cf
)),
1936 get_num_chunks(cf
));
1937 ctx
->progress
= start_delayed_progress(
1939 get_num_chunks(cf
) * ctx
->commits
.nr
);
1942 write_chunkfile(cf
, ctx
);
1944 stop_progress(&ctx
->progress
);
1945 strbuf_release(&progress_title
);
1947 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1948 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1949 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
1951 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1952 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1953 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1954 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1957 close_commit_graph(ctx
->r
->objects
);
1958 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
1959 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1963 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1964 char *final_graph_name
;
1970 error(_("unable to open commit-graph chain file"));
1974 if (ctx
->base_graph_name
) {
1976 int idx
= ctx
->num_commit_graphs_after
- 1;
1977 if (ctx
->num_commit_graphs_after
> 1)
1980 dest
= ctx
->commit_graph_filenames_after
[idx
];
1982 if (strcmp(ctx
->base_graph_name
, dest
)) {
1983 result
= rename(ctx
->base_graph_name
, dest
);
1986 error(_("failed to rename base commit-graph file"));
1991 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
1996 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
1997 final_graph_name
= get_split_graph_filename(ctx
->odb
,
1998 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1999 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
2001 result
= rename(ctx
->graph_name
, final_graph_name
);
2003 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
2004 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
2007 error(_("failed to rename temporary commit-graph file"));
2012 commit_lock_file(&lk
);
2017 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2019 struct commit_graph
*g
;
2020 uint32_t num_commits
;
2021 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2024 int max_commits
= 0;
2028 max_commits
= ctx
->opts
->max_commits
;
2030 if (ctx
->opts
->size_multiple
)
2031 size_mult
= ctx
->opts
->size_multiple
;
2033 flags
= ctx
->opts
->split_flags
;
2036 g
= ctx
->r
->objects
->commit_graph
;
2037 num_commits
= ctx
->commits
.nr
;
2038 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2039 ctx
->num_commit_graphs_after
= 1;
2041 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2043 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2044 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2045 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
2046 (max_commits
&& num_commits
> max_commits
))) {
2047 if (g
->odb
!= ctx
->odb
)
2050 num_commits
+= g
->num_commits
;
2053 ctx
->num_commit_graphs_after
--;
2057 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2058 ctx
->new_base_graph
= g
;
2059 else if (ctx
->num_commit_graphs_after
!= 1)
2060 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2061 "should be 1 with --split=replace");
2063 if (ctx
->num_commit_graphs_after
== 2) {
2064 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2066 if (!strcmp(g
->filename
, old_graph_name
) &&
2067 g
->odb
!= ctx
->odb
) {
2068 ctx
->num_commit_graphs_after
= 1;
2069 ctx
->new_base_graph
= NULL
;
2072 free(old_graph_name
);
2075 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2076 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2078 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2079 i
< ctx
->num_commit_graphs_before
; i
++)
2080 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2082 i
= ctx
->num_commit_graphs_before
- 1;
2083 g
= ctx
->r
->objects
->commit_graph
;
2086 if (i
< ctx
->num_commit_graphs_after
)
2087 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2090 * If the topmost remaining layer has generation data chunk, the
2091 * resultant layer also has generation data chunk.
2093 if (i
== ctx
->num_commit_graphs_after
- 2)
2094 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2101 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2102 struct commit_graph
*g
)
2105 uint32_t offset
= g
->num_commits_in_base
;
2107 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2109 for (i
= 0; i
< g
->num_commits
; i
++) {
2110 struct object_id oid
;
2111 struct commit
*result
;
2113 display_progress(ctx
->progress
, i
+ 1);
2115 load_oid_from_graph(g
, i
+ offset
, &oid
);
2117 /* only add commits if they still exist in the repo */
2118 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2121 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2127 static int commit_compare(const void *_a
, const void *_b
)
2129 const struct commit
*a
= *(const struct commit
**)_a
;
2130 const struct commit
*b
= *(const struct commit
**)_b
;
2131 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2134 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2136 uint32_t i
, dedup_i
= 0;
2138 if (ctx
->report_progress
)
2139 ctx
->progress
= start_delayed_progress(
2140 _("Scanning merged commits"),
2143 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2145 ctx
->num_extra_edges
= 0;
2146 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2147 display_progress(ctx
->progress
, i
+ 1);
2149 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2150 &ctx
->commits
.list
[i
]->object
.oid
)) {
2152 * Silently ignore duplicates. These were likely
2153 * created due to a commit appearing in multiple
2154 * layers of the chain, which is unexpected but
2155 * not invalid. We should make sure there is a
2156 * unique copy in the new layer.
2159 unsigned int num_parents
;
2161 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2164 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2165 if (num_parents
> 2)
2166 ctx
->num_extra_edges
+= num_parents
- 1;
2170 ctx
->commits
.nr
= dedup_i
;
2172 stop_progress(&ctx
->progress
);
2175 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2177 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2178 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2180 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2181 current_graph_number
--;
2183 if (ctx
->report_progress
)
2184 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2186 merge_commit_graph(ctx
, g
);
2187 stop_progress(&ctx
->progress
);
2193 ctx
->new_base_graph
= g
;
2194 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2197 if (ctx
->new_base_graph
)
2198 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2200 sort_and_scan_merged_commits(ctx
);
2203 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2206 time_t now
= time(NULL
);
2208 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2210 struct utimbuf updated_time
;
2212 stat(ctx
->commit_graph_filenames_before
[i
], &st
);
2214 updated_time
.actime
= st
.st_atime
;
2215 updated_time
.modtime
= now
;
2216 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2220 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2222 struct strbuf path
= STRBUF_INIT
;
2226 timestamp_t expire_time
= time(NULL
);
2228 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2229 expire_time
= ctx
->opts
->expire_time
;
2231 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2232 unlink(chain_file_name
);
2233 free(chain_file_name
);
2234 ctx
->num_commit_graphs_after
= 0;
2237 strbuf_addstr(&path
, ctx
->odb
->path
);
2238 strbuf_addstr(&path
, "/info/commit-graphs");
2239 dir
= opendir(path
.buf
);
2244 strbuf_addch(&path
, '/');
2245 dirnamelen
= path
.len
;
2246 while ((de
= readdir(dir
)) != NULL
) {
2248 uint32_t i
, found
= 0;
2250 strbuf_setlen(&path
, dirnamelen
);
2251 strbuf_addstr(&path
, de
->d_name
);
2253 stat(path
.buf
, &st
);
2255 if (st
.st_mtime
> expire_time
)
2257 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2260 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2261 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2273 strbuf_release(&path
);
2276 int write_commit_graph(struct object_directory
*odb
,
2277 const struct string_list
*const pack_indexes
,
2278 struct oidset
*commits
,
2279 enum commit_graph_write_flags flags
,
2280 const struct commit_graph_opts
*opts
)
2282 struct repository
*r
= the_repository
;
2283 struct write_commit_graph_context
*ctx
;
2287 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2288 struct topo_level_slab topo_levels
;
2290 prepare_repo_settings(r
);
2291 if (!r
->settings
.core_commit_graph
) {
2292 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2295 if (!commit_graph_compatible(r
))
2298 CALLOC_ARRAY(ctx
, 1);
2301 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2302 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2303 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2305 ctx
->total_bloom_filter_data_size
= 0;
2306 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2307 ctx
->num_generation_data_overflows
= 0;
2309 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2310 bloom_settings
.bits_per_entry
);
2311 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2312 bloom_settings
.num_hashes
);
2313 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2314 bloom_settings
.max_changed_paths
);
2315 ctx
->bloom_settings
= &bloom_settings
;
2317 init_topo_level_slab(&topo_levels
);
2318 ctx
->topo_levels
= &topo_levels
;
2320 prepare_commit_graph(ctx
->r
);
2321 if (ctx
->r
->objects
->commit_graph
) {
2322 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2325 g
->topo_levels
= &topo_levels
;
2330 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2331 ctx
->changed_paths
= 1;
2332 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2333 struct commit_graph
*g
;
2335 g
= ctx
->r
->objects
->commit_graph
;
2337 /* We have changed-paths already. Keep them in the next graph */
2338 if (g
&& g
->chunk_bloom_data
) {
2339 ctx
->changed_paths
= 1;
2340 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2345 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2348 ctx
->num_commit_graphs_before
++;
2352 if (ctx
->num_commit_graphs_before
) {
2353 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2354 i
= ctx
->num_commit_graphs_before
;
2355 g
= ctx
->r
->objects
->commit_graph
;
2358 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2364 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2367 ctx
->approx_nr_objects
= approximate_object_count();
2369 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2370 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2371 for (i
= 0; i
< g
->num_commits
; i
++) {
2372 struct object_id oid
;
2373 oidread(&oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2374 oid_array_append(&ctx
->oids
, &oid
);
2379 ctx
->order_by_pack
= 1;
2380 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2385 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2389 if (!pack_indexes
&& !commits
) {
2390 ctx
->order_by_pack
= 1;
2391 fill_oids_from_all_packs(ctx
);
2394 close_reachable(ctx
);
2396 copy_oids_to_commits(ctx
);
2398 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2399 error(_("too many commits to write graph"));
2404 if (!ctx
->commits
.nr
&& !replace
)
2408 split_graph_merge_strategy(ctx
);
2411 merge_commit_graphs(ctx
);
2413 ctx
->num_commit_graphs_after
= 1;
2415 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2417 compute_topological_levels(ctx
);
2418 if (ctx
->write_generation_data
)
2419 compute_generation_numbers(ctx
);
2421 if (ctx
->changed_paths
)
2422 compute_bloom_filters(ctx
);
2424 res
= write_commit_graph_file(ctx
);
2427 mark_commit_graphs(ctx
);
2429 expire_commit_graphs(ctx
);
2432 free(ctx
->graph_name
);
2433 free(ctx
->commits
.list
);
2434 oid_array_clear(&ctx
->oids
);
2435 clear_topo_level_slab(&topo_levels
);
2437 if (ctx
->commit_graph_filenames_after
) {
2438 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2439 free(ctx
->commit_graph_filenames_after
[i
]);
2440 free(ctx
->commit_graph_hash_after
[i
]);
2443 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2444 free(ctx
->commit_graph_filenames_before
[i
]);
2446 free(ctx
->commit_graph_filenames_after
);
2447 free(ctx
->commit_graph_filenames_before
);
2448 free(ctx
->commit_graph_hash_after
);
2456 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2457 static int verify_commit_graph_error
;
2459 __attribute__((format (printf
, 1, 2)))
2460 static void graph_report(const char *fmt
, ...)
2464 verify_commit_graph_error
= 1;
2466 vfprintf(stderr
, fmt
, ap
);
2467 fprintf(stderr
, "\n");
2471 #define GENERATION_ZERO_EXISTS 1
2472 #define GENERATION_NUMBER_EXISTS 2
2474 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2476 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2479 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2481 uint32_t i
, cur_fanout_pos
= 0;
2482 struct object_id prev_oid
, cur_oid
;
2483 int generation_zero
= 0;
2484 struct progress
*progress
= NULL
;
2485 int local_error
= 0;
2488 graph_report("no commit-graph file loaded");
2492 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2493 if (verify_commit_graph_error
)
2494 return verify_commit_graph_error
;
2496 if (!commit_graph_checksum_valid(g
)) {
2497 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2498 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2501 for (i
= 0; i
< g
->num_commits
; i
++) {
2502 struct commit
*graph_commit
;
2504 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2506 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2507 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2508 oid_to_hex(&prev_oid
),
2509 oid_to_hex(&cur_oid
));
2511 oidcpy(&prev_oid
, &cur_oid
);
2513 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2514 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2516 if (i
!= fanout_value
)
2517 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2518 cur_fanout_pos
, fanout_value
, i
);
2522 graph_commit
= lookup_commit(r
, &cur_oid
);
2523 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2524 graph_report(_("failed to parse commit %s from commit-graph"),
2525 oid_to_hex(&cur_oid
));
2528 while (cur_fanout_pos
< 256) {
2529 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2531 if (g
->num_commits
!= fanout_value
)
2532 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2533 cur_fanout_pos
, fanout_value
, i
);
2538 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2539 return verify_commit_graph_error
;
2541 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2542 progress
= start_progress(_("Verifying commits in commit graph"),
2545 for (i
= 0; i
< g
->num_commits
; i
++) {
2546 struct commit
*graph_commit
, *odb_commit
;
2547 struct commit_list
*graph_parents
, *odb_parents
;
2548 timestamp_t max_generation
= 0;
2549 timestamp_t generation
;
2551 display_progress(progress
, i
+ 1);
2552 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2554 graph_commit
= lookup_commit(r
, &cur_oid
);
2555 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2556 if (parse_commit_internal(odb_commit
, 0, 0)) {
2557 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2558 oid_to_hex(&cur_oid
));
2562 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2563 get_commit_tree_oid(odb_commit
)))
2564 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2565 oid_to_hex(&cur_oid
),
2566 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2567 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2569 graph_parents
= graph_commit
->parents
;
2570 odb_parents
= odb_commit
->parents
;
2572 while (graph_parents
) {
2573 if (odb_parents
== NULL
) {
2574 graph_report(_("commit-graph parent list for commit %s is too long"),
2575 oid_to_hex(&cur_oid
));
2579 /* parse parent in case it is in a base graph */
2580 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2582 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2583 graph_report(_("commit-graph parent for %s is %s != %s"),
2584 oid_to_hex(&cur_oid
),
2585 oid_to_hex(&graph_parents
->item
->object
.oid
),
2586 oid_to_hex(&odb_parents
->item
->object
.oid
));
2588 generation
= commit_graph_generation(graph_parents
->item
);
2589 if (generation
> max_generation
)
2590 max_generation
= generation
;
2592 graph_parents
= graph_parents
->next
;
2593 odb_parents
= odb_parents
->next
;
2596 if (odb_parents
!= NULL
)
2597 graph_report(_("commit-graph parent list for commit %s terminates early"),
2598 oid_to_hex(&cur_oid
));
2600 if (!commit_graph_generation(graph_commit
)) {
2601 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2602 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2603 oid_to_hex(&cur_oid
));
2604 generation_zero
= GENERATION_ZERO_EXISTS
;
2605 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2606 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2607 oid_to_hex(&cur_oid
));
2609 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2613 * If we are using topological level and one of our parents has
2614 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2615 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2616 * in the following condition.
2618 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2621 generation
= commit_graph_generation(graph_commit
);
2622 if (generation
< max_generation
+ 1)
2623 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2624 oid_to_hex(&cur_oid
),
2626 max_generation
+ 1);
2628 if (graph_commit
->date
!= odb_commit
->date
)
2629 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2630 oid_to_hex(&cur_oid
),
2634 stop_progress(&progress
);
2636 local_error
= verify_commit_graph_error
;
2638 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2639 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2644 void free_commit_graph(struct commit_graph
*g
)
2649 munmap((void *)g
->data
, g
->data_len
);
2653 free(g
->bloom_filter_settings
);
2657 void disable_commit_graph(struct repository
*r
)
2659 r
->commit_graph_disabled
= 1;