Sync with 'maint'
[git/gitster.git] / repository.c
blobf988b8ae68a6a29792e7f2c980a02bd0e388a3b9
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"
20 * We do not define `USE_THE_REPOSITORY_VARIABLE` in this file because we do
21 * not want to rely on functions that implicitly use `the_repository`. This
22 * means that the `extern` declaration of `the_repository` isn't visible here,
23 * which makes sparse unhappy. We thus declare it here.
25 extern struct repository *the_repository;
27 /* The main repository */
28 static struct repository the_repo;
29 struct repository *the_repository = &the_repo;
32 * An escape hatch: if we hit a bug in the production code that fails
33 * to set an appropriate hash algorithm (most likely to happen when
34 * running outside a repository), we can tell the user who reported
35 * the crash to set the environment variable to "sha1" (all lowercase)
36 * to revert to the historical behaviour of defaulting to SHA-1.
38 static void set_default_hash_algo(struct repository *repo)
40 const char *hash_name;
41 int algo;
43 hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
44 if (!hash_name)
45 return;
46 algo = hash_algo_by_name(hash_name);
47 if (algo == GIT_HASH_UNKNOWN)
48 return;
50 repo_set_hash_algo(repo, algo);
53 void initialize_repository(struct repository *repo)
55 repo->objects = raw_object_store_new();
56 repo->remote_state = remote_state_new();
57 repo->parsed_objects = parsed_object_pool_new(repo);
58 ALLOC_ARRAY(repo->index, 1);
59 index_state_init(repo->index, repo);
62 * When a command runs inside a repository, it learns what
63 * hash algorithm is in use from the repository, but some
64 * commands are designed to work outside a repository, yet
65 * they want to access the_hash_algo, if only for the length
66 * of the hashed value to see if their input looks like a
67 * plausible hash value.
69 * We are in the process of identifying such code paths and
70 * giving them an appropriate default individually; any
71 * unconverted code paths that try to access the_hash_algo
72 * will thus fail. The end-users however have an escape hatch
73 * to set GIT_TEST_DEFAULT_HASH_ALGO environment variable to
74 * "sha1" to get back the old behaviour of defaulting to SHA-1.
76 * This escape hatch is deliberately kept unadvertised, so
77 * that they see crashes and we can get a report before
78 * telling them about it.
80 if (repo == the_repository)
81 set_default_hash_algo(repo);
84 static void expand_base_dir(char **out, const char *in,
85 const char *base_dir, const char *def_in)
87 free(*out);
88 if (in)
89 *out = xstrdup(in);
90 else
91 *out = xstrfmt("%s/%s", base_dir, def_in);
94 const char *repo_get_git_dir(struct repository *repo)
96 if (!repo->gitdir)
97 BUG("repository hasn't been set up");
98 return repo->gitdir;
101 const char *repo_get_common_dir(struct repository *repo)
103 if (!repo->commondir)
104 BUG("repository hasn't been set up");
105 return repo->commondir;
108 const char *repo_get_object_directory(struct repository *repo)
110 if (!repo->objects->odb)
111 BUG("repository hasn't been set up");
112 return repo->objects->odb->path;
115 const char *repo_get_index_file(struct repository *repo)
117 if (!repo->index_file)
118 BUG("repository hasn't been set up");
119 return repo->index_file;
122 const char *repo_get_graft_file(struct repository *repo)
124 if (!repo->graft_file)
125 BUG("repository hasn't been set up");
126 return repo->graft_file;
129 const char *repo_get_work_tree(struct repository *repo)
131 return repo->worktree;
134 static void repo_set_commondir(struct repository *repo,
135 const char *commondir)
137 struct strbuf sb = STRBUF_INIT;
139 free(repo->commondir);
141 if (commondir) {
142 repo->different_commondir = 1;
143 repo->commondir = xstrdup(commondir);
144 return;
147 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
148 repo->commondir = strbuf_detach(&sb, NULL);
151 void repo_set_gitdir(struct repository *repo,
152 const char *root,
153 const struct set_gitdir_args *o)
155 const char *gitfile = read_gitfile(root);
157 * repo->gitdir is saved because the caller could pass "root"
158 * that also points to repo->gitdir. We want to keep it alive
159 * until after xstrdup(root). Then we can free it.
161 char *old_gitdir = repo->gitdir;
163 repo->gitdir = xstrdup(gitfile ? gitfile : root);
164 free(old_gitdir);
166 repo_set_commondir(repo, o->commondir);
168 if (!repo->objects->odb) {
169 CALLOC_ARRAY(repo->objects->odb, 1);
170 repo->objects->odb_tail = &repo->objects->odb->next;
172 expand_base_dir(&repo->objects->odb->path, o->object_dir,
173 repo->commondir, "objects");
175 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
177 free(repo->objects->alternate_db);
178 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
179 expand_base_dir(&repo->graft_file, o->graft_file,
180 repo->commondir, "info/grafts");
181 expand_base_dir(&repo->index_file, o->index_file,
182 repo->gitdir, "index");
185 void repo_set_hash_algo(struct repository *repo, int hash_algo)
187 repo->hash_algo = &hash_algos[hash_algo];
190 void repo_set_compat_hash_algo(struct repository *repo, int algo)
192 if (hash_algo_by_ptr(repo->hash_algo) == algo)
193 BUG("hash_algo and compat_hash_algo match");
194 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
195 if (repo->compat_hash_algo)
196 repo_read_loose_object_map(repo);
199 void repo_set_ref_storage_format(struct repository *repo,
200 enum ref_storage_format format)
202 repo->ref_storage_format = format;
206 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
207 * Return 0 upon success and a non-zero value upon failure.
209 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
211 int ret = 0;
212 int error = 0;
213 char *abspath = NULL;
214 const char *resolved_gitdir;
215 struct set_gitdir_args args = { NULL };
217 abspath = real_pathdup(gitdir, 0);
218 if (!abspath) {
219 ret = -1;
220 goto out;
223 /* 'gitdir' must reference the gitdir directly */
224 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
225 if (!resolved_gitdir) {
226 ret = -1;
227 goto out;
230 repo_set_gitdir(repo, resolved_gitdir, &args);
232 out:
233 free(abspath);
234 return ret;
237 void repo_set_worktree(struct repository *repo, const char *path)
239 repo->worktree = real_pathdup(path, 1);
241 trace2_def_repo(repo);
244 static int read_and_verify_repository_format(struct repository_format *format,
245 const char *commondir)
247 int ret = 0;
248 struct strbuf sb = STRBUF_INIT;
250 strbuf_addf(&sb, "%s/config", commondir);
251 read_repository_format(format, sb.buf);
252 strbuf_reset(&sb);
254 if (verify_repository_format(format, &sb) < 0) {
255 warning("%s", sb.buf);
256 ret = -1;
259 strbuf_release(&sb);
260 return ret;
264 * Initialize 'repo' based on the provided 'gitdir'.
265 * Return 0 upon success and a non-zero value upon failure.
267 int repo_init(struct repository *repo,
268 const char *gitdir,
269 const char *worktree)
271 struct repository_format format = REPOSITORY_FORMAT_INIT;
272 memset(repo, 0, sizeof(*repo));
274 initialize_repository(repo);
276 if (repo_init_gitdir(repo, gitdir))
277 goto error;
279 if (read_and_verify_repository_format(&format, repo->commondir))
280 goto error;
282 repo_set_hash_algo(repo, format.hash_algo);
283 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
284 repo_set_ref_storage_format(repo, format.ref_storage_format);
285 repo->repository_format_worktree_config = format.worktree_config;
287 /* take ownership of format.partial_clone */
288 repo->repository_format_partial_clone = format.partial_clone;
289 format.partial_clone = NULL;
291 if (worktree)
292 repo_set_worktree(repo, worktree);
294 if (repo->compat_hash_algo)
295 repo_read_loose_object_map(repo);
297 clear_repository_format(&format);
298 return 0;
300 error:
301 repo_clear(repo);
302 return -1;
305 int repo_submodule_init(struct repository *subrepo,
306 struct repository *superproject,
307 const char *path,
308 const struct object_id *treeish_name)
310 struct strbuf gitdir = STRBUF_INIT;
311 struct strbuf worktree = STRBUF_INIT;
312 int ret = 0;
314 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
315 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
317 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
319 * If initialization fails then it may be due to the submodule
320 * not being populated in the superproject's worktree. Instead
321 * we can try to initialize the submodule by finding it's gitdir
322 * in the superproject's 'modules' directory. In this case the
323 * submodule would not have a worktree.
325 const struct submodule *sub =
326 submodule_from_path(superproject, treeish_name, path);
327 if (!sub) {
328 ret = -1;
329 goto out;
332 strbuf_reset(&gitdir);
333 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
335 if (repo_init(subrepo, gitdir.buf, NULL)) {
336 ret = -1;
337 goto out;
341 subrepo->submodule_prefix = xstrfmt("%s%s/",
342 superproject->submodule_prefix ?
343 superproject->submodule_prefix :
344 "", path);
346 out:
347 strbuf_release(&gitdir);
348 strbuf_release(&worktree);
349 return ret;
352 static void repo_clear_path_cache(struct repo_path_cache *cache)
354 FREE_AND_NULL(cache->squash_msg);
355 FREE_AND_NULL(cache->squash_msg);
356 FREE_AND_NULL(cache->merge_msg);
357 FREE_AND_NULL(cache->merge_rr);
358 FREE_AND_NULL(cache->merge_mode);
359 FREE_AND_NULL(cache->merge_head);
360 FREE_AND_NULL(cache->fetch_head);
361 FREE_AND_NULL(cache->shallow);
364 void repo_clear(struct repository *repo)
366 struct hashmap_iter iter;
367 struct strmap_entry *e;
369 FREE_AND_NULL(repo->gitdir);
370 FREE_AND_NULL(repo->commondir);
371 FREE_AND_NULL(repo->graft_file);
372 FREE_AND_NULL(repo->index_file);
373 FREE_AND_NULL(repo->worktree);
374 FREE_AND_NULL(repo->submodule_prefix);
376 raw_object_store_clear(repo->objects);
377 FREE_AND_NULL(repo->objects);
379 parsed_object_pool_clear(repo->parsed_objects);
380 FREE_AND_NULL(repo->parsed_objects);
382 FREE_AND_NULL(repo->settings.fsmonitor);
384 if (repo->config) {
385 git_configset_clear(repo->config);
386 FREE_AND_NULL(repo->config);
389 if (repo->submodule_cache) {
390 submodule_cache_free(repo->submodule_cache);
391 repo->submodule_cache = NULL;
394 if (repo->index) {
395 discard_index(repo->index);
396 FREE_AND_NULL(repo->index);
399 if (repo->promisor_remote_config) {
400 promisor_remote_clear(repo->promisor_remote_config);
401 FREE_AND_NULL(repo->promisor_remote_config);
404 if (repo->remote_state) {
405 remote_state_clear(repo->remote_state);
406 FREE_AND_NULL(repo->remote_state);
409 strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
410 ref_store_release(e->value);
411 strmap_clear(&repo->submodule_ref_stores, 1);
413 strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
414 ref_store_release(e->value);
415 strmap_clear(&repo->worktree_ref_stores, 1);
417 repo_clear_path_cache(&repo->cached_paths);
420 int repo_read_index(struct repository *repo)
422 int res;
424 /* Complete the double-reference */
425 if (!repo->index) {
426 ALLOC_ARRAY(repo->index, 1);
427 index_state_init(repo->index, repo);
428 } else if (repo->index->repo != repo) {
429 BUG("repo's index should point back at itself");
432 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
434 prepare_repo_settings(repo);
435 if (repo->settings.command_requires_full_index)
436 ensure_full_index(repo->index);
439 * If sparse checkouts are in use, check whether paths with the
440 * SKIP_WORKTREE attribute are missing from the worktree; if not,
441 * clear that attribute for that path.
443 clear_skip_worktree_from_present_files(repo->index);
445 return res;
448 int repo_hold_locked_index(struct repository *repo,
449 struct lock_file *lf,
450 int flags)
452 if (!repo->index_file)
453 BUG("the repo hasn't been setup");
454 return hold_lock_file_for_update(lf, repo->index_file, flags);