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