4 #include "git-compat-util.h"
11 #include "sha1-lookup.h"
12 #include "commit-graph.h"
13 #include "object-store.h"
15 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
16 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
17 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
18 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
19 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
21 #define GRAPH_DATA_WIDTH 36
23 #define GRAPH_VERSION_1 0x1
24 #define GRAPH_VERSION GRAPH_VERSION_1
26 #define GRAPH_OID_VERSION_SHA1 1
27 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
28 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
29 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
31 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
32 #define GRAPH_PARENT_MISSING 0x7fffffff
33 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
34 #define GRAPH_PARENT_NONE 0x70000000
36 #define GRAPH_LAST_EDGE 0x80000000
38 #define GRAPH_FANOUT_SIZE (4 * 256)
39 #define GRAPH_CHUNKLOOKUP_WIDTH 12
40 #define GRAPH_MIN_SIZE (5 * GRAPH_CHUNKLOOKUP_WIDTH + GRAPH_FANOUT_SIZE + \
43 char *get_commit_graph_filename(const char *obj_dir
)
45 return xstrfmt("%s/info/commit-graph", obj_dir
);
48 static struct commit_graph
*alloc_commit_graph(void)
50 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
56 struct commit_graph
*load_commit_graph_one(const char *graph_file
)
59 const unsigned char *data
, *chunk_lookup
;
63 struct commit_graph
*graph
;
64 int fd
= git_open(graph_file
);
65 uint64_t last_chunk_offset
;
66 uint32_t last_chunk_id
;
67 uint32_t graph_signature
;
68 unsigned char graph_version
, hash_version
;
76 graph_size
= xsize_t(st
.st_size
);
78 if (graph_size
< GRAPH_MIN_SIZE
) {
80 die("graph file %s is too small", graph_file
);
82 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
83 data
= (const unsigned char *)graph_map
;
85 graph_signature
= get_be32(data
);
86 if (graph_signature
!= GRAPH_SIGNATURE
) {
87 error("graph signature %X does not match signature %X",
88 graph_signature
, GRAPH_SIGNATURE
);
92 graph_version
= *(unsigned char*)(data
+ 4);
93 if (graph_version
!= GRAPH_VERSION
) {
94 error("graph version %X does not match version %X",
95 graph_version
, GRAPH_VERSION
);
99 hash_version
= *(unsigned char*)(data
+ 5);
100 if (hash_version
!= GRAPH_OID_VERSION
) {
101 error("hash version %X does not match version %X",
102 hash_version
, GRAPH_OID_VERSION
);
106 graph
= alloc_commit_graph();
108 graph
->hash_len
= GRAPH_OID_LEN
;
109 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
110 graph
->graph_fd
= fd
;
111 graph
->data
= graph_map
;
112 graph
->data_len
= graph_size
;
115 last_chunk_offset
= 8;
116 chunk_lookup
= data
+ 8;
117 for (i
= 0; i
< graph
->num_chunks
; i
++) {
118 uint32_t chunk_id
= get_be32(chunk_lookup
+ 0);
119 uint64_t chunk_offset
= get_be64(chunk_lookup
+ 4);
120 int chunk_repeated
= 0;
122 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
124 if (chunk_offset
> graph_size
- GIT_MAX_RAWSZ
) {
125 error("improper chunk offset %08x%08x", (uint32_t)(chunk_offset
>> 32),
126 (uint32_t)chunk_offset
);
131 case GRAPH_CHUNKID_OIDFANOUT
:
132 if (graph
->chunk_oid_fanout
)
135 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
138 case GRAPH_CHUNKID_OIDLOOKUP
:
139 if (graph
->chunk_oid_lookup
)
142 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
145 case GRAPH_CHUNKID_DATA
:
146 if (graph
->chunk_commit_data
)
149 graph
->chunk_commit_data
= data
+ chunk_offset
;
152 case GRAPH_CHUNKID_LARGEEDGES
:
153 if (graph
->chunk_large_edges
)
156 graph
->chunk_large_edges
= data
+ chunk_offset
;
160 if (chunk_repeated
) {
161 error("chunk id %08x appears multiple times", chunk_id
);
165 if (last_chunk_id
== GRAPH_CHUNKID_OIDLOOKUP
)
167 graph
->num_commits
= (chunk_offset
- last_chunk_offset
)
171 last_chunk_id
= chunk_id
;
172 last_chunk_offset
= chunk_offset
;
178 munmap(graph_map
, graph_size
);
184 static struct commit_graph
*commit_graph
= NULL
;
186 static void prepare_commit_graph_one(const char *obj_dir
)
193 graph_name
= get_commit_graph_filename(obj_dir
);
194 commit_graph
= load_commit_graph_one(graph_name
);
196 FREE_AND_NULL(graph_name
);
199 static int prepare_commit_graph_run_once
= 0;
200 static void prepare_commit_graph(void)
202 struct alternate_object_database
*alt
;
205 if (prepare_commit_graph_run_once
)
207 prepare_commit_graph_run_once
= 1;
209 obj_dir
= get_object_directory();
210 prepare_commit_graph_one(obj_dir
);
211 prepare_alt_odb(the_repository
);
212 for (alt
= the_repository
->objects
->alt_odb_list
;
213 !commit_graph
&& alt
;
215 prepare_commit_graph_one(alt
->path
);
218 static void close_commit_graph(void)
223 if (commit_graph
->graph_fd
>= 0) {
224 munmap((void *)commit_graph
->data
, commit_graph
->data_len
);
225 commit_graph
->data
= NULL
;
226 close(commit_graph
->graph_fd
);
229 FREE_AND_NULL(commit_graph
);
232 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
234 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
235 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
238 static struct commit_list
**insert_parent_or_die(struct commit_graph
*g
,
240 struct commit_list
**pptr
)
243 struct object_id oid
;
244 hashcpy(oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* pos
);
245 c
= lookup_commit(&oid
);
247 die("could not find commit %s", oid_to_hex(&oid
));
249 return &commit_list_insert(c
, pptr
)->next
;
252 static void fill_commit_graph_info(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
254 const unsigned char *commit_data
= g
->chunk_commit_data
+ GRAPH_DATA_WIDTH
* pos
;
255 item
->graph_pos
= pos
;
256 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
259 static int fill_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
262 uint32_t *parent_data_ptr
;
263 uint64_t date_low
, date_high
;
264 struct commit_list
**pptr
;
265 const unsigned char *commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * pos
;
267 item
->object
.parsed
= 1;
268 item
->graph_pos
= pos
;
270 item
->maybe_tree
= NULL
;
272 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
273 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
274 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
276 item
->generation
= get_be32(commit_data
+ g
->hash_len
+ 8) >> 2;
278 pptr
= &item
->parents
;
280 edge_value
= get_be32(commit_data
+ g
->hash_len
);
281 if (edge_value
== GRAPH_PARENT_NONE
)
283 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
285 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
286 if (edge_value
== GRAPH_PARENT_NONE
)
288 if (!(edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
)) {
289 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
293 parent_data_ptr
= (uint32_t*)(g
->chunk_large_edges
+
294 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
296 edge_value
= get_be32(parent_data_ptr
);
297 pptr
= insert_parent_or_die(g
,
298 edge_value
& GRAPH_EDGE_LAST_MASK
,
301 } while (!(edge_value
& GRAPH_LAST_EDGE
));
306 static int find_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t *pos
)
308 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
309 *pos
= item
->graph_pos
;
312 return bsearch_graph(g
, &(item
->object
.oid
), pos
);
316 int parse_commit_in_graph(struct commit
*item
)
320 if (!core_commit_graph
)
322 if (item
->object
.parsed
)
324 prepare_commit_graph();
325 if (commit_graph
&& find_commit_in_graph(item
, commit_graph
, &pos
))
326 return fill_commit_in_graph(item
, commit_graph
, pos
);
330 void load_commit_graph_info(struct commit
*item
)
333 if (!core_commit_graph
)
335 prepare_commit_graph();
336 if (commit_graph
&& find_commit_in_graph(item
, commit_graph
, &pos
))
337 fill_commit_graph_info(item
, commit_graph
, pos
);
340 static struct tree
*load_tree_for_commit(struct commit_graph
*g
, struct commit
*c
)
342 struct object_id oid
;
343 const unsigned char *commit_data
= g
->chunk_commit_data
+
344 GRAPH_DATA_WIDTH
* (c
->graph_pos
);
346 hashcpy(oid
.hash
, commit_data
);
347 c
->maybe_tree
= lookup_tree(&oid
);
349 return c
->maybe_tree
;
352 struct tree
*get_commit_tree_in_graph(const struct commit
*c
)
355 return c
->maybe_tree
;
356 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
357 BUG("get_commit_tree_in_graph called from non-commit-graph commit");
359 return load_tree_for_commit(commit_graph
, (struct commit
*)c
);
362 static void write_graph_chunk_fanout(struct hashfile
*f
,
363 struct commit
**commits
,
367 struct commit
**list
= commits
;
370 * Write the first-level table (the list is sorted,
371 * but we use a 256-entry lookup to be able to avoid
372 * having to do eight extra binary search iterations).
374 for (i
= 0; i
< 256; i
++) {
375 while (count
< nr_commits
) {
376 if ((*list
)->object
.oid
.hash
[0] != i
)
382 hashwrite_be32(f
, count
);
386 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
387 struct commit
**commits
, int nr_commits
)
389 struct commit
**list
= commits
;
391 for (count
= 0; count
< nr_commits
; count
++, list
++)
392 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
395 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
397 struct commit
**commits
= table
;
398 return commits
[index
]->object
.oid
.hash
;
401 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
402 struct commit
**commits
, int nr_commits
)
404 struct commit
**list
= commits
;
405 struct commit
**last
= commits
+ nr_commits
;
406 uint32_t num_extra_edges
= 0;
408 while (list
< last
) {
409 struct commit_list
*parent
;
411 uint32_t packedDate
[2];
414 hashwrite(f
, get_commit_tree_oid(*list
)->hash
, hash_len
);
416 parent
= (*list
)->parents
;
419 edge_value
= GRAPH_PARENT_NONE
;
421 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
427 edge_value
= GRAPH_PARENT_MISSING
;
430 hashwrite_be32(f
, edge_value
);
433 parent
= parent
->next
;
436 edge_value
= GRAPH_PARENT_NONE
;
437 else if (parent
->next
)
438 edge_value
= GRAPH_OCTOPUS_EDGES_NEEDED
| num_extra_edges
;
440 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
445 edge_value
= GRAPH_PARENT_MISSING
;
448 hashwrite_be32(f
, edge_value
);
450 if (edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
) {
453 parent
= parent
->next
;
457 if (sizeof((*list
)->date
) > 4)
458 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
462 packedDate
[0] |= htonl((*list
)->generation
<< 2);
464 packedDate
[1] = htonl((*list
)->date
);
465 hashwrite(f
, packedDate
, 8);
471 static void write_graph_chunk_large_edges(struct hashfile
*f
,
472 struct commit
**commits
,
475 struct commit
**list
= commits
;
476 struct commit
**last
= commits
+ nr_commits
;
477 struct commit_list
*parent
;
479 while (list
< last
) {
481 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
482 parent
= parent
->next
)
485 if (num_parents
<= 2) {
490 /* Since num_parents > 2, this initializer is safe. */
491 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
492 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
498 edge_value
= GRAPH_PARENT_MISSING
;
499 else if (!parent
->next
)
500 edge_value
|= GRAPH_LAST_EDGE
;
502 hashwrite_be32(f
, edge_value
);
509 static int commit_compare(const void *_a
, const void *_b
)
511 const struct object_id
*a
= (const struct object_id
*)_a
;
512 const struct object_id
*b
= (const struct object_id
*)_b
;
516 struct packed_commit_list
{
517 struct commit
**list
;
522 struct packed_oid_list
{
523 struct object_id
*list
;
528 static int add_packed_commits(const struct object_id
*oid
,
529 struct packed_git
*pack
,
533 struct packed_oid_list
*list
= (struct packed_oid_list
*)data
;
534 enum object_type type
;
535 off_t offset
= nth_packed_object_offset(pack
, pos
);
536 struct object_info oi
= OBJECT_INFO_INIT
;
539 if (packed_object_info(the_repository
, pack
, offset
, &oi
) < 0)
540 die("unable to get type of object %s", oid_to_hex(oid
));
542 if (type
!= OBJ_COMMIT
)
545 ALLOC_GROW(list
->list
, list
->nr
+ 1, list
->alloc
);
546 oidcpy(&(list
->list
[list
->nr
]), oid
);
552 static void add_missing_parents(struct packed_oid_list
*oids
, struct commit
*commit
)
554 struct commit_list
*parent
;
555 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
556 if (!(parent
->item
->object
.flags
& UNINTERESTING
)) {
557 ALLOC_GROW(oids
->list
, oids
->nr
+ 1, oids
->alloc
);
558 oidcpy(&oids
->list
[oids
->nr
], &(parent
->item
->object
.oid
));
560 parent
->item
->object
.flags
|= UNINTERESTING
;
565 static void close_reachable(struct packed_oid_list
*oids
)
568 struct commit
*commit
;
570 for (i
= 0; i
< oids
->nr
; i
++) {
571 commit
= lookup_commit(&oids
->list
[i
]);
573 commit
->object
.flags
|= UNINTERESTING
;
577 * As this loop runs, oids->nr may grow, but not more
578 * than the number of missing commits in the reachable
581 for (i
= 0; i
< oids
->nr
; i
++) {
582 commit
= lookup_commit(&oids
->list
[i
]);
584 if (commit
&& !parse_commit(commit
))
585 add_missing_parents(oids
, commit
);
588 for (i
= 0; i
< oids
->nr
; i
++) {
589 commit
= lookup_commit(&oids
->list
[i
]);
592 commit
->object
.flags
&= ~UNINTERESTING
;
596 static void compute_generation_numbers(struct packed_commit_list
* commits
)
599 struct commit_list
*list
= NULL
;
601 for (i
= 0; i
< commits
->nr
; i
++) {
602 if (commits
->list
[i
]->generation
!= GENERATION_NUMBER_INFINITY
&&
603 commits
->list
[i
]->generation
!= GENERATION_NUMBER_ZERO
)
606 commit_list_insert(commits
->list
[i
], &list
);
608 struct commit
*current
= list
->item
;
609 struct commit_list
*parent
;
610 int all_parents_computed
= 1;
611 uint32_t max_generation
= 0;
613 for (parent
= current
->parents
; parent
; parent
= parent
->next
) {
614 if (parent
->item
->generation
== GENERATION_NUMBER_INFINITY
||
615 parent
->item
->generation
== GENERATION_NUMBER_ZERO
) {
616 all_parents_computed
= 0;
617 commit_list_insert(parent
->item
, &list
);
619 } else if (parent
->item
->generation
> max_generation
) {
620 max_generation
= parent
->item
->generation
;
624 if (all_parents_computed
) {
625 current
->generation
= max_generation
+ 1;
628 if (current
->generation
> GENERATION_NUMBER_MAX
)
629 current
->generation
= GENERATION_NUMBER_MAX
;
635 void write_commit_graph(const char *obj_dir
,
636 const char **pack_indexes
,
638 const char **commit_hex
,
642 struct packed_oid_list oids
;
643 struct packed_commit_list commits
;
645 uint32_t i
, count_distinct
= 0;
647 struct lock_file lk
= LOCK_INIT
;
648 uint32_t chunk_ids
[5];
649 uint64_t chunk_offsets
[5];
652 struct commit_list
*parent
;
655 oids
.alloc
= approximate_object_count() / 4;
658 prepare_commit_graph_one(obj_dir
);
660 oids
.alloc
+= commit_graph
->num_commits
;
663 if (oids
.alloc
< 1024)
665 ALLOC_ARRAY(oids
.list
, oids
.alloc
);
667 if (append
&& commit_graph
) {
668 for (i
= 0; i
< commit_graph
->num_commits
; i
++) {
669 const unsigned char *hash
= commit_graph
->chunk_oid_lookup
+
670 commit_graph
->hash_len
* i
;
671 hashcpy(oids
.list
[oids
.nr
++].hash
, hash
);
676 struct strbuf packname
= STRBUF_INIT
;
678 strbuf_addf(&packname
, "%s/pack/", obj_dir
);
679 dirlen
= packname
.len
;
680 for (i
= 0; i
< nr_packs
; i
++) {
681 struct packed_git
*p
;
682 strbuf_setlen(&packname
, dirlen
);
683 strbuf_addstr(&packname
, pack_indexes
[i
]);
684 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
686 die("error adding pack %s", packname
.buf
);
687 if (open_pack_index(p
))
688 die("error opening index for %s", packname
.buf
);
689 for_each_object_in_pack(p
, add_packed_commits
, &oids
);
692 strbuf_release(&packname
);
696 for (i
= 0; i
< nr_commits
; i
++) {
698 struct object_id oid
;
699 struct commit
*result
;
701 if (commit_hex
[i
] && parse_oid_hex(commit_hex
[i
], &oid
, &end
))
704 result
= lookup_commit_reference_gently(&oid
, 1);
707 ALLOC_GROW(oids
.list
, oids
.nr
+ 1, oids
.alloc
);
708 oidcpy(&oids
.list
[oids
.nr
], &(result
->object
.oid
));
714 if (!pack_indexes
&& !commit_hex
)
715 for_each_packed_object(add_packed_commits
, &oids
, 0);
717 close_reachable(&oids
);
719 QSORT(oids
.list
, oids
.nr
, commit_compare
);
722 for (i
= 1; i
< oids
.nr
; i
++) {
723 if (oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
727 if (count_distinct
>= GRAPH_PARENT_MISSING
)
728 die(_("the commit graph format cannot write %d commits"), count_distinct
);
731 commits
.alloc
= count_distinct
;
732 ALLOC_ARRAY(commits
.list
, commits
.alloc
);
735 for (i
= 0; i
< oids
.nr
; i
++) {
737 if (i
> 0 && !oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
740 commits
.list
[commits
.nr
] = lookup_commit(&oids
.list
[i
]);
741 parse_commit(commits
.list
[commits
.nr
]);
743 for (parent
= commits
.list
[commits
.nr
]->parents
;
744 parent
; parent
= parent
->next
)
748 num_extra_edges
+= num_parents
- 1;
752 num_chunks
= num_extra_edges
? 4 : 3;
754 if (commits
.nr
>= GRAPH_PARENT_MISSING
)
755 die(_("too many commits to write graph"));
757 compute_generation_numbers(&commits
);
759 graph_name
= get_commit_graph_filename(obj_dir
);
760 if (safe_create_leading_directories(graph_name
))
761 die_errno(_("unable to create leading directories of %s"),
764 hold_lock_file_for_update(&lk
, graph_name
, LOCK_DIE_ON_ERROR
);
765 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
767 hashwrite_be32(f
, GRAPH_SIGNATURE
);
769 hashwrite_u8(f
, GRAPH_VERSION
);
770 hashwrite_u8(f
, GRAPH_OID_VERSION
);
771 hashwrite_u8(f
, num_chunks
);
772 hashwrite_u8(f
, 0); /* unused padding byte */
774 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
775 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
776 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
778 chunk_ids
[3] = GRAPH_CHUNKID_LARGEEDGES
;
783 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
784 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
785 chunk_offsets
[2] = chunk_offsets
[1] + GRAPH_OID_LEN
* commits
.nr
;
786 chunk_offsets
[3] = chunk_offsets
[2] + (GRAPH_OID_LEN
+ 16) * commits
.nr
;
787 chunk_offsets
[4] = chunk_offsets
[3] + 4 * num_extra_edges
;
789 for (i
= 0; i
<= num_chunks
; i
++) {
790 uint32_t chunk_write
[3];
792 chunk_write
[0] = htonl(chunk_ids
[i
]);
793 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
794 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
795 hashwrite(f
, chunk_write
, 12);
798 write_graph_chunk_fanout(f
, commits
.list
, commits
.nr
);
799 write_graph_chunk_oids(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
800 write_graph_chunk_data(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
801 write_graph_chunk_large_edges(f
, commits
.list
, commits
.nr
);
803 close_commit_graph();
804 finalize_hashfile(f
, NULL
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
805 commit_lock_file(&lk
);