A few more topics post 2.40-rc0
[git/debian.git] / repository.c
blob937fa974b38e86a1873a0e6904cd4c35ac5fd8f1
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 "repository.h"
8 #include "object-store.h"
9 #include "config.h"
10 #include "object.h"
11 #include "lockfile.h"
12 #include "remote.h"
13 #include "submodule-config.h"
14 #include "sparse-index.h"
15 #include "promisor-remote.h"
17 /* The main repository */
18 static struct repository the_repo;
19 struct repository *the_repository;
20 struct index_state the_index;
22 void initialize_the_repository(void)
24 the_repository = &the_repo;
26 the_repo.index = &the_index;
27 the_repo.objects = raw_object_store_new();
28 the_repo.remote_state = remote_state_new();
29 the_repo.parsed_objects = parsed_object_pool_new();
31 index_state_init(&the_index, the_repository);
33 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
36 static void expand_base_dir(char **out, const char *in,
37 const char *base_dir, const char *def_in)
39 free(*out);
40 if (in)
41 *out = xstrdup(in);
42 else
43 *out = xstrfmt("%s/%s", base_dir, def_in);
46 static void repo_set_commondir(struct repository *repo,
47 const char *commondir)
49 struct strbuf sb = STRBUF_INIT;
51 free(repo->commondir);
53 if (commondir) {
54 repo->different_commondir = 1;
55 repo->commondir = xstrdup(commondir);
56 return;
59 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
60 repo->commondir = strbuf_detach(&sb, NULL);
63 void repo_set_gitdir(struct repository *repo,
64 const char *root,
65 const struct set_gitdir_args *o)
67 const char *gitfile = read_gitfile(root);
69 * repo->gitdir is saved because the caller could pass "root"
70 * that also points to repo->gitdir. We want to keep it alive
71 * until after xstrdup(root). Then we can free it.
73 char *old_gitdir = repo->gitdir;
75 repo->gitdir = xstrdup(gitfile ? gitfile : root);
76 free(old_gitdir);
78 repo_set_commondir(repo, o->commondir);
80 if (!repo->objects->odb) {
81 CALLOC_ARRAY(repo->objects->odb, 1);
82 repo->objects->odb_tail = &repo->objects->odb->next;
84 expand_base_dir(&repo->objects->odb->path, o->object_dir,
85 repo->commondir, "objects");
87 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
89 free(repo->objects->alternate_db);
90 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
91 expand_base_dir(&repo->graft_file, o->graft_file,
92 repo->commondir, "info/grafts");
93 expand_base_dir(&repo->index_file, o->index_file,
94 repo->gitdir, "index");
97 void repo_set_hash_algo(struct repository *repo, int hash_algo)
99 repo->hash_algo = &hash_algos[hash_algo];
103 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
104 * Return 0 upon success and a non-zero value upon failure.
106 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
108 int ret = 0;
109 int error = 0;
110 char *abspath = NULL;
111 const char *resolved_gitdir;
112 struct set_gitdir_args args = { NULL };
114 abspath = real_pathdup(gitdir, 0);
115 if (!abspath) {
116 ret = -1;
117 goto out;
120 /* 'gitdir' must reference the gitdir directly */
121 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
122 if (!resolved_gitdir) {
123 ret = -1;
124 goto out;
127 repo_set_gitdir(repo, resolved_gitdir, &args);
129 out:
130 free(abspath);
131 return ret;
134 void repo_set_worktree(struct repository *repo, const char *path)
136 repo->worktree = real_pathdup(path, 1);
138 trace2_def_repo(repo);
141 static int read_and_verify_repository_format(struct repository_format *format,
142 const char *commondir)
144 int ret = 0;
145 struct strbuf sb = STRBUF_INIT;
147 strbuf_addf(&sb, "%s/config", commondir);
148 read_repository_format(format, sb.buf);
149 strbuf_reset(&sb);
151 if (verify_repository_format(format, &sb) < 0) {
152 warning("%s", sb.buf);
153 ret = -1;
156 strbuf_release(&sb);
157 return ret;
161 * Initialize 'repo' based on the provided 'gitdir'.
162 * Return 0 upon success and a non-zero value upon failure.
164 int repo_init(struct repository *repo,
165 const char *gitdir,
166 const char *worktree)
168 struct repository_format format = REPOSITORY_FORMAT_INIT;
169 memset(repo, 0, sizeof(*repo));
171 repo->objects = raw_object_store_new();
172 repo->parsed_objects = parsed_object_pool_new();
173 repo->remote_state = remote_state_new();
175 if (repo_init_gitdir(repo, gitdir))
176 goto error;
178 if (read_and_verify_repository_format(&format, repo->commondir))
179 goto error;
181 repo_set_hash_algo(repo, format.hash_algo);
183 /* take ownership of format.partial_clone */
184 repo->repository_format_partial_clone = format.partial_clone;
185 format.partial_clone = NULL;
187 if (worktree)
188 repo_set_worktree(repo, worktree);
190 clear_repository_format(&format);
191 return 0;
193 error:
194 repo_clear(repo);
195 return -1;
198 int repo_submodule_init(struct repository *subrepo,
199 struct repository *superproject,
200 const char *path,
201 const struct object_id *treeish_name)
203 struct strbuf gitdir = STRBUF_INIT;
204 struct strbuf worktree = STRBUF_INIT;
205 int ret = 0;
207 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
208 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
210 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
212 * If initialization fails then it may be due to the submodule
213 * not being populated in the superproject's worktree. Instead
214 * we can try to initialize the submodule by finding it's gitdir
215 * in the superproject's 'modules' directory. In this case the
216 * submodule would not have a worktree.
218 const struct submodule *sub =
219 submodule_from_path(superproject, treeish_name, path);
220 if (!sub) {
221 ret = -1;
222 goto out;
225 strbuf_reset(&gitdir);
226 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
228 if (repo_init(subrepo, gitdir.buf, NULL)) {
229 ret = -1;
230 goto out;
234 subrepo->submodule_prefix = xstrfmt("%s%s/",
235 superproject->submodule_prefix ?
236 superproject->submodule_prefix :
237 "", path);
239 out:
240 strbuf_release(&gitdir);
241 strbuf_release(&worktree);
242 return ret;
245 static void repo_clear_path_cache(struct repo_path_cache *cache)
247 FREE_AND_NULL(cache->squash_msg);
248 FREE_AND_NULL(cache->squash_msg);
249 FREE_AND_NULL(cache->merge_msg);
250 FREE_AND_NULL(cache->merge_rr);
251 FREE_AND_NULL(cache->merge_mode);
252 FREE_AND_NULL(cache->merge_head);
253 FREE_AND_NULL(cache->merge_autostash);
254 FREE_AND_NULL(cache->auto_merge);
255 FREE_AND_NULL(cache->fetch_head);
256 FREE_AND_NULL(cache->shallow);
259 void repo_clear(struct repository *repo)
261 FREE_AND_NULL(repo->gitdir);
262 FREE_AND_NULL(repo->commondir);
263 FREE_AND_NULL(repo->graft_file);
264 FREE_AND_NULL(repo->index_file);
265 FREE_AND_NULL(repo->worktree);
266 FREE_AND_NULL(repo->submodule_prefix);
268 raw_object_store_clear(repo->objects);
269 FREE_AND_NULL(repo->objects);
271 parsed_object_pool_clear(repo->parsed_objects);
272 FREE_AND_NULL(repo->parsed_objects);
274 if (repo->config) {
275 git_configset_clear(repo->config);
276 FREE_AND_NULL(repo->config);
279 if (repo->submodule_cache) {
280 submodule_cache_free(repo->submodule_cache);
281 repo->submodule_cache = NULL;
284 if (repo->index) {
285 discard_index(repo->index);
286 if (repo->index != &the_index)
287 FREE_AND_NULL(repo->index);
290 if (repo->promisor_remote_config) {
291 promisor_remote_clear(repo->promisor_remote_config);
292 FREE_AND_NULL(repo->promisor_remote_config);
295 if (repo->remote_state) {
296 remote_state_clear(repo->remote_state);
297 FREE_AND_NULL(repo->remote_state);
300 repo_clear_path_cache(&repo->cached_paths);
303 int repo_read_index(struct repository *repo)
305 int res;
307 /* Complete the double-reference */
308 if (!repo->index) {
309 ALLOC_ARRAY(repo->index, 1);
310 index_state_init(repo->index, repo);
311 } else if (repo->index->repo != repo) {
312 BUG("repo's index should point back at itself");
315 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
317 prepare_repo_settings(repo);
318 if (repo->settings.command_requires_full_index)
319 ensure_full_index(repo->index);
322 * If sparse checkouts are in use, check whether paths with the
323 * SKIP_WORKTREE attribute are missing from the worktree; if not,
324 * clear that attribute for that path.
326 clear_skip_worktree_from_present_files(repo->index);
328 return res;
331 int repo_hold_locked_index(struct repository *repo,
332 struct lock_file *lf,
333 int flags)
335 if (!repo->index_file)
336 BUG("the repo hasn't been setup");
337 return hold_lock_file_for_update(lf, repo->index_file, flags);