| 311 | } |
| 312 | |
| 313 | int repo_submodule_init(struct repository *subrepo, |
| 314 | struct repository *superproject, |
| 315 | const char *path, |
| 316 | const struct object_id *treeish_name) |
| 317 | { |
| 318 | struct strbuf gitdir = STRBUF_INIT; |
| 319 | struct strbuf worktree = STRBUF_INIT; |
| 320 | int ret = 0; |
| 321 | |
| 322 | repo_worktree_path_append(superproject, &gitdir, "%s/.git", path); |
| 323 | repo_worktree_path_append(superproject, &worktree, "%s", path); |
| 324 | |
| 325 | if (repo_init(subrepo, gitdir.buf, worktree.buf)) { |
| 326 | /* |
| 327 | * If initialization fails then it may be due to the submodule |
| 328 | * not being populated in the superproject's worktree. Instead |
| 329 | * we can try to initialize the submodule by finding it's gitdir |
| 330 | * in the superproject's 'modules' directory. In this case the |
| 331 | * submodule would not have a worktree. |
| 332 | */ |
| 333 | const struct submodule *sub = |
| 334 | submodule_from_path(superproject, treeish_name, path); |
| 335 | if (!sub) { |
| 336 | ret = -1; |
| 337 | goto out; |
| 338 | } |
| 339 | |
| 340 | strbuf_reset(&gitdir); |
| 341 | submodule_name_to_gitdir(&gitdir, superproject, sub->name); |
| 342 | |
| 343 | if (repo_init(subrepo, gitdir.buf, NULL)) { |
| 344 | ret = -1; |
| 345 | goto out; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | subrepo->submodule_prefix = xstrfmt("%s%s/", |
| 350 | superproject->submodule_prefix ? |
| 351 | superproject->submodule_prefix : |
| 352 | "", path); |
| 353 | |
| 354 | out: |
| 355 | strbuf_release(&gitdir); |
| 356 | strbuf_release(&worktree); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | static void repo_clear_path_cache(struct repo_path_cache *cache) |
| 361 | { |