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
6 #include "git-compat-util.h"
8 #include "repository.h"
9 #include "object-store-ll.h"
14 #include "read-cache-ll.h"
18 #include "submodule-config.h"
19 #include "sparse-index.h"
21 #include "promisor-remote.h"
23 /* The main repository */
24 static struct repository the_repo
;
25 struct repository
*the_repository
;
26 struct index_state the_index
;
28 void initialize_the_repository(void)
30 the_repository
= &the_repo
;
32 the_repo
.index
= &the_index
;
33 the_repo
.objects
= raw_object_store_new();
34 the_repo
.remote_state
= remote_state_new();
35 the_repo
.parsed_objects
= parsed_object_pool_new();
37 index_state_init(&the_index
, the_repository
);
39 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
42 static void expand_base_dir(char **out
, const char *in
,
43 const char *base_dir
, const char *def_in
)
49 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
52 static void repo_set_commondir(struct repository
*repo
,
53 const char *commondir
)
55 struct strbuf sb
= STRBUF_INIT
;
57 free(repo
->commondir
);
60 repo
->different_commondir
= 1;
61 repo
->commondir
= xstrdup(commondir
);
65 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
66 repo
->commondir
= strbuf_detach(&sb
, NULL
);
69 void repo_set_gitdir(struct repository
*repo
,
71 const struct set_gitdir_args
*o
)
73 const char *gitfile
= read_gitfile(root
);
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.
79 char *old_gitdir
= repo
->gitdir
;
81 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
84 repo_set_commondir(repo
, o
->commondir
);
86 if (!repo
->objects
->odb
) {
87 CALLOC_ARRAY(repo
->objects
->odb
, 1);
88 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
90 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
91 repo
->commondir
, "objects");
93 repo
->objects
->odb
->disable_ref_updates
= o
->disable_ref_updates
;
95 free(repo
->objects
->alternate_db
);
96 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
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");
103 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
105 repo
->hash_algo
= &hash_algos
[hash_algo
];
108 void repo_set_compat_hash_algo(struct repository
*repo
, int algo
)
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
;
113 if (repo
->compat_hash_algo
)
114 repo_read_loose_object_map(repo
);
117 void repo_set_ref_storage_format(struct repository
*repo
, unsigned int format
)
119 repo
->ref_storage_format
= format
;
123 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
124 * Return 0 upon success and a non-zero value upon failure.
126 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
130 char *abspath
= NULL
;
131 const char *resolved_gitdir
;
132 struct set_gitdir_args args
= { NULL
};
134 abspath
= real_pathdup(gitdir
, 0);
140 /* 'gitdir' must reference the gitdir directly */
141 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
142 if (!resolved_gitdir
) {
147 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
154 void repo_set_worktree(struct repository
*repo
, const char *path
)
156 repo
->worktree
= real_pathdup(path
, 1);
158 trace2_def_repo(repo
);
161 static int read_and_verify_repository_format(struct repository_format
*format
,
162 const char *commondir
)
165 struct strbuf sb
= STRBUF_INIT
;
167 strbuf_addf(&sb
, "%s/config", commondir
);
168 read_repository_format(format
, sb
.buf
);
171 if (verify_repository_format(format
, &sb
) < 0) {
172 warning("%s", sb
.buf
);
181 * Initialize 'repo' based on the provided 'gitdir'.
182 * Return 0 upon success and a non-zero value upon failure.
184 int repo_init(struct repository
*repo
,
186 const char *worktree
)
188 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
189 memset(repo
, 0, sizeof(*repo
));
191 repo
->objects
= raw_object_store_new();
192 repo
->parsed_objects
= parsed_object_pool_new();
193 repo
->remote_state
= remote_state_new();
195 if (repo_init_gitdir(repo
, gitdir
))
198 if (read_and_verify_repository_format(&format
, repo
->commondir
))
201 repo_set_hash_algo(repo
, format
.hash_algo
);
202 repo_set_compat_hash_algo(repo
, format
.compat_hash_algo
);
203 repo_set_ref_storage_format(repo
, format
.ref_storage_format
);
204 repo
->repository_format_worktree_config
= format
.worktree_config
;
206 /* take ownership of format.partial_clone */
207 repo
->repository_format_partial_clone
= format
.partial_clone
;
208 format
.partial_clone
= NULL
;
211 repo_set_worktree(repo
, worktree
);
213 if (repo
->compat_hash_algo
)
214 repo_read_loose_object_map(repo
);
216 clear_repository_format(&format
);
224 int repo_submodule_init(struct repository
*subrepo
,
225 struct repository
*superproject
,
227 const struct object_id
*treeish_name
)
229 struct strbuf gitdir
= STRBUF_INIT
;
230 struct strbuf worktree
= STRBUF_INIT
;
233 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
234 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
236 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
238 * If initialization fails then it may be due to the submodule
239 * not being populated in the superproject's worktree. Instead
240 * we can try to initialize the submodule by finding it's gitdir
241 * in the superproject's 'modules' directory. In this case the
242 * submodule would not have a worktree.
244 const struct submodule
*sub
=
245 submodule_from_path(superproject
, treeish_name
, path
);
251 strbuf_reset(&gitdir
);
252 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
254 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
260 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
261 superproject
->submodule_prefix
?
262 superproject
->submodule_prefix
:
266 strbuf_release(&gitdir
);
267 strbuf_release(&worktree
);
271 static void repo_clear_path_cache(struct repo_path_cache
*cache
)
273 FREE_AND_NULL(cache
->squash_msg
);
274 FREE_AND_NULL(cache
->squash_msg
);
275 FREE_AND_NULL(cache
->merge_msg
);
276 FREE_AND_NULL(cache
->merge_rr
);
277 FREE_AND_NULL(cache
->merge_mode
);
278 FREE_AND_NULL(cache
->merge_head
);
279 FREE_AND_NULL(cache
->fetch_head
);
280 FREE_AND_NULL(cache
->shallow
);
283 void repo_clear(struct repository
*repo
)
285 FREE_AND_NULL(repo
->gitdir
);
286 FREE_AND_NULL(repo
->commondir
);
287 FREE_AND_NULL(repo
->graft_file
);
288 FREE_AND_NULL(repo
->index_file
);
289 FREE_AND_NULL(repo
->worktree
);
290 FREE_AND_NULL(repo
->submodule_prefix
);
292 raw_object_store_clear(repo
->objects
);
293 FREE_AND_NULL(repo
->objects
);
295 parsed_object_pool_clear(repo
->parsed_objects
);
296 FREE_AND_NULL(repo
->parsed_objects
);
298 FREE_AND_NULL(repo
->settings
.fsmonitor
);
301 git_configset_clear(repo
->config
);
302 FREE_AND_NULL(repo
->config
);
305 if (repo
->submodule_cache
) {
306 submodule_cache_free(repo
->submodule_cache
);
307 repo
->submodule_cache
= NULL
;
311 discard_index(repo
->index
);
312 if (repo
->index
!= &the_index
)
313 FREE_AND_NULL(repo
->index
);
316 if (repo
->promisor_remote_config
) {
317 promisor_remote_clear(repo
->promisor_remote_config
);
318 FREE_AND_NULL(repo
->promisor_remote_config
);
321 if (repo
->remote_state
) {
322 remote_state_clear(repo
->remote_state
);
323 FREE_AND_NULL(repo
->remote_state
);
326 repo_clear_path_cache(&repo
->cached_paths
);
329 int repo_read_index(struct repository
*repo
)
333 /* Complete the double-reference */
335 ALLOC_ARRAY(repo
->index
, 1);
336 index_state_init(repo
->index
, repo
);
337 } else if (repo
->index
->repo
!= repo
) {
338 BUG("repo's index should point back at itself");
341 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
343 prepare_repo_settings(repo
);
344 if (repo
->settings
.command_requires_full_index
)
345 ensure_full_index(repo
->index
);
348 * If sparse checkouts are in use, check whether paths with the
349 * SKIP_WORKTREE attribute are missing from the worktree; if not,
350 * clear that attribute for that path.
352 clear_skip_worktree_from_present_files(repo
->index
);
357 int repo_hold_locked_index(struct repository
*repo
,
358 struct lock_file
*lf
,
361 if (!repo
->index_file
)
362 BUG("the repo hasn't been setup");
363 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);