]>
git.ipfire.org Git - thirdparty/git.git/blob - repository.c
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"
14 /* The main repository */
15 static struct repository the_repo
;
16 struct repository
*the_repository
;
17 struct index_state the_index
;
19 void initialize_the_repository(void)
21 the_repository
= &the_repo
;
23 the_repo
.index
= &the_index
;
24 the_repo
.objects
= raw_object_store_new();
25 the_repo
.parsed_objects
= parsed_object_pool_new();
27 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
30 static void expand_base_dir(char **out
, const char *in
,
31 const char *base_dir
, const char *def_in
)
37 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
40 static void repo_set_commondir(struct repository
*repo
,
41 const char *commondir
)
43 struct strbuf sb
= STRBUF_INIT
;
45 free(repo
->commondir
);
48 repo
->different_commondir
= 1;
49 repo
->commondir
= xstrdup(commondir
);
53 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
54 repo
->commondir
= strbuf_detach(&sb
, NULL
);
57 void repo_set_gitdir(struct repository
*repo
,
59 const struct set_gitdir_args
*o
)
61 const char *gitfile
= read_gitfile(root
);
63 * repo->gitdir is saved because the caller could pass "root"
64 * that also points to repo->gitdir. We want to keep it alive
65 * until after xstrdup(root). Then we can free it.
67 char *old_gitdir
= repo
->gitdir
;
69 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
72 repo_set_commondir(repo
, o
->commondir
);
74 if (!repo
->objects
->odb
) {
75 repo
->objects
->odb
= xcalloc(1, sizeof(*repo
->objects
->odb
));
76 repo
->objects
->odb_tail
= &repo
->objects
->odb
->next
;
78 expand_base_dir(&repo
->objects
->odb
->path
, o
->object_dir
,
79 repo
->commondir
, "objects");
81 free(repo
->objects
->alternate_db
);
82 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
83 expand_base_dir(&repo
->graft_file
, o
->graft_file
,
84 repo
->commondir
, "info/grafts");
85 expand_base_dir(&repo
->index_file
, o
->index_file
,
86 repo
->gitdir
, "index");
89 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
91 repo
->hash_algo
= &hash_algos
[hash_algo
];
93 if (hash_algo
!= GIT_HASH_SHA1
)
94 die(_("The hash algorithm %s is not supported in this build."), repo
->hash_algo
->name
);
99 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
100 * Return 0 upon success and a non-zero value upon failure.
102 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
106 char *abspath
= NULL
;
107 const char *resolved_gitdir
;
108 struct set_gitdir_args args
= { NULL
};
110 abspath
= real_pathdup(gitdir
, 0);
116 /* 'gitdir' must reference the gitdir directly */
117 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
118 if (!resolved_gitdir
) {
123 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
130 void repo_set_worktree(struct repository
*repo
, const char *path
)
132 repo
->worktree
= real_pathdup(path
, 1);
134 trace2_def_repo(repo
);
137 static int read_and_verify_repository_format(struct repository_format
*format
,
138 const char *commondir
)
141 struct strbuf sb
= STRBUF_INIT
;
143 strbuf_addf(&sb
, "%s/config", commondir
);
144 read_repository_format(format
, sb
.buf
);
147 if (verify_repository_format(format
, &sb
) < 0) {
148 warning("%s", sb
.buf
);
157 * Initialize 'repo' based on the provided 'gitdir'.
158 * Return 0 upon success and a non-zero value upon failure.
160 int repo_init(struct repository
*repo
,
162 const char *worktree
)
164 struct repository_format format
= REPOSITORY_FORMAT_INIT
;
165 memset(repo
, 0, sizeof(*repo
));
167 repo
->objects
= raw_object_store_new();
168 repo
->parsed_objects
= parsed_object_pool_new();
170 if (repo_init_gitdir(repo
, gitdir
))
173 if (read_and_verify_repository_format(&format
, repo
->commondir
))
176 repo_set_hash_algo(repo
, format
.hash_algo
);
179 repo_set_worktree(repo
, worktree
);
181 clear_repository_format(&format
);
189 int repo_submodule_init(struct repository
*subrepo
,
190 struct repository
*superproject
,
191 const struct submodule
*sub
)
193 struct strbuf gitdir
= STRBUF_INIT
;
194 struct strbuf worktree
= STRBUF_INIT
;
202 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", sub
->path
);
203 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", sub
->path
);
205 if (repo_init(subrepo
, gitdir
.buf
, worktree
.buf
)) {
207 * If initialization fails then it may be due to the submodule
208 * not being populated in the superproject's worktree. Instead
209 * we can try to initialize the submodule by finding it's gitdir
210 * in the superproject's 'modules' directory. In this case the
211 * submodule would not have a worktree.
213 strbuf_reset(&gitdir
);
214 strbuf_repo_git_path(&gitdir
, superproject
,
215 "modules/%s", sub
->name
);
217 if (repo_init(subrepo
, gitdir
.buf
, NULL
)) {
223 subrepo
->submodule_prefix
= xstrfmt("%s%s/",
224 superproject
->submodule_prefix
?
225 superproject
->submodule_prefix
:
229 strbuf_release(&gitdir
);
230 strbuf_release(&worktree
);
234 void repo_clear(struct repository
*repo
)
236 FREE_AND_NULL(repo
->gitdir
);
237 FREE_AND_NULL(repo
->commondir
);
238 FREE_AND_NULL(repo
->graft_file
);
239 FREE_AND_NULL(repo
->index_file
);
240 FREE_AND_NULL(repo
->worktree
);
241 FREE_AND_NULL(repo
->submodule_prefix
);
243 raw_object_store_clear(repo
->objects
);
244 FREE_AND_NULL(repo
->objects
);
246 parsed_object_pool_clear(repo
->parsed_objects
);
247 FREE_AND_NULL(repo
->parsed_objects
);
250 git_configset_clear(repo
->config
);
251 FREE_AND_NULL(repo
->config
);
254 if (repo
->submodule_cache
) {
255 submodule_cache_free(repo
->submodule_cache
);
256 repo
->submodule_cache
= NULL
;
260 discard_index(repo
->index
);
261 if (repo
->index
!= &the_index
)
262 FREE_AND_NULL(repo
->index
);
266 int repo_read_index(struct repository
*repo
)
269 repo
->index
= xcalloc(1, sizeof(*repo
->index
));
271 return read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);
274 int repo_hold_locked_index(struct repository
*repo
,
275 struct lock_file
*lf
,
278 if (!repo
->index_file
)
279 BUG("the repo hasn't been setup");
280 return hold_lock_file_for_update(lf
, repo
->index_file
, flags
);