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
;
142 static int commit_gen_cmp(const void *va
, const void *vb
)
144 const struct commit
*a
= *(const struct commit
**)va
;
145 const struct commit
*b
= *(const struct commit
**)vb
;
147 uint32_t generation_a
= commit_graph_generation(a
);
148 uint32_t generation_b
= commit_graph_generation(b
);
149 /* lower generation commits first */
150 if (generation_a
< generation_b
)
152 else if (generation_a
> generation_b
)
155 /* use date as a heuristic when generations are equal */
156 if (a
->date
< b
->date
)
158 else if (a
->date
> b
->date
)
163 char *get_commit_graph_filename(struct object_directory
*obj_dir
)
165 return xstrfmt("%s/info/commit-graph", obj_dir
->path
);
168 static char *get_split_graph_filename(struct object_directory
*odb
,
171 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb
->path
,
175 char *get_commit_graph_chain_filename(struct object_directory
*odb
)
177 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb
->path
);
180 static uint8_t oid_version(void)
182 switch (hash_algo_by_ptr(the_hash_algo
)) {
185 case GIT_HASH_SHA256
:
188 die(_("invalid hash version"));
192 static struct commit_graph
*alloc_commit_graph(void)
194 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
199 extern int read_replace_refs
;
201 static int commit_graph_compatible(struct repository
*r
)
206 if (read_replace_refs
) {
207 prepare_replace_object(r
);
208 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
212 prepare_commit_graft(r
);
213 if (r
->parsed_objects
&&
214 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
216 if (is_repository_shallow(r
))
222 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
224 *fd
= git_open(graph_file
);
227 if (fstat(*fd
, st
)) {
234 struct commit_graph
*load_commit_graph_one_fd_st(struct repository
*r
,
235 int fd
, struct stat
*st
,
236 struct object_directory
*odb
)
240 struct commit_graph
*ret
;
242 graph_size
= xsize_t(st
->st_size
);
244 if (graph_size
< GRAPH_MIN_SIZE
) {
246 error(_("commit-graph file is too small"));
249 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
251 ret
= parse_commit_graph(r
, graph_map
, graph_size
);
256 munmap(graph_map
, graph_size
);
261 static int verify_commit_graph_lite(struct commit_graph
*g
)
264 * Basic validation shared between parse_commit_graph()
265 * which'll be called every time the graph is used, and the
266 * much more expensive verify_commit_graph() used by
267 * "commit-graph verify".
269 * There should only be very basic checks here to ensure that
270 * we don't e.g. segfault in fill_commit_in_graph(), but
271 * because this is a very hot codepath nothing that e.g. loops
272 * over g->num_commits, or runs a checksum on the commit-graph
275 if (!g
->chunk_oid_fanout
) {
276 error("commit-graph is missing the OID Fanout chunk");
279 if (!g
->chunk_oid_lookup
) {
280 error("commit-graph is missing the OID Lookup chunk");
283 if (!g
->chunk_commit_data
) {
284 error("commit-graph is missing the Commit Data chunk");
291 struct commit_graph
*parse_commit_graph(struct repository
*r
,
292 void *graph_map
, size_t graph_size
)
294 const unsigned char *data
, *chunk_lookup
;
296 struct commit_graph
*graph
;
297 uint64_t next_chunk_offset
;
298 uint32_t graph_signature
;
299 unsigned char graph_version
, hash_version
;
304 if (graph_size
< GRAPH_MIN_SIZE
)
307 data
= (const unsigned char *)graph_map
;
309 graph_signature
= get_be32(data
);
310 if (graph_signature
!= GRAPH_SIGNATURE
) {
311 error(_("commit-graph signature %X does not match signature %X"),
312 graph_signature
, GRAPH_SIGNATURE
);
316 graph_version
= *(unsigned char*)(data
+ 4);
317 if (graph_version
!= GRAPH_VERSION
) {
318 error(_("commit-graph version %X does not match version %X"),
319 graph_version
, GRAPH_VERSION
);
323 hash_version
= *(unsigned char*)(data
+ 5);
324 if (hash_version
!= oid_version()) {
325 error(_("commit-graph hash version %X does not match version %X"),
326 hash_version
, oid_version());
330 prepare_repo_settings(r
);
332 graph
= alloc_commit_graph();
334 graph
->hash_len
= the_hash_algo
->rawsz
;
335 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
336 graph
->data
= graph_map
;
337 graph
->data_len
= graph_size
;
339 if (graph_size
< GRAPH_HEADER_SIZE
+
340 (graph
->num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
+
341 GRAPH_FANOUT_SIZE
+ the_hash_algo
->rawsz
) {
342 error(_("commit-graph file is too small to hold %u chunks"),
348 chunk_lookup
= data
+ 8;
349 next_chunk_offset
= get_be64(chunk_lookup
+ 4);
350 for (i
= 0; i
< graph
->num_chunks
; i
++) {
352 uint64_t chunk_offset
= next_chunk_offset
;
353 int chunk_repeated
= 0;
355 chunk_id
= get_be32(chunk_lookup
+ 0);
357 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
358 next_chunk_offset
= get_be64(chunk_lookup
+ 4);
360 if (chunk_offset
> graph_size
- the_hash_algo
->rawsz
) {
361 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset
>> 32),
362 (uint32_t)chunk_offset
);
363 goto free_and_return
;
367 case GRAPH_CHUNKID_OIDFANOUT
:
368 if (graph
->chunk_oid_fanout
)
371 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
374 case GRAPH_CHUNKID_OIDLOOKUP
:
375 if (graph
->chunk_oid_lookup
)
378 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
379 graph
->num_commits
= (next_chunk_offset
- chunk_offset
)
384 case GRAPH_CHUNKID_DATA
:
385 if (graph
->chunk_commit_data
)
388 graph
->chunk_commit_data
= data
+ chunk_offset
;
391 case GRAPH_CHUNKID_EXTRAEDGES
:
392 if (graph
->chunk_extra_edges
)
395 graph
->chunk_extra_edges
= data
+ chunk_offset
;
398 case GRAPH_CHUNKID_BASE
:
399 if (graph
->chunk_base_graphs
)
402 graph
->chunk_base_graphs
= data
+ chunk_offset
;
405 case GRAPH_CHUNKID_BLOOMINDEXES
:
406 if (graph
->chunk_bloom_indexes
)
408 else if (r
->settings
.commit_graph_read_changed_paths
)
409 graph
->chunk_bloom_indexes
= data
+ chunk_offset
;
412 case GRAPH_CHUNKID_BLOOMDATA
:
413 if (graph
->chunk_bloom_data
)
415 else if (r
->settings
.commit_graph_read_changed_paths
) {
416 uint32_t hash_version
;
417 graph
->chunk_bloom_data
= data
+ chunk_offset
;
418 hash_version
= get_be32(data
+ chunk_offset
);
420 if (hash_version
!= 1)
423 graph
->bloom_filter_settings
= xmalloc(sizeof(struct bloom_filter_settings
));
424 graph
->bloom_filter_settings
->hash_version
= hash_version
;
425 graph
->bloom_filter_settings
->num_hashes
= get_be32(data
+ chunk_offset
+ 4);
426 graph
->bloom_filter_settings
->bits_per_entry
= get_be32(data
+ chunk_offset
+ 8);
427 graph
->bloom_filter_settings
->max_changed_paths
= DEFAULT_BLOOM_MAX_CHANGES
;
432 if (chunk_repeated
) {
433 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id
);
434 goto free_and_return
;
438 if (graph
->chunk_bloom_indexes
&& graph
->chunk_bloom_data
) {
439 init_bloom_filters();
441 /* We need both the bloom chunks to exist together. Else ignore the data */
442 graph
->chunk_bloom_indexes
= NULL
;
443 graph
->chunk_bloom_data
= NULL
;
444 FREE_AND_NULL(graph
->bloom_filter_settings
);
447 hashcpy(graph
->oid
.hash
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
449 if (verify_commit_graph_lite(graph
))
450 goto free_and_return
;
455 free(graph
->bloom_filter_settings
);
460 static struct commit_graph
*load_commit_graph_one(struct repository
*r
,
461 const char *graph_file
,
462 struct object_directory
*odb
)
467 struct commit_graph
*g
;
468 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
473 g
= load_commit_graph_one_fd_st(r
, fd
, &st
, odb
);
476 g
->filename
= xstrdup(graph_file
);
481 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
482 struct object_directory
*odb
)
484 char *graph_name
= get_commit_graph_filename(odb
);
485 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
491 static int add_graph_to_chain(struct commit_graph
*g
,
492 struct commit_graph
*chain
,
493 struct object_id
*oids
,
496 struct commit_graph
*cur_g
= chain
;
498 if (n
&& !g
->chunk_base_graphs
) {
499 warning(_("commit-graph has no base graphs chunk"));
507 !oideq(&oids
[n
], &cur_g
->oid
) ||
508 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
509 warning(_("commit-graph chain does not match"));
513 cur_g
= cur_g
->base_graph
;
516 g
->base_graph
= chain
;
519 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
524 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
525 struct object_directory
*odb
)
527 struct commit_graph
*graph_chain
= NULL
;
528 struct strbuf line
= STRBUF_INIT
;
530 struct object_id
*oids
;
531 int i
= 0, valid
= 1, count
;
532 char *chain_name
= get_commit_graph_chain_filename(odb
);
536 fp
= fopen(chain_name
, "r");
537 stat_res
= stat(chain_name
, &st
);
542 st
.st_size
<= the_hash_algo
->hexsz
)
545 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
546 oids
= xcalloc(count
, sizeof(struct object_id
));
550 for (i
= 0; i
< count
; i
++) {
551 struct object_directory
*odb
;
553 if (strbuf_getline_lf(&line
, fp
) == EOF
)
556 if (get_oid_hex(line
.buf
, &oids
[i
])) {
557 warning(_("invalid commit-graph chain: line '%s' not a hash"),
564 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
565 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
566 struct commit_graph
*g
= load_commit_graph_one(r
, graph_name
, odb
);
571 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
581 warning(_("unable to find all commit-graph files"));
588 strbuf_release(&line
);
593 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
594 struct object_directory
*odb
)
596 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
599 g
= load_commit_graph_chain(r
, odb
);
604 static void prepare_commit_graph_one(struct repository
*r
,
605 struct object_directory
*odb
)
608 if (r
->objects
->commit_graph
)
611 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
615 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
617 * On the first invocation, this function attempts to load the commit
618 * graph if the_repository is configured to have one.
620 static int prepare_commit_graph(struct repository
*r
)
622 struct object_directory
*odb
;
625 * This must come before the "already attempted?" check below, because
626 * we want to disable even an already-loaded graph file.
628 if (r
->commit_graph_disabled
)
631 if (r
->objects
->commit_graph_attempted
)
632 return !!r
->objects
->commit_graph
;
633 r
->objects
->commit_graph_attempted
= 1;
635 prepare_repo_settings(r
);
637 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
638 r
->settings
.core_commit_graph
!= 1)
640 * This repository is not configured to use commit graphs, so
641 * do not load one. (But report commit_graph_attempted anyway
642 * so that commit graph loading is not attempted again for this
647 if (!commit_graph_compatible(r
))
651 for (odb
= r
->objects
->odb
;
652 !r
->objects
->commit_graph
&& odb
;
654 prepare_commit_graph_one(r
, odb
);
655 return !!r
->objects
->commit_graph
;
658 int generation_numbers_enabled(struct repository
*r
)
660 uint32_t first_generation
;
661 struct commit_graph
*g
;
662 if (!prepare_commit_graph(r
))
665 g
= r
->objects
->commit_graph
;
670 first_generation
= get_be32(g
->chunk_commit_data
+
671 g
->hash_len
+ 8) >> 2;
673 return !!first_generation
;
676 struct bloom_filter_settings
*get_bloom_filter_settings(struct repository
*r
)
678 struct commit_graph
*g
= r
->objects
->commit_graph
;
680 if (g
->bloom_filter_settings
)
681 return g
->bloom_filter_settings
;
687 static void close_commit_graph_one(struct commit_graph
*g
)
692 close_commit_graph_one(g
->base_graph
);
693 free_commit_graph(g
);
696 void close_commit_graph(struct raw_object_store
*o
)
698 close_commit_graph_one(o
->commit_graph
);
699 o
->commit_graph
= NULL
;
702 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
704 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
705 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
708 static void load_oid_from_graph(struct commit_graph
*g
,
710 struct object_id
*oid
)
714 while (g
&& pos
< g
->num_commits_in_base
)
718 BUG("NULL commit-graph");
720 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
721 die(_("invalid commit position. commit-graph is likely corrupt"));
723 lex_index
= pos
- g
->num_commits_in_base
;
725 hashcpy(oid
->hash
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
728 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
729 struct commit_graph
*g
,
731 struct commit_list
**pptr
)
734 struct object_id oid
;
736 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
737 die("invalid parent position %"PRIu32
, pos
);
739 load_oid_from_graph(g
, pos
, &oid
);
740 c
= lookup_commit(r
, &oid
);
742 die(_("could not find commit %s"), oid_to_hex(&oid
));
743 commit_graph_data_at(c
)->graph_pos
= pos
;
744 return &commit_list_insert(c
, pptr
)->next
;
747 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
749 const unsigned char *commit_data
;
750 struct commit_graph_data
*graph_data
;
753 while (pos
< g
->num_commits_in_base
)
756 lex_index
= pos
- g
->num_commits_in_base
;
757 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
759 graph_data
= commit_graph_data_at(item
);
760 graph_data
->graph_pos
= pos
;
761 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
764 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
769 static int fill_commit_in_graph(struct repository
*r
,
771 struct commit_graph
*g
, uint32_t pos
)
774 uint32_t *parent_data_ptr
;
775 uint64_t date_low
, date_high
;
776 struct commit_list
**pptr
;
777 struct commit_graph_data
*graph_data
;
778 const unsigned char *commit_data
;
781 while (pos
< g
->num_commits_in_base
)
784 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
785 die(_("invalid commit position. commit-graph is likely corrupt"));
788 * Store the "full" position, but then use the
789 * "local" position for the rest of the calculation.
791 graph_data
= commit_graph_data_at(item
);
792 graph_data
->graph_pos
= pos
;
793 lex_index
= pos
- g
->num_commits_in_base
;
795 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
797 item
->object
.parsed
= 1;
799 set_commit_tree(item
, NULL
);
801 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
802 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
803 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
805 graph_data
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
807 pptr
= &item
->parents
;
809 edge_value
= get_be32(commit_data
+ g
->hash_len
);
810 if (edge_value
== GRAPH_PARENT_NONE
)
812 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
814 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
815 if (edge_value
== GRAPH_PARENT_NONE
)
817 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
818 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
822 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
823 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
825 edge_value
= get_be32(parent_data_ptr
);
826 pptr
= insert_parent_or_die(r
, g
,
827 edge_value
& GRAPH_EDGE_LAST_MASK
,
830 } while (!(edge_value
& GRAPH_LAST_EDGE
));
835 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
837 uint32_t graph_pos
= commit_graph_position(item
);
838 if (graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
842 struct commit_graph
*cur_g
= g
;
845 while (cur_g
&& !bsearch_graph(cur_g
, &(item
->object
.oid
), &lex_index
))
846 cur_g
= cur_g
->base_graph
;
849 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
857 static int parse_commit_in_graph_one(struct repository
*r
,
858 struct commit_graph
*g
,
863 if (item
->object
.parsed
)
866 if (find_commit_in_graph(item
, g
, &pos
))
867 return fill_commit_in_graph(r
, item
, g
, pos
);
872 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
874 static int checked_env
= 0;
877 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
, 0))
878 die("dying as requested by the '%s' variable on commit-graph parse!",
879 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE
);
882 if (!prepare_commit_graph(r
))
884 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
887 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
890 if (!prepare_commit_graph(r
))
892 if (find_commit_in_graph(item
, r
->objects
->commit_graph
, &pos
))
893 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
896 static struct tree
*load_tree_for_commit(struct repository
*r
,
897 struct commit_graph
*g
,
900 struct object_id oid
;
901 const unsigned char *commit_data
;
902 uint32_t graph_pos
= commit_graph_position(c
);
904 while (graph_pos
< g
->num_commits_in_base
)
907 commit_data
= g
->chunk_commit_data
+
908 GRAPH_DATA_WIDTH
* (graph_pos
- g
->num_commits_in_base
);
910 hashcpy(oid
.hash
, commit_data
);
911 set_commit_tree(c
, lookup_tree(r
, &oid
));
913 return c
->maybe_tree
;
916 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
917 struct commit_graph
*g
,
918 const struct commit
*c
)
921 return c
->maybe_tree
;
922 if (commit_graph_position(c
) == COMMIT_NOT_FROM_GRAPH
)
923 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
925 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
928 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
930 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
933 struct packed_commit_list
{
934 struct commit
**list
;
939 struct packed_oid_list
{
940 struct object_id
*list
;
945 struct write_commit_graph_context
{
946 struct repository
*r
;
947 struct object_directory
*odb
;
949 struct packed_oid_list oids
;
950 struct packed_commit_list commits
;
952 unsigned long approx_nr_objects
;
953 struct progress
*progress
;
955 uint64_t progress_cnt
;
957 char *base_graph_name
;
958 int num_commit_graphs_before
;
959 int num_commit_graphs_after
;
960 char **commit_graph_filenames_before
;
961 char **commit_graph_filenames_after
;
962 char **commit_graph_hash_after
;
963 uint32_t new_num_commits_in_base
;
964 struct commit_graph
*new_base_graph
;
972 const struct commit_graph_opts
*opts
;
973 size_t total_bloom_filter_data_size
;
974 const struct bloom_filter_settings
*bloom_settings
;
976 int count_bloom_filter_computed
;
977 int count_bloom_filter_not_computed
;
978 int count_bloom_filter_trunc_empty
;
979 int count_bloom_filter_trunc_large
;
982 static int write_graph_chunk_fanout(struct hashfile
*f
,
983 struct write_commit_graph_context
*ctx
)
986 struct commit
**list
= ctx
->commits
.list
;
989 * Write the first-level table (the list is sorted,
990 * but we use a 256-entry lookup to be able to avoid
991 * having to do eight extra binary search iterations).
993 for (i
= 0; i
< 256; i
++) {
994 while (count
< ctx
->commits
.nr
) {
995 if ((*list
)->object
.oid
.hash
[0] != i
)
997 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1002 hashwrite_be32(f
, count
);
1008 static int write_graph_chunk_oids(struct hashfile
*f
,
1009 struct write_commit_graph_context
*ctx
)
1011 struct commit
**list
= ctx
->commits
.list
;
1013 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
1014 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1015 hashwrite(f
, (*list
)->object
.oid
.hash
, the_hash_algo
->rawsz
);
1021 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
1023 struct commit
**commits
= table
;
1024 return commits
[index
]->object
.oid
.hash
;
1027 static int write_graph_chunk_data(struct hashfile
*f
,
1028 struct write_commit_graph_context
*ctx
)
1030 struct commit
**list
= ctx
->commits
.list
;
1031 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1032 uint32_t num_extra_edges
= 0;
1034 while (list
< last
) {
1035 struct commit_list
*parent
;
1036 struct object_id
*tree
;
1038 uint32_t packedDate
[2];
1039 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1041 if (parse_commit_no_graph(*list
))
1042 die(_("unable to parse commit %s"),
1043 oid_to_hex(&(*list
)->object
.oid
));
1044 tree
= get_commit_tree_oid(*list
);
1045 hashwrite(f
, tree
->hash
, the_hash_algo
->rawsz
);
1047 parent
= (*list
)->parents
;
1050 edge_value
= GRAPH_PARENT_NONE
;
1052 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
1057 if (edge_value
>= 0)
1058 edge_value
+= ctx
->new_num_commits_in_base
;
1059 else if (ctx
->new_base_graph
) {
1061 if (find_commit_in_graph(parent
->item
,
1062 ctx
->new_base_graph
,
1068 BUG("missing parent %s for commit %s",
1069 oid_to_hex(&parent
->item
->object
.oid
),
1070 oid_to_hex(&(*list
)->object
.oid
));
1073 hashwrite_be32(f
, edge_value
);
1076 parent
= parent
->next
;
1079 edge_value
= GRAPH_PARENT_NONE
;
1080 else if (parent
->next
)
1081 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
1083 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
1088 if (edge_value
>= 0)
1089 edge_value
+= ctx
->new_num_commits_in_base
;
1090 else if (ctx
->new_base_graph
) {
1092 if (find_commit_in_graph(parent
->item
,
1093 ctx
->new_base_graph
,
1099 BUG("missing parent %s for commit %s",
1100 oid_to_hex(&parent
->item
->object
.oid
),
1101 oid_to_hex(&(*list
)->object
.oid
));
1104 hashwrite_be32(f
, edge_value
);
1106 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
1109 parent
= parent
->next
;
1113 if (sizeof((*list
)->date
) > 4)
1114 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
1118 packedDate
[0] |= htonl(commit_graph_data_at(*list
)->generation
<< 2);
1120 packedDate
[1] = htonl((*list
)->date
);
1121 hashwrite(f
, packedDate
, 8);
1129 static int write_graph_chunk_extra_edges(struct hashfile
*f
,
1130 struct write_commit_graph_context
*ctx
)
1132 struct commit
**list
= ctx
->commits
.list
;
1133 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1134 struct commit_list
*parent
;
1136 while (list
< last
) {
1137 int num_parents
= 0;
1139 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1141 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
1142 parent
= parent
->next
)
1145 if (num_parents
<= 2) {
1150 /* Since num_parents > 2, this initializer is safe. */
1151 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
1152 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
1157 if (edge_value
>= 0)
1158 edge_value
+= ctx
->new_num_commits_in_base
;
1159 else if (ctx
->new_base_graph
) {
1161 if (find_commit_in_graph(parent
->item
,
1162 ctx
->new_base_graph
,
1168 BUG("missing parent %s for commit %s",
1169 oid_to_hex(&parent
->item
->object
.oid
),
1170 oid_to_hex(&(*list
)->object
.oid
));
1171 else if (!parent
->next
)
1172 edge_value
|= GRAPH_LAST_EDGE
;
1174 hashwrite_be32(f
, edge_value
);
1183 static int write_graph_chunk_bloom_indexes(struct hashfile
*f
,
1184 struct write_commit_graph_context
*ctx
)
1186 struct commit
**list
= ctx
->commits
.list
;
1187 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1188 uint32_t cur_pos
= 0;
1190 while (list
< last
) {
1191 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1192 size_t len
= filter
? filter
->len
: 0;
1194 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1195 hashwrite_be32(f
, cur_pos
);
1202 static void trace2_bloom_filter_settings(struct write_commit_graph_context
*ctx
)
1204 struct json_writer jw
= JSON_WRITER_INIT
;
1206 jw_object_begin(&jw
, 0);
1207 jw_object_intmax(&jw
, "hash_version", ctx
->bloom_settings
->hash_version
);
1208 jw_object_intmax(&jw
, "num_hashes", ctx
->bloom_settings
->num_hashes
);
1209 jw_object_intmax(&jw
, "bits_per_entry", ctx
->bloom_settings
->bits_per_entry
);
1210 jw_object_intmax(&jw
, "max_changed_paths", ctx
->bloom_settings
->max_changed_paths
);
1213 trace2_data_json("bloom", ctx
->r
, "settings", &jw
);
1218 static int write_graph_chunk_bloom_data(struct hashfile
*f
,
1219 struct write_commit_graph_context
*ctx
)
1221 struct commit
**list
= ctx
->commits
.list
;
1222 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
1224 trace2_bloom_filter_settings(ctx
);
1226 hashwrite_be32(f
, ctx
->bloom_settings
->hash_version
);
1227 hashwrite_be32(f
, ctx
->bloom_settings
->num_hashes
);
1228 hashwrite_be32(f
, ctx
->bloom_settings
->bits_per_entry
);
1230 while (list
< last
) {
1231 struct bloom_filter
*filter
= get_bloom_filter(ctx
->r
, *list
);
1232 size_t len
= filter
? filter
->len
: 0;
1234 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
1236 hashwrite(f
, filter
->data
, len
* sizeof(unsigned char));
1243 static int oid_compare(const void *_a
, const void *_b
)
1245 const struct object_id
*a
= (const struct object_id
*)_a
;
1246 const struct object_id
*b
= (const struct object_id
*)_b
;
1247 return oidcmp(a
, b
);
1250 static int add_packed_commits(const struct object_id
*oid
,
1251 struct packed_git
*pack
,
1255 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1256 enum object_type type
;
1257 off_t offset
= nth_packed_object_offset(pack
, pos
);
1258 struct object_info oi
= OBJECT_INFO_INIT
;
1261 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1264 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1265 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1267 if (type
!= OBJ_COMMIT
)
1270 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1271 oidcpy(&(ctx
->oids
.list
[ctx
->oids
.nr
]), oid
);
1274 set_commit_pos(ctx
->r
, oid
);
1279 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1281 struct commit_list
*parent
;
1282 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1283 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1284 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1285 oidcpy(&ctx
->oids
.list
[ctx
->oids
.nr
], &(parent
->item
->object
.oid
));
1287 parent
->item
->object
.flags
|= REACHABLE
;
1292 static void close_reachable(struct write_commit_graph_context
*ctx
)
1295 struct commit
*commit
;
1296 enum commit_graph_split_flags flags
= ctx
->opts
?
1297 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1299 if (ctx
->report_progress
)
1300 ctx
->progress
= start_delayed_progress(
1301 _("Loading known commits in commit graph"),
1303 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1304 display_progress(ctx
->progress
, i
+ 1);
1305 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1307 commit
->object
.flags
|= REACHABLE
;
1309 stop_progress(&ctx
->progress
);
1312 * As this loop runs, ctx->oids.nr may grow, but not more
1313 * than the number of missing commits in the reachable
1316 if (ctx
->report_progress
)
1317 ctx
->progress
= start_delayed_progress(
1318 _("Expanding reachable commits in commit graph"),
1320 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1321 display_progress(ctx
->progress
, i
+ 1);
1322 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1327 if ((!parse_commit(commit
) &&
1328 commit_graph_position(commit
) == COMMIT_NOT_FROM_GRAPH
) ||
1329 flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1330 add_missing_parents(ctx
, commit
);
1331 } else if (!parse_commit_no_graph(commit
))
1332 add_missing_parents(ctx
, commit
);
1334 stop_progress(&ctx
->progress
);
1336 if (ctx
->report_progress
)
1337 ctx
->progress
= start_delayed_progress(
1338 _("Clearing commit marks in commit graph"),
1340 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1341 display_progress(ctx
->progress
, i
+ 1);
1342 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1345 commit
->object
.flags
&= ~REACHABLE
;
1347 stop_progress(&ctx
->progress
);
1350 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1353 struct commit_list
*list
= NULL
;
1355 if (ctx
->report_progress
)
1356 ctx
->progress
= start_delayed_progress(
1357 _("Computing commit graph generation numbers"),
1359 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1360 uint32_t generation
= commit_graph_data_at(ctx
->commits
.list
[i
])->generation
;
1362 display_progress(ctx
->progress
, i
+ 1);
1363 if (generation
!= GENERATION_NUMBER_INFINITY
&&
1364 generation
!= GENERATION_NUMBER_ZERO
)
1367 commit_list_insert(ctx
->commits
.list
[i
], &list
);
1369 struct commit
*current
= list
->item
;
1370 struct commit_list
*parent
;
1371 int all_parents_computed
= 1;
1372 uint32_t max_generation
= 0;
1374 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1375 generation
= commit_graph_data_at(parent
->item
)->generation
;
1377 if (generation
== GENERATION_NUMBER_INFINITY
||
1378 generation
== GENERATION_NUMBER_ZERO
) {
1379 all_parents_computed
= 0;
1380 commit_list_insert(parent
->item
, &list
);
1382 } else if (generation
> max_generation
) {
1383 max_generation
= generation
;
1387 if (all_parents_computed
) {
1388 struct commit_graph_data
*data
= commit_graph_data_at(current
);
1390 data
->generation
= max_generation
+ 1;
1393 if (data
->generation
> GENERATION_NUMBER_MAX
)
1394 data
->generation
= GENERATION_NUMBER_MAX
;
1398 stop_progress(&ctx
->progress
);
1401 static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context
*ctx
)
1403 trace2_data_intmax("commit-graph", ctx
->r
, "filter-computed",
1404 ctx
->count_bloom_filter_computed
);
1405 trace2_data_intmax("commit-graph", ctx
->r
, "filter-not-computed",
1406 ctx
->count_bloom_filter_not_computed
);
1407 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-empty",
1408 ctx
->count_bloom_filter_trunc_empty
);
1409 trace2_data_intmax("commit-graph", ctx
->r
, "filter-trunc-large",
1410 ctx
->count_bloom_filter_trunc_large
);
1413 static void compute_bloom_filters(struct write_commit_graph_context
*ctx
)
1416 struct progress
*progress
= NULL
;
1417 struct commit
**sorted_commits
;
1418 int max_new_filters
;
1420 init_bloom_filters();
1422 if (ctx
->report_progress
)
1423 progress
= start_delayed_progress(
1424 _("Computing commit changed paths Bloom filters"),
1427 ALLOC_ARRAY(sorted_commits
, ctx
->commits
.nr
);
1428 COPY_ARRAY(sorted_commits
, ctx
->commits
.list
, ctx
->commits
.nr
);
1430 if (ctx
->order_by_pack
)
1431 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_pos_cmp
);
1433 QSORT(sorted_commits
, ctx
->commits
.nr
, commit_gen_cmp
);
1435 max_new_filters
= ctx
->opts
&& ctx
->opts
->max_new_filters
>= 0 ?
1436 ctx
->opts
->max_new_filters
: ctx
->commits
.nr
;
1438 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1439 enum bloom_filter_computed computed
= 0;
1440 struct commit
*c
= sorted_commits
[i
];
1441 struct bloom_filter
*filter
= get_or_compute_bloom_filter(
1444 ctx
->count_bloom_filter_computed
< max_new_filters
,
1445 ctx
->bloom_settings
,
1447 if (computed
& BLOOM_COMPUTED
) {
1448 ctx
->count_bloom_filter_computed
++;
1449 if (computed
& BLOOM_TRUNC_EMPTY
)
1450 ctx
->count_bloom_filter_trunc_empty
++;
1451 if (computed
& BLOOM_TRUNC_LARGE
)
1452 ctx
->count_bloom_filter_trunc_large
++;
1453 } else if (computed
& BLOOM_NOT_COMPUTED
)
1454 ctx
->count_bloom_filter_not_computed
++;
1455 ctx
->total_bloom_filter_data_size
+= filter
1456 ? sizeof(unsigned char) * filter
->len
: 0;
1457 display_progress(progress
, i
+ 1);
1460 if (trace2_is_enabled())
1461 trace2_bloom_filter_write_statistics(ctx
);
1463 free(sorted_commits
);
1464 stop_progress(&progress
);
1467 struct refs_cb_data
{
1468 struct oidset
*commits
;
1469 struct progress
*progress
;
1472 static int add_ref_to_set(const char *refname
,
1473 const struct object_id
*oid
,
1474 int flags
, void *cb_data
)
1476 struct object_id peeled
;
1477 struct refs_cb_data
*data
= (struct refs_cb_data
*)cb_data
;
1479 if (!peel_ref(refname
, &peeled
))
1481 if (oid_object_info(the_repository
, oid
, NULL
) == OBJ_COMMIT
)
1482 oidset_insert(data
->commits
, oid
);
1484 display_progress(data
->progress
, oidset_size(data
->commits
));
1489 int write_commit_graph_reachable(struct object_directory
*odb
,
1490 enum commit_graph_write_flags flags
,
1491 const struct commit_graph_opts
*opts
)
1493 struct oidset commits
= OIDSET_INIT
;
1494 struct refs_cb_data data
;
1497 memset(&data
, 0, sizeof(data
));
1498 data
.commits
= &commits
;
1499 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1500 data
.progress
= start_delayed_progress(
1501 _("Collecting referenced commits"), 0);
1503 for_each_ref(add_ref_to_set
, &data
);
1505 stop_progress(&data
.progress
);
1507 result
= write_commit_graph(odb
, NULL
, &commits
,
1510 oidset_clear(&commits
);
1514 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1515 struct string_list
*pack_indexes
)
1518 struct strbuf progress_title
= STRBUF_INIT
;
1519 struct strbuf packname
= STRBUF_INIT
;
1522 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1523 dirlen
= packname
.len
;
1524 if (ctx
->report_progress
) {
1525 strbuf_addf(&progress_title
,
1526 Q_("Finding commits for commit graph in %d pack",
1527 "Finding commits for commit graph in %d packs",
1530 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1531 ctx
->progress_done
= 0;
1533 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1534 struct packed_git
*p
;
1535 strbuf_setlen(&packname
, dirlen
);
1536 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1537 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1539 error(_("error adding pack %s"), packname
.buf
);
1542 if (open_pack_index(p
)) {
1543 error(_("error opening index for %s"), packname
.buf
);
1546 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1547 FOR_EACH_OBJECT_PACK_ORDER
);
1552 stop_progress(&ctx
->progress
);
1553 strbuf_release(&progress_title
);
1554 strbuf_release(&packname
);
1559 static int fill_oids_from_commits(struct write_commit_graph_context
*ctx
,
1560 struct oidset
*commits
)
1562 struct oidset_iter iter
;
1563 struct object_id
*oid
;
1565 if (!oidset_size(commits
))
1568 oidset_iter_init(commits
, &iter
);
1569 while ((oid
= oidset_iter_next(&iter
))) {
1570 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1571 oidcpy(&ctx
->oids
.list
[ctx
->oids
.nr
], oid
);
1578 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1580 if (ctx
->report_progress
)
1581 ctx
->progress
= start_delayed_progress(
1582 _("Finding commits for commit graph among packed objects"),
1583 ctx
->approx_nr_objects
);
1584 for_each_packed_object(add_packed_commits
, ctx
,
1585 FOR_EACH_OBJECT_PACK_ORDER
);
1586 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1587 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1588 stop_progress(&ctx
->progress
);
1591 static uint32_t count_distinct_commits(struct write_commit_graph_context
*ctx
)
1593 uint32_t i
, count_distinct
= 1;
1595 if (ctx
->report_progress
)
1596 ctx
->progress
= start_delayed_progress(
1597 _("Counting distinct commits in commit graph"),
1599 display_progress(ctx
->progress
, 0); /* TODO: Measure QSORT() progress */
1600 QSORT(ctx
->oids
.list
, ctx
->oids
.nr
, oid_compare
);
1602 for (i
= 1; i
< ctx
->oids
.nr
; i
++) {
1603 display_progress(ctx
->progress
, i
+ 1);
1604 if (!oideq(&ctx
->oids
.list
[i
- 1], &ctx
->oids
.list
[i
])) {
1606 struct commit
*c
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1608 if (!c
|| commit_graph_position(c
) != COMMIT_NOT_FROM_GRAPH
)
1615 stop_progress(&ctx
->progress
);
1617 return count_distinct
;
1620 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1623 enum commit_graph_split_flags flags
= ctx
->opts
?
1624 ctx
->opts
->split_flags
: COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1626 ctx
->num_extra_edges
= 0;
1627 if (ctx
->report_progress
)
1628 ctx
->progress
= start_delayed_progress(
1629 _("Finding extra edges in commit graph"),
1631 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1632 unsigned int num_parents
;
1634 display_progress(ctx
->progress
, i
+ 1);
1635 if (i
> 0 && oideq(&ctx
->oids
.list
[i
- 1], &ctx
->oids
.list
[i
]))
1638 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1639 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1641 if (ctx
->split
&& flags
!= COMMIT_GRAPH_SPLIT_REPLACE
&&
1642 commit_graph_position(ctx
->commits
.list
[ctx
->commits
.nr
]) != COMMIT_NOT_FROM_GRAPH
)
1645 if (ctx
->split
&& flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1646 parse_commit(ctx
->commits
.list
[ctx
->commits
.nr
]);
1648 parse_commit_no_graph(ctx
->commits
.list
[ctx
->commits
.nr
]);
1650 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1651 if (num_parents
> 2)
1652 ctx
->num_extra_edges
+= num_parents
- 1;
1656 stop_progress(&ctx
->progress
);
1659 static int write_graph_chunk_base_1(struct hashfile
*f
,
1660 struct commit_graph
*g
)
1667 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1668 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1672 static int write_graph_chunk_base(struct hashfile
*f
,
1673 struct write_commit_graph_context
*ctx
)
1675 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1677 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1678 error(_("failed to write correct number of base graph ids"));
1685 typedef int (*chunk_write_fn
)(struct hashfile
*f
,
1686 struct write_commit_graph_context
*ctx
);
1691 chunk_write_fn write_fn
;
1694 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1699 struct lock_file lk
= LOCK_INIT
;
1700 struct chunk_info chunks
[MAX_NUM_CHUNKS
+ 1];
1701 const unsigned hashsz
= the_hash_algo
->rawsz
;
1702 struct strbuf progress_title
= STRBUF_INIT
;
1704 uint64_t chunk_offset
;
1705 struct object_id file_hash
;
1708 struct strbuf tmp_file
= STRBUF_INIT
;
1710 strbuf_addf(&tmp_file
,
1711 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1713 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1715 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1718 if (safe_create_leading_directories(ctx
->graph_name
)) {
1719 UNLEAK(ctx
->graph_name
);
1720 error(_("unable to create leading directories of %s"),
1726 char *lock_name
= get_commit_graph_chain_filename(ctx
->odb
);
1728 hold_lock_file_for_update_mode(&lk
, lock_name
,
1729 LOCK_DIE_ON_ERROR
, 0444);
1731 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1733 error(_("unable to create temporary graph layer"));
1737 if (adjust_shared_perm(ctx
->graph_name
)) {
1738 error(_("unable to adjust shared permissions for '%s'"),
1743 f
= hashfd(fd
, ctx
->graph_name
);
1745 hold_lock_file_for_update_mode(&lk
, ctx
->graph_name
,
1746 LOCK_DIE_ON_ERROR
, 0444);
1747 fd
= lk
.tempfile
->fd
;
1748 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
1751 chunks
[0].id
= GRAPH_CHUNKID_OIDFANOUT
;
1752 chunks
[0].size
= GRAPH_FANOUT_SIZE
;
1753 chunks
[0].write_fn
= write_graph_chunk_fanout
;
1754 chunks
[1].id
= GRAPH_CHUNKID_OIDLOOKUP
;
1755 chunks
[1].size
= hashsz
* ctx
->commits
.nr
;
1756 chunks
[1].write_fn
= write_graph_chunk_oids
;
1757 chunks
[2].id
= GRAPH_CHUNKID_DATA
;
1758 chunks
[2].size
= (hashsz
+ 16) * ctx
->commits
.nr
;
1759 chunks
[2].write_fn
= write_graph_chunk_data
;
1760 if (ctx
->num_extra_edges
) {
1761 chunks
[num_chunks
].id
= GRAPH_CHUNKID_EXTRAEDGES
;
1762 chunks
[num_chunks
].size
= 4 * ctx
->num_extra_edges
;
1763 chunks
[num_chunks
].write_fn
= write_graph_chunk_extra_edges
;
1766 if (ctx
->changed_paths
) {
1767 chunks
[num_chunks
].id
= GRAPH_CHUNKID_BLOOMINDEXES
;
1768 chunks
[num_chunks
].size
= sizeof(uint32_t) * ctx
->commits
.nr
;
1769 chunks
[num_chunks
].write_fn
= write_graph_chunk_bloom_indexes
;
1771 chunks
[num_chunks
].id
= GRAPH_CHUNKID_BLOOMDATA
;
1772 chunks
[num_chunks
].size
= sizeof(uint32_t) * 3
1773 + ctx
->total_bloom_filter_data_size
;
1774 chunks
[num_chunks
].write_fn
= write_graph_chunk_bloom_data
;
1777 if (ctx
->num_commit_graphs_after
> 1) {
1778 chunks
[num_chunks
].id
= GRAPH_CHUNKID_BASE
;
1779 chunks
[num_chunks
].size
= hashsz
* (ctx
->num_commit_graphs_after
- 1);
1780 chunks
[num_chunks
].write_fn
= write_graph_chunk_base
;
1784 chunks
[num_chunks
].id
= 0;
1785 chunks
[num_chunks
].size
= 0;
1787 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1789 hashwrite_u8(f
, GRAPH_VERSION
);
1790 hashwrite_u8(f
, oid_version());
1791 hashwrite_u8(f
, num_chunks
);
1792 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1794 chunk_offset
= 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
1795 for (i
= 0; i
<= num_chunks
; i
++) {
1796 uint32_t chunk_write
[3];
1798 chunk_write
[0] = htonl(chunks
[i
].id
);
1799 chunk_write
[1] = htonl(chunk_offset
>> 32);
1800 chunk_write
[2] = htonl(chunk_offset
& 0xffffffff);
1801 hashwrite(f
, chunk_write
, 12);
1803 chunk_offset
+= chunks
[i
].size
;
1806 if (ctx
->report_progress
) {
1807 strbuf_addf(&progress_title
,
1808 Q_("Writing out commit graph in %d pass",
1809 "Writing out commit graph in %d passes",
1812 ctx
->progress
= start_delayed_progress(
1814 num_chunks
* ctx
->commits
.nr
);
1817 for (i
= 0; i
< num_chunks
; i
++) {
1818 uint64_t start_offset
= f
->total
+ f
->offset
;
1820 if (chunks
[i
].write_fn(f
, ctx
))
1823 if (f
->total
+ f
->offset
!= start_offset
+ chunks
[i
].size
)
1824 BUG("expected to write %"PRId64
" bytes to chunk %"PRIx32
", but wrote %"PRId64
" instead",
1825 chunks
[i
].size
, chunks
[i
].id
,
1826 f
->total
+ f
->offset
- start_offset
);
1829 stop_progress(&ctx
->progress
);
1830 strbuf_release(&progress_title
);
1832 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1833 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1834 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
1836 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1837 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1838 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1839 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1842 close_commit_graph(ctx
->r
->objects
);
1843 finalize_hashfile(f
, file_hash
.hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1846 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1847 char *final_graph_name
;
1853 error(_("unable to open commit-graph chain file"));
1857 if (ctx
->base_graph_name
) {
1859 int idx
= ctx
->num_commit_graphs_after
- 1;
1860 if (ctx
->num_commit_graphs_after
> 1)
1863 dest
= ctx
->commit_graph_filenames_after
[idx
];
1865 if (strcmp(ctx
->base_graph_name
, dest
)) {
1866 result
= rename(ctx
->base_graph_name
, dest
);
1869 error(_("failed to rename base commit-graph file"));
1874 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
1878 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(oid_to_hex(&file_hash
));
1879 final_graph_name
= get_split_graph_filename(ctx
->odb
,
1880 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1881 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
1883 result
= rename(ctx
->graph_name
, final_graph_name
);
1885 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
1886 fprintf(lk
.tempfile
->fp
, "%s\n", ctx
->commit_graph_hash_after
[i
]);
1889 error(_("failed to rename temporary commit-graph file"));
1894 commit_lock_file(&lk
);
1899 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
1901 struct commit_graph
*g
;
1902 uint32_t num_commits
;
1903 enum commit_graph_split_flags flags
= COMMIT_GRAPH_SPLIT_UNSPECIFIED
;
1906 int max_commits
= 0;
1910 max_commits
= ctx
->opts
->max_commits
;
1912 if (ctx
->opts
->size_multiple
)
1913 size_mult
= ctx
->opts
->size_multiple
;
1915 flags
= ctx
->opts
->split_flags
;
1918 g
= ctx
->r
->objects
->commit_graph
;
1919 num_commits
= ctx
->commits
.nr
;
1920 if (flags
== COMMIT_GRAPH_SPLIT_REPLACE
)
1921 ctx
->num_commit_graphs_after
= 1;
1923 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
1925 if (flags
!= COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED
&&
1926 flags
!= COMMIT_GRAPH_SPLIT_REPLACE
) {
1927 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
1928 (max_commits
&& num_commits
> max_commits
))) {
1929 if (g
->odb
!= ctx
->odb
)
1932 num_commits
+= g
->num_commits
;
1935 ctx
->num_commit_graphs_after
--;
1939 if (flags
!= COMMIT_GRAPH_SPLIT_REPLACE
)
1940 ctx
->new_base_graph
= g
;
1941 else if (ctx
->num_commit_graphs_after
!= 1)
1942 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1943 "should be 1 with --split=replace");
1945 if (ctx
->num_commit_graphs_after
== 2) {
1946 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
1948 if (!strcmp(g
->filename
, old_graph_name
) &&
1949 g
->odb
!= ctx
->odb
) {
1950 ctx
->num_commit_graphs_after
= 1;
1951 ctx
->new_base_graph
= NULL
;
1954 free(old_graph_name
);
1957 CALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
1958 CALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
1960 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
1961 i
< ctx
->num_commit_graphs_before
; i
++)
1962 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
1964 i
= ctx
->num_commit_graphs_before
- 1;
1965 g
= ctx
->r
->objects
->commit_graph
;
1968 if (i
< ctx
->num_commit_graphs_after
)
1969 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
1976 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
1977 struct commit_graph
*g
)
1980 uint32_t offset
= g
->num_commits_in_base
;
1982 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
1984 for (i
= 0; i
< g
->num_commits
; i
++) {
1985 struct object_id oid
;
1986 struct commit
*result
;
1988 display_progress(ctx
->progress
, i
+ 1);
1990 load_oid_from_graph(g
, i
+ offset
, &oid
);
1992 /* only add commits if they still exist in the repo */
1993 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
1996 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
2002 static int commit_compare(const void *_a
, const void *_b
)
2004 const struct commit
*a
= *(const struct commit
**)_a
;
2005 const struct commit
*b
= *(const struct commit
**)_b
;
2006 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
2009 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
2013 if (ctx
->report_progress
)
2014 ctx
->progress
= start_delayed_progress(
2015 _("Scanning merged commits"),
2018 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
2020 ctx
->num_extra_edges
= 0;
2021 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
2022 display_progress(ctx
->progress
, i
);
2024 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
2025 &ctx
->commits
.list
[i
]->object
.oid
)) {
2026 die(_("unexpected duplicate commit id %s"),
2027 oid_to_hex(&ctx
->commits
.list
[i
]->object
.oid
));
2029 unsigned int num_parents
;
2031 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
2032 if (num_parents
> 2)
2033 ctx
->num_extra_edges
+= num_parents
- 1;
2037 stop_progress(&ctx
->progress
);
2040 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
2042 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2043 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
2045 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
2046 current_graph_number
--;
2048 if (ctx
->report_progress
)
2049 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
2051 merge_commit_graph(ctx
, g
);
2052 stop_progress(&ctx
->progress
);
2058 ctx
->new_base_graph
= g
;
2059 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
2062 if (ctx
->new_base_graph
)
2063 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
2065 sort_and_scan_merged_commits(ctx
);
2068 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
2071 time_t now
= time(NULL
);
2073 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
2075 struct utimbuf updated_time
;
2077 stat(ctx
->commit_graph_filenames_before
[i
], &st
);
2079 updated_time
.actime
= st
.st_atime
;
2080 updated_time
.modtime
= now
;
2081 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
2085 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
2087 struct strbuf path
= STRBUF_INIT
;
2091 timestamp_t expire_time
= time(NULL
);
2093 if (ctx
->opts
&& ctx
->opts
->expire_time
)
2094 expire_time
= ctx
->opts
->expire_time
;
2096 char *chain_file_name
= get_commit_graph_chain_filename(ctx
->odb
);
2097 unlink(chain_file_name
);
2098 free(chain_file_name
);
2099 ctx
->num_commit_graphs_after
= 0;
2102 strbuf_addstr(&path
, ctx
->odb
->path
);
2103 strbuf_addstr(&path
, "/info/commit-graphs");
2104 dir
= opendir(path
.buf
);
2109 strbuf_addch(&path
, '/');
2110 dirnamelen
= path
.len
;
2111 while ((de
= readdir(dir
)) != NULL
) {
2113 uint32_t i
, found
= 0;
2115 strbuf_setlen(&path
, dirnamelen
);
2116 strbuf_addstr(&path
, de
->d_name
);
2118 stat(path
.buf
, &st
);
2120 if (st
.st_mtime
> expire_time
)
2122 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
2125 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2126 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
2138 strbuf_release(&path
);
2141 int write_commit_graph(struct object_directory
*odb
,
2142 struct string_list
*pack_indexes
,
2143 struct oidset
*commits
,
2144 enum commit_graph_write_flags flags
,
2145 const struct commit_graph_opts
*opts
)
2147 struct write_commit_graph_context
*ctx
;
2148 uint32_t i
, count_distinct
= 0;
2151 struct bloom_filter_settings bloom_settings
= DEFAULT_BLOOM_FILTER_SETTINGS
;
2153 if (!commit_graph_compatible(the_repository
))
2156 ctx
= xcalloc(1, sizeof(struct write_commit_graph_context
));
2157 ctx
->r
= the_repository
;
2159 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
2160 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
2161 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
2163 ctx
->total_bloom_filter_data_size
= 0;
2165 bloom_settings
.bits_per_entry
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2166 bloom_settings
.bits_per_entry
);
2167 bloom_settings
.num_hashes
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2168 bloom_settings
.num_hashes
);
2169 bloom_settings
.max_changed_paths
= git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2170 bloom_settings
.max_changed_paths
);
2171 ctx
->bloom_settings
= &bloom_settings
;
2173 if (flags
& COMMIT_GRAPH_WRITE_BLOOM_FILTERS
)
2174 ctx
->changed_paths
= 1;
2175 if (!(flags
& COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS
)) {
2176 struct commit_graph
*g
;
2177 prepare_commit_graph_one(ctx
->r
, ctx
->odb
);
2179 g
= ctx
->r
->objects
->commit_graph
;
2181 /* We have changed-paths already. Keep them in the next graph */
2182 if (g
&& g
->chunk_bloom_data
) {
2183 ctx
->changed_paths
= 1;
2184 ctx
->bloom_settings
= g
->bloom_filter_settings
;
2189 struct commit_graph
*g
;
2190 prepare_commit_graph(ctx
->r
);
2192 g
= ctx
->r
->objects
->commit_graph
;
2195 ctx
->num_commit_graphs_before
++;
2199 if (ctx
->num_commit_graphs_before
) {
2200 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
2201 i
= ctx
->num_commit_graphs_before
;
2202 g
= ctx
->r
->objects
->commit_graph
;
2205 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
2211 replace
= ctx
->opts
->split_flags
& COMMIT_GRAPH_SPLIT_REPLACE
;
2214 ctx
->approx_nr_objects
= approximate_object_count();
2215 ctx
->oids
.alloc
= ctx
->approx_nr_objects
/ 32;
2217 if (ctx
->split
&& opts
&& ctx
->oids
.alloc
> opts
->max_commits
)
2218 ctx
->oids
.alloc
= opts
->max_commits
;
2221 prepare_commit_graph_one(ctx
->r
, ctx
->odb
);
2222 if (ctx
->r
->objects
->commit_graph
)
2223 ctx
->oids
.alloc
+= ctx
->r
->objects
->commit_graph
->num_commits
;
2226 if (ctx
->oids
.alloc
< 1024)
2227 ctx
->oids
.alloc
= 1024;
2228 ALLOC_ARRAY(ctx
->oids
.list
, ctx
->oids
.alloc
);
2230 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
2231 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
2232 for (i
= 0; i
< g
->num_commits
; i
++) {
2233 const unsigned char *hash
= g
->chunk_oid_lookup
+ g
->hash_len
* i
;
2234 hashcpy(ctx
->oids
.list
[ctx
->oids
.nr
++].hash
, hash
);
2239 ctx
->order_by_pack
= 1;
2240 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
2245 if ((res
= fill_oids_from_commits(ctx
, commits
)))
2249 if (!pack_indexes
&& !commits
) {
2250 ctx
->order_by_pack
= 1;
2251 fill_oids_from_all_packs(ctx
);
2254 close_reachable(ctx
);
2256 count_distinct
= count_distinct_commits(ctx
);
2258 if (count_distinct
>= GRAPH_EDGE_LAST_MASK
) {
2259 error(_("the commit graph format cannot write %d commits"), count_distinct
);
2264 ctx
->commits
.alloc
= count_distinct
;
2265 ALLOC_ARRAY(ctx
->commits
.list
, ctx
->commits
.alloc
);
2267 copy_oids_to_commits(ctx
);
2269 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
2270 error(_("too many commits to write graph"));
2275 if (!ctx
->commits
.nr
&& !replace
)
2279 split_graph_merge_strategy(ctx
);
2282 merge_commit_graphs(ctx
);
2284 ctx
->num_commit_graphs_after
= 1;
2286 compute_generation_numbers(ctx
);
2288 if (ctx
->changed_paths
)
2289 compute_bloom_filters(ctx
);
2291 res
= write_commit_graph_file(ctx
);
2294 mark_commit_graphs(ctx
);
2296 expire_commit_graphs(ctx
);
2299 free(ctx
->graph_name
);
2300 free(ctx
->commits
.list
);
2301 free(ctx
->oids
.list
);
2303 if (ctx
->commit_graph_filenames_after
) {
2304 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
2305 free(ctx
->commit_graph_filenames_after
[i
]);
2306 free(ctx
->commit_graph_hash_after
[i
]);
2309 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
2310 free(ctx
->commit_graph_filenames_before
[i
]);
2312 free(ctx
->commit_graph_filenames_after
);
2313 free(ctx
->commit_graph_filenames_before
);
2314 free(ctx
->commit_graph_hash_after
);
2322 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2323 static int verify_commit_graph_error
;
2325 static void graph_report(const char *fmt
, ...)
2329 verify_commit_graph_error
= 1;
2331 vfprintf(stderr
, fmt
, ap
);
2332 fprintf(stderr
, "\n");
2336 #define GENERATION_ZERO_EXISTS 1
2337 #define GENERATION_NUMBER_EXISTS 2
2339 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
2341 uint32_t i
, cur_fanout_pos
= 0;
2342 struct object_id prev_oid
, cur_oid
, checksum
;
2343 int generation_zero
= 0;
2346 struct progress
*progress
= NULL
;
2347 int local_error
= 0;
2350 graph_report("no commit-graph file loaded");
2354 verify_commit_graph_error
= verify_commit_graph_lite(g
);
2355 if (verify_commit_graph_error
)
2356 return verify_commit_graph_error
;
2358 devnull
= open("/dev/null", O_WRONLY
);
2359 f
= hashfd(devnull
, NULL
);
2360 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
2361 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
2362 if (!hasheq(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
2363 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2364 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
2367 for (i
= 0; i
< g
->num_commits
; i
++) {
2368 struct commit
*graph_commit
;
2370 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2372 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
2373 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
2374 oid_to_hex(&prev_oid
),
2375 oid_to_hex(&cur_oid
));
2377 oidcpy(&prev_oid
, &cur_oid
);
2379 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
2380 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2382 if (i
!= fanout_value
)
2383 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2384 cur_fanout_pos
, fanout_value
, i
);
2388 graph_commit
= lookup_commit(r
, &cur_oid
);
2389 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
2390 graph_report(_("failed to parse commit %s from commit-graph"),
2391 oid_to_hex(&cur_oid
));
2394 while (cur_fanout_pos
< 256) {
2395 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
2397 if (g
->num_commits
!= fanout_value
)
2398 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
2399 cur_fanout_pos
, fanout_value
, i
);
2404 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2405 return verify_commit_graph_error
;
2407 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2408 progress
= start_progress(_("Verifying commits in commit graph"),
2411 for (i
= 0; i
< g
->num_commits
; i
++) {
2412 struct commit
*graph_commit
, *odb_commit
;
2413 struct commit_list
*graph_parents
, *odb_parents
;
2414 uint32_t max_generation
= 0;
2415 uint32_t generation
;
2417 display_progress(progress
, i
+ 1);
2418 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2420 graph_commit
= lookup_commit(r
, &cur_oid
);
2421 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2422 if (parse_commit_internal(odb_commit
, 0, 0)) {
2423 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2424 oid_to_hex(&cur_oid
));
2428 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2429 get_commit_tree_oid(odb_commit
)))
2430 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2431 oid_to_hex(&cur_oid
),
2432 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2433 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2435 graph_parents
= graph_commit
->parents
;
2436 odb_parents
= odb_commit
->parents
;
2438 while (graph_parents
) {
2439 if (odb_parents
== NULL
) {
2440 graph_report(_("commit-graph parent list for commit %s is too long"),
2441 oid_to_hex(&cur_oid
));
2445 /* parse parent in case it is in a base graph */
2446 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2448 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2449 graph_report(_("commit-graph parent for %s is %s != %s"),
2450 oid_to_hex(&cur_oid
),
2451 oid_to_hex(&graph_parents
->item
->object
.oid
),
2452 oid_to_hex(&odb_parents
->item
->object
.oid
));
2454 generation
= commit_graph_generation(graph_parents
->item
);
2455 if (generation
> max_generation
)
2456 max_generation
= generation
;
2458 graph_parents
= graph_parents
->next
;
2459 odb_parents
= odb_parents
->next
;
2462 if (odb_parents
!= NULL
)
2463 graph_report(_("commit-graph parent list for commit %s terminates early"),
2464 oid_to_hex(&cur_oid
));
2466 if (!commit_graph_generation(graph_commit
)) {
2467 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2468 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2469 oid_to_hex(&cur_oid
));
2470 generation_zero
= GENERATION_ZERO_EXISTS
;
2471 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2472 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2473 oid_to_hex(&cur_oid
));
2475 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2479 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2480 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2481 * extra logic in the following condition.
2483 if (max_generation
== GENERATION_NUMBER_MAX
)
2486 generation
= commit_graph_generation(graph_commit
);
2487 if (generation
!= max_generation
+ 1)
2488 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2489 oid_to_hex(&cur_oid
),
2491 max_generation
+ 1);
2493 if (graph_commit
->date
!= odb_commit
->date
)
2494 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2495 oid_to_hex(&cur_oid
),
2499 stop_progress(&progress
);
2501 local_error
= verify_commit_graph_error
;
2503 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2504 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2509 void free_commit_graph(struct commit_graph
*g
)
2514 munmap((void *)g
->data
, g
->data_len
);
2518 free(g
->bloom_filter_settings
);
2522 void disable_commit_graph(struct repository
*r
)
2524 r
->commit_graph_disabled
= 1;