blob: da42ff550e4a9768f5f362b58ae0bd2c6e30a501 [file] [log] [blame]
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +07001/*
2 * not really _using_ the compat macros, just make sure the_index
3 * declaration matches the definition in this file.
4 */
Ævar Arnfjörð Bjarmason666f53e2022-11-19 14:07:36 +01005#define USE_THE_INDEX_VARIABLE
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00006#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00007#include "abspath.h"
Brandon Williams359efef2017-06-22 11:43:32 -07008#include "repository.h"
Elijah Newrena034e912023-05-16 06:34:06 +00009#include "object-store-ll.h"
Brandon Williams3b256222017-06-22 11:43:42 -070010#include "config.h"
Stefan Beller99bf1152018-05-08 12:37:24 -070011#include "object.h"
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +070012#include "lockfile.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000013#include "path.h"
Elijah Newren08c46a42023-05-16 06:33:56 +000014#include "read-cache-ll.h"
Glen Choofd3cb052021-11-17 16:53:22 -080015#include "remote.h"
Elijah Newrene38da482023-03-21 06:26:05 +000016#include "setup.h"
brian m. carlson23b2c7e2023-10-01 21:40:09 -050017#include "loose.h"
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070018#include "submodule-config.h"
Derrick Stolee3964fc22021-03-30 13:10:47 +000019#include "sparse-index.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000020#include "trace2.h"
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070021#include "promisor-remote.h"
Brandon Williams359efef2017-06-22 11:43:32 -070022
23/* The main repository */
Nguyễn Thái Ngọc Duyb2f0ece2018-03-03 18:35:54 +070024static struct repository the_repo;
25struct repository *the_repository;
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +070026struct index_state the_index;
Nguyễn Thái Ngọc Duyb2f0ece2018-03-03 18:35:54 +070027
28void initialize_the_repository(void)
29{
30 the_repository = &the_repo;
31
32 the_repo.index = &the_index;
Stefan Beller90c62152018-03-23 18:20:55 +010033 the_repo.objects = raw_object_store_new();
Glen Choofd3cb052021-11-17 16:53:22 -080034 the_repo.remote_state = remote_state_new();
Stefan Beller99bf1152018-05-08 12:37:24 -070035 the_repo.parsed_objects = parsed_object_pool_new();
36
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +010037 index_state_init(&the_index, the_repository);
38
Nguyễn Thái Ngọc Duyb2f0ece2018-03-03 18:35:54 +070039 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
40}
Brandon Williams359efef2017-06-22 11:43:32 -070041
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070042static void expand_base_dir(char **out, const char *in,
43 const char *base_dir, const char *def_in)
44{
45 free(*out);
46 if (in)
47 *out = xstrdup(in);
48 else
49 *out = xstrfmt("%s/%s", base_dir, def_in);
50}
51
52static void repo_set_commondir(struct repository *repo,
53 const char *commondir)
Brandon Williams359efef2017-06-22 11:43:32 -070054{
55 struct strbuf sb = STRBUF_INIT;
56
Jeff Kingf9b75732017-09-05 09:04:57 -040057 free(repo->commondir);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070058
59 if (commondir) {
60 repo->different_commondir = 1;
61 repo->commondir = xstrdup(commondir);
62 return;
63 }
64
65 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
Brandon Williams359efef2017-06-22 11:43:32 -070066 repo->commondir = strbuf_detach(&sb, NULL);
Brandon Williams359efef2017-06-22 11:43:32 -070067}
68
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070069void repo_set_gitdir(struct repository *repo,
70 const char *root,
71 const struct set_gitdir_args *o)
Brandon Williams359efef2017-06-22 11:43:32 -070072{
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070073 const char *gitfile = read_gitfile(root);
74 /*
75 * repo->gitdir is saved because the caller could pass "root"
76 * that also points to repo->gitdir. We want to keep it alive
77 * until after xstrdup(root). Then we can free it.
78 */
Jeff King1fb2b632017-09-05 09:05:01 -040079 char *old_gitdir = repo->gitdir;
Brandon Williams359efef2017-06-22 11:43:32 -070080
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070081 repo->gitdir = xstrdup(gitfile ? gitfile : root);
Jeff King1fb2b632017-09-05 09:05:01 -040082 free(old_gitdir);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070083
84 repo_set_commondir(repo, o->commondir);
Jeff Kingf0eaf632018-11-12 09:50:39 -050085
86 if (!repo->objects->odb) {
René Scharfeca56dad2021-03-13 17:17:22 +010087 CALLOC_ARRAY(repo->objects->odb, 1);
Jeff Kingf0eaf632018-11-12 09:50:39 -050088 repo->objects->odb_tail = &repo->objects->odb->next;
89 }
90 expand_base_dir(&repo->objects->odb->path, o->object_dir,
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070091 repo->commondir, "objects");
Jeff Kingf0eaf632018-11-12 09:50:39 -050092
Neeraj Singhecd81df2021-12-06 22:05:05 +000093 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
94
Stefan Beller90c62152018-03-23 18:20:55 +010095 free(repo->objects->alternate_db);
96 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070097 expand_base_dir(&repo->graft_file, o->graft_file,
98 repo->commondir, "info/grafts");
99 expand_base_dir(&repo->index_file, o->index_file,
100 repo->gitdir, "index");
Brandon Williams359efef2017-06-22 11:43:32 -0700101}
102
brian m. carlson78a67662017-11-12 21:28:53 +0000103void repo_set_hash_algo(struct repository *repo, int hash_algo)
104{
105 repo->hash_algo = &hash_algos[hash_algo];
106}
107
Eric W. Biederman15a1ca12023-10-01 21:40:08 -0500108void repo_set_compat_hash_algo(struct repository *repo, int algo)
109{
110 if (hash_algo_by_ptr(repo->hash_algo) == algo)
111 BUG("hash_algo and compat_hash_algo match");
112 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
brian m. carlson23b2c7e2023-10-01 21:40:09 -0500113 if (repo->compat_hash_algo)
114 repo_read_loose_object_map(repo);
Eric W. Biederman15a1ca12023-10-01 21:40:08 -0500115}
116
Patrick Steinhardt173761e2023-12-29 08:26:39 +0100117void repo_set_ref_storage_format(struct repository *repo, unsigned int format)
118{
119 repo->ref_storage_format = format;
120}
121
Brandon Williams359efef2017-06-22 11:43:32 -0700122/*
123 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
124 * Return 0 upon success and a non-zero value upon failure.
125 */
126static int repo_init_gitdir(struct repository *repo, const char *gitdir)
127{
128 int ret = 0;
129 int error = 0;
130 char *abspath = NULL;
131 const char *resolved_gitdir;
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700132 struct set_gitdir_args args = { NULL };
Brandon Williams359efef2017-06-22 11:43:32 -0700133
134 abspath = real_pathdup(gitdir, 0);
135 if (!abspath) {
136 ret = -1;
137 goto out;
138 }
139
140 /* 'gitdir' must reference the gitdir directly */
141 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
142 if (!resolved_gitdir) {
143 ret = -1;
144 goto out;
145 }
146
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700147 repo_set_gitdir(repo, resolved_gitdir, &args);
Brandon Williams359efef2017-06-22 11:43:32 -0700148
149out:
150 free(abspath);
151 return ret;
152}
153
154void repo_set_worktree(struct repository *repo, const char *path)
155{
156 repo->worktree = real_pathdup(path, 1);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800157
158 trace2_def_repo(repo);
Brandon Williams359efef2017-06-22 11:43:32 -0700159}
160
161static int read_and_verify_repository_format(struct repository_format *format,
162 const char *commondir)
163{
164 int ret = 0;
165 struct strbuf sb = STRBUF_INIT;
166
167 strbuf_addf(&sb, "%s/config", commondir);
168 read_repository_format(format, sb.buf);
169 strbuf_reset(&sb);
170
171 if (verify_repository_format(format, &sb) < 0) {
172 warning("%s", sb.buf);
173 ret = -1;
174 }
175
176 strbuf_release(&sb);
177 return ret;
178}
179
180/*
181 * Initialize 'repo' based on the provided 'gitdir'.
182 * Return 0 upon success and a non-zero value upon failure.
183 */
Stefan Bellerda62f782018-03-28 15:35:31 -0700184int repo_init(struct repository *repo,
185 const char *gitdir,
186 const char *worktree)
Brandon Williams359efef2017-06-22 11:43:32 -0700187{
Martin Ågrene8805af2019-02-28 21:36:28 +0100188 struct repository_format format = REPOSITORY_FORMAT_INIT;
Brandon Williams359efef2017-06-22 11:43:32 -0700189 memset(repo, 0, sizeof(*repo));
190
Stefan Beller90c62152018-03-23 18:20:55 +0100191 repo->objects = raw_object_store_new();
Stefan Beller99bf1152018-05-08 12:37:24 -0700192 repo->parsed_objects = parsed_object_pool_new();
Glen Choofd3cb052021-11-17 16:53:22 -0800193 repo->remote_state = remote_state_new();
Stefan Beller90c62152018-03-23 18:20:55 +0100194
Brandon Williams359efef2017-06-22 11:43:32 -0700195 if (repo_init_gitdir(repo, gitdir))
196 goto error;
197
198 if (read_and_verify_repository_format(&format, repo->commondir))
199 goto error;
200
brian m. carlson78a67662017-11-12 21:28:53 +0000201 repo_set_hash_algo(repo, format.hash_algo);
brian m. carlson9ae702f2023-10-01 21:40:25 -0500202 repo_set_compat_hash_algo(repo, format.compat_hash_algo);
Patrick Steinhardt173761e2023-12-29 08:26:39 +0100203 repo_set_ref_storage_format(repo, format.ref_storage_format);
Victoria Dye3867f6d2023-05-26 01:33:00 +0000204 repo->repository_format_worktree_config = format.worktree_config;
brian m. carlson78a67662017-11-12 21:28:53 +0000205
Jonathan Tanebaf3bc2021-06-17 10:13:22 -0700206 /* take ownership of format.partial_clone */
207 repo->repository_format_partial_clone = format.partial_clone;
208 format.partial_clone = NULL;
209
Brandon Williams359efef2017-06-22 11:43:32 -0700210 if (worktree)
211 repo_set_worktree(repo, worktree);
212
brian m. carlson23b2c7e2023-10-01 21:40:09 -0500213 if (repo->compat_hash_algo)
214 repo_read_loose_object_map(repo);
215
Martin Ågrene8805af2019-02-28 21:36:28 +0100216 clear_repository_format(&format);
Brandon Williams359efef2017-06-22 11:43:32 -0700217 return 0;
218
219error:
220 repo_clear(repo);
221 return -1;
222}
223
Stefan Bellerd5498e02018-11-28 16:27:53 -0800224int repo_submodule_init(struct repository *subrepo,
Brandon Williams96dc8832017-06-22 11:43:47 -0700225 struct repository *superproject,
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700226 const char *path,
227 const struct object_id *treeish_name)
Brandon Williams96dc8832017-06-22 11:43:47 -0700228{
Brandon Williams96dc8832017-06-22 11:43:47 -0700229 struct strbuf gitdir = STRBUF_INIT;
230 struct strbuf worktree = STRBUF_INIT;
231 int ret = 0;
232
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700233 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
234 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
Brandon Williams96dc8832017-06-22 11:43:47 -0700235
Stefan Bellerd5498e02018-11-28 16:27:53 -0800236 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
Brandon Williams96dc8832017-06-22 11:43:47 -0700237 /*
Elijah Newren15beaaa2019-11-05 17:07:23 +0000238 * If initialization fails then it may be due to the submodule
Brandon Williams96dc8832017-06-22 11:43:47 -0700239 * not being populated in the superproject's worktree. Instead
Elijah Newren15beaaa2019-11-05 17:07:23 +0000240 * we can try to initialize the submodule by finding it's gitdir
Brandon Williams96dc8832017-06-22 11:43:47 -0700241 * in the superproject's 'modules' directory. In this case the
242 * submodule would not have a worktree.
243 */
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700244 const struct submodule *sub =
245 submodule_from_path(superproject, treeish_name, path);
246 if (!sub) {
247 ret = -1;
248 goto out;
249 }
250
Brandon Williams96dc8832017-06-22 11:43:47 -0700251 strbuf_reset(&gitdir);
Jonathan Tance125d42021-09-15 11:59:19 -0700252 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
Brandon Williams96dc8832017-06-22 11:43:47 -0700253
Stefan Bellerd5498e02018-11-28 16:27:53 -0800254 if (repo_init(subrepo, gitdir.buf, NULL)) {
Brandon Williams96dc8832017-06-22 11:43:47 -0700255 ret = -1;
256 goto out;
257 }
258 }
259
Stefan Bellerd5498e02018-11-28 16:27:53 -0800260 subrepo->submodule_prefix = xstrfmt("%s%s/",
261 superproject->submodule_prefix ?
262 superproject->submodule_prefix :
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700263 "", path);
Brandon Williams96dc8832017-06-22 11:43:47 -0700264
265out:
266 strbuf_release(&gitdir);
267 strbuf_release(&worktree);
268 return ret;
269}
270
Ævar Arnfjörð Bjarmason759f3402022-03-04 19:32:17 +0100271static void repo_clear_path_cache(struct repo_path_cache *cache)
272{
273 FREE_AND_NULL(cache->squash_msg);
274 FREE_AND_NULL(cache->squash_msg);
275 FREE_AND_NULL(cache->merge_msg);
276 FREE_AND_NULL(cache->merge_rr);
277 FREE_AND_NULL(cache->merge_mode);
278 FREE_AND_NULL(cache->merge_head);
Ævar Arnfjörð Bjarmason759f3402022-03-04 19:32:17 +0100279 FREE_AND_NULL(cache->fetch_head);
280 FREE_AND_NULL(cache->shallow);
281}
282
Brandon Williams359efef2017-06-22 11:43:32 -0700283void repo_clear(struct repository *repo)
284{
René Scharfe90dd04a2017-10-01 16:44:46 +0200285 FREE_AND_NULL(repo->gitdir);
286 FREE_AND_NULL(repo->commondir);
René Scharfe90dd04a2017-10-01 16:44:46 +0200287 FREE_AND_NULL(repo->graft_file);
288 FREE_AND_NULL(repo->index_file);
289 FREE_AND_NULL(repo->worktree);
290 FREE_AND_NULL(repo->submodule_prefix);
Brandon Williams3b256222017-06-22 11:43:42 -0700291
Stefan Beller90c62152018-03-23 18:20:55 +0100292 raw_object_store_clear(repo->objects);
293 FREE_AND_NULL(repo->objects);
294
Stefan Beller99bf1152018-05-08 12:37:24 -0700295 parsed_object_pool_clear(repo->parsed_objects);
296 FREE_AND_NULL(repo->parsed_objects);
297
Johannes Schindelin6741e912024-04-12 10:49:58 +0200298 FREE_AND_NULL(repo->settings.fsmonitor);
299
Brandon Williams3b256222017-06-22 11:43:42 -0700300 if (repo->config) {
301 git_configset_clear(repo->config);
René Scharfe90dd04a2017-10-01 16:44:46 +0200302 FREE_AND_NULL(repo->config);
Brandon Williams3b256222017-06-22 11:43:42 -0700303 }
Brandon Williams639e30b2017-06-22 11:43:43 -0700304
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700305 if (repo->submodule_cache) {
306 submodule_cache_free(repo->submodule_cache);
307 repo->submodule_cache = NULL;
308 }
309
Brandon Williams639e30b2017-06-22 11:43:43 -0700310 if (repo->index) {
311 discard_index(repo->index);
Nguyễn Thái Ngọc Duy74373b52018-05-10 08:13:10 +0200312 if (repo->index != &the_index)
313 FREE_AND_NULL(repo->index);
Brandon Williams639e30b2017-06-22 11:43:43 -0700314 }
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700315
316 if (repo->promisor_remote_config) {
317 promisor_remote_clear(repo->promisor_remote_config);
318 FREE_AND_NULL(repo->promisor_remote_config);
319 }
Glen Choofd3cb052021-11-17 16:53:22 -0800320
321 if (repo->remote_state) {
322 remote_state_clear(repo->remote_state);
323 FREE_AND_NULL(repo->remote_state);
324 }
Ævar Arnfjörð Bjarmason759f3402022-03-04 19:32:17 +0100325
326 repo_clear_path_cache(&repo->cached_paths);
Brandon Williams639e30b2017-06-22 11:43:43 -0700327}
328
329int repo_read_index(struct repository *repo)
330{
Derrick Stolee3964fc22021-03-30 13:10:47 +0000331 int res;
332
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +0100333 /* Complete the double-reference */
Ævar Arnfjörð Bjarmason2f6b1eb2023-01-12 13:55:27 +0100334 if (!repo->index) {
335 ALLOC_ARRAY(repo->index, 1);
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +0100336 index_state_init(repo->index, repo);
337 } else if (repo->index->repo != repo) {
Derrick Stolee1fd9ae52021-01-23 19:58:15 +0000338 BUG("repo's index should point back at itself");
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +0100339 }
Derrick Stolee1fd9ae52021-01-23 19:58:15 +0000340
Derrick Stolee3964fc22021-03-30 13:10:47 +0000341 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
342
343 prepare_repo_settings(repo);
344 if (repo->settings.command_requires_full_index)
345 ensure_full_index(repo->index);
346
Elijah Newrenaf6a5182022-01-14 15:59:41 +0000347 /*
348 * If sparse checkouts are in use, check whether paths with the
349 * SKIP_WORKTREE attribute are missing from the worktree; if not,
350 * clear that attribute for that path.
351 */
352 clear_skip_worktree_from_present_files(repo->index);
353
Derrick Stolee3964fc22021-03-30 13:10:47 +0000354 return res;
Brandon Williams359efef2017-06-22 11:43:32 -0700355}
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +0700356
357int repo_hold_locked_index(struct repository *repo,
358 struct lock_file *lf,
359 int flags)
360{
361 if (!repo->index_file)
362 BUG("the repo hasn't been setup");
363 return hold_lock_file_for_update(lf, repo->index_file, flags);
364}