3 #include "git-compat-util.h"
10 #include "sha1-lookup.h"
11 #include "commit-graph.h"
12 #include "object-store.h"
14 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
15 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
16 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
17 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
18 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
20 #define GRAPH_DATA_WIDTH 36
22 #define GRAPH_VERSION_1 0x1
23 #define GRAPH_VERSION GRAPH_VERSION_1
25 #define GRAPH_OID_VERSION_SHA1 1
26 #define GRAPH_OID_LEN_SHA1 GIT_SHA1_RAWSZ
27 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
28 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
30 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
31 #define GRAPH_PARENT_MISSING 0x7fffffff
32 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
33 #define GRAPH_PARENT_NONE 0x70000000
35 #define GRAPH_LAST_EDGE 0x80000000
37 #define GRAPH_FANOUT_SIZE (4 * 256)
38 #define GRAPH_CHUNKLOOKUP_WIDTH 12
39 #define GRAPH_MIN_SIZE (5 * GRAPH_CHUNKLOOKUP_WIDTH + GRAPH_FANOUT_SIZE + \
42 char *get_commit_graph_filename(const char *obj_dir
)
44 return xstrfmt("%s/info/commit-graph", obj_dir
);
47 static struct commit_graph
*alloc_commit_graph(void)
49 struct commit_graph
*g
= xcalloc(1, sizeof(*g
));
55 struct commit_graph
*load_commit_graph_one(const char *graph_file
)
58 const unsigned char *data
, *chunk_lookup
;
62 struct commit_graph
*graph
;
63 int fd
= git_open(graph_file
);
64 uint64_t last_chunk_offset
;
65 uint32_t last_chunk_id
;
66 uint32_t graph_signature
;
67 unsigned char graph_version
, hash_version
;
75 graph_size
= xsize_t(st
.st_size
);
77 if (graph_size
< GRAPH_MIN_SIZE
) {
79 die("graph file %s is too small", graph_file
);
81 graph_map
= xmmap(NULL
, graph_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
82 data
= (const unsigned char *)graph_map
;
84 graph_signature
= get_be32(data
);
85 if (graph_signature
!= GRAPH_SIGNATURE
) {
86 error("graph signature %X does not match signature %X",
87 graph_signature
, GRAPH_SIGNATURE
);
91 graph_version
= *(unsigned char*)(data
+ 4);
92 if (graph_version
!= GRAPH_VERSION
) {
93 error("graph version %X does not match version %X",
94 graph_version
, GRAPH_VERSION
);
98 hash_version
= *(unsigned char*)(data
+ 5);
99 if (hash_version
!= GRAPH_OID_VERSION
) {
100 error("hash version %X does not match version %X",
101 hash_version
, GRAPH_OID_VERSION
);
105 graph
= alloc_commit_graph();
107 graph
->hash_len
= GRAPH_OID_LEN
;
108 graph
->num_chunks
= *(unsigned char*)(data
+ 6);
109 graph
->graph_fd
= fd
;
110 graph
->data
= graph_map
;
111 graph
->data_len
= graph_size
;
114 last_chunk_offset
= 8;
115 chunk_lookup
= data
+ 8;
116 for (i
= 0; i
< graph
->num_chunks
; i
++) {
117 uint32_t chunk_id
= get_be32(chunk_lookup
+ 0);
118 uint64_t chunk_offset
= get_be64(chunk_lookup
+ 4);
119 int chunk_repeated
= 0;
121 chunk_lookup
+= GRAPH_CHUNKLOOKUP_WIDTH
;
123 if (chunk_offset
> graph_size
- GIT_MAX_RAWSZ
) {
124 error("improper chunk offset %08x%08x", (uint32_t)(chunk_offset
>> 32),
125 (uint32_t)chunk_offset
);
130 case GRAPH_CHUNKID_OIDFANOUT
:
131 if (graph
->chunk_oid_fanout
)
134 graph
->chunk_oid_fanout
= (uint32_t*)(data
+ chunk_offset
);
137 case GRAPH_CHUNKID_OIDLOOKUP
:
138 if (graph
->chunk_oid_lookup
)
141 graph
->chunk_oid_lookup
= data
+ chunk_offset
;
144 case GRAPH_CHUNKID_DATA
:
145 if (graph
->chunk_commit_data
)
148 graph
->chunk_commit_data
= data
+ chunk_offset
;
151 case GRAPH_CHUNKID_LARGEEDGES
:
152 if (graph
->chunk_large_edges
)
155 graph
->chunk_large_edges
= data
+ chunk_offset
;
159 if (chunk_repeated
) {
160 error("chunk id %08x appears multiple times", chunk_id
);
164 if (last_chunk_id
== GRAPH_CHUNKID_OIDLOOKUP
)
166 graph
->num_commits
= (chunk_offset
- last_chunk_offset
)
170 last_chunk_id
= chunk_id
;
171 last_chunk_offset
= chunk_offset
;
177 munmap(graph_map
, graph_size
);
183 static struct commit_graph
*commit_graph
= NULL
;
185 static void prepare_commit_graph_one(const char *obj_dir
)
192 graph_name
= get_commit_graph_filename(obj_dir
);
193 commit_graph
= load_commit_graph_one(graph_name
);
195 FREE_AND_NULL(graph_name
);
198 static int prepare_commit_graph_run_once
= 0;
199 static void prepare_commit_graph(void)
201 struct alternate_object_database
*alt
;
204 if (prepare_commit_graph_run_once
)
206 prepare_commit_graph_run_once
= 1;
208 obj_dir
= get_object_directory();
209 prepare_commit_graph_one(obj_dir
);
210 prepare_alt_odb(the_repository
);
211 for (alt
= the_repository
->objects
->alt_odb_list
;
212 !commit_graph
&& alt
;
214 prepare_commit_graph_one(alt
->path
);
217 static void close_commit_graph(void)
222 if (commit_graph
->graph_fd
>= 0) {
223 munmap((void *)commit_graph
->data
, commit_graph
->data_len
);
224 commit_graph
->data
= NULL
;
225 close(commit_graph
->graph_fd
);
228 FREE_AND_NULL(commit_graph
);
231 static int bsearch_graph(struct commit_graph
*g
, struct object_id
*oid
, uint32_t *pos
)
233 return bsearch_hash(oid
->hash
, g
->chunk_oid_fanout
,
234 g
->chunk_oid_lookup
, g
->hash_len
, pos
);
237 static struct commit_list
**insert_parent_or_die(struct commit_graph
*g
,
239 struct commit_list
**pptr
)
242 struct object_id oid
;
243 hashcpy(oid
.hash
, g
->chunk_oid_lookup
+ g
->hash_len
* pos
);
244 c
= lookup_commit(&oid
);
246 die("could not find commit %s", oid_to_hex(&oid
));
248 return &commit_list_insert(c
, pptr
)->next
;
251 static int fill_commit_in_graph(struct commit
*item
, struct commit_graph
*g
, uint32_t pos
)
254 uint32_t *parent_data_ptr
;
255 uint64_t date_low
, date_high
;
256 struct commit_list
**pptr
;
257 const unsigned char *commit_data
= g
->chunk_commit_data
+ (g
->hash_len
+ 16) * pos
;
259 item
->object
.parsed
= 1;
260 item
->graph_pos
= pos
;
262 item
->maybe_tree
= NULL
;
264 date_high
= get_be32(commit_data
+ g
->hash_len
+ 8) & 0x3;
265 date_low
= get_be32(commit_data
+ g
->hash_len
+ 12);
266 item
->date
= (timestamp_t
)((date_high
<< 32) | date_low
);
268 pptr
= &item
->parents
;
270 edge_value
= get_be32(commit_data
+ g
->hash_len
);
271 if (edge_value
== GRAPH_PARENT_NONE
)
273 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
275 edge_value
= get_be32(commit_data
+ g
->hash_len
+ 4);
276 if (edge_value
== GRAPH_PARENT_NONE
)
278 if (!(edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
)) {
279 pptr
= insert_parent_or_die(g
, edge_value
, pptr
);
283 parent_data_ptr
= (uint32_t*)(g
->chunk_large_edges
+
284 4 * (uint64_t)(edge_value
& GRAPH_EDGE_LAST_MASK
));
286 edge_value
= get_be32(parent_data_ptr
);
287 pptr
= insert_parent_or_die(g
,
288 edge_value
& GRAPH_EDGE_LAST_MASK
,
291 } while (!(edge_value
& GRAPH_LAST_EDGE
));
296 int parse_commit_in_graph(struct commit
*item
)
298 if (!core_commit_graph
)
300 if (item
->object
.parsed
)
303 prepare_commit_graph();
307 if (item
->graph_pos
!= COMMIT_NOT_FROM_GRAPH
) {
308 pos
= item
->graph_pos
;
311 found
= bsearch_graph(commit_graph
, &(item
->object
.oid
), &pos
);
315 return fill_commit_in_graph(item
, commit_graph
, pos
);
321 static struct tree
*load_tree_for_commit(struct commit_graph
*g
, struct commit
*c
)
323 struct object_id oid
;
324 const unsigned char *commit_data
= g
->chunk_commit_data
+
325 GRAPH_DATA_WIDTH
* (c
->graph_pos
);
327 hashcpy(oid
.hash
, commit_data
);
328 c
->maybe_tree
= lookup_tree(&oid
);
330 return c
->maybe_tree
;
333 struct tree
*get_commit_tree_in_graph(const struct commit
*c
)
336 return c
->maybe_tree
;
337 if (c
->graph_pos
== COMMIT_NOT_FROM_GRAPH
)
338 BUG("get_commit_tree_in_graph called from non-commit-graph commit");
340 return load_tree_for_commit(commit_graph
, (struct commit
*)c
);
343 static void write_graph_chunk_fanout(struct hashfile
*f
,
344 struct commit
**commits
,
348 struct commit
**list
= commits
;
351 * Write the first-level table (the list is sorted,
352 * but we use a 256-entry lookup to be able to avoid
353 * having to do eight extra binary search iterations).
355 for (i
= 0; i
< 256; i
++) {
356 while (count
< nr_commits
) {
357 if ((*list
)->object
.oid
.hash
[0] != i
)
363 hashwrite_be32(f
, count
);
367 static void write_graph_chunk_oids(struct hashfile
*f
, int hash_len
,
368 struct commit
**commits
, int nr_commits
)
370 struct commit
**list
= commits
;
372 for (count
= 0; count
< nr_commits
; count
++, list
++)
373 hashwrite(f
, (*list
)->object
.oid
.hash
, (int)hash_len
);
376 static const unsigned char *commit_to_sha1(size_t index
, void *table
)
378 struct commit
**commits
= table
;
379 return commits
[index
]->object
.oid
.hash
;
382 static void write_graph_chunk_data(struct hashfile
*f
, int hash_len
,
383 struct commit
**commits
, int nr_commits
)
385 struct commit
**list
= commits
;
386 struct commit
**last
= commits
+ nr_commits
;
387 uint32_t num_extra_edges
= 0;
389 while (list
< last
) {
390 struct commit_list
*parent
;
392 uint32_t packedDate
[2];
395 hashwrite(f
, get_commit_tree_oid(*list
)->hash
, hash_len
);
397 parent
= (*list
)->parents
;
400 edge_value
= GRAPH_PARENT_NONE
;
402 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
408 edge_value
= GRAPH_PARENT_MISSING
;
411 hashwrite_be32(f
, edge_value
);
414 parent
= parent
->next
;
417 edge_value
= GRAPH_PARENT_NONE
;
418 else if (parent
->next
)
419 edge_value
= GRAPH_OCTOPUS_EDGES_NEEDED
| num_extra_edges
;
421 edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
426 edge_value
= GRAPH_PARENT_MISSING
;
429 hashwrite_be32(f
, edge_value
);
431 if (edge_value
& GRAPH_OCTOPUS_EDGES_NEEDED
) {
434 parent
= parent
->next
;
438 if (sizeof((*list
)->date
) > 4)
439 packedDate
[0] = htonl(((*list
)->date
>> 32) & 0x3);
443 packedDate
[1] = htonl((*list
)->date
);
444 hashwrite(f
, packedDate
, 8);
450 static void write_graph_chunk_large_edges(struct hashfile
*f
,
451 struct commit
**commits
,
454 struct commit
**list
= commits
;
455 struct commit
**last
= commits
+ nr_commits
;
456 struct commit_list
*parent
;
458 while (list
< last
) {
460 for (parent
= (*list
)->parents
; num_parents
< 3 && parent
;
461 parent
= parent
->next
)
464 if (num_parents
<= 2) {
469 /* Since num_parents > 2, this initializer is safe. */
470 for (parent
= (*list
)->parents
->next
; parent
; parent
= parent
->next
) {
471 int edge_value
= sha1_pos(parent
->item
->object
.oid
.hash
,
477 edge_value
= GRAPH_PARENT_MISSING
;
478 else if (!parent
->next
)
479 edge_value
|= GRAPH_LAST_EDGE
;
481 hashwrite_be32(f
, edge_value
);
488 static int commit_compare(const void *_a
, const void *_b
)
490 const struct object_id
*a
= (const struct object_id
*)_a
;
491 const struct object_id
*b
= (const struct object_id
*)_b
;
495 struct packed_commit_list
{
496 struct commit
**list
;
501 struct packed_oid_list
{
502 struct object_id
*list
;
507 static int add_packed_commits(const struct object_id
*oid
,
508 struct packed_git
*pack
,
512 struct packed_oid_list
*list
= (struct packed_oid_list
*)data
;
513 enum object_type type
;
514 off_t offset
= nth_packed_object_offset(pack
, pos
);
515 struct object_info oi
= OBJECT_INFO_INIT
;
518 if (packed_object_info(the_repository
, pack
, offset
, &oi
) < 0)
519 die("unable to get type of object %s", oid_to_hex(oid
));
521 if (type
!= OBJ_COMMIT
)
524 ALLOC_GROW(list
->list
, list
->nr
+ 1, list
->alloc
);
525 oidcpy(&(list
->list
[list
->nr
]), oid
);
531 static void add_missing_parents(struct packed_oid_list
*oids
, struct commit
*commit
)
533 struct commit_list
*parent
;
534 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
535 if (!(parent
->item
->object
.flags
& UNINTERESTING
)) {
536 ALLOC_GROW(oids
->list
, oids
->nr
+ 1, oids
->alloc
);
537 oidcpy(&oids
->list
[oids
->nr
], &(parent
->item
->object
.oid
));
539 parent
->item
->object
.flags
|= UNINTERESTING
;
544 static void close_reachable(struct packed_oid_list
*oids
)
547 struct commit
*commit
;
549 for (i
= 0; i
< oids
->nr
; i
++) {
550 commit
= lookup_commit(&oids
->list
[i
]);
552 commit
->object
.flags
|= UNINTERESTING
;
556 * As this loop runs, oids->nr may grow, but not more
557 * than the number of missing commits in the reachable
560 for (i
= 0; i
< oids
->nr
; i
++) {
561 commit
= lookup_commit(&oids
->list
[i
]);
563 if (commit
&& !parse_commit(commit
))
564 add_missing_parents(oids
, commit
);
567 for (i
= 0; i
< oids
->nr
; i
++) {
568 commit
= lookup_commit(&oids
->list
[i
]);
571 commit
->object
.flags
&= ~UNINTERESTING
;
575 void write_commit_graph(const char *obj_dir
,
576 const char **pack_indexes
,
578 const char **commit_hex
,
582 struct packed_oid_list oids
;
583 struct packed_commit_list commits
;
585 uint32_t i
, count_distinct
= 0;
588 struct lock_file lk
= LOCK_INIT
;
589 uint32_t chunk_ids
[5];
590 uint64_t chunk_offsets
[5];
593 struct commit_list
*parent
;
596 oids
.alloc
= approximate_object_count() / 4;
599 prepare_commit_graph_one(obj_dir
);
601 oids
.alloc
+= commit_graph
->num_commits
;
604 if (oids
.alloc
< 1024)
606 ALLOC_ARRAY(oids
.list
, oids
.alloc
);
608 if (append
&& commit_graph
) {
609 for (i
= 0; i
< commit_graph
->num_commits
; i
++) {
610 const unsigned char *hash
= commit_graph
->chunk_oid_lookup
+
611 commit_graph
->hash_len
* i
;
612 hashcpy(oids
.list
[oids
.nr
++].hash
, hash
);
617 struct strbuf packname
= STRBUF_INIT
;
619 strbuf_addf(&packname
, "%s/pack/", obj_dir
);
620 dirlen
= packname
.len
;
621 for (i
= 0; i
< nr_packs
; i
++) {
622 struct packed_git
*p
;
623 strbuf_setlen(&packname
, dirlen
);
624 strbuf_addstr(&packname
, pack_indexes
[i
]);
625 p
= add_packed_git(packname
.buf
, packname
.len
, 1);
627 die("error adding pack %s", packname
.buf
);
628 if (open_pack_index(p
))
629 die("error opening index for %s", packname
.buf
);
630 for_each_object_in_pack(p
, add_packed_commits
, &oids
);
633 strbuf_release(&packname
);
637 for (i
= 0; i
< nr_commits
; i
++) {
639 struct object_id oid
;
640 struct commit
*result
;
642 if (commit_hex
[i
] && parse_oid_hex(commit_hex
[i
], &oid
, &end
))
645 result
= lookup_commit_reference_gently(&oid
, 1);
648 ALLOC_GROW(oids
.list
, oids
.nr
+ 1, oids
.alloc
);
649 oidcpy(&oids
.list
[oids
.nr
], &(result
->object
.oid
));
655 if (!pack_indexes
&& !commit_hex
)
656 for_each_packed_object(add_packed_commits
, &oids
, 0);
658 close_reachable(&oids
);
660 QSORT(oids
.list
, oids
.nr
, commit_compare
);
663 for (i
= 1; i
< oids
.nr
; i
++) {
664 if (oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
668 if (count_distinct
>= GRAPH_PARENT_MISSING
)
669 die(_("the commit graph format cannot write %d commits"), count_distinct
);
672 commits
.alloc
= count_distinct
;
673 ALLOC_ARRAY(commits
.list
, commits
.alloc
);
676 for (i
= 0; i
< oids
.nr
; i
++) {
678 if (i
> 0 && !oidcmp(&oids
.list
[i
-1], &oids
.list
[i
]))
681 commits
.list
[commits
.nr
] = lookup_commit(&oids
.list
[i
]);
682 parse_commit(commits
.list
[commits
.nr
]);
684 for (parent
= commits
.list
[commits
.nr
]->parents
;
685 parent
; parent
= parent
->next
)
689 num_extra_edges
+= num_parents
- 1;
693 num_chunks
= num_extra_edges
? 4 : 3;
695 if (commits
.nr
>= GRAPH_PARENT_MISSING
)
696 die(_("too many commits to write graph"));
698 graph_name
= get_commit_graph_filename(obj_dir
);
699 fd
= hold_lock_file_for_update(&lk
, graph_name
, 0);
702 struct strbuf folder
= STRBUF_INIT
;
703 strbuf_addstr(&folder
, graph_name
);
704 strbuf_setlen(&folder
, strrchr(folder
.buf
, '/') - folder
.buf
);
706 if (mkdir(folder
.buf
, 0777) < 0)
707 die_errno(_("cannot mkdir %s"), folder
.buf
);
708 strbuf_release(&folder
);
710 fd
= hold_lock_file_for_update(&lk
, graph_name
, LOCK_DIE_ON_ERROR
);
713 die_errno("unable to create '%s'", graph_name
);
716 f
= hashfd(lk
.tempfile
->fd
, lk
.tempfile
->filename
.buf
);
718 hashwrite_be32(f
, GRAPH_SIGNATURE
);
720 hashwrite_u8(f
, GRAPH_VERSION
);
721 hashwrite_u8(f
, GRAPH_OID_VERSION
);
722 hashwrite_u8(f
, num_chunks
);
723 hashwrite_u8(f
, 0); /* unused padding byte */
725 chunk_ids
[0] = GRAPH_CHUNKID_OIDFANOUT
;
726 chunk_ids
[1] = GRAPH_CHUNKID_OIDLOOKUP
;
727 chunk_ids
[2] = GRAPH_CHUNKID_DATA
;
729 chunk_ids
[3] = GRAPH_CHUNKID_LARGEEDGES
;
734 chunk_offsets
[0] = 8 + (num_chunks
+ 1) * GRAPH_CHUNKLOOKUP_WIDTH
;
735 chunk_offsets
[1] = chunk_offsets
[0] + GRAPH_FANOUT_SIZE
;
736 chunk_offsets
[2] = chunk_offsets
[1] + GRAPH_OID_LEN
* commits
.nr
;
737 chunk_offsets
[3] = chunk_offsets
[2] + (GRAPH_OID_LEN
+ 16) * commits
.nr
;
738 chunk_offsets
[4] = chunk_offsets
[3] + 4 * num_extra_edges
;
740 for (i
= 0; i
<= num_chunks
; i
++) {
741 uint32_t chunk_write
[3];
743 chunk_write
[0] = htonl(chunk_ids
[i
]);
744 chunk_write
[1] = htonl(chunk_offsets
[i
] >> 32);
745 chunk_write
[2] = htonl(chunk_offsets
[i
] & 0xffffffff);
746 hashwrite(f
, chunk_write
, 12);
749 write_graph_chunk_fanout(f
, commits
.list
, commits
.nr
);
750 write_graph_chunk_oids(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
751 write_graph_chunk_data(f
, GRAPH_OID_LEN
, commits
.list
, commits
.nr
);
752 write_graph_chunk_large_edges(f
, commits
.list
, commits
.nr
);
754 close_commit_graph();
755 finalize_hashfile(f
, NULL
, CSUM_HASH_IN_STREAM
| CSUM_FSYNC
);
756 commit_lock_file(&lk
);