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