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