Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1 | #include "git-compat-util.h" |
SZEDER Gábor | fa79653 | 2020-06-05 13:00:28 +0000 | [diff] [blame] | 2 | #include "config.h" |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 3 | #include "lockfile.h" |
| 4 | #include "pack.h" |
| 5 | #include "packfile.h" |
| 6 | #include "commit.h" |
| 7 | #include "object.h" |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 8 | #include "refs.h" |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 9 | #include "revision.h" |
| 10 | #include "sha1-lookup.h" |
| 11 | #include "commit-graph.h" |
Junio C Hamano | b10edb2 | 2018-05-08 15:59:20 +0900 | [diff] [blame] | 12 | #include "object-store.h" |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 13 | #include "alloc.h" |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 14 | #include "hashmap.h" |
| 15 | #include "replace-object.h" |
Ævar Arnfjörð Bjarmason | 7b0f229 | 2018-09-17 15:33:35 +0000 | [diff] [blame] | 16 | #include "progress.h" |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 17 | #include "bloom.h" |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 18 | #include "commit-slab.h" |
Taylor Blau | 120ad2b | 2020-04-30 13:48:50 -0600 | [diff] [blame] | 19 | #include "shallow.h" |
Derrick Stolee | 0087a87 | 2020-07-01 13:27:24 +0000 | [diff] [blame] | 20 | #include "json-writer.h" |
| 21 | #include "trace2.h" |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 22 | |
Derrick Stolee | b23ea97 | 2020-04-16 20:14:03 +0000 | [diff] [blame] | 23 | void git_test_write_commit_graph_or_die(void) |
| 24 | { |
| 25 | int flags = 0; |
| 26 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0)) |
| 27 | return; |
| 28 | |
| 29 | if (git_env_bool(GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS, 0)) |
| 30 | flags = COMMIT_GRAPH_WRITE_BLOOM_FILTERS; |
| 31 | |
| 32 | if (write_commit_graph_reachable(the_repository->objects->odb, |
| 33 | flags, NULL)) |
| 34 | die("failed to write commit-graph under GIT_TEST_COMMIT_GRAPH"); |
| 35 | } |
| 36 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 37 | #define GRAPH_SIGNATURE 0x43475048 /* "CGPH" */ |
| 38 | #define GRAPH_CHUNKID_OIDFANOUT 0x4f494446 /* "OIDF" */ |
| 39 | #define GRAPH_CHUNKID_OIDLOOKUP 0x4f49444c /* "OIDL" */ |
| 40 | #define GRAPH_CHUNKID_DATA 0x43444154 /* "CDAT" */ |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 41 | #define GRAPH_CHUNKID_EXTRAEDGES 0x45444745 /* "EDGE" */ |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 42 | #define GRAPH_CHUNKID_BLOOMINDEXES 0x42494458 /* "BIDX" */ |
| 43 | #define GRAPH_CHUNKID_BLOOMDATA 0x42444154 /* "BDAT" */ |
Derrick Stolee | 118bd57 | 2019-06-18 11:14:26 -0700 | [diff] [blame] | 44 | #define GRAPH_CHUNKID_BASE 0x42415345 /* "BASE" */ |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 45 | #define MAX_NUM_CHUNKS 7 |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 46 | |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 47 | #define GRAPH_DATA_WIDTH (the_hash_algo->rawsz + 16) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 48 | |
| 49 | #define GRAPH_VERSION_1 0x1 |
| 50 | #define GRAPH_VERSION GRAPH_VERSION_1 |
| 51 | |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 52 | #define GRAPH_EXTRA_EDGES_NEEDED 0x80000000 |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 53 | #define GRAPH_EDGE_LAST_MASK 0x7fffffff |
| 54 | #define GRAPH_PARENT_NONE 0x70000000 |
| 55 | |
| 56 | #define GRAPH_LAST_EDGE 0x80000000 |
| 57 | |
Derrick Stolee | 0e3b97c | 2018-06-27 09:24:28 -0400 | [diff] [blame] | 58 | #define GRAPH_HEADER_SIZE 8 |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 59 | #define GRAPH_FANOUT_SIZE (4 * 256) |
| 60 | #define GRAPH_CHUNKLOOKUP_WIDTH 12 |
Derrick Stolee | 0e3b97c | 2018-06-27 09:24:28 -0400 | [diff] [blame] | 61 | #define GRAPH_MIN_SIZE (GRAPH_HEADER_SIZE + 4 * GRAPH_CHUNKLOOKUP_WIDTH \ |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 62 | + GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 63 | |
Derrick Stolee | cb99a34 | 2019-10-24 13:40:42 +0000 | [diff] [blame] | 64 | /* Remember to update object flag allocation in object.h */ |
| 65 | #define REACHABLE (1u<<15) |
| 66 | |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 67 | /* Keep track of the order in which commits are added to our list. */ |
| 68 | define_commit_slab(commit_pos, int); |
| 69 | static struct commit_pos commit_pos = COMMIT_SLAB_INIT(1, commit_pos); |
| 70 | |
| 71 | static void set_commit_pos(struct repository *r, const struct object_id *oid) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 72 | { |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 73 | static int32_t max_pos; |
| 74 | struct commit *commit = lookup_commit(r, oid); |
| 75 | |
| 76 | if (!commit) |
| 77 | return; /* should never happen, but be lenient */ |
| 78 | |
| 79 | *commit_pos_at(&commit_pos, commit) = max_pos++; |
| 80 | } |
| 81 | |
| 82 | static int commit_pos_cmp(const void *va, const void *vb) |
| 83 | { |
| 84 | const struct commit *a = *(const struct commit **)va; |
| 85 | const struct commit *b = *(const struct commit **)vb; |
| 86 | return commit_pos_at(&commit_pos, a) - |
| 87 | commit_pos_at(&commit_pos, b); |
| 88 | } |
| 89 | |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 90 | define_commit_slab(commit_graph_data_slab, struct commit_graph_data); |
| 91 | static struct commit_graph_data_slab commit_graph_data_slab = |
| 92 | COMMIT_SLAB_INIT(1, commit_graph_data_slab); |
| 93 | |
| 94 | uint32_t commit_graph_position(const struct commit *c) |
| 95 | { |
| 96 | struct commit_graph_data *data = |
| 97 | commit_graph_data_slab_peek(&commit_graph_data_slab, c); |
| 98 | |
| 99 | return data ? data->graph_pos : COMMIT_NOT_FROM_GRAPH; |
| 100 | } |
| 101 | |
| 102 | uint32_t commit_graph_generation(const struct commit *c) |
| 103 | { |
| 104 | struct commit_graph_data *data = |
| 105 | commit_graph_data_slab_peek(&commit_graph_data_slab, c); |
| 106 | |
| 107 | if (!data) |
| 108 | return GENERATION_NUMBER_INFINITY; |
| 109 | else if (data->graph_pos == COMMIT_NOT_FROM_GRAPH) |
| 110 | return GENERATION_NUMBER_INFINITY; |
| 111 | |
| 112 | return data->generation; |
| 113 | } |
| 114 | |
| 115 | static struct commit_graph_data *commit_graph_data_at(const struct commit *c) |
| 116 | { |
| 117 | unsigned int i, nth_slab; |
| 118 | struct commit_graph_data *data = |
| 119 | commit_graph_data_slab_peek(&commit_graph_data_slab, c); |
| 120 | |
| 121 | if (data) |
| 122 | return data; |
| 123 | |
| 124 | nth_slab = c->index / commit_graph_data_slab.slab_size; |
| 125 | data = commit_graph_data_slab_at(&commit_graph_data_slab, c); |
| 126 | |
| 127 | /* |
| 128 | * commit-slab initializes elements with zero, overwrite this with |
| 129 | * COMMIT_NOT_FROM_GRAPH for graph_pos. |
| 130 | * |
| 131 | * We avoid initializing generation with checking if graph position |
| 132 | * is not COMMIT_NOT_FROM_GRAPH. |
| 133 | */ |
| 134 | for (i = 0; i < commit_graph_data_slab.slab_size; i++) { |
| 135 | commit_graph_data_slab.slab[nth_slab][i].graph_pos = |
| 136 | COMMIT_NOT_FROM_GRAPH; |
| 137 | } |
| 138 | |
| 139 | return data; |
| 140 | } |
| 141 | |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 142 | static int commit_gen_cmp(const void *va, const void *vb) |
| 143 | { |
| 144 | const struct commit *a = *(const struct commit **)va; |
| 145 | const struct commit *b = *(const struct commit **)vb; |
| 146 | |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 147 | uint32_t generation_a = commit_graph_generation(a); |
| 148 | uint32_t generation_b = commit_graph_generation(b); |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 149 | /* lower generation commits first */ |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 150 | if (generation_a < generation_b) |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 151 | return -1; |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 152 | else if (generation_a > generation_b) |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 153 | return 1; |
| 154 | |
| 155 | /* use date as a heuristic when generations are equal */ |
| 156 | if (a->date < b->date) |
| 157 | return -1; |
| 158 | else if (a->date > b->date) |
| 159 | return 1; |
| 160 | return 0; |
| 161 | } |
| 162 | |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 163 | char *get_commit_graph_filename(struct object_directory *obj_dir) |
| 164 | { |
| 165 | return xstrfmt("%s/info/commit-graph", obj_dir->path); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 168 | static char *get_split_graph_filename(struct object_directory *odb, |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 169 | const char *oid_hex) |
| 170 | { |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 171 | return xstrfmt("%s/info/commit-graphs/graph-%s.graph", odb->path, |
| 172 | oid_hex); |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Derrick Stolee | 663b2b1 | 2020-09-17 18:11:46 +0000 | [diff] [blame] | 175 | char *get_commit_graph_chain_filename(struct object_directory *odb) |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 176 | { |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 177 | return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 178 | } |
| 179 | |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 180 | static uint8_t oid_version(void) |
| 181 | { |
Derrick Stolee | 665d70a | 2020-08-17 14:04:47 +0000 | [diff] [blame] | 182 | switch (hash_algo_by_ptr(the_hash_algo)) { |
| 183 | case GIT_HASH_SHA1: |
| 184 | return 1; |
| 185 | case GIT_HASH_SHA256: |
| 186 | return 2; |
| 187 | default: |
| 188 | die(_("invalid hash version")); |
| 189 | } |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 192 | static struct commit_graph *alloc_commit_graph(void) |
| 193 | { |
| 194 | struct commit_graph *g = xcalloc(1, sizeof(*g)); |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 195 | |
| 196 | return g; |
| 197 | } |
| 198 | |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 199 | extern int read_replace_refs; |
| 200 | |
| 201 | static int commit_graph_compatible(struct repository *r) |
| 202 | { |
Derrick Stolee | 5cef295 | 2018-08-20 18:24:32 +0000 | [diff] [blame] | 203 | if (!r->gitdir) |
| 204 | return 0; |
| 205 | |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 206 | if (read_replace_refs) { |
| 207 | prepare_replace_object(r); |
| 208 | if (hashmap_get_size(&r->objects->replace_map->map)) |
| 209 | return 0; |
| 210 | } |
| 211 | |
Derrick Stolee | 20fd6d5 | 2018-08-20 18:24:30 +0000 | [diff] [blame] | 212 | prepare_commit_graft(r); |
Taylor Blau | ce16364 | 2020-07-08 17:10:53 -0400 | [diff] [blame] | 213 | if (r->parsed_objects && |
| 214 | (r->parsed_objects->grafts_nr || r->parsed_objects->substituted_parent)) |
Derrick Stolee | 20fd6d5 | 2018-08-20 18:24:30 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | if (is_repository_shallow(r)) |
| 217 | return 0; |
| 218 | |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 219 | return 1; |
| 220 | } |
| 221 | |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 222 | int open_commit_graph(const char *graph_file, int *fd, struct stat *st) |
| 223 | { |
| 224 | *fd = git_open(graph_file); |
| 225 | if (*fd < 0) |
| 226 | return 0; |
| 227 | if (fstat(*fd, st)) { |
| 228 | close(*fd); |
| 229 | return 0; |
| 230 | } |
| 231 | return 1; |
| 232 | } |
| 233 | |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 234 | struct commit_graph *load_commit_graph_one_fd_st(struct repository *r, |
| 235 | int fd, struct stat *st, |
Taylor Blau | a7df60c | 2020-02-03 13:18:04 -0800 | [diff] [blame] | 236 | struct object_directory *odb) |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 237 | { |
| 238 | void *graph_map; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 239 | size_t graph_size; |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 240 | struct commit_graph *ret; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 241 | |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 242 | graph_size = xsize_t(st->st_size); |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 243 | |
| 244 | if (graph_size < GRAPH_MIN_SIZE) { |
| 245 | close(fd); |
Ævar Arnfjörð Bjarmason | 67a530f | 2019-03-25 13:08:31 +0100 | [diff] [blame] | 246 | error(_("commit-graph file is too small")); |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 247 | return NULL; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 248 | } |
| 249 | graph_map = xmmap(NULL, graph_size, PROT_READ, MAP_PRIVATE, fd, 0); |
Jeff King | c882853 | 2020-04-23 15:41:13 -0600 | [diff] [blame] | 250 | close(fd); |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 251 | ret = parse_commit_graph(r, graph_map, graph_size); |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 252 | |
Taylor Blau | a7df60c | 2020-02-03 13:18:04 -0800 | [diff] [blame] | 253 | if (ret) |
| 254 | ret->odb = odb; |
Jeff King | c882853 | 2020-04-23 15:41:13 -0600 | [diff] [blame] | 255 | else |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 256 | munmap(graph_map, graph_size); |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 257 | |
| 258 | return ret; |
| 259 | } |
| 260 | |
Ævar Arnfjörð Bjarmason | 2ac138d | 2019-03-25 13:08:29 +0100 | [diff] [blame] | 261 | static int verify_commit_graph_lite(struct commit_graph *g) |
| 262 | { |
| 263 | /* |
| 264 | * Basic validation shared between parse_commit_graph() |
| 265 | * which'll be called every time the graph is used, and the |
| 266 | * much more expensive verify_commit_graph() used by |
| 267 | * "commit-graph verify". |
| 268 | * |
| 269 | * There should only be very basic checks here to ensure that |
| 270 | * we don't e.g. segfault in fill_commit_in_graph(), but |
| 271 | * because this is a very hot codepath nothing that e.g. loops |
| 272 | * over g->num_commits, or runs a checksum on the commit-graph |
| 273 | * itself. |
| 274 | */ |
| 275 | if (!g->chunk_oid_fanout) { |
| 276 | error("commit-graph is missing the OID Fanout chunk"); |
| 277 | return 1; |
| 278 | } |
| 279 | if (!g->chunk_oid_lookup) { |
| 280 | error("commit-graph is missing the OID Lookup chunk"); |
| 281 | return 1; |
| 282 | } |
| 283 | if (!g->chunk_commit_data) { |
| 284 | error("commit-graph is missing the Commit Data chunk"); |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | } |
| 290 | |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 291 | struct commit_graph *parse_commit_graph(struct repository *r, |
| 292 | void *graph_map, size_t graph_size) |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 293 | { |
| 294 | const unsigned char *data, *chunk_lookup; |
| 295 | uint32_t i; |
| 296 | struct commit_graph *graph; |
SZEDER Gábor | 5cfa438 | 2020-06-05 13:00:30 +0000 | [diff] [blame] | 297 | uint64_t next_chunk_offset; |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 298 | uint32_t graph_signature; |
| 299 | unsigned char graph_version, hash_version; |
| 300 | |
| 301 | if (!graph_map) |
| 302 | return NULL; |
| 303 | |
| 304 | if (graph_size < GRAPH_MIN_SIZE) |
| 305 | return NULL; |
| 306 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 307 | data = (const unsigned char *)graph_map; |
| 308 | |
| 309 | graph_signature = get_be32(data); |
| 310 | if (graph_signature != GRAPH_SIGNATURE) { |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 311 | error(_("commit-graph signature %X does not match signature %X"), |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 312 | graph_signature, GRAPH_SIGNATURE); |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 313 | return NULL; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | graph_version = *(unsigned char*)(data + 4); |
| 317 | if (graph_version != GRAPH_VERSION) { |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 318 | error(_("commit-graph version %X does not match version %X"), |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 319 | graph_version, GRAPH_VERSION); |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 320 | return NULL; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | hash_version = *(unsigned char*)(data + 5); |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 324 | if (hash_version != oid_version()) { |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 325 | error(_("commit-graph hash version %X does not match version %X"), |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 326 | hash_version, oid_version()); |
Josh Steadmon | aa65857 | 2019-01-15 14:25:50 -0800 | [diff] [blame] | 327 | return NULL; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Taylor Blau | b66d847 | 2020-09-09 11:23:10 -0400 | [diff] [blame] | 330 | prepare_repo_settings(r); |
| 331 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 332 | graph = alloc_commit_graph(); |
| 333 | |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 334 | graph->hash_len = the_hash_algo->rawsz; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 335 | graph->num_chunks = *(unsigned char*)(data + 6); |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 336 | graph->data = graph_map; |
| 337 | graph->data_len = graph_size; |
| 338 | |
SZEDER Gábor | 2ad4f1a | 2020-06-05 13:00:29 +0000 | [diff] [blame] | 339 | if (graph_size < GRAPH_HEADER_SIZE + |
| 340 | (graph->num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH + |
| 341 | GRAPH_FANOUT_SIZE + the_hash_algo->rawsz) { |
| 342 | error(_("commit-graph file is too small to hold %u chunks"), |
| 343 | graph->num_chunks); |
| 344 | free(graph); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 348 | chunk_lookup = data + 8; |
SZEDER Gábor | 5cfa438 | 2020-06-05 13:00:30 +0000 | [diff] [blame] | 349 | next_chunk_offset = get_be64(chunk_lookup + 4); |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 350 | for (i = 0; i < graph->num_chunks; i++) { |
Josh Steadmon | d2b86fb | 2019-01-15 14:25:51 -0800 | [diff] [blame] | 351 | uint32_t chunk_id; |
SZEDER Gábor | 5cfa438 | 2020-06-05 13:00:30 +0000 | [diff] [blame] | 352 | uint64_t chunk_offset = next_chunk_offset; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 353 | int chunk_repeated = 0; |
| 354 | |
Josh Steadmon | d2b86fb | 2019-01-15 14:25:51 -0800 | [diff] [blame] | 355 | chunk_id = get_be32(chunk_lookup + 0); |
Josh Steadmon | d2b86fb | 2019-01-15 14:25:51 -0800 | [diff] [blame] | 356 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 357 | chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH; |
SZEDER Gábor | 5cfa438 | 2020-06-05 13:00:30 +0000 | [diff] [blame] | 358 | next_chunk_offset = get_be64(chunk_lookup + 4); |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 359 | |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 360 | if (chunk_offset > graph_size - the_hash_algo->rawsz) { |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 361 | error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32), |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 362 | (uint32_t)chunk_offset); |
Jonathan Tan | fbda77c | 2020-05-04 12:13:24 -0700 | [diff] [blame] | 363 | goto free_and_return; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | switch (chunk_id) { |
| 367 | case GRAPH_CHUNKID_OIDFANOUT: |
| 368 | if (graph->chunk_oid_fanout) |
| 369 | chunk_repeated = 1; |
| 370 | else |
| 371 | graph->chunk_oid_fanout = (uint32_t*)(data + chunk_offset); |
| 372 | break; |
| 373 | |
| 374 | case GRAPH_CHUNKID_OIDLOOKUP: |
| 375 | if (graph->chunk_oid_lookup) |
| 376 | chunk_repeated = 1; |
SZEDER Gábor | 5cfa438 | 2020-06-05 13:00:30 +0000 | [diff] [blame] | 377 | else { |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 378 | graph->chunk_oid_lookup = data + chunk_offset; |
SZEDER Gábor | 5cfa438 | 2020-06-05 13:00:30 +0000 | [diff] [blame] | 379 | graph->num_commits = (next_chunk_offset - chunk_offset) |
| 380 | / graph->hash_len; |
| 381 | } |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case GRAPH_CHUNKID_DATA: |
| 385 | if (graph->chunk_commit_data) |
| 386 | chunk_repeated = 1; |
| 387 | else |
| 388 | graph->chunk_commit_data = data + chunk_offset; |
| 389 | break; |
| 390 | |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 391 | case GRAPH_CHUNKID_EXTRAEDGES: |
| 392 | if (graph->chunk_extra_edges) |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 393 | chunk_repeated = 1; |
| 394 | else |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 395 | graph->chunk_extra_edges = data + chunk_offset; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 396 | break; |
Derrick Stolee | 118bd57 | 2019-06-18 11:14:26 -0700 | [diff] [blame] | 397 | |
| 398 | case GRAPH_CHUNKID_BASE: |
| 399 | if (graph->chunk_base_graphs) |
| 400 | chunk_repeated = 1; |
| 401 | else |
| 402 | graph->chunk_base_graphs = data + chunk_offset; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 403 | break; |
| 404 | |
| 405 | case GRAPH_CHUNKID_BLOOMINDEXES: |
| 406 | if (graph->chunk_bloom_indexes) |
| 407 | chunk_repeated = 1; |
Taylor Blau | b66d847 | 2020-09-09 11:23:10 -0400 | [diff] [blame] | 408 | else if (r->settings.commit_graph_read_changed_paths) |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 409 | graph->chunk_bloom_indexes = data + chunk_offset; |
| 410 | break; |
| 411 | |
| 412 | case GRAPH_CHUNKID_BLOOMDATA: |
| 413 | if (graph->chunk_bloom_data) |
| 414 | chunk_repeated = 1; |
Taylor Blau | b66d847 | 2020-09-09 11:23:10 -0400 | [diff] [blame] | 415 | else if (r->settings.commit_graph_read_changed_paths) { |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 416 | uint32_t hash_version; |
| 417 | graph->chunk_bloom_data = data + chunk_offset; |
| 418 | hash_version = get_be32(data + chunk_offset); |
| 419 | |
| 420 | if (hash_version != 1) |
| 421 | break; |
| 422 | |
| 423 | graph->bloom_filter_settings = xmalloc(sizeof(struct bloom_filter_settings)); |
| 424 | graph->bloom_filter_settings->hash_version = hash_version; |
| 425 | graph->bloom_filter_settings->num_hashes = get_be32(data + chunk_offset + 4); |
| 426 | graph->bloom_filter_settings->bits_per_entry = get_be32(data + chunk_offset + 8); |
Taylor Blau | 97ffa4f | 2020-09-17 09:34:42 -0400 | [diff] [blame] | 427 | graph->bloom_filter_settings->max_changed_paths = DEFAULT_BLOOM_MAX_CHANGES; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 428 | } |
| 429 | break; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | if (chunk_repeated) { |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 433 | error(_("commit-graph chunk id %08x appears multiple times"), chunk_id); |
Jonathan Tan | fbda77c | 2020-05-04 12:13:24 -0700 | [diff] [blame] | 434 | goto free_and_return; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 435 | } |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 438 | if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) { |
| 439 | init_bloom_filters(); |
| 440 | } else { |
| 441 | /* We need both the bloom chunks to exist together. Else ignore the data */ |
| 442 | graph->chunk_bloom_indexes = NULL; |
| 443 | graph->chunk_bloom_data = NULL; |
Jonathan Tan | fbda77c | 2020-05-04 12:13:24 -0700 | [diff] [blame] | 444 | FREE_AND_NULL(graph->bloom_filter_settings); |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Derrick Stolee | 118bd57 | 2019-06-18 11:14:26 -0700 | [diff] [blame] | 447 | hashcpy(graph->oid.hash, graph->data + graph->data_len - graph->hash_len); |
| 448 | |
Jonathan Tan | fbda77c | 2020-05-04 12:13:24 -0700 | [diff] [blame] | 449 | if (verify_commit_graph_lite(graph)) |
| 450 | goto free_and_return; |
Ævar Arnfjörð Bjarmason | 2ac138d | 2019-03-25 13:08:29 +0100 | [diff] [blame] | 451 | |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 452 | return graph; |
Jonathan Tan | fbda77c | 2020-05-04 12:13:24 -0700 | [diff] [blame] | 453 | |
| 454 | free_and_return: |
| 455 | free(graph->bloom_filter_settings); |
| 456 | free(graph); |
| 457 | return NULL; |
Derrick Stolee | 2a2e32b | 2018-04-10 08:56:02 -0400 | [diff] [blame] | 458 | } |
| 459 | |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 460 | static struct commit_graph *load_commit_graph_one(struct repository *r, |
| 461 | const char *graph_file, |
Taylor Blau | a7df60c | 2020-02-03 13:18:04 -0800 | [diff] [blame] | 462 | struct object_directory *odb) |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 463 | { |
| 464 | |
| 465 | struct stat st; |
| 466 | int fd; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 467 | struct commit_graph *g; |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 468 | int open_ok = open_commit_graph(graph_file, &fd, &st); |
| 469 | |
| 470 | if (!open_ok) |
| 471 | return NULL; |
| 472 | |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 473 | g = load_commit_graph_one_fd_st(r, fd, &st, odb); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 474 | |
| 475 | if (g) |
| 476 | g->filename = xstrdup(graph_file); |
| 477 | |
| 478 | return g; |
Ævar Arnfjörð Bjarmason | 61df89c | 2019-03-25 13:08:30 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 481 | static struct commit_graph *load_commit_graph_v1(struct repository *r, |
| 482 | struct object_directory *odb) |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 483 | { |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 484 | char *graph_name = get_commit_graph_filename(odb); |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 485 | struct commit_graph *g = load_commit_graph_one(r, graph_name, odb); |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 486 | free(graph_name); |
| 487 | |
| 488 | return g; |
| 489 | } |
| 490 | |
| 491 | static int add_graph_to_chain(struct commit_graph *g, |
| 492 | struct commit_graph *chain, |
| 493 | struct object_id *oids, |
| 494 | int n) |
| 495 | { |
| 496 | struct commit_graph *cur_g = chain; |
| 497 | |
Derrick Stolee | 118bd57 | 2019-06-18 11:14:26 -0700 | [diff] [blame] | 498 | if (n && !g->chunk_base_graphs) { |
| 499 | warning(_("commit-graph has no base graphs chunk")); |
| 500 | return 0; |
| 501 | } |
| 502 | |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 503 | while (n) { |
| 504 | n--; |
Derrick Stolee | 118bd57 | 2019-06-18 11:14:26 -0700 | [diff] [blame] | 505 | |
| 506 | if (!cur_g || |
| 507 | !oideq(&oids[n], &cur_g->oid) || |
| 508 | !hasheq(oids[n].hash, g->chunk_base_graphs + g->hash_len * n)) { |
| 509 | warning(_("commit-graph chain does not match")); |
| 510 | return 0; |
| 511 | } |
| 512 | |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 513 | cur_g = cur_g->base_graph; |
| 514 | } |
| 515 | |
| 516 | g->base_graph = chain; |
| 517 | |
| 518 | if (chain) |
| 519 | g->num_commits_in_base = chain->num_commits + chain->num_commits_in_base; |
| 520 | |
| 521 | return 1; |
| 522 | } |
| 523 | |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 524 | static struct commit_graph *load_commit_graph_chain(struct repository *r, |
| 525 | struct object_directory *odb) |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 526 | { |
| 527 | struct commit_graph *graph_chain = NULL; |
| 528 | struct strbuf line = STRBUF_INIT; |
| 529 | struct stat st; |
| 530 | struct object_id *oids; |
| 531 | int i = 0, valid = 1, count; |
Derrick Stolee | 663b2b1 | 2020-09-17 18:11:46 +0000 | [diff] [blame] | 532 | char *chain_name = get_commit_graph_chain_filename(odb); |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 533 | FILE *fp; |
| 534 | int stat_res; |
| 535 | |
| 536 | fp = fopen(chain_name, "r"); |
| 537 | stat_res = stat(chain_name, &st); |
| 538 | free(chain_name); |
| 539 | |
| 540 | if (!fp || |
| 541 | stat_res || |
| 542 | st.st_size <= the_hash_algo->hexsz) |
| 543 | return NULL; |
| 544 | |
| 545 | count = st.st_size / (the_hash_algo->hexsz + 1); |
| 546 | oids = xcalloc(count, sizeof(struct object_id)); |
| 547 | |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 548 | prepare_alt_odb(r); |
| 549 | |
| 550 | for (i = 0; i < count; i++) { |
| 551 | struct object_directory *odb; |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 552 | |
| 553 | if (strbuf_getline_lf(&line, fp) == EOF) |
| 554 | break; |
| 555 | |
| 556 | if (get_oid_hex(line.buf, &oids[i])) { |
| 557 | warning(_("invalid commit-graph chain: line '%s' not a hash"), |
| 558 | line.buf); |
| 559 | valid = 0; |
| 560 | break; |
| 561 | } |
| 562 | |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 563 | valid = 0; |
| 564 | for (odb = r->objects->odb; odb; odb = odb->next) { |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 565 | char *graph_name = get_split_graph_filename(odb, line.buf); |
Taylor Blau | ab14d06 | 2020-09-09 11:22:56 -0400 | [diff] [blame] | 566 | struct commit_graph *g = load_commit_graph_one(r, graph_name, odb); |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 567 | |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 568 | free(graph_name); |
| 569 | |
| 570 | if (g) { |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 571 | if (add_graph_to_chain(g, graph_chain, oids, i)) { |
| 572 | graph_chain = g; |
| 573 | valid = 1; |
| 574 | } |
| 575 | |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (!valid) { |
| 581 | warning(_("unable to find all commit-graph files")); |
| 582 | break; |
| 583 | } |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | free(oids); |
| 587 | fclose(fp); |
René Scharfe | 0aa6bce | 2019-08-07 13:15:02 +0200 | [diff] [blame] | 588 | strbuf_release(&line); |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 589 | |
| 590 | return graph_chain; |
| 591 | } |
| 592 | |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 593 | struct commit_graph *read_commit_graph_one(struct repository *r, |
| 594 | struct object_directory *odb) |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 595 | { |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 596 | struct commit_graph *g = load_commit_graph_v1(r, odb); |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 597 | |
| 598 | if (!g) |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 599 | g = load_commit_graph_chain(r, odb); |
Derrick Stolee | 5c84b33 | 2019-06-18 11:14:25 -0700 | [diff] [blame] | 600 | |
| 601 | return g; |
Jonathan Tan | 5faf357 | 2018-07-11 15:42:37 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 604 | static void prepare_commit_graph_one(struct repository *r, |
| 605 | struct object_directory *odb) |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 606 | { |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 607 | |
| 608 | if (r->objects->commit_graph) |
| 609 | return; |
| 610 | |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 611 | r->objects->commit_graph = read_commit_graph_one(r, odb); |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 612 | } |
Jonathan Tan | 5faf357 | 2018-07-11 15:42:37 -0700 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * Return 1 if commit_graph is non-NULL, and 0 otherwise. |
| 616 | * |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 617 | * On the first invocation, this function attempts to load the commit |
Jonathan Tan | 5faf357 | 2018-07-11 15:42:37 -0700 | [diff] [blame] | 618 | * graph if the_repository is configured to have one. |
| 619 | */ |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 620 | static int prepare_commit_graph(struct repository *r) |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 621 | { |
Jeff King | 263db40 | 2018-11-12 09:48:47 -0500 | [diff] [blame] | 622 | struct object_directory *odb; |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 623 | |
Jeff King | 6abada1 | 2019-09-12 10:44:45 -0400 | [diff] [blame] | 624 | /* |
| 625 | * This must come before the "already attempted?" check below, because |
| 626 | * we want to disable even an already-loaded graph file. |
| 627 | */ |
| 628 | if (r->commit_graph_disabled) |
| 629 | return 0; |
Ævar Arnfjörð Bjarmason | 43d3561 | 2019-03-25 13:08:33 +0100 | [diff] [blame] | 630 | |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 631 | if (r->objects->commit_graph_attempted) |
| 632 | return !!r->objects->commit_graph; |
| 633 | r->objects->commit_graph_attempted = 1; |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 634 | |
Derrick Stolee | 7211b9e | 2019-08-13 11:37:43 -0700 | [diff] [blame] | 635 | prepare_repo_settings(r); |
| 636 | |
Derrick Stolee | 859fdc0 | 2018-08-29 05:49:04 -0700 | [diff] [blame] | 637 | if (!git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) && |
Derrick Stolee | 7211b9e | 2019-08-13 11:37:43 -0700 | [diff] [blame] | 638 | r->settings.core_commit_graph != 1) |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 639 | /* |
| 640 | * This repository is not configured to use commit graphs, so |
| 641 | * do not load one. (But report commit_graph_attempted anyway |
| 642 | * so that commit graph loading is not attempted again for this |
| 643 | * repository.) |
| 644 | */ |
Jonathan Tan | 5faf357 | 2018-07-11 15:42:37 -0700 | [diff] [blame] | 645 | return 0; |
| 646 | |
Derrick Stolee | d653824 | 2018-08-20 18:24:27 +0000 | [diff] [blame] | 647 | if (!commit_graph_compatible(r)) |
| 648 | return 0; |
| 649 | |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 650 | prepare_alt_odb(r); |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 651 | for (odb = r->objects->odb; |
Jeff King | 263db40 | 2018-11-12 09:48:47 -0500 | [diff] [blame] | 652 | !r->objects->commit_graph && odb; |
| 653 | odb = odb->next) |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 654 | prepare_commit_graph_one(r, odb); |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 655 | return !!r->objects->commit_graph; |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 656 | } |
| 657 | |
Derrick Stolee | 6cc0174 | 2018-07-20 16:33:30 +0000 | [diff] [blame] | 658 | int generation_numbers_enabled(struct repository *r) |
| 659 | { |
| 660 | uint32_t first_generation; |
| 661 | struct commit_graph *g; |
| 662 | if (!prepare_commit_graph(r)) |
| 663 | return 0; |
| 664 | |
| 665 | g = r->objects->commit_graph; |
| 666 | |
| 667 | if (!g->num_commits) |
| 668 | return 0; |
| 669 | |
| 670 | first_generation = get_be32(g->chunk_commit_data + |
| 671 | g->hash_len + 8) >> 2; |
| 672 | |
| 673 | return !!first_generation; |
| 674 | } |
| 675 | |
Taylor Blau | 4f36440 | 2020-09-09 11:22:44 -0400 | [diff] [blame] | 676 | struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r) |
| 677 | { |
| 678 | struct commit_graph *g = r->objects->commit_graph; |
| 679 | while (g) { |
| 680 | if (g->bloom_filter_settings) |
| 681 | return g->bloom_filter_settings; |
| 682 | g = g->base_graph; |
| 683 | } |
| 684 | return NULL; |
| 685 | } |
| 686 | |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 687 | static void close_commit_graph_one(struct commit_graph *g) |
| 688 | { |
| 689 | if (!g) |
| 690 | return; |
| 691 | |
| 692 | close_commit_graph_one(g->base_graph); |
| 693 | free_commit_graph(g); |
| 694 | } |
| 695 | |
Derrick Stolee | c3a3a96 | 2019-05-17 11:41:47 -0700 | [diff] [blame] | 696 | void close_commit_graph(struct raw_object_store *o) |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 697 | { |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 698 | close_commit_graph_one(o->commit_graph); |
Derrick Stolee | c3a3a96 | 2019-05-17 11:41:47 -0700 | [diff] [blame] | 699 | o->commit_graph = NULL; |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static int bsearch_graph(struct commit_graph *g, struct object_id *oid, uint32_t *pos) |
| 703 | { |
| 704 | return bsearch_hash(oid->hash, g->chunk_oid_fanout, |
| 705 | g->chunk_oid_lookup, g->hash_len, pos); |
| 706 | } |
| 707 | |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 708 | static void load_oid_from_graph(struct commit_graph *g, |
| 709 | uint32_t pos, |
| 710 | struct object_id *oid) |
| 711 | { |
| 712 | uint32_t lex_index; |
| 713 | |
| 714 | while (g && pos < g->num_commits_in_base) |
| 715 | g = g->base_graph; |
| 716 | |
| 717 | if (!g) |
| 718 | BUG("NULL commit-graph"); |
| 719 | |
| 720 | if (pos >= g->num_commits + g->num_commits_in_base) |
| 721 | die(_("invalid commit position. commit-graph is likely corrupt")); |
| 722 | |
| 723 | lex_index = pos - g->num_commits_in_base; |
| 724 | |
| 725 | hashcpy(oid->hash, g->chunk_oid_lookup + g->hash_len * lex_index); |
| 726 | } |
| 727 | |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 728 | static struct commit_list **insert_parent_or_die(struct repository *r, |
| 729 | struct commit_graph *g, |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 730 | uint32_t pos, |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 731 | struct commit_list **pptr) |
| 732 | { |
| 733 | struct commit *c; |
| 734 | struct object_id oid; |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 735 | |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 736 | if (pos >= g->num_commits + g->num_commits_in_base) |
| 737 | die("invalid parent position %"PRIu32, pos); |
Derrick Stolee | 53614b1 | 2018-06-27 09:24:38 -0400 | [diff] [blame] | 738 | |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 739 | load_oid_from_graph(g, pos, &oid); |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 740 | c = lookup_commit(r, &oid); |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 741 | if (!c) |
Nguyễn Thái Ngọc Duy | 4f5b532 | 2018-07-21 09:49:26 +0200 | [diff] [blame] | 742 | die(_("could not find commit %s"), oid_to_hex(&oid)); |
Abhishek Kumar | c49c82a | 2020-06-17 14:44:10 +0530 | [diff] [blame] | 743 | commit_graph_data_at(c)->graph_pos = pos; |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 744 | return &commit_list_insert(c, pptr)->next; |
| 745 | } |
| 746 | |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 747 | static void fill_commit_graph_info(struct commit *item, struct commit_graph *g, uint32_t pos) |
| 748 | { |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 749 | const unsigned char *commit_data; |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 750 | struct commit_graph_data *graph_data; |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 751 | uint32_t lex_index; |
| 752 | |
| 753 | while (pos < g->num_commits_in_base) |
| 754 | g = g->base_graph; |
| 755 | |
| 756 | lex_index = pos - g->num_commits_in_base; |
| 757 | commit_data = g->chunk_commit_data + GRAPH_DATA_WIDTH * lex_index; |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 758 | |
| 759 | graph_data = commit_graph_data_at(item); |
| 760 | graph_data->graph_pos = pos; |
| 761 | graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2; |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Nguyễn Thái Ngọc Duy | a133c40 | 2019-04-16 16:33:18 +0700 | [diff] [blame] | 764 | static inline void set_commit_tree(struct commit *c, struct tree *t) |
| 765 | { |
| 766 | c->maybe_tree = t; |
| 767 | } |
| 768 | |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 769 | static int fill_commit_in_graph(struct repository *r, |
| 770 | struct commit *item, |
| 771 | struct commit_graph *g, uint32_t pos) |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 772 | { |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 773 | uint32_t edge_value; |
| 774 | uint32_t *parent_data_ptr; |
| 775 | uint64_t date_low, date_high; |
| 776 | struct commit_list **pptr; |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 777 | struct commit_graph_data *graph_data; |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 778 | const unsigned char *commit_data; |
| 779 | uint32_t lex_index; |
| 780 | |
| 781 | while (pos < g->num_commits_in_base) |
| 782 | g = g->base_graph; |
| 783 | |
| 784 | if (pos >= g->num_commits + g->num_commits_in_base) |
| 785 | die(_("invalid commit position. commit-graph is likely corrupt")); |
| 786 | |
| 787 | /* |
| 788 | * Store the "full" position, but then use the |
| 789 | * "local" position for the rest of the calculation. |
| 790 | */ |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 791 | graph_data = commit_graph_data_at(item); |
| 792 | graph_data->graph_pos = pos; |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 793 | lex_index = pos - g->num_commits_in_base; |
| 794 | |
| 795 | commit_data = g->chunk_commit_data + (g->hash_len + 16) * lex_index; |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 796 | |
| 797 | item->object.parsed = 1; |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 798 | |
Nguyễn Thái Ngọc Duy | a133c40 | 2019-04-16 16:33:18 +0700 | [diff] [blame] | 799 | set_commit_tree(item, NULL); |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 800 | |
| 801 | date_high = get_be32(commit_data + g->hash_len + 8) & 0x3; |
| 802 | date_low = get_be32(commit_data + g->hash_len + 12); |
| 803 | item->date = (timestamp_t)((date_high << 32) | date_low); |
| 804 | |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 805 | graph_data->generation = get_be32(commit_data + g->hash_len + 8) >> 2; |
Derrick Stolee | 83073cc | 2018-04-25 14:37:55 +0000 | [diff] [blame] | 806 | |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 807 | pptr = &item->parents; |
| 808 | |
| 809 | edge_value = get_be32(commit_data + g->hash_len); |
| 810 | if (edge_value == GRAPH_PARENT_NONE) |
| 811 | return 1; |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 812 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 813 | |
| 814 | edge_value = get_be32(commit_data + g->hash_len + 4); |
| 815 | if (edge_value == GRAPH_PARENT_NONE) |
| 816 | return 1; |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 817 | if (!(edge_value & GRAPH_EXTRA_EDGES_NEEDED)) { |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 818 | pptr = insert_parent_or_die(r, g, edge_value, pptr); |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 819 | return 1; |
| 820 | } |
| 821 | |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 822 | parent_data_ptr = (uint32_t*)(g->chunk_extra_edges + |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 823 | 4 * (uint64_t)(edge_value & GRAPH_EDGE_LAST_MASK)); |
| 824 | do { |
| 825 | edge_value = get_be32(parent_data_ptr); |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 826 | pptr = insert_parent_or_die(r, g, |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 827 | edge_value & GRAPH_EDGE_LAST_MASK, |
| 828 | pptr); |
| 829 | parent_data_ptr++; |
| 830 | } while (!(edge_value & GRAPH_LAST_EDGE)); |
| 831 | |
| 832 | return 1; |
| 833 | } |
| 834 | |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 835 | static int find_commit_in_graph(struct commit *item, struct commit_graph *g, uint32_t *pos) |
| 836 | { |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 837 | uint32_t graph_pos = commit_graph_position(item); |
| 838 | if (graph_pos != COMMIT_NOT_FROM_GRAPH) { |
| 839 | *pos = graph_pos; |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 840 | return 1; |
| 841 | } else { |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 842 | struct commit_graph *cur_g = g; |
| 843 | uint32_t lex_index; |
| 844 | |
| 845 | while (cur_g && !bsearch_graph(cur_g, &(item->object.oid), &lex_index)) |
| 846 | cur_g = cur_g->base_graph; |
| 847 | |
| 848 | if (cur_g) { |
| 849 | *pos = lex_index + cur_g->num_commits_in_base; |
| 850 | return 1; |
| 851 | } |
| 852 | |
| 853 | return 0; |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 857 | static int parse_commit_in_graph_one(struct repository *r, |
| 858 | struct commit_graph *g, |
| 859 | struct commit *item) |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 860 | { |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 861 | uint32_t pos; |
| 862 | |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 863 | if (item->object.parsed) |
| 864 | return 1; |
Derrick Stolee | ee79705 | 2018-06-27 09:24:29 -0400 | [diff] [blame] | 865 | |
| 866 | if (find_commit_in_graph(item, g, &pos)) |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 867 | return fill_commit_in_graph(r, item, g, pos); |
Derrick Stolee | ee79705 | 2018-06-27 09:24:29 -0400 | [diff] [blame] | 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 872 | int parse_commit_in_graph(struct repository *r, struct commit *item) |
Derrick Stolee | ee79705 | 2018-06-27 09:24:29 -0400 | [diff] [blame] | 873 | { |
Derrick Stolee | 7b671f8 | 2020-06-23 17:47:01 +0000 | [diff] [blame] | 874 | static int checked_env = 0; |
| 875 | |
| 876 | if (!checked_env && |
| 877 | git_env_bool(GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE, 0)) |
| 878 | die("dying as requested by the '%s' variable on commit-graph parse!", |
| 879 | GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE); |
| 880 | checked_env = 1; |
| 881 | |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 882 | if (!prepare_commit_graph(r)) |
Derrick Stolee | ee79705 | 2018-06-27 09:24:29 -0400 | [diff] [blame] | 883 | return 0; |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 884 | return parse_commit_in_graph_one(r, r->objects->commit_graph, item); |
Derrick Stolee | 177722b | 2018-04-10 08:56:05 -0400 | [diff] [blame] | 885 | } |
| 886 | |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 887 | void load_commit_graph_info(struct repository *r, struct commit *item) |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 888 | { |
| 889 | uint32_t pos; |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 890 | if (!prepare_commit_graph(r)) |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 891 | return; |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 892 | if (find_commit_in_graph(item, r->objects->commit_graph, &pos)) |
| 893 | fill_commit_graph_info(item, r->objects->commit_graph, pos); |
Derrick Stolee | e2838d8 | 2018-05-01 12:47:13 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 896 | static struct tree *load_tree_for_commit(struct repository *r, |
| 897 | struct commit_graph *g, |
| 898 | struct commit *c) |
Derrick Stolee | 7b8a21d | 2018-04-06 19:09:46 +0000 | [diff] [blame] | 899 | { |
| 900 | struct object_id oid; |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 901 | const unsigned char *commit_data; |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 902 | uint32_t graph_pos = commit_graph_position(c); |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 903 | |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 904 | while (graph_pos < g->num_commits_in_base) |
Derrick Stolee | d4f4d60 | 2019-06-18 11:14:24 -0700 | [diff] [blame] | 905 | g = g->base_graph; |
| 906 | |
| 907 | commit_data = g->chunk_commit_data + |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 908 | GRAPH_DATA_WIDTH * (graph_pos - g->num_commits_in_base); |
Derrick Stolee | 7b8a21d | 2018-04-06 19:09:46 +0000 | [diff] [blame] | 909 | |
| 910 | hashcpy(oid.hash, commit_data); |
Nguyễn Thái Ngọc Duy | a133c40 | 2019-04-16 16:33:18 +0700 | [diff] [blame] | 911 | set_commit_tree(c, lookup_tree(r, &oid)); |
Derrick Stolee | 7b8a21d | 2018-04-06 19:09:46 +0000 | [diff] [blame] | 912 | |
| 913 | return c->maybe_tree; |
| 914 | } |
| 915 | |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 916 | static struct tree *get_commit_tree_in_graph_one(struct repository *r, |
| 917 | struct commit_graph *g, |
Derrick Stolee | 0cbef8f | 2018-06-27 09:24:31 -0400 | [diff] [blame] | 918 | const struct commit *c) |
Derrick Stolee | 7b8a21d | 2018-04-06 19:09:46 +0000 | [diff] [blame] | 919 | { |
| 920 | if (c->maybe_tree) |
| 921 | return c->maybe_tree; |
Abhishek Kumar | c49c82a | 2020-06-17 14:44:10 +0530 | [diff] [blame] | 922 | if (commit_graph_position(c) == COMMIT_NOT_FROM_GRAPH) |
Derrick Stolee | 0cbef8f | 2018-06-27 09:24:31 -0400 | [diff] [blame] | 923 | BUG("get_commit_tree_in_graph_one called from non-commit-graph commit"); |
Derrick Stolee | 7b8a21d | 2018-04-06 19:09:46 +0000 | [diff] [blame] | 924 | |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 925 | return load_tree_for_commit(r, g, (struct commit *)c); |
Derrick Stolee | 0cbef8f | 2018-06-27 09:24:31 -0400 | [diff] [blame] | 926 | } |
| 927 | |
Jonathan Tan | dade47c | 2018-07-11 15:42:42 -0700 | [diff] [blame] | 928 | struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit *c) |
Derrick Stolee | 0cbef8f | 2018-06-27 09:24:31 -0400 | [diff] [blame] | 929 | { |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 930 | return get_commit_tree_in_graph_one(r, r->objects->commit_graph, c); |
Derrick Stolee | 7b8a21d | 2018-04-06 19:09:46 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 933 | struct packed_commit_list { |
| 934 | struct commit **list; |
Jeff King | 3361390 | 2020-12-07 14:11:08 -0500 | [diff] [blame] | 935 | size_t nr; |
| 936 | size_t alloc; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 937 | }; |
| 938 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 939 | struct write_commit_graph_context { |
| 940 | struct repository *r; |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 941 | struct object_directory *odb; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 942 | char *graph_name; |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 943 | struct oid_array oids; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 944 | struct packed_commit_list commits; |
| 945 | int num_extra_edges; |
| 946 | unsigned long approx_nr_objects; |
| 947 | struct progress *progress; |
| 948 | int progress_done; |
| 949 | uint64_t progress_cnt; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 950 | |
| 951 | char *base_graph_name; |
| 952 | int num_commit_graphs_before; |
| 953 | int num_commit_graphs_after; |
| 954 | char **commit_graph_filenames_before; |
| 955 | char **commit_graph_filenames_after; |
| 956 | char **commit_graph_hash_after; |
| 957 | uint32_t new_num_commits_in_base; |
| 958 | struct commit_graph *new_base_graph; |
| 959 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 960 | unsigned append:1, |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 961 | report_progress:1, |
SZEDER Gábor | 7c5c9b9 | 2019-08-05 10:02:40 +0200 | [diff] [blame] | 962 | split:1, |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 963 | changed_paths:1, |
| 964 | order_by_pack:1; |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 965 | |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 966 | const struct commit_graph_opts *opts; |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 967 | size_t total_bloom_filter_data_size; |
Derrick Stolee | 98037f2 | 2020-06-23 17:47:00 +0000 | [diff] [blame] | 968 | const struct bloom_filter_settings *bloom_settings; |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 969 | |
| 970 | int count_bloom_filter_computed; |
| 971 | int count_bloom_filter_not_computed; |
Taylor Blau | 59f0d50 | 2020-09-17 22:59:44 -0400 | [diff] [blame] | 972 | int count_bloom_filter_trunc_empty; |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 973 | int count_bloom_filter_trunc_large; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 974 | }; |
| 975 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 976 | static int write_graph_chunk_fanout(struct hashfile *f, |
| 977 | struct write_commit_graph_context *ctx) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 978 | { |
| 979 | int i, count = 0; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 980 | struct commit **list = ctx->commits.list; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 981 | |
| 982 | /* |
| 983 | * Write the first-level table (the list is sorted, |
| 984 | * but we use a 256-entry lookup to be able to avoid |
| 985 | * having to do eight extra binary search iterations). |
| 986 | */ |
| 987 | for (i = 0; i < 256; i++) { |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 988 | while (count < ctx->commits.nr) { |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 989 | if ((*list)->object.oid.hash[0] != i) |
| 990 | break; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 991 | display_progress(ctx->progress, ++ctx->progress_cnt); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 992 | count++; |
| 993 | list++; |
| 994 | } |
| 995 | |
| 996 | hashwrite_be32(f, count); |
| 997 | } |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 998 | |
| 999 | return 0; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1000 | } |
| 1001 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1002 | static int write_graph_chunk_oids(struct hashfile *f, |
| 1003 | struct write_commit_graph_context *ctx) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1004 | { |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1005 | struct commit **list = ctx->commits.list; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1006 | int count; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1007 | for (count = 0; count < ctx->commits.nr; count++, list++) { |
| 1008 | display_progress(ctx->progress, ++ctx->progress_cnt); |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1009 | hashwrite(f, (*list)->object.oid.hash, the_hash_algo->rawsz); |
Ævar Arnfjörð Bjarmason | 53035c4 | 2019-01-19 21:21:15 +0100 | [diff] [blame] | 1010 | } |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1011 | |
| 1012 | return 0; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | static const unsigned char *commit_to_sha1(size_t index, void *table) |
| 1016 | { |
| 1017 | struct commit **commits = table; |
| 1018 | return commits[index]->object.oid.hash; |
| 1019 | } |
| 1020 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1021 | static int write_graph_chunk_data(struct hashfile *f, |
| 1022 | struct write_commit_graph_context *ctx) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1023 | { |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1024 | struct commit **list = ctx->commits.list; |
| 1025 | struct commit **last = ctx->commits.list + ctx->commits.nr; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1026 | uint32_t num_extra_edges = 0; |
| 1027 | |
| 1028 | while (list < last) { |
| 1029 | struct commit_list *parent; |
Taylor Blau | 806278d | 2019-09-05 18:04:57 -0400 | [diff] [blame] | 1030 | struct object_id *tree; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1031 | int edge_value; |
| 1032 | uint32_t packedDate[2]; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1033 | display_progress(ctx->progress, ++ctx->progress_cnt); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1034 | |
Taylor Blau | 16749b8 | 2019-09-05 18:04:55 -0400 | [diff] [blame] | 1035 | if (parse_commit_no_graph(*list)) |
| 1036 | die(_("unable to parse commit %s"), |
| 1037 | oid_to_hex(&(*list)->object.oid)); |
Taylor Blau | 806278d | 2019-09-05 18:04:57 -0400 | [diff] [blame] | 1038 | tree = get_commit_tree_oid(*list); |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1039 | hashwrite(f, tree->hash, the_hash_algo->rawsz); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1040 | |
| 1041 | parent = (*list)->parents; |
| 1042 | |
| 1043 | if (!parent) |
| 1044 | edge_value = GRAPH_PARENT_NONE; |
| 1045 | else { |
| 1046 | edge_value = sha1_pos(parent->item->object.oid.hash, |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1047 | ctx->commits.list, |
| 1048 | ctx->commits.nr, |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1049 | commit_to_sha1); |
| 1050 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1051 | if (edge_value >= 0) |
| 1052 | edge_value += ctx->new_num_commits_in_base; |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1053 | else if (ctx->new_base_graph) { |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1054 | uint32_t pos; |
| 1055 | if (find_commit_in_graph(parent->item, |
| 1056 | ctx->new_base_graph, |
| 1057 | &pos)) |
| 1058 | edge_value = pos; |
| 1059 | } |
| 1060 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1061 | if (edge_value < 0) |
Derrick Stolee | cce99cd | 2018-12-19 12:14:07 -0800 | [diff] [blame] | 1062 | BUG("missing parent %s for commit %s", |
| 1063 | oid_to_hex(&parent->item->object.oid), |
| 1064 | oid_to_hex(&(*list)->object.oid)); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | hashwrite_be32(f, edge_value); |
| 1068 | |
| 1069 | if (parent) |
| 1070 | parent = parent->next; |
| 1071 | |
| 1072 | if (!parent) |
| 1073 | edge_value = GRAPH_PARENT_NONE; |
| 1074 | else if (parent->next) |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 1075 | edge_value = GRAPH_EXTRA_EDGES_NEEDED | num_extra_edges; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1076 | else { |
| 1077 | edge_value = sha1_pos(parent->item->object.oid.hash, |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1078 | ctx->commits.list, |
| 1079 | ctx->commits.nr, |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1080 | commit_to_sha1); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1081 | |
| 1082 | if (edge_value >= 0) |
| 1083 | edge_value += ctx->new_num_commits_in_base; |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1084 | else if (ctx->new_base_graph) { |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1085 | uint32_t pos; |
| 1086 | if (find_commit_in_graph(parent->item, |
| 1087 | ctx->new_base_graph, |
| 1088 | &pos)) |
| 1089 | edge_value = pos; |
| 1090 | } |
| 1091 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1092 | if (edge_value < 0) |
Derrick Stolee | cce99cd | 2018-12-19 12:14:07 -0800 | [diff] [blame] | 1093 | BUG("missing parent %s for commit %s", |
| 1094 | oid_to_hex(&parent->item->object.oid), |
| 1095 | oid_to_hex(&(*list)->object.oid)); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | hashwrite_be32(f, edge_value); |
| 1099 | |
SZEDER Gábor | 5af7417 | 2019-01-19 21:21:13 +0100 | [diff] [blame] | 1100 | if (edge_value & GRAPH_EXTRA_EDGES_NEEDED) { |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1101 | do { |
| 1102 | num_extra_edges++; |
| 1103 | parent = parent->next; |
| 1104 | } while (parent); |
| 1105 | } |
| 1106 | |
| 1107 | if (sizeof((*list)->date) > 4) |
| 1108 | packedDate[0] = htonl(((*list)->date >> 32) & 0x3); |
| 1109 | else |
| 1110 | packedDate[0] = 0; |
| 1111 | |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 1112 | packedDate[0] |= htonl(commit_graph_data_at(*list)->generation << 2); |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1113 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1114 | packedDate[1] = htonl((*list)->date); |
| 1115 | hashwrite(f, packedDate, 8); |
| 1116 | |
| 1117 | list++; |
| 1118 | } |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1119 | |
| 1120 | return 0; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1121 | } |
| 1122 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1123 | static int write_graph_chunk_extra_edges(struct hashfile *f, |
| 1124 | struct write_commit_graph_context *ctx) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1125 | { |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1126 | struct commit **list = ctx->commits.list; |
| 1127 | struct commit **last = ctx->commits.list + ctx->commits.nr; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1128 | struct commit_list *parent; |
| 1129 | |
| 1130 | while (list < last) { |
| 1131 | int num_parents = 0; |
Ævar Arnfjörð Bjarmason | 53035c4 | 2019-01-19 21:21:15 +0100 | [diff] [blame] | 1132 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1133 | display_progress(ctx->progress, ++ctx->progress_cnt); |
Ævar Arnfjörð Bjarmason | 53035c4 | 2019-01-19 21:21:15 +0100 | [diff] [blame] | 1134 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1135 | for (parent = (*list)->parents; num_parents < 3 && parent; |
| 1136 | parent = parent->next) |
| 1137 | num_parents++; |
| 1138 | |
| 1139 | if (num_parents <= 2) { |
| 1140 | list++; |
| 1141 | continue; |
| 1142 | } |
| 1143 | |
| 1144 | /* Since num_parents > 2, this initializer is safe. */ |
| 1145 | for (parent = (*list)->parents->next; parent; parent = parent->next) { |
| 1146 | int edge_value = sha1_pos(parent->item->object.oid.hash, |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1147 | ctx->commits.list, |
| 1148 | ctx->commits.nr, |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1149 | commit_to_sha1); |
| 1150 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1151 | if (edge_value >= 0) |
| 1152 | edge_value += ctx->new_num_commits_in_base; |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1153 | else if (ctx->new_base_graph) { |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1154 | uint32_t pos; |
| 1155 | if (find_commit_in_graph(parent->item, |
| 1156 | ctx->new_base_graph, |
| 1157 | &pos)) |
| 1158 | edge_value = pos; |
| 1159 | } |
| 1160 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1161 | if (edge_value < 0) |
Derrick Stolee | cce99cd | 2018-12-19 12:14:07 -0800 | [diff] [blame] | 1162 | BUG("missing parent %s for commit %s", |
| 1163 | oid_to_hex(&parent->item->object.oid), |
| 1164 | oid_to_hex(&(*list)->object.oid)); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1165 | else if (!parent->next) |
| 1166 | edge_value |= GRAPH_LAST_EDGE; |
| 1167 | |
| 1168 | hashwrite_be32(f, edge_value); |
| 1169 | } |
| 1170 | |
| 1171 | list++; |
| 1172 | } |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1173 | |
| 1174 | return 0; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1175 | } |
| 1176 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1177 | static int write_graph_chunk_bloom_indexes(struct hashfile *f, |
| 1178 | struct write_commit_graph_context *ctx) |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1179 | { |
| 1180 | struct commit **list = ctx->commits.list; |
| 1181 | struct commit **last = ctx->commits.list + ctx->commits.nr; |
| 1182 | uint32_t cur_pos = 0; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1183 | |
| 1184 | while (list < last) { |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1185 | struct bloom_filter *filter = get_bloom_filter(ctx->r, *list); |
Derrick Stolee | 9491974 | 2020-07-01 13:27:23 +0000 | [diff] [blame] | 1186 | size_t len = filter ? filter->len : 0; |
| 1187 | cur_pos += len; |
SZEDER Gábor | 150cd3b | 2020-07-09 19:00:03 +0200 | [diff] [blame] | 1188 | display_progress(ctx->progress, ++ctx->progress_cnt); |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1189 | hashwrite_be32(f, cur_pos); |
| 1190 | list++; |
| 1191 | } |
| 1192 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1193 | return 0; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Derrick Stolee | 0087a87 | 2020-07-01 13:27:24 +0000 | [diff] [blame] | 1196 | static void trace2_bloom_filter_settings(struct write_commit_graph_context *ctx) |
| 1197 | { |
| 1198 | struct json_writer jw = JSON_WRITER_INIT; |
| 1199 | |
| 1200 | jw_object_begin(&jw, 0); |
| 1201 | jw_object_intmax(&jw, "hash_version", ctx->bloom_settings->hash_version); |
| 1202 | jw_object_intmax(&jw, "num_hashes", ctx->bloom_settings->num_hashes); |
| 1203 | jw_object_intmax(&jw, "bits_per_entry", ctx->bloom_settings->bits_per_entry); |
Taylor Blau | 97ffa4f | 2020-09-17 09:34:42 -0400 | [diff] [blame] | 1204 | jw_object_intmax(&jw, "max_changed_paths", ctx->bloom_settings->max_changed_paths); |
Derrick Stolee | 0087a87 | 2020-07-01 13:27:24 +0000 | [diff] [blame] | 1205 | jw_end(&jw); |
| 1206 | |
| 1207 | trace2_data_json("bloom", ctx->r, "settings", &jw); |
| 1208 | |
| 1209 | jw_release(&jw); |
| 1210 | } |
| 1211 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1212 | static int write_graph_chunk_bloom_data(struct hashfile *f, |
| 1213 | struct write_commit_graph_context *ctx) |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1214 | { |
| 1215 | struct commit **list = ctx->commits.list; |
| 1216 | struct commit **last = ctx->commits.list + ctx->commits.nr; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1217 | |
Derrick Stolee | 0087a87 | 2020-07-01 13:27:24 +0000 | [diff] [blame] | 1218 | trace2_bloom_filter_settings(ctx); |
| 1219 | |
Derrick Stolee | 98037f2 | 2020-06-23 17:47:00 +0000 | [diff] [blame] | 1220 | hashwrite_be32(f, ctx->bloom_settings->hash_version); |
| 1221 | hashwrite_be32(f, ctx->bloom_settings->num_hashes); |
| 1222 | hashwrite_be32(f, ctx->bloom_settings->bits_per_entry); |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1223 | |
| 1224 | while (list < last) { |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1225 | struct bloom_filter *filter = get_bloom_filter(ctx->r, *list); |
Derrick Stolee | 9491974 | 2020-07-01 13:27:23 +0000 | [diff] [blame] | 1226 | size_t len = filter ? filter->len : 0; |
Derrick Stolee | 9491974 | 2020-07-01 13:27:23 +0000 | [diff] [blame] | 1227 | |
SZEDER Gábor | 150cd3b | 2020-07-09 19:00:03 +0200 | [diff] [blame] | 1228 | display_progress(ctx->progress, ++ctx->progress_cnt); |
Derrick Stolee | 9491974 | 2020-07-01 13:27:23 +0000 | [diff] [blame] | 1229 | if (len) |
| 1230 | hashwrite(f, filter->data, len * sizeof(unsigned char)); |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1231 | list++; |
| 1232 | } |
| 1233 | |
SZEDER Gábor | 9bab081 | 2020-07-01 13:27:25 +0000 | [diff] [blame] | 1234 | return 0; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1237 | static int add_packed_commits(const struct object_id *oid, |
| 1238 | struct packed_git *pack, |
| 1239 | uint32_t pos, |
| 1240 | void *data) |
| 1241 | { |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1242 | struct write_commit_graph_context *ctx = (struct write_commit_graph_context*)data; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1243 | enum object_type type; |
| 1244 | off_t offset = nth_packed_object_offset(pack, pos); |
| 1245 | struct object_info oi = OBJECT_INFO_INIT; |
| 1246 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1247 | if (ctx->progress) |
| 1248 | display_progress(ctx->progress, ++ctx->progress_done); |
Ævar Arnfjörð Bjarmason | 7b0f229 | 2018-09-17 15:33:35 +0000 | [diff] [blame] | 1249 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1250 | oi.typep = &type; |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1251 | if (packed_object_info(ctx->r, pack, offset, &oi) < 0) |
Nguyễn Thái Ngọc Duy | 4f5b532 | 2018-07-21 09:49:26 +0200 | [diff] [blame] | 1252 | die(_("unable to get type of object %s"), oid_to_hex(oid)); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1253 | |
| 1254 | if (type != OBJ_COMMIT) |
| 1255 | return 0; |
| 1256 | |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1257 | oid_array_append(&ctx->oids, oid); |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 1258 | set_commit_pos(ctx->r, oid); |
| 1259 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1260 | return 0; |
| 1261 | } |
| 1262 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1263 | static void add_missing_parents(struct write_commit_graph_context *ctx, struct commit *commit) |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1264 | { |
| 1265 | struct commit_list *parent; |
| 1266 | for (parent = commit->parents; parent; parent = parent->next) { |
Derrick Stolee | cb99a34 | 2019-10-24 13:40:42 +0000 | [diff] [blame] | 1267 | if (!(parent->item->object.flags & REACHABLE)) { |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1268 | oid_array_append(&ctx->oids, &parent->item->object.oid); |
Derrick Stolee | cb99a34 | 2019-10-24 13:40:42 +0000 | [diff] [blame] | 1269 | parent->item->object.flags |= REACHABLE; |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1274 | static void close_reachable(struct write_commit_graph_context *ctx) |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1275 | { |
Ævar Arnfjörð Bjarmason | 49bbc57 | 2019-01-19 21:21:21 +0100 | [diff] [blame] | 1276 | int i; |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1277 | struct commit *commit; |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 1278 | enum commit_graph_split_flags flags = ctx->opts ? |
| 1279 | ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1280 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1281 | if (ctx->report_progress) |
| 1282 | ctx->progress = start_delayed_progress( |
| 1283 | _("Loading known commits in commit graph"), |
| 1284 | ctx->oids.nr); |
| 1285 | for (i = 0; i < ctx->oids.nr; i++) { |
| 1286 | display_progress(ctx->progress, i + 1); |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1287 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1288 | if (commit) |
Derrick Stolee | cb99a34 | 2019-10-24 13:40:42 +0000 | [diff] [blame] | 1289 | commit->object.flags |= REACHABLE; |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1290 | } |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1291 | stop_progress(&ctx->progress); |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1292 | |
| 1293 | /* |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1294 | * As this loop runs, ctx->oids.nr may grow, but not more |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1295 | * than the number of missing commits in the reachable |
| 1296 | * closure. |
| 1297 | */ |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1298 | if (ctx->report_progress) |
| 1299 | ctx->progress = start_delayed_progress( |
| 1300 | _("Expanding reachable commits in commit graph"), |
SZEDER Gábor | 67fa6aa | 2019-09-07 01:01:33 -0400 | [diff] [blame] | 1301 | 0); |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1302 | for (i = 0; i < ctx->oids.nr; i++) { |
| 1303 | display_progress(ctx->progress, i + 1); |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1304 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1305 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1306 | if (!commit) |
| 1307 | continue; |
| 1308 | if (ctx->split) { |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1309 | if ((!parse_commit(commit) && |
Abhishek Kumar | c49c82a | 2020-06-17 14:44:10 +0530 | [diff] [blame] | 1310 | commit_graph_position(commit) == COMMIT_NOT_FROM_GRAPH) || |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1311 | flags == COMMIT_GRAPH_SPLIT_REPLACE) |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1312 | add_missing_parents(ctx, commit); |
| 1313 | } else if (!parse_commit_no_graph(commit)) |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1314 | add_missing_parents(ctx, commit); |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1315 | } |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1316 | stop_progress(&ctx->progress); |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1317 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1318 | if (ctx->report_progress) |
| 1319 | ctx->progress = start_delayed_progress( |
| 1320 | _("Clearing commit marks in commit graph"), |
| 1321 | ctx->oids.nr); |
| 1322 | for (i = 0; i < ctx->oids.nr; i++) { |
| 1323 | display_progress(ctx->progress, i + 1); |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1324 | commit = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1325 | |
| 1326 | if (commit) |
Derrick Stolee | cb99a34 | 2019-10-24 13:40:42 +0000 | [diff] [blame] | 1327 | commit->object.flags &= ~REACHABLE; |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1328 | } |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1329 | stop_progress(&ctx->progress); |
Derrick Stolee | 4f2542b | 2018-04-10 08:56:04 -0400 | [diff] [blame] | 1330 | } |
| 1331 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1332 | static void compute_generation_numbers(struct write_commit_graph_context *ctx) |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1333 | { |
| 1334 | int i; |
| 1335 | struct commit_list *list = NULL; |
| 1336 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1337 | if (ctx->report_progress) |
Derrick Stolee | ecc0869 | 2019-11-25 21:28:23 +0000 | [diff] [blame] | 1338 | ctx->progress = start_delayed_progress( |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1339 | _("Computing commit graph generation numbers"), |
| 1340 | ctx->commits.nr); |
| 1341 | for (i = 0; i < ctx->commits.nr; i++) { |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 1342 | uint32_t generation = commit_graph_data_at(ctx->commits.list[i])->generation; |
| 1343 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1344 | display_progress(ctx->progress, i + 1); |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 1345 | if (generation != GENERATION_NUMBER_INFINITY && |
| 1346 | generation != GENERATION_NUMBER_ZERO) |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1347 | continue; |
| 1348 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1349 | commit_list_insert(ctx->commits.list[i], &list); |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1350 | while (list) { |
| 1351 | struct commit *current = list->item; |
| 1352 | struct commit_list *parent; |
| 1353 | int all_parents_computed = 1; |
| 1354 | uint32_t max_generation = 0; |
| 1355 | |
| 1356 | for (parent = current->parents; parent; parent = parent->next) { |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 1357 | generation = commit_graph_data_at(parent->item)->generation; |
| 1358 | |
| 1359 | if (generation == GENERATION_NUMBER_INFINITY || |
| 1360 | generation == GENERATION_NUMBER_ZERO) { |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1361 | all_parents_computed = 0; |
| 1362 | commit_list_insert(parent->item, &list); |
| 1363 | break; |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 1364 | } else if (generation > max_generation) { |
| 1365 | max_generation = generation; |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | if (all_parents_computed) { |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 1370 | struct commit_graph_data *data = commit_graph_data_at(current); |
| 1371 | |
| 1372 | data->generation = max_generation + 1; |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1373 | pop_commit(&list); |
| 1374 | |
Abhishek Kumar | 4844812 | 2020-06-17 14:44:09 +0530 | [diff] [blame] | 1375 | if (data->generation > GENERATION_NUMBER_MAX) |
| 1376 | data->generation = GENERATION_NUMBER_MAX; |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | } |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 1380 | stop_progress(&ctx->progress); |
Derrick Stolee | 3258c66 | 2018-05-01 12:47:09 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1383 | static void trace2_bloom_filter_write_statistics(struct write_commit_graph_context *ctx) |
| 1384 | { |
| 1385 | trace2_data_intmax("commit-graph", ctx->r, "filter-computed", |
| 1386 | ctx->count_bloom_filter_computed); |
| 1387 | trace2_data_intmax("commit-graph", ctx->r, "filter-not-computed", |
| 1388 | ctx->count_bloom_filter_not_computed); |
Taylor Blau | 59f0d50 | 2020-09-17 22:59:44 -0400 | [diff] [blame] | 1389 | trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-empty", |
| 1390 | ctx->count_bloom_filter_trunc_empty); |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1391 | trace2_data_intmax("commit-graph", ctx->r, "filter-trunc-large", |
| 1392 | ctx->count_bloom_filter_trunc_large); |
| 1393 | } |
| 1394 | |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 1395 | static void compute_bloom_filters(struct write_commit_graph_context *ctx) |
| 1396 | { |
| 1397 | int i; |
| 1398 | struct progress *progress = NULL; |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 1399 | struct commit **sorted_commits; |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 1400 | int max_new_filters; |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 1401 | |
| 1402 | init_bloom_filters(); |
| 1403 | |
| 1404 | if (ctx->report_progress) |
| 1405 | progress = start_delayed_progress( |
| 1406 | _("Computing commit changed paths Bloom filters"), |
| 1407 | ctx->commits.nr); |
| 1408 | |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 1409 | ALLOC_ARRAY(sorted_commits, ctx->commits.nr); |
| 1410 | COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr); |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (ctx->order_by_pack) |
| 1413 | QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp); |
| 1414 | else |
| 1415 | QSORT(sorted_commits, ctx->commits.nr, commit_gen_cmp); |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 1416 | |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 1417 | max_new_filters = ctx->opts && ctx->opts->max_new_filters >= 0 ? |
| 1418 | ctx->opts->max_new_filters : ctx->commits.nr; |
| 1419 | |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 1420 | for (i = 0; i < ctx->commits.nr; i++) { |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1421 | enum bloom_filter_computed computed = 0; |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 1422 | struct commit *c = sorted_commits[i]; |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1423 | struct bloom_filter *filter = get_or_compute_bloom_filter( |
| 1424 | ctx->r, |
| 1425 | c, |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 1426 | ctx->count_bloom_filter_computed < max_new_filters, |
Taylor Blau | 9a7a9ed | 2020-09-16 14:07:46 -0400 | [diff] [blame] | 1427 | ctx->bloom_settings, |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1428 | &computed); |
| 1429 | if (computed & BLOOM_COMPUTED) { |
| 1430 | ctx->count_bloom_filter_computed++; |
Taylor Blau | 59f0d50 | 2020-09-17 22:59:44 -0400 | [diff] [blame] | 1431 | if (computed & BLOOM_TRUNC_EMPTY) |
| 1432 | ctx->count_bloom_filter_trunc_empty++; |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1433 | if (computed & BLOOM_TRUNC_LARGE) |
| 1434 | ctx->count_bloom_filter_trunc_large++; |
| 1435 | } else if (computed & BLOOM_NOT_COMPUTED) |
| 1436 | ctx->count_bloom_filter_not_computed++; |
Taylor Blau | 809e032 | 2020-09-18 09:27:27 -0400 | [diff] [blame] | 1437 | ctx->total_bloom_filter_data_size += filter |
| 1438 | ? sizeof(unsigned char) * filter->len : 0; |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 1439 | display_progress(progress, i + 1); |
| 1440 | } |
| 1441 | |
Taylor Blau | 312cff5 | 2020-09-16 14:07:32 -0400 | [diff] [blame] | 1442 | if (trace2_is_enabled()) |
| 1443 | trace2_bloom_filter_write_statistics(ctx); |
| 1444 | |
Jeff King | d21ee7d | 2020-03-30 00:31:29 +0000 | [diff] [blame] | 1445 | free(sorted_commits); |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 1446 | stop_progress(&progress); |
| 1447 | } |
| 1448 | |
Taylor Blau | 1fe1084 | 2020-05-04 19:13:35 -0600 | [diff] [blame] | 1449 | struct refs_cb_data { |
| 1450 | struct oidset *commits; |
Taylor Blau | d335ce8 | 2020-05-13 15:59:33 -0600 | [diff] [blame] | 1451 | struct progress *progress; |
Taylor Blau | 1fe1084 | 2020-05-04 19:13:35 -0600 | [diff] [blame] | 1452 | }; |
| 1453 | |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1454 | static int add_ref_to_set(const char *refname, |
| 1455 | const struct object_id *oid, |
| 1456 | int flags, void *cb_data) |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 1457 | { |
Taylor Blau | 630cd51 | 2020-05-13 15:59:37 -0600 | [diff] [blame] | 1458 | struct object_id peeled; |
Taylor Blau | 1fe1084 | 2020-05-04 19:13:35 -0600 | [diff] [blame] | 1459 | struct refs_cb_data *data = (struct refs_cb_data *)cb_data; |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 1460 | |
Taylor Blau | 630cd51 | 2020-05-13 15:59:37 -0600 | [diff] [blame] | 1461 | if (!peel_ref(refname, &peeled)) |
| 1462 | oid = &peeled; |
| 1463 | if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT) |
| 1464 | oidset_insert(data->commits, oid); |
Taylor Blau | d335ce8 | 2020-05-13 15:59:33 -0600 | [diff] [blame] | 1465 | |
| 1466 | display_progress(data->progress, oidset_size(data->commits)); |
| 1467 | |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 1468 | return 0; |
| 1469 | } |
| 1470 | |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 1471 | int write_commit_graph_reachable(struct object_directory *odb, |
SZEDER Gábor | 39d8831 | 2019-08-05 10:02:39 +0200 | [diff] [blame] | 1472 | enum commit_graph_write_flags flags, |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 1473 | const struct commit_graph_opts *opts) |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 1474 | { |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1475 | struct oidset commits = OIDSET_INIT; |
Taylor Blau | 1fe1084 | 2020-05-04 19:13:35 -0600 | [diff] [blame] | 1476 | struct refs_cb_data data; |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 1477 | int result; |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 1478 | |
Taylor Blau | 1fe1084 | 2020-05-04 19:13:35 -0600 | [diff] [blame] | 1479 | memset(&data, 0, sizeof(data)); |
| 1480 | data.commits = &commits; |
Taylor Blau | d335ce8 | 2020-05-13 15:59:33 -0600 | [diff] [blame] | 1481 | if (flags & COMMIT_GRAPH_WRITE_PROGRESS) |
| 1482 | data.progress = start_delayed_progress( |
| 1483 | _("Collecting referenced commits"), 0); |
Taylor Blau | 1fe1084 | 2020-05-04 19:13:35 -0600 | [diff] [blame] | 1484 | |
| 1485 | for_each_ref(add_ref_to_set, &data); |
SZEDER Gábor | 6f9d5f2 | 2020-07-09 18:54:32 +0200 | [diff] [blame] | 1486 | |
| 1487 | stop_progress(&data.progress); |
| 1488 | |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1489 | result = write_commit_graph(odb, NULL, &commits, |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 1490 | flags, opts); |
Derrick Stolee | f4dbdfc | 2018-10-03 10:12:15 -0700 | [diff] [blame] | 1491 | |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1492 | oidset_clear(&commits); |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 1493 | return result; |
Derrick Stolee | 59fb877 | 2018-06-27 09:24:45 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1496 | static int fill_oids_from_packs(struct write_commit_graph_context *ctx, |
| 1497 | struct string_list *pack_indexes) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1498 | { |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1499 | uint32_t i; |
Ævar Arnfjörð Bjarmason | 2894473 | 2019-01-19 21:21:16 +0100 | [diff] [blame] | 1500 | struct strbuf progress_title = STRBUF_INIT; |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1501 | struct strbuf packname = STRBUF_INIT; |
| 1502 | int dirlen; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1503 | |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 1504 | strbuf_addf(&packname, "%s/pack/", ctx->odb->path); |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1505 | dirlen = packname.len; |
| 1506 | if (ctx->report_progress) { |
| 1507 | strbuf_addf(&progress_title, |
| 1508 | Q_("Finding commits for commit graph in %d pack", |
| 1509 | "Finding commits for commit graph in %d packs", |
| 1510 | pack_indexes->nr), |
| 1511 | pack_indexes->nr); |
| 1512 | ctx->progress = start_delayed_progress(progress_title.buf, 0); |
| 1513 | ctx->progress_done = 0; |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 1514 | } |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1515 | for (i = 0; i < pack_indexes->nr; i++) { |
| 1516 | struct packed_git *p; |
| 1517 | strbuf_setlen(&packname, dirlen); |
| 1518 | strbuf_addstr(&packname, pack_indexes->items[i].string); |
| 1519 | p = add_packed_git(packname.buf, packname.len, 1); |
| 1520 | if (!p) { |
| 1521 | error(_("error adding pack %s"), packname.buf); |
| 1522 | return -1; |
Derrick Stolee | 7547b95 | 2018-04-10 08:56:08 -0400 | [diff] [blame] | 1523 | } |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1524 | if (open_pack_index(p)) { |
| 1525 | error(_("error opening index for %s"), packname.buf); |
| 1526 | return -1; |
Ævar Arnfjörð Bjarmason | 7b0f229 | 2018-09-17 15:33:35 +0000 | [diff] [blame] | 1527 | } |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1528 | for_each_object_in_pack(p, add_packed_commits, ctx, |
| 1529 | FOR_EACH_OBJECT_PACK_ORDER); |
| 1530 | close_pack(p); |
| 1531 | free(p); |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 1532 | } |
| 1533 | |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1534 | stop_progress(&ctx->progress); |
René Scharfe | 0aa6bce | 2019-08-07 13:15:02 +0200 | [diff] [blame] | 1535 | strbuf_release(&progress_title); |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1536 | strbuf_release(&packname); |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 1537 | |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 1538 | return 0; |
| 1539 | } |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 1540 | |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1541 | static int fill_oids_from_commits(struct write_commit_graph_context *ctx, |
| 1542 | struct oidset *commits) |
Derrick Stolee | 4c9efe8 | 2019-06-12 06:29:42 -0700 | [diff] [blame] | 1543 | { |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1544 | struct oidset_iter iter; |
| 1545 | struct object_id *oid; |
| 1546 | |
| 1547 | if (!oidset_size(commits)) |
| 1548 | return 0; |
Derrick Stolee | 3d5df01 | 2018-04-10 08:56:07 -0400 | [diff] [blame] | 1549 | |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1550 | oidset_iter_init(commits, &iter); |
| 1551 | while ((oid = oidset_iter_next(&iter))) { |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1552 | oid_array_append(&ctx->oids, oid); |
Derrick Stolee | 4c9efe8 | 2019-06-12 06:29:42 -0700 | [diff] [blame] | 1553 | } |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 1554 | |
SZEDER Gábor | 7c5c9b9 | 2019-08-05 10:02:40 +0200 | [diff] [blame] | 1555 | return 0; |
Derrick Stolee | 4c9efe8 | 2019-06-12 06:29:42 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Derrick Stolee | b2c8306 | 2019-06-12 06:29:42 -0700 | [diff] [blame] | 1558 | static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx) |
| 1559 | { |
| 1560 | if (ctx->report_progress) |
| 1561 | ctx->progress = start_delayed_progress( |
| 1562 | _("Finding commits for commit graph among packed objects"), |
| 1563 | ctx->approx_nr_objects); |
| 1564 | for_each_packed_object(add_packed_commits, ctx, |
| 1565 | FOR_EACH_OBJECT_PACK_ORDER); |
| 1566 | if (ctx->progress_done < ctx->approx_nr_objects) |
| 1567 | display_progress(ctx->progress, ctx->approx_nr_objects); |
| 1568 | stop_progress(&ctx->progress); |
| 1569 | } |
| 1570 | |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1571 | static void copy_oids_to_commits(struct write_commit_graph_context *ctx) |
| 1572 | { |
| 1573 | uint32_t i; |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 1574 | enum commit_graph_split_flags flags = ctx->opts ? |
| 1575 | ctx->opts->split_flags : COMMIT_GRAPH_SPLIT_UNSPECIFIED; |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1576 | |
| 1577 | ctx->num_extra_edges = 0; |
| 1578 | if (ctx->report_progress) |
| 1579 | ctx->progress = start_delayed_progress( |
| 1580 | _("Finding extra edges in commit graph"), |
| 1581 | ctx->oids.nr); |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1582 | oid_array_sort(&ctx->oids); |
| 1583 | for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) { |
René Scharfe | 689a146 | 2019-09-15 19:07:44 +0200 | [diff] [blame] | 1584 | unsigned int num_parents; |
| 1585 | |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1586 | display_progress(ctx->progress, i + 1); |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1587 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1588 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc); |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 1589 | ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1590 | |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1591 | if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE && |
Abhishek Kumar | c49c82a | 2020-06-17 14:44:10 +0530 | [diff] [blame] | 1592 | commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH) |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1593 | continue; |
| 1594 | |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1595 | if (ctx->split && flags == COMMIT_GRAPH_SPLIT_REPLACE) |
| 1596 | parse_commit(ctx->commits.list[ctx->commits.nr]); |
| 1597 | else |
| 1598 | parse_commit_no_graph(ctx->commits.list[ctx->commits.nr]); |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1599 | |
René Scharfe | 689a146 | 2019-09-15 19:07:44 +0200 | [diff] [blame] | 1600 | num_parents = commit_list_count(ctx->commits.list[ctx->commits.nr]->parents); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1601 | if (num_parents > 2) |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1602 | ctx->num_extra_edges += num_parents - 1; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1603 | |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1604 | ctx->commits.nr++; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1605 | } |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 1606 | stop_progress(&ctx->progress); |
| 1607 | } |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1608 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1609 | static int write_graph_chunk_base_1(struct hashfile *f, |
| 1610 | struct commit_graph *g) |
| 1611 | { |
| 1612 | int num = 0; |
| 1613 | |
| 1614 | if (!g) |
| 1615 | return 0; |
| 1616 | |
| 1617 | num = write_graph_chunk_base_1(f, g->base_graph); |
| 1618 | hashwrite(f, g->oid.hash, the_hash_algo->rawsz); |
| 1619 | return num + 1; |
| 1620 | } |
| 1621 | |
| 1622 | static int write_graph_chunk_base(struct hashfile *f, |
| 1623 | struct write_commit_graph_context *ctx) |
| 1624 | { |
| 1625 | int num = write_graph_chunk_base_1(f, ctx->new_base_graph); |
| 1626 | |
| 1627 | if (num != ctx->num_commit_graphs_after - 1) { |
| 1628 | error(_("failed to write correct number of base graph ids")); |
| 1629 | return -1; |
| 1630 | } |
| 1631 | |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1635 | typedef int (*chunk_write_fn)(struct hashfile *f, |
| 1636 | struct write_commit_graph_context *ctx); |
| 1637 | |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1638 | struct chunk_info { |
| 1639 | uint32_t id; |
| 1640 | uint64_t size; |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1641 | chunk_write_fn write_fn; |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1642 | }; |
| 1643 | |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1644 | static int write_commit_graph_file(struct write_commit_graph_context *ctx) |
| 1645 | { |
| 1646 | uint32_t i; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1647 | int fd; |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1648 | struct hashfile *f; |
| 1649 | struct lock_file lk = LOCK_INIT; |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1650 | struct chunk_info chunks[MAX_NUM_CHUNKS + 1]; |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1651 | const unsigned hashsz = the_hash_algo->rawsz; |
| 1652 | struct strbuf progress_title = STRBUF_INIT; |
Derrick Stolee | 144354b | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1653 | int num_chunks = 3; |
SZEDER Gábor | bb4d60e | 2020-06-05 13:00:31 +0000 | [diff] [blame] | 1654 | uint64_t chunk_offset; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1655 | struct object_id file_hash; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1656 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1657 | if (ctx->split) { |
| 1658 | struct strbuf tmp_file = STRBUF_INIT; |
| 1659 | |
| 1660 | strbuf_addf(&tmp_file, |
| 1661 | "%s/info/commit-graphs/tmp_graph_XXXXXX", |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 1662 | ctx->odb->path); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1663 | ctx->graph_name = strbuf_detach(&tmp_file, NULL); |
| 1664 | } else { |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 1665 | ctx->graph_name = get_commit_graph_filename(ctx->odb); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1668 | if (safe_create_leading_directories(ctx->graph_name)) { |
| 1669 | UNLEAK(ctx->graph_name); |
| 1670 | error(_("unable to create leading directories of %s"), |
| 1671 | ctx->graph_name); |
| 1672 | return -1; |
Derrick Stolee | f4dbdfc | 2018-10-03 10:12:15 -0700 | [diff] [blame] | 1673 | } |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1674 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1675 | if (ctx->split) { |
Derrick Stolee | 663b2b1 | 2020-09-17 18:11:46 +0000 | [diff] [blame] | 1676 | char *lock_name = get_commit_graph_chain_filename(ctx->odb); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1677 | |
Taylor Blau | 45a4365 | 2020-04-29 11:36:46 -0600 | [diff] [blame] | 1678 | hold_lock_file_for_update_mode(&lk, lock_name, |
| 1679 | LOCK_DIE_ON_ERROR, 0444); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1680 | |
| 1681 | fd = git_mkstemp_mode(ctx->graph_name, 0444); |
| 1682 | if (fd < 0) { |
Taylor Blau | a2d57e2 | 2020-04-23 15:41:02 -0600 | [diff] [blame] | 1683 | error(_("unable to create temporary graph layer")); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1684 | return -1; |
| 1685 | } |
| 1686 | |
Taylor Blau | f4d6284 | 2020-04-29 11:36:42 -0600 | [diff] [blame] | 1687 | if (adjust_shared_perm(ctx->graph_name)) { |
| 1688 | error(_("unable to adjust shared permissions for '%s'"), |
| 1689 | ctx->graph_name); |
| 1690 | return -1; |
| 1691 | } |
| 1692 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1693 | f = hashfd(fd, ctx->graph_name); |
| 1694 | } else { |
Taylor Blau | 1f9beca | 2020-04-29 11:36:38 -0600 | [diff] [blame] | 1695 | hold_lock_file_for_update_mode(&lk, ctx->graph_name, |
| 1696 | LOCK_DIE_ON_ERROR, 0444); |
Martin Ågren | a52cdce | 2021-01-05 20:23:47 +0100 | [diff] [blame] | 1697 | fd = get_lock_file_fd(&lk); |
| 1698 | f = hashfd(fd, get_lock_file_path(&lk)); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1699 | } |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1700 | |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1701 | chunks[0].id = GRAPH_CHUNKID_OIDFANOUT; |
| 1702 | chunks[0].size = GRAPH_FANOUT_SIZE; |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1703 | chunks[0].write_fn = write_graph_chunk_fanout; |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1704 | chunks[1].id = GRAPH_CHUNKID_OIDLOOKUP; |
| 1705 | chunks[1].size = hashsz * ctx->commits.nr; |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1706 | chunks[1].write_fn = write_graph_chunk_oids; |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1707 | chunks[2].id = GRAPH_CHUNKID_DATA; |
| 1708 | chunks[2].size = (hashsz + 16) * ctx->commits.nr; |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1709 | chunks[2].write_fn = write_graph_chunk_data; |
Derrick Stolee | 144354b | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1710 | if (ctx->num_extra_edges) { |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1711 | chunks[num_chunks].id = GRAPH_CHUNKID_EXTRAEDGES; |
| 1712 | chunks[num_chunks].size = 4 * ctx->num_extra_edges; |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1713 | chunks[num_chunks].write_fn = write_graph_chunk_extra_edges; |
Derrick Stolee | 144354b | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1714 | num_chunks++; |
| 1715 | } |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1716 | if (ctx->changed_paths) { |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1717 | chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMINDEXES; |
| 1718 | chunks[num_chunks].size = sizeof(uint32_t) * ctx->commits.nr; |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1719 | chunks[num_chunks].write_fn = write_graph_chunk_bloom_indexes; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1720 | num_chunks++; |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1721 | chunks[num_chunks].id = GRAPH_CHUNKID_BLOOMDATA; |
| 1722 | chunks[num_chunks].size = sizeof(uint32_t) * 3 |
SZEDER Gábor | bb4d60e | 2020-06-05 13:00:31 +0000 | [diff] [blame] | 1723 | + ctx->total_bloom_filter_data_size; |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1724 | chunks[num_chunks].write_fn = write_graph_chunk_bloom_data; |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1725 | num_chunks++; |
| 1726 | } |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1727 | if (ctx->num_commit_graphs_after > 1) { |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1728 | chunks[num_chunks].id = GRAPH_CHUNKID_BASE; |
| 1729 | chunks[num_chunks].size = hashsz * (ctx->num_commit_graphs_after - 1); |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1730 | chunks[num_chunks].write_fn = write_graph_chunk_base; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1731 | num_chunks++; |
| 1732 | } |
Derrick Stolee | 144354b | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1733 | |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1734 | chunks[num_chunks].id = 0; |
| 1735 | chunks[num_chunks].size = 0; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1736 | |
| 1737 | hashwrite_be32(f, GRAPH_SIGNATURE); |
| 1738 | |
| 1739 | hashwrite_u8(f, GRAPH_VERSION); |
brian m. carlson | c166599 | 2018-11-14 04:09:35 +0000 | [diff] [blame] | 1740 | hashwrite_u8(f, oid_version()); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1741 | hashwrite_u8(f, num_chunks); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1742 | hashwrite_u8(f, ctx->num_commit_graphs_after - 1); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1743 | |
SZEDER Gábor | bb4d60e | 2020-06-05 13:00:31 +0000 | [diff] [blame] | 1744 | chunk_offset = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1745 | for (i = 0; i <= num_chunks; i++) { |
| 1746 | uint32_t chunk_write[3]; |
| 1747 | |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1748 | chunk_write[0] = htonl(chunks[i].id); |
SZEDER Gábor | bb4d60e | 2020-06-05 13:00:31 +0000 | [diff] [blame] | 1749 | chunk_write[1] = htonl(chunk_offset >> 32); |
| 1750 | chunk_write[2] = htonl(chunk_offset & 0xffffffff); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1751 | hashwrite(f, chunk_write, 12); |
SZEDER Gábor | bb4d60e | 2020-06-05 13:00:31 +0000 | [diff] [blame] | 1752 | |
SZEDER Gábor | 7fbfe07 | 2020-06-05 13:00:32 +0000 | [diff] [blame] | 1753 | chunk_offset += chunks[i].size; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1754 | } |
| 1755 | |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1756 | if (ctx->report_progress) { |
Ævar Arnfjörð Bjarmason | 2894473 | 2019-01-19 21:21:16 +0100 | [diff] [blame] | 1757 | strbuf_addf(&progress_title, |
| 1758 | Q_("Writing out commit graph in %d pass", |
| 1759 | "Writing out commit graph in %d passes", |
| 1760 | num_chunks), |
| 1761 | num_chunks); |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1762 | ctx->progress = start_delayed_progress( |
Ævar Arnfjörð Bjarmason | 2894473 | 2019-01-19 21:21:16 +0100 | [diff] [blame] | 1763 | progress_title.buf, |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1764 | num_chunks * ctx->commits.nr); |
Ævar Arnfjörð Bjarmason | 2894473 | 2019-01-19 21:21:16 +0100 | [diff] [blame] | 1765 | } |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1766 | |
| 1767 | for (i = 0; i < num_chunks; i++) { |
SZEDER Gábor | 2dd4fed | 2020-07-01 13:27:27 +0000 | [diff] [blame] | 1768 | uint64_t start_offset = f->total + f->offset; |
| 1769 | |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1770 | if (chunks[i].write_fn(f, ctx)) |
| 1771 | return -1; |
SZEDER Gábor | 2dd4fed | 2020-07-01 13:27:27 +0000 | [diff] [blame] | 1772 | |
| 1773 | if (f->total + f->offset != start_offset + chunks[i].size) |
| 1774 | BUG("expected to write %"PRId64" bytes to chunk %"PRIx32", but wrote %"PRId64" instead", |
| 1775 | chunks[i].size, chunks[i].id, |
| 1776 | f->total + f->offset - start_offset); |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 1777 | } |
SZEDER Gábor | 17e6275 | 2020-07-01 13:27:26 +0000 | [diff] [blame] | 1778 | |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1779 | stop_progress(&ctx->progress); |
Ævar Arnfjörð Bjarmason | 2894473 | 2019-01-19 21:21:16 +0100 | [diff] [blame] | 1780 | strbuf_release(&progress_title); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1781 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1782 | if (ctx->split && ctx->base_graph_name && ctx->num_commit_graphs_after > 1) { |
| 1783 | char *new_base_hash = xstrdup(oid_to_hex(&ctx->new_base_graph->oid)); |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 1784 | char *new_base_name = get_split_graph_filename(ctx->new_base_graph->odb, new_base_hash); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1785 | |
| 1786 | free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2]); |
| 1787 | free(ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2]); |
| 1788 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 2] = new_base_name; |
| 1789 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 2] = new_base_hash; |
| 1790 | } |
| 1791 | |
Derrick Stolee | c3a3a96 | 2019-05-17 11:41:47 -0700 | [diff] [blame] | 1792 | close_commit_graph(ctx->r->objects); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1793 | finalize_hashfile(f, file_hash.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC); |
| 1794 | |
| 1795 | if (ctx->split) { |
| 1796 | FILE *chainf = fdopen_lock_file(&lk, "w"); |
| 1797 | char *final_graph_name; |
| 1798 | int result; |
| 1799 | |
| 1800 | close(fd); |
| 1801 | |
| 1802 | if (!chainf) { |
| 1803 | error(_("unable to open commit-graph chain file")); |
| 1804 | return -1; |
| 1805 | } |
| 1806 | |
| 1807 | if (ctx->base_graph_name) { |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1808 | const char *dest; |
| 1809 | int idx = ctx->num_commit_graphs_after - 1; |
| 1810 | if (ctx->num_commit_graphs_after > 1) |
| 1811 | idx--; |
| 1812 | |
| 1813 | dest = ctx->commit_graph_filenames_after[idx]; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1814 | |
Derrick Stolee | 135a712 | 2019-06-18 11:14:28 -0700 | [diff] [blame] | 1815 | if (strcmp(ctx->base_graph_name, dest)) { |
| 1816 | result = rename(ctx->base_graph_name, dest); |
| 1817 | |
| 1818 | if (result) { |
| 1819 | error(_("failed to rename base commit-graph file")); |
| 1820 | return -1; |
| 1821 | } |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1822 | } |
| 1823 | } else { |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 1824 | char *graph_name = get_commit_graph_filename(ctx->odb); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1825 | unlink(graph_name); |
| 1826 | } |
| 1827 | |
| 1828 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1] = xstrdup(oid_to_hex(&file_hash)); |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 1829 | final_graph_name = get_split_graph_filename(ctx->odb, |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1830 | ctx->commit_graph_hash_after[ctx->num_commit_graphs_after - 1]); |
| 1831 | ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name; |
| 1832 | |
| 1833 | result = rename(ctx->graph_name, final_graph_name); |
| 1834 | |
| 1835 | for (i = 0; i < ctx->num_commit_graphs_after; i++) |
Martin Ågren | a52cdce | 2021-01-05 20:23:47 +0100 | [diff] [blame] | 1836 | fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 1837 | |
| 1838 | if (result) { |
| 1839 | error(_("failed to rename temporary commit-graph file")); |
| 1840 | return -1; |
| 1841 | } |
| 1842 | } |
| 1843 | |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 1844 | commit_lock_file(&lk); |
| 1845 | |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 1846 | return 0; |
| 1847 | } |
| 1848 | |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1849 | static void split_graph_merge_strategy(struct write_commit_graph_context *ctx) |
| 1850 | { |
Alex Henrie | 8da02ce | 2019-09-30 20:29:34 -0600 | [diff] [blame] | 1851 | struct commit_graph *g; |
| 1852 | uint32_t num_commits; |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 1853 | enum commit_graph_split_flags flags = COMMIT_GRAPH_SPLIT_UNSPECIFIED; |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1854 | uint32_t i; |
| 1855 | |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 1856 | int max_commits = 0; |
| 1857 | int size_mult = 2; |
| 1858 | |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 1859 | if (ctx->opts) { |
| 1860 | max_commits = ctx->opts->max_commits; |
Derrick Stolee | 63020f1 | 2020-01-02 16:14:14 +0000 | [diff] [blame] | 1861 | |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 1862 | if (ctx->opts->size_multiple) |
| 1863 | size_mult = ctx->opts->size_multiple; |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 1864 | |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 1865 | flags = ctx->opts->split_flags; |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1868 | g = ctx->r->objects->commit_graph; |
Alex Henrie | 8da02ce | 2019-09-30 20:29:34 -0600 | [diff] [blame] | 1869 | num_commits = ctx->commits.nr; |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1870 | if (flags == COMMIT_GRAPH_SPLIT_REPLACE) |
| 1871 | ctx->num_commit_graphs_after = 1; |
| 1872 | else |
| 1873 | ctx->num_commit_graphs_after = ctx->num_commit_graphs_before + 1; |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1874 | |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1875 | if (flags != COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED && |
| 1876 | flags != COMMIT_GRAPH_SPLIT_REPLACE) { |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 1877 | while (g && (g->num_commits <= size_mult * num_commits || |
| 1878 | (max_commits && num_commits > max_commits))) { |
| 1879 | if (g->odb != ctx->odb) |
| 1880 | break; |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 1881 | |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 1882 | num_commits += g->num_commits; |
| 1883 | g = g->base_graph; |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1884 | |
Taylor Blau | fdbde82 | 2020-04-13 22:04:12 -0600 | [diff] [blame] | 1885 | ctx->num_commit_graphs_after--; |
| 1886 | } |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1887 | } |
| 1888 | |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 1889 | if (flags != COMMIT_GRAPH_SPLIT_REPLACE) |
| 1890 | ctx->new_base_graph = g; |
| 1891 | else if (ctx->num_commit_graphs_after != 1) |
| 1892 | BUG("split_graph_merge_strategy: num_commit_graphs_after " |
| 1893 | "should be 1 with --split=replace"); |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1894 | |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 1895 | if (ctx->num_commit_graphs_after == 2) { |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 1896 | char *old_graph_name = get_commit_graph_filename(g->odb); |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 1897 | |
| 1898 | if (!strcmp(g->filename, old_graph_name) && |
Taylor Blau | ad2dd5b | 2020-02-03 13:18:02 -0800 | [diff] [blame] | 1899 | g->odb != ctx->odb) { |
Derrick Stolee | c523035 | 2019-06-18 11:14:30 -0700 | [diff] [blame] | 1900 | ctx->num_commit_graphs_after = 1; |
| 1901 | ctx->new_base_graph = NULL; |
| 1902 | } |
| 1903 | |
| 1904 | free(old_graph_name); |
| 1905 | } |
| 1906 | |
Taylor Blau | b78a556 | 2020-04-23 15:41:09 -0600 | [diff] [blame] | 1907 | CALLOC_ARRAY(ctx->commit_graph_filenames_after, ctx->num_commit_graphs_after); |
| 1908 | CALLOC_ARRAY(ctx->commit_graph_hash_after, ctx->num_commit_graphs_after); |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1909 | |
| 1910 | for (i = 0; i < ctx->num_commit_graphs_after && |
| 1911 | i < ctx->num_commit_graphs_before; i++) |
| 1912 | ctx->commit_graph_filenames_after[i] = xstrdup(ctx->commit_graph_filenames_before[i]); |
| 1913 | |
| 1914 | i = ctx->num_commit_graphs_before - 1; |
| 1915 | g = ctx->r->objects->commit_graph; |
| 1916 | |
| 1917 | while (g) { |
| 1918 | if (i < ctx->num_commit_graphs_after) |
| 1919 | ctx->commit_graph_hash_after[i] = xstrdup(oid_to_hex(&g->oid)); |
| 1920 | |
| 1921 | i--; |
| 1922 | g = g->base_graph; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | static void merge_commit_graph(struct write_commit_graph_context *ctx, |
| 1927 | struct commit_graph *g) |
| 1928 | { |
| 1929 | uint32_t i; |
| 1930 | uint32_t offset = g->num_commits_in_base; |
| 1931 | |
| 1932 | ALLOC_GROW(ctx->commits.list, ctx->commits.nr + g->num_commits, ctx->commits.alloc); |
| 1933 | |
| 1934 | for (i = 0; i < g->num_commits; i++) { |
| 1935 | struct object_id oid; |
| 1936 | struct commit *result; |
| 1937 | |
| 1938 | display_progress(ctx->progress, i + 1); |
| 1939 | |
| 1940 | load_oid_from_graph(g, i + offset, &oid); |
| 1941 | |
| 1942 | /* only add commits if they still exist in the repo */ |
| 1943 | result = lookup_commit_reference_gently(ctx->r, &oid, 1); |
| 1944 | |
| 1945 | if (result) { |
| 1946 | ctx->commits.list[ctx->commits.nr] = result; |
| 1947 | ctx->commits.nr++; |
| 1948 | } |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | static int commit_compare(const void *_a, const void *_b) |
| 1953 | { |
| 1954 | const struct commit *a = *(const struct commit **)_a; |
| 1955 | const struct commit *b = *(const struct commit **)_b; |
| 1956 | return oidcmp(&a->object.oid, &b->object.oid); |
| 1957 | } |
| 1958 | |
| 1959 | static void sort_and_scan_merged_commits(struct write_commit_graph_context *ctx) |
| 1960 | { |
Derrick Stolee | 150f115 | 2020-10-09 20:53:51 +0000 | [diff] [blame] | 1961 | uint32_t i, dedup_i = 0; |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1962 | |
| 1963 | if (ctx->report_progress) |
| 1964 | ctx->progress = start_delayed_progress( |
| 1965 | _("Scanning merged commits"), |
| 1966 | ctx->commits.nr); |
| 1967 | |
| 1968 | QSORT(ctx->commits.list, ctx->commits.nr, commit_compare); |
| 1969 | |
| 1970 | ctx->num_extra_edges = 0; |
| 1971 | for (i = 0; i < ctx->commits.nr; i++) { |
| 1972 | display_progress(ctx->progress, i); |
| 1973 | |
| 1974 | if (i && oideq(&ctx->commits.list[i - 1]->object.oid, |
| 1975 | &ctx->commits.list[i]->object.oid)) { |
Derrick Stolee | 150f115 | 2020-10-09 20:53:51 +0000 | [diff] [blame] | 1976 | /* |
| 1977 | * Silently ignore duplicates. These were likely |
| 1978 | * created due to a commit appearing in multiple |
| 1979 | * layers of the chain, which is unexpected but |
| 1980 | * not invalid. We should make sure there is a |
| 1981 | * unique copy in the new layer. |
| 1982 | */ |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1983 | } else { |
René Scharfe | 689a146 | 2019-09-15 19:07:44 +0200 | [diff] [blame] | 1984 | unsigned int num_parents; |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1985 | |
Derrick Stolee | 150f115 | 2020-10-09 20:53:51 +0000 | [diff] [blame] | 1986 | ctx->commits.list[dedup_i] = ctx->commits.list[i]; |
| 1987 | dedup_i++; |
| 1988 | |
René Scharfe | 689a146 | 2019-09-15 19:07:44 +0200 | [diff] [blame] | 1989 | num_parents = commit_list_count(ctx->commits.list[i]->parents); |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1990 | if (num_parents > 2) |
Derrick Stolee | a35bea4 | 2019-08-05 09:43:41 -0700 | [diff] [blame] | 1991 | ctx->num_extra_edges += num_parents - 1; |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1992 | } |
| 1993 | } |
| 1994 | |
Derrick Stolee | 150f115 | 2020-10-09 20:53:51 +0000 | [diff] [blame] | 1995 | ctx->commits.nr = dedup_i; |
| 1996 | |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 1997 | stop_progress(&ctx->progress); |
| 1998 | } |
| 1999 | |
| 2000 | static void merge_commit_graphs(struct write_commit_graph_context *ctx) |
| 2001 | { |
| 2002 | struct commit_graph *g = ctx->r->objects->commit_graph; |
| 2003 | uint32_t current_graph_number = ctx->num_commit_graphs_before; |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 2004 | |
| 2005 | while (g && current_graph_number >= ctx->num_commit_graphs_after) { |
| 2006 | current_graph_number--; |
| 2007 | |
René Scharfe | d68ce90 | 2020-02-20 19:49:18 +0100 | [diff] [blame] | 2008 | if (ctx->report_progress) |
| 2009 | ctx->progress = start_delayed_progress(_("Merging commit-graph"), 0); |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 2010 | |
| 2011 | merge_commit_graph(ctx, g); |
| 2012 | stop_progress(&ctx->progress); |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 2013 | |
| 2014 | g = g->base_graph; |
| 2015 | } |
| 2016 | |
| 2017 | if (g) { |
| 2018 | ctx->new_base_graph = g; |
| 2019 | ctx->new_num_commits_in_base = g->num_commits + g->num_commits_in_base; |
| 2020 | } |
| 2021 | |
| 2022 | if (ctx->new_base_graph) |
| 2023 | ctx->base_graph_name = xstrdup(ctx->new_base_graph->filename); |
| 2024 | |
| 2025 | sort_and_scan_merged_commits(ctx); |
| 2026 | } |
| 2027 | |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2028 | static void mark_commit_graphs(struct write_commit_graph_context *ctx) |
| 2029 | { |
| 2030 | uint32_t i; |
| 2031 | time_t now = time(NULL); |
| 2032 | |
| 2033 | for (i = ctx->num_commit_graphs_after - 1; i < ctx->num_commit_graphs_before; i++) { |
| 2034 | struct stat st; |
| 2035 | struct utimbuf updated_time; |
| 2036 | |
| 2037 | stat(ctx->commit_graph_filenames_before[i], &st); |
| 2038 | |
| 2039 | updated_time.actime = st.st_atime; |
| 2040 | updated_time.modtime = now; |
| 2041 | utime(ctx->commit_graph_filenames_before[i], &updated_time); |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | static void expire_commit_graphs(struct write_commit_graph_context *ctx) |
| 2046 | { |
| 2047 | struct strbuf path = STRBUF_INIT; |
| 2048 | DIR *dir; |
| 2049 | struct dirent *de; |
| 2050 | size_t dirnamelen; |
Derrick Stolee | c2bc6e6 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 2051 | timestamp_t expire_time = time(NULL); |
| 2052 | |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 2053 | if (ctx->opts && ctx->opts->expire_time) |
| 2054 | expire_time = ctx->opts->expire_time; |
Derrick Stolee | ba41112 | 2019-06-18 11:14:33 -0700 | [diff] [blame] | 2055 | if (!ctx->split) { |
Derrick Stolee | 663b2b1 | 2020-09-17 18:11:46 +0000 | [diff] [blame] | 2056 | char *chain_file_name = get_commit_graph_chain_filename(ctx->odb); |
Derrick Stolee | ba41112 | 2019-06-18 11:14:33 -0700 | [diff] [blame] | 2057 | unlink(chain_file_name); |
| 2058 | free(chain_file_name); |
| 2059 | ctx->num_commit_graphs_after = 0; |
| 2060 | } |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2061 | |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 2062 | strbuf_addstr(&path, ctx->odb->path); |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2063 | strbuf_addstr(&path, "/info/commit-graphs"); |
| 2064 | dir = opendir(path.buf); |
| 2065 | |
René Scharfe | 0aa6bce | 2019-08-07 13:15:02 +0200 | [diff] [blame] | 2066 | if (!dir) |
| 2067 | goto out; |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2068 | |
| 2069 | strbuf_addch(&path, '/'); |
| 2070 | dirnamelen = path.len; |
| 2071 | while ((de = readdir(dir)) != NULL) { |
| 2072 | struct stat st; |
| 2073 | uint32_t i, found = 0; |
| 2074 | |
| 2075 | strbuf_setlen(&path, dirnamelen); |
| 2076 | strbuf_addstr(&path, de->d_name); |
| 2077 | |
| 2078 | stat(path.buf, &st); |
| 2079 | |
| 2080 | if (st.st_mtime > expire_time) |
| 2081 | continue; |
| 2082 | if (path.len < 6 || strcmp(path.buf + path.len - 6, ".graph")) |
| 2083 | continue; |
| 2084 | |
| 2085 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { |
| 2086 | if (!strcmp(ctx->commit_graph_filenames_after[i], |
| 2087 | path.buf)) { |
| 2088 | found = 1; |
| 2089 | break; |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | if (!found) |
| 2094 | unlink(path.buf); |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2095 | } |
René Scharfe | 0aa6bce | 2019-08-07 13:15:02 +0200 | [diff] [blame] | 2096 | |
| 2097 | out: |
| 2098 | strbuf_release(&path); |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 2101 | int write_commit_graph(struct object_directory *odb, |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 2102 | struct string_list *pack_indexes, |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 2103 | struct oidset *commits, |
SZEDER Gábor | 39d8831 | 2019-08-05 10:02:39 +0200 | [diff] [blame] | 2104 | enum commit_graph_write_flags flags, |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 2105 | const struct commit_graph_opts *opts) |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2106 | { |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2107 | struct write_commit_graph_context *ctx; |
Jeff King | 1cbdbf3 | 2020-12-07 14:11:02 -0500 | [diff] [blame] | 2108 | uint32_t i; |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 2109 | int res = 0; |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 2110 | int replace = 0; |
Taylor Blau | 9a7a9ed | 2020-09-16 14:07:46 -0400 | [diff] [blame] | 2111 | struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2112 | |
Derrick Stolee | 85102ac | 2020-10-09 20:53:52 +0000 | [diff] [blame] | 2113 | prepare_repo_settings(the_repository); |
| 2114 | if (!the_repository->settings.core_commit_graph) { |
| 2115 | warning(_("attempting to write a commit-graph, but 'core.commitGraph' is disabled")); |
| 2116 | return 0; |
| 2117 | } |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2118 | if (!commit_graph_compatible(the_repository)) |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 2119 | return 0; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2120 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2121 | ctx = xcalloc(1, sizeof(struct write_commit_graph_context)); |
| 2122 | ctx->r = the_repository; |
Taylor Blau | 0bd52e2 | 2020-02-03 21:51:50 -0800 | [diff] [blame] | 2123 | ctx->odb = odb; |
SZEDER Gábor | 39d8831 | 2019-08-05 10:02:39 +0200 | [diff] [blame] | 2124 | ctx->append = flags & COMMIT_GRAPH_WRITE_APPEND ? 1 : 0; |
| 2125 | ctx->report_progress = flags & COMMIT_GRAPH_WRITE_PROGRESS ? 1 : 0; |
| 2126 | ctx->split = flags & COMMIT_GRAPH_WRITE_SPLIT ? 1 : 0; |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 2127 | ctx->opts = opts; |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 2128 | ctx->total_bloom_filter_data_size = 0; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 2129 | |
Taylor Blau | 9a7a9ed | 2020-09-16 14:07:46 -0400 | [diff] [blame] | 2130 | bloom_settings.bits_per_entry = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY", |
| 2131 | bloom_settings.bits_per_entry); |
| 2132 | bloom_settings.num_hashes = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_NUM_HASHES", |
| 2133 | bloom_settings.num_hashes); |
| 2134 | bloom_settings.max_changed_paths = git_env_ulong("GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS", |
| 2135 | bloom_settings.max_changed_paths); |
| 2136 | ctx->bloom_settings = &bloom_settings; |
| 2137 | |
Derrick Stolee | 0087a87 | 2020-07-01 13:27:24 +0000 | [diff] [blame] | 2138 | if (flags & COMMIT_GRAPH_WRITE_BLOOM_FILTERS) |
| 2139 | ctx->changed_paths = 1; |
| 2140 | if (!(flags & COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS)) { |
| 2141 | struct commit_graph *g; |
| 2142 | prepare_commit_graph_one(ctx->r, ctx->odb); |
| 2143 | |
| 2144 | g = ctx->r->objects->commit_graph; |
| 2145 | |
| 2146 | /* We have changed-paths already. Keep them in the next graph */ |
| 2147 | if (g && g->chunk_bloom_data) { |
| 2148 | ctx->changed_paths = 1; |
| 2149 | ctx->bloom_settings = g->bloom_filter_settings; |
| 2150 | } |
| 2151 | } |
| 2152 | |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 2153 | if (ctx->split) { |
| 2154 | struct commit_graph *g; |
| 2155 | prepare_commit_graph(ctx->r); |
| 2156 | |
| 2157 | g = ctx->r->objects->commit_graph; |
| 2158 | |
| 2159 | while (g) { |
| 2160 | ctx->num_commit_graphs_before++; |
| 2161 | g = g->base_graph; |
| 2162 | } |
| 2163 | |
| 2164 | if (ctx->num_commit_graphs_before) { |
| 2165 | ALLOC_ARRAY(ctx->commit_graph_filenames_before, ctx->num_commit_graphs_before); |
| 2166 | i = ctx->num_commit_graphs_before; |
| 2167 | g = ctx->r->objects->commit_graph; |
| 2168 | |
| 2169 | while (g) { |
| 2170 | ctx->commit_graph_filenames_before[--i] = xstrdup(g->filename); |
| 2171 | g = g->base_graph; |
| 2172 | } |
| 2173 | } |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 2174 | |
Taylor Blau | 98bb796 | 2020-09-17 22:59:49 -0400 | [diff] [blame] | 2175 | if (ctx->opts) |
| 2176 | replace = ctx->opts->split_flags & COMMIT_GRAPH_SPLIT_REPLACE; |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 2177 | } |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2178 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2179 | ctx->approx_nr_objects = approximate_object_count(); |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2180 | |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 2181 | if (ctx->append) |
Taylor Blau | 13c2499 | 2020-02-03 13:18:00 -0800 | [diff] [blame] | 2182 | prepare_commit_graph_one(ctx->r, ctx->odb); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2183 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2184 | if (ctx->append && ctx->r->objects->commit_graph) { |
| 2185 | struct commit_graph *g = ctx->r->objects->commit_graph; |
| 2186 | for (i = 0; i < g->num_commits; i++) { |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 2187 | struct object_id oid; |
| 2188 | hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
| 2189 | oid_array_append(&ctx->oids, &oid); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | if (pack_indexes) { |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 2194 | ctx->order_by_pack = 1; |
Derrick Stolee | ef5b83f | 2019-06-12 06:29:41 -0700 | [diff] [blame] | 2195 | if ((res = fill_oids_from_packs(ctx, pack_indexes))) |
| 2196 | goto cleanup; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2197 | } |
| 2198 | |
Taylor Blau | 6830c36 | 2020-04-13 22:04:25 -0600 | [diff] [blame] | 2199 | if (commits) { |
| 2200 | if ((res = fill_oids_from_commits(ctx, commits))) |
SZEDER Gábor | 7c5c9b9 | 2019-08-05 10:02:40 +0200 | [diff] [blame] | 2201 | goto cleanup; |
| 2202 | } |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2203 | |
Junio C Hamano | 9b6606f | 2020-05-01 13:39:53 -0700 | [diff] [blame] | 2204 | if (!pack_indexes && !commits) { |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 2205 | ctx->order_by_pack = 1; |
Derrick Stolee | b2c8306 | 2019-06-12 06:29:42 -0700 | [diff] [blame] | 2206 | fill_oids_from_all_packs(ctx); |
Garima Singh | 3d11275 | 2020-03-30 00:31:30 +0000 | [diff] [blame] | 2207 | } |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2208 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2209 | close_reachable(ctx); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2210 | |
Derrick Stolee | f998d54 | 2019-06-12 06:29:44 -0700 | [diff] [blame] | 2211 | copy_oids_to_commits(ctx); |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2212 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2213 | if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) { |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 2214 | error(_("too many commits to write graph")); |
| 2215 | res = -1; |
| 2216 | goto cleanup; |
| 2217 | } |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 2218 | |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 2219 | if (!ctx->commits.nr && !replace) |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 2220 | goto cleanup; |
| 2221 | |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 2222 | if (ctx->split) { |
| 2223 | split_graph_merge_strategy(ctx); |
| 2224 | |
Taylor Blau | 8a6ac28 | 2020-04-13 22:04:17 -0600 | [diff] [blame] | 2225 | if (!replace) |
| 2226 | merge_commit_graphs(ctx); |
Derrick Stolee | 1771be9 | 2019-06-18 11:14:29 -0700 | [diff] [blame] | 2227 | } else |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 2228 | ctx->num_commit_graphs_after = 1; |
| 2229 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2230 | compute_generation_numbers(ctx); |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2231 | |
Garima Singh | f97b932 | 2020-03-30 00:31:28 +0000 | [diff] [blame] | 2232 | if (ctx->changed_paths) |
| 2233 | compute_bloom_filters(ctx); |
| 2234 | |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 2235 | res = write_commit_graph_file(ctx); |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2236 | |
Derrick Stolee | ba41112 | 2019-06-18 11:14:33 -0700 | [diff] [blame] | 2237 | if (ctx->split) |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2238 | mark_commit_graphs(ctx); |
Derrick Stolee | ba41112 | 2019-06-18 11:14:33 -0700 | [diff] [blame] | 2239 | |
| 2240 | expire_commit_graphs(ctx); |
Derrick Stolee | 8d84097 | 2019-06-18 11:14:31 -0700 | [diff] [blame] | 2241 | |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 2242 | cleanup: |
Derrick Stolee | 238def5 | 2019-06-12 06:29:45 -0700 | [diff] [blame] | 2243 | free(ctx->graph_name); |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2244 | free(ctx->commits.list); |
Jeff King | a5f1c44 | 2020-12-07 14:11:05 -0500 | [diff] [blame] | 2245 | oid_array_clear(&ctx->oids); |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 2246 | |
| 2247 | if (ctx->commit_graph_filenames_after) { |
| 2248 | for (i = 0; i < ctx->num_commit_graphs_after; i++) { |
| 2249 | free(ctx->commit_graph_filenames_after[i]); |
| 2250 | free(ctx->commit_graph_hash_after[i]); |
| 2251 | } |
| 2252 | |
| 2253 | for (i = 0; i < ctx->num_commit_graphs_before; i++) |
| 2254 | free(ctx->commit_graph_filenames_before[i]); |
| 2255 | |
| 2256 | free(ctx->commit_graph_filenames_after); |
| 2257 | free(ctx->commit_graph_filenames_before); |
| 2258 | free(ctx->commit_graph_hash_after); |
| 2259 | } |
| 2260 | |
Derrick Stolee | c9905be | 2019-06-12 06:29:40 -0700 | [diff] [blame] | 2261 | free(ctx); |
Derrick Stolee | e103f72 | 2019-06-12 06:29:37 -0700 | [diff] [blame] | 2262 | |
| 2263 | return res; |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2264 | } |
Derrick Stolee | 08fd81c | 2018-04-02 16:34:19 -0400 | [diff] [blame] | 2265 | |
| 2266 | #define VERIFY_COMMIT_GRAPH_ERROR_HASH 2 |
| 2267 | static int verify_commit_graph_error; |
| 2268 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 2269 | static void graph_report(const char *fmt, ...) |
| 2270 | { |
| 2271 | va_list ap; |
| 2272 | |
| 2273 | verify_commit_graph_error = 1; |
| 2274 | va_start(ap, fmt); |
| 2275 | vfprintf(stderr, fmt, ap); |
| 2276 | fprintf(stderr, "\n"); |
| 2277 | va_end(ap); |
| 2278 | } |
| 2279 | |
| 2280 | #define GENERATION_ZERO_EXISTS 1 |
| 2281 | #define GENERATION_NUMBER_EXISTS 2 |
| 2282 | |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 2283 | int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags) |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 2284 | { |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2285 | uint32_t i, cur_fanout_pos = 0; |
Derrick Stolee | 41df0e3 | 2018-06-27 09:24:42 -0400 | [diff] [blame] | 2286 | struct object_id prev_oid, cur_oid, checksum; |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2287 | int generation_zero = 0; |
Derrick Stolee | 41df0e3 | 2018-06-27 09:24:42 -0400 | [diff] [blame] | 2288 | struct hashfile *f; |
| 2289 | int devnull; |
Ævar Arnfjörð Bjarmason | 1f7f557 | 2018-09-17 15:33:36 +0000 | [diff] [blame] | 2290 | struct progress *progress = NULL; |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 2291 | int local_error = 0; |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2292 | |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 2293 | if (!g) { |
| 2294 | graph_report("no commit-graph file loaded"); |
| 2295 | return 1; |
| 2296 | } |
| 2297 | |
Ævar Arnfjörð Bjarmason | 2ac138d | 2019-03-25 13:08:29 +0100 | [diff] [blame] | 2298 | verify_commit_graph_error = verify_commit_graph_lite(g); |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2299 | if (verify_commit_graph_error) |
| 2300 | return verify_commit_graph_error; |
| 2301 | |
Derrick Stolee | 41df0e3 | 2018-06-27 09:24:42 -0400 | [diff] [blame] | 2302 | devnull = open("/dev/null", O_WRONLY); |
| 2303 | f = hashfd(devnull, NULL); |
| 2304 | hashwrite(f, g->data, g->data_len - g->hash_len); |
| 2305 | finalize_hashfile(f, checksum.hash, CSUM_CLOSE); |
Jeff King | 67947c3 | 2018-08-28 17:22:52 -0400 | [diff] [blame] | 2306 | if (!hasheq(checksum.hash, g->data + g->data_len - g->hash_len)) { |
Derrick Stolee | 41df0e3 | 2018-06-27 09:24:42 -0400 | [diff] [blame] | 2307 | graph_report(_("the commit-graph file has incorrect checksum and is likely corrupt")); |
| 2308 | verify_commit_graph_error = VERIFY_COMMIT_GRAPH_ERROR_HASH; |
| 2309 | } |
| 2310 | |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2311 | for (i = 0; i < g->num_commits; i++) { |
Derrick Stolee | 2e3c073 | 2018-06-27 09:24:37 -0400 | [diff] [blame] | 2312 | struct commit *graph_commit; |
| 2313 | |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2314 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
| 2315 | |
| 2316 | if (i && oidcmp(&prev_oid, &cur_oid) >= 0) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2317 | graph_report(_("commit-graph has incorrect OID order: %s then %s"), |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2318 | oid_to_hex(&prev_oid), |
| 2319 | oid_to_hex(&cur_oid)); |
| 2320 | |
| 2321 | oidcpy(&prev_oid, &cur_oid); |
| 2322 | |
| 2323 | while (cur_oid.hash[0] > cur_fanout_pos) { |
| 2324 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); |
| 2325 | |
| 2326 | if (i != fanout_value) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2327 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2328 | cur_fanout_pos, fanout_value, i); |
| 2329 | cur_fanout_pos++; |
| 2330 | } |
Derrick Stolee | 2e3c073 | 2018-06-27 09:24:37 -0400 | [diff] [blame] | 2331 | |
Junio C Hamano | 8295296 | 2018-07-17 15:46:19 -0700 | [diff] [blame] | 2332 | graph_commit = lookup_commit(r, &cur_oid); |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 2333 | if (!parse_commit_in_graph_one(r, g, graph_commit)) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2334 | graph_report(_("failed to parse commit %s from commit-graph"), |
Derrick Stolee | 2e3c073 | 2018-06-27 09:24:37 -0400 | [diff] [blame] | 2335 | oid_to_hex(&cur_oid)); |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | while (cur_fanout_pos < 256) { |
| 2339 | uint32_t fanout_value = get_be32(g->chunk_oid_fanout + cur_fanout_pos); |
| 2340 | |
| 2341 | if (g->num_commits != fanout_value) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2342 | graph_report(_("commit-graph has incorrect fanout value: fanout[%d] = %u != %u"), |
Derrick Stolee | 9bda846 | 2018-06-27 09:24:35 -0400 | [diff] [blame] | 2343 | cur_fanout_pos, fanout_value, i); |
| 2344 | |
| 2345 | cur_fanout_pos++; |
| 2346 | } |
| 2347 | |
Derrick Stolee | 41df0e3 | 2018-06-27 09:24:42 -0400 | [diff] [blame] | 2348 | if (verify_commit_graph_error & ~VERIFY_COMMIT_GRAPH_ERROR_HASH) |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2349 | return verify_commit_graph_error; |
| 2350 | |
Garima Singh | 7371612 | 2019-08-26 09:29:58 -0700 | [diff] [blame] | 2351 | if (flags & COMMIT_GRAPH_WRITE_PROGRESS) |
| 2352 | progress = start_progress(_("Verifying commits in commit graph"), |
| 2353 | g->num_commits); |
| 2354 | |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2355 | for (i = 0; i < g->num_commits; i++) { |
Derrick Stolee | 2e3c073 | 2018-06-27 09:24:37 -0400 | [diff] [blame] | 2356 | struct commit *graph_commit, *odb_commit; |
Derrick Stolee | 53614b1 | 2018-06-27 09:24:38 -0400 | [diff] [blame] | 2357 | struct commit_list *graph_parents, *odb_parents; |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2358 | uint32_t max_generation = 0; |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 2359 | uint32_t generation; |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2360 | |
Ævar Arnfjörð Bjarmason | 1f7f557 | 2018-09-17 15:33:36 +0000 | [diff] [blame] | 2361 | display_progress(progress, i + 1); |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2362 | hashcpy(cur_oid.hash, g->chunk_oid_lookup + g->hash_len * i); |
| 2363 | |
Junio C Hamano | 8295296 | 2018-07-17 15:46:19 -0700 | [diff] [blame] | 2364 | graph_commit = lookup_commit(r, &cur_oid); |
Jeff King | a378509 | 2019-06-20 03:41:21 -0400 | [diff] [blame] | 2365 | odb_commit = (struct commit *)create_object(r, &cur_oid, alloc_commit_node(r)); |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2366 | if (parse_commit_internal(odb_commit, 0, 0)) { |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2367 | graph_report(_("failed to parse commit %s from object database for commit-graph"), |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2368 | oid_to_hex(&cur_oid)); |
| 2369 | continue; |
| 2370 | } |
Derrick Stolee | 2e3c073 | 2018-06-27 09:24:37 -0400 | [diff] [blame] | 2371 | |
Stefan Beller | 4f542b7 | 2018-12-14 16:09:39 -0800 | [diff] [blame] | 2372 | if (!oideq(&get_commit_tree_in_graph_one(r, g, graph_commit)->object.oid, |
Derrick Stolee | 2e3c073 | 2018-06-27 09:24:37 -0400 | [diff] [blame] | 2373 | get_commit_tree_oid(odb_commit))) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2374 | graph_report(_("root tree OID for commit %s in commit-graph is %s != %s"), |
Derrick Stolee | 2e3c073 | 2018-06-27 09:24:37 -0400 | [diff] [blame] | 2375 | oid_to_hex(&cur_oid), |
| 2376 | oid_to_hex(get_commit_tree_oid(graph_commit)), |
| 2377 | oid_to_hex(get_commit_tree_oid(odb_commit))); |
Derrick Stolee | 53614b1 | 2018-06-27 09:24:38 -0400 | [diff] [blame] | 2378 | |
| 2379 | graph_parents = graph_commit->parents; |
| 2380 | odb_parents = odb_commit->parents; |
| 2381 | |
| 2382 | while (graph_parents) { |
| 2383 | if (odb_parents == NULL) { |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2384 | graph_report(_("commit-graph parent list for commit %s is too long"), |
Derrick Stolee | 53614b1 | 2018-06-27 09:24:38 -0400 | [diff] [blame] | 2385 | oid_to_hex(&cur_oid)); |
| 2386 | break; |
| 2387 | } |
| 2388 | |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 2389 | /* parse parent in case it is in a base graph */ |
| 2390 | parse_commit_in_graph_one(r, g, graph_parents->item); |
| 2391 | |
Jeff King | 9001dc2 | 2018-08-28 17:22:48 -0400 | [diff] [blame] | 2392 | if (!oideq(&graph_parents->item->object.oid, &odb_parents->item->object.oid)) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2393 | graph_report(_("commit-graph parent for %s is %s != %s"), |
Derrick Stolee | 53614b1 | 2018-06-27 09:24:38 -0400 | [diff] [blame] | 2394 | oid_to_hex(&cur_oid), |
| 2395 | oid_to_hex(&graph_parents->item->object.oid), |
| 2396 | oid_to_hex(&odb_parents->item->object.oid)); |
| 2397 | |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 2398 | generation = commit_graph_generation(graph_parents->item); |
| 2399 | if (generation > max_generation) |
| 2400 | max_generation = generation; |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2401 | |
Derrick Stolee | 53614b1 | 2018-06-27 09:24:38 -0400 | [diff] [blame] | 2402 | graph_parents = graph_parents->next; |
| 2403 | odb_parents = odb_parents->next; |
| 2404 | } |
| 2405 | |
| 2406 | if (odb_parents != NULL) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2407 | graph_report(_("commit-graph parent list for commit %s terminates early"), |
Derrick Stolee | 53614b1 | 2018-06-27 09:24:38 -0400 | [diff] [blame] | 2408 | oid_to_hex(&cur_oid)); |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2409 | |
Abhishek Kumar | c49c82a | 2020-06-17 14:44:10 +0530 | [diff] [blame] | 2410 | if (!commit_graph_generation(graph_commit)) { |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2411 | if (generation_zero == GENERATION_NUMBER_EXISTS) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2412 | graph_report(_("commit-graph has generation number zero for commit %s, but non-zero elsewhere"), |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2413 | oid_to_hex(&cur_oid)); |
| 2414 | generation_zero = GENERATION_ZERO_EXISTS; |
| 2415 | } else if (generation_zero == GENERATION_ZERO_EXISTS) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2416 | graph_report(_("commit-graph has non-zero generation number for commit %s, but zero elsewhere"), |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2417 | oid_to_hex(&cur_oid)); |
| 2418 | |
| 2419 | if (generation_zero == GENERATION_ZERO_EXISTS) |
| 2420 | continue; |
| 2421 | |
| 2422 | /* |
| 2423 | * If one of our parents has generation GENERATION_NUMBER_MAX, then |
| 2424 | * our generation is also GENERATION_NUMBER_MAX. Decrement to avoid |
| 2425 | * extra logic in the following condition. |
| 2426 | */ |
| 2427 | if (max_generation == GENERATION_NUMBER_MAX) |
| 2428 | max_generation--; |
| 2429 | |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 2430 | generation = commit_graph_generation(graph_commit); |
| 2431 | if (generation != max_generation + 1) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2432 | graph_report(_("commit-graph generation for commit %s is %u != %u"), |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2433 | oid_to_hex(&cur_oid), |
Abhishek Kumar | c752ad0 | 2020-06-17 14:44:11 +0530 | [diff] [blame] | 2434 | generation, |
Derrick Stolee | 1373e54 | 2018-06-27 09:24:39 -0400 | [diff] [blame] | 2435 | max_generation + 1); |
Derrick Stolee | 88968eb | 2018-06-27 09:24:40 -0400 | [diff] [blame] | 2436 | |
| 2437 | if (graph_commit->date != odb_commit->date) |
Ævar Arnfjörð Bjarmason | 93b4405 | 2019-03-25 13:08:34 +0100 | [diff] [blame] | 2438 | graph_report(_("commit date for commit %s in commit-graph is %"PRItime" != %"PRItime), |
Derrick Stolee | 88968eb | 2018-06-27 09:24:40 -0400 | [diff] [blame] | 2439 | oid_to_hex(&cur_oid), |
| 2440 | graph_commit->date, |
| 2441 | odb_commit->date); |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2442 | } |
Ævar Arnfjörð Bjarmason | 1f7f557 | 2018-09-17 15:33:36 +0000 | [diff] [blame] | 2443 | stop_progress(&progress); |
Derrick Stolee | 96af91d | 2018-06-27 09:24:36 -0400 | [diff] [blame] | 2444 | |
Derrick Stolee | 3da4b60 | 2019-06-18 11:14:32 -0700 | [diff] [blame] | 2445 | local_error = verify_commit_graph_error; |
| 2446 | |
| 2447 | if (!(flags & COMMIT_GRAPH_VERIFY_SHALLOW) && g->base_graph) |
| 2448 | local_error |= verify_commit_graph(r, g->base_graph, flags); |
| 2449 | |
| 2450 | return local_error; |
Derrick Stolee | 283e68c | 2018-06-27 09:24:32 -0400 | [diff] [blame] | 2451 | } |
Jonathan Tan | c3756d5 | 2018-07-11 15:42:40 -0700 | [diff] [blame] | 2452 | |
| 2453 | void free_commit_graph(struct commit_graph *g) |
| 2454 | { |
| 2455 | if (!g) |
| 2456 | return; |
Jeff King | c882853 | 2020-04-23 15:41:13 -0600 | [diff] [blame] | 2457 | if (g->data) { |
Jonathan Tan | c3756d5 | 2018-07-11 15:42:40 -0700 | [diff] [blame] | 2458 | munmap((void *)g->data, g->data_len); |
| 2459 | g->data = NULL; |
Jonathan Tan | c3756d5 | 2018-07-11 15:42:40 -0700 | [diff] [blame] | 2460 | } |
Derrick Stolee | 6c622f9 | 2019-06-18 11:14:27 -0700 | [diff] [blame] | 2461 | free(g->filename); |
Garima Singh | 76ffbca | 2020-04-06 16:59:49 +0000 | [diff] [blame] | 2462 | free(g->bloom_filter_settings); |
Jonathan Tan | c3756d5 | 2018-07-11 15:42:40 -0700 | [diff] [blame] | 2463 | free(g); |
| 2464 | } |
Jeff King | 6abada1 | 2019-09-12 10:44:45 -0400 | [diff] [blame] | 2465 | |
| 2466 | void disable_commit_graph(struct repository *r) |
| 2467 | { |
| 2468 | r->commit_graph_disabled = 1; |
| 2469 | } |