4 #include "git-compat-util.h"
12 #include "sha1-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
17 #include "replace-object.h"
20 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
21 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
22 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
23 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
24 #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
25 #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
27 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
29 #define GRAPH_VERSION_1 0x1
30 #define GRAPH_VERSION GRAPH_VERSION_1
32 #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
33 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
34 #define GRAPH_PARENT_NONE 0x70000000
36 #define GRAPH_LAST_EDGE 0x80000000
38 #define GRAPH_HEADER_SIZE 8
39 #define GRAPH_FANOUT_SIZE (4 * 256)
40 #define GRAPH_CHUNKLOOKUP_WIDTH 12
41 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
42 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
44 /* Remember to update object flag allocation in object.h */
45 #define REACHABLE (1u<<15)
47 char *get_commit_graph_filename(const char *obj_dir
)
49 char *filename
= xstrfmt("%s/info/commit-graph", obj_dir
);
50 char *normalized
= xmalloc(strlen(filename
) + 1);
51 normalize_path_copy(normalized
, filename
);
56 static char *get_split_graph_filename(const char *obj_dir
,
59 char *filename
= xstrfmt("%s/info/commit-graphs/graph-%s.graph",
62 char *normalized
= xmalloc(strlen(filename
) + 1);
63 normalize_path_copy(normalized
, filename
);
68 static char *get_chain_filename(const char *obj_dir
)
70 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir
);
73 static uint8_t oid_version(void)
78 static struct commit_graph
*alloc_commit_graph(void)
80 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
86 extern int read_replace_refs
;
88 static int commit_graph_compatible(struct repository
*r
)
93 if (read_replace_refs
) {
94 prepare_replace_object(r
);
95 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
99 prepare_commit_graft(r
);
100 if (r
->parsed_objects
&& r
->parsed_objects
->grafts_nr
)
102 if (is_repository_shallow(r
))
108 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
110 *fd
= git_open(graph_file
);
113 if (fstat(*fd
, st
)) {
120 struct commit_graph
*load_commit_graph_one_fd_st(int fd
, struct stat
*st
)
124 struct commit_graph
*ret
;
126 graph_size
= xsize_t(st
->st_size
);
128 if (graph_size
< GRAPH_MIN_SIZE
) {
130 error(_("commit-graph file is too small"));
133 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
134 ret
= parse_commit_graph(graph_map
, fd
, graph_size
);
137 munmap(graph_map
, graph_size
);
144 static int verify_commit_graph_lite(struct commit_graph
*g
)
147 * Basic validation shared between parse_commit_graph()
148 * which'll be called every time the graph is used, and the
149 * much more expensive verify_commit_graph() used by
150 * "commit-graph verify".
152 * There should only be very basic checks here to ensure that
153 * we don't e.g. segfault in fill_commit_in_graph(), but
154 * because this is a very hot codepath nothing that e.g. loops
155 * over g->num_commits, or runs a checksum on the commit-graph
158 if (!g
->chunk_oid_fanout
) {
159 error("commit-graph is missing the OID Fanout chunk");
162 if (!g
->chunk_oid_lookup
) {
163 error("commit-graph is missing the OID Lookup chunk");
166 if (!g
->chunk_commit_data
) {
167 error("commit-graph is missing the Commit Data chunk");
174 struct commit_graph
*parse_commit_graph(void *graph_map
, int fd
,
177 const unsigned char *data
, *chunk_lookup
;
179 struct commit_graph
*graph
;
180 uint64_t last_chunk_offset
;
181 uint32_t last_chunk_id
;
182 uint32_t graph_signature
;
183 unsigned char graph_version
, hash_version
;
188 if (graph_size
< GRAPH_MIN_SIZE
)
191 data
= (const unsigned char *)graph_map
;
193 graph_signature
= get_be32(data
);
194 if (graph_signature
!= GRAPH_SIGNATURE
) {
195 error(_("commit-graph signature %X does not match signature %X"),
196 graph_signature
, GRAPH_SIGNATURE
);
200 graph_version
= *(unsigned char*)(data
+ 4);
201 if (graph_version
!= GRAPH_VERSION
) {
202 error(_("commit-graph version %X does not match version %X"),
203 graph_version
, GRAPH_VERSION
);
207 hash_version
= *(unsigned char*)(data
+ 5);
208 if (hash_version
!= oid_version()) {
209 error(_("commit-graph hash version %X does not match version %X"),
210 hash_version
, oid_version());
214 graph
= alloc_commit_graph();
216 graph
->hash_len
= the_hash_algo
->rawsz
;
217 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
218 graph
->graph_fd
= fd
;
219 graph
->data
= graph_map
;
220 graph
->data_len
= graph_size
;
223 last_chunk_offset
= 8;
224 chunk_lookup
= data
+ 8;
225 for (i
= 0; i
< graph
->num_chunks
; i
++) {
227 uint64_t chunk_offset
;
228 int chunk_repeated
= 0;
230 if (data
+ graph_size
- chunk_lookup
<
231 GRAPH_CHUNKLOOKUP_WIDTH
) {
232 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
237 chunk_id
= get_be32(chunk_lookup
+ 0);
238 chunk_offset
= get_be64(chunk_lookup
+ 4);
240 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
242 if (chunk_offset
> graph_size
- the_hash_algo
->rawsz
) {
243 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset
>> 32),
244 (uint32_t)chunk_offset
);
250 case GRAPH_CHUNKID_OIDFANOUT
:
251 if (graph
->chunk_oid_fanout
)
254 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
257 case GRAPH_CHUNKID_OIDLOOKUP
:
258 if (graph
->chunk_oid_lookup
)
261 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
264 case GRAPH_CHUNKID_DATA
:
265 if (graph
->chunk_commit_data
)
268 graph
->chunk_commit_data
= data
+ chunk_offset
;
271 case GRAPH_CHUNKID_EXTRAEDGES
:
272 if (graph
->chunk_extra_edges
)
275 graph
->chunk_extra_edges
= data
+ chunk_offset
;
278 case GRAPH_CHUNKID_BASE
:
279 if (graph
->chunk_base_graphs
)
282 graph
->chunk_base_graphs
= data
+ chunk_offset
;
285 if (chunk_repeated
) {
286 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id
);
291 if (last_chunk_id
== GRAPH_CHUNKID_OIDLOOKUP
)
293 graph
->num_commits
= (chunk_offset
- last_chunk_offset
)
297 last_chunk_id
= chunk_id
;
298 last_chunk_offset
= chunk_offset
;
301 hashcpy(graph
->oid
.hash
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
303 if (verify_commit_graph_lite(graph
)) {
311 static struct commit_graph
*load_commit_graph_one(const char *graph_file
)
316 struct commit_graph
*g
;
317 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
322 g
= load_commit_graph_one_fd_st(fd
, &st
);
325 g
->filename
= xstrdup(graph_file
);
330 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
, const char *obj_dir
)
332 char *graph_name
= get_commit_graph_filename(obj_dir
);
333 struct commit_graph
*g
= load_commit_graph_one(graph_name
);
337 g
->obj_dir
= obj_dir
;
342 static int add_graph_to_chain(struct commit_graph
*g
,
343 struct commit_graph
*chain
,
344 struct object_id
*oids
,
347 struct commit_graph
*cur_g
= chain
;
349 if (n
&& !g
->chunk_base_graphs
) {
350 warning(_("commit-graph has no base graphs chunk"));
358 !oideq(&oids
[n
], &cur_g
->oid
) ||
359 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
360 warning(_("commit-graph chain does not match"));
364 cur_g
= cur_g
->base_graph
;
367 g
->base_graph
= chain
;
370 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
375 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
, const char *obj_dir
)
377 struct commit_graph
*graph_chain
= NULL
;
378 struct strbuf line
= STRBUF_INIT
;
380 struct object_id
*oids
;
381 int i
= 0, valid
= 1, count
;
382 char *chain_name
= get_chain_filename(obj_dir
);
386 fp
= fopen(chain_name
, "r");
387 stat_res
= stat(chain_name
, &st
);
392 st
.st_size
<= the_hash_algo
->hexsz
)
395 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
396 oids
= xcalloc(count
, sizeof(struct object_id
));
400 for (i
= 0; i
< count
; i
++) {
401 struct object_directory
*odb
;
403 if (strbuf_getline_lf(&line
, fp
) == EOF
)
406 if (get_oid_hex(line
.buf
, &oids
[i
])) {
407 warning(_("invalid commit-graph chain: line '%s' not a hash"),
414 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
415 char *graph_name
= get_split_graph_filename(odb
->path
, line
.buf
);
416 struct commit_graph
*g
= load_commit_graph_one(graph_name
);
421 g
->obj_dir
= odb
->path
;
423 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
433 warning(_("unable to find all commit-graph files"));
440 strbuf_release(&line
);
445 struct commit_graph
*read_commit_graph_one(struct repository
*r
, const char *obj_dir
)
447 struct commit_graph
*g
= load_commit_graph_v1(r
, obj_dir
);
450 g
= load_commit_graph_chain(r
, obj_dir
);
455 static void prepare_commit_graph_one(struct repository
*r
, const char *obj_dir
)
458 if (r
->objects
->commit_graph
)
461 r
->objects
->commit_graph
= read_commit_graph_one(r
, obj_dir
);
465 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
467 * On the first invocation, this function attempts to load the commit
468 * graph if the_repository is configured to have one.
470 static int prepare_commit_graph(struct repository
*r
)
472 struct object_directory
*odb
;
475 * This must come before the "already attempted?" check below, because
476 * we want to disable even an already-loaded graph file.
478 if (r
->commit_graph_disabled
)
481 if (r
->objects
->commit_graph_attempted
)
482 return !!r
->objects
->commit_graph
;
483 r
->objects
->commit_graph_attempted
= 1;
485 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD
, 0))
486 die("dying as requested by the '%s' variable on commit-graph load!",
487 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD
);
489 prepare_repo_settings(r
);
491 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
492 r
->settings
.core_commit_graph
!= 1)
494 * This repository is not configured to use commit graphs, so
495 * do not load one. (But report commit_graph_attempted anyway
496 * so that commit graph loading is not attempted again for this
501 if (!commit_graph_compatible(r
))
505 for (odb
= r
->objects
->odb
;
506 !r
->objects
->commit_graph
&& odb
;
508 prepare_commit_graph_one(r
, odb
->path
);
509 return !!r
->objects
->commit_graph
;
512 int generation_numbers_enabled(struct repository
*r
)
514 uint32_t first_generation
;
515 struct commit_graph
*g
;
516 if (!prepare_commit_graph(r
))
519 g
= r
->objects
->commit_graph
;
524 first_generation
= get_be32(g
->chunk_commit_data
+
525 g
->hash_len
+ 8) >> 2;
527 return !!first_generation
;
530 static void close_commit_graph_one(struct commit_graph
*g
)
535 close_commit_graph_one(g
->base_graph
);
536 free_commit_graph(g
);
539 void close_commit_graph(struct raw_object_store
*o
)
541 close_commit_graph_one(o
->commit_graph
);
542 o
->commit_graph
= NULL
;
545 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
547 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
548 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
551 static void load_oid_from_graph(struct commit_graph
*g
,
553 struct object_id
*oid
)
557 while (g
&& pos
< g
->num_commits_in_base
)
561 BUG("NULL commit-graph");
563 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
564 die(_("invalid commit position. commit-graph is likely corrupt"));
566 lex_index
= pos
- g
->num_commits_in_base
;
568 hashcpy(oid
->hash
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
571 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
572 struct commit_graph
*g
,
574 struct commit_list
**pptr
)
577 struct object_id oid
;
579 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
580 die("invalid parent position %"PRIu32
, pos
);
582 load_oid_from_graph(g
, pos
, &oid
);
583 c
= lookup_commit(r
, &oid
);
585 die(_("could not find commit %s"), oid_to_hex(&oid
));
587 return &commit_list_insert(c
, pptr
)->next
;
590 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
592 const unsigned char *commit_data
;
595 while (pos
< g
->num_commits_in_base
)
598 lex_index
= pos
- g
->num_commits_in_base
;
599 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
600 item
->graph_pos
= pos
;
601 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
604 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
609 static int fill_commit_in_graph(struct repository
*r
,
611 struct commit_graph
*g
, uint32_t pos
)
614 uint32_t *parent_data_ptr
;
615 uint64_t date_low
, date_high
;
616 struct commit_list
**pptr
;
617 const unsigned char *commit_data
;
620 while (pos
< g
->num_commits_in_base
)
623 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
624 die(_("invalid commit position. commit-graph is likely corrupt"));
627 * Store the "full" position, but then use the
628 * "local" position for the rest of the calculation.
630 item
->graph_pos
= pos
;
631 lex_index
= pos
- g
->num_commits_in_base
;
633 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
635 item
->object
.parsed
= 1;
637 set_commit_tree(item
, NULL
);
639 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
640 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
641 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
643 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
645 pptr
= &item
->parents
;
647 edge_value
= get_be32(commit_data
+ g
->hash_len
);
648 if (edge_value
== GRAPH_PARENT_NONE
)
650 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
652 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
653 if (edge_value
== GRAPH_PARENT_NONE
)
655 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
656 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
660 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
661 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
663 edge_value
= get_be32(parent_data_ptr
);
664 pptr
= insert_parent_or_die(r
, g
,
665 edge_value
& GRAPH_EDGE_LAST_MASK
,
668 } while (!(edge_value
& GRAPH_LAST_EDGE
));
673 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
675 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
676 *pos
= item
->graph_pos
;
679 struct commit_graph
*cur_g
= g
;
682 while (cur_g
&& !bsearch_graph(cur_g
, &(item
->object
.oid
), &lex_index
))
683 cur_g
= cur_g
->base_graph
;
686 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
694 static int parse_commit_in_graph_one(struct repository
*r
,
695 struct commit_graph
*g
,
700 if (item
->object
.parsed
)
703 if (find_commit_in_graph(item
, g
, &pos
))
704 return fill_commit_in_graph(r
, item
, g
, pos
);
709 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
711 if (!prepare_commit_graph(r
))
713 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
716 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
719 if (!prepare_commit_graph(r
))
721 if (find_commit_in_graph(item
, r
->objects
->commit_graph
, &pos
))
722 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
725 static struct tree
*load_tree_for_commit(struct repository
*r
,
726 struct commit_graph
*g
,
729 struct object_id oid
;
730 const unsigned char *commit_data
;
732 while (c
->graph_pos
< g
->num_commits_in_base
)
735 commit_data
= g
->chunk_commit_data
+
736 GRAPH_DATA_WIDTH
* (c
->graph_pos
- g
->num_commits_in_base
);
738 hashcpy(oid
.hash
, commit_data
);
739 set_commit_tree(c
, lookup_tree(r
, &oid
));
741 return c
->maybe_tree
;
744 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
745 struct commit_graph
*g
,
746 const struct commit
*c
)
749 return c
->maybe_tree
;
750 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
751 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
753 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
756 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
758 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
761 struct packed_commit_list
{
762 struct commit
**list
;
767 struct packed_oid_list
{
768 struct object_id
*list
;
773 struct write_commit_graph_context
{
774 struct repository
*r
;
777 struct packed_oid_list oids
;
778 struct packed_commit_list commits
;
780 unsigned long approx_nr_objects
;
781 struct progress
*progress
;
783 uint64_t progress_cnt
;
785 char *base_graph_name
;
786 int num_commit_graphs_before
;
787 int num_commit_graphs_after
;
788 char **commit_graph_filenames_before
;
789 char **commit_graph_filenames_after
;
790 char **commit_graph_hash_after
;
791 uint32_t new_num_commits_in_base
;
792 struct commit_graph
*new_base_graph
;
799 const struct split_commit_graph_opts
*split_opts
;
802 static void write_graph_chunk_fanout(struct hashfile
*f
,
803 struct write_commit_graph_context
*ctx
)
806 struct commit
**list
= ctx
->commits
.list
;
809 * Write the first-level table (the list is sorted,
810 * but we use a 256-entry lookup to be able to avoid
811 * having to do eight extra binary search iterations).
813 for (i
= 0; i
< 256; i
++) {
814 while (count
< ctx
->commits
.nr
) {
815 if ((*list
)->object
.oid
.hash
[0] != i
)
817 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
822 hashwrite_be32(f
, count
);
826 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
827 struct write_commit_graph_context
*ctx
)
829 struct commit
**list
= ctx
->commits
.list
;
831 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
832 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
833 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
837 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
839 struct commit
**commits
= table
;
840 return commits
[index
]->object
.oid
.hash
;
843 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
844 struct write_commit_graph_context
*ctx
)
846 struct commit
**list
= ctx
->commits
.list
;
847 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
848 uint32_t num_extra_edges
= 0;
850 while (list
< last
) {
851 struct commit_list
*parent
;
852 struct object_id
*tree
;
854 uint32_t packedDate
[2];
855 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
857 if (parse_commit_no_graph(*list
))
858 die(_("unable to parse commit %s"),
859 oid_to_hex(&(*list
)->object
.oid
));
860 tree
= get_commit_tree_oid(*list
);
861 hashwrite(f
, tree
->hash
, hash_len
);
863 parent
= (*list
)->parents
;
866 edge_value
= GRAPH_PARENT_NONE
;
868 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
874 edge_value
+= ctx
->new_num_commits_in_base
;
877 if (find_commit_in_graph(parent
->item
,
884 BUG("missing parent %s for commit %s",
885 oid_to_hex(&parent
->item
->object
.oid
),
886 oid_to_hex(&(*list
)->object
.oid
));
889 hashwrite_be32(f
, edge_value
);
892 parent
= parent
->next
;
895 edge_value
= GRAPH_PARENT_NONE
;
896 else if (parent
->next
)
897 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
899 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
905 edge_value
+= ctx
->new_num_commits_in_base
;
908 if (find_commit_in_graph(parent
->item
,
915 BUG("missing parent %s for commit %s",
916 oid_to_hex(&parent
->item
->object
.oid
),
917 oid_to_hex(&(*list
)->object
.oid
));
920 hashwrite_be32(f
, edge_value
);
922 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
925 parent
= parent
->next
;
929 if (sizeof((*list
)->date
) > 4)
930 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
934 packedDate
[0] |= htonl((*list
)->generation
<< 2);
936 packedDate
[1] = htonl((*list
)->date
);
937 hashwrite(f
, packedDate
, 8);
943 static void write_graph_chunk_extra_edges(struct hashfile
*f
,
944 struct write_commit_graph_context
*ctx
)
946 struct commit
**list
= ctx
->commits
.list
;
947 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
948 struct commit_list
*parent
;
950 while (list
< last
) {
953 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
955 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
956 parent
= parent
->next
)
959 if (num_parents
<= 2) {
964 /* Since num_parents > 2, this initializer is safe. */
965 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
966 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
972 edge_value
+= ctx
->new_num_commits_in_base
;
975 if (find_commit_in_graph(parent
->item
,
982 BUG("missing parent %s for commit %s",
983 oid_to_hex(&parent
->item
->object
.oid
),
984 oid_to_hex(&(*list
)->object
.oid
));
985 else if (!parent
->next
)
986 edge_value
|= GRAPH_LAST_EDGE
;
988 hashwrite_be32(f
, edge_value
);
995 static int oid_compare(const void *_a
, const void *_b
)
997 const struct object_id
*a
= (const struct object_id
*)_a
;
998 const struct object_id
*b
= (const struct object_id
*)_b
;
1002 static int add_packed_commits(const struct object_id
*oid
,
1003 struct packed_git
*pack
,
1007 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1008 enum object_type type
;
1009 off_t offset
= nth_packed_object_offset(pack
, pos
);
1010 struct object_info oi
= OBJECT_INFO_INIT
;
1013 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1016 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1017 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1019 if (type
!= OBJ_COMMIT
)
1022 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1023 oidcpy(&(ctx
->oids
.list
[ctx
->oids
.nr
]), oid
);
1029 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1031 struct commit_list
*parent
;
1032 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1033 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1034 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1035 oidcpy(&ctx
->oids
.list
[ctx
->oids
.nr
], &(parent
->item
->object
.oid
));
1037 parent
->item
->object
.flags
|= REACHABLE
;
1042 static void close_reachable(struct write_commit_graph_context
*ctx
)
1045 struct commit
*commit
;
1047 if (ctx
->report_progress
)
1048 ctx
->progress
= start_delayed_progress(
1049 _("Loading known commits in commit graph"),
1051 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1052 display_progress(ctx
->progress
, i
+ 1);
1053 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1055 commit
->object
.flags
|= REACHABLE
;
1057 stop_progress(&ctx
->progress
);
1060 * As this loop runs, ctx->oids.nr may grow, but not more
1061 * than the number of missing commits in the reachable
1064 if (ctx
->report_progress
)
1065 ctx
->progress
= start_delayed_progress(
1066 _("Expanding reachable commits in commit graph"),
1068 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1069 display_progress(ctx
->progress
, i
+ 1);
1070 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1075 if (!parse_commit(commit
) &&
1076 commit
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
1077 add_missing_parents(ctx
, commit
);
1078 } else if (!parse_commit_no_graph(commit
))
1079 add_missing_parents(ctx
, commit
);
1081 stop_progress(&ctx
->progress
);
1083 if (ctx
->report_progress
)
1084 ctx
->progress
= start_delayed_progress(
1085 _("Clearing commit marks in commit graph"),
1087 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1088 display_progress(ctx
->progress
, i
+ 1);
1089 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1092 commit
->object
.flags
&= ~REACHABLE
;
1094 stop_progress(&ctx
->progress
);
1097 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1100 struct commit_list
*list
= NULL
;
1102 if (ctx
->report_progress
)
1103 ctx
->progress
= start_delayed_progress(
1104 _("Computing commit graph generation numbers"),
1106 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1107 display_progress(ctx
->progress
, i
+ 1);
1108 if (ctx
->commits
.list
[i
]->generation
!= GENERATION_NUMBER_INFINITY
&&
1109 ctx
->commits
.list
[i
]->generation
!= GENERATION_NUMBER_ZERO
)
1112 commit_list_insert(ctx
->commits
.list
[i
], &list
);
1114 struct commit
*current
= list
->item
;
1115 struct commit_list
*parent
;
1116 int all_parents_computed
= 1;
1117 uint32_t max_generation
= 0;
1119 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1120 if (parent
->item
->generation
== GENERATION_NUMBER_INFINITY
||
1121 parent
->item
->generation
== GENERATION_NUMBER_ZERO
) {
1122 all_parents_computed
= 0;
1123 commit_list_insert(parent
->item
, &list
);
1125 } else if (parent
->item
->generation
> max_generation
) {
1126 max_generation
= parent
->item
->generation
;
1130 if (all_parents_computed
) {
1131 current
->generation
= max_generation
+ 1;
1134 if (current
->generation
> GENERATION_NUMBER_MAX
)
1135 current
->generation
= GENERATION_NUMBER_MAX
;
1139 stop_progress(&ctx
->progress
);
1142 static int add_ref_to_list(const char *refname
,
1143 const struct object_id
*oid
,
1144 int flags
, void *cb_data
)
1146 struct string_list
*list
= (struct string_list
*)cb_data
;
1148 string_list_append(list
, oid_to_hex(oid
));
1152 int write_commit_graph_reachable(const char *obj_dir
,
1153 enum commit_graph_write_flags flags
,
1154 const struct split_commit_graph_opts
*split_opts
)
1156 struct string_list list
= STRING_LIST_INIT_DUP
;
1159 for_each_ref(add_ref_to_list
, &list
);
1160 result
= write_commit_graph(obj_dir
, NULL
, &list
,
1163 string_list_clear(&list
, 0);
1167 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1168 struct string_list
*pack_indexes
)
1171 struct strbuf progress_title
= STRBUF_INIT
;
1172 struct strbuf packname
= STRBUF_INIT
;
1175 strbuf_addf(&packname
, "%s/pack/", ctx
->obj_dir
);
1176 dirlen
= packname
.len
;
1177 if (ctx
->report_progress
) {
1178 strbuf_addf(&progress_title
,
1179 Q_("Finding commits for commit graph in %d pack",
1180 "Finding commits for commit graph in %d packs",
1183 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1184 ctx
->progress_done
= 0;
1186 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1187 struct packed_git
*p
;
1188 strbuf_setlen(&packname
, dirlen
);
1189 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1190 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1192 error(_("error adding pack %s"), packname
.buf
);
1195 if (open_pack_index(p
)) {
1196 error(_("error opening index for %s"), packname
.buf
);
1199 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1200 FOR_EACH_OBJECT_PACK_ORDER
);
1205 stop_progress(&ctx
->progress
);
1206 strbuf_release(&progress_title
);
1207 strbuf_release(&packname
);
1212 static int fill_oids_from_commit_hex(struct write_commit_graph_context
*ctx
,
1213 struct string_list
*commit_hex
)
1216 struct strbuf progress_title
= STRBUF_INIT
;
1218 if (ctx
->report_progress
) {
1219 strbuf_addf(&progress_title
,
1220 Q_("Finding commits for commit graph from %d ref",
1221 "Finding commits for commit graph from %d refs",
1224 ctx
->progress
= start_delayed_progress(
1228 for (i
= 0; i
< commit_hex
->nr
; i
++) {
1230 struct object_id oid
;
1231 struct commit
*result
;
1233 display_progress(ctx
->progress
, i
+ 1);
1234 if (!parse_oid_hex(commit_hex
->items
[i
].string
, &oid
, &end
) &&
1235 (result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1))) {
1236 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1237 oidcpy(&ctx
->oids
.list
[ctx
->oids
.nr
], &(result
->object
.oid
));
1239 } else if (ctx
->check_oids
) {
1240 error(_("invalid commit object id: %s"),
1241 commit_hex
->items
[i
].string
);
1245 stop_progress(&ctx
->progress
);
1246 strbuf_release(&progress_title
);
1251 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1253 if (ctx
->report_progress
)
1254 ctx
->progress
= start_delayed_progress(
1255 _("Finding commits for commit graph among packed objects"),
1256 ctx
->approx_nr_objects
);
1257 for_each_packed_object(add_packed_commits
, ctx
,
1258 FOR_EACH_OBJECT_PACK_ORDER
);
1259 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1260 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1261 stop_progress(&ctx
->progress
);
1264 static uint32_t count_distinct_commits(struct write_commit_graph_context
*ctx
)
1266 uint32_t i
, count_distinct
= 1;
1268 if (ctx
->report_progress
)
1269 ctx
->progress
= start_delayed_progress(
1270 _("Counting distinct commits in commit graph"),
1272 display_progress(ctx
->progress
, 0); /* TODO: Measure QSORT() progress */
1273 QSORT(ctx
->oids
.list
, ctx
->oids
.nr
, oid_compare
);
1275 for (i
= 1; i
< ctx
->oids
.nr
; i
++) {
1276 display_progress(ctx
->progress
, i
+ 1);
1277 if (!oideq(&ctx
->oids
.list
[i
- 1], &ctx
->oids
.list
[i
])) {
1279 struct commit
*c
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1281 if (!c
|| c
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
)
1288 stop_progress(&ctx
->progress
);
1290 return count_distinct
;
1293 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1297 ctx
->num_extra_edges
= 0;
1298 if (ctx
->report_progress
)
1299 ctx
->progress
= start_delayed_progress(
1300 _("Finding extra edges in commit graph"),
1302 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1303 unsigned int num_parents
;
1305 display_progress(ctx
->progress
, i
+ 1);
1306 if (i
> 0 && oideq(&ctx
->oids
.list
[i
- 1], &ctx
->oids
.list
[i
]))
1309 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1310 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1313 ctx
->commits
.list
[ctx
->commits
.nr
]->graph_pos
!= COMMIT_NOT_FROM_GRAPH
)
1316 parse_commit_no_graph(ctx
->commits
.list
[ctx
->commits
.nr
]);
1318 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1319 if (num_parents
> 2)
1320 ctx
->num_extra_edges
+= num_parents
- 1;
1324 stop_progress(&ctx
->progress
);
1327 static int write_graph_chunk_base_1(struct hashfile
*f
,
1328 struct commit_graph
*g
)
1335 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1336 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1340 static int write_graph_chunk_base(struct hashfile
*f
,
1341 struct write_commit_graph_context
*ctx
)
1343 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1345 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1346 error(_("failed to write correct number of base graph ids"));
1353 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1358 struct lock_file lk
= LOCK_INIT
;
1359 uint32_t chunk_ids
[6];
1360 uint64_t chunk_offsets
[6];
1361 const unsigned hashsz
= the_hash_algo
->rawsz
;
1362 struct strbuf progress_title
= STRBUF_INIT
;
1364 struct object_id file_hash
;
1367 struct strbuf tmp_file
= STRBUF_INIT
;
1369 strbuf_addf(&tmp_file
,
1370 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1372 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1374 ctx
->graph_name
= get_commit_graph_filename(ctx
->obj_dir
);
1377 if (safe_create_leading_directories(ctx
->graph_name
)) {
1378 UNLEAK(ctx
->graph_name
);
1379 error(_("unable to create leading directories of %s"),
1385 char *lock_name
= get_chain_filename(ctx
->obj_dir
);
1387 hold_lock_file_for_update(&lk
, lock_name
, LOCK_DIE_ON_ERROR
);
1389 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1391 error(_("unable to create '%s'"), ctx
->graph_name
);
1395 f
= hashfd(fd
, ctx
->graph_name
);
1397 hold_lock_file_for_update(&lk
, ctx
->graph_name
, LOCK_DIE_ON_ERROR
);
1398 fd
= lk
.tempfile
->fd
;
1399 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
1402 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
1403 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
1404 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
1405 if (ctx
->num_extra_edges
) {
1406 chunk_ids
[num_chunks
] = GRAPH_CHUNKID_EXTRAEDGES
;
1409 if (ctx
->num_commit_graphs_after
> 1) {
1410 chunk_ids
[num_chunks
] = GRAPH_CHUNKID_BASE
;
1414 chunk_ids
[num_chunks
] = 0;
1416 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
1417 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
1418 chunk_offsets
[2] = chunk_offsets
[1] + hashsz
* ctx
->commits
.nr
;
1419 chunk_offsets
[3] = chunk_offsets
[2] + (hashsz
+ 16) * ctx
->commits
.nr
;
1422 if (ctx
->num_extra_edges
) {
1423 chunk_offsets
[num_chunks
+ 1] = chunk_offsets
[num_chunks
] +
1424 4 * ctx
->num_extra_edges
;
1427 if (ctx
->num_commit_graphs_after
> 1) {
1428 chunk_offsets
[num_chunks
+ 1] = chunk_offsets
[num_chunks
] +
1429 hashsz
* (ctx
->num_commit_graphs_after
- 1);
1433 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1435 hashwrite_u8(f
, GRAPH_VERSION
);
1436 hashwrite_u8(f
, oid_version());
1437 hashwrite_u8(f
, num_chunks
);
1438 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1440 for (i
= 0; i
<= num_chunks
; i
++) {
1441 uint32_t chunk_write
[3];
1443 chunk_write
[0] = htonl(chunk_ids
[i
]);
1444 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
1445 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
1446 hashwrite(f
, chunk_write
, 12);
1449 if (ctx
->report_progress
) {
1450 strbuf_addf(&progress_title
,
1451 Q_("Writing out commit graph in %d pass",
1452 "Writing out commit graph in %d passes",
1455 ctx
->progress
= start_delayed_progress(
1457 num_chunks
* ctx
->commits
.nr
);
1459 write_graph_chunk_fanout(f
, ctx
);
1460 write_graph_chunk_oids(f
, hashsz
, ctx
);
1461 write_graph_chunk_data(f
, hashsz
, ctx
);
1462 if (ctx
->num_extra_edges
)
1463 write_graph_chunk_extra_edges(f
, ctx
);
1464 if (ctx
->num_commit_graphs_after
> 1 &&
1465 write_graph_chunk_base(f
, ctx
)) {
1468 stop_progress(&ctx
->progress
);
1469 strbuf_release(&progress_title
);
1471 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1472 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1473 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->obj_dir
, new_base_hash
);
1475 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1476 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1477 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1478 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1481 close_commit_graph(ctx
->r
->objects
);
1482 finalize_hashfile(f
, file_hash
.hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1485 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1486 char *final_graph_name
;
1492 error(_("unable to open commit-graph chain file"));
1496 if (ctx
->base_graph_name
) {
1497 const char *dest
= ctx
->commit_graph_filenames_after
[
1498 ctx
->num_commit_graphs_after
- 2];
1500 if (strcmp(ctx
->base_graph_name
, dest
)) {
1501 result
= rename(ctx
->base_graph_name
, dest
);
1504 error(_("failed to rename base commit-graph file"));
1509 char *graph_name
= get_commit_graph_filename(ctx
->obj_dir
);
1513 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(oid_to_hex(&file_hash
));
1514 final_graph_name
= get_split_graph_filename(ctx
->obj_dir
,
1515 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1516 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
1518 result
= rename(ctx
->graph_name
, final_graph_name
);
1520 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
1521 fprintf(lk
.tempfile
->fp
, "%s\n", ctx
->commit_graph_hash_after
[i
]);
1524 error(_("failed to rename temporary commit-graph file"));
1529 commit_lock_file(&lk
);
1534 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
1536 struct commit_graph
*g
;
1537 uint32_t num_commits
;
1540 int max_commits
= 0;
1543 if (ctx
->split_opts
) {
1544 max_commits
= ctx
->split_opts
->max_commits
;
1546 if (ctx
->split_opts
->size_multiple
)
1547 size_mult
= ctx
->split_opts
->size_multiple
;
1550 g
= ctx
->r
->objects
->commit_graph
;
1551 num_commits
= ctx
->commits
.nr
;
1552 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
1554 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
1555 (max_commits
&& num_commits
> max_commits
))) {
1556 if (strcmp(g
->obj_dir
, ctx
->obj_dir
))
1559 num_commits
+= g
->num_commits
;
1562 ctx
->num_commit_graphs_after
--;
1565 ctx
->new_base_graph
= g
;
1567 if (ctx
->num_commit_graphs_after
== 2) {
1568 char *old_graph_name
= get_commit_graph_filename(g
->obj_dir
);
1570 if (!strcmp(g
->filename
, old_graph_name
) &&
1571 strcmp(g
->obj_dir
, ctx
->obj_dir
)) {
1572 ctx
->num_commit_graphs_after
= 1;
1573 ctx
->new_base_graph
= NULL
;
1576 free(old_graph_name
);
1579 ALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
1580 ALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
1582 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
1583 i
< ctx
->num_commit_graphs_before
; i
++)
1584 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
1586 i
= ctx
->num_commit_graphs_before
- 1;
1587 g
= ctx
->r
->objects
->commit_graph
;
1590 if (i
< ctx
->num_commit_graphs_after
)
1591 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
1598 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
1599 struct commit_graph
*g
)
1602 uint32_t offset
= g
->num_commits_in_base
;
1604 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
1606 for (i
= 0; i
< g
->num_commits
; i
++) {
1607 struct object_id oid
;
1608 struct commit
*result
;
1610 display_progress(ctx
->progress
, i
+ 1);
1612 load_oid_from_graph(g
, i
+ offset
, &oid
);
1614 /* only add commits if they still exist in the repo */
1615 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
1618 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
1624 static int commit_compare(const void *_a
, const void *_b
)
1626 const struct commit
*a
= *(const struct commit
**)_a
;
1627 const struct commit
*b
= *(const struct commit
**)_b
;
1628 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
1631 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
1635 if (ctx
->report_progress
)
1636 ctx
->progress
= start_delayed_progress(
1637 _("Scanning merged commits"),
1640 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
1642 ctx
->num_extra_edges
= 0;
1643 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1644 display_progress(ctx
->progress
, i
);
1646 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
1647 &ctx
->commits
.list
[i
]->object
.oid
)) {
1648 die(_("unexpected duplicate commit id %s"),
1649 oid_to_hex(&ctx
->commits
.list
[i
]->object
.oid
));
1651 unsigned int num_parents
;
1653 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
1654 if (num_parents
> 2)
1655 ctx
->num_extra_edges
+= num_parents
- 1;
1659 stop_progress(&ctx
->progress
);
1662 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
1664 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
1665 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
1666 struct strbuf progress_title
= STRBUF_INIT
;
1668 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
1669 current_graph_number
--;
1671 if (ctx
->report_progress
) {
1672 strbuf_addstr(&progress_title
, _("Merging commit-graph"));
1673 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1676 merge_commit_graph(ctx
, g
);
1677 stop_progress(&ctx
->progress
);
1678 strbuf_release(&progress_title
);
1684 ctx
->new_base_graph
= g
;
1685 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
1688 if (ctx
->new_base_graph
)
1689 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
1691 sort_and_scan_merged_commits(ctx
);
1694 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
1697 time_t now
= time(NULL
);
1699 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
1701 struct utimbuf updated_time
;
1703 stat(ctx
->commit_graph_filenames_before
[i
], &st
);
1705 updated_time
.actime
= st
.st_atime
;
1706 updated_time
.modtime
= now
;
1707 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
1711 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
1713 struct strbuf path
= STRBUF_INIT
;
1717 timestamp_t expire_time
= time(NULL
);
1719 if (ctx
->split_opts
&& ctx
->split_opts
->expire_time
)
1720 expire_time
-= ctx
->split_opts
->expire_time
;
1722 char *chain_file_name
= get_chain_filename(ctx
->obj_dir
);
1723 unlink(chain_file_name
);
1724 free(chain_file_name
);
1725 ctx
->num_commit_graphs_after
= 0;
1728 strbuf_addstr(&path
, ctx
->obj_dir
);
1729 strbuf_addstr(&path
, "/info/commit-graphs");
1730 dir
= opendir(path
.buf
);
1735 strbuf_addch(&path
, '/');
1736 dirnamelen
= path
.len
;
1737 while ((de
= readdir(dir
)) != NULL
) {
1739 uint32_t i
, found
= 0;
1741 strbuf_setlen(&path
, dirnamelen
);
1742 strbuf_addstr(&path
, de
->d_name
);
1744 stat(path
.buf
, &st
);
1746 if (st
.st_mtime
> expire_time
)
1748 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
1751 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
1752 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
1764 strbuf_release(&path
);
1767 int write_commit_graph(const char *obj_dir
,
1768 struct string_list
*pack_indexes
,
1769 struct string_list
*commit_hex
,
1770 enum commit_graph_write_flags flags
,
1771 const struct split_commit_graph_opts
*split_opts
)
1773 struct write_commit_graph_context
*ctx
;
1774 uint32_t i
, count_distinct
= 0;
1778 if (!commit_graph_compatible(the_repository
))
1781 ctx
= xcalloc(1, sizeof(struct write_commit_graph_context
));
1782 ctx
->r
= the_repository
;
1784 /* normalize object dir with no trailing slash */
1785 ctx
->obj_dir
= xmallocz(strlen(obj_dir
) + 1);
1786 normalize_path_copy(ctx
->obj_dir
, obj_dir
);
1787 len
= strlen(ctx
->obj_dir
);
1788 if (len
&& ctx
->obj_dir
[len
- 1] == '/')
1789 ctx
->obj_dir
[len
- 1] = 0;
1791 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
1792 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
1793 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
1794 ctx
->check_oids
= flags
& COMMIT_GRAPH_WRITE_CHECK_OIDS
? 1 : 0;
1795 ctx
->split_opts
= split_opts
;
1798 struct commit_graph
*g
;
1799 prepare_commit_graph(ctx
->r
);
1801 g
= ctx
->r
->objects
->commit_graph
;
1804 ctx
->num_commit_graphs_before
++;
1808 if (ctx
->num_commit_graphs_before
) {
1809 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
1810 i
= ctx
->num_commit_graphs_before
;
1811 g
= ctx
->r
->objects
->commit_graph
;
1814 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
1820 ctx
->approx_nr_objects
= approximate_object_count();
1821 ctx
->oids
.alloc
= ctx
->approx_nr_objects
/ 32;
1823 if (ctx
->split
&& split_opts
&& ctx
->oids
.alloc
> split_opts
->max_commits
)
1824 ctx
->oids
.alloc
= split_opts
->max_commits
;
1827 prepare_commit_graph_one(ctx
->r
, ctx
->obj_dir
);
1828 if (ctx
->r
->objects
->commit_graph
)
1829 ctx
->oids
.alloc
+= ctx
->r
->objects
->commit_graph
->num_commits
;
1832 if (ctx
->oids
.alloc
< 1024)
1833 ctx
->oids
.alloc
= 1024;
1834 ALLOC_ARRAY(ctx
->oids
.list
, ctx
->oids
.alloc
);
1836 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
1837 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
1838 for (i
= 0; i
< g
->num_commits
; i
++) {
1839 const unsigned char *hash
= g
->chunk_oid_lookup
+ g
->hash_len
* i
;
1840 hashcpy(ctx
->oids
.list
[ctx
->oids
.nr
++].hash
, hash
);
1845 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
1850 if ((res
= fill_oids_from_commit_hex(ctx
, commit_hex
)))
1854 if (!pack_indexes
&& !commit_hex
)
1855 fill_oids_from_all_packs(ctx
);
1857 close_reachable(ctx
);
1859 count_distinct
= count_distinct_commits(ctx
);
1861 if (count_distinct
>= GRAPH_EDGE_LAST_MASK
) {
1862 error(_("the commit graph format cannot write %d commits"), count_distinct
);
1867 ctx
->commits
.alloc
= count_distinct
;
1868 ALLOC_ARRAY(ctx
->commits
.list
, ctx
->commits
.alloc
);
1870 copy_oids_to_commits(ctx
);
1872 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
1873 error(_("too many commits to write graph"));
1878 if (!ctx
->commits
.nr
)
1882 split_graph_merge_strategy(ctx
);
1884 merge_commit_graphs(ctx
);
1886 ctx
->num_commit_graphs_after
= 1;
1888 compute_generation_numbers(ctx
);
1890 res
= write_commit_graph_file(ctx
);
1893 mark_commit_graphs(ctx
);
1895 expire_commit_graphs(ctx
);
1898 free(ctx
->graph_name
);
1899 free(ctx
->commits
.list
);
1900 free(ctx
->oids
.list
);
1903 if (ctx
->commit_graph_filenames_after
) {
1904 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
1905 free(ctx
->commit_graph_filenames_after
[i
]);
1906 free(ctx
->commit_graph_hash_after
[i
]);
1909 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
1910 free(ctx
->commit_graph_filenames_before
[i
]);
1912 free(ctx
->commit_graph_filenames_after
);
1913 free(ctx
->commit_graph_filenames_before
);
1914 free(ctx
->commit_graph_hash_after
);
1922 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1923 static int verify_commit_graph_error
;
1925 static void graph_report(const char *fmt
, ...)
1929 verify_commit_graph_error
= 1;
1931 vfprintf(stderr
, fmt
, ap
);
1932 fprintf(stderr
, "\n");
1936 #define GENERATION_ZERO_EXISTS 1
1937 #define GENERATION_NUMBER_EXISTS 2
1939 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
1941 uint32_t i
, cur_fanout_pos
= 0;
1942 struct object_id prev_oid
, cur_oid
, checksum
;
1943 int generation_zero
= 0;
1946 struct progress
*progress
= NULL
;
1947 int local_error
= 0;
1950 graph_report("no commit-graph file loaded");
1954 verify_commit_graph_error
= verify_commit_graph_lite(g
);
1955 if (verify_commit_graph_error
)
1956 return verify_commit_graph_error
;
1958 devnull
= open("/dev/null", O_WRONLY
);
1959 f
= hashfd(devnull
, NULL
);
1960 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
1961 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
1962 if (!hasheq(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
1963 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1964 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
1967 for (i
= 0; i
< g
->num_commits
; i
++) {
1968 struct commit
*graph_commit
;
1970 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
1972 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
1973 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1974 oid_to_hex(&prev_oid
),
1975 oid_to_hex(&cur_oid
));
1977 oidcpy(&prev_oid
, &cur_oid
);
1979 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
1980 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1982 if (i
!= fanout_value
)
1983 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1984 cur_fanout_pos
, fanout_value
, i
);
1988 graph_commit
= lookup_commit(r
, &cur_oid
);
1989 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
1990 graph_report(_("failed to parse commit %s from commit-graph"),
1991 oid_to_hex(&cur_oid
));
1994 while (cur_fanout_pos
< 256) {
1995 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1997 if (g
->num_commits
!= fanout_value
)
1998 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1999 cur_fanout_pos
, fanout_value
, i
);
2004 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
2005 return verify_commit_graph_error
;
2007 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
2008 progress
= start_progress(_("Verifying commits in commit graph"),
2011 for (i
= 0; i
< g
->num_commits
; i
++) {
2012 struct commit
*graph_commit
, *odb_commit
;
2013 struct commit_list
*graph_parents
, *odb_parents
;
2014 uint32_t max_generation
= 0;
2016 display_progress(progress
, i
+ 1);
2017 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2019 graph_commit
= lookup_commit(r
, &cur_oid
);
2020 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2021 if (parse_commit_internal(odb_commit
, 0, 0)) {
2022 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2023 oid_to_hex(&cur_oid
));
2027 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2028 get_commit_tree_oid(odb_commit
)))
2029 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2030 oid_to_hex(&cur_oid
),
2031 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2032 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2034 graph_parents
= graph_commit
->parents
;
2035 odb_parents
= odb_commit
->parents
;
2037 while (graph_parents
) {
2038 if (odb_parents
== NULL
) {
2039 graph_report(_("commit-graph parent list for commit %s is too long"),
2040 oid_to_hex(&cur_oid
));
2044 /* parse parent in case it is in a base graph */
2045 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2047 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2048 graph_report(_("commit-graph parent for %s is %s != %s"),
2049 oid_to_hex(&cur_oid
),
2050 oid_to_hex(&graph_parents
->item
->object
.oid
),
2051 oid_to_hex(&odb_parents
->item
->object
.oid
));
2053 if (graph_parents
->item
->generation
> max_generation
)
2054 max_generation
= graph_parents
->item
->generation
;
2056 graph_parents
= graph_parents
->next
;
2057 odb_parents
= odb_parents
->next
;
2060 if (odb_parents
!= NULL
)
2061 graph_report(_("commit-graph parent list for commit %s terminates early"),
2062 oid_to_hex(&cur_oid
));
2064 if (!graph_commit
->generation
) {
2065 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2066 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2067 oid_to_hex(&cur_oid
));
2068 generation_zero
= GENERATION_ZERO_EXISTS
;
2069 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2070 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2071 oid_to_hex(&cur_oid
));
2073 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2077 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2078 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2079 * extra logic in the following condition.
2081 if (max_generation
== GENERATION_NUMBER_MAX
)
2084 if (graph_commit
->generation
!= max_generation
+ 1)
2085 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2086 oid_to_hex(&cur_oid
),
2087 graph_commit
->generation
,
2088 max_generation
+ 1);
2090 if (graph_commit
->date
!= odb_commit
->date
)
2091 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2092 oid_to_hex(&cur_oid
),
2096 stop_progress(&progress
);
2098 local_error
= verify_commit_graph_error
;
2100 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2101 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2106 void free_commit_graph(struct commit_graph
*g
)
2110 if (g
->graph_fd
>= 0) {
2111 munmap((void *)g
->data
, g
->data_len
);
2119 void disable_commit_graph(struct repository
*r
)
2121 r
->commit_graph_disabled
= 1;