]>
Commit | Line | Data |
---|---|---|
f8adbec9 NTND |
1 | /* |
2 | * not really _using_ the compat macros, just make sure the_index | |
3 | * declaration matches the definition in this file. | |
4 | */ | |
666f53eb | 5 | #define USE_THE_INDEX_VARIABLE |
359efeff | 6 | #include "cache.h" |
0b027f6c | 7 | #include "abspath.h" |
359efeff | 8 | #include "repository.h" |
90c62155 | 9 | #include "object-store.h" |
3b256228 | 10 | #include "config.h" |
99bf115c | 11 | #include "object.h" |
3a95f31d | 12 | #include "lockfile.h" |
fd3cb050 | 13 | #include "remote.h" |
bf12fcdf | 14 | #include "submodule-config.h" |
3964fc2a | 15 | #include "sparse-index.h" |
ef7dc2e9 | 16 | #include "promisor-remote.h" |
359efeff BW |
17 | |
18 | /* The main repository */ | |
b2f0ecee NTND |
19 | static struct repository the_repo; |
20 | struct repository *the_repository; | |
f8adbec9 | 21 | struct index_state the_index; |
b2f0ecee NTND |
22 | |
23 | void initialize_the_repository(void) | |
24 | { | |
25 | the_repository = &the_repo; | |
26 | ||
27 | the_repo.index = &the_index; | |
90c62155 | 28 | the_repo.objects = raw_object_store_new(); |
fd3cb050 | 29 | the_repo.remote_state = remote_state_new(); |
99bf115c SB |
30 | the_repo.parsed_objects = parsed_object_pool_new(); |
31 | ||
6269f8ea ÆAB |
32 | index_state_init(&the_index, the_repository); |
33 | ||
b2f0ecee NTND |
34 | repo_set_hash_algo(&the_repo, GIT_HASH_SHA1); |
35 | } | |
359efeff | 36 | |
357a03eb NTND |
37 | static void expand_base_dir(char **out, const char *in, |
38 | const char *base_dir, const char *def_in) | |
39 | { | |
40 | free(*out); | |
41 | if (in) | |
42 | *out = xstrdup(in); | |
43 | else | |
44 | *out = xstrfmt("%s/%s", base_dir, def_in); | |
45 | } | |
46 | ||
47 | static void repo_set_commondir(struct repository *repo, | |
48 | const char *commondir) | |
359efeff BW |
49 | { |
50 | struct strbuf sb = STRBUF_INIT; | |
51 | ||
f9b7573f | 52 | free(repo->commondir); |
357a03eb NTND |
53 | |
54 | if (commondir) { | |
55 | repo->different_commondir = 1; | |
56 | repo->commondir = xstrdup(commondir); | |
57 | return; | |
58 | } | |
59 | ||
60 | repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir); | |
359efeff | 61 | repo->commondir = strbuf_detach(&sb, NULL); |
359efeff BW |
62 | } |
63 | ||
357a03eb NTND |
64 | void repo_set_gitdir(struct repository *repo, |
65 | const char *root, | |
66 | const struct set_gitdir_args *o) | |
359efeff | 67 | { |
357a03eb NTND |
68 | const char *gitfile = read_gitfile(root); |
69 | /* | |
70 | * repo->gitdir is saved because the caller could pass "root" | |
71 | * that also points to repo->gitdir. We want to keep it alive | |
72 | * until after xstrdup(root). Then we can free it. | |
73 | */ | |
1fb2b636 | 74 | char *old_gitdir = repo->gitdir; |
359efeff | 75 | |
357a03eb | 76 | repo->gitdir = xstrdup(gitfile ? gitfile : root); |
1fb2b636 | 77 | free(old_gitdir); |
357a03eb NTND |
78 | |
79 | repo_set_commondir(repo, o->commondir); | |
f0eaf638 JK |
80 | |
81 | if (!repo->objects->odb) { | |
ca56dadb | 82 | CALLOC_ARRAY(repo->objects->odb, 1); |
f0eaf638 JK |
83 | repo->objects->odb_tail = &repo->objects->odb->next; |
84 | } | |
85 | expand_base_dir(&repo->objects->odb->path, o->object_dir, | |
357a03eb | 86 | repo->commondir, "objects"); |
f0eaf638 | 87 | |
ecd81dfc NS |
88 | repo->objects->odb->disable_ref_updates = o->disable_ref_updates; |
89 | ||
90c62155 SB |
90 | free(repo->objects->alternate_db); |
91 | repo->objects->alternate_db = xstrdup_or_null(o->alternate_db); | |
357a03eb NTND |
92 | expand_base_dir(&repo->graft_file, o->graft_file, |
93 | repo->commondir, "info/grafts"); | |
94 | expand_base_dir(&repo->index_file, o->index_file, | |
95 | repo->gitdir, "index"); | |
359efeff BW |
96 | } |
97 | ||
78a67668 | 98 | void repo_set_hash_algo(struct repository *repo, int hash_algo) |
99 | { | |
100 | repo->hash_algo = &hash_algos[hash_algo]; | |
101 | } | |
102 | ||
359efeff BW |
103 | /* |
104 | * Attempt to resolve and set the provided 'gitdir' for repository 'repo'. | |
105 | * Return 0 upon success and a non-zero value upon failure. | |
106 | */ | |
107 | static int repo_init_gitdir(struct repository *repo, const char *gitdir) | |
108 | { | |
109 | int ret = 0; | |
110 | int error = 0; | |
111 | char *abspath = NULL; | |
112 | const char *resolved_gitdir; | |
357a03eb | 113 | struct set_gitdir_args args = { NULL }; |
359efeff BW |
114 | |
115 | abspath = real_pathdup(gitdir, 0); | |
116 | if (!abspath) { | |
117 | ret = -1; | |
118 | goto out; | |
119 | } | |
120 | ||
121 | /* 'gitdir' must reference the gitdir directly */ | |
122 | resolved_gitdir = resolve_gitdir_gently(abspath, &error); | |
123 | if (!resolved_gitdir) { | |
124 | ret = -1; | |
125 | goto out; | |
126 | } | |
127 | ||
357a03eb | 128 | repo_set_gitdir(repo, resolved_gitdir, &args); |
359efeff BW |
129 | |
130 | out: | |
131 | free(abspath); | |
132 | return ret; | |
133 | } | |
134 | ||
135 | void repo_set_worktree(struct repository *repo, const char *path) | |
136 | { | |
137 | repo->worktree = real_pathdup(path, 1); | |
ee4512ed JH |
138 | |
139 | trace2_def_repo(repo); | |
359efeff BW |
140 | } |
141 | ||
142 | static int read_and_verify_repository_format(struct repository_format *format, | |
143 | const char *commondir) | |
144 | { | |
145 | int ret = 0; | |
146 | struct strbuf sb = STRBUF_INIT; | |
147 | ||
148 | strbuf_addf(&sb, "%s/config", commondir); | |
149 | read_repository_format(format, sb.buf); | |
150 | strbuf_reset(&sb); | |
151 | ||
152 | if (verify_repository_format(format, &sb) < 0) { | |
153 | warning("%s", sb.buf); | |
154 | ret = -1; | |
155 | } | |
156 | ||
157 | strbuf_release(&sb); | |
158 | return ret; | |
159 | } | |
160 | ||
161 | /* | |
162 | * Initialize 'repo' based on the provided 'gitdir'. | |
163 | * Return 0 upon success and a non-zero value upon failure. | |
164 | */ | |
da62f786 SB |
165 | int repo_init(struct repository *repo, |
166 | const char *gitdir, | |
167 | const char *worktree) | |
359efeff | 168 | { |
e8805af1 | 169 | struct repository_format format = REPOSITORY_FORMAT_INIT; |
359efeff BW |
170 | memset(repo, 0, sizeof(*repo)); |
171 | ||
90c62155 | 172 | repo->objects = raw_object_store_new(); |
99bf115c | 173 | repo->parsed_objects = parsed_object_pool_new(); |
fd3cb050 | 174 | repo->remote_state = remote_state_new(); |
90c62155 | 175 | |
359efeff BW |
176 | if (repo_init_gitdir(repo, gitdir)) |
177 | goto error; | |
178 | ||
179 | if (read_and_verify_repository_format(&format, repo->commondir)) | |
180 | goto error; | |
181 | ||
78a67668 | 182 | repo_set_hash_algo(repo, format.hash_algo); |
183 | ||
ebaf3bcf JT |
184 | /* take ownership of format.partial_clone */ |
185 | repo->repository_format_partial_clone = format.partial_clone; | |
186 | format.partial_clone = NULL; | |
187 | ||
359efeff BW |
188 | if (worktree) |
189 | repo_set_worktree(repo, worktree); | |
190 | ||
e8805af1 | 191 | clear_repository_format(&format); |
359efeff BW |
192 | return 0; |
193 | ||
194 | error: | |
195 | repo_clear(repo); | |
196 | return -1; | |
197 | } | |
198 | ||
d5498e08 | 199 | int repo_submodule_init(struct repository *subrepo, |
96dc883b | 200 | struct repository *superproject, |
8eb8dcf9 JT |
201 | const char *path, |
202 | const struct object_id *treeish_name) | |
96dc883b | 203 | { |
96dc883b BW |
204 | struct strbuf gitdir = STRBUF_INIT; |
205 | struct strbuf worktree = STRBUF_INIT; | |
206 | int ret = 0; | |
207 | ||
8eb8dcf9 JT |
208 | strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path); |
209 | strbuf_repo_worktree_path(&worktree, superproject, "%s", path); | |
96dc883b | 210 | |
d5498e08 | 211 | if (repo_init(subrepo, gitdir.buf, worktree.buf)) { |
96dc883b | 212 | /* |
15beaaa3 | 213 | * If initialization fails then it may be due to the submodule |
96dc883b | 214 | * not being populated in the superproject's worktree. Instead |
15beaaa3 | 215 | * we can try to initialize the submodule by finding it's gitdir |
96dc883b BW |
216 | * in the superproject's 'modules' directory. In this case the |
217 | * submodule would not have a worktree. | |
218 | */ | |
8eb8dcf9 JT |
219 | const struct submodule *sub = |
220 | submodule_from_path(superproject, treeish_name, path); | |
221 | if (!sub) { | |
222 | ret = -1; | |
223 | goto out; | |
224 | } | |
225 | ||
96dc883b | 226 | strbuf_reset(&gitdir); |
ce125d43 | 227 | submodule_name_to_gitdir(&gitdir, superproject, sub->name); |
96dc883b | 228 | |
d5498e08 | 229 | if (repo_init(subrepo, gitdir.buf, NULL)) { |
96dc883b BW |
230 | ret = -1; |
231 | goto out; | |
232 | } | |
233 | } | |
234 | ||
d5498e08 SB |
235 | subrepo->submodule_prefix = xstrfmt("%s%s/", |
236 | superproject->submodule_prefix ? | |
237 | superproject->submodule_prefix : | |
8eb8dcf9 | 238 | "", path); |
96dc883b BW |
239 | |
240 | out: | |
241 | strbuf_release(&gitdir); | |
242 | strbuf_release(&worktree); | |
243 | return ret; | |
244 | } | |
245 | ||
759f3407 ÆAB |
246 | static void repo_clear_path_cache(struct repo_path_cache *cache) |
247 | { | |
248 | FREE_AND_NULL(cache->squash_msg); | |
249 | FREE_AND_NULL(cache->squash_msg); | |
250 | FREE_AND_NULL(cache->merge_msg); | |
251 | FREE_AND_NULL(cache->merge_rr); | |
252 | FREE_AND_NULL(cache->merge_mode); | |
253 | FREE_AND_NULL(cache->merge_head); | |
254 | FREE_AND_NULL(cache->merge_autostash); | |
255 | FREE_AND_NULL(cache->auto_merge); | |
256 | FREE_AND_NULL(cache->fetch_head); | |
257 | FREE_AND_NULL(cache->shallow); | |
258 | } | |
259 | ||
359efeff BW |
260 | void repo_clear(struct repository *repo) |
261 | { | |
90dd04aa RS |
262 | FREE_AND_NULL(repo->gitdir); |
263 | FREE_AND_NULL(repo->commondir); | |
90dd04aa RS |
264 | FREE_AND_NULL(repo->graft_file); |
265 | FREE_AND_NULL(repo->index_file); | |
266 | FREE_AND_NULL(repo->worktree); | |
267 | FREE_AND_NULL(repo->submodule_prefix); | |
3b256228 | 268 | |
90c62155 SB |
269 | raw_object_store_clear(repo->objects); |
270 | FREE_AND_NULL(repo->objects); | |
271 | ||
99bf115c SB |
272 | parsed_object_pool_clear(repo->parsed_objects); |
273 | FREE_AND_NULL(repo->parsed_objects); | |
274 | ||
3b256228 BW |
275 | if (repo->config) { |
276 | git_configset_clear(repo->config); | |
90dd04aa | 277 | FREE_AND_NULL(repo->config); |
3b256228 | 278 | } |
639e30b5 | 279 | |
bf12fcdf BW |
280 | if (repo->submodule_cache) { |
281 | submodule_cache_free(repo->submodule_cache); | |
282 | repo->submodule_cache = NULL; | |
283 | } | |
284 | ||
639e30b5 BW |
285 | if (repo->index) { |
286 | discard_index(repo->index); | |
74373b5f NTND |
287 | if (repo->index != &the_index) |
288 | FREE_AND_NULL(repo->index); | |
639e30b5 | 289 | } |
ef7dc2e9 JT |
290 | |
291 | if (repo->promisor_remote_config) { | |
292 | promisor_remote_clear(repo->promisor_remote_config); | |
293 | FREE_AND_NULL(repo->promisor_remote_config); | |
294 | } | |
fd3cb050 GC |
295 | |
296 | if (repo->remote_state) { | |
297 | remote_state_clear(repo->remote_state); | |
298 | FREE_AND_NULL(repo->remote_state); | |
299 | } | |
759f3407 ÆAB |
300 | |
301 | repo_clear_path_cache(&repo->cached_paths); | |
639e30b5 BW |
302 | } |
303 | ||
304 | int repo_read_index(struct repository *repo) | |
305 | { | |
3964fc2a DS |
306 | int res; |
307 | ||
6269f8ea | 308 | /* Complete the double-reference */ |
2f6b1eb7 ÆAB |
309 | if (!repo->index) { |
310 | ALLOC_ARRAY(repo->index, 1); | |
6269f8ea ÆAB |
311 | index_state_init(repo->index, repo); |
312 | } else if (repo->index->repo != repo) { | |
1fd9ae51 | 313 | BUG("repo's index should point back at itself"); |
6269f8ea | 314 | } |
1fd9ae51 | 315 | |
3964fc2a DS |
316 | res = read_index_from(repo->index, repo->index_file, repo->gitdir); |
317 | ||
318 | prepare_repo_settings(repo); | |
319 | if (repo->settings.command_requires_full_index) | |
320 | ensure_full_index(repo->index); | |
321 | ||
af6a5187 EN |
322 | /* |
323 | * If sparse checkouts are in use, check whether paths with the | |
324 | * SKIP_WORKTREE attribute are missing from the worktree; if not, | |
325 | * clear that attribute for that path. | |
326 | */ | |
327 | clear_skip_worktree_from_present_files(repo->index); | |
328 | ||
3964fc2a | 329 | return res; |
359efeff | 330 | } |
3a95f31d NTND |
331 | |
332 | int repo_hold_locked_index(struct repository *repo, | |
333 | struct lock_file *lf, | |
334 | int flags) | |
335 | { | |
336 | if (!repo->index_file) | |
337 | BUG("the repo hasn't been setup"); | |
338 | return hold_lock_file_for_update(lf, repo->index_file, flags); | |
339 | } |