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