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