repository: initialize the_repository in main()
[git/git.git] / repository.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "submodule-config.h"
5
6 /* The main repository */
7 static struct repository the_repo;
8 struct repository *the_repository;
9
10 void initialize_the_repository(void)
11 {
12 the_repository = &the_repo;
13
14 the_repo.index = &the_index;
15 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
16 }
17
18 static char *git_path_from_env(const char *envvar, const char *git_dir,
19 const char *path, int fromenv)
20 {
21 if (fromenv) {
22 const char *value = getenv(envvar);
23 if (value)
24 return xstrdup(value);
25 }
26
27 return xstrfmt("%s/%s", git_dir, path);
28 }
29
30 static int find_common_dir(struct strbuf *sb, const char *gitdir, int fromenv)
31 {
32 if (fromenv) {
33 const char *value = getenv(GIT_COMMON_DIR_ENVIRONMENT);
34 if (value) {
35 strbuf_addstr(sb, value);
36 return 1;
37 }
38 }
39
40 return get_common_dir_noenv(sb, gitdir);
41 }
42
43 static void repo_setup_env(struct repository *repo)
44 {
45 struct strbuf sb = STRBUF_INIT;
46
47 repo->different_commondir = find_common_dir(&sb, repo->gitdir,
48 !repo->ignore_env);
49 free(repo->commondir);
50 repo->commondir = strbuf_detach(&sb, NULL);
51 free(repo->objectdir);
52 repo->objectdir = git_path_from_env(DB_ENVIRONMENT, repo->commondir,
53 "objects", !repo->ignore_env);
54 free(repo->graft_file);
55 repo->graft_file = git_path_from_env(GRAFT_ENVIRONMENT, repo->commondir,
56 "info/grafts", !repo->ignore_env);
57 free(repo->index_file);
58 repo->index_file = git_path_from_env(INDEX_ENVIRONMENT, repo->gitdir,
59 "index", !repo->ignore_env);
60 }
61
62 void repo_set_gitdir(struct repository *repo, const char *path)
63 {
64 const char *gitfile = read_gitfile(path);
65 char *old_gitdir = repo->gitdir;
66
67 repo->gitdir = xstrdup(gitfile ? gitfile : path);
68 repo_setup_env(repo);
69
70 free(old_gitdir);
71 }
72
73 void repo_set_hash_algo(struct repository *repo, int hash_algo)
74 {
75 repo->hash_algo = &hash_algos[hash_algo];
76 }
77
78 /*
79 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
80 * Return 0 upon success and a non-zero value upon failure.
81 */
82 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
83 {
84 int ret = 0;
85 int error = 0;
86 char *abspath = NULL;
87 const char *resolved_gitdir;
88
89 abspath = real_pathdup(gitdir, 0);
90 if (!abspath) {
91 ret = -1;
92 goto out;
93 }
94
95 /* 'gitdir' must reference the gitdir directly */
96 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
97 if (!resolved_gitdir) {
98 ret = -1;
99 goto out;
100 }
101
102 repo_set_gitdir(repo, resolved_gitdir);
103
104 out:
105 free(abspath);
106 return ret;
107 }
108
109 void repo_set_worktree(struct repository *repo, const char *path)
110 {
111 repo->worktree = real_pathdup(path, 1);
112 }
113
114 static int read_and_verify_repository_format(struct repository_format *format,
115 const char *commondir)
116 {
117 int ret = 0;
118 struct strbuf sb = STRBUF_INIT;
119
120 strbuf_addf(&sb, "%s/config", commondir);
121 read_repository_format(format, sb.buf);
122 strbuf_reset(&sb);
123
124 if (verify_repository_format(format, &sb) < 0) {
125 warning("%s", sb.buf);
126 ret = -1;
127 }
128
129 strbuf_release(&sb);
130 return ret;
131 }
132
133 /*
134 * Initialize 'repo' based on the provided 'gitdir'.
135 * Return 0 upon success and a non-zero value upon failure.
136 */
137 static int repo_init(struct repository *repo,
138 const char *gitdir,
139 const char *worktree)
140 {
141 struct repository_format format;
142 memset(repo, 0, sizeof(*repo));
143
144 repo->ignore_env = 1;
145
146 if (repo_init_gitdir(repo, gitdir))
147 goto error;
148
149 if (read_and_verify_repository_format(&format, repo->commondir))
150 goto error;
151
152 repo_set_hash_algo(repo, format.hash_algo);
153
154 if (worktree)
155 repo_set_worktree(repo, worktree);
156
157 return 0;
158
159 error:
160 repo_clear(repo);
161 return -1;
162 }
163
164 /*
165 * Initialize 'submodule' as the submodule given by 'path' in parent repository
166 * 'superproject'.
167 * Return 0 upon success and a non-zero value upon failure.
168 */
169 int repo_submodule_init(struct repository *submodule,
170 struct repository *superproject,
171 const char *path)
172 {
173 const struct submodule *sub;
174 struct strbuf gitdir = STRBUF_INIT;
175 struct strbuf worktree = STRBUF_INIT;
176 int ret = 0;
177
178 sub = submodule_from_cache(superproject, &null_oid, path);
179 if (!sub) {
180 ret = -1;
181 goto out;
182 }
183
184 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
185 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
186
187 if (repo_init(submodule, gitdir.buf, worktree.buf)) {
188 /*
189 * If initilization fails then it may be due to the submodule
190 * not being populated in the superproject's worktree. Instead
191 * we can try to initilize the submodule by finding it's gitdir
192 * in the superproject's 'modules' directory. In this case the
193 * submodule would not have a worktree.
194 */
195 strbuf_reset(&gitdir);
196 strbuf_repo_git_path(&gitdir, superproject,
197 "modules/%s", sub->name);
198
199 if (repo_init(submodule, gitdir.buf, NULL)) {
200 ret = -1;
201 goto out;
202 }
203 }
204
205 submodule->submodule_prefix = xstrfmt("%s%s/",
206 superproject->submodule_prefix ?
207 superproject->submodule_prefix :
208 "", path);
209
210 out:
211 strbuf_release(&gitdir);
212 strbuf_release(&worktree);
213 return ret;
214 }
215
216 void repo_clear(struct repository *repo)
217 {
218 FREE_AND_NULL(repo->gitdir);
219 FREE_AND_NULL(repo->commondir);
220 FREE_AND_NULL(repo->objectdir);
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);
225
226 if (repo->config) {
227 git_configset_clear(repo->config);
228 FREE_AND_NULL(repo->config);
229 }
230
231 if (repo->submodule_cache) {
232 submodule_cache_free(repo->submodule_cache);
233 repo->submodule_cache = NULL;
234 }
235
236 if (repo->index) {
237 discard_index(repo->index);
238 FREE_AND_NULL(repo->index);
239 }
240 }
241
242 int repo_read_index(struct repository *repo)
243 {
244 if (!repo->index)
245 repo->index = xcalloc(1, sizeof(*repo->index));
246
247 return read_index_from(repo->index, repo->index_file, repo->gitdir);
248 }