blob: b205e65ed131cb2484ad150e8f4d798a5076edc4 [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" */
Derrick Stolee08fd81c2018-04-02 16:34:19 -040026
brian m. carlsonc1665992018-11-14 04:09:35 +000027#define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040028
29#define GRAPH_VERSION_1 0x1
30#define GRAPH_VERSION GRAPH_VERSION_1
31
SZEDER Gábor5af74172019-01-19 21:21:13 +010032#define GRAPH_EXTRA_EDGES_NEEDED 0x80000000
Derrick Stolee08fd81c2018-04-02 16:34:19 -040033#define GRAPH_EDGE_LAST_MASK 0x7fffffff
34#define GRAPH_PARENT_NONE 0x70000000
35
36#define GRAPH_LAST_EDGE 0x80000000
37
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040038#define GRAPH_HEADER_SIZE 8
Derrick Stolee08fd81c2018-04-02 16:34:19 -040039#define GRAPH_FANOUT_SIZE (4 * 256)
40#define GRAPH_CHUNKLOOKUP_WIDTH 12
Derrick Stolee0e3b97c2018-06-27 09:24:28 -040041#define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \
brian m. carlsonc1665992018-11-14 04:09:35 +000042 + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040043
Derrick Stoleecb99a342019-10-24 13:40:42 +000044/* Remember to update object flag allocation in object.h */
45#define REACHABLE (1u<<15)
46
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040047char *get_commit_graph_filename(const char *obj_dir)
Derrick Stolee08fd81c2018-04-02 16:34:19 -040048{
Derrick Stolee16110c92019-06-18 11:14:36 -070049 char *filename = xstrfmt("%s/info/commit-graph", obj_dir);
50 char *normalized = xmalloc(strlen(filename) + 1);
51 normalize_path_copy(normalized, filename);
52 free(filename);
53 return normalized;
Derrick Stolee08fd81c2018-04-02 16:34:19 -040054}
55
Derrick Stolee5c84b332019-06-18 11:14:25 -070056static char *get_split_graph_filename(const char *obj_dir,
57 const char *oid_hex)
58{
Derrick Stolee16110c92019-06-18 11:14:36 -070059 char *filename = xstrfmt("%s/info/commit-graphs/graph-%s.graph",
60 obj_dir,
61 oid_hex);
62 char *normalized = xmalloc(strlen(filename) + 1);
63 normalize_path_copy(normalized, filename);
64 free(filename);
65 return normalized;
Derrick Stolee5c84b332019-06-18 11:14:25 -070066}
67
68static char *get_chain_filename(const char *obj_dir)
69{
70 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", obj_dir);
Derrick Stolee08fd81c2018-04-02 16:34:19 -040071}
72
brian m. carlsonc1665992018-11-14 04:09:35 +000073static uint8_t oid_version(void)
74{
75 return 1;
76}
77
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040078static struct commit_graph *alloc_commit_graph(void)
79{
80 struct commit_graph *g = xcalloc(1, sizeof(*g));
81 g->graph_fd = -1;
82
83 return g;
84}
85
Derrick Stoleed6538242018-08-20 18:24:27 +000086extern int read_replace_refs;
87
88static int commit_graph_compatible(struct repository *r)
89{
Derrick Stolee5cef2952018-08-20 18:24:32 +000090 if (!r->gitdir)
91 return 0;
92
Derrick Stoleed6538242018-08-20 18:24:27 +000093 if (read_replace_refs) {
94 prepare_replace_object(r);
95 if (hashmap_get_size(&r->objects->replace_map->map))
96 return 0;
97 }
98
Derrick Stolee20fd6d52018-08-20 18:24:30 +000099 prepare_commit_graft(r);
100 if (r->parsed_objects && r->parsed_objects->grafts_nr)
101 return 0;
102 if (is_repository_shallow(r))
103 return 0;
104
Derrick Stoleed6538242018-08-20 18:24:27 +0000105 return 1;
106}
107
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100108int open_commit_graph(const char *graph_file, int *fd, struct stat *st)
109{
110 *fd = git_open(graph_file);
111 if (*fd < 0)
112 return 0;
113 if (fstat(*fd, st)) {
114 close(*fd);
115 return 0;
116 }
117 return 1;
118}
119
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100120struct commit_graph *load_commit_graph_one_fd_st(int fd, struct stat *st)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400121{
122 void *graph_map;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400123 size_t graph_size;
Josh Steadmonaa658572019-01-15 14:25:50 -0800124 struct commit_graph *ret;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400125
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100126 graph_size = xsize_t(st->st_size);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400127
128 if (graph_size < GRAPH_MIN_SIZE) {
129 close(fd);
Ævar Arnfjörð Bjarmason67a530f2019-03-25 13:08:31 +0100130 error(_("commit-graph file is too small"));
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100131 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400132 }
133 graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0);
Josh Steadmonaa658572019-01-15 14:25:50 -0800134 ret = parse_commit_graph(graph_map, fd, graph_size);
135
136 if (!ret) {
137 munmap(graph_map, graph_size);
138 close(fd);
Josh Steadmonaa658572019-01-15 14:25:50 -0800139 }
140
141 return ret;
142}
143
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100144static int verify_commit_graph_lite(struct commit_graph *g)
145{
146 /*
147 * Basic validation shared between parse_commit_graph()
148 * which'll be called every time the graph is used, and the
149 * much more expensive verify_commit_graph() used by
150 * "commit-graph verify".
151 *
152 * There should only be very basic checks here to ensure that
153 * we don't e.g. segfault in fill_commit_in_graph(), but
154 * because this is a very hot codepath nothing that e.g. loops
155 * over g->num_commits, or runs a checksum on the commit-graph
156 * itself.
157 */
158 if (!g->chunk_oid_fanout) {
159 error("commit-graph is missing the OID Fanout chunk");
160 return 1;
161 }
162 if (!g->chunk_oid_lookup) {
163 error("commit-graph is missing the OID Lookup chunk");
164 return 1;
165 }
166 if (!g->chunk_commit_data) {
167 error("commit-graph is missing the Commit Data chunk");
168 return 1;
169 }
170
171 return 0;
172}
173
Josh Steadmonaa658572019-01-15 14:25:50 -0800174struct commit_graph *parse_commit_graph(void *graph_map, int fd,
175 size_t graph_size)
176{
177 const unsigned char *data, *chunk_lookup;
178 uint32_t i;
179 struct commit_graph *graph;
180 uint64_t last_chunk_offset;
181 uint32_t last_chunk_id;
182 uint32_t graph_signature;
183 unsigned char graph_version, hash_version;
184
185 if (!graph_map)
186 return NULL;
187
188 if (graph_size < GRAPH_MIN_SIZE)
189 return NULL;
190
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400191 data = (const unsigned char *)graph_map;
192
193 graph_signature = get_be32(data);
194 if (graph_signature != GRAPH_SIGNATURE) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100195 error(_("commit-graph signature %X does not match signature %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400196 graph_signature, GRAPH_SIGNATURE);
Josh Steadmonaa658572019-01-15 14:25:50 -0800197 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400198 }
199
200 graph_version = *(unsigned char*)(data + 4);
201 if (graph_version != GRAPH_VERSION) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100202 error(_("commit-graph version %X does not match version %X"),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400203 graph_version, GRAPH_VERSION);
Josh Steadmonaa658572019-01-15 14:25:50 -0800204 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400205 }
206
207 hash_version = *(unsigned char*)(data + 5);
brian m. carlsonc1665992018-11-14 04:09:35 +0000208 if (hash_version != oid_version()) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100209 error(_("commit-graph hash version %X does not match version %X"),
brian m. carlsonc1665992018-11-14 04:09:35 +0000210 hash_version, oid_version());
Josh Steadmonaa658572019-01-15 14:25:50 -0800211 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400212 }
213
214 graph = alloc_commit_graph();
215
brian m. carlsonc1665992018-11-14 04:09:35 +0000216 graph->hash_len = the_hash_algo->rawsz;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400217 graph->num_chunks = *(unsigned char*)(data + 6);
218 graph->graph_fd = fd;
219 graph->data = graph_map;
220 graph->data_len = graph_size;
221
222 last_chunk_id = 0;
223 last_chunk_offset = 8;
224 chunk_lookup = data + 8;
225 for (i = 0; i < graph->num_chunks; i++) {
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800226 uint32_t chunk_id;
227 uint64_t chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400228 int chunk_repeated = 0;
229
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800230 if (data + graph_size - chunk_lookup <
231 GRAPH_CHUNKLOOKUP_WIDTH) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100232 error(_("commit-graph chunk lookup table entry missing; file may be incomplete"));
Josh Steadmond2b86fb2019-01-15 14:25:51 -0800233 free(graph);
234 return NULL;
235 }
236
237 chunk_id = get_be32(chunk_lookup + 0);
238 chunk_offset = get_be64(chunk_lookup + 4);
239
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400240 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
241
brian m. carlsonc1665992018-11-14 04:09:35 +0000242 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100243 error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400244 (uint32_t)chunk_offset);
Josh Steadmonaa658572019-01-15 14:25:50 -0800245 free(graph);
246 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400247 }
248
249 switch (chunk_id) {
250 case GRAPH_CHUNKID_OIDFANOUT:
251 if (graph->chunk_oid_fanout)
252 chunk_repeated = 1;
253 else
254 graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset);
255 break;
256
257 case GRAPH_CHUNKID_OIDLOOKUP:
258 if (graph->chunk_oid_lookup)
259 chunk_repeated = 1;
260 else
261 graph->chunk_oid_lookup = data + chunk_offset;
262 break;
263
264 case GRAPH_CHUNKID_DATA:
265 if (graph->chunk_commit_data)
266 chunk_repeated = 1;
267 else
268 graph->chunk_commit_data = data + chunk_offset;
269 break;
270
SZEDER Gábor5af74172019-01-19 21:21:13 +0100271 case GRAPH_CHUNKID_EXTRAEDGES:
272 if (graph->chunk_extra_edges)
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400273 chunk_repeated = 1;
274 else
SZEDER Gábor5af74172019-01-19 21:21:13 +0100275 graph->chunk_extra_edges = data + chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400276 break;
Derrick Stolee118bd572019-06-18 11:14:26 -0700277
278 case GRAPH_CHUNKID_BASE:
279 if (graph->chunk_base_graphs)
280 chunk_repeated = 1;
281 else
282 graph->chunk_base_graphs = data + chunk_offset;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400283 }
284
285 if (chunk_repeated) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +0100286 error(_("commit-graph chunk id %08x appears multiple times"), chunk_id);
Josh Steadmonaa658572019-01-15 14:25:50 -0800287 free(graph);
288 return NULL;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400289 }
290
291 if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
292 {
293 graph->num_commits = (chunk_offset - last_chunk_offset)
294 / graph->hash_len;
295 }
296
297 last_chunk_id = chunk_id;
298 last_chunk_offset = chunk_offset;
299 }
300
Derrick Stolee118bd572019-06-18 11:14:26 -0700301 hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len);
302
Josh Steadmon98552f22019-05-06 14:36:58 -0700303 if (verify_commit_graph_lite(graph)) {
304 free(graph);
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100305 return NULL;
Josh Steadmon98552f22019-05-06 14:36:58 -0700306 }
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +0100307
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400308 return graph;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400309}
310
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100311static struct commit_graph *load_commit_graph_one(const char *graph_file)
312{
313
314 struct stat st;
315 int fd;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700316 struct commit_graph *g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100317 int open_ok = open_commit_graph(graph_file, &fd, &st);
318
319 if (!open_ok)
320 return NULL;
321
Derrick Stolee6c622f92019-06-18 11:14:27 -0700322 g = load_commit_graph_one_fd_st(fd, &st);
323
324 if (g)
325 g->filename = xstrdup(graph_file);
326
327 return g;
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +0100328}
329
Derrick Stolee5c84b332019-06-18 11:14:25 -0700330static struct commit_graph *load_commit_graph_v1(struct repository *r, const char *obj_dir)
331{
332 char *graph_name = get_commit_graph_filename(obj_dir);
333 struct commit_graph *g = load_commit_graph_one(graph_name);
334 free(graph_name);
335
Derrick Stoleec5230352019-06-18 11:14:30 -0700336 if (g)
337 g->obj_dir = obj_dir;
338
Derrick Stolee5c84b332019-06-18 11:14:25 -0700339 return g;
340}
341
342static int add_graph_to_chain(struct commit_graph *g,
343 struct commit_graph *chain,
344 struct object_id *oids,
345 int n)
346{
347 struct commit_graph *cur_g = chain;
348
Derrick Stolee118bd572019-06-18 11:14:26 -0700349 if (n && !g->chunk_base_graphs) {
350 warning(_("commit-graph has no base graphs chunk"));
351 return 0;
352 }
353
Derrick Stolee5c84b332019-06-18 11:14:25 -0700354 while (n) {
355 n--;
Derrick Stolee118bd572019-06-18 11:14:26 -0700356
357 if (!cur_g ||
358 !oideq(&oids[n], &cur_g->oid) ||
359 !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) {
360 warning(_("commit-graph chain does not match"));
361 return 0;
362 }
363
Derrick Stolee5c84b332019-06-18 11:14:25 -0700364 cur_g = cur_g->base_graph;
365 }
366
367 g->base_graph = chain;
368
369 if (chain)
370 g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base;
371
372 return 1;
373}
374
375static struct commit_graph *load_commit_graph_chain(struct repository *r, const char *obj_dir)
376{
377 struct commit_graph *graph_chain = NULL;
378 struct strbuf line = STRBUF_INIT;
379 struct stat st;
380 struct object_id *oids;
381 int i = 0, valid = 1, count;
382 char *chain_name = get_chain_filename(obj_dir);
383 FILE *fp;
384 int stat_res;
385
386 fp = fopen(chain_name, "r");
387 stat_res = stat(chain_name, &st);
388 free(chain_name);
389
390 if (!fp ||
391 stat_res ||
392 st.st_size <= the_hash_algo->hexsz)
393 return NULL;
394
395 count = st.st_size / (the_hash_algo->hexsz + 1);
396 oids = xcalloc(count, sizeof(struct object_id));
397
Derrick Stoleec5230352019-06-18 11:14:30 -0700398 prepare_alt_odb(r);
399
400 for (i = 0; i < count; i++) {
401 struct object_directory *odb;
Derrick Stolee5c84b332019-06-18 11:14:25 -0700402
403 if (strbuf_getline_lf(&line, fp) == EOF)
404 break;
405
406 if (get_oid_hex(line.buf, &oids[i])) {
407 warning(_("invalid commit-graph chain: line '%s' not a hash"),
408 line.buf);
409 valid = 0;
410 break;
411 }
412
Derrick Stoleec5230352019-06-18 11:14:30 -0700413 valid = 0;
414 for (odb = r->objects->odb; odb; odb = odb->next) {
415 char *graph_name = get_split_graph_filename(odb->path, line.buf);
416 struct commit_graph *g = load_commit_graph_one(graph_name);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700417
Derrick Stoleec5230352019-06-18 11:14:30 -0700418 free(graph_name);
419
420 if (g) {
421 g->obj_dir = odb->path;
422
423 if (add_graph_to_chain(g, graph_chain, oids, i)) {
424 graph_chain = g;
425 valid = 1;
426 }
427
428 break;
429 }
430 }
431
432 if (!valid) {
433 warning(_("unable to find all commit-graph files"));
434 break;
435 }
Derrick Stolee5c84b332019-06-18 11:14:25 -0700436 }
437
438 free(oids);
439 fclose(fp);
René Scharfe0aa6bce2019-08-07 13:15:02 +0200440 strbuf_release(&line);
Derrick Stolee5c84b332019-06-18 11:14:25 -0700441
442 return graph_chain;
443}
444
Derrick Stolee3da4b602019-06-18 11:14:32 -0700445struct commit_graph *read_commit_graph_one(struct repository *r, const char *obj_dir)
Derrick Stolee5c84b332019-06-18 11:14:25 -0700446{
447 struct commit_graph *g = load_commit_graph_v1(r, obj_dir);
448
449 if (!g)
450 g = load_commit_graph_chain(r, obj_dir);
451
452 return g;
Jonathan Tan5faf3572018-07-11 15:42:37 -0700453}
454
455static void prepare_commit_graph_one(struct repository *r, const char *obj_dir)
456{
Derrick Stolee177722b2018-04-10 08:56:05 -0400457
458 if (r->objects->commit_graph)
459 return;
460
Derrick Stolee5c84b332019-06-18 11:14:25 -0700461 r->objects->commit_graph = read_commit_graph_one(r, obj_dir);
Derrick Stolee177722b2018-04-10 08:56:05 -0400462}
Jonathan Tan5faf3572018-07-11 15:42:37 -0700463
464/*
465 * Return 1 if commit_graph is non-NULL, and 0 otherwise.
466 *
Elijah Newren15beaaa2019-11-05 17:07:23 +0000467 * On the first invocation, this function attempts to load the commit
Jonathan Tan5faf3572018-07-11 15:42:37 -0700468 * graph if the_repository is configured to have one.
469 */
Jonathan Tandade47c2018-07-11 15:42:42 -0700470static int prepare_commit_graph(struct repository *r)
Derrick Stolee177722b2018-04-10 08:56:05 -0400471{
Jeff King263db402018-11-12 09:48:47 -0500472 struct object_directory *odb;
Derrick Stolee177722b2018-04-10 08:56:05 -0400473
Jeff King6abada12019-09-12 10:44:45 -0400474 /*
475 * This must come before the "already attempted?" check below, because
476 * we want to disable even an already-loaded graph file.
477 */
478 if (r->commit_graph_disabled)
479 return 0;
Ævar Arnfjörð Bjarmason43d35612019-03-25 13:08:33 +0100480
Jonathan Tandade47c2018-07-11 15:42:42 -0700481 if (r->objects->commit_graph_attempted)
482 return !!r->objects->commit_graph;
483 r->objects->commit_graph_attempted = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400484
Jeff Kingfbab5522019-09-12 10:44:34 -0400485 if (git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD, 0))
486 die("dying as requested by the '%s' variable on commit-graph load!",
487 GIT_TEST_COMMIT_GRAPH_DIE_ON_LOAD);
488
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700489 prepare_repo_settings(r);
490
Derrick Stolee859fdc02018-08-29 05:49:04 -0700491 if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
Derrick Stolee7211b9e2019-08-13 11:37:43 -0700492 r->settings.core_commit_graph != 1)
Jonathan Tandade47c2018-07-11 15:42:42 -0700493 /*
494 * This repository is not configured to use commit graphs, so
495 * do not load one. (But report commit_graph_attempted anyway
496 * so that commit graph loading is not attempted again for this
497 * repository.)
498 */
Jonathan Tan5faf3572018-07-11 15:42:37 -0700499 return 0;
500
Derrick Stoleed6538242018-08-20 18:24:27 +0000501 if (!commit_graph_compatible(r))
502 return 0;
503
Jonathan Tandade47c2018-07-11 15:42:42 -0700504 prepare_alt_odb(r);
Jeff Kingf0eaf632018-11-12 09:50:39 -0500505 for (odb = r->objects->odb;
Jeff King263db402018-11-12 09:48:47 -0500506 !r->objects->commit_graph && odb;
507 odb = odb->next)
508 prepare_commit_graph_one(r, odb->path);
Jonathan Tandade47c2018-07-11 15:42:42 -0700509 return !!r->objects->commit_graph;
Derrick Stolee177722b2018-04-10 08:56:05 -0400510}
511
Derrick Stolee6cc01742018-07-20 16:33:30 +0000512int generation_numbers_enabled(struct repository *r)
513{
514 uint32_t first_generation;
515 struct commit_graph *g;
516 if (!prepare_commit_graph(r))
517 return 0;
518
519 g = r->objects->commit_graph;
520
521 if (!g->num_commits)
522 return 0;
523
524 first_generation = get_be32(g->chunk_commit_data +
525 g->hash_len + 8) >> 2;
526
527 return !!first_generation;
528}
529
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700530static void close_commit_graph_one(struct commit_graph *g)
531{
532 if (!g)
533 return;
534
535 close_commit_graph_one(g->base_graph);
536 free_commit_graph(g);
537}
538
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700539void close_commit_graph(struct raw_object_store *o)
Derrick Stolee177722b2018-04-10 08:56:05 -0400540{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700541 close_commit_graph_one(o->commit_graph);
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700542 o->commit_graph = NULL;
Derrick Stolee177722b2018-04-10 08:56:05 -0400543}
544
545static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos)
546{
547 return bsearch_hash(oid->hash, g->chunk_oid_fanout,
548 g->chunk_oid_lookup, g->hash_len, pos);
549}
550
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700551static void load_oid_from_graph(struct commit_graph *g,
552 uint32_t pos,
553 struct object_id *oid)
554{
555 uint32_t lex_index;
556
557 while (g && pos < g->num_commits_in_base)
558 g = g->base_graph;
559
560 if (!g)
561 BUG("NULL commit-graph");
562
563 if (pos >= g->num_commits + g->num_commits_in_base)
564 die(_("invalid commit position. commit-graph is likely corrupt"));
565
566 lex_index = pos - g->num_commits_in_base;
567
568 hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index);
569}
570
Stefan Beller4f542b72018-12-14 16:09:39 -0800571static struct commit_list **insert_parent_or_die(struct repository *r,
572 struct commit_graph *g,
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700573 uint32_t pos,
Derrick Stolee177722b2018-04-10 08:56:05 -0400574 struct commit_list **pptr)
575{
576 struct commit *c;
577 struct object_id oid;
Derrick Stolee96af91d2018-06-27 09:24:36 -0400578
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700579 if (pos >= g->num_commits + g->num_commits_in_base)
580 die("invalid parent position %"PRIu32, pos);
Derrick Stolee53614b12018-06-27 09:24:38 -0400581
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700582 load_oid_from_graph(g, pos, &oid);
Stefan Beller4f542b72018-12-14 16:09:39 -0800583 c = lookup_commit(r, &oid);
Derrick Stolee177722b2018-04-10 08:56:05 -0400584 if (!c)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +0200585 die(_("could not find commit %s"), oid_to_hex(&oid));
Derrick Stolee177722b2018-04-10 08:56:05 -0400586 c->graph_pos = pos;
587 return &commit_list_insert(c, pptr)->next;
588}
589
Derrick Stoleee2838d82018-05-01 12:47:13 +0000590static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos)
591{
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700592 const unsigned char *commit_data;
593 uint32_t lex_index;
594
595 while (pos < g->num_commits_in_base)
596 g = g->base_graph;
597
598 lex_index = pos - g->num_commits_in_base;
599 commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000600 item->graph_pos = pos;
601 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
602}
603
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700604static inline void set_commit_tree(struct commit *c, struct tree *t)
605{
606 c->maybe_tree = t;
607}
608
Stefan Beller4f542b72018-12-14 16:09:39 -0800609static int fill_commit_in_graph(struct repository *r,
610 struct commit *item,
611 struct commit_graph *g, uint32_t pos)
Derrick Stolee177722b2018-04-10 08:56:05 -0400612{
Derrick Stolee177722b2018-04-10 08:56:05 -0400613 uint32_t edge_value;
614 uint32_t *parent_data_ptr;
615 uint64_t date_low, date_high;
616 struct commit_list **pptr;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700617 const unsigned char *commit_data;
618 uint32_t lex_index;
619
620 while (pos < g->num_commits_in_base)
621 g = g->base_graph;
622
623 if (pos >= g->num_commits + g->num_commits_in_base)
624 die(_("invalid commit position. commit-graph is likely corrupt"));
625
626 /*
627 * Store the "full" position, but then use the
628 * "local" position for the rest of the calculation.
629 */
630 item->graph_pos = pos;
631 lex_index = pos - g->num_commits_in_base;
632
633 commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index;
Derrick Stolee177722b2018-04-10 08:56:05 -0400634
635 item->object.parsed = 1;
Derrick Stolee177722b2018-04-10 08:56:05 -0400636
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700637 set_commit_tree(item, NULL);
Derrick Stolee177722b2018-04-10 08:56:05 -0400638
639 date_high = get_be32(commit_data + g->hash_len + 8) & 0x3;
640 date_low = get_be32(commit_data + g->hash_len + 12);
641 item->date = (timestamp_t)((date_high << 32) | date_low);
642
Derrick Stolee83073cc2018-04-25 14:37:55 +0000643 item->generation = get_be32(commit_data + g->hash_len + 8) >> 2;
644
Derrick Stolee177722b2018-04-10 08:56:05 -0400645 pptr = &item->parents;
646
647 edge_value = get_be32(commit_data + g->hash_len);
648 if (edge_value == GRAPH_PARENT_NONE)
649 return 1;
Stefan Beller4f542b72018-12-14 16:09:39 -0800650 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400651
652 edge_value = get_be32(commit_data + g->hash_len + 4);
653 if (edge_value == GRAPH_PARENT_NONE)
654 return 1;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100655 if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) {
Stefan Beller4f542b72018-12-14 16:09:39 -0800656 pptr = insert_parent_or_die(r, g, edge_value, pptr);
Derrick Stolee177722b2018-04-10 08:56:05 -0400657 return 1;
658 }
659
SZEDER Gábor5af74172019-01-19 21:21:13 +0100660 parent_data_ptr = (uint32_t*)(g->chunk_extra_edges +
Derrick Stolee177722b2018-04-10 08:56:05 -0400661 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK));
662 do {
663 edge_value = get_be32(parent_data_ptr);
Stefan Beller4f542b72018-12-14 16:09:39 -0800664 pptr = insert_parent_or_die(r, g,
Derrick Stolee177722b2018-04-10 08:56:05 -0400665 edge_value & GRAPH_EDGE_LAST_MASK,
666 pptr);
667 parent_data_ptr++;
668 } while (!(edge_value & GRAPH_LAST_EDGE));
669
670 return 1;
671}
672
Derrick Stoleee2838d82018-05-01 12:47:13 +0000673static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos)
674{
675 if (item->graph_pos != COMMIT_NOT_FROM_GRAPH) {
676 *pos = item->graph_pos;
677 return 1;
678 } else {
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700679 struct commit_graph *cur_g = g;
680 uint32_t lex_index;
681
682 while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index))
683 cur_g = cur_g->base_graph;
684
685 if (cur_g) {
686 *pos = lex_index + cur_g->num_commits_in_base;
687 return 1;
688 }
689
690 return 0;
Derrick Stoleee2838d82018-05-01 12:47:13 +0000691 }
692}
693
Stefan Beller4f542b72018-12-14 16:09:39 -0800694static int parse_commit_in_graph_one(struct repository *r,
695 struct commit_graph *g,
696 struct commit *item)
Derrick Stolee177722b2018-04-10 08:56:05 -0400697{
Derrick Stoleee2838d82018-05-01 12:47:13 +0000698 uint32_t pos;
699
Derrick Stolee177722b2018-04-10 08:56:05 -0400700 if (item->object.parsed)
701 return 1;
Derrick Stoleeee797052018-06-27 09:24:29 -0400702
703 if (find_commit_in_graph(item, g, &pos))
Stefan Beller4f542b72018-12-14 16:09:39 -0800704 return fill_commit_in_graph(r, item, g, pos);
Derrick Stoleeee797052018-06-27 09:24:29 -0400705
706 return 0;
707}
708
Jonathan Tandade47c2018-07-11 15:42:42 -0700709int parse_commit_in_graph(struct repository *r, struct commit *item)
Derrick Stoleeee797052018-06-27 09:24:29 -0400710{
Jonathan Tandade47c2018-07-11 15:42:42 -0700711 if (!prepare_commit_graph(r))
Derrick Stoleeee797052018-06-27 09:24:29 -0400712 return 0;
Stefan Beller4f542b72018-12-14 16:09:39 -0800713 return parse_commit_in_graph_one(r, r->objects->commit_graph, item);
Derrick Stolee177722b2018-04-10 08:56:05 -0400714}
715
Jonathan Tandade47c2018-07-11 15:42:42 -0700716void load_commit_graph_info(struct repository *r, struct commit *item)
Derrick Stoleee2838d82018-05-01 12:47:13 +0000717{
718 uint32_t pos;
Jonathan Tandade47c2018-07-11 15:42:42 -0700719 if (!prepare_commit_graph(r))
Derrick Stoleee2838d82018-05-01 12:47:13 +0000720 return;
Jonathan Tandade47c2018-07-11 15:42:42 -0700721 if (find_commit_in_graph(item, r->objects->commit_graph, &pos))
722 fill_commit_graph_info(item, r->objects->commit_graph, pos);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000723}
724
Stefan Beller4f542b72018-12-14 16:09:39 -0800725static struct tree *load_tree_for_commit(struct repository *r,
726 struct commit_graph *g,
727 struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000728{
729 struct object_id oid;
Derrick Stoleed4f4d602019-06-18 11:14:24 -0700730 const unsigned char *commit_data;
731
732 while (c->graph_pos < g->num_commits_in_base)
733 g = g->base_graph;
734
735 commit_data = g->chunk_commit_data +
736 GRAPH_DATA_WIDTH * (c->graph_pos - g->num_commits_in_base);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000737
738 hashcpy(oid.hash, commit_data);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700739 set_commit_tree(c, lookup_tree(r, &oid));
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000740
741 return c->maybe_tree;
742}
743
Stefan Beller4f542b72018-12-14 16:09:39 -0800744static struct tree *get_commit_tree_in_graph_one(struct repository *r,
745 struct commit_graph *g,
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400746 const struct commit *c)
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000747{
748 if (c->maybe_tree)
749 return c->maybe_tree;
750 if (c->graph_pos == COMMIT_NOT_FROM_GRAPH)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400751 BUG("get_commit_tree_in_graph_one called from non-commit-graph commit");
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000752
Stefan Beller4f542b72018-12-14 16:09:39 -0800753 return load_tree_for_commit(r, g, (struct commit *)c);
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400754}
755
Jonathan Tandade47c2018-07-11 15:42:42 -0700756struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c)
Derrick Stolee0cbef8f2018-06-27 09:24:31 -0400757{
Stefan Beller4f542b72018-12-14 16:09:39 -0800758 return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000759}
760
Derrick Stoleec9905be2019-06-12 06:29:40 -0700761struct packed_commit_list {
762 struct commit **list;
763 int nr;
764 int alloc;
765};
766
767struct packed_oid_list {
768 struct object_id *list;
769 int nr;
770 int alloc;
771};
772
773struct write_commit_graph_context {
774 struct repository *r;
Derrick Stolee16110c92019-06-18 11:14:36 -0700775 char *obj_dir;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700776 char *graph_name;
777 struct packed_oid_list oids;
778 struct packed_commit_list commits;
779 int num_extra_edges;
780 unsigned long approx_nr_objects;
781 struct progress *progress;
782 int progress_done;
783 uint64_t progress_cnt;
Derrick Stolee6c622f92019-06-18 11:14:27 -0700784
785 char *base_graph_name;
786 int num_commit_graphs_before;
787 int num_commit_graphs_after;
788 char **commit_graph_filenames_before;
789 char **commit_graph_filenames_after;
790 char **commit_graph_hash_after;
791 uint32_t new_num_commits_in_base;
792 struct commit_graph *new_base_graph;
793
Derrick Stoleec9905be2019-06-12 06:29:40 -0700794 unsigned append:1,
Derrick Stolee6c622f92019-06-18 11:14:27 -0700795 report_progress:1,
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200796 split:1,
797 check_oids:1;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700798
799 const struct split_commit_graph_opts *split_opts;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700800};
801
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400802static void write_graph_chunk_fanout(struct hashfile *f,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700803 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400804{
805 int i, count = 0;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700806 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400807
808 /*
809 * Write the first-level table (the list is sorted,
810 * but we use a 256-entry lookup to be able to avoid
811 * having to do eight extra binary search iterations).
812 */
813 for (i = 0; i < 256; i++) {
Derrick Stoleec9905be2019-06-12 06:29:40 -0700814 while (count < ctx->commits.nr) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400815 if ((*list)->object.oid.hash[0] != i)
816 break;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700817 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400818 count++;
819 list++;
820 }
821
822 hashwrite_be32(f, count);
823 }
824}
825
826static void write_graph_chunk_oids(struct hashfile *f, int hash_len,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700827 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400828{
Derrick Stoleec9905be2019-06-12 06:29:40 -0700829 struct commit **list = ctx->commits.list;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400830 int count;
Derrick Stoleec9905be2019-06-12 06:29:40 -0700831 for (count = 0; count < ctx->commits.nr; count++, list++) {
832 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400833 hashwrite(f, (*list)->object.oid.hash, (int)hash_len);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +0100834 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400835}
836
837static const unsigned char *commit_to_sha1(size_t index, void *table)
838{
839 struct commit **commits = table;
840 return commits[index]->object.oid.hash;
841}
842
843static void write_graph_chunk_data(struct hashfile *f, int hash_len,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700844 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400845{
Derrick Stoleec9905be2019-06-12 06:29:40 -0700846 struct commit **list = ctx->commits.list;
847 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400848 uint32_t num_extra_edges = 0;
849
850 while (list < last) {
851 struct commit_list *parent;
Taylor Blau806278d2019-09-05 18:04:57 -0400852 struct object_id *tree;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400853 int edge_value;
854 uint32_t packedDate[2];
Derrick Stoleec9905be2019-06-12 06:29:40 -0700855 display_progress(ctx->progress, ++ctx->progress_cnt);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400856
Taylor Blau16749b82019-09-05 18:04:55 -0400857 if (parse_commit_no_graph(*list))
858 die(_("unable to parse commit %s"),
859 oid_to_hex(&(*list)->object.oid));
Taylor Blau806278d2019-09-05 18:04:57 -0400860 tree = get_commit_tree_oid(*list);
Taylor Blau806278d2019-09-05 18:04:57 -0400861 hashwrite(f, tree->hash, hash_len);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400862
863 parent = (*list)->parents;
864
865 if (!parent)
866 edge_value = GRAPH_PARENT_NONE;
867 else {
868 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700869 ctx->commits.list,
870 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400871 commit_to_sha1);
872
Derrick Stolee6c622f92019-06-18 11:14:27 -0700873 if (edge_value >= 0)
874 edge_value += ctx->new_num_commits_in_base;
875 else {
876 uint32_t pos;
877 if (find_commit_in_graph(parent->item,
878 ctx->new_base_graph,
879 &pos))
880 edge_value = pos;
881 }
882
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400883 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -0800884 BUG("missing parent %s for commit %s",
885 oid_to_hex(&parent->item->object.oid),
886 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400887 }
888
889 hashwrite_be32(f, edge_value);
890
891 if (parent)
892 parent = parent->next;
893
894 if (!parent)
895 edge_value = GRAPH_PARENT_NONE;
896 else if (parent->next)
SZEDER Gábor5af74172019-01-19 21:21:13 +0100897 edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400898 else {
899 edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700900 ctx->commits.list,
901 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400902 commit_to_sha1);
Derrick Stolee6c622f92019-06-18 11:14:27 -0700903
904 if (edge_value >= 0)
905 edge_value += ctx->new_num_commits_in_base;
906 else {
907 uint32_t pos;
908 if (find_commit_in_graph(parent->item,
909 ctx->new_base_graph,
910 &pos))
911 edge_value = pos;
912 }
913
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400914 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -0800915 BUG("missing parent %s for commit %s",
916 oid_to_hex(&parent->item->object.oid),
917 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400918 }
919
920 hashwrite_be32(f, edge_value);
921
SZEDER Gábor5af74172019-01-19 21:21:13 +0100922 if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) {
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400923 do {
924 num_extra_edges++;
925 parent = parent->next;
926 } while (parent);
927 }
928
929 if (sizeof((*list)->date) > 4)
930 packedDate[0] = htonl(((*list)->date >> 32) & 0x3);
931 else
932 packedDate[0] = 0;
933
Derrick Stolee3258c662018-05-01 12:47:09 +0000934 packedDate[0] |= htonl((*list)->generation << 2);
935
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400936 packedDate[1] = htonl((*list)->date);
937 hashwrite(f, packedDate, 8);
938
939 list++;
940 }
941}
942
SZEDER Gábor5af74172019-01-19 21:21:13 +0100943static void write_graph_chunk_extra_edges(struct hashfile *f,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700944 struct write_commit_graph_context *ctx)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400945{
Derrick Stoleec9905be2019-06-12 06:29:40 -0700946 struct commit **list = ctx->commits.list;
947 struct commit **last = ctx->commits.list + ctx->commits.nr;
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400948 struct commit_list *parent;
949
950 while (list < last) {
951 int num_parents = 0;
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +0100952
Derrick Stoleec9905be2019-06-12 06:29:40 -0700953 display_progress(ctx->progress, ++ctx->progress_cnt);
Ævar Arnfjörð Bjarmason53035c42019-01-19 21:21:15 +0100954
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400955 for (parent = (*list)->parents; num_parents < 3 && parent;
956 parent = parent->next)
957 num_parents++;
958
959 if (num_parents <= 2) {
960 list++;
961 continue;
962 }
963
964 /* Since num_parents > 2, this initializer is safe. */
965 for (parent = (*list)->parents->next; parent; parent = parent->next) {
966 int edge_value = sha1_pos(parent->item->object.oid.hash,
Derrick Stoleec9905be2019-06-12 06:29:40 -0700967 ctx->commits.list,
968 ctx->commits.nr,
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400969 commit_to_sha1);
970
Derrick Stolee6c622f92019-06-18 11:14:27 -0700971 if (edge_value >= 0)
972 edge_value += ctx->new_num_commits_in_base;
973 else {
974 uint32_t pos;
975 if (find_commit_in_graph(parent->item,
976 ctx->new_base_graph,
977 &pos))
978 edge_value = pos;
979 }
980
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400981 if (edge_value < 0)
Derrick Stoleecce99cd2018-12-19 12:14:07 -0800982 BUG("missing parent %s for commit %s",
983 oid_to_hex(&parent->item->object.oid),
984 oid_to_hex(&(*list)->object.oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400985 else if (!parent->next)
986 edge_value |= GRAPH_LAST_EDGE;
987
988 hashwrite_be32(f, edge_value);
989 }
990
991 list++;
992 }
993}
994
Derrick Stolee3cbc6ed2019-06-18 11:14:24 -0700995static int oid_compare(const void *_a, const void *_b)
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400996{
997 const struct object_id *a = (const struct object_id *)_a;
998 const struct object_id *b = (const struct object_id *)_b;
999 return oidcmp(a, b);
1000}
1001
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001002static int add_packed_commits(const struct object_id *oid,
1003 struct packed_git *pack,
1004 uint32_t pos,
1005 void *data)
1006{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001007 struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001008 enum object_type type;
1009 off_t offset = nth_packed_object_offset(pack, pos);
1010 struct object_info oi = OBJECT_INFO_INIT;
1011
Derrick Stoleec9905be2019-06-12 06:29:40 -07001012 if (ctx->progress)
1013 display_progress(ctx->progress, ++ctx->progress_done);
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001014
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001015 oi.typep = &type;
Derrick Stoleec9905be2019-06-12 06:29:40 -07001016 if (packed_object_info(ctx->r, pack, offset, &oi) < 0)
Nguyễn Thái Ngọc Duy4f5b5322018-07-21 09:49:26 +02001017 die(_("unable to get type of object %s"), oid_to_hex(oid));
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001018
1019 if (type != OBJ_COMMIT)
1020 return 0;
1021
Derrick Stoleec9905be2019-06-12 06:29:40 -07001022 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1023 oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1024 ctx->oids.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001025
1026 return 0;
1027}
1028
Derrick Stoleec9905be2019-06-12 06:29:40 -07001029static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001030{
1031 struct commit_list *parent;
1032 for (parent = commit->parents; parent; parent = parent->next) {
Derrick Stoleecb99a342019-10-24 13:40:42 +00001033 if (!(parent->item->object.flags & REACHABLE)) {
Derrick Stoleec9905be2019-06-12 06:29:40 -07001034 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1035 oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1036 ctx->oids.nr++;
Derrick Stoleecb99a342019-10-24 13:40:42 +00001037 parent->item->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001038 }
1039 }
1040}
1041
Derrick Stoleec9905be2019-06-12 06:29:40 -07001042static void close_reachable(struct write_commit_graph_context *ctx)
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001043{
Ævar Arnfjörð Bjarmason49bbc572019-01-19 21:21:21 +01001044 int i;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001045 struct commit *commit;
1046
Derrick Stoleec9905be2019-06-12 06:29:40 -07001047 if (ctx->report_progress)
1048 ctx->progress = start_delayed_progress(
1049 _("Loading known commits in commit graph"),
1050 ctx->oids.nr);
1051 for (i = 0; i < ctx->oids.nr; i++) {
1052 display_progress(ctx->progress, i + 1);
1053 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001054 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001055 commit->object.flags |= REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001056 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001057 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001058
1059 /*
Derrick Stoleec9905be2019-06-12 06:29:40 -07001060 * As this loop runs, ctx->oids.nr may grow, but not more
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001061 * than the number of missing commits in the reachable
1062 * closure.
1063 */
Derrick Stoleec9905be2019-06-12 06:29:40 -07001064 if (ctx->report_progress)
1065 ctx->progress = start_delayed_progress(
1066 _("Expanding reachable commits in commit graph"),
SZEDER Gábor67fa6aa2019-09-07 01:01:33 -04001067 0);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001068 for (i = 0; i < ctx->oids.nr; i++) {
1069 display_progress(ctx->progress, i + 1);
1070 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001071
Derrick Stolee6c622f92019-06-18 11:14:27 -07001072 if (!commit)
1073 continue;
1074 if (ctx->split) {
1075 if (!parse_commit(commit) &&
1076 commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
1077 add_missing_parents(ctx, commit);
1078 } else if (!parse_commit_no_graph(commit))
Derrick Stoleec9905be2019-06-12 06:29:40 -07001079 add_missing_parents(ctx, commit);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001080 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001081 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001082
Derrick Stoleec9905be2019-06-12 06:29:40 -07001083 if (ctx->report_progress)
1084 ctx->progress = start_delayed_progress(
1085 _("Clearing commit marks in commit graph"),
1086 ctx->oids.nr);
1087 for (i = 0; i < ctx->oids.nr; i++) {
1088 display_progress(ctx->progress, i + 1);
1089 commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001090
1091 if (commit)
Derrick Stoleecb99a342019-10-24 13:40:42 +00001092 commit->object.flags &= ~REACHABLE;
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001093 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001094 stop_progress(&ctx->progress);
Derrick Stolee4f2542b2018-04-10 08:56:04 -04001095}
1096
Derrick Stoleec9905be2019-06-12 06:29:40 -07001097static void compute_generation_numbers(struct write_commit_graph_context *ctx)
Derrick Stolee3258c662018-05-01 12:47:09 +00001098{
1099 int i;
1100 struct commit_list *list = NULL;
1101
Derrick Stoleec9905be2019-06-12 06:29:40 -07001102 if (ctx->report_progress)
Derrick Stoleeecc08692019-11-25 21:28:23 +00001103 ctx->progress = start_delayed_progress(
Derrick Stoleec9905be2019-06-12 06:29:40 -07001104 _("Computing commit graph generation numbers"),
1105 ctx->commits.nr);
1106 for (i = 0; i < ctx->commits.nr; i++) {
1107 display_progress(ctx->progress, i + 1);
1108 if (ctx->commits.list[i]->generation != GENERATION_NUMBER_INFINITY &&
1109 ctx->commits.list[i]->generation != GENERATION_NUMBER_ZERO)
Derrick Stolee3258c662018-05-01 12:47:09 +00001110 continue;
1111
Derrick Stoleec9905be2019-06-12 06:29:40 -07001112 commit_list_insert(ctx->commits.list[i], &list);
Derrick Stolee3258c662018-05-01 12:47:09 +00001113 while (list) {
1114 struct commit *current = list->item;
1115 struct commit_list *parent;
1116 int all_parents_computed = 1;
1117 uint32_t max_generation = 0;
1118
1119 for (parent = current->parents; parent; parent = parent->next) {
1120 if (parent->item->generation == GENERATION_NUMBER_INFINITY ||
1121 parent->item->generation == GENERATION_NUMBER_ZERO) {
1122 all_parents_computed = 0;
1123 commit_list_insert(parent->item, &list);
1124 break;
1125 } else if (parent->item->generation > max_generation) {
1126 max_generation = parent->item->generation;
1127 }
1128 }
1129
1130 if (all_parents_computed) {
1131 current->generation = max_generation + 1;
1132 pop_commit(&list);
1133
1134 if (current->generation > GENERATION_NUMBER_MAX)
1135 current->generation = GENERATION_NUMBER_MAX;
1136 }
1137 }
1138 }
Derrick Stoleec9905be2019-06-12 06:29:40 -07001139 stop_progress(&ctx->progress);
Derrick Stolee3258c662018-05-01 12:47:09 +00001140}
1141
Derrick Stolee59fb8772018-06-27 09:24:45 -04001142static int add_ref_to_list(const char *refname,
1143 const struct object_id *oid,
1144 int flags, void *cb_data)
1145{
1146 struct string_list *list = (struct string_list *)cb_data;
1147
1148 string_list_append(list, oid_to_hex(oid));
1149 return 0;
1150}
1151
SZEDER Gábor39d88312019-08-05 10:02:39 +02001152int write_commit_graph_reachable(const char *obj_dir,
1153 enum commit_graph_write_flags flags,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001154 const struct split_commit_graph_opts *split_opts)
Derrick Stolee59fb8772018-06-27 09:24:45 -04001155{
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001156 struct string_list list = STRING_LIST_INIT_DUP;
Derrick Stoleee103f722019-06-12 06:29:37 -07001157 int result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001158
Derrick Stolee59fb8772018-06-27 09:24:45 -04001159 for_each_ref(add_ref_to_list, &list);
Derrick Stoleee103f722019-06-12 06:29:37 -07001160 result = write_commit_graph(obj_dir, NULL, &list,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001161 flags, split_opts);
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001162
1163 string_list_clear(&list, 0);
Derrick Stoleee103f722019-06-12 06:29:37 -07001164 return result;
Derrick Stolee59fb8772018-06-27 09:24:45 -04001165}
1166
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001167static int fill_oids_from_packs(struct write_commit_graph_context *ctx,
1168 struct string_list *pack_indexes)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001169{
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001170 uint32_t i;
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001171 struct strbuf progress_title = STRBUF_INIT;
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001172 struct strbuf packname = STRBUF_INIT;
1173 int dirlen;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001174
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001175 strbuf_addf(&packname, "%s/pack/", ctx->obj_dir);
1176 dirlen = packname.len;
1177 if (ctx->report_progress) {
1178 strbuf_addf(&progress_title,
1179 Q_("Finding commits for commit graph in %d pack",
1180 "Finding commits for commit graph in %d packs",
1181 pack_indexes->nr),
1182 pack_indexes->nr);
1183 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1184 ctx->progress_done = 0;
Derrick Stolee7547b952018-04-10 08:56:08 -04001185 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001186 for (i = 0; i < pack_indexes->nr; i++) {
1187 struct packed_git *p;
1188 strbuf_setlen(&packname, dirlen);
1189 strbuf_addstr(&packname, pack_indexes->items[i].string);
1190 p = add_packed_git(packname.buf, packname.len, 1);
1191 if (!p) {
1192 error(_("error adding pack %s"), packname.buf);
1193 return -1;
Derrick Stolee7547b952018-04-10 08:56:08 -04001194 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001195 if (open_pack_index(p)) {
1196 error(_("error opening index for %s"), packname.buf);
1197 return -1;
Ævar Arnfjörð Bjarmason7b0f2292018-09-17 15:33:35 +00001198 }
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001199 for_each_object_in_pack(p, add_packed_commits, ctx,
1200 FOR_EACH_OBJECT_PACK_ORDER);
1201 close_pack(p);
1202 free(p);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001203 }
1204
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001205 stop_progress(&ctx->progress);
René Scharfe0aa6bce2019-08-07 13:15:02 +02001206 strbuf_release(&progress_title);
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001207 strbuf_release(&packname);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001208
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001209 return 0;
1210}
Derrick Stolee3d5df012018-04-10 08:56:07 -04001211
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001212static int fill_oids_from_commit_hex(struct write_commit_graph_context *ctx,
1213 struct string_list *commit_hex)
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001214{
1215 uint32_t i;
1216 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001217
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001218 if (ctx->report_progress) {
1219 strbuf_addf(&progress_title,
1220 Q_("Finding commits for commit graph from %d ref",
1221 "Finding commits for commit graph from %d refs",
1222 commit_hex->nr),
1223 commit_hex->nr);
1224 ctx->progress = start_delayed_progress(
1225 progress_title.buf,
1226 commit_hex->nr);
Derrick Stolee3d5df012018-04-10 08:56:07 -04001227 }
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001228 for (i = 0; i < commit_hex->nr; i++) {
1229 const char *end;
1230 struct object_id oid;
1231 struct commit *result;
Derrick Stolee3d5df012018-04-10 08:56:07 -04001232
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001233 display_progress(ctx->progress, i + 1);
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001234 if (!parse_oid_hex(commit_hex->items[i].string, &oid, &end) &&
1235 (result = lookup_commit_reference_gently(ctx->r, &oid, 1))) {
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001236 ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1237 oidcpy(&ctx->oids.list[ctx->oids.nr], &(result->object.oid));
1238 ctx->oids.nr++;
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001239 } else if (ctx->check_oids) {
1240 error(_("invalid commit object id: %s"),
1241 commit_hex->items[i].string);
1242 return -1;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001243 }
1244 }
1245 stop_progress(&ctx->progress);
1246 strbuf_release(&progress_title);
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001247
1248 return 0;
Derrick Stolee4c9efe82019-06-12 06:29:42 -07001249}
1250
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001251static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
1252{
1253 if (ctx->report_progress)
1254 ctx->progress = start_delayed_progress(
1255 _("Finding commits for commit graph among packed objects"),
1256 ctx->approx_nr_objects);
1257 for_each_packed_object(add_packed_commits, ctx,
1258 FOR_EACH_OBJECT_PACK_ORDER);
1259 if (ctx->progress_done < ctx->approx_nr_objects)
1260 display_progress(ctx->progress, ctx->approx_nr_objects);
1261 stop_progress(&ctx->progress);
1262}
1263
Derrick Stolee014e3442019-06-12 06:29:43 -07001264static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1265{
1266 uint32_t i, count_distinct = 1;
1267
1268 if (ctx->report_progress)
1269 ctx->progress = start_delayed_progress(
1270 _("Counting distinct commits in commit graph"),
1271 ctx->oids.nr);
1272 display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
Derrick Stolee3cbc6ed2019-06-18 11:14:24 -07001273 QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
Derrick Stolee014e3442019-06-12 06:29:43 -07001274
1275 for (i = 1; i < ctx->oids.nr; i++) {
1276 display_progress(ctx->progress, i + 1);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001277 if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1278 if (ctx->split) {
1279 struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1280
1281 if (!c || c->graph_pos != COMMIT_NOT_FROM_GRAPH)
1282 continue;
1283 }
1284
Derrick Stolee014e3442019-06-12 06:29:43 -07001285 count_distinct++;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001286 }
Derrick Stolee014e3442019-06-12 06:29:43 -07001287 }
1288 stop_progress(&ctx->progress);
1289
1290 return count_distinct;
1291}
1292
Derrick Stoleef998d542019-06-12 06:29:44 -07001293static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
1294{
1295 uint32_t i;
Derrick Stoleef998d542019-06-12 06:29:44 -07001296
1297 ctx->num_extra_edges = 0;
1298 if (ctx->report_progress)
1299 ctx->progress = start_delayed_progress(
1300 _("Finding extra edges in commit graph"),
1301 ctx->oids.nr);
1302 for (i = 0; i < ctx->oids.nr; i++) {
René Scharfe689a1462019-09-15 19:07:44 +02001303 unsigned int num_parents;
1304
Derrick Stoleef998d542019-06-12 06:29:44 -07001305 display_progress(ctx->progress, i + 1);
1306 if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1307 continue;
1308
Derrick Stolee6c622f92019-06-18 11:14:27 -07001309 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
Derrick Stoleef998d542019-06-12 06:29:44 -07001310 ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001311
1312 if (ctx->split &&
1313 ctx->commits.list[ctx->commits.nr]->graph_pos != COMMIT_NOT_FROM_GRAPH)
1314 continue;
1315
Derrick Stoleef998d542019-06-12 06:29:44 -07001316 parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]);
1317
René Scharfe689a1462019-09-15 19:07:44 +02001318 num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001319 if (num_parents > 2)
Derrick Stoleef998d542019-06-12 06:29:44 -07001320 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001321
Derrick Stoleef998d542019-06-12 06:29:44 -07001322 ctx->commits.nr++;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001323 }
Derrick Stoleef998d542019-06-12 06:29:44 -07001324 stop_progress(&ctx->progress);
1325}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001326
Derrick Stolee6c622f92019-06-18 11:14:27 -07001327static int write_graph_chunk_base_1(struct hashfile *f,
1328 struct commit_graph *g)
1329{
1330 int num = 0;
1331
1332 if (!g)
1333 return 0;
1334
1335 num = write_graph_chunk_base_1(f, g->base_graph);
1336 hashwrite(f, g->oid.hash, the_hash_algo->rawsz);
1337 return num + 1;
1338}
1339
1340static int write_graph_chunk_base(struct hashfile *f,
1341 struct write_commit_graph_context *ctx)
1342{
1343 int num = write_graph_chunk_base_1(f, ctx->new_base_graph);
1344
1345 if (num != ctx->num_commit_graphs_after - 1) {
1346 error(_("failed to write correct number of base graph ids"));
1347 return -1;
1348 }
1349
1350 return 0;
1351}
1352
Derrick Stolee238def52019-06-12 06:29:45 -07001353static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1354{
1355 uint32_t i;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001356 int fd;
Derrick Stolee238def52019-06-12 06:29:45 -07001357 struct hashfile *f;
1358 struct lock_file lk = LOCK_INIT;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001359 uint32_t chunk_ids[6];
1360 uint64_t chunk_offsets[6];
Derrick Stolee238def52019-06-12 06:29:45 -07001361 const unsigned hashsz = the_hash_algo->rawsz;
1362 struct strbuf progress_title = STRBUF_INIT;
Derrick Stolee144354b2019-06-18 11:14:27 -07001363 int num_chunks = 3;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001364 struct object_id file_hash;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001365
Derrick Stolee6c622f92019-06-18 11:14:27 -07001366 if (ctx->split) {
1367 struct strbuf tmp_file = STRBUF_INIT;
1368
1369 strbuf_addf(&tmp_file,
1370 "%s/info/commit-graphs/tmp_graph_XXXXXX",
1371 ctx->obj_dir);
1372 ctx->graph_name = strbuf_detach(&tmp_file, NULL);
1373 } else {
1374 ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
1375 }
1376
Derrick Stolee238def52019-06-12 06:29:45 -07001377 if (safe_create_leading_directories(ctx->graph_name)) {
1378 UNLEAK(ctx->graph_name);
1379 error(_("unable to create leading directories of %s"),
1380 ctx->graph_name);
1381 return -1;
Derrick Stoleef4dbdfc2018-10-03 10:12:15 -07001382 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001383
Derrick Stolee6c622f92019-06-18 11:14:27 -07001384 if (ctx->split) {
1385 char *lock_name = get_chain_filename(ctx->obj_dir);
1386
1387 hold_lock_file_for_update(&lk, lock_name, LOCK_DIE_ON_ERROR);
1388
1389 fd = git_mkstemp_mode(ctx->graph_name, 0444);
1390 if (fd < 0) {
1391 error(_("unable to create '%s'"), ctx->graph_name);
1392 return -1;
1393 }
1394
1395 f = hashfd(fd, ctx->graph_name);
1396 } else {
1397 hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
1398 fd = lk.tempfile->fd;
1399 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
1400 }
Derrick Stolee238def52019-06-12 06:29:45 -07001401
Derrick Stolee238def52019-06-12 06:29:45 -07001402 chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
1403 chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
1404 chunk_ids[2] = GRAPH_CHUNKID_DATA;
Derrick Stolee144354b2019-06-18 11:14:27 -07001405 if (ctx->num_extra_edges) {
1406 chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1407 num_chunks++;
1408 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001409 if (ctx->num_commit_graphs_after > 1) {
1410 chunk_ids[num_chunks] = GRAPH_CHUNKID_BASE;
1411 num_chunks++;
1412 }
Derrick Stolee144354b2019-06-18 11:14:27 -07001413
1414 chunk_ids[num_chunks] = 0;
Derrick Stolee238def52019-06-12 06:29:45 -07001415
1416 chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
1417 chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
1418 chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
1419 chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
Derrick Stolee144354b2019-06-18 11:14:27 -07001420
1421 num_chunks = 3;
1422 if (ctx->num_extra_edges) {
1423 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1424 4 * ctx->num_extra_edges;
1425 num_chunks++;
1426 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001427 if (ctx->num_commit_graphs_after > 1) {
1428 chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1429 hashsz * (ctx->num_commit_graphs_after - 1);
1430 num_chunks++;
1431 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001432
1433 hashwrite_be32(f, GRAPH_SIGNATURE);
1434
1435 hashwrite_u8(f, GRAPH_VERSION);
brian m. carlsonc1665992018-11-14 04:09:35 +00001436 hashwrite_u8(f, oid_version());
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001437 hashwrite_u8(f, num_chunks);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001438 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001439
1440 for (i = 0; i <= num_chunks; i++) {
1441 uint32_t chunk_write[3];
1442
1443 chunk_write[0] = htonl(chunk_ids[i]);
1444 chunk_write[1] = htonl(chunk_offsets[i] >> 32);
1445 chunk_write[2] = htonl(chunk_offsets[i] & 0xffffffff);
1446 hashwrite(f, chunk_write, 12);
1447 }
1448
Derrick Stolee238def52019-06-12 06:29:45 -07001449 if (ctx->report_progress) {
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001450 strbuf_addf(&progress_title,
1451 Q_("Writing out commit graph in %d pass",
1452 "Writing out commit graph in %d passes",
1453 num_chunks),
1454 num_chunks);
Derrick Stolee238def52019-06-12 06:29:45 -07001455 ctx->progress = start_delayed_progress(
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001456 progress_title.buf,
Derrick Stolee238def52019-06-12 06:29:45 -07001457 num_chunks * ctx->commits.nr);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001458 }
Derrick Stolee238def52019-06-12 06:29:45 -07001459 write_graph_chunk_fanout(f, ctx);
1460 write_graph_chunk_oids(f, hashsz, ctx);
1461 write_graph_chunk_data(f, hashsz, ctx);
1462 if (ctx->num_extra_edges)
1463 write_graph_chunk_extra_edges(f, ctx);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001464 if (ctx->num_commit_graphs_after > 1 &&
1465 write_graph_chunk_base(f, ctx)) {
1466 return -1;
1467 }
Derrick Stolee238def52019-06-12 06:29:45 -07001468 stop_progress(&ctx->progress);
Ævar Arnfjörð Bjarmason28944732019-01-19 21:21:16 +01001469 strbuf_release(&progress_title);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001470
Derrick Stolee6c622f92019-06-18 11:14:27 -07001471 if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) {
1472 char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid));
Derrick Stoleec5230352019-06-18 11:14:30 -07001473 char *new_base_name = get_split_graph_filename(ctx->new_base_graph->obj_dir, new_base_hash);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001474
1475 free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]);
1476 free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]);
1477 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name;
1478 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash;
1479 }
1480
Derrick Stoleec3a3a962019-05-17 11:41:47 -07001481 close_commit_graph(ctx->r->objects);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001482 finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC);
1483
1484 if (ctx->split) {
1485 FILE *chainf = fdopen_lock_file(&lk, "w");
1486 char *final_graph_name;
1487 int result;
1488
1489 close(fd);
1490
1491 if (!chainf) {
1492 error(_("unable to open commit-graph chain file"));
1493 return -1;
1494 }
1495
1496 if (ctx->base_graph_name) {
Derrick Stolee135a7122019-06-18 11:14:28 -07001497 const char *dest = ctx->commit_graph_filenames_after[
1498 ctx->num_commit_graphs_after - 2];
Derrick Stolee6c622f92019-06-18 11:14:27 -07001499
Derrick Stolee135a7122019-06-18 11:14:28 -07001500 if (strcmp(ctx->base_graph_name, dest)) {
1501 result = rename(ctx->base_graph_name, dest);
1502
1503 if (result) {
1504 error(_("failed to rename base commit-graph file"));
1505 return -1;
1506 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07001507 }
1508 } else {
1509 char *graph_name = get_commit_graph_filename(ctx->obj_dir);
1510 unlink(graph_name);
1511 }
1512
1513 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash));
1514 final_graph_name = get_split_graph_filename(ctx->obj_dir,
1515 ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]);
1516 ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
1517
1518 result = rename(ctx->graph_name, final_graph_name);
1519
1520 for (i = 0; i < ctx->num_commit_graphs_after; i++)
1521 fprintf(lk.tempfile->fp, "%s\n", ctx->commit_graph_hash_after[i]);
1522
1523 if (result) {
1524 error(_("failed to rename temporary commit-graph file"));
1525 return -1;
1526 }
1527 }
1528
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001529 commit_lock_file(&lk);
1530
Derrick Stolee238def52019-06-12 06:29:45 -07001531 return 0;
1532}
1533
Derrick Stolee1771be92019-06-18 11:14:29 -07001534static void split_graph_merge_strategy(struct write_commit_graph_context *ctx)
1535{
Alex Henrie8da02ce2019-09-30 20:29:34 -06001536 struct commit_graph *g;
1537 uint32_t num_commits;
Derrick Stolee1771be92019-06-18 11:14:29 -07001538 uint32_t i;
1539
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001540 int max_commits = 0;
1541 int size_mult = 2;
1542
1543 if (ctx->split_opts) {
1544 max_commits = ctx->split_opts->max_commits;
Derrick Stolee63020f12020-01-02 16:14:14 +00001545
1546 if (ctx->split_opts->size_multiple)
1547 size_mult = ctx->split_opts->size_multiple;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001548 }
1549
Derrick Stolee1771be92019-06-18 11:14:29 -07001550 g = ctx->r->objects->commit_graph;
Alex Henrie8da02ce2019-09-30 20:29:34 -06001551 num_commits = ctx->commits.nr;
Derrick Stolee1771be92019-06-18 11:14:29 -07001552 ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1;
1553
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001554 while (g && (g->num_commits <= size_mult * num_commits ||
1555 (max_commits && num_commits > max_commits))) {
Derrick Stoleec5230352019-06-18 11:14:30 -07001556 if (strcmp(g->obj_dir, ctx->obj_dir))
1557 break;
1558
Derrick Stolee1771be92019-06-18 11:14:29 -07001559 num_commits += g->num_commits;
1560 g = g->base_graph;
1561
1562 ctx->num_commit_graphs_after--;
1563 }
1564
1565 ctx->new_base_graph = g;
1566
Derrick Stoleec5230352019-06-18 11:14:30 -07001567 if (ctx->num_commit_graphs_after == 2) {
1568 char *old_graph_name = get_commit_graph_filename(g->obj_dir);
1569
1570 if (!strcmp(g->filename, old_graph_name) &&
1571 strcmp(g->obj_dir, ctx->obj_dir)) {
1572 ctx->num_commit_graphs_after = 1;
1573 ctx->new_base_graph = NULL;
1574 }
1575
1576 free(old_graph_name);
1577 }
1578
Derrick Stolee1771be92019-06-18 11:14:29 -07001579 ALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after);
1580 ALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after);
1581
1582 for (i = 0; i < ctx->num_commit_graphs_after &&
1583 i < ctx->num_commit_graphs_before; i++)
1584 ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]);
1585
1586 i = ctx->num_commit_graphs_before - 1;
1587 g = ctx->r->objects->commit_graph;
1588
1589 while (g) {
1590 if (i < ctx->num_commit_graphs_after)
1591 ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid));
1592
1593 i--;
1594 g = g->base_graph;
1595 }
1596}
1597
1598static void merge_commit_graph(struct write_commit_graph_context *ctx,
1599 struct commit_graph *g)
1600{
1601 uint32_t i;
1602 uint32_t offset = g->num_commits_in_base;
1603
1604 ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc);
1605
1606 for (i = 0; i < g->num_commits; i++) {
1607 struct object_id oid;
1608 struct commit *result;
1609
1610 display_progress(ctx->progress, i + 1);
1611
1612 load_oid_from_graph(g, i + offset, &oid);
1613
1614 /* only add commits if they still exist in the repo */
1615 result = lookup_commit_reference_gently(ctx->r, &oid, 1);
1616
1617 if (result) {
1618 ctx->commits.list[ctx->commits.nr] = result;
1619 ctx->commits.nr++;
1620 }
1621 }
1622}
1623
1624static int commit_compare(const void *_a, const void *_b)
1625{
1626 const struct commit *a = *(const struct commit **)_a;
1627 const struct commit *b = *(const struct commit **)_b;
1628 return oidcmp(&a->object.oid, &b->object.oid);
1629}
1630
1631static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx)
1632{
René Scharfe689a1462019-09-15 19:07:44 +02001633 uint32_t i;
Derrick Stolee1771be92019-06-18 11:14:29 -07001634
1635 if (ctx->report_progress)
1636 ctx->progress = start_delayed_progress(
1637 _("Scanning merged commits"),
1638 ctx->commits.nr);
1639
1640 QSORT(ctx->commits.list, ctx->commits.nr, commit_compare);
1641
1642 ctx->num_extra_edges = 0;
1643 for (i = 0; i < ctx->commits.nr; i++) {
1644 display_progress(ctx->progress, i);
1645
1646 if (i && oideq(&ctx->commits.list[i - 1]->object.oid,
1647 &ctx->commits.list[i]->object.oid)) {
1648 die(_("unexpected duplicate commit id %s"),
1649 oid_to_hex(&ctx->commits.list[i]->object.oid));
1650 } else {
René Scharfe689a1462019-09-15 19:07:44 +02001651 unsigned int num_parents;
Derrick Stolee1771be92019-06-18 11:14:29 -07001652
René Scharfe689a1462019-09-15 19:07:44 +02001653 num_parents = commit_list_count(ctx->commits.list[i]->parents);
Derrick Stolee1771be92019-06-18 11:14:29 -07001654 if (num_parents > 2)
Derrick Stoleea35bea42019-08-05 09:43:41 -07001655 ctx->num_extra_edges += num_parents - 1;
Derrick Stolee1771be92019-06-18 11:14:29 -07001656 }
1657 }
1658
1659 stop_progress(&ctx->progress);
1660}
1661
1662static void merge_commit_graphs(struct write_commit_graph_context *ctx)
1663{
1664 struct commit_graph *g = ctx->r->objects->commit_graph;
1665 uint32_t current_graph_number = ctx->num_commit_graphs_before;
1666 struct strbuf progress_title = STRBUF_INIT;
1667
1668 while (g && current_graph_number >= ctx->num_commit_graphs_after) {
1669 current_graph_number--;
1670
1671 if (ctx->report_progress) {
1672 strbuf_addstr(&progress_title, _("Merging commit-graph"));
1673 ctx->progress = start_delayed_progress(progress_title.buf, 0);
1674 }
1675
1676 merge_commit_graph(ctx, g);
1677 stop_progress(&ctx->progress);
1678 strbuf_release(&progress_title);
1679
1680 g = g->base_graph;
1681 }
1682
1683 if (g) {
1684 ctx->new_base_graph = g;
1685 ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base;
1686 }
1687
1688 if (ctx->new_base_graph)
1689 ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename);
1690
1691 sort_and_scan_merged_commits(ctx);
1692}
1693
Derrick Stolee8d840972019-06-18 11:14:31 -07001694static void mark_commit_graphs(struct write_commit_graph_context *ctx)
1695{
1696 uint32_t i;
1697 time_t now = time(NULL);
1698
1699 for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) {
1700 struct stat st;
1701 struct utimbuf updated_time;
1702
1703 stat(ctx->commit_graph_filenames_before[i], &st);
1704
1705 updated_time.actime = st.st_atime;
1706 updated_time.modtime = now;
1707 utime(ctx->commit_graph_filenames_before[i], &updated_time);
1708 }
1709}
1710
1711static void expire_commit_graphs(struct write_commit_graph_context *ctx)
1712{
1713 struct strbuf path = STRBUF_INIT;
1714 DIR *dir;
1715 struct dirent *de;
1716 size_t dirnamelen;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001717 timestamp_t expire_time = time(NULL);
1718
1719 if (ctx->split_opts && ctx->split_opts->expire_time)
1720 expire_time -= ctx->split_opts->expire_time;
Derrick Stoleeba411122019-06-18 11:14:33 -07001721 if (!ctx->split) {
1722 char *chain_file_name = get_chain_filename(ctx->obj_dir);
1723 unlink(chain_file_name);
1724 free(chain_file_name);
1725 ctx->num_commit_graphs_after = 0;
1726 }
Derrick Stolee8d840972019-06-18 11:14:31 -07001727
1728 strbuf_addstr(&path, ctx->obj_dir);
1729 strbuf_addstr(&path, "/info/commit-graphs");
1730 dir = opendir(path.buf);
1731
René Scharfe0aa6bce2019-08-07 13:15:02 +02001732 if (!dir)
1733 goto out;
Derrick Stolee8d840972019-06-18 11:14:31 -07001734
1735 strbuf_addch(&path, '/');
1736 dirnamelen = path.len;
1737 while ((de = readdir(dir)) != NULL) {
1738 struct stat st;
1739 uint32_t i, found = 0;
1740
1741 strbuf_setlen(&path, dirnamelen);
1742 strbuf_addstr(&path, de->d_name);
1743
1744 stat(path.buf, &st);
1745
1746 if (st.st_mtime > expire_time)
1747 continue;
1748 if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph"))
1749 continue;
1750
1751 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1752 if (!strcmp(ctx->commit_graph_filenames_after[i],
1753 path.buf)) {
1754 found = 1;
1755 break;
1756 }
1757 }
1758
1759 if (!found)
1760 unlink(path.buf);
Derrick Stolee8d840972019-06-18 11:14:31 -07001761 }
René Scharfe0aa6bce2019-08-07 13:15:02 +02001762
1763out:
1764 strbuf_release(&path);
Derrick Stolee8d840972019-06-18 11:14:31 -07001765}
1766
Derrick Stoleee103f722019-06-12 06:29:37 -07001767int write_commit_graph(const char *obj_dir,
1768 struct string_list *pack_indexes,
1769 struct string_list *commit_hex,
SZEDER Gábor39d88312019-08-05 10:02:39 +02001770 enum commit_graph_write_flags flags,
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001771 const struct split_commit_graph_opts *split_opts)
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001772{
Derrick Stoleec9905be2019-06-12 06:29:40 -07001773 struct write_commit_graph_context *ctx;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001774 uint32_t i, count_distinct = 0;
Derrick Stolee16110c92019-06-18 11:14:36 -07001775 size_t len;
Derrick Stoleee103f722019-06-12 06:29:37 -07001776 int res = 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001777
1778 if (!commit_graph_compatible(the_repository))
Derrick Stoleee103f722019-06-12 06:29:37 -07001779 return 0;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001780
Derrick Stoleec9905be2019-06-12 06:29:40 -07001781 ctx = xcalloc(1, sizeof(struct write_commit_graph_context));
1782 ctx->r = the_repository;
Derrick Stolee16110c92019-06-18 11:14:36 -07001783
1784 /* normalize object dir with no trailing slash */
1785 ctx->obj_dir = xmallocz(strlen(obj_dir) + 1);
1786 normalize_path_copy(ctx->obj_dir, obj_dir);
1787 len = strlen(ctx->obj_dir);
1788 if (len && ctx->obj_dir[len - 1] == '/')
1789 ctx->obj_dir[len - 1] = 0;
1790
SZEDER Gábor39d88312019-08-05 10:02:39 +02001791 ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0;
1792 ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0;
1793 ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0;
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001794 ctx->check_oids = flags & COMMIT_GRAPH_WRITE_CHECK_OIDS ? 1 : 0;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001795 ctx->split_opts = split_opts;
Derrick Stolee6c622f92019-06-18 11:14:27 -07001796
1797 if (ctx->split) {
1798 struct commit_graph *g;
1799 prepare_commit_graph(ctx->r);
1800
1801 g = ctx->r->objects->commit_graph;
1802
1803 while (g) {
1804 ctx->num_commit_graphs_before++;
1805 g = g->base_graph;
1806 }
1807
1808 if (ctx->num_commit_graphs_before) {
1809 ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before);
1810 i = ctx->num_commit_graphs_before;
1811 g = ctx->r->objects->commit_graph;
1812
1813 while (g) {
1814 ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename);
1815 g = g->base_graph;
1816 }
1817 }
1818 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001819
Derrick Stoleec9905be2019-06-12 06:29:40 -07001820 ctx->approx_nr_objects = approximate_object_count();
1821 ctx->oids.alloc = ctx->approx_nr_objects / 32;
1822
Derrick Stoleec2bc6e62019-06-18 11:14:32 -07001823 if (ctx->split && split_opts && ctx->oids.alloc > split_opts->max_commits)
1824 ctx->oids.alloc = split_opts->max_commits;
Derrick Stolee1771be92019-06-18 11:14:29 -07001825
Derrick Stoleec9905be2019-06-12 06:29:40 -07001826 if (ctx->append) {
1827 prepare_commit_graph_one(ctx->r, ctx->obj_dir);
1828 if (ctx->r->objects->commit_graph)
1829 ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001830 }
1831
Derrick Stoleec9905be2019-06-12 06:29:40 -07001832 if (ctx->oids.alloc < 1024)
1833 ctx->oids.alloc = 1024;
1834 ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001835
Derrick Stoleec9905be2019-06-12 06:29:40 -07001836 if (ctx->append && ctx->r->objects->commit_graph) {
1837 struct commit_graph *g = ctx->r->objects->commit_graph;
1838 for (i = 0; i < g->num_commits; i++) {
1839 const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
1840 hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001841 }
1842 }
1843
1844 if (pack_indexes) {
Derrick Stoleeef5b83f2019-06-12 06:29:41 -07001845 if ((res = fill_oids_from_packs(ctx, pack_indexes)))
1846 goto cleanup;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001847 }
1848
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +02001849 if (commit_hex) {
1850 if ((res = fill_oids_from_commit_hex(ctx, commit_hex)))
1851 goto cleanup;
1852 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001853
Derrick Stoleeb2c83062019-06-12 06:29:42 -07001854 if (!pack_indexes && !commit_hex)
1855 fill_oids_from_all_packs(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001856
Derrick Stoleec9905be2019-06-12 06:29:40 -07001857 close_reachable(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001858
Derrick Stolee014e3442019-06-12 06:29:43 -07001859 count_distinct = count_distinct_commits(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001860
Derrick Stoleee103f722019-06-12 06:29:37 -07001861 if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
1862 error(_("the commit graph format cannot write %d commits"), count_distinct);
1863 res = -1;
1864 goto cleanup;
1865 }
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001866
Derrick Stoleec9905be2019-06-12 06:29:40 -07001867 ctx->commits.alloc = count_distinct;
1868 ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001869
Derrick Stoleef998d542019-06-12 06:29:44 -07001870 copy_oids_to_commits(ctx);
Derrick Stolee1373e542018-06-27 09:24:39 -04001871
Derrick Stoleec9905be2019-06-12 06:29:40 -07001872 if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
Derrick Stoleee103f722019-06-12 06:29:37 -07001873 error(_("too many commits to write graph"));
1874 res = -1;
1875 goto cleanup;
1876 }
Derrick Stolee283e68c2018-06-27 09:24:32 -04001877
Derrick Stolee6c622f92019-06-18 11:14:27 -07001878 if (!ctx->commits.nr)
1879 goto cleanup;
1880
Derrick Stolee1771be92019-06-18 11:14:29 -07001881 if (ctx->split) {
1882 split_graph_merge_strategy(ctx);
1883
1884 merge_commit_graphs(ctx);
1885 } else
Derrick Stolee6c622f92019-06-18 11:14:27 -07001886 ctx->num_commit_graphs_after = 1;
1887
Derrick Stoleec9905be2019-06-12 06:29:40 -07001888 compute_generation_numbers(ctx);
Derrick Stolee9bda8462018-06-27 09:24:35 -04001889
Derrick Stolee238def52019-06-12 06:29:45 -07001890 res = write_commit_graph_file(ctx);
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001891
Derrick Stoleeba411122019-06-18 11:14:33 -07001892 if (ctx->split)
Derrick Stolee8d840972019-06-18 11:14:31 -07001893 mark_commit_graphs(ctx);
Derrick Stoleeba411122019-06-18 11:14:33 -07001894
1895 expire_commit_graphs(ctx);
Derrick Stolee8d840972019-06-18 11:14:31 -07001896
Derrick Stoleee103f722019-06-12 06:29:37 -07001897cleanup:
Derrick Stolee238def52019-06-12 06:29:45 -07001898 free(ctx->graph_name);
Derrick Stoleec9905be2019-06-12 06:29:40 -07001899 free(ctx->commits.list);
1900 free(ctx->oids.list);
Derrick Stolee16110c92019-06-18 11:14:36 -07001901 free(ctx->obj_dir);
Derrick Stolee6c622f92019-06-18 11:14:27 -07001902
1903 if (ctx->commit_graph_filenames_after) {
1904 for (i = 0; i < ctx->num_commit_graphs_after; i++) {
1905 free(ctx->commit_graph_filenames_after[i]);
1906 free(ctx->commit_graph_hash_after[i]);
1907 }
1908
1909 for (i = 0; i < ctx->num_commit_graphs_before; i++)
1910 free(ctx->commit_graph_filenames_before[i]);
1911
1912 free(ctx->commit_graph_filenames_after);
1913 free(ctx->commit_graph_filenames_before);
1914 free(ctx->commit_graph_hash_after);
1915 }
1916
Derrick Stoleec9905be2019-06-12 06:29:40 -07001917 free(ctx);
Derrick Stoleee103f722019-06-12 06:29:37 -07001918
1919 return res;
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001920}
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001921
1922#define VERIFY_COMMIT_GRAPH_ERROR_HASH 2
1923static int verify_commit_graph_error;
1924
Derrick Stolee283e68c2018-06-27 09:24:32 -04001925static void graph_report(const char *fmt, ...)
1926{
1927 va_list ap;
1928
1929 verify_commit_graph_error = 1;
1930 va_start(ap, fmt);
1931 vfprintf(stderr, fmt, ap);
1932 fprintf(stderr, "\n");
1933 va_end(ap);
1934}
1935
1936#define GENERATION_ZERO_EXISTS 1
1937#define GENERATION_NUMBER_EXISTS 2
1938
Derrick Stolee3da4b602019-06-18 11:14:32 -07001939int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags)
Derrick Stolee283e68c2018-06-27 09:24:32 -04001940{
Derrick Stolee9bda8462018-06-27 09:24:35 -04001941 uint32_t i, cur_fanout_pos = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04001942 struct object_id prev_oid, cur_oid, checksum;
Derrick Stolee1373e542018-06-27 09:24:39 -04001943 int generation_zero = 0;
Derrick Stolee41df0e32018-06-27 09:24:42 -04001944 struct hashfile *f;
1945 int devnull;
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00001946 struct progress *progress = NULL;
Derrick Stolee3da4b602019-06-18 11:14:32 -07001947 int local_error = 0;
Derrick Stolee9bda8462018-06-27 09:24:35 -04001948
Derrick Stolee283e68c2018-06-27 09:24:32 -04001949 if (!g) {
1950 graph_report("no commit-graph file loaded");
1951 return 1;
1952 }
1953
Ævar Arnfjörð Bjarmason2ac138d2019-03-25 13:08:29 +01001954 verify_commit_graph_error = verify_commit_graph_lite(g);
Derrick Stolee9bda8462018-06-27 09:24:35 -04001955 if (verify_commit_graph_error)
1956 return verify_commit_graph_error;
1957
Derrick Stolee41df0e32018-06-27 09:24:42 -04001958 devnull = open("/dev/null", O_WRONLY);
1959 f = hashfd(devnull, NULL);
1960 hashwrite(f, g->data, g->data_len - g->hash_len);
1961 finalize_hashfile(f, checksum.hash, CSUM_CLOSE);
Jeff King67947c32018-08-28 17:22:52 -04001962 if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) {
Derrick Stolee41df0e32018-06-27 09:24:42 -04001963 graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt"));
1964 verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH;
1965 }
1966
Derrick Stolee9bda8462018-06-27 09:24:35 -04001967 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04001968 struct commit *graph_commit;
1969
Derrick Stolee9bda8462018-06-27 09:24:35 -04001970 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
1971
1972 if (i && oidcmp(&prev_oid, &cur_oid) >= 0)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001973 graph_report(_("commit-graph has incorrect OID order: %s then %s"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04001974 oid_to_hex(&prev_oid),
1975 oid_to_hex(&cur_oid));
1976
1977 oidcpy(&prev_oid, &cur_oid);
1978
1979 while (cur_oid.hash[0] > cur_fanout_pos) {
1980 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1981
1982 if (i != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001983 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04001984 cur_fanout_pos, fanout_value, i);
1985 cur_fanout_pos++;
1986 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04001987
Junio C Hamano82952962018-07-17 15:46:19 -07001988 graph_commit = lookup_commit(r, &cur_oid);
Stefan Beller4f542b72018-12-14 16:09:39 -08001989 if (!parse_commit_in_graph_one(r, g, graph_commit))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001990 graph_report(_("failed to parse commit %s from commit-graph"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04001991 oid_to_hex(&cur_oid));
Derrick Stolee9bda8462018-06-27 09:24:35 -04001992 }
1993
1994 while (cur_fanout_pos < 256) {
1995 uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos);
1996
1997 if (g->num_commits != fanout_value)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01001998 graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"),
Derrick Stolee9bda8462018-06-27 09:24:35 -04001999 cur_fanout_pos, fanout_value, i);
2000
2001 cur_fanout_pos++;
2002 }
2003
Derrick Stolee41df0e32018-06-27 09:24:42 -04002004 if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH)
Derrick Stolee96af91d2018-06-27 09:24:36 -04002005 return verify_commit_graph_error;
2006
Garima Singh73716122019-08-26 09:29:58 -07002007 if (flags & COMMIT_GRAPH_WRITE_PROGRESS)
2008 progress = start_progress(_("Verifying commits in commit graph"),
2009 g->num_commits);
2010
Derrick Stolee96af91d2018-06-27 09:24:36 -04002011 for (i = 0; i < g->num_commits; i++) {
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002012 struct commit *graph_commit, *odb_commit;
Derrick Stolee53614b12018-06-27 09:24:38 -04002013 struct commit_list *graph_parents, *odb_parents;
Derrick Stolee1373e542018-06-27 09:24:39 -04002014 uint32_t max_generation = 0;
Derrick Stolee96af91d2018-06-27 09:24:36 -04002015
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002016 display_progress(progress, i + 1);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002017 hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2018
Junio C Hamano82952962018-07-17 15:46:19 -07002019 graph_commit = lookup_commit(r, &cur_oid);
Jeff Kinga3785092019-06-20 03:41:21 -04002020 odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r));
Derrick Stolee96af91d2018-06-27 09:24:36 -04002021 if (parse_commit_internal(odb_commit, 0, 0)) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002022 graph_report(_("failed to parse commit %s from object database for commit-graph"),
Derrick Stolee96af91d2018-06-27 09:24:36 -04002023 oid_to_hex(&cur_oid));
2024 continue;
2025 }
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002026
Stefan Beller4f542b72018-12-14 16:09:39 -08002027 if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid,
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002028 get_commit_tree_oid(odb_commit)))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002029 graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"),
Derrick Stolee2e3c0732018-06-27 09:24:37 -04002030 oid_to_hex(&cur_oid),
2031 oid_to_hex(get_commit_tree_oid(graph_commit)),
2032 oid_to_hex(get_commit_tree_oid(odb_commit)));
Derrick Stolee53614b12018-06-27 09:24:38 -04002033
2034 graph_parents = graph_commit->parents;
2035 odb_parents = odb_commit->parents;
2036
2037 while (graph_parents) {
2038 if (odb_parents == NULL) {
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002039 graph_report(_("commit-graph parent list for commit %s is too long"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002040 oid_to_hex(&cur_oid));
2041 break;
2042 }
2043
Derrick Stolee3da4b602019-06-18 11:14:32 -07002044 /* parse parent in case it is in a base graph */
2045 parse_commit_in_graph_one(r, g, graph_parents->item);
2046
Jeff King9001dc22018-08-28 17:22:48 -04002047 if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid))
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002048 graph_report(_("commit-graph parent for %s is %s != %s"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002049 oid_to_hex(&cur_oid),
2050 oid_to_hex(&graph_parents->item->object.oid),
2051 oid_to_hex(&odb_parents->item->object.oid));
2052
Derrick Stolee1373e542018-06-27 09:24:39 -04002053 if (graph_parents->item->generation > max_generation)
2054 max_generation = graph_parents->item->generation;
2055
Derrick Stolee53614b12018-06-27 09:24:38 -04002056 graph_parents = graph_parents->next;
2057 odb_parents = odb_parents->next;
2058 }
2059
2060 if (odb_parents != NULL)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002061 graph_report(_("commit-graph parent list for commit %s terminates early"),
Derrick Stolee53614b12018-06-27 09:24:38 -04002062 oid_to_hex(&cur_oid));
Derrick Stolee1373e542018-06-27 09:24:39 -04002063
2064 if (!graph_commit->generation) {
2065 if (generation_zero == GENERATION_NUMBER_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002066 graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002067 oid_to_hex(&cur_oid));
2068 generation_zero = GENERATION_ZERO_EXISTS;
2069 } else if (generation_zero == GENERATION_ZERO_EXISTS)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002070 graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002071 oid_to_hex(&cur_oid));
2072
2073 if (generation_zero == GENERATION_ZERO_EXISTS)
2074 continue;
2075
2076 /*
2077 * If one of our parents has generation GENERATION_NUMBER_MAX, then
2078 * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid
2079 * extra logic in the following condition.
2080 */
2081 if (max_generation == GENERATION_NUMBER_MAX)
2082 max_generation--;
2083
2084 if (graph_commit->generation != max_generation + 1)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002085 graph_report(_("commit-graph generation for commit %s is %u != %u"),
Derrick Stolee1373e542018-06-27 09:24:39 -04002086 oid_to_hex(&cur_oid),
2087 graph_commit->generation,
2088 max_generation + 1);
Derrick Stolee88968eb2018-06-27 09:24:40 -04002089
2090 if (graph_commit->date != odb_commit->date)
Ævar Arnfjörð Bjarmason93b44052019-03-25 13:08:34 +01002091 graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime),
Derrick Stolee88968eb2018-06-27 09:24:40 -04002092 oid_to_hex(&cur_oid),
2093 graph_commit->date,
2094 odb_commit->date);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002095 }
Ævar Arnfjörð Bjarmason1f7f5572018-09-17 15:33:36 +00002096 stop_progress(&progress);
Derrick Stolee96af91d2018-06-27 09:24:36 -04002097
Derrick Stolee3da4b602019-06-18 11:14:32 -07002098 local_error = verify_commit_graph_error;
2099
2100 if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph)
2101 local_error |= verify_commit_graph(r, g->base_graph, flags);
2102
2103 return local_error;
Derrick Stolee283e68c2018-06-27 09:24:32 -04002104}
Jonathan Tanc3756d52018-07-11 15:42:40 -07002105
2106void free_commit_graph(struct commit_graph *g)
2107{
2108 if (!g)
2109 return;
2110 if (g->graph_fd >= 0) {
2111 munmap((void *)g->data, g->data_len);
2112 g->data = NULL;
2113 close(g->graph_fd);
2114 }
Derrick Stolee6c622f92019-06-18 11:14:27 -07002115 free(g->filename);
Jonathan Tanc3756d52018-07-11 15:42:40 -07002116 free(g);
2117}
Jeff King6abada12019-09-12 10:44:45 -04002118
2119void disable_commit_graph(struct repository *r)
2120{
2121 r->commit_graph_disabled = 1;
2122}