Merge branch 'jt/get-reference-with-commit-graph'
[git] / commit-graph.c
1 #include "cache.h"
2 #include "config.h"
3 #include "dir.h"
4 #include "git-compat-util.h"
5 #include "lockfile.h"
6 #include "pack.h"
7 #include "packfile.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "refs.h"
11 #include "revision.h"
12 #include "sha1-lookup.h"
13 #include "commit-graph.h"
14 #include "object-store.h"
15 #include "alloc.h"
16 #include "hashmap.h"
17 #include "replace-object.h"
18 #include "progress.h"
19
20 #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
21 #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
22 #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
23 #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
24 #define GRAPH_CHUNKID_LARGEEDGES 0x45444745 /* "EDGE" */
25
26 #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
27
28 #define GRAPH_VERSION_1 0x1
29 #define GRAPH_VERSION GRAPH_VERSION_1
30
31 #define GRAPH_OCTOPUS_EDGES_NEEDED 0x80000000
32 #define GRAPH_EDGE_LAST_MASK 0x7fffffff
33 #define GRAPH_PARENT_NONE 0x70000000
34
35 #define GRAPH_LAST_EDGE 0x80000000
36
37 #define GRAPH_HEADER_SIZE 8
38 #define GRAPH_FANOUT_SIZE (4 * 256)
39 #define GRAPH_CHUNKLOOKUP_WIDTH 12
40 #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
41                         + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
42
43 char *get_commit_graph_filename(const char *obj_dir)
44 {
45         return xstrfmt("%s/info/commit-graph", obj_dir);
46 }
47
48 static uint8_t oid_version(void)
49 {
50         return 1;
51 }
52
53 static struct commit_graph *alloc_commit_graph(void)
54 {
55         struct commit_graph *g = xcalloc(1, sizeof(*g));
56         g->graph_fd = -1;
57
58         return g;
59 }
60
61 extern int read_replace_refs;
62
63 static int commit_graph_compatible(struct repository *r)
64 {
65         if (!r->gitdir)
66                 return 0;
67
68         if (read_replace_refs) {
69                 prepare_replace_object(r);
70                 if (hashmap_get_size(&r->objects->replace_map->map))
71                         return 0;
72         }
73
74         prepare_commit_graft(r);
75         if (r->parsed_objects && r->parsed_objects->grafts_nr)
76                 return 0;
77         if (is_repository_shallow(r))
78                 return 0;
79
80         return 1;
81 }
82
83 struct commit_graph *load_commit_graph_one(const char *graph_file)
84 {
85         void *graph_map;
86         const unsigned char *data, *chunk_lookup;
87         size_t graph_size;
88         struct stat st;
89         uint32_t i;
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;
96
97         if (fd < 0)
98                 return NULL;
99         if (fstat(fd, &st)) {
100                 close(fd);
101                 return NULL;
102         }
103         graph_size = xsize_t(st.st_size);
104
105         if (graph_size < GRAPH_MIN_SIZE) {
106                 close(fd);
107                 die(_("graph file %s is too small"), graph_file);
108         }
109         graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
110         data = (const unsigned char *)graph_map;
111
112         graph_signature = get_be32(data);
113         if (graph_signature != GRAPH_SIGNATURE) {
114                 error(_("graph signature %X does not match signature %X"),
115                       graph_signature, GRAPH_SIGNATURE);
116                 goto cleanup_fail;
117         }
118
119         graph_version = *(unsigned char*)(data + 4);
120         if (graph_version != GRAPH_VERSION) {
121                 error(_("graph version %X does not match version %X"),
122                       graph_version, GRAPH_VERSION);
123                 goto cleanup_fail;
124         }
125
126         hash_version = *(unsigned char*)(data + 5);
127         if (hash_version != oid_version()) {
128                 error(_("hash version %X does not match version %X"),
129                       hash_version, oid_version());
130                 goto cleanup_fail;
131         }
132
133         graph = alloc_commit_graph();
134
135         graph->hash_len = the_hash_algo->rawsz;
136         graph->num_chunks = *(unsigned char*)(data + 6);
137         graph->graph_fd = fd;
138         graph->data = graph_map;
139         graph->data_len = graph_size;
140
141         last_chunk_id = 0;
142         last_chunk_offset = 8;
143         chunk_lookup = data + 8;
144         for (i = 0; i < graph->num_chunks; i++) {
145                 uint32_t chunk_id = get_be32(chunk_lookup + 0);
146                 uint64_t chunk_offset = get_be64(chunk_lookup + 4);
147                 int chunk_repeated = 0;
148
149                 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
150
151                 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
152                         error(_("improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
153                               (uint32_t)chunk_offset);
154                         goto cleanup_fail;
155                 }
156
157                 switch (chunk_id) {
158                 case GRAPH_CHUNKID_OIDFANOUT:
159                         if (graph->chunk_oid_fanout)
160                                 chunk_repeated = 1;
161                         else
162                                 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
163                         break;
164
165                 case GRAPH_CHUNKID_OIDLOOKUP:
166                         if (graph->chunk_oid_lookup)
167                                 chunk_repeated = 1;
168                         else
169                                 graph->chunk_oid_lookup = data + chunk_offset;
170                         break;
171
172                 case GRAPH_CHUNKID_DATA:
173                         if (graph->chunk_commit_data)
174                                 chunk_repeated = 1;
175                         else
176                                 graph->chunk_commit_data = data + chunk_offset;
177                         break;
178
179                 case GRAPH_CHUNKID_LARGEEDGES:
180                         if (graph->chunk_large_edges)
181                                 chunk_repeated = 1;
182                         else
183                                 graph->chunk_large_edges = data + chunk_offset;
184                         break;
185                 }
186
187                 if (chunk_repeated) {
188                         error(_("chunk id %08x appears multiple times"), chunk_id);
189                         goto cleanup_fail;
190                 }
191
192                 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
193                 {
194                         graph->num_commits = (chunk_offset - last_chunk_offset)
195                                              / graph->hash_len;
196                 }
197
198                 last_chunk_id = chunk_id;
199                 last_chunk_offset = chunk_offset;
200         }
201
202         return graph;
203
204 cleanup_fail:
205         munmap(graph_map, graph_size);
206         close(fd);
207         exit(1);
208 }
209
210 static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
211 {
212         char *graph_name;
213
214         if (r->objects->commit_graph)
215                 return;
216
217         graph_name = get_commit_graph_filename(obj_dir);
218         r->objects->commit_graph =
219                 load_commit_graph_one(graph_name);
220
221         FREE_AND_NULL(graph_name);
222 }
223
224 /*
225  * Return 1 if commit_graph is non-NULL, and 0 otherwise.
226  *
227  * On the first invocation, this function attemps to load the commit
228  * graph if the_repository is configured to have one.
229  */
230 static int prepare_commit_graph(struct repository *r)
231 {
232         struct object_directory *odb;
233         int config_value;
234
235         if (r->objects->commit_graph_attempted)
236                 return !!r->objects->commit_graph;
237         r->objects->commit_graph_attempted = 1;
238
239         if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
240             (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
241             !config_value))
242                 /*
243                  * This repository is not configured to use commit graphs, so
244                  * do not load one. (But report commit_graph_attempted anyway
245                  * so that commit graph loading is not attempted again for this
246                  * repository.)
247                  */
248                 return 0;
249
250         if (!commit_graph_compatible(r))
251                 return 0;
252
253         prepare_alt_odb(r);
254         for (odb = r->objects->odb;
255              !r->objects->commit_graph && odb;
256              odb = odb->next)
257                 prepare_commit_graph_one(r, odb->path);
258         return !!r->objects->commit_graph;
259 }
260
261 int generation_numbers_enabled(struct repository *r)
262 {
263         uint32_t first_generation;
264         struct commit_graph *g;
265         if (!prepare_commit_graph(r))
266                return 0;
267
268         g = r->objects->commit_graph;
269
270         if (!g->num_commits)
271                 return 0;
272
273         first_generation = get_be32(g->chunk_commit_data +
274                                     g->hash_len + 8) >> 2;
275
276         return !!first_generation;
277 }
278
279 void close_commit_graph(struct repository *r)
280 {
281         free_commit_graph(r->objects->commit_graph);
282         r->objects->commit_graph = NULL;
283 }
284
285 static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
286 {
287         return bsearch_hash(oid->hash, g->chunk_oid_fanout,
288                             g->chunk_oid_lookup, g->hash_len, pos);
289 }
290
291 static struct commit_list **insert_parent_or_die(struct repository *r,
292                                                  struct commit_graph *g,
293                                                  uint64_t pos,
294                                                  struct commit_list **pptr)
295 {
296         struct commit *c;
297         struct object_id oid;
298
299         if (pos >= g->num_commits)
300                 die("invalid parent position %"PRIu64, pos);
301
302         hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos);
303         c = lookup_commit(r, &oid);
304         if (!c)
305                 die(_("could not find commit %s"), oid_to_hex(&oid));
306         c->graph_pos = pos;
307         return &commit_list_insert(c, pptr)->next;
308 }
309
310 static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
311 {
312         const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos;
313         item->graph_pos = pos;
314         item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
315 }
316
317 static int fill_commit_in_graph(struct repository *r,
318                                 struct commit *item,
319                                 struct commit_graph *g, uint32_t pos)
320 {
321         uint32_t edge_value;
322         uint32_t *parent_data_ptr;
323         uint64_t date_low, date_high;
324         struct commit_list **pptr;
325         const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos;
326
327         item->object.parsed = 1;
328         item->graph_pos = pos;
329
330         item->maybe_tree = NULL;
331
332         date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
333         date_low = get_be32(commit_data + g->hash_len + 12);
334         item->date = (timestamp_t)((date_high << 32) | date_low);
335
336         item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
337
338         pptr = &item->parents;
339
340         edge_value = get_be32(commit_data + g->hash_len);
341         if (edge_value == GRAPH_PARENT_NONE)
342                 return 1;
343         pptr = insert_parent_or_die(r, g, edge_value, pptr);
344
345         edge_value = get_be32(commit_data + g->hash_len + 4);
346         if (edge_value == GRAPH_PARENT_NONE)
347                 return 1;
348         if (!(edge_value & GRAPH_OCTOPUS_EDGES_NEEDED)) {
349                 pptr = insert_parent_or_die(r, g, edge_value, pptr);
350                 return 1;
351         }
352
353         parent_data_ptr = (uint32_t*)(g->chunk_large_edges +
354                           4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
355         do {
356                 edge_value = get_be32(parent_data_ptr);
357                 pptr = insert_parent_or_die(r, g,
358                                             edge_value & GRAPH_EDGE_LAST_MASK,
359                                             pptr);
360                 parent_data_ptr++;
361         } while (!(edge_value & GRAPH_LAST_EDGE));
362
363         return 1;
364 }
365
366 static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
367 {
368         if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
369                 *pos = item->graph_pos;
370                 return 1;
371         } else {
372                 return bsearch_graph(g, &(item->object.oid), pos);
373         }
374 }
375
376 static int parse_commit_in_graph_one(struct repository *r,
377                                      struct commit_graph *g,
378                                      struct commit *item)
379 {
380         uint32_t pos;
381
382         if (item->object.parsed)
383                 return 1;
384
385         if (find_commit_in_graph(item, g, &pos))
386                 return fill_commit_in_graph(r, item, g, pos);
387
388         return 0;
389 }
390
391 int parse_commit_in_graph(struct repository *r, struct commit *item)
392 {
393         if (!prepare_commit_graph(r))
394                 return 0;
395         return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
396 }
397
398 void load_commit_graph_info(struct repository *r, struct commit *item)
399 {
400         uint32_t pos;
401         if (!prepare_commit_graph(r))
402                 return;
403         if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
404                 fill_commit_graph_info(item, r->objects->commit_graph, pos);
405 }
406
407 static struct tree *load_tree_for_commit(struct repository *r,
408                                          struct commit_graph *g,
409                                          struct commit *c)
410 {
411         struct object_id oid;
412         const unsigned char *commit_data = g->chunk_commit_data +
413                                            GRAPH_DATA_WIDTH * (c->graph_pos);
414
415         hashcpy(oid.hash, commit_data);
416         c->maybe_tree = lookup_tree(r, &oid);
417
418         return c->maybe_tree;
419 }
420
421 static struct tree *get_commit_tree_in_graph_one(struct repository *r,
422                                                  struct commit_graph *g,
423                                                  const struct commit *c)
424 {
425         if (c->maybe_tree)
426                 return c->maybe_tree;
427         if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
428                 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
429
430         return load_tree_for_commit(r, g, (struct commit *)c);
431 }
432
433 struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
434 {
435         return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
436 }
437
438 static void write_graph_chunk_fanout(struct hashfile *f,
439                                      struct commit **commits,
440                                      int nr_commits)
441 {
442         int i, count = 0;
443         struct commit **list = commits;
444
445         /*
446          * Write the first-level table (the list is sorted,
447          * but we use a 256-entry lookup to be able to avoid
448          * having to do eight extra binary search iterations).
449          */
450         for (i = 0; i < 256; i++) {
451                 while (count < nr_commits) {
452                         if ((*list)->object.oid.hash[0] != i)
453                                 break;
454                         count++;
455                         list++;
456                 }
457
458                 hashwrite_be32(f, count);
459         }
460 }
461
462 static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
463                                    struct commit **commits, int nr_commits)
464 {
465         struct commit **list = commits;
466         int count;
467         for (count = 0; count < nr_commits; count++, list++)
468                 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
469 }
470
471 static const unsigned char *commit_to_sha1(size_t index, void *table)
472 {
473         struct commit **commits = table;
474         return commits[index]->object.oid.hash;
475 }
476
477 static void write_graph_chunk_data(struct hashfile *f, int hash_len,
478                                    struct commit **commits, int nr_commits)
479 {
480         struct commit **list = commits;
481         struct commit **last = commits + nr_commits;
482         uint32_t num_extra_edges = 0;
483
484         while (list < last) {
485                 struct commit_list *parent;
486                 int edge_value;
487                 uint32_t packedDate[2];
488
489                 parse_commit(*list);
490                 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
491
492                 parent = (*list)->parents;
493
494                 if (!parent)
495                         edge_value = GRAPH_PARENT_NONE;
496                 else {
497                         edge_value = sha1_pos(parent->item->object.oid.hash,
498                                               commits,
499                                               nr_commits,
500                                               commit_to_sha1);
501
502                         if (edge_value < 0)
503                                 BUG("missing parent %s for commit %s",
504                                     oid_to_hex(&parent->item->object.oid),
505                                     oid_to_hex(&(*list)->object.oid));
506                 }
507
508                 hashwrite_be32(f, edge_value);
509
510                 if (parent)
511                         parent = parent->next;
512
513                 if (!parent)
514                         edge_value = GRAPH_PARENT_NONE;
515                 else if (parent->next)
516                         edge_value = GRAPH_OCTOPUS_EDGES_NEEDED | num_extra_edges;
517                 else {
518                         edge_value = sha1_pos(parent->item->object.oid.hash,
519                                               commits,
520                                               nr_commits,
521                                               commit_to_sha1);
522                         if (edge_value < 0)
523                                 BUG("missing parent %s for commit %s",
524                                     oid_to_hex(&parent->item->object.oid),
525                                     oid_to_hex(&(*list)->object.oid));
526                 }
527
528                 hashwrite_be32(f, edge_value);
529
530                 if (edge_value & GRAPH_OCTOPUS_EDGES_NEEDED) {
531                         do {
532                                 num_extra_edges++;
533                                 parent = parent->next;
534                         } while (parent);
535                 }
536
537                 if (sizeof((*list)->date) > 4)
538                         packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
539                 else
540                         packedDate[0] = 0;
541
542                 packedDate[0] |= htonl((*list)->generation << 2);
543
544                 packedDate[1] = htonl((*list)->date);
545                 hashwrite(f, packedDate, 8);
546
547                 list++;
548         }
549 }
550
551 static void write_graph_chunk_large_edges(struct hashfile *f,
552                                           struct commit **commits,
553                                           int nr_commits)
554 {
555         struct commit **list = commits;
556         struct commit **last = commits + nr_commits;
557         struct commit_list *parent;
558
559         while (list < last) {
560                 int num_parents = 0;
561                 for (parent = (*list)->parents; num_parents < 3 && parent;
562                      parent = parent->next)
563                         num_parents++;
564
565                 if (num_parents <= 2) {
566                         list++;
567                         continue;
568                 }
569
570                 /* Since num_parents > 2, this initializer is safe. */
571                 for (parent = (*list)->parents->next; parent; parent = parent->next) {
572                         int edge_value = sha1_pos(parent->item->object.oid.hash,
573                                                   commits,
574                                                   nr_commits,
575                                                   commit_to_sha1);
576
577                         if (edge_value < 0)
578                                 BUG("missing parent %s for commit %s",
579                                     oid_to_hex(&parent->item->object.oid),
580                                     oid_to_hex(&(*list)->object.oid));
581                         else if (!parent->next)
582                                 edge_value |= GRAPH_LAST_EDGE;
583
584                         hashwrite_be32(f, edge_value);
585                 }
586
587                 list++;
588         }
589 }
590
591 static int commit_compare(const void *_a, const void *_b)
592 {
593         const struct object_id *a = (const struct object_id *)_a;
594         const struct object_id *b = (const struct object_id *)_b;
595         return oidcmp(a, b);
596 }
597
598 struct packed_commit_list {
599         struct commit **list;
600         int nr;
601         int alloc;
602 };
603
604 struct packed_oid_list {
605         struct object_id *list;
606         int nr;
607         int alloc;
608         struct progress *progress;
609         int progress_done;
610 };
611
612 static int add_packed_commits(const struct object_id *oid,
613                               struct packed_git *pack,
614                               uint32_t pos,
615                               void *data)
616 {
617         struct packed_oid_list *list = (struct packed_oid_list*)data;
618         enum object_type type;
619         off_t offset = nth_packed_object_offset(pack, pos);
620         struct object_info oi = OBJECT_INFO_INIT;
621
622         if (list->progress)
623                 display_progress(list->progress, ++list->progress_done);
624
625         oi.typep = &type;
626         if (packed_object_info(the_repository, pack, offset, &oi) < 0)
627                 die(_("unable to get type of object %s"), oid_to_hex(oid));
628
629         if (type != OBJ_COMMIT)
630                 return 0;
631
632         ALLOC_GROW(list->list, list->nr + 1, list->alloc);
633         oidcpy(&(list->list[list->nr]), oid);
634         list->nr++;
635
636         return 0;
637 }
638
639 static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit)
640 {
641         struct commit_list *parent;
642         for (parent = commit->parents; parent; parent = parent->next) {
643                 if (!(parent->item->object.flags & UNINTERESTING)) {
644                         ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc);
645                         oidcpy(&oids->list[oids->nr], &(parent->item->object.oid));
646                         oids->nr++;
647                         parent->item->object.flags |= UNINTERESTING;
648                 }
649         }
650 }
651
652 static void close_reachable(struct packed_oid_list *oids, int report_progress)
653 {
654         int i, j;
655         struct commit *commit;
656         struct progress *progress = NULL;
657
658         if (report_progress)
659                 progress = start_delayed_progress(
660                         _("Loading known commits in commit graph"), j = 0);
661         for (i = 0; i < oids->nr; i++) {
662                 display_progress(progress, ++j);
663                 commit = lookup_commit(the_repository, &oids->list[i]);
664                 if (commit)
665                         commit->object.flags |= UNINTERESTING;
666         }
667         stop_progress(&progress);
668
669         /*
670          * As this loop runs, oids->nr may grow, but not more
671          * than the number of missing commits in the reachable
672          * closure.
673          */
674         if (report_progress)
675                 progress = start_delayed_progress(
676                         _("Expanding reachable commits in commit graph"), j = 0);
677         for (i = 0; i < oids->nr; i++) {
678                 display_progress(progress, ++j);
679                 commit = lookup_commit(the_repository, &oids->list[i]);
680
681                 if (commit && !parse_commit(commit))
682                         add_missing_parents(oids, commit);
683         }
684         stop_progress(&progress);
685
686         if (report_progress)
687                 progress = start_delayed_progress(
688                         _("Clearing commit marks in commit graph"), j = 0);
689         for (i = 0; i < oids->nr; i++) {
690                 display_progress(progress, ++j);
691                 commit = lookup_commit(the_repository, &oids->list[i]);
692
693                 if (commit)
694                         commit->object.flags &= ~UNINTERESTING;
695         }
696         stop_progress(&progress);
697 }
698
699 static void compute_generation_numbers(struct packed_commit_list* commits,
700                                        int report_progress)
701 {
702         int i;
703         struct commit_list *list = NULL;
704         struct progress *progress = NULL;
705
706         if (report_progress)
707                 progress = start_progress(
708                         _("Computing commit graph generation numbers"),
709                         commits->nr);
710         for (i = 0; i < commits->nr; i++) {
711                 display_progress(progress, i + 1);
712                 if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY &&
713                     commits->list[i]->generation != GENERATION_NUMBER_ZERO)
714                         continue;
715
716                 commit_list_insert(commits->list[i], &list);
717                 while (list) {
718                         struct commit *current = list->item;
719                         struct commit_list *parent;
720                         int all_parents_computed = 1;
721                         uint32_t max_generation = 0;
722
723                         for (parent = current->parents; parent; parent = parent->next) {
724                                 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
725                                     parent->item->generation == GENERATION_NUMBER_ZERO) {
726                                         all_parents_computed = 0;
727                                         commit_list_insert(parent->item, &list);
728                                         break;
729                                 } else if (parent->item->generation > max_generation) {
730                                         max_generation = parent->item->generation;
731                                 }
732                         }
733
734                         if (all_parents_computed) {
735                                 current->generation = max_generation + 1;
736                                 pop_commit(&list);
737
738                                 if (current->generation > GENERATION_NUMBER_MAX)
739                                         current->generation = GENERATION_NUMBER_MAX;
740                         }
741                 }
742         }
743         stop_progress(&progress);
744 }
745
746 static int add_ref_to_list(const char *refname,
747                            const struct object_id *oid,
748                            int flags, void *cb_data)
749 {
750         struct string_list *list = (struct string_list *)cb_data;
751
752         string_list_append(list, oid_to_hex(oid));
753         return 0;
754 }
755
756 void write_commit_graph_reachable(const char *obj_dir, int append,
757                                   int report_progress)
758 {
759         struct string_list list = STRING_LIST_INIT_DUP;
760
761         for_each_ref(add_ref_to_list, &list);
762         write_commit_graph(obj_dir, NULL, &list, append, report_progress);
763
764         string_list_clear(&list, 0);
765 }
766
767 void write_commit_graph(const char *obj_dir,
768                         struct string_list *pack_indexes,
769                         struct string_list *commit_hex,
770                         int append, int report_progress)
771 {
772         struct packed_oid_list oids;
773         struct packed_commit_list commits;
774         struct hashfile *f;
775         uint32_t i, count_distinct = 0;
776         char *graph_name;
777         struct lock_file lk = LOCK_INIT;
778         uint32_t chunk_ids[5];
779         uint64_t chunk_offsets[5];
780         int num_chunks;
781         int num_extra_edges;
782         struct commit_list *parent;
783         struct progress *progress = NULL;
784         const unsigned hashsz = the_hash_algo->rawsz;
785
786         if (!commit_graph_compatible(the_repository))
787                 return;
788
789         oids.nr = 0;
790         oids.alloc = approximate_object_count() / 32;
791         oids.progress = NULL;
792         oids.progress_done = 0;
793
794         if (append) {
795                 prepare_commit_graph_one(the_repository, obj_dir);
796                 if (the_repository->objects->commit_graph)
797                         oids.alloc += the_repository->objects->commit_graph->num_commits;
798         }
799
800         if (oids.alloc < 1024)
801                 oids.alloc = 1024;
802         ALLOC_ARRAY(oids.list, oids.alloc);
803
804         if (append && the_repository->objects->commit_graph) {
805                 struct commit_graph *commit_graph =
806                         the_repository->objects->commit_graph;
807                 for (i = 0; i < commit_graph->num_commits; i++) {
808                         const unsigned char *hash = commit_graph->chunk_oid_lookup +
809                                 commit_graph->hash_len * i;
810                         hashcpy(oids.list[oids.nr++].hash, hash);
811                 }
812         }
813
814         if (pack_indexes) {
815                 struct strbuf packname = STRBUF_INIT;
816                 int dirlen;
817                 strbuf_addf(&packname, "%s/pack/", obj_dir);
818                 dirlen = packname.len;
819                 if (report_progress) {
820                         oids.progress = start_delayed_progress(
821                                 _("Finding commits for commit graph"), 0);
822                         oids.progress_done = 0;
823                 }
824                 for (i = 0; i < pack_indexes->nr; i++) {
825                         struct packed_git *p;
826                         strbuf_setlen(&packname, dirlen);
827                         strbuf_addstr(&packname, pack_indexes->items[i].string);
828                         p = add_packed_git(packname.buf, packname.len, 1);
829                         if (!p)
830                                 die(_("error adding pack %s"), packname.buf);
831                         if (open_pack_index(p))
832                                 die(_("error opening index for %s"), packname.buf);
833                         for_each_object_in_pack(p, add_packed_commits, &oids, 0);
834                         close_pack(p);
835                         free(p);
836                 }
837                 stop_progress(&oids.progress);
838                 strbuf_release(&packname);
839         }
840
841         if (commit_hex) {
842                 if (report_progress)
843                         progress = start_delayed_progress(
844                                 _("Finding commits for commit graph"),
845                                 commit_hex->nr);
846                 for (i = 0; i < commit_hex->nr; i++) {
847                         const char *end;
848                         struct object_id oid;
849                         struct commit *result;
850
851                         display_progress(progress, i + 1);
852                         if (commit_hex->items[i].string &&
853                             parse_oid_hex(commit_hex->items[i].string, &oid, &end))
854                                 continue;
855
856                         result = lookup_commit_reference_gently(the_repository, &oid, 1);
857
858                         if (result) {
859                                 ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc);
860                                 oidcpy(&oids.list[oids.nr], &(result->object.oid));
861                                 oids.nr++;
862                         }
863                 }
864                 stop_progress(&progress);
865         }
866
867         if (!pack_indexes && !commit_hex) {
868                 if (report_progress)
869                         oids.progress = start_delayed_progress(
870                                 _("Finding commits for commit graph"), 0);
871                 for_each_packed_object(add_packed_commits, &oids, 0);
872                 stop_progress(&oids.progress);
873         }
874
875         close_reachable(&oids, report_progress);
876
877         QSORT(oids.list, oids.nr, commit_compare);
878
879         count_distinct = 1;
880         for (i = 1; i < oids.nr; i++) {
881                 if (!oideq(&oids.list[i - 1], &oids.list[i]))
882                         count_distinct++;
883         }
884
885         if (count_distinct >= GRAPH_EDGE_LAST_MASK)
886                 die(_("the commit graph format cannot write %d commits"), count_distinct);
887
888         commits.nr = 0;
889         commits.alloc = count_distinct;
890         ALLOC_ARRAY(commits.list, commits.alloc);
891
892         num_extra_edges = 0;
893         for (i = 0; i < oids.nr; i++) {
894                 int num_parents = 0;
895                 if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i]))
896                         continue;
897
898                 commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]);
899                 parse_commit(commits.list[commits.nr]);
900
901                 for (parent = commits.list[commits.nr]->parents;
902                      parent; parent = parent->next)
903                         num_parents++;
904
905                 if (num_parents > 2)
906                         num_extra_edges += num_parents - 1;
907
908                 commits.nr++;
909         }
910         num_chunks = num_extra_edges ? 4 : 3;
911
912         if (commits.nr >= GRAPH_EDGE_LAST_MASK)
913                 die(_("too many commits to write graph"));
914
915         compute_generation_numbers(&commits, report_progress);
916
917         graph_name = get_commit_graph_filename(obj_dir);
918         if (safe_create_leading_directories(graph_name)) {
919                 UNLEAK(graph_name);
920                 die_errno(_("unable to create leading directories of %s"),
921                           graph_name);
922         }
923
924         hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR);
925         f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
926
927         hashwrite_be32(f, GRAPH_SIGNATURE);
928
929         hashwrite_u8(f, GRAPH_VERSION);
930         hashwrite_u8(f, oid_version());
931         hashwrite_u8(f, num_chunks);
932         hashwrite_u8(f, 0); /* unused padding byte */
933
934         chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
935         chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
936         chunk_ids[2] = GRAPH_CHUNKID_DATA;
937         if (num_extra_edges)
938                 chunk_ids[3] = GRAPH_CHUNKID_LARGEEDGES;
939         else
940                 chunk_ids[3] = 0;
941         chunk_ids[4] = 0;
942
943         chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
944         chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
945         chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr;
946         chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr;
947         chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges;
948
949         for (i = 0; i <= num_chunks; i++) {
950                 uint32_t chunk_write[3];
951
952                 chunk_write[0] = htonl(chunk_ids[i]);
953                 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
954                 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
955                 hashwrite(f, chunk_write, 12);
956         }
957
958         write_graph_chunk_fanout(f, commits.list, commits.nr);
959         write_graph_chunk_oids(f, hashsz, commits.list, commits.nr);
960         write_graph_chunk_data(f, hashsz, commits.list, commits.nr);
961         write_graph_chunk_large_edges(f, commits.list, commits.nr);
962
963         close_commit_graph(the_repository);
964         finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
965         commit_lock_file(&lk);
966
967         free(graph_name);
968         free(commits.list);
969         free(oids.list);
970 }
971
972 #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
973 static int verify_commit_graph_error;
974
975 static void graph_report(const char *fmt, ...)
976 {
977         va_list ap;
978
979         verify_commit_graph_error = 1;
980         va_start(ap, fmt);
981         vfprintf(stderr, fmt, ap);
982         fprintf(stderr, "\n");
983         va_end(ap);
984 }
985
986 #define GENERATION_ZERO_EXISTS 1
987 #define GENERATION_NUMBER_EXISTS 2
988
989 int verify_commit_graph(struct repository *r, struct commit_graph *g)
990 {
991         uint32_t i, cur_fanout_pos = 0;
992         struct object_id prev_oid, cur_oid, checksum;
993         int generation_zero = 0;
994         struct hashfile *f;
995         int devnull;
996         struct progress *progress = NULL;
997
998         if (!g) {
999                 graph_report("no commit-graph file loaded");
1000                 return 1;
1001         }
1002
1003         verify_commit_graph_error = 0;
1004
1005         if (!g->chunk_oid_fanout)
1006                 graph_report("commit-graph is missing the OID Fanout chunk");
1007         if (!g->chunk_oid_lookup)
1008                 graph_report("commit-graph is missing the OID Lookup chunk");
1009         if (!g->chunk_commit_data)
1010                 graph_report("commit-graph is missing the Commit Data chunk");
1011
1012         if (verify_commit_graph_error)
1013                 return verify_commit_graph_error;
1014
1015         devnull = open("/dev/null", O_WRONLY);
1016         f = hashfd(devnull, NULL);
1017         hashwrite(f, g->data, g->data_len - g->hash_len);
1018         finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1019         if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1020                 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1021                 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1022         }
1023
1024         for (i = 0; i < g->num_commits; i++) {
1025                 struct commit *graph_commit;
1026
1027                 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1028
1029                 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1030                         graph_report("commit-graph has incorrect OID order: %s then %s",
1031                                      oid_to_hex(&prev_oid),
1032                                      oid_to_hex(&cur_oid));
1033
1034                 oidcpy(&prev_oid, &cur_oid);
1035
1036                 while (cur_oid.hash[0] > cur_fanout_pos) {
1037                         uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1038
1039                         if (i != fanout_value)
1040                                 graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1041                                              cur_fanout_pos, fanout_value, i);
1042                         cur_fanout_pos++;
1043                 }
1044
1045                 graph_commit = lookup_commit(r, &cur_oid);
1046                 if (!parse_commit_in_graph_one(r, g, graph_commit))
1047                         graph_report("failed to parse %s from commit-graph",
1048                                      oid_to_hex(&cur_oid));
1049         }
1050
1051         while (cur_fanout_pos < 256) {
1052                 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1053
1054                 if (g->num_commits != fanout_value)
1055                         graph_report("commit-graph has incorrect fanout value: fanout[%d] = %u != %u",
1056                                      cur_fanout_pos, fanout_value, i);
1057
1058                 cur_fanout_pos++;
1059         }
1060
1061         if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1062                 return verify_commit_graph_error;
1063
1064         progress = start_progress(_("Verifying commits in commit graph"),
1065                                   g->num_commits);
1066         for (i = 0; i < g->num_commits; i++) {
1067                 struct commit *graph_commit, *odb_commit;
1068                 struct commit_list *graph_parents, *odb_parents;
1069                 uint32_t max_generation = 0;
1070
1071                 display_progress(progress, i + 1);
1072                 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1073
1074                 graph_commit = lookup_commit(r, &cur_oid);
1075                 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1076                 if (parse_commit_internal(odb_commit, 0, 0)) {
1077                         graph_report("failed to parse %s from object database",
1078                                      oid_to_hex(&cur_oid));
1079                         continue;
1080                 }
1081
1082                 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
1083                            get_commit_tree_oid(odb_commit)))
1084                         graph_report("root tree OID for commit %s in commit-graph is %s != %s",
1085                                      oid_to_hex(&cur_oid),
1086                                      oid_to_hex(get_commit_tree_oid(graph_commit)),
1087                                      oid_to_hex(get_commit_tree_oid(odb_commit)));
1088
1089                 graph_parents = graph_commit->parents;
1090                 odb_parents = odb_commit->parents;
1091
1092                 while (graph_parents) {
1093                         if (odb_parents == NULL) {
1094                                 graph_report("commit-graph parent list for commit %s is too long",
1095                                              oid_to_hex(&cur_oid));
1096                                 break;
1097                         }
1098
1099                         if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
1100                                 graph_report("commit-graph parent for %s is %s != %s",
1101                                              oid_to_hex(&cur_oid),
1102                                              oid_to_hex(&graph_parents->item->object.oid),
1103                                              oid_to_hex(&odb_parents->item->object.oid));
1104
1105                         if (graph_parents->item->generation > max_generation)
1106                                 max_generation = graph_parents->item->generation;
1107
1108                         graph_parents = graph_parents->next;
1109                         odb_parents = odb_parents->next;
1110                 }
1111
1112                 if (odb_parents != NULL)
1113                         graph_report("commit-graph parent list for commit %s terminates early",
1114                                      oid_to_hex(&cur_oid));
1115
1116                 if (!graph_commit->generation) {
1117                         if (generation_zero == GENERATION_NUMBER_EXISTS)
1118                                 graph_report("commit-graph has generation number zero for commit %s, but non-zero elsewhere",
1119                                              oid_to_hex(&cur_oid));
1120                         generation_zero = GENERATION_ZERO_EXISTS;
1121                 } else if (generation_zero == GENERATION_ZERO_EXISTS)
1122                         graph_report("commit-graph has non-zero generation number for commit %s, but zero elsewhere",
1123                                      oid_to_hex(&cur_oid));
1124
1125                 if (generation_zero == GENERATION_ZERO_EXISTS)
1126                         continue;
1127
1128                 /*
1129                  * If one of our parents has generation GENERATION_NUMBER_MAX, then
1130                  * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
1131                  * extra logic in the following condition.
1132                  */
1133                 if (max_generation == GENERATION_NUMBER_MAX)
1134                         max_generation--;
1135
1136                 if (graph_commit->generation != max_generation + 1)
1137                         graph_report("commit-graph generation for commit %s is %u != %u",
1138                                      oid_to_hex(&cur_oid),
1139                                      graph_commit->generation,
1140                                      max_generation + 1);
1141
1142                 if (graph_commit->date != odb_commit->date)
1143                         graph_report("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime,
1144                                      oid_to_hex(&cur_oid),
1145                                      graph_commit->date,
1146                                      odb_commit->date);
1147         }
1148         stop_progress(&progress);
1149
1150         return verify_commit_graph_error;
1151 }
1152
1153 void free_commit_graph(struct commit_graph *g)
1154 {
1155         if (!g)
1156                 return;
1157         if (g->graph_fd >= 0) {
1158                 munmap((void *)g->data, g->data_len);
1159                 g->data = NULL;
1160                 close(g->graph_fd);
1161         }
1162         free(g);
1163 }