2 #include "repository.h"
3 #include "object-store.h"
5 #include "submodule-config.h"
7 /* The main repository */
8 static struct repository the_repo
;
9 struct repository
*the_repository
;
11 void initialize_the_repository(void)
13 the_repository
= &the_repo
;
15 the_repo
.index
= &the_index
;
16 the_repo
.objects
= raw_object_store_new();
17 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
20 static void expand_base_dir(char **out
, const char *in
,
21 const char *base_dir
, const char *def_in
)
27 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
30 static void repo_set_commondir(struct repository
*repo
,
31 const char *commondir
)
33 struct strbuf sb
= STRBUF_INIT
;
35 free(repo
->commondir
);
38 repo
->different_commondir
= 1;
39 repo
->commondir
= xstrdup(commondir
);
43 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
44 repo
->commondir
= strbuf_detach(&sb
, NULL
);
47 void repo_set_gitdir(struct repository
*repo
,
49 const struct set_gitdir_args
*o
)
51 const char *gitfile
= read_gitfile(root
);
53 * repo->gitdir is saved because the caller could pass "root"
54 * that also points to repo->gitdir. We want to keep it alive
55 * until after xstrdup(root). Then we can free it.
57 char *old_gitdir
= repo
->gitdir
;
59 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
62 repo_set_commondir(repo
, o
->commondir
);
63 expand_base_dir(&repo
->objects
->objectdir
, o
->object_dir
,
64 repo
->commondir
, "objects");
65 free(repo
->objects
->alternate_db
);
66 repo
->objects
->alternate_db
= xstrdup_or_null(o
->alternate_db
);
67 expand_base_dir(&repo
->graft_file
, o
->graft_file
,
68 repo
->commondir
, "info/grafts");
69 expand_base_dir(&repo
->index_file
, o
->index_file
,
70 repo
->gitdir
, "index");
73 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
75 repo
->hash_algo
= &hash_algos
[hash_algo
];
79 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
80 * Return 0 upon success and a non-zero value upon failure.
82 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
87 const char *resolved_gitdir
;
88 struct set_gitdir_args args
= { NULL
};
90 abspath
= real_pathdup(gitdir
, 0);
96 /* 'gitdir' must reference the gitdir directly */
97 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
98 if (!resolved_gitdir
) {
103 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
110 void repo_set_worktree(struct repository
*repo
, const char *path
)
112 repo
->worktree
= real_pathdup(path
, 1);
115 static int read_and_verify_repository_format(struct repository_format
*format
,
116 const char *commondir
)
119 struct strbuf sb
= STRBUF_INIT
;
121 strbuf_addf(&sb
, "%s/config", commondir
);
122 read_repository_format(format
, sb
.buf
);
125 if (verify_repository_format(format
, &sb
) < 0) {
126 warning("%s", sb
.buf
);
135 * Initialize 'repo' based on the provided 'gitdir'.
136 * Return 0 upon success and a non-zero value upon failure.
138 int repo_init(struct repository
*repo
,
140 const char *worktree
)
142 struct repository_format format
;
143 memset(repo
, 0, sizeof(*repo
));
145 repo
->objects
= raw_object_store_new();
147 if (repo_init_gitdir(repo
, gitdir
))
150 if (read_and_verify_repository_format(&format
, repo
->commondir
))
153 repo_set_hash_algo(repo
, format
.hash_algo
);
156 repo_set_worktree(repo
, worktree
);
166 * Initialize 'submodule' as the submodule given by 'path' in parent repository
168 * Return 0 upon success and a non-zero value upon failure.
170 int repo_submodule_init(struct repository
*submodule
,
171 struct repository
*superproject
,
174 const struct submodule
*sub
;
175 struct strbuf gitdir
= STRBUF_INIT
;
176 struct strbuf worktree
= STRBUF_INIT
;
179 sub
= submodule_from_path(superproject
, &null_oid
, path
);
185 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
186 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
188 if (repo_init(submodule
, gitdir
.buf
, worktree
.buf
)) {
190 * If initilization fails then it may be due to the submodule
191 * not being populated in the superproject's worktree. Instead
192 * we can try to initilize the submodule by finding it's gitdir
193 * in the superproject's 'modules' directory. In this case the
194 * submodule would not have a worktree.
196 strbuf_reset(&gitdir
);
197 strbuf_repo_git_path(&gitdir
, superproject
,
198 "modules/%s", sub
->name
);
200 if (repo_init(submodule
, gitdir
.buf
, NULL
)) {
206 submodule
->submodule_prefix
= xstrfmt("%s%s/",
207 superproject
->submodule_prefix
?
208 superproject
->submodule_prefix
:
212 strbuf_release(&gitdir
);
213 strbuf_release(&worktree
);
217 void repo_clear(struct repository
*repo
)
219 FREE_AND_NULL(repo
->gitdir
);
220 FREE_AND_NULL(repo
->commondir
);
221 FREE_AND_NULL(repo
->graft_file
);
222 FREE_AND_NULL(repo
->index_file
);
223 FREE_AND_NULL(repo
->worktree
);
224 FREE_AND_NULL(repo
->submodule_prefix
);
226 raw_object_store_clear(repo
->objects
);
227 FREE_AND_NULL(repo
->objects
);
230 git_configset_clear(repo
->config
);
231 FREE_AND_NULL(repo
->config
);
234 if (repo
->submodule_cache
) {
235 submodule_cache_free(repo
->submodule_cache
);
236 repo
->submodule_cache
= NULL
;
240 discard_index(repo
->index
);
241 if (repo
->index
!= &the_index
)
242 FREE_AND_NULL(repo
->index
);
246 int repo_read_index(struct repository
*repo
)
249 repo
->index
= xcalloc(1, sizeof(*repo
->index
));
251 return read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);