sha1_file.c: move delayed getenv(altdb) back to setup_git_env()
[git/git.git] / repository.c
CommitLineData
359efeff
BW
1#include "cache.h"
2#include "repository.h"
3b256228 3#include "config.h"
bf12fcdf 4#include "submodule-config.h"
359efeff
BW
5
6/* The main repository */
b2f0ecee
NTND
7static struct repository the_repo;
8struct repository *the_repository;
9
10void initialize_the_repository(void)
11{
12 the_repository = &the_repo;
13
14 the_repo.index = &the_index;
15 repo_set_hash_algo(&the_repo, GIT_HASH_SHA1);
16}
359efeff 17
357a03eb
NTND
18static void expand_base_dir(char **out, const char *in,
19 const char *base_dir, const char *def_in)
20{
21 free(*out);
22 if (in)
23 *out = xstrdup(in);
24 else
25 *out = xstrfmt("%s/%s", base_dir, def_in);
26}
27
28static void repo_set_commondir(struct repository *repo,
29 const char *commondir)
359efeff
BW
30{
31 struct strbuf sb = STRBUF_INIT;
32
f9b7573f 33 free(repo->commondir);
357a03eb
NTND
34
35 if (commondir) {
36 repo->different_commondir = 1;
37 repo->commondir = xstrdup(commondir);
38 return;
39 }
40
41 repo->different_commondir = get_common_dir_noenv(&sb, repo->gitdir);
359efeff 42 repo->commondir = strbuf_detach(&sb, NULL);
359efeff
BW
43}
44
357a03eb
NTND
45void repo_set_gitdir(struct repository *repo,
46 const char *root,
47 const struct set_gitdir_args *o)
359efeff 48{
357a03eb
NTND
49 const char *gitfile = read_gitfile(root);
50 /*
51 * repo->gitdir is saved because the caller could pass "root"
52 * that also points to repo->gitdir. We want to keep it alive
53 * until after xstrdup(root). Then we can free it.
54 */
1fb2b636 55 char *old_gitdir = repo->gitdir;
359efeff 56
357a03eb 57 repo->gitdir = xstrdup(gitfile ? gitfile : root);
1fb2b636 58 free(old_gitdir);
357a03eb
NTND
59
60 repo_set_commondir(repo, o->commondir);
61 expand_base_dir(&repo->objectdir, o->object_dir,
62 repo->commondir, "objects");
7bc0dcaa
NTND
63 free(repo->alternate_db);
64 repo->alternate_db = xstrdup_or_null(o->alternate_db);
357a03eb
NTND
65 expand_base_dir(&repo->graft_file, o->graft_file,
66 repo->commondir, "info/grafts");
67 expand_base_dir(&repo->index_file, o->index_file,
68 repo->gitdir, "index");
359efeff
BW
69}
70
78a67668 71void repo_set_hash_algo(struct repository *repo, int hash_algo)
72{
73 repo->hash_algo = &hash_algos[hash_algo];
74}
75
359efeff
BW
76/*
77 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
78 * Return 0 upon success and a non-zero value upon failure.
79 */
80static int repo_init_gitdir(struct repository *repo, const char *gitdir)
81{
82 int ret = 0;
83 int error = 0;
84 char *abspath = NULL;
85 const char *resolved_gitdir;
357a03eb 86 struct set_gitdir_args args = { NULL };
359efeff
BW
87
88 abspath = real_pathdup(gitdir, 0);
89 if (!abspath) {
90 ret = -1;
91 goto out;
92 }
93
94 /* 'gitdir' must reference the gitdir directly */
95 resolved_gitdir = resolve_gitdir_gently(abspath, &error);
96 if (!resolved_gitdir) {
97 ret = -1;
98 goto out;
99 }
100
357a03eb 101 repo_set_gitdir(repo, resolved_gitdir, &args);
359efeff
BW
102
103out:
104 free(abspath);
105 return ret;
106}
107
108void repo_set_worktree(struct repository *repo, const char *path)
109{
110 repo->worktree = real_pathdup(path, 1);
111}
112
113static int read_and_verify_repository_format(struct repository_format *format,
114 const char *commondir)
115{
116 int ret = 0;
117 struct strbuf sb = STRBUF_INIT;
118
119 strbuf_addf(&sb, "%s/config", commondir);
120 read_repository_format(format, sb.buf);
121 strbuf_reset(&sb);
122
123 if (verify_repository_format(format, &sb) < 0) {
124 warning("%s", sb.buf);
125 ret = -1;
126 }
127
128 strbuf_release(&sb);
129 return ret;
130}
131
132/*
133 * Initialize 'repo' based on the provided 'gitdir'.
134 * Return 0 upon success and a non-zero value upon failure.
135 */
b2f0ecee
NTND
136static int repo_init(struct repository *repo,
137 const char *gitdir,
138 const char *worktree)
359efeff
BW
139{
140 struct repository_format format;
141 memset(repo, 0, sizeof(*repo));
142
143 repo->ignore_env = 1;
144
145 if (repo_init_gitdir(repo, gitdir))
146 goto error;
147
148 if (read_and_verify_repository_format(&format, repo->commondir))
149 goto error;
150
78a67668 151 repo_set_hash_algo(repo, format.hash_algo);
152
359efeff
BW
153 if (worktree)
154 repo_set_worktree(repo, worktree);
155
156 return 0;
157
158error:
159 repo_clear(repo);
160 return -1;
161}
162
96dc883b
BW
163/*
164 * Initialize 'submodule' as the submodule given by 'path' in parent repository
165 * 'superproject'.
166 * Return 0 upon success and a non-zero value upon failure.
167 */
168int repo_submodule_init(struct repository *submodule,
169 struct repository *superproject,
170 const char *path)
171{
172 const struct submodule *sub;
173 struct strbuf gitdir = STRBUF_INIT;
174 struct strbuf worktree = STRBUF_INIT;
175 int ret = 0;
176
cd73de47 177 sub = submodule_from_cache(superproject, &null_oid, path);
96dc883b
BW
178 if (!sub) {
179 ret = -1;
180 goto out;
181 }
182
183 strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
184 strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
185
186 if (repo_init(submodule, gitdir.buf, worktree.buf)) {
187 /*
188 * If initilization fails then it may be due to the submodule
189 * not being populated in the superproject's worktree. Instead
190 * we can try to initilize the submodule by finding it's gitdir
191 * in the superproject's 'modules' directory. In this case the
192 * submodule would not have a worktree.
193 */
194 strbuf_reset(&gitdir);
195 strbuf_repo_git_path(&gitdir, superproject,
196 "modules/%s", sub->name);
197
198 if (repo_init(submodule, gitdir.buf, NULL)) {
199 ret = -1;
200 goto out;
201 }
202 }
203
204 submodule->submodule_prefix = xstrfmt("%s%s/",
205 superproject->submodule_prefix ?
206 superproject->submodule_prefix :
207 "", path);
208
209out:
210 strbuf_release(&gitdir);
211 strbuf_release(&worktree);
212 return ret;
213}
214
359efeff
BW
215void repo_clear(struct repository *repo)
216{
90dd04aa
RS
217 FREE_AND_NULL(repo->gitdir);
218 FREE_AND_NULL(repo->commondir);
219 FREE_AND_NULL(repo->objectdir);
7bc0dcaa 220 FREE_AND_NULL(repo->alternate_db);
90dd04aa
RS
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);
3b256228
BW
225
226 if (repo->config) {
227 git_configset_clear(repo->config);
90dd04aa 228 FREE_AND_NULL(repo->config);
3b256228 229 }
639e30b5 230
bf12fcdf
BW
231 if (repo->submodule_cache) {
232 submodule_cache_free(repo->submodule_cache);
233 repo->submodule_cache = NULL;
234 }
235
639e30b5
BW
236 if (repo->index) {
237 discard_index(repo->index);
90dd04aa 238 FREE_AND_NULL(repo->index);
639e30b5
BW
239 }
240}
241
242int repo_read_index(struct repository *repo)
243{
244 if (!repo->index)
245 repo->index = xcalloc(1, sizeof(*repo->index));
639e30b5 246
a125a223 247 return read_index_from(repo->index, repo->index_file, repo->gitdir);
359efeff 248}