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