completion: disable dwim on "git switch -d"
[git.git] / repository.c
blob5cad2dcc86a530ab088cda98a5c64bbe78102038
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_COMPATIBILITY_MACROS
6 #include "cache.h"
7 #include "repository.h"
8 #include "object-store.h"
9 #include "config.h"
10 #include "object.h"
11 #include "lockfile.h"
12 #include "submodule-config.h"
14 /* The main repository */
15 static struct repository the_repo;
16 struct repository *the_repository;
17 struct index_state the_index;
19 void initialize_the_repository(void)
21 the_repository = &the_repo;
23 the_repo.index = &the_index;
24 the_repo.objects = raw_object_store_new();
25 the_repo.parsed_objects = parsed_object_pool_new();
27 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
30 static void expand_base_dir(char **out, const char *in,
31 const char *base_dir, const char *def_in)
33 free(*out);
34 if (in)
35 *out = xstrdup(in);
36 else
37 *out = xstrfmt("%s/%s", base_dir, def_in);
40 static void repo_set_commondir(struct repository *repo,
41 const char *commondir)
43 struct strbuf sb = STRBUF_INIT;
45 free(repo->commondir);
47 if (commondir) {
48 repo->different_commondir = 1;
49 repo->commondir = xstrdup(commondir);
50 return;
53 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
54 repo->commondir = strbuf_detach(&sb, NULL);
57 void repo_set_gitdir(struct repository *repo,
58 const char *root,
59 const struct set_gitdir_args *o)
61 const char *gitfile = read_gitfile(root);
63 * repo->gitdir is saved because the caller could pass "root"
64 * that also points to repo->gitdir. We want to keep it alive
65 * until after xstrdup(root). Then we can free it.
67 char *old_gitdir = repo->gitdir;
69 repo->gitdir = xstrdup(gitfile ? gitfile : root);
70 free(old_gitdir);
72 repo_set_commondir(repo, o->commondir);
74 if (!repo->objects->odb) {
75 repo->objects->odb = xcalloc(1, sizeof(*repo->objects->odb));
76 repo->objects->odb_tail = &repo->objects->odb->next;
78 expand_base_dir(&repo->objects->odb->path, o->object_dir,
79 repo->commondir, "objects");
81 free(repo->objects->alternate_db);
82 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
83 expand_base_dir(&repo->graft_file, o->graft_file,
84 repo->commondir, "info/grafts");
85 expand_base_dir(&repo->index_file, o->index_file,
86 repo->gitdir, "index");
89 void repo_set_hash_algo(struct repository *repo, int hash_algo)
91 repo->hash_algo = &hash_algos[hash_algo];
95 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
96 * Return 0 upon success and a non-zero value upon failure.
98 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
100 int ret = 0;
101 int error = 0;
102 char *abspath = NULL;
103 const char *resolved_gitdir;
104 struct set_gitdir_args args = { NULL };
106 abspath = real_pathdup(gitdir, 0);
107 if (!abspath) {
108 ret = -1;
109 goto out;
112 /* 'gitdir' must reference the gitdir directly */
113 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
114 if (!resolved_gitdir) {
115 ret = -1;
116 goto out;
119 repo_set_gitdir(repo, resolved_gitdir, &args);
121 out:
122 free(abspath);
123 return ret;
126 void repo_set_worktree(struct repository *repo, const char *path)
128 repo->worktree = real_pathdup(path, 1);
130 trace2_def_repo(repo);
133 static int read_and_verify_repository_format(struct repository_format *format,
134 const char *commondir)
136 int ret = 0;
137 struct strbuf sb = STRBUF_INIT;
139 strbuf_addf(&sb, "%s/config", commondir);
140 read_repository_format(format, sb.buf);
141 strbuf_reset(&sb);
143 if (verify_repository_format(format, &sb) < 0) {
144 warning("%s", sb.buf);
145 ret = -1;
148 strbuf_release(&sb);
149 return ret;
153 * Initialize 'repo' based on the provided 'gitdir'.
154 * Return 0 upon success and a non-zero value upon failure.
156 int repo_init(struct repository *repo,
157 const char *gitdir,
158 const char *worktree)
160 struct repository_format format;
161 memset(repo, 0, sizeof(*repo));
163 repo->objects = raw_object_store_new();
164 repo->parsed_objects = parsed_object_pool_new();
166 if (repo_init_gitdir(repo, gitdir))
167 goto error;
169 if (read_and_verify_repository_format(&format, repo->commondir))
170 goto error;
172 repo_set_hash_algo(repo, format.hash_algo);
174 if (worktree)
175 repo_set_worktree(repo, worktree);
177 return 0;
179 error:
180 repo_clear(repo);
181 return -1;
184 int repo_submodule_init(struct repository *subrepo,
185 struct repository *superproject,
186 const struct submodule *sub)
188 struct strbuf gitdir = STRBUF_INIT;
189 struct strbuf worktree = STRBUF_INIT;
190 int ret = 0;
192 if (!sub) {
193 ret = -1;
194 goto out;
197 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
198 strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
200 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
202 * If initilization fails then it may be due to the submodule
203 * not being populated in the superproject's worktree. Instead
204 * we can try to initilize the submodule by finding it's gitdir
205 * in the superproject's 'modules' directory. In this case the
206 * submodule would not have a worktree.
208 strbuf_reset(&gitdir);
209 strbuf_repo_git_path(&gitdir, superproject,
210 "modules/%s", sub->name);
212 if (repo_init(subrepo, gitdir.buf, NULL)) {
213 ret = -1;
214 goto out;
218 subrepo->submodule_prefix = xstrfmt("%s%s/",
219 superproject->submodule_prefix ?
220 superproject->submodule_prefix :
221 "", sub->path);
223 out:
224 strbuf_release(&gitdir);
225 strbuf_release(&worktree);
226 return ret;
229 void repo_clear(struct repository *repo)
231 FREE_AND_NULL(repo->gitdir);
232 FREE_AND_NULL(repo->commondir);
233 FREE_AND_NULL(repo->graft_file);
234 FREE_AND_NULL(repo->index_file);
235 FREE_AND_NULL(repo->worktree);
236 FREE_AND_NULL(repo->submodule_prefix);
238 raw_object_store_clear(repo->objects);
239 FREE_AND_NULL(repo->objects);
241 parsed_object_pool_clear(repo->parsed_objects);
242 FREE_AND_NULL(repo->parsed_objects);
244 if (repo->config) {
245 git_configset_clear(repo->config);
246 FREE_AND_NULL(repo->config);
249 if (repo->submodule_cache) {
250 submodule_cache_free(repo->submodule_cache);
251 repo->submodule_cache = NULL;
254 if (repo->index) {
255 discard_index(repo->index);
256 if (repo->index != &the_index)
257 FREE_AND_NULL(repo->index);
261 int repo_read_index(struct repository *repo)
263 if (!repo->index)
264 repo->index = xcalloc(1, sizeof(*repo->index));
266 return read_index_from(repo->index, repo->index_file, repo->gitdir);
269 int repo_hold_locked_index(struct repository *repo,
270 struct lock_file *lf,
271 int flags)
273 if (!repo->index_file)
274 BUG("the repo hasn't been setup");
275 return hold_lock_file_for_update(lf, repo->index_file, flags);