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 struct commit_graph
*alloc_commit_graph(void)
198 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
203 extern int read_replace_refs
;
205 static int commit_graph_compatible(struct repository
*r
)
210 if (read_replace_refs
) {
211 prepare_replace_object(r
);
212 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
216 prepare_commit_graft(r
);
217 if (r
->parsed_objects
&&
218 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
220 if (is_repository_shallow(r
))
226 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
228 *fd
= git_open(graph_file
);
231 if (fstat(*fd
, st
)) {
238 struct commit_graph
*load_commit_graph_one_fd_st(struct repository
*r
,
239 int fd
, struct stat
*st
,
240 struct object_directory
*odb
)
244 struct commit_graph
*ret
;
246 graph_size
= xsize_t(st
->st_size
);
248 if (graph_size
< GRAPH_MIN_SIZE
) {
250 error(_("commit-graph file is too small"));
253 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
255 ret
= parse_commit_graph(r
, graph_map
, graph_size
);
260 munmap(graph_map
, graph_size
);
265 static int verify_commit_graph_lite(struct commit_graph
*g
)
268 * Basic validation shared between parse_commit_graph()
269 * which'll be called every time the graph is used, and the
270 * much more expensive verify_commit_graph() used by
271 * "commit-graph verify".
273 * There should only be very basic checks here to ensure that
274 * we don't e.g. segfault in fill_commit_in_graph(), but
275 * because this is a very hot codepath nothing that e.g. loops
276 * over g->num_commits, or runs a checksum on the commit-graph
279 if (!g
->chunk_oid_fanout
) {
280 error("commit-graph is missing the OID Fanout chunk");
283 if (!g
->chunk_oid_lookup
) {
284 error("commit-graph is missing the OID Lookup chunk");
287 if (!g
->chunk_commit_data
) {
288 error("commit-graph is missing the Commit Data chunk");
295 static int graph_read_oid_lookup(const unsigned char *chunk_start
,
296 size_t chunk_size
, void *data
)
298 struct commit_graph
*g
= data
;
299 g
->chunk_oid_lookup
= chunk_start
;
300 g
->num_commits
= chunk_size
/ g
->hash_len
;
304 static int graph_read_bloom_data(const unsigned char *chunk_start
,
305 size_t chunk_size
, void *data
)
307 struct commit_graph
*g
= data
;
308 uint32_t hash_version
;
309 g
->chunk_bloom_data
= chunk_start
;
310 hash_version
= get_be32(chunk_start
);
312 if (hash_version
!= 1)
315 g
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
316 g
->bloom_filter_settings
->hash_version
= hash_version
;
317 g
->bloom_filter_settings
->num_hashes
= get_be32(chunk_start
+ 4);
318 g
->bloom_filter_settings
->bits_per_entry
= get_be32(chunk_start
+ 8);
319 g
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
324 struct commit_graph
*parse_commit_graph(struct repository
*r
,
325 void *graph_map
, size_t graph_size
)
327 const unsigned char *data
;
328 struct commit_graph
*graph
;
329 uint32_t graph_signature
;
330 unsigned char graph_version
, hash_version
;
331 struct chunkfile
*cf
= NULL
;
336 if (graph_size
< GRAPH_MIN_SIZE
)
339 data
= (const unsigned char *)graph_map
;
341 graph_signature
= get_be32(data
);
342 if (graph_signature
!= GRAPH_SIGNATURE
) {
343 error(_("commit-graph signature %X does not match signature %X"),
344 graph_signature
, GRAPH_SIGNATURE
);
348 graph_version
= *(unsigned char*)(data
+ 4);
349 if (graph_version
!= GRAPH_VERSION
) {
350 error(_("commit-graph version %X does not match version %X"),
351 graph_version
, GRAPH_VERSION
);
355 hash_version
= *(unsigned char*)(data
+ 5);
356 if (hash_version
!= oid_version(the_hash_algo
)) {
357 error(_("commit-graph hash version %X does not match version %X"),
358 hash_version
, oid_version(the_hash_algo
));
362 prepare_repo_settings(r
);
364 graph
= alloc_commit_graph();
366 graph
->hash_len
= the_hash_algo
->rawsz
;
367 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
368 graph
->data
= graph_map
;
369 graph
->data_len
= graph_size
;
371 if (graph_size
< GRAPH_HEADER_SIZE
+
372 (graph
->num_chunks
+ 1) * CHUNK_TOC_ENTRY_SIZE
+
373 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
374 error(_("commit-graph file is too small to hold %u chunks"),
380 cf
= init_chunkfile(NULL
);
382 if (read_table_of_contents(cf
, graph
->data
, graph_size
,
383 GRAPH_HEADER_SIZE
, graph
->num_chunks
))
384 goto free_and_return
;
386 pair_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
,
387 (const unsigned char **)&graph
->chunk_oid_fanout
);
388 read_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, graph_read_oid_lookup
, graph
);
389 pair_chunk(cf
, GRAPH_CHUNKID_DATA
, &graph
->chunk_commit_data
);
390 pair_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
, &graph
->chunk_extra_edges
);
391 pair_chunk(cf
, GRAPH_CHUNKID_BASE
, &graph
->chunk_base_graphs
);
393 if (get_configured_generation_version(r
) >= 2) {
394 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
395 &graph
->chunk_generation_data
);
396 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
397 &graph
->chunk_generation_data_overflow
);
399 if (graph
->chunk_generation_data
)
400 graph
->read_generation_data
= 1;
403 if (r
->settings
.commit_graph_read_changed_paths
) {
404 pair_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
405 &graph
->chunk_bloom_indexes
);
406 read_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
407 graph_read_bloom_data
, graph
);
410 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
411 init_bloom_filters();
413 /* We need both the bloom chunks to exist together. Else ignore the data */
414 graph
->chunk_bloom_indexes
= NULL
;
415 graph
->chunk_bloom_data
= NULL
;
416 FREE_AND_NULL(graph
->bloom_filter_settings
);
419 oidread(&graph
->oid
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
421 if (verify_commit_graph_lite(graph
))
422 goto free_and_return
;
429 free(graph
->bloom_filter_settings
);
434 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
435 const char *graph_file
,
436 struct object_directory
*odb
)
441 struct commit_graph
*g
;
442 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
447 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
450 g
->filename
= xstrdup(graph_file
);
455 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
456 struct object_directory
*odb
)
458 char *graph_name
= get_commit_graph_filename(odb
);
459 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
465 static int add_graph_to_chain(struct commit_graph
*g
,
466 struct commit_graph
*chain
,
467 struct object_id
*oids
,
470 struct commit_graph
*cur_g
= chain
;
472 if (n
&& !g
->chunk_base_graphs
) {
473 warning(_("commit-graph has no base graphs chunk"));
481 !oideq(&oids
[n
], &cur_g
->oid
) ||
482 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
483 warning(_("commit-graph chain does not match"));
487 cur_g
= cur_g
->base_graph
;
490 g
->base_graph
= chain
;
493 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
498 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
499 struct object_directory
*odb
)
501 struct commit_graph
*graph_chain
= NULL
;
502 struct strbuf line
= STRBUF_INIT
;
504 struct object_id
*oids
;
505 int i
= 0, valid
= 1, count
;
506 char *chain_name
= get_commit_graph_chain_filename(odb
);
510 fp
= fopen(chain_name
, "r");
511 stat_res
= stat(chain_name
, &st
);
517 st
.st_size
<= the_hash_algo
->hexsz
) {
522 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
523 CALLOC_ARRAY(oids
, count
);
527 for (i
= 0; i
< count
; i
++) {
528 struct object_directory
*odb
;
530 if (strbuf_getline_lf(&line
, fp
) == EOF
)
533 if (get_oid_hex(line
.buf
, &oids
[i
])) {
534 warning(_("invalid commit-graph chain: line '%s' not a hash"),
541 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
542 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
543 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
548 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
558 warning(_("unable to find all commit-graph files"));
565 strbuf_release(&line
);
571 * returns 1 if and only if all graphs in the chain have
572 * corrected commit dates stored in the generation_data chunk.
574 static int validate_mixed_generation_chain(struct commit_graph
*g
)
576 int read_generation_data
= 1;
577 struct commit_graph
*p
= g
;
579 while (read_generation_data
&& p
) {
580 read_generation_data
= p
->read_generation_data
;
584 if (read_generation_data
)
588 g
->read_generation_data
= 0;
595 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
596 struct object_directory
*odb
)
598 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
601 g
= load_commit_graph_chain(r
, odb
);
603 validate_mixed_generation_chain(g
);
608 static void prepare_commit_graph_one(struct repository
*r
,
609 struct object_directory
*odb
)
612 if (r
->objects
->commit_graph
)
615 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
619 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
621 * On the first invocation, this function attempts to load the commit
622 * graph if the_repository is configured to have one.
624 static int prepare_commit_graph(struct repository
*r
)
626 struct object_directory
*odb
;
629 * Early return if there is no git dir or if the commit graph is
632 * This must come before the "already attempted?" check below, because
633 * we want to disable even an already-loaded graph file.
635 if (!r
->gitdir
|| r
->commit_graph_disabled
)
638 if (r
->objects
->commit_graph_attempted
)
639 return !!r
->objects
->commit_graph
;
640 r
->objects
->commit_graph_attempted
= 1;
642 prepare_repo_settings(r
);
644 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
645 r
->settings
.core_commit_graph
!= 1)
647 * This repository is not configured to use commit graphs, so
648 * do not load one. (But report commit_graph_attempted anyway
649 * so that commit graph loading is not attempted again for this
654 if (!commit_graph_compatible(r
))
658 for (odb
= r
->objects
->odb
;
659 !r
->objects
->commit_graph
&& odb
;
661 prepare_commit_graph_one(r
, odb
);
662 return !!r
->objects
->commit_graph
;
665 int generation_numbers_enabled(struct repository
*r
)
667 uint32_t first_generation
;
668 struct commit_graph
*g
;
669 if (!prepare_commit_graph(r
))
672 g
= r
->objects
->commit_graph
;
677 first_generation
= get_be32(g
->chunk_commit_data
+
678 g
->hash_len
+ 8) >> 2;
680 return !!first_generation
;
683 int corrected_commit_dates_enabled(struct repository
*r
)
685 struct commit_graph
*g
;
686 if (!prepare_commit_graph(r
))
689 g
= r
->objects
->commit_graph
;
694 return g
->read_generation_data
;
697 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
699 struct commit_graph
*g
= r
->objects
->commit_graph
;
701 if (g
->bloom_filter_settings
)
702 return g
->bloom_filter_settings
;
708 static void close_commit_graph_one(struct commit_graph
*g
)
713 clear_commit_graph_data_slab(&commit_graph_data_slab
);
714 close_commit_graph_one(g
->base_graph
);
715 free_commit_graph(g
);
718 void close_commit_graph(struct raw_object_store
*o
)
720 close_commit_graph_one(o
->commit_graph
);
721 o
->commit_graph
= NULL
;
724 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
726 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
727 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
730 static void load_oid_from_graph(struct commit_graph
*g
,
732 struct object_id
*oid
)
736 while (g
&& pos
< g
->num_commits_in_base
)
740 BUG("NULL commit-graph");
742 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
743 die(_("invalid commit position. commit-graph is likely corrupt"));
745 lex_index
= pos
- g
->num_commits_in_base
;
747 oidread(oid
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
750 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
751 struct commit_graph
*g
,
753 struct commit_list
**pptr
)
756 struct object_id oid
;
758 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
759 die("invalid parent position %"PRIu32
, pos
);
761 load_oid_from_graph(g
, pos
, &oid
);
762 c
= lookup_commit(r
, &oid
);
764 die(_("could not find commit %s"), oid_to_hex(&oid
));
765 commit_graph_data_at(c
)->graph_pos
= pos
;
766 return &commit_list_insert(c
, pptr
)->next
;
769 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
771 const unsigned char *commit_data
;
772 struct commit_graph_data
*graph_data
;
773 uint32_t lex_index
, offset_pos
;
774 uint64_t date_high
, date_low
, offset
;
776 while (pos
< g
->num_commits_in_base
)
779 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
780 die(_("invalid commit position. commit-graph is likely corrupt"));
782 lex_index
= pos
- g
->num_commits_in_base
;
783 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
785 graph_data
= commit_graph_data_at(item
);
786 graph_data
->graph_pos
= pos
;
788 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
789 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
790 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
792 if (g
->read_generation_data
) {
793 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ sizeof(uint32_t) * lex_index
);
795 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
796 if (!g
->chunk_generation_data_overflow
)
797 die(_("commit-graph requires overflow generation data but has none"));
799 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
800 graph_data
->generation
= item
->date
+ get_be64(g
->chunk_generation_data_overflow
+ 8 * offset_pos
);
802 graph_data
->generation
= item
->date
+ offset
;
804 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
807 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
810 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
815 static int fill_commit_in_graph(struct repository
*r
,
817 struct commit_graph
*g
, uint32_t pos
)
820 uint32_t *parent_data_ptr
;
821 struct commit_list
**pptr
;
822 const unsigned char *commit_data
;
825 while (pos
< g
->num_commits_in_base
)
828 fill_commit_graph_info(item
, g
, pos
);
830 lex_index
= pos
- g
->num_commits_in_base
;
831 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
833 item
->object
.parsed
= 1;
835 set_commit_tree(item
, NULL
);
837 pptr
= &item
->parents
;
839 edge_value
= get_be32(commit_data
+ g
->hash_len
);
840 if (edge_value
== GRAPH_PARENT_NONE
)
842 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
844 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
845 if (edge_value
== GRAPH_PARENT_NONE
)
847 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
848 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
852 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
853 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
855 edge_value
= get_be32(parent_data_ptr
);
856 pptr
= insert_parent_or_die(r
, g
,
857 edge_value
& GRAPH_EDGE_LAST_MASK
,
860 } while (!(edge_value
& GRAPH_LAST_EDGE
));
865 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
867 struct commit_graph
*cur_g
= g
;
870 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
871 cur_g
= cur_g
->base_graph
;
874 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
881 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
883 uint32_t graph_pos
= commit_graph_position(item
);
884 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
888 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
892 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
894 struct commit
*commit
;
897 if (!repo
->objects
->commit_graph
)
899 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
901 if (!repo_has_object_file(repo
, id
))
904 commit
= lookup_commit(repo
, id
);
907 if (commit
->object
.parsed
)
910 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
916 static int parse_commit_in_graph_one(struct repository
*r
,
917 struct commit_graph
*g
,
922 if (item
->object
.parsed
)
925 if (find_commit_pos_in_graph(item
, g
, &pos
))
926 return fill_commit_in_graph(r
, item
, g
, pos
);
931 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
933 static int checked_env
= 0;
936 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
937 die("dying as requested by the '%s' variable on commit-graph parse!",
938 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
941 if (!prepare_commit_graph(r
))
943 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
946 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
949 if (!prepare_commit_graph(r
))
951 if (find_commit_pos_in_graph(item
, r
->objects
->commit_graph
, &pos
))
952 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
955 static struct tree
*load_tree_for_commit(struct repository
*r
,
956 struct commit_graph
*g
,
959 struct object_id oid
;
960 const unsigned char *commit_data
;
961 uint32_t graph_pos
= commit_graph_position(c
);
963 while (graph_pos
< g
->num_commits_in_base
)
966 commit_data
= g
->chunk_commit_data
+
967 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
969 oidread(&oid
, commit_data
);
970 set_commit_tree(c
, lookup_tree(r
, &oid
));
972 return c
->maybe_tree
;
975 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
976 struct commit_graph
*g
,
977 const struct commit
*c
)
980 return c
->maybe_tree
;
981 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
982 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
984 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
987 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
989 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
992 struct packed_commit_list
{
993 struct commit
**list
;
998 struct write_commit_graph_context
{
999 struct repository
*r
;
1000 struct object_directory
*odb
;
1002 struct oid_array oids
;
1003 struct packed_commit_list commits
;
1004 int num_extra_edges
;
1005 int num_generation_data_overflows
;
1006 unsigned long approx_nr_objects
;
1007 struct progress
*progress
;
1009 uint64_t progress_cnt
;
1011 char *base_graph_name
;
1012 int num_commit_graphs_before
;
1013 int num_commit_graphs_after
;
1014 char **commit_graph_filenames_before
;
1015 char **commit_graph_filenames_after
;
1016 char **commit_graph_hash_after
;
1017 uint32_t new_num_commits_in_base
;
1018 struct commit_graph
*new_base_graph
;
1025 write_generation_data
:1,
1026 trust_generation_numbers
:1;
1028 struct topo_level_slab
*topo_levels
;
1029 const struct commit_graph_opts
*opts
;
1030 size_t total_bloom_filter_data_size
;
1031 const struct bloom_filter_settings
*bloom_settings
;
1033 int count_bloom_filter_computed
;
1034 int count_bloom_filter_not_computed
;
1035 int count_bloom_filter_trunc_empty
;
1036 int count_bloom_filter_trunc_large
;
1039 static int write_graph_chunk_fanout(struct hashfile
*f
,
1042 struct write_commit_graph_context
*ctx
= data
;
1044 struct commit
**list
= ctx
->commits
.list
;
1047 * Write the first-level table (the list is sorted,
1048 * but we use a 256-entry lookup to be able to avoid
1049 * having to do eight extra binary search iterations).
1051 for (i
= 0; i
< 256; i
++) {
1052 while (count
< ctx
->commits
.nr
) {
1053 if ((*list
)->object
.oid
.hash
[0] != i
)
1055 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1060 hashwrite_be32(f
, count
);
1066 static int write_graph_chunk_oids(struct hashfile
*f
,
1069 struct write_commit_graph_context
*ctx
= data
;
1070 struct commit
**list
= ctx
->commits
.list
;
1072 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1073 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1074 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1080 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1082 const struct commit
* const *commits
= table
;
1083 return &commits
[index
]->object
.oid
;
1086 static int write_graph_chunk_data(struct hashfile
*f
,
1089 struct write_commit_graph_context
*ctx
= data
;
1090 struct commit
**list
= ctx
->commits
.list
;
1091 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1092 uint32_t num_extra_edges
= 0;
1094 while (list
< last
) {
1095 struct commit_list
*parent
;
1096 struct object_id
*tree
;
1098 uint32_t packedDate
[2];
1099 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1101 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1102 die(_("unable to parse commit %s"),
1103 oid_to_hex(&(*list
)->object
.oid
));
1104 tree
= get_commit_tree_oid(*list
);
1105 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1107 parent
= (*list
)->parents
;
1110 edge_value
= GRAPH_PARENT_NONE
;
1112 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1117 if (edge_value
>= 0)
1118 edge_value
+= ctx
->new_num_commits_in_base
;
1119 else if (ctx
->new_base_graph
) {
1121 if (find_commit_pos_in_graph(parent
->item
,
1122 ctx
->new_base_graph
,
1128 BUG("missing parent %s for commit %s",
1129 oid_to_hex(&parent
->item
->object
.oid
),
1130 oid_to_hex(&(*list
)->object
.oid
));
1133 hashwrite_be32(f
, edge_value
);
1136 parent
= parent
->next
;
1139 edge_value
= GRAPH_PARENT_NONE
;
1140 else if (parent
->next
)
1141 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1143 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1148 if (edge_value
>= 0)
1149 edge_value
+= ctx
->new_num_commits_in_base
;
1150 else if (ctx
->new_base_graph
) {
1152 if (find_commit_pos_in_graph(parent
->item
,
1153 ctx
->new_base_graph
,
1159 BUG("missing parent %s for commit %s",
1160 oid_to_hex(&parent
->item
->object
.oid
),
1161 oid_to_hex(&(*list
)->object
.oid
));
1164 hashwrite_be32(f
, edge_value
);
1166 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1169 parent
= parent
->next
;
1173 if (sizeof((*list
)->date
) > 4)
1174 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1178 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1180 packedDate
[1] = htonl((*list
)->date
);
1181 hashwrite(f
, packedDate
, 8);
1189 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1192 struct write_commit_graph_context
*ctx
= data
;
1193 int i
, num_generation_data_overflows
= 0;
1195 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1196 struct commit
*c
= ctx
->commits
.list
[i
];
1198 repo_parse_commit(ctx
->r
, c
);
1199 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1200 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1202 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1203 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1204 num_generation_data_overflows
++;
1207 hashwrite_be32(f
, offset
);
1213 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1216 struct write_commit_graph_context
*ctx
= data
;
1218 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1219 struct commit
*c
= ctx
->commits
.list
[i
];
1220 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1221 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1223 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1224 hashwrite_be32(f
, offset
>> 32);
1225 hashwrite_be32(f
, (uint32_t) offset
);
1232 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1235 struct write_commit_graph_context
*ctx
= data
;
1236 struct commit
**list
= ctx
->commits
.list
;
1237 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1238 struct commit_list
*parent
;
1240 while (list
< last
) {
1241 int num_parents
= 0;
1243 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1245 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1246 parent
= parent
->next
)
1249 if (num_parents
<= 2) {
1254 /* Since num_parents > 2, this initializer is safe. */
1255 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1256 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1261 if (edge_value
>= 0)
1262 edge_value
+= ctx
->new_num_commits_in_base
;
1263 else if (ctx
->new_base_graph
) {
1265 if (find_commit_pos_in_graph(parent
->item
,
1266 ctx
->new_base_graph
,
1272 BUG("missing parent %s for commit %s",
1273 oid_to_hex(&parent
->item
->object
.oid
),
1274 oid_to_hex(&(*list
)->object
.oid
));
1275 else if (!parent
->next
)
1276 edge_value
|= GRAPH_LAST_EDGE
;
1278 hashwrite_be32(f
, edge_value
);
1287 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1290 struct write_commit_graph_context
*ctx
= data
;
1291 struct commit
**list
= ctx
->commits
.list
;
1292 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1293 uint32_t cur_pos
= 0;
1295 while (list
< last
) {
1296 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1297 size_t len
= filter
? filter
->len
: 0;
1299 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1300 hashwrite_be32(f
, cur_pos
);
1307 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1309 struct json_writer jw
= JSON_WRITER_INIT
;
1311 jw_object_begin(&jw
, 0);
1312 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1313 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1314 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1315 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1318 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1323 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1326 struct write_commit_graph_context
*ctx
= data
;
1327 struct commit
**list
= ctx
->commits
.list
;
1328 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1330 trace2_bloom_filter_settings(ctx
);
1332 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1333 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1334 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1336 while (list
< last
) {
1337 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1338 size_t len
= filter
? filter
->len
: 0;
1340 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1342 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1349 static int add_packed_commits(const struct object_id
*oid
,
1350 struct packed_git
*pack
,
1354 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1355 enum object_type type
;
1356 off_t offset
= nth_packed_object_offset(pack
, pos
);
1357 struct object_info oi
= OBJECT_INFO_INIT
;
1360 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1363 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1364 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1366 if (type
!= OBJ_COMMIT
)
1369 oid_array_append(&ctx
->oids
, oid
);
1370 set_commit_pos(ctx
->r
, oid
);
1375 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1377 struct commit_list
*parent
;
1378 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1379 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1380 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1381 parent
->item
->object
.flags
|= REACHABLE
;
1386 static void close_reachable(struct write_commit_graph_context
*ctx
)
1389 struct commit
*commit
;
1390 enum commit_graph_split_flags flags
= ctx
->opts
?
1391 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1393 if (ctx
->report_progress
)
1394 ctx
->progress
= start_delayed_progress(
1395 _("Loading known commits in commit graph"),
1397 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1398 display_progress(ctx
->progress
, i
+ 1);
1399 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1401 commit
->object
.flags
|= REACHABLE
;
1403 stop_progress(&ctx
->progress
);
1406 * As this loop runs, ctx->oids.nr may grow, but not more
1407 * than the number of missing commits in the reachable
1410 if (ctx
->report_progress
)
1411 ctx
->progress
= start_delayed_progress(
1412 _("Expanding reachable commits in commit graph"),
1414 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1415 display_progress(ctx
->progress
, i
+ 1);
1416 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1421 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1422 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1423 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1424 add_missing_parents(ctx
, commit
);
1425 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1426 add_missing_parents(ctx
, commit
);
1428 stop_progress(&ctx
->progress
);
1430 if (ctx
->report_progress
)
1431 ctx
->progress
= start_delayed_progress(
1432 _("Clearing commit marks in commit graph"),
1434 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1435 display_progress(ctx
->progress
, i
+ 1);
1436 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1439 commit
->object
.flags
&= ~REACHABLE
;
1441 stop_progress(&ctx
->progress
);
1444 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1447 struct commit_list
*list
= NULL
;
1449 if (ctx
->report_progress
)
1450 ctx
->progress
= start_delayed_progress(
1451 _("Computing commit graph topological levels"),
1453 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1454 struct commit
*c
= ctx
->commits
.list
[i
];
1457 repo_parse_commit(ctx
->r
, c
);
1458 level
= *topo_level_slab_at(ctx
->topo_levels
, c
);
1460 display_progress(ctx
->progress
, i
+ 1);
1461 if (level
!= GENERATION_NUMBER_ZERO
)
1464 commit_list_insert(c
, &list
);
1466 struct commit
*current
= list
->item
;
1467 struct commit_list
*parent
;
1468 int all_parents_computed
= 1;
1469 uint32_t max_level
= 0;
1471 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1472 repo_parse_commit(ctx
->r
, parent
->item
);
1473 level
= *topo_level_slab_at(ctx
->topo_levels
, parent
->item
);
1475 if (level
== GENERATION_NUMBER_ZERO
) {
1476 all_parents_computed
= 0;
1477 commit_list_insert(parent
->item
, &list
);
1481 if (level
> max_level
)
1485 if (all_parents_computed
) {
1488 if (max_level
> GENERATION_NUMBER_V1_MAX
- 1)
1489 max_level
= GENERATION_NUMBER_V1_MAX
- 1;
1490 *topo_level_slab_at(ctx
->topo_levels
, current
) = max_level
+ 1;
1494 stop_progress(&ctx
->progress
);
1497 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1500 struct commit_list
*list
= NULL
;
1502 if (ctx
->report_progress
)
1503 ctx
->progress
= start_delayed_progress(
1504 _("Computing commit graph generation numbers"),
1507 if (!ctx
->trust_generation_numbers
) {
1508 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1509 struct commit
*c
= ctx
->commits
.list
[i
];
1510 repo_parse_commit(ctx
->r
, c
);
1511 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1515 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1516 struct commit
*c
= ctx
->commits
.list
[i
];
1517 timestamp_t corrected_commit_date
;
1519 repo_parse_commit(ctx
->r
, c
);
1520 corrected_commit_date
= commit_graph_data_at(c
)->generation
;
1522 display_progress(ctx
->progress
, i
+ 1);
1523 if (corrected_commit_date
!= GENERATION_NUMBER_ZERO
)
1526 commit_list_insert(c
, &list
);
1528 struct commit
*current
= list
->item
;
1529 struct commit_list
*parent
;
1530 int all_parents_computed
= 1;
1531 timestamp_t max_corrected_commit_date
= 0;
1533 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1534 repo_parse_commit(ctx
->r
, parent
->item
);
1535 corrected_commit_date
= commit_graph_data_at(parent
->item
)->generation
;
1537 if (corrected_commit_date
== GENERATION_NUMBER_ZERO
) {
1538 all_parents_computed
= 0;
1539 commit_list_insert(parent
->item
, &list
);
1543 if (corrected_commit_date
> max_corrected_commit_date
)
1544 max_corrected_commit_date
= corrected_commit_date
;
1547 if (all_parents_computed
) {
1550 if (current
->date
&& current
->date
> max_corrected_commit_date
)
1551 max_corrected_commit_date
= current
->date
- 1;
1552 commit_graph_data_at(current
)->generation
= max_corrected_commit_date
+ 1;
1557 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1558 struct commit
*c
= ctx
->commits
.list
[i
];
1559 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1560 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1561 ctx
->num_generation_data_overflows
++;
1563 stop_progress(&ctx
->progress
);
1566 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1568 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1569 ctx
->count_bloom_filter_computed
);
1570 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1571 ctx
->count_bloom_filter_not_computed
);
1572 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1573 ctx
->count_bloom_filter_trunc_empty
);
1574 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1575 ctx
->count_bloom_filter_trunc_large
);
1578 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1581 struct progress
*progress
= NULL
;
1582 struct commit
**sorted_commits
;
1583 int max_new_filters
;
1585 init_bloom_filters();
1587 if (ctx
->report_progress
)
1588 progress
= start_delayed_progress(
1589 _("Computing commit changed paths Bloom filters"),
1592 ALLOC_ARRAY(sorted_commits
, ctx
->commits
.nr
);
1593 COPY_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1595 if (ctx
->order_by_pack
)
1596 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1598 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1600 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1601 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1603 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1604 enum bloom_filter_computed computed
= 0;
1605 struct commit
*c
= sorted_commits
[i
];
1606 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1609 ctx
->count_bloom_filter_computed
< max_new_filters
,
1610 ctx
->bloom_settings
,
1612 if (computed
& BLOOM_COMPUTED
) {
1613 ctx
->count_bloom_filter_computed
++;
1614 if (computed
& BLOOM_TRUNC_EMPTY
)
1615 ctx
->count_bloom_filter_trunc_empty
++;
1616 if (computed
& BLOOM_TRUNC_LARGE
)
1617 ctx
->count_bloom_filter_trunc_large
++;
1618 } else if (computed
& BLOOM_NOT_COMPUTED
)
1619 ctx
->count_bloom_filter_not_computed
++;
1620 ctx
->total_bloom_filter_data_size
+= filter
1621 ? sizeof(unsigned char) * filter
->len
: 0;
1622 display_progress(progress
, i
+ 1);
1625 if (trace2_is_enabled())
1626 trace2_bloom_filter_write_statistics(ctx
);
1628 free(sorted_commits
);
1629 stop_progress(&progress
);
1632 struct refs_cb_data
{
1633 struct oidset
*commits
;
1634 struct progress
*progress
;
1637 static int add_ref_to_set(const char *refname
,
1638 const struct object_id
*oid
,
1639 int flags
, void *cb_data
)
1641 struct object_id peeled
;
1642 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1644 if (!peel_iterated_oid(oid
, &peeled
))
1646 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1647 oidset_insert(data
->commits
, oid
);
1649 display_progress(data
->progress
, oidset_size(data
->commits
));
1654 int write_commit_graph_reachable(struct object_directory
*odb
,
1655 enum commit_graph_write_flags flags
,
1656 const struct commit_graph_opts
*opts
)
1658 struct oidset commits
= OIDSET_INIT
;
1659 struct refs_cb_data data
;
1662 memset(&data
, 0, sizeof(data
));
1663 data
.commits
= &commits
;
1664 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1665 data
.progress
= start_delayed_progress(
1666 _("Collecting referenced commits"), 0);
1668 for_each_ref(add_ref_to_set
, &data
);
1670 stop_progress(&data
.progress
);
1672 result
= write_commit_graph(odb
, NULL
, &commits
,
1675 oidset_clear(&commits
);
1679 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1680 const struct string_list
*pack_indexes
)
1683 struct strbuf progress_title
= STRBUF_INIT
;
1684 struct strbuf packname
= STRBUF_INIT
;
1688 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1689 dirlen
= packname
.len
;
1690 if (ctx
->report_progress
) {
1691 strbuf_addf(&progress_title
,
1692 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1693 "Finding commits for commit graph in %"PRIuMAX
" packs",
1695 (uintmax_t)pack_indexes
->nr
);
1696 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1697 ctx
->progress_done
= 0;
1699 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1700 struct packed_git
*p
;
1701 strbuf_setlen(&packname
, dirlen
);
1702 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1703 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1705 ret
= error(_("error adding pack %s"), packname
.buf
);
1708 if (open_pack_index(p
)) {
1709 ret
= error(_("error opening index for %s"), packname
.buf
);
1712 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1713 FOR_EACH_OBJECT_PACK_ORDER
);
1719 stop_progress(&ctx
->progress
);
1720 strbuf_release(&progress_title
);
1721 strbuf_release(&packname
);
1726 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1727 struct oidset
*commits
)
1729 struct oidset_iter iter
;
1730 struct object_id
*oid
;
1732 if (!oidset_size(commits
))
1735 oidset_iter_init(commits
, &iter
);
1736 while ((oid
= oidset_iter_next(&iter
))) {
1737 oid_array_append(&ctx
->oids
, oid
);
1743 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1745 if (ctx
->report_progress
)
1746 ctx
->progress
= start_delayed_progress(
1747 _("Finding commits for commit graph among packed objects"),
1748 ctx
->approx_nr_objects
);
1749 for_each_packed_object(add_packed_commits
, ctx
,
1750 FOR_EACH_OBJECT_PACK_ORDER
);
1751 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1752 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1753 stop_progress(&ctx
->progress
);
1756 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1759 enum commit_graph_split_flags flags
= ctx
->opts
?
1760 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1762 ctx
->num_extra_edges
= 0;
1763 if (ctx
->report_progress
)
1764 ctx
->progress
= start_delayed_progress(
1765 _("Finding extra edges in commit graph"),
1767 oid_array_sort(&ctx
->oids
);
1768 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1769 unsigned int num_parents
;
1771 display_progress(ctx
->progress
, i
+ 1);
1773 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1774 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1776 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1777 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1780 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1781 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1783 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1785 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1786 if (num_parents
> 2)
1787 ctx
->num_extra_edges
+= num_parents
- 1;
1791 stop_progress(&ctx
->progress
);
1794 static int write_graph_chunk_base_1(struct hashfile
*f
,
1795 struct commit_graph
*g
)
1802 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1803 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1807 static int write_graph_chunk_base(struct hashfile
*f
,
1810 struct write_commit_graph_context
*ctx
= data
;
1811 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1813 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1814 error(_("failed to write correct number of base graph ids"));
1821 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1826 struct lock_file lk
= LOCK_INIT
;
1827 const unsigned hashsz
= the_hash_algo
->rawsz
;
1828 struct strbuf progress_title
= STRBUF_INIT
;
1829 struct chunkfile
*cf
;
1830 unsigned char file_hash
[GIT_MAX_RAWSZ
];
1833 struct strbuf tmp_file
= STRBUF_INIT
;
1835 strbuf_addf(&tmp_file
,
1836 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1838 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1840 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1843 if (safe_create_leading_directories(ctx
->graph_name
)) {
1844 UNLEAK(ctx
->graph_name
);
1845 error(_("unable to create leading directories of %s"),
1851 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1853 hold_lock_file_for_update_mode(&lk
, lock_name
,
1854 LOCK_DIE_ON_ERROR
, 0444);
1857 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1859 error(_("unable to create temporary graph layer"));
1863 if (adjust_shared_perm(ctx
->graph_name
)) {
1864 error(_("unable to adjust shared permissions for '%s'"),
1869 f
= hashfd(fd
, ctx
->graph_name
);
1871 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1872 LOCK_DIE_ON_ERROR
, 0444);
1873 fd
= get_lock_file_fd(&lk
);
1874 f
= hashfd(fd
, get_lock_file_path(&lk
));
1877 cf
= init_chunkfile(f
);
1879 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
1880 write_graph_chunk_fanout
);
1881 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, hashsz
* ctx
->commits
.nr
,
1882 write_graph_chunk_oids
);
1883 add_chunk(cf
, GRAPH_CHUNKID_DATA
, (hashsz
+ 16) * ctx
->commits
.nr
,
1884 write_graph_chunk_data
);
1886 if (ctx
->write_generation_data
)
1887 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
1888 sizeof(uint32_t) * ctx
->commits
.nr
,
1889 write_graph_chunk_generation_data
);
1890 if (ctx
->num_generation_data_overflows
)
1891 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
1892 sizeof(timestamp_t
) * ctx
->num_generation_data_overflows
,
1893 write_graph_chunk_generation_data_overflow
);
1894 if (ctx
->num_extra_edges
)
1895 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
1896 4 * ctx
->num_extra_edges
,
1897 write_graph_chunk_extra_edges
);
1898 if (ctx
->changed_paths
) {
1899 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
1900 sizeof(uint32_t) * ctx
->commits
.nr
,
1901 write_graph_chunk_bloom_indexes
);
1902 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
1903 sizeof(uint32_t) * 3
1904 + ctx
->total_bloom_filter_data_size
,
1905 write_graph_chunk_bloom_data
);
1907 if (ctx
->num_commit_graphs_after
> 1)
1908 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
1909 hashsz
* (ctx
->num_commit_graphs_after
- 1),
1910 write_graph_chunk_base
);
1912 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1914 hashwrite_u8(f
, GRAPH_VERSION
);
1915 hashwrite_u8(f
, oid_version(the_hash_algo
));
1916 hashwrite_u8(f
, get_num_chunks(cf
));
1917 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1919 if (ctx
->report_progress
) {
1920 strbuf_addf(&progress_title
,
1921 Q_("Writing out commit graph in %d pass",
1922 "Writing out commit graph in %d passes",
1923 get_num_chunks(cf
)),
1924 get_num_chunks(cf
));
1925 ctx
->progress
= start_delayed_progress(
1927 get_num_chunks(cf
) * ctx
->commits
.nr
);
1930 write_chunkfile(cf
, ctx
);
1932 stop_progress(&ctx
->progress
);
1933 strbuf_release(&progress_title
);
1935 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1936 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1937 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
1939 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1940 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1941 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1942 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1945 close_commit_graph(ctx
->r
->objects
);
1946 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
1947 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1951 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1952 char *final_graph_name
;
1958 error(_("unable to open commit-graph chain file"));
1962 if (ctx
->base_graph_name
) {
1964 int idx
= ctx
->num_commit_graphs_after
- 1;
1965 if (ctx
->num_commit_graphs_after
> 1)
1968 dest
= ctx
->commit_graph_filenames_after
[idx
];
1970 if (strcmp(ctx
->base_graph_name
, dest
)) {
1971 result
= rename(ctx
->base_graph_name
, dest
);
1974 error(_("failed to rename base commit-graph file"));
1979 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
1984 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
1985 final_graph_name
= get_split_graph_filename(ctx
->odb
,
1986 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1987 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
1989 result
= rename(ctx
->graph_name
, final_graph_name
);
1991 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
1992 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
1995 error(_("failed to rename temporary commit-graph file"));
2000 commit_lock_file(&lk
);
2005 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2007 struct commit_graph
*g
;
2008 uint32_t num_commits
;
2009 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2012 int max_commits
= 0;
2016 max_commits
= ctx
->opts
->max_commits
;
2018 if (ctx
->opts
->size_multiple
)
2019 size_mult
= ctx
->opts
->size_multiple
;
2021 flags
= ctx
->opts
->split_flags
;
2024 g
= ctx
->r
->objects
->commit_graph
;
2025 num_commits
= ctx
->commits
.nr
;
2026 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2027 ctx
->num_commit_graphs_after
= 1;
2029 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2031 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2032 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2033 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
2034 (max_commits
&& num_commits
> max_commits
))) {
2035 if (g
->odb
!= ctx
->odb
)
2038 num_commits
+= g
->num_commits
;
2041 ctx
->num_commit_graphs_after
--;
2045 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2046 ctx
->new_base_graph
= g
;
2047 else if (ctx
->num_commit_graphs_after
!= 1)
2048 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2049 "should be 1 with --split=replace");
2051 if (ctx
->num_commit_graphs_after
== 2) {
2052 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2054 if (!strcmp(g
->filename
, old_graph_name
) &&
2055 g
->odb
!= ctx
->odb
) {
2056 ctx
->num_commit_graphs_after
= 1;
2057 ctx
->new_base_graph
= NULL
;
2060 free(old_graph_name
);
2063 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2064 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2066 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2067 i
< ctx
->num_commit_graphs_before
; i
++)
2068 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2070 i
= ctx
->num_commit_graphs_before
- 1;
2071 g
= ctx
->r
->objects
->commit_graph
;
2074 if (i
< ctx
->num_commit_graphs_after
)
2075 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2078 * If the topmost remaining layer has generation data chunk, the
2079 * resultant layer also has generation data chunk.
2081 if (i
== ctx
->num_commit_graphs_after
- 2)
2082 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2089 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2090 struct commit_graph
*g
)
2093 uint32_t offset
= g
->num_commits_in_base
;
2095 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2097 for (i
= 0; i
< g
->num_commits
; i
++) {
2098 struct object_id oid
;
2099 struct commit
*result
;
2101 display_progress(ctx
->progress
, i
+ 1);
2103 load_oid_from_graph(g
, i
+ offset
, &oid
);
2105 /* only add commits if they still exist in the repo */
2106 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2109 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2115 static int commit_compare(const void *_a
, const void *_b
)
2117 const struct commit
*a
= *(const struct commit
**)_a
;
2118 const struct commit
*b
= *(const struct commit
**)_b
;
2119 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2122 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2124 uint32_t i
, dedup_i
= 0;
2126 if (ctx
->report_progress
)
2127 ctx
->progress
= start_delayed_progress(
2128 _("Scanning merged commits"),
2131 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2133 ctx
->num_extra_edges
= 0;
2134 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2135 display_progress(ctx
->progress
, i
+ 1);
2137 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2138 &ctx
->commits
.list
[i
]->object
.oid
)) {
2140 * Silently ignore duplicates. These were likely
2141 * created due to a commit appearing in multiple
2142 * layers of the chain, which is unexpected but
2143 * not invalid. We should make sure there is a
2144 * unique copy in the new layer.
2147 unsigned int num_parents
;
2149 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2152 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2153 if (num_parents
> 2)
2154 ctx
->num_extra_edges
+= num_parents
- 1;
2158 ctx
->commits
.nr
= dedup_i
;
2160 stop_progress(&ctx
->progress
);
2163 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2165 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2166 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2168 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2169 current_graph_number
--;
2171 if (ctx
->report_progress
)
2172 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2174 merge_commit_graph(ctx
, g
);
2175 stop_progress(&ctx
->progress
);
2181 ctx
->new_base_graph
= g
;
2182 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2185 if (ctx
->new_base_graph
)
2186 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2188 sort_and_scan_merged_commits(ctx
);
2191 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2194 time_t now
= time(NULL
);
2196 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2198 struct utimbuf updated_time
;
2200 if (stat(ctx
->commit_graph_filenames_before
[i
], &st
) < 0)
2203 updated_time
.actime
= st
.st_atime
;
2204 updated_time
.modtime
= now
;
2205 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2209 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2211 struct strbuf path
= STRBUF_INIT
;
2215 timestamp_t expire_time
= time(NULL
);
2217 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2218 expire_time
= ctx
->opts
->expire_time
;
2220 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2221 unlink(chain_file_name
);
2222 free(chain_file_name
);
2223 ctx
->num_commit_graphs_after
= 0;
2226 strbuf_addstr(&path
, ctx
->odb
->path
);
2227 strbuf_addstr(&path
, "/info/commit-graphs");
2228 dir
= opendir(path
.buf
);
2233 strbuf_addch(&path
, '/');
2234 dirnamelen
= path
.len
;
2235 while ((de
= readdir(dir
)) != NULL
) {
2237 uint32_t i
, found
= 0;
2239 strbuf_setlen(&path
, dirnamelen
);
2240 strbuf_addstr(&path
, de
->d_name
);
2242 if (stat(path
.buf
, &st
) < 0)
2245 if (st
.st_mtime
> expire_time
)
2247 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2250 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2251 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2263 strbuf_release(&path
);
2266 int write_commit_graph(struct object_directory
*odb
,
2267 const struct string_list
*const pack_indexes
,
2268 struct oidset
*commits
,
2269 enum commit_graph_write_flags flags
,
2270 const struct commit_graph_opts
*opts
)
2272 struct repository
*r
= the_repository
;
2273 struct write_commit_graph_context
*ctx
;
2277 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2278 struct topo_level_slab topo_levels
;
2280 prepare_repo_settings(r
);
2281 if (!r
->settings
.core_commit_graph
) {
2282 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2285 if (!commit_graph_compatible(r
))
2288 CALLOC_ARRAY(ctx
, 1);
2291 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2292 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2293 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2295 ctx
->total_bloom_filter_data_size
= 0;
2296 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2297 ctx
->num_generation_data_overflows
= 0;
2299 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2300 bloom_settings
.bits_per_entry
);
2301 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2302 bloom_settings
.num_hashes
);
2303 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2304 bloom_settings
.max_changed_paths
);
2305 ctx
->bloom_settings
= &bloom_settings
;
2307 init_topo_level_slab(&topo_levels
);
2308 ctx
->topo_levels
= &topo_levels
;
2310 prepare_commit_graph(ctx
->r
);
2311 if (ctx
->r
->objects
->commit_graph
) {
2312 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2315 g
->topo_levels
= &topo_levels
;
2320 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2321 ctx
->changed_paths
= 1;
2322 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2323 struct commit_graph
*g
;
2325 g
= ctx
->r
->objects
->commit_graph
;
2327 /* We have changed-paths already. Keep them in the next graph */
2328 if (g
&& g
->chunk_bloom_data
) {
2329 ctx
->changed_paths
= 1;
2330 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2335 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2338 ctx
->num_commit_graphs_before
++;
2342 if (ctx
->num_commit_graphs_before
) {
2343 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2344 i
= ctx
->num_commit_graphs_before
;
2345 g
= ctx
->r
->objects
->commit_graph
;
2348 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2354 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2357 ctx
->approx_nr_objects
= approximate_object_count();
2359 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2360 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2361 for (i
= 0; i
< g
->num_commits
; i
++) {
2362 struct object_id oid
;
2363 oidread(&oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2364 oid_array_append(&ctx
->oids
, &oid
);
2369 ctx
->order_by_pack
= 1;
2370 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2375 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2379 if (!pack_indexes
&& !commits
) {
2380 ctx
->order_by_pack
= 1;
2381 fill_oids_from_all_packs(ctx
);
2384 close_reachable(ctx
);
2386 copy_oids_to_commits(ctx
);
2388 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2389 error(_("too many commits to write graph"));
2394 if (!ctx
->commits
.nr
&& !replace
)
2398 split_graph_merge_strategy(ctx
);
2401 merge_commit_graphs(ctx
);
2403 ctx
->num_commit_graphs_after
= 1;
2405 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2407 compute_topological_levels(ctx
);
2408 if (ctx
->write_generation_data
)
2409 compute_generation_numbers(ctx
);
2411 if (ctx
->changed_paths
)
2412 compute_bloom_filters(ctx
);
2414 res
= write_commit_graph_file(ctx
);
2417 mark_commit_graphs(ctx
);
2419 expire_commit_graphs(ctx
);
2422 free(ctx
->graph_name
);
2423 free(ctx
->commits
.list
);
2424 oid_array_clear(&ctx
->oids
);
2425 clear_topo_level_slab(&topo_levels
);
2427 if (ctx
->commit_graph_filenames_after
) {
2428 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2429 free(ctx
->commit_graph_filenames_after
[i
]);
2430 free(ctx
->commit_graph_hash_after
[i
]);
2433 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2434 free(ctx
->commit_graph_filenames_before
[i
]);
2436 free(ctx
->commit_graph_filenames_after
);
2437 free(ctx
->commit_graph_filenames_before
);
2438 free(ctx
->commit_graph_hash_after
);
2446 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2447 static int verify_commit_graph_error
;
2449 __attribute__((format (printf
, 1, 2)))
2450 static void graph_report(const char *fmt
, ...)
2454 verify_commit_graph_error
= 1;
2456 vfprintf(stderr
, fmt
, ap
);
2457 fprintf(stderr
, "\n");
2461 #define GENERATION_ZERO_EXISTS 1
2462 #define GENERATION_NUMBER_EXISTS 2
2464 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2466 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2469 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2471 uint32_t i
, cur_fanout_pos
= 0;
2472 struct object_id prev_oid
, cur_oid
;
2473 int generation_zero
= 0;
2474 struct progress
*progress
= NULL
;
2475 int local_error
= 0;
2478 graph_report("no commit-graph file loaded");
2482 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2483 if (verify_commit_graph_error
)
2484 return verify_commit_graph_error
;
2486 if (!commit_graph_checksum_valid(g
)) {
2487 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2488 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2491 for (i
= 0; i
< g
->num_commits
; i
++) {
2492 struct commit
*graph_commit
;
2494 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2496 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2497 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2498 oid_to_hex(&prev_oid
),
2499 oid_to_hex(&cur_oid
));
2501 oidcpy(&prev_oid
, &cur_oid
);
2503 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2504 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2506 if (i
!= fanout_value
)
2507 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2508 cur_fanout_pos
, fanout_value
, i
);
2512 graph_commit
= lookup_commit(r
, &cur_oid
);
2513 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2514 graph_report(_("failed to parse commit %s from commit-graph"),
2515 oid_to_hex(&cur_oid
));
2518 while (cur_fanout_pos
< 256) {
2519 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2521 if (g
->num_commits
!= fanout_value
)
2522 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2523 cur_fanout_pos
, fanout_value
, i
);
2528 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2529 return verify_commit_graph_error
;
2531 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2532 progress
= start_progress(_("Verifying commits in commit graph"),
2535 for (i
= 0; i
< g
->num_commits
; i
++) {
2536 struct commit
*graph_commit
, *odb_commit
;
2537 struct commit_list
*graph_parents
, *odb_parents
;
2538 timestamp_t max_generation
= 0;
2539 timestamp_t generation
;
2541 display_progress(progress
, i
+ 1);
2542 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2544 graph_commit
= lookup_commit(r
, &cur_oid
);
2545 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2546 if (parse_commit_internal(odb_commit
, 0, 0)) {
2547 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2548 oid_to_hex(&cur_oid
));
2552 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2553 get_commit_tree_oid(odb_commit
)))
2554 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2555 oid_to_hex(&cur_oid
),
2556 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2557 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2559 graph_parents
= graph_commit
->parents
;
2560 odb_parents
= odb_commit
->parents
;
2562 while (graph_parents
) {
2564 graph_report(_("commit-graph parent list for commit %s is too long"),
2565 oid_to_hex(&cur_oid
));
2569 /* parse parent in case it is in a base graph */
2570 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2572 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2573 graph_report(_("commit-graph parent for %s is %s != %s"),
2574 oid_to_hex(&cur_oid
),
2575 oid_to_hex(&graph_parents
->item
->object
.oid
),
2576 oid_to_hex(&odb_parents
->item
->object
.oid
));
2578 generation
= commit_graph_generation(graph_parents
->item
);
2579 if (generation
> max_generation
)
2580 max_generation
= generation
;
2582 graph_parents
= graph_parents
->next
;
2583 odb_parents
= odb_parents
->next
;
2587 graph_report(_("commit-graph parent list for commit %s terminates early"),
2588 oid_to_hex(&cur_oid
));
2590 if (!commit_graph_generation(graph_commit
)) {
2591 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2592 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2593 oid_to_hex(&cur_oid
));
2594 generation_zero
= GENERATION_ZERO_EXISTS
;
2595 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2596 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2597 oid_to_hex(&cur_oid
));
2599 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2603 * If we are using topological level and one of our parents has
2604 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2605 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2606 * in the following condition.
2608 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2611 generation
= commit_graph_generation(graph_commit
);
2612 if (generation
< max_generation
+ 1)
2613 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2614 oid_to_hex(&cur_oid
),
2616 max_generation
+ 1);
2618 if (graph_commit
->date
!= odb_commit
->date
)
2619 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2620 oid_to_hex(&cur_oid
),
2624 stop_progress(&progress
);
2626 local_error
= verify_commit_graph_error
;
2628 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2629 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2634 void free_commit_graph(struct commit_graph
*g
)
2639 munmap((void *)g
->data
, g
->data_len
);
2643 free(g
->bloom_filter_settings
);
2647 void disable_commit_graph(struct repository
*r
)
2649 r
->commit_graph_disabled
= 1;