diffcore-order: fix leaking buffer when parsing orderfiles
[git/gitster.git] / repository.c
blobe6fc2c6aa9d19d8b35be471483e6575b65ad82e9
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 static void repo_set_commondir(struct repository *repo,
95 const char *commondir)
97 struct strbuf sb = STRBUF_INIT;
99 free(repo->commondir);
101 if (commondir) {
102 repo->different_commondir = 1;
103 repo->commondir = xstrdup(commondir);
104 return;
107 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
108 repo->commondir = strbuf_detach(&sb, NULL);
111 void repo_set_gitdir(struct repository *repo,
112 const char *root,
113 const struct set_gitdir_args *o)
115 const char *gitfile = read_gitfile(root);
117 * repo->gitdir is saved because the caller could pass "root"
118 * that also points to repo->gitdir. We want to keep it alive
119 * until after xstrdup(root). Then we can free it.
121 char *old_gitdir = repo->gitdir;
123 repo->gitdir = xstrdup(gitfile ? gitfile : root);
124 free(old_gitdir);
126 repo_set_commondir(repo, o->commondir);
128 if (!repo->objects->odb) {
129 CALLOC_ARRAY(repo->objects->odb, 1);
130 repo->objects->odb_tail = &repo->objects->odb->next;
132 expand_base_dir(&repo->objects->odb->path, o->object_dir,
133 repo->commondir, "objects");
135 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
137 free(repo->objects->alternate_db);
138 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
139 expand_base_dir(&repo->graft_file, o->graft_file,
140 repo->commondir, "info/grafts");
141 expand_base_dir(&repo->index_file, o->index_file,
142 repo->gitdir, "index");
145 void repo_set_hash_algo(struct repository *repo, int hash_algo)
147 repo->hash_algo = &hash_algos[hash_algo];
150 void repo_set_compat_hash_algo(struct repository *repo, int algo)
152 if (hash_algo_by_ptr(repo->hash_algo) == algo)
153 BUG("hash_algo and compat_hash_algo match");
154 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
155 if (repo->compat_hash_algo)
156 repo_read_loose_object_map(repo);
159 void repo_set_ref_storage_format(struct repository *repo,
160 enum ref_storage_format format)
162 repo->ref_storage_format = format;
166 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
167 * Return 0 upon success and a non-zero value upon failure.
169 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
171 int ret = 0;
172 int error = 0;
173 char *abspath = NULL;
174 const char *resolved_gitdir;
175 struct set_gitdir_args args = { NULL };
177 abspath = real_pathdup(gitdir, 0);
178 if (!abspath) {
179 ret = -1;
180 goto out;
183 /* 'gitdir' must reference the gitdir directly */
184 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
185 if (!resolved_gitdir) {
186 ret = -1;
187 goto out;
190 repo_set_gitdir(repo, resolved_gitdir, &args);
192 out:
193 free(abspath);
194 return ret;
197 void repo_set_worktree(struct repository *repo, const char *path)
199 repo->worktree = real_pathdup(path, 1);
201 trace2_def_repo(repo);
204 static int read_and_verify_repository_format(struct repository_format *format,
205 const char *commondir)
207 int ret = 0;
208 struct strbuf sb = STRBUF_INIT;
210 strbuf_addf(&sb, "%s/config", commondir);
211 read_repository_format(format, sb.buf);
212 strbuf_reset(&sb);
214 if (verify_repository_format(format, &sb) < 0) {
215 warning("%s", sb.buf);
216 ret = -1;
219 strbuf_release(&sb);
220 return ret;
224 * Initialize 'repo' based on the provided 'gitdir'.
225 * Return 0 upon success and a non-zero value upon failure.
227 int repo_init(struct repository *repo,
228 const char *gitdir,
229 const char *worktree)
231 struct repository_format format = REPOSITORY_FORMAT_INIT;
232 memset(repo, 0, sizeof(*repo));
234 initialize_repository(repo);
236 if (repo_init_gitdir(repo, gitdir))
237 goto error;
239 if (read_and_verify_repository_format(&format, repo->commondir))
240 goto error;
242 repo_set_hash_algo(repo, format.hash_algo);
243 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
244 repo_set_ref_storage_format(repo, format.ref_storage_format);
245 repo->repository_format_worktree_config = format.worktree_config;
247 /* take ownership of format.partial_clone */
248 repo->repository_format_partial_clone = format.partial_clone;
249 format.partial_clone = NULL;
251 if (worktree)
252 repo_set_worktree(repo, worktree);
254 if (repo->compat_hash_algo)
255 repo_read_loose_object_map(repo);
257 clear_repository_format(&format);
258 return 0;
260 error:
261 repo_clear(repo);
262 return -1;
265 int repo_submodule_init(struct repository *subrepo,
266 struct repository *superproject,
267 const char *path,
268 const struct object_id *treeish_name)
270 struct strbuf gitdir = STRBUF_INIT;
271 struct strbuf worktree = STRBUF_INIT;
272 int ret = 0;
274 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
275 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
277 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
279 * If initialization fails then it may be due to the submodule
280 * not being populated in the superproject's worktree. Instead
281 * we can try to initialize the submodule by finding it's gitdir
282 * in the superproject's 'modules' directory. In this case the
283 * submodule would not have a worktree.
285 const struct submodule *sub =
286 submodule_from_path(superproject, treeish_name, path);
287 if (!sub) {
288 ret = -1;
289 goto out;
292 strbuf_reset(&gitdir);
293 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
295 if (repo_init(subrepo, gitdir.buf, NULL)) {
296 ret = -1;
297 goto out;
301 subrepo->submodule_prefix = xstrfmt("%s%s/",
302 superproject->submodule_prefix ?
303 superproject->submodule_prefix :
304 "", path);
306 out:
307 strbuf_release(&gitdir);
308 strbuf_release(&worktree);
309 return ret;
312 static void repo_clear_path_cache(struct repo_path_cache *cache)
314 FREE_AND_NULL(cache->squash_msg);
315 FREE_AND_NULL(cache->squash_msg);
316 FREE_AND_NULL(cache->merge_msg);
317 FREE_AND_NULL(cache->merge_rr);
318 FREE_AND_NULL(cache->merge_mode);
319 FREE_AND_NULL(cache->merge_head);
320 FREE_AND_NULL(cache->fetch_head);
321 FREE_AND_NULL(cache->shallow);
324 void repo_clear(struct repository *repo)
326 struct hashmap_iter iter;
327 struct strmap_entry *e;
329 FREE_AND_NULL(repo->gitdir);
330 FREE_AND_NULL(repo->commondir);
331 FREE_AND_NULL(repo->graft_file);
332 FREE_AND_NULL(repo->index_file);
333 FREE_AND_NULL(repo->worktree);
334 FREE_AND_NULL(repo->submodule_prefix);
336 raw_object_store_clear(repo->objects);
337 FREE_AND_NULL(repo->objects);
339 parsed_object_pool_clear(repo->parsed_objects);
340 FREE_AND_NULL(repo->parsed_objects);
342 FREE_AND_NULL(repo->settings.fsmonitor);
344 if (repo->config) {
345 git_configset_clear(repo->config);
346 FREE_AND_NULL(repo->config);
349 if (repo->submodule_cache) {
350 submodule_cache_free(repo->submodule_cache);
351 repo->submodule_cache = NULL;
354 if (repo->index) {
355 discard_index(repo->index);
356 FREE_AND_NULL(repo->index);
359 if (repo->promisor_remote_config) {
360 promisor_remote_clear(repo->promisor_remote_config);
361 FREE_AND_NULL(repo->promisor_remote_config);
364 if (repo->remote_state) {
365 remote_state_clear(repo->remote_state);
366 FREE_AND_NULL(repo->remote_state);
369 strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
370 ref_store_release(e->value);
371 strmap_clear(&repo->submodule_ref_stores, 1);
373 strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
374 ref_store_release(e->value);
375 strmap_clear(&repo->worktree_ref_stores, 1);
377 repo_clear_path_cache(&repo->cached_paths);
380 int repo_read_index(struct repository *repo)
382 int res;
384 /* Complete the double-reference */
385 if (!repo->index) {
386 ALLOC_ARRAY(repo->index, 1);
387 index_state_init(repo->index, repo);
388 } else if (repo->index->repo != repo) {
389 BUG("repo's index should point back at itself");
392 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
394 prepare_repo_settings(repo);
395 if (repo->settings.command_requires_full_index)
396 ensure_full_index(repo->index);
399 * If sparse checkouts are in use, check whether paths with the
400 * SKIP_WORKTREE attribute are missing from the worktree; if not,
401 * clear that attribute for that path.
403 clear_skip_worktree_from_present_files(repo->index);
405 return res;
408 int repo_hold_locked_index(struct repository *repo,
409 struct lock_file *lf,
410 int flags)
412 if (!repo->index_file)
413 BUG("the repo hasn't been setup");
414 return hold_lock_file_for_update(lf, repo->index_file, flags);