]>
Commit | Line | Data |
---|---|---|
08fd81c9 DS |
1 | #include "cache.h" |
2 | #include "config.h" | |
33286dcd | 3 | #include "dir.h" |
08fd81c9 DS |
4 | #include "git-compat-util.h" |
5 | #include "lockfile.h" | |
6 | #include "pack.h" | |
7 | #include "packfile.h" | |
8 | #include "commit.h" | |
9 | #include "object.h" | |
59fb8770 | 10 | #include "refs.h" |
08fd81c9 DS |
11 | #include "revision.h" |
12 | #include "sha1-lookup.h" | |
13 | #include "commit-graph.h" | |
b10edb2d | 14 | #include "object-store.h" |
96af91d4 | 15 | #include "alloc.h" |
d6538246 DS |
16 | #include "hashmap.h" |
17 | #include "replace-object.h" | |
7b0f2292 | 18 | #include "progress.h" |
08fd81c9 DS |
19 | |
20 | #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ | |
21 | #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ | |
22 | #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ | |
23 | #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */ | |
5af7417b | 24 | #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */ |
08fd81c9 | 25 | |
c1665998 | 26 | #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16) |
08fd81c9 DS |
27 | |
28 | #define GRAPH_VERSION_1 0x1 | |
29 | #define GRAPH_VERSION GRAPH_VERSION_1 | |
30 | ||
5af7417b | 31 | #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000 |
08fd81c9 DS |
32 | #define GRAPH_EDGE_LAST_MASK 0x7fffffff |
33 | #define GRAPH_PARENT_NONE 0x70000000 | |
34 | ||
35 | #define GRAPH_LAST_EDGE 0x80000000 | |
36 | ||
0e3b97cc | 37 | #define GRAPH_HEADER_SIZE 8 |
08fd81c9 DS |
38 | #define GRAPH_FANOUT_SIZE (4 * 256) |
39 | #define GRAPH_CHUNKLOOKUP_WIDTH 12 | |
0e3b97cc | 40 | #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \ |
c1665998 | 41 | + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) |
08fd81c9 | 42 | |
2a2e32bd | 43 | char *get_commit_graph_filename(const char *obj_dir) |
08fd81c9 DS |
44 | { |
45 | return xstrfmt("%s/info/commit-graph", obj_dir); | |
46 | } | |
47 | ||
c1665998 | 48 | static uint8_t oid_version(void) |
49 | { | |
50 | return 1; | |
51 | } | |
52 | ||
2a2e32bd DS |
53 | static struct commit_graph *alloc_commit_graph(void) |
54 | { | |
55 | struct commit_graph *g = xcalloc(1, sizeof(*g)); | |
56 | g->graph_fd = -1; | |
57 | ||
58 | return g; | |
59 | } | |
60 | ||
d6538246 DS |
61 | extern int read_replace_refs; |
62 | ||
63 | static int commit_graph_compatible(struct repository *r) | |
64 | { | |
5cef295f DS |
65 | if (!r->gitdir) |
66 | return 0; | |
67 | ||
d6538246 DS |
68 | if (read_replace_refs) { |
69 | prepare_replace_object(r); | |
70 | if (hashmap_get_size(&r->objects->replace_map->map)) | |
71 | return 0; | |
72 | } | |
73 | ||
20fd6d57 DS |
74 | prepare_commit_graft(r); |
75 | if (r->parsed_objects && r->parsed_objects->grafts_nr) | |
76 | return 0; | |
77 | if (is_repository_shallow(r)) | |
78 | return 0; | |
79 | ||
d6538246 DS |
80 | return 1; |
81 | } | |
82 | ||
61df89c8 ÆAB |
83 | int open_commit_graph(const char *graph_file, int *fd, struct stat *st) |
84 | { | |
85 | *fd = git_open(graph_file); | |
86 | if (*fd < 0) | |
87 | return 0; | |
88 | if (fstat(*fd, st)) { | |
89 | close(*fd); | |
90 | return 0; | |
91 | } | |
92 | return 1; | |
93 | } | |
94 | ||
67a530fa | 95 | struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st) |
2a2e32bd DS |
96 | { |
97 | void *graph_map; | |
2a2e32bd | 98 | size_t graph_size; |
aa658574 | 99 | struct commit_graph *ret; |
2a2e32bd | 100 | |
61df89c8 | 101 | graph_size = xsize_t(st->st_size); |
2a2e32bd DS |
102 | |
103 | if (graph_size < GRAPH_MIN_SIZE) { | |
104 | close(fd); | |
67a530fa | 105 | error(_("commit-graph file is too small")); |
61df89c8 | 106 | return NULL; |
2a2e32bd DS |
107 | } |
108 | graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
aa658574 JS |
109 | ret = parse_commit_graph(graph_map, fd, graph_size); |
110 | ||
111 | if (!ret) { | |
112 | munmap(graph_map, graph_size); | |
113 | close(fd); | |
aa658574 JS |
114 | } |
115 | ||
116 | return ret; | |
117 | } | |
118 | ||
2ac138d5 ÆAB |
119 | static int verify_commit_graph_lite(struct commit_graph *g) |
120 | { | |
121 | /* | |
122 | * Basic validation shared between parse_commit_graph() | |
123 | * which'll be called every time the graph is used, and the | |
124 | * much more expensive verify_commit_graph() used by | |
125 | * "commit-graph verify". | |
126 | * | |
127 | * There should only be very basic checks here to ensure that | |
128 | * we don't e.g. segfault in fill_commit_in_graph(), but | |
129 | * because this is a very hot codepath nothing that e.g. loops | |
130 | * over g->num_commits, or runs a checksum on the commit-graph | |
131 | * itself. | |
132 | */ | |
133 | if (!g->chunk_oid_fanout) { | |
134 | error("commit-graph is missing the OID Fanout chunk"); | |
135 | return 1; | |
136 | } | |
137 | if (!g->chunk_oid_lookup) { | |
138 | error("commit-graph is missing the OID Lookup chunk"); | |
139 | return 1; | |
140 | } | |
141 | if (!g->chunk_commit_data) { | |
142 | error("commit-graph is missing the Commit Data chunk"); | |
143 | return 1; | |
144 | } | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
aa658574 JS |
149 | struct commit_graph *parse_commit_graph(void *graph_map, int fd, |
150 | size_t graph_size) | |
151 | { | |
152 | const unsigned char *data, *chunk_lookup; | |
153 | uint32_t i; | |
154 | struct commit_graph *graph; | |
155 | uint64_t last_chunk_offset; | |
156 | uint32_t last_chunk_id; | |
157 | uint32_t graph_signature; | |
158 | unsigned char graph_version, hash_version; | |
159 | ||
160 | if (!graph_map) | |
161 | return NULL; | |
162 | ||
163 | if (graph_size < GRAPH_MIN_SIZE) | |
164 | return NULL; | |
165 | ||
2a2e32bd DS |
166 | data = (const unsigned char *)graph_map; |
167 | ||
168 | graph_signature = get_be32(data); | |
169 | if (graph_signature != GRAPH_SIGNATURE) { | |
93b4405f | 170 | error(_("commit-graph signature %X does not match signature %X"), |
2a2e32bd | 171 | graph_signature, GRAPH_SIGNATURE); |
aa658574 | 172 | return NULL; |
2a2e32bd DS |
173 | } |
174 | ||
175 | graph_version = *(unsigned char*)(data + 4); | |
176 | if (graph_version != GRAPH_VERSION) { | |
93b4405f | 177 | error(_("commit-graph version %X does not match version %X"), |
2a2e32bd | 178 | graph_version, GRAPH_VERSION); |
aa658574 | 179 | return NULL; |
2a2e32bd DS |
180 | } |
181 | ||
182 | hash_version = *(unsigned char*)(data + 5); | |
c1665998 | 183 | if (hash_version != oid_version()) { |
93b4405f | 184 | error(_("commit-graph hash version %X does not match version %X"), |
c1665998 | 185 | hash_version, oid_version()); |
aa658574 | 186 | return NULL; |
2a2e32bd DS |
187 | } |
188 | ||
189 | graph = alloc_commit_graph(); | |
190 | ||
c1665998 | 191 | graph->hash_len = the_hash_algo->rawsz; |
2a2e32bd DS |
192 | graph->num_chunks = *(unsigned char*)(data + 6); |
193 | graph->graph_fd = fd; | |
194 | graph->data = graph_map; | |
195 | graph->data_len = graph_size; | |
196 | ||
197 | last_chunk_id = 0; | |
198 | last_chunk_offset = 8; | |
199 | chunk_lookup = data + 8; | |
200 | for (i = 0; i < graph->num_chunks; i++) { | |
d2b86fba JS |
201 | uint32_t chunk_id; |
202 | uint64_t chunk_offset; | |
2a2e32bd DS |
203 | int chunk_repeated = 0; |
204 | ||
d2b86fba JS |
205 | if (data + graph_size - chunk_lookup < |
206 | GRAPH_CHUNKLOOKUP_WIDTH) { | |
93b4405f | 207 | error(_("commit-graph chunk lookup table entry missing; file may be incomplete")); |
d2b86fba JS |
208 | free(graph); |
209 | return NULL; | |
210 | } | |
211 | ||
212 | chunk_id = get_be32(chunk_lookup + 0); | |
213 | chunk_offset = get_be64(chunk_lookup + 4); | |
214 | ||
2a2e32bd DS |
215 | chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH; |
216 | ||
c1665998 | 217 | if (chunk_offset > graph_size - the_hash_algo->rawsz) { |
93b4405f | 218 | error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32), |
2a2e32bd | 219 | (uint32_t)chunk_offset); |
aa658574 JS |
220 | free(graph); |
221 | return NULL; | |
2a2e32bd DS |
222 | } |
223 | ||
224 | switch (chunk_id) { | |
225 | case GRAPH_CHUNKID_OIDFANOUT: | |
226 | if (graph->chunk_oid_fanout) | |
227 | chunk_repeated = 1; | |
228 | else | |
229 | graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset); | |
230 | break; | |
231 | ||
232 | case GRAPH_CHUNKID_OIDLOOKUP: | |
233 | if (graph->chunk_oid_lookup) | |
234 | chunk_repeated = 1; | |
235 | else | |
236 | graph->chunk_oid_lookup = data + chunk_offset; | |
237 | break; | |
238 | ||
239 | case GRAPH_CHUNKID_DATA: | |
240 | if (graph->chunk_commit_data) | |
241 | chunk_repeated = 1; | |
242 | else | |
243 | graph->chunk_commit_data = data + chunk_offset; | |
244 | break; | |
245 | ||
5af7417b SG |
246 | case GRAPH_CHUNKID_EXTRAEDGES: |
247 | if (graph->chunk_extra_edges) | |
2a2e32bd DS |
248 | chunk_repeated = 1; |
249 | else | |
5af7417b | 250 | graph->chunk_extra_edges = data + chunk_offset; |
2a2e32bd DS |
251 | break; |
252 | } | |
253 | ||
254 | if (chunk_repeated) { | |
93b4405f | 255 | error(_("commit-graph chunk id %08x appears multiple times"), chunk_id); |
aa658574 JS |
256 | free(graph); |
257 | return NULL; | |
2a2e32bd DS |
258 | } |
259 | ||
260 | if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP) | |
261 | { | |
262 | graph->num_commits = (chunk_offset - last_chunk_offset) | |
263 | / graph->hash_len; | |
264 | } | |
265 | ||
266 | last_chunk_id = chunk_id; | |
267 | last_chunk_offset = chunk_offset; | |
268 | } | |
269 | ||
98552f25 JS |
270 | if (verify_commit_graph_lite(graph)) { |
271 | free(graph); | |
2ac138d5 | 272 | return NULL; |
98552f25 | 273 | } |
2ac138d5 | 274 | |
2a2e32bd | 275 | return graph; |
2a2e32bd DS |
276 | } |
277 | ||
61df89c8 ÆAB |
278 | static struct commit_graph *load_commit_graph_one(const char *graph_file) |
279 | { | |
280 | ||
281 | struct stat st; | |
282 | int fd; | |
283 | int open_ok = open_commit_graph(graph_file, &fd, &st); | |
284 | ||
285 | if (!open_ok) | |
286 | return NULL; | |
287 | ||
67a530fa | 288 | return load_commit_graph_one_fd_st(fd, &st); |
61df89c8 ÆAB |
289 | } |
290 | ||
dade47c0 | 291 | static void prepare_commit_graph_one(struct repository *r, const char *obj_dir) |
177722b3 DS |
292 | { |
293 | char *graph_name; | |
294 | ||
dade47c0 | 295 | if (r->objects->commit_graph) |
177722b3 DS |
296 | return; |
297 | ||
298 | graph_name = get_commit_graph_filename(obj_dir); | |
dade47c0 | 299 | r->objects->commit_graph = |
85277506 | 300 | load_commit_graph_one(graph_name); |
177722b3 DS |
301 | |
302 | FREE_AND_NULL(graph_name); | |
303 | } | |
304 | ||
5faf357b JT |
305 | /* |
306 | * Return 1 if commit_graph is non-NULL, and 0 otherwise. | |
307 | * | |
308 | * On the first invocation, this function attemps to load the commit | |
309 | * graph if the_repository is configured to have one. | |
310 | */ | |
dade47c0 | 311 | static int prepare_commit_graph(struct repository *r) |
177722b3 | 312 | { |
263db403 | 313 | struct object_directory *odb; |
dade47c0 JT |
314 | int config_value; |
315 | ||
43d35618 ÆAB |
316 | if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0)) |
317 | die("dying as requested by the '%s' variable on commit-graph load!", | |
318 | GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD); | |
319 | ||
dade47c0 JT |
320 | if (r->objects->commit_graph_attempted) |
321 | return !!r->objects->commit_graph; | |
322 | r->objects->commit_graph_attempted = 1; | |
323 | ||
859fdc0c DS |
324 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) && |
325 | (repo_config_get_bool(r, "core.commitgraph", &config_value) || | |
326 | !config_value)) | |
dade47c0 JT |
327 | /* |
328 | * This repository is not configured to use commit graphs, so | |
329 | * do not load one. (But report commit_graph_attempted anyway | |
330 | * so that commit graph loading is not attempted again for this | |
331 | * repository.) | |
332 | */ | |
5faf357b JT |
333 | return 0; |
334 | ||
d6538246 DS |
335 | if (!commit_graph_compatible(r)) |
336 | return 0; | |
337 | ||
dade47c0 | 338 | prepare_alt_odb(r); |
f0eaf638 | 339 | for (odb = r->objects->odb; |
263db403 JK |
340 | !r->objects->commit_graph && odb; |
341 | odb = odb->next) | |
342 | prepare_commit_graph_one(r, odb->path); | |
dade47c0 | 343 | return !!r->objects->commit_graph; |
177722b3 DS |
344 | } |
345 | ||
6cc01743 DS |
346 | int generation_numbers_enabled(struct repository *r) |
347 | { | |
348 | uint32_t first_generation; | |
349 | struct commit_graph *g; | |
350 | if (!prepare_commit_graph(r)) | |
351 | return 0; | |
352 | ||
353 | g = r->objects->commit_graph; | |
354 | ||
355 | if (!g->num_commits) | |
356 | return 0; | |
357 | ||
358 | first_generation = get_be32(g->chunk_commit_data + | |
359 | g->hash_len + 8) >> 2; | |
360 | ||
361 | return !!first_generation; | |
362 | } | |
363 | ||
c3a3a964 | 364 | void close_commit_graph(struct raw_object_store *o) |
177722b3 | 365 | { |
c3a3a964 DS |
366 | free_commit_graph(o->commit_graph); |
367 | o->commit_graph = NULL; | |
177722b3 DS |
368 | } |
369 | ||
370 | static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos) | |
371 | { | |
372 | return bsearch_hash(oid->hash, g->chunk_oid_fanout, | |
373 | g->chunk_oid_lookup, g->hash_len, pos); | |
374 | } | |
375 | ||
4f542b7a SB |
376 | static struct commit_list **insert_parent_or_die(struct repository *r, |
377 | struct commit_graph *g, | |
177722b3 DS |
378 | uint64_t pos, |
379 | struct commit_list **pptr) | |
380 | { | |
381 | struct commit *c; | |
382 | struct object_id oid; | |
96af91d4 | 383 | |
53614b13 DS |
384 | if (pos >= g->num_commits) |
385 | die("invalid parent position %"PRIu64, pos); | |
386 | ||
177722b3 | 387 | hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * pos); |
4f542b7a | 388 | c = lookup_commit(r, &oid); |
177722b3 | 389 | if (!c) |
4f5b532d | 390 | die(_("could not find commit %s"), oid_to_hex(&oid)); |
177722b3 DS |
391 | c->graph_pos = pos; |
392 | return &commit_list_insert(c, pptr)->next; | |
393 | } | |
394 | ||
e2838d85 DS |
395 | static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos) |
396 | { | |
397 | const unsigned char *commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * pos; | |
398 | item->graph_pos = pos; | |
399 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; | |
400 | } | |
401 | ||
a133c40b NTND |
402 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
403 | { | |
404 | c->maybe_tree = t; | |
405 | } | |
406 | ||
4f542b7a SB |
407 | static int fill_commit_in_graph(struct repository *r, |
408 | struct commit *item, | |
409 | struct commit_graph *g, uint32_t pos) | |
177722b3 | 410 | { |
177722b3 DS |
411 | uint32_t edge_value; |
412 | uint32_t *parent_data_ptr; | |
413 | uint64_t date_low, date_high; | |
414 | struct commit_list **pptr; | |
415 | const unsigned char *commit_data = g->chunk_commit_data + (g->hash_len + 16) * pos; | |
416 | ||
417 | item->object.parsed = 1; | |
418 | item->graph_pos = pos; | |
419 | ||
a133c40b | 420 | set_commit_tree(item, NULL); |
177722b3 DS |
421 | |
422 | date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; | |
423 | date_low = get_be32(commit_data + g->hash_len + 12); | |
424 | item->date = (timestamp_t)((date_high << 32) | date_low); | |
425 | ||
83073cc9 DS |
426 | item->generation = get_be32(commit_data + g->hash_len + 8) >> 2; |
427 | ||
177722b3 DS |
428 | pptr = &item->parents; |
429 | ||
430 | edge_value = get_be32(commit_data + g->hash_len); | |
431 | if (edge_value == GRAPH_PARENT_NONE) | |
432 | return 1; | |
4f542b7a | 433 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
434 | |
435 | edge_value = get_be32(commit_data + g->hash_len + 4); | |
436 | if (edge_value == GRAPH_PARENT_NONE) | |
437 | return 1; | |
5af7417b | 438 | if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) { |
4f542b7a | 439 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
177722b3 DS |
440 | return 1; |
441 | } | |
442 | ||
5af7417b | 443 | parent_data_ptr = (uint32_t*)(g->chunk_extra_edges + |
177722b3 DS |
444 | 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK)); |
445 | do { | |
446 | edge_value = get_be32(parent_data_ptr); | |
4f542b7a | 447 | pptr = insert_parent_or_die(r, g, |
177722b3 DS |
448 | edge_value & GRAPH_EDGE_LAST_MASK, |
449 | pptr); | |
450 | parent_data_ptr++; | |
451 | } while (!(edge_value & GRAPH_LAST_EDGE)); | |
452 | ||
453 | return 1; | |
454 | } | |
455 | ||
e2838d85 DS |
456 | static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos) |
457 | { | |
458 | if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) { | |
459 | *pos = item->graph_pos; | |
460 | return 1; | |
461 | } else { | |
462 | return bsearch_graph(g, &(item->object.oid), pos); | |
463 | } | |
464 | } | |
465 | ||
4f542b7a SB |
466 | static int parse_commit_in_graph_one(struct repository *r, |
467 | struct commit_graph *g, | |
468 | struct commit *item) | |
177722b3 | 469 | { |
e2838d85 DS |
470 | uint32_t pos; |
471 | ||
177722b3 DS |
472 | if (item->object.parsed) |
473 | return 1; | |
ee797053 DS |
474 | |
475 | if (find_commit_in_graph(item, g, &pos)) | |
4f542b7a | 476 | return fill_commit_in_graph(r, item, g, pos); |
ee797053 DS |
477 | |
478 | return 0; | |
479 | } | |
480 | ||
dade47c0 | 481 | int parse_commit_in_graph(struct repository *r, struct commit *item) |
ee797053 | 482 | { |
dade47c0 | 483 | if (!prepare_commit_graph(r)) |
ee797053 | 484 | return 0; |
4f542b7a | 485 | return parse_commit_in_graph_one(r, r->objects->commit_graph, item); |
177722b3 DS |
486 | } |
487 | ||
dade47c0 | 488 | void load_commit_graph_info(struct repository *r, struct commit *item) |
e2838d85 DS |
489 | { |
490 | uint32_t pos; | |
dade47c0 | 491 | if (!prepare_commit_graph(r)) |
e2838d85 | 492 | return; |
dade47c0 JT |
493 | if (find_commit_in_graph(item, r->objects->commit_graph, &pos)) |
494 | fill_commit_graph_info(item, r->objects->commit_graph, pos); | |
e2838d85 DS |
495 | } |
496 | ||
4f542b7a SB |
497 | static struct tree *load_tree_for_commit(struct repository *r, |
498 | struct commit_graph *g, | |
499 | struct commit *c) | |
7b8a21db DS |
500 | { |
501 | struct object_id oid; | |
502 | const unsigned char *commit_data = g->chunk_commit_data + | |
503 | GRAPH_DATA_WIDTH * (c->graph_pos); | |
504 | ||
505 | hashcpy(oid.hash, commit_data); | |
a133c40b | 506 | set_commit_tree(c, lookup_tree(r, &oid)); |
7b8a21db DS |
507 | |
508 | return c->maybe_tree; | |
509 | } | |
510 | ||
4f542b7a SB |
511 | static struct tree *get_commit_tree_in_graph_one(struct repository *r, |
512 | struct commit_graph *g, | |
0cbef8f8 | 513 | const struct commit *c) |
7b8a21db DS |
514 | { |
515 | if (c->maybe_tree) | |
516 | return c->maybe_tree; | |
517 | if (c->graph_pos == COMMIT_NOT_FROM_GRAPH) | |
0cbef8f8 DS |
518 | BUG("get_commit_tree_in_graph_one called from non-commit-graph commit"); |
519 | ||
4f542b7a | 520 | return load_tree_for_commit(r, g, (struct commit *)c); |
0cbef8f8 | 521 | } |
7b8a21db | 522 | |
dade47c0 | 523 | struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c) |
0cbef8f8 | 524 | { |
4f542b7a | 525 | return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c); |
7b8a21db DS |
526 | } |
527 | ||
c9905bea DS |
528 | struct packed_commit_list { |
529 | struct commit **list; | |
530 | int nr; | |
531 | int alloc; | |
532 | }; | |
533 | ||
534 | struct packed_oid_list { | |
535 | struct object_id *list; | |
536 | int nr; | |
537 | int alloc; | |
538 | }; | |
539 | ||
540 | struct write_commit_graph_context { | |
541 | struct repository *r; | |
542 | const char *obj_dir; | |
543 | char *graph_name; | |
544 | struct packed_oid_list oids; | |
545 | struct packed_commit_list commits; | |
546 | int num_extra_edges; | |
547 | unsigned long approx_nr_objects; | |
548 | struct progress *progress; | |
549 | int progress_done; | |
550 | uint64_t progress_cnt; | |
551 | unsigned append:1, | |
552 | report_progress:1; | |
553 | }; | |
554 | ||
08fd81c9 | 555 | static void write_graph_chunk_fanout(struct hashfile *f, |
c9905bea | 556 | struct write_commit_graph_context *ctx) |
08fd81c9 DS |
557 | { |
558 | int i, count = 0; | |
c9905bea | 559 | struct commit **list = ctx->commits.list; |
08fd81c9 DS |
560 | |
561 | /* | |
562 | * Write the first-level table (the list is sorted, | |
563 | * but we use a 256-entry lookup to be able to avoid | |
564 | * having to do eight extra binary search iterations). | |
565 | */ | |
566 | for (i = 0; i < 256; i++) { | |
c9905bea | 567 | while (count < ctx->commits.nr) { |
08fd81c9 DS |
568 | if ((*list)->object.oid.hash[0] != i) |
569 | break; | |
c9905bea | 570 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 DS |
571 | count++; |
572 | list++; | |
573 | } | |
574 | ||
575 | hashwrite_be32(f, count); | |
576 | } | |
577 | } | |
578 | ||
579 | static void write_graph_chunk_oids(struct hashfile *f, int hash_len, | |
c9905bea | 580 | struct write_commit_graph_context *ctx) |
08fd81c9 | 581 | { |
c9905bea | 582 | struct commit **list = ctx->commits.list; |
08fd81c9 | 583 | int count; |
c9905bea DS |
584 | for (count = 0; count < ctx->commits.nr; count++, list++) { |
585 | display_progress(ctx->progress, ++ctx->progress_cnt); | |
08fd81c9 | 586 | hashwrite(f, (*list)->object.oid.hash, (int)hash_len); |
53035c4f | 587 | } |
08fd81c9 DS |
588 | } |
589 | ||
590 | static const unsigned char *commit_to_sha1(size_t index, void *table) | |
591 | { | |
592 | struct commit **commits = table; | |
593 | return commits[index]->object.oid.hash; | |
594 | } | |
595 | ||
596 | static void write_graph_chunk_data(struct hashfile *f, int hash_len, | |
c9905bea | 597 | struct write_commit_graph_context *ctx) |
08fd81c9 | 598 | { |
c9905bea DS |
599 | struct commit **list = ctx->commits.list; |
600 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
601 | uint32_t num_extra_edges = 0; |
602 | ||
603 | while (list < last) { | |
604 | struct commit_list *parent; | |
605 | int edge_value; | |
606 | uint32_t packedDate[2]; | |
c9905bea | 607 | display_progress(ctx->progress, ++ctx->progress_cnt); |
08fd81c9 | 608 | |
43d35618 | 609 | parse_commit_no_graph(*list); |
2e27bd77 | 610 | hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len); |
08fd81c9 DS |
611 | |
612 | parent = (*list)->parents; | |
613 | ||
614 | if (!parent) | |
615 | edge_value = GRAPH_PARENT_NONE; | |
616 | else { | |
617 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
618 | ctx->commits.list, |
619 | ctx->commits.nr, | |
08fd81c9 DS |
620 | commit_to_sha1); |
621 | ||
622 | if (edge_value < 0) | |
cce99cd8 DS |
623 | BUG("missing parent %s for commit %s", |
624 | oid_to_hex(&parent->item->object.oid), | |
625 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
626 | } |
627 | ||
628 | hashwrite_be32(f, edge_value); | |
629 | ||
630 | if (parent) | |
631 | parent = parent->next; | |
632 | ||
633 | if (!parent) | |
634 | edge_value = GRAPH_PARENT_NONE; | |
635 | else if (parent->next) | |
5af7417b | 636 | edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; |
08fd81c9 DS |
637 | else { |
638 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
639 | ctx->commits.list, |
640 | ctx->commits.nr, | |
08fd81c9 DS |
641 | commit_to_sha1); |
642 | if (edge_value < 0) | |
cce99cd8 DS |
643 | BUG("missing parent %s for commit %s", |
644 | oid_to_hex(&parent->item->object.oid), | |
645 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
646 | } |
647 | ||
648 | hashwrite_be32(f, edge_value); | |
649 | ||
5af7417b | 650 | if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) { |
08fd81c9 DS |
651 | do { |
652 | num_extra_edges++; | |
653 | parent = parent->next; | |
654 | } while (parent); | |
655 | } | |
656 | ||
657 | if (sizeof((*list)->date) > 4) | |
658 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); | |
659 | else | |
660 | packedDate[0] = 0; | |
661 | ||
3258c663 DS |
662 | packedDate[0] |= htonl((*list)->generation << 2); |
663 | ||
08fd81c9 DS |
664 | packedDate[1] = htonl((*list)->date); |
665 | hashwrite(f, packedDate, 8); | |
666 | ||
667 | list++; | |
668 | } | |
669 | } | |
670 | ||
5af7417b | 671 | static void write_graph_chunk_extra_edges(struct hashfile *f, |
c9905bea | 672 | struct write_commit_graph_context *ctx) |
08fd81c9 | 673 | { |
c9905bea DS |
674 | struct commit **list = ctx->commits.list; |
675 | struct commit **last = ctx->commits.list + ctx->commits.nr; | |
08fd81c9 DS |
676 | struct commit_list *parent; |
677 | ||
678 | while (list < last) { | |
679 | int num_parents = 0; | |
53035c4f | 680 | |
c9905bea | 681 | display_progress(ctx->progress, ++ctx->progress_cnt); |
53035c4f | 682 | |
08fd81c9 DS |
683 | for (parent = (*list)->parents; num_parents < 3 && parent; |
684 | parent = parent->next) | |
685 | num_parents++; | |
686 | ||
687 | if (num_parents <= 2) { | |
688 | list++; | |
689 | continue; | |
690 | } | |
691 | ||
692 | /* Since num_parents > 2, this initializer is safe. */ | |
693 | for (parent = (*list)->parents->next; parent; parent = parent->next) { | |
694 | int edge_value = sha1_pos(parent->item->object.oid.hash, | |
c9905bea DS |
695 | ctx->commits.list, |
696 | ctx->commits.nr, | |
08fd81c9 DS |
697 | commit_to_sha1); |
698 | ||
699 | if (edge_value < 0) | |
cce99cd8 DS |
700 | BUG("missing parent %s for commit %s", |
701 | oid_to_hex(&parent->item->object.oid), | |
702 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
703 | else if (!parent->next) |
704 | edge_value |= GRAPH_LAST_EDGE; | |
705 | ||
706 | hashwrite_be32(f, edge_value); | |
707 | } | |
708 | ||
709 | list++; | |
710 | } | |
711 | } | |
712 | ||
713 | static int commit_compare(const void *_a, const void *_b) | |
714 | { | |
715 | const struct object_id *a = (const struct object_id *)_a; | |
716 | const struct object_id *b = (const struct object_id *)_b; | |
717 | return oidcmp(a, b); | |
718 | } | |
719 | ||
08fd81c9 DS |
720 | static int add_packed_commits(const struct object_id *oid, |
721 | struct packed_git *pack, | |
722 | uint32_t pos, | |
723 | void *data) | |
724 | { | |
c9905bea | 725 | struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data; |
08fd81c9 DS |
726 | enum object_type type; |
727 | off_t offset = nth_packed_object_offset(pack, pos); | |
728 | struct object_info oi = OBJECT_INFO_INIT; | |
729 | ||
c9905bea DS |
730 | if (ctx->progress) |
731 | display_progress(ctx->progress, ++ctx->progress_done); | |
7b0f2292 | 732 | |
08fd81c9 | 733 | oi.typep = &type; |
c9905bea | 734 | if (packed_object_info(ctx->r, pack, offset, &oi) < 0) |
4f5b532d | 735 | die(_("unable to get type of object %s"), oid_to_hex(oid)); |
08fd81c9 DS |
736 | |
737 | if (type != OBJ_COMMIT) | |
738 | return 0; | |
739 | ||
c9905bea DS |
740 | ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc); |
741 | oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid); | |
742 | ctx->oids.nr++; | |
08fd81c9 DS |
743 | |
744 | return 0; | |
745 | } | |
746 | ||
c9905bea | 747 | static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit) |
4f2542b4 DS |
748 | { |
749 | struct commit_list *parent; | |
750 | for (parent = commit->parents; parent; parent = parent->next) { | |
751 | if (!(parent->item->object.flags & UNINTERESTING)) { | |
c9905bea DS |
752 | ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc); |
753 | oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid)); | |
754 | ctx->oids.nr++; | |
4f2542b4 DS |
755 | parent->item->object.flags |= UNINTERESTING; |
756 | } | |
757 | } | |
758 | } | |
759 | ||
c9905bea | 760 | static void close_reachable(struct write_commit_graph_context *ctx) |
4f2542b4 | 761 | { |
49bbc57a | 762 | int i; |
4f2542b4 DS |
763 | struct commit *commit; |
764 | ||
c9905bea DS |
765 | if (ctx->report_progress) |
766 | ctx->progress = start_delayed_progress( | |
767 | _("Loading known commits in commit graph"), | |
768 | ctx->oids.nr); | |
769 | for (i = 0; i < ctx->oids.nr; i++) { | |
770 | display_progress(ctx->progress, i + 1); | |
771 | commit = lookup_commit(ctx->r, &ctx->oids.list[i]); | |
4f2542b4 DS |
772 | if (commit) |
773 | commit->object.flags |= UNINTERESTING; | |
774 | } | |
c9905bea | 775 | stop_progress(&ctx->progress); |
4f2542b4 DS |
776 | |
777 | /* | |
c9905bea | 778 | * As this loop runs, ctx->oids.nr may grow, but not more |
4f2542b4 DS |
779 | * than the number of missing commits in the reachable |
780 | * closure. | |
781 | */ | |
c9905bea DS |
782 | if (ctx->report_progress) |
783 | ctx->progress = start_delayed_progress( | |
784 | _("Expanding reachable commits in commit graph"), | |
785 | ctx->oids.nr); | |
786 | for (i = 0; i < ctx->oids.nr; i++) { | |
787 | display_progress(ctx->progress, i + 1); | |
788 | commit = lookup_commit(ctx->r, &ctx->oids.list[i]); | |
4f2542b4 | 789 | |
43d35618 | 790 | if (commit && !parse_commit_no_graph(commit)) |
c9905bea | 791 | add_missing_parents(ctx, commit); |
4f2542b4 | 792 | } |
c9905bea | 793 | stop_progress(&ctx->progress); |
4f2542b4 | 794 | |
c9905bea DS |
795 | if (ctx->report_progress) |
796 | ctx->progress = start_delayed_progress( | |
797 | _("Clearing commit marks in commit graph"), | |
798 | ctx->oids.nr); | |
799 | for (i = 0; i < ctx->oids.nr; i++) { | |
800 | display_progress(ctx->progress, i + 1); | |
801 | commit = lookup_commit(ctx->r, &ctx->oids.list[i]); | |
4f2542b4 DS |
802 | |
803 | if (commit) | |
804 | commit->object.flags &= ~UNINTERESTING; | |
805 | } | |
c9905bea | 806 | stop_progress(&ctx->progress); |
4f2542b4 DS |
807 | } |
808 | ||
c9905bea | 809 | static void compute_generation_numbers(struct write_commit_graph_context *ctx) |
3258c663 DS |
810 | { |
811 | int i; | |
812 | struct commit_list *list = NULL; | |
813 | ||
c9905bea DS |
814 | if (ctx->report_progress) |
815 | ctx->progress = start_progress( | |
816 | _("Computing commit graph generation numbers"), | |
817 | ctx->commits.nr); | |
818 | for (i = 0; i < ctx->commits.nr; i++) { | |
819 | display_progress(ctx->progress, i + 1); | |
820 | if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY && | |
821 | ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO) | |
3258c663 DS |
822 | continue; |
823 | ||
c9905bea | 824 | commit_list_insert(ctx->commits.list[i], &list); |
3258c663 DS |
825 | while (list) { |
826 | struct commit *current = list->item; | |
827 | struct commit_list *parent; | |
828 | int all_parents_computed = 1; | |
829 | uint32_t max_generation = 0; | |
830 | ||
831 | for (parent = current->parents; parent; parent = parent->next) { | |
832 | if (parent->item->generation == GENERATION_NUMBER_INFINITY || | |
833 | parent->item->generation == GENERATION_NUMBER_ZERO) { | |
834 | all_parents_computed = 0; | |
835 | commit_list_insert(parent->item, &list); | |
836 | break; | |
837 | } else if (parent->item->generation > max_generation) { | |
838 | max_generation = parent->item->generation; | |
839 | } | |
840 | } | |
841 | ||
842 | if (all_parents_computed) { | |
843 | current->generation = max_generation + 1; | |
844 | pop_commit(&list); | |
845 | ||
846 | if (current->generation > GENERATION_NUMBER_MAX) | |
847 | current->generation = GENERATION_NUMBER_MAX; | |
848 | } | |
849 | } | |
850 | } | |
c9905bea | 851 | stop_progress(&ctx->progress); |
3258c663 DS |
852 | } |
853 | ||
59fb8770 DS |
854 | static int add_ref_to_list(const char *refname, |
855 | const struct object_id *oid, | |
856 | int flags, void *cb_data) | |
857 | { | |
858 | struct string_list *list = (struct string_list *)cb_data; | |
859 | ||
860 | string_list_append(list, oid_to_hex(oid)); | |
861 | return 0; | |
862 | } | |
863 | ||
5af80394 | 864 | int write_commit_graph_reachable(const char *obj_dir, unsigned int flags) |
59fb8770 | 865 | { |
f4dbdfc4 | 866 | struct string_list list = STRING_LIST_INIT_DUP; |
e103f727 | 867 | int result; |
59fb8770 | 868 | |
59fb8770 | 869 | for_each_ref(add_ref_to_list, &list); |
e103f727 | 870 | result = write_commit_graph(obj_dir, NULL, &list, |
5af80394 | 871 | flags); |
f4dbdfc4 DS |
872 | |
873 | string_list_clear(&list, 0); | |
e103f727 | 874 | return result; |
59fb8770 DS |
875 | } |
876 | ||
ef5b83f2 DS |
877 | static int fill_oids_from_packs(struct write_commit_graph_context *ctx, |
878 | struct string_list *pack_indexes) | |
08fd81c9 | 879 | { |
ef5b83f2 | 880 | uint32_t i; |
28944739 | 881 | struct strbuf progress_title = STRBUF_INIT; |
ef5b83f2 DS |
882 | struct strbuf packname = STRBUF_INIT; |
883 | int dirlen; | |
08fd81c9 | 884 | |
ef5b83f2 DS |
885 | strbuf_addf(&packname, "%s/pack/", ctx->obj_dir); |
886 | dirlen = packname.len; | |
887 | if (ctx->report_progress) { | |
888 | strbuf_addf(&progress_title, | |
889 | Q_("Finding commits for commit graph in %d pack", | |
890 | "Finding commits for commit graph in %d packs", | |
891 | pack_indexes->nr), | |
892 | pack_indexes->nr); | |
893 | ctx->progress = start_delayed_progress(progress_title.buf, 0); | |
894 | ctx->progress_done = 0; | |
7547b95b | 895 | } |
ef5b83f2 DS |
896 | for (i = 0; i < pack_indexes->nr; i++) { |
897 | struct packed_git *p; | |
898 | strbuf_setlen(&packname, dirlen); | |
899 | strbuf_addstr(&packname, pack_indexes->items[i].string); | |
900 | p = add_packed_git(packname.buf, packname.len, 1); | |
901 | if (!p) { | |
902 | error(_("error adding pack %s"), packname.buf); | |
903 | return -1; | |
7547b95b | 904 | } |
ef5b83f2 DS |
905 | if (open_pack_index(p)) { |
906 | error(_("error opening index for %s"), packname.buf); | |
907 | return -1; | |
908 | } | |
909 | for_each_object_in_pack(p, add_packed_commits, ctx, | |
910 | FOR_EACH_OBJECT_PACK_ORDER); | |
911 | close_pack(p); | |
912 | free(p); | |
7547b95b DS |
913 | } |
914 | ||
ef5b83f2 DS |
915 | stop_progress(&ctx->progress); |
916 | strbuf_reset(&progress_title); | |
917 | strbuf_release(&packname); | |
918 | ||
919 | return 0; | |
920 | } | |
921 | ||
4c9efe85 DS |
922 | static void fill_oids_from_commit_hex(struct write_commit_graph_context *ctx, |
923 | struct string_list *commit_hex) | |
924 | { | |
925 | uint32_t i; | |
926 | struct strbuf progress_title = STRBUF_INIT; | |
927 | ||
928 | if (ctx->report_progress) { | |
929 | strbuf_addf(&progress_title, | |
930 | Q_("Finding commits for commit graph from %d ref", | |
931 | "Finding commits for commit graph from %d refs", | |
932 | commit_hex->nr), | |
933 | commit_hex->nr); | |
934 | ctx->progress = start_delayed_progress( | |
935 | progress_title.buf, | |
936 | commit_hex->nr); | |
3d5df01b | 937 | } |
4c9efe85 DS |
938 | for (i = 0; i < commit_hex->nr; i++) { |
939 | const char *end; | |
940 | struct object_id oid; | |
941 | struct commit *result; | |
942 | ||
943 | display_progress(ctx->progress, i + 1); | |
944 | if (commit_hex->items[i].string && | |
945 | parse_oid_hex(commit_hex->items[i].string, &oid, &end)) | |
946 | continue; | |
3d5df01b | 947 | |
4c9efe85 DS |
948 | result = lookup_commit_reference_gently(ctx->r, &oid, 1); |
949 | ||
950 | if (result) { | |
951 | ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc); | |
952 | oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid)); | |
953 | ctx->oids.nr++; | |
3d5df01b DS |
954 | } |
955 | } | |
4c9efe85 DS |
956 | stop_progress(&ctx->progress); |
957 | strbuf_release(&progress_title); | |
958 | } | |
3d5df01b | 959 | |
b2c83060 DS |
960 | static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx) |
961 | { | |
962 | if (ctx->report_progress) | |
963 | ctx->progress = start_delayed_progress( | |
964 | _("Finding commits for commit graph among packed objects"), | |
965 | ctx->approx_nr_objects); | |
966 | for_each_packed_object(add_packed_commits, ctx, | |
967 | FOR_EACH_OBJECT_PACK_ORDER); | |
968 | if (ctx->progress_done < ctx->approx_nr_objects) | |
969 | display_progress(ctx->progress, ctx->approx_nr_objects); | |
970 | stop_progress(&ctx->progress); | |
971 | } | |
049d51a2 | 972 | |
014e3440 DS |
973 | static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx) |
974 | { | |
975 | uint32_t i, count_distinct = 1; | |
08fd81c9 | 976 | |
014e3440 DS |
977 | if (ctx->report_progress) |
978 | ctx->progress = start_delayed_progress( | |
890226cc | 979 | _("Counting distinct commits in commit graph"), |
014e3440 DS |
980 | ctx->oids.nr); |
981 | display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */ | |
982 | QSORT(ctx->oids.list, ctx->oids.nr, commit_compare); | |
983 | ||
984 | for (i = 1; i < ctx->oids.nr; i++) { | |
985 | display_progress(ctx->progress, i + 1); | |
986 | if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) | |
08fd81c9 DS |
987 | count_distinct++; |
988 | } | |
014e3440 | 989 | stop_progress(&ctx->progress); |
08fd81c9 | 990 | |
014e3440 DS |
991 | return count_distinct; |
992 | } | |
08fd81c9 | 993 | |
f998d542 DS |
994 | static void copy_oids_to_commits(struct write_commit_graph_context *ctx) |
995 | { | |
996 | uint32_t i; | |
997 | struct commit_list *parent; | |
08fd81c9 | 998 | |
f998d542 DS |
999 | ctx->num_extra_edges = 0; |
1000 | if (ctx->report_progress) | |
1001 | ctx->progress = start_delayed_progress( | |
890226cc | 1002 | _("Finding extra edges in commit graph"), |
f998d542 DS |
1003 | ctx->oids.nr); |
1004 | for (i = 0; i < ctx->oids.nr; i++) { | |
08fd81c9 | 1005 | int num_parents = 0; |
f998d542 DS |
1006 | display_progress(ctx->progress, i + 1); |
1007 | if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) | |
08fd81c9 DS |
1008 | continue; |
1009 | ||
f998d542 DS |
1010 | ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]); |
1011 | parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]); | |
08fd81c9 | 1012 | |
f998d542 | 1013 | for (parent = ctx->commits.list[ctx->commits.nr]->parents; |
08fd81c9 DS |
1014 | parent; parent = parent->next) |
1015 | num_parents++; | |
1016 | ||
1017 | if (num_parents > 2) | |
f998d542 | 1018 | ctx->num_extra_edges += num_parents - 1; |
08fd81c9 | 1019 | |
f998d542 | 1020 | ctx->commits.nr++; |
08fd81c9 | 1021 | } |
f998d542 DS |
1022 | stop_progress(&ctx->progress); |
1023 | } | |
08fd81c9 | 1024 | |
238def57 | 1025 | static int write_commit_graph_file(struct write_commit_graph_context *ctx) |
08fd81c9 | 1026 | { |
238def57 | 1027 | uint32_t i; |
08fd81c9 | 1028 | struct hashfile *f; |
08fd81c9 DS |
1029 | struct lock_file lk = LOCK_INIT; |
1030 | uint32_t chunk_ids[5]; | |
1031 | uint64_t chunk_offsets[5]; | |
c1665998 | 1032 | const unsigned hashsz = the_hash_algo->rawsz; |
28944739 | 1033 | struct strbuf progress_title = STRBUF_INIT; |
238def57 DS |
1034 | int num_chunks = ctx->num_extra_edges ? 4 : 3; |
1035 | ||
1036 | ctx->graph_name = get_commit_graph_filename(ctx->obj_dir); | |
1037 | if (safe_create_leading_directories(ctx->graph_name)) { | |
1038 | UNLEAK(ctx->graph_name); | |
1039 | error(_("unable to create leading directories of %s"), | |
1040 | ctx->graph_name); | |
1041 | return -1; | |
f4dbdfc4 | 1042 | } |
08fd81c9 | 1043 | |
238def57 | 1044 | hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR); |
08fd81c9 DS |
1045 | f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); |
1046 | ||
1047 | hashwrite_be32(f, GRAPH_SIGNATURE); | |
1048 | ||
1049 | hashwrite_u8(f, GRAPH_VERSION); | |
c1665998 | 1050 | hashwrite_u8(f, oid_version()); |
08fd81c9 DS |
1051 | hashwrite_u8(f, num_chunks); |
1052 | hashwrite_u8(f, 0); /* unused padding byte */ | |
1053 | ||
1054 | chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT; | |
1055 | chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP; | |
1056 | chunk_ids[2] = GRAPH_CHUNKID_DATA; | |
238def57 | 1057 | if (ctx->num_extra_edges) |
5af7417b | 1058 | chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES; |
08fd81c9 DS |
1059 | else |
1060 | chunk_ids[3] = 0; | |
1061 | chunk_ids[4] = 0; | |
1062 | ||
1063 | chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH; | |
1064 | chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE; | |
238def57 DS |
1065 | chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr; |
1066 | chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr; | |
1067 | chunk_offsets[4] = chunk_offsets[3] + 4 * ctx->num_extra_edges; | |
08fd81c9 DS |
1068 | |
1069 | for (i = 0; i <= num_chunks; i++) { | |
1070 | uint32_t chunk_write[3]; | |
1071 | ||
1072 | chunk_write[0] = htonl(chunk_ids[i]); | |
1073 | chunk_write[1] = htonl(chunk_offsets[i] >> 32); | |
1074 | chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff); | |
1075 | hashwrite(f, chunk_write, 12); | |
1076 | } | |
1077 | ||
238def57 | 1078 | if (ctx->report_progress) { |
28944739 ÆAB |
1079 | strbuf_addf(&progress_title, |
1080 | Q_("Writing out commit graph in %d pass", | |
1081 | "Writing out commit graph in %d passes", | |
1082 | num_chunks), | |
1083 | num_chunks); | |
238def57 | 1084 | ctx->progress = start_delayed_progress( |
28944739 | 1085 | progress_title.buf, |
238def57 | 1086 | num_chunks * ctx->commits.nr); |
28944739 | 1087 | } |
238def57 DS |
1088 | write_graph_chunk_fanout(f, ctx); |
1089 | write_graph_chunk_oids(f, hashsz, ctx); | |
1090 | write_graph_chunk_data(f, hashsz, ctx); | |
1091 | if (ctx->num_extra_edges) | |
1092 | write_graph_chunk_extra_edges(f, ctx); | |
1093 | stop_progress(&ctx->progress); | |
28944739 | 1094 | strbuf_release(&progress_title); |
08fd81c9 | 1095 | |
c3a3a964 | 1096 | close_commit_graph(ctx->r->objects); |
08fd81c9 DS |
1097 | finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC); |
1098 | commit_lock_file(&lk); | |
1099 | ||
238def57 DS |
1100 | return 0; |
1101 | } | |
1102 | ||
1103 | int write_commit_graph(const char *obj_dir, | |
1104 | struct string_list *pack_indexes, | |
1105 | struct string_list *commit_hex, | |
1106 | unsigned int flags) | |
1107 | { | |
1108 | struct write_commit_graph_context *ctx; | |
1109 | uint32_t i, count_distinct = 0; | |
e103f727 | 1110 | int res = 0; |
08fd81c9 | 1111 | |
d6538246 | 1112 | if (!commit_graph_compatible(the_repository)) |
e103f727 | 1113 | return 0; |
d6538246 | 1114 | |
c9905bea DS |
1115 | ctx = xcalloc(1, sizeof(struct write_commit_graph_context)); |
1116 | ctx->r = the_repository; | |
1117 | ctx->obj_dir = obj_dir; | |
1118 | ctx->append = flags & COMMIT_GRAPH_APPEND ? 1 : 0; | |
1119 | ctx->report_progress = flags & COMMIT_GRAPH_PROGRESS ? 1 : 0; | |
1120 | ||
1121 | ctx->approx_nr_objects = approximate_object_count(); | |
1122 | ctx->oids.alloc = ctx->approx_nr_objects / 32; | |
1123 | ||
1124 | if (ctx->append) { | |
1125 | prepare_commit_graph_one(ctx->r, ctx->obj_dir); | |
1126 | if (ctx->r->objects->commit_graph) | |
1127 | ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits; | |
7547b95b DS |
1128 | } |
1129 | ||
c9905bea DS |
1130 | if (ctx->oids.alloc < 1024) |
1131 | ctx->oids.alloc = 1024; | |
1132 | ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc); | |
1133 | ||
1134 | if (ctx->append && ctx->r->objects->commit_graph) { | |
1135 | struct commit_graph *g = ctx->r->objects->commit_graph; | |
1136 | for (i = 0; i < g->num_commits; i++) { | |
1137 | const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i; | |
1138 | hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash); | |
7547b95b DS |
1139 | } |
1140 | } | |
1141 | ||
049d51a2 | 1142 | if (pack_indexes) { |
ef5b83f2 DS |
1143 | if ((res = fill_oids_from_packs(ctx, pack_indexes))) |
1144 | goto cleanup; | |
3d5df01b DS |
1145 | } |
1146 | ||
4c9efe85 DS |
1147 | if (commit_hex) |
1148 | fill_oids_from_commit_hex(ctx, commit_hex); | |
3d5df01b | 1149 | |
b2c83060 DS |
1150 | if (!pack_indexes && !commit_hex) |
1151 | fill_oids_from_all_packs(ctx); | |
049d51a2 | 1152 | |
c9905bea | 1153 | close_reachable(ctx); |
08fd81c9 | 1154 | |
014e3440 | 1155 | count_distinct = count_distinct_commits(ctx); |
08fd81c9 | 1156 | |
e103f727 DS |
1157 | if (count_distinct >= GRAPH_EDGE_LAST_MASK) { |
1158 | error(_("the commit graph format cannot write %d commits"), count_distinct); | |
1159 | res = -1; | |
1160 | goto cleanup; | |
1161 | } | |
08fd81c9 | 1162 | |
c9905bea DS |
1163 | ctx->commits.alloc = count_distinct; |
1164 | ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc); | |
08fd81c9 | 1165 | |
f998d542 | 1166 | copy_oids_to_commits(ctx); |
08fd81c9 | 1167 | |
c9905bea | 1168 | if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) { |
e103f727 DS |
1169 | error(_("too many commits to write graph")); |
1170 | res = -1; | |
1171 | goto cleanup; | |
1172 | } | |
08fd81c9 | 1173 | |
c9905bea | 1174 | compute_generation_numbers(ctx); |
08fd81c9 | 1175 | |
238def57 | 1176 | res = write_commit_graph_file(ctx); |
08fd81c9 | 1177 | |
e103f727 | 1178 | cleanup: |
238def57 | 1179 | free(ctx->graph_name); |
c9905bea DS |
1180 | free(ctx->commits.list); |
1181 | free(ctx->oids.list); | |
1182 | free(ctx); | |
e103f727 DS |
1183 | |
1184 | return res; | |
08fd81c9 | 1185 | } |
283e68c7 | 1186 | |
41df0e30 | 1187 | #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2 |
283e68c7 DS |
1188 | static int verify_commit_graph_error; |
1189 | ||
1190 | static void graph_report(const char *fmt, ...) | |
1191 | { | |
1192 | va_list ap; | |
1193 | ||
1194 | verify_commit_graph_error = 1; | |
1195 | va_start(ap, fmt); | |
1196 | vfprintf(stderr, fmt, ap); | |
1197 | fprintf(stderr, "\n"); | |
1198 | va_end(ap); | |
1199 | } | |
1200 | ||
1373e547 DS |
1201 | #define GENERATION_ZERO_EXISTS 1 |
1202 | #define GENERATION_NUMBER_EXISTS 2 | |
1203 | ||
283e68c7 DS |
1204 | int verify_commit_graph(struct repository *r, struct commit_graph *g) |
1205 | { | |
9bda8467 | 1206 | uint32_t i, cur_fanout_pos = 0; |
41df0e30 | 1207 | struct object_id prev_oid, cur_oid, checksum; |
1373e547 | 1208 | int generation_zero = 0; |
41df0e30 DS |
1209 | struct hashfile *f; |
1210 | int devnull; | |
1f7f557f | 1211 | struct progress *progress = NULL; |
9bda8467 | 1212 | |
283e68c7 DS |
1213 | if (!g) { |
1214 | graph_report("no commit-graph file loaded"); | |
1215 | return 1; | |
1216 | } | |
1217 | ||
2ac138d5 | 1218 | verify_commit_graph_error = verify_commit_graph_lite(g); |
9bda8467 DS |
1219 | if (verify_commit_graph_error) |
1220 | return verify_commit_graph_error; | |
1221 | ||
41df0e30 DS |
1222 | devnull = open("/dev/null", O_WRONLY); |
1223 | f = hashfd(devnull, NULL); | |
1224 | hashwrite(f, g->data, g->data_len - g->hash_len); | |
1225 | finalize_hashfile(f, checksum.hash, CSUM_CLOSE); | |
67947c34 | 1226 | if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) { |
41df0e30 DS |
1227 | graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt")); |
1228 | verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH; | |
1229 | } | |
1230 | ||
9bda8467 | 1231 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 DS |
1232 | struct commit *graph_commit; |
1233 | ||
9bda8467 DS |
1234 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
1235 | ||
1236 | if (i && oidcmp(&prev_oid, &cur_oid) >= 0) | |
93b4405f | 1237 | graph_report(_("commit-graph has incorrect OID order: %s then %s"), |
9bda8467 DS |
1238 | oid_to_hex(&prev_oid), |
1239 | oid_to_hex(&cur_oid)); | |
1240 | ||
1241 | oidcpy(&prev_oid, &cur_oid); | |
1242 | ||
1243 | while (cur_oid.hash[0] > cur_fanout_pos) { | |
1244 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
1245 | ||
1246 | if (i != fanout_value) | |
93b4405f | 1247 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
1248 | cur_fanout_pos, fanout_value, i); |
1249 | cur_fanout_pos++; | |
1250 | } | |
2e3c0737 | 1251 | |
82952964 | 1252 | graph_commit = lookup_commit(r, &cur_oid); |
4f542b7a | 1253 | if (!parse_commit_in_graph_one(r, g, graph_commit)) |
93b4405f | 1254 | graph_report(_("failed to parse commit %s from commit-graph"), |
2e3c0737 | 1255 | oid_to_hex(&cur_oid)); |
9bda8467 DS |
1256 | } |
1257 | ||
1258 | while (cur_fanout_pos < 256) { | |
1259 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
1260 | ||
1261 | if (g->num_commits != fanout_value) | |
93b4405f | 1262 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
1263 | cur_fanout_pos, fanout_value, i); |
1264 | ||
1265 | cur_fanout_pos++; | |
1266 | } | |
1267 | ||
41df0e30 | 1268 | if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH) |
96af91d4 DS |
1269 | return verify_commit_graph_error; |
1270 | ||
1f7f557f ÆAB |
1271 | progress = start_progress(_("Verifying commits in commit graph"), |
1272 | g->num_commits); | |
96af91d4 | 1273 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 | 1274 | struct commit *graph_commit, *odb_commit; |
53614b13 | 1275 | struct commit_list *graph_parents, *odb_parents; |
1373e547 | 1276 | uint32_t max_generation = 0; |
96af91d4 | 1277 | |
1f7f557f | 1278 | display_progress(progress, i + 1); |
96af91d4 DS |
1279 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
1280 | ||
82952964 | 1281 | graph_commit = lookup_commit(r, &cur_oid); |
a378509e | 1282 | odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r)); |
96af91d4 | 1283 | if (parse_commit_internal(odb_commit, 0, 0)) { |
93b4405f | 1284 | graph_report(_("failed to parse commit %s from object database for commit-graph"), |
96af91d4 DS |
1285 | oid_to_hex(&cur_oid)); |
1286 | continue; | |
1287 | } | |
2e3c0737 | 1288 | |
4f542b7a | 1289 | if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, |
2e3c0737 | 1290 | get_commit_tree_oid(odb_commit))) |
93b4405f | 1291 | graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"), |
2e3c0737 DS |
1292 | oid_to_hex(&cur_oid), |
1293 | oid_to_hex(get_commit_tree_oid(graph_commit)), | |
1294 | oid_to_hex(get_commit_tree_oid(odb_commit))); | |
53614b13 DS |
1295 | |
1296 | graph_parents = graph_commit->parents; | |
1297 | odb_parents = odb_commit->parents; | |
1298 | ||
1299 | while (graph_parents) { | |
1300 | if (odb_parents == NULL) { | |
93b4405f | 1301 | graph_report(_("commit-graph parent list for commit %s is too long"), |
53614b13 DS |
1302 | oid_to_hex(&cur_oid)); |
1303 | break; | |
1304 | } | |
1305 | ||
9001dc2a | 1306 | if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) |
93b4405f | 1307 | graph_report(_("commit-graph parent for %s is %s != %s"), |
53614b13 DS |
1308 | oid_to_hex(&cur_oid), |
1309 | oid_to_hex(&graph_parents->item->object.oid), | |
1310 | oid_to_hex(&odb_parents->item->object.oid)); | |
1311 | ||
1373e547 DS |
1312 | if (graph_parents->item->generation > max_generation) |
1313 | max_generation = graph_parents->item->generation; | |
1314 | ||
53614b13 DS |
1315 | graph_parents = graph_parents->next; |
1316 | odb_parents = odb_parents->next; | |
1317 | } | |
1318 | ||
1319 | if (odb_parents != NULL) | |
93b4405f | 1320 | graph_report(_("commit-graph parent list for commit %s terminates early"), |
53614b13 | 1321 | oid_to_hex(&cur_oid)); |
1373e547 DS |
1322 | |
1323 | if (!graph_commit->generation) { | |
1324 | if (generation_zero == GENERATION_NUMBER_EXISTS) | |
93b4405f | 1325 | graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"), |
1373e547 DS |
1326 | oid_to_hex(&cur_oid)); |
1327 | generation_zero = GENERATION_ZERO_EXISTS; | |
1328 | } else if (generation_zero == GENERATION_ZERO_EXISTS) | |
93b4405f | 1329 | graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"), |
1373e547 DS |
1330 | oid_to_hex(&cur_oid)); |
1331 | ||
1332 | if (generation_zero == GENERATION_ZERO_EXISTS) | |
1333 | continue; | |
1334 | ||
1335 | /* | |
1336 | * If one of our parents has generation GENERATION_NUMBER_MAX, then | |
1337 | * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid | |
1338 | * extra logic in the following condition. | |
1339 | */ | |
1340 | if (max_generation == GENERATION_NUMBER_MAX) | |
1341 | max_generation--; | |
1342 | ||
1343 | if (graph_commit->generation != max_generation + 1) | |
93b4405f | 1344 | graph_report(_("commit-graph generation for commit %s is %u != %u"), |
1373e547 DS |
1345 | oid_to_hex(&cur_oid), |
1346 | graph_commit->generation, | |
1347 | max_generation + 1); | |
88968ebf DS |
1348 | |
1349 | if (graph_commit->date != odb_commit->date) | |
93b4405f | 1350 | graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime), |
88968ebf DS |
1351 | oid_to_hex(&cur_oid), |
1352 | graph_commit->date, | |
1353 | odb_commit->date); | |
96af91d4 | 1354 | } |
1f7f557f | 1355 | stop_progress(&progress); |
96af91d4 | 1356 | |
283e68c7 DS |
1357 | return verify_commit_graph_error; |
1358 | } | |
c3756d5b JT |
1359 | |
1360 | void free_commit_graph(struct commit_graph *g) | |
1361 | { | |
1362 | if (!g) | |
1363 | return; | |
1364 | if (g->graph_fd >= 0) { | |
1365 | munmap((void *)g->data, g->data_len); | |
1366 | g->data = NULL; | |
1367 | close(g->graph_fd); | |
1368 | } | |
1369 | free(g); | |
1370 | } |