|
5 | 5 | "os" |
6 | 6 | "os/exec" |
7 | 7 | "path/filepath" |
| 8 | + "regexp" |
8 | 9 | "spark/internal/git" |
9 | 10 | "strings" |
10 | 11 | ) |
@@ -154,3 +155,98 @@ func FindSubRepos(parentDir string) ([]string, error) { |
154 | 155 |
|
155 | 156 | return repos, nil |
156 | 157 | } |
| 158 | + |
| 159 | +// ExtractRepoName extracts repository name from a git URL |
| 160 | +func ExtractRepoName(repoURL string) string { |
| 161 | + // Remove trailing .git if present |
| 162 | + repoURL = strings.TrimSuffix(repoURL, ".git") |
| 163 | + |
| 164 | + // Handle different URL formats |
| 165 | + // HTTPS: https://github.com/user/repo |
| 166 | + // SSH: git@github.com:user/repo |
| 167 | + // Simple: user/repo |
| 168 | + |
| 169 | + // Try SSH format first |
| 170 | + sshPattern := regexp.MustCompile(`git@[^:]+:([^/]+/[^/]+)$`) |
| 171 | + if matches := sshPattern.FindStringSubmatch(repoURL); len(matches) > 1 { |
| 172 | + parts := strings.Split(matches[1], "/") |
| 173 | + return parts[len(parts)-1] |
| 174 | + } |
| 175 | + |
| 176 | + // Try HTTPS format |
| 177 | + if strings.Contains(repoURL, "://") { |
| 178 | + parts := strings.Split(repoURL, "/") |
| 179 | + if len(parts) > 0 { |
| 180 | + return parts[len(parts)-1] |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + // Simple format: user/repo or just repo |
| 185 | + parts := strings.Split(repoURL, "/") |
| 186 | + return parts[len(parts)-1] |
| 187 | +} |
| 188 | + |
| 189 | +// AddRemoteRepoAsSubmodule adds a remote git repository as a submodule |
| 190 | +func AddRemoteRepoAsSubmodule(parentDir string, repoURL string, submodulePath string) error { |
| 191 | + absParent, err := filepath.Abs(parentDir) |
| 192 | + if err != nil { |
| 193 | + return fmt.Errorf("failed to resolve parent dir: %w", err) |
| 194 | + } |
| 195 | + |
| 196 | + // Determine submodule name/path |
| 197 | + if submodulePath == "" { |
| 198 | + submodulePath = ExtractRepoName(repoURL) |
| 199 | + } |
| 200 | + |
| 201 | + // Remove trailing .git for the path name |
| 202 | + submodulePath = strings.TrimSuffix(submodulePath, ".git") |
| 203 | + |
| 204 | + // Check if destination already exists |
| 205 | + targetPath := filepath.Join(absParent, submodulePath) |
| 206 | + if _, err := os.Stat(targetPath); err == nil { |
| 207 | + return fmt.Errorf("destination path already exists: %s", submodulePath) |
| 208 | + } |
| 209 | + |
| 210 | + // Initialize git repo if not exists |
| 211 | + if !git.IsGitRepository(absParent) { |
| 212 | + fmt.Println("Initializing git repository...") |
| 213 | + cmd := exec.Command("git", "init") |
| 214 | + cmd.Dir = absParent |
| 215 | + cmd.Stdout = os.Stdout |
| 216 | + cmd.Stderr = os.Stderr |
| 217 | + if err := cmd.Run(); err != nil { |
| 218 | + return fmt.Errorf("failed to init git repo: %w", err) |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + // Check if already a submodule |
| 223 | + gitmodulesPath := filepath.Join(absParent, ".gitmodules") |
| 224 | + if _, err := os.Stat(gitmodulesPath); err == nil { |
| 225 | + // Check if submodule already exists |
| 226 | + checkCmd := exec.Command("git", "config", "--file", gitmodulesPath, fmt.Sprintf("submodule.%s.path", submodulePath)) |
| 227 | + checkCmd.Dir = absParent |
| 228 | + if err := checkCmd.Run(); err == nil { |
| 229 | + return fmt.Errorf("submodule '%s' already exists", submodulePath) |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + fmt.Printf("Adding submodule: %s (%s)\n", submodulePath, repoURL) |
| 234 | + |
| 235 | + // Add submodule using git submodule add |
| 236 | + cmd := exec.Command("git", "submodule", "add", "-f", repoURL, submodulePath) |
| 237 | + cmd.Dir = absParent |
| 238 | + cmd.Stdout = os.Stdout |
| 239 | + cmd.Stderr = os.Stderr |
| 240 | + if err := cmd.Run(); err != nil { |
| 241 | + return fmt.Errorf("failed to add submodule: %w", err) |
| 242 | + } |
| 243 | + |
| 244 | + // Stage .gitmodules |
| 245 | + addModulesCmd := exec.Command("git", "add", ".gitmodules") |
| 246 | + addModulesCmd.Dir = absParent |
| 247 | + if err := addModulesCmd.Run(); err != nil { |
| 248 | + fmt.Printf("Warning: Failed to stage .gitmodules: %v\n", err) |
| 249 | + } |
| 250 | + |
| 251 | + return nil |
| 252 | +} |
0 commit comments