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