]>
Commit | Line | Data |
---|---|---|
b6fdc44c | 1 | #include "git-compat-util.h" |
fa796530 | 2 | #include "config.h" |
f394e093 | 3 | #include "gettext.h" |
41771fa4 | 4 | #include "hex.h" |
08fd81c9 DS |
5 | #include "lockfile.h" |
6 | #include "pack.h" | |
7 | #include "packfile.h" | |
8 | #include "commit.h" | |
9 | #include "object.h" | |
59fb8770 | 10 | #include "refs.h" |
08fd81c9 | 11 | #include "revision.h" |
bc626927 | 12 | #include "hash-lookup.h" |
08fd81c9 | 13 | #include "commit-graph.h" |
87bed179 | 14 | #include "object-file.h" |
b10edb2d | 15 | #include "object-store.h" |
6f2d7430 | 16 | #include "oid-array.h" |
96af91d4 | 17 | #include "alloc.h" |
d6538246 DS |
18 | #include "hashmap.h" |
19 | #include "replace-object.h" | |
7b0f2292 | 20 | #include "progress.h" |
f97b9325 | 21 | #include "bloom.h" |
d21ee7d1 | 22 | #include "commit-slab.h" |
120ad2b0 | 23 | #include "shallow.h" |
0087a87b DS |
24 | #include "json-writer.h" |
25 | #include "trace2.h" | |
d4a4f929 | 26 | #include "tree.h" |
47410aa8 | 27 | #include "chunk-format.h" |
d5ebb50d | 28 | #include "wrapper.h" |
08fd81c9 | 29 | |
b23ea979 DS |
30 | void git_test_write_commit_graph_or_die(void) |
31 | { | |
32 | int flags = 0; | |
33 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0)) | |
34 | return; | |
35 | ||
36 | if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0)) | |
37 | flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS; | |
38 | ||
39 | if (write_commit_graph_reachable(the_repository->objects->odb, | |
40 | flags, NULL)) | |
41 | die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH"); | |
42 | } | |
43 | ||
08fd81c9 DS |
44 | #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ |
45 | #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ | |
46 | #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ | |
47 | #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */ | |
6dbf4b81 DS |
48 | #define GRAPH_CHUNKID_GENERATION_DATA 0x47444132 /* "GDA2" */ |
49 | #define GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW 0x47444f32 /* "GDO2" */ | |
5af7417b | 50 | #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */ |
76ffbca7 GS |
51 | #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */ |
52 | #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */ | |
118bd570 | 53 | #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */ |
08fd81c9 | 54 | |
c1665998 | 55 | #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16) |
08fd81c9 DS |
56 | |
57 | #define GRAPH_VERSION_1 0x1 | |
58 | #define GRAPH_VERSION GRAPH_VERSION_1 | |
59 | ||
5af7417b | 60 | #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000 |
08fd81c9 DS |
61 | #define GRAPH_EDGE_LAST_MASK 0x7fffffff |
62 | #define GRAPH_PARENT_NONE 0x70000000 | |
63 | ||
64 | #define GRAPH_LAST_EDGE 0x80000000 | |
65 | ||
0e3b97cc | 66 | #define GRAPH_HEADER_SIZE 8 |
08fd81c9 | 67 | #define GRAPH_FANOUT_SIZE (4 * 256) |
2692c2f6 | 68 | #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * CHUNK_TOC_ENTRY_SIZE \ |
c1665998 | 69 | + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) |
08fd81c9 | 70 | |
e8b63005 AK |
71 | #define CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW (1ULL << 31) |
72 | ||
cb99a34e DS |
73 | /* Remember to update object flag allocation in object.h */ |
74 | #define REACHABLE (1u<<15) | |
75 | ||
72a2bfca AK |
76 | define_commit_slab(topo_level_slab, uint32_t); |
77 | ||
d21ee7d1 JK |
78 | /* Keep track of the order in which commits are added to our list. */ |
79 | define_commit_slab(commit_pos, int); | |
80 | static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos); | |
81 | ||
82 | static void set_commit_pos(struct repository *r, const struct object_id *oid) | |
83 | { | |
84 | static int32_t max_pos; | |
85 | struct commit *commit = lookup_commit(r, oid); | |
86 | ||
87 | if (!commit) | |
88 | return; /* should never happen, but be lenient */ | |
89 | ||
90 | *commit_pos_at(&commit_pos, commit) = max_pos++; | |
91 | } | |
92 | ||
93 | static int commit_pos_cmp(const void *va, const void *vb) | |
08fd81c9 | 94 | { |
d21ee7d1 JK |
95 | const struct commit *a = *(const struct commit **)va; |
96 | const struct commit *b = *(const struct commit **)vb; | |
97 | return commit_pos_at(&commit_pos, a) - | |
98 | commit_pos_at(&commit_pos, b); | |
99 | } | |
100 | ||
4844812b AK |
101 | define_commit_slab(commit_graph_data_slab, struct commit_graph_data); |
102 | static struct commit_graph_data_slab commit_graph_data_slab = | |
103 | COMMIT_SLAB_INIT(1, commit_graph_data_slab); | |
104 | ||
702110aa DS |
105 | static int get_configured_generation_version(struct repository *r) |
106 | { | |
107 | int version = 2; | |
108 | repo_config_get_int(r, "commitgraph.generationversion", &version); | |
109 | return version; | |
110 | } | |
111 | ||
4844812b AK |
112 | uint32_t commit_graph_position(const struct commit *c) |
113 | { | |
114 | struct commit_graph_data *data = | |
115 | commit_graph_data_slab_peek(&commit_graph_data_slab, c); | |
116 | ||
117 | return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH; | |
118 | } | |
119 | ||
d7f92784 | 120 | timestamp_t commit_graph_generation(const struct commit *c) |
4844812b AK |
121 | { |
122 | struct commit_graph_data *data = | |
123 | commit_graph_data_slab_peek(&commit_graph_data_slab, c); | |
124 | ||
2ee11f72 DS |
125 | if (data && data->generation) |
126 | return data->generation; | |
4844812b | 127 | |
2ee11f72 | 128 | return GENERATION_NUMBER_INFINITY; |
4844812b AK |
129 | } |
130 | ||
131 | static struct commit_graph_data *commit_graph_data_at(const struct commit *c) | |
132 | { | |
133 | unsigned int i, nth_slab; | |
134 | struct commit_graph_data *data = | |
135 | commit_graph_data_slab_peek(&commit_graph_data_slab, c); | |
136 | ||
137 | if (data) | |
138 | return data; | |
139 | ||
140 | nth_slab = c->index / commit_graph_data_slab.slab_size; | |
141 | data = commit_graph_data_slab_at(&commit_graph_data_slab, c); | |
142 | ||
143 | /* | |
144 | * commit-slab initializes elements with zero, overwrite this with | |
145 | * COMMIT_NOT_FROM_GRAPH for graph_pos. | |
146 | * | |
147 | * We avoid initializing generation with checking if graph position | |
148 | * is not COMMIT_NOT_FROM_GRAPH. | |
149 | */ | |
150 | for (i = 0; i < commit_graph_data_slab.slab_size; i++) { | |
151 | commit_graph_data_slab.slab[nth_slab][i].graph_pos = | |
152 | COMMIT_NOT_FROM_GRAPH; | |
153 | } | |
154 | ||
155 | return data; | |
156 | } | |
157 | ||
e30c5ee7 AK |
158 | /* |
159 | * Should be used only while writing commit-graph as it compares | |
160 | * generation value of commits by directly accessing commit-slab. | |
161 | */ | |
3d112755 GS |
162 | static int commit_gen_cmp(const void *va, const void *vb) |
163 | { | |
164 | const struct commit *a = *(const struct commit **)va; | |
165 | const struct commit *b = *(const struct commit **)vb; | |
166 | ||
d7f92784 AK |
167 | const timestamp_t generation_a = commit_graph_data_at(a)->generation; |
168 | const timestamp_t generation_b = commit_graph_data_at(b)->generation; | |
3d112755 | 169 | /* lower generation commits first */ |
c752ad09 | 170 | if (generation_a < generation_b) |
3d112755 | 171 | return -1; |
c752ad09 | 172 | else if (generation_a > generation_b) |
3d112755 GS |
173 | return 1; |
174 | ||
175 | /* use date as a heuristic when generations are equal */ | |
176 | if (a->date < b->date) | |
177 | return -1; | |
178 | else if (a->date > b->date) | |
179 | return 1; | |
180 | return 0; | |
181 | } | |
182 | ||
d21ee7d1 | 183 | char *get_commit_graph_filename(struct object_directory *obj_dir) |
08fd81c9 | 184 | { |
d21ee7d1 | 185 | return xstrfmt("%s/info/commit-graph", obj_dir->path); |
08fd81c9 DS |
186 | } |
187 | ||
ad2dd5bb | 188 | static char *get_split_graph_filename(struct object_directory *odb, |
5c84b339 DS |
189 | const char *oid_hex) |
190 | { | |
ad2dd5bb TB |
191 | return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path, |
192 | oid_hex); | |
5c84b339 DS |
193 | } |
194 | ||
663b2b1b | 195 | char *get_commit_graph_chain_filename(struct object_directory *odb) |
5c84b339 | 196 | { |
ad2dd5bb | 197 | return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path); |
08fd81c9 DS |
198 | } |
199 | ||
2a2e32bd DS |
200 | static struct commit_graph *alloc_commit_graph(void) |
201 | { | |
202 | struct commit_graph *g = xcalloc(1, sizeof(*g)); | |
2a2e32bd DS |
203 | |
204 | return g; | |
205 | } | |
206 | ||
d6538246 DS |
207 | extern int read_replace_refs; |
208 | ||
209 | static int commit_graph_compatible(struct repository *r) | |
210 | { | |
5cef295f DS |
211 | if (!r->gitdir) |
212 | return 0; | |
213 | ||
d6538246 DS |
214 | if (read_replace_refs) { |
215 | prepare_replace_object(r); | |
cdc986a7 | 216 | if (hashmap_get_size(&r->objects->replace_map->map)) |
d6538246 DS |
217 | return 0; |
218 | } | |
219 | ||
20fd6d57 | 220 | prepare_commit_graft(r); |
ce16364e | 221 | if (r->parsed_objects && |
cdc986a7 | 222 | (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent)) |
20fd6d57 | 223 | return 0; |
cdc986a7 | 224 | if (is_repository_shallow(r)) |
20fd6d57 DS |
225 | return 0; |
226 | ||
d6538246 DS |
227 | return 1; |
228 | } | |
229 | ||
61df89c8 ÆAB |
230 | int open_commit_graph(const char *graph_file, int *fd, struct stat *st) |
231 | { | |
232 | *fd = git_open(graph_file); | |
233 | if (*fd < 0) | |
234 | return 0; | |
235 | if (fstat(*fd, st)) { | |
236 | close(*fd); | |
237 | return 0; | |
238 | } | |
239 | return 1; | |
240 | } | |
241 | ||
ab14d067 TB |
242 | struct commit_graph *load_commit_graph_one_fd_st(struct repository *r, |
243 | int fd, struct stat *st, | |
a7df60ca | 244 | struct object_directory *odb) |
2a2e32bd DS |
245 | { |
246 | void *graph_map; | |
2a2e32bd | 247 | size_t graph_size; |
aa658574 | 248 | struct commit_graph *ret; |
2a2e32bd | 249 | |
61df89c8 | 250 | graph_size = xsize_t(st->st_size); |
2a2e32bd DS |
251 | |
252 | if (graph_size < GRAPH_MIN_SIZE) { | |
253 | close(fd); | |
67a530fa | 254 | error(_("commit-graph file is too small")); |
61df89c8 | 255 | return NULL; |
2a2e32bd DS |
256 | } |
257 | graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
c8828530 | 258 | close(fd); |
a92d8523 TB |
259 | prepare_repo_settings(r); |
260 | ret = parse_commit_graph(&r->settings, graph_map, graph_size); | |
aa658574 | 261 | |
a7df60ca TB |
262 | if (ret) |
263 | ret->odb = odb; | |
c8828530 | 264 | else |
aa658574 | 265 | munmap(graph_map, graph_size); |
aa658574 JS |
266 | |
267 | return ret; | |
268 | } | |
269 | ||
2ac138d5 ÆAB |
270 | static int verify_commit_graph_lite(struct commit_graph *g) |
271 | { | |
272 | /* | |
273 | * Basic validation shared between parse_commit_graph() | |
274 | * which'll be called every time the graph is used, and the | |
275 | * much more expensive verify_commit_graph() used by | |
276 | * "commit-graph verify". | |
277 | * | |
278 | * There should only be very basic checks here to ensure that | |
279 | * we don't e.g. segfault in fill_commit_in_graph(), but | |
280 | * because this is a very hot codepath nothing that e.g. loops | |
281 | * over g->num_commits, or runs a checksum on the commit-graph | |
282 | * itself. | |
283 | */ | |
284 | if (!g->chunk_oid_fanout) { | |
285 | error("commit-graph is missing the OID Fanout chunk"); | |
286 | return 1; | |
287 | } | |
288 | if (!g->chunk_oid_lookup) { | |
289 | error("commit-graph is missing the OID Lookup chunk"); | |
290 | return 1; | |
291 | } | |
292 | if (!g->chunk_commit_data) { | |
293 | error("commit-graph is missing the Commit Data chunk"); | |
294 | return 1; | |
295 | } | |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
2692c2f6 DS |
300 | static int graph_read_oid_lookup(const unsigned char *chunk_start, |
301 | size_t chunk_size, void *data) | |
302 | { | |
303 | struct commit_graph *g = data; | |
304 | g->chunk_oid_lookup = chunk_start; | |
305 | g->num_commits = chunk_size / g->hash_len; | |
306 | return 0; | |
307 | } | |
308 | ||
309 | static int graph_read_bloom_data(const unsigned char *chunk_start, | |
310 | size_t chunk_size, void *data) | |
311 | { | |
312 | struct commit_graph *g = data; | |
313 | uint32_t hash_version; | |
314 | g->chunk_bloom_data = chunk_start; | |
315 | hash_version = get_be32(chunk_start); | |
316 | ||
317 | if (hash_version != 1) | |
318 | return 0; | |
319 | ||
320 | g->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings)); | |
321 | g->bloom_filter_settings->hash_version = hash_version; | |
322 | g->bloom_filter_settings->num_hashes = get_be32(chunk_start + 4); | |
323 | g->bloom_filter_settings->bits_per_entry = get_be32(chunk_start + 8); | |
324 | g->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES; | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
a92d8523 | 329 | struct commit_graph *parse_commit_graph(struct repo_settings *s, |
ab14d067 | 330 | void *graph_map, size_t graph_size) |
aa658574 | 331 | { |
2692c2f6 | 332 | const unsigned char *data; |
aa658574 | 333 | struct commit_graph *graph; |
aa658574 JS |
334 | uint32_t graph_signature; |
335 | unsigned char graph_version, hash_version; | |
2692c2f6 | 336 | struct chunkfile *cf = NULL; |
aa658574 JS |
337 | |
338 | if (!graph_map) | |
339 | return NULL; | |
340 | ||
341 | if (graph_size < GRAPH_MIN_SIZE) | |
342 | return NULL; | |
343 | ||
2a2e32bd DS |
344 | data = (const unsigned char *)graph_map; |
345 | ||
346 | graph_signature = get_be32(data); | |
347 | if (graph_signature != GRAPH_SIGNATURE) { | |
93b4405f | 348 | error(_("commit-graph signature %X does not match signature %X"), |
2a2e32bd | 349 | graph_signature, GRAPH_SIGNATURE); |
aa658574 | 350 | return NULL; |
2a2e32bd DS |
351 | } |
352 | ||
353 | graph_version = *(unsigned char*)(data + 4); | |
354 | if (graph_version != GRAPH_VERSION) { | |
93b4405f | 355 | error(_("commit-graph version %X does not match version %X"), |
2a2e32bd | 356 | graph_version, GRAPH_VERSION); |
aa658574 | 357 | return NULL; |
2a2e32bd DS |
358 | } |
359 | ||
360 | hash_version = *(unsigned char*)(data + 5); | |
d9fef9d9 | 361 | if (hash_version != oid_version(the_hash_algo)) { |
93b4405f | 362 | error(_("commit-graph hash version %X does not match version %X"), |
d9fef9d9 | 363 | hash_version, oid_version(the_hash_algo)); |
aa658574 | 364 | return NULL; |
2a2e32bd DS |
365 | } |
366 | ||
367 | graph = alloc_commit_graph(); | |
368 | ||
c1665998 | 369 | graph->hash_len = the_hash_algo->rawsz; |
2a2e32bd | 370 | graph->num_chunks = *(unsigned char*)(data + 6); |
2a2e32bd DS |
371 | graph->data = graph_map; |
372 | graph->data_len = graph_size; | |
373 | ||
2ad4f1a7 | 374 | if (graph_size < GRAPH_HEADER_SIZE + |
2692c2f6 | 375 | (graph->num_chunks + 1) * CHUNK_TOC_ENTRY_SIZE + |
2ad4f1a7 SG |
376 | GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) { |
377 | error(_("commit-graph file is too small to hold %u chunks"), | |
378 | graph->num_chunks); | |
379 | free(graph); | |
380 | return NULL; | |
381 | } | |
382 | ||
2692c2f6 | 383 | cf = init_chunkfile(NULL); |
118bd570 | 384 | |
2692c2f6 DS |
385 | if (read_table_of_contents(cf, graph->data, graph_size, |
386 | GRAPH_HEADER_SIZE, graph->num_chunks)) | |
387 | goto free_and_return; | |
2a2e32bd | 388 | |
2692c2f6 DS |
389 | pair_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, |
390 | (const unsigned char **)&graph->chunk_oid_fanout); | |
391 | read_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, graph_read_oid_lookup, graph); | |
392 | pair_chunk(cf, GRAPH_CHUNKID_DATA, &graph->chunk_commit_data); | |
393 | pair_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, &graph->chunk_extra_edges); | |
394 | pair_chunk(cf, GRAPH_CHUNKID_BASE, &graph->chunk_base_graphs); | |
702110aa | 395 | |
a92d8523 | 396 | if (s->commit_graph_generation_version >= 2) { |
702110aa DS |
397 | pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA, |
398 | &graph->chunk_generation_data); | |
399 | pair_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW, | |
400 | &graph->chunk_generation_data_overflow); | |
3b0199d4 DS |
401 | |
402 | if (graph->chunk_generation_data) | |
403 | graph->read_generation_data = 1; | |
702110aa | 404 | } |
2692c2f6 | 405 | |
a92d8523 | 406 | if (s->commit_graph_read_changed_paths) { |
2692c2f6 DS |
407 | pair_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES, |
408 | &graph->chunk_bloom_indexes); | |
409 | read_chunk(cf, GRAPH_CHUNKID_BLOOMDATA, | |
410 | graph_read_bloom_data, graph); | |
2a2e32bd DS |
411 | } |
412 | ||
76ffbca7 GS |
413 | if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) { |
414 | init_bloom_filters(); | |
415 | } else { | |
416 | /* We need both the bloom chunks to exist together. Else ignore the data */ | |
417 | graph->chunk_bloom_indexes = NULL; | |
418 | graph->chunk_bloom_data = NULL; | |
fbda77c6 | 419 | FREE_AND_NULL(graph->bloom_filter_settings); |
76ffbca7 GS |
420 | } |
421 | ||
92e2cab9 | 422 | oidread(&graph->oid, graph->data + graph->data_len - graph->hash_len); |
118bd570 | 423 | |
fbda77c6 JT |
424 | if (verify_commit_graph_lite(graph)) |
425 | goto free_and_return; | |
2ac138d5 | 426 | |
2692c2f6 | 427 | free_chunkfile(cf); |
2a2e32bd | 428 | return graph; |
fbda77c6 JT |
429 | |
430 | free_and_return: | |
2692c2f6 | 431 | free_chunkfile(cf); |
fbda77c6 JT |
432 | free(graph->bloom_filter_settings); |
433 | free(graph); | |
434 | return NULL; | |
2a2e32bd DS |
435 | } |
436 | ||
ab14d067 TB |
437 | static struct commit_graph *load_commit_graph_one(struct repository *r, |
438 | const char *graph_file, | |
a7df60ca | 439 | struct object_directory *odb) |
61df89c8 ÆAB |
440 | { |
441 | ||
442 | struct stat st; | |
443 | int fd; | |
6c622f9f | 444 | struct commit_graph *g; |
61df89c8 ÆAB |
445 | int open_ok = open_commit_graph(graph_file, &fd, &st); |
446 | ||
447 | if (!open_ok) | |
448 | return NULL; | |
449 | ||
ab14d067 | 450 | g = load_commit_graph_one_fd_st(r, fd, &st, odb); |
6c622f9f DS |
451 | |
452 | if (g) | |
453 | g->filename = xstrdup(graph_file); | |
454 | ||
455 | return g; | |
61df89c8 ÆAB |
456 | } |
457 | ||
13c24992 TB |
458 | static struct commit_graph *load_commit_graph_v1(struct repository *r, |
459 | struct object_directory *odb) | |
5c84b339 | 460 | { |
ad2dd5bb | 461 | char *graph_name = get_commit_graph_filename(odb); |
ab14d067 | 462 | struct commit_graph *g = load_commit_graph_one(r, graph_name, odb); |
5c84b339 DS |
463 | free(graph_name); |
464 | ||
465 | return g; | |
466 | } | |
467 | ||
468 | static int add_graph_to_chain(struct commit_graph *g, | |
469 | struct commit_graph *chain, | |
470 | struct object_id *oids, | |
471 | int n) | |
472 | { | |
473 | struct commit_graph *cur_g = chain; | |
474 | ||
118bd570 DS |
475 | if (n && !g->chunk_base_graphs) { |
476 | warning(_("commit-graph has no base graphs chunk")); | |
477 | return 0; | |
478 | } | |
479 | ||
5c84b339 DS |
480 | while (n) { |
481 | n--; | |
118bd570 DS |
482 | |
483 | if (!cur_g || | |
484 | !oideq(&oids[n], &cur_g->oid) || | |
209250ef | 485 | !hasheq(oids[n].hash, g->chunk_base_graphs + st_mult(g->hash_len, n))) { |
118bd570 DS |
486 | warning(_("commit-graph chain does not match")); |
487 | return 0; | |
488 | } | |
489 | ||
5c84b339 DS |
490 | cur_g = cur_g->base_graph; |
491 | } | |
492 | ||
493 | g->base_graph = chain; | |
494 | ||
209250ef TB |
495 | if (chain) { |
496 | if (unsigned_add_overflows(chain->num_commits, | |
497 | chain->num_commits_in_base)) { | |
498 | warning(_("commit count in base graph too high: %"PRIuMAX), | |
499 | (uintmax_t)chain->num_commits_in_base); | |
500 | return 0; | |
501 | } | |
5c84b339 | 502 | g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base; |
209250ef | 503 | } |
5c84b339 DS |
504 | |
505 | return 1; | |
506 | } | |
507 | ||
13c24992 TB |
508 | static struct commit_graph *load_commit_graph_chain(struct repository *r, |
509 | struct object_directory *odb) | |
5c84b339 DS |
510 | { |
511 | struct commit_graph *graph_chain = NULL; | |
512 | struct strbuf line = STRBUF_INIT; | |
513 | struct stat st; | |
514 | struct object_id *oids; | |
515 | int i = 0, valid = 1, count; | |
663b2b1b | 516 | char *chain_name = get_commit_graph_chain_filename(odb); |
5c84b339 DS |
517 | FILE *fp; |
518 | int stat_res; | |
519 | ||
520 | fp = fopen(chain_name, "r"); | |
521 | stat_res = stat(chain_name, &st); | |
522 | free(chain_name); | |
523 | ||
c0befa0c | 524 | if (!fp) |
5c84b339 | 525 | return NULL; |
c0befa0c KT |
526 | if (stat_res || |
527 | st.st_size <= the_hash_algo->hexsz) { | |
528 | fclose(fp); | |
529 | return NULL; | |
530 | } | |
5c84b339 DS |
531 | |
532 | count = st.st_size / (the_hash_algo->hexsz + 1); | |
ca56dadb | 533 | CALLOC_ARRAY(oids, count); |
5c84b339 | 534 | |
c523035c DS |
535 | prepare_alt_odb(r); |
536 | ||
537 | for (i = 0; i < count; i++) { | |
538 | struct object_directory *odb; | |
5c84b339 DS |
539 | |
540 | if (strbuf_getline_lf(&line, fp) == EOF) | |
541 | break; | |
542 | ||
543 | if (get_oid_hex(line.buf, &oids[i])) { | |
544 | warning(_("invalid commit-graph chain: line '%s' not a hash"), | |
545 | line.buf); | |
546 | valid = 0; | |
547 | break; | |
548 | } | |
549 | ||
c523035c DS |
550 | valid = 0; |
551 | for (odb = r->objects->odb; odb; odb = odb->next) { | |
ad2dd5bb | 552 | char *graph_name = get_split_graph_filename(odb, line.buf); |
ab14d067 | 553 | struct commit_graph *g = load_commit_graph_one(r, graph_name, odb); |
5c84b339 | 554 | |
c523035c DS |
555 | free(graph_name); |
556 | ||
557 | if (g) { | |
c523035c DS |
558 | if (add_graph_to_chain(g, graph_chain, oids, i)) { |
559 | graph_chain = g; | |
560 | valid = 1; | |
561 | } | |
562 | ||
563 | break; | |
564 | } | |
565 | } | |
566 | ||
567 | if (!valid) { | |
568 | warning(_("unable to find all commit-graph files")); | |
569 | break; | |
570 | } | |
5c84b339 DS |
571 | } |
572 | ||
573 | free(oids); | |
574 | fclose(fp); | |
0aa6bce7 | 575 | strbuf_release(&line); |
5c84b339 DS |
576 | |
577 | return graph_chain; | |
578 | } | |
579 | ||
448a39e6 DS |
580 | /* |
581 | * returns 1 if and only if all graphs in the chain have | |
582 | * corrected commit dates stored in the generation_data chunk. | |
583 | */ | |
584 | static int validate_mixed_generation_chain(struct commit_graph *g) | |
1fdc383c | 585 | { |
448a39e6 DS |
586 | int read_generation_data = 1; |
587 | struct commit_graph *p = g; | |
1fdc383c | 588 | |
448a39e6 DS |
589 | while (read_generation_data && p) { |
590 | read_generation_data = p->read_generation_data; | |
591 | p = p->base_graph; | |
592 | } | |
1fdc383c | 593 | |
448a39e6 DS |
594 | if (read_generation_data) |
595 | return 1; | |
1fdc383c AK |
596 | |
597 | while (g) { | |
448a39e6 | 598 | g->read_generation_data = 0; |
1fdc383c AK |
599 | g = g->base_graph; |
600 | } | |
448a39e6 DS |
601 | |
602 | return 0; | |
1fdc383c AK |
603 | } |
604 | ||
13c24992 TB |
605 | struct commit_graph *read_commit_graph_one(struct repository *r, |
606 | struct object_directory *odb) | |
5c84b339 | 607 | { |
13c24992 | 608 | struct commit_graph *g = load_commit_graph_v1(r, odb); |
5c84b339 DS |
609 | |
610 | if (!g) | |
13c24992 | 611 | g = load_commit_graph_chain(r, odb); |
5c84b339 | 612 | |
1fdc383c AK |
613 | validate_mixed_generation_chain(g); |
614 | ||
5c84b339 | 615 | return g; |
61df89c8 ÆAB |
616 | } |
617 | ||
13c24992 TB |
618 | static void prepare_commit_graph_one(struct repository *r, |
619 | struct object_directory *odb) | |
177722b3 | 620 | { |
177722b3 | 621 | |
dade47c0 | 622 | if (r->objects->commit_graph) |
177722b3 DS |
623 | return; |
624 | ||
13c24992 | 625 | r->objects->commit_graph = read_commit_graph_one(r, odb); |
177722b3 DS |
626 | } |
627 | ||
5faf357b JT |
628 | /* |
629 | * Return 1 if commit_graph is non-NULL, and 0 otherwise. | |
630 | * | |
15beaaa3 | 631 | * On the first invocation, this function attempts to load the commit |
5faf357b JT |
632 | * graph if the_repository is configured to have one. |
633 | */ | |
dade47c0 | 634 | static int prepare_commit_graph(struct repository *r) |
177722b3 | 635 | { |
263db403 | 636 | struct object_directory *odb; |
dade47c0 | 637 | |
6abada18 | 638 | /* |
0803f9c7 LD |
639 | * Early return if there is no git dir or if the commit graph is |
640 | * disabled. | |
641 | * | |
6abada18 JK |
642 | * This must come before the "already attempted?" check below, because |
643 | * we want to disable even an already-loaded graph file. | |
644 | */ | |
0803f9c7 | 645 | if (!r->gitdir || r->commit_graph_disabled) |
6abada18 | 646 | return 0; |
43d35618 | 647 | |
dade47c0 JT |
648 | if (r->objects->commit_graph_attempted) |
649 | return !!r->objects->commit_graph; | |
650 | r->objects->commit_graph_attempted = 1; | |
651 | ||
7211b9e7 DS |
652 | prepare_repo_settings(r); |
653 | ||
859fdc0c | 654 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) && |
7211b9e7 | 655 | r->settings.core_commit_graph != 1) |
dade47c0 JT |
656 | /* |
657 | * This repository is not configured to use commit graphs, so | |
658 | * do not load one. (But report commit_graph_attempted anyway | |
659 | * so that commit graph loading is not attempted again for this | |
660 | * repository.) | |
661 | */ | |
5faf357b JT |
662 | return 0; |
663 | ||
d6538246 DS |
664 | if (!commit_graph_compatible(r)) |
665 | return 0; | |
666 | ||
dade47c0 | 667 | prepare_alt_odb(r); |
f0eaf638 | 668 | for (odb = r->objects->odb; |
263db403 JK |
669 | !r->objects->commit_graph && odb; |
670 | odb = odb->next) | |
13c24992 | 671 | prepare_commit_graph_one(r, odb); |
dade47c0 | 672 | return !!r->objects->commit_graph; |
177722b3 DS |
673 | } |
674 | ||
6cc01743 DS |
675 | int generation_numbers_enabled(struct repository *r) |
676 | { | |
677 | uint32_t first_generation; | |
678 | struct commit_graph *g; | |
679 | if (!prepare_commit_graph(r)) | |
680 | return 0; | |
681 | ||
682 | g = r->objects->commit_graph; | |
683 | ||
684 | if (!g->num_commits) | |
685 | return 0; | |
686 | ||
687 | first_generation = get_be32(g->chunk_commit_data + | |
688 | g->hash_len + 8) >> 2; | |
689 | ||
690 | return !!first_generation; | |
691 | } | |
692 | ||
8d00d7c3 AK |
693 | int corrected_commit_dates_enabled(struct repository *r) |
694 | { | |
695 | struct commit_graph *g; | |
696 | if (!prepare_commit_graph(r)) | |
697 | return 0; | |
698 | ||
699 | g = r->objects->commit_graph; | |
700 | ||
701 | if (!g->num_commits) | |
702 | return 0; | |
703 | ||
704 | return g->read_generation_data; | |
705 | } | |
706 | ||
4f364405 TB |
707 | struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r) |
708 | { | |
709 | struct commit_graph *g = r->objects->commit_graph; | |
710 | while (g) { | |
711 | if (g->bloom_filter_settings) | |
712 | return g->bloom_filter_settings; | |
713 | g = g->base_graph; | |
714 | } | |
715 | return NULL; | |
716 | } | |
717 | ||
d4f4d60f DS |
718 | static void close_commit_graph_one(struct commit_graph *g) |
719 | { | |
720 | if (!g) | |
721 | return; | |
722 | ||
957ba814 | 723 | clear_commit_graph_data_slab(&commit_graph_data_slab); |
d4f4d60f DS |
724 | close_commit_graph_one(g->base_graph); |
725 | free_commit_graph(g); | |
726 | } | |
727 | ||
c3a3a964 | 728 | void close_commit_graph(struct raw_object_store *o) |
177722b3 | 729 | { |
d4f4d60f | 730 | close_commit_graph_one(o->commit_graph); |
c3a3a964 | 731 | o->commit_graph = NULL; |
177722b3 DS |
732 | } |
733 | ||
809ea28f | 734 | static int bsearch_graph(struct commit_graph *g, const struct object_id *oid, uint32_t *pos) |
177722b3 DS |
735 | { |
736 | return bsearch_hash(oid->hash, g->chunk_oid_fanout, | |
737 | g->chunk_oid_lookup, g->hash_len, pos); | |
738 | } | |
739 | ||
d4f4d60f DS |
740 | static void load_oid_from_graph(struct commit_graph *g, |
741 | uint32_t pos, | |
742 | struct object_id *oid) | |
743 | { | |
744 | uint32_t lex_index; | |
745 | ||
746 | while (g && pos < g->num_commits_in_base) | |
747 | g = g->base_graph; | |
748 | ||
749 | if (!g) | |
750 | BUG("NULL commit-graph"); | |
751 | ||
752 | if (pos >= g->num_commits + g->num_commits_in_base) | |
753 | die(_("invalid commit position. commit-graph is likely corrupt")); | |
754 | ||
755 | lex_index = pos - g->num_commits_in_base; | |
756 | ||
0bd8f30a | 757 | oidread(oid, g->chunk_oid_lookup + st_mult(g->hash_len, lex_index)); |
d4f4d60f DS |
758 | } |
759 | ||
4f542b7a SB |
760 | static struct commit_list **insert_parent_or_die(struct repository *r, |
761 | struct commit_graph *g, | |
d4f4d60f | 762 | uint32_t pos, |
177722b3 DS |
763 | struct commit_list **pptr) |
764 | { | |
765 | struct commit *c; | |
766 | struct object_id oid; | |
96af91d4 | 767 | |
d4f4d60f DS |
768 | if (pos >= g->num_commits + g->num_commits_in_base) |
769 | die("invalid parent position %"PRIu32, pos); | |
53614b13 | 770 | |
d4f4d60f | 771 | load_oid_from_graph(g, pos, &oid); |
4f542b7a | 772 | c = lookup_commit(r, &oid); |
177722b3 | 773 | if (!c) |
4f5b532d | 774 | die(_("could not find commit %s"), oid_to_hex(&oid)); |
c49c82aa | 775 | commit_graph_data_at(c)->graph_pos = pos; |
177722b3 DS |
776 | return &commit_list_insert(c, pptr)->next; |
777 | } | |
778 | ||
e2838d85 DS |
779 | static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos) |
780 | { | |
d4f4d60f | 781 | const unsigned char *commit_data; |
c752ad09 | 782 | struct commit_graph_data *graph_data; |
e8b63005 AK |
783 | uint32_t lex_index, offset_pos; |
784 | uint64_t date_high, date_low, offset; | |
d4f4d60f DS |
785 | |
786 | while (pos < g->num_commits_in_base) | |
787 | g = g->base_graph; | |
788 | ||
f90fca63 AK |
789 | if (pos >= g->num_commits + g->num_commits_in_base) |
790 | die(_("invalid commit position. commit-graph is likely corrupt")); | |
791 | ||
d4f4d60f | 792 | lex_index = pos - g->num_commits_in_base; |
2740ed1c | 793 | commit_data = g->chunk_commit_data + st_mult(GRAPH_DATA_WIDTH, lex_index); |
c752ad09 AK |
794 | |
795 | graph_data = commit_graph_data_at(item); | |
796 | graph_data->graph_pos = pos; | |
f90fca63 AK |
797 | |
798 | date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; | |
799 | date_low = get_be32(commit_data + g->hash_len + 12); | |
800 | item->date = (timestamp_t)((date_high << 32) | date_low); | |
801 | ||
1fdc383c | 802 | if (g->read_generation_data) { |
2740ed1c | 803 | offset = (timestamp_t)get_be32(g->chunk_generation_data + st_mult(sizeof(uint32_t), lex_index)); |
e8b63005 AK |
804 | |
805 | if (offset & CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW) { | |
806 | if (!g->chunk_generation_data_overflow) | |
807 | die(_("commit-graph requires overflow generation data but has none")); | |
808 | ||
809 | offset_pos = offset ^ CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW; | |
2740ed1c | 810 | graph_data->generation = item->date + get_be64(g->chunk_generation_data_overflow + st_mult(8, offset_pos)); |
e8b63005 AK |
811 | } else |
812 | graph_data->generation = item->date + offset; | |
813 | } else | |
814 | graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2; | |
72a2bfca AK |
815 | |
816 | if (g->topo_levels) | |
817 | *topo_level_slab_at(g->topo_levels, item) = get_be32(commit_data + g->hash_len + 8) >> 2; | |
e2838d85 DS |
818 | } |
819 | ||
a133c40b NTND |
820 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
821 | { | |
822 | c->maybe_tree = t; | |
823 | } | |
824 | ||
4f542b7a SB |
825 | static int fill_commit_in_graph(struct repository *r, |
826 | struct commit *item, | |
827 | struct commit_graph *g, uint32_t pos) | |
177722b3 | 828 | { |
177722b3 DS |
829 | uint32_t edge_value; |
830 | uint32_t *parent_data_ptr; | |
177722b3 | 831 | struct commit_list **pptr; |
d4f4d60f DS |
832 | const unsigned char *commit_data; |
833 | uint32_t lex_index; | |
177722b3 | 834 | |
d4f4d60f DS |
835 | while (pos < g->num_commits_in_base) |
836 | g = g->base_graph; | |
837 | ||
f90fca63 | 838 | fill_commit_graph_info(item, g, pos); |
d4f4d60f | 839 | |
d4f4d60f | 840 | lex_index = pos - g->num_commits_in_base; |
50a71c29 | 841 | commit_data = g->chunk_commit_data + st_mult(g->hash_len + 16, lex_index); |
d4f4d60f DS |
842 | |
843 | item->object.parsed = 1; | |
177722b3 | 844 | |
a133c40b | 845 | set_commit_tree(item, NULL); |
177722b3 | 846 | |
177722b3 DS |
847 | pptr = &item->parents; |
848 | ||
849 | edge_value = get_be32(commit_data + g->hash_len); | |
850 | if (edge_value == GRAPH_PARENT_NONE) | |
851 | return 1; | |
4f542b7a | 852 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
853 | |
854 | edge_value = get_be32(commit_data + g->hash_len + 4); | |
855 | if (edge_value == GRAPH_PARENT_NONE) | |
856 | return 1; | |
5af7417b | 857 | if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) { |
4f542b7a | 858 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
859 | return 1; |
860 | } | |
861 | ||
5af7417b | 862 | parent_data_ptr = (uint32_t*)(g->chunk_extra_edges + |
50a71c29 | 863 | st_mult(4, edge_value & GRAPH_EDGE_LAST_MASK)); |
177722b3 DS |
864 | do { |
865 | edge_value = get_be32(parent_data_ptr); | |
4f542b7a | 866 | pptr = insert_parent_or_die(r, g, |
177722b3 DS |
867 | edge_value & GRAPH_EDGE_LAST_MASK, |
868 | pptr); | |
869 | parent_data_ptr++; | |
870 | } while (!(edge_value & GRAPH_LAST_EDGE)); | |
871 | ||
872 | return 1; | |
873 | } | |
874 | ||
809ea28f PS |
875 | static int search_commit_pos_in_graph(const struct object_id *id, struct commit_graph *g, uint32_t *pos) |
876 | { | |
877 | struct commit_graph *cur_g = g; | |
878 | uint32_t lex_index; | |
879 | ||
880 | while (cur_g && !bsearch_graph(cur_g, id, &lex_index)) | |
881 | cur_g = cur_g->base_graph; | |
882 | ||
883 | if (cur_g) { | |
884 | *pos = lex_index + cur_g->num_commits_in_base; | |
885 | return 1; | |
886 | } | |
887 | ||
888 | return 0; | |
889 | } | |
890 | ||
891 | static int find_commit_pos_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos) | |
e2838d85 | 892 | { |
c752ad09 AK |
893 | uint32_t graph_pos = commit_graph_position(item); |
894 | if (graph_pos != COMMIT_NOT_FROM_GRAPH) { | |
895 | *pos = graph_pos; | |
e2838d85 DS |
896 | return 1; |
897 | } else { | |
809ea28f | 898 | return search_commit_pos_in_graph(&item->object.oid, g, pos); |
e2838d85 DS |
899 | } |
900 | } | |
901 | ||
7805360b TB |
902 | int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c, |
903 | uint32_t *pos) | |
904 | { | |
905 | if (!prepare_commit_graph(r)) | |
906 | return 0; | |
907 | return find_commit_pos_in_graph(c, r->objects->commit_graph, pos); | |
908 | } | |
909 | ||
f559d6d4 PS |
910 | struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id) |
911 | { | |
912 | struct commit *commit; | |
913 | uint32_t pos; | |
914 | ||
d6045294 | 915 | if (!prepare_commit_graph(repo)) |
f559d6d4 PS |
916 | return NULL; |
917 | if (!search_commit_pos_in_graph(id, repo->objects->commit_graph, &pos)) | |
918 | return NULL; | |
3a1ea94a | 919 | if (!has_object(repo, id, 0)) |
f559d6d4 PS |
920 | return NULL; |
921 | ||
922 | commit = lookup_commit(repo, id); | |
923 | if (!commit) | |
924 | return NULL; | |
925 | if (commit->object.parsed) | |
926 | return commit; | |
927 | ||
928 | if (!fill_commit_in_graph(repo, commit, repo->objects->commit_graph, pos)) | |
929 | return NULL; | |
930 | ||
931 | return commit; | |
932 | } | |
933 | ||
4f542b7a SB |
934 | static int parse_commit_in_graph_one(struct repository *r, |
935 | struct commit_graph *g, | |
936 | struct commit *item) | |
177722b3 | 937 | { |
e2838d85 DS |
938 | uint32_t pos; |
939 | ||
177722b3 DS |
940 | if (item->object.parsed) |
941 | return 1; | |
ee797053 | 942 | |
809ea28f | 943 | if (find_commit_pos_in_graph(item, g, &pos)) |
4f542b7a | 944 | return fill_commit_in_graph(r, item, g, pos); |
ee797053 DS |
945 | |
946 | return 0; | |
947 | } | |
948 | ||
dade47c0 | 949 | int parse_commit_in_graph(struct repository *r, struct commit *item) |
ee797053 | 950 | { |
7b671f8c DS |
951 | static int checked_env = 0; |
952 | ||
953 | if (!checked_env && | |
954 | git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0)) | |
955 | die("dying as requested by the '%s' variable on commit-graph parse!", | |
956 | GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE); | |
957 | checked_env = 1; | |
958 | ||
dade47c0 | 959 | if (!prepare_commit_graph(r)) |
ee797053 | 960 | return 0; |
4f542b7a | 961 | return parse_commit_in_graph_one(r, r->objects->commit_graph, item); |
177722b3 DS |
962 | } |
963 | ||
dade47c0 | 964 | void load_commit_graph_info(struct repository *r, struct commit *item) |
e2838d85 DS |
965 | { |
966 | uint32_t pos; | |
7805360b | 967 | if (repo_find_commit_pos_in_graph(r, item, &pos)) |
dade47c0 | 968 | fill_commit_graph_info(item, r->objects->commit_graph, pos); |
e2838d85 DS |
969 | } |
970 | ||
4f542b7a SB |
971 | static struct tree *load_tree_for_commit(struct repository *r, |
972 | struct commit_graph *g, | |
973 | struct commit *c) | |
7b8a21db DS |
974 | { |
975 | struct object_id oid; | |
d4f4d60f | 976 | const unsigned char *commit_data; |
c752ad09 | 977 | uint32_t graph_pos = commit_graph_position(c); |
d4f4d60f | 978 | |
c752ad09 | 979 | while (graph_pos < g->num_commits_in_base) |
d4f4d60f DS |
980 | g = g->base_graph; |
981 | ||
982 | commit_data = g->chunk_commit_data + | |
51c31a64 | 983 | st_mult(GRAPH_DATA_WIDTH, graph_pos - g->num_commits_in_base); |
7b8a21db | 984 | |
92e2cab9 | 985 | oidread(&oid, commit_data); |
a133c40b | 986 | set_commit_tree(c, lookup_tree(r, &oid)); |
7b8a21db DS |
987 | |
988 | return c->maybe_tree; | |
989 | } | |
990 | ||
4f542b7a SB |
991 | static struct tree *get_commit_tree_in_graph_one(struct repository *r, |
992 | struct commit_graph *g, | |
0cbef8f8 | 993 | const struct commit *c) |
7b8a21db DS |
994 | { |
995 | if (c->maybe_tree) | |
996 | return c->maybe_tree; | |
c49c82aa | 997 | if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH) |
0cbef8f8 DS |
998 | BUG("get_commit_tree_in_graph_one called from non-commit-graph commit"); |
999 | ||
4f542b7a | 1000 | return load_tree_for_commit(r, g, (struct commit *)c); |
0cbef8f8 | 1001 | } |
7b8a21db | 1002 | |
dade47c0 | 1003 | struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c) |
0cbef8f8 | 1004 | { |
4f542b7a | 1005 | return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c); |
7b8a21db DS |
1006 | } |
1007 | ||
c9905bea DS |
1008 | struct packed_commit_list { |
1009 | struct commit **list; | |
3361390c JK |
1010 | size_t nr; |
1011 | size_t alloc; | |
c9905bea DS |
1012 | }; |
1013 | ||
c9905bea DS |
1014 | struct write_commit_graph_context { |
1015 | struct repository *r; | |
0bd52e27 | 1016 | struct object_directory *odb; |
c9905bea | 1017 | char *graph_name; |
a5f1c448 | 1018 | struct oid_array oids; |
c9905bea DS |
1019 | struct packed_commit_list commits; |
1020 | int num_extra_edges; | |
e8b63005 | 1021 | int num_generation_data_overflows; |
c9905bea DS |
1022 | unsigned long approx_nr_objects; |
1023 | struct progress *progress; | |
1024 | int progress_done; | |
1025 | uint64_t progress_cnt; | |
6c622f9f DS |
1026 | |
1027 | char *base_graph_name; | |
1028 | int num_commit_graphs_before; | |
1029 | int num_commit_graphs_after; | |
1030 | char **commit_graph_filenames_before; | |
1031 | char **commit_graph_filenames_after; | |
1032 | char **commit_graph_hash_after; | |
1033 | uint32_t new_num_commits_in_base; | |
1034 | struct commit_graph *new_base_graph; | |
1035 | ||
c9905bea | 1036 | unsigned append:1, |
6c622f9f | 1037 | report_progress:1, |
7c5c9b9c | 1038 | split:1, |
3d112755 | 1039 | changed_paths:1, |
e8b63005 | 1040 | order_by_pack:1, |
fde55b09 DS |
1041 | write_generation_data:1, |
1042 | trust_generation_numbers:1; | |
c2bc6e6a | 1043 | |
72a2bfca | 1044 | struct topo_level_slab *topo_levels; |
98bb7961 | 1045 | const struct commit_graph_opts *opts; |
f97b9325 | 1046 | size_t total_bloom_filter_data_size; |
98037f2b | 1047 | const struct bloom_filter_settings *bloom_settings; |
312cff52 TB |
1048 | |
1049 | int count_bloom_filter_computed; | |
1050 | int count_bloom_filter_not_computed; | |
59f0d507 | 1051 | int count_bloom_filter_trunc_empty; |
312cff52 | 1052 | int count_bloom_filter_trunc_large; |
c9905bea DS |
1053 | }; |
1054 | ||
9bab081d | 1055 | static int write_graph_chunk_fanout(struct hashfile *f, |
eb907191 | 1056 | void *data) |
08fd81c9 | 1057 | { |
eb907191 | 1058 | struct write_commit_graph_context *ctx = data; |
08fd81c9 | 1059 | int i, count = 0; |
c9905bea | 1060 | struct commit **list = ctx->commits.list; |
08fd81c9 DS |
1061 | |
1062 | /* | |
1063 | * Write the first-level table (the list is sorted, | |
1064 | * but we use a 256-entry lookup to be able to avoid | |
1065 | * having to do eight extra binary search iterations). | |
1066 | */ | |
1067 | for (i = 0; i < 256; i++) { | |
c9905bea | 1068 | while (count < ctx->commits.nr) { |
08fd81c9 DS |
1069 | if ((*list)->object.oid.hash[0] != i) |
1070 | break; | |
c9905bea | 1071 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 DS |
1072 | count++; |
1073 | list++; | |
1074 | } | |
1075 | ||
1076 | hashwrite_be32(f, count); | |
1077 | } | |
9bab081d SG |
1078 | |
1079 | return 0; | |
08fd81c9 DS |
1080 | } |
1081 | ||
9bab081d | 1082 | static int write_graph_chunk_oids(struct hashfile *f, |
eb907191 | 1083 | void *data) |
08fd81c9 | 1084 | { |
eb907191 | 1085 | struct write_commit_graph_context *ctx = data; |
c9905bea | 1086 | struct commit **list = ctx->commits.list; |
08fd81c9 | 1087 | int count; |
c9905bea DS |
1088 | for (count = 0; count < ctx->commits.nr; count++, list++) { |
1089 | display_progress(ctx->progress, ++ctx->progress_cnt); | |
9bab081d | 1090 | hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz); |
53035c4f | 1091 | } |
9bab081d SG |
1092 | |
1093 | return 0; | |
08fd81c9 DS |
1094 | } |
1095 | ||
8380dcd7 | 1096 | static const struct object_id *commit_to_oid(size_t index, const void *table) |
08fd81c9 | 1097 | { |
8380dcd7 | 1098 | const struct commit * const *commits = table; |
45ee13b9 | 1099 | return &commits[index]->object.oid; |
08fd81c9 DS |
1100 | } |
1101 | ||
9bab081d | 1102 | static int write_graph_chunk_data(struct hashfile *f, |
eb907191 | 1103 | void *data) |
08fd81c9 | 1104 | { |
eb907191 | 1105 | struct write_commit_graph_context *ctx = data; |
c9905bea DS |
1106 | struct commit **list = ctx->commits.list; |
1107 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
1108 | uint32_t num_extra_edges = 0; |
1109 | ||
1110 | while (list < last) { | |
1111 | struct commit_list *parent; | |
806278de | 1112 | struct object_id *tree; |
08fd81c9 DS |
1113 | int edge_value; |
1114 | uint32_t packedDate[2]; | |
c9905bea | 1115 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 | 1116 | |
c4cc0831 | 1117 | if (repo_parse_commit_no_graph(ctx->r, *list)) |
16749b8d TB |
1118 | die(_("unable to parse commit %s"), |
1119 | oid_to_hex(&(*list)->object.oid)); | |
806278de | 1120 | tree = get_commit_tree_oid(*list); |
9bab081d | 1121 | hashwrite(f, tree->hash, the_hash_algo->rawsz); |
08fd81c9 DS |
1122 | |
1123 | parent = (*list)->parents; | |
1124 | ||
1125 | if (!parent) | |
1126 | edge_value = GRAPH_PARENT_NONE; | |
1127 | else { | |
45ee13b9 JK |
1128 | edge_value = oid_pos(&parent->item->object.oid, |
1129 | ctx->commits.list, | |
1130 | ctx->commits.nr, | |
1131 | commit_to_oid); | |
08fd81c9 | 1132 | |
6c622f9f DS |
1133 | if (edge_value >= 0) |
1134 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 1135 | else if (ctx->new_base_graph) { |
6c622f9f | 1136 | uint32_t pos; |
809ea28f PS |
1137 | if (find_commit_pos_in_graph(parent->item, |
1138 | ctx->new_base_graph, | |
1139 | &pos)) | |
6c622f9f DS |
1140 | edge_value = pos; |
1141 | } | |
1142 | ||
08fd81c9 | 1143 | if (edge_value < 0) |
cce99cd8 DS |
1144 | BUG("missing parent %s for commit %s", |
1145 | oid_to_hex(&parent->item->object.oid), | |
1146 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
1147 | } |
1148 | ||
1149 | hashwrite_be32(f, edge_value); | |
1150 | ||
1151 | if (parent) | |
1152 | parent = parent->next; | |
1153 | ||
1154 | if (!parent) | |
1155 | edge_value = GRAPH_PARENT_NONE; | |
1156 | else if (parent->next) | |
5af7417b | 1157 | edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; |
08fd81c9 | 1158 | else { |
45ee13b9 JK |
1159 | edge_value = oid_pos(&parent->item->object.oid, |
1160 | ctx->commits.list, | |
1161 | ctx->commits.nr, | |
1162 | commit_to_oid); | |
6c622f9f DS |
1163 | |
1164 | if (edge_value >= 0) | |
1165 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 1166 | else if (ctx->new_base_graph) { |
6c622f9f | 1167 | uint32_t pos; |
809ea28f PS |
1168 | if (find_commit_pos_in_graph(parent->item, |
1169 | ctx->new_base_graph, | |
1170 | &pos)) | |
6c622f9f DS |
1171 | edge_value = pos; |
1172 | } | |
1173 | ||
08fd81c9 | 1174 | if (edge_value < 0) |
cce99cd8 DS |
1175 | BUG("missing parent %s for commit %s", |
1176 | oid_to_hex(&parent->item->object.oid), | |
1177 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
1178 | } |
1179 | ||
1180 | hashwrite_be32(f, edge_value); | |
1181 | ||
5af7417b | 1182 | if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) { |
08fd81c9 DS |
1183 | do { |
1184 | num_extra_edges++; | |
1185 | parent = parent->next; | |
1186 | } while (parent); | |
1187 | } | |
1188 | ||
1189 | if (sizeof((*list)->date) > 4) | |
1190 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); | |
1191 | else | |
1192 | packedDate[0] = 0; | |
1193 | ||
72a2bfca | 1194 | packedDate[0] |= htonl(*topo_level_slab_at(ctx->topo_levels, *list) << 2); |
3258c663 | 1195 | |
08fd81c9 DS |
1196 | packedDate[1] = htonl((*list)->date); |
1197 | hashwrite(f, packedDate, 8); | |
1198 | ||
1199 | list++; | |
1200 | } | |
9bab081d SG |
1201 | |
1202 | return 0; | |
08fd81c9 DS |
1203 | } |
1204 | ||
e8b63005 | 1205 | static int write_graph_chunk_generation_data(struct hashfile *f, |
eb907191 | 1206 | void *data) |
e8b63005 | 1207 | { |
eb907191 | 1208 | struct write_commit_graph_context *ctx = data; |
e8b63005 AK |
1209 | int i, num_generation_data_overflows = 0; |
1210 | ||
1211 | for (i = 0; i < ctx->commits.nr; i++) { | |
1212 | struct commit *c = ctx->commits.list[i]; | |
90cb1c47 DS |
1213 | timestamp_t offset; |
1214 | repo_parse_commit(ctx->r, c); | |
1215 | offset = commit_graph_data_at(c)->generation - c->date; | |
e8b63005 AK |
1216 | display_progress(ctx->progress, ++ctx->progress_cnt); |
1217 | ||
1218 | if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) { | |
1219 | offset = CORRECTED_COMMIT_DATE_OFFSET_OVERFLOW | num_generation_data_overflows; | |
1220 | num_generation_data_overflows++; | |
1221 | } | |
1222 | ||
1223 | hashwrite_be32(f, offset); | |
1224 | } | |
1225 | ||
1226 | return 0; | |
1227 | } | |
1228 | ||
1229 | static int write_graph_chunk_generation_data_overflow(struct hashfile *f, | |
eb907191 | 1230 | void *data) |
e8b63005 | 1231 | { |
eb907191 | 1232 | struct write_commit_graph_context *ctx = data; |
e8b63005 AK |
1233 | int i; |
1234 | for (i = 0; i < ctx->commits.nr; i++) { | |
1235 | struct commit *c = ctx->commits.list[i]; | |
1236 | timestamp_t offset = commit_graph_data_at(c)->generation - c->date; | |
1237 | display_progress(ctx->progress, ++ctx->progress_cnt); | |
1238 | ||
1239 | if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) { | |
1240 | hashwrite_be32(f, offset >> 32); | |
1241 | hashwrite_be32(f, (uint32_t) offset); | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | return 0; | |
1246 | } | |
1247 | ||
9bab081d | 1248 | static int write_graph_chunk_extra_edges(struct hashfile *f, |
eb907191 | 1249 | void *data) |
08fd81c9 | 1250 | { |
eb907191 | 1251 | struct write_commit_graph_context *ctx = data; |
c9905bea DS |
1252 | struct commit **list = ctx->commits.list; |
1253 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
1254 | struct commit_list *parent; |
1255 | ||
1256 | while (list < last) { | |
1257 | int num_parents = 0; | |
53035c4f | 1258 | |
c9905bea | 1259 | display_progress(ctx->progress, ++ctx->progress_cnt); |
53035c4f | 1260 | |
08fd81c9 DS |
1261 | for (parent = (*list)->parents; num_parents < 3 && parent; |
1262 | parent = parent->next) | |
1263 | num_parents++; | |
1264 | ||
1265 | if (num_parents <= 2) { | |
1266 | list++; | |
1267 | continue; | |
1268 | } | |
1269 | ||
1270 | /* Since num_parents > 2, this initializer is safe. */ | |
1271 | for (parent = (*list)->parents->next; parent; parent = parent->next) { | |
45ee13b9 JK |
1272 | int edge_value = oid_pos(&parent->item->object.oid, |
1273 | ctx->commits.list, | |
1274 | ctx->commits.nr, | |
1275 | commit_to_oid); | |
08fd81c9 | 1276 | |
6c622f9f DS |
1277 | if (edge_value >= 0) |
1278 | edge_value += ctx->new_num_commits_in_base; | |
8a6ac287 | 1279 | else if (ctx->new_base_graph) { |
6c622f9f | 1280 | uint32_t pos; |
809ea28f PS |
1281 | if (find_commit_pos_in_graph(parent->item, |
1282 | ctx->new_base_graph, | |
1283 | &pos)) | |
6c622f9f DS |
1284 | edge_value = pos; |
1285 | } | |
1286 | ||
08fd81c9 | 1287 | if (edge_value < 0) |
cce99cd8 DS |
1288 | BUG("missing parent %s for commit %s", |
1289 | oid_to_hex(&parent->item->object.oid), | |
1290 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
1291 | else if (!parent->next) |
1292 | edge_value |= GRAPH_LAST_EDGE; | |
1293 | ||
1294 | hashwrite_be32(f, edge_value); | |
1295 | } | |
1296 | ||
1297 | list++; | |
1298 | } | |
9bab081d SG |
1299 | |
1300 | return 0; | |
08fd81c9 DS |
1301 | } |
1302 | ||
9bab081d | 1303 | static int write_graph_chunk_bloom_indexes(struct hashfile *f, |
eb907191 | 1304 | void *data) |
76ffbca7 | 1305 | { |
eb907191 | 1306 | struct write_commit_graph_context *ctx = data; |
76ffbca7 GS |
1307 | struct commit **list = ctx->commits.list; |
1308 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
1309 | uint32_t cur_pos = 0; | |
76ffbca7 GS |
1310 | |
1311 | while (list < last) { | |
312cff52 | 1312 | struct bloom_filter *filter = get_bloom_filter(ctx->r, *list); |
94919742 DS |
1313 | size_t len = filter ? filter->len : 0; |
1314 | cur_pos += len; | |
150cd3b6 | 1315 | display_progress(ctx->progress, ++ctx->progress_cnt); |
76ffbca7 GS |
1316 | hashwrite_be32(f, cur_pos); |
1317 | list++; | |
1318 | } | |
1319 | ||
9bab081d | 1320 | return 0; |
76ffbca7 GS |
1321 | } |
1322 | ||
0087a87b DS |
1323 | static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx) |
1324 | { | |
1325 | struct json_writer jw = JSON_WRITER_INIT; | |
1326 | ||
1327 | jw_object_begin(&jw, 0); | |
1328 | jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version); | |
1329 | jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes); | |
1330 | jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry); | |
97ffa4fa | 1331 | jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths); |
0087a87b DS |
1332 | jw_end(&jw); |
1333 | ||
1334 | trace2_data_json("bloom", ctx->r, "settings", &jw); | |
1335 | ||
1336 | jw_release(&jw); | |
76ffbca7 GS |
1337 | } |
1338 | ||
9bab081d | 1339 | static int write_graph_chunk_bloom_data(struct hashfile *f, |
eb907191 | 1340 | void *data) |
76ffbca7 | 1341 | { |
eb907191 | 1342 | struct write_commit_graph_context *ctx = data; |
76ffbca7 GS |
1343 | struct commit **list = ctx->commits.list; |
1344 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
76ffbca7 | 1345 | |
0087a87b DS |
1346 | trace2_bloom_filter_settings(ctx); |
1347 | ||
98037f2b DS |
1348 | hashwrite_be32(f, ctx->bloom_settings->hash_version); |
1349 | hashwrite_be32(f, ctx->bloom_settings->num_hashes); | |
1350 | hashwrite_be32(f, ctx->bloom_settings->bits_per_entry); | |
76ffbca7 GS |
1351 | |
1352 | while (list < last) { | |
312cff52 | 1353 | struct bloom_filter *filter = get_bloom_filter(ctx->r, *list); |
94919742 | 1354 | size_t len = filter ? filter->len : 0; |
94919742 | 1355 | |
150cd3b6 | 1356 | display_progress(ctx->progress, ++ctx->progress_cnt); |
94919742 DS |
1357 | if (len) |
1358 | hashwrite(f, filter->data, len * sizeof(unsigned char)); | |
76ffbca7 GS |
1359 | list++; |
1360 | } | |
1361 | ||
9bab081d | 1362 | return 0; |
76ffbca7 GS |
1363 | } |
1364 | ||
08fd81c9 DS |
1365 | static int add_packed_commits(const struct object_id *oid, |
1366 | struct packed_git *pack, | |
1367 | uint32_t pos, | |
1368 | void *data) | |
1369 | { | |
c9905bea | 1370 | struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data; |
08fd81c9 DS |
1371 | enum object_type type; |
1372 | off_t offset = nth_packed_object_offset(pack, pos); | |
1373 | struct object_info oi = OBJECT_INFO_INIT; | |
1374 | ||
c9905bea DS |
1375 | if (ctx->progress) |
1376 | display_progress(ctx->progress, ++ctx->progress_done); | |
7b0f2292 | 1377 | |
08fd81c9 | 1378 | oi.typep = &type; |
c9905bea | 1379 | if (packed_object_info(ctx->r, pack, offset, &oi) < 0) |
4f5b532d | 1380 | die(_("unable to get type of object %s"), oid_to_hex(oid)); |
08fd81c9 DS |
1381 | |
1382 | if (type != OBJ_COMMIT) | |
1383 | return 0; | |
1384 | ||
a5f1c448 | 1385 | oid_array_append(&ctx->oids, oid); |
d21ee7d1 JK |
1386 | set_commit_pos(ctx->r, oid); |
1387 | ||
08fd81c9 DS |
1388 | return 0; |
1389 | } | |
1390 | ||
c9905bea | 1391 | static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit) |
4f2542b4 DS |
1392 | { |
1393 | struct commit_list *parent; | |
1394 | for (parent = commit->parents; parent; parent = parent->next) { | |
cb99a34e | 1395 | if (!(parent->item->object.flags & REACHABLE)) { |
a5f1c448 | 1396 | oid_array_append(&ctx->oids, &parent->item->object.oid); |
cb99a34e | 1397 | parent->item->object.flags |= REACHABLE; |
4f2542b4 DS |
1398 | } |
1399 | } | |
1400 | } | |
1401 | ||
c9905bea | 1402 | static void close_reachable(struct write_commit_graph_context *ctx) |
4f2542b4 | 1403 | { |
49bbc57a | 1404 | int i; |
4f2542b4 | 1405 | struct commit *commit; |
98bb7961 TB |
1406 | enum commit_graph_split_flags flags = ctx->opts ? |
1407 | ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; | |
4f2542b4 | 1408 | |
c9905bea DS |
1409 | if (ctx->report_progress) |
1410 | ctx->progress = start_delayed_progress( | |
1411 | _("Loading known commits in commit graph"), | |
1412 | ctx->oids.nr); | |
1413 | for (i = 0; i < ctx->oids.nr; i++) { | |
1414 | display_progress(ctx->progress, i + 1); | |
a5f1c448 | 1415 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
4f2542b4 | 1416 | if (commit) |
cb99a34e | 1417 | commit->object.flags |= REACHABLE; |
4f2542b4 | 1418 | } |
c9905bea | 1419 | stop_progress(&ctx->progress); |
4f2542b4 DS |
1420 | |
1421 | /* | |
c9905bea | 1422 | * As this loop runs, ctx->oids.nr may grow, but not more |
4f2542b4 DS |
1423 | * than the number of missing commits in the reachable |
1424 | * closure. | |
1425 | */ | |
c9905bea DS |
1426 | if (ctx->report_progress) |
1427 | ctx->progress = start_delayed_progress( | |
1428 | _("Expanding reachable commits in commit graph"), | |
67fa6aac | 1429 | 0); |
c9905bea DS |
1430 | for (i = 0; i < ctx->oids.nr; i++) { |
1431 | display_progress(ctx->progress, i + 1); | |
a5f1c448 | 1432 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
4f2542b4 | 1433 | |
6c622f9f DS |
1434 | if (!commit) |
1435 | continue; | |
1436 | if (ctx->split) { | |
c4cc0831 | 1437 | if ((!repo_parse_commit(ctx->r, commit) && |
c49c82aa | 1438 | commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) || |
8a6ac287 | 1439 | flags == COMMIT_GRAPH_SPLIT_REPLACE) |
6c622f9f | 1440 | add_missing_parents(ctx, commit); |
c4cc0831 | 1441 | } else if (!repo_parse_commit_no_graph(ctx->r, commit)) |
c9905bea | 1442 | add_missing_parents(ctx, commit); |
4f2542b4 | 1443 | } |
c9905bea | 1444 | stop_progress(&ctx->progress); |
4f2542b4 | 1445 | |
c9905bea DS |
1446 | if (ctx->report_progress) |
1447 | ctx->progress = start_delayed_progress( | |
1448 | _("Clearing commit marks in commit graph"), | |
1449 | ctx->oids.nr); | |
1450 | for (i = 0; i < ctx->oids.nr; i++) { | |
1451 | display_progress(ctx->progress, i + 1); | |
a5f1c448 | 1452 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
4f2542b4 DS |
1453 | |
1454 | if (commit) | |
cb99a34e | 1455 | commit->object.flags &= ~REACHABLE; |
4f2542b4 | 1456 | } |
c9905bea | 1457 | stop_progress(&ctx->progress); |
4f2542b4 DS |
1458 | } |
1459 | ||
368d19b0 DS |
1460 | struct compute_generation_info { |
1461 | struct repository *r; | |
1462 | struct packed_commit_list *commits; | |
1463 | struct progress *progress; | |
1464 | int progress_cnt; | |
1465 | ||
1466 | timestamp_t (*get_generation)(struct commit *c, void *data); | |
1467 | void (*set_generation)(struct commit *c, timestamp_t gen, void *data); | |
1468 | void *data; | |
1469 | }; | |
1470 | ||
1471 | static timestamp_t compute_generation_from_max(struct commit *c, | |
1472 | timestamp_t max_gen, | |
1473 | int generation_version) | |
1474 | { | |
1475 | switch (generation_version) { | |
1476 | case 1: /* topological levels */ | |
1477 | if (max_gen > GENERATION_NUMBER_V1_MAX - 1) | |
1478 | max_gen = GENERATION_NUMBER_V1_MAX - 1; | |
1479 | return max_gen + 1; | |
1480 | ||
1481 | case 2: /* corrected commit date */ | |
1482 | if (c->date && c->date > max_gen) | |
1483 | max_gen = c->date - 1; | |
1484 | return max_gen + 1; | |
1485 | ||
1486 | default: | |
1487 | BUG("attempting unimplemented version"); | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | static void compute_reachable_generation_numbers( | |
1492 | struct compute_generation_info *info, | |
1493 | int generation_version) | |
3258c663 DS |
1494 | { |
1495 | int i; | |
1496 | struct commit_list *list = NULL; | |
1497 | ||
368d19b0 DS |
1498 | for (i = 0; i < info->commits->nr; i++) { |
1499 | struct commit *c = info->commits->list[i]; | |
1500 | timestamp_t gen; | |
1501 | repo_parse_commit(info->r, c); | |
1502 | gen = info->get_generation(c, info->data); | |
1503 | display_progress(info->progress, info->progress_cnt + 1); | |
4844812b | 1504 | |
368d19b0 | 1505 | if (gen != GENERATION_NUMBER_ZERO && gen != GENERATION_NUMBER_INFINITY) |
3258c663 DS |
1506 | continue; |
1507 | ||
90cb1c47 | 1508 | commit_list_insert(c, &list); |
3258c663 DS |
1509 | while (list) { |
1510 | struct commit *current = list->item; | |
1511 | struct commit_list *parent; | |
1512 | int all_parents_computed = 1; | |
368d19b0 | 1513 | uint32_t max_gen = 0; |
3258c663 DS |
1514 | |
1515 | for (parent = current->parents; parent; parent = parent->next) { | |
368d19b0 DS |
1516 | repo_parse_commit(info->r, parent->item); |
1517 | gen = info->get_generation(parent->item, info->data); | |
4844812b | 1518 | |
368d19b0 | 1519 | if (gen == GENERATION_NUMBER_ZERO) { |
3258c663 DS |
1520 | all_parents_computed = 0; |
1521 | commit_list_insert(parent->item, &list); | |
1522 | break; | |
3258c663 | 1523 | } |
c1a09119 | 1524 | |
368d19b0 DS |
1525 | if (gen > max_gen) |
1526 | max_gen = gen; | |
3258c663 DS |
1527 | } |
1528 | ||
1529 | if (all_parents_computed) { | |
3258c663 | 1530 | pop_commit(&list); |
368d19b0 DS |
1531 | gen = compute_generation_from_max( |
1532 | current, max_gen, | |
1533 | generation_version); | |
1534 | info->set_generation(current, gen, info->data); | |
9c2c0a82 DS |
1535 | } |
1536 | } | |
1537 | } | |
368d19b0 DS |
1538 | } |
1539 | ||
1540 | static timestamp_t get_topo_level(struct commit *c, void *data) | |
1541 | { | |
1542 | struct write_commit_graph_context *ctx = data; | |
1543 | return *topo_level_slab_at(ctx->topo_levels, c); | |
1544 | } | |
1545 | ||
1546 | static void set_topo_level(struct commit *c, timestamp_t t, void *data) | |
1547 | { | |
1548 | struct write_commit_graph_context *ctx = data; | |
1549 | *topo_level_slab_at(ctx->topo_levels, c) = (uint32_t)t; | |
1550 | } | |
1551 | ||
1552 | static void compute_topological_levels(struct write_commit_graph_context *ctx) | |
1553 | { | |
1554 | struct compute_generation_info info = { | |
1555 | .r = ctx->r, | |
1556 | .commits = &ctx->commits, | |
1557 | .get_generation = get_topo_level, | |
1558 | .set_generation = set_topo_level, | |
1559 | .data = ctx, | |
1560 | }; | |
1561 | ||
1562 | if (ctx->report_progress) | |
1563 | info.progress = ctx->progress | |
1564 | = start_delayed_progress( | |
1565 | _("Computing commit graph topological levels"), | |
1566 | ctx->commits.nr); | |
1567 | ||
1568 | compute_reachable_generation_numbers(&info, 1); | |
1569 | ||
9c2c0a82 DS |
1570 | stop_progress(&ctx->progress); |
1571 | } | |
1572 | ||
80c928d9 DS |
1573 | static timestamp_t get_generation_from_graph_data(struct commit *c, void *data) |
1574 | { | |
1575 | return commit_graph_data_at(c)->generation; | |
1576 | } | |
1577 | ||
1578 | static void set_generation_v2(struct commit *c, timestamp_t t, void *data) | |
1579 | { | |
1580 | struct commit_graph_data *g = commit_graph_data_at(c); | |
d3af1c19 | 1581 | g->generation = t; |
80c928d9 DS |
1582 | } |
1583 | ||
9c2c0a82 DS |
1584 | static void compute_generation_numbers(struct write_commit_graph_context *ctx) |
1585 | { | |
1586 | int i; | |
80c928d9 DS |
1587 | struct compute_generation_info info = { |
1588 | .r = ctx->r, | |
1589 | .commits = &ctx->commits, | |
1590 | .get_generation = get_generation_from_graph_data, | |
1591 | .set_generation = set_generation_v2, | |
1592 | .data = ctx, | |
1593 | }; | |
9c2c0a82 DS |
1594 | |
1595 | if (ctx->report_progress) | |
80c928d9 DS |
1596 | info.progress = ctx->progress |
1597 | = start_delayed_progress( | |
9c2c0a82 DS |
1598 | _("Computing commit graph generation numbers"), |
1599 | ctx->commits.nr); | |
fde55b09 DS |
1600 | |
1601 | if (!ctx->trust_generation_numbers) { | |
1602 | for (i = 0; i < ctx->commits.nr; i++) { | |
1603 | struct commit *c = ctx->commits.list[i]; | |
1604 | repo_parse_commit(ctx->r, c); | |
1605 | commit_graph_data_at(c)->generation = GENERATION_NUMBER_ZERO; | |
1606 | } | |
1607 | } | |
1608 | ||
80c928d9 | 1609 | compute_reachable_generation_numbers(&info, 2); |
75979d94 DS |
1610 | |
1611 | for (i = 0; i < ctx->commits.nr; i++) { | |
1612 | struct commit *c = ctx->commits.list[i]; | |
1613 | timestamp_t offset = commit_graph_data_at(c)->generation - c->date; | |
1614 | if (offset > GENERATION_NUMBER_V2_OFFSET_MAX) | |
1615 | ctx->num_generation_data_overflows++; | |
1616 | } | |
c9905bea | 1617 | stop_progress(&ctx->progress); |
3258c663 DS |
1618 | } |
1619 | ||
c08645b3 TB |
1620 | static void set_generation_in_graph_data(struct commit *c, timestamp_t t, |
1621 | void *data) | |
1622 | { | |
1623 | commit_graph_data_at(c)->generation = t; | |
1624 | } | |
1625 | ||
1626 | /* | |
1627 | * After this method, all commits reachable from those in the given | |
1628 | * list will have non-zero, non-infinite generation numbers. | |
1629 | */ | |
1630 | void ensure_generations_valid(struct repository *r, | |
1631 | struct commit **commits, size_t nr) | |
1632 | { | |
1633 | int generation_version = get_configured_generation_version(r); | |
1634 | struct packed_commit_list list = { | |
1635 | .list = commits, | |
1636 | .alloc = nr, | |
1637 | .nr = nr, | |
1638 | }; | |
1639 | struct compute_generation_info info = { | |
1640 | .r = r, | |
1641 | .commits = &list, | |
1642 | .get_generation = get_generation_from_graph_data, | |
1643 | .set_generation = set_generation_in_graph_data, | |
1644 | }; | |
1645 | ||
1646 | compute_reachable_generation_numbers(&info, generation_version); | |
1647 | } | |
1648 | ||
312cff52 TB |
1649 | static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx) |
1650 | { | |
1651 | trace2_data_intmax("commit-graph", ctx->r, "filter-computed", | |
1652 | ctx->count_bloom_filter_computed); | |
1653 | trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed", | |
1654 | ctx->count_bloom_filter_not_computed); | |
59f0d507 TB |
1655 | trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty", |
1656 | ctx->count_bloom_filter_trunc_empty); | |
312cff52 TB |
1657 | trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large", |
1658 | ctx->count_bloom_filter_trunc_large); | |
1659 | } | |
1660 | ||
f97b9325 GS |
1661 | static void compute_bloom_filters(struct write_commit_graph_context *ctx) |
1662 | { | |
1663 | int i; | |
1664 | struct progress *progress = NULL; | |
d21ee7d1 | 1665 | struct commit **sorted_commits; |
809e0327 | 1666 | int max_new_filters; |
f97b9325 GS |
1667 | |
1668 | init_bloom_filters(); | |
1669 | ||
1670 | if (ctx->report_progress) | |
1671 | progress = start_delayed_progress( | |
1672 | _("Computing commit changed paths Bloom filters"), | |
1673 | ctx->commits.nr); | |
1674 | ||
6e578410 | 1675 | DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr); |
3d112755 GS |
1676 | |
1677 | if (ctx->order_by_pack) | |
1678 | QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp); | |
1679 | else | |
1680 | QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp); | |
d21ee7d1 | 1681 | |
809e0327 TB |
1682 | max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ? |
1683 | ctx->opts->max_new_filters : ctx->commits.nr; | |
1684 | ||
f97b9325 | 1685 | for (i = 0; i < ctx->commits.nr; i++) { |
312cff52 | 1686 | enum bloom_filter_computed computed = 0; |
d21ee7d1 | 1687 | struct commit *c = sorted_commits[i]; |
312cff52 TB |
1688 | struct bloom_filter *filter = get_or_compute_bloom_filter( |
1689 | ctx->r, | |
1690 | c, | |
809e0327 | 1691 | ctx->count_bloom_filter_computed < max_new_filters, |
9a7a9ed1 | 1692 | ctx->bloom_settings, |
312cff52 TB |
1693 | &computed); |
1694 | if (computed & BLOOM_COMPUTED) { | |
1695 | ctx->count_bloom_filter_computed++; | |
59f0d507 TB |
1696 | if (computed & BLOOM_TRUNC_EMPTY) |
1697 | ctx->count_bloom_filter_trunc_empty++; | |
312cff52 TB |
1698 | if (computed & BLOOM_TRUNC_LARGE) |
1699 | ctx->count_bloom_filter_trunc_large++; | |
1700 | } else if (computed & BLOOM_NOT_COMPUTED) | |
1701 | ctx->count_bloom_filter_not_computed++; | |
809e0327 TB |
1702 | ctx->total_bloom_filter_data_size += filter |
1703 | ? sizeof(unsigned char) * filter->len : 0; | |
f97b9325 GS |
1704 | display_progress(progress, i + 1); |
1705 | } | |
1706 | ||
312cff52 TB |
1707 | if (trace2_is_enabled()) |
1708 | trace2_bloom_filter_write_statistics(ctx); | |
1709 | ||
d21ee7d1 | 1710 | free(sorted_commits); |
f97b9325 GS |
1711 | stop_progress(&progress); |
1712 | } | |
1713 | ||
1fe10844 TB |
1714 | struct refs_cb_data { |
1715 | struct oidset *commits; | |
d335ce8f | 1716 | struct progress *progress; |
1fe10844 TB |
1717 | }; |
1718 | ||
5cf88fd8 | 1719 | static int add_ref_to_set(const char *refname UNUSED, |
6830c360 | 1720 | const struct object_id *oid, |
5cf88fd8 | 1721 | int flags UNUSED, void *cb_data) |
59fb8770 | 1722 | { |
630cd519 | 1723 | struct object_id peeled; |
1fe10844 | 1724 | struct refs_cb_data *data = (struct refs_cb_data *)cb_data; |
59fb8770 | 1725 | |
36a31792 | 1726 | if (!peel_iterated_oid(oid, &peeled)) |
630cd519 TB |
1727 | oid = &peeled; |
1728 | if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT) | |
1729 | oidset_insert(data->commits, oid); | |
d335ce8f TB |
1730 | |
1731 | display_progress(data->progress, oidset_size(data->commits)); | |
59fb8770 | 1732 | |
59fb8770 DS |
1733 | return 0; |
1734 | } | |
1735 | ||
0bd52e27 | 1736 | int write_commit_graph_reachable(struct object_directory *odb, |
39d88318 | 1737 | enum commit_graph_write_flags flags, |
98bb7961 | 1738 | const struct commit_graph_opts *opts) |
59fb8770 | 1739 | { |
6830c360 | 1740 | struct oidset commits = OIDSET_INIT; |
1fe10844 | 1741 | struct refs_cb_data data; |
e103f727 | 1742 | int result; |
59fb8770 | 1743 | |
1fe10844 TB |
1744 | memset(&data, 0, sizeof(data)); |
1745 | data.commits = &commits; | |
d335ce8f TB |
1746 | if (flags & COMMIT_GRAPH_WRITE_PROGRESS) |
1747 | data.progress = start_delayed_progress( | |
1748 | _("Collecting referenced commits"), 0); | |
1fe10844 TB |
1749 | |
1750 | for_each_ref(add_ref_to_set, &data); | |
6f9d5f2f SG |
1751 | |
1752 | stop_progress(&data.progress); | |
1753 | ||
6830c360 | 1754 | result = write_commit_graph(odb, NULL, &commits, |
98bb7961 | 1755 | flags, opts); |
f4dbdfc4 | 1756 | |
6830c360 | 1757 | oidset_clear(&commits); |
e103f727 | 1758 | return result; |
59fb8770 DS |
1759 | } |
1760 | ||
ef5b83f2 | 1761 | static int fill_oids_from_packs(struct write_commit_graph_context *ctx, |
4a047908 | 1762 | const struct string_list *pack_indexes) |
08fd81c9 | 1763 | { |
ef5b83f2 | 1764 | uint32_t i; |
28944739 | 1765 | struct strbuf progress_title = STRBUF_INIT; |
ef5b83f2 DS |
1766 | struct strbuf packname = STRBUF_INIT; |
1767 | int dirlen; | |
51a94d8f | 1768 | int ret = 0; |
08fd81c9 | 1769 | |
0bd52e27 | 1770 | strbuf_addf(&packname, "%s/pack/", ctx->odb->path); |
ef5b83f2 DS |
1771 | dirlen = packname.len; |
1772 | if (ctx->report_progress) { | |
1773 | strbuf_addf(&progress_title, | |
99d60545 ÆAB |
1774 | Q_("Finding commits for commit graph in %"PRIuMAX" pack", |
1775 | "Finding commits for commit graph in %"PRIuMAX" packs", | |
ef5b83f2 | 1776 | pack_indexes->nr), |
99d60545 | 1777 | (uintmax_t)pack_indexes->nr); |
ef5b83f2 DS |
1778 | ctx->progress = start_delayed_progress(progress_title.buf, 0); |
1779 | ctx->progress_done = 0; | |
7547b95b | 1780 | } |
ef5b83f2 DS |
1781 | for (i = 0; i < pack_indexes->nr; i++) { |
1782 | struct packed_git *p; | |
1783 | strbuf_setlen(&packname, dirlen); | |
1784 | strbuf_addstr(&packname, pack_indexes->items[i].string); | |
1785 | p = add_packed_git(packname.buf, packname.len, 1); | |
1786 | if (!p) { | |
51a94d8f ÆAB |
1787 | ret = error(_("error adding pack %s"), packname.buf); |
1788 | goto cleanup; | |
7547b95b | 1789 | } |
ef5b83f2 | 1790 | if (open_pack_index(p)) { |
51a94d8f ÆAB |
1791 | ret = error(_("error opening index for %s"), packname.buf); |
1792 | goto cleanup; | |
ef5b83f2 DS |
1793 | } |
1794 | for_each_object_in_pack(p, add_packed_commits, ctx, | |
1795 | FOR_EACH_OBJECT_PACK_ORDER); | |
1796 | close_pack(p); | |
1797 | free(p); | |
7547b95b DS |
1798 | } |
1799 | ||
51a94d8f | 1800 | cleanup: |
ef5b83f2 | 1801 | stop_progress(&ctx->progress); |
0aa6bce7 | 1802 | strbuf_release(&progress_title); |
ef5b83f2 DS |
1803 | strbuf_release(&packname); |
1804 | ||
51a94d8f | 1805 | return ret; |
ef5b83f2 DS |
1806 | } |
1807 | ||
6830c360 TB |
1808 | static int fill_oids_from_commits(struct write_commit_graph_context *ctx, |
1809 | struct oidset *commits) | |
4c9efe85 | 1810 | { |
6830c360 TB |
1811 | struct oidset_iter iter; |
1812 | struct object_id *oid; | |
1813 | ||
1814 | if (!oidset_size(commits)) | |
1815 | return 0; | |
4c9efe85 | 1816 | |
6830c360 TB |
1817 | oidset_iter_init(commits, &iter); |
1818 | while ((oid = oidset_iter_next(&iter))) { | |
a5f1c448 | 1819 | oid_array_append(&ctx->oids, oid); |
3d5df01b | 1820 | } |
6830c360 | 1821 | |
7c5c9b9c | 1822 | return 0; |
4c9efe85 | 1823 | } |
3d5df01b | 1824 | |
b2c83060 DS |
1825 | static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx) |
1826 | { | |
1827 | if (ctx->report_progress) | |
1828 | ctx->progress = start_delayed_progress( | |
1829 | _("Finding commits for commit graph among packed objects"), | |
1830 | ctx->approx_nr_objects); | |
1831 | for_each_packed_object(add_packed_commits, ctx, | |
1832 | FOR_EACH_OBJECT_PACK_ORDER); | |
1833 | if (ctx->progress_done < ctx->approx_nr_objects) | |
1834 | display_progress(ctx->progress, ctx->approx_nr_objects); | |
1835 | stop_progress(&ctx->progress); | |
1836 | } | |
049d51a2 | 1837 | |
f998d542 DS |
1838 | static void copy_oids_to_commits(struct write_commit_graph_context *ctx) |
1839 | { | |
1840 | uint32_t i; | |
98bb7961 TB |
1841 | enum commit_graph_split_flags flags = ctx->opts ? |
1842 | ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; | |
08fd81c9 | 1843 | |
f998d542 DS |
1844 | ctx->num_extra_edges = 0; |
1845 | if (ctx->report_progress) | |
1846 | ctx->progress = start_delayed_progress( | |
890226cc | 1847 | _("Finding extra edges in commit graph"), |
f998d542 | 1848 | ctx->oids.nr); |
a5f1c448 JK |
1849 | oid_array_sort(&ctx->oids); |
1850 | for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) { | |
689a146c RS |
1851 | unsigned int num_parents; |
1852 | ||
f998d542 | 1853 | display_progress(ctx->progress, i + 1); |
08fd81c9 | 1854 | |
6c622f9f | 1855 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc); |
a5f1c448 | 1856 | ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
6c622f9f | 1857 | |
8a6ac287 | 1858 | if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE && |
c49c82aa | 1859 | commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH) |
6c622f9f DS |
1860 | continue; |
1861 | ||
8a6ac287 | 1862 | if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE) |
c4cc0831 | 1863 | repo_parse_commit(ctx->r, ctx->commits.list[ctx->commits.nr]); |
8a6ac287 | 1864 | else |
c4cc0831 | 1865 | repo_parse_commit_no_graph(ctx->r, ctx->commits.list[ctx->commits.nr]); |
08fd81c9 | 1866 | |
689a146c | 1867 | num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents); |
08fd81c9 | 1868 | if (num_parents > 2) |
f998d542 | 1869 | ctx->num_extra_edges += num_parents - 1; |
08fd81c9 | 1870 | |
f998d542 | 1871 | ctx->commits.nr++; |
08fd81c9 | 1872 | } |
f998d542 DS |
1873 | stop_progress(&ctx->progress); |
1874 | } | |
08fd81c9 | 1875 | |
6c622f9f DS |
1876 | static int write_graph_chunk_base_1(struct hashfile *f, |
1877 | struct commit_graph *g) | |
1878 | { | |
1879 | int num = 0; | |
1880 | ||
1881 | if (!g) | |
1882 | return 0; | |
1883 | ||
1884 | num = write_graph_chunk_base_1(f, g->base_graph); | |
1885 | hashwrite(f, g->oid.hash, the_hash_algo->rawsz); | |
1886 | return num + 1; | |
1887 | } | |
1888 | ||
1889 | static int write_graph_chunk_base(struct hashfile *f, | |
eb907191 | 1890 | void *data) |
6c622f9f | 1891 | { |
eb907191 | 1892 | struct write_commit_graph_context *ctx = data; |
6c622f9f DS |
1893 | int num = write_graph_chunk_base_1(f, ctx->new_base_graph); |
1894 | ||
1895 | if (num != ctx->num_commit_graphs_after - 1) { | |
1896 | error(_("failed to write correct number of base graph ids")); | |
1897 | return -1; | |
1898 | } | |
1899 | ||
1900 | return 0; | |
1901 | } | |
1902 | ||
238def57 | 1903 | static int write_commit_graph_file(struct write_commit_graph_context *ctx) |
08fd81c9 | 1904 | { |
238def57 | 1905 | uint32_t i; |
6c622f9f | 1906 | int fd; |
08fd81c9 | 1907 | struct hashfile *f; |
08fd81c9 | 1908 | struct lock_file lk = LOCK_INIT; |
c1665998 | 1909 | const unsigned hashsz = the_hash_algo->rawsz; |
28944739 | 1910 | struct strbuf progress_title = STRBUF_INIT; |
47410aa8 | 1911 | struct chunkfile *cf; |
72871b13 | 1912 | unsigned char file_hash[GIT_MAX_RAWSZ]; |
6c622f9f DS |
1913 | |
1914 | if (ctx->split) { | |
1915 | struct strbuf tmp_file = STRBUF_INIT; | |
1916 | ||
1917 | strbuf_addf(&tmp_file, | |
1918 | "%s/info/commit-graphs/tmp_graph_XXXXXX", | |
0bd52e27 | 1919 | ctx->odb->path); |
6c622f9f DS |
1920 | ctx->graph_name = strbuf_detach(&tmp_file, NULL); |
1921 | } else { | |
ad2dd5bb | 1922 | ctx->graph_name = get_commit_graph_filename(ctx->odb); |
6c622f9f | 1923 | } |
238def57 | 1924 | |
238def57 DS |
1925 | if (safe_create_leading_directories(ctx->graph_name)) { |
1926 | UNLEAK(ctx->graph_name); | |
1927 | error(_("unable to create leading directories of %s"), | |
1928 | ctx->graph_name); | |
1929 | return -1; | |
f4dbdfc4 | 1930 | } |
08fd81c9 | 1931 | |
6c622f9f | 1932 | if (ctx->split) { |
663b2b1b | 1933 | char *lock_name = get_commit_graph_chain_filename(ctx->odb); |
08fd81c9 | 1934 | |
45a4365c TB |
1935 | hold_lock_file_for_update_mode(&lk, lock_name, |
1936 | LOCK_DIE_ON_ERROR, 0444); | |
ef3fe214 | 1937 | free(lock_name); |
08fd81c9 | 1938 | |
6c622f9f DS |
1939 | fd = git_mkstemp_mode(ctx->graph_name, 0444); |
1940 | if (fd < 0) { | |
a2d57e22 | 1941 | error(_("unable to create temporary graph layer")); |
6c622f9f DS |
1942 | return -1; |
1943 | } | |
1944 | ||
f4d62847 TB |
1945 | if (adjust_shared_perm(ctx->graph_name)) { |
1946 | error(_("unable to adjust shared permissions for '%s'"), | |
1947 | ctx->graph_name); | |
1948 | return -1; | |
1949 | } | |
1950 | ||
6c622f9f DS |
1951 | f = hashfd(fd, ctx->graph_name); |
1952 | } else { | |
1f9becae TB |
1953 | hold_lock_file_for_update_mode(&lk, ctx->graph_name, |
1954 | LOCK_DIE_ON_ERROR, 0444); | |
a52cdce9 MÅ |
1955 | fd = get_lock_file_fd(&lk); |
1956 | f = hashfd(fd, get_lock_file_path(&lk)); | |
6c622f9f | 1957 | } |
08fd81c9 | 1958 | |
47410aa8 DS |
1959 | cf = init_chunkfile(f); |
1960 | ||
1961 | add_chunk(cf, GRAPH_CHUNKID_OIDFANOUT, GRAPH_FANOUT_SIZE, | |
1962 | write_graph_chunk_fanout); | |
48f3f8cf | 1963 | add_chunk(cf, GRAPH_CHUNKID_OIDLOOKUP, st_mult(hashsz, ctx->commits.nr), |
47410aa8 | 1964 | write_graph_chunk_oids); |
48f3f8cf | 1965 | add_chunk(cf, GRAPH_CHUNKID_DATA, st_mult(hashsz + 16, ctx->commits.nr), |
47410aa8 | 1966 | write_graph_chunk_data); |
e8b63005 | 1967 | |
47410aa8 DS |
1968 | if (ctx->write_generation_data) |
1969 | add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA, | |
48f3f8cf | 1970 | st_mult(sizeof(uint32_t), ctx->commits.nr), |
47410aa8 DS |
1971 | write_graph_chunk_generation_data); |
1972 | if (ctx->num_generation_data_overflows) | |
1973 | add_chunk(cf, GRAPH_CHUNKID_GENERATION_DATA_OVERFLOW, | |
48f3f8cf | 1974 | st_mult(sizeof(timestamp_t), ctx->num_generation_data_overflows), |
47410aa8 DS |
1975 | write_graph_chunk_generation_data_overflow); |
1976 | if (ctx->num_extra_edges) | |
1977 | add_chunk(cf, GRAPH_CHUNKID_EXTRAEDGES, | |
48f3f8cf | 1978 | st_mult(4, ctx->num_extra_edges), |
47410aa8 | 1979 | write_graph_chunk_extra_edges); |
76ffbca7 | 1980 | if (ctx->changed_paths) { |
47410aa8 | 1981 | add_chunk(cf, GRAPH_CHUNKID_BLOOMINDEXES, |
48f3f8cf | 1982 | st_mult(sizeof(uint32_t), ctx->commits.nr), |
47410aa8 DS |
1983 | write_graph_chunk_bloom_indexes); |
1984 | add_chunk(cf, GRAPH_CHUNKID_BLOOMDATA, | |
48f3f8cf TB |
1985 | st_add(sizeof(uint32_t) * 3, |
1986 | ctx->total_bloom_filter_data_size), | |
47410aa8 | 1987 | write_graph_chunk_bloom_data); |
76ffbca7 | 1988 | } |
47410aa8 DS |
1989 | if (ctx->num_commit_graphs_after > 1) |
1990 | add_chunk(cf, GRAPH_CHUNKID_BASE, | |
48f3f8cf | 1991 | st_mult(hashsz, ctx->num_commit_graphs_after - 1), |
47410aa8 | 1992 | write_graph_chunk_base); |
144354b0 DS |
1993 | |
1994 | hashwrite_be32(f, GRAPH_SIGNATURE); | |
1995 | ||
1996 | hashwrite_u8(f, GRAPH_VERSION); | |
d9fef9d9 | 1997 | hashwrite_u8(f, oid_version(the_hash_algo)); |
47410aa8 | 1998 | hashwrite_u8(f, get_num_chunks(cf)); |
6c622f9f | 1999 | hashwrite_u8(f, ctx->num_commit_graphs_after - 1); |
08fd81c9 | 2000 | |
238def57 | 2001 | if (ctx->report_progress) { |
28944739 ÆAB |
2002 | strbuf_addf(&progress_title, |
2003 | Q_("Writing out commit graph in %d pass", | |
2004 | "Writing out commit graph in %d passes", | |
c4ff24bb TB |
2005 | get_num_chunks(cf)), |
2006 | get_num_chunks(cf)); | |
238def57 | 2007 | ctx->progress = start_delayed_progress( |
28944739 | 2008 | progress_title.buf, |
48f3f8cf | 2009 | st_mult(get_num_chunks(cf), ctx->commits.nr)); |
28944739 | 2010 | } |
17e6275f | 2011 | |
47410aa8 | 2012 | write_chunkfile(cf, ctx); |
17e6275f | 2013 | |
238def57 | 2014 | stop_progress(&ctx->progress); |
28944739 | 2015 | strbuf_release(&progress_title); |
08fd81c9 | 2016 | |
6c622f9f DS |
2017 | if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) { |
2018 | char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid)); | |
ad2dd5bb | 2019 | char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash); |
6c622f9f DS |
2020 | |
2021 | free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]); | |
2022 | free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]); | |
2023 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name; | |
2024 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash; | |
2025 | } | |
2026 | ||
c3a3a964 | 2027 | close_commit_graph(ctx->r->objects); |
020406ea NS |
2028 | finalize_hashfile(f, file_hash, FSYNC_COMPONENT_COMMIT_GRAPH, |
2029 | CSUM_HASH_IN_STREAM | CSUM_FSYNC); | |
47410aa8 | 2030 | free_chunkfile(cf); |
6c622f9f DS |
2031 | |
2032 | if (ctx->split) { | |
2033 | FILE *chainf = fdopen_lock_file(&lk, "w"); | |
2034 | char *final_graph_name; | |
2035 | int result; | |
2036 | ||
2037 | close(fd); | |
2038 | ||
2039 | if (!chainf) { | |
2040 | error(_("unable to open commit-graph chain file")); | |
2041 | return -1; | |
2042 | } | |
2043 | ||
2044 | if (ctx->base_graph_name) { | |
8a6ac287 TB |
2045 | const char *dest; |
2046 | int idx = ctx->num_commit_graphs_after - 1; | |
2047 | if (ctx->num_commit_graphs_after > 1) | |
2048 | idx--; | |
2049 | ||
2050 | dest = ctx->commit_graph_filenames_after[idx]; | |
6c622f9f | 2051 | |
135a7123 DS |
2052 | if (strcmp(ctx->base_graph_name, dest)) { |
2053 | result = rename(ctx->base_graph_name, dest); | |
2054 | ||
2055 | if (result) { | |
2056 | error(_("failed to rename base commit-graph file")); | |
2057 | return -1; | |
2058 | } | |
6c622f9f DS |
2059 | } |
2060 | } else { | |
ad2dd5bb | 2061 | char *graph_name = get_commit_graph_filename(ctx->odb); |
6c622f9f | 2062 | unlink(graph_name); |
ef3fe214 | 2063 | free(graph_name); |
6c622f9f DS |
2064 | } |
2065 | ||
72871b13 | 2066 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(hash_to_hex(file_hash)); |
ad2dd5bb | 2067 | final_graph_name = get_split_graph_filename(ctx->odb, |
6c622f9f DS |
2068 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]); |
2069 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name; | |
2070 | ||
2071 | result = rename(ctx->graph_name, final_graph_name); | |
2072 | ||
2073 | for (i = 0; i < ctx->num_commit_graphs_after; i++) | |
a52cdce9 | 2074 | fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]); |
6c622f9f DS |
2075 | |
2076 | if (result) { | |
2077 | error(_("failed to rename temporary commit-graph file")); | |
2078 | return -1; | |
2079 | } | |
2080 | } | |
2081 | ||
08fd81c9 DS |
2082 | commit_lock_file(&lk); |
2083 | ||
238def57 DS |
2084 | return 0; |
2085 | } | |
2086 | ||
1771be90 DS |
2087 | static void split_graph_merge_strategy(struct write_commit_graph_context *ctx) |
2088 | { | |
8da02ce6 AH |
2089 | struct commit_graph *g; |
2090 | uint32_t num_commits; | |
fdbde82f | 2091 | enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED; |
1771be90 DS |
2092 | uint32_t i; |
2093 | ||
c2bc6e6a DS |
2094 | int max_commits = 0; |
2095 | int size_mult = 2; | |
2096 | ||
98bb7961 TB |
2097 | if (ctx->opts) { |
2098 | max_commits = ctx->opts->max_commits; | |
63020f17 | 2099 | |
98bb7961 TB |
2100 | if (ctx->opts->size_multiple) |
2101 | size_mult = ctx->opts->size_multiple; | |
fdbde82f | 2102 | |
98bb7961 | 2103 | flags = ctx->opts->split_flags; |
c2bc6e6a DS |
2104 | } |
2105 | ||
1771be90 | 2106 | g = ctx->r->objects->commit_graph; |
8da02ce6 | 2107 | num_commits = ctx->commits.nr; |
8a6ac287 TB |
2108 | if (flags == COMMIT_GRAPH_SPLIT_REPLACE) |
2109 | ctx->num_commit_graphs_after = 1; | |
2110 | else | |
2111 | ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1; | |
1771be90 | 2112 | |
8a6ac287 TB |
2113 | if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED && |
2114 | flags != COMMIT_GRAPH_SPLIT_REPLACE) { | |
19565d09 | 2115 | while (g && (g->num_commits <= st_mult(size_mult, num_commits) || |
fdbde82f TB |
2116 | (max_commits && num_commits > max_commits))) { |
2117 | if (g->odb != ctx->odb) | |
2118 | break; | |
c523035c | 2119 | |
19565d09 TB |
2120 | if (unsigned_add_overflows(num_commits, g->num_commits)) |
2121 | die(_("cannot merge graphs with %"PRIuMAX", " | |
2122 | "%"PRIuMAX" commits"), | |
2123 | (uintmax_t)num_commits, | |
2124 | (uintmax_t)g->num_commits); | |
fdbde82f TB |
2125 | num_commits += g->num_commits; |
2126 | g = g->base_graph; | |
1771be90 | 2127 | |
fdbde82f TB |
2128 | ctx->num_commit_graphs_after--; |
2129 | } | |
1771be90 DS |
2130 | } |
2131 | ||
8a6ac287 TB |
2132 | if (flags != COMMIT_GRAPH_SPLIT_REPLACE) |
2133 | ctx->new_base_graph = g; | |
2134 | else if (ctx->num_commit_graphs_after != 1) | |
2135 | BUG("split_graph_merge_strategy: num_commit_graphs_after " | |
2136 | "should be 1 with --split=replace"); | |
1771be90 | 2137 | |
c523035c | 2138 | if (ctx->num_commit_graphs_after == 2) { |
ad2dd5bb | 2139 | char *old_graph_name = get_commit_graph_filename(g->odb); |
c523035c DS |
2140 | |
2141 | if (!strcmp(g->filename, old_graph_name) && | |
ad2dd5bb | 2142 | g->odb != ctx->odb) { |
c523035c DS |
2143 | ctx->num_commit_graphs_after = 1; |
2144 | ctx->new_base_graph = NULL; | |
2145 | } | |
2146 | ||
2147 | free(old_graph_name); | |
2148 | } | |
2149 | ||
b78a556a TB |
2150 | CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after); |
2151 | CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after); | |
1771be90 DS |
2152 | |
2153 | for (i = 0; i < ctx->num_commit_graphs_after && | |
2154 | i < ctx->num_commit_graphs_before; i++) | |
2155 | ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]); | |
2156 | ||
2157 | i = ctx->num_commit_graphs_before - 1; | |
2158 | g = ctx->r->objects->commit_graph; | |
2159 | ||
2160 | while (g) { | |
2161 | if (i < ctx->num_commit_graphs_after) | |
2162 | ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid)); | |
2163 | ||
1fdc383c AK |
2164 | /* |
2165 | * If the topmost remaining layer has generation data chunk, the | |
2166 | * resultant layer also has generation data chunk. | |
2167 | */ | |
2168 | if (i == ctx->num_commit_graphs_after - 2) | |
2169 | ctx->write_generation_data = !!g->chunk_generation_data; | |
2170 | ||
1771be90 DS |
2171 | i--; |
2172 | g = g->base_graph; | |
2173 | } | |
2174 | } | |
2175 | ||
2176 | static void merge_commit_graph(struct write_commit_graph_context *ctx, | |
2177 | struct commit_graph *g) | |
2178 | { | |
2179 | uint32_t i; | |
2180 | uint32_t offset = g->num_commits_in_base; | |
2181 | ||
d76e0a74 TB |
2182 | if (unsigned_add_overflows(ctx->commits.nr, g->num_commits)) |
2183 | die(_("cannot merge graph %s, too many commits: %"PRIuMAX), | |
2184 | oid_to_hex(&g->oid), | |
2185 | (uintmax_t)st_add(ctx->commits.nr, g->num_commits)); | |
2186 | ||
1771be90 DS |
2187 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc); |
2188 | ||
2189 | for (i = 0; i < g->num_commits; i++) { | |
2190 | struct object_id oid; | |
2191 | struct commit *result; | |
2192 | ||
2193 | display_progress(ctx->progress, i + 1); | |
2194 | ||
2195 | load_oid_from_graph(g, i + offset, &oid); | |
2196 | ||
2197 | /* only add commits if they still exist in the repo */ | |
2198 | result = lookup_commit_reference_gently(ctx->r, &oid, 1); | |
2199 | ||
2200 | if (result) { | |
2201 | ctx->commits.list[ctx->commits.nr] = result; | |
2202 | ctx->commits.nr++; | |
2203 | } | |
2204 | } | |
2205 | } | |
2206 | ||
2207 | static int commit_compare(const void *_a, const void *_b) | |
2208 | { | |
2209 | const struct commit *a = *(const struct commit **)_a; | |
2210 | const struct commit *b = *(const struct commit **)_b; | |
2211 | return oidcmp(&a->object.oid, &b->object.oid); | |
2212 | } | |
2213 | ||
2214 | static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx) | |
2215 | { | |
150f1157 | 2216 | uint32_t i, dedup_i = 0; |
1771be90 DS |
2217 | |
2218 | if (ctx->report_progress) | |
2219 | ctx->progress = start_delayed_progress( | |
2220 | _("Scanning merged commits"), | |
2221 | ctx->commits.nr); | |
2222 | ||
2223 | QSORT(ctx->commits.list, ctx->commits.nr, commit_compare); | |
2224 | ||
2225 | ctx->num_extra_edges = 0; | |
2226 | for (i = 0; i < ctx->commits.nr; i++) { | |
40112249 | 2227 | display_progress(ctx->progress, i + 1); |
1771be90 DS |
2228 | |
2229 | if (i && oideq(&ctx->commits.list[i - 1]->object.oid, | |
2230 | &ctx->commits.list[i]->object.oid)) { | |
150f1157 DS |
2231 | /* |
2232 | * Silently ignore duplicates. These were likely | |
2233 | * created due to a commit appearing in multiple | |
2234 | * layers of the chain, which is unexpected but | |
2235 | * not invalid. We should make sure there is a | |
2236 | * unique copy in the new layer. | |
2237 | */ | |
1771be90 | 2238 | } else { |
689a146c | 2239 | unsigned int num_parents; |
1771be90 | 2240 | |
150f1157 DS |
2241 | ctx->commits.list[dedup_i] = ctx->commits.list[i]; |
2242 | dedup_i++; | |
2243 | ||
689a146c | 2244 | num_parents = commit_list_count(ctx->commits.list[i]->parents); |
1771be90 | 2245 | if (num_parents > 2) |
a35bea40 | 2246 | ctx->num_extra_edges += num_parents - 1; |
1771be90 DS |
2247 | } |
2248 | } | |
2249 | ||
150f1157 DS |
2250 | ctx->commits.nr = dedup_i; |
2251 | ||
1771be90 DS |
2252 | stop_progress(&ctx->progress); |
2253 | } | |
2254 | ||
2255 | static void merge_commit_graphs(struct write_commit_graph_context *ctx) | |
2256 | { | |
2257 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
2258 | uint32_t current_graph_number = ctx->num_commit_graphs_before; | |
1771be90 DS |
2259 | |
2260 | while (g && current_graph_number >= ctx->num_commit_graphs_after) { | |
2261 | current_graph_number--; | |
2262 | ||
d68ce906 RS |
2263 | if (ctx->report_progress) |
2264 | ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0); | |
1771be90 DS |
2265 | |
2266 | merge_commit_graph(ctx, g); | |
2267 | stop_progress(&ctx->progress); | |
1771be90 DS |
2268 | |
2269 | g = g->base_graph; | |
2270 | } | |
2271 | ||
2272 | if (g) { | |
2273 | ctx->new_base_graph = g; | |
2274 | ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base; | |
2275 | } | |
2276 | ||
2277 | if (ctx->new_base_graph) | |
2278 | ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename); | |
2279 | ||
2280 | sort_and_scan_merged_commits(ctx); | |
2281 | } | |
2282 | ||
8d84097f DS |
2283 | static void mark_commit_graphs(struct write_commit_graph_context *ctx) |
2284 | { | |
2285 | uint32_t i; | |
2286 | time_t now = time(NULL); | |
2287 | ||
2288 | for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) { | |
2289 | struct stat st; | |
2290 | struct utimbuf updated_time; | |
2291 | ||
7c898554 ÆAB |
2292 | if (stat(ctx->commit_graph_filenames_before[i], &st) < 0) |
2293 | continue; | |
8d84097f DS |
2294 | |
2295 | updated_time.actime = st.st_atime; | |
2296 | updated_time.modtime = now; | |
2297 | utime(ctx->commit_graph_filenames_before[i], &updated_time); | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | static void expire_commit_graphs(struct write_commit_graph_context *ctx) | |
2302 | { | |
2303 | struct strbuf path = STRBUF_INIT; | |
2304 | DIR *dir; | |
2305 | struct dirent *de; | |
2306 | size_t dirnamelen; | |
c2bc6e6a DS |
2307 | timestamp_t expire_time = time(NULL); |
2308 | ||
98bb7961 TB |
2309 | if (ctx->opts && ctx->opts->expire_time) |
2310 | expire_time = ctx->opts->expire_time; | |
ba41112a | 2311 | if (!ctx->split) { |
663b2b1b | 2312 | char *chain_file_name = get_commit_graph_chain_filename(ctx->odb); |
ba41112a DS |
2313 | unlink(chain_file_name); |
2314 | free(chain_file_name); | |
2315 | ctx->num_commit_graphs_after = 0; | |
2316 | } | |
8d84097f | 2317 | |
0bd52e27 | 2318 | strbuf_addstr(&path, ctx->odb->path); |
8d84097f DS |
2319 | strbuf_addstr(&path, "/info/commit-graphs"); |
2320 | dir = opendir(path.buf); | |
2321 | ||
0aa6bce7 RS |
2322 | if (!dir) |
2323 | goto out; | |
8d84097f DS |
2324 | |
2325 | strbuf_addch(&path, '/'); | |
2326 | dirnamelen = path.len; | |
2327 | while ((de = readdir(dir)) != NULL) { | |
2328 | struct stat st; | |
2329 | uint32_t i, found = 0; | |
2330 | ||
2331 | strbuf_setlen(&path, dirnamelen); | |
2332 | strbuf_addstr(&path, de->d_name); | |
2333 | ||
7c898554 ÆAB |
2334 | if (stat(path.buf, &st) < 0) |
2335 | continue; | |
8d84097f DS |
2336 | |
2337 | if (st.st_mtime > expire_time) | |
2338 | continue; | |
2339 | if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph")) | |
2340 | continue; | |
2341 | ||
2342 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { | |
2343 | if (!strcmp(ctx->commit_graph_filenames_after[i], | |
2344 | path.buf)) { | |
2345 | found = 1; | |
2346 | break; | |
2347 | } | |
2348 | } | |
2349 | ||
2350 | if (!found) | |
2351 | unlink(path.buf); | |
8d84097f | 2352 | } |
0aa6bce7 RS |
2353 | |
2354 | out: | |
12f1ae53 ML |
2355 | if(dir) |
2356 | closedir(dir); | |
0aa6bce7 | 2357 | strbuf_release(&path); |
8d84097f DS |
2358 | } |
2359 | ||
0bd52e27 | 2360 | int write_commit_graph(struct object_directory *odb, |
4a047908 | 2361 | const struct string_list *const pack_indexes, |
6830c360 | 2362 | struct oidset *commits, |
39d88318 | 2363 | enum commit_graph_write_flags flags, |
98bb7961 | 2364 | const struct commit_graph_opts *opts) |
238def57 | 2365 | { |
c7ef8fe6 | 2366 | struct repository *r = the_repository; |
238def57 | 2367 | struct write_commit_graph_context *ctx; |
1cbdbf3b | 2368 | uint32_t i; |
e103f727 | 2369 | int res = 0; |
8a6ac287 | 2370 | int replace = 0; |
9a7a9ed1 | 2371 | struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS; |
72a2bfca | 2372 | struct topo_level_slab topo_levels; |
08fd81c9 | 2373 | |
c7ef8fe6 DS |
2374 | prepare_repo_settings(r); |
2375 | if (!r->settings.core_commit_graph) { | |
85102ac7 DS |
2376 | warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled")); |
2377 | return 0; | |
2378 | } | |
c7ef8fe6 | 2379 | if (!commit_graph_compatible(r)) |
e103f727 | 2380 | return 0; |
d6538246 | 2381 | |
ca56dadb | 2382 | CALLOC_ARRAY(ctx, 1); |
c7ef8fe6 | 2383 | ctx->r = r; |
0bd52e27 | 2384 | ctx->odb = odb; |
39d88318 SG |
2385 | ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0; |
2386 | ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0; | |
2387 | ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0; | |
98bb7961 | 2388 | ctx->opts = opts; |
f97b9325 | 2389 | ctx->total_bloom_filter_data_size = 0; |
702110aa | 2390 | ctx->write_generation_data = (get_configured_generation_version(r) == 2); |
e8b63005 | 2391 | ctx->num_generation_data_overflows = 0; |
6c622f9f | 2392 | |
9a7a9ed1 TB |
2393 | bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY", |
2394 | bloom_settings.bits_per_entry); | |
2395 | bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES", | |
2396 | bloom_settings.num_hashes); | |
2397 | bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS", | |
2398 | bloom_settings.max_changed_paths); | |
2399 | ctx->bloom_settings = &bloom_settings; | |
2400 | ||
72a2bfca AK |
2401 | init_topo_level_slab(&topo_levels); |
2402 | ctx->topo_levels = &topo_levels; | |
2403 | ||
bc50d6c9 | 2404 | prepare_commit_graph(ctx->r); |
72a2bfca AK |
2405 | if (ctx->r->objects->commit_graph) { |
2406 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
2407 | ||
2408 | while (g) { | |
2409 | g->topo_levels = &topo_levels; | |
2410 | g = g->base_graph; | |
2411 | } | |
2412 | } | |
2413 | ||
0087a87b DS |
2414 | if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS) |
2415 | ctx->changed_paths = 1; | |
2416 | if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) { | |
2417 | struct commit_graph *g; | |
0087a87b DS |
2418 | |
2419 | g = ctx->r->objects->commit_graph; | |
2420 | ||
2421 | /* We have changed-paths already. Keep them in the next graph */ | |
2422 | if (g && g->chunk_bloom_data) { | |
2423 | ctx->changed_paths = 1; | |
2424 | ctx->bloom_settings = g->bloom_filter_settings; | |
2425 | } | |
2426 | } | |
2427 | ||
6c622f9f | 2428 | if (ctx->split) { |
bc50d6c9 | 2429 | struct commit_graph *g = ctx->r->objects->commit_graph; |
6c622f9f DS |
2430 | |
2431 | while (g) { | |
2432 | ctx->num_commit_graphs_before++; | |
2433 | g = g->base_graph; | |
2434 | } | |
2435 | ||
2436 | if (ctx->num_commit_graphs_before) { | |
2437 | ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before); | |
2438 | i = ctx->num_commit_graphs_before; | |
2439 | g = ctx->r->objects->commit_graph; | |
2440 | ||
2441 | while (g) { | |
2442 | ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename); | |
2443 | g = g->base_graph; | |
2444 | } | |
2445 | } | |
8a6ac287 | 2446 | |
98bb7961 TB |
2447 | if (ctx->opts) |
2448 | replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE; | |
6c622f9f | 2449 | } |
c9905bea | 2450 | |
afe27c88 | 2451 | ctx->approx_nr_objects = repo_approximate_object_count(the_repository); |
c9905bea | 2452 | |
c9905bea DS |
2453 | if (ctx->append && ctx->r->objects->commit_graph) { |
2454 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
2455 | for (i = 0; i < g->num_commits; i++) { | |
a5f1c448 | 2456 | struct object_id oid; |
92e2cab9 | 2457 | oidread(&oid, g->chunk_oid_lookup + g->hash_len * i); |
a5f1c448 | 2458 | oid_array_append(&ctx->oids, &oid); |
7547b95b DS |
2459 | } |
2460 | } | |
2461 | ||
049d51a2 | 2462 | if (pack_indexes) { |
3d112755 | 2463 | ctx->order_by_pack = 1; |
ef5b83f2 DS |
2464 | if ((res = fill_oids_from_packs(ctx, pack_indexes))) |
2465 | goto cleanup; | |
3d5df01b DS |
2466 | } |
2467 | ||
6830c360 TB |
2468 | if (commits) { |
2469 | if ((res = fill_oids_from_commits(ctx, commits))) | |
7c5c9b9c SG |
2470 | goto cleanup; |
2471 | } | |
3d5df01b | 2472 | |
9b6606f4 | 2473 | if (!pack_indexes && !commits) { |
3d112755 | 2474 | ctx->order_by_pack = 1; |
b2c83060 | 2475 | fill_oids_from_all_packs(ctx); |
3d112755 | 2476 | } |
049d51a2 | 2477 | |
c9905bea | 2478 | close_reachable(ctx); |
08fd81c9 | 2479 | |
f998d542 | 2480 | copy_oids_to_commits(ctx); |
08fd81c9 | 2481 | |
c9905bea | 2482 | if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) { |
e103f727 DS |
2483 | error(_("too many commits to write graph")); |
2484 | res = -1; | |
2485 | goto cleanup; | |
2486 | } | |
08fd81c9 | 2487 | |
8a6ac287 | 2488 | if (!ctx->commits.nr && !replace) |
6c622f9f DS |
2489 | goto cleanup; |
2490 | ||
1771be90 DS |
2491 | if (ctx->split) { |
2492 | split_graph_merge_strategy(ctx); | |
2493 | ||
8a6ac287 TB |
2494 | if (!replace) |
2495 | merge_commit_graphs(ctx); | |
1771be90 | 2496 | } else |
6c622f9f DS |
2497 | ctx->num_commit_graphs_after = 1; |
2498 | ||
fde55b09 | 2499 | ctx->trust_generation_numbers = validate_mixed_generation_chain(ctx->r->objects->commit_graph); |
1fdc383c | 2500 | |
9c2c0a82 DS |
2501 | compute_topological_levels(ctx); |
2502 | if (ctx->write_generation_data) | |
2503 | compute_generation_numbers(ctx); | |
08fd81c9 | 2504 | |
f97b9325 GS |
2505 | if (ctx->changed_paths) |
2506 | compute_bloom_filters(ctx); | |
2507 | ||
238def57 | 2508 | res = write_commit_graph_file(ctx); |
08fd81c9 | 2509 | |
ba41112a | 2510 | if (ctx->split) |
8d84097f | 2511 | mark_commit_graphs(ctx); |
ba41112a DS |
2512 | |
2513 | expire_commit_graphs(ctx); | |
8d84097f | 2514 | |
e103f727 | 2515 | cleanup: |
238def57 | 2516 | free(ctx->graph_name); |
c9905bea | 2517 | free(ctx->commits.list); |
a5f1c448 | 2518 | oid_array_clear(&ctx->oids); |
bf4bb9f9 | 2519 | clear_topo_level_slab(&topo_levels); |
6c622f9f DS |
2520 | |
2521 | if (ctx->commit_graph_filenames_after) { | |
2522 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { | |
2523 | free(ctx->commit_graph_filenames_after[i]); | |
2524 | free(ctx->commit_graph_hash_after[i]); | |
2525 | } | |
2526 | ||
2527 | for (i = 0; i < ctx->num_commit_graphs_before; i++) | |
2528 | free(ctx->commit_graph_filenames_before[i]); | |
2529 | ||
2530 | free(ctx->commit_graph_filenames_after); | |
2531 | free(ctx->commit_graph_filenames_before); | |
2532 | free(ctx->commit_graph_hash_after); | |
2533 | } | |
2534 | ||
c9905bea | 2535 | free(ctx); |
e103f727 DS |
2536 | |
2537 | return res; | |
08fd81c9 | 2538 | } |
283e68c7 | 2539 | |
41df0e30 | 2540 | #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2 |
283e68c7 DS |
2541 | static int verify_commit_graph_error; |
2542 | ||
48ca53ca | 2543 | __attribute__((format (printf, 1, 2))) |
283e68c7 DS |
2544 | static void graph_report(const char *fmt, ...) |
2545 | { | |
2546 | va_list ap; | |
2547 | ||
2548 | verify_commit_graph_error = 1; | |
2549 | va_start(ap, fmt); | |
2550 | vfprintf(stderr, fmt, ap); | |
2551 | fprintf(stderr, "\n"); | |
2552 | va_end(ap); | |
2553 | } | |
2554 | ||
1373e547 DS |
2555 | #define GENERATION_ZERO_EXISTS 1 |
2556 | #define GENERATION_NUMBER_EXISTS 2 | |
2557 | ||
15316a47 TB |
2558 | static int commit_graph_checksum_valid(struct commit_graph *g) |
2559 | { | |
2560 | return hashfile_checksum_valid(g->data, g->data_len); | |
2561 | } | |
2562 | ||
3da4b609 | 2563 | int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags) |
283e68c7 | 2564 | { |
9bda8467 | 2565 | uint32_t i, cur_fanout_pos = 0; |
72871b13 | 2566 | struct object_id prev_oid, cur_oid; |
1373e547 | 2567 | int generation_zero = 0; |
1f7f557f | 2568 | struct progress *progress = NULL; |
3da4b609 | 2569 | int local_error = 0; |
9bda8467 | 2570 | |
283e68c7 DS |
2571 | if (!g) { |
2572 | graph_report("no commit-graph file loaded"); | |
2573 | return 1; | |
2574 | } | |
2575 | ||
2ac138d5 | 2576 | verify_commit_graph_error = verify_commit_graph_lite(g); |
9bda8467 DS |
2577 | if (verify_commit_graph_error) |
2578 | return verify_commit_graph_error; | |
2579 | ||
15316a47 | 2580 | if (!commit_graph_checksum_valid(g)) { |
41df0e30 DS |
2581 | graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt")); |
2582 | verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH; | |
2583 | } | |
2584 | ||
9bda8467 | 2585 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 DS |
2586 | struct commit *graph_commit; |
2587 | ||
92e2cab9 | 2588 | oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i); |
9bda8467 DS |
2589 | |
2590 | if (i && oidcmp(&prev_oid, &cur_oid) >= 0) | |
93b4405f | 2591 | graph_report(_("commit-graph has incorrect OID order: %s then %s"), |
9bda8467 DS |
2592 | oid_to_hex(&prev_oid), |
2593 | oid_to_hex(&cur_oid)); | |
2594 | ||
2595 | oidcpy(&prev_oid, &cur_oid); | |
2596 | ||
2597 | while (cur_oid.hash[0] > cur_fanout_pos) { | |
2598 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
2599 | ||
2600 | if (i != fanout_value) | |
93b4405f | 2601 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
2602 | cur_fanout_pos, fanout_value, i); |
2603 | cur_fanout_pos++; | |
2604 | } | |
2e3c0737 | 2605 | |
82952964 | 2606 | graph_commit = lookup_commit(r, &cur_oid); |
4f542b7a | 2607 | if (!parse_commit_in_graph_one(r, g, graph_commit)) |
93b4405f | 2608 | graph_report(_("failed to parse commit %s from commit-graph"), |
2e3c0737 | 2609 | oid_to_hex(&cur_oid)); |
9bda8467 DS |
2610 | } |
2611 | ||
2612 | while (cur_fanout_pos < 256) { | |
2613 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
2614 | ||
2615 | if (g->num_commits != fanout_value) | |
93b4405f | 2616 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
2617 | cur_fanout_pos, fanout_value, i); |
2618 | ||
2619 | cur_fanout_pos++; | |
2620 | } | |
2621 | ||
41df0e30 | 2622 | if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH) |
96af91d4 DS |
2623 | return verify_commit_graph_error; |
2624 | ||
73716122 GS |
2625 | if (flags & COMMIT_GRAPH_WRITE_PROGRESS) |
2626 | progress = start_progress(_("Verifying commits in commit graph"), | |
2627 | g->num_commits); | |
2628 | ||
96af91d4 | 2629 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 | 2630 | struct commit *graph_commit, *odb_commit; |
53614b13 | 2631 | struct commit_list *graph_parents, *odb_parents; |
d7f92784 AK |
2632 | timestamp_t max_generation = 0; |
2633 | timestamp_t generation; | |
96af91d4 | 2634 | |
1f7f557f | 2635 | display_progress(progress, i + 1); |
92e2cab9 | 2636 | oidread(&cur_oid, g->chunk_oid_lookup + g->hash_len * i); |
96af91d4 | 2637 | |
82952964 | 2638 | graph_commit = lookup_commit(r, &cur_oid); |
a378509e | 2639 | odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r)); |
4a93b899 | 2640 | if (repo_parse_commit_internal(r, odb_commit, 0, 0)) { |
93b4405f | 2641 | graph_report(_("failed to parse commit %s from object database for commit-graph"), |
96af91d4 DS |
2642 | oid_to_hex(&cur_oid)); |
2643 | continue; | |
2644 | } | |
2e3c0737 | 2645 | |
4f542b7a | 2646 | if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, |
2e3c0737 | 2647 | get_commit_tree_oid(odb_commit))) |
93b4405f | 2648 | graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"), |
2e3c0737 DS |
2649 | oid_to_hex(&cur_oid), |
2650 | oid_to_hex(get_commit_tree_oid(graph_commit)), | |
2651 | oid_to_hex(get_commit_tree_oid(odb_commit))); | |
53614b13 DS |
2652 | |
2653 | graph_parents = graph_commit->parents; | |
2654 | odb_parents = odb_commit->parents; | |
2655 | ||
2656 | while (graph_parents) { | |
afe8a907 | 2657 | if (!odb_parents) { |
93b4405f | 2658 | graph_report(_("commit-graph parent list for commit %s is too long"), |
53614b13 DS |
2659 | oid_to_hex(&cur_oid)); |
2660 | break; | |
2661 | } | |
2662 | ||
3da4b609 DS |
2663 | /* parse parent in case it is in a base graph */ |
2664 | parse_commit_in_graph_one(r, g, graph_parents->item); | |
2665 | ||
9001dc2a | 2666 | if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) |
93b4405f | 2667 | graph_report(_("commit-graph parent for %s is %s != %s"), |
53614b13 DS |
2668 | oid_to_hex(&cur_oid), |
2669 | oid_to_hex(&graph_parents->item->object.oid), | |
2670 | oid_to_hex(&odb_parents->item->object.oid)); | |
2671 | ||
c752ad09 AK |
2672 | generation = commit_graph_generation(graph_parents->item); |
2673 | if (generation > max_generation) | |
2674 | max_generation = generation; | |
1373e547 | 2675 | |
53614b13 DS |
2676 | graph_parents = graph_parents->next; |
2677 | odb_parents = odb_parents->next; | |
2678 | } | |
2679 | ||
afe8a907 | 2680 | if (odb_parents) |
93b4405f | 2681 | graph_report(_("commit-graph parent list for commit %s terminates early"), |
53614b13 | 2682 | oid_to_hex(&cur_oid)); |
1373e547 | 2683 | |
c49c82aa | 2684 | if (!commit_graph_generation(graph_commit)) { |
1373e547 | 2685 | if (generation_zero == GENERATION_NUMBER_EXISTS) |
93b4405f | 2686 | graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"), |
1373e547 DS |
2687 | oid_to_hex(&cur_oid)); |
2688 | generation_zero = GENERATION_ZERO_EXISTS; | |
2689 | } else if (generation_zero == GENERATION_ZERO_EXISTS) | |
93b4405f | 2690 | graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"), |
1373e547 DS |
2691 | oid_to_hex(&cur_oid)); |
2692 | ||
2693 | if (generation_zero == GENERATION_ZERO_EXISTS) | |
2694 | continue; | |
2695 | ||
2696 | /* | |
e8b63005 AK |
2697 | * If we are using topological level and one of our parents has |
2698 | * generation GENERATION_NUMBER_V1_MAX, then our generation is | |
2699 | * also GENERATION_NUMBER_V1_MAX. Decrement to avoid extra logic | |
2700 | * in the following condition. | |
1373e547 | 2701 | */ |
1fdc383c | 2702 | if (!g->read_generation_data && max_generation == GENERATION_NUMBER_V1_MAX) |
1373e547 DS |
2703 | max_generation--; |
2704 | ||
c752ad09 | 2705 | generation = commit_graph_generation(graph_commit); |
e8b63005 AK |
2706 | if (generation < max_generation + 1) |
2707 | graph_report(_("commit-graph generation for commit %s is %"PRItime" < %"PRItime), | |
1373e547 | 2708 | oid_to_hex(&cur_oid), |
c752ad09 | 2709 | generation, |
1373e547 | 2710 | max_generation + 1); |
88968ebf DS |
2711 | |
2712 | if (graph_commit->date != odb_commit->date) | |
93b4405f | 2713 | graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime), |
88968ebf DS |
2714 | oid_to_hex(&cur_oid), |
2715 | graph_commit->date, | |
2716 | odb_commit->date); | |
96af91d4 | 2717 | } |
1f7f557f | 2718 | stop_progress(&progress); |
96af91d4 | 2719 | |
3da4b609 DS |
2720 | local_error = verify_commit_graph_error; |
2721 | ||
2722 | if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph) | |
2723 | local_error |= verify_commit_graph(r, g->base_graph, flags); | |
2724 | ||
2725 | return local_error; | |
283e68c7 | 2726 | } |
c3756d5b JT |
2727 | |
2728 | void free_commit_graph(struct commit_graph *g) | |
2729 | { | |
2730 | if (!g) | |
2731 | return; | |
c8828530 | 2732 | if (g->data) { |
c3756d5b JT |
2733 | munmap((void *)g->data, g->data_len); |
2734 | g->data = NULL; | |
c3756d5b | 2735 | } |
6c622f9f | 2736 | free(g->filename); |
76ffbca7 | 2737 | free(g->bloom_filter_settings); |
c3756d5b JT |
2738 | free(g); |
2739 | } | |
6abada18 JK |
2740 | |
2741 | void disable_commit_graph(struct repository *r) | |
2742 | { | |
2743 | r->commit_graph_disabled = 1; | |
2744 | } |