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