2 #include "repository.h"
3 #include "object-store.h"
6 #include "submodule-config.h"
8 /* The main repository */
9 static struct repository the_repo;
10 struct repository *the_repository;
12 void initialize_the_repository(void)
14 the_repository = &the_repo;
16 the_repo.index = &the_index;
17 the_repo.objects = raw_object_store_new();
18 the_repo.parsed_objects = parsed_object_pool_new();
20 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
23 static void expand_base_dir(char **out, const char *in,
24 const char *base_dir, const char *def_in)
30 *out = xstrfmt("%s/%s", base_dir, def_in);
33 static void repo_set_commondir(struct repository *repo,
34 const char *commondir)
36 struct strbuf sb = STRBUF_INIT;
38 free(repo->commondir);
41 repo->different_commondir = 1;
42 repo->commondir = xstrdup(commondir);
46 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
47 repo->commondir = strbuf_detach(&sb, NULL);
50 void repo_set_gitdir(struct repository *repo,
52 const struct set_gitdir_args *o)
54 const char *gitfile = read_gitfile(root);
56 * repo->gitdir is saved because the caller could pass "root"
57 * that also points to repo->gitdir. We want to keep it alive
58 * until after xstrdup(root). Then we can free it.
60 char *old_gitdir = repo->gitdir;
62 repo->gitdir = xstrdup(gitfile ? gitfile : root);
65 repo_set_commondir(repo, o->commondir);
66 expand_base_dir(&repo->objects->objectdir, o->object_dir,
67 repo->commondir, "objects");
68 free(repo->objects->alternate_db);
69 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
70 expand_base_dir(&repo->graft_file, o->graft_file,
71 repo->commondir, "info/grafts");
72 expand_base_dir(&repo->index_file, o->index_file,
73 repo->gitdir, "index");
76 void repo_set_hash_algo(struct repository *repo, int hash_algo)
78 repo->hash_algo = &hash_algos[hash_algo];
82 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
83 * Return 0 upon success and a non-zero value upon failure.
85 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
90 const char *resolved_gitdir;
91 struct set_gitdir_args args = { NULL };
93 abspath = real_pathdup(gitdir, 0);
99 /* 'gitdir' must reference the gitdir directly */
100 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
101 if (!resolved_gitdir) {
106 repo_set_gitdir(repo, resolved_gitdir, &args);
113 void repo_set_worktree(struct repository *repo, const char *path)
115 repo->worktree = real_pathdup(path, 1);
118 static int read_and_verify_repository_format(struct repository_format *format,
119 const char *commondir)
122 struct strbuf sb = STRBUF_INIT;
124 strbuf_addf(&sb, "%s/config", commondir);
125 read_repository_format(format, sb.buf);
128 if (verify_repository_format(format, &sb) < 0) {
129 warning("%s", sb.buf);
138 * Initialize 'repo' based on the provided 'gitdir'.
139 * Return 0 upon success and a non-zero value upon failure.
141 int repo_init(struct repository *repo,
143 const char *worktree)
145 struct repository_format format;
146 memset(repo, 0, sizeof(*repo));
148 repo->objects = raw_object_store_new();
149 repo->parsed_objects = parsed_object_pool_new();
151 if (repo_init_gitdir(repo, gitdir))
154 if (read_and_verify_repository_format(&format, repo->commondir))
157 repo_set_hash_algo(repo, format.hash_algo);
160 repo_set_worktree(repo, worktree);
169 int repo_submodule_init(struct repository *subrepo,
170 struct repository *superproject,
171 const struct submodule *sub)
173 struct strbuf gitdir = STRBUF_INIT;
174 struct strbuf worktree = STRBUF_INIT;
182 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
183 strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
185 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
187 * If initilization fails then it may be due to the submodule
188 * not being populated in the superproject's worktree. Instead
189 * we can try to initilize the submodule by finding it's gitdir
190 * in the superproject's 'modules' directory. In this case the
191 * submodule would not have a worktree.
193 strbuf_reset(&gitdir);
194 strbuf_repo_git_path(&gitdir, superproject,
195 "modules/%s", sub->name);
197 if (repo_init(subrepo, gitdir.buf, NULL)) {
203 subrepo->submodule_prefix = xstrfmt("%s%s/",
204 superproject->submodule_prefix ?
205 superproject->submodule_prefix :
209 strbuf_release(&gitdir);
210 strbuf_release(&worktree);
214 void repo_clear(struct repository *repo)
216 FREE_AND_NULL(repo->gitdir);
217 FREE_AND_NULL(repo->commondir);
218 FREE_AND_NULL(repo->graft_file);
219 FREE_AND_NULL(repo->index_file);
220 FREE_AND_NULL(repo->worktree);
221 FREE_AND_NULL(repo->submodule_prefix);
223 raw_object_store_clear(repo->objects);
224 FREE_AND_NULL(repo->objects);
226 parsed_object_pool_clear(repo->parsed_objects);
227 FREE_AND_NULL(repo->parsed_objects);
230 git_configset_clear(repo->config);
231 FREE_AND_NULL(repo->config);
234 if (repo->submodule_cache) {
235 submodule_cache_free(repo->submodule_cache);
236 repo->submodule_cache = NULL;
240 discard_index(repo->index);
241 if (repo->index != &the_index)
242 FREE_AND_NULL(repo->index);
246 int repo_read_index(struct repository *repo)
249 repo->index = xcalloc(1, sizeof(*repo->index));
251 return read_index_from(repo->index, repo->index_file, repo->gitdir);