git-p4: narrow the scope of exceptions caught when parsing an int
[git.git] / commit-graph.c
blob3fc1e0da2768d3f209da08b6b4c71df133a3ec88
1 #include "cache.h"
2 #include "config.h"
3 #include "git-compat-util.h"
4 #include "lockfile.h"
5 #include "pack.h"
6 #include "packfile.h"
7 #include "commit.h"
8 #include "object.h"
9 #include "revision.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 + \
40 GRAPH_OID_LEN + 8)
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));
50 g->graph_fd = -1;
52 return g;
55 struct commit_graph *load_commit_graph_one(const char *graph_file)
57 void *graph_map;
58 const unsigned char *data, *chunk_lookup;
59 size_t graph_size;
60 struct stat st;
61 uint32_t i;
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;
69 if (fd < 0)
70 return NULL;
71 if (fstat(fd, &st)) {
72 close(fd);
73 return NULL;
75 graph_size = xsize_t(st.st_size);
77 if (graph_size < GRAPH_MIN_SIZE) {
78 close(fd);
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);
88 goto cleanup_fail;
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);
95 goto cleanup_fail;
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);
102 goto cleanup_fail;
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;
113 last_chunk_id = 0;
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);
126 goto cleanup_fail;
129 switch (chunk_id) {
130 case GRAPH_CHUNKID_OIDFANOUT:
131 if (graph->chunk_oid_fanout)
132 chunk_repeated = 1;
133 else
134 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
135 break;
137 case GRAPH_CHUNKID_OIDLOOKUP:
138 if (graph->chunk_oid_lookup)
139 chunk_repeated = 1;
140 else
141 graph->chunk_oid_lookup = data + chunk_offset;
142 break;
144 case GRAPH_CHUNKID_DATA:
145 if (graph->chunk_commit_data)
146 chunk_repeated = 1;
147 else
148 graph->chunk_commit_data = data + chunk_offset;
149 break;
151 case GRAPH_CHUNKID_LARGEEDGES:
152 if (graph->chunk_large_edges)
153 chunk_repeated = 1;
154 else
155 graph->chunk_large_edges = data + chunk_offset;
156 break;
159 if (chunk_repeated) {
160 error("chunk id %08x appears multiple times", chunk_id);
161 goto cleanup_fail;
164 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
166 graph->num_commits = (chunk_offset - last_chunk_offset)
167 / graph->hash_len;
170 last_chunk_id = chunk_id;
171 last_chunk_offset = chunk_offset;
174 return graph;
176 cleanup_fail:
177 munmap(graph_map, graph_size);
178 close(fd);
179 exit(1);
182 /* global storage */
183 static struct commit_graph *commit_graph = NULL;
185 static void prepare_commit_graph_one(const char *obj_dir)
187 char *graph_name;
189 if (commit_graph)
190 return;
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;
202 char *obj_dir;
204 if (prepare_commit_graph_run_once)
205 return;
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;
213 alt = alt->next)
214 prepare_commit_graph_one(alt->path);
217 static void close_commit_graph(void)
219 if (!commit_graph)
220 return;
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,
238 uint64_t pos,
239 struct commit_list **pptr)
241 struct commit *c;
242 struct object_id oid;
243 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
244 c = lookup_commit(&oid);
245 if (!c)
246 die("could not find commit %s", oid_to_hex(&oid));
247 c->graph_pos = pos;
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)
253 struct object_id oid;
254 uint32_t edge_value;
255 uint32_t *parent_data_ptr;
256 uint64_t date_low, date_high;
257 struct commit_list **pptr;
258 const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
260 item->object.parsed = 1;
261 item->graph_pos = pos;
263 hashcpy(oid.hash, commit_data);
264 item->tree = lookup_tree(&oid);
266 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
267 date_low = get_be32(commit_data + g->hash_len + 12);
268 item->date = (timestamp_t)((date_high << 32) | date_low);
270 pptr = &item->parents;
272 edge_value = get_be32(commit_data + g->hash_len);
273 if (edge_value == GRAPH_PARENT_NONE)
274 return 1;
275 pptr = insert_parent_or_die(g, edge_value, pptr);
277 edge_value = get_be32(commit_data + g->hash_len + 4);
278 if (edge_value == GRAPH_PARENT_NONE)
279 return 1;
280 if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
281 pptr = insert_parent_or_die(g, edge_value, pptr);
282 return 1;
285 parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
286 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
287 do {
288 edge_value = get_be32(parent_data_ptr);
289 pptr = insert_parent_or_die(g,
290 edge_value & GRAPH_EDGE_LAST_MASK,
291 pptr);
292 parent_data_ptr++;
293 } while (!(edge_value & GRAPH_LAST_EDGE));
295 return 1;
298 int parse_commit_in_graph(struct commit *item)
300 if (!core_commit_graph)
301 return 0;
302 if (item->object.parsed)
303 return 1;
305 prepare_commit_graph();
306 if (commit_graph) {
307 uint32_t pos;
308 int found;
309 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
310 pos = item->graph_pos;
311 found = 1;
312 } else {
313 found = bsearch_graph(commit_graph, &(item->object.oid), &pos);
316 if (found)
317 return fill_commit_in_graph(item, commit_graph, pos);
320 return 0;
323 static void write_graph_chunk_fanout(struct hashfile *f,
324 struct commit **commits,
325 int nr_commits)
327 int i, count = 0;
328 struct commit **list = commits;
331 * Write the first-level table (the list is sorted,
332 * but we use a 256-entry lookup to be able to avoid
333 * having to do eight extra binary search iterations).
335 for (i = 0; i < 256; i++) {
336 while (count < nr_commits) {
337 if ((*list)->object.oid.hash[0] != i)
338 break;
339 count++;
340 list++;
343 hashwrite_be32(f, count);
347 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
348 struct commit **commits, int nr_commits)
350 struct commit **list = commits;
351 int count;
352 for (count = 0; count < nr_commits; count++, list++)
353 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
356 static const unsigned char *commit_to_sha1(size_t index, void *table)
358 struct commit **commits = table;
359 return commits[index]->object.oid.hash;
362 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
363 struct commit **commits, int nr_commits)
365 struct commit **list = commits;
366 struct commit **last = commits + nr_commits;
367 uint32_t num_extra_edges = 0;
369 while (list < last) {
370 struct commit_list *parent;
371 int edge_value;
372 uint32_t packedDate[2];
374 parse_commit(*list);
375 hashwrite(f, (*list)->tree->object.oid.hash, hash_len);
377 parent = (*list)->parents;
379 if (!parent)
380 edge_value = GRAPH_PARENT_NONE;
381 else {
382 edge_value = sha1_pos(parent->item->object.oid.hash,
383 commits,
384 nr_commits,
385 commit_to_sha1);
387 if (edge_value < 0)
388 edge_value = GRAPH_PARENT_MISSING;
391 hashwrite_be32(f, edge_value);
393 if (parent)
394 parent = parent->next;
396 if (!parent)
397 edge_value = GRAPH_PARENT_NONE;
398 else if (parent->next)
399 edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
400 else {
401 edge_value = sha1_pos(parent->item->object.oid.hash,
402 commits,
403 nr_commits,
404 commit_to_sha1);
405 if (edge_value < 0)
406 edge_value = GRAPH_PARENT_MISSING;
409 hashwrite_be32(f, edge_value);
411 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
412 do {
413 num_extra_edges++;
414 parent = parent->next;
415 } while (parent);
418 if (sizeof((*list)->date) > 4)
419 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
420 else
421 packedDate[0] = 0;
423 packedDate[1] = htonl((*list)->date);
424 hashwrite(f, packedDate, 8);
426 list++;
430 static void write_graph_chunk_large_edges(struct hashfile *f,
431 struct commit **commits,
432 int nr_commits)
434 struct commit **list = commits;
435 struct commit **last = commits + nr_commits;
436 struct commit_list *parent;
438 while (list < last) {
439 int num_parents = 0;
440 for (parent = (*list)->parents; num_parents < 3 && parent;
441 parent = parent->next)
442 num_parents++;
444 if (num_parents <= 2) {
445 list++;
446 continue;
449 /* Since num_parents > 2, this initializer is safe. */
450 for (parent = (*list)->parents->next; parent; parent = parent->next) {
451 int edge_value = sha1_pos(parent->item->object.oid.hash,
452 commits,
453 nr_commits,
454 commit_to_sha1);
456 if (edge_value < 0)
457 edge_value = GRAPH_PARENT_MISSING;
458 else if (!parent->next)
459 edge_value |= GRAPH_LAST_EDGE;
461 hashwrite_be32(f, edge_value);
464 list++;
468 static int commit_compare(const void *_a, const void *_b)
470 const struct object_id *a = (const struct object_id *)_a;
471 const struct object_id *b = (const struct object_id *)_b;
472 return oidcmp(a, b);
475 struct packed_commit_list {
476 struct commit **list;
477 int nr;
478 int alloc;
481 struct packed_oid_list {
482 struct object_id *list;
483 int nr;
484 int alloc;
487 static int add_packed_commits(const struct object_id *oid,
488 struct packed_git *pack,
489 uint32_t pos,
490 void *data)
492 struct packed_oid_list *list = (struct packed_oid_list*)data;
493 enum object_type type;
494 off_t offset = nth_packed_object_offset(pack, pos);
495 struct object_info oi = OBJECT_INFO_INIT;
497 oi.typep = &type;
498 if (packed_object_info(pack, offset, &oi) < 0)
499 die("unable to get type of object %s", oid_to_hex(oid));
501 if (type != OBJ_COMMIT)
502 return 0;
504 ALLOC_GROW(list->list, list->nr + 1, list->alloc);
505 oidcpy(&(list->list[list->nr]), oid);
506 list->nr++;
508 return 0;
511 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
513 struct commit_list *parent;
514 for (parent = commit->parents; parent; parent = parent->next) {
515 if (!(parent->item->object.flags & UNINTERESTING)) {
516 ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
517 oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
518 oids->nr++;
519 parent->item->object.flags |= UNINTERESTING;
524 static void close_reachable(struct packed_oid_list *oids)
526 int i;
527 struct commit *commit;
529 for (i = 0; i < oids->nr; i++) {
530 commit = lookup_commit(&oids->list[i]);
531 if (commit)
532 commit->object.flags |= UNINTERESTING;
536 * As this loop runs, oids->nr may grow, but not more
537 * than the number of missing commits in the reachable
538 * closure.
540 for (i = 0; i < oids->nr; i++) {
541 commit = lookup_commit(&oids->list[i]);
543 if (commit && !parse_commit(commit))
544 add_missing_parents(oids, commit);
547 for (i = 0; i < oids->nr; i++) {
548 commit = lookup_commit(&oids->list[i]);
550 if (commit)
551 commit->object.flags &= ~UNINTERESTING;
555 void write_commit_graph(const char *obj_dir,
556 const char **pack_indexes,
557 int nr_packs,
558 const char **commit_hex,
559 int nr_commits,
560 int append)
562 struct packed_oid_list oids;
563 struct packed_commit_list commits;
564 struct hashfile *f;
565 uint32_t i, count_distinct = 0;
566 char *graph_name;
567 int fd;
568 struct lock_file lk = LOCK_INIT;
569 uint32_t chunk_ids[5];
570 uint64_t chunk_offsets[5];
571 int num_chunks;
572 int num_extra_edges;
573 struct commit_list *parent;
575 oids.nr = 0;
576 oids.alloc = approximate_object_count() / 4;
578 if (append) {
579 prepare_commit_graph_one(obj_dir);
580 if (commit_graph)
581 oids.alloc += commit_graph->num_commits;
584 if (oids.alloc < 1024)
585 oids.alloc = 1024;
586 ALLOC_ARRAY(oids.list, oids.alloc);
588 if (append && commit_graph) {
589 for (i = 0; i < commit_graph->num_commits; i++) {
590 const unsigned char *hash = commit_graph->chunk_oid_lookup +
591 commit_graph->hash_len * i;
592 hashcpy(oids.list[oids.nr++].hash, hash);
596 if (pack_indexes) {
597 struct strbuf packname = STRBUF_INIT;
598 int dirlen;
599 strbuf_addf(&packname, "%s/pack/", obj_dir);
600 dirlen = packname.len;
601 for (i = 0; i < nr_packs; i++) {
602 struct packed_git *p;
603 strbuf_setlen(&packname, dirlen);
604 strbuf_addstr(&packname, pack_indexes[i]);
605 p = add_packed_git(packname.buf, packname.len, 1);
606 if (!p)
607 die("error adding pack %s", packname.buf);
608 if (open_pack_index(p))
609 die("error opening index for %s", packname.buf);
610 for_each_object_in_pack(p, add_packed_commits, &oids);
611 close_pack(p);
613 strbuf_release(&packname);
616 if (commit_hex) {
617 for (i = 0; i < nr_commits; i++) {
618 const char *end;
619 struct object_id oid;
620 struct commit *result;
622 if (commit_hex[i] && parse_oid_hex(commit_hex[i], &oid, &end))
623 continue;
625 result = lookup_commit_reference_gently(&oid, 1);
627 if (result) {
628 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
629 oidcpy(&oids.list[oids.nr], &(result->object.oid));
630 oids.nr++;
635 if (!pack_indexes && !commit_hex)
636 for_each_packed_object(add_packed_commits, &oids, 0);
638 close_reachable(&oids);
640 QSORT(oids.list, oids.nr, commit_compare);
642 count_distinct = 1;
643 for (i = 1; i < oids.nr; i++) {
644 if (oidcmp(&oids.list[i-1], &oids.list[i]))
645 count_distinct++;
648 if (count_distinct >= GRAPH_PARENT_MISSING)
649 die(_("the commit graph format cannot write %d commits"), count_distinct);
651 commits.nr = 0;
652 commits.alloc = count_distinct;
653 ALLOC_ARRAY(commits.list, commits.alloc);
655 num_extra_edges = 0;
656 for (i = 0; i < oids.nr; i++) {
657 int num_parents = 0;
658 if (i > 0 && !oidcmp(&oids.list[i-1], &oids.list[i]))
659 continue;
661 commits.list[commits.nr] = lookup_commit(&oids.list[i]);
662 parse_commit(commits.list[commits.nr]);
664 for (parent = commits.list[commits.nr]->parents;
665 parent; parent = parent->next)
666 num_parents++;
668 if (num_parents > 2)
669 num_extra_edges += num_parents - 1;
671 commits.nr++;
673 num_chunks = num_extra_edges ? 4 : 3;
675 if (commits.nr >= GRAPH_PARENT_MISSING)
676 die(_("too many commits to write graph"));
678 graph_name = get_commit_graph_filename(obj_dir);
679 fd = hold_lock_file_for_update(&lk, graph_name, 0);
681 if (fd < 0) {
682 struct strbuf folder = STRBUF_INIT;
683 strbuf_addstr(&folder, graph_name);
684 strbuf_setlen(&folder, strrchr(folder.buf, '/') - folder.buf);
686 if (mkdir(folder.buf, 0777) < 0)
687 die_errno(_("cannot mkdir %s"), folder.buf);
688 strbuf_release(&folder);
690 fd = hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
692 if (fd < 0)
693 die_errno("unable to create '%s'", graph_name);
696 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
698 hashwrite_be32(f, GRAPH_SIGNATURE);
700 hashwrite_u8(f, GRAPH_VERSION);
701 hashwrite_u8(f, GRAPH_OID_VERSION);
702 hashwrite_u8(f, num_chunks);
703 hashwrite_u8(f, 0); /* unused padding byte */
705 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
706 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
707 chunk_ids[2] = GRAPH_CHUNKID_DATA;
708 if (num_extra_edges)
709 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
710 else
711 chunk_ids[3] = 0;
712 chunk_ids[4] = 0;
714 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
715 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
716 chunk_offsets[2] = chunk_offsets[1] + GRAPH_OID_LEN * commits.nr;
717 chunk_offsets[3] = chunk_offsets[2] + (GRAPH_OID_LEN + 16) * commits.nr;
718 chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
720 for (i = 0; i <= num_chunks; i++) {
721 uint32_t chunk_write[3];
723 chunk_write[0] = htonl(chunk_ids[i]);
724 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
725 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
726 hashwrite(f, chunk_write, 12);
729 write_graph_chunk_fanout(f, commits.list, commits.nr);
730 write_graph_chunk_oids(f, GRAPH_OID_LEN, commits.list, commits.nr);
731 write_graph_chunk_data(f, GRAPH_OID_LEN, commits.list, commits.nr);
732 write_graph_chunk_large_edges(f, commits.list, commits.nr);
734 close_commit_graph();
735 finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
736 commit_lock_file(&lk);
738 free(oids.list);
739 oids.alloc = 0;
740 oids.nr = 0;