]> git.ipfire.org Git - thirdparty/git.git/blob - repository.c
read-cache*.h: move declarations for read-cache.c functions from cache.h
[thirdparty/git.git] / repository.c
1 /*
2 * not really _using_ the compat macros, just make sure the_index
3 * declaration matches the definition in this file.
4 */
5 #define USE_THE_INDEX_VARIABLE
6 #include "cache.h"
7 #include "abspath.h"
8 #include "repository.h"
9 #include "object-store.h"
10 #include "config.h"
11 #include "object.h"
12 #include "lockfile.h"
13 #include "read-cache-ll.h"
14 #include "remote.h"
15 #include "setup.h"
16 #include "submodule-config.h"
17 #include "sparse-index.h"
18 #include "trace2.h"
19 #include "promisor-remote.h"
20
21 /* The main repository */
22 static struct repository the_repo;
23 struct repository *the_repository;
24 struct index_state the_index;
25
26 void initialize_the_repository(void)
27 {
28 the_repository = &the_repo;
29
30 the_repo.index = &the_index;
31 the_repo.objects = raw_object_store_new();
32 the_repo.remote_state = remote_state_new();
33 the_repo.parsed_objects = parsed_object_pool_new();
34
35 index_state_init(&the_index, the_repository);
36
37 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
38 }
39
40 static void expand_base_dir(char **out, const char *in,
41 const char *base_dir, const char *def_in)
42 {
43 free(*out);
44 if (in)
45 *out = xstrdup(in);
46 else
47 *out = xstrfmt("%s/%s", base_dir, def_in);
48 }
49
50 static void repo_set_commondir(struct repository *repo,
51 const char *commondir)
52 {
53 struct strbuf sb = STRBUF_INIT;
54
55 free(repo->commondir);
56
57 if (commondir) {
58 repo->different_commondir = 1;
59 repo->commondir = xstrdup(commondir);
60 return;
61 }
62
63 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
64 repo->commondir = strbuf_detach(&sb, NULL);
65 }
66
67 void repo_set_gitdir(struct repository *repo,
68 const char *root,
69 const struct set_gitdir_args *o)
70 {
71 const char *gitfile = read_gitfile(root);
72 /*
73 * repo->gitdir is saved because the caller could pass "root"
74 * that also points to repo->gitdir. We want to keep it alive
75 * until after xstrdup(root). Then we can free it.
76 */
77 char *old_gitdir = repo->gitdir;
78
79 repo->gitdir = xstrdup(gitfile ? gitfile : root);
80 free(old_gitdir);
81
82 repo_set_commondir(repo, o->commondir);
83
84 if (!repo->objects->odb) {
85 CALLOC_ARRAY(repo->objects->odb, 1);
86 repo->objects->odb_tail = &repo->objects->odb->next;
87 }
88 expand_base_dir(&repo->objects->odb->path, o->object_dir,
89 repo->commondir, "objects");
90
91 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
92
93 free(repo->objects->alternate_db);
94 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
95 expand_base_dir(&repo->graft_file, o->graft_file,
96 repo->commondir, "info/grafts");
97 expand_base_dir(&repo->index_file, o->index_file,
98 repo->gitdir, "index");
99 }
100
101 void repo_set_hash_algo(struct repository *repo, int hash_algo)
102 {
103 repo->hash_algo = &hash_algos[hash_algo];
104 }
105
106 /*
107 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
108 * Return 0 upon success and a non-zero value upon failure.
109 */
110 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
111 {
112 int ret = 0;
113 int error = 0;
114 char *abspath = NULL;
115 const char *resolved_gitdir;
116 struct set_gitdir_args args = { NULL };
117
118 abspath = real_pathdup(gitdir, 0);
119 if (!abspath) {
120 ret = -1;
121 goto out;
122 }
123
124 /* 'gitdir' must reference the gitdir directly */
125 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
126 if (!resolved_gitdir) {
127 ret = -1;
128 goto out;
129 }
130
131 repo_set_gitdir(repo, resolved_gitdir, &args);
132
133 out:
134 free(abspath);
135 return ret;
136 }
137
138 void repo_set_worktree(struct repository *repo, const char *path)
139 {
140 repo->worktree = real_pathdup(path, 1);
141
142 trace2_def_repo(repo);
143 }
144
145 static int read_and_verify_repository_format(struct repository_format *format,
146 const char *commondir)
147 {
148 int ret = 0;
149 struct strbuf sb = STRBUF_INIT;
150
151 strbuf_addf(&sb, "%s/config", commondir);
152 read_repository_format(format, sb.buf);
153 strbuf_reset(&sb);
154
155 if (verify_repository_format(format, &sb) < 0) {
156 warning("%s", sb.buf);
157 ret = -1;
158 }
159
160 strbuf_release(&sb);
161 return ret;
162 }
163
164 /*
165 * Initialize 'repo' based on the provided 'gitdir'.
166 * Return 0 upon success and a non-zero value upon failure.
167 */
168 int repo_init(struct repository *repo,
169 const char *gitdir,
170 const char *worktree)
171 {
172 struct repository_format format = REPOSITORY_FORMAT_INIT;
173 memset(repo, 0, sizeof(*repo));
174
175 repo->objects = raw_object_store_new();
176 repo->parsed_objects = parsed_object_pool_new();
177 repo->remote_state = remote_state_new();
178
179 if (repo_init_gitdir(repo, gitdir))
180 goto error;
181
182 if (read_and_verify_repository_format(&format, repo->commondir))
183 goto error;
184
185 repo_set_hash_algo(repo, format.hash_algo);
186 repo->repository_format_worktree_config = format.worktree_config;
187
188 /* take ownership of format.partial_clone */
189 repo->repository_format_partial_clone = format.partial_clone;
190 format.partial_clone = NULL;
191
192 if (worktree)
193 repo_set_worktree(repo, worktree);
194
195 clear_repository_format(&format);
196 return 0;
197
198 error:
199 repo_clear(repo);
200 return -1;
201 }
202
203 int repo_submodule_init(struct repository *subrepo,
204 struct repository *superproject,
205 const char *path,
206 const struct object_id *treeish_name)
207 {
208 struct strbuf gitdir = STRBUF_INIT;
209 struct strbuf worktree = STRBUF_INIT;
210 int ret = 0;
211
212 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
213 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
214
215 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
216 /*
217 * If initialization fails then it may be due to the submodule
218 * not being populated in the superproject's worktree. Instead
219 * we can try to initialize the submodule by finding it's gitdir
220 * in the superproject's 'modules' directory. In this case the
221 * submodule would not have a worktree.
222 */
223 const struct submodule *sub =
224 submodule_from_path(superproject, treeish_name, path);
225 if (!sub) {
226 ret = -1;
227 goto out;
228 }
229
230 strbuf_reset(&gitdir);
231 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
232
233 if (repo_init(subrepo, gitdir.buf, NULL)) {
234 ret = -1;
235 goto out;
236 }
237 }
238
239 subrepo->submodule_prefix = xstrfmt("%s%s/",
240 superproject->submodule_prefix ?
241 superproject->submodule_prefix :
242 "", path);
243
244 out:
245 strbuf_release(&gitdir);
246 strbuf_release(&worktree);
247 return ret;
248 }
249
250 static void repo_clear_path_cache(struct repo_path_cache *cache)
251 {
252 FREE_AND_NULL(cache->squash_msg);
253 FREE_AND_NULL(cache->squash_msg);
254 FREE_AND_NULL(cache->merge_msg);
255 FREE_AND_NULL(cache->merge_rr);
256 FREE_AND_NULL(cache->merge_mode);
257 FREE_AND_NULL(cache->merge_head);
258 FREE_AND_NULL(cache->merge_autostash);
259 FREE_AND_NULL(cache->auto_merge);
260 FREE_AND_NULL(cache->fetch_head);
261 FREE_AND_NULL(cache->shallow);
262 }
263
264 void repo_clear(struct repository *repo)
265 {
266 FREE_AND_NULL(repo->gitdir);
267 FREE_AND_NULL(repo->commondir);
268 FREE_AND_NULL(repo->graft_file);
269 FREE_AND_NULL(repo->index_file);
270 FREE_AND_NULL(repo->worktree);
271 FREE_AND_NULL(repo->submodule_prefix);
272
273 raw_object_store_clear(repo->objects);
274 FREE_AND_NULL(repo->objects);
275
276 parsed_object_pool_clear(repo->parsed_objects);
277 FREE_AND_NULL(repo->parsed_objects);
278
279 if (repo->config) {
280 git_configset_clear(repo->config);
281 FREE_AND_NULL(repo->config);
282 }
283
284 if (repo->submodule_cache) {
285 submodule_cache_free(repo->submodule_cache);
286 repo->submodule_cache = NULL;
287 }
288
289 if (repo->index) {
290 discard_index(repo->index);
291 if (repo->index != &the_index)
292 FREE_AND_NULL(repo->index);
293 }
294
295 if (repo->promisor_remote_config) {
296 promisor_remote_clear(repo->promisor_remote_config);
297 FREE_AND_NULL(repo->promisor_remote_config);
298 }
299
300 if (repo->remote_state) {
301 remote_state_clear(repo->remote_state);
302 FREE_AND_NULL(repo->remote_state);
303 }
304
305 repo_clear_path_cache(&repo->cached_paths);
306 }
307
308 int repo_read_index(struct repository *repo)
309 {
310 int res;
311
312 /* Complete the double-reference */
313 if (!repo->index) {
314 ALLOC_ARRAY(repo->index, 1);
315 index_state_init(repo->index, repo);
316 } else if (repo->index->repo != repo) {
317 BUG("repo's index should point back at itself");
318 }
319
320 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
321
322 prepare_repo_settings(repo);
323 if (repo->settings.command_requires_full_index)
324 ensure_full_index(repo->index);
325
326 /*
327 * If sparse checkouts are in use, check whether paths with the
328 * SKIP_WORKTREE attribute are missing from the worktree; if not,
329 * clear that attribute for that path.
330 */
331 clear_skip_worktree_from_present_files(repo->index);
332
333 return res;
334 }
335
336 int repo_hold_locked_index(struct repository *repo,
337 struct lock_file *lf,
338 int flags)
339 {
340 if (!repo->index_file)
341 BUG("the repo hasn't been setup");
342 return hold_lock_file_for_update(lf, repo->index_file, flags);
343 }