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