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_LARGEEDGES 0x45444745 /* "EDGE" */
26 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
28 #define GRAPH_VERSION_1 0x1
29 #define GRAPH_VERSION GRAPH_VERSION_1
31 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
32 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
33 #define GRAPH_PARENT_NONE 0x70000000
35 #define GRAPH_LAST_EDGE 0x80000000
37 #define GRAPH_HEADER_SIZE 8
38 #define GRAPH_FANOUT_SIZE (4 * 256)
39 #define GRAPH_CHUNKLOOKUP_WIDTH 12
40 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
41 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
43 char *get_commit_graph_filename(const char *obj_dir
)
45 return xstrfmt("%s/info/commit-graph", obj_dir
);
48 static uint8_t oid_version(void)
53 static struct commit_graph
*alloc_commit_graph(void)
55 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
61 extern int read_replace_refs
;
63 static int commit_graph_compatible(struct repository
*r
)
68 if (read_replace_refs
) {
69 prepare_replace_object(r
);
70 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
74 prepare_commit_graft(r
);
75 if (r
->parsed_objects
&& r
->parsed_objects
->grafts_nr
)
77 if (is_repository_shallow(r
))
83 struct commit_graph
*load_commit_graph_one(const char *graph_file
)
86 const unsigned char *data
, *chunk_lookup
;
90 struct commit_graph
*graph
;
91 int fd
= git_open(graph_file
);
92 uint64_t last_chunk_offset
;
93 uint32_t last_chunk_id
;
94 uint32_t graph_signature
;
95 unsigned char graph_version
, hash_version
;
103 graph_size
= xsize_t(st
.st_size
);
105 if (graph_size
< GRAPH_MIN_SIZE
) {
107 die(_("graph file %s is too small"), graph_file
);
109 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
110 data
= (const unsigned char *)graph_map
;
112 graph_signature
= get_be32(data
);
113 if (graph_signature
!= GRAPH_SIGNATURE
) {
114 error(_("graph signature %X does not match signature %X"),
115 graph_signature
, GRAPH_SIGNATURE
);
119 graph_version
= *(unsigned char*)(data
+ 4);
120 if (graph_version
!= GRAPH_VERSION
) {
121 error(_("graph version %X does not match version %X"),
122 graph_version
, GRAPH_VERSION
);
126 hash_version
= *(unsigned char*)(data
+ 5);
127 if (hash_version
!= oid_version()) {
128 error(_("hash version %X does not match version %X"),
129 hash_version
, oid_version());
133 graph
= alloc_commit_graph();
135 graph
->hash_len
= the_hash_algo
->rawsz
;
136 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
137 graph
->graph_fd
= fd
;
138 graph
->data
= graph_map
;
139 graph
->data_len
= graph_size
;
142 last_chunk_offset
= 8;
143 chunk_lookup
= data
+ 8;
144 for (i
= 0; i
< graph
->num_chunks
; i
++) {
145 uint32_t chunk_id
= get_be32(chunk_lookup
+ 0);
146 uint64_t chunk_offset
= get_be64(chunk_lookup
+ 4);
147 int chunk_repeated
= 0;
149 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
151 if (chunk_offset
> graph_size
- the_hash_algo
->rawsz
) {
152 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset
>> 32),
153 (uint32_t)chunk_offset
);
158 case GRAPH_CHUNKID_OIDFANOUT
:
159 if (graph
->chunk_oid_fanout
)
162 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
165 case GRAPH_CHUNKID_OIDLOOKUP
:
166 if (graph
->chunk_oid_lookup
)
169 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
172 case GRAPH_CHUNKID_DATA
:
173 if (graph
->chunk_commit_data
)
176 graph
->chunk_commit_data
= data
+ chunk_offset
;
179 case GRAPH_CHUNKID_LARGEEDGES
:
180 if (graph
->chunk_large_edges
)
183 graph
->chunk_large_edges
= data
+ chunk_offset
;
187 if (chunk_repeated
) {
188 error(_("chunk id %08x appears multiple times"), chunk_id
);
192 if (last_chunk_id
== GRAPH_CHUNKID_OIDLOOKUP
)
194 graph
->num_commits
= (chunk_offset
- last_chunk_offset
)
198 last_chunk_id
= chunk_id
;
199 last_chunk_offset
= chunk_offset
;
205 munmap(graph_map
, graph_size
);
210 static void prepare_commit_graph_one(struct repository
*r
, const char *obj_dir
)
214 if (r
->objects
->commit_graph
)
217 graph_name
= get_commit_graph_filename(obj_dir
);
218 r
->objects
->commit_graph
=
219 load_commit_graph_one(graph_name
);
221 FREE_AND_NULL(graph_name
);
225 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
227 * On the first invocation, this function attemps to load the commit
228 * graph if the_repository is configured to have one.
230 static int prepare_commit_graph(struct repository
*r
)
232 struct object_directory
*odb
;
235 if (r
->objects
->commit_graph_attempted
)
236 return !!r
->objects
->commit_graph
;
237 r
->objects
->commit_graph_attempted
= 1;
239 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
240 (repo_config_get_bool(r
, "core.commitgraph", &config_value
) ||
243 * This repository is not configured to use commit graphs, so
244 * do not load one. (But report commit_graph_attempted anyway
245 * so that commit graph loading is not attempted again for this
250 if (!commit_graph_compatible(r
))
254 for (odb
= r
->objects
->odb
;
255 !r
->objects
->commit_graph
&& odb
;
257 prepare_commit_graph_one(r
, odb
->path
);
258 return !!r
->objects
->commit_graph
;
261 int generation_numbers_enabled(struct repository
*r
)
263 uint32_t first_generation
;
264 struct commit_graph
*g
;
265 if (!prepare_commit_graph(r
))
268 g
= r
->objects
->commit_graph
;
273 first_generation
= get_be32(g
->chunk_commit_data
+
274 g
->hash_len
+ 8) >> 2;
276 return !!first_generation
;
279 void close_commit_graph(struct repository
*r
)
281 free_commit_graph(r
->objects
->commit_graph
);
282 r
->objects
->commit_graph
= NULL
;
285 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
287 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
288 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
291 static struct commit_list
**insert_parent_or_die(struct commit_graph
*g
,
293 struct commit_list
**pptr
)
296 struct object_id oid
;
298 if (pos
>= g
->num_commits
)
299 die("invalid parent position %"PRIu64
, pos
);
301 hashcpy(oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* pos
);
302 c
= lookup_commit(the_repository
, &oid
);
304 die(_("could not find commit %s"), oid_to_hex(&oid
));
306 return &commit_list_insert(c
, pptr
)->next
;
309 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
311 const unsigned char *commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* pos
;
312 item
->graph_pos
= pos
;
313 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
316 static int fill_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
319 uint32_t *parent_data_ptr
;
320 uint64_t date_low
, date_high
;
321 struct commit_list
**pptr
;
322 const unsigned char *commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * pos
;
324 item
->object
.parsed
= 1;
325 item
->graph_pos
= pos
;
327 item
->maybe_tree
= NULL
;
329 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
330 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
331 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
333 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
335 pptr
= &item
->parents
;
337 edge_value
= get_be32(commit_data
+ g
->hash_len
);
338 if (edge_value
== GRAPH_PARENT_NONE
)
340 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
342 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
343 if (edge_value
== GRAPH_PARENT_NONE
)
345 if (!(edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
)) {
346 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
350 parent_data_ptr
= (uint32_t*)(g
->chunk_large_edges
+
351 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
353 edge_value
= get_be32(parent_data_ptr
);
354 pptr
= insert_parent_or_die(g
,
355 edge_value
& GRAPH_EDGE_LAST_MASK
,
358 } while (!(edge_value
& GRAPH_LAST_EDGE
));
363 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
365 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
366 *pos
= item
->graph_pos
;
369 return bsearch_graph(g
, &(item
->object
.oid
), pos
);
373 static int parse_commit_in_graph_one(struct commit_graph
*g
, struct commit
*item
)
377 if (item
->object
.parsed
)
380 if (find_commit_in_graph(item
, g
, &pos
))
381 return fill_commit_in_graph(item
, g
, pos
);
386 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
388 if (!prepare_commit_graph(r
))
390 return parse_commit_in_graph_one(r
->objects
->commit_graph
, item
);
393 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
396 if (!prepare_commit_graph(r
))
398 if (find_commit_in_graph(item
, r
->objects
->commit_graph
, &pos
))
399 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
402 static struct tree
*load_tree_for_commit(struct commit_graph
*g
, struct commit
*c
)
404 struct object_id oid
;
405 const unsigned char *commit_data
= g
->chunk_commit_data
+
406 GRAPH_DATA_WIDTH
* (c
->graph_pos
);
408 hashcpy(oid
.hash
, commit_data
);
409 c
->maybe_tree
= lookup_tree(the_repository
, &oid
);
411 return c
->maybe_tree
;
414 static struct tree
*get_commit_tree_in_graph_one(struct commit_graph
*g
,
415 const struct commit
*c
)
418 return c
->maybe_tree
;
419 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
420 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
422 return load_tree_for_commit(g
, (struct commit
*)c
);
425 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
427 return get_commit_tree_in_graph_one(r
->objects
->commit_graph
, c
);
430 static void write_graph_chunk_fanout(struct hashfile
*f
,
431 struct commit
**commits
,
435 struct commit
**list
= commits
;
438 * Write the first-level table (the list is sorted,
439 * but we use a 256-entry lookup to be able to avoid
440 * having to do eight extra binary search iterations).
442 for (i
= 0; i
< 256; i
++) {
443 while (count
< nr_commits
) {
444 if ((*list
)->object
.oid
.hash
[0] != i
)
450 hashwrite_be32(f
, count
);
454 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
455 struct commit
**commits
, int nr_commits
)
457 struct commit
**list
= commits
;
459 for (count
= 0; count
< nr_commits
; count
++, list
++)
460 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
463 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
465 struct commit
**commits
= table
;
466 return commits
[index
]->object
.oid
.hash
;
469 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
470 struct commit
**commits
, int nr_commits
)
472 struct commit
**list
= commits
;
473 struct commit
**last
= commits
+ nr_commits
;
474 uint32_t num_extra_edges
= 0;
476 while (list
< last
) {
477 struct commit_list
*parent
;
479 uint32_t packedDate
[2];
482 hashwrite(f
, get_commit_tree_oid(*list
)->hash
, hash_len
);
484 parent
= (*list
)->parents
;
487 edge_value
= GRAPH_PARENT_NONE
;
489 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
495 BUG("missing parent %s for commit %s",
496 oid_to_hex(&parent
->item
->object
.oid
),
497 oid_to_hex(&(*list
)->object
.oid
));
500 hashwrite_be32(f
, edge_value
);
503 parent
= parent
->next
;
506 edge_value
= GRAPH_PARENT_NONE
;
507 else if (parent
->next
)
508 edge_value
= GRAPH_OCTOPUS_EDGES_NEEDED
| num_extra_edges
;
510 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
515 BUG("missing parent %s for commit %s",
516 oid_to_hex(&parent
->item
->object
.oid
),
517 oid_to_hex(&(*list
)->object
.oid
));
520 hashwrite_be32(f
, edge_value
);
522 if (edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
) {
525 parent
= parent
->next
;
529 if (sizeof((*list
)->date
) > 4)
530 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
534 packedDate
[0] |= htonl((*list
)->generation
<< 2);
536 packedDate
[1] = htonl((*list
)->date
);
537 hashwrite(f
, packedDate
, 8);
543 static void write_graph_chunk_large_edges(struct hashfile
*f
,
544 struct commit
**commits
,
547 struct commit
**list
= commits
;
548 struct commit
**last
= commits
+ nr_commits
;
549 struct commit_list
*parent
;
551 while (list
< last
) {
553 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
554 parent
= parent
->next
)
557 if (num_parents
<= 2) {
562 /* Since num_parents > 2, this initializer is safe. */
563 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
564 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
570 BUG("missing parent %s for commit %s",
571 oid_to_hex(&parent
->item
->object
.oid
),
572 oid_to_hex(&(*list
)->object
.oid
));
573 else if (!parent
->next
)
574 edge_value
|= GRAPH_LAST_EDGE
;
576 hashwrite_be32(f
, edge_value
);
583 static int commit_compare(const void *_a
, const void *_b
)
585 const struct object_id
*a
= (const struct object_id
*)_a
;
586 const struct object_id
*b
= (const struct object_id
*)_b
;
590 struct packed_commit_list
{
591 struct commit
**list
;
596 struct packed_oid_list
{
597 struct object_id
*list
;
600 struct progress
*progress
;
604 static int add_packed_commits(const struct object_id
*oid
,
605 struct packed_git
*pack
,
609 struct packed_oid_list
*list
= (struct packed_oid_list
*)data
;
610 enum object_type type
;
611 off_t offset
= nth_packed_object_offset(pack
, pos
);
612 struct object_info oi
= OBJECT_INFO_INIT
;
615 display_progress(list
->progress
, ++list
->progress_done
);
618 if (packed_object_info(the_repository
, pack
, offset
, &oi
) < 0)
619 die(_("unable to get type of object %s"), oid_to_hex(oid
));
621 if (type
!= OBJ_COMMIT
)
624 ALLOC_GROW(list
->list
, list
->nr
+ 1, list
->alloc
);
625 oidcpy(&(list
->list
[list
->nr
]), oid
);
631 static void add_missing_parents(struct packed_oid_list
*oids
, struct commit
*commit
)
633 struct commit_list
*parent
;
634 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
635 if (!(parent
->item
->object
.flags
& UNINTERESTING
)) {
636 ALLOC_GROW(oids
->list
, oids
->nr
+ 1, oids
->alloc
);
637 oidcpy(&oids
->list
[oids
->nr
], &(parent
->item
->object
.oid
));
639 parent
->item
->object
.flags
|= UNINTERESTING
;
644 static void close_reachable(struct packed_oid_list
*oids
, int report_progress
)
647 struct commit
*commit
;
648 struct progress
*progress
= NULL
;
651 progress
= start_delayed_progress(
652 _("Loading known commits in commit graph"), j
= 0);
653 for (i
= 0; i
< oids
->nr
; i
++) {
654 display_progress(progress
, ++j
);
655 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
657 commit
->object
.flags
|= UNINTERESTING
;
659 stop_progress(&progress
);
662 * As this loop runs, oids->nr may grow, but not more
663 * than the number of missing commits in the reachable
667 progress
= start_delayed_progress(
668 _("Expanding reachable commits in commit graph"), j
= 0);
669 for (i
= 0; i
< oids
->nr
; i
++) {
670 display_progress(progress
, ++j
);
671 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
673 if (commit
&& !parse_commit(commit
))
674 add_missing_parents(oids
, commit
);
676 stop_progress(&progress
);
679 progress
= start_delayed_progress(
680 _("Clearing commit marks in commit graph"), j
= 0);
681 for (i
= 0; i
< oids
->nr
; i
++) {
682 display_progress(progress
, ++j
);
683 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
686 commit
->object
.flags
&= ~UNINTERESTING
;
688 stop_progress(&progress
);
691 static void compute_generation_numbers(struct packed_commit_list
* commits
,
695 struct commit_list
*list
= NULL
;
696 struct progress
*progress
= NULL
;
699 progress
= start_progress(
700 _("Computing commit graph generation numbers"),
702 for (i
= 0; i
< commits
->nr
; i
++) {
703 display_progress(progress
, i
+ 1);
704 if (commits
->list
[i
]->generation
!= GENERATION_NUMBER_INFINITY
&&
705 commits
->list
[i
]->generation
!= GENERATION_NUMBER_ZERO
)
708 commit_list_insert(commits
->list
[i
], &list
);
710 struct commit
*current
= list
->item
;
711 struct commit_list
*parent
;
712 int all_parents_computed
= 1;
713 uint32_t max_generation
= 0;
715 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
716 if (parent
->item
->generation
== GENERATION_NUMBER_INFINITY
||
717 parent
->item
->generation
== GENERATION_NUMBER_ZERO
) {
718 all_parents_computed
= 0;
719 commit_list_insert(parent
->item
, &list
);
721 } else if (parent
->item
->generation
> max_generation
) {
722 max_generation
= parent
->item
->generation
;
726 if (all_parents_computed
) {
727 current
->generation
= max_generation
+ 1;
730 if (current
->generation
> GENERATION_NUMBER_MAX
)
731 current
->generation
= GENERATION_NUMBER_MAX
;
735 stop_progress(&progress
);
738 static int add_ref_to_list(const char *refname
,
739 const struct object_id
*oid
,
740 int flags
, void *cb_data
)
742 struct string_list
*list
= (struct string_list
*)cb_data
;
744 string_list_append(list
, oid_to_hex(oid
));
748 void write_commit_graph_reachable(const char *obj_dir
, int append
,
751 struct string_list list
= STRING_LIST_INIT_DUP
;
753 for_each_ref(add_ref_to_list
, &list
);
754 write_commit_graph(obj_dir
, NULL
, &list
, append
, report_progress
);
756 string_list_clear(&list
, 0);
759 void write_commit_graph(const char *obj_dir
,
760 struct string_list
*pack_indexes
,
761 struct string_list
*commit_hex
,
762 int append
, int report_progress
)
764 struct packed_oid_list oids
;
765 struct packed_commit_list commits
;
767 uint32_t i
, count_distinct
= 0;
769 struct lock_file lk
= LOCK_INIT
;
770 uint32_t chunk_ids
[5];
771 uint64_t chunk_offsets
[5];
774 struct commit_list
*parent
;
775 struct progress
*progress
= NULL
;
776 const unsigned hashsz
= the_hash_algo
->rawsz
;
778 if (!commit_graph_compatible(the_repository
))
782 oids
.alloc
= approximate_object_count() / 32;
783 oids
.progress
= NULL
;
784 oids
.progress_done
= 0;
787 prepare_commit_graph_one(the_repository
, obj_dir
);
788 if (the_repository
->objects
->commit_graph
)
789 oids
.alloc
+= the_repository
->objects
->commit_graph
->num_commits
;
792 if (oids
.alloc
< 1024)
794 ALLOC_ARRAY(oids
.list
, oids
.alloc
);
796 if (append
&& the_repository
->objects
->commit_graph
) {
797 struct commit_graph
*commit_graph
=
798 the_repository
->objects
->commit_graph
;
799 for (i
= 0; i
< commit_graph
->num_commits
; i
++) {
800 const unsigned char *hash
= commit_graph
->chunk_oid_lookup
+
801 commit_graph
->hash_len
* i
;
802 hashcpy(oids
.list
[oids
.nr
++].hash
, hash
);
807 struct strbuf packname
= STRBUF_INIT
;
809 strbuf_addf(&packname
, "%s/pack/", obj_dir
);
810 dirlen
= packname
.len
;
811 if (report_progress
) {
812 oids
.progress
= start_delayed_progress(
813 _("Finding commits for commit graph"), 0);
814 oids
.progress_done
= 0;
816 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
817 struct packed_git
*p
;
818 strbuf_setlen(&packname
, dirlen
);
819 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
820 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
822 die(_("error adding pack %s"), packname
.buf
);
823 if (open_pack_index(p
))
824 die(_("error opening index for %s"), packname
.buf
);
825 for_each_object_in_pack(p
, add_packed_commits
, &oids
, 0);
829 stop_progress(&oids
.progress
);
830 strbuf_release(&packname
);
835 progress
= start_delayed_progress(
836 _("Finding commits for commit graph"),
838 for (i
= 0; i
< commit_hex
->nr
; i
++) {
840 struct object_id oid
;
841 struct commit
*result
;
843 display_progress(progress
, i
+ 1);
844 if (commit_hex
->items
[i
].string
&&
845 parse_oid_hex(commit_hex
->items
[i
].string
, &oid
, &end
))
848 result
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
851 ALLOC_GROW(oids
.list
, oids
.nr
+ 1, oids
.alloc
);
852 oidcpy(&oids
.list
[oids
.nr
], &(result
->object
.oid
));
856 stop_progress(&progress
);
859 if (!pack_indexes
&& !commit_hex
) {
861 oids
.progress
= start_delayed_progress(
862 _("Finding commits for commit graph"), 0);
863 for_each_packed_object(add_packed_commits
, &oids
, 0);
864 stop_progress(&oids
.progress
);
867 close_reachable(&oids
, report_progress
);
869 QSORT(oids
.list
, oids
.nr
, commit_compare
);
872 for (i
= 1; i
< oids
.nr
; i
++) {
873 if (!oideq(&oids
.list
[i
- 1], &oids
.list
[i
]))
877 if (count_distinct
>= GRAPH_EDGE_LAST_MASK
)
878 die(_("the commit graph format cannot write %d commits"), count_distinct
);
881 commits
.alloc
= count_distinct
;
882 ALLOC_ARRAY(commits
.list
, commits
.alloc
);
885 for (i
= 0; i
< oids
.nr
; i
++) {
887 if (i
> 0 && oideq(&oids
.list
[i
- 1], &oids
.list
[i
]))
890 commits
.list
[commits
.nr
] = lookup_commit(the_repository
, &oids
.list
[i
]);
891 parse_commit(commits
.list
[commits
.nr
]);
893 for (parent
= commits
.list
[commits
.nr
]->parents
;
894 parent
; parent
= parent
->next
)
898 num_extra_edges
+= num_parents
- 1;
902 num_chunks
= num_extra_edges
? 4 : 3;
904 if (commits
.nr
>= GRAPH_EDGE_LAST_MASK
)
905 die(_("too many commits to write graph"));
907 compute_generation_numbers(&commits
, report_progress
);
909 graph_name
= get_commit_graph_filename(obj_dir
);
910 if (safe_create_leading_directories(graph_name
)) {
912 die_errno(_("unable to create leading directories of %s"),
916 hold_lock_file_for_update(&lk
, graph_name
, LOCK_DIE_ON_ERROR
);
917 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
919 hashwrite_be32(f
, GRAPH_SIGNATURE
);
921 hashwrite_u8(f
, GRAPH_VERSION
);
922 hashwrite_u8(f
, oid_version());
923 hashwrite_u8(f
, num_chunks
);
924 hashwrite_u8(f
, 0); /* unused padding byte */
926 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
927 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
928 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
930 chunk_ids
[3] = GRAPH_CHUNKID_LARGEEDGES
;
935 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
936 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
937 chunk_offsets
[2] = chunk_offsets
[1] + hashsz
* commits
.nr
;
938 chunk_offsets
[3] = chunk_offsets
[2] + (hashsz
+ 16) * commits
.nr
;
939 chunk_offsets
[4] = chunk_offsets
[3] + 4 * num_extra_edges
;
941 for (i
= 0; i
<= num_chunks
; i
++) {
942 uint32_t chunk_write
[3];
944 chunk_write
[0] = htonl(chunk_ids
[i
]);
945 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
946 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
947 hashwrite(f
, chunk_write
, 12);
950 write_graph_chunk_fanout(f
, commits
.list
, commits
.nr
);
951 write_graph_chunk_oids(f
, hashsz
, commits
.list
, commits
.nr
);
952 write_graph_chunk_data(f
, hashsz
, commits
.list
, commits
.nr
);
953 write_graph_chunk_large_edges(f
, commits
.list
, commits
.nr
);
955 close_commit_graph(the_repository
);
956 finalize_hashfile(f
, NULL
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
957 commit_lock_file(&lk
);
964 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
965 static int verify_commit_graph_error
;
967 static void graph_report(const char *fmt
, ...)
971 verify_commit_graph_error
= 1;
973 vfprintf(stderr
, fmt
, ap
);
974 fprintf(stderr
, "\n");
978 #define GENERATION_ZERO_EXISTS 1
979 #define GENERATION_NUMBER_EXISTS 2
981 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
)
983 uint32_t i
, cur_fanout_pos
= 0;
984 struct object_id prev_oid
, cur_oid
, checksum
;
985 int generation_zero
= 0;
988 struct progress
*progress
= NULL
;
991 graph_report("no commit-graph file loaded");
995 verify_commit_graph_error
= 0;
997 if (!g
->chunk_oid_fanout
)
998 graph_report("commit-graph is missing the OID Fanout chunk");
999 if (!g
->chunk_oid_lookup
)
1000 graph_report("commit-graph is missing the OID Lookup chunk");
1001 if (!g
->chunk_commit_data
)
1002 graph_report("commit-graph is missing the Commit Data chunk");
1004 if (verify_commit_graph_error
)
1005 return verify_commit_graph_error
;
1007 devnull
= open("/dev/null", O_WRONLY
);
1008 f
= hashfd(devnull
, NULL
);
1009 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
1010 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
1011 if (!hasheq(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
1012 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1013 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
1016 for (i
= 0; i
< g
->num_commits
; i
++) {
1017 struct commit
*graph_commit
;
1019 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
1021 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
1022 graph_report("commit-graph has incorrect OID order: %s then %s",
1023 oid_to_hex(&prev_oid
),
1024 oid_to_hex(&cur_oid
));
1026 oidcpy(&prev_oid
, &cur_oid
);
1028 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
1029 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1031 if (i
!= fanout_value
)
1032 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1033 cur_fanout_pos
, fanout_value
, i
);
1037 graph_commit
= lookup_commit(r
, &cur_oid
);
1038 if (!parse_commit_in_graph_one(g
, graph_commit
))
1039 graph_report("failed to parse %s from commit-graph",
1040 oid_to_hex(&cur_oid
));
1043 while (cur_fanout_pos
< 256) {
1044 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1046 if (g
->num_commits
!= fanout_value
)
1047 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1048 cur_fanout_pos
, fanout_value
, i
);
1053 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
1054 return verify_commit_graph_error
;
1056 progress
= start_progress(_("Verifying commits in commit graph"),
1058 for (i
= 0; i
< g
->num_commits
; i
++) {
1059 struct commit
*graph_commit
, *odb_commit
;
1060 struct commit_list
*graph_parents
, *odb_parents
;
1061 uint32_t max_generation
= 0;
1063 display_progress(progress
, i
+ 1);
1064 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
1066 graph_commit
= lookup_commit(r
, &cur_oid
);
1067 odb_commit
= (struct commit
*)create_object(r
, cur_oid
.hash
, alloc_commit_node(r
));
1068 if (parse_commit_internal(odb_commit
, 0, 0)) {
1069 graph_report("failed to parse %s from object database",
1070 oid_to_hex(&cur_oid
));
1074 if (!oideq(&get_commit_tree_in_graph_one(g
, graph_commit
)->object
.oid
,
1075 get_commit_tree_oid(odb_commit
)))
1076 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1077 oid_to_hex(&cur_oid
),
1078 oid_to_hex(get_commit_tree_oid(graph_commit
)),
1079 oid_to_hex(get_commit_tree_oid(odb_commit
)));
1081 graph_parents
= graph_commit
->parents
;
1082 odb_parents
= odb_commit
->parents
;
1084 while (graph_parents
) {
1085 if (odb_parents
== NULL
) {
1086 graph_report("commit-graph parent list for commit %s is too long",
1087 oid_to_hex(&cur_oid
));
1091 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
1092 graph_report("commit-graph parent for %s is %s != %s",
1093 oid_to_hex(&cur_oid
),
1094 oid_to_hex(&graph_parents
->item
->object
.oid
),
1095 oid_to_hex(&odb_parents
->item
->object
.oid
));
1097 if (graph_parents
->item
->generation
> max_generation
)
1098 max_generation
= graph_parents
->item
->generation
;
1100 graph_parents
= graph_parents
->next
;
1101 odb_parents
= odb_parents
->next
;
1104 if (odb_parents
!= NULL
)
1105 graph_report("commit-graph parent list for commit %s terminates early",
1106 oid_to_hex(&cur_oid
));
1108 if (!graph_commit
->generation
) {
1109 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
1110 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1111 oid_to_hex(&cur_oid
));
1112 generation_zero
= GENERATION_ZERO_EXISTS
;
1113 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
1114 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1115 oid_to_hex(&cur_oid
));
1117 if (generation_zero
== GENERATION_ZERO_EXISTS
)
1121 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1122 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1123 * extra logic in the following condition.
1125 if (max_generation
== GENERATION_NUMBER_MAX
)
1128 if (graph_commit
->generation
!= max_generation
+ 1)
1129 graph_report("commit-graph generation for commit %s is %u != %u",
1130 oid_to_hex(&cur_oid
),
1131 graph_commit
->generation
,
1132 max_generation
+ 1);
1134 if (graph_commit
->date
!= odb_commit
->date
)
1135 graph_report("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
,
1136 oid_to_hex(&cur_oid
),
1140 stop_progress(&progress
);
1142 return verify_commit_graph_error
;
1145 void free_commit_graph(struct commit_graph
*g
)
1149 if (g
->graph_fd
>= 0) {
1150 munmap((void *)g
->data
, g
->data_len
);