@@ -831,10 +831,13 @@ func prepareDeltaBuild(options Options, repository *git.Repository) (repos map[f
831831
832832func prepareNormalBuild (options Options , repository * git.Repository ) (repos map [fileKey ]BlobLocation , branchVersions map [string ]map [string ]plumbing.Hash , err error ) {
833833 var repoCache * RepoCache
834- if options .Submodules {
834+ if options .Submodules && options . RepoCacheDir != "" {
835835 repoCache = NewRepoCache (options .RepoCacheDir )
836836 }
837+ return prepareNormalBuildRecurse (options , repository , repoCache , false )
838+ }
837839
840+ func prepareNormalBuildRecurse (options Options , repository * git.Repository , repoCache * RepoCache , isSubrepo bool ) (repos map [fileKey ]BlobLocation , branchVersions map [string ]map [string ]plumbing.Hash , err error ) {
838841 // Branch => Repo => SHA1
839842 branchVersions = map [string ]map [string ]plumbing.Hash {}
840843
@@ -843,7 +846,22 @@ func prepareNormalBuild(options Options, repository *git.Repository) (repos map[
843846 return nil , nil , fmt .Errorf ("expandBranches: %w" , err )
844847 }
845848
846- rw := NewRepoWalker (repository , options .BuildOptions .RepositoryDescription .URL , repoCache )
849+ repoURL := options .BuildOptions .RepositoryDescription .URL
850+
851+ if isSubrepo {
852+ cfg , err := repository .Config ()
853+ if err != nil {
854+ return nil , nil , fmt .Errorf ("unable to get repository config: %w" , err )
855+ }
856+
857+ u , err := normalizeSubmoduleRemoteURL (cfg )
858+ if err != nil {
859+ return nil , nil , fmt .Errorf ("failed to identify subrepository URL: %w" , err )
860+ }
861+ repoURL = u .String ()
862+ }
863+
864+ rw := NewRepoWalker (repository , repoURL , repoCache )
847865 for _ , b := range branches {
848866 commit , err := getCommit (repository , options .BranchPrefix , b )
849867 if err != nil {
@@ -872,6 +890,47 @@ func prepareNormalBuild(options Options, repository *git.Repository) (repos map[
872890 branchVersions [b ] = subVersions
873891 }
874892
893+ // Index submodules using go-git if we didn't do so using the repo cache
894+ if options .Submodules && options .RepoCacheDir == "" {
895+ worktree , err := repository .Worktree ()
896+ if err != nil {
897+ return nil , nil , fmt .Errorf ("failed to get repository worktree: %w" , err )
898+ }
899+
900+ submodules , err := worktree .Submodules ()
901+ if err != nil {
902+ return nil , nil , fmt .Errorf ("failed to get submodules: %w" , err )
903+ }
904+
905+ for _ , submodule := range submodules {
906+ subRepository , err := submodule .Repository ()
907+ if err != nil {
908+ log .Printf ("failed to open submodule repository: %s, %s" , submodule .Config ().Name , err )
909+ continue
910+ }
911+
912+ sw , subVersions , err := prepareNormalBuildRecurse (options , subRepository , repoCache , true )
913+ if err != nil {
914+ log .Printf ("failed to index submodule repository: %s, %s" , submodule .Config ().Name , err )
915+ continue
916+ }
917+
918+ log .Printf ("adding subrepository files from: %s" , submodule .Config ().Name )
919+
920+ for k , repo := range sw {
921+ rw .Files [fileKey {
922+ SubRepoPath : filepath .Join (submodule .Config ().Path , k .SubRepoPath ),
923+ Path : k .Path ,
924+ ID : k .ID ,
925+ }] = repo
926+ }
927+
928+ for k , v := range subVersions {
929+ branchVersions [filepath .Join (submodule .Config ().Path , k )] = v
930+ }
931+ }
932+ }
933+
875934 return rw .Files , branchVersions , nil
876935}
877936
0 commit comments