Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 1 | /* |
| 2 | * not really _using_ the compat macros, just make sure the_index |
| 3 | * declaration matches the definition in this file. |
| 4 | */ |
Ævar Arnfjörð Bjarmason | 666f53e | 2022-11-19 14:07:36 +0100 | [diff] [blame] | 5 | #define USE_THE_INDEX_VARIABLE |
Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 6 | #include "git-compat-util.h" |
Elijah Newren | 0b027f6 | 2023-03-21 06:25:58 +0000 | [diff] [blame] | 7 | #include "abspath.h" |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 8 | #include "repository.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 9 | #include "object-store-ll.h" |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 10 | #include "config.h" |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 11 | #include "object.h" |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 12 | #include "lockfile.h" |
Elijah Newren | c339932 | 2023-05-16 06:33:59 +0000 | [diff] [blame] | 13 | #include "path.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 14 | #include "read-cache-ll.h" |
Glen Choo | fd3cb05 | 2021-11-17 16:53:22 -0800 | [diff] [blame] | 15 | #include "remote.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 16 | #include "setup.h" |
brian m. carlson | 23b2c7e | 2023-10-01 21:40:09 -0500 | [diff] [blame] | 17 | #include "loose.h" |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 18 | #include "submodule-config.h" |
Derrick Stolee | 3964fc2 | 2021-03-30 13:10:47 +0000 | [diff] [blame] | 19 | #include "sparse-index.h" |
Elijah Newren | 74ea5c9 | 2023-04-11 03:00:38 +0000 | [diff] [blame] | 20 | #include "trace2.h" |
Jonathan Tan | ef7dc2e | 2021-06-17 10:13:23 -0700 | [diff] [blame] | 21 | #include "promisor-remote.h" |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 22 | |
| 23 | /* The main repository */ |
Nguyễn Thái Ngọc Duy | b2f0ece | 2018-03-03 18:35:54 +0700 | [diff] [blame] | 24 | static struct repository the_repo; |
| 25 | struct repository *the_repository; |
Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 26 | struct index_state the_index; |
Nguyễn Thái Ngọc Duy | b2f0ece | 2018-03-03 18:35:54 +0700 | [diff] [blame] | 27 | |
| 28 | void initialize_the_repository(void) |
| 29 | { |
| 30 | the_repository = &the_repo; |
| 31 | |
| 32 | the_repo.index = &the_index; |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 33 | the_repo.objects = raw_object_store_new(); |
Glen Choo | fd3cb05 | 2021-11-17 16:53:22 -0800 | [diff] [blame] | 34 | the_repo.remote_state = remote_state_new(); |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 35 | the_repo.parsed_objects = parsed_object_pool_new(); |
| 36 | |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 37 | index_state_init(&the_index, the_repository); |
| 38 | |
Nguyễn Thái Ngọc Duy | b2f0ece | 2018-03-03 18:35:54 +0700 | [diff] [blame] | 39 | repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); |
| 40 | } |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 41 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 42 | static void expand_base_dir(char **out, const char *in, |
| 43 | const char *base_dir, const char *def_in) |
| 44 | { |
| 45 | free(*out); |
| 46 | if (in) |
| 47 | *out = xstrdup(in); |
| 48 | else |
| 49 | *out = xstrfmt("%s/%s", base_dir, def_in); |
| 50 | } |
| 51 | |
| 52 | static void repo_set_commondir(struct repository *repo, |
| 53 | const char *commondir) |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 54 | { |
| 55 | struct strbuf sb = STRBUF_INIT; |
| 56 | |
Jeff King | f9b7573 | 2017-09-05 09:04:57 -0400 | [diff] [blame] | 57 | free(repo->commondir); |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 58 | |
| 59 | if (commondir) { |
| 60 | repo->different_commondir = 1; |
| 61 | repo->commondir = xstrdup(commondir); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 66 | repo->commondir = strbuf_detach(&sb, NULL); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 69 | void repo_set_gitdir(struct repository *repo, |
| 70 | const char *root, |
| 71 | const struct set_gitdir_args *o) |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 72 | { |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 73 | const char *gitfile = read_gitfile(root); |
| 74 | /* |
| 75 | * repo->gitdir is saved because the caller could pass "root" |
| 76 | * that also points to repo->gitdir. We want to keep it alive |
| 77 | * until after xstrdup(root). Then we can free it. |
| 78 | */ |
Jeff King | 1fb2b63 | 2017-09-05 09:05:01 -0400 | [diff] [blame] | 79 | char *old_gitdir = repo->gitdir; |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 80 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 81 | repo->gitdir = xstrdup(gitfile ? gitfile : root); |
Jeff King | 1fb2b63 | 2017-09-05 09:05:01 -0400 | [diff] [blame] | 82 | free(old_gitdir); |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 83 | |
| 84 | repo_set_commondir(repo, o->commondir); |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 85 | |
| 86 | if (!repo->objects->odb) { |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 87 | CALLOC_ARRAY(repo->objects->odb, 1); |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 88 | repo->objects->odb_tail = &repo->objects->odb->next; |
| 89 | } |
| 90 | expand_base_dir(&repo->objects->odb->path, o->object_dir, |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 91 | repo->commondir, "objects"); |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 92 | |
Neeraj Singh | ecd81df | 2021-12-06 22:05:05 +0000 | [diff] [blame] | 93 | repo->objects->odb->disable_ref_updates = o->disable_ref_updates; |
| 94 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 95 | free(repo->objects->alternate_db); |
| 96 | repo->objects->alternate_db = xstrdup_or_null(o->alternate_db); |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 97 | expand_base_dir(&repo->graft_file, o->graft_file, |
| 98 | repo->commondir, "info/grafts"); |
| 99 | expand_base_dir(&repo->index_file, o->index_file, |
| 100 | repo->gitdir, "index"); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 101 | } |
| 102 | |
brian m. carlson | 78a6766 | 2017-11-12 21:28:53 +0000 | [diff] [blame] | 103 | void repo_set_hash_algo(struct repository *repo, int hash_algo) |
| 104 | { |
| 105 | repo->hash_algo = &hash_algos[hash_algo]; |
| 106 | } |
| 107 | |
Eric W. Biederman | 15a1ca1 | 2023-10-01 21:40:08 -0500 | [diff] [blame] | 108 | void repo_set_compat_hash_algo(struct repository *repo, int algo) |
| 109 | { |
| 110 | if (hash_algo_by_ptr(repo->hash_algo) == algo) |
| 111 | BUG("hash_algo and compat_hash_algo match"); |
| 112 | repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL; |
brian m. carlson | 23b2c7e | 2023-10-01 21:40:09 -0500 | [diff] [blame] | 113 | if (repo->compat_hash_algo) |
| 114 | repo_read_loose_object_map(repo); |
Eric W. Biederman | 15a1ca1 | 2023-10-01 21:40:08 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Patrick Steinhardt | 173761e | 2023-12-29 08:26:39 +0100 | [diff] [blame] | 117 | void repo_set_ref_storage_format(struct repository *repo, unsigned int format) |
| 118 | { |
| 119 | repo->ref_storage_format = format; |
| 120 | } |
| 121 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 122 | /* |
| 123 | * Attempt to resolve and set the provided 'gitdir' for repository 'repo'. |
| 124 | * Return 0 upon success and a non-zero value upon failure. |
| 125 | */ |
| 126 | static int repo_init_gitdir(struct repository *repo, const char *gitdir) |
| 127 | { |
| 128 | int ret = 0; |
| 129 | int error = 0; |
| 130 | char *abspath = NULL; |
| 131 | const char *resolved_gitdir; |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 132 | struct set_gitdir_args args = { NULL }; |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 133 | |
| 134 | abspath = real_pathdup(gitdir, 0); |
| 135 | if (!abspath) { |
| 136 | ret = -1; |
| 137 | goto out; |
| 138 | } |
| 139 | |
| 140 | /* 'gitdir' must reference the gitdir directly */ |
| 141 | resolved_gitdir = resolve_gitdir_gently(abspath, &error); |
| 142 | if (!resolved_gitdir) { |
| 143 | ret = -1; |
| 144 | goto out; |
| 145 | } |
| 146 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 147 | repo_set_gitdir(repo, resolved_gitdir, &args); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 148 | |
| 149 | out: |
| 150 | free(abspath); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | void repo_set_worktree(struct repository *repo, const char *path) |
| 155 | { |
| 156 | repo->worktree = real_pathdup(path, 1); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 157 | |
| 158 | trace2_def_repo(repo); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static int read_and_verify_repository_format(struct repository_format *format, |
| 162 | const char *commondir) |
| 163 | { |
| 164 | int ret = 0; |
| 165 | struct strbuf sb = STRBUF_INIT; |
| 166 | |
| 167 | strbuf_addf(&sb, "%s/config", commondir); |
| 168 | read_repository_format(format, sb.buf); |
| 169 | strbuf_reset(&sb); |
| 170 | |
| 171 | if (verify_repository_format(format, &sb) < 0) { |
| 172 | warning("%s", sb.buf); |
| 173 | ret = -1; |
| 174 | } |
| 175 | |
| 176 | strbuf_release(&sb); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Initialize 'repo' based on the provided 'gitdir'. |
| 182 | * Return 0 upon success and a non-zero value upon failure. |
| 183 | */ |
Stefan Beller | da62f78 | 2018-03-28 15:35:31 -0700 | [diff] [blame] | 184 | int repo_init(struct repository *repo, |
| 185 | const char *gitdir, |
| 186 | const char *worktree) |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 187 | { |
Martin Ågren | e8805af | 2019-02-28 21:36:28 +0100 | [diff] [blame] | 188 | struct repository_format format = REPOSITORY_FORMAT_INIT; |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 189 | memset(repo, 0, sizeof(*repo)); |
| 190 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 191 | repo->objects = raw_object_store_new(); |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 192 | repo->parsed_objects = parsed_object_pool_new(); |
Glen Choo | fd3cb05 | 2021-11-17 16:53:22 -0800 | [diff] [blame] | 193 | repo->remote_state = remote_state_new(); |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 194 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 195 | if (repo_init_gitdir(repo, gitdir)) |
| 196 | goto error; |
| 197 | |
| 198 | if (read_and_verify_repository_format(&format, repo->commondir)) |
| 199 | goto error; |
| 200 | |
brian m. carlson | 78a6766 | 2017-11-12 21:28:53 +0000 | [diff] [blame] | 201 | repo_set_hash_algo(repo, format.hash_algo); |
brian m. carlson | 9ae702f | 2023-10-01 21:40:25 -0500 | [diff] [blame] | 202 | repo_set_compat_hash_algo(repo, format.compat_hash_algo); |
Patrick Steinhardt | 173761e | 2023-12-29 08:26:39 +0100 | [diff] [blame] | 203 | repo_set_ref_storage_format(repo, format.ref_storage_format); |
Victoria Dye | 3867f6d | 2023-05-26 01:33:00 +0000 | [diff] [blame] | 204 | repo->repository_format_worktree_config = format.worktree_config; |
brian m. carlson | 78a6766 | 2017-11-12 21:28:53 +0000 | [diff] [blame] | 205 | |
Jonathan Tan | ebaf3bc | 2021-06-17 10:13:22 -0700 | [diff] [blame] | 206 | /* take ownership of format.partial_clone */ |
| 207 | repo->repository_format_partial_clone = format.partial_clone; |
| 208 | format.partial_clone = NULL; |
| 209 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 210 | if (worktree) |
| 211 | repo_set_worktree(repo, worktree); |
| 212 | |
brian m. carlson | 23b2c7e | 2023-10-01 21:40:09 -0500 | [diff] [blame] | 213 | if (repo->compat_hash_algo) |
| 214 | repo_read_loose_object_map(repo); |
| 215 | |
Martin Ågren | e8805af | 2019-02-28 21:36:28 +0100 | [diff] [blame] | 216 | clear_repository_format(&format); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 217 | return 0; |
| 218 | |
| 219 | error: |
| 220 | repo_clear(repo); |
| 221 | return -1; |
| 222 | } |
| 223 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 224 | int repo_submodule_init(struct repository *subrepo, |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 225 | struct repository *superproject, |
Jonathan Tan | 8eb8dcf | 2021-09-09 11:47:28 -0700 | [diff] [blame] | 226 | const char *path, |
| 227 | const struct object_id *treeish_name) |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 228 | { |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 229 | struct strbuf gitdir = STRBUF_INIT; |
| 230 | struct strbuf worktree = STRBUF_INIT; |
| 231 | int ret = 0; |
| 232 | |
Jonathan Tan | 8eb8dcf | 2021-09-09 11:47:28 -0700 | [diff] [blame] | 233 | strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path); |
| 234 | strbuf_repo_worktree_path(&worktree, superproject, "%s", path); |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 235 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 236 | if (repo_init(subrepo, gitdir.buf, worktree.buf)) { |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 237 | /* |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 238 | * If initialization fails then it may be due to the submodule |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 239 | * not being populated in the superproject's worktree. Instead |
Elijah Newren | 15beaaa | 2019-11-05 17:07:23 +0000 | [diff] [blame] | 240 | * we can try to initialize the submodule by finding it's gitdir |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 241 | * in the superproject's 'modules' directory. In this case the |
| 242 | * submodule would not have a worktree. |
| 243 | */ |
Jonathan Tan | 8eb8dcf | 2021-09-09 11:47:28 -0700 | [diff] [blame] | 244 | const struct submodule *sub = |
| 245 | submodule_from_path(superproject, treeish_name, path); |
| 246 | if (!sub) { |
| 247 | ret = -1; |
| 248 | goto out; |
| 249 | } |
| 250 | |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 251 | strbuf_reset(&gitdir); |
Jonathan Tan | ce125d4 | 2021-09-15 11:59:19 -0700 | [diff] [blame] | 252 | submodule_name_to_gitdir(&gitdir, superproject, sub->name); |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 253 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 254 | if (repo_init(subrepo, gitdir.buf, NULL)) { |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 255 | ret = -1; |
| 256 | goto out; |
| 257 | } |
| 258 | } |
| 259 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 260 | subrepo->submodule_prefix = xstrfmt("%s%s/", |
| 261 | superproject->submodule_prefix ? |
| 262 | superproject->submodule_prefix : |
Jonathan Tan | 8eb8dcf | 2021-09-09 11:47:28 -0700 | [diff] [blame] | 263 | "", path); |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 264 | |
| 265 | out: |
| 266 | strbuf_release(&gitdir); |
| 267 | strbuf_release(&worktree); |
| 268 | return ret; |
| 269 | } |
| 270 | |
Ævar Arnfjörð Bjarmason | 759f340 | 2022-03-04 19:32:17 +0100 | [diff] [blame] | 271 | static void repo_clear_path_cache(struct repo_path_cache *cache) |
| 272 | { |
| 273 | FREE_AND_NULL(cache->squash_msg); |
| 274 | FREE_AND_NULL(cache->squash_msg); |
| 275 | FREE_AND_NULL(cache->merge_msg); |
| 276 | FREE_AND_NULL(cache->merge_rr); |
| 277 | FREE_AND_NULL(cache->merge_mode); |
| 278 | FREE_AND_NULL(cache->merge_head); |
Ævar Arnfjörð Bjarmason | 759f340 | 2022-03-04 19:32:17 +0100 | [diff] [blame] | 279 | FREE_AND_NULL(cache->fetch_head); |
| 280 | FREE_AND_NULL(cache->shallow); |
| 281 | } |
| 282 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 283 | void repo_clear(struct repository *repo) |
| 284 | { |
René Scharfe | 90dd04a | 2017-10-01 16:44:46 +0200 | [diff] [blame] | 285 | FREE_AND_NULL(repo->gitdir); |
| 286 | FREE_AND_NULL(repo->commondir); |
René Scharfe | 90dd04a | 2017-10-01 16:44:46 +0200 | [diff] [blame] | 287 | FREE_AND_NULL(repo->graft_file); |
| 288 | FREE_AND_NULL(repo->index_file); |
| 289 | FREE_AND_NULL(repo->worktree); |
| 290 | FREE_AND_NULL(repo->submodule_prefix); |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 291 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 292 | raw_object_store_clear(repo->objects); |
| 293 | FREE_AND_NULL(repo->objects); |
| 294 | |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 295 | parsed_object_pool_clear(repo->parsed_objects); |
| 296 | FREE_AND_NULL(repo->parsed_objects); |
| 297 | |
Johannes Schindelin | 6741e91 | 2024-04-12 10:49:58 +0200 | [diff] [blame] | 298 | FREE_AND_NULL(repo->settings.fsmonitor); |
| 299 | |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 300 | if (repo->config) { |
| 301 | git_configset_clear(repo->config); |
René Scharfe | 90dd04a | 2017-10-01 16:44:46 +0200 | [diff] [blame] | 302 | FREE_AND_NULL(repo->config); |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 303 | } |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 304 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 305 | if (repo->submodule_cache) { |
| 306 | submodule_cache_free(repo->submodule_cache); |
| 307 | repo->submodule_cache = NULL; |
| 308 | } |
| 309 | |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 310 | if (repo->index) { |
| 311 | discard_index(repo->index); |
Nguyễn Thái Ngọc Duy | 74373b5 | 2018-05-10 08:13:10 +0200 | [diff] [blame] | 312 | if (repo->index != &the_index) |
| 313 | FREE_AND_NULL(repo->index); |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 314 | } |
Jonathan Tan | ef7dc2e | 2021-06-17 10:13:23 -0700 | [diff] [blame] | 315 | |
| 316 | if (repo->promisor_remote_config) { |
| 317 | promisor_remote_clear(repo->promisor_remote_config); |
| 318 | FREE_AND_NULL(repo->promisor_remote_config); |
| 319 | } |
Glen Choo | fd3cb05 | 2021-11-17 16:53:22 -0800 | [diff] [blame] | 320 | |
| 321 | if (repo->remote_state) { |
| 322 | remote_state_clear(repo->remote_state); |
| 323 | FREE_AND_NULL(repo->remote_state); |
| 324 | } |
Ævar Arnfjörð Bjarmason | 759f340 | 2022-03-04 19:32:17 +0100 | [diff] [blame] | 325 | |
| 326 | repo_clear_path_cache(&repo->cached_paths); |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | int repo_read_index(struct repository *repo) |
| 330 | { |
Derrick Stolee | 3964fc2 | 2021-03-30 13:10:47 +0000 | [diff] [blame] | 331 | int res; |
| 332 | |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 333 | /* Complete the double-reference */ |
Ævar Arnfjörð Bjarmason | 2f6b1eb | 2023-01-12 13:55:27 +0100 | [diff] [blame] | 334 | if (!repo->index) { |
| 335 | ALLOC_ARRAY(repo->index, 1); |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 336 | index_state_init(repo->index, repo); |
| 337 | } else if (repo->index->repo != repo) { |
Derrick Stolee | 1fd9ae5 | 2021-01-23 19:58:15 +0000 | [diff] [blame] | 338 | BUG("repo's index should point back at itself"); |
Ævar Arnfjörð Bjarmason | 6269f8e | 2023-01-17 14:57:00 +0100 | [diff] [blame] | 339 | } |
Derrick Stolee | 1fd9ae5 | 2021-01-23 19:58:15 +0000 | [diff] [blame] | 340 | |
Derrick Stolee | 3964fc2 | 2021-03-30 13:10:47 +0000 | [diff] [blame] | 341 | res = read_index_from(repo->index, repo->index_file, repo->gitdir); |
| 342 | |
| 343 | prepare_repo_settings(repo); |
| 344 | if (repo->settings.command_requires_full_index) |
| 345 | ensure_full_index(repo->index); |
| 346 | |
Elijah Newren | af6a518 | 2022-01-14 15:59:41 +0000 | [diff] [blame] | 347 | /* |
| 348 | * If sparse checkouts are in use, check whether paths with the |
| 349 | * SKIP_WORKTREE attribute are missing from the worktree; if not, |
| 350 | * clear that attribute for that path. |
| 351 | */ |
| 352 | clear_skip_worktree_from_present_files(repo->index); |
| 353 | |
Derrick Stolee | 3964fc2 | 2021-03-30 13:10:47 +0000 | [diff] [blame] | 354 | return res; |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 355 | } |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 356 | |
| 357 | int repo_hold_locked_index(struct repository *repo, |
| 358 | struct lock_file *lf, |
| 359 | int flags) |
| 360 | { |
| 361 | if (!repo->index_file) |
| 362 | BUG("the repo hasn't been setup"); |
| 363 | return hold_lock_file_for_update(lf, repo->index_file, flags); |
| 364 | } |