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
;
350 if (chunk_size
< BLOOMDATA_CHUNK_HEADER_SIZE
) {
351 warning(_("ignoring too-small changed-path chunk"
352 " (%"PRIuMAX
" < %"PRIuMAX
") in commit-graph file"),
353 (uintmax_t)chunk_size
,
354 (uintmax_t)BLOOMDATA_CHUNK_HEADER_SIZE
);
358 g
->chunk_bloom_data
= chunk_start
;
359 g
->chunk_bloom_data_size
= chunk_size
;
361 g
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
362 g
->bloom_filter_settings
->hash_version
= get_be32(chunk_start
);
363 g
->bloom_filter_settings
->num_hashes
= get_be32(chunk_start
+ 4);
364 g
->bloom_filter_settings
->bits_per_entry
= get_be32(chunk_start
+ 8);
365 g
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
370 struct commit_graph
*parse_commit_graph(struct repo_settings
*s
,
371 void *graph_map
, size_t graph_size
)
373 const unsigned char *data
;
374 struct commit_graph
*graph
;
375 uint32_t graph_signature
;
376 unsigned char graph_version
, hash_version
;
377 struct chunkfile
*cf
= NULL
;
382 if (graph_size
< GRAPH_MIN_SIZE
)
385 data
= (const unsigned char *)graph_map
;
387 graph_signature
= get_be32(data
);
388 if (graph_signature
!= GRAPH_SIGNATURE
) {
389 error(_("commit-graph signature %X does not match signature %X"),
390 graph_signature
, GRAPH_SIGNATURE
);
394 graph_version
= *(unsigned char*)(data
+ 4);
395 if (graph_version
!= GRAPH_VERSION
) {
396 error(_("commit-graph version %X does not match version %X"),
397 graph_version
, GRAPH_VERSION
);
401 hash_version
= *(unsigned char*)(data
+ 5);
402 if (hash_version
!= oid_version(the_hash_algo
)) {
403 error(_("commit-graph hash version %X does not match version %X"),
404 hash_version
, oid_version(the_hash_algo
));
408 graph
= alloc_commit_graph();
410 graph
->hash_len
= the_hash_algo
->rawsz
;
411 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
412 graph
->data
= graph_map
;
413 graph
->data_len
= graph_size
;
415 if (graph_size
< GRAPH_HEADER_SIZE
+
416 (graph
->num_chunks
+ 1) * CHUNK_TOC_ENTRY_SIZE
+
417 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
418 error(_("commit-graph file is too small to hold %u chunks"),
424 cf
= init_chunkfile(NULL
);
426 if (read_table_of_contents(cf
, graph
->data
, graph_size
,
427 GRAPH_HEADER_SIZE
, graph
->num_chunks
, 1))
428 goto free_and_return
;
430 if (read_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, graph_read_oid_fanout
, graph
)) {
431 error(_("commit-graph required OID fanout chunk missing or corrupted"));
432 goto free_and_return
;
434 if (read_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, graph_read_oid_lookup
, graph
)) {
435 error(_("commit-graph required OID lookup chunk missing or corrupted"));
436 goto free_and_return
;
438 if (read_chunk(cf
, GRAPH_CHUNKID_DATA
, graph_read_commit_data
, graph
)) {
439 error(_("commit-graph required commit data chunk missing or corrupted"));
440 goto free_and_return
;
443 pair_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
, &graph
->chunk_extra_edges
,
444 &graph
->chunk_extra_edges_size
);
445 pair_chunk(cf
, GRAPH_CHUNKID_BASE
, &graph
->chunk_base_graphs
,
446 &graph
->chunk_base_graphs_size
);
448 if (s
->commit_graph_generation_version
>= 2) {
449 read_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
450 graph_read_generation_data
, graph
);
451 pair_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
452 &graph
->chunk_generation_data_overflow
,
453 &graph
->chunk_generation_data_overflow_size
);
455 if (graph
->chunk_generation_data
)
456 graph
->read_generation_data
= 1;
459 if (s
->commit_graph_changed_paths_version
) {
460 read_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
461 graph_read_bloom_index
, graph
);
462 read_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
463 graph_read_bloom_data
, graph
);
466 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
467 init_bloom_filters();
469 /* We need both the bloom chunks to exist together. Else ignore the data */
470 graph
->chunk_bloom_indexes
= NULL
;
471 graph
->chunk_bloom_data
= NULL
;
472 FREE_AND_NULL(graph
->bloom_filter_settings
);
475 oidread(&graph
->oid
, graph
->data
+ graph
->data_len
- graph
->hash_len
,
476 the_repository
->hash_algo
);
483 free(graph
->bloom_filter_settings
);
488 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
489 const char *graph_file
,
490 struct object_directory
*odb
)
495 struct commit_graph
*g
;
496 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
501 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
504 g
->filename
= xstrdup(graph_file
);
509 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
510 struct object_directory
*odb
)
512 char *graph_name
= get_commit_graph_filename(odb
);
513 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
520 * returns 1 if and only if all graphs in the chain have
521 * corrected commit dates stored in the generation_data chunk.
523 static int validate_mixed_generation_chain(struct commit_graph
*g
)
525 int read_generation_data
= 1;
526 struct commit_graph
*p
= g
;
528 while (read_generation_data
&& p
) {
529 read_generation_data
= p
->read_generation_data
;
533 if (read_generation_data
)
537 g
->read_generation_data
= 0;
544 static void validate_mixed_bloom_settings(struct commit_graph
*g
)
546 struct bloom_filter_settings
*settings
= NULL
;
547 for (; g
; g
= g
->base_graph
) {
548 if (!g
->bloom_filter_settings
)
551 settings
= g
->bloom_filter_settings
;
555 if (g
->bloom_filter_settings
->bits_per_entry
!= settings
->bits_per_entry
||
556 g
->bloom_filter_settings
->num_hashes
!= settings
->num_hashes
||
557 g
->bloom_filter_settings
->hash_version
!= settings
->hash_version
) {
558 g
->chunk_bloom_indexes
= NULL
;
559 g
->chunk_bloom_data
= NULL
;
560 FREE_AND_NULL(g
->bloom_filter_settings
);
562 warning(_("disabling Bloom filters for commit-graph "
563 "layer '%s' due to incompatible settings"),
564 oid_to_hex(&g
->oid
));
569 static int add_graph_to_chain(struct commit_graph
*g
,
570 struct commit_graph
*chain
,
571 struct object_id
*oids
,
574 struct commit_graph
*cur_g
= chain
;
576 if (n
&& !g
->chunk_base_graphs
) {
577 warning(_("commit-graph has no base graphs chunk"));
581 if (g
->chunk_base_graphs_size
/ g
->hash_len
< n
) {
582 warning(_("commit-graph base graphs chunk is too small"));
590 !oideq(&oids
[n
], &cur_g
->oid
) ||
591 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ st_mult(g
->hash_len
, n
),
592 the_repository
->hash_algo
)) {
593 warning(_("commit-graph chain does not match"));
597 cur_g
= cur_g
->base_graph
;
601 if (unsigned_add_overflows(chain
->num_commits
,
602 chain
->num_commits_in_base
)) {
603 warning(_("commit count in base graph too high: %"PRIuMAX
),
604 (uintmax_t)chain
->num_commits_in_base
);
607 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
610 g
->base_graph
= chain
;
615 int open_commit_graph_chain(const char *chain_file
,
616 int *fd
, struct stat
*st
)
618 *fd
= git_open(chain_file
);
621 if (fstat(*fd
, st
)) {
625 if (st
->st_size
< the_hash_algo
->hexsz
) {
628 /* treat empty files the same as missing */
631 warning(_("commit-graph chain file too small"));
639 struct commit_graph
*load_commit_graph_chain_fd_st(struct repository
*r
,
640 int fd
, struct stat
*st
,
641 int *incomplete_chain
)
643 struct commit_graph
*graph_chain
= NULL
;
644 struct strbuf line
= STRBUF_INIT
;
645 struct object_id
*oids
;
646 int i
= 0, valid
= 1, count
;
647 FILE *fp
= xfdopen(fd
, "r");
649 count
= st
->st_size
/ (the_hash_algo
->hexsz
+ 1);
650 CALLOC_ARRAY(oids
, count
);
654 for (i
= 0; i
< count
; i
++) {
655 struct object_directory
*odb
;
657 if (strbuf_getline_lf(&line
, fp
) == EOF
)
660 if (get_oid_hex(line
.buf
, &oids
[i
])) {
661 warning(_("invalid commit-graph chain: line '%s' not a hash"),
668 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
669 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
670 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
675 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
679 free_commit_graph(g
);
687 warning(_("unable to find all commit-graph files"));
692 validate_mixed_generation_chain(graph_chain
);
693 validate_mixed_bloom_settings(graph_chain
);
697 strbuf_release(&line
);
699 *incomplete_chain
= !valid
;
703 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
704 struct object_directory
*odb
)
706 char *chain_file
= get_commit_graph_chain_filename(odb
);
709 struct commit_graph
*g
= NULL
;
711 if (open_commit_graph_chain(chain_file
, &fd
, &st
)) {
713 /* ownership of fd is taken over by load function */
714 g
= load_commit_graph_chain_fd_st(r
, fd
, &st
, &incomplete
);
721 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
722 struct object_directory
*odb
)
724 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
727 g
= load_commit_graph_chain(r
, odb
);
732 static void prepare_commit_graph_one(struct repository
*r
,
733 struct object_directory
*odb
)
736 if (r
->objects
->commit_graph
)
739 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
743 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
745 * On the first invocation, this function attempts to load the commit
746 * graph if the_repository is configured to have one.
748 static int prepare_commit_graph(struct repository
*r
)
750 struct object_directory
*odb
;
753 * Early return if there is no git dir or if the commit graph is
756 * This must come before the "already attempted?" check below, because
757 * we want to disable even an already-loaded graph file.
759 if (!r
->gitdir
|| r
->commit_graph_disabled
)
762 if (r
->objects
->commit_graph_attempted
)
763 return !!r
->objects
->commit_graph
;
764 r
->objects
->commit_graph_attempted
= 1;
766 prepare_repo_settings(r
);
768 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
769 r
->settings
.core_commit_graph
!= 1)
771 * This repository is not configured to use commit graphs, so
772 * do not load one. (But report commit_graph_attempted anyway
773 * so that commit graph loading is not attempted again for this
778 if (!commit_graph_compatible(r
))
782 for (odb
= r
->objects
->odb
;
783 !r
->objects
->commit_graph
&& odb
;
785 prepare_commit_graph_one(r
, odb
);
786 return !!r
->objects
->commit_graph
;
789 int generation_numbers_enabled(struct repository
*r
)
791 uint32_t first_generation
;
792 struct commit_graph
*g
;
793 if (!prepare_commit_graph(r
))
796 g
= r
->objects
->commit_graph
;
801 first_generation
= get_be32(g
->chunk_commit_data
+
802 g
->hash_len
+ 8) >> 2;
804 return !!first_generation
;
807 int corrected_commit_dates_enabled(struct repository
*r
)
809 struct commit_graph
*g
;
810 if (!prepare_commit_graph(r
))
813 g
= r
->objects
->commit_graph
;
818 return g
->read_generation_data
;
821 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
823 struct commit_graph
*g
= r
->objects
->commit_graph
;
825 if (g
->bloom_filter_settings
)
826 return g
->bloom_filter_settings
;
832 void close_commit_graph(struct raw_object_store
*o
)
834 if (!o
->commit_graph
)
837 clear_commit_graph_data_slab(&commit_graph_data_slab
);
838 deinit_bloom_filters();
839 free_commit_graph(o
->commit_graph
);
840 o
->commit_graph
= NULL
;
843 static int bsearch_graph(struct commit_graph
*g
, const struct object_id
*oid
, uint32_t *pos
)
845 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
846 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
849 static void load_oid_from_graph(struct commit_graph
*g
,
851 struct object_id
*oid
)
855 while (g
&& pos
< g
->num_commits_in_base
)
859 BUG("NULL commit-graph");
861 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
862 die(_("invalid commit position. commit-graph is likely corrupt"));
864 lex_index
= pos
- g
->num_commits_in_base
;
866 oidread(oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, lex_index
),
867 the_repository
->hash_algo
);
870 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
871 struct commit_graph
*g
,
873 struct commit_list
**pptr
)
876 struct object_id oid
;
878 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
879 die("invalid parent position %"PRIu32
, pos
);
881 load_oid_from_graph(g
, pos
, &oid
);
882 c
= lookup_commit(r
, &oid
);
884 die(_("could not find commit %s"), oid_to_hex(&oid
));
885 commit_graph_data_at(c
)->graph_pos
= pos
;
886 return &commit_list_insert(c
, pptr
)->next
;
889 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
891 const unsigned char *commit_data
;
892 struct commit_graph_data
*graph_data
;
893 uint32_t lex_index
, offset_pos
;
894 uint64_t date_high
, date_low
, offset
;
896 while (pos
< g
->num_commits_in_base
)
899 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
900 die(_("invalid commit position. commit-graph is likely corrupt"));
902 lex_index
= pos
- g
->num_commits_in_base
;
903 commit_data
= g
->chunk_commit_data
+ st_mult(GRAPH_DATA_WIDTH
, lex_index
);
905 graph_data
= commit_graph_data_at(item
);
906 graph_data
->graph_pos
= pos
;
908 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
909 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
910 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
912 if (g
->read_generation_data
) {
913 offset
= (timestamp_t
)get_be32(g
->chunk_generation_data
+ st_mult(sizeof(uint32_t), lex_index
));
915 if (offset
& CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
) {
916 if (!g
->chunk_generation_data_overflow
)
917 die(_("commit-graph requires overflow generation data but has none"));
919 offset_pos
= offset
^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
;
920 if (g
->chunk_generation_data_overflow_size
/ sizeof(uint64_t) <= offset_pos
)
921 die(_("commit-graph overflow generation data is too small"));
922 graph_data
->generation
= item
->date
+
923 get_be64(g
->chunk_generation_data_overflow
+ sizeof(uint64_t) * offset_pos
);
925 graph_data
->generation
= item
->date
+ offset
;
927 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
930 *topo_level_slab_at(g
->topo_levels
, item
) = get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
933 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
938 static int fill_commit_in_graph(struct repository
*r
,
940 struct commit_graph
*g
, uint32_t pos
)
943 uint32_t parent_data_pos
;
944 struct commit_list
**pptr
;
945 const unsigned char *commit_data
;
948 while (pos
< g
->num_commits_in_base
)
951 fill_commit_graph_info(item
, g
, pos
);
953 lex_index
= pos
- g
->num_commits_in_base
;
954 commit_data
= g
->chunk_commit_data
+ st_mult(g
->hash_len
+ 16, lex_index
);
956 item
->object
.parsed
= 1;
958 set_commit_tree(item
, NULL
);
960 pptr
= &item
->parents
;
962 edge_value
= get_be32(commit_data
+ g
->hash_len
);
963 if (edge_value
== GRAPH_PARENT_NONE
)
965 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
967 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
968 if (edge_value
== GRAPH_PARENT_NONE
)
970 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
971 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
975 parent_data_pos
= edge_value
& GRAPH_EDGE_LAST_MASK
;
977 if (g
->chunk_extra_edges_size
/ sizeof(uint32_t) <= parent_data_pos
) {
978 error(_("commit-graph extra-edges pointer out of bounds"));
979 free_commit_list(item
->parents
);
980 item
->parents
= NULL
;
981 item
->object
.parsed
= 0;
984 edge_value
= get_be32(g
->chunk_extra_edges
+
985 sizeof(uint32_t) * parent_data_pos
);
986 pptr
= insert_parent_or_die(r
, g
,
987 edge_value
& GRAPH_EDGE_LAST_MASK
,
990 } while (!(edge_value
& GRAPH_LAST_EDGE
));
995 static int search_commit_pos_in_graph(const struct object_id
*id
, struct commit_graph
*g
, uint32_t *pos
)
997 struct commit_graph
*cur_g
= g
;
1000 while (cur_g
&& !bsearch_graph(cur_g
, id
, &lex_index
))
1001 cur_g
= cur_g
->base_graph
;
1004 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
1011 static int find_commit_pos_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
1013 uint32_t graph_pos
= commit_graph_position(item
);
1014 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
1018 return search_commit_pos_in_graph(&item
->object
.oid
, g
, pos
);
1022 int repo_find_commit_pos_in_graph(struct repository
*r
, struct commit
*c
,
1025 if (!prepare_commit_graph(r
))
1027 return find_commit_pos_in_graph(c
, r
->objects
->commit_graph
, pos
);
1030 struct commit
*lookup_commit_in_graph(struct repository
*repo
, const struct object_id
*id
)
1032 static int commit_graph_paranoia
= -1;
1033 struct commit
*commit
;
1036 if (commit_graph_paranoia
== -1)
1037 commit_graph_paranoia
= git_env_bool(GIT_COMMIT_GRAPH_PARANOIA
, 0);
1039 if (!prepare_commit_graph(repo
))
1041 if (!search_commit_pos_in_graph(id
, repo
->objects
->commit_graph
, &pos
))
1043 if (commit_graph_paranoia
&& !has_object(repo
, id
, 0))
1046 commit
= lookup_commit(repo
, id
);
1049 if (commit
->object
.parsed
)
1052 if (!fill_commit_in_graph(repo
, commit
, repo
->objects
->commit_graph
, pos
))
1058 static int parse_commit_in_graph_one(struct repository
*r
,
1059 struct commit_graph
*g
,
1060 struct commit
*item
)
1064 if (item
->object
.parsed
)
1067 if (find_commit_pos_in_graph(item
, g
, &pos
))
1068 return fill_commit_in_graph(r
, item
, g
, pos
);
1073 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
1075 static int checked_env
= 0;
1078 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
1079 die("dying as requested by the '%s' variable on commit-graph parse!",
1080 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
1083 if (!prepare_commit_graph(r
))
1085 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
1088 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
1091 if (repo_find_commit_pos_in_graph(r
, item
, &pos
))
1092 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
1095 static struct tree
*load_tree_for_commit(struct repository
*r
,
1096 struct commit_graph
*g
,
1099 struct object_id oid
;
1100 const unsigned char *commit_data
;
1101 uint32_t graph_pos
= commit_graph_position(c
);
1103 while (graph_pos
< g
->num_commits_in_base
)
1106 commit_data
= g
->chunk_commit_data
+
1107 st_mult(GRAPH_DATA_WIDTH
, graph_pos
- g
->num_commits_in_base
);
1109 oidread(&oid
, commit_data
, the_repository
->hash_algo
);
1110 set_commit_tree(c
, lookup_tree(r
, &oid
));
1112 return c
->maybe_tree
;
1115 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
1116 struct commit_graph
*g
,
1117 const struct commit
*c
)
1120 return c
->maybe_tree
;
1121 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
1122 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
1124 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
1127 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
1129 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
1132 struct packed_commit_list
{
1133 struct commit
**list
;
1138 struct write_commit_graph_context
{
1139 struct repository
*r
;
1140 struct object_directory
*odb
;
1142 struct oid_array oids
;
1143 struct packed_commit_list commits
;
1144 int num_extra_edges
;
1145 int num_generation_data_overflows
;
1146 unsigned long approx_nr_objects
;
1147 struct progress
*progress
;
1149 uint64_t progress_cnt
;
1151 char *base_graph_name
;
1152 int num_commit_graphs_before
;
1153 int num_commit_graphs_after
;
1154 char **commit_graph_filenames_before
;
1155 char **commit_graph_filenames_after
;
1156 char **commit_graph_hash_after
;
1157 uint32_t new_num_commits_in_base
;
1158 struct commit_graph
*new_base_graph
;
1165 write_generation_data
:1,
1166 trust_generation_numbers
:1;
1168 struct topo_level_slab
*topo_levels
;
1169 const struct commit_graph_opts
*opts
;
1170 size_t total_bloom_filter_data_size
;
1171 const struct bloom_filter_settings
*bloom_settings
;
1173 int count_bloom_filter_computed
;
1174 int count_bloom_filter_not_computed
;
1175 int count_bloom_filter_trunc_empty
;
1176 int count_bloom_filter_trunc_large
;
1177 int count_bloom_filter_upgraded
;
1180 static int write_graph_chunk_fanout(struct hashfile
*f
,
1183 struct write_commit_graph_context
*ctx
= data
;
1185 struct commit
**list
= ctx
->commits
.list
;
1188 * Write the first-level table (the list is sorted,
1189 * but we use a 256-entry lookup to be able to avoid
1190 * having to do eight extra binary search iterations).
1192 for (i
= 0; i
< 256; i
++) {
1193 while (count
< ctx
->commits
.nr
) {
1194 if ((*list
)->object
.oid
.hash
[0] != i
)
1196 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1201 hashwrite_be32(f
, count
);
1207 static int write_graph_chunk_oids(struct hashfile
*f
,
1210 struct write_commit_graph_context
*ctx
= data
;
1211 struct commit
**list
= ctx
->commits
.list
;
1213 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1214 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1215 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1221 static const struct object_id
*commit_to_oid(size_t index
, const void *table
)
1223 const struct commit
* const *commits
= table
;
1224 return &commits
[index
]->object
.oid
;
1227 static int write_graph_chunk_data(struct hashfile
*f
,
1230 struct write_commit_graph_context
*ctx
= data
;
1231 struct commit
**list
= ctx
->commits
.list
;
1232 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1233 uint32_t num_extra_edges
= 0;
1235 while (list
< last
) {
1236 struct commit_list
*parent
;
1237 struct object_id
*tree
;
1239 uint32_t packedDate
[2];
1240 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1242 if (repo_parse_commit_no_graph(ctx
->r
, *list
))
1243 die(_("unable to parse commit %s"),
1244 oid_to_hex(&(*list
)->object
.oid
));
1245 tree
= get_commit_tree_oid(*list
);
1246 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1248 parent
= (*list
)->parents
;
1251 edge_value
= GRAPH_PARENT_NONE
;
1253 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
));
1274 hashwrite_be32(f
, edge_value
);
1277 parent
= parent
->next
;
1280 edge_value
= GRAPH_PARENT_NONE
;
1281 else if (parent
->next
)
1282 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1284 edge_value
= oid_pos(&parent
->item
->object
.oid
,
1289 if (edge_value
>= 0)
1290 edge_value
+= ctx
->new_num_commits_in_base
;
1291 else if (ctx
->new_base_graph
) {
1293 if (find_commit_pos_in_graph(parent
->item
,
1294 ctx
->new_base_graph
,
1300 BUG("missing parent %s for commit %s",
1301 oid_to_hex(&parent
->item
->object
.oid
),
1302 oid_to_hex(&(*list
)->object
.oid
));
1305 hashwrite_be32(f
, edge_value
);
1307 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1310 parent
= parent
->next
;
1314 if (sizeof((*list
)->date
) > 4)
1315 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1319 packedDate
[0] |= htonl(*topo_level_slab_at(ctx
->topo_levels
, *list
) << 2);
1321 packedDate
[1] = htonl((*list
)->date
);
1322 hashwrite(f
, packedDate
, 8);
1330 static int write_graph_chunk_generation_data(struct hashfile
*f
,
1333 struct write_commit_graph_context
*ctx
= data
;
1334 int i
, num_generation_data_overflows
= 0;
1336 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1337 struct commit
*c
= ctx
->commits
.list
[i
];
1339 repo_parse_commit(ctx
->r
, c
);
1340 offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1341 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1343 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1344 offset
= CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW
| num_generation_data_overflows
;
1345 num_generation_data_overflows
++;
1348 hashwrite_be32(f
, offset
);
1354 static int write_graph_chunk_generation_data_overflow(struct hashfile
*f
,
1357 struct write_commit_graph_context
*ctx
= data
;
1359 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1360 struct commit
*c
= ctx
->commits
.list
[i
];
1361 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1362 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1364 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
) {
1365 hashwrite_be32(f
, offset
>> 32);
1366 hashwrite_be32(f
, (uint32_t) offset
);
1373 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1376 struct write_commit_graph_context
*ctx
= data
;
1377 struct commit
**list
= ctx
->commits
.list
;
1378 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1379 struct commit_list
*parent
;
1381 while (list
< last
) {
1382 int num_parents
= 0;
1384 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1386 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1387 parent
= parent
->next
)
1390 if (num_parents
<= 2) {
1395 /* Since num_parents > 2, this initializer is safe. */
1396 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1397 int edge_value
= oid_pos(&parent
->item
->object
.oid
,
1402 if (edge_value
>= 0)
1403 edge_value
+= ctx
->new_num_commits_in_base
;
1404 else if (ctx
->new_base_graph
) {
1406 if (find_commit_pos_in_graph(parent
->item
,
1407 ctx
->new_base_graph
,
1413 BUG("missing parent %s for commit %s",
1414 oid_to_hex(&parent
->item
->object
.oid
),
1415 oid_to_hex(&(*list
)->object
.oid
));
1416 else if (!parent
->next
)
1417 edge_value
|= GRAPH_LAST_EDGE
;
1419 hashwrite_be32(f
, edge_value
);
1428 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1431 struct write_commit_graph_context
*ctx
= data
;
1432 struct commit
**list
= ctx
->commits
.list
;
1433 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1434 uint32_t cur_pos
= 0;
1436 while (list
< last
) {
1437 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1438 size_t len
= filter
? filter
->len
: 0;
1440 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1441 hashwrite_be32(f
, cur_pos
);
1448 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1450 struct json_writer jw
= JSON_WRITER_INIT
;
1452 jw_object_begin(&jw
, 0);
1453 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1454 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1455 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1456 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1459 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1464 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1467 struct write_commit_graph_context
*ctx
= data
;
1468 struct commit
**list
= ctx
->commits
.list
;
1469 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1471 trace2_bloom_filter_settings(ctx
);
1473 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1474 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1475 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1477 while (list
< last
) {
1478 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1479 size_t len
= filter
? filter
->len
: 0;
1481 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1483 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1490 static int add_packed_commits(const struct object_id
*oid
,
1491 struct packed_git
*pack
,
1495 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1496 enum object_type type
;
1497 off_t offset
= nth_packed_object_offset(pack
, pos
);
1498 struct object_info oi
= OBJECT_INFO_INIT
;
1501 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1504 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1505 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1507 if (type
!= OBJ_COMMIT
)
1510 oid_array_append(&ctx
->oids
, oid
);
1511 set_commit_pos(ctx
->r
, oid
);
1516 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1518 struct commit_list
*parent
;
1519 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1520 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1521 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1522 parent
->item
->object
.flags
|= REACHABLE
;
1527 static void close_reachable(struct write_commit_graph_context
*ctx
)
1530 struct commit
*commit
;
1531 enum commit_graph_split_flags flags
= ctx
->opts
?
1532 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1534 if (ctx
->report_progress
)
1535 ctx
->progress
= start_delayed_progress(
1536 _("Loading known commits in commit graph"),
1538 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1539 display_progress(ctx
->progress
, i
+ 1);
1540 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1542 commit
->object
.flags
|= REACHABLE
;
1544 stop_progress(&ctx
->progress
);
1547 * As this loop runs, ctx->oids.nr may grow, but not more
1548 * than the number of missing commits in the reachable
1551 if (ctx
->report_progress
)
1552 ctx
->progress
= start_delayed_progress(
1553 _("Expanding reachable commits in commit graph"),
1555 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1556 display_progress(ctx
->progress
, i
+ 1);
1557 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1562 if ((!repo_parse_commit(ctx
->r
, commit
) &&
1563 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1564 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1565 add_missing_parents(ctx
, commit
);
1566 } else if (!repo_parse_commit_no_graph(ctx
->r
, commit
))
1567 add_missing_parents(ctx
, commit
);
1569 stop_progress(&ctx
->progress
);
1571 if (ctx
->report_progress
)
1572 ctx
->progress
= start_delayed_progress(
1573 _("Clearing commit marks in commit graph"),
1575 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1576 display_progress(ctx
->progress
, i
+ 1);
1577 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1580 commit
->object
.flags
&= ~REACHABLE
;
1582 stop_progress(&ctx
->progress
);
1585 struct compute_generation_info
{
1586 struct repository
*r
;
1587 struct packed_commit_list
*commits
;
1588 struct progress
*progress
;
1591 timestamp_t (*get_generation
)(struct commit
*c
, void *data
);
1592 void (*set_generation
)(struct commit
*c
, timestamp_t gen
, void *data
);
1596 static timestamp_t
compute_generation_from_max(struct commit
*c
,
1597 timestamp_t max_gen
,
1598 int generation_version
)
1600 switch (generation_version
) {
1601 case 1: /* topological levels */
1602 if (max_gen
> GENERATION_NUMBER_V1_MAX
- 1)
1603 max_gen
= GENERATION_NUMBER_V1_MAX
- 1;
1606 case 2: /* corrected commit date */
1607 if (c
->date
&& c
->date
> max_gen
)
1608 max_gen
= c
->date
- 1;
1612 BUG("attempting unimplemented version");
1616 static void compute_reachable_generation_numbers(
1617 struct compute_generation_info
*info
,
1618 int generation_version
)
1621 struct commit_list
*list
= NULL
;
1623 for (i
= 0; i
< info
->commits
->nr
; i
++) {
1624 struct commit
*c
= info
->commits
->list
[i
];
1626 repo_parse_commit(info
->r
, c
);
1627 gen
= info
->get_generation(c
, info
->data
);
1628 display_progress(info
->progress
, ++info
->progress_cnt
);
1630 if (gen
!= GENERATION_NUMBER_ZERO
&& gen
!= GENERATION_NUMBER_INFINITY
)
1633 commit_list_insert(c
, &list
);
1635 struct commit
*current
= list
->item
;
1636 struct commit_list
*parent
;
1637 int all_parents_computed
= 1;
1638 uint32_t max_gen
= 0;
1640 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1641 repo_parse_commit(info
->r
, parent
->item
);
1642 gen
= info
->get_generation(parent
->item
, info
->data
);
1644 if (gen
== GENERATION_NUMBER_ZERO
) {
1645 all_parents_computed
= 0;
1646 commit_list_insert(parent
->item
, &list
);
1654 if (all_parents_computed
) {
1656 gen
= compute_generation_from_max(
1658 generation_version
);
1659 info
->set_generation(current
, gen
, info
->data
);
1665 static timestamp_t
get_topo_level(struct commit
*c
, void *data
)
1667 struct write_commit_graph_context
*ctx
= data
;
1668 return *topo_level_slab_at(ctx
->topo_levels
, c
);
1671 static void set_topo_level(struct commit
*c
, timestamp_t t
, void *data
)
1673 struct write_commit_graph_context
*ctx
= data
;
1674 *topo_level_slab_at(ctx
->topo_levels
, c
) = (uint32_t)t
;
1677 static void compute_topological_levels(struct write_commit_graph_context
*ctx
)
1679 struct compute_generation_info info
= {
1681 .commits
= &ctx
->commits
,
1682 .get_generation
= get_topo_level
,
1683 .set_generation
= set_topo_level
,
1687 if (ctx
->report_progress
)
1688 info
.progress
= ctx
->progress
1689 = start_delayed_progress(
1690 _("Computing commit graph topological levels"),
1693 compute_reachable_generation_numbers(&info
, 1);
1695 stop_progress(&ctx
->progress
);
1698 static timestamp_t
get_generation_from_graph_data(struct commit
*c
,
1701 return commit_graph_data_at(c
)->generation
;
1704 static void set_generation_v2(struct commit
*c
, timestamp_t t
,
1707 struct commit_graph_data
*g
= commit_graph_data_at(c
);
1711 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1714 struct compute_generation_info info
= {
1716 .commits
= &ctx
->commits
,
1717 .get_generation
= get_generation_from_graph_data
,
1718 .set_generation
= set_generation_v2
,
1721 if (ctx
->report_progress
)
1722 info
.progress
= ctx
->progress
1723 = start_delayed_progress(
1724 _("Computing commit graph generation numbers"),
1727 if (!ctx
->trust_generation_numbers
) {
1728 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1729 struct commit
*c
= ctx
->commits
.list
[i
];
1730 repo_parse_commit(ctx
->r
, c
);
1731 commit_graph_data_at(c
)->generation
= GENERATION_NUMBER_ZERO
;
1735 compute_reachable_generation_numbers(&info
, 2);
1737 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1738 struct commit
*c
= ctx
->commits
.list
[i
];
1739 timestamp_t offset
= commit_graph_data_at(c
)->generation
- c
->date
;
1740 if (offset
> GENERATION_NUMBER_V2_OFFSET_MAX
)
1741 ctx
->num_generation_data_overflows
++;
1743 stop_progress(&ctx
->progress
);
1746 static void set_generation_in_graph_data(struct commit
*c
, timestamp_t t
,
1749 commit_graph_data_at(c
)->generation
= t
;
1753 * After this method, all commits reachable from those in the given
1754 * list will have non-zero, non-infinite generation numbers.
1756 void ensure_generations_valid(struct repository
*r
,
1757 struct commit
**commits
, size_t nr
)
1759 int generation_version
= get_configured_generation_version(r
);
1760 struct packed_commit_list list
= {
1765 struct compute_generation_info info
= {
1768 .get_generation
= get_generation_from_graph_data
,
1769 .set_generation
= set_generation_in_graph_data
,
1772 compute_reachable_generation_numbers(&info
, generation_version
);
1775 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1777 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1778 ctx
->count_bloom_filter_computed
);
1779 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1780 ctx
->count_bloom_filter_not_computed
);
1781 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1782 ctx
->count_bloom_filter_trunc_empty
);
1783 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1784 ctx
->count_bloom_filter_trunc_large
);
1785 trace2_data_intmax("commit-graph", ctx
->r
, "filter-upgraded",
1786 ctx
->count_bloom_filter_upgraded
);
1789 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1792 struct progress
*progress
= NULL
;
1793 struct commit
**sorted_commits
;
1794 int max_new_filters
;
1796 init_bloom_filters();
1798 if (ctx
->report_progress
)
1799 progress
= start_delayed_progress(
1800 _("Computing commit changed paths Bloom filters"),
1803 DUP_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1805 if (ctx
->order_by_pack
)
1806 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1808 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1810 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1811 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1813 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1814 enum bloom_filter_computed computed
= 0;
1815 struct commit
*c
= sorted_commits
[i
];
1816 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1819 ctx
->count_bloom_filter_computed
< max_new_filters
,
1820 ctx
->bloom_settings
,
1822 if (computed
& BLOOM_COMPUTED
) {
1823 ctx
->count_bloom_filter_computed
++;
1824 if (computed
& BLOOM_TRUNC_EMPTY
)
1825 ctx
->count_bloom_filter_trunc_empty
++;
1826 if (computed
& BLOOM_TRUNC_LARGE
)
1827 ctx
->count_bloom_filter_trunc_large
++;
1828 } else if (computed
& BLOOM_UPGRADED
) {
1829 ctx
->count_bloom_filter_upgraded
++;
1830 } else if (computed
& BLOOM_NOT_COMPUTED
)
1831 ctx
->count_bloom_filter_not_computed
++;
1832 ctx
->total_bloom_filter_data_size
+= filter
1833 ? sizeof(unsigned char) * filter
->len
: 0;
1834 display_progress(progress
, i
+ 1);
1837 if (trace2_is_enabled())
1838 trace2_bloom_filter_write_statistics(ctx
);
1840 free(sorted_commits
);
1841 stop_progress(&progress
);
1844 struct refs_cb_data
{
1845 struct oidset
*commits
;
1846 struct progress
*progress
;
1849 static int add_ref_to_set(const char *refname UNUSED
,
1850 const char *referent UNUSED
,
1851 const struct object_id
*oid
,
1852 int flags UNUSED
, void *cb_data
)
1854 struct object_id peeled
;
1855 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1857 if (!peel_iterated_oid(the_repository
, oid
, &peeled
))
1859 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1860 oidset_insert(data
->commits
, oid
);
1862 display_progress(data
->progress
, oidset_size(data
->commits
));
1867 int write_commit_graph_reachable(struct object_directory
*odb
,
1868 enum commit_graph_write_flags flags
,
1869 const struct commit_graph_opts
*opts
)
1871 struct oidset commits
= OIDSET_INIT
;
1872 struct refs_cb_data data
;
1875 memset(&data
, 0, sizeof(data
));
1876 data
.commits
= &commits
;
1877 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1878 data
.progress
= start_delayed_progress(
1879 _("Collecting referenced commits"), 0);
1881 refs_for_each_ref(get_main_ref_store(the_repository
), add_ref_to_set
,
1884 stop_progress(&data
.progress
);
1886 result
= write_commit_graph(odb
, NULL
, &commits
,
1889 oidset_clear(&commits
);
1893 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1894 const struct string_list
*pack_indexes
)
1897 struct strbuf progress_title
= STRBUF_INIT
;
1898 struct strbuf packname
= STRBUF_INIT
;
1902 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1903 dirlen
= packname
.len
;
1904 if (ctx
->report_progress
) {
1905 strbuf_addf(&progress_title
,
1906 Q_("Finding commits for commit graph in %"PRIuMAX
" pack",
1907 "Finding commits for commit graph in %"PRIuMAX
" packs",
1909 (uintmax_t)pack_indexes
->nr
);
1910 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1911 ctx
->progress_done
= 0;
1913 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1914 struct packed_git
*p
;
1915 strbuf_setlen(&packname
, dirlen
);
1916 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1917 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1919 ret
= error(_("error adding pack %s"), packname
.buf
);
1922 if (open_pack_index(p
)) {
1923 ret
= error(_("error opening index for %s"), packname
.buf
);
1926 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1927 FOR_EACH_OBJECT_PACK_ORDER
);
1933 stop_progress(&ctx
->progress
);
1934 strbuf_release(&progress_title
);
1935 strbuf_release(&packname
);
1940 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1941 struct oidset
*commits
)
1943 struct oidset_iter iter
;
1944 struct object_id
*oid
;
1946 if (!oidset_size(commits
))
1949 oidset_iter_init(commits
, &iter
);
1950 while ((oid
= oidset_iter_next(&iter
))) {
1951 oid_array_append(&ctx
->oids
, oid
);
1957 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1959 if (ctx
->report_progress
)
1960 ctx
->progress
= start_delayed_progress(
1961 _("Finding commits for commit graph among packed objects"),
1962 ctx
->approx_nr_objects
);
1963 for_each_packed_object(add_packed_commits
, ctx
,
1964 FOR_EACH_OBJECT_PACK_ORDER
);
1965 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1966 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1967 stop_progress(&ctx
->progress
);
1970 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1973 enum commit_graph_split_flags flags
= ctx
->opts
?
1974 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1976 ctx
->num_extra_edges
= 0;
1977 if (ctx
->report_progress
)
1978 ctx
->progress
= start_delayed_progress(
1979 _("Finding extra edges in commit graph"),
1981 oid_array_sort(&ctx
->oids
);
1982 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1983 unsigned int num_parents
;
1985 display_progress(ctx
->progress
, i
+ 1);
1987 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1988 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1990 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1991 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1994 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1995 repo_parse_commit(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1997 repo_parse_commit_no_graph(ctx
->r
, ctx
->commits
.list
[ctx
->commits
.nr
]);
1999 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
2000 if (num_parents
> 2)
2001 ctx
->num_extra_edges
+= num_parents
- 1;
2005 stop_progress(&ctx
->progress
);
2008 static int write_graph_chunk_base_1(struct hashfile
*f
,
2009 struct commit_graph
*g
)
2016 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
2017 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
2021 static int write_graph_chunk_base(struct hashfile
*f
,
2024 struct write_commit_graph_context
*ctx
= data
;
2025 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
2027 if (num
!= ctx
->num_commit_graphs_after
- 1) {
2028 error(_("failed to write correct number of base graph ids"));
2035 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
2039 struct tempfile
*graph_layer
; /* when ctx->split is non-zero */
2040 struct lock_file lk
= LOCK_INIT
;
2041 const unsigned hashsz
= the_hash_algo
->rawsz
;
2042 struct strbuf progress_title
= STRBUF_INIT
;
2043 struct chunkfile
*cf
;
2044 unsigned char file_hash
[GIT_MAX_RAWSZ
];
2047 struct strbuf tmp_file
= STRBUF_INIT
;
2049 strbuf_addf(&tmp_file
,
2050 "%s/info/commit-graphs/tmp_graph_XXXXXX",
2052 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
2054 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
2057 if (safe_create_leading_directories(ctx
->graph_name
)) {
2058 UNLEAK(ctx
->graph_name
);
2059 error(_("unable to create leading directories of %s"),
2065 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
2067 hold_lock_file_for_update_mode(&lk
, lock_name
,
2068 LOCK_DIE_ON_ERROR
, 0444);
2071 graph_layer
= mks_tempfile_m(ctx
->graph_name
, 0444);
2073 error(_("unable to create temporary graph layer"));
2077 if (adjust_shared_perm(get_tempfile_path(graph_layer
))) {
2078 error(_("unable to adjust shared permissions for '%s'"),
2079 get_tempfile_path(graph_layer
));
2083 f
= hashfd(get_tempfile_fd(graph_layer
), get_tempfile_path(graph_layer
));
2085 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
2086 LOCK_DIE_ON_ERROR
, 0444);
2087 f
= hashfd(get_lock_file_fd(&lk
), get_lock_file_path(&lk
));
2090 cf
= init_chunkfile(f
);
2092 add_chunk(cf
, GRAPH_CHUNKID_OIDFANOUT
, GRAPH_FANOUT_SIZE
,
2093 write_graph_chunk_fanout
);
2094 add_chunk(cf
, GRAPH_CHUNKID_OIDLOOKUP
, st_mult(hashsz
, ctx
->commits
.nr
),
2095 write_graph_chunk_oids
);
2096 add_chunk(cf
, GRAPH_CHUNKID_DATA
, st_mult(hashsz
+ 16, ctx
->commits
.nr
),
2097 write_graph_chunk_data
);
2099 if (ctx
->write_generation_data
)
2100 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA
,
2101 st_mult(sizeof(uint32_t), ctx
->commits
.nr
),
2102 write_graph_chunk_generation_data
);
2103 if (ctx
->num_generation_data_overflows
)
2104 add_chunk(cf
, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW
,
2105 st_mult(sizeof(timestamp_t
), ctx
->num_generation_data_overflows
),
2106 write_graph_chunk_generation_data_overflow
);
2107 if (ctx
->num_extra_edges
)
2108 add_chunk(cf
, GRAPH_CHUNKID_EXTRAEDGES
,
2109 st_mult(4, ctx
->num_extra_edges
),
2110 write_graph_chunk_extra_edges
);
2111 if (ctx
->changed_paths
) {
2112 add_chunk(cf
, GRAPH_CHUNKID_BLOOMINDEXES
,
2113 st_mult(sizeof(uint32_t), ctx
->commits
.nr
),
2114 write_graph_chunk_bloom_indexes
);
2115 add_chunk(cf
, GRAPH_CHUNKID_BLOOMDATA
,
2116 st_add(sizeof(uint32_t) * 3,
2117 ctx
->total_bloom_filter_data_size
),
2118 write_graph_chunk_bloom_data
);
2120 if (ctx
->num_commit_graphs_after
> 1)
2121 add_chunk(cf
, GRAPH_CHUNKID_BASE
,
2122 st_mult(hashsz
, ctx
->num_commit_graphs_after
- 1),
2123 write_graph_chunk_base
);
2125 hashwrite_be32(f
, GRAPH_SIGNATURE
);
2127 hashwrite_u8(f
, GRAPH_VERSION
);
2128 hashwrite_u8(f
, oid_version(the_hash_algo
));
2129 hashwrite_u8(f
, get_num_chunks(cf
));
2130 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
2132 if (ctx
->report_progress
) {
2133 strbuf_addf(&progress_title
,
2134 Q_("Writing out commit graph in %d pass",
2135 "Writing out commit graph in %d passes",
2136 get_num_chunks(cf
)),
2137 get_num_chunks(cf
));
2138 ctx
->progress
= start_delayed_progress(
2140 st_mult(get_num_chunks(cf
), ctx
->commits
.nr
));
2143 write_chunkfile(cf
, ctx
);
2145 stop_progress(&ctx
->progress
);
2146 strbuf_release(&progress_title
);
2148 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
2149 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
2150 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
2152 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
2153 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
2154 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
2155 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
2158 close_commit_graph(ctx
->r
->objects
);
2159 finalize_hashfile(f
, file_hash
, FSYNC_COMPONENT_COMMIT_GRAPH
,
2160 CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
2164 FILE *chainf
= fdopen_lock_file(&lk
, "w");
2165 char *final_graph_name
;
2169 error(_("unable to open commit-graph chain file"));
2173 if (ctx
->base_graph_name
) {
2175 int idx
= ctx
->num_commit_graphs_after
- 1;
2176 if (ctx
->num_commit_graphs_after
> 1)
2179 dest
= ctx
->commit_graph_filenames_after
[idx
];
2181 if (strcmp(ctx
->base_graph_name
, dest
)) {
2182 result
= rename(ctx
->base_graph_name
, dest
);
2185 error(_("failed to rename base commit-graph file"));
2190 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
2195 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
2196 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(hash_to_hex(file_hash
));
2197 final_graph_name
= get_split_graph_filename(ctx
->odb
,
2198 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
2199 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1]);
2200 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
2202 result
= rename_tempfile(&graph_layer
, final_graph_name
);
2204 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
2205 fprintf(get_lock_file_fp(&lk
), "%s\n", ctx
->commit_graph_hash_after
[i
]);
2208 error(_("failed to rename temporary commit-graph file"));
2213 commit_lock_file(&lk
);
2218 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
2220 struct commit_graph
*g
;
2221 uint32_t num_commits
;
2222 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
2225 int max_commits
= 0;
2229 max_commits
= ctx
->opts
->max_commits
;
2231 if (ctx
->opts
->size_multiple
)
2232 size_mult
= ctx
->opts
->size_multiple
;
2234 flags
= ctx
->opts
->split_flags
;
2237 g
= ctx
->r
->objects
->commit_graph
;
2238 num_commits
= ctx
->commits
.nr
;
2239 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
2240 ctx
->num_commit_graphs_after
= 1;
2242 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
2244 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
2245 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
2246 while (g
&& (g
->num_commits
<= st_mult(size_mult
, num_commits
) ||
2247 (max_commits
&& num_commits
> max_commits
))) {
2248 if (g
->odb
!= ctx
->odb
)
2251 if (unsigned_add_overflows(num_commits
, g
->num_commits
))
2252 die(_("cannot merge graphs with %"PRIuMAX
", "
2253 "%"PRIuMAX
" commits"),
2254 (uintmax_t)num_commits
,
2255 (uintmax_t)g
->num_commits
);
2256 num_commits
+= g
->num_commits
;
2259 ctx
->num_commit_graphs_after
--;
2263 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
2264 ctx
->new_base_graph
= g
;
2265 else if (ctx
->num_commit_graphs_after
!= 1)
2266 BUG("split_graph_merge_strategy: num_commit_graphs_after "
2267 "should be 1 with --split=replace");
2269 if (ctx
->num_commit_graphs_after
== 2) {
2270 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
2272 if (!strcmp(g
->filename
, old_graph_name
) &&
2273 g
->odb
!= ctx
->odb
) {
2274 ctx
->num_commit_graphs_after
= 1;
2275 ctx
->new_base_graph
= NULL
;
2278 free(old_graph_name
);
2281 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
2282 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
2284 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
2285 i
< ctx
->num_commit_graphs_before
; i
++)
2286 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
2288 i
= ctx
->num_commit_graphs_before
- 1;
2289 g
= ctx
->r
->objects
->commit_graph
;
2292 if (i
< ctx
->num_commit_graphs_after
)
2293 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
2296 * If the topmost remaining layer has generation data chunk, the
2297 * resultant layer also has generation data chunk.
2299 if (i
== ctx
->num_commit_graphs_after
- 2)
2300 ctx
->write_generation_data
= !!g
->chunk_generation_data
;
2307 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
2308 struct commit_graph
*g
)
2311 uint32_t offset
= g
->num_commits_in_base
;
2313 if (unsigned_add_overflows(ctx
->commits
.nr
, g
->num_commits
))
2314 die(_("cannot merge graph %s, too many commits: %"PRIuMAX
),
2315 oid_to_hex(&g
->oid
),
2316 (uintmax_t)st_add(ctx
->commits
.nr
, g
->num_commits
));
2318 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
2320 for (i
= 0; i
< g
->num_commits
; i
++) {
2321 struct object_id oid
;
2322 struct commit
*result
;
2324 display_progress(ctx
->progress
, i
+ 1);
2326 load_oid_from_graph(g
, i
+ offset
, &oid
);
2328 /* only add commits if they still exist in the repo */
2329 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
2332 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2338 static int commit_compare(const void *_a
, const void *_b
)
2340 const struct commit
*a
= *(const struct commit
**)_a
;
2341 const struct commit
*b
= *(const struct commit
**)_b
;
2342 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2345 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2347 uint32_t i
, dedup_i
= 0;
2349 if (ctx
->report_progress
)
2350 ctx
->progress
= start_delayed_progress(
2351 _("Scanning merged commits"),
2354 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2356 ctx
->num_extra_edges
= 0;
2357 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2358 display_progress(ctx
->progress
, i
+ 1);
2360 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2361 &ctx
->commits
.list
[i
]->object
.oid
)) {
2363 * Silently ignore duplicates. These were likely
2364 * created due to a commit appearing in multiple
2365 * layers of the chain, which is unexpected but
2366 * not invalid. We should make sure there is a
2367 * unique copy in the new layer.
2370 unsigned int num_parents
;
2372 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
2375 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2376 if (num_parents
> 2)
2377 ctx
->num_extra_edges
+= num_parents
- 1;
2381 ctx
->commits
.nr
= dedup_i
;
2383 stop_progress(&ctx
->progress
);
2386 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2388 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2389 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2391 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2392 current_graph_number
--;
2394 if (ctx
->report_progress
)
2395 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2397 merge_commit_graph(ctx
, g
);
2398 stop_progress(&ctx
->progress
);
2404 ctx
->new_base_graph
= g
;
2405 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2408 if (ctx
->new_base_graph
)
2409 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2411 sort_and_scan_merged_commits(ctx
);
2414 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2417 time_t now
= time(NULL
);
2419 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2421 struct utimbuf updated_time
;
2423 if (stat(ctx
->commit_graph_filenames_before
[i
], &st
) < 0)
2426 updated_time
.actime
= st
.st_atime
;
2427 updated_time
.modtime
= now
;
2428 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2432 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2434 struct strbuf path
= STRBUF_INIT
;
2438 timestamp_t expire_time
= time(NULL
);
2440 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2441 expire_time
= ctx
->opts
->expire_time
;
2443 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2444 unlink(chain_file_name
);
2445 free(chain_file_name
);
2446 ctx
->num_commit_graphs_after
= 0;
2449 strbuf_addstr(&path
, ctx
->odb
->path
);
2450 strbuf_addstr(&path
, "/info/commit-graphs");
2451 dir
= opendir(path
.buf
);
2456 strbuf_addch(&path
, '/');
2457 dirnamelen
= path
.len
;
2458 while ((de
= readdir(dir
)) != NULL
) {
2460 uint32_t i
, found
= 0;
2462 strbuf_setlen(&path
, dirnamelen
);
2463 strbuf_addstr(&path
, de
->d_name
);
2465 if (stat(path
.buf
, &st
) < 0)
2468 if (st
.st_mtime
> expire_time
)
2470 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2473 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2474 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2488 strbuf_release(&path
);
2491 int write_commit_graph(struct object_directory
*odb
,
2492 const struct string_list
*const pack_indexes
,
2493 struct oidset
*commits
,
2494 enum commit_graph_write_flags flags
,
2495 const struct commit_graph_opts
*opts
)
2497 struct repository
*r
= the_repository
;
2498 struct write_commit_graph_context
*ctx
;
2502 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2503 struct topo_level_slab topo_levels
;
2505 prepare_repo_settings(r
);
2506 if (!r
->settings
.core_commit_graph
) {
2507 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2510 if (!commit_graph_compatible(r
))
2512 if (r
->settings
.commit_graph_changed_paths_version
< -1
2513 || r
->settings
.commit_graph_changed_paths_version
> 2) {
2514 warning(_("attempting to write a commit-graph, but "
2515 "'commitGraph.changedPathsVersion' (%d) is not supported"),
2516 r
->settings
.commit_graph_changed_paths_version
);
2520 CALLOC_ARRAY(ctx
, 1);
2523 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2524 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2525 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2527 ctx
->total_bloom_filter_data_size
= 0;
2528 ctx
->write_generation_data
= (get_configured_generation_version(r
) == 2);
2529 ctx
->num_generation_data_overflows
= 0;
2531 bloom_settings
.hash_version
= r
->settings
.commit_graph_changed_paths_version
;
2532 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2533 bloom_settings
.bits_per_entry
);
2534 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2535 bloom_settings
.num_hashes
);
2536 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2537 bloom_settings
.max_changed_paths
);
2538 ctx
->bloom_settings
= &bloom_settings
;
2540 init_topo_level_slab(&topo_levels
);
2541 ctx
->topo_levels
= &topo_levels
;
2543 prepare_commit_graph(ctx
->r
);
2544 if (ctx
->r
->objects
->commit_graph
) {
2545 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2548 g
->topo_levels
= &topo_levels
;
2553 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2554 ctx
->changed_paths
= 1;
2555 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2556 struct commit_graph
*g
;
2558 g
= ctx
->r
->objects
->commit_graph
;
2560 /* We have changed-paths already. Keep them in the next graph */
2561 if (g
&& g
->bloom_filter_settings
) {
2562 ctx
->changed_paths
= 1;
2564 /* don't propagate the hash_version unless unspecified */
2565 if (bloom_settings
.hash_version
== -1)
2566 bloom_settings
.hash_version
= g
->bloom_filter_settings
->hash_version
;
2567 bloom_settings
.bits_per_entry
= g
->bloom_filter_settings
->bits_per_entry
;
2568 bloom_settings
.num_hashes
= g
->bloom_filter_settings
->num_hashes
;
2569 bloom_settings
.max_changed_paths
= g
->bloom_filter_settings
->max_changed_paths
;
2573 bloom_settings
.hash_version
= bloom_settings
.hash_version
== 2 ? 2 : 1;
2576 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2579 ctx
->num_commit_graphs_before
++;
2583 if (ctx
->num_commit_graphs_before
) {
2584 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2585 i
= ctx
->num_commit_graphs_before
;
2586 g
= ctx
->r
->objects
->commit_graph
;
2589 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2595 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2598 ctx
->approx_nr_objects
= repo_approximate_object_count(the_repository
);
2600 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2601 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2602 for (i
= 0; i
< g
->num_commits
; i
++) {
2603 struct object_id oid
;
2604 oidread(&oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, i
),
2605 the_repository
->hash_algo
);
2606 oid_array_append(&ctx
->oids
, &oid
);
2611 ctx
->order_by_pack
= 1;
2612 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2617 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2621 if (!pack_indexes
&& !commits
) {
2622 ctx
->order_by_pack
= 1;
2623 fill_oids_from_all_packs(ctx
);
2626 close_reachable(ctx
);
2628 copy_oids_to_commits(ctx
);
2630 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2631 error(_("too many commits to write graph"));
2636 if (!ctx
->commits
.nr
&& !replace
)
2640 split_graph_merge_strategy(ctx
);
2643 merge_commit_graphs(ctx
);
2645 ctx
->num_commit_graphs_after
= 1;
2647 ctx
->trust_generation_numbers
= validate_mixed_generation_chain(ctx
->r
->objects
->commit_graph
);
2649 compute_topological_levels(ctx
);
2650 if (ctx
->write_generation_data
)
2651 compute_generation_numbers(ctx
);
2653 if (ctx
->changed_paths
)
2654 compute_bloom_filters(ctx
);
2656 res
= write_commit_graph_file(ctx
);
2658 if (ctx
->changed_paths
)
2659 deinit_bloom_filters();
2662 mark_commit_graphs(ctx
);
2664 expire_commit_graphs(ctx
);
2667 free(ctx
->graph_name
);
2668 free(ctx
->base_graph_name
);
2669 free(ctx
->commits
.list
);
2670 oid_array_clear(&ctx
->oids
);
2671 clear_topo_level_slab(&topo_levels
);
2673 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2674 free(ctx
->commit_graph_filenames_before
[i
]);
2675 free(ctx
->commit_graph_filenames_before
);
2677 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2678 free(ctx
->commit_graph_filenames_after
[i
]);
2679 free(ctx
->commit_graph_hash_after
[i
]);
2681 free(ctx
->commit_graph_filenames_after
);
2682 free(ctx
->commit_graph_hash_after
);
2689 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2690 static int verify_commit_graph_error
;
2692 __attribute__((format (printf
, 1, 2)))
2693 static void graph_report(const char *fmt
, ...)
2697 verify_commit_graph_error
= 1;
2699 vfprintf(stderr
, fmt
, ap
);
2700 fprintf(stderr
, "\n");
2704 static int commit_graph_checksum_valid(struct commit_graph
*g
)
2706 return hashfile_checksum_valid(g
->data
, g
->data_len
);
2709 static int verify_one_commit_graph(struct repository
*r
,
2710 struct commit_graph
*g
,
2711 struct progress
*progress
,
2714 uint32_t i
, cur_fanout_pos
= 0;
2715 struct object_id prev_oid
, cur_oid
;
2716 struct commit
*seen_gen_zero
= NULL
;
2717 struct commit
*seen_gen_non_zero
= NULL
;
2719 if (!commit_graph_checksum_valid(g
)) {
2720 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2721 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2724 for (i
= 0; i
< g
->num_commits
; i
++) {
2725 struct commit
*graph_commit
;
2727 oidread(&cur_oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, i
),
2728 the_repository
->hash_algo
);
2730 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2731 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2732 oid_to_hex(&prev_oid
),
2733 oid_to_hex(&cur_oid
));
2735 oidcpy(&prev_oid
, &cur_oid
);
2737 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2738 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2740 if (i
!= fanout_value
)
2741 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2742 cur_fanout_pos
, fanout_value
, i
);
2746 graph_commit
= lookup_commit(r
, &cur_oid
);
2747 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2748 graph_report(_("failed to parse commit %s from commit-graph"),
2749 oid_to_hex(&cur_oid
));
2752 while (cur_fanout_pos
< 256) {
2753 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2755 if (g
->num_commits
!= fanout_value
)
2756 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2757 cur_fanout_pos
, fanout_value
, i
);
2762 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2763 return verify_commit_graph_error
;
2765 for (i
= 0; i
< g
->num_commits
; i
++) {
2766 struct commit
*graph_commit
, *odb_commit
;
2767 struct commit_list
*graph_parents
, *odb_parents
;
2768 timestamp_t max_generation
= 0;
2769 timestamp_t generation
;
2771 display_progress(progress
, ++(*seen
));
2772 oidread(&cur_oid
, g
->chunk_oid_lookup
+ st_mult(g
->hash_len
, i
),
2773 the_repository
->hash_algo
);
2775 graph_commit
= lookup_commit(r
, &cur_oid
);
2776 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2777 if (repo_parse_commit_internal(r
, odb_commit
, 0, 0)) {
2778 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2779 oid_to_hex(&cur_oid
));
2783 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2784 get_commit_tree_oid(odb_commit
)))
2785 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2786 oid_to_hex(&cur_oid
),
2787 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2788 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2790 graph_parents
= graph_commit
->parents
;
2791 odb_parents
= odb_commit
->parents
;
2793 while (graph_parents
) {
2795 graph_report(_("commit-graph parent list for commit %s is too long"),
2796 oid_to_hex(&cur_oid
));
2800 /* parse parent in case it is in a base graph */
2801 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2803 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2804 graph_report(_("commit-graph parent for %s is %s != %s"),
2805 oid_to_hex(&cur_oid
),
2806 oid_to_hex(&graph_parents
->item
->object
.oid
),
2807 oid_to_hex(&odb_parents
->item
->object
.oid
));
2809 generation
= commit_graph_generation_from_graph(graph_parents
->item
);
2810 if (generation
> max_generation
)
2811 max_generation
= generation
;
2813 graph_parents
= graph_parents
->next
;
2814 odb_parents
= odb_parents
->next
;
2818 graph_report(_("commit-graph parent list for commit %s terminates early"),
2819 oid_to_hex(&cur_oid
));
2821 if (commit_graph_generation_from_graph(graph_commit
))
2822 seen_gen_non_zero
= graph_commit
;
2824 seen_gen_zero
= graph_commit
;
2830 * If we are using topological level and one of our parents has
2831 * generation GENERATION_NUMBER_V1_MAX, then our generation is
2832 * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic
2833 * in the following condition.
2835 if (!g
->read_generation_data
&& max_generation
== GENERATION_NUMBER_V1_MAX
)
2838 generation
= commit_graph_generation(graph_commit
);
2839 if (generation
< max_generation
+ 1)
2840 graph_report(_("commit-graph generation for commit %s is %"PRItime
" < %"PRItime
),
2841 oid_to_hex(&cur_oid
),
2843 max_generation
+ 1);
2845 if (graph_commit
->date
!= odb_commit
->date
)
2846 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2847 oid_to_hex(&cur_oid
),
2852 if (seen_gen_zero
&& seen_gen_non_zero
)
2853 graph_report(_("commit-graph has both zero and non-zero "
2854 "generations (e.g., commits '%s' and '%s')"),
2855 oid_to_hex(&seen_gen_zero
->object
.oid
),
2856 oid_to_hex(&seen_gen_non_zero
->object
.oid
));
2858 return verify_commit_graph_error
;
2861 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2863 struct progress
*progress
= NULL
;
2864 int local_error
= 0;
2868 graph_report("no commit-graph file loaded");
2872 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
) {
2873 uint64_t total
= g
->num_commits
;
2874 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
))
2875 total
+= g
->num_commits_in_base
;
2877 progress
= start_progress(_("Verifying commits in commit graph"),
2881 for (; g
; g
= g
->base_graph
) {
2882 local_error
|= verify_one_commit_graph(r
, g
, progress
, &seen
);
2883 if (flags
& COMMIT_GRAPH_VERIFY_SHALLOW
)
2887 stop_progress(&progress
);
2892 void free_commit_graph(struct commit_graph
*g
)
2895 struct commit_graph
*next
= g
->base_graph
;
2898 munmap((void *)g
->data
, g
->data_len
);
2900 free(g
->bloom_filter_settings
);
2907 void disable_commit_graph(struct repository
*r
)
2909 r
->commit_graph_disabled
= 1;