diff --git a/pkg/git/fetch.go b/pkg/git/fetch.go index 8725045d3..990f30960 100644 --- a/pkg/git/fetch.go +++ b/pkg/git/fetch.go @@ -71,6 +71,23 @@ func (f Fetcher) Fetch(dir, gitURL, gitRevision, metadataDir string) error { return errors.Wrapf(err, "checking out revision") } + submodules, err := worktree.Submodules() + if err != nil { + return errors.Wrapf(err, "getting submodules") + } + + for _, submodule := range submodules { + f.Logger.Printf("Updating submodule %v", submodule.Config().URL) + submoduleAuth, err := f.Keychain.Resolve(submodule.Config().URL) + if err != nil { + return err + } + err = submodule.Update(&gogit.SubmoduleUpdateOptions{Auth: submoduleAuth, Init: true, RecurseSubmodules: gogit.DefaultSubmoduleRecursionDepth }) + if err != nil { + return errors.Wrapf(err, "updating submodules") + } + } + projectMetadataFile, err := os.Create(path.Join(metadataDir, "project-metadata.toml")) if err != nil { return errors.Wrapf(err, "invalid metadata destination '%s/project-metadata.toml' for git repository: %s", metadataDir, gitURL) diff --git a/pkg/git/fetch_test.go b/pkg/git/fetch_test.go index 7d32ddd07..a0784f69d 100644 --- a/pkg/git/fetch_test.go +++ b/pkg/git/fetch_test.go @@ -106,6 +106,16 @@ func testGitCheckout(t *testing.T, when spec.G, it spec.S) { err := fetcher.Fetch(testDir, "git@bitbucket.com:org/repo", "main", metadataDir) require.ErrorContains(t, err, "unable to fetch references for repository") }) + + it("initializes submodules", func() { + err := fetcher.Fetch(testDir, "https://github.com/git-fixtures/submodule", "master", metadataDir) + require.NoError(t, err) + + _, err = os.Lstat(path.Join(testDir, "basic", ".gitignore")) + require.NoError(t, err) + + + }) }) }