Raw
1 #include "git-compat-util.h"
2 #include "abspath.h"
3 #include "repository.h"
4 #include "hook.h"
5 #include "odb.h"
6 #include "odb/source.h"
7 #include "config.h"
8 #include "gettext.h"
9 #include "object.h"
10 #include "lockfile.h"
11 #include "path.h"
12 #include "read-cache-ll.h"
13 #include "remote.h"
14 #include "setup.h"
15 #include "loose.h"
16 #include "submodule-config.h"
17 #include "sparse-index.h"
18 #include "trace2.h"
19 #include "promisor-remote.h"
20 #include "refs.h"
21
22 /*
23 * We do not define `USE_THE_REPOSITORY_VARIABLE` in this file because we do
24 * not want to rely on functions that implicitly use `the_repository`. This
25 * means that the `extern` declaration of `the_repository` isn't visible here,
26 * which makes sparse unhappy. We thus declare it here.
27 */
28 extern struct repository *the_repository;
29
30 /* The main repository */
31 static struct repository the_repo;
32 struct repository *the_repository = &the_repo;
33
34 /*
35 * An escape hatch: if we hit a bug in the production code that fails
36 * to set an appropriate hash algorithm (most likely to happen when
37 * running outside a repository), we can tell the user who reported
38 * the crash to set the environment variable to "sha1" (all lowercase)
39 * to revert to the historical behaviour of defaulting to SHA-1.
40 */
41 static void set_default_hash_algo(struct repository *repo)
42 {
43 const char *hash_name;
44 uint32_t algo;
45
46 hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
47 if (!hash_name)
48 return;
49 algo = hash_algo_by_name(hash_name);
50 if (algo == GIT_HASH_UNKNOWN)
51 return;
52
53 repo_set_hash_algo(repo, algo);
54 }
55
56 struct repo_config_values *repo_config_values(struct repository *repo)
57 {
58 if (repo != the_repository)
59 BUG("trying to read config from wrong repository instance");
60 if (!repo->initialized)
61 BUG("config values from uninitialized repository");
62 return &repo->config_values_private_;
63 }
64
65 void initialize_repository(struct repository *repo)
66 {
67 if (repo->initialized)
68 BUG("repository initialized already");
69 repo->initialized = true;
70
71 repo->remote_state = remote_state_new();
72 repo->parsed_objects = parsed_object_pool_new(repo);
73 ALLOC_ARRAY(repo->index, 1);
74 index_state_init(repo->index, repo);
75 repo->check_deprecated_config = true;
76 repo->bare_cfg = -1;
77 repo_config_values_init(&repo->config_values_private_);
78
79 /*
80 * When a command runs inside a repository, it learns what
81 * hash algorithm is in use from the repository, but some
82 * commands are designed to work outside a repository, yet
83 * they want to access the_hash_algo, if only for the length
84 * of the hashed value to see if their input looks like a
85 * plausible hash value.
86 *
87 * We are in the process of identifying such code paths and
88 * giving them an appropriate default individually; any
89 * unconverted code paths that try to access the_hash_algo
90 * will thus fail. The end-users however have an escape hatch
91 * to set GIT_TEST_DEFAULT_HASH_ALGO environment variable to
92 * "sha1" to get back the old behaviour of defaulting to SHA-1.
93 *
94 * This escape hatch is deliberately kept unadvertised, so
95 * that they see crashes and we can get a report before
96 * telling them about it.
97 */
98 if (repo == the_repository)
99 set_default_hash_algo(repo);
100 }
101
102 static void expand_base_dir(char **out, const char *in,
103 const char *base_dir, const char *def_in)
104 {
105 free(*out);
106 if (in)
107 *out = xstrdup(in);
108 else
109 *out = xstrfmt("%s/%s", base_dir, def_in);
110 }
111
112 const char *repo_get_git_dir(struct repository *repo)
113 {
114 if (!repo->gitdir)
115 BUG("repository hasn't been set up");
116 return repo->gitdir;
117 }
118
119 const char *repo_get_common_dir(struct repository *repo)
120 {
121 if (!repo->commondir)
122 BUG("repository hasn't been set up");
123 return repo->commondir;
124 }
125
126 const char *repo_get_object_directory(struct repository *repo)
127 {
128 if (!repo->objects->sources)
129 BUG("repository hasn't been set up");
130 return repo->objects->sources->path;
131 }
132
133 const char *repo_get_index_file(struct repository *repo)
134 {
135 if (!repo->index_file)
136 BUG("repository hasn't been set up");
137 return repo->index_file;
138 }
139
140 const char *repo_get_graft_file(struct repository *repo)
141 {
142 if (!repo->graft_file)
143 BUG("repository hasn't been set up");
144 return repo->graft_file;
145 }
146
147 const char *repo_get_work_tree(struct repository *repo)
148 {
149 return repo->worktree;
150 }
151
152 static void repo_set_commondir(struct repository *repo,
153 const char *commondir)
154 {
155 struct strbuf sb = STRBUF_INIT;
156
157 free(repo->commondir);
158
159 if (commondir) {
160 repo->different_commondir = 1;
161 repo->commondir = xstrdup(commondir);
162 return;
163 }
164
165 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
166 repo->commondir = strbuf_detach(&sb, NULL);
167 }
168
169 void repo_set_gitdir(struct repository *repo,
170 const char *root,
171 const struct set_gitdir_args *o)
172 {
173 const char *gitfile = read_gitfile(root);
174 /*
175 * repo->gitdir is saved because the caller could pass "root"
176 * that also points to repo->gitdir. We want to keep it alive
177 * until after xstrdup(root). Then we can free it.
178 */
179 char *old_gitdir = repo->gitdir;
180
181 repo->gitdir = xstrdup(gitfile ? gitfile : root);
182 free(old_gitdir);
183
184 repo_set_commondir(repo, o->commondir);
185 repo->disable_ref_updates = o->disable_ref_updates;
186
187 expand_base_dir(&repo->graft_file, o->graft_file,
188 repo->commondir, "info/grafts");
189 expand_base_dir(&repo->index_file, o->index_file,
190 repo->gitdir, "index");
191 }
192
193 void repo_set_hash_algo(struct repository *repo, uint32_t hash_algo)
194 {
195 repo->hash_algo = &hash_algos[hash_algo];
196 }
197
198 void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, uint32_t algo)
199 {
200 #ifdef WITH_RUST
201 if (hash_algo_by_ptr(repo->hash_algo) == algo)
202 BUG("hash_algo and compat_hash_algo match");
203 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
204 if (repo->compat_hash_algo)
205 repo_read_loose_object_map(repo);
206 #else
207 if (algo)
208 die(_("compatibility hash algorithm support requires Rust"));
209 #endif
210 }
211
212 void repo_set_ref_storage_format(struct repository *repo,
213 enum ref_storage_format format,
214 const char *payload)
215 {
216 repo->ref_storage_format = format;
217 free(repo->ref_storage_payload);
218 repo->ref_storage_payload = xstrdup_or_null(payload);
219 }
220
221 /*
222 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
223 * Return 0 upon success and a non-zero value upon failure.
224 */
225 static int repo_init_gitdir(struct repository *repo, const char *gitdir)
226 {
227 int ret = 0;
228 int error = 0;
229 char *abspath = NULL;
230 const char *resolved_gitdir;
231 struct set_gitdir_args args = { NULL };
232
233 abspath = real_pathdup(gitdir, 0);
234 if (!abspath) {
235 ret = -1;
236 goto out;
237 }
238
239 /* 'gitdir' must reference the gitdir directly */
240 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
241 if (!resolved_gitdir) {
242 ret = -1;
243 goto out;
244 }
245
246 repo_set_gitdir(repo, resolved_gitdir, &args);
247
248 out:
249 free(abspath);
250 return ret;
251 }
252
253 void repo_set_worktree(struct repository *repo, const char *path)
254 {
255 repo->worktree = real_pathdup(path, 1);
256
257 trace2_def_repo(repo);
258 }
259
260 static int read_repository_format_from_commondir(struct repository_format *format,
261 const char *commondir)
262 {
263 int ret = 0;
264 struct strbuf sb = STRBUF_INIT;
265
266 strbuf_addf(&sb, "%s/config", commondir);
267 read_repository_format(format, sb.buf);
268 strbuf_reset(&sb);
269
270 strbuf_release(&sb);
271 return ret;
272 }
273
274 /*
275 * Initialize 'repo' based on the provided 'gitdir'.
276 * Return 0 upon success and a non-zero value upon failure.
277 */
278 int repo_init(struct repository *repo,
279 const char *gitdir,
280 const char *worktree)
281 {
282 struct repository_format format = REPOSITORY_FORMAT_INIT;
283 struct strbuf err = STRBUF_INIT;
284
285 memset(repo, 0, sizeof(*repo));
286
287 initialize_repository(repo);
288
289 if (repo_init_gitdir(repo, gitdir))
290 goto error;
291
292 if (read_repository_format_from_commondir(&format, repo->commondir))
293 goto error;
294
295 if (apply_repository_format(repo, &format, 0, &err) < 0) {
296 warning("%s", err.buf);
297 goto error;
298 }
299
300 if (worktree)
301 repo_set_worktree(repo, worktree);
302
303 clear_repository_format(&format);
304 strbuf_release(&err);
305 return 0;
306
307 error:
308 clear_repository_format(&format);
309 strbuf_release(&err);
310 repo_clear(repo);
311 return -1;
312 }
313
314 int repo_submodule_init(struct repository *subrepo,
315 struct repository *superproject,
316 const char *path,
317 const struct object_id *treeish_name)
318 {
319 struct strbuf gitdir = STRBUF_INIT;
320 struct strbuf worktree = STRBUF_INIT;
321 int ret = 0;
322
323 repo_worktree_path_append(superproject, &gitdir, "%s/.git", path);
324 repo_worktree_path_append(superproject, &worktree, "%s", path);
325
326 if (repo_init(subrepo, gitdir.buf, worktree.buf)) {
327 /*
328 * If initialization fails then it may be due to the submodule
329 * not being populated in the superproject's worktree. Instead
330 * we can try to initialize the submodule by finding it's gitdir
331 * in the superproject's 'modules' directory. In this case the
332 * submodule would not have a worktree.
333 */
334 const struct submodule *sub =
335 submodule_from_path(superproject, treeish_name, path);
336 if (!sub) {
337 ret = -1;
338 goto out;
339 }
340
341 strbuf_reset(&gitdir);
342 submodule_name_to_gitdir(&gitdir, superproject, sub->name);
343
344 if (repo_init(subrepo, gitdir.buf, NULL)) {
345 ret = -1;
346 goto out;
347 }
348 }
349
350 subrepo->submodule_prefix = xstrfmt("%s%s/",
351 superproject->submodule_prefix ?
352 superproject->submodule_prefix :
353 "", path);
354
355 out:
356 strbuf_release(&gitdir);
357 strbuf_release(&worktree);
358 return ret;
359 }
360
361 static void repo_clear_path_cache(struct repo_path_cache *cache)
362 {
363 FREE_AND_NULL(cache->squash_msg);
364 FREE_AND_NULL(cache->merge_msg);
365 FREE_AND_NULL(cache->merge_rr);
366 FREE_AND_NULL(cache->merge_mode);
367 FREE_AND_NULL(cache->merge_head);
368 FREE_AND_NULL(cache->fetch_head);
369 FREE_AND_NULL(cache->shallow);
370 }
371
372 void repo_clear(struct repository *repo)
373 {
374 struct hashmap_iter iter;
375 struct strmap_entry *e;
376
377 FREE_AND_NULL(repo->gitdir);
378 FREE_AND_NULL(repo->commondir);
379 FREE_AND_NULL(repo->prefix);
380 FREE_AND_NULL(repo->graft_file);
381 FREE_AND_NULL(repo->index_file);
382 FREE_AND_NULL(repo->worktree);
383 FREE_AND_NULL(repo->submodule_prefix);
384 FREE_AND_NULL(repo->ref_storage_payload);
385
386 odb_free(repo->objects);
387 repo->objects = NULL;
388
389 parsed_object_pool_clear(repo->parsed_objects);
390 FREE_AND_NULL(repo->parsed_objects);
391
392 repo_settings_clear(repo);
393 repo_config_values_clear(&repo->config_values_private_);
394
395 if (repo->config) {
396 git_configset_clear(repo->config);
397 FREE_AND_NULL(repo->config);
398 }
399
400 if (repo->submodule_cache) {
401 submodule_cache_free(repo->submodule_cache);
402 repo->submodule_cache = NULL;
403 }
404
405 if (repo->index) {
406 discard_index(repo->index);
407 FREE_AND_NULL(repo->index);
408 }
409
410 if (repo->hook_config_cache) {
411 hook_cache_clear(repo->hook_config_cache);
412 FREE_AND_NULL(repo->hook_config_cache);
413 }
414 strmap_clear(&repo->event_jobs, 0); /* values are uintptr_t, not heap ptrs */
415 string_list_clear(&repo->disabled_events, 0);
416
417 if (repo->promisor_remote_config) {
418 promisor_remote_clear(repo->promisor_remote_config);
419 FREE_AND_NULL(repo->promisor_remote_config);
420 }
421
422 if (repo->remote_state) {
423 remote_state_clear(repo->remote_state);
424 FREE_AND_NULL(repo->remote_state);
425 }
426
427 if (repo->refs_private) {
428 ref_store_release(repo->refs_private);
429 FREE_AND_NULL(repo->refs_private);
430 }
431
432 strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e)
433 ref_store_release(e->value);
434 strmap_clear(&repo->submodule_ref_stores, 1);
435
436 strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e)
437 ref_store_release(e->value);
438 strmap_clear(&repo->worktree_ref_stores, 1);
439
440 repo_clear_path_cache(&repo->cached_paths);
441 }
442
443 int repo_read_index(struct repository *repo)
444 {
445 int res;
446
447 /* Complete the double-reference */
448 if (!repo->index) {
449 ALLOC_ARRAY(repo->index, 1);
450 index_state_init(repo->index, repo);
451 } else if (repo->index->repo != repo) {
452 BUG("repo's index should point back at itself");
453 }
454
455 res = read_index_from(repo->index, repo->index_file, repo->gitdir);
456
457 prepare_repo_settings(repo);
458 if (repo->settings.command_requires_full_index)
459 ensure_full_index(repo->index);
460
461 /*
462 * If sparse checkouts are in use, check whether paths with the
463 * SKIP_WORKTREE attribute are missing from the worktree; if not,
464 * clear that attribute for that path.
465 */
466 clear_skip_worktree_from_present_files(repo->index);
467
468 return res;
469 }
470
471 int repo_hold_locked_index(struct repository *repo,
472 struct lock_file *lf,
473 int flags)
474 {
475 if (!repo->index_file)
476 BUG("the repo hasn't been setup");
477 return repo_hold_lock_file_for_update(repo, lf, repo->index_file, flags);
478 }