]> git.ipfire.org Git - thirdparty/git.git/blame - repository.c
promisor-remote: support per-repository config
[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"
bf12fcdf 12#include "submodule-config.h"
3964fc2a 13#include "sparse-index.h"
ef7dc2e9 14#include "promisor-remote.h"
359efeff
BW
15
16/* The main repository */
b2f0ecee
NTND
17static struct repository the_repo;
18struct repository *the_repository;
f8adbec9 19struct index_state the_index;
b2f0ecee
NTND
20
21void initialize_the_repository(void)
22{
23 the_repository = &the_repo;
24
25 the_repo.index = &the_index;
90c62155 26 the_repo.objects = raw_object_store_new();
99bf115c
SB
27 the_repo.parsed_objects = parsed_object_pool_new();
28
b2f0ecee
NTND
29 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
30}
359efeff 31
357a03eb
NTND
32static 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)
359efeff
BW
44{
45 struct strbuf sb = STRBUF_INIT;
46
f9b7573f 47 free(repo->commondir);
357a03eb
NTND
48
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);
359efeff 56 repo->commondir = strbuf_detach(&sb, NULL);
359efeff
BW
57}
58
357a03eb
NTND
59void repo_set_gitdir(struct repository *repo,
60 const char *root,
61 const struct set_gitdir_args *o)
359efeff 62{
357a03eb
NTND
63 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 */
1fb2b636 69 char *old_gitdir = repo->gitdir;
359efeff 70
357a03eb 71 repo->gitdir = xstrdup(gitfile ? gitfile : root);
1fb2b636 72 free(old_gitdir);
357a03eb
NTND
73
74 repo_set_commondir(repo, o->commondir);
f0eaf638
JK
75
76 if (!repo->objects->odb) {
ca56dadb 77 CALLOC_ARRAY(repo->objects->odb, 1);
f0eaf638
JK
78 repo->objects->odb_tail = &repo->objects->odb->next;
79 }
80 expand_base_dir(&repo->objects->odb->path, o->object_dir,
357a03eb 81 repo->commondir, "objects");
f0eaf638 82
90c62155
SB
83 free(repo->objects->alternate_db);
84 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
357a03eb
NTND
85 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");
359efeff
BW
89}
90
78a67668 91void repo_set_hash_algo(struct repository *repo, int hash_algo)
92{
93 repo->hash_algo = &hash_algos[hash_algo];
94}
95
359efeff
BW
96/*
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;
357a03eb 106 struct set_gitdir_args args = { NULL };
359efeff
BW
107
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
357a03eb 121 repo_set_gitdir(repo, resolved_gitdir, &args);
359efeff
BW
122
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);
ee4512ed
JH
131
132 trace2_def_repo(repo);
359efeff
BW
133}
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 */
da62f786
SB
158int repo_init(struct repository *repo,
159 const char *gitdir,
160 const char *worktree)
359efeff 161{
e8805af1 162 struct repository_format format = REPOSITORY_FORMAT_INIT;
359efeff
BW
163 memset(repo, 0, sizeof(*repo));
164
90c62155 165 repo->objects = raw_object_store_new();
99bf115c 166 repo->parsed_objects = parsed_object_pool_new();
90c62155 167
359efeff
BW
168 if (repo_init_gitdir(repo, gitdir))
169 goto error;
170
171 if (read_and_verify_repository_format(&format, repo->commondir))
172 goto error;
173
78a67668 174 repo_set_hash_algo(repo, format.hash_algo);
175
ebaf3bcf
JT
176 /* take ownership of format.partial_clone */
177 repo->repository_format_partial_clone = format.partial_clone;
178 format.partial_clone = NULL;
179
359efeff
BW
180 if (worktree)
181 repo_set_worktree(repo, worktree);
182
e8805af1 183 clear_repository_format(&format);
359efeff
BW
184 return 0;
185
186error:
187 repo_clear(repo);
188 return -1;
189}
190
d5498e08 191int repo_submodule_init(struct repository *subrepo,
96dc883b 192 struct repository *superproject,
d5498e08 193 const struct submodule *sub)
96dc883b 194{
96dc883b
BW
195 struct strbuf gitdir = STRBUF_INIT;
196 struct strbuf worktree = STRBUF_INIT;
197 int ret = 0;
198
96dc883b
BW
199 if (!sub) {
200 ret = -1;
201 goto out;
202 }
203
d5498e08
SB
204 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
205 strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
96dc883b 206
d5498e08 207 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
96dc883b 208 /*
15beaaa3 209 * If initialization fails then it may be due to the submodule
96dc883b 210 * not being populated in the superproject's worktree. Instead
15beaaa3 211 * we can try to initialize the submodule by finding it's gitdir
96dc883b
BW
212 * in the superproject's 'modules' directory. In this case the
213 * submodule would not have a worktree.
214 */
215 strbuf_reset(&gitdir);
216 strbuf_repo_git_path(&gitdir, superproject,
217 "modules/%s", sub->name);
218
d5498e08 219 if (repo_init(subrepo, gitdir.buf, NULL)) {
96dc883b
BW
220 ret = -1;
221 goto out;
222 }
223 }
224
d5498e08
SB
225 subrepo->submodule_prefix = xstrfmt("%s%s/",
226 superproject->submodule_prefix ?
227 superproject->submodule_prefix :
228 "", sub->path);
96dc883b
BW
229
230out:
231 strbuf_release(&gitdir);
232 strbuf_release(&worktree);
233 return ret;
234}
235
359efeff
BW
236void repo_clear(struct repository *repo)
237{
90dd04aa
RS
238 FREE_AND_NULL(repo->gitdir);
239 FREE_AND_NULL(repo->commondir);
90dd04aa
RS
240 FREE_AND_NULL(repo->graft_file);
241 FREE_AND_NULL(repo->index_file);
242 FREE_AND_NULL(repo->worktree);
243 FREE_AND_NULL(repo->submodule_prefix);
3b256228 244
90c62155
SB
245 raw_object_store_clear(repo->objects);
246 FREE_AND_NULL(repo->objects);
247
99bf115c
SB
248 parsed_object_pool_clear(repo->parsed_objects);
249 FREE_AND_NULL(repo->parsed_objects);
250
3b256228
BW
251 if (repo->config) {
252 git_configset_clear(repo->config);
90dd04aa 253 FREE_AND_NULL(repo->config);
3b256228 254 }
639e30b5 255
bf12fcdf
BW
256 if (repo->submodule_cache) {
257 submodule_cache_free(repo->submodule_cache);
258 repo->submodule_cache = NULL;
259 }
260
639e30b5
BW
261 if (repo->index) {
262 discard_index(repo->index);
74373b5f
NTND
263 if (repo->index != &the_index)
264 FREE_AND_NULL(repo->index);
639e30b5 265 }
ef7dc2e9
JT
266
267 if (repo->promisor_remote_config) {
268 promisor_remote_clear(repo->promisor_remote_config);
269 FREE_AND_NULL(repo->promisor_remote_config);
270 }
639e30b5
BW
271}
272
273int repo_read_index(struct repository *repo)
274{
3964fc2a
DS
275 int res;
276
639e30b5 277 if (!repo->index)
ca56dadb 278 CALLOC_ARRAY(repo->index, 1);
639e30b5 279
1fd9ae51
DS
280 /* Complete the double-reference */
281 if (!repo->index->repo)
282 repo->index->repo = repo;
283 else if (repo->index->repo != repo)
284 BUG("repo's index should point back at itself");
285
3964fc2a
DS
286 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
287
288 prepare_repo_settings(repo);
289 if (repo->settings.command_requires_full_index)
290 ensure_full_index(repo->index);
291
292 return res;
359efeff 293}
3a95f31d
NTND
294
295int repo_hold_locked_index(struct repository *repo,
296 struct lock_file *lf,
297 int flags)
298{
299 if (!repo->index_file)
300 BUG("the repo hasn't been setup");
301 return hold_lock_file_for_update(lf, repo->index_file, flags);
302}