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