setup: unset ref storage when reinitializing repository version
[git.git] / repository.c
blobd29b0304fbd32ecfb6d8f63060cc5e1577f3db0d
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "repository.h"
4 #include "object-store-ll.h"
5 #include "config.h"
6 #include "object.h"
7 #include "lockfile.h"
8 #include "path.h"
9 #include "read-cache-ll.h"
10 #include "remote.h"
11 #include "setup.h"
12 #include "loose.h"
13 #include "submodule-config.h"
14 #include "sparse-index.h"
15 #include "trace2.h"
16 #include "promisor-remote.h"
17 #include "refs.h"
19 /* The main repository */
20 static struct repository the_repo;
21 struct repository *the_repository = &the_repo;
23 void initialize_repository(struct repository *repo)
25 repo->objects = raw_object_store_new();
26 repo->remote_state = remote_state_new();
27 repo->parsed_objects = parsed_object_pool_new();
28 ALLOC_ARRAY(repo->index, 1);
29 index_state_init(repo->index, repo);
32 * Unfortunately, we need to keep this hack around for the time being:
34 * - Not setting up the hash algorithm for `the_repository` leads to
35 * crashes because `the_hash_algo` is a macro that expands to
36 * `the_repository->hash_algo`. So if Git commands try to access
37 * `the_hash_algo` without a Git directory we crash.
39 * - Setting up the hash algorithm to be SHA1 by default breaks other
40 * commands when running with SHA256.
42 * This is another point in case why having global state is a bad idea.
43 * Eventually, we should remove this hack and stop setting the hash
44 * algorithm in this function altogether. Instead, it should only ever
45 * be set via our repository setup procedures. But that requires more
46 * work.
48 if (repo == the_repository)
49 repo_set_hash_algo(repo, GIT_HASH_SHA1);
52 static void expand_base_dir(char **out, const char *in,
53 const char *base_dir, const char *def_in)
55 free(*out);
56 if (in)
57 *out = xstrdup(in);
58 else
59 *out = xstrfmt("%s/%s", base_dir, def_in);
62 static void repo_set_commondir(struct repository *repo,
63 const char *commondir)
65 struct strbuf sb = STRBUF_INIT;
67 free(repo->commondir);
69 if (commondir) {
70 repo->different_commondir = 1;
71 repo->commondir = xstrdup(commondir);
72 return;
75 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
76 repo->commondir = strbuf_detach(&sb, NULL);
79 void repo_set_gitdir(struct repository *repo,
80 const char *root,
81 const struct set_gitdir_args *o)
83 const char *gitfile = read_gitfile(root);
85 * repo->gitdir is saved because the caller could pass "root"
86 * that also points to repo->gitdir. We want to keep it alive
87 * until after xstrdup(root). Then we can free it.
89 char *old_gitdir = repo->gitdir;
91 repo->gitdir = xstrdup(gitfile ? gitfile : root);
92 free(old_gitdir);
94 repo_set_commondir(repo, o->commondir);
96 if (!repo->objects->odb) {
97 CALLOC_ARRAY(repo->objects->odb, 1);
98 repo->objects->odb_tail = &repo->objects->odb->next;
100 expand_base_dir(&repo->objects->odb->path, o->object_dir,
101 repo->commondir, "objects");
103 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
105 free(repo->objects->alternate_db);
106 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
107 expand_base_dir(&repo->graft_file, o->graft_file,
108 repo->commondir, "info/grafts");
109 expand_base_dir(&repo->index_file, o->index_file,
110 repo->gitdir, "index");
113 void repo_set_hash_algo(struct repository *repo, int hash_algo)
115 repo->hash_algo = &hash_algos[hash_algo];
118 void repo_set_compat_hash_algo(struct repository *repo, int algo)
120 if (hash_algo_by_ptr(repo->hash_algo) == algo)
121 BUG("hash_algo and compat_hash_algo match");
122 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
123 if (repo->compat_hash_algo)
124 repo_read_loose_object_map(repo);
127 void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
129 repo->ref_storage_format = format;
133 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
134 * Return 0 upon success and a non-zero value upon failure.
136 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
138 int ret = 0;
139 int error = 0;
140 char *abspath = NULL;
141 const char *resolved_gitdir;
142 struct set_gitdir_args args = { NULL };
144 abspath = real_pathdup(gitdir, 0);
145 if (!abspath) {
146 ret = -1;
147 goto out;
150 /* 'gitdir' must reference the gitdir directly */
151 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
152 if (!resolved_gitdir) {
153 ret = -1;
154 goto out;
157 repo_set_gitdir(repo, resolved_gitdir, &args);
159 out:
160 free(abspath);
161 return ret;
164 void repo_set_worktree(struct repository *repo, const char *path)
166 repo->worktree = real_pathdup(path, 1);
168 trace2_def_repo(repo);
171 static int read_and_verify_repository_format(struct repository_format *format,
172 const char *commondir)
174 int ret = 0;
175 struct strbuf sb = STRBUF_INIT;
177 strbuf_addf(&sb, "%s/config", commondir);
178 read_repository_format(format, sb.buf);
179 strbuf_reset(&sb);
181 if (verify_repository_format(format, &sb) < 0) {
182 warning("%s", sb.buf);
183 ret = -1;
186 strbuf_release(&sb);
187 return ret;
191 * Initialize 'repo' based on the provided 'gitdir'.
192 * Return 0 upon success and a non-zero value upon failure.
194 int repo_init(struct repository *repo,
195 const char *gitdir,
196 const char *worktree)
198 struct repository_format format = REPOSITORY_FORMAT_INIT;
199 memset(repo, 0, sizeof(*repo));
201 initialize_repository(repo);
203 if (repo_init_gitdir(repo, gitdir))
204 goto error;
206 if (read_and_verify_repository_format(&format, repo->commondir))
207 goto error;
209 repo_set_hash_algo(repo, format.hash_algo);
210 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
211 repo_set_ref_storage_format(repo, format.ref_storage_format);
212 repo->repository_format_worktree_config = format.worktree_config;
214 /* take ownership of format.partial_clone */
215 repo->repository_format_partial_clone = format.partial_clone;
216 format.partial_clone = NULL;
218 if (worktree)
219 repo_set_worktree(repo, worktree);
221 if (repo->compat_hash_algo)
222 repo_read_loose_object_map(repo);
224 clear_repository_format(&format);
225 return 0;
227 error:
228 repo_clear(repo);
229 return -1;
232 int repo_submodule_init(struct repository *subrepo,
233 struct repository *superproject,
234 const char *path,
235 const struct object_id *treeish_name)
237 struct strbuf gitdir = STRBUF_INIT;
238 struct strbuf worktree = STRBUF_INIT;
239 int ret = 0;
241 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
242 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
244 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
246 * If initialization fails then it may be due to the submodule
247 * not being populated in the superproject's worktree. Instead
248 * we can try to initialize the submodule by finding it's gitdir
249 * in the superproject's 'modules' directory. In this case the
250 * submodule would not have a worktree.
252 const struct submodule *sub =
253 submodule_from_path(superproject, treeish_name, path);
254 if (!sub) {
255 ret = -1;
256 goto out;
259 strbuf_reset(&gitdir);
260 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
262 if (repo_init(subrepo, gitdir.buf, NULL)) {
263 ret = -1;
264 goto out;
268 subrepo->submodule_prefix = xstrfmt("%s%s/",
269 superproject->submodule_prefix ?
270 superproject->submodule_prefix :
271 "", path);
273 out:
274 strbuf_release(&gitdir);
275 strbuf_release(&worktree);
276 return ret;
279 static void repo_clear_path_cache(struct repo_path_cache *cache)
281 FREE_AND_NULL(cache->squash_msg);
282 FREE_AND_NULL(cache->squash_msg);
283 FREE_AND_NULL(cache->merge_msg);
284 FREE_AND_NULL(cache->merge_rr);
285 FREE_AND_NULL(cache->merge_mode);
286 FREE_AND_NULL(cache->merge_head);
287 FREE_AND_NULL(cache->fetch_head);
288 FREE_AND_NULL(cache->shallow);
291 void repo_clear(struct repository *repo)
293 struct hashmap_iter iter;
294 struct strmap_entry *e;
296 FREE_AND_NULL(repo->gitdir);
297 FREE_AND_NULL(repo->commondir);
298 FREE_AND_NULL(repo->graft_file);
299 FREE_AND_NULL(repo->index_file);
300 FREE_AND_NULL(repo->worktree);
301 FREE_AND_NULL(repo->submodule_prefix);
303 raw_object_store_clear(repo->objects);
304 FREE_AND_NULL(repo->objects);
306 parsed_object_pool_clear(repo->parsed_objects);
307 FREE_AND_NULL(repo->parsed_objects);
309 FREE_AND_NULL(repo->settings.fsmonitor);
311 if (repo->config) {
312 git_configset_clear(repo->config);
313 FREE_AND_NULL(repo->config);
316 if (repo->submodule_cache) {
317 submodule_cache_free(repo->submodule_cache);
318 repo->submodule_cache = NULL;
321 if (repo->index) {
322 discard_index(repo->index);
323 FREE_AND_NULL(repo->index);
326 if (repo->promisor_remote_config) {
327 promisor_remote_clear(repo->promisor_remote_config);
328 FREE_AND_NULL(repo->promisor_remote_config);
331 if (repo->remote_state) {
332 remote_state_clear(repo->remote_state);
333 FREE_AND_NULL(repo->remote_state);
336 strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
337 ref_store_release(e->value);
338 strmap_clear(&repo->submodule_ref_stores, 1);
340 strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
341 ref_store_release(e->value);
342 strmap_clear(&repo->worktree_ref_stores, 1);
344 repo_clear_path_cache(&repo->cached_paths);
347 int repo_read_index(struct repository *repo)
349 int res;
351 /* Complete the double-reference */
352 if (!repo->index) {
353 ALLOC_ARRAY(repo->index, 1);
354 index_state_init(repo->index, repo);
355 } else if (repo->index->repo != repo) {
356 BUG("repo's index should point back at itself");
359 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
361 prepare_repo_settings(repo);
362 if (repo->settings.command_requires_full_index)
363 ensure_full_index(repo->index);
366 * If sparse checkouts are in use, check whether paths with the
367 * SKIP_WORKTREE attribute are missing from the worktree; if not,
368 * clear that attribute for that path.
370 clear_skip_worktree_from_present_files(repo->index);
372 return res;
375 int repo_hold_locked_index(struct repository *repo,
376 struct lock_file *lf,
377 int flags)
379 if (!repo->index_file)
380 BUG("the repo hasn't been setup");
381 return hold_lock_file_for_update(lf, repo->index_file, flags);