4 #include "git-compat-util.h"
12 #include "sha1-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
17 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
18 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
19 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
20 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
21 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
23 #define GRAPH_DATA_WIDTH 36
25 #define GRAPH_VERSION_1 0x1
26 #define GRAPH_VERSION GRAPH_VERSION_1
28 #define GRAPH_OID_VERSION_SHA1 1
29 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
30 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
31 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
33 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
34 #define GRAPH_PARENT_MISSING 0x7fffffff
35 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
36 #define GRAPH_PARENT_NONE 0x70000000
38 #define GRAPH_LAST_EDGE 0x80000000
40 #define GRAPH_HEADER_SIZE 8
41 #define GRAPH_FANOUT_SIZE (4 * 256)
42 #define GRAPH_CHUNKLOOKUP_WIDTH 12
43 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
44 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
46 char *get_commit_graph_filename(const char *obj_dir
)
48 return xstrfmt("%s/info/commit-graph", obj_dir
);
51 static struct commit_graph
*alloc_commit_graph(void)
53 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
59 struct commit_graph
*load_commit_graph_one(const char *graph_file
)
62 const unsigned char *data
, *chunk_lookup
;
66 struct commit_graph
*graph
;
67 int fd
= git_open(graph_file
);
68 uint64_t last_chunk_offset
;
69 uint32_t last_chunk_id
;
70 uint32_t graph_signature
;
71 unsigned char graph_version
, hash_version
;
79 graph_size
= xsize_t(st
.st_size
);
81 if (graph_size
< GRAPH_MIN_SIZE
) {
83 die("graph file %s is too small", graph_file
);
85 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
86 data
= (const unsigned char *)graph_map
;
88 graph_signature
= get_be32(data
);
89 if (graph_signature
!= GRAPH_SIGNATURE
) {
90 error("graph signature %X does not match signature %X",
91 graph_signature
, GRAPH_SIGNATURE
);
95 graph_version
= *(unsigned char*)(data
+ 4);
96 if (graph_version
!= GRAPH_VERSION
) {
97 error("graph version %X does not match version %X",
98 graph_version
, GRAPH_VERSION
);
102 hash_version
= *(unsigned char*)(data
+ 5);
103 if (hash_version
!= GRAPH_OID_VERSION
) {
104 error("hash version %X does not match version %X",
105 hash_version
, GRAPH_OID_VERSION
);
109 graph
= alloc_commit_graph();
111 graph
->hash_len
= GRAPH_OID_LEN
;
112 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
113 graph
->graph_fd
= fd
;
114 graph
->data
= graph_map
;
115 graph
->data_len
= graph_size
;
118 last_chunk_offset
= 8;
119 chunk_lookup
= data
+ 8;
120 for (i
= 0; i
< graph
->num_chunks
; i
++) {
121 uint32_t chunk_id
= get_be32(chunk_lookup
+ 0);
122 uint64_t chunk_offset
= get_be64(chunk_lookup
+ 4);
123 int chunk_repeated
= 0;
125 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
127 if (chunk_offset
> graph_size
- GIT_MAX_RAWSZ
) {
128 error("improper chunk offset %08x%08x", (uint32_t)(chunk_offset
>> 32),
129 (uint32_t)chunk_offset
);
134 case GRAPH_CHUNKID_OIDFANOUT
:
135 if (graph
->chunk_oid_fanout
)
138 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
141 case GRAPH_CHUNKID_OIDLOOKUP
:
142 if (graph
->chunk_oid_lookup
)
145 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
148 case GRAPH_CHUNKID_DATA
:
149 if (graph
->chunk_commit_data
)
152 graph
->chunk_commit_data
= data
+ chunk_offset
;
155 case GRAPH_CHUNKID_LARGEEDGES
:
156 if (graph
->chunk_large_edges
)
159 graph
->chunk_large_edges
= data
+ chunk_offset
;
163 if (chunk_repeated
) {
164 error("chunk id %08x appears multiple times", chunk_id
);
168 if (last_chunk_id
== GRAPH_CHUNKID_OIDLOOKUP
)
170 graph
->num_commits
= (chunk_offset
- last_chunk_offset
)
174 last_chunk_id
= chunk_id
;
175 last_chunk_offset
= chunk_offset
;
181 munmap(graph_map
, graph_size
);
187 static struct commit_graph
*commit_graph
= NULL
;
189 static void prepare_commit_graph_one(const char *obj_dir
)
196 graph_name
= get_commit_graph_filename(obj_dir
);
197 commit_graph
= load_commit_graph_one(graph_name
);
199 FREE_AND_NULL(graph_name
);
202 static int prepare_commit_graph_run_once
= 0;
203 static void prepare_commit_graph(void)
205 struct alternate_object_database
*alt
;
208 if (prepare_commit_graph_run_once
)
210 prepare_commit_graph_run_once
= 1;
212 obj_dir
= get_object_directory();
213 prepare_commit_graph_one(obj_dir
);
214 prepare_alt_odb(the_repository
);
215 for (alt
= the_repository
->objects
->alt_odb_list
;
216 !commit_graph
&& alt
;
218 prepare_commit_graph_one(alt
->path
);
221 static void close_commit_graph(void)
226 if (commit_graph
->graph_fd
>= 0) {
227 munmap((void *)commit_graph
->data
, commit_graph
->data_len
);
228 commit_graph
->data
= NULL
;
229 close(commit_graph
->graph_fd
);
232 FREE_AND_NULL(commit_graph
);
235 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
237 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
238 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
241 static struct commit_list
**insert_parent_or_die(struct commit_graph
*g
,
243 struct commit_list
**pptr
)
246 struct object_id oid
;
248 if (pos
>= g
->num_commits
)
249 die("invalid parent position %"PRIu64
, pos
);
251 hashcpy(oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* pos
);
252 c
= lookup_commit(&oid
);
254 die("could not find commit %s", oid_to_hex(&oid
));
256 return &commit_list_insert(c
, pptr
)->next
;
259 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
261 const unsigned char *commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* pos
;
262 item
->graph_pos
= pos
;
263 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
266 static int fill_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
269 uint32_t *parent_data_ptr
;
270 uint64_t date_low
, date_high
;
271 struct commit_list
**pptr
;
272 const unsigned char *commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * pos
;
274 item
->object
.parsed
= 1;
275 item
->graph_pos
= pos
;
277 item
->maybe_tree
= NULL
;
279 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
280 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
281 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
283 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
285 pptr
= &item
->parents
;
287 edge_value
= get_be32(commit_data
+ g
->hash_len
);
288 if (edge_value
== GRAPH_PARENT_NONE
)
290 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
292 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
293 if (edge_value
== GRAPH_PARENT_NONE
)
295 if (!(edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
)) {
296 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
300 parent_data_ptr
= (uint32_t*)(g
->chunk_large_edges
+
301 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
303 edge_value
= get_be32(parent_data_ptr
);
304 pptr
= insert_parent_or_die(g
,
305 edge_value
& GRAPH_EDGE_LAST_MASK
,
308 } while (!(edge_value
& GRAPH_LAST_EDGE
));
313 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
315 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
316 *pos
= item
->graph_pos
;
319 return bsearch_graph(g
, &(item
->object
.oid
), pos
);
323 static int parse_commit_in_graph_one(struct commit_graph
*g
, struct commit
*item
)
327 if (!core_commit_graph
)
329 if (item
->object
.parsed
)
332 if (find_commit_in_graph(item
, g
, &pos
))
333 return fill_commit_in_graph(item
, g
, pos
);
338 int parse_commit_in_graph(struct commit
*item
)
340 if (!core_commit_graph
)
343 prepare_commit_graph();
345 return parse_commit_in_graph_one(commit_graph
, item
);
349 void load_commit_graph_info(struct commit
*item
)
352 if (!core_commit_graph
)
354 prepare_commit_graph();
355 if (commit_graph
&& find_commit_in_graph(item
, commit_graph
, &pos
))
356 fill_commit_graph_info(item
, commit_graph
, pos
);
359 static struct tree
*load_tree_for_commit(struct commit_graph
*g
, struct commit
*c
)
361 struct object_id oid
;
362 const unsigned char *commit_data
= g
->chunk_commit_data
+
363 GRAPH_DATA_WIDTH
* (c
->graph_pos
);
365 hashcpy(oid
.hash
, commit_data
);
366 c
->maybe_tree
= lookup_tree(&oid
);
368 return c
->maybe_tree
;
371 static struct tree
*get_commit_tree_in_graph_one(struct commit_graph
*g
,
372 const struct commit
*c
)
375 return c
->maybe_tree
;
376 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
377 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
379 return load_tree_for_commit(g
, (struct commit
*)c
);
382 struct tree
*get_commit_tree_in_graph(const struct commit
*c
)
384 return get_commit_tree_in_graph_one(commit_graph
, c
);
387 static void write_graph_chunk_fanout(struct hashfile
*f
,
388 struct commit
**commits
,
392 struct commit
**list
= commits
;
395 * Write the first-level table (the list is sorted,
396 * but we use a 256-entry lookup to be able to avoid
397 * having to do eight extra binary search iterations).
399 for (i
= 0; i
< 256; i
++) {
400 while (count
< nr_commits
) {
401 if ((*list
)->object
.oid
.hash
[0] != i
)
407 hashwrite_be32(f
, count
);
411 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
412 struct commit
**commits
, int nr_commits
)
414 struct commit
**list
= commits
;
416 for (count
= 0; count
< nr_commits
; count
++, list
++)
417 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
420 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
422 struct commit
**commits
= table
;
423 return commits
[index
]->object
.oid
.hash
;
426 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
427 struct commit
**commits
, int nr_commits
)
429 struct commit
**list
= commits
;
430 struct commit
**last
= commits
+ nr_commits
;
431 uint32_t num_extra_edges
= 0;
433 while (list
< last
) {
434 struct commit_list
*parent
;
436 uint32_t packedDate
[2];
439 hashwrite(f
, get_commit_tree_oid(*list
)->hash
, hash_len
);
441 parent
= (*list
)->parents
;
444 edge_value
= GRAPH_PARENT_NONE
;
446 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
452 edge_value
= GRAPH_PARENT_MISSING
;
455 hashwrite_be32(f
, edge_value
);
458 parent
= parent
->next
;
461 edge_value
= GRAPH_PARENT_NONE
;
462 else if (parent
->next
)
463 edge_value
= GRAPH_OCTOPUS_EDGES_NEEDED
| num_extra_edges
;
465 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
470 edge_value
= GRAPH_PARENT_MISSING
;
473 hashwrite_be32(f
, edge_value
);
475 if (edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
) {
478 parent
= parent
->next
;
482 if (sizeof((*list
)->date
) > 4)
483 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
487 packedDate
[0] |= htonl((*list
)->generation
<< 2);
489 packedDate
[1] = htonl((*list
)->date
);
490 hashwrite(f
, packedDate
, 8);
496 static void write_graph_chunk_large_edges(struct hashfile
*f
,
497 struct commit
**commits
,
500 struct commit
**list
= commits
;
501 struct commit
**last
= commits
+ nr_commits
;
502 struct commit_list
*parent
;
504 while (list
< last
) {
506 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
507 parent
= parent
->next
)
510 if (num_parents
<= 2) {
515 /* Since num_parents > 2, this initializer is safe. */
516 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
517 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
523 edge_value
= GRAPH_PARENT_MISSING
;
524 else if (!parent
->next
)
525 edge_value
|= GRAPH_LAST_EDGE
;
527 hashwrite_be32(f
, edge_value
);
534 static int commit_compare(const void *_a
, const void *_b
)
536 const struct object_id
*a
= (const struct object_id
*)_a
;
537 const struct object_id
*b
= (const struct object_id
*)_b
;
541 struct packed_commit_list
{
542 struct commit
**list
;
547 struct packed_oid_list
{
548 struct object_id
*list
;
553 static int add_packed_commits(const struct object_id
*oid
,
554 struct packed_git
*pack
,
558 struct packed_oid_list
*list
= (struct packed_oid_list
*)data
;
559 enum object_type type
;
560 off_t offset
= nth_packed_object_offset(pack
, pos
);
561 struct object_info oi
= OBJECT_INFO_INIT
;
564 if (packed_object_info(the_repository
, pack
, offset
, &oi
) < 0)
565 die("unable to get type of object %s", oid_to_hex(oid
));
567 if (type
!= OBJ_COMMIT
)
570 ALLOC_GROW(list
->list
, list
->nr
+ 1, list
->alloc
);
571 oidcpy(&(list
->list
[list
->nr
]), oid
);
577 static void add_missing_parents(struct packed_oid_list
*oids
, struct commit
*commit
)
579 struct commit_list
*parent
;
580 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
581 if (!(parent
->item
->object
.flags
& UNINTERESTING
)) {
582 ALLOC_GROW(oids
->list
, oids
->nr
+ 1, oids
->alloc
);
583 oidcpy(&oids
->list
[oids
->nr
], &(parent
->item
->object
.oid
));
585 parent
->item
->object
.flags
|= UNINTERESTING
;
590 static void close_reachable(struct packed_oid_list
*oids
)
593 struct commit
*commit
;
595 for (i
= 0; i
< oids
->nr
; i
++) {
596 commit
= lookup_commit(&oids
->list
[i
]);
598 commit
->object
.flags
|= UNINTERESTING
;
602 * As this loop runs, oids->nr may grow, but not more
603 * than the number of missing commits in the reachable
606 for (i
= 0; i
< oids
->nr
; i
++) {
607 commit
= lookup_commit(&oids
->list
[i
]);
609 if (commit
&& !parse_commit(commit
))
610 add_missing_parents(oids
, commit
);
613 for (i
= 0; i
< oids
->nr
; i
++) {
614 commit
= lookup_commit(&oids
->list
[i
]);
617 commit
->object
.flags
&= ~UNINTERESTING
;
621 static void compute_generation_numbers(struct packed_commit_list
* commits
)
624 struct commit_list
*list
= NULL
;
626 for (i
= 0; i
< commits
->nr
; i
++) {
627 if (commits
->list
[i
]->generation
!= GENERATION_NUMBER_INFINITY
&&
628 commits
->list
[i
]->generation
!= GENERATION_NUMBER_ZERO
)
631 commit_list_insert(commits
->list
[i
], &list
);
633 struct commit
*current
= list
->item
;
634 struct commit_list
*parent
;
635 int all_parents_computed
= 1;
636 uint32_t max_generation
= 0;
638 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
639 if (parent
->item
->generation
== GENERATION_NUMBER_INFINITY
||
640 parent
->item
->generation
== GENERATION_NUMBER_ZERO
) {
641 all_parents_computed
= 0;
642 commit_list_insert(parent
->item
, &list
);
644 } else if (parent
->item
->generation
> max_generation
) {
645 max_generation
= parent
->item
->generation
;
649 if (all_parents_computed
) {
650 current
->generation
= max_generation
+ 1;
653 if (current
->generation
> GENERATION_NUMBER_MAX
)
654 current
->generation
= GENERATION_NUMBER_MAX
;
660 static int add_ref_to_list(const char *refname
,
661 const struct object_id
*oid
,
662 int flags
, void *cb_data
)
664 struct string_list
*list
= (struct string_list
*)cb_data
;
666 string_list_append(list
, oid_to_hex(oid
));
670 void write_commit_graph_reachable(const char *obj_dir
, int append
)
672 struct string_list list
;
674 string_list_init(&list
, 1);
675 for_each_ref(add_ref_to_list
, &list
);
676 write_commit_graph(obj_dir
, NULL
, &list
, append
);
679 void write_commit_graph(const char *obj_dir
,
680 struct string_list
*pack_indexes
,
681 struct string_list
*commit_hex
,
684 struct packed_oid_list oids
;
685 struct packed_commit_list commits
;
687 uint32_t i
, count_distinct
= 0;
689 struct lock_file lk
= LOCK_INIT
;
690 uint32_t chunk_ids
[5];
691 uint64_t chunk_offsets
[5];
694 struct commit_list
*parent
;
697 oids
.alloc
= approximate_object_count() / 4;
700 prepare_commit_graph_one(obj_dir
);
702 oids
.alloc
+= commit_graph
->num_commits
;
705 if (oids
.alloc
< 1024)
707 ALLOC_ARRAY(oids
.list
, oids
.alloc
);
709 if (append
&& commit_graph
) {
710 for (i
= 0; i
< commit_graph
->num_commits
; i
++) {
711 const unsigned char *hash
= commit_graph
->chunk_oid_lookup
+
712 commit_graph
->hash_len
* i
;
713 hashcpy(oids
.list
[oids
.nr
++].hash
, hash
);
718 struct strbuf packname
= STRBUF_INIT
;
720 strbuf_addf(&packname
, "%s/pack/", obj_dir
);
721 dirlen
= packname
.len
;
722 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
723 struct packed_git
*p
;
724 strbuf_setlen(&packname
, dirlen
);
725 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
726 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
728 die("error adding pack %s", packname
.buf
);
729 if (open_pack_index(p
))
730 die("error opening index for %s", packname
.buf
);
731 for_each_object_in_pack(p
, add_packed_commits
, &oids
);
734 strbuf_release(&packname
);
738 for (i
= 0; i
< commit_hex
->nr
; i
++) {
740 struct object_id oid
;
741 struct commit
*result
;
743 if (commit_hex
->items
[i
].string
&&
744 parse_oid_hex(commit_hex
->items
[i
].string
, &oid
, &end
))
747 result
= lookup_commit_reference_gently(&oid
, 1);
750 ALLOC_GROW(oids
.list
, oids
.nr
+ 1, oids
.alloc
);
751 oidcpy(&oids
.list
[oids
.nr
], &(result
->object
.oid
));
757 if (!pack_indexes
&& !commit_hex
)
758 for_each_packed_object(add_packed_commits
, &oids
, 0);
760 close_reachable(&oids
);
762 QSORT(oids
.list
, oids
.nr
, commit_compare
);
765 for (i
= 1; i
< oids
.nr
; i
++) {
766 if (oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
770 if (count_distinct
>= GRAPH_PARENT_MISSING
)
771 die(_("the commit graph format cannot write %d commits"), count_distinct
);
774 commits
.alloc
= count_distinct
;
775 ALLOC_ARRAY(commits
.list
, commits
.alloc
);
778 for (i
= 0; i
< oids
.nr
; i
++) {
780 if (i
> 0 && !oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
783 commits
.list
[commits
.nr
] = lookup_commit(&oids
.list
[i
]);
784 parse_commit(commits
.list
[commits
.nr
]);
786 for (parent
= commits
.list
[commits
.nr
]->parents
;
787 parent
; parent
= parent
->next
)
791 num_extra_edges
+= num_parents
- 1;
795 num_chunks
= num_extra_edges
? 4 : 3;
797 if (commits
.nr
>= GRAPH_PARENT_MISSING
)
798 die(_("too many commits to write graph"));
800 compute_generation_numbers(&commits
);
802 graph_name
= get_commit_graph_filename(obj_dir
);
803 if (safe_create_leading_directories(graph_name
))
804 die_errno(_("unable to create leading directories of %s"),
807 hold_lock_file_for_update(&lk
, graph_name
, LOCK_DIE_ON_ERROR
);
808 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
810 hashwrite_be32(f
, GRAPH_SIGNATURE
);
812 hashwrite_u8(f
, GRAPH_VERSION
);
813 hashwrite_u8(f
, GRAPH_OID_VERSION
);
814 hashwrite_u8(f
, num_chunks
);
815 hashwrite_u8(f
, 0); /* unused padding byte */
817 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
818 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
819 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
821 chunk_ids
[3] = GRAPH_CHUNKID_LARGEEDGES
;
826 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
827 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
828 chunk_offsets
[2] = chunk_offsets
[1] + GRAPH_OID_LEN
* commits
.nr
;
829 chunk_offsets
[3] = chunk_offsets
[2] + (GRAPH_OID_LEN
+ 16) * commits
.nr
;
830 chunk_offsets
[4] = chunk_offsets
[3] + 4 * num_extra_edges
;
832 for (i
= 0; i
<= num_chunks
; i
++) {
833 uint32_t chunk_write
[3];
835 chunk_write
[0] = htonl(chunk_ids
[i
]);
836 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
837 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
838 hashwrite(f
, chunk_write
, 12);
841 write_graph_chunk_fanout(f
, commits
.list
, commits
.nr
);
842 write_graph_chunk_oids(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
843 write_graph_chunk_data(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
844 write_graph_chunk_large_edges(f
, commits
.list
, commits
.nr
);
846 close_commit_graph();
847 finalize_hashfile(f
, NULL
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
848 commit_lock_file(&lk
);
855 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
856 static int verify_commit_graph_error
;
858 static void graph_report(const char *fmt
, ...)
862 verify_commit_graph_error
= 1;
864 vfprintf(stderr
, fmt
, ap
);
865 fprintf(stderr
, "\n");
869 #define GENERATION_ZERO_EXISTS 1
870 #define GENERATION_NUMBER_EXISTS 2
872 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
)
874 uint32_t i
, cur_fanout_pos
= 0;
875 struct object_id prev_oid
, cur_oid
, checksum
;
876 int generation_zero
= 0;
881 graph_report("no commit-graph file loaded");
885 verify_commit_graph_error
= 0;
887 if (!g
->chunk_oid_fanout
)
888 graph_report("commit-graph is missing the OID Fanout chunk");
889 if (!g
->chunk_oid_lookup
)
890 graph_report("commit-graph is missing the OID Lookup chunk");
891 if (!g
->chunk_commit_data
)
892 graph_report("commit-graph is missing the Commit Data chunk");
894 if (verify_commit_graph_error
)
895 return verify_commit_graph_error
;
897 devnull
= open("/dev/null", O_WRONLY
);
898 f
= hashfd(devnull
, NULL
);
899 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
900 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
901 if (hashcmp(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
902 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
903 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
906 for (i
= 0; i
< g
->num_commits
; i
++) {
907 struct commit
*graph_commit
;
909 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
911 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
912 graph_report("commit-graph has incorrect OID order: %s then %s",
913 oid_to_hex(&prev_oid
),
914 oid_to_hex(&cur_oid
));
916 oidcpy(&prev_oid
, &cur_oid
);
918 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
919 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
921 if (i
!= fanout_value
)
922 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
923 cur_fanout_pos
, fanout_value
, i
);
927 graph_commit
= lookup_commit(&cur_oid
);
928 if (!parse_commit_in_graph_one(g
, graph_commit
))
929 graph_report("failed to parse %s from commit-graph",
930 oid_to_hex(&cur_oid
));
933 while (cur_fanout_pos
< 256) {
934 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
936 if (g
->num_commits
!= fanout_value
)
937 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
938 cur_fanout_pos
, fanout_value
, i
);
943 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
944 return verify_commit_graph_error
;
946 for (i
= 0; i
< g
->num_commits
; i
++) {
947 struct commit
*graph_commit
, *odb_commit
;
948 struct commit_list
*graph_parents
, *odb_parents
;
949 uint32_t max_generation
= 0;
951 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
953 graph_commit
= lookup_commit(&cur_oid
);
954 odb_commit
= (struct commit
*)create_object(r
, cur_oid
.hash
, alloc_commit_node(r
));
955 if (parse_commit_internal(odb_commit
, 0, 0)) {
956 graph_report("failed to parse %s from object database",
957 oid_to_hex(&cur_oid
));
961 if (oidcmp(&get_commit_tree_in_graph_one(g
, graph_commit
)->object
.oid
,
962 get_commit_tree_oid(odb_commit
)))
963 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
964 oid_to_hex(&cur_oid
),
965 oid_to_hex(get_commit_tree_oid(graph_commit
)),
966 oid_to_hex(get_commit_tree_oid(odb_commit
)));
968 graph_parents
= graph_commit
->parents
;
969 odb_parents
= odb_commit
->parents
;
971 while (graph_parents
) {
972 if (odb_parents
== NULL
) {
973 graph_report("commit-graph parent list for commit %s is too long",
974 oid_to_hex(&cur_oid
));
978 if (oidcmp(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
979 graph_report("commit-graph parent for %s is %s != %s",
980 oid_to_hex(&cur_oid
),
981 oid_to_hex(&graph_parents
->item
->object
.oid
),
982 oid_to_hex(&odb_parents
->item
->object
.oid
));
984 if (graph_parents
->item
->generation
> max_generation
)
985 max_generation
= graph_parents
->item
->generation
;
987 graph_parents
= graph_parents
->next
;
988 odb_parents
= odb_parents
->next
;
991 if (odb_parents
!= NULL
)
992 graph_report("commit-graph parent list for commit %s terminates early",
993 oid_to_hex(&cur_oid
));
995 if (!graph_commit
->generation
) {
996 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
997 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
998 oid_to_hex(&cur_oid
));
999 generation_zero
= GENERATION_ZERO_EXISTS
;
1000 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
1001 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1002 oid_to_hex(&cur_oid
));
1004 if (generation_zero
== GENERATION_ZERO_EXISTS
)
1008 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1009 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1010 * extra logic in the following condition.
1012 if (max_generation
== GENERATION_NUMBER_MAX
)
1015 if (graph_commit
->generation
!= max_generation
+ 1)
1016 graph_report("commit-graph generation for commit %s is %u != %u",
1017 oid_to_hex(&cur_oid
),
1018 graph_commit
->generation
,
1019 max_generation
+ 1);
1021 if (graph_commit
->date
!= odb_commit
->date
)
1022 graph_report("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
,
1023 oid_to_hex(&cur_oid
),
1028 return verify_commit_graph_error
;