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
;
22 static 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 void initialize_the_repository(void)
33 the_repository
= &the_repo
;
34 initialize_repository(the_repository
);
35 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
38 static void expand_base_dir(char **out
, const char *in
,
39 const char *base_dir
, const char *def_in
)
45 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
48 static void repo_set_commondir(struct repository
*repo
,
49 const char *commondir
)
51 struct strbuf sb
= STRBUF_INIT
;
53 free(repo
->commondir
);
56 repo
->different_commondir
= 1;
57 repo
->commondir
= xstrdup(commondir
);
61 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
62 repo
->commondir
= strbuf_detach(&sb
, NULL
);
65 void repo_set_gitdir(struct repository
*repo
,
67 const struct set_gitdir_args
*o
)
69 const char *gitfile
= read_gitfile(root
);
71 * repo->gitdir is saved because the caller could pass "root"
72 * that also points to repo->gitdir. We want to keep it alive
73 * until after xstrdup(root). Then we can free it.
75 char *old_gitdir
= repo
->gitdir
;
77 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
80 repo_set_commondir(repo
, o
->commondir
);
82 if (!repo
->objects
->odb
) {
83 CALLOC_ARRAY(repo
->objects
->odb
, 1);
84 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
86 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
87 repo
->commondir
, "objects");
89 repo
->objects
->odb
->disable_ref_updates
= o
->disable_ref_updates
;
91 free(repo
->objects
->alternate_db
);
92 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
93 expand_base_dir(&repo
->graft_file
, o
->graft_file
,
94 repo
->commondir
, "info/grafts");
95 expand_base_dir(&repo
->index_file
, o
->index_file
,
96 repo
->gitdir
, "index");
99 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
101 repo
->hash_algo
= &hash_algos
[hash_algo
];
104 void repo_set_compat_hash_algo(struct repository
*repo
, int algo
)
106 if (hash_algo_by_ptr(repo
->hash_algo
) == algo
)
107 BUG("hash_algo and compat_hash_algo match");
108 repo
->compat_hash_algo
= algo
? &hash_algos
[algo
] : NULL
;
109 if (repo
->compat_hash_algo
)
110 repo_read_loose_object_map(repo
);
113 void repo_set_ref_storage_format(struct repository
*repo
, unsigned int format
)
115 repo
->ref_storage_format
= format
;
119 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
120 * Return 0 upon success and a non-zero value upon failure.
122 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
126 char *abspath
= NULL
;
127 const char *resolved_gitdir
;
128 struct set_gitdir_args args
= { NULL
};
130 abspath
= real_pathdup(gitdir
, 0);
136 /* 'gitdir' must reference the gitdir directly */
137 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
138 if (!resolved_gitdir
) {
143 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
150 void repo_set_worktree(struct repository
*repo
, const char *path
)
152 repo
->worktree
= real_pathdup(path
, 1);
154 trace2_def_repo(repo
);
157 static int read_and_verify_repository_format(struct repository_format
*format
,
158 const char *commondir
)
161 struct strbuf sb
= STRBUF_INIT
;
163 strbuf_addf(&sb
, "%s/config", commondir
);
164 read_repository_format(format
, sb
.buf
);
167 if (verify_repository_format(format
, &sb
) < 0) {
168 warning("%s", sb
.buf
);
177 * Initialize 'repo' based on the provided 'gitdir'.
178 * Return 0 upon success and a non-zero value upon failure.
180 int repo_init(struct repository
*repo
,
182 const char *worktree
)
184 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
185 memset(repo
, 0, sizeof(*repo
));
187 initialize_repository(repo
);
189 if (repo_init_gitdir(repo
, gitdir
))
192 if (read_and_verify_repository_format(&format
, repo
->commondir
))
195 repo_set_hash_algo(repo
, format
.hash_algo
);
196 repo_set_compat_hash_algo(repo
, format
.compat_hash_algo
);
197 repo_set_ref_storage_format(repo
, format
.ref_storage_format
);
198 repo
->repository_format_worktree_config
= format
.worktree_config
;
200 /* take ownership of format.partial_clone */
201 repo
->repository_format_partial_clone
= format
.partial_clone
;
202 format
.partial_clone
= NULL
;
205 repo_set_worktree(repo
, worktree
);
207 if (repo
->compat_hash_algo
)
208 repo_read_loose_object_map(repo
);
210 clear_repository_format(&format
);
218 int repo_submodule_init(struct repository
*subrepo
,
219 struct repository
*superproject
,
221 const struct object_id
*treeish_name
)
223 struct strbuf gitdir
= STRBUF_INIT
;
224 struct strbuf worktree
= STRBUF_INIT
;
227 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
228 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
230 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
232 * If initialization fails then it may be due to the submodule
233 * not being populated in the superproject's worktree. Instead
234 * we can try to initialize the submodule by finding it's gitdir
235 * in the superproject's 'modules' directory. In this case the
236 * submodule would not have a worktree.
238 const struct submodule
*sub
=
239 submodule_from_path(superproject
, treeish_name
, path
);
245 strbuf_reset(&gitdir
);
246 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
248 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
254 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
255 superproject
->submodule_prefix
?
256 superproject
->submodule_prefix
:
260 strbuf_release(&gitdir
);
261 strbuf_release(&worktree
);
265 static void repo_clear_path_cache(struct repo_path_cache
*cache
)
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
->fetch_head
);
274 FREE_AND_NULL(cache
->shallow
);
277 void repo_clear(struct repository
*repo
)
279 FREE_AND_NULL(repo
->gitdir
);
280 FREE_AND_NULL(repo
->commondir
);
281 FREE_AND_NULL(repo
->graft_file
);
282 FREE_AND_NULL(repo
->index_file
);
283 FREE_AND_NULL(repo
->worktree
);
284 FREE_AND_NULL(repo
->submodule_prefix
);
286 raw_object_store_clear(repo
->objects
);
287 FREE_AND_NULL(repo
->objects
);
289 parsed_object_pool_clear(repo
->parsed_objects
);
290 FREE_AND_NULL(repo
->parsed_objects
);
293 git_configset_clear(repo
->config
);
294 FREE_AND_NULL(repo
->config
);
297 if (repo
->submodule_cache
) {
298 submodule_cache_free(repo
->submodule_cache
);
299 repo
->submodule_cache
= NULL
;
303 discard_index(repo
->index
);
304 FREE_AND_NULL(repo
->index
);
307 if (repo
->promisor_remote_config
) {
308 promisor_remote_clear(repo
->promisor_remote_config
);
309 FREE_AND_NULL(repo
->promisor_remote_config
);
312 if (repo
->remote_state
) {
313 remote_state_clear(repo
->remote_state
);
314 FREE_AND_NULL(repo
->remote_state
);
317 repo_clear_path_cache(&repo
->cached_paths
);
320 int repo_read_index(struct repository
*repo
)
324 /* Complete the double-reference */
326 ALLOC_ARRAY(repo
->index
, 1);
327 index_state_init(repo
->index
, repo
);
328 } else if (repo
->index
->repo
!= repo
) {
329 BUG("repo's index should point back at itself");
332 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
334 prepare_repo_settings(repo
);
335 if (repo
->settings
.command_requires_full_index
)
336 ensure_full_index(repo
->index
);
339 * If sparse checkouts are in use, check whether paths with the
340 * SKIP_WORKTREE attribute are missing from the worktree; if not,
341 * clear that attribute for that path.
343 clear_skip_worktree_from_present_files(repo
->index
);
348 int repo_hold_locked_index(struct repository
*repo
,
349 struct lock_file
*lf
,
352 if (!repo
->index_file
)
353 BUG("the repo hasn't been setup");
354 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);