]> git.ipfire.org Git - thirdparty/git.git/blame - repository.c
tmp-objdir: disable ref updates when replacing the primary odb
[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
ecd81dfc
NS
83 repo->objects->odb->disable_ref_updates = o->disable_ref_updates;
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();
90c62155 169
359efeff
BW
170 if (repo_init_gitdir(repo, gitdir))
171 goto error;
172
173 if (read_and_verify_repository_format(&format, repo->commondir))
174 goto error;
175
78a67668 176 repo_set_hash_algo(repo, format.hash_algo);
177
ebaf3bcf
JT
178 /* take ownership of format.partial_clone */
179 repo->repository_format_partial_clone = format.partial_clone;
180 format.partial_clone = NULL;
181
359efeff
BW
182 if (worktree)
183 repo_set_worktree(repo, worktree);
184
e8805af1 185 clear_repository_format(&format);
359efeff
BW
186 return 0;
187
188error:
189 repo_clear(repo);
190 return -1;
191}
192
d5498e08 193int repo_submodule_init(struct repository *subrepo,
96dc883b 194 struct repository *superproject,
d5498e08 195 const struct submodule *sub)
96dc883b 196{
96dc883b
BW
197 struct strbuf gitdir = STRBUF_INIT;
198 struct strbuf worktree = STRBUF_INIT;
199 int ret = 0;
200
96dc883b
BW
201 if (!sub) {
202 ret = -1;
203 goto out;
204 }
205
d5498e08
SB
206 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
207 strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
96dc883b 208
d5498e08 209 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
96dc883b 210 /*
15beaaa3 211 * If initialization fails then it may be due to the submodule
96dc883b 212 * not being populated in the superproject's worktree. Instead
15beaaa3 213 * we can try to initialize the submodule by finding it's gitdir
96dc883b
BW
214 * in the superproject's 'modules' directory. In this case the
215 * submodule would not have a worktree.
216 */
217 strbuf_reset(&gitdir);
ce125d43 218 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
96dc883b 219
d5498e08 220 if (repo_init(subrepo, gitdir.buf, NULL)) {
96dc883b
BW
221 ret = -1;
222 goto out;
223 }
224 }
225
d5498e08
SB
226 subrepo->submodule_prefix = xstrfmt("%s%s/",
227 superproject->submodule_prefix ?
228 superproject->submodule_prefix :
229 "", sub->path);
96dc883b
BW
230
231out:
232 strbuf_release(&gitdir);
233 strbuf_release(&worktree);
234 return ret;
235}
236
359efeff
BW
237void repo_clear(struct repository *repo)
238{
90dd04aa
RS
239 FREE_AND_NULL(repo->gitdir);
240 FREE_AND_NULL(repo->commondir);
90dd04aa
RS
241 FREE_AND_NULL(repo->graft_file);
242 FREE_AND_NULL(repo->index_file);
243 FREE_AND_NULL(repo->worktree);
244 FREE_AND_NULL(repo->submodule_prefix);
3b256228 245
90c62155
SB
246 raw_object_store_clear(repo->objects);
247 FREE_AND_NULL(repo->objects);
248
99bf115c
SB
249 parsed_object_pool_clear(repo->parsed_objects);
250 FREE_AND_NULL(repo->parsed_objects);
251
3b256228
BW
252 if (repo->config) {
253 git_configset_clear(repo->config);
90dd04aa 254 FREE_AND_NULL(repo->config);
3b256228 255 }
639e30b5 256
bf12fcdf
BW
257 if (repo->submodule_cache) {
258 submodule_cache_free(repo->submodule_cache);
259 repo->submodule_cache = NULL;
260 }
261
639e30b5
BW
262 if (repo->index) {
263 discard_index(repo->index);
74373b5f
NTND
264 if (repo->index != &the_index)
265 FREE_AND_NULL(repo->index);
639e30b5 266 }
ef7dc2e9
JT
267
268 if (repo->promisor_remote_config) {
269 promisor_remote_clear(repo->promisor_remote_config);
270 FREE_AND_NULL(repo->promisor_remote_config);
271 }
639e30b5
BW
272}
273
274int repo_read_index(struct repository *repo)
275{
3964fc2a
DS
276 int res;
277
639e30b5 278 if (!repo->index)
ca56dadb 279 CALLOC_ARRAY(repo->index, 1);
639e30b5 280
1fd9ae51
DS
281 /* Complete the double-reference */
282 if (!repo->index->repo)
283 repo->index->repo = repo;
284 else if (repo->index->repo != repo)
285 BUG("repo's index should point back at itself");
286
3964fc2a
DS
287 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
288
289 prepare_repo_settings(repo);
290 if (repo->settings.command_requires_full_index)
291 ensure_full_index(repo->index);
292
293 return res;
359efeff 294}
3a95f31d
NTND
295
296int repo_hold_locked_index(struct repository *repo,
297 struct lock_file *lf,
298 int flags)
299{
300 if (!repo->index_file)
301 BUG("the repo hasn't been setup");
302 return hold_lock_file_for_update(lf, repo->index_file, flags);
303}