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_HEADER_SIZE 8
39 #define GRAPH_FANOUT_SIZE (4 * 256)
40 #define GRAPH_CHUNKLOOKUP_WIDTH 12
41 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
42 + GRAPH_FANOUT_SIZE + GRAPH_OID_LEN)
44 char *get_commit_graph_filename(const char *obj_dir)
46 return xstrfmt("%s/info/commit-graph", obj_dir);
49 static struct commit_graph *alloc_commit_graph(void)
51 struct commit_graph *g = xcalloc(1, sizeof(*g));
57 struct commit_graph *load_commit_graph_one(const char *graph_file)
60 const unsigned char *data, *chunk_lookup;
64 struct commit_graph *graph;
65 int fd = git_open(graph_file);
66 uint64_t last_chunk_offset;
67 uint32_t last_chunk_id;
68 uint32_t graph_signature;
69 unsigned char graph_version, hash_version;
77 graph_size = xsize_t(st.st_size);
79 if (graph_size < GRAPH_MIN_SIZE) {
81 die("graph file %s is too small", graph_file);
83 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
84 data = (const unsigned char *)graph_map;
86 graph_signature = get_be32(data);
87 if (graph_signature != GRAPH_SIGNATURE) {
88 error("graph signature %X does not match signature %X",
89 graph_signature, GRAPH_SIGNATURE);
93 graph_version = *(unsigned char*)(data + 4);
94 if (graph_version != GRAPH_VERSION) {
95 error("graph version %X does not match version %X",
96 graph_version, GRAPH_VERSION);
100 hash_version = *(unsigned char*)(data + 5);
101 if (hash_version != GRAPH_OID_VERSION) {
102 error("hash version %X does not match version %X",
103 hash_version, GRAPH_OID_VERSION);
107 graph = alloc_commit_graph();
109 graph->hash_len = GRAPH_OID_LEN;
110 graph->num_chunks = *(unsigned char*)(data + 6);
111 graph->graph_fd = fd;
112 graph->data = graph_map;
113 graph->data_len = graph_size;
116 last_chunk_offset = 8;
117 chunk_lookup = data + 8;
118 for (i = 0; i < graph->num_chunks; i++) {
119 uint32_t chunk_id = get_be32(chunk_lookup + 0);
120 uint64_t chunk_offset = get_be64(chunk_lookup + 4);
121 int chunk_repeated = 0;
123 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
125 if (chunk_offset > graph_size - GIT_MAX_RAWSZ) {
126 error("improper chunk offset %08x%08x", (uint32_t)(chunk_offset >> 32),
127 (uint32_t)chunk_offset);
132 case GRAPH_CHUNKID_OIDFANOUT:
133 if (graph->chunk_oid_fanout)
136 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
139 case GRAPH_CHUNKID_OIDLOOKUP:
140 if (graph->chunk_oid_lookup)
143 graph->chunk_oid_lookup = data + chunk_offset;
146 case GRAPH_CHUNKID_DATA:
147 if (graph->chunk_commit_data)
150 graph->chunk_commit_data = data + chunk_offset;
153 case GRAPH_CHUNKID_LARGEEDGES:
154 if (graph->chunk_large_edges)
157 graph->chunk_large_edges = data + chunk_offset;
161 if (chunk_repeated) {
162 error("chunk id %08x appears multiple times", chunk_id);
166 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
168 graph->num_commits = (chunk_offset - last_chunk_offset)
172 last_chunk_id = chunk_id;
173 last_chunk_offset = chunk_offset;
179 munmap(graph_map, graph_size);
185 static struct commit_graph *commit_graph = NULL;
187 static void prepare_commit_graph_one(const char *obj_dir)
194 graph_name = get_commit_graph_filename(obj_dir);
195 commit_graph = load_commit_graph_one(graph_name);
197 FREE_AND_NULL(graph_name);
200 static int prepare_commit_graph_run_once = 0;
201 static void prepare_commit_graph(void)
203 struct alternate_object_database *alt;
206 if (prepare_commit_graph_run_once)
208 prepare_commit_graph_run_once = 1;
210 obj_dir = get_object_directory();
211 prepare_commit_graph_one(obj_dir);
212 prepare_alt_odb(the_repository);
213 for (alt = the_repository->objects->alt_odb_list;
214 !commit_graph && alt;
216 prepare_commit_graph_one(alt->path);
219 static void close_commit_graph(void)
224 if (commit_graph->graph_fd >= 0) {
225 munmap((void *)commit_graph->data, commit_graph->data_len);
226 commit_graph->data = NULL;
227 close(commit_graph->graph_fd);
230 FREE_AND_NULL(commit_graph);
233 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
235 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
236 g->chunk_oid_lookup, g->hash_len, pos);
239 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
241 struct commit_list **pptr)
244 struct object_id oid;
245 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
246 c = lookup_commit(&oid);
248 die("could not find commit %s", oid_to_hex(&oid));
250 return &commit_list_insert(c, pptr)->next;
253 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
255 const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
256 item->graph_pos = pos;
257 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
260 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
263 uint32_t *parent_data_ptr;
264 uint64_t date_low, date_high;
265 struct commit_list **pptr;
266 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
268 item->object.parsed = 1;
269 item->graph_pos = pos;
271 item->maybe_tree = NULL;
273 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
274 date_low = get_be32(commit_data + g->hash_len + 12);
275 item->date = (timestamp_t)((date_high << 32) | date_low);
277 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
279 pptr = &item->parents;
281 edge_value = get_be32(commit_data + g->hash_len);
282 if (edge_value == GRAPH_PARENT_NONE)
284 pptr = insert_parent_or_die(g, edge_value, pptr);
286 edge_value = get_be32(commit_data + g->hash_len + 4);
287 if (edge_value == GRAPH_PARENT_NONE)
289 if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
290 pptr = insert_parent_or_die(g, edge_value, pptr);
294 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
295 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
297 edge_value = get_be32(parent_data_ptr);
298 pptr = insert_parent_or_die(g,
299 edge_value & GRAPH_EDGE_LAST_MASK,
302 } while (!(edge_value & GRAPH_LAST_EDGE));
307 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
309 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
310 *pos = item->graph_pos;
313 return bsearch_graph(g, &(item->object.oid), pos);
317 static int parse_commit_in_graph_one(struct commit_graph *g, struct commit *item)
321 if (!core_commit_graph)
323 if (item->object.parsed)
326 if (find_commit_in_graph(item, g, &pos))
327 return fill_commit_in_graph(item, g, pos);
332 int parse_commit_in_graph(struct commit *item)
334 if (!core_commit_graph)
337 prepare_commit_graph();
339 return parse_commit_in_graph_one(commit_graph, item);
343 void load_commit_graph_info(struct commit *item)
346 if (!core_commit_graph)
348 prepare_commit_graph();
349 if (commit_graph && find_commit_in_graph(item, commit_graph, &pos))
350 fill_commit_graph_info(item, commit_graph, pos);
353 static struct tree *load_tree_for_commit(struct commit_graph *g, struct commit *c)
355 struct object_id oid;
356 const unsigned char *commit_data = g->chunk_commit_data +
357 GRAPH_DATA_WIDTH * (c->graph_pos);
359 hashcpy(oid.hash, commit_data);
360 c->maybe_tree = lookup_tree(&oid);
362 return c->maybe_tree;
365 static struct tree *get_commit_tree_in_graph_one(struct commit_graph *g,
366 const struct commit *c)
369 return c->maybe_tree;
370 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
371 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
373 return load_tree_for_commit(g, (struct commit *)c);
376 struct tree *get_commit_tree_in_graph(const struct commit *c)
378 return get_commit_tree_in_graph_one(commit_graph, c);
381 static void write_graph_chunk_fanout(struct hashfile *f,
382 struct commit **commits,
386 struct commit **list = commits;
389 * Write the first-level table (the list is sorted,
390 * but we use a 256-entry lookup to be able to avoid
391 * having to do eight extra binary search iterations).
393 for (i = 0; i < 256; i++) {
394 while (count < nr_commits) {
395 if ((*list)->object.oid.hash[0] != i)
401 hashwrite_be32(f, count);
405 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
406 struct commit **commits, int nr_commits)
408 struct commit **list = commits;
410 for (count = 0; count < nr_commits; count++, list++)
411 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
414 static const unsigned char *commit_to_sha1(size_t index, void *table)
416 struct commit **commits = table;
417 return commits[index]->object.oid.hash;
420 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
421 struct commit **commits, int nr_commits)
423 struct commit **list = commits;
424 struct commit **last = commits + nr_commits;
425 uint32_t num_extra_edges = 0;
427 while (list < last) {
428 struct commit_list *parent;
430 uint32_t packedDate[2];
433 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
435 parent = (*list)->parents;
438 edge_value = GRAPH_PARENT_NONE;
440 edge_value = sha1_pos(parent->item->object.oid.hash,
446 edge_value = GRAPH_PARENT_MISSING;
449 hashwrite_be32(f, edge_value);
452 parent = parent->next;
455 edge_value = GRAPH_PARENT_NONE;
456 else if (parent->next)
457 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
459 edge_value = sha1_pos(parent->item->object.oid.hash,
464 edge_value = GRAPH_PARENT_MISSING;
467 hashwrite_be32(f, edge_value);
469 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
472 parent = parent->next;
476 if (sizeof((*list)->date) > 4)
477 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
481 packedDate[0] |= htonl((*list)->generation << 2);
483 packedDate[1] = htonl((*list)->date);
484 hashwrite(f, packedDate, 8);
490 static void write_graph_chunk_large_edges(struct hashfile *f,
491 struct commit **commits,
494 struct commit **list = commits;
495 struct commit **last = commits + nr_commits;
496 struct commit_list *parent;
498 while (list < last) {
500 for (parent = (*list)->parents; num_parents < 3 && parent;
501 parent = parent->next)
504 if (num_parents <= 2) {
509 /* Since num_parents > 2, this initializer is safe. */
510 for (parent = (*list)->parents->next; parent; parent = parent->next) {
511 int edge_value = sha1_pos(parent->item->object.oid.hash,
517 edge_value = GRAPH_PARENT_MISSING;
518 else if (!parent->next)
519 edge_value |= GRAPH_LAST_EDGE;
521 hashwrite_be32(f, edge_value);
528 static int commit_compare(const void *_a, const void *_b)
530 const struct object_id *a = (const struct object_id *)_a;
531 const struct object_id *b = (const struct object_id *)_b;
535 struct packed_commit_list {
536 struct commit **list;
541 struct packed_oid_list {
542 struct object_id *list;
547 static int add_packed_commits(const struct object_id *oid,
548 struct packed_git *pack,
552 struct packed_oid_list *list = (struct packed_oid_list*)data;
553 enum object_type type;
554 off_t offset = nth_packed_object_offset(pack, pos);
555 struct object_info oi = OBJECT_INFO_INIT;
558 if (packed_object_info(the_repository, pack, offset, &oi) < 0)
559 die("unable to get type of object %s", oid_to_hex(oid));
561 if (type != OBJ_COMMIT)
564 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
565 oidcpy(&(list->list[list->nr]), oid);
571 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
573 struct commit_list *parent;
574 for (parent = commit->parents; parent; parent = parent->next) {
575 if (!(parent->item->object.flags & UNINTERESTING)) {
576 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
577 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
579 parent->item->object.flags |= UNINTERESTING;
584 static void close_reachable(struct packed_oid_list *oids)
587 struct commit *commit;
589 for (i = 0; i < oids->nr; i++) {
590 commit = lookup_commit(&oids->list[i]);
592 commit->object.flags |= UNINTERESTING;
596 * As this loop runs, oids->nr may grow, but not more
597 * than the number of missing commits in the reachable
600 for (i = 0; i < oids->nr; i++) {
601 commit = lookup_commit(&oids->list[i]);
603 if (commit && !parse_commit(commit))
604 add_missing_parents(oids, commit);
607 for (i = 0; i < oids->nr; i++) {
608 commit = lookup_commit(&oids->list[i]);
611 commit->object.flags &= ~UNINTERESTING;
615 static void compute_generation_numbers(struct packed_commit_list* commits)
618 struct commit_list *list = NULL;
620 for (i = 0; i < commits->nr; i++) {
621 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
622 commits->list[i]->generation != GENERATION_NUMBER_ZERO)
625 commit_list_insert(commits->list[i], &list);
627 struct commit *current = list->item;
628 struct commit_list *parent;
629 int all_parents_computed = 1;
630 uint32_t max_generation = 0;
632 for (parent = current->parents; parent; parent = parent->next) {
633 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
634 parent->item->generation == GENERATION_NUMBER_ZERO) {
635 all_parents_computed = 0;
636 commit_list_insert(parent->item, &list);
638 } else if (parent->item->generation > max_generation) {
639 max_generation = parent->item->generation;
643 if (all_parents_computed) {
644 current->generation = max_generation + 1;
647 if (current->generation > GENERATION_NUMBER_MAX)
648 current->generation = GENERATION_NUMBER_MAX;
654 void write_commit_graph(const char *obj_dir,
655 const char **pack_indexes,
657 const char **commit_hex,
661 struct packed_oid_list oids;
662 struct packed_commit_list commits;
664 uint32_t i, count_distinct = 0;
666 struct lock_file lk = LOCK_INIT;
667 uint32_t chunk_ids[5];
668 uint64_t chunk_offsets[5];
671 struct commit_list *parent;
674 oids.alloc = approximate_object_count() / 4;
677 prepare_commit_graph_one(obj_dir);
679 oids.alloc += commit_graph->num_commits;
682 if (oids.alloc < 1024)
684 ALLOC_ARRAY(oids.list, oids.alloc);
686 if (append && commit_graph) {
687 for (i = 0; i < commit_graph->num_commits; i++) {
688 const unsigned char *hash = commit_graph->chunk_oid_lookup +
689 commit_graph->hash_len * i;
690 hashcpy(oids.list[oids.nr++].hash, hash);
695 struct strbuf packname = STRBUF_INIT;
697 strbuf_addf(&packname, "%s/pack/", obj_dir);
698 dirlen = packname.len;
699 for (i = 0; i < nr_packs; i++) {
700 struct packed_git *p;
701 strbuf_setlen(&packname, dirlen);
702 strbuf_addstr(&packname, pack_indexes[i]);
703 p = add_packed_git(packname.buf, packname.len, 1);
705 die("error adding pack %s", packname.buf);
706 if (open_pack_index(p))
707 die("error opening index for %s", packname.buf);
708 for_each_object_in_pack(p, add_packed_commits, &oids);
711 strbuf_release(&packname);
715 for (i = 0; i < nr_commits; i++) {
717 struct object_id oid;
718 struct commit *result;
720 if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
723 result = lookup_commit_reference_gently(&oid, 1);
726 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
727 oidcpy(&oids.list[oids.nr], &(result->object.oid));
733 if (!pack_indexes && !commit_hex)
734 for_each_packed_object(add_packed_commits, &oids, 0);
736 close_reachable(&oids);
738 QSORT(oids.list, oids.nr, commit_compare);
741 for (i = 1; i < oids.nr; i++) {
742 if (oidcmp(&oids.list[i-1], &oids.list[i]))
746 if (count_distinct >= GRAPH_PARENT_MISSING)
747 die(_("the commit graph format cannot write %d commits"), count_distinct);
750 commits.alloc = count_distinct;
751 ALLOC_ARRAY(commits.list, commits.alloc);
754 for (i = 0; i < oids.nr; i++) {
756 if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
759 commits.list[commits.nr] = lookup_commit(&oids.list[i]);
760 parse_commit(commits.list[commits.nr]);
762 for (parent = commits.list[commits.nr]->parents;
763 parent; parent = parent->next)
767 num_extra_edges += num_parents - 1;
771 num_chunks = num_extra_edges ? 4 : 3;
773 if (commits.nr >= GRAPH_PARENT_MISSING)
774 die(_("too many commits to write graph"));
776 compute_generation_numbers(&commits);
778 graph_name = get_commit_graph_filename(obj_dir);
779 if (safe_create_leading_directories(graph_name))
780 die_errno(_("unable to create leading directories of %s"),
783 hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
784 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
786 hashwrite_be32(f, GRAPH_SIGNATURE);
788 hashwrite_u8(f, GRAPH_VERSION);
789 hashwrite_u8(f, GRAPH_OID_VERSION);
790 hashwrite_u8(f, num_chunks);
791 hashwrite_u8(f, 0); /* unused padding byte */
793 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
794 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
795 chunk_ids[2] = GRAPH_CHUNKID_DATA;
797 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
802 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
803 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
804 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
805 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
806 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
808 for (i = 0; i <= num_chunks; i++) {
809 uint32_t chunk_write[3];
811 chunk_write[0] = htonl(chunk_ids[i]);
812 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
813 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
814 hashwrite(f, chunk_write, 12);
817 write_graph_chunk_fanout(f, commits.list, commits.nr);
818 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
819 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
820 write_graph_chunk_large_edges(f, commits.list, commits.nr);
822 close_commit_graph();
823 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
824 commit_lock_file(&lk);
831 static int verify_commit_graph_error;
833 static void graph_report(const char *fmt, ...)
837 verify_commit_graph_error = 1;
839 vfprintf(stderr, fmt, ap);
840 fprintf(stderr, "\n");
844 int verify_commit_graph(struct repository *r, struct commit_graph *g)
846 uint32_t i, cur_fanout_pos = 0;
847 struct object_id prev_oid, cur_oid;
850 graph_report("no commit-graph file loaded");
854 verify_commit_graph_error = 0;
856 if (!g->chunk_oid_fanout)
857 graph_report("commit-graph is missing the OID Fanout chunk");
858 if (!g->chunk_oid_lookup)
859 graph_report("commit-graph is missing the OID Lookup chunk");
860 if (!g->chunk_commit_data)
861 graph_report("commit-graph is missing the Commit Data chunk");
863 if (verify_commit_graph_error)
864 return verify_commit_graph_error;
866 for (i = 0; i < g->num_commits; i++) {
867 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
869 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
870 graph_report("commit-graph has incorrect OID order: %s then %s",
871 oid_to_hex(&prev_oid),
872 oid_to_hex(&cur_oid));
874 oidcpy(&prev_oid, &cur_oid);
876 while (cur_oid.hash[0] > cur_fanout_pos) {
877 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
879 if (i != fanout_value)
880 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
881 cur_fanout_pos, fanout_value, i);
886 while (cur_fanout_pos < 256) {
887 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
889 if (g->num_commits != fanout_value)
890 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
891 cur_fanout_pos, fanout_value, i);
896 return verify_commit_graph_error;