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_EXTRAEDGES 0x45444745 /* "EDGE" */
25#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
26
27#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
28
29#define GRAPH_VERSION_1 0x1
30#define GRAPH_VERSION GRAPH_VERSION_1
31
32#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
33#define GRAPH_EDGE_LAST_MASK 0x7fffffff
34#define GRAPH_PARENT_NONE 0x70000000
35
36#define GRAPH_LAST_EDGE 0x80000000
37
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 + the_hash_algo->rawsz)
43
44char *get_commit_graph_filename(const char *obj_dir)
45{
46 char *filename = xstrfmt("%s/info/commit-graph", obj_dir);
47 char *normalized = xmalloc(strlen(filename) + 1);
48 normalize_path_copy(normalized, filename);
49 free(filename);
50 return normalized;
51}
52
53static char *get_split_graph_filename(const char *obj_dir,
54 const char *oid_hex)
55{
56 char *filename = xstrfmt("%s/info/commit-graphs/graph-%s.graph",
57 obj_dir,
58 oid_hex);
59 char *normalized = xmalloc(strlen(filename) + 1);
60 normalize_path_copy(normalized, filename);
61 free(filename);
62 return normalized;
63}
64
65static char *get_chain_filename(const char *obj_dir)
66{
67 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
68}
69
70static uint8_t oid_version(void)
71{
72 return 1;
73}
74
75static struct commit_graph *alloc_commit_graph(void)
76{
77 struct commit_graph *g = xcalloc(1, sizeof(*g));
78 g->graph_fd = -1;
79
80 return g;
81}
82
83extern int read_replace_refs;
84
85static int commit_graph_compatible(struct repository *r)
86{
87 if (!r->gitdir)
88 return 0;
89
90 if (read_replace_refs) {
91 prepare_replace_object(r);
92 if (hashmap_get_size(&r->objects->replace_map->map))
93 return 0;
94 }
95
96 prepare_commit_graft(r);
97 if (r->parsed_objects && r->parsed_objects->grafts_nr)
98 return 0;
99 if (is_repository_shallow(r))
100 return 0;
101
102 return 1;
103}
104
105int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
106{
107 *fd = git_open(graph_file);
108 if (*fd < 0)
109 return 0;
110 if (fstat(*fd, st)) {
111 close(*fd);
112 return 0;
113 }
114 return 1;
115}
116
117struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
118{
119 void *graph_map;
120 size_t graph_size;
121 struct commit_graph *ret;
122
123 graph_size = xsize_t(st->st_size);
124
125 if (graph_size < GRAPH_MIN_SIZE) {
126 close(fd);
127 error(_("commit-graph file is too small"));
128 return NULL;
129 }
130 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
131 ret = parse_commit_graph(graph_map, fd, graph_size);
132
133 if (!ret) {
134 munmap(graph_map, graph_size);
135 close(fd);
136 }
137
138 return ret;
139}
140
141static int verify_commit_graph_lite(struct commit_graph *g)
142{
143 /*
144 * Basic validation shared between parse_commit_graph()
145 * which'll be called every time the graph is used, and the
146 * much more expensive verify_commit_graph() used by
147 * "commit-graph verify".
148 *
149 * There should only be very basic checks here to ensure that
150 * we don't e.g. segfault in fill_commit_in_graph(), but
151 * because this is a very hot codepath nothing that e.g. loops
152 * over g->num_commits, or runs a checksum on the commit-graph
153 * itself.
154 */
155 if (!g->chunk_oid_fanout) {
156 error("commit-graph is missing the OID Fanout chunk");
157 return 1;
158 }
159 if (!g->chunk_oid_lookup) {
160 error("commit-graph is missing the OID Lookup chunk");
161 return 1;
162 }
163 if (!g->chunk_commit_data) {
164 error("commit-graph is missing the Commit Data chunk");
165 return 1;
166 }
167
168 return 0;
169}
170
171struct commit_graph *parse_commit_graph(void *graph_map, int fd,
172 size_t graph_size)
173{
174 const unsigned char *data, *chunk_lookup;
175 uint32_t i;
176 struct commit_graph *graph;
177 uint64_t last_chunk_offset;
178 uint32_t last_chunk_id;
179 uint32_t graph_signature;
180 unsigned char graph_version, hash_version;
181
182 if (!graph_map)
183 return NULL;
184
185 if (graph_size < GRAPH_MIN_SIZE)
186 return NULL;
187
188 data = (const unsigned char *)graph_map;
189
190 graph_signature = get_be32(data);
191 if (graph_signature != GRAPH_SIGNATURE) {
192 error(_("commit-graph signature %X does not match signature %X"),
193 graph_signature, GRAPH_SIGNATURE);
194 return NULL;
195 }
196
197 graph_version = *(unsigned char*)(data + 4);
198 if (graph_version != GRAPH_VERSION) {
199 error(_("commit-graph version %X does not match version %X"),
200 graph_version, GRAPH_VERSION);
201 return NULL;
202 }
203
204 hash_version = *(unsigned char*)(data + 5);
205 if (hash_version != oid_version()) {
206 error(_("commit-graph hash version %X does not match version %X"),
207 hash_version, oid_version());
208 return NULL;
209 }
210
211 graph = alloc_commit_graph();
212
213 graph->hash_len = the_hash_algo->rawsz;
214 graph->num_chunks = *(unsigned char*)(data + 6);
215 graph->graph_fd = fd;
216 graph->data = graph_map;
217 graph->data_len = graph_size;
218
219 last_chunk_id = 0;
220 last_chunk_offset = 8;
221 chunk_lookup = data + 8;
222 for (i = 0; i < graph->num_chunks; i++) {
223 uint32_t chunk_id;
224 uint64_t chunk_offset;
225 int chunk_repeated = 0;
226
227 if (data + graph_size - chunk_lookup <
228 GRAPH_CHUNKLOOKUP_WIDTH) {
229 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
230 free(graph);
231 return NULL;
232 }
233
234 chunk_id = get_be32(chunk_lookup + 0);
235 chunk_offset = get_be64(chunk_lookup + 4);
236
237 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
238
239 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
240 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
241 (uint32_t)chunk_offset);
242 free(graph);
243 return NULL;
244 }
245
246 switch (chunk_id) {
247 case GRAPH_CHUNKID_OIDFANOUT:
248 if (graph->chunk_oid_fanout)
249 chunk_repeated = 1;
250 else
251 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
252 break;
253
254 case GRAPH_CHUNKID_OIDLOOKUP:
255 if (graph->chunk_oid_lookup)
256 chunk_repeated = 1;
257 else
258 graph->chunk_oid_lookup = data + chunk_offset;
259 break;
260
261 case GRAPH_CHUNKID_DATA:
262 if (graph->chunk_commit_data)
263 chunk_repeated = 1;
264 else
265 graph->chunk_commit_data = data + chunk_offset;
266 break;
267
268 case GRAPH_CHUNKID_EXTRAEDGES:
269 if (graph->chunk_extra_edges)
270 chunk_repeated = 1;
271 else
272 graph->chunk_extra_edges = data + chunk_offset;
273 break;
274
275 case GRAPH_CHUNKID_BASE:
276 if (graph->chunk_base_graphs)
277 chunk_repeated = 1;
278 else
279 graph->chunk_base_graphs = data + chunk_offset;
280 }
281
282 if (chunk_repeated) {
283 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
284 free(graph);
285 return NULL;
286 }
287
288 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
289 {
290 graph->num_commits = (chunk_offset - last_chunk_offset)
291 / graph->hash_len;
292 }
293
294 last_chunk_id = chunk_id;
295 last_chunk_offset = chunk_offset;
296 }
297
298 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
299
300 if (verify_commit_graph_lite(graph))
301 return NULL;
302
303 return graph;
304}
305
306static struct commit_graph *load_commit_graph_one(const char *graph_file)
307{
308
309 struct stat st;
310 int fd;
311 struct commit_graph *g;
312 int open_ok = open_commit_graph(graph_file, &fd, &st);
313
314 if (!open_ok)
315 return NULL;
316
317 g = load_commit_graph_one_fd_st(fd, &st);
318
319 if (g)
320 g->filename = xstrdup(graph_file);
321
322 return g;
323}
324
325static struct commit_graph *load_commit_graph_v1(struct repository *r, const char *obj_dir)
326{
327 char *graph_name = get_commit_graph_filename(obj_dir);
328 struct commit_graph *g = load_commit_graph_one(graph_name);
329 free(graph_name);
330
331 if (g)
332 g->obj_dir = obj_dir;
333
334 return g;
335}
336
337static int add_graph_to_chain(struct commit_graph *g,
338 struct commit_graph *chain,
339 struct object_id *oids,
340 int n)
341{
342 struct commit_graph *cur_g = chain;
343
344 if (n && !g->chunk_base_graphs) {
345 warning(_("commit-graph has no base graphs chunk"));
346 return 0;
347 }
348
349 while (n) {
350 n--;
351
352 if (!cur_g ||
353 !oideq(&oids[n], &cur_g->oid) ||
354 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
355 warning(_("commit-graph chain does not match"));
356 return 0;
357 }
358
359 cur_g = cur_g->base_graph;
360 }
361
362 g->base_graph = chain;
363
364 if (chain)
365 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
366
367 return 1;
368}
369
370static struct commit_graph *load_commit_graph_chain(struct repository *r, const char *obj_dir)
371{
372 struct commit_graph *graph_chain = NULL;
373 struct strbuf line = STRBUF_INIT;
374 struct stat st;
375 struct object_id *oids;
376 int i = 0, valid = 1, count;
377 char *chain_name = get_chain_filename(obj_dir);
378 FILE *fp;
379 int stat_res;
380
381 fp = fopen(chain_name, "r");
382 stat_res = stat(chain_name, &st);
383 free(chain_name);
384
385 if (!fp ||
386 stat_res ||
387 st.st_size <= the_hash_algo->hexsz)
388 return NULL;
389
390 count = st.st_size / (the_hash_algo->hexsz + 1);
391 oids = xcalloc(count, sizeof(struct object_id));
392
393 prepare_alt_odb(r);
394
395 for (i = 0; i < count; i++) {
396 struct object_directory *odb;
397
398 if (strbuf_getline_lf(&line, fp) == EOF)
399 break;
400
401 if (get_oid_hex(line.buf, &oids[i])) {
402 warning(_("invalid commit-graph chain: line '%s' not a hash"),
403 line.buf);
404 valid = 0;
405 break;
406 }
407
408 valid = 0;
409 for (odb = r->objects->odb; odb; odb = odb->next) {
410 char *graph_name = get_split_graph_filename(odb->path, line.buf);
411 struct commit_graph *g = load_commit_graph_one(graph_name);
412
413 free(graph_name);
414
415 if (g) {
416 g->obj_dir = odb->path;
417
418 if (add_graph_to_chain(g, graph_chain, oids, i)) {
419 graph_chain = g;
420 valid = 1;
421 }
422
423 break;
424 }
425 }
426
427 if (!valid) {
428 warning(_("unable to find all commit-graph files"));
429 break;
430 }
431 }
432
433 free(oids);
434 fclose(fp);
435 strbuf_release(&line);
436
437 return graph_chain;
438}
439
440struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
441{
442 struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
443
444 if (!g)
445 g = load_commit_graph_chain(r, obj_dir);
446
447 return g;
448}
449
450static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
451{
452
453 if (r->objects->commit_graph)
454 return;
455
456 r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
457}
458
459/*
460 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
461 *
462 * On the first invocation, this function attemps to load the commit
463 * graph if the_repository is configured to have one.
464 */
465static int prepare_commit_graph(struct repository *r)
466{
467 struct object_directory *odb;
468 int config_value;
469
470 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
471 die("dying as requested by the '%s' variable on commit-graph load!",
472 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
473
474 if (r->objects->commit_graph_attempted)
475 return !!r->objects->commit_graph;
476 r->objects->commit_graph_attempted = 1;
477
478 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
479 (repo_config_get_bool(r, "core.commitgraph", &config_value) ||
480 !config_value))
481 /*
482 * This repository is not configured to use commit graphs, so
483 * do not load one. (But report commit_graph_attempted anyway
484 * so that commit graph loading is not attempted again for this
485 * repository.)
486 */
487 return 0;
488
489 if (!commit_graph_compatible(r))
490 return 0;
491
492 prepare_alt_odb(r);
493 for (odb = r->objects->odb;
494 !r->objects->commit_graph && odb;
495 odb = odb->next)
496 prepare_commit_graph_one(r, odb->path);
497 return !!r->objects->commit_graph;
498}
499
500int generation_numbers_enabled(struct repository *r)
501{
502 uint32_t first_generation;
503 struct commit_graph *g;
504 if (!prepare_commit_graph(r))
505 return 0;
506
507 g = r->objects->commit_graph;
508
509 if (!g->num_commits)
510 return 0;
511
512 first_generation = get_be32(g->chunk_commit_data +
513 g->hash_len + 8) >> 2;
514
515 return !!first_generation;
516}
517
518static void close_commit_graph_one(struct commit_graph *g)
519{
520 if (!g)
521 return;
522
523 close_commit_graph_one(g->base_graph);
524 free_commit_graph(g);
525}
526
527void close_commit_graph(struct raw_object_store *o)
528{
529 close_commit_graph_one(o->commit_graph);
530 o->commit_graph = NULL;
531}
532
533static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
534{
535 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
536 g->chunk_oid_lookup, g->hash_len, pos);
537}
538
539static void load_oid_from_graph(struct commit_graph *g,
540 uint32_t pos,
541 struct object_id *oid)
542{
543 uint32_t lex_index;
544
545 while (g && pos < g->num_commits_in_base)
546 g = g->base_graph;
547
548 if (!g)
549 BUG("NULL commit-graph");
550
551 if (pos >= g->num_commits + g->num_commits_in_base)
552 die(_("invalid commit position. commit-graph is likely corrupt"));
553
554 lex_index = pos - g->num_commits_in_base;
555
556 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
557}
558
559static struct commit_list **insert_parent_or_die(struct repository *r,
560 struct commit_graph *g,
561 uint32_t pos,
562 struct commit_list **pptr)
563{
564 struct commit *c;
565 struct object_id oid;
566
567 if (pos >= g->num_commits + g->num_commits_in_base)
568 die("invalid parent position %"PRIu32, pos);
569
570 load_oid_from_graph(g, pos, &oid);
571 c = lookup_commit(r, &oid);
572 if (!c)
573 die(_("could not find commit %s"), oid_to_hex(&oid));
574 c->graph_pos = pos;
575 return &commit_list_insert(c, pptr)->next;
576}
577
578static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
579{
580 const unsigned char *commit_data;
581 uint32_t lex_index;
582
583 while (pos < g->num_commits_in_base)
584 g = g->base_graph;
585
586 lex_index = pos - g->num_commits_in_base;
587 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
588 item->graph_pos = pos;
589 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
590}
591
592static int fill_commit_in_graph(struct repository *r,
593 struct commit *item,
594 struct commit_graph *g, uint32_t pos)
595{
596 uint32_t edge_value;
597 uint32_t *parent_data_ptr;
598 uint64_t date_low, date_high;
599 struct commit_list **pptr;
600 const unsigned char *commit_data;
601 uint32_t lex_index;
602
603 while (pos < g->num_commits_in_base)
604 g = g->base_graph;
605
606 if (pos >= g->num_commits + g->num_commits_in_base)
607 die(_("invalid commit position. commit-graph is likely corrupt"));
608
609 /*
610 * Store the "full" position, but then use the
611 * "local" position for the rest of the calculation.
612 */
613 item->graph_pos = pos;
614 lex_index = pos - g->num_commits_in_base;
615
616 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
617
618 item->object.parsed = 1;
619
620 item->maybe_tree = NULL;
621
622 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
623 date_low = get_be32(commit_data + g->hash_len + 12);
624 item->date = (timestamp_t)((date_high << 32) | date_low);
625
626 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
627
628 pptr = &item->parents;
629
630 edge_value = get_be32(commit_data + g->hash_len);
631 if (edge_value == GRAPH_PARENT_NONE)
632 return 1;
633 pptr = insert_parent_or_die(r, g, edge_value, pptr);
634
635 edge_value = get_be32(commit_data + g->hash_len + 4);
636 if (edge_value == GRAPH_PARENT_NONE)
637 return 1;
638 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
639 pptr = insert_parent_or_die(r, g, edge_value, pptr);
640 return 1;
641 }
642
643 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
644 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
645 do {
646 edge_value = get_be32(parent_data_ptr);
647 pptr = insert_parent_or_die(r, g,
648 edge_value & GRAPH_EDGE_LAST_MASK,
649 pptr);
650 parent_data_ptr++;
651 } while (!(edge_value & GRAPH_LAST_EDGE));
652
653 return 1;
654}
655
656static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
657{
658 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
659 *pos = item->graph_pos;
660 return 1;
661 } else {
662 struct commit_graph *cur_g = g;
663 uint32_t lex_index;
664
665 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
666 cur_g = cur_g->base_graph;
667
668 if (cur_g) {
669 *pos = lex_index + cur_g->num_commits_in_base;
670 return 1;
671 }
672
673 return 0;
674 }
675}
676
677static int parse_commit_in_graph_one(struct repository *r,
678 struct commit_graph *g,
679 struct commit *item)
680{
681 uint32_t pos;
682
683 if (item->object.parsed)
684 return 1;
685
686 if (find_commit_in_graph(item, g, &pos))
687 return fill_commit_in_graph(r, item, g, pos);
688
689 return 0;
690}
691
692int parse_commit_in_graph(struct repository *r, struct commit *item)
693{
694 if (!prepare_commit_graph(r))
695 return 0;
696 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
697}
698
699void load_commit_graph_info(struct repository *r, struct commit *item)
700{
701 uint32_t pos;
702 if (!prepare_commit_graph(r))
703 return;
704 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
705 fill_commit_graph_info(item, r->objects->commit_graph, pos);
706}
707
708static struct tree *load_tree_for_commit(struct repository *r,
709 struct commit_graph *g,
710 struct commit *c)
711{
712 struct object_id oid;
713 const unsigned char *commit_data;
714
715 while (c->graph_pos < g->num_commits_in_base)
716 g = g->base_graph;
717
718 commit_data = g->chunk_commit_data +
719 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
720
721 hashcpy(oid.hash, commit_data);
722 c->maybe_tree = lookup_tree(r, &oid);
723
724 return c->maybe_tree;
725}
726
727static struct tree *get_commit_tree_in_graph_one(struct repository *r,
728 struct commit_graph *g,
729 const struct commit *c)
730{
731 if (c->maybe_tree)
732 return c->maybe_tree;
733 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
734 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
735
736 return load_tree_for_commit(r, g, (struct commit *)c);
737}
738
739struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
740{
741 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
742}
743
744struct packed_commit_list {
745 struct commit **list;
746 int nr;
747 int alloc;
748};
749
750struct packed_oid_list {
751 struct object_id *list;
752 int nr;
753 int alloc;
754};
755
756struct write_commit_graph_context {
757 struct repository *r;
758 char *obj_dir;
759 char *graph_name;
760 struct packed_oid_list oids;
761 struct packed_commit_list commits;
762 int num_extra_edges;
763 unsigned long approx_nr_objects;
764 struct progress *progress;
765 int progress_done;
766 uint64_t progress_cnt;
767
768 char *base_graph_name;
769 int num_commit_graphs_before;
770 int num_commit_graphs_after;
771 char **commit_graph_filenames_before;
772 char **commit_graph_filenames_after;
773 char **commit_graph_hash_after;
774 uint32_t new_num_commits_in_base;
775 struct commit_graph *new_base_graph;
776
777 unsigned append:1,
778 report_progress:1,
779 split:1;
780
781 const struct split_commit_graph_opts *split_opts;
782};
783
784static void write_graph_chunk_fanout(struct hashfile *f,
785 struct write_commit_graph_context *ctx)
786{
787 int i, count = 0;
788 struct commit **list = ctx->commits.list;
789
790 /*
791 * Write the first-level table (the list is sorted,
792 * but we use a 256-entry lookup to be able to avoid
793 * having to do eight extra binary search iterations).
794 */
795 for (i = 0; i < 256; i++) {
796 while (count < ctx->commits.nr) {
797 if ((*list)->object.oid.hash[0] != i)
798 break;
799 display_progress(ctx->progress, ++ctx->progress_cnt);
800 count++;
801 list++;
802 }
803
804 hashwrite_be32(f, count);
805 }
806}
807
808static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
809 struct write_commit_graph_context *ctx)
810{
811 struct commit **list = ctx->commits.list;
812 int count;
813 for (count = 0; count < ctx->commits.nr; count++, list++) {
814 display_progress(ctx->progress, ++ctx->progress_cnt);
815 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
816 }
817}
818
819static const unsigned char *commit_to_sha1(size_t index, void *table)
820{
821 struct commit **commits = table;
822 return commits[index]->object.oid.hash;
823}
824
825static void write_graph_chunk_data(struct hashfile *f, int hash_len,
826 struct write_commit_graph_context *ctx)
827{
828 struct commit **list = ctx->commits.list;
829 struct commit **last = ctx->commits.list + ctx->commits.nr;
830 uint32_t num_extra_edges = 0;
831
832 while (list < last) {
833 struct commit_list *parent;
834 int edge_value;
835 uint32_t packedDate[2];
836 display_progress(ctx->progress, ++ctx->progress_cnt);
837
838 parse_commit_no_graph(*list);
839 hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len);
840
841 parent = (*list)->parents;
842
843 if (!parent)
844 edge_value = GRAPH_PARENT_NONE;
845 else {
846 edge_value = sha1_pos(parent->item->object.oid.hash,
847 ctx->commits.list,
848 ctx->commits.nr,
849 commit_to_sha1);
850
851 if (edge_value >= 0)
852 edge_value += ctx->new_num_commits_in_base;
853 else {
854 uint32_t pos;
855 if (find_commit_in_graph(parent->item,
856 ctx->new_base_graph,
857 &pos))
858 edge_value = pos;
859 }
860
861 if (edge_value < 0)
862 BUG("missing parent %s for commit %s",
863 oid_to_hex(&parent->item->object.oid),
864 oid_to_hex(&(*list)->object.oid));
865 }
866
867 hashwrite_be32(f, edge_value);
868
869 if (parent)
870 parent = parent->next;
871
872 if (!parent)
873 edge_value = GRAPH_PARENT_NONE;
874 else if (parent->next)
875 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
876 else {
877 edge_value = sha1_pos(parent->item->object.oid.hash,
878 ctx->commits.list,
879 ctx->commits.nr,
880 commit_to_sha1);
881
882 if (edge_value >= 0)
883 edge_value += ctx->new_num_commits_in_base;
884 else {
885 uint32_t pos;
886 if (find_commit_in_graph(parent->item,
887 ctx->new_base_graph,
888 &pos))
889 edge_value = pos;
890 }
891
892 if (edge_value < 0)
893 BUG("missing parent %s for commit %s",
894 oid_to_hex(&parent->item->object.oid),
895 oid_to_hex(&(*list)->object.oid));
896 }
897
898 hashwrite_be32(f, edge_value);
899
900 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
901 do {
902 num_extra_edges++;
903 parent = parent->next;
904 } while (parent);
905 }
906
907 if (sizeof((*list)->date) > 4)
908 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
909 else
910 packedDate[0] = 0;
911
912 packedDate[0] |= htonl((*list)->generation << 2);
913
914 packedDate[1] = htonl((*list)->date);
915 hashwrite(f, packedDate, 8);
916
917 list++;
918 }
919}
920
921static void write_graph_chunk_extra_edges(struct hashfile *f,
922 struct write_commit_graph_context *ctx)
923{
924 struct commit **list = ctx->commits.list;
925 struct commit **last = ctx->commits.list + ctx->commits.nr;
926 struct commit_list *parent;
927
928 while (list < last) {
929 int num_parents = 0;
930
931 display_progress(ctx->progress, ++ctx->progress_cnt);
932
933 for (parent = (*list)->parents; num_parents < 3 && parent;
934 parent = parent->next)
935 num_parents++;
936
937 if (num_parents <= 2) {
938 list++;
939 continue;
940 }
941
942 /* Since num_parents > 2, this initializer is safe. */
943 for (parent = (*list)->parents->next; parent; parent = parent->next) {
944 int edge_value = sha1_pos(parent->item->object.oid.hash,
945 ctx->commits.list,
946 ctx->commits.nr,
947 commit_to_sha1);
948
949 if (edge_value >= 0)
950 edge_value += ctx->new_num_commits_in_base;
951 else {
952 uint32_t pos;
953 if (find_commit_in_graph(parent->item,
954 ctx->new_base_graph,
955 &pos))
956 edge_value = pos;
957 }
958
959 if (edge_value < 0)
960 BUG("missing parent %s for commit %s",
961 oid_to_hex(&parent->item->object.oid),
962 oid_to_hex(&(*list)->object.oid));
963 else if (!parent->next)
964 edge_value |= GRAPH_LAST_EDGE;
965
966 hashwrite_be32(f, edge_value);
967 }
968
969 list++;
970 }
971}
972
973static int oid_compare(const void *_a, const void *_b)
974{
975 const struct object_id *a = (const struct object_id *)_a;
976 const struct object_id *b = (const struct object_id *)_b;
977 return oidcmp(a, b);
978}
979
980static int add_packed_commits(const struct object_id *oid,
981 struct packed_git *pack,
982 uint32_t pos,
983 void *data)
984{
985 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
986 enum object_type type;
987 off_t offset = nth_packed_object_offset(pack, pos);
988 struct object_info oi = OBJECT_INFO_INIT;
989
990 if (ctx->progress)
991 display_progress(ctx->progress, ++ctx->progress_done);
992
993 oi.typep = &type;
994 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
995 die(_("unable to get type of object %s"), oid_to_hex(oid));
996
997 if (type != OBJ_COMMIT)
998 return 0;
999
1000 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1001 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1002 ctx->oids.nr++;
1003
1004 return 0;
1005}
1006
1007static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
1008{
1009 struct commit_list *parent;
1010 for (parent = commit->parents; parent; parent = parent->next) {
1011 if (!(parent->item->object.flags & UNINTERESTING)) {
1012 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1013 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1014 ctx->oids.nr++;
1015 parent->item->object.flags |= UNINTERESTING;
1016 }
1017 }
1018}
1019
1020static void close_reachable(struct write_commit_graph_context *ctx)
1021{
1022 int i;
1023 struct commit *commit;
1024
1025 if (ctx->report_progress)
1026 ctx->progress = start_delayed_progress(
1027 _("Loading known commits in commit graph"),
1028 ctx->oids.nr);
1029 for (i = 0; i < ctx->oids.nr; i++) {
1030 display_progress(ctx->progress, i + 1);
1031 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1032 if (commit)
1033 commit->object.flags |= UNINTERESTING;
1034 }
1035 stop_progress(&ctx->progress);
1036
1037 /*
1038 * As this loop runs, ctx->oids.nr may grow, but not more
1039 * than the number of missing commits in the reachable
1040 * closure.
1041 */
1042 if (ctx->report_progress)
1043 ctx->progress = start_delayed_progress(
1044 _("Expanding reachable commits in commit graph"),
1045 ctx->oids.nr);
1046 for (i = 0; i < ctx->oids.nr; i++) {
1047 display_progress(ctx->progress, i + 1);
1048 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1049
1050 if (!commit)
1051 continue;
1052 if (ctx->split) {
1053 if (!parse_commit(commit) &&
1054 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1055 add_missing_parents(ctx, commit);
1056 } else if (!parse_commit_no_graph(commit))
1057 add_missing_parents(ctx, commit);
1058 }
1059 stop_progress(&ctx->progress);
1060
1061 if (ctx->report_progress)
1062 ctx->progress = start_delayed_progress(
1063 _("Clearing commit marks in commit graph"),
1064 ctx->oids.nr);
1065 for (i = 0; i < ctx->oids.nr; i++) {
1066 display_progress(ctx->progress, i + 1);
1067 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1068
1069 if (commit)
1070 commit->object.flags &= ~UNINTERESTING;
1071 }
1072 stop_progress(&ctx->progress);
1073}
1074
1075static void compute_generation_numbers(struct write_commit_graph_context *ctx)
1076{
1077 int i;
1078 struct commit_list *list = NULL;
1079
1080 if (ctx->report_progress)
1081 ctx->progress = start_progress(
1082 _("Computing commit graph generation numbers"),
1083 ctx->commits.nr);
1084 for (i = 0; i < ctx->commits.nr; i++) {
1085 display_progress(ctx->progress, i + 1);
1086 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1087 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
1088 continue;
1089
1090 commit_list_insert(ctx->commits.list[i], &list);
1091 while (list) {
1092 struct commit *current = list->item;
1093 struct commit_list *parent;
1094 int all_parents_computed = 1;
1095 uint32_t max_generation = 0;
1096
1097 for (parent = current->parents; parent; parent = parent->next) {
1098 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1099 parent->item->generation == GENERATION_NUMBER_ZERO) {
1100 all_parents_computed = 0;
1101 commit_list_insert(parent->item, &list);
1102 break;
1103 } else if (parent->item->generation > max_generation) {
1104 max_generation = parent->item->generation;
1105 }
1106 }
1107
1108 if (all_parents_computed) {
1109 current->generation = max_generation + 1;
1110 pop_commit(&list);
1111
1112 if (current->generation > GENERATION_NUMBER_MAX)
1113 current->generation = GENERATION_NUMBER_MAX;
1114 }
1115 }
1116 }
1117 stop_progress(&ctx->progress);
1118}
1119
1120static int add_ref_to_list(const char *refname,
1121 const struct object_id *oid,
1122 int flags, void *cb_data)
1123{
1124 struct string_list *list = (struct string_list *)cb_data;
1125
1126 string_list_append(list, oid_to_hex(oid));
1127 return 0;
1128}
1129
1130int write_commit_graph_reachable(const char *obj_dir, unsigned int flags,
1131 const struct split_commit_graph_opts *split_opts)
1132{
1133 struct string_list list = STRING_LIST_INIT_DUP;
1134 int result;
1135
1136 for_each_ref(add_ref_to_list, &list);
1137 result = write_commit_graph(obj_dir, NULL, &list,
1138 flags, split_opts);
1139
1140 string_list_clear(&list, 0);
1141 return result;
1142}
1143
1144static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1145 struct string_list *pack_indexes)
1146{
1147 uint32_t i;
1148 struct strbuf progress_title = STRBUF_INIT;
1149 struct strbuf packname = STRBUF_INIT;
1150 int dirlen;
1151
1152 strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
1153 dirlen = packname.len;
1154 if (ctx->report_progress) {
1155 strbuf_addf(&progress_title,
1156 Q_("Finding commits for commit graph in %d pack",
1157 "Finding commits for commit graph in %d packs",
1158 pack_indexes->nr),
1159 pack_indexes->nr);
1160 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1161 ctx->progress_done = 0;
1162 }
1163 for (i = 0; i < pack_indexes->nr; i++) {
1164 struct packed_git *p;
1165 strbuf_setlen(&packname, dirlen);
1166 strbuf_addstr(&packname, pack_indexes->items[i].string);
1167 p = add_packed_git(packname.buf, packname.len, 1);
1168 if (!p) {
1169 error(_("error adding pack %s"), packname.buf);
1170 return -1;
1171 }
1172 if (open_pack_index(p)) {
1173 error(_("error opening index for %s"), packname.buf);
1174 return -1;
1175 }
1176 for_each_object_in_pack(p, add_packed_commits, ctx,
1177 FOR_EACH_OBJECT_PACK_ORDER);
1178 close_pack(p);
1179 free(p);
1180 }
1181
1182 stop_progress(&ctx->progress);
1183 strbuf_release(&progress_title);
1184 strbuf_release(&packname);
1185
1186 return 0;
1187}
1188
1189static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1190 struct string_list *commit_hex)
1191{
1192 uint32_t i;
1193 struct strbuf progress_title = STRBUF_INIT;
1194
1195 if (ctx->report_progress) {
1196 strbuf_addf(&progress_title,
1197 Q_("Finding commits for commit graph from %d ref",
1198 "Finding commits for commit graph from %d refs",
1199 commit_hex->nr),
1200 commit_hex->nr);
1201 ctx->progress = start_delayed_progress(
1202 progress_title.buf,
1203 commit_hex->nr);
1204 }
1205 for (i = 0; i < commit_hex->nr; i++) {
1206 const char *end;
1207 struct object_id oid;
1208 struct commit *result;
1209
1210 display_progress(ctx->progress, i + 1);
1211 if (commit_hex->items[i].string &&
1212 parse_oid_hex(commit_hex->items[i].string, &oid, &end))
1213 continue;
1214
1215 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1216
1217 if (result) {
1218 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1219 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1220 ctx->oids.nr++;
1221 }
1222 }
1223 stop_progress(&ctx->progress);
1224 strbuf_release(&progress_title);
1225}
1226
1227static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1228{
1229 if (ctx->report_progress)
1230 ctx->progress = start_delayed_progress(
1231 _("Finding commits for commit graph among packed objects"),
1232 ctx->approx_nr_objects);
1233 for_each_packed_object(add_packed_commits, ctx,
1234 FOR_EACH_OBJECT_PACK_ORDER);
1235 if (ctx->progress_done < ctx->approx_nr_objects)
1236 display_progress(ctx->progress, ctx->approx_nr_objects);
1237 stop_progress(&ctx->progress);
1238}
1239
1240static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1241{
1242 uint32_t i, count_distinct = 1;
1243
1244 if (ctx->report_progress)
1245 ctx->progress = start_delayed_progress(
1246 _("Counting distinct commits in commit graph"),
1247 ctx->oids.nr);
1248 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1249 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1250
1251 for (i = 1; i < ctx->oids.nr; i++) {
1252 display_progress(ctx->progress, i + 1);
1253 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1254 if (ctx->split) {
1255 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1256
1257 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1258 continue;
1259 }
1260
1261 count_distinct++;
1262 }
1263 }
1264 stop_progress(&ctx->progress);
1265
1266 return count_distinct;
1267}
1268
1269static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1270{
1271 uint32_t i;
1272 struct commit_list *parent;
1273
1274 ctx->num_extra_edges = 0;
1275 if (ctx->report_progress)
1276 ctx->progress = start_delayed_progress(
1277 _("Finding extra edges in commit graph"),
1278 ctx->oids.nr);
1279 for (i = 0; i < ctx->oids.nr; i++) {
1280 int num_parents = 0;
1281 display_progress(ctx->progress, i + 1);
1282 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1283 continue;
1284
1285 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1286 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1287
1288 if (ctx->split &&
1289 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1290 continue;
1291
1292 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1293
1294 for (parent = ctx->commits.list[ctx->commits.nr]->parents;
1295 parent; parent = parent->next)
1296 num_parents++;
1297
1298 if (num_parents > 2)
1299 ctx->num_extra_edges += num_parents - 1;
1300
1301 ctx->commits.nr++;
1302 }
1303 stop_progress(&ctx->progress);
1304}
1305
1306static int write_graph_chunk_base_1(struct hashfile *f,
1307 struct commit_graph *g)
1308{
1309 int num = 0;
1310
1311 if (!g)
1312 return 0;
1313
1314 num = write_graph_chunk_base_1(f, g->base_graph);
1315 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1316 return num + 1;
1317}
1318
1319static int write_graph_chunk_base(struct hashfile *f,
1320 struct write_commit_graph_context *ctx)
1321{
1322 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1323
1324 if (num != ctx->num_commit_graphs_after - 1) {
1325 error(_("failed to write correct number of base graph ids"));
1326 return -1;
1327 }
1328
1329 return 0;
1330}
1331
1332static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1333{
1334 uint32_t i;
1335 int fd;
1336 struct hashfile *f;
1337 struct lock_file lk = LOCK_INIT;
1338 uint32_t chunk_ids[6];
1339 uint64_t chunk_offsets[6];
1340 const unsigned hashsz = the_hash_algo->rawsz;
1341 struct strbuf progress_title = STRBUF_INIT;
1342 int num_chunks = 3;
1343 struct object_id file_hash;
1344
1345 if (ctx->split) {
1346 struct strbuf tmp_file = STRBUF_INIT;
1347
1348 strbuf_addf(&tmp_file,
1349 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1350 ctx->obj_dir);
1351 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1352 } else {
1353 ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1354 }
1355
1356 if (safe_create_leading_directories(ctx->graph_name)) {
1357 UNLEAK(ctx->graph_name);
1358 error(_("unable to create leading directories of %s"),
1359 ctx->graph_name);
1360 return -1;
1361 }
1362
1363 if (ctx->split) {
1364 char *lock_name = get_chain_filename(ctx->obj_dir);
1365
1366 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1367
1368 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1369 if (fd < 0) {
1370 error(_("unable to create '%s'"), ctx->graph_name);
1371 return -1;
1372 }
1373
1374 f = hashfd(fd, ctx->graph_name);
1375 } else {
1376 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1377 fd = lk.tempfile->fd;
1378 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1379 }
1380
1381 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1382 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1383 chunk_ids[2] = GRAPH_CHUNKID_DATA;
1384 if (ctx->num_extra_edges) {
1385 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1386 num_chunks++;
1387 }
1388 if (ctx->num_commit_graphs_after > 1) {
1389 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1390 num_chunks++;
1391 }
1392
1393 chunk_ids[num_chunks] = 0;
1394
1395 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1396 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1397 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1398 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1399
1400 num_chunks = 3;
1401 if (ctx->num_extra_edges) {
1402 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1403 4 * ctx->num_extra_edges;
1404 num_chunks++;
1405 }
1406 if (ctx->num_commit_graphs_after > 1) {
1407 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1408 hashsz * (ctx->num_commit_graphs_after - 1);
1409 num_chunks++;
1410 }
1411
1412 hashwrite_be32(f, GRAPH_SIGNATURE);
1413
1414 hashwrite_u8(f, GRAPH_VERSION);
1415 hashwrite_u8(f, oid_version());
1416 hashwrite_u8(f, num_chunks);
1417 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1418
1419 for (i = 0; i <= num_chunks; i++) {
1420 uint32_t chunk_write[3];
1421
1422 chunk_write[0] = htonl(chunk_ids[i]);
1423 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1424 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1425 hashwrite(f, chunk_write, 12);
1426 }
1427
1428 if (ctx->report_progress) {
1429 strbuf_addf(&progress_title,
1430 Q_("Writing out commit graph in %d pass",
1431 "Writing out commit graph in %d passes",
1432 num_chunks),
1433 num_chunks);
1434 ctx->progress = start_delayed_progress(
1435 progress_title.buf,
1436 num_chunks * ctx->commits.nr);
1437 }
1438 write_graph_chunk_fanout(f, ctx);
1439 write_graph_chunk_oids(f, hashsz, ctx);
1440 write_graph_chunk_data(f, hashsz, ctx);
1441 if (ctx->num_extra_edges)
1442 write_graph_chunk_extra_edges(f, ctx);
1443 if (ctx->num_commit_graphs_after > 1 &&
1444 write_graph_chunk_base(f, ctx)) {
1445 return -1;
1446 }
1447 stop_progress(&ctx->progress);
1448 strbuf_release(&progress_title);
1449
1450 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1451 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
1452 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
1453
1454 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1455 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1456 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1457 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1458 }
1459
1460 close_commit_graph(ctx->r->objects);
1461 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1462
1463 if (ctx->split) {
1464 FILE *chainf = fdopen_lock_file(&lk, "w");
1465 char *final_graph_name;
1466 int result;
1467
1468 close(fd);
1469
1470 if (!chainf) {
1471 error(_("unable to open commit-graph chain file"));
1472 return -1;
1473 }
1474
1475 if (ctx->base_graph_name) {
1476 const char *dest = ctx->commit_graph_filenames_after[
1477 ctx->num_commit_graphs_after - 2];
1478
1479 if (strcmp(ctx->base_graph_name, dest)) {
1480 result = rename(ctx->base_graph_name, dest);
1481
1482 if (result) {
1483 error(_("failed to rename base commit-graph file"));
1484 return -1;
1485 }
1486 }
1487 } else {
1488 char *graph_name = get_commit_graph_filename(ctx->obj_dir);
1489 unlink(graph_name);
1490 }
1491
1492 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1493 final_graph_name = get_split_graph_filename(ctx->obj_dir,
1494 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1495 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1496
1497 result = rename(ctx->graph_name, final_graph_name);
1498
1499 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1500 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1501
1502 if (result) {
1503 error(_("failed to rename temporary commit-graph file"));
1504 return -1;
1505 }
1506 }
1507
1508 commit_lock_file(&lk);
1509
1510 return 0;
1511}
1512
1513static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1514{
1515 struct commit_graph *g = ctx->r->objects->commit_graph;
1516 uint32_t num_commits = ctx->commits.nr;
1517 uint32_t i;
1518
1519 int max_commits = 0;
1520 int size_mult = 2;
1521
1522 if (ctx->split_opts) {
1523 max_commits = ctx->split_opts->max_commits;
1524 size_mult = ctx->split_opts->size_multiple;
1525 }
1526
1527 g = ctx->r->objects->commit_graph;
1528 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1529
1530 while (g && (g->num_commits <= size_mult * num_commits ||
1531 (max_commits && num_commits > max_commits))) {
1532 if (strcmp(g->obj_dir, ctx->obj_dir))
1533 break;
1534
1535 num_commits += g->num_commits;
1536 g = g->base_graph;
1537
1538 ctx->num_commit_graphs_after--;
1539 }
1540
1541 ctx->new_base_graph = g;
1542
1543 if (ctx->num_commit_graphs_after == 2) {
1544 char *old_graph_name = get_commit_graph_filename(g->obj_dir);
1545
1546 if (!strcmp(g->filename, old_graph_name) &&
1547 strcmp(g->obj_dir, ctx->obj_dir)) {
1548 ctx->num_commit_graphs_after = 1;
1549 ctx->new_base_graph = NULL;
1550 }
1551
1552 free(old_graph_name);
1553 }
1554
1555 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1556 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1557
1558 for (i = 0; i < ctx->num_commit_graphs_after &&
1559 i < ctx->num_commit_graphs_before; i++)
1560 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1561
1562 i = ctx->num_commit_graphs_before - 1;
1563 g = ctx->r->objects->commit_graph;
1564
1565 while (g) {
1566 if (i < ctx->num_commit_graphs_after)
1567 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1568
1569 i--;
1570 g = g->base_graph;
1571 }
1572}
1573
1574static void merge_commit_graph(struct write_commit_graph_context *ctx,
1575 struct commit_graph *g)
1576{
1577 uint32_t i;
1578 uint32_t offset = g->num_commits_in_base;
1579
1580 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1581
1582 for (i = 0; i < g->num_commits; i++) {
1583 struct object_id oid;
1584 struct commit *result;
1585
1586 display_progress(ctx->progress, i + 1);
1587
1588 load_oid_from_graph(g, i + offset, &oid);
1589
1590 /* only add commits if they still exist in the repo */
1591 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1592
1593 if (result) {
1594 ctx->commits.list[ctx->commits.nr] = result;
1595 ctx->commits.nr++;
1596 }
1597 }
1598}
1599
1600static int commit_compare(const void *_a, const void *_b)
1601{
1602 const struct commit *a = *(const struct commit **)_a;
1603 const struct commit *b = *(const struct commit **)_b;
1604 return oidcmp(&a->object.oid, &b->object.oid);
1605}
1606
1607static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1608{
1609 uint32_t i, num_parents;
1610 struct commit_list *parent;
1611
1612 if (ctx->report_progress)
1613 ctx->progress = start_delayed_progress(
1614 _("Scanning merged commits"),
1615 ctx->commits.nr);
1616
1617 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1618
1619 ctx->num_extra_edges = 0;
1620 for (i = 0; i < ctx->commits.nr; i++) {
1621 display_progress(ctx->progress, i);
1622
1623 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1624 &ctx->commits.list[i]->object.oid)) {
1625 die(_("unexpected duplicate commit id %s"),
1626 oid_to_hex(&ctx->commits.list[i]->object.oid));
1627 } else {
1628 num_parents = 0;
1629 for (parent = ctx->commits.list[i]->parents; parent; parent = parent->next)
1630 num_parents++;
1631
1632 if (num_parents > 2)
1633 ctx->num_extra_edges += num_parents - 2;
1634 }
1635 }
1636
1637 stop_progress(&ctx->progress);
1638}
1639
1640static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1641{
1642 struct commit_graph *g = ctx->r->objects->commit_graph;
1643 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1644 struct strbuf progress_title = STRBUF_INIT;
1645
1646 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1647 current_graph_number--;
1648
1649 if (ctx->report_progress) {
1650 strbuf_addstr(&progress_title, _("Merging commit-graph"));
1651 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1652 }
1653
1654 merge_commit_graph(ctx, g);
1655 stop_progress(&ctx->progress);
1656 strbuf_release(&progress_title);
1657
1658 g = g->base_graph;
1659 }
1660
1661 if (g) {
1662 ctx->new_base_graph = g;
1663 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1664 }
1665
1666 if (ctx->new_base_graph)
1667 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1668
1669 sort_and_scan_merged_commits(ctx);
1670}
1671
1672static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1673{
1674 uint32_t i;
1675 time_t now = time(NULL);
1676
1677 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1678 struct stat st;
1679 struct utimbuf updated_time;
1680
1681 stat(ctx->commit_graph_filenames_before[i], &st);
1682
1683 updated_time.actime = st.st_atime;
1684 updated_time.modtime = now;
1685 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1686 }
1687}
1688
1689static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1690{
1691 struct strbuf path = STRBUF_INIT;
1692 DIR *dir;
1693 struct dirent *de;
1694 size_t dirnamelen;
1695 timestamp_t expire_time = time(NULL);
1696
1697 if (ctx->split_opts && ctx->split_opts->expire_time)
1698 expire_time -= ctx->split_opts->expire_time;
1699 if (!ctx->split) {
1700 char *chain_file_name = get_chain_filename(ctx->obj_dir);
1701 unlink(chain_file_name);
1702 free(chain_file_name);
1703 ctx->num_commit_graphs_after = 0;
1704 }
1705
1706 strbuf_addstr(&path, ctx->obj_dir);
1707 strbuf_addstr(&path, "/info/commit-graphs");
1708 dir = opendir(path.buf);
1709
1710 if (!dir)
1711 goto out;
1712
1713 strbuf_addch(&path, '/');
1714 dirnamelen = path.len;
1715 while ((de = readdir(dir)) != NULL) {
1716 struct stat st;
1717 uint32_t i, found = 0;
1718
1719 strbuf_setlen(&path, dirnamelen);
1720 strbuf_addstr(&path, de->d_name);
1721
1722 stat(path.buf, &st);
1723
1724 if (st.st_mtime > expire_time)
1725 continue;
1726 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1727 continue;
1728
1729 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1730 if (!strcmp(ctx->commit_graph_filenames_after[i],
1731 path.buf)) {
1732 found = 1;
1733 break;
1734 }
1735 }
1736
1737 if (!found)
1738 unlink(path.buf);
1739 }
1740
1741out:
1742 strbuf_release(&path);
1743}
1744
1745int write_commit_graph(const char *obj_dir,
1746 struct string_list *pack_indexes,
1747 struct string_list *commit_hex,
1748 unsigned int flags,
1749 const struct split_commit_graph_opts *split_opts)
1750{
1751 struct write_commit_graph_context *ctx;
1752 uint32_t i, count_distinct = 0;
1753 size_t len;
1754 int res = 0;
1755
1756 if (!commit_graph_compatible(the_repository))
1757 return 0;
1758
1759 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1760 ctx->r = the_repository;
1761
1762 /* normalize object dir with no trailing slash */
1763 ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
1764 normalize_path_copy(ctx->obj_dir, obj_dir);
1765 len = strlen(ctx->obj_dir);
1766 if (len && ctx->obj_dir[len - 1] == '/')
1767 ctx->obj_dir[len - 1] = 0;
1768
1769 ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0;
1770 ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0;
1771 ctx->split = flags & COMMIT_GRAPH_SPLIT ? 1 : 0;
1772 ctx->split_opts = split_opts;
1773
1774 if (ctx->split) {
1775 struct commit_graph *g;
1776 prepare_commit_graph(ctx->r);
1777
1778 g = ctx->r->objects->commit_graph;
1779
1780 while (g) {
1781 ctx->num_commit_graphs_before++;
1782 g = g->base_graph;
1783 }
1784
1785 if (ctx->num_commit_graphs_before) {
1786 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1787 i = ctx->num_commit_graphs_before;
1788 g = ctx->r->objects->commit_graph;
1789
1790 while (g) {
1791 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1792 g = g->base_graph;
1793 }
1794 }
1795 }
1796
1797 ctx->approx_nr_objects = approximate_object_count();
1798 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1799
1800 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1801 ctx->oids.alloc = split_opts->max_commits;
1802
1803 if (ctx->append) {
1804 prepare_commit_graph_one(ctx->r, ctx->obj_dir);
1805 if (ctx->r->objects->commit_graph)
1806 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
1807 }
1808
1809 if (ctx->oids.alloc < 1024)
1810 ctx->oids.alloc = 1024;
1811 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
1812
1813 if (ctx->append && ctx->r->objects->commit_graph) {
1814 struct commit_graph *g = ctx->r->objects->commit_graph;
1815 for (i = 0; i < g->num_commits; i++) {
1816 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1817 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
1818 }
1819 }
1820
1821 if (pack_indexes) {
1822 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1823 goto cleanup;
1824 }
1825
1826 if (commit_hex)
1827 fill_oids_from_commit_hex(ctx, commit_hex);
1828
1829 if (!pack_indexes && !commit_hex)
1830 fill_oids_from_all_packs(ctx);
1831
1832 close_reachable(ctx);
1833
1834 count_distinct = count_distinct_commits(ctx);
1835
1836 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1837 error(_("the commit graph format cannot write %d commits"), count_distinct);
1838 res = -1;
1839 goto cleanup;
1840 }
1841
1842 ctx->commits.alloc = count_distinct;
1843 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
1844
1845 copy_oids_to_commits(ctx);
1846
1847 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
1848 error(_("too many commits to write graph"));
1849 res = -1;
1850 goto cleanup;
1851 }
1852
1853 if (!ctx->commits.nr)
1854 goto cleanup;
1855
1856 if (ctx->split) {
1857 split_graph_merge_strategy(ctx);
1858
1859 merge_commit_graphs(ctx);
1860 } else
1861 ctx->num_commit_graphs_after = 1;
1862
1863 compute_generation_numbers(ctx);
1864
1865 res = write_commit_graph_file(ctx);
1866
1867 if (ctx->split)
1868 mark_commit_graphs(ctx);
1869
1870 expire_commit_graphs(ctx);
1871
1872cleanup:
1873 free(ctx->graph_name);
1874 free(ctx->commits.list);
1875 free(ctx->oids.list);
1876 free(ctx->obj_dir);
1877
1878 if (ctx->commit_graph_filenames_after) {
1879 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1880 free(ctx->commit_graph_filenames_after[i]);
1881 free(ctx->commit_graph_hash_after[i]);
1882 }
1883
1884 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1885 free(ctx->commit_graph_filenames_before[i]);
1886
1887 free(ctx->commit_graph_filenames_after);
1888 free(ctx->commit_graph_filenames_before);
1889 free(ctx->commit_graph_hash_after);
1890 }
1891
1892 free(ctx);
1893
1894 return res;
1895}
1896
1897#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1898static int verify_commit_graph_error;
1899
1900static void graph_report(const char *fmt, ...)
1901{
1902 va_list ap;
1903
1904 verify_commit_graph_error = 1;
1905 va_start(ap, fmt);
1906 vfprintf(stderr, fmt, ap);
1907 fprintf(stderr, "\n");
1908 va_end(ap);
1909}
1910
1911#define GENERATION_ZERO_EXISTS 1
1912#define GENERATION_NUMBER_EXISTS 2
1913
1914int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
1915{
1916 uint32_t i, cur_fanout_pos = 0;
1917 struct object_id prev_oid, cur_oid, checksum;
1918 int generation_zero = 0;
1919 struct hashfile *f;
1920 int devnull;
1921 struct progress *progress = NULL;
1922 int local_error = 0;
1923
1924 if (!g) {
1925 graph_report("no commit-graph file loaded");
1926 return 1;
1927 }
1928
1929 verify_commit_graph_error = verify_commit_graph_lite(g);
1930 if (verify_commit_graph_error)
1931 return verify_commit_graph_error;
1932
1933 devnull = open("/dev/null", O_WRONLY);
1934 f = hashfd(devnull, NULL);
1935 hashwrite(f, g->data, g->data_len - g->hash_len);
1936 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
1937 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
1938 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1939 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1940 }
1941
1942 for (i = 0; i < g->num_commits; i++) {
1943 struct commit *graph_commit;
1944
1945 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1946
1947 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
1948 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
1949 oid_to_hex(&prev_oid),
1950 oid_to_hex(&cur_oid));
1951
1952 oidcpy(&prev_oid, &cur_oid);
1953
1954 while (cur_oid.hash[0] > cur_fanout_pos) {
1955 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1956
1957 if (i != fanout_value)
1958 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1959 cur_fanout_pos, fanout_value, i);
1960 cur_fanout_pos++;
1961 }
1962
1963 graph_commit = lookup_commit(r, &cur_oid);
1964 if (!parse_commit_in_graph_one(r, g, graph_commit))
1965 graph_report(_("failed to parse commit %s from commit-graph"),
1966 oid_to_hex(&cur_oid));
1967 }
1968
1969 while (cur_fanout_pos < 256) {
1970 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1971
1972 if (g->num_commits != fanout_value)
1973 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
1974 cur_fanout_pos, fanout_value, i);
1975
1976 cur_fanout_pos++;
1977 }
1978
1979 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
1980 return verify_commit_graph_error;
1981
1982 progress = start_progress(_("Verifying commits in commit graph"),
1983 g->num_commits);
1984 for (i = 0; i < g->num_commits; i++) {
1985 struct commit *graph_commit, *odb_commit;
1986 struct commit_list *graph_parents, *odb_parents;
1987 uint32_t max_generation = 0;
1988
1989 display_progress(progress, i + 1);
1990 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1991
1992 graph_commit = lookup_commit(r, &cur_oid);
1993 odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r));
1994 if (parse_commit_internal(odb_commit, 0, 0)) {
1995 graph_report(_("failed to parse commit %s from object database for commit-graph"),
1996 oid_to_hex(&cur_oid));
1997 continue;
1998 }
1999
2000 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
2001 get_commit_tree_oid(odb_commit)))
2002 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
2003 oid_to_hex(&cur_oid),
2004 oid_to_hex(get_commit_tree_oid(graph_commit)),
2005 oid_to_hex(get_commit_tree_oid(odb_commit)));
2006
2007 graph_parents = graph_commit->parents;
2008 odb_parents = odb_commit->parents;
2009
2010 while (graph_parents) {
2011 if (odb_parents == NULL) {
2012 graph_report(_("commit-graph parent list for commit %s is too long"),
2013 oid_to_hex(&cur_oid));
2014 break;
2015 }
2016
2017 /* parse parent in case it is in a base graph */
2018 parse_commit_in_graph_one(r, g, graph_parents->item);
2019
2020 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
2021 graph_report(_("commit-graph parent for %s is %s != %s"),
2022 oid_to_hex(&cur_oid),
2023 oid_to_hex(&graph_parents->item->object.oid),
2024 oid_to_hex(&odb_parents->item->object.oid));
2025
2026 if (graph_parents->item->generation > max_generation)
2027 max_generation = graph_parents->item->generation;
2028
2029 graph_parents = graph_parents->next;
2030 odb_parents = odb_parents->next;
2031 }
2032
2033 if (odb_parents != NULL)
2034 graph_report(_("commit-graph parent list for commit %s terminates early"),
2035 oid_to_hex(&cur_oid));
2036
2037 if (!graph_commit->generation) {
2038 if (generation_zero == GENERATION_NUMBER_EXISTS)
2039 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
2040 oid_to_hex(&cur_oid));
2041 generation_zero = GENERATION_ZERO_EXISTS;
2042 } else if (generation_zero == GENERATION_ZERO_EXISTS)
2043 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
2044 oid_to_hex(&cur_oid));
2045
2046 if (generation_zero == GENERATION_ZERO_EXISTS)
2047 continue;
2048
2049 /*
2050 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2051 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2052 * extra logic in the following condition.
2053 */
2054 if (max_generation == GENERATION_NUMBER_MAX)
2055 max_generation--;
2056
2057 if (graph_commit->generation != max_generation + 1)
2058 graph_report(_("commit-graph generation for commit %s is %u != %u"),
2059 oid_to_hex(&cur_oid),
2060 graph_commit->generation,
2061 max_generation + 1);
2062
2063 if (graph_commit->date != odb_commit->date)
2064 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
2065 oid_to_hex(&cur_oid),
2066 graph_commit->date,
2067 odb_commit->date);
2068 }
2069 stop_progress(&progress);
2070
2071 local_error = verify_commit_graph_error;
2072
2073 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2074 local_error |= verify_commit_graph(r, g->base_graph, flags);
2075
2076 return local_error;
2077}
2078
2079void free_commit_graph(struct commit_graph *g)
2080{
2081 if (!g)
2082 return;
2083 if (g->graph_fd >= 0) {
2084 munmap((void *)g->data, g->data_len);
2085 g->data = NULL;
2086 close(g->graph_fd);
2087 }
2088 free(g->filename);
2089 free(g);
2090}