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(struct object_directory
*odb
)
49 return xstrfmt("%s/info/commit-graph", odb
->path
);
52 static char *get_split_graph_filename(struct object_directory
*odb
,
55 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb
->path
,
59 static char *get_chain_filename(struct object_directory
*odb
)
61 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb
->path
);
64 static uint8_t oid_version(void)
69 static struct commit_graph
*alloc_commit_graph(void)
71 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
77 extern int read_replace_refs
;
79 static int commit_graph_compatible(struct repository
*r
)
84 if (read_replace_refs
) {
85 prepare_replace_object(r
);
86 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
90 prepare_commit_graft(r
);
91 if (r
->parsed_objects
&&
92 (r
->parsed_objects
->grafts_nr
|| r
->parsed_objects
->substituted_parent
))
94 if (is_repository_shallow(r
))
100 int open_commit_graph(const char *graph_file
, int *fd
, struct stat
*st
)
102 *fd
= git_open(graph_file
);
105 if (fstat(*fd
, st
)) {
112 struct commit_graph
*load_commit_graph_one_fd_st(int fd
, struct stat
*st
,
113 struct object_directory
*odb
)
117 struct commit_graph
*ret
;
119 graph_size
= xsize_t(st
->st_size
);
121 if (graph_size
< GRAPH_MIN_SIZE
) {
123 error(_("commit-graph file is too small"));
126 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
127 ret
= parse_commit_graph(graph_map
, fd
, graph_size
);
132 munmap(graph_map
, graph_size
);
139 static int verify_commit_graph_lite(struct commit_graph
*g
)
142 * Basic validation shared between parse_commit_graph()
143 * which'll be called every time the graph is used, and the
144 * much more expensive verify_commit_graph() used by
145 * "commit-graph verify".
147 * There should only be very basic checks here to ensure that
148 * we don't e.g. segfault in fill_commit_in_graph(), but
149 * because this is a very hot codepath nothing that e.g. loops
150 * over g->num_commits, or runs a checksum on the commit-graph
153 if (!g
->chunk_oid_fanout
) {
154 error("commit-graph is missing the OID Fanout chunk");
157 if (!g
->chunk_oid_lookup
) {
158 error("commit-graph is missing the OID Lookup chunk");
161 if (!g
->chunk_commit_data
) {
162 error("commit-graph is missing the Commit Data chunk");
169 struct commit_graph
*parse_commit_graph(void *graph_map
, int fd
,
172 const unsigned char *data
, *chunk_lookup
;
174 struct commit_graph
*graph
;
175 uint64_t last_chunk_offset
;
176 uint32_t last_chunk_id
;
177 uint32_t graph_signature
;
178 unsigned char graph_version
, hash_version
;
183 if (graph_size
< GRAPH_MIN_SIZE
)
186 data
= (const unsigned char *)graph_map
;
188 graph_signature
= get_be32(data
);
189 if (graph_signature
!= GRAPH_SIGNATURE
) {
190 error(_("commit-graph signature %X does not match signature %X"),
191 graph_signature
, GRAPH_SIGNATURE
);
195 graph_version
= *(unsigned char*)(data
+ 4);
196 if (graph_version
!= GRAPH_VERSION
) {
197 error(_("commit-graph version %X does not match version %X"),
198 graph_version
, GRAPH_VERSION
);
202 hash_version
= *(unsigned char*)(data
+ 5);
203 if (hash_version
!= oid_version()) {
204 error(_("commit-graph hash version %X does not match version %X"),
205 hash_version
, oid_version());
209 graph
= alloc_commit_graph();
211 graph
->hash_len
= the_hash_algo
->rawsz
;
212 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
213 graph
->graph_fd
= fd
;
214 graph
->data
= graph_map
;
215 graph
->data_len
= graph_size
;
218 last_chunk_offset
= 8;
219 chunk_lookup
= data
+ 8;
220 for (i
= 0; i
< graph
->num_chunks
; i
++) {
222 uint64_t chunk_offset
;
223 int chunk_repeated
= 0;
225 if (data
+ graph_size
- chunk_lookup
<
226 GRAPH_CHUNKLOOKUP_WIDTH
) {
227 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
232 chunk_id
= get_be32(chunk_lookup
+ 0);
233 chunk_offset
= get_be64(chunk_lookup
+ 4);
235 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
237 if (chunk_offset
> graph_size
- the_hash_algo
->rawsz
) {
238 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset
>> 32),
239 (uint32_t)chunk_offset
);
245 case GRAPH_CHUNKID_OIDFANOUT
:
246 if (graph
->chunk_oid_fanout
)
249 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
252 case GRAPH_CHUNKID_OIDLOOKUP
:
253 if (graph
->chunk_oid_lookup
)
256 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
259 case GRAPH_CHUNKID_DATA
:
260 if (graph
->chunk_commit_data
)
263 graph
->chunk_commit_data
= data
+ chunk_offset
;
266 case GRAPH_CHUNKID_EXTRAEDGES
:
267 if (graph
->chunk_extra_edges
)
270 graph
->chunk_extra_edges
= data
+ chunk_offset
;
273 case GRAPH_CHUNKID_BASE
:
274 if (graph
->chunk_base_graphs
)
277 graph
->chunk_base_graphs
= data
+ chunk_offset
;
280 if (chunk_repeated
) {
281 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id
);
286 if (last_chunk_id
== GRAPH_CHUNKID_OIDLOOKUP
)
288 graph
->num_commits
= (chunk_offset
- last_chunk_offset
)
292 last_chunk_id
= chunk_id
;
293 last_chunk_offset
= chunk_offset
;
296 hashcpy(graph
->oid
.hash
, graph
->data
+ graph
->data_len
- graph
->hash_len
);
298 if (verify_commit_graph_lite(graph
)) {
306 static struct commit_graph
*load_commit_graph_one(const char *graph_file
,
307 struct object_directory
*odb
)
312 struct commit_graph
*g
;
313 int open_ok
= open_commit_graph(graph_file
, &fd
, &st
);
318 g
= load_commit_graph_one_fd_st(fd
, &st
, odb
);
321 g
->filename
= xstrdup(graph_file
);
326 static struct commit_graph
*load_commit_graph_v1(struct repository
*r
,
327 struct object_directory
*odb
)
329 char *graph_name
= get_commit_graph_filename(odb
);
330 struct commit_graph
*g
= load_commit_graph_one(graph_name
, odb
);
336 static int add_graph_to_chain(struct commit_graph
*g
,
337 struct commit_graph
*chain
,
338 struct object_id
*oids
,
341 struct commit_graph
*cur_g
= chain
;
343 if (n
&& !g
->chunk_base_graphs
) {
344 warning(_("commit-graph has no base graphs chunk"));
352 !oideq(&oids
[n
], &cur_g
->oid
) ||
353 !hasheq(oids
[n
].hash
, g
->chunk_base_graphs
+ g
->hash_len
* n
)) {
354 warning(_("commit-graph chain does not match"));
358 cur_g
= cur_g
->base_graph
;
361 g
->base_graph
= chain
;
364 g
->num_commits_in_base
= chain
->num_commits
+ chain
->num_commits_in_base
;
369 static struct commit_graph
*load_commit_graph_chain(struct repository
*r
,
370 struct object_directory
*odb
)
372 struct commit_graph
*graph_chain
= NULL
;
373 struct strbuf line
= STRBUF_INIT
;
375 struct object_id
*oids
;
376 int i
= 0, valid
= 1, count
;
377 char *chain_name
= get_chain_filename(odb
);
381 fp
= fopen(chain_name
, "r");
382 stat_res
= stat(chain_name
, &st
);
387 st
.st_size
<= the_hash_algo
->hexsz
)
390 count
= st
.st_size
/ (the_hash_algo
->hexsz
+ 1);
391 oids
= xcalloc(count
, sizeof(struct object_id
));
395 for (i
= 0; i
< count
; i
++) {
396 struct object_directory
*odb
;
398 if (strbuf_getline_lf(&line
, fp
) == EOF
)
401 if (get_oid_hex(line
.buf
, &oids
[i
])) {
402 warning(_("invalid commit-graph chain: line '%s' not a hash"),
409 for (odb
= r
->objects
->odb
; odb
; odb
= odb
->next
) {
410 char *graph_name
= get_split_graph_filename(odb
, line
.buf
);
411 struct commit_graph
*g
= load_commit_graph_one(graph_name
, odb
);
416 if (add_graph_to_chain(g
, graph_chain
, oids
, i
)) {
426 warning(_("unable to find all commit-graph files"));
433 strbuf_release(&line
);
438 struct commit_graph
*read_commit_graph_one(struct repository
*r
,
439 struct object_directory
*odb
)
441 struct commit_graph
*g
= load_commit_graph_v1(r
, odb
);
444 g
= load_commit_graph_chain(r
, odb
);
449 static void prepare_commit_graph_one(struct repository
*r
,
450 struct object_directory
*odb
)
453 if (r
->objects
->commit_graph
)
456 r
->objects
->commit_graph
= read_commit_graph_one(r
, odb
);
460 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
462 * On the first invocation, this function attempts to load the commit
463 * graph if the_repository is configured to have one.
465 static int prepare_commit_graph(struct repository
*r
)
467 struct object_directory
*odb
;
470 * This must come before the "already attempted?" check below, because
471 * we want to disable even an already-loaded graph file.
473 if (r
->commit_graph_disabled
)
476 if (r
->objects
->commit_graph_attempted
)
477 return !!r
->objects
->commit_graph
;
478 r
->objects
->commit_graph_attempted
= 1;
480 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD
, 0))
481 die("dying as requested by the '%s' variable on commit-graph load!",
482 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD
);
484 prepare_repo_settings(r
);
486 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
487 r
->settings
.core_commit_graph
!= 1)
489 * This repository is not configured to use commit graphs, so
490 * do not load one. (But report commit_graph_attempted anyway
491 * so that commit graph loading is not attempted again for this
496 if (!commit_graph_compatible(r
))
500 for (odb
= r
->objects
->odb
;
501 !r
->objects
->commit_graph
&& odb
;
503 prepare_commit_graph_one(r
, odb
);
504 return !!r
->objects
->commit_graph
;
507 int generation_numbers_enabled(struct repository
*r
)
509 uint32_t first_generation
;
510 struct commit_graph
*g
;
511 if (!prepare_commit_graph(r
))
514 g
= r
->objects
->commit_graph
;
519 first_generation
= get_be32(g
->chunk_commit_data
+
520 g
->hash_len
+ 8) >> 2;
522 return !!first_generation
;
525 static void close_commit_graph_one(struct commit_graph
*g
)
530 close_commit_graph_one(g
->base_graph
);
531 free_commit_graph(g
);
534 void close_commit_graph(struct raw_object_store
*o
)
536 close_commit_graph_one(o
->commit_graph
);
537 o
->commit_graph
= NULL
;
540 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
542 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
543 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
546 static void load_oid_from_graph(struct commit_graph
*g
,
548 struct object_id
*oid
)
552 while (g
&& pos
< g
->num_commits_in_base
)
556 BUG("NULL commit-graph");
558 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
559 die(_("invalid commit position. commit-graph is likely corrupt"));
561 lex_index
= pos
- g
->num_commits_in_base
;
563 hashcpy(oid
->hash
, g
->chunk_oid_lookup
+ g
->hash_len
* lex_index
);
566 static struct commit_list
**insert_parent_or_die(struct repository
*r
,
567 struct commit_graph
*g
,
569 struct commit_list
**pptr
)
572 struct object_id oid
;
574 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
575 die("invalid parent position %"PRIu32
, pos
);
577 load_oid_from_graph(g
, pos
, &oid
);
578 c
= lookup_commit(r
, &oid
);
580 die(_("could not find commit %s"), oid_to_hex(&oid
));
582 return &commit_list_insert(c
, pptr
)->next
;
585 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
587 const unsigned char *commit_data
;
590 while (pos
< g
->num_commits_in_base
)
593 lex_index
= pos
- g
->num_commits_in_base
;
594 commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* lex_index
;
595 item
->graph_pos
= pos
;
596 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
599 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
604 static int fill_commit_in_graph(struct repository
*r
,
606 struct commit_graph
*g
, uint32_t pos
)
609 uint32_t *parent_data_ptr
;
610 uint64_t date_low
, date_high
;
611 struct commit_list
**pptr
;
612 const unsigned char *commit_data
;
615 while (pos
< g
->num_commits_in_base
)
618 if (pos
>= g
->num_commits
+ g
->num_commits_in_base
)
619 die(_("invalid commit position. commit-graph is likely corrupt"));
622 * Store the "full" position, but then use the
623 * "local" position for the rest of the calculation.
625 item
->graph_pos
= pos
;
626 lex_index
= pos
- g
->num_commits_in_base
;
628 commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * lex_index
;
630 item
->object
.parsed
= 1;
632 set_commit_tree(item
, NULL
);
634 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
635 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
636 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
638 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
640 pptr
= &item
->parents
;
642 edge_value
= get_be32(commit_data
+ g
->hash_len
);
643 if (edge_value
== GRAPH_PARENT_NONE
)
645 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
647 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
648 if (edge_value
== GRAPH_PARENT_NONE
)
650 if (!(edge_value
& GRAPH_EXTRA_EDGES_NEEDED
)) {
651 pptr
= insert_parent_or_die(r
, g
, edge_value
, pptr
);
655 parent_data_ptr
= (uint32_t*)(g
->chunk_extra_edges
+
656 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
658 edge_value
= get_be32(parent_data_ptr
);
659 pptr
= insert_parent_or_die(r
, g
,
660 edge_value
& GRAPH_EDGE_LAST_MASK
,
663 } while (!(edge_value
& GRAPH_LAST_EDGE
));
668 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
670 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
671 *pos
= item
->graph_pos
;
674 struct commit_graph
*cur_g
= g
;
677 while (cur_g
&& !bsearch_graph(cur_g
, &(item
->object
.oid
), &lex_index
))
678 cur_g
= cur_g
->base_graph
;
681 *pos
= lex_index
+ cur_g
->num_commits_in_base
;
689 static int parse_commit_in_graph_one(struct repository
*r
,
690 struct commit_graph
*g
,
695 if (item
->object
.parsed
)
698 if (find_commit_in_graph(item
, g
, &pos
))
699 return fill_commit_in_graph(r
, item
, g
, pos
);
704 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
706 if (!prepare_commit_graph(r
))
708 return parse_commit_in_graph_one(r
, r
->objects
->commit_graph
, item
);
711 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
714 if (!prepare_commit_graph(r
))
716 if (find_commit_in_graph(item
, r
->objects
->commit_graph
, &pos
))
717 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
720 static struct tree
*load_tree_for_commit(struct repository
*r
,
721 struct commit_graph
*g
,
724 struct object_id oid
;
725 const unsigned char *commit_data
;
727 while (c
->graph_pos
< g
->num_commits_in_base
)
730 commit_data
= g
->chunk_commit_data
+
731 GRAPH_DATA_WIDTH
* (c
->graph_pos
- g
->num_commits_in_base
);
733 hashcpy(oid
.hash
, commit_data
);
734 set_commit_tree(c
, lookup_tree(r
, &oid
));
736 return c
->maybe_tree
;
739 static struct tree
*get_commit_tree_in_graph_one(struct repository
*r
,
740 struct commit_graph
*g
,
741 const struct commit
*c
)
744 return c
->maybe_tree
;
745 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
746 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
748 return load_tree_for_commit(r
, g
, (struct commit
*)c
);
751 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
753 return get_commit_tree_in_graph_one(r
, r
->objects
->commit_graph
, c
);
756 struct packed_commit_list
{
757 struct commit
**list
;
762 struct packed_oid_list
{
763 struct object_id
*list
;
768 struct write_commit_graph_context
{
769 struct repository
*r
;
770 struct object_directory
*odb
;
772 struct packed_oid_list oids
;
773 struct packed_commit_list commits
;
775 unsigned long approx_nr_objects
;
776 struct progress
*progress
;
778 uint64_t progress_cnt
;
780 char *base_graph_name
;
781 int num_commit_graphs_before
;
782 int num_commit_graphs_after
;
783 char **commit_graph_filenames_before
;
784 char **commit_graph_filenames_after
;
785 char **commit_graph_hash_after
;
786 uint32_t new_num_commits_in_base
;
787 struct commit_graph
*new_base_graph
;
794 const struct split_commit_graph_opts
*split_opts
;
797 static void write_graph_chunk_fanout(struct hashfile
*f
,
798 struct write_commit_graph_context
*ctx
)
801 struct commit
**list
= ctx
->commits
.list
;
804 * Write the first-level table (the list is sorted,
805 * but we use a 256-entry lookup to be able to avoid
806 * having to do eight extra binary search iterations).
808 for (i
= 0; i
< 256; i
++) {
809 while (count
< ctx
->commits
.nr
) {
810 if ((*list
)->object
.oid
.hash
[0] != i
)
812 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
817 hashwrite_be32(f
, count
);
821 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
822 struct write_commit_graph_context
*ctx
)
824 struct commit
**list
= ctx
->commits
.list
;
826 for (count
= 0; count
< ctx
->commits
.nr
; count
++, list
++) {
827 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
828 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
832 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
834 struct commit
**commits
= table
;
835 return commits
[index
]->object
.oid
.hash
;
838 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
839 struct write_commit_graph_context
*ctx
)
841 struct commit
**list
= ctx
->commits
.list
;
842 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
843 uint32_t num_extra_edges
= 0;
845 while (list
< last
) {
846 struct commit_list
*parent
;
847 struct object_id
*tree
;
849 uint32_t packedDate
[2];
850 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
852 if (parse_commit_no_graph(*list
))
853 die(_("unable to parse commit %s"),
854 oid_to_hex(&(*list
)->object
.oid
));
855 tree
= get_commit_tree_oid(*list
);
856 hashwrite(f
, tree
->hash
, hash_len
);
858 parent
= (*list
)->parents
;
861 edge_value
= GRAPH_PARENT_NONE
;
863 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
869 edge_value
+= ctx
->new_num_commits_in_base
;
872 if (find_commit_in_graph(parent
->item
,
879 BUG("missing parent %s for commit %s",
880 oid_to_hex(&parent
->item
->object
.oid
),
881 oid_to_hex(&(*list
)->object
.oid
));
884 hashwrite_be32(f
, edge_value
);
887 parent
= parent
->next
;
890 edge_value
= GRAPH_PARENT_NONE
;
891 else if (parent
->next
)
892 edge_value
= GRAPH_EXTRA_EDGES_NEEDED
| num_extra_edges
;
894 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
900 edge_value
+= ctx
->new_num_commits_in_base
;
903 if (find_commit_in_graph(parent
->item
,
910 BUG("missing parent %s for commit %s",
911 oid_to_hex(&parent
->item
->object
.oid
),
912 oid_to_hex(&(*list
)->object
.oid
));
915 hashwrite_be32(f
, edge_value
);
917 if (edge_value
& GRAPH_EXTRA_EDGES_NEEDED
) {
920 parent
= parent
->next
;
924 if (sizeof((*list
)->date
) > 4)
925 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
929 packedDate
[0] |= htonl((*list
)->generation
<< 2);
931 packedDate
[1] = htonl((*list
)->date
);
932 hashwrite(f
, packedDate
, 8);
938 static void write_graph_chunk_extra_edges(struct hashfile
*f
,
939 struct write_commit_graph_context
*ctx
)
941 struct commit
**list
= ctx
->commits
.list
;
942 struct commit
**last
= ctx
->commits
.list
+ ctx
->commits
.nr
;
943 struct commit_list
*parent
;
945 while (list
< last
) {
948 display_progress(ctx
->progress
, ++ctx
->progress_cnt
);
950 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
951 parent
= parent
->next
)
954 if (num_parents
<= 2) {
959 /* Since num_parents > 2, this initializer is safe. */
960 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
961 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
967 edge_value
+= ctx
->new_num_commits_in_base
;
970 if (find_commit_in_graph(parent
->item
,
977 BUG("missing parent %s for commit %s",
978 oid_to_hex(&parent
->item
->object
.oid
),
979 oid_to_hex(&(*list
)->object
.oid
));
980 else if (!parent
->next
)
981 edge_value
|= GRAPH_LAST_EDGE
;
983 hashwrite_be32(f
, edge_value
);
990 static int oid_compare(const void *_a
, const void *_b
)
992 const struct object_id
*a
= (const struct object_id
*)_a
;
993 const struct object_id
*b
= (const struct object_id
*)_b
;
997 static int add_packed_commits(const struct object_id
*oid
,
998 struct packed_git
*pack
,
1002 struct write_commit_graph_context
*ctx
= (struct write_commit_graph_context
*)data
;
1003 enum object_type type
;
1004 off_t offset
= nth_packed_object_offset(pack
, pos
);
1005 struct object_info oi
= OBJECT_INFO_INIT
;
1008 display_progress(ctx
->progress
, ++ctx
->progress_done
);
1011 if (packed_object_info(ctx
->r
, pack
, offset
, &oi
) < 0)
1012 die(_("unable to get type of object %s"), oid_to_hex(oid
));
1014 if (type
!= OBJ_COMMIT
)
1017 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1018 oidcpy(&(ctx
->oids
.list
[ctx
->oids
.nr
]), oid
);
1024 static void add_missing_parents(struct write_commit_graph_context
*ctx
, struct commit
*commit
)
1026 struct commit_list
*parent
;
1027 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
1028 if (!(parent
->item
->object
.flags
& REACHABLE
)) {
1029 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1030 oidcpy(&ctx
->oids
.list
[ctx
->oids
.nr
], &(parent
->item
->object
.oid
));
1032 parent
->item
->object
.flags
|= REACHABLE
;
1037 static void close_reachable(struct write_commit_graph_context
*ctx
)
1040 struct commit
*commit
;
1042 if (ctx
->report_progress
)
1043 ctx
->progress
= start_delayed_progress(
1044 _("Loading known commits in commit graph"),
1046 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1047 display_progress(ctx
->progress
, i
+ 1);
1048 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1050 commit
->object
.flags
|= REACHABLE
;
1052 stop_progress(&ctx
->progress
);
1055 * As this loop runs, ctx->oids.nr may grow, but not more
1056 * than the number of missing commits in the reachable
1059 if (ctx
->report_progress
)
1060 ctx
->progress
= start_delayed_progress(
1061 _("Expanding reachable commits in commit graph"),
1063 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1064 display_progress(ctx
->progress
, i
+ 1);
1065 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1070 if (!parse_commit(commit
) &&
1071 commit
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
1072 add_missing_parents(ctx
, commit
);
1073 } else if (!parse_commit_no_graph(commit
))
1074 add_missing_parents(ctx
, commit
);
1076 stop_progress(&ctx
->progress
);
1078 if (ctx
->report_progress
)
1079 ctx
->progress
= start_delayed_progress(
1080 _("Clearing commit marks in commit graph"),
1082 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1083 display_progress(ctx
->progress
, i
+ 1);
1084 commit
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1087 commit
->object
.flags
&= ~REACHABLE
;
1089 stop_progress(&ctx
->progress
);
1092 static void compute_generation_numbers(struct write_commit_graph_context
*ctx
)
1095 struct commit_list
*list
= NULL
;
1097 if (ctx
->report_progress
)
1098 ctx
->progress
= start_delayed_progress(
1099 _("Computing commit graph generation numbers"),
1101 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1102 display_progress(ctx
->progress
, i
+ 1);
1103 if (ctx
->commits
.list
[i
]->generation
!= GENERATION_NUMBER_INFINITY
&&
1104 ctx
->commits
.list
[i
]->generation
!= GENERATION_NUMBER_ZERO
)
1107 commit_list_insert(ctx
->commits
.list
[i
], &list
);
1109 struct commit
*current
= list
->item
;
1110 struct commit_list
*parent
;
1111 int all_parents_computed
= 1;
1112 uint32_t max_generation
= 0;
1114 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
1115 if (parent
->item
->generation
== GENERATION_NUMBER_INFINITY
||
1116 parent
->item
->generation
== GENERATION_NUMBER_ZERO
) {
1117 all_parents_computed
= 0;
1118 commit_list_insert(parent
->item
, &list
);
1120 } else if (parent
->item
->generation
> max_generation
) {
1121 max_generation
= parent
->item
->generation
;
1125 if (all_parents_computed
) {
1126 current
->generation
= max_generation
+ 1;
1129 if (current
->generation
> GENERATION_NUMBER_MAX
)
1130 current
->generation
= GENERATION_NUMBER_MAX
;
1134 stop_progress(&ctx
->progress
);
1137 static int add_ref_to_list(const char *refname
,
1138 const struct object_id
*oid
,
1139 int flags
, void *cb_data
)
1141 struct string_list
*list
= (struct string_list
*)cb_data
;
1143 string_list_append(list
, oid_to_hex(oid
));
1147 int write_commit_graph_reachable(struct object_directory
*odb
,
1148 enum commit_graph_write_flags flags
,
1149 const struct split_commit_graph_opts
*split_opts
)
1151 struct string_list list
= STRING_LIST_INIT_DUP
;
1154 for_each_ref(add_ref_to_list
, &list
);
1155 result
= write_commit_graph(odb
, NULL
, &list
,
1158 string_list_clear(&list
, 0);
1162 static int fill_oids_from_packs(struct write_commit_graph_context
*ctx
,
1163 struct string_list
*pack_indexes
)
1166 struct strbuf progress_title
= STRBUF_INIT
;
1167 struct strbuf packname
= STRBUF_INIT
;
1170 strbuf_addf(&packname
, "%s/pack/", ctx
->odb
->path
);
1171 dirlen
= packname
.len
;
1172 if (ctx
->report_progress
) {
1173 strbuf_addf(&progress_title
,
1174 Q_("Finding commits for commit graph in %d pack",
1175 "Finding commits for commit graph in %d packs",
1178 ctx
->progress
= start_delayed_progress(progress_title
.buf
, 0);
1179 ctx
->progress_done
= 0;
1181 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
1182 struct packed_git
*p
;
1183 strbuf_setlen(&packname
, dirlen
);
1184 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
1185 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
1187 error(_("error adding pack %s"), packname
.buf
);
1190 if (open_pack_index(p
)) {
1191 error(_("error opening index for %s"), packname
.buf
);
1194 for_each_object_in_pack(p
, add_packed_commits
, ctx
,
1195 FOR_EACH_OBJECT_PACK_ORDER
);
1200 stop_progress(&ctx
->progress
);
1201 strbuf_release(&progress_title
);
1202 strbuf_release(&packname
);
1207 static int fill_oids_from_commit_hex(struct write_commit_graph_context
*ctx
,
1208 struct string_list
*commit_hex
)
1211 struct strbuf progress_title
= STRBUF_INIT
;
1213 if (ctx
->report_progress
) {
1214 strbuf_addf(&progress_title
,
1215 Q_("Finding commits for commit graph from %d ref",
1216 "Finding commits for commit graph from %d refs",
1219 ctx
->progress
= start_delayed_progress(
1223 for (i
= 0; i
< commit_hex
->nr
; i
++) {
1225 struct object_id oid
;
1226 struct commit
*result
;
1228 display_progress(ctx
->progress
, i
+ 1);
1229 if (!parse_oid_hex(commit_hex
->items
[i
].string
, &oid
, &end
) &&
1230 (result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1))) {
1231 ALLOC_GROW(ctx
->oids
.list
, ctx
->oids
.nr
+ 1, ctx
->oids
.alloc
);
1232 oidcpy(&ctx
->oids
.list
[ctx
->oids
.nr
], &(result
->object
.oid
));
1234 } else if (ctx
->check_oids
) {
1235 error(_("invalid commit object id: %s"),
1236 commit_hex
->items
[i
].string
);
1240 stop_progress(&ctx
->progress
);
1241 strbuf_release(&progress_title
);
1246 static void fill_oids_from_all_packs(struct write_commit_graph_context
*ctx
)
1248 if (ctx
->report_progress
)
1249 ctx
->progress
= start_delayed_progress(
1250 _("Finding commits for commit graph among packed objects"),
1251 ctx
->approx_nr_objects
);
1252 for_each_packed_object(add_packed_commits
, ctx
,
1253 FOR_EACH_OBJECT_PACK_ORDER
);
1254 if (ctx
->progress_done
< ctx
->approx_nr_objects
)
1255 display_progress(ctx
->progress
, ctx
->approx_nr_objects
);
1256 stop_progress(&ctx
->progress
);
1259 static uint32_t count_distinct_commits(struct write_commit_graph_context
*ctx
)
1261 uint32_t i
, count_distinct
= 1;
1263 if (ctx
->report_progress
)
1264 ctx
->progress
= start_delayed_progress(
1265 _("Counting distinct commits in commit graph"),
1267 display_progress(ctx
->progress
, 0); /* TODO: Measure QSORT() progress */
1268 QSORT(ctx
->oids
.list
, ctx
->oids
.nr
, oid_compare
);
1270 for (i
= 1; i
< ctx
->oids
.nr
; i
++) {
1271 display_progress(ctx
->progress
, i
+ 1);
1272 if (!oideq(&ctx
->oids
.list
[i
- 1], &ctx
->oids
.list
[i
])) {
1274 struct commit
*c
= lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1276 if (!c
|| c
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
)
1283 stop_progress(&ctx
->progress
);
1285 return count_distinct
;
1288 static void copy_oids_to_commits(struct write_commit_graph_context
*ctx
)
1292 ctx
->num_extra_edges
= 0;
1293 if (ctx
->report_progress
)
1294 ctx
->progress
= start_delayed_progress(
1295 _("Finding extra edges in commit graph"),
1297 for (i
= 0; i
< ctx
->oids
.nr
; i
++) {
1298 unsigned int num_parents
;
1300 display_progress(ctx
->progress
, i
+ 1);
1301 if (i
> 0 && oideq(&ctx
->oids
.list
[i
- 1], &ctx
->oids
.list
[i
]))
1304 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ 1, ctx
->commits
.alloc
);
1305 ctx
->commits
.list
[ctx
->commits
.nr
] = lookup_commit(ctx
->r
, &ctx
->oids
.list
[i
]);
1308 ctx
->commits
.list
[ctx
->commits
.nr
]->graph_pos
!= COMMIT_NOT_FROM_GRAPH
)
1311 parse_commit_no_graph(ctx
->commits
.list
[ctx
->commits
.nr
]);
1313 num_parents
= commit_list_count(ctx
->commits
.list
[ctx
->commits
.nr
]->parents
);
1314 if (num_parents
> 2)
1315 ctx
->num_extra_edges
+= num_parents
- 1;
1319 stop_progress(&ctx
->progress
);
1322 static int write_graph_chunk_base_1(struct hashfile
*f
,
1323 struct commit_graph
*g
)
1330 num
= write_graph_chunk_base_1(f
, g
->base_graph
);
1331 hashwrite(f
, g
->oid
.hash
, the_hash_algo
->rawsz
);
1335 static int write_graph_chunk_base(struct hashfile
*f
,
1336 struct write_commit_graph_context
*ctx
)
1338 int num
= write_graph_chunk_base_1(f
, ctx
->new_base_graph
);
1340 if (num
!= ctx
->num_commit_graphs_after
- 1) {
1341 error(_("failed to write correct number of base graph ids"));
1348 static int write_commit_graph_file(struct write_commit_graph_context
*ctx
)
1353 struct lock_file lk
= LOCK_INIT
;
1354 uint32_t chunk_ids
[6];
1355 uint64_t chunk_offsets
[6];
1356 const unsigned hashsz
= the_hash_algo
->rawsz
;
1357 struct strbuf progress_title
= STRBUF_INIT
;
1359 struct object_id file_hash
;
1362 struct strbuf tmp_file
= STRBUF_INIT
;
1364 strbuf_addf(&tmp_file
,
1365 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1367 ctx
->graph_name
= strbuf_detach(&tmp_file
, NULL
);
1369 ctx
->graph_name
= get_commit_graph_filename(ctx
->odb
);
1372 if (safe_create_leading_directories(ctx
->graph_name
)) {
1373 UNLEAK(ctx
->graph_name
);
1374 error(_("unable to create leading directories of %s"),
1380 char *lock_name
= get_chain_filename(ctx
->odb
);
1382 hold_lock_file_for_update(&lk
, lock_name
, LOCK_DIE_ON_ERROR
);
1384 fd
= git_mkstemp_mode(ctx
->graph_name
, 0444);
1386 error(_("unable to create '%s'"), ctx
->graph_name
);
1390 f
= hashfd(fd
, ctx
->graph_name
);
1392 hold_lock_file_for_update(&lk
, ctx
->graph_name
, LOCK_DIE_ON_ERROR
);
1393 fd
= lk
.tempfile
->fd
;
1394 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
1397 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
1398 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
1399 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
1400 if (ctx
->num_extra_edges
) {
1401 chunk_ids
[num_chunks
] = GRAPH_CHUNKID_EXTRAEDGES
;
1404 if (ctx
->num_commit_graphs_after
> 1) {
1405 chunk_ids
[num_chunks
] = GRAPH_CHUNKID_BASE
;
1409 chunk_ids
[num_chunks
] = 0;
1411 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
1412 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
1413 chunk_offsets
[2] = chunk_offsets
[1] + hashsz
* ctx
->commits
.nr
;
1414 chunk_offsets
[3] = chunk_offsets
[2] + (hashsz
+ 16) * ctx
->commits
.nr
;
1417 if (ctx
->num_extra_edges
) {
1418 chunk_offsets
[num_chunks
+ 1] = chunk_offsets
[num_chunks
] +
1419 4 * ctx
->num_extra_edges
;
1422 if (ctx
->num_commit_graphs_after
> 1) {
1423 chunk_offsets
[num_chunks
+ 1] = chunk_offsets
[num_chunks
] +
1424 hashsz
* (ctx
->num_commit_graphs_after
- 1);
1428 hashwrite_be32(f
, GRAPH_SIGNATURE
);
1430 hashwrite_u8(f
, GRAPH_VERSION
);
1431 hashwrite_u8(f
, oid_version());
1432 hashwrite_u8(f
, num_chunks
);
1433 hashwrite_u8(f
, ctx
->num_commit_graphs_after
- 1);
1435 for (i
= 0; i
<= num_chunks
; i
++) {
1436 uint32_t chunk_write
[3];
1438 chunk_write
[0] = htonl(chunk_ids
[i
]);
1439 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
1440 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
1441 hashwrite(f
, chunk_write
, 12);
1444 if (ctx
->report_progress
) {
1445 strbuf_addf(&progress_title
,
1446 Q_("Writing out commit graph in %d pass",
1447 "Writing out commit graph in %d passes",
1450 ctx
->progress
= start_delayed_progress(
1452 num_chunks
* ctx
->commits
.nr
);
1454 write_graph_chunk_fanout(f
, ctx
);
1455 write_graph_chunk_oids(f
, hashsz
, ctx
);
1456 write_graph_chunk_data(f
, hashsz
, ctx
);
1457 if (ctx
->num_extra_edges
)
1458 write_graph_chunk_extra_edges(f
, ctx
);
1459 if (ctx
->num_commit_graphs_after
> 1 &&
1460 write_graph_chunk_base(f
, ctx
)) {
1463 stop_progress(&ctx
->progress
);
1464 strbuf_release(&progress_title
);
1466 if (ctx
->split
&& ctx
->base_graph_name
&& ctx
->num_commit_graphs_after
> 1) {
1467 char *new_base_hash
= xstrdup(oid_to_hex(&ctx
->new_base_graph
->oid
));
1468 char *new_base_name
= get_split_graph_filename(ctx
->new_base_graph
->odb
, new_base_hash
);
1470 free(ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2]);
1471 free(ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2]);
1472 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 2] = new_base_name
;
1473 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 2] = new_base_hash
;
1476 close_commit_graph(ctx
->r
->objects
);
1477 finalize_hashfile(f
, file_hash
.hash
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
1480 FILE *chainf
= fdopen_lock_file(&lk
, "w");
1481 char *final_graph_name
;
1487 error(_("unable to open commit-graph chain file"));
1491 if (ctx
->base_graph_name
) {
1492 const char *dest
= ctx
->commit_graph_filenames_after
[
1493 ctx
->num_commit_graphs_after
- 2];
1495 if (strcmp(ctx
->base_graph_name
, dest
)) {
1496 result
= rename(ctx
->base_graph_name
, dest
);
1499 error(_("failed to rename base commit-graph file"));
1504 char *graph_name
= get_commit_graph_filename(ctx
->odb
);
1508 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1] = xstrdup(oid_to_hex(&file_hash
));
1509 final_graph_name
= get_split_graph_filename(ctx
->odb
,
1510 ctx
->commit_graph_hash_after
[ctx
->num_commit_graphs_after
- 1]);
1511 ctx
->commit_graph_filenames_after
[ctx
->num_commit_graphs_after
- 1] = final_graph_name
;
1513 result
= rename(ctx
->graph_name
, final_graph_name
);
1515 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++)
1516 fprintf(lk
.tempfile
->fp
, "%s\n", ctx
->commit_graph_hash_after
[i
]);
1519 error(_("failed to rename temporary commit-graph file"));
1524 commit_lock_file(&lk
);
1529 static void split_graph_merge_strategy(struct write_commit_graph_context
*ctx
)
1531 struct commit_graph
*g
;
1532 uint32_t num_commits
;
1535 int max_commits
= 0;
1538 if (ctx
->split_opts
) {
1539 max_commits
= ctx
->split_opts
->max_commits
;
1541 if (ctx
->split_opts
->size_multiple
)
1542 size_mult
= ctx
->split_opts
->size_multiple
;
1545 g
= ctx
->r
->objects
->commit_graph
;
1546 num_commits
= ctx
->commits
.nr
;
1547 ctx
->num_commit_graphs_after
= ctx
->num_commit_graphs_before
+ 1;
1549 while (g
&& (g
->num_commits
<= size_mult
* num_commits
||
1550 (max_commits
&& num_commits
> max_commits
))) {
1551 if (g
->odb
!= ctx
->odb
)
1554 num_commits
+= g
->num_commits
;
1557 ctx
->num_commit_graphs_after
--;
1560 ctx
->new_base_graph
= g
;
1562 if (ctx
->num_commit_graphs_after
== 2) {
1563 char *old_graph_name
= get_commit_graph_filename(g
->odb
);
1565 if (!strcmp(g
->filename
, old_graph_name
) &&
1566 g
->odb
!= ctx
->odb
) {
1567 ctx
->num_commit_graphs_after
= 1;
1568 ctx
->new_base_graph
= NULL
;
1571 free(old_graph_name
);
1574 ALLOC_ARRAY(ctx
->commit_graph_filenames_after
, ctx
->num_commit_graphs_after
);
1575 ALLOC_ARRAY(ctx
->commit_graph_hash_after
, ctx
->num_commit_graphs_after
);
1577 for (i
= 0; i
< ctx
->num_commit_graphs_after
&&
1578 i
< ctx
->num_commit_graphs_before
; i
++)
1579 ctx
->commit_graph_filenames_after
[i
] = xstrdup(ctx
->commit_graph_filenames_before
[i
]);
1581 i
= ctx
->num_commit_graphs_before
- 1;
1582 g
= ctx
->r
->objects
->commit_graph
;
1585 if (i
< ctx
->num_commit_graphs_after
)
1586 ctx
->commit_graph_hash_after
[i
] = xstrdup(oid_to_hex(&g
->oid
));
1593 static void merge_commit_graph(struct write_commit_graph_context
*ctx
,
1594 struct commit_graph
*g
)
1597 uint32_t offset
= g
->num_commits_in_base
;
1599 ALLOC_GROW(ctx
->commits
.list
, ctx
->commits
.nr
+ g
->num_commits
, ctx
->commits
.alloc
);
1601 for (i
= 0; i
< g
->num_commits
; i
++) {
1602 struct object_id oid
;
1603 struct commit
*result
;
1605 display_progress(ctx
->progress
, i
+ 1);
1607 load_oid_from_graph(g
, i
+ offset
, &oid
);
1609 /* only add commits if they still exist in the repo */
1610 result
= lookup_commit_reference_gently(ctx
->r
, &oid
, 1);
1613 ctx
->commits
.list
[ctx
->commits
.nr
] = result
;
1619 static int commit_compare(const void *_a
, const void *_b
)
1621 const struct commit
*a
= *(const struct commit
**)_a
;
1622 const struct commit
*b
= *(const struct commit
**)_b
;
1623 return oidcmp(&a
->object
.oid
, &b
->object
.oid
);
1626 static void sort_and_scan_merged_commits(struct write_commit_graph_context
*ctx
)
1630 if (ctx
->report_progress
)
1631 ctx
->progress
= start_delayed_progress(
1632 _("Scanning merged commits"),
1635 QSORT(ctx
->commits
.list
, ctx
->commits
.nr
, commit_compare
);
1637 ctx
->num_extra_edges
= 0;
1638 for (i
= 0; i
< ctx
->commits
.nr
; i
++) {
1639 display_progress(ctx
->progress
, i
);
1641 if (i
&& oideq(&ctx
->commits
.list
[i
- 1]->object
.oid
,
1642 &ctx
->commits
.list
[i
]->object
.oid
)) {
1643 die(_("unexpected duplicate commit id %s"),
1644 oid_to_hex(&ctx
->commits
.list
[i
]->object
.oid
));
1646 unsigned int num_parents
;
1648 num_parents
= commit_list_count(ctx
->commits
.list
[i
]->parents
);
1649 if (num_parents
> 2)
1650 ctx
->num_extra_edges
+= num_parents
- 1;
1654 stop_progress(&ctx
->progress
);
1657 static void merge_commit_graphs(struct write_commit_graph_context
*ctx
)
1659 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
1660 uint32_t current_graph_number
= ctx
->num_commit_graphs_before
;
1662 while (g
&& current_graph_number
>= ctx
->num_commit_graphs_after
) {
1663 current_graph_number
--;
1665 if (ctx
->report_progress
)
1666 ctx
->progress
= start_delayed_progress(_("Merging commit-graph"), 0);
1668 merge_commit_graph(ctx
, g
);
1669 stop_progress(&ctx
->progress
);
1675 ctx
->new_base_graph
= g
;
1676 ctx
->new_num_commits_in_base
= g
->num_commits
+ g
->num_commits_in_base
;
1679 if (ctx
->new_base_graph
)
1680 ctx
->base_graph_name
= xstrdup(ctx
->new_base_graph
->filename
);
1682 sort_and_scan_merged_commits(ctx
);
1685 static void mark_commit_graphs(struct write_commit_graph_context
*ctx
)
1688 time_t now
= time(NULL
);
1690 for (i
= ctx
->num_commit_graphs_after
- 1; i
< ctx
->num_commit_graphs_before
; i
++) {
1692 struct utimbuf updated_time
;
1694 stat(ctx
->commit_graph_filenames_before
[i
], &st
);
1696 updated_time
.actime
= st
.st_atime
;
1697 updated_time
.modtime
= now
;
1698 utime(ctx
->commit_graph_filenames_before
[i
], &updated_time
);
1702 static void expire_commit_graphs(struct write_commit_graph_context
*ctx
)
1704 struct strbuf path
= STRBUF_INIT
;
1708 timestamp_t expire_time
= time(NULL
);
1710 if (ctx
->split_opts
&& ctx
->split_opts
->expire_time
)
1711 expire_time
-= ctx
->split_opts
->expire_time
;
1713 char *chain_file_name
= get_chain_filename(ctx
->odb
);
1714 unlink(chain_file_name
);
1715 free(chain_file_name
);
1716 ctx
->num_commit_graphs_after
= 0;
1719 strbuf_addstr(&path
, ctx
->odb
->path
);
1720 strbuf_addstr(&path
, "/info/commit-graphs");
1721 dir
= opendir(path
.buf
);
1726 strbuf_addch(&path
, '/');
1727 dirnamelen
= path
.len
;
1728 while ((de
= readdir(dir
)) != NULL
) {
1730 uint32_t i
, found
= 0;
1732 strbuf_setlen(&path
, dirnamelen
);
1733 strbuf_addstr(&path
, de
->d_name
);
1735 stat(path
.buf
, &st
);
1737 if (st
.st_mtime
> expire_time
)
1739 if (path
.len
< 6 || strcmp(path
.buf
+ path
.len
- 6, ".graph"))
1742 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
1743 if (!strcmp(ctx
->commit_graph_filenames_after
[i
],
1755 strbuf_release(&path
);
1758 int write_commit_graph(struct object_directory
*odb
,
1759 struct string_list
*pack_indexes
,
1760 struct string_list
*commit_hex
,
1761 enum commit_graph_write_flags flags
,
1762 const struct split_commit_graph_opts
*split_opts
)
1764 struct write_commit_graph_context
*ctx
;
1765 uint32_t i
, count_distinct
= 0;
1768 if (!commit_graph_compatible(the_repository
))
1771 ctx
= xcalloc(1, sizeof(struct write_commit_graph_context
));
1772 ctx
->r
= the_repository
;
1774 ctx
->append
= flags
& COMMIT_GRAPH_WRITE_APPEND
? 1 : 0;
1775 ctx
->report_progress
= flags
& COMMIT_GRAPH_WRITE_PROGRESS
? 1 : 0;
1776 ctx
->split
= flags
& COMMIT_GRAPH_WRITE_SPLIT
? 1 : 0;
1777 ctx
->check_oids
= flags
& COMMIT_GRAPH_WRITE_CHECK_OIDS
? 1 : 0;
1778 ctx
->split_opts
= split_opts
;
1781 struct commit_graph
*g
;
1782 prepare_commit_graph(ctx
->r
);
1784 g
= ctx
->r
->objects
->commit_graph
;
1787 ctx
->num_commit_graphs_before
++;
1791 if (ctx
->num_commit_graphs_before
) {
1792 ALLOC_ARRAY(ctx
->commit_graph_filenames_before
, ctx
->num_commit_graphs_before
);
1793 i
= ctx
->num_commit_graphs_before
;
1794 g
= ctx
->r
->objects
->commit_graph
;
1797 ctx
->commit_graph_filenames_before
[--i
] = xstrdup(g
->filename
);
1803 ctx
->approx_nr_objects
= approximate_object_count();
1804 ctx
->oids
.alloc
= ctx
->approx_nr_objects
/ 32;
1806 if (ctx
->split
&& split_opts
&& ctx
->oids
.alloc
> split_opts
->max_commits
)
1807 ctx
->oids
.alloc
= split_opts
->max_commits
;
1810 prepare_commit_graph_one(ctx
->r
, ctx
->odb
);
1811 if (ctx
->r
->objects
->commit_graph
)
1812 ctx
->oids
.alloc
+= ctx
->r
->objects
->commit_graph
->num_commits
;
1815 if (ctx
->oids
.alloc
< 1024)
1816 ctx
->oids
.alloc
= 1024;
1817 ALLOC_ARRAY(ctx
->oids
.list
, ctx
->oids
.alloc
);
1819 if (ctx
->append
&& ctx
->r
->objects
->commit_graph
) {
1820 struct commit_graph
*g
= ctx
->r
->objects
->commit_graph
;
1821 for (i
= 0; i
< g
->num_commits
; i
++) {
1822 const unsigned char *hash
= g
->chunk_oid_lookup
+ g
->hash_len
* i
;
1823 hashcpy(ctx
->oids
.list
[ctx
->oids
.nr
++].hash
, hash
);
1828 if ((res
= fill_oids_from_packs(ctx
, pack_indexes
)))
1833 if ((res
= fill_oids_from_commit_hex(ctx
, commit_hex
)))
1837 if (!pack_indexes
&& !commit_hex
)
1838 fill_oids_from_all_packs(ctx
);
1840 close_reachable(ctx
);
1842 count_distinct
= count_distinct_commits(ctx
);
1844 if (count_distinct
>= GRAPH_EDGE_LAST_MASK
) {
1845 error(_("the commit graph format cannot write %d commits"), count_distinct
);
1850 ctx
->commits
.alloc
= count_distinct
;
1851 ALLOC_ARRAY(ctx
->commits
.list
, ctx
->commits
.alloc
);
1853 copy_oids_to_commits(ctx
);
1855 if (ctx
->commits
.nr
>= GRAPH_EDGE_LAST_MASK
) {
1856 error(_("too many commits to write graph"));
1861 if (!ctx
->commits
.nr
)
1865 split_graph_merge_strategy(ctx
);
1867 merge_commit_graphs(ctx
);
1869 ctx
->num_commit_graphs_after
= 1;
1871 compute_generation_numbers(ctx
);
1873 res
= write_commit_graph_file(ctx
);
1876 mark_commit_graphs(ctx
);
1878 expire_commit_graphs(ctx
);
1881 free(ctx
->graph_name
);
1882 free(ctx
->commits
.list
);
1883 free(ctx
->oids
.list
);
1885 if (ctx
->commit_graph_filenames_after
) {
1886 for (i
= 0; i
< ctx
->num_commit_graphs_after
; i
++) {
1887 free(ctx
->commit_graph_filenames_after
[i
]);
1888 free(ctx
->commit_graph_hash_after
[i
]);
1891 for (i
= 0; i
< ctx
->num_commit_graphs_before
; i
++)
1892 free(ctx
->commit_graph_filenames_before
[i
]);
1894 free(ctx
->commit_graph_filenames_after
);
1895 free(ctx
->commit_graph_filenames_before
);
1896 free(ctx
->commit_graph_hash_after
);
1904 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1905 static int verify_commit_graph_error
;
1907 static void graph_report(const char *fmt
, ...)
1911 verify_commit_graph_error
= 1;
1913 vfprintf(stderr
, fmt
, ap
);
1914 fprintf(stderr
, "\n");
1918 #define GENERATION_ZERO_EXISTS 1
1919 #define GENERATION_NUMBER_EXISTS 2
1921 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
, int flags
)
1923 uint32_t i
, cur_fanout_pos
= 0;
1924 struct object_id prev_oid
, cur_oid
, checksum
;
1925 int generation_zero
= 0;
1928 struct progress
*progress
= NULL
;
1929 int local_error
= 0;
1932 graph_report("no commit-graph file loaded");
1936 verify_commit_graph_error
= verify_commit_graph_lite(g
);
1937 if (verify_commit_graph_error
)
1938 return verify_commit_graph_error
;
1940 devnull
= open("/dev/null", O_WRONLY
);
1941 f
= hashfd(devnull
, NULL
);
1942 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
1943 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
1944 if (!hasheq(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
1945 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1946 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
1949 for (i
= 0; i
< g
->num_commits
; i
++) {
1950 struct commit
*graph_commit
;
1952 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
1954 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
1955 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1956 oid_to_hex(&prev_oid
),
1957 oid_to_hex(&cur_oid
));
1959 oidcpy(&prev_oid
, &cur_oid
);
1961 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
1962 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1964 if (i
!= fanout_value
)
1965 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1966 cur_fanout_pos
, fanout_value
, i
);
1970 graph_commit
= lookup_commit(r
, &cur_oid
);
1971 if (!parse_commit_in_graph_one(r
, g
, graph_commit
))
1972 graph_report(_("failed to parse commit %s from commit-graph"),
1973 oid_to_hex(&cur_oid
));
1976 while (cur_fanout_pos
< 256) {
1977 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1979 if (g
->num_commits
!= fanout_value
)
1980 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1981 cur_fanout_pos
, fanout_value
, i
);
1986 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
1987 return verify_commit_graph_error
;
1989 if (flags
& COMMIT_GRAPH_WRITE_PROGRESS
)
1990 progress
= start_progress(_("Verifying commits in commit graph"),
1993 for (i
= 0; i
< g
->num_commits
; i
++) {
1994 struct commit
*graph_commit
, *odb_commit
;
1995 struct commit_list
*graph_parents
, *odb_parents
;
1996 uint32_t max_generation
= 0;
1998 display_progress(progress
, i
+ 1);
1999 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
2001 graph_commit
= lookup_commit(r
, &cur_oid
);
2002 odb_commit
= (struct commit
*)create_object(r
, &cur_oid
, alloc_commit_node(r
));
2003 if (parse_commit_internal(odb_commit
, 0, 0)) {
2004 graph_report(_("failed to parse commit %s from object database for commit-graph"),
2005 oid_to_hex(&cur_oid
));
2009 if (!oideq(&get_commit_tree_in_graph_one(r
, g
, graph_commit
)->object
.oid
,
2010 get_commit_tree_oid(odb_commit
)))
2011 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2012 oid_to_hex(&cur_oid
),
2013 oid_to_hex(get_commit_tree_oid(graph_commit
)),
2014 oid_to_hex(get_commit_tree_oid(odb_commit
)));
2016 graph_parents
= graph_commit
->parents
;
2017 odb_parents
= odb_commit
->parents
;
2019 while (graph_parents
) {
2020 if (odb_parents
== NULL
) {
2021 graph_report(_("commit-graph parent list for commit %s is too long"),
2022 oid_to_hex(&cur_oid
));
2026 /* parse parent in case it is in a base graph */
2027 parse_commit_in_graph_one(r
, g
, graph_parents
->item
);
2029 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
2030 graph_report(_("commit-graph parent for %s is %s != %s"),
2031 oid_to_hex(&cur_oid
),
2032 oid_to_hex(&graph_parents
->item
->object
.oid
),
2033 oid_to_hex(&odb_parents
->item
->object
.oid
));
2035 if (graph_parents
->item
->generation
> max_generation
)
2036 max_generation
= graph_parents
->item
->generation
;
2038 graph_parents
= graph_parents
->next
;
2039 odb_parents
= odb_parents
->next
;
2042 if (odb_parents
!= NULL
)
2043 graph_report(_("commit-graph parent list for commit %s terminates early"),
2044 oid_to_hex(&cur_oid
));
2046 if (!graph_commit
->generation
) {
2047 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
2048 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2049 oid_to_hex(&cur_oid
));
2050 generation_zero
= GENERATION_ZERO_EXISTS
;
2051 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
2052 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2053 oid_to_hex(&cur_oid
));
2055 if (generation_zero
== GENERATION_ZERO_EXISTS
)
2059 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2060 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2061 * extra logic in the following condition.
2063 if (max_generation
== GENERATION_NUMBER_MAX
)
2066 if (graph_commit
->generation
!= max_generation
+ 1)
2067 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2068 oid_to_hex(&cur_oid
),
2069 graph_commit
->generation
,
2070 max_generation
+ 1);
2072 if (graph_commit
->date
!= odb_commit
->date
)
2073 graph_report(_("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
),
2074 oid_to_hex(&cur_oid
),
2078 stop_progress(&progress
);
2080 local_error
= verify_commit_graph_error
;
2082 if (!(flags
& COMMIT_GRAPH_VERIFY_SHALLOW
) && g
->base_graph
)
2083 local_error
|= verify_commit_graph(r
, g
->base_graph
, flags
);
2088 void free_commit_graph(struct commit_graph
*g
)
2092 if (g
->graph_fd
>= 0) {
2093 munmap((void *)g
->data
, g
->data_len
);
2101 void disable_commit_graph(struct repository
*r
)
2103 r
->commit_graph_disabled
= 1;