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