Merge branch 'tq/t1510'
[git/raj.git] / repository.c
blobbeff3caa9e24a902e560372cdab3c337769c7c89
1 #include "cache.h"
2 #include "repository.h"
3 #include "object-store.h"
4 #include "config.h"
5 #include "submodule-config.h"
7 /* The main repository */
8 static struct repository the_repo;
9 struct repository *the_repository;
11 void initialize_the_repository(void)
13 the_repository = &the_repo;
15 the_repo.index = &the_index;
16 the_repo.objects = raw_object_store_new();
17 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
20 static void expand_base_dir(char **out, const char *in,
21 const char *base_dir, const char *def_in)
23 free(*out);
24 if (in)
25 *out = xstrdup(in);
26 else
27 *out = xstrfmt("%s/%s", base_dir, def_in);
30 static void repo_set_commondir(struct repository *repo,
31 const char *commondir)
33 struct strbuf sb = STRBUF_INIT;
35 free(repo->commondir);
37 if (commondir) {
38 repo->different_commondir = 1;
39 repo->commondir = xstrdup(commondir);
40 return;
43 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
44 repo->commondir = strbuf_detach(&sb, NULL);
47 void repo_set_gitdir(struct repository *repo,
48 const char *root,
49 const struct set_gitdir_args *o)
51 const char *gitfile = read_gitfile(root);
53 * repo->gitdir is saved because the caller could pass "root"
54 * that also points to repo->gitdir. We want to keep it alive
55 * until after xstrdup(root). Then we can free it.
57 char *old_gitdir = repo->gitdir;
59 repo->gitdir = xstrdup(gitfile ? gitfile : root);
60 free(old_gitdir);
62 repo_set_commondir(repo, o->commondir);
63 expand_base_dir(&repo->objects->objectdir, o->object_dir,
64 repo->commondir, "objects");
65 free(repo->objects->alternate_db);
66 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
67 expand_base_dir(&repo->graft_file, o->graft_file,
68 repo->commondir, "info/grafts");
69 expand_base_dir(&repo->index_file, o->index_file,
70 repo->gitdir, "index");
73 void repo_set_hash_algo(struct repository *repo, int hash_algo)
75 repo->hash_algo = &hash_algos[hash_algo];
79 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
80 * Return 0 upon success and a non-zero value upon failure.
82 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
84 int ret = 0;
85 int error = 0;
86 char *abspath = NULL;
87 const char *resolved_gitdir;
88 struct set_gitdir_args args = { NULL };
90 abspath = real_pathdup(gitdir, 0);
91 if (!abspath) {
92 ret = -1;
93 goto out;
96 /* 'gitdir' must reference the gitdir directly */
97 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
98 if (!resolved_gitdir) {
99 ret = -1;
100 goto out;
103 repo_set_gitdir(repo, resolved_gitdir, &args);
105 out:
106 free(abspath);
107 return ret;
110 void repo_set_worktree(struct repository *repo, const char *path)
112 repo->worktree = real_pathdup(path, 1);
115 static int read_and_verify_repository_format(struct repository_format *format,
116 const char *commondir)
118 int ret = 0;
119 struct strbuf sb = STRBUF_INIT;
121 strbuf_addf(&sb, "%s/config", commondir);
122 read_repository_format(format, sb.buf);
123 strbuf_reset(&sb);
125 if (verify_repository_format(format, &sb) < 0) {
126 warning("%s", sb.buf);
127 ret = -1;
130 strbuf_release(&sb);
131 return ret;
135 * Initialize 'repo' based on the provided 'gitdir'.
136 * Return 0 upon success and a non-zero value upon failure.
138 int repo_init(struct repository *repo,
139 const char *gitdir,
140 const char *worktree)
142 struct repository_format format;
143 memset(repo, 0, sizeof(*repo));
145 repo->objects = raw_object_store_new();
147 if (repo_init_gitdir(repo, gitdir))
148 goto error;
150 if (read_and_verify_repository_format(&format, repo->commondir))
151 goto error;
153 repo_set_hash_algo(repo, format.hash_algo);
155 if (worktree)
156 repo_set_worktree(repo, worktree);
158 return 0;
160 error:
161 repo_clear(repo);
162 return -1;
166 * Initialize 'submodule' as the submodule given by 'path' in parent repository
167 * 'superproject'.
168 * Return 0 upon success and a non-zero value upon failure.
170 int repo_submodule_init(struct repository *submodule,
171 struct repository *superproject,
172 const char *path)
174 const struct submodule *sub;
175 struct strbuf gitdir = STRBUF_INIT;
176 struct strbuf worktree = STRBUF_INIT;
177 int ret = 0;
179 sub = submodule_from_path(superproject, &null_oid, path);
180 if (!sub) {
181 ret = -1;
182 goto out;
185 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
186 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
188 if (repo_init(submodule, gitdir.buf, worktree.buf)) {
190 * If initilization fails then it may be due to the submodule
191 * not being populated in the superproject's worktree. Instead
192 * we can try to initilize the submodule by finding it's gitdir
193 * in the superproject's 'modules' directory. In this case the
194 * submodule would not have a worktree.
196 strbuf_reset(&gitdir);
197 strbuf_repo_git_path(&gitdir, superproject,
198 "modules/%s", sub->name);
200 if (repo_init(submodule, gitdir.buf, NULL)) {
201 ret = -1;
202 goto out;
206 submodule->submodule_prefix = xstrfmt("%s%s/",
207 superproject->submodule_prefix ?
208 superproject->submodule_prefix :
209 "", path);
211 out:
212 strbuf_release(&gitdir);
213 strbuf_release(&worktree);
214 return ret;
217 void repo_clear(struct repository *repo)
219 FREE_AND_NULL(repo->gitdir);
220 FREE_AND_NULL(repo->commondir);
221 FREE_AND_NULL(repo->graft_file);
222 FREE_AND_NULL(repo->index_file);
223 FREE_AND_NULL(repo->worktree);
224 FREE_AND_NULL(repo->submodule_prefix);
226 raw_object_store_clear(repo->objects);
227 FREE_AND_NULL(repo->objects);
229 if (repo->config) {
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;
239 if (repo->index) {
240 discard_index(repo->index);
241 FREE_AND_NULL(repo->index);
245 int repo_read_index(struct repository *repo)
247 if (!repo->index)
248 repo->index = xcalloc(1, sizeof(*repo->index));
250 return read_index_from(repo->index, repo->index_file, repo->gitdir);