Raw
1 #ifndef REPOSITORY_H
2 #define REPOSITORY_H
3
4 #include "strmap.h"
5 #include "string-list.h"
6 #include "repo-settings.h"
7 #include "environment.h"
8
9 struct config_set;
10 struct diff_provider;
11 struct git_hash_algo;
12 struct index_state;
13 struct lock_file;
14 struct pathspec;
15 struct object_database;
16 struct submodule_cache;
17 struct promisor_remote_config;
18 struct remote_state;
19
20 enum ref_storage_format {
21 REF_STORAGE_FORMAT_UNKNOWN,
22 REF_STORAGE_FORMAT_FILES,
23 REF_STORAGE_FORMAT_REFTABLE,
24 };
25
26 #ifdef WITH_BREAKING_CHANGES /* Git 3.0 */
27 # define REF_STORAGE_FORMAT_DEFAULT REF_STORAGE_FORMAT_REFTABLE
28 #else
29 # define REF_STORAGE_FORMAT_DEFAULT REF_STORAGE_FORMAT_FILES
30 #endif
31
32 struct repo_path_cache {
33 char *squash_msg;
34 char *merge_msg;
35 char *merge_rr;
36 char *merge_mode;
37 char *merge_head;
38 char *fetch_head;
39 char *shallow;
40 };
41
42 struct repository {
43 /* Environment */
44 /*
45 * Path to the git directory.
46 * Cannot be NULL after initialization.
47 */
48 char *gitdir;
49
50 /*
51 * Path to the common git directory.
52 * Cannot be NULL after initialization.
53 */
54 char *commondir;
55
56 /*
57 * The "prefix", a path to the current working directory relative to
58 * the work tree root, or NULL, if the current working directory is not
59 * a strict subdirectory of the work tree root. The prefix always ends
60 * with a '/' character.
61 */
62 char *prefix;
63
64 /*
65 * Holds any information related to accessing the raw object content.
66 */
67 struct object_database *objects;
68
69 /*
70 * All objects in this repository that have been parsed. This structure
71 * owns all objects it references, so users of "struct object *"
72 * generally do not need to free them; instead, when a repository is no
73 * longer used, call parsed_object_pool_clear() on this structure, which
74 * is called by the repositories repo_clear on its desconstruction.
75 */
76 struct parsed_object_pool *parsed_objects;
77
78 /*
79 * The store in which the refs are held. This should generally only be
80 * accessed via get_main_ref_store(), as that will lazily initialize
81 * the ref object.
82 */
83 struct ref_store *refs_private;
84
85 /*
86 * Disable ref updates. This is especially used in contexts where
87 * transactions may still be rolled back so that we don't start to
88 * reference objects that may vanish.
89 */
90 bool disable_ref_updates;
91
92 /*
93 * A strmap of ref_stores, stored by submodule name, accessible via
94 * `repo_get_submodule_ref_store()`.
95 */
96 struct strmap submodule_ref_stores;
97
98 /*
99 * A strmap of ref_stores, stored by worktree id, accessible via
100 * `get_worktree_ref_store()`.
101 */
102 struct strmap worktree_ref_stores;
103
104 /*
105 * Contains path to often used file names.
106 */
107 struct repo_path_cache cached_paths;
108
109 /*
110 * Path to the repository's graft file.
111 * Cannot be NULL after initialization.
112 */
113 char *graft_file;
114
115 /*
116 * Path to the current worktree's index file.
117 * Cannot be NULL after initialization.
118 */
119 char *index_file;
120
121 /*
122 * Path to the working directory.
123 * A NULL value indicates that there is no working directory.
124 */
125 char *worktree;
126 bool worktree_initialized;
127 bool worktree_config_is_bogus;
128
129 /*
130 * Whether the repository is bare, as set by "core.bare" config or
131 * inferred during repository discovery. -1 means unset/unknown, 0
132 * means non-bare, 1 means bare.
133 */
134 int bare_cfg;
135
136 /*
137 * Path from the root of the top-level superproject down to this
138 * repository. This is only non-NULL if the repository is initialized
139 * as a submodule of another repository.
140 */
141 char *submodule_prefix;
142
143 struct repo_settings settings;
144
145 /* Subsystems */
146 /*
147 * Repository's config which contains key-value pairs from the usual
148 * set of config files (i.e. repo specific .git/config, user wide
149 * ~/.gitconfig, XDG config file and the global /etc/gitconfig)
150 */
151 struct config_set *config;
152
153 /* Repository's submodule config as defined by '.gitmodules' */
154 struct submodule_cache *submodule_cache;
155
156 /*
157 * Repository's in-memory index.
158 * 'repo_read_index()' can be used to populate 'index'.
159 */
160 struct index_state *index;
161
162 /* Repository's remotes and associated structures. */
163 struct remote_state *remote_state;
164
165 /*
166 * The repository's diff hunk provider chain, NULL until the
167 * first consultation builds it (diff-provider.c); repo_clear()
168 * releases it.
169 */
170 struct diff_provider *diff_providers;
171
172 /* Repository's current hash algorithm, as serialized on disk. */
173 const struct git_hash_algo *hash_algo;
174
175 /* Repository's compatibility hash algorithm. */
176 const struct git_hash_algo *compat_hash_algo;
177
178 /* Repository-specific configuration values. */
179 struct repo_config_values config_values_private_;
180
181 /* Repository's reference storage format, as serialized on disk. */
182 enum ref_storage_format ref_storage_format;
183 /*
184 * Reference storage information as needed for the backend. This contains
185 * only the payload from the reference URI without the schema.
186 */
187 char *ref_storage_payload;
188
189 /* A unique-id for tracing purposes. */
190 int trace2_repo_id;
191
192 /* True if commit-graph has been disabled within this process. */
193 int commit_graph_disabled;
194
195 /*
196 * Lazily-populated cache mapping hook event names to configured hooks.
197 * NULL until first hook use.
198 */
199 struct strmap *hook_config_cache;
200
201 /* Cached value of hook.jobs config (0 if unset, defaults to serial). */
202 unsigned int hook_jobs;
203
204 /* Cached map of event-name -> jobs count (as uintptr_t) from hook.<event>.jobs. */
205 struct strmap event_jobs;
206
207 /* Cached list of event names with hook.<event>.enabled = false. */
208 struct string_list disabled_events;
209
210 /* Configurations related to promisor remotes. */
211 char *repository_format_partial_clone;
212 struct promisor_remote_config *promisor_remote_config;
213
214 /* Configurations */
215 int repository_format_worktree_config;
216 int repository_format_relative_worktrees;
217 int repository_format_precious_objects;
218 int repository_format_submodule_path_cfg;
219
220 /* Indicate if a repository has a different 'commondir' from 'gitdir' */
221 unsigned different_commondir:1;
222
223 /* Should repo_config() check for deprecated settings */
224 bool check_deprecated_config;
225
226 /* Has this repository instance been initialized? */
227 bool initialized;
228 };
229
230 #ifdef USE_THE_REPOSITORY_VARIABLE
231 extern struct repository *the_repository;
232 #endif
233
234 const char *repo_get_git_dir(struct repository *repo);
235 const char *repo_get_common_dir(struct repository *repo);
236 const char *repo_get_object_directory(struct repository *repo);
237 const char *repo_get_index_file(struct repository *repo);
238 const char *repo_get_graft_file(struct repository *repo);
239 const char *repo_get_work_tree(struct repository *repo);
240
241 /*
242 * Define a custom repository layout. Any field can be NULL, which
243 * will default back to the path according to the default layout.
244 */
245 struct set_gitdir_args {
246 const char *commondir;
247 const char *graft_file;
248 const char *index_file;
249 bool disable_ref_updates;
250 };
251
252 void repo_set_gitdir(struct repository *repo, const char *root,
253 const struct set_gitdir_args *extra_args);
254 void repo_set_worktree(struct repository *repo, const char *path);
255 void repo_set_hash_algo(struct repository *repo, uint32_t algo);
256 void repo_set_compat_hash_algo(struct repository *repo, uint32_t compat_algo);
257 void repo_set_ref_storage_format(struct repository *repo,
258 enum ref_storage_format format,
259 const char *payload);
260 void initialize_repository(struct repository *repo);
261 RESULT_MUST_BE_USED
262 int repo_init(struct repository *r, const char *gitdir, const char *worktree);
263
264 /*
265 * Initialize the repository 'subrepo' as the submodule at the given path. If
266 * the submodule's gitdir cannot be found at <path>/.git, this function calls
267 * submodule_from_path() to try to find it. treeish_name is only used if
268 * submodule_from_path() needs to be called; see its documentation for more
269 * information.
270 * Return 0 upon success and a non-zero value upon failure.
271 */
272 struct object_id;
273 RESULT_MUST_BE_USED
274 int repo_submodule_init(struct repository *subrepo,
275 struct repository *superproject,
276 const char *path,
277 const struct object_id *treeish_name);
278 void repo_clear(struct repository *repo);
279
280 /*
281 * Populates the repository's index from its index_file, an index struct will
282 * be allocated if needed.
283 *
284 * Return the number of index entries in the populated index or a value less
285 * than zero if an error occurred. If the repository's index has already been
286 * populated then the number of entries will simply be returned.
287 */
288 int repo_read_index(struct repository *repo);
289 int repo_hold_locked_index(struct repository *repo,
290 struct lock_file *lf,
291 int flags);
292
293 int repo_read_index_unmerged(struct repository *);
294 /*
295 * Opportunistically update the index but do not complain if we can't.
296 * The lockfile is always committed or rolled back.
297 */
298 void repo_update_index_if_able(struct repository *, struct lock_file *);
299
300 /*
301 * Return 1 if upgrade repository format to target_version succeeded,
302 * 0 if no upgrade is necessary, and -1 when upgrade is not possible.
303 */
304 int upgrade_repository_format(struct repository *repo, int target_version);
305
306 #endif /* REPOSITORY_H */