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