2 #include "repository.h"
4 #include "submodule-config.h"
6 /* The main repository */
7 static struct repository the_repo
;
8 struct repository
*the_repository
;
10 void initialize_the_repository(void)
12 the_repository
= &the_repo
;
14 the_repo
.index
= &the_index
;
15 repo_set_hash_algo(&the_repo
, GIT_HASH_SHA1
);
18 static char *git_path_from_env(const char *envvar
, const char *git_dir
,
19 const char *path
, int fromenv
)
22 const char *value
= getenv(envvar
);
24 return xstrdup(value
);
27 return xstrfmt("%s/%s", git_dir
, path
);
30 static int find_common_dir(struct strbuf
*sb
, const char *gitdir
, int fromenv
)
33 const char *value
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
35 strbuf_addstr(sb
, value
);
40 return get_common_dir_noenv(sb
, gitdir
);
43 static void expand_base_dir(char **out
, const char *in
,
44 const char *base_dir
, const char *def_in
)
50 *out
= xstrfmt("%s/%s", base_dir
, def_in
);
53 static void repo_set_commondir(struct repository
*repo
,
54 const char *commondir
)
56 struct strbuf sb
= STRBUF_INIT
;
58 free(repo
->commondir
);
61 repo
->different_commondir
= 1;
62 repo
->commondir
= xstrdup(commondir
);
66 repo
->different_commondir
= get_common_dir_noenv(&sb
, repo
->gitdir
);
67 repo
->commondir
= strbuf_detach(&sb
, NULL
);
70 void repo_set_gitdir(struct repository
*repo
,
72 const struct set_gitdir_args
*o
)
74 const char *gitfile
= read_gitfile(root
);
76 * repo->gitdir is saved because the caller could pass "root"
77 * that also points to repo->gitdir. We want to keep it alive
78 * until after xstrdup(root). Then we can free it.
80 char *old_gitdir
= repo
->gitdir
;
82 repo
->gitdir
= xstrdup(gitfile
? gitfile
: root
);
85 repo_set_commondir(repo
, o
->commondir
);
86 expand_base_dir(&repo
->objectdir
, o
->object_dir
,
87 repo
->commondir
, "objects");
88 expand_base_dir(&repo
->graft_file
, o
->graft_file
,
89 repo
->commondir
, "info/grafts");
90 expand_base_dir(&repo
->index_file
, o
->index_file
,
91 repo
->gitdir
, "index");
94 void repo_set_hash_algo(struct repository
*repo
, int hash_algo
)
96 repo
->hash_algo
= &hash_algos
[hash_algo
];
100 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
101 * Return 0 upon success and a non-zero value upon failure.
103 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
107 char *abspath
= NULL
;
108 const char *resolved_gitdir
;
109 struct set_gitdir_args args
= { NULL
};
111 abspath
= real_pathdup(gitdir
, 0);
117 /* 'gitdir' must reference the gitdir directly */
118 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
119 if (!resolved_gitdir
) {
124 repo_set_gitdir(repo
, resolved_gitdir
, &args
);
131 void repo_set_worktree(struct repository
*repo
, const char *path
)
133 repo
->worktree
= real_pathdup(path
, 1);
136 static int read_and_verify_repository_format(struct repository_format
*format
,
137 const char *commondir
)
140 struct strbuf sb
= STRBUF_INIT
;
142 strbuf_addf(&sb
, "%s/config", commondir
);
143 read_repository_format(format
, sb
.buf
);
146 if (verify_repository_format(format
, &sb
) < 0) {
147 warning("%s", sb
.buf
);
156 * Initialize 'repo' based on the provided 'gitdir'.
157 * Return 0 upon success and a non-zero value upon failure.
159 static int repo_init(struct repository
*repo
,
161 const char *worktree
)
163 struct repository_format format
;
164 memset(repo
, 0, sizeof(*repo
));
166 repo
->ignore_env
= 1;
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
);
177 repo_set_worktree(repo
, worktree
);
187 * Initialize 'submodule' as the submodule given by 'path' in parent repository
189 * Return 0 upon success and a non-zero value upon failure.
191 int repo_submodule_init(struct repository
*submodule
,
192 struct repository
*superproject
,
195 const struct submodule
*sub
;
196 struct strbuf gitdir
= STRBUF_INIT
;
197 struct strbuf worktree
= STRBUF_INIT
;
200 sub
= submodule_from_cache(superproject
, &null_oid
, path
);
206 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
207 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
209 if (repo_init(submodule
, gitdir
.buf
, worktree
.buf
)) {
211 * If initilization fails then it may be due to the submodule
212 * not being populated in the superproject's worktree. Instead
213 * we can try to initilize the submodule by finding it's gitdir
214 * in the superproject's 'modules' directory. In this case the
215 * submodule would not have a worktree.
217 strbuf_reset(&gitdir
);
218 strbuf_repo_git_path(&gitdir
, superproject
,
219 "modules/%s", sub
->name
);
221 if (repo_init(submodule
, gitdir
.buf
, NULL
)) {
227 submodule
->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
->objectdir
);
243 FREE_AND_NULL(repo
->graft_file
);
244 FREE_AND_NULL(repo
->index_file
);
245 FREE_AND_NULL(repo
->worktree
);
246 FREE_AND_NULL(repo
->submodule_prefix
);
249 git_configset_clear(repo
->config
);
250 FREE_AND_NULL(repo
->config
);
253 if (repo
->submodule_cache
) {
254 submodule_cache_free(repo
->submodule_cache
);
255 repo
->submodule_cache
= NULL
;
259 discard_index(repo
->index
);
260 FREE_AND_NULL(repo
->index
);
264 int repo_read_index(struct repository
*repo
)
267 repo
->index
= xcalloc(1, sizeof(*repo
->index
));
269 return read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);