2 * not really _using_ the compat macros, just make sure the_index
3 * declaration matches the definition in this file.
5 #define USE_THE_INDEX_VARIABLE
8 #include "repository.h"
9 #include "object-store.h"
15 #include "submodule-config.h"
16 #include "sparse-index.h"
18 #include "promisor-remote.h"
20 /* The main repository */
21 static struct repository the_repo
;
22 struct repository
*the_repository
;
23 struct index_state the_index
;
25 void initialize_the_repository(void)
27 the_repository
= &the_repo
;
29 the_repo
.index
= &the_index
;
30 the_repo
.objects
= raw_object_store_new();
31 the_repo
.remote_state
= remote_state_new();
32 the_repo
.parsed_objects
= parsed_object_pool_new();
34 index_state_init(&the_index
, the_repository
);
36 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
39 static void expand_base_dir(char **out
, const char *in
,
40 const char *base_dir
, const char *def_in
)
46 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
49 static void repo_set_commondir(struct repository
*repo
,
50 const char *commondir
)
52 struct strbuf sb
= STRBUF_INIT
;
54 free(repo
->commondir
);
57 repo
->different_commondir
= 1;
58 repo
->commondir
= xstrdup(commondir
);
62 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
63 repo
->commondir
= strbuf_detach(&sb
, NULL
);
66 void repo_set_gitdir(struct repository
*repo
,
68 const struct set_gitdir_args
*o
)
70 const char *gitfile
= read_gitfile(root
);
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.
76 char *old_gitdir
= repo
->gitdir
;
78 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
81 repo_set_commondir(repo
, o
->commondir
);
83 if (!repo
->objects
->odb
) {
84 CALLOC_ARRAY(repo
->objects
->odb
, 1);
85 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
87 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
88 repo
->commondir
, "objects");
90 repo
->objects
->odb
->disable_ref_updates
= o
->disable_ref_updates
;
92 free(repo
->objects
->alternate_db
);
93 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
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");
100 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
102 repo
->hash_algo
= &hash_algos
[hash_algo
];
106 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
107 * Return 0 upon success and a non-zero value upon failure.
109 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
113 char *abspath
= NULL
;
114 const char *resolved_gitdir
;
115 struct set_gitdir_args args
= { NULL
};
117 abspath
= real_pathdup(gitdir
, 0);
123 /* 'gitdir' must reference the gitdir directly */
124 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
125 if (!resolved_gitdir
) {
130 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
137 void repo_set_worktree(struct repository
*repo
, const char *path
)
139 repo
->worktree
= real_pathdup(path
, 1);
141 trace2_def_repo(repo
);
144 static int read_and_verify_repository_format(struct repository_format
*format
,
145 const char *commondir
)
148 struct strbuf sb
= STRBUF_INIT
;
150 strbuf_addf(&sb
, "%s/config", commondir
);
151 read_repository_format(format
, sb
.buf
);
154 if (verify_repository_format(format
, &sb
) < 0) {
155 warning("%s", sb
.buf
);
164 * Initialize 'repo' based on the provided 'gitdir'.
165 * Return 0 upon success and a non-zero value upon failure.
167 int repo_init(struct repository
*repo
,
169 const char *worktree
)
171 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
172 memset(repo
, 0, sizeof(*repo
));
174 repo
->objects
= raw_object_store_new();
175 repo
->parsed_objects
= parsed_object_pool_new();
176 repo
->remote_state
= remote_state_new();
178 if (repo_init_gitdir(repo
, gitdir
))
181 if (read_and_verify_repository_format(&format
, repo
->commondir
))
184 repo_set_hash_algo(repo
, format
.hash_algo
);
185 repo
->repository_format_worktree_config
= format
.worktree_config
;
187 /* take ownership of format.partial_clone */
188 repo
->repository_format_partial_clone
= format
.partial_clone
;
189 format
.partial_clone
= NULL
;
192 repo_set_worktree(repo
, worktree
);
194 clear_repository_format(&format
);
202 int repo_submodule_init(struct repository
*subrepo
,
203 struct repository
*superproject
,
205 const struct object_id
*treeish_name
)
207 struct strbuf gitdir
= STRBUF_INIT
;
208 struct strbuf worktree
= STRBUF_INIT
;
211 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
212 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
214 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
216 * If initialization fails then it may be due to the submodule
217 * not being populated in the superproject's worktree. Instead
218 * we can try to initialize the submodule by finding it's gitdir
219 * in the superproject's 'modules' directory. In this case the
220 * submodule would not have a worktree.
222 const struct submodule
*sub
=
223 submodule_from_path(superproject
, treeish_name
, path
);
229 strbuf_reset(&gitdir
);
230 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
232 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
238 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
239 superproject
->submodule_prefix
?
240 superproject
->submodule_prefix
:
244 strbuf_release(&gitdir
);
245 strbuf_release(&worktree
);
249 static void repo_clear_path_cache(struct repo_path_cache
*cache
)
251 FREE_AND_NULL(cache
->squash_msg
);
252 FREE_AND_NULL(cache
->squash_msg
);
253 FREE_AND_NULL(cache
->merge_msg
);
254 FREE_AND_NULL(cache
->merge_rr
);
255 FREE_AND_NULL(cache
->merge_mode
);
256 FREE_AND_NULL(cache
->merge_head
);
257 FREE_AND_NULL(cache
->merge_autostash
);
258 FREE_AND_NULL(cache
->auto_merge
);
259 FREE_AND_NULL(cache
->fetch_head
);
260 FREE_AND_NULL(cache
->shallow
);
263 void repo_clear(struct repository
*repo
)
265 FREE_AND_NULL(repo
->gitdir
);
266 FREE_AND_NULL(repo
->commondir
);
267 FREE_AND_NULL(repo
->graft_file
);
268 FREE_AND_NULL(repo
->index_file
);
269 FREE_AND_NULL(repo
->worktree
);
270 FREE_AND_NULL(repo
->submodule_prefix
);
272 raw_object_store_clear(repo
->objects
);
273 FREE_AND_NULL(repo
->objects
);
275 parsed_object_pool_clear(repo
->parsed_objects
);
276 FREE_AND_NULL(repo
->parsed_objects
);
279 git_configset_clear(repo
->config
);
280 FREE_AND_NULL(repo
->config
);
283 if (repo
->submodule_cache
) {
284 submodule_cache_free(repo
->submodule_cache
);
285 repo
->submodule_cache
= NULL
;
289 discard_index(repo
->index
);
290 if (repo
->index
!= &the_index
)
291 FREE_AND_NULL(repo
->index
);
294 if (repo
->promisor_remote_config
) {
295 promisor_remote_clear(repo
->promisor_remote_config
);
296 FREE_AND_NULL(repo
->promisor_remote_config
);
299 if (repo
->remote_state
) {
300 remote_state_clear(repo
->remote_state
);
301 FREE_AND_NULL(repo
->remote_state
);
304 repo_clear_path_cache(&repo
->cached_paths
);
307 int repo_read_index(struct repository
*repo
)
311 /* Complete the double-reference */
313 ALLOC_ARRAY(repo
->index
, 1);
314 index_state_init(repo
->index
, repo
);
315 } else if (repo
->index
->repo
!= repo
) {
316 BUG("repo's index should point back at itself");
319 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
321 prepare_repo_settings(repo
);
322 if (repo
->settings
.command_requires_full_index
)
323 ensure_full_index(repo
->index
);
326 * If sparse checkouts are in use, check whether paths with the
327 * SKIP_WORKTREE attribute are missing from the worktree; if not,
328 * clear that attribute for that path.
330 clear_skip_worktree_from_present_files(repo
->index
);
335 int repo_hold_locked_index(struct repository
*repo
,
336 struct lock_file
*lf
,
339 if (!repo
->index_file
)
340 BUG("the repo hasn't been setup");
341 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);