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