144
return -1;
145
}
146
147
+/*
148
+ * Initialize 'submodule' as the submodule given by 'path' in parent repository
149
+ * 'superproject'.
150
+ * Return 0 upon success and a non-zero value upon failure.
151
+ */
152
+int repo_submodule_init(struct repository *submodule,
153
+ struct repository *superproject,
154
+ const char *path)
155
+{
156
+ const struct submodule *sub;
157
+ struct strbuf gitdir = STRBUF_INIT;
158
+ struct strbuf worktree = STRBUF_INIT;
159
+ int ret = 0;
160
+
161
+ sub = submodule_from_cache(superproject, null_sha1, path);
162
+ if (!sub) {
163
+ ret = -1;
164
+ goto out;
165
+ }
166
+
167
+ strbuf_repo_worktree_path(&gitdir, superproject, "%s/.git", path);
168
+ strbuf_repo_worktree_path(&worktree, superproject, "%s", path);
169
+
170
+ if (repo_init(submodule, gitdir.buf, worktree.buf)) {
171
+ /*
172
+ * If initilization fails then it may be due to the submodule
173
+ * not being populated in the superproject's worktree. Instead
174
+ * we can try to initilize the submodule by finding it's gitdir
175
+ * in the superproject's 'modules' directory. In this case the
176
+ * submodule would not have a worktree.
177
+ */
178
+ strbuf_reset(&gitdir);
179
+ strbuf_repo_git_path(&gitdir, superproject,
180
+ "modules/%s", sub->name);
181
+
182
+ if (repo_init(submodule, gitdir.buf, NULL)) {
183
+ ret = -1;
184
+ goto out;
185
+ }
186
+ }
187
+
188
+ submodule->submodule_prefix = xstrfmt("%s%s/",
189
+ superproject->submodule_prefix ?
190
+ superproject->submodule_prefix :
191
+ "", path);
192
+
193
+out:
194
+ strbuf_release(&gitdir);
195
+ strbuf_release(&worktree);
196
+ return ret;
197
+}
198
+
199
void repo_clear(struct repository *repo)
200
{
201
free(repo->gitdir);
210
repo->index_file = NULL;
211
free(repo->worktree);
212
repo->worktree = NULL;
213
+ free(repo->submodule_prefix);
214
+ repo->submodule_prefix = NULL;
215
216
if (repo->config) {
217
git_configset_clear(repo->config);