1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
13 #include "hash-lookup.h"
14 #include "commit-graph.h"
15 #include "object-file.h"
16 #include "object-store-ll.h"
17 #include "oid-array.h"
21 #include "replace-object.h"
24 #include "commit-slab.h"
26 #include "json-writer.h"
29 #include "chunk-format.h"
31 void git_test_write_commit_graph_or_die(void)
34 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0))
37 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
38 flags
= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
40 if (write_commit_graph_reachable(the_repository
->objects
->odb
,
42 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
45 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
46 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
47 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
48 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
49 #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */
50 #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */
51 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
52 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
53 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
54 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
56 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
58 #define GRAPH_VERSION_1 0x1
59 #define GRAPH_VERSION GRAPH_VERSION_1
61 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
62 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
63 #define GRAPH_PARENT_NONE 0x70000000
65 #define GRAPH_LAST_EDGE 0x80000000
67 #define GRAPH_HEADER_SIZE 8
68 #define GRAPH_FANOUT_SIZE (4 * 256)
69 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \
70 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
72 #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31)
74 /* Remember to update object flag allocation in object.h */
75 #define REACHABLE (1u<<15)
77 define_commit_slab(topo_level_slab
, uint32_t);
79 /* Keep track of the order in which commits are added to our list. */
80 define_commit_slab(commit_pos
, int);
81 static struct commit_pos commit_pos
= COMMIT_SLAB_INIT(1, commit_pos
);
83 static void set_commit_pos(struct repository
*r
, const struct object_id
*oid
)
85 static int32_t max_pos
;
86 struct commit
*commit
= lookup_commit(r
, oid
);
89 return; /* should never happen, but be lenient */
91 *commit_pos_at(&commit_pos
, commit
) = max_pos
++;
94 static int commit_pos_cmp(const void *va
, const void *vb
)
96 const struct commit
*a
= *(const struct commit
**)va
;
97 const struct commit
*b
= *(const struct commit
**)vb
;
98 return commit_pos_at(&commit_pos
, a
) -
99 commit_pos_at(&commit_pos
, b
);
102 define_commit_slab(commit_graph_data_slab
, struct commit_graph_data
);
103 static struct commit_graph_data_slab commit_graph_data_slab
=
104 COMMIT_SLAB_INIT(1, commit_graph_data_slab
);
106 static int get_configured_generation_version(struct repository
*r
)
109 repo_config_get_int(r
, "commitgraph.generationversion", &version
);
113 uint32_t commit_graph_position(const struct commit
*c
)
115 struct commit_graph_data
*data
=
116 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
118 return data
? data
->graph_pos
: COMMIT_NOT_FROM_GRAPH
;
121 timestamp_t
commit_graph_generation(const struct commit
*c
)
123 struct commit_graph_data
*data
=
124 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
126 if (data
&& data
->generation
)
127 return data
->generation
;
129 return GENERATION_NUMBER_INFINITY
;
132 static timestamp_t
commit_graph_generation_from_graph(const struct commit
*c
)
134 struct commit_graph_data
*data
=
135 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
137 if (!data
|| data
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
138 return GENERATION_NUMBER_INFINITY
;
139 return data
->generation
;
142 static struct commit_graph_data
*commit_graph_data_at(const struct commit
*c
)
144 unsigned int i
, nth_slab
;
145 struct commit_graph_data
*data
=
146 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
151 nth_slab
= c
->index
/ commit_graph_data_slab
.slab_size
;
152 data
= commit_graph_data_slab_at(&commit_graph_data_slab
, c
);
155 * commit-slab initializes elements with zero, overwrite this with
156 * COMMIT_NOT_FROM_GRAPH for graph_pos.
158 * We avoid initializing generation with checking if graph position
159 * is not COMMIT_NOT_FROM_GRAPH.
161 for (i
= 0; i
< commit_graph_data_slab
.slab_size
; i
++) {
162 commit_graph_data_slab
.slab
[nth_slab
][i
].graph_pos
=
163 COMMIT_NOT_FROM_GRAPH
;
170 * Should be used only while writing commit-graph as it compares
171 * generation value of commits by directly accessing commit-slab.
173 static int commit_gen_cmp(const void *va
, const void *vb
)
175 const struct commit
*a
= *(const struct commit
**)va
;
176 const struct commit
*b
= *(const struct commit
**)vb
;
178 const timestamp_t generation_a
= commit_graph_data_at(a
)->generation
;
179 const timestamp_t generation_b
= commit_graph_data_at(b
)->generation
;
180 /* lower generation commits first */
181 if (generation_a
< generation_b
)
183 else if (generation_a
> generation_b
)
186 /* use date as a heuristic when generations are equal */
187 if (a
->date
< b
->date
)
189 else if (a
->date
> b
->date
)
194 char *get_commit_graph_filename(struct object_directory
*obj_dir
)
196 return xstrfmt("%s/info/commit-graph", obj_dir
->path
);
199 static char *get_split_graph_filename(struct object_directory
*odb
,
202 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb
->path
,
206 char *get_commit_graph_chain_filename(struct object_directory
*odb
)
208 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb
->path
);
211 static struct commit_graph
*alloc_commit_graph(void)
213 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
218 static int commit_graph_compatible(struct repository
*r
)
223 if (replace_refs_enabled(r
)) {
224 prepare_replace_object(r
);
225 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
229 prepare_commit_graft(r
);
230 if (r
->parsed_objects
&&
231 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
233 if (is_repository_shallow(r
))
239 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
241 *fd
= git_open(graph_file
);
244 if (fstat(*fd
, st
)) {
251 struct commit_graph
*load_commit_graph_one_fd_st(struct repository
*r
,
252 int fd
, struct stat
*st
,
253 struct object_directory
*odb
)
257 struct commit_graph
*ret
;
259 graph_size
= xsize_t(st
->st_size
);
261 if (graph_size
< GRAPH_MIN_SIZE
) {
263 error(_("commit-graph file is too small"));
266 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
268 prepare_repo_settings(r
);
269 ret
= parse_commit_graph(&r
->settings
, graph_map
, graph_size
);
274 munmap(graph_map
, graph_size
);
279 static int graph_read_oid_fanout(const unsigned char *chunk_start
,
280 size_t chunk_size
, void *data
)
282 struct commit_graph
*g
= data
;
285 if (chunk_size
!= 256 * sizeof(uint32_t))
286 return error(_("commit-graph oid fanout chunk is wrong size"));
287 g
->chunk_oid_fanout
= (const uint32_t *)chunk_start
;
288 g
->num_commits
= ntohl(g
->chunk_oid_fanout
[255]);
290 for (i
= 0; i
< 255; i
++) {
291 uint32_t oid_fanout1
= ntohl(g
->chunk_oid_fanout
[i
]);
292 uint32_t oid_fanout2
= ntohl(g
->chunk_oid_fanout
[i
+ 1]);
294 if (oid_fanout1
> oid_fanout2
) {
295 error(_("commit-graph fanout values out of order"));
303 static int graph_read_oid_lookup(const unsigned char *chunk_start
,
304 size_t chunk_size
, void *data
)
306 struct commit_graph
*g
= data
;
307 g
->chunk_oid_lookup
= chunk_start
;
308 if (chunk_size
/ g
->hash_len
!= g
->num_commits
)
309 return error(_("commit-graph OID lookup chunk is the wrong size"));
313 static int graph_read_commit_data(const unsigned char *chunk_start
,
314 size_t chunk_size
, void *data
)
316 struct commit_graph
*g
= data
;
317 if (chunk_size
/ GRAPH_DATA_WIDTH
!= g
->num_commits
)
318 return error(_("commit-graph commit data chunk is wrong size"));
319 g
->chunk_commit_data
= chunk_start
;
323 static int graph_read_generation_data(const unsigned char *chunk_start
,
324 size_t chunk_size
, void *data
)
326 struct commit_graph
*g
= data
;
327 if (chunk_size
/ sizeof(uint32_t) != g
->num_commits
)
328 return error(_("commit-graph generations chunk is wrong size"));
329 g
->chunk_generation_data
= chunk_start
;
333 static int graph_read_bloom_index(const unsigned char *chunk_start
,
334 size_t chunk_size
, void *data
)
336 struct commit_graph
*g
= data
;
337 if (chunk_size
/ 4 != g
->num_commits
) {
338 warning(_("commit-graph changed-path index chunk is too small"));
341 g
->chunk_bloom_indexes
= chunk_start
;
345 static int graph_read_bloom_data(const unsigned char *chunk_start
,
346 size_t chunk_size
, void *data
)
348 struct commit_graph
*g
= data
;
349 uint32_t hash_version
;
351 if (chunk_size
< BLOOMDATA_CHUNK_HEADER_SIZE
) {
352 warning(_("ignoring too-small changed-path chunk"
353 " (%"PRIuMAX
" < %"PRIuMAX
") in commit-graph file"),
354 (uintmax_t)chunk_size
,
355 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE
);
359 g
->chunk_bloom_data
= chunk_start
;
360 g
->chunk_bloom_data_size
= chunk_size
;
361 hash_version
= get_be32(chunk_start
);
363 if (hash_version
!= 1)
366 g
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
367 g
->bloom_filter_settings
->hash_version
= hash_version
;
368 g
->bloom_filter_settings
->num_hashes
= get_be32(chunk_start
+ 4);
369 g
->bloom_filter_settings
->bits_per_entry
= get_be32(chunk_start
+ 8);
370 g
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
375 struct commit_graph
*parse_commit_graph(struct repo_settings
*s
,
376 void *graph_map
, size_t graph_size
)
378 const unsigned char *data
;
379 struct commit_graph
*graph
;
380 uint32_t graph_signature
;
381 unsigned char graph_version
, hash_version
;
382 struct chunkfile
*cf
= NULL
;
387 if (graph_size
< GRAPH_MIN_SIZE
)
390 data
= (const unsigned char *)graph_map
;
392 graph_signature
= get_be32(data
);
393 if (graph_signature
!= GRAPH_SIGNATURE
) {
394 error(_("commit-graph signature %X does not match signature %X"),
395 graph_signature
, GRAPH_SIGNATURE
);
399 graph_version
= *(unsigned char*)(data
+ 4);
400 if (graph_version
!= GRAPH_VERSION
) {
401 error(_("commit-graph version %X does not match version %X"),
402 graph_version
, GRAPH_VERSION
);
406 hash_version
= *(unsigned char*)(data
+ 5);
407 if (hash_version
!= oid_version(the_hash_algo
)) {
408 error(_("commit-graph hash version %X does not match version %X"),
409 hash_version
, oid_version(the_hash_algo
));
413 graph
= alloc_commit_graph();
415 graph
->hash_len
= the_hash_algo
->rawsz
;
416 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
417 graph
->data
= graph_map
;
418 graph
->data_len
= graph_size
;
420 if (graph_size
< GRAPH_HEADER_SIZE
+
421 (graph
->num_chunks
+ 1) * CHUNK_TOC_ENTRY_SIZE
+
422 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
423 error(_("commit-graph file is too small to hold %u chunks"),
429 cf
= init_chunkfile(NULL
);
431 if (read_table_of_contents(cf
, graph
->data
, graph_size
,
432 GRAPH_HEADER_SIZE
, graph
->num_chunks
, 1))
433 goto free_and_return
;
435 if (read_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, graph_read_oid_fanout
, graph
)) {
436 error(_("commit-graph required OID fanout chunk missing or corrupted"));
437 goto free_and_return
;
439 if (read_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, graph_read_oid_lookup
, graph
)) {
440 error(_("commit-graph required OID lookup chunk missing or corrupted"));
441 goto free_and_return
;
443 if (read_chunk(cf
, GRAPH_CHUNKID_DATA
, graph_read_commit_data
, graph
)) {
444 error(_("commit-graph required commit data chunk missing or corrupted"));
445 goto free_and_return
;
448 pair_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
, &graph
->chunk_extra_edges
,
449 &graph
->chunk_extra_edges_size
);
450 pair_chunk(cf
, GRAPH_CHUNKID_BASE
, &graph
->chunk_base_graphs
,
451 &graph
->chunk_base_graphs_size
);
453 if (s
->commit_graph_generation_version
>= 2) {
454 read_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
455 graph_read_generation_data
, graph
);
456 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
457 &graph
->chunk_generation_data_overflow
,
458 &graph
->chunk_generation_data_overflow_size
);
460 if (graph
->chunk_generation_data
)
461 graph
->read_generation_data
= 1;
464 if (s
->commit_graph_read_changed_paths
) {
465 read_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
466 graph_read_bloom_index
, graph
);
467 read_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
468 graph_read_bloom_data
, graph
);
471 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
472 init_bloom_filters();
474 /* We need both the bloom chunks to exist together. Else ignore the data */
475 graph
->chunk_bloom_indexes
= NULL
;
476 graph
->chunk_bloom_data
= NULL
;
477 FREE_AND_NULL(graph
->bloom_filter_settings
);
480 oidread(&graph
->oid
, graph
->data
+ graph
->data_len
- graph
->hash_len
,
481 the_repository
->hash_algo
);
488 free(graph
->bloom_filter_settings
);
493 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
494 const char *graph_file
,
495 struct object_directory
*odb
)
500 struct commit_graph
*g
;
501 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
506 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
509 g
->filename
= xstrdup(graph_file
);
514 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
515 struct object_directory
*odb
)
517 char *graph_name
= get_commit_graph_filename(odb
);
518 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
525 * returns 1 if and only if all graphs in the chain have
526 * corrected commit dates stored in the generation_data chunk.
528 static int validate_mixed_generation_chain(struct commit_graph
*g
)
530 int read_generation_data
= 1;
531 struct commit_graph
*p
= g
;
533 while (read_generation_data
&& p
) {
534 read_generation_data
= p
->read_generation_data
;
538 if (read_generation_data
)
542 g
->read_generation_data
= 0;
549 static int add_graph_to_chain(struct commit_graph
*g
,
550 struct commit_graph
*chain
,
551 struct object_id
*oids
,
554 struct commit_graph
*cur_g
= chain
;
556 if (n
&& !g
->chunk_base_graphs
) {
557 warning(_("commit-graph has no base graphs chunk"));
561 if (g
->chunk_base_graphs_size
/ g
->hash_len
< n
) {
562 warning(_("commit-graph base graphs chunk is too small"));
570 !oideq(&oids
[n
], &cur_g
->oid
) ||
571 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ st_mult(g
->hash_len
, n
),
572 the_repository
->hash_algo
)) {
573 warning(_("commit-graph chain does not match"));
577 cur_g
= cur_g
->base_graph
;
581 if (unsigned_add_overflows(chain
->num_commits
,
582 chain
->num_commits_in_base
)) {
583 warning(_("commit count in base graph too high: %"PRIuMAX
),
584 (uintmax_t)chain
->num_commits_in_base
);
587 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
590 g
->base_graph
= chain
;
595 int open_commit_graph_chain(const char *chain_file
,
596 int *fd
, struct stat
*st
)
598 *fd
= git_open(chain_file
);
601 if (fstat(*fd
, st
)) {
605 if (st
->st_size
< the_hash_algo
->hexsz
) {
608 /* treat empty files the same as missing */
611 warning(_("commit-graph chain file too small"));
619 struct commit_graph
*load_commit_graph_chain_fd_st(struct repository
*r
,
620 int fd
, struct stat
*st
,
621 int *incomplete_chain
)
623 struct commit_graph
*graph_chain
= NULL
;
624 struct strbuf line
= STRBUF_INIT
;
625 struct object_id
*oids
;
626 int i
= 0, valid
= 1, count
;
627 FILE *fp
= xfdopen(fd
, "r");
629 count
= st
->st_size
/ (the_hash_algo
->hexsz
+ 1);
630 CALLOC_ARRAY(oids
, count
);
634 for (i
= 0; i
< count
; i
++) {
635 struct object_directory
*odb
;
637 if (strbuf_getline_lf(&line
, fp
) == EOF
)
640 if (get_oid_hex(line
.buf
, &oids
[i
])) {
641 warning(_("invalid commit-graph chain: line '%s' not a hash"),
648 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
649 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
650 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
655 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
659 free_commit_graph(g
);
667 warning(_("unable to find all commit-graph files"));
672 validate_mixed_generation_chain(graph_chain
);
676 strbuf_release(&line
);
678 *incomplete_chain
= !valid
;
682 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
683 struct object_directory
*odb
)
685 char *chain_file
= get_commit_graph_chain_filename(odb
);
688 struct commit_graph
*g
= NULL
;
690 if (open_commit_graph_chain(chain_file
, &fd
, &st
)) {
692 /* ownership of fd is taken over by load function */
693 g
= load_commit_graph_chain_fd_st(r
, fd
, &st
, &incomplete
);
700 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
701 struct object_directory
*odb
)
703 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
706 g
= load_commit_graph_chain(r
, odb
);
711 static void prepare_commit_graph_one(struct repository
*r
,
712 struct object_directory
*odb
)
715 if (r
->objects
->commit_graph
)
718 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
722 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
724 * On the first invocation, this function attempts to load the commit
725 * graph if the_repository is configured to have one.
727 static int prepare_commit_graph(struct repository
*r
)
729 struct object_directory
*odb
;
732 * Early return if there is no git dir or if the commit graph is
735 * This must come before the "already attempted?" check below, because
736 * we want to disable even an already-loaded graph file.
738 if (!r
->gitdir
|| r
->commit_graph_disabled
)
741 if (r
->objects
->commit_graph_attempted
)
742 return !!r
->objects
->commit_graph
;
743 r
->objects
->commit_graph_attempted
= 1;
745 prepare_repo_settings(r
);
747 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
748 r
->settings
.core_commit_graph
!= 1)
750 * This repository is not configured to use commit graphs, so
751 * do not load one. (But report commit_graph_attempted anyway
752 * so that commit graph loading is not attempted again for this
757 if (!commit_graph_compatible(r
))
761 for (odb
= r
->objects
->odb
;
762 !r
->objects
->commit_graph
&& odb
;
764 prepare_commit_graph_one(r
, odb
);
765 return !!r
->objects
->commit_graph
;
768 int generation_numbers_enabled(struct repository
*r
)
770 uint32_t first_generation
;
771 struct commit_graph
*g
;
772 if (!prepare_commit_graph(r
))
775 g
= r
->objects
->commit_graph
;
780 first_generation
= get_be32(g
->chunk_commit_data
+
781 g
->hash_len
+ 8) >> 2;
783 return !!first_generation
;
786 int corrected_commit_dates_enabled(struct repository
*r
)
788 struct commit_graph
*g
;
789 if (!prepare_commit_graph(r
))
792 g
= r
->objects
->commit_graph
;
797 return g
->read_generation_data
;
800 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
802 struct commit_graph
*g
= r
->objects
->commit_graph
;
804 if (g
->bloom_filter_settings
)
805 return g
->bloom_filter_settings
;
811 void close_commit_graph(struct raw_object_store
*o
)
813 if (!o
->commit_graph
)
816 clear_commit_graph_data_slab(&commit_graph_data_slab
);
817 free_commit_graph(o
->commit_graph
);
818 o
->commit_graph
= NULL
;
821 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
823 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
824 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
827 static void load_oid_from_graph(struct commit_graph
*g
,
829 struct object_id
*oid
)
833 while (g
&& pos
< g
->num_commits_in_base
)
837 BUG("NULL commit-graph");
839 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
840 die(_("invalid commit position. commit-graph is likely corrupt"));
842 lex_index
= pos
- g
->num_commits_in_base
;
844 oidread(oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, lex_index
),
845 the_repository
->hash_algo
);
848 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
849 struct commit_graph
*g
,
851 struct commit_list
**pptr
)
854 struct object_id oid
;
856 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
857 die("invalid parent position %"PRIu32
, pos
);
859 load_oid_from_graph(g
, pos
, &oid
);
860 c
= lookup_commit(r
, &oid
);
862 die(_("could not find commit %s"), oid_to_hex(&oid
));
863 commit_graph_data_at(c
)->graph_pos
= pos
;
864 return &commit_list_insert(c
, pptr
)->next
;
867 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
869 const unsigned char *commit_data
;
870 struct commit_graph_data
*graph_data
;
871 uint32_t lex_index
, offset_pos
;
872 uint64_t date_high
, date_low
, offset
;
874 while (pos
< g
->num_commits_in_base
)
877 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
878 die(_("invalid commit position. commit-graph is likely corrupt"));
880 lex_index
= pos
- g
->num_commits_in_base
;
881 commit_data
= g
->chunk_commit_data
+ st_mult(GRAPH_DATA_WIDTH
, lex_index
);
883 graph_data
= commit_graph_data_at(item
);
884 graph_data
->graph_pos
= pos
;
886 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
887 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
888 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
890 if (g
->read_generation_data
) {
891 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ st_mult(sizeof(uint32_t), lex_index
));
893 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
894 if (!g
->chunk_generation_data_overflow
)
895 die(_("commit-graph requires overflow generation data but has none"));
897 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
898 if (g
->chunk_generation_data_overflow_size
/ sizeof(uint64_t) <= offset_pos
)
899 die(_("commit-graph overflow generation data is too small"));
900 graph_data
->generation
= item
->date
+
901 get_be64(g
->chunk_generation_data_overflow
+ sizeof(uint64_t) * offset_pos
);
903 graph_data
->generation
= item
->date
+ offset
;
905 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
908 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
911 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
916 static int fill_commit_in_graph(struct repository
*r
,
918 struct commit_graph
*g
, uint32_t pos
)
921 uint32_t parent_data_pos
;
922 struct commit_list
**pptr
;
923 const unsigned char *commit_data
;
926 while (pos
< g
->num_commits_in_base
)
929 fill_commit_graph_info(item
, g
, pos
);
931 lex_index
= pos
- g
->num_commits_in_base
;
932 commit_data
= g
->chunk_commit_data
+ st_mult(g
->hash_len
+ 16, lex_index
);
934 item
->object
.parsed
= 1;
936 set_commit_tree(item
, NULL
);
938 pptr
= &item
->parents
;
940 edge_value
= get_be32(commit_data
+ g
->hash_len
);
941 if (edge_value
== GRAPH_PARENT_NONE
)
943 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
945 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
946 if (edge_value
== GRAPH_PARENT_NONE
)
948 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
949 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
953 parent_data_pos
= edge_value
& GRAPH_EDGE_LAST_MASK
;
955 if (g
->chunk_extra_edges_size
/ sizeof(uint32_t) <= parent_data_pos
) {
956 error(_("commit-graph extra-edges pointer out of bounds"));
957 free_commit_list(item
->parents
);
958 item
->parents
= NULL
;
959 item
->object
.parsed
= 0;
962 edge_value
= get_be32(g
->chunk_extra_edges
+
963 sizeof(uint32_t) * parent_data_pos
);
964 pptr
= insert_parent_or_die(r
, g
,
965 edge_value
& GRAPH_EDGE_LAST_MASK
,
968 } while (!(edge_value
& GRAPH_LAST_EDGE
));
973 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
975 struct commit_graph
*cur_g
= g
;
978 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
979 cur_g
= cur_g
->base_graph
;
982 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
989 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
991 uint32_t graph_pos
= commit_graph_position(item
);
992 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
996 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
1000 int repo_find_commit_pos_in_graph(struct repository
*r
, struct commit
*c
,
1003 if (!prepare_commit_graph(r
))
1005 return find_commit_pos_in_graph(c
, r
->objects
->commit_graph
, pos
);
1008 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
1010 static int commit_graph_paranoia
= -1;
1011 struct commit
*commit
;
1014 if (commit_graph_paranoia
== -1)
1015 commit_graph_paranoia
= git_env_bool(GIT_COMMIT_GRAPH_PARANOIA
, 0);
1017 if (!prepare_commit_graph(repo
))
1019 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
1021 if (commit_graph_paranoia
&& !has_object(repo
, id
, 0))
1024 commit
= lookup_commit(repo
, id
);
1027 if (commit
->object
.parsed
)
1030 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
1036 static int parse_commit_in_graph_one(struct repository
*r
,
1037 struct commit_graph
*g
,
1038 struct commit
*item
)
1042 if (item
->object
.parsed
)
1045 if (find_commit_pos_in_graph(item
, g
, &pos
))
1046 return fill_commit_in_graph(r
, item
, g
, pos
);
1051 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
1053 static int checked_env
= 0;
1056 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
1057 die("dying as requested by the '%s' variable on commit-graph parse!",
1058 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
1061 if (!prepare_commit_graph(r
))
1063 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
1066 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
1069 if (repo_find_commit_pos_in_graph(r
, item
, &pos
))
1070 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
1073 static struct tree
*load_tree_for_commit(struct repository
*r
,
1074 struct commit_graph
*g
,
1077 struct object_id oid
;
1078 const unsigned char *commit_data
;
1079 uint32_t graph_pos
= commit_graph_position(c
);
1081 while (graph_pos
< g
->num_commits_in_base
)
1084 commit_data
= g
->chunk_commit_data
+
1085 st_mult(GRAPH_DATA_WIDTH
, graph_pos
- g
->num_commits_in_base
);
1087 oidread(&oid
, commit_data
, the_repository
->hash_algo
);
1088 set_commit_tree(c
, lookup_tree(r
, &oid
));
1090 return c
->maybe_tree
;
1093 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
1094 struct commit_graph
*g
,
1095 const struct commit
*c
)
1098 return c
->maybe_tree
;
1099 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
1100 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1102 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
1105 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
1107 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
1110 struct packed_commit_list
{
1111 struct commit
**list
;
1116 struct write_commit_graph_context
{
1117 struct repository
*r
;
1118 struct object_directory
*odb
;
1120 struct oid_array oids
;
1121 struct packed_commit_list commits
;
1122 int num_extra_edges
;
1123 int num_generation_data_overflows
;
1124 unsigned long approx_nr_objects
;
1125 struct progress
*progress
;
1127 uint64_t progress_cnt
;
1129 char *base_graph_name
;
1130 int num_commit_graphs_before
;
1131 int num_commit_graphs_after
;
1132 char **commit_graph_filenames_before
;
1133 char **commit_graph_filenames_after
;
1134 char **commit_graph_hash_after
;
1135 uint32_t new_num_commits_in_base
;
1136 struct commit_graph
*new_base_graph
;
1143 write_generation_data
:1,
1144 trust_generation_numbers
:1;
1146 struct topo_level_slab
*topo_levels
;
1147 const struct commit_graph_opts
*opts
;
1148 size_t total_bloom_filter_data_size
;
1149 const struct bloom_filter_settings
*bloom_settings
;
1151 int count_bloom_filter_computed
;
1152 int count_bloom_filter_not_computed
;
1153 int count_bloom_filter_trunc_empty
;
1154 int count_bloom_filter_trunc_large
;
1157 static int write_graph_chunk_fanout(struct hashfile
*f
,
1160 struct write_commit_graph_context
*ctx
= data
;
1162 struct commit
**list
= ctx
->commits
.list
;
1165 * Write the first-level table (the list is sorted,
1166 * but we use a 256-entry lookup to be able to avoid
1167 * having to do eight extra binary search iterations).
1169 for (i
= 0; i
< 256; i
++) {
1170 while (count
< ctx
->commits
.nr
) {
1171 if ((*list
)->object
.oid
.hash
[0] != i
)
1173 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1178 hashwrite_be32(f
, count
);
1184 static int write_graph_chunk_oids(struct hashfile
*f
,
1187 struct write_commit_graph_context
*ctx
= data
;
1188 struct commit
**list
= ctx
->commits
.list
;
1190 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1191 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1192 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1198 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1200 const struct commit
* const *commits
= table
;
1201 return &commits
[index
]->object
.oid
;
1204 static int write_graph_chunk_data(struct hashfile
*f
,
1207 struct write_commit_graph_context
*ctx
= data
;
1208 struct commit
**list
= ctx
->commits
.list
;
1209 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1210 uint32_t num_extra_edges
= 0;
1212 while (list
< last
) {
1213 struct commit_list
*parent
;
1214 struct object_id
*tree
;
1216 uint32_t packedDate
[2];
1217 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1219 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1220 die(_("unable to parse commit %s"),
1221 oid_to_hex(&(*list
)->object
.oid
));
1222 tree
= get_commit_tree_oid(*list
);
1223 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1225 parent
= (*list
)->parents
;
1228 edge_value
= GRAPH_PARENT_NONE
;
1230 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1235 if (edge_value
>= 0)
1236 edge_value
+= ctx
->new_num_commits_in_base
;
1237 else if (ctx
->new_base_graph
) {
1239 if (find_commit_pos_in_graph(parent
->item
,
1240 ctx
->new_base_graph
,
1246 BUG("missing parent %s for commit %s",
1247 oid_to_hex(&parent
->item
->object
.oid
),
1248 oid_to_hex(&(*list
)->object
.oid
));
1251 hashwrite_be32(f
, edge_value
);
1254 parent
= parent
->next
;
1257 edge_value
= GRAPH_PARENT_NONE
;
1258 else if (parent
->next
)
1259 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1261 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
));
1282 hashwrite_be32(f
, edge_value
);
1284 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1287 parent
= parent
->next
;
1291 if (sizeof((*list
)->date
) > 4)
1292 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1296 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1298 packedDate
[1] = htonl((*list
)->date
);
1299 hashwrite(f
, packedDate
, 8);
1307 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1310 struct write_commit_graph_context
*ctx
= data
;
1311 int i
, num_generation_data_overflows
= 0;
1313 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1314 struct commit
*c
= ctx
->commits
.list
[i
];
1316 repo_parse_commit(ctx
->r
, c
);
1317 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1318 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1320 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1321 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1322 num_generation_data_overflows
++;
1325 hashwrite_be32(f
, offset
);
1331 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1334 struct write_commit_graph_context
*ctx
= data
;
1336 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1337 struct commit
*c
= ctx
->commits
.list
[i
];
1338 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1339 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1341 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1342 hashwrite_be32(f
, offset
>> 32);
1343 hashwrite_be32(f
, (uint32_t) offset
);
1350 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1353 struct write_commit_graph_context
*ctx
= data
;
1354 struct commit
**list
= ctx
->commits
.list
;
1355 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1356 struct commit_list
*parent
;
1358 while (list
< last
) {
1359 int num_parents
= 0;
1361 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1363 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1364 parent
= parent
->next
)
1367 if (num_parents
<= 2) {
1372 /* Since num_parents > 2, this initializer is safe. */
1373 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1374 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1379 if (edge_value
>= 0)
1380 edge_value
+= ctx
->new_num_commits_in_base
;
1381 else if (ctx
->new_base_graph
) {
1383 if (find_commit_pos_in_graph(parent
->item
,
1384 ctx
->new_base_graph
,
1390 BUG("missing parent %s for commit %s",
1391 oid_to_hex(&parent
->item
->object
.oid
),
1392 oid_to_hex(&(*list
)->object
.oid
));
1393 else if (!parent
->next
)
1394 edge_value
|= GRAPH_LAST_EDGE
;
1396 hashwrite_be32(f
, edge_value
);
1405 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1408 struct write_commit_graph_context
*ctx
= data
;
1409 struct commit
**list
= ctx
->commits
.list
;
1410 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1411 uint32_t cur_pos
= 0;
1413 while (list
< last
) {
1414 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1415 size_t len
= filter
? filter
->len
: 0;
1417 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1418 hashwrite_be32(f
, cur_pos
);
1425 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1427 struct json_writer jw
= JSON_WRITER_INIT
;
1429 jw_object_begin(&jw
, 0);
1430 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1431 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1432 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1433 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1436 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1441 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1444 struct write_commit_graph_context
*ctx
= data
;
1445 struct commit
**list
= ctx
->commits
.list
;
1446 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1448 trace2_bloom_filter_settings(ctx
);
1450 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1451 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1452 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1454 while (list
< last
) {
1455 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1456 size_t len
= filter
? filter
->len
: 0;
1458 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1460 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1467 static int add_packed_commits(const struct object_id
*oid
,
1468 struct packed_git
*pack
,
1472 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1473 enum object_type type
;
1474 off_t offset
= nth_packed_object_offset(pack
, pos
);
1475 struct object_info oi
= OBJECT_INFO_INIT
;
1478 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1481 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1482 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1484 if (type
!= OBJ_COMMIT
)
1487 oid_array_append(&ctx
->oids
, oid
);
1488 set_commit_pos(ctx
->r
, oid
);
1493 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1495 struct commit_list
*parent
;
1496 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1497 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1498 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1499 parent
->item
->object
.flags
|= REACHABLE
;
1504 static void close_reachable(struct write_commit_graph_context
*ctx
)
1507 struct commit
*commit
;
1508 enum commit_graph_split_flags flags
= ctx
->opts
?
1509 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1511 if (ctx
->report_progress
)
1512 ctx
->progress
= start_delayed_progress(
1513 _("Loading known commits in commit graph"),
1515 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1516 display_progress(ctx
->progress
, i
+ 1);
1517 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1519 commit
->object
.flags
|= REACHABLE
;
1521 stop_progress(&ctx
->progress
);
1524 * As this loop runs, ctx->oids.nr may grow, but not more
1525 * than the number of missing commits in the reachable
1528 if (ctx
->report_progress
)
1529 ctx
->progress
= start_delayed_progress(
1530 _("Expanding reachable commits in commit graph"),
1532 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1533 display_progress(ctx
->progress
, i
+ 1);
1534 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1539 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1540 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1541 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1542 add_missing_parents(ctx
, commit
);
1543 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1544 add_missing_parents(ctx
, commit
);
1546 stop_progress(&ctx
->progress
);
1548 if (ctx
->report_progress
)
1549 ctx
->progress
= start_delayed_progress(
1550 _("Clearing commit marks in commit graph"),
1552 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1553 display_progress(ctx
->progress
, i
+ 1);
1554 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1557 commit
->object
.flags
&= ~REACHABLE
;
1559 stop_progress(&ctx
->progress
);
1562 struct compute_generation_info
{
1563 struct repository
*r
;
1564 struct packed_commit_list
*commits
;
1565 struct progress
*progress
;
1568 timestamp_t (*get_generation
)(struct commit
*c
, void *data
);
1569 void (*set_generation
)(struct commit
*c
, timestamp_t gen
, void *data
);
1573 static timestamp_t
compute_generation_from_max(struct commit
*c
,
1574 timestamp_t max_gen
,
1575 int generation_version
)
1577 switch (generation_version
) {
1578 case 1: /* topological levels */
1579 if (max_gen
> GENERATION_NUMBER_V1_MAX
- 1)
1580 max_gen
= GENERATION_NUMBER_V1_MAX
- 1;
1583 case 2: /* corrected commit date */
1584 if (c
->date
&& c
->date
> max_gen
)
1585 max_gen
= c
->date
- 1;
1589 BUG("attempting unimplemented version");
1593 static void compute_reachable_generation_numbers(
1594 struct compute_generation_info
*info
,
1595 int generation_version
)
1598 struct commit_list
*list
= NULL
;
1600 for (i
= 0; i
< info
->commits
->nr
; i
++) {
1601 struct commit
*c
= info
->commits
->list
[i
];
1603 repo_parse_commit(info
->r
, c
);
1604 gen
= info
->get_generation(c
, info
->data
);
1605 display_progress(info
->progress
, ++info
->progress_cnt
);
1607 if (gen
!= GENERATION_NUMBER_ZERO
&& gen
!= GENERATION_NUMBER_INFINITY
)
1610 commit_list_insert(c
, &list
);
1612 struct commit
*current
= list
->item
;
1613 struct commit_list
*parent
;
1614 int all_parents_computed
= 1;
1615 uint32_t max_gen
= 0;
1617 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1618 repo_parse_commit(info
->r
, parent
->item
);
1619 gen
= info
->get_generation(parent
->item
, info
->data
);
1621 if (gen
== GENERATION_NUMBER_ZERO
) {
1622 all_parents_computed
= 0;
1623 commit_list_insert(parent
->item
, &list
);
1631 if (all_parents_computed
) {
1633 gen
= compute_generation_from_max(
1635 generation_version
);
1636 info
->set_generation(current
, gen
, info
->data
);
1642 static timestamp_t
get_topo_level(struct commit
*c
, void *data
)
1644 struct write_commit_graph_context
*ctx
= data
;
1645 return *topo_level_slab_at(ctx
->topo_levels
, c
);
1648 static void set_topo_level(struct commit
*c
, timestamp_t t
, void *data
)
1650 struct write_commit_graph_context
*ctx
= data
;
1651 *topo_level_slab_at(ctx
->topo_levels
, c
) = (uint32_t)t
;
1654 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1656 struct compute_generation_info info
= {
1658 .commits
= &ctx
->commits
,
1659 .get_generation
= get_topo_level
,
1660 .set_generation
= set_topo_level
,
1664 if (ctx
->report_progress
)
1665 info
.progress
= ctx
->progress
1666 = start_delayed_progress(
1667 _("Computing commit graph topological levels"),
1670 compute_reachable_generation_numbers(&info
, 1);
1672 stop_progress(&ctx
->progress
);
1675 static timestamp_t
get_generation_from_graph_data(struct commit
*c
,
1678 return commit_graph_data_at(c
)->generation
;
1681 static void set_generation_v2(struct commit
*c
, timestamp_t t
,
1684 struct commit_graph_data
*g
= commit_graph_data_at(c
);
1688 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1691 struct compute_generation_info info
= {
1693 .commits
= &ctx
->commits
,
1694 .get_generation
= get_generation_from_graph_data
,
1695 .set_generation
= set_generation_v2
,
1698 if (ctx
->report_progress
)
1699 info
.progress
= ctx
->progress
1700 = start_delayed_progress(
1701 _("Computing commit graph generation numbers"),
1704 if (!ctx
->trust_generation_numbers
) {
1705 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1706 struct commit
*c
= ctx
->commits
.list
[i
];
1707 repo_parse_commit(ctx
->r
, c
);
1708 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1712 compute_reachable_generation_numbers(&info
, 2);
1714 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1715 struct commit
*c
= ctx
->commits
.list
[i
];
1716 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1717 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1718 ctx
->num_generation_data_overflows
++;
1720 stop_progress(&ctx
->progress
);
1723 static void set_generation_in_graph_data(struct commit
*c
, timestamp_t t
,
1726 commit_graph_data_at(c
)->generation
= t
;
1730 * After this method, all commits reachable from those in the given
1731 * list will have non-zero, non-infinite generation numbers.
1733 void ensure_generations_valid(struct repository
*r
,
1734 struct commit
**commits
, size_t nr
)
1736 int generation_version
= get_configured_generation_version(r
);
1737 struct packed_commit_list list
= {
1742 struct compute_generation_info info
= {
1745 .get_generation
= get_generation_from_graph_data
,
1746 .set_generation
= set_generation_in_graph_data
,
1749 compute_reachable_generation_numbers(&info
, generation_version
);
1752 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1754 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1755 ctx
->count_bloom_filter_computed
);
1756 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1757 ctx
->count_bloom_filter_not_computed
);
1758 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1759 ctx
->count_bloom_filter_trunc_empty
);
1760 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1761 ctx
->count_bloom_filter_trunc_large
);
1764 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1767 struct progress
*progress
= NULL
;
1768 struct commit
**sorted_commits
;
1769 int max_new_filters
;
1771 init_bloom_filters();
1773 if (ctx
->report_progress
)
1774 progress
= start_delayed_progress(
1775 _("Computing commit changed paths Bloom filters"),
1778 DUP_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1780 if (ctx
->order_by_pack
)
1781 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1783 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1785 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1786 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1788 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1789 enum bloom_filter_computed computed
= 0;
1790 struct commit
*c
= sorted_commits
[i
];
1791 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1794 ctx
->count_bloom_filter_computed
< max_new_filters
,
1795 ctx
->bloom_settings
,
1797 if (computed
& BLOOM_COMPUTED
) {
1798 ctx
->count_bloom_filter_computed
++;
1799 if (computed
& BLOOM_TRUNC_EMPTY
)
1800 ctx
->count_bloom_filter_trunc_empty
++;
1801 if (computed
& BLOOM_TRUNC_LARGE
)
1802 ctx
->count_bloom_filter_trunc_large
++;
1803 } else if (computed
& BLOOM_NOT_COMPUTED
)
1804 ctx
->count_bloom_filter_not_computed
++;
1805 ctx
->total_bloom_filter_data_size
+= filter
1806 ? sizeof(unsigned char) * filter
->len
: 0;
1807 display_progress(progress
, i
+ 1);
1810 if (trace2_is_enabled())
1811 trace2_bloom_filter_write_statistics(ctx
);
1813 free(sorted_commits
);
1814 stop_progress(&progress
);
1817 struct refs_cb_data
{
1818 struct oidset
*commits
;
1819 struct progress
*progress
;
1822 static int add_ref_to_set(const char *refname UNUSED
,
1823 const struct object_id
*oid
,
1824 int flags UNUSED
, void *cb_data
)
1826 struct object_id peeled
;
1827 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1829 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
1831 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1832 oidset_insert(data
->commits
, oid
);
1834 display_progress(data
->progress
, oidset_size(data
->commits
));
1839 int write_commit_graph_reachable(struct object_directory
*odb
,
1840 enum commit_graph_write_flags flags
,
1841 const struct commit_graph_opts
*opts
)
1843 struct oidset commits
= OIDSET_INIT
;
1844 struct refs_cb_data data
;
1847 memset(&data
, 0, sizeof(data
));
1848 data
.commits
= &commits
;
1849 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1850 data
.progress
= start_delayed_progress(
1851 _("Collecting referenced commits"), 0);
1853 refs_for_each_ref(get_main_ref_store(the_repository
), add_ref_to_set
,
1856 stop_progress(&data
.progress
);
1858 result
= write_commit_graph(odb
, NULL
, &commits
,
1861 oidset_clear(&commits
);
1865 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1866 const struct string_list
*pack_indexes
)
1869 struct strbuf progress_title
= STRBUF_INIT
;
1870 struct strbuf packname
= STRBUF_INIT
;
1874 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1875 dirlen
= packname
.len
;
1876 if (ctx
->report_progress
) {
1877 strbuf_addf(&progress_title
,
1878 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1879 "Finding commits for commit graph in %"PRIuMAX
" packs",
1881 (uintmax_t)pack_indexes
->nr
);
1882 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1883 ctx
->progress_done
= 0;
1885 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1886 struct packed_git
*p
;
1887 strbuf_setlen(&packname
, dirlen
);
1888 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1889 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1891 ret
= error(_("error adding pack %s"), packname
.buf
);
1894 if (open_pack_index(p
)) {
1895 ret
= error(_("error opening index for %s"), packname
.buf
);
1898 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1899 FOR_EACH_OBJECT_PACK_ORDER
);
1905 stop_progress(&ctx
->progress
);
1906 strbuf_release(&progress_title
);
1907 strbuf_release(&packname
);
1912 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1913 struct oidset
*commits
)
1915 struct oidset_iter iter
;
1916 struct object_id
*oid
;
1918 if (!oidset_size(commits
))
1921 oidset_iter_init(commits
, &iter
);
1922 while ((oid
= oidset_iter_next(&iter
))) {
1923 oid_array_append(&ctx
->oids
, oid
);
1929 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1931 if (ctx
->report_progress
)
1932 ctx
->progress
= start_delayed_progress(
1933 _("Finding commits for commit graph among packed objects"),
1934 ctx
->approx_nr_objects
);
1935 for_each_packed_object(add_packed_commits
, ctx
,
1936 FOR_EACH_OBJECT_PACK_ORDER
);
1937 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1938 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1939 stop_progress(&ctx
->progress
);
1942 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1945 enum commit_graph_split_flags flags
= ctx
->opts
?
1946 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1948 ctx
->num_extra_edges
= 0;
1949 if (ctx
->report_progress
)
1950 ctx
->progress
= start_delayed_progress(
1951 _("Finding extra edges in commit graph"),
1953 oid_array_sort(&ctx
->oids
);
1954 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1955 unsigned int num_parents
;
1957 display_progress(ctx
->progress
, i
+ 1);
1959 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1960 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1962 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1963 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1966 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1967 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1969 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1971 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1972 if (num_parents
> 2)
1973 ctx
->num_extra_edges
+= num_parents
- 1;
1977 stop_progress(&ctx
->progress
);
1980 static int write_graph_chunk_base_1(struct hashfile
*f
,
1981 struct commit_graph
*g
)
1988 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1989 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1993 static int write_graph_chunk_base(struct hashfile
*f
,
1996 struct write_commit_graph_context
*ctx
= data
;
1997 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1999 if (num
!= ctx
->num_commit_graphs_after
- 1) {
2000 error(_("failed to write correct number of base graph ids"));
2007 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
2011 struct tempfile
*graph_layer
; /* when ctx->split is non-zero */
2012 struct lock_file lk
= LOCK_INIT
;
2013 const unsigned hashsz
= the_hash_algo
->rawsz
;
2014 struct strbuf progress_title
= STRBUF_INIT
;
2015 struct chunkfile
*cf
;
2016 unsigned char file_hash
[GIT_MAX_RAWSZ
];
2019 struct strbuf tmp_file
= STRBUF_INIT
;
2021 strbuf_addf(&tmp_file
,
2022 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2024 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
2026 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
2029 if (safe_create_leading_directories(ctx
->graph_name
)) {
2030 UNLEAK(ctx
->graph_name
);
2031 error(_("unable to create leading directories of %s"),
2037 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
2039 hold_lock_file_for_update_mode(&lk
, lock_name
,
2040 LOCK_DIE_ON_ERROR
, 0444);
2043 graph_layer
= mks_tempfile_m(ctx
->graph_name
, 0444);
2045 error(_("unable to create temporary graph layer"));
2049 if (adjust_shared_perm(get_tempfile_path(graph_layer
))) {
2050 error(_("unable to adjust shared permissions for '%s'"),
2051 get_tempfile_path(graph_layer
));
2055 f
= hashfd(get_tempfile_fd(graph_layer
), get_tempfile_path(graph_layer
));
2057 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
2058 LOCK_DIE_ON_ERROR
, 0444);
2059 f
= hashfd(get_lock_file_fd(&lk
), get_lock_file_path(&lk
));
2062 cf
= init_chunkfile(f
);
2064 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
2065 write_graph_chunk_fanout
);
2066 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, st_mult(hashsz
, ctx
->commits
.nr
),
2067 write_graph_chunk_oids
);
2068 add_chunk(cf
, GRAPH_CHUNKID_DATA
, st_mult(hashsz
+ 16, ctx
->commits
.nr
),
2069 write_graph_chunk_data
);
2071 if (ctx
->write_generation_data
)
2072 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
2073 st_mult(sizeof(uint32_t), ctx
->commits
.nr
),
2074 write_graph_chunk_generation_data
);
2075 if (ctx
->num_generation_data_overflows
)
2076 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
2077 st_mult(sizeof(timestamp_t
), ctx
->num_generation_data_overflows
),
2078 write_graph_chunk_generation_data_overflow
);
2079 if (ctx
->num_extra_edges
)
2080 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
2081 st_mult(4, ctx
->num_extra_edges
),
2082 write_graph_chunk_extra_edges
);
2083 if (ctx
->changed_paths
) {
2084 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
2085 st_mult(sizeof(uint32_t), ctx
->commits
.nr
),
2086 write_graph_chunk_bloom_indexes
);
2087 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
2088 st_add(sizeof(uint32_t) * 3,
2089 ctx
->total_bloom_filter_data_size
),
2090 write_graph_chunk_bloom_data
);
2092 if (ctx
->num_commit_graphs_after
> 1)
2093 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
2094 st_mult(hashsz
, ctx
->num_commit_graphs_after
- 1),
2095 write_graph_chunk_base
);
2097 hashwrite_be32(f
, GRAPH_SIGNATURE
);
2099 hashwrite_u8(f
, GRAPH_VERSION
);
2100 hashwrite_u8(f
, oid_version(the_hash_algo
));
2101 hashwrite_u8(f
, get_num_chunks(cf
));
2102 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
2104 if (ctx
->report_progress
) {
2105 strbuf_addf(&progress_title
,
2106 Q_("Writing out commit graph in %d pass",
2107 "Writing out commit graph in %d passes",
2108 get_num_chunks(cf
)),
2109 get_num_chunks(cf
));
2110 ctx
->progress
= start_delayed_progress(
2112 st_mult(get_num_chunks(cf
), ctx
->commits
.nr
));
2115 write_chunkfile(cf
, ctx
);
2117 stop_progress(&ctx
->progress
);
2118 strbuf_release(&progress_title
);
2120 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
2121 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
2122 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
2124 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
2125 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
2126 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
2127 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
2130 close_commit_graph(ctx
->r
->objects
);
2131 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
2132 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
2136 FILE *chainf
= fdopen_lock_file(&lk
, "w");
2137 char *final_graph_name
;
2141 error(_("unable to open commit-graph chain file"));
2145 if (ctx
->base_graph_name
) {
2147 int idx
= ctx
->num_commit_graphs_after
- 1;
2148 if (ctx
->num_commit_graphs_after
> 1)
2151 dest
= ctx
->commit_graph_filenames_after
[idx
];
2153 if (strcmp(ctx
->base_graph_name
, dest
)) {
2154 result
= rename(ctx
->base_graph_name
, dest
);
2157 error(_("failed to rename base commit-graph file"));
2162 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
2167 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
2168 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
2169 final_graph_name
= get_split_graph_filename(ctx
->odb
,
2170 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
2171 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1]);
2172 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
2174 result
= rename_tempfile(&graph_layer
, final_graph_name
);
2176 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
2177 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
2180 error(_("failed to rename temporary commit-graph file"));
2185 commit_lock_file(&lk
);
2190 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2192 struct commit_graph
*g
;
2193 uint32_t num_commits
;
2194 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2197 int max_commits
= 0;
2201 max_commits
= ctx
->opts
->max_commits
;
2203 if (ctx
->opts
->size_multiple
)
2204 size_mult
= ctx
->opts
->size_multiple
;
2206 flags
= ctx
->opts
->split_flags
;
2209 g
= ctx
->r
->objects
->commit_graph
;
2210 num_commits
= ctx
->commits
.nr
;
2211 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2212 ctx
->num_commit_graphs_after
= 1;
2214 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2216 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2217 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2218 while (g
&& (g
->num_commits
<= st_mult(size_mult
, num_commits
) ||
2219 (max_commits
&& num_commits
> max_commits
))) {
2220 if (g
->odb
!= ctx
->odb
)
2223 if (unsigned_add_overflows(num_commits
, g
->num_commits
))
2224 die(_("cannot merge graphs with %"PRIuMAX
", "
2225 "%"PRIuMAX
" commits"),
2226 (uintmax_t)num_commits
,
2227 (uintmax_t)g
->num_commits
);
2228 num_commits
+= g
->num_commits
;
2231 ctx
->num_commit_graphs_after
--;
2235 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2236 ctx
->new_base_graph
= g
;
2237 else if (ctx
->num_commit_graphs_after
!= 1)
2238 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2239 "should be 1 with --split=replace");
2241 if (ctx
->num_commit_graphs_after
== 2) {
2242 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2244 if (!strcmp(g
->filename
, old_graph_name
) &&
2245 g
->odb
!= ctx
->odb
) {
2246 ctx
->num_commit_graphs_after
= 1;
2247 ctx
->new_base_graph
= NULL
;
2250 free(old_graph_name
);
2253 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2254 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2256 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2257 i
< ctx
->num_commit_graphs_before
; i
++)
2258 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2260 i
= ctx
->num_commit_graphs_before
- 1;
2261 g
= ctx
->r
->objects
->commit_graph
;
2264 if (i
< ctx
->num_commit_graphs_after
)
2265 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2268 * If the topmost remaining layer has generation data chunk, the
2269 * resultant layer also has generation data chunk.
2271 if (i
== ctx
->num_commit_graphs_after
- 2)
2272 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2279 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2280 struct commit_graph
*g
)
2283 uint32_t offset
= g
->num_commits_in_base
;
2285 if (unsigned_add_overflows(ctx
->commits
.nr
, g
->num_commits
))
2286 die(_("cannot merge graph %s, too many commits: %"PRIuMAX
),
2287 oid_to_hex(&g
->oid
),
2288 (uintmax_t)st_add(ctx
->commits
.nr
, g
->num_commits
));
2290 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2292 for (i
= 0; i
< g
->num_commits
; i
++) {
2293 struct object_id oid
;
2294 struct commit
*result
;
2296 display_progress(ctx
->progress
, i
+ 1);
2298 load_oid_from_graph(g
, i
+ offset
, &oid
);
2300 /* only add commits if they still exist in the repo */
2301 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2304 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2310 static int commit_compare(const void *_a
, const void *_b
)
2312 const struct commit
*a
= *(const struct commit
**)_a
;
2313 const struct commit
*b
= *(const struct commit
**)_b
;
2314 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2317 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2319 uint32_t i
, dedup_i
= 0;
2321 if (ctx
->report_progress
)
2322 ctx
->progress
= start_delayed_progress(
2323 _("Scanning merged commits"),
2326 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2328 ctx
->num_extra_edges
= 0;
2329 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2330 display_progress(ctx
->progress
, i
+ 1);
2332 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2333 &ctx
->commits
.list
[i
]->object
.oid
)) {
2335 * Silently ignore duplicates. These were likely
2336 * created due to a commit appearing in multiple
2337 * layers of the chain, which is unexpected but
2338 * not invalid. We should make sure there is a
2339 * unique copy in the new layer.
2342 unsigned int num_parents
;
2344 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2347 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2348 if (num_parents
> 2)
2349 ctx
->num_extra_edges
+= num_parents
- 1;
2353 ctx
->commits
.nr
= dedup_i
;
2355 stop_progress(&ctx
->progress
);
2358 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2360 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2361 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2363 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2364 current_graph_number
--;
2366 if (ctx
->report_progress
)
2367 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2369 merge_commit_graph(ctx
, g
);
2370 stop_progress(&ctx
->progress
);
2376 ctx
->new_base_graph
= g
;
2377 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2380 if (ctx
->new_base_graph
)
2381 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2383 sort_and_scan_merged_commits(ctx
);
2386 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2389 time_t now
= time(NULL
);
2391 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2393 struct utimbuf updated_time
;
2395 if (stat(ctx
->commit_graph_filenames_before
[i
], &st
) < 0)
2398 updated_time
.actime
= st
.st_atime
;
2399 updated_time
.modtime
= now
;
2400 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2404 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2406 struct strbuf path
= STRBUF_INIT
;
2410 timestamp_t expire_time
= time(NULL
);
2412 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2413 expire_time
= ctx
->opts
->expire_time
;
2415 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2416 unlink(chain_file_name
);
2417 free(chain_file_name
);
2418 ctx
->num_commit_graphs_after
= 0;
2421 strbuf_addstr(&path
, ctx
->odb
->path
);
2422 strbuf_addstr(&path
, "/info/commit-graphs");
2423 dir
= opendir(path
.buf
);
2428 strbuf_addch(&path
, '/');
2429 dirnamelen
= path
.len
;
2430 while ((de
= readdir(dir
)) != NULL
) {
2432 uint32_t i
, found
= 0;
2434 strbuf_setlen(&path
, dirnamelen
);
2435 strbuf_addstr(&path
, de
->d_name
);
2437 if (stat(path
.buf
, &st
) < 0)
2440 if (st
.st_mtime
> expire_time
)
2442 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2445 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2446 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2460 strbuf_release(&path
);
2463 int write_commit_graph(struct object_directory
*odb
,
2464 const struct string_list
*const pack_indexes
,
2465 struct oidset
*commits
,
2466 enum commit_graph_write_flags flags
,
2467 const struct commit_graph_opts
*opts
)
2469 struct repository
*r
= the_repository
;
2470 struct write_commit_graph_context
*ctx
;
2474 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2475 struct topo_level_slab topo_levels
;
2477 prepare_repo_settings(r
);
2478 if (!r
->settings
.core_commit_graph
) {
2479 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2482 if (!commit_graph_compatible(r
))
2485 CALLOC_ARRAY(ctx
, 1);
2488 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2489 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2490 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2492 ctx
->total_bloom_filter_data_size
= 0;
2493 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2494 ctx
->num_generation_data_overflows
= 0;
2496 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2497 bloom_settings
.bits_per_entry
);
2498 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2499 bloom_settings
.num_hashes
);
2500 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2501 bloom_settings
.max_changed_paths
);
2502 ctx
->bloom_settings
= &bloom_settings
;
2504 init_topo_level_slab(&topo_levels
);
2505 ctx
->topo_levels
= &topo_levels
;
2507 prepare_commit_graph(ctx
->r
);
2508 if (ctx
->r
->objects
->commit_graph
) {
2509 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2512 g
->topo_levels
= &topo_levels
;
2517 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2518 ctx
->changed_paths
= 1;
2519 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2520 struct commit_graph
*g
;
2522 g
= ctx
->r
->objects
->commit_graph
;
2524 /* We have changed-paths already. Keep them in the next graph */
2525 if (g
&& g
->chunk_bloom_data
) {
2526 ctx
->changed_paths
= 1;
2527 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2532 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2535 ctx
->num_commit_graphs_before
++;
2539 if (ctx
->num_commit_graphs_before
) {
2540 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2541 i
= ctx
->num_commit_graphs_before
;
2542 g
= ctx
->r
->objects
->commit_graph
;
2545 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2551 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2554 ctx
->approx_nr_objects
= repo_approximate_object_count(the_repository
);
2556 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2557 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2558 for (i
= 0; i
< g
->num_commits
; i
++) {
2559 struct object_id oid
;
2560 oidread(&oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, i
),
2561 the_repository
->hash_algo
);
2562 oid_array_append(&ctx
->oids
, &oid
);
2567 ctx
->order_by_pack
= 1;
2568 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2573 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2577 if (!pack_indexes
&& !commits
) {
2578 ctx
->order_by_pack
= 1;
2579 fill_oids_from_all_packs(ctx
);
2582 close_reachable(ctx
);
2584 copy_oids_to_commits(ctx
);
2586 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2587 error(_("too many commits to write graph"));
2592 if (!ctx
->commits
.nr
&& !replace
)
2596 split_graph_merge_strategy(ctx
);
2599 merge_commit_graphs(ctx
);
2601 ctx
->num_commit_graphs_after
= 1;
2603 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2605 compute_topological_levels(ctx
);
2606 if (ctx
->write_generation_data
)
2607 compute_generation_numbers(ctx
);
2609 if (ctx
->changed_paths
)
2610 compute_bloom_filters(ctx
);
2612 res
= write_commit_graph_file(ctx
);
2615 mark_commit_graphs(ctx
);
2617 expire_commit_graphs(ctx
);
2620 free(ctx
->graph_name
);
2621 free(ctx
->base_graph_name
);
2622 free(ctx
->commits
.list
);
2623 oid_array_clear(&ctx
->oids
);
2624 clear_topo_level_slab(&topo_levels
);
2626 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2627 free(ctx
->commit_graph_filenames_before
[i
]);
2628 free(ctx
->commit_graph_filenames_before
);
2630 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2631 free(ctx
->commit_graph_filenames_after
[i
]);
2632 free(ctx
->commit_graph_hash_after
[i
]);
2634 free(ctx
->commit_graph_filenames_after
);
2635 free(ctx
->commit_graph_hash_after
);
2642 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2643 static int verify_commit_graph_error
;
2645 __attribute__((format (printf
, 1, 2)))
2646 static void graph_report(const char *fmt
, ...)
2650 verify_commit_graph_error
= 1;
2652 vfprintf(stderr
, fmt
, ap
);
2653 fprintf(stderr
, "\n");
2657 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2659 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2662 static int verify_one_commit_graph(struct repository
*r
,
2663 struct commit_graph
*g
,
2664 struct progress
*progress
,
2667 uint32_t i
, cur_fanout_pos
= 0;
2668 struct object_id prev_oid
, cur_oid
;
2669 struct commit
*seen_gen_zero
= NULL
;
2670 struct commit
*seen_gen_non_zero
= NULL
;
2672 if (!commit_graph_checksum_valid(g
)) {
2673 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2674 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2677 for (i
= 0; i
< g
->num_commits
; i
++) {
2678 struct commit
*graph_commit
;
2680 oidread(&cur_oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, i
),
2681 the_repository
->hash_algo
);
2683 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2684 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2685 oid_to_hex(&prev_oid
),
2686 oid_to_hex(&cur_oid
));
2688 oidcpy(&prev_oid
, &cur_oid
);
2690 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2691 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2693 if (i
!= fanout_value
)
2694 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2695 cur_fanout_pos
, fanout_value
, i
);
2699 graph_commit
= lookup_commit(r
, &cur_oid
);
2700 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2701 graph_report(_("failed to parse commit %s from commit-graph"),
2702 oid_to_hex(&cur_oid
));
2705 while (cur_fanout_pos
< 256) {
2706 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2708 if (g
->num_commits
!= fanout_value
)
2709 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2710 cur_fanout_pos
, fanout_value
, i
);
2715 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2716 return verify_commit_graph_error
;
2718 for (i
= 0; i
< g
->num_commits
; i
++) {
2719 struct commit
*graph_commit
, *odb_commit
;
2720 struct commit_list
*graph_parents
, *odb_parents
;
2721 timestamp_t max_generation
= 0;
2722 timestamp_t generation
;
2724 display_progress(progress
, ++(*seen
));
2725 oidread(&cur_oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, i
),
2726 the_repository
->hash_algo
);
2728 graph_commit
= lookup_commit(r
, &cur_oid
);
2729 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2730 if (repo_parse_commit_internal(r
, odb_commit
, 0, 0)) {
2731 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2732 oid_to_hex(&cur_oid
));
2736 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2737 get_commit_tree_oid(odb_commit
)))
2738 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2739 oid_to_hex(&cur_oid
),
2740 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2741 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2743 graph_parents
= graph_commit
->parents
;
2744 odb_parents
= odb_commit
->parents
;
2746 while (graph_parents
) {
2748 graph_report(_("commit-graph parent list for commit %s is too long"),
2749 oid_to_hex(&cur_oid
));
2753 /* parse parent in case it is in a base graph */
2754 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2756 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2757 graph_report(_("commit-graph parent for %s is %s != %s"),
2758 oid_to_hex(&cur_oid
),
2759 oid_to_hex(&graph_parents
->item
->object
.oid
),
2760 oid_to_hex(&odb_parents
->item
->object
.oid
));
2762 generation
= commit_graph_generation_from_graph(graph_parents
->item
);
2763 if (generation
> max_generation
)
2764 max_generation
= generation
;
2766 graph_parents
= graph_parents
->next
;
2767 odb_parents
= odb_parents
->next
;
2771 graph_report(_("commit-graph parent list for commit %s terminates early"),
2772 oid_to_hex(&cur_oid
));
2774 if (commit_graph_generation_from_graph(graph_commit
))
2775 seen_gen_non_zero
= graph_commit
;
2777 seen_gen_zero
= graph_commit
;
2783 * If we are using topological level and one of our parents has
2784 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2785 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2786 * in the following condition.
2788 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2791 generation
= commit_graph_generation(graph_commit
);
2792 if (generation
< max_generation
+ 1)
2793 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2794 oid_to_hex(&cur_oid
),
2796 max_generation
+ 1);
2798 if (graph_commit
->date
!= odb_commit
->date
)
2799 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2800 oid_to_hex(&cur_oid
),
2805 if (seen_gen_zero
&& seen_gen_non_zero
)
2806 graph_report(_("commit-graph has both zero and non-zero "
2807 "generations (e.g., commits '%s' and '%s')"),
2808 oid_to_hex(&seen_gen_zero
->object
.oid
),
2809 oid_to_hex(&seen_gen_non_zero
->object
.oid
));
2811 return verify_commit_graph_error
;
2814 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2816 struct progress
*progress
= NULL
;
2817 int local_error
= 0;
2821 graph_report("no commit-graph file loaded");
2825 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
) {
2826 uint64_t total
= g
->num_commits
;
2827 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
))
2828 total
+= g
->num_commits_in_base
;
2830 progress
= start_progress(_("Verifying commits in commit graph"),
2834 for (; g
; g
= g
->base_graph
) {
2835 local_error
|= verify_one_commit_graph(r
, g
, progress
, &seen
);
2836 if (flags
& COMMIT_GRAPH_VERIFY_SHALLOW
)
2840 stop_progress(&progress
);
2845 void free_commit_graph(struct commit_graph
*g
)
2848 struct commit_graph
*next
= g
->base_graph
;
2851 munmap((void *)g
->data
, g
->data_len
);
2853 free(g
->bloom_filter_settings
);
2860 void disable_commit_graph(struct repository
*r
)
2862 r
->commit_graph_disabled
= 1;