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