blob: c5b90ba93ea816c62eeaf73433e65e2730827063 [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 */
5#define USE_THE_INDEX_COMPATIBILITY_MACROS
Brandon Williams359efef2017-06-22 11:43:32 -07006#include "cache.h"
7#include "repository.h"
Stefan Beller90c62152018-03-23 18:20:55 +01008#include "object-store.h"
Brandon Williams3b256222017-06-22 11:43:42 -07009#include "config.h"
Stefan Beller99bf1152018-05-08 12:37:24 -070010#include "object.h"
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +070011#include "lockfile.h"
Brandon Williamsbf12fcd2017-06-22 11:43:44 -070012#include "submodule-config.h"
Derrick Stolee3964fc22021-03-30 13:10:47 +000013#include "sparse-index.h"
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070014#include "promisor-remote.h"
Brandon Williams359efef2017-06-22 11:43:32 -070015
16/* The main repository */
Nguyễn Thái Ngọc Duyb2f0ece2018-03-03 18:35:54 +070017static struct repository the_repo;
18struct repository *the_repository;
Nguyễn Thái Ngọc Duyf8adbec2019-01-24 15:29:12 +070019struct index_state the_index;
Nguyễn Thái Ngọc Duyb2f0ece2018-03-03 18:35:54 +070020
21void initialize_the_repository(void)
22{
23 the_repository = &the_repo;
24
25 the_repo.index = &the_index;
Stefan Beller90c62152018-03-23 18:20:55 +010026 the_repo.objects = raw_object_store_new();
Stefan Beller99bf1152018-05-08 12:37:24 -070027 the_repo.parsed_objects = parsed_object_pool_new();
28
Nguyễn Thái Ngọc Duyb2f0ece2018-03-03 18:35:54 +070029 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
30}
Brandon Williams359efef2017-06-22 11:43:32 -070031
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070032static void expand_base_dir(char **out, const char *in,
33 const char *base_dir, const char *def_in)
34{
35 free(*out);
36 if (in)
37 *out = xstrdup(in);
38 else
39 *out = xstrfmt("%s/%s", base_dir, def_in);
40}
41
42static void repo_set_commondir(struct repository *repo,
43 const char *commondir)
Brandon Williams359efef2017-06-22 11:43:32 -070044{
45 struct strbuf sb = STRBUF_INIT;
46
Jeff Kingf9b75732017-09-05 09:04:57 -040047 free(repo->commondir);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070048
49 if (commondir) {
50 repo->different_commondir = 1;
51 repo->commondir = xstrdup(commondir);
52 return;
53 }
54
55 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
Brandon Williams359efef2017-06-22 11:43:32 -070056 repo->commondir = strbuf_detach(&sb, NULL);
Brandon Williams359efef2017-06-22 11:43:32 -070057}
58
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070059void repo_set_gitdir(struct repository *repo,
60 const char *root,
61 const struct set_gitdir_args *o)
Brandon Williams359efef2017-06-22 11:43:32 -070062{
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070063 const char *gitfile = read_gitfile(root);
64 /*
65 * repo->gitdir is saved because the caller could pass "root"
66 * that also points to repo->gitdir. We want to keep it alive
67 * until after xstrdup(root). Then we can free it.
68 */
Jeff King1fb2b632017-09-05 09:05:01 -040069 char *old_gitdir = repo->gitdir;
Brandon Williams359efef2017-06-22 11:43:32 -070070
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070071 repo->gitdir = xstrdup(gitfile ? gitfile : root);
Jeff King1fb2b632017-09-05 09:05:01 -040072 free(old_gitdir);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070073
74 repo_set_commondir(repo, o->commondir);
Jeff Kingf0eaf632018-11-12 09:50:39 -050075
76 if (!repo->objects->odb) {
René Scharfeca56dad2021-03-13 17:17:22 +010077 CALLOC_ARRAY(repo->objects->odb, 1);
Jeff Kingf0eaf632018-11-12 09:50:39 -050078 repo->objects->odb_tail = &repo->objects->odb->next;
79 }
80 expand_base_dir(&repo->objects->odb->path, o->object_dir,
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070081 repo->commondir, "objects");
Jeff Kingf0eaf632018-11-12 09:50:39 -050082
Stefan Beller90c62152018-03-23 18:20:55 +010083 free(repo->objects->alternate_db);
84 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +070085 expand_base_dir(&repo->graft_file, o->graft_file,
86 repo->commondir, "info/grafts");
87 expand_base_dir(&repo->index_file, o->index_file,
88 repo->gitdir, "index");
Brandon Williams359efef2017-06-22 11:43:32 -070089}
90
brian m. carlson78a67662017-11-12 21:28:53 +000091void repo_set_hash_algo(struct repository *repo, int hash_algo)
92{
93 repo->hash_algo = &hash_algos[hash_algo];
94}
95
Brandon Williams359efef2017-06-22 11:43:32 -070096/*
97 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
98 * Return 0 upon success and a non-zero value upon failure.
99 */
100static int repo_init_gitdir(struct repository *repo, const char *gitdir)
101{
102 int ret = 0;
103 int error = 0;
104 char *abspath = NULL;
105 const char *resolved_gitdir;
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700106 struct set_gitdir_args args = { NULL };
Brandon Williams359efef2017-06-22 11:43:32 -0700107
108 abspath = real_pathdup(gitdir, 0);
109 if (!abspath) {
110 ret = -1;
111 goto out;
112 }
113
114 /* 'gitdir' must reference the gitdir directly */
115 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
116 if (!resolved_gitdir) {
117 ret = -1;
118 goto out;
119 }
120
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700121 repo_set_gitdir(repo, resolved_gitdir, &args);
Brandon Williams359efef2017-06-22 11:43:32 -0700122
123out:
124 free(abspath);
125 return ret;
126}
127
128void repo_set_worktree(struct repository *repo, const char *path)
129{
130 repo->worktree = real_pathdup(path, 1);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800131
132 trace2_def_repo(repo);
Brandon Williams359efef2017-06-22 11:43:32 -0700133}
134
135static int read_and_verify_repository_format(struct repository_format *format,
136 const char *commondir)
137{
138 int ret = 0;
139 struct strbuf sb = STRBUF_INIT;
140
141 strbuf_addf(&sb, "%s/config", commondir);
142 read_repository_format(format, sb.buf);
143 strbuf_reset(&sb);
144
145 if (verify_repository_format(format, &sb) < 0) {
146 warning("%s", sb.buf);
147 ret = -1;
148 }
149
150 strbuf_release(&sb);
151 return ret;
152}
153
154/*
155 * Initialize 'repo' based on the provided 'gitdir'.
156 * Return 0 upon success and a non-zero value upon failure.
157 */
Stefan Bellerda62f782018-03-28 15:35:31 -0700158int repo_init(struct repository *repo,
159 const char *gitdir,
160 const char *worktree)
Brandon Williams359efef2017-06-22 11:43:32 -0700161{
Martin Ågrene8805af2019-02-28 21:36:28 +0100162 struct repository_format format = REPOSITORY_FORMAT_INIT;
Brandon Williams359efef2017-06-22 11:43:32 -0700163 memset(repo, 0, sizeof(*repo));
164
Stefan Beller90c62152018-03-23 18:20:55 +0100165 repo->objects = raw_object_store_new();
Stefan Beller99bf1152018-05-08 12:37:24 -0700166 repo->parsed_objects = parsed_object_pool_new();
Stefan Beller90c62152018-03-23 18:20:55 +0100167
Brandon Williams359efef2017-06-22 11:43:32 -0700168 if (repo_init_gitdir(repo, gitdir))
169 goto error;
170
171 if (read_and_verify_repository_format(&format, repo->commondir))
172 goto error;
173
brian m. carlson78a67662017-11-12 21:28:53 +0000174 repo_set_hash_algo(repo, format.hash_algo);
175
Jonathan Tanebaf3bc2021-06-17 10:13:22 -0700176 /* take ownership of format.partial_clone */
177 repo->repository_format_partial_clone = format.partial_clone;
178 format.partial_clone = NULL;
179
Brandon Williams359efef2017-06-22 11:43:32 -0700180 if (worktree)
181 repo_set_worktree(repo, worktree);
182
Martin Ågrene8805af2019-02-28 21:36:28 +0100183 clear_repository_format(&format);
Brandon Williams359efef2017-06-22 11:43:32 -0700184 return 0;
185
186error:
187 repo_clear(repo);
188 return -1;
189}
190
Stefan Bellerd5498e02018-11-28 16:27:53 -0800191int repo_submodule_init(struct repository *subrepo,
Brandon Williams96dc8832017-06-22 11:43:47 -0700192 struct repository *superproject,
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700193 const char *path,
194 const struct object_id *treeish_name)
Brandon Williams96dc8832017-06-22 11:43:47 -0700195{
Brandon Williams96dc8832017-06-22 11:43:47 -0700196 struct strbuf gitdir = STRBUF_INIT;
197 struct strbuf worktree = STRBUF_INIT;
198 int ret = 0;
199
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700200 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
201 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
Brandon Williams96dc8832017-06-22 11:43:47 -0700202
Stefan Bellerd5498e02018-11-28 16:27:53 -0800203 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
Brandon Williams96dc8832017-06-22 11:43:47 -0700204 /*
Elijah Newren15beaaa2019-11-05 17:07:23 +0000205 * If initialization fails then it may be due to the submodule
Brandon Williams96dc8832017-06-22 11:43:47 -0700206 * not being populated in the superproject's worktree. Instead
Elijah Newren15beaaa2019-11-05 17:07:23 +0000207 * we can try to initialize the submodule by finding it's gitdir
Brandon Williams96dc8832017-06-22 11:43:47 -0700208 * in the superproject's 'modules' directory. In this case the
209 * submodule would not have a worktree.
210 */
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700211 const struct submodule *sub =
212 submodule_from_path(superproject, treeish_name, path);
213 if (!sub) {
214 ret = -1;
215 goto out;
216 }
217
Brandon Williams96dc8832017-06-22 11:43:47 -0700218 strbuf_reset(&gitdir);
Jonathan Tance125d42021-09-15 11:59:19 -0700219 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
Brandon Williams96dc8832017-06-22 11:43:47 -0700220
Stefan Bellerd5498e02018-11-28 16:27:53 -0800221 if (repo_init(subrepo, gitdir.buf, NULL)) {
Brandon Williams96dc8832017-06-22 11:43:47 -0700222 ret = -1;
223 goto out;
224 }
225 }
226
Stefan Bellerd5498e02018-11-28 16:27:53 -0800227 subrepo->submodule_prefix = xstrfmt("%s%s/",
228 superproject->submodule_prefix ?
229 superproject->submodule_prefix :
Jonathan Tan8eb8dcf2021-09-09 11:47:28 -0700230 "", path);
Brandon Williams96dc8832017-06-22 11:43:47 -0700231
232out:
233 strbuf_release(&gitdir);
234 strbuf_release(&worktree);
235 return ret;
236}
237
Brandon Williams359efef2017-06-22 11:43:32 -0700238void repo_clear(struct repository *repo)
239{
René Scharfe90dd04a2017-10-01 16:44:46 +0200240 FREE_AND_NULL(repo->gitdir);
241 FREE_AND_NULL(repo->commondir);
René Scharfe90dd04a2017-10-01 16:44:46 +0200242 FREE_AND_NULL(repo->graft_file);
243 FREE_AND_NULL(repo->index_file);
244 FREE_AND_NULL(repo->worktree);
245 FREE_AND_NULL(repo->submodule_prefix);
Brandon Williams3b256222017-06-22 11:43:42 -0700246
Stefan Beller90c62152018-03-23 18:20:55 +0100247 raw_object_store_clear(repo->objects);
248 FREE_AND_NULL(repo->objects);
249
Stefan Beller99bf1152018-05-08 12:37:24 -0700250 parsed_object_pool_clear(repo->parsed_objects);
251 FREE_AND_NULL(repo->parsed_objects);
252
Brandon Williams3b256222017-06-22 11:43:42 -0700253 if (repo->config) {
254 git_configset_clear(repo->config);
René Scharfe90dd04a2017-10-01 16:44:46 +0200255 FREE_AND_NULL(repo->config);
Brandon Williams3b256222017-06-22 11:43:42 -0700256 }
Brandon Williams639e30b2017-06-22 11:43:43 -0700257
Brandon Williamsbf12fcd2017-06-22 11:43:44 -0700258 if (repo->submodule_cache) {
259 submodule_cache_free(repo->submodule_cache);
260 repo->submodule_cache = NULL;
261 }
262
Brandon Williams639e30b2017-06-22 11:43:43 -0700263 if (repo->index) {
264 discard_index(repo->index);
Nguyễn Thái Ngọc Duy74373b52018-05-10 08:13:10 +0200265 if (repo->index != &the_index)
266 FREE_AND_NULL(repo->index);
Brandon Williams639e30b2017-06-22 11:43:43 -0700267 }
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700268
269 if (repo->promisor_remote_config) {
270 promisor_remote_clear(repo->promisor_remote_config);
271 FREE_AND_NULL(repo->promisor_remote_config);
272 }
Brandon Williams639e30b2017-06-22 11:43:43 -0700273}
274
275int repo_read_index(struct repository *repo)
276{
Derrick Stolee3964fc22021-03-30 13:10:47 +0000277 int res;
278
Brandon Williams639e30b2017-06-22 11:43:43 -0700279 if (!repo->index)
René Scharfeca56dad2021-03-13 17:17:22 +0100280 CALLOC_ARRAY(repo->index, 1);
Brandon Williams639e30b2017-06-22 11:43:43 -0700281
Derrick Stolee1fd9ae52021-01-23 19:58:15 +0000282 /* Complete the double-reference */
283 if (!repo->index->repo)
284 repo->index->repo = repo;
285 else if (repo->index->repo != repo)
286 BUG("repo's index should point back at itself");
287
Derrick Stolee3964fc22021-03-30 13:10:47 +0000288 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
289
290 prepare_repo_settings(repo);
291 if (repo->settings.command_requires_full_index)
292 ensure_full_index(repo->index);
293
294 return res;
Brandon Williams359efef2017-06-22 11:43:32 -0700295}
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +0700296
297int repo_hold_locked_index(struct repository *repo,
298 struct lock_file *lf,
299 int flags)
300{
301 if (!repo->index_file)
302 BUG("the repo hasn't been setup");
303 return hold_lock_file_for_update(lf, repo->index_file, flags);
304}