2 #include "repository.h"
4 #include "submodule-config.h"
6 /* The main repository */
7 static struct repository the_repo
= {
8 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &the_index
, 0, 0
10 struct repository
*the_repository
= &the_repo
;
12 static char *git_path_from_env(const char *envvar
, const char *git_dir
,
13 const char *path
, int fromenv
)
16 const char *value
= getenv(envvar
);
18 return xstrdup(value
);
21 return xstrfmt("%s/%s", git_dir
, path
);
24 static int find_common_dir(struct strbuf
*sb
, const char *gitdir
, int fromenv
)
27 const char *value
= getenv(GIT_COMMON_DIR_ENVIRONMENT
);
29 strbuf_addstr(sb
, value
);
34 return get_common_dir_noenv(sb
, gitdir
);
37 static void repo_setup_env(struct repository
*repo
)
39 struct strbuf sb
= STRBUF_INIT
;
41 repo
->different_commondir
= find_common_dir(&sb
, repo
->gitdir
,
43 repo
->commondir
= strbuf_detach(&sb
, NULL
);
44 repo
->objectdir
= git_path_from_env(DB_ENVIRONMENT
, repo
->commondir
,
45 "objects", !repo
->ignore_env
);
46 repo
->graft_file
= git_path_from_env(GRAFT_ENVIRONMENT
, repo
->commondir
,
47 "info/grafts", !repo
->ignore_env
);
48 repo
->index_file
= git_path_from_env(INDEX_ENVIRONMENT
, repo
->gitdir
,
49 "index", !repo
->ignore_env
);
52 void repo_set_gitdir(struct repository
*repo
, const char *path
)
54 const char *gitfile
= read_gitfile(path
);
57 * NEEDSWORK: Eventually we want to be able to free gitdir and the rest
58 * of the environment before reinitializing it again, but we have some
59 * crazy code paths where we try to set gitdir with the current gitdir
60 * and we don't want to free gitdir before copying the passed in value.
62 repo
->gitdir
= xstrdup(gitfile
? gitfile
: path
);
68 * Attempt to resolve and set the provided 'gitdir' for repository 'repo'.
69 * Return 0 upon success and a non-zero value upon failure.
71 static int repo_init_gitdir(struct repository
*repo
, const char *gitdir
)
76 const char *resolved_gitdir
;
78 abspath
= real_pathdup(gitdir
, 0);
84 /* 'gitdir' must reference the gitdir directly */
85 resolved_gitdir
= resolve_gitdir_gently(abspath
, &error
);
86 if (!resolved_gitdir
) {
91 repo_set_gitdir(repo
, resolved_gitdir
);
98 void repo_set_worktree(struct repository
*repo
, const char *path
)
100 repo
->worktree
= real_pathdup(path
, 1);
103 static int read_and_verify_repository_format(struct repository_format
*format
,
104 const char *commondir
)
107 struct strbuf sb
= STRBUF_INIT
;
109 strbuf_addf(&sb
, "%s/config", commondir
);
110 read_repository_format(format
, sb
.buf
);
113 if (verify_repository_format(format
, &sb
) < 0) {
114 warning("%s", sb
.buf
);
123 * Initialize 'repo' based on the provided 'gitdir'.
124 * Return 0 upon success and a non-zero value upon failure.
126 int repo_init(struct repository
*repo
, const char *gitdir
, const char *worktree
)
128 struct repository_format format
;
129 memset(repo
, 0, sizeof(*repo
));
131 repo
->ignore_env
= 1;
133 if (repo_init_gitdir(repo
, gitdir
))
136 if (read_and_verify_repository_format(&format
, repo
->commondir
))
140 repo_set_worktree(repo
, worktree
);
150 * Initialize 'submodule' as the submodule given by 'path' in parent repository
152 * Return 0 upon success and a non-zero value upon failure.
154 int repo_submodule_init(struct repository
*submodule
,
155 struct repository
*superproject
,
158 const struct submodule
*sub
;
159 struct strbuf gitdir
= STRBUF_INIT
;
160 struct strbuf worktree
= STRBUF_INIT
;
163 sub
= submodule_from_cache(superproject
, &null_oid
, path
);
169 strbuf_repo_worktree_path(&gitdir
, superproject
, "%s/.git", path
);
170 strbuf_repo_worktree_path(&worktree
, superproject
, "%s", path
);
172 if (repo_init(submodule
, gitdir
.buf
, worktree
.buf
)) {
174 * If initilization fails then it may be due to the submodule
175 * not being populated in the superproject's worktree. Instead
176 * we can try to initilize the submodule by finding it's gitdir
177 * in the superproject's 'modules' directory. In this case the
178 * submodule would not have a worktree.
180 strbuf_reset(&gitdir
);
181 strbuf_repo_git_path(&gitdir
, superproject
,
182 "modules/%s", sub
->name
);
184 if (repo_init(submodule
, gitdir
.buf
, NULL
)) {
190 submodule
->submodule_prefix
= xstrfmt("%s%s/",
191 superproject
->submodule_prefix
?
192 superproject
->submodule_prefix
:
196 strbuf_release(&gitdir
);
197 strbuf_release(&worktree
);
201 void repo_clear(struct repository
*repo
)
205 free(repo
->commondir
);
206 repo
->commondir
= NULL
;
207 free(repo
->objectdir
);
208 repo
->objectdir
= NULL
;
209 free(repo
->graft_file
);
210 repo
->graft_file
= NULL
;
211 free(repo
->index_file
);
212 repo
->index_file
= NULL
;
213 free(repo
->worktree
);
214 repo
->worktree
= NULL
;
215 free(repo
->submodule_prefix
);
216 repo
->submodule_prefix
= NULL
;
219 git_configset_clear(repo
->config
);
224 if (repo
->submodule_cache
) {
225 submodule_cache_free(repo
->submodule_cache
);
226 repo
->submodule_cache
= NULL
;
230 discard_index(repo
->index
);
236 int repo_read_index(struct repository
*repo
)
239 repo
->index
= xcalloc(1, sizeof(*repo
->index
));
241 return read_index_from(repo
->index
, repo
->index_file
);