@@ -89,31 +89,41 @@ func addFolderAsSubmodules(repoPath, folder string) error {
8989 return fmt .Errorf ("failed to resolve path: %w" , err )
9090 }
9191
92- entries , err := os .ReadDir (absFolder )
93- if err != nil {
94- return fmt .Errorf ("failed to read directory: %w" , err )
95- }
96-
9792 var repos []string
98- for _ , entry := range entries {
99- if ! entry .IsDir () {
100- continue
101- }
102- if strings .HasPrefix (entry .Name (), "." ) {
103- continue
104- }
10593
106- childPath := filepath .Join (absFolder , entry .Name ())
107- if ! git .IsGitRepository (childPath ) {
108- continue
94+ if git .IsGitRepository (absFolder ) {
95+ isGitHub , _ := git .IsGitHubRepo (absFolder )
96+ if isGitHub {
97+ repos = append (repos , absFolder )
10998 }
99+ }
110100
111- isGitHub , _ := git .IsGitHubRepo (childPath )
112- if ! isGitHub {
113- continue
101+ if len (repos ) == 0 {
102+ entries , err := os .ReadDir (absFolder )
103+ if err != nil {
104+ return fmt .Errorf ("failed to read directory: %w" , err )
114105 }
115106
116- repos = append (repos , childPath )
107+ for _ , entry := range entries {
108+ if ! entry .IsDir () {
109+ continue
110+ }
111+ if strings .HasPrefix (entry .Name (), "." ) {
112+ continue
113+ }
114+
115+ childPath := filepath .Join (absFolder , entry .Name ())
116+ if ! git .IsGitRepository (childPath ) {
117+ continue
118+ }
119+
120+ isGitHub , _ := git .IsGitHubRepo (childPath )
121+ if ! isGitHub {
122+ continue
123+ }
124+
125+ repos = append (repos , childPath )
126+ }
117127 }
118128
119129 if len (repos ) == 0 {
@@ -132,7 +142,12 @@ func addFolderAsSubmodules(repoPath, folder string) error {
132142 }
133143
134144 fmt .Printf ("Adding submodule: %s (%s)\n " , name , url )
135- if err := addLocalRepoAsSubmodule (repoPath , repo , url ); err != nil {
145+
146+ cmd := exec .Command ("git" , "submodule" , "add" , "-f" , url , name )
147+ cmd .Dir = repoPath
148+ cmd .Stdout = os .Stdout
149+ cmd .Stderr = os .Stderr
150+ if err := cmd .Run (); err != nil {
136151 fmt .Printf ("Warning: failed to add %s: %v\n " , name , err )
137152 }
138153 }
@@ -141,63 +156,6 @@ func addFolderAsSubmodules(repoPath, folder string) error {
141156 return nil
142157}
143158
144- func addLocalRepoAsSubmodule (parentPath , childPath , remoteURL string ) error {
145- childName := filepath .Base (childPath )
146-
147- gitmodulesPath := filepath .Join (parentPath , ".gitmodules" )
148-
149- modulesDir := filepath .Join (parentPath , ".git" , "modules" , childName )
150- if err := os .MkdirAll (modulesDir , 0755 ); err != nil {
151- return fmt .Errorf ("failed to create modules dir: %w" , err )
152- }
153-
154- gitEntry := filepath .Join (childPath , ".git" )
155- if info , err := os .Lstat (gitEntry ); err == nil && ! info .IsDir () {
156- fmt .Printf ("Skipping %s: already a submodule\n " , childName )
157- return nil
158- }
159-
160- for _ , item := range []string {"config" , "description" , "hooks" , "info" , "logs" , "objects" , "refs" } {
161- src := filepath .Join (gitEntry , item )
162- if _ , err := os .Stat (src ); err == nil {
163- dst := filepath .Join (modulesDir , item )
164- os .Rename (src , dst )
165- }
166- }
167-
168- for _ , item := range []string {"HEAD" , "index" , "packed-refs" } {
169- src := filepath .Join (gitEntry , item )
170- if _ , err := os .Stat (src ); err == nil {
171- dst := filepath .Join (modulesDir , item )
172- os .Rename (src , dst )
173- }
174- }
175- os .Remove (gitEntry )
176-
177- relPath := filepath .Join (".." , ".git" , "modules" , childName )
178- os .WriteFile (gitEntry , []byte ("gitdir: " + relPath + "\n " ), 0644 )
179-
180- submoduleConfig := filepath .Join (modulesDir , "config" )
181- worktree := filepath .Join (".." , ".." , ".." , childName )
182- exec .Command ("git" , "config" , "--file" , submoduleConfig , "core.worktree" , worktree ).Run ()
183-
184- setPath := exec .Command ("git" , "config" , "--file" , gitmodulesPath , fmt .Sprintf ("submodule.%s.path" , childName ), childName )
185- setPath .Dir = parentPath
186- setPath .Stdout = os .Stdout
187- setPath .Stderr = os .Stderr
188- setPath .Run ()
189-
190- setURL := exec .Command ("git" , "config" , "--file" , gitmodulesPath , fmt .Sprintf ("submodule.%s.url" , childName ), remoteURL )
191- setURL .Dir = parentPath
192- setURL .Stdout = os .Stdout
193- setURL .Stderr = os .Stderr
194- setURL .Run ()
195-
196- exec .Command ("git" , "add" , childName ).Run ()
197-
198- return nil
199- }
200-
201159func extractRepoName (url string ) string {
202160 url = strings .TrimSuffix (url , ".git" )
203161 if strings .Contains (url , "://" ) {
0 commit comments