fast-import: allow unquoted empty path for root
[git/gitster.git] / repository.c
blobe15b416944dfb21f752093fab777fabf475d4f31
1 /*
2 * not really _using_ the compat macros, just make sure the_index
3 * declaration matches the definition in this file.
4 */
5 #define USE_THE_INDEX_VARIABLE
6 #include "git-compat-util.h"
7 #include "abspath.h"
8 #include "repository.h"
9 #include "object-store-ll.h"
10 #include "config.h"
11 #include "object.h"
12 #include "lockfile.h"
13 #include "path.h"
14 #include "read-cache-ll.h"
15 #include "remote.h"
16 #include "setup.h"
17 #include "loose.h"
18 #include "submodule-config.h"
19 #include "sparse-index.h"
20 #include "trace2.h"
21 #include "promisor-remote.h"
23 /* The main repository */
24 static struct repository the_repo;
25 struct repository *the_repository;
26 struct index_state the_index;
28 void initialize_the_repository(void)
30 the_repository = &the_repo;
32 the_repo.index = &the_index;
33 the_repo.objects = raw_object_store_new();
34 the_repo.remote_state = remote_state_new();
35 the_repo.parsed_objects = parsed_object_pool_new();
37 index_state_init(&the_index, the_repository);
39 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
42 static void expand_base_dir(char **out, const char *in,
43 const char *base_dir, const char *def_in)
45 free(*out);
46 if (in)
47 *out = xstrdup(in);
48 else
49 *out = xstrfmt("%s/%s", base_dir, def_in);
52 static void repo_set_commondir(struct repository *repo,
53 const char *commondir)
55 struct strbuf sb = STRBUF_INIT;
57 free(repo->commondir);
59 if (commondir) {
60 repo->different_commondir = 1;
61 repo->commondir = xstrdup(commondir);
62 return;
65 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
66 repo->commondir = strbuf_detach(&sb, NULL);
69 void repo_set_gitdir(struct repository *repo,
70 const char *root,
71 const struct set_gitdir_args *o)
73 const char *gitfile = read_gitfile(root);
75 * repo->gitdir is saved because the caller could pass "root"
76 * that also points to repo->gitdir. We want to keep it alive
77 * until after xstrdup(root). Then we can free it.
79 char *old_gitdir = repo->gitdir;
81 repo->gitdir = xstrdup(gitfile ? gitfile : root);
82 free(old_gitdir);
84 repo_set_commondir(repo, o->commondir);
86 if (!repo->objects->odb) {
87 CALLOC_ARRAY(repo->objects->odb, 1);
88 repo->objects->odb_tail = &repo->objects->odb->next;
90 expand_base_dir(&repo->objects->odb->path, o->object_dir,
91 repo->commondir, "objects");
93 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
95 free(repo->objects->alternate_db);
96 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
97 expand_base_dir(&repo->graft_file, o->graft_file,
98 repo->commondir, "info/grafts");
99 expand_base_dir(&repo->index_file, o->index_file,
100 repo->gitdir, "index");
103 void repo_set_hash_algo(struct repository *repo, int hash_algo)
105 repo->hash_algo = &hash_algos[hash_algo];
108 void repo_set_compat_hash_algo(struct repository *repo, int algo)
110 if (hash_algo_by_ptr(repo->hash_algo) == algo)
111 BUG("hash_algo and compat_hash_algo match");
112 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
113 if (repo->compat_hash_algo)
114 repo_read_loose_object_map(repo);
117 void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
119 repo->ref_storage_format = format;
123 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
124 * Return 0 upon success and a non-zero value upon failure.
126 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
128 int ret = 0;
129 int error = 0;
130 char *abspath = NULL;
131 const char *resolved_gitdir;
132 struct set_gitdir_args args = { NULL };
134 abspath = real_pathdup(gitdir, 0);
135 if (!abspath) {
136 ret = -1;
137 goto out;
140 /* 'gitdir' must reference the gitdir directly */
141 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
142 if (!resolved_gitdir) {
143 ret = -1;
144 goto out;
147 repo_set_gitdir(repo, resolved_gitdir, &args);
149 out:
150 free(abspath);
151 return ret;
154 void repo_set_worktree(struct repository *repo, const char *path)
156 repo->worktree = real_pathdup(path, 1);
158 trace2_def_repo(repo);
161 static int read_and_verify_repository_format(struct repository_format *format,
162 const char *commondir)
164 int ret = 0;
165 struct strbuf sb = STRBUF_INIT;
167 strbuf_addf(&sb, "%s/config", commondir);
168 read_repository_format(format, sb.buf);
169 strbuf_reset(&sb);
171 if (verify_repository_format(format, &sb) < 0) {
172 warning("%s", sb.buf);
173 ret = -1;
176 strbuf_release(&sb);
177 return ret;
181 * Initialize 'repo' based on the provided 'gitdir'.
182 * Return 0 upon success and a non-zero value upon failure.
184 int repo_init(struct repository *repo,
185 const char *gitdir,
186 const char *worktree)
188 struct repository_format format = REPOSITORY_FORMAT_INIT;
189 memset(repo, 0, sizeof(*repo));
191 repo->objects = raw_object_store_new();
192 repo->parsed_objects = parsed_object_pool_new();
193 repo->remote_state = remote_state_new();
195 if (repo_init_gitdir(repo, gitdir))
196 goto error;
198 if (read_and_verify_repository_format(&format, repo->commondir))
199 goto error;
201 repo_set_hash_algo(repo, format.hash_algo);
202 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
203 repo_set_ref_storage_format(repo, format.ref_storage_format);
204 repo->repository_format_worktree_config = format.worktree_config;
206 /* take ownership of format.partial_clone */
207 repo->repository_format_partial_clone = format.partial_clone;
208 format.partial_clone = NULL;
210 if (worktree)
211 repo_set_worktree(repo, worktree);
213 if (repo->compat_hash_algo)
214 repo_read_loose_object_map(repo);
216 clear_repository_format(&format);
217 return 0;
219 error:
220 repo_clear(repo);
221 return -1;
224 int repo_submodule_init(struct repository *subrepo,
225 struct repository *superproject,
226 const char *path,
227 const struct object_id *treeish_name)
229 struct strbuf gitdir = STRBUF_INIT;
230 struct strbuf worktree = STRBUF_INIT;
231 int ret = 0;
233 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
234 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
236 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
238 * If initialization fails then it may be due to the submodule
239 * not being populated in the superproject's worktree. Instead
240 * we can try to initialize the submodule by finding it's gitdir
241 * in the superproject's 'modules' directory. In this case the
242 * submodule would not have a worktree.
244 const struct submodule *sub =
245 submodule_from_path(superproject, treeish_name, path);
246 if (!sub) {
247 ret = -1;
248 goto out;
251 strbuf_reset(&gitdir);
252 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
254 if (repo_init(subrepo, gitdir.buf, NULL)) {
255 ret = -1;
256 goto out;
260 subrepo->submodule_prefix = xstrfmt("%s%s/",
261 superproject->submodule_prefix ?
262 superproject->submodule_prefix :
263 "", path);
265 out:
266 strbuf_release(&gitdir);
267 strbuf_release(&worktree);
268 return ret;
271 static void repo_clear_path_cache(struct repo_path_cache *cache)
273 FREE_AND_NULL(cache->squash_msg);
274 FREE_AND_NULL(cache->squash_msg);
275 FREE_AND_NULL(cache->merge_msg);
276 FREE_AND_NULL(cache->merge_rr);
277 FREE_AND_NULL(cache->merge_mode);
278 FREE_AND_NULL(cache->merge_head);
279 FREE_AND_NULL(cache->fetch_head);
280 FREE_AND_NULL(cache->shallow);
283 void repo_clear(struct repository *repo)
285 FREE_AND_NULL(repo->gitdir);
286 FREE_AND_NULL(repo->commondir);
287 FREE_AND_NULL(repo->graft_file);
288 FREE_AND_NULL(repo->index_file);
289 FREE_AND_NULL(repo->worktree);
290 FREE_AND_NULL(repo->submodule_prefix);
292 raw_object_store_clear(repo->objects);
293 FREE_AND_NULL(repo->objects);
295 parsed_object_pool_clear(repo->parsed_objects);
296 FREE_AND_NULL(repo->parsed_objects);
298 if (repo->config) {
299 git_configset_clear(repo->config);
300 FREE_AND_NULL(repo->config);
303 if (repo->submodule_cache) {
304 submodule_cache_free(repo->submodule_cache);
305 repo->submodule_cache = NULL;
308 if (repo->index) {
309 discard_index(repo->index);
310 if (repo->index != &the_index)
311 FREE_AND_NULL(repo->index);
314 if (repo->promisor_remote_config) {
315 promisor_remote_clear(repo->promisor_remote_config);
316 FREE_AND_NULL(repo->promisor_remote_config);
319 if (repo->remote_state) {
320 remote_state_clear(repo->remote_state);
321 FREE_AND_NULL(repo->remote_state);
324 repo_clear_path_cache(&repo->cached_paths);
327 int repo_read_index(struct repository *repo)
329 int res;
331 /* Complete the double-reference */
332 if (!repo->index) {
333 ALLOC_ARRAY(repo->index, 1);
334 index_state_init(repo->index, repo);
335 } else if (repo->index->repo != repo) {
336 BUG("repo's index should point back at itself");
339 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
341 prepare_repo_settings(repo);
342 if (repo->settings.command_requires_full_index)
343 ensure_full_index(repo->index);
346 * If sparse checkouts are in use, check whether paths with the
347 * SKIP_WORKTREE attribute are missing from the worktree; if not,
348 * clear that attribute for that path.
350 clear_skip_worktree_from_present_files(repo->index);
352 return res;
355 int repo_hold_locked_index(struct repository *repo,
356 struct lock_file *lf,
357 int flags)
359 if (!repo->index_file)
360 BUG("the repo hasn't been setup");
361 return hold_lock_file_for_update(lf, repo->index_file, flags);