sha1_file: add repository argument to sha1_file_name
[git] / repository.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "object-store.h"
4 #include "config.h"
5 #include "submodule-config.h"
6
7 /* The main repository */
8 static struct repository the_repo;
9 struct repository *the_repository;
10
11 void initialize_the_repository(void)
12 {
13         the_repository = &the_repo;
14
15         the_repo.index = &the_index;
16         the_repo.objects = raw_object_store_new();
17         repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
18 }
19
20 static void expand_base_dir(char **out, const char *in,
21                             const char *base_dir, const char *def_in)
22 {
23         free(*out);
24         if (in)
25                 *out = xstrdup(in);
26         else
27                 *out = xstrfmt("%s/%s", base_dir, def_in);
28 }
29
30 static void repo_set_commondir(struct repository *repo,
31                                const char *commondir)
32 {
33         struct strbuf sb = STRBUF_INIT;
34
35         free(repo->commondir);
36
37         if (commondir) {
38                 repo->different_commondir = 1;
39                 repo->commondir = xstrdup(commondir);
40                 return;
41         }
42
43         repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
44         repo->commondir = strbuf_detach(&sb, NULL);
45 }
46
47 void repo_set_gitdir(struct repository *repo,
48                      const char *root,
49                      const struct set_gitdir_args *o)
50 {
51         const char *gitfile = read_gitfile(root);
52         /*
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.
56          */
57         char *old_gitdir = repo->gitdir;
58
59         repo->gitdir = xstrdup(gitfile ? gitfile : root);
60         free(old_gitdir);
61
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");
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         struct set_gitdir_args args = { NULL };
89
90         abspath = real_pathdup(gitdir, 0);
91         if (!abspath) {
92                 ret = -1;
93                 goto out;
94         }
95
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;
101         }
102
103         repo_set_gitdir(repo, resolved_gitdir, &args);
104
105 out:
106         free(abspath);
107         return ret;
108 }
109
110 void repo_set_worktree(struct repository *repo, const char *path)
111 {
112         repo->worktree = real_pathdup(path, 1);
113 }
114
115 static int read_and_verify_repository_format(struct repository_format *format,
116                                              const char *commondir)
117 {
118         int ret = 0;
119         struct strbuf sb = STRBUF_INIT;
120
121         strbuf_addf(&sb, "%s/config", commondir);
122         read_repository_format(format, sb.buf);
123         strbuf_reset(&sb);
124
125         if (verify_repository_format(format, &sb) < 0) {
126                 warning("%s", sb.buf);
127                 ret = -1;
128         }
129
130         strbuf_release(&sb);
131         return ret;
132 }
133
134 /*
135  * Initialize 'repo' based on the provided 'gitdir'.
136  * Return 0 upon success and a non-zero value upon failure.
137  */
138 static int repo_init(struct repository *repo,
139                      const char *gitdir,
140                      const char *worktree)
141 {
142         struct repository_format format;
143         memset(repo, 0, sizeof(*repo));
144
145         repo->objects = raw_object_store_new();
146
147         if (repo_init_gitdir(repo, gitdir))
148                 goto error;
149
150         if (read_and_verify_repository_format(&format, repo->commondir))
151                 goto error;
152
153         repo_set_hash_algo(repo, format.hash_algo);
154
155         if (worktree)
156                 repo_set_worktree(repo, worktree);
157
158         return 0;
159
160 error:
161         repo_clear(repo);
162         return -1;
163 }
164
165 /*
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.
169  */
170 int repo_submodule_init(struct repository *submodule,
171                         struct repository *superproject,
172                         const char *path)
173 {
174         const struct submodule *sub;
175         struct strbuf gitdir = STRBUF_INIT;
176         struct strbuf worktree = STRBUF_INIT;
177         int ret = 0;
178
179         sub = submodule_from_cache(superproject, &null_oid, path);
180         if (!sub) {
181                 ret = -1;
182                 goto out;
183         }
184
185         strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
186         strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
187
188         if (repo_init(submodule, gitdir.buf, worktree.buf)) {
189                 /*
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.
195                  */
196                 strbuf_reset(&gitdir);
197                 strbuf_repo_git_path(&gitdir, superproject,
198                                      "modules/%s", sub->name);
199
200                 if (repo_init(submodule, gitdir.buf, NULL)) {
201                         ret = -1;
202                         goto out;
203                 }
204         }
205
206         submodule->submodule_prefix = xstrfmt("%s%s/",
207                                               superproject->submodule_prefix ?
208                                               superproject->submodule_prefix :
209                                               "", path);
210
211 out:
212         strbuf_release(&gitdir);
213         strbuf_release(&worktree);
214         return ret;
215 }
216
217 void repo_clear(struct repository *repo)
218 {
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);
225
226         raw_object_store_clear(repo->objects);
227         FREE_AND_NULL(repo->objects);
228
229         if (repo->config) {
230                 git_configset_clear(repo->config);
231                 FREE_AND_NULL(repo->config);
232         }
233
234         if (repo->submodule_cache) {
235                 submodule_cache_free(repo->submodule_cache);
236                 repo->submodule_cache = NULL;
237         }
238
239         if (repo->index) {
240                 discard_index(repo->index);
241                 FREE_AND_NULL(repo->index);
242         }
243 }
244
245 int repo_read_index(struct repository *repo)
246 {
247         if (!repo->index)
248                 repo->index = xcalloc(1, sizeof(*repo->index));
249
250         return read_index_from(repo->index, repo->index_file, repo->gitdir);
251 }