Sync with 'master'
[alt-git.git] / repository.c
blob15c10015b056051c18ced7b5bc6cd910cc874a52
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 static void expand_base_dir(char **out, const char *in,
32 const char *base_dir, const char *def_in)
34 free(*out);
35 if (in)
36 *out = xstrdup(in);
37 else
38 *out = xstrfmt("%s/%s", base_dir, def_in);
41 static void repo_set_commondir(struct repository *repo,
42 const char *commondir)
44 struct strbuf sb = STRBUF_INIT;
46 free(repo->commondir);
48 if (commondir) {
49 repo->different_commondir = 1;
50 repo->commondir = xstrdup(commondir);
51 return;
54 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
55 repo->commondir = strbuf_detach(&sb, NULL);
58 void repo_set_gitdir(struct repository *repo,
59 const char *root,
60 const struct set_gitdir_args *o)
62 const char *gitfile = read_gitfile(root);
64 * repo->gitdir is saved because the caller could pass "root"
65 * that also points to repo->gitdir. We want to keep it alive
66 * until after xstrdup(root). Then we can free it.
68 char *old_gitdir = repo->gitdir;
70 repo->gitdir = xstrdup(gitfile ? gitfile : root);
71 free(old_gitdir);
73 repo_set_commondir(repo, o->commondir);
75 if (!repo->objects->odb) {
76 CALLOC_ARRAY(repo->objects->odb, 1);
77 repo->objects->odb_tail = &repo->objects->odb->next;
79 expand_base_dir(&repo->objects->odb->path, o->object_dir,
80 repo->commondir, "objects");
82 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
84 free(repo->objects->alternate_db);
85 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
86 expand_base_dir(&repo->graft_file, o->graft_file,
87 repo->commondir, "info/grafts");
88 expand_base_dir(&repo->index_file, o->index_file,
89 repo->gitdir, "index");
92 void repo_set_hash_algo(struct repository *repo, int hash_algo)
94 repo->hash_algo = &hash_algos[hash_algo];
97 void repo_set_compat_hash_algo(struct repository *repo, int algo)
99 if (hash_algo_by_ptr(repo->hash_algo) == algo)
100 BUG("hash_algo and compat_hash_algo match");
101 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
102 if (repo->compat_hash_algo)
103 repo_read_loose_object_map(repo);
106 void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
108 repo->ref_storage_format = format;
112 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
113 * Return 0 upon success and a non-zero value upon failure.
115 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
117 int ret = 0;
118 int error = 0;
119 char *abspath = NULL;
120 const char *resolved_gitdir;
121 struct set_gitdir_args args = { NULL };
123 abspath = real_pathdup(gitdir, 0);
124 if (!abspath) {
125 ret = -1;
126 goto out;
129 /* 'gitdir' must reference the gitdir directly */
130 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
131 if (!resolved_gitdir) {
132 ret = -1;
133 goto out;
136 repo_set_gitdir(repo, resolved_gitdir, &args);
138 out:
139 free(abspath);
140 return ret;
143 void repo_set_worktree(struct repository *repo, const char *path)
145 repo->worktree = real_pathdup(path, 1);
147 trace2_def_repo(repo);
150 static int read_and_verify_repository_format(struct repository_format *format,
151 const char *commondir)
153 int ret = 0;
154 struct strbuf sb = STRBUF_INIT;
156 strbuf_addf(&sb, "%s/config", commondir);
157 read_repository_format(format, sb.buf);
158 strbuf_reset(&sb);
160 if (verify_repository_format(format, &sb) < 0) {
161 warning("%s", sb.buf);
162 ret = -1;
165 strbuf_release(&sb);
166 return ret;
170 * Initialize 'repo' based on the provided 'gitdir'.
171 * Return 0 upon success and a non-zero value upon failure.
173 int repo_init(struct repository *repo,
174 const char *gitdir,
175 const char *worktree)
177 struct repository_format format = REPOSITORY_FORMAT_INIT;
178 memset(repo, 0, sizeof(*repo));
180 initialize_repository(repo);
182 if (repo_init_gitdir(repo, gitdir))
183 goto error;
185 if (read_and_verify_repository_format(&format, repo->commondir))
186 goto error;
188 repo_set_hash_algo(repo, format.hash_algo);
189 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
190 repo_set_ref_storage_format(repo, format.ref_storage_format);
191 repo->repository_format_worktree_config = format.worktree_config;
193 /* take ownership of format.partial_clone */
194 repo->repository_format_partial_clone = format.partial_clone;
195 format.partial_clone = NULL;
197 if (worktree)
198 repo_set_worktree(repo, worktree);
200 if (repo->compat_hash_algo)
201 repo_read_loose_object_map(repo);
203 clear_repository_format(&format);
204 return 0;
206 error:
207 repo_clear(repo);
208 return -1;
211 int repo_submodule_init(struct repository *subrepo,
212 struct repository *superproject,
213 const char *path,
214 const struct object_id *treeish_name)
216 struct strbuf gitdir = STRBUF_INIT;
217 struct strbuf worktree = STRBUF_INIT;
218 int ret = 0;
220 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
221 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
223 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
225 * If initialization fails then it may be due to the submodule
226 * not being populated in the superproject's worktree. Instead
227 * we can try to initialize the submodule by finding it's gitdir
228 * in the superproject's 'modules' directory. In this case the
229 * submodule would not have a worktree.
231 const struct submodule *sub =
232 submodule_from_path(superproject, treeish_name, path);
233 if (!sub) {
234 ret = -1;
235 goto out;
238 strbuf_reset(&gitdir);
239 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
241 if (repo_init(subrepo, gitdir.buf, NULL)) {
242 ret = -1;
243 goto out;
247 subrepo->submodule_prefix = xstrfmt("%s%s/",
248 superproject->submodule_prefix ?
249 superproject->submodule_prefix :
250 "", path);
252 out:
253 strbuf_release(&gitdir);
254 strbuf_release(&worktree);
255 return ret;
258 static void repo_clear_path_cache(struct repo_path_cache *cache)
260 FREE_AND_NULL(cache->squash_msg);
261 FREE_AND_NULL(cache->squash_msg);
262 FREE_AND_NULL(cache->merge_msg);
263 FREE_AND_NULL(cache->merge_rr);
264 FREE_AND_NULL(cache->merge_mode);
265 FREE_AND_NULL(cache->merge_head);
266 FREE_AND_NULL(cache->fetch_head);
267 FREE_AND_NULL(cache->shallow);
270 void repo_clear(struct repository *repo)
272 FREE_AND_NULL(repo->gitdir);
273 FREE_AND_NULL(repo->commondir);
274 FREE_AND_NULL(repo->graft_file);
275 FREE_AND_NULL(repo->index_file);
276 FREE_AND_NULL(repo->worktree);
277 FREE_AND_NULL(repo->submodule_prefix);
279 raw_object_store_clear(repo->objects);
280 FREE_AND_NULL(repo->objects);
282 parsed_object_pool_clear(repo->parsed_objects);
283 FREE_AND_NULL(repo->parsed_objects);
285 if (repo->config) {
286 git_configset_clear(repo->config);
287 FREE_AND_NULL(repo->config);
290 if (repo->submodule_cache) {
291 submodule_cache_free(repo->submodule_cache);
292 repo->submodule_cache = NULL;
295 if (repo->index) {
296 discard_index(repo->index);
297 FREE_AND_NULL(repo->index);
300 if (repo->promisor_remote_config) {
301 promisor_remote_clear(repo->promisor_remote_config);
302 FREE_AND_NULL(repo->promisor_remote_config);
305 if (repo->remote_state) {
306 remote_state_clear(repo->remote_state);
307 FREE_AND_NULL(repo->remote_state);
310 repo_clear_path_cache(&repo->cached_paths);
313 int repo_read_index(struct repository *repo)
315 int res;
317 /* Complete the double-reference */
318 if (!repo->index) {
319 ALLOC_ARRAY(repo->index, 1);
320 index_state_init(repo->index, repo);
321 } else if (repo->index->repo != repo) {
322 BUG("repo's index should point back at itself");
325 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
327 prepare_repo_settings(repo);
328 if (repo->settings.command_requires_full_index)
329 ensure_full_index(repo->index);
332 * If sparse checkouts are in use, check whether paths with the
333 * SKIP_WORKTREE attribute are missing from the worktree; if not,
334 * clear that attribute for that path.
336 clear_skip_worktree_from_present_files(repo->index);
338 return res;
341 int repo_hold_locked_index(struct repository *repo,
342 struct lock_file *lf,
343 int flags)
345 if (!repo->index_file)
346 BUG("the repo hasn't been setup");
347 return hold_lock_file_for_update(lf, repo->index_file, flags);