The sixteenth batch
[git/gitster.git] / repository.c
blobecd691181fc97d59241a53765d898300b1f9cf99
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "repository.h"
4 #include "odb.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 = odb_new(repo);
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->sources)
111 BUG("repository hasn't been set up");
112 return repo->objects->sources->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->sources) {
169 CALLOC_ARRAY(repo->objects->sources, 1);
170 repo->objects->sources->odb = repo->objects;
171 repo->objects->sources_tail = &repo->objects->sources->next;
173 expand_base_dir(&repo->objects->sources->path, o->object_dir,
174 repo->commondir, "objects");
176 repo->objects->sources->disable_ref_updates = o->disable_ref_updates;
178 free(repo->objects->alternate_db);
179 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
180 expand_base_dir(&repo->graft_file, o->graft_file,
181 repo->commondir, "info/grafts");
182 expand_base_dir(&repo->index_file, o->index_file,
183 repo->gitdir, "index");
186 void repo_set_hash_algo(struct repository *repo, int hash_algo)
188 repo->hash_algo = &hash_algos[hash_algo];
191 void repo_set_compat_hash_algo(struct repository *repo, int algo)
193 if (hash_algo_by_ptr(repo->hash_algo) == algo)
194 BUG("hash_algo and compat_hash_algo match");
195 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
196 if (repo->compat_hash_algo)
197 repo_read_loose_object_map(repo);
200 void repo_set_ref_storage_format(struct repository *repo,
201 enum ref_storage_format format)
203 repo->ref_storage_format = format;
207 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
208 * Return 0 upon success and a non-zero value upon failure.
210 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
212 int ret = 0;
213 int error = 0;
214 char *abspath = NULL;
215 const char *resolved_gitdir;
216 struct set_gitdir_args args = { NULL };
218 abspath = real_pathdup(gitdir, 0);
219 if (!abspath) {
220 ret = -1;
221 goto out;
224 /* 'gitdir' must reference the gitdir directly */
225 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
226 if (!resolved_gitdir) {
227 ret = -1;
228 goto out;
231 repo_set_gitdir(repo, resolved_gitdir, &args);
233 out:
234 free(abspath);
235 return ret;
238 void repo_set_worktree(struct repository *repo, const char *path)
240 repo->worktree = real_pathdup(path, 1);
242 trace2_def_repo(repo);
245 static int read_and_verify_repository_format(struct repository_format *format,
246 const char *commondir)
248 int ret = 0;
249 struct strbuf sb = STRBUF_INIT;
251 strbuf_addf(&sb, "%s/config", commondir);
252 read_repository_format(format, sb.buf);
253 strbuf_reset(&sb);
255 if (verify_repository_format(format, &sb) < 0) {
256 warning("%s", sb.buf);
257 ret = -1;
260 strbuf_release(&sb);
261 return ret;
265 * Initialize 'repo' based on the provided 'gitdir'.
266 * Return 0 upon success and a non-zero value upon failure.
268 int repo_init(struct repository *repo,
269 const char *gitdir,
270 const char *worktree)
272 struct repository_format format = REPOSITORY_FORMAT_INIT;
273 memset(repo, 0, sizeof(*repo));
275 initialize_repository(repo);
277 if (repo_init_gitdir(repo, gitdir))
278 goto error;
280 if (read_and_verify_repository_format(&format, repo->commondir))
281 goto error;
283 repo_set_hash_algo(repo, format.hash_algo);
284 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
285 repo_set_ref_storage_format(repo, format.ref_storage_format);
286 repo->repository_format_worktree_config = format.worktree_config;
287 repo->repository_format_relative_worktrees = format.relative_worktrees;
288 repo->repository_format_precious_objects = format.precious_objects;
290 /* take ownership of format.partial_clone */
291 repo->repository_format_partial_clone = format.partial_clone;
292 format.partial_clone = NULL;
294 if (worktree)
295 repo_set_worktree(repo, worktree);
297 if (repo->compat_hash_algo)
298 repo_read_loose_object_map(repo);
300 clear_repository_format(&format);
301 return 0;
303 error:
304 repo_clear(repo);
305 return -1;
308 int repo_submodule_init(struct repository *subrepo,
309 struct repository *superproject,
310 const char *path,
311 const struct object_id *treeish_name)
313 struct strbuf gitdir = STRBUF_INIT;
314 struct strbuf worktree = STRBUF_INIT;
315 int ret = 0;
317 repo_worktree_path_append(superproject, &gitdir, "%s/.git", path);
318 repo_worktree_path_append(superproject, &worktree, "%s", path);
320 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
322 * If initialization fails then it may be due to the submodule
323 * not being populated in the superproject's worktree. Instead
324 * we can try to initialize the submodule by finding it's gitdir
325 * in the superproject's 'modules' directory. In this case the
326 * submodule would not have a worktree.
328 const struct submodule *sub =
329 submodule_from_path(superproject, treeish_name, path);
330 if (!sub) {
331 ret = -1;
332 goto out;
335 strbuf_reset(&gitdir);
336 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
338 if (repo_init(subrepo, gitdir.buf, NULL)) {
339 ret = -1;
340 goto out;
344 subrepo->submodule_prefix = xstrfmt("%s%s/",
345 superproject->submodule_prefix ?
346 superproject->submodule_prefix :
347 "", path);
349 out:
350 strbuf_release(&gitdir);
351 strbuf_release(&worktree);
352 return ret;
355 static void repo_clear_path_cache(struct repo_path_cache *cache)
357 FREE_AND_NULL(cache->squash_msg);
358 FREE_AND_NULL(cache->squash_msg);
359 FREE_AND_NULL(cache->merge_msg);
360 FREE_AND_NULL(cache->merge_rr);
361 FREE_AND_NULL(cache->merge_mode);
362 FREE_AND_NULL(cache->merge_head);
363 FREE_AND_NULL(cache->fetch_head);
364 FREE_AND_NULL(cache->shallow);
367 void repo_clear(struct repository *repo)
369 struct hashmap_iter iter;
370 struct strmap_entry *e;
372 FREE_AND_NULL(repo->gitdir);
373 FREE_AND_NULL(repo->commondir);
374 FREE_AND_NULL(repo->graft_file);
375 FREE_AND_NULL(repo->index_file);
376 FREE_AND_NULL(repo->worktree);
377 FREE_AND_NULL(repo->submodule_prefix);
379 odb_clear(repo->objects);
380 FREE_AND_NULL(repo->objects);
382 parsed_object_pool_clear(repo->parsed_objects);
383 FREE_AND_NULL(repo->parsed_objects);
385 repo_settings_clear(repo);
387 if (repo->config) {
388 git_configset_clear(repo->config);
389 FREE_AND_NULL(repo->config);
392 if (repo->submodule_cache) {
393 submodule_cache_free(repo->submodule_cache);
394 repo->submodule_cache = NULL;
397 if (repo->index) {
398 discard_index(repo->index);
399 FREE_AND_NULL(repo->index);
402 if (repo->promisor_remote_config) {
403 promisor_remote_clear(repo->promisor_remote_config);
404 FREE_AND_NULL(repo->promisor_remote_config);
407 if (repo->remote_state) {
408 remote_state_clear(repo->remote_state);
409 FREE_AND_NULL(repo->remote_state);
412 strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
413 ref_store_release(e->value);
414 strmap_clear(&repo->submodule_ref_stores, 1);
416 strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
417 ref_store_release(e->value);
418 strmap_clear(&repo->worktree_ref_stores, 1);
420 repo_clear_path_cache(&repo->cached_paths);
423 int repo_read_index(struct repository *repo)
425 int res;
427 /* Complete the double-reference */
428 if (!repo->index) {
429 ALLOC_ARRAY(repo->index, 1);
430 index_state_init(repo->index, repo);
431 } else if (repo->index->repo != repo) {
432 BUG("repo's index should point back at itself");
435 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
437 prepare_repo_settings(repo);
438 if (repo->settings.command_requires_full_index)
439 ensure_full_index(repo->index);
442 * If sparse checkouts are in use, check whether paths with the
443 * SKIP_WORKTREE attribute are missing from the worktree; if not,
444 * clear that attribute for that path.
446 clear_skip_worktree_from_present_files(repo->index);
448 return res;
451 int repo_hold_locked_index(struct repository *repo,
452 struct lock_file *lf,
453 int flags)
455 if (!repo->index_file)
456 BUG("the repo hasn't been setup");
457 return hold_lock_file_for_update(lf, repo->index_file, flags);