The seventh batch
[git/gitster.git] / repository.c
blobc50849fd8333900432d0bb6449a6410ea5915bb9
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"
18 /* The main repository */
19 static struct repository the_repo;
20 struct repository *the_repository = &the_repo;
22 void initialize_repository(struct repository *repo)
24 repo->objects = raw_object_store_new();
25 repo->remote_state = remote_state_new();
26 repo->parsed_objects = parsed_object_pool_new();
27 ALLOC_ARRAY(repo->index, 1);
28 index_state_init(repo->index, repo);
31 * Unfortunately, we need to keep this hack around for the time being:
33 * - Not setting up the hash algorithm for `the_repository` leads to
34 * crashes because `the_hash_algo` is a macro that expands to
35 * `the_repository->hash_algo`. So if Git commands try to access
36 * `the_hash_algo` without a Git directory we crash.
38 * - Setting up the hash algorithm to be SHA1 by default breaks other
39 * commands when running with SHA256.
41 * This is another point in case why having global state is a bad idea.
42 * Eventually, we should remove this hack and stop setting the hash
43 * algorithm in this function altogether. Instead, it should only ever
44 * be set via our repository setup procedures. But that requires more
45 * work.
47 if (repo == the_repository)
48 repo_set_hash_algo(repo, GIT_HASH_SHA1);
51 static void expand_base_dir(char **out, const char *in,
52 const char *base_dir, const char *def_in)
54 free(*out);
55 if (in)
56 *out = xstrdup(in);
57 else
58 *out = xstrfmt("%s/%s", base_dir, def_in);
61 static void repo_set_commondir(struct repository *repo,
62 const char *commondir)
64 struct strbuf sb = STRBUF_INIT;
66 free(repo->commondir);
68 if (commondir) {
69 repo->different_commondir = 1;
70 repo->commondir = xstrdup(commondir);
71 return;
74 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
75 repo->commondir = strbuf_detach(&sb, NULL);
78 void repo_set_gitdir(struct repository *repo,
79 const char *root,
80 const struct set_gitdir_args *o)
82 const char *gitfile = read_gitfile(root);
84 * repo->gitdir is saved because the caller could pass "root"
85 * that also points to repo->gitdir. We want to keep it alive
86 * until after xstrdup(root). Then we can free it.
88 char *old_gitdir = repo->gitdir;
90 repo->gitdir = xstrdup(gitfile ? gitfile : root);
91 free(old_gitdir);
93 repo_set_commondir(repo, o->commondir);
95 if (!repo->objects->odb) {
96 CALLOC_ARRAY(repo->objects->odb, 1);
97 repo->objects->odb_tail = &repo->objects->odb->next;
99 expand_base_dir(&repo->objects->odb->path, o->object_dir,
100 repo->commondir, "objects");
102 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
104 free(repo->objects->alternate_db);
105 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
106 expand_base_dir(&repo->graft_file, o->graft_file,
107 repo->commondir, "info/grafts");
108 expand_base_dir(&repo->index_file, o->index_file,
109 repo->gitdir, "index");
112 void repo_set_hash_algo(struct repository *repo, int hash_algo)
114 repo->hash_algo = &hash_algos[hash_algo];
117 void repo_set_compat_hash_algo(struct repository *repo, int algo)
119 if (hash_algo_by_ptr(repo->hash_algo) == algo)
120 BUG("hash_algo and compat_hash_algo match");
121 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
122 if (repo->compat_hash_algo)
123 repo_read_loose_object_map(repo);
126 void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
128 repo->ref_storage_format = format;
132 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
133 * Return 0 upon success and a non-zero value upon failure.
135 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
137 int ret = 0;
138 int error = 0;
139 char *abspath = NULL;
140 const char *resolved_gitdir;
141 struct set_gitdir_args args = { NULL };
143 abspath = real_pathdup(gitdir, 0);
144 if (!abspath) {
145 ret = -1;
146 goto out;
149 /* 'gitdir' must reference the gitdir directly */
150 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
151 if (!resolved_gitdir) {
152 ret = -1;
153 goto out;
156 repo_set_gitdir(repo, resolved_gitdir, &args);
158 out:
159 free(abspath);
160 return ret;
163 void repo_set_worktree(struct repository *repo, const char *path)
165 repo->worktree = real_pathdup(path, 1);
167 trace2_def_repo(repo);
170 static int read_and_verify_repository_format(struct repository_format *format,
171 const char *commondir)
173 int ret = 0;
174 struct strbuf sb = STRBUF_INIT;
176 strbuf_addf(&sb, "%s/config", commondir);
177 read_repository_format(format, sb.buf);
178 strbuf_reset(&sb);
180 if (verify_repository_format(format, &sb) < 0) {
181 warning("%s", sb.buf);
182 ret = -1;
185 strbuf_release(&sb);
186 return ret;
190 * Initialize 'repo' based on the provided 'gitdir'.
191 * Return 0 upon success and a non-zero value upon failure.
193 int repo_init(struct repository *repo,
194 const char *gitdir,
195 const char *worktree)
197 struct repository_format format = REPOSITORY_FORMAT_INIT;
198 memset(repo, 0, sizeof(*repo));
200 initialize_repository(repo);
202 if (repo_init_gitdir(repo, gitdir))
203 goto error;
205 if (read_and_verify_repository_format(&format, repo->commondir))
206 goto error;
208 repo_set_hash_algo(repo, format.hash_algo);
209 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
210 repo_set_ref_storage_format(repo, format.ref_storage_format);
211 repo->repository_format_worktree_config = format.worktree_config;
213 /* take ownership of format.partial_clone */
214 repo->repository_format_partial_clone = format.partial_clone;
215 format.partial_clone = NULL;
217 if (worktree)
218 repo_set_worktree(repo, worktree);
220 if (repo->compat_hash_algo)
221 repo_read_loose_object_map(repo);
223 clear_repository_format(&format);
224 return 0;
226 error:
227 repo_clear(repo);
228 return -1;
231 int repo_submodule_init(struct repository *subrepo,
232 struct repository *superproject,
233 const char *path,
234 const struct object_id *treeish_name)
236 struct strbuf gitdir = STRBUF_INIT;
237 struct strbuf worktree = STRBUF_INIT;
238 int ret = 0;
240 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
241 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
243 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
245 * If initialization fails then it may be due to the submodule
246 * not being populated in the superproject's worktree. Instead
247 * we can try to initialize the submodule by finding it's gitdir
248 * in the superproject's 'modules' directory. In this case the
249 * submodule would not have a worktree.
251 const struct submodule *sub =
252 submodule_from_path(superproject, treeish_name, path);
253 if (!sub) {
254 ret = -1;
255 goto out;
258 strbuf_reset(&gitdir);
259 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
261 if (repo_init(subrepo, gitdir.buf, NULL)) {
262 ret = -1;
263 goto out;
267 subrepo->submodule_prefix = xstrfmt("%s%s/",
268 superproject->submodule_prefix ?
269 superproject->submodule_prefix :
270 "", path);
272 out:
273 strbuf_release(&gitdir);
274 strbuf_release(&worktree);
275 return ret;
278 static void repo_clear_path_cache(struct repo_path_cache *cache)
280 FREE_AND_NULL(cache->squash_msg);
281 FREE_AND_NULL(cache->squash_msg);
282 FREE_AND_NULL(cache->merge_msg);
283 FREE_AND_NULL(cache->merge_rr);
284 FREE_AND_NULL(cache->merge_mode);
285 FREE_AND_NULL(cache->merge_head);
286 FREE_AND_NULL(cache->fetch_head);
287 FREE_AND_NULL(cache->shallow);
290 void repo_clear(struct repository *repo)
292 FREE_AND_NULL(repo->gitdir);
293 FREE_AND_NULL(repo->commondir);
294 FREE_AND_NULL(repo->graft_file);
295 FREE_AND_NULL(repo->index_file);
296 FREE_AND_NULL(repo->worktree);
297 FREE_AND_NULL(repo->submodule_prefix);
299 raw_object_store_clear(repo->objects);
300 FREE_AND_NULL(repo->objects);
302 parsed_object_pool_clear(repo->parsed_objects);
303 FREE_AND_NULL(repo->parsed_objects);
305 FREE_AND_NULL(repo->settings.fsmonitor);
307 if (repo->config) {
308 git_configset_clear(repo->config);
309 FREE_AND_NULL(repo->config);
312 if (repo->submodule_cache) {
313 submodule_cache_free(repo->submodule_cache);
314 repo->submodule_cache = NULL;
317 if (repo->index) {
318 discard_index(repo->index);
319 FREE_AND_NULL(repo->index);
322 if (repo->promisor_remote_config) {
323 promisor_remote_clear(repo->promisor_remote_config);
324 FREE_AND_NULL(repo->promisor_remote_config);
327 if (repo->remote_state) {
328 remote_state_clear(repo->remote_state);
329 FREE_AND_NULL(repo->remote_state);
332 repo_clear_path_cache(&repo->cached_paths);
335 int repo_read_index(struct repository *repo)
337 int res;
339 /* Complete the double-reference */
340 if (!repo->index) {
341 ALLOC_ARRAY(repo->index, 1);
342 index_state_init(repo->index, repo);
343 } else if (repo->index->repo != repo) {
344 BUG("repo's index should point back at itself");
347 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
349 prepare_repo_settings(repo);
350 if (repo->settings.command_requires_full_index)
351 ensure_full_index(repo->index);
354 * If sparse checkouts are in use, check whether paths with the
355 * SKIP_WORKTREE attribute are missing from the worktree; if not,
356 * clear that attribute for that path.
358 clear_skip_worktree_from_present_files(repo->index);
360 return res;
363 int repo_hold_locked_index(struct repository *repo,
364 struct lock_file *lf,
365 int flags)
367 if (!repo->index_file)
368 BUG("the repo hasn't been setup");
369 return hold_lock_file_for_update(lf, repo->index_file, flags);