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