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