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 prepare_repo_settings(r
);
256 ret
= parse_commit_graph(&r
->settings
, graph_map
, graph_size
);
261 munmap(graph_map
, graph_size
);
266 static int verify_commit_graph_lite(struct commit_graph
*g
)
269 * Basic validation shared between parse_commit_graph()
270 * which'll be called every time the graph is used, and the
271 * much more expensive verify_commit_graph() used by
272 * "commit-graph verify".
274 * There should only be very basic checks here to ensure that
275 * we don't e.g. segfault in fill_commit_in_graph(), but
276 * because this is a very hot codepath nothing that e.g. loops
277 * over g->num_commits, or runs a checksum on the commit-graph
280 if (!g
->chunk_oid_fanout
) {
281 error("commit-graph is missing the OID Fanout chunk");
284 if (!g
->chunk_oid_lookup
) {
285 error("commit-graph is missing the OID Lookup chunk");
288 if (!g
->chunk_commit_data
) {
289 error("commit-graph is missing the Commit Data chunk");
296 static int graph_read_oid_lookup(const unsigned char *chunk_start
,
297 size_t chunk_size
, void *data
)
299 struct commit_graph
*g
= data
;
300 g
->chunk_oid_lookup
= chunk_start
;
301 g
->num_commits
= chunk_size
/ g
->hash_len
;
305 static int graph_read_bloom_data(const unsigned char *chunk_start
,
306 size_t chunk_size
, void *data
)
308 struct commit_graph
*g
= data
;
309 uint32_t hash_version
;
310 g
->chunk_bloom_data
= chunk_start
;
311 hash_version
= get_be32(chunk_start
);
313 if (hash_version
!= 1)
316 g
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
317 g
->bloom_filter_settings
->hash_version
= hash_version
;
318 g
->bloom_filter_settings
->num_hashes
= get_be32(chunk_start
+ 4);
319 g
->bloom_filter_settings
->bits_per_entry
= get_be32(chunk_start
+ 8);
320 g
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
325 struct commit_graph
*parse_commit_graph(struct repo_settings
*s
,
326 void *graph_map
, size_t graph_size
)
328 const unsigned char *data
;
329 struct commit_graph
*graph
;
330 uint32_t graph_signature
;
331 unsigned char graph_version
, hash_version
;
332 struct chunkfile
*cf
= NULL
;
337 if (graph_size
< GRAPH_MIN_SIZE
)
340 data
= (const unsigned char *)graph_map
;
342 graph_signature
= get_be32(data
);
343 if (graph_signature
!= GRAPH_SIGNATURE
) {
344 error(_("commit-graph signature %X does not match signature %X"),
345 graph_signature
, GRAPH_SIGNATURE
);
349 graph_version
= *(unsigned char*)(data
+ 4);
350 if (graph_version
!= GRAPH_VERSION
) {
351 error(_("commit-graph version %X does not match version %X"),
352 graph_version
, GRAPH_VERSION
);
356 hash_version
= *(unsigned char*)(data
+ 5);
357 if (hash_version
!= oid_version(the_hash_algo
)) {
358 error(_("commit-graph hash version %X does not match version %X"),
359 hash_version
, oid_version(the_hash_algo
));
363 graph
= alloc_commit_graph();
365 graph
->hash_len
= the_hash_algo
->rawsz
;
366 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
367 graph
->data
= graph_map
;
368 graph
->data_len
= graph_size
;
370 if (graph_size
< GRAPH_HEADER_SIZE
+
371 (graph
->num_chunks
+ 1) * CHUNK_TOC_ENTRY_SIZE
+
372 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
373 error(_("commit-graph file is too small to hold %u chunks"),
379 cf
= init_chunkfile(NULL
);
381 if (read_table_of_contents(cf
, graph
->data
, graph_size
,
382 GRAPH_HEADER_SIZE
, graph
->num_chunks
))
383 goto free_and_return
;
385 pair_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
,
386 (const unsigned char **)&graph
->chunk_oid_fanout
);
387 read_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, graph_read_oid_lookup
, graph
);
388 pair_chunk(cf
, GRAPH_CHUNKID_DATA
, &graph
->chunk_commit_data
);
389 pair_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
, &graph
->chunk_extra_edges
);
390 pair_chunk(cf
, GRAPH_CHUNKID_BASE
, &graph
->chunk_base_graphs
);
392 if (s
->commit_graph_generation_version
>= 2) {
393 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
394 &graph
->chunk_generation_data
);
395 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
396 &graph
->chunk_generation_data_overflow
);
398 if (graph
->chunk_generation_data
)
399 graph
->read_generation_data
= 1;
402 if (s
->commit_graph_read_changed_paths
) {
403 pair_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
404 &graph
->chunk_bloom_indexes
);
405 read_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
406 graph_read_bloom_data
, graph
);
409 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
410 init_bloom_filters();
412 /* We need both the bloom chunks to exist together. Else ignore the data */
413 graph
->chunk_bloom_indexes
= NULL
;
414 graph
->chunk_bloom_data
= NULL
;
415 FREE_AND_NULL(graph
->bloom_filter_settings
);
418 oidread(&graph
->oid
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
420 if (verify_commit_graph_lite(graph
))
421 goto free_and_return
;
428 free(graph
->bloom_filter_settings
);
433 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
434 const char *graph_file
,
435 struct object_directory
*odb
)
440 struct commit_graph
*g
;
441 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
446 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
449 g
->filename
= xstrdup(graph_file
);
454 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
455 struct object_directory
*odb
)
457 char *graph_name
= get_commit_graph_filename(odb
);
458 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
464 static int add_graph_to_chain(struct commit_graph
*g
,
465 struct commit_graph
*chain
,
466 struct object_id
*oids
,
469 struct commit_graph
*cur_g
= chain
;
471 if (n
&& !g
->chunk_base_graphs
) {
472 warning(_("commit-graph has no base graphs chunk"));
480 !oideq(&oids
[n
], &cur_g
->oid
) ||
481 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
482 warning(_("commit-graph chain does not match"));
486 cur_g
= cur_g
->base_graph
;
489 g
->base_graph
= chain
;
492 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
497 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
498 struct object_directory
*odb
)
500 struct commit_graph
*graph_chain
= NULL
;
501 struct strbuf line
= STRBUF_INIT
;
503 struct object_id
*oids
;
504 int i
= 0, valid
= 1, count
;
505 char *chain_name
= get_commit_graph_chain_filename(odb
);
509 fp
= fopen(chain_name
, "r");
510 stat_res
= stat(chain_name
, &st
);
516 st
.st_size
<= the_hash_algo
->hexsz
) {
521 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
522 CALLOC_ARRAY(oids
, count
);
526 for (i
= 0; i
< count
; i
++) {
527 struct object_directory
*odb
;
529 if (strbuf_getline_lf(&line
, fp
) == EOF
)
532 if (get_oid_hex(line
.buf
, &oids
[i
])) {
533 warning(_("invalid commit-graph chain: line '%s' not a hash"),
540 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
541 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
542 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
547 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
557 warning(_("unable to find all commit-graph files"));
564 strbuf_release(&line
);
570 * returns 1 if and only if all graphs in the chain have
571 * corrected commit dates stored in the generation_data chunk.
573 static int validate_mixed_generation_chain(struct commit_graph
*g
)
575 int read_generation_data
= 1;
576 struct commit_graph
*p
= g
;
578 while (read_generation_data
&& p
) {
579 read_generation_data
= p
->read_generation_data
;
583 if (read_generation_data
)
587 g
->read_generation_data
= 0;
594 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
595 struct object_directory
*odb
)
597 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
600 g
= load_commit_graph_chain(r
, odb
);
602 validate_mixed_generation_chain(g
);
607 static void prepare_commit_graph_one(struct repository
*r
,
608 struct object_directory
*odb
)
611 if (r
->objects
->commit_graph
)
614 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
618 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
620 * On the first invocation, this function attempts to load the commit
621 * graph if the_repository is configured to have one.
623 static int prepare_commit_graph(struct repository
*r
)
625 struct object_directory
*odb
;
628 * Early return if there is no git dir or if the commit graph is
631 * This must come before the "already attempted?" check below, because
632 * we want to disable even an already-loaded graph file.
634 if (!r
->gitdir
|| r
->commit_graph_disabled
)
637 if (r
->objects
->commit_graph_attempted
)
638 return !!r
->objects
->commit_graph
;
639 r
->objects
->commit_graph_attempted
= 1;
641 prepare_repo_settings(r
);
643 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
644 r
->settings
.core_commit_graph
!= 1)
646 * This repository is not configured to use commit graphs, so
647 * do not load one. (But report commit_graph_attempted anyway
648 * so that commit graph loading is not attempted again for this
653 if (!commit_graph_compatible(r
))
657 for (odb
= r
->objects
->odb
;
658 !r
->objects
->commit_graph
&& odb
;
660 prepare_commit_graph_one(r
, odb
);
661 return !!r
->objects
->commit_graph
;
664 int generation_numbers_enabled(struct repository
*r
)
666 uint32_t first_generation
;
667 struct commit_graph
*g
;
668 if (!prepare_commit_graph(r
))
671 g
= r
->objects
->commit_graph
;
676 first_generation
= get_be32(g
->chunk_commit_data
+
677 g
->hash_len
+ 8) >> 2;
679 return !!first_generation
;
682 int corrected_commit_dates_enabled(struct repository
*r
)
684 struct commit_graph
*g
;
685 if (!prepare_commit_graph(r
))
688 g
= r
->objects
->commit_graph
;
693 return g
->read_generation_data
;
696 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
698 struct commit_graph
*g
= r
->objects
->commit_graph
;
700 if (g
->bloom_filter_settings
)
701 return g
->bloom_filter_settings
;
707 static void close_commit_graph_one(struct commit_graph
*g
)
712 clear_commit_graph_data_slab(&commit_graph_data_slab
);
713 close_commit_graph_one(g
->base_graph
);
714 free_commit_graph(g
);
717 void close_commit_graph(struct raw_object_store
*o
)
719 close_commit_graph_one(o
->commit_graph
);
720 o
->commit_graph
= NULL
;
723 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
725 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
726 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
729 static void load_oid_from_graph(struct commit_graph
*g
,
731 struct object_id
*oid
)
735 while (g
&& pos
< g
->num_commits_in_base
)
739 BUG("NULL commit-graph");
741 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
742 die(_("invalid commit position. commit-graph is likely corrupt"));
744 lex_index
= pos
- g
->num_commits_in_base
;
746 oidread(oid
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
749 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
750 struct commit_graph
*g
,
752 struct commit_list
**pptr
)
755 struct object_id oid
;
757 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
758 die("invalid parent position %"PRIu32
, pos
);
760 load_oid_from_graph(g
, pos
, &oid
);
761 c
= lookup_commit(r
, &oid
);
763 die(_("could not find commit %s"), oid_to_hex(&oid
));
764 commit_graph_data_at(c
)->graph_pos
= pos
;
765 return &commit_list_insert(c
, pptr
)->next
;
768 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
770 const unsigned char *commit_data
;
771 struct commit_graph_data
*graph_data
;
772 uint32_t lex_index
, offset_pos
;
773 uint64_t date_high
, date_low
, offset
;
775 while (pos
< g
->num_commits_in_base
)
778 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
779 die(_("invalid commit position. commit-graph is likely corrupt"));
781 lex_index
= pos
- g
->num_commits_in_base
;
782 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
784 graph_data
= commit_graph_data_at(item
);
785 graph_data
->graph_pos
= pos
;
787 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
788 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
789 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
791 if (g
->read_generation_data
) {
792 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ sizeof(uint32_t) * lex_index
);
794 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
795 if (!g
->chunk_generation_data_overflow
)
796 die(_("commit-graph requires overflow generation data but has none"));
798 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
799 graph_data
->generation
= item
->date
+ get_be64(g
->chunk_generation_data_overflow
+ 8 * offset_pos
);
801 graph_data
->generation
= item
->date
+ offset
;
803 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
806 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
809 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
814 static int fill_commit_in_graph(struct repository
*r
,
816 struct commit_graph
*g
, uint32_t pos
)
819 uint32_t *parent_data_ptr
;
820 struct commit_list
**pptr
;
821 const unsigned char *commit_data
;
824 while (pos
< g
->num_commits_in_base
)
827 fill_commit_graph_info(item
, g
, pos
);
829 lex_index
= pos
- g
->num_commits_in_base
;
830 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
832 item
->object
.parsed
= 1;
834 set_commit_tree(item
, NULL
);
836 pptr
= &item
->parents
;
838 edge_value
= get_be32(commit_data
+ g
->hash_len
);
839 if (edge_value
== GRAPH_PARENT_NONE
)
841 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
843 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
844 if (edge_value
== GRAPH_PARENT_NONE
)
846 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
847 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
851 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
852 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
854 edge_value
= get_be32(parent_data_ptr
);
855 pptr
= insert_parent_or_die(r
, g
,
856 edge_value
& GRAPH_EDGE_LAST_MASK
,
859 } while (!(edge_value
& GRAPH_LAST_EDGE
));
864 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
866 struct commit_graph
*cur_g
= g
;
869 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
870 cur_g
= cur_g
->base_graph
;
873 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
880 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
882 uint32_t graph_pos
= commit_graph_position(item
);
883 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
887 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
891 int repo_find_commit_pos_in_graph(struct repository
*r
, struct commit
*c
,
894 if (!prepare_commit_graph(r
))
896 return find_commit_pos_in_graph(c
, r
->objects
->commit_graph
, pos
);
899 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
901 struct commit
*commit
;
904 if (!prepare_commit_graph(repo
))
906 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
908 if (!has_object(repo
, id
, 0))
911 commit
= lookup_commit(repo
, id
);
914 if (commit
->object
.parsed
)
917 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
923 static int parse_commit_in_graph_one(struct repository
*r
,
924 struct commit_graph
*g
,
929 if (item
->object
.parsed
)
932 if (find_commit_pos_in_graph(item
, g
, &pos
))
933 return fill_commit_in_graph(r
, item
, g
, pos
);
938 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
940 static int checked_env
= 0;
943 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
944 die("dying as requested by the '%s' variable on commit-graph parse!",
945 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
948 if (!prepare_commit_graph(r
))
950 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
953 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
956 if (repo_find_commit_pos_in_graph(r
, item
, &pos
))
957 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
960 static struct tree
*load_tree_for_commit(struct repository
*r
,
961 struct commit_graph
*g
,
964 struct object_id oid
;
965 const unsigned char *commit_data
;
966 uint32_t graph_pos
= commit_graph_position(c
);
968 while (graph_pos
< g
->num_commits_in_base
)
971 commit_data
= g
->chunk_commit_data
+
972 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
974 oidread(&oid
, commit_data
);
975 set_commit_tree(c
, lookup_tree(r
, &oid
));
977 return c
->maybe_tree
;
980 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
981 struct commit_graph
*g
,
982 const struct commit
*c
)
985 return c
->maybe_tree
;
986 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
987 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
989 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
992 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
994 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
997 struct packed_commit_list
{
998 struct commit
**list
;
1003 struct write_commit_graph_context
{
1004 struct repository
*r
;
1005 struct object_directory
*odb
;
1007 struct oid_array oids
;
1008 struct packed_commit_list commits
;
1009 int num_extra_edges
;
1010 int num_generation_data_overflows
;
1011 unsigned long approx_nr_objects
;
1012 struct progress
*progress
;
1014 uint64_t progress_cnt
;
1016 char *base_graph_name
;
1017 int num_commit_graphs_before
;
1018 int num_commit_graphs_after
;
1019 char **commit_graph_filenames_before
;
1020 char **commit_graph_filenames_after
;
1021 char **commit_graph_hash_after
;
1022 uint32_t new_num_commits_in_base
;
1023 struct commit_graph
*new_base_graph
;
1030 write_generation_data
:1,
1031 trust_generation_numbers
:1;
1033 struct topo_level_slab
*topo_levels
;
1034 const struct commit_graph_opts
*opts
;
1035 size_t total_bloom_filter_data_size
;
1036 const struct bloom_filter_settings
*bloom_settings
;
1038 int count_bloom_filter_computed
;
1039 int count_bloom_filter_not_computed
;
1040 int count_bloom_filter_trunc_empty
;
1041 int count_bloom_filter_trunc_large
;
1044 static int write_graph_chunk_fanout(struct hashfile
*f
,
1047 struct write_commit_graph_context
*ctx
= data
;
1049 struct commit
**list
= ctx
->commits
.list
;
1052 * Write the first-level table (the list is sorted,
1053 * but we use a 256-entry lookup to be able to avoid
1054 * having to do eight extra binary search iterations).
1056 for (i
= 0; i
< 256; i
++) {
1057 while (count
< ctx
->commits
.nr
) {
1058 if ((*list
)->object
.oid
.hash
[0] != i
)
1060 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1065 hashwrite_be32(f
, count
);
1071 static int write_graph_chunk_oids(struct hashfile
*f
,
1074 struct write_commit_graph_context
*ctx
= data
;
1075 struct commit
**list
= ctx
->commits
.list
;
1077 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1078 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1079 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1085 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1087 const struct commit
* const *commits
= table
;
1088 return &commits
[index
]->object
.oid
;
1091 static int write_graph_chunk_data(struct hashfile
*f
,
1094 struct write_commit_graph_context
*ctx
= data
;
1095 struct commit
**list
= ctx
->commits
.list
;
1096 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1097 uint32_t num_extra_edges
= 0;
1099 while (list
< last
) {
1100 struct commit_list
*parent
;
1101 struct object_id
*tree
;
1103 uint32_t packedDate
[2];
1104 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1106 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1107 die(_("unable to parse commit %s"),
1108 oid_to_hex(&(*list
)->object
.oid
));
1109 tree
= get_commit_tree_oid(*list
);
1110 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1112 parent
= (*list
)->parents
;
1115 edge_value
= GRAPH_PARENT_NONE
;
1117 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1122 if (edge_value
>= 0)
1123 edge_value
+= ctx
->new_num_commits_in_base
;
1124 else if (ctx
->new_base_graph
) {
1126 if (find_commit_pos_in_graph(parent
->item
,
1127 ctx
->new_base_graph
,
1133 BUG("missing parent %s for commit %s",
1134 oid_to_hex(&parent
->item
->object
.oid
),
1135 oid_to_hex(&(*list
)->object
.oid
));
1138 hashwrite_be32(f
, edge_value
);
1141 parent
= parent
->next
;
1144 edge_value
= GRAPH_PARENT_NONE
;
1145 else if (parent
->next
)
1146 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1148 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1153 if (edge_value
>= 0)
1154 edge_value
+= ctx
->new_num_commits_in_base
;
1155 else if (ctx
->new_base_graph
) {
1157 if (find_commit_pos_in_graph(parent
->item
,
1158 ctx
->new_base_graph
,
1164 BUG("missing parent %s for commit %s",
1165 oid_to_hex(&parent
->item
->object
.oid
),
1166 oid_to_hex(&(*list
)->object
.oid
));
1169 hashwrite_be32(f
, edge_value
);
1171 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1174 parent
= parent
->next
;
1178 if (sizeof((*list
)->date
) > 4)
1179 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1183 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1185 packedDate
[1] = htonl((*list
)->date
);
1186 hashwrite(f
, packedDate
, 8);
1194 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1197 struct write_commit_graph_context
*ctx
= data
;
1198 int i
, num_generation_data_overflows
= 0;
1200 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1201 struct commit
*c
= ctx
->commits
.list
[i
];
1203 repo_parse_commit(ctx
->r
, c
);
1204 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1205 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1207 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1208 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1209 num_generation_data_overflows
++;
1212 hashwrite_be32(f
, offset
);
1218 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1221 struct write_commit_graph_context
*ctx
= data
;
1223 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1224 struct commit
*c
= ctx
->commits
.list
[i
];
1225 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1226 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1228 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1229 hashwrite_be32(f
, offset
>> 32);
1230 hashwrite_be32(f
, (uint32_t) offset
);
1237 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1240 struct write_commit_graph_context
*ctx
= data
;
1241 struct commit
**list
= ctx
->commits
.list
;
1242 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1243 struct commit_list
*parent
;
1245 while (list
< last
) {
1246 int num_parents
= 0;
1248 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1250 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1251 parent
= parent
->next
)
1254 if (num_parents
<= 2) {
1259 /* Since num_parents > 2, this initializer is safe. */
1260 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1261 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1266 if (edge_value
>= 0)
1267 edge_value
+= ctx
->new_num_commits_in_base
;
1268 else if (ctx
->new_base_graph
) {
1270 if (find_commit_pos_in_graph(parent
->item
,
1271 ctx
->new_base_graph
,
1277 BUG("missing parent %s for commit %s",
1278 oid_to_hex(&parent
->item
->object
.oid
),
1279 oid_to_hex(&(*list
)->object
.oid
));
1280 else if (!parent
->next
)
1281 edge_value
|= GRAPH_LAST_EDGE
;
1283 hashwrite_be32(f
, edge_value
);
1292 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1295 struct write_commit_graph_context
*ctx
= data
;
1296 struct commit
**list
= ctx
->commits
.list
;
1297 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1298 uint32_t cur_pos
= 0;
1300 while (list
< last
) {
1301 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1302 size_t len
= filter
? filter
->len
: 0;
1304 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1305 hashwrite_be32(f
, cur_pos
);
1312 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1314 struct json_writer jw
= JSON_WRITER_INIT
;
1316 jw_object_begin(&jw
, 0);
1317 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1318 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1319 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1320 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1323 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1328 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1331 struct write_commit_graph_context
*ctx
= data
;
1332 struct commit
**list
= ctx
->commits
.list
;
1333 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1335 trace2_bloom_filter_settings(ctx
);
1337 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1338 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1339 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1341 while (list
< last
) {
1342 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1343 size_t len
= filter
? filter
->len
: 0;
1345 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1347 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1354 static int add_packed_commits(const struct object_id
*oid
,
1355 struct packed_git
*pack
,
1359 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1360 enum object_type type
;
1361 off_t offset
= nth_packed_object_offset(pack
, pos
);
1362 struct object_info oi
= OBJECT_INFO_INIT
;
1365 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1368 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1369 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1371 if (type
!= OBJ_COMMIT
)
1374 oid_array_append(&ctx
->oids
, oid
);
1375 set_commit_pos(ctx
->r
, oid
);
1380 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1382 struct commit_list
*parent
;
1383 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1384 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1385 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1386 parent
->item
->object
.flags
|= REACHABLE
;
1391 static void close_reachable(struct write_commit_graph_context
*ctx
)
1394 struct commit
*commit
;
1395 enum commit_graph_split_flags flags
= ctx
->opts
?
1396 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1398 if (ctx
->report_progress
)
1399 ctx
->progress
= start_delayed_progress(
1400 _("Loading known commits in commit graph"),
1402 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1403 display_progress(ctx
->progress
, i
+ 1);
1404 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1406 commit
->object
.flags
|= REACHABLE
;
1408 stop_progress(&ctx
->progress
);
1411 * As this loop runs, ctx->oids.nr may grow, but not more
1412 * than the number of missing commits in the reachable
1415 if (ctx
->report_progress
)
1416 ctx
->progress
= start_delayed_progress(
1417 _("Expanding reachable commits in commit graph"),
1419 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1420 display_progress(ctx
->progress
, i
+ 1);
1421 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1426 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1427 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1428 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1429 add_missing_parents(ctx
, commit
);
1430 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1431 add_missing_parents(ctx
, commit
);
1433 stop_progress(&ctx
->progress
);
1435 if (ctx
->report_progress
)
1436 ctx
->progress
= start_delayed_progress(
1437 _("Clearing commit marks in commit graph"),
1439 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1440 display_progress(ctx
->progress
, i
+ 1);
1441 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1444 commit
->object
.flags
&= ~REACHABLE
;
1446 stop_progress(&ctx
->progress
);
1449 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1452 struct commit_list
*list
= NULL
;
1454 if (ctx
->report_progress
)
1455 ctx
->progress
= start_delayed_progress(
1456 _("Computing commit graph topological levels"),
1458 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1459 struct commit
*c
= ctx
->commits
.list
[i
];
1462 repo_parse_commit(ctx
->r
, c
);
1463 level
= *topo_level_slab_at(ctx
->topo_levels
, c
);
1465 display_progress(ctx
->progress
, i
+ 1);
1466 if (level
!= GENERATION_NUMBER_ZERO
)
1469 commit_list_insert(c
, &list
);
1471 struct commit
*current
= list
->item
;
1472 struct commit_list
*parent
;
1473 int all_parents_computed
= 1;
1474 uint32_t max_level
= 0;
1476 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1477 repo_parse_commit(ctx
->r
, parent
->item
);
1478 level
= *topo_level_slab_at(ctx
->topo_levels
, parent
->item
);
1480 if (level
== GENERATION_NUMBER_ZERO
) {
1481 all_parents_computed
= 0;
1482 commit_list_insert(parent
->item
, &list
);
1486 if (level
> max_level
)
1490 if (all_parents_computed
) {
1493 if (max_level
> GENERATION_NUMBER_V1_MAX
- 1)
1494 max_level
= GENERATION_NUMBER_V1_MAX
- 1;
1495 *topo_level_slab_at(ctx
->topo_levels
, current
) = max_level
+ 1;
1499 stop_progress(&ctx
->progress
);
1502 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1505 struct commit_list
*list
= NULL
;
1507 if (ctx
->report_progress
)
1508 ctx
->progress
= start_delayed_progress(
1509 _("Computing commit graph generation numbers"),
1512 if (!ctx
->trust_generation_numbers
) {
1513 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1514 struct commit
*c
= ctx
->commits
.list
[i
];
1515 repo_parse_commit(ctx
->r
, c
);
1516 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1520 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1521 struct commit
*c
= ctx
->commits
.list
[i
];
1522 timestamp_t corrected_commit_date
;
1524 repo_parse_commit(ctx
->r
, c
);
1525 corrected_commit_date
= commit_graph_data_at(c
)->generation
;
1527 display_progress(ctx
->progress
, i
+ 1);
1528 if (corrected_commit_date
!= GENERATION_NUMBER_ZERO
)
1531 commit_list_insert(c
, &list
);
1533 struct commit
*current
= list
->item
;
1534 struct commit_list
*parent
;
1535 int all_parents_computed
= 1;
1536 timestamp_t max_corrected_commit_date
= 0;
1538 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1539 repo_parse_commit(ctx
->r
, parent
->item
);
1540 corrected_commit_date
= commit_graph_data_at(parent
->item
)->generation
;
1542 if (corrected_commit_date
== GENERATION_NUMBER_ZERO
) {
1543 all_parents_computed
= 0;
1544 commit_list_insert(parent
->item
, &list
);
1548 if (corrected_commit_date
> max_corrected_commit_date
)
1549 max_corrected_commit_date
= corrected_commit_date
;
1552 if (all_parents_computed
) {
1555 if (current
->date
&& current
->date
> max_corrected_commit_date
)
1556 max_corrected_commit_date
= current
->date
- 1;
1557 commit_graph_data_at(current
)->generation
= max_corrected_commit_date
+ 1;
1562 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1563 struct commit
*c
= ctx
->commits
.list
[i
];
1564 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1565 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1566 ctx
->num_generation_data_overflows
++;
1568 stop_progress(&ctx
->progress
);
1571 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1573 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1574 ctx
->count_bloom_filter_computed
);
1575 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1576 ctx
->count_bloom_filter_not_computed
);
1577 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1578 ctx
->count_bloom_filter_trunc_empty
);
1579 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1580 ctx
->count_bloom_filter_trunc_large
);
1583 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1586 struct progress
*progress
= NULL
;
1587 struct commit
**sorted_commits
;
1588 int max_new_filters
;
1590 init_bloom_filters();
1592 if (ctx
->report_progress
)
1593 progress
= start_delayed_progress(
1594 _("Computing commit changed paths Bloom filters"),
1597 ALLOC_ARRAY(sorted_commits
, ctx
->commits
.nr
);
1598 COPY_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1600 if (ctx
->order_by_pack
)
1601 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1603 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1605 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1606 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1608 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1609 enum bloom_filter_computed computed
= 0;
1610 struct commit
*c
= sorted_commits
[i
];
1611 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1614 ctx
->count_bloom_filter_computed
< max_new_filters
,
1615 ctx
->bloom_settings
,
1617 if (computed
& BLOOM_COMPUTED
) {
1618 ctx
->count_bloom_filter_computed
++;
1619 if (computed
& BLOOM_TRUNC_EMPTY
)
1620 ctx
->count_bloom_filter_trunc_empty
++;
1621 if (computed
& BLOOM_TRUNC_LARGE
)
1622 ctx
->count_bloom_filter_trunc_large
++;
1623 } else if (computed
& BLOOM_NOT_COMPUTED
)
1624 ctx
->count_bloom_filter_not_computed
++;
1625 ctx
->total_bloom_filter_data_size
+= filter
1626 ? sizeof(unsigned char) * filter
->len
: 0;
1627 display_progress(progress
, i
+ 1);
1630 if (trace2_is_enabled())
1631 trace2_bloom_filter_write_statistics(ctx
);
1633 free(sorted_commits
);
1634 stop_progress(&progress
);
1637 struct refs_cb_data
{
1638 struct oidset
*commits
;
1639 struct progress
*progress
;
1642 static int add_ref_to_set(const char *refname UNUSED
,
1643 const struct object_id
*oid
,
1644 int flags UNUSED
, void *cb_data
)
1646 struct object_id peeled
;
1647 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1649 if (!peel_iterated_oid(oid
, &peeled
))
1651 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1652 oidset_insert(data
->commits
, oid
);
1654 display_progress(data
->progress
, oidset_size(data
->commits
));
1659 int write_commit_graph_reachable(struct object_directory
*odb
,
1660 enum commit_graph_write_flags flags
,
1661 const struct commit_graph_opts
*opts
)
1663 struct oidset commits
= OIDSET_INIT
;
1664 struct refs_cb_data data
;
1667 memset(&data
, 0, sizeof(data
));
1668 data
.commits
= &commits
;
1669 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1670 data
.progress
= start_delayed_progress(
1671 _("Collecting referenced commits"), 0);
1673 for_each_ref(add_ref_to_set
, &data
);
1675 stop_progress(&data
.progress
);
1677 result
= write_commit_graph(odb
, NULL
, &commits
,
1680 oidset_clear(&commits
);
1684 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1685 const struct string_list
*pack_indexes
)
1688 struct strbuf progress_title
= STRBUF_INIT
;
1689 struct strbuf packname
= STRBUF_INIT
;
1693 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1694 dirlen
= packname
.len
;
1695 if (ctx
->report_progress
) {
1696 strbuf_addf(&progress_title
,
1697 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1698 "Finding commits for commit graph in %"PRIuMAX
" packs",
1700 (uintmax_t)pack_indexes
->nr
);
1701 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1702 ctx
->progress_done
= 0;
1704 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1705 struct packed_git
*p
;
1706 strbuf_setlen(&packname
, dirlen
);
1707 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1708 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1710 ret
= error(_("error adding pack %s"), packname
.buf
);
1713 if (open_pack_index(p
)) {
1714 ret
= error(_("error opening index for %s"), packname
.buf
);
1717 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1718 FOR_EACH_OBJECT_PACK_ORDER
);
1724 stop_progress(&ctx
->progress
);
1725 strbuf_release(&progress_title
);
1726 strbuf_release(&packname
);
1731 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1732 struct oidset
*commits
)
1734 struct oidset_iter iter
;
1735 struct object_id
*oid
;
1737 if (!oidset_size(commits
))
1740 oidset_iter_init(commits
, &iter
);
1741 while ((oid
= oidset_iter_next(&iter
))) {
1742 oid_array_append(&ctx
->oids
, oid
);
1748 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1750 if (ctx
->report_progress
)
1751 ctx
->progress
= start_delayed_progress(
1752 _("Finding commits for commit graph among packed objects"),
1753 ctx
->approx_nr_objects
);
1754 for_each_packed_object(add_packed_commits
, ctx
,
1755 FOR_EACH_OBJECT_PACK_ORDER
);
1756 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1757 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1758 stop_progress(&ctx
->progress
);
1761 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1764 enum commit_graph_split_flags flags
= ctx
->opts
?
1765 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1767 ctx
->num_extra_edges
= 0;
1768 if (ctx
->report_progress
)
1769 ctx
->progress
= start_delayed_progress(
1770 _("Finding extra edges in commit graph"),
1772 oid_array_sort(&ctx
->oids
);
1773 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1774 unsigned int num_parents
;
1776 display_progress(ctx
->progress
, i
+ 1);
1778 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1779 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1781 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1782 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1785 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1786 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1788 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1790 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1791 if (num_parents
> 2)
1792 ctx
->num_extra_edges
+= num_parents
- 1;
1796 stop_progress(&ctx
->progress
);
1799 static int write_graph_chunk_base_1(struct hashfile
*f
,
1800 struct commit_graph
*g
)
1807 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1808 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1812 static int write_graph_chunk_base(struct hashfile
*f
,
1815 struct write_commit_graph_context
*ctx
= data
;
1816 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1818 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1819 error(_("failed to write correct number of base graph ids"));
1826 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1831 struct lock_file lk
= LOCK_INIT
;
1832 const unsigned hashsz
= the_hash_algo
->rawsz
;
1833 struct strbuf progress_title
= STRBUF_INIT
;
1834 struct chunkfile
*cf
;
1835 unsigned char file_hash
[GIT_MAX_RAWSZ
];
1838 struct strbuf tmp_file
= STRBUF_INIT
;
1840 strbuf_addf(&tmp_file
,
1841 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1843 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1845 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1848 if (safe_create_leading_directories(ctx
->graph_name
)) {
1849 UNLEAK(ctx
->graph_name
);
1850 error(_("unable to create leading directories of %s"),
1856 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1858 hold_lock_file_for_update_mode(&lk
, lock_name
,
1859 LOCK_DIE_ON_ERROR
, 0444);
1862 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1864 error(_("unable to create temporary graph layer"));
1868 if (adjust_shared_perm(ctx
->graph_name
)) {
1869 error(_("unable to adjust shared permissions for '%s'"),
1874 f
= hashfd(fd
, ctx
->graph_name
);
1876 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1877 LOCK_DIE_ON_ERROR
, 0444);
1878 fd
= get_lock_file_fd(&lk
);
1879 f
= hashfd(fd
, get_lock_file_path(&lk
));
1882 cf
= init_chunkfile(f
);
1884 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
1885 write_graph_chunk_fanout
);
1886 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, hashsz
* ctx
->commits
.nr
,
1887 write_graph_chunk_oids
);
1888 add_chunk(cf
, GRAPH_CHUNKID_DATA
, (hashsz
+ 16) * ctx
->commits
.nr
,
1889 write_graph_chunk_data
);
1891 if (ctx
->write_generation_data
)
1892 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
1893 sizeof(uint32_t) * ctx
->commits
.nr
,
1894 write_graph_chunk_generation_data
);
1895 if (ctx
->num_generation_data_overflows
)
1896 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
1897 sizeof(timestamp_t
) * ctx
->num_generation_data_overflows
,
1898 write_graph_chunk_generation_data_overflow
);
1899 if (ctx
->num_extra_edges
)
1900 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
1901 4 * ctx
->num_extra_edges
,
1902 write_graph_chunk_extra_edges
);
1903 if (ctx
->changed_paths
) {
1904 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
1905 sizeof(uint32_t) * ctx
->commits
.nr
,
1906 write_graph_chunk_bloom_indexes
);
1907 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
1908 sizeof(uint32_t) * 3
1909 + ctx
->total_bloom_filter_data_size
,
1910 write_graph_chunk_bloom_data
);
1912 if (ctx
->num_commit_graphs_after
> 1)
1913 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
1914 hashsz
* (ctx
->num_commit_graphs_after
- 1),
1915 write_graph_chunk_base
);
1917 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1919 hashwrite_u8(f
, GRAPH_VERSION
);
1920 hashwrite_u8(f
, oid_version(the_hash_algo
));
1921 hashwrite_u8(f
, get_num_chunks(cf
));
1922 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1924 if (ctx
->report_progress
) {
1925 strbuf_addf(&progress_title
,
1926 Q_("Writing out commit graph in %d pass",
1927 "Writing out commit graph in %d passes",
1928 get_num_chunks(cf
)),
1929 get_num_chunks(cf
));
1930 ctx
->progress
= start_delayed_progress(
1932 get_num_chunks(cf
) * ctx
->commits
.nr
);
1935 write_chunkfile(cf
, ctx
);
1937 stop_progress(&ctx
->progress
);
1938 strbuf_release(&progress_title
);
1940 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1941 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1942 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
1944 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1945 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1946 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1947 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1950 close_commit_graph(ctx
->r
->objects
);
1951 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
1952 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1956 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1957 char *final_graph_name
;
1963 error(_("unable to open commit-graph chain file"));
1967 if (ctx
->base_graph_name
) {
1969 int idx
= ctx
->num_commit_graphs_after
- 1;
1970 if (ctx
->num_commit_graphs_after
> 1)
1973 dest
= ctx
->commit_graph_filenames_after
[idx
];
1975 if (strcmp(ctx
->base_graph_name
, dest
)) {
1976 result
= rename(ctx
->base_graph_name
, dest
);
1979 error(_("failed to rename base commit-graph file"));
1984 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
1989 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
1990 final_graph_name
= get_split_graph_filename(ctx
->odb
,
1991 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1992 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
1994 result
= rename(ctx
->graph_name
, final_graph_name
);
1996 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
1997 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
2000 error(_("failed to rename temporary commit-graph file"));
2005 commit_lock_file(&lk
);
2010 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2012 struct commit_graph
*g
;
2013 uint32_t num_commits
;
2014 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2017 int max_commits
= 0;
2021 max_commits
= ctx
->opts
->max_commits
;
2023 if (ctx
->opts
->size_multiple
)
2024 size_mult
= ctx
->opts
->size_multiple
;
2026 flags
= ctx
->opts
->split_flags
;
2029 g
= ctx
->r
->objects
->commit_graph
;
2030 num_commits
= ctx
->commits
.nr
;
2031 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2032 ctx
->num_commit_graphs_after
= 1;
2034 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2036 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2037 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2038 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
2039 (max_commits
&& num_commits
> max_commits
))) {
2040 if (g
->odb
!= ctx
->odb
)
2043 num_commits
+= g
->num_commits
;
2046 ctx
->num_commit_graphs_after
--;
2050 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2051 ctx
->new_base_graph
= g
;
2052 else if (ctx
->num_commit_graphs_after
!= 1)
2053 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2054 "should be 1 with --split=replace");
2056 if (ctx
->num_commit_graphs_after
== 2) {
2057 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2059 if (!strcmp(g
->filename
, old_graph_name
) &&
2060 g
->odb
!= ctx
->odb
) {
2061 ctx
->num_commit_graphs_after
= 1;
2062 ctx
->new_base_graph
= NULL
;
2065 free(old_graph_name
);
2068 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2069 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2071 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2072 i
< ctx
->num_commit_graphs_before
; i
++)
2073 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2075 i
= ctx
->num_commit_graphs_before
- 1;
2076 g
= ctx
->r
->objects
->commit_graph
;
2079 if (i
< ctx
->num_commit_graphs_after
)
2080 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2083 * If the topmost remaining layer has generation data chunk, the
2084 * resultant layer also has generation data chunk.
2086 if (i
== ctx
->num_commit_graphs_after
- 2)
2087 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2094 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2095 struct commit_graph
*g
)
2098 uint32_t offset
= g
->num_commits_in_base
;
2100 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2102 for (i
= 0; i
< g
->num_commits
; i
++) {
2103 struct object_id oid
;
2104 struct commit
*result
;
2106 display_progress(ctx
->progress
, i
+ 1);
2108 load_oid_from_graph(g
, i
+ offset
, &oid
);
2110 /* only add commits if they still exist in the repo */
2111 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2114 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2120 static int commit_compare(const void *_a
, const void *_b
)
2122 const struct commit
*a
= *(const struct commit
**)_a
;
2123 const struct commit
*b
= *(const struct commit
**)_b
;
2124 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2127 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2129 uint32_t i
, dedup_i
= 0;
2131 if (ctx
->report_progress
)
2132 ctx
->progress
= start_delayed_progress(
2133 _("Scanning merged commits"),
2136 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2138 ctx
->num_extra_edges
= 0;
2139 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2140 display_progress(ctx
->progress
, i
+ 1);
2142 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2143 &ctx
->commits
.list
[i
]->object
.oid
)) {
2145 * Silently ignore duplicates. These were likely
2146 * created due to a commit appearing in multiple
2147 * layers of the chain, which is unexpected but
2148 * not invalid. We should make sure there is a
2149 * unique copy in the new layer.
2152 unsigned int num_parents
;
2154 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2157 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2158 if (num_parents
> 2)
2159 ctx
->num_extra_edges
+= num_parents
- 1;
2163 ctx
->commits
.nr
= dedup_i
;
2165 stop_progress(&ctx
->progress
);
2168 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2170 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2171 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2173 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2174 current_graph_number
--;
2176 if (ctx
->report_progress
)
2177 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2179 merge_commit_graph(ctx
, g
);
2180 stop_progress(&ctx
->progress
);
2186 ctx
->new_base_graph
= g
;
2187 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2190 if (ctx
->new_base_graph
)
2191 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2193 sort_and_scan_merged_commits(ctx
);
2196 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2199 time_t now
= time(NULL
);
2201 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2203 struct utimbuf updated_time
;
2205 if (stat(ctx
->commit_graph_filenames_before
[i
], &st
) < 0)
2208 updated_time
.actime
= st
.st_atime
;
2209 updated_time
.modtime
= now
;
2210 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2214 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2216 struct strbuf path
= STRBUF_INIT
;
2220 timestamp_t expire_time
= time(NULL
);
2222 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2223 expire_time
= ctx
->opts
->expire_time
;
2225 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2226 unlink(chain_file_name
);
2227 free(chain_file_name
);
2228 ctx
->num_commit_graphs_after
= 0;
2231 strbuf_addstr(&path
, ctx
->odb
->path
);
2232 strbuf_addstr(&path
, "/info/commit-graphs");
2233 dir
= opendir(path
.buf
);
2238 strbuf_addch(&path
, '/');
2239 dirnamelen
= path
.len
;
2240 while ((de
= readdir(dir
)) != NULL
) {
2242 uint32_t i
, found
= 0;
2244 strbuf_setlen(&path
, dirnamelen
);
2245 strbuf_addstr(&path
, de
->d_name
);
2247 if (stat(path
.buf
, &st
) < 0)
2250 if (st
.st_mtime
> expire_time
)
2252 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2255 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2256 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2270 strbuf_release(&path
);
2273 int write_commit_graph(struct object_directory
*odb
,
2274 const struct string_list
*const pack_indexes
,
2275 struct oidset
*commits
,
2276 enum commit_graph_write_flags flags
,
2277 const struct commit_graph_opts
*opts
)
2279 struct repository
*r
= the_repository
;
2280 struct write_commit_graph_context
*ctx
;
2284 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2285 struct topo_level_slab topo_levels
;
2287 prepare_repo_settings(r
);
2288 if (!r
->settings
.core_commit_graph
) {
2289 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2292 if (!commit_graph_compatible(r
))
2295 CALLOC_ARRAY(ctx
, 1);
2298 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2299 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2300 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2302 ctx
->total_bloom_filter_data_size
= 0;
2303 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2304 ctx
->num_generation_data_overflows
= 0;
2306 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2307 bloom_settings
.bits_per_entry
);
2308 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2309 bloom_settings
.num_hashes
);
2310 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2311 bloom_settings
.max_changed_paths
);
2312 ctx
->bloom_settings
= &bloom_settings
;
2314 init_topo_level_slab(&topo_levels
);
2315 ctx
->topo_levels
= &topo_levels
;
2317 prepare_commit_graph(ctx
->r
);
2318 if (ctx
->r
->objects
->commit_graph
) {
2319 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2322 g
->topo_levels
= &topo_levels
;
2327 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2328 ctx
->changed_paths
= 1;
2329 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2330 struct commit_graph
*g
;
2332 g
= ctx
->r
->objects
->commit_graph
;
2334 /* We have changed-paths already. Keep them in the next graph */
2335 if (g
&& g
->chunk_bloom_data
) {
2336 ctx
->changed_paths
= 1;
2337 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2342 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2345 ctx
->num_commit_graphs_before
++;
2349 if (ctx
->num_commit_graphs_before
) {
2350 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2351 i
= ctx
->num_commit_graphs_before
;
2352 g
= ctx
->r
->objects
->commit_graph
;
2355 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2361 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2364 ctx
->approx_nr_objects
= approximate_object_count();
2366 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2367 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2368 for (i
= 0; i
< g
->num_commits
; i
++) {
2369 struct object_id oid
;
2370 oidread(&oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2371 oid_array_append(&ctx
->oids
, &oid
);
2376 ctx
->order_by_pack
= 1;
2377 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2382 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2386 if (!pack_indexes
&& !commits
) {
2387 ctx
->order_by_pack
= 1;
2388 fill_oids_from_all_packs(ctx
);
2391 close_reachable(ctx
);
2393 copy_oids_to_commits(ctx
);
2395 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2396 error(_("too many commits to write graph"));
2401 if (!ctx
->commits
.nr
&& !replace
)
2405 split_graph_merge_strategy(ctx
);
2408 merge_commit_graphs(ctx
);
2410 ctx
->num_commit_graphs_after
= 1;
2412 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2414 compute_topological_levels(ctx
);
2415 if (ctx
->write_generation_data
)
2416 compute_generation_numbers(ctx
);
2418 if (ctx
->changed_paths
)
2419 compute_bloom_filters(ctx
);
2421 res
= write_commit_graph_file(ctx
);
2424 mark_commit_graphs(ctx
);
2426 expire_commit_graphs(ctx
);
2429 free(ctx
->graph_name
);
2430 free(ctx
->commits
.list
);
2431 oid_array_clear(&ctx
->oids
);
2432 clear_topo_level_slab(&topo_levels
);
2434 if (ctx
->commit_graph_filenames_after
) {
2435 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2436 free(ctx
->commit_graph_filenames_after
[i
]);
2437 free(ctx
->commit_graph_hash_after
[i
]);
2440 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2441 free(ctx
->commit_graph_filenames_before
[i
]);
2443 free(ctx
->commit_graph_filenames_after
);
2444 free(ctx
->commit_graph_filenames_before
);
2445 free(ctx
->commit_graph_hash_after
);
2453 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2454 static int verify_commit_graph_error
;
2456 __attribute__((format (printf
, 1, 2)))
2457 static void graph_report(const char *fmt
, ...)
2461 verify_commit_graph_error
= 1;
2463 vfprintf(stderr
, fmt
, ap
);
2464 fprintf(stderr
, "\n");
2468 #define GENERATION_ZERO_EXISTS 1
2469 #define GENERATION_NUMBER_EXISTS 2
2471 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2473 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2476 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2478 uint32_t i
, cur_fanout_pos
= 0;
2479 struct object_id prev_oid
, cur_oid
;
2480 int generation_zero
= 0;
2481 struct progress
*progress
= NULL
;
2482 int local_error
= 0;
2485 graph_report("no commit-graph file loaded");
2489 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2490 if (verify_commit_graph_error
)
2491 return verify_commit_graph_error
;
2493 if (!commit_graph_checksum_valid(g
)) {
2494 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2495 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2498 for (i
= 0; i
< g
->num_commits
; i
++) {
2499 struct commit
*graph_commit
;
2501 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2503 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2504 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2505 oid_to_hex(&prev_oid
),
2506 oid_to_hex(&cur_oid
));
2508 oidcpy(&prev_oid
, &cur_oid
);
2510 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2511 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2513 if (i
!= fanout_value
)
2514 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2515 cur_fanout_pos
, fanout_value
, i
);
2519 graph_commit
= lookup_commit(r
, &cur_oid
);
2520 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2521 graph_report(_("failed to parse commit %s from commit-graph"),
2522 oid_to_hex(&cur_oid
));
2525 while (cur_fanout_pos
< 256) {
2526 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2528 if (g
->num_commits
!= fanout_value
)
2529 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2530 cur_fanout_pos
, fanout_value
, i
);
2535 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2536 return verify_commit_graph_error
;
2538 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2539 progress
= start_progress(_("Verifying commits in commit graph"),
2542 for (i
= 0; i
< g
->num_commits
; i
++) {
2543 struct commit
*graph_commit
, *odb_commit
;
2544 struct commit_list
*graph_parents
, *odb_parents
;
2545 timestamp_t max_generation
= 0;
2546 timestamp_t generation
;
2548 display_progress(progress
, i
+ 1);
2549 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2551 graph_commit
= lookup_commit(r
, &cur_oid
);
2552 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2553 if (parse_commit_internal(odb_commit
, 0, 0)) {
2554 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2555 oid_to_hex(&cur_oid
));
2559 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2560 get_commit_tree_oid(odb_commit
)))
2561 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2562 oid_to_hex(&cur_oid
),
2563 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2564 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2566 graph_parents
= graph_commit
->parents
;
2567 odb_parents
= odb_commit
->parents
;
2569 while (graph_parents
) {
2571 graph_report(_("commit-graph parent list for commit %s is too long"),
2572 oid_to_hex(&cur_oid
));
2576 /* parse parent in case it is in a base graph */
2577 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2579 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2580 graph_report(_("commit-graph parent for %s is %s != %s"),
2581 oid_to_hex(&cur_oid
),
2582 oid_to_hex(&graph_parents
->item
->object
.oid
),
2583 oid_to_hex(&odb_parents
->item
->object
.oid
));
2585 generation
= commit_graph_generation(graph_parents
->item
);
2586 if (generation
> max_generation
)
2587 max_generation
= generation
;
2589 graph_parents
= graph_parents
->next
;
2590 odb_parents
= odb_parents
->next
;
2594 graph_report(_("commit-graph parent list for commit %s terminates early"),
2595 oid_to_hex(&cur_oid
));
2597 if (!commit_graph_generation(graph_commit
)) {
2598 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2599 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2600 oid_to_hex(&cur_oid
));
2601 generation_zero
= GENERATION_ZERO_EXISTS
;
2602 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2603 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2604 oid_to_hex(&cur_oid
));
2606 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2610 * If we are using topological level and one of our parents has
2611 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2612 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2613 * in the following condition.
2615 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2618 generation
= commit_graph_generation(graph_commit
);
2619 if (generation
< max_generation
+ 1)
2620 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2621 oid_to_hex(&cur_oid
),
2623 max_generation
+ 1);
2625 if (graph_commit
->date
!= odb_commit
->date
)
2626 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2627 oid_to_hex(&cur_oid
),
2631 stop_progress(&progress
);
2633 local_error
= verify_commit_graph_error
;
2635 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2636 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2641 void free_commit_graph(struct commit_graph
*g
)
2646 munmap((void *)g
->data
, g
->data_len
);
2650 free(g
->bloom_filter_settings
);
2654 void disable_commit_graph(struct repository
*r
)
2656 r
->commit_graph_disabled
= 1;