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