at commit a0189536

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 * Controls whether the repository should lazily fetch missing
189 * objects from promisor remotes. Defaults to 1.
190 */
191 int fetch_if_missing;
192
193 /*
194 * Lazily-populated cache mapping hook event names to configured hooks.
195 * NULL until first hook use.
196 */
197 struct strmap *hook_config_cache;
198
199 /* Cached value of hook.jobs config (0 if unset, defaults to serial). */
200 unsigned int hook_jobs;
201
202 /* Cached map of event-name -> jobs count (as uintptr_t) from hook.<event>.jobs. */
203 struct strmap event_jobs;
204
205 /* Cached list of event names with hook.<event>.enabled = false. */
206 struct string_list disabled_events;
207
208 /* Configurations related to promisor remotes. */
209 char *repository_format_partial_clone;
210 struct promisor_remote_config *promisor_remote_config;
211
212 /* Configurations */
213 int repository_format_worktree_config;
214 int repository_format_relative_worktrees;
215 int repository_format_precious_objects;
216 int repository_format_submodule_path_cfg;
217
218 /* Indicate if a repository has a different 'commondir' from 'gitdir' */
219 unsigned different_commondir:1;
220
221 /* Should repo_config() check for deprecated settings */
222 bool check_deprecated_config;
223
224 /* Has this repository instance been initialized? */
225 bool initialized;
226 };
227
228 #ifdef USE_THE_REPOSITORY_VARIABLE
229 extern struct repository *the_repository;
230 #endif
231
232 const char *repo_get_git_dir(struct repository *repo);
233 const char *repo_get_common_dir(struct repository *repo);
234 const char *repo_get_object_directory(struct repository *repo);
235 const char *repo_get_index_file(struct repository *repo);
236 const char *repo_get_graft_file(struct repository *repo);
237 const char *repo_get_work_tree(struct repository *repo);
238
239 /*
240 * Define a custom repository layout. Any field can be NULL, which
241 * will default back to the path according to the default layout.
242 */
243 struct set_gitdir_args {
244 const char *commondir;
245 const char *graft_file;
246 const char *index_file;
247 bool disable_ref_updates;
248 };
249
250 void repo_set_gitdir(struct repository *repo, const char *root,
251 const struct set_gitdir_args *extra_args);
252 void repo_set_worktree(struct repository *repo, const char *path);
253 void repo_set_hash_algo(struct repository *repo, uint32_t algo);
254 void repo_set_compat_hash_algo(struct repository *repo, uint32_t compat_algo);
255 void repo_set_ref_storage_format(struct repository *repo,
256 enum ref_storage_format format,
257 const char *payload);
258 void initialize_repository(struct repository *repo);
259 RESULT_MUST_BE_USED
260 int repo_init(struct repository *r, const char *gitdir, const char *worktree);
261
262 /*
263 * Initialize the repository 'subrepo' as the submodule at the given path. If
264 * the submodule's gitdir cannot be found at <path>/.git, this function calls
265 * submodule_from_path() to try to find it. treeish_name is only used if
266 * submodule_from_path() needs to be called; see its documentation for more
267 * information.
268 * Return 0 upon success and a non-zero value upon failure.
269 */
270 struct object_id;
271 RESULT_MUST_BE_USED
272 int repo_submodule_init(struct repository *subrepo,
273 struct repository *superproject,
274 const char *path,
275 const struct object_id *treeish_name);
276 void repo_clear(struct repository *repo);
277
278 /*
279 * Populates the repository's index from its index_file, an index struct will
280 * be allocated if needed.
281 *
282 * Return the number of index entries in the populated index or a value less
283 * than zero if an error occurred. If the repository's index has already been
284 * populated then the number of entries will simply be returned.
285 */
286 int repo_read_index(struct repository *repo);
287 int repo_hold_locked_index(struct repository *repo,
288 struct lock_file *lf,
289 int flags);
290
291 int repo_read_index_unmerged(struct repository *);
292 /*
293 * Opportunistically update the index but do not complain if we can't.
294 * The lockfile is always committed or rolled back.
295 */
296 void repo_update_index_if_able(struct repository *, struct lock_file *);
297
298 /*
299 * Return 1 if upgrade repository format to target_version succeeded,
300 * 0 if no upgrade is necessary, and -1 when upgrade is not possible.
301 */
302 int upgrade_repository_format(struct repository *repo, int target_version);
303
304 #endif /* REPOSITORY_H */