|
| 1 | +package mono |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "spark/internal/git" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +func AddExistingReposAsSubmodules(parentDir string, repoPaths []string) error { |
| 13 | + absParent, err := filepath.Abs(parentDir) |
| 14 | + if err != nil { |
| 15 | + return fmt.Errorf("failed to resolve parent dir: %w", err) |
| 16 | + } |
| 17 | + |
| 18 | + if !git.IsGitRepository(absParent) { |
| 19 | + fmt.Println("Initializing git repository...") |
| 20 | + cmd := exec.Command("git", "init") |
| 21 | + cmd.Dir = absParent |
| 22 | + cmd.Stdout = os.Stdout |
| 23 | + cmd.Stderr = os.Stderr |
| 24 | + if err := cmd.Run(); err != nil { |
| 25 | + return fmt.Errorf("failed to init git repo: %w", err) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + modulesBase := filepath.Join(absParent, ".git", "modules") |
| 30 | + if err := os.MkdirAll(modulesBase, 0755); err != nil { |
| 31 | + return fmt.Errorf("failed to create modules dir: %w", err) |
| 32 | + } |
| 33 | + |
| 34 | + gitmodulesPath := filepath.Join(absParent, ".gitmodules") |
| 35 | + |
| 36 | + for _, repoPath := range repoPaths { |
| 37 | + absRepo, err := filepath.Abs(repoPath) |
| 38 | + if err != nil { |
| 39 | + fmt.Printf("Warning: Could not resolve path %s: %v\n", repoPath, err) |
| 40 | + continue |
| 41 | + } |
| 42 | + |
| 43 | + repoName := filepath.Base(absRepo) |
| 44 | + remoteURL, err := git.GetRemoteURL(absRepo) |
| 45 | + if err != nil { |
| 46 | + fmt.Printf("Warning: Could not get remote URL for %s: %v\n", repoName, err) |
| 47 | + continue |
| 48 | + } |
| 49 | + |
| 50 | + // Skip if already a submodule (.git is a file pointing to modules) |
| 51 | + gitEntry := filepath.Join(absRepo, ".git") |
| 52 | + if info, err := os.Lstat(gitEntry); err == nil && !info.IsDir() { |
| 53 | + fmt.Printf("Skipping %s: already a submodule\n", repoName) |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Printf("Adding submodule: %s (%s)\n", repoName, remoteURL) |
| 58 | + |
| 59 | + // Move .git directory to .git/modules/<name> |
| 60 | + targetModuleDir := filepath.Join(modulesBase, repoName) |
| 61 | + if _, err := os.Stat(targetModuleDir); err == nil { |
| 62 | + fmt.Printf("Warning: Module dir already exists for %s, skipping\n", repoName) |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + if err := os.Rename(gitEntry, targetModuleDir); err != nil { |
| 67 | + fmt.Printf("Warning: Failed to move .git for %s: %v\n", repoName, err) |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + // Create .git file pointing to modules dir |
| 72 | + relPath := filepath.Join("..", ".git", "modules", repoName) |
| 73 | + if err := os.WriteFile(gitEntry, []byte("gitdir: "+relPath+"\n"), 0644); err != nil { |
| 74 | + fmt.Printf("Warning: Failed to create .git file for %s: %v\n", repoName, err) |
| 75 | + // Try to restore |
| 76 | + os.Rename(targetModuleDir, gitEntry) |
| 77 | + continue |
| 78 | + } |
| 79 | + |
| 80 | + // Update core.worktree in the module config |
| 81 | + setWorktreeCmd := exec.Command("git", "config", "--file", |
| 82 | + filepath.Join(targetModuleDir, "config"), |
| 83 | + "core.worktree", "../../../"+repoName) |
| 84 | + setWorktreeCmd.Dir = absParent |
| 85 | + if err := setWorktreeCmd.Run(); err != nil { |
| 86 | + fmt.Printf("Warning: Failed to set worktree for %s: %v\n", repoName, err) |
| 87 | + } |
| 88 | + |
| 89 | + // Add entry to .gitmodules |
| 90 | + setPathCmd := exec.Command("git", "config", "--file", gitmodulesPath, |
| 91 | + fmt.Sprintf("submodule.%s.path", repoName), repoName) |
| 92 | + setPathCmd.Dir = absParent |
| 93 | + setPathCmd.Stdout = os.Stdout |
| 94 | + setPathCmd.Stderr = os.Stderr |
| 95 | + if err := setPathCmd.Run(); err != nil { |
| 96 | + fmt.Printf("Warning: Failed to set .gitmodules path for %s: %v\n", repoName, err) |
| 97 | + continue |
| 98 | + } |
| 99 | + |
| 100 | + setURLCmd := exec.Command("git", "config", "--file", gitmodulesPath, |
| 101 | + fmt.Sprintf("submodule.%s.url", repoName), remoteURL) |
| 102 | + setURLCmd.Dir = absParent |
| 103 | + setURLCmd.Stdout = os.Stdout |
| 104 | + setURLCmd.Stderr = os.Stderr |
| 105 | + if err := setURLCmd.Run(); err != nil { |
| 106 | + fmt.Printf("Warning: Failed to set .gitmodules url for %s: %v\n", repoName, err) |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + // Stage the submodule |
| 111 | + addCmd := exec.Command("git", "add", repoName) |
| 112 | + addCmd.Dir = absParent |
| 113 | + if err := addCmd.Run(); err != nil { |
| 114 | + fmt.Printf("Warning: Failed to stage submodule %s: %v\n", repoName, err) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Stage .gitmodules if it exists |
| 119 | + if _, err := os.Stat(gitmodulesPath); err == nil { |
| 120 | + addModulesCmd := exec.Command("git", "add", ".gitmodules") |
| 121 | + addModulesCmd.Dir = absParent |
| 122 | + if err := addModulesCmd.Run(); err != nil { |
| 123 | + fmt.Printf("Warning: Failed to stage .gitmodules: %v\n", err) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func FindSubRepos(parentDir string) ([]string, error) { |
| 131 | + absParent, err := filepath.Abs(parentDir) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + |
| 136 | + entries, err := os.ReadDir(absParent) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + var repos []string |
| 142 | + for _, entry := range entries { |
| 143 | + if !entry.IsDir() { |
| 144 | + continue |
| 145 | + } |
| 146 | + if strings.HasPrefix(entry.Name(), ".") { |
| 147 | + continue |
| 148 | + } |
| 149 | + dirPath := filepath.Join(absParent, entry.Name()) |
| 150 | + if git.IsGitRepository(dirPath) { |
| 151 | + repos = append(repos, dirPath) |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return repos, nil |
| 156 | +} |
0 commit comments