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"
19 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
20 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
21 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
22 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
23 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
25 #define GRAPH_DATA_WIDTH 36
27 #define GRAPH_VERSION_1 0x1
28 #define GRAPH_VERSION GRAPH_VERSION_1
30 #define GRAPH_OID_VERSION_SHA1 1
31 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
32 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
33 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
35 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
36 #define GRAPH_PARENT_MISSING 0x7fffffff
37 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
38 #define GRAPH_PARENT_NONE 0x70000000
40 #define GRAPH_LAST_EDGE 0x80000000
42 #define GRAPH_HEADER_SIZE 8
43 #define GRAPH_FANOUT_SIZE (4 * 256)
44 #define GRAPH_CHUNKLOOKUP_WIDTH 12
45 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
46 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
48 char *get_commit_graph_filename(const char *obj_dir
)
50 return xstrfmt("%s/info/commit-graph", obj_dir
);
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
!= GRAPH_OID_VERSION
) {
128 error(_("hash version %X does not match version %X"),
129 hash_version
, GRAPH_OID_VERSION
);
133 graph
= alloc_commit_graph();
135 graph
->hash_len
= GRAPH_OID_LEN
;
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
- GIT_MAX_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 alternate_object_database
*alt
;
236 if (r
->objects
->commit_graph_attempted
)
237 return !!r
->objects
->commit_graph
;
238 r
->objects
->commit_graph_attempted
= 1;
240 if (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
))
253 obj_dir
= r
->objects
->objectdir
;
254 prepare_commit_graph_one(r
, obj_dir
);
256 for (alt
= r
->objects
->alt_odb_list
;
257 !r
->objects
->commit_graph
&& alt
;
259 prepare_commit_graph_one(r
, alt
->path
);
260 return !!r
->objects
->commit_graph
;
263 void close_commit_graph(struct repository
*r
)
265 free_commit_graph(r
->objects
->commit_graph
);
266 r
->objects
->commit_graph
= NULL
;
269 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
271 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
272 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
275 static struct commit_list
**insert_parent_or_die(struct commit_graph
*g
,
277 struct commit_list
**pptr
)
280 struct object_id oid
;
282 if (pos
>= g
->num_commits
)
283 die("invalid parent position %"PRIu64
, pos
);
285 hashcpy(oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* pos
);
286 c
= lookup_commit(the_repository
, &oid
);
288 die(_("could not find commit %s"), oid_to_hex(&oid
));
290 return &commit_list_insert(c
, pptr
)->next
;
293 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
295 const unsigned char *commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* pos
;
296 item
->graph_pos
= pos
;
297 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
300 static int fill_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
303 uint32_t *parent_data_ptr
;
304 uint64_t date_low
, date_high
;
305 struct commit_list
**pptr
;
306 const unsigned char *commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * pos
;
308 item
->object
.parsed
= 1;
309 item
->graph_pos
= pos
;
311 item
->maybe_tree
= NULL
;
313 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
314 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
315 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
317 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
319 pptr
= &item
->parents
;
321 edge_value
= get_be32(commit_data
+ g
->hash_len
);
322 if (edge_value
== GRAPH_PARENT_NONE
)
324 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
326 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
327 if (edge_value
== GRAPH_PARENT_NONE
)
329 if (!(edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
)) {
330 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
334 parent_data_ptr
= (uint32_t*)(g
->chunk_large_edges
+
335 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
337 edge_value
= get_be32(parent_data_ptr
);
338 pptr
= insert_parent_or_die(g
,
339 edge_value
& GRAPH_EDGE_LAST_MASK
,
342 } while (!(edge_value
& GRAPH_LAST_EDGE
));
347 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
349 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
350 *pos
= item
->graph_pos
;
353 return bsearch_graph(g
, &(item
->object
.oid
), pos
);
357 static int parse_commit_in_graph_one(struct commit_graph
*g
, struct commit
*item
)
361 if (item
->object
.parsed
)
364 if (find_commit_in_graph(item
, g
, &pos
))
365 return fill_commit_in_graph(item
, g
, pos
);
370 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
372 if (!prepare_commit_graph(r
))
374 return parse_commit_in_graph_one(r
->objects
->commit_graph
, item
);
377 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
380 if (!prepare_commit_graph(r
))
382 if (find_commit_in_graph(item
, r
->objects
->commit_graph
, &pos
))
383 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
386 static struct tree
*load_tree_for_commit(struct commit_graph
*g
, struct commit
*c
)
388 struct object_id oid
;
389 const unsigned char *commit_data
= g
->chunk_commit_data
+
390 GRAPH_DATA_WIDTH
* (c
->graph_pos
);
392 hashcpy(oid
.hash
, commit_data
);
393 c
->maybe_tree
= lookup_tree(the_repository
, &oid
);
395 return c
->maybe_tree
;
398 static struct tree
*get_commit_tree_in_graph_one(struct commit_graph
*g
,
399 const struct commit
*c
)
402 return c
->maybe_tree
;
403 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
404 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
406 return load_tree_for_commit(g
, (struct commit
*)c
);
409 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
411 return get_commit_tree_in_graph_one(r
->objects
->commit_graph
, c
);
414 static void write_graph_chunk_fanout(struct hashfile
*f
,
415 struct commit
**commits
,
419 struct commit
**list
= commits
;
422 * Write the first-level table (the list is sorted,
423 * but we use a 256-entry lookup to be able to avoid
424 * having to do eight extra binary search iterations).
426 for (i
= 0; i
< 256; i
++) {
427 while (count
< nr_commits
) {
428 if ((*list
)->object
.oid
.hash
[0] != i
)
434 hashwrite_be32(f
, count
);
438 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
439 struct commit
**commits
, int nr_commits
)
441 struct commit
**list
= commits
;
443 for (count
= 0; count
< nr_commits
; count
++, list
++)
444 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
447 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
449 struct commit
**commits
= table
;
450 return commits
[index
]->object
.oid
.hash
;
453 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
454 struct commit
**commits
, int nr_commits
)
456 struct commit
**list
= commits
;
457 struct commit
**last
= commits
+ nr_commits
;
458 uint32_t num_extra_edges
= 0;
460 while (list
< last
) {
461 struct commit_list
*parent
;
463 uint32_t packedDate
[2];
466 hashwrite(f
, get_commit_tree_oid(*list
)->hash
, hash_len
);
468 parent
= (*list
)->parents
;
471 edge_value
= GRAPH_PARENT_NONE
;
473 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
479 edge_value
= GRAPH_PARENT_MISSING
;
482 hashwrite_be32(f
, edge_value
);
485 parent
= parent
->next
;
488 edge_value
= GRAPH_PARENT_NONE
;
489 else if (parent
->next
)
490 edge_value
= GRAPH_OCTOPUS_EDGES_NEEDED
| num_extra_edges
;
492 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
497 edge_value
= GRAPH_PARENT_MISSING
;
500 hashwrite_be32(f
, edge_value
);
502 if (edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
) {
505 parent
= parent
->next
;
509 if (sizeof((*list
)->date
) > 4)
510 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
514 packedDate
[0] |= htonl((*list
)->generation
<< 2);
516 packedDate
[1] = htonl((*list
)->date
);
517 hashwrite(f
, packedDate
, 8);
523 static void write_graph_chunk_large_edges(struct hashfile
*f
,
524 struct commit
**commits
,
527 struct commit
**list
= commits
;
528 struct commit
**last
= commits
+ nr_commits
;
529 struct commit_list
*parent
;
531 while (list
< last
) {
533 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
534 parent
= parent
->next
)
537 if (num_parents
<= 2) {
542 /* Since num_parents > 2, this initializer is safe. */
543 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
544 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
550 edge_value
= GRAPH_PARENT_MISSING
;
551 else if (!parent
->next
)
552 edge_value
|= GRAPH_LAST_EDGE
;
554 hashwrite_be32(f
, edge_value
);
561 static int commit_compare(const void *_a
, const void *_b
)
563 const struct object_id
*a
= (const struct object_id
*)_a
;
564 const struct object_id
*b
= (const struct object_id
*)_b
;
568 struct packed_commit_list
{
569 struct commit
**list
;
574 struct packed_oid_list
{
575 struct object_id
*list
;
580 static int add_packed_commits(const struct object_id
*oid
,
581 struct packed_git
*pack
,
585 struct packed_oid_list
*list
= (struct packed_oid_list
*)data
;
586 enum object_type type
;
587 off_t offset
= nth_packed_object_offset(pack
, pos
);
588 struct object_info oi
= OBJECT_INFO_INIT
;
591 if (packed_object_info(the_repository
, pack
, offset
, &oi
) < 0)
592 die(_("unable to get type of object %s"), oid_to_hex(oid
));
594 if (type
!= OBJ_COMMIT
)
597 ALLOC_GROW(list
->list
, list
->nr
+ 1, list
->alloc
);
598 oidcpy(&(list
->list
[list
->nr
]), oid
);
604 static void add_missing_parents(struct packed_oid_list
*oids
, struct commit
*commit
)
606 struct commit_list
*parent
;
607 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
608 if (!(parent
->item
->object
.flags
& UNINTERESTING
)) {
609 ALLOC_GROW(oids
->list
, oids
->nr
+ 1, oids
->alloc
);
610 oidcpy(&oids
->list
[oids
->nr
], &(parent
->item
->object
.oid
));
612 parent
->item
->object
.flags
|= UNINTERESTING
;
617 static void close_reachable(struct packed_oid_list
*oids
)
620 struct commit
*commit
;
622 for (i
= 0; i
< oids
->nr
; i
++) {
623 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
625 commit
->object
.flags
|= UNINTERESTING
;
629 * As this loop runs, oids->nr may grow, but not more
630 * than the number of missing commits in the reachable
633 for (i
= 0; i
< oids
->nr
; i
++) {
634 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
636 if (commit
&& !parse_commit(commit
))
637 add_missing_parents(oids
, commit
);
640 for (i
= 0; i
< oids
->nr
; i
++) {
641 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
644 commit
->object
.flags
&= ~UNINTERESTING
;
648 static void compute_generation_numbers(struct packed_commit_list
* commits
)
651 struct commit_list
*list
= NULL
;
653 for (i
= 0; i
< commits
->nr
; i
++) {
654 if (commits
->list
[i
]->generation
!= GENERATION_NUMBER_INFINITY
&&
655 commits
->list
[i
]->generation
!= GENERATION_NUMBER_ZERO
)
658 commit_list_insert(commits
->list
[i
], &list
);
660 struct commit
*current
= list
->item
;
661 struct commit_list
*parent
;
662 int all_parents_computed
= 1;
663 uint32_t max_generation
= 0;
665 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
666 if (parent
->item
->generation
== GENERATION_NUMBER_INFINITY
||
667 parent
->item
->generation
== GENERATION_NUMBER_ZERO
) {
668 all_parents_computed
= 0;
669 commit_list_insert(parent
->item
, &list
);
671 } else if (parent
->item
->generation
> max_generation
) {
672 max_generation
= parent
->item
->generation
;
676 if (all_parents_computed
) {
677 current
->generation
= max_generation
+ 1;
680 if (current
->generation
> GENERATION_NUMBER_MAX
)
681 current
->generation
= GENERATION_NUMBER_MAX
;
687 static int add_ref_to_list(const char *refname
,
688 const struct object_id
*oid
,
689 int flags
, void *cb_data
)
691 struct string_list
*list
= (struct string_list
*)cb_data
;
693 string_list_append(list
, oid_to_hex(oid
));
697 void write_commit_graph_reachable(const char *obj_dir
, int append
)
699 struct string_list list
;
701 string_list_init(&list
, 1);
702 for_each_ref(add_ref_to_list
, &list
);
703 write_commit_graph(obj_dir
, NULL
, &list
, append
);
706 void write_commit_graph(const char *obj_dir
,
707 struct string_list
*pack_indexes
,
708 struct string_list
*commit_hex
,
711 struct packed_oid_list oids
;
712 struct packed_commit_list commits
;
714 uint32_t i
, count_distinct
= 0;
716 struct lock_file lk
= LOCK_INIT
;
717 uint32_t chunk_ids
[5];
718 uint64_t chunk_offsets
[5];
721 struct commit_list
*parent
;
723 if (!commit_graph_compatible(the_repository
))
727 oids
.alloc
= approximate_object_count() / 4;
730 prepare_commit_graph_one(the_repository
, obj_dir
);
731 if (the_repository
->objects
->commit_graph
)
732 oids
.alloc
+= the_repository
->objects
->commit_graph
->num_commits
;
735 if (oids
.alloc
< 1024)
737 ALLOC_ARRAY(oids
.list
, oids
.alloc
);
739 if (append
&& the_repository
->objects
->commit_graph
) {
740 struct commit_graph
*commit_graph
=
741 the_repository
->objects
->commit_graph
;
742 for (i
= 0; i
< commit_graph
->num_commits
; i
++) {
743 const unsigned char *hash
= commit_graph
->chunk_oid_lookup
+
744 commit_graph
->hash_len
* i
;
745 hashcpy(oids
.list
[oids
.nr
++].hash
, hash
);
750 struct strbuf packname
= STRBUF_INIT
;
752 strbuf_addf(&packname
, "%s/pack/", obj_dir
);
753 dirlen
= packname
.len
;
754 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
755 struct packed_git
*p
;
756 strbuf_setlen(&packname
, dirlen
);
757 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
758 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
760 die(_("error adding pack %s"), packname
.buf
);
761 if (open_pack_index(p
))
762 die(_("error opening index for %s"), packname
.buf
);
763 for_each_object_in_pack(p
, add_packed_commits
, &oids
, 0);
766 strbuf_release(&packname
);
770 for (i
= 0; i
< commit_hex
->nr
; i
++) {
772 struct object_id oid
;
773 struct commit
*result
;
775 if (commit_hex
->items
[i
].string
&&
776 parse_oid_hex(commit_hex
->items
[i
].string
, &oid
, &end
))
779 result
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
782 ALLOC_GROW(oids
.list
, oids
.nr
+ 1, oids
.alloc
);
783 oidcpy(&oids
.list
[oids
.nr
], &(result
->object
.oid
));
789 if (!pack_indexes
&& !commit_hex
)
790 for_each_packed_object(add_packed_commits
, &oids
, 0);
792 close_reachable(&oids
);
794 QSORT(oids
.list
, oids
.nr
, commit_compare
);
797 for (i
= 1; i
< oids
.nr
; i
++) {
798 if (oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
802 if (count_distinct
>= GRAPH_PARENT_MISSING
)
803 die(_("the commit graph format cannot write %d commits"), count_distinct
);
806 commits
.alloc
= count_distinct
;
807 ALLOC_ARRAY(commits
.list
, commits
.alloc
);
810 for (i
= 0; i
< oids
.nr
; i
++) {
812 if (i
> 0 && !oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
815 commits
.list
[commits
.nr
] = lookup_commit(the_repository
, &oids
.list
[i
]);
816 parse_commit(commits
.list
[commits
.nr
]);
818 for (parent
= commits
.list
[commits
.nr
]->parents
;
819 parent
; parent
= parent
->next
)
823 num_extra_edges
+= num_parents
- 1;
827 num_chunks
= num_extra_edges
? 4 : 3;
829 if (commits
.nr
>= GRAPH_PARENT_MISSING
)
830 die(_("too many commits to write graph"));
832 compute_generation_numbers(&commits
);
834 graph_name
= get_commit_graph_filename(obj_dir
);
835 if (safe_create_leading_directories(graph_name
))
836 die_errno(_("unable to create leading directories of %s"),
839 hold_lock_file_for_update(&lk
, graph_name
, LOCK_DIE_ON_ERROR
);
840 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
842 hashwrite_be32(f
, GRAPH_SIGNATURE
);
844 hashwrite_u8(f
, GRAPH_VERSION
);
845 hashwrite_u8(f
, GRAPH_OID_VERSION
);
846 hashwrite_u8(f
, num_chunks
);
847 hashwrite_u8(f
, 0); /* unused padding byte */
849 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
850 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
851 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
853 chunk_ids
[3] = GRAPH_CHUNKID_LARGEEDGES
;
858 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
859 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
860 chunk_offsets
[2] = chunk_offsets
[1] + GRAPH_OID_LEN
* commits
.nr
;
861 chunk_offsets
[3] = chunk_offsets
[2] + (GRAPH_OID_LEN
+ 16) * commits
.nr
;
862 chunk_offsets
[4] = chunk_offsets
[3] + 4 * num_extra_edges
;
864 for (i
= 0; i
<= num_chunks
; i
++) {
865 uint32_t chunk_write
[3];
867 chunk_write
[0] = htonl(chunk_ids
[i
]);
868 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
869 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
870 hashwrite(f
, chunk_write
, 12);
873 write_graph_chunk_fanout(f
, commits
.list
, commits
.nr
);
874 write_graph_chunk_oids(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
875 write_graph_chunk_data(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
876 write_graph_chunk_large_edges(f
, commits
.list
, commits
.nr
);
878 close_commit_graph(the_repository
);
879 finalize_hashfile(f
, NULL
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
880 commit_lock_file(&lk
);
887 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
888 static int verify_commit_graph_error
;
890 static void graph_report(const char *fmt
, ...)
894 verify_commit_graph_error
= 1;
896 vfprintf(stderr
, fmt
, ap
);
897 fprintf(stderr
, "\n");
901 #define GENERATION_ZERO_EXISTS 1
902 #define GENERATION_NUMBER_EXISTS 2
904 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
)
906 uint32_t i
, cur_fanout_pos
= 0;
907 struct object_id prev_oid
, cur_oid
, checksum
;
908 int generation_zero
= 0;
913 graph_report("no commit-graph file loaded");
917 verify_commit_graph_error
= 0;
919 if (!g
->chunk_oid_fanout
)
920 graph_report("commit-graph is missing the OID Fanout chunk");
921 if (!g
->chunk_oid_lookup
)
922 graph_report("commit-graph is missing the OID Lookup chunk");
923 if (!g
->chunk_commit_data
)
924 graph_report("commit-graph is missing the Commit Data chunk");
926 if (verify_commit_graph_error
)
927 return verify_commit_graph_error
;
929 devnull
= open("/dev/null", O_WRONLY
);
930 f
= hashfd(devnull
, NULL
);
931 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
932 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
933 if (hashcmp(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
934 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
935 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
938 for (i
= 0; i
< g
->num_commits
; i
++) {
939 struct commit
*graph_commit
;
941 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
943 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
944 graph_report("commit-graph has incorrect OID order: %s then %s",
945 oid_to_hex(&prev_oid
),
946 oid_to_hex(&cur_oid
));
948 oidcpy(&prev_oid
, &cur_oid
);
950 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
951 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
953 if (i
!= fanout_value
)
954 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
955 cur_fanout_pos
, fanout_value
, i
);
959 graph_commit
= lookup_commit(r
, &cur_oid
);
960 if (!parse_commit_in_graph_one(g
, graph_commit
))
961 graph_report("failed to parse %s from commit-graph",
962 oid_to_hex(&cur_oid
));
965 while (cur_fanout_pos
< 256) {
966 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
968 if (g
->num_commits
!= fanout_value
)
969 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
970 cur_fanout_pos
, fanout_value
, i
);
975 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
976 return verify_commit_graph_error
;
978 for (i
= 0; i
< g
->num_commits
; i
++) {
979 struct commit
*graph_commit
, *odb_commit
;
980 struct commit_list
*graph_parents
, *odb_parents
;
981 uint32_t max_generation
= 0;
983 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
985 graph_commit
= lookup_commit(r
, &cur_oid
);
986 odb_commit
= (struct commit
*)create_object(r
, cur_oid
.hash
, alloc_commit_node(r
));
987 if (parse_commit_internal(odb_commit
, 0, 0)) {
988 graph_report("failed to parse %s from object database",
989 oid_to_hex(&cur_oid
));
993 if (oidcmp(&get_commit_tree_in_graph_one(g
, graph_commit
)->object
.oid
,
994 get_commit_tree_oid(odb_commit
)))
995 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
996 oid_to_hex(&cur_oid
),
997 oid_to_hex(get_commit_tree_oid(graph_commit
)),
998 oid_to_hex(get_commit_tree_oid(odb_commit
)));
1000 graph_parents
= graph_commit
->parents
;
1001 odb_parents
= odb_commit
->parents
;
1003 while (graph_parents
) {
1004 if (odb_parents
== NULL
) {
1005 graph_report("commit-graph parent list for commit %s is too long",
1006 oid_to_hex(&cur_oid
));
1010 if (oidcmp(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
1011 graph_report("commit-graph parent for %s is %s != %s",
1012 oid_to_hex(&cur_oid
),
1013 oid_to_hex(&graph_parents
->item
->object
.oid
),
1014 oid_to_hex(&odb_parents
->item
->object
.oid
));
1016 if (graph_parents
->item
->generation
> max_generation
)
1017 max_generation
= graph_parents
->item
->generation
;
1019 graph_parents
= graph_parents
->next
;
1020 odb_parents
= odb_parents
->next
;
1023 if (odb_parents
!= NULL
)
1024 graph_report("commit-graph parent list for commit %s terminates early",
1025 oid_to_hex(&cur_oid
));
1027 if (!graph_commit
->generation
) {
1028 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
1029 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1030 oid_to_hex(&cur_oid
));
1031 generation_zero
= GENERATION_ZERO_EXISTS
;
1032 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
1033 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1034 oid_to_hex(&cur_oid
));
1036 if (generation_zero
== GENERATION_ZERO_EXISTS
)
1040 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1041 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1042 * extra logic in the following condition.
1044 if (max_generation
== GENERATION_NUMBER_MAX
)
1047 if (graph_commit
->generation
!= max_generation
+ 1)
1048 graph_report("commit-graph generation for commit %s is %u != %u",
1049 oid_to_hex(&cur_oid
),
1050 graph_commit
->generation
,
1051 max_generation
+ 1);
1053 if (graph_commit
->date
!= odb_commit
->date
)
1054 graph_report("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
,
1055 oid_to_hex(&cur_oid
),
1060 return verify_commit_graph_error
;
1063 void free_commit_graph(struct commit_graph
*g
)
1067 if (g
->graph_fd
>= 0) {
1068 munmap((void *)g
->data
, g
->data_len
);