Merge branch 'ab/config-based-hooks-base' into seen
[git] / repository.c
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"
13 #include "sparse-index.h"
14 #include "promisor-remote.h"
15
16 /* The main repository */
17 static struct repository the_repo;
18 struct repository *the_repository;
19 struct index_state the_index;
20
21 void initialize_the_repository(void)
22 {
23         the_repository = &the_repo;
24
25         the_repo.index = &the_index;
26         the_repo.objects = raw_object_store_new();
27         the_repo.parsed_objects = parsed_object_pool_new();
28
29         repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
30 }
31
32 static void expand_base_dir(char **out, const char *in,
33                             const char *base_dir, const char *def_in)
34 {
35         free(*out);
36         if (in)
37                 *out = xstrdup(in);
38         else
39                 *out = xstrfmt("%s/%s", base_dir, def_in);
40 }
41
42 static void repo_set_commondir(struct repository *repo,
43                                const char *commondir)
44 {
45         struct strbuf sb = STRBUF_INIT;
46
47         free(repo->commondir);
48
49         if (commondir) {
50                 repo->different_commondir = 1;
51                 repo->commondir = xstrdup(commondir);
52                 return;
53         }
54
55         repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
56         repo->commondir = strbuf_detach(&sb, NULL);
57 }
58
59 void repo_set_gitdir(struct repository *repo,
60                      const char *root,
61                      const struct set_gitdir_args *o)
62 {
63         const char *gitfile = read_gitfile(root);
64         /*
65          * repo->gitdir is saved because the caller could pass "root"
66          * that also points to repo->gitdir. We want to keep it alive
67          * until after xstrdup(root). Then we can free it.
68          */
69         char *old_gitdir = repo->gitdir;
70
71         repo->gitdir = xstrdup(gitfile ? gitfile : root);
72         free(old_gitdir);
73
74         repo_set_commondir(repo, o->commondir);
75
76         if (!repo->objects->odb) {
77                 CALLOC_ARRAY(repo->objects->odb, 1);
78                 repo->objects->odb_tail = &repo->objects->odb->next;
79         }
80         expand_base_dir(&repo->objects->odb->path, o->object_dir,
81                         repo->commondir, "objects");
82
83         free(repo->objects->alternate_db);
84         repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
85         expand_base_dir(&repo->graft_file, o->graft_file,
86                         repo->commondir, "info/grafts");
87         expand_base_dir(&repo->index_file, o->index_file,
88                         repo->gitdir, "index");
89 }
90
91 void repo_set_hash_algo(struct repository *repo, int hash_algo)
92 {
93         repo->hash_algo = &hash_algos[hash_algo];
94 }
95
96 /*
97  * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
98  * Return 0 upon success and a non-zero value upon failure.
99  */
100 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
101 {
102         int ret = 0;
103         int error = 0;
104         char *abspath = NULL;
105         const char *resolved_gitdir;
106         struct set_gitdir_args args = { NULL };
107
108         abspath = real_pathdup(gitdir, 0);
109         if (!abspath) {
110                 ret = -1;
111                 goto out;
112         }
113
114         /* 'gitdir' must reference the gitdir directly */
115         resolved_gitdir = resolve_gitdir_gently(abspath, &error);
116         if (!resolved_gitdir) {
117                 ret = -1;
118                 goto out;
119         }
120
121         repo_set_gitdir(repo, resolved_gitdir, &args);
122
123 out:
124         free(abspath);
125         return ret;
126 }
127
128 void repo_set_worktree(struct repository *repo, const char *path)
129 {
130         repo->worktree = real_pathdup(path, 1);
131
132         trace2_def_repo(repo);
133 }
134
135 static int read_and_verify_repository_format(struct repository_format *format,
136                                              const char *commondir)
137 {
138         int ret = 0;
139         struct strbuf sb = STRBUF_INIT;
140
141         strbuf_addf(&sb, "%s/config", commondir);
142         read_repository_format(format, sb.buf);
143         strbuf_reset(&sb);
144
145         if (verify_repository_format(format, &sb) < 0) {
146                 warning("%s", sb.buf);
147                 ret = -1;
148         }
149
150         strbuf_release(&sb);
151         return ret;
152 }
153
154 /*
155  * Initialize 'repo' based on the provided 'gitdir'.
156  * Return 0 upon success and a non-zero value upon failure.
157  */
158 int repo_init(struct repository *repo,
159               const char *gitdir,
160               const char *worktree)
161 {
162         struct repository_format format = REPOSITORY_FORMAT_INIT;
163         memset(repo, 0, sizeof(*repo));
164
165         repo->objects = raw_object_store_new();
166         repo->parsed_objects = parsed_object_pool_new();
167
168         if (repo_init_gitdir(repo, gitdir))
169                 goto error;
170
171         if (read_and_verify_repository_format(&format, repo->commondir))
172                 goto error;
173
174         repo_set_hash_algo(repo, format.hash_algo);
175
176         repo->repository_format_partial_clone = format.partial_clone;
177         format.partial_clone = NULL;
178
179         if (worktree)
180                 repo_set_worktree(repo, worktree);
181
182         clear_repository_format(&format);
183         return 0;
184
185 error:
186         repo_clear(repo);
187         return -1;
188 }
189
190 int repo_submodule_init(struct repository *subrepo,
191                         struct repository *superproject,
192                         const struct submodule *sub)
193 {
194         struct strbuf gitdir = STRBUF_INIT;
195         struct strbuf worktree = STRBUF_INIT;
196         int ret = 0;
197
198         if (!sub) {
199                 ret = -1;
200                 goto out;
201         }
202
203         strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
204         strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
205
206         if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
207                 /*
208                  * If initialization fails then it may be due to the submodule
209                  * not being populated in the superproject's worktree.  Instead
210                  * we can try to initialize the submodule by finding it's gitdir
211                  * in the superproject's 'modules' directory.  In this case the
212                  * submodule would not have a worktree.
213                  */
214                 strbuf_reset(&gitdir);
215                 strbuf_repo_git_path(&gitdir, superproject,
216                                      "modules/%s", sub->name);
217
218                 if (repo_init(subrepo, gitdir.buf, NULL)) {
219                         ret = -1;
220                         goto out;
221                 }
222         }
223
224         subrepo->submodule_prefix = xstrfmt("%s%s/",
225                                             superproject->submodule_prefix ?
226                                             superproject->submodule_prefix :
227                                             "", sub->path);
228
229 out:
230         strbuf_release(&gitdir);
231         strbuf_release(&worktree);
232         return ret;
233 }
234
235 void repo_clear(struct repository *repo)
236 {
237         FREE_AND_NULL(repo->gitdir);
238         FREE_AND_NULL(repo->commondir);
239         FREE_AND_NULL(repo->graft_file);
240         FREE_AND_NULL(repo->index_file);
241         FREE_AND_NULL(repo->worktree);
242         FREE_AND_NULL(repo->submodule_prefix);
243
244         raw_object_store_clear(repo->objects);
245         FREE_AND_NULL(repo->objects);
246
247         parsed_object_pool_clear(repo->parsed_objects);
248         FREE_AND_NULL(repo->parsed_objects);
249
250         if (repo->config) {
251                 git_configset_clear(repo->config);
252                 FREE_AND_NULL(repo->config);
253         }
254
255         if (repo->submodule_cache) {
256                 submodule_cache_free(repo->submodule_cache);
257                 repo->submodule_cache = NULL;
258         }
259
260         if (repo->index) {
261                 discard_index(repo->index);
262                 if (repo->index != &the_index)
263                         FREE_AND_NULL(repo->index);
264         }
265
266         if (repo->promisor_remote_config) {
267                 promisor_remote_clear(repo->promisor_remote_config);
268                 FREE_AND_NULL(repo->promisor_remote_config);
269         }
270 }
271
272 int repo_read_index(struct repository *repo)
273 {
274         int res;
275
276         if (!repo->index)
277                 CALLOC_ARRAY(repo->index, 1);
278
279         /* Complete the double-reference */
280         if (!repo->index->repo)
281                 repo->index->repo = repo;
282         else if (repo->index->repo != repo)
283                 BUG("repo's index should point back at itself");
284
285         res = read_index_from(repo->index, repo->index_file, repo->gitdir);
286
287         prepare_repo_settings(repo);
288         if (repo->settings.command_requires_full_index)
289                 ensure_full_index(repo->index);
290
291         return res;
292 }
293
294 int repo_hold_locked_index(struct repository *repo,
295                            struct lock_file *lf,
296                            int flags)
297 {
298         if (!repo->index_file)
299                 BUG("the repo hasn't been setup");
300         return hold_lock_file_for_update(lf, repo->index_file, flags);
301 }