@@ -2,15 +2,18 @@ package command
22
33import (
44 "fmt"
5+ "os"
56 "strings"
67
8+ "github.com/cli/cli/git"
79 "github.com/cli/cli/internal/ghrepo"
810 "github.com/cli/cli/utils"
911 "github.com/spf13/cobra"
1012)
1113
1214func init () {
1315 RootCmd .AddCommand (repoCmd )
16+ repoCmd .AddCommand (repoCloneCmd )
1417 repoCmd .AddCommand (repoViewCmd )
1518}
1619
@@ -24,6 +27,16 @@ A repository can be supplied as an argument in any of the following formats:
2427- by URL, e.g. "https://github.com/OWNER/REPO"` ,
2528}
2629
30+ var repoCloneCmd = & cobra.Command {
31+ Use : "clone <repo>" ,
32+ Args : cobra .MinimumNArgs (1 ),
33+ Short : "Clone a repository locally" ,
34+ Long : `Clone a GitHub repository locally.
35+
36+ To pass 'git clone' options, separate them with '--'.` ,
37+ RunE : repoClone ,
38+ }
39+
2740var repoViewCmd = & cobra.Command {
2841 Use : "view [<repo>]" ,
2942 Short : "View a repository in the browser" ,
@@ -33,6 +46,23 @@ With no argument, the repository for the current directory is opened.`,
3346 RunE : repoView ,
3447}
3548
49+ func repoClone (cmd * cobra.Command , args []string ) error {
50+ cloneURL := args [0 ]
51+ if ! strings .Contains (cloneURL , ":" ) {
52+ cloneURL = fmt .Sprintf ("https://github.com/%s.git" , cloneURL )
53+ }
54+
55+ cloneArgs := []string {"clone" }
56+ cloneArgs = append (cloneArgs , args [1 :]... )
57+ cloneArgs = append (cloneArgs , cloneURL )
58+
59+ cloneCmd := git .GitCommand (cloneArgs ... )
60+ cloneCmd .Stdin = os .Stdin
61+ cloneCmd .Stdout = os .Stdout
62+ cloneCmd .Stderr = os .Stderr
63+ return utils .PrepareCmd (cloneCmd ).Run ()
64+ }
65+
3666func repoView (cmd * cobra.Command , args []string ) error {
3767 ctx := contextForCommand (cmd )
3868
0 commit comments