|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var cloneCmd = &cobra.Command{ |
| 14 | + Use: "clone <url-or-slug> [directory] [-- <git-args>]", |
| 15 | + Short: "Clone a GitHub repository using gh repo clone (defaults to SSH)", |
| 16 | + Long: `Clone a GitHub repository using the GitHub CLI ('gh repo clone'). |
| 17 | +
|
| 18 | +This command converts various GitHub URL formats into the owner/repo slug |
| 19 | +and invokes 'gh repo clone', which defaults to SSH for better stability |
| 20 | +in regions where HTTPS connections to GitHub are unreliable. |
| 21 | +
|
| 22 | +Supported inputs: |
| 23 | + - https://github.com/owner/repo.git |
| 24 | + - git@github.com:owner/repo.git |
| 25 | + - github.com/owner/repo |
| 26 | + - owner/repo |
| 27 | +
|
| 28 | +Extra arguments after '--' are forwarded to 'git clone'.`, |
| 29 | + Example: ` spark git clone https://github.com/Nutlope/pdf-to-interactive-lesson.git |
| 30 | + spark git clone Nutlope/pdf-to-interactive-lesson |
| 31 | + spark git clone https://github.com/owner/repo.git my-dir -- --branch main --depth 1`, |
| 32 | + Args: cobra.MinimumNArgs(1), |
| 33 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 34 | + input := args[0] |
| 35 | + |
| 36 | + slug, err := parseRepoSlug(input) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + cmdPath := cmd.CommandPath() |
| 42 | + cmdArgs := resolveCmdArgs(cmdPath) |
| 43 | + |
| 44 | + var dir string |
| 45 | + var gitArgs []string |
| 46 | + if len(cmdArgs) > 1 { |
| 47 | + sepIdx := -1 |
| 48 | + for i, a := range cmdArgs[1:] { |
| 49 | + if a == "--" { |
| 50 | + sepIdx = i + 1 |
| 51 | + break |
| 52 | + } |
| 53 | + } |
| 54 | + if sepIdx == -1 { |
| 55 | + dir = cmdArgs[1] |
| 56 | + } else { |
| 57 | + if sepIdx > 1 { |
| 58 | + dir = cmdArgs[1] |
| 59 | + } |
| 60 | + if sepIdx+1 < len(cmdArgs) { |
| 61 | + gitArgs = cmdArgs[sepIdx+1:] |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + ghArgs := []string{"repo", "clone", slug} |
| 67 | + if dir != "" { |
| 68 | + ghArgs = append(ghArgs, dir) |
| 69 | + } |
| 70 | + if len(gitArgs) > 0 { |
| 71 | + ghArgs = append(ghArgs, "--") |
| 72 | + ghArgs = append(ghArgs, gitArgs...) |
| 73 | + } |
| 74 | + |
| 75 | + fmt.Printf("Cloning %s via gh (SSH)...\n", slug) |
| 76 | + c := exec.Command("gh", ghArgs...) |
| 77 | + c.Stdout = os.Stdout |
| 78 | + c.Stderr = os.Stderr |
| 79 | + c.Stdin = os.Stdin |
| 80 | + return c.Run() |
| 81 | + }, |
| 82 | +} |
| 83 | + |
| 84 | +func resolveCmdArgs(cmdPath string) []string { |
| 85 | + parts := strings.Fields(cmdPath) |
| 86 | + if len(parts) == 0 { |
| 87 | + return os.Args[1:] |
| 88 | + } |
| 89 | + if len(os.Args) <= len(parts) { |
| 90 | + return nil |
| 91 | + } |
| 92 | + return os.Args[len(parts):] |
| 93 | +} |
| 94 | + |
| 95 | +var repoSlugRegex = regexp.MustCompile(`^(?:https?://(?:www\.)?github\.com/|git@github\.com:|(?:www\.)?github\.com/)?([^/]+/[^/]+?)(?:\.git)?/?$`) |
| 96 | + |
| 97 | +func parseRepoSlug(input string) (string, error) { |
| 98 | + input = strings.TrimSpace(input) |
| 99 | + input = strings.TrimSuffix(input, "/") |
| 100 | + |
| 101 | + matches := repoSlugRegex.FindStringSubmatch(input) |
| 102 | + if len(matches) == 2 { |
| 103 | + return matches[1], nil |
| 104 | + } |
| 105 | + |
| 106 | + return "", fmt.Errorf("unable to parse GitHub repository from %q", input) |
| 107 | +} |
| 108 | + |
| 109 | +func init() { |
| 110 | + GitCmd.AddCommand(cloneCmd) |
| 111 | +} |
0 commit comments