blob: e4f1a5b2f1aecf5217a9c3606fee4e625ec83862 [file] [log] [blame]
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001#include "cache.h"
2#include "config.h"
Derrick Stolee33286dc2018-05-10 17:42:52 +00003#include "dir.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -04004#include "git-compat-util.h"
5#include "lockfile.h"
6#include "pack.h"
7#include "packfile.h"
8#include "commit.h"
9#include "object.h"
Derrick Stolee59fb8772018-06-27 09:24:45 -040010#include "refs.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040011#include "revision.h"
12#include "sha1-lookup.h"
13#include "commit-graph.h"
Junio C Hamanob10edb22018-05-08 15:59:20 +090014#include "object-store.h"
Derrick Stolee96af91d2018-06-27 09:24:36 -040015#include "alloc.h"
Derrick Stoleed6538242018-08-20 18:24:27 +000016#include "hashmap.h"
17#include "replace-object.h"
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +000018#include "progress.h"
Derrick Stolee08fd81c2018-04-02 16:34:19 -040019
20#define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */
21#define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */
22#define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */
23#define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */
SZEDER Gábor5af74172019-01-19 21:21:13 +010024#define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */
Derrick Stolee118bd572019-06-18 11:14:26 -070025#define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */
Garima Singh3be7efc2020-03-30 00:31:23 +000026#define MAX_NUM_CHUNKS 5
Derrick Stolee08fd81c2018-04-02 16:34:19 -040027
brian m. carlsonc1665992018-11-14 04:09:35 +000028#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040029
30#define GRAPH_VERSION_1 0x1
31#define GRAPH_VERSION GRAPH_VERSION_1
32
SZEDER Gábor5af74172019-01-19 21:21:13 +010033#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
Derrick Stolee08fd81c2018-04-02 16:34:19 -040034#define GRAPH_EDGE_LAST_MASK 0x7fffffff
35#define GRAPH_PARENT_NONE 0x70000000
36
37#define GRAPH_LAST_EDGE 0x80000000
38
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040039#define GRAPH_HEADER_SIZE 8
Derrick Stolee08fd81c2018-04-02 16:34:19 -040040#define GRAPH_FANOUT_SIZE (4 * 256)
41#define GRAPH_CHUNKLOOKUP_WIDTH 12
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040042#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
brian m. carlsonc1665992018-11-14 04:09:35 +000043 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040044
Derrick Stoleecb99a342019-10-24 13:40:42 +000045/* Remember to update object flag allocation in object.h */
46#define REACHABLE (1u<<15)
47
Taylor Blauad2dd5b2020-02-03 13:18:02 -080048char *get_commit_graph_filename(struct object_directory *odb)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040049{
Taylor Blauad2dd5b2020-02-03 13:18:02 -080050 return xstrfmt("%s/info/commit-graph", odb->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -040051}
52
Taylor Blauad2dd5b2020-02-03 13:18:02 -080053static char *get_split_graph_filename(struct object_directory *odb,
Derrick Stolee5c84b332019-06-18 11:14:25 -070054 const char *oid_hex)
55{
Taylor Blauad2dd5b2020-02-03 13:18:02 -080056 return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path,
57 oid_hex);
Derrick Stolee5c84b332019-06-18 11:14:25 -070058}
59
Taylor Blauad2dd5b2020-02-03 13:18:02 -080060static char *get_chain_filename(struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -070061{
Taylor Blauad2dd5b2020-02-03 13:18:02 -080062 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
Derrick Stolee08fd81c2018-04-02 16:34:19 -040063}
64
brian m. carlsonc1665992018-11-14 04:09:35 +000065static uint8_t oid_version(void)
66{
67 return 1;
68}
69
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040070static struct commit_graph *alloc_commit_graph(void)
71{
72 struct commit_graph *g = xcalloc(1, sizeof(*g));
73 g->graph_fd = -1;
74
75 return g;
76}
77
Derrick Stoleed6538242018-08-20 18:24:27 +000078extern int read_replace_refs;
79
80static int commit_graph_compatible(struct repository *r)
81{
Derrick Stolee5cef2952018-08-20 18:24:32 +000082 if (!r->gitdir)
83 return 0;
84
Derrick Stoleed6538242018-08-20 18:24:27 +000085 if (read_replace_refs) {
86 prepare_replace_object(r);
87 if (hashmap_get_size(&r->objects->replace_map->map))
88 return 0;
89 }
90
Derrick Stolee20fd6d52018-08-20 18:24:30 +000091 prepare_commit_graft(r);
92 if (r->parsed_objects && r->parsed_objects->grafts_nr)
93 return 0;
94 if (is_repository_shallow(r))
95 return 0;
96
Derrick Stoleed6538242018-08-20 18:24:27 +000097 return 1;
98}
99
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100100int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
101{
102 *fd = git_open(graph_file);
103 if (*fd < 0)
104 return 0;
105 if (fstat(*fd, st)) {
106 close(*fd);
107 return 0;
108 }
109 return 1;
110}
111
Taylor Blaua7df60c2020-02-03 13:18:04 -0800112struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st,
113 struct object_directory *odb)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400114{
115 void *graph_map;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400116 size_t graph_size;
Josh Steadmonaa658572019-01-15 14:25:50 -0800117 struct commit_graph *ret;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400118
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100119 graph_size = xsize_t(st->st_size);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400120
121 if (graph_size < GRAPH_MIN_SIZE) {
122 close(fd);
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100123 error(_("commit-graph file is too small"));
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100124 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400125 }
126 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
Josh Steadmonaa658572019-01-15 14:25:50 -0800127 ret = parse_commit_graph(graph_map, fd, graph_size);
128
Taylor Blaua7df60c2020-02-03 13:18:04 -0800129 if (ret)
130 ret->odb = odb;
131 else {
Josh Steadmonaa658572019-01-15 14:25:50 -0800132 munmap(graph_map, graph_size);
133 close(fd);
Josh Steadmonaa658572019-01-15 14:25:50 -0800134 }
135
136 return ret;
137}
138
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100139static int verify_commit_graph_lite(struct commit_graph *g)
140{
141 /*
142 * Basic validation shared between parse_commit_graph()
143 * which'll be called every time the graph is used, and the
144 * much more expensive verify_commit_graph() used by
145 * "commit-graph verify".
146 *
147 * There should only be very basic checks here to ensure that
148 * we don't e.g. segfault in fill_commit_in_graph(), but
149 * because this is a very hot codepath nothing that e.g. loops
150 * over g->num_commits, or runs a checksum on the commit-graph
151 * itself.
152 */
153 if (!g->chunk_oid_fanout) {
154 error("commit-graph is missing the OID Fanout chunk");
155 return 1;
156 }
157 if (!g->chunk_oid_lookup) {
158 error("commit-graph is missing the OID Lookup chunk");
159 return 1;
160 }
161 if (!g->chunk_commit_data) {
162 error("commit-graph is missing the Commit Data chunk");
163 return 1;
164 }
165
166 return 0;
167}
168
Josh Steadmonaa658572019-01-15 14:25:50 -0800169struct commit_graph *parse_commit_graph(void *graph_map, int fd,
170 size_t graph_size)
171{
172 const unsigned char *data, *chunk_lookup;
173 uint32_t i;
174 struct commit_graph *graph;
175 uint64_t last_chunk_offset;
176 uint32_t last_chunk_id;
177 uint32_t graph_signature;
178 unsigned char graph_version, hash_version;
179
180 if (!graph_map)
181 return NULL;
182
183 if (graph_size < GRAPH_MIN_SIZE)
184 return NULL;
185
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400186 data = (const unsigned char *)graph_map;
187
188 graph_signature = get_be32(data);
189 if (graph_signature != GRAPH_SIGNATURE) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100190 error(_("commit-graph signature %X does not match signature %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400191 graph_signature, GRAPH_SIGNATURE);
Josh Steadmonaa658572019-01-15 14:25:50 -0800192 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400193 }
194
195 graph_version = *(unsigned char*)(data + 4);
196 if (graph_version != GRAPH_VERSION) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100197 error(_("commit-graph version %X does not match version %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400198 graph_version, GRAPH_VERSION);
Josh Steadmonaa658572019-01-15 14:25:50 -0800199 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400200 }
201
202 hash_version = *(unsigned char*)(data + 5);
brian m. carlsonc1665992018-11-14 04:09:35 +0000203 if (hash_version != oid_version()) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100204 error(_("commit-graph hash version %X does not match version %X"),
brian m. carlsonc1665992018-11-14 04:09:35 +0000205 hash_version, oid_version());
Josh Steadmonaa658572019-01-15 14:25:50 -0800206 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400207 }
208
209 graph = alloc_commit_graph();
210
brian m. carlsonc1665992018-11-14 04:09:35 +0000211 graph->hash_len = the_hash_algo->rawsz;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400212 graph->num_chunks = *(unsigned char*)(data + 6);
213 graph->graph_fd = fd;
214 graph->data = graph_map;
215 graph->data_len = graph_size;
216
217 last_chunk_id = 0;
218 last_chunk_offset = 8;
219 chunk_lookup = data + 8;
220 for (i = 0; i < graph->num_chunks; i++) {
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800221 uint32_t chunk_id;
222 uint64_t chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400223 int chunk_repeated = 0;
224
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800225 if (data + graph_size - chunk_lookup <
226 GRAPH_CHUNKLOOKUP_WIDTH) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100227 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800228 free(graph);
229 return NULL;
230 }
231
232 chunk_id = get_be32(chunk_lookup + 0);
233 chunk_offset = get_be64(chunk_lookup + 4);
234
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400235 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
236
brian m. carlsonc1665992018-11-14 04:09:35 +0000237 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100238 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400239 (uint32_t)chunk_offset);
Josh Steadmonaa658572019-01-15 14:25:50 -0800240 free(graph);
241 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400242 }
243
244 switch (chunk_id) {
245 case GRAPH_CHUNKID_OIDFANOUT:
246 if (graph->chunk_oid_fanout)
247 chunk_repeated = 1;
248 else
249 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
250 break;
251
252 case GRAPH_CHUNKID_OIDLOOKUP:
253 if (graph->chunk_oid_lookup)
254 chunk_repeated = 1;
255 else
256 graph->chunk_oid_lookup = data + chunk_offset;
257 break;
258
259 case GRAPH_CHUNKID_DATA:
260 if (graph->chunk_commit_data)
261 chunk_repeated = 1;
262 else
263 graph->chunk_commit_data = data + chunk_offset;
264 break;
265
SZEDER Gábor5af74172019-01-19 21:21:13 +0100266 case GRAPH_CHUNKID_EXTRAEDGES:
267 if (graph->chunk_extra_edges)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400268 chunk_repeated = 1;
269 else
SZEDER Gábor5af74172019-01-19 21:21:13 +0100270 graph->chunk_extra_edges = data + chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400271 break;
Derrick Stolee118bd572019-06-18 11:14:26 -0700272
273 case GRAPH_CHUNKID_BASE:
274 if (graph->chunk_base_graphs)
275 chunk_repeated = 1;
276 else
277 graph->chunk_base_graphs = data + chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400278 }
279
280 if (chunk_repeated) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100281 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
Josh Steadmonaa658572019-01-15 14:25:50 -0800282 free(graph);
283 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400284 }
285
286 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
287 {
288 graph->num_commits = (chunk_offset - last_chunk_offset)
289 / graph->hash_len;
290 }
291
292 last_chunk_id = chunk_id;
293 last_chunk_offset = chunk_offset;
294 }
295
Derrick Stolee118bd572019-06-18 11:14:26 -0700296 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
297
Josh Steadmon98552f22019-05-06 14:36:58 -0700298 if (verify_commit_graph_lite(graph)) {
299 free(graph);
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100300 return NULL;
Josh Steadmon98552f22019-05-06 14:36:58 -0700301 }
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100302
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400303 return graph;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400304}
305
Taylor Blaua7df60c2020-02-03 13:18:04 -0800306static struct commit_graph *load_commit_graph_one(const char *graph_file,
307 struct object_directory *odb)
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100308{
309
310 struct stat st;
311 int fd;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700312 struct commit_graph *g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100313 int open_ok = open_commit_graph(graph_file, &fd, &st);
314
315 if (!open_ok)
316 return NULL;
317
Taylor Blaua7df60c2020-02-03 13:18:04 -0800318 g = load_commit_graph_one_fd_st(fd, &st, odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700319
320 if (g)
321 g->filename = xstrdup(graph_file);
322
323 return g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100324}
325
Taylor Blau13c24992020-02-03 13:18:00 -0800326static struct commit_graph *load_commit_graph_v1(struct repository *r,
327 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700328{
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800329 char *graph_name = get_commit_graph_filename(odb);
Taylor Blaua7df60c2020-02-03 13:18:04 -0800330 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700331 free(graph_name);
332
333 return g;
334}
335
336static int add_graph_to_chain(struct commit_graph *g,
337 struct commit_graph *chain,
338 struct object_id *oids,
339 int n)
340{
341 struct commit_graph *cur_g = chain;
342
Derrick Stolee118bd572019-06-18 11:14:26 -0700343 if (n && !g->chunk_base_graphs) {
344 warning(_("commit-graph has no base graphs chunk"));
345 return 0;
346 }
347
Derrick Stolee5c84b332019-06-18 11:14:25 -0700348 while (n) {
349 n--;
Derrick Stolee118bd572019-06-18 11:14:26 -0700350
351 if (!cur_g ||
352 !oideq(&oids[n], &cur_g->oid) ||
353 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
354 warning(_("commit-graph chain does not match"));
355 return 0;
356 }
357
Derrick Stolee5c84b332019-06-18 11:14:25 -0700358 cur_g = cur_g->base_graph;
359 }
360
361 g->base_graph = chain;
362
363 if (chain)
364 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
365
366 return 1;
367}
368
Taylor Blau13c24992020-02-03 13:18:00 -0800369static struct commit_graph *load_commit_graph_chain(struct repository *r,
370 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700371{
372 struct commit_graph *graph_chain = NULL;
373 struct strbuf line = STRBUF_INIT;
374 struct stat st;
375 struct object_id *oids;
376 int i = 0, valid = 1, count;
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800377 char *chain_name = get_chain_filename(odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700378 FILE *fp;
379 int stat_res;
380
381 fp = fopen(chain_name, "r");
382 stat_res = stat(chain_name, &st);
383 free(chain_name);
384
385 if (!fp ||
386 stat_res ||
387 st.st_size <= the_hash_algo->hexsz)
388 return NULL;
389
390 count = st.st_size / (the_hash_algo->hexsz + 1);
391 oids = xcalloc(count, sizeof(struct object_id));
392
Derrick Stoleec5230352019-06-18 11:14:30 -0700393 prepare_alt_odb(r);
394
395 for (i = 0; i < count; i++) {
396 struct object_directory *odb;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700397
398 if (strbuf_getline_lf(&line, fp) == EOF)
399 break;
400
401 if (get_oid_hex(line.buf, &oids[i])) {
402 warning(_("invalid commit-graph chain: line '%s' not a hash"),
403 line.buf);
404 valid = 0;
405 break;
406 }
407
Derrick Stoleec5230352019-06-18 11:14:30 -0700408 valid = 0;
409 for (odb = r->objects->odb; odb; odb = odb->next) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -0800410 char *graph_name = get_split_graph_filename(odb, line.buf);
Taylor Blaua7df60c2020-02-03 13:18:04 -0800411 struct commit_graph *g = load_commit_graph_one(graph_name, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700412
Derrick Stoleec5230352019-06-18 11:14:30 -0700413 free(graph_name);
414
415 if (g) {
Derrick Stoleec5230352019-06-18 11:14:30 -0700416 if (add_graph_to_chain(g, graph_chain, oids, i)) {
417 graph_chain = g;
418 valid = 1;
419 }
420
421 break;
422 }
423 }
424
425 if (!valid) {
426 warning(_("unable to find all commit-graph files"));
427 break;
428 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700429 }
430
431 free(oids);
432 fclose(fp);
René Scharfe0aa6bce2019-08-07 13:15:02 +0200433 strbuf_release(&line);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700434
435 return graph_chain;
436}
437
Taylor Blau13c24992020-02-03 13:18:00 -0800438struct commit_graph *read_commit_graph_one(struct repository *r,
439 struct object_directory *odb)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700440{
Taylor Blau13c24992020-02-03 13:18:00 -0800441 struct commit_graph *g = load_commit_graph_v1(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700442
443 if (!g)
Taylor Blau13c24992020-02-03 13:18:00 -0800444 g = load_commit_graph_chain(r, odb);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700445
446 return g;
Jonathan Tan5faf3572018-07-11 15:42:37 -0700447}
448
Taylor Blau13c24992020-02-03 13:18:00 -0800449static void prepare_commit_graph_one(struct repository *r,
450 struct object_directory *odb)
Derrick Stolee177722b2018-04-10 08:56:05 -0400451{
Derrick Stolee177722b2018-04-10 08:56:05 -0400452
453 if (r->objects->commit_graph)
454 return;
455
Taylor Blau13c24992020-02-03 13:18:00 -0800456 r->objects->commit_graph = read_commit_graph_one(r, odb);
Derrick Stolee177722b2018-04-10 08:56:05 -0400457}
Jonathan Tan5faf3572018-07-11 15:42:37 -0700458
459/*
460 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
461 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000462 * On the first invocation, this function attempts to load the commit
Jonathan Tan5faf3572018-07-11 15:42:37 -0700463 * graph if the_repository is configured to have one.
464 */
Jonathan Tandade47c2018-07-11 15:42:42 -0700465static int prepare_commit_graph(struct repository *r)
Derrick Stolee177722b2018-04-10 08:56:05 -0400466{
Jeff King263db402018-11-12 09:48:47 -0500467 struct object_directory *odb;
Derrick Stolee177722b2018-04-10 08:56:05 -0400468
Jeff King6abada12019-09-12 10:44:45 -0400469 /*
470 * This must come before the "already attempted?" check below, because
471 * we want to disable even an already-loaded graph file.
472 */
473 if (r->commit_graph_disabled)
474 return 0;
Ævar Arnfjörð Bjarmason43d35612019-03-25 13:08:33 +0100475
Jonathan Tandade47c2018-07-11 15:42:42 -0700476 if (r->objects->commit_graph_attempted)
477 return !!r->objects->commit_graph;
478 r->objects->commit_graph_attempted = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400479
Jeff Kingfbab5522019-09-12 10:44:34 -0400480 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
481 die("dying as requested by the '%s' variable on commit-graph load!",
482 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
483
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700484 prepare_repo_settings(r);
485
Derrick Stolee859fdc02018-08-29 05:49:04 -0700486 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700487 r->settings.core_commit_graph != 1)
Jonathan Tandade47c2018-07-11 15:42:42 -0700488 /*
489 * This repository is not configured to use commit graphs, so
490 * do not load one. (But report commit_graph_attempted anyway
491 * so that commit graph loading is not attempted again for this
492 * repository.)
493 */
Jonathan Tan5faf3572018-07-11 15:42:37 -0700494 return 0;
495
Derrick Stoleed6538242018-08-20 18:24:27 +0000496 if (!commit_graph_compatible(r))
497 return 0;
498
Jonathan Tandade47c2018-07-11 15:42:42 -0700499 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500500 for (odb = r->objects->odb;
Jeff King263db402018-11-12 09:48:47 -0500501 !r->objects->commit_graph && odb;
502 odb = odb->next)
Taylor Blau13c24992020-02-03 13:18:00 -0800503 prepare_commit_graph_one(r, odb);
Jonathan Tandade47c2018-07-11 15:42:42 -0700504 return !!r->objects->commit_graph;
Derrick Stolee177722b2018-04-10 08:56:05 -0400505}
506
Derrick Stolee6cc01742018-07-20 16:33:30 +0000507int generation_numbers_enabled(struct repository *r)
508{
509 uint32_t first_generation;
510 struct commit_graph *g;
511 if (!prepare_commit_graph(r))
512 return 0;
513
514 g = r->objects->commit_graph;
515
516 if (!g->num_commits)
517 return 0;
518
519 first_generation = get_be32(g->chunk_commit_data +
520 g->hash_len + 8) >> 2;
521
522 return !!first_generation;
523}
524
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700525static void close_commit_graph_one(struct commit_graph *g)
526{
527 if (!g)
528 return;
529
530 close_commit_graph_one(g->base_graph);
531 free_commit_graph(g);
532}
533
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700534void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400535{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700536 close_commit_graph_one(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700537 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400538}
539
540static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
541{
542 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
543 g->chunk_oid_lookup, g->hash_len, pos);
544}
545
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700546static void load_oid_from_graph(struct commit_graph *g,
547 uint32_t pos,
548 struct object_id *oid)
549{
550 uint32_t lex_index;
551
552 while (g && pos < g->num_commits_in_base)
553 g = g->base_graph;
554
555 if (!g)
556 BUG("NULL commit-graph");
557
558 if (pos >= g->num_commits + g->num_commits_in_base)
559 die(_("invalid commit position. commit-graph is likely corrupt"));
560
561 lex_index = pos - g->num_commits_in_base;
562
563 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
564}
565
Stefan Beller4f542b72018-12-14 16:09:39 -0800566static struct commit_list **insert_parent_or_die(struct repository *r,
567 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700568 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400569 struct commit_list **pptr)
570{
571 struct commit *c;
572 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400573
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700574 if (pos >= g->num_commits + g->num_commits_in_base)
575 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400576
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700577 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800578 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400579 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200580 die(_("could not find commit %s"), oid_to_hex(&oid));
Derrick Stolee177722b2018-04-10 08:56:05 -0400581 c->graph_pos = pos;
582 return &commit_list_insert(c, pptr)->next;
583}
584
Derrick Stoleee2838d82018-05-01 12:47:13 +0000585static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
586{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700587 const unsigned char *commit_data;
588 uint32_t lex_index;
589
590 while (pos < g->num_commits_in_base)
591 g = g->base_graph;
592
593 lex_index = pos - g->num_commits_in_base;
594 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000595 item->graph_pos = pos;
596 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
597}
598
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700599static inline void set_commit_tree(struct commit *c, struct tree *t)
600{
601 c->maybe_tree = t;
602}
603
Stefan Beller4f542b72018-12-14 16:09:39 -0800604static int fill_commit_in_graph(struct repository *r,
605 struct commit *item,
606 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400607{
Derrick Stolee177722b2018-04-10 08:56:05 -0400608 uint32_t edge_value;
609 uint32_t *parent_data_ptr;
610 uint64_t date_low, date_high;
611 struct commit_list **pptr;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700612 const unsigned char *commit_data;
613 uint32_t lex_index;
614
615 while (pos < g->num_commits_in_base)
616 g = g->base_graph;
617
618 if (pos >= g->num_commits + g->num_commits_in_base)
619 die(_("invalid commit position. commit-graph is likely corrupt"));
620
621 /*
622 * Store the "full" position, but then use the
623 * "local" position for the rest of the calculation.
624 */
625 item->graph_pos = pos;
626 lex_index = pos - g->num_commits_in_base;
627
628 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
Derrick Stolee177722b2018-04-10 08:56:05 -0400629
630 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400631
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700632 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400633
634 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
635 date_low = get_be32(commit_data + g->hash_len + 12);
636 item->date = (timestamp_t)((date_high << 32) | date_low);
637
Derrick Stolee83073cc2018-04-25 14:37:55 +0000638 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
639
Derrick Stolee177722b2018-04-10 08:56:05 -0400640 pptr = &item->parents;
641
642 edge_value = get_be32(commit_data + g->hash_len);
643 if (edge_value == GRAPH_PARENT_NONE)
644 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800645 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400646
647 edge_value = get_be32(commit_data + g->hash_len + 4);
648 if (edge_value == GRAPH_PARENT_NONE)
649 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100650 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800651 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400652 return 1;
653 }
654
SZEDER Gábor5af74172019-01-19 21:21:13 +0100655 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
Derrick Stolee177722b2018-04-10 08:56:05 -0400656 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
657 do {
658 edge_value = get_be32(parent_data_ptr);
Stefan Beller4f542b72018-12-14 16:09:39 -0800659 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400660 edge_value & GRAPH_EDGE_LAST_MASK,
661 pptr);
662 parent_data_ptr++;
663 } while (!(edge_value & GRAPH_LAST_EDGE));
664
665 return 1;
666}
667
Derrick Stoleee2838d82018-05-01 12:47:13 +0000668static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
669{
670 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
671 *pos = item->graph_pos;
672 return 1;
673 } else {
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700674 struct commit_graph *cur_g = g;
675 uint32_t lex_index;
676
677 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
678 cur_g = cur_g->base_graph;
679
680 if (cur_g) {
681 *pos = lex_index + cur_g->num_commits_in_base;
682 return 1;
683 }
684
685 return 0;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000686 }
687}
688
Stefan Beller4f542b72018-12-14 16:09:39 -0800689static int parse_commit_in_graph_one(struct repository *r,
690 struct commit_graph *g,
691 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -0400692{
Derrick Stoleee2838d82018-05-01 12:47:13 +0000693 uint32_t pos;
694
Derrick Stolee177722b2018-04-10 08:56:05 -0400695 if (item->object.parsed)
696 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -0400697
698 if (find_commit_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -0800699 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -0400700
701 return 0;
702}
703
Jonathan Tandade47c2018-07-11 15:42:42 -0700704int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -0400705{
Jonathan Tandade47c2018-07-11 15:42:42 -0700706 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -0400707 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -0800708 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -0400709}
710
Jonathan Tandade47c2018-07-11 15:42:42 -0700711void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000712{
713 uint32_t pos;
Jonathan Tandade47c2018-07-11 15:42:42 -0700714 if (!prepare_commit_graph(r))
Derrick Stoleee2838d82018-05-01 12:47:13 +0000715 return;
Jonathan Tandade47c2018-07-11 15:42:42 -0700716 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
717 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000718}
719
Stefan Beller4f542b72018-12-14 16:09:39 -0800720static struct tree *load_tree_for_commit(struct repository *r,
721 struct commit_graph *g,
722 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000723{
724 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700725 const unsigned char *commit_data;
726
727 while (c->graph_pos < g->num_commits_in_base)
728 g = g->base_graph;
729
730 commit_data = g->chunk_commit_data +
731 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000732
733 hashcpy(oid.hash, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700734 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000735
736 return c->maybe_tree;
737}
738
Stefan Beller4f542b72018-12-14 16:09:39 -0800739static struct tree *get_commit_tree_in_graph_one(struct repository *r,
740 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400741 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000742{
743 if (c->maybe_tree)
744 return c->maybe_tree;
745 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400746 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000747
Stefan Beller4f542b72018-12-14 16:09:39 -0800748 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400749}
750
Jonathan Tandade47c2018-07-11 15:42:42 -0700751struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400752{
Stefan Beller4f542b72018-12-14 16:09:39 -0800753 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000754}
755
Derrick Stoleec9905be2019-06-12 06:29:40 -0700756struct packed_commit_list {
757 struct commit **list;
758 int nr;
759 int alloc;
760};
761
762struct packed_oid_list {
763 struct object_id *list;
764 int nr;
765 int alloc;
766};
767
768struct write_commit_graph_context {
769 struct repository *r;
Taylor Blau0bd52e22020-02-03 21:51:50 -0800770 struct object_directory *odb;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700771 char *graph_name;
772 struct packed_oid_list oids;
773 struct packed_commit_list commits;
774 int num_extra_edges;
775 unsigned long approx_nr_objects;
776 struct progress *progress;
777 int progress_done;
778 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700779
780 char *base_graph_name;
781 int num_commit_graphs_before;
782 int num_commit_graphs_after;
783 char **commit_graph_filenames_before;
784 char **commit_graph_filenames_after;
785 char **commit_graph_hash_after;
786 uint32_t new_num_commits_in_base;
787 struct commit_graph *new_base_graph;
788
Derrick Stoleec9905be2019-06-12 06:29:40 -0700789 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -0700790 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200791 split:1,
792 check_oids:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700793
794 const struct split_commit_graph_opts *split_opts;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700795};
796
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400797static void write_graph_chunk_fanout(struct hashfile *f,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700798 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400799{
800 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700801 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400802
803 /*
804 * Write the first-level table (the list is sorted,
805 * but we use a 256-entry lookup to be able to avoid
806 * having to do eight extra binary search iterations).
807 */
808 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -0700809 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400810 if ((*list)->object.oid.hash[0] != i)
811 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700812 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400813 count++;
814 list++;
815 }
816
817 hashwrite_be32(f, count);
818 }
819}
820
821static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700822 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400823{
Derrick Stoleec9905be2019-06-12 06:29:40 -0700824 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400825 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700826 for (count = 0; count < ctx->commits.nr; count++, list++) {
827 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400828 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +0100829 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400830}
831
832static const unsigned char *commit_to_sha1(size_t index, void *table)
833{
834 struct commit **commits = table;
835 return commits[index]->object.oid.hash;
836}
837
838static void write_graph_chunk_data(struct hashfile *f, int hash_len,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700839 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400840{
Derrick Stoleec9905be2019-06-12 06:29:40 -0700841 struct commit **list = ctx->commits.list;
842 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400843 uint32_t num_extra_edges = 0;
844
845 while (list < last) {
846 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -0400847 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400848 int edge_value;
849 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -0700850 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400851
Taylor Blau16749b82019-09-05 18:04:55 -0400852 if (parse_commit_no_graph(*list))
853 die(_("unable to parse commit %s"),
854 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -0400855 tree = get_commit_tree_oid(*list);
Taylor Blau806278d2019-09-05 18:04:57 -0400856 hashwrite(f, tree->hash, hash_len);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400857
858 parent = (*list)->parents;
859
860 if (!parent)
861 edge_value = GRAPH_PARENT_NONE;
862 else {
863 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700864 ctx->commits.list,
865 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400866 commit_to_sha1);
867
Derrick Stolee6c622f92019-06-18 11:14:27 -0700868 if (edge_value >= 0)
869 edge_value += ctx->new_num_commits_in_base;
870 else {
871 uint32_t pos;
872 if (find_commit_in_graph(parent->item,
873 ctx->new_base_graph,
874 &pos))
875 edge_value = pos;
876 }
877
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400878 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -0800879 BUG("missing parent %s for commit %s",
880 oid_to_hex(&parent->item->object.oid),
881 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400882 }
883
884 hashwrite_be32(f, edge_value);
885
886 if (parent)
887 parent = parent->next;
888
889 if (!parent)
890 edge_value = GRAPH_PARENT_NONE;
891 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +0100892 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400893 else {
894 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700895 ctx->commits.list,
896 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400897 commit_to_sha1);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700898
899 if (edge_value >= 0)
900 edge_value += ctx->new_num_commits_in_base;
901 else {
902 uint32_t pos;
903 if (find_commit_in_graph(parent->item,
904 ctx->new_base_graph,
905 &pos))
906 edge_value = pos;
907 }
908
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400909 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -0800910 BUG("missing parent %s for commit %s",
911 oid_to_hex(&parent->item->object.oid),
912 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400913 }
914
915 hashwrite_be32(f, edge_value);
916
SZEDER Gábor5af74172019-01-19 21:21:13 +0100917 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400918 do {
919 num_extra_edges++;
920 parent = parent->next;
921 } while (parent);
922 }
923
924 if (sizeof((*list)->date) > 4)
925 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
926 else
927 packedDate[0] = 0;
928
Derrick Stolee3258c662018-05-01 12:47:09 +0000929 packedDate[0] |= htonl((*list)->generation << 2);
930
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400931 packedDate[1] = htonl((*list)->date);
932 hashwrite(f, packedDate, 8);
933
934 list++;
935 }
936}
937
SZEDER Gábor5af74172019-01-19 21:21:13 +0100938static void write_graph_chunk_extra_edges(struct hashfile *f,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700939 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400940{
Derrick Stoleec9905be2019-06-12 06:29:40 -0700941 struct commit **list = ctx->commits.list;
942 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400943 struct commit_list *parent;
944
945 while (list < last) {
946 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +0100947
Derrick Stoleec9905be2019-06-12 06:29:40 -0700948 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +0100949
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400950 for (parent = (*list)->parents; num_parents < 3 && parent;
951 parent = parent->next)
952 num_parents++;
953
954 if (num_parents <= 2) {
955 list++;
956 continue;
957 }
958
959 /* Since num_parents > 2, this initializer is safe. */
960 for (parent = (*list)->parents->next; parent; parent = parent->next) {
961 int edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700962 ctx->commits.list,
963 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400964 commit_to_sha1);
965
Derrick Stolee6c622f92019-06-18 11:14:27 -0700966 if (edge_value >= 0)
967 edge_value += ctx->new_num_commits_in_base;
968 else {
969 uint32_t pos;
970 if (find_commit_in_graph(parent->item,
971 ctx->new_base_graph,
972 &pos))
973 edge_value = pos;
974 }
975
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400976 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -0800977 BUG("missing parent %s for commit %s",
978 oid_to_hex(&parent->item->object.oid),
979 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400980 else if (!parent->next)
981 edge_value |= GRAPH_LAST_EDGE;
982
983 hashwrite_be32(f, edge_value);
984 }
985
986 list++;
987 }
988}
989
Derrick Stolee3cbc6ed2019-06-18 11:14:24 -0700990static int oid_compare(const void *_a, const void *_b)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400991{
992 const struct object_id *a = (const struct object_id *)_a;
993 const struct object_id *b = (const struct object_id *)_b;
994 return oidcmp(a, b);
995}
996
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400997static int add_packed_commits(const struct object_id *oid,
998 struct packed_git *pack,
999 uint32_t pos,
1000 void *data)
1001{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001002 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001003 enum object_type type;
1004 off_t offset = nth_packed_object_offset(pack, pos);
1005 struct object_info oi = OBJECT_INFO_INIT;
1006
Derrick Stoleec9905be2019-06-12 06:29:40 -07001007 if (ctx->progress)
1008 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001009
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001010 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001011 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001012 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001013
1014 if (type != OBJ_COMMIT)
1015 return 0;
1016
Derrick Stoleec9905be2019-06-12 06:29:40 -07001017 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1018 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1019 ctx->oids.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001020
1021 return 0;
1022}
1023
Derrick Stoleec9905be2019-06-12 06:29:40 -07001024static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001025{
1026 struct commit_list *parent;
1027 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001028 if (!(parent->item->object.flags & REACHABLE)) {
Derrick Stoleec9905be2019-06-12 06:29:40 -07001029 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1030 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1031 ctx->oids.nr++;
Derrick Stoleecb99a342019-10-24 13:40:42 +00001032 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001033 }
1034 }
1035}
1036
Derrick Stoleec9905be2019-06-12 06:29:40 -07001037static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001038{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001039 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001040 struct commit *commit;
1041
Derrick Stoleec9905be2019-06-12 06:29:40 -07001042 if (ctx->report_progress)
1043 ctx->progress = start_delayed_progress(
1044 _("Loading known commits in commit graph"),
1045 ctx->oids.nr);
1046 for (i = 0; i < ctx->oids.nr; i++) {
1047 display_progress(ctx->progress, i + 1);
1048 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001049 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001050 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001051 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001052 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001053
1054 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001055 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001056 * than the number of missing commits in the reachable
1057 * closure.
1058 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001059 if (ctx->report_progress)
1060 ctx->progress = start_delayed_progress(
1061 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001062 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001063 for (i = 0; i < ctx->oids.nr; i++) {
1064 display_progress(ctx->progress, i + 1);
1065 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001066
Derrick Stolee6c622f92019-06-18 11:14:27 -07001067 if (!commit)
1068 continue;
1069 if (ctx->split) {
1070 if (!parse_commit(commit) &&
1071 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1072 add_missing_parents(ctx, commit);
1073 } else if (!parse_commit_no_graph(commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001074 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001075 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001076 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001077
Derrick Stoleec9905be2019-06-12 06:29:40 -07001078 if (ctx->report_progress)
1079 ctx->progress = start_delayed_progress(
1080 _("Clearing commit marks in commit graph"),
1081 ctx->oids.nr);
1082 for (i = 0; i < ctx->oids.nr; i++) {
1083 display_progress(ctx->progress, i + 1);
1084 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001085
1086 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001087 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001088 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001089 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001090}
1091
Derrick Stoleec9905be2019-06-12 06:29:40 -07001092static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001093{
1094 int i;
1095 struct commit_list *list = NULL;
1096
Derrick Stoleec9905be2019-06-12 06:29:40 -07001097 if (ctx->report_progress)
Derrick Stoleeecc08692019-11-25 21:28:23 +00001098 ctx->progress = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001099 _("Computing commit graph generation numbers"),
1100 ctx->commits.nr);
1101 for (i = 0; i < ctx->commits.nr; i++) {
1102 display_progress(ctx->progress, i + 1);
1103 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1104 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
Derrick Stolee3258c662018-05-01 12:47:09 +00001105 continue;
1106
Derrick Stoleec9905be2019-06-12 06:29:40 -07001107 commit_list_insert(ctx->commits.list[i], &list);
Derrick Stolee3258c662018-05-01 12:47:09 +00001108 while (list) {
1109 struct commit *current = list->item;
1110 struct commit_list *parent;
1111 int all_parents_computed = 1;
1112 uint32_t max_generation = 0;
1113
1114 for (parent = current->parents; parent; parent = parent->next) {
1115 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1116 parent->item->generation == GENERATION_NUMBER_ZERO) {
1117 all_parents_computed = 0;
1118 commit_list_insert(parent->item, &list);
1119 break;
1120 } else if (parent->item->generation > max_generation) {
1121 max_generation = parent->item->generation;
1122 }
1123 }
1124
1125 if (all_parents_computed) {
1126 current->generation = max_generation + 1;
1127 pop_commit(&list);
1128
1129 if (current->generation > GENERATION_NUMBER_MAX)
1130 current->generation = GENERATION_NUMBER_MAX;
1131 }
1132 }
1133 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001134 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001135}
1136
Derrick Stolee59fb8772018-06-27 09:24:45 -04001137static int add_ref_to_list(const char *refname,
1138 const struct object_id *oid,
1139 int flags, void *cb_data)
1140{
1141 struct string_list *list = (struct string_list *)cb_data;
1142
1143 string_list_append(list, oid_to_hex(oid));
1144 return 0;
1145}
1146
Taylor Blau0bd52e22020-02-03 21:51:50 -08001147int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001148 enum commit_graph_write_flags flags,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001149 const struct split_commit_graph_opts *split_opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001150{
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001151 struct string_list list = STRING_LIST_INIT_DUP;
Derrick Stoleee103f722019-06-12 06:29:37 -07001152 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001153
Derrick Stolee59fb8772018-06-27 09:24:45 -04001154 for_each_ref(add_ref_to_list, &list);
Taylor Blau0bd52e22020-02-03 21:51:50 -08001155 result = write_commit_graph(odb, NULL, &list,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001156 flags, split_opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001157
1158 string_list_clear(&list, 0);
Derrick Stoleee103f722019-06-12 06:29:37 -07001159 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001160}
1161
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001162static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1163 struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001164{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001165 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001166 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001167 struct strbuf packname = STRBUF_INIT;
1168 int dirlen;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001169
Taylor Blau0bd52e22020-02-03 21:51:50 -08001170 strbuf_addf(&packname, "%s/pack/", ctx->odb->path);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001171 dirlen = packname.len;
1172 if (ctx->report_progress) {
1173 strbuf_addf(&progress_title,
1174 Q_("Finding commits for commit graph in %d pack",
1175 "Finding commits for commit graph in %d packs",
1176 pack_indexes->nr),
1177 pack_indexes->nr);
1178 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1179 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001180 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001181 for (i = 0; i < pack_indexes->nr; i++) {
1182 struct packed_git *p;
1183 strbuf_setlen(&packname, dirlen);
1184 strbuf_addstr(&packname, pack_indexes->items[i].string);
1185 p = add_packed_git(packname.buf, packname.len, 1);
1186 if (!p) {
1187 error(_("error adding pack %s"), packname.buf);
1188 return -1;
Derrick Stolee7547b952018-04-10 08:56:08 -04001189 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001190 if (open_pack_index(p)) {
1191 error(_("error opening index for %s"), packname.buf);
1192 return -1;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001193 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001194 for_each_object_in_pack(p, add_packed_commits, ctx,
1195 FOR_EACH_OBJECT_PACK_ORDER);
1196 close_pack(p);
1197 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001198 }
1199
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001200 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001201 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001202 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001203
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001204 return 0;
1205}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001206
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001207static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1208 struct string_list *commit_hex)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001209{
1210 uint32_t i;
1211 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001212
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001213 if (ctx->report_progress) {
1214 strbuf_addf(&progress_title,
1215 Q_("Finding commits for commit graph from %d ref",
1216 "Finding commits for commit graph from %d refs",
1217 commit_hex->nr),
1218 commit_hex->nr);
1219 ctx->progress = start_delayed_progress(
1220 progress_title.buf,
1221 commit_hex->nr);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001222 }
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001223 for (i = 0; i < commit_hex->nr; i++) {
1224 const char *end;
1225 struct object_id oid;
1226 struct commit *result;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001227
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001228 display_progress(ctx->progress, i + 1);
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001229 if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1230 (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001231 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1232 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1233 ctx->oids.nr++;
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001234 } else if (ctx->check_oids) {
1235 error(_("invalid commit object id: %s"),
1236 commit_hex->items[i].string);
1237 return -1;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001238 }
1239 }
1240 stop_progress(&ctx->progress);
1241 strbuf_release(&progress_title);
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001242
1243 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001244}
1245
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001246static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1247{
1248 if (ctx->report_progress)
1249 ctx->progress = start_delayed_progress(
1250 _("Finding commits for commit graph among packed objects"),
1251 ctx->approx_nr_objects);
1252 for_each_packed_object(add_packed_commits, ctx,
1253 FOR_EACH_OBJECT_PACK_ORDER);
1254 if (ctx->progress_done < ctx->approx_nr_objects)
1255 display_progress(ctx->progress, ctx->approx_nr_objects);
1256 stop_progress(&ctx->progress);
1257}
1258
Derrick Stolee014e3442019-06-12 06:29:43 -07001259static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1260{
1261 uint32_t i, count_distinct = 1;
1262
1263 if (ctx->report_progress)
1264 ctx->progress = start_delayed_progress(
1265 _("Counting distinct commits in commit graph"),
1266 ctx->oids.nr);
1267 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
Derrick Stolee3cbc6ed2019-06-18 11:14:24 -07001268 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
Derrick Stolee014e3442019-06-12 06:29:43 -07001269
1270 for (i = 1; i < ctx->oids.nr; i++) {
1271 display_progress(ctx->progress, i + 1);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001272 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1273 if (ctx->split) {
1274 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1275
1276 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1277 continue;
1278 }
1279
Derrick Stolee014e3442019-06-12 06:29:43 -07001280 count_distinct++;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001281 }
Derrick Stolee014e3442019-06-12 06:29:43 -07001282 }
1283 stop_progress(&ctx->progress);
1284
1285 return count_distinct;
1286}
1287
Derrick Stoleef998d542019-06-12 06:29:44 -07001288static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1289{
1290 uint32_t i;
Derrick Stoleef998d542019-06-12 06:29:44 -07001291
1292 ctx->num_extra_edges = 0;
1293 if (ctx->report_progress)
1294 ctx->progress = start_delayed_progress(
1295 _("Finding extra edges in commit graph"),
1296 ctx->oids.nr);
1297 for (i = 0; i < ctx->oids.nr; i++) {
René Scharfe689a1462019-09-15 19:07:44 +02001298 unsigned int num_parents;
1299
Derrick Stoleef998d542019-06-12 06:29:44 -07001300 display_progress(ctx->progress, i + 1);
1301 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1302 continue;
1303
Derrick Stolee6c622f92019-06-18 11:14:27 -07001304 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Derrick Stoleef998d542019-06-12 06:29:44 -07001305 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001306
1307 if (ctx->split &&
1308 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1309 continue;
1310
Derrick Stoleef998d542019-06-12 06:29:44 -07001311 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1312
René Scharfe689a1462019-09-15 19:07:44 +02001313 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001314 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001315 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001316
Derrick Stoleef998d542019-06-12 06:29:44 -07001317 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001318 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001319 stop_progress(&ctx->progress);
1320}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001321
Derrick Stolee6c622f92019-06-18 11:14:27 -07001322static int write_graph_chunk_base_1(struct hashfile *f,
1323 struct commit_graph *g)
1324{
1325 int num = 0;
1326
1327 if (!g)
1328 return 0;
1329
1330 num = write_graph_chunk_base_1(f, g->base_graph);
1331 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1332 return num + 1;
1333}
1334
1335static int write_graph_chunk_base(struct hashfile *f,
1336 struct write_commit_graph_context *ctx)
1337{
1338 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1339
1340 if (num != ctx->num_commit_graphs_after - 1) {
1341 error(_("failed to write correct number of base graph ids"));
1342 return -1;
1343 }
1344
1345 return 0;
1346}
1347
Derrick Stolee238def52019-06-12 06:29:45 -07001348static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1349{
1350 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001351 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07001352 struct hashfile *f;
1353 struct lock_file lk = LOCK_INIT;
Garima Singh3be7efc2020-03-30 00:31:23 +00001354 uint32_t chunk_ids[MAX_NUM_CHUNKS + 1];
1355 uint64_t chunk_offsets[MAX_NUM_CHUNKS + 1];
Derrick Stolee238def52019-06-12 06:29:45 -07001356 const unsigned hashsz = the_hash_algo->rawsz;
1357 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee144354b2019-06-18 11:14:27 -07001358 int num_chunks = 3;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001359 struct object_id file_hash;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001360
Derrick Stolee6c622f92019-06-18 11:14:27 -07001361 if (ctx->split) {
1362 struct strbuf tmp_file = STRBUF_INIT;
1363
1364 strbuf_addf(&tmp_file,
1365 "%s/info/commit-graphs/tmp_graph_XXXXXX",
Taylor Blau0bd52e22020-02-03 21:51:50 -08001366 ctx->odb->path);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001367 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1368 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001369 ctx->graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001370 }
1371
Derrick Stolee238def52019-06-12 06:29:45 -07001372 if (safe_create_leading_directories(ctx->graph_name)) {
1373 UNLEAK(ctx->graph_name);
1374 error(_("unable to create leading directories of %s"),
1375 ctx->graph_name);
1376 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001377 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001378
Derrick Stolee6c622f92019-06-18 11:14:27 -07001379 if (ctx->split) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001380 char *lock_name = get_chain_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001381
1382 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1383
1384 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1385 if (fd < 0) {
1386 error(_("unable to create '%s'"), ctx->graph_name);
1387 return -1;
1388 }
1389
1390 f = hashfd(fd, ctx->graph_name);
1391 } else {
1392 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1393 fd = lk.tempfile->fd;
1394 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1395 }
Derrick Stolee238def52019-06-12 06:29:45 -07001396
Derrick Stolee238def52019-06-12 06:29:45 -07001397 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1398 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1399 chunk_ids[2] = GRAPH_CHUNKID_DATA;
Derrick Stolee144354b2019-06-18 11:14:27 -07001400 if (ctx->num_extra_edges) {
1401 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1402 num_chunks++;
1403 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001404 if (ctx->num_commit_graphs_after > 1) {
1405 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1406 num_chunks++;
1407 }
Derrick Stolee144354b2019-06-18 11:14:27 -07001408
1409 chunk_ids[num_chunks] = 0;
Derrick Stolee238def52019-06-12 06:29:45 -07001410
1411 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1412 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1413 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1414 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
Derrick Stolee144354b2019-06-18 11:14:27 -07001415
1416 num_chunks = 3;
1417 if (ctx->num_extra_edges) {
1418 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1419 4 * ctx->num_extra_edges;
1420 num_chunks++;
1421 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001422 if (ctx->num_commit_graphs_after > 1) {
1423 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1424 hashsz * (ctx->num_commit_graphs_after - 1);
1425 num_chunks++;
1426 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001427
1428 hashwrite_be32(f, GRAPH_SIGNATURE);
1429
1430 hashwrite_u8(f, GRAPH_VERSION);
brian m. carlsonc1665992018-11-14 04:09:35 +00001431 hashwrite_u8(f, oid_version());
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001432 hashwrite_u8(f, num_chunks);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001433 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001434
1435 for (i = 0; i <= num_chunks; i++) {
1436 uint32_t chunk_write[3];
1437
1438 chunk_write[0] = htonl(chunk_ids[i]);
1439 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1440 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1441 hashwrite(f, chunk_write, 12);
1442 }
1443
Derrick Stolee238def52019-06-12 06:29:45 -07001444 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001445 strbuf_addf(&progress_title,
1446 Q_("Writing out commit graph in %d pass",
1447 "Writing out commit graph in %d passes",
1448 num_chunks),
1449 num_chunks);
Derrick Stolee238def52019-06-12 06:29:45 -07001450 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001451 progress_title.buf,
Derrick Stolee238def52019-06-12 06:29:45 -07001452 num_chunks * ctx->commits.nr);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001453 }
Derrick Stolee238def52019-06-12 06:29:45 -07001454 write_graph_chunk_fanout(f, ctx);
1455 write_graph_chunk_oids(f, hashsz, ctx);
1456 write_graph_chunk_data(f, hashsz, ctx);
1457 if (ctx->num_extra_edges)
1458 write_graph_chunk_extra_edges(f, ctx);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001459 if (ctx->num_commit_graphs_after > 1 &&
1460 write_graph_chunk_base(f, ctx)) {
1461 return -1;
1462 }
Derrick Stolee238def52019-06-12 06:29:45 -07001463 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001464 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001465
Derrick Stolee6c622f92019-06-18 11:14:27 -07001466 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1467 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001468 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001469
1470 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1471 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1472 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1473 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1474 }
1475
Derrick Stoleec3a3a962019-05-17 11:41:47 -07001476 close_commit_graph(ctx->r->objects);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001477 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1478
1479 if (ctx->split) {
1480 FILE *chainf = fdopen_lock_file(&lk, "w");
1481 char *final_graph_name;
1482 int result;
1483
1484 close(fd);
1485
1486 if (!chainf) {
1487 error(_("unable to open commit-graph chain file"));
1488 return -1;
1489 }
1490
1491 if (ctx->base_graph_name) {
Derrick Stolee135a7122019-06-18 11:14:28 -07001492 const char *dest = ctx->commit_graph_filenames_after[
1493 ctx->num_commit_graphs_after - 2];
Derrick Stolee6c622f92019-06-18 11:14:27 -07001494
Derrick Stolee135a7122019-06-18 11:14:28 -07001495 if (strcmp(ctx->base_graph_name, dest)) {
1496 result = rename(ctx->base_graph_name, dest);
1497
1498 if (result) {
1499 error(_("failed to rename base commit-graph file"));
1500 return -1;
1501 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001502 }
1503 } else {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001504 char *graph_name = get_commit_graph_filename(ctx->odb);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001505 unlink(graph_name);
1506 }
1507
1508 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001509 final_graph_name = get_split_graph_filename(ctx->odb,
Derrick Stolee6c622f92019-06-18 11:14:27 -07001510 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1511 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1512
1513 result = rename(ctx->graph_name, final_graph_name);
1514
1515 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1516 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1517
1518 if (result) {
1519 error(_("failed to rename temporary commit-graph file"));
1520 return -1;
1521 }
1522 }
1523
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001524 commit_lock_file(&lk);
1525
Derrick Stolee238def52019-06-12 06:29:45 -07001526 return 0;
1527}
1528
Derrick Stolee1771be92019-06-18 11:14:29 -07001529static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1530{
Alex Henrie8da02ce2019-09-30 20:29:34 -06001531 struct commit_graph *g;
1532 uint32_t num_commits;
Derrick Stolee1771be92019-06-18 11:14:29 -07001533 uint32_t i;
1534
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001535 int max_commits = 0;
1536 int size_mult = 2;
1537
1538 if (ctx->split_opts) {
1539 max_commits = ctx->split_opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00001540
1541 if (ctx->split_opts->size_multiple)
1542 size_mult = ctx->split_opts->size_multiple;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001543 }
1544
Derrick Stolee1771be92019-06-18 11:14:29 -07001545 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06001546 num_commits = ctx->commits.nr;
Derrick Stolee1771be92019-06-18 11:14:29 -07001547 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1548
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001549 while (g && (g->num_commits <= size_mult * num_commits ||
1550 (max_commits && num_commits > max_commits))) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001551 if (g->odb != ctx->odb)
Derrick Stoleec5230352019-06-18 11:14:30 -07001552 break;
1553
Derrick Stolee1771be92019-06-18 11:14:29 -07001554 num_commits += g->num_commits;
1555 g = g->base_graph;
1556
1557 ctx->num_commit_graphs_after--;
1558 }
1559
1560 ctx->new_base_graph = g;
1561
Derrick Stoleec5230352019-06-18 11:14:30 -07001562 if (ctx->num_commit_graphs_after == 2) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001563 char *old_graph_name = get_commit_graph_filename(g->odb);
Derrick Stoleec5230352019-06-18 11:14:30 -07001564
1565 if (!strcmp(g->filename, old_graph_name) &&
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001566 g->odb != ctx->odb) {
Derrick Stoleec5230352019-06-18 11:14:30 -07001567 ctx->num_commit_graphs_after = 1;
1568 ctx->new_base_graph = NULL;
1569 }
1570
1571 free(old_graph_name);
1572 }
1573
Derrick Stolee1771be92019-06-18 11:14:29 -07001574 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1575 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1576
1577 for (i = 0; i < ctx->num_commit_graphs_after &&
1578 i < ctx->num_commit_graphs_before; i++)
1579 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1580
1581 i = ctx->num_commit_graphs_before - 1;
1582 g = ctx->r->objects->commit_graph;
1583
1584 while (g) {
1585 if (i < ctx->num_commit_graphs_after)
1586 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1587
1588 i--;
1589 g = g->base_graph;
1590 }
1591}
1592
1593static void merge_commit_graph(struct write_commit_graph_context *ctx,
1594 struct commit_graph *g)
1595{
1596 uint32_t i;
1597 uint32_t offset = g->num_commits_in_base;
1598
1599 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1600
1601 for (i = 0; i < g->num_commits; i++) {
1602 struct object_id oid;
1603 struct commit *result;
1604
1605 display_progress(ctx->progress, i + 1);
1606
1607 load_oid_from_graph(g, i + offset, &oid);
1608
1609 /* only add commits if they still exist in the repo */
1610 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1611
1612 if (result) {
1613 ctx->commits.list[ctx->commits.nr] = result;
1614 ctx->commits.nr++;
1615 }
1616 }
1617}
1618
1619static int commit_compare(const void *_a, const void *_b)
1620{
1621 const struct commit *a = *(const struct commit **)_a;
1622 const struct commit *b = *(const struct commit **)_b;
1623 return oidcmp(&a->object.oid, &b->object.oid);
1624}
1625
1626static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1627{
René Scharfe689a1462019-09-15 19:07:44 +02001628 uint32_t i;
Derrick Stolee1771be92019-06-18 11:14:29 -07001629
1630 if (ctx->report_progress)
1631 ctx->progress = start_delayed_progress(
1632 _("Scanning merged commits"),
1633 ctx->commits.nr);
1634
1635 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1636
1637 ctx->num_extra_edges = 0;
1638 for (i = 0; i < ctx->commits.nr; i++) {
1639 display_progress(ctx->progress, i);
1640
1641 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1642 &ctx->commits.list[i]->object.oid)) {
1643 die(_("unexpected duplicate commit id %s"),
1644 oid_to_hex(&ctx->commits.list[i]->object.oid));
1645 } else {
René Scharfe689a1462019-09-15 19:07:44 +02001646 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07001647
René Scharfe689a1462019-09-15 19:07:44 +02001648 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07001649 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07001650 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07001651 }
1652 }
1653
1654 stop_progress(&ctx->progress);
1655}
1656
1657static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1658{
1659 struct commit_graph *g = ctx->r->objects->commit_graph;
1660 uint32_t current_graph_number = ctx->num_commit_graphs_before;
Derrick Stolee1771be92019-06-18 11:14:29 -07001661
1662 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1663 current_graph_number--;
1664
René Scharfed68ce902020-02-20 19:49:18 +01001665 if (ctx->report_progress)
1666 ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0);
Derrick Stolee1771be92019-06-18 11:14:29 -07001667
1668 merge_commit_graph(ctx, g);
1669 stop_progress(&ctx->progress);
Derrick Stolee1771be92019-06-18 11:14:29 -07001670
1671 g = g->base_graph;
1672 }
1673
1674 if (g) {
1675 ctx->new_base_graph = g;
1676 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1677 }
1678
1679 if (ctx->new_base_graph)
1680 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1681
1682 sort_and_scan_merged_commits(ctx);
1683}
1684
Derrick Stolee8d840972019-06-18 11:14:31 -07001685static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1686{
1687 uint32_t i;
1688 time_t now = time(NULL);
1689
1690 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1691 struct stat st;
1692 struct utimbuf updated_time;
1693
1694 stat(ctx->commit_graph_filenames_before[i], &st);
1695
1696 updated_time.actime = st.st_atime;
1697 updated_time.modtime = now;
1698 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1699 }
1700}
1701
1702static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1703{
1704 struct strbuf path = STRBUF_INIT;
1705 DIR *dir;
1706 struct dirent *de;
1707 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001708 timestamp_t expire_time = time(NULL);
1709
1710 if (ctx->split_opts && ctx->split_opts->expire_time)
1711 expire_time -= ctx->split_opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07001712 if (!ctx->split) {
Taylor Blauad2dd5b2020-02-03 13:18:02 -08001713 char *chain_file_name = get_chain_filename(ctx->odb);
Derrick Stoleeba411122019-06-18 11:14:33 -07001714 unlink(chain_file_name);
1715 free(chain_file_name);
1716 ctx->num_commit_graphs_after = 0;
1717 }
Derrick Stolee8d840972019-06-18 11:14:31 -07001718
Taylor Blau0bd52e22020-02-03 21:51:50 -08001719 strbuf_addstr(&path, ctx->odb->path);
Derrick Stolee8d840972019-06-18 11:14:31 -07001720 strbuf_addstr(&path, "/info/commit-graphs");
1721 dir = opendir(path.buf);
1722
René Scharfe0aa6bce2019-08-07 13:15:02 +02001723 if (!dir)
1724 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07001725
1726 strbuf_addch(&path, '/');
1727 dirnamelen = path.len;
1728 while ((de = readdir(dir)) != NULL) {
1729 struct stat st;
1730 uint32_t i, found = 0;
1731
1732 strbuf_setlen(&path, dirnamelen);
1733 strbuf_addstr(&path, de->d_name);
1734
1735 stat(path.buf, &st);
1736
1737 if (st.st_mtime > expire_time)
1738 continue;
1739 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1740 continue;
1741
1742 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1743 if (!strcmp(ctx->commit_graph_filenames_after[i],
1744 path.buf)) {
1745 found = 1;
1746 break;
1747 }
1748 }
1749
1750 if (!found)
1751 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07001752 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02001753
1754out:
1755 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07001756}
1757
Taylor Blau0bd52e22020-02-03 21:51:50 -08001758int write_commit_graph(struct object_directory *odb,
Derrick Stoleee103f722019-06-12 06:29:37 -07001759 struct string_list *pack_indexes,
1760 struct string_list *commit_hex,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001761 enum commit_graph_write_flags flags,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001762 const struct split_commit_graph_opts *split_opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001763{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001764 struct write_commit_graph_context *ctx;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001765 uint32_t i, count_distinct = 0;
Derrick Stoleee103f722019-06-12 06:29:37 -07001766 int res = 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001767
1768 if (!commit_graph_compatible(the_repository))
Derrick Stoleee103f722019-06-12 06:29:37 -07001769 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001770
Derrick Stoleec9905be2019-06-12 06:29:40 -07001771 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1772 ctx->r = the_repository;
Taylor Blau0bd52e22020-02-03 21:51:50 -08001773 ctx->odb = odb;
SZEDER Gábor39d88312019-08-05 10:02:39 +02001774 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1775 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1776 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001777 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001778 ctx->split_opts = split_opts;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001779
1780 if (ctx->split) {
1781 struct commit_graph *g;
1782 prepare_commit_graph(ctx->r);
1783
1784 g = ctx->r->objects->commit_graph;
1785
1786 while (g) {
1787 ctx->num_commit_graphs_before++;
1788 g = g->base_graph;
1789 }
1790
1791 if (ctx->num_commit_graphs_before) {
1792 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1793 i = ctx->num_commit_graphs_before;
1794 g = ctx->r->objects->commit_graph;
1795
1796 while (g) {
1797 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1798 g = g->base_graph;
1799 }
1800 }
1801 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001802
Derrick Stoleec9905be2019-06-12 06:29:40 -07001803 ctx->approx_nr_objects = approximate_object_count();
1804 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1805
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001806 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1807 ctx->oids.alloc = split_opts->max_commits;
Derrick Stolee1771be92019-06-18 11:14:29 -07001808
Derrick Stoleec9905be2019-06-12 06:29:40 -07001809 if (ctx->append) {
Taylor Blau13c24992020-02-03 13:18:00 -08001810 prepare_commit_graph_one(ctx->r, ctx->odb);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001811 if (ctx->r->objects->commit_graph)
1812 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001813 }
1814
Derrick Stoleec9905be2019-06-12 06:29:40 -07001815 if (ctx->oids.alloc < 1024)
1816 ctx->oids.alloc = 1024;
1817 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001818
Derrick Stoleec9905be2019-06-12 06:29:40 -07001819 if (ctx->append && ctx->r->objects->commit_graph) {
1820 struct commit_graph *g = ctx->r->objects->commit_graph;
1821 for (i = 0; i < g->num_commits; i++) {
1822 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1823 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001824 }
1825 }
1826
1827 if (pack_indexes) {
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001828 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1829 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001830 }
1831
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001832 if (commit_hex) {
1833 if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
1834 goto cleanup;
1835 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001836
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001837 if (!pack_indexes && !commit_hex)
1838 fill_oids_from_all_packs(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001839
Derrick Stoleec9905be2019-06-12 06:29:40 -07001840 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001841
Derrick Stolee014e3442019-06-12 06:29:43 -07001842 count_distinct = count_distinct_commits(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001843
Derrick Stoleee103f722019-06-12 06:29:37 -07001844 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1845 error(_("the commit graph format cannot write %d commits"), count_distinct);
1846 res = -1;
1847 goto cleanup;
1848 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001849
Derrick Stoleec9905be2019-06-12 06:29:40 -07001850 ctx->commits.alloc = count_distinct;
1851 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001852
Derrick Stoleef998d542019-06-12 06:29:44 -07001853 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04001854
Derrick Stoleec9905be2019-06-12 06:29:40 -07001855 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07001856 error(_("too many commits to write graph"));
1857 res = -1;
1858 goto cleanup;
1859 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04001860
Derrick Stolee6c622f92019-06-18 11:14:27 -07001861 if (!ctx->commits.nr)
1862 goto cleanup;
1863
Derrick Stolee1771be92019-06-18 11:14:29 -07001864 if (ctx->split) {
1865 split_graph_merge_strategy(ctx);
1866
1867 merge_commit_graphs(ctx);
1868 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07001869 ctx->num_commit_graphs_after = 1;
1870
Derrick Stoleec9905be2019-06-12 06:29:40 -07001871 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04001872
Derrick Stolee238def52019-06-12 06:29:45 -07001873 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001874
Derrick Stoleeba411122019-06-18 11:14:33 -07001875 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07001876 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07001877
1878 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07001879
Derrick Stoleee103f722019-06-12 06:29:37 -07001880cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07001881 free(ctx->graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001882 free(ctx->commits.list);
1883 free(ctx->oids.list);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001884
1885 if (ctx->commit_graph_filenames_after) {
1886 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1887 free(ctx->commit_graph_filenames_after[i]);
1888 free(ctx->commit_graph_hash_after[i]);
1889 }
1890
1891 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1892 free(ctx->commit_graph_filenames_before[i]);
1893
1894 free(ctx->commit_graph_filenames_after);
1895 free(ctx->commit_graph_filenames_before);
1896 free(ctx->commit_graph_hash_after);
1897 }
1898
Derrick Stoleec9905be2019-06-12 06:29:40 -07001899 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07001900
1901 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001902}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001903
1904#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1905static int verify_commit_graph_error;
1906
Derrick Stolee283e68c2018-06-27 09:24:32 -04001907static void graph_report(const char *fmt, ...)
1908{
1909 va_list ap;
1910
1911 verify_commit_graph_error = 1;
1912 va_start(ap, fmt);
1913 vfprintf(stderr, fmt, ap);
1914 fprintf(stderr, "\n");
1915 va_end(ap);
1916}
1917
1918#define GENERATION_ZERO_EXISTS 1
1919#define GENERATION_NUMBER_EXISTS 2
1920
Derrick Stolee3da4b602019-06-18 11:14:32 -07001921int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
Derrick Stolee283e68c2018-06-27 09:24:32 -04001922{
Derrick Stolee9bda8462018-06-27 09:24:35 -04001923 uint32_t i, cur_fanout_pos = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04001924 struct object_id prev_oid, cur_oid, checksum;
Derrick Stolee1373e542018-06-27 09:24:39 -04001925 int generation_zero = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04001926 struct hashfile *f;
1927 int devnull;
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00001928 struct progress *progress = NULL;
Derrick Stolee3da4b602019-06-18 11:14:32 -07001929 int local_error = 0;
Derrick Stolee9bda8462018-06-27 09:24:35 -04001930
Derrick Stolee283e68c2018-06-27 09:24:32 -04001931 if (!g) {
1932 graph_report("no commit-graph file loaded");
1933 return 1;
1934 }
1935
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +01001936 verify_commit_graph_error = verify_commit_graph_lite(g);
Derrick Stolee9bda8462018-06-27 09:24:35 -04001937 if (verify_commit_graph_error)
1938 return verify_commit_graph_error;
1939
Derrick Stolee41df0e32018-06-27 09:24:42 -04001940 devnull = open("/dev/null", O_WRONLY);
1941 f = hashfd(devnull, NULL);
1942 hashwrite(f, g->data, g->data_len - g->hash_len);
1943 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
Jeff King67947c32018-08-28 17:22:52 -04001944 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04001945 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1946 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1947 }
1948
Derrick Stolee9bda8462018-06-27 09:24:35 -04001949 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04001950 struct commit *graph_commit;
1951
Derrick Stolee9bda8462018-06-27 09:24:35 -04001952 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1953
1954 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001955 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04001956 oid_to_hex(&prev_oid),
1957 oid_to_hex(&cur_oid));
1958
1959 oidcpy(&prev_oid, &cur_oid);
1960
1961 while (cur_oid.hash[0] > cur_fanout_pos) {
1962 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1963
1964 if (i != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001965 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04001966 cur_fanout_pos, fanout_value, i);
1967 cur_fanout_pos++;
1968 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04001969
Junio C Hamano82952962018-07-17 15:46:19 -07001970 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08001971 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001972 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04001973 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04001974 }
1975
1976 while (cur_fanout_pos < 256) {
1977 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1978
1979 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001980 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04001981 cur_fanout_pos, fanout_value, i);
1982
1983 cur_fanout_pos++;
1984 }
1985
Derrick Stolee41df0e32018-06-27 09:24:42 -04001986 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04001987 return verify_commit_graph_error;
1988
Garima Singh73716122019-08-26 09:29:58 -07001989 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
1990 progress = start_progress(_("Verifying commits in commit graph"),
1991 g->num_commits);
1992
Derrick Stolee96af91d2018-06-27 09:24:36 -04001993 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04001994 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04001995 struct commit_list *graph_parents, *odb_parents;
Derrick Stolee1373e542018-06-27 09:24:39 -04001996 uint32_t max_generation = 0;
Derrick Stolee96af91d2018-06-27 09:24:36 -04001997
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00001998 display_progress(progress, i + 1);
Derrick Stolee96af91d2018-06-27 09:24:36 -04001999 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2000
Junio C Hamano82952962018-07-17 15:46:19 -07002001 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002002 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002003 if (parse_commit_internal(odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002004 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002005 oid_to_hex(&cur_oid));
2006 continue;
2007 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002008
Stefan Beller4f542b72018-12-14 16:09:39 -08002009 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002010 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002011 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002012 oid_to_hex(&cur_oid),
2013 oid_to_hex(get_commit_tree_oid(graph_commit)),
2014 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002015
2016 graph_parents = graph_commit->parents;
2017 odb_parents = odb_commit->parents;
2018
2019 while (graph_parents) {
2020 if (odb_parents == NULL) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002021 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002022 oid_to_hex(&cur_oid));
2023 break;
2024 }
2025
Derrick Stolee3da4b602019-06-18 11:14:32 -07002026 /* parse parent in case it is in a base graph */
2027 parse_commit_in_graph_one(r, g, graph_parents->item);
2028
Jeff King9001dc22018-08-28 17:22:48 -04002029 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002030 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002031 oid_to_hex(&cur_oid),
2032 oid_to_hex(&graph_parents->item->object.oid),
2033 oid_to_hex(&odb_parents->item->object.oid));
2034
Derrick Stolee1373e542018-06-27 09:24:39 -04002035 if (graph_parents->item->generation > max_generation)
2036 max_generation = graph_parents->item->generation;
2037
Derrick Stolee53614b12018-06-27 09:24:38 -04002038 graph_parents = graph_parents->next;
2039 odb_parents = odb_parents->next;
2040 }
2041
2042 if (odb_parents != NULL)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002043 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002044 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002045
2046 if (!graph_commit->generation) {
2047 if (generation_zero == GENERATION_NUMBER_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002048 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002049 oid_to_hex(&cur_oid));
2050 generation_zero = GENERATION_ZERO_EXISTS;
2051 } else if (generation_zero == GENERATION_ZERO_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002052 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002053 oid_to_hex(&cur_oid));
2054
2055 if (generation_zero == GENERATION_ZERO_EXISTS)
2056 continue;
2057
2058 /*
2059 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2060 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2061 * extra logic in the following condition.
2062 */
2063 if (max_generation == GENERATION_NUMBER_MAX)
2064 max_generation--;
2065
2066 if (graph_commit->generation != max_generation + 1)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002067 graph_report(_("commit-graph generation for commit %s is %u != %u"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002068 oid_to_hex(&cur_oid),
2069 graph_commit->generation,
2070 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002071
2072 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002073 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002074 oid_to_hex(&cur_oid),
2075 graph_commit->date,
2076 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002077 }
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002078 stop_progress(&progress);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002079
Derrick Stolee3da4b602019-06-18 11:14:32 -07002080 local_error = verify_commit_graph_error;
2081
2082 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2083 local_error |= verify_commit_graph(r, g->base_graph, flags);
2084
2085 return local_error;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002086}
Jonathan Tanc3756d52018-07-11 15:42:40 -07002087
2088void free_commit_graph(struct commit_graph *g)
2089{
2090 if (!g)
2091 return;
2092 if (g->graph_fd >= 0) {
2093 munmap((void *)g->data, g->data_len);
2094 g->data = NULL;
2095 close(g->graph_fd);
2096 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002097 free(g->filename);
Jonathan Tanc3756d52018-07-11 15:42:40 -07002098 free(g);
2099}
Jeff King6abada12019-09-12 10:44:45 -04002100
2101void disable_commit_graph(struct repository *r)
2102{
2103 r->commit_graph_disabled = 1;
2104}