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