blob: e519cb81cb649dd016fdb5f26dbfd3623ead13e9 [file] [log] [blame]
Derrick Stolee08fd81c2018-04-02 16:34:19 -04001#ifndef COMMIT_GRAPH_H
2#define COMMIT_GRAPH_H
3
Elijah Newrena034e912023-05-16 06:34:06 +00004#include "object-store-ll.h"
Taylor Blau6830c362020-04-13 22:04:25 -06005#include "oidset.h"
Derrick Stolee2a2e32b2018-04-10 08:56:02 -04006
Derrick Stolee859fdc02018-08-29 05:49:04 -07007#define GIT_TEST_COMMIT_GRAPH "GIT_TEST_COMMIT_GRAPH"
Derrick Stolee7b671f82020-06-23 17:47:01 +00008#define GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE "GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE"
Garima Singhd5b873c2020-04-06 16:59:55 +00009#define GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS "GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS"
Derrick Stolee859fdc02018-08-29 05:49:04 -070010
Derrick Stoleeb23ea972020-04-16 20:14:03 +000011/*
Patrick Steinhardte04838e2023-10-31 08:16:13 +010012 * This environment variable controls whether commits looked up via the
13 * commit graph will be double checked to exist in the object database.
14 */
15#define GIT_COMMIT_GRAPH_PARANOIA "GIT_COMMIT_GRAPH_PARANOIA"
16
17/*
Derrick Stoleeb23ea972020-04-16 20:14:03 +000018 * This method is only used to enhance coverage of the commit-graph
19 * feature in the test suite with the GIT_TEST_COMMIT_GRAPH and
20 * GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS environment variables. Do not
21 * call this method oustide of a builtin, and only if you know what
22 * you are doing!
23 */
24void git_test_write_commit_graph_or_die(void);
25
Jonathan Tane5c5ca22018-07-11 15:42:39 -070026struct commit;
Garima Singh76ffbca2020-04-06 16:59:49 +000027struct bloom_filter_settings;
SZEDER Gáborfa796532020-06-05 13:00:28 +000028struct repository;
29struct raw_object_store;
30struct string_list;
Jonathan Tane5c5ca22018-07-11 15:42:39 -070031
Taylor Blauad2dd5b2020-02-03 13:18:02 -080032char *get_commit_graph_filename(struct object_directory *odb);
Derrick Stolee663b2b12020-09-17 18:11:46 +000033char *get_commit_graph_chain_filename(struct object_directory *odb);
Ævar Arnfjörð Bjarmason61df89c2019-03-25 13:08:30 +010034int open_commit_graph(const char *graph_file, int *fd, struct stat *st);
Jeff King7ed76b42023-09-28 00:38:07 -040035int open_commit_graph_chain(const char *chain_file, int *fd, struct stat *st);
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040036
Derrick Stolee177722b2018-04-10 08:56:05 -040037/*
38 * Given a commit struct, try to fill the commit struct info, including:
39 * 1. tree object
40 * 2. date
41 * 3. parents.
42 *
43 * Returns 1 if and only if the commit was found in the packed graph.
44 *
45 * See parse_commit_buffer() for the fallback after this call.
46 */
Jonathan Tandade47c2018-07-11 15:42:42 -070047int parse_commit_in_graph(struct repository *r, struct commit *item);
Derrick Stolee177722b2018-04-10 08:56:05 -040048
Derrick Stoleee2838d82018-05-01 12:47:13 +000049/*
Taylor Blau78053602022-07-12 19:10:31 -040050 * Fills `*pos` with the graph position of `c`, and returns 1 if `c` is
51 * found in the commit-graph belonging to `r`, or 0 otherwise.
52 * Initializes the commit-graph belonging to `r` if it hasn't been
53 * already.
54 *
55 * Note: this is a low-level helper that does not alter any slab data
56 * associated with `c`. Useful in circumstances where the slab data is
57 * already being modified (e.g., writing the commit-graph itself).
58 *
59 * In most cases, callers should use `parse_commit_in_graph()` instead.
60 */
61int repo_find_commit_pos_in_graph(struct repository *r, struct commit *c,
62 uint32_t *pos);
63
64/*
Patrick Steinhardtf559d6d2021-08-09 10:12:03 +020065 * Look up the given commit ID in the commit-graph. This will only return a
66 * commit if the ID exists both in the graph and in the object database such
67 * that we don't return commits whose object has been pruned. Otherwise, this
68 * function returns `NULL`.
69 */
70struct commit *lookup_commit_in_graph(struct repository *repo, const struct object_id *id);
71
72/*
Derrick Stoleee2838d82018-05-01 12:47:13 +000073 * It is possible that we loaded commit contents from the commit buffer,
74 * but we also want to ensure the commit-graph content is correctly
75 * checked and filled. Fill the graph_pos and generation members of
76 * the given commit.
77 */
Jonathan Tandade47c2018-07-11 15:42:42 -070078void load_commit_graph_info(struct repository *r, struct commit *item);
Derrick Stoleee2838d82018-05-01 12:47:13 +000079
Jonathan Tandade47c2018-07-11 15:42:42 -070080struct tree *get_commit_tree_in_graph(struct repository *r,
81 const struct commit *c);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +000082
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040083struct commit_graph {
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040084 const unsigned char *data;
85 size_t data_len;
86
87 unsigned char hash_len;
88 unsigned char num_chunks;
89 uint32_t num_commits;
90 struct object_id oid;
Derrick Stolee6c622f92019-06-18 11:14:27 -070091 char *filename;
Taylor Blau13c24992020-02-03 13:18:00 -080092 struct object_directory *odb;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040093
Derrick Stoleed4f4d602019-06-18 11:14:24 -070094 uint32_t num_commits_in_base;
Abhishek Kumar1fdc3832021-01-16 18:11:16 +000095 unsigned int read_generation_data;
Derrick Stoleed4f4d602019-06-18 11:14:24 -070096 struct commit_graph *base_graph;
97
Derrick Stolee2a2e32b2018-04-10 08:56:02 -040098 const uint32_t *chunk_oid_fanout;
99 const unsigned char *chunk_oid_lookup;
100 const unsigned char *chunk_commit_data;
Abhishek Kumare8b63002021-01-16 18:11:15 +0000101 const unsigned char *chunk_generation_data;
102 const unsigned char *chunk_generation_data_overflow;
Jeff Kingee6a7922023-10-09 17:05:47 -0400103 size_t chunk_generation_data_overflow_size;
SZEDER Gábor5af74172019-01-19 21:21:13 +0100104 const unsigned char *chunk_extra_edges;
Jeff King96226102023-10-09 17:05:38 -0400105 size_t chunk_extra_edges_size;
Derrick Stolee118bd572019-06-18 11:14:26 -0700106 const unsigned char *chunk_base_graphs;
Jeff King6cf61d02023-10-09 17:05:41 -0400107 size_t chunk_base_graphs_size;
Garima Singh76ffbca2020-04-06 16:59:49 +0000108 const unsigned char *chunk_bloom_indexes;
109 const unsigned char *chunk_bloom_data;
Jeff King920f4002023-10-09 17:05:50 -0400110 size_t chunk_bloom_data_size;
Garima Singh76ffbca2020-04-06 16:59:49 +0000111
Abhishek Kumar72a2bfc2021-01-16 18:11:12 +0000112 struct topo_level_slab *topo_levels;
Garima Singh76ffbca2020-04-06 16:59:49 +0000113 struct bloom_filter_settings *bloom_filter_settings;
Derrick Stolee2a2e32b2018-04-10 08:56:02 -0400114};
115
Taylor Blauab14d062020-09-09 11:22:56 -0400116struct commit_graph *load_commit_graph_one_fd_st(struct repository *r,
117 int fd, struct stat *st,
Taylor Blaua7df60c2020-02-03 13:18:04 -0800118 struct object_directory *odb);
Jeff King7ed76b42023-09-28 00:38:07 -0400119struct commit_graph *load_commit_graph_chain_fd_st(struct repository *r,
Jeff King5f259192023-09-28 00:39:51 -0400120 int fd, struct stat *st,
121 int *incomplete_chain);
Taylor Blau13c24992020-02-03 13:18:00 -0800122struct commit_graph *read_commit_graph_one(struct repository *r,
123 struct object_directory *odb);
Taylor Blaua92d8522022-07-14 14:43:06 -0700124
125/*
126 * Callers should initialize the repo_settings with prepare_repo_settings()
127 * prior to calling parse_commit_graph().
128 */
129struct commit_graph *parse_commit_graph(struct repo_settings *s,
Taylor Blauab14d062020-09-09 11:22:56 -0400130 void *graph_map, size_t graph_size);
Josh Steadmonaa658572019-01-15 14:25:50 -0800131
Derrick Stolee6cc01742018-07-20 16:33:30 +0000132/*
133 * Return 1 if and only if the repository has a commit-graph
134 * file and generation numbers are computed in that file.
135 */
136int generation_numbers_enabled(struct repository *r);
137
Abhishek Kumar8d00d7c2021-01-16 18:11:17 +0000138/*
139 * Return 1 if and only if the repository has a commit-graph
140 * file and generation data chunk has been written for the file.
141 */
142int corrected_commit_dates_enabled(struct repository *r);
143
Taylor Blau4f364402020-09-09 11:22:44 -0400144struct bloom_filter_settings *get_bloom_filter_settings(struct repository *r);
145
SZEDER Gábor39d88312019-08-05 10:02:39 +0200146enum commit_graph_write_flags {
147 COMMIT_GRAPH_WRITE_APPEND = (1 << 0),
148 COMMIT_GRAPH_WRITE_PROGRESS = (1 << 1),
SZEDER Gábor7c5c9b92019-08-05 10:02:40 +0200149 COMMIT_GRAPH_WRITE_SPLIT = (1 << 2),
Taylor Blau2f00c352020-05-13 15:59:55 -0600150 COMMIT_GRAPH_WRITE_BLOOM_FILTERS = (1 << 3),
Junio C Hamano70cdbbe2020-07-30 13:20:31 -0700151 COMMIT_GRAPH_NO_WRITE_BLOOM_FILTERS = (1 << 4),
SZEDER Gábor39d88312019-08-05 10:02:39 +0200152};
Derrick Stolee5af80392019-06-12 06:29:38 -0700153
Taylor Blau4f027352020-04-13 22:04:08 -0600154enum commit_graph_split_flags {
Taylor Blaufdbde822020-04-13 22:04:12 -0600155 COMMIT_GRAPH_SPLIT_UNSPECIFIED = 0,
Taylor Blau8a6ac282020-04-13 22:04:17 -0600156 COMMIT_GRAPH_SPLIT_MERGE_PROHIBITED = 1,
157 COMMIT_GRAPH_SPLIT_REPLACE = 2
Taylor Blau4f027352020-04-13 22:04:08 -0600158};
159
Taylor Blau98bb7962020-09-17 22:59:49 -0400160struct commit_graph_opts {
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700161 int size_multiple;
162 int max_commits;
163 timestamp_t expire_time;
Taylor Blau98bb7962020-09-17 22:59:49 -0400164 enum commit_graph_split_flags split_flags;
Taylor Blau809e0322020-09-18 09:27:27 -0400165 int max_new_filters;
Derrick Stoleec2bc6e62019-06-18 11:14:32 -0700166};
167
Derrick Stoleee103f722019-06-12 06:29:37 -0700168/*
169 * The write_commit_graph* methods return zero on success
170 * and a negative value on failure. Note that if the repository
171 * is not compatible with the commit-graph feature, then the
172 * methods will return 0 without writing a commit-graph.
173 */
Taylor Blau0bd52e22020-02-03 21:51:50 -0800174int write_commit_graph_reachable(struct object_directory *odb,
SZEDER Gábor39d88312019-08-05 10:02:39 +0200175 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -0400176 const struct commit_graph_opts *opts);
Taylor Blau0bd52e22020-02-03 21:51:50 -0800177int write_commit_graph(struct object_directory *odb,
Ævar Arnfjörð Bjarmason4a047902022-03-04 19:32:12 +0100178 const struct string_list *pack_indexes,
Taylor Blau6830c362020-04-13 22:04:25 -0600179 struct oidset *commits,
SZEDER Gábor39d88312019-08-05 10:02:39 +0200180 enum commit_graph_write_flags flags,
Taylor Blau98bb7962020-09-17 22:59:49 -0400181 const struct commit_graph_opts *opts);
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400182
Derrick Stolee3da4b602019-06-18 11:14:32 -0700183#define COMMIT_GRAPH_VERIFY_SHALLOW (1 << 0)
184
185int verify_commit_graph(struct repository *r, struct commit_graph *g, int flags);
Derrick Stolee283e68c2018-06-27 09:24:32 -0400186
Derrick Stoleec3a3a962019-05-17 11:41:47 -0700187void close_commit_graph(struct raw_object_store *);
Jonathan Tanc3756d52018-07-11 15:42:40 -0700188void free_commit_graph(struct commit_graph *);
189
Jeff King6abada12019-09-12 10:44:45 -0400190/*
191 * Disable further use of the commit graph in this process when parsing a
192 * "struct commit".
193 */
194void disable_commit_graph(struct repository *r);
195
Abhishek Kumar48448122020-06-17 14:44:09 +0530196struct commit_graph_data {
197 uint32_t graph_pos;
Abhishek Kumard7f92782021-01-16 18:11:13 +0000198 timestamp_t generation;
Abhishek Kumar48448122020-06-17 14:44:09 +0530199};
200
201/*
202 * Commits should be parsed before accessing generation, graph positions.
203 */
Abhishek Kumard7f92782021-01-16 18:11:13 +0000204timestamp_t commit_graph_generation(const struct commit *);
Abhishek Kumar48448122020-06-17 14:44:09 +0530205uint32_t commit_graph_position(const struct commit *);
Taylor Blauc08645b2023-03-20 11:26:52 +0000206
207/*
208 * After this method, all commits reachable from those in the given
209 * list will have non-zero, non-infinite generation numbers.
210 */
211void ensure_generations_valid(struct repository *r,
212 struct commit **commits, size_t nr);
213
Derrick Stolee08fd81c2018-04-02 16:34:19 -0400214#endif