]> git.ipfire.org Git - thirdparty/git.git/blame - repository.c
Merge branch 'jt/t5500-unflake'
[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"
359efeff
BW
13
14/* The main repository */
b2f0ecee
NTND
15static struct repository the_repo;
16struct repository *the_repository;
f8adbec9 17struct index_state the_index;
b2f0ecee
NTND
18
19void initialize_the_repository(void)
20{
21 the_repository = &the_repo;
22
23 the_repo.index = &the_index;
90c62155 24 the_repo.objects = raw_object_store_new();
99bf115c
SB
25 the_repo.parsed_objects = parsed_object_pool_new();
26
b2f0ecee
NTND
27 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
28}
359efeff 29
357a03eb
NTND
30static void expand_base_dir(char **out, const char *in,
31 const char *base_dir, const char *def_in)
32{
33 free(*out);
34 if (in)
35 *out = xstrdup(in);
36 else
37 *out = xstrfmt("%s/%s", base_dir, def_in);
38}
39
40static void repo_set_commondir(struct repository *repo,
41 const char *commondir)
359efeff
BW
42{
43 struct strbuf sb = STRBUF_INIT;
44
f9b7573f 45 free(repo->commondir);
357a03eb
NTND
46
47 if (commondir) {
48 repo->different_commondir = 1;
49 repo->commondir = xstrdup(commondir);
50 return;
51 }
52
53 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
359efeff 54 repo->commondir = strbuf_detach(&sb, NULL);
359efeff
BW
55}
56
357a03eb
NTND
57void repo_set_gitdir(struct repository *repo,
58 const char *root,
59 const struct set_gitdir_args *o)
359efeff 60{
357a03eb
NTND
61 const char *gitfile = read_gitfile(root);
62 /*
63 * repo->gitdir is saved because the caller could pass "root"
64 * that also points to repo->gitdir. We want to keep it alive
65 * until after xstrdup(root). Then we can free it.
66 */
1fb2b636 67 char *old_gitdir = repo->gitdir;
359efeff 68
357a03eb 69 repo->gitdir = xstrdup(gitfile ? gitfile : root);
1fb2b636 70 free(old_gitdir);
357a03eb
NTND
71
72 repo_set_commondir(repo, o->commondir);
f0eaf638
JK
73
74 if (!repo->objects->odb) {
75 repo->objects->odb = xcalloc(1, sizeof(*repo->objects->odb));
76 repo->objects->odb_tail = &repo->objects->odb->next;
77 }
78 expand_base_dir(&repo->objects->odb->path, o->object_dir,
357a03eb 79 repo->commondir, "objects");
f0eaf638 80
90c62155
SB
81 free(repo->objects->alternate_db);
82 repo->objects->alternate_db = xstrdup_or_null(o->alternate_db);
357a03eb
NTND
83 expand_base_dir(&repo->graft_file, o->graft_file,
84 repo->commondir, "info/grafts");
85 expand_base_dir(&repo->index_file, o->index_file,
86 repo->gitdir, "index");
359efeff
BW
87}
88
78a67668 89void repo_set_hash_algo(struct repository *repo, int hash_algo)
90{
91 repo->hash_algo = &hash_algos[hash_algo];
94127599 92#ifndef ENABLE_SHA256
93 if (hash_algo != GIT_HASH_SHA1)
94 die(_("The hash algorithm %s is not supported in this build."), repo->hash_algo->name);
95#endif
78a67668 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
359efeff
BW
178 if (worktree)
179 repo_set_worktree(repo, worktree);
180
e8805af1 181 clear_repository_format(&format);
359efeff
BW
182 return 0;
183
184error:
185 repo_clear(repo);
186 return -1;
187}
188
d5498e08 189int repo_submodule_init(struct repository *subrepo,
96dc883b 190 struct repository *superproject,
d5498e08 191 const struct submodule *sub)
96dc883b 192{
96dc883b
BW
193 struct strbuf gitdir = STRBUF_INIT;
194 struct strbuf worktree = STRBUF_INIT;
195 int ret = 0;
196
96dc883b
BW
197 if (!sub) {
198 ret = -1;
199 goto out;
200 }
201
d5498e08
SB
202 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path);
203 strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path);
96dc883b 204
d5498e08 205 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
96dc883b 206 /*
15beaaa3 207 * If initialization fails then it may be due to the submodule
96dc883b 208 * not being populated in the superproject's worktree. Instead
15beaaa3 209 * we can try to initialize the submodule by finding it's gitdir
96dc883b
BW
210 * in the superproject's 'modules' directory. In this case the
211 * submodule would not have a worktree.
212 */
213 strbuf_reset(&gitdir);
214 strbuf_repo_git_path(&gitdir, superproject,
215 "modules/%s", sub->name);
216
d5498e08 217 if (repo_init(subrepo, gitdir.buf, NULL)) {
96dc883b
BW
218 ret = -1;
219 goto out;
220 }
221 }
222
d5498e08
SB
223 subrepo->submodule_prefix = xstrfmt("%s%s/",
224 superproject->submodule_prefix ?
225 superproject->submodule_prefix :
226 "", sub->path);
96dc883b
BW
227
228out:
229 strbuf_release(&gitdir);
230 strbuf_release(&worktree);
231 return ret;
232}
233
359efeff
BW
234void repo_clear(struct repository *repo)
235{
90dd04aa
RS
236 FREE_AND_NULL(repo->gitdir);
237 FREE_AND_NULL(repo->commondir);
90dd04aa
RS
238 FREE_AND_NULL(repo->graft_file);
239 FREE_AND_NULL(repo->index_file);
240 FREE_AND_NULL(repo->worktree);
241 FREE_AND_NULL(repo->submodule_prefix);
3b256228 242
90c62155
SB
243 raw_object_store_clear(repo->objects);
244 FREE_AND_NULL(repo->objects);
245
99bf115c
SB
246 parsed_object_pool_clear(repo->parsed_objects);
247 FREE_AND_NULL(repo->parsed_objects);
248
3b256228
BW
249 if (repo->config) {
250 git_configset_clear(repo->config);
90dd04aa 251 FREE_AND_NULL(repo->config);
3b256228 252 }
639e30b5 253
bf12fcdf
BW
254 if (repo->submodule_cache) {
255 submodule_cache_free(repo->submodule_cache);
256 repo->submodule_cache = NULL;
257 }
258
639e30b5
BW
259 if (repo->index) {
260 discard_index(repo->index);
74373b5f
NTND
261 if (repo->index != &the_index)
262 FREE_AND_NULL(repo->index);
639e30b5
BW
263 }
264}
265
266int repo_read_index(struct repository *repo)
267{
268 if (!repo->index)
269 repo->index = xcalloc(1, sizeof(*repo->index));
639e30b5 270
a125a223 271 return read_index_from(repo->index, repo->index_file, repo->gitdir);
359efeff 272}
3a95f31d
NTND
273
274int repo_hold_locked_index(struct repository *repo,
275 struct lock_file *lf,
276 int flags)
277{
278 if (!repo->index_file)
279 BUG("the repo hasn't been setup");
280 return hold_lock_file_for_update(lf, repo->index_file, flags);
281}