cache.h: remove this no-longer-used header
[git/debian.git] / repository.c
blob2616aabde50063794e6e1070a66cf0af6f6d494e
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 "git-compat-util.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"
21 /* The main repository */
22 static struct repository the_repo;
23 struct repository *the_repository;
24 struct index_state the_index;
26 void initialize_the_repository(void)
28 the_repository = &the_repo;
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();
35 index_state_init(&the_index, the_repository);
37 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
40 static void expand_base_dir(char **out, const char *in,
41 const char *base_dir, const char *def_in)
43 free(*out);
44 if (in)
45 *out = xstrdup(in);
46 else
47 *out = xstrfmt("%s/%s", base_dir, def_in);
50 static void repo_set_commondir(struct repository *repo,
51 const char *commondir)
53 struct strbuf sb = STRBUF_INIT;
55 free(repo->commondir);
57 if (commondir) {
58 repo->different_commondir = 1;
59 repo->commondir = xstrdup(commondir);
60 return;
63 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
64 repo->commondir = strbuf_detach(&sb, NULL);
67 void repo_set_gitdir(struct repository *repo,
68 const char *root,
69 const struct set_gitdir_args *o)
71 const char *gitfile = read_gitfile(root);
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.
77 char *old_gitdir = repo->gitdir;
79 repo->gitdir = xstrdup(gitfile ? gitfile : root);
80 free(old_gitdir);
82 repo_set_commondir(repo, o->commondir);
84 if (!repo->objects->odb) {
85 CALLOC_ARRAY(repo->objects->odb, 1);
86 repo->objects->odb_tail = &repo->objects->odb->next;
88 expand_base_dir(&repo->objects->odb->path, o->object_dir,
89 repo->commondir, "objects");
91 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
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");
101 void repo_set_hash_algo(struct repository *repo, int hash_algo)
103 repo->hash_algo = &hash_algos[hash_algo];
107 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
108 * Return 0 upon success and a non-zero value upon failure.
110 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
112 int ret = 0;
113 int error = 0;
114 char *abspath = NULL;
115 const char *resolved_gitdir;
116 struct set_gitdir_args args = { NULL };
118 abspath = real_pathdup(gitdir, 0);
119 if (!abspath) {
120 ret = -1;
121 goto out;
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;
131 repo_set_gitdir(repo, resolved_gitdir, &args);
133 out:
134 free(abspath);
135 return ret;
138 void repo_set_worktree(struct repository *repo, const char *path)
140 repo->worktree = real_pathdup(path, 1);
142 trace2_def_repo(repo);
145 static int read_and_verify_repository_format(struct repository_format *format,
146 const char *commondir)
148 int ret = 0;
149 struct strbuf sb = STRBUF_INIT;
151 strbuf_addf(&sb, "%s/config", commondir);
152 read_repository_format(format, sb.buf);
153 strbuf_reset(&sb);
155 if (verify_repository_format(format, &sb) < 0) {
156 warning("%s", sb.buf);
157 ret = -1;
160 strbuf_release(&sb);
161 return ret;
165 * Initialize 'repo' based on the provided 'gitdir'.
166 * Return 0 upon success and a non-zero value upon failure.
168 int repo_init(struct repository *repo,
169 const char *gitdir,
170 const char *worktree)
172 struct repository_format format = REPOSITORY_FORMAT_INIT;
173 memset(repo, 0, sizeof(*repo));
175 repo->objects = raw_object_store_new();
176 repo->parsed_objects = parsed_object_pool_new();
177 repo->remote_state = remote_state_new();
179 if (repo_init_gitdir(repo, gitdir))
180 goto error;
182 if (read_and_verify_repository_format(&format, repo->commondir))
183 goto error;
185 repo_set_hash_algo(repo, format.hash_algo);
186 repo->repository_format_worktree_config = format.worktree_config;
188 /* take ownership of format.partial_clone */
189 repo->repository_format_partial_clone = format.partial_clone;
190 format.partial_clone = NULL;
192 if (worktree)
193 repo_set_worktree(repo, worktree);
195 clear_repository_format(&format);
196 return 0;
198 error:
199 repo_clear(repo);
200 return -1;
203 int repo_submodule_init(struct repository *subrepo,
204 struct repository *superproject,
205 const char *path,
206 const struct object_id *treeish_name)
208 struct strbuf gitdir = STRBUF_INIT;
209 struct strbuf worktree = STRBUF_INIT;
210 int ret = 0;
212 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
213 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
215 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
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.
223 const struct submodule *sub =
224 submodule_from_path(superproject, treeish_name, path);
225 if (!sub) {
226 ret = -1;
227 goto out;
230 strbuf_reset(&gitdir);
231 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
233 if (repo_init(subrepo, gitdir.buf, NULL)) {
234 ret = -1;
235 goto out;
239 subrepo->submodule_prefix = xstrfmt("%s%s/",
240 superproject->submodule_prefix ?
241 superproject->submodule_prefix :
242 "", path);
244 out:
245 strbuf_release(&gitdir);
246 strbuf_release(&worktree);
247 return ret;
250 static void repo_clear_path_cache(struct repo_path_cache *cache)
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);
264 void repo_clear(struct repository *repo)
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);
273 raw_object_store_clear(repo->objects);
274 FREE_AND_NULL(repo->objects);
276 parsed_object_pool_clear(repo->parsed_objects);
277 FREE_AND_NULL(repo->parsed_objects);
279 if (repo->config) {
280 git_configset_clear(repo->config);
281 FREE_AND_NULL(repo->config);
284 if (repo->submodule_cache) {
285 submodule_cache_free(repo->submodule_cache);
286 repo->submodule_cache = NULL;
289 if (repo->index) {
290 discard_index(repo->index);
291 if (repo->index != &the_index)
292 FREE_AND_NULL(repo->index);
295 if (repo->promisor_remote_config) {
296 promisor_remote_clear(repo->promisor_remote_config);
297 FREE_AND_NULL(repo->promisor_remote_config);
300 if (repo->remote_state) {
301 remote_state_clear(repo->remote_state);
302 FREE_AND_NULL(repo->remote_state);
305 repo_clear_path_cache(&repo->cached_paths);
308 int repo_read_index(struct repository *repo)
310 int res;
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");
320 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
322 prepare_repo_settings(repo);
323 if (repo->settings.command_requires_full_index)
324 ensure_full_index(repo->index);
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.
331 clear_skip_worktree_from_present_files(repo->index);
333 return res;
336 int repo_hold_locked_index(struct repository *repo,
337 struct lock_file *lf,
338 int flags)
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);