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