|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "spark/internal/github" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + batchCloneUseSSH bool |
| 15 | + batchCloneInclude string |
| 16 | + batchCloneExclude string |
| 17 | + batchCloneIncludeFork bool |
| 18 | + batchCloneOutput string |
| 19 | +) |
| 20 | + |
| 21 | +var batchCloneCmd = &cobra.Command{ |
| 22 | + Use: "batch-clone <account-name-or-url>", |
| 23 | + Short: "Clone all repositories from a GitHub organization or user", |
| 24 | + Long: `Clone all repositories from a GitHub organization or user account. |
| 25 | +
|
| 26 | +This command will: |
| 27 | +1. Detect whether the account is an organization or a user |
| 28 | +2. Fetch all public repositories from the specified account |
| 29 | +3. Clone each repository to the current directory (or specified output directory) |
| 30 | +
|
| 31 | +You can provide either the account name (e.g., "jackwener" or "variableway") |
| 32 | +or the full GitHub URL (e.g., "https://github.com/jackwener").`, |
| 33 | + Args: cobra.ExactArgs(1), |
| 34 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 35 | + input := args[0] |
| 36 | + |
| 37 | + accountName, err := github.ParseAccountFromURL(input) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + |
| 42 | + outputDir := batchCloneOutput |
| 43 | + if outputDir == "" { |
| 44 | + outputDir = "." |
| 45 | + } |
| 46 | + |
| 47 | + fmt.Printf("Detecting account type for: %s\n", accountName) |
| 48 | + |
| 49 | + repos, accountType, err := github.GetReposForAccount(accountName) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + accountTypeLabel := "organization" |
| 55 | + if accountType == github.AccountTypeUser { |
| 56 | + accountTypeLabel = "user" |
| 57 | + } |
| 58 | + fmt.Printf("Found %d repositories for %s: %s\n\n", len(repos), accountTypeLabel, accountName) |
| 59 | + |
| 60 | + var reposToClone []github.Repository |
| 61 | + for _, repo := range repos { |
| 62 | + if !batchCloneIncludeFork && repo.Fork { |
| 63 | + continue |
| 64 | + } |
| 65 | + |
| 66 | + if batchCloneExclude != "" && matchesPattern(repo.Name, batchCloneExclude) { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + if batchCloneInclude != "" && !matchesPattern(repo.Name, batchCloneInclude) { |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + reposToClone = append(reposToClone, repo) |
| 75 | + } |
| 76 | + |
| 77 | + fmt.Printf("Cloning %d repositories...\n\n", len(reposToClone)) |
| 78 | + |
| 79 | + successCount := 0 |
| 80 | + skipCount := 0 |
| 81 | + failCount := 0 |
| 82 | + |
| 83 | + for i, repo := range reposToClone { |
| 84 | + fmt.Printf("[%d/%d] ", i+1, len(reposToClone)) |
| 85 | + |
| 86 | + repoPath := fmt.Sprintf("%s/%s", outputDir, repo.Name) |
| 87 | + if _, err := os.Stat(repoPath); !os.IsNotExist(err) { |
| 88 | + fmt.Printf("Skipping %s (already exists)\n", repo.Name) |
| 89 | + skipCount++ |
| 90 | + continue |
| 91 | + } |
| 92 | + |
| 93 | + var cloneURL string |
| 94 | + if batchCloneUseSSH { |
| 95 | + cloneURL = repo.SSHURL |
| 96 | + } else { |
| 97 | + cloneURL = repo.CloneURL |
| 98 | + } |
| 99 | + |
| 100 | + fmt.Printf("Cloning %s...\n", repo.Name) |
| 101 | + |
| 102 | + cloneCmd := exec.Command("git", "clone", cloneURL, repoPath) |
| 103 | + cloneCmd.Stdout = os.Stdout |
| 104 | + cloneCmd.Stderr = os.Stderr |
| 105 | + |
| 106 | + if err := cloneCmd.Run(); err != nil { |
| 107 | + fmt.Printf(" Error: failed to clone %s: %v\n", repo.Name, err) |
| 108 | + failCount++ |
| 109 | + } else { |
| 110 | + fmt.Printf(" Successfully cloned %s\n", repo.Name) |
| 111 | + successCount++ |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + fmt.Printf("\n--- Summary ---\n") |
| 116 | + fmt.Printf("Cloned: %d\n", successCount) |
| 117 | + fmt.Printf("Skipped: %d\n", skipCount) |
| 118 | + fmt.Printf("Failed: %d\n", failCount) |
| 119 | + |
| 120 | + return nil |
| 121 | + }, |
| 122 | +} |
| 123 | + |
| 124 | +func matchesPattern(name, pattern string) bool { |
| 125 | + if pattern == "" { |
| 126 | + return true |
| 127 | + } |
| 128 | + |
| 129 | + patterns := strings.Split(pattern, ",") |
| 130 | + for _, p := range patterns { |
| 131 | + p = strings.TrimSpace(p) |
| 132 | + if p != "" && strings.Contains(name, p) { |
| 133 | + return true |
| 134 | + } |
| 135 | + } |
| 136 | + return false |
| 137 | +} |
| 138 | + |
| 139 | +func init() { |
| 140 | + GitCmd.AddCommand(batchCloneCmd) |
| 141 | + |
| 142 | + batchCloneCmd.Flags().BoolVar(&batchCloneUseSSH, "ssh", false, "Use SSH URLs instead of HTTPS") |
| 143 | + batchCloneCmd.Flags().StringVar(&batchCloneInclude, "include", "", "Include only repos matching pattern (comma-separated)") |
| 144 | + batchCloneCmd.Flags().StringVar(&batchCloneExclude, "exclude", "", "Exclude repos matching pattern (comma-separated)") |
| 145 | + batchCloneCmd.Flags().BoolVar(&batchCloneIncludeFork, "include-forks", false, "Include forked repositories") |
| 146 | + batchCloneCmd.Flags().StringVarP(&batchCloneOutput, "output", "o", ".", "Output directory for cloned repositories") |
| 147 | +} |
0 commit comments