1 #include "git-compat-util.h"
10 #include "sha1-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
15 #include "replace-object.h"
18 #include "commit-slab.h"
20 #include "json-writer.h"
23 void git_test_write_commit_graph_or_die(void)
26 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0))
29 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS
, 0))
30 flags
= COMMIT_GRAPH_WRITE_BLOOM_FILTERS
;
32 if (write_commit_graph_reachable(the_repository
->objects
->odb
,
34 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
37 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
38 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
39 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
40 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
41 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
42 #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
43 #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
44 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
45 #define MAX_NUM_CHUNKS 7
47 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
49 #define GRAPH_VERSION_1 0x1
50 #define GRAPH_VERSION GRAPH_VERSION_1
52 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
53 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
54 #define GRAPH_PARENT_NONE 0x70000000
56 #define GRAPH_LAST_EDGE 0x80000000
58 #define GRAPH_HEADER_SIZE 8
59 #define GRAPH_FANOUT_SIZE (4 * 256)
60 #define GRAPH_CHUNKLOOKUP_WIDTH 12
61 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
62 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
64 /* Remember to update object flag allocation in object.h */
65 #define REACHABLE (1u<<15)
67 /* Keep track of the order in which commits are added to our list. */
68 define_commit_slab(commit_pos
, int);
69 static struct commit_pos commit_pos
= COMMIT_SLAB_INIT(1, commit_pos
);
71 static void set_commit_pos(struct repository
*r
, const struct object_id
*oid
)
73 static int32_t max_pos
;
74 struct commit
*commit
= lookup_commit(r
, oid
);
77 return; /* should never happen, but be lenient */
79 *commit_pos_at(&commit_pos
, commit
) = max_pos
++;
82 static int commit_pos_cmp(const void *va
, const void *vb
)
84 const struct commit
*a
= *(const struct commit
**)va
;
85 const struct commit
*b
= *(const struct commit
**)vb
;
86 return commit_pos_at(&commit_pos
, a
) -
87 commit_pos_at(&commit_pos
, b
);
90 define_commit_slab(commit_graph_data_slab
, struct commit_graph_data
);
91 static struct commit_graph_data_slab commit_graph_data_slab
=
92 COMMIT_SLAB_INIT(1, commit_graph_data_slab
);
94 uint32_t commit_graph_position(const struct commit
*c
)
96 struct commit_graph_data
*data
=
97 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
99 return data
? data
->graph_pos
: COMMIT_NOT_FROM_GRAPH
;
102 uint32_t commit_graph_generation(const struct commit
*c
)
104 struct commit_graph_data
*data
=
105 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
108 return GENERATION_NUMBER_INFINITY
;
109 else if (data
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
110 return GENERATION_NUMBER_INFINITY
;
112 return data
->generation
;
115 static struct commit_graph_data
*commit_graph_data_at(const struct commit
*c
)
117 unsigned int i
, nth_slab
;
118 struct commit_graph_data
*data
=
119 commit_graph_data_slab_peek(&commit_graph_data_slab
, c
);
124 nth_slab
= c
->index
/ commit_graph_data_slab
.slab_size
;
125 data
= commit_graph_data_slab_at(&commit_graph_data_slab
, c
);
128 * commit-slab initializes elements with zero, overwrite this with
129 * COMMIT_NOT_FROM_GRAPH for graph_pos.
131 * We avoid initializing generation with checking if graph position
132 * is not COMMIT_NOT_FROM_GRAPH.
134 for (i
= 0; i
< commit_graph_data_slab
.slab_size
; i
++) {
135 commit_graph_data_slab
.slab
[nth_slab
][i
].graph_pos
=
136 COMMIT_NOT_FROM_GRAPH
;
143 * Should be used only while writing commit-graph as it compares
144 * generation value of commits by directly accessing commit-slab.
146 static int commit_gen_cmp(const void *va
, const void *vb
)
148 const struct commit
*a
= *(const struct commit
**)va
;
149 const struct commit
*b
= *(const struct commit
**)vb
;
151 uint32_t generation_a
= commit_graph_data_at(a
)->generation
;
152 uint32_t generation_b
= commit_graph_data_at(b
)->generation
;
153 /* lower generation commits first */
154 if (generation_a
< generation_b
)
156 else if (generation_a
> generation_b
)
159 /* use date as a heuristic when generations are equal */
160 if (a
->date
< b
->date
)
162 else if (a
->date
> b
->date
)
167 char *get_commit_graph_filename(struct object_directory
*obj_dir
)
169 return xstrfmt("%s/info/commit-graph", obj_dir
->path
);
172 static char *get_split_graph_filename(struct object_directory
*odb
,
175 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb
->path
,
179 char *get_commit_graph_chain_filename(struct object_directory
*odb
)
181 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb
->path
);
184 static uint8_t oid_version(void)
186 switch (hash_algo_by_ptr(the_hash_algo
)) {
189 case GIT_HASH_SHA256
:
192 die(_("invalid hash version"));
196 static struct commit_graph
*alloc_commit_graph(void)
198 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
203 extern int read_replace_refs
;
205 static int commit_graph_compatible(struct repository
*r
)
210 if (read_replace_refs
) {
211 prepare_replace_object(r
);
212 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
216 prepare_commit_graft(r
);
217 if (r
->parsed_objects
&&
218 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
220 if (is_repository_shallow(r
))
226 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
228 *fd
= git_open(graph_file
);
231 if (fstat(*fd
, st
)) {
238 struct commit_graph
*load_commit_graph_one_fd_st(struct repository
*r
,
239 int fd
, struct stat
*st
,
240 struct object_directory
*odb
)
244 struct commit_graph
*ret
;
246 graph_size
= xsize_t(st
->st_size
);
248 if (graph_size
< GRAPH_MIN_SIZE
) {
250 error(_("commit-graph file is too small"));
253 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
255 ret
= parse_commit_graph(r
, graph_map
, graph_size
);
260 munmap(graph_map
, graph_size
);
265 static int verify_commit_graph_lite(struct commit_graph
*g
)
268 * Basic validation shared between parse_commit_graph()
269 * which'll be called every time the graph is used, and the
270 * much more expensive verify_commit_graph() used by
271 * "commit-graph verify".
273 * There should only be very basic checks here to ensure that
274 * we don't e.g. segfault in fill_commit_in_graph(), but
275 * because this is a very hot codepath nothing that e.g. loops
276 * over g->num_commits, or runs a checksum on the commit-graph
279 if (!g
->chunk_oid_fanout
) {
280 error("commit-graph is missing the OID Fanout chunk");
283 if (!g
->chunk_oid_lookup
) {
284 error("commit-graph is missing the OID Lookup chunk");
287 if (!g
->chunk_commit_data
) {
288 error("commit-graph is missing the Commit Data chunk");
295 struct commit_graph
*parse_commit_graph(struct repository
*r
,
296 void *graph_map
, size_t graph_size
)
298 const unsigned char *data
, *chunk_lookup
;
300 struct commit_graph
*graph
;
301 uint64_t next_chunk_offset
;
302 uint32_t graph_signature
;
303 unsigned char graph_version
, hash_version
;
308 if (graph_size
< GRAPH_MIN_SIZE
)
311 data
= (const unsigned char *)graph_map
;
313 graph_signature
= get_be32(data
);
314 if (graph_signature
!= GRAPH_SIGNATURE
) {
315 error(_("commit-graph signature %X does not match signature %X"),
316 graph_signature
, GRAPH_SIGNATURE
);
320 graph_version
= *(unsigned char*)(data
+ 4);
321 if (graph_version
!= GRAPH_VERSION
) {
322 error(_("commit-graph version %X does not match version %X"),
323 graph_version
, GRAPH_VERSION
);
327 hash_version
= *(unsigned char*)(data
+ 5);
328 if (hash_version
!= oid_version()) {
329 error(_("commit-graph hash version %X does not match version %X"),
330 hash_version
, oid_version());
334 prepare_repo_settings(r
);
336 graph
= alloc_commit_graph();
338 graph
->hash_len
= the_hash_algo
->rawsz
;
339 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
340 graph
->data
= graph_map
;
341 graph
->data_len
= graph_size
;
343 if (graph_size
< GRAPH_HEADER_SIZE
+
344 (graph
->num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
+
345 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
346 error(_("commit-graph file is too small to hold %u chunks"),
352 chunk_lookup
= data
+ 8;
353 next_chunk_offset
= get_be64(chunk_lookup
+ 4);
354 for (i
= 0; i
< graph
->num_chunks
; i
++) {
356 uint64_t chunk_offset
= next_chunk_offset
;
357 int chunk_repeated
= 0;
359 chunk_id
= get_be32(chunk_lookup
+ 0);
361 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
362 next_chunk_offset
= get_be64(chunk_lookup
+ 4);
364 if (chunk_offset
> graph_size
- the_hash_algo
->rawsz
) {
365 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset
>> 32),
366 (uint32_t)chunk_offset
);
367 goto free_and_return
;
371 case GRAPH_CHUNKID_OIDFANOUT
:
372 if (graph
->chunk_oid_fanout
)
375 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
378 case GRAPH_CHUNKID_OIDLOOKUP
:
379 if (graph
->chunk_oid_lookup
)
382 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
383 graph
->num_commits
= (next_chunk_offset
- chunk_offset
)
388 case GRAPH_CHUNKID_DATA
:
389 if (graph
->chunk_commit_data
)
392 graph
->chunk_commit_data
= data
+ chunk_offset
;
395 case GRAPH_CHUNKID_EXTRAEDGES
:
396 if (graph
->chunk_extra_edges
)
399 graph
->chunk_extra_edges
= data
+ chunk_offset
;
402 case GRAPH_CHUNKID_BASE
:
403 if (graph
->chunk_base_graphs
)
406 graph
->chunk_base_graphs
= data
+ chunk_offset
;
409 case GRAPH_CHUNKID_BLOOMINDEXES
:
410 if (graph
->chunk_bloom_indexes
)
412 else if (r
->settings
.commit_graph_read_changed_paths
)
413 graph
->chunk_bloom_indexes
= data
+ chunk_offset
;
416 case GRAPH_CHUNKID_BLOOMDATA
:
417 if (graph
->chunk_bloom_data
)
419 else if (r
->settings
.commit_graph_read_changed_paths
) {
420 uint32_t hash_version
;
421 graph
->chunk_bloom_data
= data
+ chunk_offset
;
422 hash_version
= get_be32(data
+ chunk_offset
);
424 if (hash_version
!= 1)
427 graph
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
428 graph
->bloom_filter_settings
->hash_version
= hash_version
;
429 graph
->bloom_filter_settings
->num_hashes
= get_be32(data
+ chunk_offset
+ 4);
430 graph
->bloom_filter_settings
->bits_per_entry
= get_be32(data
+ chunk_offset
+ 8);
431 graph
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
436 if (chunk_repeated
) {
437 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id
);
438 goto free_and_return
;
442 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
443 init_bloom_filters();
445 /* We need both the bloom chunks to exist together. Else ignore the data */
446 graph
->chunk_bloom_indexes
= NULL
;
447 graph
->chunk_bloom_data
= NULL
;
448 FREE_AND_NULL(graph
->bloom_filter_settings
);
451 hashcpy(graph
->oid
.hash
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
453 if (verify_commit_graph_lite(graph
))
454 goto free_and_return
;
459 free(graph
->bloom_filter_settings
);
464 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
465 const char *graph_file
,
466 struct object_directory
*odb
)
471 struct commit_graph
*g
;
472 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
477 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
480 g
->filename
= xstrdup(graph_file
);
485 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
486 struct object_directory
*odb
)
488 char *graph_name
= get_commit_graph_filename(odb
);
489 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
495 static int add_graph_to_chain(struct commit_graph
*g
,
496 struct commit_graph
*chain
,
497 struct object_id
*oids
,
500 struct commit_graph
*cur_g
= chain
;
502 if (n
&& !g
->chunk_base_graphs
) {
503 warning(_("commit-graph has no base graphs chunk"));
511 !oideq(&oids
[n
], &cur_g
->oid
) ||
512 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
513 warning(_("commit-graph chain does not match"));
517 cur_g
= cur_g
->base_graph
;
520 g
->base_graph
= chain
;
523 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
528 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
529 struct object_directory
*odb
)
531 struct commit_graph
*graph_chain
= NULL
;
532 struct strbuf line
= STRBUF_INIT
;
534 struct object_id
*oids
;
535 int i
= 0, valid
= 1, count
;
536 char *chain_name
= get_commit_graph_chain_filename(odb
);
540 fp
= fopen(chain_name
, "r");
541 stat_res
= stat(chain_name
, &st
);
546 st
.st_size
<= the_hash_algo
->hexsz
)
549 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
550 oids
= xcalloc(count
, sizeof(struct object_id
));
554 for (i
= 0; i
< count
; i
++) {
555 struct object_directory
*odb
;
557 if (strbuf_getline_lf(&line
, fp
) == EOF
)
560 if (get_oid_hex(line
.buf
, &oids
[i
])) {
561 warning(_("invalid commit-graph chain: line '%s' not a hash"),
568 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
569 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
570 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
575 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
585 warning(_("unable to find all commit-graph files"));
592 strbuf_release(&line
);
597 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
598 struct object_directory
*odb
)
600 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
603 g
= load_commit_graph_chain(r
, odb
);
608 static void prepare_commit_graph_one(struct repository
*r
,
609 struct object_directory
*odb
)
612 if (r
->objects
->commit_graph
)
615 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
619 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
621 * On the first invocation, this function attempts to load the commit
622 * graph if the_repository is configured to have one.
624 static int prepare_commit_graph(struct repository
*r
)
626 struct object_directory
*odb
;
629 * This must come before the "already attempted?" check below, because
630 * we want to disable even an already-loaded graph file.
632 if (r
->commit_graph_disabled
)
635 if (r
->objects
->commit_graph_attempted
)
636 return !!r
->objects
->commit_graph
;
637 r
->objects
->commit_graph_attempted
= 1;
639 prepare_repo_settings(r
);
641 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
642 r
->settings
.core_commit_graph
!= 1)
644 * This repository is not configured to use commit graphs, so
645 * do not load one. (But report commit_graph_attempted anyway
646 * so that commit graph loading is not attempted again for this
651 if (!commit_graph_compatible(r
))
655 for (odb
= r
->objects
->odb
;
656 !r
->objects
->commit_graph
&& odb
;
658 prepare_commit_graph_one(r
, odb
);
659 return !!r
->objects
->commit_graph
;
662 int generation_numbers_enabled(struct repository
*r
)
664 uint32_t first_generation
;
665 struct commit_graph
*g
;
666 if (!prepare_commit_graph(r
))
669 g
= r
->objects
->commit_graph
;
674 first_generation
= get_be32(g
->chunk_commit_data
+
675 g
->hash_len
+ 8) >> 2;
677 return !!first_generation
;
680 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
682 struct commit_graph
*g
= r
->objects
->commit_graph
;
684 if (g
->bloom_filter_settings
)
685 return g
->bloom_filter_settings
;
691 static void close_commit_graph_one(struct commit_graph
*g
)
696 close_commit_graph_one(g
->base_graph
);
697 free_commit_graph(g
);
700 void close_commit_graph(struct raw_object_store
*o
)
702 close_commit_graph_one(o
->commit_graph
);
703 o
->commit_graph
= NULL
;
706 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
708 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
709 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
712 static void load_oid_from_graph(struct commit_graph
*g
,
714 struct object_id
*oid
)
718 while (g
&& pos
< g
->num_commits_in_base
)
722 BUG("NULL commit-graph");
724 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
725 die(_("invalid commit position. commit-graph is likely corrupt"));
727 lex_index
= pos
- g
->num_commits_in_base
;
729 hashcpy(oid
->hash
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
732 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
733 struct commit_graph
*g
,
735 struct commit_list
**pptr
)
738 struct object_id oid
;
740 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
741 die("invalid parent position %"PRIu32
, pos
);
743 load_oid_from_graph(g
, pos
, &oid
);
744 c
= lookup_commit(r
, &oid
);
746 die(_("could not find commit %s"), oid_to_hex(&oid
));
747 commit_graph_data_at(c
)->graph_pos
= pos
;
748 return &commit_list_insert(c
, pptr
)->next
;
751 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
753 const unsigned char *commit_data
;
754 struct commit_graph_data
*graph_data
;
757 while (pos
< g
->num_commits_in_base
)
760 lex_index
= pos
- g
->num_commits_in_base
;
761 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
763 graph_data
= commit_graph_data_at(item
);
764 graph_data
->graph_pos
= pos
;
765 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
768 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
773 static int fill_commit_in_graph(struct repository
*r
,
775 struct commit_graph
*g
, uint32_t pos
)
778 uint32_t *parent_data_ptr
;
779 uint64_t date_low
, date_high
;
780 struct commit_list
**pptr
;
781 struct commit_graph_data
*graph_data
;
782 const unsigned char *commit_data
;
785 while (pos
< g
->num_commits_in_base
)
788 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
789 die(_("invalid commit position. commit-graph is likely corrupt"));
792 * Store the "full" position, but then use the
793 * "local" position for the rest of the calculation.
795 graph_data
= commit_graph_data_at(item
);
796 graph_data
->graph_pos
= pos
;
797 lex_index
= pos
- g
->num_commits_in_base
;
799 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
801 item
->object
.parsed
= 1;
803 set_commit_tree(item
, NULL
);
805 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
806 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
807 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
809 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
811 pptr
= &item
->parents
;
813 edge_value
= get_be32(commit_data
+ g
->hash_len
);
814 if (edge_value
== GRAPH_PARENT_NONE
)
816 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
818 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
819 if (edge_value
== GRAPH_PARENT_NONE
)
821 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
822 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
826 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
827 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
829 edge_value
= get_be32(parent_data_ptr
);
830 pptr
= insert_parent_or_die(r
, g
,
831 edge_value
& GRAPH_EDGE_LAST_MASK
,
834 } while (!(edge_value
& GRAPH_LAST_EDGE
));
839 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
841 uint32_t graph_pos
= commit_graph_position(item
);
842 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
846 struct commit_graph
*cur_g
= g
;
849 while (cur_g
&& !bsearch_graph(cur_g
, &(item
->object
.oid
), &lex_index
))
850 cur_g
= cur_g
->base_graph
;
853 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
861 static int parse_commit_in_graph_one(struct repository
*r
,
862 struct commit_graph
*g
,
867 if (item
->object
.parsed
)
870 if (find_commit_in_graph(item
, g
, &pos
))
871 return fill_commit_in_graph(r
, item
, g
, pos
);
876 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
878 static int checked_env
= 0;
881 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
882 die("dying as requested by the '%s' variable on commit-graph parse!",
883 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
886 if (!prepare_commit_graph(r
))
888 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
891 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
894 if (!prepare_commit_graph(r
))
896 if (find_commit_in_graph(item
, r
->objects
->commit_graph
, &pos
))
897 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
900 static struct tree
*load_tree_for_commit(struct repository
*r
,
901 struct commit_graph
*g
,
904 struct object_id oid
;
905 const unsigned char *commit_data
;
906 uint32_t graph_pos
= commit_graph_position(c
);
908 while (graph_pos
< g
->num_commits_in_base
)
911 commit_data
= g
->chunk_commit_data
+
912 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
914 hashcpy(oid
.hash
, commit_data
);
915 set_commit_tree(c
, lookup_tree(r
, &oid
));
917 return c
->maybe_tree
;
920 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
921 struct commit_graph
*g
,
922 const struct commit
*c
)
925 return c
->maybe_tree
;
926 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
927 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
929 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
932 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
934 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
937 struct packed_commit_list
{
938 struct commit
**list
;
943 struct write_commit_graph_context
{
944 struct repository
*r
;
945 struct object_directory
*odb
;
947 struct oid_array oids
;
948 struct packed_commit_list commits
;
950 unsigned long approx_nr_objects
;
951 struct progress
*progress
;
953 uint64_t progress_cnt
;
955 char *base_graph_name
;
956 int num_commit_graphs_before
;
957 int num_commit_graphs_after
;
958 char **commit_graph_filenames_before
;
959 char **commit_graph_filenames_after
;
960 char **commit_graph_hash_after
;
961 uint32_t new_num_commits_in_base
;
962 struct commit_graph
*new_base_graph
;
970 const struct commit_graph_opts
*opts
;
971 size_t total_bloom_filter_data_size
;
972 const struct bloom_filter_settings
*bloom_settings
;
974 int count_bloom_filter_computed
;
975 int count_bloom_filter_not_computed
;
976 int count_bloom_filter_trunc_empty
;
977 int count_bloom_filter_trunc_large
;
980 static int write_graph_chunk_fanout(struct hashfile
*f
,
981 struct write_commit_graph_context
*ctx
)
984 struct commit
**list
= ctx
->commits
.list
;
987 * Write the first-level table (the list is sorted,
988 * but we use a 256-entry lookup to be able to avoid
989 * having to do eight extra binary search iterations).
991 for (i
= 0; i
< 256; i
++) {
992 while (count
< ctx
->commits
.nr
) {
993 if ((*list
)->object
.oid
.hash
[0] != i
)
995 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1000 hashwrite_be32(f
, count
);
1006 static int write_graph_chunk_oids(struct hashfile
*f
,
1007 struct write_commit_graph_context
*ctx
)
1009 struct commit
**list
= ctx
->commits
.list
;
1011 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1012 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1013 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1019 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
1021 struct commit
**commits
= table
;
1022 return commits
[index
]->object
.oid
.hash
;
1025 static int write_graph_chunk_data(struct hashfile
*f
,
1026 struct write_commit_graph_context
*ctx
)
1028 struct commit
**list
= ctx
->commits
.list
;
1029 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1030 uint32_t num_extra_edges
= 0;
1032 while (list
< last
) {
1033 struct commit_list
*parent
;
1034 struct object_id
*tree
;
1036 uint32_t packedDate
[2];
1037 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1039 if (parse_commit_no_graph(*list
))
1040 die(_("unable to parse commit %s"),
1041 oid_to_hex(&(*list
)->object
.oid
));
1042 tree
= get_commit_tree_oid(*list
);
1043 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1045 parent
= (*list
)->parents
;
1048 edge_value
= GRAPH_PARENT_NONE
;
1050 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
1055 if (edge_value
>= 0)
1056 edge_value
+= ctx
->new_num_commits_in_base
;
1057 else if (ctx
->new_base_graph
) {
1059 if (find_commit_in_graph(parent
->item
,
1060 ctx
->new_base_graph
,
1066 BUG("missing parent %s for commit %s",
1067 oid_to_hex(&parent
->item
->object
.oid
),
1068 oid_to_hex(&(*list
)->object
.oid
));
1071 hashwrite_be32(f
, edge_value
);
1074 parent
= parent
->next
;
1077 edge_value
= GRAPH_PARENT_NONE
;
1078 else if (parent
->next
)
1079 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1081 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
1086 if (edge_value
>= 0)
1087 edge_value
+= ctx
->new_num_commits_in_base
;
1088 else if (ctx
->new_base_graph
) {
1090 if (find_commit_in_graph(parent
->item
,
1091 ctx
->new_base_graph
,
1097 BUG("missing parent %s for commit %s",
1098 oid_to_hex(&parent
->item
->object
.oid
),
1099 oid_to_hex(&(*list
)->object
.oid
));
1102 hashwrite_be32(f
, edge_value
);
1104 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1107 parent
= parent
->next
;
1111 if (sizeof((*list
)->date
) > 4)
1112 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1116 packedDate
[0] |= htonl(commit_graph_data_at(*list
)->generation
<< 2);
1118 packedDate
[1] = htonl((*list
)->date
);
1119 hashwrite(f
, packedDate
, 8);
1127 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1128 struct write_commit_graph_context
*ctx
)
1130 struct commit
**list
= ctx
->commits
.list
;
1131 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1132 struct commit_list
*parent
;
1134 while (list
< last
) {
1135 int num_parents
= 0;
1137 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1139 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1140 parent
= parent
->next
)
1143 if (num_parents
<= 2) {
1148 /* Since num_parents > 2, this initializer is safe. */
1149 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1150 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
1155 if (edge_value
>= 0)
1156 edge_value
+= ctx
->new_num_commits_in_base
;
1157 else if (ctx
->new_base_graph
) {
1159 if (find_commit_in_graph(parent
->item
,
1160 ctx
->new_base_graph
,
1166 BUG("missing parent %s for commit %s",
1167 oid_to_hex(&parent
->item
->object
.oid
),
1168 oid_to_hex(&(*list
)->object
.oid
));
1169 else if (!parent
->next
)
1170 edge_value
|= GRAPH_LAST_EDGE
;
1172 hashwrite_be32(f
, edge_value
);
1181 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1182 struct write_commit_graph_context
*ctx
)
1184 struct commit
**list
= ctx
->commits
.list
;
1185 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1186 uint32_t cur_pos
= 0;
1188 while (list
< last
) {
1189 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1190 size_t len
= filter
? filter
->len
: 0;
1192 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1193 hashwrite_be32(f
, cur_pos
);
1200 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1202 struct json_writer jw
= JSON_WRITER_INIT
;
1204 jw_object_begin(&jw
, 0);
1205 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1206 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1207 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1208 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1211 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1216 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1217 struct write_commit_graph_context
*ctx
)
1219 struct commit
**list
= ctx
->commits
.list
;
1220 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1222 trace2_bloom_filter_settings(ctx
);
1224 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1225 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1226 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1228 while (list
< last
) {
1229 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1230 size_t len
= filter
? filter
->len
: 0;
1232 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1234 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1241 static int add_packed_commits(const struct object_id
*oid
,
1242 struct packed_git
*pack
,
1246 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1247 enum object_type type
;
1248 off_t offset
= nth_packed_object_offset(pack
, pos
);
1249 struct object_info oi
= OBJECT_INFO_INIT
;
1252 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1255 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1256 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1258 if (type
!= OBJ_COMMIT
)
1261 oid_array_append(&ctx
->oids
, oid
);
1262 set_commit_pos(ctx
->r
, oid
);
1267 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1269 struct commit_list
*parent
;
1270 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1271 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1272 oid_array_append(&ctx
->oids
, &parent
->item
->object
.oid
);
1273 parent
->item
->object
.flags
|= REACHABLE
;
1278 static void close_reachable(struct write_commit_graph_context
*ctx
)
1281 struct commit
*commit
;
1282 enum commit_graph_split_flags flags
= ctx
->opts
?
1283 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1285 if (ctx
->report_progress
)
1286 ctx
->progress
= start_delayed_progress(
1287 _("Loading known commits in commit graph"),
1289 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1290 display_progress(ctx
->progress
, i
+ 1);
1291 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1293 commit
->object
.flags
|= REACHABLE
;
1295 stop_progress(&ctx
->progress
);
1298 * As this loop runs, ctx->oids.nr may grow, but not more
1299 * than the number of missing commits in the reachable
1302 if (ctx
->report_progress
)
1303 ctx
->progress
= start_delayed_progress(
1304 _("Expanding reachable commits in commit graph"),
1306 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1307 display_progress(ctx
->progress
, i
+ 1);
1308 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1313 if ((!parse_commit(commit
) &&
1314 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1315 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1316 add_missing_parents(ctx
, commit
);
1317 } else if (!parse_commit_no_graph(commit
))
1318 add_missing_parents(ctx
, commit
);
1320 stop_progress(&ctx
->progress
);
1322 if (ctx
->report_progress
)
1323 ctx
->progress
= start_delayed_progress(
1324 _("Clearing commit marks in commit graph"),
1326 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1327 display_progress(ctx
->progress
, i
+ 1);
1328 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1331 commit
->object
.flags
&= ~REACHABLE
;
1333 stop_progress(&ctx
->progress
);
1336 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1339 struct commit_list
*list
= NULL
;
1341 if (ctx
->report_progress
)
1342 ctx
->progress
= start_delayed_progress(
1343 _("Computing commit graph generation numbers"),
1345 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1346 uint32_t generation
= commit_graph_data_at(ctx
->commits
.list
[i
])->generation
;
1348 display_progress(ctx
->progress
, i
+ 1);
1349 if (generation
!= GENERATION_NUMBER_INFINITY
&&
1350 generation
!= GENERATION_NUMBER_ZERO
)
1353 commit_list_insert(ctx
->commits
.list
[i
], &list
);
1355 struct commit
*current
= list
->item
;
1356 struct commit_list
*parent
;
1357 int all_parents_computed
= 1;
1358 uint32_t max_generation
= 0;
1360 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1361 generation
= commit_graph_data_at(parent
->item
)->generation
;
1363 if (generation
== GENERATION_NUMBER_INFINITY
||
1364 generation
== GENERATION_NUMBER_ZERO
) {
1365 all_parents_computed
= 0;
1366 commit_list_insert(parent
->item
, &list
);
1368 } else if (generation
> max_generation
) {
1369 max_generation
= generation
;
1373 if (all_parents_computed
) {
1374 struct commit_graph_data
*data
= commit_graph_data_at(current
);
1376 data
->generation
= max_generation
+ 1;
1379 if (data
->generation
> GENERATION_NUMBER_MAX
)
1380 data
->generation
= GENERATION_NUMBER_MAX
;
1384 stop_progress(&ctx
->progress
);
1387 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1389 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1390 ctx
->count_bloom_filter_computed
);
1391 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1392 ctx
->count_bloom_filter_not_computed
);
1393 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1394 ctx
->count_bloom_filter_trunc_empty
);
1395 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1396 ctx
->count_bloom_filter_trunc_large
);
1399 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1402 struct progress
*progress
= NULL
;
1403 struct commit
**sorted_commits
;
1404 int max_new_filters
;
1406 init_bloom_filters();
1408 if (ctx
->report_progress
)
1409 progress
= start_delayed_progress(
1410 _("Computing commit changed paths Bloom filters"),
1413 ALLOC_ARRAY(sorted_commits
, ctx
->commits
.nr
);
1414 COPY_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1416 if (ctx
->order_by_pack
)
1417 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1419 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1421 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1422 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1424 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1425 enum bloom_filter_computed computed
= 0;
1426 struct commit
*c
= sorted_commits
[i
];
1427 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1430 ctx
->count_bloom_filter_computed
< max_new_filters
,
1431 ctx
->bloom_settings
,
1433 if (computed
& BLOOM_COMPUTED
) {
1434 ctx
->count_bloom_filter_computed
++;
1435 if (computed
& BLOOM_TRUNC_EMPTY
)
1436 ctx
->count_bloom_filter_trunc_empty
++;
1437 if (computed
& BLOOM_TRUNC_LARGE
)
1438 ctx
->count_bloom_filter_trunc_large
++;
1439 } else if (computed
& BLOOM_NOT_COMPUTED
)
1440 ctx
->count_bloom_filter_not_computed
++;
1441 ctx
->total_bloom_filter_data_size
+= filter
1442 ? sizeof(unsigned char) * filter
->len
: 0;
1443 display_progress(progress
, i
+ 1);
1446 if (trace2_is_enabled())
1447 trace2_bloom_filter_write_statistics(ctx
);
1449 free(sorted_commits
);
1450 stop_progress(&progress
);
1453 struct refs_cb_data
{
1454 struct oidset
*commits
;
1455 struct progress
*progress
;
1458 static int add_ref_to_set(const char *refname
,
1459 const struct object_id
*oid
,
1460 int flags
, void *cb_data
)
1462 struct object_id peeled
;
1463 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1465 if (!peel_ref(refname
, &peeled
))
1467 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1468 oidset_insert(data
->commits
, oid
);
1470 display_progress(data
->progress
, oidset_size(data
->commits
));
1475 int write_commit_graph_reachable(struct object_directory
*odb
,
1476 enum commit_graph_write_flags flags
,
1477 const struct commit_graph_opts
*opts
)
1479 struct oidset commits
= OIDSET_INIT
;
1480 struct refs_cb_data data
;
1483 memset(&data
, 0, sizeof(data
));
1484 data
.commits
= &commits
;
1485 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1486 data
.progress
= start_delayed_progress(
1487 _("Collecting referenced commits"), 0);
1489 for_each_ref(add_ref_to_set
, &data
);
1491 stop_progress(&data
.progress
);
1493 result
= write_commit_graph(odb
, NULL
, &commits
,
1496 oidset_clear(&commits
);
1500 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1501 struct string_list
*pack_indexes
)
1504 struct strbuf progress_title
= STRBUF_INIT
;
1505 struct strbuf packname
= STRBUF_INIT
;
1508 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1509 dirlen
= packname
.len
;
1510 if (ctx
->report_progress
) {
1511 strbuf_addf(&progress_title
,
1512 Q_("Finding commits for commit graph in %d pack",
1513 "Finding commits for commit graph in %d packs",
1516 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1517 ctx
->progress_done
= 0;
1519 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1520 struct packed_git
*p
;
1521 strbuf_setlen(&packname
, dirlen
);
1522 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1523 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1525 error(_("error adding pack %s"), packname
.buf
);
1528 if (open_pack_index(p
)) {
1529 error(_("error opening index for %s"), packname
.buf
);
1532 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1533 FOR_EACH_OBJECT_PACK_ORDER
);
1538 stop_progress(&ctx
->progress
);
1539 strbuf_release(&progress_title
);
1540 strbuf_release(&packname
);
1545 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1546 struct oidset
*commits
)
1548 struct oidset_iter iter
;
1549 struct object_id
*oid
;
1551 if (!oidset_size(commits
))
1554 oidset_iter_init(commits
, &iter
);
1555 while ((oid
= oidset_iter_next(&iter
))) {
1556 oid_array_append(&ctx
->oids
, oid
);
1562 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1564 if (ctx
->report_progress
)
1565 ctx
->progress
= start_delayed_progress(
1566 _("Finding commits for commit graph among packed objects"),
1567 ctx
->approx_nr_objects
);
1568 for_each_packed_object(add_packed_commits
, ctx
,
1569 FOR_EACH_OBJECT_PACK_ORDER
);
1570 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1571 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1572 stop_progress(&ctx
->progress
);
1575 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1578 enum commit_graph_split_flags flags
= ctx
->opts
?
1579 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1581 ctx
->num_extra_edges
= 0;
1582 if (ctx
->report_progress
)
1583 ctx
->progress
= start_delayed_progress(
1584 _("Finding extra edges in commit graph"),
1586 oid_array_sort(&ctx
->oids
);
1587 for (i
= 0; i
< ctx
->oids
.nr
; i
= oid_array_next_unique(&ctx
->oids
, i
)) {
1588 unsigned int num_parents
;
1590 display_progress(ctx
->progress
, i
+ 1);
1592 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1593 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.oid
[i
]);
1595 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1596 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1599 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1600 parse_commit(ctx
->commits
.list
[ctx
->commits
.nr
]);
1602 parse_commit_no_graph(ctx
->commits
.list
[ctx
->commits
.nr
]);
1604 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1605 if (num_parents
> 2)
1606 ctx
->num_extra_edges
+= num_parents
- 1;
1610 stop_progress(&ctx
->progress
);
1613 static int write_graph_chunk_base_1(struct hashfile
*f
,
1614 struct commit_graph
*g
)
1621 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1622 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1626 static int write_graph_chunk_base(struct hashfile
*f
,
1627 struct write_commit_graph_context
*ctx
)
1629 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1631 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1632 error(_("failed to write correct number of base graph ids"));
1639 typedef int (*chunk_write_fn
)(struct hashfile
*f
,
1640 struct write_commit_graph_context
*ctx
);
1645 chunk_write_fn write_fn
;
1648 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1653 struct lock_file lk
= LOCK_INIT
;
1654 struct chunk_info chunks
[MAX_NUM_CHUNKS
+ 1];
1655 const unsigned hashsz
= the_hash_algo
->rawsz
;
1656 struct strbuf progress_title
= STRBUF_INIT
;
1658 uint64_t chunk_offset
;
1659 struct object_id file_hash
;
1662 struct strbuf tmp_file
= STRBUF_INIT
;
1664 strbuf_addf(&tmp_file
,
1665 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1667 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1669 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1672 if (safe_create_leading_directories(ctx
->graph_name
)) {
1673 UNLEAK(ctx
->graph_name
);
1674 error(_("unable to create leading directories of %s"),
1680 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1682 hold_lock_file_for_update_mode(&lk
, lock_name
,
1683 LOCK_DIE_ON_ERROR
, 0444);
1685 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1687 error(_("unable to create temporary graph layer"));
1691 if (adjust_shared_perm(ctx
->graph_name
)) {
1692 error(_("unable to adjust shared permissions for '%s'"),
1697 f
= hashfd(fd
, ctx
->graph_name
);
1699 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1700 LOCK_DIE_ON_ERROR
, 0444);
1701 fd
= lk
.tempfile
->fd
;
1702 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
1705 chunks
[0].id
= GRAPH_CHUNKID_OIDFANOUT
;
1706 chunks
[0].size
= GRAPH_FANOUT_SIZE
;
1707 chunks
[0].write_fn
= write_graph_chunk_fanout
;
1708 chunks
[1].id
= GRAPH_CHUNKID_OIDLOOKUP
;
1709 chunks
[1].size
= hashsz
* ctx
->commits
.nr
;
1710 chunks
[1].write_fn
= write_graph_chunk_oids
;
1711 chunks
[2].id
= GRAPH_CHUNKID_DATA
;
1712 chunks
[2].size
= (hashsz
+ 16) * ctx
->commits
.nr
;
1713 chunks
[2].write_fn
= write_graph_chunk_data
;
1714 if (ctx
->num_extra_edges
) {
1715 chunks
[num_chunks
].id
= GRAPH_CHUNKID_EXTRAEDGES
;
1716 chunks
[num_chunks
].size
= 4 * ctx
->num_extra_edges
;
1717 chunks
[num_chunks
].write_fn
= write_graph_chunk_extra_edges
;
1720 if (ctx
->changed_paths
) {
1721 chunks
[num_chunks
].id
= GRAPH_CHUNKID_BLOOMINDEXES
;
1722 chunks
[num_chunks
].size
= sizeof(uint32_t) * ctx
->commits
.nr
;
1723 chunks
[num_chunks
].write_fn
= write_graph_chunk_bloom_indexes
;
1725 chunks
[num_chunks
].id
= GRAPH_CHUNKID_BLOOMDATA
;
1726 chunks
[num_chunks
].size
= sizeof(uint32_t) * 3
1727 + ctx
->total_bloom_filter_data_size
;
1728 chunks
[num_chunks
].write_fn
= write_graph_chunk_bloom_data
;
1731 if (ctx
->num_commit_graphs_after
> 1) {
1732 chunks
[num_chunks
].id
= GRAPH_CHUNKID_BASE
;
1733 chunks
[num_chunks
].size
= hashsz
* (ctx
->num_commit_graphs_after
- 1);
1734 chunks
[num_chunks
].write_fn
= write_graph_chunk_base
;
1738 chunks
[num_chunks
].id
= 0;
1739 chunks
[num_chunks
].size
= 0;
1741 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1743 hashwrite_u8(f
, GRAPH_VERSION
);
1744 hashwrite_u8(f
, oid_version());
1745 hashwrite_u8(f
, num_chunks
);
1746 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1748 chunk_offset
= 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
1749 for (i
= 0; i
<= num_chunks
; i
++) {
1750 uint32_t chunk_write
[3];
1752 chunk_write
[0] = htonl(chunks
[i
].id
);
1753 chunk_write
[1] = htonl(chunk_offset
>> 32);
1754 chunk_write
[2] = htonl(chunk_offset
& 0xffffffff);
1755 hashwrite(f
, chunk_write
, 12);
1757 chunk_offset
+= chunks
[i
].size
;
1760 if (ctx
->report_progress
) {
1761 strbuf_addf(&progress_title
,
1762 Q_("Writing out commit graph in %d pass",
1763 "Writing out commit graph in %d passes",
1766 ctx
->progress
= start_delayed_progress(
1768 num_chunks
* ctx
->commits
.nr
);
1771 for (i
= 0; i
< num_chunks
; i
++) {
1772 uint64_t start_offset
= f
->total
+ f
->offset
;
1774 if (chunks
[i
].write_fn(f
, ctx
))
1777 if (f
->total
+ f
->offset
!= start_offset
+ chunks
[i
].size
)
1778 BUG("expected to write %"PRId64
" bytes to chunk %"PRIx32
", but wrote %"PRId64
" instead",
1779 chunks
[i
].size
, chunks
[i
].id
,
1780 f
->total
+ f
->offset
- start_offset
);
1783 stop_progress(&ctx
->progress
);
1784 strbuf_release(&progress_title
);
1786 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1787 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1788 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
1790 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1791 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1792 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1793 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1796 close_commit_graph(ctx
->r
->objects
);
1797 finalize_hashfile(f
, file_hash
.hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1800 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1801 char *final_graph_name
;
1807 error(_("unable to open commit-graph chain file"));
1811 if (ctx
->base_graph_name
) {
1813 int idx
= ctx
->num_commit_graphs_after
- 1;
1814 if (ctx
->num_commit_graphs_after
> 1)
1817 dest
= ctx
->commit_graph_filenames_after
[idx
];
1819 if (strcmp(ctx
->base_graph_name
, dest
)) {
1820 result
= rename(ctx
->base_graph_name
, dest
);
1823 error(_("failed to rename base commit-graph file"));
1828 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
1832 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(oid_to_hex(&file_hash
));
1833 final_graph_name
= get_split_graph_filename(ctx
->odb
,
1834 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1835 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
1837 result
= rename(ctx
->graph_name
, final_graph_name
);
1839 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
1840 fprintf(lk
.tempfile
->fp
, "%s\n", ctx
->commit_graph_hash_after
[i
]);
1843 error(_("failed to rename temporary commit-graph file"));
1848 commit_lock_file(&lk
);
1853 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
1855 struct commit_graph
*g
;
1856 uint32_t num_commits
;
1857 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1860 int max_commits
= 0;
1864 max_commits
= ctx
->opts
->max_commits
;
1866 if (ctx
->opts
->size_multiple
)
1867 size_mult
= ctx
->opts
->size_multiple
;
1869 flags
= ctx
->opts
->split_flags
;
1872 g
= ctx
->r
->objects
->commit_graph
;
1873 num_commits
= ctx
->commits
.nr
;
1874 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1875 ctx
->num_commit_graphs_after
= 1;
1877 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
1879 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
1880 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
1881 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
1882 (max_commits
&& num_commits
> max_commits
))) {
1883 if (g
->odb
!= ctx
->odb
)
1886 num_commits
+= g
->num_commits
;
1889 ctx
->num_commit_graphs_after
--;
1893 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
1894 ctx
->new_base_graph
= g
;
1895 else if (ctx
->num_commit_graphs_after
!= 1)
1896 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1897 "should be 1 with --split=replace");
1899 if (ctx
->num_commit_graphs_after
== 2) {
1900 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
1902 if (!strcmp(g
->filename
, old_graph_name
) &&
1903 g
->odb
!= ctx
->odb
) {
1904 ctx
->num_commit_graphs_after
= 1;
1905 ctx
->new_base_graph
= NULL
;
1908 free(old_graph_name
);
1911 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
1912 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
1914 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
1915 i
< ctx
->num_commit_graphs_before
; i
++)
1916 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
1918 i
= ctx
->num_commit_graphs_before
- 1;
1919 g
= ctx
->r
->objects
->commit_graph
;
1922 if (i
< ctx
->num_commit_graphs_after
)
1923 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
1930 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
1931 struct commit_graph
*g
)
1934 uint32_t offset
= g
->num_commits_in_base
;
1936 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
1938 for (i
= 0; i
< g
->num_commits
; i
++) {
1939 struct object_id oid
;
1940 struct commit
*result
;
1942 display_progress(ctx
->progress
, i
+ 1);
1944 load_oid_from_graph(g
, i
+ offset
, &oid
);
1946 /* only add commits if they still exist in the repo */
1947 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
1950 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
1956 static int commit_compare(const void *_a
, const void *_b
)
1958 const struct commit
*a
= *(const struct commit
**)_a
;
1959 const struct commit
*b
= *(const struct commit
**)_b
;
1960 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
1963 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
1965 uint32_t i
, dedup_i
= 0;
1967 if (ctx
->report_progress
)
1968 ctx
->progress
= start_delayed_progress(
1969 _("Scanning merged commits"),
1972 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
1974 ctx
->num_extra_edges
= 0;
1975 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1976 display_progress(ctx
->progress
, i
);
1978 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
1979 &ctx
->commits
.list
[i
]->object
.oid
)) {
1981 * Silently ignore duplicates. These were likely
1982 * created due to a commit appearing in multiple
1983 * layers of the chain, which is unexpected but
1984 * not invalid. We should make sure there is a
1985 * unique copy in the new layer.
1988 unsigned int num_parents
;
1990 ctx
->commits
.list
[dedup_i
] = ctx
->commits
.list
[i
];
1993 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
1994 if (num_parents
> 2)
1995 ctx
->num_extra_edges
+= num_parents
- 1;
1999 ctx
->commits
.nr
= dedup_i
;
2001 stop_progress(&ctx
->progress
);
2004 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2006 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2007 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2009 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2010 current_graph_number
--;
2012 if (ctx
->report_progress
)
2013 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2015 merge_commit_graph(ctx
, g
);
2016 stop_progress(&ctx
->progress
);
2022 ctx
->new_base_graph
= g
;
2023 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2026 if (ctx
->new_base_graph
)
2027 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2029 sort_and_scan_merged_commits(ctx
);
2032 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2035 time_t now
= time(NULL
);
2037 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2039 struct utimbuf updated_time
;
2041 stat(ctx
->commit_graph_filenames_before
[i
], &st
);
2043 updated_time
.actime
= st
.st_atime
;
2044 updated_time
.modtime
= now
;
2045 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2049 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2051 struct strbuf path
= STRBUF_INIT
;
2055 timestamp_t expire_time
= time(NULL
);
2057 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2058 expire_time
= ctx
->opts
->expire_time
;
2060 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2061 unlink(chain_file_name
);
2062 free(chain_file_name
);
2063 ctx
->num_commit_graphs_after
= 0;
2066 strbuf_addstr(&path
, ctx
->odb
->path
);
2067 strbuf_addstr(&path
, "/info/commit-graphs");
2068 dir
= opendir(path
.buf
);
2073 strbuf_addch(&path
, '/');
2074 dirnamelen
= path
.len
;
2075 while ((de
= readdir(dir
)) != NULL
) {
2077 uint32_t i
, found
= 0;
2079 strbuf_setlen(&path
, dirnamelen
);
2080 strbuf_addstr(&path
, de
->d_name
);
2082 stat(path
.buf
, &st
);
2084 if (st
.st_mtime
> expire_time
)
2086 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2089 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2090 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2102 strbuf_release(&path
);
2105 int write_commit_graph(struct object_directory
*odb
,
2106 struct string_list
*pack_indexes
,
2107 struct oidset
*commits
,
2108 enum commit_graph_write_flags flags
,
2109 const struct commit_graph_opts
*opts
)
2111 struct write_commit_graph_context
*ctx
;
2115 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2117 prepare_repo_settings(the_repository
);
2118 if (!the_repository
->settings
.core_commit_graph
) {
2119 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2122 if (!commit_graph_compatible(the_repository
))
2125 ctx
= xcalloc(1, sizeof(struct write_commit_graph_context
));
2126 ctx
->r
= the_repository
;
2128 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2129 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2130 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2132 ctx
->total_bloom_filter_data_size
= 0;
2134 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2135 bloom_settings
.bits_per_entry
);
2136 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2137 bloom_settings
.num_hashes
);
2138 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2139 bloom_settings
.max_changed_paths
);
2140 ctx
->bloom_settings
= &bloom_settings
;
2142 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2143 ctx
->changed_paths
= 1;
2144 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2145 struct commit_graph
*g
;
2146 prepare_commit_graph_one(ctx
->r
, ctx
->odb
);
2148 g
= ctx
->r
->objects
->commit_graph
;
2150 /* We have changed-paths already. Keep them in the next graph */
2151 if (g
&& g
->chunk_bloom_data
) {
2152 ctx
->changed_paths
= 1;
2153 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2158 struct commit_graph
*g
;
2159 prepare_commit_graph(ctx
->r
);
2161 g
= ctx
->r
->objects
->commit_graph
;
2164 ctx
->num_commit_graphs_before
++;
2168 if (ctx
->num_commit_graphs_before
) {
2169 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2170 i
= ctx
->num_commit_graphs_before
;
2171 g
= ctx
->r
->objects
->commit_graph
;
2174 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2180 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2183 ctx
->approx_nr_objects
= approximate_object_count();
2186 prepare_commit_graph_one(ctx
->r
, ctx
->odb
);
2188 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2189 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2190 for (i
= 0; i
< g
->num_commits
; i
++) {
2191 struct object_id oid
;
2192 hashcpy(oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2193 oid_array_append(&ctx
->oids
, &oid
);
2198 ctx
->order_by_pack
= 1;
2199 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2204 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2208 if (!pack_indexes
&& !commits
) {
2209 ctx
->order_by_pack
= 1;
2210 fill_oids_from_all_packs(ctx
);
2213 close_reachable(ctx
);
2215 copy_oids_to_commits(ctx
);
2217 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2218 error(_("too many commits to write graph"));
2223 if (!ctx
->commits
.nr
&& !replace
)
2227 split_graph_merge_strategy(ctx
);
2230 merge_commit_graphs(ctx
);
2232 ctx
->num_commit_graphs_after
= 1;
2234 compute_generation_numbers(ctx
);
2236 if (ctx
->changed_paths
)
2237 compute_bloom_filters(ctx
);
2239 res
= write_commit_graph_file(ctx
);
2242 mark_commit_graphs(ctx
);
2244 expire_commit_graphs(ctx
);
2247 free(ctx
->graph_name
);
2248 free(ctx
->commits
.list
);
2249 oid_array_clear(&ctx
->oids
);
2251 if (ctx
->commit_graph_filenames_after
) {
2252 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2253 free(ctx
->commit_graph_filenames_after
[i
]);
2254 free(ctx
->commit_graph_hash_after
[i
]);
2257 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2258 free(ctx
->commit_graph_filenames_before
[i
]);
2260 free(ctx
->commit_graph_filenames_after
);
2261 free(ctx
->commit_graph_filenames_before
);
2262 free(ctx
->commit_graph_hash_after
);
2270 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2271 static int verify_commit_graph_error
;
2273 static void graph_report(const char *fmt
, ...)
2277 verify_commit_graph_error
= 1;
2279 vfprintf(stderr
, fmt
, ap
);
2280 fprintf(stderr
, "\n");
2284 #define GENERATION_ZERO_EXISTS 1
2285 #define GENERATION_NUMBER_EXISTS 2
2287 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2289 uint32_t i
, cur_fanout_pos
= 0;
2290 struct object_id prev_oid
, cur_oid
, checksum
;
2291 int generation_zero
= 0;
2294 struct progress
*progress
= NULL
;
2295 int local_error
= 0;
2298 graph_report("no commit-graph file loaded");
2302 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2303 if (verify_commit_graph_error
)
2304 return verify_commit_graph_error
;
2306 devnull
= open("/dev/null", O_WRONLY
);
2307 f
= hashfd(devnull
, NULL
);
2308 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
2309 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
2310 if (!hasheq(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
2311 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2312 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2315 for (i
= 0; i
< g
->num_commits
; i
++) {
2316 struct commit
*graph_commit
;
2318 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2320 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2321 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2322 oid_to_hex(&prev_oid
),
2323 oid_to_hex(&cur_oid
));
2325 oidcpy(&prev_oid
, &cur_oid
);
2327 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2328 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2330 if (i
!= fanout_value
)
2331 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2332 cur_fanout_pos
, fanout_value
, i
);
2336 graph_commit
= lookup_commit(r
, &cur_oid
);
2337 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2338 graph_report(_("failed to parse commit %s from commit-graph"),
2339 oid_to_hex(&cur_oid
));
2342 while (cur_fanout_pos
< 256) {
2343 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2345 if (g
->num_commits
!= fanout_value
)
2346 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2347 cur_fanout_pos
, fanout_value
, i
);
2352 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2353 return verify_commit_graph_error
;
2355 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2356 progress
= start_progress(_("Verifying commits in commit graph"),
2359 for (i
= 0; i
< g
->num_commits
; i
++) {
2360 struct commit
*graph_commit
, *odb_commit
;
2361 struct commit_list
*graph_parents
, *odb_parents
;
2362 uint32_t max_generation
= 0;
2363 uint32_t generation
;
2365 display_progress(progress
, i
+ 1);
2366 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2368 graph_commit
= lookup_commit(r
, &cur_oid
);
2369 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2370 if (parse_commit_internal(odb_commit
, 0, 0)) {
2371 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2372 oid_to_hex(&cur_oid
));
2376 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2377 get_commit_tree_oid(odb_commit
)))
2378 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2379 oid_to_hex(&cur_oid
),
2380 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2381 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2383 graph_parents
= graph_commit
->parents
;
2384 odb_parents
= odb_commit
->parents
;
2386 while (graph_parents
) {
2387 if (odb_parents
== NULL
) {
2388 graph_report(_("commit-graph parent list for commit %s is too long"),
2389 oid_to_hex(&cur_oid
));
2393 /* parse parent in case it is in a base graph */
2394 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2396 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2397 graph_report(_("commit-graph parent for %s is %s != %s"),
2398 oid_to_hex(&cur_oid
),
2399 oid_to_hex(&graph_parents
->item
->object
.oid
),
2400 oid_to_hex(&odb_parents
->item
->object
.oid
));
2402 generation
= commit_graph_generation(graph_parents
->item
);
2403 if (generation
> max_generation
)
2404 max_generation
= generation
;
2406 graph_parents
= graph_parents
->next
;
2407 odb_parents
= odb_parents
->next
;
2410 if (odb_parents
!= NULL
)
2411 graph_report(_("commit-graph parent list for commit %s terminates early"),
2412 oid_to_hex(&cur_oid
));
2414 if (!commit_graph_generation(graph_commit
)) {
2415 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2416 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2417 oid_to_hex(&cur_oid
));
2418 generation_zero
= GENERATION_ZERO_EXISTS
;
2419 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2420 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2421 oid_to_hex(&cur_oid
));
2423 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2427 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2428 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2429 * extra logic in the following condition.
2431 if (max_generation
== GENERATION_NUMBER_MAX
)
2434 generation
= commit_graph_generation(graph_commit
);
2435 if (generation
!= max_generation
+ 1)
2436 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2437 oid_to_hex(&cur_oid
),
2439 max_generation
+ 1);
2441 if (graph_commit
->date
!= odb_commit
->date
)
2442 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2443 oid_to_hex(&cur_oid
),
2447 stop_progress(&progress
);
2449 local_error
= verify_commit_graph_error
;
2451 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2452 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2457 void free_commit_graph(struct commit_graph
*g
)
2462 munmap((void *)g
->data
, g
->data_len
);
2466 free(g
->bloom_filter_settings
);
2470 void disable_commit_graph(struct repository
*r
)
2472 r
->commit_graph_disabled
= 1;