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_COMPATIBILITY_MACROS
7 #include "repository.h"
8 #include "object-store.h"
13 #include "submodule-config.h"
14 #include "sparse-index.h"
15 #include "promisor-remote.h"
17 /* The main repository */
18 static struct repository the_repo
;
19 struct repository
*the_repository
;
20 struct index_state the_index
;
22 void initialize_the_repository(void)
24 the_repository
= &the_repo
;
26 the_repo
.index
= &the_index
;
27 the_repo
.objects
= raw_object_store_new();
28 the_repo
.remote_state
= remote_state_new();
29 the_repo
.parsed_objects
= parsed_object_pool_new();
31 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
34 static void expand_base_dir(char **out
, const char *in
,
35 const char *base_dir
, const char *def_in
)
41 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
44 static void repo_set_commondir(struct repository
*repo
,
45 const char *commondir
)
47 struct strbuf sb
= STRBUF_INIT
;
49 free(repo
->commondir
);
52 repo
->different_commondir
= 1;
53 repo
->commondir
= xstrdup(commondir
);
57 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
58 repo
->commondir
= strbuf_detach(&sb
, NULL
);
61 void repo_set_gitdir(struct repository
*repo
,
63 const struct set_gitdir_args
*o
)
65 const char *gitfile
= read_gitfile(root
);
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.
71 char *old_gitdir
= repo
->gitdir
;
73 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
76 repo_set_commondir(repo
, o
->commondir
);
78 if (!repo
->objects
->odb
) {
79 CALLOC_ARRAY(repo
->objects
->odb
, 1);
80 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
82 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
83 repo
->commondir
, "objects");
85 repo
->objects
->odb
->disable_ref_updates
= o
->disable_ref_updates
;
87 free(repo
->objects
->alternate_db
);
88 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
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");
95 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
97 repo
->hash_algo
= &hash_algos
[hash_algo
];
101 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
102 * Return 0 upon success and a non-zero value upon failure.
104 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
108 char *abspath
= NULL
;
109 const char *resolved_gitdir
;
110 struct set_gitdir_args args
= { NULL
};
112 abspath
= real_pathdup(gitdir
, 0);
118 /* 'gitdir' must reference the gitdir directly */
119 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
120 if (!resolved_gitdir
) {
125 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
132 void repo_set_worktree(struct repository
*repo
, const char *path
)
134 repo
->worktree
= real_pathdup(path
, 1);
136 trace2_def_repo(repo
);
139 static int read_and_verify_repository_format(struct repository_format
*format
,
140 const char *commondir
)
143 struct strbuf sb
= STRBUF_INIT
;
145 strbuf_addf(&sb
, "%s/config", commondir
);
146 read_repository_format(format
, sb
.buf
);
149 if (verify_repository_format(format
, &sb
) < 0) {
150 warning("%s", sb
.buf
);
159 * Initialize 'repo' based on the provided 'gitdir'.
160 * Return 0 upon success and a non-zero value upon failure.
162 int repo_init(struct repository
*repo
,
164 const char *worktree
)
166 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
167 memset(repo
, 0, sizeof(*repo
));
169 repo
->objects
= raw_object_store_new();
170 repo
->parsed_objects
= parsed_object_pool_new();
171 repo
->remote_state
= remote_state_new();
173 if (repo_init_gitdir(repo
, gitdir
))
176 if (read_and_verify_repository_format(&format
, repo
->commondir
))
179 repo_set_hash_algo(repo
, format
.hash_algo
);
181 /* take ownership of format.partial_clone */
182 repo
->repository_format_partial_clone
= format
.partial_clone
;
183 format
.partial_clone
= NULL
;
186 repo_set_worktree(repo
, worktree
);
188 clear_repository_format(&format
);
196 int repo_submodule_init(struct repository
*subrepo
,
197 struct repository
*superproject
,
199 const struct object_id
*treeish_name
)
201 struct strbuf gitdir
= STRBUF_INIT
;
202 struct strbuf worktree
= STRBUF_INIT
;
205 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
206 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
208 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
210 * If initialization fails then it may be due to the submodule
211 * not being populated in the superproject's worktree. Instead
212 * we can try to initialize the submodule by finding it's gitdir
213 * in the superproject's 'modules' directory. In this case the
214 * submodule would not have a worktree.
216 const struct submodule
*sub
=
217 submodule_from_path(superproject
, treeish_name
, path
);
223 strbuf_reset(&gitdir
);
224 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
226 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
232 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
233 superproject
->submodule_prefix
?
234 superproject
->submodule_prefix
:
238 strbuf_release(&gitdir
);
239 strbuf_release(&worktree
);
243 void repo_clear(struct repository
*repo
)
245 FREE_AND_NULL(repo
->gitdir
);
246 FREE_AND_NULL(repo
->commondir
);
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
);
252 raw_object_store_clear(repo
->objects
);
253 FREE_AND_NULL(repo
->objects
);
255 parsed_object_pool_clear(repo
->parsed_objects
);
256 FREE_AND_NULL(repo
->parsed_objects
);
259 git_configset_clear(repo
->config
);
260 FREE_AND_NULL(repo
->config
);
263 if (repo
->submodule_cache
) {
264 submodule_cache_free(repo
->submodule_cache
);
265 repo
->submodule_cache
= NULL
;
269 discard_index(repo
->index
);
270 if (repo
->index
!= &the_index
)
271 FREE_AND_NULL(repo
->index
);
274 if (repo
->promisor_remote_config
) {
275 promisor_remote_clear(repo
->promisor_remote_config
);
276 FREE_AND_NULL(repo
->promisor_remote_config
);
279 if (repo
->remote_state
) {
280 remote_state_clear(repo
->remote_state
);
281 FREE_AND_NULL(repo
->remote_state
);
285 int repo_read_index(struct repository
*repo
)
290 CALLOC_ARRAY(repo
->index
, 1);
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");
298 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
300 prepare_repo_settings(repo
);
301 if (repo
->settings
.command_requires_full_index
)
302 ensure_full_index(repo
->index
);
307 int repo_hold_locked_index(struct repository
*repo
,
308 struct lock_file
*lf
,
311 if (!repo
->index_file
)
312 BUG("the repo hasn't been setup");
313 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);