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