1 #include "git-compat-util.h"
3 #include "repository.h"
4 #include "object-store-ll.h"
9 #include "read-cache-ll.h"
13 #include "submodule-config.h"
14 #include "sparse-index.h"
16 #include "promisor-remote.h"
18 /* The main repository */
19 static struct repository the_repo
;
20 struct repository
*the_repository
= &the_repo
;
22 void initialize_repository(struct repository
*repo
)
24 repo
->objects
= raw_object_store_new();
25 repo
->remote_state
= remote_state_new();
26 repo
->parsed_objects
= parsed_object_pool_new();
27 ALLOC_ARRAY(repo
->index
, 1);
28 index_state_init(repo
->index
, repo
);
31 static void expand_base_dir(char **out
, const char *in
,
32 const char *base_dir
, const char *def_in
)
38 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
41 static void repo_set_commondir(struct repository
*repo
,
42 const char *commondir
)
44 struct strbuf sb
= STRBUF_INIT
;
46 free(repo
->commondir
);
49 repo
->different_commondir
= 1;
50 repo
->commondir
= xstrdup(commondir
);
54 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
55 repo
->commondir
= strbuf_detach(&sb
, NULL
);
58 void repo_set_gitdir(struct repository
*repo
,
60 const struct set_gitdir_args
*o
)
62 const char *gitfile
= read_gitfile(root
);
64 * repo->gitdir is saved because the caller could pass "root"
65 * that also points to repo->gitdir. We want to keep it alive
66 * until after xstrdup(root). Then we can free it.
68 char *old_gitdir
= repo
->gitdir
;
70 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
73 repo_set_commondir(repo
, o
->commondir
);
75 if (!repo
->objects
->odb
) {
76 CALLOC_ARRAY(repo
->objects
->odb
, 1);
77 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
79 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
80 repo
->commondir
, "objects");
82 repo
->objects
->odb
->disable_ref_updates
= o
->disable_ref_updates
;
84 free(repo
->objects
->alternate_db
);
85 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
86 expand_base_dir(&repo
->graft_file
, o
->graft_file
,
87 repo
->commondir
, "info/grafts");
88 expand_base_dir(&repo
->index_file
, o
->index_file
,
89 repo
->gitdir
, "index");
92 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
94 repo
->hash_algo
= &hash_algos
[hash_algo
];
97 void repo_set_compat_hash_algo(struct repository
*repo
, int algo
)
99 if (hash_algo_by_ptr(repo
->hash_algo
) == algo
)
100 BUG("hash_algo and compat_hash_algo match");
101 repo
->compat_hash_algo
= algo
? &hash_algos
[algo
] : NULL
;
102 if (repo
->compat_hash_algo
)
103 repo_read_loose_object_map(repo
);
106 void repo_set_ref_storage_format(struct repository
*repo
, unsigned int format
)
108 repo
->ref_storage_format
= format
;
112 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
113 * Return 0 upon success and a non-zero value upon failure.
115 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
119 char *abspath
= NULL
;
120 const char *resolved_gitdir
;
121 struct set_gitdir_args args
= { NULL
};
123 abspath
= real_pathdup(gitdir
, 0);
129 /* 'gitdir' must reference the gitdir directly */
130 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
131 if (!resolved_gitdir
) {
136 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
143 void repo_set_worktree(struct repository
*repo
, const char *path
)
145 repo
->worktree
= real_pathdup(path
, 1);
147 trace2_def_repo(repo
);
150 static int read_and_verify_repository_format(struct repository_format
*format
,
151 const char *commondir
)
154 struct strbuf sb
= STRBUF_INIT
;
156 strbuf_addf(&sb
, "%s/config", commondir
);
157 read_repository_format(format
, sb
.buf
);
160 if (verify_repository_format(format
, &sb
) < 0) {
161 warning("%s", sb
.buf
);
170 * Initialize 'repo' based on the provided 'gitdir'.
171 * Return 0 upon success and a non-zero value upon failure.
173 int repo_init(struct repository
*repo
,
175 const char *worktree
)
177 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
178 memset(repo
, 0, sizeof(*repo
));
180 initialize_repository(repo
);
182 if (repo_init_gitdir(repo
, gitdir
))
185 if (read_and_verify_repository_format(&format
, repo
->commondir
))
188 repo_set_hash_algo(repo
, format
.hash_algo
);
189 repo_set_compat_hash_algo(repo
, format
.compat_hash_algo
);
190 repo_set_ref_storage_format(repo
, format
.ref_storage_format
);
191 repo
->repository_format_worktree_config
= format
.worktree_config
;
193 /* take ownership of format.partial_clone */
194 repo
->repository_format_partial_clone
= format
.partial_clone
;
195 format
.partial_clone
= NULL
;
198 repo_set_worktree(repo
, worktree
);
200 if (repo
->compat_hash_algo
)
201 repo_read_loose_object_map(repo
);
203 clear_repository_format(&format
);
211 int repo_submodule_init(struct repository
*subrepo
,
212 struct repository
*superproject
,
214 const struct object_id
*treeish_name
)
216 struct strbuf gitdir
= STRBUF_INIT
;
217 struct strbuf worktree
= STRBUF_INIT
;
220 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
221 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
223 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
225 * If initialization fails then it may be due to the submodule
226 * not being populated in the superproject's worktree. Instead
227 * we can try to initialize the submodule by finding it's gitdir
228 * in the superproject's 'modules' directory. In this case the
229 * submodule would not have a worktree.
231 const struct submodule
*sub
=
232 submodule_from_path(superproject
, treeish_name
, path
);
238 strbuf_reset(&gitdir
);
239 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
241 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
247 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
248 superproject
->submodule_prefix
?
249 superproject
->submodule_prefix
:
253 strbuf_release(&gitdir
);
254 strbuf_release(&worktree
);
258 static void repo_clear_path_cache(struct repo_path_cache
*cache
)
260 FREE_AND_NULL(cache
->squash_msg
);
261 FREE_AND_NULL(cache
->squash_msg
);
262 FREE_AND_NULL(cache
->merge_msg
);
263 FREE_AND_NULL(cache
->merge_rr
);
264 FREE_AND_NULL(cache
->merge_mode
);
265 FREE_AND_NULL(cache
->merge_head
);
266 FREE_AND_NULL(cache
->fetch_head
);
267 FREE_AND_NULL(cache
->shallow
);
270 void repo_clear(struct repository
*repo
)
272 FREE_AND_NULL(repo
->gitdir
);
273 FREE_AND_NULL(repo
->commondir
);
274 FREE_AND_NULL(repo
->graft_file
);
275 FREE_AND_NULL(repo
->index_file
);
276 FREE_AND_NULL(repo
->worktree
);
277 FREE_AND_NULL(repo
->submodule_prefix
);
279 raw_object_store_clear(repo
->objects
);
280 FREE_AND_NULL(repo
->objects
);
282 parsed_object_pool_clear(repo
->parsed_objects
);
283 FREE_AND_NULL(repo
->parsed_objects
);
285 FREE_AND_NULL(repo
->settings
.fsmonitor
);
288 git_configset_clear(repo
->config
);
289 FREE_AND_NULL(repo
->config
);
292 if (repo
->submodule_cache
) {
293 submodule_cache_free(repo
->submodule_cache
);
294 repo
->submodule_cache
= NULL
;
298 discard_index(repo
->index
);
299 FREE_AND_NULL(repo
->index
);
302 if (repo
->promisor_remote_config
) {
303 promisor_remote_clear(repo
->promisor_remote_config
);
304 FREE_AND_NULL(repo
->promisor_remote_config
);
307 if (repo
->remote_state
) {
308 remote_state_clear(repo
->remote_state
);
309 FREE_AND_NULL(repo
->remote_state
);
312 repo_clear_path_cache(&repo
->cached_paths
);
315 int repo_read_index(struct repository
*repo
)
319 /* Complete the double-reference */
321 ALLOC_ARRAY(repo
->index
, 1);
322 index_state_init(repo
->index
, repo
);
323 } else if (repo
->index
->repo
!= repo
) {
324 BUG("repo's index should point back at itself");
327 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
329 prepare_repo_settings(repo
);
330 if (repo
->settings
.command_requires_full_index
)
331 ensure_full_index(repo
->index
);
334 * If sparse checkouts are in use, check whether paths with the
335 * SKIP_WORKTREE attribute are missing from the worktree; if not,
336 * clear that attribute for that path.
338 clear_skip_worktree_from_present_files(repo
->index
);
343 int repo_hold_locked_index(struct repository
*repo
,
344 struct lock_file
*lf
,
347 if (!repo
->index_file
)
348 BUG("the repo hasn't been setup");
349 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);