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