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"
12 #include "submodule-config.h"
13 #include "sparse-index.h"
14 #include "promisor-remote.h"
16 /* The main repository */
17 static struct repository the_repo
;
18 struct repository
*the_repository
;
19 struct index_state the_index
;
21 void initialize_the_repository(void)
23 the_repository
= &the_repo
;
25 the_repo
.index
= &the_index
;
26 the_repo
.objects
= raw_object_store_new();
27 the_repo
.parsed_objects
= parsed_object_pool_new();
29 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
32 static void expand_base_dir(char **out
, const char *in
,
33 const char *base_dir
, const char *def_in
)
39 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
42 static void repo_set_commondir(struct repository
*repo
,
43 const char *commondir
)
45 struct strbuf sb
= STRBUF_INIT
;
47 free(repo
->commondir
);
50 repo
->different_commondir
= 1;
51 repo
->commondir
= xstrdup(commondir
);
55 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
56 repo
->commondir
= strbuf_detach(&sb
, NULL
);
59 void repo_set_gitdir(struct repository
*repo
,
61 const struct set_gitdir_args
*o
)
63 const char *gitfile
= read_gitfile(root
);
65 * repo->gitdir is saved because the caller could pass "root"
66 * that also points to repo->gitdir. We want to keep it alive
67 * until after xstrdup(root). Then we can free it.
69 char *old_gitdir
= repo
->gitdir
;
71 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
74 repo_set_commondir(repo
, o
->commondir
);
76 if (!repo
->objects
->odb
) {
77 CALLOC_ARRAY(repo
->objects
->odb
, 1);
78 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
80 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
81 repo
->commondir
, "objects");
83 free(repo
->objects
->alternate_db
);
84 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
85 expand_base_dir(&repo
->graft_file
, o
->graft_file
,
86 repo
->commondir
, "info/grafts");
87 expand_base_dir(&repo
->index_file
, o
->index_file
,
88 repo
->gitdir
, "index");
91 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
93 repo
->hash_algo
= &hash_algos
[hash_algo
];
97 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
98 * Return 0 upon success and a non-zero value upon failure.
100 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
104 char *abspath
= NULL
;
105 const char *resolved_gitdir
;
106 struct set_gitdir_args args
= { NULL
};
108 abspath
= real_pathdup(gitdir
, 0);
114 /* 'gitdir' must reference the gitdir directly */
115 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
116 if (!resolved_gitdir
) {
121 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
128 void repo_set_worktree(struct repository
*repo
, const char *path
)
130 repo
->worktree
= real_pathdup(path
, 1);
132 trace2_def_repo(repo
);
135 static int read_and_verify_repository_format(struct repository_format
*format
,
136 const char *commondir
)
139 struct strbuf sb
= STRBUF_INIT
;
141 strbuf_addf(&sb
, "%s/config", commondir
);
142 read_repository_format(format
, sb
.buf
);
145 if (verify_repository_format(format
, &sb
) < 0) {
146 warning("%s", sb
.buf
);
155 * Initialize 'repo' based on the provided 'gitdir'.
156 * Return 0 upon success and a non-zero value upon failure.
158 int repo_init(struct repository
*repo
,
160 const char *worktree
)
162 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
163 memset(repo
, 0, sizeof(*repo
));
165 repo
->objects
= raw_object_store_new();
166 repo
->parsed_objects
= parsed_object_pool_new();
168 if (repo_init_gitdir(repo
, gitdir
))
171 if (read_and_verify_repository_format(&format
, repo
->commondir
))
174 repo_set_hash_algo(repo
, format
.hash_algo
);
176 /* take ownership of format.partial_clone */
177 repo
->repository_format_partial_clone
= format
.partial_clone
;
178 format
.partial_clone
= NULL
;
181 repo_set_worktree(repo
, worktree
);
183 clear_repository_format(&format
);
191 int repo_submodule_init(struct repository
*subrepo
,
192 struct repository
*superproject
,
194 const struct object_id
*treeish_name
)
196 struct strbuf gitdir
= STRBUF_INIT
;
197 struct strbuf worktree
= STRBUF_INIT
;
200 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
201 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
203 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
205 * If initialization fails then it may be due to the submodule
206 * not being populated in the superproject's worktree. Instead
207 * we can try to initialize the submodule by finding it's gitdir
208 * in the superproject's 'modules' directory. In this case the
209 * submodule would not have a worktree.
211 const struct submodule
*sub
=
212 submodule_from_path(superproject
, treeish_name
, path
);
218 strbuf_reset(&gitdir
);
219 submodule_name_to_gitdir(&gitdir
, superproject
, sub
->name
);
221 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
227 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
228 superproject
->submodule_prefix
?
229 superproject
->submodule_prefix
:
233 strbuf_release(&gitdir
);
234 strbuf_release(&worktree
);
238 void repo_clear(struct repository
*repo
)
240 FREE_AND_NULL(repo
->gitdir
);
241 FREE_AND_NULL(repo
->commondir
);
242 FREE_AND_NULL(repo
->graft_file
);
243 FREE_AND_NULL(repo
->index_file
);
244 FREE_AND_NULL(repo
->worktree
);
245 FREE_AND_NULL(repo
->submodule_prefix
);
247 raw_object_store_clear(repo
->objects
);
248 FREE_AND_NULL(repo
->objects
);
250 parsed_object_pool_clear(repo
->parsed_objects
);
251 FREE_AND_NULL(repo
->parsed_objects
);
254 git_configset_clear(repo
->config
);
255 FREE_AND_NULL(repo
->config
);
258 if (repo
->submodule_cache
) {
259 submodule_cache_free(repo
->submodule_cache
);
260 repo
->submodule_cache
= NULL
;
264 discard_index(repo
->index
);
265 if (repo
->index
!= &the_index
)
266 FREE_AND_NULL(repo
->index
);
269 if (repo
->promisor_remote_config
) {
270 promisor_remote_clear(repo
->promisor_remote_config
);
271 FREE_AND_NULL(repo
->promisor_remote_config
);
275 int repo_read_index(struct repository
*repo
)
280 CALLOC_ARRAY(repo
->index
, 1);
282 /* Complete the double-reference */
283 if (!repo
->index
->repo
)
284 repo
->index
->repo
= repo
;
285 else if (repo
->index
->repo
!= repo
)
286 BUG("repo's index should point back at itself");
288 res
= read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
290 prepare_repo_settings(repo
);
291 if (repo
->settings
.command_requires_full_index
)
292 ensure_full_index(repo
->index
);
297 int repo_hold_locked_index(struct repository
*repo
,
298 struct lock_file
*lf
,
301 if (!repo
->index_file
)
302 BUG("the repo hasn't been setup");
303 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);