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 repo_setup_env(struct repository
*repo
)
45 struct strbuf sb
= STRBUF_INIT
;
47 repo
->different_commondir
= find_common_dir(&sb
, repo
->gitdir
,
49 free(repo
->commondir
);
50 repo
->commondir
= strbuf_detach(&sb
, NULL
);
51 free(repo
->objectdir
);
52 repo
->objectdir
= git_path_from_env(DB_ENVIRONMENT
, repo
->commondir
,
53 "objects", !repo
->ignore_env
);
54 free(repo
->graft_file
);
55 repo
->graft_file
= git_path_from_env(GRAFT_ENVIRONMENT
, repo
->commondir
,
56 "info/grafts", !repo
->ignore_env
);
57 free(repo
->index_file
);
58 repo
->index_file
= git_path_from_env(INDEX_ENVIRONMENT
, repo
->gitdir
,
59 "index", !repo
->ignore_env
);
62 void repo_set_gitdir(struct repository
*repo
, const char *path
)
64 const char *gitfile
= read_gitfile(path
);
65 char *old_gitdir
= repo
->gitdir
;
67 repo
->gitdir
= xstrdup(gitfile
? gitfile
: path
);
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
;
89 abspath
= real_pathdup(gitdir
, 0);
95 /* 'gitdir' must reference the gitdir directly */
96 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
97 if (!resolved_gitdir
) {
102 repo_set_gitdir(repo
, resolved_gitdir
);
109 void repo_set_worktree(struct repository
*repo
, const char *path
)
111 repo
->worktree
= real_pathdup(path
, 1);
114 static int read_and_verify_repository_format(struct repository_format
*format
,
115 const char *commondir
)
118 struct strbuf sb
= STRBUF_INIT
;
120 strbuf_addf(&sb
, "%s/config", commondir
);
121 read_repository_format(format
, sb
.buf
);
124 if (verify_repository_format(format
, &sb
) < 0) {
125 warning("%s", sb
.buf
);
134 * Initialize 'repo' based on the provided 'gitdir'.
135 * Return 0 upon success and a non-zero value upon failure.
137 static int repo_init(struct repository
*repo
,
139 const char *worktree
)
141 struct repository_format format
;
142 memset(repo
, 0, sizeof(*repo
));
144 repo
->ignore_env
= 1;
146 if (repo_init_gitdir(repo
, gitdir
))
149 if (read_and_verify_repository_format(&format
, repo
->commondir
))
152 repo_set_hash_algo(repo
, format
.hash_algo
);
155 repo_set_worktree(repo
, worktree
);
165 * Initialize 'submodule' as the submodule given by 'path' in parent repository
167 * Return 0 upon success and a non-zero value upon failure.
169 int repo_submodule_init(struct repository
*submodule
,
170 struct repository
*superproject
,
173 const struct submodule
*sub
;
174 struct strbuf gitdir
= STRBUF_INIT
;
175 struct strbuf worktree
= STRBUF_INIT
;
178 sub
= submodule_from_cache(superproject
, &null_oid
, path
);
184 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
185 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
187 if (repo_init(submodule
, gitdir
.buf
, worktree
.buf
)) {
189 * If initilization fails then it may be due to the submodule
190 * not being populated in the superproject's worktree. Instead
191 * we can try to initilize the submodule by finding it's gitdir
192 * in the superproject's 'modules' directory. In this case the
193 * submodule would not have a worktree.
195 strbuf_reset(&gitdir
);
196 strbuf_repo_git_path(&gitdir
, superproject
,
197 "modules/%s", sub
->name
);
199 if (repo_init(submodule
, gitdir
.buf
, NULL
)) {
205 submodule
->submodule_prefix
= xstrfmt("%s%s/",
206 superproject
->submodule_prefix
?
207 superproject
->submodule_prefix
:
211 strbuf_release(&gitdir
);
212 strbuf_release(&worktree
);
216 void repo_clear(struct repository
*repo
)
218 FREE_AND_NULL(repo
->gitdir
);
219 FREE_AND_NULL(repo
->commondir
);
220 FREE_AND_NULL(repo
->objectdir
);
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
);
227 git_configset_clear(repo
->config
);
228 FREE_AND_NULL(repo
->config
);
231 if (repo
->submodule_cache
) {
232 submodule_cache_free(repo
->submodule_cache
);
233 repo
->submodule_cache
= NULL
;
237 discard_index(repo
->index
);
238 FREE_AND_NULL(repo
->index
);
242 int repo_read_index(struct repository
*repo
)
245 repo
->index
= xcalloc(1, sizeof(*repo
->index
));
247 return read_index_from(repo
->index
, repo
->index_file
, repo
->gitdir
);