blob: 6f62a0731383293eb7e617f0ecda932d421757fd [file] [log] [blame]
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001#include "git-compat-util.h"
SZEDER Gáborfa796532020-06-05 13:00:28 +00002#include "config.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04003#include "lockfile.h"
4#include "pack.h"
5#include "packfile.h"
6#include "commit.h"
7#include "object.h"
Derrick Stolee59fb8772018-06-27 09:24:45 -04008#include "refs.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04009#include "revision.h"
10#include "sha1-lookup.h"
11#include "commit-graph.h"
Junio C Hamanob10edb22018-05-08 15:59:20 +090012#include "object-store.h"
Derrick Stolee96af91d2018-06-27 09:24:36 -040013#include "alloc.h"
Derrick Stoleed6538242018-08-20 18:24:27 +000014#include "hashmap.h"
15#include "replace-object.h"
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +000016#include "progress.h"
Garima Singhf97b9322020-03-30 00:31:28 +000017#include "bloom.h"
Jeff Kingd21ee7d2020-03-30 00:31:29 +000018#include "commit-slab.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060019#include "shallow.h"
Derrick Stolee0087a872020-07-01 13:27:24 +000020#include "json-writer.h"
21#include "trace2.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040022
Derrick Stoleeb23ea972020-04-16 20:14:03 +000023void git_test_write_commit_graph_or_die(void)
24{
25 int flags = 0;
26 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0))
27 return;
28
29 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0))
30 flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS;
31
32 if (write_commit_graph_reachable(the_repository->objects->odb,
33 flags, NULL))
34 die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH");
35}
36
Derrick Stolee08fd81c2018-04-02 16:34:19 -040037#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
38#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
39#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
40#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
SZEDER Gábor5af74172019-01-19 21:21:13 +010041#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
Garima Singh76ffbca2020-04-06 16:59:49 +000042#define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */
43#define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */
Derrick Stolee118bd572019-06-18 11:14:26 -070044#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
Garima Singh76ffbca2020-04-06 16:59:49 +000045#define MAX_NUM_CHUNKS 7
Derrick Stolee08fd81c2018-04-02 16:34:19 -040046
brian m. carlsonc1665992018-11-14 04:09:35 +000047#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040048
49#define GRAPH_VERSION_1 0x1
50#define GRAPH_VERSION GRAPH_VERSION_1
51
SZEDER Gábor5af74172019-01-19 21:21:13 +010052#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
Derrick Stolee08fd81c2018-04-02 16:34:19 -040053#define GRAPH_EDGE_LAST_MASK 0x7fffffff
54#define GRAPH_PARENT_NONE 0x70000000
55
56#define GRAPH_LAST_EDGE 0x80000000
57
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040058#define GRAPH_HEADER_SIZE 8
Derrick Stolee08fd81c2018-04-02 16:34:19 -040059#define GRAPH_FANOUT_SIZE (4 * 256)
60#define GRAPH_CHUNKLOOKUP_WIDTH 12
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040061#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
brian m. carlsonc1665992018-11-14 04:09:35 +000062 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040063
Derrick Stoleecb99a342019-10-24 13:40:42 +000064/* Remember to update object flag allocation in object.h */
65#define REACHABLE (1u<<15)
66
Jeff Kingd21ee7d2020-03-30 00:31:29 +000067/* Keep track of the order in which commits are added to our list. */
68define_commit_slab(commit_pos, int);
69static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos);
70
71static void set_commit_pos(struct repository *r, const struct object_id *oid)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040072{
Jeff Kingd21ee7d2020-03-30 00:31:29 +000073 static int32_t max_pos;
74 struct commit *commit = lookup_commit(r, oid);
75
76 if (!commit)
77 return; /* should never happen, but be lenient */
78
79 *commit_pos_at(&commit_pos, commit) = max_pos++;
80}
81
82static int commit_pos_cmp(const void *va, const void *vb)
83{
84 const struct commit *a = *(const struct commit **)va;
85 const struct commit *b = *(const struct commit **)vb;
86 return commit_pos_at(&commit_pos, a) -
87 commit_pos_at(&commit_pos, b);
88}
89
Abhishek Kumar48448122020-06-17 14:44:09 +053090define_commit_slab(commit_graph_data_slab, struct commit_graph_data);
91static struct commit_graph_data_slab commit_graph_data_slab =
92 COMMIT_SLAB_INIT(1, commit_graph_data_slab);
93
94uint32_t commit_graph_position(const struct commit *c)
95{
96 struct commit_graph_data *data =
97 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
98
99 return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH;
100}
101
102uint32_t commit_graph_generation(const struct commit *c)
103{
104 struct commit_graph_data *data =
105 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
106
107 if (!data)
108 return GENERATION_NUMBER_INFINITY;
109 else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH)
110 return GENERATION_NUMBER_INFINITY;
111
112 return data->generation;
113}
114
115static struct commit_graph_data *commit_graph_data_at(const struct commit *c)
116{
117 unsigned int i, nth_slab;
118 struct commit_graph_data *data =
119 commit_graph_data_slab_peek(&commit_graph_data_slab, c);
120
121 if (data)
122 return data;
123
124 nth_slab = c->index / commit_graph_data_slab.slab_size;
125 data = commit_graph_data_slab_at(&commit_graph_data_slab, c);
126
127 /*
128 * commit-slab initializes elements with zero, overwrite this with
129 * COMMIT_NOT_FROM_GRAPH for graph_pos.
130 *
131 * We avoid initializing generation with checking if graph position
132 * is not COMMIT_NOT_FROM_GRAPH.
133 */
134 for (i = 0; i < commit_graph_data_slab.slab_size; i++) {
135 commit_graph_data_slab.slab[nth_slab][i].graph_pos =
136 COMMIT_NOT_FROM_GRAPH;
137 }
138
139 return data;
140}
141
Garima Singh3d112752020-03-30 00:31:30 +0000142static int commit_gen_cmp(const void *va, const void *vb)
143{
144 const struct commit *a = *(const struct commit **)va;
145 const struct commit *b = *(const struct commit **)vb;
146
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530147 uint32_t generation_a = commit_graph_generation(a);
148 uint32_t generation_b = commit_graph_generation(b);
Garima Singh3d112752020-03-30 00:31:30 +0000149 /* lower generation commits first */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530150 if (generation_a < generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000151 return -1;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530152 else if (generation_a > generation_b)
Garima Singh3d112752020-03-30 00:31:30 +0000153 return 1;
154
155 /* use date as a heuristic when generations are equal */
156 if (a->date < b->date)
157 return -1;
158 else if (a->date > b->date)
159 return 1;
160 return 0;
161}
162
Jeff Kingd21ee7d2020-03-30 00:31:29 +0000163char *get_commit_graph_filename(struct object_directory *obj_dir)
164{
165 return xstrfmt("%s/info/commit-graph", obj_dir->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400166}
167
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800168static char *get_split_graph_filename(struct object_directory *odb,
Derrick Stolee5c84b332019-06-18 11:14:25 -0700169 const char *oid_hex)
170{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800171 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
172 oid_hex);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700173}
174
Derrick Stolee663b2b12020-09-17 18:11:46 +0000175char *get_commit_graph_chain_filename(struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700176{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800177 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400178}
179
brian m. carlsonc1665992018-11-14 04:09:35 +0000180static uint8_t oid_version(void)
181{
Derrick Stolee665d70a2020-08-17 14:04:47 +0000182 switch (hash_algo_by_ptr(the_hash_algo)) {
183 case GIT_HASH_SHA1:
184 return 1;
185 case GIT_HASH_SHA256:
186 return 2;
187 default:
188 die(_("invalid hash version"));
189 }
brian m. carlsonc1665992018-11-14 04:09:35 +0000190}
191
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400192static struct commit_graph *alloc_commit_graph(void)
193{
194 struct commit_graph *g = xcalloc(1, sizeof(*g));
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400195
196 return g;
197}
198
Derrick Stoleed6538242018-08-20 18:24:27 +0000199extern int read_replace_refs;
200
201static int commit_graph_compatible(struct repository *r)
202{
Derrick Stolee5cef2952018-08-20 18:24:32 +0000203 if (!r->gitdir)
204 return 0;
205
Derrick Stoleed6538242018-08-20 18:24:27 +0000206 if (read_replace_refs) {
207 prepare_replace_object(r);
208 if (hashmap_get_size(&r->objects->replace_map->map))
209 return 0;
210 }
211
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000212 prepare_commit_graft(r);
Taylor Blauce163642020-07-08 17:10:53 -0400213 if (r->parsed_objects &&
214 (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent))
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000215 return 0;
216 if (is_repository_shallow(r))
217 return 0;
218
Derrick Stoleed6538242018-08-20 18:24:27 +0000219 return 1;
220}
221
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100222int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
223{
224 *fd = git_open(graph_file);
225 if (*fd < 0)
226 return 0;
227 if (fstat(*fd, st)) {
228 close(*fd);
229 return 0;
230 }
231 return 1;
232}
233
Taylor Blauab14d062020-09-09 11:22:56 -0400234struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
235 int fd, struct stat *st,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800236 struct object_directory *odb)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400237{
238 void *graph_map;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400239 size_t graph_size;
Josh Steadmonaa658572019-01-15 14:25:50 -0800240 struct commit_graph *ret;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400241
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100242 graph_size = xsize_t(st->st_size);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400243
244 if (graph_size < GRAPH_MIN_SIZE) {
245 close(fd);
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100246 error(_("commit-graph file is too small"));
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100247 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400248 }
249 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
Jeff Kingc8828532020-04-23 15:41:13 -0600250 close(fd);
Taylor Blauab14d062020-09-09 11:22:56 -0400251 ret = parse_commit_graph(r, graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800252
Taylor Blaua7df60c2020-02-03 13:18:04 -0800253 if (ret)
254 ret->odb = odb;
Jeff Kingc8828532020-04-23 15:41:13 -0600255 else
Josh Steadmonaa658572019-01-15 14:25:50 -0800256 munmap(graph_map, graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800257
258 return ret;
259}
260
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100261static int verify_commit_graph_lite(struct commit_graph *g)
262{
263 /*
264 * Basic validation shared between parse_commit_graph()
265 * which'll be called every time the graph is used, and the
266 * much more expensive verify_commit_graph() used by
267 * "commit-graph verify".
268 *
269 * There should only be very basic checks here to ensure that
270 * we don't e.g. segfault in fill_commit_in_graph(), but
271 * because this is a very hot codepath nothing that e.g. loops
272 * over g->num_commits, or runs a checksum on the commit-graph
273 * itself.
274 */
275 if (!g->chunk_oid_fanout) {
276 error("commit-graph is missing the OID Fanout chunk");
277 return 1;
278 }
279 if (!g->chunk_oid_lookup) {
280 error("commit-graph is missing the OID Lookup chunk");
281 return 1;
282 }
283 if (!g->chunk_commit_data) {
284 error("commit-graph is missing the Commit Data chunk");
285 return 1;
286 }
287
288 return 0;
289}
290
Taylor Blauab14d062020-09-09 11:22:56 -0400291struct commit_graph *parse_commit_graph(struct repository *r,
292 void *graph_map, size_t graph_size)
Josh Steadmonaa658572019-01-15 14:25:50 -0800293{
294 const unsigned char *data, *chunk_lookup;
295 uint32_t i;
296 struct commit_graph *graph;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000297 uint64_t next_chunk_offset;
Josh Steadmonaa658572019-01-15 14:25:50 -0800298 uint32_t graph_signature;
299 unsigned char graph_version, hash_version;
300
301 if (!graph_map)
302 return NULL;
303
304 if (graph_size < GRAPH_MIN_SIZE)
305 return NULL;
306
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400307 data = (const unsigned char *)graph_map;
308
309 graph_signature = get_be32(data);
310 if (graph_signature != GRAPH_SIGNATURE) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100311 error(_("commit-graph signature %X does not match signature %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400312 graph_signature, GRAPH_SIGNATURE);
Josh Steadmonaa658572019-01-15 14:25:50 -0800313 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400314 }
315
316 graph_version = *(unsigned char*)(data + 4);
317 if (graph_version != GRAPH_VERSION) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100318 error(_("commit-graph version %X does not match version %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400319 graph_version, GRAPH_VERSION);
Josh Steadmonaa658572019-01-15 14:25:50 -0800320 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400321 }
322
323 hash_version = *(unsigned char*)(data + 5);
brian m. carlsonc1665992018-11-14 04:09:35 +0000324 if (hash_version != oid_version()) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100325 error(_("commit-graph hash version %X does not match version %X"),
brian m. carlsonc1665992018-11-14 04:09:35 +0000326 hash_version, oid_version());
Josh Steadmonaa658572019-01-15 14:25:50 -0800327 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400328 }
329
Taylor Blaub66d8472020-09-09 11:23:10 -0400330 prepare_repo_settings(r);
331
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400332 graph = alloc_commit_graph();
333
brian m. carlsonc1665992018-11-14 04:09:35 +0000334 graph->hash_len = the_hash_algo->rawsz;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400335 graph->num_chunks = *(unsigned char*)(data + 6);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400336 graph->data = graph_map;
337 graph->data_len = graph_size;
338
SZEDER Gábor2ad4f1a2020-06-05 13:00:29 +0000339 if (graph_size < GRAPH_HEADER_SIZE +
340 (graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH +
341 GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) {
342 error(_("commit-graph file is too small to hold %u chunks"),
343 graph->num_chunks);
344 free(graph);
345 return NULL;
346 }
347
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400348 chunk_lookup = data + 8;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000349 next_chunk_offset = get_be64(chunk_lookup + 4);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400350 for (i = 0; i < graph->num_chunks; i++) {
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800351 uint32_t chunk_id;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000352 uint64_t chunk_offset = next_chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400353 int chunk_repeated = 0;
354
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800355 chunk_id = get_be32(chunk_lookup + 0);
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800356
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400357 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000358 next_chunk_offset = get_be64(chunk_lookup + 4);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400359
brian m. carlsonc1665992018-11-14 04:09:35 +0000360 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100361 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400362 (uint32_t)chunk_offset);
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700363 goto free_and_return;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400364 }
365
366 switch (chunk_id) {
367 case GRAPH_CHUNKID_OIDFANOUT:
368 if (graph->chunk_oid_fanout)
369 chunk_repeated = 1;
370 else
371 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
372 break;
373
374 case GRAPH_CHUNKID_OIDLOOKUP:
375 if (graph->chunk_oid_lookup)
376 chunk_repeated = 1;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000377 else {
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400378 graph->chunk_oid_lookup = data + chunk_offset;
SZEDER Gábor5cfa4382020-06-05 13:00:30 +0000379 graph->num_commits = (next_chunk_offset - chunk_offset)
380 / graph->hash_len;
381 }
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400382 break;
383
384 case GRAPH_CHUNKID_DATA:
385 if (graph->chunk_commit_data)
386 chunk_repeated = 1;
387 else
388 graph->chunk_commit_data = data + chunk_offset;
389 break;
390
SZEDER Gábor5af74172019-01-19 21:21:13 +0100391 case GRAPH_CHUNKID_EXTRAEDGES:
392 if (graph->chunk_extra_edges)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400393 chunk_repeated = 1;
394 else
SZEDER Gábor5af74172019-01-19 21:21:13 +0100395 graph->chunk_extra_edges = data + chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400396 break;
Derrick Stolee118bd572019-06-18 11:14:26 -0700397
398 case GRAPH_CHUNKID_BASE:
399 if (graph->chunk_base_graphs)
400 chunk_repeated = 1;
401 else
402 graph->chunk_base_graphs = data + chunk_offset;
Garima Singh76ffbca2020-04-06 16:59:49 +0000403 break;
404
405 case GRAPH_CHUNKID_BLOOMINDEXES:
406 if (graph->chunk_bloom_indexes)
407 chunk_repeated = 1;
Taylor Blaub66d8472020-09-09 11:23:10 -0400408 else if (r->settings.commit_graph_read_changed_paths)
Garima Singh76ffbca2020-04-06 16:59:49 +0000409 graph->chunk_bloom_indexes = data + chunk_offset;
410 break;
411
412 case GRAPH_CHUNKID_BLOOMDATA:
413 if (graph->chunk_bloom_data)
414 chunk_repeated = 1;
Taylor Blaub66d8472020-09-09 11:23:10 -0400415 else if (r->settings.commit_graph_read_changed_paths) {
Garima Singh76ffbca2020-04-06 16:59:49 +0000416 uint32_t hash_version;
417 graph->chunk_bloom_data = data + chunk_offset;
418 hash_version = get_be32(data + chunk_offset);
419
420 if (hash_version != 1)
421 break;
422
423 graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings));
424 graph->bloom_filter_settings->hash_version = hash_version;
425 graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4);
426 graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8);
Taylor Blau97ffa4f2020-09-17 09:34:42 -0400427 graph->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES;
Garima Singh76ffbca2020-04-06 16:59:49 +0000428 }
429 break;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400430 }
431
432 if (chunk_repeated) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100433 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700434 goto free_and_return;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400435 }
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400436 }
437
Garima Singh76ffbca2020-04-06 16:59:49 +0000438 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {
439 init_bloom_filters();
440 } else {
441 /* We need both the bloom chunks to exist together. Else ignore the data */
442 graph->chunk_bloom_indexes = NULL;
443 graph->chunk_bloom_data = NULL;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700444 FREE_AND_NULL(graph->bloom_filter_settings);
Garima Singh76ffbca2020-04-06 16:59:49 +0000445 }
446
Derrick Stolee118bd572019-06-18 11:14:26 -0700447 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
448
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700449 if (verify_commit_graph_lite(graph))
450 goto free_and_return;
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100451
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400452 return graph;
Jonathan Tanfbda77c2020-05-04 12:13:24 -0700453
454free_and_return:
455 free(graph->bloom_filter_settings);
456 free(graph);
457 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400458}
459
Taylor Blauab14d062020-09-09 11:22:56 -0400460static struct commit_graph *load_commit_graph_one(struct repository *r,
461 const char *graph_file,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800462 struct object_directory *odb)
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100463{
464
465 struct stat st;
466 int fd;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700467 struct commit_graph *g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100468 int open_ok = open_commit_graph(graph_file, &fd, &st);
469
470 if (!open_ok)
471 return NULL;
472
Taylor Blauab14d062020-09-09 11:22:56 -0400473 g = load_commit_graph_one_fd_st(r, fd, &st, odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700474
475 if (g)
476 g->filename = xstrdup(graph_file);
477
478 return g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100479}
480
Taylor Blau13c24992020-02-03 13:18:00 -0800481static struct commit_graph *load_commit_graph_v1(struct repository *r,
482 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700483{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800484 char *graph_name = get_commit_graph_filename(odb);
Taylor Blauab14d062020-09-09 11:22:56 -0400485 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700486 free(graph_name);
487
488 return g;
489}
490
491static int add_graph_to_chain(struct commit_graph *g,
492 struct commit_graph *chain,
493 struct object_id *oids,
494 int n)
495{
496 struct commit_graph *cur_g = chain;
497
Derrick Stolee118bd572019-06-18 11:14:26 -0700498 if (n && !g->chunk_base_graphs) {
499 warning(_("commit-graph has no base graphs chunk"));
500 return 0;
501 }
502
Derrick Stolee5c84b332019-06-18 11:14:25 -0700503 while (n) {
504 n--;
Derrick Stolee118bd572019-06-18 11:14:26 -0700505
506 if (!cur_g ||
507 !oideq(&oids[n], &cur_g->oid) ||
508 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
509 warning(_("commit-graph chain does not match"));
510 return 0;
511 }
512
Derrick Stolee5c84b332019-06-18 11:14:25 -0700513 cur_g = cur_g->base_graph;
514 }
515
516 g->base_graph = chain;
517
518 if (chain)
519 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
520
521 return 1;
522}
523
Taylor Blau13c24992020-02-03 13:18:00 -0800524static struct commit_graph *load_commit_graph_chain(struct repository *r,
525 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700526{
527 struct commit_graph *graph_chain = NULL;
528 struct strbuf line = STRBUF_INIT;
529 struct stat st;
530 struct object_id *oids;
531 int i = 0, valid = 1, count;
Derrick Stolee663b2b12020-09-17 18:11:46 +0000532 char *chain_name = get_commit_graph_chain_filename(odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700533 FILE *fp;
534 int stat_res;
535
536 fp = fopen(chain_name, "r");
537 stat_res = stat(chain_name, &st);
538 free(chain_name);
539
540 if (!fp ||
541 stat_res ||
542 st.st_size <= the_hash_algo->hexsz)
543 return NULL;
544
545 count = st.st_size / (the_hash_algo->hexsz + 1);
546 oids = xcalloc(count, sizeof(struct object_id));
547
Derrick Stoleec5230352019-06-18 11:14:30 -0700548 prepare_alt_odb(r);
549
550 for (i = 0; i < count; i++) {
551 struct object_directory *odb;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700552
553 if (strbuf_getline_lf(&line, fp) == EOF)
554 break;
555
556 if (get_oid_hex(line.buf, &oids[i])) {
557 warning(_("invalid commit-graph chain: line '%s' not a hash"),
558 line.buf);
559 valid = 0;
560 break;
561 }
562
Derrick Stoleec5230352019-06-18 11:14:30 -0700563 valid = 0;
564 for (odb = r->objects->odb; odb; odb = odb->next) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800565 char *graph_name = get_split_graph_filename(odb, line.buf);
Taylor Blauab14d062020-09-09 11:22:56 -0400566 struct commit_graph *g = load_commit_graph_one(r, graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700567
Derrick Stoleec5230352019-06-18 11:14:30 -0700568 free(graph_name);
569
570 if (g) {
Derrick Stoleec5230352019-06-18 11:14:30 -0700571 if (add_graph_to_chain(g, graph_chain, oids, i)) {
572 graph_chain = g;
573 valid = 1;
574 }
575
576 break;
577 }
578 }
579
580 if (!valid) {
581 warning(_("unable to find all commit-graph files"));
582 break;
583 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700584 }
585
586 free(oids);
587 fclose(fp);
René Scharfe0aa6bce2019-08-07 13:15:02 +0200588 strbuf_release(&line);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700589
590 return graph_chain;
591}
592
Taylor Blau13c24992020-02-03 13:18:00 -0800593struct commit_graph *read_commit_graph_one(struct repository *r,
594 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700595{
Taylor Blau13c24992020-02-03 13:18:00 -0800596 struct commit_graph *g = load_commit_graph_v1(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700597
598 if (!g)
Taylor Blau13c24992020-02-03 13:18:00 -0800599 g = load_commit_graph_chain(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700600
601 return g;
Jonathan Tan5faf3572018-07-11 15:42:37 -0700602}
603
Taylor Blau13c24992020-02-03 13:18:00 -0800604static void prepare_commit_graph_one(struct repository *r,
605 struct object_directory *odb)
Derrick Stolee177722b2018-04-10 08:56:05 -0400606{
Derrick Stolee177722b2018-04-10 08:56:05 -0400607
608 if (r->objects->commit_graph)
609 return;
610
Taylor Blau13c24992020-02-03 13:18:00 -0800611 r->objects->commit_graph = read_commit_graph_one(r, odb);
Derrick Stolee177722b2018-04-10 08:56:05 -0400612}
Jonathan Tan5faf3572018-07-11 15:42:37 -0700613
614/*
615 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
616 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000617 * On the first invocation, this function attempts to load the commit
Jonathan Tan5faf3572018-07-11 15:42:37 -0700618 * graph if the_repository is configured to have one.
619 */
Jonathan Tandade47c2018-07-11 15:42:42 -0700620static int prepare_commit_graph(struct repository *r)
Derrick Stolee177722b2018-04-10 08:56:05 -0400621{
Jeff King263db402018-11-12 09:48:47 -0500622 struct object_directory *odb;
Derrick Stolee177722b2018-04-10 08:56:05 -0400623
Jeff King6abada12019-09-12 10:44:45 -0400624 /*
625 * This must come before the "already attempted?" check below, because
626 * we want to disable even an already-loaded graph file.
627 */
628 if (r->commit_graph_disabled)
629 return 0;
Ævar Arnfjörð Bjarmason43d35612019-03-25 13:08:33 +0100630
Jonathan Tandade47c2018-07-11 15:42:42 -0700631 if (r->objects->commit_graph_attempted)
632 return !!r->objects->commit_graph;
633 r->objects->commit_graph_attempted = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400634
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700635 prepare_repo_settings(r);
636
Derrick Stolee859fdc02018-08-29 05:49:04 -0700637 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700638 r->settings.core_commit_graph != 1)
Jonathan Tandade47c2018-07-11 15:42:42 -0700639 /*
640 * This repository is not configured to use commit graphs, so
641 * do not load one. (But report commit_graph_attempted anyway
642 * so that commit graph loading is not attempted again for this
643 * repository.)
644 */
Jonathan Tan5faf3572018-07-11 15:42:37 -0700645 return 0;
646
Derrick Stoleed6538242018-08-20 18:24:27 +0000647 if (!commit_graph_compatible(r))
648 return 0;
649
Jonathan Tandade47c2018-07-11 15:42:42 -0700650 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500651 for (odb = r->objects->odb;
Jeff King263db402018-11-12 09:48:47 -0500652 !r->objects->commit_graph && odb;
653 odb = odb->next)
Taylor Blau13c24992020-02-03 13:18:00 -0800654 prepare_commit_graph_one(r, odb);
Jonathan Tandade47c2018-07-11 15:42:42 -0700655 return !!r->objects->commit_graph;
Derrick Stolee177722b2018-04-10 08:56:05 -0400656}
657
Derrick Stolee6cc01742018-07-20 16:33:30 +0000658int generation_numbers_enabled(struct repository *r)
659{
660 uint32_t first_generation;
661 struct commit_graph *g;
662 if (!prepare_commit_graph(r))
663 return 0;
664
665 g = r->objects->commit_graph;
666
667 if (!g->num_commits)
668 return 0;
669
670 first_generation = get_be32(g->chunk_commit_data +
671 g->hash_len + 8) >> 2;
672
673 return !!first_generation;
674}
675
Taylor Blau4f364402020-09-09 11:22:44 -0400676struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r)
677{
678 struct commit_graph *g = r->objects->commit_graph;
679 while (g) {
680 if (g->bloom_filter_settings)
681 return g->bloom_filter_settings;
682 g = g->base_graph;
683 }
684 return NULL;
685}
686
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700687static void close_commit_graph_one(struct commit_graph *g)
688{
689 if (!g)
690 return;
691
692 close_commit_graph_one(g->base_graph);
693 free_commit_graph(g);
694}
695
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700696void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400697{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700698 close_commit_graph_one(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700699 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400700}
701
702static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
703{
704 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
705 g->chunk_oid_lookup, g->hash_len, pos);
706}
707
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700708static void load_oid_from_graph(struct commit_graph *g,
709 uint32_t pos,
710 struct object_id *oid)
711{
712 uint32_t lex_index;
713
714 while (g && pos < g->num_commits_in_base)
715 g = g->base_graph;
716
717 if (!g)
718 BUG("NULL commit-graph");
719
720 if (pos >= g->num_commits + g->num_commits_in_base)
721 die(_("invalid commit position. commit-graph is likely corrupt"));
722
723 lex_index = pos - g->num_commits_in_base;
724
725 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
726}
727
Stefan Beller4f542b72018-12-14 16:09:39 -0800728static struct commit_list **insert_parent_or_die(struct repository *r,
729 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700730 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400731 struct commit_list **pptr)
732{
733 struct commit *c;
734 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400735
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700736 if (pos >= g->num_commits + g->num_commits_in_base)
737 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400738
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700739 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800740 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400741 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200742 die(_("could not find commit %s"), oid_to_hex(&oid));
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530743 commit_graph_data_at(c)->graph_pos = pos;
Derrick Stolee177722b2018-04-10 08:56:05 -0400744 return &commit_list_insert(c, pptr)->next;
745}
746
Derrick Stoleee2838d82018-05-01 12:47:13 +0000747static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
748{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700749 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530750 struct commit_graph_data *graph_data;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700751 uint32_t lex_index;
752
753 while (pos < g->num_commits_in_base)
754 g = g->base_graph;
755
756 lex_index = pos - g->num_commits_in_base;
757 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530758
759 graph_data = commit_graph_data_at(item);
760 graph_data->graph_pos = pos;
761 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000762}
763
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700764static inline void set_commit_tree(struct commit *c, struct tree *t)
765{
766 c->maybe_tree = t;
767}
768
Stefan Beller4f542b72018-12-14 16:09:39 -0800769static int fill_commit_in_graph(struct repository *r,
770 struct commit *item,
771 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400772{
Derrick Stolee177722b2018-04-10 08:56:05 -0400773 uint32_t edge_value;
774 uint32_t *parent_data_ptr;
775 uint64_t date_low, date_high;
776 struct commit_list **pptr;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530777 struct commit_graph_data *graph_data;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700778 const unsigned char *commit_data;
779 uint32_t lex_index;
780
781 while (pos < g->num_commits_in_base)
782 g = g->base_graph;
783
784 if (pos >= g->num_commits + g->num_commits_in_base)
785 die(_("invalid commit position. commit-graph is likely corrupt"));
786
787 /*
788 * Store the "full" position, but then use the
789 * "local" position for the rest of the calculation.
790 */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530791 graph_data = commit_graph_data_at(item);
792 graph_data->graph_pos = pos;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700793 lex_index = pos - g->num_commits_in_base;
794
795 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
Derrick Stolee177722b2018-04-10 08:56:05 -0400796
797 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400798
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700799 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400800
801 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
802 date_low = get_be32(commit_data + g->hash_len + 12);
803 item->date = (timestamp_t)((date_high << 32) | date_low);
804
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530805 graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
Derrick Stolee83073cc2018-04-25 14:37:55 +0000806
Derrick Stolee177722b2018-04-10 08:56:05 -0400807 pptr = &item->parents;
808
809 edge_value = get_be32(commit_data + g->hash_len);
810 if (edge_value == GRAPH_PARENT_NONE)
811 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800812 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400813
814 edge_value = get_be32(commit_data + g->hash_len + 4);
815 if (edge_value == GRAPH_PARENT_NONE)
816 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100817 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800818 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400819 return 1;
820 }
821
SZEDER Gábor5af74172019-01-19 21:21:13 +0100822 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
Derrick Stolee177722b2018-04-10 08:56:05 -0400823 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
824 do {
825 edge_value = get_be32(parent_data_ptr);
Stefan Beller4f542b72018-12-14 16:09:39 -0800826 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400827 edge_value & GRAPH_EDGE_LAST_MASK,
828 pptr);
829 parent_data_ptr++;
830 } while (!(edge_value & GRAPH_LAST_EDGE));
831
832 return 1;
833}
834
Derrick Stoleee2838d82018-05-01 12:47:13 +0000835static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
836{
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530837 uint32_t graph_pos = commit_graph_position(item);
838 if (graph_pos != COMMIT_NOT_FROM_GRAPH) {
839 *pos = graph_pos;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000840 return 1;
841 } else {
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700842 struct commit_graph *cur_g = g;
843 uint32_t lex_index;
844
845 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
846 cur_g = cur_g->base_graph;
847
848 if (cur_g) {
849 *pos = lex_index + cur_g->num_commits_in_base;
850 return 1;
851 }
852
853 return 0;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000854 }
855}
856
Stefan Beller4f542b72018-12-14 16:09:39 -0800857static int parse_commit_in_graph_one(struct repository *r,
858 struct commit_graph *g,
859 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -0400860{
Derrick Stoleee2838d82018-05-01 12:47:13 +0000861 uint32_t pos;
862
Derrick Stolee177722b2018-04-10 08:56:05 -0400863 if (item->object.parsed)
864 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -0400865
866 if (find_commit_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -0800867 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -0400868
869 return 0;
870}
871
Jonathan Tandade47c2018-07-11 15:42:42 -0700872int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -0400873{
Derrick Stolee7b671f82020-06-23 17:47:01 +0000874 static int checked_env = 0;
875
876 if (!checked_env &&
877 git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0))
878 die("dying as requested by the '%s' variable on commit-graph parse!",
879 GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE);
880 checked_env = 1;
881
Jonathan Tandade47c2018-07-11 15:42:42 -0700882 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -0400883 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -0800884 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -0400885}
886
Jonathan Tandade47c2018-07-11 15:42:42 -0700887void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000888{
889 uint32_t pos;
Jonathan Tandade47c2018-07-11 15:42:42 -0700890 if (!prepare_commit_graph(r))
Derrick Stoleee2838d82018-05-01 12:47:13 +0000891 return;
Jonathan Tandade47c2018-07-11 15:42:42 -0700892 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
893 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000894}
895
Stefan Beller4f542b72018-12-14 16:09:39 -0800896static struct tree *load_tree_for_commit(struct repository *r,
897 struct commit_graph *g,
898 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000899{
900 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700901 const unsigned char *commit_data;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530902 uint32_t graph_pos = commit_graph_position(c);
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700903
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530904 while (graph_pos < g->num_commits_in_base)
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700905 g = g->base_graph;
906
907 commit_data = g->chunk_commit_data +
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530908 GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000909
910 hashcpy(oid.hash, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700911 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000912
913 return c->maybe_tree;
914}
915
Stefan Beller4f542b72018-12-14 16:09:39 -0800916static struct tree *get_commit_tree_in_graph_one(struct repository *r,
917 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400918 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000919{
920 if (c->maybe_tree)
921 return c->maybe_tree;
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530922 if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400923 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000924
Stefan Beller4f542b72018-12-14 16:09:39 -0800925 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400926}
927
Jonathan Tandade47c2018-07-11 15:42:42 -0700928struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400929{
Stefan Beller4f542b72018-12-14 16:09:39 -0800930 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000931}
932
Derrick Stoleec9905be2019-06-12 06:29:40 -0700933struct packed_commit_list {
934 struct commit **list;
935 int nr;
936 int alloc;
937};
938
939struct packed_oid_list {
940 struct object_id *list;
941 int nr;
942 int alloc;
943};
944
945struct write_commit_graph_context {
946 struct repository *r;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800947 struct object_directory *odb;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700948 char *graph_name;
949 struct packed_oid_list oids;
950 struct packed_commit_list commits;
951 int num_extra_edges;
952 unsigned long approx_nr_objects;
953 struct progress *progress;
954 int progress_done;
955 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700956
957 char *base_graph_name;
958 int num_commit_graphs_before;
959 int num_commit_graphs_after;
960 char **commit_graph_filenames_before;
961 char **commit_graph_filenames_after;
962 char **commit_graph_hash_after;
963 uint32_t new_num_commits_in_base;
964 struct commit_graph *new_base_graph;
965
Derrick Stoleec9905be2019-06-12 06:29:40 -0700966 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -0700967 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200968 split:1,
Garima Singh3d112752020-03-30 00:31:30 +0000969 changed_paths:1,
970 order_by_pack:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700971
Taylor Blau98bb7962020-09-17 22:59:49 -0400972 const struct commit_graph_opts *opts;
Garima Singhf97b9322020-03-30 00:31:28 +0000973 size_t total_bloom_filter_data_size;
Derrick Stolee98037f22020-06-23 17:47:00 +0000974 const struct bloom_filter_settings *bloom_settings;
Taylor Blau312cff52020-09-16 14:07:32 -0400975
976 int count_bloom_filter_computed;
977 int count_bloom_filter_not_computed;
Taylor Blau59f0d502020-09-17 22:59:44 -0400978 int count_bloom_filter_trunc_empty;
Taylor Blau312cff52020-09-16 14:07:32 -0400979 int count_bloom_filter_trunc_large;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700980};
981
SZEDER Gábor9bab0812020-07-01 13:27:25 +0000982static int write_graph_chunk_fanout(struct hashfile *f,
983 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400984{
985 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700986 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400987
988 /*
989 * Write the first-level table (the list is sorted,
990 * but we use a 256-entry lookup to be able to avoid
991 * having to do eight extra binary search iterations).
992 */
993 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -0700994 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400995 if ((*list)->object.oid.hash[0] != i)
996 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700997 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400998 count++;
999 list++;
1000 }
1001
1002 hashwrite_be32(f, count);
1003 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001004
1005 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001006}
1007
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001008static int write_graph_chunk_oids(struct hashfile *f,
1009 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001010{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001011 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001012 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001013 for (count = 0; count < ctx->commits.nr; count++, list++) {
1014 display_progress(ctx->progress, ++ctx->progress_cnt);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001015 hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001016 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001017
1018 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001019}
1020
1021static const unsigned char *commit_to_sha1(size_t index, void *table)
1022{
1023 struct commit **commits = table;
1024 return commits[index]->object.oid.hash;
1025}
1026
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001027static int write_graph_chunk_data(struct hashfile *f,
1028 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001029{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001030 struct commit **list = ctx->commits.list;
1031 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001032 uint32_t num_extra_edges = 0;
1033
1034 while (list < last) {
1035 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -04001036 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001037 int edge_value;
1038 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -07001039 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001040
Taylor Blau16749b82019-09-05 18:04:55 -04001041 if (parse_commit_no_graph(*list))
1042 die(_("unable to parse commit %s"),
1043 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -04001044 tree = get_commit_tree_oid(*list);
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001045 hashwrite(f, tree->hash, the_hash_algo->rawsz);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001046
1047 parent = (*list)->parents;
1048
1049 if (!parent)
1050 edge_value = GRAPH_PARENT_NONE;
1051 else {
1052 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -07001053 ctx->commits.list,
1054 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001055 commit_to_sha1);
1056
Derrick Stolee6c622f92019-06-18 11:14:27 -07001057 if (edge_value >= 0)
1058 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001059 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001060 uint32_t pos;
1061 if (find_commit_in_graph(parent->item,
1062 ctx->new_base_graph,
1063 &pos))
1064 edge_value = pos;
1065 }
1066
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001067 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001068 BUG("missing parent %s for commit %s",
1069 oid_to_hex(&parent->item->object.oid),
1070 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001071 }
1072
1073 hashwrite_be32(f, edge_value);
1074
1075 if (parent)
1076 parent = parent->next;
1077
1078 if (!parent)
1079 edge_value = GRAPH_PARENT_NONE;
1080 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +01001081 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001082 else {
1083 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -07001084 ctx->commits.list,
1085 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001086 commit_to_sha1);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001087
1088 if (edge_value >= 0)
1089 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001090 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001091 uint32_t pos;
1092 if (find_commit_in_graph(parent->item,
1093 ctx->new_base_graph,
1094 &pos))
1095 edge_value = pos;
1096 }
1097
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001098 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001099 BUG("missing parent %s for commit %s",
1100 oid_to_hex(&parent->item->object.oid),
1101 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001102 }
1103
1104 hashwrite_be32(f, edge_value);
1105
SZEDER Gábor5af74172019-01-19 21:21:13 +01001106 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001107 do {
1108 num_extra_edges++;
1109 parent = parent->next;
1110 } while (parent);
1111 }
1112
1113 if (sizeof((*list)->date) > 4)
1114 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
1115 else
1116 packedDate[0] = 0;
1117
Abhishek Kumar48448122020-06-17 14:44:09 +05301118 packedDate[0] |= htonl(commit_graph_data_at(*list)->generation << 2);
Derrick Stolee3258c662018-05-01 12:47:09 +00001119
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001120 packedDate[1] = htonl((*list)->date);
1121 hashwrite(f, packedDate, 8);
1122
1123 list++;
1124 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001125
1126 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001127}
1128
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001129static int write_graph_chunk_extra_edges(struct hashfile *f,
1130 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001131{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001132 struct commit **list = ctx->commits.list;
1133 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001134 struct commit_list *parent;
1135
1136 while (list < last) {
1137 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001138
Derrick Stoleec9905be2019-06-12 06:29:40 -07001139 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +01001140
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001141 for (parent = (*list)->parents; num_parents < 3 && parent;
1142 parent = parent->next)
1143 num_parents++;
1144
1145 if (num_parents <= 2) {
1146 list++;
1147 continue;
1148 }
1149
1150 /* Since num_parents > 2, this initializer is safe. */
1151 for (parent = (*list)->parents->next; parent; parent = parent->next) {
1152 int edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -07001153 ctx->commits.list,
1154 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001155 commit_to_sha1);
1156
Derrick Stolee6c622f92019-06-18 11:14:27 -07001157 if (edge_value >= 0)
1158 edge_value += ctx->new_num_commits_in_base;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001159 else if (ctx->new_base_graph) {
Derrick Stolee6c622f92019-06-18 11:14:27 -07001160 uint32_t pos;
1161 if (find_commit_in_graph(parent->item,
1162 ctx->new_base_graph,
1163 &pos))
1164 edge_value = pos;
1165 }
1166
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001167 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -08001168 BUG("missing parent %s for commit %s",
1169 oid_to_hex(&parent->item->object.oid),
1170 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001171 else if (!parent->next)
1172 edge_value |= GRAPH_LAST_EDGE;
1173
1174 hashwrite_be32(f, edge_value);
1175 }
1176
1177 list++;
1178 }
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001179
1180 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001181}
1182
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001183static int write_graph_chunk_bloom_indexes(struct hashfile *f,
1184 struct write_commit_graph_context *ctx)
Garima Singh76ffbca2020-04-06 16:59:49 +00001185{
1186 struct commit **list = ctx->commits.list;
1187 struct commit **last = ctx->commits.list + ctx->commits.nr;
1188 uint32_t cur_pos = 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001189
1190 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001191 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001192 size_t len = filter ? filter->len : 0;
1193 cur_pos += len;
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001194 display_progress(ctx->progress, ++ctx->progress_cnt);
Garima Singh76ffbca2020-04-06 16:59:49 +00001195 hashwrite_be32(f, cur_pos);
1196 list++;
1197 }
1198
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001199 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001200}
1201
Derrick Stolee0087a872020-07-01 13:27:24 +00001202static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx)
1203{
1204 struct json_writer jw = JSON_WRITER_INIT;
1205
1206 jw_object_begin(&jw, 0);
1207 jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version);
1208 jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes);
1209 jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry);
Taylor Blau97ffa4f2020-09-17 09:34:42 -04001210 jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths);
Derrick Stolee0087a872020-07-01 13:27:24 +00001211 jw_end(&jw);
1212
1213 trace2_data_json("bloom", ctx->r, "settings", &jw);
1214
1215 jw_release(&jw);
1216}
1217
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001218static int write_graph_chunk_bloom_data(struct hashfile *f,
1219 struct write_commit_graph_context *ctx)
Garima Singh76ffbca2020-04-06 16:59:49 +00001220{
1221 struct commit **list = ctx->commits.list;
1222 struct commit **last = ctx->commits.list + ctx->commits.nr;
Garima Singh76ffbca2020-04-06 16:59:49 +00001223
Derrick Stolee0087a872020-07-01 13:27:24 +00001224 trace2_bloom_filter_settings(ctx);
1225
Derrick Stolee98037f22020-06-23 17:47:00 +00001226 hashwrite_be32(f, ctx->bloom_settings->hash_version);
1227 hashwrite_be32(f, ctx->bloom_settings->num_hashes);
1228 hashwrite_be32(f, ctx->bloom_settings->bits_per_entry);
Garima Singh76ffbca2020-04-06 16:59:49 +00001229
1230 while (list < last) {
Taylor Blau312cff52020-09-16 14:07:32 -04001231 struct bloom_filter *filter = get_bloom_filter(ctx->r, *list);
Derrick Stolee94919742020-07-01 13:27:23 +00001232 size_t len = filter ? filter->len : 0;
Derrick Stolee94919742020-07-01 13:27:23 +00001233
SZEDER Gábor150cd3b2020-07-09 19:00:03 +02001234 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee94919742020-07-01 13:27:23 +00001235 if (len)
1236 hashwrite(f, filter->data, len * sizeof(unsigned char));
Garima Singh76ffbca2020-04-06 16:59:49 +00001237 list++;
1238 }
1239
SZEDER Gábor9bab0812020-07-01 13:27:25 +00001240 return 0;
Garima Singh76ffbca2020-04-06 16:59:49 +00001241}
1242
Derrick Stolee3cbc6ed2019-06-18 11:14:24 -07001243static int oid_compare(const void *_a, const void *_b)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001244{
1245 const struct object_id *a = (const struct object_id *)_a;
1246 const struct object_id *b = (const struct object_id *)_b;
1247 return oidcmp(a, b);
1248}
1249
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001250static int add_packed_commits(const struct object_id *oid,
1251 struct packed_git *pack,
1252 uint32_t pos,
1253 void *data)
1254{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001255 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001256 enum object_type type;
1257 off_t offset = nth_packed_object_offset(pack, pos);
1258 struct object_info oi = OBJECT_INFO_INIT;
1259
Derrick Stoleec9905be2019-06-12 06:29:40 -07001260 if (ctx->progress)
1261 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001262
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001263 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001264 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001265 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001266
1267 if (type != OBJ_COMMIT)
1268 return 0;
1269
Derrick Stoleec9905be2019-06-12 06:29:40 -07001270 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1271 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1272 ctx->oids.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001273
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001274 set_commit_pos(ctx->r, oid);
1275
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001276 return 0;
1277}
1278
Derrick Stoleec9905be2019-06-12 06:29:40 -07001279static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001280{
1281 struct commit_list *parent;
1282 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001283 if (!(parent->item->object.flags & REACHABLE)) {
Derrick Stoleec9905be2019-06-12 06:29:40 -07001284 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1285 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1286 ctx->oids.nr++;
Derrick Stoleecb99a342019-10-24 13:40:42 +00001287 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001288 }
1289 }
1290}
1291
Derrick Stoleec9905be2019-06-12 06:29:40 -07001292static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001293{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001294 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001295 struct commit *commit;
Taylor Blau98bb7962020-09-17 22:59:49 -04001296 enum commit_graph_split_flags flags = ctx->opts ?
1297 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001298
Derrick Stoleec9905be2019-06-12 06:29:40 -07001299 if (ctx->report_progress)
1300 ctx->progress = start_delayed_progress(
1301 _("Loading known commits in commit graph"),
1302 ctx->oids.nr);
1303 for (i = 0; i < ctx->oids.nr; i++) {
1304 display_progress(ctx->progress, i + 1);
1305 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001306 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001307 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001308 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001309 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001310
1311 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001312 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001313 * than the number of missing commits in the reachable
1314 * closure.
1315 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001316 if (ctx->report_progress)
1317 ctx->progress = start_delayed_progress(
1318 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001319 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001320 for (i = 0; i < ctx->oids.nr; i++) {
1321 display_progress(ctx->progress, i + 1);
1322 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001323
Derrick Stolee6c622f92019-06-18 11:14:27 -07001324 if (!commit)
1325 continue;
1326 if (ctx->split) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06001327 if ((!parse_commit(commit) &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301328 commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) ||
Taylor Blau8a6ac282020-04-13 22:04:17 -06001329 flags == COMMIT_GRAPH_SPLIT_REPLACE)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001330 add_missing_parents(ctx, commit);
1331 } else if (!parse_commit_no_graph(commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001332 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001333 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001334 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001335
Derrick Stoleec9905be2019-06-12 06:29:40 -07001336 if (ctx->report_progress)
1337 ctx->progress = start_delayed_progress(
1338 _("Clearing commit marks in commit graph"),
1339 ctx->oids.nr);
1340 for (i = 0; i < ctx->oids.nr; i++) {
1341 display_progress(ctx->progress, i + 1);
1342 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001343
1344 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001345 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001346 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001347 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001348}
1349
Derrick Stoleec9905be2019-06-12 06:29:40 -07001350static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001351{
1352 int i;
1353 struct commit_list *list = NULL;
1354
Derrick Stoleec9905be2019-06-12 06:29:40 -07001355 if (ctx->report_progress)
Derrick Stoleeecc08692019-11-25 21:28:23 +00001356 ctx->progress = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001357 _("Computing commit graph generation numbers"),
1358 ctx->commits.nr);
1359 for (i = 0; i < ctx->commits.nr; i++) {
Abhishek Kumar48448122020-06-17 14:44:09 +05301360 uint32_t generation = commit_graph_data_at(ctx->commits.list[i])->generation;
1361
Derrick Stoleec9905be2019-06-12 06:29:40 -07001362 display_progress(ctx->progress, i + 1);
Abhishek Kumar48448122020-06-17 14:44:09 +05301363 if (generation != GENERATION_NUMBER_INFINITY &&
1364 generation != GENERATION_NUMBER_ZERO)
Derrick Stolee3258c662018-05-01 12:47:09 +00001365 continue;
1366
Derrick Stoleec9905be2019-06-12 06:29:40 -07001367 commit_list_insert(ctx->commits.list[i], &list);
Derrick Stolee3258c662018-05-01 12:47:09 +00001368 while (list) {
1369 struct commit *current = list->item;
1370 struct commit_list *parent;
1371 int all_parents_computed = 1;
1372 uint32_t max_generation = 0;
1373
1374 for (parent = current->parents; parent; parent = parent->next) {
Abhishek Kumar48448122020-06-17 14:44:09 +05301375 generation = commit_graph_data_at(parent->item)->generation;
1376
1377 if (generation == GENERATION_NUMBER_INFINITY ||
1378 generation == GENERATION_NUMBER_ZERO) {
Derrick Stolee3258c662018-05-01 12:47:09 +00001379 all_parents_computed = 0;
1380 commit_list_insert(parent->item, &list);
1381 break;
Abhishek Kumar48448122020-06-17 14:44:09 +05301382 } else if (generation > max_generation) {
1383 max_generation = generation;
Derrick Stolee3258c662018-05-01 12:47:09 +00001384 }
1385 }
1386
1387 if (all_parents_computed) {
Abhishek Kumar48448122020-06-17 14:44:09 +05301388 struct commit_graph_data *data = commit_graph_data_at(current);
1389
1390 data->generation = max_generation + 1;
Derrick Stolee3258c662018-05-01 12:47:09 +00001391 pop_commit(&list);
1392
Abhishek Kumar48448122020-06-17 14:44:09 +05301393 if (data->generation > GENERATION_NUMBER_MAX)
1394 data->generation = GENERATION_NUMBER_MAX;
Derrick Stolee3258c662018-05-01 12:47:09 +00001395 }
1396 }
1397 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001398 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001399}
1400
Taylor Blau312cff52020-09-16 14:07:32 -04001401static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx)
1402{
1403 trace2_data_intmax("commit-graph", ctx->r, "filter-computed",
1404 ctx->count_bloom_filter_computed);
1405 trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed",
1406 ctx->count_bloom_filter_not_computed);
Taylor Blau59f0d502020-09-17 22:59:44 -04001407 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty",
1408 ctx->count_bloom_filter_trunc_empty);
Taylor Blau312cff52020-09-16 14:07:32 -04001409 trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large",
1410 ctx->count_bloom_filter_trunc_large);
1411}
1412
Garima Singhf97b9322020-03-30 00:31:28 +00001413static void compute_bloom_filters(struct write_commit_graph_context *ctx)
1414{
1415 int i;
1416 struct progress *progress = NULL;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001417 struct commit **sorted_commits;
Taylor Blau809e0322020-09-18 09:27:27 -04001418 int max_new_filters;
Garima Singhf97b9322020-03-30 00:31:28 +00001419
1420 init_bloom_filters();
1421
1422 if (ctx->report_progress)
1423 progress = start_delayed_progress(
1424 _("Computing commit changed paths Bloom filters"),
1425 ctx->commits.nr);
1426
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001427 ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1428 COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
Garima Singh3d112752020-03-30 00:31:30 +00001429
1430 if (ctx->order_by_pack)
1431 QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);
1432 else
1433 QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp);
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001434
Taylor Blau809e0322020-09-18 09:27:27 -04001435 max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ?
1436 ctx->opts->max_new_filters : ctx->commits.nr;
1437
Garima Singhf97b9322020-03-30 00:31:28 +00001438 for (i = 0; i < ctx->commits.nr; i++) {
Taylor Blau312cff52020-09-16 14:07:32 -04001439 enum bloom_filter_computed computed = 0;
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001440 struct commit *c = sorted_commits[i];
Taylor Blau312cff52020-09-16 14:07:32 -04001441 struct bloom_filter *filter = get_or_compute_bloom_filter(
1442 ctx->r,
1443 c,
Taylor Blau809e0322020-09-18 09:27:27 -04001444 ctx->count_bloom_filter_computed < max_new_filters,
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04001445 ctx->bloom_settings,
Taylor Blau312cff52020-09-16 14:07:32 -04001446 &computed);
1447 if (computed & BLOOM_COMPUTED) {
1448 ctx->count_bloom_filter_computed++;
Taylor Blau59f0d502020-09-17 22:59:44 -04001449 if (computed & BLOOM_TRUNC_EMPTY)
1450 ctx->count_bloom_filter_trunc_empty++;
Taylor Blau312cff52020-09-16 14:07:32 -04001451 if (computed & BLOOM_TRUNC_LARGE)
1452 ctx->count_bloom_filter_trunc_large++;
1453 } else if (computed & BLOOM_NOT_COMPUTED)
1454 ctx->count_bloom_filter_not_computed++;
Taylor Blau809e0322020-09-18 09:27:27 -04001455 ctx->total_bloom_filter_data_size += filter
1456 ? sizeof(unsigned char) * filter->len : 0;
Garima Singhf97b9322020-03-30 00:31:28 +00001457 display_progress(progress, i + 1);
1458 }
1459
Taylor Blau312cff52020-09-16 14:07:32 -04001460 if (trace2_is_enabled())
1461 trace2_bloom_filter_write_statistics(ctx);
1462
Jeff Kingd21ee7d2020-03-30 00:31:29 +00001463 free(sorted_commits);
Garima Singhf97b9322020-03-30 00:31:28 +00001464 stop_progress(&progress);
1465}
1466
Taylor Blau1fe10842020-05-04 19:13:35 -06001467struct refs_cb_data {
1468 struct oidset *commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001469 struct progress *progress;
Taylor Blau1fe10842020-05-04 19:13:35 -06001470};
1471
Taylor Blau6830c362020-04-13 22:04:25 -06001472static int add_ref_to_set(const char *refname,
1473 const struct object_id *oid,
1474 int flags, void *cb_data)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001475{
Taylor Blau630cd512020-05-13 15:59:37 -06001476 struct object_id peeled;
Taylor Blau1fe10842020-05-04 19:13:35 -06001477 struct refs_cb_data *data = (struct refs_cb_data *)cb_data;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001478
Taylor Blau630cd512020-05-13 15:59:37 -06001479 if (!peel_ref(refname, &peeled))
1480 oid = &peeled;
1481 if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT)
1482 oidset_insert(data->commits, oid);
Taylor Blaud335ce82020-05-13 15:59:33 -06001483
1484 display_progress(data->progress, oidset_size(data->commits));
1485
Derrick Stolee59fb8772018-06-27 09:24:45 -04001486 return 0;
1487}
1488
Taylor Blau0bd52e22020-02-03 21:51:50 -08001489int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001490 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04001491 const struct commit_graph_opts *opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001492{
Taylor Blau6830c362020-04-13 22:04:25 -06001493 struct oidset commits = OIDSET_INIT;
Taylor Blau1fe10842020-05-04 19:13:35 -06001494 struct refs_cb_data data;
Derrick Stoleee103f722019-06-12 06:29:37 -07001495 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001496
Taylor Blau1fe10842020-05-04 19:13:35 -06001497 memset(&data, 0, sizeof(data));
1498 data.commits = &commits;
Taylor Blaud335ce82020-05-13 15:59:33 -06001499 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1500 data.progress = start_delayed_progress(
1501 _("Collecting referenced commits"), 0);
Taylor Blau1fe10842020-05-04 19:13:35 -06001502
1503 for_each_ref(add_ref_to_set, &data);
SZEDER Gábor6f9d5f22020-07-09 18:54:32 +02001504
1505 stop_progress(&data.progress);
1506
Taylor Blau6830c362020-04-13 22:04:25 -06001507 result = write_commit_graph(odb, NULL, &commits,
Taylor Blau98bb7962020-09-17 22:59:49 -04001508 flags, opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001509
Taylor Blau6830c362020-04-13 22:04:25 -06001510 oidset_clear(&commits);
Derrick Stoleee103f722019-06-12 06:29:37 -07001511 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001512}
1513
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001514static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1515 struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001516{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001517 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001518 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001519 struct strbuf packname = STRBUF_INIT;
1520 int dirlen;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001521
Taylor Blau0bd52e22020-02-03 21:51:50 -08001522 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001523 dirlen = packname.len;
1524 if (ctx->report_progress) {
1525 strbuf_addf(&progress_title,
1526 Q_("Finding commits for commit graph in %d pack",
1527 "Finding commits for commit graph in %d packs",
1528 pack_indexes->nr),
1529 pack_indexes->nr);
1530 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1531 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001532 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001533 for (i = 0; i < pack_indexes->nr; i++) {
1534 struct packed_git *p;
1535 strbuf_setlen(&packname, dirlen);
1536 strbuf_addstr(&packname, pack_indexes->items[i].string);
1537 p = add_packed_git(packname.buf, packname.len, 1);
1538 if (!p) {
1539 error(_("error adding pack %s"), packname.buf);
1540 return -1;
Derrick Stolee7547b952018-04-10 08:56:08 -04001541 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001542 if (open_pack_index(p)) {
1543 error(_("error opening index for %s"), packname.buf);
1544 return -1;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001545 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001546 for_each_object_in_pack(p, add_packed_commits, ctx,
1547 FOR_EACH_OBJECT_PACK_ORDER);
1548 close_pack(p);
1549 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001550 }
1551
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001552 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001553 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001554 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001555
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001556 return 0;
1557}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001558
Taylor Blau6830c362020-04-13 22:04:25 -06001559static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
1560 struct oidset *commits)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001561{
Taylor Blau6830c362020-04-13 22:04:25 -06001562 struct oidset_iter iter;
1563 struct object_id *oid;
1564
1565 if (!oidset_size(commits))
1566 return 0;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001567
Taylor Blau6830c362020-04-13 22:04:25 -06001568 oidset_iter_init(commits, &iter);
1569 while ((oid = oidset_iter_next(&iter))) {
Taylor Blau0ec2d0f2020-05-13 15:59:47 -06001570 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1571 oidcpy(&ctx->oids.list[ctx->oids.nr], oid);
1572 ctx->oids.nr++;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001573 }
Taylor Blau6830c362020-04-13 22:04:25 -06001574
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001575 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001576}
1577
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001578static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1579{
1580 if (ctx->report_progress)
1581 ctx->progress = start_delayed_progress(
1582 _("Finding commits for commit graph among packed objects"),
1583 ctx->approx_nr_objects);
1584 for_each_packed_object(add_packed_commits, ctx,
1585 FOR_EACH_OBJECT_PACK_ORDER);
1586 if (ctx->progress_done < ctx->approx_nr_objects)
1587 display_progress(ctx->progress, ctx->approx_nr_objects);
1588 stop_progress(&ctx->progress);
1589}
1590
Derrick Stolee014e3442019-06-12 06:29:43 -07001591static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1592{
1593 uint32_t i, count_distinct = 1;
1594
1595 if (ctx->report_progress)
1596 ctx->progress = start_delayed_progress(
1597 _("Counting distinct commits in commit graph"),
1598 ctx->oids.nr);
1599 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
Derrick Stolee3cbc6ed2019-06-18 11:14:24 -07001600 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
Derrick Stolee014e3442019-06-12 06:29:43 -07001601
1602 for (i = 1; i < ctx->oids.nr; i++) {
1603 display_progress(ctx->progress, i + 1);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001604 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1605 if (ctx->split) {
1606 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1607
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301608 if (!c || commit_graph_position(c) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001609 continue;
1610 }
1611
Derrick Stolee014e3442019-06-12 06:29:43 -07001612 count_distinct++;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001613 }
Derrick Stolee014e3442019-06-12 06:29:43 -07001614 }
1615 stop_progress(&ctx->progress);
1616
1617 return count_distinct;
1618}
1619
Derrick Stoleef998d542019-06-12 06:29:44 -07001620static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1621{
1622 uint32_t i;
Taylor Blau98bb7962020-09-17 22:59:49 -04001623 enum commit_graph_split_flags flags = ctx->opts ?
1624 ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stoleef998d542019-06-12 06:29:44 -07001625
1626 ctx->num_extra_edges = 0;
1627 if (ctx->report_progress)
1628 ctx->progress = start_delayed_progress(
1629 _("Finding extra edges in commit graph"),
1630 ctx->oids.nr);
1631 for (i = 0; i < ctx->oids.nr; i++) {
René Scharfe689a1462019-09-15 19:07:44 +02001632 unsigned int num_parents;
1633
Derrick Stoleef998d542019-06-12 06:29:44 -07001634 display_progress(ctx->progress, i + 1);
1635 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1636 continue;
1637
Derrick Stolee6c622f92019-06-18 11:14:27 -07001638 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Derrick Stoleef998d542019-06-12 06:29:44 -07001639 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001640
Taylor Blau8a6ac282020-04-13 22:04:17 -06001641 if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05301642 commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
Derrick Stolee6c622f92019-06-18 11:14:27 -07001643 continue;
1644
Taylor Blau8a6ac282020-04-13 22:04:17 -06001645 if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE)
1646 parse_commit(ctx->commits.list[ctx->commits.nr]);
1647 else
1648 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
Derrick Stoleef998d542019-06-12 06:29:44 -07001649
René Scharfe689a1462019-09-15 19:07:44 +02001650 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001651 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001652 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001653
Derrick Stoleef998d542019-06-12 06:29:44 -07001654 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001655 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001656 stop_progress(&ctx->progress);
1657}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001658
Derrick Stolee6c622f92019-06-18 11:14:27 -07001659static int write_graph_chunk_base_1(struct hashfile *f,
1660 struct commit_graph *g)
1661{
1662 int num = 0;
1663
1664 if (!g)
1665 return 0;
1666
1667 num = write_graph_chunk_base_1(f, g->base_graph);
1668 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1669 return num + 1;
1670}
1671
1672static int write_graph_chunk_base(struct hashfile *f,
1673 struct write_commit_graph_context *ctx)
1674{
1675 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1676
1677 if (num != ctx->num_commit_graphs_after - 1) {
1678 error(_("failed to write correct number of base graph ids"));
1679 return -1;
1680 }
1681
1682 return 0;
1683}
1684
SZEDER Gábor17e62752020-07-01 13:27:26 +00001685typedef int (*chunk_write_fn)(struct hashfile *f,
1686 struct write_commit_graph_context *ctx);
1687
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001688struct chunk_info {
1689 uint32_t id;
1690 uint64_t size;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001691 chunk_write_fn write_fn;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001692};
1693
Derrick Stolee238def52019-06-12 06:29:45 -07001694static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1695{
1696 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001697 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07001698 struct hashfile *f;
1699 struct lock_file lk = LOCK_INIT;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001700 struct chunk_info chunks[MAX_NUM_CHUNKS + 1];
Derrick Stolee238def52019-06-12 06:29:45 -07001701 const unsigned hashsz = the_hash_algo->rawsz;
1702 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee144354b2019-06-18 11:14:27 -07001703 int num_chunks = 3;
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001704 uint64_t chunk_offset;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001705 struct object_id file_hash;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001706
Derrick Stolee6c622f92019-06-18 11:14:27 -07001707 if (ctx->split) {
1708 struct strbuf tmp_file = STRBUF_INIT;
1709
1710 strbuf_addf(&tmp_file,
1711 "%s/info/commit-graphs/tmp_graph_XXXXXX",
Taylor Blau0bd52e22020-02-03 21:51:50 -08001712 ctx->odb->path);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001713 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1714 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001715 ctx->graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001716 }
1717
Derrick Stolee238def52019-06-12 06:29:45 -07001718 if (safe_create_leading_directories(ctx->graph_name)) {
1719 UNLEAK(ctx->graph_name);
1720 error(_("unable to create leading directories of %s"),
1721 ctx->graph_name);
1722 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001723 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001724
Derrick Stolee6c622f92019-06-18 11:14:27 -07001725 if (ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00001726 char *lock_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001727
Taylor Blau45a43652020-04-29 11:36:46 -06001728 hold_lock_file_for_update_mode(&lk, lock_name,
1729 LOCK_DIE_ON_ERROR, 0444);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001730
1731 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1732 if (fd < 0) {
Taylor Blaua2d57e22020-04-23 15:41:02 -06001733 error(_("unable to create temporary graph layer"));
Derrick Stolee6c622f92019-06-18 11:14:27 -07001734 return -1;
1735 }
1736
Taylor Blauf4d62842020-04-29 11:36:42 -06001737 if (adjust_shared_perm(ctx->graph_name)) {
1738 error(_("unable to adjust shared permissions for '%s'"),
1739 ctx->graph_name);
1740 return -1;
1741 }
1742
Derrick Stolee6c622f92019-06-18 11:14:27 -07001743 f = hashfd(fd, ctx->graph_name);
1744 } else {
Taylor Blau1f9beca2020-04-29 11:36:38 -06001745 hold_lock_file_for_update_mode(&lk, ctx->graph_name,
1746 LOCK_DIE_ON_ERROR, 0444);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001747 fd = lk.tempfile->fd;
1748 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1749 }
Derrick Stolee238def52019-06-12 06:29:45 -07001750
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001751 chunks[0].id = GRAPH_CHUNKID_OIDFANOUT;
1752 chunks[0].size = GRAPH_FANOUT_SIZE;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001753 chunks[0].write_fn = write_graph_chunk_fanout;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001754 chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP;
1755 chunks[1].size = hashsz * ctx->commits.nr;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001756 chunks[1].write_fn = write_graph_chunk_oids;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001757 chunks[2].id = GRAPH_CHUNKID_DATA;
1758 chunks[2].size = (hashsz + 16) * ctx->commits.nr;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001759 chunks[2].write_fn = write_graph_chunk_data;
Derrick Stolee144354b2019-06-18 11:14:27 -07001760 if (ctx->num_extra_edges) {
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001761 chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES;
1762 chunks[num_chunks].size = 4 * ctx->num_extra_edges;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001763 chunks[num_chunks].write_fn = write_graph_chunk_extra_edges;
Derrick Stolee144354b2019-06-18 11:14:27 -07001764 num_chunks++;
1765 }
Garima Singh76ffbca2020-04-06 16:59:49 +00001766 if (ctx->changed_paths) {
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001767 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES;
1768 chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001769 chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes;
Garima Singh76ffbca2020-04-06 16:59:49 +00001770 num_chunks++;
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001771 chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA;
1772 chunks[num_chunks].size = sizeof(uint32_t) * 3
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001773 + ctx->total_bloom_filter_data_size;
SZEDER Gábor17e62752020-07-01 13:27:26 +00001774 chunks[num_chunks].write_fn = write_graph_chunk_bloom_data;
Garima Singh76ffbca2020-04-06 16:59:49 +00001775 num_chunks++;
1776 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001777 if (ctx->num_commit_graphs_after > 1) {
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001778 chunks[num_chunks].id = GRAPH_CHUNKID_BASE;
1779 chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1);
SZEDER Gábor17e62752020-07-01 13:27:26 +00001780 chunks[num_chunks].write_fn = write_graph_chunk_base;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001781 num_chunks++;
1782 }
Derrick Stolee144354b2019-06-18 11:14:27 -07001783
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001784 chunks[num_chunks].id = 0;
1785 chunks[num_chunks].size = 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001786
1787 hashwrite_be32(f, GRAPH_SIGNATURE);
1788
1789 hashwrite_u8(f, GRAPH_VERSION);
brian m. carlsonc1665992018-11-14 04:09:35 +00001790 hashwrite_u8(f, oid_version());
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001791 hashwrite_u8(f, num_chunks);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001792 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001793
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001794 chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001795 for (i = 0; i <= num_chunks; i++) {
1796 uint32_t chunk_write[3];
1797
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001798 chunk_write[0] = htonl(chunks[i].id);
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001799 chunk_write[1] = htonl(chunk_offset >> 32);
1800 chunk_write[2] = htonl(chunk_offset & 0xffffffff);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001801 hashwrite(f, chunk_write, 12);
SZEDER Gáborbb4d60e2020-06-05 13:00:31 +00001802
SZEDER Gábor7fbfe072020-06-05 13:00:32 +00001803 chunk_offset += chunks[i].size;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001804 }
1805
Derrick Stolee238def52019-06-12 06:29:45 -07001806 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001807 strbuf_addf(&progress_title,
1808 Q_("Writing out commit graph in %d pass",
1809 "Writing out commit graph in %d passes",
1810 num_chunks),
1811 num_chunks);
Derrick Stolee238def52019-06-12 06:29:45 -07001812 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001813 progress_title.buf,
Derrick Stolee238def52019-06-12 06:29:45 -07001814 num_chunks * ctx->commits.nr);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001815 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00001816
1817 for (i = 0; i < num_chunks; i++) {
SZEDER Gábor2dd4fed2020-07-01 13:27:27 +00001818 uint64_t start_offset = f->total + f->offset;
1819
SZEDER Gábor17e62752020-07-01 13:27:26 +00001820 if (chunks[i].write_fn(f, ctx))
1821 return -1;
SZEDER Gábor2dd4fed2020-07-01 13:27:27 +00001822
1823 if (f->total + f->offset != start_offset + chunks[i].size)
1824 BUG("expected to write %"PRId64" bytes to chunk %"PRIx32", but wrote %"PRId64" instead",
1825 chunks[i].size, chunks[i].id,
1826 f->total + f->offset - start_offset);
Garima Singh76ffbca2020-04-06 16:59:49 +00001827 }
SZEDER Gábor17e62752020-07-01 13:27:26 +00001828
Derrick Stolee238def52019-06-12 06:29:45 -07001829 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001830 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001831
Derrick Stolee6c622f92019-06-18 11:14:27 -07001832 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1833 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001834 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001835
1836 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1837 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1838 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1839 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1840 }
1841
Derrick Stoleec3a3a962019-05-17 11:41:47 -07001842 close_commit_graph(ctx->r->objects);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001843 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1844
1845 if (ctx->split) {
1846 FILE *chainf = fdopen_lock_file(&lk, "w");
1847 char *final_graph_name;
1848 int result;
1849
1850 close(fd);
1851
1852 if (!chainf) {
1853 error(_("unable to open commit-graph chain file"));
1854 return -1;
1855 }
1856
1857 if (ctx->base_graph_name) {
Taylor Blau8a6ac282020-04-13 22:04:17 -06001858 const char *dest;
1859 int idx = ctx->num_commit_graphs_after - 1;
1860 if (ctx->num_commit_graphs_after > 1)
1861 idx--;
1862
1863 dest = ctx->commit_graph_filenames_after[idx];
Derrick Stolee6c622f92019-06-18 11:14:27 -07001864
Derrick Stolee135a7122019-06-18 11:14:28 -07001865 if (strcmp(ctx->base_graph_name, dest)) {
1866 result = rename(ctx->base_graph_name, dest);
1867
1868 if (result) {
1869 error(_("failed to rename base commit-graph file"));
1870 return -1;
1871 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001872 }
1873 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001874 char *graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001875 unlink(graph_name);
1876 }
1877
1878 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001879 final_graph_name = get_split_graph_filename(ctx->odb,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001880 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1881 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1882
1883 result = rename(ctx->graph_name, final_graph_name);
1884
1885 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1886 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1887
1888 if (result) {
1889 error(_("failed to rename temporary commit-graph file"));
1890 return -1;
1891 }
1892 }
1893
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001894 commit_lock_file(&lk);
1895
Derrick Stolee238def52019-06-12 06:29:45 -07001896 return 0;
1897}
1898
Derrick Stolee1771be92019-06-18 11:14:29 -07001899static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1900{
Alex Henrie8da02ce2019-09-30 20:29:34 -06001901 struct commit_graph *g;
1902 uint32_t num_commits;
Taylor Blaufdbde822020-04-13 22:04:12 -06001903 enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED;
Derrick Stolee1771be92019-06-18 11:14:29 -07001904 uint32_t i;
1905
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001906 int max_commits = 0;
1907 int size_mult = 2;
1908
Taylor Blau98bb7962020-09-17 22:59:49 -04001909 if (ctx->opts) {
1910 max_commits = ctx->opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00001911
Taylor Blau98bb7962020-09-17 22:59:49 -04001912 if (ctx->opts->size_multiple)
1913 size_mult = ctx->opts->size_multiple;
Taylor Blaufdbde822020-04-13 22:04:12 -06001914
Taylor Blau98bb7962020-09-17 22:59:49 -04001915 flags = ctx->opts->split_flags;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001916 }
1917
Derrick Stolee1771be92019-06-18 11:14:29 -07001918 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06001919 num_commits = ctx->commits.nr;
Taylor Blau8a6ac282020-04-13 22:04:17 -06001920 if (flags == COMMIT_GRAPH_SPLIT_REPLACE)
1921 ctx->num_commit_graphs_after = 1;
1922 else
1923 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07001924
Taylor Blau8a6ac282020-04-13 22:04:17 -06001925 if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED &&
1926 flags != COMMIT_GRAPH_SPLIT_REPLACE) {
Taylor Blaufdbde822020-04-13 22:04:12 -06001927 while (g && (g->num_commits <= size_mult * num_commits ||
1928 (max_commits && num_commits > max_commits))) {
1929 if (g->odb != ctx->odb)
1930 break;
Derrick Stoleec5230352019-06-18 11:14:30 -07001931
Taylor Blaufdbde822020-04-13 22:04:12 -06001932 num_commits += g->num_commits;
1933 g = g->base_graph;
Derrick Stolee1771be92019-06-18 11:14:29 -07001934
Taylor Blaufdbde822020-04-13 22:04:12 -06001935 ctx->num_commit_graphs_after--;
1936 }
Derrick Stolee1771be92019-06-18 11:14:29 -07001937 }
1938
Taylor Blau8a6ac282020-04-13 22:04:17 -06001939 if (flags != COMMIT_GRAPH_SPLIT_REPLACE)
1940 ctx->new_base_graph = g;
1941 else if (ctx->num_commit_graphs_after != 1)
1942 BUG("split_graph_merge_strategy: num_commit_graphs_after "
1943 "should be 1 with --split=replace");
Derrick Stolee1771be92019-06-18 11:14:29 -07001944
Derrick Stoleec5230352019-06-18 11:14:30 -07001945 if (ctx->num_commit_graphs_after == 2) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001946 char *old_graph_name = get_commit_graph_filename(g->odb);
Derrick Stoleec5230352019-06-18 11:14:30 -07001947
1948 if (!strcmp(g->filename, old_graph_name) &&
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001949 g->odb != ctx->odb) {
Derrick Stoleec5230352019-06-18 11:14:30 -07001950 ctx->num_commit_graphs_after = 1;
1951 ctx->new_base_graph = NULL;
1952 }
1953
1954 free(old_graph_name);
1955 }
1956
Taylor Blaub78a5562020-04-23 15:41:09 -06001957 CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1958 CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
Derrick Stolee1771be92019-06-18 11:14:29 -07001959
1960 for (i = 0; i < ctx->num_commit_graphs_after &&
1961 i < ctx->num_commit_graphs_before; i++)
1962 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1963
1964 i = ctx->num_commit_graphs_before - 1;
1965 g = ctx->r->objects->commit_graph;
1966
1967 while (g) {
1968 if (i < ctx->num_commit_graphs_after)
1969 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1970
1971 i--;
1972 g = g->base_graph;
1973 }
1974}
1975
1976static void merge_commit_graph(struct write_commit_graph_context *ctx,
1977 struct commit_graph *g)
1978{
1979 uint32_t i;
1980 uint32_t offset = g->num_commits_in_base;
1981
1982 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1983
1984 for (i = 0; i < g->num_commits; i++) {
1985 struct object_id oid;
1986 struct commit *result;
1987
1988 display_progress(ctx->progress, i + 1);
1989
1990 load_oid_from_graph(g, i + offset, &oid);
1991
1992 /* only add commits if they still exist in the repo */
1993 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1994
1995 if (result) {
1996 ctx->commits.list[ctx->commits.nr] = result;
1997 ctx->commits.nr++;
1998 }
1999 }
2000}
2001
2002static int commit_compare(const void *_a, const void *_b)
2003{
2004 const struct commit *a = *(const struct commit **)_a;
2005 const struct commit *b = *(const struct commit **)_b;
2006 return oidcmp(&a->object.oid, &b->object.oid);
2007}
2008
2009static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
2010{
Derrick Stolee150f1152020-10-09 20:53:51 +00002011 uint32_t i, dedup_i = 0;
Derrick Stolee1771be92019-06-18 11:14:29 -07002012
2013 if (ctx->report_progress)
2014 ctx->progress = start_delayed_progress(
2015 _("Scanning merged commits"),
2016 ctx->commits.nr);
2017
2018 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
2019
2020 ctx->num_extra_edges = 0;
2021 for (i = 0; i < ctx->commits.nr; i++) {
2022 display_progress(ctx->progress, i);
2023
2024 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
2025 &ctx->commits.list[i]->object.oid)) {
Derrick Stolee150f1152020-10-09 20:53:51 +00002026 /*
2027 * Silently ignore duplicates. These were likely
2028 * created due to a commit appearing in multiple
2029 * layers of the chain, which is unexpected but
2030 * not invalid. We should make sure there is a
2031 * unique copy in the new layer.
2032 */
Derrick Stolee1771be92019-06-18 11:14:29 -07002033 } else {
René Scharfe689a1462019-09-15 19:07:44 +02002034 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07002035
Derrick Stolee150f1152020-10-09 20:53:51 +00002036 ctx->commits.list[dedup_i] = ctx->commits.list[i];
2037 dedup_i++;
2038
René Scharfe689a1462019-09-15 19:07:44 +02002039 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07002040 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07002041 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07002042 }
2043 }
2044
Derrick Stolee150f1152020-10-09 20:53:51 +00002045 ctx->commits.nr = dedup_i;
2046
Derrick Stolee1771be92019-06-18 11:14:29 -07002047 stop_progress(&ctx->progress);
2048}
2049
2050static void merge_commit_graphs(struct write_commit_graph_context *ctx)
2051{
2052 struct commit_graph *g = ctx->r->objects->commit_graph;
2053 uint32_t current_graph_number = ctx->num_commit_graphs_before;
Derrick Stolee1771be92019-06-18 11:14:29 -07002054
2055 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
2056 current_graph_number--;
2057
René Scharfed68ce902020-02-20 19:49:18 +01002058 if (ctx->report_progress)
2059 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
Derrick Stolee1771be92019-06-18 11:14:29 -07002060
2061 merge_commit_graph(ctx, g);
2062 stop_progress(&ctx->progress);
Derrick Stolee1771be92019-06-18 11:14:29 -07002063
2064 g = g->base_graph;
2065 }
2066
2067 if (g) {
2068 ctx->new_base_graph = g;
2069 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
2070 }
2071
2072 if (ctx->new_base_graph)
2073 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
2074
2075 sort_and_scan_merged_commits(ctx);
2076}
2077
Derrick Stolee8d840972019-06-18 11:14:31 -07002078static void mark_commit_graphs(struct write_commit_graph_context *ctx)
2079{
2080 uint32_t i;
2081 time_t now = time(NULL);
2082
2083 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
2084 struct stat st;
2085 struct utimbuf updated_time;
2086
2087 stat(ctx->commit_graph_filenames_before[i], &st);
2088
2089 updated_time.actime = st.st_atime;
2090 updated_time.modtime = now;
2091 utime(ctx->commit_graph_filenames_before[i], &updated_time);
2092 }
2093}
2094
2095static void expire_commit_graphs(struct write_commit_graph_context *ctx)
2096{
2097 struct strbuf path = STRBUF_INIT;
2098 DIR *dir;
2099 struct dirent *de;
2100 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07002101 timestamp_t expire_time = time(NULL);
2102
Taylor Blau98bb7962020-09-17 22:59:49 -04002103 if (ctx->opts && ctx->opts->expire_time)
2104 expire_time = ctx->opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07002105 if (!ctx->split) {
Derrick Stolee663b2b12020-09-17 18:11:46 +00002106 char *chain_file_name = get_commit_graph_chain_filename(ctx->odb);
Derrick Stoleeba411122019-06-18 11:14:33 -07002107 unlink(chain_file_name);
2108 free(chain_file_name);
2109 ctx->num_commit_graphs_after = 0;
2110 }
Derrick Stolee8d840972019-06-18 11:14:31 -07002111
Taylor Blau0bd52e22020-02-03 21:51:50 -08002112 strbuf_addstr(&path, ctx->odb->path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002113 strbuf_addstr(&path, "/info/commit-graphs");
2114 dir = opendir(path.buf);
2115
René Scharfe0aa6bce2019-08-07 13:15:02 +02002116 if (!dir)
2117 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07002118
2119 strbuf_addch(&path, '/');
2120 dirnamelen = path.len;
2121 while ((de = readdir(dir)) != NULL) {
2122 struct stat st;
2123 uint32_t i, found = 0;
2124
2125 strbuf_setlen(&path, dirnamelen);
2126 strbuf_addstr(&path, de->d_name);
2127
2128 stat(path.buf, &st);
2129
2130 if (st.st_mtime > expire_time)
2131 continue;
2132 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
2133 continue;
2134
2135 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2136 if (!strcmp(ctx->commit_graph_filenames_after[i],
2137 path.buf)) {
2138 found = 1;
2139 break;
2140 }
2141 }
2142
2143 if (!found)
2144 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07002145 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02002146
2147out:
2148 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07002149}
2150
Taylor Blau0bd52e22020-02-03 21:51:50 -08002151int write_commit_graph(struct object_directory *odb,
Derrick Stoleee103f722019-06-12 06:29:37 -07002152 struct string_list *pack_indexes,
Taylor Blau6830c362020-04-13 22:04:25 -06002153 struct oidset *commits,
SZEDER Gábor39d88312019-08-05 10:02:39 +02002154 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -04002155 const struct commit_graph_opts *opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002156{
Derrick Stoleec9905be2019-06-12 06:29:40 -07002157 struct write_commit_graph_context *ctx;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002158 uint32_t i, count_distinct = 0;
Derrick Stoleee103f722019-06-12 06:29:37 -07002159 int res = 0;
Taylor Blau8a6ac282020-04-13 22:04:17 -06002160 int replace = 0;
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002161 struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002162
Derrick Stolee85102ac2020-10-09 20:53:52 +00002163 prepare_repo_settings(the_repository);
2164 if (!the_repository->settings.core_commit_graph) {
2165 warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled"));
2166 return 0;
2167 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002168 if (!commit_graph_compatible(the_repository))
Derrick Stoleee103f722019-06-12 06:29:37 -07002169 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002170
Derrick Stoleec9905be2019-06-12 06:29:40 -07002171 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
2172 ctx->r = the_repository;
Taylor Blau0bd52e22020-02-03 21:51:50 -08002173 ctx->odb = odb;
SZEDER Gábor39d88312019-08-05 10:02:39 +02002174 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
2175 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
2176 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
Taylor Blau98bb7962020-09-17 22:59:49 -04002177 ctx->opts = opts;
Garima Singhf97b9322020-03-30 00:31:28 +00002178 ctx->total_bloom_filter_data_size = 0;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002179
Taylor Blau9a7a9ed2020-09-16 14:07:46 -04002180 bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY",
2181 bloom_settings.bits_per_entry);
2182 bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES",
2183 bloom_settings.num_hashes);
2184 bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS",
2185 bloom_settings.max_changed_paths);
2186 ctx->bloom_settings = &bloom_settings;
2187
Derrick Stolee0087a872020-07-01 13:27:24 +00002188 if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS)
2189 ctx->changed_paths = 1;
2190 if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) {
2191 struct commit_graph *g;
2192 prepare_commit_graph_one(ctx->r, ctx->odb);
2193
2194 g = ctx->r->objects->commit_graph;
2195
2196 /* We have changed-paths already. Keep them in the next graph */
2197 if (g && g->chunk_bloom_data) {
2198 ctx->changed_paths = 1;
2199 ctx->bloom_settings = g->bloom_filter_settings;
2200 }
2201 }
2202
Derrick Stolee6c622f92019-06-18 11:14:27 -07002203 if (ctx->split) {
2204 struct commit_graph *g;
2205 prepare_commit_graph(ctx->r);
2206
2207 g = ctx->r->objects->commit_graph;
2208
2209 while (g) {
2210 ctx->num_commit_graphs_before++;
2211 g = g->base_graph;
2212 }
2213
2214 if (ctx->num_commit_graphs_before) {
2215 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
2216 i = ctx->num_commit_graphs_before;
2217 g = ctx->r->objects->commit_graph;
2218
2219 while (g) {
2220 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
2221 g = g->base_graph;
2222 }
2223 }
Taylor Blau8a6ac282020-04-13 22:04:17 -06002224
Taylor Blau98bb7962020-09-17 22:59:49 -04002225 if (ctx->opts)
2226 replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE;
Derrick Stolee6c622f92019-06-18 11:14:27 -07002227 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002228
Derrick Stoleec9905be2019-06-12 06:29:40 -07002229 ctx->approx_nr_objects = approximate_object_count();
2230 ctx->oids.alloc = ctx->approx_nr_objects / 32;
2231
Taylor Blau98bb7962020-09-17 22:59:49 -04002232 if (ctx->split && opts && ctx->oids.alloc > opts->max_commits)
2233 ctx->oids.alloc = opts->max_commits;
Derrick Stolee1771be92019-06-18 11:14:29 -07002234
Derrick Stoleec9905be2019-06-12 06:29:40 -07002235 if (ctx->append) {
Taylor Blau13c24992020-02-03 13:18:00 -08002236 prepare_commit_graph_one(ctx->r, ctx->odb);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002237 if (ctx->r->objects->commit_graph)
2238 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002239 }
2240
Derrick Stoleec9905be2019-06-12 06:29:40 -07002241 if (ctx->oids.alloc < 1024)
2242 ctx->oids.alloc = 1024;
2243 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002244
Derrick Stoleec9905be2019-06-12 06:29:40 -07002245 if (ctx->append && ctx->r->objects->commit_graph) {
2246 struct commit_graph *g = ctx->r->objects->commit_graph;
2247 for (i = 0; i < g->num_commits; i++) {
2248 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
2249 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002250 }
2251 }
2252
2253 if (pack_indexes) {
Garima Singh3d112752020-03-30 00:31:30 +00002254 ctx->order_by_pack = 1;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07002255 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
2256 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002257 }
2258
Taylor Blau6830c362020-04-13 22:04:25 -06002259 if (commits) {
2260 if ((res = fill_oids_from_commits(ctx, commits)))
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02002261 goto cleanup;
2262 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002263
Junio C Hamano9b6606f2020-05-01 13:39:53 -07002264 if (!pack_indexes && !commits) {
Garima Singh3d112752020-03-30 00:31:30 +00002265 ctx->order_by_pack = 1;
Derrick Stoleeb2c83062019-06-12 06:29:42 -07002266 fill_oids_from_all_packs(ctx);
Garima Singh3d112752020-03-30 00:31:30 +00002267 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002268
Derrick Stoleec9905be2019-06-12 06:29:40 -07002269 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002270
Derrick Stolee014e3442019-06-12 06:29:43 -07002271 count_distinct = count_distinct_commits(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002272
Derrick Stoleee103f722019-06-12 06:29:37 -07002273 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
2274 error(_("the commit graph format cannot write %d commits"), count_distinct);
2275 res = -1;
2276 goto cleanup;
2277 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002278
Derrick Stoleec9905be2019-06-12 06:29:40 -07002279 ctx->commits.alloc = count_distinct;
2280 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002281
Derrick Stoleef998d542019-06-12 06:29:44 -07002282 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04002283
Derrick Stoleec9905be2019-06-12 06:29:40 -07002284 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07002285 error(_("too many commits to write graph"));
2286 res = -1;
2287 goto cleanup;
2288 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04002289
Taylor Blau8a6ac282020-04-13 22:04:17 -06002290 if (!ctx->commits.nr && !replace)
Derrick Stolee6c622f92019-06-18 11:14:27 -07002291 goto cleanup;
2292
Derrick Stolee1771be92019-06-18 11:14:29 -07002293 if (ctx->split) {
2294 split_graph_merge_strategy(ctx);
2295
Taylor Blau8a6ac282020-04-13 22:04:17 -06002296 if (!replace)
2297 merge_commit_graphs(ctx);
Derrick Stolee1771be92019-06-18 11:14:29 -07002298 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07002299 ctx->num_commit_graphs_after = 1;
2300
Derrick Stoleec9905be2019-06-12 06:29:40 -07002301 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002302
Garima Singhf97b9322020-03-30 00:31:28 +00002303 if (ctx->changed_paths)
2304 compute_bloom_filters(ctx);
2305
Derrick Stolee238def52019-06-12 06:29:45 -07002306 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002307
Derrick Stoleeba411122019-06-18 11:14:33 -07002308 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07002309 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07002310
2311 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07002312
Derrick Stoleee103f722019-06-12 06:29:37 -07002313cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07002314 free(ctx->graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07002315 free(ctx->commits.list);
2316 free(ctx->oids.list);
Derrick Stolee6c622f92019-06-18 11:14:27 -07002317
2318 if (ctx->commit_graph_filenames_after) {
2319 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
2320 free(ctx->commit_graph_filenames_after[i]);
2321 free(ctx->commit_graph_hash_after[i]);
2322 }
2323
2324 for (i = 0; i < ctx->num_commit_graphs_before; i++)
2325 free(ctx->commit_graph_filenames_before[i]);
2326
2327 free(ctx->commit_graph_filenames_after);
2328 free(ctx->commit_graph_filenames_before);
2329 free(ctx->commit_graph_hash_after);
2330 }
2331
Derrick Stoleec9905be2019-06-12 06:29:40 -07002332 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07002333
2334 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002335}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04002336
2337#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
2338static int verify_commit_graph_error;
2339
Derrick Stolee283e68c2018-06-27 09:24:32 -04002340static void graph_report(const char *fmt, ...)
2341{
2342 va_list ap;
2343
2344 verify_commit_graph_error = 1;
2345 va_start(ap, fmt);
2346 vfprintf(stderr, fmt, ap);
2347 fprintf(stderr, "\n");
2348 va_end(ap);
2349}
2350
2351#define GENERATION_ZERO_EXISTS 1
2352#define GENERATION_NUMBER_EXISTS 2
2353
Derrick Stolee3da4b602019-06-18 11:14:32 -07002354int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
Derrick Stolee283e68c2018-06-27 09:24:32 -04002355{
Derrick Stolee9bda8462018-06-27 09:24:35 -04002356 uint32_t i, cur_fanout_pos = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04002357 struct object_id prev_oid, cur_oid, checksum;
Derrick Stolee1373e542018-06-27 09:24:39 -04002358 int generation_zero = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04002359 struct hashfile *f;
2360 int devnull;
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002361 struct progress *progress = NULL;
Derrick Stolee3da4b602019-06-18 11:14:32 -07002362 int local_error = 0;
Derrick Stolee9bda8462018-06-27 09:24:35 -04002363
Derrick Stolee283e68c2018-06-27 09:24:32 -04002364 if (!g) {
2365 graph_report("no commit-graph file loaded");
2366 return 1;
2367 }
2368
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +01002369 verify_commit_graph_error = verify_commit_graph_lite(g);
Derrick Stolee9bda8462018-06-27 09:24:35 -04002370 if (verify_commit_graph_error)
2371 return verify_commit_graph_error;
2372
Derrick Stolee41df0e32018-06-27 09:24:42 -04002373 devnull = open("/dev/null", O_WRONLY);
2374 f = hashfd(devnull, NULL);
2375 hashwrite(f, g->data, g->data_len - g->hash_len);
2376 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
Jeff King67947c32018-08-28 17:22:52 -04002377 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04002378 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
2379 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
2380 }
2381
Derrick Stolee9bda8462018-06-27 09:24:35 -04002382 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002383 struct commit *graph_commit;
2384
Derrick Stolee9bda8462018-06-27 09:24:35 -04002385 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2386
2387 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002388 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002389 oid_to_hex(&prev_oid),
2390 oid_to_hex(&cur_oid));
2391
2392 oidcpy(&prev_oid, &cur_oid);
2393
2394 while (cur_oid.hash[0] > cur_fanout_pos) {
2395 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2396
2397 if (i != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002398 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002399 cur_fanout_pos, fanout_value, i);
2400 cur_fanout_pos++;
2401 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002402
Junio C Hamano82952962018-07-17 15:46:19 -07002403 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08002404 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002405 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002406 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04002407 }
2408
2409 while (cur_fanout_pos < 256) {
2410 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
2411
2412 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002413 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04002414 cur_fanout_pos, fanout_value, i);
2415
2416 cur_fanout_pos++;
2417 }
2418
Derrick Stolee41df0e32018-06-27 09:24:42 -04002419 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04002420 return verify_commit_graph_error;
2421
Garima Singh73716122019-08-26 09:29:58 -07002422 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2423 progress = start_progress(_("Verifying commits in commit graph"),
2424 g->num_commits);
2425
Derrick Stolee96af91d2018-06-27 09:24:36 -04002426 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002427 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04002428 struct commit_list *graph_parents, *odb_parents;
Derrick Stolee1373e542018-06-27 09:24:39 -04002429 uint32_t max_generation = 0;
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302430 uint32_t generation;
Derrick Stolee96af91d2018-06-27 09:24:36 -04002431
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002432 display_progress(progress, i + 1);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002433 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2434
Junio C Hamano82952962018-07-17 15:46:19 -07002435 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002436 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002437 if (parse_commit_internal(odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002438 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002439 oid_to_hex(&cur_oid));
2440 continue;
2441 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002442
Stefan Beller4f542b72018-12-14 16:09:39 -08002443 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002444 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002445 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002446 oid_to_hex(&cur_oid),
2447 oid_to_hex(get_commit_tree_oid(graph_commit)),
2448 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002449
2450 graph_parents = graph_commit->parents;
2451 odb_parents = odb_commit->parents;
2452
2453 while (graph_parents) {
2454 if (odb_parents == NULL) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002455 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002456 oid_to_hex(&cur_oid));
2457 break;
2458 }
2459
Derrick Stolee3da4b602019-06-18 11:14:32 -07002460 /* parse parent in case it is in a base graph */
2461 parse_commit_in_graph_one(r, g, graph_parents->item);
2462
Jeff King9001dc22018-08-28 17:22:48 -04002463 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002464 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002465 oid_to_hex(&cur_oid),
2466 oid_to_hex(&graph_parents->item->object.oid),
2467 oid_to_hex(&odb_parents->item->object.oid));
2468
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302469 generation = commit_graph_generation(graph_parents->item);
2470 if (generation > max_generation)
2471 max_generation = generation;
Derrick Stolee1373e542018-06-27 09:24:39 -04002472
Derrick Stolee53614b12018-06-27 09:24:38 -04002473 graph_parents = graph_parents->next;
2474 odb_parents = odb_parents->next;
2475 }
2476
2477 if (odb_parents != NULL)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002478 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002479 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002480
Abhishek Kumarc49c82a2020-06-17 14:44:10 +05302481 if (!commit_graph_generation(graph_commit)) {
Derrick Stolee1373e542018-06-27 09:24:39 -04002482 if (generation_zero == GENERATION_NUMBER_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002483 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002484 oid_to_hex(&cur_oid));
2485 generation_zero = GENERATION_ZERO_EXISTS;
2486 } else if (generation_zero == GENERATION_ZERO_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002487 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002488 oid_to_hex(&cur_oid));
2489
2490 if (generation_zero == GENERATION_ZERO_EXISTS)
2491 continue;
2492
2493 /*
2494 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2495 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2496 * extra logic in the following condition.
2497 */
2498 if (max_generation == GENERATION_NUMBER_MAX)
2499 max_generation--;
2500
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302501 generation = commit_graph_generation(graph_commit);
2502 if (generation != max_generation + 1)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002503 graph_report(_("commit-graph generation for commit %s is %u != %u"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002504 oid_to_hex(&cur_oid),
Abhishek Kumarc752ad02020-06-17 14:44:11 +05302505 generation,
Derrick Stolee1373e542018-06-27 09:24:39 -04002506 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002507
2508 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002509 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002510 oid_to_hex(&cur_oid),
2511 graph_commit->date,
2512 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002513 }
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002514 stop_progress(&progress);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002515
Derrick Stolee3da4b602019-06-18 11:14:32 -07002516 local_error = verify_commit_graph_error;
2517
2518 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2519 local_error |= verify_commit_graph(r, g->base_graph, flags);
2520
2521 return local_error;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002522}
Jonathan Tanc3756d52018-07-11 15:42:40 -07002523
2524void free_commit_graph(struct commit_graph *g)
2525{
2526 if (!g)
2527 return;
Jeff Kingc8828532020-04-23 15:41:13 -06002528 if (g->data) {
Jonathan Tanc3756d52018-07-11 15:42:40 -07002529 munmap((void *)g->data, g->data_len);
2530 g->data = NULL;
Jonathan Tanc3756d52018-07-11 15:42:40 -07002531 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002532 free(g->filename);
Garima Singh76ffbca2020-04-06 16:59:49 +00002533 free(g->bloom_filter_settings);
Jonathan Tanc3756d52018-07-11 15:42:40 -07002534 free(g);
2535}
Jeff King6abada12019-09-12 10:44:45 -04002536
2537void disable_commit_graph(struct repository *r)
2538{
2539 r->commit_graph_disabled = 1;
2540}