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