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 | */ |
| 5 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 6 | #include "cache.h" |
| 7 | #include "repository.h" |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 8 | #include "object-store.h" |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 9 | #include "config.h" |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 10 | #include "object.h" |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 11 | #include "lockfile.h" |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 12 | #include "submodule-config.h" |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 13 | |
| 14 | /* The main repository */ |
Nguyễn Thái Ngọc Duy | b2f0ece | 2018-03-03 18:35:54 +0700 | [diff] [blame] | 15 | static struct repository the_repo; |
| 16 | struct repository *the_repository; |
Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 17 | struct index_state the_index; |
Nguyễn Thái Ngọc Duy | b2f0ece | 2018-03-03 18:35:54 +0700 | [diff] [blame] | 18 | |
| 19 | void initialize_the_repository(void) |
| 20 | { |
| 21 | the_repository = &the_repo; |
| 22 | |
| 23 | the_repo.index = &the_index; |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 24 | the_repo.objects = raw_object_store_new(); |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 25 | the_repo.parsed_objects = parsed_object_pool_new(); |
| 26 | |
Nguyễn Thái Ngọc Duy | b2f0ece | 2018-03-03 18:35:54 +0700 | [diff] [blame] | 27 | repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); |
| 28 | } |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 29 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 30 | static void expand_base_dir(char **out, const char *in, |
| 31 | const char *base_dir, const char *def_in) |
| 32 | { |
| 33 | free(*out); |
| 34 | if (in) |
| 35 | *out = xstrdup(in); |
| 36 | else |
| 37 | *out = xstrfmt("%s/%s", base_dir, def_in); |
| 38 | } |
| 39 | |
| 40 | static void repo_set_commondir(struct repository *repo, |
| 41 | const char *commondir) |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 42 | { |
| 43 | struct strbuf sb = STRBUF_INIT; |
| 44 | |
Jeff King | f9b7573 | 2017-09-05 09:04:57 -0400 | [diff] [blame] | 45 | free(repo->commondir); |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 46 | |
| 47 | if (commondir) { |
| 48 | repo->different_commondir = 1; |
| 49 | repo->commondir = xstrdup(commondir); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 54 | repo->commondir = strbuf_detach(&sb, NULL); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 57 | void repo_set_gitdir(struct repository *repo, |
| 58 | const char *root, |
| 59 | const struct set_gitdir_args *o) |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 60 | { |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 61 | const char *gitfile = read_gitfile(root); |
| 62 | /* |
| 63 | * repo->gitdir is saved because the caller could pass "root" |
| 64 | * that also points to repo->gitdir. We want to keep it alive |
| 65 | * until after xstrdup(root). Then we can free it. |
| 66 | */ |
Jeff King | 1fb2b63 | 2017-09-05 09:05:01 -0400 | [diff] [blame] | 67 | char *old_gitdir = repo->gitdir; |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 68 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 69 | repo->gitdir = xstrdup(gitfile ? gitfile : root); |
Jeff King | 1fb2b63 | 2017-09-05 09:05:01 -0400 | [diff] [blame] | 70 | free(old_gitdir); |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 71 | |
| 72 | repo_set_commondir(repo, o->commondir); |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 73 | |
| 74 | if (!repo->objects->odb) { |
| 75 | repo->objects->odb = xcalloc(1, sizeof(*repo->objects->odb)); |
| 76 | repo->objects->odb_tail = &repo->objects->odb->next; |
| 77 | } |
| 78 | 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] | 79 | repo->commondir, "objects"); |
Jeff King | f0eaf63 | 2018-11-12 09:50:39 -0500 | [diff] [blame] | 80 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 81 | free(repo->objects->alternate_db); |
| 82 | 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] | 83 | expand_base_dir(&repo->graft_file, o->graft_file, |
| 84 | repo->commondir, "info/grafts"); |
| 85 | expand_base_dir(&repo->index_file, o->index_file, |
| 86 | repo->gitdir, "index"); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 87 | } |
| 88 | |
brian m. carlson | 78a6766 | 2017-11-12 21:28:53 +0000 | [diff] [blame] | 89 | void repo_set_hash_algo(struct repository *repo, int hash_algo) |
| 90 | { |
| 91 | repo->hash_algo = &hash_algos[hash_algo]; |
| 92 | } |
| 93 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 94 | /* |
| 95 | * Attempt to resolve and set the provided 'gitdir' for repository 'repo'. |
| 96 | * Return 0 upon success and a non-zero value upon failure. |
| 97 | */ |
| 98 | static int repo_init_gitdir(struct repository *repo, const char *gitdir) |
| 99 | { |
| 100 | int ret = 0; |
| 101 | int error = 0; |
| 102 | char *abspath = NULL; |
| 103 | const char *resolved_gitdir; |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 104 | struct set_gitdir_args args = { NULL }; |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 105 | |
| 106 | abspath = real_pathdup(gitdir, 0); |
| 107 | if (!abspath) { |
| 108 | ret = -1; |
| 109 | goto out; |
| 110 | } |
| 111 | |
| 112 | /* 'gitdir' must reference the gitdir directly */ |
| 113 | resolved_gitdir = resolve_gitdir_gently(abspath, &error); |
| 114 | if (!resolved_gitdir) { |
| 115 | ret = -1; |
| 116 | goto out; |
| 117 | } |
| 118 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 119 | repo_set_gitdir(repo, resolved_gitdir, &args); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 120 | |
| 121 | out: |
| 122 | free(abspath); |
| 123 | return ret; |
| 124 | } |
| 125 | |
| 126 | void repo_set_worktree(struct repository *repo, const char *path) |
| 127 | { |
| 128 | repo->worktree = real_pathdup(path, 1); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 129 | |
| 130 | trace2_def_repo(repo); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static int read_and_verify_repository_format(struct repository_format *format, |
| 134 | const char *commondir) |
| 135 | { |
| 136 | int ret = 0; |
| 137 | struct strbuf sb = STRBUF_INIT; |
| 138 | |
| 139 | strbuf_addf(&sb, "%s/config", commondir); |
| 140 | read_repository_format(format, sb.buf); |
| 141 | strbuf_reset(&sb); |
| 142 | |
| 143 | if (verify_repository_format(format, &sb) < 0) { |
| 144 | warning("%s", sb.buf); |
| 145 | ret = -1; |
| 146 | } |
| 147 | |
| 148 | strbuf_release(&sb); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Initialize 'repo' based on the provided 'gitdir'. |
| 154 | * Return 0 upon success and a non-zero value upon failure. |
| 155 | */ |
Stefan Beller | da62f78 | 2018-03-28 15:35:31 -0700 | [diff] [blame] | 156 | int repo_init(struct repository *repo, |
| 157 | const char *gitdir, |
| 158 | const char *worktree) |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 159 | { |
Martin Ågren | e8805af | 2019-02-28 21:36:28 +0100 | [diff] [blame] | 160 | struct repository_format format = REPOSITORY_FORMAT_INIT; |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 161 | memset(repo, 0, sizeof(*repo)); |
| 162 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 163 | repo->objects = raw_object_store_new(); |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 164 | repo->parsed_objects = parsed_object_pool_new(); |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 165 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 166 | if (repo_init_gitdir(repo, gitdir)) |
| 167 | goto error; |
| 168 | |
| 169 | if (read_and_verify_repository_format(&format, repo->commondir)) |
| 170 | goto error; |
| 171 | |
brian m. carlson | 78a6766 | 2017-11-12 21:28:53 +0000 | [diff] [blame] | 172 | repo_set_hash_algo(repo, format.hash_algo); |
| 173 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 174 | if (worktree) |
| 175 | repo_set_worktree(repo, worktree); |
| 176 | |
Martin Ågren | e8805af | 2019-02-28 21:36:28 +0100 | [diff] [blame] | 177 | clear_repository_format(&format); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 178 | return 0; |
| 179 | |
| 180 | error: |
| 181 | repo_clear(repo); |
| 182 | return -1; |
| 183 | } |
| 184 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 185 | int repo_submodule_init(struct repository *subrepo, |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 186 | struct repository *superproject, |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 187 | const struct submodule *sub) |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 188 | { |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 189 | struct strbuf gitdir = STRBUF_INIT; |
| 190 | struct strbuf worktree = STRBUF_INIT; |
| 191 | int ret = 0; |
| 192 | |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 193 | if (!sub) { |
| 194 | ret = -1; |
| 195 | goto out; |
| 196 | } |
| 197 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 198 | strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", sub->path); |
| 199 | strbuf_repo_worktree_path(&worktree, superproject, "%s", sub->path); |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 200 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 201 | if (repo_init(subrepo, gitdir.buf, worktree.buf)) { |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 202 | /* |
| 203 | * If initilization fails then it may be due to the submodule |
| 204 | * not being populated in the superproject's worktree. Instead |
| 205 | * we can try to initilize the submodule by finding it's gitdir |
| 206 | * in the superproject's 'modules' directory. In this case the |
| 207 | * submodule would not have a worktree. |
| 208 | */ |
| 209 | strbuf_reset(&gitdir); |
| 210 | strbuf_repo_git_path(&gitdir, superproject, |
| 211 | "modules/%s", sub->name); |
| 212 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 213 | if (repo_init(subrepo, gitdir.buf, NULL)) { |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 214 | ret = -1; |
| 215 | goto out; |
| 216 | } |
| 217 | } |
| 218 | |
Stefan Beller | d5498e0 | 2018-11-28 16:27:53 -0800 | [diff] [blame] | 219 | subrepo->submodule_prefix = xstrfmt("%s%s/", |
| 220 | superproject->submodule_prefix ? |
| 221 | superproject->submodule_prefix : |
| 222 | "", sub->path); |
Brandon Williams | 96dc883 | 2017-06-22 11:43:47 -0700 | [diff] [blame] | 223 | |
| 224 | out: |
| 225 | strbuf_release(&gitdir); |
| 226 | strbuf_release(&worktree); |
| 227 | return ret; |
| 228 | } |
| 229 | |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 230 | void repo_clear(struct repository *repo) |
| 231 | { |
René Scharfe | 90dd04a | 2017-10-01 16:44:46 +0200 | [diff] [blame] | 232 | FREE_AND_NULL(repo->gitdir); |
| 233 | FREE_AND_NULL(repo->commondir); |
René Scharfe | 90dd04a | 2017-10-01 16:44:46 +0200 | [diff] [blame] | 234 | FREE_AND_NULL(repo->graft_file); |
| 235 | FREE_AND_NULL(repo->index_file); |
| 236 | FREE_AND_NULL(repo->worktree); |
| 237 | FREE_AND_NULL(repo->submodule_prefix); |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 238 | |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 239 | raw_object_store_clear(repo->objects); |
| 240 | FREE_AND_NULL(repo->objects); |
| 241 | |
Stefan Beller | 99bf115 | 2018-05-08 12:37:24 -0700 | [diff] [blame] | 242 | parsed_object_pool_clear(repo->parsed_objects); |
| 243 | FREE_AND_NULL(repo->parsed_objects); |
| 244 | |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 245 | if (repo->config) { |
| 246 | git_configset_clear(repo->config); |
René Scharfe | 90dd04a | 2017-10-01 16:44:46 +0200 | [diff] [blame] | 247 | FREE_AND_NULL(repo->config); |
Brandon Williams | 3b25622 | 2017-06-22 11:43:42 -0700 | [diff] [blame] | 248 | } |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 249 | |
Brandon Williams | bf12fcd | 2017-06-22 11:43:44 -0700 | [diff] [blame] | 250 | if (repo->submodule_cache) { |
| 251 | submodule_cache_free(repo->submodule_cache); |
| 252 | repo->submodule_cache = NULL; |
| 253 | } |
| 254 | |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 255 | if (repo->index) { |
| 256 | discard_index(repo->index); |
Nguyễn Thái Ngọc Duy | 74373b5 | 2018-05-10 08:13:10 +0200 | [diff] [blame] | 257 | if (repo->index != &the_index) |
| 258 | FREE_AND_NULL(repo->index); |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | int repo_read_index(struct repository *repo) |
| 263 | { |
| 264 | if (!repo->index) |
| 265 | repo->index = xcalloc(1, sizeof(*repo->index)); |
Brandon Williams | 639e30b | 2017-06-22 11:43:43 -0700 | [diff] [blame] | 266 | |
Thomas Gummerer | a125a22 | 2018-01-07 22:30:13 +0000 | [diff] [blame] | 267 | return read_index_from(repo->index, repo->index_file, repo->gitdir); |
Brandon Williams | 359efef | 2017-06-22 11:43:32 -0700 | [diff] [blame] | 268 | } |
Nguyễn Thái Ngọc Duy | 3a95f31 | 2019-01-12 09:13:24 +0700 | [diff] [blame] | 269 | |
| 270 | int repo_hold_locked_index(struct repository *repo, |
| 271 | struct lock_file *lf, |
| 272 | int flags) |
| 273 | { |
| 274 | if (!repo->index_file) |
| 275 | BUG("the repo hasn't been setup"); |
| 276 | return hold_lock_file_for_update(lf, repo->index_file, flags); |
| 277 | } |