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