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
);
516 st
.st_size
<= the_hash_algo
->hexsz
)
519 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
520 CALLOC_ARRAY(oids
, count
);
524 for (i
= 0; i
< count
; i
++) {
525 struct object_directory
*odb
;
527 if (strbuf_getline_lf(&line
, fp
) == EOF
)
530 if (get_oid_hex(line
.buf
, &oids
[i
])) {
531 warning(_("invalid commit-graph chain: line '%s' not a hash"),
538 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
539 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
540 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
545 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
555 warning(_("unable to find all commit-graph files"));
562 strbuf_release(&line
);
568 * returns 1 if and only if all graphs in the chain have
569 * corrected commit dates stored in the generation_data chunk.
571 static int validate_mixed_generation_chain(struct commit_graph
*g
)
573 int read_generation_data
= 1;
574 struct commit_graph
*p
= g
;
576 while (read_generation_data
&& p
) {
577 read_generation_data
= p
->read_generation_data
;
581 if (read_generation_data
)
585 g
->read_generation_data
= 0;
592 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
593 struct object_directory
*odb
)
595 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
598 g
= load_commit_graph_chain(r
, odb
);
600 validate_mixed_generation_chain(g
);
605 static void prepare_commit_graph_one(struct repository
*r
,
606 struct object_directory
*odb
)
609 if (r
->objects
->commit_graph
)
612 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
616 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
618 * On the first invocation, this function attempts to load the commit
619 * graph if the_repository is configured to have one.
621 static int prepare_commit_graph(struct repository
*r
)
623 struct object_directory
*odb
;
626 * Early return if there is no git dir or if the commit graph is
629 * This must come before the "already attempted?" check below, because
630 * we want to disable even an already-loaded graph file.
632 if (!r
->gitdir
|| r
->commit_graph_disabled
)
635 if (r
->objects
->commit_graph_attempted
)
636 return !!r
->objects
->commit_graph
;
637 r
->objects
->commit_graph_attempted
= 1;
639 prepare_repo_settings(r
);
641 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
642 r
->settings
.core_commit_graph
!= 1)
644 * This repository is not configured to use commit graphs, so
645 * do not load one. (But report commit_graph_attempted anyway
646 * so that commit graph loading is not attempted again for this
651 if (!commit_graph_compatible(r
))
655 for (odb
= r
->objects
->odb
;
656 !r
->objects
->commit_graph
&& odb
;
658 prepare_commit_graph_one(r
, odb
);
659 return !!r
->objects
->commit_graph
;
662 int generation_numbers_enabled(struct repository
*r
)
664 uint32_t first_generation
;
665 struct commit_graph
*g
;
666 if (!prepare_commit_graph(r
))
669 g
= r
->objects
->commit_graph
;
674 first_generation
= get_be32(g
->chunk_commit_data
+
675 g
->hash_len
+ 8) >> 2;
677 return !!first_generation
;
680 int corrected_commit_dates_enabled(struct repository
*r
)
682 struct commit_graph
*g
;
683 if (!prepare_commit_graph(r
))
686 g
= r
->objects
->commit_graph
;
691 return g
->read_generation_data
;
694 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
696 struct commit_graph
*g
= r
->objects
->commit_graph
;
698 if (g
->bloom_filter_settings
)
699 return g
->bloom_filter_settings
;
705 static void close_commit_graph_one(struct commit_graph
*g
)
710 clear_commit_graph_data_slab(&commit_graph_data_slab
);
711 close_commit_graph_one(g
->base_graph
);
712 free_commit_graph(g
);
715 void close_commit_graph(struct raw_object_store
*o
)
717 close_commit_graph_one(o
->commit_graph
);
718 o
->commit_graph
= NULL
;
721 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
723 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
724 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
727 static void load_oid_from_graph(struct commit_graph
*g
,
729 struct object_id
*oid
)
733 while (g
&& pos
< g
->num_commits_in_base
)
737 BUG("NULL commit-graph");
739 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
740 die(_("invalid commit position. commit-graph is likely corrupt"));
742 lex_index
= pos
- g
->num_commits_in_base
;
744 oidread(oid
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
747 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
748 struct commit_graph
*g
,
750 struct commit_list
**pptr
)
753 struct object_id oid
;
755 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
756 die("invalid parent position %"PRIu32
, pos
);
758 load_oid_from_graph(g
, pos
, &oid
);
759 c
= lookup_commit(r
, &oid
);
761 die(_("could not find commit %s"), oid_to_hex(&oid
));
762 commit_graph_data_at(c
)->graph_pos
= pos
;
763 return &commit_list_insert(c
, pptr
)->next
;
766 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
768 const unsigned char *commit_data
;
769 struct commit_graph_data
*graph_data
;
770 uint32_t lex_index
, offset_pos
;
771 uint64_t date_high
, date_low
, offset
;
773 while (pos
< g
->num_commits_in_base
)
776 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
777 die(_("invalid commit position. commit-graph is likely corrupt"));
779 lex_index
= pos
- g
->num_commits_in_base
;
780 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
782 graph_data
= commit_graph_data_at(item
);
783 graph_data
->graph_pos
= pos
;
785 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
786 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
787 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
789 if (g
->read_generation_data
) {
790 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ sizeof(uint32_t) * lex_index
);
792 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
793 if (!g
->chunk_generation_data_overflow
)
794 die(_("commit-graph requires overflow generation data but has none"));
796 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
797 graph_data
->generation
= item
->date
+ get_be64(g
->chunk_generation_data_overflow
+ 8 * offset_pos
);
799 graph_data
->generation
= item
->date
+ offset
;
801 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
804 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
807 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
812 static int fill_commit_in_graph(struct repository
*r
,
814 struct commit_graph
*g
, uint32_t pos
)
817 uint32_t *parent_data_ptr
;
818 struct commit_list
**pptr
;
819 const unsigned char *commit_data
;
822 while (pos
< g
->num_commits_in_base
)
825 fill_commit_graph_info(item
, g
, pos
);
827 lex_index
= pos
- g
->num_commits_in_base
;
828 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
830 item
->object
.parsed
= 1;
832 set_commit_tree(item
, NULL
);
834 pptr
= &item
->parents
;
836 edge_value
= get_be32(commit_data
+ g
->hash_len
);
837 if (edge_value
== GRAPH_PARENT_NONE
)
839 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
841 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
842 if (edge_value
== GRAPH_PARENT_NONE
)
844 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
845 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
849 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
850 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
852 edge_value
= get_be32(parent_data_ptr
);
853 pptr
= insert_parent_or_die(r
, g
,
854 edge_value
& GRAPH_EDGE_LAST_MASK
,
857 } while (!(edge_value
& GRAPH_LAST_EDGE
));
862 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
864 struct commit_graph
*cur_g
= g
;
867 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
868 cur_g
= cur_g
->base_graph
;
871 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
878 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
880 uint32_t graph_pos
= commit_graph_position(item
);
881 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
885 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
889 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
891 struct commit
*commit
;
894 if (!repo
->objects
->commit_graph
)
896 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
898 if (!repo_has_object_file(repo
, id
))
901 commit
= lookup_commit(repo
, id
);
904 if (commit
->object
.parsed
)
907 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
913 static int parse_commit_in_graph_one(struct repository
*r
,
914 struct commit_graph
*g
,
919 if (item
->object
.parsed
)
922 if (find_commit_pos_in_graph(item
, g
, &pos
))
923 return fill_commit_in_graph(r
, item
, g
, pos
);
928 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
930 static int checked_env
= 0;
933 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
934 die("dying as requested by the '%s' variable on commit-graph parse!",
935 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
938 if (!prepare_commit_graph(r
))
940 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
943 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
946 if (!prepare_commit_graph(r
))
948 if (find_commit_pos_in_graph(item
, r
->objects
->commit_graph
, &pos
))
949 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
952 static struct tree
*load_tree_for_commit(struct repository
*r
,
953 struct commit_graph
*g
,
956 struct object_id oid
;
957 const unsigned char *commit_data
;
958 uint32_t graph_pos
= commit_graph_position(c
);
960 while (graph_pos
< g
->num_commits_in_base
)
963 commit_data
= g
->chunk_commit_data
+
964 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
966 oidread(&oid
, commit_data
);
967 set_commit_tree(c
, lookup_tree(r
, &oid
));
969 return c
->maybe_tree
;
972 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
973 struct commit_graph
*g
,
974 const struct commit
*c
)
977 return c
->maybe_tree
;
978 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
979 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
981 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
984 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
986 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
989 struct packed_commit_list
{
990 struct commit
**list
;
995 struct write_commit_graph_context
{
996 struct repository
*r
;
997 struct object_directory
*odb
;
999 struct oid_array oids
;
1000 struct packed_commit_list commits
;
1001 int num_extra_edges
;
1002 int num_generation_data_overflows
;
1003 unsigned long approx_nr_objects
;
1004 struct progress
*progress
;
1006 uint64_t progress_cnt
;
1008 char *base_graph_name
;
1009 int num_commit_graphs_before
;
1010 int num_commit_graphs_after
;
1011 char **commit_graph_filenames_before
;
1012 char **commit_graph_filenames_after
;
1013 char **commit_graph_hash_after
;
1014 uint32_t new_num_commits_in_base
;
1015 struct commit_graph
*new_base_graph
;
1022 write_generation_data
:1,
1023 trust_generation_numbers
:1;
1025 struct topo_level_slab
*topo_levels
;
1026 const struct commit_graph_opts
*opts
;
1027 size_t total_bloom_filter_data_size
;
1028 const struct bloom_filter_settings
*bloom_settings
;
1030 int count_bloom_filter_computed
;
1031 int count_bloom_filter_not_computed
;
1032 int count_bloom_filter_trunc_empty
;
1033 int count_bloom_filter_trunc_large
;
1036 static int write_graph_chunk_fanout(struct hashfile
*f
,
1039 struct write_commit_graph_context
*ctx
= data
;
1041 struct commit
**list
= ctx
->commits
.list
;
1044 * Write the first-level table (the list is sorted,
1045 * but we use a 256-entry lookup to be able to avoid
1046 * having to do eight extra binary search iterations).
1048 for (i
= 0; i
< 256; i
++) {
1049 while (count
< ctx
->commits
.nr
) {
1050 if ((*list
)->object
.oid
.hash
[0] != i
)
1052 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1057 hashwrite_be32(f
, count
);
1063 static int write_graph_chunk_oids(struct hashfile
*f
,
1066 struct write_commit_graph_context
*ctx
= data
;
1067 struct commit
**list
= ctx
->commits
.list
;
1069 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1070 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1071 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1077 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1079 const struct commit
* const *commits
= table
;
1080 return &commits
[index
]->object
.oid
;
1083 static int write_graph_chunk_data(struct hashfile
*f
,
1086 struct write_commit_graph_context
*ctx
= data
;
1087 struct commit
**list
= ctx
->commits
.list
;
1088 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1089 uint32_t num_extra_edges
= 0;
1091 while (list
< last
) {
1092 struct commit_list
*parent
;
1093 struct object_id
*tree
;
1095 uint32_t packedDate
[2];
1096 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1098 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1099 die(_("unable to parse commit %s"),
1100 oid_to_hex(&(*list
)->object
.oid
));
1101 tree
= get_commit_tree_oid(*list
);
1102 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1104 parent
= (*list
)->parents
;
1107 edge_value
= GRAPH_PARENT_NONE
;
1109 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1114 if (edge_value
>= 0)
1115 edge_value
+= ctx
->new_num_commits_in_base
;
1116 else if (ctx
->new_base_graph
) {
1118 if (find_commit_pos_in_graph(parent
->item
,
1119 ctx
->new_base_graph
,
1125 BUG("missing parent %s for commit %s",
1126 oid_to_hex(&parent
->item
->object
.oid
),
1127 oid_to_hex(&(*list
)->object
.oid
));
1130 hashwrite_be32(f
, edge_value
);
1133 parent
= parent
->next
;
1136 edge_value
= GRAPH_PARENT_NONE
;
1137 else if (parent
->next
)
1138 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1140 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1145 if (edge_value
>= 0)
1146 edge_value
+= ctx
->new_num_commits_in_base
;
1147 else if (ctx
->new_base_graph
) {
1149 if (find_commit_pos_in_graph(parent
->item
,
1150 ctx
->new_base_graph
,
1156 BUG("missing parent %s for commit %s",
1157 oid_to_hex(&parent
->item
->object
.oid
),
1158 oid_to_hex(&(*list
)->object
.oid
));
1161 hashwrite_be32(f
, edge_value
);
1163 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1166 parent
= parent
->next
;
1170 if (sizeof((*list
)->date
) > 4)
1171 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1175 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1177 packedDate
[1] = htonl((*list
)->date
);
1178 hashwrite(f
, packedDate
, 8);
1186 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1189 struct write_commit_graph_context
*ctx
= data
;
1190 int i
, num_generation_data_overflows
= 0;
1192 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1193 struct commit
*c
= ctx
->commits
.list
[i
];
1195 repo_parse_commit(ctx
->r
, c
);
1196 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1197 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1199 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1200 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1201 num_generation_data_overflows
++;
1204 hashwrite_be32(f
, offset
);
1210 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1213 struct write_commit_graph_context
*ctx
= data
;
1215 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1216 struct commit
*c
= ctx
->commits
.list
[i
];
1217 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1218 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1220 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1221 hashwrite_be32(f
, offset
>> 32);
1222 hashwrite_be32(f
, (uint32_t) offset
);
1229 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1232 struct write_commit_graph_context
*ctx
= data
;
1233 struct commit
**list
= ctx
->commits
.list
;
1234 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1235 struct commit_list
*parent
;
1237 while (list
< last
) {
1238 int num_parents
= 0;
1240 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1242 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1243 parent
= parent
->next
)
1246 if (num_parents
<= 2) {
1251 /* Since num_parents > 2, this initializer is safe. */
1252 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1253 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1258 if (edge_value
>= 0)
1259 edge_value
+= ctx
->new_num_commits_in_base
;
1260 else if (ctx
->new_base_graph
) {
1262 if (find_commit_pos_in_graph(parent
->item
,
1263 ctx
->new_base_graph
,
1269 BUG("missing parent %s for commit %s",
1270 oid_to_hex(&parent
->item
->object
.oid
),
1271 oid_to_hex(&(*list
)->object
.oid
));
1272 else if (!parent
->next
)
1273 edge_value
|= GRAPH_LAST_EDGE
;
1275 hashwrite_be32(f
, edge_value
);
1284 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1287 struct write_commit_graph_context
*ctx
= data
;
1288 struct commit
**list
= ctx
->commits
.list
;
1289 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1290 uint32_t cur_pos
= 0;
1292 while (list
< last
) {
1293 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1294 size_t len
= filter
? filter
->len
: 0;
1296 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1297 hashwrite_be32(f
, cur_pos
);
1304 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1306 struct json_writer jw
= JSON_WRITER_INIT
;
1308 jw_object_begin(&jw
, 0);
1309 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1310 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1311 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1312 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1315 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1320 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1323 struct write_commit_graph_context
*ctx
= data
;
1324 struct commit
**list
= ctx
->commits
.list
;
1325 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1327 trace2_bloom_filter_settings(ctx
);
1329 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1330 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1331 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1333 while (list
< last
) {
1334 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1335 size_t len
= filter
? filter
->len
: 0;
1337 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1339 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1346 static int add_packed_commits(const struct object_id
*oid
,
1347 struct packed_git
*pack
,
1351 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1352 enum object_type type
;
1353 off_t offset
= nth_packed_object_offset(pack
, pos
);
1354 struct object_info oi
= OBJECT_INFO_INIT
;
1357 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1360 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1361 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1363 if (type
!= OBJ_COMMIT
)
1366 oid_array_append(&ctx
->oids
, oid
);
1367 set_commit_pos(ctx
->r
, oid
);
1372 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1374 struct commit_list
*parent
;
1375 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1376 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1377 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1378 parent
->item
->object
.flags
|= REACHABLE
;
1383 static void close_reachable(struct write_commit_graph_context
*ctx
)
1386 struct commit
*commit
;
1387 enum commit_graph_split_flags flags
= ctx
->opts
?
1388 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1390 if (ctx
->report_progress
)
1391 ctx
->progress
= start_delayed_progress(
1392 _("Loading known commits in commit graph"),
1394 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1395 display_progress(ctx
->progress
, i
+ 1);
1396 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1398 commit
->object
.flags
|= REACHABLE
;
1400 stop_progress(&ctx
->progress
);
1403 * As this loop runs, ctx->oids.nr may grow, but not more
1404 * than the number of missing commits in the reachable
1407 if (ctx
->report_progress
)
1408 ctx
->progress
= start_delayed_progress(
1409 _("Expanding reachable commits in commit graph"),
1411 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1412 display_progress(ctx
->progress
, i
+ 1);
1413 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1418 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1419 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1420 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1421 add_missing_parents(ctx
, commit
);
1422 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1423 add_missing_parents(ctx
, commit
);
1425 stop_progress(&ctx
->progress
);
1427 if (ctx
->report_progress
)
1428 ctx
->progress
= start_delayed_progress(
1429 _("Clearing commit marks in commit graph"),
1431 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1432 display_progress(ctx
->progress
, i
+ 1);
1433 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1436 commit
->object
.flags
&= ~REACHABLE
;
1438 stop_progress(&ctx
->progress
);
1441 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1444 struct commit_list
*list
= NULL
;
1446 if (ctx
->report_progress
)
1447 ctx
->progress
= start_delayed_progress(
1448 _("Computing commit graph topological levels"),
1450 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1451 struct commit
*c
= ctx
->commits
.list
[i
];
1454 repo_parse_commit(ctx
->r
, c
);
1455 level
= *topo_level_slab_at(ctx
->topo_levels
, c
);
1457 display_progress(ctx
->progress
, i
+ 1);
1458 if (level
!= GENERATION_NUMBER_ZERO
)
1461 commit_list_insert(c
, &list
);
1463 struct commit
*current
= list
->item
;
1464 struct commit_list
*parent
;
1465 int all_parents_computed
= 1;
1466 uint32_t max_level
= 0;
1468 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1469 repo_parse_commit(ctx
->r
, parent
->item
);
1470 level
= *topo_level_slab_at(ctx
->topo_levels
, parent
->item
);
1472 if (level
== GENERATION_NUMBER_ZERO
) {
1473 all_parents_computed
= 0;
1474 commit_list_insert(parent
->item
, &list
);
1478 if (level
> max_level
)
1482 if (all_parents_computed
) {
1485 if (max_level
> GENERATION_NUMBER_V1_MAX
- 1)
1486 max_level
= GENERATION_NUMBER_V1_MAX
- 1;
1487 *topo_level_slab_at(ctx
->topo_levels
, current
) = max_level
+ 1;
1491 stop_progress(&ctx
->progress
);
1494 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1497 struct commit_list
*list
= NULL
;
1499 if (ctx
->report_progress
)
1500 ctx
->progress
= start_delayed_progress(
1501 _("Computing commit graph generation numbers"),
1504 if (!ctx
->trust_generation_numbers
) {
1505 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1506 struct commit
*c
= ctx
->commits
.list
[i
];
1507 repo_parse_commit(ctx
->r
, c
);
1508 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1512 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1513 struct commit
*c
= ctx
->commits
.list
[i
];
1514 timestamp_t corrected_commit_date
;
1516 repo_parse_commit(ctx
->r
, c
);
1517 corrected_commit_date
= commit_graph_data_at(c
)->generation
;
1519 display_progress(ctx
->progress
, i
+ 1);
1520 if (corrected_commit_date
!= GENERATION_NUMBER_ZERO
)
1523 commit_list_insert(c
, &list
);
1525 struct commit
*current
= list
->item
;
1526 struct commit_list
*parent
;
1527 int all_parents_computed
= 1;
1528 timestamp_t max_corrected_commit_date
= 0;
1530 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1531 repo_parse_commit(ctx
->r
, parent
->item
);
1532 corrected_commit_date
= commit_graph_data_at(parent
->item
)->generation
;
1534 if (corrected_commit_date
== GENERATION_NUMBER_ZERO
) {
1535 all_parents_computed
= 0;
1536 commit_list_insert(parent
->item
, &list
);
1540 if (corrected_commit_date
> max_corrected_commit_date
)
1541 max_corrected_commit_date
= corrected_commit_date
;
1544 if (all_parents_computed
) {
1547 if (current
->date
&& current
->date
> max_corrected_commit_date
)
1548 max_corrected_commit_date
= current
->date
- 1;
1549 commit_graph_data_at(current
)->generation
= max_corrected_commit_date
+ 1;
1554 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1555 struct commit
*c
= ctx
->commits
.list
[i
];
1556 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1557 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1558 ctx
->num_generation_data_overflows
++;
1560 stop_progress(&ctx
->progress
);
1563 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1565 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1566 ctx
->count_bloom_filter_computed
);
1567 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1568 ctx
->count_bloom_filter_not_computed
);
1569 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1570 ctx
->count_bloom_filter_trunc_empty
);
1571 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1572 ctx
->count_bloom_filter_trunc_large
);
1575 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1578 struct progress
*progress
= NULL
;
1579 struct commit
**sorted_commits
;
1580 int max_new_filters
;
1582 init_bloom_filters();
1584 if (ctx
->report_progress
)
1585 progress
= start_delayed_progress(
1586 _("Computing commit changed paths Bloom filters"),
1589 ALLOC_ARRAY(sorted_commits
, ctx
->commits
.nr
);
1590 COPY_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1592 if (ctx
->order_by_pack
)
1593 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1595 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1597 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1598 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1600 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1601 enum bloom_filter_computed computed
= 0;
1602 struct commit
*c
= sorted_commits
[i
];
1603 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1606 ctx
->count_bloom_filter_computed
< max_new_filters
,
1607 ctx
->bloom_settings
,
1609 if (computed
& BLOOM_COMPUTED
) {
1610 ctx
->count_bloom_filter_computed
++;
1611 if (computed
& BLOOM_TRUNC_EMPTY
)
1612 ctx
->count_bloom_filter_trunc_empty
++;
1613 if (computed
& BLOOM_TRUNC_LARGE
)
1614 ctx
->count_bloom_filter_trunc_large
++;
1615 } else if (computed
& BLOOM_NOT_COMPUTED
)
1616 ctx
->count_bloom_filter_not_computed
++;
1617 ctx
->total_bloom_filter_data_size
+= filter
1618 ? sizeof(unsigned char) * filter
->len
: 0;
1619 display_progress(progress
, i
+ 1);
1622 if (trace2_is_enabled())
1623 trace2_bloom_filter_write_statistics(ctx
);
1625 free(sorted_commits
);
1626 stop_progress(&progress
);
1629 struct refs_cb_data
{
1630 struct oidset
*commits
;
1631 struct progress
*progress
;
1634 static int add_ref_to_set(const char *refname
,
1635 const struct object_id
*oid
,
1636 int flags
, void *cb_data
)
1638 struct object_id peeled
;
1639 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1641 if (!peel_iterated_oid(oid
, &peeled
))
1643 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1644 oidset_insert(data
->commits
, oid
);
1646 display_progress(data
->progress
, oidset_size(data
->commits
));
1651 int write_commit_graph_reachable(struct object_directory
*odb
,
1652 enum commit_graph_write_flags flags
,
1653 const struct commit_graph_opts
*opts
)
1655 struct oidset commits
= OIDSET_INIT
;
1656 struct refs_cb_data data
;
1659 memset(&data
, 0, sizeof(data
));
1660 data
.commits
= &commits
;
1661 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1662 data
.progress
= start_delayed_progress(
1663 _("Collecting referenced commits"), 0);
1665 for_each_ref(add_ref_to_set
, &data
);
1667 stop_progress(&data
.progress
);
1669 result
= write_commit_graph(odb
, NULL
, &commits
,
1672 oidset_clear(&commits
);
1676 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1677 const struct string_list
*pack_indexes
)
1680 struct strbuf progress_title
= STRBUF_INIT
;
1681 struct strbuf packname
= STRBUF_INIT
;
1685 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1686 dirlen
= packname
.len
;
1687 if (ctx
->report_progress
) {
1688 strbuf_addf(&progress_title
,
1689 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1690 "Finding commits for commit graph in %"PRIuMAX
" packs",
1692 (uintmax_t)pack_indexes
->nr
);
1693 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1694 ctx
->progress_done
= 0;
1696 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1697 struct packed_git
*p
;
1698 strbuf_setlen(&packname
, dirlen
);
1699 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1700 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1702 ret
= error(_("error adding pack %s"), packname
.buf
);
1705 if (open_pack_index(p
)) {
1706 ret
= error(_("error opening index for %s"), packname
.buf
);
1709 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1710 FOR_EACH_OBJECT_PACK_ORDER
);
1716 stop_progress(&ctx
->progress
);
1717 strbuf_release(&progress_title
);
1718 strbuf_release(&packname
);
1723 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1724 struct oidset
*commits
)
1726 struct oidset_iter iter
;
1727 struct object_id
*oid
;
1729 if (!oidset_size(commits
))
1732 oidset_iter_init(commits
, &iter
);
1733 while ((oid
= oidset_iter_next(&iter
))) {
1734 oid_array_append(&ctx
->oids
, oid
);
1740 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1742 if (ctx
->report_progress
)
1743 ctx
->progress
= start_delayed_progress(
1744 _("Finding commits for commit graph among packed objects"),
1745 ctx
->approx_nr_objects
);
1746 for_each_packed_object(add_packed_commits
, ctx
,
1747 FOR_EACH_OBJECT_PACK_ORDER
);
1748 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1749 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1750 stop_progress(&ctx
->progress
);
1753 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1756 enum commit_graph_split_flags flags
= ctx
->opts
?
1757 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1759 ctx
->num_extra_edges
= 0;
1760 if (ctx
->report_progress
)
1761 ctx
->progress
= start_delayed_progress(
1762 _("Finding extra edges in commit graph"),
1764 oid_array_sort(&ctx
->oids
);
1765 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1766 unsigned int num_parents
;
1768 display_progress(ctx
->progress
, i
+ 1);
1770 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1771 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1773 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1774 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1777 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1778 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1780 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1782 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1783 if (num_parents
> 2)
1784 ctx
->num_extra_edges
+= num_parents
- 1;
1788 stop_progress(&ctx
->progress
);
1791 static int write_graph_chunk_base_1(struct hashfile
*f
,
1792 struct commit_graph
*g
)
1799 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1800 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1804 static int write_graph_chunk_base(struct hashfile
*f
,
1807 struct write_commit_graph_context
*ctx
= data
;
1808 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1810 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1811 error(_("failed to write correct number of base graph ids"));
1818 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1823 struct lock_file lk
= LOCK_INIT
;
1824 const unsigned hashsz
= the_hash_algo
->rawsz
;
1825 struct strbuf progress_title
= STRBUF_INIT
;
1826 struct chunkfile
*cf
;
1827 unsigned char file_hash
[GIT_MAX_RAWSZ
];
1830 struct strbuf tmp_file
= STRBUF_INIT
;
1832 strbuf_addf(&tmp_file
,
1833 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1835 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1837 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1840 if (safe_create_leading_directories(ctx
->graph_name
)) {
1841 UNLEAK(ctx
->graph_name
);
1842 error(_("unable to create leading directories of %s"),
1848 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1850 hold_lock_file_for_update_mode(&lk
, lock_name
,
1851 LOCK_DIE_ON_ERROR
, 0444);
1854 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1856 error(_("unable to create temporary graph layer"));
1860 if (adjust_shared_perm(ctx
->graph_name
)) {
1861 error(_("unable to adjust shared permissions for '%s'"),
1866 f
= hashfd(fd
, ctx
->graph_name
);
1868 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1869 LOCK_DIE_ON_ERROR
, 0444);
1870 fd
= get_lock_file_fd(&lk
);
1871 f
= hashfd(fd
, get_lock_file_path(&lk
));
1874 cf
= init_chunkfile(f
);
1876 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
1877 write_graph_chunk_fanout
);
1878 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, hashsz
* ctx
->commits
.nr
,
1879 write_graph_chunk_oids
);
1880 add_chunk(cf
, GRAPH_CHUNKID_DATA
, (hashsz
+ 16) * ctx
->commits
.nr
,
1881 write_graph_chunk_data
);
1883 if (ctx
->write_generation_data
)
1884 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
1885 sizeof(uint32_t) * ctx
->commits
.nr
,
1886 write_graph_chunk_generation_data
);
1887 if (ctx
->num_generation_data_overflows
)
1888 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
1889 sizeof(timestamp_t
) * ctx
->num_generation_data_overflows
,
1890 write_graph_chunk_generation_data_overflow
);
1891 if (ctx
->num_extra_edges
)
1892 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
1893 4 * ctx
->num_extra_edges
,
1894 write_graph_chunk_extra_edges
);
1895 if (ctx
->changed_paths
) {
1896 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
1897 sizeof(uint32_t) * ctx
->commits
.nr
,
1898 write_graph_chunk_bloom_indexes
);
1899 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
1900 sizeof(uint32_t) * 3
1901 + ctx
->total_bloom_filter_data_size
,
1902 write_graph_chunk_bloom_data
);
1904 if (ctx
->num_commit_graphs_after
> 1)
1905 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
1906 hashsz
* (ctx
->num_commit_graphs_after
- 1),
1907 write_graph_chunk_base
);
1909 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1911 hashwrite_u8(f
, GRAPH_VERSION
);
1912 hashwrite_u8(f
, oid_version(the_hash_algo
));
1913 hashwrite_u8(f
, get_num_chunks(cf
));
1914 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1916 if (ctx
->report_progress
) {
1917 strbuf_addf(&progress_title
,
1918 Q_("Writing out commit graph in %d pass",
1919 "Writing out commit graph in %d passes",
1920 get_num_chunks(cf
)),
1921 get_num_chunks(cf
));
1922 ctx
->progress
= start_delayed_progress(
1924 get_num_chunks(cf
) * ctx
->commits
.nr
);
1927 write_chunkfile(cf
, ctx
);
1929 stop_progress(&ctx
->progress
);
1930 strbuf_release(&progress_title
);
1932 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1933 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1934 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
1936 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1937 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1938 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1939 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1942 close_commit_graph(ctx
->r
->objects
);
1943 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
1944 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1948 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1949 char *final_graph_name
;
1955 error(_("unable to open commit-graph chain file"));
1959 if (ctx
->base_graph_name
) {
1961 int idx
= ctx
->num_commit_graphs_after
- 1;
1962 if (ctx
->num_commit_graphs_after
> 1)
1965 dest
= ctx
->commit_graph_filenames_after
[idx
];
1967 if (strcmp(ctx
->base_graph_name
, dest
)) {
1968 result
= rename(ctx
->base_graph_name
, dest
);
1971 error(_("failed to rename base commit-graph file"));
1976 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
1981 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
1982 final_graph_name
= get_split_graph_filename(ctx
->odb
,
1983 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1984 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
1986 result
= rename(ctx
->graph_name
, final_graph_name
);
1988 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
1989 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
1992 error(_("failed to rename temporary commit-graph file"));
1997 commit_lock_file(&lk
);
2002 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2004 struct commit_graph
*g
;
2005 uint32_t num_commits
;
2006 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2009 int max_commits
= 0;
2013 max_commits
= ctx
->opts
->max_commits
;
2015 if (ctx
->opts
->size_multiple
)
2016 size_mult
= ctx
->opts
->size_multiple
;
2018 flags
= ctx
->opts
->split_flags
;
2021 g
= ctx
->r
->objects
->commit_graph
;
2022 num_commits
= ctx
->commits
.nr
;
2023 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2024 ctx
->num_commit_graphs_after
= 1;
2026 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2028 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2029 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2030 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
2031 (max_commits
&& num_commits
> max_commits
))) {
2032 if (g
->odb
!= ctx
->odb
)
2035 num_commits
+= g
->num_commits
;
2038 ctx
->num_commit_graphs_after
--;
2042 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2043 ctx
->new_base_graph
= g
;
2044 else if (ctx
->num_commit_graphs_after
!= 1)
2045 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2046 "should be 1 with --split=replace");
2048 if (ctx
->num_commit_graphs_after
== 2) {
2049 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2051 if (!strcmp(g
->filename
, old_graph_name
) &&
2052 g
->odb
!= ctx
->odb
) {
2053 ctx
->num_commit_graphs_after
= 1;
2054 ctx
->new_base_graph
= NULL
;
2057 free(old_graph_name
);
2060 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2061 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2063 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2064 i
< ctx
->num_commit_graphs_before
; i
++)
2065 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2067 i
= ctx
->num_commit_graphs_before
- 1;
2068 g
= ctx
->r
->objects
->commit_graph
;
2071 if (i
< ctx
->num_commit_graphs_after
)
2072 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2075 * If the topmost remaining layer has generation data chunk, the
2076 * resultant layer also has generation data chunk.
2078 if (i
== ctx
->num_commit_graphs_after
- 2)
2079 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2086 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2087 struct commit_graph
*g
)
2090 uint32_t offset
= g
->num_commits_in_base
;
2092 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2094 for (i
= 0; i
< g
->num_commits
; i
++) {
2095 struct object_id oid
;
2096 struct commit
*result
;
2098 display_progress(ctx
->progress
, i
+ 1);
2100 load_oid_from_graph(g
, i
+ offset
, &oid
);
2102 /* only add commits if they still exist in the repo */
2103 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2106 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2112 static int commit_compare(const void *_a
, const void *_b
)
2114 const struct commit
*a
= *(const struct commit
**)_a
;
2115 const struct commit
*b
= *(const struct commit
**)_b
;
2116 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2119 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2121 uint32_t i
, dedup_i
= 0;
2123 if (ctx
->report_progress
)
2124 ctx
->progress
= start_delayed_progress(
2125 _("Scanning merged commits"),
2128 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2130 ctx
->num_extra_edges
= 0;
2131 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2132 display_progress(ctx
->progress
, i
+ 1);
2134 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2135 &ctx
->commits
.list
[i
]->object
.oid
)) {
2137 * Silently ignore duplicates. These were likely
2138 * created due to a commit appearing in multiple
2139 * layers of the chain, which is unexpected but
2140 * not invalid. We should make sure there is a
2141 * unique copy in the new layer.
2144 unsigned int num_parents
;
2146 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2149 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2150 if (num_parents
> 2)
2151 ctx
->num_extra_edges
+= num_parents
- 1;
2155 ctx
->commits
.nr
= dedup_i
;
2157 stop_progress(&ctx
->progress
);
2160 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2162 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2163 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2165 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2166 current_graph_number
--;
2168 if (ctx
->report_progress
)
2169 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2171 merge_commit_graph(ctx
, g
);
2172 stop_progress(&ctx
->progress
);
2178 ctx
->new_base_graph
= g
;
2179 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2182 if (ctx
->new_base_graph
)
2183 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2185 sort_and_scan_merged_commits(ctx
);
2188 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2191 time_t now
= time(NULL
);
2193 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2195 struct utimbuf updated_time
;
2197 stat(ctx
->commit_graph_filenames_before
[i
], &st
);
2199 updated_time
.actime
= st
.st_atime
;
2200 updated_time
.modtime
= now
;
2201 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2205 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2207 struct strbuf path
= STRBUF_INIT
;
2211 timestamp_t expire_time
= time(NULL
);
2213 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2214 expire_time
= ctx
->opts
->expire_time
;
2216 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2217 unlink(chain_file_name
);
2218 free(chain_file_name
);
2219 ctx
->num_commit_graphs_after
= 0;
2222 strbuf_addstr(&path
, ctx
->odb
->path
);
2223 strbuf_addstr(&path
, "/info/commit-graphs");
2224 dir
= opendir(path
.buf
);
2229 strbuf_addch(&path
, '/');
2230 dirnamelen
= path
.len
;
2231 while ((de
= readdir(dir
)) != NULL
) {
2233 uint32_t i
, found
= 0;
2235 strbuf_setlen(&path
, dirnamelen
);
2236 strbuf_addstr(&path
, de
->d_name
);
2238 stat(path
.buf
, &st
);
2240 if (st
.st_mtime
> expire_time
)
2242 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2245 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2246 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2258 strbuf_release(&path
);
2261 int write_commit_graph(struct object_directory
*odb
,
2262 const struct string_list
*const pack_indexes
,
2263 struct oidset
*commits
,
2264 enum commit_graph_write_flags flags
,
2265 const struct commit_graph_opts
*opts
)
2267 struct repository
*r
= the_repository
;
2268 struct write_commit_graph_context
*ctx
;
2272 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2273 struct topo_level_slab topo_levels
;
2275 prepare_repo_settings(r
);
2276 if (!r
->settings
.core_commit_graph
) {
2277 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2280 if (!commit_graph_compatible(r
))
2283 CALLOC_ARRAY(ctx
, 1);
2286 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2287 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2288 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2290 ctx
->total_bloom_filter_data_size
= 0;
2291 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2292 ctx
->num_generation_data_overflows
= 0;
2294 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2295 bloom_settings
.bits_per_entry
);
2296 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2297 bloom_settings
.num_hashes
);
2298 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2299 bloom_settings
.max_changed_paths
);
2300 ctx
->bloom_settings
= &bloom_settings
;
2302 init_topo_level_slab(&topo_levels
);
2303 ctx
->topo_levels
= &topo_levels
;
2305 prepare_commit_graph(ctx
->r
);
2306 if (ctx
->r
->objects
->commit_graph
) {
2307 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2310 g
->topo_levels
= &topo_levels
;
2315 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2316 ctx
->changed_paths
= 1;
2317 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2318 struct commit_graph
*g
;
2320 g
= ctx
->r
->objects
->commit_graph
;
2322 /* We have changed-paths already. Keep them in the next graph */
2323 if (g
&& g
->chunk_bloom_data
) {
2324 ctx
->changed_paths
= 1;
2325 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2330 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2333 ctx
->num_commit_graphs_before
++;
2337 if (ctx
->num_commit_graphs_before
) {
2338 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2339 i
= ctx
->num_commit_graphs_before
;
2340 g
= ctx
->r
->objects
->commit_graph
;
2343 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2349 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2352 ctx
->approx_nr_objects
= approximate_object_count();
2354 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2355 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2356 for (i
= 0; i
< g
->num_commits
; i
++) {
2357 struct object_id oid
;
2358 oidread(&oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2359 oid_array_append(&ctx
->oids
, &oid
);
2364 ctx
->order_by_pack
= 1;
2365 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2370 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2374 if (!pack_indexes
&& !commits
) {
2375 ctx
->order_by_pack
= 1;
2376 fill_oids_from_all_packs(ctx
);
2379 close_reachable(ctx
);
2381 copy_oids_to_commits(ctx
);
2383 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2384 error(_("too many commits to write graph"));
2389 if (!ctx
->commits
.nr
&& !replace
)
2393 split_graph_merge_strategy(ctx
);
2396 merge_commit_graphs(ctx
);
2398 ctx
->num_commit_graphs_after
= 1;
2400 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2402 compute_topological_levels(ctx
);
2403 if (ctx
->write_generation_data
)
2404 compute_generation_numbers(ctx
);
2406 if (ctx
->changed_paths
)
2407 compute_bloom_filters(ctx
);
2409 res
= write_commit_graph_file(ctx
);
2412 mark_commit_graphs(ctx
);
2414 expire_commit_graphs(ctx
);
2417 free(ctx
->graph_name
);
2418 free(ctx
->commits
.list
);
2419 oid_array_clear(&ctx
->oids
);
2420 clear_topo_level_slab(&topo_levels
);
2422 if (ctx
->commit_graph_filenames_after
) {
2423 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2424 free(ctx
->commit_graph_filenames_after
[i
]);
2425 free(ctx
->commit_graph_hash_after
[i
]);
2428 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2429 free(ctx
->commit_graph_filenames_before
[i
]);
2431 free(ctx
->commit_graph_filenames_after
);
2432 free(ctx
->commit_graph_filenames_before
);
2433 free(ctx
->commit_graph_hash_after
);
2441 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2442 static int verify_commit_graph_error
;
2444 __attribute__((format (printf
, 1, 2)))
2445 static void graph_report(const char *fmt
, ...)
2449 verify_commit_graph_error
= 1;
2451 vfprintf(stderr
, fmt
, ap
);
2452 fprintf(stderr
, "\n");
2456 #define GENERATION_ZERO_EXISTS 1
2457 #define GENERATION_NUMBER_EXISTS 2
2459 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2461 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2464 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2466 uint32_t i
, cur_fanout_pos
= 0;
2467 struct object_id prev_oid
, cur_oid
;
2468 int generation_zero
= 0;
2469 struct progress
*progress
= NULL
;
2470 int local_error
= 0;
2473 graph_report("no commit-graph file loaded");
2477 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2478 if (verify_commit_graph_error
)
2479 return verify_commit_graph_error
;
2481 if (!commit_graph_checksum_valid(g
)) {
2482 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2483 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2486 for (i
= 0; i
< g
->num_commits
; i
++) {
2487 struct commit
*graph_commit
;
2489 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2491 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2492 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2493 oid_to_hex(&prev_oid
),
2494 oid_to_hex(&cur_oid
));
2496 oidcpy(&prev_oid
, &cur_oid
);
2498 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2499 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2501 if (i
!= fanout_value
)
2502 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2503 cur_fanout_pos
, fanout_value
, i
);
2507 graph_commit
= lookup_commit(r
, &cur_oid
);
2508 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2509 graph_report(_("failed to parse commit %s from commit-graph"),
2510 oid_to_hex(&cur_oid
));
2513 while (cur_fanout_pos
< 256) {
2514 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2516 if (g
->num_commits
!= fanout_value
)
2517 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2518 cur_fanout_pos
, fanout_value
, i
);
2523 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2524 return verify_commit_graph_error
;
2526 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2527 progress
= start_progress(_("Verifying commits in commit graph"),
2530 for (i
= 0; i
< g
->num_commits
; i
++) {
2531 struct commit
*graph_commit
, *odb_commit
;
2532 struct commit_list
*graph_parents
, *odb_parents
;
2533 timestamp_t max_generation
= 0;
2534 timestamp_t generation
;
2536 display_progress(progress
, i
+ 1);
2537 oidread(&cur_oid
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2539 graph_commit
= lookup_commit(r
, &cur_oid
);
2540 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2541 if (parse_commit_internal(odb_commit
, 0, 0)) {
2542 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2543 oid_to_hex(&cur_oid
));
2547 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2548 get_commit_tree_oid(odb_commit
)))
2549 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2550 oid_to_hex(&cur_oid
),
2551 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2552 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2554 graph_parents
= graph_commit
->parents
;
2555 odb_parents
= odb_commit
->parents
;
2557 while (graph_parents
) {
2558 if (odb_parents
== NULL
) {
2559 graph_report(_("commit-graph parent list for commit %s is too long"),
2560 oid_to_hex(&cur_oid
));
2564 /* parse parent in case it is in a base graph */
2565 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2567 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2568 graph_report(_("commit-graph parent for %s is %s != %s"),
2569 oid_to_hex(&cur_oid
),
2570 oid_to_hex(&graph_parents
->item
->object
.oid
),
2571 oid_to_hex(&odb_parents
->item
->object
.oid
));
2573 generation
= commit_graph_generation(graph_parents
->item
);
2574 if (generation
> max_generation
)
2575 max_generation
= generation
;
2577 graph_parents
= graph_parents
->next
;
2578 odb_parents
= odb_parents
->next
;
2581 if (odb_parents
!= NULL
)
2582 graph_report(_("commit-graph parent list for commit %s terminates early"),
2583 oid_to_hex(&cur_oid
));
2585 if (!commit_graph_generation(graph_commit
)) {
2586 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2587 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2588 oid_to_hex(&cur_oid
));
2589 generation_zero
= GENERATION_ZERO_EXISTS
;
2590 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2591 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2592 oid_to_hex(&cur_oid
));
2594 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2598 * If we are using topological level and one of our parents has
2599 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2600 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2601 * in the following condition.
2603 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2606 generation
= commit_graph_generation(graph_commit
);
2607 if (generation
< max_generation
+ 1)
2608 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2609 oid_to_hex(&cur_oid
),
2611 max_generation
+ 1);
2613 if (graph_commit
->date
!= odb_commit
->date
)
2614 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2615 oid_to_hex(&cur_oid
),
2619 stop_progress(&progress
);
2621 local_error
= verify_commit_graph_error
;
2623 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2624 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2629 void free_commit_graph(struct commit_graph
*g
)
2634 munmap((void *)g
->data
, g
->data_len
);
2638 free(g
->bloom_filter_settings
);
2642 void disable_commit_graph(struct repository
*r
)
2644 r
->commit_graph_disabled
= 1;