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