line-range: plug leaking find functions
[git/gitster.git] / repository.c
blobdeb68625e0a830a136ebd7d43c5a15b0e00c0574
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;
24 * An escape hatch: if we hit a bug in the production code that fails
25 * to set an appropriate hash algorithm (most likely to happen when
26 * running outside a repository), we can tell the user who reported
27 * the crash to set the environment variable to "sha1" (all lowercase)
28 * to revert to the historical behaviour of defaulting to SHA-1.
30 static void set_default_hash_algo(struct repository *repo)
32 const char *hash_name;
33 int algo;
35 hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
36 if (!hash_name)
37 return;
38 algo = hash_algo_by_name(hash_name);
39 if (algo == GIT_HASH_UNKNOWN)
40 return;
42 repo_set_hash_algo(repo, algo);
45 void initialize_repository(struct repository *repo)
47 repo->objects = raw_object_store_new();
48 repo->remote_state = remote_state_new();
49 repo->parsed_objects = parsed_object_pool_new();
50 ALLOC_ARRAY(repo->index, 1);
51 index_state_init(repo->index, repo);
54 * When a command runs inside a repository, it learns what
55 * hash algorithm is in use from the repository, but some
56 * commands are designed to work outside a repository, yet
57 * they want to access the_hash_algo, if only for the length
58 * of the hashed value to see if their input looks like a
59 * plausible hash value.
61 * We are in the process of identifying such code paths and
62 * giving them an appropriate default individually; any
63 * unconverted code paths that try to access the_hash_algo
64 * will thus fail. The end-users however have an escape hatch
65 * to set GIT_TEST_DEFAULT_HASH_ALGO environment variable to
66 * "sha1" to get back the old behaviour of defaulting to SHA-1.
68 * This escape hatch is deliberately kept unadvertised, so
69 * that they see crashes and we can get a report before
70 * telling them about it.
72 if (repo == the_repository)
73 set_default_hash_algo(repo);
76 static void expand_base_dir(char **out, const char *in,
77 const char *base_dir, const char *def_in)
79 free(*out);
80 if (in)
81 *out = xstrdup(in);
82 else
83 *out = xstrfmt("%s/%s", base_dir, def_in);
86 static void repo_set_commondir(struct repository *repo,
87 const char *commondir)
89 struct strbuf sb = STRBUF_INIT;
91 free(repo->commondir);
93 if (commondir) {
94 repo->different_commondir = 1;
95 repo->commondir = xstrdup(commondir);
96 return;
99 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
100 repo->commondir = strbuf_detach(&sb, NULL);
103 void repo_set_gitdir(struct repository *repo,
104 const char *root,
105 const struct set_gitdir_args *o)
107 const char *gitfile = read_gitfile(root);
109 * repo->gitdir is saved because the caller could pass "root"
110 * that also points to repo->gitdir. We want to keep it alive
111 * until after xstrdup(root). Then we can free it.
113 char *old_gitdir = repo->gitdir;
115 repo->gitdir = xstrdup(gitfile ? gitfile : root);
116 free(old_gitdir);
118 repo_set_commondir(repo, o->commondir);
120 if (!repo->objects->odb) {
121 CALLOC_ARRAY(repo->objects->odb, 1);
122 repo->objects->odb_tail = &repo->objects->odb->next;
124 expand_base_dir(&repo->objects->odb->path, o->object_dir,
125 repo->commondir, "objects");
127 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
129 free(repo->objects->alternate_db);
130 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
131 expand_base_dir(&repo->graft_file, o->graft_file,
132 repo->commondir, "info/grafts");
133 expand_base_dir(&repo->index_file, o->index_file,
134 repo->gitdir, "index");
137 void repo_set_hash_algo(struct repository *repo, int hash_algo)
139 repo->hash_algo = &hash_algos[hash_algo];
142 void repo_set_compat_hash_algo(struct repository *repo, int algo)
144 if (hash_algo_by_ptr(repo->hash_algo) == algo)
145 BUG("hash_algo and compat_hash_algo match");
146 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
147 if (repo->compat_hash_algo)
148 repo_read_loose_object_map(repo);
151 void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
153 repo->ref_storage_format = format;
157 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
158 * Return 0 upon success and a non-zero value upon failure.
160 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
162 int ret = 0;
163 int error = 0;
164 char *abspath = NULL;
165 const char *resolved_gitdir;
166 struct set_gitdir_args args = { NULL };
168 abspath = real_pathdup(gitdir, 0);
169 if (!abspath) {
170 ret = -1;
171 goto out;
174 /* 'gitdir' must reference the gitdir directly */
175 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
176 if (!resolved_gitdir) {
177 ret = -1;
178 goto out;
181 repo_set_gitdir(repo, resolved_gitdir, &args);
183 out:
184 free(abspath);
185 return ret;
188 void repo_set_worktree(struct repository *repo, const char *path)
190 repo->worktree = real_pathdup(path, 1);
192 trace2_def_repo(repo);
195 static int read_and_verify_repository_format(struct repository_format *format,
196 const char *commondir)
198 int ret = 0;
199 struct strbuf sb = STRBUF_INIT;
201 strbuf_addf(&sb, "%s/config", commondir);
202 read_repository_format(format, sb.buf);
203 strbuf_reset(&sb);
205 if (verify_repository_format(format, &sb) < 0) {
206 warning("%s", sb.buf);
207 ret = -1;
210 strbuf_release(&sb);
211 return ret;
215 * Initialize 'repo' based on the provided 'gitdir'.
216 * Return 0 upon success and a non-zero value upon failure.
218 int repo_init(struct repository *repo,
219 const char *gitdir,
220 const char *worktree)
222 struct repository_format format = REPOSITORY_FORMAT_INIT;
223 memset(repo, 0, sizeof(*repo));
225 initialize_repository(repo);
227 if (repo_init_gitdir(repo, gitdir))
228 goto error;
230 if (read_and_verify_repository_format(&format, repo->commondir))
231 goto error;
233 repo_set_hash_algo(repo, format.hash_algo);
234 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
235 repo_set_ref_storage_format(repo, format.ref_storage_format);
236 repo->repository_format_worktree_config = format.worktree_config;
238 /* take ownership of format.partial_clone */
239 repo->repository_format_partial_clone = format.partial_clone;
240 format.partial_clone = NULL;
242 if (worktree)
243 repo_set_worktree(repo, worktree);
245 if (repo->compat_hash_algo)
246 repo_read_loose_object_map(repo);
248 clear_repository_format(&format);
249 return 0;
251 error:
252 repo_clear(repo);
253 return -1;
256 int repo_submodule_init(struct repository *subrepo,
257 struct repository *superproject,
258 const char *path,
259 const struct object_id *treeish_name)
261 struct strbuf gitdir = STRBUF_INIT;
262 struct strbuf worktree = STRBUF_INIT;
263 int ret = 0;
265 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
266 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
268 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
270 * If initialization fails then it may be due to the submodule
271 * not being populated in the superproject's worktree. Instead
272 * we can try to initialize the submodule by finding it's gitdir
273 * in the superproject's 'modules' directory. In this case the
274 * submodule would not have a worktree.
276 const struct submodule *sub =
277 submodule_from_path(superproject, treeish_name, path);
278 if (!sub) {
279 ret = -1;
280 goto out;
283 strbuf_reset(&gitdir);
284 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
286 if (repo_init(subrepo, gitdir.buf, NULL)) {
287 ret = -1;
288 goto out;
292 subrepo->submodule_prefix = xstrfmt("%s%s/",
293 superproject->submodule_prefix ?
294 superproject->submodule_prefix :
295 "", path);
297 out:
298 strbuf_release(&gitdir);
299 strbuf_release(&worktree);
300 return ret;
303 static void repo_clear_path_cache(struct repo_path_cache *cache)
305 FREE_AND_NULL(cache->squash_msg);
306 FREE_AND_NULL(cache->squash_msg);
307 FREE_AND_NULL(cache->merge_msg);
308 FREE_AND_NULL(cache->merge_rr);
309 FREE_AND_NULL(cache->merge_mode);
310 FREE_AND_NULL(cache->merge_head);
311 FREE_AND_NULL(cache->fetch_head);
312 FREE_AND_NULL(cache->shallow);
315 void repo_clear(struct repository *repo)
317 struct hashmap_iter iter;
318 struct strmap_entry *e;
320 FREE_AND_NULL(repo->gitdir);
321 FREE_AND_NULL(repo->commondir);
322 FREE_AND_NULL(repo->graft_file);
323 FREE_AND_NULL(repo->index_file);
324 FREE_AND_NULL(repo->worktree);
325 FREE_AND_NULL(repo->submodule_prefix);
327 raw_object_store_clear(repo->objects);
328 FREE_AND_NULL(repo->objects);
330 parsed_object_pool_clear(repo->parsed_objects);
331 FREE_AND_NULL(repo->parsed_objects);
333 FREE_AND_NULL(repo->settings.fsmonitor);
335 if (repo->config) {
336 git_configset_clear(repo->config);
337 FREE_AND_NULL(repo->config);
340 if (repo->submodule_cache) {
341 submodule_cache_free(repo->submodule_cache);
342 repo->submodule_cache = NULL;
345 if (repo->index) {
346 discard_index(repo->index);
347 FREE_AND_NULL(repo->index);
350 if (repo->promisor_remote_config) {
351 promisor_remote_clear(repo->promisor_remote_config);
352 FREE_AND_NULL(repo->promisor_remote_config);
355 if (repo->remote_state) {
356 remote_state_clear(repo->remote_state);
357 FREE_AND_NULL(repo->remote_state);
360 strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
361 ref_store_release(e->value);
362 strmap_clear(&repo->submodule_ref_stores, 1);
364 strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
365 ref_store_release(e->value);
366 strmap_clear(&repo->worktree_ref_stores, 1);
368 repo_clear_path_cache(&repo->cached_paths);
371 int repo_read_index(struct repository *repo)
373 int res;
375 /* Complete the double-reference */
376 if (!repo->index) {
377 ALLOC_ARRAY(repo->index, 1);
378 index_state_init(repo->index, repo);
379 } else if (repo->index->repo != repo) {
380 BUG("repo's index should point back at itself");
383 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
385 prepare_repo_settings(repo);
386 if (repo->settings.command_requires_full_index)
387 ensure_full_index(repo->index);
390 * If sparse checkouts are in use, check whether paths with the
391 * SKIP_WORKTREE attribute are missing from the worktree; if not,
392 * clear that attribute for that path.
394 clear_skip_worktree_from_present_files(repo->index);
396 return res;
399 int repo_hold_locked_index(struct repository *repo,
400 struct lock_file *lf,
401 int flags)
403 if (!repo->index_file)
404 BUG("the repo hasn't been setup");
405 return hold_lock_file_for_update(lf, repo->index_file, flags);