blob: 031641014faacfbefb288b88a670c6716566aa2a [file] [log] [blame]
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001#include "git-compat-util.h"
SZEDER Gáborfa796532020-06-05 13:00:28 +00002#include "config.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04003#include "lockfile.h"
4#include "pack.h"
5#include "packfile.h"
6#include "commit.h"
7#include "object.h"
Derrick Stolee59fb8772018-06-27 09:24:45 -04008#include "refs.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04009#include "revision.h"
10#include "sha1-lookup.h"
11#include "commit-graph.h"
Junio C Hamanob10edb22018-05-08 15:59:20 +090012#include "object-store.h"
Derrick Stolee96af91d2018-06-27 09:24:36 -040013#include "alloc.h"
Derrick Stoleed6538242018-08-20 18:24:27 +000014#include "hashmap.h"
15#include "replace-object.h"
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +000016#include "progress.h"
Garima Singhf97b9322020-03-30 00:31:28 +000017#include "bloom.h"
Jeff Kingd21ee7d2020-03-30 00:31:29 +000018#include "commit-slab.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060019#include "shallow.h"
Derrick Stolee0087a872020-07-01 13:27:24 +000020#include "json-writer.h"
21#include "trace2.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040022
Derrick Stoleeb23ea972020-04-16 20:14:03 +000023void git_test_write_commit_graph_or_die(void)
24{
25 int flags = 0;
26 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
27 return;
28
29 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
30 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
31
32 if (write_commit_graph_reachable(the_repository->objects->odb,
33 flags, NULL))
34 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
35}
36
Derrick Stolee08fd81c2018-04-02 16:34:19 -040037#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
38#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
39#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
40#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
SZEDER Gábor5af74172019-01-19 21:21:13 +010041#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
Garima Singh76ffbca2020-04-06 16:59:49 +000042#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
43#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
Derrick Stolee118bd572019-06-18 11:14:26 -070044#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
Garima Singh76ffbca2020-04-06 16:59:49 +000045#define MAX_NUM_CHUNKS 7
Derrick Stolee08fd81c2018-04-02 16:34:19 -040046
brian m. carlsonc1665992018-11-14 04:09:35 +000047#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040048
49#define GRAPH_VERSION_1 0x1
50#define GRAPH_VERSION GRAPH_VERSION_1
51
SZEDER Gábor5af74172019-01-19 21:21:13 +010052#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
Derrick Stolee08fd81c2018-04-02 16:34:19 -040053#define GRAPH_EDGE_LAST_MASK 0x7fffffff
54#define GRAPH_PARENT_NONE 0x70000000
55
56#define GRAPH_LAST_EDGE 0x80000000
57
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040058#define GRAPH_HEADER_SIZE 8
Derrick Stolee08fd81c2018-04-02 16:34:19 -040059#define GRAPH_FANOUT_SIZE (4 * 256)
60#define GRAPH_CHUNKLOOKUP_WIDTH 12
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040061#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
brian m. carlsonc1665992018-11-14 04:09:35 +000062 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040063
Derrick Stoleecb99a342019-10-24 13:40:42 +000064/* Remember to update object flag allocation in object.h */
65#define REACHABLE (1u<<15)
66
Jeff Kingd21ee7d2020-03-30 00:31:29 +000067/* Keep track of the order in which commits are added to our list. */
68define_commit_slab(commit_pos, int);
69static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
70
71static void set_commit_pos(struct repository *r, const struct object_id *oid)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040072{
Jeff Kingd21ee7d2020-03-30 00:31:29 +000073 static int32_t max_pos;
74 struct commit *commit = lookup_commit(r, oid);
75
76 if (!commit)
77 return; /* should never happen, but be lenient */
78
79 *commit_pos_at(&commit_pos, commit) = max_pos++;
80}
81
82static int commit_pos_cmp(const void *va, const void *vb)
83{
84 const struct commit *a = *(const struct commit **)va;
85 const struct commit *b = *(const struct commit **)vb;
86 return commit_pos_at(&commit_pos, a) -
87 commit_pos_at(&commit_pos, b);
88}
89
Abhishek Kumar48448122020-06-17 14:44:09 +053090define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
91static struct commit_graph_data_slab commit_graph_data_slab =
92 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
93
94uint32_t commit_graph_position(const struct commit *c)
95{
96 struct commit_graph_data *data =
97 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
98
99 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
100}
101
102uint32_t commit_graph_generation(const struct commit *c)
103{
104 struct commit_graph_data *data =
105 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
106
107 if (!data)
108 return GENERATION_NUMBER_INFINITY;
109 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
110 return GENERATION_NUMBER_INFINITY;
111
112 return data->generation;
113}
114
115static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
116{
117 unsigned int i, nth_slab;
118 struct commit_graph_data *data =
119 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
120
121 if (data)
122 return data;
123
124 nth_slab = c->index / commit_graph_data_slab.slab_size;
125 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
126
127 /*
128 * commit-slab initializes elements with zero, overwrite this with
129 * COMMIT_NOT_FROM_GRAPH for graph_pos.
130 *
131 * We avoid initializing generation with checking if graph position
132 * is not COMMIT_NOT_FROM_GRAPH.
133 */
134 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
135 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
136 COMMIT_NOT_FROM_GRAPH;
137 }
138
139 return data;
140}
141
Garima Singh3d112752020-03-30 00:31:30 +0000142static int commit_gen_cmp(const void *va, const void *vb)
143{
144 const struct commit *a = *(const struct commit **)va;
145 const struct commit *b = *(const struct commit **)vb;
146
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530147 uint32_t generation_a = commit_graph_generation(a);
148 uint32_t generation_b = commit_graph_generation(b);
Garima Singh3d112752020-03-30 00:31:30 +0000149 /* lower generation commits first */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530150 if (generation_a < generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000151 return -1;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530152 else if (generation_a > generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000153 return 1;
154
155 /* use date as a heuristic when generations are equal */
156 if (a->date < b->date)
157 return -1;
158 else if (a->date > b->date)
159 return 1;
160 return 0;
161}
162
Jeff Kingd21ee7d2020-03-30 00:31:29 +0000163char *get_commit_graph_filename(struct object_directory *obj_dir)
164{
165 return xstrfmt("%s/info/commit-graph", obj_dir->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400166}
167
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800168static char *get_split_graph_filename(struct object_directory *odb,
Derrick Stolee5c84b332019-06-18 11:14:25 -0700169 const char *oid_hex)
170{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800171 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
172 oid_hex);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700173}
174
Derrick Stolee663b2b12020-09-17 18:11:46 +0000175char *get_commit_graph_chain_filename(struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700176{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800177 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400178}
179
brian m. carlsonc1665992018-11-14 04:09:35 +0000180static uint8_t oid_version(void)
181{
Derrick Stolee665d70a2020-08-17 14:04:47 +0000182 switch (hash_algo_by_ptr(the_hash_algo)) {
183 case GIT_HASH_SHA1:
184 return 1;
185 case GIT_HASH_SHA256:
186 return 2;
187 default:
188 die(_("invalid hash version"));
189 }
brian m. carlsonc1665992018-11-14 04:09:35 +0000190}
191
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400192static struct commit_graph *alloc_commit_graph(void)
193{
194 struct commit_graph *g = xcalloc(1, sizeof(*g));
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400195
196 return g;
197}
198
Derrick Stoleed6538242018-08-20 18:24:27 +0000199extern int read_replace_refs;
200
201static int commit_graph_compatible(struct repository *r)
202{
Derrick Stolee5cef2952018-08-20 18:24:32 +0000203 if (!r->gitdir)
204 return 0;
205
Derrick Stoleed6538242018-08-20 18:24:27 +0000206 if (read_replace_refs) {
207 prepare_replace_object(r);
208 if (hashmap_get_size(&r->objects->replace_map->map))
209 return 0;
210 }
211
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000212 prepare_commit_graft(r);
Taylor Blauce163642020-07-08 17:10:53 -0400213 if (r->parsed_objects &&
214 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000215 return 0;
216 if (is_repository_shallow(r))
217 return 0;
218
Derrick Stoleed6538242018-08-20 18:24:27 +0000219 return 1;
220}
221
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100222int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
223{
224 *fd = git_open(graph_file);
225 if (*fd < 0)
226 return 0;
227 if (fstat(*fd, st)) {
228 close(*fd);
229 return 0;
230 }
231 return 1;
232}
233
Taylor Blauab14d062020-09-09 11:22:56 -0400234struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
235 int fd, struct stat *st,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800236 struct object_directory *odb)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400237{
238 void *graph_map;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400239 size_t graph_size;
Josh Steadmonaa658572019-01-15 14:25:50 -0800240 struct commit_graph *ret;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400241
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100242 graph_size = xsize_t(st->st_size);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400243
244 if (graph_size < GRAPH_MIN_SIZE) {
245 close(fd);
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100246 error(_("commit-graph file is too small"));
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100247 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400248 }
249 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
Jeff Kingc8828532020-04-23 15:41:13 -0600250 close(fd);
Taylor Blauab14d062020-09-09 11:22:56 -0400251 ret = parse_commit_graph(r, graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800252
Taylor Blaua7df60c2020-02-03 13:18:04 -0800253 if (ret)
254 ret->odb = odb;
Jeff Kingc8828532020-04-23 15:41:13 -0600255 else
Josh Steadmonaa658572019-01-15 14:25:50 -0800256 munmap(graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800257
258 return ret;
259}
260
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100261static int verify_commit_graph_lite(struct commit_graph *g)
262{
263 /*
264 * Basic validation shared between parse_commit_graph()
265 * which'll be called every time the graph is used, and the
266 * much more expensive verify_commit_graph() used by
267 * "commit-graph verify".
268 *
269 * There should only be very basic checks here to ensure that
270 * we don't e.g. segfault in fill_commit_in_graph(), but
271 * because this is a very hot codepath nothing that e.g. loops
272 * over g->num_commits, or runs a checksum on the commit-graph
273 * itself.
274 */
275 if (!g->chunk_oid_fanout) {
276 error("commit-graph is missing the OID Fanout chunk");
277 return 1;
278 }
279 if (!g->chunk_oid_lookup) {
280 error("commit-graph is missing the OID Lookup chunk");
281 return 1;
282 }
283 if (!g->chunk_commit_data) {
284 error("commit-graph is missing the Commit Data chunk");
285 return 1;
286 }
287
288 return 0;
289}
290
Taylor Blauab14d062020-09-09 11:22:56 -0400291struct commit_graph *parse_commit_graph(struct repository *r,
292 void *graph_map, size_t graph_size)
Josh Steadmonaa658572019-01-15 14:25:50 -0800293{
294 const unsigned char *data, *chunk_lookup;
295 uint32_t i;
296 struct commit_graph *graph;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000297 uint64_t next_chunk_offset;
Josh Steadmonaa658572019-01-15 14:25:50 -0800298 uint32_t graph_signature;
299 unsigned char graph_version, hash_version;
300
301 if (!graph_map)
302 return NULL;
303
304 if (graph_size < GRAPH_MIN_SIZE)
305 return NULL;
306
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400307 data = (const unsigned char *)graph_map;
308
309 graph_signature = get_be32(data);
310 if (graph_signature != GRAPH_SIGNATURE) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100311 error(_("commit-graph signature %X does not match signature %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400312 graph_signature, GRAPH_SIGNATURE);
Josh Steadmonaa658572019-01-15 14:25:50 -0800313 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400314 }
315
316 graph_version = *(unsigned char*)(data + 4);
317 if (graph_version != GRAPH_VERSION) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100318 error(_("commit-graph version %X does not match version %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400319 graph_version, GRAPH_VERSION);
Josh Steadmonaa658572019-01-15 14:25:50 -0800320 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400321 }
322
323 hash_version = *(unsigned char*)(data + 5);
brian m. carlsonc1665992018-11-14 04:09:35 +0000324 if (hash_version != oid_version()) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100325 error(_("commit-graph hash version %X does not match version %X"),
brian m. carlsonc1665992018-11-14 04:09:35 +0000326 hash_version, oid_version());
Josh Steadmonaa658572019-01-15 14:25:50 -0800327 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400328 }
329
Taylor Blaub66d8472020-09-09 11:23:10 -0400330 prepare_repo_settings(r);
331
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400332 graph = alloc_commit_graph();
333
brian m. carlsonc1665992018-11-14 04:09:35 +0000334 graph->hash_len = the_hash_algo->rawsz;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400335 graph->num_chunks = *(unsigned char*)(data + 6);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400336 graph->data = graph_map;
337 graph->data_len = graph_size;
338
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000339 if (graph_size < GRAPH_HEADER_SIZE +
340 (graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH +
341 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
342 error(_("commit-graph file is too small to hold %u chunks"),
343 graph->num_chunks);
344 free(graph);
345 return NULL;
346 }
347
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400348 chunk_lookup = data + 8;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000349 next_chunk_offset = get_be64(chunk_lookup + 4);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400350 for (i = 0; i < graph->num_chunks; i++) {
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800351 uint32_t chunk_id;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000352 uint64_t chunk_offset = next_chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400353 int chunk_repeated = 0;
354
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800355 chunk_id = get_be32(chunk_lookup + 0);
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800356
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400357 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000358 next_chunk_offset = get_be64(chunk_lookup + 4);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400359
brian m. carlsonc1665992018-11-14 04:09:35 +0000360 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100361 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400362 (uint32_t)chunk_offset);
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700363 goto free_and_return;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400364 }
365
366 switch (chunk_id) {
367 case GRAPH_CHUNKID_OIDFANOUT:
368 if (graph->chunk_oid_fanout)
369 chunk_repeated = 1;
370 else
371 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
372 break;
373
374 case GRAPH_CHUNKID_OIDLOOKUP:
375 if (graph->chunk_oid_lookup)
376 chunk_repeated = 1;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000377 else {
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400378 graph->chunk_oid_lookup = data + chunk_offset;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000379 graph->num_commits = (next_chunk_offset - chunk_offset)
380 / graph->hash_len;
381 }
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400382 break;
383
384 case GRAPH_CHUNKID_DATA:
385 if (graph->chunk_commit_data)
386 chunk_repeated = 1;
387 else
388 graph->chunk_commit_data = data + chunk_offset;
389 break;
390
SZEDER Gábor5af74172019-01-19 21:21:13 +0100391 case GRAPH_CHUNKID_EXTRAEDGES:
392 if (graph->chunk_extra_edges)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400393 chunk_repeated = 1;
394 else
SZEDER Gábor5af74172019-01-19 21:21:13 +0100395 graph->chunk_extra_edges = data + chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400396 break;
Derrick Stolee118bd572019-06-18 11:14:26 -0700397
398 case GRAPH_CHUNKID_BASE:
399 if (graph->chunk_base_graphs)
400 chunk_repeated = 1;
401 else
402 graph->chunk_base_graphs = data + chunk_offset;
Garima Singh76ffbca2020-04-06 16:59:49 +0000403 break;
404
405 case GRAPH_CHUNKID_BLOOMINDEXES:
406 if (graph->chunk_bloom_indexes)
407 chunk_repeated = 1;
Taylor Blaub66d8472020-09-09 11:23:10 -0400408 else if (r->settings.commit_graph_read_changed_paths)
Garima Singh76ffbca2020-04-06 16:59:49 +0000409 graph->chunk_bloom_indexes = data + chunk_offset;
410 break;
411
412 case GRAPH_CHUNKID_BLOOMDATA:
413 if (graph->chunk_bloom_data)
414 chunk_repeated = 1;
Taylor Blaub66d8472020-09-09 11:23:10 -0400415 else if (r->settings.commit_graph_read_changed_paths) {
Garima Singh76ffbca2020-04-06 16:59:49 +0000416 uint32_t hash_version;
417 graph->chunk_bloom_data = data + chunk_offset;
418 hash_version = get_be32(data + chunk_offset);
419
420 if (hash_version != 1)
421 break;
422
423 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
424 graph->bloom_filter_settings->hash_version = hash_version;
425 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
426 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
Taylor Blau97ffa4f2020-09-17 09:34:42 -0400427 graph->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
Garima Singh76ffbca2020-04-06 16:59:49 +0000428 }
429 break;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400430 }
431
432 if (chunk_repeated) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100433 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700434 goto free_and_return;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400435 }
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400436 }
437
Garima Singh76ffbca2020-04-06 16:59:49 +0000438 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
439 init_bloom_filters();
440 } else {
441 /* We need both the bloom chunks to exist together. Else ignore the data */
442 graph->chunk_bloom_indexes = NULL;
443 graph->chunk_bloom_data = NULL;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700444 FREE_AND_NULL(graph->bloom_filter_settings);
Garima Singh76ffbca2020-04-06 16:59:49 +0000445 }
446
Derrick Stolee118bd572019-06-18 11:14:26 -0700447 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
448
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700449 if (verify_commit_graph_lite(graph))
450 goto free_and_return;
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100451
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400452 return graph;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700453
454free_and_return:
455 free(graph->bloom_filter_settings);
456 free(graph);
457 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400458}
459
Taylor Blauab14d062020-09-09 11:22:56 -0400460static struct commit_graph *load_commit_graph_one(struct repository *r,
461 const char *graph_file,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800462 struct object_directory *odb)
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100463{
464
465 struct stat st;
466 int fd;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700467 struct commit_graph *g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100468 int open_ok = open_commit_graph(graph_file, &fd, &st);
469
470 if (!open_ok)
471 return NULL;
472
Taylor Blauab14d062020-09-09 11:22:56 -0400473 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700474
475 if (g)
476 g->filename = xstrdup(graph_file);
477
478 return g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100479}
480
Taylor Blau13c24992020-02-03 13:18:00 -0800481static struct commit_graph *load_commit_graph_v1(struct repository *r,
482 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700483{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800484 char *graph_name = get_commit_graph_filename(odb);
Taylor Blauab14d062020-09-09 11:22:56 -0400485 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700486 free(graph_name);
487
488 return g;
489}
490
491static int add_graph_to_chain(struct commit_graph *g,
492 struct commit_graph *chain,
493 struct object_id *oids,
494 int n)
495{
496 struct commit_graph *cur_g = chain;
497
Derrick Stolee118bd572019-06-18 11:14:26 -0700498 if (n && !g->chunk_base_graphs) {
499 warning(_("commit-graph has no base graphs chunk"));
500 return 0;
501 }
502
Derrick Stolee5c84b332019-06-18 11:14:25 -0700503 while (n) {
504 n--;
Derrick Stolee118bd572019-06-18 11:14:26 -0700505
506 if (!cur_g ||
507 !oideq(&oids[n], &cur_g->oid) ||
508 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
509 warning(_("commit-graph chain does not match"));
510 return 0;
511 }
512
Derrick Stolee5c84b332019-06-18 11:14:25 -0700513 cur_g = cur_g->base_graph;
514 }
515
516 g->base_graph = chain;
517
518 if (chain)
519 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
520
521 return 1;
522}
523
Taylor Blau13c24992020-02-03 13:18:00 -0800524static struct commit_graph *load_commit_graph_chain(struct repository *r,
525 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700526{
527 struct commit_graph *graph_chain = NULL;
528 struct strbuf line = STRBUF_INIT;
529 struct stat st;
530 struct object_id *oids;
531 int i = 0, valid = 1, count;
Derrick Stolee663b2b12020-09-17 18:11:46 +0000532 char *chain_name = get_commit_graph_chain_filename(odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700533 FILE *fp;
534 int stat_res;
535
536 fp = fopen(chain_name, "r");
537 stat_res = stat(chain_name, &st);
538 free(chain_name);
539
540 if (!fp ||
541 stat_res ||
542 st.st_size <= the_hash_algo->hexsz)
543 return NULL;
544
545 count = st.st_size / (the_hash_algo->hexsz + 1);
546 oids = xcalloc(count, sizeof(struct object_id));
547
Derrick Stoleec5230352019-06-18 11:14:30 -0700548 prepare_alt_odb(r);
549
550 for (i = 0; i < count; i++) {
551 struct object_directory *odb;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700552
553 if (strbuf_getline_lf(&line, fp) == EOF)
554 break;
555
556 if (get_oid_hex(line.buf, &oids[i])) {
557 warning(_("invalid commit-graph chain: line '%s' not a hash"),
558 line.buf);
559 valid = 0;
560 break;
561 }
562
Derrick Stoleec5230352019-06-18 11:14:30 -0700563 valid = 0;
564 for (odb = r->objects->odb; odb; odb = odb->next) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800565 char *graph_name = get_split_graph_filename(odb, line.buf);
Taylor Blauab14d062020-09-09 11:22:56 -0400566 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700567
Derrick Stoleec5230352019-06-18 11:14:30 -0700568 free(graph_name);
569
570 if (g) {
Derrick Stoleec5230352019-06-18 11:14:30 -0700571 if (add_graph_to_chain(g, graph_chain, oids, i)) {
572 graph_chain = g;
573 valid = 1;
574 }
575
576 break;
577 }
578 }
579
580 if (!valid) {
581 warning(_("unable to find all commit-graph files"));
582 break;
583 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700584 }
585
586 free(oids);
587 fclose(fp);
René Scharfe0aa6bce2019-08-07 13:15:02 +0200588 strbuf_release(&line);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700589
590 return graph_chain;
591}
592
Taylor Blau13c24992020-02-03 13:18:00 -0800593struct commit_graph *read_commit_graph_one(struct repository *r,
594 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700595{
Taylor Blau13c24992020-02-03 13:18:00 -0800596 struct commit_graph *g = load_commit_graph_v1(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700597
598 if (!g)
Taylor Blau13c24992020-02-03 13:18:00 -0800599 g = load_commit_graph_chain(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700600
601 return g;
Jonathan Tan5faf3572018-07-11 15:42:37 -0700602}
603
Taylor Blau13c24992020-02-03 13:18:00 -0800604static void prepare_commit_graph_one(struct repository *r,
605 struct object_directory *odb)
Derrick Stolee177722b2018-04-10 08:56:05 -0400606{
Derrick Stolee177722b2018-04-10 08:56:05 -0400607
608 if (r->objects->commit_graph)
609 return;
610
Taylor Blau13c24992020-02-03 13:18:00 -0800611 r->objects->commit_graph = read_commit_graph_one(r, odb);
Derrick Stolee177722b2018-04-10 08:56:05 -0400612}
Jonathan Tan5faf3572018-07-11 15:42:37 -0700613
614/*
615 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
616 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000617 * On the first invocation, this function attempts to load the commit
Jonathan Tan5faf3572018-07-11 15:42:37 -0700618 * graph if the_repository is configured to have one.
619 */
Jonathan Tandade47c2018-07-11 15:42:42 -0700620static int prepare_commit_graph(struct repository *r)
Derrick Stolee177722b2018-04-10 08:56:05 -0400621{
Jeff King263db402018-11-12 09:48:47 -0500622 struct object_directory *odb;
Derrick Stolee177722b2018-04-10 08:56:05 -0400623
Jeff King6abada12019-09-12 10:44:45 -0400624 /*
625 * This must come before the "already attempted?" check below, because
626 * we want to disable even an already-loaded graph file.
627 */
628 if (r->commit_graph_disabled)
629 return 0;
Ævar Arnfjörð Bjarmason43d35612019-03-25 13:08:33 +0100630
Jonathan Tandade47c2018-07-11 15:42:42 -0700631 if (r->objects->commit_graph_attempted)
632 return !!r->objects->commit_graph;
633 r->objects->commit_graph_attempted = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400634
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700635 prepare_repo_settings(r);
636
Derrick Stolee859fdc02018-08-29 05:49:04 -0700637 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700638 r->settings.core_commit_graph != 1)
Jonathan Tandade47c2018-07-11 15:42:42 -0700639 /*
640 * This repository is not configured to use commit graphs, so
641 * do not load one. (But report commit_graph_attempted anyway
642 * so that commit graph loading is not attempted again for this
643 * repository.)
644 */
Jonathan Tan5faf3572018-07-11 15:42:37 -0700645 return 0;
646
Derrick Stoleed6538242018-08-20 18:24:27 +0000647 if (!commit_graph_compatible(r))
648 return 0;
649
Jonathan Tandade47c2018-07-11 15:42:42 -0700650 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500651 for (odb = r->objects->odb;
Jeff King263db402018-11-12 09:48:47 -0500652 !r->objects->commit_graph && odb;
653 odb = odb->next)
Taylor Blau13c24992020-02-03 13:18:00 -0800654 prepare_commit_graph_one(r, odb);
Jonathan Tandade47c2018-07-11 15:42:42 -0700655 return !!r->objects->commit_graph;
Derrick Stolee177722b2018-04-10 08:56:05 -0400656}
657
Derrick Stolee6cc01742018-07-20 16:33:30 +0000658int generation_numbers_enabled(struct repository *r)
659{
660 uint32_t first_generation;
661 struct commit_graph *g;
662 if (!prepare_commit_graph(r))
663 return 0;
664
665 g = r->objects->commit_graph;
666
667 if (!g->num_commits)
668 return 0;
669
670 first_generation = get_be32(g->chunk_commit_data +
671 g->hash_len + 8) >> 2;
672
673 return !!first_generation;
674}
675
Taylor Blau4f364402020-09-09 11:22:44 -0400676struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
677{
678 struct commit_graph *g = r->objects->commit_graph;
679 while (g) {
680 if (g->bloom_filter_settings)
681 return g->bloom_filter_settings;
682 g = g->base_graph;
683 }
684 return NULL;
685}
686
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700687static void close_commit_graph_one(struct commit_graph *g)
688{
689 if (!g)
690 return;
691
692 close_commit_graph_one(g->base_graph);
693 free_commit_graph(g);
694}
695
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700696void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400697{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700698 close_commit_graph_one(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700699 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400700}
701
702static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
703{
704 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
705 g->chunk_oid_lookup, g->hash_len, pos);
706}
707
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700708static void load_oid_from_graph(struct commit_graph *g,
709 uint32_t pos,
710 struct object_id *oid)
711{
712 uint32_t lex_index;
713
714 while (g && pos < g->num_commits_in_base)
715 g = g->base_graph;
716
717 if (!g)
718 BUG("NULL commit-graph");
719
720 if (pos >= g->num_commits + g->num_commits_in_base)
721 die(_("invalid commit position. commit-graph is likely corrupt"));
722
723 lex_index = pos - g->num_commits_in_base;
724
725 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
726}
727
Stefan Beller4f542b72018-12-14 16:09:39 -0800728static struct commit_list **insert_parent_or_die(struct repository *r,
729 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700730 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400731 struct commit_list **pptr)
732{
733 struct commit *c;
734 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400735
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700736 if (pos >= g->num_commits + g->num_commits_in_base)
737 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400738
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700739 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800740 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400741 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200742 die(_("could not find commit %s"), oid_to_hex(&oid));
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530743 commit_graph_data_at(c)->graph_pos = pos;
Derrick Stolee177722b2018-04-10 08:56:05 -0400744 return &commit_list_insert(c, pptr)->next;
745}
746
Derrick Stoleee2838d82018-05-01 12:47:13 +0000747static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
748{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700749 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530750 struct commit_graph_data *graph_data;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700751 uint32_t lex_index;
752
753 while (pos < g->num_commits_in_base)
754 g = g->base_graph;
755
756 lex_index = pos - g->num_commits_in_base;
757 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530758
759 graph_data = commit_graph_data_at(item);
760 graph_data->graph_pos = pos;
761 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000762}
763
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700764static inline void set_commit_tree(struct commit *c, struct tree *t)
765{
766 c->maybe_tree = t;
767}
768
Stefan Beller4f542b72018-12-14 16:09:39 -0800769static int fill_commit_in_graph(struct repository *r,
770 struct commit *item,
771 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400772{
Derrick Stolee177722b2018-04-10 08:56:05 -0400773 uint32_t edge_value;
774 uint32_t *parent_data_ptr;
775 uint64_t date_low, date_high;
776 struct commit_list **pptr;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530777 struct commit_graph_data *graph_data;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700778 const unsigned char *commit_data;
779 uint32_t lex_index;
780
781 while (pos < g->num_commits_in_base)
782 g = g->base_graph;
783
784 if (pos >= g->num_commits + g->num_commits_in_base)
785 die(_("invalid commit position. commit-graph is likely corrupt"));
786
787 /*
788 * Store the "full" position, but then use the
789 * "local" position for the rest of the calculation.
790 */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530791 graph_data = commit_graph_data_at(item);
792 graph_data->graph_pos = pos;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700793 lex_index = pos - g->num_commits_in_base;
794
795 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
Derrick Stolee177722b2018-04-10 08:56:05 -0400796
797 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400798
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700799 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400800
801 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
802 date_low = get_be32(commit_data + g->hash_len + 12);
803 item->date = (timestamp_t)((date_high << 32) | date_low);
804
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530805 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stolee83073cc2018-04-25 14:37:55 +0000806
Derrick Stolee177722b2018-04-10 08:56:05 -0400807 pptr = &item->parents;
808
809 edge_value = get_be32(commit_data + g->hash_len);
810 if (edge_value == GRAPH_PARENT_NONE)
811 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800812 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400813
814 edge_value = get_be32(commit_data + g->hash_len + 4);
815 if (edge_value == GRAPH_PARENT_NONE)
816 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100817 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800818 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400819 return 1;
820 }
821
SZEDER Gábor5af74172019-01-19 21:21:13 +0100822 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
Derrick Stolee177722b2018-04-10 08:56:05 -0400823 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
824 do {
825 edge_value = get_be32(parent_data_ptr);
Stefan Beller4f542b72018-12-14 16:09:39 -0800826 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400827 edge_value & GRAPH_EDGE_LAST_MASK,
828 pptr);
829 parent_data_ptr++;
830 } while (!(edge_value & GRAPH_LAST_EDGE));
831
832 return 1;
833}
834
Derrick Stoleee2838d82018-05-01 12:47:13 +0000835static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
836{
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530837 uint32_t graph_pos = commit_graph_position(item);
838 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
839 *pos = graph_pos;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000840 return 1;
841 } else {
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700842 struct commit_graph *cur_g = g;
843 uint32_t lex_index;
844
845 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
846 cur_g = cur_g->base_graph;
847
848 if (cur_g) {
849 *pos = lex_index + cur_g->num_commits_in_base;
850 return 1;
851 }
852
853 return 0;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000854 }
855}
856
Stefan Beller4f542b72018-12-14 16:09:39 -0800857static int parse_commit_in_graph_one(struct repository *r,
858 struct commit_graph *g,
859 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -0400860{
Derrick Stoleee2838d82018-05-01 12:47:13 +0000861 uint32_t pos;
862
Derrick Stolee177722b2018-04-10 08:56:05 -0400863 if (item->object.parsed)
864 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -0400865
866 if (find_commit_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -0800867 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -0400868
869 return 0;
870}
871
Jonathan Tandade47c2018-07-11 15:42:42 -0700872int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -0400873{
Derrick Stolee7b671f82020-06-23 17:47:01 +0000874 static int checked_env = 0;
875
876 if (!checked_env &&
877 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
878 die("dying as requested by the '%s' variable on commit-graph parse!",
879 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
880 checked_env = 1;
881
Jonathan Tandade47c2018-07-11 15:42:42 -0700882 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -0400883 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -0800884 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -0400885}
886
Jonathan Tandade47c2018-07-11 15:42:42 -0700887void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000888{
889 uint32_t pos;
Jonathan Tandade47c2018-07-11 15:42:42 -0700890 if (!prepare_commit_graph(r))
Derrick Stoleee2838d82018-05-01 12:47:13 +0000891 return;
Jonathan Tandade47c2018-07-11 15:42:42 -0700892 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
893 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000894}
895
Stefan Beller4f542b72018-12-14 16:09:39 -0800896static struct tree *load_tree_for_commit(struct repository *r,
897 struct commit_graph *g,
898 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000899{
900 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700901 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530902 uint32_t graph_pos = commit_graph_position(c);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700903
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530904 while (graph_pos < g->num_commits_in_base)
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700905 g = g->base_graph;
906
907 commit_data = g->chunk_commit_data +
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530908 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000909
910 hashcpy(oid.hash, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700911 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000912
913 return c->maybe_tree;
914}
915
Stefan Beller4f542b72018-12-14 16:09:39 -0800916static struct tree *get_commit_tree_in_graph_one(struct repository *r,
917 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400918 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000919{
920 if (c->maybe_tree)
921 return c->maybe_tree;
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530922 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400923 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000924
Stefan Beller4f542b72018-12-14 16:09:39 -0800925 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400926}
927
Jonathan Tandade47c2018-07-11 15:42:42 -0700928struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400929{
Stefan Beller4f542b72018-12-14 16:09:39 -0800930 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000931}
932
Derrick Stoleec9905be2019-06-12 06:29:40 -0700933struct packed_commit_list {
934 struct commit **list;
Jeff King33613902020-12-07 14:11:08 -0500935 size_t nr;
936 size_t alloc;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700937};
938
Derrick Stoleec9905be2019-06-12 06:29:40 -0700939struct write_commit_graph_context {
940 struct repository *r;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800941 struct object_directory *odb;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700942 char *graph_name;
Jeff Kinga5f1c442020-12-07 14:11:05 -0500943 struct oid_array oids;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700944 struct packed_commit_list commits;
945 int num_extra_edges;
946 unsigned long approx_nr_objects;
947 struct progress *progress;
948 int progress_done;
949 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700950
951 char *base_graph_name;
952 int num_commit_graphs_before;
953 int num_commit_graphs_after;
954 char **commit_graph_filenames_before;
955 char **commit_graph_filenames_after;
956 char **commit_graph_hash_after;
957 uint32_t new_num_commits_in_base;
958 struct commit_graph *new_base_graph;
959
Derrick Stoleec9905be2019-06-12 06:29:40 -0700960 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -0700961 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200962 split:1,
Garima Singh3d112752020-03-30 00:31:30 +0000963 changed_paths:1,
964 order_by_pack:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700965
Taylor Blau98bb7962020-09-17 22:59:49 -0400966 const struct commit_graph_opts *opts;
Garima Singhf97b9322020-03-30 00:31:28 +0000967 size_t total_bloom_filter_data_size;
Derrick Stolee98037f22020-06-23 17:47:00 +0000968 const struct bloom_filter_settings *bloom_settings;
Taylor Blau312cff52020-09-16 14:07:32 -0400969
970 int count_bloom_filter_computed;
971 int count_bloom_filter_not_computed;
Taylor Blau59f0d502020-09-17 22:59:44 -0400972 int count_bloom_filter_trunc_empty;
Taylor Blau312cff52020-09-16 14:07:32 -0400973 int count_bloom_filter_trunc_large;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700974};
975
SZEDER Gábor9bab0812020-07-01 13:27:25 +0000976static int write_graph_chunk_fanout(struct hashfile *f,
977 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400978{
979 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700980 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400981
982 /*
983 * Write the first-level table (the list is sorted,
984 * but we use a 256-entry lookup to be able to avoid
985 * having to do eight extra binary search iterations).
986 */
987 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -0700988 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400989 if ((*list)->object.oid.hash[0] != i)
990 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700991 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400992 count++;
993 list++;
994 }
995
996 hashwrite_be32(f, count);
997 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +0000998
999 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001000}
1001
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001002static int write_graph_chunk_oids(struct hashfile *f,
1003 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001004{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001005 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001006 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001007 for (count = 0; count < ctx->commits.nr; count++, list++) {
1008 display_progress(ctx->progress, ++ctx->progress_cnt);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001009 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001010 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001011
1012 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001013}
1014
1015static const unsigned char *commit_to_sha1(size_t index, void *table)
1016{
1017 struct commit **commits = table;
1018 return commits[index]->object.oid.hash;
1019}
1020
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001021static int write_graph_chunk_data(struct hashfile *f,
1022 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001023{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001024 struct commit **list = ctx->commits.list;
1025 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001026 uint32_t num_extra_edges = 0;
1027
1028 while (list < last) {
1029 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -04001030 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001031 int edge_value;
1032 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -07001033 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001034
Taylor Blau16749b82019-09-05 18:04:55 -04001035 if (parse_commit_no_graph(*list))
1036 die(_("unable to parse commit %s"),
1037 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -04001038 tree = get_commit_tree_oid(*list);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001039 hashwrite(f, tree->hash, the_hash_algo->rawsz);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001040
1041 parent = (*list)->parents;
1042
1043 if (!parent)
1044 edge_value = GRAPH_PARENT_NONE;
1045 else {
1046 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -07001047 ctx->commits.list,
1048 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001049 commit_to_sha1);
1050
Derrick Stolee6c622f92019-06-18 11:14:27 -07001051 if (edge_value >= 0)
1052 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001053 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001054 uint32_t pos;
1055 if (find_commit_in_graph(parent->item,
1056 ctx->new_base_graph,
1057 &pos))
1058 edge_value = pos;
1059 }
1060
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001061 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001062 BUG("missing parent %s for commit %s",
1063 oid_to_hex(&parent->item->object.oid),
1064 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001065 }
1066
1067 hashwrite_be32(f, edge_value);
1068
1069 if (parent)
1070 parent = parent->next;
1071
1072 if (!parent)
1073 edge_value = GRAPH_PARENT_NONE;
1074 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +01001075 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001076 else {
1077 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -07001078 ctx->commits.list,
1079 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001080 commit_to_sha1);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001081
1082 if (edge_value >= 0)
1083 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001084 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001085 uint32_t pos;
1086 if (find_commit_in_graph(parent->item,
1087 ctx->new_base_graph,
1088 &pos))
1089 edge_value = pos;
1090 }
1091
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001092 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001093 BUG("missing parent %s for commit %s",
1094 oid_to_hex(&parent->item->object.oid),
1095 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001096 }
1097
1098 hashwrite_be32(f, edge_value);
1099
SZEDER Gábor5af74172019-01-19 21:21:13 +01001100 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001101 do {
1102 num_extra_edges++;
1103 parent = parent->next;
1104 } while (parent);
1105 }
1106
1107 if (sizeof((*list)->date) > 4)
1108 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1109 else
1110 packedDate[0] = 0;
1111
Abhishek Kumar48448122020-06-17 14:44:09 +05301112 packedDate[0] |= htonl(commit_graph_data_at(*list)->generation << 2);
Derrick Stolee3258c662018-05-01 12:47:09 +00001113
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001114 packedDate[1] = htonl((*list)->date);
1115 hashwrite(f, packedDate, 8);
1116
1117 list++;
1118 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001119
1120 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001121}
1122
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001123static int write_graph_chunk_extra_edges(struct hashfile *f,
1124 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001125{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001126 struct commit **list = ctx->commits.list;
1127 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001128 struct commit_list *parent;
1129
1130 while (list < last) {
1131 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001132
Derrick Stoleec9905be2019-06-12 06:29:40 -07001133 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001134
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001135 for (parent = (*list)->parents; num_parents < 3 && parent;
1136 parent = parent->next)
1137 num_parents++;
1138
1139 if (num_parents <= 2) {
1140 list++;
1141 continue;
1142 }
1143
1144 /* Since num_parents > 2, this initializer is safe. */
1145 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1146 int edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -07001147 ctx->commits.list,
1148 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001149 commit_to_sha1);
1150
Derrick Stolee6c622f92019-06-18 11:14:27 -07001151 if (edge_value >= 0)
1152 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001153 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001154 uint32_t pos;
1155 if (find_commit_in_graph(parent->item,
1156 ctx->new_base_graph,
1157 &pos))
1158 edge_value = pos;
1159 }
1160
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001161 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001162 BUG("missing parent %s for commit %s",
1163 oid_to_hex(&parent->item->object.oid),
1164 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001165 else if (!parent->next)
1166 edge_value |= GRAPH_LAST_EDGE;
1167
1168 hashwrite_be32(f, edge_value);
1169 }
1170
1171 list++;
1172 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001173
1174 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001175}
1176
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001177static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1178 struct write_commit_graph_context *ctx)
Garima Singh76ffbca2020-04-06 16:59:49 +00001179{
1180 struct commit **list = ctx->commits.list;
1181 struct commit **last = ctx->commits.list + ctx->commits.nr;
1182 uint32_t cur_pos = 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001183
1184 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001185 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001186 size_t len = filter ? filter->len : 0;
1187 cur_pos += len;
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001188 display_progress(ctx->progress, ++ctx->progress_cnt);
Garima Singh76ffbca2020-04-06 16:59:49 +00001189 hashwrite_be32(f, cur_pos);
1190 list++;
1191 }
1192
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001193 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001194}
1195
Derrick Stolee0087a872020-07-01 13:27:24 +00001196static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1197{
1198 struct json_writer jw = JSON_WRITER_INIT;
1199
1200 jw_object_begin(&jw, 0);
1201 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1202 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1203 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
Taylor Blau97ffa4f2020-09-17 09:34:42 -04001204 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
Derrick Stolee0087a872020-07-01 13:27:24 +00001205 jw_end(&jw);
1206
1207 trace2_data_json("bloom", ctx->r, "settings", &jw);
1208
1209 jw_release(&jw);
1210}
1211
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001212static int write_graph_chunk_bloom_data(struct hashfile *f,
1213 struct write_commit_graph_context *ctx)
Garima Singh76ffbca2020-04-06 16:59:49 +00001214{
1215 struct commit **list = ctx->commits.list;
1216 struct commit **last = ctx->commits.list + ctx->commits.nr;
Garima Singh76ffbca2020-04-06 16:59:49 +00001217
Derrick Stolee0087a872020-07-01 13:27:24 +00001218 trace2_bloom_filter_settings(ctx);
1219
Derrick Stolee98037f22020-06-23 17:47:00 +00001220 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1221 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1222 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
Garima Singh76ffbca2020-04-06 16:59:49 +00001223
1224 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001225 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001226 size_t len = filter ? filter->len : 0;
Derrick Stolee94919742020-07-01 13:27:23 +00001227
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001228 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee94919742020-07-01 13:27:23 +00001229 if (len)
1230 hashwrite(f, filter->data, len * sizeof(unsigned char));
Garima Singh76ffbca2020-04-06 16:59:49 +00001231 list++;
1232 }
1233
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001234 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001235}
1236
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001237static int add_packed_commits(const struct object_id *oid,
1238 struct packed_git *pack,
1239 uint32_t pos,
1240 void *data)
1241{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001242 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001243 enum object_type type;
1244 off_t offset = nth_packed_object_offset(pack, pos);
1245 struct object_info oi = OBJECT_INFO_INIT;
1246
Derrick Stoleec9905be2019-06-12 06:29:40 -07001247 if (ctx->progress)
1248 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001249
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001250 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001251 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001252 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001253
1254 if (type != OBJ_COMMIT)
1255 return 0;
1256
Jeff Kinga5f1c442020-12-07 14:11:05 -05001257 oid_array_append(&ctx->oids, oid);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001258 set_commit_pos(ctx->r, oid);
1259
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001260 return 0;
1261}
1262
Derrick Stoleec9905be2019-06-12 06:29:40 -07001263static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001264{
1265 struct commit_list *parent;
1266 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001267 if (!(parent->item->object.flags & REACHABLE)) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001268 oid_array_append(&ctx->oids, &parent->item->object.oid);
Derrick Stoleecb99a342019-10-24 13:40:42 +00001269 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001270 }
1271 }
1272}
1273
Derrick Stoleec9905be2019-06-12 06:29:40 -07001274static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001275{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001276 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001277 struct commit *commit;
Taylor Blau98bb7962020-09-17 22:59:49 -04001278 enum commit_graph_split_flags flags = ctx->opts ?
1279 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001280
Derrick Stoleec9905be2019-06-12 06:29:40 -07001281 if (ctx->report_progress)
1282 ctx->progress = start_delayed_progress(
1283 _("Loading known commits in commit graph"),
1284 ctx->oids.nr);
1285 for (i = 0; i < ctx->oids.nr; i++) {
1286 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001287 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001288 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001289 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001290 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001291 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001292
1293 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001294 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001295 * than the number of missing commits in the reachable
1296 * closure.
1297 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001298 if (ctx->report_progress)
1299 ctx->progress = start_delayed_progress(
1300 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001301 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001302 for (i = 0; i < ctx->oids.nr; i++) {
1303 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001304 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001305
Derrick Stolee6c622f92019-06-18 11:14:27 -07001306 if (!commit)
1307 continue;
1308 if (ctx->split) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06001309 if ((!parse_commit(commit) &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301310 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
Taylor Blau8a6ac282020-04-13 22:04:17 -06001311 flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001312 add_missing_parents(ctx, commit);
1313 } else if (!parse_commit_no_graph(commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001314 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001315 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001316 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001317
Derrick Stoleec9905be2019-06-12 06:29:40 -07001318 if (ctx->report_progress)
1319 ctx->progress = start_delayed_progress(
1320 _("Clearing commit marks in commit graph"),
1321 ctx->oids.nr);
1322 for (i = 0; i < ctx->oids.nr; i++) {
1323 display_progress(ctx->progress, i + 1);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001324 commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001325
1326 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001327 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001328 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001329 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001330}
1331
Derrick Stoleec9905be2019-06-12 06:29:40 -07001332static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001333{
1334 int i;
1335 struct commit_list *list = NULL;
1336
Derrick Stoleec9905be2019-06-12 06:29:40 -07001337 if (ctx->report_progress)
Derrick Stoleeecc08692019-11-25 21:28:23 +00001338 ctx->progress = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001339 _("Computing commit graph generation numbers"),
1340 ctx->commits.nr);
1341 for (i = 0; i < ctx->commits.nr; i++) {
Abhishek Kumar48448122020-06-17 14:44:09 +05301342 uint32_t generation = commit_graph_data_at(ctx->commits.list[i])->generation;
1343
Derrick Stoleec9905be2019-06-12 06:29:40 -07001344 display_progress(ctx->progress, i + 1);
Abhishek Kumar48448122020-06-17 14:44:09 +05301345 if (generation != GENERATION_NUMBER_INFINITY &&
1346 generation != GENERATION_NUMBER_ZERO)
Derrick Stolee3258c662018-05-01 12:47:09 +00001347 continue;
1348
Derrick Stoleec9905be2019-06-12 06:29:40 -07001349 commit_list_insert(ctx->commits.list[i], &list);
Derrick Stolee3258c662018-05-01 12:47:09 +00001350 while (list) {
1351 struct commit *current = list->item;
1352 struct commit_list *parent;
1353 int all_parents_computed = 1;
1354 uint32_t max_generation = 0;
1355
1356 for (parent = current->parents; parent; parent = parent->next) {
Abhishek Kumar48448122020-06-17 14:44:09 +05301357 generation = commit_graph_data_at(parent->item)->generation;
1358
1359 if (generation == GENERATION_NUMBER_INFINITY ||
1360 generation == GENERATION_NUMBER_ZERO) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001361 all_parents_computed = 0;
1362 commit_list_insert(parent->item, &list);
1363 break;
Abhishek Kumar48448122020-06-17 14:44:09 +05301364 } else if (generation > max_generation) {
1365 max_generation = generation;
Derrick Stolee3258c662018-05-01 12:47:09 +00001366 }
1367 }
1368
1369 if (all_parents_computed) {
Abhishek Kumar48448122020-06-17 14:44:09 +05301370 struct commit_graph_data *data = commit_graph_data_at(current);
1371
1372 data->generation = max_generation + 1;
Derrick Stolee3258c662018-05-01 12:47:09 +00001373 pop_commit(&list);
1374
Abhishek Kumar48448122020-06-17 14:44:09 +05301375 if (data->generation > GENERATION_NUMBER_MAX)
1376 data->generation = GENERATION_NUMBER_MAX;
Derrick Stolee3258c662018-05-01 12:47:09 +00001377 }
1378 }
1379 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001380 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001381}
1382
Taylor Blau312cff52020-09-16 14:07:32 -04001383static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1384{
1385 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1386 ctx->count_bloom_filter_computed);
1387 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1388 ctx->count_bloom_filter_not_computed);
Taylor Blau59f0d502020-09-17 22:59:44 -04001389 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1390 ctx->count_bloom_filter_trunc_empty);
Taylor Blau312cff52020-09-16 14:07:32 -04001391 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1392 ctx->count_bloom_filter_trunc_large);
1393}
1394
Garima Singhf97b9322020-03-30 00:31:28 +00001395static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1396{
1397 int i;
1398 struct progress *progress = NULL;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001399 struct commit **sorted_commits;
Taylor Blau809e0322020-09-18 09:27:27 -04001400 int max_new_filters;
Garima Singhf97b9322020-03-30 00:31:28 +00001401
1402 init_bloom_filters();
1403
1404 if (ctx->report_progress)
1405 progress = start_delayed_progress(
1406 _("Computing commit changed paths Bloom filters"),
1407 ctx->commits.nr);
1408
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001409 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1410 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
Garima Singh3d112752020-03-30 00:31:30 +00001411
1412 if (ctx->order_by_pack)
1413 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1414 else
1415 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001416
Taylor Blau809e0322020-09-18 09:27:27 -04001417 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1418 ctx->opts->max_new_filters : ctx->commits.nr;
1419
Garima Singhf97b9322020-03-30 00:31:28 +00001420 for (i = 0; i < ctx->commits.nr; i++) {
Taylor Blau312cff52020-09-16 14:07:32 -04001421 enum bloom_filter_computed computed = 0;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001422 struct commit *c = sorted_commits[i];
Taylor Blau312cff52020-09-16 14:07:32 -04001423 struct bloom_filter *filter = get_or_compute_bloom_filter(
1424 ctx->r,
1425 c,
Taylor Blau809e0322020-09-18 09:27:27 -04001426 ctx->count_bloom_filter_computed < max_new_filters,
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04001427 ctx->bloom_settings,
Taylor Blau312cff52020-09-16 14:07:32 -04001428 &computed);
1429 if (computed & BLOOM_COMPUTED) {
1430 ctx->count_bloom_filter_computed++;
Taylor Blau59f0d502020-09-17 22:59:44 -04001431 if (computed & BLOOM_TRUNC_EMPTY)
1432 ctx->count_bloom_filter_trunc_empty++;
Taylor Blau312cff52020-09-16 14:07:32 -04001433 if (computed & BLOOM_TRUNC_LARGE)
1434 ctx->count_bloom_filter_trunc_large++;
1435 } else if (computed & BLOOM_NOT_COMPUTED)
1436 ctx->count_bloom_filter_not_computed++;
Taylor Blau809e0322020-09-18 09:27:27 -04001437 ctx->total_bloom_filter_data_size += filter
1438 ? sizeof(unsigned char) * filter->len : 0;
Garima Singhf97b9322020-03-30 00:31:28 +00001439 display_progress(progress, i + 1);
1440 }
1441
Taylor Blau312cff52020-09-16 14:07:32 -04001442 if (trace2_is_enabled())
1443 trace2_bloom_filter_write_statistics(ctx);
1444
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001445 free(sorted_commits);
Garima Singhf97b9322020-03-30 00:31:28 +00001446 stop_progress(&progress);
1447}
1448
Taylor Blau1fe10842020-05-04 19:13:35 -06001449struct refs_cb_data {
1450 struct oidset *commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001451 struct progress *progress;
Taylor Blau1fe10842020-05-04 19:13:35 -06001452};
1453
Taylor Blau6830c362020-04-13 22:04:25 -06001454static int add_ref_to_set(const char *refname,
1455 const struct object_id *oid,
1456 int flags, void *cb_data)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001457{
Taylor Blau630cd512020-05-13 15:59:37 -06001458 struct object_id peeled;
Taylor Blau1fe10842020-05-04 19:13:35 -06001459 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001460
Taylor Blau630cd512020-05-13 15:59:37 -06001461 if (!peel_ref(refname, &peeled))
1462 oid = &peeled;
1463 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1464 oidset_insert(data->commits, oid);
Taylor Blaud335ce82020-05-13 15:59:33 -06001465
1466 display_progress(data->progress, oidset_size(data->commits));
1467
Derrick Stolee59fb8772018-06-27 09:24:45 -04001468 return 0;
1469}
1470
Taylor Blau0bd52e22020-02-03 21:51:50 -08001471int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001472 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04001473 const struct commit_graph_opts *opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001474{
Taylor Blau6830c362020-04-13 22:04:25 -06001475 struct oidset commits = OIDSET_INIT;
Taylor Blau1fe10842020-05-04 19:13:35 -06001476 struct refs_cb_data data;
Derrick Stoleee103f722019-06-12 06:29:37 -07001477 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001478
Taylor Blau1fe10842020-05-04 19:13:35 -06001479 memset(&data, 0, sizeof(data));
1480 data.commits = &commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001481 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1482 data.progress = start_delayed_progress(
1483 _("Collecting referenced commits"), 0);
Taylor Blau1fe10842020-05-04 19:13:35 -06001484
1485 for_each_ref(add_ref_to_set, &data);
SZEDER Gábor6f9d5f22020-07-09 18:54:32 +02001486
1487 stop_progress(&data.progress);
1488
Taylor Blau6830c362020-04-13 22:04:25 -06001489 result = write_commit_graph(odb, NULL, &commits,
Taylor Blau98bb7962020-09-17 22:59:49 -04001490 flags, opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001491
Taylor Blau6830c362020-04-13 22:04:25 -06001492 oidset_clear(&commits);
Derrick Stoleee103f722019-06-12 06:29:37 -07001493 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001494}
1495
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001496static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1497 struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001498{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001499 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001500 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001501 struct strbuf packname = STRBUF_INIT;
1502 int dirlen;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001503
Taylor Blau0bd52e22020-02-03 21:51:50 -08001504 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001505 dirlen = packname.len;
1506 if (ctx->report_progress) {
1507 strbuf_addf(&progress_title,
1508 Q_("Finding commits for commit graph in %d pack",
1509 "Finding commits for commit graph in %d packs",
1510 pack_indexes->nr),
1511 pack_indexes->nr);
1512 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1513 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001514 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001515 for (i = 0; i < pack_indexes->nr; i++) {
1516 struct packed_git *p;
1517 strbuf_setlen(&packname, dirlen);
1518 strbuf_addstr(&packname, pack_indexes->items[i].string);
1519 p = add_packed_git(packname.buf, packname.len, 1);
1520 if (!p) {
1521 error(_("error adding pack %s"), packname.buf);
1522 return -1;
Derrick Stolee7547b952018-04-10 08:56:08 -04001523 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001524 if (open_pack_index(p)) {
1525 error(_("error opening index for %s"), packname.buf);
1526 return -1;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001527 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001528 for_each_object_in_pack(p, add_packed_commits, ctx,
1529 FOR_EACH_OBJECT_PACK_ORDER);
1530 close_pack(p);
1531 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001532 }
1533
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001534 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001535 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001536 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001537
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001538 return 0;
1539}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001540
Taylor Blau6830c362020-04-13 22:04:25 -06001541static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1542 struct oidset *commits)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001543{
Taylor Blau6830c362020-04-13 22:04:25 -06001544 struct oidset_iter iter;
1545 struct object_id *oid;
1546
1547 if (!oidset_size(commits))
1548 return 0;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001549
Taylor Blau6830c362020-04-13 22:04:25 -06001550 oidset_iter_init(commits, &iter);
1551 while ((oid = oidset_iter_next(&iter))) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05001552 oid_array_append(&ctx->oids, oid);
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001553 }
Taylor Blau6830c362020-04-13 22:04:25 -06001554
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001555 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001556}
1557
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001558static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1559{
1560 if (ctx->report_progress)
1561 ctx->progress = start_delayed_progress(
1562 _("Finding commits for commit graph among packed objects"),
1563 ctx->approx_nr_objects);
1564 for_each_packed_object(add_packed_commits, ctx,
1565 FOR_EACH_OBJECT_PACK_ORDER);
1566 if (ctx->progress_done < ctx->approx_nr_objects)
1567 display_progress(ctx->progress, ctx->approx_nr_objects);
1568 stop_progress(&ctx->progress);
1569}
1570
Derrick Stoleef998d542019-06-12 06:29:44 -07001571static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1572{
1573 uint32_t i;
Taylor Blau98bb7962020-09-17 22:59:49 -04001574 enum commit_graph_split_flags flags = ctx->opts ?
1575 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stoleef998d542019-06-12 06:29:44 -07001576
1577 ctx->num_extra_edges = 0;
1578 if (ctx->report_progress)
1579 ctx->progress = start_delayed_progress(
1580 _("Finding extra edges in commit graph"),
1581 ctx->oids.nr);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001582 oid_array_sort(&ctx->oids);
1583 for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
René Scharfe689a1462019-09-15 19:07:44 +02001584 unsigned int num_parents;
1585
Derrick Stoleef998d542019-06-12 06:29:44 -07001586 display_progress(ctx->progress, i + 1);
Derrick Stoleef998d542019-06-12 06:29:44 -07001587
Derrick Stolee6c622f92019-06-18 11:14:27 -07001588 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Jeff Kinga5f1c442020-12-07 14:11:05 -05001589 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001590
Taylor Blau8a6ac282020-04-13 22:04:17 -06001591 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301592 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001593 continue;
1594
Taylor Blau8a6ac282020-04-13 22:04:17 -06001595 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1596 parse_commit(ctx->commits.list[ctx->commits.nr]);
1597 else
1598 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
Derrick Stoleef998d542019-06-12 06:29:44 -07001599
René Scharfe689a1462019-09-15 19:07:44 +02001600 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001601 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001602 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001603
Derrick Stoleef998d542019-06-12 06:29:44 -07001604 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001605 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001606 stop_progress(&ctx->progress);
1607}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001608
Derrick Stolee6c622f92019-06-18 11:14:27 -07001609static int write_graph_chunk_base_1(struct hashfile *f,
1610 struct commit_graph *g)
1611{
1612 int num = 0;
1613
1614 if (!g)
1615 return 0;
1616
1617 num = write_graph_chunk_base_1(f, g->base_graph);
1618 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1619 return num + 1;
1620}
1621
1622static int write_graph_chunk_base(struct hashfile *f,
1623 struct write_commit_graph_context *ctx)
1624{
1625 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1626
1627 if (num != ctx->num_commit_graphs_after - 1) {
1628 error(_("failed to write correct number of base graph ids"));
1629 return -1;
1630 }
1631
1632 return 0;
1633}
1634
SZEDER Gábor17e62752020-07-01 13:27:26 +00001635typedef int (*chunk_write_fn)(struct hashfile *f,
1636 struct write_commit_graph_context *ctx);
1637
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001638struct chunk_info {
1639 uint32_t id;
1640 uint64_t size;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001641 chunk_write_fn write_fn;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001642};
1643
Derrick Stolee238def52019-06-12 06:29:45 -07001644static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1645{
1646 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001647 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07001648 struct hashfile *f;
1649 struct lock_file lk = LOCK_INIT;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001650 struct chunk_info chunks[MAX_NUM_CHUNKS + 1];
Derrick Stolee238def52019-06-12 06:29:45 -07001651 const unsigned hashsz = the_hash_algo->rawsz;
1652 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee144354b2019-06-18 11:14:27 -07001653 int num_chunks = 3;
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001654 uint64_t chunk_offset;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001655 struct object_id file_hash;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001656
Derrick Stolee6c622f92019-06-18 11:14:27 -07001657 if (ctx->split) {
1658 struct strbuf tmp_file = STRBUF_INIT;
1659
1660 strbuf_addf(&tmp_file,
1661 "%s/info/commit-graphs/tmp_graph_XXXXXX",
Taylor Blau0bd52e22020-02-03 21:51:50 -08001662 ctx->odb->path);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001663 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1664 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001665 ctx->graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001666 }
1667
Derrick Stolee238def52019-06-12 06:29:45 -07001668 if (safe_create_leading_directories(ctx->graph_name)) {
1669 UNLEAK(ctx->graph_name);
1670 error(_("unable to create leading directories of %s"),
1671 ctx->graph_name);
1672 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001673 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001674
Derrick Stolee6c622f92019-06-18 11:14:27 -07001675 if (ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00001676 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001677
Taylor Blau45a43652020-04-29 11:36:46 -06001678 hold_lock_file_for_update_mode(&lk, lock_name,
1679 LOCK_DIE_ON_ERROR, 0444);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001680
1681 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1682 if (fd < 0) {
Taylor Blaua2d57e22020-04-23 15:41:02 -06001683 error(_("unable to create temporary graph layer"));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001684 return -1;
1685 }
1686
Taylor Blauf4d62842020-04-29 11:36:42 -06001687 if (adjust_shared_perm(ctx->graph_name)) {
1688 error(_("unable to adjust shared permissions for '%s'"),
1689 ctx->graph_name);
1690 return -1;
1691 }
1692
Derrick Stolee6c622f92019-06-18 11:14:27 -07001693 f = hashfd(fd, ctx->graph_name);
1694 } else {
Taylor Blau1f9beca2020-04-29 11:36:38 -06001695 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1696 LOCK_DIE_ON_ERROR, 0444);
Martin Ågrena52cdce2021-01-05 20:23:47 +01001697 fd = get_lock_file_fd(&lk);
1698 f = hashfd(fd, get_lock_file_path(&lk));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001699 }
Derrick Stolee238def52019-06-12 06:29:45 -07001700
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001701 chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
1702 chunks[0].size = GRAPH_FANOUT_SIZE;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001703 chunks[0].write_fn = write_graph_chunk_fanout;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001704 chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
1705 chunks[1].size = hashsz * ctx->commits.nr;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001706 chunks[1].write_fn = write_graph_chunk_oids;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001707 chunks[2].id = GRAPH_CHUNKID_DATA;
1708 chunks[2].size = (hashsz + 16) * ctx->commits.nr;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001709 chunks[2].write_fn = write_graph_chunk_data;
Derrick Stolee144354b2019-06-18 11:14:27 -07001710 if (ctx->num_extra_edges) {
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001711 chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
1712 chunks[num_chunks].size = 4 * ctx->num_extra_edges;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001713 chunks[num_chunks].write_fn = write_graph_chunk_extra_edges;
Derrick Stolee144354b2019-06-18 11:14:27 -07001714 num_chunks++;
1715 }
Garima Singh76ffbca2020-04-06 16:59:49 +00001716 if (ctx->changed_paths) {
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001717 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
1718 chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001719 chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes;
Garima Singh76ffbca2020-04-06 16:59:49 +00001720 num_chunks++;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001721 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
1722 chunks[num_chunks].size = sizeof(uint32_t) * 3
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001723 + ctx->total_bloom_filter_data_size;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001724 chunks[num_chunks].write_fn = write_graph_chunk_bloom_data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001725 num_chunks++;
1726 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001727 if (ctx->num_commit_graphs_after > 1) {
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001728 chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
1729 chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
SZEDER Gábor17e62752020-07-01 13:27:26 +00001730 chunks[num_chunks].write_fn = write_graph_chunk_base;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001731 num_chunks++;
1732 }
Derrick Stolee144354b2019-06-18 11:14:27 -07001733
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001734 chunks[num_chunks].id = 0;
1735 chunks[num_chunks].size = 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001736
1737 hashwrite_be32(f, GRAPH_SIGNATURE);
1738
1739 hashwrite_u8(f, GRAPH_VERSION);
brian m. carlsonc1665992018-11-14 04:09:35 +00001740 hashwrite_u8(f, oid_version());
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001741 hashwrite_u8(f, num_chunks);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001742 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001743
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001744 chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001745 for (i = 0; i <= num_chunks; i++) {
1746 uint32_t chunk_write[3];
1747
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001748 chunk_write[0] = htonl(chunks[i].id);
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001749 chunk_write[1] = htonl(chunk_offset >> 32);
1750 chunk_write[2] = htonl(chunk_offset & 0xffffffff);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001751 hashwrite(f, chunk_write, 12);
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001752
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001753 chunk_offset += chunks[i].size;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001754 }
1755
Derrick Stolee238def52019-06-12 06:29:45 -07001756 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001757 strbuf_addf(&progress_title,
1758 Q_("Writing out commit graph in %d pass",
1759 "Writing out commit graph in %d passes",
1760 num_chunks),
1761 num_chunks);
Derrick Stolee238def52019-06-12 06:29:45 -07001762 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001763 progress_title.buf,
Derrick Stolee238def52019-06-12 06:29:45 -07001764 num_chunks * ctx->commits.nr);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001765 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00001766
1767 for (i = 0; i < num_chunks; i++) {
SZEDER Gábor2dd4fed2020-07-01 13:27:27 +00001768 uint64_t start_offset = f->total + f->offset;
1769
SZEDER Gábor17e62752020-07-01 13:27:26 +00001770 if (chunks[i].write_fn(f, ctx))
1771 return -1;
SZEDER Gábor2dd4fed2020-07-01 13:27:27 +00001772
1773 if (f->total + f->offset != start_offset + chunks[i].size)
1774 BUG("expected to write %"PRId64" bytes to chunk %"PRIx32", but wrote %"PRId64" instead",
1775 chunks[i].size, chunks[i].id,
1776 f->total + f->offset - start_offset);
Garima Singh76ffbca2020-04-06 16:59:49 +00001777 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00001778
Derrick Stolee238def52019-06-12 06:29:45 -07001779 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001780 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001781
Derrick Stolee6c622f92019-06-18 11:14:27 -07001782 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1783 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001784 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001785
1786 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1787 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1788 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1789 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1790 }
1791
Derrick Stoleec3a3a962019-05-17 11:41:47 -07001792 close_commit_graph(ctx->r->objects);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001793 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1794
1795 if (ctx->split) {
1796 FILE *chainf = fdopen_lock_file(&lk, "w");
1797 char *final_graph_name;
1798 int result;
1799
1800 close(fd);
1801
1802 if (!chainf) {
1803 error(_("unable to open commit-graph chain file"));
1804 return -1;
1805 }
1806
1807 if (ctx->base_graph_name) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06001808 const char *dest;
1809 int idx = ctx->num_commit_graphs_after - 1;
1810 if (ctx->num_commit_graphs_after > 1)
1811 idx--;
1812
1813 dest = ctx->commit_graph_filenames_after[idx];
Derrick Stolee6c622f92019-06-18 11:14:27 -07001814
Derrick Stolee135a7122019-06-18 11:14:28 -07001815 if (strcmp(ctx->base_graph_name, dest)) {
1816 result = rename(ctx->base_graph_name, dest);
1817
1818 if (result) {
1819 error(_("failed to rename base commit-graph file"));
1820 return -1;
1821 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001822 }
1823 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001824 char *graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001825 unlink(graph_name);
1826 }
1827
1828 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001829 final_graph_name = get_split_graph_filename(ctx->odb,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001830 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1831 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1832
1833 result = rename(ctx->graph_name, final_graph_name);
1834
1835 for (i = 0; i < ctx->num_commit_graphs_after; i++)
Martin Ågrena52cdce2021-01-05 20:23:47 +01001836 fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001837
1838 if (result) {
1839 error(_("failed to rename temporary commit-graph file"));
1840 return -1;
1841 }
1842 }
1843
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001844 commit_lock_file(&lk);
1845
Derrick Stolee238def52019-06-12 06:29:45 -07001846 return 0;
1847}
1848
Derrick Stolee1771be92019-06-18 11:14:29 -07001849static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1850{
Alex Henrie8da02ce2019-09-30 20:29:34 -06001851 struct commit_graph *g;
1852 uint32_t num_commits;
Taylor Blaufdbde822020-04-13 22:04:12 -06001853 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee1771be92019-06-18 11:14:29 -07001854 uint32_t i;
1855
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001856 int max_commits = 0;
1857 int size_mult = 2;
1858
Taylor Blau98bb7962020-09-17 22:59:49 -04001859 if (ctx->opts) {
1860 max_commits = ctx->opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00001861
Taylor Blau98bb7962020-09-17 22:59:49 -04001862 if (ctx->opts->size_multiple)
1863 size_mult = ctx->opts->size_multiple;
Taylor Blaufdbde822020-04-13 22:04:12 -06001864
Taylor Blau98bb7962020-09-17 22:59:49 -04001865 flags = ctx->opts->split_flags;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001866 }
1867
Derrick Stolee1771be92019-06-18 11:14:29 -07001868 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06001869 num_commits = ctx->commits.nr;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001870 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1871 ctx->num_commit_graphs_after = 1;
1872 else
1873 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07001874
Taylor Blau8a6ac282020-04-13 22:04:17 -06001875 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1876 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
Taylor Blaufdbde822020-04-13 22:04:12 -06001877 while (g && (g->num_commits <= size_mult * num_commits ||
1878 (max_commits && num_commits > max_commits))) {
1879 if (g->odb != ctx->odb)
1880 break;
Derrick Stoleec5230352019-06-18 11:14:30 -07001881
Taylor Blaufdbde822020-04-13 22:04:12 -06001882 num_commits += g->num_commits;
1883 g = g->base_graph;
Derrick Stolee1771be92019-06-18 11:14:29 -07001884
Taylor Blaufdbde822020-04-13 22:04:12 -06001885 ctx->num_commit_graphs_after--;
1886 }
Derrick Stolee1771be92019-06-18 11:14:29 -07001887 }
1888
Taylor Blau8a6ac282020-04-13 22:04:17 -06001889 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
1890 ctx->new_base_graph = g;
1891 else if (ctx->num_commit_graphs_after != 1)
1892 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1893 "should be 1 with --split=replace");
Derrick Stolee1771be92019-06-18 11:14:29 -07001894
Derrick Stoleec5230352019-06-18 11:14:30 -07001895 if (ctx->num_commit_graphs_after == 2) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001896 char *old_graph_name = get_commit_graph_filename(g->odb);
Derrick Stoleec5230352019-06-18 11:14:30 -07001897
1898 if (!strcmp(g->filename, old_graph_name) &&
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001899 g->odb != ctx->odb) {
Derrick Stoleec5230352019-06-18 11:14:30 -07001900 ctx->num_commit_graphs_after = 1;
1901 ctx->new_base_graph = NULL;
1902 }
1903
1904 free(old_graph_name);
1905 }
1906
Taylor Blaub78a5562020-04-23 15:41:09 -06001907 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1908 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
Derrick Stolee1771be92019-06-18 11:14:29 -07001909
1910 for (i = 0; i < ctx->num_commit_graphs_after &&
1911 i < ctx->num_commit_graphs_before; i++)
1912 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1913
1914 i = ctx->num_commit_graphs_before - 1;
1915 g = ctx->r->objects->commit_graph;
1916
1917 while (g) {
1918 if (i < ctx->num_commit_graphs_after)
1919 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1920
1921 i--;
1922 g = g->base_graph;
1923 }
1924}
1925
1926static void merge_commit_graph(struct write_commit_graph_context *ctx,
1927 struct commit_graph *g)
1928{
1929 uint32_t i;
1930 uint32_t offset = g->num_commits_in_base;
1931
1932 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1933
1934 for (i = 0; i < g->num_commits; i++) {
1935 struct object_id oid;
1936 struct commit *result;
1937
1938 display_progress(ctx->progress, i + 1);
1939
1940 load_oid_from_graph(g, i + offset, &oid);
1941
1942 /* only add commits if they still exist in the repo */
1943 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1944
1945 if (result) {
1946 ctx->commits.list[ctx->commits.nr] = result;
1947 ctx->commits.nr++;
1948 }
1949 }
1950}
1951
1952static int commit_compare(const void *_a, const void *_b)
1953{
1954 const struct commit *a = *(const struct commit **)_a;
1955 const struct commit *b = *(const struct commit **)_b;
1956 return oidcmp(&a->object.oid, &b->object.oid);
1957}
1958
1959static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1960{
Derrick Stolee150f1152020-10-09 20:53:51 +00001961 uint32_t i, dedup_i = 0;
Derrick Stolee1771be92019-06-18 11:14:29 -07001962
1963 if (ctx->report_progress)
1964 ctx->progress = start_delayed_progress(
1965 _("Scanning merged commits"),
1966 ctx->commits.nr);
1967
1968 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1969
1970 ctx->num_extra_edges = 0;
1971 for (i = 0; i < ctx->commits.nr; i++) {
1972 display_progress(ctx->progress, i);
1973
1974 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1975 &ctx->commits.list[i]->object.oid)) {
Derrick Stolee150f1152020-10-09 20:53:51 +00001976 /*
1977 * Silently ignore duplicates. These were likely
1978 * created due to a commit appearing in multiple
1979 * layers of the chain, which is unexpected but
1980 * not invalid. We should make sure there is a
1981 * unique copy in the new layer.
1982 */
Derrick Stolee1771be92019-06-18 11:14:29 -07001983 } else {
René Scharfe689a1462019-09-15 19:07:44 +02001984 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07001985
Derrick Stolee150f1152020-10-09 20:53:51 +00001986 ctx->commits.list[dedup_i] = ctx->commits.list[i];
1987 dedup_i++;
1988
René Scharfe689a1462019-09-15 19:07:44 +02001989 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07001990 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07001991 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07001992 }
1993 }
1994
Derrick Stolee150f1152020-10-09 20:53:51 +00001995 ctx->commits.nr = dedup_i;
1996
Derrick Stolee1771be92019-06-18 11:14:29 -07001997 stop_progress(&ctx->progress);
1998}
1999
2000static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2001{
2002 struct commit_graph *g = ctx->r->objects->commit_graph;
2003 uint32_t current_graph_number = ctx->num_commit_graphs_before;
Derrick Stolee1771be92019-06-18 11:14:29 -07002004
2005 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2006 current_graph_number--;
2007
René Scharfed68ce902020-02-20 19:49:18 +01002008 if (ctx->report_progress)
2009 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
Derrick Stolee1771be92019-06-18 11:14:29 -07002010
2011 merge_commit_graph(ctx, g);
2012 stop_progress(&ctx->progress);
Derrick Stolee1771be92019-06-18 11:14:29 -07002013
2014 g = g->base_graph;
2015 }
2016
2017 if (g) {
2018 ctx->new_base_graph = g;
2019 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2020 }
2021
2022 if (ctx->new_base_graph)
2023 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2024
2025 sort_and_scan_merged_commits(ctx);
2026}
2027
Derrick Stolee8d840972019-06-18 11:14:31 -07002028static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2029{
2030 uint32_t i;
2031 time_t now = time(NULL);
2032
2033 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2034 struct stat st;
2035 struct utimbuf updated_time;
2036
2037 stat(ctx->commit_graph_filenames_before[i], &st);
2038
2039 updated_time.actime = st.st_atime;
2040 updated_time.modtime = now;
2041 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2042 }
2043}
2044
2045static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2046{
2047 struct strbuf path = STRBUF_INIT;
2048 DIR *dir;
2049 struct dirent *de;
2050 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002051 timestamp_t expire_time = time(NULL);
2052
Taylor Blau98bb7962020-09-17 22:59:49 -04002053 if (ctx->opts && ctx->opts->expire_time)
2054 expire_time = ctx->opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07002055 if (!ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00002056 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stoleeba411122019-06-18 11:14:33 -07002057 unlink(chain_file_name);
2058 free(chain_file_name);
2059 ctx->num_commit_graphs_after = 0;
2060 }
Derrick Stolee8d840972019-06-18 11:14:31 -07002061
Taylor Blau0bd52e22020-02-03 21:51:50 -08002062 strbuf_addstr(&path, ctx->odb->path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002063 strbuf_addstr(&path, "/info/commit-graphs");
2064 dir = opendir(path.buf);
2065
René Scharfe0aa6bce2019-08-07 13:15:02 +02002066 if (!dir)
2067 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07002068
2069 strbuf_addch(&path, '/');
2070 dirnamelen = path.len;
2071 while ((de = readdir(dir)) != NULL) {
2072 struct stat st;
2073 uint32_t i, found = 0;
2074
2075 strbuf_setlen(&path, dirnamelen);
2076 strbuf_addstr(&path, de->d_name);
2077
2078 stat(path.buf, &st);
2079
2080 if (st.st_mtime > expire_time)
2081 continue;
2082 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2083 continue;
2084
2085 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2086 if (!strcmp(ctx->commit_graph_filenames_after[i],
2087 path.buf)) {
2088 found = 1;
2089 break;
2090 }
2091 }
2092
2093 if (!found)
2094 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07002095 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02002096
2097out:
2098 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002099}
2100
Taylor Blau0bd52e22020-02-03 21:51:50 -08002101int write_commit_graph(struct object_directory *odb,
Derrick Stoleee103f722019-06-12 06:29:37 -07002102 struct string_list *pack_indexes,
Taylor Blau6830c362020-04-13 22:04:25 -06002103 struct oidset *commits,
SZEDER Gábor39d88312019-08-05 10:02:39 +02002104 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04002105 const struct commit_graph_opts *opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002106{
Derrick Stoleec9905be2019-06-12 06:29:40 -07002107 struct write_commit_graph_context *ctx;
Jeff King1cbdbf32020-12-07 14:11:02 -05002108 uint32_t i;
Derrick Stoleee103f722019-06-12 06:29:37 -07002109 int res = 0;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002110 int replace = 0;
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002111 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002112
Derrick Stolee85102ac2020-10-09 20:53:52 +00002113 prepare_repo_settings(the_repository);
2114 if (!the_repository->settings.core_commit_graph) {
2115 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2116 return 0;
2117 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002118 if (!commit_graph_compatible(the_repository))
Derrick Stoleee103f722019-06-12 06:29:37 -07002119 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002120
Derrick Stoleec9905be2019-06-12 06:29:40 -07002121 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
2122 ctx->r = the_repository;
Taylor Blau0bd52e22020-02-03 21:51:50 -08002123 ctx->odb = odb;
SZEDER Gábor39d88312019-08-05 10:02:39 +02002124 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2125 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2126 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
Taylor Blau98bb7962020-09-17 22:59:49 -04002127 ctx->opts = opts;
Garima Singhf97b9322020-03-30 00:31:28 +00002128 ctx->total_bloom_filter_data_size = 0;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002129
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002130 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2131 bloom_settings.bits_per_entry);
2132 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2133 bloom_settings.num_hashes);
2134 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2135 bloom_settings.max_changed_paths);
2136 ctx->bloom_settings = &bloom_settings;
2137
Derrick Stolee0087a872020-07-01 13:27:24 +00002138 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2139 ctx->changed_paths = 1;
2140 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2141 struct commit_graph *g;
2142 prepare_commit_graph_one(ctx->r, ctx->odb);
2143
2144 g = ctx->r->objects->commit_graph;
2145
2146 /* We have changed-paths already. Keep them in the next graph */
2147 if (g && g->chunk_bloom_data) {
2148 ctx->changed_paths = 1;
2149 ctx->bloom_settings = g->bloom_filter_settings;
2150 }
2151 }
2152
Derrick Stolee6c622f92019-06-18 11:14:27 -07002153 if (ctx->split) {
2154 struct commit_graph *g;
2155 prepare_commit_graph(ctx->r);
2156
2157 g = ctx->r->objects->commit_graph;
2158
2159 while (g) {
2160 ctx->num_commit_graphs_before++;
2161 g = g->base_graph;
2162 }
2163
2164 if (ctx->num_commit_graphs_before) {
2165 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2166 i = ctx->num_commit_graphs_before;
2167 g = ctx->r->objects->commit_graph;
2168
2169 while (g) {
2170 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2171 g = g->base_graph;
2172 }
2173 }
Taylor Blau8a6ac282020-04-13 22:04:17 -06002174
Taylor Blau98bb7962020-09-17 22:59:49 -04002175 if (ctx->opts)
2176 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002177 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002178
Derrick Stoleec9905be2019-06-12 06:29:40 -07002179 ctx->approx_nr_objects = approximate_object_count();
Derrick Stoleec9905be2019-06-12 06:29:40 -07002180
Jeff Kinga5f1c442020-12-07 14:11:05 -05002181 if (ctx->append)
Taylor Blau13c24992020-02-03 13:18:00 -08002182 prepare_commit_graph_one(ctx->r, ctx->odb);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002183
Derrick Stoleec9905be2019-06-12 06:29:40 -07002184 if (ctx->append && ctx->r->objects->commit_graph) {
2185 struct commit_graph *g = ctx->r->objects->commit_graph;
2186 for (i = 0; i < g->num_commits; i++) {
Jeff Kinga5f1c442020-12-07 14:11:05 -05002187 struct object_id oid;
2188 hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2189 oid_array_append(&ctx->oids, &oid);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002190 }
2191 }
2192
2193 if (pack_indexes) {
Garima Singh3d112752020-03-30 00:31:30 +00002194 ctx->order_by_pack = 1;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07002195 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2196 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002197 }
2198
Taylor Blau6830c362020-04-13 22:04:25 -06002199 if (commits) {
2200 if ((res = fill_oids_from_commits(ctx, commits)))
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02002201 goto cleanup;
2202 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002203
Junio C Hamano9b6606f2020-05-01 13:39:53 -07002204 if (!pack_indexes && !commits) {
Garima Singh3d112752020-03-30 00:31:30 +00002205 ctx->order_by_pack = 1;
Derrick Stoleeb2c83062019-06-12 06:29:42 -07002206 fill_oids_from_all_packs(ctx);
Garima Singh3d112752020-03-30 00:31:30 +00002207 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002208
Derrick Stoleec9905be2019-06-12 06:29:40 -07002209 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002210
Derrick Stoleef998d542019-06-12 06:29:44 -07002211 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04002212
Derrick Stoleec9905be2019-06-12 06:29:40 -07002213 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07002214 error(_("too many commits to write graph"));
2215 res = -1;
2216 goto cleanup;
2217 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04002218
Taylor Blau8a6ac282020-04-13 22:04:17 -06002219 if (!ctx->commits.nr && !replace)
Derrick Stolee6c622f92019-06-18 11:14:27 -07002220 goto cleanup;
2221
Derrick Stolee1771be92019-06-18 11:14:29 -07002222 if (ctx->split) {
2223 split_graph_merge_strategy(ctx);
2224
Taylor Blau8a6ac282020-04-13 22:04:17 -06002225 if (!replace)
2226 merge_commit_graphs(ctx);
Derrick Stolee1771be92019-06-18 11:14:29 -07002227 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07002228 ctx->num_commit_graphs_after = 1;
2229
Derrick Stoleec9905be2019-06-12 06:29:40 -07002230 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002231
Garima Singhf97b9322020-03-30 00:31:28 +00002232 if (ctx->changed_paths)
2233 compute_bloom_filters(ctx);
2234
Derrick Stolee238def52019-06-12 06:29:45 -07002235 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002236
Derrick Stoleeba411122019-06-18 11:14:33 -07002237 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07002238 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07002239
2240 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07002241
Derrick Stoleee103f722019-06-12 06:29:37 -07002242cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07002243 free(ctx->graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002244 free(ctx->commits.list);
Jeff Kinga5f1c442020-12-07 14:11:05 -05002245 oid_array_clear(&ctx->oids);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002246
2247 if (ctx->commit_graph_filenames_after) {
2248 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2249 free(ctx->commit_graph_filenames_after[i]);
2250 free(ctx->commit_graph_hash_after[i]);
2251 }
2252
2253 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2254 free(ctx->commit_graph_filenames_before[i]);
2255
2256 free(ctx->commit_graph_filenames_after);
2257 free(ctx->commit_graph_filenames_before);
2258 free(ctx->commit_graph_hash_after);
2259 }
2260
Derrick Stoleec9905be2019-06-12 06:29:40 -07002261 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07002262
2263 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002264}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002265
2266#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2267static int verify_commit_graph_error;
2268
Derrick Stolee283e68c2018-06-27 09:24:32 -04002269static void graph_report(const char *fmt, ...)
2270{
2271 va_list ap;
2272
2273 verify_commit_graph_error = 1;
2274 va_start(ap, fmt);
2275 vfprintf(stderr, fmt, ap);
2276 fprintf(stderr, "\n");
2277 va_end(ap);
2278}
2279
2280#define GENERATION_ZERO_EXISTS 1
2281#define GENERATION_NUMBER_EXISTS 2
2282
Derrick Stolee3da4b602019-06-18 11:14:32 -07002283int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
Derrick Stolee283e68c2018-06-27 09:24:32 -04002284{
Derrick Stolee9bda8462018-06-27 09:24:35 -04002285 uint32_t i, cur_fanout_pos = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04002286 struct object_id prev_oid, cur_oid, checksum;
Derrick Stolee1373e542018-06-27 09:24:39 -04002287 int generation_zero = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04002288 struct hashfile *f;
2289 int devnull;
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002290 struct progress *progress = NULL;
Derrick Stolee3da4b602019-06-18 11:14:32 -07002291 int local_error = 0;
Derrick Stolee9bda8462018-06-27 09:24:35 -04002292
Derrick Stolee283e68c2018-06-27 09:24:32 -04002293 if (!g) {
2294 graph_report("no commit-graph file loaded");
2295 return 1;
2296 }
2297
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +01002298 verify_commit_graph_error = verify_commit_graph_lite(g);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002299 if (verify_commit_graph_error)
2300 return verify_commit_graph_error;
2301
Derrick Stolee41df0e32018-06-27 09:24:42 -04002302 devnull = open("/dev/null", O_WRONLY);
2303 f = hashfd(devnull, NULL);
2304 hashwrite(f, g->data, g->data_len - g->hash_len);
2305 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
Jeff King67947c32018-08-28 17:22:52 -04002306 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04002307 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2308 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2309 }
2310
Derrick Stolee9bda8462018-06-27 09:24:35 -04002311 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002312 struct commit *graph_commit;
2313
Derrick Stolee9bda8462018-06-27 09:24:35 -04002314 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2315
2316 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002317 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002318 oid_to_hex(&prev_oid),
2319 oid_to_hex(&cur_oid));
2320
2321 oidcpy(&prev_oid, &cur_oid);
2322
2323 while (cur_oid.hash[0] > cur_fanout_pos) {
2324 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2325
2326 if (i != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002327 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002328 cur_fanout_pos, fanout_value, i);
2329 cur_fanout_pos++;
2330 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002331
Junio C Hamano82952962018-07-17 15:46:19 -07002332 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08002333 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002334 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002335 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04002336 }
2337
2338 while (cur_fanout_pos < 256) {
2339 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2340
2341 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002342 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002343 cur_fanout_pos, fanout_value, i);
2344
2345 cur_fanout_pos++;
2346 }
2347
Derrick Stolee41df0e32018-06-27 09:24:42 -04002348 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04002349 return verify_commit_graph_error;
2350
Garima Singh73716122019-08-26 09:29:58 -07002351 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2352 progress = start_progress(_("Verifying commits in commit graph"),
2353 g->num_commits);
2354
Derrick Stolee96af91d2018-06-27 09:24:36 -04002355 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002356 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04002357 struct commit_list *graph_parents, *odb_parents;
Derrick Stolee1373e542018-06-27 09:24:39 -04002358 uint32_t max_generation = 0;
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302359 uint32_t generation;
Derrick Stolee96af91d2018-06-27 09:24:36 -04002360
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002361 display_progress(progress, i + 1);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002362 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2363
Junio C Hamano82952962018-07-17 15:46:19 -07002364 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002365 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002366 if (parse_commit_internal(odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002367 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002368 oid_to_hex(&cur_oid));
2369 continue;
2370 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002371
Stefan Beller4f542b72018-12-14 16:09:39 -08002372 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002373 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002374 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002375 oid_to_hex(&cur_oid),
2376 oid_to_hex(get_commit_tree_oid(graph_commit)),
2377 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002378
2379 graph_parents = graph_commit->parents;
2380 odb_parents = odb_commit->parents;
2381
2382 while (graph_parents) {
2383 if (odb_parents == NULL) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002384 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002385 oid_to_hex(&cur_oid));
2386 break;
2387 }
2388
Derrick Stolee3da4b602019-06-18 11:14:32 -07002389 /* parse parent in case it is in a base graph */
2390 parse_commit_in_graph_one(r, g, graph_parents->item);
2391
Jeff King9001dc22018-08-28 17:22:48 -04002392 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002393 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002394 oid_to_hex(&cur_oid),
2395 oid_to_hex(&graph_parents->item->object.oid),
2396 oid_to_hex(&odb_parents->item->object.oid));
2397
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302398 generation = commit_graph_generation(graph_parents->item);
2399 if (generation > max_generation)
2400 max_generation = generation;
Derrick Stolee1373e542018-06-27 09:24:39 -04002401
Derrick Stolee53614b12018-06-27 09:24:38 -04002402 graph_parents = graph_parents->next;
2403 odb_parents = odb_parents->next;
2404 }
2405
2406 if (odb_parents != NULL)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002407 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002408 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002409
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05302410 if (!commit_graph_generation(graph_commit)) {
Derrick Stolee1373e542018-06-27 09:24:39 -04002411 if (generation_zero == GENERATION_NUMBER_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002412 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002413 oid_to_hex(&cur_oid));
2414 generation_zero = GENERATION_ZERO_EXISTS;
2415 } else if (generation_zero == GENERATION_ZERO_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002416 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002417 oid_to_hex(&cur_oid));
2418
2419 if (generation_zero == GENERATION_ZERO_EXISTS)
2420 continue;
2421
2422 /*
2423 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2424 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2425 * extra logic in the following condition.
2426 */
2427 if (max_generation == GENERATION_NUMBER_MAX)
2428 max_generation--;
2429
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302430 generation = commit_graph_generation(graph_commit);
2431 if (generation != max_generation + 1)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002432 graph_report(_("commit-graph generation for commit %s is %u != %u"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002433 oid_to_hex(&cur_oid),
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302434 generation,
Derrick Stolee1373e542018-06-27 09:24:39 -04002435 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002436
2437 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002438 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002439 oid_to_hex(&cur_oid),
2440 graph_commit->date,
2441 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002442 }
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002443 stop_progress(&progress);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002444
Derrick Stolee3da4b602019-06-18 11:14:32 -07002445 local_error = verify_commit_graph_error;
2446
2447 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2448 local_error |= verify_commit_graph(r, g->base_graph, flags);
2449
2450 return local_error;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002451}
Jonathan Tanc3756d52018-07-11 15:42:40 -07002452
2453void free_commit_graph(struct commit_graph *g)
2454{
2455 if (!g)
2456 return;
Jeff Kingc8828532020-04-23 15:41:13 -06002457 if (g->data) {
Jonathan Tanc3756d52018-07-11 15:42:40 -07002458 munmap((void *)g->data, g->data_len);
2459 g->data = NULL;
Jonathan Tanc3756d52018-07-11 15:42:40 -07002460 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002461 free(g->filename);
Garima Singh76ffbca2020-04-06 16:59:49 +00002462 free(g->bloom_filter_settings);
Jonathan Tanc3756d52018-07-11 15:42:40 -07002463 free(g);
2464}
Jeff King6abada12019-09-12 10:44:45 -04002465
2466void disable_commit_graph(struct repository *r)
2467{
2468 r->commit_graph_disabled = 1;
2469}