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