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