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