3 #include "git-compat-util.h"
9 #include "sha1-lookup.h"
10 #include "commit-graph.h"
12 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
13 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
14 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
15 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
16 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
18 #define GRAPH_DATA_WIDTH 36
20 #define GRAPH_VERSION_1 0x1
21 #define GRAPH_VERSION GRAPH_VERSION_1
23 #define GRAPH_OID_VERSION_SHA1 1
24 #define GRAPH_OID_LEN_SHA1 20
25 #define GRAPH_OID_VERSION GRAPH_OID_VERSION_SHA1
26 #define GRAPH_OID_LEN GRAPH_OID_LEN_SHA1
28 #define GRAPH_LARGE_EDGES_NEEDED 0x80000000
29 #define GRAPH_PARENT_MISSING 0x7fffffff
30 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
31 #define GRAPH_PARENT_NONE 0x70000000
33 #define GRAPH_LAST_EDGE 0x80000000
35 #define GRAPH_FANOUT_SIZE (4 * 256)
36 #define GRAPH_CHUNKLOOKUP_WIDTH 12
37 #define GRAPH_CHUNKLOOKUP_SIZE (5 * GRAPH_CHUNKLOOKUP_WIDTH)
38 #define GRAPH_MIN_SIZE (GRAPH_CHUNKLOOKUP_SIZE + GRAPH_FANOUT_SIZE + \
42 struct commit_graph *commit_graph = NULL;
44 char *get_graph_latest_filename(const char *obj_dir)
46 struct strbuf fname = STRBUF_INIT;
47 strbuf_addf(&fname, "%s/info/graph-latest", obj_dir);
48 return strbuf_detach(&fname, 0);
51 char *get_graph_latest_contents(const char *obj_dir)
53 struct strbuf graph_file = STRBUF_INIT;
58 fname = get_graph_latest_filename(obj_dir);
59 f = fopen(fname, "r");
66 if (fgets(buf, sizeof(buf), f))
67 strbuf_addstr(&graph_file, buf);
71 return strbuf_detach(&graph_file, NULL);
74 static struct commit_graph *alloc_commit_graph(void)
76 struct commit_graph *g = xmalloc(sizeof(*g));
77 memset(g, 0, sizeof(*g));
83 struct commit_graph *load_commit_graph_one(const char *graph_file)
86 const unsigned char *data, *chunk_lookup;
90 struct commit_graph *graph;
91 int fd = git_open(graph_file);
92 uint64_t last_chunk_offset;
93 uint32_t last_chunk_id;
94 uint32_t graph_signature;
95 unsigned char graph_version, hash_version;
103 graph_size = xsize_t(st.st_size);
105 if (graph_size < GRAPH_MIN_SIZE) {
107 die("graph file %s is too small", graph_file);
109 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
110 data = (const unsigned char *)graph_map;
112 graph_signature = ntohl(*(uint32_t*)data);
113 if (graph_signature != GRAPH_SIGNATURE) {
114 munmap(graph_map, graph_size);
116 die("graph signature %X does not match signature %X",
117 graph_signature, GRAPH_SIGNATURE);
120 graph_version = *(unsigned char*)(data + 4);
121 if (graph_version != GRAPH_VERSION) {
122 munmap(graph_map, graph_size);
124 die("graph version %X does not match version %X",
125 graph_version, GRAPH_VERSION);
128 hash_version = *(unsigned char*)(data + 5);
129 if (hash_version != GRAPH_OID_VERSION) {
130 munmap(graph_map, graph_size);
132 die("hash version %X does not match version %X",
133 hash_version, GRAPH_OID_VERSION);
136 graph = alloc_commit_graph();
138 graph->hash_len = GRAPH_OID_LEN;
139 graph->num_chunks = *(unsigned char*)(data + 6);
140 graph->graph_fd = fd;
141 graph->data = graph_map;
142 graph->data_len = graph_size;
145 last_chunk_offset = 8;
146 chunk_lookup = data + 8;
147 for (i = 0; i < graph->num_chunks; i++) {
148 uint32_t chunk_id = get_be32(chunk_lookup + 0);
149 uint64_t chunk_offset1 = get_be32(chunk_lookup + 4);
150 uint32_t chunk_offset2 = get_be32(chunk_lookup + 8);
151 uint64_t chunk_offset = (chunk_offset1 << 32) | chunk_offset2;
153 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
155 if (chunk_offset > graph_size - GIT_MAX_RAWSZ)
156 die("improper chunk offset %08x%08x", (uint32_t)(chunk_offset >> 32),
157 (uint32_t)chunk_offset);
160 case GRAPH_CHUNKID_OIDFANOUT:
161 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
164 case GRAPH_CHUNKID_OIDLOOKUP:
165 graph->chunk_oid_lookup = data + chunk_offset;
168 case GRAPH_CHUNKID_DATA:
169 graph->chunk_commit_data = data + chunk_offset;
172 case GRAPH_CHUNKID_LARGEEDGES:
173 graph->chunk_large_edges = data + chunk_offset;
177 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
179 graph->num_commits = (chunk_offset - last_chunk_offset)
183 last_chunk_id = chunk_id;
184 last_chunk_offset = chunk_offset;
190 static void prepare_commit_graph_one(const char *obj_dir)
192 struct strbuf graph_file = STRBUF_INIT;
198 graph_name = get_graph_latest_contents(obj_dir);
203 strbuf_addf(&graph_file, "%s/info/%s", obj_dir, graph_name);
205 commit_graph = load_commit_graph_one(graph_file.buf);
207 FREE_AND_NULL(graph_name);
208 strbuf_release(&graph_file);
211 static int prepare_commit_graph_run_once = 0;
212 void prepare_commit_graph(void)
214 struct alternate_object_database *alt;
217 if (prepare_commit_graph_run_once)
219 prepare_commit_graph_run_once = 1;
221 obj_dir = get_object_directory();
222 prepare_commit_graph_one(obj_dir);
224 for (alt = alt_odb_list; !commit_graph && alt; alt = alt->next)
225 prepare_commit_graph_one(alt->path);
228 static void close_commit_graph(void)
233 if (commit_graph->graph_fd >= 0) {
234 munmap((void *)commit_graph->data, commit_graph->data_len);
235 commit_graph->data = NULL;
236 close(commit_graph->graph_fd);
239 FREE_AND_NULL(commit_graph);
242 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
244 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
245 g->chunk_oid_lookup, g->hash_len, pos);
248 static struct commit_list **insert_parent_or_die(struct commit_graph *g,
250 struct commit_list **pptr)
253 struct object_id oid;
254 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
255 c = lookup_commit(&oid);
257 die("could not find commit %s", oid_to_hex(&oid));
259 return &commit_list_insert(c, pptr)->next;
262 static int fill_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t pos)
264 struct object_id oid;
265 uint32_t new_parent_pos;
266 uint32_t *parent_data_ptr;
267 uint64_t date_low, date_high;
268 struct commit_list **pptr;
269 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
271 item->object.parsed = 1;
272 item->graph_pos = pos;
274 hashcpy(oid.hash, commit_data);
275 item->tree = lookup_tree(&oid);
277 date_high = ntohl(*(uint32_t*)(commit_data + g->hash_len + 8)) & 0x3;
278 date_low = ntohl(*(uint32_t*)(commit_data + g->hash_len + 12));
279 item->date = (timestamp_t)((date_high << 32) | date_low);
281 pptr = &item->parents;
283 new_parent_pos = ntohl(*(uint32_t*)(commit_data + g->hash_len));
284 if (new_parent_pos == GRAPH_PARENT_NONE)
286 pptr = insert_parent_or_die(g, new_parent_pos, pptr);
288 new_parent_pos = ntohl(*(uint32_t*)(commit_data + g->hash_len + 4));
289 if (new_parent_pos == GRAPH_PARENT_NONE)
291 if (!(new_parent_pos & GRAPH_LARGE_EDGES_NEEDED)) {
292 pptr = insert_parent_or_die(g, new_parent_pos, pptr);
296 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
297 4 * (uint64_t)(new_parent_pos & GRAPH_EDGE_LAST_MASK));
299 new_parent_pos = ntohl(*parent_data_ptr);
300 pptr = insert_parent_or_die(g,
301 new_parent_pos & GRAPH_EDGE_LAST_MASK,
304 } while (!(new_parent_pos & GRAPH_LAST_EDGE));
309 int parse_commit_in_graph(struct commit *item)
311 if (!core_commit_graph)
313 if (item->object.parsed)
316 prepare_commit_graph();
320 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
321 pos = item->graph_pos;
324 found = bsearch_graph(commit_graph, &(item->object.oid), &pos);
328 return fill_commit_in_graph(item, commit_graph, pos);
334 static void write_graph_chunk_fanout(struct hashfile *f,
335 struct commit **commits,
338 uint32_t i, count = 0;
339 struct commit **list = commits;
340 struct commit **last = commits + nr_commits;
343 * Write the first-level table (the list is sorted,
344 * but we use a 256-entry lookup to be able to avoid
345 * having to do eight extra binary search iterations).
347 for (i = 0; i < 256; i++) {
348 while (list < last) {
349 if ((*list)->object.oid.hash[0] != i)
355 hashwrite_be32(f, count);
359 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
360 struct commit **commits, int nr_commits)
362 struct commit **list, **last = commits + nr_commits;
363 for (list = commits; list < last; list++)
364 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
367 static int commit_pos(struct commit **commits, int nr_commits,
368 const struct object_id *oid, uint32_t *pos)
370 uint32_t first = 0, last = nr_commits;
372 while (first < last) {
373 uint32_t mid = first + (last - first) / 2;
374 struct object_id *current;
377 current = &(commits[mid]->object.oid);
378 cmp = oidcmp(oid, current);
394 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
395 struct commit **commits, int nr_commits)
397 struct commit **list = commits;
398 struct commit **last = commits + nr_commits;
399 uint32_t num_large_edges = 0;
401 while (list < last) {
402 struct commit_list *parent;
404 uint32_t packedDate[2];
407 hashwrite(f, (*list)->tree->object.oid.hash, hash_len);
409 parent = (*list)->parents;
412 int_id = GRAPH_PARENT_NONE;
413 else if (!commit_pos(commits, nr_commits,
414 &(parent->item->object.oid), &int_id))
415 int_id = GRAPH_PARENT_MISSING;
417 hashwrite_be32(f, int_id);
420 parent = parent->next;
423 int_id = GRAPH_PARENT_NONE;
424 else if (parent->next)
425 int_id = GRAPH_LARGE_EDGES_NEEDED | num_large_edges;
426 else if (!commit_pos(commits, nr_commits,
427 &(parent->item->object.oid), &int_id))
428 int_id = GRAPH_PARENT_MISSING;
430 hashwrite_be32(f, int_id);
432 if (parent && parent->next) {
435 parent = parent->next;
439 if (sizeof((*list)->date) > 4)
440 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
444 packedDate[1] = htonl((*list)->date);
445 hashwrite(f, packedDate, 8);
451 static void write_graph_chunk_large_edges(struct hashfile *f,
452 struct commit **commits,
455 struct commit **list = commits;
456 struct commit **last = commits + nr_commits;
457 struct commit_list *parent;
459 while (list < last) {
461 for (parent = (*list)->parents; num_parents < 3 && parent;
462 parent = parent->next)
465 if (num_parents <= 2) {
470 /* Since num_parents > 2, this initializer is safe. */
471 for (parent = (*list)->parents->next; parent; parent = parent->next) {
472 uint32_t int_id, swap_int_id;
473 uint32_t last_edge = 0;
475 last_edge |= GRAPH_LAST_EDGE;
477 if (commit_pos(commits, nr_commits,
478 &(parent->item->object.oid),
480 swap_int_id = htonl(int_id | last_edge);
482 swap_int_id = htonl(GRAPH_PARENT_MISSING | last_edge);
484 hashwrite(f, &swap_int_id, 4);
491 static int commit_compare(const void *_a, const void *_b)
493 struct object_id *a = (struct object_id *)_a;
494 struct object_id *b = (struct object_id *)_b;
498 struct packed_commit_list {
499 struct commit **list;
504 struct packed_oid_list {
505 struct object_id *list;
510 static int if_packed_commit_add_to_list(const struct object_id *oid,
511 struct packed_git *pack,
515 struct packed_oid_list *list = (struct packed_oid_list*)data;
516 enum object_type type;
519 off_t offset = nth_packed_object_offset(pack, pos);
520 inner_data = unpack_entry(pack, offset, &type, &size);
525 if (type != OBJ_COMMIT)
528 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
529 oidcpy(&(list->list[list->nr]), oid);
535 static void close_reachable(struct packed_oid_list *oids)
538 struct rev_info revs;
539 struct commit *commit;
540 init_revisions(&revs, NULL);
541 for (i = 0; i < oids->nr; i++) {
542 commit = lookup_commit(&oids->list[i]);
543 if (commit && !parse_commit(commit))
544 revs.commits = commit_list_insert(commit, &revs.commits);
547 if (prepare_revision_walk(&revs))
548 die(_("revision walk setup failed"));
550 while ((commit = get_revision(&revs)) != NULL) {
551 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
552 oidcpy(&oids->list[oids->nr], &(commit->object.oid));
557 char *write_commit_graph(const char *obj_dir,
558 const char **pack_indexes,
560 const char **commit_hex,
563 struct packed_oid_list oids;
564 struct packed_commit_list commits;
566 int i, count_distinct = 0;
568 struct strbuf tmp_file = STRBUF_INIT;
569 struct strbuf graph_file = STRBUF_INIT;
570 unsigned char final_hash[GIT_MAX_RAWSZ];
573 uint32_t chunk_ids[5];
574 uint64_t chunk_offsets[5];
577 struct commit_list *parent;
580 oids.alloc = (int)(0.15 * approximate_object_count());
582 if (oids.alloc < 1024)
584 ALLOC_ARRAY(oids.list, oids.alloc);
587 struct strbuf packname = STRBUF_INIT;
589 strbuf_addf(&packname, "%s/pack/", obj_dir);
590 dirlen = packname.len;
591 for (i = 0; i < nr_packs; i++) {
592 struct packed_git *p;
593 strbuf_setlen(&packname, dirlen);
594 strbuf_addstr(&packname, pack_indexes[i]);
595 p = add_packed_git(packname.buf, packname.len, 1);
597 die("error adding pack %s", packname.buf);
598 if (open_pack_index(p))
599 die("error opening index for %s", packname.buf);
600 for_each_object_in_pack(p, if_packed_commit_add_to_list, &oids);
606 for (i = 0; i < nr_commits; i++) {
608 struct object_id oid;
609 struct commit *result;
611 if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
614 result = lookup_commit_reference_gently(&oid, 1);
617 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
618 oidcpy(&oids.list[oids.nr], &(result->object.oid));
624 if (!pack_indexes && !commit_hex)
625 for_each_packed_object(if_packed_commit_add_to_list, &oids, 0);
627 close_reachable(&oids);
629 QSORT(oids.list, oids.nr, commit_compare);
632 for (i = 1; i < oids.nr; i++) {
633 if (oidcmp(&oids.list[i-1], &oids.list[i]))
638 commits.alloc = count_distinct;
639 ALLOC_ARRAY(commits.list, commits.alloc);
642 for (i = 0; i < oids.nr; i++) {
644 if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
647 commits.list[commits.nr] = lookup_commit(&oids.list[i]);
648 parse_commit(commits.list[commits.nr]);
650 for (parent = commits.list[commits.nr]->parents;
651 parent; parent = parent->next)
655 num_long_edges += num_parents - 1;
659 num_chunks = num_long_edges ? 4 : 3;
661 strbuf_addf(&tmp_file, "%s/info", obj_dir);
662 info_dir = opendir(tmp_file.buf);
664 if (!info_dir && mkdir(tmp_file.buf, 0777) < 0)
665 die_errno(_("cannot mkdir %s"), tmp_file.buf);
669 strbuf_addstr(&tmp_file, "/tmp_graph_XXXXXX");
671 fd = git_mkstemp_mode(tmp_file.buf, 0444);
673 die_errno("unable to create '%s'", tmp_file.buf);
675 f = hashfd(fd, tmp_file.buf);
677 hashwrite_be32(f, GRAPH_SIGNATURE);
679 hashwrite_u8(f, GRAPH_VERSION);
680 hashwrite_u8(f, GRAPH_OID_VERSION);
681 hashwrite_u8(f, num_chunks);
682 hashwrite_u8(f, 0); /* unused padding byte */
684 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
685 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
686 chunk_ids[2] = GRAPH_CHUNKID_DATA;
688 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
693 chunk_offsets[0] = 8 + GRAPH_CHUNKLOOKUP_SIZE;
694 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
695 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
696 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
697 chunk_offsets[4] = chunk_offsets[3] + 4 * num_long_edges;
699 for (i = 0; i <= num_chunks; i++) {
700 uint32_t chunk_write[3];
702 chunk_write[0] = htonl(chunk_ids[i]);
703 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
704 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
705 hashwrite(f, chunk_write, 12);
708 write_graph_chunk_fanout(f, commits.list, commits.nr);
709 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
710 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
711 write_graph_chunk_large_edges(f, commits.list, commits.nr);
713 hashclose(f, final_hash, CSUM_CLOSE | CSUM_FSYNC);
715 strbuf_addf(&graph_file, "graph-%s.graph", sha1_to_hex(final_hash));
716 graph_name = strbuf_detach(&graph_file, NULL);
717 strbuf_addf(&graph_file, "%s/info/%s", obj_dir, graph_name);
719 close_commit_graph();
720 if (rename(tmp_file.buf, graph_file.buf))
721 die("failed to rename %s to %s", tmp_file.buf, graph_file.buf);
723 strbuf_release(&tmp_file);
724 strbuf_release(&graph_file);