]>
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 | ||
829a3215 | 364 | void close_commit_graph(struct repository *r) |
177722b3 | 365 | { |
829a3215 DS |
366 | free_commit_graph(r->objects->commit_graph); |
367 | r->objects->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 | ||
08fd81c9 DS |
528 | static void write_graph_chunk_fanout(struct hashfile *f, |
529 | struct commit **commits, | |
53035c4f ÆAB |
530 | int nr_commits, |
531 | struct progress *progress, | |
532 | uint64_t *progress_cnt) | |
08fd81c9 DS |
533 | { |
534 | int i, count = 0; | |
535 | struct commit **list = commits; | |
536 | ||
537 | /* | |
538 | * Write the first-level table (the list is sorted, | |
539 | * but we use a 256-entry lookup to be able to avoid | |
540 | * having to do eight extra binary search iterations). | |
541 | */ | |
542 | for (i = 0; i < 256; i++) { | |
543 | while (count < nr_commits) { | |
544 | if ((*list)->object.oid.hash[0] != i) | |
545 | break; | |
53035c4f | 546 | display_progress(progress, ++*progress_cnt); |
08fd81c9 DS |
547 | count++; |
548 | list++; | |
549 | } | |
550 | ||
551 | hashwrite_be32(f, count); | |
552 | } | |
553 | } | |
554 | ||
555 | static void write_graph_chunk_oids(struct hashfile *f, int hash_len, | |
53035c4f ÆAB |
556 | struct commit **commits, int nr_commits, |
557 | struct progress *progress, | |
558 | uint64_t *progress_cnt) | |
08fd81c9 DS |
559 | { |
560 | struct commit **list = commits; | |
561 | int count; | |
53035c4f ÆAB |
562 | for (count = 0; count < nr_commits; count++, list++) { |
563 | display_progress(progress, ++*progress_cnt); | |
08fd81c9 | 564 | hashwrite(f, (*list)->object.oid.hash, (int)hash_len); |
53035c4f | 565 | } |
08fd81c9 DS |
566 | } |
567 | ||
568 | static const unsigned char *commit_to_sha1(size_t index, void *table) | |
569 | { | |
570 | struct commit **commits = table; | |
571 | return commits[index]->object.oid.hash; | |
572 | } | |
573 | ||
574 | static void write_graph_chunk_data(struct hashfile *f, int hash_len, | |
53035c4f ÆAB |
575 | struct commit **commits, int nr_commits, |
576 | struct progress *progress, | |
577 | uint64_t *progress_cnt) | |
08fd81c9 DS |
578 | { |
579 | struct commit **list = commits; | |
580 | struct commit **last = commits + nr_commits; | |
581 | uint32_t num_extra_edges = 0; | |
582 | ||
583 | while (list < last) { | |
584 | struct commit_list *parent; | |
585 | int edge_value; | |
586 | uint32_t packedDate[2]; | |
53035c4f | 587 | display_progress(progress, ++*progress_cnt); |
08fd81c9 | 588 | |
43d35618 | 589 | parse_commit_no_graph(*list); |
2e27bd77 | 590 | hashwrite(f, get_commit_tree_oid(*list)->hash, hash_len); |
08fd81c9 DS |
591 | |
592 | parent = (*list)->parents; | |
593 | ||
594 | if (!parent) | |
595 | edge_value = GRAPH_PARENT_NONE; | |
596 | else { | |
597 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
598 | commits, | |
599 | nr_commits, | |
600 | commit_to_sha1); | |
601 | ||
602 | if (edge_value < 0) | |
cce99cd8 DS |
603 | BUG("missing parent %s for commit %s", |
604 | oid_to_hex(&parent->item->object.oid), | |
605 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
606 | } |
607 | ||
608 | hashwrite_be32(f, edge_value); | |
609 | ||
610 | if (parent) | |
611 | parent = parent->next; | |
612 | ||
613 | if (!parent) | |
614 | edge_value = GRAPH_PARENT_NONE; | |
615 | else if (parent->next) | |
5af7417b | 616 | edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; |
08fd81c9 DS |
617 | else { |
618 | edge_value = sha1_pos(parent->item->object.oid.hash, | |
619 | commits, | |
620 | nr_commits, | |
621 | commit_to_sha1); | |
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 | ||
5af7417b | 630 | if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) { |
08fd81c9 DS |
631 | do { |
632 | num_extra_edges++; | |
633 | parent = parent->next; | |
634 | } while (parent); | |
635 | } | |
636 | ||
637 | if (sizeof((*list)->date) > 4) | |
638 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); | |
639 | else | |
640 | packedDate[0] = 0; | |
641 | ||
3258c663 DS |
642 | packedDate[0] |= htonl((*list)->generation << 2); |
643 | ||
08fd81c9 DS |
644 | packedDate[1] = htonl((*list)->date); |
645 | hashwrite(f, packedDate, 8); | |
646 | ||
647 | list++; | |
648 | } | |
649 | } | |
650 | ||
5af7417b | 651 | static void write_graph_chunk_extra_edges(struct hashfile *f, |
08fd81c9 | 652 | struct commit **commits, |
53035c4f ÆAB |
653 | int nr_commits, |
654 | struct progress *progress, | |
655 | uint64_t *progress_cnt) | |
08fd81c9 DS |
656 | { |
657 | struct commit **list = commits; | |
658 | struct commit **last = commits + nr_commits; | |
659 | struct commit_list *parent; | |
660 | ||
661 | while (list < last) { | |
662 | int num_parents = 0; | |
53035c4f ÆAB |
663 | |
664 | display_progress(progress, ++*progress_cnt); | |
665 | ||
08fd81c9 DS |
666 | for (parent = (*list)->parents; num_parents < 3 && parent; |
667 | parent = parent->next) | |
668 | num_parents++; | |
669 | ||
670 | if (num_parents <= 2) { | |
671 | list++; | |
672 | continue; | |
673 | } | |
674 | ||
675 | /* Since num_parents > 2, this initializer is safe. */ | |
676 | for (parent = (*list)->parents->next; parent; parent = parent->next) { | |
677 | int edge_value = sha1_pos(parent->item->object.oid.hash, | |
678 | commits, | |
679 | nr_commits, | |
680 | commit_to_sha1); | |
681 | ||
682 | if (edge_value < 0) | |
cce99cd8 DS |
683 | BUG("missing parent %s for commit %s", |
684 | oid_to_hex(&parent->item->object.oid), | |
685 | oid_to_hex(&(*list)->object.oid)); | |
08fd81c9 DS |
686 | else if (!parent->next) |
687 | edge_value |= GRAPH_LAST_EDGE; | |
688 | ||
689 | hashwrite_be32(f, edge_value); | |
690 | } | |
691 | ||
692 | list++; | |
693 | } | |
694 | } | |
695 | ||
696 | static int commit_compare(const void *_a, const void *_b) | |
697 | { | |
698 | const struct object_id *a = (const struct object_id *)_a; | |
699 | const struct object_id *b = (const struct object_id *)_b; | |
700 | return oidcmp(a, b); | |
701 | } | |
702 | ||
703 | struct packed_commit_list { | |
704 | struct commit **list; | |
705 | int nr; | |
706 | int alloc; | |
707 | }; | |
708 | ||
709 | struct packed_oid_list { | |
710 | struct object_id *list; | |
711 | int nr; | |
712 | int alloc; | |
7b0f2292 ÆAB |
713 | struct progress *progress; |
714 | int progress_done; | |
08fd81c9 DS |
715 | }; |
716 | ||
717 | static int add_packed_commits(const struct object_id *oid, | |
718 | struct packed_git *pack, | |
719 | uint32_t pos, | |
720 | void *data) | |
721 | { | |
722 | struct packed_oid_list *list = (struct packed_oid_list*)data; | |
723 | enum object_type type; | |
724 | off_t offset = nth_packed_object_offset(pack, pos); | |
725 | struct object_info oi = OBJECT_INFO_INIT; | |
726 | ||
7b0f2292 ÆAB |
727 | if (list->progress) |
728 | display_progress(list->progress, ++list->progress_done); | |
729 | ||
08fd81c9 | 730 | oi.typep = &type; |
fcb6df32 | 731 | if (packed_object_info(the_repository, pack, offset, &oi) < 0) |
4f5b532d | 732 | die(_("unable to get type of object %s"), oid_to_hex(oid)); |
08fd81c9 DS |
733 | |
734 | if (type != OBJ_COMMIT) | |
735 | return 0; | |
736 | ||
737 | ALLOC_GROW(list->list, list->nr + 1, list->alloc); | |
738 | oidcpy(&(list->list[list->nr]), oid); | |
739 | list->nr++; | |
740 | ||
741 | return 0; | |
742 | } | |
743 | ||
4f2542b4 DS |
744 | static void add_missing_parents(struct packed_oid_list *oids, struct commit *commit) |
745 | { | |
746 | struct commit_list *parent; | |
747 | for (parent = commit->parents; parent; parent = parent->next) { | |
748 | if (!(parent->item->object.flags & UNINTERESTING)) { | |
749 | ALLOC_GROW(oids->list, oids->nr + 1, oids->alloc); | |
750 | oidcpy(&oids->list[oids->nr], &(parent->item->object.oid)); | |
751 | oids->nr++; | |
752 | parent->item->object.flags |= UNINTERESTING; | |
753 | } | |
754 | } | |
755 | } | |
756 | ||
7b0f2292 | 757 | static void close_reachable(struct packed_oid_list *oids, int report_progress) |
4f2542b4 | 758 | { |
49bbc57a | 759 | int i; |
4f2542b4 | 760 | struct commit *commit; |
7b0f2292 | 761 | struct progress *progress = NULL; |
4f2542b4 | 762 | |
7b0f2292 ÆAB |
763 | if (report_progress) |
764 | progress = start_delayed_progress( | |
49bbc57a | 765 | _("Loading known commits in commit graph"), oids->nr); |
4f2542b4 | 766 | for (i = 0; i < oids->nr; i++) { |
49bbc57a | 767 | display_progress(progress, i + 1); |
c1f5eb49 | 768 | commit = lookup_commit(the_repository, &oids->list[i]); |
4f2542b4 DS |
769 | if (commit) |
770 | commit->object.flags |= UNINTERESTING; | |
771 | } | |
01ca3877 | 772 | stop_progress(&progress); |
4f2542b4 DS |
773 | |
774 | /* | |
775 | * As this loop runs, oids->nr may grow, but not more | |
776 | * than the number of missing commits in the reachable | |
777 | * closure. | |
778 | */ | |
01ca3877 ÆAB |
779 | if (report_progress) |
780 | progress = start_delayed_progress( | |
49bbc57a | 781 | _("Expanding reachable commits in commit graph"), oids->nr); |
4f2542b4 | 782 | for (i = 0; i < oids->nr; i++) { |
49bbc57a | 783 | display_progress(progress, i + 1); |
c1f5eb49 | 784 | commit = lookup_commit(the_repository, &oids->list[i]); |
4f2542b4 | 785 | |
43d35618 | 786 | if (commit && !parse_commit_no_graph(commit)) |
4f2542b4 DS |
787 | add_missing_parents(oids, commit); |
788 | } | |
01ca3877 | 789 | stop_progress(&progress); |
4f2542b4 | 790 | |
01ca3877 ÆAB |
791 | if (report_progress) |
792 | progress = start_delayed_progress( | |
49bbc57a | 793 | _("Clearing commit marks in commit graph"), oids->nr); |
4f2542b4 | 794 | for (i = 0; i < oids->nr; i++) { |
49bbc57a | 795 | display_progress(progress, i + 1); |
c1f5eb49 | 796 | commit = lookup_commit(the_repository, &oids->list[i]); |
4f2542b4 DS |
797 | |
798 | if (commit) | |
799 | commit->object.flags &= ~UNINTERESTING; | |
800 | } | |
7b0f2292 | 801 | stop_progress(&progress); |
4f2542b4 DS |
802 | } |
803 | ||
7b0f2292 ÆAB |
804 | static void compute_generation_numbers(struct packed_commit_list* commits, |
805 | int report_progress) | |
3258c663 DS |
806 | { |
807 | int i; | |
808 | struct commit_list *list = NULL; | |
7b0f2292 | 809 | struct progress *progress = NULL; |
3258c663 | 810 | |
7b0f2292 ÆAB |
811 | if (report_progress) |
812 | progress = start_progress( | |
813 | _("Computing commit graph generation numbers"), | |
814 | commits->nr); | |
3258c663 | 815 | for (i = 0; i < commits->nr; i++) { |
7b0f2292 | 816 | display_progress(progress, i + 1); |
3258c663 DS |
817 | if (commits->list[i]->generation != GENERATION_NUMBER_INFINITY && |
818 | commits->list[i]->generation != GENERATION_NUMBER_ZERO) | |
819 | continue; | |
820 | ||
821 | commit_list_insert(commits->list[i], &list); | |
822 | while (list) { | |
823 | struct commit *current = list->item; | |
824 | struct commit_list *parent; | |
825 | int all_parents_computed = 1; | |
826 | uint32_t max_generation = 0; | |
827 | ||
828 | for (parent = current->parents; parent; parent = parent->next) { | |
829 | if (parent->item->generation == GENERATION_NUMBER_INFINITY || | |
830 | parent->item->generation == GENERATION_NUMBER_ZERO) { | |
831 | all_parents_computed = 0; | |
832 | commit_list_insert(parent->item, &list); | |
833 | break; | |
834 | } else if (parent->item->generation > max_generation) { | |
835 | max_generation = parent->item->generation; | |
836 | } | |
837 | } | |
838 | ||
839 | if (all_parents_computed) { | |
840 | current->generation = max_generation + 1; | |
841 | pop_commit(&list); | |
842 | ||
843 | if (current->generation > GENERATION_NUMBER_MAX) | |
844 | current->generation = GENERATION_NUMBER_MAX; | |
845 | } | |
846 | } | |
847 | } | |
7b0f2292 | 848 | stop_progress(&progress); |
3258c663 DS |
849 | } |
850 | ||
59fb8770 DS |
851 | static int add_ref_to_list(const char *refname, |
852 | const struct object_id *oid, | |
853 | int flags, void *cb_data) | |
854 | { | |
855 | struct string_list *list = (struct string_list *)cb_data; | |
856 | ||
857 | string_list_append(list, oid_to_hex(oid)); | |
858 | return 0; | |
859 | } | |
860 | ||
7b0f2292 ÆAB |
861 | void write_commit_graph_reachable(const char *obj_dir, int append, |
862 | int report_progress) | |
59fb8770 | 863 | { |
f4dbdfc4 | 864 | struct string_list list = STRING_LIST_INIT_DUP; |
59fb8770 | 865 | |
59fb8770 | 866 | for_each_ref(add_ref_to_list, &list); |
7b0f2292 | 867 | write_commit_graph(obj_dir, NULL, &list, append, report_progress); |
f4dbdfc4 DS |
868 | |
869 | string_list_clear(&list, 0); | |
59fb8770 DS |
870 | } |
871 | ||
049d51a2 | 872 | void write_commit_graph(const char *obj_dir, |
d88b14b3 DS |
873 | struct string_list *pack_indexes, |
874 | struct string_list *commit_hex, | |
7b0f2292 | 875 | int append, int report_progress) |
08fd81c9 DS |
876 | { |
877 | struct packed_oid_list oids; | |
878 | struct packed_commit_list commits; | |
879 | struct hashfile *f; | |
880 | uint32_t i, count_distinct = 0; | |
881 | char *graph_name; | |
08fd81c9 DS |
882 | struct lock_file lk = LOCK_INIT; |
883 | uint32_t chunk_ids[5]; | |
884 | uint64_t chunk_offsets[5]; | |
885 | int num_chunks; | |
886 | int num_extra_edges; | |
887 | struct commit_list *parent; | |
7b0f2292 | 888 | struct progress *progress = NULL; |
c1665998 | 889 | const unsigned hashsz = the_hash_algo->rawsz; |
53035c4f | 890 | uint64_t progress_cnt = 0; |
28944739 | 891 | struct strbuf progress_title = STRBUF_INIT; |
d9b1b309 | 892 | unsigned long approx_nr_objects; |
08fd81c9 | 893 | |
d6538246 DS |
894 | if (!commit_graph_compatible(the_repository)) |
895 | return; | |
896 | ||
08fd81c9 | 897 | oids.nr = 0; |
d9b1b309 ÆAB |
898 | approx_nr_objects = approximate_object_count(); |
899 | oids.alloc = approx_nr_objects / 32; | |
7b0f2292 ÆAB |
900 | oids.progress = NULL; |
901 | oids.progress_done = 0; | |
08fd81c9 | 902 | |
7547b95b | 903 | if (append) { |
dade47c0 | 904 | prepare_commit_graph_one(the_repository, obj_dir); |
85277506 JT |
905 | if (the_repository->objects->commit_graph) |
906 | oids.alloc += the_repository->objects->commit_graph->num_commits; | |
7547b95b DS |
907 | } |
908 | ||
08fd81c9 DS |
909 | if (oids.alloc < 1024) |
910 | oids.alloc = 1024; | |
911 | ALLOC_ARRAY(oids.list, oids.alloc); | |
912 | ||
85277506 JT |
913 | if (append && the_repository->objects->commit_graph) { |
914 | struct commit_graph *commit_graph = | |
915 | the_repository->objects->commit_graph; | |
7547b95b DS |
916 | for (i = 0; i < commit_graph->num_commits; i++) { |
917 | const unsigned char *hash = commit_graph->chunk_oid_lookup + | |
918 | commit_graph->hash_len * i; | |
919 | hashcpy(oids.list[oids.nr++].hash, hash); | |
920 | } | |
921 | } | |
922 | ||
049d51a2 DS |
923 | if (pack_indexes) { |
924 | struct strbuf packname = STRBUF_INIT; | |
925 | int dirlen; | |
926 | strbuf_addf(&packname, "%s/pack/", obj_dir); | |
927 | dirlen = packname.len; | |
7b0f2292 | 928 | if (report_progress) { |
7c7b8a7f ÆAB |
929 | strbuf_addf(&progress_title, |
930 | Q_("Finding commits for commit graph in %d pack", | |
931 | "Finding commits for commit graph in %d packs", | |
932 | pack_indexes->nr), | |
933 | pack_indexes->nr); | |
934 | oids.progress = start_delayed_progress(progress_title.buf, 0); | |
7b0f2292 ÆAB |
935 | oids.progress_done = 0; |
936 | } | |
d88b14b3 | 937 | for (i = 0; i < pack_indexes->nr; i++) { |
049d51a2 DS |
938 | struct packed_git *p; |
939 | strbuf_setlen(&packname, dirlen); | |
d88b14b3 | 940 | strbuf_addstr(&packname, pack_indexes->items[i].string); |
049d51a2 DS |
941 | p = add_packed_git(packname.buf, packname.len, 1); |
942 | if (!p) | |
4f5b532d | 943 | die(_("error adding pack %s"), packname.buf); |
049d51a2 | 944 | if (open_pack_index(p)) |
4f5b532d | 945 | die(_("error opening index for %s"), packname.buf); |
d7574c95 ÆAB |
946 | for_each_object_in_pack(p, add_packed_commits, &oids, |
947 | FOR_EACH_OBJECT_PACK_ORDER); | |
049d51a2 | 948 | close_pack(p); |
f4dbdfc4 | 949 | free(p); |
049d51a2 | 950 | } |
7b0f2292 | 951 | stop_progress(&oids.progress); |
7c7b8a7f | 952 | strbuf_reset(&progress_title); |
049d51a2 | 953 | strbuf_release(&packname); |
3d5df01b DS |
954 | } |
955 | ||
956 | if (commit_hex) { | |
7c7b8a7f ÆAB |
957 | if (report_progress) { |
958 | strbuf_addf(&progress_title, | |
959 | Q_("Finding commits for commit graph from %d ref", | |
960 | "Finding commits for commit graph from %d refs", | |
961 | commit_hex->nr), | |
962 | commit_hex->nr); | |
963 | progress = start_delayed_progress(progress_title.buf, | |
964 | commit_hex->nr); | |
965 | } | |
d88b14b3 | 966 | for (i = 0; i < commit_hex->nr; i++) { |
3d5df01b DS |
967 | const char *end; |
968 | struct object_id oid; | |
969 | struct commit *result; | |
970 | ||
7b0f2292 | 971 | display_progress(progress, i + 1); |
d88b14b3 DS |
972 | if (commit_hex->items[i].string && |
973 | parse_oid_hex(commit_hex->items[i].string, &oid, &end)) | |
3d5df01b DS |
974 | continue; |
975 | ||
21e1ee8f | 976 | result = lookup_commit_reference_gently(the_repository, &oid, 1); |
3d5df01b DS |
977 | |
978 | if (result) { | |
979 | ALLOC_GROW(oids.list, oids.nr + 1, oids.alloc); | |
980 | oidcpy(&oids.list[oids.nr], &(result->object.oid)); | |
981 | oids.nr++; | |
982 | } | |
983 | } | |
7b0f2292 | 984 | stop_progress(&progress); |
7c7b8a7f | 985 | strbuf_reset(&progress_title); |
3d5df01b DS |
986 | } |
987 | ||
7b0f2292 ÆAB |
988 | if (!pack_indexes && !commit_hex) { |
989 | if (report_progress) | |
990 | oids.progress = start_delayed_progress( | |
7c7b8a7f | 991 | _("Finding commits for commit graph among packed objects"), |
d9b1b309 | 992 | approx_nr_objects); |
d7574c95 ÆAB |
993 | for_each_packed_object(add_packed_commits, &oids, |
994 | FOR_EACH_OBJECT_PACK_ORDER); | |
d9b1b309 ÆAB |
995 | if (oids.progress_done < approx_nr_objects) |
996 | display_progress(oids.progress, approx_nr_objects); | |
7b0f2292 ÆAB |
997 | stop_progress(&oids.progress); |
998 | } | |
049d51a2 | 999 | |
7b0f2292 | 1000 | close_reachable(&oids, report_progress); |
08fd81c9 | 1001 | |
890226cc ÆAB |
1002 | if (report_progress) |
1003 | progress = start_delayed_progress( | |
1004 | _("Counting distinct commits in commit graph"), | |
1005 | oids.nr); | |
1006 | display_progress(progress, 0); /* TODO: Measure QSORT() progress */ | |
08fd81c9 | 1007 | QSORT(oids.list, oids.nr, commit_compare); |
08fd81c9 DS |
1008 | count_distinct = 1; |
1009 | for (i = 1; i < oids.nr; i++) { | |
890226cc | 1010 | display_progress(progress, i + 1); |
9001dc2a | 1011 | if (!oideq(&oids.list[i - 1], &oids.list[i])) |
08fd81c9 DS |
1012 | count_distinct++; |
1013 | } | |
890226cc | 1014 | stop_progress(&progress); |
08fd81c9 | 1015 | |
cce99cd8 | 1016 | if (count_distinct >= GRAPH_EDGE_LAST_MASK) |
08fd81c9 DS |
1017 | die(_("the commit graph format cannot write %d commits"), count_distinct); |
1018 | ||
1019 | commits.nr = 0; | |
1020 | commits.alloc = count_distinct; | |
1021 | ALLOC_ARRAY(commits.list, commits.alloc); | |
1022 | ||
1023 | num_extra_edges = 0; | |
890226cc ÆAB |
1024 | if (report_progress) |
1025 | progress = start_delayed_progress( | |
1026 | _("Finding extra edges in commit graph"), | |
1027 | oids.nr); | |
08fd81c9 DS |
1028 | for (i = 0; i < oids.nr; i++) { |
1029 | int num_parents = 0; | |
890226cc | 1030 | display_progress(progress, i + 1); |
4a7e27e9 | 1031 | if (i > 0 && oideq(&oids.list[i - 1], &oids.list[i])) |
08fd81c9 DS |
1032 | continue; |
1033 | ||
c1f5eb49 | 1034 | commits.list[commits.nr] = lookup_commit(the_repository, &oids.list[i]); |
43d35618 | 1035 | parse_commit_no_graph(commits.list[commits.nr]); |
08fd81c9 DS |
1036 | |
1037 | for (parent = commits.list[commits.nr]->parents; | |
1038 | parent; parent = parent->next) | |
1039 | num_parents++; | |
1040 | ||
1041 | if (num_parents > 2) | |
1042 | num_extra_edges += num_parents - 1; | |
1043 | ||
1044 | commits.nr++; | |
1045 | } | |
1046 | num_chunks = num_extra_edges ? 4 : 3; | |
890226cc | 1047 | stop_progress(&progress); |
08fd81c9 | 1048 | |
cce99cd8 | 1049 | if (commits.nr >= GRAPH_EDGE_LAST_MASK) |
08fd81c9 DS |
1050 | die(_("too many commits to write graph")); |
1051 | ||
7b0f2292 | 1052 | compute_generation_numbers(&commits, report_progress); |
08fd81c9 | 1053 | |
08fd81c9 | 1054 | graph_name = get_commit_graph_filename(obj_dir); |
f4dbdfc4 DS |
1055 | if (safe_create_leading_directories(graph_name)) { |
1056 | UNLEAK(graph_name); | |
33286dcd DS |
1057 | die_errno(_("unable to create leading directories of %s"), |
1058 | graph_name); | |
f4dbdfc4 | 1059 | } |
08fd81c9 | 1060 | |
33286dcd | 1061 | hold_lock_file_for_update(&lk, graph_name, LOCK_DIE_ON_ERROR); |
08fd81c9 DS |
1062 | f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf); |
1063 | ||
1064 | hashwrite_be32(f, GRAPH_SIGNATURE); | |
1065 | ||
1066 | hashwrite_u8(f, GRAPH_VERSION); | |
c1665998 | 1067 | hashwrite_u8(f, oid_version()); |
08fd81c9 DS |
1068 | hashwrite_u8(f, num_chunks); |
1069 | hashwrite_u8(f, 0); /* unused padding byte */ | |
1070 | ||
1071 | chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT; | |
1072 | chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP; | |
1073 | chunk_ids[2] = GRAPH_CHUNKID_DATA; | |
1074 | if (num_extra_edges) | |
5af7417b | 1075 | chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES; |
08fd81c9 DS |
1076 | else |
1077 | chunk_ids[3] = 0; | |
1078 | chunk_ids[4] = 0; | |
1079 | ||
1080 | chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH; | |
1081 | chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE; | |
c1665998 | 1082 | chunk_offsets[2] = chunk_offsets[1] + hashsz * commits.nr; |
1083 | chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * commits.nr; | |
08fd81c9 DS |
1084 | chunk_offsets[4] = chunk_offsets[3] + 4 * num_extra_edges; |
1085 | ||
1086 | for (i = 0; i <= num_chunks; i++) { | |
1087 | uint32_t chunk_write[3]; | |
1088 | ||
1089 | chunk_write[0] = htonl(chunk_ids[i]); | |
1090 | chunk_write[1] = htonl(chunk_offsets[i] >> 32); | |
1091 | chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff); | |
1092 | hashwrite(f, chunk_write, 12); | |
1093 | } | |
1094 | ||
28944739 ÆAB |
1095 | if (report_progress) { |
1096 | strbuf_addf(&progress_title, | |
1097 | Q_("Writing out commit graph in %d pass", | |
1098 | "Writing out commit graph in %d passes", | |
1099 | num_chunks), | |
1100 | num_chunks); | |
53035c4f | 1101 | progress = start_delayed_progress( |
28944739 | 1102 | progress_title.buf, |
53035c4f | 1103 | num_chunks * commits.nr); |
28944739 | 1104 | } |
53035c4f | 1105 | write_graph_chunk_fanout(f, commits.list, commits.nr, progress, &progress_cnt); |
e5eac573 JH |
1106 | write_graph_chunk_oids(f, hashsz, commits.list, commits.nr, progress, &progress_cnt); |
1107 | write_graph_chunk_data(f, hashsz, commits.list, commits.nr, progress, &progress_cnt); | |
857ba928 | 1108 | if (num_extra_edges) |
53035c4f ÆAB |
1109 | write_graph_chunk_extra_edges(f, commits.list, commits.nr, progress, &progress_cnt); |
1110 | stop_progress(&progress); | |
28944739 | 1111 | strbuf_release(&progress_title); |
08fd81c9 | 1112 | |
829a3215 | 1113 | close_commit_graph(the_repository); |
08fd81c9 DS |
1114 | finalize_hashfile(f, NULL, CSUM_HASH_IN_STREAM | CSUM_FSYNC); |
1115 | commit_lock_file(&lk); | |
1116 | ||
f4dbdfc4 DS |
1117 | free(graph_name); |
1118 | free(commits.list); | |
08fd81c9 | 1119 | free(oids.list); |
08fd81c9 | 1120 | } |
283e68c7 | 1121 | |
41df0e30 | 1122 | #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2 |
283e68c7 DS |
1123 | static int verify_commit_graph_error; |
1124 | ||
1125 | static void graph_report(const char *fmt, ...) | |
1126 | { | |
1127 | va_list ap; | |
1128 | ||
1129 | verify_commit_graph_error = 1; | |
1130 | va_start(ap, fmt); | |
1131 | vfprintf(stderr, fmt, ap); | |
1132 | fprintf(stderr, "\n"); | |
1133 | va_end(ap); | |
1134 | } | |
1135 | ||
1373e547 DS |
1136 | #define GENERATION_ZERO_EXISTS 1 |
1137 | #define GENERATION_NUMBER_EXISTS 2 | |
1138 | ||
283e68c7 DS |
1139 | int verify_commit_graph(struct repository *r, struct commit_graph *g) |
1140 | { | |
9bda8467 | 1141 | uint32_t i, cur_fanout_pos = 0; |
41df0e30 | 1142 | struct object_id prev_oid, cur_oid, checksum; |
1373e547 | 1143 | int generation_zero = 0; |
41df0e30 DS |
1144 | struct hashfile *f; |
1145 | int devnull; | |
1f7f557f | 1146 | struct progress *progress = NULL; |
9bda8467 | 1147 | |
283e68c7 DS |
1148 | if (!g) { |
1149 | graph_report("no commit-graph file loaded"); | |
1150 | return 1; | |
1151 | } | |
1152 | ||
2ac138d5 | 1153 | verify_commit_graph_error = verify_commit_graph_lite(g); |
9bda8467 DS |
1154 | if (verify_commit_graph_error) |
1155 | return verify_commit_graph_error; | |
1156 | ||
41df0e30 DS |
1157 | devnull = open("/dev/null", O_WRONLY); |
1158 | f = hashfd(devnull, NULL); | |
1159 | hashwrite(f, g->data, g->data_len - g->hash_len); | |
1160 | finalize_hashfile(f, checksum.hash, CSUM_CLOSE); | |
67947c34 | 1161 | if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) { |
41df0e30 DS |
1162 | graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt")); |
1163 | verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH; | |
1164 | } | |
1165 | ||
9bda8467 | 1166 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 DS |
1167 | struct commit *graph_commit; |
1168 | ||
9bda8467 DS |
1169 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
1170 | ||
1171 | if (i && oidcmp(&prev_oid, &cur_oid) >= 0) | |
93b4405f | 1172 | graph_report(_("commit-graph has incorrect OID order: %s then %s"), |
9bda8467 DS |
1173 | oid_to_hex(&prev_oid), |
1174 | oid_to_hex(&cur_oid)); | |
1175 | ||
1176 | oidcpy(&prev_oid, &cur_oid); | |
1177 | ||
1178 | while (cur_oid.hash[0] > cur_fanout_pos) { | |
1179 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
1180 | ||
1181 | if (i != fanout_value) | |
93b4405f | 1182 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
1183 | cur_fanout_pos, fanout_value, i); |
1184 | cur_fanout_pos++; | |
1185 | } | |
2e3c0737 | 1186 | |
82952964 | 1187 | graph_commit = lookup_commit(r, &cur_oid); |
4f542b7a | 1188 | if (!parse_commit_in_graph_one(r, g, graph_commit)) |
93b4405f | 1189 | graph_report(_("failed to parse commit %s from commit-graph"), |
2e3c0737 | 1190 | oid_to_hex(&cur_oid)); |
9bda8467 DS |
1191 | } |
1192 | ||
1193 | while (cur_fanout_pos < 256) { | |
1194 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); | |
1195 | ||
1196 | if (g->num_commits != fanout_value) | |
93b4405f | 1197 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
9bda8467 DS |
1198 | cur_fanout_pos, fanout_value, i); |
1199 | ||
1200 | cur_fanout_pos++; | |
1201 | } | |
1202 | ||
41df0e30 | 1203 | if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH) |
96af91d4 DS |
1204 | return verify_commit_graph_error; |
1205 | ||
1f7f557f ÆAB |
1206 | progress = start_progress(_("Verifying commits in commit graph"), |
1207 | g->num_commits); | |
96af91d4 | 1208 | for (i = 0; i < g->num_commits; i++) { |
2e3c0737 | 1209 | struct commit *graph_commit, *odb_commit; |
53614b13 | 1210 | struct commit_list *graph_parents, *odb_parents; |
1373e547 | 1211 | uint32_t max_generation = 0; |
96af91d4 | 1212 | |
1f7f557f | 1213 | display_progress(progress, i + 1); |
96af91d4 DS |
1214 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
1215 | ||
82952964 | 1216 | graph_commit = lookup_commit(r, &cur_oid); |
96af91d4 DS |
1217 | odb_commit = (struct commit *)create_object(r, cur_oid.hash, alloc_commit_node(r)); |
1218 | if (parse_commit_internal(odb_commit, 0, 0)) { | |
93b4405f | 1219 | graph_report(_("failed to parse commit %s from object database for commit-graph"), |
96af91d4 DS |
1220 | oid_to_hex(&cur_oid)); |
1221 | continue; | |
1222 | } | |
2e3c0737 | 1223 | |
4f542b7a | 1224 | if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, |
2e3c0737 | 1225 | get_commit_tree_oid(odb_commit))) |
93b4405f | 1226 | graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"), |
2e3c0737 DS |
1227 | oid_to_hex(&cur_oid), |
1228 | oid_to_hex(get_commit_tree_oid(graph_commit)), | |
1229 | oid_to_hex(get_commit_tree_oid(odb_commit))); | |
53614b13 DS |
1230 | |
1231 | graph_parents = graph_commit->parents; | |
1232 | odb_parents = odb_commit->parents; | |
1233 | ||
1234 | while (graph_parents) { | |
1235 | if (odb_parents == NULL) { | |
93b4405f | 1236 | graph_report(_("commit-graph parent list for commit %s is too long"), |
53614b13 DS |
1237 | oid_to_hex(&cur_oid)); |
1238 | break; | |
1239 | } | |
1240 | ||
9001dc2a | 1241 | if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) |
93b4405f | 1242 | graph_report(_("commit-graph parent for %s is %s != %s"), |
53614b13 DS |
1243 | oid_to_hex(&cur_oid), |
1244 | oid_to_hex(&graph_parents->item->object.oid), | |
1245 | oid_to_hex(&odb_parents->item->object.oid)); | |
1246 | ||
1373e547 DS |
1247 | if (graph_parents->item->generation > max_generation) |
1248 | max_generation = graph_parents->item->generation; | |
1249 | ||
53614b13 DS |
1250 | graph_parents = graph_parents->next; |
1251 | odb_parents = odb_parents->next; | |
1252 | } | |
1253 | ||
1254 | if (odb_parents != NULL) | |
93b4405f | 1255 | graph_report(_("commit-graph parent list for commit %s terminates early"), |
53614b13 | 1256 | oid_to_hex(&cur_oid)); |
1373e547 DS |
1257 | |
1258 | if (!graph_commit->generation) { | |
1259 | if (generation_zero == GENERATION_NUMBER_EXISTS) | |
93b4405f | 1260 | graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"), |
1373e547 DS |
1261 | oid_to_hex(&cur_oid)); |
1262 | generation_zero = GENERATION_ZERO_EXISTS; | |
1263 | } else if (generation_zero == GENERATION_ZERO_EXISTS) | |
93b4405f | 1264 | graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"), |
1373e547 DS |
1265 | oid_to_hex(&cur_oid)); |
1266 | ||
1267 | if (generation_zero == GENERATION_ZERO_EXISTS) | |
1268 | continue; | |
1269 | ||
1270 | /* | |
1271 | * If one of our parents has generation GENERATION_NUMBER_MAX, then | |
1272 | * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid | |
1273 | * extra logic in the following condition. | |
1274 | */ | |
1275 | if (max_generation == GENERATION_NUMBER_MAX) | |
1276 | max_generation--; | |
1277 | ||
1278 | if (graph_commit->generation != max_generation + 1) | |
93b4405f | 1279 | graph_report(_("commit-graph generation for commit %s is %u != %u"), |
1373e547 DS |
1280 | oid_to_hex(&cur_oid), |
1281 | graph_commit->generation, | |
1282 | max_generation + 1); | |
88968ebf DS |
1283 | |
1284 | if (graph_commit->date != odb_commit->date) | |
93b4405f | 1285 | graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime), |
88968ebf DS |
1286 | oid_to_hex(&cur_oid), |
1287 | graph_commit->date, | |
1288 | odb_commit->date); | |
96af91d4 | 1289 | } |
1f7f557f | 1290 | stop_progress(&progress); |
96af91d4 | 1291 | |
283e68c7 DS |
1292 | return verify_commit_graph_error; |
1293 | } | |
c3756d5b JT |
1294 | |
1295 | void free_commit_graph(struct commit_graph *g) | |
1296 | { | |
1297 | if (!g) | |
1298 | return; | |
1299 | if (g->graph_fd >= 0) { | |
1300 | munmap((void *)g->data, g->data_len); | |
1301 | g->data = NULL; | |
1302 | close(g->graph_fd); | |
1303 | } | |
1304 | free(g); | |
1305 | } |