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