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 36
28 #define GRAPH_VERSION_1 0x1
29 #define GRAPH_VERSION GRAPH_VERSION_1
31 #define GRAPH_OID_VERSION_SHA1 1
32 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
33 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
34 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
36 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
37 #define GRAPH_PARENT_MISSING 0x7fffffff
38 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
39 #define GRAPH_PARENT_NONE 0x70000000
41 #define GRAPH_LAST_EDGE 0x80000000
43 #define GRAPH_HEADER_SIZE 8
44 #define GRAPH_FANOUT_SIZE (4 * 256)
45 #define GRAPH_CHUNKLOOKUP_WIDTH 12
46 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
47 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
49 char *get_commit_graph_filename(const char *obj_dir
)
51 return xstrfmt("%s/info/commit-graph", obj_dir
);
54 static struct commit_graph
*alloc_commit_graph(void)
56 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
62 extern int read_replace_refs
;
64 static int commit_graph_compatible(struct repository
*r
)
69 if (read_replace_refs
) {
70 prepare_replace_object(r
);
71 if (hashmap_get_size(&r
->objects
->replace_map
->map
))
75 prepare_commit_graft(r
);
76 if (r
->parsed_objects
&& r
->parsed_objects
->grafts_nr
)
78 if (is_repository_shallow(r
))
84 struct commit_graph
*load_commit_graph_one(const char *graph_file
)
89 struct commit_graph
*ret
;
90 int fd
= git_open(graph_file
);
98 graph_size
= xsize_t(st
.st_size
);
100 if (graph_size
< GRAPH_MIN_SIZE
) {
102 die(_("graph file %s is too small"), graph_file
);
104 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
105 ret
= parse_commit_graph(graph_map
, fd
, graph_size
);
108 munmap(graph_map
, graph_size
);
116 struct commit_graph
*parse_commit_graph(void *graph_map
, int fd
,
119 const unsigned char *data
, *chunk_lookup
;
121 struct commit_graph
*graph
;
122 uint64_t last_chunk_offset
;
123 uint32_t last_chunk_id
;
124 uint32_t graph_signature
;
125 unsigned char graph_version
, hash_version
;
130 if (graph_size
< GRAPH_MIN_SIZE
)
133 data
= (const unsigned char *)graph_map
;
135 graph_signature
= get_be32(data
);
136 if (graph_signature
!= GRAPH_SIGNATURE
) {
137 error(_("graph signature %X does not match signature %X"),
138 graph_signature
, GRAPH_SIGNATURE
);
142 graph_version
= *(unsigned char*)(data
+ 4);
143 if (graph_version
!= GRAPH_VERSION
) {
144 error(_("graph version %X does not match version %X"),
145 graph_version
, GRAPH_VERSION
);
149 hash_version
= *(unsigned char*)(data
+ 5);
150 if (hash_version
!= GRAPH_OID_VERSION
) {
151 error(_("hash version %X does not match version %X"),
152 hash_version
, GRAPH_OID_VERSION
);
156 graph
= alloc_commit_graph();
158 graph
->hash_len
= GRAPH_OID_LEN
;
159 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
160 graph
->graph_fd
= fd
;
161 graph
->data
= graph_map
;
162 graph
->data_len
= graph_size
;
165 last_chunk_offset
= 8;
166 chunk_lookup
= data
+ 8;
167 for (i
= 0; i
< graph
->num_chunks
; i
++) {
169 uint64_t chunk_offset
;
170 int chunk_repeated
= 0;
172 if (data
+ graph_size
- chunk_lookup
<
173 GRAPH_CHUNKLOOKUP_WIDTH
) {
174 error(_("chunk lookup table entry missing; graph file may be incomplete"));
179 chunk_id
= get_be32(chunk_lookup
+ 0);
180 chunk_offset
= get_be64(chunk_lookup
+ 4);
182 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
184 if (chunk_offset
> graph_size
- GIT_MAX_RAWSZ
) {
185 error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset
>> 32),
186 (uint32_t)chunk_offset
);
192 case GRAPH_CHUNKID_OIDFANOUT
:
193 if (graph
->chunk_oid_fanout
)
196 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
199 case GRAPH_CHUNKID_OIDLOOKUP
:
200 if (graph
->chunk_oid_lookup
)
203 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
206 case GRAPH_CHUNKID_DATA
:
207 if (graph
->chunk_commit_data
)
210 graph
->chunk_commit_data
= data
+ chunk_offset
;
213 case GRAPH_CHUNKID_LARGEEDGES
:
214 if (graph
->chunk_large_edges
)
217 graph
->chunk_large_edges
= data
+ chunk_offset
;
221 if (chunk_repeated
) {
222 error(_("chunk id %08x appears multiple times"), chunk_id
);
227 if (last_chunk_id
== GRAPH_CHUNKID_OIDLOOKUP
)
229 graph
->num_commits
= (chunk_offset
- last_chunk_offset
)
233 last_chunk_id
= chunk_id
;
234 last_chunk_offset
= chunk_offset
;
240 static void prepare_commit_graph_one(struct repository
*r
, const char *obj_dir
)
244 if (r
->objects
->commit_graph
)
247 graph_name
= get_commit_graph_filename(obj_dir
);
248 r
->objects
->commit_graph
=
249 load_commit_graph_one(graph_name
);
251 FREE_AND_NULL(graph_name
);
255 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
257 * On the first invocation, this function attemps to load the commit
258 * graph if the_repository is configured to have one.
260 static int prepare_commit_graph(struct repository
*r
)
262 struct object_directory
*odb
;
265 if (r
->objects
->commit_graph_attempted
)
266 return !!r
->objects
->commit_graph
;
267 r
->objects
->commit_graph_attempted
= 1;
269 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH
, 0) &&
270 (repo_config_get_bool(r
, "core.commitgraph", &config_value
) ||
273 * This repository is not configured to use commit graphs, so
274 * do not load one. (But report commit_graph_attempted anyway
275 * so that commit graph loading is not attempted again for this
280 if (!commit_graph_compatible(r
))
284 for (odb
= r
->objects
->odb
;
285 !r
->objects
->commit_graph
&& odb
;
287 prepare_commit_graph_one(r
, odb
->path
);
288 return !!r
->objects
->commit_graph
;
291 int generation_numbers_enabled(struct repository
*r
)
293 uint32_t first_generation
;
294 struct commit_graph
*g
;
295 if (!prepare_commit_graph(r
))
298 g
= r
->objects
->commit_graph
;
303 first_generation
= get_be32(g
->chunk_commit_data
+
304 g
->hash_len
+ 8) >> 2;
306 return !!first_generation
;
309 void close_commit_graph(struct repository
*r
)
311 free_commit_graph(r
->objects
->commit_graph
);
312 r
->objects
->commit_graph
= NULL
;
315 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
317 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
318 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
321 static struct commit_list
**insert_parent_or_die(struct commit_graph
*g
,
323 struct commit_list
**pptr
)
326 struct object_id oid
;
328 if (pos
>= g
->num_commits
)
329 die("invalid parent position %"PRIu64
, pos
);
331 hashcpy(oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* pos
);
332 c
= lookup_commit(the_repository
, &oid
);
334 die(_("could not find commit %s"), oid_to_hex(&oid
));
336 return &commit_list_insert(c
, pptr
)->next
;
339 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
341 const unsigned char *commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* pos
;
342 item
->graph_pos
= pos
;
343 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
346 static int fill_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
349 uint32_t *parent_data_ptr
;
350 uint64_t date_low
, date_high
;
351 struct commit_list
**pptr
;
352 const unsigned char *commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * pos
;
354 item
->object
.parsed
= 1;
355 item
->graph_pos
= pos
;
357 item
->maybe_tree
= NULL
;
359 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
360 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
361 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
363 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
365 pptr
= &item
->parents
;
367 edge_value
= get_be32(commit_data
+ g
->hash_len
);
368 if (edge_value
== GRAPH_PARENT_NONE
)
370 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
372 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
373 if (edge_value
== GRAPH_PARENT_NONE
)
375 if (!(edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
)) {
376 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
380 parent_data_ptr
= (uint32_t*)(g
->chunk_large_edges
+
381 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
383 edge_value
= get_be32(parent_data_ptr
);
384 pptr
= insert_parent_or_die(g
,
385 edge_value
& GRAPH_EDGE_LAST_MASK
,
388 } while (!(edge_value
& GRAPH_LAST_EDGE
));
393 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
395 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
396 *pos
= item
->graph_pos
;
399 return bsearch_graph(g
, &(item
->object
.oid
), pos
);
403 static int parse_commit_in_graph_one(struct commit_graph
*g
, struct commit
*item
)
407 if (item
->object
.parsed
)
410 if (find_commit_in_graph(item
, g
, &pos
))
411 return fill_commit_in_graph(item
, g
, pos
);
416 int parse_commit_in_graph(struct repository
*r
, struct commit
*item
)
418 if (!prepare_commit_graph(r
))
420 return parse_commit_in_graph_one(r
->objects
->commit_graph
, item
);
423 void load_commit_graph_info(struct repository
*r
, struct commit
*item
)
426 if (!prepare_commit_graph(r
))
428 if (find_commit_in_graph(item
, r
->objects
->commit_graph
, &pos
))
429 fill_commit_graph_info(item
, r
->objects
->commit_graph
, pos
);
432 static struct tree
*load_tree_for_commit(struct commit_graph
*g
, struct commit
*c
)
434 struct object_id oid
;
435 const unsigned char *commit_data
= g
->chunk_commit_data
+
436 GRAPH_DATA_WIDTH
* (c
->graph_pos
);
438 hashcpy(oid
.hash
, commit_data
);
439 c
->maybe_tree
= lookup_tree(the_repository
, &oid
);
441 return c
->maybe_tree
;
444 static struct tree
*get_commit_tree_in_graph_one(struct commit_graph
*g
,
445 const struct commit
*c
)
448 return c
->maybe_tree
;
449 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
450 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
452 return load_tree_for_commit(g
, (struct commit
*)c
);
455 struct tree
*get_commit_tree_in_graph(struct repository
*r
, const struct commit
*c
)
457 return get_commit_tree_in_graph_one(r
->objects
->commit_graph
, c
);
460 static void write_graph_chunk_fanout(struct hashfile
*f
,
461 struct commit
**commits
,
465 struct commit
**list
= commits
;
468 * Write the first-level table (the list is sorted,
469 * but we use a 256-entry lookup to be able to avoid
470 * having to do eight extra binary search iterations).
472 for (i
= 0; i
< 256; i
++) {
473 while (count
< nr_commits
) {
474 if ((*list
)->object
.oid
.hash
[0] != i
)
480 hashwrite_be32(f
, count
);
484 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
485 struct commit
**commits
, int nr_commits
)
487 struct commit
**list
= commits
;
489 for (count
= 0; count
< nr_commits
; count
++, list
++)
490 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
493 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
495 struct commit
**commits
= table
;
496 return commits
[index
]->object
.oid
.hash
;
499 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
500 struct commit
**commits
, int nr_commits
)
502 struct commit
**list
= commits
;
503 struct commit
**last
= commits
+ nr_commits
;
504 uint32_t num_extra_edges
= 0;
506 while (list
< last
) {
507 struct commit_list
*parent
;
509 uint32_t packedDate
[2];
512 hashwrite(f
, get_commit_tree_oid(*list
)->hash
, hash_len
);
514 parent
= (*list
)->parents
;
517 edge_value
= GRAPH_PARENT_NONE
;
519 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
525 edge_value
= GRAPH_PARENT_MISSING
;
528 hashwrite_be32(f
, edge_value
);
531 parent
= parent
->next
;
534 edge_value
= GRAPH_PARENT_NONE
;
535 else if (parent
->next
)
536 edge_value
= GRAPH_OCTOPUS_EDGES_NEEDED
| num_extra_edges
;
538 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
543 edge_value
= GRAPH_PARENT_MISSING
;
546 hashwrite_be32(f
, edge_value
);
548 if (edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
) {
551 parent
= parent
->next
;
555 if (sizeof((*list
)->date
) > 4)
556 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
560 packedDate
[0] |= htonl((*list
)->generation
<< 2);
562 packedDate
[1] = htonl((*list
)->date
);
563 hashwrite(f
, packedDate
, 8);
569 static void write_graph_chunk_large_edges(struct hashfile
*f
,
570 struct commit
**commits
,
573 struct commit
**list
= commits
;
574 struct commit
**last
= commits
+ nr_commits
;
575 struct commit_list
*parent
;
577 while (list
< last
) {
579 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
580 parent
= parent
->next
)
583 if (num_parents
<= 2) {
588 /* Since num_parents > 2, this initializer is safe. */
589 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
590 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
596 edge_value
= GRAPH_PARENT_MISSING
;
597 else if (!parent
->next
)
598 edge_value
|= GRAPH_LAST_EDGE
;
600 hashwrite_be32(f
, edge_value
);
607 static int commit_compare(const void *_a
, const void *_b
)
609 const struct object_id
*a
= (const struct object_id
*)_a
;
610 const struct object_id
*b
= (const struct object_id
*)_b
;
614 struct packed_commit_list
{
615 struct commit
**list
;
620 struct packed_oid_list
{
621 struct object_id
*list
;
624 struct progress
*progress
;
628 static int add_packed_commits(const struct object_id
*oid
,
629 struct packed_git
*pack
,
633 struct packed_oid_list
*list
= (struct packed_oid_list
*)data
;
634 enum object_type type
;
635 off_t offset
= nth_packed_object_offset(pack
, pos
);
636 struct object_info oi
= OBJECT_INFO_INIT
;
639 display_progress(list
->progress
, ++list
->progress_done
);
642 if (packed_object_info(the_repository
, pack
, offset
, &oi
) < 0)
643 die(_("unable to get type of object %s"), oid_to_hex(oid
));
645 if (type
!= OBJ_COMMIT
)
648 ALLOC_GROW(list
->list
, list
->nr
+ 1, list
->alloc
);
649 oidcpy(&(list
->list
[list
->nr
]), oid
);
655 static void add_missing_parents(struct packed_oid_list
*oids
, struct commit
*commit
)
657 struct commit_list
*parent
;
658 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
659 if (!(parent
->item
->object
.flags
& UNINTERESTING
)) {
660 ALLOC_GROW(oids
->list
, oids
->nr
+ 1, oids
->alloc
);
661 oidcpy(&oids
->list
[oids
->nr
], &(parent
->item
->object
.oid
));
663 parent
->item
->object
.flags
|= UNINTERESTING
;
668 static void close_reachable(struct packed_oid_list
*oids
, int report_progress
)
671 struct commit
*commit
;
672 struct progress
*progress
= NULL
;
675 progress
= start_delayed_progress(
676 _("Loading known commits in commit graph"), j
= 0);
677 for (i
= 0; i
< oids
->nr
; i
++) {
678 display_progress(progress
, ++j
);
679 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
681 commit
->object
.flags
|= UNINTERESTING
;
683 stop_progress(&progress
);
686 * As this loop runs, oids->nr may grow, but not more
687 * than the number of missing commits in the reachable
691 progress
= start_delayed_progress(
692 _("Expanding reachable commits in commit graph"), j
= 0);
693 for (i
= 0; i
< oids
->nr
; i
++) {
694 display_progress(progress
, ++j
);
695 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
697 if (commit
&& !parse_commit(commit
))
698 add_missing_parents(oids
, commit
);
700 stop_progress(&progress
);
703 progress
= start_delayed_progress(
704 _("Clearing commit marks in commit graph"), j
= 0);
705 for (i
= 0; i
< oids
->nr
; i
++) {
706 display_progress(progress
, ++j
);
707 commit
= lookup_commit(the_repository
, &oids
->list
[i
]);
710 commit
->object
.flags
&= ~UNINTERESTING
;
712 stop_progress(&progress
);
715 static void compute_generation_numbers(struct packed_commit_list
* commits
,
719 struct commit_list
*list
= NULL
;
720 struct progress
*progress
= NULL
;
723 progress
= start_progress(
724 _("Computing commit graph generation numbers"),
726 for (i
= 0; i
< commits
->nr
; i
++) {
727 display_progress(progress
, i
+ 1);
728 if (commits
->list
[i
]->generation
!= GENERATION_NUMBER_INFINITY
&&
729 commits
->list
[i
]->generation
!= GENERATION_NUMBER_ZERO
)
732 commit_list_insert(commits
->list
[i
], &list
);
734 struct commit
*current
= list
->item
;
735 struct commit_list
*parent
;
736 int all_parents_computed
= 1;
737 uint32_t max_generation
= 0;
739 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
740 if (parent
->item
->generation
== GENERATION_NUMBER_INFINITY
||
741 parent
->item
->generation
== GENERATION_NUMBER_ZERO
) {
742 all_parents_computed
= 0;
743 commit_list_insert(parent
->item
, &list
);
745 } else if (parent
->item
->generation
> max_generation
) {
746 max_generation
= parent
->item
->generation
;
750 if (all_parents_computed
) {
751 current
->generation
= max_generation
+ 1;
754 if (current
->generation
> GENERATION_NUMBER_MAX
)
755 current
->generation
= GENERATION_NUMBER_MAX
;
759 stop_progress(&progress
);
762 static int add_ref_to_list(const char *refname
,
763 const struct object_id
*oid
,
764 int flags
, void *cb_data
)
766 struct string_list
*list
= (struct string_list
*)cb_data
;
768 string_list_append(list
, oid_to_hex(oid
));
772 void write_commit_graph_reachable(const char *obj_dir
, int append
,
775 struct string_list list
= STRING_LIST_INIT_DUP
;
777 for_each_ref(add_ref_to_list
, &list
);
778 write_commit_graph(obj_dir
, NULL
, &list
, append
, report_progress
);
780 string_list_clear(&list
, 0);
783 void write_commit_graph(const char *obj_dir
,
784 struct string_list
*pack_indexes
,
785 struct string_list
*commit_hex
,
786 int append
, int report_progress
)
788 struct packed_oid_list oids
;
789 struct packed_commit_list commits
;
791 uint32_t i
, count_distinct
= 0;
793 struct lock_file lk
= LOCK_INIT
;
794 uint32_t chunk_ids
[5];
795 uint64_t chunk_offsets
[5];
798 struct commit_list
*parent
;
799 struct progress
*progress
= NULL
;
801 if (!commit_graph_compatible(the_repository
))
805 oids
.alloc
= approximate_object_count() / 32;
806 oids
.progress
= NULL
;
807 oids
.progress_done
= 0;
810 prepare_commit_graph_one(the_repository
, obj_dir
);
811 if (the_repository
->objects
->commit_graph
)
812 oids
.alloc
+= the_repository
->objects
->commit_graph
->num_commits
;
815 if (oids
.alloc
< 1024)
817 ALLOC_ARRAY(oids
.list
, oids
.alloc
);
819 if (append
&& the_repository
->objects
->commit_graph
) {
820 struct commit_graph
*commit_graph
=
821 the_repository
->objects
->commit_graph
;
822 for (i
= 0; i
< commit_graph
->num_commits
; i
++) {
823 const unsigned char *hash
= commit_graph
->chunk_oid_lookup
+
824 commit_graph
->hash_len
* i
;
825 hashcpy(oids
.list
[oids
.nr
++].hash
, hash
);
830 struct strbuf packname
= STRBUF_INIT
;
832 strbuf_addf(&packname
, "%s/pack/", obj_dir
);
833 dirlen
= packname
.len
;
834 if (report_progress
) {
835 oids
.progress
= start_delayed_progress(
836 _("Finding commits for commit graph"), 0);
837 oids
.progress_done
= 0;
839 for (i
= 0; i
< pack_indexes
->nr
; i
++) {
840 struct packed_git
*p
;
841 strbuf_setlen(&packname
, dirlen
);
842 strbuf_addstr(&packname
, pack_indexes
->items
[i
].string
);
843 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
845 die(_("error adding pack %s"), packname
.buf
);
846 if (open_pack_index(p
))
847 die(_("error opening index for %s"), packname
.buf
);
848 for_each_object_in_pack(p
, add_packed_commits
, &oids
, 0);
852 stop_progress(&oids
.progress
);
853 strbuf_release(&packname
);
858 progress
= start_delayed_progress(
859 _("Finding commits for commit graph"),
861 for (i
= 0; i
< commit_hex
->nr
; i
++) {
863 struct object_id oid
;
864 struct commit
*result
;
866 display_progress(progress
, i
+ 1);
867 if (commit_hex
->items
[i
].string
&&
868 parse_oid_hex(commit_hex
->items
[i
].string
, &oid
, &end
))
871 result
= lookup_commit_reference_gently(the_repository
, &oid
, 1);
874 ALLOC_GROW(oids
.list
, oids
.nr
+ 1, oids
.alloc
);
875 oidcpy(&oids
.list
[oids
.nr
], &(result
->object
.oid
));
879 stop_progress(&progress
);
882 if (!pack_indexes
&& !commit_hex
) {
884 oids
.progress
= start_delayed_progress(
885 _("Finding commits for commit graph"), 0);
886 for_each_packed_object(add_packed_commits
, &oids
, 0);
887 stop_progress(&oids
.progress
);
890 close_reachable(&oids
, report_progress
);
892 QSORT(oids
.list
, oids
.nr
, commit_compare
);
895 for (i
= 1; i
< oids
.nr
; i
++) {
896 if (!oideq(&oids
.list
[i
- 1], &oids
.list
[i
]))
900 if (count_distinct
>= GRAPH_PARENT_MISSING
)
901 die(_("the commit graph format cannot write %d commits"), count_distinct
);
904 commits
.alloc
= count_distinct
;
905 ALLOC_ARRAY(commits
.list
, commits
.alloc
);
908 for (i
= 0; i
< oids
.nr
; i
++) {
910 if (i
> 0 && oideq(&oids
.list
[i
- 1], &oids
.list
[i
]))
913 commits
.list
[commits
.nr
] = lookup_commit(the_repository
, &oids
.list
[i
]);
914 parse_commit(commits
.list
[commits
.nr
]);
916 for (parent
= commits
.list
[commits
.nr
]->parents
;
917 parent
; parent
= parent
->next
)
921 num_extra_edges
+= num_parents
- 1;
925 num_chunks
= num_extra_edges
? 4 : 3;
927 if (commits
.nr
>= GRAPH_PARENT_MISSING
)
928 die(_("too many commits to write graph"));
930 compute_generation_numbers(&commits
, report_progress
);
932 graph_name
= get_commit_graph_filename(obj_dir
);
933 if (safe_create_leading_directories(graph_name
)) {
935 die_errno(_("unable to create leading directories of %s"),
939 hold_lock_file_for_update(&lk
, graph_name
, LOCK_DIE_ON_ERROR
);
940 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
942 hashwrite_be32(f
, GRAPH_SIGNATURE
);
944 hashwrite_u8(f
, GRAPH_VERSION
);
945 hashwrite_u8(f
, GRAPH_OID_VERSION
);
946 hashwrite_u8(f
, num_chunks
);
947 hashwrite_u8(f
, 0); /* unused padding byte */
949 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
950 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
951 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
953 chunk_ids
[3] = GRAPH_CHUNKID_LARGEEDGES
;
958 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
959 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
960 chunk_offsets
[2] = chunk_offsets
[1] + GRAPH_OID_LEN
* commits
.nr
;
961 chunk_offsets
[3] = chunk_offsets
[2] + (GRAPH_OID_LEN
+ 16) * commits
.nr
;
962 chunk_offsets
[4] = chunk_offsets
[3] + 4 * num_extra_edges
;
964 for (i
= 0; i
<= num_chunks
; i
++) {
965 uint32_t chunk_write
[3];
967 chunk_write
[0] = htonl(chunk_ids
[i
]);
968 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
969 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
970 hashwrite(f
, chunk_write
, 12);
973 write_graph_chunk_fanout(f
, commits
.list
, commits
.nr
);
974 write_graph_chunk_oids(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
975 write_graph_chunk_data(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
976 write_graph_chunk_large_edges(f
, commits
.list
, commits
.nr
);
978 close_commit_graph(the_repository
);
979 finalize_hashfile(f
, NULL
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
980 commit_lock_file(&lk
);
987 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
988 static int verify_commit_graph_error
;
990 static void graph_report(const char *fmt
, ...)
994 verify_commit_graph_error
= 1;
996 vfprintf(stderr
, fmt
, ap
);
997 fprintf(stderr
, "\n");
1001 #define GENERATION_ZERO_EXISTS 1
1002 #define GENERATION_NUMBER_EXISTS 2
1004 int verify_commit_graph(struct repository
*r
, struct commit_graph
*g
)
1006 uint32_t i
, cur_fanout_pos
= 0;
1007 struct object_id prev_oid
, cur_oid
, checksum
;
1008 int generation_zero
= 0;
1011 struct progress
*progress
= NULL
;
1014 graph_report("no commit-graph file loaded");
1018 verify_commit_graph_error
= 0;
1020 if (!g
->chunk_oid_fanout
)
1021 graph_report("commit-graph is missing the OID Fanout chunk");
1022 if (!g
->chunk_oid_lookup
)
1023 graph_report("commit-graph is missing the OID Lookup chunk");
1024 if (!g
->chunk_commit_data
)
1025 graph_report("commit-graph is missing the Commit Data chunk");
1027 if (verify_commit_graph_error
)
1028 return verify_commit_graph_error
;
1030 devnull
= open("/dev/null", O_WRONLY
);
1031 f
= hashfd(devnull
, NULL
);
1032 hashwrite(f
, g
->data
, g
->data_len
- g
->hash_len
);
1033 finalize_hashfile(f
, checksum
.hash
, CSUM_CLOSE
);
1034 if (!hasheq(checksum
.hash
, g
->data
+ g
->data_len
- g
->hash_len
)) {
1035 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1036 verify_commit_graph_error
= VERIFY_COMMIT_GRAPH_ERROR_HASH
;
1039 for (i
= 0; i
< g
->num_commits
; i
++) {
1040 struct commit
*graph_commit
;
1042 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
1044 if (i
&& oidcmp(&prev_oid
, &cur_oid
) >= 0)
1045 graph_report("commit-graph has incorrect OID order: %s then %s",
1046 oid_to_hex(&prev_oid
),
1047 oid_to_hex(&cur_oid
));
1049 oidcpy(&prev_oid
, &cur_oid
);
1051 while (cur_oid
.hash
[0] > cur_fanout_pos
) {
1052 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1054 if (i
!= fanout_value
)
1055 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1056 cur_fanout_pos
, fanout_value
, i
);
1060 graph_commit
= lookup_commit(r
, &cur_oid
);
1061 if (!parse_commit_in_graph_one(g
, graph_commit
))
1062 graph_report("failed to parse %s from commit-graph",
1063 oid_to_hex(&cur_oid
));
1066 while (cur_fanout_pos
< 256) {
1067 uint32_t fanout_value
= get_be32(g
->chunk_oid_fanout
+ cur_fanout_pos
);
1069 if (g
->num_commits
!= fanout_value
)
1070 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1071 cur_fanout_pos
, fanout_value
, i
);
1076 if (verify_commit_graph_error
& ~VERIFY_COMMIT_GRAPH_ERROR_HASH
)
1077 return verify_commit_graph_error
;
1079 progress
= start_progress(_("Verifying commits in commit graph"),
1081 for (i
= 0; i
< g
->num_commits
; i
++) {
1082 struct commit
*graph_commit
, *odb_commit
;
1083 struct commit_list
*graph_parents
, *odb_parents
;
1084 uint32_t max_generation
= 0;
1086 display_progress(progress
, i
+ 1);
1087 hashcpy(cur_oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* i
);
1089 graph_commit
= lookup_commit(r
, &cur_oid
);
1090 odb_commit
= (struct commit
*)create_object(r
, cur_oid
.hash
, alloc_commit_node(r
));
1091 if (parse_commit_internal(odb_commit
, 0, 0)) {
1092 graph_report("failed to parse %s from object database",
1093 oid_to_hex(&cur_oid
));
1097 if (!oideq(&get_commit_tree_in_graph_one(g
, graph_commit
)->object
.oid
,
1098 get_commit_tree_oid(odb_commit
)))
1099 graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1100 oid_to_hex(&cur_oid
),
1101 oid_to_hex(get_commit_tree_oid(graph_commit
)),
1102 oid_to_hex(get_commit_tree_oid(odb_commit
)));
1104 graph_parents
= graph_commit
->parents
;
1105 odb_parents
= odb_commit
->parents
;
1107 while (graph_parents
) {
1108 if (odb_parents
== NULL
) {
1109 graph_report("commit-graph parent list for commit %s is too long",
1110 oid_to_hex(&cur_oid
));
1114 if (!oideq(&graph_parents
->item
->object
.oid
, &odb_parents
->item
->object
.oid
))
1115 graph_report("commit-graph parent for %s is %s != %s",
1116 oid_to_hex(&cur_oid
),
1117 oid_to_hex(&graph_parents
->item
->object
.oid
),
1118 oid_to_hex(&odb_parents
->item
->object
.oid
));
1120 if (graph_parents
->item
->generation
> max_generation
)
1121 max_generation
= graph_parents
->item
->generation
;
1123 graph_parents
= graph_parents
->next
;
1124 odb_parents
= odb_parents
->next
;
1127 if (odb_parents
!= NULL
)
1128 graph_report("commit-graph parent list for commit %s terminates early",
1129 oid_to_hex(&cur_oid
));
1131 if (!graph_commit
->generation
) {
1132 if (generation_zero
== GENERATION_NUMBER_EXISTS
)
1133 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1134 oid_to_hex(&cur_oid
));
1135 generation_zero
= GENERATION_ZERO_EXISTS
;
1136 } else if (generation_zero
== GENERATION_ZERO_EXISTS
)
1137 graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1138 oid_to_hex(&cur_oid
));
1140 if (generation_zero
== GENERATION_ZERO_EXISTS
)
1144 * If one of our parents has generation GENERATION_NUMBER_MAX, then
1145 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1146 * extra logic in the following condition.
1148 if (max_generation
== GENERATION_NUMBER_MAX
)
1151 if (graph_commit
->generation
!= max_generation
+ 1)
1152 graph_report("commit-graph generation for commit %s is %u != %u",
1153 oid_to_hex(&cur_oid
),
1154 graph_commit
->generation
,
1155 max_generation
+ 1);
1157 if (graph_commit
->date
!= odb_commit
->date
)
1158 graph_report("commit date for commit %s in commit-graph is %"PRItime
" != %"PRItime
,
1159 oid_to_hex(&cur_oid
),
1163 stop_progress(&progress
);
1165 return verify_commit_graph_error
;
1168 void free_commit_graph(struct commit_graph
*g
)
1172 if (g
->graph_fd
>= 0) {
1173 munmap((void *)g
->data
, g
->data_len
);